FFmpeg
Loading...
Searching...
No Matches
d3d12va_h264.c
Go to the documentation of this file.
1/*
2 * Direct3D 12 h264 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 "h264dec.h"
26#include "h264data.h"
27#include "h264_ps.h"
28#include "mpegutils.h"
29#include "dxva2_internal.h"
30#include "d3d12va_decode.h"
32#include <dxva.h>
33
35 DXVA_PicParams_H264 pp;
36 DXVA_Qmatrix_H264 qm;
37 unsigned slice_count;
38 DXVA_Slice_H264_Short slice_short[MAX_SLICES];
39 const uint8_t *bitstream;
42
43static void fill_slice_short(DXVA_Slice_H264_Short *slice,
44 unsigned position, unsigned size)
45{
46 memset(slice, 0, sizeof(*slice));
47 slice->BSNALunitDataLocation = position;
48 slice->SliceBytesInBuffer = size;
49 slice->wBadSliceChopping = 0;
50}
51
53 av_unused const AVBufferRef *buffer_ref,
54 av_unused const uint8_t *buffer,
55 av_unused uint32_t size)
56{
57 const H264Context *h = avctx->priv_data;
58 H264DecodePictureContext *ctx_pic = h->cur_pic_ptr->hwaccel_picture_private;
60
61 if (!ctx)
62 return -1;
63
64 av_assert0(ctx_pic);
65
66 ctx->used_mask = 0;
67
69
71
72 ctx_pic->slice_count = 0;
73 ctx_pic->bitstream_size = 0;
74 ctx_pic->bitstream = NULL;
75
76 return 0;
77}
78
79static int d3d12va_h264_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
80{
81 unsigned position;
82 const H264Context *h = avctx->priv_data;
83 const H264SliceContext *sl = &h->slice_ctx[0];
84 const H264Picture *current_picture = h->cur_pic_ptr;
85 H264DecodePictureContext *ctx_pic = current_picture->hwaccel_picture_private;
86
87 if (ctx_pic->slice_count >= MAX_SLICES)
88 return AVERROR(ERANGE);
89
90 if (!ctx_pic->bitstream)
91 ctx_pic->bitstream = buffer;
92 ctx_pic->bitstream_size += size;
93
94 position = buffer - ctx_pic->bitstream;
95 fill_slice_short(&ctx_pic->slice_short[ctx_pic->slice_count], position, size);
96 ctx_pic->slice_count++;
97
99 ctx_pic->pp.wBitFields &= ~(1 << 15); /* Set IntraPicFlag to 0 */
100
101 return 0;
102}
103
104#define START_CODE 65536
105#define START_CODE_SIZE 3
106static int update_input_arguments(AVCodecContext *avctx, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *input_args, ID3D12Resource *buffer)
107{
109 const H264Context *h = avctx->priv_data;
110 const H264Picture *current_picture = h->cur_pic_ptr;
111 H264DecodePictureContext *ctx_pic = current_picture->hwaccel_picture_private;
112
113 int i;
114 uint8_t *mapped_data, *mapped_ptr;
115 DXVA_Slice_H264_Short *slice;
116 D3D12_VIDEO_DECODE_FRAME_ARGUMENT *args;
117 UINT bitstream_size = ctx->bitstream_size;
118
119 if (FAILED(ID3D12Resource_Map(buffer, 0, NULL, (void **)&mapped_data))) {
120 av_log(avctx, AV_LOG_ERROR, "Failed to map D3D12 Buffer resource!\n");
121 return AVERROR(EINVAL);
122 }
123
124 mapped_ptr = mapped_data;
125 for (i = 0; i < ctx_pic->slice_count; i++) {
126 UINT position, size;
127 slice = &ctx_pic->slice_short[i];
128
129 position = slice->BSNALunitDataLocation;
130 size = slice->SliceBytesInBuffer;
131
132 if (START_CODE_SIZE + (uint64_t)size > bitstream_size) {
133 av_log(avctx, AV_LOG_ERROR, "Input frame bitstream size exceeds internal buffer!\n");
134 ID3D12Resource_Unmap(buffer, 0, NULL);
135 return AVERROR(EINVAL);
136 }
137
138 slice->SliceBytesInBuffer += START_CODE_SIZE;
139 slice->BSNALunitDataLocation = mapped_ptr - mapped_data;
140
141 *(uint32_t *)mapped_ptr = START_CODE;
142 mapped_ptr += START_CODE_SIZE;
143 bitstream_size -= START_CODE_SIZE;
144
145 memcpy(mapped_ptr, &ctx_pic->bitstream[position], size);
146 mapped_ptr += size;
147 bitstream_size -= size;
148 }
149
150 ID3D12Resource_Unmap(buffer, 0, NULL);
151
152 input_args->CompressedBitstream = (D3D12_VIDEO_DECODE_COMPRESSED_BITSTREAM){
153 .pBuffer = buffer,
154 .Offset = 0,
155 .Size = mapped_ptr - mapped_data,
156 };
157
158 args = &input_args->FrameArguments[input_args->NumFrameArguments++];
159 args->Type = D3D12_VIDEO_DECODE_ARGUMENT_TYPE_SLICE_CONTROL;
160 args->Size = sizeof(DXVA_Slice_H264_Short) * ctx_pic->slice_count;
161 args->pData = ctx_pic->slice_short;
162
163 return 0;
164}
165
167{
168 H264Context *h = avctx->priv_data;
169 H264DecodePictureContext *ctx_pic = h->cur_pic_ptr->hwaccel_picture_private;
170 H264SliceContext *sl = &h->slice_ctx[0];
171
172 int ret;
173
174 if (ctx_pic->slice_count <= 0 || ctx_pic->bitstream_size <= 0)
175 return -1;
176
177 ret = ff_d3d12va_common_end_frame(avctx, h->cur_pic_ptr->f,
178 &ctx_pic->pp, sizeof(ctx_pic->pp),
179 &ctx_pic->qm, sizeof(ctx_pic->qm),
181 if (!ret)
182 ff_h264_draw_horiz_band(h, sl, 0, h->avctx->height);
183
184 return ret;
185}
186
188{
190 DXVA_PicParams_H264 pp;
191
192 ctx->cfg.DecodeProfile = D3D12_VIDEO_DECODE_PROFILE_H264;
193
194 ctx->max_num_ref = FF_ARRAY_ELEMS(pp.RefFrameList) + 1;
195
196 return ff_d3d12va_decode_init(avctx);
197}
198
199#if CONFIG_H264_D3D12VA_HWACCEL
201 .p.name = "h264_d3d12va",
202 .p.type = AVMEDIA_TYPE_VIDEO,
203 .p.id = AV_CODEC_ID_H264,
204 .p.pix_fmt = AV_PIX_FMT_D3D12,
206 .uninit = ff_d3d12va_decode_uninit,
207 .start_frame = d3d12va_h264_start_frame,
208 .decode_slice = d3d12va_h264_decode_slice,
209 .end_frame = d3d12va_h264_end_frame,
210 .frame_params = ff_d3d12va_common_frame_params,
211 .frame_priv_data_size = sizeof(H264DecodePictureContext),
212 .priv_data_size = sizeof(D3D12VADecodeContext),
213};
214#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 i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#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 int d3d12va_h264_end_frame(AVCodecContext *avctx)
static int d3d12va_h264_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
static int update_input_arguments(AVCodecContext *avctx, D3D12_VIDEO_DECODE_INPUT_STREAM_ARGUMENTS *input_args, ID3D12Resource *buffer)
static void fill_slice_short(DXVA_Slice_H264_Short *slice, unsigned position, unsigned size)
#define START_CODE_SIZE
static int d3d12va_h264_start_frame(AVCodecContext *avctx, av_unused const AVBufferRef *buffer_ref, av_unused const uint8_t *buffer, av_unused uint32_t size)
static av_cold int d3d12va_h264_decode_init(AVCodecContext *avctx)
#define MAX_SLICES
void ff_dxva2_h264_fill_scaling_lists(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_Qmatrix_H264 *qm)
Definition dxva2_h264.c:168
void ff_dxva2_h264_fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, DXVA_PicParams_H264 *pp)
Definition dxva2_h264.c:51
@ AV_CODEC_ID_H264
Definition codec_id.h:77
#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
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_SI
Switching Intra.
Definition avutil.h:282
H.264 parameter set handling.
H.264 / AVC / MPEG-4 part10 codec.
const struct FFHWAccel ff_h264_d3d12va_hwaccel
void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl, int y, int height)
Definition h264dec.c:104
#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 START_CODE
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
Definition buffer.h:82
main external API structure.
Definition avcodec.h:443
void * priv_data
Definition avcodec.h:470
This structure is used to provide the necessary configurations and data to the FFmpeg Direct3D 12 HWA...
H264Context.
Definition h264dec.h:338
DXVA_Qmatrix_H264 qm
const uint8_t * bitstream
DXVA_Slice_H264_Short slice_short[MAX_SLICES]
DXVA_PicParams_H264 pp
void * hwaccel_picture_private
RefStruct reference for hardware accelerator private data.
Definition h264dec.h:128
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
static char buffer[20]
Definition seek.c:32
int size