FFmpeg
Loading...
Searching...
No Matches
d3d12va_encode_h264.c
Go to the documentation of this file.
1/*
2 * Direct3D 12 HW acceleration video encoder
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20#include "libavutil/opt.h"
21#include "libavutil/common.h"
22#include "libavutil/mem.h"
23#include "libavutil/pixdesc.h"
25
26#include "avcodec.h"
27#include "cbs.h"
28#include "cbs_h264.h"
29#include "hw_base_encode_h264.h"
30#include "h2645data.h"
31#include "h264_levels.h"
32#include "codec_internal.h"
33#include "d3d12va_encode.h"
34
39
58
59typedef struct D3D12VAEncodeH264Level {
60 int level;
61 D3D12_VIDEO_ENCODER_LEVELS_H264 d3d12_level;
63
65 { 10, D3D12_VIDEO_ENCODER_LEVELS_H264_1 },
66 { 11, D3D12_VIDEO_ENCODER_LEVELS_H264_11 },
67 { 12, D3D12_VIDEO_ENCODER_LEVELS_H264_12 },
68 { 13, D3D12_VIDEO_ENCODER_LEVELS_H264_13 },
69 { 20, D3D12_VIDEO_ENCODER_LEVELS_H264_2 },
70 { 21, D3D12_VIDEO_ENCODER_LEVELS_H264_21 },
71 { 22, D3D12_VIDEO_ENCODER_LEVELS_H264_22 },
72 { 30, D3D12_VIDEO_ENCODER_LEVELS_H264_3 },
73 { 31, D3D12_VIDEO_ENCODER_LEVELS_H264_31 },
74 { 32, D3D12_VIDEO_ENCODER_LEVELS_H264_32 },
75 { 40, D3D12_VIDEO_ENCODER_LEVELS_H264_4 },
76 { 41, D3D12_VIDEO_ENCODER_LEVELS_H264_41 },
77 { 42, D3D12_VIDEO_ENCODER_LEVELS_H264_42 },
78 { 50, D3D12_VIDEO_ENCODER_LEVELS_H264_5 },
79 { 51, D3D12_VIDEO_ENCODER_LEVELS_H264_51 },
80 { 52, D3D12_VIDEO_ENCODER_LEVELS_H264_52 },
81 { 60, D3D12_VIDEO_ENCODER_LEVELS_H264_6 },
82 { 61, D3D12_VIDEO_ENCODER_LEVELS_H264_61 },
83 { 62, D3D12_VIDEO_ENCODER_LEVELS_H264_62 },
84};
85
86static const D3D12_VIDEO_ENCODER_PROFILE_H264 profile_main = D3D12_VIDEO_ENCODER_PROFILE_H264_MAIN;
87static const D3D12_VIDEO_ENCODER_PROFILE_H264 profile_high = D3D12_VIDEO_ENCODER_PROFILE_H264_HIGH;
88static const D3D12_VIDEO_ENCODER_PROFILE_H264 profile_high_10 = D3D12_VIDEO_ENCODER_PROFILE_H264_HIGH_10;
89
90#define D3D_PROFILE_DESC(name) \
91 { sizeof(D3D12_VIDEO_ENCODER_PROFILE_H264), { .pH264Profile = (D3D12_VIDEO_ENCODER_PROFILE_H264 *)&profile_ ## name } }
98
100 char *data, size_t *data_len,
102{
103 D3D12VAEncodeH264Context *priv = avctx->priv_data;
104 int err;
105
106 err = ff_cbs_write_fragment_data(priv->cbc, au);
107 if (err < 0) {
108 av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
109 return err;
110 }
111
112 if (*data_len < 8 * au->data_size - au->data_bit_padding) {
113 av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
114 "%zu < %zu.\n", *data_len,
115 8 * au->data_size - au->data_bit_padding);
116 return AVERROR(ENOSPC);
117 }
118
119 memcpy(data, au->data, au->data_size);
120 *data_len = 8 * au->data_size - au->data_bit_padding;
121
122 return 0;
123}
124
127 void *nal_unit)
128{
129 H264RawNALUnitHeader *header = nal_unit;
130 int err;
131
132 err = ff_cbs_insert_unit_content(au, -1,
133 header->nal_unit_type, nal_unit, NULL);
134 if (err < 0) {
135 av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
136 "type = %d.\n", header->nal_unit_type);
137 return err;
138 }
139
140 return 0;
141}
142
144 char *data, size_t *data_len)
145{
146 D3D12VAEncodeH264Context *priv = avctx->priv_data;
148 int err;
149
150 err = d3d12va_encode_h264_add_nal(avctx, au, &priv->units.raw_sps);
151 if (err < 0)
152 goto fail;
153
154 err = d3d12va_encode_h264_add_nal(avctx, au, &priv->units.raw_pps);
155 if (err < 0)
156 goto fail;
157
158 err = d3d12va_encode_h264_write_access_unit(avctx, data, data_len, au);
159fail:
160 ff_cbs_fragment_reset(au);
161 return err;
162}
163
165{
166 FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
168 D3D12VAEncodeH264Context *priv = avctx->priv_data;
169 AVD3D12VAFramesContext *hwctx = base_ctx->input_frames->hwctx;
170 H264RawSPS *sps = &priv->units.raw_sps;
171 H264RawPPS *pps = &priv->units.raw_pps;
172 D3D12_VIDEO_ENCODER_PROFILE_H264 profile = D3D12_VIDEO_ENCODER_PROFILE_H264_MAIN;
173 D3D12_VIDEO_ENCODER_LEVELS_H264 level = { 0 };
175 HRESULT hr;
176 int err;
177
178 D3D12_FEATURE_DATA_VIDEO_ENCODER_SUPPORT support = {
179 .NodeIndex = 0,
180 .Codec = D3D12_VIDEO_ENCODER_CODEC_H264,
181 .InputFormat = hwctx->format,
182 .RateControl = ctx->rc,
183 .IntraRefresh = ctx->intra_refresh.Mode,
184 .SubregionFrameEncoding = D3D12_VIDEO_ENCODER_FRAME_SUBREGION_LAYOUT_MODE_FULL_FRAME,
185 .ResolutionsListCount = 1,
186 .pResolutionList = &ctx->resolution,
187 .CodecGopSequence = ctx->gop,
188 .MaxReferenceFramesInDPB = MAX_DPB_SIZE - 1,
189 .CodecConfiguration = ctx->codec_conf,
190 .SuggestedProfile.DataSize = sizeof(D3D12_VIDEO_ENCODER_PROFILE_H264),
191 .SuggestedProfile.pH264Profile = &profile,
192 .SuggestedLevel.DataSize = sizeof(D3D12_VIDEO_ENCODER_LEVELS_H264),
193 .SuggestedLevel.pH264LevelSetting = &level,
194 .pResolutionDependentSupport = &ctx->res_limits,
195 };
196
197 hr = ID3D12VideoDevice3_CheckFeatureSupport(ctx->video_device3, D3D12_FEATURE_VIDEO_ENCODER_SUPPORT,
198 &support, sizeof(support));
199
200 if (FAILED(hr)) {
201 av_log(avctx, AV_LOG_ERROR, "Failed to check encoder support(%lx).\n", (long)hr);
202 return AVERROR(EINVAL);
203 }
204
205 if (!(support.SupportFlags & D3D12_VIDEO_ENCODER_SUPPORT_FLAG_GENERAL_SUPPORT_OK)) {
206 av_log(avctx, AV_LOG_ERROR, "Driver does not support requested features. ValidationFlags: %#x\n",
207 support.ValidationFlags);
208 ff_d3d12va_encode_check_encoder_feature_flags(avctx, support.ValidationFlags);
209 return AVERROR(EINVAL);
210 }
211
212 if (support.SupportFlags & D3D12_VIDEO_ENCODER_SUPPORT_FLAG_RECONSTRUCTED_FRAMES_REQUIRE_TEXTURE_ARRAYS) {
213 ctx->is_texture_array = 1;
214 av_log(avctx, AV_LOG_DEBUG, "D3D12 video encode on this device uses texture array mode.\n");
215 }
216
217 // Check if the configuration with DELTA_QP is supported
218 if (support.SupportFlags & D3D12_VIDEO_ENCODER_SUPPORT_FLAG_RATE_CONTROL_DELTA_QP_AVAILABLE) {
219 base_ctx->roi_allowed = 1;
220 // Store the QP map region size from resolution limits
221 ctx->qp_map_region_size = ctx->res_limits.QPMapRegionPixelsSize;
222 av_log(avctx, AV_LOG_DEBUG, "ROI encoding is supported via delta QP "
223 "(QP map region size: %d pixels).\n", ctx->qp_map_region_size);
224 } else {
225 base_ctx->roi_allowed = 0;
226 av_log(avctx, AV_LOG_DEBUG, "ROI encoding not supported by hardware for current rate control mode \n");
227 }
228
229 // Check motion estimation precision mode support
230 if (ctx->me_precision != D3D12_VIDEO_ENCODER_MOTION_ESTIMATION_PRECISION_MODE_MAXIMUM) {
231 if (!(support.SupportFlags & D3D12_VIDEO_ENCODER_SUPPORT_FLAG_MOTION_ESTIMATION_PRECISION_MODE_LIMIT_AVAILABLE)) {
232 av_log(avctx, AV_LOG_ERROR, "Hardware does not support motion estimation "
233 "precision mode limits.\n");
234 return AVERROR(ENOTSUP);
235 }
236 av_log(avctx, AV_LOG_VERBOSE, "Hardware supports motion estimation "
237 "precision mode limits.\n");
238 }
239
242
243 sps->pic_width_in_mbs_minus1 = ((base_ctx->surface_width + 0x0F) >> 4) - 1;
244 sps->pic_height_in_map_units_minus1 = ((base_ctx->surface_height + 0x0F) >> 4) - 1;
245
246 priv->unit_opts.mb_width = sps->pic_width_in_mbs_minus1 + 1;
247 priv->unit_opts.mb_height = sps->pic_height_in_map_units_minus1 +1;
248
249 err = ff_hw_base_encode_init_params_h264(base_ctx, avctx,
250 &priv->units, &priv->unit_opts);
251 if (err < 0)
252 return err;
253
254 avctx->level = priv->units.raw_sps.level_idc;
255
256 ctx->gop.pH264GroupOfPictures->pic_order_cnt_type = sps->pic_order_cnt_type;
257
258 // override the default value according to the gop size
259 sps->log2_max_frame_num_minus4 = FFMAX(av_ceil_log2(base_ctx->gop_size) - 4, 0);
260 ctx->gop.pH264GroupOfPictures->log2_max_frame_num_minus4 = sps->log2_max_frame_num_minus4;
261 pps->deblocking_filter_control_present_flag = 1;
262 pps->constrained_intra_pred_flag = priv->constrained_intra_pred;
263
264 return 0;
265}
266
268{
269 HRESULT hr;
270 FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
272 D3D12VAEncodeH264Context *priv = avctx->priv_data;
273
274 D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264 *config;
275 D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_H264 h264_caps;
276
277 D3D12_FEATURE_DATA_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT codec_caps = {
278 .NodeIndex = 0,
279 .Codec = D3D12_VIDEO_ENCODER_CODEC_H264,
280 .Profile = ctx->profile->d3d12_profile,
281 .CodecSupportLimits.DataSize = sizeof(D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_H264),
282 };
283
284 codec_caps.CodecSupportLimits.pH264Support = &h264_caps;
285 hr = ID3D12VideoDevice3_CheckFeatureSupport(ctx->video_device3, D3D12_FEATURE_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT,
286 &codec_caps, sizeof(codec_caps));
287 if (!(SUCCEEDED(hr) && codec_caps.IsSupported))
288 return AVERROR(EINVAL);
289
290 ctx->codec_conf.DataSize = sizeof(D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264);
291 ctx->codec_conf.pH264Config = av_mallocz(ctx->codec_conf.DataSize);
292 if (!ctx->codec_conf.pH264Config)
293 return AVERROR(ENOMEM);
294
295 config = ctx->codec_conf.pH264Config;
296
297 config->ConfigurationFlags = D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_FLAG_NONE;
298
299 // Deblocking filter configuration
300 if (priv->deblock) {
301 if (h264_caps.DisableDeblockingFilterSupportedModes & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_SLICES_DEBLOCKING_MODE_FLAG_0_ALL_LUMA_CHROMA_SLICE_BLOCK_EDGES_ALWAYS_FILTERED) {
302 config->DisableDeblockingFilterConfig = D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_SLICES_DEBLOCKING_MODE_0_ALL_LUMA_CHROMA_SLICE_BLOCK_EDGES_ALWAYS_FILTERED;
303 } else {
304 av_log(avctx, AV_LOG_ERROR, "Requested deblocking filter enable mode not supported by driver.\n");
305 return AVERROR(ENOTSUP);
306 }
307 } else {
308 if (h264_caps.DisableDeblockingFilterSupportedModes & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_SLICES_DEBLOCKING_MODE_FLAG_1_DISABLE_ALL_SLICE_BLOCK_EDGES) {
309 config->DisableDeblockingFilterConfig = D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_SLICES_DEBLOCKING_MODE_1_DISABLE_ALL_SLICE_BLOCK_EDGES;
310 } else {
311 av_log(avctx, AV_LOG_ERROR, "Requested deblocking filter disable mode not supported by driver.\n");
312 return AVERROR(ENOTSUP);
313 }
314 }
315
316 // Entropy coder configuration
317 if (priv->unit_opts.cabac) {
318 if (h264_caps.SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_H264_FLAG_CABAC_ENCODING_SUPPORT) {
319 config->ConfigurationFlags |= D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_FLAG_ENABLE_CABAC_ENCODING;
320 } else {
321 av_log(avctx, AV_LOG_WARNING, "CABAC entropy coding is not supported by the driver, falling back to CAVLC.\n");
322 priv->unit_opts.cabac = 0;
323 }
324 }
325
326 // Constrained intra prediction configuration
327 if (priv->constrained_intra_pred) {
328 if (h264_caps.SupportFlags & D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_SUPPORT_H264_FLAG_CONSTRAINED_INTRAPREDICTION_SUPPORT) {
329 config->ConfigurationFlags |= D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION_H264_FLAG_USE_CONSTRAINED_INTRAPREDICTION;
330 } else {
331 av_log(avctx, AV_LOG_WARNING, "Constrained intra prediction is not supported by the driver, disabling.\n");
332 priv->constrained_intra_pred = 0;
333 }
334 }
335
336 base_ctx->surface_width = FFALIGN(avctx->width, 16);
337 base_ctx->surface_height = FFALIGN(avctx->height, 16);
338
339 return 0;
340}
341
343{
344 FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
346 D3D12VAEncodeH264Context *priv = avctx->priv_data;
347 int fixed_qp_idr, fixed_qp_p, fixed_qp_b;
348 int err;
349
350 err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_H264, avctx);
351 if (err < 0)
352 return err;
353
354 // Rate control
355 if (ctx->rc.Mode == D3D12_VIDEO_ENCODER_RATE_CONTROL_MODE_CQP) {
356 D3D12_VIDEO_ENCODER_RATE_CONTROL_CQP *cqp_ctl;
357 fixed_qp_p = av_clip(ctx->rc_quality, 1, 51);
358 if (avctx->i_quant_factor > 0.0)
359 fixed_qp_idr = av_clip((avctx->i_quant_factor * fixed_qp_p +
360 avctx->i_quant_offset) + 0.5, 1, 51);
361 else
362 fixed_qp_idr = fixed_qp_p;
363 if (avctx->b_quant_factor > 0.0)
364 fixed_qp_b = av_clip((avctx->b_quant_factor * fixed_qp_p +
365 avctx->b_quant_offset) + 0.5, 1, 51);
366 else
367 fixed_qp_b = fixed_qp_p;
368
369 av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
370 "%d / %d / %d for IDR- / P- / B-frames.\n",
371 fixed_qp_idr, fixed_qp_p, fixed_qp_b);
372
373 ctx->rc.ConfigParams.DataSize = sizeof(D3D12_VIDEO_ENCODER_RATE_CONTROL_CQP);
374 cqp_ctl = av_mallocz(ctx->rc.ConfigParams.DataSize);
375 if (!cqp_ctl)
376 return AVERROR(ENOMEM);
377
378 cqp_ctl->ConstantQP_FullIntracodedFrame = fixed_qp_idr;
379 cqp_ctl->ConstantQP_InterPredictedFrame_PrevRefOnly = fixed_qp_p;
380 cqp_ctl->ConstantQP_InterPredictedFrame_BiDirectionalRef = fixed_qp_b;
381
382 ctx->rc.ConfigParams.pConfiguration_CQP = cqp_ctl;
383 }
384 priv->unit_opts.fixed_qp_idr = 26;
385
386 // GOP
387 ctx->gop.DataSize = sizeof(D3D12_VIDEO_ENCODER_SEQUENCE_GOP_STRUCTURE_H264);
388 ctx->gop.pH264GroupOfPictures = av_mallocz(ctx->gop.DataSize);
389 if (!ctx->gop.pH264GroupOfPictures)
390 return AVERROR(ENOMEM);
391
392 ctx->gop.pH264GroupOfPictures->GOPLength = base_ctx->gop_size;
393 ctx->gop.pH264GroupOfPictures->PPicturePeriod = base_ctx->b_per_p + 1;
394 ctx->gop.pH264GroupOfPictures->log2_max_frame_num_minus4 = FFMAX(av_ceil_log2(base_ctx->gop_size) - 4, 0);
395
396 return 0;
397}
398
400{
402 int i;
403
404 ctx->level.DataSize = sizeof(D3D12_VIDEO_ENCODER_LEVELS_H264);
405 ctx->level.pH264LevelSetting = av_mallocz(ctx->level.DataSize);
406 if (!ctx->level.pH264LevelSetting)
407 return AVERROR(ENOMEM);
408
409 for (i = 0; i < FF_ARRAY_ELEMS(h264_levels); i++) {
410 if (avctx->level == h264_levels[i].level) {
411 *ctx->level.pH264LevelSetting = h264_levels[i].d3d12_level;
412 break;
413 }
414 }
415
416 if (i == FF_ARRAY_ELEMS(h264_levels)) {
417 av_log(avctx, AV_LOG_ERROR, "Invalid level %d.\n", avctx->level);
418 return AVERROR(EINVAL);
419 }
420
421 return 0;
422}
423
425{
426 if (!pic->pic_ctl.pH264PicData)
427 return;
428
429 av_freep(&pic->pic_ctl.pH264PicData->pList0ReferenceFrames);
430 av_freep(&pic->pic_ctl.pH264PicData->pList1ReferenceFrames);
431 av_freep(&pic->pic_ctl.pH264PicData->pReferenceFramesReconPictureDescriptors);
432 av_freep(&pic->pic_ctl.pH264PicData);
433}
434
436 FFHWBaseEncodePicture *base_pic)
437{
438 FFHWBaseEncodeContext *base_ctx = avctx->priv_data;
440 D3D12VAEncodePicture *pic = base_pic->priv;
441 D3D12VAEncodeH264Picture *hpic = base_pic->codec_priv;
442 FFHWBaseEncodePicture *prev = base_pic->prev;
443 D3D12VAEncodeH264Picture *hprev = prev ? prev->codec_priv : NULL;
444 D3D12_VIDEO_ENCODER_REFERENCE_PICTURE_DESCRIPTOR_H264 *pd = NULL;
445 UINT *ref_list0 = NULL, *ref_list1 = NULL;
446 int i, idx = 0;
447
448 pic->pic_ctl.DataSize = sizeof(D3D12_VIDEO_ENCODER_PICTURE_CONTROL_CODEC_DATA_H264);
449 pic->pic_ctl.pH264PicData = av_mallocz(pic->pic_ctl.DataSize);
450 if (!pic->pic_ctl.pH264PicData)
451 return AVERROR(ENOMEM);
452
453 if (base_pic->type == FF_HW_PICTURE_TYPE_IDR) {
454 av_assert0(base_pic->display_order == base_pic->encode_order);
455 hpic->last_idr_frame = base_pic->display_order;
456 ctx->idr_pic_id++;
457 } else {
458 av_assert0(prev);
459 hpic->last_idr_frame = hprev->last_idr_frame;
460 }
461 hpic->pic_order_cnt = base_pic->display_order - hpic->last_idr_frame;
462
463 switch(base_pic->type) {
465 pic->pic_ctl.pH264PicData->FrameType = D3D12_VIDEO_ENCODER_FRAME_TYPE_H264_IDR_FRAME;
466 pic->pic_ctl.pH264PicData->idr_pic_id = ctx->idr_pic_id;
467 break;
469 pic->pic_ctl.pH264PicData->FrameType = D3D12_VIDEO_ENCODER_FRAME_TYPE_H264_I_FRAME;
470 break;
472 pic->pic_ctl.pH264PicData->FrameType = D3D12_VIDEO_ENCODER_FRAME_TYPE_H264_P_FRAME;
473 break;
475 pic->pic_ctl.pH264PicData->FrameType = D3D12_VIDEO_ENCODER_FRAME_TYPE_H264_B_FRAME;
476 break;
477 default:
478 av_assert0(0 && "invalid picture type");
479 }
480
481 pic->pic_ctl.pH264PicData->PictureOrderCountNumber = hpic->pic_order_cnt;
482 pic->pic_ctl.pH264PicData->FrameDecodingOrderNumber = hpic->pic_order_cnt;
483
484 if (base_pic->type == FF_HW_PICTURE_TYPE_P || base_pic->type == FF_HW_PICTURE_TYPE_B) {
485 pd = av_calloc(MAX_PICTURE_REFERENCES, sizeof(*pd));
486 if (!pd)
487 return AVERROR(ENOMEM);
488
489 ref_list0 = av_calloc(MAX_PICTURE_REFERENCES, sizeof(*ref_list0));
490 if (!ref_list0)
491 return AVERROR(ENOMEM);
492
493 pic->pic_ctl.pH264PicData->List0ReferenceFramesCount = base_pic->nb_refs[0];
494 for (i = 0; i < base_pic->nb_refs[0]; i++) {
495 FFHWBaseEncodePicture *ref = base_pic->refs[0][i];
497
498 av_assert0(ref && ref->encode_order < base_pic->encode_order);
499 href = ref->codec_priv;
500
501 ref_list0[i] = idx;
502 pd[idx].ReconstructedPictureResourceIndex = idx;
503 pd[idx].PictureOrderCountNumber = href->pic_order_cnt;
504 idx++;
505 }
506 }
507
508 if (base_pic->type == FF_HW_PICTURE_TYPE_B) {
509 ref_list1 = av_calloc(MAX_PICTURE_REFERENCES, sizeof(*ref_list1));
510 if (!ref_list1)
511 return AVERROR(ENOMEM);
512
513 pic->pic_ctl.pH264PicData->List1ReferenceFramesCount = base_pic->nb_refs[1];
514 for (i = 0; i < base_pic->nb_refs[1]; i++) {
515 FFHWBaseEncodePicture *ref = base_pic->refs[1][i];
517
518 av_assert0(ref && ref->encode_order < base_pic->encode_order);
519 href = ref->codec_priv;
520
521 ref_list1[i] = idx;
522 pd[idx].ReconstructedPictureResourceIndex = idx;
523 pd[idx].PictureOrderCountNumber = href->pic_order_cnt;
524 idx++;
525 }
526 }
527
528 pic->pic_ctl.pH264PicData->pList0ReferenceFrames = ref_list0;
529 pic->pic_ctl.pH264PicData->pList1ReferenceFrames = ref_list1;
530 pic->pic_ctl.pH264PicData->ReferenceFramesReconPictureDescriptorsCount = idx;
531 pic->pic_ctl.pH264PicData->pReferenceFramesReconPictureDescriptors = pd;
532
533 // Process ROI side data if present and supported
534 if (base_ctx->roi_allowed && pic->qp_map && pic->qp_map_size > 0) {
535 pic->pic_ctl.pH264PicData->QPMapValuesCount = pic->qp_map_size;
536 pic->pic_ctl.pH264PicData->pRateControlQPMap = (INT8 *)pic->qp_map;
537 }
538
539 return 0;
540}
541
544
545 .d3d12_codec = D3D12_VIDEO_ENCODER_CODEC_H264,
546
547 .flags = FF_HW_FLAG_B_PICTURES |
550
551 .default_quality = 25,
552
553 .get_encoder_caps = &d3d12va_encode_h264_get_encoder_caps,
554
555 .configure = &d3d12va_encode_h264_configure,
556
557 .set_level = &d3d12va_encode_h264_set_level,
558
559 .picture_priv_data_size = sizeof(D3D12VAEncodeH264Picture),
560
561 .init_sequence_params = &d3d12va_encode_h264_init_sequence_params,
562
563 .init_picture_params = &d3d12va_encode_h264_init_picture_params,
564
565 .free_picture_params = &d3d12va_encode_h264_free_picture_params,
566
568};
569
571{
573 D3D12VAEncodeH264Context *priv = avctx->priv_data;
574
576
577 if (avctx->profile == AV_PROFILE_UNKNOWN)
578 avctx->profile = priv->profile;
579 if (avctx->level == AV_LEVEL_UNKNOWN)
580 avctx->level = priv->level;
581
582 if (avctx->level != AV_LEVEL_UNKNOWN && avctx->level & ~0xff) {
583 av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
584 "in 8-bit unsigned integer.\n", avctx->level);
585 return AVERROR(EINVAL);
586 }
587
588 if (priv->qp > 0)
589 ctx->explicit_qp = priv->qp;
590
591 return ff_d3d12va_encode_init(avctx);
592}
593
595{
596 D3D12VAEncodeH264Context *priv = avctx->priv_data;
597
598 ff_cbs_fragment_free(&priv->current_access_unit);
599 ff_cbs_close(&priv->cbc);
600
601 av_freep(&priv->common.codec_conf.pH264Config);
602 av_freep(&priv->common.gop.pH264GroupOfPictures);
603 av_freep(&priv->common.level.pH264LevelSetting);
604
605 if (priv->common.rc.ConfigParams.pConfiguration_CQP != NULL) {
606 av_freep(&priv->common.rc.ConfigParams.pConfiguration_CQP);
607 }
608
609 return ff_d3d12va_encode_close(avctx);
610}
611
612#define OFFSET(x) offsetof(D3D12VAEncodeH264Context, x)
613#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
618
619 { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
620 OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, FLAGS },
621
622 { "profile", "Set profile (general_profile_idc)",
624 { .i64 = AV_PROFILE_UNKNOWN }, AV_PROFILE_UNKNOWN, 0xff, FLAGS, "profile" },
625
626#define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
627 { .i64 = value }, 0, 0, FLAGS, "profile"
628 { PROFILE("main", AV_PROFILE_H264_MAIN) },
629 { PROFILE("high", AV_PROFILE_H264_HIGH) },
630 { PROFILE("high10", AV_PROFILE_H264_HIGH_10) },
631#undef PROFILE
632
633 { "level", "Set level (general_level_idc)",
635 { .i64 = AV_LEVEL_UNKNOWN }, AV_LEVEL_UNKNOWN, 0xff, FLAGS, "level" },
636
637#define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
638 { .i64 = value }, 0, 0, FLAGS, "level"
639 { LEVEL("1", 10) },
640 { LEVEL("1.1", 11) },
641 { LEVEL("1.2", 12) },
642 { LEVEL("1.3", 13) },
643 { LEVEL("2", 20) },
644 { LEVEL("2.1", 21) },
645 { LEVEL("2.2", 22) },
646 { LEVEL("3", 30) },
647 { LEVEL("3.1", 31) },
648 { LEVEL("3.2", 32) },
649 { LEVEL("4", 40) },
650 { LEVEL("4.1", 41) },
651 { LEVEL("4.2", 42) },
652 { LEVEL("5", 50) },
653 { LEVEL("5.1", 51) },
654 { LEVEL("5.2", 52) },
655 { LEVEL("6", 60) },
656 { LEVEL("6.1", 61) },
657 { LEVEL("6.2", 62) },
658#undef LEVEL
659
660 { "deblock", "Deblocking filter mode",
661 OFFSET(deblock), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
662
663 { "coder", "Entropy coder type",
664 OFFSET(unit_opts.cabac), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "coder" },
665 { "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
666 { "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
667
668 { "constrained_intra_pred", "Constrained intra prediction (constrained_intra_pred_flag)",
669 OFFSET(constrained_intra_pred), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
670
671 { NULL },
672};
673
675 { "b", "0" },
676 { "bf", "2" },
677 { "g", "120" },
678 { "qmin", "-1" },
679 { "qmax", "-1" },
680 { NULL },
681};
682
684 .class_name = "h264_d3d12va",
685 .item_name = av_default_item_name,
687 .version = LIBAVUTIL_VERSION_INT,
688};
689
691 .p.name = "h264_d3d12va",
692 CODEC_LONG_NAME("D3D12VA h264 encoder"),
693 .p.type = AVMEDIA_TYPE_VIDEO,
694 .p.id = AV_CODEC_ID_H264,
695 .priv_data_size = sizeof(D3D12VAEncodeH264Context),
699 .p.priv_class = &d3d12va_encode_h264_class,
700 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE |
702 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
706 .hw_configs = ff_d3d12va_encode_hw_configs,
707 .p.wrapper_name = "d3d12va",
708};
const FFCodec ff_h264_d3d12va_encoder
static AVFormatContext * ctx
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
static int FUNC sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
#define FLAGS
Definition cmdutils.c:598
#define CODEC_PIXFMTS(...)
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
#define FF_CODEC_RECEIVE_PACKET_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
common internal and external API header
#define av_clip
Definition common.h:100
#define av_ceil_log2
Definition common.h:97
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int ff_d3d12va_encode_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
void ff_d3d12va_encode_check_encoder_feature_flags(void *log_ctx, D3D12_VIDEO_ENCODER_VALIDATION_FLAGS flags)
const AVCodecHWConfigInternal *const ff_d3d12va_encode_hw_configs[]
int ff_d3d12va_encode_init(AVCodecContext *avctx)
int ff_d3d12va_encode_close(AVCodecContext *avctx)
#define D3D12VA_ENCODE_RC_OPTIONS
#define D3D12VA_ENCODE_COMMON_OPTIONS
static const D3D12_VIDEO_ENCODER_AV1_PROFILE profile_main
static const D3D12_VIDEO_ENCODER_AV1_PROFILE profile_high
#define D3D_PROFILE_DESC(name)
static const AVOption d3d12va_encode_h264_options[]
static const D3D12VAEncodeH264Level h264_levels[]
static void d3d12va_encode_h264_free_picture_params(D3D12VAEncodePicture *pic)
static int d3d12va_encode_h264_close(AVCodecContext *avctx)
static int d3d12va_encode_h264_add_nal(AVCodecContext *avctx, CodedBitstreamFragment *au, void *nal_unit)
static int d3d12va_encode_h264_configure(AVCodecContext *avctx)
static const D3D12_VIDEO_ENCODER_PROFILE_H264 profile_high_10
static int d3d12va_encode_h264_get_encoder_caps(AVCodecContext *avctx)
#define LEVEL(name, value)
static const D3D12VAEncodeType d3d12va_encode_type_h264
static int d3d12va_encode_h264_init_picture_params(AVCodecContext *avctx, FFHWBaseEncodePicture *base_pic)
static int d3d12va_encode_h264_write_sequence_header(AVCodecContext *avctx, char *data, size_t *data_len)
static const AVClass d3d12va_encode_h264_class
#define PROFILE(name, value)
#define OFFSET(x)
static const FFCodecDefault d3d12va_encode_h264_defaults[]
static int d3d12va_encode_h264_write_access_unit(AVCodecContext *avctx, char *data, size_t *data_len, CodedBitstreamFragment *au)
static int d3d12va_encode_h264_set_level(AVCodecContext *avctx)
static int d3d12va_encode_h264_init(AVCodecContext *avctx)
static const D3D12VAEncodeProfile d3d12va_encode_h264_profiles[]
static int d3d12va_encode_h264_init_sequence_params(AVCodecContext *avctx)
#define AV_PROFILE_H264_MAIN
Definition defs.h:112
#define AV_PROFILE_UNKNOWN
Definition defs.h:65
#define AV_LEVEL_UNKNOWN
Definition defs.h:209
#define AV_PROFILE_H264_HIGH
Definition defs.h:114
#define AV_PROFILE_H264_HIGH_10
Definition defs.h:115
uint64_t pps
Definition dovi_rpuenc.c:36
int high
Definition dovi_rpuenc.c:39
int main
Definition dovi_rpuenc.c:38
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define fail
Definition test.h:479
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition codec.h:147
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_HARDWARE
Codec is backed by a hardware implementation.
Definition codec.h:133
@ AV_CODEC_ID_H264
Definition codec_id.h:77
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define MAX_PICTURE_REFERENCES
#define HW_BASE_ENCODE_COMMON_OPTIONS
#define MAX_DPB_SIZE
@ FF_HW_FLAG_NON_IDR_KEY_PICTURES
@ FF_HW_FLAG_B_PICTURE_REFERENCES
@ FF_HW_FLAG_B_PICTURES
@ FF_HW_PICTURE_TYPE_P
@ FF_HW_PICTURE_TYPE_I
@ FF_HW_PICTURE_TYPE_B
@ FF_HW_PICTURE_TYPE_IDR
int ff_hw_base_encode_init_params_h264(FFHWBaseEncodeContext *base_ctx, AVCodecContext *avctx, FFHWBaseEncodeH264 *common, FFHWBaseEncodeH264Opts *opts)
const char * desc
Definition libsvtav1.c:83
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
const char data[16]
Definition mxf.c:149
int profile
Definition mxfenc.c:2299
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
@ AV_PIX_FMT_D3D12
Hardware surfaces for Direct3D 12.
Definition pixfmt.h:440
static void deblock(const RV60Context *s, AVFrame *frame, int xpos, int ypos, int size, int dpos)
Definition rv60dec.c:2154
static const uint8_t header[24]
Definition sdr2.c:68
#define FF_ARRAY_ELEMS(a)
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
int width
picture width / height.
Definition avcodec.h:604
float b_quant_offset
qscale offset between IP and B-frames
Definition avcodec.h:797
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
Definition avcodec.h:790
int level
Encoding level descriptor.
Definition avcodec.h:1646
int profile
profile
Definition avcodec.h:1636
float i_quant_factor
qscale factor between P- and I-frames If > 0 then the last P-frame quantizer will be used (q = lastp_...
Definition avcodec.h:806
void * priv_data
Definition avcodec.h:470
float i_quant_offset
qscale offset between P and I-frames
Definition avcodec.h:813
This struct is allocated as AVHWFramesContext.hwctx.
DXGI_FORMAT format
DXGI_FORMAT format.
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition hwcontext.h:153
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition hwcontext.h:213
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
Context structure for coded bitstream operations.
Definition cbs.h:226
Coded bitstream fragment structure, combining one or more units.
Definition cbs.h:129
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
Definition cbs.h:146
size_t data_size
The number of bytes in the bitstream.
Definition cbs.h:142
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition cbs.h:135
D3D12_VIDEO_ENCODER_CODEC_CONFIGURATION codec_conf
D3D12_VIDEO_ENCODER_SEQUENCE_GOP_STRUCTURE gop
D3D12_VIDEO_ENCODER_RATE_CONTROL rc
D3D12_VIDEO_ENCODER_LEVEL_SETTING level
FFHWBaseEncodeH264Opts unit_opts
CodedBitstreamContext * cbc
D3D12VAEncodeContext common
CodedBitstreamFragment current_access_unit
D3D12_VIDEO_ENCODER_LEVELS_H264 d3d12_level
D3D12_VIDEO_ENCODER_PICTURE_CONTROL_CODEC_DATA pic_ctl
AVHWFramesContext * input_frames
struct FFHWBaseEncodePicture * prev
int nb_refs[MAX_REFERENCE_LIST_NUM]
struct FFHWBaseEncodePicture * refs[MAX_REFERENCE_LIST_NUM][MAX_PICTURE_REFERENCES]
uint8_t level_idc
Definition cbs_h264.h:113
uint8_t level
Definition svq3.c:208
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static int ref[MAX_W *MAX_W]
static int write_sequence_header(AVCodecContext *avctx, FFHWBaseEncodePicture *base_pic, uint8_t *data, size_t *data_len)