FFmpeg
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 "av1dec.h"
27 #include "dxva2_internal.h"
28 #include "d3d12va_decode.h"
29 
30 #define MAX_TILES 256
31 
32 typedef struct D3D12AV1DecodeContext {
34  uint8_t *bitstream_buffer;
36 
37 #define D3D12_AV1_DECODE_CONTEXT(avctx) ((D3D12AV1DecodeContext *)D3D12VA_DECODE_CONTEXT(avctx))
38 
39 typedef 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;
46 
47 static 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;
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 
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 
109 static int update_input_arguments(AVCodecContext *avctx, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *input_args, ID3D12Resource *buffer)
110 {
111  const AV1DecContext *h = avctx->priv_data;
112  AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
113  void *mapped_data;
114 
115  D3D12_VIDEO_DECODE_FRAME_ARGUMENT *args = &input_args->FrameArguments[input_args->NumFrameArguments++];
116  args->Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_SLICE_CONTROL;
117  args->Size = sizeof(DXVA_Tile_AV1) * ctx_pic->tile_count;
118  args->pData = ctx_pic->tiles;
119 
120  input_args->CompressedBitstream = (D3D12_VIDEO_DECODE_COMPRESSED_BITSTREAM){
121  .pBuffer = buffer,
122  .Offset = 0,
123  .Size = ctx_pic->bitstream_size,
124  };
125 
126  if (FAILED(ID3D12Resource_Map(buffer, 0, NULL, &mapped_data))) {
127  av_log(avctx, AV_LOG_ERROR, "Failed to map D3D12 Buffer resource!\n");
128  return AVERROR(EINVAL);
129  }
130 
131  memcpy(mapped_data, ctx_pic->bitstream, ctx_pic->bitstream_size);
132 
133  ID3D12Resource_Unmap(buffer, 0, NULL);
134 
135  return 0;
136 }
137 
139 {
140  int ret;
141  const AV1DecContext *h = avctx->priv_data;
142  AV1DecodePictureContext *ctx_pic = h->cur_frame.hwaccel_picture_private;
143 
144  if (ctx_pic->tiles <= 0 || ctx_pic->bitstream_size <= 0)
145  return -1;
146 
147  ret = ff_d3d12va_common_end_frame(avctx, h->cur_frame.f, &ctx_pic->pp, sizeof(ctx_pic->pp),
149 
150  return ret;
151 }
152 
154 {
157  DXVA_PicParams_AV1 pp;
158 
159  int ret;
160 
161  if (avctx->profile != AV_PROFILE_AV1_MAIN)
162  return AVERROR(EINVAL);
163 
164  ctx->cfg.DecodeProfile = D3D12_VIDEO_DECODE_PROFILE_AV1_PROFILE0;
165 
166  ctx->max_num_ref = FF_ARRAY_ELEMS(pp.RefFrameMapTextureIndex) + 1;
167 
168  ret = ff_d3d12va_decode_init(avctx);
169  if (ret < 0)
170  return ret;
171 
172  if (!av1_ctx->bitstream_buffer) {
174  if (!av1_ctx->bitstream_buffer)
175  return AVERROR(ENOMEM);
176  }
177 
178  return 0;
179 }
180 
182 {
184 
185  if (ctx->bitstream_buffer)
186  av_freep(&ctx->bitstream_buffer);
187 
188  return ff_d3d12va_decode_uninit(avctx);
189 }
190 
191 #if CONFIG_AV1_D3D12VA_HWACCEL
193  .p.name = "av1_d3d12va",
194  .p.type = AVMEDIA_TYPE_VIDEO,
195  .p.id = AV_CODEC_ID_AV1,
196  .p.pix_fmt = AV_PIX_FMT_D3D12,
197  .init = d3d12va_av1_decode_init,
198  .uninit = d3d12va_av1_decode_uninit,
199  .start_frame = d3d12va_av1_start_frame,
200  .decode_slice = d3d12va_av1_decode_slice,
201  .end_frame = d3d12va_av1_end_frame,
202  .frame_params = ff_d3d12va_common_frame_params,
203  .frame_priv_data_size = sizeof(AV1DecodePictureContext),
204  .priv_data_size = sizeof(D3D12AV1DecodeContext),
205 };
206 #endif
D3D12AV1DecodeContext::ctx
D3D12VADecodeContext ctx
Definition: d3d12va_av1.c:33
AV1DecodePictureContext
Definition: d3d12va_av1.c:39
d3d12va_av1_end_frame
static int d3d12va_av1_end_frame(AVCodecContext *avctx)
Definition: d3d12va_av1.c:138
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ff_av1_d3d12va_hwaccel
const struct FFHWAccel ff_av1_d3d12va_hwaccel
MAX_TILES
#define MAX_TILES
Definition: d3d12va_av1.c:30
av_unused
#define av_unused
Definition: attributes.h:131
FFHWAccel::p
AVHWAccel p
The public AVHWAccel.
Definition: hwaccel_internal.h:38
AV1DecodePictureContext::tiles
DXVA_Tile_AV1 tiles[MAX_TILES]
Definition: d3d12va_av1.c:42
ff_d3d12va_decode_uninit
int ff_d3d12va_decode_uninit(AVCodecContext *avctx)
uninit D3D12VADecodeContext
Definition: d3d12va_decode.c:372
D3D12AV1DecodeContext
Definition: d3d12va_av1.c:32
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
FFHWAccel
Definition: hwaccel_internal.h:34
AV1DecodePictureContext::bitstream_size
unsigned bitstream_size
Definition: d3d12va_av1.c:44
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV1DecodePictureContext::bitstream
uint8_t * bitstream
Definition: d3d12va_av1.c:43
ff_d3d12va_common_frame_params
int ff_d3d12va_common_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
d3d12va common frame params
Definition: d3d12va_decode.c:270
av1dec.h
dxva2_internal.h
NULL
#define NULL
Definition: coverity.c:32
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:280
AV1RawFrameHeader
Definition: cbs_av1.h:165
d3d12va_av1_start_frame
static int d3d12va_av1_start_frame(AVCodecContext *avctx, av_unused const uint8_t *buffer, av_unused uint32_t size)
Definition: d3d12va_av1.c:47
ff_d3d12va_common_end_frame
int ff_d3d12va_common_end_frame(AVCodecContext *avctx, AVFrame *frame, const void *pp, unsigned pp_size, const void *qm, unsigned qm_size, int(*update_input_arguments)(AVCodecContext *, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *, ID3D12Resource *))
d3d12va common end frame
Definition: d3d12va_decode.c:433
AV_PIX_FMT_D3D12
@ AV_PIX_FMT_D3D12
Hardware surfaces for Direct3D 12.
Definition: pixfmt.h:440
D3D12VA_DECODE_CONTEXT
#define D3D12VA_DECODE_CONTEXT(avctx)
Definition: d3d12va_decode.h:128
AV1DecodePictureContext::pp
DXVA_PicParams_AV1 pp
Definition: d3d12va_av1.c:40
d3d12va_av1_decode_slice
static int d3d12va_av1_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: d3d12va_av1.c:69
size
int size
Definition: twinvq_data.h:10344
AV1DecContext
Definition: av1dec.h:79
AVDXVAContext
Definition: dxva2_internal.h:74
ff_d3d12va_get_suitable_max_bitstream_size
int ff_d3d12va_get_suitable_max_bitstream_size(AVCodecContext *avctx)
Get a suitable maximum bitstream size.
Definition: d3d12va_decode.c:43
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
AVHWAccel::name
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:2094
D3D12_AV1_DECODE_CONTEXT
#define D3D12_AV1_DECODE_CONTEXT(avctx)
Definition: d3d12va_av1.c:37
ret
ret
Definition: filter_design.txt:187
AV1DecodePictureContext::tile_count
unsigned tile_count
Definition: d3d12va_av1.c:41
AVCodecContext
main external API structure.
Definition: avcodec.h:445
frame_header
Definition: truemotion1.c:88
D3D12AV1DecodeContext::bitstream_buffer
uint8_t * bitstream_buffer
Definition: d3d12va_av1.c:34
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1639
d3d12va_av1_decode_init
static int d3d12va_av1_decode_init(AVCodecContext *avctx)
Definition: d3d12va_av1.c:153
ff_dxva2_av1_fill_picture_parameters
int ff_dxva2_av1_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_AV1 *pp)
Definition: dxva2_av1.c:59
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
D3D12VADecodeContext
This structure is used to provide the necessary configurations and data to the FFmpeg Direct3D 12 HWA...
Definition: d3d12va_decode.h:37
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
d3d12va_av1_decode_uninit
static int d3d12va_av1_decode_uninit(AVCodecContext *avctx)
Definition: d3d12va_av1.c:181
h
h
Definition: vp9dsp_template.c:2038
update_input_arguments
static int update_input_arguments(AVCodecContext *avctx, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *input_args, ID3D12Resource *buffer)
Definition: d3d12va_av1.c:109
hwcontext_d3d12va_internal.h
AV_PROFILE_AV1_MAIN
#define AV_PROFILE_AV1_MAIN
Definition: defs.h:167
ff_d3d12va_decode_init
int ff_d3d12va_decode_init(AVCodecContext *avctx)
init D3D12VADecodeContext
Definition: d3d12va_decode.c:282
d3d12va_decode.h