| @@ -33,6 +33,7 @@ · 32 unmodified lines |
| 33 | #include "cbs_h264.h" | 33 | #include "cbs_h264.h" |
| 34 | #include "codec_internal.h" | 34 | #include "codec_internal.h" |
| 35 | #include "h264.h" | 35 | #include "h264.h" |
| | 36 | #include "hw_base_encode_h264.h" |
| 36 | #include "h264_levels.h" | 37 | #include "h264_levels.h" |
| 37 | #include "h2645data.h" | 38 | #include "h2645data.h" |
| 38 | #include "vaapi_encode.h" | 39 | #include "vaapi_encode.h" |
| @@ -67,6 +68,7 @@ typedef struct VAAPIEncodeH264Picture { · 28 unmodified lines |
| 67 | | 68 | |
| 68 | typedef struct VAAPIEncodeH264Context { | 69 | typedef struct VAAPIEncodeH264Context { |
| 69 | VAAPIEncodeContext common; | 70 | VAAPIEncodeContext common; |
| | 71 | FFHWBaseEncodeH264 units; |
| 70 | | 72 | |
| 71 | // User options. | 73 | // User options. |
| 72 | int qp; | 74 | int qp; |
| @@ -85,18 +87,13 @@ typedef struct VAAPIEncodeH264Context { · 12 unmodified lines |
| 85 | int fixed_qp_p; | 87 | int fixed_qp_p; |
| 86 | int fixed_qp_b; | 88 | int fixed_qp_b; |
| 87 | | 89 | |
| 88 | int dpb_frames; | | |
| 89 | | | |
| 90 | // Writer structures. | 90 | // Writer structures. |
| 91 | CodedBitstreamContext *cbc; | 91 | CodedBitstreamContext *cbc; |
| 92 | CodedBitstreamFragment current_access_unit; | 92 | CodedBitstreamFragment current_access_unit; |
| 93 | | 93 | |
| 94 | H264RawAUD raw_aud; | 94 | H264RawAUD raw_aud; |
| 95 | H264RawSPS raw_sps; | | |
| 96 | H264RawPPS raw_pps; | | |
| 97 | H264RawSlice raw_slice; | 95 | H264RawSlice raw_slice; |
| 98 | | 96 | |
| 99 | H264RawSEIBufferingPeriod sei_buffering_period; | | |
| 100 | H264RawSEIPicTiming sei_pic_timing; | 97 | H264RawSEIPicTiming sei_pic_timing; |
| 101 | H264RawSEIRecoveryPoint sei_recovery_point; | 98 | H264RawSEIRecoveryPoint sei_recovery_point; |
| 102 | SEIRawUserDataUnregistered sei_identifier; | 99 | SEIRawUserDataUnregistered sei_identifier; |
| @@ -168,11 +165,11 @@ static int vaapi_encode_h264_write_sequence_header(AVCodecContext *avctx, · 65 unmodified lines |
| 168 | priv->aud_needed = 0; | 165 | priv->aud_needed = 0; |
| 169 | } | 166 | } |
| 170 | | 167 | |
| 171 | err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_sps); | 168 | err = vaapi_encode_h264_add_nal(avctx, au, &priv->units.raw_sps); |
| 172 | if (err < 0) | 169 | if (err < 0) |
| 173 | goto fail; | 170 | goto fail; |
| 174 | | 171 | |
| 175 | err = vaapi_encode_h264_add_nal(avctx, au, &priv->raw_pps); | 172 | err = vaapi_encode_h264_add_nal(avctx, au, &priv->units.raw_pps); |
| 176 | if (err < 0) | 173 | if (err < 0) |
| 177 | goto fail; | 174 | goto fail; |
| 178 | | 175 | |
| @@ -236,7 +233,7 @@ static int vaapi_encode_h264_write_extra_header(AVCodecContext *avctx, · 57 unmodified lines |
| 236 | if (base->type == FF_HW_PICTURE_TYPE_IDR) { | 233 | if (base->type == FF_HW_PICTURE_TYPE_IDR) { |
| 237 | err = ff_cbs_sei_add_message(priv->cbc, au, 1, | 234 | err = ff_cbs_sei_add_message(priv->cbc, au, 1, |
| 238 | SEI_TYPE_BUFFERING_PERIOD, | 235 | SEI_TYPE_BUFFERING_PERIOD, |
| 239 | &priv->sei_buffering_period, NULL); | 236 | &priv->units.sei_buffering_period, NULL); |
| 240 | if (err < 0) | 237 | if (err < 0) |
| 241 | goto fail; | 238 | goto fail; |
| 242 | } | 239 | } |
| @@ -298,240 +295,26 @@ static int vaapi_encode_h264_init_sequence_params(AVCodecContext *avctx) · 55 unmodified lines |
| 298 | FFHWBaseEncodeContext *base_ctx = avctx->priv_data; | 295 | FFHWBaseEncodeContext *base_ctx = avctx->priv_data; |
| 299 | VAAPIEncodeContext *ctx = avctx->priv_data; | 296 | VAAPIEncodeContext *ctx = avctx->priv_data; |
| 300 | VAAPIEncodeH264Context *priv = avctx->priv_data; | 297 | VAAPIEncodeH264Context *priv = avctx->priv_data; |
| 301 | H264RawSPS *sps = &priv->raw_sps; | 298 | H264RawSPS *sps = &priv->units.raw_sps; |
| 302 | H264RawPPS *pps = &priv->raw_pps; | 299 | H264RawPPS *pps = &priv->units.raw_pps; |
| 303 | VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params; | 300 | VAEncSequenceParameterBufferH264 *vseq = ctx->codec_sequence_params; |
| 304 | VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params; | 301 | VAEncPictureParameterBufferH264 *vpic = ctx->codec_picture_params; |
| 305 | const AVPixFmtDescriptor *desc; | | |
| 306 | int bit_depth; | | |
| 307 | | | |
| 308 | memset(sps, 0, sizeof(*sps)); | | |
| 309 | memset(pps, 0, sizeof(*pps)); | | |
| 310 | | | |
| 311 | desc = av_pix_fmt_desc_get(base_ctx->input_frames->sw_format); | | |
| 312 | av_assert0(desc); | | |
| 313 | if (desc->nb_components == 1 || desc->log2_chroma_w != 1 || desc->log2_chroma_h != 1) { | | |
| 314 | av_log(avctx, AV_LOG_ERROR, "Chroma format of input pixel format " | | |
| 315 | "%s is not supported.\n", desc->name); | | |
| 316 | return AVERROR(EINVAL); | | |
| 317 | } | | |
| 318 | bit_depth = desc->comp[0].depth; | | |
| 319 | | | |
| 320 | sps->nal_unit_header.nal_ref_idc = 3; | | |
| 321 | sps->nal_unit_header.nal_unit_type = H264_NAL_SPS; | | |
| 322 | | | |
| 323 | sps->profile_idc = avctx->profile & 0xff; | | |
| 324 | | | |
| 325 | if (avctx->profile == AV_PROFILE_H264_CONSTRAINED_BASELINE || | | |
| 326 | avctx->profile == AV_PROFILE_H264_MAIN) | | |
| 327 | sps->constraint_set1_flag = 1; | | |
| 328 | | | |
| 329 | if (avctx->profile == AV_PROFILE_H264_HIGH || avctx->profile == AV_PROFILE_H264_HIGH_10) | | |
| 330 | sps->constraint_set3_flag = base_ctx->gop_size == 1; | | |
| 331 | | | |
| 332 | if (avctx->profile == AV_PROFILE_H264_MAIN || | | |
| 333 | avctx->profile == AV_PROFILE_H264_HIGH || avctx->profile == AV_PROFILE_H264_HIGH_10) { | | |
| 334 | sps->constraint_set4_flag = 1; | | |
| 335 | sps->constraint_set5_flag = base_ctx->b_per_p == 0; | | |
| 336 | } | | |
| 337 | | | |
| 338 | if (base_ctx->gop_size == 1) | | |
| 339 | priv->dpb_frames = 0; | | |
| 340 | else | | |
| 341 | priv->dpb_frames = 1 + base_ctx->max_b_depth; | | |
| 342 | | | |
| 343 | if (avctx->level != AV_LEVEL_UNKNOWN) { | | |
| 344 | sps->level_idc = avctx->level; | | |
| 345 | } else { | | |
| 346 | const H264LevelDescriptor *level; | | |
| 347 | int framerate; | | |
| 348 | | | |
| 349 | if (avctx->framerate.num > 0 && avctx->framerate.den > 0) | | |
| 350 | framerate = avctx->framerate.num / avctx->framerate.den; | | |
| 351 | else | | |
| 352 | framerate = 0; | | |
| 353 | | | |
| 354 | level = ff_h264_guess_level(sps->profile_idc, | | |
| 355 | avctx->bit_rate, | | |
| 356 | framerate, | | |
| 357 | priv->mb_width * 16, | | |
| 358 | priv->mb_height * 16, | | |
| 359 | priv->dpb_frames); | | |
| 360 | if (level) { | | |
| 361 | av_log(avctx, AV_LOG_VERBOSE, "Using level %s.\n", level->name); | | |
| 362 | if (level->constraint_set3_flag) | | |
| 363 | sps->constraint_set3_flag = 1; | | |
| 364 | sps->level_idc = level->level_idc; | | |
| 365 | } else { | | |
| 366 | av_log(avctx, AV_LOG_WARNING, "Stream will not conform " | | |
| 367 | "to any level: using level 6.2.\n"); | | |
| 368 | sps->level_idc = 62; | | |
| 369 | } | | |
| 370 | } | | |
| 371 | | | |
| 372 | sps->seq_parameter_set_id = 0; | | |
| 373 | sps->chroma_format_idc = 1; | | |
| 374 | sps->bit_depth_luma_minus8 = bit_depth - 8; | | |
| 375 | sps->bit_depth_chroma_minus8 = bit_depth - 8; | | |
| 376 | | | |
| 377 | sps->log2_max_frame_num_minus4 = 4; | | |
| 378 | sps->pic_order_cnt_type = base_ctx->max_b_depth ? 0 : 2; | | |
| 379 | if (sps->pic_order_cnt_type == 0) { | | |
| 380 | sps->log2_max_pic_order_cnt_lsb_minus4 = 4; | | |
| 381 | } | | |
| 382 | | | |
| 383 | sps->max_num_ref_frames = priv->dpb_frames; | | |
| 384 | | | |
| 385 | sps->pic_width_in_mbs_minus1 = priv->mb_width - 1; | | |
| 386 | sps->pic_height_in_map_units_minus1 = priv->mb_height - 1; | | |
| 387 | | | |
| 388 | sps->frame_mbs_only_flag = 1; | | |
| 389 | sps->direct_8x8_inference_flag = 1; | | |
| 390 | | | |
| 391 | if (avctx->width != 16 * priv->mb_width || | | |
| 392 | avctx->height != 16 * priv->mb_height) { | | |
| 393 | sps->frame_cropping_flag = 1; | | |
| 394 | | | |
| 395 | sps->frame_crop_left_offset = 0; | | |
| 396 | sps->frame_crop_right_offset = | | |
| 397 | (16 * priv->mb_width - avctx->width) / 2; | | |
| 398 | sps->frame_crop_top_offset = 0; | | |
| 399 | sps->frame_crop_bottom_offset = | | |
| 400 | (16 * priv->mb_height - avctx->height) / 2; | | |
| 401 | } else { | | |
| 402 | sps->frame_cropping_flag = 0; | | |
| 403 | } | | |
| 404 | | | |
| 405 | sps->vui_parameters_present_flag = 1; | | |
| 406 | | | |
| 407 | if (avctx->sample_aspect_ratio.num != 0 && | | |
| 408 | avctx->sample_aspect_ratio.den != 0) { | | |
| 409 | int num, den, i; | | |
| 410 | av_reduce(&num, &den, avctx->sample_aspect_ratio.num, | | |
| 411 | avctx->sample_aspect_ratio.den, 65535); | | |
| 412 | for (i = 0; i < FF_ARRAY_ELEMS(ff_h2645_pixel_aspect); i++) { | | |
| 413 | if (num == ff_h2645_pixel_aspect[i].num && | | |
| 414 | den == ff_h2645_pixel_aspect[i].den) { | | |
| 415 | sps->vui.aspect_ratio_idc = i; | | |
| 416 | break; | | |
| 417 | } | | |
| 418 | } | | |
| 419 | if (i >= FF_ARRAY_ELEMS(ff_h2645_pixel_aspect)) { | | |
| 420 | sps->vui.aspect_ratio_idc = 255; | | |
| 421 | sps->vui.sar_width = num; | | |
| 422 | sps->vui.sar_height = den; | | |
| 423 | } | | |
| 424 | sps->vui.aspect_ratio_info_present_flag = 1; | | |
| 425 | } | | |
| 426 | | | |
| 427 | // Unspecified video format, from table E-2. | | |
| 428 | sps->vui.video_format = 5; | | |
| 429 | sps->vui.video_full_range_flag = | | |
| 430 | avctx->color_range == AVCOL_RANGE_JPEG; | | |
| 431 | sps->vui.colour_primaries = avctx->color_primaries; | | |
| 432 | sps->vui.transfer_characteristics = avctx->color_trc; | | |
| 433 | sps->vui.matrix_coefficients = avctx->colorspace; | | |
| 434 | if (avctx->color_primaries != AVCOL_PRI_UNSPECIFIED || | | |
| 435 | avctx->color_trc != AVCOL_TRC_UNSPECIFIED || | | |
| 436 | avctx->colorspace != AVCOL_SPC_UNSPECIFIED) | | |
| 437 | sps->vui.colour_description_present_flag = 1; | | |
| 438 | if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED || | | |
| 439 | sps->vui.colour_description_present_flag) | | |
| 440 | sps->vui.video_signal_type_present_flag = 1; | | |
| 441 | | | |
| 442 | if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) { | | |
| 443 | sps->vui.chroma_loc_info_present_flag = 1; | | |
| 444 | sps->vui.chroma_sample_loc_type_top_field = | | |
| 445 | sps->vui.chroma_sample_loc_type_bottom_field = | | |
| 446 | avctx->chroma_sample_location - 1; | | |
| 447 | } | | |
| 448 | | | |
| 449 | sps->vui.timing_info_present_flag = 1; | | |
| 450 | if (avctx->framerate.num > 0 && avctx->framerate.den > 0) { | | |
| 451 | sps->vui.num_units_in_tick = avctx->framerate.den; | | |
| 452 | sps->vui.time_scale = 2 * avctx->framerate.num; | | |
| 453 | sps->vui.fixed_frame_rate_flag = 1; | | |
| 454 | } else { | | |
| 455 | sps->vui.num_units_in_tick = avctx->time_base.num; | | |
| 456 | sps->vui.time_scale = 2 * avctx->time_base.den; | | |
| 457 | sps->vui.fixed_frame_rate_flag = 0; | | |
| 458 | } | | |
| 459 | | | |
| 460 | if (priv->sei & SEI_TIMING) { | | |
| 461 | H264RawHRD *hrd = &sps->vui.nal_hrd_parameters; | | |
| 462 | H264RawSEIBufferingPeriod *bp = &priv->sei_buffering_period; | | |
| 463 | | | |
| 464 | sps->vui.nal_hrd_parameters_present_flag = 1; | | |
| 465 | | | |
| 466 | hrd->cpb_cnt_minus1 = 0; | | |
| 467 | | | |
| 468 | // Try to scale these to a sensible range so that the | | |
| 469 | // golomb encode of the value is not overlong. | | |
| 470 | hrd->bit_rate_scale = | | |
| 471 | av_clip_uintp2(av_log2(ctx->va_bit_rate) - 15 - 6, 4); | | |
| 472 | hrd->bit_rate_value_minus1[0] = | | |
| 473 | (ctx->va_bit_rate >> hrd->bit_rate_scale + 6) - 1; | | |
| 474 | | | |
| 475 | hrd->cpb_size_scale = | | |
| 476 | av_clip_uintp2(av_log2(ctx->hrd_params.buffer_size) - 15 - 4, 4); | | |
| 477 | hrd->cpb_size_value_minus1[0] = | | |
| 478 | (ctx->hrd_params.buffer_size >> hrd->cpb_size_scale + 4) - 1; | | |
| 479 | | | |
| 480 | // CBR mode as defined for the HRD cannot be achieved without filler | | |
| 481 | // data, so this flag cannot be set even with VAAPI CBR modes. | | |
| 482 | hrd->cbr_flag[0] = 0; | | |
| 483 | | 302 | |
| 484 | hrd->initial_cpb_removal_delay_length_minus1 = 23; | 303 | FFHWBaseEncodeH264Opts unit_opts = { |
| 485 | hrd->cpb_removal_delay_length_minus1 = 23; | 304 | .flags = (priv->sei & SEI_TIMING) ? FF_HW_H264_SEI_TIMING : 0, |
| 486 | hrd->dpb_output_delay_length_minus1 = 7; | 305 | .mb_width = priv->mb_width, |
| 487 | hrd->time_offset_length = 0; | 306 | .mb_height = priv->mb_height, |
| 488 | | 307 | .cabac = priv->coder, |
| 489 | bp->seq_parameter_set_id = sps->seq_parameter_set_id; | 308 | .hrd_buffer_size = ctx->hrd_params.buffer_size, |
| 490 | | 309 | .fixed_qp_idr = priv->fixed_qp_idr, |
| 491 | // This calculation can easily overflow 32 bits. | 310 | .initial_buffer_fullness = ctx->hrd_params.initial_buffer_fullness, |
| 492 | bp->nal.initial_cpb_removal_delay[0] = 90000 * | 311 | .bit_rate = ctx->va_bit_rate, |
| 493 | (uint64_t)ctx->hrd_params.initial_buffer_fullness / | 312 | }; |
| 494 | ctx->hrd_params.buffer_size; | | |
| 495 | bp->nal.initial_cpb_removal_delay_offset[0] = 0; | | |
| 496 | } else { | | |
| 497 | sps->vui.nal_hrd_parameters_present_flag = 0; | | |
| 498 | sps->vui.low_delay_hrd_flag = 1 - sps->vui.fixed_frame_rate_flag; | | |
| 499 | } | | |
| 500 | | | |
| 501 | sps->vui.bitstream_restriction_flag = 1; | | |
| 502 | sps->vui.motion_vectors_over_pic_boundaries_flag = 1; | | |
| 503 | sps->vui.log2_max_mv_length_horizontal = 15; | | |
| 504 | sps->vui.log2_max_mv_length_vertical = 15; | | |
| 505 | sps->vui.max_num_reorder_frames = base_ctx->max_b_depth; | | |
| 506 | sps->vui.max_dec_frame_buffering = base_ctx->max_b_depth + 1; | | |
| 507 | | | |
| 508 | pps->nal_unit_header.nal_ref_idc = 3; | | |
| 509 | pps->nal_unit_header.nal_unit_type = H264_NAL_PPS; | | |
| 510 | | | |
| 511 | pps->pic_parameter_set_id = 0; | | |
| 512 | pps->seq_parameter_set_id = 0; | | |
| 513 | | | |
| 514 | pps->entropy_coding_mode_flag = | | |
| 515 | !(sps->profile_idc == AV_PROFILE_H264_BASELINE || | | |
| 516 | sps->profile_idc == AV_PROFILE_H264_EXTENDED || | | |
| 517 | sps->profile_idc == AV_PROFILE_H264_CAVLC_444); | | |
| 518 | if (!priv->coder && pps->entropy_coding_mode_flag) | | |
| 519 | pps->entropy_coding_mode_flag = 0; | | |
| 520 | | | |
| 521 | pps->num_ref_idx_l0_default_active_minus1 = 0; | | |
| 522 | pps->num_ref_idx_l1_default_active_minus1 = 0; | | |
| 523 | | | |
| 524 | pps->pic_init_qp_minus26 = priv->fixed_qp_idr - 26; | | |
| 525 | | | |
| 526 | if (sps->profile_idc == AV_PROFILE_H264_BASELINE || | | |
| 527 | sps->profile_idc == AV_PROFILE_H264_EXTENDED || | | |
| 528 | sps->profile_idc == AV_PROFILE_H264_MAIN) { | | |
| 529 | pps->more_rbsp_data = 0; | | |
| 530 | } else { | | |
| 531 | pps->more_rbsp_data = 1; | | |
| 532 | | 313 | |
| 533 | pps->transform_8x8_mode_flag = 1; | 314 | int err = ff_hw_base_encode_init_params_h264(base_ctx, avctx, |
| 534 | } | 315 | &priv->units, &unit_opts); |
| | 316 | if (err < 0) |
| | 317 | return err; |
| 535 | | 318 | |
| 536 | *vseq = (VAEncSequenceParameterBufferH264) { | 319 | *vseq = (VAEncSequenceParameterBufferH264) { |
| 537 | .seq_parameter_set_id = sps->seq_parameter_set_id, | 320 | .seq_parameter_set_id = sps->seq_parameter_set_id, |
| @@ -660,7 +443,7 @@ static int vaapi_encode_h264_init_picture_params(AVCodecContext *avctx, · 122 unmodified lines |
| 660 | } | 443 | } |
| 661 | } | 444 | } |
| 662 | hpic->pic_order_cnt = pic->display_order - hpic->last_idr_frame; | 445 | hpic->pic_order_cnt = pic->display_order - hpic->last_idr_frame; |
| 663 | if (priv->raw_sps.pic_order_cnt_type == 2) { | 446 | if (priv->units.raw_sps.pic_order_cnt_type == 2) { |
| 664 | hpic->pic_order_cnt *= 2; | 447 | hpic->pic_order_cnt *= 2; |
| 665 | } | 448 | } |
| 666 | | 449 | |
| @@ -870,8 +653,8 @@ static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx, · 203 unmodified lines |
| 870 | VAAPIEncodePicture *vaapi_pic = pic->priv; | 653 | VAAPIEncodePicture *vaapi_pic = pic->priv; |
| 871 | VAAPIEncodeH264Picture *hpic = pic->codec_priv; | 654 | VAAPIEncodeH264Picture *hpic = pic->codec_priv; |
| 872 | FFHWBaseEncodePicture *prev = pic->prev; | 655 | FFHWBaseEncodePicture *prev = pic->prev; |
| 873 | H264RawSPS *sps = &priv->raw_sps; | 656 | H264RawSPS *sps = &priv->units.raw_sps; |
| 874 | H264RawPPS *pps = &priv->raw_pps; | 657 | H264RawPPS *pps = &priv->units.raw_pps; |
| 875 | H264RawSliceHeader *sh = &priv->raw_slice.header; | 658 | H264RawSliceHeader *sh = &priv->raw_slice.header; |
| 876 | VAEncPictureParameterBufferH264 *vpic = vaapi_pic->codec_picture_params; | 659 | VAEncPictureParameterBufferH264 *vpic = vaapi_pic->codec_picture_params; |
| 877 | VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params; | 660 | VAEncSliceParameterBufferH264 *vslice = slice->codec_slice_params; |
| @@ -923,7 +706,7 @@ static int vaapi_encode_h264_init_slice_params(AVCodecContext *avctx, · 45 unmodified lines |
| 923 | ++keep; | 706 | ++keep; |
| 924 | } | 707 | } |
| 925 | } | 708 | } |
| 926 | av_assert0(keep <= priv->dpb_frames); | 709 | av_assert0(keep <= priv->units.dpb_frames); |
| 927 | | 710 | |
| 928 | if (discard == 0) { | 711 | if (discard == 0) { |
| 929 | sh->adaptive_ref_pic_marking_mode_flag = 0; | 712 | sh->adaptive_ref_pic_marking_mode_flag = 0; |