summaryrefslogtreecommitdiffstats
path: root/multimedia/ffmpeg/patches/120-v4l2-request-01.patch
blob: 07526086d653c69c234bcbe81593b1c0c9f81eef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
From 900fa8e6625a0134432a1dccf87633b499af5fb1 Mon Sep 17 00:00:00 2001
From: Jonas Karlman <jonas@kwiboo.se>
Date: Tue, 6 Aug 2024 09:06:00 +0000
Subject: [PATCH 01/13] avutil/hwcontext: Add hwdevice type for V4L2 Request
 API

Add a hwdevice type for use with V4L2 Request API stateless decoding.

AVV4L2RequestFramesContext is expected to be filled with a V4L2 coded
pixel format and optional codec-specific extended controls before an
AVHWFramesContext is initialized.

Once initialized AVV4L2RequestFramesContextInternal describe the opened
V4L2 stateless decoder session, with details about media/video file
descriptors, formats of CAPTURE/OUTPUT queues and buffer pools for
creating CAPTURE/OUTPUT buffers.

AVHWFramesContext.pool defaults to create frame descriptors around a
CAPTURE buffer, and frames are returned with AVFrame.data[0] pointing
to a AVDRMFrameDescriptor and AVFrame.data[1] the CAPTURE buffer index.

Signed-off-by: Jonas Karlman <jonas@kwiboo.se>
---
 configure                                  |   8 +
 libavutil/Makefile                         |   4 +
 libavutil/hwcontext.c                      |   4 +
 libavutil/hwcontext.h                      |   1 +
 libavutil/hwcontext_internal.h             |   1 +
 libavutil/hwcontext_v4l2request.c          | 617 +++++++++++++++++++++
 libavutil/hwcontext_v4l2request.h          |  70 +++
 libavutil/hwcontext_v4l2request_internal.h |  67 +++
 8 files changed, 772 insertions(+)
 create mode 100644 libavutil/hwcontext_v4l2request.c
 create mode 100644 libavutil/hwcontext_v4l2request.h
 create mode 100644 libavutil/hwcontext_v4l2request_internal.h

--- a/configure
+++ b/configure
@@ -365,6 +365,7 @@ External library support:
   --disable-nvenc          disable Nvidia video encoding code [autodetect]
   --enable-rkmpp           enable Rockchip Media Process Platform code [no]
   --disable-v4l2-m2m       disable V4L2 mem2mem code [autodetect]
+  --enable-v4l2-request    enable V4L2 Request API code [no]
   --disable-vaapi          disable Video Acceleration API (mainly Unix/Intel) code [autodetect]
   --disable-vdpau          disable Nvidia Video Decode and Presentation API for Unix code [autodetect]
   --disable-videotoolbox   disable VideoToolbox code [autodetect]
@@ -2201,6 +2202,7 @@ HWACCEL_LIBRARY_LIST="
     libvpl
     mmal
     opencl
+    v4l2_request
 "
 
 DOCUMENT_LIST="
@@ -3400,6 +3402,8 @@ dxva2_deps="dxva2api_h DXVA2_ConfigPictu
 ffnvcodec_deps_any="libdl LoadLibrary"
 mediacodec_deps="android mediandk pthreads"
 nvdec_deps="ffnvcodec"
+v4l2_request_deps="linux_media_h v4l2_timeval_to_ns libdrm"
+v4l2_request_suggest="libdrm"
 vaapi_x11_deps="xlib_x11"
 videotoolbox_hwaccel_deps="videotoolbox pthreads"
 videotoolbox_hwaccel_extralibs="-framework QuartzCore"
@@ -7656,6 +7660,10 @@ if enabled v4l2_m2m; then
     check_cc vp9_v4l2_m2m linux/videodev2.h "int i = V4L2_PIX_FMT_VP9;"
 fi
 
+if enabled v4l2_request; then
+    check_func_headers "linux/media.h linux/videodev2.h" v4l2_timeval_to_ns
+fi
+
 check_headers sys/videoio.h
 test_code cc sys/videoio.h "struct v4l2_frmsizeenum vfse; vfse.discrete.width = 0;" && enable_sanitized struct_v4l2_frmivalenum_discrete
 
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -52,6 +52,7 @@ HEADERS = adler32.h
           hwcontext_mediacodec.h                                        \
           hwcontext_opencl.h                                            \
           hwcontext_oh.h                                                \
+          hwcontext_v4l2request.h                                       \
           hwcontext_vaapi.h                                             \
           hwcontext_videotoolbox.h                                      \
           hwcontext_vdpau.h                                             \
