5 #include "packager/media/filters/h264_parser.h"
7 #include "packager/base/logging.h"
8 #include "packager/base/memory/scoped_ptr.h"
9 #include "packager/base/stl_util.h"
10 #include "packager/media/base/buffer_reader.h"
12 namespace edash_packager {
17 bool ExtractResolutionFromSps(
const H264SPS& sps,
18 uint32_t* coded_width,
19 uint32_t* coded_height,
20 uint32_t* pixel_width,
21 uint32_t* pixel_height) {
24 if (sps.frame_cropping_flag) {
28 switch (sps.chroma_format_idc) {
48 LOG(ERROR) <<
"Unexpected chroma_format_idc " << sps.chroma_format_idc;
53 int crop_unit_x = sub_width_c;
54 int crop_unit_y = sub_height_c * (2 - (sps.frame_mbs_only_flag ? 1 : 0));
55 crop_x = crop_unit_x *
56 (sps.frame_crop_left_offset + sps.frame_crop_right_offset);
57 crop_y = crop_unit_y *
58 (sps.frame_crop_top_offset + sps.frame_crop_bottom_offset);
62 int pic_width_in_mbs = sps.pic_width_in_mbs_minus1 + 1;
63 *coded_width = pic_width_in_mbs * 16 - crop_x;
66 int pic_height_in_mbs = (2 - (sps.frame_mbs_only_flag ? 1 : 0)) *
67 (sps.pic_height_in_map_units_minus1 + 1);
68 *coded_height = pic_height_in_mbs * 16 - crop_y;
71 *pixel_width = sps.sar_width == 0 ? 1 : sps.sar_width;
72 *pixel_height = sps.sar_height == 0 ? 1 : sps.sar_height;
73 DVLOG(2) <<
"Found coded_width: " << *coded_width
74 <<
" coded_height: " << *coded_height
75 <<
" pixel_width: " << *pixel_width
76 <<
" pixel_height: " << *pixel_height;
80 bool H264SliceHeader::IsPSlice()
const {
81 return (slice_type % 5 == kPSlice);
84 bool H264SliceHeader::IsBSlice()
const {
85 return (slice_type % 5 == kBSlice);
88 bool H264SliceHeader::IsISlice()
const {
89 return (slice_type % 5 == kISlice);
92 bool H264SliceHeader::IsSPSlice()
const {
93 return (slice_type % 5 == kSPSlice);
96 bool H264SliceHeader::IsSISlice()
const {
97 return (slice_type % 5 == kSISlice);
100 H264NALU::H264NALU() {
101 memset(
this, 0,
sizeof(*
this));
105 memset(
this, 0,
sizeof(*
this));
109 memset(
this, 0,
sizeof(*
this));
112 H264SliceHeader::H264SliceHeader() {
113 memset(
this, 0,
sizeof(*
this));
116 H264SEIMessage::H264SEIMessage() {
117 memset(
this, 0,
sizeof(*
this));
120 #define READ_BITS_OR_RETURN(num_bits, out) \
123 if (!br_.ReadBits(num_bits, &_out)) { \
125 << "Error in stream: unexpected EOS while trying to read " #out; \
126 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; \
142 #define READ_UE_OR_RETURN(out) \
144 if (ReadUE(out) != kOk) { \
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 (ReadSE(out) != kOk) { \
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 COMPILE_ASSERT(arraysize(kTableSarWidth) == arraysize(kTableSarHeight),
189 sar_tables_must_have_same_size);
191 H264Parser::H264Parser() {
195 H264Parser::~H264Parser() {
196 STLDeleteValues(&active_SPSes_);
197 STLDeleteValues(&active_PPSes_);
200 void H264Parser::Reset() {
205 void H264Parser::SetStream(
const uint8_t* stream, off_t stream_size) {
207 DCHECK_GT(stream_size, 0);
210 bytes_left_ = stream_size;
213 const H264PPS* H264Parser::GetPPS(
int pps_id) {
214 return active_PPSes_[pps_id];
217 const H264SPS* H264Parser::GetSPS(
int sps_id) {
218 return active_SPSes_[sps_id];
221 static inline bool IsStartCode(
const uint8_t* data) {
222 return data[0] == 0x00 && data[1] == 0x00 && data[2] == 0x01;
226 bool H264Parser::FindStartCode(
const uint8_t* data,
229 off_t* start_code_size) {
230 DCHECK_GE(data_size, 0);
231 off_t bytes_left = data_size;
233 while (bytes_left >= 3) {
234 if (IsStartCode(data)) {
236 *offset = data_size - bytes_left;
237 *start_code_size = 3;
241 if (*offset > 0 && *(data - 1) == 0x00) {
243 ++(*start_code_size);
258 *offset = data_size - bytes_left;
259 *start_code_size = 0;
263 bool H264Parser::LocateNALU(off_t* nalu_size, off_t* start_code_size) {
265 off_t nalu_start_off = 0;
266 off_t annexb_start_code_size = 0;
267 if (!FindStartCode(stream_, bytes_left_,
268 &nalu_start_off, &annexb_start_code_size)) {
269 DVLOG(4) <<
"Could not find start code, end of stream?";
274 stream_ += nalu_start_off;
275 bytes_left_ -= nalu_start_off;
277 const uint8_t* nalu_data = stream_ + annexb_start_code_size;
278 off_t max_nalu_data_size = bytes_left_ - annexb_start_code_size;
279 if (max_nalu_data_size <= 0) {
280 DVLOG(3) <<
"End of stream";
290 off_t next_start_code_size = 0;
291 off_t nalu_size_without_start_code = 0;
292 if (!FindStartCode(nalu_data, max_nalu_data_size,
293 &nalu_size_without_start_code, &next_start_code_size)) {
294 nalu_size_without_start_code = max_nalu_data_size;
296 *nalu_size = nalu_size_without_start_code + annexb_start_code_size;
297 *start_code_size = annexb_start_code_size;
301 H264Parser::Result H264Parser::ReadUE(
int* val) {
308 READ_BITS_OR_RETURN(1, &bit);
313 return kInvalidStream;
316 *val = (1 << num_bits) - 1;
319 READ_BITS_OR_RETURN(num_bits, &rest);
326 H264Parser::Result H264Parser::ReadSE(
int* val) {
343 H264Parser::Result H264Parser::AdvanceToNextNALU(H264NALU* nalu) {
344 off_t start_code_size;
345 off_t nalu_size_with_start_code;
346 if (!LocateNALU(&nalu_size_with_start_code, &start_code_size)) {
347 DVLOG(4) <<
"Could not find next NALU, bytes left in stream: "
352 nalu->data = stream_ + start_code_size;
353 nalu->size = nalu_size_with_start_code - start_code_size;
354 DVLOG(4) <<
"NALU found: size=" << nalu_size_with_start_code;
357 if (!br_.Initialize(nalu->data, nalu->size))
364 stream_ += nalu_size_with_start_code;
365 bytes_left_ -= nalu_size_with_start_code;
369 READ_BITS_OR_RETURN(1, &data);
370 TRUE_OR_RETURN(data == 0);
372 READ_BITS_OR_RETURN(2, &nalu->nal_ref_idc);
373 READ_BITS_OR_RETURN(5, &nalu->nal_unit_type);
375 DVLOG(4) <<
"NALU type: " <<
static_cast<int>(nalu->nal_unit_type)
376 <<
" at: " << reinterpret_cast<const void*>(nalu->data)
377 <<
" size: " << nalu->size
378 <<
" ref: " <<
static_cast<int>(nalu->nal_ref_idc);
384 static const int kDefault4x4Intra[kH264ScalingList4x4Length] = {
385 6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32, 32, 37, 37, 42, };
387 static const int kDefault4x4Inter[kH264ScalingList4x4Length] = {
388 10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27, 27, 30, 30, 34, };
390 static const int kDefault8x8Intra[kH264ScalingList8x8Length] = {
391 6, 10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18, 18, 18, 18, 23,
392 23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27,
393 27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31,
394 31, 33, 33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42, };
396 static const int kDefault8x8Inter[kH264ScalingList8x8Length] = {
397 9, 13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19, 19, 19, 19, 21,
398 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24, 24,
399 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27,
400 27, 28, 28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35, };
402 static inline void DefaultScalingList4x4(
404 int scaling_list4x4[][kH264ScalingList4x4Length]) {
408 memcpy(scaling_list4x4[i], kDefault4x4Intra,
sizeof(kDefault4x4Intra));
410 memcpy(scaling_list4x4[i], kDefault4x4Inter,
sizeof(kDefault4x4Inter));
413 static inline void DefaultScalingList8x8(
415 int scaling_list8x8[][kH264ScalingList8x8Length]) {
419 memcpy(scaling_list8x8[i], kDefault8x8Intra,
sizeof(kDefault8x8Intra));
421 memcpy(scaling_list8x8[i], kDefault8x8Inter,
sizeof(kDefault8x8Inter));
424 static void FallbackScalingList4x4(
426 const int default_scaling_list_intra[],
427 const int default_scaling_list_inter[],
428 int scaling_list4x4[][kH264ScalingList4x4Length]) {
429 static const int kScalingList4x4ByteSize =
430 sizeof(scaling_list4x4[0][0]) * kH264ScalingList4x4Length;
434 memcpy(scaling_list4x4[i], default_scaling_list_intra,
435 kScalingList4x4ByteSize);
439 memcpy(scaling_list4x4[i], scaling_list4x4[0], kScalingList4x4ByteSize);
443 memcpy(scaling_list4x4[i], scaling_list4x4[1], kScalingList4x4ByteSize);
447 memcpy(scaling_list4x4[i], default_scaling_list_inter,
448 kScalingList4x4ByteSize);
452 memcpy(scaling_list4x4[i], scaling_list4x4[3], kScalingList4x4ByteSize);
456 memcpy(scaling_list4x4[i], scaling_list4x4[4], kScalingList4x4ByteSize);
465 static void FallbackScalingList8x8(
467 const int default_scaling_list_intra[],
468 const int default_scaling_list_inter[],
469 int scaling_list8x8[][kH264ScalingList8x8Length]) {
470 static const int kScalingList8x8ByteSize =
471 sizeof(scaling_list8x8[0][0]) * kH264ScalingList8x8Length;
475 memcpy(scaling_list8x8[i], default_scaling_list_intra,
476 kScalingList8x8ByteSize);
480 memcpy(scaling_list8x8[i], default_scaling_list_inter,
481 kScalingList8x8ByteSize);
485 memcpy(scaling_list8x8[i], scaling_list8x8[0], kScalingList8x8ByteSize);
489 memcpy(scaling_list8x8[i], scaling_list8x8[1], kScalingList8x8ByteSize);
493 memcpy(scaling_list8x8[i], scaling_list8x8[2], kScalingList8x8ByteSize);
497 memcpy(scaling_list8x8[i], scaling_list8x8[3], kScalingList8x8ByteSize);
506 H264Parser::Result H264Parser::ParseScalingList(
int size,
514 *use_default =
false;
516 for (
int j = 0; j < size; ++j) {
517 if (next_scale != 0) {
518 READ_SE_OR_RETURN(&delta_scale);
519 IN_RANGE_OR_RETURN(delta_scale, -128, 127);
520 next_scale = (last_scale + delta_scale + 256) & 0xff;
522 if (j == 0 && next_scale == 0) {
528 scaling_list[j] = (next_scale == 0) ? last_scale : next_scale;
529 last_scale = scaling_list[j];
535 H264Parser::Result H264Parser::ParseSPSScalingLists(H264SPS* sps) {
537 bool seq_scaling_list_present_flag;
542 for (
int i = 0; i < 6; ++i) {
543 READ_BOOL_OR_RETURN(&seq_scaling_list_present_flag);
545 if (seq_scaling_list_present_flag) {
546 res = ParseScalingList(arraysize(sps->scaling_list4x4[i]),
547 sps->scaling_list4x4[i],
553 DefaultScalingList4x4(i, sps->scaling_list4x4);
556 FallbackScalingList4x4(
557 i, kDefault4x4Intra, kDefault4x4Inter, sps->scaling_list4x4);
562 for (
int i = 0; i < ((sps->chroma_format_idc != 3) ? 2 : 6); ++i) {
563 READ_BOOL_OR_RETURN(&seq_scaling_list_present_flag);
565 if (seq_scaling_list_present_flag) {
566 res = ParseScalingList(arraysize(sps->scaling_list8x8[i]),
567 sps->scaling_list8x8[i],
573 DefaultScalingList8x8(i, sps->scaling_list8x8);
576 FallbackScalingList8x8(
577 i, kDefault8x8Intra, kDefault8x8Inter, sps->scaling_list8x8);
584 H264Parser::Result H264Parser::ParsePPSScalingLists(
const H264SPS& sps,
587 bool pic_scaling_list_present_flag;
591 for (
int i = 0; i < 6; ++i) {
592 READ_BOOL_OR_RETURN(&pic_scaling_list_present_flag);
594 if (pic_scaling_list_present_flag) {
595 res = ParseScalingList(arraysize(pps->scaling_list4x4[i]),
596 pps->scaling_list4x4[i],
602 DefaultScalingList4x4(i, pps->scaling_list4x4);
605 if (sps.seq_scaling_matrix_present_flag) {
607 FallbackScalingList4x4(
608 i, kDefault4x4Intra, kDefault4x4Inter, pps->scaling_list4x4);
611 FallbackScalingList4x4(i,
612 sps.scaling_list4x4[0],
613 sps.scaling_list4x4[3],
614 pps->scaling_list4x4);
619 if (pps->transform_8x8_mode_flag) {
620 for (
int i = 0; i < ((sps.chroma_format_idc != 3) ? 2 : 6); ++i) {
621 READ_BOOL_OR_RETURN(&pic_scaling_list_present_flag);
623 if (pic_scaling_list_present_flag) {
624 res = ParseScalingList(arraysize(pps->scaling_list8x8[i]),
625 pps->scaling_list8x8[i],
631 DefaultScalingList8x8(i, pps->scaling_list8x8);
634 if (sps.seq_scaling_matrix_present_flag) {
636 FallbackScalingList8x8(
637 i, kDefault8x8Intra, kDefault8x8Inter, pps->scaling_list8x8);
640 FallbackScalingList8x8(i,
641 sps.scaling_list8x8[0],
642 sps.scaling_list8x8[1],
643 pps->scaling_list8x8);
651 H264Parser::Result H264Parser::ParseAndIgnoreHRDParameters(
652 bool* hrd_parameters_present) {
654 READ_BOOL_OR_RETURN(&data);
658 *hrd_parameters_present =
true;
661 READ_UE_OR_RETURN(&cpb_cnt_minus1);
662 IN_RANGE_OR_RETURN(cpb_cnt_minus1, 0, 31);
663 READ_BITS_OR_RETURN(8, &data);
664 for (
int i = 0; i <= cpb_cnt_minus1; ++i) {
665 READ_UE_OR_RETURN(&data);
666 READ_UE_OR_RETURN(&data);
667 READ_BOOL_OR_RETURN(&data);
669 READ_BITS_OR_RETURN(20, &data);
674 H264Parser::Result H264Parser::ParseVUIParameters(H264SPS* sps) {
675 bool aspect_ratio_info_present_flag;
676 READ_BOOL_OR_RETURN(&aspect_ratio_info_present_flag);
677 if (aspect_ratio_info_present_flag) {
678 int aspect_ratio_idc;
679 READ_BITS_OR_RETURN(8, &aspect_ratio_idc);
680 if (aspect_ratio_idc == kExtendedSar) {
681 READ_BITS_OR_RETURN(16, &sps->sar_width);
682 READ_BITS_OR_RETURN(16, &sps->sar_height);
684 const int max_aspect_ratio_idc = arraysize(kTableSarWidth) - 1;
685 IN_RANGE_OR_RETURN(aspect_ratio_idc, 0, max_aspect_ratio_idc);
686 sps->sar_width = kTableSarWidth[aspect_ratio_idc];
687 sps->sar_height = kTableSarHeight[aspect_ratio_idc];
693 READ_BOOL_OR_RETURN(&data);
695 READ_BOOL_OR_RETURN(&data);
697 READ_BOOL_OR_RETURN(&data);
699 READ_BITS_OR_RETURN(3, &data);
700 READ_BOOL_OR_RETURN(&data);
701 READ_BOOL_OR_RETURN(&data);
703 READ_BITS_OR_RETURN(24, &data);
706 READ_BOOL_OR_RETURN(&data);
708 READ_UE_OR_RETURN(&data);
709 READ_UE_OR_RETURN(&data);
713 READ_BOOL_OR_RETURN(&data);
715 READ_BITS_OR_RETURN(16, &data);
716 READ_BITS_OR_RETURN(16, &data);
717 READ_BITS_OR_RETURN(16, &data);
718 READ_BITS_OR_RETURN(16, &data);
719 READ_BOOL_OR_RETURN(&data);
723 bool hrd_parameters_present =
false;
724 Result res = ParseAndIgnoreHRDParameters(&hrd_parameters_present);
729 res = ParseAndIgnoreHRDParameters(&hrd_parameters_present);
733 if (hrd_parameters_present)
734 READ_BOOL_OR_RETURN(&data);
736 READ_BOOL_OR_RETURN(&data);
737 READ_BOOL_OR_RETURN(&sps->bitstream_restriction_flag);
738 if (sps->bitstream_restriction_flag) {
739 READ_BOOL_OR_RETURN(&data);
740 READ_UE_OR_RETURN(&data);
741 READ_UE_OR_RETURN(&data);
742 READ_UE_OR_RETURN(&data);
743 READ_UE_OR_RETURN(&data);
744 READ_UE_OR_RETURN(&sps->max_num_reorder_frames);
745 READ_UE_OR_RETURN(&sps->max_dec_frame_buffering);
746 TRUE_OR_RETURN(sps->max_dec_frame_buffering >= sps->max_num_ref_frames);
748 sps->max_num_reorder_frames, 0, sps->max_dec_frame_buffering);
754 static void FillDefaultSeqScalingLists(H264SPS* sps) {
755 for (
int i = 0; i < 6; ++i)
756 for (
int j = 0; j < kH264ScalingList4x4Length; ++j)
757 sps->scaling_list4x4[i][j] = 16;
759 for (
int i = 0; i < 6; ++i)
760 for (
int j = 0; j < kH264ScalingList8x8Length; ++j)
761 sps->scaling_list8x8[i][j] = 16;
764 H264Parser::Result H264Parser::ParseSPS(
int* sps_id) {
771 scoped_ptr<H264SPS> sps(
new H264SPS());
773 READ_BITS_OR_RETURN(8, &sps->profile_idc);
774 READ_BOOL_OR_RETURN(&sps->constraint_set0_flag);
775 READ_BOOL_OR_RETURN(&sps->constraint_set1_flag);
776 READ_BOOL_OR_RETURN(&sps->constraint_set2_flag);
777 READ_BOOL_OR_RETURN(&sps->constraint_set3_flag);
778 READ_BOOL_OR_RETURN(&sps->constraint_set4_flag);
779 READ_BOOL_OR_RETURN(&sps->constraint_set5_flag);
780 READ_BITS_OR_RETURN(2, &data);
781 READ_BITS_OR_RETURN(8, &sps->level_idc);
782 READ_UE_OR_RETURN(&sps->seq_parameter_set_id);
783 TRUE_OR_RETURN(sps->seq_parameter_set_id < 32);
785 if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
786 sps->profile_idc == 122 || sps->profile_idc == 244 ||
787 sps->profile_idc == 44 || sps->profile_idc == 83 ||
788 sps->profile_idc == 86 || sps->profile_idc == 118 ||
789 sps->profile_idc == 128) {
790 READ_UE_OR_RETURN(&sps->chroma_format_idc);
791 TRUE_OR_RETURN(sps->chroma_format_idc < 4);
793 if (sps->chroma_format_idc == 3)
794 READ_BOOL_OR_RETURN(&sps->separate_colour_plane_flag);
796 READ_UE_OR_RETURN(&sps->bit_depth_luma_minus8);
797 TRUE_OR_RETURN(sps->bit_depth_luma_minus8 < 7);
799 READ_UE_OR_RETURN(&sps->bit_depth_chroma_minus8);
800 TRUE_OR_RETURN(sps->bit_depth_chroma_minus8 < 7);
802 READ_BOOL_OR_RETURN(&sps->qpprime_y_zero_transform_bypass_flag);
803 READ_BOOL_OR_RETURN(&sps->seq_scaling_matrix_present_flag);
805 if (sps->seq_scaling_matrix_present_flag) {
806 DVLOG(4) <<
"Scaling matrix present";
807 res = ParseSPSScalingLists(sps.get());
811 FillDefaultSeqScalingLists(sps.get());
814 sps->chroma_format_idc = 1;
815 FillDefaultSeqScalingLists(sps.get());
818 if (sps->separate_colour_plane_flag)
819 sps->chroma_array_type = 0;
821 sps->chroma_array_type = sps->chroma_format_idc;
823 READ_UE_OR_RETURN(&sps->log2_max_frame_num_minus4);
824 TRUE_OR_RETURN(sps->log2_max_frame_num_minus4 < 13);
826 READ_UE_OR_RETURN(&sps->pic_order_cnt_type);
827 TRUE_OR_RETURN(sps->pic_order_cnt_type < 3);
829 sps->expected_delta_per_pic_order_cnt_cycle = 0;
830 if (sps->pic_order_cnt_type == 0) {
831 READ_UE_OR_RETURN(&sps->log2_max_pic_order_cnt_lsb_minus4);
832 TRUE_OR_RETURN(sps->log2_max_pic_order_cnt_lsb_minus4 < 13);
833 }
else if (sps->pic_order_cnt_type == 1) {
834 READ_BOOL_OR_RETURN(&sps->delta_pic_order_always_zero_flag);
835 READ_SE_OR_RETURN(&sps->offset_for_non_ref_pic);
836 READ_SE_OR_RETURN(&sps->offset_for_top_to_bottom_field);
837 READ_UE_OR_RETURN(&sps->num_ref_frames_in_pic_order_cnt_cycle);
838 TRUE_OR_RETURN(sps->num_ref_frames_in_pic_order_cnt_cycle < 255);
840 for (
int i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; ++i) {
841 READ_SE_OR_RETURN(&sps->offset_for_ref_frame[i]);
842 sps->expected_delta_per_pic_order_cnt_cycle +=
843 sps->offset_for_ref_frame[i];
847 READ_UE_OR_RETURN(&sps->max_num_ref_frames);
848 READ_BOOL_OR_RETURN(&sps->gaps_in_frame_num_value_allowed_flag);
850 if (sps->gaps_in_frame_num_value_allowed_flag)
851 return kUnsupportedStream;
853 READ_UE_OR_RETURN(&sps->pic_width_in_mbs_minus1);
854 READ_UE_OR_RETURN(&sps->pic_height_in_map_units_minus1);
856 READ_BOOL_OR_RETURN(&sps->frame_mbs_only_flag);
857 if (!sps->frame_mbs_only_flag)
858 READ_BOOL_OR_RETURN(&sps->mb_adaptive_frame_field_flag);
860 READ_BOOL_OR_RETURN(&sps->direct_8x8_inference_flag);
862 READ_BOOL_OR_RETURN(&sps->frame_cropping_flag);
863 if (sps->frame_cropping_flag) {
864 READ_UE_OR_RETURN(&sps->frame_crop_left_offset);
865 READ_UE_OR_RETURN(&sps->frame_crop_right_offset);
866 READ_UE_OR_RETURN(&sps->frame_crop_top_offset);
867 READ_UE_OR_RETURN(&sps->frame_crop_bottom_offset);
870 READ_BOOL_OR_RETURN(&sps->vui_parameters_present_flag);
871 if (sps->vui_parameters_present_flag) {
872 DVLOG(4) <<
"VUI parameters present";
873 res = ParseVUIParameters(sps.get());
879 *sps_id = sps->seq_parameter_set_id;
880 delete active_SPSes_[*sps_id];
881 active_SPSes_[*sps_id] = sps.release();
886 H264Parser::Result H264Parser::ParsePPS(
int* pps_id) {
893 scoped_ptr<H264PPS> pps(
new H264PPS());
895 READ_UE_OR_RETURN(&pps->pic_parameter_set_id);
896 READ_UE_OR_RETURN(&pps->seq_parameter_set_id);
897 TRUE_OR_RETURN(pps->seq_parameter_set_id < 32);
899 sps = GetSPS(pps->seq_parameter_set_id);
902 READ_BOOL_OR_RETURN(&pps->entropy_coding_mode_flag);
903 READ_BOOL_OR_RETURN(&pps->bottom_field_pic_order_in_frame_present_flag);
905 READ_UE_OR_RETURN(&pps->num_slice_groups_minus1);
906 if (pps->num_slice_groups_minus1 > 1) {
907 DVLOG(1) <<
"Slice groups not supported";
908 return kUnsupportedStream;
911 READ_UE_OR_RETURN(&pps->num_ref_idx_l0_default_active_minus1);
912 TRUE_OR_RETURN(pps->num_ref_idx_l0_default_active_minus1 < 32);
914 READ_UE_OR_RETURN(&pps->num_ref_idx_l1_default_active_minus1);
915 TRUE_OR_RETURN(pps->num_ref_idx_l1_default_active_minus1 < 32);
917 READ_BOOL_OR_RETURN(&pps->weighted_pred_flag);
918 READ_BITS_OR_RETURN(2, &pps->weighted_bipred_idc);
919 TRUE_OR_RETURN(pps->weighted_bipred_idc < 3);
921 READ_SE_OR_RETURN(&pps->pic_init_qp_minus26);
922 IN_RANGE_OR_RETURN(pps->pic_init_qp_minus26, -26, 25);
924 READ_SE_OR_RETURN(&pps->pic_init_qs_minus26);
925 IN_RANGE_OR_RETURN(pps->pic_init_qs_minus26, -26, 25);
927 READ_SE_OR_RETURN(&pps->chroma_qp_index_offset);
928 IN_RANGE_OR_RETURN(pps->chroma_qp_index_offset, -12, 12);
929 pps->second_chroma_qp_index_offset = pps->chroma_qp_index_offset;
931 READ_BOOL_OR_RETURN(&pps->deblocking_filter_control_present_flag);
932 READ_BOOL_OR_RETURN(&pps->constrained_intra_pred_flag);
933 READ_BOOL_OR_RETURN(&pps->redundant_pic_cnt_present_flag);
935 if (br_.HasMoreRBSPData()) {
936 READ_BOOL_OR_RETURN(&pps->transform_8x8_mode_flag);
937 READ_BOOL_OR_RETURN(&pps->pic_scaling_matrix_present_flag);
939 if (pps->pic_scaling_matrix_present_flag) {
940 DVLOG(4) <<
"Picture scaling matrix present";
941 res = ParsePPSScalingLists(*sps, pps.get());
946 READ_SE_OR_RETURN(&pps->second_chroma_qp_index_offset);
950 *pps_id = pps->pic_parameter_set_id;
951 delete active_PPSes_[*pps_id];
952 active_PPSes_[*pps_id] = pps.release();
957 H264Parser::Result H264Parser::ParseSPSFromArray(
958 const uint8_t* sps_data,
959 size_t sps_data_length,
961 br_.Initialize(sps_data, sps_data_length);
964 READ_BITS_OR_RETURN(1, &data);
966 TRUE_OR_RETURN(data == 0);
968 READ_BITS_OR_RETURN(2, &nal_ref_idc);
971 TRUE_OR_RETURN(nal_ref_idc != 0);
973 READ_BITS_OR_RETURN(5, &nal_unit_type);
974 TRUE_OR_RETURN(nal_unit_type == H264NALU::kSPS);
976 return ParseSPS(sps_id);
979 H264Parser::Result H264Parser::ParseRefPicListModification(
980 int num_ref_idx_active_minus1,
981 H264ModificationOfPicNum* ref_list_mods) {
982 H264ModificationOfPicNum* pic_num_mod;
984 if (num_ref_idx_active_minus1 >= 32)
985 return kInvalidStream;
987 for (
int i = 0; i < 32; ++i) {
988 pic_num_mod = &ref_list_mods[i];
989 READ_UE_OR_RETURN(&pic_num_mod->modification_of_pic_nums_idc);
990 TRUE_OR_RETURN(pic_num_mod->modification_of_pic_nums_idc < 4);
992 switch (pic_num_mod->modification_of_pic_nums_idc) {
995 READ_UE_OR_RETURN(&pic_num_mod->abs_diff_pic_num_minus1);
999 READ_UE_OR_RETURN(&pic_num_mod->long_term_pic_num);
1005 return kInvalidStream;
1009 return kInvalidStream;
1015 int modification_of_pic_nums_idc;
1016 READ_UE_OR_RETURN(&modification_of_pic_nums_idc);
1017 TRUE_OR_RETURN(modification_of_pic_nums_idc == 3);
1022 H264Parser::Result H264Parser::ParseRefPicListModifications(
1023 H264SliceHeader* shdr) {
1026 if (!shdr->IsISlice() && !shdr->IsSISlice()) {
1027 READ_BOOL_OR_RETURN(&shdr->ref_pic_list_modification_flag_l0);
1028 if (shdr->ref_pic_list_modification_flag_l0) {
1029 res = ParseRefPicListModification(shdr->num_ref_idx_l0_active_minus1,
1030 shdr->ref_list_l0_modifications);
1036 if (shdr->IsBSlice()) {
1037 READ_BOOL_OR_RETURN(&shdr->ref_pic_list_modification_flag_l1);
1038 if (shdr->ref_pic_list_modification_flag_l1) {
1039 res = ParseRefPicListModification(shdr->num_ref_idx_l1_active_minus1,
1040 shdr->ref_list_l1_modifications);
1049 H264Parser::Result H264Parser::ParseWeightingFactors(
1050 int num_ref_idx_active_minus1,
1051 int chroma_array_type,
1052 int luma_log2_weight_denom,
1053 int chroma_log2_weight_denom,
1054 H264WeightingFactors* w_facts) {
1056 int def_luma_weight = 1 << luma_log2_weight_denom;
1057 int def_chroma_weight = 1 << chroma_log2_weight_denom;
1059 for (
int i = 0; i < num_ref_idx_active_minus1 + 1; ++i) {
1060 READ_BOOL_OR_RETURN(&w_facts->luma_weight_flag);
1061 if (w_facts->luma_weight_flag) {
1062 READ_SE_OR_RETURN(&w_facts->luma_weight[i]);
1063 IN_RANGE_OR_RETURN(w_facts->luma_weight[i], -128, 127);
1065 READ_SE_OR_RETURN(&w_facts->luma_offset[i]);
1066 IN_RANGE_OR_RETURN(w_facts->luma_offset[i], -128, 127);
1068 w_facts->luma_weight[i] = def_luma_weight;
1069 w_facts->luma_offset[i] = 0;
1072 if (chroma_array_type != 0) {
1073 READ_BOOL_OR_RETURN(&w_facts->chroma_weight_flag);
1074 if (w_facts->chroma_weight_flag) {
1075 for (
int j = 0; j < 2; ++j) {
1076 READ_SE_OR_RETURN(&w_facts->chroma_weight[i][j]);
1077 IN_RANGE_OR_RETURN(w_facts->chroma_weight[i][j], -128, 127);
1079 READ_SE_OR_RETURN(&w_facts->chroma_offset[i][j]);
1080 IN_RANGE_OR_RETURN(w_facts->chroma_offset[i][j], -128, 127);
1083 for (
int j = 0; j < 2; ++j) {
1084 w_facts->chroma_weight[i][j] = def_chroma_weight;
1085 w_facts->chroma_offset[i][j] = 0;
1094 H264Parser::Result H264Parser::ParsePredWeightTable(
const H264SPS& sps,
1095 H264SliceHeader* shdr) {
1096 READ_UE_OR_RETURN(&shdr->luma_log2_weight_denom);
1097 TRUE_OR_RETURN(shdr->luma_log2_weight_denom < 8);
1099 if (sps.chroma_array_type != 0)
1100 READ_UE_OR_RETURN(&shdr->chroma_log2_weight_denom);
1101 TRUE_OR_RETURN(shdr->chroma_log2_weight_denom < 8);
1103 Result res = ParseWeightingFactors(shdr->num_ref_idx_l0_active_minus1,
1104 sps.chroma_array_type,
1105 shdr->luma_log2_weight_denom,
1106 shdr->chroma_log2_weight_denom,
1107 &shdr->pred_weight_table_l0);
1111 if (shdr->IsBSlice()) {
1112 res = ParseWeightingFactors(shdr->num_ref_idx_l1_active_minus1,
1113 sps.chroma_array_type,
1114 shdr->luma_log2_weight_denom,
1115 shdr->chroma_log2_weight_denom,
1116 &shdr->pred_weight_table_l1);
1124 H264Parser::Result H264Parser::ParseDecRefPicMarking(H264SliceHeader* shdr) {
1125 if (shdr->idr_pic_flag) {
1126 READ_BOOL_OR_RETURN(&shdr->no_output_of_prior_pics_flag);
1127 READ_BOOL_OR_RETURN(&shdr->long_term_reference_flag);
1129 READ_BOOL_OR_RETURN(&shdr->adaptive_ref_pic_marking_mode_flag);
1131 H264DecRefPicMarking* marking;
1132 if (shdr->adaptive_ref_pic_marking_mode_flag) {
1134 for (i = 0; i < arraysize(shdr->ref_pic_marking); ++i) {
1135 marking = &shdr->ref_pic_marking[i];
1137 READ_UE_OR_RETURN(&marking->memory_mgmnt_control_operation);
1138 if (marking->memory_mgmnt_control_operation == 0)
1141 if (marking->memory_mgmnt_control_operation == 1 ||
1142 marking->memory_mgmnt_control_operation == 3)
1143 READ_UE_OR_RETURN(&marking->difference_of_pic_nums_minus1);
1145 if (marking->memory_mgmnt_control_operation == 2)
1146 READ_UE_OR_RETURN(&marking->long_term_pic_num);
1148 if (marking->memory_mgmnt_control_operation == 3 ||
1149 marking->memory_mgmnt_control_operation == 6)
1150 READ_UE_OR_RETURN(&marking->long_term_frame_idx);
1152 if (marking->memory_mgmnt_control_operation == 4)
1153 READ_UE_OR_RETURN(&marking->max_long_term_frame_idx_plus1);
1155 if (marking->memory_mgmnt_control_operation > 6)
1156 return kInvalidStream;
1159 if (i == arraysize(shdr->ref_pic_marking)) {
1160 DVLOG(1) <<
"Ran out of dec ref pic marking fields";
1161 return kUnsupportedStream;
1169 H264Parser::Result H264Parser::ParseSliceHeader(
const H264NALU& nalu,
1170 H264SliceHeader* shdr) {
1176 memset(shdr, 0,
sizeof(*shdr));
1178 shdr->idr_pic_flag = (nalu.nal_unit_type == 5);
1179 shdr->nal_ref_idc = nalu.nal_ref_idc;
1180 shdr->nalu_data = nalu.data;
1181 shdr->nalu_size = nalu.size;
1183 READ_UE_OR_RETURN(&shdr->first_mb_in_slice);
1184 READ_UE_OR_RETURN(&shdr->slice_type);
1185 TRUE_OR_RETURN(shdr->slice_type < 10);
1187 READ_UE_OR_RETURN(&shdr->pic_parameter_set_id);
1189 pps = GetPPS(shdr->pic_parameter_set_id);
1190 TRUE_OR_RETURN(pps);
1192 sps = GetSPS(pps->seq_parameter_set_id);
1193 TRUE_OR_RETURN(sps);
1195 if (sps->separate_colour_plane_flag) {
1196 DVLOG(1) <<
"Interlaced streams not supported";
1197 return kUnsupportedStream;
1200 READ_BITS_OR_RETURN(sps->log2_max_frame_num_minus4 + 4, &shdr->frame_num);
1201 if (!sps->frame_mbs_only_flag) {
1202 READ_BOOL_OR_RETURN(&shdr->field_pic_flag);
1203 if (shdr->field_pic_flag) {
1204 DVLOG(1) <<
"Interlaced streams not supported";
1205 return kUnsupportedStream;
1209 if (shdr->idr_pic_flag)
1210 READ_UE_OR_RETURN(&shdr->idr_pic_id);
1212 if (sps->pic_order_cnt_type == 0) {
1213 READ_BITS_OR_RETURN(sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
1214 &shdr->pic_order_cnt_lsb);
1215 if (pps->bottom_field_pic_order_in_frame_present_flag &&
1216 !shdr->field_pic_flag)
1217 READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt_bottom);
1220 if (sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag) {
1221 READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt[0]);
1222 if (pps->bottom_field_pic_order_in_frame_present_flag &&
1223 !shdr->field_pic_flag)
1224 READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt[1]);
1227 if (pps->redundant_pic_cnt_present_flag) {
1228 READ_UE_OR_RETURN(&shdr->redundant_pic_cnt);
1229 TRUE_OR_RETURN(shdr->redundant_pic_cnt < 128);
1232 if (shdr->IsBSlice())
1233 READ_BOOL_OR_RETURN(&shdr->direct_spatial_mv_pred_flag);
1235 if (shdr->IsPSlice() || shdr->IsSPSlice() || shdr->IsBSlice()) {
1236 READ_BOOL_OR_RETURN(&shdr->num_ref_idx_active_override_flag);
1237 if (shdr->num_ref_idx_active_override_flag) {
1238 READ_UE_OR_RETURN(&shdr->num_ref_idx_l0_active_minus1);
1239 if (shdr->IsBSlice())
1240 READ_UE_OR_RETURN(&shdr->num_ref_idx_l1_active_minus1);
1242 shdr->num_ref_idx_l0_active_minus1 =
1243 pps->num_ref_idx_l0_default_active_minus1;
1244 if (shdr->IsBSlice()) {
1245 shdr->num_ref_idx_l1_active_minus1 =
1246 pps->num_ref_idx_l1_default_active_minus1;
1250 if (shdr->field_pic_flag) {
1251 TRUE_OR_RETURN(shdr->num_ref_idx_l0_active_minus1 < 32);
1252 TRUE_OR_RETURN(shdr->num_ref_idx_l1_active_minus1 < 32);
1254 TRUE_OR_RETURN(shdr->num_ref_idx_l0_active_minus1 < 16);
1255 TRUE_OR_RETURN(shdr->num_ref_idx_l1_active_minus1 < 16);
1258 if (nalu.nal_unit_type == H264NALU::kCodedSliceExtension) {
1259 return kUnsupportedStream;
1261 res = ParseRefPicListModifications(shdr);
1266 if ((pps->weighted_pred_flag && (shdr->IsPSlice() || shdr->IsSPSlice())) ||
1267 (pps->weighted_bipred_idc == 1 && shdr->IsBSlice())) {
1268 res = ParsePredWeightTable(*sps, shdr);
1273 if (nalu.nal_ref_idc != 0) {
1274 res = ParseDecRefPicMarking(shdr);
1279 if (pps->entropy_coding_mode_flag && !shdr->IsISlice() &&
1280 !shdr->IsSISlice()) {
1281 READ_UE_OR_RETURN(&shdr->cabac_init_idc);
1282 TRUE_OR_RETURN(shdr->cabac_init_idc < 3);
1285 READ_SE_OR_RETURN(&shdr->slice_qp_delta);
1287 if (shdr->IsSPSlice() || shdr->IsSISlice()) {
1288 if (shdr->IsSPSlice())
1289 READ_BOOL_OR_RETURN(&shdr->sp_for_switch_flag);
1290 READ_SE_OR_RETURN(&shdr->slice_qs_delta);
1293 if (pps->deblocking_filter_control_present_flag) {
1294 READ_UE_OR_RETURN(&shdr->disable_deblocking_filter_idc);
1295 TRUE_OR_RETURN(shdr->disable_deblocking_filter_idc < 3);
1297 if (shdr->disable_deblocking_filter_idc != 1) {
1298 READ_SE_OR_RETURN(&shdr->slice_alpha_c0_offset_div2);
1299 IN_RANGE_OR_RETURN(shdr->slice_alpha_c0_offset_div2, -6, 6);
1301 READ_SE_OR_RETURN(&shdr->slice_beta_offset_div2);
1302 IN_RANGE_OR_RETURN(shdr->slice_beta_offset_div2, -6, 6);
1306 if (pps->num_slice_groups_minus1 > 0) {
1307 DVLOG(1) <<
"Slice groups not supported";
1308 return kUnsupportedStream;
1311 size_t epb = br_.NumEmulationPreventionBytesRead();
1312 shdr->header_bit_size = (shdr->nalu_size - epb) * 8 - br_.NumBitsLeft();
1317 H264Parser::Result H264Parser::ParseSEI(H264SEIMessage* sei_msg) {
1320 memset(sei_msg, 0,
sizeof(*sei_msg));
1322 READ_BITS_OR_RETURN(8, &byte);
1323 while (byte == 0xff) {
1324 sei_msg->type += 255;
1325 READ_BITS_OR_RETURN(8, &byte);
1327 sei_msg->type += byte;
1329 READ_BITS_OR_RETURN(8, &byte);
1330 while (byte == 0xff) {
1331 sei_msg->payload_size += 255;
1332 READ_BITS_OR_RETURN(8, &byte);
1334 sei_msg->payload_size += byte;
1336 DVLOG(4) <<
"Found SEI message type: " << sei_msg->type
1337 <<
" payload size: " << sei_msg->payload_size;
1339 switch (sei_msg->type) {
1340 case H264SEIMessage::kSEIRecoveryPoint:
1341 READ_UE_OR_RETURN(&sei_msg->recovery_point.recovery_frame_cnt);
1342 READ_BOOL_OR_RETURN(&sei_msg->recovery_point.exact_match_flag);
1343 READ_BOOL_OR_RETURN(&sei_msg->recovery_point.broken_link_flag);
1344 READ_BITS_OR_RETURN(2, &sei_msg->recovery_point.changing_slice_group_idc);
1348 DVLOG(4) <<
"Unsupported SEI message";