mbox series

[FFmpeg-devel,v2,0/8] Add V4L2 Request API hwaccels for MPEG2, H.264 and HEVC

Message ID 20240806090607.43240-1-jonas@kwiboo.se
Headers show
Series Add V4L2 Request API hwaccels for MPEG2, H.264 and HEVC | expand

Message

Jonas Karlman Aug. 6, 2024, 9:05 a.m. UTC
This is a follow up to a very old series from April 2019 [1] and
December 2020 [2], adding V4L2 Request API hwaccels for stateless
decoding of MPEG2, H.264 and HEVC.

These hwaccels has in one form or another been used in LibreELEC
community, nightly and release images since Dec 20th 2018.

HISTORY

The initial v4l2-request hwaccel code was mainly created as a proof of
concept at the end of 2018 by me and Jernej Skrabec. Back when Bootlin's
crowdfunding campaign for upstream Linux kernel driver for Allwinner VPU
had started to bear fruit.

At the time we had very little knowledge on how to properly interact
with any of the V4L2 APIs, and the existing V4L2 M2M code in FFmpeg
seemed very complex to start working with, so we started from scratch.

(That is the main reason why these hwaccels still does not use any of
the existing V4L2 M2M code in FFmpeg.)

The hwaccels had one major limitation, it waited on the kernel to
complete decoding before continuing with next request. Due to hwaccels
still was able to decode up to 4k 50-60fps on Allwinner and Rockchip
boards, time was never spent trying to improve this limitation.

The initial version of these hwaccels was merged as patches into
LibreELEC in April 2019 [3], with my FFmpeg tree as main source for any
update of FFmpeg v4l2-request patches included in LibreELEC.

A RFC was sent in April 2019 [1], with very little feedback and the
required kernel headers not being merged until a year later there was
never any new RFC revision submitted.

After the release of the Raspberry Pi 4 in June 2019 there was hope that
RPi folks would rework and/or improve our proof-of-concept to something
better that could be upstreamed, as history has show this did not happen.
And instead we have just ended up with a second FFmpeg v4l2-request
implementation that has only really been used for HEVC decoding on RPi.

Due to personal reasons I took a long break from doing open-source
contributions during ~2020-2023. During this time Jernej's FFmpeg tree
became the main source for FFmpeg v4l2-request patches in LibreELEC [4].

In December 2020 a v1 of this series was sent [2], at that time kernel
headers for H.264 had been merged into Linux kernel, and I cannot
remember why it never went any further.

Last update to the v4l2-request patches in LibreELEC was done by me in
November 2023 [5].

Along the way there has been noteworthy contributions including from
Boris Brezillon, Ezequiel Garcia, Alex Bee and Benjamin Gaignard.

PRESENT DAY

The version submitted in this series have seen major refactoring to the
common code, to make it more ready for upstreaming and also a rework of
how buffers and requests are handled.

The limitation of waiting for decoding to complete has been removed and
now multiple pending requests can be in-flight at the same time. This
was a requirement to better support HEVC decoding on RPi.

The older versions also incorrectly required use of a DRM hwdevice,
however V4L2 decoding has nothing to do with the DRM subsystem in Linux.

Instead in this version a new V4L2 Request API hwdevice has been added.
Thanks to this it is now possible to specify what media device to use
for decoding, in case multiple decoders exists on a system. E.g. using a
-init_hw_device v4l2request:/dev/media1 parameter.

This version only add support for MPEG2, H264 and HEVC. Support for VP8,
VP9 and AV1 is planned and be added in next revision or in a follow up
series.

HOW TO USE

To use the V4L2 Request API hwaccels you must build FFmpeg on a system
with recent Linux kernel headers, v6.0+. It also requires libdrm and
libudev for the hwaccels to be enabled and successfully build.

This can then be runtime tested on multiple Allwinner and Rockchip
devices. To runtime test this on a RPi 4 or 5 you should use latest
rpi-6.6.y kernel.

  ffmpeg -hwaccel v4l2request -hwaccel_output_format drm_prime \
         -i <input-path> -map 0:v -f null -

This series has been tested with cedrus driver on Allwinner H6, hantro
and rkvdec driver on Rockchip RK3399, and rpivid driver on RPi 4/5.