@@ -216,6 +217,7 @@ OBJS-$(CONFIG_MEDIACODEC)
 OBJS-$(CONFIG_OHCODEC)                  += hwcontext_oh.o
 OBJS-$(CONFIG_OPENCL)                   += hwcontext_opencl.o
 OBJS-$(CONFIG_QSV)                      += hwcontext_qsv.o
+OBJS-$(CONFIG_V4L2_REQUEST)             += hwcontext_v4l2request.o
 OBJS-$(CONFIG_VAAPI)                    += hwcontext_vaapi.o
 OBJS-$(CONFIG_VIDEOTOOLBOX)             += hwcontext_videotoolbox.o
 OBJS-$(CONFIG_VDPAU)                    += hwcontext_vdpau.o
@@ -247,6 +249,8 @@ SKIPHEADERS-$(CONFIG_AMF)              +
                                           hwcontext_amf_internal.h
 SKIPHEADERS-$(CONFIG_QSV)              += hwcontext_qsv.h
 SKIPHEADERS-$(CONFIG_OPENCL)           += hwcontext_opencl.h
+SKIPHEADERS-$(CONFIG_V4L2_REQUEST)     += hwcontext_v4l2request.h       \
+                                          hwcontext_v4l2request_internal.h
 SKIPHEADERS-$(CONFIG_VAAPI)            += hwcontext_vaapi.h
 SKIPHEADERS-$(CONFIG_VIDEOTOOLBOX)     += hwcontext_videotoolbox.h
 SKIPHEADERS-$(CONFIG_VDPAU)            += hwcontext_vdpau.h
--- a/libavutil/hwcontext.c
+++ b/libavutil/hwcontext.c
@@ -72,6 +72,9 @@ static const HWContextType * const hw_ta
 #if CONFIG_OHCODEC
     &ff_hwcontext_type_oh,
 #endif
+#if CONFIG_V4L2_REQUEST
+    &ff_hwcontext_type_v4l2request,
+#endif
     NULL,
 };
 
@@ -83,6 +86,7 @@ static const char *const hw_type_names[]
     [AV_HWDEVICE_TYPE_D3D12VA] = "d3d12va",
     [AV_HWDEVICE_TYPE_OPENCL] = "opencl",
     [AV_HWDEVICE_TYPE_QSV]    = "qsv",
+    [AV_HWDEVICE_TYPE_V4L2REQUEST] = "v4l2request",
     [AV_HWDEVICE_TYPE_VAAPI]  = "vaapi",
     [AV_HWDEVICE_TYPE_VDPAU]  = "vdpau",
     [AV_HWDEVICE_TYPE_VIDEOTOOLBOX] = "videotoolbox",
