FFmpeg
Loading...
Searching...
No Matches
d3d12va_av1.c
Go to the documentation of this file.
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"
26#include "libavutil/mem.h"
27#include "av1dec.h"
28#include "dxva2_internal.h"
29#include "d3d12va_decode.h"
30
31#define MAX_TILES 256
32
38
39#define D3D12_AV1_DECODE_CONTEXT(avctx) ((D3D12AV1DecodeContext *)D3D12VA_DECODE_CONTEXT(avctx))
40
42 DXVA_PicParams_AV1 pp;
43 unsigned tile_count;
44 DXVA_Tile_AV1 tiles[MAX_TILES];
45 uint8_t *bitstream;
48
50 av_unused const AVBufferRef *buffer_ref,
51 av_unused const uint8_t *buffer,
52 av_unused uint32_t size)
53{
54 const AV1DecContext *h = avctx->priv_data;
55 AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
57 if (!ctx)
58 return -1;
59
60 av_assert0(ctx_pic);
61
62 ctx->used_mask = 0;
63
64 if (ff_dxva2_av1_fill_picture_parameters(avctx, (AVDXVAContext *)ctx, &ctx_pic->pp) < 0)
65 return -1;
66
67 ctx_pic->bitstream = NULL;
68 ctx_pic->bitstream_size = 0;
69 ctx_pic->tile_count = 0;
70
71 return 0;
72}
73
75 const uint8_t *buffer,
76 uint32_t size)
77{
78 const AV1DecContext *h = avctx->priv_data;
79 const AV1RawFrameHeader *frame_header = h->raw_frame_header;
80 AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
82 int offset = 0;
83 uint32_t tg_start, tg_end;
84
85 ctx_pic->tile_count = frame_header->tile_cols * frame_header->tile_rows;
86
87 if (ctx_pic->tile_count > MAX_TILES)
88 return AVERROR(ENOSYS);
89
90 if (ctx_pic->tile_count == h->tg_end - h->tg_start + 1) {
91 tg_start = 0;
92 tg_end = ctx_pic->tile_count - 1;
93 ctx_pic->bitstream = (uint8_t *)buffer;
94 ctx_pic->bitstream_size = size;
95 } else {
96 size_t new_size = ctx_pic->bitstream_size + (size_t)size;
97 uint8_t *tmp = av_fast_realloc(av1_ctx->bitstream_buffer, &av1_ctx->bitstream_size, new_size);
98 if (!tmp)
99 return AVERROR(ENOMEM);
100
101 if (ctx_pic->bitstream_size && ctx_pic->bitstream != av1_ctx->bitstream_buffer)
102 memcpy(tmp, ctx_pic->bitstream, ctx_pic->bitstream_size);
103 av1_ctx->bitstream_buffer = tmp;
104 ctx_pic->bitstream = av1_ctx->bitstream_buffer;
105 memcpy(ctx_pic->bitstream + ctx_pic->bitstream_size, buffer, size);
106 tg_start = h->tg_start;
107 tg_end = h->tg_end;
108 offset = ctx_pic->bitstream_size;
109 ctx_pic->bitstream_size += size;
110 }
111
112 for (uint32_t tile_num = tg_start; tile_num <= tg_end; tile_num++) {
113 ctx_pic->tiles[tile_num].DataOffset = offset + h->tile_group_info[tile_num].tile_offset;
114 ctx_pic->tiles[tile_num].DataSize = h->tile_group_info[tile_num].tile_size;
115 ctx_pic->tiles[tile_num].row = h->tile_group_info[tile_num].tile_row;
116 ctx_pic->tiles[tile_num].column = h->tile_group_info[tile_num].tile_column;
117 ctx_pic->tiles[tile_num].anchor_frame = 0xFF;
118 }
119
120 return 0;
121}
122
123static int update_input_arguments(AVCodecContext *avctx, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *input_args, ID3D12Resource *buffer)
124{
125 const AV1DecContext *h = avctx->priv_data;
126 AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
127 void *mapped_data;
128
129 D3D12_VIDEO_DECODE_FRAME_ARGUMENT *args = &input_args->FrameArguments[input_args->NumFrameArguments++];
130 args->Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_SLICE_CONTROL;
131 args->Size = sizeof(DXVA_Tile_AV1) * ctx_pic->tile_count;
132 args->pData = ctx_pic->tiles;
133
134 input_args->CompressedBitstream = (D3D12_VIDEO_DECODE_COMPRESSED_BITSTREAM){
135 .pBuffer = buffer,
136 .Offset = 0,
137 .Size = ctx_pic->bitstream_size,
138 };
139
140 if (FAILED(ID3D12Resource_Map(buffer, 0, NULL, &mapped_data))) {
141 av_log(avctx, AV_LOG_ERROR, "Failed to map D3D12 Buffer resource!\n");
142 return AVERROR(EINVAL);
143 }
144
145 memcpy(mapped_data, ctx_pic->bitstream, ctx_pic->bitstream_size);
146
147 ID3D12Resource_Unmap(buffer, 0, NULL);
148
149 return 0;
150}
151
153{
154 int ret;
155 const AV1DecContext *h = avctx->priv_data;
156 AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
157
158 if (ctx_pic->tiles <= 0 || ctx_pic->bitstream_size <= 0)
159 return -1;
160
161 ret = ff_d3d12va_common_end_frame(avctx, h->cur_frame.f, &ctx_pic->pp, sizeof(ctx_pic->pp),
162 NULL, 0, ctx_pic->bitstream_size,
164
165 return ret;
166}
167
169{
171 DXVA_PicParams_AV1 pp;
172
173 int ret;
174
175 switch (avctx->profile) {
177 ctx->cfg.DecodeProfile = D3D12_VIDEO_DECODE_PROFILE_AV1_PROFILE0;
178 break;
180 ctx->cfg.DecodeProfile = avctx->sw_pix_fmt == AV_PIX_FMT_YUV420P12 ?
181 D3D12_VIDEO_DECODE_PROFILE_AV1_12BIT_PROFILE2_420 :
182 D3D12_VIDEO_DECODE_PROFILE_AV1_PROFILE2;
183 break;
184 default:
185 return AVERROR(EINVAL);
186 }
187
188 ctx->max_num_ref = FF_ARRAY_ELEMS(pp.RefFrameMapTextureIndex) + 1;
189
190 ret = ff_d3d12va_decode_init(avctx);
191 if (ret < 0)
192 return ret;
193
194 return 0;
195}
196
198{
200
201 if (ctx->bitstream_buffer)
202 av_freep(&ctx->bitstream_buffer);
203 ctx->bitstream_size = 0;
204
205 return ff_d3d12va_decode_uninit(avctx);
206}
207
208#if CONFIG_AV1_D3D12VA_HWACCEL
210 .p.name = "av1_d3d12va",
211 .p.type = AVMEDIA_TYPE_VIDEO,
212 .p.id = AV_CODEC_ID_AV1,
213 .p.pix_fmt = AV_PIX_FMT_D3D12,
216 .start_frame = d3d12va_av1_start_frame,
217 .decode_slice = d3d12va_av1_decode_slice,
218 .end_frame = d3d12va_av1_end_frame,
219 .frame_params = ff_d3d12va_common_frame_params,
220 .frame_priv_data_size = sizeof(AV1DecodePictureContext),
221 .priv_data_size = sizeof(D3D12AV1DecodeContext),
222};
223#endif
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define NULL
Definition coverity.c:32
static av_cold int d3d12va_av1_decode_init(AVCodecContext *avctx)
static int update_input_arguments(AVCodecContext *avctx, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *input_args, ID3D12Resource *buffer)
#define MAX_TILES
Definition d3d12va_av1.c:31
static int d3d12va_av1_start_frame(AVCodecContext *avctx, av_unused const AVBufferRef *buffer_ref, av_unused const uint8_t *buffer, av_unused uint32_t size)
Definition d3d12va_av1.c:49
static int d3d12va_av1_end_frame(AVCodecContext *avctx)
#define D3D12_AV1_DECODE_CONTEXT(avctx)
Definition d3d12va_av1.c:39
static int d3d12va_av1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition d3d12va_av1.c:74
static av_cold int d3d12va_av1_decode_uninit(AVCodecContext *avctx)
av_cold int ff_d3d12va_decode_init(AVCodecContext *avctx)
init D3D12VADecodeContext
int ff_d3d12va_common_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
d3d12va common frame params
int ff_d3d12va_common_end_frame(AVCodecContext *avctx, AVFrame *frame, const void *pp, unsigned pp_size, const void *qm, unsigned qm_size, UINT64 bitstream_size, int(*update_input_arguments)(AVCodecContext *, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *, ID3D12Resource *))
d3d12va common end frame
av_cold int ff_d3d12va_decode_uninit(AVCodecContext *avctx)
uninit D3D12VADecodeContext
#define D3D12VA_DECODE_CONTEXT(avctx)
#define AV_PROFILE_AV1_MAIN
Definition defs.h:169
#define AV_PROFILE_AV1_PROFESSIONAL
Definition defs.h:171
int ff_dxva2_av1_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_AV1 *pp)
Definition dxva2_av1.c:60
@ AV_CODEC_ID_AV1
Definition codec_id.h:275
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition mem.c:495
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
const struct FFHWAccel ff_av1_d3d12va_hwaccel
unsigned offset
Definition libaomenc.c:763
#define av_unused
Definition attributes.h:164
#define av_cold
Definition attributes.h:117
Memory handling functions.
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:549
@ AV_PIX_FMT_D3D12
Hardware surfaces for Direct3D 12.
Definition pixfmt.h:440
#define FF_ARRAY_ELEMS(a)
DXVA_Tile_AV1 tiles[MAX_TILES]
Definition d3d12va_av1.c:44
DXVA_PicParams_AV1 pp
Definition d3d12va_av1.c:42
A reference to a data buffer.
Definition buffer.h:82
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:650
int profile
profile
Definition avcodec.h:1637
void * priv_data
Definition avcodec.h:470
D3D12VADecodeContext ctx
Definition d3d12va_av1.c:34
uint8_t * bitstream_buffer
Definition d3d12va_av1.c:35
unsigned int bitstream_size
Definition d3d12va_av1.c:36
This structure is used to provide the necessary configurations and data to the FFmpeg Direct3D 12 HWA...
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
static char buffer[20]
Definition seek.c:32
int size