History

avcodec: add D3D12VA hardware accelerated AV1 decoding

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

Wu Jianhua committed 2 years ago

Commit b16fd961 parent 326288c7 files · +237 −3Browse files

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

Unified · Split

6 files
  1. libavcodec/Makefile +1 −0
  2. libavcodec/av1dec.c +10 −0
  3. libavcodec/d3d12va_av1.c +214 −0
  4. libavcodec/dxva2_av1.c +5 −3
  5. libavcodec/dxva2_internal.h +4 −0
  6. libavcodec/hwaccels.h +1 −0
libavcodec/Makefile+1−0
@@ -996,6 +996,7 @@ OBJS-$(CONFIG_VULKAN) += vulkan.o vulkan_video.o · 995 unmodified lines
996 996
997OBJS-$(CONFIG_AV1_D3D11VA_HWACCEL) += dxva2_av1.o997OBJS-$(CONFIG_AV1_D3D11VA_HWACCEL) += dxva2_av1.o
998OBJS-$(CONFIG_AV1_DXVA2_HWACCEL) += dxva2_av1.o998OBJS-$(CONFIG_AV1_DXVA2_HWACCEL) += dxva2_av1.o
999OBJS-$(CONFIG_AV1_D3D12VA_HWACCEL) += dxva2_av1.o d3d12va_av1.o
999OBJS-$(CONFIG_AV1_NVDEC_HWACCEL) += nvdec_av1.o1000OBJS-$(CONFIG_AV1_NVDEC_HWACCEL) += nvdec_av1.o
1000OBJS-$(CONFIG_AV1_VAAPI_HWACCEL) += vaapi_av1.o1001OBJS-$(CONFIG_AV1_VAAPI_HWACCEL) += vaapi_av1.o
1001OBJS-$(CONFIG_AV1_VDPAU_HWACCEL) += vdpau_av1.o1002OBJS-$(CONFIG_AV1_VDPAU_HWACCEL) += vdpau_av1.o
libavcodec/av1dec.c+10−0
@@ -511,6 +511,7 @@ static int get_pixel_format(AVCodecContext *avctx) · 510 unmodified lines
511 enum AVPixelFormat pix_fmt = get_sw_pixel_format(avctx, seq);511 enum AVPixelFormat pix_fmt = get_sw_pixel_format(avctx, seq);
512#define HWACCEL_MAX (CONFIG_AV1_DXVA2_HWACCEL + \512#define HWACCEL_MAX (CONFIG_AV1_DXVA2_HWACCEL + \
513 CONFIG_AV1_D3D11VA_HWACCEL * 2 + \513 CONFIG_AV1_D3D11VA_HWACCEL * 2 + \
514 CONFIG_AV1_D3D12VA_HWACCEL + \
514 CONFIG_AV1_NVDEC_HWACCEL + \515 CONFIG_AV1_NVDEC_HWACCEL + \
515 CONFIG_AV1_VAAPI_HWACCEL + \516 CONFIG_AV1_VAAPI_HWACCEL + \
516 CONFIG_AV1_VDPAU_HWACCEL + \517 CONFIG_AV1_VDPAU_HWACCEL + \
@@ -529,6 +530,9 @@ static int get_pixel_format(AVCodecContext *avctx) · 12 unmodified lines
529 *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;530 *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
530 *fmtp++ = AV_PIX_FMT_D3D11;531 *fmtp++ = AV_PIX_FMT_D3D11;
531#endif532#endif
533#if CONFIG_AV1_D3D12VA_HWACCEL
534 *fmtp++ = AV_PIX_FMT_D3D12;
535#endif
532#if CONFIG_AV1_NVDEC_HWACCEL536#if CONFIG_AV1_NVDEC_HWACCEL
533 *fmtp++ = AV_PIX_FMT_CUDA;537 *fmtp++ = AV_PIX_FMT_CUDA;
534#endif538#endif
@@ -550,6 +554,9 @@ static int get_pixel_format(AVCodecContext *avctx) · 15 unmodified lines
550 *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;554 *fmtp++ = AV_PIX_FMT_D3D11VA_VLD;
551 *fmtp++ = AV_PIX_FMT_D3D11;555 *fmtp++ = AV_PIX_FMT_D3D11;
552#endif556#endif
557#if CONFIG_AV1_D3D12VA_HWACCEL
558 *fmtp++ = AV_PIX_FMT_D3D12;
559#endif
553#if CONFIG_AV1_NVDEC_HWACCEL560#if CONFIG_AV1_NVDEC_HWACCEL
554 *fmtp++ = AV_PIX_FMT_CUDA;561 *fmtp++ = AV_PIX_FMT_CUDA;
555#endif562#endif
@@ -1507,6 +1514,9 @@ const FFCodec ff_av1_decoder = { · 951 unmodified lines
1507#if CONFIG_AV1_D3D11VA2_HWACCEL1514#if CONFIG_AV1_D3D11VA2_HWACCEL
1508 HWACCEL_D3D11VA2(av1),1515 HWACCEL_D3D11VA2(av1),
1509#endif1516#endif
1517#if CONFIG_AV1_D3D12VA_HWACCEL
1518 HWACCEL_D3D12VA(av1),
1519#endif
1510#if CONFIG_AV1_NVDEC_HWACCEL1520#if CONFIG_AV1_NVDEC_HWACCEL
1511 HWACCEL_NVDEC(av1),1521 HWACCEL_NVDEC(av1),
1512#endif1522#endif
libavcodec/d3d12va_av1.c+214−0
@@ -0,0 +1,214 @@
1/*
2 * Direct3D 12 AV1 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 "av1dec.h"
27#include "dxva2_internal.h"
28#include "d3d12va_decode.h"
29
30#define MAX_TILES 256
31
32typedef struct D3D12AV1DecodeContext {
33 D3D12VADecodeContext ctx;
34 uint8_t *bitstream_buffer;
35} D3D12AV1DecodeContext;
36
37#define D3D12_AV1_DECODE_CONTEXT(avctx) ((D3D12AV1DecodeContext *)D3D12VA_DECODE_CONTEXT(avctx))
38
39typedef struct AV1DecodePictureContext {
40 DXVA_PicParams_AV1 pp;
41 unsigned tile_count;
42 DXVA_Tile_AV1 tiles[MAX_TILES];
43 uint8_t *bitstream;
44 unsigned bitstream_size;
45} AV1DecodePictureContext;
46
47static int d3d12va_av1_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
48{
49 const AV1DecContext *h = avctx->priv_data;
50 AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
51 D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
52 if (!ctx)
53 return -1;
54
55 av_assert0(ctx_pic);
56
57 ctx->used_mask = 0;
58
59 if (ff_dxva2_av1_fill_picture_parameters(avctx, (AVDXVAContext *)ctx, &ctx_pic->pp) < 0)
60 return -1;
61
62 ctx_pic->bitstream = NULL;
63 ctx_pic->bitstream_size = 0;
64 ctx_pic->tile_count = 0;
65
66 return 0;
67}
68
69static int d3d12va_av1_decode_slice(AVCodecContext *avctx,
70 const uint8_t *buffer,
71 uint32_t size)
72{
73 const AV1DecContext *h = avctx->priv_data;
74 const AV1RawFrameHeader *frame_header = h->raw_frame_header;
75 AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
76 int offset = 0;
77 uint32_t tg_start, tg_end;
78
79 ctx_pic->tile_count = frame_header->tile_cols * frame_header->tile_rows;
80
81 if (ctx_pic->tile_count > MAX_TILES)
82 return AVERROR(ENOSYS);
83
84 if (ctx_pic->tile_count == h->tg_end - h->tg_start + 1) {
85 tg_start = 0;
86 tg_end = ctx_pic->tile_count - 1;
87 ctx_pic->bitstream = (uint8_t *)buffer;
88 ctx_pic->bitstream_size = size;
89 } else {
90 ctx_pic->bitstream = D3D12_AV1_DECODE_CONTEXT(avctx)->bitstream_buffer;
91 memcpy(ctx_pic->bitstream + ctx_pic->bitstream_size, buffer, size);
92 tg_start = h->tg_start;
93 tg_end = h->tg_end;
94 offset = ctx_pic->bitstream_size;
95 ctx_pic->bitstream_size += size;
96 }
97
98 for (uint32_t tile_num = tg_start; tile_num <= tg_end; tile_num++) {
99 ctx_pic->tiles[tile_num].DataOffset = offset + h->tile_group_info[tile_num].tile_offset;
100 ctx_pic->tiles[tile_num].DataSize = h->tile_group_info[tile_num].tile_size;
101 ctx_pic->tiles[tile_num].row = h->tile_group_info[tile_num].tile_row;
102 ctx_pic->tiles[tile_num].column = h->tile_group_info[tile_num].tile_column;
103 ctx_pic->tiles[tile_num].anchor_frame = 0xFF;
104 }
105
106 return 0;
107}
108
109static int update_input_arguments(AVCodecContext *avctx, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *input_args, ID3D12Resource *buffer)
110{
111 D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
112 AVHWFramesContext *frames_ctx = D3D12VA_FRAMES_CONTEXT(avctx);
113 AVD3D12VAFramesContext *frames_hwctx = frames_ctx->hwctx;
114 const AV1DecContext *h = avctx->priv_data;
115 AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
116 uint8_t *mapped_data;
117
118 D3D12_VIDEO_DECODE_FRAME_ARGUMENT *args = &input_args->FrameArguments[input_args->NumFrameArguments++];
119 args->Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_SLICE_CONTROL;
120 args->Size = sizeof(DXVA_Tile_AV1) * ctx_pic->tile_count;
121 args->pData = ctx_pic->tiles;
122
123 input_args->CompressedBitstream = (D3D12_VIDEO_DECODE_COMPRESSED_BITSTREAM){
124 .pBuffer = buffer,
125 .Offset = 0,
126 .Size = ctx_pic->bitstream_size,
127 };
128
129 if (FAILED(ID3D12Resource_Map(buffer, 0, NULL, &mapped_data))) {
130 av_log(avctx, AV_LOG_ERROR, "Failed to map D3D12 Buffer resource!\n");
131 return AVERROR(EINVAL);
132 }
133
134 memcpy(mapped_data, ctx_pic->bitstream, ctx_pic->bitstream_size);
135
136 ID3D12Resource_Unmap(buffer, 0, NULL);
137
138 return 0;
139}
140
141static int d3d12va_av1_end_frame(AVCodecContext *avctx)
142{
143 int ret;
144 const AV1DecContext *h = avctx->priv_data;
145 D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
146 AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
147
148 if (ctx_pic->tiles <= 0 || ctx_pic->bitstream_size <= 0)
149 return -1;
150
151 ret = ff_d3d12va_common_end_frame(avctx, h->cur_frame.f, &ctx_pic->pp, sizeof(ctx_pic->pp),
152 NULL, 0, update_input_arguments);
153
154 return ret;
155}
156
157static int d3d12va_av1_decode_init(AVCodecContext *avctx)
158{
159 const AV1DecContext *h = avctx->priv_data;
160 D3D12VADecodeContext *ctx = D3D12VA_DECODE_CONTEXT(avctx);
161 D3D12AV1DecodeContext *av1_ctx = D3D12_AV1_DECODE_CONTEXT(avctx);
162 AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
163 DXVA_PicParams_AV1 pp;
164
165 int ret;
166
167 if (avctx->profile != FF_PROFILE_AV1_MAIN)
168 return AVERROR(EINVAL);
169
170 ctx->cfg.DecodeProfile = D3D12_VIDEO_DECODE_PROFILE_AV1_PROFILE0;
171
172 ctx->max_num_ref = FF_ARRAY_ELEMS(pp.RefFrameMapTextureIndex) + 1;
173
174 ret = ff_d3d12va_decode_init(avctx);
175 if (ret < 0)
176 return ret;
177
178 if (!av1_ctx->bitstream_buffer) {
179 av1_ctx->bitstream_buffer = av_malloc(ff_d3d12va_get_suitable_max_bitstream_size(avctx));
180 if (!av1_ctx->bitstream_buffer)
181 return AVERROR(ENOMEM);
182 }
183
184 return 0;
185}
186
187static int d3d12va_av1_decode_uninit(AVCodecContext *avctx)
188{
189 const AV1DecContext *h = avctx->priv_data;
190 D3D12AV1DecodeContext *ctx = D3D12_AV1_DECODE_CONTEXT(avctx);
191 AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
192
193 if (ctx->bitstream_buffer)
194 av_freep(&ctx->bitstream_buffer);
195
196 return ff_d3d12va_decode_uninit(avctx);
197}
198
199#if CONFIG_AV1_D3D12VA_HWACCEL
200const FFHWAccel ff_av1_d3d12va_hwaccel = {
201 .p.name = "av1_d3d12va",
202 .p.type = AVMEDIA_TYPE_VIDEO,
203 .p.id = AV_CODEC_ID_AV1,
204 .p.pix_fmt = AV_PIX_FMT_D3D12,
205 .init = d3d12va_av1_decode_init,
206 .uninit = d3d12va_av1_decode_uninit,
207 .start_frame = d3d12va_av1_start_frame,
208 .decode_slice = d3d12va_av1_decode_slice,
209 .end_frame = d3d12va_av1_end_frame,
210 .frame_params = ff_d3d12va_common_frame_params,
211 .frame_priv_data_size = sizeof(AV1DecodePictureContext),
212 .priv_data_size = sizeof(D3D12AV1DecodeContext),
213};
214#endif
libavcodec/dxva2_av1.c+5−3
@@ -56,10 +56,11 @@ static int get_bit_depth_from_seq(const AV1RawSequenceHeader *seq) · 55 unmodified lines
56 return 8;56 return 8;
57}57}
58 58
59static int fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, const AV1DecContext *h,59int ff_dxva2_av1_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx,
60 DXVA_PicParams_AV1 *pp)60 DXVA_PicParams_AV1 *pp)
61{61{
62 int i,j, uses_lr;62 int i,j, uses_lr;
63 const AV1DecContext *h = avctx->priv_data;
63 const AV1RawSequenceHeader *seq = h->raw_seq;64 const AV1RawSequenceHeader *seq = h->raw_seq;
64 const AV1RawFrameHeader *frame_header = h->raw_frame_header;65 const AV1RawFrameHeader *frame_header = h->raw_frame_header;
65 const AV1RawFilmGrainParams *film_grain = &h->cur_frame.film_grain;66 const AV1RawFilmGrainParams *film_grain = &h->cur_frame.film_grain;
@@ -75,7 +76,6 @@ static int fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *c · 9 unmodified lines
75 pp->max_width = seq->max_frame_width_minus_1 + 1;76 pp->max_width = seq->max_frame_width_minus_1 + 1;
76 pp->max_height = seq->max_frame_height_minus_1 + 1;77 pp->max_height = seq->max_frame_height_minus_1 + 1;
77 78
78 pp->CurrPicTextureIndex = ff_dxva2_get_surface_index(avctx, ctx, h->cur_frame.f, 1);
79 pp->superres_denom = frame_header->use_superres ? frame_header->coded_denom + AV1_SUPERRES_DENOM_MIN : AV1_SUPERRES_NUM;79 pp->superres_denom = frame_header->use_superres ? frame_header->coded_denom + AV1_SUPERRES_DENOM_MIN : AV1_SUPERRES_NUM;
80 pp->bitdepth = get_bit_depth_from_seq(seq);80 pp->bitdepth = get_bit_depth_from_seq(seq);
81 pp->seq_profile = seq->seq_profile;81 pp->seq_profile = seq->seq_profile;
@@ -154,6 +154,8 @@ static int fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *c · 72 unmodified lines
154 pp->RefFrameMapTextureIndex[i] = ff_dxva2_get_surface_index(avctx, ctx, ref_frame, 0);154 pp->RefFrameMapTextureIndex[i] = ff_dxva2_get_surface_index(avctx, ctx, ref_frame, 0);
155 }155 }
156 156
157 pp->CurrPicTextureIndex = ff_dxva2_get_surface_index(avctx, ctx, h->cur_frame.f, 1);
158
157 /* Loop filter parameters */159 /* Loop filter parameters */
158 pp->loop_filter.filter_level[0] = frame_header->loop_filter_level[0];160 pp->loop_filter.filter_level[0] = frame_header->loop_filter_level[0];
159 pp->loop_filter.filter_level[1] = frame_header->loop_filter_level[1];161 pp->loop_filter.filter_level[1] = frame_header->loop_filter_level[1];
@@ -281,7 +283,7 @@ static int dxva2_av1_start_frame(AVCodecContext *avctx, · 121 unmodified lines
281 av_assert0(ctx_pic);283 av_assert0(ctx_pic);
282 284
283 /* Fill up DXVA_PicParams_AV1 */285 /* Fill up DXVA_PicParams_AV1 */
284 if (fill_picture_parameters(avctx, ctx, h, &ctx_pic->pp) < 0)286 if (ff_dxva2_av1_fill_picture_parameters(avctx, ctx, &ctx_pic->pp) < 0)
285 return -1;287 return -1;
286 288
287 ctx_pic->bitstream_size = 0;289 ctx_pic->bitstream_size = 0;
libavcodec/dxva2_internal.h+4−0
@@ -177,4 +177,8 @@ void ff_dxva2_hevc_fill_scaling_lists(const AVCodecContext *avctx, AVDXVAContext · 176 unmodified lines
177 177
178int ff_dxva2_vp9_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_VP9 *pp);178int ff_dxva2_vp9_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_VP9 *pp);
179 179
180#if CONFIG_AV1_D3D12VA_HWACCEL || CONFIG_AV1_D3D11VA_HWACCEL || CONFIG_AV1_D3D11VA2_HWACCEL || CONFIG_AV1_DXVA2_HWACCEL
181int ff_dxva2_av1_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_AV1 *pp);
182#endif
183
180#endif /* AVCODEC_DXVA2_INTERNAL_H */184#endif /* AVCODEC_DXVA2_INTERNAL_H */
libavcodec/hwaccels.h+1−0
@@ -21,6 +21,7 @@ · 20 unmodified lines
21 21
22extern const struct FFHWAccel ff_av1_d3d11va_hwaccel;22extern const struct FFHWAccel ff_av1_d3d11va_hwaccel;
23extern const struct FFHWAccel ff_av1_d3d11va2_hwaccel;23extern const struct FFHWAccel ff_av1_d3d11va2_hwaccel;
24extern const struct FFHWAccel ff_av1_d3d12va_hwaccel;
24extern const struct FFHWAccel ff_av1_dxva2_hwaccel;25extern const struct FFHWAccel ff_av1_dxva2_hwaccel;
25extern const struct FFHWAccel ff_av1_nvdec_hwaccel;26extern const struct FFHWAccel ff_av1_nvdec_hwaccel;
26extern const struct FFHWAccel ff_av1_vaapi_hwaccel;27extern const struct FFHWAccel ff_av1_vaapi_hwaccel;

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