--- a/libavutil/hwcontext.h
+++ b/libavutil/hwcontext.h
@@ -41,6 +41,7 @@ enum AVHWDeviceType {
     AV_HWDEVICE_TYPE_AMF,
     /* OpenHarmony Codec device */
     AV_HWDEVICE_TYPE_OHCODEC,
+    AV_HWDEVICE_TYPE_V4L2REQUEST,
 };
 
 /**
--- a/libavutil/hwcontext_internal.h
+++ b/libavutil/hwcontext_internal.h
@@ -158,6 +158,7 @@ extern const HWContextType ff_hwcontext_
 extern const HWContextType ff_hwcontext_type_dxva2;
 extern const HWContextType ff_hwcontext_type_opencl;
 extern const HWContextType ff_hwcontext_type_qsv;
+extern const HWContextType ff_hwcontext_type_v4l2request;
 extern const HWContextType ff_hwcontext_type_vaapi;
 extern const HWContextType ff_hwcontext_type_vdpau;
 extern const HWContextType ff_hwcontext_type_videotoolbox;
--- /dev/null
+++ b/libavutil/hwcontext_v4l2request.c
@@ -0,0 +1,617 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "config.h"
+
+#include <fcntl.h>
+#include <linux/dma-buf.h>
+#include <linux/media.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include <drm_fourcc.h>
+
+#include "avassert.h"
+#include "hwcontext_drm.h"
+#include "hwcontext_internal.h"
+#include "hwcontext_v4l2request_internal.h"
+#include "mem.h"
+
+typedef struct V4L2RequestDeviceContext {
+} V4L2RequestDeviceContext;
+
+typedef struct V4L2RequestFramesContext {
+    AVV4L2RequestFramesContext p;
+    AVV4L2RequestFramesContextInternal internal;
+} V4L2RequestFramesContext;
+
+typedef struct V4L2RequestFrameDescriptor {
+    AVDRMFrameDescriptor base;
+    AVBufferRef *ref;
+    uint32_t index;
+    int fd[AV_DRM_MAX_PLANES];
+} V4L2RequestFrameDescriptor;
+
+static const struct {
+    uint32_t pixelformat;
+    enum AVPixelFormat sw_format;
+    uint32_t drm_format;
+    uint64_t format_modifier;
+    uint32_t bit_depth;
+} v4l2request_capture_pixelformats[] = {
+    { V4L2_PIX_FMT_NV12, AV_PIX_FMT_NV12, DRM_FORMAT_NV12, DRM_FORMAT_MOD_LINEAR, 8 },
+#if defined(V4L2_PIX_FMT_NV12_32L32)
+    { V4L2_PIX_FMT_NV12_32L32, AV_PIX_FMT_YUV420P, DRM_FORMAT_NV12, DRM_FORMAT_MOD_ALLWINNER_TILED, 8 },
+#endif
+#if defined(V4L2_PIX_FMT_NV15) && defined(DRM_FORMAT_NV15)
+    { V4L2_PIX_FMT_NV15, AV_PIX_FMT_YUV420P10, DRM_FORMAT_NV15, DRM_FORMAT_MOD_LINEAR, 10 },
+#endif
+    { V4L2_PIX_FMT_NV16, AV_PIX_FMT_NV16, DRM_FORMAT_NV16, DRM_FORMAT_MOD_LINEAR, 8 },
+#if defined(V4L2_PIX_FMT_NV20) && defined(DRM_FORMAT_NV20)
+    { V4L2_PIX_FMT_NV20, AV_PIX_FMT_YUV422P10, DRM_FORMAT_NV20, DRM_FORMAT_MOD_LINEAR, 10 },
+#endif
+#if defined(V4L2_PIX_FMT_P010) && defined(DRM_FORMAT_P010)
+    { V4L2_PIX_FMT_P010, AV_PIX_FMT_P010, DRM_FORMAT_P010, DRM_FORMAT_MOD_LINEAR, 10 },
+#endif
+#if defined(V4L2_PIX_FMT_NV12MT_COL128) && defined(V4L2_PIX_FMT_NV12MT_10_COL128)
+    { V4L2_PIX_FMT_NV12MT_COL128, AV_PIX_FMT_YUV420P, DRM_FORMAT_NV12, DRM_FORMAT_MOD_BROADCOM_SAND128, 8 },
+#if defined(DRM_FORMAT_P030)
+    { V4L2_PIX_FMT_NV12MT_10_COL128, AV_PIX_FMT_YUV420P10, DRM_FORMAT_P030, DRM_FORMAT_MOD_BROADCOM_SAND128, 10 },
+#endif
+#endif
+};
+
+static int v4l2request_set_drm_descriptor(AVDRMFrameDescriptor *desc,
+                                          struct v4l2_format *format)
+{
+    AVDRMLayerDescriptor *layer = &desc->layers[0];
+    uint32_t pixelformat = V4L2_TYPE_IS_MULTIPLANAR(format->type) ?
+                           format->fmt.pix_mp.pixelformat :
+                           format->fmt.pix.pixelformat;
+    uint64_t format_modifier;
+
+    layer->format = 0;
+    for (int i = 0; i < FF_ARRAY_ELEMS(v4l2request_capture_pixelformats); i++) {
+        if (pixelformat == v4l2request_capture_pixelformats[i].pixelformat) {
+            layer->format = v4l2request_capture_pixelformats[i].drm_format;
+            format_modifier = v4l2request_capture_pixelformats[i].format_modifier;
+            break;
+        }
+    }
+    if (!layer->format)
+        return AVERROR(ENOENT);
+
+    for (int i = 0; i < desc->nb_objects; i++) {
+        desc->objects[i].format_modifier = format_modifier;
+        desc->objects[i].size = V4L2_TYPE_IS_MULTIPLANAR(format->type) ?
+                                format->fmt.pix_mp.plane_fmt[i].sizeimage :
+                                format->fmt.pix.sizeimage;
+    }
+
+    desc->nb_layers = 1;
+    layer->nb_planes = 2;
+
+    layer->planes[0].object_index = 0;
+    layer->planes[0].offset = 0;
+    layer->planes[0].pitch = V4L2_TYPE_IS_MULTIPLANAR(format->type) ?
+                             format->fmt.pix_mp.plane_fmt[0].bytesperline :
+                             format->fmt.pix.bytesperline;
+
+    layer->planes[1].object_index = 0;
+    layer->planes[1].offset = layer->planes[0].pitch *
+                              (V4L2_TYPE_IS_MULTIPLANAR(format->type) ?
+                               format->fmt.pix_mp.height :
+                               format->fmt.pix.height);
+    layer->planes[1].pitch = layer->planes[0].pitch;
+
+#if defined(V4L2_PIX_FMT_NV12MT_COL128) && defined(V4L2_PIX_FMT_NV12MT_10_COL128)
+    // Raspberry Pi formats need special handling
+    if (pixelformat == V4L2_PIX_FMT_NV12MT_COL128 ||
+        pixelformat == V4L2_PIX_FMT_NV12MT_10_COL128) {
+        layer->planes[1].object_index = 1;
+        layer->planes[1].offset = 0;
+        layer->planes[0].pitch = (V4L2_TYPE_IS_MULTIPLANAR(format->type) ?
+                                  format->fmt.pix_mp.height :
+                                  format->fmt.pix.height);
+        layer->planes[1].pitch = layer->planes[0].pitch / 2;
+    }
+#endif
+
+    return 0;
+}
+
+static void v4l2request_device_uninit(AVHWDeviceContext *hwdev)
+{
+}
+
+static int v4l2request_device_create(AVHWDeviceContext *hwdev, const char *device,
+                                     AVDictionary *opts, int flags)
+{
+    return 0;
+}
+
+static int v4l2request_open_decoder(AVHWFramesContext *hwfc)
+{
+    AVV4L2RequestFramesContext *fctx = hwfc->hwctx;
+
+    // Ensure codec pixelformat is set
+    if (!fctx->pixelformat)
+        return AVERROR(EINVAL);
+
+    // TODO: locate a decoder supporting the requested pixelformat
+
+    return AVERROR(ENOSYS);
+}
+
+static AVBufferRef *v4l2request_v4l2_buffer_alloc(AVHWFramesContext *hwfc,
+                                                  struct v4l2_format *format)
+{
+    AVV4L2RequestFramesContext *fctx = hwfc->hwctx;
+    AVV4L2RequestFramesContextInternal *fctxi = fctx->internal;
+    struct v4l2_create_buffers buffers = {
+        .count = 1,
+        .memory = V4L2_MEMORY_MMAP,
+        .format = *format,
+    };
+    struct v4l2_buffer *buffer;
+    uint8_t num_planes;
+    AVBufferRef *ref;
+
+    num_planes = V4L2_TYPE_IS_MULTIPLANAR(format->type) ?
+                 format->fmt.pix_mp.num_planes : 0;
+
+    ref = av_buffer_allocz(sizeof(struct v4l2_buffer) +
+                           (sizeof(struct v4l2_plane) * num_planes));
+    if (!ref)
+        return NULL;
+
+    buffer = (struct v4l2_buffer *)ref->data;
+    buffer->type = format->type;
+
+    if (num_planes) {
+        buffer->length = num_planes;
+        buffer->m.planes = (struct v4l2_plane *)(buffer + 1);
+    }
+
+    // Create the buffer
+    if (ioctl(fctxi->video_fd, VIDIOC_CREATE_BUFS, &buffers) < 0) {
+        av_log(hwfc, AV_LOG_ERROR, "Failed to create buffer of type %d: %s (%d)\n",
+               buffer->type, strerror(errno), errno);
+        goto fail;
+    }
+
+    buffer->memory = buffers.memory;
+    buffer->index = buffers.index;
+
+    // Query more details of the created buffer
+    if (ioctl(fctxi->video_fd, VIDIOC_QUERYBUF, buffer) < 0) {
+        av_log(hwfc, AV_LOG_ERROR, "Failed to query buffer %d of type %d: %s (%d)\n",
+               buffer->index, buffer->type, strerror(errno), errno);
+        goto fail;
+    }
+
+    return ref;
+
+fail:
+    av_buffer_unref(&ref);
+    return NULL;
+}
+
+static AVBufferRef *v4l2request_capture_buffer_alloc(void *opaque, size_t size)
+{
+    AVHWFramesContext *hwfc = opaque;
+    AVV4L2RequestFramesContext *fctx = hwfc->hwctx;
+    AVV4L2RequestFramesContextInternal *fctxi = fctx->internal;
+
+    return v4l2request_v4l2_buffer_alloc(hwfc, &fctxi->capture.format);
+}
+
+static AVBufferRef *v4l2request_output_buffer_alloc(void *opaque, size_t size)
+{
+    AVHWFramesContext *hwfc = opaque;
+    AVV4L2RequestFramesContext *fctx = hwfc->hwctx;
+    AVV4L2RequestFramesContextInternal *fctxi = fctx->internal;
+
+    return v4l2request_v4l2_buffer_alloc(hwfc, &fctxi->output.format);
+}
+
+static void v4l2request_frame_free(void *opaque, uint8_t *data)
+{
+    V4L2RequestFrameDescriptor *desc = (V4L2RequestFrameDescriptor *)data;
+
+    // Close the exported CAPTURE buffer memory planes
+    for (int i = 0; i < FF_ARRAY_ELEMS(desc->fd); i++) {
+        if (desc->fd[i] >= 0) {
+            close(desc->fd[i]);
+            desc->fd[i] = -1;
+        }
+    }
+
+    // Return the CAPTURE buffer to the frames context CAPTURE pool
+    av_buffer_unref(&desc->ref);
+
+    av_free(data);
+}
+
+static AVBufferRef *v4l2request_frame_alloc(void *opaque, size_t size)
+{
+    AVHWFramesContext *hwfc = opaque;
+    AVV4L2RequestFramesContext *fctx = hwfc->hwctx;
+    AVV4L2RequestFramesContextInternal *fctxi = fctx->internal;
+    struct v4l2_format *format = &fctxi->capture.format;
+    V4L2RequestFrameDescriptor *desc;
+    struct v4l2_buffer *buffer;
+    AVBufferRef *ref;
+    uint8_t *data;
+
+    data = av_mallocz(size);
+    if (!data)
+        return NULL;
+
+    ref = av_buffer_create(data, size, v4l2request_frame_free,
+                           hwfc, AV_BUFFER_FLAG_READONLY);
+    if (!ref) {
+        av_free(data);
+        return NULL;
+    }
+
+    // Set initial default values
+    desc = (V4L2RequestFrameDescriptor *)data;
+    for (int i = 0; i < FF_ARRAY_ELEMS(desc->fd); i++)
+        desc->fd[i] = -1;
+
+    // Get a CAPTURE buffer from frames context CAPTURE pool
+    desc->ref = av_buffer_pool_get(fctxi->capture.pool);
+    if (!desc->ref)
+       goto fail;
+
+    buffer = (struct v4l2_buffer *)desc->ref->data;
+    desc->index = buffer->index;
+
+    // Export CAPTURE buffer memory planes
+    desc->base.nb_objects = V4L2_TYPE_IS_MULTIPLANAR(format->type) ?
+                            format->fmt.pix_mp.num_planes : 1;
+    av_assert0(desc->base.nb_objects <= AV_DRM_MAX_PLANES);
+    for (int i = 0; i < desc->base.nb_objects; i++) {
+        struct v4l2_exportbuffer exportbuffer = {
+            .type = buffer->type,
+            .index = buffer->index,
+            .plane = i,
+            .flags = O_RDONLY,
+        };
+        if (ioctl(fctxi->video_fd, VIDIOC_EXPBUF, &exportbuffer) < 0) {
+            av_log(hwfc, AV_LOG_ERROR, "Failed to export memory plane %d (%d): %s (%d)\n",
+                   i, buffer->index, strerror(errno), errno);
+            goto fail;
+        }
+        desc->base.objects[i].fd = desc->fd[i] = exportbuffer.fd;
+    }
+
+    // Set AVDRMFrameDescriptor based on CAPTURE buffer format
+    if (v4l2request_set_drm_descriptor(&desc->base, format) < 0)
+        goto fail;
+
+    return ref;
+
+fail:
+    av_buffer_unref(&ref);
+    return NULL;
+}
+
+static int v4l2request_frames_init(AVHWFramesContext *hwfc)
+{
+    V4L2RequestFramesContext *hwctx = hwfc->hwctx;
+    AVV4L2RequestFramesContextInternal *fctxi;
+    uint32_t pixelformat;
+    int ret;
+
+    // Set initial default values
+    fctxi = &hwctx->internal;
+    hwctx->p.internal = fctxi;
+    fctxi->media_fd = -1;
+    fctxi->video_fd = -1;
+
+    // Locate and open a capable video decoder device
+    ret = v4l2request_open_decoder(hwfc);
+    if (ret < 0)
+        return ret;
+
+    // Reset init controls after video device is opened
+    hwctx->p.init_controls = NULL;
+    hwctx->p.nb_init_controls = 0;
+
+    // Update frames context with CAPTURE format details
+    if (V4L2_TYPE_IS_MULTIPLANAR(fctxi->capture.format.type)) {
+        hwfc->width = fctxi->capture.format.fmt.pix_mp.width;
+        hwfc->height = fctxi->capture.format.fmt.pix_mp.height;
+        pixelformat = fctxi->capture.format.fmt.pix_mp.pixelformat;
+    } else {
+        hwfc->width = fctxi->capture.format.fmt.pix.width;
+        hwfc->height = fctxi->capture.format.fmt.pix.height;
+        pixelformat = fctxi->capture.format.fmt.pix.pixelformat;
+    }
+
+    hwfc->sw_format = AV_PIX_FMT_NONE;
+    for (int i = 0; i < FF_ARRAY_ELEMS(v4l2request_capture_pixelformats); i++) {
+        if (pixelformat == v4l2request_capture_pixelformats[i].pixelformat) {
+            hwctx->p.bit_depth = v4l2request_capture_pixelformats[i].bit_depth;
+            hwfc->sw_format = v4l2request_capture_pixelformats[i].sw_format;
+            break;
+        }
+    }
+
+    // Initialize buffer pool for CAPTURE buffers
+    fctxi->capture.pool = av_buffer_pool_init2(sizeof(struct v4l2_buffer), hwfc,
+                                               v4l2request_capture_buffer_alloc, NULL);
+    if (!fctxi->capture.pool)
+        return AVERROR(ENOMEM);
+
+    // Initialize buffer pool for OUTPUT buffers
+    fctxi->output.pool = av_buffer_pool_init2(sizeof(struct v4l2_buffer), hwfc,
+                                              v4l2request_output_buffer_alloc, NULL);
+    if (!fctxi->output.pool)
+        return AVERROR(ENOMEM);
+
+    // Initialize buffer pool for frame descriptors
+    ffhwframesctx(hwfc)->pool_internal =
+                av_buffer_pool_init2(sizeof(V4L2RequestFrameDescriptor), hwfc,
+                                     v4l2request_frame_alloc, NULL);
+    if (!ffhwframesctx(hwfc)->pool_internal)
+        return AVERROR(ENOMEM);
+
+    av_log(hwfc, AV_LOG_VERBOSE, "Using CAPTURE buffer format %s (%dx%d)\n",
+           av_fourcc2str(pixelformat), hwfc->width, hwfc->height);
+
+    return 0;
+}
+
+static void v4l2request_frames_uninit(AVHWFramesContext *hwfc)
+{
+    AVV4L2RequestFramesContext *fctx = hwfc->hwctx;
+    AVV4L2RequestFramesContextInternal *fctxi = fctx->internal;
+
+    av_buffer_pool_uninit(&fctxi->capture.pool);
+    av_buffer_pool_uninit(&fctxi->output.pool);
+
+    if (fctxi->video_fd >= 0) {
+        close(fctxi->video_fd);
+        fctxi->video_fd = -1;
+    }
+
+    if (fctxi->media_fd) {
+        close(fctxi->media_fd);
+        fctxi->media_fd = -1;
+    }
+}
+
+static int v4l2request_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
+{
+    V4L2RequestFrameDescriptor *desc;
+
+    frame->buf[0] = av_buffer_pool_get(hwfc->pool);
+    if (!frame->buf[0])
+        return AVERROR(ENOMEM);
+
+    desc = (V4L2RequestFrameDescriptor *)frame->buf[0]->data;
+    frame->data[0] = (uint8_t *)&desc->base;
+    frame->data[1] = (uint8_t *)(uintptr_t)desc->index;
+
+    frame->format = AV_PIX_FMT_DRM_PRIME;
+    frame->width  = hwfc->width;
+    frame->height = hwfc->height;
+
+    return 0;
+}
+
+typedef struct V4L2RequestMapping {
+    // Address and length of each mmap()ed region.
+    int nb_regions;
+    int object[AV_DRM_MAX_PLANES];
+    void *address[AV_DRM_MAX_PLANES];
+    size_t length[AV_DRM_MAX_PLANES];
+} V4L2RequestMapping;
+
+static void v4l2request_unmap_frame(AVHWFramesContext *hwfc,
+                                    HWMapDescriptor *hwmap)
+{
+    V4L2RequestMapping *map = hwmap->priv;
+
+    for (int i = 0; i < map->nb_regions; i++) {
+        struct dma_buf_sync sync = {
+            .flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ,
+        };
+        ioctl(map->object[i], DMA_BUF_IOCTL_SYNC, &sync);
+        munmap(map->address[i], map->length[i]);
+    }
+
+    av_free(map);
+}
+
+static int v4l2request_map_frame(AVHWFramesContext *hwfc,
+                                 AVFrame *dst, const AVFrame *src)
+{
+    const AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor *)src->data[0];
+    struct dma_buf_sync sync = {
+        .flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ,
+    };
+    V4L2RequestMapping *map;
+    int ret, i, p, plane;
+    void *addr;
+
+    map = av_mallocz(sizeof(*map));
+    if (!map)
+        return AVERROR(ENOMEM);
+
+    av_assert0(desc->nb_objects <= AV_DRM_MAX_PLANES);
+    for (i = 0; i < desc->nb_objects; i++) {
+        addr = mmap(NULL, desc->objects[i].size, PROT_READ, MAP_SHARED,
+                    desc->objects[i].fd, 0);
+        if (addr == MAP_FAILED) {
+            ret = AVERROR(errno);
+            av_log(hwfc, AV_LOG_ERROR, "Failed to map DRM object %d to memory: %s (%d)\n",
+                   desc->objects[i].fd, strerror(errno), errno);
+            goto fail;
+        }
+
+        map->address[i] = addr;
+        map->length[i]  = desc->objects[i].size;
+        map->object[i]  = desc->objects[i].fd;
+
+        /*
+         * We're not checking for errors here because the kernel may not
+         * support the ioctl, in which case its okay to carry on
+         */
+        ioctl(desc->objects[i].fd, DMA_BUF_IOCTL_SYNC, &sync);
+    }
+    map->nb_regions = i;
+
+    plane = 0;
+    for (i = 0; i < desc->nb_layers; i++) {
+        const AVDRMLayerDescriptor *layer = &desc->layers[i];
+        for (p = 0; p < layer->nb_planes; p++) {
+            dst->data[plane] =
+                (uint8_t *)map->address[layer->planes[p].object_index] +
+                                        layer->planes[p].offset;
+            dst->linesize[plane] =      layer->planes[p].pitch;
+            ++plane;
+        }
+    }
+    av_assert0(plane <= AV_DRM_MAX_PLANES);
+
+    dst->width  = src->width;
+    dst->height = src->height;
+
+    ret = ff_hwframe_map_create(src->hw_frames_ctx, dst, src,
+                                v4l2request_unmap_frame, map);
+    if (ret < 0)
+        goto fail;
+
+    return 0;
+
+fail:
+    for (i = 0; i < desc->nb_objects; i++) {
+        if (map->address[i])
+            munmap(map->address[i], map->length[i]);
+    }
+    av_free(map);
+    return ret;
+}
+
+static int v4l2request_transfer_get_formats(AVHWFramesContext *hwfc,
+                                            enum AVHWFrameTransferDirection dir,
+                                            enum AVPixelFormat **formats)
+{
+    enum AVPixelFormat *fmts;
+
+    if (dir == AV_HWFRAME_TRANSFER_DIRECTION_TO)
+        return AVERROR(ENOSYS);
+
+    fmts = av_malloc_array(2, sizeof(*fmts));
+    if (!fmts)
+        return AVERROR(ENOMEM);
+
+    fmts[0] = hwfc->sw_format;
+    fmts[1] = AV_PIX_FMT_NONE;
+
+    if (hwfc->sw_format == AV_PIX_FMT_YUV420P ||
+        hwfc->sw_format == AV_PIX_FMT_YUV420P10 ||
+        hwfc->sw_format == AV_PIX_FMT_YUV422P10)
+        fmts[0] = AV_PIX_FMT_NONE;
+
+    *formats = fmts;
+    return 0;
+}
+
+static int v4l2request_transfer_data_from(AVHWFramesContext *hwfc,
+                                          AVFrame *dst, const AVFrame *src)
+{
+    AVFrame *map;
+    int ret;
+
+    if (dst->width > hwfc->width || dst->height > hwfc->height)
+        return AVERROR(EINVAL);
+
+    map = av_frame_alloc();
+    if (!map)
+        return AVERROR(ENOMEM);
+    map->format = dst->format;
+
+    ret = v4l2request_map_frame(hwfc, map, src);
+    if (ret)
+        goto fail;
+
+    map->width  = dst->width;
+    map->height = dst->height;
+
+    ret = av_frame_copy(dst, map);
+    if (ret)
+        goto fail;
+
+    ret = 0;
+fail:
+    av_frame_free(&map);
+    return ret;
+}
+
+static int v4l2request_map_from(AVHWFramesContext *hwfc, AVFrame *dst,
+                                const AVFrame *src, int flags)
+{
+    int ret;
+
+    if (!(flags & AV_HWFRAME_MAP_READ))
+        return AVERROR(ENOSYS);
+
+    if (hwfc->sw_format == AV_PIX_FMT_NONE ||
+        hwfc->sw_format == AV_PIX_FMT_YUV420P ||
+        hwfc->sw_format == AV_PIX_FMT_YUV420P10 ||
+        hwfc->sw_format == AV_PIX_FMT_YUV422P10)
+        return AVERROR(ENOSYS);
+    else if (dst->format == AV_PIX_FMT_NONE)
+        dst->format = hwfc->sw_format;
+    else if (hwfc->sw_format != dst->format)
+        return AVERROR(ENOSYS);
+
+    ret = v4l2request_map_frame(hwfc, dst, src);
+    if (ret)
+        return ret;
+
+    return av_frame_copy_props(dst, src);
+}
+
+const HWContextType ff_hwcontext_type_v4l2request = {
+    .type                   = AV_HWDEVICE_TYPE_V4L2REQUEST,
+    .name                   = "V4L2 Request API",
+
+    .device_hwctx_size      = sizeof(V4L2RequestDeviceContext),
+    .device_create          = v4l2request_device_create,
+    .device_uninit          = v4l2request_device_uninit,
+
+    .frames_hwctx_size      = sizeof(V4L2RequestFramesContext),
+    .frames_init            = v4l2request_frames_init,
+    .frames_uninit          = v4l2request_frames_uninit,
+    .frames_get_buffer      = v4l2request_get_buffer,
+    .transfer_get_formats   = v4l2request_transfer_get_formats,
+    .transfer_data_from     = v4l2request_transfer_data_from,
+    .map_from               = v4l2request_map_from,
+
+    .pix_fmts = (const enum AVPixelFormat[]) {
+        AV_PIX_FMT_DRM_PRIME,
+        AV_PIX_FMT_NONE
+    },
+};
--- /dev/null
+++ b/libavutil/hwcontext_v4l2request.h
@@ -0,0 +1,70 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVUTIL_HWCONTEXT_V4L2REQUEST_H
+#define AVUTIL_HWCONTEXT_V4L2REQUEST_H
+
+#include <stdint.h>
+#include <linux/videodev2.h>
+
+/**
+ * @file
+ * An API-specific header for AV_HWDEVICE_TYPE_V4L2REQUEST.
+ */
+
+typedef struct AVV4L2RequestFramesContextInternal AVV4L2RequestFramesContextInternal;
+
+/**
+ * V4L2 Request API frames context.
+ *
+ * This struct is allocated as AVHWFramesContext.hwctx
+ */
+typedef struct AVV4L2RequestFramesContext {
+    /**
+     * Internal context for the initialized V4L2 stateless decoder/encoder session.
+     */
+    AVV4L2RequestFramesContextInternal *internal;
+
+    /**
+     * V4L2_PIX_FMT_* coded pixel format to set on the OUTPUT queue (decoders)
+     * or the CAPTURE queue (encoders) during initialization.
+     *
+     * This field must be set by caller before av_hwframe_ctx_init() is called.
+     */
+    uint32_t pixelformat;
+
+    /**
+     * Optional bit depth of the frame pixel format, e.g. 8 or 10.
+     *
+     * This field should be set by caller before av_hwframe_ctx_init() is called,
+     * the field will be updated to match the selected frame pixel format after
+     * successful initialization.
+     */
+    uint32_t bit_depth;
+
+    /**
+     * Optional codec-specific extended controls to be set during initialization.
+     *
+     * These fields should be set by caller before av_hwframe_ctx_init() is called,
+     * fields are reset to NULL and 0 after successful initialization.
+     */
+    struct v4l2_ext_control *init_controls;
+    int nb_init_controls;
+} AVV4L2RequestFramesContext;
+
+#endif /* AVUTIL_HWCONTEXT_V4L2REQUEST_H */
--- /dev/null
+++ b/libavutil/hwcontext_v4l2request_internal.h
@@ -0,0 +1,67 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVUTIL_HWCONTEXT_V4L2REQUEST_INTERNAL_H
+#define AVUTIL_HWCONTEXT_V4L2REQUEST_INTERNAL_H
+
+#include "buffer.h"
+#include "hwcontext_v4l2request.h"
+
+/**
+ * @file
+ * FFmpeg internal API-specific header for AV_HWDEVICE_TYPE_V4L2REQUEST.
+ */
+
+/**
+ * Internal context for the initialized V4L2 stateless decoder/encoder session.
+ */
+struct AVV4L2RequestFramesContextInternal {
+    /**
+     * Media device file descriptor of the initialized session.
+     */
+    int media_fd;
+
+    /**
+     * Video device file descriptor of the initialized session.
+     */
+    int video_fd;
+
+    /**
+     * Details of the initialized CAPTURE and OUTPUT queues.
+     */
+    struct {
+        /**
+         * V4L2 buffer format.
+         */
+        struct v4l2_format format;
+
+        /**
+         * V4L2 buffer capabilities flags.
+         */
+        uint32_t capabilities;
+
+        /**
+        * Buffer pool of allocated V4L2 buffers.
+        *
+        * AVBufferRef.data points to a struct v4l2_buffer for the created buffer.
+        */
+        AVBufferPool *pool;
+    } capture, output;
+};
+
+#endif /* AVUTIL_HWCONTEXT_V4L2REQUEST_INTERNAL_H */