2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <va/va_enc_h264.h>
24 #include "libavutil/avassert.h"
25 #include "libavutil/common.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/opt.h"
33 #include "h264_levels.h"
36 #include "vaapi_encode.h"
40 SEI_IDENTIFIER = 0x02,
41 SEI_RECOVERY_POINT = 0x04,
44 // Random (version 4) ISO 11578 UUID.
45 static const uint8_t vaapi_encode_h264_sei_identifier_uuid[16] = {
46 0x59, 0x94, 0x8b, 0x28, 0x11, 0xec, 0x45, 0xaf,
47 0x96, 0x75, 0x19, 0xd4, 0x1f, 0xea, 0xa9, 0x4d,
50 typedef struct VAAPIEncodeH264Context {
51 VAAPIEncodeContext common;
74 int64_t last_idr_frame;
75 int64_t idr_pic_count;
84 CodedBitstreamContext *cbc;
85 CodedBitstreamFragment current_access_unit;
91 H264RawSlice raw_slice;
93 H264RawSEIBufferingPeriod sei_buffering_period;
94 H264RawSEIPicTiming sei_pic_timing;
95 H264RawSEIRecoveryPoint sei_recovery_point;
96 H264RawSEIUserDataUnregistered sei_identifier;
97 char *sei_identifier_string;
101 int sei_cbr_workaround_needed;
102 } VAAPIEncodeH264Context;
105 static int vaapi_encode_h264_write_access_unit(AVCodecContext *avctx,
106 char *data, size_t *data_len,
107 CodedBitstreamFragment *au)
109 VAAPIEncodeH264Context *priv = avctx->priv_data;
112 err = ff_cbs_write_fragment_data(priv->cbc, au);
114 av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
118 if (*data_len < 8 * au->data_size - au->data_bit_padding) {
119 av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
120 "%zu < %zu.\n", *data_len,
121 8 * au->data_size - au->data_bit_padding);
122 return AVERROR(ENOSPC);
125 memcpy(data, au->data, au->data_size);
126 *data_len = 8 * au->data_size - au->data_bit_padding;
131 static int vaapi_encode_h264_add_nal(AVCodecContext *avctx,
132 CodedBitstreamFragment *au,
135 VAAPIEncodeH264Context *priv = avctx->priv_data;
136 H264RawNALUnitHeader *header = nal_unit;
139 err = ff_cbs_insert_unit_content(priv->cbc, au, -1,
140 header->nal_unit_type, nal_unit, NULL);
142 av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
143 "type = %d.\n", header->nal_unit_type);
150 static int vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx,
151 char *data, size_t *data_len)
153 VAAPIEncodeH264Context *priv = avctx->priv_data;
154 CodedBitstreamFragment *au = &priv->current_access_unit;
157 if (priv->aud_needed) {
158 err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
161 priv->aud_needed = 0;
164 err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_sps);
168 err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_pps);
172 err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
174 ff_cbs_fragment_uninit(priv->cbc, au);
178 static int vaapi_encode_h264_write_slice_header(AVCodecContext *avctx,
179 VAAPIEncodePicture *pic,
180 VAAPIEncodeSlice *slice,
181 char *data, size_t *data_len)
183 VAAPIEncodeH264Context *priv = avctx->priv_data;
184 CodedBitstreamFragment *au = &priv->current_access_unit;
187 if (priv->aud_needed) {
188 err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
191 priv->aud_needed = 0;
194 err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_slice);
198 err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
200 ff_cbs_fragment_uninit(priv->cbc, au);
204 static int vaapi_encode_h264_write_extra_header(AVCodecContext *avctx,
205 VAAPIEncodePicture *pic,
206 int index, int *type,
207 char *data, size_t *data_len)
209 VAAPIEncodeH264Context *priv = avctx->priv_data;
210 CodedBitstreamFragment *au = &priv->current_access_unit;
213 if (priv->sei_needed) {
214 H264RawSEI *sei = &priv->raw_sei;
216 if (priv->aud_needed) {
217 err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
220 priv->aud_needed = 0;
223 *sei = (H264RawSEI) {
225 .nal_unit_type = H264_NAL_SEI,
231 if (priv->sei_needed & SEI_IDENTIFIER) {
232 sei->payload[i].payload_type = H264_SEI_TYPE_USER_DATA_UNREGISTERED;
233 sei->payload[i].payload.user_data_unregistered = priv->sei_identifier;
236 if (priv->sei_needed & SEI_TIMING) {
237 if (pic->type == PICTURE_TYPE_IDR) {
238 sei->payload[i].payload_type = H264_SEI_TYPE_BUFFERING_PERIOD;
239 sei->payload[i].payload.buffering_period = priv->sei_buffering_period;
242 sei->payload[i].payload_type = H264_SEI_TYPE_PIC_TIMING;
243 sei->payload[i].payload.pic_timing = priv->sei_pic_timing;
246 if (priv->sei_needed & SEI_RECOVERY_POINT) {
247 sei->payload[i].payload_type = H264_SEI_TYPE_RECOVERY_POINT;
248 sei->payload[i].payload.recovery_point = priv->sei_recovery_point;
252 sei->payload_count = i;
253 av_assert0(sei->payload_count > 0);
255 err = vaapi_encode_h264_add_nal(avctx, au, sei);
258 priv->sei_needed = 0;
260 err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
264 ff_cbs_fragment_uninit(priv->cbc, au);
266 *type = VAEncPackedHeaderRawData;
270 } else if (priv->sei_cbr_workaround_needed) {
271 // Insert a zero-length header using the old SEI type. This is
272 // required to avoid triggering broken behaviour on Intel platforms
273 // in CBR mode where an invalid SEI message is generated by the
274 // driver and inserted into the stream.
276 *type = VAEncPackedHeaderH264_SEI;
277 priv->sei_cbr_workaround_needed = 0;
286 ff_cbs_fragment_uninit(priv->cbc, au);
290 static int vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx)
292 VAAPIEncodeContext *ctx = avctx->priv_data;
293 VAAPIEncodeH264Context *priv = avctx->priv_data;
294 H264RawSPS *sps = &priv->raw_sps;
295 H264RawPPS *pps = &priv->raw_pps;
296 VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
297 VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params;
300 memset(&priv->current_access_unit, 0,
301 sizeof(priv->current_access_unit));
303 memset(sps, 0, sizeof(*sps));
304 memset(pps, 0, sizeof(*pps));
306 sps->nal_unit_header.nal_ref_idc = 3;
307 sps->nal_unit_header.nal_unit_type = H264_NAL_SPS;
309 sps->profile_idc = avctx->profile & 0xff;
311 if (avctx->profile == FF_PROFILE_H264_CONSTRAINED_BASELINE ||
312 avctx->profile == FF_PROFILE_H264_MAIN)
313 sps->constraint_set1_flag = 1;
315 if (avctx->profile == FF_PROFILE_H264_HIGH)
316 sps->constraint_set3_flag = ctx->gop_size == 1;
318 if (avctx->profile == FF_PROFILE_H264_MAIN ||
319 avctx->profile == FF_PROFILE_H264_HIGH) {
320 sps->constraint_set4_flag = 1;
321 sps->constraint_set5_flag = ctx->b_per_p == 0;
324 if (ctx->gop_size == 1)
327 dpb_frames = 1 + (ctx->b_per_p > 0);
329 if (avctx->level != FF_LEVEL_UNKNOWN) {
330 sps->level_idc = avctx->level;
332 const H264LevelDescriptor *level;
334 level = ff_h264_guess_level(sps->profile_idc,
337 priv->mb_height * 16,
340 av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name);
341 if (level->constraint_set3_flag)
342 sps->constraint_set3_flag = 1;
343 sps->level_idc = level->level_idc;
345 av_log(avctx, AV_LOG_WARNING, "Stream will not conform "
346 "to any level: using level 6.2.\n");
351 sps->seq_parameter_set_id = 0;
352 sps->chroma_format_idc = 1;
354 sps->log2_max_frame_num_minus4 = 4;
355 sps->pic_order_cnt_type = 0;
356 sps->log2_max_pic_order_cnt_lsb_minus4 =
357 av_clip(av_log2(ctx->b_per_p + 1) - 2, 0, 12);
359 sps->max_num_ref_frames = dpb_frames;
361 sps->pic_width_in_mbs_minus1 = priv->mb_width - 1;
362 sps->pic_height_in_map_units_minus1 = priv->mb_height - 1;
364 sps->frame_mbs_only_flag = 1;
365 sps->direct_8x8_inference_flag = 1;
367 if (avctx->width != 16 * priv->mb_width ||
368 avctx->height != 16 * priv->mb_height) {
369 sps->frame_cropping_flag = 1;
371 sps->frame_crop_left_offset = 0;
372 sps->frame_crop_right_offset =
373 (16 * priv->mb_width - avctx->width) / 2;
374 sps->frame_crop_top_offset = 0;
375 sps->frame_crop_bottom_offset =
376 (16 * priv->mb_height - avctx->height) / 2;
378 sps->frame_cropping_flag = 0;
381 sps->vui_parameters_present_flag = 1;
383 if (avctx->sample_aspect_ratio.num != 0 &&
384 avctx->sample_aspect_ratio.den != 0) {
385 static const AVRational sar_idc[] = {
387 { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
388 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
389 { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
390 { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
393 av_reduce(&num, &den, avctx->sample_aspect_ratio.num,
394 avctx->sample_aspect_ratio.den, 65535);
395 for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
396 if (num == sar_idc[i].num &&
397 den == sar_idc[i].den) {
398 sps->vui.aspect_ratio_idc = i;
402 if (i >= FF_ARRAY_ELEMS(sar_idc)) {
403 sps->vui.aspect_ratio_idc = 255;
404 sps->vui.sar_width = num;
405 sps->vui.sar_height = den;
407 sps->vui.aspect_ratio_info_present_flag = 1;
410 if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
411 avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
412 avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
413 avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
414 sps->vui.video_signal_type_present_flag = 1;
415 sps->vui.video_format = 5; // Unspecified.
416 sps->vui.video_full_range_flag =
417 avctx->color_range == AVCOL_RANGE_JPEG;
419 if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
420 avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
421 avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
422 sps->vui.colour_description_present_flag = 1;
423 sps->vui.colour_primaries = avctx->color_primaries;
424 sps->vui.transfer_characteristics = avctx->color_trc;
425 sps->vui.matrix_coefficients = avctx->colorspace;
428 sps->vui.video_format = 5;
429 sps->vui.video_full_range_flag = 0;
430 sps->vui.colour_primaries = avctx->color_primaries;
431 sps->vui.transfer_characteristics = avctx->color_trc;
432 sps->vui.matrix_coefficients = avctx->colorspace;
435 if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
436 sps->vui.chroma_loc_info_present_flag = 1;
437 sps->vui.chroma_sample_loc_type_top_field =
438 sps->vui.chroma_sample_loc_type_bottom_field =
439 avctx->chroma_sample_location - 1;
442 sps->vui.timing_info_present_flag = 1;
443 if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
444 sps->vui.num_units_in_tick = avctx->framerate.den;
445 sps->vui.time_scale = 2 * avctx->framerate.num;
446 sps->vui.fixed_frame_rate_flag = 1;
448 sps->vui.num_units_in_tick = avctx->time_base.num;
449 sps->vui.time_scale = 2 * avctx->time_base.den;
450 sps->vui.fixed_frame_rate_flag = 0;
453 if (priv->sei & SEI_TIMING) {
454 H264RawHRD *hrd = &sps->vui.nal_hrd_parameters;
455 H264RawSEIBufferingPeriod *bp = &priv->sei_buffering_period;
457 sps->vui.nal_hrd_parameters_present_flag = 1;
459 hrd->cpb_cnt_minus1 = 0;
461 // Try to scale these to a sensible range so that the
462 // golomb encode of the value is not overlong.
463 hrd->bit_rate_scale =
464 av_clip_uintp2(av_log2(ctx->va_bit_rate) - 15 - 6, 4);
465 hrd->bit_rate_value_minus1[0] =
466 (ctx->va_bit_rate >> hrd->bit_rate_scale + 6) - 1;
468 hrd->cpb_size_scale =
469 av_clip_uintp2(av_log2(ctx->hrd_params.hrd.buffer_size) - 15 - 4, 4);
470 hrd->cpb_size_value_minus1[0] =
471 (ctx->hrd_params.hrd.buffer_size >> hrd->cpb_size_scale + 4) - 1;
473 // CBR mode as defined for the HRD cannot be achieved without filler
474 // data, so this flag cannot be set even with VAAPI CBR modes.
475 hrd->cbr_flag[0] = 0;
477 hrd->initial_cpb_removal_delay_length_minus1 = 23;
478 hrd->cpb_removal_delay_length_minus1 = 23;
479 hrd->dpb_output_delay_length_minus1 = 7;
480 hrd->time_offset_length = 0;
482 bp->seq_parameter_set_id = sps->seq_parameter_set_id;
484 // This calculation can easily overflow 32 bits.
485 bp->nal.initial_cpb_removal_delay[0] = 90000 *
486 (uint64_t)ctx->hrd_params.hrd.initial_buffer_fullness /
487 ctx->hrd_params.hrd.buffer_size;
488 bp->nal.initial_cpb_removal_delay_offset[0] = 0;
490 sps->vui.nal_hrd_parameters_present_flag = 0;
491 sps->vui.low_delay_hrd_flag = 1 - sps->vui.fixed_frame_rate_flag;
494 sps->vui.bitstream_restriction_flag = 1;
495 sps->vui.motion_vectors_over_pic_boundaries_flag = 1;
496 sps->vui.log2_max_mv_length_horizontal = 15;
497 sps->vui.log2_max_mv_length_vertical = 15;
498 sps->vui.max_num_reorder_frames = (ctx->b_per_p > 0);
499 sps->vui.max_dec_frame_buffering = sps->max_num_ref_frames;
501 pps->nal_unit_header.nal_ref_idc = 3;
502 pps->nal_unit_header.nal_unit_type = H264_NAL_PPS;
504 pps->pic_parameter_set_id = 0;
505 pps->seq_parameter_set_id = 0;
507 pps->entropy_coding_mode_flag =
508 !(sps->profile_idc == FF_PROFILE_H264_BASELINE ||
509 sps->profile_idc == FF_PROFILE_H264_EXTENDED ||
510 sps->profile_idc == FF_PROFILE_H264_CAVLC_444);
511 if (!priv->coder && pps->entropy_coding_mode_flag)
512 pps->entropy_coding_mode_flag = 0;
514 pps->num_ref_idx_l0_default_active_minus1 = 0;
515 pps->num_ref_idx_l1_default_active_minus1 = 0;
517 pps->pic_init_qp_minus26 = priv->fixed_qp_idr - 26;
519 if (sps->profile_idc == FF_PROFILE_H264_BASELINE ||
520 sps->profile_idc == FF_PROFILE_H264_EXTENDED ||
521 sps->profile_idc == FF_PROFILE_H264_MAIN) {
522 pps->more_rbsp_data = 0;
524 pps->more_rbsp_data = 1;
526 pps->transform_8x8_mode_flag = 1;
529 *vseq = (VAEncSequenceParameterBufferH264) {
530 .seq_parameter_set_id = sps->seq_parameter_set_id,
531 .level_idc = sps->level_idc,
532 .intra_period = ctx->gop_size,
533 .intra_idr_period = ctx->gop_size,
534 .ip_period = ctx->b_per_p + 1,
536 .bits_per_second = ctx->va_bit_rate,
537 .max_num_ref_frames = sps->max_num_ref_frames,
538 .picture_width_in_mbs = sps->pic_width_in_mbs_minus1 + 1,
539 .picture_height_in_mbs = sps->pic_height_in_map_units_minus1 + 1,
542 .chroma_format_idc = sps->chroma_format_idc,
543 .frame_mbs_only_flag = sps->frame_mbs_only_flag,
544 .mb_adaptive_frame_field_flag = sps->mb_adaptive_frame_field_flag,
545 .seq_scaling_matrix_present_flag = sps->seq_scaling_matrix_present_flag,
546 .direct_8x8_inference_flag = sps->direct_8x8_inference_flag,
547 .log2_max_frame_num_minus4 = sps->log2_max_frame_num_minus4,
548 .pic_order_cnt_type = sps->pic_order_cnt_type,
549 .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_pic_order_cnt_lsb_minus4,
550 .delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag,
553 .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
554 .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
556 .frame_cropping_flag = sps->frame_cropping_flag,
557 .frame_crop_left_offset = sps->frame_crop_left_offset,
558 .frame_crop_right_offset = sps->frame_crop_right_offset,
559 .frame_crop_top_offset = sps->frame_crop_top_offset,
560 .frame_crop_bottom_offset = sps->frame_crop_bottom_offset,
562 .vui_parameters_present_flag = sps->vui_parameters_present_flag,
565 .aspect_ratio_info_present_flag = sps->vui.aspect_ratio_info_present_flag,
566 .timing_info_present_flag = sps->vui.timing_info_present_flag,
567 .bitstream_restriction_flag = sps->vui.bitstream_restriction_flag,
568 .log2_max_mv_length_horizontal = sps->vui.log2_max_mv_length_horizontal,
569 .log2_max_mv_length_vertical = sps->vui.log2_max_mv_length_vertical,
572 .aspect_ratio_idc = sps->vui.aspect_ratio_idc,
573 .sar_width = sps->vui.sar_width,
574 .sar_height = sps->vui.sar_height,
575 .num_units_in_tick = sps->vui.num_units_in_tick,
576 .time_scale = sps->vui.time_scale,
579 *vpic = (VAEncPictureParameterBufferH264) {
581 .picture_id = VA_INVALID_ID,
582 .flags = VA_PICTURE_H264_INVALID,
585 .coded_buf = VA_INVALID_ID,
587 .pic_parameter_set_id = pps->pic_parameter_set_id,
588 .seq_parameter_set_id = pps->seq_parameter_set_id,
590 .pic_init_qp = pps->pic_init_qp_minus26 + 26,
591 .num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1,
592 .num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1,
594 .chroma_qp_index_offset = pps->chroma_qp_index_offset,
595 .second_chroma_qp_index_offset = pps->second_chroma_qp_index_offset,
598 .entropy_coding_mode_flag = pps->entropy_coding_mode_flag,
599 .weighted_pred_flag = pps->weighted_pred_flag,
600 .weighted_bipred_idc = pps->weighted_bipred_idc,
601 .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
602 .transform_8x8_mode_flag = pps->transform_8x8_mode_flag,
603 .deblocking_filter_control_present_flag =
604 pps->deblocking_filter_control_present_flag,
605 .redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present_flag,
606 .pic_order_present_flag =
607 pps->bottom_field_pic_order_in_frame_present_flag,
608 .pic_scaling_matrix_present_flag = pps->pic_scaling_matrix_present_flag,
615 static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
616 VAAPIEncodePicture *pic)
618 VAAPIEncodeContext *ctx = avctx->priv_data;
619 VAAPIEncodeH264Context *priv = avctx->priv_data;
620 H264RawSPS *sps = &priv->raw_sps;
621 VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
624 memset(&priv->current_access_unit, 0,
625 sizeof(priv->current_access_unit));
627 if (pic->type == PICTURE_TYPE_IDR) {
628 av_assert0(pic->display_order == pic->encode_order);
630 priv->next_frame_num = 1;
632 priv->last_idr_frame = pic->display_order;
633 ++priv->idr_pic_count;
635 priv->slice_type = 7;
636 priv->primary_pic_type = 0;
638 priv->frame_num = priv->next_frame_num;
640 if (pic->type != PICTURE_TYPE_B) {
641 // Reference picture, so frame_num advances.
642 priv->next_frame_num = (priv->frame_num + 1) &
643 ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
647 if (pic->type == PICTURE_TYPE_I) {
648 priv->slice_type = 7;
649 priv->primary_pic_type = 0;
650 } else if (pic->type == PICTURE_TYPE_P) {
651 priv->slice_type = 5;
652 priv->primary_pic_type = 1;
654 priv->slice_type = 6;
655 priv->primary_pic_type = 2;
658 priv->pic_order_cnt = pic->display_order - priv->last_idr_frame;
659 priv->dpb_delay = pic->display_order - pic->encode_order + 1;
662 priv->aud_needed = 1;
663 priv->raw_aud = (H264RawAUD) {
665 .nal_unit_type = H264_NAL_AUD,
667 .primary_pic_type = priv->primary_pic_type,
670 priv->aud_needed = 0;
673 priv->sei_needed = 0;
675 if (priv->sei & SEI_IDENTIFIER && pic->encode_order == 0)
676 priv->sei_needed |= SEI_IDENTIFIER;
678 if (ctx->va_rc_mode == VA_RC_CBR)
679 priv->sei_cbr_workaround_needed = 1;
682 if (priv->sei & SEI_TIMING) {
683 priv->sei_pic_timing = (H264RawSEIPicTiming) {
684 .cpb_removal_delay = 2 * priv->cpb_delay,
685 .dpb_output_delay = 2 * priv->dpb_delay,
688 priv->sei_needed |= SEI_TIMING;
691 if (priv->sei & SEI_RECOVERY_POINT && pic->type == PICTURE_TYPE_I) {
692 priv->sei_recovery_point = (H264RawSEIRecoveryPoint) {
693 .recovery_frame_cnt = 0,
694 .exact_match_flag = 1,
695 .broken_link_flag = ctx->b_per_p > 0,
698 priv->sei_needed |= SEI_RECOVERY_POINT;
701 vpic->CurrPic = (VAPictureH264) {
702 .picture_id = pic->recon_surface,
703 .frame_idx = priv->frame_num,
705 .TopFieldOrderCnt = priv->pic_order_cnt,
706 .BottomFieldOrderCnt = priv->pic_order_cnt,
709 for (i = 0; i < pic->nb_refs; i++) {
710 VAAPIEncodePicture *ref = pic->refs[i];
711 unsigned int frame_num = (ref->encode_order - priv->last_idr_frame) &
712 ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
713 unsigned int pic_order_cnt = ref->display_order - priv->last_idr_frame;
715 av_assert0(ref && ref->encode_order < pic->encode_order);
716 vpic->ReferenceFrames[i] = (VAPictureH264) {
717 .picture_id = ref->recon_surface,
718 .frame_idx = frame_num,
719 .flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE,
720 .TopFieldOrderCnt = pic_order_cnt,
721 .BottomFieldOrderCnt = pic_order_cnt,
724 for (; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
725 vpic->ReferenceFrames[i] = (VAPictureH264) {
726 .picture_id = VA_INVALID_ID,
727 .flags = VA_PICTURE_H264_INVALID,
731 vpic->coded_buf = pic->output_buffer;
733 vpic->frame_num = priv->frame_num;
735 vpic->pic_fields.bits.idr_pic_flag = (pic->type == PICTURE_TYPE_IDR);
736 vpic->pic_fields.bits.reference_pic_flag = (pic->type != PICTURE_TYPE_B);
741 static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx,
742 VAAPIEncodePicture *pic,
743 VAAPIEncodeSlice *slice)
745 VAAPIEncodeH264Context *priv = avctx->priv_data;
746 H264RawSPS *sps = &priv->raw_sps;
747 H264RawPPS *pps = &priv->raw_pps;
748 H264RawSliceHeader *sh = &priv->raw_slice.header;
749 VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
750 VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params;
753 if (pic->type == PICTURE_TYPE_IDR) {
754 sh->nal_unit_header.nal_unit_type = H264_NAL_IDR_SLICE;
755 sh->nal_unit_header.nal_ref_idc = 3;
757 sh->nal_unit_header.nal_unit_type = H264_NAL_SLICE;
758 sh->nal_unit_header.nal_ref_idc = pic->type != PICTURE_TYPE_B;
761 sh->first_mb_in_slice = slice->block_start;
762 sh->slice_type = priv->slice_type;
764 sh->pic_parameter_set_id = pps->pic_parameter_set_id;
766 sh->frame_num = priv->frame_num;
767 sh->idr_pic_id = priv->idr_pic_count;
769 sh->pic_order_cnt_lsb = priv->pic_order_cnt &
770 ((1 << (4 + sps->log2_max_pic_order_cnt_lsb_minus4)) - 1);
772 sh->direct_spatial_mv_pred_flag = 1;
774 if (pic->type == PICTURE_TYPE_B)
775 sh->slice_qp_delta = priv->fixed_qp_b - (pps->pic_init_qp_minus26 + 26);
776 else if (pic->type == PICTURE_TYPE_P)
777 sh->slice_qp_delta = priv->fixed_qp_p - (pps->pic_init_qp_minus26 + 26);
779 sh->slice_qp_delta = priv->fixed_qp_idr - (pps->pic_init_qp_minus26 + 26);
782 vslice->macroblock_address = slice->block_start;
783 vslice->num_macroblocks = slice->block_size;
785 vslice->macroblock_info = VA_INVALID_ID;
787 vslice->slice_type = sh->slice_type % 5;
788 vslice->pic_parameter_set_id = sh->pic_parameter_set_id;
789 vslice->idr_pic_id = sh->idr_pic_id;
791 vslice->pic_order_cnt_lsb = sh->pic_order_cnt_lsb;
793 vslice->direct_spatial_mv_pred_flag = sh->direct_spatial_mv_pred_flag;
795 for (i = 0; i < FF_ARRAY_ELEMS(vslice->RefPicList0); i++) {
796 vslice->RefPicList0[i].picture_id = VA_INVALID_ID;
797 vslice->RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
798 vslice->RefPicList1[i].picture_id = VA_INVALID_ID;
799 vslice->RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
802 av_assert0(pic->nb_refs <= 2);
803 if (pic->nb_refs >= 1) {
804 // Backward reference for P- or B-frame.
805 av_assert0(pic->type == PICTURE_TYPE_P ||
806 pic->type == PICTURE_TYPE_B);
807 vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
809 if (pic->nb_refs >= 2) {
810 // Forward reference for B-frame.
811 av_assert0(pic->type == PICTURE_TYPE_B);
812 vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
815 vslice->slice_qp_delta = sh->slice_qp_delta;
820 static av_cold int vaapi_encode_h264_configure(AVCodecContext *avctx)
822 VAAPIEncodeContext *ctx = avctx->priv_data;
823 VAAPIEncodeH264Context *priv = avctx->priv_data;
826 err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_H264, avctx);
830 priv->mb_width = FFALIGN(avctx->width, 16) / 16;
831 priv->mb_height = FFALIGN(avctx->height, 16) / 16;
833 if (ctx->va_rc_mode == VA_RC_CQP) {
834 priv->fixed_qp_p = priv->qp;
835 if (avctx->i_quant_factor > 0.0)
836 priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
837 avctx->i_quant_offset) + 0.5);
839 priv->fixed_qp_idr = priv->fixed_qp_p;
840 if (avctx->b_quant_factor > 0.0)
841 priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
842 avctx->b_quant_offset) + 0.5);
844 priv->fixed_qp_b = priv->fixed_qp_p;
846 priv->sei &= ~SEI_TIMING;
848 av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
849 "%d / %d / %d for IDR- / P- / B-frames.\n",
850 priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
852 } else if (ctx->va_rc_mode == VA_RC_CBR ||
853 ctx->va_rc_mode == VA_RC_VBR) {
854 // These still need to be set for pic_init_qp/slice_qp_delta.
855 priv->fixed_qp_idr = 26;
856 priv->fixed_qp_p = 26;
857 priv->fixed_qp_b = 26;
860 av_assert0(0 && "Invalid RC mode.");
863 if (priv->sei & SEI_IDENTIFIER) {
864 const char *lavc = LIBAVCODEC_IDENT;
865 const char *vaapi = VA_VERSION_S;
869 memcpy(priv->sei_identifier.uuid_iso_iec_11578,
870 vaapi_encode_h264_sei_identifier_uuid,
871 sizeof(priv->sei_identifier.uuid_iso_iec_11578));
873 driver = vaQueryVendorString(ctx->hwctx->display);
875 driver = "unknown driver";
877 len = snprintf(NULL, 0, "%s / VAAPI %s / %s", lavc, vaapi, driver);
879 priv->sei_identifier_string = av_malloc(len + 1);
880 if (!priv->sei_identifier_string)
881 return AVERROR(ENOMEM);
883 snprintf(priv->sei_identifier_string, len + 1,
884 "%s / VAAPI %s / %s", lavc, vaapi, driver);
886 priv->sei_identifier.data = priv->sei_identifier_string;
887 priv->sei_identifier.data_length = len + 1;
894 static const VAAPIEncodeProfile vaapi_encode_h264_profiles[] = {
895 { FF_PROFILE_H264_HIGH, 8, 3, 1, 1, VAProfileH264High },
896 { FF_PROFILE_H264_MAIN, 8, 3, 1, 1, VAProfileH264Main },
897 { FF_PROFILE_H264_CONSTRAINED_BASELINE,
898 8, 3, 1, 1, VAProfileH264ConstrainedBaseline },
899 { FF_PROFILE_UNKNOWN }
902 static const VAAPIEncodeType vaapi_encode_type_h264 = {
903 .profiles = vaapi_encode_h264_profiles,
905 .flags = FLAG_SLICE_CONTROL,
907 .configure = &vaapi_encode_h264_configure,
909 .sequence_params_size = sizeof(VAEncSequenceParameterBufferH264),
910 .init_sequence_params = &vaapi_encode_h264_init_sequence_params,
912 .picture_params_size = sizeof(VAEncPictureParameterBufferH264),
913 .init_picture_params = &vaapi_encode_h264_init_picture_params,
915 .slice_params_size = sizeof(VAEncSliceParameterBufferH264),
916 .init_slice_params = &vaapi_encode_h264_init_slice_params,
918 .sequence_header_type = VAEncPackedHeaderSequence,
919 .write_sequence_header = &vaapi_encode_h264_write_sequence_header,
921 .slice_header_type = VAEncPackedHeaderH264_Slice,
922 .write_slice_header = &vaapi_encode_h264_write_slice_header,
924 .write_extra_header = &vaapi_encode_h264_write_extra_header,
927 static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
929 VAAPIEncodeContext *ctx = avctx->priv_data;
930 VAAPIEncodeH264Context *priv = avctx->priv_data;
932 ctx->codec = &vaapi_encode_type_h264;
934 if (avctx->profile == FF_PROFILE_UNKNOWN)
935 avctx->profile = priv->profile;
936 if (avctx->level == FF_LEVEL_UNKNOWN)
937 avctx->level = priv->level;
938 if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
939 avctx->compression_level = priv->quality;
941 // Reject unsupported profiles.
942 switch (avctx->profile) {
943 case FF_PROFILE_H264_BASELINE:
944 av_log(avctx, AV_LOG_WARNING, "H.264 baseline profile is not "
945 "supported, using constrained baseline profile instead.\n");
946 avctx->profile = FF_PROFILE_H264_CONSTRAINED_BASELINE;
948 case FF_PROFILE_H264_EXTENDED:
949 av_log(avctx, AV_LOG_ERROR, "H.264 extended profile "
950 "is not supported.\n");
951 return AVERROR_PATCHWELCOME;
952 case FF_PROFILE_H264_HIGH_10:
953 case FF_PROFILE_H264_HIGH_10_INTRA:
954 av_log(avctx, AV_LOG_ERROR, "H.264 10-bit profiles "
955 "are not supported.\n");
956 return AVERROR_PATCHWELCOME;
957 case FF_PROFILE_H264_HIGH_422:
958 case FF_PROFILE_H264_HIGH_422_INTRA:
959 case FF_PROFILE_H264_HIGH_444:
960 case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
961 case FF_PROFILE_H264_HIGH_444_INTRA:
962 case FF_PROFILE_H264_CAVLC_444:
963 av_log(avctx, AV_LOG_ERROR, "H.264 non-4:2:0 profiles "
964 "are not supported.\n");
965 return AVERROR_PATCHWELCOME;
968 if (avctx->level != FF_LEVEL_UNKNOWN && avctx->level & ~0xff) {
969 av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
970 "in 8-bit unsigned integer.\n", avctx->level);
971 return AVERROR(EINVAL);
974 ctx->desired_packed_headers =
975 VA_ENC_PACKED_HEADER_SEQUENCE | // SPS and PPS.
976 VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
977 VA_ENC_PACKED_HEADER_MISC; // SEI.
979 ctx->surface_width = FFALIGN(avctx->width, 16);
980 ctx->surface_height = FFALIGN(avctx->height, 16);
982 ctx->slice_block_height = ctx->slice_block_width = 16;
984 return ff_vaapi_encode_init(avctx);
987 static av_cold int vaapi_encode_h264_close(AVCodecContext *avctx)
989 VAAPIEncodeH264Context *priv = avctx->priv_data;
991 ff_cbs_close(&priv->cbc);
992 av_freep(&priv->sei_identifier_string);
994 return ff_vaapi_encode_close(avctx);
997 #define OFFSET(x) offsetof(VAAPIEncodeH264Context, x)
998 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
999 static const AVOption vaapi_encode_h264_options[] = {
1000 VAAPI_ENCODE_COMMON_OPTIONS,
1002 { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1003 OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 20 }, 0, 52, FLAGS },
1004 { "quality", "Set encode quality (trades off against speed, higher is faster)",
1005 OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
1006 { "coder", "Entropy coder type",
1007 OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "coder" },
1008 { "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1009 { "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1010 { "vlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1011 { "ac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1013 { "aud", "Include AUD",
1014 OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1016 { "sei", "Set SEI to include",
1017 OFFSET(sei), AV_OPT_TYPE_FLAGS,
1018 { .i64 = SEI_IDENTIFIER | SEI_TIMING | SEI_RECOVERY_POINT },
1019 0, INT_MAX, FLAGS, "sei" },
1020 { "identifier", "Include encoder version identifier",
1021 0, AV_OPT_TYPE_CONST, { .i64 = SEI_IDENTIFIER },
1022 INT_MIN, INT_MAX, FLAGS, "sei" },
1023 { "timing", "Include timing parameters (buffering_period and pic_timing)",
1024 0, AV_OPT_TYPE_CONST, { .i64 = SEI_TIMING },
1025 INT_MIN, INT_MAX, FLAGS, "sei" },
1026 { "recovery_point", "Include recovery points where appropriate",
1027 0, AV_OPT_TYPE_CONST, { .i64 = SEI_RECOVERY_POINT },
1028 INT_MIN, INT_MAX, FLAGS, "sei" },
1030 { "profile", "Set profile (profile_idc and constraint_set*_flag)",
1031 OFFSET(profile), AV_OPT_TYPE_INT,
1032 { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xffff, FLAGS, "profile" },
1034 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1035 { .i64 = value }, 0, 0, FLAGS, "profile"
1036 { PROFILE("constrained_baseline", FF_PROFILE_H264_CONSTRAINED_BASELINE) },
1037 { PROFILE("main", FF_PROFILE_H264_MAIN) },
1038 { PROFILE("high", FF_PROFILE_H264_HIGH) },
1041 { "level", "Set level (level_idc)",
1042 OFFSET(level), AV_OPT_TYPE_INT,
1043 { .i64 = FF_LEVEL_UNKNOWN }, FF_LEVEL_UNKNOWN, 0xff, FLAGS, "level" },
1045 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1046 { .i64 = value }, 0, 0, FLAGS, "level"
1048 { LEVEL("1.1", 11) },
1049 { LEVEL("1.2", 12) },
1050 { LEVEL("1.3", 13) },
1052 { LEVEL("2.1", 21) },
1053 { LEVEL("2.2", 22) },
1055 { LEVEL("3.1", 31) },
1056 { LEVEL("3.2", 32) },
1058 { LEVEL("4.1", 41) },
1059 { LEVEL("4.2", 42) },
1061 { LEVEL("5.1", 51) },
1062 { LEVEL("5.2", 52) },
1064 { LEVEL("6.1", 61) },
1065 { LEVEL("6.2", 62) },
1071 static const AVCodecDefault vaapi_encode_h264_defaults[] = {
1075 { "i_qfactor", "1" },
1076 { "i_qoffset", "0" },
1077 { "b_qfactor", "6/5" },
1078 { "b_qoffset", "0" },
1084 static const AVClass vaapi_encode_h264_class = {
1085 .class_name = "h264_vaapi",
1086 .item_name = av_default_item_name,
1087 .option = vaapi_encode_h264_options,
1088 .version = LIBAVUTIL_VERSION_INT,
1091 AVCodec ff_h264_vaapi_encoder = {
1092 .name = "h264_vaapi",
1093 .long_name = NULL_IF_CONFIG_SMALL("H.264/AVC (VAAPI)"),
1094 .type = AVMEDIA_TYPE_VIDEO,
1095 .id = AV_CODEC_ID_H264,
1096 .priv_data_size = sizeof(VAAPIEncodeH264Context),
1097 .init = &vaapi_encode_h264_init,
1098 .encode2 = &ff_vaapi_encode2,
1099 .close = &vaapi_encode_h264_close,
1100 .priv_class = &vaapi_encode_h264_class,
1101 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
1102 .defaults = vaapi_encode_h264_defaults,
1103 .pix_fmts = (const enum AVPixelFormat[]) {
1107 .wrapper_name = "vaapi",