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"
35 #include "vaapi_encode.h"
39 SEI_IDENTIFIER = 0x02,
40 SEI_RECOVERY_POINT = 0x04,
43 // Random (version 4) ISO 11578 UUID.
44 static const uint8_t vaapi_encode_h264_sei_identifier_uuid[16] = {
45 0x59, 0x94, 0x8b, 0x28, 0x11, 0xec, 0x45, 0xaf,
46 0x96, 0x75, 0x19, 0xd4, 0x1f, 0xea, 0xa9, 0x4d,
49 typedef struct VAAPIEncodeH264Context {
63 H264RawSEIBufferingPeriod buffering_period;
64 H264RawSEIPicTiming pic_timing;
65 H264RawSEIRecoveryPoint recovery_point;
66 H264RawSEIUserDataUnregistered identifier;
67 char *identifier_string;
72 int64_t last_idr_frame;
73 int64_t idr_pic_count;
81 CodedBitstreamContext *cbc;
82 CodedBitstreamFragment current_access_unit;
85 } VAAPIEncodeH264Context;
87 typedef struct VAAPIEncodeH264Options {
91 // Entropy encoder type.
95 } VAAPIEncodeH264Options;
98 static int vaapi_encode_h264_write_access_unit(AVCodecContext *avctx,
99 char *data, size_t *data_len,
100 CodedBitstreamFragment *au)
102 VAAPIEncodeContext *ctx = avctx->priv_data;
103 VAAPIEncodeH264Context *priv = ctx->priv_data;
106 err = ff_cbs_write_fragment_data(priv->cbc, au);
108 av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
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);
119 memcpy(data, au->data, au->data_size);
120 *data_len = 8 * au->data_size - au->data_bit_padding;
125 static int vaapi_encode_h264_add_nal(AVCodecContext *avctx,
126 CodedBitstreamFragment *au,
129 VAAPIEncodeContext *ctx = avctx->priv_data;
130 VAAPIEncodeH264Context *priv = ctx->priv_data;
131 H264RawNALUnitHeader *header = nal_unit;
134 err = ff_cbs_insert_unit_content(priv->cbc, au, -1,
135 header->nal_unit_type, nal_unit);
137 av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
138 "type = %d.\n", header->nal_unit_type);
145 static int vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx,
146 char *data, size_t *data_len)
148 VAAPIEncodeContext *ctx = avctx->priv_data;
149 VAAPIEncodeH264Context *priv = ctx->priv_data;
150 CodedBitstreamFragment *au = &priv->current_access_unit;
153 if (priv->aud_needed) {
154 err = vaapi_encode_h264_add_nal(avctx, au, &priv->aud);
157 priv->aud_needed = 0;
160 err = vaapi_encode_h264_add_nal(avctx, au, &priv->sps);
164 err = vaapi_encode_h264_add_nal(avctx, au, &priv->pps);
168 err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
170 ff_cbs_fragment_uninit(priv->cbc, au);
174 static int vaapi_encode_h264_write_slice_header(AVCodecContext *avctx,
175 VAAPIEncodePicture *pic,
176 VAAPIEncodeSlice *slice,
177 char *data, size_t *data_len)
179 VAAPIEncodeContext *ctx = avctx->priv_data;
180 VAAPIEncodeH264Context *priv = ctx->priv_data;
181 CodedBitstreamFragment *au = &priv->current_access_unit;
184 if (priv->aud_needed) {
185 err = vaapi_encode_h264_add_nal(avctx, au, &priv->aud);
188 priv->aud_needed = 0;
191 err = vaapi_encode_h264_add_nal(avctx, au, &priv->slice);
195 err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
197 ff_cbs_fragment_uninit(priv->cbc, au);
201 static int vaapi_encode_h264_write_extra_header(AVCodecContext *avctx,
202 VAAPIEncodePicture *pic,
203 int index, int *type,
204 char *data, size_t *data_len)
206 VAAPIEncodeContext *ctx = avctx->priv_data;
207 VAAPIEncodeH264Context *priv = ctx->priv_data;
208 VAAPIEncodeH264Options *opt = ctx->codec_options;
209 CodedBitstreamFragment *au = &priv->current_access_unit;
212 if (priv->sei_needed) {
213 if (priv->aud_needed) {
214 err = vaapi_encode_h264_add_nal(avctx, au, &priv->aud);
217 priv->aud_needed = 0;
220 memset(&priv->sei, 0, sizeof(priv->sei));
221 priv->sei.nal_unit_header.nal_unit_type = H264_NAL_SEI;
224 if (pic->encode_order == 0 && opt->sei & SEI_IDENTIFIER) {
225 priv->sei.payload[i].payload_type = H264_SEI_TYPE_USER_DATA_UNREGISTERED;
226 priv->sei.payload[i].payload.user_data_unregistered = priv->identifier;
229 if (opt->sei & SEI_TIMING) {
230 if (pic->type == PICTURE_TYPE_IDR) {
231 priv->sei.payload[i].payload_type = H264_SEI_TYPE_BUFFERING_PERIOD;
232 priv->sei.payload[i].payload.buffering_period = priv->buffering_period;
235 priv->sei.payload[i].payload_type = H264_SEI_TYPE_PIC_TIMING;
236 priv->sei.payload[i].payload.pic_timing = priv->pic_timing;
239 if (opt->sei & SEI_RECOVERY_POINT && pic->type == PICTURE_TYPE_I) {
240 priv->sei.payload[i].payload_type = H264_SEI_TYPE_RECOVERY_POINT;
241 priv->sei.payload[i].payload.recovery_point = priv->recovery_point;
245 priv->sei.payload_count = i;
246 av_assert0(priv->sei.payload_count > 0);
248 err = vaapi_encode_h264_add_nal(avctx, au, &priv->sei);
251 priv->sei_needed = 0;
253 err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
257 ff_cbs_fragment_uninit(priv->cbc, au);
259 *type = VAEncPackedHeaderRawData;
266 ff_cbs_fragment_uninit(priv->cbc, au);
270 static int vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx)
272 VAAPIEncodeContext *ctx = avctx->priv_data;
273 VAAPIEncodeH264Context *priv = ctx->priv_data;
274 VAAPIEncodeH264Options *opt = ctx->codec_options;
275 H264RawSPS *sps = &priv->sps;
276 H264RawPPS *pps = &priv->pps;
277 VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
278 VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params;
280 memset(&priv->current_access_unit, 0,
281 sizeof(priv->current_access_unit));
283 memset(sps, 0, sizeof(*sps));
284 memset(pps, 0, sizeof(*pps));
286 sps->nal_unit_header.nal_ref_idc = 3;
287 sps->nal_unit_header.nal_unit_type = H264_NAL_SPS;
289 sps->profile_idc = avctx->profile & 0xff;
290 sps->constraint_set1_flag =
291 !!(avctx->profile & FF_PROFILE_H264_CONSTRAINED);
292 sps->constraint_set3_flag =
293 !!(avctx->profile & FF_PROFILE_H264_INTRA);
295 sps->level_idc = avctx->level;
297 sps->seq_parameter_set_id = 0;
298 sps->chroma_format_idc = 1;
300 sps->log2_max_frame_num_minus4 = 4;
301 sps->pic_order_cnt_type = 0;
302 sps->log2_max_pic_order_cnt_lsb_minus4 =
303 av_clip(av_log2(ctx->b_per_p + 1) - 2, 0, 12);
305 sps->max_num_ref_frames =
306 (avctx->profile & FF_PROFILE_H264_INTRA) ? 0 :
307 1 + (ctx->b_per_p > 0);
309 sps->pic_width_in_mbs_minus1 = priv->mb_width - 1;
310 sps->pic_height_in_map_units_minus1 = priv->mb_height - 1;
312 sps->frame_mbs_only_flag = 1;
313 sps->direct_8x8_inference_flag = 1;
315 if (avctx->width != 16 * priv->mb_width ||
316 avctx->height != 16 * priv->mb_height) {
317 sps->frame_cropping_flag = 1;
319 sps->frame_crop_left_offset = 0;
320 sps->frame_crop_right_offset =
321 (16 * priv->mb_width - avctx->width) / 2;
322 sps->frame_crop_top_offset = 0;
323 sps->frame_crop_bottom_offset =
324 (16 * priv->mb_height - avctx->height) / 2;
326 sps->frame_cropping_flag = 0;
329 sps->vui_parameters_present_flag = 1;
331 if (avctx->sample_aspect_ratio.num != 0 &&
332 avctx->sample_aspect_ratio.den != 0) {
333 static const AVRational sar_idc[] = {
335 { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
336 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
337 { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
338 { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
341 for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
342 if (avctx->sample_aspect_ratio.num == sar_idc[i].num &&
343 avctx->sample_aspect_ratio.den == sar_idc[i].den) {
344 sps->vui.aspect_ratio_idc = i;
348 if (i >= FF_ARRAY_ELEMS(sar_idc)) {
349 sps->vui.aspect_ratio_idc = 255;
350 sps->vui.sar_width = avctx->sample_aspect_ratio.num;
351 sps->vui.sar_height = avctx->sample_aspect_ratio.den;
353 sps->vui.aspect_ratio_info_present_flag = 1;
356 if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
357 avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
358 avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
359 avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
360 sps->vui.video_signal_type_present_flag = 1;
361 sps->vui.video_format = 5; // Unspecified.
362 sps->vui.video_full_range_flag =
363 avctx->color_range == AVCOL_RANGE_JPEG;
365 if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
366 avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
367 avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
368 sps->vui.colour_description_present_flag = 1;
369 sps->vui.colour_primaries = avctx->color_primaries;
370 sps->vui.transfer_characteristics = avctx->color_trc;
371 sps->vui.matrix_coefficients = avctx->colorspace;
374 sps->vui.video_format = 5;
375 sps->vui.video_full_range_flag = 0;
376 sps->vui.colour_primaries = avctx->color_primaries;
377 sps->vui.transfer_characteristics = avctx->color_trc;
378 sps->vui.matrix_coefficients = avctx->colorspace;
381 if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
382 sps->vui.chroma_loc_info_present_flag = 1;
383 sps->vui.chroma_sample_loc_type_top_field =
384 sps->vui.chroma_sample_loc_type_bottom_field =
385 avctx->chroma_sample_location - 1;
388 sps->vui.timing_info_present_flag = 1;
389 if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
390 sps->vui.num_units_in_tick = avctx->framerate.den;
391 sps->vui.time_scale = 2 * avctx->framerate.num;
392 sps->vui.fixed_frame_rate_flag = 1;
394 sps->vui.num_units_in_tick = avctx->time_base.num;
395 sps->vui.time_scale = 2 * avctx->time_base.den;
396 sps->vui.fixed_frame_rate_flag = 0;
399 if (opt->sei & SEI_TIMING) {
400 H264RawHRD *hrd = &sps->vui.nal_hrd_parameters;
402 sps->vui.nal_hrd_parameters_present_flag = 1;
404 hrd->cpb_cnt_minus1 = 0;
406 // Try to scale these to a sensible range so that the
407 // golomb encode of the value is not overlong.
408 hrd->bit_rate_scale =
409 av_clip_uintp2(av_log2(avctx->bit_rate) - 15 - 6, 4);
410 hrd->bit_rate_value_minus1[0] =
411 (avctx->bit_rate >> hrd->bit_rate_scale + 6) - 1;
413 hrd->cpb_size_scale =
414 av_clip_uintp2(av_log2(ctx->hrd_params.hrd.buffer_size) - 15 - 4, 4);
415 hrd->cpb_size_value_minus1[0] =
416 (ctx->hrd_params.hrd.buffer_size >> hrd->cpb_size_scale + 4) - 1;
418 // CBR mode as defined for the HRD cannot be achieved without filler
419 // data, so this flag cannot be set even with VAAPI CBR modes.
420 hrd->cbr_flag[0] = 0;
422 hrd->initial_cpb_removal_delay_length_minus1 = 23;
423 hrd->cpb_removal_delay_length_minus1 = 23;
424 hrd->dpb_output_delay_length_minus1 = 7;
425 hrd->time_offset_length = 0;
427 priv->buffering_period.seq_parameter_set_id = sps->seq_parameter_set_id;
429 // This calculation can easily overflow 32 bits.
430 priv->buffering_period.nal.initial_cpb_removal_delay[0] = 90000 *
431 (uint64_t)ctx->hrd_params.hrd.initial_buffer_fullness /
432 ctx->hrd_params.hrd.buffer_size;
433 priv->buffering_period.nal.initial_cpb_removal_delay_offset[0] = 0;
435 sps->vui.nal_hrd_parameters_present_flag = 0;
436 sps->vui.low_delay_hrd_flag = 1 - sps->vui.fixed_frame_rate_flag;
439 sps->vui.bitstream_restriction_flag = 1;
440 sps->vui.motion_vectors_over_pic_boundaries_flag = 1;
441 sps->vui.log2_max_mv_length_horizontal = 16;
442 sps->vui.log2_max_mv_length_vertical = 16;
443 sps->vui.max_num_reorder_frames = (ctx->b_per_p > 0);
444 sps->vui.max_dec_frame_buffering = sps->max_num_ref_frames;
446 pps->nal_unit_header.nal_ref_idc = 3;
447 pps->nal_unit_header.nal_unit_type = H264_NAL_PPS;
449 pps->pic_parameter_set_id = 0;
450 pps->seq_parameter_set_id = 0;
452 pps->entropy_coding_mode_flag =
453 !(sps->profile_idc == FF_PROFILE_H264_BASELINE ||
454 sps->profile_idc == FF_PROFILE_H264_EXTENDED ||
455 sps->profile_idc == FF_PROFILE_H264_CAVLC_444);
456 if (!opt->coder && pps->entropy_coding_mode_flag)
457 pps->entropy_coding_mode_flag = 0;
459 pps->num_ref_idx_l0_default_active_minus1 = 0;
460 pps->num_ref_idx_l1_default_active_minus1 = 0;
462 pps->pic_init_qp_minus26 = priv->fixed_qp_idr - 26;
464 if (sps->profile_idc == FF_PROFILE_H264_BASELINE ||
465 sps->profile_idc == FF_PROFILE_H264_EXTENDED ||
466 sps->profile_idc == FF_PROFILE_H264_MAIN) {
467 pps->more_rbsp_data = 0;
469 pps->more_rbsp_data = 1;
471 pps->transform_8x8_mode_flag = 1;
474 *vseq = (VAEncSequenceParameterBufferH264) {
475 .seq_parameter_set_id = sps->seq_parameter_set_id,
476 .level_idc = sps->level_idc,
477 .intra_period = avctx->gop_size,
478 .intra_idr_period = avctx->gop_size,
479 .ip_period = ctx->b_per_p + 1,
481 .bits_per_second = avctx->bit_rate,
482 .max_num_ref_frames = sps->max_num_ref_frames,
483 .picture_width_in_mbs = sps->pic_width_in_mbs_minus1 + 1,
484 .picture_height_in_mbs = sps->pic_height_in_map_units_minus1 + 1,
487 .chroma_format_idc = sps->chroma_format_idc,
488 .frame_mbs_only_flag = sps->frame_mbs_only_flag,
489 .mb_adaptive_frame_field_flag = sps->mb_adaptive_frame_field_flag,
490 .seq_scaling_matrix_present_flag = sps->seq_scaling_matrix_present_flag,
491 .direct_8x8_inference_flag = sps->direct_8x8_inference_flag,
492 .log2_max_frame_num_minus4 = sps->log2_max_frame_num_minus4,
493 .pic_order_cnt_type = sps->pic_order_cnt_type,
494 .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_pic_order_cnt_lsb_minus4,
495 .delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag,
498 .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
499 .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
501 .frame_cropping_flag = sps->frame_cropping_flag,
502 .frame_crop_left_offset = sps->frame_crop_left_offset,
503 .frame_crop_right_offset = sps->frame_crop_right_offset,
504 .frame_crop_top_offset = sps->frame_crop_top_offset,
505 .frame_crop_bottom_offset = sps->frame_crop_bottom_offset,
507 .vui_parameters_present_flag = sps->vui_parameters_present_flag,
510 .aspect_ratio_info_present_flag = sps->vui.aspect_ratio_info_present_flag,
511 .timing_info_present_flag = sps->vui.timing_info_present_flag,
512 .bitstream_restriction_flag = sps->vui.bitstream_restriction_flag,
513 .log2_max_mv_length_horizontal = sps->vui.log2_max_mv_length_horizontal,
514 .log2_max_mv_length_vertical = sps->vui.log2_max_mv_length_vertical,
517 .aspect_ratio_idc = sps->vui.aspect_ratio_idc,
518 .sar_width = sps->vui.sar_width,
519 .sar_height = sps->vui.sar_height,
520 .num_units_in_tick = sps->vui.num_units_in_tick,
521 .time_scale = sps->vui.time_scale,
524 *vpic = (VAEncPictureParameterBufferH264) {
526 .picture_id = VA_INVALID_ID,
527 .flags = VA_PICTURE_H264_INVALID,
530 .coded_buf = VA_INVALID_ID,
532 .pic_parameter_set_id = pps->pic_parameter_set_id,
533 .seq_parameter_set_id = pps->seq_parameter_set_id,
535 .pic_init_qp = pps->pic_init_qp_minus26 + 26,
536 .num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1,
537 .num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1,
539 .chroma_qp_index_offset = pps->chroma_qp_index_offset,
540 .second_chroma_qp_index_offset = pps->second_chroma_qp_index_offset,
543 .entropy_coding_mode_flag = pps->entropy_coding_mode_flag,
544 .weighted_pred_flag = pps->weighted_pred_flag,
545 .weighted_bipred_idc = pps->weighted_bipred_idc,
546 .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
547 .transform_8x8_mode_flag = pps->transform_8x8_mode_flag,
548 .deblocking_filter_control_present_flag =
549 pps->deblocking_filter_control_present_flag,
550 .redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present_flag,
551 .pic_order_present_flag =
552 pps->bottom_field_pic_order_in_frame_present_flag,
553 .pic_scaling_matrix_present_flag = pps->pic_scaling_matrix_present_flag,
560 static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
561 VAAPIEncodePicture *pic)
563 VAAPIEncodeContext *ctx = avctx->priv_data;
564 VAAPIEncodeH264Context *priv = ctx->priv_data;
565 VAAPIEncodeH264Options *opt = ctx->codec_options;
566 H264RawSPS *sps = &priv->sps;
567 VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
570 memset(&priv->current_access_unit, 0,
571 sizeof(priv->current_access_unit));
573 if (pic->type == PICTURE_TYPE_IDR) {
574 av_assert0(pic->display_order == pic->encode_order);
576 priv->next_frame_num = 1;
578 priv->last_idr_frame = pic->display_order;
579 ++priv->idr_pic_count;
581 priv->slice_type = 7;
582 priv->primary_pic_type = 0;
584 priv->frame_num = priv->next_frame_num;
586 if (pic->type != PICTURE_TYPE_B) {
587 // Reference picture, so frame_num advances.
588 priv->next_frame_num = (priv->frame_num + 1) &
589 ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
593 if (pic->type == PICTURE_TYPE_I) {
594 priv->slice_type = 7;
595 priv->primary_pic_type = 0;
596 } else if (pic->type == PICTURE_TYPE_P) {
597 priv->slice_type = 5;
598 priv->primary_pic_type = 1;
600 priv->slice_type = 6;
601 priv->primary_pic_type = 2;
604 priv->pic_order_cnt = pic->display_order - priv->last_idr_frame;
605 priv->dpb_delay = pic->display_order - pic->encode_order + 1;
608 priv->aud_needed = 1;
609 priv->aud.nal_unit_header.nal_unit_type = H264_NAL_AUD;
610 priv->aud.primary_pic_type = priv->primary_pic_type;
612 priv->aud_needed = 0;
615 if (opt->sei & SEI_IDENTIFIER && pic->encode_order == 0)
616 priv->sei_needed = 1;
618 if (opt->sei & SEI_TIMING) {
619 memset(&priv->pic_timing, 0, sizeof(priv->pic_timing));
621 priv->pic_timing.cpb_removal_delay = 2 * priv->cpb_delay;
622 priv->pic_timing.dpb_output_delay = 2 * priv->dpb_delay;
624 priv->sei_needed = 1;
627 if (opt->sei & SEI_RECOVERY_POINT && pic->type == PICTURE_TYPE_I) {
628 priv->recovery_point.recovery_frame_cnt = 0;
629 priv->recovery_point.exact_match_flag = 1;
630 priv->recovery_point.broken_link_flag = ctx->b_per_p > 0;
632 priv->sei_needed = 1;
635 vpic->CurrPic = (VAPictureH264) {
636 .picture_id = pic->recon_surface,
637 .frame_idx = priv->frame_num,
639 .TopFieldOrderCnt = priv->pic_order_cnt,
640 .BottomFieldOrderCnt = priv->pic_order_cnt,
643 for (i = 0; i < pic->nb_refs; i++) {
644 VAAPIEncodePicture *ref = pic->refs[i];
645 unsigned int frame_num = (ref->encode_order - priv->last_idr_frame) &
646 ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
647 unsigned int pic_order_cnt = ref->display_order - priv->last_idr_frame;
649 av_assert0(ref && ref->encode_order < pic->encode_order);
650 vpic->ReferenceFrames[i] = (VAPictureH264) {
651 .picture_id = ref->recon_surface,
652 .frame_idx = frame_num,
653 .flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE,
654 .TopFieldOrderCnt = pic_order_cnt,
655 .BottomFieldOrderCnt = pic_order_cnt,
658 for (; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
659 vpic->ReferenceFrames[i] = (VAPictureH264) {
660 .picture_id = VA_INVALID_ID,
661 .flags = VA_PICTURE_H264_INVALID,
665 vpic->coded_buf = pic->output_buffer;
667 vpic->frame_num = priv->frame_num;
669 vpic->pic_fields.bits.idr_pic_flag = (pic->type == PICTURE_TYPE_IDR);
670 vpic->pic_fields.bits.reference_pic_flag = (pic->type != PICTURE_TYPE_B);
677 static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx,
678 VAAPIEncodePicture *pic,
679 VAAPIEncodeSlice *slice)
681 VAAPIEncodeContext *ctx = avctx->priv_data;
682 VAAPIEncodeH264Context *priv = ctx->priv_data;
683 H264RawSPS *sps = &priv->sps;
684 H264RawPPS *pps = &priv->pps;
685 H264RawSliceHeader *sh = &priv->slice.header;
686 VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
687 VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params;
690 if (pic->type == PICTURE_TYPE_IDR) {
691 sh->nal_unit_header.nal_unit_type = H264_NAL_IDR_SLICE;
692 sh->nal_unit_header.nal_ref_idc = 3;
694 sh->nal_unit_header.nal_unit_type = H264_NAL_SLICE;
695 sh->nal_unit_header.nal_ref_idc = pic->type != PICTURE_TYPE_B;
698 // Only one slice per frame.
699 sh->first_mb_in_slice = 0;
700 sh->slice_type = priv->slice_type;
702 sh->pic_parameter_set_id = pps->pic_parameter_set_id;
704 sh->frame_num = priv->frame_num;
705 sh->idr_pic_id = priv->idr_pic_count;
707 sh->pic_order_cnt_lsb = priv->pic_order_cnt &
708 ((1 << (4 + sps->log2_max_pic_order_cnt_lsb_minus4)) - 1);
710 sh->direct_spatial_mv_pred_flag = 1;
712 if (pic->type == PICTURE_TYPE_B)
713 sh->slice_qp_delta = priv->fixed_qp_b - (pps->pic_init_qp_minus26 + 26);
714 else if (pic->type == PICTURE_TYPE_P)
715 sh->slice_qp_delta = priv->fixed_qp_p - (pps->pic_init_qp_minus26 + 26);
717 sh->slice_qp_delta = priv->fixed_qp_idr - (pps->pic_init_qp_minus26 + 26);
720 vslice->macroblock_address = sh->first_mb_in_slice;
721 vslice->num_macroblocks = priv->mb_width * priv->mb_height;
723 vslice->macroblock_info = VA_INVALID_ID;
725 vslice->slice_type = sh->slice_type % 5;
726 vslice->pic_parameter_set_id = sh->pic_parameter_set_id;
727 vslice->idr_pic_id = sh->idr_pic_id;
729 vslice->pic_order_cnt_lsb = sh->pic_order_cnt_lsb;
731 vslice->direct_spatial_mv_pred_flag = sh->direct_spatial_mv_pred_flag;
733 for (i = 0; i < FF_ARRAY_ELEMS(vslice->RefPicList0); i++) {
734 vslice->RefPicList0[i].picture_id = VA_INVALID_ID;
735 vslice->RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
736 vslice->RefPicList1[i].picture_id = VA_INVALID_ID;
737 vslice->RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
740 av_assert0(pic->nb_refs <= 2);
741 if (pic->nb_refs >= 1) {
742 // Backward reference for P- or B-frame.
743 av_assert0(pic->type == PICTURE_TYPE_P ||
744 pic->type == PICTURE_TYPE_B);
745 vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
747 if (pic->nb_refs >= 2) {
748 // Forward reference for B-frame.
749 av_assert0(pic->type == PICTURE_TYPE_B);
750 vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
753 vslice->slice_qp_delta = sh->slice_qp_delta;
758 static av_cold int vaapi_encode_h264_configure(AVCodecContext *avctx)
760 VAAPIEncodeContext *ctx = avctx->priv_data;
761 VAAPIEncodeH264Context *priv = ctx->priv_data;
762 VAAPIEncodeH264Options *opt = ctx->codec_options;
765 err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_H264, avctx);
769 priv->mb_width = FFALIGN(avctx->width, 16) / 16;
770 priv->mb_height = FFALIGN(avctx->height, 16) / 16;
772 if (ctx->va_rc_mode == VA_RC_CQP) {
773 priv->fixed_qp_p = opt->qp;
774 if (avctx->i_quant_factor > 0.0)
775 priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
776 avctx->i_quant_offset) + 0.5);
778 priv->fixed_qp_idr = priv->fixed_qp_p;
779 if (avctx->b_quant_factor > 0.0)
780 priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
781 avctx->b_quant_offset) + 0.5);
783 priv->fixed_qp_b = priv->fixed_qp_p;
785 opt->sei &= ~SEI_TIMING;
787 av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
788 "%d / %d / %d for IDR- / P- / B-frames.\n",
789 priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
791 } else if (ctx->va_rc_mode == VA_RC_CBR ||
792 ctx->va_rc_mode == VA_RC_VBR) {
793 // These still need to be set for pic_init_qp/slice_qp_delta.
794 priv->fixed_qp_idr = 26;
795 priv->fixed_qp_p = 26;
796 priv->fixed_qp_b = 26;
798 av_log(avctx, AV_LOG_DEBUG, "Using %s-bitrate = %"PRId64" bps.\n",
799 ctx->va_rc_mode == VA_RC_CBR ? "constant" : "variable",
803 av_assert0(0 && "Invalid RC mode.");
806 if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
807 avctx->compression_level = opt->quality;
809 if (opt->sei & SEI_IDENTIFIER) {
810 const char *lavc = LIBAVCODEC_IDENT;
811 const char *vaapi = VA_VERSION_S;
815 memcpy(priv->identifier.uuid_iso_iec_11578,
816 vaapi_encode_h264_sei_identifier_uuid,
817 sizeof(priv->identifier.uuid_iso_iec_11578));
819 driver = vaQueryVendorString(ctx->hwctx->display);
821 driver = "unknown driver";
823 len = snprintf(NULL, 0, "%s / VAAPI %s / %s", lavc, vaapi, driver);
825 priv->identifier_string = av_malloc(len + 1);
826 if (!priv->identifier_string)
827 return AVERROR(ENOMEM);
829 snprintf(priv->identifier_string, len + 1,
830 "%s / VAAPI %s / %s", lavc, vaapi, driver);
832 priv->identifier.data = priv->identifier_string;
833 priv->identifier.data_length = len + 1;
840 static const VAAPIEncodeType vaapi_encode_type_h264 = {
841 .priv_data_size = sizeof(VAAPIEncodeH264Context),
843 .configure = &vaapi_encode_h264_configure,
845 .sequence_params_size = sizeof(VAEncSequenceParameterBufferH264),
846 .init_sequence_params = &vaapi_encode_h264_init_sequence_params,
848 .picture_params_size = sizeof(VAEncPictureParameterBufferH264),
849 .init_picture_params = &vaapi_encode_h264_init_picture_params,
851 .slice_params_size = sizeof(VAEncSliceParameterBufferH264),
852 .init_slice_params = &vaapi_encode_h264_init_slice_params,
854 .sequence_header_type = VAEncPackedHeaderSequence,
855 .write_sequence_header = &vaapi_encode_h264_write_sequence_header,
857 .slice_header_type = VAEncPackedHeaderH264_Slice,
858 .write_slice_header = &vaapi_encode_h264_write_slice_header,
860 .write_extra_header = &vaapi_encode_h264_write_extra_header,
863 static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
865 VAAPIEncodeContext *ctx = avctx->priv_data;
866 VAAPIEncodeH264Options *opt =
867 (VAAPIEncodeH264Options*)ctx->codec_options_data;
869 ctx->codec = &vaapi_encode_type_h264;
871 switch (avctx->profile) {
872 case FF_PROFILE_H264_BASELINE:
873 av_log(avctx, AV_LOG_WARNING, "H.264 baseline profile is not "
874 "supported, using constrained baseline profile instead.\n");
875 avctx->profile = FF_PROFILE_H264_CONSTRAINED_BASELINE;
876 case FF_PROFILE_H264_CONSTRAINED_BASELINE:
877 ctx->va_profile = VAProfileH264ConstrainedBaseline;
878 if (avctx->max_b_frames != 0) {
879 avctx->max_b_frames = 0;
880 av_log(avctx, AV_LOG_WARNING, "H.264 constrained baseline profile "
881 "doesn't support encoding with B frames, disabling them.\n");
884 case FF_PROFILE_H264_MAIN:
885 ctx->va_profile = VAProfileH264Main;
887 case FF_PROFILE_H264_EXTENDED:
888 av_log(avctx, AV_LOG_ERROR, "H.264 extended profile "
889 "is not supported.\n");
890 return AVERROR_PATCHWELCOME;
891 case FF_PROFILE_UNKNOWN:
892 case FF_PROFILE_H264_HIGH:
893 ctx->va_profile = VAProfileH264High;
895 case FF_PROFILE_H264_HIGH_10:
896 case FF_PROFILE_H264_HIGH_10_INTRA:
897 av_log(avctx, AV_LOG_ERROR, "H.264 10-bit profiles "
898 "are not supported.\n");
899 return AVERROR_PATCHWELCOME;
900 case FF_PROFILE_H264_HIGH_422:
901 case FF_PROFILE_H264_HIGH_422_INTRA:
902 case FF_PROFILE_H264_HIGH_444:
903 case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
904 case FF_PROFILE_H264_HIGH_444_INTRA:
905 case FF_PROFILE_H264_CAVLC_444:
906 av_log(avctx, AV_LOG_ERROR, "H.264 non-4:2:0 profiles "
907 "are not supported.\n");
908 return AVERROR_PATCHWELCOME;
910 av_log(avctx, AV_LOG_ERROR, "Unknown H.264 profile %d.\n",
912 return AVERROR(EINVAL);
914 if (opt->low_power) {
915 #if VA_CHECK_VERSION(0, 39, 2)
916 ctx->va_entrypoint = VAEntrypointEncSliceLP;
918 av_log(avctx, AV_LOG_ERROR, "Low-power encoding is not "
919 "supported with this VAAPI version.\n");
920 return AVERROR(EINVAL);
923 ctx->va_entrypoint = VAEntrypointEncSlice;
926 // Only 8-bit encode is supported.
927 ctx->va_rt_format = VA_RT_FORMAT_YUV420;
929 if (avctx->bit_rate > 0) {
930 if (avctx->rc_max_rate == avctx->bit_rate)
931 ctx->va_rc_mode = VA_RC_CBR;
933 ctx->va_rc_mode = VA_RC_VBR;
935 ctx->va_rc_mode = VA_RC_CQP;
937 ctx->va_packed_headers =
938 VA_ENC_PACKED_HEADER_SEQUENCE | // SPS and PPS.
939 VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
940 VA_ENC_PACKED_HEADER_MISC; // SEI.
942 ctx->surface_width = FFALIGN(avctx->width, 16);
943 ctx->surface_height = FFALIGN(avctx->height, 16);
945 return ff_vaapi_encode_init(avctx);
948 static av_cold int vaapi_encode_h264_close(AVCodecContext *avctx)
950 VAAPIEncodeContext *ctx = avctx->priv_data;
951 VAAPIEncodeH264Context *priv = ctx->priv_data;
954 ff_cbs_close(&priv->cbc);
955 av_freep(&priv->identifier_string);
958 return ff_vaapi_encode_close(avctx);
961 #define OFFSET(x) (offsetof(VAAPIEncodeContext, codec_options_data) + \
962 offsetof(VAAPIEncodeH264Options, x))
963 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
964 static const AVOption vaapi_encode_h264_options[] = {
965 { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
966 OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 20 }, 0, 52, FLAGS },
967 { "quality", "Set encode quality (trades off against speed, higher is faster)",
968 OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 8, FLAGS },
969 { "low_power", "Use low-power encoding mode (experimental: only supported "
970 "on some platforms, does not support all features)",
971 OFFSET(low_power), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
972 { "coder", "Entropy coder type",
973 OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "coder" },
974 { "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
975 { "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
976 { "vlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
977 { "ac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
979 { "aud", "Include AUD",
980 OFFSET(aud), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
982 { "sei", "Set SEI to include",
983 OFFSET(sei), AV_OPT_TYPE_FLAGS,
984 { .i64 = SEI_IDENTIFIER | SEI_TIMING | SEI_RECOVERY_POINT },
985 0, INT_MAX, FLAGS, "sei" },
986 { "identifier", "Include encoder version identifier",
987 0, AV_OPT_TYPE_CONST, { .i64 = SEI_IDENTIFIER },
988 INT_MIN, INT_MAX, FLAGS, "sei" },
989 { "timing", "Include timing parameters (buffering_period and pic_timing)",
990 0, AV_OPT_TYPE_CONST, { .i64 = SEI_TIMING },
991 INT_MIN, INT_MAX, FLAGS, "sei" },
992 { "recovery_point", "Include recovery points where appropriate",
993 0, AV_OPT_TYPE_CONST, { .i64 = SEI_RECOVERY_POINT },
994 INT_MIN, INT_MAX, FLAGS, "sei" },
998 static const AVCodecDefault vaapi_encode_h264_defaults[] = {
999 { "profile", "100" },
1004 { "i_qfactor", "1" },
1005 { "i_qoffset", "0" },
1006 { "b_qfactor", "6/5" },
1007 { "b_qoffset", "0" },
1012 static const AVClass vaapi_encode_h264_class = {
1013 .class_name = "h264_vaapi",
1014 .item_name = av_default_item_name,
1015 .option = vaapi_encode_h264_options,
1016 .version = LIBAVUTIL_VERSION_INT,
1019 AVCodec ff_h264_vaapi_encoder = {
1020 .name = "h264_vaapi",
1021 .long_name = NULL_IF_CONFIG_SMALL("H.264/AVC (VAAPI)"),
1022 .type = AVMEDIA_TYPE_VIDEO,
1023 .id = AV_CODEC_ID_H264,
1024 .priv_data_size = (sizeof(VAAPIEncodeContext) +
1025 sizeof(VAAPIEncodeH264Options)),
1026 .init = &vaapi_encode_h264_init,
1027 .encode2 = &ff_vaapi_encode2,
1028 .close = &vaapi_encode_h264_close,
1029 .priv_class = &vaapi_encode_h264_class,
1030 .capabilities = AV_CODEC_CAP_DELAY,
1031 .defaults = vaapi_encode_h264_defaults,
1032 .pix_fmts = (const enum AVPixelFormat[]) {