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 VAAPIEncodeH264Picture {
54 int64_t last_idr_frame;
62 } VAAPIEncodeH264Picture;
64 typedef struct VAAPIEncodeH264Context {
65 VAAPIEncodeContext common;
87 CodedBitstreamContext *cbc;
88 CodedBitstreamFragment current_access_unit;
94 H264RawSlice raw_slice;
96 H264RawSEIBufferingPeriod sei_buffering_period;
97 H264RawSEIPicTiming sei_pic_timing;
98 H264RawSEIRecoveryPoint sei_recovery_point;
99 H264RawSEIUserDataUnregistered sei_identifier;
100 char *sei_identifier_string;
104 int sei_cbr_workaround_needed;
105 } VAAPIEncodeH264Context;
108 static int vaapi_encode_h264_write_access_unit(AVCodecContext *avctx,
109 char *data, size_t *data_len,
110 CodedBitstreamFragment *au)
112 VAAPIEncodeH264Context *priv = avctx->priv_data;
115 err = ff_cbs_write_fragment_data(priv->cbc, au);
117 av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
121 if (*data_len < 8 * au->data_size - au->data_bit_padding) {
122 av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
123 "%zu < %zu.\n", *data_len,
124 8 * au->data_size - au->data_bit_padding);
125 return AVERROR(ENOSPC);
128 memcpy(data, au->data, au->data_size);
129 *data_len = 8 * au->data_size - au->data_bit_padding;
134 static int vaapi_encode_h264_add_nal(AVCodecContext *avctx,
135 CodedBitstreamFragment *au,
138 H264RawNALUnitHeader *header = nal_unit;
141 err = ff_cbs_insert_unit_content(au, -1,
142 header->nal_unit_type, nal_unit, NULL);
144 av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
145 "type = %d.\n", header->nal_unit_type);
152 static int vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx,
153 char *data, size_t *data_len)
155 VAAPIEncodeH264Context *priv = avctx->priv_data;
156 CodedBitstreamFragment *au = &priv->current_access_unit;
159 if (priv->aud_needed) {
160 err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
163 priv->aud_needed = 0;
166 err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_sps);
170 err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_pps);
174 err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
176 ff_cbs_fragment_reset(au);
180 static int vaapi_encode_h264_write_slice_header(AVCodecContext *avctx,
181 VAAPIEncodePicture *pic,
182 VAAPIEncodeSlice *slice,
183 char *data, size_t *data_len)
185 VAAPIEncodeH264Context *priv = avctx->priv_data;
186 CodedBitstreamFragment *au = &priv->current_access_unit;
189 if (priv->aud_needed) {
190 err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
193 priv->aud_needed = 0;
196 err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_slice);
200 err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
202 ff_cbs_fragment_reset(au);
206 static int vaapi_encode_h264_write_extra_header(AVCodecContext *avctx,
207 VAAPIEncodePicture *pic,
208 int index, int *type,
209 char *data, size_t *data_len)
211 VAAPIEncodeH264Context *priv = avctx->priv_data;
212 CodedBitstreamFragment *au = &priv->current_access_unit;
215 if (priv->sei_needed) {
216 H264RawSEI *sei = &priv->raw_sei;
218 if (priv->aud_needed) {
219 err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_aud);
222 priv->aud_needed = 0;
225 *sei = (H264RawSEI) {
227 .nal_unit_type = H264_NAL_SEI,
233 if (priv->sei_needed & SEI_IDENTIFIER) {
234 sei->payload[i].payload_type = H264_SEI_TYPE_USER_DATA_UNREGISTERED;
235 sei->payload[i].payload.user_data_unregistered = priv->sei_identifier;
238 if (priv->sei_needed & SEI_TIMING) {
239 if (pic->type == PICTURE_TYPE_IDR) {
240 sei->payload[i].payload_type = H264_SEI_TYPE_BUFFERING_PERIOD;
241 sei->payload[i].payload.buffering_period = priv->sei_buffering_period;
244 sei->payload[i].payload_type = H264_SEI_TYPE_PIC_TIMING;
245 sei->payload[i].payload.pic_timing = priv->sei_pic_timing;
248 if (priv->sei_needed & SEI_RECOVERY_POINT) {
249 sei->payload[i].payload_type = H264_SEI_TYPE_RECOVERY_POINT;
250 sei->payload[i].payload.recovery_point = priv->sei_recovery_point;
254 sei->payload_count = i;
255 av_assert0(sei->payload_count > 0);
257 err = vaapi_encode_h264_add_nal(avctx, au, sei);
260 priv->sei_needed = 0;
262 err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
266 ff_cbs_fragment_reset(au);
268 *type = VAEncPackedHeaderRawData;
272 } else if (priv->sei_cbr_workaround_needed) {
273 // Insert a zero-length header using the old SEI type. This is
274 // required to avoid triggering broken behaviour on Intel platforms
275 // in CBR mode where an invalid SEI message is generated by the
276 // driver and inserted into the stream.
278 *type = VAEncPackedHeaderH264_SEI;
279 priv->sei_cbr_workaround_needed = 0;
288 ff_cbs_fragment_reset(au);
292 static int vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx)
294 VAAPIEncodeContext *ctx = avctx->priv_data;
295 VAAPIEncodeH264Context *priv = avctx->priv_data;
296 H264RawSPS *sps = &priv->raw_sps;
297 H264RawPPS *pps = &priv->raw_pps;
298 VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
299 VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params;
301 memset(sps, 0, sizeof(*sps));
302 memset(pps, 0, sizeof(*pps));
304 sps->nal_unit_header.nal_ref_idc = 3;
305 sps->nal_unit_header.nal_unit_type = H264_NAL_SPS;
307 sps->profile_idc = avctx->profile & 0xff;
309 if (avctx->profile == FF_PROFILE_H264_CONSTRAINED_BASELINE ||
310 avctx->profile == FF_PROFILE_H264_MAIN)
311 sps->constraint_set1_flag = 1;
313 if (avctx->profile == FF_PROFILE_H264_HIGH)
314 sps->constraint_set3_flag = ctx->gop_size == 1;
316 if (avctx->profile == FF_PROFILE_H264_MAIN ||
317 avctx->profile == FF_PROFILE_H264_HIGH) {
318 sps->constraint_set4_flag = 1;
319 sps->constraint_set5_flag = ctx->b_per_p == 0;
322 if (ctx->gop_size == 1)
323 priv->dpb_frames = 0;
325 priv->dpb_frames = 1 + ctx->max_b_depth;
327 if (avctx->level != FF_LEVEL_UNKNOWN) {
328 sps->level_idc = avctx->level;
330 const H264LevelDescriptor *level;
333 if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
334 framerate = avctx->framerate.num / avctx->framerate.den;
338 level = ff_h264_guess_level(sps->profile_idc,
342 priv->mb_height * 16,
345 av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name);
346 if (level->constraint_set3_flag)
347 sps->constraint_set3_flag = 1;
348 sps->level_idc = level->level_idc;
350 av_log(avctx, AV_LOG_WARNING, "Stream will not conform "
351 "to any level: using level 6.2.\n");
356 sps->seq_parameter_set_id = 0;
357 sps->chroma_format_idc = 1;
359 sps->log2_max_frame_num_minus4 = 4;
360 sps->pic_order_cnt_type = 0;
361 sps->log2_max_pic_order_cnt_lsb_minus4 = 4;
363 sps->max_num_ref_frames = priv->dpb_frames;
365 sps->pic_width_in_mbs_minus1 = priv->mb_width - 1;
366 sps->pic_height_in_map_units_minus1 = priv->mb_height - 1;
368 sps->frame_mbs_only_flag = 1;
369 sps->direct_8x8_inference_flag = 1;
371 if (avctx->width != 16 * priv->mb_width ||
372 avctx->height != 16 * priv->mb_height) {
373 sps->frame_cropping_flag = 1;
375 sps->frame_crop_left_offset = 0;
376 sps->frame_crop_right_offset =
377 (16 * priv->mb_width - avctx->width) / 2;
378 sps->frame_crop_top_offset = 0;
379 sps->frame_crop_bottom_offset =
380 (16 * priv->mb_height - avctx->height) / 2;
382 sps->frame_cropping_flag = 0;
385 sps->vui_parameters_present_flag = 1;
387 if (avctx->sample_aspect_ratio.num != 0 &&
388 avctx->sample_aspect_ratio.den != 0) {
389 static const AVRational sar_idc[] = {
391 { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
392 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
393 { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
394 { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
397 av_reduce(&num, &den, avctx->sample_aspect_ratio.num,
398 avctx->sample_aspect_ratio.den, 65535);
399 for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
400 if (num == sar_idc[i].num &&
401 den == sar_idc[i].den) {
402 sps->vui.aspect_ratio_idc = i;
406 if (i >= FF_ARRAY_ELEMS(sar_idc)) {
407 sps->vui.aspect_ratio_idc = 255;
408 sps->vui.sar_width = num;
409 sps->vui.sar_height = den;
411 sps->vui.aspect_ratio_info_present_flag = 1;
414 // Unspecified video format, from table E-2.
415 sps->vui.video_format = 5;
416 sps->vui.video_full_range_flag =
417 avctx->color_range == AVCOL_RANGE_JPEG;
418 sps->vui.colour_primaries = avctx->color_primaries;
419 sps->vui.transfer_characteristics = avctx->color_trc;
420 sps->vui.matrix_coefficients = avctx->colorspace;
421 if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
422 avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
423 avctx->colorspace != AVCOL_SPC_UNSPECIFIED)
424 sps->vui.colour_description_present_flag = 1;
425 if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
426 sps->vui.colour_description_present_flag)
427 sps->vui.video_signal_type_present_flag = 1;
429 if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
430 sps->vui.chroma_loc_info_present_flag = 1;
431 sps->vui.chroma_sample_loc_type_top_field =
432 sps->vui.chroma_sample_loc_type_bottom_field =
433 avctx->chroma_sample_location - 1;
436 sps->vui.timing_info_present_flag = 1;
437 if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
438 sps->vui.num_units_in_tick = avctx->framerate.den;
439 sps->vui.time_scale = 2 * avctx->framerate.num;
440 sps->vui.fixed_frame_rate_flag = 1;
442 sps->vui.num_units_in_tick = avctx->time_base.num;
443 sps->vui.time_scale = 2 * avctx->time_base.den;
444 sps->vui.fixed_frame_rate_flag = 0;
447 if (priv->sei & SEI_TIMING) {
448 H264RawHRD *hrd = &sps->vui.nal_hrd_parameters;
449 H264RawSEIBufferingPeriod *bp = &priv->sei_buffering_period;
451 sps->vui.nal_hrd_parameters_present_flag = 1;
453 hrd->cpb_cnt_minus1 = 0;
455 // Try to scale these to a sensible range so that the
456 // golomb encode of the value is not overlong.
457 hrd->bit_rate_scale =
458 av_clip_uintp2(av_log2(ctx->va_bit_rate) - 15 - 6, 4);
459 hrd->bit_rate_value_minus1[0] =
460 (ctx->va_bit_rate >> hrd->bit_rate_scale + 6) - 1;
462 hrd->cpb_size_scale =
463 av_clip_uintp2(av_log2(ctx->hrd_params.buffer_size) - 15 - 4, 4);
464 hrd->cpb_size_value_minus1[0] =
465 (ctx->hrd_params.buffer_size >> hrd->cpb_size_scale + 4) - 1;
467 // CBR mode as defined for the HRD cannot be achieved without filler
468 // data, so this flag cannot be set even with VAAPI CBR modes.
469 hrd->cbr_flag[0] = 0;
471 hrd->initial_cpb_removal_delay_length_minus1 = 23;
472 hrd->cpb_removal_delay_length_minus1 = 23;
473 hrd->dpb_output_delay_length_minus1 = 7;
474 hrd->time_offset_length = 0;
476 bp->seq_parameter_set_id = sps->seq_parameter_set_id;
478 // This calculation can easily overflow 32 bits.
479 bp->nal.initial_cpb_removal_delay[0] = 90000 *
480 (uint64_t)ctx->hrd_params.initial_buffer_fullness /
481 ctx->hrd_params.buffer_size;
482 bp->nal.initial_cpb_removal_delay_offset[0] = 0;
484 sps->vui.nal_hrd_parameters_present_flag = 0;
485 sps->vui.low_delay_hrd_flag = 1 - sps->vui.fixed_frame_rate_flag;
488 sps->vui.bitstream_restriction_flag = 1;
489 sps->vui.motion_vectors_over_pic_boundaries_flag = 1;
490 sps->vui.log2_max_mv_length_horizontal = 15;
491 sps->vui.log2_max_mv_length_vertical = 15;
492 sps->vui.max_num_reorder_frames = ctx->max_b_depth;
493 sps->vui.max_dec_frame_buffering = ctx->max_b_depth + 1;
495 pps->nal_unit_header.nal_ref_idc = 3;
496 pps->nal_unit_header.nal_unit_type = H264_NAL_PPS;
498 pps->pic_parameter_set_id = 0;
499 pps->seq_parameter_set_id = 0;
501 pps->entropy_coding_mode_flag =
502 !(sps->profile_idc == FF_PROFILE_H264_BASELINE ||
503 sps->profile_idc == FF_PROFILE_H264_EXTENDED ||
504 sps->profile_idc == FF_PROFILE_H264_CAVLC_444);
505 if (!priv->coder && pps->entropy_coding_mode_flag)
506 pps->entropy_coding_mode_flag = 0;
508 pps->num_ref_idx_l0_default_active_minus1 = 0;
509 pps->num_ref_idx_l1_default_active_minus1 = 0;
511 pps->pic_init_qp_minus26 = priv->fixed_qp_idr - 26;
513 if (sps->profile_idc == FF_PROFILE_H264_BASELINE ||
514 sps->profile_idc == FF_PROFILE_H264_EXTENDED ||
515 sps->profile_idc == FF_PROFILE_H264_MAIN) {
516 pps->more_rbsp_data = 0;
518 pps->more_rbsp_data = 1;
520 pps->transform_8x8_mode_flag = 1;
523 *vseq = (VAEncSequenceParameterBufferH264) {
524 .seq_parameter_set_id = sps->seq_parameter_set_id,
525 .level_idc = sps->level_idc,
526 .intra_period = ctx->gop_size,
527 .intra_idr_period = ctx->gop_size,
528 .ip_period = ctx->b_per_p + 1,
530 .bits_per_second = ctx->va_bit_rate,
531 .max_num_ref_frames = sps->max_num_ref_frames,
532 .picture_width_in_mbs = sps->pic_width_in_mbs_minus1 + 1,
533 .picture_height_in_mbs = sps->pic_height_in_map_units_minus1 + 1,
536 .chroma_format_idc = sps->chroma_format_idc,
537 .frame_mbs_only_flag = sps->frame_mbs_only_flag,
538 .mb_adaptive_frame_field_flag = sps->mb_adaptive_frame_field_flag,
539 .seq_scaling_matrix_present_flag = sps->seq_scaling_matrix_present_flag,
540 .direct_8x8_inference_flag = sps->direct_8x8_inference_flag,
541 .log2_max_frame_num_minus4 = sps->log2_max_frame_num_minus4,
542 .pic_order_cnt_type = sps->pic_order_cnt_type,
543 .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_pic_order_cnt_lsb_minus4,
544 .delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag,
547 .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
548 .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
550 .frame_cropping_flag = sps->frame_cropping_flag,
551 .frame_crop_left_offset = sps->frame_crop_left_offset,
552 .frame_crop_right_offset = sps->frame_crop_right_offset,
553 .frame_crop_top_offset = sps->frame_crop_top_offset,
554 .frame_crop_bottom_offset = sps->frame_crop_bottom_offset,
556 .vui_parameters_present_flag = sps->vui_parameters_present_flag,
559 .aspect_ratio_info_present_flag = sps->vui.aspect_ratio_info_present_flag,
560 .timing_info_present_flag = sps->vui.timing_info_present_flag,
561 .bitstream_restriction_flag = sps->vui.bitstream_restriction_flag,
562 .log2_max_mv_length_horizontal = sps->vui.log2_max_mv_length_horizontal,
563 .log2_max_mv_length_vertical = sps->vui.log2_max_mv_length_vertical,
566 .aspect_ratio_idc = sps->vui.aspect_ratio_idc,
567 .sar_width = sps->vui.sar_width,
568 .sar_height = sps->vui.sar_height,
569 .num_units_in_tick = sps->vui.num_units_in_tick,
570 .time_scale = sps->vui.time_scale,
573 *vpic = (VAEncPictureParameterBufferH264) {
575 .picture_id = VA_INVALID_ID,
576 .flags = VA_PICTURE_H264_INVALID,
579 .coded_buf = VA_INVALID_ID,
581 .pic_parameter_set_id = pps->pic_parameter_set_id,
582 .seq_parameter_set_id = pps->seq_parameter_set_id,
584 .pic_init_qp = pps->pic_init_qp_minus26 + 26,
585 .num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1,
586 .num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1,
588 .chroma_qp_index_offset = pps->chroma_qp_index_offset,
589 .second_chroma_qp_index_offset = pps->second_chroma_qp_index_offset,
592 .entropy_coding_mode_flag = pps->entropy_coding_mode_flag,
593 .weighted_pred_flag = pps->weighted_pred_flag,
594 .weighted_bipred_idc = pps->weighted_bipred_idc,
595 .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
596 .transform_8x8_mode_flag = pps->transform_8x8_mode_flag,
597 .deblocking_filter_control_present_flag =
598 pps->deblocking_filter_control_present_flag,
599 .redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present_flag,
600 .pic_order_present_flag =
601 pps->bottom_field_pic_order_in_frame_present_flag,
602 .pic_scaling_matrix_present_flag = pps->pic_scaling_matrix_present_flag,
609 static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
610 VAAPIEncodePicture *pic)
612 VAAPIEncodeContext *ctx = avctx->priv_data;
613 VAAPIEncodeH264Context *priv = avctx->priv_data;
614 VAAPIEncodeH264Picture *hpic = pic->priv_data;
615 VAAPIEncodePicture *prev = pic->prev;
616 VAAPIEncodeH264Picture *hprev = prev ? prev->priv_data : NULL;
617 VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
620 if (pic->type == PICTURE_TYPE_IDR) {
621 av_assert0(pic->display_order == pic->encode_order);
624 hpic->last_idr_frame = pic->display_order;
625 hpic->idr_pic_id = hprev ? hprev->idr_pic_id + 1 : 0;
627 hpic->primary_pic_type = 0;
628 hpic->slice_type = 7;
632 hpic->frame_num = hprev->frame_num + prev->is_reference;
634 hpic->last_idr_frame = hprev->last_idr_frame;
635 hpic->idr_pic_id = hprev->idr_pic_id;
637 if (pic->type == PICTURE_TYPE_I) {
638 hpic->slice_type = 7;
639 hpic->primary_pic_type = 0;
640 } else if (pic->type == PICTURE_TYPE_P) {
641 hpic->slice_type = 5;
642 hpic->primary_pic_type = 1;
644 hpic->slice_type = 6;
645 hpic->primary_pic_type = 2;
648 hpic->pic_order_cnt = pic->display_order - hpic->last_idr_frame;
649 hpic->dpb_delay = pic->display_order - pic->encode_order + ctx->max_b_depth;
650 hpic->cpb_delay = pic->encode_order - hpic->last_idr_frame;
653 priv->aud_needed = 1;
654 priv->raw_aud = (H264RawAUD) {
656 .nal_unit_type = H264_NAL_AUD,
658 .primary_pic_type = hpic->primary_pic_type,
661 priv->aud_needed = 0;
664 priv->sei_needed = 0;
666 if (priv->sei & SEI_IDENTIFIER && pic->encode_order == 0)
667 priv->sei_needed |= SEI_IDENTIFIER;
669 if (ctx->va_rc_mode == VA_RC_CBR)
670 priv->sei_cbr_workaround_needed = 1;
673 if (priv->sei & SEI_TIMING) {
674 priv->sei_pic_timing = (H264RawSEIPicTiming) {
675 .cpb_removal_delay = 2 * hpic->cpb_delay,
676 .dpb_output_delay = 2 * hpic->dpb_delay,
679 priv->sei_needed |= SEI_TIMING;
682 if (priv->sei & SEI_RECOVERY_POINT && pic->type == PICTURE_TYPE_I) {
683 priv->sei_recovery_point = (H264RawSEIRecoveryPoint) {
684 .recovery_frame_cnt = 0,
685 .exact_match_flag = 1,
686 .broken_link_flag = ctx->b_per_p > 0,
689 priv->sei_needed |= SEI_RECOVERY_POINT;
692 vpic->CurrPic = (VAPictureH264) {
693 .picture_id = pic->recon_surface,
694 .frame_idx = hpic->frame_num,
696 .TopFieldOrderCnt = hpic->pic_order_cnt,
697 .BottomFieldOrderCnt = hpic->pic_order_cnt,
700 for (i = 0; i < pic->nb_refs; i++) {
701 VAAPIEncodePicture *ref = pic->refs[i];
702 VAAPIEncodeH264Picture *href;
704 av_assert0(ref && ref->encode_order < pic->encode_order);
705 href = ref->priv_data;
707 vpic->ReferenceFrames[i] = (VAPictureH264) {
708 .picture_id = ref->recon_surface,
709 .frame_idx = href->frame_num,
710 .flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE,
711 .TopFieldOrderCnt = href->pic_order_cnt,
712 .BottomFieldOrderCnt = href->pic_order_cnt,
715 for (; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
716 vpic->ReferenceFrames[i] = (VAPictureH264) {
717 .picture_id = VA_INVALID_ID,
718 .flags = VA_PICTURE_H264_INVALID,
722 vpic->coded_buf = pic->output_buffer;
724 vpic->frame_num = hpic->frame_num;
726 vpic->pic_fields.bits.idr_pic_flag = (pic->type == PICTURE_TYPE_IDR);
727 vpic->pic_fields.bits.reference_pic_flag = (pic->type != PICTURE_TYPE_B);
732 static void vaapi_encode_h264_default_ref_pic_list(AVCodecContext *avctx,
733 VAAPIEncodePicture *pic,
734 VAAPIEncodePicture **rpl0,
735 VAAPIEncodePicture **rpl1,
738 VAAPIEncodePicture *prev;
739 VAAPIEncodeH264Picture *hp, *hn, *hc;
746 for (i = 0; i < pic->prev->nb_dpb_pics; i++) {
747 hn = prev->dpb[i]->priv_data;
748 av_assert0(hn->frame_num < hp->frame_num);
750 if (pic->type == PICTURE_TYPE_P) {
751 for (j = n; j > 0; j--) {
752 hc = rpl0[j - 1]->priv_data;
753 av_assert0(hc->frame_num != hn->frame_num);
754 if (hc->frame_num > hn->frame_num)
756 rpl0[j] = rpl0[j - 1];
758 rpl0[j] = prev->dpb[i];
760 } else if (pic->type == PICTURE_TYPE_B) {
761 for (j = n; j > 0; j--) {
762 hc = rpl0[j - 1]->priv_data;
763 av_assert0(hc->pic_order_cnt != hp->pic_order_cnt);
764 if (hc->pic_order_cnt < hp->pic_order_cnt) {
765 if (hn->pic_order_cnt > hp->pic_order_cnt ||
766 hn->pic_order_cnt < hc->pic_order_cnt)
769 if (hn->pic_order_cnt > hc->pic_order_cnt)
772 rpl0[j] = rpl0[j - 1];
774 rpl0[j] = prev->dpb[i];
776 for (j = n; j > 0; j--) {
777 hc = rpl1[j - 1]->priv_data;
778 av_assert0(hc->pic_order_cnt != hp->pic_order_cnt);
779 if (hc->pic_order_cnt > hp->pic_order_cnt) {
780 if (hn->pic_order_cnt < hp->pic_order_cnt ||
781 hn->pic_order_cnt > hc->pic_order_cnt)
784 if (hn->pic_order_cnt < hc->pic_order_cnt)
787 rpl1[j] = rpl1[j - 1];
789 rpl1[j] = prev->dpb[i];
795 if (pic->type == PICTURE_TYPE_B) {
796 for (i = 0; i < n; i++) {
797 if (rpl0[i] != rpl1[i])
801 FFSWAP(VAAPIEncodePicture*, rpl1[0], rpl1[1]);
804 if (pic->type == PICTURE_TYPE_P ||
805 pic->type == PICTURE_TYPE_B) {
806 av_log(avctx, AV_LOG_DEBUG, "Default RefPicList0 for fn=%d/poc=%d:",
807 hp->frame_num, hp->pic_order_cnt);
808 for (i = 0; i < n; i++) {
809 hn = rpl0[i]->priv_data;
810 av_log(avctx, AV_LOG_DEBUG, " fn=%d/poc=%d",
811 hn->frame_num, hn->pic_order_cnt);
813 av_log(avctx, AV_LOG_DEBUG, "\n");
815 if (pic->type == PICTURE_TYPE_B) {
816 av_log(avctx, AV_LOG_DEBUG, "Default RefPicList1 for fn=%d/poc=%d:",
817 hp->frame_num, hp->pic_order_cnt);
818 for (i = 0; i < n; i++) {
819 hn = rpl1[i]->priv_data;
820 av_log(avctx, AV_LOG_DEBUG, " fn=%d/poc=%d",
821 hn->frame_num, hn->pic_order_cnt);
823 av_log(avctx, AV_LOG_DEBUG, "\n");
829 static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx,
830 VAAPIEncodePicture *pic,
831 VAAPIEncodeSlice *slice)
833 VAAPIEncodeH264Context *priv = avctx->priv_data;
834 VAAPIEncodeH264Picture *hpic = pic->priv_data;
835 VAAPIEncodePicture *prev = pic->prev;
836 H264RawSPS *sps = &priv->raw_sps;
837 H264RawPPS *pps = &priv->raw_pps;
838 H264RawSliceHeader *sh = &priv->raw_slice.header;
839 VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
840 VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params;
843 if (pic->type == PICTURE_TYPE_IDR) {
844 sh->nal_unit_header.nal_unit_type = H264_NAL_IDR_SLICE;
845 sh->nal_unit_header.nal_ref_idc = 3;
847 sh->nal_unit_header.nal_unit_type = H264_NAL_SLICE;
848 sh->nal_unit_header.nal_ref_idc = pic->is_reference;
851 sh->first_mb_in_slice = slice->block_start;
852 sh->slice_type = hpic->slice_type;
854 sh->pic_parameter_set_id = pps->pic_parameter_set_id;
856 sh->frame_num = hpic->frame_num &
857 ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
858 sh->idr_pic_id = hpic->idr_pic_id;
859 sh->pic_order_cnt_lsb = hpic->pic_order_cnt &
860 ((1 << (4 + sps->log2_max_pic_order_cnt_lsb_minus4)) - 1);
862 sh->direct_spatial_mv_pred_flag = 1;
864 if (pic->type == PICTURE_TYPE_B)
865 sh->slice_qp_delta = priv->fixed_qp_b - (pps->pic_init_qp_minus26 + 26);
866 else if (pic->type == PICTURE_TYPE_P)
867 sh->slice_qp_delta = priv->fixed_qp_p - (pps->pic_init_qp_minus26 + 26);
869 sh->slice_qp_delta = priv->fixed_qp_idr - (pps->pic_init_qp_minus26 + 26);
871 if (pic->is_reference && pic->type != PICTURE_TYPE_IDR) {
872 VAAPIEncodePicture *discard_list[MAX_DPB_SIZE];
873 int discard = 0, keep = 0;
875 // Discard everything which is in the DPB of the previous frame but
876 // not in the DPB of this one.
877 for (i = 0; i < prev->nb_dpb_pics; i++) {
878 for (j = 0; j < pic->nb_dpb_pics; j++) {
879 if (prev->dpb[i] == pic->dpb[j])
882 if (j == pic->nb_dpb_pics) {
883 discard_list[discard] = prev->dpb[i];
889 av_assert0(keep <= priv->dpb_frames);
892 sh->adaptive_ref_pic_marking_mode_flag = 0;
894 sh->adaptive_ref_pic_marking_mode_flag = 1;
895 for (i = 0; i < discard; i++) {
896 VAAPIEncodeH264Picture *old = discard_list[i]->priv_data;
897 av_assert0(old->frame_num < hpic->frame_num);
898 sh->mmco[i].memory_management_control_operation = 1;
899 sh->mmco[i].difference_of_pic_nums_minus1 =
900 hpic->frame_num - old->frame_num - 1;
902 sh->mmco[i].memory_management_control_operation = 0;
906 // If the intended references are not the first entries of RefPicListN
907 // by default, use ref-pic-list-modification to move them there.
908 if (pic->type == PICTURE_TYPE_P || pic->type == PICTURE_TYPE_B) {
909 VAAPIEncodePicture *def_l0[MAX_DPB_SIZE], *def_l1[MAX_DPB_SIZE];
910 VAAPIEncodeH264Picture *href;
913 vaapi_encode_h264_default_ref_pic_list(avctx, pic,
916 if (pic->type == PICTURE_TYPE_P) {
918 for (i = 0; i < pic->nb_refs; i++) {
919 av_assert0(pic->refs[i]);
920 if (pic->refs[i] != def_l0[i])
924 sh->ref_pic_list_modification_flag_l0 = need_rplm;
926 int pic_num = hpic->frame_num;
927 for (i = 0; i < pic->nb_refs; i++) {
928 href = pic->refs[i]->priv_data;
929 av_assert0(href->frame_num != pic_num);
930 if (href->frame_num < pic_num) {
931 sh->rplm_l0[i].modification_of_pic_nums_idc = 0;
932 sh->rplm_l0[i].abs_diff_pic_num_minus1 =
933 pic_num - href->frame_num - 1;
935 sh->rplm_l0[i].modification_of_pic_nums_idc = 1;
936 sh->rplm_l0[i].abs_diff_pic_num_minus1 =
937 href->frame_num - pic_num - 1;
939 pic_num = href->frame_num;
941 sh->rplm_l0[i].modification_of_pic_nums_idc = 3;
945 int need_rplm_l0 = 0, need_rplm_l1 = 0;
947 for (i = 0; i < pic->nb_refs; i++) {
948 av_assert0(pic->refs[i]);
949 href = pic->refs[i]->priv_data;
950 av_assert0(href->pic_order_cnt != hpic->pic_order_cnt);
951 if (href->pic_order_cnt < hpic->pic_order_cnt) {
952 if (pic->refs[i] != def_l0[n0])
956 if (pic->refs[i] != def_l1[n1])
962 sh->ref_pic_list_modification_flag_l0 = need_rplm_l0;
964 int pic_num = hpic->frame_num;
965 for (i = j = 0; i < pic->nb_refs; i++) {
966 href = pic->refs[i]->priv_data;
967 if (href->pic_order_cnt > hpic->pic_order_cnt)
969 av_assert0(href->frame_num != pic_num);
970 if (href->frame_num < pic_num) {
971 sh->rplm_l0[j].modification_of_pic_nums_idc = 0;
972 sh->rplm_l0[j].abs_diff_pic_num_minus1 =
973 pic_num - href->frame_num - 1;
975 sh->rplm_l0[j].modification_of_pic_nums_idc = 1;
976 sh->rplm_l0[j].abs_diff_pic_num_minus1 =
977 href->frame_num - pic_num - 1;
979 pic_num = href->frame_num;
983 sh->rplm_l0[j].modification_of_pic_nums_idc = 3;
986 sh->ref_pic_list_modification_flag_l1 = need_rplm_l1;
988 int pic_num = hpic->frame_num;
989 for (i = j = 0; i < pic->nb_refs; i++) {
990 href = pic->refs[i]->priv_data;
991 if (href->pic_order_cnt < hpic->pic_order_cnt)
993 av_assert0(href->frame_num != pic_num);
994 if (href->frame_num < pic_num) {
995 sh->rplm_l1[j].modification_of_pic_nums_idc = 0;
996 sh->rplm_l1[j].abs_diff_pic_num_minus1 =
997 pic_num - href->frame_num - 1;
999 sh->rplm_l1[j].modification_of_pic_nums_idc = 1;
1000 sh->rplm_l1[j].abs_diff_pic_num_minus1 =
1001 href->frame_num - pic_num - 1;
1003 pic_num = href->frame_num;
1006 av_assert0(j == n1);
1007 sh->rplm_l1[j].modification_of_pic_nums_idc = 3;
1012 vslice->macroblock_address = slice->block_start;
1013 vslice->num_macroblocks = slice->block_size;
1015 vslice->macroblock_info = VA_INVALID_ID;
1017 vslice->slice_type = sh->slice_type % 5;
1018 vslice->pic_parameter_set_id = sh->pic_parameter_set_id;
1019 vslice->idr_pic_id = sh->idr_pic_id;
1021 vslice->pic_order_cnt_lsb = sh->pic_order_cnt_lsb;
1023 vslice->direct_spatial_mv_pred_flag = sh->direct_spatial_mv_pred_flag;
1025 for (i = 0; i < FF_ARRAY_ELEMS(vslice->RefPicList0); i++) {
1026 vslice->RefPicList0[i].picture_id = VA_INVALID_ID;
1027 vslice->RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
1028 vslice->RefPicList1[i].picture_id = VA_INVALID_ID;
1029 vslice->RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
1032 av_assert0(pic->nb_refs <= 2);
1033 if (pic->nb_refs >= 1) {
1034 // Backward reference for P- or B-frame.
1035 av_assert0(pic->type == PICTURE_TYPE_P ||
1036 pic->type == PICTURE_TYPE_B);
1037 vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
1039 if (pic->nb_refs >= 2) {
1040 // Forward reference for B-frame.
1041 av_assert0(pic->type == PICTURE_TYPE_B);
1042 vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
1045 vslice->slice_qp_delta = sh->slice_qp_delta;
1050 static av_cold int vaapi_encode_h264_configure(AVCodecContext *avctx)
1052 VAAPIEncodeContext *ctx = avctx->priv_data;
1053 VAAPIEncodeH264Context *priv = avctx->priv_data;
1056 err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_H264, avctx);
1060 priv->mb_width = FFALIGN(avctx->width, 16) / 16;
1061 priv->mb_height = FFALIGN(avctx->height, 16) / 16;
1063 if (ctx->va_rc_mode == VA_RC_CQP) {
1064 priv->fixed_qp_p = av_clip(ctx->rc_quality, 1, 51);
1065 if (avctx->i_quant_factor > 0.0)
1066 priv->fixed_qp_idr =
1067 av_clip((avctx->i_quant_factor * priv->fixed_qp_p +
1068 avctx->i_quant_offset) + 0.5, 1, 51);
1070 priv->fixed_qp_idr = priv->fixed_qp_p;
1071 if (avctx->b_quant_factor > 0.0)
1073 av_clip((avctx->b_quant_factor * priv->fixed_qp_p +
1074 avctx->b_quant_offset) + 0.5, 1, 51);
1076 priv->fixed_qp_b = priv->fixed_qp_p;
1078 av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
1079 "%d / %d / %d for IDR- / P- / B-frames.\n",
1080 priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
1083 // These still need to be set for pic_init_qp/slice_qp_delta.
1084 priv->fixed_qp_idr = 26;
1085 priv->fixed_qp_p = 26;
1086 priv->fixed_qp_b = 26;
1089 if (!ctx->rc_mode->hrd) {
1090 // Timing SEI requires a mode respecting HRD parameters.
1091 priv->sei &= ~SEI_TIMING;
1094 if (priv->sei & SEI_IDENTIFIER) {
1095 const char *lavc = LIBAVCODEC_IDENT;
1096 const char *vaapi = VA_VERSION_S;
1100 memcpy(priv->sei_identifier.uuid_iso_iec_11578,
1101 vaapi_encode_h264_sei_identifier_uuid,
1102 sizeof(priv->sei_identifier.uuid_iso_iec_11578));
1104 driver = vaQueryVendorString(ctx->hwctx->display);
1106 driver = "unknown driver";
1108 len = snprintf(NULL, 0, "%s / VAAPI %s / %s", lavc, vaapi, driver);
1110 priv->sei_identifier_string = av_malloc(len + 1);
1111 if (!priv->sei_identifier_string)
1112 return AVERROR(ENOMEM);
1114 snprintf(priv->sei_identifier_string, len + 1,
1115 "%s / VAAPI %s / %s", lavc, vaapi, driver);
1117 priv->sei_identifier.data = priv->sei_identifier_string;
1118 priv->sei_identifier.data_length = len + 1;
1122 ctx->roi_quant_range = 51 + 6 * (ctx->profile->depth - 8);
1127 static const VAAPIEncodeProfile vaapi_encode_h264_profiles[] = {
1128 { FF_PROFILE_H264_HIGH, 8, 3, 1, 1, VAProfileH264High },
1129 { FF_PROFILE_H264_MAIN, 8, 3, 1, 1, VAProfileH264Main },
1130 { FF_PROFILE_H264_CONSTRAINED_BASELINE,
1131 8, 3, 1, 1, VAProfileH264ConstrainedBaseline },
1132 { FF_PROFILE_UNKNOWN }
1135 static const VAAPIEncodeType vaapi_encode_type_h264 = {
1136 .profiles = vaapi_encode_h264_profiles,
1138 .flags = FLAG_SLICE_CONTROL |
1140 FLAG_B_PICTURE_REFERENCES |
1141 FLAG_NON_IDR_KEY_PICTURES,
1143 .default_quality = 20,
1145 .configure = &vaapi_encode_h264_configure,
1147 .picture_priv_data_size = sizeof(VAAPIEncodeH264Picture),
1149 .sequence_params_size = sizeof(VAEncSequenceParameterBufferH264),
1150 .init_sequence_params = &vaapi_encode_h264_init_sequence_params,
1152 .picture_params_size = sizeof(VAEncPictureParameterBufferH264),
1153 .init_picture_params = &vaapi_encode_h264_init_picture_params,
1155 .slice_params_size = sizeof(VAEncSliceParameterBufferH264),
1156 .init_slice_params = &vaapi_encode_h264_init_slice_params,
1158 .sequence_header_type = VAEncPackedHeaderSequence,
1159 .write_sequence_header = &vaapi_encode_h264_write_sequence_header,
1161 .slice_header_type = VAEncPackedHeaderH264_Slice,
1162 .write_slice_header = &vaapi_encode_h264_write_slice_header,
1164 .write_extra_header = &vaapi_encode_h264_write_extra_header,
1167 static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
1169 VAAPIEncodeContext *ctx = avctx->priv_data;
1170 VAAPIEncodeH264Context *priv = avctx->priv_data;
1172 ctx->codec = &vaapi_encode_type_h264;
1174 if (avctx->profile == FF_PROFILE_UNKNOWN)
1175 avctx->profile = priv->profile;
1176 if (avctx->level == FF_LEVEL_UNKNOWN)
1177 avctx->level = priv->level;
1178 if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
1179 avctx->compression_level = priv->quality;
1181 // Reject unsupported profiles.
1182 switch (avctx->profile) {
1183 case FF_PROFILE_H264_BASELINE:
1184 av_log(avctx, AV_LOG_WARNING, "H.264 baseline profile is not "
1185 "supported, using constrained baseline profile instead.\n");
1186 avctx->profile = FF_PROFILE_H264_CONSTRAINED_BASELINE;
1188 case FF_PROFILE_H264_EXTENDED:
1189 av_log(avctx, AV_LOG_ERROR, "H.264 extended profile "
1190 "is not supported.\n");
1191 return AVERROR_PATCHWELCOME;
1192 case FF_PROFILE_H264_HIGH_10:
1193 case FF_PROFILE_H264_HIGH_10_INTRA:
1194 av_log(avctx, AV_LOG_ERROR, "H.264 10-bit profiles "
1195 "are not supported.\n");
1196 return AVERROR_PATCHWELCOME;
1197 case FF_PROFILE_H264_HIGH_422:
1198 case FF_PROFILE_H264_HIGH_422_INTRA:
1199 case FF_PROFILE_H264_HIGH_444:
1200 case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
1201 case FF_PROFILE_H264_HIGH_444_INTRA:
1202 case FF_PROFILE_H264_CAVLC_444:
1203 av_log(avctx, AV_LOG_ERROR, "H.264 non-4:2:0 profiles "
1204 "are not supported.\n");
1205 return AVERROR_PATCHWELCOME;
1208 if (avctx->level != FF_LEVEL_UNKNOWN && avctx->level & ~0xff) {
1209 av_log(avctx, AV_LOG_ERROR, "Invalid level %d: must fit "
1210 "in 8-bit unsigned integer.\n", avctx->level);
1211 return AVERROR(EINVAL);
1214 ctx->desired_packed_headers =
1215 VA_ENC_PACKED_HEADER_SEQUENCE | // SPS and PPS.
1216 VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
1217 VA_ENC_PACKED_HEADER_MISC; // SEI.
1219 ctx->surface_width = FFALIGN(avctx->width, 16);
1220 ctx->surface_height = FFALIGN(avctx->height, 16);
1222 ctx->slice_block_height = ctx->slice_block_width = 16;
1225 ctx->explicit_qp = priv->qp;
1227 return ff_vaapi_encode_init(avctx);
1230 static av_cold int vaapi_encode_h264_close(AVCodecContext *avctx)
1232 VAAPIEncodeH264Context *priv = avctx->priv_data;
1234 ff_cbs_fragment_free(&priv->current_access_unit);
1235 ff_cbs_close(&priv->cbc);
1236 av_freep(&priv->sei_identifier_string);
1238 return ff_vaapi_encode_close(avctx);
1241 #define OFFSET(x) offsetof(VAAPIEncodeH264Context, x)
1242 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
1243 static const AVOption vaapi_encode_h264_options[] = {
1244 VAAPI_ENCODE_COMMON_OPTIONS,
1245 VAAPI_ENCODE_RC_OPTIONS,
1247 { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
1248 OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 52, FLAGS },
1249 { "quality", "Set encode quality (trades off against speed, higher is faster)",
1250 OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
1251 { "coder", "Entropy coder type",
1252 OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "coder" },
1253 { "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1254 { "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1255 { "vlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1256 { "ac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
1258 { "aud", "Include AUD",
1259 OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
1261 { "sei", "Set SEI to include",
1262 OFFSET(sei), AV_OPT_TYPE_FLAGS,
1263 { .i64 = SEI_IDENTIFIER | SEI_TIMING | SEI_RECOVERY_POINT },
1264 0, INT_MAX, FLAGS, "sei" },
1265 { "identifier", "Include encoder version identifier",
1266 0, AV_OPT_TYPE_CONST, { .i64 = SEI_IDENTIFIER },
1267 INT_MIN, INT_MAX, FLAGS, "sei" },
1268 { "timing", "Include timing parameters (buffering_period and pic_timing)",
1269 0, AV_OPT_TYPE_CONST, { .i64 = SEI_TIMING },
1270 INT_MIN, INT_MAX, FLAGS, "sei" },
1271 { "recovery_point", "Include recovery points where appropriate",
1272 0, AV_OPT_TYPE_CONST, { .i64 = SEI_RECOVERY_POINT },
1273 INT_MIN, INT_MAX, FLAGS, "sei" },
1275 { "profile", "Set profile (profile_idc and constraint_set*_flag)",
1276 OFFSET(profile), AV_OPT_TYPE_INT,
1277 { .i64 = FF_PROFILE_UNKNOWN }, FF_PROFILE_UNKNOWN, 0xffff, FLAGS, "profile" },
1279 #define PROFILE(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1280 { .i64 = value }, 0, 0, FLAGS, "profile"
1281 { PROFILE("constrained_baseline", FF_PROFILE_H264_CONSTRAINED_BASELINE) },
1282 { PROFILE("main", FF_PROFILE_H264_MAIN) },
1283 { PROFILE("high", FF_PROFILE_H264_HIGH) },
1286 { "level", "Set level (level_idc)",
1287 OFFSET(level), AV_OPT_TYPE_INT,
1288 { .i64 = FF_LEVEL_UNKNOWN }, FF_LEVEL_UNKNOWN, 0xff, FLAGS, "level" },
1290 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
1291 { .i64 = value }, 0, 0, FLAGS, "level"
1293 { LEVEL("1.1", 11) },
1294 { LEVEL("1.2", 12) },
1295 { LEVEL("1.3", 13) },
1297 { LEVEL("2.1", 21) },
1298 { LEVEL("2.2", 22) },
1300 { LEVEL("3.1", 31) },
1301 { LEVEL("3.2", 32) },
1303 { LEVEL("4.1", 41) },
1304 { LEVEL("4.2", 42) },
1306 { LEVEL("5.1", 51) },
1307 { LEVEL("5.2", 52) },
1309 { LEVEL("6.1", 61) },
1310 { LEVEL("6.2", 62) },
1316 static const AVCodecDefault vaapi_encode_h264_defaults[] = {
1320 { "i_qfactor", "1" },
1321 { "i_qoffset", "0" },
1322 { "b_qfactor", "6/5" },
1323 { "b_qoffset", "0" },
1329 static const AVClass vaapi_encode_h264_class = {
1330 .class_name = "h264_vaapi",
1331 .item_name = av_default_item_name,
1332 .option = vaapi_encode_h264_options,
1333 .version = LIBAVUTIL_VERSION_INT,
1336 AVCodec ff_h264_vaapi_encoder = {
1337 .name = "h264_vaapi",
1338 .long_name = NULL_IF_CONFIG_SMALL("H.264/AVC (VAAPI)"),
1339 .type = AVMEDIA_TYPE_VIDEO,
1340 .id = AV_CODEC_ID_H264,
1341 .priv_data_size = sizeof(VAAPIEncodeH264Context),
1342 .init = &vaapi_encode_h264_init,
1343 .receive_packet = &ff_vaapi_encode_receive_packet,
1344 .close = &vaapi_encode_h264_close,
1345 .priv_class = &vaapi_encode_h264_class,
1346 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE,
1347 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1348 .defaults = vaapi_encode_h264_defaults,
1349 .pix_fmts = (const enum AVPixelFormat[]) {
1353 .hw_configs = ff_vaapi_encode_hw_configs,
1354 .wrapper_name = "vaapi",