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,
42 // Random (version 4) ISO 11578 UUID.
43 static const uint8_t vaapi_encode_h264_sei_identifier_uuid[16] = {
44 0x59, 0x94, 0x8b, 0x28, 0x11, 0xec, 0x45, 0xaf,
45 0x96, 0x75, 0x19, 0xd4, 0x1f, 0xea, 0xa9, 0x4d,
48 typedef struct VAAPIEncodeH264Context {
62 H264RawSEIBufferingPeriod buffering_period;
63 H264RawSEIPicTiming pic_timing;
64 H264RawSEIUserDataUnregistered identifier;
65 char *identifier_string;
70 int64_t last_idr_frame;
71 int64_t idr_pic_count;
79 CodedBitstreamContext *cbc;
80 CodedBitstreamFragment current_access_unit;
83 } VAAPIEncodeH264Context;
85 typedef struct VAAPIEncodeH264Options {
89 // Entropy encoder type.
93 } VAAPIEncodeH264Options;
96 static int vaapi_encode_h264_write_access_unit(AVCodecContext *avctx,
97 char *data, size_t *data_len,
98 CodedBitstreamFragment *au)
100 VAAPIEncodeContext *ctx = avctx->priv_data;
101 VAAPIEncodeH264Context *priv = ctx->priv_data;
104 err = ff_cbs_write_fragment_data(priv->cbc, au);
106 av_log(avctx, AV_LOG_ERROR, "Failed to write packed header.\n");
110 if (*data_len < 8 * au->data_size - au->data_bit_padding) {
111 av_log(avctx, AV_LOG_ERROR, "Access unit too large: "
112 "%zu < %zu.\n", *data_len,
113 8 * au->data_size - au->data_bit_padding);
114 return AVERROR(ENOSPC);
117 memcpy(data, au->data, au->data_size);
118 *data_len = 8 * au->data_size - au->data_bit_padding;
123 static int vaapi_encode_h264_add_nal(AVCodecContext *avctx,
124 CodedBitstreamFragment *au,
127 VAAPIEncodeContext *ctx = avctx->priv_data;
128 VAAPIEncodeH264Context *priv = ctx->priv_data;
129 H264RawNALUnitHeader *header = nal_unit;
132 err = ff_cbs_insert_unit_content(priv->cbc, au, -1,
133 header->nal_unit_type, nal_unit);
135 av_log(avctx, AV_LOG_ERROR, "Failed to add NAL unit: "
136 "type = %d.\n", header->nal_unit_type);
143 static int vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx,
144 char *data, size_t *data_len)
146 VAAPIEncodeContext *ctx = avctx->priv_data;
147 VAAPIEncodeH264Context *priv = ctx->priv_data;
148 CodedBitstreamFragment *au = &priv->current_access_unit;
151 if (priv->aud_needed) {
152 err = vaapi_encode_h264_add_nal(avctx, au, &priv->aud);
155 priv->aud_needed = 0;
158 err = vaapi_encode_h264_add_nal(avctx, au, &priv->sps);
162 err = vaapi_encode_h264_add_nal(avctx, au, &priv->pps);
166 err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
168 ff_cbs_fragment_uninit(priv->cbc, au);
172 static int vaapi_encode_h264_write_slice_header(AVCodecContext *avctx,
173 VAAPIEncodePicture *pic,
174 VAAPIEncodeSlice *slice,
175 char *data, size_t *data_len)
177 VAAPIEncodeContext *ctx = avctx->priv_data;
178 VAAPIEncodeH264Context *priv = ctx->priv_data;
179 CodedBitstreamFragment *au = &priv->current_access_unit;
182 if (priv->aud_needed) {
183 err = vaapi_encode_h264_add_nal(avctx, au, &priv->aud);
186 priv->aud_needed = 0;
189 err = vaapi_encode_h264_add_nal(avctx, au, &priv->slice);
193 err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
195 ff_cbs_fragment_uninit(priv->cbc, au);
199 static int vaapi_encode_h264_write_extra_header(AVCodecContext *avctx,
200 VAAPIEncodePicture *pic,
201 int index, int *type,
202 char *data, size_t *data_len)
204 VAAPIEncodeContext *ctx = avctx->priv_data;
205 VAAPIEncodeH264Context *priv = ctx->priv_data;
206 VAAPIEncodeH264Options *opt = ctx->codec_options;
207 CodedBitstreamFragment *au = &priv->current_access_unit;
210 if (priv->sei_needed) {
211 if (priv->aud_needed) {
212 vaapi_encode_h264_add_nal(avctx, au, &priv->aud);
213 priv->aud_needed = 0;
216 memset(&priv->sei, 0, sizeof(priv->sei));
217 priv->sei.nal_unit_header.nal_unit_type = H264_NAL_SEI;
220 if (pic->encode_order == 0 && opt->sei & SEI_IDENTIFIER) {
221 priv->sei.payload[i].payload_type = H264_SEI_TYPE_USER_DATA_UNREGISTERED;
222 priv->sei.payload[i].payload.user_data_unregistered = priv->identifier;
225 if (opt->sei & SEI_TIMING) {
226 if (pic->type == PICTURE_TYPE_IDR) {
227 priv->sei.payload[i].payload_type = H264_SEI_TYPE_BUFFERING_PERIOD;
228 priv->sei.payload[i].payload.buffering_period = priv->buffering_period;
231 priv->sei.payload[i].payload_type = H264_SEI_TYPE_PIC_TIMING;
232 priv->sei.payload[i].payload.pic_timing = priv->pic_timing;
236 priv->sei.payload_count = i;
237 av_assert0(priv->sei.payload_count > 0);
239 err = vaapi_encode_h264_add_nal(avctx, au, &priv->sei);
242 priv->sei_needed = 0;
244 err = vaapi_encode_h264_write_access_unit(avctx, data, data_len, au);
248 ff_cbs_fragment_uninit(priv->cbc, au);
250 *type = VAEncPackedHeaderH264_SEI;
257 ff_cbs_fragment_uninit(priv->cbc, au);
261 static int vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx)
263 VAAPIEncodeContext *ctx = avctx->priv_data;
264 VAAPIEncodeH264Context *priv = ctx->priv_data;
265 VAAPIEncodeH264Options *opt = ctx->codec_options;
266 H264RawSPS *sps = &priv->sps;
267 H264RawPPS *pps = &priv->pps;
268 VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params;
269 VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params;
271 memset(&priv->current_access_unit, 0,
272 sizeof(priv->current_access_unit));
274 memset(sps, 0, sizeof(*sps));
275 memset(pps, 0, sizeof(*pps));
277 sps->nal_unit_header.nal_ref_idc = 3;
278 sps->nal_unit_header.nal_unit_type = H264_NAL_SPS;
280 sps->profile_idc = avctx->profile & 0xff;
281 sps->constraint_set1_flag =
282 !!(avctx->profile & FF_PROFILE_H264_CONSTRAINED);
283 sps->constraint_set3_flag =
284 !!(avctx->profile & FF_PROFILE_H264_INTRA);
286 sps->level_idc = avctx->level;
288 sps->seq_parameter_set_id = 0;
289 sps->chroma_format_idc = 1;
291 sps->log2_max_frame_num_minus4 = 4;
292 sps->pic_order_cnt_type = 0;
293 sps->log2_max_pic_order_cnt_lsb_minus4 =
294 av_clip(av_log2(ctx->b_per_p + 1) - 2, 0, 12);
296 sps->max_num_ref_frames =
297 (avctx->profile & FF_PROFILE_H264_INTRA) ? 0 :
298 1 + (ctx->b_per_p > 0);
300 sps->pic_width_in_mbs_minus1 = priv->mb_width - 1;
301 sps->pic_height_in_map_units_minus1 = priv->mb_height - 1;
303 sps->frame_mbs_only_flag = 1;
304 sps->direct_8x8_inference_flag = 1;
306 if (avctx->width != 16 * priv->mb_width ||
307 avctx->height != 16 * priv->mb_height) {
308 sps->frame_cropping_flag = 1;
310 sps->frame_crop_left_offset = 0;
311 sps->frame_crop_right_offset =
312 (16 * priv->mb_width - avctx->width) / 2;
313 sps->frame_crop_top_offset = 0;
314 sps->frame_crop_bottom_offset =
315 (16 * priv->mb_height - avctx->height) / 2;
317 sps->frame_cropping_flag = 0;
320 sps->vui_parameters_present_flag = 1;
322 if (avctx->sample_aspect_ratio.num != 0 &&
323 avctx->sample_aspect_ratio.den != 0) {
324 static const AVRational sar_idc[] = {
326 { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
327 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
328 { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
329 { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
332 for (i = 0; i < FF_ARRAY_ELEMS(sar_idc); i++) {
333 if (avctx->sample_aspect_ratio.num == sar_idc[i].num &&
334 avctx->sample_aspect_ratio.den == sar_idc[i].den) {
335 sps->vui.aspect_ratio_idc = i;
339 if (i >= FF_ARRAY_ELEMS(sar_idc)) {
340 sps->vui.aspect_ratio_idc = 255;
341 sps->vui.sar_width = avctx->sample_aspect_ratio.num;
342 sps->vui.sar_height = avctx->sample_aspect_ratio.den;
344 sps->vui.aspect_ratio_info_present_flag = 1;
347 if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED ||
348 avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
349 avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
350 avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
351 sps->vui.video_signal_type_present_flag = 1;
352 sps->vui.video_format = 5; // Unspecified.
353 sps->vui.video_full_range_flag =
354 avctx->color_range == AVCOL_RANGE_JPEG;
356 if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED ||
357 avctx->color_trc != AVCOL_TRC_UNSPECIFIED ||
358 avctx->colorspace != AVCOL_SPC_UNSPECIFIED) {
359 sps->vui.colour_description_present_flag = 1;
360 sps->vui.colour_primaries = avctx->color_primaries;
361 sps->vui.transfer_characteristics = avctx->color_trc;
362 sps->vui.matrix_coefficients = avctx->colorspace;
365 sps->vui.video_format = 5;
366 sps->vui.video_full_range_flag = 0;
367 sps->vui.colour_primaries = avctx->color_primaries;
368 sps->vui.transfer_characteristics = avctx->color_trc;
369 sps->vui.matrix_coefficients = avctx->colorspace;
372 if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
373 sps->vui.chroma_loc_info_present_flag = 1;
374 sps->vui.chroma_sample_loc_type_top_field =
375 sps->vui.chroma_sample_loc_type_bottom_field =
376 avctx->chroma_sample_location - 1;
379 sps->vui.timing_info_present_flag = 1;
380 if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
381 sps->vui.num_units_in_tick = avctx->framerate.den;
382 sps->vui.time_scale = 2 * avctx->framerate.num;
383 sps->vui.fixed_frame_rate_flag = 1;
385 sps->vui.num_units_in_tick = avctx->time_base.num;
386 sps->vui.time_scale = 2 * avctx->time_base.den;
387 sps->vui.fixed_frame_rate_flag = 0;
390 if (opt->sei & SEI_TIMING) {
391 H264RawHRD *hrd = &sps->vui.nal_hrd_parameters;
393 sps->vui.nal_hrd_parameters_present_flag = 1;
395 hrd->cpb_cnt_minus1 = 0;
397 // Try to scale these to a sensible range so that the
398 // golomb encode of the value is not overlong.
399 hrd->bit_rate_scale =
400 av_clip_uintp2(av_log2(avctx->bit_rate) - 15 - 6, 4);
401 hrd->bit_rate_value_minus1[0] =
402 (avctx->bit_rate >> hrd->bit_rate_scale + 6) - 1;
404 hrd->cpb_size_scale =
405 av_clip_uintp2(av_log2(ctx->hrd_params.hrd.buffer_size) - 15 - 4, 4);
406 hrd->cpb_size_value_minus1[0] =
407 (ctx->hrd_params.hrd.buffer_size >> hrd->cpb_size_scale + 4) - 1;
409 // CBR mode as defined for the HRD cannot be achieved without filler
410 // data, so this flag cannot be set even with VAAPI CBR modes.
411 hrd->cbr_flag[0] = 0;
413 hrd->initial_cpb_removal_delay_length_minus1 = 23;
414 hrd->cpb_removal_delay_length_minus1 = 23;
415 hrd->dpb_output_delay_length_minus1 = 7;
416 hrd->time_offset_length = 0;
418 priv->buffering_period.seq_parameter_set_id = sps->seq_parameter_set_id;
420 // This calculation can easily overflow 32 bits.
421 priv->buffering_period.nal.initial_cpb_removal_delay[0] = 90000 *
422 (uint64_t)ctx->hrd_params.hrd.initial_buffer_fullness /
423 ctx->hrd_params.hrd.buffer_size;
424 priv->buffering_period.nal.initial_cpb_removal_delay_offset[0] = 0;
426 sps->vui.nal_hrd_parameters_present_flag = 0;
427 sps->vui.low_delay_hrd_flag = 1 - sps->vui.fixed_frame_rate_flag;
430 sps->vui.bitstream_restriction_flag = 1;
431 sps->vui.motion_vectors_over_pic_boundaries_flag = 1;
432 sps->vui.log2_max_mv_length_horizontal = 16;
433 sps->vui.log2_max_mv_length_vertical = 16;
434 sps->vui.max_num_reorder_frames = (ctx->b_per_p > 0);
435 sps->vui.max_dec_frame_buffering = vseq->max_num_ref_frames;
437 pps->nal_unit_header.nal_ref_idc = 3;
438 pps->nal_unit_header.nal_unit_type = H264_NAL_PPS;
440 pps->pic_parameter_set_id = 0;
441 pps->seq_parameter_set_id = 0;
443 pps->entropy_coding_mode_flag =
444 !(sps->profile_idc == FF_PROFILE_H264_BASELINE ||
445 sps->profile_idc == FF_PROFILE_H264_EXTENDED ||
446 sps->profile_idc == FF_PROFILE_H264_CAVLC_444);
447 if (!opt->coder && pps->entropy_coding_mode_flag)
448 pps->entropy_coding_mode_flag = 0;
450 pps->num_ref_idx_l0_default_active_minus1 = 0;
451 pps->num_ref_idx_l1_default_active_minus1 = 0;
453 pps->pic_init_qp_minus26 = priv->fixed_qp_idr - 26;
455 if (sps->profile_idc == FF_PROFILE_H264_BASELINE ||
456 sps->profile_idc == FF_PROFILE_H264_EXTENDED ||
457 sps->profile_idc == FF_PROFILE_H264_MAIN) {
458 pps->more_rbsp_data = 0;
460 pps->more_rbsp_data = 1;
462 pps->transform_8x8_mode_flag = 1;
465 *vseq = (VAEncSequenceParameterBufferH264) {
466 .seq_parameter_set_id = sps->seq_parameter_set_id,
467 .level_idc = sps->level_idc,
468 .intra_period = avctx->gop_size,
469 .intra_idr_period = avctx->gop_size,
470 .ip_period = ctx->b_per_p + 1,
472 .bits_per_second = avctx->bit_rate,
473 .max_num_ref_frames = sps->max_num_ref_frames,
474 .picture_width_in_mbs = sps->pic_width_in_mbs_minus1 + 1,
475 .picture_height_in_mbs = sps->pic_height_in_map_units_minus1 + 1,
478 .chroma_format_idc = sps->chroma_format_idc,
479 .frame_mbs_only_flag = sps->frame_mbs_only_flag,
480 .mb_adaptive_frame_field_flag = sps->mb_adaptive_frame_field_flag,
481 .seq_scaling_matrix_present_flag = sps->seq_scaling_matrix_present_flag,
482 .direct_8x8_inference_flag = sps->direct_8x8_inference_flag,
483 .log2_max_frame_num_minus4 = sps->log2_max_frame_num_minus4,
484 .pic_order_cnt_type = sps->pic_order_cnt_type,
485 .log2_max_pic_order_cnt_lsb_minus4 = sps->log2_max_pic_order_cnt_lsb_minus4,
486 .delta_pic_order_always_zero_flag = sps->delta_pic_order_always_zero_flag,
489 .bit_depth_luma_minus8 = sps->bit_depth_luma_minus8,
490 .bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8,
492 .frame_cropping_flag = sps->frame_cropping_flag,
493 .frame_crop_left_offset = sps->frame_crop_left_offset,
494 .frame_crop_right_offset = sps->frame_crop_right_offset,
495 .frame_crop_top_offset = sps->frame_crop_top_offset,
496 .frame_crop_bottom_offset = sps->frame_crop_bottom_offset,
498 .vui_parameters_present_flag = sps->vui_parameters_present_flag,
501 .aspect_ratio_info_present_flag = sps->vui.aspect_ratio_info_present_flag,
502 .timing_info_present_flag = sps->vui.timing_info_present_flag,
503 .bitstream_restriction_flag = sps->vui.bitstream_restriction_flag,
504 .log2_max_mv_length_horizontal = sps->vui.log2_max_mv_length_horizontal,
505 .log2_max_mv_length_vertical = sps->vui.log2_max_mv_length_vertical,
508 .aspect_ratio_idc = sps->vui.aspect_ratio_idc,
509 .sar_width = sps->vui.sar_width,
510 .sar_height = sps->vui.sar_height,
511 .num_units_in_tick = sps->vui.num_units_in_tick,
512 .time_scale = sps->vui.time_scale,
515 *vpic = (VAEncPictureParameterBufferH264) {
517 .picture_id = VA_INVALID_ID,
518 .flags = VA_PICTURE_H264_INVALID,
521 .coded_buf = VA_INVALID_ID,
523 .pic_parameter_set_id = pps->pic_parameter_set_id,
524 .seq_parameter_set_id = pps->seq_parameter_set_id,
526 .pic_init_qp = pps->pic_init_qp_minus26 + 26,
527 .num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1,
528 .num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1,
530 .chroma_qp_index_offset = pps->chroma_qp_index_offset,
531 .second_chroma_qp_index_offset = pps->second_chroma_qp_index_offset,
534 .entropy_coding_mode_flag = pps->entropy_coding_mode_flag,
535 .weighted_pred_flag = pps->weighted_pred_flag,
536 .weighted_bipred_idc = pps->weighted_bipred_idc,
537 .constrained_intra_pred_flag = pps->constrained_intra_pred_flag,
538 .transform_8x8_mode_flag = pps->transform_8x8_mode_flag,
539 .deblocking_filter_control_present_flag =
540 pps->deblocking_filter_control_present_flag,
541 .redundant_pic_cnt_present_flag = pps->redundant_pic_cnt_present_flag,
542 .pic_order_present_flag =
543 pps->bottom_field_pic_order_in_frame_present_flag,
544 .pic_scaling_matrix_present_flag = pps->pic_scaling_matrix_present_flag,
551 static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx,
552 VAAPIEncodePicture *pic)
554 VAAPIEncodeContext *ctx = avctx->priv_data;
555 VAAPIEncodeH264Context *priv = ctx->priv_data;
556 VAAPIEncodeH264Options *opt = ctx->codec_options;
557 H264RawSPS *sps = &priv->sps;
558 VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
561 memset(&priv->current_access_unit, 0,
562 sizeof(priv->current_access_unit));
564 if (pic->type == PICTURE_TYPE_IDR) {
565 av_assert0(pic->display_order == pic->encode_order);
567 priv->next_frame_num = 1;
569 priv->last_idr_frame = pic->display_order;
570 ++priv->idr_pic_count;
572 priv->slice_type = 7;
573 priv->primary_pic_type = 0;
575 priv->frame_num = priv->next_frame_num;
577 if (pic->type != PICTURE_TYPE_B) {
578 // Reference picture, so frame_num advances.
579 priv->next_frame_num = (priv->frame_num + 1) &
580 ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
584 if (pic->type == PICTURE_TYPE_I) {
585 priv->slice_type = 7;
586 priv->primary_pic_type = 0;
587 } else if (pic->type == PICTURE_TYPE_P) {
588 priv->slice_type = 5;
589 priv->primary_pic_type = 1;
591 priv->slice_type = 6;
592 priv->primary_pic_type = 2;
595 priv->pic_order_cnt = pic->display_order - priv->last_idr_frame;
596 priv->dpb_delay = pic->display_order - pic->encode_order + 1;
599 priv->aud_needed = 1;
600 priv->aud.nal_unit_header.nal_unit_type = H264_NAL_AUD;
601 priv->aud.primary_pic_type = priv->primary_pic_type;
603 priv->aud_needed = 0;
606 if (opt->sei & SEI_IDENTIFIER && pic->encode_order == 0)
607 priv->sei_needed = 1;
609 if (opt->sei & SEI_TIMING) {
610 memset(&priv->pic_timing, 0, sizeof(priv->pic_timing));
612 priv->pic_timing.cpb_removal_delay =
613 2 * sps->vui.num_units_in_tick * priv->cpb_delay;
614 priv->pic_timing.dpb_output_delay =
615 2 * sps->vui.num_units_in_tick * priv->dpb_delay;
617 priv->sei_needed = 1;
620 vpic->CurrPic = (VAPictureH264) {
621 .picture_id = pic->recon_surface,
622 .frame_idx = priv->frame_num,
624 .TopFieldOrderCnt = priv->pic_order_cnt,
625 .BottomFieldOrderCnt = priv->pic_order_cnt,
628 for (i = 0; i < pic->nb_refs; i++) {
629 VAAPIEncodePicture *ref = pic->refs[i];
630 unsigned int frame_num = (ref->encode_order - priv->last_idr_frame) &
631 ((1 << (4 + sps->log2_max_frame_num_minus4)) - 1);
632 unsigned int pic_order_cnt = ref->display_order - priv->last_idr_frame;
634 av_assert0(ref && ref->encode_order < pic->encode_order);
635 vpic->ReferenceFrames[i] = (VAPictureH264) {
636 .picture_id = ref->recon_surface,
637 .frame_idx = frame_num,
638 .flags = VA_PICTURE_H264_SHORT_TERM_REFERENCE,
639 .TopFieldOrderCnt = pic_order_cnt,
640 .BottomFieldOrderCnt = pic_order_cnt,
643 for (; i < FF_ARRAY_ELEMS(vpic->ReferenceFrames); i++) {
644 vpic->ReferenceFrames[i] = (VAPictureH264) {
645 .picture_id = VA_INVALID_ID,
646 .flags = VA_PICTURE_H264_INVALID,
650 vpic->coded_buf = pic->output_buffer;
652 vpic->frame_num = priv->frame_num;
654 vpic->pic_fields.bits.idr_pic_flag = (pic->type == PICTURE_TYPE_IDR);
655 vpic->pic_fields.bits.reference_pic_flag = (pic->type != PICTURE_TYPE_B);
662 static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx,
663 VAAPIEncodePicture *pic,
664 VAAPIEncodeSlice *slice)
666 VAAPIEncodeContext *ctx = avctx->priv_data;
667 VAAPIEncodeH264Context *priv = ctx->priv_data;
668 H264RawSPS *sps = &priv->sps;
669 H264RawPPS *pps = &priv->pps;
670 H264RawSliceHeader *sh = &priv->slice.header;
671 VAEncPictureParameterBufferH264 *vpic = pic->codec_picture_params;
672 VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params;
675 if (pic->type == PICTURE_TYPE_IDR) {
676 sh->nal_unit_header.nal_unit_type = H264_NAL_IDR_SLICE;
677 sh->nal_unit_header.nal_ref_idc = 3;
679 sh->nal_unit_header.nal_unit_type = H264_NAL_SLICE;
680 sh->nal_unit_header.nal_ref_idc = pic->type != PICTURE_TYPE_B;
683 // Only one slice per frame.
684 sh->first_mb_in_slice = 0;
685 sh->slice_type = priv->slice_type;
687 sh->pic_parameter_set_id = pps->pic_parameter_set_id;
689 sh->frame_num = priv->frame_num;
690 sh->idr_pic_id = priv->idr_pic_count;
692 sh->pic_order_cnt_lsb = priv->pic_order_cnt &
693 ((1 << (4 + sps->log2_max_pic_order_cnt_lsb_minus4)) - 1);
695 sh->direct_spatial_mv_pred_flag = 1;
697 if (pic->type == PICTURE_TYPE_B)
698 sh->slice_qp_delta = priv->fixed_qp_b - (pps->pic_init_qp_minus26 + 26);
699 else if (pic->type == PICTURE_TYPE_P)
700 sh->slice_qp_delta = priv->fixed_qp_p - (pps->pic_init_qp_minus26 + 26);
702 sh->slice_qp_delta = priv->fixed_qp_idr - (pps->pic_init_qp_minus26 + 26);
705 vslice->macroblock_address = sh->first_mb_in_slice;
706 vslice->num_macroblocks = priv->mb_width * priv->mb_height;
708 vslice->macroblock_info = VA_INVALID_ID;
710 vslice->slice_type = sh->slice_type % 5;
711 vslice->pic_parameter_set_id = sh->pic_parameter_set_id;
712 vslice->idr_pic_id = sh->idr_pic_id;
714 vslice->pic_order_cnt_lsb = sh->pic_order_cnt_lsb;
716 vslice->direct_spatial_mv_pred_flag = sh->direct_spatial_mv_pred_flag;
718 for (i = 0; i < FF_ARRAY_ELEMS(vslice->RefPicList0); i++) {
719 vslice->RefPicList0[i].picture_id = VA_INVALID_ID;
720 vslice->RefPicList0[i].flags = VA_PICTURE_H264_INVALID;
721 vslice->RefPicList1[i].picture_id = VA_INVALID_ID;
722 vslice->RefPicList1[i].flags = VA_PICTURE_H264_INVALID;
725 av_assert0(pic->nb_refs <= 2);
726 if (pic->nb_refs >= 1) {
727 // Backward reference for P- or B-frame.
728 av_assert0(pic->type == PICTURE_TYPE_P ||
729 pic->type == PICTURE_TYPE_B);
730 vslice->RefPicList0[0] = vpic->ReferenceFrames[0];
732 if (pic->nb_refs >= 2) {
733 // Forward reference for B-frame.
734 av_assert0(pic->type == PICTURE_TYPE_B);
735 vslice->RefPicList1[0] = vpic->ReferenceFrames[1];
738 vslice->slice_qp_delta = sh->slice_qp_delta;
743 static av_cold int vaapi_encode_h264_configure(AVCodecContext *avctx)
745 VAAPIEncodeContext *ctx = avctx->priv_data;
746 VAAPIEncodeH264Context *priv = ctx->priv_data;
747 VAAPIEncodeH264Options *opt = ctx->codec_options;
750 err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_H264, avctx);
754 priv->mb_width = FFALIGN(avctx->width, 16) / 16;
755 priv->mb_height = FFALIGN(avctx->height, 16) / 16;
757 if (ctx->va_rc_mode == VA_RC_CQP) {
758 priv->fixed_qp_p = opt->qp;
759 if (avctx->i_quant_factor > 0.0)
760 priv->fixed_qp_idr = (int)((priv->fixed_qp_p * avctx->i_quant_factor +
761 avctx->i_quant_offset) + 0.5);
763 priv->fixed_qp_idr = priv->fixed_qp_p;
764 if (avctx->b_quant_factor > 0.0)
765 priv->fixed_qp_b = (int)((priv->fixed_qp_p * avctx->b_quant_factor +
766 avctx->b_quant_offset) + 0.5);
768 priv->fixed_qp_b = priv->fixed_qp_p;
770 opt->sei &= ~SEI_TIMING;
772 av_log(avctx, AV_LOG_DEBUG, "Using fixed QP = "
773 "%d / %d / %d for IDR- / P- / B-frames.\n",
774 priv->fixed_qp_idr, priv->fixed_qp_p, priv->fixed_qp_b);
776 } else if (ctx->va_rc_mode == VA_RC_CBR ||
777 ctx->va_rc_mode == VA_RC_VBR) {
778 // These still need to be set for pic_init_qp/slice_qp_delta.
779 priv->fixed_qp_idr = 26;
780 priv->fixed_qp_p = 26;
781 priv->fixed_qp_b = 26;
783 av_log(avctx, AV_LOG_DEBUG, "Using %s-bitrate = %"PRId64" bps.\n",
784 ctx->va_rc_mode == VA_RC_CBR ? "constant" : "variable",
788 av_assert0(0 && "Invalid RC mode.");
791 if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
792 avctx->compression_level = opt->quality;
794 if (opt->sei & SEI_IDENTIFIER) {
795 const char *lavc = LIBAVCODEC_IDENT;
796 const char *vaapi = VA_VERSION_S;
800 memcpy(priv->identifier.uuid_iso_iec_11578,
801 vaapi_encode_h264_sei_identifier_uuid,
802 sizeof(priv->identifier.uuid_iso_iec_11578));
804 driver = vaQueryVendorString(ctx->hwctx->display);
806 driver = "unknown driver";
808 len = snprintf(NULL, 0, "%s / VAAPI %s / %s", lavc, vaapi, driver);
810 priv->identifier_string = av_malloc(len + 1);
811 if (!priv->identifier_string)
812 return AVERROR(ENOMEM);
814 snprintf(priv->identifier_string, len + 1,
815 "%s / VAAPI %s / %s", lavc, vaapi, driver);
817 priv->identifier.data = priv->identifier_string;
818 priv->identifier.data_length = len + 1;
825 static const VAAPIEncodeType vaapi_encode_type_h264 = {
826 .priv_data_size = sizeof(VAAPIEncodeH264Context),
828 .configure = &vaapi_encode_h264_configure,
830 .sequence_params_size = sizeof(VAEncSequenceParameterBufferH264),
831 .init_sequence_params = &vaapi_encode_h264_init_sequence_params,
833 .picture_params_size = sizeof(VAEncPictureParameterBufferH264),
834 .init_picture_params = &vaapi_encode_h264_init_picture_params,
836 .slice_params_size = sizeof(VAEncSliceParameterBufferH264),
837 .init_slice_params = &vaapi_encode_h264_init_slice_params,
839 .sequence_header_type = VAEncPackedHeaderSequence,
840 .write_sequence_header = &vaapi_encode_h264_write_sequence_header,
842 .slice_header_type = VAEncPackedHeaderH264_Slice,
843 .write_slice_header = &vaapi_encode_h264_write_slice_header,
845 .write_extra_header = &vaapi_encode_h264_write_extra_header,
848 static av_cold int vaapi_encode_h264_init(AVCodecContext *avctx)
850 VAAPIEncodeContext *ctx = avctx->priv_data;
851 VAAPIEncodeH264Options *opt =
852 (VAAPIEncodeH264Options*)ctx->codec_options_data;
854 ctx->codec = &vaapi_encode_type_h264;
856 switch (avctx->profile) {
857 case FF_PROFILE_H264_BASELINE:
858 av_log(avctx, AV_LOG_WARNING, "H.264 baseline profile is not "
859 "supported, using constrained baseline profile instead.\n");
860 avctx->profile = FF_PROFILE_H264_CONSTRAINED_BASELINE;
861 case FF_PROFILE_H264_CONSTRAINED_BASELINE:
862 ctx->va_profile = VAProfileH264ConstrainedBaseline;
863 if (avctx->max_b_frames != 0) {
864 avctx->max_b_frames = 0;
865 av_log(avctx, AV_LOG_WARNING, "H.264 constrained baseline profile "
866 "doesn't support encoding with B frames, disabling them.\n");
869 case FF_PROFILE_H264_MAIN:
870 ctx->va_profile = VAProfileH264Main;
872 case FF_PROFILE_H264_EXTENDED:
873 av_log(avctx, AV_LOG_ERROR, "H.264 extended profile "
874 "is not supported.\n");
875 return AVERROR_PATCHWELCOME;
876 case FF_PROFILE_UNKNOWN:
877 case FF_PROFILE_H264_HIGH:
878 ctx->va_profile = VAProfileH264High;
880 case FF_PROFILE_H264_HIGH_10:
881 case FF_PROFILE_H264_HIGH_10_INTRA:
882 av_log(avctx, AV_LOG_ERROR, "H.264 10-bit profiles "
883 "are not supported.\n");
884 return AVERROR_PATCHWELCOME;
885 case FF_PROFILE_H264_HIGH_422:
886 case FF_PROFILE_H264_HIGH_422_INTRA:
887 case FF_PROFILE_H264_HIGH_444:
888 case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
889 case FF_PROFILE_H264_HIGH_444_INTRA:
890 case FF_PROFILE_H264_CAVLC_444:
891 av_log(avctx, AV_LOG_ERROR, "H.264 non-4:2:0 profiles "
892 "are not supported.\n");
893 return AVERROR_PATCHWELCOME;
895 av_log(avctx, AV_LOG_ERROR, "Unknown H.264 profile %d.\n",
897 return AVERROR(EINVAL);
899 if (opt->low_power) {
900 #if VA_CHECK_VERSION(0, 39, 2)
901 ctx->va_entrypoint = VAEntrypointEncSliceLP;
903 av_log(avctx, AV_LOG_ERROR, "Low-power encoding is not "
904 "supported with this VAAPI version.\n");
905 return AVERROR(EINVAL);
908 ctx->va_entrypoint = VAEntrypointEncSlice;
911 // Only 8-bit encode is supported.
912 ctx->va_rt_format = VA_RT_FORMAT_YUV420;
914 if (avctx->bit_rate > 0) {
915 if (avctx->rc_max_rate == avctx->bit_rate)
916 ctx->va_rc_mode = VA_RC_CBR;
918 ctx->va_rc_mode = VA_RC_VBR;
920 ctx->va_rc_mode = VA_RC_CQP;
922 ctx->va_packed_headers =
923 VA_ENC_PACKED_HEADER_SEQUENCE | // SPS and PPS.
924 VA_ENC_PACKED_HEADER_SLICE | // Slice headers.
925 VA_ENC_PACKED_HEADER_MISC; // SEI.
927 ctx->surface_width = FFALIGN(avctx->width, 16);
928 ctx->surface_height = FFALIGN(avctx->height, 16);
930 return ff_vaapi_encode_init(avctx);
933 static av_cold int vaapi_encode_h264_close(AVCodecContext *avctx)
935 VAAPIEncodeContext *ctx = avctx->priv_data;
936 VAAPIEncodeH264Context *priv = ctx->priv_data;
939 ff_cbs_close(&priv->cbc);
940 av_freep(&priv->identifier_string);
943 return ff_vaapi_encode_close(avctx);
946 #define OFFSET(x) (offsetof(VAAPIEncodeContext, codec_options_data) + \
947 offsetof(VAAPIEncodeH264Options, x))
948 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
949 static const AVOption vaapi_encode_h264_options[] = {
950 { "qp", "Constant QP (for P-frames; scaled by qfactor/qoffset for I/B)",
951 OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 20 }, 0, 52, FLAGS },
952 { "quality", "Set encode quality (trades off against speed, higher is faster)",
953 OFFSET(quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 8, FLAGS },
954 { "low_power", "Use low-power encoding mode (experimental: only supported "
955 "on some platforms, does not support all features)",
956 OFFSET(low_power), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
957 { "coder", "Entropy coder type",
958 OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "coder" },
959 { "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
960 { "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
961 { "vlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, FLAGS, "coder" },
962 { "ac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, FLAGS, "coder" },
964 { "aud", "Include AUD",
965 OFFSET(aud), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
967 { "sei", "Set SEI to include",
968 OFFSET(sei), AV_OPT_TYPE_FLAGS,
969 { .i64 = SEI_IDENTIFIER | SEI_TIMING },
970 0, INT_MAX, FLAGS, "sei" },
971 { "identifier", "Include encoder version identifier",
972 0, AV_OPT_TYPE_CONST, { .i64 = SEI_IDENTIFIER },
973 INT_MIN, INT_MAX, FLAGS, "sei" },
974 { "timing", "Include timing parameters (buffering_period and pic_timing)",
975 0, AV_OPT_TYPE_CONST, { .i64 = SEI_TIMING },
976 INT_MIN, INT_MAX, FLAGS, "sei" },
980 static const AVCodecDefault vaapi_encode_h264_defaults[] = {
981 { "profile", "100" },
986 { "i_qfactor", "1" },
987 { "i_qoffset", "0" },
988 { "b_qfactor", "6/5" },
989 { "b_qoffset", "0" },
994 static const AVClass vaapi_encode_h264_class = {
995 .class_name = "h264_vaapi",
996 .item_name = av_default_item_name,
997 .option = vaapi_encode_h264_options,
998 .version = LIBAVUTIL_VERSION_INT,
1001 AVCodec ff_h264_vaapi_encoder = {
1002 .name = "h264_vaapi",
1003 .long_name = NULL_IF_CONFIG_SMALL("H.264/AVC (VAAPI)"),
1004 .type = AVMEDIA_TYPE_VIDEO,
1005 .id = AV_CODEC_ID_H264,
1006 .priv_data_size = (sizeof(VAAPIEncodeContext) +
1007 sizeof(VAAPIEncodeH264Options)),
1008 .init = &vaapi_encode_h264_init,
1009 .encode2 = &ff_vaapi_encode2,
1010 .close = &vaapi_encode_h264_close,
1011 .priv_class = &vaapi_encode_h264_class,
1012 .capabilities = AV_CODEC_CAP_DELAY,
1013 .defaults = vaapi_encode_h264_defaults,
1014 .pix_fmts = (const enum AVPixelFormat[]) {