Add Amlogic video decoder driver
Subsystems (3)
v3 cover Add Amlogic video decoder driver
Hi everyone,
This patch series adds support for the Amlogic video decoder, as well as the corresponding dt bindings for GXBB/GXL/GXM chips.
It features decoding for the following formats:
- MPEG 1
- MPEG 2
The following formats will be added in future patches:
- MJPEG
- MPEG 4 (incl. Xvid, H.263)
- H.264
- HEVC (incl. 10-bit)
The following formats' development has still not started, but they are supported by the hardware:
- VC1
- VP9
The code was made in such a way to allow easy inclusion of those formats in the future.
The decoder is single instance.
Files:
- vdec.c handles the V4L2 M2M logic
- esparser.c manages the hardware bitstream parser
- vdec_helpers.c provides helpers to DONE the dst buffers as well as various common code used by the codecs
- vdec_1.c manages the VDEC_1 block of the vdec IP
- codec_mpeg12.c enables decoding for MPEG 1/2.
- vdec_platform.c links codec units with vdec units
(e.g vdec_1 with codec_mpeg12) and lists all the available src/dst formats and requirements (max width/height, etc.), per compatible chip.
Firmwares are necessary to run the vdec. They can currently be found at:
https://github.com/chewitt/meson-firmware
It was tested primarily with ffmpeg's v4l2-m2m implementation. For instance:
$ ffmpeg -c:v mpeg2_v4l2m2m -i sample_mpeg2.mkv -f null -
Note: This patch series depends on
"[PATCH v3 0/3] soc: amlogic: add meson-canvas"
https://patchwork.kernel.org/cover/10573763/
This canvas series is merged in Kevin Hilman's tree with a tag:
Repo: https://git.kernel.org/pub/scm/linux/kernel/git/khilman/linux-amlogic.git Tag: amlogic-drivers-canvas
The v4l2-compliance results are available below the patch diff.
Changes since v2 [1]:
- Override capture queue's min_buffers_needed in queue_setup
The HW needs the full buffer list to be available when doing start_streaming
- Fix the draining sequence
The blob that we write to the ESPARSER to trigger drain is codec-dependent. The one that was sent in v1 is specific to H.264 and isn't guaranteed to trigger drain for MPEG2. For the latter, a simple MPEG2 EOS code should be sent to the ESPARSER instead.
- Slight enhancements to the way we do vififo offset<=>timestamp matching
Changes since v1 [0]:
- use named interrupts in the bindings
- rewrite description in the bindings doc
- don't include the dts changes in the patch series
- fill the vb2 queues locks
- fill the video_device lock
- use helpers for wait_prepare and wait_finish vb2_ops
- remove unnecessary usleep in between esparser writes. Extensive testing of every codec on GXBB/GXL didn't reveal
any fails without it, so just remove it.
- compile v4l2_compliance inside the git repo
- Check for plane number/plane size to pass the latest v4l2-compliance test
- Moved the single instance check (returning -EBUSY) to start/stop streaming The check was previously in queue_setup but there was no great location to clear it except for .close().
- Slight rework of the way CAPTURE frames are timestamped for better accuracy
- Implement PAR reporting via VIDIOC_CROPCAP
[0] https://patchwork.kernel.org/cover/10583391/
[1] https://patchwork.kernel.org/cover/10595803/
Maxime Jourdan (3):
dt-bindings: media: add Amlogic Video Decoder Bindings
media: meson: add v4l2 m2m video decoder driver
MAINTAINERS: Add meson video decoder
.../bindings/media/amlogic,vdec.txt | 71 ++ MAINTAINERS | 8 + drivers/media/platform/Kconfig | 10 + drivers/media/platform/meson/Makefile | 1 + drivers/media/platform/meson/vdec/Makefile | 8 + .../media/platform/meson/vdec/codec_mpeg12.c | 209 ++++ .../media/platform/meson/vdec/codec_mpeg12.h | 14 + drivers/media/platform/meson/vdec/dos_regs.h | 98 ++ drivers/media/platform/meson/vdec/esparser.c | 322 ++++++ drivers/media/platform/meson/vdec/esparser.h | 32 + drivers/media/platform/meson/vdec/vdec.c | 1024 +++++++++++++++++ drivers/media/platform/meson/vdec/vdec.h | 251 ++++ drivers/media/platform/meson/vdec/vdec_1.c | 231 ++++ drivers/media/platform/meson/vdec/vdec_1.h | 14 + .../media/platform/meson/vdec/vdec_helpers.c | 412 +++++++ .../media/platform/meson/vdec/vdec_helpers.h | 48 + .../media/platform/meson/vdec/vdec_platform.c | 101 ++ .../media/platform/meson/vdec/vdec_platform.h | 30 + 18 files changed, 2884 insertions(+) create mode 100644 Documentation/devicetree/bindings/media/amlogic,vdec.txt create mode 100644 drivers/media/platform/meson/vdec/Makefile create mode 100644 drivers/media/platform/meson/vdec/codec_mpeg12.c create mode 100644 drivers/media/platform/meson/vdec/codec_mpeg12.h create mode 100644 drivers/media/platform/meson/vdec/dos_regs.h create mode 100644 drivers/media/platform/meson/vdec/esparser.c create mode 100644 drivers/media/platform/meson/vdec/esparser.h create mode 100644 drivers/media/platform/meson/vdec/vdec.c create mode 100644 drivers/media/platform/meson/vdec/vdec.h create mode 100644 drivers/media/platform/meson/vdec/vdec_1.c create mode 100644 drivers/media/platform/meson/vdec/vdec_1.h create mode 100644 drivers/media/platform/meson/vdec/vdec_helpers.c create mode 100644 drivers/media/platform/meson/vdec/vdec_helpers.h create mode 100644 drivers/media/platform/meson/vdec/vdec_platform.c create mode 100644 drivers/media/platform/meson/vdec/vdec_platform.h
root at hardkernel-odroidc2:~# v4l2-compliance -d /dev/video0 v4l2-compliance SHA: 7bde5ef172bd4a09b9544788ba9c5dbb1aa9994a, 64 bits
Compliance test for device /dev/video0:
Driver Info:
Driver name : meson-vdec Card type : Amlogic Video Decoder Bus info : platform:meson-vdec Driver version : 4.19.0 Capabilities : 0x84204000
Video Memory-to-Memory Multiplanar Streaming
Extended Pix Format
Device Capabilities
Device Caps : 0x04204000
Video Memory-to-Memory Multiplanar Streaming
Extended Pix Format
Required ioctls:
test VIDIOC_QUERYCAP: OK
Allow for multiple opens:
test second /dev/video0 open: OK
test VIDIOC_QUERYCAP: OK
test VIDIOC_G/S_PRIORITY: OK
test for unlimited opens: OK
Debug ioctls:
test VIDIOC_DBG_G/S_REGISTER: OK (Not Supported) test VIDIOC_LOG_STATUS: OK (Not Supported)
Input ioctls:
test VIDIOC_G/S_TUNER/ENUM_FREQ_BANDS: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_S_HW_FREQ_SEEK: OK (Not Supported) test VIDIOC_ENUMAUDIO: OK (Not Supported)
test VIDIOC_G/S/ENUMINPUT: OK (Not Supported) test VIDIOC_G/S_AUDIO: OK (Not Supported)
Inputs: 0 Audio Inputs: 0 Tuners: 0
Output ioctls:
test VIDIOC_G/S_MODULATOR: OK (Not Supported) test VIDIOC_G/S_FREQUENCY: OK (Not Supported) test VIDIOC_ENUMAUDOUT: OK (Not Supported) test VIDIOC_G/S/ENUMOUTPUT: OK (Not Supported) test VIDIOC_G/S_AUDOUT: OK (Not Supported)
Outputs: 0 Audio Outputs: 0 Modulators: 0
Input/Output configuration ioctls:
test VIDIOC_ENUM/G/S/QUERY_STD: OK (Not Supported) test VIDIOC_ENUM/G/S/QUERY_DV_TIMINGS: OK (Not Supported) test VIDIOC_DV_TIMINGS_CAP: OK (Not Supported) test VIDIOC_G/S_EDID: OK (Not Supported)
Control ioctls:
test VIDIOC_QUERY_EXT_CTRL/QUERYMENU: OK (Not Supported) test VIDIOC_QUERYCTRL: OK (Not Supported)
test VIDIOC_G/S_CTRL: OK (Not Supported)
test VIDIOC_G/S/TRY_EXT_CTRLS: OK (Not Supported) test VIDIOC_(UN)SUBSCRIBE_EVENT/DQEVENT: OK (Not Supported) test VIDIOC_G/S_JPEGCOMP: OK (Not Supported) Standard Controls: 0 Private Controls: 0
Format ioctls:
test VIDIOC_ENUM_FMT/FRAMESIZES/FRAMEINTERVALS: OK test VIDIOC_G/S_PARM: OK (Not Supported) test VIDIOC_G_FBUF: OK (Not Supported) test VIDIOC_G_FMT: OK
test VIDIOC_TRY_FMT: OK
test VIDIOC_S_FMT: OK
test VIDIOC_G_SLICED_VBI_CAP: OK (Not Supported) test Cropping: OK (Not Supported)
test Composing: OK (Not Supported)
test Scaling: OK
Codec ioctls:
test VIDIOC_(TRY_)ENCODER_CMD: OK (Not Supported) test VIDIOC_G_ENC_INDEX: OK (Not Supported) test VIDIOC_(TRY_)DECODER_CMD: OK
Buffer ioctls:
test VIDIOC_REQBUFS/CREATE_BUFS/QUERYBUF: OK test VIDIOC_EXPBUF: OK
Total: 43, Succeeded: 43, Failed: 0, Warnings: 0
Signature · 1 line
2.19.0
PATCH v3 1/3 dt-bindings: media: add Amlogic Video Decoder Bindings
Add documentation for the meson vdec dts node.
--- .../bindings/media/amlogic,vdec.txt | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Documentation/devicetree/bindings/media/amlogic,vdec.txt
Documentation/devicetree/bindings/media/amlogic,vdec.txt new file +71 -0
@@ -0,0 +1,71 @@ +Amlogic Video Decoder +================================ + +The video decoding IP lies within the DOS memory region, +except for the hardware bitstream parser that makes use of an undocumented +region. + +It makes use of the following blocks: + +- ESPARSER is a bitstream parser that outputs to a VIFIFO. Further VDEC blocks +then feed from this VIFIFO. +- VDEC_1 can decode MPEG-1, MPEG-2, MPEG-4 part 2, MJPEG, H.263, H.264, VC-1. +- VDEC_HEVC can decode HEVC and VP9. + +Both VDEC_1 and VDEC_HEVC share the "vdec" IRQ and as such cannot run +concurrently. + +Device Tree Bindings: +--------------------- + +VDEC: Video Decoder +-------------------------- + +Required properties: +- compatible: value should be different for each SoC family as : + - GXBB (S905) : "amlogic,gxbb-vdec" + - GXL (S905X, S905D) : "amlogic,gxl-vdec" + - GXM (S912) : "amlogic,gxm-vdec" +- reg: base address and size of he following memory-mapped regions : + - dos + - esparser +- reg-names: should contain the names of the previous memory regions +- interrupts: should contain the following IRQs: + - vdec + - esparser +- interrupt-names: should contain the names of the previous interrupts +- amlogic,ao-sysctrl: should point to the AOBUS sysctrl node +- amlogic,canvas: should point to a canvas provider node +- clocks: should contain the following clocks : + - dos_parser + - dos + - vdec_1 + - vdec_hevc +- clock-names: should contain the names of the previous clocks +- resets: should contain the parser reset +- reset-names: should be "esparser" + +Example: + +vdec: video-decoder at c8820000 { + compatible = "amlogic,gxbb-vdec"; + reg = <0x0 0xc8820000 0x0 0x10000>, + <0x0 0xc110a580 0x0 0xe4>; + reg-names = "dos", "esparser"; + + interrupts = <GIC_SPI 44 IRQ_TYPE_EDGE_RISING>, + <GIC_SPI 32 IRQ_TYPE_EDGE_RISING>; + interrupt-names = "vdec", "esparser"; + + amlogic,ao-sysctrl = <&sysctrl_AO>; + amlogic,canvas = <&canvas>; + + clocks = <&clkc CLKID_DOS_PARSER>, + <&clkc CLKID_DOS>, + <&clkc CLKID_VDEC_1>, + <&clkc CLKID_VDEC_HEVC>; + clock-names = "dos_parser", "dos", "vdec_1", "vdec_hevc"; + + resets = <&reset RESET_PARSER>; + reset-names = "esparser"; +};
Signature · 1 line
2.19.0
Re: [PATCH v3 1/3] dt-bindings: media: add Amlogic Video Decoder Bindings
Errata: I forgot the following tag:
(The patch is unchanged from v2)
Le ven. 28 sept. 2018 ? 16:29, Maxime Jourdan <[email protected]> a ?crit :
Quoted 89 lines
Add documentation for the meson vdec dts node.
Signed-off-by Maxime Jourdan <[email protected]>--- .../bindings/media/amlogic,vdec.txt | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Documentation/devicetree/bindings/media/amlogic,vdec.txtDocumentation/devicetree/bindings/media/amlogic,vdec.txt new file +71
-1@@ -0,0 +1,71 @@ +Amlogic Video Decoder +================================ + +The video decoding IP lies within the DOS memory region, +except for the hardware bitstream parser that makes use of an undocumented +region. + +It makes use of the following blocks: + +- ESPARSER is a bitstream parser that outputs to a VIFIFO. Further VDEC blocks +then feed from this VIFIFO. +- VDEC_1 can decode MPEG-1, MPEG-2, MPEG-4 part 2, MJPEG, H.263, H.264, VC-1. +- VDEC_HEVC can decode HEVC and VP9. + +Both VDEC_1 and VDEC_HEVC share the "vdec" IRQ and as such cannot run +concurrently. + +Device Tree Bindings: +--------------------- + +VDEC: Video Decoder +-------------------------- + +Required properties: +- compatible: value should be different for each SoC family as : + - GXBB (S905) : "amlogic,gxbb-vdec" + - GXL (S905X, S905D) : "amlogic,gxl-vdec" + - GXM (S912) : "amlogic,gxm-vdec" +- reg: base address and size of he following memory-mapped regions : + - dos + - esparser +- reg-names: should contain the names of the previous memory regions +- interrupts: should contain the following IRQs: + - vdec + - esparser +- interrupt-names: should contain the names of the previous interrupts +- amlogic,ao-sysctrl: should point to the AOBUS sysctrl node +- amlogic,canvas: should point to a canvas provider node +- clocks: should contain the following clocks : + - dos_parser + - dos + - vdec_1 + - vdec_hevc +- clock-names: should contain the names of the previous clocks +- resets: should contain the parser reset +- reset-names: should be "esparser" + +Example: + +vdec: video-decoder at c8820000 { + compatible = "amlogic,gxbb-vdec"; + reg = <0x0 0xc8820000 0x0 0x10000>, + <0x0 0xc110a580 0x0 0xe4>; + reg-names = "dos", "esparser"; + + interrupts = <GIC_SPI 44 IRQ_TYPE_EDGE_RISING>, + <GIC_SPI 32 IRQ_TYPE_EDGE_RISING>; + interrupt-names = "vdec", "esparser"; + + amlogic,ao-sysctrl = <&sysctrl_AO>; + amlogic,canvas = <&canvas>; + + clocks = <&clkc CLKID_DOS_PARSER>, + <&clkc CLKID_DOS>, + <&clkc CLKID_VDEC_1>, + <&clkc CLKID_VDEC_HEVC>; + clock-names = "dos_parser", "dos", "vdec_1", "vdec_hevc"; + + resets = <&reset RESET_PARSER>; + reset-names = "esparser"; +}; --2.19.0
PATCH v3 2/3 media: meson: add v4l2 m2m video decoder driver
In reply to Maxime JourdanAmlogic SoCs feature a powerful video decoder unit able to decode many formats, with a performance of usually up to 4k60.
This is a driver for this IP that is based around the v4l2 m2m framework.
It features decoding for:
- MPEG 1
- MPEG 2
Supported SoCs are: GXBB (S905), GXL (S905X/W/D), GXM (S912)
There is also a hardware bitstream parser (ESPARSER) that is handled here.
--- drivers/media/platform/Kconfig | 10 + drivers/media/platform/meson/Makefile | 1 + drivers/media/platform/meson/vdec/Makefile | 8 + .../media/platform/meson/vdec/codec_mpeg12.c | 209 ++++ .../media/platform/meson/vdec/codec_mpeg12.h | 14 + drivers/media/platform/meson/vdec/dos_regs.h | 98 ++ drivers/media/platform/meson/vdec/esparser.c | 322 ++++++ drivers/media/platform/meson/vdec/esparser.h | 32 + drivers/media/platform/meson/vdec/vdec.c | 1024 +++++++++++++++++ drivers/media/platform/meson/vdec/vdec.h | 251 ++++ drivers/media/platform/meson/vdec/vdec_1.c | 231 ++++ drivers/media/platform/meson/vdec/vdec_1.h | 14 + .../media/platform/meson/vdec/vdec_helpers.c | 412 +++++++ .../media/platform/meson/vdec/vdec_helpers.h | 48 + .../media/platform/meson/vdec/vdec_platform.c | 101 ++ .../media/platform/meson/vdec/vdec_platform.h | 30 + 16 files changed, 2805 insertions(+) create mode 100644 drivers/media/platform/meson/vdec/Makefile create mode 100644 drivers/media/platform/meson/vdec/codec_mpeg12.c create mode 100644 drivers/media/platform/meson/vdec/codec_mpeg12.h create mode 100644 drivers/media/platform/meson/vdec/dos_regs.h create mode 100644 drivers/media/platform/meson/vdec/esparser.c create mode 100644 drivers/media/platform/meson/vdec/esparser.h create mode 100644 drivers/media/platform/meson/vdec/vdec.c create mode 100644 drivers/media/platform/meson/vdec/vdec.h create mode 100644 drivers/media/platform/meson/vdec/vdec_1.c create mode 100644 drivers/media/platform/meson/vdec/vdec_1.h create mode 100644 drivers/media/platform/meson/vdec/vdec_helpers.c create mode 100644 drivers/media/platform/meson/vdec/vdec_helpers.h create mode 100644 drivers/media/platform/meson/vdec/vdec_platform.c create mode 100644 drivers/media/platform/meson/vdec/vdec_platform.h
drivers/media/platform/Kconfig +10 -0
@@ -482,6 +482,16 @@ config VIDEO_QCOM_VENUS on various Qualcomm SoCs. To compile this driver as a module choose m here. +config VIDEO_MESON_VDEC + tristate "Amlogic video decoder driver" + depends on VIDEO_DEV && VIDEO_V4L2 && HAS_DMA + depends on ARCH_MESON || COMPILE_TEST + select VIDEOBUF2_DMA_CONTIG + select V4L2_MEM2MEM_DEV + select MESON_CANVAS + help + Support for the video decoder found in gxbb/gxl/gxm chips. + endif # V4L_MEM2MEM_DRIVERS # TI VIDEO PORT Helper Modules
drivers/media/platform/meson/Makefile +1 -0
@@ -1 +1,2 @@ obj-$(CONFIG_VIDEO_MESON_AO_CEC) += ao-cec.o +obj-$(CONFIG_VIDEO_MESON_VDEC) += vdec/
drivers/media/platform/meson/vdec/Makefile new file +8 -0
@@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0 +# Makefile for Amlogic meson video decoder driver + +meson-vdec-objs = esparser.o vdec.o vdec_helpers.o vdec_platform.o +meson-vdec-objs += vdec_1.o +meson-vdec-objs += codec_mpeg12.o + +obj-$(CONFIG_VIDEO_MESON_VDEC) += meson-vdec.o
drivers/media/platform/meson/vdec/codec_mpeg12.c new file +209 -0
209 lines not on this page · raw message
drivers/media/platform/meson/vdec/codec_mpeg12.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* +* Copyright (C) 2018 BayLibre, SAS +* Author: Maxime Jourdan <[email protected]> +*/ + +#ifndef __MESON_VDEC_CODEC_MPEG12_H_ +#define __MESON_VDEC_CODEC_MPEG12_H_ + +#include "vdec.h" + +extern struct amvdec_codec_ops codec_mpeg12_ops; + +#endif
drivers/media/platform/meson/vdec/dos_regs.h new file +98 -0
98 lines not on this page · raw message
drivers/media/platform/meson/vdec/esparser.c new file +322 -0
322 lines not on this page · raw message
drivers/media/platform/meson/vdec/esparser.h new file +32 -0
@@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* +* Copyright (C) 2018 BayLibre, SAS +* Author: Maxime Jourdan <[email protected]> +*/ + +#ifndef __MESON_VDEC_ESPARSER_H_ +#define __MESON_VDEC_ESPARSER_H_ + +#include <linux/platform_device.h> + +#include "vdec.h" + +int esparser_init (struct platform_device *pdev,struct amvdec_core *core); +int esparser_power_up (struct amvdec_session *sess); + +/** +* esparser_queue_eos() - write End Of Stream sequence to the ESPARSER +* +* @core vdec core struct +*/ +int esparser_queue_eos (struct amvdec_core *core,const u8 *data,u32 len); + +/** +* esparser_queue_all_src() - work handler that writes as many src buffers +* as possible to the ESPARSER +*/ +void esparser_queue_all_src (struct work_struct *work); + +#define ESPARSER_MIN_PACKET_SIZE SZ_4K + +#endif
drivers/media/platform/meson/vdec/vdec.c new file +1024 -0
1,024 lines not on this page · raw message
drivers/media/platform/meson/vdec/vdec.h new file +251 -0
251 lines not on this page · raw message
drivers/media/platform/meson/vdec/vdec_1.c new file +231 -0
231 lines not on this page · raw message
drivers/media/platform/meson/vdec/vdec_1.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* +* Copyright (C) 2018 BayLibre, SAS +* Author: Maxime Jourdan <[email protected]> +*/ + +#ifndef __MESON_VDEC_VDEC_1_H_ +#define __MESON_VDEC_VDEC_1_H_ + +#include "vdec.h" + +extern struct amvdec_ops vdec_1_ops; + +#endif
drivers/media/platform/meson/vdec/vdec_helpers.c new file +412 -0
412 lines not on this page · raw message
drivers/media/platform/meson/vdec/vdec_helpers.h new file +48 -0
48 lines not on this page · raw message
drivers/media/platform/meson/vdec/vdec_platform.c new file +101 -0
101 lines not on this page · raw message
drivers/media/platform/meson/vdec/vdec_platform.h new file +30 -0
@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* +* Copyright (C) 2018 BayLibre, SAS +* Author: Maxime Jourdan <[email protected]> +*/ + +#ifndef __MESON_VDEC_PLATFORM_H_ +#define __MESON_VDEC_PLATFORM_H_ + +#include "vdec.h" + +struct amvdec_format ; + +enum vdec_revision { +VDEC_REVISION_GXBB , +VDEC_REVISION_GXL , +VDEC_REVISION_GXM , +}; + +struct vdec_platform { +const struct amvdec_format *formats; +const u32 num_formats; +enum vdec_revision revision; +}; + +extern const struct vdec_platform vdec_platform_gxbb; +extern const struct vdec_platform vdec_platform_gxm; +extern const struct vdec_platform vdec_platform_gxl; + +#endif
Signature · 1 line
2.19.0
Re: [PATCH v3 2/3] media: meson: add v4l2 m2m video decoder driver
Quoted 47 lines · Maxime Jourdan
Amlogic SoCs feature a powerful video decoder unit able to decode many formats, with a performance of usually up to 4k60.
This is a driver for this IP that is based around the v4l2 m2m framework.
It features decoding for:
- MPEG 1
- MPEG 2Supported SoCs are: GXBB (S905), GXL (S905X/W/D), GXM (S912)
There is also a hardware bitstream parser (ESPARSER) that is handled here.
Signed-off-by Maxime Jourdan <[email protected]>--- drivers/media/platform/Kconfig | 10 + drivers/media/platform/meson/Makefile | 1 + drivers/media/platform/meson/vdec/Makefile | 8 + .../media/platform/meson/vdec/codec_mpeg12.c | 209 ++++ .../media/platform/meson/vdec/codec_mpeg12.h | 14 + drivers/media/platform/meson/vdec/dos_regs.h | 98 ++ drivers/media/platform/meson/vdec/esparser.c | 322 ++++++ drivers/media/platform/meson/vdec/esparser.h | 32 + drivers/media/platform/meson/vdec/vdec.c | 1024 +++++++++++++++++ drivers/media/platform/meson/vdec/vdec.h | 251 ++++ drivers/media/platform/meson/vdec/vdec_1.c | 231 ++++ drivers/media/platform/meson/vdec/vdec_1.h | 14 + .../media/platform/meson/vdec/vdec_helpers.c | 412 +++++++ .../media/platform/meson/vdec/vdec_helpers.h | 48 + .../media/platform/meson/vdec/vdec_platform.c | 101 ++ .../media/platform/meson/vdec/vdec_platform.h | 30 + 16 files changed, 2805 insertions(+) create mode 100644 drivers/media/platform/meson/vdec/Makefile create mode 100644 drivers/media/platform/meson/vdec/codec_mpeg12.c create mode 100644 drivers/media/platform/meson/vdec/codec_mpeg12.h create mode 100644 drivers/media/platform/meson/vdec/dos_regs.h create mode 100644 drivers/media/platform/meson/vdec/esparser.c create mode 100644 drivers/media/platform/meson/vdec/esparser.h create mode 100644 drivers/media/platform/meson/vdec/vdec.c create mode 100644 drivers/media/platform/meson/vdec/vdec.h create mode 100644 drivers/media/platform/meson/vdec/vdec_1.c create mode 100644 drivers/media/platform/meson/vdec/vdec_1.h create mode 100644 drivers/media/platform/meson/vdec/vdec_helpers.c create mode 100644 drivers/media/platform/meson/vdec/vdec_helpers.h create mode 100644 drivers/media/platform/meson/vdec/vdec_platform.c create mode 100644 drivers/media/platform/meson/vdec/vdec_platform.h
<snip>
Quoted 187 lines
drivers/media/platform/meson/vdec/vdec.c new file +181
-0@@ -0,0 +1,1024 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (C) 2018 BayLibre, SAS +* Author: Maxime Jourdan <[email protected]> +*/ + +#include <linux/of_device.h> +#include <linux/clk.h> +#include <linux/io.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/mfd/syscon.h> +#include <linux/slab.h> +#include <media/v4l2-ioctl.h> +#include <media/v4l2-event.h> +#include <media/v4l2-ctrls.h> +#include <media/v4l2-mem2mem.h> +#include <media/v4l2-dev.h> +#include <media/videobuf2-dma-contig.h> + +#include "vdec.h" +#include "esparser.h" +#include "vdec_helpers.h" + +struct dummy_buf { +struct vb2_v4l2_buffer vb; +struct list_head list; +}; + +/* 16 MiB for parsed bitstream swap exchange */ +#define SIZE_VIFIFO SZ_16M + +static u32 get_output_size (u32 width,u32 height) +{ +return ALIGN (width * height,SZ_64K ); +} + +u32 amvdec_get_output_size (struct amvdec_session *sess) +{ +return get_output_size (sess->width, sess->height); +} +EXPORT_SYMBOL_GPL (amvdec_get_output_size); + +static int vdec_codec_needs_recycle (struct amvdec_session *sess) +{ +struct amvdec_codec_ops *codec_ops = sess->fmt_out->codec_ops; + +return codec_ops->can_recycle && codec_ops->recycle; +} + +static int vdec_recycle_thread (void *data) +{ +struct amvdec_session *sess = data; +struct amvdec_core *core = sess->core; +struct amvdec_codec_ops *codec_ops = sess->fmt_out->codec_ops; +struct amvdec_buffer *tmp, *n; + +while (!kthread_should_stop ()) { +mutex_lock (&sess->bufs_recycle_lock); +list_for_each_entry_safe (tmp, n, &sess->bufs_recycle, list) { +if (!codec_ops->can_recycle (core)) +break ; + + codec_ops->recycle (core, tmp->vb->index); +list_del (&tmp->list); +kfree (tmp); + } +mutex_unlock (&sess->bufs_recycle_lock); + +usleep_range (5000 ,10000 ); + } + +return 0 ; +} + +static int vdec_poweron (struct amvdec_session *sess) +{ +int ret; +struct amvdec_ops *vdec_ops = sess->fmt_out->vdec_ops; + + ret =clk_prepare_enable (sess->core->dos_parser_clk); +if (ret) +return ret; + + ret =clk_prepare_enable (sess->core->dos_clk); +if (ret) + goto disable_dos_parser; + + ret = vdec_ops->start (sess); +if (ret) + goto disable_dos; + +esparser_power_up (sess); + +return 0 ; + +disable_dos: +clk_disable_unprepare (sess->core->dos_clk); +disable_dos_parser: +clk_disable_unprepare (sess->core->dos_parser_clk); + +return ret; +} + +static void vdec_wait_inactive (struct amvdec_session *sess) +{ +/* We consider 50ms with no IRQ to be inactive. */ +while (time_is_after_jiffies64 (sess->last_irq_jiffies + +msecs_to_jiffies (50 ))) +msleep (25 ); +} + +static void vdec_poweroff (struct amvdec_session *sess) +{ +struct amvdec_ops *vdec_ops = sess->fmt_out->vdec_ops; +struct amvdec_codec_ops *codec_ops = sess->fmt_out->codec_ops; + +vdec_wait_inactive (sess); +if (codec_ops->drain) + codec_ops->drain (sess); + + vdec_ops->stop (sess); +clk_disable_unprepare (sess->core->dos_clk); +clk_disable_unprepare (sess->core->dos_parser_clk); +} + +static void +vdec_queue_recycle (struct amvdec_session *sess,struct vb2_buffer *vb) +{ +struct amvdec_buffer *new_buf; + + new_buf =kmalloc (sizeof (*new_buf),GFP_KERNEL ); + new_buf->vb = vb; + +mutex_lock (&sess->bufs_recycle_lock); +list_add_tail (&new_buf->list, &sess->bufs_recycle); +mutex_unlock (&sess->bufs_recycle_lock); +} + +static void vdec_m2m_device_run (void *priv) +{ +struct amvdec_session *sess = priv; + +schedule_work (&sess->esparser_queue_work); +} + +static void vdec_m2m_job_abort (void *priv) +{ +struct amvdec_session *sess = priv; + +v4l2_m2m_job_finish (sess->m2m_dev, sess->m2m_ctx); +} + +static const struct v4l2_m2m_ops vdec_m2m_ops = { + .device_run = vdec_m2m_device_run, + .job_abort = vdec_m2m_job_abort, +}; + +static int vdec_queue_setup (struct vb2_queue *q, +unsigned int *num_buffers,unsigned int *num_planes, +unsigned int sizes[],struct device *alloc_devs[]) +{ +struct amvdec_session *sess =vb2_get_drv_priv (q); +const struct amvdec_format *fmt_out = sess->fmt_out; +u32 output_size =amvdec_get_output_size (sess); + +if (*num_planes) { +switch (q->type) { +case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : +if (*num_planes !=1 || sizes[0 ] < output_size) +return -EINVAL ; +break ; +case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : +switch (sess->pixfmt_cap) { +case V4L2_PIX_FMT_NV12M : +if (*num_planes !=2 || + sizes[0 ] < output_size || + sizes[1 ] < output_size /2 ) +return -EINVAL ; +break ; +case V4L2_PIX_FMT_YUV420M :
drivers/media/platform/meson/vdec/vdec.c line 191
Quoted 10 lines
drivers/media/platform/meson/vdec/vdec.c new file +10
-0+if (*num_planes !=3 || + sizes[0 ] < output_size || + sizes[1 ] < output_size /4 || + sizes[2 ] < output_size /4 ) +return -EINVAL ; +break ; +default : +return -EINVAL ; + } +break ;
You want to clamp *num_buffers here as well (and likely update min_buffers_needed).
Note that *num_buffers in this case refers to the number of buffers that VIDIOC_CREATE_BUFS wants to add. So the total number of buffers after this call is actually *num_buffers + q->num_buffers.
Quoted 18 lines
drivers/media/platform/meson/vdec/vdec.c new file +18
-0+ } + +return 0 ; + } + +switch (q->type) { +case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : + sizes[0 ] =amvdec_get_output_size (sess); + *num_planes =1 ; +break ; +case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : +switch (sess->pixfmt_cap) { +case V4L2_PIX_FMT_NV12M : + sizes[0 ] = output_size; + sizes[1 ] = output_size /2 ; + *num_planes =2 ; +break ; +case V4L2_PIX_FMT_YUV420M :
drivers/media/platform/meson/vdec/vdec.c line 219
Quoted 10 lines
drivers/media/platform/meson/vdec/vdec.c new file +10
-0+ sizes[0 ] = output_size; + sizes[1 ] = output_size /4 ; + sizes[2 ] = output_size /4 ; + *num_planes =3 ; +break ; +default : +return -EINVAL ; + } + *num_buffers =min (max (*num_buffers, fmt_out->min_buffers), + fmt_out->max_buffers);
You can use clamp here. That's easier to read.
drivers/media/platform/meson/vdec/vdec.c line 220
Quoted 1 line
drivers/media/platform/meson/vdec/vdec.c new file +1
-0+/* The HW needs all buffers to be configured during startup */
Why? I kind of expected to see 'q->min_buffers_needed = fmt_out->min_buffers' here. I think some more information is needed here in the comment.
Quoted 164 lines
drivers/media/platform/meson/vdec/vdec.c new file +164
-0+ q->min_buffers_needed = *num_buffers; +break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +static void vdec_vb2_buf_queue (struct vb2_buffer *vb) +{ +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); +struct amvdec_session *sess =vb2_get_drv_priv (vb->vb2_queue); +struct v4l2_m2m_ctx *m2m_ctx = sess->m2m_ctx; + +v4l2_m2m_buf_queue (m2m_ctx, vbuf); + +if (!sess->streamon_out || !sess->streamon_cap) +return ; + +if (vb->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && +vdec_codec_needs_recycle (sess)) +vdec_queue_recycle (sess, vb); + +schedule_work (&sess->esparser_queue_work); +} + +static int vdec_start_streaming (struct vb2_queue *q,unsigned int count) +{ +struct amvdec_session *sess =vb2_get_drv_priv (q); +struct amvdec_core *core = sess->core; +struct vb2_v4l2_buffer *buf; +int ret; + +if (core->cur_sess && core->cur_sess != sess) { + ret = -EBUSY ; + goto bufs_done; + } + +if (q->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) + sess->streamon_out =1 ; +else + sess->streamon_cap =1 ; + +if (!sess->streamon_out || !sess->streamon_cap) +return 0 ; + + sess->vififo_size =SIZE_VIFIFO ; + sess->vififo_vaddr = +dma_alloc_coherent (sess->core->dev, sess->vififo_size, + &sess->vififo_paddr,GFP_KERNEL ); +if (!sess->vififo_vaddr) { +dev_err (sess->core->dev,"Failed to request VIFIFO buffer\n" ); + ret = -ENOMEM ; + goto bufs_done; + } + + sess->should_stop =0 ; + sess->keyframe_found =0 ; + sess->last_offset =0 ; + sess->wrap_count =0 ; + sess->pixelaspect.numerator =1 ; + sess->pixelaspect.denominator =1 ; +atomic_set (&sess->esparser_queued_bufs,0 ); + + ret =vdec_poweron (sess); +if (ret) + goto vififo_free; + + sess->sequence_cap =0 ; +if (vdec_codec_needs_recycle (sess)) + sess->recycle_thread =kthread_run (vdec_recycle_thread, sess, +"vdec_recycle" ); + + core->cur_sess = sess; + +return 0 ; + +vififo_free: +dma_free_coherent (sess->core->dev, sess->vififo_size, + sess->vififo_vaddr, sess->vififo_paddr); +bufs_done: +while ((buf =v4l2_m2m_src_buf_remove (sess->m2m_ctx))) +v4l2_m2m_buf_done (buf,VB2_BUF_STATE_QUEUED ); +while ((buf =v4l2_m2m_dst_buf_remove (sess->m2m_ctx))) +v4l2_m2m_buf_done (buf,VB2_BUF_STATE_QUEUED ); + +if (q->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) + sess->streamon_out =0 ; +else + sess->streamon_cap =0 ; + +return ret; +} + +static void vdec_free_canvas (struct amvdec_session *sess) +{ +int i; + +for (i =0 ; i < sess->canvas_num; ++i) +meson_canvas_free (sess->core->canvas, sess->canvas_alloc[i]); + + sess->canvas_num =0 ; +} + +static void vdec_reset_timestamps (struct amvdec_session *sess) +{ +struct amvdec_timestamp *tmp, *n; + +list_for_each_entry_safe (tmp, n, &sess->timestamps, list) { +list_del (&tmp->list); +kfree (tmp); + } +} + +static void vdec_reset_bufs_recycle (struct amvdec_session *sess) +{ +struct amvdec_buffer *tmp, *n; + +list_for_each_entry_safe (tmp, n, &sess->bufs_recycle, list) { +list_del (&tmp->list); +kfree (tmp); + } +} + +static void vdec_stop_streaming (struct vb2_queue *q) +{ +struct amvdec_session *sess =vb2_get_drv_priv (q); +struct amvdec_core *core = sess->core; +struct vb2_v4l2_buffer *buf; + +if (sess->streamon_out && sess->streamon_cap) { +if (vdec_codec_needs_recycle (sess)) +kthread_stop (sess->recycle_thread); + +vdec_poweroff (sess); +vdec_free_canvas (sess); +dma_free_coherent (sess->core->dev, sess->vififo_size, + sess->vififo_vaddr, sess->vififo_paddr); +vdec_reset_timestamps (sess); +vdec_reset_bufs_recycle (sess); +kfree (sess->priv); + sess->priv =NULL ; + core->cur_sess =NULL ; + } + +if (q->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) { +while ((buf =v4l2_m2m_src_buf_remove (sess->m2m_ctx))) +v4l2_m2m_buf_done (buf,VB2_BUF_STATE_ERROR ); + + sess->streamon_out =0 ; + }else { +while ((buf =v4l2_m2m_dst_buf_remove (sess->m2m_ctx))) +v4l2_m2m_buf_done (buf,VB2_BUF_STATE_ERROR ); + + sess->streamon_cap =0 ; + } +} + +static const struct vb2_ops vdec_vb2_ops = { + .queue_setup = vdec_queue_setup, + .start_streaming = vdec_start_streaming, + .stop_streaming = vdec_stop_streaming, + .buf_queue = vdec_vb2_buf_queue,
drivers/media/platform/meson/vdec/vdec.c line 394
Quoted 10 lines
drivers/media/platform/meson/vdec/vdec.c new file +10
-0+ .wait_prepare = vb2_ops_wait_prepare, + .wait_finish = vb2_ops_wait_finish, +}; + +static int +vdec_querycap (struct file *file,void *fh,struct v4l2_capability *cap) +{ +strlcpy (cap->driver,"meson-vdec" ,sizeof (cap->driver)); +strlcpy (cap->card,"Amlogic Video Decoder" ,sizeof (cap->card)); +strlcpy (cap->bus_info,"platform:meson-vdec" ,sizeof (cap->bus_info));
Replace all strlcpy/strcpy/strncpy by strscpy. That's the recommended function.
Quoted 620 lines
drivers/media/platform/meson/vdec/vdec.c new file +620
-0+ +return 0 ; +} + +static const struct amvdec_format * +find_format (const struct amvdec_format *fmts,u32 size,u32 pixfmt) +{ +unsigned int i; + +for (i =0 ; i < size; i++) { +if (fmts[i].pixfmt == pixfmt) +return &fmts[i]; + } + +return NULL ; +} + +static unsigned int +vdec_supports_pixfmt_cap (const struct amvdec_format *fmt_out,u32 pixfmt_cap) +{ +int i; + +for (i =0 ; fmt_out->pixfmts_cap[i]; i++) +if (fmt_out->pixfmts_cap[i] == pixfmt_cap) +return 1 ; + +return 0 ; +} + +static const struct amvdec_format * +vdec_try_fmt_common (struct amvdec_session *sess,u32 size, +struct v4l2_format *f) +{ +struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; +struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt; +const struct amvdec_format *fmts = sess->core->platform->formats; +const struct amvdec_format *fmt_out; + +memset (pfmt[0 ].reserved,0 ,sizeof (pfmt[0 ].reserved)); +memset (pixmp->reserved,0 ,sizeof (pixmp->reserved)); + +if (f->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) { + fmt_out =find_format (fmts, size, pixmp->pixelformat); +if (!fmt_out) { + pixmp->pixelformat =V4L2_PIX_FMT_MPEG2 ; + fmt_out =find_format (fmts, size, pixmp->pixelformat); + } + + pfmt[0 ].sizeimage = +get_output_size (pixmp->width, pixmp->height); + pfmt[0 ].bytesperline =0 ; + pixmp->num_planes =1 ; + }else if (f->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ) { + fmt_out = sess->fmt_out; +if (!vdec_supports_pixfmt_cap (fmt_out, pixmp->pixelformat)) + pixmp->pixelformat = fmt_out->pixfmts_cap[0 ]; + +memset (pfmt[1 ].reserved,0 ,sizeof (pfmt[1 ].reserved)); +if (pixmp->pixelformat ==V4L2_PIX_FMT_NV12M ) { + pfmt[0 ].sizeimage = +get_output_size (pixmp->width, pixmp->height); + pfmt[0 ].bytesperline =ALIGN (pixmp->width,64 ); + + pfmt[1 ].sizeimage = +get_output_size (pixmp->width, pixmp->height) /2 ; + pfmt[1 ].bytesperline =ALIGN (pixmp->width,64 ); + pixmp->num_planes =2 ; + }else if (pixmp->pixelformat ==V4L2_PIX_FMT_YUV420M ) { + pfmt[0 ].sizeimage = +get_output_size (pixmp->width, pixmp->height); + pfmt[0 ].bytesperline =ALIGN (pixmp->width,64 ); + + pfmt[1 ].sizeimage = +get_output_size (pixmp->width, pixmp->height) /4 ; + pfmt[1 ].bytesperline =ALIGN (pixmp->width,64 ) /2 ; + + pfmt[2 ].sizeimage = +get_output_size (pixmp->width, pixmp->height) /4 ; + pfmt[2 ].bytesperline =ALIGN (pixmp->width,64 ) /2 ; + pixmp->num_planes =3 ; + } + }else { +return NULL ; + } + + pixmp->width =clamp (pixmp->width, (u32 )256 , fmt_out->max_width); + pixmp->height =clamp (pixmp->height, (u32 )144 , fmt_out->max_height); + +if (pixmp->field ==V4L2_FIELD_ANY ) + pixmp->field =V4L2_FIELD_NONE ; + +return fmt_out; +} + +static int vdec_try_fmt (struct file *file,void *fh,struct v4l2_format *f) +{ +struct amvdec_session *sess = + container_of(file->private_data, struct amvdec_session, fh); + +vdec_try_fmt_common (sess, sess->core->platform->num_formats, f); + +return 0 ; +} + +static int vdec_g_fmt (struct file *file,void *fh,struct v4l2_format *f) +{ +struct amvdec_session *sess = + container_of(file->private_data, struct amvdec_session, fh); +struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; + +if (f->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ) + pixmp->pixelformat = sess->pixfmt_cap; +else if (f->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) + pixmp->pixelformat = sess->fmt_out->pixfmt; + +if (f->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ) { + pixmp->width = sess->width; + pixmp->height = sess->height; + pixmp->colorspace = sess->colorspace; + pixmp->ycbcr_enc = sess->ycbcr_enc; + pixmp->quantization = sess->quantization; + pixmp->xfer_func = sess->xfer_func; + }else if (f->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) { + pixmp->width = sess->width; + pixmp->height = sess->height; + } + +vdec_try_fmt_common (sess, sess->core->platform->num_formats, f); + +return 0 ; +} + +static int vdec_s_fmt (struct file *file,void *fh,struct v4l2_format *f) +{ +struct amvdec_session *sess = + container_of(file->private_data, struct amvdec_session, fh); +struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; +u32 num_formats = sess->core->platform->num_formats; +const struct amvdec_format *fmt_out; +struct v4l2_pix_format_mplane orig_pixmp; +struct v4l2_format format; +u32 pixfmt_out =0 , pixfmt_cap =0 ; + + orig_pixmp = *pixmp; + + fmt_out =vdec_try_fmt_common (sess, num_formats, f); + +if (f->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) { + pixfmt_out = pixmp->pixelformat; + pixfmt_cap = sess->pixfmt_cap; + }else if (f->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ) { + pixfmt_cap = pixmp->pixelformat; + pixfmt_out = sess->fmt_out->pixfmt; + } + +memset (&format,0 ,sizeof (format)); + + format.type =V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ; + format.fmt.pix_mp.pixelformat = pixfmt_out; + format.fmt.pix_mp.width = orig_pixmp.width; + format.fmt.pix_mp.height = orig_pixmp.height; +vdec_try_fmt_common (sess, num_formats, &format); + +if (f->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) { + sess->width = format.fmt.pix_mp.width; + sess->height = format.fmt.pix_mp.height; + sess->colorspace = pixmp->colorspace; + sess->ycbcr_enc = pixmp->ycbcr_enc; + sess->quantization = pixmp->quantization; + sess->xfer_func = pixmp->xfer_func; + } + +memset (&format,0 ,sizeof (format)); + + format.type =V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ; + format.fmt.pix_mp.pixelformat = pixfmt_cap; + format.fmt.pix_mp.width = orig_pixmp.width; + format.fmt.pix_mp.height = orig_pixmp.height; +vdec_try_fmt_common (sess, num_formats, &format); + + sess->width = format.fmt.pix_mp.width; + sess->height = format.fmt.pix_mp.height; + +if (f->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) + sess->fmt_out = fmt_out; +else if (f->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ) + sess->pixfmt_cap = format.fmt.pix_mp.pixelformat; + +return 0 ; +} + +static int vdec_enum_fmt (struct file *file,void *fh,struct v4l2_fmtdesc *f) +{ +struct amvdec_session *sess = + container_of(file->private_data, struct amvdec_session, fh); +const struct vdec_platform *platform = sess->core->platform; +const struct amvdec_format *fmt_out; + +memset (f->reserved,0 ,sizeof (f->reserved)); + +if (f->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) { +if (f->index >= platform->num_formats) +return -EINVAL ; + + fmt_out = &platform->formats[f->index]; + f->pixelformat = fmt_out->pixfmt; + }else if (f->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ) { + fmt_out = sess->fmt_out; +if (f->index >=4 || !fmt_out->pixfmts_cap[f->index]) +return -EINVAL ; + + f->pixelformat = fmt_out->pixfmts_cap[f->index]; + }else { +return -EINVAL ; + } + +return 0 ; +} + +static int vdec_enum_framesizes (struct file *file,void *fh, +struct v4l2_frmsizeenum *fsize) +{ +struct amvdec_session *sess = + container_of(file->private_data, struct amvdec_session, fh); +const struct amvdec_format *formats = sess->core->platform->formats; +const struct amvdec_format *fmt; +u32 num_formats = sess->core->platform->num_formats; + + fmt =find_format (formats, num_formats, fsize->pixel_format); +if (!fmt || fsize->index) +return -EINVAL ; + + fsize->type =V4L2_FRMSIZE_TYPE_STEPWISE ; + + fsize->stepwise.min_width =256 ; + fsize->stepwise.max_width = fmt->max_width; + fsize->stepwise.step_width =1 ; + fsize->stepwise.min_height =144 ; + fsize->stepwise.max_height = fmt->max_height; + fsize->stepwise.step_height =1 ; + +return 0 ; +} + +static int +vdec_try_decoder_cmd (struct file *file,void *fh,struct v4l2_decoder_cmd *cmd) +{ +switch (cmd->cmd) { +case V4L2_DEC_CMD_STOP : +if (cmd->flags &V4L2_DEC_CMD_STOP_TO_BLACK ) +return -EINVAL ; +break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +static int +vdec_decoder_cmd (struct file *file,void *fh,struct v4l2_decoder_cmd *cmd) +{ +struct amvdec_session *sess = + container_of(file->private_data, struct amvdec_session, fh); +struct amvdec_codec_ops *codec_ops = sess->fmt_out->codec_ops; +struct device *dev = sess->core->dev; +int ret; + + ret =vdec_try_decoder_cmd (file, fh, cmd); +if (ret) +return ret; + +if (!(sess->streamon_out & sess->streamon_cap)) +return 0 ; + +dev_dbg (dev,"Received V4L2_DEC_CMD_STOP\n" ); + sess->should_stop =1 ; + +vdec_wait_inactive (sess); + +if (codec_ops->drain) { + codec_ops->drain (sess); + }else if (codec_ops->eos_sequence) { +u32 len; +const u8 *data = codec_ops->eos_sequence (&len); + +esparser_queue_eos (sess->core, data, len); + } + +return ret; +} + +static int vdec_subscribe_event (struct v4l2_fh *fh, +const struct v4l2_event_subscription *sub) +{ +switch (sub->type) { +case V4L2_EVENT_EOS : +return v4l2_event_subscribe (fh, sub,2 ,NULL ); +default : +return -EINVAL ; + } +} + +static int vdec_cropcap (struct file *file,void *fh, +struct v4l2_cropcap *crop) +{ +struct amvdec_session *sess = + container_of(file->private_data, struct amvdec_session, fh); + +if (crop->type !=V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ) +return -EINVAL ; + + crop->pixelaspect = sess->pixelaspect; +return 0 ; +} + +static const struct v4l2_ioctl_ops vdec_ioctl_ops = { + .vidioc_querycap = vdec_querycap, + .vidioc_enum_fmt_vid_cap_mplane = vdec_enum_fmt, + .vidioc_enum_fmt_vid_out_mplane = vdec_enum_fmt, + .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt, + .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt, + .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt, + .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt, + .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt, + .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt, + .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, + .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, + .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, + .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, + .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, + .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, + .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, + .vidioc_streamon = v4l2_m2m_ioctl_streamon, + .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, + .vidioc_enum_framesizes = vdec_enum_framesizes, + .vidioc_subscribe_event = vdec_subscribe_event, + .vidioc_unsubscribe_event = v4l2_event_unsubscribe, + .vidioc_try_decoder_cmd = vdec_try_decoder_cmd, + .vidioc_decoder_cmd = vdec_decoder_cmd, + .vidioc_cropcap = vdec_cropcap, +}; + +static int m2m_queue_init (void *priv,struct vb2_queue *src_vq, +struct vb2_queue *dst_vq) +{ +struct amvdec_session *sess = priv; +int ret; + + src_vq->type =V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ; + src_vq->io_modes =VB2_MMAP |VB2_DMABUF ; + src_vq->timestamp_flags =V4L2_BUF_FLAG_TIMESTAMP_COPY ; + src_vq->ops = &vdec_vb2_ops; + src_vq->mem_ops = &vb2_dma_contig_memops; + src_vq->drv_priv = sess; + src_vq->buf_struct_size =sizeof (struct dummy_buf ); + src_vq->min_buffers_needed =1 ; + src_vq->dev = sess->core->dev; + src_vq->lock = &sess->lock; + ret =vb2_queue_init (src_vq); +if (ret) +return ret; + + dst_vq->type =V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ; + dst_vq->io_modes =VB2_MMAP |VB2_DMABUF ; + dst_vq->timestamp_flags =V4L2_BUF_FLAG_TIMESTAMP_COPY ; + dst_vq->ops = &vdec_vb2_ops; + dst_vq->mem_ops = &vb2_dma_contig_memops; + dst_vq->drv_priv = sess; + dst_vq->buf_struct_size =sizeof (struct dummy_buf ); + dst_vq->min_buffers_needed =1 ; + dst_vq->dev = sess->core->dev; + dst_vq->lock = &sess->lock; + ret =vb2_queue_init (dst_vq); +if (ret) { +vb2_queue_release (src_vq); +return ret; + } + +return 0 ; +} + +static int vdec_open (struct file *file) +{ +struct amvdec_core *core =video_drvdata (file); +struct device *dev = core->dev; +const struct amvdec_format *formats = core->platform->formats; +struct amvdec_session *sess; +int ret; + + sess =kzalloc (sizeof (*sess),GFP_KERNEL ); +if (!sess) +return -ENOMEM ; + + sess->core = core; + + sess->m2m_dev =v4l2_m2m_init (&vdec_m2m_ops); +if (IS_ERR (sess->m2m_dev)) { +dev_err (dev,"Fail to v4l2_m2m_init\n" ); + ret =PTR_ERR (sess->m2m_dev); + goto err_free_sess; + } + + sess->m2m_ctx =v4l2_m2m_ctx_init (sess->m2m_dev, sess, m2m_queue_init); +if (IS_ERR (sess->m2m_ctx)) { +dev_err (dev,"Fail to v4l2_m2m_ctx_init\n" ); + ret =PTR_ERR (sess->m2m_ctx); + goto err_m2m_release; + } + + sess->pixfmt_cap = formats[0 ].pixfmts_cap[0 ]; + sess->fmt_out = &formats[0 ]; + sess->width =1280 ; + sess->height =720 ; + sess->pixelaspect.numerator =1 ; + sess->pixelaspect.denominator =1 ; + +INIT_LIST_HEAD (&sess->timestamps); +INIT_LIST_HEAD (&sess->bufs_recycle); +INIT_WORK (&sess->esparser_queue_work, esparser_queue_all_src); +mutex_init (&sess->lock); +mutex_init (&sess->bufs_recycle_lock); +spin_lock_init (&sess->ts_spinlock); + +v4l2_fh_init (&sess->fh, core->vdev_dec); +v4l2_fh_add (&sess->fh); + sess->fh.m2m_ctx = sess->m2m_ctx; + file->private_data = &sess->fh; + +return 0 ; + +err_m2m_release: +v4l2_m2m_release (sess->m2m_dev); +err_free_sess: +kfree (sess); +return ret; +} + +static int vdec_close (struct file *file) +{ +struct amvdec_session *sess = + container_of(file->private_data, struct amvdec_session, fh); + +v4l2_m2m_ctx_release (sess->m2m_ctx); +v4l2_m2m_release (sess->m2m_dev); +v4l2_fh_del (&sess->fh); +v4l2_fh_exit (&sess->fh); + +mutex_destroy (&sess->lock); +mutex_destroy (&sess->bufs_recycle_lock); + +kfree (sess); + +return 0 ; +} + +static const struct v4l2_file_operations vdec_fops = { + .owner =THIS_MODULE , + .open = vdec_open, + .release = vdec_close, + .unlocked_ioctl = video_ioctl2, + .poll = v4l2_m2m_fop_poll, + .mmap = v4l2_m2m_fop_mmap, +}; + +static irqreturn_t vdec_isr (int irq,void *data) +{ +struct amvdec_core *core = data; +struct amvdec_session *sess = core->cur_sess; + + sess->last_irq_jiffies =get_jiffies_64 (); + +return sess->fmt_out->codec_ops->isr (sess); +} + +static irqreturn_t vdec_threaded_isr (int irq,void *data) +{ +struct amvdec_core *core = data; +struct amvdec_session *sess = core->cur_sess; + +return sess->fmt_out->codec_ops->threaded_isr (sess); +} + +static const struct of_device_id vdec_dt_match[] = { + { .compatible ="amlogic,gxbb-vdec" , + .data = &vdec_platform_gxbb }, + { .compatible ="amlogic,gxm-vdec" , + .data = &vdec_platform_gxm }, + { .compatible ="amlogic,gxl-vdec" , + .data = &vdec_platform_gxl }, + {} +}; +MODULE_DEVICE_TABLE (of, vdec_dt_match); + +static int vdec_probe (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct video_device *vdev; +struct amvdec_core *core; +struct resource *r; +const struct of_device_id *of_id; +int irq; +int ret; + + core =devm_kzalloc (dev,sizeof (*core),GFP_KERNEL ); +if (!core) +return -ENOMEM ; + + core->dev = dev; +platform_set_drvdata (pdev, core); + + r =platform_get_resource_byname (pdev,IORESOURCE_MEM ,"dos" ); + core->dos_base =devm_ioremap_resource (dev, r); +if (IS_ERR (core->dos_base)) { +dev_err (dev,"Couldn't remap DOS memory\n" ); +return PTR_ERR (core->dos_base); + } + + r =platform_get_resource_byname (pdev,IORESOURCE_MEM ,"esparser" ); + core->esparser_base =devm_ioremap_resource (dev, r); +if (IS_ERR (core->esparser_base)) { +dev_err (dev,"Couldn't remap ESPARSER memory\n" ); +return PTR_ERR (core->esparser_base); + } + + core->regmap_ao =syscon_regmap_lookup_by_phandle (dev->of_node, +"amlogic,ao-sysctrl" ); +if (IS_ERR (core->regmap_ao)) { +dev_err (dev,"Couldn't regmap AO sysctrl\n" ); +return PTR_ERR (core->regmap_ao); + } + + core->canvas =meson_canvas_get (dev); +if (!core->canvas) +return PTR_ERR (core->canvas); + + core->dos_parser_clk =devm_clk_get (dev,"dos_parser" ); +if (IS_ERR (core->dos_parser_clk)) +return -EPROBE_DEFER ; + + core->dos_clk =devm_clk_get (dev,"dos" ); +if (IS_ERR (core->dos_clk)) +return -EPROBE_DEFER ; + + core->vdec_1_clk =devm_clk_get (dev,"vdec_1" ); +if (IS_ERR (core->vdec_1_clk)) +return -EPROBE_DEFER ; + + core->vdec_hevc_clk =devm_clk_get (dev,"vdec_hevc" ); +if (IS_ERR (core->vdec_hevc_clk)) +return -EPROBE_DEFER ; + + irq =platform_get_irq_byname (pdev,"vdec" ); +if (irq <0 ) +return irq; + + ret =devm_request_threaded_irq (core->dev, irq, vdec_isr, + vdec_threaded_isr,IRQF_ONESHOT , +"vdec" , core); +if (ret) +return ret; + + ret =esparser_init (pdev, core); +if (ret) +return ret; + + ret =v4l2_device_register (dev, &core->v4l2_dev); +if (ret) { +dev_err (dev,"Couldn't register v4l2 device\n" ); +return -ENOMEM ; + } + + vdev =video_device_alloc (); +if (!vdev) { + ret = -ENOMEM ; + goto err_vdev_release; + } + + of_id =of_match_node (vdec_dt_match, dev->of_node); + core->platform = of_id->data; + core->vdev_dec = vdev; + core->dev_dec = dev; +mutex_init (&core->lock); + +strlcpy (vdev->name,"meson-video-decoder" ,sizeof (vdev->name)); + vdev->release = video_device_release; + vdev->fops = &vdec_fops; + vdev->ioctl_ops = &vdec_ioctl_ops; + vdev->vfl_dir =VFL_DIR_M2M ; + vdev->v4l2_dev = &core->v4l2_dev; + vdev->lock = &core->lock; + vdev->device_caps =V4L2_CAP_VIDEO_M2M_MPLANE |V4L2_CAP_STREAMING ; + +video_set_drvdata (vdev, core); + + ret =video_register_device (vdev,VFL_TYPE_GRABBER ,-1 ); +if (ret) { +dev_err (dev,"Failed registering video device\n" ); + goto err_vdev_release; + } + +return 0 ; + +err_vdev_release: +video_device_release (vdev); +return ret; +} + +static int vdec_remove (struct platform_device *pdev) +{ +struct amvdec_core *core =platform_get_drvdata (pdev); + +video_unregister_device (core->vdev_dec); + +return 0 ; +} + +static struct platform_driver meson_vdec_driver = { + .probe = vdec_probe, + .remove = vdec_remove,
drivers/media/platform/meson/vdec/vdec.c line 1024
Quoted 10 lines
drivers/media/platform/meson/vdec/vdec.c new file +10
-0+ .driver = { + .name = "meson-vdec", + .of_match_table = vdec_dt_match, + }, +}; +module_platform_driver (meson_vdec_driver); + +MODULE_DESCRIPTION ("Meson video decoder driver for GXBB/GXL/GXM" ); +MODULE_AUTHOR ("Maxime Jourdan <[email protected]>" ); +MODULE_LICENSE ("GPL" );
<snip>
Regards,
Hans
Re: [PATCH v3 2/3] media: meson: add v4l2 m2m video decoder driver
Le lun. 1 oct. 2018 ? 12:26, Hans Verkuil <[email protected]> a ?crit :
Quoted 252 lines · Maxime Jourdan
Quoted 47 lines
Amlogic SoCs feature a powerful video decoder unit able to decode many formats, with a performance of usually up to 4k60.
This is a driver for this IP that is based around the v4l2 m2m framework.
It features decoding for:
- MPEG 1
- MPEG 2Supported SoCs are: GXBB (S905), GXL (S905X/W/D), GXM (S912)
There is also a hardware bitstream parser (ESPARSER) that is handled here.
Signed-off-by Maxime Jourdan <[email protected]>--- drivers/media/platform/Kconfig | 10 + drivers/media/platform/meson/Makefile | 1 + drivers/media/platform/meson/vdec/Makefile | 8 + .../media/platform/meson/vdec/codec_mpeg12.c | 209 ++++ .../media/platform/meson/vdec/codec_mpeg12.h | 14 + drivers/media/platform/meson/vdec/dos_regs.h | 98 ++ drivers/media/platform/meson/vdec/esparser.c | 322 ++++++ drivers/media/platform/meson/vdec/esparser.h | 32 + drivers/media/platform/meson/vdec/vdec.c | 1024 +++++++++++++++++ drivers/media/platform/meson/vdec/vdec.h | 251 ++++ drivers/media/platform/meson/vdec/vdec_1.c | 231 ++++ drivers/media/platform/meson/vdec/vdec_1.h | 14 + .../media/platform/meson/vdec/vdec_helpers.c | 412 +++++++ .../media/platform/meson/vdec/vdec_helpers.h | 48 + .../media/platform/meson/vdec/vdec_platform.c | 101 ++ .../media/platform/meson/vdec/vdec_platform.h | 30 + 16 files changed, 2805 insertions(+) create mode 100644 drivers/media/platform/meson/vdec/Makefile create mode 100644 drivers/media/platform/meson/vdec/codec_mpeg12.c create mode 100644 drivers/media/platform/meson/vdec/codec_mpeg12.h create mode 100644 drivers/media/platform/meson/vdec/dos_regs.h create mode 100644 drivers/media/platform/meson/vdec/esparser.c create mode 100644 drivers/media/platform/meson/vdec/esparser.h create mode 100644 drivers/media/platform/meson/vdec/vdec.c create mode 100644 drivers/media/platform/meson/vdec/vdec.h create mode 100644 drivers/media/platform/meson/vdec/vdec_1.c create mode 100644 drivers/media/platform/meson/vdec/vdec_1.h create mode 100644 drivers/media/platform/meson/vdec/vdec_helpers.c create mode 100644 drivers/media/platform/meson/vdec/vdec_helpers.h create mode 100644 drivers/media/platform/meson/vdec/vdec_platform.c create mode 100644 drivers/media/platform/meson/vdec/vdec_platform.h<snip>
Quoted 187 lines
drivers/media/platform/meson/vdec/vdec.c new file +181
-0@@ -0,0 +1,1024 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Copyright (C) 2018 BayLibre, SAS +* Author: Maxime Jourdan <[email protected]> +*/ + +#include <linux/of_device.h> +#include <linux/clk.h> +#include <linux/io.h> +#include <linux/module.h> +#include <linux/platform_device.h> +#include <linux/mfd/syscon.h> +#include <linux/slab.h> +#include <media/v4l2-ioctl.h> +#include <media/v4l2-event.h> +#include <media/v4l2-ctrls.h> +#include <media/v4l2-mem2mem.h> +#include <media/v4l2-dev.h> +#include <media/videobuf2-dma-contig.h> + +#include "vdec.h" +#include "esparser.h" +#include "vdec_helpers.h" + +struct dummy_buf { +struct vb2_v4l2_buffer vb; +struct list_head list; +}; + +/* 16 MiB for parsed bitstream swap exchange */ +#define SIZE_VIFIFO SZ_16M + +static u32 get_output_size (u32 width,u32 height) +{ +return ALIGN (width * height,SZ_64K ); +} + +u32 amvdec_get_output_size (struct amvdec_session *sess) +{ +return get_output_size (sess->width, sess->height); +} +EXPORT_SYMBOL_GPL (amvdec_get_output_size); + +static int vdec_codec_needs_recycle (struct amvdec_session *sess) +{ +struct amvdec_codec_ops *codec_ops = sess->fmt_out->codec_ops; + +return codec_ops->can_recycle && codec_ops->recycle; +} + +static int vdec_recycle_thread (void *data) +{ +struct amvdec_session *sess = data; +struct amvdec_core *core = sess->core; +struct amvdec_codec_ops *codec_ops = sess->fmt_out->codec_ops; +struct amvdec_buffer *tmp, *n; + +while (!kthread_should_stop ()) { +mutex_lock (&sess->bufs_recycle_lock); +list_for_each_entry_safe (tmp, n, &sess->bufs_recycle, list) { +if (!codec_ops->can_recycle (core)) +break ; + + codec_ops->recycle (core, tmp->vb->index); +list_del (&tmp->list); +kfree (tmp); + } +mutex_unlock (&sess->bufs_recycle_lock); + +usleep_range (5000 ,10000 ); + } + +return 0 ; +} + +static int vdec_poweron (struct amvdec_session *sess) +{ +int ret; +struct amvdec_ops *vdec_ops = sess->fmt_out->vdec_ops; + + ret =clk_prepare_enable (sess->core->dos_parser_clk); +if (ret) +return ret; + + ret =clk_prepare_enable (sess->core->dos_clk); +if (ret) + goto disable_dos_parser; + + ret = vdec_ops->start (sess); +if (ret) + goto disable_dos; + +esparser_power_up (sess); + +return 0 ; + +disable_dos: +clk_disable_unprepare (sess->core->dos_clk); +disable_dos_parser: +clk_disable_unprepare (sess->core->dos_parser_clk); + +return ret; +} + +static void vdec_wait_inactive (struct amvdec_session *sess) +{ +/* We consider 50ms with no IRQ to be inactive. */ +while (time_is_after_jiffies64 (sess->last_irq_jiffies + +msecs_to_jiffies (50 ))) +msleep (25 ); +} + +static void vdec_poweroff (struct amvdec_session *sess) +{ +struct amvdec_ops *vdec_ops = sess->fmt_out->vdec_ops; +struct amvdec_codec_ops *codec_ops = sess->fmt_out->codec_ops; + +vdec_wait_inactive (sess); +if (codec_ops->drain) + codec_ops->drain (sess); + + vdec_ops->stop (sess); +clk_disable_unprepare (sess->core->dos_clk); +clk_disable_unprepare (sess->core->dos_parser_clk); +} + +static void +vdec_queue_recycle (struct amvdec_session *sess,struct vb2_buffer *vb) +{ +struct amvdec_buffer *new_buf; + + new_buf =kmalloc (sizeof (*new_buf),GFP_KERNEL ); + new_buf->vb = vb; + +mutex_lock (&sess->bufs_recycle_lock); +list_add_tail (&new_buf->list, &sess->bufs_recycle); +mutex_unlock (&sess->bufs_recycle_lock); +} + +static void vdec_m2m_device_run (void *priv) +{ +struct amvdec_session *sess = priv; + +schedule_work (&sess->esparser_queue_work); +} + +static void vdec_m2m_job_abort (void *priv) +{ +struct amvdec_session *sess = priv; + +v4l2_m2m_job_finish (sess->m2m_dev, sess->m2m_ctx); +} + +static const struct v4l2_m2m_ops vdec_m2m_ops = { + .device_run = vdec_m2m_device_run, + .job_abort = vdec_m2m_job_abort, +}; + +static int vdec_queue_setup (struct vb2_queue *q, +unsigned int *num_buffers,unsigned int *num_planes, +unsigned int sizes[],struct device *alloc_devs[]) +{ +struct amvdec_session *sess =vb2_get_drv_priv (q); +const struct amvdec_format *fmt_out = sess->fmt_out; +u32 output_size =amvdec_get_output_size (sess); + +if (*num_planes) { +switch (q->type) { +case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : +if (*num_planes !=1 || sizes[0 ] < output_size) +return -EINVAL ; +break ; +case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : +switch (sess->pixfmt_cap) { +case V4L2_PIX_FMT_NV12M : +if (*num_planes !=2 || + sizes[0 ] < output_size || + sizes[1 ] < output_size /2 ) +return -EINVAL ; +break ; +case V4L2_PIX_FMT_YUV420M :drivers/media/platform/meson/vdec/vdec.c line 191
Quoted 10 lines
drivers/media/platform/meson/vdec/vdec.c new file +10
-0+if (*num_planes !=3 || + sizes[0 ] < output_size || + sizes[1 ] < output_size /4 || + sizes[2 ] < output_size /4 ) +return -EINVAL ; +break ; +default : +return -EINVAL ; + } +break ;You want to clamp
*num_buffershere as well (and likely updatemin_buffers_needed).
Quoted 4 lines
Note that
*num_buffersin this case refers to the number of buffers thatVIDIOC_CREATE_BUFSwants to add. So the total number of buffers after this call is actually*num_buffers+q->num_buffers.
Good point, ack.
Quoted 22 lines
Quoted 22 lines
drivers/media/platform/meson/vdec/vdec.c new file +22
-0+ } + +return 0 ; + } + +switch (q->type) { +case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : + sizes[0 ] =amvdec_get_output_size (sess); + *num_planes =1 ; +break ; +case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : +switch (sess->pixfmt_cap) { +case V4L2_PIX_FMT_NV12M : + sizes[0 ] = output_size; + sizes[1 ] = output_size /2 ; + *num_planes =2 ; +break ; +case V4L2_PIX_FMT_YUV420M : + sizes[0 ] = output_size; + sizes[1 ] = output_size /4 ; + sizes[2 ] = output_size /4 ; + *num_planes =3 ;
Quoted 9 lines
drivers/media/platform/meson/vdec/vdec.c line 219
Quoted 6 lines
drivers/media/platform/meson/vdec/vdec.c new file +6
-0+break ; +default : +return -EINVAL ; + } + *num_buffers =min (max (*num_buffers, fmt_out->min_buffers), + fmt_out->max_buffers);You can use clamp here. That's easier to read.
Ack
Quoted 5 lines
drivers/media/platform/meson/vdec/vdec.c line 220
Quoted 1 line
drivers/media/platform/meson/vdec/vdec.c new file +1
-0+/* The HW needs all buffers to be configured during startup */ Why? I kind of expected to see '
q->min_buffers_needed=fmt_out->min_buffers' here. I think some more information is needed here in the comment.
I'll extend the comments to reflect the following:
All codecs in the Amlogic vdec need the full available buffer list to be configured at startup, i.e all buffer phy addrs must be written to registers prior to decoding.
The firmwares then decide how they use those buffers and the interrupts only tell me "the decoder has written a frame to buffer N? X".
fmt_out->min_buffers and fmt_out->max_buffers refer to the min/max amount of buffers that can be setup during initialization. In the case of MPEG2, the firmware expects 8 buffers, no more no less, so both min_buffers and max_buffers have the value "8".
But even if those values differ (as for H.264 that will come later), the firmware still expects all allocated buffers to be setup in registers. As such, q->min_buffers_needed must reflect the total amount of CAPTURE buffers.
Quoted 168 lines
Quoted 168 lines
drivers/media/platform/meson/vdec/vdec.c new file +168
-0+ q->min_buffers_needed = *num_buffers; +break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +static void vdec_vb2_buf_queue (struct vb2_buffer *vb) +{ +struct vb2_v4l2_buffer *vbuf =to_vb2_v4l2_buffer (vb); +struct amvdec_session *sess =vb2_get_drv_priv (vb->vb2_queue); +struct v4l2_m2m_ctx *m2m_ctx = sess->m2m_ctx; + +v4l2_m2m_buf_queue (m2m_ctx, vbuf); + +if (!sess->streamon_out || !sess->streamon_cap) +return ; + +if (vb->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && +vdec_codec_needs_recycle (sess)) +vdec_queue_recycle (sess, vb); + +schedule_work (&sess->esparser_queue_work); +} + +static int vdec_start_streaming (struct vb2_queue *q,unsigned int count) +{ +struct amvdec_session *sess =vb2_get_drv_priv (q); +struct amvdec_core *core = sess->core; +struct vb2_v4l2_buffer *buf; +int ret; + +if (core->cur_sess && core->cur_sess != sess) { + ret = -EBUSY ; + goto bufs_done; + } + +if (q->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) + sess->streamon_out =1 ; +else + sess->streamon_cap =1 ; + +if (!sess->streamon_out || !sess->streamon_cap) +return 0 ; + + sess->vififo_size =SIZE_VIFIFO ; + sess->vififo_vaddr = +dma_alloc_coherent (sess->core->dev, sess->vififo_size, + &sess->vififo_paddr,GFP_KERNEL ); +if (!sess->vififo_vaddr) { +dev_err (sess->core->dev,"Failed to request VIFIFO buffer\n" ); + ret = -ENOMEM ; + goto bufs_done; + } + + sess->should_stop =0 ; + sess->keyframe_found =0 ; + sess->last_offset =0 ; + sess->wrap_count =0 ; + sess->pixelaspect.numerator =1 ; + sess->pixelaspect.denominator =1 ; +atomic_set (&sess->esparser_queued_bufs,0 ); + + ret =vdec_poweron (sess); +if (ret) + goto vififo_free; + + sess->sequence_cap =0 ; +if (vdec_codec_needs_recycle (sess)) + sess->recycle_thread =kthread_run (vdec_recycle_thread, sess, +"vdec_recycle" ); + + core->cur_sess = sess; + +return 0 ; + +vififo_free: +dma_free_coherent (sess->core->dev, sess->vififo_size, + sess->vififo_vaddr, sess->vififo_paddr); +bufs_done: +while ((buf =v4l2_m2m_src_buf_remove (sess->m2m_ctx))) +v4l2_m2m_buf_done (buf,VB2_BUF_STATE_QUEUED ); +while ((buf =v4l2_m2m_dst_buf_remove (sess->m2m_ctx))) +v4l2_m2m_buf_done (buf,VB2_BUF_STATE_QUEUED ); + +if (q->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) + sess->streamon_out =0 ; +else + sess->streamon_cap =0 ; + +return ret; +} + +static void vdec_free_canvas (struct amvdec_session *sess) +{ +int i; + +for (i =0 ; i < sess->canvas_num; ++i) +meson_canvas_free (sess->core->canvas, sess->canvas_alloc[i]); + + sess->canvas_num =0 ; +} + +static void vdec_reset_timestamps (struct amvdec_session *sess) +{ +struct amvdec_timestamp *tmp, *n; + +list_for_each_entry_safe (tmp, n, &sess->timestamps, list) { +list_del (&tmp->list); +kfree (tmp); + } +} + +static void vdec_reset_bufs_recycle (struct amvdec_session *sess) +{ +struct amvdec_buffer *tmp, *n; + +list_for_each_entry_safe (tmp, n, &sess->bufs_recycle, list) { +list_del (&tmp->list); +kfree (tmp); + } +} + +static void vdec_stop_streaming (struct vb2_queue *q) +{ +struct amvdec_session *sess =vb2_get_drv_priv (q); +struct amvdec_core *core = sess->core; +struct vb2_v4l2_buffer *buf; + +if (sess->streamon_out && sess->streamon_cap) { +if (vdec_codec_needs_recycle (sess)) +kthread_stop (sess->recycle_thread); + +vdec_poweroff (sess); +vdec_free_canvas (sess); +dma_free_coherent (sess->core->dev, sess->vififo_size, + sess->vififo_vaddr, sess->vififo_paddr); +vdec_reset_timestamps (sess); +vdec_reset_bufs_recycle (sess); +kfree (sess->priv); + sess->priv =NULL ; + core->cur_sess =NULL ; + } + +if (q->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) { +while ((buf =v4l2_m2m_src_buf_remove (sess->m2m_ctx))) +v4l2_m2m_buf_done (buf,VB2_BUF_STATE_ERROR ); + + sess->streamon_out =0 ; + }else { +while ((buf =v4l2_m2m_dst_buf_remove (sess->m2m_ctx))) +v4l2_m2m_buf_done (buf,VB2_BUF_STATE_ERROR ); + + sess->streamon_cap =0 ; + } +} + +static const struct vb2_ops vdec_vb2_ops = { + .queue_setup = vdec_queue_setup, + .start_streaming = vdec_start_streaming, + .stop_streaming = vdec_stop_streaming, + .buf_queue = vdec_vb2_buf_queue, + .wait_prepare = vb2_ops_wait_prepare, + .wait_finish = vb2_ops_wait_finish, +}; +
Quoted 9 lines
drivers/media/platform/meson/vdec/vdec.c line 394
Quoted 6 lines
drivers/media/platform/meson/vdec/vdec.c new file +6
-0+static int +vdec_querycap (struct file *file,void *fh,struct v4l2_capability *cap) +{ +strlcpy (cap->driver,"meson-vdec" ,sizeof (cap->driver)); +strlcpy (cap->card,"Amlogic Video Decoder" ,sizeof (cap->card)); +strlcpy (cap->bus_info,"platform:meson-vdec" ,sizeof (cap->bus_info));Replace all strlcpy/strcpy/strncpy by strscpy. That's the recommended function.
Ack
Quoted 633 lines
Quoted 630 lines
drivers/media/platform/meson/vdec/vdec.c new file +630
-0+ +return 0 ; +} + +static const struct amvdec_format * +find_format (const struct amvdec_format *fmts,u32 size,u32 pixfmt) +{ +unsigned int i; + +for (i =0 ; i < size; i++) { +if (fmts[i].pixfmt == pixfmt) +return &fmts[i]; + } + +return NULL ; +} + +static unsigned int +vdec_supports_pixfmt_cap (const struct amvdec_format *fmt_out,u32 pixfmt_cap) +{ +int i; + +for (i =0 ; fmt_out->pixfmts_cap[i]; i++) +if (fmt_out->pixfmts_cap[i] == pixfmt_cap) +return 1 ; + +return 0 ; +} + +static const struct amvdec_format * +vdec_try_fmt_common (struct amvdec_session *sess,u32 size, +struct v4l2_format *f) +{ +struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; +struct v4l2_plane_pix_format *pfmt = pixmp->plane_fmt; +const struct amvdec_format *fmts = sess->core->platform->formats; +const struct amvdec_format *fmt_out; + +memset (pfmt[0 ].reserved,0 ,sizeof (pfmt[0 ].reserved)); +memset (pixmp->reserved,0 ,sizeof (pixmp->reserved)); + +if (f->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) { + fmt_out =find_format (fmts, size, pixmp->pixelformat); +if (!fmt_out) { + pixmp->pixelformat =V4L2_PIX_FMT_MPEG2 ; + fmt_out =find_format (fmts, size, pixmp->pixelformat); + } + + pfmt[0 ].sizeimage = +get_output_size (pixmp->width, pixmp->height); + pfmt[0 ].bytesperline =0 ; + pixmp->num_planes =1 ; + }else if (f->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ) { + fmt_out = sess->fmt_out; +if (!vdec_supports_pixfmt_cap (fmt_out, pixmp->pixelformat)) + pixmp->pixelformat = fmt_out->pixfmts_cap[0 ]; + +memset (pfmt[1 ].reserved,0 ,sizeof (pfmt[1 ].reserved)); +if (pixmp->pixelformat ==V4L2_PIX_FMT_NV12M ) { + pfmt[0 ].sizeimage = +get_output_size (pixmp->width, pixmp->height); + pfmt[0 ].bytesperline =ALIGN (pixmp->width,64 ); + + pfmt[1 ].sizeimage = +get_output_size (pixmp->width, pixmp->height) /2 ; + pfmt[1 ].bytesperline =ALIGN (pixmp->width,64 ); + pixmp->num_planes =2 ; + }else if (pixmp->pixelformat ==V4L2_PIX_FMT_YUV420M ) { + pfmt[0 ].sizeimage = +get_output_size (pixmp->width, pixmp->height); + pfmt[0 ].bytesperline =ALIGN (pixmp->width,64 ); + + pfmt[1 ].sizeimage = +get_output_size (pixmp->width, pixmp->height) /4 ; + pfmt[1 ].bytesperline =ALIGN (pixmp->width,64 ) /2 ; + + pfmt[2 ].sizeimage = +get_output_size (pixmp->width, pixmp->height) /4 ; + pfmt[2 ].bytesperline =ALIGN (pixmp->width,64 ) /2 ; + pixmp->num_planes =3 ; + } + }else { +return NULL ; + } + + pixmp->width =clamp (pixmp->width, (u32 )256 , fmt_out->max_width); + pixmp->height =clamp (pixmp->height, (u32 )144 , fmt_out->max_height); + +if (pixmp->field ==V4L2_FIELD_ANY ) + pixmp->field =V4L2_FIELD_NONE ; + +return fmt_out; +} + +static int vdec_try_fmt (struct file *file,void *fh,struct v4l2_format *f) +{ +struct amvdec_session *sess = + container_of(file->private_data, struct amvdec_session, fh); + +vdec_try_fmt_common (sess, sess->core->platform->num_formats, f); + +return 0 ; +} + +static int vdec_g_fmt (struct file *file,void *fh,struct v4l2_format *f) +{ +struct amvdec_session *sess = + container_of(file->private_data, struct amvdec_session, fh); +struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; + +if (f->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ) + pixmp->pixelformat = sess->pixfmt_cap; +else if (f->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) + pixmp->pixelformat = sess->fmt_out->pixfmt; + +if (f->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ) { + pixmp->width = sess->width; + pixmp->height = sess->height; + pixmp->colorspace = sess->colorspace; + pixmp->ycbcr_enc = sess->ycbcr_enc; + pixmp->quantization = sess->quantization; + pixmp->xfer_func = sess->xfer_func; + }else if (f->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) { + pixmp->width = sess->width; + pixmp->height = sess->height; + } + +vdec_try_fmt_common (sess, sess->core->platform->num_formats, f); + +return 0 ; +} + +static int vdec_s_fmt (struct file *file,void *fh,struct v4l2_format *f) +{ +struct amvdec_session *sess = + container_of(file->private_data, struct amvdec_session, fh); +struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; +u32 num_formats = sess->core->platform->num_formats; +const struct amvdec_format *fmt_out; +struct v4l2_pix_format_mplane orig_pixmp; +struct v4l2_format format; +u32 pixfmt_out =0 , pixfmt_cap =0 ; + + orig_pixmp = *pixmp; + + fmt_out =vdec_try_fmt_common (sess, num_formats, f); + +if (f->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) { + pixfmt_out = pixmp->pixelformat; + pixfmt_cap = sess->pixfmt_cap; + }else if (f->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ) { + pixfmt_cap = pixmp->pixelformat; + pixfmt_out = sess->fmt_out->pixfmt; + } + +memset (&format,0 ,sizeof (format)); + + format.type =V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ; + format.fmt.pix_mp.pixelformat = pixfmt_out; + format.fmt.pix_mp.width = orig_pixmp.width; + format.fmt.pix_mp.height = orig_pixmp.height; +vdec_try_fmt_common (sess, num_formats, &format); + +if (f->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) { + sess->width = format.fmt.pix_mp.width; + sess->height = format.fmt.pix_mp.height; + sess->colorspace = pixmp->colorspace; + sess->ycbcr_enc = pixmp->ycbcr_enc; + sess->quantization = pixmp->quantization; + sess->xfer_func = pixmp->xfer_func; + } + +memset (&format,0 ,sizeof (format)); + + format.type =V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ; + format.fmt.pix_mp.pixelformat = pixfmt_cap; + format.fmt.pix_mp.width = orig_pixmp.width; + format.fmt.pix_mp.height = orig_pixmp.height; +vdec_try_fmt_common (sess, num_formats, &format); + + sess->width = format.fmt.pix_mp.width; + sess->height = format.fmt.pix_mp.height; + +if (f->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) + sess->fmt_out = fmt_out; +else if (f->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ) + sess->pixfmt_cap = format.fmt.pix_mp.pixelformat; + +return 0 ; +} + +static int vdec_enum_fmt (struct file *file,void *fh,struct v4l2_fmtdesc *f) +{ +struct amvdec_session *sess = + container_of(file->private_data, struct amvdec_session, fh); +const struct vdec_platform *platform = sess->core->platform; +const struct amvdec_format *fmt_out; + +memset (f->reserved,0 ,sizeof (f->reserved)); + +if (f->type ==V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ) { +if (f->index >= platform->num_formats) +return -EINVAL ; + + fmt_out = &platform->formats[f->index]; + f->pixelformat = fmt_out->pixfmt; + }else if (f->type ==V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ) { + fmt_out = sess->fmt_out; +if (f->index >=4 || !fmt_out->pixfmts_cap[f->index]) +return -EINVAL ; + + f->pixelformat = fmt_out->pixfmts_cap[f->index]; + }else { +return -EINVAL ; + } + +return 0 ; +} + +static int vdec_enum_framesizes (struct file *file,void *fh, +struct v4l2_frmsizeenum *fsize) +{ +struct amvdec_session *sess = + container_of(file->private_data, struct amvdec_session, fh); +const struct amvdec_format *formats = sess->core->platform->formats; +const struct amvdec_format *fmt; +u32 num_formats = sess->core->platform->num_formats; + + fmt =find_format (formats, num_formats, fsize->pixel_format); +if (!fmt || fsize->index) +return -EINVAL ; + + fsize->type =V4L2_FRMSIZE_TYPE_STEPWISE ; + + fsize->stepwise.min_width =256 ; + fsize->stepwise.max_width = fmt->max_width; + fsize->stepwise.step_width =1 ; + fsize->stepwise.min_height =144 ; + fsize->stepwise.max_height = fmt->max_height; + fsize->stepwise.step_height =1 ; + +return 0 ; +} + +static int +vdec_try_decoder_cmd (struct file *file,void *fh,struct v4l2_decoder_cmd *cmd) +{ +switch (cmd->cmd) { +case V4L2_DEC_CMD_STOP : +if (cmd->flags &V4L2_DEC_CMD_STOP_TO_BLACK ) +return -EINVAL ; +break ; +default : +return -EINVAL ; + } + +return 0 ; +} + +static int +vdec_decoder_cmd (struct file *file,void *fh,struct v4l2_decoder_cmd *cmd) +{ +struct amvdec_session *sess = + container_of(file->private_data, struct amvdec_session, fh); +struct amvdec_codec_ops *codec_ops = sess->fmt_out->codec_ops; +struct device *dev = sess->core->dev; +int ret; + + ret =vdec_try_decoder_cmd (file, fh, cmd); +if (ret) +return ret; + +if (!(sess->streamon_out & sess->streamon_cap)) +return 0 ; + +dev_dbg (dev,"Received V4L2_DEC_CMD_STOP\n" ); + sess->should_stop =1 ; + +vdec_wait_inactive (sess); + +if (codec_ops->drain) { + codec_ops->drain (sess); + }else if (codec_ops->eos_sequence) { +u32 len; +const u8 *data = codec_ops->eos_sequence (&len); + +esparser_queue_eos (sess->core, data, len); + } + +return ret; +} + +static int vdec_subscribe_event (struct v4l2_fh *fh, +const struct v4l2_event_subscription *sub) +{ +switch (sub->type) { +case V4L2_EVENT_EOS : +return v4l2_event_subscribe (fh, sub,2 ,NULL ); +default : +return -EINVAL ; + } +} + +static int vdec_cropcap (struct file *file,void *fh, +struct v4l2_cropcap *crop) +{ +struct amvdec_session *sess = + container_of(file->private_data, struct amvdec_session, fh); + +if (crop->type !=V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ) +return -EINVAL ; + + crop->pixelaspect = sess->pixelaspect; +return 0 ; +} + +static const struct v4l2_ioctl_ops vdec_ioctl_ops = { + .vidioc_querycap = vdec_querycap, + .vidioc_enum_fmt_vid_cap_mplane = vdec_enum_fmt, + .vidioc_enum_fmt_vid_out_mplane = vdec_enum_fmt, + .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt, + .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt, + .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt, + .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt, + .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt, + .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt, + .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, + .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, + .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, + .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, + .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, + .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, + .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, + .vidioc_streamon = v4l2_m2m_ioctl_streamon, + .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, + .vidioc_enum_framesizes = vdec_enum_framesizes, + .vidioc_subscribe_event = vdec_subscribe_event, + .vidioc_unsubscribe_event = v4l2_event_unsubscribe, + .vidioc_try_decoder_cmd = vdec_try_decoder_cmd, + .vidioc_decoder_cmd = vdec_decoder_cmd, + .vidioc_cropcap = vdec_cropcap, +}; + +static int m2m_queue_init (void *priv,struct vb2_queue *src_vq, +struct vb2_queue *dst_vq) +{ +struct amvdec_session *sess = priv; +int ret; + + src_vq->type =V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ; + src_vq->io_modes =VB2_MMAP |VB2_DMABUF ; + src_vq->timestamp_flags =V4L2_BUF_FLAG_TIMESTAMP_COPY ; + src_vq->ops = &vdec_vb2_ops; + src_vq->mem_ops = &vb2_dma_contig_memops; + src_vq->drv_priv = sess; + src_vq->buf_struct_size =sizeof (struct dummy_buf ); + src_vq->min_buffers_needed =1 ; + src_vq->dev = sess->core->dev; + src_vq->lock = &sess->lock; + ret =vb2_queue_init (src_vq); +if (ret) +return ret; + + dst_vq->type =V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ; + dst_vq->io_modes =VB2_MMAP |VB2_DMABUF ; + dst_vq->timestamp_flags =V4L2_BUF_FLAG_TIMESTAMP_COPY ; + dst_vq->ops = &vdec_vb2_ops; + dst_vq->mem_ops = &vb2_dma_contig_memops; + dst_vq->drv_priv = sess; + dst_vq->buf_struct_size =sizeof (struct dummy_buf ); + dst_vq->min_buffers_needed =1 ; + dst_vq->dev = sess->core->dev; + dst_vq->lock = &sess->lock; + ret =vb2_queue_init (dst_vq); +if (ret) { +vb2_queue_release (src_vq); +return ret; + } + +return 0 ; +} + +static int vdec_open (struct file *file) +{ +struct amvdec_core *core =video_drvdata (file); +struct device *dev = core->dev; +const struct amvdec_format *formats = core->platform->formats; +struct amvdec_session *sess; +int ret; + + sess =kzalloc (sizeof (*sess),GFP_KERNEL ); +if (!sess) +return -ENOMEM ; + + sess->core = core; + + sess->m2m_dev =v4l2_m2m_init (&vdec_m2m_ops); +if (IS_ERR (sess->m2m_dev)) { +dev_err (dev,"Fail to v4l2_m2m_init\n" ); + ret =PTR_ERR (sess->m2m_dev); + goto err_free_sess; + } + + sess->m2m_ctx =v4l2_m2m_ctx_init (sess->m2m_dev, sess, m2m_queue_init); +if (IS_ERR (sess->m2m_ctx)) { +dev_err (dev,"Fail to v4l2_m2m_ctx_init\n" ); + ret =PTR_ERR (sess->m2m_ctx); + goto err_m2m_release; + } + + sess->pixfmt_cap = formats[0 ].pixfmts_cap[0 ]; + sess->fmt_out = &formats[0 ]; + sess->width =1280 ; + sess->height =720 ; + sess->pixelaspect.numerator =1 ; + sess->pixelaspect.denominator =1 ; + +INIT_LIST_HEAD (&sess->timestamps); +INIT_LIST_HEAD (&sess->bufs_recycle); +INIT_WORK (&sess->esparser_queue_work, esparser_queue_all_src); +mutex_init (&sess->lock); +mutex_init (&sess->bufs_recycle_lock); +spin_lock_init (&sess->ts_spinlock); + +v4l2_fh_init (&sess->fh, core->vdev_dec); +v4l2_fh_add (&sess->fh); + sess->fh.m2m_ctx = sess->m2m_ctx; + file->private_data = &sess->fh; + +return 0 ; + +err_m2m_release: +v4l2_m2m_release (sess->m2m_dev); +err_free_sess: +kfree (sess); +return ret; +} + +static int vdec_close (struct file *file) +{ +struct amvdec_session *sess = + container_of(file->private_data, struct amvdec_session, fh); + +v4l2_m2m_ctx_release (sess->m2m_ctx); +v4l2_m2m_release (sess->m2m_dev); +v4l2_fh_del (&sess->fh); +v4l2_fh_exit (&sess->fh); + +mutex_destroy (&sess->lock); +mutex_destroy (&sess->bufs_recycle_lock); + +kfree (sess); + +return 0 ; +} + +static const struct v4l2_file_operations vdec_fops = { + .owner =THIS_MODULE , + .open = vdec_open, + .release = vdec_close, + .unlocked_ioctl = video_ioctl2, + .poll = v4l2_m2m_fop_poll, + .mmap = v4l2_m2m_fop_mmap, +}; + +static irqreturn_t vdec_isr (int irq,void *data) +{ +struct amvdec_core *core = data; +struct amvdec_session *sess = core->cur_sess; + + sess->last_irq_jiffies =get_jiffies_64 (); + +return sess->fmt_out->codec_ops->isr (sess); +} + +static irqreturn_t vdec_threaded_isr (int irq,void *data) +{ +struct amvdec_core *core = data; +struct amvdec_session *sess = core->cur_sess; + +return sess->fmt_out->codec_ops->threaded_isr (sess); +} + +static const struct of_device_id vdec_dt_match[] = { + { .compatible ="amlogic,gxbb-vdec" , + .data = &vdec_platform_gxbb }, + { .compatible ="amlogic,gxm-vdec" , + .data = &vdec_platform_gxm }, + { .compatible ="amlogic,gxl-vdec" , + .data = &vdec_platform_gxl }, + {} +}; +MODULE_DEVICE_TABLE (of, vdec_dt_match); + +static int vdec_probe (struct platform_device *pdev) +{ +struct device *dev = &pdev->dev; +struct video_device *vdev; +struct amvdec_core *core; +struct resource *r; +const struct of_device_id *of_id; +int irq; +int ret; + + core =devm_kzalloc (dev,sizeof (*core),GFP_KERNEL ); +if (!core) +return -ENOMEM ; + + core->dev = dev; +platform_set_drvdata (pdev, core); + + r =platform_get_resource_byname (pdev,IORESOURCE_MEM ,"dos" ); + core->dos_base =devm_ioremap_resource (dev, r); +if (IS_ERR (core->dos_base)) { +dev_err (dev,"Couldn't remap DOS memory\n" ); +return PTR_ERR (core->dos_base); + } + + r =platform_get_resource_byname (pdev,IORESOURCE_MEM ,"esparser" ); + core->esparser_base =devm_ioremap_resource (dev, r); +if (IS_ERR (core->esparser_base)) { +dev_err (dev,"Couldn't remap ESPARSER memory\n" ); +return PTR_ERR (core->esparser_base); + } + + core->regmap_ao =syscon_regmap_lookup_by_phandle (dev->of_node, +"amlogic,ao-sysctrl" ); +if (IS_ERR (core->regmap_ao)) { +dev_err (dev,"Couldn't regmap AO sysctrl\n" ); +return PTR_ERR (core->regmap_ao); + } + + core->canvas =meson_canvas_get (dev); +if (!core->canvas) +return PTR_ERR (core->canvas); + + core->dos_parser_clk =devm_clk_get (dev,"dos_parser" ); +if (IS_ERR (core->dos_parser_clk)) +return -EPROBE_DEFER ; + + core->dos_clk =devm_clk_get (dev,"dos" ); +if (IS_ERR (core->dos_clk)) +return -EPROBE_DEFER ; + + core->vdec_1_clk =devm_clk_get (dev,"vdec_1" ); +if (IS_ERR (core->vdec_1_clk)) +return -EPROBE_DEFER ; + + core->vdec_hevc_clk =devm_clk_get (dev,"vdec_hevc" ); +if (IS_ERR (core->vdec_hevc_clk)) +return -EPROBE_DEFER ; + + irq =platform_get_irq_byname (pdev,"vdec" ); +if (irq <0 ) +return irq; + + ret =devm_request_threaded_irq (core->dev, irq, vdec_isr, + vdec_threaded_isr,IRQF_ONESHOT , +"vdec" , core); +if (ret) +return ret; + + ret =esparser_init (pdev, core); +if (ret) +return ret; + + ret =v4l2_device_register (dev, &core->v4l2_dev); +if (ret) { +dev_err (dev,"Couldn't register v4l2 device\n" ); +return -ENOMEM ; + } + + vdev =video_device_alloc (); +if (!vdev) { + ret = -ENOMEM ; + goto err_vdev_release; + } + + of_id =of_match_node (vdec_dt_match, dev->of_node); + core->platform = of_id->data; + core->vdev_dec = vdev; + core->dev_dec = dev; +mutex_init (&core->lock); + +strlcpy (vdev->name,"meson-video-decoder" ,sizeof (vdev->name)); + vdev->release = video_device_release; + vdev->fops = &vdec_fops; + vdev->ioctl_ops = &vdec_ioctl_ops; + vdev->vfl_dir =VFL_DIR_M2M ; + vdev->v4l2_dev = &core->v4l2_dev; + vdev->lock = &core->lock; + vdev->device_caps =V4L2_CAP_VIDEO_M2M_MPLANE |V4L2_CAP_STREAMING ; + +video_set_drvdata (vdev, core); + + ret =video_register_device (vdev,VFL_TYPE_GRABBER ,-1 ); +if (ret) { +dev_err (dev,"Failed registering video device\n" ); + goto err_vdev_release; + } + +return 0 ; + +err_vdev_release: +video_device_release (vdev); +return ret; +} + +static int vdec_remove (struct platform_device *pdev) +{ +struct amvdec_core *core =platform_get_drvdata (pdev); + +video_unregister_device (core->vdev_dec); + +return 0 ; +} + +static struct platform_driver meson_vdec_driver = { + .probe = vdec_probe, + .remove = vdec_remove, + .driver = { + .name ="meson-vdec" , + .of_match_table = vdec_dt_match, + }, +}; +module_platform_driver (meson_vdec_driver); + +MODULE_DESCRIPTION ("Meson video decoder driver for GXBB/GXL/GXM" ); +MODULE_AUTHOR ("Maxime Jourdan <[email protected]>" ); +MODULE_LICENSE ("GPL" );<snip>
Quoted 3 lines
Regards,
Hans
Cheers,
Maxime
Re: [PATCH v3 2/3] media: meson: add v4l2 m2m video decoder driver
Quoted 57 lines · Maxime Jourdan
Le lun. 1 oct. 2018 ? 12:26, Hans Verkuil <[email protected]> a ?crit :
Quoted 24 lines · Maxime Jourdan
Quoted 22 lines
drivers/media/platform/meson/vdec/vdec.c new file +22
-0+ } + +return 0 ; + } + +switch (q->type) { +case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : + sizes[0 ] =amvdec_get_output_size (sess); + *num_planes =1 ; +break ; +case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : +switch (sess->pixfmt_cap) { +case V4L2_PIX_FMT_NV12M : + sizes[0 ] = output_size; + sizes[1 ] = output_size /2 ; + *num_planes =2 ; +break ; +case V4L2_PIX_FMT_YUV420M : + sizes[0 ] = output_size; + sizes[1 ] = output_size /4 ; + sizes[2 ] = output_size /4 ; + *num_planes =3 ;Quoted 9 lines
drivers/media/platform/meson/vdec/vdec.c line 219
Quoted 6 lines
drivers/media/platform/meson/vdec/vdec.c new file +6
-0+break ; +default : +return -EINVAL ; + } + *num_buffers =min (max (*num_buffers, fmt_out->min_buffers), + fmt_out->max_buffers);You can use clamp here. That's easier to read.
Ack
Quoted 5 lines
Quoted 1 line
+ /* The HW needs all buffers to be configured during startup */
Why? I kind of expected to see '
q->min_buffers_needed=fmt_out->min_buffers' here. I think some more information is needed here in the comment.I'll extend the comments to reflect the following:
All codecs in the Amlogic vdec need the full available buffer list to be configured at startup, i.e all buffer phy addrs must be written to registers prior to decoding.
The firmwares then decide how they use those buffers and the interrupts only tell me "the decoder has written a frame to buffer N? X".
fmt_out->min_buffersandfmt_out->max_buffersrefer to the min/max amount of buffers that can be setup during initialization. In the case of MPEG2, the firmware expects 8 buffers, no more no less, so bothmin_buffersandmax_buffershave the value "8".
Quoted 4 lines
But even if those values differ (as for H.264 that will come later), the firmware still expects all allocated buffers to be setup in registers. As such,
q->min_buffers_neededmust reflect the total amount of CAPTURE buffers.
That's much better, thank you! Now I understand why you do this :-)
Regards,
Hans
PATCH v3 3/3 MAINTAINERS: Add meson video decoder
In reply to Maxime JourdanAdd an entry for the meson video decoder for amlogic SoCs.
--- MAINTAINERS | 8 ++++++++ 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
@@ -9507,6 +9507,14 @@ F: drivers/media/platform/meson/ao-cec.c F: Documentation/devicetree/bindings/media/meson-ao-cec.txt T: git git://linuxtv.org/media_tree.git +MESON VIDEO DECODER DRIVER FOR AMLOGIC SOCS +M: Maxime Jourdan <[email protected]> +L: linux-media at lists.freedesktop.org +L: linux-amlogic at lists.infradead.org +S: Supported +F: drivers/media/platform/meson/vdec/ +T: git git://linuxtv.org/media_tree.git + MICROBLAZE ARCHITECTURE M: Michal Simek <[email protected]> W: http://www.monstr.eu/fdt/
Signature · 1 line
2.19.0
Quoted 37 lines · Maxime Jourdan
Hi everyone,
This patch series adds support for the Amlogic video decoder, as well as the corresponding dt bindings for GXBB/GXL/GXM chips.
It features decoding for the following formats:
- MPEG 1
- MPEG 2The following formats will be added in future patches:
- MJPEG
- MPEG 4 (incl. Xvid, H.263)
- H.264
- HEVC (incl. 10-bit)The following formats' development has still not started, but they are supported by the hardware:
- VC1
- VP9The code was made in such a way to allow easy inclusion of those formats in the future.
The decoder is single instance.
Files:
-vdec.chandles the V4L2 M2M logic
-esparser.cmanages the hardware bitstream parser
-vdec_helpers.cprovides helpers to DONE the dst buffers as well as various common code used by the codecs
-vdec_1.cmanages theVDEC_1block of the vdec IP
-codec_mpeg12.cenables decoding for MPEG 1/2.
-vdec_platform.clinks codec units with vdec units
(e.gvdec_1withcodec_mpeg12) and lists all the available src/dst formats and requirements (max width/height, etc.), per compatible chip.
Quoted 2 lines
Firmwares are necessary to run the vdec. They can currently be found at:
https://github.com/chewitt/meson-firmware
Are you trying to get this into the linux-firmware repository?
I believe that Mauro requires that before he will merge this driver.
So I think this driver will be ready to be merged once v4 is posted, dt-bindings is Acked and the firmware is merged to the linux-firmware repo.
Regards,
Hans
Le lun. 1 oct. 2018 ? 12:29, Hans Verkuil <[email protected]> a ?crit :
Quoted 42 lines · Maxime Jourdan
Quoted 37 lines
Hi everyone,
This patch series adds support for the Amlogic video decoder, as well as the corresponding dt bindings for GXBB/GXL/GXM chips.
It features decoding for the following formats:
- MPEG 1
- MPEG 2The following formats will be added in future patches:
- MJPEG
- MPEG 4 (incl. Xvid, H.263)
- H.264
- HEVC (incl. 10-bit)The following formats' development has still not started, but they are supported by the hardware:
- VC1
- VP9The code was made in such a way to allow easy inclusion of those formats in the future.
The decoder is single instance.
Files:
-vdec.chandles the V4L2 M2M logic
-esparser.cmanages the hardware bitstream parser
-vdec_helpers.cprovides helpers to DONE the dst buffers as well as various common code used by the codecs
-vdec_1.cmanages theVDEC_1block of the vdec IP
-codec_mpeg12.cenables decoding for MPEG 1/2.
-vdec_platform.clinks codec units with vdec units
(e.gvdec_1withcodec_mpeg12) and lists all the available src/dst formats and requirements (max width/height, etc.), per compatible chip.Quoted 2 lines
Firmwares are necessary to run the vdec. They can currently be found at:
https://github.com/chewitt/meson-firmware
Quoted 6 lines
Are you trying to get this into the linux-firmware repository?
I believe that Mauro requires that before he will merge this driver.
So I think this driver will be ready to be merged once v4 is posted, dt-bindings is Acked and the firmware is merged to the linux-firmware repo.
It was planned indeed to get the firmwares into linux-firmware, I'll speed this up.
dt-bindings was reviewed by Rob but I forgot to add it in v3, I'll fix that in v4.
Cheers,
Maxime
Quoted 3 lines
Regards,
Hans