PRs have also been opened for Kodi and mpv to assist with the transition
from using a DRM hwdevice type to a new V4L2REQUEST hwdevice type.

- Kodi: https://github.com/xbmc/xbmc/pull/25467
- mpv: https://github.com/mpv-player/mpv/pull/14511

With those PRs applied it should be possible to playback video using
kodi-gbm or mpv, see the PRs above for more details.

It should also be possible to run fluster test suites with following PR:

- fluster: https://github.com/fluendo/fluster/pull/179

FUTURE

I am expecting that there will be a new revision of this series adding
VP8, VP9 and AV1 support in a week or two.

Until then, please get back with any type of feedback.

A copy of this series can also be found at [6].

[1] https://lists.ffmpeg.org/pipermail/ffmpeg-devel/2019-April/242316.html
[2] https://lists.ffmpeg.org/pipermail/ffmpeg-devel/2020-December/273579.html
[3] https://github.com/LibreELEC/LibreELEC.tv/pull/3405
[4] https://github.com/LibreELEC/LibreELEC.tv/commits/master/packages/multimedia/ffmpeg/patches/v4l2-request
[5] https://github.com/LibreELEC/LibreELEC.tv/pull/8356
[6] https://github.com/Kwiboo/FFmpeg/commits/v4l2request-2024-v2/

Boris Brezillon (1):
  avcodec/h264dec: add ref_pic_marking and pic_order_cnt bit_size to
    slice context

Jernej Skrabec (2):
  avcodec: Add V4L2 Request API h264 hwaccel
  avcodec: Add V4L2 Request API hevc hwaccel

Jonas Karlman (5):
  avutil/hwcontext: Add hwdevice type for V4L2 Request API
  avcodec: Add common V4L2 Request API code
  avcodec/v4l2request: Probe for a capable media and video device
  avcodec/v4l2request: Add common decode support for hwaccels
  avcodec: Add V4L2 Request API mpeg2 hwaccel

 MAINTAINERS                        |   1 +
 configure                          |  21 +
 libavcodec/Makefile                |   4 +
 libavcodec/h264_slice.c            |  13 +-
 libavcodec/h264dec.c               |   3 +
 libavcodec/h264dec.h               |   2 +
 libavcodec/hevc/hevcdec.c          |  10 +
 libavcodec/hwaccels.h              |   3 +
 libavcodec/hwconfig.h              |   2 +
 libavcodec/mpeg12dec.c             |   6 +
 libavcodec/v4l2_request.c          | 452 +++++++++++++++++
 libavcodec/v4l2_request.h          | 107 ++++
 libavcodec/v4l2_request_decode.c   | 459 +++++++++++++++++
 libavcodec/v4l2_request_h264.c     | 523 +++++++++++++++++++
 libavcodec/v4l2_request_hevc.c     | 790 +++++++++++++++++++++++++++++
 libavcodec/v4l2_request_internal.h |  51 ++
 libavcodec/v4l2_request_mpeg2.c    | 176 +++++++
 libavcodec/v4l2_request_probe.c    | 614 ++++++++++++++++++++++
 libavutil/Makefile                 |   3 +
 libavutil/hwcontext.c              |   4 +
 libavutil/hwcontext.h              |   1 +
 libavutil/hwcontext_internal.h     |   1 +
 libavutil/hwcontext_v4l2request.c  | 261 ++++++++++
 libavutil/hwcontext_v4l2request.h  |  41 ++
 24 files changed, 3547 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/v4l2_request.c
 create mode 100644 libavcodec/v4l2_request.h
 create mode 100644 libavcodec/v4l2_request_decode.c
 create mode 100644 libavcodec/v4l2_request_h264.c
 create mode 100644 libavcodec/v4l2_request_hevc.c
 create mode 100644 libavcodec/v4l2_request_internal.h
 create mode 100644 libavcodec/v4l2_request_mpeg2.c
 create mode 100644 libavcodec/v4l2_request_probe.c
 create mode 100644 libavutil/hwcontext_v4l2request.c
 create mode 100644 libavutil/hwcontext_v4l2request.h