History

avcodec: add D3D12VA hardware accelerated VC1 decoding

The command below is how to enable d3d12va: ffmpeg -hwaccel d3d12va -i input.mp4 output.mp4

Wu Jianhua committed 2 years ago

Commit ffa158e1 parent c6c05dd7 files · +241 −7Browse files

Showing the changes to libavcodec (6 of 7 files). Show all

Unified · Split

6 files
  1. libavcodec/Makefile +1 −0
  2. libavcodec/d3d12va_vc1.c +214 −0
  3. libavcodec/dxva2_internal.h +4 −0
  4. libavcodec/dxva2_vc1.c +8 −7
  5. libavcodec/hwaccels.h +2 −0
  6. libavcodec/vc1dec.c +9 −0
libavcodec/Makefile+1−0
@@ -1039,6 +1039,7 @@ OBJS-$(CONFIG_MPEG4_VDPAU_HWACCEL) += vdpau_mpeg4.o · 1038 unmodified lines
10391039 OBJS-$(CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL) += videotoolbox.o
10401040 OBJS-$(CONFIG_VC1_D3D11VA_HWACCEL) += dxva2_vc1.o
10411041 OBJS-$(CONFIG_VC1_DXVA2_HWACCEL) += dxva2_vc1.o
1042+OBJS-$(CONFIG_VC1_D3D12VA_HWACCEL) += dxva2_vc1.o d3d12va_vc1.o
10421043 OBJS-$(CONFIG_VC1_NVDEC_HWACCEL) += nvdec_vc1.o
10431044 OBJS-$(CONFIG_VC1_QSV_HWACCEL) += qsvdec.o
10441045 OBJS-$(CONFIG_VC1_VAAPI_HWACCEL) += vaapi_vc1.o
libavcodec/d3d12va_vc1.c+214−0
@@ -0,0 +1,214 @@
1+/*
2+ * Direct3D12 WMV3/VC-1 HW acceleration
3+ *
4+ * copyright (c) 2022-2023 Wu Jianhua <toqsxw@outlook.com>
5+ *
6+ * This file is part of FFmpeg.
7+ *
8+ * FFmpeg is free software; you can redistribute it and/or
9+ * modify it under the terms of the GNU Lesser General Public
10+ * License as published by the Free Software Foundation; either
11+ * version 2.1 of the License, or (at your option) any later version.
12+ *
13+ * FFmpeg is distributed in the hope that it will be useful,
14+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+ * Lesser General Public License for more details.
17+ *
18+ * You should have received a copy of the GNU Lesser General Public
19+ * License along with FFmpeg; if not, write to the Free Software
20+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+ */
22+
23+#include "config_components.h"
24+#include "libavutil/avassert.h"
25+#include "libavutil/hwcontext_d3d12va_internal.h"
26+#include "mpegutils.h"
27+#include "mpegvideodec.h"
28+#include "vc1.h"
29+#include "vc1data.h"
30+#include "d3d12va_decode.h"
31+#include "dxva2_internal.h"
32+
33+#define MAX_SLICES 1024
34+#define INVALID_REF 0xffff
35+
36+#define REF_RESOURCE(index) if (index != INVALID_REF) { \
37+ ctx->ref_resources[index] = frames_hwctx->texture_infos[index].texture; \
38+}
39+
40+typedef struct D3D12DecodePictureContext {
41+ DXVA_PictureParameters pp;
42+ unsigned slice_count;
43+ DXVA_SliceInfo slices[MAX_SLICES];
44+ const uint8_t *bitstream;
45+ unsigned bitstream_size;
46+} D3D12DecodePictureContext;
47+
48+static int d3d12va_vc1_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
49+{
50+ const VC1Context *v = avctx->priv_data;
51+ D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
52+ D3D12DecodePictureContext *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
53+
54+ if (!ctx)
55+ return -1;
56+
57+ av_assert0(ctx_pic);
58+
59+ ctx->used_mask = 0;
60+
61+ ff_dxva2_vc1_fill_picture_parameters(avctx, (AVDXVAContext *)ctx, &ctx_pic->pp);
62+ ctx_pic->pp.wDeblockedPictureIndex = INVALID_REF;
63+
64+ ctx_pic->bitstream = NULL;
65+ ctx_pic->bitstream_size = 0;
66+ ctx_pic->slice_count = 0;
67+
68+ return 0;
69+}
70+
71+static int d3d12va_vc1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
72+{
73+ const VC1Context *v = avctx->priv_data;
74+ D3D12DecodePictureContext *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
75+
76+ if (ctx_pic->slice_count >= MAX_SLICES) {
77+ return AVERROR(ERANGE);
78+ }
79+
80+ if (avctx->codec_id == AV_CODEC_ID_VC1 &&
81+ size >= 4 && IS_MARKER(AV_RB32(buffer))) {
82+ buffer += 4;
83+ size -= 4;
84+ }
85+
86+ if (!ctx_pic->bitstream)
87+ ctx_pic->bitstream = buffer;
88+ ctx_pic->bitstream_size += size;
89+
90+ ff_dxva2_vc1_fill_slice(avctx, &ctx_pic->slices[ctx_pic->slice_count++],
91+ buffer - ctx_pic->bitstream, size);
92+
93+ return 0;
94+}
95+
96+static int update_input_arguments(AVCodecContext *avctx, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *input_args, ID3D12Resource *buffer)
97+{
98+ D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
99+ AVHWFramesContext *frames_ctx = D3D12VA_FRAMES_CONTEXT(avctx);
100+ AVD3D12VAFramesContext *frames_hwctx = frames_ctx->hwctx;
101+ const VC1Context *v = avctx->priv_data;
102+ const MpegEncContext *s = &v->s;
103+ D3D12DecodePictureContext *ctx_pic = s->current_picture_ptr->hwaccel_picture_private;
104+ D3D12_VIDEO_DECODE_FRAME_ARGUMENT *args = &input_args->FrameArguments[input_args->NumFrameArguments++];
105+
106+ const unsigned mb_count = s->mb_width * (s->mb_height >> v->field_mode);
107+ uint8_t *mapped_data, *mapped_ptr;
108+
109+ static const uint8_t start_code[] = { 0, 0, 1, 0x0d };
110+
111+ if (FAILED(ID3D12Resource_Map(buffer, 0, NULL, &mapped_data))) {
112+ av_log(avctx, AV_LOG_ERROR, "Failed to map D3D12 Buffer resource!\n");
113+ return AVERROR(EINVAL);
114+ }
115+
116+ mapped_ptr = mapped_data;
117+ for (int i = 0; i < ctx_pic->slice_count; i++) {
118+ DXVA_SliceInfo *slice = &ctx_pic->slices[i];
119+ unsigned position = slice->dwSliceDataLocation;
120+ unsigned size = slice->dwSliceBitsInBuffer / 8;
121+
122+ slice->dwSliceDataLocation = mapped_ptr - mapped_data;
123+ if (i < ctx_pic->slice_count - 1)
124+ slice->wNumberMBsInSlice = slice[1].wNumberMBsInSlice - slice[0].wNumberMBsInSlice;
125+ else
126+ slice->wNumberMBsInSlice = mb_count - slice[0].wNumberMBsInSlice;
127+
128+ if (avctx->codec_id == AV_CODEC_ID_VC1) {
129+ memcpy(mapped_ptr, start_code, sizeof(start_code));
130+ if (i == 0 && v->second_field)
131+ mapped_ptr[3] = 0x0c;
132+ else if (i > 0)
133+ mapped_ptr[3] = 0x0b;
134+
135+ mapped_ptr += sizeof(start_code);
136+ slice->dwSliceBitsInBuffer += sizeof(start_code) * 8;
137+ }
138+
139+ memcpy(mapped_ptr, &ctx_pic->bitstream[position], size);
140+ mapped_ptr += size;
141+ }
142+
143+ ID3D12Resource_Unmap(buffer, 0, NULL);
144+
145+ args->Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_SLICE_CONTROL;
146+ args->Size = sizeof(DXVA_SliceInfo) * ctx_pic->slice_count;
147+ args->pData = ctx_pic->slices;
148+
149+ input_args->CompressedBitstream = (D3D12_VIDEO_DECODE_COMPRESSED_BITSTREAM){
150+ .pBuffer = buffer,
151+ .Offset = 0,
152+ .Size = mapped_ptr - mapped_data,
153+ };
154+
155+ return 0;
156+}
157+
158+static int d3d12va_vc1_end_frame(AVCodecContext *avctx)
159+{
160+ const VC1Context *v = avctx->priv_data;
161+ D3D12DecodePictureContext *ctx_pic = v->s.current_picture_ptr->hwaccel_picture_private;
162+
163+ if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
164+ return -1;
165+
166+ return ff_d3d12va_common_end_frame(avctx, v->s.current_picture_ptr->f,
167+ &ctx_pic->pp, sizeof(ctx_pic->pp),
168+ NULL, 0,
169+ update_input_arguments);
170+}
171+
172+static int d3d12va_vc1_decode_init(AVCodecContext *avctx)
173+{
174+ D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
175+ ctx->cfg.DecodeProfile = D3D12_VIDEO_DECODE_PROFILE_VC1;
176+
177+ ctx->max_num_ref = 3;
178+
179+ return ff_d3d12va_decode_init(avctx);
180+}
181+
182+#if CONFIG_WMV3_D3D12VA_HWACCEL
183+const FFHWAccel ff_wmv3_d3d12va_hwaccel = {
184+ .p.name = "wmv3_d3d12va",
185+ .p.type = AVMEDIA_TYPE_VIDEO,
186+ .p.id = AV_CODEC_ID_WMV3,
187+ .p.pix_fmt = AV_PIX_FMT_D3D12,
188+ .init = d3d12va_vc1_decode_init,
189+ .uninit = ff_d3d12va_decode_uninit,
190+ .start_frame = d3d12va_vc1_start_frame,
191+ .decode_slice = d3d12va_vc1_decode_slice,
192+ .end_frame = d3d12va_vc1_end_frame,
193+ .frame_params = ff_d3d12va_common_frame_params,
194+ .frame_priv_data_size = sizeof(D3D12DecodePictureContext),
195+ .priv_data_size = sizeof(D3D12VADecodeContext),
196+};
197+#endif
198+
199+#if CONFIG_VC1_D3D12VA_HWACCEL
200+const FFHWAccel ff_vc1_d3d12va_hwaccel = {
201+ .p.name = "vc1_d3d12va",
202+ .p.type = AVMEDIA_TYPE_VIDEO,
203+ .p.id = AV_CODEC_ID_VC1,
204+ .p.pix_fmt = AV_PIX_FMT_D3D12,
205+ .init = d3d12va_vc1_decode_init,
206+ .uninit = ff_d3d12va_decode_uninit,
207+ .start_frame = d3d12va_vc1_start_frame,
208+ .decode_slice = d3d12va_vc1_decode_slice,
209+ .end_frame = d3d12va_vc1_end_frame,
210+ .frame_params = ff_d3d12va_common_frame_params,
211+ .frame_priv_data_size = sizeof(D3D12DecodePictureContext),
212+ .priv_data_size = sizeof(D3D12VADecodeContext),
213+};
214+#endif
libavcodec/dxva2_internal.h+4−0
@@ -187,4 +187,8 @@ void ff_dxva2_mpeg2_fill_quantization_matrices(AVCodecContext *avctx, AVDXVACont · 186 unmodified lines
187187
188188 void ff_dxva2_mpeg2_fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice, unsigned position, const uint8_t *buffer, unsigned size);
189189
190+void ff_dxva2_vc1_fill_picture_parameters(AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PictureParameters *pp);
191+
192+void ff_dxva2_vc1_fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice, unsigned position, unsigned size);
193+
190194 #endif /* AVCODEC_DXVA2_INTERNAL_H */
libavcodec/dxva2_vc1.c+8−7
@@ -40,10 +40,11 @@ struct dxva2_picture_context { · 39 unmodified lines
4040 unsigned bitstream_size;
4141 };
4242
43−static void fill_picture_parameters(AVCodecContext *avctx,
44− AVDXVAContext *ctx, const VC1Context *v,
43+void ff_dxva2_vc1_fill_picture_parameters(AVCodecContext *avctx,
44+ AVDXVAContext *ctx,
4545 DXVA_PictureParameters *pp)
4646 {
47+ const VC1Context *v = avctx->priv_data;
4748 const MpegEncContext *s = &v->s;
4849 const Picture *current_picture = s->current_picture_ptr;
4950 int intcomp = 0;
@@ -57,8 +58,6 @@ static void fill_picture_parameters(AVCodecContext *avctx, · 7 unmodified lines
5758 }
5859
5960 memset(pp, 0, sizeof(*pp));
60− pp->wDecodedPictureIndex =
61− pp->wDeblockedPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, current_picture->f, 1);
6261 if (s->pict_type != AV_PICTURE_TYPE_I && !v->bi_type)
6362 pp->wForwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->last_picture.f, 0);
6463 else
@@ -67,6 +66,8 @@ static void fill_picture_parameters(AVCodecContext *avctx, · 2 unmodified lines
6766 pp->wBackwardRefPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, s->next_picture.f, 0);
6867 else
6968 pp->wBackwardRefPictureIndex = 0xffff;
69+ pp->wDecodedPictureIndex =
70+ pp->wDeblockedPictureIndex = ff_dxva2_get_surface_index(avctx, ctx, current_picture->f, 1);
7071 if (v->profile == PROFILE_ADVANCED) {
7172 /* It is the cropped width/height -1 of the frame */
7273 pp->wPicWidthInMBminus1 = avctx->width - 1;
@@ -163,7 +164,7 @@ static void fill_picture_parameters(AVCodecContext *avctx, · 90 unmodified lines
163164 pp->bBitstreamConcealmentMethod = 0;
164165 }
165166
166−static void fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice,
167+void ff_dxva2_vc1_fill_slice(AVCodecContext *avctx, DXVA_SliceInfo *slice,
167168 unsigned position, unsigned size)
168169 {
169170 const VC1Context *v = avctx->priv_data;
@@ -322,7 +323,7 @@ static int dxva2_vc1_start_frame(AVCodecContext *avctx, · 152 unmodified lines
322323 return -1;
323324 assert(ctx_pic);
324325
325− fill_picture_parameters(avctx, ctx, v, &ctx_pic->pp);
326+ ff_dxva2_vc1_fill_picture_parameters(avctx, ctx, &ctx_pic->pp);
326327
327328 ctx_pic->slice_count = 0;
328329 ctx_pic->bitstream_size = 0;
@@ -356,7 +357,7 @@ static int dxva2_vc1_decode_slice(AVCodecContext *avctx, · 27 unmodified lines
356357 ctx_pic->bitstream_size += size;
357358
358359 position = buffer - ctx_pic->bitstream;
359− fill_slice(avctx, &ctx_pic->slice[ctx_pic->slice_count++], position, size);
360+ ff_dxva2_vc1_fill_slice(avctx, &ctx_pic->slice[ctx_pic->slice_count++], position, size);
360361 return 0;
361362 }
362363
libavcodec/hwaccels.h+2−0
@@ -67,6 +67,7 @@ extern const struct FFHWAccel ff_mpeg4_videotoolbox_hwaccel; · 66 unmodified lines
6767 extern const struct FFHWAccel ff_prores_videotoolbox_hwaccel;
6868 extern const struct FFHWAccel ff_vc1_d3d11va_hwaccel;
6969 extern const struct FFHWAccel ff_vc1_d3d11va2_hwaccel;
70+extern const struct FFHWAccel ff_vc1_d3d12va_hwaccel;
7071 extern const struct FFHWAccel ff_vc1_dxva2_hwaccel;
7172 extern const struct FFHWAccel ff_vc1_nvdec_hwaccel;
7273 extern const struct FFHWAccel ff_vc1_vaapi_hwaccel;
@@ -83,6 +84,7 @@ extern const struct FFHWAccel ff_vp9_vdpau_hwaccel; · 10 unmodified lines
8384 extern const struct FFHWAccel ff_vp9_videotoolbox_hwaccel;
8485 extern const struct FFHWAccel ff_wmv3_d3d11va_hwaccel;
8586 extern const struct FFHWAccel ff_wmv3_d3d11va2_hwaccel;
87+extern const struct FFHWAccel ff_wmv3_d3d12va_hwaccel;
8688 extern const struct FFHWAccel ff_wmv3_dxva2_hwaccel;
8789 extern const struct FFHWAccel ff_wmv3_nvdec_hwaccel;
8890 extern const struct FFHWAccel ff_wmv3_vaapi_hwaccel;
libavcodec/vc1dec.c+9−0
@@ -1373,6 +1373,9 @@ static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = { · 1372 unmodified lines
13731373 AV_PIX_FMT_D3D11VA_VLD,
13741374 AV_PIX_FMT_D3D11,
13751375 #endif
1376+#if CONFIG_VC1_D3D12VA_HWACCEL
1377+ AV_PIX_FMT_D3D12,
1378+#endif
13761379 #if CONFIG_VC1_NVDEC_HWACCEL
13771380 AV_PIX_FMT_CUDA,
13781381 #endif
@@ -1408,6 +1411,9 @@ const FFCodec ff_vc1_decoder = { · 29 unmodified lines
14081411 #if CONFIG_VC1_D3D11VA2_HWACCEL
14091412 HWACCEL_D3D11VA2(vc1),
14101413 #endif
1414+#if CONFIG_VC1_D3D12VA_HWACCEL
1415+ HWACCEL_D3D12VA(vc1),
1416+#endif
14111417 #if CONFIG_VC1_NVDEC_HWACCEL
14121418 HWACCEL_NVDEC(vc1),
14131419 #endif
@@ -1445,6 +1451,9 @@ const FFCodec ff_wmv3_decoder = { · 31 unmodified lines
14451451 #if CONFIG_WMV3_D3D11VA2_HWACCEL
14461452 HWACCEL_D3D11VA2(wmv3),
14471453 #endif
1454+#if CONFIG_WMV3_D3D12VA_HWACCEL
1455+ HWACCEL_D3D12VA(wmv3),
1456+#endif
14481457 #if CONFIG_WMV3_NVDEC_HWACCEL
14491458 HWACCEL_NVDEC(wmv3),
14501459 #endif

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t