FFmpeg
Loading...
Searching...
No Matches
d3d12va_vp9.c
Go to the documentation of this file.
1/*
2 * Direct3D 12 VP9 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
25#include "libavutil/avassert.h"
26#include "libavutil/pixdesc.h"
28
29#include "vp9shared.h"
30#include "dxva2_internal.h"
31#include "d3d12va_decode.h"
32
34 DXVA_PicParams_VP9 pp;
35 DXVA_Slice_VPx_Short slice;
36 const uint8_t *bitstream;
39
40static void fill_slice_short(DXVA_Slice_VPx_Short *slice, unsigned position, unsigned size)
41{
42 memset(slice, 0, sizeof(*slice));
43 slice->BSNALunitDataLocation = position;
44 slice->SliceBytesInBuffer = size;
45 slice->wBadSliceChopping = 0;
46}
47
49 av_unused const AVBufferRef *buffer_ref,
50 av_unused const uint8_t *buffer,
51 av_unused uint32_t size)
52{
53 const VP9SharedContext *h = avctx->priv_data;
55 VP9DecodePictureContext *ctx_pic = h->frames[CUR_FRAME].hwaccel_picture_private;
56
57 if (!ctx)
58 return -1;
59
60 av_assert0(ctx_pic);
61
62 ctx->used_mask = 0;
63
64 if (ff_dxva2_vp9_fill_picture_parameters(avctx, (AVDXVAContext *)ctx, &ctx_pic->pp) < 0)
65 return -1;
66
67 ctx_pic->bitstream_size = 0;
68 ctx_pic->bitstream = NULL;
69
70 return 0;
71}
72
73static int d3d12va_vp9_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
74{
75 const VP9SharedContext *h = avctx->priv_data;
76 VP9DecodePictureContext *ctx_pic = h->frames[CUR_FRAME].hwaccel_picture_private;
77 unsigned position;
78
79 if (!ctx_pic->bitstream)
80 ctx_pic->bitstream = buffer;
81 ctx_pic->bitstream_size += size;
82
83 position = buffer - ctx_pic->bitstream;
84 fill_slice_short(&ctx_pic->slice, position, size);
85
86 return 0;
87}
88
89static int update_input_arguments(AVCodecContext *avctx, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *input_args, ID3D12Resource *buffer)
90{
92 const VP9SharedContext *h = avctx->priv_data;
93 VP9DecodePictureContext *ctx_pic = h->frames[CUR_FRAME].hwaccel_picture_private;
94
95 void *mapped_data;
96 D3D12_VIDEO_DECODE_FRAME_ARGUMENT *args;
97
98 if (ctx_pic->slice.SliceBytesInBuffer > ctx->bitstream_size) {
99 av_log(avctx, AV_LOG_ERROR, "Input frame bitstream size exceeds internal buffer!\n");
100 return AVERROR(EINVAL);
101 }
102
103 if (FAILED(ID3D12Resource_Map(buffer, 0, NULL, &mapped_data))) {
104 av_log(avctx, AV_LOG_ERROR, "Failed to map D3D12 Buffer resource!\n");
105 return AVERROR(EINVAL);
106 }
107
108 args = &input_args->FrameArguments[input_args->NumFrameArguments++];
109 args->Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_SLICE_CONTROL;
110 args->Size = sizeof(ctx_pic->slice);
111 args->pData = &ctx_pic->slice;
112
113 memcpy(mapped_data, ctx_pic->bitstream, ctx_pic->slice.SliceBytesInBuffer);
114
115 ID3D12Resource_Unmap(buffer, 0, NULL);
116
117 input_args->CompressedBitstream = (D3D12_VIDEO_DECODE_COMPRESSED_BITSTREAM){
118 .pBuffer = buffer,
119 .Offset = 0,
120 .Size = ctx_pic->slice.SliceBytesInBuffer,
121 };
122
123 return 0;
124}
125
127{
128 VP9SharedContext *h = avctx->priv_data;
129 VP9DecodePictureContext *ctx_pic = h->frames[CUR_FRAME].hwaccel_picture_private;
130
131 if (ctx_pic->bitstream_size <= 0)
132 return -1;
133
134 return ff_d3d12va_common_end_frame(avctx, h->frames[CUR_FRAME].tf.f,
135 &ctx_pic->pp, sizeof(ctx_pic->pp), NULL, 0, update_input_arguments);
136}
137
139{
141 DXVA_PicParams_VP9 pp;
142
143 switch (avctx->profile) {
144 case AV_PROFILE_VP9_2:
145 case AV_PROFILE_VP9_3:
146 ctx->cfg.DecodeProfile = D3D12_VIDEO_DECODE_PROFILE_VP9_10BIT_PROFILE2;
147 break;
148
149 case AV_PROFILE_VP9_0:
150 case AV_PROFILE_VP9_1:
151 default:
152 ctx->cfg.DecodeProfile = D3D12_VIDEO_DECODE_PROFILE_VP9;
153 break;
154 };
155
156 ctx->max_num_ref = FF_ARRAY_ELEMS(pp.ref_frame_map) + 1;
157
158 return ff_d3d12va_decode_init(avctx);
159}
160
161#if CONFIG_VP9_D3D12VA_HWACCEL
163 .p.name = "vp9_d3d12va",
164 .p.type = AVMEDIA_TYPE_VIDEO,
165 .p.id = AV_CODEC_ID_VP9,
166 .p.pix_fmt = AV_PIX_FMT_D3D12,
168 .uninit = ff_d3d12va_decode_uninit,
169 .start_frame = d3d12va_vp9_start_frame,
170 .decode_slice = d3d12va_vp9_decode_slice,
171 .end_frame = d3d12va_vp9_end_frame,
172 .frame_params = ff_d3d12va_common_frame_params,
173 .frame_priv_data_size = sizeof(VP9DecodePictureContext),
174 .priv_data_size = sizeof(D3D12VADecodeContext),
175};
176#endif
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 int update_input_arguments(AVCodecContext *avctx, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *input_args, ID3D12Resource *buffer)
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
av_cold int ff_d3d12va_decode_uninit(AVCodecContext *avctx)
uninit D3D12VADecodeContext
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
#define D3D12VA_DECODE_CONTEXT(avctx)
static void fill_slice_short(DXVA_Slice_VPx_Short *slice, unsigned position, unsigned size)
Definition d3d12va_vp9.c:40
static int update_input_arguments(AVCodecContext *avctx, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *input_args, ID3D12Resource *buffer)
Definition d3d12va_vp9.c:89
static int d3d12va_vp9_start_frame(AVCodecContext *avctx, av_unused const AVBufferRef *buffer_ref, av_unused const uint8_t *buffer, av_unused uint32_t size)
Definition d3d12va_vp9.c:48
static int d3d12va_vp9_end_frame(AVCodecContext *avctx)
static av_cold int d3d12va_vp9_decode_init(AVCodecContext *avctx)
static int d3d12va_vp9_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition d3d12va_vp9.c:73
#define AV_PROFILE_VP9_3
Definition defs.h:157
#define AV_PROFILE_VP9_1
Definition defs.h:155
#define AV_PROFILE_VP9_0
Definition defs.h:154
#define AV_PROFILE_VP9_2
Definition defs.h:156
int ff_dxva2_vp9_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_VP9 *pp)
Definition dxva2_vp9.c:46
@ AV_CODEC_ID_VP9
Definition codec_id.h:217
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
const struct FFHWAccel ff_vp9_d3d12va_hwaccel
#define av_unused
Definition attributes.h:164
#define av_cold
Definition attributes.h:117
@ AV_PIX_FMT_D3D12
Hardware surfaces for Direct3D 12.
Definition pixfmt.h:440
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
Definition buffer.h:82
main external API structure.
Definition avcodec.h:443
int profile
profile
Definition avcodec.h:1636
void * priv_data
Definition avcodec.h:470
This structure is used to provide the necessary configurations and data to the FFmpeg Direct3D 12 HWA...
DXVA_Slice_VPx_Short slice
Definition d3d12va_vp9.c:35
DXVA_PicParams_VP9 pp
Definition d3d12va_vp9.c:34
const uint8_t * bitstream
Definition d3d12va_vp9.c:36
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
static char buffer[20]
Definition seek.c:32
int size
#define CUR_FRAME
Definition vp9shared.h:172