1 From 5e154b0357829007566930846f880b9f5fb404d9 Mon Sep 17 00:00:00 2001
2 From: Jonas Karlman <jonas@kwiboo.se>
3 Date: Mon, 3 Dec 2018 23:48:04 +0100
4 Subject: [PATCH] avutil: add av_buffer_pool_flush()
6 Used by V4L2 request API hwaccel
8 Signed-off-by: Jonas Karlman <jonas@kwiboo.se>
10 libavutil/buffer.c | 13 +++++++++++++
11 libavutil/buffer.h | 5 +++++
12 2 files changed, 18 insertions(+)
14 diff --git a/libavutil/buffer.c b/libavutil/buffer.c
15 index 38a554208a90..b0fedabc3e7d 100644
16 --- a/libavutil/buffer.c
17 +++ b/libavutil/buffer.c
18 @@ -273,6 +273,19 @@ static void buffer_pool_free(AVBufferPool *pool)
22 +void av_buffer_pool_flush(AVBufferPool *pool)
24 + ff_mutex_lock(&pool->mutex);
25 + while (pool->pool) {
26 + BufferPoolEntry *buf = pool->pool;
27 + pool->pool = buf->next;
29 + buf->free(buf->opaque, buf->data);
32 + ff_mutex_unlock(&pool->mutex);
35 void av_buffer_pool_uninit(AVBufferPool **ppool)
38 diff --git a/libavutil/buffer.h b/libavutil/buffer.h
39 index c0f3f6cc9abe..998beec9ac5b 100644
40 --- a/libavutil/buffer.h
41 +++ b/libavutil/buffer.h
42 @@ -267,6 +267,11 @@ AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
43 AVBufferRef* (*alloc)(void *opaque, size_t size),
44 void (*pool_free)(void *opaque));
47 + * Free all available buffers in a buffer pool.
49 + void av_buffer_pool_flush(AVBufferPool *pool);
52 * Mark the pool as being available for freeing. It will actually be freed only
53 * once all the allocated buffers associated with the pool are released. Thus it
54 From 01319970d90ffd3c073b054a3d2e4784f6ebea0d Mon Sep 17 00:00:00 2001
55 From: Jonas Karlman <jonas@kwiboo.se>
56 Date: Sat, 15 Dec 2018 22:32:16 +0100
57 Subject: [PATCH] Add common V4L2 request API code
59 Signed-off-by: Jonas Karlman <jonas@kwiboo.se>
62 libavcodec/Makefile | 1 +
63 libavcodec/hwconfig.h | 2 +
64 libavcodec/v4l2_request.c | 984 ++++++++++++++++++++++++++++++++++++++
65 libavcodec/v4l2_request.h | 77 +++
66 5 files changed, 1076 insertions(+)
67 create mode 100644 libavcodec/v4l2_request.c
68 create mode 100644 libavcodec/v4l2_request.h
70 diff --git a/configure b/configure
71 index 8569a60bf827..9f9909a23696 100755
74 @@ -274,6 +274,7 @@ External library support:
75 --enable-libtls enable LibreSSL (via libtls), needed for https support
76 if openssl, gnutls or mbedtls is not used [no]
77 --enable-libtwolame enable MP2 encoding via libtwolame [no]
78 + --enable-libudev enable libudev [no]
79 --enable-libuavs3d enable AVS3 decoding via libuavs3d [no]
80 --enable-libv4l2 enable libv4l2/v4l-utils [no]
81 --enable-libvidstab enable video stabilization using vid.stab [no]
82 @@ -342,6 +343,7 @@ External library support:
83 --enable-omx-rpi enable OpenMAX IL code for Raspberry Pi [no]
84 --enable-rkmpp enable Rockchip Media Process Platform code [no]
85 --disable-v4l2-m2m disable V4L2 mem2mem code [autodetect]
86 + --enable-v4l2-request enable V4L2 request API code [no]
87 --disable-vaapi disable Video Acceleration API (mainly Unix/Intel) code [autodetect]
88 --disable-vdpau disable Nvidia Video Decode and Presentation API for Unix code [autodetect]
89 --disable-videotoolbox disable VideoToolbox code [autodetect]
90 @@ -1807,6 +1809,7 @@ EXTERNAL_LIBRARY_LIST="
98 @@ -1861,6 +1864,7 @@ HWACCEL_LIBRARY_LIST="
106 @@ -2903,6 +2907,7 @@ d3d11va_deps="dxva_h ID3D11VideoDecoder ID3D11VideoContext"
107 dxva2_deps="dxva2api_h DXVA2_ConfigPictureDecode ole32 user32"
108 ffnvcodec_deps_any="libdl LoadLibrary"
109 nvdec_deps="ffnvcodec"
110 +v4l2_request_deps="linux_videodev2_h linux_media_h v4l2_timeval_to_ns libdrm libudev"
111 vaapi_x11_deps="xlib_x11"
112 videotoolbox_hwaccel_deps="videotoolbox pthreads"
113 videotoolbox_hwaccel_extralibs="-framework QuartzCore"
114 @@ -6376,6 +6381,7 @@ enabled libtls && require_pkg_config libtls libtls tls.h tls_configur
115 enabled libtwolame && require libtwolame twolame.h twolame_init -ltwolame &&
116 { check_lib libtwolame twolame.h twolame_encode_buffer_float32_interleaved -ltwolame ||
117 die "ERROR: libtwolame must be installed and version must be >= 0.3.10"; }
118 +enabled libudev && require_pkg_config libudev libudev libudev.h udev_new
119 enabled libuavs3d && require_pkg_config libuavs3d "uavs3d >= 1.1.41" uavs3d.h uavs3d_decode
120 enabled libv4l2 && require_pkg_config libv4l2 libv4l2 libv4l2.h v4l2_ioctl
121 enabled libvidstab && require_pkg_config libvidstab "vidstab >= 0.98" vid.stab/libvidstab.h vsMotionDetectInit
122 @@ -6475,6 +6481,10 @@ enabled rkmpp && { require_pkg_config rkmpp rockchip_mpp rockchip/r
124 die "ERROR: rkmpp requires --enable-libdrm"; }
126 +enabled v4l2_request && { enabled libdrm ||
127 + die "ERROR: v4l2-request requires --enable-libdrm"; } &&
128 + { enabled libudev ||
129 + die "ERROR: v4l2-request requires --enable-libudev"; }
130 enabled vapoursynth && require_pkg_config vapoursynth "vapoursynth-script >= 42" VSScript.h vsscript_init
133 @@ -6556,6 +6566,8 @@ if enabled v4l2_m2m; then
134 check_cc vp9_v4l2_m2m linux/videodev2.h "int i = V4L2_PIX_FMT_VP9;"
137 +check_func_headers "linux/media.h linux/videodev2.h" v4l2_timeval_to_ns
139 check_headers sys/videoio.h
140 test_code cc sys/videoio.h "struct v4l2_frmsizeenum vfse; vfse.discrete.width = 0;" && enable_sanitized struct_v4l2_frmivalenum_discrete
142 diff --git a/libavcodec/Makefile b/libavcodec/Makefile
143 index 5a6ea59715af..d74220516826 100644
144 --- a/libavcodec/Makefile
145 +++ b/libavcodec/Makefile
146 @@ -153,6 +153,7 @@ OBJS-$(CONFIG_VP3DSP) += vp3dsp.o
147 OBJS-$(CONFIG_VP56DSP) += vp56dsp.o
148 OBJS-$(CONFIG_VP8DSP) += vp8dsp.o
149 OBJS-$(CONFIG_V4L2_M2M) += v4l2_m2m.o v4l2_context.o v4l2_buffers.o v4l2_fmt.o
150 +OBJS-$(CONFIG_V4L2_REQUEST) += v4l2_request.o
151 OBJS-$(CONFIG_WMA_FREQS) += wma_freqs.o
152 OBJS-$(CONFIG_WMV2DSP) += wmv2dsp.o
154 diff --git a/libavcodec/hwconfig.h b/libavcodec/hwconfig.h
155 index f421dc909f44..ee78d8ab8e89 100644
156 --- a/libavcodec/hwconfig.h
157 +++ b/libavcodec/hwconfig.h
158 @@ -80,6 +80,8 @@ typedef struct AVCodecHWConfigInternal {
159 HW_CONFIG_HWACCEL(0, 0, 1, D3D11VA_VLD, NONE, ff_ ## codec ## _d3d11va_hwaccel)
160 #define HWACCEL_XVMC(codec) \
161 HW_CONFIG_HWACCEL(0, 0, 1, XVMC, NONE, ff_ ## codec ## _xvmc_hwaccel)
162 +#define HWACCEL_V4L2REQUEST(codec) \
163 + HW_CONFIG_HWACCEL(1, 0, 0, DRM_PRIME, DRM, ff_ ## codec ## _v4l2request_hwaccel)
165 #define HW_CONFIG_ENCODER(device, frames, ad_hoc, format, device_type_) \
166 &(const AVCodecHWConfigInternal) { \
167 diff --git a/libavcodec/v4l2_request.c b/libavcodec/v4l2_request.c
169 index 000000000000..5234b5049b0d
171 +++ b/libavcodec/v4l2_request.c
174 + * This file is part of FFmpeg.
176 + * FFmpeg is free software; you can redistribute it and/or
177 + * modify it under the terms of the GNU Lesser General Public
178 + * License as published by the Free Software Foundation; either
179 + * version 2.1 of the License, or (at your option) any later version.
181 + * FFmpeg is distributed in the hope that it will be useful,
182 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
183 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
184 + * Lesser General Public License for more details.
186 + * You should have received a copy of the GNU Lesser General Public
187 + * License along with FFmpeg; if not, write to the Free Software
188 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
191 +#include <drm_fourcc.h>
192 +#include <linux/media.h>
193 +#include <sys/mman.h>
194 +#include <sys/types.h>
195 +#include <sys/stat.h>
198 +#include <sys/sysmacros.h>
199 +#include <libudev.h>
202 +#include "internal.h"
203 +#include "v4l2_request.h"
205 +uint64_t ff_v4l2_request_get_capture_timestamp(AVFrame *frame)
207 + V4L2RequestDescriptor *req = (V4L2RequestDescriptor*)frame->data[0];
208 + return req ? v4l2_timeval_to_ns(&req->capture.buffer.timestamp) : 0;
211 +int ff_v4l2_request_reset_frame(AVCodecContext *avctx, AVFrame *frame)
213 + V4L2RequestDescriptor *req = (V4L2RequestDescriptor*)frame->data[0];
214 + memset(&req->drm, 0, sizeof(AVDRMFrameDescriptor));
215 + req->output.used = 0;
219 +int ff_v4l2_request_append_output_buffer(AVCodecContext *avctx, AVFrame *frame, const uint8_t *data, uint32_t size)
221 + V4L2RequestDescriptor *req = (V4L2RequestDescriptor*)frame->data[0];
222 + if (req->output.used + size + (AV_INPUT_BUFFER_PADDING_SIZE * 4) <= req->output.size) {
223 + memcpy(req->output.addr + req->output.used, data, size);
224 + req->output.used += size;
226 + av_log(avctx, AV_LOG_ERROR, "%s: output.used=%u output.size=%u size=%u\n", __func__, req->output.used, req->output.size, size);
231 +static int v4l2_request_controls(V4L2RequestContext *ctx, int request_fd, unsigned long type, struct v4l2_ext_control *control, int count)
233 + struct v4l2_ext_controls controls = {
234 + .controls = control,
236 + .request_fd = request_fd,
237 + .which = (request_fd >= 0) ? V4L2_CTRL_WHICH_REQUEST_VAL : 0,
240 + if (!control || !count)
243 + return ioctl(ctx->video_fd, type, &controls);
246 +static int v4l2_request_set_controls(V4L2RequestContext *ctx, int request_fd, struct v4l2_ext_control *control, int count)
248 + return v4l2_request_controls(ctx, request_fd, VIDIOC_S_EXT_CTRLS, control, count);
251 +int ff_v4l2_request_set_controls(AVCodecContext *avctx, struct v4l2_ext_control *control, int count)
253 + V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
256 + ret = v4l2_request_controls(ctx, -1, VIDIOC_S_EXT_CTRLS, control, count);
258 + av_log(avctx, AV_LOG_ERROR, "%s: set controls failed, %s (%d)\n", __func__, strerror(errno), errno);
259 + return AVERROR(EINVAL);
265 +int ff_v4l2_request_get_controls(AVCodecContext *avctx, struct v4l2_ext_control *control, int count)
267 + V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
270 + ret = v4l2_request_controls(ctx, -1, VIDIOC_G_EXT_CTRLS, control, count);
272 + av_log(avctx, AV_LOG_ERROR, "%s: get controls failed, %s (%d)\n", __func__, strerror(errno), errno);
273 + return AVERROR(EINVAL);
279 +int ff_v4l2_request_query_control(AVCodecContext *avctx, struct v4l2_query_ext_ctrl *control)
282 + V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
284 + ret = ioctl(ctx->video_fd, VIDIOC_QUERY_EXT_CTRL, control);
286 + av_log(avctx, AV_LOG_ERROR, "%s: query control failed, %s (%d)\n", __func__, strerror(errno), errno);
287 + return AVERROR(EINVAL);
293 +int ff_v4l2_request_query_control_default_value(AVCodecContext *avctx, uint32_t id)
296 + V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
297 + struct v4l2_queryctrl control = {
301 + ret = ioctl(ctx->video_fd, VIDIOC_QUERYCTRL, &control);
303 + av_log(avctx, AV_LOG_ERROR, "%s: query control failed, %s (%d)\n", __func__, strerror(errno), errno);
304 + return AVERROR(EINVAL);
307 + return control.default_value;
310 +static int v4l2_request_queue_buffer(V4L2RequestContext *ctx, int request_fd, V4L2RequestBuffer *buf, uint32_t flags)
312 + struct v4l2_plane planes[1] = {};
313 + struct v4l2_buffer buffer = {
314 + .type = buf->buffer.type,
315 + .memory = buf->buffer.memory,
316 + .index = buf->index,
317 + .timestamp.tv_usec = ctx->timestamp,
318 + .bytesused = buf->used,
319 + .request_fd = request_fd,
320 + .flags = ((request_fd >= 0) ? V4L2_BUF_FLAG_REQUEST_FD : 0) | flags,
323 + buf->buffer.timestamp = buffer.timestamp;
325 + if (V4L2_TYPE_IS_MULTIPLANAR(buf->buffer.type)) {
326 + planes[0].bytesused = buf->used;
327 + buffer.bytesused = 0;
329 + buffer.m.planes = planes;
332 + return ioctl(ctx->video_fd, VIDIOC_QBUF, &buffer);
335 +static int v4l2_request_dequeue_buffer(V4L2RequestContext *ctx, V4L2RequestBuffer *buf)
338 + struct v4l2_plane planes[1] = {};
339 + struct v4l2_buffer buffer = {
340 + .type = buf->buffer.type,
341 + .memory = buf->buffer.memory,
342 + .index = buf->index,
345 + if (V4L2_TYPE_IS_MULTIPLANAR(buf->buffer.type)) {
347 + buffer.m.planes = planes;
350 + ret = ioctl(ctx->video_fd, VIDIOC_DQBUF, &buffer);
354 + buf->buffer.timestamp = buffer.timestamp;
358 +const uint32_t v4l2_request_capture_pixelformats[] = {
360 +#ifdef DRM_FORMAT_MOD_ALLWINNER_TILED
361 + V4L2_PIX_FMT_SUNXI_TILED_NV12,
365 +static int v4l2_request_set_drm_descriptor(V4L2RequestDescriptor *req, struct v4l2_format *format)
367 + AVDRMFrameDescriptor *desc = &req->drm;
368 + AVDRMLayerDescriptor *layer = &desc->layers[0];
369 + uint32_t pixelformat = V4L2_TYPE_IS_MULTIPLANAR(format->type) ? format->fmt.pix_mp.pixelformat : format->fmt.pix.pixelformat;
371 + switch (pixelformat) {
372 + case V4L2_PIX_FMT_NV12:
373 + layer->format = DRM_FORMAT_NV12;
374 + desc->objects[0].format_modifier = DRM_FORMAT_MOD_LINEAR;
376 +#ifdef DRM_FORMAT_MOD_ALLWINNER_TILED
377 + case V4L2_PIX_FMT_SUNXI_TILED_NV12:
378 + layer->format = DRM_FORMAT_NV12;
379 + desc->objects[0].format_modifier = DRM_FORMAT_MOD_ALLWINNER_TILED;
386 + desc->nb_objects = 1;
387 + desc->objects[0].fd = req->capture.fd;
388 + desc->objects[0].size = req->capture.size;
390 + desc->nb_layers = 1;
391 + layer->nb_planes = 2;
393 + layer->planes[0].object_index = 0;
394 + layer->planes[0].offset = 0;
395 + layer->planes[0].pitch = V4L2_TYPE_IS_MULTIPLANAR(format->type) ? format->fmt.pix_mp.plane_fmt[0].bytesperline : format->fmt.pix.bytesperline;
397 + layer->planes[1].object_index = 0;
398 + layer->planes[1].offset = layer->planes[0].pitch * (V4L2_TYPE_IS_MULTIPLANAR(format->type) ? format->fmt.pix_mp.height : format->fmt.pix.height);
399 + layer->planes[1].pitch = layer->planes[0].pitch;
404 +static int v4l2_request_queue_decode(AVCodecContext *avctx, AVFrame *frame, struct v4l2_ext_control *control, int count, int first_slice, int last_slice)
406 + V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
407 + V4L2RequestDescriptor *req = (V4L2RequestDescriptor*)frame->data[0];
408 + struct timeval tv = { 2, 0 };
412 + 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);
417 + ret = v4l2_request_set_controls(ctx, req->request_fd, control, count);
419 + av_log(avctx, AV_LOG_ERROR, "%s: set controls failed for request %d, %s (%d)\n", __func__, req->request_fd, strerror(errno), errno);
423 + memset(req->output.addr + req->output.used, 0, AV_INPUT_BUFFER_PADDING_SIZE * 4);
425 + ret = v4l2_request_queue_buffer(ctx, req->request_fd, &req->output, last_slice ? 0 : V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF);
427 + av_log(avctx, AV_LOG_ERROR, "%s: queue output buffer %d failed for request %d, %s (%d)\n", __func__, req->output.index, req->request_fd, strerror(errno), errno);
432 + ret = v4l2_request_queue_buffer(ctx, -1, &req->capture, 0);
434 + av_log(avctx, AV_LOG_ERROR, "%s: queue capture buffer %d failed for request %d, %s (%d)\n", __func__, req->capture.index, req->request_fd, strerror(errno), errno);
439 + // NOTE: do we need to dequeue when request fails/timeout?
441 + // 4. queue request and wait
442 + ret = ioctl(req->request_fd, MEDIA_REQUEST_IOC_QUEUE, NULL);
444 + av_log(avctx, AV_LOG_ERROR, "%s: queue request %d failed, %s (%d)\n", __func__, req->request_fd, strerror(errno), errno);
448 + FD_ZERO(&except_fds);
449 + FD_SET(req->request_fd, &except_fds);
451 + ret = select(req->request_fd + 1, NULL, NULL, &except_fds, &tv);
453 + av_log(avctx, AV_LOG_ERROR, "%s: request %d timeout\n", __func__, req->request_fd);
455 + } else if (ret < 0) {
456 + av_log(avctx, AV_LOG_ERROR, "%s: select request %d failed, %s (%d)\n", __func__, req->request_fd, strerror(errno), errno);
460 + ret = v4l2_request_dequeue_buffer(ctx, &req->output);
462 + av_log(avctx, AV_LOG_ERROR, "%s: dequeue output buffer %d failed for request %d, %s (%d)\n", __func__, req->output.index, req->request_fd, strerror(errno), errno);
467 + ret = v4l2_request_dequeue_buffer(ctx, &req->capture);
469 + av_log(avctx, AV_LOG_ERROR, "%s: dequeue capture buffer %d failed for request %d, %s (%d)\n", __func__, req->capture.index, req->request_fd, strerror(errno), errno);
474 + // TODO: check errors
475 + // buffer.flags & V4L2_BUF_FLAG_ERROR
477 + ret = ioctl(req->request_fd, MEDIA_REQUEST_IOC_REINIT, NULL);
479 + av_log(avctx, AV_LOG_ERROR, "%s: reinit request %d failed, %s (%d)\n", __func__, req->request_fd, strerror(errno), errno);
484 + return v4l2_request_set_drm_descriptor(req, &ctx->format);
489 + ret = v4l2_request_dequeue_buffer(ctx, &req->output);
491 + av_log(avctx, AV_LOG_ERROR, "%s: dequeue output buffer %d failed for request %d, %s (%d)\n", __func__, req->output.index, req->request_fd, strerror(errno), errno);
493 + ret = v4l2_request_dequeue_buffer(ctx, &req->capture);
495 + av_log(avctx, AV_LOG_ERROR, "%s: dequeue capture buffer %d failed for request %d, %s (%d)\n", __func__, req->capture.index, req->request_fd, strerror(errno), errno);
497 + ret = ioctl(req->request_fd, MEDIA_REQUEST_IOC_REINIT, NULL);
499 + av_log(avctx, AV_LOG_ERROR, "%s: reinit request %d failed, %s (%d)\n", __func__, req->request_fd, strerror(errno), errno);
504 +int ff_v4l2_request_decode_slice(AVCodecContext *avctx, AVFrame *frame, struct v4l2_ext_control *control, int count, int first_slice, int last_slice)
506 + V4L2RequestDescriptor *req = (V4L2RequestDescriptor*)frame->data[0];
508 + // fall back to queue each slice as a full frame
509 + if ((req->output.capabilities & V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF) != V4L2_BUF_CAP_SUPPORTS_M2M_HOLD_CAPTURE_BUF)
510 + return v4l2_request_queue_decode(avctx, frame, control, count, 1, 1);
512 + return v4l2_request_queue_decode(avctx, frame, control, count, first_slice, last_slice);
515 +int ff_v4l2_request_decode_frame(AVCodecContext *avctx, AVFrame *frame, struct v4l2_ext_control *control, int count)
517 + return v4l2_request_queue_decode(avctx, frame, control, count, 1, 1);
520 +static int v4l2_request_try_format(AVCodecContext *avctx, enum v4l2_buf_type type, uint32_t pixelformat)
522 + V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
523 + struct v4l2_fmtdesc fmtdesc = {
528 + if (V4L2_TYPE_IS_OUTPUT(type)) {
529 + struct v4l2_create_buffers buffers = {
531 + .memory = V4L2_MEMORY_MMAP,
532 + .format.type = type,
535 + if (ioctl(ctx->video_fd, VIDIOC_CREATE_BUFS, &buffers) < 0) {
536 + av_log(avctx, AV_LOG_ERROR, "%s: create buffers failed for type %u, %s (%d)\n", __func__, type, strerror(errno), errno);
540 + if ((buffers.capabilities & V4L2_BUF_CAP_SUPPORTS_REQUESTS) != V4L2_BUF_CAP_SUPPORTS_REQUESTS) {
541 + av_log(avctx, AV_LOG_INFO, "%s: output buffer type do not support requests, capabilities %u\n", __func__, buffers.capabilities);
546 + while (ioctl(ctx->video_fd, VIDIOC_ENUM_FMT, &fmtdesc) >= 0) {
547 + if (fmtdesc.pixelformat == pixelformat)
553 + av_log(avctx, AV_LOG_INFO, "%s: pixelformat %u not supported for type %u\n", __func__, pixelformat, type);
557 +static int v4l2_request_set_format(AVCodecContext *avctx, enum v4l2_buf_type type, uint32_t pixelformat, uint32_t buffersize)
559 + V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
560 + struct v4l2_format format = {
564 + if (V4L2_TYPE_IS_MULTIPLANAR(type)) {
565 + format.fmt.pix_mp.width = avctx->coded_width;
566 + format.fmt.pix_mp.height = avctx->coded_height;
567 + format.fmt.pix_mp.pixelformat = pixelformat;
568 + format.fmt.pix_mp.plane_fmt[0].sizeimage = buffersize;
569 + format.fmt.pix_mp.num_planes = 1;
571 + format.fmt.pix.width = avctx->coded_width;
572 + format.fmt.pix.height = avctx->coded_height;
573 + format.fmt.pix.pixelformat = pixelformat;
574 + format.fmt.pix.sizeimage = buffersize;
577 + return ioctl(ctx->video_fd, VIDIOC_S_FMT, &format);
580 +static int v4l2_request_select_capture_format(AVCodecContext *avctx)
582 + V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
583 + enum v4l2_buf_type type = ctx->format.type;
586 + struct v4l2_format format = {
589 + struct v4l2_fmtdesc fmtdesc = {
593 + uint32_t pixelformat;
596 + if (ioctl(ctx->video_fd, VIDIOC_G_FMT, &format) < 0) {
597 + av_log(avctx, AV_LOG_ERROR, "%s: get capture format failed, %s (%d)\n", __func__, strerror(errno), errno);
601 + pixelformat = V4L2_TYPE_IS_MULTIPLANAR(type) ? format.fmt.pix_mp.pixelformat : format.fmt.pix.pixelformat;
603 + for (i = 0; i < FF_ARRAY_ELEMS(v4l2_request_capture_pixelformats); i++) {
604 + if (pixelformat == v4l2_request_capture_pixelformats[i])
605 + return v4l2_request_set_format(avctx, type, pixelformat, 0);
608 + while (ioctl(ctx->video_fd, VIDIOC_ENUM_FMT, &fmtdesc) >= 0) {
609 + for (i = 0; i < FF_ARRAY_ELEMS(v4l2_request_capture_pixelformats); i++) {
610 + if (fmtdesc.pixelformat == v4l2_request_capture_pixelformats[i])
611 + return v4l2_request_set_format(avctx, type, fmtdesc.pixelformat, 0);
617 + for (int i = 0; i < FF_ARRAY_ELEMS(v4l2_request_capture_pixelformats); i++) {
618 + uint32_t pixelformat = v4l2_request_capture_pixelformats[i];
619 + if (!v4l2_request_try_format(avctx, type, pixelformat))
620 + return v4l2_request_set_format(avctx, type, pixelformat, 0);
627 +static int v4l2_request_probe_video_device(struct udev_device *device, AVCodecContext *avctx, uint32_t pixelformat, uint32_t buffersize, struct v4l2_ext_control *control, int count)
629 + V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
630 + int ret = AVERROR(EINVAL);
631 + struct v4l2_capability capability = {0};
632 + unsigned int capabilities = 0;
634 + const char *path = udev_device_get_devnode(device);
636 + av_log(avctx, AV_LOG_ERROR, "%s: get video device devnode failed\n", __func__);
637 + ret = AVERROR(EINVAL);
641 + ctx->video_fd = open(path, O_RDWR | O_NONBLOCK, 0);
642 + if (ctx->video_fd < 0) {
643 + av_log(avctx, AV_LOG_ERROR, "%s: opening %s failed, %s (%d)\n", __func__, path, strerror(errno), errno);
644 + ret = AVERROR(EINVAL);
648 + ret = ioctl(ctx->video_fd, VIDIOC_QUERYCAP, &capability);
650 + av_log(avctx, AV_LOG_ERROR, "%s: get video capability failed, %s (%d)\n", __func__, strerror(errno), errno);
651 + ret = AVERROR(EINVAL);
655 + if (capability.capabilities & V4L2_CAP_DEVICE_CAPS)
656 + capabilities = capability.device_caps;
658 + capabilities = capability.capabilities;
660 + av_log(avctx, AV_LOG_DEBUG, "%s: avctx=%p ctx=%p path=%s capabilities=%u\n", __func__, avctx, ctx, path, capabilities);
662 + if ((capabilities & V4L2_CAP_STREAMING) != V4L2_CAP_STREAMING) {
663 + av_log(avctx, AV_LOG_ERROR, "%s: missing required streaming capability\n", __func__);
664 + ret = AVERROR(EINVAL);
668 + if ((capabilities & V4L2_CAP_VIDEO_M2M_MPLANE) == V4L2_CAP_VIDEO_M2M_MPLANE) {
669 + ctx->output_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
670 + ctx->format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
671 + } else if ((capabilities & V4L2_CAP_VIDEO_M2M) == V4L2_CAP_VIDEO_M2M) {
672 + ctx->output_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
673 + ctx->format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
675 + av_log(avctx, AV_LOG_ERROR, "%s: missing required mem2mem capability\n", __func__);
676 + ret = AVERROR(EINVAL);
680 + ret = v4l2_request_try_format(avctx, ctx->output_type, pixelformat);
682 + av_log(avctx, AV_LOG_WARNING, "%s: try output format failed\n", __func__);
683 + ret = AVERROR(EINVAL);
687 + ret = v4l2_request_set_format(avctx, ctx->output_type, pixelformat, buffersize);
689 + av_log(avctx, AV_LOG_ERROR, "%s: set output format failed, %s (%d)\n", __func__, strerror(errno), errno);
690 + ret = AVERROR(EINVAL);
694 + ret = v4l2_request_set_controls(ctx, -1, control, count);
696 + av_log(avctx, AV_LOG_ERROR, "%s: set controls failed, %s (%d)\n", __func__, strerror(errno), errno);
697 + ret = AVERROR(EINVAL);
701 + ret = v4l2_request_select_capture_format(avctx);
703 + av_log(avctx, AV_LOG_WARNING, "%s: select capture format failed\n", __func__);
704 + ret = AVERROR(EINVAL);
711 + if (ctx->video_fd >= 0) {
712 + close(ctx->video_fd);
713 + ctx->video_fd = -1;
718 +static int v4l2_request_init_context(AVCodecContext *avctx)
720 + V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
723 + ret = ioctl(ctx->video_fd, VIDIOC_G_FMT, &ctx->format);
725 + av_log(avctx, AV_LOG_ERROR, "%s: get capture format failed, %s (%d)\n", __func__, strerror(errno), errno);
726 + ret = AVERROR(EINVAL);
730 + if (V4L2_TYPE_IS_MULTIPLANAR(ctx->format.type)) {
731 + 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);
733 + 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);
736 + ret = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_DRM);
740 + ret = ioctl(ctx->video_fd, VIDIOC_STREAMON, &ctx->output_type);
742 + av_log(avctx, AV_LOG_ERROR, "%s: output stream on failed, %s (%d)\n", __func__, strerror(errno), errno);
743 + ret = AVERROR(EINVAL);
747 + ret = ioctl(ctx->video_fd, VIDIOC_STREAMON, &ctx->format.type);
749 + av_log(avctx, AV_LOG_ERROR, "%s: capture stream on failed, %s (%d)\n", __func__, strerror(errno), errno);
750 + ret = AVERROR(EINVAL);
757 + ff_v4l2_request_uninit(avctx);
761 +static int v4l2_request_probe_media_device(struct udev_device *device, AVCodecContext *avctx, uint32_t pixelformat, uint32_t buffersize, struct v4l2_ext_control *control, int count)
763 + V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
765 + struct media_device_info device_info = {0};
766 + struct media_v2_topology topology = {0};
767 + struct media_v2_interface *interfaces = NULL;
768 + struct udev *udev = udev_device_get_udev(device);
769 + struct udev_device *video_device;
772 + const char *path = udev_device_get_devnode(device);
774 + av_log(avctx, AV_LOG_ERROR, "%s: get media device devnode failed\n", __func__);
775 + ret = AVERROR(EINVAL);
779 + ctx->media_fd = open(path, O_RDWR, 0);
780 + if (ctx->media_fd < 0) {
781 + av_log(avctx, AV_LOG_ERROR, "%s: opening %s failed, %s (%d)\n", __func__, path, strerror(errno), errno);
782 + ret = AVERROR(EINVAL);
786 + ret = ioctl(ctx->media_fd, MEDIA_IOC_DEVICE_INFO, &device_info);
788 + av_log(avctx, AV_LOG_ERROR, "%s: get media device info failed, %s (%d)\n", __func__, strerror(errno), errno);
789 + ret = AVERROR(EINVAL);
793 + av_log(avctx, AV_LOG_DEBUG, "%s: avctx=%p ctx=%p path=%s driver=%s\n", __func__, avctx, ctx, path, device_info.driver);
795 + ret = ioctl(ctx->media_fd, MEDIA_IOC_G_TOPOLOGY, &topology);
797 + av_log(avctx, AV_LOG_ERROR, "%s: get media topology failed, %s (%d)\n", __func__, strerror(errno), errno);
798 + ret = AVERROR(EINVAL);
802 + if (topology.num_interfaces <= 0) {
803 + av_log(avctx, AV_LOG_ERROR, "%s: media device has no interfaces\n", __func__);
804 + ret = AVERROR(EINVAL);
808 + interfaces = av_mallocz(topology.num_interfaces * sizeof(struct media_v2_interface));
810 + av_log(avctx, AV_LOG_ERROR, "%s: allocating media interface struct failed\n", __func__);
811 + ret = AVERROR(ENOMEM);
815 + topology.ptr_interfaces = (__u64)(uintptr_t)interfaces;
816 + ret = ioctl(ctx->media_fd, MEDIA_IOC_G_TOPOLOGY, &topology);
818 + av_log(avctx, AV_LOG_ERROR, "%s: get media topology failed, %s (%d)\n", __func__, strerror(errno), errno);
819 + ret = AVERROR(EINVAL);
823 + ret = AVERROR(EINVAL);
824 + for (int i = 0; i < topology.num_interfaces; i++) {
825 + if (interfaces[i].intf_type != MEDIA_INTF_T_V4L_VIDEO)
828 + devnum = makedev(interfaces[i].devnode.major, interfaces[i].devnode.minor);
829 + video_device = udev_device_new_from_devnum(udev, 'c', devnum);
830 + if (!video_device) {
831 + av_log(avctx, AV_LOG_ERROR, "%s: video_device=%p\n", __func__, video_device);
835 + ret = v4l2_request_probe_video_device(video_device, avctx, pixelformat, buffersize, control, count);
836 + udev_device_unref(video_device);
842 + av_freep(&interfaces);
846 + av_freep(&interfaces);
847 + if (ctx->media_fd >= 0) {
848 + close(ctx->media_fd);
849 + ctx->media_fd = -1;
854 +int ff_v4l2_request_init(AVCodecContext *avctx, uint32_t pixelformat, uint32_t buffersize, struct v4l2_ext_control *control, int count)
856 + V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
857 + int ret = AVERROR(EINVAL);
859 + struct udev_enumerate *enumerate;
860 + struct udev_list_entry *devices;
861 + struct udev_list_entry *entry;
862 + struct udev_device *device;
864 + 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);
866 + ctx->media_fd = -1;
867 + ctx->video_fd = -1;
868 + ctx->timestamp = 0;
872 + av_log(avctx, AV_LOG_ERROR, "%s: allocating udev context failed\n", __func__);
873 + ret = AVERROR(ENOMEM);
877 + enumerate = udev_enumerate_new(udev);
879 + av_log(avctx, AV_LOG_ERROR, "%s: allocating udev enumerator failed\n", __func__);
880 + ret = AVERROR(ENOMEM);
884 + udev_enumerate_add_match_subsystem(enumerate, "media");
885 + udev_enumerate_scan_devices(enumerate);
887 + devices = udev_enumerate_get_list_entry(enumerate);
888 + udev_list_entry_foreach(entry, devices) {
889 + const char *path = udev_list_entry_get_name(entry);
893 + device = udev_device_new_from_syspath(udev, path);
897 + ret = v4l2_request_probe_media_device(device, avctx, pixelformat, buffersize, control, count);
898 + udev_device_unref(device);
904 + udev_enumerate_unref(enumerate);
907 + ret = v4l2_request_init_context(avctx);
914 +int ff_v4l2_request_uninit(AVCodecContext *avctx)
916 + V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
919 + av_log(avctx, AV_LOG_DEBUG, "%s: avctx=%p ctx=%p\n", __func__, avctx, ctx);
921 + if (ctx->video_fd >= 0) {
922 + ret = ioctl(ctx->video_fd, VIDIOC_STREAMOFF, &ctx->output_type);
924 + av_log(avctx, AV_LOG_ERROR, "%s: output stream off failed, %s (%d)\n", __func__, strerror(errno), errno);
926 + ret = ioctl(ctx->video_fd, VIDIOC_STREAMOFF, &ctx->format.type);
928 + av_log(avctx, AV_LOG_ERROR, "%s: capture stream off failed, %s (%d)\n", __func__, strerror(errno), errno);
931 + if (avctx->hw_frames_ctx) {
932 + AVHWFramesContext *hwfc = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
933 + av_buffer_pool_flush(hwfc->pool);
936 + if (ctx->video_fd >= 0)
937 + close(ctx->video_fd);
939 + if (ctx->media_fd >= 0)
940 + close(ctx->media_fd);
945 +static int v4l2_request_buffer_alloc(AVCodecContext *avctx, V4L2RequestBuffer *buf, enum v4l2_buf_type type)
947 + V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
949 + struct v4l2_plane planes[1] = {};
950 + struct v4l2_create_buffers buffers = {
952 + .memory = V4L2_MEMORY_MMAP,
953 + .format.type = type,
956 + av_log(avctx, AV_LOG_DEBUG, "%s: avctx=%p buf=%p type=%u\n", __func__, avctx, buf, type);
958 + ret = ioctl(ctx->video_fd, VIDIOC_G_FMT, &buffers.format);
960 + av_log(avctx, AV_LOG_ERROR, "%s: get format failed for type %u, %s (%d)\n", __func__, type, strerror(errno), errno);
964 + if (V4L2_TYPE_IS_MULTIPLANAR(buffers.format.type)) {
965 + 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);
967 + 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);
970 + ret = ioctl(ctx->video_fd, VIDIOC_CREATE_BUFS, &buffers);
972 + av_log(avctx, AV_LOG_ERROR, "%s: create buffers failed for type %u, %s (%d)\n", __func__, type, strerror(errno), errno);
976 + if (V4L2_TYPE_IS_MULTIPLANAR(type)) {
977 + buf->width = buffers.format.fmt.pix_mp.width;
978 + buf->height = buffers.format.fmt.pix_mp.height;
979 + buf->size = buffers.format.fmt.pix_mp.plane_fmt[0].sizeimage;
980 + buf->buffer.length = 1;
981 + buf->buffer.m.planes = planes;
983 + buf->width = buffers.format.fmt.pix.width;
984 + buf->height = buffers.format.fmt.pix.height;
985 + buf->size = buffers.format.fmt.pix.sizeimage;
988 + buf->index = buffers.index;
989 + buf->capabilities = buffers.capabilities;
992 + buf->buffer.type = type;
993 + buf->buffer.memory = V4L2_MEMORY_MMAP;
994 + buf->buffer.index = buf->index;
996 + ret = ioctl(ctx->video_fd, VIDIOC_QUERYBUF, &buf->buffer);
998 + av_log(avctx, AV_LOG_ERROR, "%s: query buffer %d failed, %s (%d)\n", __func__, buf->index, strerror(errno), errno);
1002 + if (V4L2_TYPE_IS_OUTPUT(type)) {
1003 + 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);
1004 + if (addr == MAP_FAILED) {
1005 + av_log(avctx, AV_LOG_ERROR, "%s: mmap failed, %s (%d)\n", __func__, strerror(errno), errno);
1009 + buf->addr = (uint8_t*)addr;
1011 + struct v4l2_exportbuffer exportbuffer = {
1013 + .index = buf->index,
1014 + .flags = O_RDONLY,
1017 + ret = ioctl(ctx->video_fd, VIDIOC_EXPBUF, &exportbuffer);
1019 + av_log(avctx, AV_LOG_ERROR, "%s: export buffer %d failed, %s (%d)\n", __func__, buf->index, strerror(errno), errno);
1023 + buf->fd = exportbuffer.fd;
1026 + 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);
1030 +static void v4l2_request_buffer_free(V4L2RequestBuffer *buf)
1032 + 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);
1035 + munmap(buf->addr, buf->size);
1041 +static void v4l2_request_frame_free(void *opaque, uint8_t *data)
1043 + AVCodecContext *avctx = opaque;
1044 + V4L2RequestDescriptor *req = (V4L2RequestDescriptor*)data;
1046 + av_log(NULL, AV_LOG_DEBUG, "%s: avctx=%p data=%p request_fd=%d\n", __func__, avctx, data, req->request_fd);
1048 + if (req->request_fd >= 0)
1049 + close(req->request_fd);
1051 + v4l2_request_buffer_free(&req->capture);
1052 + v4l2_request_buffer_free(&req->output);
1057 +static AVBufferRef *v4l2_request_frame_alloc(void *opaque, int size)
1059 + AVCodecContext *avctx = opaque;
1060 + V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
1061 + V4L2RequestDescriptor *req;
1066 + data = av_mallocz(size);
1070 + av_log(avctx, AV_LOG_DEBUG, "%s: avctx=%p size=%d data=%p\n", __func__, avctx, size, data);
1072 + ref = av_buffer_create(data, size, v4l2_request_frame_free, avctx, 0);
1078 + req = (V4L2RequestDescriptor*)data;
1079 + req->request_fd = -1;
1080 + req->output.fd = -1;
1081 + req->capture.fd = -1;
1083 + ret = v4l2_request_buffer_alloc(avctx, &req->output, ctx->output_type);
1085 + av_buffer_unref(&ref);
1089 + ret = v4l2_request_buffer_alloc(avctx, &req->capture, ctx->format.type);
1091 + av_buffer_unref(&ref);
1095 + ret = ioctl(ctx->media_fd, MEDIA_IOC_REQUEST_ALLOC, &req->request_fd);
1097 + av_log(avctx, AV_LOG_ERROR, "%s: request alloc failed, %s (%d)\n", __func__, strerror(errno), errno);
1098 + av_buffer_unref(&ref);
1102 + av_log(avctx, AV_LOG_DEBUG, "%s: avctx=%p size=%d data=%p request_fd=%d\n", __func__, avctx, size, data, req->request_fd);
1106 +static void v4l2_request_pool_free(void *opaque)
1108 + av_log(NULL, AV_LOG_DEBUG, "%s: opaque=%p\n", __func__, opaque);
1111 +static void v4l2_request_hwframe_ctx_free(AVHWFramesContext *hwfc)
1113 + av_log(NULL, AV_LOG_DEBUG, "%s: hwfc=%p pool=%p\n", __func__, hwfc, hwfc->pool);
1115 + av_buffer_pool_flush(hwfc->pool);
1116 + av_buffer_pool_uninit(&hwfc->pool);
1119 +int ff_v4l2_request_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
1121 + V4L2RequestContext *ctx = avctx->internal->hwaccel_priv_data;
1122 + AVHWFramesContext *hwfc = (AVHWFramesContext*)hw_frames_ctx->data;
1124 + hwfc->format = AV_PIX_FMT_DRM_PRIME;
1125 + hwfc->sw_format = AV_PIX_FMT_NV12;
1126 + if (V4L2_TYPE_IS_MULTIPLANAR(ctx->format.type)) {
1127 + hwfc->width = ctx->format.fmt.pix_mp.width;
1128 + hwfc->height = ctx->format.fmt.pix_mp.height;
1130 + hwfc->width = ctx->format.fmt.pix.width;
1131 + hwfc->height = ctx->format.fmt.pix.height;
1134 + hwfc->pool = av_buffer_pool_init2(sizeof(V4L2RequestDescriptor), avctx, v4l2_request_frame_alloc, v4l2_request_pool_free);
1136 + return AVERROR(ENOMEM);
1138 + hwfc->free = v4l2_request_hwframe_ctx_free;
1140 + hwfc->initial_pool_size = 1;
1142 + switch (avctx->codec_id) {
1143 + case AV_CODEC_ID_VP9:
1144 + hwfc->initial_pool_size += 8;
1146 + case AV_CODEC_ID_VP8:
1147 + hwfc->initial_pool_size += 3;
1150 + hwfc->initial_pool_size += 2;
1153 + 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);
1157 diff --git a/libavcodec/v4l2_request.h b/libavcodec/v4l2_request.h
1158 new file mode 100644
1159 index 000000000000..58d2aa70af80
1161 +++ b/libavcodec/v4l2_request.h
1164 + * This file is part of FFmpeg.
1166 + * FFmpeg is free software; you can redistribute it and/or
1167 + * modify it under the terms of the GNU Lesser General Public
1168 + * License as published by the Free Software Foundation; either
1169 + * version 2.1 of the License, or (at your option) any later version.
1171 + * FFmpeg is distributed in the hope that it will be useful,
1172 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1173 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1174 + * Lesser General Public License for more details.
1176 + * You should have received a copy of the GNU Lesser General Public
1177 + * License along with FFmpeg; if not, write to the Free Software
1178 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1181 +#ifndef AVCODEC_V4L2_REQUEST_H
1182 +#define AVCODEC_V4L2_REQUEST_H
1184 +#include <linux/videodev2.h>
1186 +#include "libavutil/hwcontext_drm.h"
1188 +typedef struct V4L2RequestContext {
1191 + enum v4l2_buf_type output_type;
1192 + struct v4l2_format format;
1194 +} V4L2RequestContext;
1196 +typedef struct V4L2RequestBuffer {
1204 + uint32_t capabilities;
1205 + struct v4l2_buffer buffer;
1206 +} V4L2RequestBuffer;
1208 +typedef struct V4L2RequestDescriptor {
1209 + AVDRMFrameDescriptor drm;
1211 + V4L2RequestBuffer output;
1212 + V4L2RequestBuffer capture;
1213 +} V4L2RequestDescriptor;
1215 +uint64_t ff_v4l2_request_get_capture_timestamp(AVFrame *frame);
1217 +int ff_v4l2_request_reset_frame(AVCodecContext *avctx, AVFrame *frame);
1219 +int ff_v4l2_request_append_output_buffer(AVCodecContext *avctx, AVFrame *frame, const uint8_t *data, uint32_t size);
1221 +int ff_v4l2_request_set_controls(AVCodecContext *avctx, struct v4l2_ext_control *control, int count);
1223 +int ff_v4l2_request_get_controls(AVCodecContext *avctx, struct v4l2_ext_control *control, int count);
1225 +int ff_v4l2_request_query_control(AVCodecContext *avctx, struct v4l2_query_ext_ctrl *control);
1227 +int ff_v4l2_request_query_control_default_value(AVCodecContext *avctx, uint32_t id);
1229 +int ff_v4l2_request_decode_slice(AVCodecContext *avctx, AVFrame *frame, struct v4l2_ext_control *control, int count, int first_slice, int last_slice);
1231 +int ff_v4l2_request_decode_frame(AVCodecContext *avctx, AVFrame *frame, struct v4l2_ext_control *control, int count);
1233 +int ff_v4l2_request_init(AVCodecContext *avctx, uint32_t pixelformat, uint32_t buffersize, struct v4l2_ext_control *control, int count);
1235 +int ff_v4l2_request_uninit(AVCodecContext *avctx);
1237 +int ff_v4l2_request_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx);
1239 +#endif /* AVCODEC_V4L2_REQUEST_H */
1240 From 6349ae1e10d1b68c47c3b883b27567f9678c6e0c Mon Sep 17 00:00:00 2001
1241 From: Boris Brezillon <boris.brezillon@collabora.com>
1242 Date: Wed, 22 May 2019 14:44:22 +0200
1243 Subject: [PATCH] h264dec: add ref_pic_marking and pic_order_cnt bit_size to
1246 Used by V4L2 request API h264 hwaccel
1248 Signed-off-by: Boris Brezillon <boris.brezillon@collabora.com>
1249 Signed-off-by: Jonas Karlman <jonas@kwiboo.se>
1251 libavcodec/h264_slice.c | 6 +++++-
1252 libavcodec/h264dec.h | 2 ++
1253 2 files changed, 7 insertions(+), 1 deletion(-)
1255 diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c
1256 index a19635a23d44..5a5ceb09352c 100644
1257 --- a/libavcodec/h264_slice.c
1258 +++ b/libavcodec/h264_slice.c
1259 @@ -1736,7 +1736,7 @@ static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
1260 unsigned int slice_type, tmp, i;
1261 int field_pic_flag, bottom_field_flag;
1262 int first_slice = sl == h->slice_ctx && !h->current_slice;
1263 - int picture_structure;
1264 + int picture_structure, pos;
1267 av_assert0(!h->setup_finished);
1268 @@ -1820,6 +1820,7 @@ static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
1269 av_log(h->avctx, AV_LOG_WARNING, "idr_pic_id is invalid\n");
1272 + pos = sl->gb.index;
1274 sl->delta_poc_bottom = 0;
1275 if (sps->poc_type == 0) {
1276 @@ -1833,6 +1834,7 @@ static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
1277 if (pps->pic_order_present == 1 && picture_structure == PICT_FRAME)
1278 sl->delta_poc[1] = get_se_golomb(&sl->gb);
1280 + sl->pic_order_cnt_bit_size = sl->gb.index - pos;
1282 sl->redundant_pic_count = 0;
1283 if (pps->redundant_pic_cnt_present)
1284 @@ -1872,9 +1874,11 @@ static int h264_slice_header_parse(const H264Context *h, H264SliceContext *sl,
1286 sl->explicit_ref_marking = 0;
1288 + pos = sl->gb.index;
1289 ret = ff_h264_decode_ref_pic_marking(sl, &sl->gb, nal, h->avctx);
1290 if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
1291 return AVERROR_INVALIDDATA;
1292 + sl->ref_pic_marking_bit_size = sl->gb.index - pos;
1295 if (sl->slice_type_nos != AV_PICTURE_TYPE_I && pps->cabac) {
1296 diff --git a/libavcodec/h264dec.h b/libavcodec/h264dec.h
1297 index 316dc6a2c890..f2cabac468d0 100644
1298 --- a/libavcodec/h264dec.h
1299 +++ b/libavcodec/h264dec.h
1300 @@ -328,6 +328,7 @@ typedef struct H264SliceContext {
1301 MMCO mmco[MAX_MMCO_COUNT];
1303 int explicit_ref_marking;
1304 + int ref_pic_marking_bit_size;
1308 @@ -336,6 +337,7 @@ typedef struct H264SliceContext {
1312 + int pic_order_cnt_bit_size;
1316 From 650875c24687c35290b2393ef39f77074ca49813 Mon Sep 17 00:00:00 2001
1317 From: Jernej Skrabec <jernej.skrabec@siol.net>
1318 Date: Sat, 15 Dec 2018 22:32:16 +0100
1319 Subject: [PATCH] Add V4L2 request API h264 hwaccel
1321 Signed-off-by: Jernej Skrabec <jernej.skrabec@siol.net>
1322 Signed-off-by: Jonas Karlman <jonas@kwiboo.se>
1325 libavcodec/Makefile | 1 +
1326 libavcodec/h264_slice.c | 4 +
1327 libavcodec/h264dec.c | 3 +
1328 libavcodec/hwaccels.h | 1 +
1329 libavcodec/v4l2_request_h264.c | 456 +++++++++++++++++++++++++++++++++
1330 6 files changed, 468 insertions(+)
1331 create mode 100644 libavcodec/v4l2_request_h264.c
1333 diff --git a/configure b/configure
1334 index 9f9909a23696..293d0447f683 100755
1337 @@ -2925,6 +2925,8 @@ h264_dxva2_hwaccel_deps="dxva2"
1338 h264_dxva2_hwaccel_select="h264_decoder"
1339 h264_nvdec_hwaccel_deps="nvdec"
1340 h264_nvdec_hwaccel_select="h264_decoder"
1341 +h264_v4l2request_hwaccel_deps="v4l2_request"
1342 +h264_v4l2request_hwaccel_select="h264_decoder"
1343 h264_vaapi_hwaccel_deps="vaapi"
1344 h264_vaapi_hwaccel_select="h264_decoder"
1345 h264_vdpau_hwaccel_deps="vdpau"
1346 @@ -6567,6 +6569,7 @@ if enabled v4l2_m2m; then
1349 check_func_headers "linux/media.h linux/videodev2.h" v4l2_timeval_to_ns
1350 +check_cc h264_v4l2_request linux/videodev2.h "int i = V4L2_PIX_FMT_H264_SLICE;"
1352 check_headers sys/videoio.h
1353 test_code cc sys/videoio.h "struct v4l2_frmsizeenum vfse; vfse.discrete.width = 0;" && enable_sanitized struct_v4l2_frmivalenum_discrete
1354 diff --git a/libavcodec/Makefile b/libavcodec/Makefile
1355 index d74220516826..4f6e7fc2515c 100644
1356 --- a/libavcodec/Makefile
1357 +++ b/libavcodec/Makefile
1358 @@ -903,6 +903,7 @@ OBJS-$(CONFIG_H264_D3D11VA_HWACCEL) += dxva2_h264.o
1359 OBJS-$(CONFIG_H264_DXVA2_HWACCEL) += dxva2_h264.o
1360 OBJS-$(CONFIG_H264_NVDEC_HWACCEL) += nvdec_h264.o
1361 OBJS-$(CONFIG_H264_QSV_HWACCEL) += qsvdec.o
1362 +OBJS-$(CONFIG_H264_V4L2REQUEST_HWACCEL) += v4l2_request_h264.o
1363 OBJS-$(CONFIG_H264_VAAPI_HWACCEL) += vaapi_h264.o
1364 OBJS-$(CONFIG_H264_VDPAU_HWACCEL) += vdpau_h264.o
1365 OBJS-$(CONFIG_H264_VIDEOTOOLBOX_HWACCEL) += videotoolbox.o
1366 diff --git a/libavcodec/h264_slice.c b/libavcodec/h264_slice.c
1367 index 5a5ceb09352c..4ed267317184 100644
1368 --- a/libavcodec/h264_slice.c
1369 +++ b/libavcodec/h264_slice.c
1370 @@ -759,6 +759,7 @@ static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
1371 #define HWACCEL_MAX (CONFIG_H264_DXVA2_HWACCEL + \
1372 (CONFIG_H264_D3D11VA_HWACCEL * 2) + \
1373 CONFIG_H264_NVDEC_HWACCEL + \
1374 + CONFIG_H264_V4L2REQUEST_HWACCEL + \
1375 CONFIG_H264_VAAPI_HWACCEL + \
1376 CONFIG_H264_VIDEOTOOLBOX_HWACCEL + \
1377 CONFIG_H264_VDPAU_HWACCEL)
1378 @@ -843,6 +844,9 @@ static enum AVPixelFormat get_pixel_format(H264Context *h, int force_callback)
1379 #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
1380 if (h->avctx->colorspace != AVCOL_SPC_RGB)
1381 *fmt++ = AV_PIX_FMT_VIDEOTOOLBOX;
1383 +#if CONFIG_H264_V4L2REQUEST_HWACCEL
1384 + *fmt++ = AV_PIX_FMT_DRM_PRIME;
1387 if (h->avctx->colorspace == AVCOL_SPC_RGB) {
1388 diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c
1389 index 5eedeb3c275d..a504c89565c7 100644
1390 --- a/libavcodec/h264dec.c
1391 +++ b/libavcodec/h264dec.c
1392 @@ -1102,6 +1102,9 @@ AVCodec ff_h264_decoder = {
1394 #if CONFIG_H264_VIDEOTOOLBOX_HWACCEL
1395 HWACCEL_VIDEOTOOLBOX(h264),
1397 +#if CONFIG_H264_V4L2REQUEST_HWACCEL
1398 + HWACCEL_V4L2REQUEST(h264),
1402 diff --git a/libavcodec/hwaccels.h b/libavcodec/hwaccels.h
1403 index 6109c89bd63c..f758c34ddcf9 100644
1404 --- a/libavcodec/hwaccels.h
1405 +++ b/libavcodec/hwaccels.h
1406 @@ -27,6 +27,7 @@ extern const AVHWAccel ff_h264_d3d11va_hwaccel;
1407 extern const AVHWAccel ff_h264_d3d11va2_hwaccel;
1408 extern const AVHWAccel ff_h264_dxva2_hwaccel;
1409 extern const AVHWAccel ff_h264_nvdec_hwaccel;
1410 +extern const AVHWAccel ff_h264_v4l2request_hwaccel;
1411 extern const AVHWAccel ff_h264_vaapi_hwaccel;
1412 extern const AVHWAccel ff_h264_vdpau_hwaccel;
1413 extern const AVHWAccel ff_h264_videotoolbox_hwaccel;
1414 diff --git a/libavcodec/v4l2_request_h264.c b/libavcodec/v4l2_request_h264.c
1415 new file mode 100644
1416 index 000000000000..88da8f0a2db0
1418 +++ b/libavcodec/v4l2_request_h264.c
1421 + * This file is part of FFmpeg.
1423 + * FFmpeg is free software; you can redistribute it and/or
1424 + * modify it under the terms of the GNU Lesser General Public
1425 + * License as published by the Free Software Foundation; either
1426 + * version 2.1 of the License, or (at your option) any later version.
1428 + * FFmpeg is distributed in the hope that it will be useful,
1429 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1430 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1431 + * Lesser General Public License for more details.
1433 + * You should have received a copy of the GNU Lesser General Public
1434 + * License along with FFmpeg; if not, write to the Free Software
1435 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1438 +#include "h264dec.h"
1439 +#include "hwconfig.h"
1440 +#include "v4l2_request.h"
1442 +typedef struct V4L2RequestControlsH264 {
1443 + struct v4l2_ctrl_h264_sps sps;
1444 + struct v4l2_ctrl_h264_pps pps;
1445 + struct v4l2_ctrl_h264_scaling_matrix scaling_matrix;
1446 + struct v4l2_ctrl_h264_decode_params decode_params;
1447 + struct v4l2_ctrl_h264_slice_params slice_params;
1448 + struct v4l2_ctrl_h264_pred_weights pred_weights;
1449 + int pred_weights_required;
1452 +} V4L2RequestControlsH264;
1454 +typedef struct V4L2RequestContextH264 {
1455 + V4L2RequestContext base;
1458 +} V4L2RequestContextH264;
1460 +static uint8_t nalu_slice_start_code[] = { 0x00, 0x00, 0x01 };
1462 +static void fill_weight_factors(struct v4l2_h264_weight_factors *factors, int list, const H264SliceContext *sl)
1464 + for (int i = 0; i < sl->ref_count[list]; i++) {
1465 + if (sl->pwt.luma_weight_flag[list]) {
1466 + factors->luma_weight[i] = sl->pwt.luma_weight[i][list][0];
1467 + factors->luma_offset[i] = sl->pwt.luma_weight[i][list][1];
1469 + factors->luma_weight[i] = 1 << sl->pwt.luma_log2_weight_denom;
1470 + factors->luma_offset[i] = 0;
1472 + for (int j = 0; j < 2; j++) {
1473 + if (sl->pwt.chroma_weight_flag[list]) {
1474 + factors->chroma_weight[i][j] = sl->pwt.chroma_weight[i][list][j][0];
1475 + factors->chroma_offset[i][j] = sl->pwt.chroma_weight[i][list][j][1];
1477 + factors->chroma_weight[i][j] = 1 << sl->pwt.chroma_log2_weight_denom;
1478 + factors->chroma_offset[i][j] = 0;
1484 +static void fill_dpb_entry(struct v4l2_h264_dpb_entry *entry, const H264Picture *pic)
1486 + entry->reference_ts = ff_v4l2_request_get_capture_timestamp(pic->f);
1487 + entry->pic_num = pic->pic_id;
1488 + entry->frame_num = pic->frame_num;
1489 + entry->fields = pic->reference & V4L2_H264_FRAME_REF;
1490 + entry->flags = V4L2_H264_DPB_ENTRY_FLAG_VALID;
1491 + if (entry->fields)
1492 + entry->flags |= V4L2_H264_DPB_ENTRY_FLAG_ACTIVE;
1493 + if (pic->long_ref)
1494 + entry->flags |= V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM;
1495 + if (pic->field_picture)
1496 + entry->flags |= V4L2_H264_DPB_ENTRY_FLAG_FIELD;
1497 + if (pic->field_poc[0] != INT_MAX)
1498 + entry->top_field_order_cnt = pic->field_poc[0];
1499 + if (pic->field_poc[1] != INT_MAX)
1500 + entry->bottom_field_order_cnt = pic->field_poc[1];
1503 +static void fill_dpb(struct v4l2_ctrl_h264_decode_params *decode, const H264Context *h)
1507 + for (int i = 0; i < h->short_ref_count; i++) {
1508 + const H264Picture *pic = h->short_ref[i];
1509 + if (pic && (pic->field_poc[0] != INT_MAX || pic->field_poc[1] != INT_MAX))
1510 + fill_dpb_entry(&decode->dpb[entries++], pic);
1513 + if (!h->long_ref_count)
1516 + for (int i = 0; i < FF_ARRAY_ELEMS(h->long_ref); i++) {
1517 + const H264Picture *pic = h->long_ref[i];
1518 + if (pic && (pic->field_poc[0] != INT_MAX || pic->field_poc[1] != INT_MAX))
1519 + fill_dpb_entry(&decode->dpb[entries++], pic);
1523 +static void fill_ref_list(struct v4l2_h264_reference *reference, struct v4l2_ctrl_h264_decode_params *decode, const H264Ref *ref)
1525 + uint64_t timestamp;
1530 + timestamp = ff_v4l2_request_get_capture_timestamp(ref->parent->f);
1532 + for (uint8_t i = 0; i < FF_ARRAY_ELEMS(decode->dpb); i++) {
1533 + struct v4l2_h264_dpb_entry *entry = &decode->dpb[i];
1534 + if ((entry->flags & V4L2_H264_DPB_ENTRY_FLAG_VALID) &&
1535 + entry->reference_ts == timestamp) {
1536 + reference->fields = ref->reference & V4L2_H264_FRAME_REF;
1537 + reference->index = i;
1543 +static void fill_sps(struct v4l2_ctrl_h264_sps *ctrl, const H264Context *h)
1545 + const SPS *sps = h->ps.sps;
1547 + *ctrl = (struct v4l2_ctrl_h264_sps) {
1548 + .profile_idc = sps->profile_idc,
1549 + .constraint_set_flags = sps->constraint_set_flags,
1550 + .level_idc = sps->level_idc,
1551 + .seq_parameter_set_id = sps->sps_id,
1552 + .chroma_format_idc = sps->chroma_format_idc,
1553 + .bit_depth_luma_minus8 = sps->bit_depth_luma - 8,
1554 + .bit_depth_chroma_minus8 = sps->bit_depth_chroma - 8,
1555 + .log2_max_frame_num_minus4 = sps->log2_max_frame_num - 4,
1556 + .pic_order_cnt_type = sps->poc_type,
1557 + .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_poc_lsb - 4,
1558 + .max_num_ref_frames = sps->ref_frame_count,
1559 + .num_ref_frames_in_pic_order_cnt_cycle = sps->poc_cycle_length,
1560 + .offset_for_non_ref_pic = sps->offset_for_non_ref_pic,
1561 + .offset_for_top_to_bottom_field = sps->offset_for_top_to_bottom_field,
1562 + .pic_width_in_mbs_minus1 = h->mb_width - 1,
1563 + .pic_height_in_map_units_minus1 = sps->frame_mbs_only_flag ? h->mb_height - 1 : h->mb_height / 2 - 1,
1566 + if (sps->poc_cycle_length > 0 && sps->poc_cycle_length <= 255)
1567 + memcpy(ctrl->offset_for_ref_frame, sps->offset_for_ref_frame, sps->poc_cycle_length * sizeof(ctrl->offset_for_ref_frame[0]));
1569 + if (sps->residual_color_transform_flag)
1570 + ctrl->flags |= V4L2_H264_SPS_FLAG_SEPARATE_COLOUR_PLANE;
1571 + if (sps->transform_bypass)
1572 + ctrl->flags |= V4L2_H264_SPS_FLAG_QPPRIME_Y_ZERO_TRANSFORM_BYPASS;
1573 + if (sps->delta_pic_order_always_zero_flag)
1574 + ctrl->flags |= V4L2_H264_SPS_FLAG_DELTA_PIC_ORDER_ALWAYS_ZERO;
1575 + if (sps->gaps_in_frame_num_allowed_flag)
1576 + ctrl->flags |= V4L2_H264_SPS_FLAG_GAPS_IN_FRAME_NUM_VALUE_ALLOWED;
1577 + if (sps->frame_mbs_only_flag)
1578 + ctrl->flags |= V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY;
1580 + ctrl->flags |= V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD;
1581 + if (sps->direct_8x8_inference_flag)
1582 + ctrl->flags |= V4L2_H264_SPS_FLAG_DIRECT_8X8_INFERENCE;
1585 +static void fill_pps(struct v4l2_ctrl_h264_pps *ctrl, const H264Context *h)
1587 + const SPS *sps = h->ps.sps;
1588 + const PPS *pps = h->ps.pps;
1589 + const H264SliceContext *sl = &h->slice_ctx[0];
1590 + int qp_bd_offset = 6 * (sps->bit_depth_luma - 8);
1592 + *ctrl = (struct v4l2_ctrl_h264_pps) {
1593 + .pic_parameter_set_id = sl->pps_id,
1594 + .seq_parameter_set_id = pps->sps_id,
1595 + .num_slice_groups_minus1 = pps->slice_group_count - 1,
1596 + .num_ref_idx_l0_default_active_minus1 = pps->ref_count[0] - 1,
1597 + .num_ref_idx_l1_default_active_minus1 = pps->ref_count[1] - 1,
1598 + .weighted_bipred_idc = pps->weighted_bipred_idc,
1599 + .pic_init_qp_minus26 = pps->init_qp - 26 - qp_bd_offset,
1600 + .pic_init_qs_minus26 = pps->init_qs - 26 - qp_bd_offset,
1601 + .chroma_qp_index_offset = pps->chroma_qp_index_offset[0],
1602 + .second_chroma_qp_index_offset = pps->chroma_qp_index_offset[1],
1606 + ctrl->flags |= V4L2_H264_PPS_FLAG_ENTROPY_CODING_MODE;
1607 + if (pps->pic_order_present)
1608 + ctrl->flags |= V4L2_H264_PPS_FLAG_BOTTOM_FIELD_PIC_ORDER_IN_FRAME_PRESENT;
1609 + if (pps->weighted_pred)
1610 + ctrl->flags |= V4L2_H264_PPS_FLAG_WEIGHTED_PRED;
1611 + if (pps->deblocking_filter_parameters_present)
1612 + ctrl->flags |= V4L2_H264_PPS_FLAG_DEBLOCKING_FILTER_CONTROL_PRESENT;
1613 + if (pps->constrained_intra_pred)
1614 + ctrl->flags |= V4L2_H264_PPS_FLAG_CONSTRAINED_INTRA_PRED;
1615 + if (pps->redundant_pic_cnt_present)
1616 + ctrl->flags |= V4L2_H264_PPS_FLAG_REDUNDANT_PIC_CNT_PRESENT;
1617 + if (pps->transform_8x8_mode)
1618 + ctrl->flags |= V4L2_H264_PPS_FLAG_TRANSFORM_8X8_MODE;
1620 + /* FFmpeg always provide a scaling matrix */
1621 + ctrl->flags |= V4L2_H264_PPS_FLAG_SCALING_MATRIX_PRESENT;
1624 +static int v4l2_request_h264_start_frame(AVCodecContext *avctx,
1625 + av_unused const uint8_t *buffer,
1626 + av_unused uint32_t size)
1628 + const H264Context *h = avctx->priv_data;
1629 + const PPS *pps = h->ps.pps;
1630 + const SPS *sps = h->ps.sps;
1631 + const H264SliceContext *sl = &h->slice_ctx[0];
1632 + V4L2RequestControlsH264 *controls = h->cur_pic_ptr->hwaccel_picture_private;
1634 + fill_sps(&controls->sps, h);
1635 + fill_pps(&controls->pps, h);
1637 + memcpy(controls->scaling_matrix.scaling_list_4x4, pps->scaling_matrix4, sizeof(controls->scaling_matrix.scaling_list_4x4));
1638 + memcpy(controls->scaling_matrix.scaling_list_8x8[0], pps->scaling_matrix8[0], sizeof(controls->scaling_matrix.scaling_list_8x8[0]));
1639 + memcpy(controls->scaling_matrix.scaling_list_8x8[1], pps->scaling_matrix8[3], sizeof(controls->scaling_matrix.scaling_list_8x8[1]));
1641 + if (sps->chroma_format_idc == 3) {
1642 + memcpy(controls->scaling_matrix.scaling_list_8x8[2], pps->scaling_matrix8[1], sizeof(controls->scaling_matrix.scaling_list_8x8[2]));
1643 + memcpy(controls->scaling_matrix.scaling_list_8x8[3], pps->scaling_matrix8[4], sizeof(controls->scaling_matrix.scaling_list_8x8[3]));
1644 + memcpy(controls->scaling_matrix.scaling_list_8x8[4], pps->scaling_matrix8[2], sizeof(controls->scaling_matrix.scaling_list_8x8[4]));
1645 + memcpy(controls->scaling_matrix.scaling_list_8x8[5], pps->scaling_matrix8[5], sizeof(controls->scaling_matrix.scaling_list_8x8[5]));
1648 + controls->decode_params = (struct v4l2_ctrl_h264_decode_params) {
1649 + .nal_ref_idc = h->nal_ref_idc,
1650 + .frame_num = h->poc.frame_num,
1651 + .top_field_order_cnt = h->cur_pic_ptr->field_poc[0] != INT_MAX ? h->cur_pic_ptr->field_poc[0] : 0,
1652 + .bottom_field_order_cnt = h->cur_pic_ptr->field_poc[1] != INT_MAX ? h->cur_pic_ptr->field_poc[1] : 0,
1653 + .idr_pic_id = sl->idr_pic_id,
1654 + .pic_order_cnt_lsb = sl->poc_lsb,
1655 + .delta_pic_order_cnt_bottom = sl->delta_poc_bottom,
1656 + .delta_pic_order_cnt0 = sl->delta_poc[0],
1657 + .delta_pic_order_cnt1 = sl->delta_poc[1],
1658 + /* Size in bits of dec_ref_pic_marking() syntax element. */
1659 + .dec_ref_pic_marking_bit_size = sl->ref_pic_marking_bit_size,
1660 + /* Size in bits of pic order count syntax. */
1661 + .pic_order_cnt_bit_size = sl->pic_order_cnt_bit_size,
1662 + .slice_group_change_cycle = 0, /* slice group not supported by FFmpeg */
1665 + if (h->picture_idr)
1666 + controls->decode_params.flags |= V4L2_H264_DECODE_PARAM_FLAG_IDR_PIC;
1667 + if (FIELD_PICTURE(h))
1668 + controls->decode_params.flags |= V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC;
1669 + if (h->picture_structure == PICT_BOTTOM_FIELD)
1670 + controls->decode_params.flags |= V4L2_H264_DECODE_PARAM_FLAG_BOTTOM_FIELD;
1672 + fill_dpb(&controls->decode_params, h);
1674 + controls->first_slice = !FIELD_PICTURE(h) || h->first_field;
1675 + controls->num_slices = 0;
1677 + return ff_v4l2_request_reset_frame(avctx, h->cur_pic_ptr->f);
1680 +static int v4l2_request_h264_queue_decode(AVCodecContext *avctx, int last_slice)
1682 + const H264Context *h = avctx->priv_data;
1683 + V4L2RequestControlsH264 *controls = h->cur_pic_ptr->hwaccel_picture_private;
1684 + V4L2RequestContextH264 *ctx = avctx->internal->hwaccel_priv_data;
1686 + struct v4l2_ext_control control[] = {
1688 + .id = V4L2_CID_STATELESS_H264_SPS,
1689 + .ptr = &controls->sps,
1690 + .size = sizeof(controls->sps),
1693 + .id = V4L2_CID_STATELESS_H264_PPS,
1694 + .ptr = &controls->pps,
1695 + .size = sizeof(controls->pps),
1698 + .id = V4L2_CID_STATELESS_H264_SCALING_MATRIX,
1699 + .ptr = &controls->scaling_matrix,
1700 + .size = sizeof(controls->scaling_matrix),
1703 + .id = V4L2_CID_STATELESS_H264_DECODE_PARAMS,
1704 + .ptr = &controls->decode_params,
1705 + .size = sizeof(controls->decode_params),
1708 + .id = V4L2_CID_STATELESS_H264_SLICE_PARAMS,
1709 + .ptr = &controls->slice_params,
1710 + .size = sizeof(controls->slice_params),
1713 + .id = V4L2_CID_STATELESS_H264_PRED_WEIGHTS,
1714 + .ptr = &controls->pred_weights,
1715 + .size = sizeof(controls->pred_weights),
1719 + if (ctx->decode_mode == V4L2_STATELESS_H264_DECODE_MODE_SLICE_BASED) {
1720 + int count = FF_ARRAY_ELEMS(control) - (controls->pred_weights_required ? 0 : 1);
1721 + return ff_v4l2_request_decode_slice(avctx, h->cur_pic_ptr->f, control, count, controls->first_slice, last_slice);
1724 + return ff_v4l2_request_decode_frame(avctx, h->cur_pic_ptr->f, control, FF_ARRAY_ELEMS(control) - 2);
1727 +static int v4l2_request_h264_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
1729 + const H264Context *h = avctx->priv_data;
1730 + const PPS *pps = h->ps.pps;
1731 + const H264SliceContext *sl = &h->slice_ctx[0];
1732 + V4L2RequestControlsH264 *controls = h->cur_pic_ptr->hwaccel_picture_private;
1733 + V4L2RequestContextH264 *ctx = avctx->internal->hwaccel_priv_data;
1734 + int i, ret, count;
1736 + if (ctx->decode_mode == V4L2_STATELESS_H264_DECODE_MODE_SLICE_BASED && controls->num_slices) {
1737 + ret = v4l2_request_h264_queue_decode(avctx, 0);
1741 + ff_v4l2_request_reset_frame(avctx, h->cur_pic_ptr->f);
1742 + controls->first_slice = 0;
1745 + if (ctx->start_code == V4L2_STATELESS_H264_START_CODE_ANNEX_B) {
1746 + ret = ff_v4l2_request_append_output_buffer(avctx, h->cur_pic_ptr->f, nalu_slice_start_code, 3);
1751 + ret = ff_v4l2_request_append_output_buffer(avctx, h->cur_pic_ptr->f, buffer, size);
1755 + if (ctx->decode_mode != V4L2_STATELESS_H264_DECODE_MODE_SLICE_BASED)
1758 + controls->slice_params = (struct v4l2_ctrl_h264_slice_params) {
1759 + /* Offset in bits to slice_data() from the beginning of this slice. */
1760 + .header_bit_size = get_bits_count(&sl->gb),
1762 + .first_mb_in_slice = sl->first_mb_addr,
1764 + .slice_type = ff_h264_get_slice_type(sl),
1765 + .colour_plane_id = 0, /* separate colour plane not supported by FFmpeg */
1766 + .redundant_pic_cnt = sl->redundant_pic_count,
1767 + .cabac_init_idc = sl->cabac_init_idc,
1768 + .slice_qp_delta = sl->qscale - pps->init_qp,
1769 + .slice_qs_delta = 0, /* not implemented by FFmpeg */
1770 + .disable_deblocking_filter_idc = sl->deblocking_filter < 2 ? !sl->deblocking_filter : sl->deblocking_filter,
1771 + .slice_alpha_c0_offset_div2 = sl->slice_alpha_c0_offset / 2,
1772 + .slice_beta_offset_div2 = sl->slice_beta_offset / 2,
1773 + .num_ref_idx_l0_active_minus1 = sl->list_count > 0 ? sl->ref_count[0] - 1 : 0,
1774 + .num_ref_idx_l1_active_minus1 = sl->list_count > 1 ? sl->ref_count[1] - 1 : 0,
1777 + if (sl->slice_type == AV_PICTURE_TYPE_B && sl->direct_spatial_mv_pred)
1778 + controls->slice_params.flags |= V4L2_H264_SLICE_FLAG_DIRECT_SPATIAL_MV_PRED;
1779 + /* V4L2_H264_SLICE_FLAG_SP_FOR_SWITCH: not implemented by FFmpeg */
1781 + controls->pred_weights_required = V4L2_H264_CTRL_PRED_WEIGHTS_REQUIRED(&controls->pps, &controls->slice_params);
1782 + if (controls->pred_weights_required) {
1783 + controls->pred_weights.chroma_log2_weight_denom = sl->pwt.chroma_log2_weight_denom;
1784 + controls->pred_weights.luma_log2_weight_denom = sl->pwt.luma_log2_weight_denom;
1787 + count = sl->list_count > 0 ? sl->ref_count[0] : 0;
1788 + for (i = 0; i < count; i++)
1789 + fill_ref_list(&controls->slice_params.ref_pic_list0[i], &controls->decode_params, &sl->ref_list[0][i]);
1790 + if (count && controls->pred_weights_required)
1791 + fill_weight_factors(&controls->pred_weights.weight_factors[0], 0, sl);
1793 + count = sl->list_count > 1 ? sl->ref_count[1] : 0;
1794 + for (i = 0; i < count; i++)
1795 + fill_ref_list(&controls->slice_params.ref_pic_list1[i], &controls->decode_params, &sl->ref_list[1][i]);
1796 + if (count && controls->pred_weights_required)
1797 + fill_weight_factors(&controls->pred_weights.weight_factors[1], 1, sl);
1799 + controls->num_slices++;
1803 +static int v4l2_request_h264_end_frame(AVCodecContext *avctx)
1805 + const H264Context *h = avctx->priv_data;
1806 + return v4l2_request_h264_queue_decode(avctx, !FIELD_PICTURE(h) || !h->first_field);
1809 +static int v4l2_request_h264_set_controls(AVCodecContext *avctx)
1811 + V4L2RequestContextH264 *ctx = avctx->internal->hwaccel_priv_data;
1813 + struct v4l2_ext_control control[] = {
1814 + { .id = V4L2_CID_STATELESS_H264_DECODE_MODE, },
1815 + { .id = V4L2_CID_STATELESS_H264_START_CODE, },
1818 + ctx->decode_mode = ff_v4l2_request_query_control_default_value(avctx, V4L2_CID_STATELESS_H264_DECODE_MODE);
1819 + if (ctx->decode_mode != V4L2_STATELESS_H264_DECODE_MODE_SLICE_BASED &&
1820 + ctx->decode_mode != V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED) {
1821 + av_log(avctx, AV_LOG_ERROR, "%s: unsupported decode mode, %d\n", __func__, ctx->decode_mode);
1822 + return AVERROR(EINVAL);
1825 + ctx->start_code = ff_v4l2_request_query_control_default_value(avctx, V4L2_CID_STATELESS_H264_START_CODE);
1826 + if (ctx->start_code != V4L2_STATELESS_H264_START_CODE_NONE &&
1827 + ctx->start_code != V4L2_STATELESS_H264_START_CODE_ANNEX_B) {
1828 + av_log(avctx, AV_LOG_ERROR, "%s: unsupported start code, %d\n", __func__, ctx->start_code);
1829 + return AVERROR(EINVAL);
1832 + control[0].value = ctx->decode_mode;
1833 + control[1].value = ctx->start_code;
1835 + return ff_v4l2_request_set_controls(avctx, control, FF_ARRAY_ELEMS(control));
1838 +static int v4l2_request_h264_init(AVCodecContext *avctx)
1840 + const H264Context *h = avctx->priv_data;
1841 + struct v4l2_ctrl_h264_sps sps;
1844 + struct v4l2_ext_control control[] = {
1846 + .id = V4L2_CID_STATELESS_H264_SPS,
1848 + .size = sizeof(sps),
1852 + fill_sps(&sps, h);
1854 + ret = ff_v4l2_request_init(avctx, V4L2_PIX_FMT_H264_SLICE, 4 * 1024 * 1024, control, FF_ARRAY_ELEMS(control));
1858 + return v4l2_request_h264_set_controls(avctx);
1861 +const AVHWAccel ff_h264_v4l2request_hwaccel = {
1862 + .name = "h264_v4l2request",
1863 + .type = AVMEDIA_TYPE_VIDEO,
1864 + .id = AV_CODEC_ID_H264,
1865 + .pix_fmt = AV_PIX_FMT_DRM_PRIME,
1866 + .start_frame = v4l2_request_h264_start_frame,
1867 + .decode_slice = v4l2_request_h264_decode_slice,
1868 + .end_frame = v4l2_request_h264_end_frame,
1869 + .frame_priv_data_size = sizeof(V4L2RequestControlsH264),
1870 + .init = v4l2_request_h264_init,
1871 + .uninit = ff_v4l2_request_uninit,
1872 + .priv_data_size = sizeof(V4L2RequestContextH264),
1873 + .frame_params = ff_v4l2_request_frame_params,
1874 + .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
1876 From f9f4a89058a6fac25712cc385eab72f70e9ac4c8 Mon Sep 17 00:00:00 2001
1877 From: Jonas Karlman <jonas@kwiboo.se>
1878 Date: Mon, 29 Apr 2019 22:08:59 +0000
1879 Subject: [PATCH] HACK: hwcontext_drm: do not require drm device
1881 Signed-off-by: Jonas Karlman <jonas@kwiboo.se>
1883 libavutil/hwcontext_drm.c | 5 +++++
1884 1 file changed, 5 insertions(+)
1886 diff --git a/libavutil/hwcontext_drm.c b/libavutil/hwcontext_drm.c
1887 index 32cbde82ebfa..aa4794c5e665 100644
1888 --- a/libavutil/hwcontext_drm.c
1889 +++ b/libavutil/hwcontext_drm.c
1890 @@ -43,6 +43,11 @@ static int drm_device_create(AVHWDeviceContext *hwdev, const char *device,
1891 AVDRMDeviceContext *hwctx = hwdev->hwctx;
1892 drmVersionPtr version;
1894 + if (device == NULL) {
1899 hwctx->fd = open(device, O_RDWR);
1901 return AVERROR(errno);
1902 From fa7165e391287bf970569e36b0b19bff947b084f Mon Sep 17 00:00:00 2001
1903 From: Jonas Karlman <jonas@kwiboo.se>
1904 Date: Mon, 27 Jul 2020 23:15:45 +0000
1905 Subject: [PATCH] HACK: define drm NV15 and NV20 format
1908 libavcodec/v4l2_request.c | 8 ++++++++
1909 1 file changed, 8 insertions(+)
1911 diff --git a/libavcodec/v4l2_request.c b/libavcodec/v4l2_request.c
1912 index 0b294feff2eb..a8f0ee79eeef 100644
1913 --- a/libavcodec/v4l2_request.c
1914 +++ b/libavcodec/v4l2_request.c
1916 #include "internal.h"
1917 #include "v4l2_request.h"
1919 +#ifndef DRM_FORMAT_NV15
1920 +#define DRM_FORMAT_NV15 fourcc_code('N', 'V', '1', '5')
1923 +#ifndef DRM_FORMAT_NV20
1924 +#define DRM_FORMAT_NV20 fourcc_code('N', 'V', '2', '0')
1927 uint64_t ff_v4l2_request_get_capture_timestamp(AVFrame *frame)
1929 V4L2RequestDescriptor *req = (V4L2RequestDescriptor*)frame->data[0];