You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
v4l2 codec does not support V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME #3171
Describe the bug
Using bcm2835-codec there is no way to force key frame generation
To reproduce
While using v4l2 encoder, send ioctl with V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME
Expected behaviour
Expect ioctl to succeed and i-frame to be generated.
Actual behaviour
ioctl fails
System
Which model of Raspberry Pi? e.g. Pi3B+, PiZeroW
Pi3B+
Which OS and version (cat /etc/rpi-issue)?
Raspberry Pi reference 2019-07-10
Generated using pi-gen, https://github.com/RPi-Distro/pi-gen, 175dfb027ffabd4b8d5080097af0e51ed9a4a56c, stage2
Which firmware version (vcgencmd version)?
Jul 9 2019 14:40:53
Copyright (c) 2012 Broadcom
version 6c3fe3f096a93de3b34252ad98cdccadeb534be2 (clean) (release) (start)
Which kernel version (uname -a)?
Linux tmm1-pi 4.19.57-v7+ Rpi 4.4.y #1244 SMP Thu Jul 4 18:45:25 BST 2019 armv7l GNU/Linux
Additional context
Similar functionality is available via the OMX encoder using either OMX_IndexConfigBrcmVideoRequestIFrame or OMX_IndexConfigVideoIntraVOPRefresh
bcm2835_codec_s_ctrl() needs to have a V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME case statement added. See for example https://lkml.org/lkml/2016/1/19/24
Have you actually got some software which uses this V4L2 parameter? It's one of those annoying things that for this sort of patch it'll take longer to produce a test app to verify that it works than it does to make the change.
And so ffmpeg only ends up sending the Y-plane from a YUV420P picture.
Should I look into fixing the capabilities advertised, or is the correct fix to teach ffmpeg to pack the planes together before sending them into the driver?
pi@raspberrypi:~ $ v4l2-ctl -D -d 11
Driver Info:
Driver name : bcm2835-codec
Card type : bcm2835-codec
Bus info : platform:bcm2835-codec
Driver version : 4.19.64
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
There is an ffmpeg patch that should be in Raspbian to add support for NV21 and YUV420P - the standard code only had NV12 support. That causes some grief with the chroma planes not being setup
Off my local branch:
commit 4ced57196354736ea2f831fd012be69f7a01dc30
Author: Dave Stevenson <dave.stevenson@raspberrypi.org>
Date: Thu Jul 4 17:50:13 2019 +0100
avcodec: v4l2_m2m: Support NV21 and YUV420P
The single planar support was for NV21 only.
Add NV21 and YUV420P support.
diff --git a/libavcodec/v4l2_buffers.c b/libavcodec/v4l2_buffers.c
index aef911f3bb..1934ff9377 100644
--- a/libavcodec/v4l2_buffers.c
+++ b/libavcodec/v4l2_buffers.c
@@ -321,11 +321,21 @@ int ff_v4l2_buffer_buf_to_avframe(AVFrame *frame, V4L2Buffer *avbuf)
/* 1.1 fixup special cases */
switch (avbuf->context->av_pix_fmt) {
case AV_PIX_FMT_NV12:
+ case AV_PIX_FMT_NV21:
if (avbuf->num_planes > 1)
break;
frame->linesize[1] = avbuf->plane_info[0].bytesperline;
frame->data[1] = frame->buf[0]->data + avbuf->plane_info[0].bytesperline * avbuf->context->format.fmt.pix_mp.height;
break;
+ case AV_PIX_FMT_YUV420P:
+ /* No YV12? support? */
+ if (avbuf->num_planes > 1)
+ break;
+ frame->linesize[1] = avbuf->plane_info[0].bytesperline >> 1;
+ frame->linesize[2] = avbuf->plane_info[0].bytesperline >> 1;
+ frame->data[1] = frame->buf[0]->data + avbuf->plane_info[0].bytesperline * avbuf->context->format.fmt.pix_mp.height;
+ frame->data[2] = frame->data[1] + ((avbuf->plane_info[0].bytesperline * avbuf->context->format.fmt.pix_mp.height) >> 2);
+ break;
default:
break;
}
As a quick test, try reverting the patches I referenced from #3097 to see if it plays nicely with the single planar API. I have a suspicion that it won't, and yes FFmpeg should be munging the buffer pointers/sizes, having first confirmed that the planes are contiguous.
The question over single planar vs multi planar, and the use of single planar formats through the multi planar API, was brought up in discussing #3097 and had been discussed on linux-media https://www.spinics.net/lists/linux-media/msg154053.html
Cedrus for one also uses the single planar API, therefore having support for the single planar formats in FFmpeg would be sensible.
I'll try to sort out the decode YUV420/NV21 patch at some point.
Sorry, but we've found ffmpeg-devel not the most welcoming or helpful of places.
The bugtracker also seems to be a dead end. I opened a ticket 7 months ago for an omx issue, with a proposed patch to fix it, and got no response beyond "isn't this a duplicate" (it wasn't) https://trac.ffmpeg.org/ticket/7687.
I guess it depends on the relevant people seeing messages.
sudo rpi-update will grab the latest kernel with this change to save you rebuilding your own kernel.
Thanks that's very helpful.
I'm not sure where/when you were hitting the issue with not supporting MPLANE.
Sorry I should have been more specific. There were no errors- it worked in single plane mode, but since only the Y-plane was sent to the encoder the generate picture was not color correct. I thought maybe if the driver supported mplane then the picture would get uploaded correctly. I will try that after rpi-update completes.
I'll try to sort out the decode YUV420/NV21 patch at some point.
Sorry, but we've found ffmpeg-devel not the most welcoming or helpful of places.
Your experience is not uncommon.
I opened a ticket 7 months ago for an omx issue, with a proposed patch to fix it, and got no response beyond "isn't this a duplicate" (it wasn't) https://trac.ffmpeg.org/ticket/7687.
It seems the omx driver is pretty much unmaintained. I will be happy to add myself as the maintainer of omx.c and merge any relevant patches to improve RPi support.
After rpi-update, I can confirm v4l2-ctl shows "Multiplanar" support.
The behavior on ffmpeg's side is still the same- the images generated by the encoder are missing chroma. I will work on a patch to convert to V4L's requested pixel format. How does V4L signal that it wants packed planes?
Thanks for looking at my issue in the bug tracker. I can fully relate to things dropping to being unmaintained as people get bored, but I had hoped that it might have had some traction seeing as I had provided a fix for it.
Thanks. I'm new to v4l2 so I appreciate the pointers.
BTW, one thing I'm interested in adding to ffmpeg is accelerated scaling on the RPi.
It looks like using "OMX.broadcom.resize" from avcodec/omx.c would be fairly straightfoward (although ffmpeg-devel would probably prefer if I wrote a separate avfilter/vf_scale_omx.c instead).
But I'm curious what you think about adding the resize component to the v4l2 driver? (I saw /dev/video12 exposes ISP already, but it's unclear to me if I can use the second downscaling output port reasonably from v4l2).
I was able to teach ff_v4l2_buffer_avframe_to_buf about contiguous planar formats. However the each chroma in the encoded picture is offset. I assume there is some sort of padding requirement?
I was able to teach ff_v4l2_buffer_avframe_to_buf about contiguous planar formats. However the each chroma in the encoded picture is offset. I assume there is some sort of padding requirement?
It looks like using "OMX.broadcom.resize" from avcodec/omx.c would be fairly straightfoward (although ffmpeg-devel would probably prefer if I wrote a separate avfilter/vf_scale_omx.c instead).
But I'm curious what you think about adding the resize component to the v4l2 driver? (I saw /dev/video12 exposes ISP already, but it's unclear to me if I can use the second downscaling output port reasonably from v4l2).
/dev/video12 will happily rescale images for you using the ISP hardware block. GStreamer will happily use it via the v4l2convert component.
OMX.broadcom.resize is done on the VPU (generic SIMD processor within the GPU). More power required, and slower. I see little point in exposing that via V4L2.
Also note that the V4L2 M2M devices are set up to support dmabuf import and export, therefore can do zero copy paths around the place. This is something that has limited support in FFmpeg, but is used by GStreamer effectively. A pipeline of v4l2src to v4l2h264enc with zero copy should achieve 1080p30 easily, and potentially 1080p50.
The OMX framework will always end up copying all buffers to and from GPU memory, therefore the performance is lower.
I'm getting random failures starting up the h264 encoder sometimes:
[12205.121366] cma: cma_alloc: alloc failed, req-size: 192 pages, ret: -12
[12205.121375] bcm2835-codec bcm2835-codec: dma_alloc_coherent of size 786432 failed
So V4L2 is using the kernel CMA heap for all allocations instead of gpu_mem (although there is still some requirement for gpu_mem).
The default size of the cma heap on Pi0-3 is generally fairly small, unless you are using vc4-fkms-v3d or vc4-kms-v3d for 3D and rendering, in which case the heap is increased to 256MB.
vc4-fkms-v3d is the default on Pi4 as there is no firmware GL driver, therefore it is generally 256MB.
GL and DRM/KMS are the main reason the V4L2 drivers were written as it moves towards the Linux standard APIs for things like Kodi, and they are looking at removing as much platform specific stuff as possible.
Add cma=128M to /boot/cmdline.txt to define the heap to be 128MB
Padding is controlled via a combination of bytesperline, and the selection API. No padding is allowed between the planes for the single planar format. I'll check what you've done in your patch.
I'm trying to integrate the v4l2 with hardware encoding into googles c++ webrtc implementation. Which works great! However for a clean implementation I'd like to trigger an i-frame. I do it like:
Activity
6by9 commentedon Aug 22, 2019
MMAL_PARAMETER_VIDEO_REQUEST_I_FRAMEis the MMAL parameter.Pull Requests welcome - it won't be a high priority for me to fix.
I'd expect the actual active bit to be something like:
Have you actually got some software which uses this V4L2 parameter? It's one of those annoying things that for this sort of patch it'll take longer to produce a test app to verify that it works than it does to make the change.
tmm1 commentedon Aug 22, 2019
Great, that's all I needed. I will open a PR. I am using ffmpeg's h264_v4l2m2m encoder to test and that's how I discovered this ioctl was failing.
tmm1 commentedon Aug 22, 2019
Another issue I ran into is that the driver doesn't seem to advertise MPLANE support. This check in ffmpeg fails: https://github.com/FFmpeg/FFmpeg/blob/9bcb1cb6ed50e66e0489beb871eed83533b3de97/libavcodec/v4l2_m2m.c#L53
And so ffmpeg only ends up sending the Y-plane from a YUV420P picture.
Should I look into fixing the capabilities advertised, or is the correct fix to teach ffmpeg to pack the planes together before sending them into the driver?
6by9 commentedon Aug 22, 2019
Is that on the latest kernel tree?
The original version of the driver supported the single planar API, but that was replaced by #3097 to use the multiplanar API (although still the single planar formats).
https://github.com/raspberrypi/linux/blob/rpi-4.19.y/drivers/staging/vc04_services/bcm2835-codec/bcm2835-v4l2-codec.c#L2607 seems to say multi planar, as does
There is an ffmpeg patch that should be in Raspbian to add support for NV21 and YUV420P - the standard code only had NV12 support. That causes some grief with the chroma planes not being setup
Off my local branch:
6by9 commentedon Aug 22, 2019
Oh, hang on, you're looking at encode, not decode.
V4L2 dictates how the formats should be packed or the single planar formats.
It does look like
ff_v4l2_buffer_avframe_to_bufassumes that if the avframe is multiple planes then it doesn't check the pixel format nor the correct number of planes, ie the reverse conversion to that done inff_v4l2_buffer_buf_to_avframehttps://github.com/FFmpeg/FFmpeg/blob/9bcb1cb6ed50e66e0489beb871eed83533b3de97/libavcodec/v4l2_buffers.c#L289
As a quick test, try reverting the patches I referenced from #3097 to see if it plays nicely with the single planar API. I have a suspicion that it won't, and yes FFmpeg should be munging the buffer pointers/sizes, having first confirmed that the planes are contiguous.
tmm1 commentedon Aug 22, 2019
Wonderful, thanks for the link. I am on the stable release and v4l2-ctl wasn't showing mplane support.
I will build tip and try again.
I did try this patch, but it only affects AVFrame generation (i.e decoding). I'm working with the encoder which doesn't use this code path.
Would you mind dropping the comment and sending a copy of this to ffmpeg-devel? I will merge it into ffmpeg master.
6by9 commentedon Aug 22, 2019
sudo rpi-updatewill grab the latest kernel with this change to save you rebuilding your own kernel.I'm not sure where/when you were hitting the issue with not supporting MPLANE. On failure
v4l2_prepare_contextsshould have then checked for single plane and been happy. It will still go wrong as there is the missing mapping for single planar formats, but fundamentally it is correct.https://github.com/FFmpeg/FFmpeg/blob/9bcb1cb6ed50e66e0489beb871eed83533b3de97/libavcodec/v4l2_m2m.c#L81
The question over single planar vs multi planar, and the use of single planar formats through the multi planar API, was brought up in discussing #3097 and had been discussed on linux-media
https://www.spinics.net/lists/linux-media/msg154053.html
Cedrus for one also uses the single planar API, therefore having support for the single planar formats in FFmpeg would be sensible.
I'll try to sort out the decode YUV420/NV21 patch at some point.
Sorry, but we've found ffmpeg-devel not the most welcoming or helpful of places.
The bugtracker also seems to be a dead end. I opened a ticket 7 months ago for an omx issue, with a proposed patch to fix it, and got no response beyond "isn't this a duplicate" (it wasn't) https://trac.ffmpeg.org/ticket/7687.
I guess it depends on the relevant people seeing messages.
tmm1 commentedon Aug 22, 2019
Thanks that's very helpful.
Sorry I should have been more specific. There were no errors- it worked in single plane mode, but since only the Y-plane was sent to the encoder the generate picture was not color correct. I thought maybe if the driver supported mplane then the picture would get uploaded correctly. I will try that after
rpi-updatecompletes.Your experience is not uncommon.
It seems the omx driver is pretty much unmaintained. I will be happy to add myself as the maintainer of omx.c and merge any relevant patches to improve RPi support.
tmm1 commentedon Aug 22, 2019
After
rpi-update, I can confirmv4l2-ctlshows "Multiplanar" support.The behavior on ffmpeg's side is still the same- the images generated by the encoder are missing chroma. I will work on a patch to convert to V4L's requested pixel format. How does V4L signal that it wants packed planes?
6by9 commentedon Aug 22, 2019
https://www.linuxtv.org/downloads/v4l-dvb-apis-new/uapi/v4l/yuv-formats.html
V4L2_PIX_FMT_YUV420 vs V4L2_PIX_FMT_YUV420M
V4L2_PIX_FMT_NV12 vs V4L2_PIX_FMT_NV12M
etc
The device will enumerate the formats that it supports via ioctl(VIDIOC_ENUM_FMT), and allow selection via VIDIOC_S_FMT. If the format passed into S_FMT isn't supported then the driver will select some form of default (it does NOT fail the S_FMT call).
https://www.linuxtv.org/downloads/v4l-dvb-apis-new/uapi/v4l/vidioc-enum-fmt.html#vidioc-enum-fmt
Thanks for looking at my issue in the bug tracker. I can fully relate to things dropping to being unmaintained as people get bored, but I had hoped that it might have had some traction seeing as I had provided a fix for it.
tmm1 commentedon Aug 22, 2019
Thanks. I'm new to v4l2 so I appreciate the pointers.
BTW, one thing I'm interested in adding to ffmpeg is accelerated scaling on the RPi.
It looks like using "OMX.broadcom.resize" from
avcodec/omx.cwould be fairly straightfoward (although ffmpeg-devel would probably prefer if I wrote a separateavfilter/vf_scale_omx.cinstead).But I'm curious what you think about adding the resize component to the v4l2 driver? (I saw
/dev/video12exposes ISP already, but it's unclear to me if I can use the second downscaling output port reasonably from v4l2).tmm1 commentedon Aug 22, 2019
I was able to teach ff_v4l2_buffer_avframe_to_buf about contiguous planar formats. However the each chroma in the encoded picture is offset. I assume there is some sort of padding requirement?
tmm1 commentedon Aug 22, 2019
ffmpeg's AVFrame had some padding, but it didn't match the padding expected by v4l2. I got this working and have submitted a patchset to ffmpeg-devel: http://ffmpeg.org/pipermail/ffmpeg-devel/2019-August/248775.html
The change to
ff_v4l2_buffer_avframe_to_bufspecifically is in tmm1/FFmpeg@55380acI also included your NV21/YUV420P decoding patch in my set.
tmm1 commentedon Aug 22, 2019
I'm getting random failures starting up the h264 encoder sometimes:
dmesg shows:
6by9 commentedon Aug 23, 2019
/dev/video12 will happily rescale images for you using the ISP hardware block. GStreamer will happily use it via the v4l2convert component.
OMX.broadcom.resize is done on the VPU (generic SIMD processor within the GPU). More power required, and slower. I see little point in exposing that via V4L2.
Also note that the V4L2 M2M devices are set up to support dmabuf import and export, therefore can do zero copy paths around the place. This is something that has limited support in FFmpeg, but is used by GStreamer effectively. A pipeline of v4l2src to v4l2h264enc with zero copy should achieve 1080p30 easily, and potentially 1080p50.
The OMX framework will always end up copying all buffers to and from GPU memory, therefore the performance is lower.
So V4L2 is using the kernel CMA heap for all allocations instead of gpu_mem (although there is still some requirement for gpu_mem).
The default size of the cma heap on Pi0-3 is generally fairly small, unless you are using vc4-fkms-v3d or vc4-kms-v3d for 3D and rendering, in which case the heap is increased to 256MB.
vc4-fkms-v3d is the default on Pi4 as there is no firmware GL driver, therefore it is generally 256MB.
GL and DRM/KMS are the main reason the V4L2 drivers were written as it moves towards the Linux standard APIs for things like Kodi, and they are looking at removing as much platform specific stuff as possible.
Add
cma=128Mto /boot/cmdline.txt to define the heap to be 128MBPadding is controlled via a combination of bytesperline, and the selection API. No padding is allowed between the planes for the single planar format. I'll check what you've done in your patch.
118 remaining items
staging: bcm2835-codec: add support for V4L2_CID_MPEG_VIDEO_FORCE_KEY…
staging: bcm2835-codec: add support for V4L2_CID_MPEG_VIDEO_FORCE_KEY…
staging: bcm2835-codec: add support for V4L2_CID_MPEG_VIDEO_FORCE_KEY…
staging: bcm2835-codec: add support for V4L2_CID_MPEG_VIDEO_FORCE_KEY…
staging: bcm2835-codec: add support for V4L2_CID_MPEG_VIDEO_FORCE_KEY…
staging: bcm2835-codec: add support for V4L2_CID_MPEG_VIDEO_FORCE_KEY…
staging: bcm2835-codec: add support for V4L2_CID_MPEG_VIDEO_FORCE_KEY…
staging: bcm2835-codec: add support for V4L2_CID_MPEG_VIDEO_FORCE_KEY…
staging: bcm2835-codec: add support for V4L2_CID_MPEG_VIDEO_FORCE_KEY…
staging: bcm2835-codec: add support for V4L2_CID_MPEG_VIDEO_FORCE_KEY…
qwertzui11 commentedon Mar 29, 2021
Hi
I'm trying to integrate the v4l2 with hardware encoding into googles c++ webrtc implementation. Which works great! However for a clean implementation I'd like to trigger an i-frame. I do it like:
the ioctl call results in the error:
Invalid argument.Any idea what I'm doing wrong?
hw: rpi4
camera: v2.1
6by9 commentedon Mar 29, 2021
You're better off creating a new issue rather than commenting on a closed one, or use the forums.
Please confirm you are talking about using the V4L2 M2M encoder (generally /dev/video11) and not the camera driver (normally /dev/video0).
qwertzui11 commentedon Mar 29, 2021
Thanks for the quick response, and the hint! I'm talking about the camera driver
/dev/video0.6by9 commentedon Mar 29, 2021
Not this issue then. This was adding V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME to the M2M encoder (/dev/video11).
v4l2-ctl --list-ctrls would have told you that force-key-frame was not supported on /dev/video0.
Adding it isn't too involved, but won't be immediate.
qwertzui11 commentedon Mar 29, 2021
Ah, oh! Thx for the feedback! and quick response!