media: v4l2: Add m2m codec helpers
Subsystems (4)
cover media: v4l2: Add m2m codec helpers
Hello,
This patch series is an attempt at factorizing some of the boiler plate code that we find in most stateless codec drivers (and potentially stateful codec ones too). It's been designed mostly based on the hantro and cedrus driver and I decided to implement a third one using these helpers to prove that they can be used more widely.
Those helpers are rather basic, and we could probably extend them (or add new ones) to further simplify codec drivers, but with those in place we already have a good level of code re-usability without forcing anyone to use them if they feel they need things to be done differently.
Note that the H264 codec lib should soon see addition of helpers to create the P/B0/B1 reflists (which are needed by both the hantro and rockchip drivers), but I'd like the uAPI discussion on how to support interlaced content to take place before providing generic helpers for that.
I'd appreciate some feedback on patches 1-4. Patch 5 is here to show how those helpers can be used and has dependencies on various H264 that are still being discussed [1][2], so expect some kbuild failure reports.
Thanks,
Boris
[1]https://patchwork.kernel.org/patch/11004013/ [2]https://patchwork.kernel.org/project/linux-media/list/?series=129567
Boris Brezillon (5):
media: vb2: Add a helper to get the vb2 buffer attached to a request
media: v4l2: Prepare things for addition of m2m codec helpers
media: v4l2: Add m2m codec helpers
media: v4l2: Provide helpers for H264 codecs
media: rockchip: Add the rkvdec driver
.../media/common/videobuf2/videobuf2-core.c | 23 +
drivers/media/v4l2-core/Kconfig | 9 +
drivers/media/v4l2-core/Makefile | 3 +
drivers/media/v4l2-core/v4l2-mem2mem-codec.c | 1170 +++++++++++++++++
.../{v4l2-mem2mem.c => v4l2-mem2mem-core.c} | 0
.../media/v4l2-core/v4l2-mem2mem-h264-codec.c | 47 +
drivers/staging/media/Kconfig | 2 +
drivers/staging/media/Makefile | 1 +
drivers/staging/media/rockchip/Kconfig | 16 +
drivers/staging/media/rockchip/Makefile | 2 +
drivers/staging/media/rockchip/vdec/Kconfig | 16 +
drivers/staging/media/rockchip/vdec/Makefile | 3 +
.../staging/media/rockchip/vdec/rkvdec-h264.c | 909 +++++++++++++
.../staging/media/rockchip/vdec/rkvdec-regs.h | 305 +++++
drivers/staging/media/rockchip/vdec/rkvdec.c | 564 ++++++++
drivers/staging/media/rockchip/vdec/rkvdec.h | 94 ++
include/media/v4l2-mem2mem-codec.h | 317 +++++
include/media/v4l2-mem2mem-h264-codec.h | 100 ++
include/media/videobuf2-core.h | 11 +
19 files changed, 3592 insertions(+)
create mode 100644 drivers/media/v4l2-core/v4l2-mem2mem-codec.c
rename drivers/media/v4l2-core/{v4l2-mem2mem.c => v4l2-mem2mem-core.c} (100%)
create mode 100644 drivers/media/v4l2-core/v4l2-mem2mem-h264-codec.c
create mode 100644 drivers/staging/media/rockchip/Kconfig
create mode 100644 drivers/staging/media/rockchip/Makefile
create mode 100644 drivers/staging/media/rockchip/vdec/Kconfig
create mode 100644 drivers/staging/media/rockchip/vdec/Makefile
create mode 100644 drivers/staging/media/rockchip/vdec/rkvdec-h264.c
create mode 100644 drivers/staging/media/rockchip/vdec/rkvdec-regs.h
create mode 100644 drivers/staging/media/rockchip/vdec/rkvdec.c
create mode 100644 drivers/staging/media/rockchip/vdec/rkvdec.h
create mode 100644 include/media/v4l2-mem2mem-codec.h
create mode 100644 include/media/v4l2-mem2mem-h264-codec.hSignature · 1 line
2.21.0
RFC PATCH 1/5 media: vb2: Add a helper to get the vb2 buffer attached to a request
vb2_request_get_buf() returns the N-th buffer attached to a media request.
--- .../media/common/videobuf2/videobuf2-core.c | 23 +++++++++++++++++++ include/media/videobuf2-core.h | 11 +++++++++ 2 files changed, 34 insertions(+)
drivers/media/common/videobuf2/videobuf2-core.c +23 -0
@@ -1416,6 +1416,29 @@ unsigned int vb2_request_buffer_cnt(struct media_request *req) }EXPORT_SYMBOL_GPL (vb2_request_buffer_cnt); +struct vb2_buffer *vb2_request_get_buf (struct media_request *req, +unsigned int idx) +{ +struct media_request_object *obj; +struct vb2_buffer *buf =NULL ; +unsigned int nbufs =0 ; +unsigned long flags; + +spin_lock_irqsave (&req->lock, flags); +list_for_each_entry (obj, &req->objects, list) { +if (!vb2_request_object_is_buffer (obj) || + nbufs++ < idx) +continue ; + + buf = container_of(obj, struct vb2_buffer, req_obj); +break ; + } +spin_unlock_irqrestore (&req->lock, flags); + +return buf; +} +EXPORT_SYMBOL_GPL (vb2_request_get_buf); +int vb2_core_prepare_buf (struct vb2_queue *q,unsigned int index,void *pb) {struct vb2_buffer *vb;
include/media/videobuf2-core.h +11 -0
@@ -1222,4 +1222,15 @@ bool vb2_request_object_is_buffer(struct media_request_object *obj);*/ unsigned int vb2_request_buffer_cnt (struct media_request *req); +/** +* vb2_request_get_buf() - return the buffer at index @idx +* +* @req: the request. +* @idx: index of the buffer in the req object list +* +* Return a vb2 buffer or NULL if there's no buffer at the specified index +*/ +struct vb2_buffer *vb2_request_get_buf (struct media_request *req, +unsigned int idx); +#endif /* _MEDIA_VIDEOBUF2_CORE_H */
Signature · 1 line
2.21.0
RFC PATCH 2/5 media: v4l2: Prepare things for addition of m2m codec helpers
In reply to Boris BrezillonRight now, all m2m helper are in v4l2-mem2mem.c, but we are about to had new helpers for codec devices and we want to make them optional while still having all m2m helpers embedded in a single module (v4l2-mem2mem.ko).
Move the existing code base in v4l2-mem2mem-core.c so we can easily add new .o objects to the v4l2-mem2mem.o object.
---
drivers/media/v4l2-core/Makefile | 1 +
drivers/media/v4l2-core/{v4l2-mem2mem.c => v4l2-mem2mem-core.c} | 0
2 files changed, 1 insertion(+)
rename drivers/media/v4l2-core/{v4l2-mem2mem.c => v4l2-mem2mem-core.c} (100%)drivers/media/v4l2-core/Makefile +1 -0
@@ -19,6 +19,7 @@ obj-$(CONFIG_VIDEO_V4L2) += v4l2-dv-timings.o obj-$(CONFIG_VIDEO_TUNER) += tuner.o +v4l2-mem2mem-y = v4l2-mem2mem-core.o obj-$(CONFIG_V4L2_MEM2MEM_DEV) += v4l2-mem2mem.o obj-$(CONFIG_V4L2_FLASH_LED_CLASS) += v4l2-flash-led-class.o
drivers/media/v4l2-core/v4l2-mem2mem-core.c renamed from drivers/media/v4l2-core/v4l2-mem2mem.c +0 -0
Signature · 1 line
2.21.0
RFC PATCH 4/5 media: v4l2: Provide helpers for H264 codecs
In reply to Boris Brezillon--- drivers/media/v4l2-core/Kconfig | 5 + drivers/media/v4l2-core/Makefile | 1 + .../media/v4l2-core/v4l2-mem2mem-h264-codec.c | 47 ++++++++ include/media/v4l2-mem2mem-h264-codec.h | 100 ++++++++++++++++++ 4 files changed, 153 insertions(+) create mode 100644 drivers/media/v4l2-core/v4l2-mem2mem-h264-codec.c create mode 100644 include/media/v4l2-mem2mem-h264-codec.h
drivers/media/v4l2-core/Kconfig +5 -0
@@ -49,6 +49,11 @@ config V4L2_MEM2MEM_CODEC bool depends on V4L2_MEM2MEM_DEV +config V4L2_MEM2MEM_H264_CODEC + bool + depends on V4L2_MEM2MEM_DEV + select V4L2_MEM2MEM_CODEC + # Used by LED subsystem flash drivers config V4L2_FLASH_LED_CLASS tristate "V4L2 flash API for LED flash class devices"
drivers/media/v4l2-core/Makefile +1 -0
@@ -21,6 +21,7 @@ obj-$(CONFIG_VIDEO_TUNER) += tuner.o v4l2-mem2mem-y = v4l2-mem2mem-core.o v4l2-mem2mem-$(CONFIG_V4L2_MEM2MEM_CODEC) += v4l2-mem2mem-codec.o +v4l2-mem2mem-$(CONFIG_V4L2_MEM2MEM_H264_CODEC) += v4l2-mem2mem-h264-codec.o obj-$(CONFIG_V4L2_MEM2MEM_DEV) += v4l2-mem2mem.o obj-$(CONFIG_V4L2_FLASH_LED_CLASS) += v4l2-flash-led-class.o
drivers/media/v4l2-core/v4l2-mem2mem-h264-codec.c new file +47 -0
47 lines not on this page · raw message
include/media/v4l2-mem2mem-h264-codec.h new file +100 -0
100 lines not on this page · raw message
Signature · 1 line
2.21.0
RFC PATCH 3/5 media: v4l2: Add m2m codec helpers
In reply to Boris BrezillonMost codec drivers do the same thing, let's extract those generic bits and provide them as m2m_codec helpers.
Each compressed-format will have its own set of sub-helpers.
--- drivers/media/v4l2-core/Kconfig | 4 + drivers/media/v4l2-core/Makefile | 1 + drivers/media/v4l2-core/v4l2-mem2mem-codec.c | 1170 ++++++++++++++++++ include/media/v4l2-mem2mem-codec.h | 317 +++++ 4 files changed, 1492 insertions(+) create mode 100644 drivers/media/v4l2-core/v4l2-mem2mem-codec.c create mode 100644 include/media/v4l2-mem2mem-codec.h
drivers/media/v4l2-core/Kconfig +4 -0
@@ -45,6 +45,10 @@ config V4L2_MEM2MEM_DEV tristate depends on VIDEOBUF2_CORE +config V4L2_MEM2MEM_CODEC + bool + depends on V4L2_MEM2MEM_DEV + # Used by LED subsystem flash drivers config V4L2_FLASH_LED_CLASS tristate "V4L2 flash API for LED flash class devices"
drivers/media/v4l2-core/Makefile +1 -0
@@ -20,6 +20,7 @@ obj-$(CONFIG_VIDEO_V4L2) += v4l2-dv-timings.o obj-$(CONFIG_VIDEO_TUNER) += tuner.o v4l2-mem2mem-y = v4l2-mem2mem-core.o +v4l2-mem2mem-$(CONFIG_V4L2_MEM2MEM_CODEC) += v4l2-mem2mem-codec.o obj-$(CONFIG_V4L2_MEM2MEM_DEV) += v4l2-mem2mem.o obj-$(CONFIG_V4L2_FLASH_LED_CLASS) += v4l2-flash-led-class.o
drivers/media/v4l2-core/v4l2-mem2mem-codec.c new file +1170 -0
1,170 lines not on this page · raw message
include/media/v4l2-mem2mem-codec.h new file +317 -0
317 lines not on this page · raw message
Signature · 1 line
2.21.0
RFC PATCH 5/5 media: rockchip: Add the rkvdec driver
In reply to Boris BrezillonThe rockchip vdec block is a stateless decoder that's able to decode H264, HEVC and VP9 content. This patch only adds H264 support and is making use of the m2m codec helpers.
--- drivers/staging/media/Kconfig | 2 + drivers/staging/media/Makefile | 1 + drivers/staging/media/rockchip/Kconfig | 16 + drivers/staging/media/rockchip/Makefile | 2 + drivers/staging/media/rockchip/vdec/Kconfig | 16 + drivers/staging/media/rockchip/vdec/Makefile | 3 + .../staging/media/rockchip/vdec/rkvdec-h264.c | 909 ++++++++++++++++++ .../staging/media/rockchip/vdec/rkvdec-regs.h | 305 ++++++ drivers/staging/media/rockchip/vdec/rkvdec.c | 564 +++++++++++ drivers/staging/media/rockchip/vdec/rkvdec.h | 94 ++ 10 files changed, 1912 insertions(+) create mode 100644 drivers/staging/media/rockchip/Kconfig create mode 100644 drivers/staging/media/rockchip/Makefile create mode 100644 drivers/staging/media/rockchip/vdec/Kconfig create mode 100644 drivers/staging/media/rockchip/vdec/Makefile create mode 100644 drivers/staging/media/rockchip/vdec/rkvdec-h264.c create mode 100644 drivers/staging/media/rockchip/vdec/rkvdec-regs.h create mode 100644 drivers/staging/media/rockchip/vdec/rkvdec.c create mode 100644 drivers/staging/media/rockchip/vdec/rkvdec.h
drivers/staging/media/Kconfig +2 -0
@@ -30,6 +30,8 @@ source "drivers/staging/media/meson/vdec/Kconfig" source "drivers/staging/media/omap4iss/Kconfig" +source "drivers/staging/media/rockchip/Kconfig" + source "drivers/staging/media/sunxi/Kconfig" source "drivers/staging/media/tegra-vde/Kconfig"
drivers/staging/media/Makefile +1 -0
@@ -3,6 +3,7 @@ obj-$(CONFIG_VIDEO_ALLEGRO_DVT) += allegro-dvt/ obj-$(CONFIG_VIDEO_IMX_MEDIA) += imx/ obj-$(CONFIG_VIDEO_MESON_VDEC) += meson/vdec/ obj-$(CONFIG_VIDEO_OMAP4) += omap4iss/ +obj-$(CONFIG_VIDEO_ROCKCHIP) += rockchip/ obj-$(CONFIG_VIDEO_SUNXI) += sunxi/ obj-$(CONFIG_TEGRA_VDE) += tegra-vde/ obj-$(CONFIG_VIDEO_HANTRO) += hantro/
drivers/staging/media/rockchip/Kconfig new file +16 -0
@@ -0,0 +1,16 @@ +# SPDX-License-Identifier: GPL-2.0 +config VIDEO_ROCKCHIP + bool "Rockchip Video Devices" + depends on ARCH_ROCKCHIP || COMPILE_TEST + help + If you have a Rockchip SoC based embedding a video codec. + + Note that this option doesn't include new drivers in the + kernel: saying N will just cause Kconfig to skip all the + questions about Allwinner media devices. + +if VIDEO_ROCKCHIP + +source "drivers/staging/media/rockchip/vdec/Kconfig" + +endif
drivers/staging/media/rockchip/Makefile new file +2 -0
@@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_VIDEO_ROCKCHIP_VDEC) += vdec/
drivers/staging/media/rockchip/vdec/Kconfig new file +16 -0
@@ -0,0 +1,16 @@ +# SPDX-License-Identifier: GPL-2.0 +config VIDEO_ROCKCHIP_VDEC + tristate "Rockchip Video Decoder driver" + depends on ARCH_ROCKCHIP || COMPILE_TEST + depends on VIDEO_DEV && VIDEO_V4L2 && MEDIA_CONTROLLER + depends on MEDIA_CONTROLLER_REQUEST_API + select VIDEOBUF2_DMA_CONTIG + select VIDEOBUF2_VMALLOC + select V4L2_MEM2MEM_DEV + select V4L2_MEM2MEM_CODEC + select V4L2_MEM2MEM_H264_CODEC + help + Support for the Rockchip Video Decoder IP present on Rockchip SoC, + which accelerates video decoding. + To compile this driver as a module, choose M here: the module + will be called hantro-vpu.
drivers/staging/media/rockchip/vdec/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +obj-$(CONFIG_VIDEO_ROCKCHIP_VDEC) += rockchip-vdec.o + +rockchip-vdec-y += rkvdec.o rkvdec-h264.o
drivers/staging/media/rockchip/vdec/rkvdec-h264.c new file +909 -0
909 lines not on this page · raw message
drivers/staging/media/rockchip/vdec/rkvdec-regs.h new file +305 -0
305 lines not on this page · raw message
drivers/staging/media/rockchip/vdec/rkvdec.c new file +564 -0
564 lines not on this page · raw message
drivers/staging/media/rockchip/vdec/rkvdec.h new file +94 -0
94 lines not on this page · raw message
Signature · 1 line
2.21.0
Re: [RFC PATCH 1/5] media: vb2: Add a helper to get the vb2 buffer attached to a request
In reply to Boris BrezillonQuoted 48 lines · Boris Brezillon
vb2_request_get_buf()returns the N-th buffer attached to a media request.Signed-off-by Boris Brezillon <[email protected]>--- .../media/common/videobuf2/videobuf2-core.c | 23 +++++++++++++++++++ include/media/videobuf2-core.h | 11 +++++++++ 2 files changed, 34 insertions(+)drivers/media/common/videobuf2/videobuf2-core.c +23
-0@@ -1416,6 +1416,29 @@ unsigned int vb2_request_buffer_cnt(struct media_request *req) }EXPORT_SYMBOL_GPL (vb2_request_buffer_cnt); +struct vb2_buffer *vb2_request_get_buf (struct media_request *req, +unsigned int idx) +{ +struct media_request_object *obj; +struct vb2_buffer *buf =NULL ; +unsigned int nbufs =0 ; +unsigned long flags; + +spin_lock_irqsave (&req->lock, flags); +list_for_each_entry (obj, &req->objects, list) { +if (!vb2_request_object_is_buffer (obj) || + nbufs++ < idx) +continue ; + + buf = container_of(obj, struct vb2_buffer, req_obj); +break ; + } +spin_unlock_irqrestore (&req->lock, flags); + +return buf; +} +EXPORT_SYMBOL_GPL (vb2_request_get_buf); +int vb2_core_prepare_buf (struct vb2_queue *q,unsigned int index,void *pb) {struct vb2_buffer *vb;include/media/videobuf2-core.h +0
-0
include/media/videobuf2-core.h bool vb2_request_object_is_buffer(struct media_request_object *obj); · line 1229
Quoted 8 lines
include/media/videobuf2-core.h +5
-0@@ -1222,4 +1222,15 @@ bool vb2_request_object_is_buffer(struct media_request_object *obj);*/ unsigned int vb2_request_buffer_cnt (struct media_request *req); +/** +* vb2_request_get_buf() - return the buffer at index @idx +* +* @req: the request. +* @idx: index of the buffer in the req object list
This is confusing: it suggests that you are talking about the buffer index itself (buf->index) instead of the nth buffer in the request.
Looks good otherwise.
Regards,
Hans
Quoted 8 lines
include/media/videobuf2-core.h +6
-0+* +* Return a vb2 buffer or NULL if there's no buffer at the specified index +*/ +struct vb2_buffer *vb2_request_get_buf (struct media_request *req, +unsigned int idx); +#endif /* _MEDIA_VIDEOBUF2_CORE_H */
Re: [RFC PATCH 1/5] media: vb2: Add a helper to get the vb2 buffer attached to a request
Quoted 52 lines · On Mon, 5 Aug 2019 10:12:53 -0300 Hans Verkuil <[email protected]> wrote:
Quoted 51 lines · Boris Brezillon
vb2_request_get_buf()returns the N-th buffer attached to a media request.Signed-off-by Boris Brezillon <[email protected]>--- .../media/common/videobuf2/videobuf2-core.c | 23 +++++++++++++++++++ include/media/videobuf2-core.h | 11 +++++++++ 2 files changed, 34 insertions(+)drivers/media/common/videobuf2/videobuf2-core.c +23
-0@@ -1416,6 +1416,29 @@ unsigned int vb2_request_buffer_cnt(struct media_request *req) }EXPORT_SYMBOL_GPL (vb2_request_buffer_cnt); +struct vb2_buffer *vb2_request_get_buf (struct media_request *req, +unsigned int idx) +{ +struct media_request_object *obj; +struct vb2_buffer *buf =NULL ; +unsigned int nbufs =0 ; +unsigned long flags; + +spin_lock_irqsave (&req->lock, flags); +list_for_each_entry (obj, &req->objects, list) { +if (!vb2_request_object_is_buffer (obj) || + nbufs++ < idx) +continue ; + + buf = container_of(obj, struct vb2_buffer, req_obj); +break ; + } +spin_unlock_irqrestore (&req->lock, flags); + +return buf; +} +EXPORT_SYMBOL_GPL (vb2_request_get_buf); +int vb2_core_prepare_buf (struct vb2_queue *q,unsigned int index,void *pb) {struct vb2_buffer *vb;include/media/videobuf2-core.h +0
-0@@ -1222,4 +1222,15 @@ bool vb2_request_object_is_buffer(struct media_request_object *obj);*/ unsigned int vb2_request_buffer_cnt (struct media_request *req);
Quoted 8 lines
include/media/videobuf2-core.h bool vb2_request_object_is_buffer(struct media_request_object *obj); · line 1229
Quoted 5 lines
include/media/videobuf2-core.h +5
-0+/** +* vb2_request_get_buf() - return the buffer at index @idx +* +* @req: the request. +* @idx: index of the buffer in the req object list This is confusing: it suggests that you are talking about the buffer index itself (
buf->index) instead of the nth buffer in the request.
How about:
@n: search for the Nth buffer in the req object list
Quoted 16 lines
Looks good otherwise.
Regards,
Hans
Quoted 8 lines
include/media/videobuf2-core.h +6
-0+* +* Return a vb2 buffer or NULL if there's no buffer at the specified index +*/ +struct vb2_buffer *vb2_request_get_buf (struct media_request *req, +unsigned int idx); +#endif /* _MEDIA_VIDEOBUF2_CORE_H */
Quoted 15 lines · Boris Brezillon
Most codec drivers do the same thing, let's extract those generic bits and provide them as
m2m_codechelpers.Each compressed-format will have its own set of sub-helpers.
Signed-off-by Boris Brezillon <[email protected]>--- drivers/media/v4l2-core/Kconfig | 4 + drivers/media/v4l2-core/Makefile | 1 + drivers/media/v4l2-core/v4l2-mem2mem-codec.c | 1170 ++++++++++++++++++ include/media/v4l2-mem2mem-codec.h | 317 +++++ 4 files changed, 1492 insertions(+)create mode 100644
drivers/media/v4l2-core/v4l2-mem2mem-codec.ccreate mode 100644include/media/v4l2-mem2mem-codec.h
<snip>
Quoted 163 lines
include/media/v4l2-mem2mem-codec.h new file +157
-0@@ -0,0 +1,317 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* +* Memory-to-memory codec framework for Video for Linux 2. +* +* Helper functions for codec devices that use memory buffers for both source +* and destination. +* +* Copyright (c) 2019 Collabora Ltd. +* +* Author: +* Boris Brezillon <[email protected]> +*/ + +#ifndef _MEDIA_V4L2_MEM2MEM_CODEC_H +#define _MEDIA_V4L2_MEM2MEM_CODEC_H + +#include <media/v4l2-ctrls.h> +#include <media/v4l2-dev.h> +#include <media/v4l2-fh.h> +#include <media/v4l2-mem2mem.h> + +struct v4l2_m2m_codec_ctx ; + +/** +* struct v4l2_m2m_codec_ctrl_desc - Codec control description +* @per_request: set to true if the control is expected to be set everytime a +* decoding/encoding request is queued +* @mandatory: set to true if the control is mandatory +* @cfg: control configuration +*/ +struct v4l2_m2m_codec_ctrl_desc { +u32 per_request :1 ; +u32 mandatory :1 ; +struct v4l2_ctrl_config cfg; +}; + +/** +* struct v4l2_m2m_codec_ctrls - Codec controls +* @ctrls: array of control descriptions +* @num_ctrls: size of the ctrls array +* +* Structure used to declare codec specific controls. +*/ +struct v4l2_m2m_codec_ctrls { +const struct v4l2_m2m_codec_ctrl_desc *ctrls; +unsigned int num_ctrls; +}; + +#define V4L2_M2M_CODEC_CTRLS (_name, ...) \ + struct v4l2_m2m_codec_ctrls _name = { \ + .ctrls = (const struct v4l2_m2m_codec_ctrl_desc[]){__VA_ARGS__}, \ + .num_ctrls = sizeof((struct v4l2_m2m_codec_ctrl_desc[]){__VA_ARGS__}) / \ + sizeof(struct v4l2_m2m_codec_ctrl_desc), \ + } + +/** +* struct v4l2_m2m_codec_decoded_fmt_desc - Decoded format description +* @fourcc: the 4CC code of the decoded format +* @priv: driver private data to associate to this decoded format +* +* Structure used to describe decoded formats. +*/ +struct v4l2_m2m_codec_decoded_fmt_desc { +u32 fourcc; +const void *priv; +}; + +/** +* struct v4l2_m2m_codec_coded_fmt_ops - Coded format methods +* @adjust_fmt: adjust a coded format before passing it back to userspace. +* Particularly useful when one wants to tweak any of the +* params set by the core (sizeimage, width, height, ...) +* @start: called when vb2_ops->start_streaming() is called. Any coded-format +* specific context initialization should happen here +* @stop: called when vb2_ops->stop_streaming() is called. Any coded-format +* specific context cleanup should happen here +* @run: called when v4l2_m2m_ops->device_run() is called. This method should +* issue the encoding/decoding request +*/ +struct v4l2_m2m_codec_coded_fmt_ops { +int (*adjust_fmt)(struct v4l2_m2m_codec_ctx *ctx, +struct v4l2_format *f); +int (*start)(struct v4l2_m2m_codec_ctx *ctx); +void (*stop)(struct v4l2_m2m_codec_ctx *ctx); +int (*run)(struct v4l2_m2m_codec_ctx *ctx); +}; + +/** +* struct v4l2_m2m_codec_coded_fmt_desc - Coded format description +* @fourcc: 4CC code describing this coded format +* @requires_request: set to true if the codec requires a media request object +* to process encoding/decoding requests +* @frmsize: frame size constraint. Can be NULL if the codec does not have any +* alignment/min/max size constraints for this coded format +* @ctrls: controls attached to this coded format +* @ops: coded format ops +* @priv: driver private data +*/ +struct v4l2_m2m_codec_coded_fmt_desc { +u32 fourcc; +u32 requires_requests :1 ; +const struct v4l2_frmsize_stepwise *frmsize; +const struct v4l2_m2m_codec_ctrls *ctrls; +const struct v4l2_m2m_codec_coded_fmt_ops *ops; +const void *priv; +}; + +#define V4L2_M2M_CODEC_CODED_FMTS (_fmt_array) \ + .num_coded_fmts = ARRAY_SIZE(_fmt_array), \ + .coded_fmts = _fmt_array + +#define V4L2_M2M_CODEC_DECODED_FMTS (_fmt_array) \ + .num_decoded_fmts = ARRAY_SIZE(_fmt_array), \ + .decoded_fmts = _fmt_array + +/** +* struct v4l2_m2m_codec_caps - Codec capabilities +* @coded_fmts: array of supported coded formats +* @num_coded_fmts: size of the coded_fmts array +* @decoded_fmts: array of supported decoded formats +* @num_decoded_fmts: size of the decoded_fmts array +* +* This structure is describing the formats supported by the codec. +*/ +struct v4l2_m2m_codec_caps { +const struct v4l2_m2m_codec_coded_fmt_desc *coded_fmts; +unsigned int num_coded_fmts; +const struct v4l2_m2m_codec_decoded_fmt_desc *decoded_fmts; +unsigned int num_decoded_fmts; +}; + +/** +* enum v4l2_m2m_codec_type - Codec type +* @V4L2_M2M_ENCODER: encoder +* @V4L2_M2M_DECODER: decoder +*/ +enum v4l2_m2m_codec_type { +V4L2_M2M_ENCODER , +V4L2_M2M_DECODER , +}; + +/** +* struct v4l2_m2m_codec_ops - Codec methods +* @queue_init: called by the v4l2_m2m_codec_queue_init() helper to let the +* driver initialize the src/dst queues +*/ +struct v4l2_m2m_codec_ops { +int (*queue_init)(struct v4l2_m2m_codec_ctx *ctx, +struct vb2_queue *src_vq, +struct vb2_queue *dst_vq); +}; + +/** +* struct v4l2_m2m_codec - Codec object +* @vdev: video device exposed by the codec +* @type: type of codec +* @m2m_dev: m2m device this codec is attached to
include/media/v4l2-mem2mem-codec.h line 167
Quoted 10 lines
include/media/v4l2-mem2mem-codec.h new file +10
-0+* @caps: codec capabilities +* @ops: codec operations +*/ +struct v4l2_m2m_codec { +struct video_device vdev; +enum v4l2_m2m_codec_type type; +struct v4l2_m2m_dev *m2m_dev; +const struct v4l2_m2m_codec_caps *caps; +const struct v4l2_m2m_codec_ops *ops; +};
I have problems with this struct and...
Quoted 28 lines
include/media/v4l2-mem2mem-codec.h new file +28
-0+ +static inline struct v4l2_m2m_codec * +vdev_to_v4l2_m2m_codec (struct video_device *vdev) +{ + return container_of(vdev, struct v4l2_m2m_codec, vdev); +} + +static inline struct video_device * +v4l2_m2m_codec_to_vdev (struct v4l2_m2m_codec *codec) +{ +return &codec->vdev; +} + +static inline enum v4l2_m2m_codec_type +v4l2_m2m_codec_get_type (const struct v4l2_m2m_codec *codec) +{ +return codec->type; +} + +/** +* struct v4l2_m2m_codec_ctx - Codec context +* @fh: file handle +* @coded_fmt: current coded format +* @decoded_fmt: current decoded format +* @coded_fmt_desc: current coded format desc +* @decoded_fmt_desc: current decoded format desc +* @ctrl_hdl: control handler +* @codec: the codec that has created this context
include/media/v4l2-mem2mem-codec.h line 205
Quoted 10 lines
include/media/v4l2-mem2mem-codec.h new file +10
-0+*/ +struct v4l2_m2m_codec_ctx { +struct v4l2_fh fh; +struct v4l2_format coded_fmt; +struct v4l2_format decoded_fmt; +const struct v4l2_m2m_codec_coded_fmt_desc *coded_fmt_desc; +const struct v4l2_m2m_codec_decoded_fmt_desc *decoded_fmt_desc; +struct v4l2_ctrl_handler ctrl_hdl; +struct v4l2_m2m_codec *codec; +};
...this struct.
So basically everything in this header :-)
I haven't done an in-depth review, but my main concern is that I believe these structs and the helpers depending on them are too high-level.
The helpers themselves often look reasonable, except that they could be more generic if it wasn't for these high-level structs.
My feeling is that it would make more sense if you would create structs dealing just with formats and structs just for controls and don't try to mix in things like struct video_device or struct v4l2_fh.
I think that will create a better balance between providing helpers for codec drivers without hiding too much inside v4l2_m2m_codec_* structs.
Regards,
Hans
Quoted 113 lines
include/media/v4l2-mem2mem-codec.h new file +112
-0+ +static inline struct v4l2_m2m_codec_ctx * +fh_to_v4l2_m2m_codec_ctx (struct v4l2_fh *fh) +{ + return container_of(fh, struct v4l2_m2m_codec_ctx, fh); +} + +static inline struct v4l2_m2m_codec_ctx * +file_to_v4l2_m2m_codec_ctx (struct file *file) +{ +return fh_to_v4l2_m2m_codec_ctx (file->private_data); +} + +static inline struct v4l2_m2m_ctx * +v4l2_m2m_codec_get_m2m_ctx (struct v4l2_m2m_codec_ctx *ctx) +{ +return ctx->fh.m2m_ctx; +} + +static inline struct v4l2_ctrl_handler * +v4l2_m2m_codec_get_ctrl_handler (struct v4l2_m2m_codec_ctx *ctx) +{ +return &ctx->ctrl_hdl; +} + +struct v4l2_m2m_codec_run { +struct { +struct vb2_v4l2_buffer *src; +struct vb2_v4l2_buffer *dst; + } bufs; +}; + +int v4l2_m2m_codec_init (struct v4l2_m2m_codec *codec, +enum v4l2_m2m_codec_type type, +struct v4l2_m2m_dev *m2m_dev, +struct v4l2_device *v4l2_dev, +const struct v4l2_m2m_codec_caps *caps, +const struct v4l2_m2m_codec_ops *ops, +const struct v4l2_file_operations *vdev_fops, +const struct v4l2_ioctl_ops *vdev_ioctl_ops, +struct mutex *lock,const char *name,void *drvdata); +int v4l2_m2m_codec_ctx_init (struct v4l2_m2m_codec_ctx *ctx,struct file *file, +struct v4l2_m2m_codec *codec); +void v4l2_m2m_codec_ctx_cleanup (struct v4l2_m2m_codec_ctx *ctx); +void v4l2_m2m_codec_run_preamble (struct v4l2_m2m_codec_ctx *ctx, +struct v4l2_m2m_codec_run *run); +void v4l2_m2m_codec_run_postamble (struct v4l2_m2m_codec_ctx *ctx, +struct v4l2_m2m_codec_run *run); +void v4l2_m2m_codec_job_finish (struct v4l2_m2m_codec_ctx *ctx, +enum vb2_buffer_state state); + +static inline const struct v4l2_format * +v4l2_m2m_codec_get_coded_fmt (struct v4l2_m2m_codec_ctx *ctx) +{ +return &ctx->coded_fmt; +} + +static inline const struct v4l2_m2m_codec_coded_fmt_desc * +v4l2_m2m_codec_get_coded_fmt_desc (struct v4l2_m2m_codec_ctx *ctx) +{ +return ctx->coded_fmt_desc; +} + +static inline const struct v4l2_format * +v4l2_m2m_codec_get_decoded_fmt (struct v4l2_m2m_codec_ctx *ctx) +{ +return &ctx->decoded_fmt; +} + +static inline const struct v4l2_m2m_codec_decoded_fmt_desc * +v4l2_m2m_codec_get_decoded_fmt_desc (struct v4l2_m2m_codec_ctx *ctx) +{ +return ctx->decoded_fmt_desc; +} + +void v4l2_m2m_codec_reset_decoded_fmt (struct v4l2_m2m_codec_ctx *ctx); +const struct v4l2_m2m_codec_coded_fmt_desc * +v4l2_m2m_codec_find_coded_fmt_desc (struct v4l2_m2m_codec *codec,u32 fourcc); +int v4l2_m2m_codec_enum_framesizes (struct file *file,void *priv, +struct v4l2_frmsizeenum *fsize); +int v4l2_m2m_codec_enum_output_fmt (struct file *file,void *priv, +struct v4l2_fmtdesc *f); +int v4l2_m2m_codec_enum_capture_fmt (struct file *file,void *priv, +struct v4l2_fmtdesc *f); +int v4l2_m2m_codec_g_output_fmt (struct file *file,void *priv, +struct v4l2_format *f); +int v4l2_m2m_codec_g_capture_fmt (struct file *file,void *priv, +struct v4l2_format *f); +int v4l2_m2m_codec_try_output_fmt (struct file *file,void *priv, +struct v4l2_format *f); +int v4l2_m2m_codec_try_capture_fmt (struct file *file,void *priv, +struct v4l2_format *f); +int v4l2_m2m_codec_s_output_fmt (struct file *file,void *priv, +struct v4l2_format *f); +int v4l2_m2m_codec_s_capture_fmt (struct file *file,void *priv, +struct v4l2_format *f); + +int v4l2_m2m_codec_queue_setup (struct vb2_queue *vq,unsigned int *num_buffers, +unsigned int *num_planes,unsigned int sizes[], +struct device *alloc_devs[]); +void v4l2_m2m_codec_queue_cleanup (struct vb2_queue *vq,u32 state); +int v4l2_m2m_codec_buf_out_validate (struct vb2_buffer *vb); +int v4l2_m2m_codec_buf_prepare (struct vb2_buffer *vb); +void v4l2_m2m_codec_buf_queue (struct vb2_buffer *vb); +void v4l2_m2m_codec_buf_request_complete (struct vb2_buffer *vb); +int v4l2_m2m_codec_start_streaming (struct vb2_queue *q,unsigned int count); +void v4l2_m2m_codec_stop_streaming (struct vb2_queue *q); + +int v4l2_m2m_codec_request_validate (struct media_request *req); +int v4l2_m2m_codec_device_run (struct v4l2_m2m_codec_ctx *ctx); + +#endif /* _MEDIA_V4L2_MEM2MEM_CODEC_H */
Re: [RFC PATCH 1/5] media: vb2: Add a helper to get the vb2 buffer attached to a request
In reply to Boris BrezillonQuoted 64 lines · Boris Brezillon
Quoted 52 lines · On Mon, 5 Aug 2019 10:12:53 -0300 Hans Verkuil <[email protected]> wrote:
Quoted 51 lines · Boris Brezillon
vb2_request_get_buf()returns the N-th buffer attached to a media request.Signed-off-by Boris Brezillon <[email protected]>--- .../media/common/videobuf2/videobuf2-core.c | 23 +++++++++++++++++++ include/media/videobuf2-core.h | 11 +++++++++ 2 files changed, 34 insertions(+)drivers/media/common/videobuf2/videobuf2-core.c +23
-0@@ -1416,6 +1416,29 @@ unsigned int vb2_request_buffer_cnt(struct media_request *req) }EXPORT_SYMBOL_GPL (vb2_request_buffer_cnt); +struct vb2_buffer *vb2_request_get_buf (struct media_request *req, +unsigned int idx) +{ +struct media_request_object *obj; +struct vb2_buffer *buf =NULL ; +unsigned int nbufs =0 ; +unsigned long flags; + +spin_lock_irqsave (&req->lock, flags); +list_for_each_entry (obj, &req->objects, list) { +if (!vb2_request_object_is_buffer (obj) || + nbufs++ < idx) +continue ; + + buf = container_of(obj, struct vb2_buffer, req_obj); +break ; + } +spin_unlock_irqrestore (&req->lock, flags); + +return buf; +} +EXPORT_SYMBOL_GPL (vb2_request_get_buf); +int vb2_core_prepare_buf (struct vb2_queue *q,unsigned int index,void *pb) {struct vb2_buffer *vb;include/media/videobuf2-core.h +0
-0@@ -1222,4 +1222,15 @@ bool vb2_request_object_is_buffer(struct media_request_object *obj);*/ unsigned int vb2_request_buffer_cnt (struct media_request *req);Quoted 8 lines
include/media/videobuf2-core.h bool vb2_request_object_is_buffer(struct media_request_object *obj); · line 1229
Quoted 5 lines
include/media/videobuf2-core.h +5
-0+/** +* vb2_request_get_buf() - return the buffer at index @idx +* +* @req: the request. +* @idx: index of the buffer in the req object list This is confusing: it suggests that you are talking about the buffer index itself (
buf->index) instead of the nth buffer in the request.
Quoted 3 lines
How about:
@n: search for the Nth buffer in the req object list
That looks good to me.
Hans
Quoted 18 lines
Quoted 17 lines
Looks good otherwise.
Regards,
Hans
Quoted 8 lines
include/media/videobuf2-core.h +6
-0+* +* Return a vb2 buffer or NULL if there's no buffer at the specified index +*/ +struct vb2_buffer *vb2_request_get_buf (struct media_request *req, +unsigned int idx); +#endif /* _MEDIA_VIDEOBUF2_CORE_H */
Quoted 22 lines · On Mon, 5 Aug 2019 13:53:20 -0300 Hans Verkuil <[email protected]> wrote:
Quoted 9 lines
include/media/v4l2-mem2mem-codec.h new file +9
-0+/** +* struct v4l2_m2m_codec_ctx - Codec context +* @fh: file handle +* @coded_fmt: current coded format +* @decoded_fmt: current decoded format +* @coded_fmt_desc: current coded format desc +* @decoded_fmt_desc: current decoded format desc +* @ctrl_hdl: control handler +* @codec: the codec that has created this context include/media/v4l2-mem2mem-codec.h line 205
Quoted 10 lines
include/media/v4l2-mem2mem-codec.h new file +10
-0+*/ +struct v4l2_m2m_codec_ctx { +struct v4l2_fh fh; +struct v4l2_format coded_fmt; +struct v4l2_format decoded_fmt; +const struct v4l2_m2m_codec_coded_fmt_desc *coded_fmt_desc; +const struct v4l2_m2m_codec_decoded_fmt_desc *decoded_fmt_desc; +struct v4l2_ctrl_handler ctrl_hdl; +struct v4l2_m2m_codec *codec; +};...this struct.
Quoted 8 lines
So basically everything in this header :-)
I haven't done an in-depth review, but my main concern is that I believe these structs and the helpers depending on them are too high-level.
The helpers themselves often look reasonable, except that they could be more generic if it wasn't for these high-level structs.
I'll have to double check, but I fear most of those helpers are useless if we don't have these generic structs.
Quoted 4 lines
My feeling is that it would make more sense if you would create structs dealing just with formats and structs just for controls and don't try to mix in things like struct
video_deviceor structv4l2_fh.
Except that's where most of the boiler-plate code is (basically all the ioctl and vb2_queue ops).
Quoted 3 lines
I think that will create a better balance between providing helpers for codec drivers without hiding too much inside v4l2_m2m_codec_* structs.
I'm fine with that, just not sure this will significantly reduce code duplication...