[packages/ffmpeg] update v4l2 request hwdec patch
atler
atler at pld-linux.org
Sun Apr 7 02:30:37 CEST 2024
commit 0068d4abd0aba2ae817c145777d8897c72ac7703
Author: Jan Palus <atler at pld-linux.org>
Date: Sun Apr 7 00:34:35 2024 +0200
update v4l2 request hwdec patch
sync with https://github.com/Kwiboo/FFmpeg/commits/v4l2-request-n6.1-dev/
v4l2-request-hwdec.patch | 3108 ++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 2887 insertions(+), 221 deletions(-)
---
diff --git a/v4l2-request-hwdec.patch b/v4l2-request-hwdec.patch
index f34e16a..b9eb3ef 100644
--- a/v4l2-request-hwdec.patch
+++ b/v4l2-request-hwdec.patch
@@ -1,7 +1,7 @@
-From 5e154b0357829007566930846f880b9f5fb404d9 Mon Sep 17 00:00:00 2001
+From 8b34700cbd43b18950dc81cceb958d0b39126806 Mon Sep 17 00:00:00 2001
From: Jonas Karlman <jonas at kwiboo.se>
Date: Mon, 3 Dec 2018 23:48:04 +0100
-Subject: [PATCH] avutil: add av_buffer_pool_flush()
+Subject: [PATCH 01/18] avutil: add av_buffer_pool_flush()
Used by V4L2 request API hwaccel
@@ -12,10 +12,10 @@ Signed-off-by: Jonas Karlman <jonas at kwiboo.se>
2 files changed, 18 insertions(+)
diff --git a/libavutil/buffer.c b/libavutil/buffer.c
-index 38a554208a90..b0fedabc3e7d 100644
+index e4562a7..09da632 100644
--- a/libavutil/buffer.c
+++ b/libavutil/buffer.c
-@@ -273,6 +273,19 @@ static void buffer_pool_free(AVBufferPool *pool)
+@@ -319,6 +319,19 @@ static void buffer_pool_free(AVBufferPool *pool)
av_freep(&pool);
}
@@ -36,10 +36,10 @@ index 38a554208a90..b0fedabc3e7d 100644
{
AVBufferPool *pool;
diff --git a/libavutil/buffer.h b/libavutil/buffer.h
-index c0f3f6cc9abe..998beec9ac5b 100644
+index e1ef5b7..fde9bae 100644
--- a/libavutil/buffer.h
+++ b/libavutil/buffer.h
-@@ -267,6 +267,11 @@ AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
+@@ -284,6 +284,11 @@ AVBufferPool *av_buffer_pool_init2(size_t size, void *opaque,
AVBufferRef* (*alloc)(void *opaque, size_t size),
void (*pool_free)(void *opaque));
@@ -51,35 +51,40 @@ index c0f3f6cc9abe..998beec9ac5b 100644
/**
* Mark the pool as being available for freeing. It will actually be freed only
* once all the allocated buffers associated with the pool are released. Thus it
-From 01319970d90ffd3c073b054a3d2e4784f6ebea0d Mon Sep 17 00:00:00 2001
+--
+2.44.0
+
+
+From da33bb8f94c073c8fb6dc7a3f3efe72d78b64a14 Mon Sep 17 00:00:00 2001
From: Jonas Karlman <jonas at kwiboo.se>
Date: Sat, 15 Dec 2018 22:32:16 +0100
-Subject: [PATCH] Add common V4L2 request API code
+Subject: [PATCH 02/18] Add common V4L2 request API code
Signed-off-by: Jonas Karlman <jonas at kwiboo.se>
+Signed-off-by: Alex Bee <knaerzche at gmail.com>
---
- configure | 12 +
- libavcodec/Makefile | 1 +
- libavcodec/hwconfig.h | 2 +
- libavcodec/v4l2_request.c | 984 ++++++++++++++++++++++++++++++++++++++
- libavcodec/v4l2_request.h | 77 +++
- 5 files changed, 1076 insertions(+)
+ configure | 12 +
+ libavcodec/Makefile | 1 +
+ libavcodec/hwconfig.h | 2 +
+ libavcodec/v4l2_request.c | 1048 +++++++++++++++++++++++++++++++++++++
+ libavcodec/v4l2_request.h | 78 +++
+ 5 files changed, 1141 insertions(+)
create mode 100644 libavcodec/v4l2_request.c
create mode 100644 libavcodec/v4l2_request.h
diff --git a/configure b/configure
-index 8569a60bf827..9f9909a23696 100755
+index a89cfa6..503eb06 100755
--- a/configure
+++ b/configure
-@@ -274,6 +274,7 @@ External library support:
- --enable-libtls enable LibreSSL (via libtls), needed for https support
+@@ -279,6 +279,7 @@ External library support:
if openssl, gnutls or mbedtls is not used [no]
--enable-libtwolame enable MP2 encoding via libtwolame [no]
-+ --enable-libudev enable libudev [no]
--enable-libuavs3d enable AVS3 decoding via libuavs3d [no]
++ --enable-libudev enable libudev [no]
--enable-libv4l2 enable libv4l2/v4l-utils [no]
--enable-libvidstab enable video stabilization using vid.stab [no]
-@@ -342,6 +343,7 @@ External library support:
+ --enable-libvmaf enable vmaf filter via libvmaf [no]
+@@ -347,6 +348,7 @@ External library support:
--enable-omx-rpi enable OpenMAX IL code for Raspberry Pi [no]
--enable-rkmpp enable Rockchip Media Process Platform code [no]
--disable-v4l2-m2m disable V4L2 mem2mem code [autodetect]
@@ -87,15 +92,15 @@ index 8569a60bf827..9f9909a23696 100755
--disable-vaapi disable Video Acceleration API (mainly Unix/Intel) code [autodetect]
--disable-vdpau disable Nvidia Video Decode and Presentation API for Unix code [autodetect]
--disable-videotoolbox disable VideoToolbox code [autodetect]
-@@ -1807,6 +1809,7 @@ EXTERNAL_LIBRARY_LIST="
- libtesseract
+@@ -1896,6 +1898,7 @@ EXTERNAL_LIBRARY_LIST="
libtheora
libtwolame
-+ libudev
libuavs3d
++ libudev
libv4l2
libvmaf
-@@ -1861,6 +1864,7 @@ HWACCEL_LIBRARY_LIST="
+ libvorbis
+@@ -1952,6 +1955,7 @@ HWACCEL_LIBRARY_LIST="
mmal
omx
opencl
@@ -103,23 +108,23 @@ index 8569a60bf827..9f9909a23696 100755
"
DOCUMENT_LIST="
-@@ -2903,6 +2907,7 @@ d3d11va_deps="dxva_h ID3D11VideoDecoder ID3D11VideoContext"
- dxva2_deps="dxva2api_h DXVA2_ConfigPictureDecode ole32 user32"
+@@ -3045,6 +3049,7 @@ dxva2_deps="dxva2api_h DXVA2_ConfigPictureDecode ole32 user32"
ffnvcodec_deps_any="libdl LoadLibrary"
+ mediacodec_deps="android"
nvdec_deps="ffnvcodec"
+v4l2_request_deps="linux_videodev2_h linux_media_h v4l2_timeval_to_ns libdrm libudev"
vaapi_x11_deps="xlib_x11"
videotoolbox_hwaccel_deps="videotoolbox pthreads"
videotoolbox_hwaccel_extralibs="-framework QuartzCore"
-@@ -6376,6 +6381,7 @@ enabled libtls && require_pkg_config libtls libtls tls.h tls_configur
- enabled libtwolame && require libtwolame twolame.h twolame_init -ltwolame &&
+@@ -6807,6 +6812,7 @@ enabled libtwolame && require libtwolame twolame.h twolame_init -ltwolame
{ check_lib libtwolame twolame.h twolame_encode_buffer_float32_interleaved -ltwolame ||
die "ERROR: libtwolame must be installed and version must be >= 0.3.10"; }
-+enabled libudev && require_pkg_config libudev libudev libudev.h udev_new
enabled libuavs3d && require_pkg_config libuavs3d "uavs3d >= 1.1.41" uavs3d.h uavs3d_decode
++enabled libudev && require_pkg_config libudev libudev libudev.h udev_new
enabled libv4l2 && require_pkg_config libv4l2 libv4l2 libv4l2.h v4l2_ioctl
enabled libvidstab && require_pkg_config libvidstab "vidstab >= 0.98" vid.stab/libvidstab.h vsMotionDetectInit
-@@ -6475,6 +6481,10 @@ enabled rkmpp && { require_pkg_config rkmpp rockchip_mpp rockchip/r
+ enabled libvmaf && require_pkg_config libvmaf "libvmaf >= 2.0.0" libvmaf.h vmaf_init
+@@ -6910,6 +6916,10 @@ enabled rkmpp && { require_pkg_config rkmpp rockchip_mpp rockchip/r
{ enabled libdrm ||
die "ERROR: rkmpp requires --enable-libdrm"; }
}
@@ -130,7 +135,7 @@ index 8569a60bf827..9f9909a23696 100755
enabled vapoursynth && require_pkg_config vapoursynth "vapoursynth-script >= 42" VSScript.h vsscript_init
-@@ -6556,6 +6566,8 @@ if enabled v4l2_m2m; then
+@@ -6992,6 +7002,8 @@ if enabled v4l2_m2m; then
check_cc vp9_v4l2_m2m linux/videodev2.h "int i = V4L2_PIX_FMT_VP9;"
fi
@@ -140,10 +145,10 @@ index 8569a60bf827..9f9909a23696 100755
test_code cc sys/videoio.h "struct v4l2_frmsizeenum vfse; vfse.discrete.width = 0;" && enable_sanitized struct_v4l2_frmivalenum_discrete
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
-index 5a6ea59715af..d74220516826 100644
+index ec57e53..5e626c9 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
-@@ -153,6 +153,7 @@ OBJS-$(CONFIG_VP3DSP) += vp3dsp.o
+@@ -169,6 +169,7 @@ OBJS-$(CONFIG_VP3DSP) += vp3dsp.o
OBJS-$(CONFIG_VP56DSP) += vp56dsp.o
OBJS-$(CONFIG_VP8DSP) += vp8dsp.o
OBJS-$(CONFIG_V4L2_M2M) += v4l2_m2m.o v4l2_context.o v4l2_buffers.o v4l2_fmt.o
@@ -152,13 +157,13 @@ index 5a6ea59715af..d74220516826 100644
OBJS-$(CONFIG_WMV2DSP) += wmv2dsp.o
diff --git a/libavcodec/hwconfig.h b/libavcodec/hwconfig.h
-index f421dc909f44..ee78d8ab8e89 100644
+index e164722..ebafb6f 100644
--- a/libavcodec/hwconfig.h
+++ b/libavcodec/hwconfig.h
-@@ -80,6 +80,8 @@ typedef struct AVCodecHWConfigInternal {
+@@ -77,6 +77,8 @@ void ff_hwaccel_uninit(AVCodecContext *avctx);
+ HW_CONFIG_HWACCEL(1, 1, 1, VULKAN, VULKAN, ff_ ## codec ## _vulkan_hwaccel)
+ #define HWACCEL_D3D11VA(codec) \
HW_CONFIG_HWACCEL(0, 0, 1, D3D11VA_VLD, NONE, ff_ ## codec ## _d3d11va_hwaccel)
- #define HWACCEL_XVMC(codec) \
- HW_CONFIG_HWACCEL(0, 0, 1, XVMC, NONE, ff_ ## codec ## _xvmc_hwaccel)
+#define HWACCEL_V4L2REQUEST(codec) \
+ HW_CONFIG_HWACCEL(1, 0, 0, DRM_PRIME, DRM, ff_ ## codec ## _v4l2request_hwaccel)
@@ -166,10 +171,10 @@ index f421dc909f44..ee78d8ab8e89 100644
&(const AVCodecHWConfigInternal) { \
diff --git a/libavcodec/v4l2_request.c b/libavcodec/v4l2_request.c
new file mode 100644
-index 000000000000..5234b5049b0d
+index 0000000..595a53e
--- /dev/null
+++ b/libavcodec/v4l2_request.c
-@@ -0,0 +1,984 @@
+@@ -0,0 +1,1048 @@
+/*
+ * This file is part of FFmpeg.
+ *
@@ -189,19 +194,26 @@ index 000000000000..5234b5049b0d
+ */
+
+#include <drm_fourcc.h>
++#include <fcntl.h>
++#include <libudev.h>
+#include <linux/media.h>
++#include <sys/ioctl.h>
+#include <sys/mman.h>
-+#include <sys/types.h>
+#include <sys/stat.h>
-+#include <fcntl.h>
-+
+#include <sys/sysmacros.h>
-+#include <libudev.h>
++#include <sys/types.h>
++#include <unistd.h>
+
+#include "decode.h"
+#include "internal.h"
+#include "v4l2_request.h"
+
++static const AVClass v4l2_request_context_class = {
++ .class_name = "V4L2RequestContext",
++ .item_name = av_default_item_name,
++ .version = LIBAVUTIL_VERSION_INT,
++};
++
+uint64_t ff_v4l2_request_get_capture_timestamp(AVFrame *frame)
+{
+ V4L2RequestDescriptor *req = (V4L2RequestDescriptor*)frame->data[0];
@@ -314,14 +326,12 @@ index 000000000000..5234b5049b0d
+ .type = buf->buffer.type,
+ .memory = buf->buffer.memory,
+ .index = buf->index,
-+ .timestamp.tv_usec = ctx->timestamp,
++ .timestamp.tv_usec = buf->index + 1,
+ .bytesused = buf->used,
+ .request_fd = request_fd,
+ .flags = ((request_fd >= 0) ? V4L2_BUF_FLAG_REQUEST_FD : 0) | flags,
+ };
+
-+ buf->buffer.timestamp = buffer.timestamp;
-+
+ if (V4L2_TYPE_IS_MULTIPLANAR(buf->buffer.type)) {
+ planes[0].bytesused = buf->used;
+ buffer.bytesused = 0;
@@ -355,11 +365,14 @@ index 000000000000..5234b5049b0d
+ return 0;
+}
+
-+const uint32_t v4l2_request_capture_pixelformats[] = {
-+ V4L2_PIX_FMT_NV12,
-+#ifdef DRM_FORMAT_MOD_ALLWINNER_TILED
-+ V4L2_PIX_FMT_SUNXI_TILED_NV12,
-+#endif
++static const struct {
++ uint32_t pixelformat;
++ enum AVPixelFormat sw_format;
++ uint32_t drm_format;
++ uint64_t format_modifier;
++} v4l2_request_capture_pixelformats[] = {
++ { V4L2_PIX_FMT_NV12, AV_PIX_FMT_NV12, DRM_FORMAT_NV12, DRM_FORMAT_MOD_LINEAR },
++ { V4L2_PIX_FMT_SUNXI_TILED_NV12, AV_PIX_FMT_NV12, DRM_FORMAT_NV12, DRM_FORMAT_MOD_ALLWINNER_TILED },
+};
+
+static int v4l2_request_set_drm_descriptor(V4L2RequestDescriptor *req, struct v4l2_format *format)
@@ -368,21 +381,18 @@ index 000000000000..5234b5049b0d
+ AVDRMLayerDescriptor *layer = &desc->layers[0];
+ uint32_t pixelformat = V4L2_TYPE_IS_MULTIPLANAR(format->type) ? format->fmt.pix_mp.pixelformat : format->fmt.pix.pixelformat;
+
-+ switch (pixelformat) {
-+ case V4L2_PIX_FMT_NV12:
-+ layer->format = DRM_FORMAT_NV12;
-+ desc->objects[0].format_modifier = DRM_FORMAT_MOD_LINEAR;
-+ break;
-+#ifdef DRM_FORMAT_MOD_ALLWINNER_TILED
-+ case V4L2_PIX_FMT_SUNXI_TILED_NV12:
-+ layer->format = DRM_FORMAT_NV12;
-+ desc->objects[0].format_modifier = DRM_FORMAT_MOD_ALLWINNER_TILED;
-+ break;
-+#endif
-+ default:
-+ return -1;
++ layer->format = 0;
++ for (int i = 0; i < FF_ARRAY_ELEMS(v4l2_request_capture_pixelformats); i++) {
++ if (pixelformat == v4l2_request_capture_pixelformats[i].pixelformat) {
++ layer->format = v4l2_request_capture_pixelformats[i].drm_format;
++ desc->objects[0].format_modifier = v4l2_request_capture_pixelformats[i].format_modifier;
++ break;
++ }
+ }
+
++ if (!layer->format)
++ return -1;
++
+ desc->nb_objects = 1;
+ desc->objects[0].fd = req->capture.fd;
+ desc->objects[0].size = req->capture.size;
@@ -409,10 +419,8 @@ index 000000000000..5234b5049b0d
+ fd_set except_fds;
+ int ret;
+
-+ av_log(avctx, AV_LOG_DEBUG, "%s: avctx=%p used=%u controls=%d index=%d fd=%d request_fd=%d first_slice=%d last_slice=%d\n", __func__, avctx, req->output.used, count, req->capture.index, req->capture.fd, req->request_fd, first_slice, last_slice);
-+
-+ if (first_slice)
-+ ctx->timestamp++;
++ av_log(avctx, AV_LOG_DEBUG, "%s: ctx=%p used=%u controls=%d index=%d fd=%d request_fd=%d first_slice=%d last_slice=%d\n", __func__,
++ ctx, req->output.used, count, req->capture.index, req->capture.fd, req->request_fd, first_slice, last_slice);
+
+ ret = v4l2_request_set_controls(ctx, req->request_fd, control, count);
+ if (ret < 0) {
@@ -517,6 +525,38 @@ index 000000000000..5234b5049b0d
+ return v4l2_request_queue_decode(avctx, frame, control, count, 1, 1);
+}
+
++static int v4l2_request_try_framesize(AVCodecContext *avctx, uint32_t pixelformat)
++{
++ V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
++ struct v4l2_frmsizeenum frmsize = {
++ .index = 0,
++ .pixel_format = pixelformat,
++ };
++
++ if (ioctl(ctx->video_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize) < 0)
++ return 0;
++
++ /*
++ * We only validate min/max framesize for V4L2_FRMSIZE_TYPE_STEPWISE here, since the alignment
++ * which is eventually needed will be done driver-side later in VIDIOC_S_FMT and there is no need
++ * validate step_width/step_height here
++ */
++ do {
++ if (frmsize.type == V4L2_FRMSIZE_TYPE_DISCRETE && frmsize.discrete.width == avctx->coded_width &&
++ frmsize.discrete.height == avctx->coded_height)
++ return 0;
++ else if ((frmsize.type == V4L2_FRMSIZE_TYPE_STEPWISE || frmsize.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) &&
++ avctx->coded_width >= frmsize.stepwise.min_width && avctx->coded_height >= frmsize.stepwise.min_height &&
++ avctx->coded_width <= frmsize.stepwise.max_width && avctx->coded_height <= frmsize.stepwise.max_height)
++ return 0;
++
++ frmsize.index++;
++ } while (ioctl(ctx->video_fd, VIDIOC_ENUM_FRAMESIZES, &frmsize) >= 0);
++
++ av_log(avctx, AV_LOG_INFO, "%s: pixelformat %s not supported for width %u height %u\n", __func__, av_fourcc2str(pixelformat), avctx->coded_width, avctx->coded_height);
++ return -1;
++}
++
+static int v4l2_request_try_format(AVCodecContext *avctx, enum v4l2_buf_type type, uint32_t pixelformat)
+{
+ V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
@@ -550,7 +590,7 @@ index 000000000000..5234b5049b0d
+ fmtdesc.index++;
+ }
+
-+ av_log(avctx, AV_LOG_INFO, "%s: pixelformat %u not supported for type %u\n", __func__, pixelformat, type);
++ av_log(avctx, AV_LOG_DEBUG, "%s: pixelformat %s not supported for type %u\n", __func__, av_fourcc2str(pixelformat), type);
+ return -1;
+}
+
@@ -582,7 +622,7 @@ index 000000000000..5234b5049b0d
+ V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
+ enum v4l2_buf_type type = ctx->format.type;
+
-+#if 0
++#if 1
+ struct v4l2_format format = {
+ .type = type,
+ };
@@ -591,7 +631,6 @@ index 000000000000..5234b5049b0d
+ .type = type,
+ };
+ uint32_t pixelformat;
-+ int i;
+
+ if (ioctl(ctx->video_fd, VIDIOC_G_FMT, &format) < 0) {
+ av_log(avctx, AV_LOG_ERROR, "%s: get capture format failed, %s (%d)\n", __func__, strerror(errno), errno);
@@ -600,14 +639,14 @@ index 000000000000..5234b5049b0d
+
+ pixelformat = V4L2_TYPE_IS_MULTIPLANAR(type) ? format.fmt.pix_mp.pixelformat : format.fmt.pix.pixelformat;
+
-+ for (i = 0; i < FF_ARRAY_ELEMS(v4l2_request_capture_pixelformats); i++) {
-+ if (pixelformat == v4l2_request_capture_pixelformats[i])
++ for (int i = 0; i < FF_ARRAY_ELEMS(v4l2_request_capture_pixelformats); i++) {
++ if (pixelformat == v4l2_request_capture_pixelformats[i].pixelformat)
+ return v4l2_request_set_format(avctx, type, pixelformat, 0);
+ }
+
+ while (ioctl(ctx->video_fd, VIDIOC_ENUM_FMT, &fmtdesc) >= 0) {
-+ for (i = 0; i < FF_ARRAY_ELEMS(v4l2_request_capture_pixelformats); i++) {
-+ if (fmtdesc.pixelformat == v4l2_request_capture_pixelformats[i])
++ for (int i = 0; i < FF_ARRAY_ELEMS(v4l2_request_capture_pixelformats); i++) {
++ if (fmtdesc.pixelformat == v4l2_request_capture_pixelformats[i].pixelformat)
+ return v4l2_request_set_format(avctx, type, fmtdesc.pixelformat, 0);
+ }
+
@@ -615,7 +654,7 @@ index 000000000000..5234b5049b0d
+ }
+#else
+ for (int i = 0; i < FF_ARRAY_ELEMS(v4l2_request_capture_pixelformats); i++) {
-+ uint32_t pixelformat = v4l2_request_capture_pixelformats[i];
++ uint32_t pixelformat = v4l2_request_capture_pixelformats[i].pixelformat;
+ if (!v4l2_request_try_format(avctx, type, pixelformat))
+ return v4l2_request_set_format(avctx, type, pixelformat, 0);
+ }
@@ -657,7 +696,7 @@ index 000000000000..5234b5049b0d
+ else
+ capabilities = capability.capabilities;
+
-+ av_log(avctx, AV_LOG_DEBUG, "%s: avctx=%p ctx=%p path=%s capabilities=%u\n", __func__, avctx, ctx, path, capabilities);
++ av_log(avctx, AV_LOG_DEBUG, "%s: ctx=%p path=%s capabilities=%u\n", __func__, ctx, path, capabilities);
+
+ if ((capabilities & V4L2_CAP_STREAMING) != V4L2_CAP_STREAMING) {
+ av_log(avctx, AV_LOG_ERROR, "%s: missing required streaming capability\n", __func__);
@@ -679,7 +718,14 @@ index 000000000000..5234b5049b0d
+
+ ret = v4l2_request_try_format(avctx, ctx->output_type, pixelformat);
+ if (ret < 0) {
-+ av_log(avctx, AV_LOG_WARNING, "%s: try output format failed\n", __func__);
++ av_log(avctx, AV_LOG_DEBUG, "%s: try output format failed\n", __func__);
++ ret = AVERROR(EINVAL);
++ goto fail;
++ }
++
++ ret = v4l2_request_try_framesize(avctx, pixelformat);
++ if (ret < 0) {
++ av_log(avctx, AV_LOG_DEBUG, "%s: try framesize failed\n", __func__);
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
@@ -693,14 +739,14 @@ index 000000000000..5234b5049b0d
+
+ ret = v4l2_request_set_controls(ctx, -1, control, count);
+ if (ret < 0) {
-+ av_log(avctx, AV_LOG_ERROR, "%s: set controls failed, %s (%d)\n", __func__, strerror(errno), errno);
++ av_log(avctx, AV_LOG_DEBUG, "%s: set controls failed, %s (%d)\n", __func__, strerror(errno), errno);
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
+
+ ret = v4l2_request_select_capture_format(avctx);
+ if (ret < 0) {
-+ av_log(avctx, AV_LOG_WARNING, "%s: select capture format failed\n", __func__);
++ av_log(avctx, AV_LOG_DEBUG, "%s: select capture format failed\n", __func__);
+ ret = AVERROR(EINVAL);
+ goto fail;
+ }
@@ -728,9 +774,13 @@ index 000000000000..5234b5049b0d
+ }
+
+ if (V4L2_TYPE_IS_MULTIPLANAR(ctx->format.type)) {
-+ av_log(avctx, AV_LOG_DEBUG, "%s: pixelformat=%d width=%u height=%u bytesperline=%u sizeimage=%u num_planes=%u\n", __func__, ctx->format.fmt.pix_mp.pixelformat, ctx->format.fmt.pix_mp.width, ctx->format.fmt.pix_mp.height, ctx->format.fmt.pix_mp.plane_fmt[0].bytesperline, ctx->format.fmt.pix_mp.plane_fmt[0].sizeimage, ctx->format.fmt.pix_mp.num_planes);
++ struct v4l2_pix_format_mplane *fmt = &ctx->format.fmt.pix_mp;
++ av_log(ctx, AV_LOG_DEBUG, "%s: pixelformat=%s width=%u height=%u bytesperline=%u sizeimage=%u num_planes=%u\n", __func__,
++ av_fourcc2str(fmt->pixelformat), fmt->width, fmt->height, fmt->plane_fmt[0].bytesperline, fmt->plane_fmt[0].sizeimage, fmt->num_planes);
+ } else {
-+ av_log(avctx, AV_LOG_DEBUG, "%s: pixelformat=%d width=%u height=%u bytesperline=%u sizeimage=%u\n", __func__, ctx->format.fmt.pix.pixelformat, ctx->format.fmt.pix.width, ctx->format.fmt.pix.height, ctx->format.fmt.pix.bytesperline, ctx->format.fmt.pix.sizeimage);
++ struct v4l2_pix_format *fmt = &ctx->format.fmt.pix;
++ av_log(ctx, AV_LOG_DEBUG, "%s: pixelformat=%s width=%u height=%u bytesperline=%u sizeimage=%u\n", __func__,
++ av_fourcc2str(fmt->pixelformat), fmt->width, fmt->height, fmt->bytesperline, fmt->sizeimage);
+ }
+
+ ret = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_DRM);
@@ -790,7 +840,7 @@ index 000000000000..5234b5049b0d
+ goto fail;
+ }
+
-+ av_log(avctx, AV_LOG_DEBUG, "%s: avctx=%p ctx=%p path=%s driver=%s\n", __func__, avctx, ctx, path, device_info.driver);
++ av_log(avctx, AV_LOG_DEBUG, "%s: ctx=%p path=%s driver=%s\n", __func__, ctx, path, device_info.driver);
+
+ ret = ioctl(ctx->media_fd, MEDIA_IOC_G_TOPOLOGY, &topology);
+ if (ret < 0) {
@@ -835,13 +885,13 @@ index 000000000000..5234b5049b0d
+ ret = v4l2_request_probe_video_device(video_device, avctx, pixelformat, buffersize, control, count);
+ udev_device_unref(video_device);
+
-+ if (!ret)
-+ break;
++ if (!ret) {
++ av_freep(&interfaces);
++ av_log(avctx, AV_LOG_INFO, "Using V4L2 media device %s (%s) for %s\n", path, device_info.driver, av_fourcc2str(pixelformat));
++ return 0;
++ }
+ }
+
-+ av_freep(&interfaces);
-+ return ret;
-+
+fail:
+ av_freep(&interfaces);
+ if (ctx->media_fd >= 0) {
@@ -861,11 +911,11 @@ index 000000000000..5234b5049b0d
+ struct udev_list_entry *entry;
+ struct udev_device *device;
+
-+ av_log(avctx, AV_LOG_DEBUG, "%s: avctx=%p hw_device_ctx=%p hw_frames_ctx=%p\n", __func__, avctx, avctx->hw_device_ctx, avctx->hw_frames_ctx);
++ av_log(avctx, AV_LOG_DEBUG, "%s: ctx=%p hw_device_ctx=%p hw_frames_ctx=%p\n", __func__, ctx, avctx->hw_device_ctx, avctx->hw_frames_ctx);
+
++ ctx->av_class = &v4l2_request_context_class;
+ ctx->media_fd = -1;
+ ctx->video_fd = -1;
-+ ctx->timestamp = 0;
+
+ udev = udev_new();
+ if (!udev) {
@@ -905,6 +955,8 @@ index 000000000000..5234b5049b0d
+
+ if (!ret)
+ ret = v4l2_request_init_context(avctx);
++ else
++ av_log(avctx, AV_LOG_INFO, "No V4L2 media device found for %s\n", av_fourcc2str(pixelformat));
+
+fail:
+ udev_unref(udev);
@@ -916,7 +968,7 @@ index 000000000000..5234b5049b0d
+ V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
+ int ret;
+
-+ av_log(avctx, AV_LOG_DEBUG, "%s: avctx=%p ctx=%p\n", __func__, avctx, ctx);
++ av_log(ctx, AV_LOG_DEBUG, "%s: avctx=%p\n", __func__, avctx);
+
+ if (ctx->video_fd >= 0) {
+ ret = ioctl(ctx->video_fd, VIDIOC_STREAMOFF, &ctx->output_type);
@@ -942,9 +994,8 @@ index 000000000000..5234b5049b0d
+ return 0;
+}
+
-+static int v4l2_request_buffer_alloc(AVCodecContext *avctx, V4L2RequestBuffer *buf, enum v4l2_buf_type type)
++static int v4l2_request_buffer_alloc(V4L2RequestContext *ctx, V4L2RequestBuffer *buf, enum v4l2_buf_type type)
+{
-+ V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
+ int ret;
+ struct v4l2_plane planes[1] = {};
+ struct v4l2_create_buffers buffers = {
@@ -953,23 +1004,27 @@ index 000000000000..5234b5049b0d
+ .format.type = type,
+ };
+
-+ av_log(avctx, AV_LOG_DEBUG, "%s: avctx=%p buf=%p type=%u\n", __func__, avctx, buf, type);
++ av_log(ctx, AV_LOG_DEBUG, "%s: buf=%p type=%u\n", __func__, buf, type);
+
+ ret = ioctl(ctx->video_fd, VIDIOC_G_FMT, &buffers.format);
+ if (ret < 0) {
-+ av_log(avctx, AV_LOG_ERROR, "%s: get format failed for type %u, %s (%d)\n", __func__, type, strerror(errno), errno);
++ av_log(ctx, AV_LOG_ERROR, "%s: get format failed for type %u, %s (%d)\n", __func__, type, strerror(errno), errno);
+ return ret;
+ }
+
+ if (V4L2_TYPE_IS_MULTIPLANAR(buffers.format.type)) {
-+ av_log(avctx, AV_LOG_DEBUG, "%s: pixelformat=%d width=%u height=%u bytesperline=%u sizeimage=%u num_planes=%u\n", __func__, buffers.format.fmt.pix_mp.pixelformat, buffers.format.fmt.pix_mp.width, buffers.format.fmt.pix_mp.height, buffers.format.fmt.pix_mp.plane_fmt[0].bytesperline, buffers.format.fmt.pix_mp.plane_fmt[0].sizeimage, buffers.format.fmt.pix_mp.num_planes);
++ struct v4l2_pix_format_mplane *fmt = &buffers.format.fmt.pix_mp;
++ av_log(ctx, AV_LOG_DEBUG, "%s: pixelformat=%s width=%u height=%u bytesperline=%u sizeimage=%u num_planes=%u\n", __func__,
++ av_fourcc2str(fmt->pixelformat), fmt->width, fmt->height, fmt->plane_fmt[0].bytesperline, fmt->plane_fmt[0].sizeimage, fmt->num_planes);
+ } else {
-+ av_log(avctx, AV_LOG_DEBUG, "%s: pixelformat=%d width=%u height=%u bytesperline=%u sizeimage=%u\n", __func__, buffers.format.fmt.pix.pixelformat, buffers.format.fmt.pix.width, buffers.format.fmt.pix.height, buffers.format.fmt.pix.bytesperline, buffers.format.fmt.pix.sizeimage);
++ struct v4l2_pix_format *fmt = &buffers.format.fmt.pix;
++ av_log(ctx, AV_LOG_DEBUG, "%s: pixelformat=%s width=%u height=%u bytesperline=%u sizeimage=%u\n", __func__,
++ av_fourcc2str(fmt->pixelformat), fmt->width, fmt->height, fmt->bytesperline, fmt->sizeimage);
+ }
+
+ ret = ioctl(ctx->video_fd, VIDIOC_CREATE_BUFS, &buffers);
+ if (ret < 0) {
-+ av_log(avctx, AV_LOG_ERROR, "%s: create buffers failed for type %u, %s (%d)\n", __func__, type, strerror(errno), errno);
++ av_log(ctx, AV_LOG_ERROR, "%s: create buffers failed for type %u, %s (%d)\n", __func__, type, strerror(errno), errno);
+ return ret;
+ }
+
@@ -995,14 +1050,17 @@ index 000000000000..5234b5049b0d
+
+ ret = ioctl(ctx->video_fd, VIDIOC_QUERYBUF, &buf->buffer);
+ if (ret < 0) {
-+ av_log(avctx, AV_LOG_ERROR, "%s: query buffer %d failed, %s (%d)\n", __func__, buf->index, strerror(errno), errno);
++ av_log(ctx, AV_LOG_ERROR, "%s: query buffer %d failed, %s (%d)\n", __func__, buf->index, strerror(errno), errno);
+ return ret;
+ }
+
++ buf->buffer.timestamp.tv_usec = buf->index + 1;
++
+ if (V4L2_TYPE_IS_OUTPUT(type)) {
-+ void *addr = mmap(NULL, buf->size, PROT_READ | PROT_WRITE, MAP_SHARED, ctx->video_fd, V4L2_TYPE_IS_MULTIPLANAR(type) ? buf->buffer.m.planes[0].m.mem_offset : buf->buffer.m.offset);
++ uint32_t offset = V4L2_TYPE_IS_MULTIPLANAR(type) ? buf->buffer.m.planes[0].m.mem_offset : buf->buffer.m.offset;
++ void *addr = mmap(NULL, buf->size, PROT_READ | PROT_WRITE, MAP_SHARED, ctx->video_fd, offset);
+ if (addr == MAP_FAILED) {
-+ av_log(avctx, AV_LOG_ERROR, "%s: mmap failed, %s (%d)\n", __func__, strerror(errno), errno);
++ av_log(ctx, AV_LOG_ERROR, "%s: mmap failed, %s (%d)\n", __func__, strerror(errno), errno);
+ return -1;
+ }
+
@@ -1016,20 +1074,22 @@ index 000000000000..5234b5049b0d
+
+ ret = ioctl(ctx->video_fd, VIDIOC_EXPBUF, &exportbuffer);
+ if (ret < 0) {
-+ av_log(avctx, AV_LOG_ERROR, "%s: export buffer %d failed, %s (%d)\n", __func__, buf->index, strerror(errno), errno);
++ av_log(ctx, AV_LOG_ERROR, "%s: export buffer %d failed, %s (%d)\n", __func__, buf->index, strerror(errno), errno);
+ return ret;
+ }
+
+ buf->fd = exportbuffer.fd;
+ }
+
-+ av_log(avctx, AV_LOG_DEBUG, "%s: buf=%p index=%d fd=%d addr=%p width=%u height=%u size=%u\n", __func__, buf, buf->index, buf->fd, buf->addr, buf->width, buf->height, buf->size);
++ av_log(ctx, AV_LOG_DEBUG, "%s: buf=%p index=%d fd=%d addr=%p width=%u height=%u size=%u\n", __func__,
++ buf, buf->index, buf->fd, buf->addr, buf->width, buf->height, buf->size);
+ return 0;
+}
+
+static void v4l2_request_buffer_free(V4L2RequestBuffer *buf)
+{
-+ av_log(NULL, AV_LOG_DEBUG, "%s: buf=%p index=%d fd=%d addr=%p width=%u height=%u size=%u\n", __func__, buf, buf->index, buf->fd, buf->addr, buf->width, buf->height, buf->size);
++ av_log(NULL, AV_LOG_DEBUG, "%s: buf=%p index=%d fd=%d addr=%p width=%u height=%u size=%u\n", __func__,
++ buf, buf->index, buf->fd, buf->addr, buf->width, buf->height, buf->size);
+
+ if (buf->addr)
+ munmap(buf->addr, buf->size);
@@ -1040,10 +1100,9 @@ index 000000000000..5234b5049b0d
+
+static void v4l2_request_frame_free(void *opaque, uint8_t *data)
+{
-+ AVCodecContext *avctx = opaque;
+ V4L2RequestDescriptor *req = (V4L2RequestDescriptor*)data;
+
-+ av_log(NULL, AV_LOG_DEBUG, "%s: avctx=%p data=%p request_fd=%d\n", __func__, avctx, data, req->request_fd);
++ av_log(NULL, AV_LOG_DEBUG, "%s: opaque=%p data=%p request_fd=%d\n", __func__, opaque, data, req->request_fd);
+
+ if (req->request_fd >= 0)
+ close(req->request_fd);
@@ -1054,10 +1113,9 @@ index 000000000000..5234b5049b0d
+ av_free(data);
+}
+
-+static AVBufferRef *v4l2_request_frame_alloc(void *opaque, int size)
++static AVBufferRef *v4l2_request_frame_alloc(void *opaque, size_t size)
+{
-+ AVCodecContext *avctx = opaque;
-+ V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
++ V4L2RequestContext *ctx = opaque;
+ V4L2RequestDescriptor *req;
+ AVBufferRef *ref;
+ uint8_t *data;
@@ -1067,9 +1125,9 @@ index 000000000000..5234b5049b0d
+ if (!data)
+ return NULL;
+
-+ av_log(avctx, AV_LOG_DEBUG, "%s: avctx=%p size=%d data=%p\n", __func__, avctx, size, data);
++ av_log(ctx, AV_LOG_DEBUG, "%s: size=%zu data=%p\n", __func__, size, data);
+
-+ ref = av_buffer_create(data, size, v4l2_request_frame_free, avctx, 0);
++ ref = av_buffer_create(data, size, v4l2_request_frame_free, ctx, 0);
+ if (!ref) {
+ av_freep(&data);
+ return NULL;
@@ -1080,13 +1138,13 @@ index 000000000000..5234b5049b0d
+ req->output.fd = -1;
+ req->capture.fd = -1;
+
-+ ret = v4l2_request_buffer_alloc(avctx, &req->output, ctx->output_type);
++ ret = v4l2_request_buffer_alloc(ctx, &req->output, ctx->output_type);
+ if (ret < 0) {
+ av_buffer_unref(&ref);
+ return NULL;
+ }
+
-+ ret = v4l2_request_buffer_alloc(avctx, &req->capture, ctx->format.type);
++ ret = v4l2_request_buffer_alloc(ctx, &req->capture, ctx->format.type);
+ if (ret < 0) {
+ av_buffer_unref(&ref);
+ return NULL;
@@ -1094,12 +1152,12 @@ index 000000000000..5234b5049b0d
+
+ ret = ioctl(ctx->media_fd, MEDIA_IOC_REQUEST_ALLOC, &req->request_fd);
+ if (ret < 0) {
-+ av_log(avctx, AV_LOG_ERROR, "%s: request alloc failed, %s (%d)\n", __func__, strerror(errno), errno);
++ av_log(ctx, AV_LOG_ERROR, "%s: request alloc failed, %s (%d)\n", __func__, strerror(errno), errno);
+ av_buffer_unref(&ref);
+ return NULL;
+ }
+
-+ av_log(avctx, AV_LOG_DEBUG, "%s: avctx=%p size=%d data=%p request_fd=%d\n", __func__, avctx, size, data, req->request_fd);
++ av_log(ctx, AV_LOG_DEBUG, "%s: size=%zu data=%p request_fd=%d\n", __func__, size, data, req->request_fd);
+ return ref;
+}
+
@@ -1120,18 +1178,29 @@ index 000000000000..5234b5049b0d
+{
+ V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
+ AVHWFramesContext *hwfc = (AVHWFramesContext*)hw_frames_ctx->data;
++ uint32_t pixelformat;
+
+ hwfc->format = AV_PIX_FMT_DRM_PRIME;
-+ hwfc->sw_format = AV_PIX_FMT_NV12;
++ hwfc->sw_format = AV_PIX_FMT_NONE;
++
+ if (V4L2_TYPE_IS_MULTIPLANAR(ctx->format.type)) {
+ hwfc->width = ctx->format.fmt.pix_mp.width;
+ hwfc->height = ctx->format.fmt.pix_mp.height;
++ pixelformat = ctx->format.fmt.pix_mp.pixelformat;
+ } else {
+ hwfc->width = ctx->format.fmt.pix.width;
+ hwfc->height = ctx->format.fmt.pix.height;
++ pixelformat = ctx->format.fmt.pix.pixelformat;
++ }
++
++ for (int i = 0; i < FF_ARRAY_ELEMS(v4l2_request_capture_pixelformats); i++) {
++ if (pixelformat == v4l2_request_capture_pixelformats[i].pixelformat) {
++ hwfc->sw_format = v4l2_request_capture_pixelformats[i].sw_format;
++ break;
++ }
+ }
+
-+ hwfc->pool = av_buffer_pool_init2(sizeof(V4L2RequestDescriptor), avctx, v4l2_request_frame_alloc, v4l2_request_pool_free);
++ hwfc->pool = av_buffer_pool_init2(sizeof(V4L2RequestDescriptor), ctx, v4l2_request_frame_alloc, v4l2_request_pool_free);
+ if (!hwfc->pool)
+ return AVERROR(ENOMEM);
+
@@ -1150,16 +1219,16 @@ index 000000000000..5234b5049b0d
+ hwfc->initial_pool_size += 2;
+ }
+
-+ av_log(avctx, AV_LOG_DEBUG, "%s: avctx=%p ctx=%p hw_frames_ctx=%p hwfc=%p pool=%p width=%d height=%d initial_pool_size=%d\n", __func__, avctx, ctx, hw_frames_ctx, hwfc, hwfc->pool, hwfc->width, hwfc->height, hwfc->initial_pool_size);
-+
++ av_log(ctx, AV_LOG_DEBUG, "%s: avctx=%p hw_frames_ctx=%p hwfc=%p pool=%p width=%d height=%d initial_pool_size=%d\n", __func__,
++ avctx, hw_frames_ctx, hwfc, hwfc->pool, hwfc->width, hwfc->height, hwfc->initial_pool_size);
+ return 0;
+}
diff --git a/libavcodec/v4l2_request.h b/libavcodec/v4l2_request.h
new file mode 100644
-index 000000000000..58d2aa70af80
+index 0000000..5f105d8
--- /dev/null
+++ b/libavcodec/v4l2_request.h
-@@ -0,0 +1,77 @@
+@@ -0,0 +1,78 @@
+/*
+ * This file is part of FFmpeg.
+ *
@@ -1186,6 +1255,7 @@ index 000000000000..58d2aa70af80
+#include "libavutil/hwcontext_drm.h"
+
+typedef struct V4L2RequestContext {
++ const AVClass *av_class;
+ int video_fd;
+ int media_fd;
+ enum v4l2_buf_type output_type;
@@ -1237,11 +1307,15 @@ index 000000000000..58d2aa70af80
+int ff_v4l2_request_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx);
+
+#endif /* AVCODEC_V4L2_REQUEST_H */
-From 6349ae1e10d1b68c47c3b883b27567f9678c6e0c Mon Sep 17 00:00:00 2001
+--
+2.44.0
+
+
+From 9c440906da4c4f0f63faa71a0b988d2447ca57bf Mon Sep 17 00:00:00 2001
From: Boris Brezillon <boris.brezillon at collabora.com>
Date: Wed, 22 May 2019 14:44:22 +0200
-Subject: [PATCH] h264dec: add ref_pic_marking and pic_order_cnt bit_size to
- slice context
+Subject: [PATCH 03/18] h264dec: add ref_pic_marking and pic_order_cnt bit_size
+ to slice context
Used by V4L2 request API h264 hwaccel
@@ -1253,10 +1327,10 @@ Signed-off-by: Jonas Karlman <jonas at kwiboo.se>
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c
-index a19635a23d44..5a5ceb09352c 100644
+index 5c17edf..ea7e882 100644
--- a/libavcodec/h264_slice.c
+++ b/libavcodec/h264_slice.c
-@@ -1736,7 +1736,7 @@ static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
+@@ -1685,7 +1685,7 @@ static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
unsigned int slice_type, tmp, i;
int field_pic_flag, bottom_field_flag;
int first_slice = sl == h->slice_ctx && !h->current_slice;
@@ -1265,15 +1339,15 @@ index a19635a23d44..5a5ceb09352c 100644
if (first_slice)
av_assert0(!h->setup_finished);
-@@ -1820,6 +1820,7 @@ static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
- av_log(h->avctx, AV_LOG_WARNING, "idr_pic_id is invalid\n");
- }
+@@ -1776,6 +1776,7 @@ static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
-+ pos = sl->gb.index;
sl->poc_lsb = 0;
sl->delta_poc_bottom = 0;
++ pos = sl->gb.index;
if (sps->poc_type == 0) {
-@@ -1833,6 +1834,7 @@ static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
+ sl->poc_lsb = get_bits(&sl->gb, sps->log2_max_poc_lsb);
+
+@@ -1790,6 +1791,7 @@ static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
sl->delta_poc[1] = get_se_golomb(&sl->gb);
}
@@ -1281,7 +1355,7 @@ index a19635a23d44..5a5ceb09352c 100644
sl->redundant_pic_count = 0;
if (pps->redundant_pic_cnt_present)
-@@ -1872,9 +1874,11 @@ static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
+@@ -1829,9 +1831,11 @@ static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
sl->explicit_ref_marking = 0;
if (nal->ref_idc) {
@@ -1294,18 +1368,18 @@ index a19635a23d44..5a5ceb09352c 100644
if (sl->slice_type_nos != AV_PICTURE_TYPE_I && pps->cabac) {
diff --git a/libavcodec/h264dec.h b/libavcodec/h264dec.h
-index 316dc6a2c890..f2cabac468d0 100644
+index 675be9b..a9d515d 100644
--- a/libavcodec/h264dec.h
+++ b/libavcodec/h264dec.h
-@@ -328,6 +328,7 @@ typedef struct H264SliceContext {
- MMCO mmco[MAX_MMCO_COUNT];
+@@ -315,6 +315,7 @@ typedef struct H264SliceContext {
+ MMCO mmco[H264_MAX_MMCO_COUNT];
int nb_mmco;
int explicit_ref_marking;
+ int ref_pic_marking_bit_size;
int frame_num;
int idr_pic_id;
-@@ -336,6 +337,7 @@ typedef struct H264SliceContext {
+@@ -323,6 +324,7 @@ typedef struct H264SliceContext {
int delta_poc[2];
int curr_pic_num;
int max_pic_num;
@@ -1313,10 +1387,14 @@ index 316dc6a2c890..f2cabac468d0 100644
} H264SliceContext;
/**
-From 650875c24687c35290b2393ef39f77074ca49813 Mon Sep 17 00:00:00 2001
+--
+2.44.0
+
+
+From d507e050b4dbab847abcc3d97c902f8e01d39945 Mon Sep 17 00:00:00 2001
From: Jernej Skrabec <jernej.skrabec at siol.net>
Date: Sat, 15 Dec 2018 22:32:16 +0100
-Subject: [PATCH] Add V4L2 request API h264 hwaccel
+Subject: [PATCH 04/18] Add V4L2 request API h264 hwaccel
Signed-off-by: Jernej Skrabec <jernej.skrabec at siol.net>
Signed-off-by: Jonas Karlman <jonas at kwiboo.se>
@@ -1326,24 +1404,24 @@ Signed-off-by: Jonas Karlman <jonas at kwiboo.se>
libavcodec/h264_slice.c | 4 +
libavcodec/h264dec.c | 3 +
libavcodec/hwaccels.h | 1 +
- libavcodec/v4l2_request_h264.c | 456 +++++++++++++++++++++++++++++++++
- 6 files changed, 468 insertions(+)
+ libavcodec/v4l2_request_h264.c | 462 +++++++++++++++++++++++++++++++++
+ 6 files changed, 474 insertions(+)
create mode 100644 libavcodec/v4l2_request_h264.c
diff --git a/configure b/configure
-index 9f9909a23696..293d0447f683 100755
+index 503eb06..252d188 100755
--- a/configure
+++ b/configure
-@@ -2925,6 +2925,8 @@ h264_dxva2_hwaccel_deps="dxva2"
+@@ -3081,6 +3081,8 @@ h264_dxva2_hwaccel_deps="dxva2"
h264_dxva2_hwaccel_select="h264_decoder"
h264_nvdec_hwaccel_deps="nvdec"
h264_nvdec_hwaccel_select="h264_decoder"
-+h264_v4l2request_hwaccel_deps="v4l2_request"
++h264_v4l2request_hwaccel_deps="v4l2_request h264_v4l2_request"
+h264_v4l2request_hwaccel_select="h264_decoder"
h264_vaapi_hwaccel_deps="vaapi"
h264_vaapi_hwaccel_select="h264_decoder"
h264_vdpau_hwaccel_deps="vdpau"
-@@ -6567,6 +6569,7 @@ if enabled v4l2_m2m; then
+@@ -7003,6 +7005,7 @@ if enabled v4l2_m2m; then
fi
check_func_headers "linux/media.h linux/videodev2.h" v4l2_timeval_to_ns
@@ -1352,10 +1430,10 @@ index 9f9909a23696..293d0447f683 100755
check_headers sys/videoio.h
test_code cc sys/videoio.h "struct v4l2_frmsizeenum vfse; vfse.discrete.width = 0;" && enable_sanitized struct_v4l2_frmivalenum_discrete
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
-index d74220516826..4f6e7fc2515c 100644
+index 5e626c9..4bfd477 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
-@@ -903,6 +903,7 @@ OBJS-$(CONFIG_H264_D3D11VA_HWACCEL) += dxva2_h264.o
+@@ -1001,6 +1001,7 @@ OBJS-$(CONFIG_H264_D3D11VA_HWACCEL) += dxva2_h264.o
OBJS-$(CONFIG_H264_DXVA2_HWACCEL) += dxva2_h264.o
OBJS-$(CONFIG_H264_NVDEC_HWACCEL) += nvdec_h264.o
OBJS-$(CONFIG_H264_QSV_HWACCEL) += qsvdec.o
@@ -1364,35 +1442,35 @@ index d74220516826..4f6e7fc2515c 100644
OBJS-$(CONFIG_H264_VDPAU_HWACCEL) += vdpau_h264.o
OBJS-$(CONFIG_H264_VIDEOTOOLBOX_HWACCEL) += videotoolbox.o
diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c
-index 5a5ceb09352c..4ed267317184 100644
+index ea7e882..236bdc6 100644
--- a/libavcodec/h264_slice.c
+++ b/libavcodec/h264_slice.c
-@@ -759,6 +759,7 @@ static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
+@@ -786,6 +786,7 @@ static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
#define HWACCEL_MAX (CONFIG_H264_DXVA2_HWACCEL + \
(CONFIG_H264_D3D11VA_HWACCEL * 2) + \
CONFIG_H264_NVDEC_HWACCEL + \
+ CONFIG_H264_V4L2REQUEST_HWACCEL + \
CONFIG_H264_VAAPI_HWACCEL + \
CONFIG_H264_VIDEOTOOLBOX_HWACCEL + \
- CONFIG_H264_VDPAU_HWACCEL)
-@@ -843,6 +844,9 @@ static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
- #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
- if (h->avctx->colorspace != AVCOL_SPC_RGB)
- *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
+ CONFIG_H264_VDPAU_HWACCEL + \
+@@ -890,6 +891,9 @@ static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
+ #endif
+ #if CONFIG_H264_VAAPI_HWACCEL
+ *fmt++ = AV_PIX_FMT_VAAPI;
+#endif
+#if CONFIG_H264_V4L2REQUEST_HWACCEL
+ *fmt++ = AV_PIX_FMT_DRM_PRIME;
#endif
- if (CHROMA444(h)) {
- if (h->avctx->colorspace == AVCOL_SPC_RGB) {
+ if (h->avctx->color_range == AVCOL_RANGE_JPEG)
+ *fmt++ = AV_PIX_FMT_YUVJ420P;
diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c
-index 5eedeb3c275d..a504c89565c7 100644
+index f346c65..8d6334d 100644
--- a/libavcodec/h264dec.c
+++ b/libavcodec/h264dec.c
-@@ -1102,6 +1102,9 @@ AVCodec ff_h264_decoder = {
+@@ -1135,6 +1135,9 @@ const FFCodec ff_h264_decoder = {
#endif
- #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
- HWACCEL_VIDEOTOOLBOX(h264),
+ #if CONFIG_H264_VULKAN_HWACCEL
+ HWACCEL_VULKAN(h264),
+#endif
+#if CONFIG_H264_V4L2REQUEST_HWACCEL
+ HWACCEL_V4L2REQUEST(h264),
@@ -1400,23 +1478,23 @@ index 5eedeb3c275d..a504c89565c7 100644
NULL
},
diff --git a/libavcodec/hwaccels.h b/libavcodec/hwaccels.h
-index 6109c89bd63c..f758c34ddcf9 100644
+index c463071..5211553 100644
--- a/libavcodec/hwaccels.h
+++ b/libavcodec/hwaccels.h
-@@ -27,6 +27,7 @@ extern const AVHWAccel ff_h264_d3d11va_hwaccel;
- extern const AVHWAccel ff_h264_d3d11va2_hwaccel;
- extern const AVHWAccel ff_h264_dxva2_hwaccel;
- extern const AVHWAccel ff_h264_nvdec_hwaccel;
-+extern const AVHWAccel ff_h264_v4l2request_hwaccel;
- extern const AVHWAccel ff_h264_vaapi_hwaccel;
- extern const AVHWAccel ff_h264_vdpau_hwaccel;
- extern const AVHWAccel ff_h264_videotoolbox_hwaccel;
+@@ -32,6 +32,7 @@ extern const struct FFHWAccel ff_h264_d3d11va_hwaccel;
+ extern const struct FFHWAccel ff_h264_d3d11va2_hwaccel;
+ extern const struct FFHWAccel ff_h264_dxva2_hwaccel;
+ extern const struct FFHWAccel ff_h264_nvdec_hwaccel;
++extern const struct FFHWAccel ff_h264_v4l2request_hwaccel;
+ extern const struct FFHWAccel ff_h264_vaapi_hwaccel;
+ extern const struct FFHWAccel ff_h264_vdpau_hwaccel;
+ extern const struct FFHWAccel ff_h264_videotoolbox_hwaccel;
diff --git a/libavcodec/v4l2_request_h264.c b/libavcodec/v4l2_request_h264.c
new file mode 100644
-index 000000000000..88da8f0a2db0
+index 0000000..980cd93
--- /dev/null
+++ b/libavcodec/v4l2_request_h264.c
-@@ -0,0 +1,456 @@
+@@ -0,0 +1,462 @@
+/*
+ * This file is part of FFmpeg.
+ *
@@ -1436,7 +1514,9 @@ index 000000000000..88da8f0a2db0
+ */
+
+#include "h264dec.h"
++#include "hwaccel_internal.h"
+#include "hwconfig.h"
++#include "internal.h"
+#include "v4l2_request.h"
+
+typedef struct V4L2RequestControlsH264 {
@@ -1481,11 +1561,11 @@ index 000000000000..88da8f0a2db0
+ }
+}
+
-+static void fill_dpb_entry(struct v4l2_h264_dpb_entry *entry, const H264Picture *pic)
++static void fill_dpb_entry(struct v4l2_h264_dpb_entry *entry, const H264Picture *pic, int long_idx)
+{
+ entry->reference_ts = ff_v4l2_request_get_capture_timestamp(pic->f);
+ entry->pic_num = pic->pic_id;
-+ entry->frame_num = pic->frame_num;
++ entry->frame_num = pic->long_ref ? long_idx : pic->frame_num;
+ entry->fields = pic->reference & V4L2_H264_FRAME_REF;
+ entry->flags = V4L2_H264_DPB_ENTRY_FLAG_VALID;
+ if (entry->fields)
@@ -1507,7 +1587,7 @@ index 000000000000..88da8f0a2db0
+ for (int i = 0; i < h->short_ref_count; i++) {
+ const H264Picture *pic = h->short_ref[i];
+ if (pic && (pic->field_poc[0] != INT_MAX || pic->field_poc[1] != INT_MAX))
-+ fill_dpb_entry(&decode->dpb[entries++], pic);
++ fill_dpb_entry(&decode->dpb[entries++], pic, pic->pic_id);
+ }
+
+ if (!h->long_ref_count)
@@ -1516,7 +1596,7 @@ index 000000000000..88da8f0a2db0
+ for (int i = 0; i < FF_ARRAY_ELEMS(h->long_ref); i++) {
+ const H264Picture *pic = h->long_ref[i];
+ if (pic && (pic->field_poc[0] != INT_MAX || pic->field_poc[1] != INT_MAX))
-+ fill_dpb_entry(&decode->dpb[entries++], pic);
++ fill_dpb_entry(&decode->dpb[entries++], pic, i);
+ }
+}
+
@@ -1668,10 +1748,14 @@ index 000000000000..88da8f0a2db0
+ controls->decode_params.flags |= V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC;
+ if (h->picture_structure == PICT_BOTTOM_FIELD)
+ controls->decode_params.flags |= V4L2_H264_DECODE_PARAM_FLAG_BOTTOM_FIELD;
++ if (sl->slice_type_nos == AV_PICTURE_TYPE_P)
++ controls->decode_params.flags |= V4L2_H264_DECODE_PARAM_FLAG_PFRAME;
++ if (sl->slice_type_nos == AV_PICTURE_TYPE_B)
++ controls->decode_params.flags |= V4L2_H264_DECODE_PARAM_FLAG_BFRAME;
+
+ fill_dpb(&controls->decode_params, h);
+
-+ controls->first_slice = !FIELD_PICTURE(h) || h->first_field;
++ controls->first_slice = 1;
+ controls->num_slices = 0;
+
+ return ff_v4l2_request_reset_frame(avctx, h->cur_pic_ptr->f);
@@ -1776,6 +1860,7 @@ index 000000000000..88da8f0a2db0
+
+ if (sl->slice_type == AV_PICTURE_TYPE_B && sl->direct_spatial_mv_pred)
+ controls->slice_params.flags |= V4L2_H264_SLICE_FLAG_DIRECT_SPATIAL_MV_PRED;
++
+ /* V4L2_H264_SLICE_FLAG_SP_FOR_SWITCH: not implemented by FFmpeg */
+
+ controls->pred_weights_required = V4L2_H264_CTRL_PRED_WEIGHTS_REQUIRED(&controls->pps, &controls->slice_params);
@@ -1802,8 +1887,7 @@ index 000000000000..88da8f0a2db0
+
+static int v4l2_request_h264_end_frame(AVCodecContext *avctx)
+{
-+ const H264Context *h = avctx->priv_data;
-+ return v4l2_request_h264_queue_decode(avctx, !FIELD_PICTURE(h) || !h->first_field);
++ return v4l2_request_h264_queue_decode(avctx, 1);
+}
+
+static int v4l2_request_h264_set_controls(AVCodecContext *avctx)
@@ -1858,11 +1942,11 @@ index 000000000000..88da8f0a2db0
+ return v4l2_request_h264_set_controls(avctx);
+}
+
-+const AVHWAccel ff_h264_v4l2request_hwaccel = {
-+ .name = "h264_v4l2request",
-+ .type = AVMEDIA_TYPE_VIDEO,
-+ .id = AV_CODEC_ID_H264,
-+ .pix_fmt = AV_PIX_FMT_DRM_PRIME,
++const FFHWAccel ff_h264_v4l2request_hwaccel = {
++ .p.name = "h264_v4l2request",
++ .p.type = AVMEDIA_TYPE_VIDEO,
++ .p.id = AV_CODEC_ID_H264,
++ .p.pix_fmt = AV_PIX_FMT_DRM_PRIME,
+ .start_frame = v4l2_request_h264_start_frame,
+ .decode_slice = v4l2_request_h264_decode_slice,
+ .end_frame = v4l2_request_h264_end_frame,
@@ -1873,57 +1957,2639 @@ index 000000000000..88da8f0a2db0
+ .frame_params = ff_v4l2_request_frame_params,
+ .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
+};
-From f9f4a89058a6fac25712cc385eab72f70e9ac4c8 Mon Sep 17 00:00:00 2001
+--
+2.44.0
+
+
+From f125ce861fcf1f4dfba26304f72a46dc15553d5a Mon Sep 17 00:00:00 2001
From: Jonas Karlman <jonas at kwiboo.se>
-Date: Mon, 29 Apr 2019 22:08:59 +0000
-Subject: [PATCH] HACK: hwcontext_drm: do not require drm device
+Date: Sat, 15 Dec 2018 22:32:16 +0100
+Subject: [PATCH 05/18] Add V4L2 request API mpeg2 hwaccel
Signed-off-by: Jonas Karlman <jonas at kwiboo.se>
---
- libavutil/hwcontext_drm.c | 5 +++++
- 1 file changed, 5 insertions(+)
+ configure | 3 +
+ libavcodec/Makefile | 1 +
+ libavcodec/hwaccels.h | 1 +
+ libavcodec/mpeg12dec.c | 6 ++
+ libavcodec/v4l2_request_mpeg2.c | 160 ++++++++++++++++++++++++++++++++
+ 5 files changed, 171 insertions(+)
+ create mode 100644 libavcodec/v4l2_request_mpeg2.c
-diff --git a/libavutil/hwcontext_drm.c b/libavutil/hwcontext_drm.c
-index 32cbde82ebfa..aa4794c5e665 100644
---- a/libavutil/hwcontext_drm.c
-+++ b/libavutil/hwcontext_drm.c
-@@ -43,6 +43,11 @@ static int drm_device_create(AVHWDeviceContext *hwdev, const char *device,
- AVDRMDeviceContext *hwctx = hwdev->hwctx;
- drmVersionPtr version;
+diff --git a/configure b/configure
+index 252d188..3127877 100755
+--- a/configure
++++ b/configure
+@@ -3125,6 +3125,8 @@ mpeg2_dxva2_hwaccel_deps="dxva2"
+ mpeg2_dxva2_hwaccel_select="mpeg2video_decoder"
+ mpeg2_nvdec_hwaccel_deps="nvdec"
+ mpeg2_nvdec_hwaccel_select="mpeg2video_decoder"
++mpeg2_v4l2request_hwaccel_deps="v4l2_request mpeg2_v4l2_request"
++mpeg2_v4l2request_hwaccel_select="mpeg2video_decoder"
+ mpeg2_vaapi_hwaccel_deps="vaapi"
+ mpeg2_vaapi_hwaccel_select="mpeg2video_decoder"
+ mpeg2_vdpau_hwaccel_deps="vdpau"
+@@ -7006,6 +7008,7 @@ fi
-+ if (device == NULL) {
-+ hwctx->fd = -1;
-+ return 0;
+ check_func_headers "linux/media.h linux/videodev2.h" v4l2_timeval_to_ns
+ check_cc h264_v4l2_request linux/videodev2.h "int i = V4L2_PIX_FMT_H264_SLICE;"
++check_cc mpeg2_v4l2_request linux/videodev2.h "int i = V4L2_PIX_FMT_MPEG2_SLICE;"
+
+ check_headers sys/videoio.h
+ test_code cc sys/videoio.h "struct v4l2_frmsizeenum vfse; vfse.discrete.width = 0;" && enable_sanitized struct_v4l2_frmivalenum_discrete
+diff --git a/libavcodec/Makefile b/libavcodec/Makefile
+index 4bfd477..668f06a 100644
+--- a/libavcodec/Makefile
++++ b/libavcodec/Makefile
+@@ -1022,6 +1022,7 @@ OBJS-$(CONFIG_MPEG2_D3D11VA_HWACCEL) += dxva2_mpeg2.o
+ OBJS-$(CONFIG_MPEG2_DXVA2_HWACCEL) += dxva2_mpeg2.o
+ OBJS-$(CONFIG_MPEG2_NVDEC_HWACCEL) += nvdec_mpeg12.o
+ OBJS-$(CONFIG_MPEG2_QSV_HWACCEL) += qsvdec.o
++OBJS-$(CONFIG_MPEG2_V4L2REQUEST_HWACCEL) += v4l2_request_mpeg2.o
+ OBJS-$(CONFIG_MPEG2_VAAPI_HWACCEL) += vaapi_mpeg2.o
+ OBJS-$(CONFIG_MPEG2_VDPAU_HWACCEL) += vdpau_mpeg12.o
+ OBJS-$(CONFIG_MPEG2_VIDEOTOOLBOX_HWACCEL) += videotoolbox.o
+diff --git a/libavcodec/hwaccels.h b/libavcodec/hwaccels.h
+index 5211553..f5baf8a 100644
+--- a/libavcodec/hwaccels.h
++++ b/libavcodec/hwaccels.h
+@@ -54,6 +54,7 @@ extern const struct FFHWAccel ff_mpeg2_d3d11va_hwaccel;
+ extern const struct FFHWAccel ff_mpeg2_d3d11va2_hwaccel;
+ extern const struct FFHWAccel ff_mpeg2_dxva2_hwaccel;
+ extern const struct FFHWAccel ff_mpeg2_nvdec_hwaccel;
++extern const struct FFHWAccel ff_mpeg2_v4l2request_hwaccel;
+ extern const struct FFHWAccel ff_mpeg2_vaapi_hwaccel;
+ extern const struct FFHWAccel ff_mpeg2_vdpau_hwaccel;
+ extern const struct FFHWAccel ff_mpeg2_videotoolbox_hwaccel;
+diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c
+index 5dac83e..4aa341c 100644
+--- a/libavcodec/mpeg12dec.c
++++ b/libavcodec/mpeg12dec.c
+@@ -1120,6 +1120,9 @@ static const enum AVPixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
+ #endif
+ #if CONFIG_MPEG2_VIDEOTOOLBOX_HWACCEL
+ AV_PIX_FMT_VIDEOTOOLBOX,
++#endif
++#if CONFIG_MPEG2_V4L2REQUEST_HWACCEL
++ AV_PIX_FMT_DRM_PRIME,
+ #endif
+ AV_PIX_FMT_YUV420P,
+ AV_PIX_FMT_NONE
+@@ -2896,6 +2899,9 @@ const FFCodec ff_mpeg2video_decoder = {
+ #endif
+ #if CONFIG_MPEG2_VIDEOTOOLBOX_HWACCEL
+ HWACCEL_VIDEOTOOLBOX(mpeg2),
++#endif
++#if CONFIG_MPEG2_V4L2REQUEST_HWACCEL
++ HWACCEL_V4L2REQUEST(mpeg2),
+ #endif
+ NULL
+ },
+diff --git a/libavcodec/v4l2_request_mpeg2.c b/libavcodec/v4l2_request_mpeg2.c
+new file mode 100644
+index 0000000..ae3f043
+--- /dev/null
++++ b/libavcodec/v4l2_request_mpeg2.c
+@@ -0,0 +1,160 @@
++/*
++ * This file is part of FFmpeg.
++ *
++ * FFmpeg is free software; you can redistribute it and/or
++ * modify it under the terms of the GNU Lesser General Public
++ * License as published by the Free Software Foundation; either
++ * version 2.1 of the License, or (at your option) any later version.
++ *
++ * FFmpeg is distributed in the hope that it will be useful,
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
++ * Lesser General Public License for more details.
++ *
++ * You should have received a copy of the GNU Lesser General Public
++ * License along with FFmpeg; if not, write to the Free Software
++ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
++ */
++
++#include "hwaccel_internal.h"
++#include "hwconfig.h"
++#include "mpegvideo.h"
++#include "v4l2_request.h"
++
++typedef struct V4L2RequestControlsMPEG2 {
++ struct v4l2_ctrl_mpeg2_sequence sequence;
++ struct v4l2_ctrl_mpeg2_picture picture;
++ struct v4l2_ctrl_mpeg2_quantisation quantisation;
++} V4L2RequestControlsMPEG2;
++
++static int v4l2_request_mpeg2_start_frame(AVCodecContext *avctx,
++ av_unused const uint8_t *buffer,
++ av_unused uint32_t size)
++{
++ const MpegEncContext *s = avctx->priv_data;
++ V4L2RequestControlsMPEG2 *controls = s->current_picture_ptr->hwaccel_picture_private;
++ V4L2RequestDescriptor *req = (V4L2RequestDescriptor*)s->current_picture_ptr->f->data[0];
++
++ controls->sequence = (struct v4l2_ctrl_mpeg2_sequence) {
++ /* ISO/IEC 13818-2, ITU-T Rec. H.262: Sequence header */
++ .horizontal_size = s->width,
++ .vertical_size = s->height,
++ .vbv_buffer_size = req->output.size,
++
++ /* ISO/IEC 13818-2, ITU-T Rec. H.262: Sequence extension */
++ .profile_and_level_indication = 0,
++ .chroma_format = s->chroma_format,
++ };
++
++ if (s->progressive_sequence)
++ controls->sequence.flags |= V4L2_MPEG2_SEQ_FLAG_PROGRESSIVE;
++
++ controls->picture = (struct v4l2_ctrl_mpeg2_picture) {
++ /* ISO/IEC 13818-2, ITU-T Rec. H.262: Picture header */
++ .picture_coding_type = s->pict_type,
++
++ /* ISO/IEC 13818-2, ITU-T Rec. H.262: Picture coding extension */
++ .f_code[0][0] = s->mpeg_f_code[0][0],
++ .f_code[0][1] = s->mpeg_f_code[0][1],
++ .f_code[1][0] = s->mpeg_f_code[1][0],
++ .f_code[1][1] = s->mpeg_f_code[1][1],
++ .picture_structure = s->picture_structure,
++ .intra_dc_precision = s->intra_dc_precision,
++ };
++
++ if (s->top_field_first)
++ controls->picture.flags |= V4L2_MPEG2_PIC_FLAG_TOP_FIELD_FIRST;
++
++ if (s->frame_pred_frame_dct)
++ controls->picture.flags |= V4L2_MPEG2_PIC_FLAG_FRAME_PRED_DCT;
++
++ if (s->concealment_motion_vectors)
++ controls->picture.flags |= V4L2_MPEG2_PIC_FLAG_CONCEALMENT_MV;
++
++ if (s->intra_vlc_format)
++ controls->picture.flags |= V4L2_MPEG2_PIC_FLAG_INTRA_VLC;
++
++ if (s->q_scale_type)
++ controls->picture.flags |= V4L2_MPEG2_PIC_FLAG_Q_SCALE_TYPE;
++
++ if (s->alternate_scan)
++ controls->picture.flags |= V4L2_MPEG2_PIC_FLAG_ALT_SCAN;
++
++ if (s->repeat_first_field)
++ controls->picture.flags |= V4L2_MPEG2_PIC_FLAG_REPEAT_FIRST;
++
++ if (s->progressive_frame)
++ controls->picture.flags |= V4L2_MPEG2_PIC_FLAG_PROGRESSIVE;
++
++ switch (s->pict_type) {
++ case AV_PICTURE_TYPE_B:
++ controls->picture.backward_ref_ts = ff_v4l2_request_get_capture_timestamp(s->next_picture.f);
++ // fall-through
++ case AV_PICTURE_TYPE_P:
++ controls->picture.forward_ref_ts = ff_v4l2_request_get_capture_timestamp(s->last_picture.f);
+ }
+
- hwctx->fd = open(device, O_RDWR);
- if (hwctx->fd < 0)
- return AVERROR(errno);
-From fa7165e391287bf970569e36b0b19bff947b084f Mon Sep 17 00:00:00 2001
-From: Jonas Karlman <jonas at kwiboo.se>
-Date: Mon, 27 Jul 2020 23:15:45 +0000
-Subject: [PATCH] HACK: define drm NV15 and NV20 format
++ for (int i = 0; i < 64; i++) {
++ int n = s->idsp.idct_permutation[ff_zigzag_direct[i]];
++ controls->quantisation.intra_quantiser_matrix[i] = s->intra_matrix[n];
++ controls->quantisation.non_intra_quantiser_matrix[i] = s->inter_matrix[n];
++ controls->quantisation.chroma_intra_quantiser_matrix[i] = s->chroma_intra_matrix[n];
++ controls->quantisation.chroma_non_intra_quantiser_matrix[i] = s->chroma_inter_matrix[n];
++ }
++
++ return ff_v4l2_request_reset_frame(avctx, s->current_picture_ptr->f);
++}
++
++static int v4l2_request_mpeg2_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
++{
++ const MpegEncContext *s = avctx->priv_data;
++
++ return ff_v4l2_request_append_output_buffer(avctx, s->current_picture_ptr->f, buffer, size);
++}
++
++static int v4l2_request_mpeg2_end_frame(AVCodecContext *avctx)
++{
++ const MpegEncContext *s = avctx->priv_data;
++ V4L2RequestControlsMPEG2 *controls = s->current_picture_ptr->hwaccel_picture_private;
++
++ struct v4l2_ext_control control[] = {
++ {
++ .id = V4L2_CID_STATELESS_MPEG2_SEQUENCE,
++ .ptr = &controls->sequence,
++ .size = sizeof(controls->sequence),
++ },
++ {
++ .id = V4L2_CID_STATELESS_MPEG2_PICTURE,
++ .ptr = &controls->picture,
++ .size = sizeof(controls->picture),
++ },
++ {
++ .id = V4L2_CID_STATELESS_MPEG2_QUANTISATION,
++ .ptr = &controls->quantisation,
++ .size = sizeof(controls->quantisation),
++ },
++ };
++
++ return ff_v4l2_request_decode_frame(avctx, s->current_picture_ptr->f, control, FF_ARRAY_ELEMS(control));
++}
++
++static int v4l2_request_mpeg2_init(AVCodecContext *avctx)
++{
++ return ff_v4l2_request_init(avctx, V4L2_PIX_FMT_MPEG2_SLICE, 1024 * 1024, NULL, 0);
++}
++
++const FFHWAccel ff_mpeg2_v4l2request_hwaccel = {
++ .p.name = "mpeg2_v4l2request",
++ .p.type = AVMEDIA_TYPE_VIDEO,
++ .p.id = AV_CODEC_ID_MPEG2VIDEO,
++ .p.pix_fmt = AV_PIX_FMT_DRM_PRIME,
++ .start_frame = v4l2_request_mpeg2_start_frame,
++ .decode_slice = v4l2_request_mpeg2_decode_slice,
++ .end_frame = v4l2_request_mpeg2_end_frame,
++ .frame_priv_data_size = sizeof(V4L2RequestControlsMPEG2),
++ .init = v4l2_request_mpeg2_init,
++ .uninit = ff_v4l2_request_uninit,
++ .priv_data_size = sizeof(V4L2RequestContext),
++ .frame_params = ff_v4l2_request_frame_params,
++ .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
++};
+--
+2.44.0
+
+From 54cf5e04d320bac0839879728d912c30787b983b Mon Sep 17 00:00:00 2001
+From: Boris Brezillon <boris.brezillon at collabora.com>
+Date: Wed, 22 May 2019 14:46:58 +0200
+Subject: [PATCH 06/18] Add V4L2 request API vp8 hwaccel
+
+Signed-off-by: Boris Brezillon <boris.brezillon at collabora.com>
+Signed-off-by: Ezequiel Garcia <ezequiel at collabora.com>
+Signed-off-by: Jonas Karlman <jonas at kwiboo.se>
---
- libavcodec/v4l2_request.c | 8 ++++++++
- 1 file changed, 8 insertions(+)
+ configure | 3 +
+ libavcodec/Makefile | 1 +
+ libavcodec/hwaccels.h | 1 +
+ libavcodec/v4l2_request_vp8.c | 181 ++++++++++++++++++++++++++++++++++
+ libavcodec/vp8.c | 6 ++
+ 5 files changed, 192 insertions(+)
+ create mode 100644 libavcodec/v4l2_request_vp8.c
-diff --git a/libavcodec/v4l2_request.c b/libavcodec/v4l2_request.c
-index 0b294feff2eb..a8f0ee79eeef 100644
---- a/libavcodec/v4l2_request.c
-+++ b/libavcodec/v4l2_request.c
-@@ -30,6 +30,14 @@
- #include "internal.h"
- #include "v4l2_request.h"
+diff --git a/configure b/configure
+index 3127877..3ea4a3f 100755
+--- a/configure
++++ b/configure
+@@ -3157,6 +3157,8 @@ vc1_vdpau_hwaccel_deps="vdpau"
+ vc1_vdpau_hwaccel_select="vc1_decoder"
+ vp8_nvdec_hwaccel_deps="nvdec"
+ vp8_nvdec_hwaccel_select="vp8_decoder"
++vp8_v4l2request_hwaccel_deps="v4l2_request vp8_v4l2_request"
++vp8_v4l2request_hwaccel_select="vp8_decoder"
+ vp8_vaapi_hwaccel_deps="vaapi"
+ vp8_vaapi_hwaccel_select="vp8_decoder"
+ vp9_d3d11va_hwaccel_deps="d3d11va DXVA_PicParams_VP9"
+@@ -7009,6 +7011,7 @@ fi
+ check_func_headers "linux/media.h linux/videodev2.h" v4l2_timeval_to_ns
+ check_cc h264_v4l2_request linux/videodev2.h "int i = V4L2_PIX_FMT_H264_SLICE;"
+ check_cc mpeg2_v4l2_request linux/videodev2.h "int i = V4L2_PIX_FMT_MPEG2_SLICE;"
++check_cc vp8_v4l2_request linux/videodev2.h "int i = V4L2_PIX_FMT_VP8_FRAME;"
-+#ifndef DRM_FORMAT_NV15
-+#define DRM_FORMAT_NV15 fourcc_code('N', 'V', '1', '5')
-+#endif
+ check_headers sys/videoio.h
+ test_code cc sys/videoio.h "struct v4l2_frmsizeenum vfse; vfse.discrete.width = 0;" && enable_sanitized struct_v4l2_frmivalenum_discrete
+diff --git a/libavcodec/Makefile b/libavcodec/Makefile
+index 668f06a..3de7182 100644
+--- a/libavcodec/Makefile
++++ b/libavcodec/Makefile
+@@ -1037,6 +1037,7 @@ OBJS-$(CONFIG_VC1_QSV_HWACCEL) += qsvdec.o
+ OBJS-$(CONFIG_VC1_VAAPI_HWACCEL) += vaapi_vc1.o
+ OBJS-$(CONFIG_VC1_VDPAU_HWACCEL) += vdpau_vc1.o
+ OBJS-$(CONFIG_VP8_NVDEC_HWACCEL) += nvdec_vp8.o
++OBJS-$(CONFIG_VP8_V4L2REQUEST_HWACCEL) += v4l2_request_vp8.o
+ OBJS-$(CONFIG_VP8_VAAPI_HWACCEL) += vaapi_vp8.o
+ OBJS-$(CONFIG_VP9_D3D11VA_HWACCEL) += dxva2_vp9.o
+ OBJS-$(CONFIG_VP9_DXVA2_HWACCEL) += dxva2_vp9.o
+diff --git a/libavcodec/hwaccels.h b/libavcodec/hwaccels.h
+index f5baf8a..a531226 100644
+--- a/libavcodec/hwaccels.h
++++ b/libavcodec/hwaccels.h
+@@ -70,6 +70,7 @@ extern const struct FFHWAccel ff_vc1_nvdec_hwaccel;
+ extern const struct FFHWAccel ff_vc1_vaapi_hwaccel;
+ extern const struct FFHWAccel ff_vc1_vdpau_hwaccel;
+ extern const struct FFHWAccel ff_vp8_nvdec_hwaccel;
++extern const struct FFHWAccel ff_vp8_v4l2request_hwaccel;
+ extern const struct FFHWAccel ff_vp8_vaapi_hwaccel;
+ extern const struct FFHWAccel ff_vp9_d3d11va_hwaccel;
+ extern const struct FFHWAccel ff_vp9_d3d11va2_hwaccel;
+diff --git a/libavcodec/v4l2_request_vp8.c b/libavcodec/v4l2_request_vp8.c
+new file mode 100644
+index 0000000..415a4c4
+--- /dev/null
++++ b/libavcodec/v4l2_request_vp8.c
+@@ -0,0 +1,181 @@
++/*
++ * This file is part of FFmpeg.
++ *
++ * FFmpeg is free software; you can redistribute it and/or
++ * modify it under the terms of the GNU Lesser General Public
++ * License as published by the Free Software Foundation; either
++ * version 2.1 of the License, or (at your option) any later version.
++ *
++ * FFmpeg is distributed in the hope that it will be useful,
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
++ * Lesser General Public License for more details.
++ *
++ * You should have received a copy of the GNU Lesser General Public
++ * License along with FFmpeg; if not, write to the Free Software
++ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
++ */
+
-+#ifndef DRM_FORMAT_NV20
-+#define DRM_FORMAT_NV20 fourcc_code('N', 'V', '2', '0')
-+#endif
++#include "hwaccel_internal.h"
++#include "hwconfig.h"
++#include "v4l2_request.h"
++#include "vp8.h"
+
- uint64_t ff_v4l2_request_get_capture_timestamp(AVFrame *frame)
- {
- V4L2RequestDescriptor *req = (V4L2RequestDescriptor*)frame->data[0];
++typedef struct V4L2RequestControlsVP8 {
++ struct v4l2_ctrl_vp8_frame ctrl;
++} V4L2RequestControlsVP8;
++
++static int v4l2_request_vp8_start_frame(AVCodecContext *avctx,
++ av_unused const uint8_t *buffer,
++ av_unused uint32_t size)
++{
++ const VP8Context *s = avctx->priv_data;
++ V4L2RequestControlsVP8 *controls = s->framep[VP8_FRAME_CURRENT]->hwaccel_picture_private;
++
++ memset(&controls->ctrl, 0, sizeof(controls->ctrl));
++ return ff_v4l2_request_reset_frame(avctx, s->framep[VP8_FRAME_CURRENT]->tf.f);
++}
++
++static int v4l2_request_vp8_end_frame(AVCodecContext *avctx)
++{
++ const VP8Context *s = avctx->priv_data;
++ V4L2RequestControlsVP8 *controls = s->framep[VP8_FRAME_CURRENT]->hwaccel_picture_private;
++ struct v4l2_ext_control control[] = {
++ {
++ .id = V4L2_CID_STATELESS_VP8_FRAME,
++ .ptr = &controls->ctrl,
++ .size = sizeof(controls->ctrl),
++ },
++ };
++
++ return ff_v4l2_request_decode_frame(avctx, s->framep[VP8_FRAME_CURRENT]->tf.f,
++ control, FF_ARRAY_ELEMS(control));
++}
++
++static int v4l2_request_vp8_decode_slice(AVCodecContext *avctx,
++ const uint8_t *buffer,
++ uint32_t size)
++{
++ const VP8Context *s = avctx->priv_data;
++ V4L2RequestControlsVP8 *controls = s->framep[VP8_FRAME_CURRENT]->hwaccel_picture_private;
++ struct v4l2_ctrl_vp8_frame *frame = &controls->ctrl;
++ const uint8_t *data = buffer + 3 + 7 * s->keyframe;
++ unsigned int i, j, k;
++
++ frame->version = s->profile & 0x3;
++ frame->width = avctx->width;
++ frame->height = avctx->height;
++ /* FIXME: set ->xx_scale */
++ frame->prob_skip_false = s->prob->mbskip;
++ frame->prob_intra = s->prob->intra;
++ frame->prob_gf = s->prob->golden;
++ frame->prob_last = s->prob->last;
++ frame->first_part_size = s->header_partition_size;
++ frame->first_part_header_bits = (8 * (s->coder_state_at_header_end.input - data) -
++ s->coder_state_at_header_end.bit_count - 8);
++ frame->num_dct_parts = s->num_coeff_partitions;
++ for (i = 0; i < 8; i++)
++ frame->dct_part_sizes[i] = s->coeff_partition_size[i];
++
++ frame->coder_state.range = s->coder_state_at_header_end.range;
++ frame->coder_state.value = s->coder_state_at_header_end.value;
++ frame->coder_state.bit_count = s->coder_state_at_header_end.bit_count;
++ if (s->framep[VP8_FRAME_PREVIOUS])
++ frame->last_frame_ts = ff_v4l2_request_get_capture_timestamp(s->framep[VP8_FRAME_PREVIOUS]->tf.f);
++ if (s->framep[VP8_FRAME_GOLDEN])
++ frame->golden_frame_ts = ff_v4l2_request_get_capture_timestamp(s->framep[VP8_FRAME_GOLDEN]->tf.f);
++ if (s->framep[VP8_FRAME_ALTREF])
++ frame->alt_frame_ts = ff_v4l2_request_get_capture_timestamp(s->framep[VP8_FRAME_ALTREF]->tf.f);
++ frame->flags |= s->invisible ? 0 : V4L2_VP8_FRAME_FLAG_SHOW_FRAME;
++ frame->flags |= s->mbskip_enabled ? V4L2_VP8_FRAME_FLAG_MB_NO_SKIP_COEFF : 0;
++ frame->flags |= (s->profile & 0x4) ? V4L2_VP8_FRAME_FLAG_EXPERIMENTAL : 0;
++ frame->flags |= s->keyframe ? V4L2_VP8_FRAME_FLAG_KEY_FRAME : 0;
++ frame->flags |= s->sign_bias[VP8_FRAME_GOLDEN] ? V4L2_VP8_FRAME_FLAG_SIGN_BIAS_GOLDEN : 0;
++ frame->flags |= s->sign_bias[VP8_FRAME_ALTREF] ? V4L2_VP8_FRAME_FLAG_SIGN_BIAS_ALT : 0;
++ frame->segment.flags |= s->segmentation.enabled ? V4L2_VP8_SEGMENT_FLAG_ENABLED : 0;
++ frame->segment.flags |= s->segmentation.update_map ? V4L2_VP8_SEGMENT_FLAG_UPDATE_MAP : 0;
++ frame->segment.flags |= s->segmentation.update_feature_data ? V4L2_VP8_SEGMENT_FLAG_UPDATE_FEATURE_DATA : 0;
++ frame->segment.flags |= s->segmentation.absolute_vals ? 0 : V4L2_VP8_SEGMENT_FLAG_DELTA_VALUE_MODE;
++ for (i = 0; i < 4; i++) {
++ frame->segment.quant_update[i] = s->segmentation.base_quant[i];
++ frame->segment.lf_update[i] = s->segmentation.filter_level[i];
++ }
++
++ for (i = 0; i < 3; i++)
++ frame->segment.segment_probs[i] = s->prob->segmentid[i];
++
++ frame->lf.level = s->filter.level;
++ frame->lf.sharpness_level = s->filter.sharpness;
++ frame->lf.flags |= s->lf_delta.enabled ? V4L2_VP8_LF_ADJ_ENABLE : 0;
++ frame->lf.flags |= s->lf_delta.update ? V4L2_VP8_LF_DELTA_UPDATE : 0;
++ frame->lf.flags |= s->filter.simple ? V4L2_VP8_LF_FILTER_TYPE_SIMPLE : 0;
++ for (i = 0; i < 4; i++) {
++ frame->lf.ref_frm_delta[i] = s->lf_delta.ref[i];
++ frame->lf.mb_mode_delta[i] = s->lf_delta.mode[i + MODE_I4x4];
++ }
++
++ // Probabilites
++ if (s->keyframe) {
++ static const uint8_t keyframe_y_mode_probs[4] = {
++ 145, 156, 163, 128
++ };
++ static const uint8_t keyframe_uv_mode_probs[3] = {
++ 142, 114, 183
++ };
++
++ memcpy(frame->entropy.y_mode_probs, keyframe_y_mode_probs, 4);
++ memcpy(frame->entropy.uv_mode_probs, keyframe_uv_mode_probs, 3);
++ } else {
++ for (i = 0; i < 4; i++)
++ frame->entropy.y_mode_probs[i] = s->prob->pred16x16[i];
++ for (i = 0; i < 3; i++)
++ frame->entropy.uv_mode_probs[i] = s->prob->pred8x8c[i];
++ }
++ for (i = 0; i < 2; i++)
++ for (j = 0; j < 19; j++)
++ frame->entropy.mv_probs[i][j] = s->prob->mvc[i][j];
++
++ for (i = 0; i < 4; i++) {
++ for (j = 0; j < 8; j++) {
++ static const int coeff_bands_inverse[8] = {
++ 0, 1, 2, 3, 5, 6, 4, 15
++ };
++ int coeff_pos = coeff_bands_inverse[j];
++
++ for (k = 0; k < 3; k++) {
++ memcpy(frame->entropy.coeff_probs[i][j][k],
++ s->prob->token[i][coeff_pos][k], 11);
++ }
++ }
++ }
++
++ frame->quant.y_ac_qi = s->quant.yac_qi;
++ frame->quant.y_dc_delta = s->quant.ydc_delta;
++ frame->quant.y2_dc_delta = s->quant.y2dc_delta;
++ frame->quant.y2_ac_delta = s->quant.y2ac_delta;
++ frame->quant.uv_dc_delta = s->quant.uvdc_delta;
++ frame->quant.uv_ac_delta = s->quant.uvac_delta;
++
++ return ff_v4l2_request_append_output_buffer(avctx, s->framep[VP8_FRAME_CURRENT]->tf.f, buffer, size);
++}
++
++static int v4l2_request_vp8_init(AVCodecContext *avctx)
++{
++ return ff_v4l2_request_init(avctx, V4L2_PIX_FMT_VP8_FRAME, 2 * 1024 * 1024, NULL, 0);
++}
++
++const FFHWAccel ff_vp8_v4l2request_hwaccel = {
++ .p.name = "vp8_v4l2request",
++ .p.type = AVMEDIA_TYPE_VIDEO,
++ .p.id = AV_CODEC_ID_VP8,
++ .p.pix_fmt = AV_PIX_FMT_DRM_PRIME,
++ .start_frame = v4l2_request_vp8_start_frame,
++ .decode_slice = v4l2_request_vp8_decode_slice,
++ .end_frame = v4l2_request_vp8_end_frame,
++ .frame_priv_data_size = sizeof(V4L2RequestControlsVP8),
++ .init = v4l2_request_vp8_init,
++ .uninit = ff_v4l2_request_uninit,
++ .priv_data_size = sizeof(V4L2RequestContext),
++ .frame_params = ff_v4l2_request_frame_params,
++ .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
++};
+diff --git a/libavcodec/vp8.c b/libavcodec/vp8.c
+index ffc430d..eb619c9 100644
+--- a/libavcodec/vp8.c
++++ b/libavcodec/vp8.c
+@@ -197,6 +197,9 @@ static enum AVPixelFormat get_pixel_format(VP8Context *s)
+ #endif
+ #if CONFIG_VP8_NVDEC_HWACCEL
+ AV_PIX_FMT_CUDA,
++#endif
++#if CONFIG_VP8_V4L2REQUEST_HWACCEL
++ AV_PIX_FMT_DRM_PRIME,
+ #endif
+ AV_PIX_FMT_YUV420P,
+ AV_PIX_FMT_NONE,
+@@ -3002,6 +3005,9 @@ const FFCodec ff_vp8_decoder = {
+ #endif
+ #if CONFIG_VP8_NVDEC_HWACCEL
+ HWACCEL_NVDEC(vp8),
++#endif
++#if CONFIG_VP8_V4L2REQUEST_HWACCEL
++ HWACCEL_V4L2REQUEST(vp8),
+ #endif
+ NULL
+ },
+--
+2.44.0
+
+
+From 8f67d3099fcb63251f3fe718bc464417a053eb8e Mon Sep 17 00:00:00 2001
+From: Jernej Skrabec <jernej.skrabec at siol.net>
+Date: Sat, 15 Dec 2018 22:32:16 +0100
+Subject: [PATCH 07/18] Add V4L2 request API hevc hwaccel
+
+Signed-off-by: Jernej Skrabec <jernej.skrabec at siol.net>
+Signed-off-by: Jonas Karlman <jonas at kwiboo.se>
+Signed-off-by: Benjamin Gaignard <benjamin.gaignard at collabora.com>
+Signed-off-by: Alex Bee <knaerzche at gmail.com>
+---
+ configure | 3 +
+ libavcodec/Makefile | 1 +
+ libavcodec/hevcdec.c | 10 +
+ libavcodec/hwaccels.h | 1 +
+ libavcodec/v4l2_request_hevc.c | 682 +++++++++++++++++++++++++++++++++
+ 5 files changed, 697 insertions(+)
+ create mode 100644 libavcodec/v4l2_request_hevc.c
+
+diff --git a/configure b/configure
+index 3ea4a3f..ad9af0b 100755
+--- a/configure
++++ b/configure
+@@ -3099,6 +3099,8 @@ hevc_dxva2_hwaccel_deps="dxva2 DXVA_PicParams_HEVC"
+ hevc_dxva2_hwaccel_select="hevc_decoder"
+ hevc_nvdec_hwaccel_deps="nvdec"
+ hevc_nvdec_hwaccel_select="hevc_decoder"
++hevc_v4l2request_hwaccel_deps="v4l2_request hevc_v4l2_request"
++hevc_v4l2request_hwaccel_select="hevc_decoder"
+ hevc_vaapi_hwaccel_deps="vaapi VAPictureParameterBufferHEVC"
+ hevc_vaapi_hwaccel_select="hevc_decoder"
+ hevc_vdpau_hwaccel_deps="vdpau VdpPictureInfoHEVC"
+@@ -7010,6 +7012,7 @@ fi
+
+ check_func_headers "linux/media.h linux/videodev2.h" v4l2_timeval_to_ns
+ check_cc h264_v4l2_request linux/videodev2.h "int i = V4L2_PIX_FMT_H264_SLICE;"
++check_cc hevc_v4l2_request linux/videodev2.h "int i = V4L2_PIX_FMT_HEVC_SLICE;"
+ check_cc mpeg2_v4l2_request linux/videodev2.h "int i = V4L2_PIX_FMT_MPEG2_SLICE;"
+ check_cc vp8_v4l2_request linux/videodev2.h "int i = V4L2_PIX_FMT_VP8_FRAME;"
+
+diff --git a/libavcodec/Makefile b/libavcodec/Makefile
+index 3de7182..e32bd18 100644
+--- a/libavcodec/Makefile
++++ b/libavcodec/Makefile
+@@ -1010,6 +1010,7 @@ OBJS-$(CONFIG_HEVC_D3D11VA_HWACCEL) += dxva2_hevc.o
+ OBJS-$(CONFIG_HEVC_DXVA2_HWACCEL) += dxva2_hevc.o
+ OBJS-$(CONFIG_HEVC_NVDEC_HWACCEL) += nvdec_hevc.o
+ OBJS-$(CONFIG_HEVC_QSV_HWACCEL) += qsvdec.o
++OBJS-$(CONFIG_HEVC_V4L2REQUEST_HWACCEL) += v4l2_request_hevc.o
+ OBJS-$(CONFIG_HEVC_VAAPI_HWACCEL) += vaapi_hevc.o h265_profile_level.o
+ OBJS-$(CONFIG_HEVC_VDPAU_HWACCEL) += vdpau_hevc.o h265_profile_level.o
+ OBJS-$(CONFIG_HEVC_VULKAN_HWACCEL) += vulkan_decode.o vulkan_hevc.o
+diff --git a/libavcodec/hevcdec.c b/libavcodec/hevcdec.c
+index b267f6e..2086ef3 100644
+--- a/libavcodec/hevcdec.c
++++ b/libavcodec/hevcdec.c
+@@ -401,6 +401,7 @@ static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
+ #define HWACCEL_MAX (CONFIG_HEVC_DXVA2_HWACCEL + \
+ CONFIG_HEVC_D3D11VA_HWACCEL * 2 + \
+ CONFIG_HEVC_NVDEC_HWACCEL + \
++ CONFIG_HEVC_V4L2REQUEST_HWACCEL + \
+ CONFIG_HEVC_VAAPI_HWACCEL + \
+ CONFIG_HEVC_VIDEOTOOLBOX_HWACCEL + \
+ CONFIG_HEVC_VDPAU_HWACCEL + \
+@@ -431,6 +432,9 @@ static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
+ #endif
+ #if CONFIG_HEVC_VULKAN_HWACCEL
+ *fmt++ = AV_PIX_FMT_VULKAN;
++#endif
++#if CONFIG_HEVC_V4L2REQUEST_HWACCEL
++ *fmt++ = AV_PIX_FMT_DRM_PRIME;
+ #endif
+ break;
+ case AV_PIX_FMT_YUV420P10:
+@@ -455,6 +459,9 @@ static enum AVPixelFormat get_format(HEVCContext *s, const HEVCSPS *sps)
+ #endif
+ #if CONFIG_HEVC_NVDEC_HWACCEL
+ *fmt++ = AV_PIX_FMT_CUDA;
++#endif
++#if CONFIG_HEVC_V4L2REQUEST_HWACCEL
++ *fmt++ = AV_PIX_FMT_DRM_PRIME;
+ #endif
+ break;
+ case AV_PIX_FMT_YUV444P:
+@@ -3728,6 +3735,9 @@ const FFCodec ff_hevc_decoder = {
+ #endif
+ #if CONFIG_HEVC_VULKAN_HWACCEL
+ HWACCEL_VULKAN(hevc),
++#endif
++#if CONFIG_HEVC_V4L2REQUEST_HWACCEL
++ HWACCEL_V4L2REQUEST(hevc),
+ #endif
+ NULL
+ },
+diff --git a/libavcodec/hwaccels.h b/libavcodec/hwaccels.h
+index a531226..4a0de92 100644
+--- a/libavcodec/hwaccels.h
++++ b/libavcodec/hwaccels.h
+@@ -41,6 +41,7 @@ extern const struct FFHWAccel ff_hevc_d3d11va_hwaccel;
+ extern const struct FFHWAccel ff_hevc_d3d11va2_hwaccel;
+ extern const struct FFHWAccel ff_hevc_dxva2_hwaccel;
+ extern const struct FFHWAccel ff_hevc_nvdec_hwaccel;
++extern const struct FFHWAccel ff_hevc_v4l2request_hwaccel;
+ extern const struct FFHWAccel ff_hevc_vaapi_hwaccel;
+ extern const struct FFHWAccel ff_hevc_vdpau_hwaccel;
+ extern const struct FFHWAccel ff_hevc_videotoolbox_hwaccel;
+diff --git a/libavcodec/v4l2_request_hevc.c b/libavcodec/v4l2_request_hevc.c
+new file mode 100644
+index 0000000..2c3537d
+--- /dev/null
++++ b/libavcodec/v4l2_request_hevc.c
+@@ -0,0 +1,682 @@
++/*
++ * This file is part of FFmpeg.
++ *
++ * FFmpeg is free software; you can redistribute it and/or
++ * modify it under the terms of the GNU Lesser General Public
++ * License as published by the Free Software Foundation; either
++ * version 2.1 of the License, or (at your option) any later version.
++ *
++ * FFmpeg is distributed in the hope that it will be useful,
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
++ * Lesser General Public License for more details.
++ *
++ * You should have received a copy of the GNU Lesser General Public
++ * License along with FFmpeg; if not, write to the Free Software
++ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
++ */
++
++#include "hevcdec.h"
++#include "hwaccel_internal.h"
++#include "hwconfig.h"
++#include "internal.h"
++#include "v4l2_request.h"
++
++#define MAX_SLICES 600 // as per HEVC spec ?
++#define V4L2_HEVC_CONTROLS_MAX 6
++
++typedef struct V4L2RequestControlsHEVC {
++ struct v4l2_ctrl_hevc_sps sps;
++ struct v4l2_ctrl_hevc_pps pps;
++ struct v4l2_ctrl_hevc_decode_params dec_params;
++ struct v4l2_ctrl_hevc_scaling_matrix scaling_matrix;
++ struct v4l2_ctrl_hevc_slice_params slice_params[MAX_SLICES];
++ __u32 *entry_point_offsets;
++ unsigned int num_entry_point_offsets;
++ int first_slice;
++ int num_slices;
++} V4L2RequestControlsHEVC;
++
++typedef struct V4L2RequestContextHEVC {
++ V4L2RequestContext base;
++ unsigned int decode_mode;
++ unsigned int start_code;
++ __u32 max_slices;
++ unsigned int supports_entry_point_offsets;
++ unsigned int supports_slices;
++ unsigned int supports_scaling_matrix;
++} V4L2RequestContextHEVC;
++
++static uint8_t nalu_slice_start_code[] = { 0x00, 0x00, 0x01 };
++
++static void v4l2_request_hevc_fill_pred_table(const HEVCContext *h, struct v4l2_hevc_pred_weight_table *table)
++{
++ int32_t luma_weight_denom, chroma_weight_denom;
++ const SliceHeader *sh = &h->sh;
++
++ if (sh->slice_type == HEVC_SLICE_I ||
++ (sh->slice_type == HEVC_SLICE_P && !h->ps.pps->weighted_pred_flag) ||
++ (sh->slice_type == HEVC_SLICE_B && !h->ps.pps->weighted_bipred_flag))
++ return;
++
++ table->luma_log2_weight_denom = sh->luma_log2_weight_denom;
++
++ if (h->ps.sps->chroma_format_idc)
++ table->delta_chroma_log2_weight_denom = sh->chroma_log2_weight_denom - sh->luma_log2_weight_denom;
++
++ luma_weight_denom = (1 << sh->luma_log2_weight_denom);
++ chroma_weight_denom = (1 << sh->chroma_log2_weight_denom);
++
++ for (int i = 0; i < 15 && i < sh->nb_refs[L0]; i++) {
++ table->delta_luma_weight_l0[i] = sh->luma_weight_l0[i] - luma_weight_denom;
++ table->luma_offset_l0[i] = sh->luma_offset_l0[i];
++ table->delta_chroma_weight_l0[i][0] = sh->chroma_weight_l0[i][0] - chroma_weight_denom;
++ table->delta_chroma_weight_l0[i][1] = sh->chroma_weight_l0[i][1] - chroma_weight_denom;
++ table->chroma_offset_l0[i][0] = sh->chroma_offset_l0[i][0];
++ table->chroma_offset_l0[i][1] = sh->chroma_offset_l0[i][1];
++ }
++
++ if (sh->slice_type != HEVC_SLICE_B)
++ return;
++
++ for (int i = 0; i < 15 && i < sh->nb_refs[L1]; i++) {
++ table->delta_luma_weight_l1[i] = sh->luma_weight_l1[i] - luma_weight_denom;
++ table->luma_offset_l1[i] = sh->luma_offset_l1[i];
++ table->delta_chroma_weight_l1[i][0] = sh->chroma_weight_l1[i][0] - chroma_weight_denom;
++ table->delta_chroma_weight_l1[i][1] = sh->chroma_weight_l1[i][1] - chroma_weight_denom;
++ table->chroma_offset_l1[i][0] = sh->chroma_offset_l1[i][0];
++ table->chroma_offset_l1[i][1] = sh->chroma_offset_l1[i][1];
++ }
++}
++
++static uint8_t get_ref_pic_index(const HEVCContext *h, const HEVCFrame *frame,
++ struct v4l2_ctrl_hevc_decode_params *dec_params)
++{
++ uint64_t timestamp;
++
++ if (!frame)
++ return 0;
++
++ timestamp = ff_v4l2_request_get_capture_timestamp(frame->frame);
++
++ for (uint8_t i = 0; i < dec_params->num_active_dpb_entries; i++) {
++ struct v4l2_hevc_dpb_entry *entry = &dec_params->dpb[i];
++ if (entry->timestamp == timestamp)
++ return i;
++ }
++
++ return 0;
++}
++
++static void fill_dec_params(struct v4l2_ctrl_hevc_decode_params *dec_params, const HEVCContext *h)
++{
++ const HEVCFrame *pic = h->ref;
++ const SliceHeader *sh = &h->sh;
++ int i, entries = 0;
++
++ *dec_params = (struct v4l2_ctrl_hevc_decode_params) {
++ .pic_order_cnt_val = pic->poc, /* FIXME: is this same as slice_params->slice_pic_order_cnt ? */
++ .short_term_ref_pic_set_size = sh->short_term_ref_pic_set_size,
++ .long_term_ref_pic_set_size = sh->long_term_ref_pic_set_size,
++ .num_poc_st_curr_before = h->rps[ST_CURR_BEF].nb_refs,
++ .num_poc_st_curr_after = h->rps[ST_CURR_AFT].nb_refs,
++ .num_poc_lt_curr = h->rps[LT_CURR].nb_refs,
++ };
++
++ for (i = 0; i < FF_ARRAY_ELEMS(h->DPB); i++) {
++ const HEVCFrame *frame = &h->DPB[i];
++ if (frame != pic && (frame->flags & (HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF))) {
++ struct v4l2_hevc_dpb_entry *entry = &dec_params->dpb[entries++];
++
++ entry->timestamp = ff_v4l2_request_get_capture_timestamp(frame->frame);
++ entry->field_pic = !!(frame->frame->flags & AV_FRAME_FLAG_INTERLACED);
++ entry->flags = 0;
++ if (frame->flags & HEVC_FRAME_FLAG_LONG_REF)
++ entry->flags |= V4L2_HEVC_DPB_ENTRY_LONG_TERM_REFERENCE;
++
++ entry->pic_order_cnt_val = frame->poc;
++ }
++ }
++
++ dec_params->num_active_dpb_entries = entries;
++
++ if (IS_IRAP(h))
++ dec_params->flags |= V4L2_HEVC_DECODE_PARAM_FLAG_IRAP_PIC;
++
++ if (IS_IDR(h))
++ dec_params->flags |= V4L2_HEVC_DECODE_PARAM_FLAG_IDR_PIC;
++
++ if (sh->no_output_of_prior_pics_flag)
++ dec_params->flags |= V4L2_HEVC_DECODE_PARAM_FLAG_NO_OUTPUT_OF_PRIOR;
++
++ for (i = 0; i < V4L2_HEVC_DPB_ENTRIES_NUM_MAX; i++) {
++ dec_params->poc_st_curr_before[i] = get_ref_pic_index(h, h->rps[ST_CURR_BEF].ref[i], dec_params);
++ dec_params->poc_st_curr_after[i] = get_ref_pic_index(h, h->rps[ST_CURR_AFT].ref[i], dec_params);
++ dec_params->poc_lt_curr[i] = get_ref_pic_index(h, h->rps[LT_CURR].ref[i], dec_params);
++ }
++}
++
++static int v4l2_request_hevc_fill_slice_params(const HEVCContext *h,
++ V4L2RequestControlsHEVC *controls,
++ int slice)
++{
++ struct v4l2_ctrl_hevc_slice_params *slice_params = &controls->slice_params[slice];
++ struct v4l2_ctrl_hevc_decode_params *dec_params = &controls->dec_params;
++ const HEVCFrame *pic = h->ref;
++ const SliceHeader *sh = &h->sh;
++ RefPicList *rpl;
++ int i;
++
++ *slice_params = (struct v4l2_ctrl_hevc_slice_params) {
++ .bit_size = 0,
++ .data_byte_offset = (get_bits_count(&h->HEVClc->gb) + 1 + 7) / 8,
++ .num_entry_point_offsets = sh->num_entry_point_offsets,
++
++ /* ISO/IEC 23008-2, ITU-T Rec. H.265: NAL unit header */
++ .nal_unit_type = h->nal_unit_type,
++ .nuh_temporal_id_plus1 = h->temporal_id + 1,
++
++ /* ISO/IEC 23008-2, ITU-T Rec. H.265: General slice segment header */
++ .slice_type = sh->slice_type,
++ .colour_plane_id = sh->colour_plane_id,
++ .slice_pic_order_cnt = pic->poc,
++ .num_ref_idx_l0_active_minus1 = sh->nb_refs[L0] ? sh->nb_refs[L0] - 1 : 0,
++ .num_ref_idx_l1_active_minus1 = sh->nb_refs[L1] ? sh->nb_refs[L1] - 1 : 0,
++ .collocated_ref_idx = sh->slice_temporal_mvp_enabled_flag ? sh->collocated_ref_idx : 0,
++ .five_minus_max_num_merge_cand = sh->slice_type == HEVC_SLICE_I ? 0 : 5 - sh->max_num_merge_cand,
++ .slice_qp_delta = sh->slice_qp_delta,
++ .slice_cb_qp_offset = sh->slice_cb_qp_offset,
++ .slice_cr_qp_offset = sh->slice_cr_qp_offset,
++ .slice_act_y_qp_offset = 0,
++ .slice_act_cb_qp_offset = 0,
++ .slice_act_cr_qp_offset = 0,
++ .slice_beta_offset_div2 = sh->beta_offset / 2,
++ .slice_tc_offset_div2 = sh->tc_offset / 2,
++
++ /* ISO/IEC 23008-2, ITU-T Rec. H.265: Picture timing SEI message */
++ .pic_struct = h->sei.picture_timing.picture_struct,
++
++ /* ISO/IEC 23008-2, ITU-T Rec. H.265: General slice segment header */
++ .slice_segment_addr = sh->slice_segment_addr,
++ .short_term_ref_pic_set_size = sh->short_term_ref_pic_set_size,
++ .long_term_ref_pic_set_size = sh->long_term_ref_pic_set_size,
++ };
++
++ if (sh->slice_sample_adaptive_offset_flag[0])
++ slice_params->flags |= V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_SAO_LUMA;
++
++ if (sh->slice_sample_adaptive_offset_flag[1])
++ slice_params->flags |= V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_SAO_CHROMA;
++
++ if (sh->slice_temporal_mvp_enabled_flag)
++ slice_params->flags |= V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_TEMPORAL_MVP_ENABLED;
++
++ if (sh->mvd_l1_zero_flag)
++ slice_params->flags |= V4L2_HEVC_SLICE_PARAMS_FLAG_MVD_L1_ZERO;
++
++ if (sh->cabac_init_flag)
++ slice_params->flags |= V4L2_HEVC_SLICE_PARAMS_FLAG_CABAC_INIT;
++
++ if (sh->collocated_list == L0)
++ slice_params->flags |= V4L2_HEVC_SLICE_PARAMS_FLAG_COLLOCATED_FROM_L0;
++
++ /* TODO: V4L2_HEVC_SLICE_PARAMS_FLAG_USE_INTEGER_MV */
++
++ if (sh->disable_deblocking_filter_flag)
++ slice_params->flags |= V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_DEBLOCKING_FILTER_DISABLED;
++
++ if (sh->slice_loop_filter_across_slices_enabled_flag)
++ slice_params->flags |= V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_LOOP_FILTER_ACROSS_SLICES_ENABLED;
++
++ if (sh->dependent_slice_segment_flag)
++ slice_params->flags |= V4L2_HEVC_SLICE_PARAMS_FLAG_DEPENDENT_SLICE_SEGMENT;
++
++ if (sh->slice_type != HEVC_SLICE_I) {
++ rpl = &h->ref->refPicList[0];
++ for (i = 0; i < rpl->nb_refs; i++)
++ slice_params->ref_idx_l0[i] = get_ref_pic_index(h, rpl->ref[i], dec_params);
++ }
++
++ if (sh->slice_type == HEVC_SLICE_B) {
++ rpl = &h->ref->refPicList[1];
++ for (i = 0; i < rpl->nb_refs; i++)
++ slice_params->ref_idx_l1[i] = get_ref_pic_index(h, rpl->ref[i], dec_params);
++ }
++
++ v4l2_request_hevc_fill_pred_table(h, &slice_params->pred_weight_table);
++
++ if (controls->num_entry_point_offsets < sh->num_entry_point_offsets) {
++ av_freep(&controls->entry_point_offsets);
++ controls->entry_point_offsets = av_mallocz(sizeof(*controls->entry_point_offsets) * sh->num_entry_point_offsets);
++ if (!controls->entry_point_offsets)
++ return AVERROR(ENOMEM);
++ controls->num_entry_point_offsets = sh->num_entry_point_offsets;
++ }
++
++ for (i = 0; i < sh->num_entry_point_offsets; i++)
++ controls->entry_point_offsets[i] = sh->entry_point_offset[i];
++
++ return 0;
++}
++
++static void fill_sps(struct v4l2_ctrl_hevc_sps *ctrl, const HEVCContext *h)
++{
++ const HEVCSPS *sps = h->ps.sps;
++ const HEVCPPS *pps = h->ps.pps;
++
++ /* ISO/IEC 23008-2, ITU-T Rec. H.265: Sequence parameter set */
++ *ctrl = (struct v4l2_ctrl_hevc_sps) {
++ .video_parameter_set_id = sps->vps_id,
++ .seq_parameter_set_id = pps->sps_id,
++ .pic_width_in_luma_samples = sps->width,
++ .pic_height_in_luma_samples = sps->height,
++ .bit_depth_luma_minus8 = sps->bit_depth - 8,
++ .bit_depth_chroma_minus8 = sps->bit_depth_chroma - 8,
++ .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_poc_lsb - 4,
++ .sps_max_dec_pic_buffering_minus1 = sps->temporal_layer[sps->max_sub_layers - 1].max_dec_pic_buffering - 1,
++ .sps_max_num_reorder_pics = sps->temporal_layer[sps->max_sub_layers - 1].num_reorder_pics,
++ .sps_max_latency_increase_plus1 = sps->temporal_layer[sps->max_sub_layers - 1].max_latency_increase + 1,
++ .log2_min_luma_coding_block_size_minus3 = sps->log2_min_cb_size - 3,
++ .log2_diff_max_min_luma_coding_block_size = sps->log2_diff_max_min_coding_block_size,
++ .log2_min_luma_transform_block_size_minus2 = sps->log2_min_tb_size - 2,
++ .log2_diff_max_min_luma_transform_block_size = sps->log2_max_trafo_size - sps->log2_min_tb_size,
++ .max_transform_hierarchy_depth_inter = sps->max_transform_hierarchy_depth_inter,
++ .max_transform_hierarchy_depth_intra = sps->max_transform_hierarchy_depth_intra,
++ .pcm_sample_bit_depth_luma_minus1 = sps->pcm.bit_depth - 1,
++ .pcm_sample_bit_depth_chroma_minus1 = sps->pcm.bit_depth_chroma - 1,
++ .log2_min_pcm_luma_coding_block_size_minus3 = sps->pcm.log2_min_pcm_cb_size - 3,
++ .log2_diff_max_min_pcm_luma_coding_block_size = sps->pcm.log2_max_pcm_cb_size - sps->pcm.log2_min_pcm_cb_size,
++ .num_short_term_ref_pic_sets = sps->nb_st_rps,
++ .num_long_term_ref_pics_sps = sps->num_long_term_ref_pics_sps,
++ .chroma_format_idc = sps->chroma_format_idc,
++ .sps_max_sub_layers_minus1 = sps->max_sub_layers - 1,
++ };
++
++ if (sps->separate_colour_plane_flag)
++ ctrl->flags |= V4L2_HEVC_SPS_FLAG_SEPARATE_COLOUR_PLANE;
++
++ if (sps->scaling_list_enable_flag)
++ ctrl->flags |= V4L2_HEVC_SPS_FLAG_SCALING_LIST_ENABLED;
++
++ if (sps->amp_enabled_flag)
++ ctrl->flags |= V4L2_HEVC_SPS_FLAG_AMP_ENABLED;
++
++ if (sps->sao_enabled)
++ ctrl->flags |= V4L2_HEVC_SPS_FLAG_SAMPLE_ADAPTIVE_OFFSET;
++
++ if (sps->pcm_enabled_flag)
++ ctrl->flags |= V4L2_HEVC_SPS_FLAG_PCM_ENABLED;
++
++ if (sps->pcm.loop_filter_disable_flag)
++ ctrl->flags |= V4L2_HEVC_SPS_FLAG_PCM_LOOP_FILTER_DISABLED;
++
++ if (sps->long_term_ref_pics_present_flag)
++ ctrl->flags |= V4L2_HEVC_SPS_FLAG_LONG_TERM_REF_PICS_PRESENT;
++
++ if (sps->sps_temporal_mvp_enabled_flag)
++ ctrl->flags |= V4L2_HEVC_SPS_FLAG_SPS_TEMPORAL_MVP_ENABLED;
++
++ if (sps->sps_strong_intra_smoothing_enable_flag)
++ ctrl->flags |= V4L2_HEVC_SPS_FLAG_STRONG_INTRA_SMOOTHING_ENABLED;
++}
++
++static int v4l2_request_hevc_start_frame(AVCodecContext *avctx,
++ av_unused const uint8_t *buffer,
++ av_unused uint32_t size)
++{
++ const HEVCContext *h = avctx->priv_data;
++ const HEVCPPS *pps = h->ps.pps;
++ const HEVCSPS *sps = h->ps.sps;
++ const ScalingList *sl = pps->scaling_list_data_present_flag ?
++ &pps->scaling_list :
++ sps->scaling_list_enable_flag ?
++ &sps->scaling_list : NULL;
++ V4L2RequestControlsHEVC *controls = h->ref->hwaccel_picture_private;
++ const SliceHeader *sh = &h->sh;
++
++ fill_sps(&controls->sps, h);
++ fill_dec_params(&controls->dec_params, h);
++
++ if (sl) {
++ for (int i = 0; i < 6; i++) {
++ for (int j = 0; j < 16; j++)
++ controls->scaling_matrix.scaling_list_4x4[i][j] = sl->sl[0][i][j];
++ for (int j = 0; j < 64; j++) {
++ controls->scaling_matrix.scaling_list_8x8[i][j] = sl->sl[1][i][j];
++ controls->scaling_matrix.scaling_list_16x16[i][j] = sl->sl[2][i][j];
++ if (i < 2)
++ controls->scaling_matrix.scaling_list_32x32[i][j] = sl->sl[3][i * 3][j];
++ }
++ controls->scaling_matrix.scaling_list_dc_coef_16x16[i] = sl->sl_dc[0][i];
++ if (i < 2)
++ controls->scaling_matrix.scaling_list_dc_coef_32x32[i] = sl->sl_dc[1][i * 3];
++ }
++ }
++
++ /* ISO/IEC 23008-2, ITU-T Rec. H.265: Picture parameter set */
++ controls->pps = (struct v4l2_ctrl_hevc_pps) {
++ .pic_parameter_set_id = sh->pps_id,
++ .num_extra_slice_header_bits = pps->num_extra_slice_header_bits,
++ .num_ref_idx_l0_default_active_minus1 = pps->num_ref_idx_l0_default_active - 1,
++ .num_ref_idx_l1_default_active_minus1 = pps->num_ref_idx_l1_default_active - 1,
++ .init_qp_minus26 = pps->pic_init_qp_minus26,
++ .diff_cu_qp_delta_depth = pps->diff_cu_qp_delta_depth,
++ .pps_cb_qp_offset = pps->cb_qp_offset,
++ .pps_cr_qp_offset = pps->cr_qp_offset,
++ .pps_beta_offset_div2 = pps->beta_offset / 2,
++ .pps_tc_offset_div2 = pps->tc_offset / 2,
++ .log2_parallel_merge_level_minus2 = pps->log2_parallel_merge_level - 2,
++ };
++
++ if (pps->dependent_slice_segments_enabled_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_DEPENDENT_SLICE_SEGMENT_ENABLED;
++
++ if (pps->output_flag_present_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_OUTPUT_FLAG_PRESENT;
++
++ if (pps->sign_data_hiding_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_SIGN_DATA_HIDING_ENABLED;
++
++ if (pps->cabac_init_present_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_CABAC_INIT_PRESENT;
++
++ if (pps->constrained_intra_pred_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_CONSTRAINED_INTRA_PRED;
++
++ if (pps->transform_skip_enabled_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_TRANSFORM_SKIP_ENABLED;
++
++ if (pps->cu_qp_delta_enabled_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_CU_QP_DELTA_ENABLED;
++
++ if (pps->pic_slice_level_chroma_qp_offsets_present_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_PPS_SLICE_CHROMA_QP_OFFSETS_PRESENT;
++
++ if (pps->weighted_pred_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_WEIGHTED_PRED;
++
++ if (pps->weighted_bipred_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_WEIGHTED_BIPRED;
++
++ if (pps->transquant_bypass_enable_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_TRANSQUANT_BYPASS_ENABLED;
++
++ if (pps->tiles_enabled_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_TILES_ENABLED;
++
++ if (pps->entropy_coding_sync_enabled_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_ENTROPY_CODING_SYNC_ENABLED;
++
++ if (pps->loop_filter_across_tiles_enabled_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_LOOP_FILTER_ACROSS_TILES_ENABLED;
++
++ if (pps->seq_loop_filter_across_slices_enabled_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_PPS_LOOP_FILTER_ACROSS_SLICES_ENABLED;
++
++ if (pps->deblocking_filter_override_enabled_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_DEBLOCKING_FILTER_OVERRIDE_ENABLED;
++
++ if (pps->disable_dbf)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_PPS_DISABLE_DEBLOCKING_FILTER;
++
++ if (pps->lists_modification_present_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_LISTS_MODIFICATION_PRESENT;
++
++ if (pps->slice_header_extension_present_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_SLICE_SEGMENT_HEADER_EXTENSION_PRESENT;
++
++ if (pps->deblocking_filter_control_present_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_DEBLOCKING_FILTER_CONTROL_PRESENT;
++
++ if (pps->uniform_spacing_flag)
++ controls->pps.flags |= V4L2_HEVC_PPS_FLAG_UNIFORM_SPACING;
++
++ if (pps->tiles_enabled_flag) {
++ controls->pps.num_tile_columns_minus1 = pps->num_tile_columns - 1;
++ controls->pps.num_tile_rows_minus1 = pps->num_tile_rows - 1;
++
++ for (int i = 0; i < pps->num_tile_columns; i++)
++ controls->pps.column_width_minus1[i] = pps->column_width[i] - 1;
++
++ for (int i = 0; i < pps->num_tile_rows; i++)
++ controls->pps.row_height_minus1[i] = pps->row_height[i] - 1;
++ }
++
++ controls->first_slice = 1;
++ controls->num_slices = 0;
++ controls->num_entry_point_offsets = 0;
++
++ return ff_v4l2_request_reset_frame(avctx, h->ref->frame);
++}
++
++static int v4l2_request_hevc_queue_decode(AVCodecContext *avctx, int last_slice)
++{
++ const HEVCContext *h = avctx->priv_data;
++ V4L2RequestControlsHEVC *controls = h->ref->hwaccel_picture_private;
++ struct v4l2_ctrl_hevc_slice_params *first_slice_params = &controls->slice_params[0];
++ V4L2RequestContextHEVC *ctx = avctx->internal->hwaccel_priv_data;
++ int num_controls = 0;
++
++ struct v4l2_ext_control control[V4L2_HEVC_CONTROLS_MAX] = {};
++
++ control[num_controls++] = (struct v4l2_ext_control) {
++ .id = V4L2_CID_STATELESS_HEVC_SPS,
++ .ptr = &controls->sps,
++ .size = sizeof(controls->sps),
++ };
++
++ control[num_controls++] = (struct v4l2_ext_control) {
++ .id = V4L2_CID_STATELESS_HEVC_PPS,
++ .ptr = &controls->pps,
++ .size = sizeof(controls->pps),
++ };
++
++ control[num_controls++] = (struct v4l2_ext_control) {
++ .id = V4L2_CID_STATELESS_HEVC_DECODE_PARAMS,
++ .ptr = &controls->dec_params,
++ .size = sizeof(controls->dec_params),
++ };
++
++ if (ctx->supports_scaling_matrix) {
++ control[num_controls++] = (struct v4l2_ext_control) {
++ .id = V4L2_CID_STATELESS_HEVC_SCALING_MATRIX,
++ .ptr = &controls->scaling_matrix,
++ .size = sizeof(controls->scaling_matrix),
++ };
++ }
++
++ if (ctx->supports_slices) {
++ control[num_controls++] = (struct v4l2_ext_control) {
++ .id = V4L2_CID_STATELESS_HEVC_SLICE_PARAMS,
++ .ptr = &controls->slice_params,
++ .size = sizeof(*first_slice_params) * controls->num_slices,
++ };
++ }
++
++ //this assumes that decoders supporting entry_point_offsets submit a single slice per request
++ if (ctx->supports_entry_point_offsets && first_slice_params->num_entry_point_offsets > 0) {
++ control[num_controls++] = (struct v4l2_ext_control) {
++ .id = V4L2_CID_STATELESS_HEVC_ENTRY_POINT_OFFSETS,
++ .ptr = controls->entry_point_offsets,
++ .size = sizeof(*controls->entry_point_offsets) * first_slice_params->num_entry_point_offsets,
++ };
++ }
++
++ if (ctx->decode_mode == V4L2_STATELESS_HEVC_DECODE_MODE_SLICE_BASED)
++ return ff_v4l2_request_decode_slice(avctx, h->ref->frame, control, num_controls, controls->first_slice, last_slice);
++
++ return ff_v4l2_request_decode_frame(avctx, h->ref->frame, control, num_controls);
++}
++
++static int v4l2_request_hevc_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
++{
++ const HEVCContext *h = avctx->priv_data;
++ V4L2RequestControlsHEVC *controls = h->ref->hwaccel_picture_private;
++ V4L2RequestContextHEVC *ctx = avctx->internal->hwaccel_priv_data;
++ V4L2RequestDescriptor *req = (V4L2RequestDescriptor*)h->ref->frame->data[0];
++ int ret, slice = FFMIN(controls->num_slices, MAX_SLICES - 1);
++
++ if (ctx->decode_mode == V4L2_STATELESS_HEVC_DECODE_MODE_SLICE_BASED && slice) {
++ ret = v4l2_request_hevc_queue_decode(avctx, 0);
++ if (ret)
++ return ret;
++
++ ff_v4l2_request_reset_frame(avctx, h->ref->frame);
++ slice = controls->num_slices = 0;
++ controls->first_slice = 0;
++ }
++
++ ret = v4l2_request_hevc_fill_slice_params(h, controls, slice);
++ if (ret)
++ return ret;
++
++ if (ctx->start_code == V4L2_STATELESS_HEVC_START_CODE_ANNEX_B) {
++ ret = ff_v4l2_request_append_output_buffer(avctx, h->ref->frame, nalu_slice_start_code, 3);
++ if (ret)
++ return ret;
++ }
++
++ ret = ff_v4l2_request_append_output_buffer(avctx, h->ref->frame, buffer, size);
++ if (ret)
++ return ret;
++
++ controls->slice_params[slice].bit_size = req->output.used * 8; //FIXME
++ controls->num_slices++;
++ return 0;
++}
++
++static int v4l2_request_hevc_end_frame(AVCodecContext *avctx)
++{
++ const HEVCContext *h = avctx->priv_data;
++ V4L2RequestControlsHEVC *controls = h->ref->hwaccel_picture_private;
++ int ret;
++
++ ret = v4l2_request_hevc_queue_decode(avctx, 1);
++
++ av_freep(&controls->entry_point_offsets);
++
++ return ret;
++}
++
++static int v4l2_request_hevc_set_controls(AVCodecContext *avctx)
++{
++ V4L2RequestContextHEVC *ctx = avctx->internal->hwaccel_priv_data;
++ int ret;
++
++ struct v4l2_ext_control control[] = {
++ { .id = V4L2_CID_STATELESS_HEVC_DECODE_MODE, },
++ { .id = V4L2_CID_STATELESS_HEVC_START_CODE, },
++ };
++ struct v4l2_query_ext_ctrl entry_point_offsets = {
++ .id = V4L2_CID_STATELESS_HEVC_ENTRY_POINT_OFFSETS,
++ };
++ struct v4l2_query_ext_ctrl slice_params = {
++ .id = V4L2_CID_STATELESS_HEVC_SLICE_PARAMS,
++ };
++ struct v4l2_query_ext_ctrl scaling_matrix = {
++ .id = V4L2_CID_STATELESS_HEVC_SCALING_MATRIX,
++ };
++
++ ctx->decode_mode = ff_v4l2_request_query_control_default_value(avctx, V4L2_CID_STATELESS_HEVC_DECODE_MODE);
++ if (ctx->decode_mode != V4L2_STATELESS_HEVC_DECODE_MODE_SLICE_BASED &&
++ ctx->decode_mode != V4L2_STATELESS_HEVC_DECODE_MODE_FRAME_BASED) {
++ av_log(avctx, AV_LOG_ERROR, "%s: unsupported decode mode, %d\n", __func__, ctx->decode_mode);
++ return AVERROR(EINVAL);
++ }
++
++ ctx->start_code = ff_v4l2_request_query_control_default_value(avctx, V4L2_CID_STATELESS_HEVC_START_CODE);
++ if (ctx->start_code != V4L2_STATELESS_HEVC_START_CODE_NONE &&
++ ctx->start_code != V4L2_STATELESS_HEVC_START_CODE_ANNEX_B) {
++ av_log(avctx, AV_LOG_ERROR, "%s: unsupported start code, %d\n", __func__, ctx->start_code);
++ return AVERROR(EINVAL);
++ }
++
++ ret = ff_v4l2_request_query_control(avctx, &entry_point_offsets);
++ if (ret)
++ ctx->supports_entry_point_offsets = 0;
++ else
++ ctx->supports_entry_point_offsets = 1;
++
++ ret = ff_v4l2_request_query_control(avctx, &slice_params);
++ if (ret) {
++ ctx->supports_slices = 0;
++ ctx->max_slices = 0;
++ if (ctx->decode_mode == V4L2_STATELESS_HEVC_DECODE_MODE_SLICE_BASED) {
++ av_log(avctx, AV_LOG_ERROR, "%s: decoder is slice-based, \
++ but doesn't support V4L2_CID_STATELESS_HEVC_SLICE_PARAMS control \n", __func__);
++ return AVERROR(EINVAL);
++ }
++
++ if (ctx->supports_entry_point_offsets) {
++ av_log(avctx, AV_LOG_ERROR, "%s: decoder supports entry_point_offsets, \
++ but doesn't support V4L2_CID_STATELESS_HEVC_SLICE_PARAMS control \n", __func__);
++ return AVERROR(EINVAL);
++ }
++ } else {
++ ctx->supports_slices = 1;
++ ctx->max_slices = slice_params.dims[0];
++ if (ctx->max_slices > MAX_SLICES) {
++ av_log(avctx, AV_LOG_ERROR, "%s: unsupported max slices, %u\n", __func__, ctx->max_slices);
++ return AVERROR(EINVAL);
++ }
++ }
++
++ ret = ff_v4l2_request_query_control(avctx, &scaling_matrix);
++ if (ret)
++ ctx->supports_scaling_matrix = 0;
++ else
++ ctx->supports_scaling_matrix = 1;
++
++ av_log(avctx, AV_LOG_DEBUG, "%s: decoder is %s and supports slices %d, supports entry_point_offsets: %d supports scaling_matrix: %d max slices: %u\n",
++ __func__,
++ ctx->decode_mode == V4L2_STATELESS_HEVC_DECODE_MODE_SLICE_BASED ? "slice based" : "frame based",
++ ctx->supports_slices,
++ ctx->supports_entry_point_offsets,
++ ctx->supports_scaling_matrix,
++ ctx->max_slices
++ );
++
++ control[0].value = ctx->decode_mode;
++ control[1].value = ctx->start_code;
++
++ return ff_v4l2_request_set_controls(avctx, control, FF_ARRAY_ELEMS(control));
++}
++
++static int v4l2_request_hevc_init(AVCodecContext *avctx)
++{
++ const HEVCContext *h = avctx->priv_data;
++ struct v4l2_ctrl_hevc_sps sps;
++ int ret;
++
++ struct v4l2_ext_control control[] = {
++ {
++ .id = V4L2_CID_STATELESS_HEVC_SPS,
++ .ptr = &sps,
++ .size = sizeof(sps),
++ },
++ };
++
++ fill_sps(&sps, h);
++
++ ret = ff_v4l2_request_init(avctx, V4L2_PIX_FMT_HEVC_SLICE, 4 * 1024 * 1024, control, FF_ARRAY_ELEMS(control));
++ if (ret)
++ return ret;
++
++ return v4l2_request_hevc_set_controls(avctx);
++}
++
++const FFHWAccel ff_hevc_v4l2request_hwaccel = {
++ .p.name = "hevc_v4l2request",
++ .p.type = AVMEDIA_TYPE_VIDEO,
++ .p.id = AV_CODEC_ID_HEVC,
++ .p.pix_fmt = AV_PIX_FMT_DRM_PRIME,
++ .start_frame = v4l2_request_hevc_start_frame,
++ .decode_slice = v4l2_request_hevc_decode_slice,
++ .end_frame = v4l2_request_hevc_end_frame,
++ .frame_priv_data_size = sizeof(V4L2RequestControlsHEVC),
++ .init = v4l2_request_hevc_init,
++ .uninit = ff_v4l2_request_uninit,
++ .priv_data_size = sizeof(V4L2RequestContextHEVC),
++ .frame_params = ff_v4l2_request_frame_params,
++ .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
++};
+--
+2.44.0
+
+
+From 381025bd969276c176fd4fda0b6f2271cedfac8e Mon Sep 17 00:00:00 2001
+From: Boris Brezillon <boris.brezillon at collabora.com>
+Date: Thu, 12 Dec 2019 16:13:55 +0100
+Subject: [PATCH 08/18] Add V4L2 request API vp9 hwaccel
+
+Signed-off-by: Boris Brezillon <boris.brezillon at collabora.com>
+Signed-off-by: Jernej Skrabec <jernej.skrabec at gmail.com>
+---
+ configure | 3 +
+ libavcodec/Makefile | 1 +
+ libavcodec/hwaccels.h | 1 +
+ libavcodec/v4l2_request_vp9.c | 283 ++++++++++++++++++++++++++++++++++
+ libavcodec/vp9.c | 190 ++++++++++++++++-------
+ libavcodec/vp9dec.h | 4 +
+ libavcodec/vp9shared.h | 1 +
+ 7 files changed, 429 insertions(+), 54 deletions(-)
+ create mode 100644 libavcodec/v4l2_request_vp9.c
+
+diff --git a/configure b/configure
+index ad9af0b..810e57c 100755
+--- a/configure
++++ b/configure
+@@ -3171,6 +3171,8 @@ vp9_dxva2_hwaccel_deps="dxva2 DXVA_PicParams_VP9"
+ vp9_dxva2_hwaccel_select="vp9_decoder"
+ vp9_nvdec_hwaccel_deps="nvdec"
+ vp9_nvdec_hwaccel_select="vp9_decoder"
++vp9_v4l2request_hwaccel_deps="v4l2_request"
++vp9_v4l2request_hwaccel_select="vp9_decoder"
+ vp9_vaapi_hwaccel_deps="vaapi VADecPictureParameterBufferVP9_bit_depth"
+ vp9_vaapi_hwaccel_select="vp9_decoder"
+ vp9_vdpau_hwaccel_deps="vdpau VdpPictureInfoVP9"
+@@ -7015,6 +7017,7 @@ check_cc h264_v4l2_request linux/videodev2.h "int i = V4L2_PIX_FMT_H264_SLICE;"
+ check_cc hevc_v4l2_request linux/videodev2.h "int i = V4L2_PIX_FMT_HEVC_SLICE;"
+ check_cc mpeg2_v4l2_request linux/videodev2.h "int i = V4L2_PIX_FMT_MPEG2_SLICE;"
+ check_cc vp8_v4l2_request linux/videodev2.h "int i = V4L2_PIX_FMT_VP8_FRAME;"
++check_cc vp9_v4l2_request linux/videodev2.h "int i = V4L2_PIX_FMT_VP9_FRAME;"
+
+ check_headers sys/videoio.h
+ test_code cc sys/videoio.h "struct v4l2_frmsizeenum vfse; vfse.discrete.width = 0;" && enable_sanitized struct_v4l2_frmivalenum_discrete
+diff --git a/libavcodec/Makefile b/libavcodec/Makefile
+index e32bd18..a2dffd5 100644
+--- a/libavcodec/Makefile
++++ b/libavcodec/Makefile
+@@ -1043,6 +1043,7 @@ OBJS-$(CONFIG_VP8_VAAPI_HWACCEL) += vaapi_vp8.o
+ OBJS-$(CONFIG_VP9_D3D11VA_HWACCEL) += dxva2_vp9.o
+ OBJS-$(CONFIG_VP9_DXVA2_HWACCEL) += dxva2_vp9.o
+ OBJS-$(CONFIG_VP9_NVDEC_HWACCEL) += nvdec_vp9.o
++OBJS-$(CONFIG_VP9_V4L2REQUEST_HWACCEL) += v4l2_request_vp9.o
+ OBJS-$(CONFIG_VP9_VAAPI_HWACCEL) += vaapi_vp9.o
+ OBJS-$(CONFIG_VP9_VDPAU_HWACCEL) += vdpau_vp9.o
+ OBJS-$(CONFIG_VP9_VIDEOTOOLBOX_HWACCEL) += videotoolbox_vp9.o
+diff --git a/libavcodec/hwaccels.h b/libavcodec/hwaccels.h
+index 4a0de92..8d0d348 100644
+--- a/libavcodec/hwaccels.h
++++ b/libavcodec/hwaccels.h
+@@ -77,6 +77,7 @@ extern const struct FFHWAccel ff_vp9_d3d11va_hwaccel;
+ extern const struct FFHWAccel ff_vp9_d3d11va2_hwaccel;
+ extern const struct FFHWAccel ff_vp9_dxva2_hwaccel;
+ extern const struct FFHWAccel ff_vp9_nvdec_hwaccel;
++extern const struct FFHWAccel ff_vp9_v4l2request_hwaccel;
+ extern const struct FFHWAccel ff_vp9_vaapi_hwaccel;
+ extern const struct FFHWAccel ff_vp9_vdpau_hwaccel;
+ extern const struct FFHWAccel ff_vp9_videotoolbox_hwaccel;
+diff --git a/libavcodec/v4l2_request_vp9.c b/libavcodec/v4l2_request_vp9.c
+new file mode 100644
+index 0000000..2c6a9e4
+--- /dev/null
++++ b/libavcodec/v4l2_request_vp9.c
+@@ -0,0 +1,283 @@
++/*
++ * This file is part of FFmpeg.
++ *
++ * FFmpeg is free software; you can redistribute it and/or
++ * modify it under the terms of the GNU Lesser General Public
++ * License as published by the Free Software Foundation; either
++ * version 2.1 of the License, or (at your option) any later version.
++ *
++ * FFmpeg is distributed in the hope that it will be useful,
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
++ * Lesser General Public License for more details.
++ *
++ * You should have received a copy of the GNU Lesser General Public
++ * License along with FFmpeg; if not, write to the Free Software
++ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
++ */
++
++#include "hwaccel_internal.h"
++#include "hwconfig.h"
++#include "v4l2_request.h"
++#include "vp9dec.h"
++
++typedef struct V4L2RequestControlsVP9 {
++ struct v4l2_ctrl_vp9_frame decode_params;
++ struct v4l2_ctrl_vp9_compressed_hdr chp;
++} V4L2RequestControlsVP9;
++
++static void v4l2_request_vp9_set_frame_ctx(AVCodecContext *avctx)
++{
++ VP9Context *s = avctx->priv_data;
++ const VP9Frame *f = &s->s.frames[CUR_FRAME];
++ V4L2RequestControlsVP9 *controls = f->hwaccel_picture_private;
++ struct v4l2_ctrl_vp9_compressed_hdr *chp = &controls->chp;
++
++ memset(chp, 0, sizeof(&chp));
++
++ chp->tx_mode = s->s.h.txfmmode;
++ memcpy(chp->tx8, s->prob_raw.p.tx8p, sizeof(s->prob_raw.p.tx8p));
++ memcpy(chp->tx16, s->prob_raw.p.tx16p, sizeof(s->prob_raw.p.tx16p));
++ memcpy(chp->tx32, s->prob_raw.p.tx32p, sizeof(s->prob_raw.p.tx32p));
++ for (unsigned i = 0; i < 4; i++) {
++ for (unsigned j = 0; j < 2; j++) {
++ for (unsigned k = 0; k < 2; k++) {
++ for (unsigned l = 0; l < 6; l++) {
++ for (unsigned m = 0; m < 6; m++) {
++ memcpy(chp->coef[i][j][k][l][m], s->prob_raw.coef[i][j][k][l][m], sizeof(chp->coef[0][0][0][0][0]));
++ }
++ }
++ }
++ }
++ }
++ memcpy(chp->skip, s->prob_raw.p.skip, sizeof(s->prob_raw.p.skip));
++ memcpy(chp->inter_mode, s->prob_raw.p.mv_mode, sizeof(s->prob_raw.p.mv_mode));
++ memcpy(chp->interp_filter, s->prob_raw.p.filter, sizeof(s->prob_raw.p.filter));
++ memcpy(chp->is_inter, s->prob_raw.p.intra, sizeof(s->prob_raw.p.intra));
++ memcpy(chp->comp_mode, s->prob_raw.p.comp, sizeof(s->prob_raw.p.comp));
++ memcpy(chp->single_ref, s->prob_raw.p.single_ref, sizeof(s->prob_raw.p.single_ref));
++ memcpy(chp->comp_ref, s->prob_raw.p.comp_ref, sizeof(s->prob_raw.p.comp_ref));
++ memcpy(chp->y_mode, s->prob_raw.p.y_mode, sizeof(s->prob_raw.p.y_mode));
++ for (unsigned i = 0; i < 10; i++)
++ memcpy(chp->uv_mode[i], s->prob.p.uv_mode[i], sizeof(s->prob.p.uv_mode[0]));
++ for (unsigned i = 0; i < 4; i++)
++ memcpy(chp->partition[i * 4], s->prob_raw.p.partition[i], sizeof(s->prob_raw.p.partition[0]));
++ memcpy(chp->mv.joint, s->prob_raw.p.mv_joint, sizeof(s->prob_raw.p.mv_joint));
++ for (unsigned i = 0; i < 2; i++) {
++ chp->mv.sign[i] = s->prob_raw.p.mv_comp[i].sign;
++ memcpy(chp->mv.classes[i], s->prob_raw.p.mv_comp[i].classes, sizeof(s->prob_raw.p.mv_comp[0].classes));
++ chp->mv.class0_bit[i] = s->prob_raw.p.mv_comp[i].class0;
++ memcpy(chp->mv.bits[i], s->prob_raw.p.mv_comp[i].bits, sizeof(s->prob_raw.p.mv_comp[0].bits));
++ memcpy(chp->mv.class0_fr[i], s->prob_raw.p.mv_comp[i].class0_fp, sizeof(s->prob_raw.p.mv_comp[0].class0_fp));
++ memcpy(chp->mv.fr[i], s->prob_raw.p.mv_comp[i].fp, sizeof(s->prob_raw.p.mv_comp[0].fp));
++ chp->mv.class0_hp[i] = s->prob_raw.p.mv_comp[i].class0_hp;
++ chp->mv.hp[i] = s->prob_raw.p.mv_comp[i].hp;
++ }
++}
++
++static void fill_frame(struct v4l2_ctrl_vp9_frame *dec_params, AVCodecContext *avctx)
++{
++ const VP9Context *s = avctx->priv_data;
++ const ThreadFrame *ref;
++
++ memset(dec_params, 0, sizeof(*dec_params));
++
++ if (s->s.h.keyframe)
++ dec_params->flags |= V4L2_VP9_FRAME_FLAG_KEY_FRAME;
++ if (!s->s.h.invisible)
++ dec_params->flags |= V4L2_VP9_FRAME_FLAG_SHOW_FRAME;
++ if (s->s.h.errorres)
++ dec_params->flags |= V4L2_VP9_FRAME_FLAG_ERROR_RESILIENT;
++ if (s->s.h.intraonly)
++ dec_params->flags |= V4L2_VP9_FRAME_FLAG_INTRA_ONLY;
++ if (!s->s.h.keyframe && s->s.h.highprecisionmvs)
++ dec_params->flags |= V4L2_VP9_FRAME_FLAG_ALLOW_HIGH_PREC_MV;
++ if (s->s.h.refreshctx)
++ dec_params->flags |= V4L2_VP9_FRAME_FLAG_REFRESH_FRAME_CTX;
++ if (s->s.h.parallelmode)
++ dec_params->flags |= V4L2_VP9_FRAME_FLAG_PARALLEL_DEC_MODE;
++ if (s->ss_h)
++ dec_params->flags |= V4L2_VP9_FRAME_FLAG_X_SUBSAMPLING;
++ if (s->ss_v)
++ dec_params->flags |= V4L2_VP9_FRAME_FLAG_Y_SUBSAMPLING;
++ if (avctx->color_range == AVCOL_RANGE_JPEG)
++ dec_params->flags |= V4L2_VP9_FRAME_FLAG_COLOR_RANGE_FULL_SWING;
++
++ dec_params->compressed_header_size = s->s.h.compressed_header_size;
++ dec_params->uncompressed_header_size = s->s.h.uncompressed_header_size;
++ dec_params->profile = s->s.h.profile;
++ dec_params->reset_frame_context = s->s.h.resetctx > 0 ? s->s.h.resetctx - 1 : 0;
++ dec_params->frame_context_idx = s->s.h.framectxid;
++ dec_params->bit_depth = s->s.h.bpp;
++
++ dec_params->interpolation_filter = s->s.h.filtermode ^ (s->s.h.filtermode <= 1);
++ dec_params->tile_cols_log2 = s->s.h.tiling.log2_tile_cols;
++ dec_params->tile_rows_log2 = s->s.h.tiling.log2_tile_rows;
++ dec_params->reference_mode = s->s.h.comppredmode;
++ dec_params->frame_width_minus_1 = s->w - 1;
++ dec_params->frame_height_minus_1 = s->h - 1;
++ //dec_params->render_width_minus_1 = avctx->width - 1;
++ //dec_params->render_height_minus_1 = avctx->height - 1;
++
++ ref = &s->s.refs[s->s.h.refidx[0]];
++ if (ref->f && ref->f->buf[0])
++ dec_params->last_frame_ts = ff_v4l2_request_get_capture_timestamp(ref->f);
++ ref = &s->s.refs[s->s.h.refidx[1]];
++ if (ref->f && ref->f->buf[0])
++ dec_params->golden_frame_ts = ff_v4l2_request_get_capture_timestamp(ref->f);
++ ref = &s->s.refs[s->s.h.refidx[2]];
++ if (ref->f && ref->f->buf[0])
++ dec_params->alt_frame_ts = ff_v4l2_request_get_capture_timestamp(ref->f);
++
++ if (s->s.h.signbias[0])
++ dec_params->ref_frame_sign_bias |= V4L2_VP9_SIGN_BIAS_LAST;
++ if (s->s.h.signbias[1])
++ dec_params->ref_frame_sign_bias |= V4L2_VP9_SIGN_BIAS_GOLDEN;
++ if (s->s.h.signbias[2])
++ dec_params->ref_frame_sign_bias |= V4L2_VP9_SIGN_BIAS_ALT;
++
++ if (s->s.h.lf_delta.enabled)
++ dec_params->lf.flags |= V4L2_VP9_LOOP_FILTER_FLAG_DELTA_ENABLED;
++ if (s->s.h.lf_delta.updated)
++ dec_params->lf.flags |= V4L2_VP9_LOOP_FILTER_FLAG_DELTA_UPDATE;
++
++ dec_params->lf.level = s->s.h.filter.level;
++ dec_params->lf.sharpness = s->s.h.filter.sharpness;
++ for (unsigned i = 0; i < 4; i++)
++ dec_params->lf.ref_deltas[i] = s->s.h.lf_delta.ref[i];
++ for (unsigned i = 0; i < 2; i++)
++ dec_params->lf.mode_deltas[i] = s->s.h.lf_delta.mode[i];
++
++ dec_params->quant.base_q_idx = s->s.h.yac_qi;
++ dec_params->quant.delta_q_y_dc = s->s.h.ydc_qdelta;
++ dec_params->quant.delta_q_uv_dc = s->s.h.uvdc_qdelta;
++ dec_params->quant.delta_q_uv_ac = s->s.h.uvac_qdelta;
++
++ if (s->s.h.segmentation.enabled)
++ dec_params->seg.flags |= V4L2_VP9_SEGMENTATION_FLAG_ENABLED;
++ if (s->s.h.segmentation.update_map)
++ dec_params->seg.flags |= V4L2_VP9_SEGMENTATION_FLAG_UPDATE_MAP;
++ if (s->s.h.segmentation.temporal)
++ dec_params->seg.flags |= V4L2_VP9_SEGMENTATION_FLAG_TEMPORAL_UPDATE;
++ if (s->s.h.segmentation.update_data)
++ dec_params->seg.flags |= V4L2_VP9_SEGMENTATION_FLAG_UPDATE_DATA;
++ if (s->s.h.segmentation.absolute_vals)
++ dec_params->seg.flags |= V4L2_VP9_SEGMENTATION_FLAG_ABS_OR_DELTA_UPDATE;
++
++ for (unsigned i = 0; i < 7; i++)
++ dec_params->seg.tree_probs[i] = s->s.h.segmentation.prob[i];
++
++ if (s->s.h.segmentation.temporal) {
++ for (unsigned i = 0; i < 3; i++)
++ dec_params->seg.pred_probs[i] = s->s.h.segmentation.pred_prob[i];
++ } else {
++ memset(dec_params->seg.pred_probs, 255, sizeof(dec_params->seg.pred_probs));
++ }
++
++ for (unsigned i = 0; i < 8; i++) {
++ if (s->s.h.segmentation.feat[i].q_enabled) {
++ dec_params->seg.feature_enabled[i] |= 1 << V4L2_VP9_SEG_LVL_ALT_Q;
++ dec_params->seg.feature_data[i][V4L2_VP9_SEG_LVL_ALT_Q] = s->s.h.segmentation.feat[i].q_val;
++ }
++
++ if (s->s.h.segmentation.feat[i].lf_enabled) {
++ dec_params->seg.feature_enabled[i] |= 1 << V4L2_VP9_SEG_LVL_ALT_L;
++ dec_params->seg.feature_data[i][V4L2_VP9_SEG_LVL_ALT_L] = s->s.h.segmentation.feat[i].lf_val;
++ }
++
++ if (s->s.h.segmentation.feat[i].ref_enabled) {
++ dec_params->seg.feature_enabled[i] |= 1 << V4L2_VP9_SEG_LVL_REF_FRAME;
++ dec_params->seg.feature_data[i][V4L2_VP9_SEG_LVL_REF_FRAME] = s->s.h.segmentation.feat[i].ref_val;
++ }
++
++ if (s->s.h.segmentation.feat[i].skip_enabled)
++ dec_params->seg.feature_enabled[i] |= 1 << V4L2_VP9_SEG_LVL_SKIP;
++ }
++}
++
++static int v4l2_request_vp9_start_frame(AVCodecContext *avctx,
++ av_unused const uint8_t *buffer,
++ av_unused uint32_t size)
++{
++ const VP9Context *s = avctx->priv_data;
++ const VP9Frame *f = &s->s.frames[CUR_FRAME];
++ V4L2RequestControlsVP9 *controls = f->hwaccel_picture_private;
++
++ v4l2_request_vp9_set_frame_ctx(avctx);
++
++ fill_frame(&controls->decode_params, avctx);
++
++ return ff_v4l2_request_reset_frame(avctx, f->tf.f);
++}
++
++static int v4l2_request_vp9_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
++{
++ const VP9Context *s = avctx->priv_data;
++ const VP9Frame *f = &s->s.frames[CUR_FRAME];
++
++ return ff_v4l2_request_append_output_buffer(avctx, f->tf.f, buffer, size);
++}
++
++static int v4l2_request_vp9_end_frame(AVCodecContext *avctx)
++{
++ const VP9Context *s = avctx->priv_data;
++ const VP9Frame *f = &s->s.frames[CUR_FRAME];
++ V4L2RequestControlsVP9 *controls = f->hwaccel_picture_private;
++ int ret;
++
++ struct v4l2_ext_control control[] = {
++ {
++ .id = V4L2_CID_STATELESS_VP9_FRAME,
++ .ptr = &controls->decode_params,
++ .size = sizeof(controls->decode_params),
++ },
++ {
++ .id = V4L2_CID_STATELESS_VP9_COMPRESSED_HDR,
++ .ptr = &controls->chp,
++ .size = sizeof(controls->chp),
++ },
++ };
++
++ ret = ff_v4l2_request_decode_frame(avctx, f->tf.f, control, FF_ARRAY_ELEMS(control));
++ if (ret)
++ return ret;
++
++ if (!s->s.h.refreshctx)
++ return 0;
++
++ return 0;
++}
++
++static int v4l2_request_vp9_init(AVCodecContext *avctx)
++{
++ struct v4l2_ctrl_vp9_frame frame;
++
++ struct v4l2_ext_control control[] = {
++ {
++ .id = V4L2_CID_STATELESS_VP9_FRAME,
++ .ptr = &frame,
++ .size = sizeof(frame),
++ },
++ };
++
++ fill_frame(&frame, avctx);
++
++ // TODO: check V4L2_CID_MPEG_VIDEO_VP9_PROFILE
++ return ff_v4l2_request_init(avctx, V4L2_PIX_FMT_VP9_FRAME, 3 * 1024 * 1024, control, FF_ARRAY_ELEMS(control));
++}
++
++const FFHWAccel ff_vp9_v4l2request_hwaccel = {
++ .p.name = "vp9_v4l2request",
++ .p.type = AVMEDIA_TYPE_VIDEO,
++ .p.id = AV_CODEC_ID_VP9,
++ .p.pix_fmt = AV_PIX_FMT_DRM_PRIME,
++ .start_frame = v4l2_request_vp9_start_frame,
++ .decode_slice = v4l2_request_vp9_decode_slice,
++ .end_frame = v4l2_request_vp9_end_frame,
++ .frame_priv_data_size = sizeof(V4L2RequestControlsVP9),
++ .init = v4l2_request_vp9_init,
++ .uninit = ff_v4l2_request_uninit,
++ .priv_data_size = sizeof(V4L2RequestContext),
++ .frame_params = ff_v4l2_request_frame_params,
++ .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
++};
+diff --git a/libavcodec/vp9.c b/libavcodec/vp9.c
+index 885500f..6ab88e5 100644
+--- a/libavcodec/vp9.c
++++ b/libavcodec/vp9.c
+@@ -177,6 +177,7 @@ static int update_size(AVCodecContext *avctx, int w, int h)
+ #define HWACCEL_MAX (CONFIG_VP9_DXVA2_HWACCEL + \
+ CONFIG_VP9_D3D11VA_HWACCEL * 2 + \
+ CONFIG_VP9_NVDEC_HWACCEL + \
++ CONFIG_VP9_V4L2REQUEST_HWACCEL + \
+ CONFIG_VP9_VAAPI_HWACCEL + \
+ CONFIG_VP9_VDPAU_HWACCEL + \
+ CONFIG_VP9_VIDEOTOOLBOX_HWACCEL)
+@@ -205,6 +206,9 @@ static int update_size(AVCodecContext *avctx, int w, int h)
+ #if CONFIG_VP9_NVDEC_HWACCEL
+ *fmtp++ = AV_PIX_FMT_CUDA;
+ #endif
++#if CONFIG_VP9_V4L2REQUEST_HWACCEL
++ *fmtp++ = AV_PIX_FMT_DRM_PRIME;
++#endif
+ #if CONFIG_VP9_VAAPI_HWACCEL
+ *fmtp++ = AV_PIX_FMT_VAAPI;
+ #endif
+@@ -219,6 +223,9 @@ static int update_size(AVCodecContext *avctx, int w, int h)
+ #if CONFIG_VP9_NVDEC_HWACCEL
+ *fmtp++ = AV_PIX_FMT_CUDA;
+ #endif
++#if CONFIG_VP9_V4L2REQUEST_HWACCEL
++ *fmtp++ = AV_PIX_FMT_DRM_PRIME;
++#endif
+ #if CONFIG_VP9_VAAPI_HWACCEL
+ *fmtp++ = AV_PIX_FMT_VAAPI;
+ #endif
+@@ -386,7 +393,7 @@ static av_always_inline int inv_recenter_nonneg(int v, int m)
+ }
+
+ // differential forward probability updates
+-static int update_prob(VPXRangeCoder *c, int p)
++static int read_prob_delta(VPXRangeCoder *c)
+ {
+ static const uint8_t inv_map_table[255] = {
+ 7, 20, 33, 46, 59, 72, 85, 98, 111, 124, 137, 150, 163, 176,
+@@ -440,8 +447,13 @@ static int update_prob(VPXRangeCoder *c, int p)
+ av_assert2(d < FF_ARRAY_ELEMS(inv_map_table));
+ }
+
+- return p <= 128 ? 1 + inv_recenter_nonneg(inv_map_table[d], p - 1) :
+- 255 - inv_recenter_nonneg(inv_map_table[d], 255 - p);
++ return inv_map_table[d];
++}
++
++static int update_prob(int p, int d)
++{
++ return p <= 128 ? 1 + inv_recenter_nonneg(d, p - 1) :
++ 255 - inv_recenter_nonneg(d, 255 - p);
+ }
+
+ static int read_colorspace_details(AVCodecContext *avctx)
+@@ -707,7 +719,8 @@ static int decode_frame_header(AVCodecContext *avctx,
+ get_bits(&s->gb, 8) : 255;
+ }
+
+- if (get_bits1(&s->gb)) {
++ s->s.h.segmentation.update_data = get_bits1(&s->gb);
++ if (s->s.h.segmentation.update_data) {
+ s->s.h.segmentation.absolute_vals = get_bits1(&s->gb);
+ for (i = 0; i < 8; i++) {
+ if ((s->s.h.segmentation.feat[i].q_enabled = get_bits1(&s->gb)))
+@@ -907,6 +920,8 @@ static int decode_frame_header(AVCodecContext *avctx,
+ * as explicit copies if the fw update is missing (and skip the copy upon
+ * fw update)? */
+ s->prob.p = s->prob_ctx[c].p;
++ memset(&s->prob_raw.p, 0, sizeof(s->prob_raw.p));
++ memset(&s->prob_raw.coef, 0, sizeof(s->prob_raw.coef));
+
+ // txfm updates
+ if (s->s.h.lossless) {
+@@ -918,18 +933,25 @@ static int decode_frame_header(AVCodecContext *avctx,
+
+ if (s->s.h.txfmmode == TX_SWITCHABLE) {
+ for (i = 0; i < 2; i++)
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
+- s->prob.p.tx8p[i] = update_prob(&s->c, s->prob.p.tx8p[i]);
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.tx8p[i] = read_prob_delta(&s->c);
++ s->prob.p.tx8p[i] = update_prob(s->prob.p.tx8p[i],
++ s->prob_raw.p.tx8p[i]);
++ }
+ for (i = 0; i < 2; i++)
+ for (j = 0; j < 2; j++)
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
+- s->prob.p.tx16p[i][j] =
+- update_prob(&s->c, s->prob.p.tx16p[i][j]);
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.tx16p[i][j] = read_prob_delta(&s->c);
++ s->prob.p.tx16p[i][j] = update_prob(s->prob.p.tx16p[i][j],
++ s->prob_raw.p.tx16p[i][j]);
++ }
+ for (i = 0; i < 2; i++)
+ for (j = 0; j < 3; j++)
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
+- s->prob.p.tx32p[i][j] =
+- update_prob(&s->c, s->prob.p.tx32p[i][j]);
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.tx32p[i][j] = read_prob_delta(&s->c);
++ s->prob.p.tx32p[i][j] = update_prob(s->prob.p.tx32p[i][j],
++ s->prob_raw.p.tx32p[i][j]);
++ }
+ }
+ }
+
+@@ -941,15 +963,18 @@ static int decode_frame_header(AVCodecContext *avctx,
+ for (k = 0; k < 2; k++)
+ for (l = 0; l < 6; l++)
+ for (m = 0; m < 6; m++) {
++ uint8_t *pd = s->prob_raw.coef[i][j][k][l][m];
+ uint8_t *p = s->prob.coef[i][j][k][l][m];
+ uint8_t *r = ref[j][k][l][m];
+ if (m >= 3 && l == 0) // dc only has 3 pt
+ break;
+ for (n = 0; n < 3; n++) {
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
+- p[n] = update_prob(&s->c, r[n]);
+- else
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ pd[n] = read_prob_delta(&s->c);
++ p[n] = update_prob(r[n], pd[n]);
++ } else {
+ p[n] = r[n];
++ }
+ }
+ memcpy(&p[3], ff_vp9_model_pareto8[p[2]], 8);
+ }
+@@ -972,25 +997,37 @@ static int decode_frame_header(AVCodecContext *avctx,
+
+ // mode updates
+ for (i = 0; i < 3; i++)
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
+- s->prob.p.skip[i] = update_prob(&s->c, s->prob.p.skip[i]);
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.skip[i] = read_prob_delta(&s->c);
++ s->prob.p.skip[i] = update_prob(s->prob.p.skip[i],
++ s->prob_raw.p.skip[i]);
++ }
+ if (!s->s.h.keyframe && !s->s.h.intraonly) {
+ for (i = 0; i < 7; i++)
+ for (j = 0; j < 3; j++)
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.mv_mode[i][j] = read_prob_delta(&s->c);
+ s->prob.p.mv_mode[i][j] =
+- update_prob(&s->c, s->prob.p.mv_mode[i][j]);
++ update_prob(s->prob.p.mv_mode[i][j],
++ s->prob_raw.p.mv_mode[i][j]);
++ }
+
+ if (s->s.h.filtermode == FILTER_SWITCHABLE)
+ for (i = 0; i < 4; i++)
+ for (j = 0; j < 2; j++)
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.filter[i][j] = read_prob_delta(&s->c);
+ s->prob.p.filter[i][j] =
+- update_prob(&s->c, s->prob.p.filter[i][j]);
++ update_prob(s->prob.p.filter[i][j],
++ s->prob_raw.p.filter[i][j]);
++ }
+
+ for (i = 0; i < 4; i++)
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
+- s->prob.p.intra[i] = update_prob(&s->c, s->prob.p.intra[i]);
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.intra[i] = read_prob_delta(&s->c);
++ s->prob.p.intra[i] = update_prob(s->prob.p.intra[i],
++ s->prob_raw.p.intra[i]);
++ }
+
+ if (s->s.h.allowcompinter) {
+ s->s.h.comppredmode = vp89_rac_get(&s->c);
+@@ -998,92 +1035,134 @@ static int decode_frame_header(AVCodecContext *avctx,
+ s->s.h.comppredmode += vp89_rac_get(&s->c);
+ if (s->s.h.comppredmode == PRED_SWITCHABLE)
+ for (i = 0; i < 5; i++)
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.comp[i] = read_prob_delta(&s->c);
+ s->prob.p.comp[i] =
+- update_prob(&s->c, s->prob.p.comp[i]);
++ update_prob(s->prob.p.comp[i], s->prob_raw.p.comp[i]);
++ }
+ } else {
+ s->s.h.comppredmode = PRED_SINGLEREF;
+ }
+
+ if (s->s.h.comppredmode != PRED_COMPREF) {
+ for (i = 0; i < 5; i++) {
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.single_ref[i][0] = read_prob_delta(&s->c);
+ s->prob.p.single_ref[i][0] =
+- update_prob(&s->c, s->prob.p.single_ref[i][0]);
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
++ update_prob(s->prob.p.single_ref[i][0],
++ s->prob_raw.p.single_ref[i][0]);
++ }
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.single_ref[i][1] = read_prob_delta(&s->c);
+ s->prob.p.single_ref[i][1] =
+- update_prob(&s->c, s->prob.p.single_ref[i][1]);
++ update_prob(s->prob.p.single_ref[i][1],
++ s->prob_raw.p.single_ref[i][1]);
++ }
+ }
+ }
+
+ if (s->s.h.comppredmode != PRED_SINGLEREF) {
+ for (i = 0; i < 5; i++)
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.comp_ref[i] = read_prob_delta(&s->c);
+ s->prob.p.comp_ref[i] =
+- update_prob(&s->c, s->prob.p.comp_ref[i]);
++ update_prob(s->prob.p.comp_ref[i],
++ s->prob_raw.p.comp_ref[i]);
++ }
+ }
+
+ for (i = 0; i < 4; i++)
+ for (j = 0; j < 9; j++)
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.y_mode[i][j] = read_prob_delta(&s->c);
+ s->prob.p.y_mode[i][j] =
+- update_prob(&s->c, s->prob.p.y_mode[i][j]);
++ update_prob(s->prob.p.y_mode[i][j],
++ s->prob_raw.p.y_mode[i][j]);
++ }
+
+ for (i = 0; i < 4; i++)
+ for (j = 0; j < 4; j++)
+ for (k = 0; k < 3; k++)
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.partition[i][j][k] = read_prob_delta(&s->c);
+ s->prob.p.partition[3 - i][j][k] =
+- update_prob(&s->c,
+- s->prob.p.partition[3 - i][j][k]);
++ update_prob(s->prob.p.partition[3 - i][j][k],
++ s->prob_raw.p.partition[i][j][k]);
++ }
+
+ // mv fields don't use the update_prob subexp model for some reason
+ for (i = 0; i < 3; i++)
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
+- s->prob.p.mv_joint[i] = (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.mv_joint[i] = (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
++ s->prob.p.mv_joint[i] = s->prob_raw.p.mv_joint[i];
++ }
+
+ for (i = 0; i < 2; i++) {
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
+- s->prob.p.mv_comp[i].sign =
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.mv_comp[i].sign =
+ (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
++ s->prob.p.mv_comp[i].sign =
++ s->prob_raw.p.mv_comp[i].sign;
++ }
+
+ for (j = 0; j < 10; j++)
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
+- s->prob.p.mv_comp[i].classes[j] =
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.mv_comp[i].classes[j] =
+ (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
++ s->prob.p.mv_comp[i].classes[j] =
++ s->prob_raw.p.mv_comp[i].classes[j];
++ }
+
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
+- s->prob.p.mv_comp[i].class0 =
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.mv_comp[i].class0 =
+ (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
++ s->prob.p.mv_comp[i].class0 =
++ s->prob_raw.p.mv_comp[i].class0;
++ }
+
+ for (j = 0; j < 10; j++)
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
+- s->prob.p.mv_comp[i].bits[j] =
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.mv_comp[i].bits[j] =
+ (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
++ s->prob.p.mv_comp[i].bits[j] =
++ s->prob_raw.p.mv_comp[i].bits[j];
++ }
+ }
+
+ for (i = 0; i < 2; i++) {
+ for (j = 0; j < 2; j++)
+ for (k = 0; k < 3; k++)
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
+- s->prob.p.mv_comp[i].class0_fp[j][k] =
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.mv_comp[i].class0_fp[j][k] =
+ (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
++ s->prob.p.mv_comp[i].class0_fp[j][k] =
++ s->prob_raw.p.mv_comp[i].class0_fp[j][k];
++ }
+
+ for (j = 0; j < 3; j++)
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
+- s->prob.p.mv_comp[i].fp[j] =
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.mv_comp[i].fp[j] =
+ (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
++ s->prob.p.mv_comp[i].fp[j] =
++ s->prob_raw.p.mv_comp[i].fp[j];
++ }
+ }
+
+ if (s->s.h.highprecisionmvs) {
+ for (i = 0; i < 2; i++) {
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
+- s->prob.p.mv_comp[i].class0_hp =
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.mv_comp[i].class0_hp =
+ (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
++ s->prob.p.mv_comp[i].class0_hp =
++ s->prob_raw.p.mv_comp[i].class0_hp;
++ }
+
+- if (vpx_rac_get_prob_branchy(&s->c, 252))
+- s->prob.p.mv_comp[i].hp =
++ if (vpx_rac_get_prob_branchy(&s->c, 252)) {
++ s->prob_raw.p.mv_comp[i].hp =
+ (vp89_rac_get_uint(&s->c, 7) << 1) | 1;
++ s->prob.p.mv_comp[i].hp =
++ s->prob_raw.p.mv_comp[i].hp;
++ }
+ }
+ }
+ }
+@@ -1912,6 +1991,9 @@ const FFCodec ff_vp9_decoder = {
+ #if CONFIG_VP9_VDPAU_HWACCEL
+ HWACCEL_VDPAU(vp9),
+ #endif
++#if CONFIG_VP9_V4L2REQUEST_HWACCEL
++ HWACCEL_V4L2REQUEST(vp9),
++#endif
+ #if CONFIG_VP9_VIDEOTOOLBOX_HWACCEL
+ HWACCEL_VIDEOTOOLBOX(vp9),
+ #endif
+diff --git a/libavcodec/vp9dec.h b/libavcodec/vp9dec.h
+index de7aba0..5935ba6 100644
+--- a/libavcodec/vp9dec.h
++++ b/libavcodec/vp9dec.h
+@@ -135,6 +135,10 @@ typedef struct VP9Context {
+ ProbContext p;
+ uint8_t coef[4][2][2][6][6][11];
+ } prob;
++ struct {
++ ProbContext p;
++ uint8_t coef[4][2][2][6][6][11];
++ } prob_raw;
+
+ // contextual (above) cache
+ uint8_t *above_partition_ctx;
+diff --git a/libavcodec/vp9shared.h b/libavcodec/vp9shared.h
+index e54f235..b8fef7d 100644
+--- a/libavcodec/vp9shared.h
++++ b/libavcodec/vp9shared.h
+@@ -136,6 +136,7 @@ typedef struct VP9BitstreamHeader {
+ uint8_t temporal;
+ uint8_t absolute_vals;
+ uint8_t update_map;
++ uint8_t update_data;
+ uint8_t prob[7];
+ uint8_t pred_prob[3];
+ struct {
+--
+2.44.0
+
+
+From fdfd56866f020df1e333af7e8beb47b19b1c4ad0 Mon Sep 17 00:00:00 2001
+From: Jonas Karlman <jonas at kwiboo.se>
+Date: Mon, 29 Apr 2019 22:08:59 +0000
+Subject: [PATCH 09/18] HACK: hwcontext_drm: do not require drm device
+
+Signed-off-by: Jonas Karlman <jonas at kwiboo.se>
+---
+ libavutil/hwcontext_drm.c | 5 +++++
+ 1 file changed, 5 insertions(+)
+
+diff --git a/libavutil/hwcontext_drm.c b/libavutil/hwcontext_drm.c
+index 7a9fdbd..6297d1f 100644
+--- a/libavutil/hwcontext_drm.c
++++ b/libavutil/hwcontext_drm.c
+@@ -53,6 +53,11 @@ static int drm_device_create(AVHWDeviceContext *hwdev, const char *device,
+ AVDRMDeviceContext *hwctx = hwdev->hwctx;
+ drmVersionPtr version;
+
++ if (device == NULL) {
++ hwctx->fd = -1;
++ return 0;
++ }
++
+ hwctx->fd = open(device, O_RDWR);
+ if (hwctx->fd < 0)
+ return AVERROR(errno);
+--
+2.44.0
+
+
+From 38d09d83e6131bca6473674bf5979a13c979278b Mon Sep 17 00:00:00 2001
+From: Jonas Karlman <jonas at kwiboo.se>
+Date: Fri, 15 May 2020 16:54:05 +0000
+Subject: [PATCH 10/18] WIP: add NV15 and NV20 support
+
+Signed-off-by: Jonas Karlman <jonas at kwiboo.se>
+---
+ libavcodec/h264_slice.c | 9 ++++++---
+ libavcodec/v4l2_request.c | 7 +++++++
+ 2 files changed, 13 insertions(+), 3 deletions(-)
+
+diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c
+index 236bdc6..75f8235 100644
+--- a/libavcodec/h264_slice.c
++++ b/libavcodec/h264_slice.c
+@@ -812,6 +812,9 @@ static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
+ #endif
+ #if CONFIG_H264_VULKAN_HWACCEL
+ *fmt++ = AV_PIX_FMT_VULKAN;
++#endif
++#if CONFIG_H264_V4L2REQUEST_HWACCEL
++ *fmt++ = AV_PIX_FMT_DRM_PRIME;
+ #endif
+ if (CHROMA444(h)) {
+ if (h->avctx->colorspace == AVCOL_SPC_RGB) {
+@@ -868,6 +871,9 @@ static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
+ #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
+ if (h->avctx->colorspace != AVCOL_SPC_RGB)
+ *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
++#endif
++#if CONFIG_H264_V4L2REQUEST_HWACCEL
++ *fmt++ = AV_PIX_FMT_DRM_PRIME;
+ #endif
+ if (CHROMA444(h)) {
+ if (h->avctx->colorspace == AVCOL_SPC_RGB)
+@@ -891,9 +897,6 @@ static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
+ #endif
+ #if CONFIG_H264_VAAPI_HWACCEL
+ *fmt++ = AV_PIX_FMT_VAAPI;
+-#endif
+-#if CONFIG_H264_V4L2REQUEST_HWACCEL
+- *fmt++ = AV_PIX_FMT_DRM_PRIME;
+ #endif
+ if (h->avctx->color_range == AVCOL_RANGE_JPEG)
+ *fmt++ = AV_PIX_FMT_YUVJ420P;
+diff --git a/libavcodec/v4l2_request.c b/libavcodec/v4l2_request.c
+index 595a53e..8d2a248 100644
+--- a/libavcodec/v4l2_request.c
++++ b/libavcodec/v4l2_request.c
+@@ -196,6 +196,13 @@ static const struct {
+ } v4l2_request_capture_pixelformats[] = {
+ { V4L2_PIX_FMT_NV12, AV_PIX_FMT_NV12, DRM_FORMAT_NV12, DRM_FORMAT_MOD_LINEAR },
+ { V4L2_PIX_FMT_SUNXI_TILED_NV12, AV_PIX_FMT_NV12, DRM_FORMAT_NV12, DRM_FORMAT_MOD_ALLWINNER_TILED },
++#if defined(V4L2_PIX_FMT_NV15) && defined(DRM_FORMAT_NV15)
++ { V4L2_PIX_FMT_NV15, AV_PIX_FMT_NONE, DRM_FORMAT_NV15, DRM_FORMAT_MOD_LINEAR },
++#endif
++ { V4L2_PIX_FMT_NV16, AV_PIX_FMT_NV16, DRM_FORMAT_NV16, DRM_FORMAT_MOD_LINEAR },
++#if defined(V4L2_PIX_FMT_NV20) && defined(DRM_FORMAT_NV20)
++ { V4L2_PIX_FMT_NV20, AV_PIX_FMT_NONE, DRM_FORMAT_NV20, DRM_FORMAT_MOD_LINEAR },
++#endif
+ };
+
+ static int v4l2_request_set_drm_descriptor(V4L2RequestDescriptor *req, struct v4l2_format *format)
+--
+2.44.0
+
+
+From 71a1f3f8cb5d81fa8b8239bc302dc27580558550 Mon Sep 17 00:00:00 2001
+From: Jernej Skrabec <jernej.skrabec at gmail.com>
+Date: Mon, 9 Oct 2023 22:19:18 +0200
+Subject: [PATCH 11/18] v4l2 request api: add new codecs
+
+---
+ libavcodec/v4l2_request.c | 32 ++++++++++++++++++++++++++++----
+ 1 file changed, 28 insertions(+), 4 deletions(-)
+
+diff --git a/libavcodec/v4l2_request.c b/libavcodec/v4l2_request.c
+index 8d2a248..813c1b3 100644
+--- a/libavcodec/v4l2_request.c
++++ b/libavcodec/v4l2_request.c
+@@ -202,6 +202,27 @@ static const struct {
+ { V4L2_PIX_FMT_NV16, AV_PIX_FMT_NV16, DRM_FORMAT_NV16, DRM_FORMAT_MOD_LINEAR },
+ #if defined(V4L2_PIX_FMT_NV20) && defined(DRM_FORMAT_NV20)
+ { V4L2_PIX_FMT_NV20, AV_PIX_FMT_NONE, DRM_FORMAT_NV20, DRM_FORMAT_MOD_LINEAR },
++#endif
++ { V4L2_PIX_FMT_P010, AV_PIX_FMT_P010, DRM_FORMAT_P010, DRM_FORMAT_MOD_LINEAR },
++#if defined(V4L2_PIX_FMT_YUV420_10_AFBC_16X16_SPLIT)
++ {
++ .pixelformat = V4L2_PIX_FMT_YUV420_10_AFBC_16X16_SPLIT,
++ .sw_format = AV_PIX_FMT_NONE,
++ .drm_format = DRM_FORMAT_YUV420_10BIT,
++ .format_modifier = DRM_FORMAT_MOD_ARM_AFBC(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16 |
++ AFBC_FORMAT_MOD_SPARSE |
++ AFBC_FORMAT_MOD_SPLIT),
++ },
++#endif
++#if defined(V4L2_PIX_FMT_YUV420_8_AFBC_16X16_SPLIT)
++ {
++ .pixelformat = V4L2_PIX_FMT_YUV420_8_AFBC_16X16_SPLIT,
++ .sw_format = AV_PIX_FMT_NONE,
++ .drm_format = DRM_FORMAT_YUV420_8BIT,
++ .format_modifier = DRM_FORMAT_MOD_ARM_AFBC(AFBC_FORMAT_MOD_BLOCK_SIZE_16x16 |
++ AFBC_FORMAT_MOD_SPARSE |
++ AFBC_FORMAT_MOD_SPLIT),
++ },
+ #endif
+ };
+
+@@ -228,15 +249,18 @@ static int v4l2_request_set_drm_descriptor(V4L2RequestDescriptor *req, struct v4
+ desc->objects[0].size = req->capture.size;
+
+ desc->nb_layers = 1;
+- layer->nb_planes = 2;
++ layer->nb_planes = 1;
+
+ layer->planes[0].object_index = 0;
+ layer->planes[0].offset = 0;
+ layer->planes[0].pitch = V4L2_TYPE_IS_MULTIPLANAR(format->type) ? format->fmt.pix_mp.plane_fmt[0].bytesperline : format->fmt.pix.bytesperline;
+
+- layer->planes[1].object_index = 0;
+- layer->planes[1].offset = layer->planes[0].pitch * (V4L2_TYPE_IS_MULTIPLANAR(format->type) ? format->fmt.pix_mp.height : format->fmt.pix.height);
+- layer->planes[1].pitch = layer->planes[0].pitch;
++ if (!fourcc_mod_is_vendor(desc->objects[0].format_modifier, ARM)) {
++ layer->nb_planes = 2;
++ layer->planes[1].object_index = 0;
++ layer->planes[1].offset = layer->planes[0].pitch * (V4L2_TYPE_IS_MULTIPLANAR(format->type) ? format->fmt.pix_mp.height : format->fmt.pix.height);
++ layer->planes[1].pitch = layer->planes[0].pitch;
++ }
+
+ return 0;
+ }
+--
+2.44.0
+
+
+From b6ec3a75b3ebaae839257c5a6de8bbdd0fdc237c Mon Sep 17 00:00:00 2001
+From: Jonas Karlman <jonas at kwiboo.se>
+Date: Sat, 4 Nov 2023 21:14:21 +0000
+Subject: [PATCH 12/18] WIP: libavutil: add NV15 and NV20 pix fmt
+
+---
+ libavutil/pixdesc.c | 24 ++++++++++++++++++++++++
+ libavutil/pixfmt.h | 3 +++
+ 2 files changed, 27 insertions(+)
+
+diff --git a/libavutil/pixdesc.c b/libavutil/pixdesc.c
+index 6ded946..9747b5a 100644
+--- a/libavutil/pixdesc.c
++++ b/libavutil/pixdesc.c
+@@ -584,6 +584,18 @@ static const AVPixFmtDescriptor av_pix_fmt_descriptors[AV_PIX_FMT_NB] = {
+ },
+ .flags = AV_PIX_FMT_FLAG_PLANAR,
+ },
++ [AV_PIX_FMT_NV15] = {
++ .name = "nv15",
++ .nb_components = 3,
++ .log2_chroma_w = 1,
++ .log2_chroma_h = 1,
++ .comp = {
++ { 0, 1, 0, 0, 10 }, /* Y */
++ { 1, 2, 0, 0, 10 }, /* U */
++ { 1, 2, 1, 0, 10 }, /* V */
++ },
++ .flags = AV_PIX_FMT_FLAG_PLANAR,
++ },
+ [AV_PIX_FMT_ARGB] = {
+ .name = "argb",
+ .nb_components = 4,
+@@ -2085,6 +2097,18 @@ static const AVPixFmtDescriptor av_pix_fmt_descriptors[AV_PIX_FMT_NB] = {
+ },
+ .flags = AV_PIX_FMT_FLAG_PLANAR,
+ },
++ [AV_PIX_FMT_NV20NP] = {
++ .name = "nv20",
++ .nb_components = 3,
++ .log2_chroma_w = 1,
++ .log2_chroma_h = 0,
++ .comp = {
++ { 0, 1, 0, 0, 10 }, /* Y */
++ { 1, 2, 0, 0, 10 }, /* U */
++ { 1, 2, 1, 0, 10 }, /* V */
++ },
++ .flags = AV_PIX_FMT_FLAG_PLANAR,
++ },
+ [AV_PIX_FMT_NV20LE] = {
+ .name = "nv20le",
+ .nb_components = 3,
+diff --git a/libavutil/pixfmt.h b/libavutil/pixfmt.h
+index a26c72d..08e7aac 100644
+--- a/libavutil/pixfmt.h
++++ b/libavutil/pixfmt.h
+@@ -429,6 +429,9 @@ enum AVPixelFormat {
+ AV_PIX_FMT_GBRAP14BE, ///< planar GBR 4:4:4:4 56bpp, big-endian
+ AV_PIX_FMT_GBRAP14LE, ///< planar GBR 4:4:4:4 56bpp, little-endian
+
++ AV_PIX_FMT_NV15, ///< packed planar YUV 4:2:0, 15bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (first component U and the following component V), no padding between components
++ AV_PIX_FMT_NV20NP, ///< packed planar YUV 4:2:2, 20bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (first component U and the following component V), no padding between components
++
+ AV_PIX_FMT_NB ///< number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of formats might differ between versions
+ };
+
+--
+2.44.0
+
+
+From 5f3f0bf02e8728a9d36050260055feaabdccf687 Mon Sep 17 00:00:00 2001
+From: Jonas Karlman <jonas at kwiboo.se>
+Date: Sat, 4 Nov 2023 22:35:38 +0000
+Subject: [PATCH 13/18] WIP: libavutil: force NV15 and NV20 linesize
+
+---
+ libavutil/imgutils.c | 2 ++
+ libavutil/pixdesc.c | 4 ++--
+ libavutil/pixdesc.h | 5 +++++
+ 3 files changed, 9 insertions(+), 2 deletions(-)
+
+diff --git a/libavutil/imgutils.c b/libavutil/imgutils.c
+index da38126..0cd8d3d 100644
+--- a/libavutil/imgutils.c
++++ b/libavutil/imgutils.c
+@@ -70,6 +70,8 @@ int image_get_linesize(int width, int plane,
+
+ if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM)
+ linesize = (linesize + 7) >> 3;
++ if (desc->flags & AV_PIX_FMT_FLAG_10BIT_NO_PADDING)
++ linesize = linesize * 5 / 4;
+ return linesize;
+ }
+
+diff --git a/libavutil/pixdesc.c b/libavutil/pixdesc.c
+index 9747b5a..42c4acd 100644
+--- a/libavutil/pixdesc.c
++++ b/libavutil/pixdesc.c
+@@ -594,7 +594,7 @@ static const AVPixFmtDescriptor av_pix_fmt_descriptors[AV_PIX_FMT_NB] = {
+ { 1, 2, 0, 0, 10 }, /* U */
+ { 1, 2, 1, 0, 10 }, /* V */
+ },
+- .flags = AV_PIX_FMT_FLAG_PLANAR,
++ .flags = AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_10BIT_NO_PADDING,
+ },
+ [AV_PIX_FMT_ARGB] = {
+ .name = "argb",
+@@ -2107,7 +2107,7 @@ static const AVPixFmtDescriptor av_pix_fmt_descriptors[AV_PIX_FMT_NB] = {
+ { 1, 2, 0, 0, 10 }, /* U */
+ { 1, 2, 1, 0, 10 }, /* V */
+ },
+- .flags = AV_PIX_FMT_FLAG_PLANAR,
++ .flags = AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_10BIT_NO_PADDING,
+ },
+ [AV_PIX_FMT_NV20LE] = {
+ .name = "nv20le",
+diff --git a/libavutil/pixdesc.h b/libavutil/pixdesc.h
+index 0df73e6..73714c2 100644
+--- a/libavutil/pixdesc.h
++++ b/libavutil/pixdesc.h
+@@ -157,6 +157,11 @@ typedef struct AVPixFmtDescriptor {
+ */
+ #define AV_PIX_FMT_FLAG_FLOAT (1 << 9)
+
++/**
++ * HACK: force use of linesize = linesize * 5 / 4
++ */
++#define AV_PIX_FMT_FLAG_10BIT_NO_PADDING (1 << 10)
++
+ /**
+ * Return the number of bits per pixel used by the pixel format
+ * described by pixdesc. Note that this is not the same as the number
+--
+2.44.0
+
+
+From c941547fb6068b09140ad618f7cea7fcb2806189 Mon Sep 17 00:00:00 2001
+From: Jonas Karlman <jonas at kwiboo.se>
+Date: Sat, 4 Nov 2023 21:14:21 +0000
+Subject: [PATCH 14/18] WIP: libswscale: add NV15 and NV20 unscaled conversion
+
+---
+ libswscale/swscale_unscaled.c | 55 +++++++++++++++++++++++++++++++++++
+ libswscale/utils.c | 2 ++
+ 2 files changed, 57 insertions(+)
+
+diff --git a/libswscale/swscale_unscaled.c b/libswscale/swscale_unscaled.c
+index a5c9917..5d054c0 100644
+--- a/libswscale/swscale_unscaled.c
++++ b/libswscale/swscale_unscaled.c
+@@ -221,6 +221,56 @@ static int nv24ToPlanarWrapper(SwsContext *c, const uint8_t *src[],
+ return srcSliceH;
+ }
+
++static int nv15ToPlanarWrapper(SwsContext *c, const uint8_t *src[],
++ int srcStride[], int srcSliceY,
++ int srcSliceH, uint8_t *dstParam[],
++ int dstStride[])
++{
++ const AVPixFmtDescriptor *dst_format = av_pix_fmt_desc_get(c->dstFormat);
++ int vsub = 1 << dst_format->log2_chroma_h;
++ uint16_t *dstY = (uint16_t*)(dstParam[0] + dstStride[0] * srcSliceY);
++ uint16_t *dstU = (uint16_t*)(dstParam[1] + dstStride[1] * srcSliceY / vsub);
++ uint16_t *dstV = (uint16_t*)(dstParam[2] + dstStride[2] * srcSliceY / vsub);
++ int x, y;
++
++ const int shift[3] = {
++ dst_format->comp[0].shift,
++ dst_format->comp[1].shift,
++ dst_format->comp[2].shift,
++ };
++
++ for (y = srcSliceH; y > 0; y--) {
++ const uint8_t *tsrcY = src[0];
++ uint16_t *tdstY = dstY;
++ for (x = c->srcW / 4; x > 0; x--) {
++ *tdstY++ = (((tsrcY[1] & 0x3) << 8) | (tsrcY[0] & 0xff)) << shift[0];
++ *tdstY++ = (((tsrcY[2] & 0xf) << 6) | ((tsrcY[1] >> 2) & 0x3f)) << shift[0];
++ *tdstY++ = (((tsrcY[3] & 0x3f) << 4) | ((tsrcY[2] >> 4) & 0xf)) << shift[0];
++ *tdstY++ = (((tsrcY[4] & 0xff) << 2) | ((tsrcY[3] >> 6) & 0x3)) << shift[0];
++ tsrcY += 5;
++ }
++ src[0] += srcStride[0];
++ dstY += dstStride[0] / sizeof(uint16_t);
++ }
++
++ for (y = srcSliceH / vsub; y > 0; y--) {
++ const uint8_t *tsrcUV = src[1];
++ uint16_t *tdstU = dstU, *tdstV = dstV;
++ for (x = c->chrSrcW / 2; x > 0; x--) {
++ *tdstU++ = (((tsrcUV[1] & 0x3) << 8) | (tsrcUV[0] & 0xff)) << shift[1];
++ *tdstV++ = (((tsrcUV[2] & 0xf) << 6) | ((tsrcUV[1] >> 2) & 0x3f)) << shift[2];
++ *tdstU++ = (((tsrcUV[3] & 0x3f) << 4) | ((tsrcUV[2] >> 4) & 0xf)) << shift[1];
++ *tdstV++ = (((tsrcUV[4] & 0xff) << 2) | ((tsrcUV[3] >> 6) & 0x3)) << shift[2];
++ tsrcUV += 5;
++ }
++ src[1] += srcStride[1];
++ dstU += dstStride[1] / sizeof(uint16_t);
++ dstV += dstStride[2] / sizeof(uint16_t);
++ }
++
++ return srcSliceH;
++}
++
+ static int planarToP01xWrapper(SwsContext *c, const uint8_t *src8[],
+ int srcStride[], int srcSliceY,
+ int srcSliceH, uint8_t *dstParam8[],
+@@ -2004,6 +2054,11 @@ void ff_get_unscaled_swscale(SwsContext *c)
+ (srcFormat == AV_PIX_FMT_NV24 || srcFormat == AV_PIX_FMT_NV42)) {
+ c->convert_unscaled = nv24ToPlanarWrapper;
+ }
++ /* nv15_to_yuv420p10 */
++ if ((dstFormat == AV_PIX_FMT_YUV420P10 && srcFormat == AV_PIX_FMT_NV15) ||
++ (dstFormat == AV_PIX_FMT_YUV422P10 && srcFormat == AV_PIX_FMT_NV20NP)) {
++ c->convert_unscaled = nv15ToPlanarWrapper;
++ }
+ /* yuv2bgr */
+ if ((srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUV422P ||
+ srcFormat == AV_PIX_FMT_YUVA420P) && isAnyRGB(dstFormat) &&
+diff --git a/libswscale/utils.c b/libswscale/utils.c
+index e1ad685..a878456 100644
+--- a/libswscale/utils.c
++++ b/libswscale/utils.c
+@@ -267,6 +267,8 @@ static const FormatEntry format_entries[] = {
+ [AV_PIX_FMT_RGBAF16LE] = { 1, 0 },
+ [AV_PIX_FMT_XV30LE] = { 1, 1 },
+ [AV_PIX_FMT_XV36LE] = { 1, 1 },
++ [AV_PIX_FMT_NV15] = { 1, 0 },
++ [AV_PIX_FMT_NV20NP] = { 1, 0 },
+ };
+
+ int ff_shuffle_filter_coefficients(SwsContext *c, int *filterPos,
+--
+2.44.0
+
+
+From 49ff7fcf6ae1de10007f529c979f7478f2d7a451 Mon Sep 17 00:00:00 2001
+From: Jonas Karlman <jonas at kwiboo.se>
+Date: Sat, 11 Nov 2023 21:27:42 +0000
+Subject: [PATCH 15/18] WIP: v4l2-request: use NV15 and NV20 pix fmt
+
+---
+ libavcodec/v4l2_request.c | 4 ++--
+ 1 file changed, 2 insertions(+), 2 deletions(-)
+
+diff --git a/libavcodec/v4l2_request.c b/libavcodec/v4l2_request.c
+index 813c1b3..bf1e3c4 100644
+--- a/libavcodec/v4l2_request.c
++++ b/libavcodec/v4l2_request.c
+@@ -197,11 +197,11 @@ static const struct {
+ { V4L2_PIX_FMT_NV12, AV_PIX_FMT_NV12, DRM_FORMAT_NV12, DRM_FORMAT_MOD_LINEAR },
+ { V4L2_PIX_FMT_SUNXI_TILED_NV12, AV_PIX_FMT_NV12, DRM_FORMAT_NV12, DRM_FORMAT_MOD_ALLWINNER_TILED },
+ #if defined(V4L2_PIX_FMT_NV15) && defined(DRM_FORMAT_NV15)
+- { V4L2_PIX_FMT_NV15, AV_PIX_FMT_NONE, DRM_FORMAT_NV15, DRM_FORMAT_MOD_LINEAR },
++ { V4L2_PIX_FMT_NV15, AV_PIX_FMT_NV15, DRM_FORMAT_NV15, DRM_FORMAT_MOD_LINEAR },
+ #endif
+ { V4L2_PIX_FMT_NV16, AV_PIX_FMT_NV16, DRM_FORMAT_NV16, DRM_FORMAT_MOD_LINEAR },
+ #if defined(V4L2_PIX_FMT_NV20) && defined(DRM_FORMAT_NV20)
+- { V4L2_PIX_FMT_NV20, AV_PIX_FMT_NONE, DRM_FORMAT_NV20, DRM_FORMAT_MOD_LINEAR },
++ { V4L2_PIX_FMT_NV20, AV_PIX_FMT_NV20NP, DRM_FORMAT_NV20, DRM_FORMAT_MOD_LINEAR },
+ #endif
+ { V4L2_PIX_FMT_P010, AV_PIX_FMT_P010, DRM_FORMAT_P010, DRM_FORMAT_MOD_LINEAR },
+ #if defined(V4L2_PIX_FMT_YUV420_10_AFBC_16X16_SPLIT)
+--
+2.44.0
+
+
+From 0db39ac91179f6893ecaede6936fb12f3e642306 Mon Sep 17 00:00:00 2001
+From: Jonas Karlman <jonas at kwiboo.se>
+Date: Sat, 4 Nov 2023 19:13:43 +0000
+Subject: [PATCH 16/18] WIP: v4l2-request: change to autodetect
+
+fixes a review note from prior upstreaming attempt
+---
+ configure | 4 ++--
+ 1 file changed, 2 insertions(+), 2 deletions(-)
+
+diff --git a/configure b/configure
+index 810e57c..bde3d46 100755
+--- a/configure
++++ b/configure
+@@ -348,7 +348,7 @@ External library support:
+ --enable-omx-rpi enable OpenMAX IL code for Raspberry Pi [no]
+ --enable-rkmpp enable Rockchip Media Process Platform code [no]
+ --disable-v4l2-m2m disable V4L2 mem2mem code [autodetect]
+- --enable-v4l2-request enable V4L2 request API code [no]
++ --disable-v4l2-request disable V4L2 request API code [autodetect]
+ --disable-vaapi disable Video Acceleration API (mainly Unix/Intel) code [autodetect]
+ --disable-vdpau disable Nvidia Video Decode and Presentation API for Unix code [autodetect]
+ --disable-videotoolbox disable VideoToolbox code [autodetect]
+@@ -1934,6 +1934,7 @@ HWACCEL_AUTODETECT_LIBRARY_LIST="
+ videotoolbox
+ vulkan
+ v4l2_m2m
++ v4l2_request
+ "
+
+ # catchall list of things that require external libs to link
+@@ -1955,7 +1956,6 @@ HWACCEL_LIBRARY_LIST="
+ mmal
+ omx
+ opencl
+- v4l2_request
+ "
+
+ DOCUMENT_LIST="
+--
+2.44.0
+
+
+From 36a27318e0f280b448fb5ab6e59f4151f6001517 Mon Sep 17 00:00:00 2001
+From: Jonas Karlman <jonas at kwiboo.se>
+Date: Sat, 4 Nov 2023 21:13:27 +0000
+Subject: [PATCH 17/18] libswscale: fix NV16 unscaled conversion
+
+---
+ libswscale/swscale_unscaled.c | 13 +++++++++++--
+ 1 file changed, 11 insertions(+), 2 deletions(-)
+
+diff --git a/libswscale/swscale_unscaled.c b/libswscale/swscale_unscaled.c
+index 5d054c0..833c2c6 100644
+--- a/libswscale/swscale_unscaled.c
++++ b/libswscale/swscale_unscaled.c
+@@ -190,7 +190,7 @@ static int planarToNv24Wrapper(SwsContext *c, const uint8_t *src[],
+ copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
+ dstParam[0], dstStride[0]);
+
+- if (c->dstFormat == AV_PIX_FMT_NV24)
++ if (c->dstFormat == AV_PIX_FMT_NV24 || c->dstFormat == AV_PIX_FMT_NV16)
+ interleaveBytes(src[1], src[2], dst, c->chrSrcW, srcSliceH,
+ srcStride[1], srcStride[2], dstStride[1]);
+ else
+@@ -211,7 +211,7 @@ static int nv24ToPlanarWrapper(SwsContext *c, const uint8_t *src[],
+ copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
+ dstParam[0], dstStride[0]);
+
+- if (c->srcFormat == AV_PIX_FMT_NV24)
++ if (c->srcFormat == AV_PIX_FMT_NV24 || c->srcFormat == AV_PIX_FMT_NV16)
+ deinterleaveBytes(src[1], dst1, dst2, c->chrSrcW, srcSliceH,
+ srcStride[1], dstStride[1], dstStride[2]);
+ else
+@@ -2039,6 +2039,11 @@ void ff_get_unscaled_swscale(SwsContext *c)
+ (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)) {
+ c->convert_unscaled = planarToNv12Wrapper;
+ }
++ /* yv16_to_nv16 */
++ if ((srcFormat == AV_PIX_FMT_YUV422P || srcFormat == AV_PIX_FMT_YUVA422P) &&
++ dstFormat == AV_PIX_FMT_NV16) {
++ c->convert_unscaled = planarToNv24Wrapper;
++ }
+ /* yv24_to_nv24 */
+ if ((srcFormat == AV_PIX_FMT_YUV444P || srcFormat == AV_PIX_FMT_YUVA444P) &&
+ (dstFormat == AV_PIX_FMT_NV24 || dstFormat == AV_PIX_FMT_NV42)) {
+@@ -2049,6 +2054,10 @@ void ff_get_unscaled_swscale(SwsContext *c)
+ (srcFormat == AV_PIX_FMT_NV12 || srcFormat == AV_PIX_FMT_NV21)) {
+ c->convert_unscaled = nv12ToPlanarWrapper;
+ }
++ /* nv16_to_yv16 */
++ if (dstFormat == AV_PIX_FMT_YUV422P && srcFormat == AV_PIX_FMT_NV16) {
++ c->convert_unscaled = nv24ToPlanarWrapper;
++ }
+ /* nv24_to_yv24 */
+ if (dstFormat == AV_PIX_FMT_YUV444P &&
+ (srcFormat == AV_PIX_FMT_NV24 || srcFormat == AV_PIX_FMT_NV42)) {
+--
+2.44.0
+
+
+From 502d19c9564969538952051c2d395c714e41e567 Mon Sep 17 00:00:00 2001
+From: Jonas Karlman <jonas at kwiboo.se>
+Date: Sun, 31 Dec 2023 08:56:14 +0000
+Subject: [PATCH 18/18] WIP: limit v4l2 udev enum sysname match
+
+---
+ libavcodec/v4l2_request.c | 4 ++++
+ 1 file changed, 4 insertions(+)
+
+diff --git a/libavcodec/v4l2_request.c b/libavcodec/v4l2_request.c
+index bf1e3c4..e249dea 100644
+--- a/libavcodec/v4l2_request.c
++++ b/libavcodec/v4l2_request.c
+@@ -764,6 +764,7 @@ int ff_v4l2_request_init(AVCodecContext *avctx, uint32_t pixelformat, uint32_t b
+ struct udev_list_entry *devices;
+ struct udev_list_entry *entry;
+ struct udev_device *device;
++ const char *sysname;
+
+ av_log(avctx, AV_LOG_DEBUG, "%s: ctx=%p hw_device_ctx=%p hw_frames_ctx=%p\n", __func__, ctx, avctx->hw_device_ctx, avctx->hw_frames_ctx);
+
+@@ -786,6 +787,9 @@ int ff_v4l2_request_init(AVCodecContext *avctx, uint32_t pixelformat, uint32_t b
+ }
+
+ udev_enumerate_add_match_subsystem(enumerate, "media");
++ sysname = getenv("V4L2_SYSNAME");
++ if (sysname)
++ udev_enumerate_add_match_sysname(enumerate, sysname);
+ udev_enumerate_scan_devices(enumerate);
+
+ devices = udev_enumerate_get_list_entry(enumerate);
+--
+2.44.0
+
================================================================
---- gitweb:
http://git.pld-linux.org/gitweb.cgi/packages/ffmpeg.git/commitdiff/0068d4abd0aba2ae817c145777d8897c72ac7703
More information about the pld-cvs-commit
mailing list