[FFmpeg-user] How to set V4L2 M2M codec options like bitrate

Adam Nielsen a.nielsen at shikadi.net
Sun Aug 15 11:32:40 EEST 2021


Hi all,

I'm using the h264_v4l2m2m codec to do hardware accelerated encoding on
a Raspberry Pi.  This is working well, except I am unable to set any
encoder options such as bitrate, as I cannot find how to tell ffmpeg to
set V4L2 control options.

When I run ffmpeg, it says:

  [h264_v4l2m2m @ 0x5d1180] Using device /dev/video11

And if I look at that device, there are a number of encoding options
available:

  # v4l2-ctl -d /dev/video11 -l
        video_bitrate 0x009909cf (int)    : min=25000 max=25000000 step=25000 default=10000000 value=10000000
  h264_i_frame_period 0x00990a66 (int)    : min=0 max=2147483647 step=1 default=60 value=60
  [...]

The problem is that I cannot use v4l2-ctl to set these values, as the
values only remain set until the file handle is closed, as per the
docs[1]:

  Memory-to-memory devices function as a shared resource: you can open
  the video node multiple times, each application setting up their own
  properties that are local to the file handle, and each can use it
  independently from the others. The driver will arbitrate access to
  the hardware and reprogram it whenever another file handler gets
  access. This is different from the usual video node behavior where
  the video properties are global to the device (i.e. changing
  something through one file handle is visible through another file
  handle).

So v4l2-ctl can set the values but as soon as it closes the file handle
the values are lost, and ffmpeg never sees them as it has a different
file handle (i.e. an independent M2M instance to the one v4l2-ctl is
using).

This means that only ffmpeg is able to set the V4L2 controls as it is
the only one that has the open file handle.

I can't see anything in the docs about how to pass V4L2 control values
to ffmpeg, so is this something I have missed, or is it a feature
missing from ffmpeg, for now limiting the usefulness of M2M hardware
acceleration?

Many thanks,
Adam.

[1] https://www.kernel.org/doc/html/latest/userspace-api/media/v4l/dev-mem2mem.html


More information about the ffmpeg-user mailing list