5 #include "packager/media/codecs/h264_parser.h" 8 #include "packager/base/logging.h" 9 #include "packager/media/base/buffer_reader.h" 11 #define LOG_ERROR_ONCE(msg) \ 13 static bool logged_once = false; \ 14 LOG_IF(ERROR, !logged_once) << msg; \ 23 bool ExtractResolutionFromSps(
const H264Sps& sps,
24 uint32_t* coded_width,
25 uint32_t* coded_height,
26 uint32_t* pixel_width,
27 uint32_t* pixel_height) {
30 if (sps.frame_cropping_flag) {
34 switch (sps.chroma_format_idc) {
54 LOG(ERROR) <<
"Unexpected chroma_format_idc " << sps.chroma_format_idc;
59 int crop_unit_x = sub_width_c;
60 int crop_unit_y = sub_height_c * (2 - (sps.frame_mbs_only_flag ? 1 : 0));
61 crop_x = crop_unit_x *
62 (sps.frame_crop_left_offset + sps.frame_crop_right_offset);
63 crop_y = crop_unit_y *
64 (sps.frame_crop_top_offset + sps.frame_crop_bottom_offset);
68 int pic_width_in_mbs = sps.pic_width_in_mbs_minus1 + 1;
69 *coded_width = pic_width_in_mbs * 16 - crop_x;
72 int pic_height_in_mbs = (2 - (sps.frame_mbs_only_flag ? 1 : 0)) *
73 (sps.pic_height_in_map_units_minus1 + 1);
74 *coded_height = pic_height_in_mbs * 16 - crop_y;
77 *pixel_width = sps.sar_width == 0 ? 1 : sps.sar_width;
78 *pixel_height = sps.sar_height == 0 ? 1 : sps.sar_height;
79 DVLOG(2) <<
"Found coded_width: " << *coded_width
80 <<
" coded_height: " << *coded_height
81 <<
" pixel_width: " << *pixel_width
82 <<
" pixel_height: " << *pixel_height;
86 bool H264SliceHeader::IsPSlice()
const {
87 return (slice_type % 5 == kPSlice);
90 bool H264SliceHeader::IsBSlice()
const {
91 return (slice_type % 5 == kBSlice);
94 bool H264SliceHeader::IsISlice()
const {
95 return (slice_type % 5 == kISlice);
98 bool H264SliceHeader::IsSPSlice()
const {
99 return (slice_type % 5 == kSPSlice);
102 bool H264SliceHeader::IsSISlice()
const {
103 return (slice_type % 5 == kSISlice);
107 memset(
this, 0,
sizeof(*
this));
111 memset(
this, 0,
sizeof(*
this));
114 H264SliceHeader::H264SliceHeader() {
115 memset(
this, 0,
sizeof(*
this));
118 H264SEIMessage::H264SEIMessage() {
119 memset(
this, 0,
sizeof(*
this));
122 #define READ_BITS_OR_RETURN(num_bits, out) \ 124 if (!br->ReadBits(num_bits, (out))) { \ 126 << "Error in stream: unexpected EOS while trying to read " #out; \ 127 return kInvalidStream; \ 131 #define READ_BOOL_OR_RETURN(out) \ 134 if (!br->ReadBits(1, &_out)) { \ 136 << "Error in stream: unexpected EOS while trying to read " #out; \ 137 return kInvalidStream; \ 139 *(out) = _out != 0; \ 142 #define READ_UE_OR_RETURN(out) \ 144 if (!br->ReadUE(out)) { \ 145 DVLOG(1) << "Error in stream: invalid value while trying to read " #out; \ 146 return kInvalidStream; \ 150 #define READ_SE_OR_RETURN(out) \ 152 if (!br->ReadSE(out)) { \ 153 DVLOG(1) << "Error in stream: invalid value while trying to read " #out; \ 154 return kInvalidStream; \ 158 #define IN_RANGE_OR_RETURN(val, min, max) \ 160 if ((val) < (min) || (val) > (max)) { \ 161 DVLOG(1) << "Error in stream: invalid value, expected " #val " to be" \ 162 << " in range [" << (min) << ":" << (max) << "]" \ 163 << " found " << (val) << " instead"; \ 164 return kInvalidStream; \ 168 #define TRUE_OR_RETURN(a) \ 171 DVLOG(1) << "Error in stream: invalid value, expected " << #a; \ 172 return kInvalidStream; \ 176 enum AspectRatioIdc {
182 static const int kTableSarWidth[] = {
183 0, 1, 12, 10, 16, 40, 24, 20, 32, 80, 18, 15, 64, 160, 4, 3, 2
185 static const int kTableSarHeight[] = {
186 0, 1, 11, 11, 11, 33, 11, 11, 11, 33, 11, 11, 33, 99, 3, 2, 1
188 static_assert(arraysize(kTableSarWidth) == arraysize(kTableSarHeight),
189 "sar_tables_must_have_same_size");
191 H264Parser::H264Parser() {}
193 H264Parser::~H264Parser() {}
195 const H264Pps* H264Parser::GetPps(
int pps_id) {
196 return active_PPSes_[pps_id].get();
199 const H264Sps* H264Parser::GetSps(
int sps_id) {
200 return active_SPSes_[sps_id].get();
204 static const int kDefault4x4Intra[kH264ScalingList4x4Length] = {
205 6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32, 32, 37, 37, 42, };
207 static const int kDefault4x4Inter[kH264ScalingList4x4Length] = {
208 10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27, 27, 30, 30, 34, };
210 static const int kDefault8x8Intra[kH264ScalingList8x8Length] = {
211 6, 10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18, 18, 18, 18, 23,
212 23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27,
213 27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31,
214 31, 33, 33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42, };
216 static const int kDefault8x8Inter[kH264ScalingList8x8Length] = {
217 9, 13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19, 19, 19, 19, 21,
218 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24, 24,
219 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27,
220 27, 28, 28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35, };
222 static inline void DefaultScalingList4x4(
224 int scaling_list4x4[][kH264ScalingList4x4Length]) {
228 memcpy(scaling_list4x4[i], kDefault4x4Intra,
sizeof(kDefault4x4Intra));
230 memcpy(scaling_list4x4[i], kDefault4x4Inter,
sizeof(kDefault4x4Inter));
233 static inline void DefaultScalingList8x8(
235 int scaling_list8x8[][kH264ScalingList8x8Length]) {
239 memcpy(scaling_list8x8[i], kDefault8x8Intra,
sizeof(kDefault8x8Intra));
241 memcpy(scaling_list8x8[i], kDefault8x8Inter,
sizeof(kDefault8x8Inter));
244 static void FallbackScalingList4x4(
246 const int default_scaling_list_intra[],
247 const int default_scaling_list_inter[],
248 int scaling_list4x4[][kH264ScalingList4x4Length]) {
249 static const int kScalingList4x4ByteSize =
250 sizeof(scaling_list4x4[0][0]) * kH264ScalingList4x4Length;
254 memcpy(scaling_list4x4[i], default_scaling_list_intra,
255 kScalingList4x4ByteSize);
259 memcpy(scaling_list4x4[i], scaling_list4x4[0], kScalingList4x4ByteSize);
263 memcpy(scaling_list4x4[i], scaling_list4x4[1], kScalingList4x4ByteSize);
267 memcpy(scaling_list4x4[i], default_scaling_list_inter,
268 kScalingList4x4ByteSize);
272 memcpy(scaling_list4x4[i], scaling_list4x4[3], kScalingList4x4ByteSize);
276 memcpy(scaling_list4x4[i], scaling_list4x4[4], kScalingList4x4ByteSize);
285 static void FallbackScalingList8x8(
287 const int default_scaling_list_intra[],
288 const int default_scaling_list_inter[],
289 int scaling_list8x8[][kH264ScalingList8x8Length]) {
290 static const int kScalingList8x8ByteSize =
291 sizeof(scaling_list8x8[0][0]) * kH264ScalingList8x8Length;
295 memcpy(scaling_list8x8[i], default_scaling_list_intra,
296 kScalingList8x8ByteSize);
300 memcpy(scaling_list8x8[i], default_scaling_list_inter,
301 kScalingList8x8ByteSize);
305 memcpy(scaling_list8x8[i], scaling_list8x8[0], kScalingList8x8ByteSize);
309 memcpy(scaling_list8x8[i], scaling_list8x8[1], kScalingList8x8ByteSize);
313 memcpy(scaling_list8x8[i], scaling_list8x8[2], kScalingList8x8ByteSize);
317 memcpy(scaling_list8x8[i], scaling_list8x8[3], kScalingList8x8ByteSize);
326 H264Parser::Result H264Parser::ParseScalingList(H26xBitReader* br,
335 *use_default =
false;
337 for (
int j = 0; j < size; ++j) {
338 if (next_scale != 0) {
339 READ_SE_OR_RETURN(&delta_scale);
340 IN_RANGE_OR_RETURN(delta_scale, -128, 127);
341 next_scale = (last_scale + delta_scale + 256) & 0xff;
343 if (j == 0 && next_scale == 0) {
349 scaling_list[j] = (next_scale == 0) ? last_scale : next_scale;
350 last_scale = scaling_list[j];
356 H264Parser::Result H264Parser::ParseSpsScalingLists(H26xBitReader* br,
359 bool seq_scaling_list_present_flag;
364 for (
int i = 0; i < 6; ++i) {
365 READ_BOOL_OR_RETURN(&seq_scaling_list_present_flag);
367 if (seq_scaling_list_present_flag) {
368 res = ParseScalingList(br,
369 arraysize(sps->scaling_list4x4[i]),
370 sps->scaling_list4x4[i],
376 DefaultScalingList4x4(i, sps->scaling_list4x4);
379 FallbackScalingList4x4(
380 i, kDefault4x4Intra, kDefault4x4Inter, sps->scaling_list4x4);
385 for (
int i = 0; i < ((sps->chroma_format_idc != 3) ? 2 : 6); ++i) {
386 READ_BOOL_OR_RETURN(&seq_scaling_list_present_flag);
388 if (seq_scaling_list_present_flag) {
389 res = ParseScalingList(br,
390 arraysize(sps->scaling_list8x8[i]),
391 sps->scaling_list8x8[i],
397 DefaultScalingList8x8(i, sps->scaling_list8x8);
400 FallbackScalingList8x8(
401 i, kDefault8x8Intra, kDefault8x8Inter, sps->scaling_list8x8);
408 H264Parser::Result H264Parser::ParsePpsScalingLists(H26xBitReader* br,
412 bool pic_scaling_list_present_flag;
416 for (
int i = 0; i < 6; ++i) {
417 READ_BOOL_OR_RETURN(&pic_scaling_list_present_flag);
419 if (pic_scaling_list_present_flag) {
420 res = ParseScalingList(br,
421 arraysize(pps->scaling_list4x4[i]),
422 pps->scaling_list4x4[i],
428 DefaultScalingList4x4(i, pps->scaling_list4x4);
431 if (sps.seq_scaling_matrix_present_flag) {
433 FallbackScalingList4x4(
434 i, kDefault4x4Intra, kDefault4x4Inter, pps->scaling_list4x4);
437 FallbackScalingList4x4(i,
438 sps.scaling_list4x4[0],
439 sps.scaling_list4x4[3],
440 pps->scaling_list4x4);
445 if (pps->transform_8x8_mode_flag) {
446 for (
int i = 0; i < ((sps.chroma_format_idc != 3) ? 2 : 6); ++i) {
447 READ_BOOL_OR_RETURN(&pic_scaling_list_present_flag);
449 if (pic_scaling_list_present_flag) {
450 res = ParseScalingList(br,
451 arraysize(pps->scaling_list8x8[i]),
452 pps->scaling_list8x8[i],
458 DefaultScalingList8x8(i, pps->scaling_list8x8);
461 if (sps.seq_scaling_matrix_present_flag) {
463 FallbackScalingList8x8(
464 i, kDefault8x8Intra, kDefault8x8Inter, pps->scaling_list8x8);
467 FallbackScalingList8x8(i,
468 sps.scaling_list8x8[0],
469 sps.scaling_list8x8[1],
470 pps->scaling_list8x8);
478 H264Parser::Result H264Parser::ParseAndIgnoreHRDParameters(
479 H26xBitReader* br,
bool* hrd_parameters_present) {
481 READ_BOOL_OR_RETURN(&data);
485 *hrd_parameters_present =
true;
488 READ_UE_OR_RETURN(&cpb_cnt_minus1);
489 IN_RANGE_OR_RETURN(cpb_cnt_minus1, 0, 31);
490 READ_BITS_OR_RETURN(8, &data);
491 for (
int i = 0; i <= cpb_cnt_minus1; ++i) {
492 READ_UE_OR_RETURN(&data);
493 READ_UE_OR_RETURN(&data);
494 READ_BOOL_OR_RETURN(&data);
496 READ_BITS_OR_RETURN(20, &data);
501 H264Parser::Result H264Parser::ParseVUIParameters(H26xBitReader* br,
503 bool aspect_ratio_info_present_flag;
504 READ_BOOL_OR_RETURN(&aspect_ratio_info_present_flag);
505 if (aspect_ratio_info_present_flag) {
506 int aspect_ratio_idc;
507 READ_BITS_OR_RETURN(8, &aspect_ratio_idc);
508 if (aspect_ratio_idc == kExtendedSar) {
509 READ_BITS_OR_RETURN(16, &sps->sar_width);
510 READ_BITS_OR_RETURN(16, &sps->sar_height);
512 const int max_aspect_ratio_idc = arraysize(kTableSarWidth) - 1;
513 IN_RANGE_OR_RETURN(aspect_ratio_idc, 0, max_aspect_ratio_idc);
514 sps->sar_width = kTableSarWidth[aspect_ratio_idc];
515 sps->sar_height = kTableSarHeight[aspect_ratio_idc];
521 READ_BOOL_OR_RETURN(&data);
523 READ_BOOL_OR_RETURN(&data);
525 READ_BOOL_OR_RETURN(&data);
527 READ_BITS_OR_RETURN(3, &data);
528 READ_BOOL_OR_RETURN(&data);
529 READ_BOOL_OR_RETURN(&data);
531 READ_BITS_OR_RETURN(24, &data);
534 READ_BOOL_OR_RETURN(&data);
536 READ_UE_OR_RETURN(&data);
537 READ_UE_OR_RETURN(&data);
541 READ_BOOL_OR_RETURN(&data);
543 READ_BITS_OR_RETURN(16, &data);
544 READ_BITS_OR_RETURN(16, &data);
545 READ_BITS_OR_RETURN(16, &data);
546 READ_BITS_OR_RETURN(16, &data);
547 READ_BOOL_OR_RETURN(&data);
551 bool hrd_parameters_present =
false;
552 Result res = ParseAndIgnoreHRDParameters(br, &hrd_parameters_present);
557 res = ParseAndIgnoreHRDParameters(br, &hrd_parameters_present);
561 if (hrd_parameters_present)
562 READ_BOOL_OR_RETURN(&data);
564 READ_BOOL_OR_RETURN(&data);
565 READ_BOOL_OR_RETURN(&sps->bitstream_restriction_flag);
566 if (sps->bitstream_restriction_flag) {
567 READ_BOOL_OR_RETURN(&data);
568 READ_UE_OR_RETURN(&data);
569 READ_UE_OR_RETURN(&data);
570 READ_UE_OR_RETURN(&data);
571 READ_UE_OR_RETURN(&data);
572 READ_UE_OR_RETURN(&sps->max_num_reorder_frames);
573 READ_UE_OR_RETURN(&sps->max_dec_frame_buffering);
574 TRUE_OR_RETURN(sps->max_dec_frame_buffering >= sps->max_num_ref_frames);
576 sps->max_num_reorder_frames, 0, sps->max_dec_frame_buffering);
582 static void FillDefaultSeqScalingLists(H264Sps* sps) {
583 for (
int i = 0; i < 6; ++i)
584 for (
int j = 0; j < kH264ScalingList4x4Length; ++j)
585 sps->scaling_list4x4[i][j] = 16;
587 for (
int i = 0; i < 6; ++i)
588 for (
int j = 0; j < kH264ScalingList8x8Length; ++j)
589 sps->scaling_list8x8[i][j] = 16;
592 H264Parser::Result H264Parser::ParseSps(
const Nalu& nalu,
int* sps_id) {
596 H26xBitReader reader;
597 reader.Initialize(nalu.data() + nalu.header_size(), nalu.payload_size());
598 H26xBitReader* br = &reader;
602 std::unique_ptr<H264Sps> sps(
new H264Sps());
604 READ_BITS_OR_RETURN(8, &sps->profile_idc);
605 READ_BOOL_OR_RETURN(&sps->constraint_set0_flag);
606 READ_BOOL_OR_RETURN(&sps->constraint_set1_flag);
607 READ_BOOL_OR_RETURN(&sps->constraint_set2_flag);
608 READ_BOOL_OR_RETURN(&sps->constraint_set3_flag);
609 READ_BOOL_OR_RETURN(&sps->constraint_set4_flag);
610 READ_BOOL_OR_RETURN(&sps->constraint_set5_flag);
611 READ_BITS_OR_RETURN(2, &data);
612 READ_BITS_OR_RETURN(8, &sps->level_idc);
613 READ_UE_OR_RETURN(&sps->seq_parameter_set_id);
614 TRUE_OR_RETURN(sps->seq_parameter_set_id < 32);
616 if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
617 sps->profile_idc == 122 || sps->profile_idc == 244 ||
618 sps->profile_idc == 44 || sps->profile_idc == 83 ||
619 sps->profile_idc == 86 || sps->profile_idc == 118 ||
620 sps->profile_idc == 128) {
621 READ_UE_OR_RETURN(&sps->chroma_format_idc);
622 TRUE_OR_RETURN(sps->chroma_format_idc < 4);
624 if (sps->chroma_format_idc == 3)
625 READ_BOOL_OR_RETURN(&sps->separate_colour_plane_flag);
627 READ_UE_OR_RETURN(&sps->bit_depth_luma_minus8);
628 TRUE_OR_RETURN(sps->bit_depth_luma_minus8 < 7);
630 READ_UE_OR_RETURN(&sps->bit_depth_chroma_minus8);
631 TRUE_OR_RETURN(sps->bit_depth_chroma_minus8 < 7);
633 READ_BOOL_OR_RETURN(&sps->qpprime_y_zero_transform_bypass_flag);
634 READ_BOOL_OR_RETURN(&sps->seq_scaling_matrix_present_flag);
636 if (sps->seq_scaling_matrix_present_flag) {
637 DVLOG(4) <<
"Scaling matrix present";
638 res = ParseSpsScalingLists(br, sps.get());
642 FillDefaultSeqScalingLists(sps.get());
645 sps->chroma_format_idc = 1;
646 FillDefaultSeqScalingLists(sps.get());
649 if (sps->separate_colour_plane_flag)
650 sps->chroma_array_type = 0;
652 sps->chroma_array_type = sps->chroma_format_idc;
654 READ_UE_OR_RETURN(&sps->log2_max_frame_num_minus4);
655 TRUE_OR_RETURN(sps->log2_max_frame_num_minus4 < 13);
657 READ_UE_OR_RETURN(&sps->pic_order_cnt_type);
658 TRUE_OR_RETURN(sps->pic_order_cnt_type < 3);
660 sps->expected_delta_per_pic_order_cnt_cycle = 0;
661 if (sps->pic_order_cnt_type == 0) {
662 READ_UE_OR_RETURN(&sps->log2_max_pic_order_cnt_lsb_minus4);
663 TRUE_OR_RETURN(sps->log2_max_pic_order_cnt_lsb_minus4 < 13);
664 }
else if (sps->pic_order_cnt_type == 1) {
665 READ_BOOL_OR_RETURN(&sps->delta_pic_order_always_zero_flag);
666 READ_SE_OR_RETURN(&sps->offset_for_non_ref_pic);
667 READ_SE_OR_RETURN(&sps->offset_for_top_to_bottom_field);
668 READ_UE_OR_RETURN(&sps->num_ref_frames_in_pic_order_cnt_cycle);
669 TRUE_OR_RETURN(sps->num_ref_frames_in_pic_order_cnt_cycle < 255);
671 for (
int i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; ++i) {
672 READ_SE_OR_RETURN(&sps->offset_for_ref_frame[i]);
673 sps->expected_delta_per_pic_order_cnt_cycle +=
674 sps->offset_for_ref_frame[i];
678 READ_UE_OR_RETURN(&sps->max_num_ref_frames);
679 READ_BOOL_OR_RETURN(&sps->gaps_in_frame_num_value_allowed_flag);
681 READ_UE_OR_RETURN(&sps->pic_width_in_mbs_minus1);
682 READ_UE_OR_RETURN(&sps->pic_height_in_map_units_minus1);
684 READ_BOOL_OR_RETURN(&sps->frame_mbs_only_flag);
685 if (!sps->frame_mbs_only_flag)
686 READ_BOOL_OR_RETURN(&sps->mb_adaptive_frame_field_flag);
688 READ_BOOL_OR_RETURN(&sps->direct_8x8_inference_flag);
690 READ_BOOL_OR_RETURN(&sps->frame_cropping_flag);
691 if (sps->frame_cropping_flag) {
692 READ_UE_OR_RETURN(&sps->frame_crop_left_offset);
693 READ_UE_OR_RETURN(&sps->frame_crop_right_offset);
694 READ_UE_OR_RETURN(&sps->frame_crop_top_offset);
695 READ_UE_OR_RETURN(&sps->frame_crop_bottom_offset);
698 READ_BOOL_OR_RETURN(&sps->vui_parameters_present_flag);
699 if (sps->vui_parameters_present_flag) {
700 DVLOG(4) <<
"VUI parameters present";
701 res = ParseVUIParameters(br, sps.get());
707 *sps_id = sps->seq_parameter_set_id;
708 active_SPSes_[*sps_id] = std::move(sps);
713 H264Parser::Result H264Parser::ParsePps(
const Nalu& nalu,
int* pps_id) {
717 H26xBitReader reader;
718 reader.Initialize(nalu.data() + nalu.header_size(), nalu.payload_size());
719 H26xBitReader* br = &reader;
723 std::unique_ptr<H264Pps> pps(
new H264Pps());
725 READ_UE_OR_RETURN(&pps->pic_parameter_set_id);
726 READ_UE_OR_RETURN(&pps->seq_parameter_set_id);
727 TRUE_OR_RETURN(pps->seq_parameter_set_id < 32);
729 sps = GetSps(pps->seq_parameter_set_id);
732 READ_BOOL_OR_RETURN(&pps->entropy_coding_mode_flag);
733 READ_BOOL_OR_RETURN(&pps->bottom_field_pic_order_in_frame_present_flag);
735 READ_UE_OR_RETURN(&pps->num_slice_groups_minus1);
736 if (pps->num_slice_groups_minus1 > 1) {
737 LOG_ERROR_ONCE(
"Slice groups not supported");
738 return kUnsupportedStream;
741 READ_UE_OR_RETURN(&pps->num_ref_idx_l0_default_active_minus1);
742 TRUE_OR_RETURN(pps->num_ref_idx_l0_default_active_minus1 < 32);
744 READ_UE_OR_RETURN(&pps->num_ref_idx_l1_default_active_minus1);
745 TRUE_OR_RETURN(pps->num_ref_idx_l1_default_active_minus1 < 32);
747 READ_BOOL_OR_RETURN(&pps->weighted_pred_flag);
748 READ_BITS_OR_RETURN(2, &pps->weighted_bipred_idc);
749 TRUE_OR_RETURN(pps->weighted_bipred_idc < 3);
751 READ_SE_OR_RETURN(&pps->pic_init_qp_minus26);
752 IN_RANGE_OR_RETURN(pps->pic_init_qp_minus26, -26, 25);
754 READ_SE_OR_RETURN(&pps->pic_init_qs_minus26);
755 IN_RANGE_OR_RETURN(pps->pic_init_qs_minus26, -26, 25);
757 READ_SE_OR_RETURN(&pps->chroma_qp_index_offset);
758 IN_RANGE_OR_RETURN(pps->chroma_qp_index_offset, -12, 12);
759 pps->second_chroma_qp_index_offset = pps->chroma_qp_index_offset;
761 READ_BOOL_OR_RETURN(&pps->deblocking_filter_control_present_flag);
762 READ_BOOL_OR_RETURN(&pps->constrained_intra_pred_flag);
763 READ_BOOL_OR_RETURN(&pps->redundant_pic_cnt_present_flag);
765 if (br->HasMoreRBSPData()) {
766 READ_BOOL_OR_RETURN(&pps->transform_8x8_mode_flag);
767 READ_BOOL_OR_RETURN(&pps->pic_scaling_matrix_present_flag);
769 if (pps->pic_scaling_matrix_present_flag) {
770 DVLOG(4) <<
"Picture scaling matrix present";
771 res = ParsePpsScalingLists(br, *sps, pps.get());
776 READ_SE_OR_RETURN(&pps->second_chroma_qp_index_offset);
780 *pps_id = pps->pic_parameter_set_id;
781 active_PPSes_[*pps_id] = std::move(pps);
786 H264Parser::Result H264Parser::ParseRefPicListModification(
788 int num_ref_idx_active_minus1,
789 H264ModificationOfPicNum* ref_list_mods) {
790 H264ModificationOfPicNum* pic_num_mod;
792 if (num_ref_idx_active_minus1 >= 32)
793 return kInvalidStream;
795 for (
int i = 0; i < 32; ++i) {
796 pic_num_mod = &ref_list_mods[i];
797 READ_UE_OR_RETURN(&pic_num_mod->modification_of_pic_nums_idc);
798 TRUE_OR_RETURN(pic_num_mod->modification_of_pic_nums_idc < 4);
800 switch (pic_num_mod->modification_of_pic_nums_idc) {
803 READ_UE_OR_RETURN(&pic_num_mod->abs_diff_pic_num_minus1);
807 READ_UE_OR_RETURN(&pic_num_mod->long_term_pic_num);
813 return kInvalidStream;
817 return kInvalidStream;
823 int modification_of_pic_nums_idc;
824 READ_UE_OR_RETURN(&modification_of_pic_nums_idc);
825 TRUE_OR_RETURN(modification_of_pic_nums_idc == 3);
830 H264Parser::Result H264Parser::ParseRefPicListModifications(
831 H26xBitReader* br, H264SliceHeader* shdr) {
834 if (!shdr->IsISlice() && !shdr->IsSISlice()) {
835 READ_BOOL_OR_RETURN(&shdr->ref_pic_list_modification_flag_l0);
836 if (shdr->ref_pic_list_modification_flag_l0) {
837 res = ParseRefPicListModification(br, shdr->num_ref_idx_l0_active_minus1,
838 shdr->ref_list_l0_modifications);
844 if (shdr->IsBSlice()) {
845 READ_BOOL_OR_RETURN(&shdr->ref_pic_list_modification_flag_l1);
846 if (shdr->ref_pic_list_modification_flag_l1) {
847 res = ParseRefPicListModification(br, shdr->num_ref_idx_l1_active_minus1,
848 shdr->ref_list_l1_modifications);
857 H264Parser::Result H264Parser::ParseWeightingFactors(
859 int num_ref_idx_active_minus1,
860 int chroma_array_type,
861 int luma_log2_weight_denom,
862 int chroma_log2_weight_denom,
863 H264WeightingFactors* w_facts) {
864 int def_luma_weight = 1 << luma_log2_weight_denom;
865 int def_chroma_weight = 1 << chroma_log2_weight_denom;
867 for (
int i = 0; i < num_ref_idx_active_minus1 + 1; ++i) {
868 READ_BOOL_OR_RETURN(&w_facts->luma_weight_flag[i]);
869 if (w_facts->luma_weight_flag[i]) {
870 READ_SE_OR_RETURN(&w_facts->luma_weight[i]);
871 IN_RANGE_OR_RETURN(w_facts->luma_weight[i], -128, 127);
873 READ_SE_OR_RETURN(&w_facts->luma_offset[i]);
874 IN_RANGE_OR_RETURN(w_facts->luma_offset[i], -128, 127);
876 w_facts->luma_weight[i] = def_luma_weight;
877 w_facts->luma_offset[i] = 0;
880 if (chroma_array_type != 0) {
881 READ_BOOL_OR_RETURN(&w_facts->chroma_weight_flag[i]);
882 if (w_facts->chroma_weight_flag[i]) {
883 for (
int j = 0; j < 2; ++j) {
884 READ_SE_OR_RETURN(&w_facts->chroma_weight[i][j]);
885 IN_RANGE_OR_RETURN(w_facts->chroma_weight[i][j], -128, 127);
887 READ_SE_OR_RETURN(&w_facts->chroma_offset[i][j]);
888 IN_RANGE_OR_RETURN(w_facts->chroma_offset[i][j], -128, 127);
891 for (
int j = 0; j < 2; ++j) {
892 w_facts->chroma_weight[i][j] = def_chroma_weight;
893 w_facts->chroma_offset[i][j] = 0;
902 H264Parser::Result H264Parser::ParsePredWeightTable(H26xBitReader* br,
904 H264SliceHeader* shdr) {
905 READ_UE_OR_RETURN(&shdr->luma_log2_weight_denom);
906 TRUE_OR_RETURN(shdr->luma_log2_weight_denom < 8);
908 if (sps.chroma_array_type != 0)
909 READ_UE_OR_RETURN(&shdr->chroma_log2_weight_denom);
910 TRUE_OR_RETURN(shdr->chroma_log2_weight_denom < 8);
912 Result res = ParseWeightingFactors(br,
913 shdr->num_ref_idx_l0_active_minus1,
914 sps.chroma_array_type,
915 shdr->luma_log2_weight_denom,
916 shdr->chroma_log2_weight_denom,
917 &shdr->pred_weight_table_l0);
921 if (shdr->IsBSlice()) {
922 res = ParseWeightingFactors(br,
923 shdr->num_ref_idx_l1_active_minus1,
924 sps.chroma_array_type,
925 shdr->luma_log2_weight_denom,
926 shdr->chroma_log2_weight_denom,
927 &shdr->pred_weight_table_l1);
935 H264Parser::Result H264Parser::ParseDecRefPicMarking(H26xBitReader* br,
936 H264SliceHeader* shdr) {
937 if (shdr->idr_pic_flag) {
938 READ_BOOL_OR_RETURN(&shdr->no_output_of_prior_pics_flag);
939 READ_BOOL_OR_RETURN(&shdr->long_term_reference_flag);
941 READ_BOOL_OR_RETURN(&shdr->adaptive_ref_pic_marking_mode_flag);
943 H264DecRefPicMarking* marking;
944 if (shdr->adaptive_ref_pic_marking_mode_flag) {
946 for (i = 0; i < arraysize(shdr->ref_pic_marking); ++i) {
947 marking = &shdr->ref_pic_marking[i];
949 READ_UE_OR_RETURN(&marking->memory_mgmnt_control_operation);
950 if (marking->memory_mgmnt_control_operation == 0)
953 if (marking->memory_mgmnt_control_operation == 1 ||
954 marking->memory_mgmnt_control_operation == 3)
955 READ_UE_OR_RETURN(&marking->difference_of_pic_nums_minus1);
957 if (marking->memory_mgmnt_control_operation == 2)
958 READ_UE_OR_RETURN(&marking->long_term_pic_num);
960 if (marking->memory_mgmnt_control_operation == 3 ||
961 marking->memory_mgmnt_control_operation == 6)
962 READ_UE_OR_RETURN(&marking->long_term_frame_idx);
964 if (marking->memory_mgmnt_control_operation == 4)
965 READ_UE_OR_RETURN(&marking->max_long_term_frame_idx_plus1);
967 if (marking->memory_mgmnt_control_operation > 6)
968 return kInvalidStream;
971 if (i == arraysize(shdr->ref_pic_marking)) {
972 LOG_ERROR_ONCE(
"Ran out of dec ref pic marking fields");
973 return kUnsupportedStream;
981 H264Parser::Result H264Parser::ParseSliceHeader(
const Nalu& nalu,
982 H264SliceHeader* shdr) {
987 H26xBitReader reader;
988 reader.Initialize(nalu.data() + nalu.header_size(), nalu.payload_size());
989 H26xBitReader* br = &reader;
991 memset(shdr, 0,
sizeof(*shdr));
993 shdr->idr_pic_flag = (nalu.type() == 5);
994 shdr->nal_ref_idc = nalu.ref_idc();
995 shdr->nalu_data = nalu.data();
996 shdr->nalu_size = nalu.header_size() + nalu.payload_size();
998 READ_UE_OR_RETURN(&shdr->first_mb_in_slice);
999 READ_UE_OR_RETURN(&shdr->slice_type);
1000 TRUE_OR_RETURN(shdr->slice_type < 10);
1002 READ_UE_OR_RETURN(&shdr->pic_parameter_set_id);
1004 pps = GetPps(shdr->pic_parameter_set_id);
1005 TRUE_OR_RETURN(pps);
1007 sps = GetSps(pps->seq_parameter_set_id);
1008 TRUE_OR_RETURN(sps);
1010 if (sps->separate_colour_plane_flag) {
1011 LOG_ERROR_ONCE(
"Interlaced streams not supported");
1012 return kUnsupportedStream;
1015 READ_BITS_OR_RETURN(sps->log2_max_frame_num_minus4 + 4, &shdr->frame_num);
1016 if (!sps->frame_mbs_only_flag) {
1017 READ_BOOL_OR_RETURN(&shdr->field_pic_flag);
1018 if (shdr->field_pic_flag) {
1019 LOG_ERROR_ONCE(
"Interlaced streams not supported");
1020 return kUnsupportedStream;
1024 if (shdr->idr_pic_flag)
1025 READ_UE_OR_RETURN(&shdr->idr_pic_id);
1027 if (sps->pic_order_cnt_type == 0) {
1028 READ_BITS_OR_RETURN(sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
1029 &shdr->pic_order_cnt_lsb);
1030 if (pps->bottom_field_pic_order_in_frame_present_flag &&
1031 !shdr->field_pic_flag)
1032 READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt_bottom);
1035 if (sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag) {
1036 READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt[0]);
1037 if (pps->bottom_field_pic_order_in_frame_present_flag &&
1038 !shdr->field_pic_flag)
1039 READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt[1]);
1042 if (pps->redundant_pic_cnt_present_flag) {
1043 READ_UE_OR_RETURN(&shdr->redundant_pic_cnt);
1044 TRUE_OR_RETURN(shdr->redundant_pic_cnt < 128);
1047 if (shdr->IsBSlice())
1048 READ_BOOL_OR_RETURN(&shdr->direct_spatial_mv_pred_flag);
1050 if (shdr->IsPSlice() || shdr->IsSPSlice() || shdr->IsBSlice()) {
1051 READ_BOOL_OR_RETURN(&shdr->num_ref_idx_active_override_flag);
1052 if (shdr->num_ref_idx_active_override_flag) {
1053 READ_UE_OR_RETURN(&shdr->num_ref_idx_l0_active_minus1);
1054 if (shdr->IsBSlice())
1055 READ_UE_OR_RETURN(&shdr->num_ref_idx_l1_active_minus1);
1057 shdr->num_ref_idx_l0_active_minus1 =
1058 pps->num_ref_idx_l0_default_active_minus1;
1059 if (shdr->IsBSlice()) {
1060 shdr->num_ref_idx_l1_active_minus1 =
1061 pps->num_ref_idx_l1_default_active_minus1;
1065 if (shdr->field_pic_flag) {
1066 TRUE_OR_RETURN(shdr->num_ref_idx_l0_active_minus1 < 32);
1067 TRUE_OR_RETURN(shdr->num_ref_idx_l1_active_minus1 < 32);
1069 TRUE_OR_RETURN(shdr->num_ref_idx_l0_active_minus1 < 16);
1070 TRUE_OR_RETURN(shdr->num_ref_idx_l1_active_minus1 < 16);
1073 if (nalu.type() == Nalu::H264_CodedSliceExtension) {
1074 return kUnsupportedStream;
1076 res = ParseRefPicListModifications(br, shdr);
1081 if ((pps->weighted_pred_flag && (shdr->IsPSlice() || shdr->IsSPSlice())) ||
1082 (pps->weighted_bipred_idc == 1 && shdr->IsBSlice())) {
1083 res = ParsePredWeightTable(br, *sps, shdr);
1088 if (nalu.ref_idc() != 0) {
1089 res = ParseDecRefPicMarking(br, shdr);
1094 if (pps->entropy_coding_mode_flag && !shdr->IsISlice() &&
1095 !shdr->IsSISlice()) {
1096 READ_UE_OR_RETURN(&shdr->cabac_init_idc);
1097 TRUE_OR_RETURN(shdr->cabac_init_idc < 3);
1100 READ_SE_OR_RETURN(&shdr->slice_qp_delta);
1102 if (shdr->IsSPSlice() || shdr->IsSISlice()) {
1103 if (shdr->IsSPSlice())
1104 READ_BOOL_OR_RETURN(&shdr->sp_for_switch_flag);
1105 READ_SE_OR_RETURN(&shdr->slice_qs_delta);
1108 if (pps->deblocking_filter_control_present_flag) {
1109 READ_UE_OR_RETURN(&shdr->disable_deblocking_filter_idc);
1110 TRUE_OR_RETURN(shdr->disable_deblocking_filter_idc < 3);
1112 if (shdr->disable_deblocking_filter_idc != 1) {
1113 READ_SE_OR_RETURN(&shdr->slice_alpha_c0_offset_div2);
1114 IN_RANGE_OR_RETURN(shdr->slice_alpha_c0_offset_div2, -6, 6);
1116 READ_SE_OR_RETURN(&shdr->slice_beta_offset_div2);
1117 IN_RANGE_OR_RETURN(shdr->slice_beta_offset_div2, -6, 6);
1121 if (pps->num_slice_groups_minus1 > 0) {
1122 LOG_ERROR_ONCE(
"Slice groups not supported");
1123 return kUnsupportedStream;
1126 shdr->header_bit_size = nalu.payload_size() * 8 - br->NumBitsLeft();
1130 H264Parser::Result H264Parser::ParseSEI(
const Nalu& nalu,
1131 H264SEIMessage* sei_msg) {
1133 H26xBitReader reader;
1134 reader.Initialize(nalu.data() + nalu.header_size(), nalu.payload_size());
1135 H26xBitReader* br = &reader;
1137 memset(sei_msg, 0,
sizeof(*sei_msg));
1139 READ_BITS_OR_RETURN(8, &byte);
1140 while (byte == 0xff) {
1141 sei_msg->type += 255;
1142 READ_BITS_OR_RETURN(8, &byte);
1144 sei_msg->type += byte;
1146 READ_BITS_OR_RETURN(8, &byte);
1147 while (byte == 0xff) {
1148 sei_msg->payload_size += 255;
1149 READ_BITS_OR_RETURN(8, &byte);
1151 sei_msg->payload_size += byte;
1153 DVLOG(4) <<
"Found SEI message type: " << sei_msg->type
1154 <<
" payload size: " << sei_msg->payload_size;
1156 switch (sei_msg->type) {
1157 case H264SEIMessage::kSEIRecoveryPoint:
1158 READ_UE_OR_RETURN(&sei_msg->recovery_point.recovery_frame_cnt);
1159 READ_BOOL_OR_RETURN(&sei_msg->recovery_point.exact_match_flag);
1160 READ_BOOL_OR_RETURN(&sei_msg->recovery_point.broken_link_flag);
1161 READ_BITS_OR_RETURN(2, &sei_msg->recovery_point.changing_slice_group_idc);
1165 DVLOG(4) <<
"Unsupported SEI message";
All the methods that are virtual are virtual for mocking.