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
|
From 51a441640119ef3359a38dae67290a96ed77e6a0 Mon Sep 17 00:00:00 2001
From: Jonas Karlman <jonas@kwiboo.se>
Date: Tue, 6 Aug 2024 09:06:04 +0000
Subject: [PATCH 05/13] avcodec: Add V4L2 Request API mpeg2 hwaccel
Add a V4L2 Request API hwaccel for MPEG2.
Support for MPEG2 is enabled when Linux kernel headers declare the
control id V4L2_CID_STATELESS_MPEG2_SEQUENCE, added in v5.14.
This also change v4l2_request hwaccel to use autodetect in configure.
Signed-off-by: Jonas Karlman <jonas@kwiboo.se>
---
configure | 7 +-
libavcodec/Makefile | 1 +
libavcodec/hwaccels.h | 1 +
libavcodec/mpeg12dec.c | 6 ++
libavcodec/v4l2_request_mpeg2.c | 184 ++++++++++++++++++++++++++++++++
5 files changed, 197 insertions(+), 2 deletions(-)
create mode 100644 libavcodec/v4l2_request_mpeg2.c
--- a/configure
+++ b/configure
@@ -365,7 +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-v4l2-request disable V4L2 Request API code [autodetect]
--disable-vaapi disable Video Acceleration API (mainly Unix/Intel) code [autodetect]
--disable-vdpau disable Nvidia Video Decode and Presentation API for Unix code [autodetect]
--disable-videotoolbox disable VideoToolbox code [autodetect]
@@ -2183,6 +2183,7 @@ HWACCEL_AUTODETECT_LIBRARY_LIST="
videotoolbox
vulkan
v4l2_m2m
+ v4l2_request
"
# catchall list of things that require external libs to link
@@ -2203,7 +2204,6 @@ HWACCEL_LIBRARY_LIST="
libvpl
mmal
opencl
- v4l2_request
"
DOCUMENT_LIST="
@@ -3495,6 +3495,8 @@ mpeg2_dxva2_hwaccel_deps="dxva2"
mpeg2_dxva2_hwaccel_select="mpeg2video_decoder"
mpeg2_nvdec_hwaccel_deps="nvdec"
mpeg2_nvdec_hwaccel_select="mpeg2video_decoder"
+mpeg2_v4l2request_hwaccel_deps="v4l2_request mpeg2_v4l2_request"
+mpeg2_v4l2request_hwaccel_select="mpeg2video_decoder"
mpeg2_vaapi_hwaccel_deps="vaapi"
mpeg2_vaapi_hwaccel_select="mpeg2video_decoder"
mpeg2_vdpau_hwaccel_deps="vdpau"
@@ -7662,6 +7664,7 @@ if enabled v4l2_m2m; then
fi
if enabled v4l2_request; then
+ check_cc mpeg2_v4l2_request linux/videodev2.h "int i = V4L2_CID_STATELESS_MPEG2_SEQUENCE"
check_cc v4l2_m2m_hold_capture_buf linux/videodev2.h "int i = V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF"
check_func_headers "linux/media.h linux/videodev2.h" v4l2_timeval_to_ns
check_pkg_config libudev libudev libudev.h udev_new
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1084,6 +1084,7 @@ OBJS-$(CONFIG_MPEG2_DXVA2_HWACCEL)
OBJS-$(CONFIG_MPEG2_D3D12VA_HWACCEL) += dxva2_mpeg2.o d3d12va_mpeg2.o
OBJS-$(CONFIG_MPEG2_NVDEC_HWACCEL) += nvdec_mpeg12.o
OBJS-$(CONFIG_MPEG2_QSV_HWACCEL) += qsvdec.o
+OBJS-$(CONFIG_MPEG2_V4L2REQUEST_HWACCEL) += v4l2_request_mpeg2.o
OBJS-$(CONFIG_MPEG2_VAAPI_HWACCEL) += vaapi_mpeg2.o
OBJS-$(CONFIG_MPEG2_VDPAU_HWACCEL) += vdpau_mpeg12.o
OBJS-$(CONFIG_MPEG2_VIDEOTOOLBOX_HWACCEL) += videotoolbox.o
--- a/libavcodec/hwaccels.h
+++ b/libavcodec/hwaccels.h
@@ -61,6 +61,7 @@ extern const struct FFHWAccel ff_mpeg2_d
extern const struct FFHWAccel ff_mpeg2_d3d12va_hwaccel;
extern const struct FFHWAccel ff_mpeg2_dxva2_hwaccel;
extern const struct FFHWAccel ff_mpeg2_nvdec_hwaccel;
+extern const struct FFHWAccel ff_mpeg2_v4l2request_hwaccel;
extern const struct FFHWAccel ff_mpeg2_vaapi_hwaccel;
extern const struct FFHWAccel ff_mpeg2_vdpau_hwaccel;
extern const struct FFHWAccel ff_mpeg2_videotoolbox_hwaccel;
--- a/libavcodec/mpeg12dec.c
+++ b/libavcodec/mpeg12dec.c
@@ -821,6 +821,9 @@ static const enum AVPixelFormat mpeg2_hw
#if CONFIG_MPEG2_VIDEOTOOLBOX_HWACCEL
AV_PIX_FMT_VIDEOTOOLBOX,
#endif
+#if CONFIG_MPEG2_V4L2REQUEST_HWACCEL
+ AV_PIX_FMT_DRM_PRIME,
+#endif
AV_PIX_FMT_YUV420P,
AV_PIX_FMT_NONE
};
@@ -2728,6 +2731,9 @@ const FFCodec ff_mpeg2video_decoder = {
#if CONFIG_MPEG2_VIDEOTOOLBOX_HWACCEL
HWACCEL_VIDEOTOOLBOX(mpeg2),
#endif
+#if CONFIG_MPEG2_V4L2REQUEST_HWACCEL
+ HWACCEL_V4L2REQUEST(mpeg2),
+#endif
NULL
},
};
--- /dev/null
+++ b/libavcodec/v4l2_request_mpeg2.c
@@ -0,0 +1,184 @@
+/*
+ * 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 "hwaccel_internal.h"
+#include "hwconfig.h"
+#include "mathops.h"
+#include "mpegvideo.h"
+#include "v4l2_request.h"
+
+typedef struct V4L2RequestControlsMPEG2 {
+ V4L2RequestPictureContext pic;
+ struct v4l2_ctrl_mpeg2_sequence sequence;
+ struct v4l2_ctrl_mpeg2_picture picture;
+ struct v4l2_ctrl_mpeg2_quantisation quantisation;
+} V4L2RequestControlsMPEG2;
+
+static int v4l2_request_mpeg2_start_frame(AVCodecContext *avctx,
+ av_unused const AVBufferRef *buf_ref,
+ av_unused const uint8_t *buffer,
+ av_unused uint32_t size)
+{
+ const MpegEncContext *s = avctx->priv_data;
+ V4L2RequestControlsMPEG2 *controls = s->cur_pic.ptr->hwaccel_picture_private;
+ int ret;
+
+ ret = ff_v4l2_request_start_frame(avctx, &controls->pic, s->cur_pic.ptr->f);
+ if (ret)
+ return ret;
+
+ controls->sequence = (struct v4l2_ctrl_mpeg2_sequence) {
+ /* ISO/IEC 13818-2, ITU-T Rec. H.262: Sequence header */
+ .horizontal_size = s->width,
+ .vertical_size = s->height,
+ .vbv_buffer_size = controls->pic.output->size,
+
+ /* ISO/IEC 13818-2, ITU-T Rec. H.262: Sequence extension */
+ .profile_and_level_indication = 0,
+ .chroma_format = s->chroma_format,
+ };
+
+ if (s->progressive_sequence)
+ controls->sequence.flags |= V4L2_MPEG2_SEQ_FLAG_PROGRESSIVE;
+
+ controls->picture = (struct v4l2_ctrl_mpeg2_picture) {
+ /* ISO/IEC 13818-2, ITU-T Rec. H.262: Picture header */
+ .picture_coding_type = s->pict_type,
+
+ /* ISO/IEC 13818-2, ITU-T Rec. H.262: Picture coding extension */
+ .f_code[0][0] = s->mpeg_f_code[0][0],
+ .f_code[0][1] = s->mpeg_f_code[0][1],
+ .f_code[1][0] = s->mpeg_f_code[1][0],
+ .f_code[1][1] = s->mpeg_f_code[1][1],
+ .picture_structure = s->picture_structure,
+ .intra_dc_precision = s->intra_dc_precision,
+ };
+
+ if (s->top_field_first)
+ controls->picture.flags |= V4L2_MPEG2_PIC_FLAG_TOP_FIELD_FIRST;
+
+ if (s->frame_pred_frame_dct)
+ controls->picture.flags |= V4L2_MPEG2_PIC_FLAG_FRAME_PRED_DCT;
+
+ if (s->concealment_motion_vectors)
+ controls->picture.flags |= V4L2_MPEG2_PIC_FLAG_CONCEALMENT_MV;
+
+ if (s->intra_vlc_format)
+ controls->picture.flags |= V4L2_MPEG2_PIC_FLAG_INTRA_VLC;
+
+ if (s->q_scale_type)
+ controls->picture.flags |= V4L2_MPEG2_PIC_FLAG_Q_SCALE_TYPE;
+
+ if (s->alternate_scan)
+ controls->picture.flags |= V4L2_MPEG2_PIC_FLAG_ALT_SCAN;
+
+ if (s->repeat_first_field)
+ controls->picture.flags |= V4L2_MPEG2_PIC_FLAG_REPEAT_FIRST;
+
+ if (s->progressive_frame)
+ controls->picture.flags |= V4L2_MPEG2_PIC_FLAG_PROGRESSIVE;
+
+ switch (s->pict_type) {
+ case AV_PICTURE_TYPE_B:
+ if (s->next_pic.ptr)
+ controls->picture.backward_ref_ts =
+ ff_v4l2_request_get_capture_timestamp(s->next_pic.ptr->f);
+ // fall-through
+ case AV_PICTURE_TYPE_P:
+ if (s->last_pic.ptr)
+ controls->picture.forward_ref_ts =
+ ff_v4l2_request_get_capture_timestamp(s->last_pic.ptr->f);
+ }
+
+ for (int i = 0; i < 64; i++) {
+ int n = s->idsp.idct_permutation[ff_zigzag_direct[i]];
+ controls->quantisation.intra_quantiser_matrix[i] = s->intra_matrix[n];
+ controls->quantisation.non_intra_quantiser_matrix[i] = s->inter_matrix[n];
+ controls->quantisation.chroma_intra_quantiser_matrix[i] = s->chroma_intra_matrix[n];
+ controls->quantisation.chroma_non_intra_quantiser_matrix[i] = s->chroma_inter_matrix[n];
+ }
+
+ return 0;
+}
+
+static int v4l2_request_mpeg2_decode_slice(AVCodecContext *avctx,
+ const uint8_t *buffer, uint32_t size)
+{
+ const MpegEncContext *s = avctx->priv_data;
+ V4L2RequestControlsMPEG2 *controls = s->cur_pic.ptr->hwaccel_picture_private;
+
+ return ff_v4l2_request_append_output(avctx, &controls->pic, buffer, size);
+}
+
+static int v4l2_request_mpeg2_end_frame(AVCodecContext *avctx)
+{
+ const MpegEncContext *s = avctx->priv_data;
+ V4L2RequestControlsMPEG2 *controls = s->cur_pic.ptr->hwaccel_picture_private;
+
+ struct v4l2_ext_control control[] = {
+ {
+ .id = V4L2_CID_STATELESS_MPEG2_SEQUENCE,
+ .ptr = &controls->sequence,
+ .size = sizeof(controls->sequence),
+ },
+ {
+ .id = V4L2_CID_STATELESS_MPEG2_PICTURE,
+ .ptr = &controls->picture,
+ .size = sizeof(controls->picture),
+ },
+ {
+ .id = V4L2_CID_STATELESS_MPEG2_QUANTISATION,
+ .ptr = &controls->quantisation,
+ .size = sizeof(controls->quantisation),
+ },
+ };
+
+ return ff_v4l2_request_decode_frame(avctx, &controls->pic,
+ control, FF_ARRAY_ELEMS(control));
+}
+
+static int v4l2_request_mpeg2_init(AVCodecContext *avctx)
+{
+ return ff_v4l2_request_init(avctx, NULL, 0, NULL);
+}
+
+static int v4l2_request_mpeg2_frame_params(AVCodecContext *avctx,
+ AVBufferRef *hw_frames_ctx)
+{
+ return ff_v4l2_request_frame_params(avctx, hw_frames_ctx,
+ V4L2_PIX_FMT_MPEG2_SLICE, 8);
+}
+
+const FFHWAccel ff_mpeg2_v4l2request_hwaccel = {
+ .p.name = "mpeg2_v4l2request",
+ .p.type = AVMEDIA_TYPE_VIDEO,
+ .p.id = AV_CODEC_ID_MPEG2VIDEO,
+ .p.pix_fmt = AV_PIX_FMT_DRM_PRIME,
+ .start_frame = v4l2_request_mpeg2_start_frame,
+ .decode_slice = v4l2_request_mpeg2_decode_slice,
+ .end_frame = v4l2_request_mpeg2_end_frame,
+ .flush = ff_v4l2_request_flush,
+ .frame_priv_data_size = sizeof(V4L2RequestControlsMPEG2),
+ .init = v4l2_request_mpeg2_init,
+ .uninit = ff_v4l2_request_uninit,
+ .priv_data_size = sizeof(V4L2RequestContext),
+ .frame_params = v4l2_request_mpeg2_frame_params,
+ .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
+};
|