1 From 40a990827399c05ad3ce3f8242321bd8a67aa0bd Mon Sep 17 00:00:00 2001
2 From: Lukas Rusak <lorusak@gmail.com>
3 Date: Tue, 24 Apr 2018 23:00:23 -0700
4 Subject: [PATCH 1/9] libavcodec: v4l2m2m: output AVDRMFrameDescriptor
6 This allows for a zero-copy output by exporting the v4l2 buffer then wrapping that buffer
7 in the AVDRMFrameDescriptor like it is done in rkmpp.
9 This has been in use for quite some time with great success on many platforms including:
15 This was developed in conjunction with Kodi to allow handling the zero-copy buffer rendering.
16 A simply utility for testing is also available here: https://github.com/BayLibre/ffmpeg-drm
19 - allow selecting pixel format output from decoder
20 - allow configuring amount of output and capture buffers
23 - allow selecting AV_PIX_FMT_DRM_PRIME
26 - use get_format to select AV_PIX_FMT_DRM_PRIME
28 - add handling of AV_PIX_FMT_YUV420P format (for raspberry pi)
29 - add handling of AV_PIX_FMT_YUYV422 format (for i.MX6 coda decoder)
37 libavcodec/v4l2_buffers.c | 155 ++++++++++++++++++++++++++++++++++++--
38 libavcodec/v4l2_buffers.h | 4 +
39 libavcodec/v4l2_context.c | 40 +++++++++-
40 libavcodec/v4l2_m2m.h | 3 +
41 libavcodec/v4l2_m2m_dec.c | 23 ++++++
42 5 files changed, 213 insertions(+), 12 deletions(-)
44 diff --git a/libavcodec/v4l2_buffers.c b/libavcodec/v4l2_buffers.c
45 index 4b2679eb38..cbd3e5680d 100644
46 --- a/libavcodec/v4l2_buffers.c
47 +++ b/libavcodec/v4l2_buffers.c
49 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
52 +#include <drm/drm_fourcc.h>
53 #include <linux/videodev2.h>
54 #include <sys/ioctl.h>
57 #include "libavcodec/avcodec.h"
58 #include "libavcodec/internal.h"
59 #include "libavutil/pixdesc.h"
60 +#include "libavutil/hwcontext.h"
61 #include "v4l2_context.h"
62 #include "v4l2_buffers.h"
64 @@ -210,7 +212,79 @@ static enum AVColorTransferCharacteristic v4l2_get_color_trc(V4L2Buffer *buf)
65 return AVCOL_TRC_UNSPECIFIED;
68 -static void v4l2_free_buffer(void *opaque, uint8_t *unused)
69 +static uint8_t * v4l2_get_drm_frame(V4L2Buffer *avbuf)
71 + AVDRMFrameDescriptor *drm_desc = &avbuf->drm_frame;
72 + AVDRMLayerDescriptor *layer;
74 + /* fill the DRM frame descriptor */
75 + drm_desc->nb_objects = avbuf->num_planes;
76 + drm_desc->nb_layers = 1;
78 + layer = &drm_desc->layers[0];
79 + layer->nb_planes = avbuf->num_planes;
81 + for (int i = 0; i < avbuf->num_planes; i++) {
82 + layer->planes[i].object_index = i;
83 + layer->planes[i].offset = 0;
84 + layer->planes[i].pitch = avbuf->plane_info[i].bytesperline;
87 + switch (avbuf->context->av_pix_fmt) {
88 + case AV_PIX_FMT_YUYV422:
90 + layer->format = DRM_FORMAT_YUYV;
91 + layer->nb_planes = 1;
95 + case AV_PIX_FMT_NV12:
96 + case AV_PIX_FMT_NV21:
98 + layer->format = avbuf->context->av_pix_fmt == AV_PIX_FMT_NV12 ?
99 + DRM_FORMAT_NV12 : DRM_FORMAT_NV21;
101 + if (avbuf->num_planes > 1)
104 + layer->nb_planes = 2;
106 + layer->planes[1].object_index = 0;
107 + layer->planes[1].offset = avbuf->plane_info[0].bytesperline *
108 + avbuf->context->format.fmt.pix.height;
109 + layer->planes[1].pitch = avbuf->plane_info[0].bytesperline;
112 + case AV_PIX_FMT_YUV420P:
114 + layer->format = DRM_FORMAT_YUV420;
116 + if (avbuf->num_planes > 1)
119 + layer->nb_planes = 3;
121 + layer->planes[1].object_index = 0;
122 + layer->planes[1].offset = avbuf->plane_info[0].bytesperline *
123 + avbuf->context->format.fmt.pix.height;
124 + layer->planes[1].pitch = avbuf->plane_info[0].bytesperline >> 1;
126 + layer->planes[2].object_index = 0;
127 + layer->planes[2].offset = layer->planes[1].offset +
128 + ((avbuf->plane_info[0].bytesperline *
129 + avbuf->context->format.fmt.pix.height) >> 2);
130 + layer->planes[2].pitch = avbuf->plane_info[0].bytesperline >> 1;
134 + drm_desc->nb_layers = 0;
138 + return (uint8_t *) drm_desc;
141 +static void v4l2_free_buffer(void *opaque, uint8_t *data)
143 V4L2Buffer* avbuf = opaque;
144 V4L2m2mContext *s = buf_to_m2mctx(avbuf);
145 @@ -234,6 +308,36 @@ static void v4l2_free_buffer(void *opaque, uint8_t *unused)
149 +static int v4l2_buffer_export_drm(V4L2Buffer* avbuf)
151 + struct v4l2_exportbuffer expbuf;
154 + for (i = 0; i < avbuf->num_planes; i++) {
155 + memset(&expbuf, 0, sizeof(expbuf));
157 + expbuf.index = avbuf->buf.index;
158 + expbuf.type = avbuf->buf.type;
161 + ret = ioctl(buf_to_m2mctx(avbuf)->fd, VIDIOC_EXPBUF, &expbuf);
163 + return AVERROR(errno);
165 + if (V4L2_TYPE_IS_MULTIPLANAR(avbuf->buf.type)) {
167 + avbuf->drm_frame.objects[i].size = avbuf->buf.m.planes[i].length;
168 + avbuf->drm_frame.objects[i].fd = expbuf.fd;
171 + avbuf->drm_frame.objects[0].size = avbuf->buf.length;
172 + avbuf->drm_frame.objects[0].fd = expbuf.fd;
179 static int v4l2_buf_increase_ref(V4L2Buffer *in)
181 V4L2m2mContext *s = buf_to_m2mctx(in);
182 @@ -254,6 +358,24 @@ static int v4l2_buf_increase_ref(V4L2Buffer *in)
186 +static int v4l2_buf_to_bufref_drm(V4L2Buffer *in, AVBufferRef **buf)
190 + *buf = av_buffer_create((uint8_t *) &in->drm_frame,
191 + sizeof(in->drm_frame),
193 + in, AV_BUFFER_FLAG_READONLY);
195 + return AVERROR(ENOMEM);
197 + ret = v4l2_buf_increase_ref(in);
199 + av_buffer_unref(buf);
204 static int v4l2_buf_to_bufref(V4L2Buffer *in, int plane, AVBufferRef **buf)
207 @@ -303,13 +425,24 @@ static int v4l2_buffer_buf_to_swframe(AVFrame *frame, V4L2Buffer *avbuf)
209 frame->format = avbuf->context->av_pix_fmt;
211 - for (i = 0; i < avbuf->num_planes; i++) {
212 - ret = v4l2_buf_to_bufref(avbuf, i, &frame->buf[i]);
213 + if (buf_to_m2mctx(avbuf)->output_drm) {
214 + /* 1. get references to the actual data */
215 + ret = v4l2_buf_to_bufref_drm(avbuf, &frame->buf[0]);
219 - frame->linesize[i] = avbuf->plane_info[i].bytesperline;
220 - frame->data[i] = frame->buf[i]->data;
221 + frame->data[0] = (uint8_t *) v4l2_get_drm_frame(avbuf);
222 + frame->format = AV_PIX_FMT_DRM_PRIME;
224 + /* 1. get references to the actual data */
225 + for (i = 0; i < avbuf->num_planes; i++) {
226 + ret = v4l2_buf_to_bufref(avbuf, i, &frame->buf[i]);
230 + frame->linesize[i] = avbuf->plane_info[i].bytesperline;
231 + frame->data[i] = frame->buf[i]->data;
235 /* fixup special cases */
236 @@ -543,9 +676,6 @@ int ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index)
238 avbuf->status = V4L2BUF_AVAILABLE;
240 - if (V4L2_TYPE_IS_OUTPUT(ctx->type))
243 if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
244 avbuf->buf.m.planes = avbuf->planes;
245 avbuf->buf.length = avbuf->num_planes;
246 @@ -555,6 +685,15 @@ int ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index)
247 avbuf->buf.length = avbuf->planes[0].length;
250 + if (V4L2_TYPE_IS_OUTPUT(ctx->type))
253 + if (buf_to_m2mctx(avbuf)->output_drm) {
254 + ret = v4l2_buffer_export_drm(avbuf);
259 return ff_v4l2_buffer_enqueue(avbuf);
262 diff --git a/libavcodec/v4l2_buffers.h b/libavcodec/v4l2_buffers.h
263 index 8dbc7fc104..037e667997 100644
264 --- a/libavcodec/v4l2_buffers.h
265 +++ b/libavcodec/v4l2_buffers.h
267 #include <stdatomic.h>
268 #include <linux/videodev2.h>
270 +#include "libavutil/hwcontext_drm.h"
273 enum V4L2Buffer_status {
274 @@ -42,6 +43,9 @@ typedef struct V4L2Buffer {
275 /* each buffer needs to have a reference to its context */
276 struct V4L2Context *context;
278 + /* DRM descriptor */
279 + AVDRMFrameDescriptor drm_frame;
281 /* This object is refcounted per-plane, so we need to keep track
282 * of how many context-refs we are holding. */
283 AVBufferRef *context_ref;
284 diff --git a/libavcodec/v4l2_context.c b/libavcodec/v4l2_context.c
285 index ff1ea8e57b..e9e8c27a54 100644
286 --- a/libavcodec/v4l2_context.c
287 +++ b/libavcodec/v4l2_context.c
288 @@ -455,22 +455,54 @@ static int v4l2_release_buffers(V4L2Context* ctx)
289 struct v4l2_requestbuffers req = {
290 .memory = V4L2_MEMORY_MMAP,
292 - .count = 0, /* 0 -> unmaps buffers from the driver */
293 + .count = 0, /* 0 -> unmap all buffers from the driver */
298 for (i = 0; i < ctx->num_buffers; i++) {
299 V4L2Buffer *buffer = &ctx->buffers[i];
301 for (j = 0; j < buffer->num_planes; j++) {
302 struct V4L2Plane_info *p = &buffer->plane_info[j];
304 + if (V4L2_TYPE_IS_OUTPUT(ctx->type)) {
305 + /* output buffers are not EXPORTED */
309 + if (ctx_to_m2mctx(ctx)->output_drm) {
310 + /* use the DRM frame to close */
311 + if (buffer->drm_frame.objects[j].fd >= 0) {
312 + if (close(buffer->drm_frame.objects[j].fd) < 0) {
313 + av_log(logger(ctx), AV_LOG_ERROR, "%s close drm fd "
314 + "[buffer=%2d, plane=%d, fd=%2d] - %s \n",
315 + ctx->name, i, j, buffer->drm_frame.objects[j].fd,
316 + av_err2str(AVERROR(errno)));
321 if (p->mm_addr && p->length)
322 if (munmap(p->mm_addr, p->length) < 0)
323 - av_log(logger(ctx), AV_LOG_ERROR, "%s unmap plane (%s))\n", ctx->name, av_err2str(AVERROR(errno)));
324 + av_log(logger(ctx), AV_LOG_ERROR, "%s unmap plane (%s))\n",
325 + ctx->name, av_err2str(AVERROR(errno)));
329 - return ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_REQBUFS, &req);
330 + ret = ioctl(ctx_to_m2mctx(ctx)->fd, VIDIOC_REQBUFS, &req);
332 + av_log(logger(ctx), AV_LOG_ERROR, "release all %s buffers (%s)\n",
333 + ctx->name, av_err2str(AVERROR(errno)));
335 + if (ctx_to_m2mctx(ctx)->output_drm)
336 + av_log(logger(ctx), AV_LOG_ERROR,
337 + "Make sure the DRM client releases all FB/GEM objects before closing the codec (ie):\n"
338 + "for all buffers: \n"
339 + " 1. drmModeRmFB(..)\n"
340 + " 2. drmIoctl(.., DRM_IOCTL_GEM_CLOSE,... )\n");
346 static inline int v4l2_try_raw_format(V4L2Context* ctx, enum AVPixelFormat pixfmt)
347 diff --git a/libavcodec/v4l2_m2m.h b/libavcodec/v4l2_m2m.h
348 index b67b216331..0fbd19a013 100644
349 --- a/libavcodec/v4l2_m2m.h
350 +++ b/libavcodec/v4l2_m2m.h
351 @@ -66,6 +66,9 @@ typedef struct V4L2m2mContext {
353 /* reference back to V4L2m2mPriv */
356 + /* generate DRM frames */
360 typedef struct V4L2m2mPriv {
361 diff --git a/libavcodec/v4l2_m2m_dec.c b/libavcodec/v4l2_m2m_dec.c
362 index ab07c0a24a..6bc7442702 100644
363 --- a/libavcodec/v4l2_m2m_dec.c
364 +++ b/libavcodec/v4l2_m2m_dec.c
367 #include <linux/videodev2.h>
368 #include <sys/ioctl.h>
370 +#include "libavutil/hwcontext.h"
371 +#include "libavutil/hwcontext_drm.h"
372 #include "libavutil/pixfmt.h"
373 #include "libavutil/pixdesc.h"
374 #include "libavutil/opt.h"
376 #include "libavcodec/decode.h"
377 #include "libavcodec/internal.h"
379 +#include "libavcodec/hwaccels.h"
380 +#include "libavcodec/internal.h"
382 #include "v4l2_context.h"
383 #include "v4l2_m2m.h"
384 #include "v4l2_fmt.h"
385 @@ -201,6 +207,15 @@ static av_cold int v4l2_decode_init(AVCodecContext *avctx)
386 capture->av_codec_id = AV_CODEC_ID_RAWVIDEO;
387 capture->av_pix_fmt = avctx->pix_fmt;
389 + /* the client requests the codec to generate DRM frames:
390 + * - data[0] will therefore point to the returned AVDRMFrameDescriptor
391 + * check the ff_v4l2_buffer_to_avframe conversion function.
392 + * - the DRM frame format is passed in the DRM frame descriptor layer.
393 + * check the v4l2_get_drm_frame function.
395 + if (ff_get_format(avctx, avctx->codec->pix_fmts) == AV_PIX_FMT_DRM_PRIME)
399 ret = ff_v4l2_m2m_codec_init(priv);
401 @@ -226,6 +241,11 @@ static const AVOption options[] = {
405 +static const AVCodecHWConfigInternal *v4l2_m2m_hw_configs[] = {
406 + HW_CONFIG_INTERNAL(DRM_PRIME),
410 #define M2MDEC_CLASS(NAME) \
411 static const AVClass v4l2_m2m_ ## NAME ## _dec_class = { \
412 .class_name = #NAME "_v4l2m2m_decoder", \
413 @@ -249,6 +269,9 @@ static const AVOption options[] = {
415 .capabilities = AV_CODEC_CAP_HARDWARE | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING, \
416 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS | FF_CODEC_CAP_INIT_CLEANUP, \
417 + .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_DRM_PRIME, \
418 + AV_PIX_FMT_NONE}, \
419 + .hw_configs = v4l2_m2m_hw_configs, \
420 .wrapper_name = "v4l2m2m", \
424 From 9a2a361c2c84c8da54cd3a74b0d0bb966df8fe69 Mon Sep 17 00:00:00 2001
425 From: Lukas Rusak <lorusak@gmail.com>
426 Date: Thu, 16 Aug 2018 21:09:40 -0700
427 Subject: [PATCH 2/9] libavcodec: v4l2m2m: depends on libdrm
431 libavcodec/v4l2_buffers.c | 2 +-
432 2 files changed, 2 insertions(+), 1 deletion(-)
434 diff --git a/configure b/configure
435 index d7a3f507e8..d203f6f7da 100755
438 @@ -3437,6 +3437,7 @@ sndio_indev_deps="sndio"
439 sndio_outdev_deps="sndio"
440 v4l2_indev_deps_any="linux_videodev2_h sys_videoio_h"
441 v4l2_indev_suggest="libv4l2"
442 +v4l2_outdev_deps="libdrm"
443 v4l2_outdev_deps_any="linux_videodev2_h sys_videoio_h"
444 v4l2_outdev_suggest="libv4l2"
445 vfwcap_indev_deps="vfw32 vfwcap_defines"
446 diff --git a/libavcodec/v4l2_buffers.c b/libavcodec/v4l2_buffers.c
447 index cbd3e5680d..bebe2c1796 100644
448 --- a/libavcodec/v4l2_buffers.c
449 +++ b/libavcodec/v4l2_buffers.c
451 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
454 -#include <drm/drm_fourcc.h>
455 +#include <drm_fourcc.h>
456 #include <linux/videodev2.h>
457 #include <sys/ioctl.h>
458 #include <sys/mman.h>
460 From 7b0fa2d859c12a8a129c884d16673ca731336c06 Mon Sep 17 00:00:00 2001
461 From: Lukas Rusak <lorusak@gmail.com>
462 Date: Thu, 16 Aug 2018 21:10:13 -0700
463 Subject: [PATCH 3/9] libavcodec: v4l2m2m: set format_modifier to
464 DRM_FORMAT_MOD_LINEAR
467 libavcodec/v4l2_buffers.c | 2 ++
468 1 file changed, 2 insertions(+)
470 diff --git a/libavcodec/v4l2_buffers.c b/libavcodec/v4l2_buffers.c
471 index bebe2c1796..12037d5d66 100644
472 --- a/libavcodec/v4l2_buffers.c
473 +++ b/libavcodec/v4l2_buffers.c
474 @@ -328,10 +328,12 @@ static int v4l2_buffer_export_drm(V4L2Buffer* avbuf)
476 avbuf->drm_frame.objects[i].size = avbuf->buf.m.planes[i].length;
477 avbuf->drm_frame.objects[i].fd = expbuf.fd;
478 + avbuf->drm_frame.objects[i].format_modifier = DRM_FORMAT_MOD_LINEAR;
481 avbuf->drm_frame.objects[0].size = avbuf->buf.length;
482 avbuf->drm_frame.objects[0].fd = expbuf.fd;
483 + avbuf->drm_frame.objects[0].format_modifier = DRM_FORMAT_MOD_LINEAR;
488 From e1857456c8f24e40d5c898886f2f51014e59ee9d Mon Sep 17 00:00:00 2001
489 From: Lukas Rusak <lorusak@gmail.com>
490 Date: Thu, 16 Aug 2018 21:10:53 -0700
491 Subject: [PATCH 4/9] libavcodec: v4l2m2m: only mmap the buffer when it is
492 output type and drm prime is used
495 libavcodec/v4l2_buffers.c | 20 ++++++++++++++------
496 1 file changed, 14 insertions(+), 6 deletions(-)
498 diff --git a/libavcodec/v4l2_buffers.c b/libavcodec/v4l2_buffers.c
499 index 12037d5d66..1adf518ab9 100644
500 --- a/libavcodec/v4l2_buffers.c
501 +++ b/libavcodec/v4l2_buffers.c
502 @@ -662,14 +662,22 @@ int ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index)
504 if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
505 avbuf->plane_info[i].length = avbuf->buf.m.planes[i].length;
506 - avbuf->plane_info[i].mm_addr = mmap(NULL, avbuf->buf.m.planes[i].length,
507 - PROT_READ | PROT_WRITE, MAP_SHARED,
508 - buf_to_m2mctx(avbuf)->fd, avbuf->buf.m.planes[i].m.mem_offset);
510 + if ((V4L2_TYPE_IS_OUTPUT(ctx->type) && buf_to_m2mctx(avbuf)->output_drm) ||
511 + !buf_to_m2mctx(avbuf)->output_drm) {
512 + avbuf->plane_info[i].mm_addr = mmap(NULL, avbuf->buf.m.planes[i].length,
513 + PROT_READ | PROT_WRITE, MAP_SHARED,
514 + buf_to_m2mctx(avbuf)->fd, avbuf->buf.m.planes[i].m.mem_offset);
517 avbuf->plane_info[i].length = avbuf->buf.length;
518 - avbuf->plane_info[i].mm_addr = mmap(NULL, avbuf->buf.length,
519 - PROT_READ | PROT_WRITE, MAP_SHARED,
520 - buf_to_m2mctx(avbuf)->fd, avbuf->buf.m.offset);
522 + if ((V4L2_TYPE_IS_OUTPUT(ctx->type) && buf_to_m2mctx(avbuf)->output_drm) ||
523 + !buf_to_m2mctx(avbuf)->output_drm) {
524 + avbuf->plane_info[i].mm_addr = mmap(NULL, avbuf->buf.length,
525 + PROT_READ | PROT_WRITE, MAP_SHARED,
526 + buf_to_m2mctx(avbuf)->fd, avbuf->buf.m.offset);
530 if (avbuf->plane_info[i].mm_addr == MAP_FAILED)
532 From c8fc3ea1b5777546f7ec72a54b053a2d4fa9fd59 Mon Sep 17 00:00:00 2001
533 From: Lukas Rusak <lorusak@gmail.com>
534 Date: Thu, 16 Aug 2018 21:11:38 -0700
535 Subject: [PATCH 5/9] libavcodec: v4l2m2m: allow using software pixel formats
538 libavcodec/v4l2_m2m_dec.c | 11 ++++++++++-
539 1 file changed, 10 insertions(+), 1 deletion(-)
541 diff --git a/libavcodec/v4l2_m2m_dec.c b/libavcodec/v4l2_m2m_dec.c
542 index 6bc7442702..4b9baf833c 100644
543 --- a/libavcodec/v4l2_m2m_dec.c
544 +++ b/libavcodec/v4l2_m2m_dec.c
545 @@ -213,8 +213,16 @@ static av_cold int v4l2_decode_init(AVCodecContext *avctx)
546 * - the DRM frame format is passed in the DRM frame descriptor layer.
547 * check the v4l2_get_drm_frame function.
549 - if (ff_get_format(avctx, avctx->codec->pix_fmts) == AV_PIX_FMT_DRM_PRIME)
550 + switch (ff_get_format(avctx, avctx->codec->pix_fmts)) {
551 + case AV_PIX_FMT_DRM_PRIME:
554 + case AV_PIX_FMT_NONE:
562 ret = ff_v4l2_m2m_codec_init(priv);
563 @@ -270,6 +278,7 @@ static const AVCodecHWConfigInternal *v4l2_m2m_hw_configs[] = {
564 .capabilities = AV_CODEC_CAP_HARDWARE | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING, \
565 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS | FF_CODEC_CAP_INIT_CLEANUP, \
566 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_DRM_PRIME, \
569 .hw_configs = v4l2_m2m_hw_configs, \
570 .wrapper_name = "v4l2m2m", \
572 From 13f02e940f083f19dbe8b9ac8fc7df45700dd36e Mon Sep 17 00:00:00 2001
573 From: Lukas Rusak <lorusak@gmail.com>
574 Date: Mon, 24 Sep 2018 13:39:31 -0700
575 Subject: [PATCH 6/9] libavcodec: v4l2m2m: implement hwcontext
578 libavcodec/v4l2_buffers.c | 22 ++++++++++++++++++++++
579 libavcodec/v4l2_context.h | 2 ++
580 libavcodec/v4l2_m2m.h | 2 ++
581 libavcodec/v4l2_m2m_dec.c | 11 +++++++++++
582 4 files changed, 37 insertions(+)
584 diff --git a/libavcodec/v4l2_buffers.c b/libavcodec/v4l2_buffers.c
585 index 1adf518ab9..6e2a544394 100644
586 --- a/libavcodec/v4l2_buffers.c
587 +++ b/libavcodec/v4l2_buffers.c
588 @@ -435,6 +435,7 @@ static int v4l2_buffer_buf_to_swframe(AVFrame *frame, V4L2Buffer *avbuf)
590 frame->data[0] = (uint8_t *) v4l2_get_drm_frame(avbuf);
591 frame->format = AV_PIX_FMT_DRM_PRIME;
592 + frame->hw_frames_ctx = av_buffer_ref(avbuf->context->frames_ref);
594 /* 1. get references to the actual data */
595 for (i = 0; i < avbuf->num_planes; i++) {
596 @@ -635,6 +636,27 @@ int ff_v4l2_buffer_initialize(V4L2Buffer* avbuf, int index)
597 avbuf->buf.type = ctx->type;
598 avbuf->buf.index = index;
600 + if (buf_to_m2mctx(avbuf)->output_drm) {
601 + AVHWFramesContext *hwframes;
603 + av_buffer_unref(&ctx->frames_ref);
605 + ctx->frames_ref = av_hwframe_ctx_alloc(buf_to_m2mctx(avbuf)->device_ref);
606 + if (!ctx->frames_ref) {
607 + ret = AVERROR(ENOMEM);
611 + hwframes = (AVHWFramesContext*)ctx->frames_ref->data;
612 + hwframes->format = AV_PIX_FMT_DRM_PRIME;
613 + hwframes->sw_format = ctx->av_pix_fmt;
614 + hwframes->width = ctx->width;
615 + hwframes->height = ctx->height;
616 + ret = av_hwframe_ctx_init(ctx->frames_ref);
621 if (V4L2_TYPE_IS_MULTIPLANAR(ctx->type)) {
622 avbuf->buf.length = VIDEO_MAX_PLANES;
623 avbuf->buf.m.planes = avbuf->planes;
624 diff --git a/libavcodec/v4l2_context.h b/libavcodec/v4l2_context.h
625 index 22a9532444..e804e94131 100644
626 --- a/libavcodec/v4l2_context.h
627 +++ b/libavcodec/v4l2_context.h
628 @@ -92,6 +92,8 @@ typedef struct V4L2Context {
632 + AVBufferRef *frames_ref;
637 diff --git a/libavcodec/v4l2_m2m.h b/libavcodec/v4l2_m2m.h
638 index 0fbd19a013..adf5997bb5 100644
639 --- a/libavcodec/v4l2_m2m.h
640 +++ b/libavcodec/v4l2_m2m.h
641 @@ -67,6 +67,8 @@ typedef struct V4L2m2mContext {
642 /* reference back to V4L2m2mPriv */
645 + AVBufferRef *device_ref;
647 /* generate DRM frames */
650 diff --git a/libavcodec/v4l2_m2m_dec.c b/libavcodec/v4l2_m2m_dec.c
651 index 4b9baf833c..6c23693137 100644
652 --- a/libavcodec/v4l2_m2m_dec.c
653 +++ b/libavcodec/v4l2_m2m_dec.c
656 #include "libavcodec/hwaccels.h"
657 #include "libavcodec/internal.h"
658 +#include "libavcodec/hwconfig.h"
660 #include "v4l2_context.h"
661 #include "v4l2_m2m.h"
662 @@ -224,6 +225,16 @@ static av_cold int v4l2_decode_init(AVCodecContext *avctx)
666 + s->device_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_DRM);
667 + if (!s->device_ref) {
668 + ret = AVERROR(ENOMEM);
672 + ret = av_hwdevice_ctx_init(s->device_ref);
677 ret = ff_v4l2_m2m_codec_init(priv);
680 From 34be198b8039c9df434792f19f0985e45419407e Mon Sep 17 00:00:00 2001
681 From: Lukas Rusak <lorusak@gmail.com>
682 Date: Mon, 4 May 2020 13:01:29 -0700
683 Subject: [PATCH 7/9] libavcodec: v4l2m2m: allow lower minimum buffer values
685 There is no reason to enforce a high minimum. In the context
686 of streaming only a few output buffers and capture buffers
687 are even needed for continuous playback. This also helps
688 alleviate memory pressure when decoding 4K media.
690 libavcodec/v4l2_m2m.h | 2 +-
691 libavcodec/v4l2_m2m_dec.c | 2 +-
692 2 files changed, 2 insertions(+), 2 deletions(-)
694 diff --git a/libavcodec/v4l2_m2m.h b/libavcodec/v4l2_m2m.h
695 index adf5997bb5..1082b9dad2 100644
696 --- a/libavcodec/v4l2_m2m.h
697 +++ b/libavcodec/v4l2_m2m.h
700 #define V4L_M2M_DEFAULT_OPTS \
701 { "num_output_buffers", "Number of buffers in the output context",\
702 - OFFSET(num_output_buffers), AV_OPT_TYPE_INT, { .i64 = 16 }, 6, INT_MAX, FLAGS }
703 + OFFSET(num_output_buffers), AV_OPT_TYPE_INT, { .i64 = 16 }, 2, INT_MAX, FLAGS }
705 typedef struct V4L2m2mContext {
706 char devname[PATH_MAX];
707 diff --git a/libavcodec/v4l2_m2m_dec.c b/libavcodec/v4l2_m2m_dec.c
708 index 6c23693137..e323c37052 100644
709 --- a/libavcodec/v4l2_m2m_dec.c
710 +++ b/libavcodec/v4l2_m2m_dec.c
711 @@ -256,7 +256,7 @@ static av_cold int v4l2_decode_close(AVCodecContext *avctx)
712 static const AVOption options[] = {
713 V4L_M2M_DEFAULT_OPTS,
714 { "num_capture_buffers", "Number of buffers in the capture context",
715 - OFFSET(num_capture_buffers), AV_OPT_TYPE_INT, {.i64 = 20}, 20, INT_MAX, FLAGS },
716 + OFFSET(num_capture_buffers), AV_OPT_TYPE_INT, {.i64 = 20}, 2, INT_MAX, FLAGS },
721 From 2956fd1881d28abf6bf77bd9a57866c4ba81d199 Mon Sep 17 00:00:00 2001
722 From: Lukas Rusak <lorusak@gmail.com>
723 Date: Wed, 6 May 2020 11:12:58 -0700
724 Subject: [PATCH 8/9] libavcodec: v4l2m2m: add option to specify pixel format
728 libavcodec/v4l2_context.c | 9 +++++++++
729 libavcodec/v4l2_m2m.h | 2 ++
730 libavcodec/v4l2_m2m_dec.c | 1 +
731 3 files changed, 12 insertions(+)
733 diff --git a/libavcodec/v4l2_context.c b/libavcodec/v4l2_context.c
734 index e9e8c27a54..a97b70e836 100644
735 --- a/libavcodec/v4l2_context.c
736 +++ b/libavcodec/v4l2_context.c
737 @@ -531,6 +531,8 @@ static inline int v4l2_try_raw_format(V4L2Context* ctx, enum AVPixelFormat pixfm
739 static int v4l2_get_raw_format(V4L2Context* ctx, enum AVPixelFormat *p)
741 + V4L2m2mContext* s = ctx_to_m2mctx(ctx);
742 + V4L2m2mPriv *priv = s->avctx->priv_data;
743 enum AVPixelFormat pixfmt = ctx->av_pix_fmt;
744 struct v4l2_fmtdesc fdesc;
746 @@ -549,6 +551,13 @@ static int v4l2_get_raw_format(V4L2Context* ctx, enum AVPixelFormat *p)
748 return AVERROR(EINVAL);
750 + if (priv->pix_fmt != AV_PIX_FMT_NONE) {
751 + if (fdesc.pixelformat != ff_v4l2_format_avfmt_to_v4l2(priv->pix_fmt)) {
757 pixfmt = ff_v4l2_format_v4l2_to_avfmt(fdesc.pixelformat, AV_CODEC_ID_RAWVIDEO);
758 ret = v4l2_try_raw_format(ctx, pixfmt);
760 diff --git a/libavcodec/v4l2_m2m.h b/libavcodec/v4l2_m2m.h
761 index 1082b9dad2..943a8923c4 100644
762 --- a/libavcodec/v4l2_m2m.h
763 +++ b/libavcodec/v4l2_m2m.h
765 #include <linux/videodev2.h>
767 #include "libavcodec/avcodec.h"
768 +#include "libavutil/pixfmt.h"
769 #include "v4l2_context.h"
771 #define container_of(ptr, type, member) ({ \
772 @@ -81,6 +82,7 @@ typedef struct V4L2m2mPriv {
774 int num_output_buffers;
775 int num_capture_buffers;
776 + enum AVPixelFormat pix_fmt;
780 diff --git a/libavcodec/v4l2_m2m_dec.c b/libavcodec/v4l2_m2m_dec.c
781 index e323c37052..363e998142 100644
782 --- a/libavcodec/v4l2_m2m_dec.c
783 +++ b/libavcodec/v4l2_m2m_dec.c
784 @@ -257,6 +257,7 @@ static const AVOption options[] = {
785 V4L_M2M_DEFAULT_OPTS,
786 { "num_capture_buffers", "Number of buffers in the capture context",
787 OFFSET(num_capture_buffers), AV_OPT_TYPE_INT, {.i64 = 20}, 2, INT_MAX, FLAGS },
788 + { "pixel_format", "Pixel format to be used by the decoder", OFFSET(pix_fmt), AV_OPT_TYPE_PIXEL_FMT, {.i64 = AV_PIX_FMT_NONE}, AV_PIX_FMT_NONE, AV_PIX_FMT_NB, FLAGS },
793 From 2bb6d0cb244590f0c70dd111ed978cd87fa3bee1 Mon Sep 17 00:00:00 2001
794 From: Lukas Rusak <lorusak@gmail.com>
795 Date: Mon, 24 Sep 2018 13:39:56 -0700
796 Subject: [PATCH 9/9] libavcodec: v4l2m2m: implement flush
799 libavcodec/v4l2_m2m_dec.c | 36 ++++++++++++++++++++++++++++++++++++
800 1 file changed, 36 insertions(+)
802 diff --git a/libavcodec/v4l2_m2m_dec.c b/libavcodec/v4l2_m2m_dec.c
803 index 363e998142..52ec67cb59 100644
804 --- a/libavcodec/v4l2_m2m_dec.c
805 +++ b/libavcodec/v4l2_m2m_dec.c
806 @@ -250,6 +250,41 @@ static av_cold int v4l2_decode_close(AVCodecContext *avctx)
807 return ff_v4l2_m2m_codec_end(avctx->priv_data);
810 +static void v4l2_decode_flush(AVCodecContext *avctx)
812 + V4L2m2mPriv *priv = avctx->priv_data;
813 + V4L2m2mContext* s = priv->context;
814 + V4L2Context* output = &s->output;
815 + V4L2Context* capture = &s->capture;
818 + ret = ff_v4l2_context_set_status(output, VIDIOC_STREAMOFF);
820 + av_log(avctx, AV_LOG_ERROR, "VIDIOC_STREAMOFF %s error: %d\n", output->name, ret);
822 + ret = ff_v4l2_context_set_status(output, VIDIOC_STREAMON);
824 + av_log(avctx, AV_LOG_ERROR, "VIDIOC_STREAMON %s error: %d\n", output->name, ret);
826 + for (i = 0; i < output->num_buffers; i++) {
827 + if (output->buffers[i].status == V4L2BUF_IN_DRIVER)
828 + output->buffers[i].status = V4L2BUF_AVAILABLE;
831 + struct v4l2_decoder_cmd cmd = {
832 + .cmd = V4L2_DEC_CMD_START,
836 + ret = ioctl(s->fd, VIDIOC_DECODER_CMD, &cmd);
838 + av_log(avctx, AV_LOG_ERROR, "VIDIOC_DECODER_CMD start error: %d\n", errno);
845 #define OFFSET(x) offsetof(V4L2m2mPriv, x)
846 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
848 @@ -286,6 +321,7 @@ static const AVCodecHWConfigInternal *v4l2_m2m_hw_configs[] = {
849 .init = v4l2_decode_init, \
850 .receive_frame = v4l2_receive_frame, \
851 .close = v4l2_decode_close, \
852 + .flush = v4l2_decode_flush, \
854 .capabilities = AV_CODEC_CAP_HARDWARE | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING, \
855 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS | FF_CODEC_CAP_INIT_CLEANUP, \