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 {
18 LOG(ERROR) << "Failure while parsing AVCDecoderConfig: " << #x; \
23 bool ExtractResolutionFromDecoderConfig(
const uint8_t* avc_decoder_config_data,
24 size_t avc_decoder_config_data_size,
25 uint32_t* coded_width,
26 uint32_t* coded_height,
27 uint32_t* pixel_width,
28 uint32_t* pixel_height) {
29 BufferReader reader(avc_decoder_config_data, avc_decoder_config_data_size);
32 RCHECK(reader.Read1(&value));
36 RCHECK(reader.Read1(&value));
39 RCHECK(reader.Read1(&value));
42 RCHECK(reader.Read1(&value));
45 RCHECK(reader.Read1(&value));
47 RCHECK((value & 0xFC) == 0xFC);
50 RCHECK(reader.Read1(&value));
52 RCHECK((value & 0xE0) == 0xE0);
54 const uint8_t num_sps = value & 0x1F;
56 LOG(ERROR) <<
"No SPS found.";
59 uint16_t sps_length = 0;
60 RCHECK(reader.Read2(&sps_length));
62 return ExtractResolutionFromSpsData(reader.data() + reader.pos(), sps_length,
63 coded_width, coded_height, pixel_width,
70 bool ExtractResolutionFromSpsData(
const uint8_t* sps_data,
72 uint32_t* coded_width,
73 uint32_t* coded_height,
74 uint32_t* pixel_width,
75 uint32_t* pixel_height) {
78 RCHECK(parser.ParseSPSFromArray(sps_data, sps_data_size, &sps_id) ==
80 return ExtractResolutionFromSps(*parser.GetSPS(sps_id), coded_width,
81 coded_height, pixel_width, pixel_height);
86 bool ExtractResolutionFromSps(
const H264SPS& sps,
87 uint32_t* coded_width,
88 uint32_t* coded_height,
89 uint32_t* pixel_width,
90 uint32_t* pixel_height) {
93 if (sps.frame_cropping_flag) {
97 switch (sps.chroma_format_idc) {
117 LOG(ERROR) <<
"Unexpected chroma_format_idc " << sps.chroma_format_idc;
122 int crop_unit_x = sub_width_c;
123 int crop_unit_y = sub_height_c * (2 - (sps.frame_mbs_only_flag ? 1 : 0));
124 crop_x = crop_unit_x *
125 (sps.frame_crop_left_offset + sps.frame_crop_right_offset);
126 crop_y = crop_unit_y *
127 (sps.frame_crop_top_offset + sps.frame_crop_bottom_offset);
131 int pic_width_in_mbs = sps.pic_width_in_mbs_minus1 + 1;
132 *coded_width = pic_width_in_mbs * 16 - crop_x;
135 int pic_height_in_mbs = (2 - (sps.frame_mbs_only_flag ? 1 : 0)) *
136 (sps.pic_height_in_map_units_minus1 + 1);
137 *coded_height = pic_height_in_mbs * 16 - crop_y;
140 *pixel_width = sps.sar_width == 0 ? 1 : sps.sar_width;
141 *pixel_height = sps.sar_height == 0 ? 1 : sps.sar_height;
142 DVLOG(2) <<
"Found coded_width: " << *coded_width
143 <<
" coded_height: " << *coded_height
144 <<
" pixel_width: " << *pixel_width
145 <<
" pixel_height: " << *pixel_height;
151 bool H264SliceHeader::IsPSlice()
const {
152 return (slice_type % 5 == kPSlice);
155 bool H264SliceHeader::IsBSlice()
const {
156 return (slice_type % 5 == kBSlice);
159 bool H264SliceHeader::IsISlice()
const {
160 return (slice_type % 5 == kISlice);
163 bool H264SliceHeader::IsSPSlice()
const {
164 return (slice_type % 5 == kSPSlice);
167 bool H264SliceHeader::IsSISlice()
const {
168 return (slice_type % 5 == kSISlice);
171 H264NALU::H264NALU() {
172 memset(
this, 0,
sizeof(*
this));
176 memset(
this, 0,
sizeof(*
this));
180 memset(
this, 0,
sizeof(*
this));
183 H264SliceHeader::H264SliceHeader() {
184 memset(
this, 0,
sizeof(*
this));
187 H264SEIMessage::H264SEIMessage() {
188 memset(
this, 0,
sizeof(*
this));
191 #define READ_BITS_OR_RETURN(num_bits, out) \
194 if (!br_.ReadBits(num_bits, &_out)) { \
196 << "Error in stream: unexpected EOS while trying to read " #out; \
197 return kInvalidStream; \
202 #define READ_BOOL_OR_RETURN(out) \
205 if (!br_.ReadBits(1, &_out)) { \
207 << "Error in stream: unexpected EOS while trying to read " #out; \
208 return kInvalidStream; \
213 #define READ_UE_OR_RETURN(out) \
215 if (ReadUE(out) != kOk) { \
216 DVLOG(1) << "Error in stream: invalid value while trying to read " #out; \
217 return kInvalidStream; \
221 #define READ_SE_OR_RETURN(out) \
223 if (ReadSE(out) != kOk) { \
224 DVLOG(1) << "Error in stream: invalid value while trying to read " #out; \
225 return kInvalidStream; \
229 #define IN_RANGE_OR_RETURN(val, min, max) \
231 if ((val) < (min) || (val) > (max)) { \
232 DVLOG(1) << "Error in stream: invalid value, expected " #val " to be" \
233 << " in range [" << (min) << ":" << (max) << "]" \
234 << " found " << (val) << " instead"; \
235 return kInvalidStream; \
239 #define TRUE_OR_RETURN(a) \
242 DVLOG(1) << "Error in stream: invalid value, expected " << #a; \
243 return kInvalidStream; \
247 enum AspectRatioIdc {
253 static const int kTableSarWidth[] = {
254 0, 1, 12, 10, 16, 40, 24, 20, 32, 80, 18, 15, 64, 160, 4, 3, 2
256 static const int kTableSarHeight[] = {
257 0, 1, 11, 11, 11, 33, 11, 11, 11, 33, 11, 11, 33, 99, 3, 2, 1
259 COMPILE_ASSERT(arraysize(kTableSarWidth) == arraysize(kTableSarHeight),
260 sar_tables_must_have_same_size);
262 H264Parser::H264Parser() {
266 H264Parser::~H264Parser() {
267 STLDeleteValues(&active_SPSes_);
268 STLDeleteValues(&active_PPSes_);
271 void H264Parser::Reset() {
276 void H264Parser::SetStream(
const uint8_t* stream, off_t stream_size) {
278 DCHECK_GT(stream_size, 0);
281 bytes_left_ = stream_size;
284 const H264PPS* H264Parser::GetPPS(
int pps_id) {
285 return active_PPSes_[pps_id];
288 const H264SPS* H264Parser::GetSPS(
int sps_id) {
289 return active_SPSes_[sps_id];
292 static inline bool IsStartCode(
const uint8_t* data) {
293 return data[0] == 0x00 && data[1] == 0x00 && data[2] == 0x01;
297 bool H264Parser::FindStartCode(
const uint8_t* data,
300 off_t* start_code_size) {
301 DCHECK_GE(data_size, 0);
302 off_t bytes_left = data_size;
304 while (bytes_left >= 3) {
305 if (IsStartCode(data)) {
307 *offset = data_size - bytes_left;
308 *start_code_size = 3;
312 if (*offset > 0 && *(data - 1) == 0x00) {
314 ++(*start_code_size);
329 *offset = data_size - bytes_left;
330 *start_code_size = 0;
334 bool H264Parser::LocateNALU(off_t* nalu_size, off_t* start_code_size) {
336 off_t nalu_start_off = 0;
337 off_t annexb_start_code_size = 0;
338 if (!FindStartCode(stream_, bytes_left_,
339 &nalu_start_off, &annexb_start_code_size)) {
340 DVLOG(4) <<
"Could not find start code, end of stream?";
345 stream_ += nalu_start_off;
346 bytes_left_ -= nalu_start_off;
348 const uint8_t* nalu_data = stream_ + annexb_start_code_size;
349 off_t max_nalu_data_size = bytes_left_ - annexb_start_code_size;
350 if (max_nalu_data_size <= 0) {
351 DVLOG(3) <<
"End of stream";
361 off_t next_start_code_size = 0;
362 off_t nalu_size_without_start_code = 0;
363 if (!FindStartCode(nalu_data, max_nalu_data_size,
364 &nalu_size_without_start_code, &next_start_code_size)) {
365 nalu_size_without_start_code = max_nalu_data_size;
367 *nalu_size = nalu_size_without_start_code + annexb_start_code_size;
368 *start_code_size = annexb_start_code_size;
372 H264Parser::Result H264Parser::ReadUE(
int* val) {
379 READ_BITS_OR_RETURN(1, &bit);
384 return kInvalidStream;
387 *val = (1 << num_bits) - 1;
390 READ_BITS_OR_RETURN(num_bits, &rest);
397 H264Parser::Result H264Parser::ReadSE(
int* val) {
414 H264Parser::Result H264Parser::AdvanceToNextNALU(H264NALU* nalu) {
415 off_t start_code_size;
416 off_t nalu_size_with_start_code;
417 if (!LocateNALU(&nalu_size_with_start_code, &start_code_size)) {
418 DVLOG(4) <<
"Could not find next NALU, bytes left in stream: "
423 nalu->data = stream_ + start_code_size;
424 nalu->size = nalu_size_with_start_code - start_code_size;
425 DVLOG(4) <<
"NALU found: size=" << nalu_size_with_start_code;
428 if (!br_.Initialize(nalu->data, nalu->size))
435 stream_ += nalu_size_with_start_code;
436 bytes_left_ -= nalu_size_with_start_code;
440 READ_BITS_OR_RETURN(1, &data);
441 TRUE_OR_RETURN(data == 0);
443 READ_BITS_OR_RETURN(2, &nalu->nal_ref_idc);
444 READ_BITS_OR_RETURN(5, &nalu->nal_unit_type);
446 DVLOG(4) <<
"NALU type: " <<
static_cast<int>(nalu->nal_unit_type)
447 <<
" at: " << reinterpret_cast<const void*>(nalu->data)
448 <<
" size: " << nalu->size
449 <<
" ref: " <<
static_cast<int>(nalu->nal_ref_idc);
455 static const int kDefault4x4Intra[kH264ScalingList4x4Length] = {
456 6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32, 32, 37, 37, 42, };
458 static const int kDefault4x4Inter[kH264ScalingList4x4Length] = {
459 10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27, 27, 30, 30, 34, };
461 static const int kDefault8x8Intra[kH264ScalingList8x8Length] = {
462 6, 10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18, 18, 18, 18, 23,
463 23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27,
464 27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31,
465 31, 33, 33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42, };
467 static const int kDefault8x8Inter[kH264ScalingList8x8Length] = {
468 9, 13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19, 19, 19, 19, 21,
469 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24, 24,
470 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27,
471 27, 28, 28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35, };
473 static inline void DefaultScalingList4x4(
475 int scaling_list4x4[][kH264ScalingList4x4Length]) {
479 memcpy(scaling_list4x4[i], kDefault4x4Intra,
sizeof(kDefault4x4Intra));
481 memcpy(scaling_list4x4[i], kDefault4x4Inter,
sizeof(kDefault4x4Inter));
484 static inline void DefaultScalingList8x8(
486 int scaling_list8x8[][kH264ScalingList8x8Length]) {
490 memcpy(scaling_list8x8[i], kDefault8x8Intra,
sizeof(kDefault8x8Intra));
492 memcpy(scaling_list8x8[i], kDefault8x8Inter,
sizeof(kDefault8x8Inter));
495 static void FallbackScalingList4x4(
497 const int default_scaling_list_intra[],
498 const int default_scaling_list_inter[],
499 int scaling_list4x4[][kH264ScalingList4x4Length]) {
500 static const int kScalingList4x4ByteSize =
501 sizeof(scaling_list4x4[0][0]) * kH264ScalingList4x4Length;
505 memcpy(scaling_list4x4[i], default_scaling_list_intra,
506 kScalingList4x4ByteSize);
510 memcpy(scaling_list4x4[i], scaling_list4x4[0], kScalingList4x4ByteSize);
514 memcpy(scaling_list4x4[i], scaling_list4x4[1], kScalingList4x4ByteSize);
518 memcpy(scaling_list4x4[i], default_scaling_list_inter,
519 kScalingList4x4ByteSize);
523 memcpy(scaling_list4x4[i], scaling_list4x4[3], kScalingList4x4ByteSize);
527 memcpy(scaling_list4x4[i], scaling_list4x4[4], kScalingList4x4ByteSize);
536 static void FallbackScalingList8x8(
538 const int default_scaling_list_intra[],
539 const int default_scaling_list_inter[],
540 int scaling_list8x8[][kH264ScalingList8x8Length]) {
541 static const int kScalingList8x8ByteSize =
542 sizeof(scaling_list8x8[0][0]) * kH264ScalingList8x8Length;
546 memcpy(scaling_list8x8[i], default_scaling_list_intra,
547 kScalingList8x8ByteSize);
551 memcpy(scaling_list8x8[i], default_scaling_list_inter,
552 kScalingList8x8ByteSize);
556 memcpy(scaling_list8x8[i], scaling_list8x8[0], kScalingList8x8ByteSize);
560 memcpy(scaling_list8x8[i], scaling_list8x8[1], kScalingList8x8ByteSize);
564 memcpy(scaling_list8x8[i], scaling_list8x8[2], kScalingList8x8ByteSize);
568 memcpy(scaling_list8x8[i], scaling_list8x8[3], kScalingList8x8ByteSize);
577 H264Parser::Result H264Parser::ParseScalingList(
int size,
585 *use_default =
false;
587 for (
int j = 0; j < size; ++j) {
588 if (next_scale != 0) {
589 READ_SE_OR_RETURN(&delta_scale);
590 IN_RANGE_OR_RETURN(delta_scale, -128, 127);
591 next_scale = (last_scale + delta_scale + 256) & 0xff;
593 if (j == 0 && next_scale == 0) {
599 scaling_list[j] = (next_scale == 0) ? last_scale : next_scale;
600 last_scale = scaling_list[j];
606 H264Parser::Result H264Parser::ParseSPSScalingLists(H264SPS* sps) {
608 bool seq_scaling_list_present_flag;
613 for (
int i = 0; i < 6; ++i) {
614 READ_BOOL_OR_RETURN(&seq_scaling_list_present_flag);
616 if (seq_scaling_list_present_flag) {
617 res = ParseScalingList(arraysize(sps->scaling_list4x4[i]),
618 sps->scaling_list4x4[i],
624 DefaultScalingList4x4(i, sps->scaling_list4x4);
627 FallbackScalingList4x4(
628 i, kDefault4x4Intra, kDefault4x4Inter, sps->scaling_list4x4);
633 for (
int i = 0; i < ((sps->chroma_format_idc != 3) ? 2 : 6); ++i) {
634 READ_BOOL_OR_RETURN(&seq_scaling_list_present_flag);
636 if (seq_scaling_list_present_flag) {
637 res = ParseScalingList(arraysize(sps->scaling_list8x8[i]),
638 sps->scaling_list8x8[i],
644 DefaultScalingList8x8(i, sps->scaling_list8x8);
647 FallbackScalingList8x8(
648 i, kDefault8x8Intra, kDefault8x8Inter, sps->scaling_list8x8);
655 H264Parser::Result H264Parser::ParsePPSScalingLists(
const H264SPS& sps,
658 bool pic_scaling_list_present_flag;
662 for (
int i = 0; i < 6; ++i) {
663 READ_BOOL_OR_RETURN(&pic_scaling_list_present_flag);
665 if (pic_scaling_list_present_flag) {
666 res = ParseScalingList(arraysize(pps->scaling_list4x4[i]),
667 pps->scaling_list4x4[i],
673 DefaultScalingList4x4(i, pps->scaling_list4x4);
676 if (sps.seq_scaling_matrix_present_flag) {
678 FallbackScalingList4x4(
679 i, kDefault4x4Intra, kDefault4x4Inter, pps->scaling_list4x4);
682 FallbackScalingList4x4(i,
683 sps.scaling_list4x4[0],
684 sps.scaling_list4x4[3],
685 pps->scaling_list4x4);
690 if (pps->transform_8x8_mode_flag) {
691 for (
int i = 0; i < ((sps.chroma_format_idc != 3) ? 2 : 6); ++i) {
692 READ_BOOL_OR_RETURN(&pic_scaling_list_present_flag);
694 if (pic_scaling_list_present_flag) {
695 res = ParseScalingList(arraysize(pps->scaling_list8x8[i]),
696 pps->scaling_list8x8[i],
702 DefaultScalingList8x8(i, pps->scaling_list8x8);
705 if (sps.seq_scaling_matrix_present_flag) {
707 FallbackScalingList8x8(
708 i, kDefault8x8Intra, kDefault8x8Inter, pps->scaling_list8x8);
711 FallbackScalingList8x8(i,
712 sps.scaling_list8x8[0],
713 sps.scaling_list8x8[1],
714 pps->scaling_list8x8);
722 H264Parser::Result H264Parser::ParseAndIgnoreHRDParameters(
723 bool* hrd_parameters_present) {
725 READ_BOOL_OR_RETURN(&data);
729 *hrd_parameters_present =
true;
732 READ_UE_OR_RETURN(&cpb_cnt_minus1);
733 IN_RANGE_OR_RETURN(cpb_cnt_minus1, 0, 31);
734 READ_BITS_OR_RETURN(8, &data);
735 for (
int i = 0; i <= cpb_cnt_minus1; ++i) {
736 READ_UE_OR_RETURN(&data);
737 READ_UE_OR_RETURN(&data);
738 READ_BOOL_OR_RETURN(&data);
740 READ_BITS_OR_RETURN(20, &data);
745 H264Parser::Result H264Parser::ParseVUIParameters(H264SPS* sps) {
746 bool aspect_ratio_info_present_flag;
747 READ_BOOL_OR_RETURN(&aspect_ratio_info_present_flag);
748 if (aspect_ratio_info_present_flag) {
749 int aspect_ratio_idc;
750 READ_BITS_OR_RETURN(8, &aspect_ratio_idc);
751 if (aspect_ratio_idc == kExtendedSar) {
752 READ_BITS_OR_RETURN(16, &sps->sar_width);
753 READ_BITS_OR_RETURN(16, &sps->sar_height);
755 const int max_aspect_ratio_idc = arraysize(kTableSarWidth) - 1;
756 IN_RANGE_OR_RETURN(aspect_ratio_idc, 0, max_aspect_ratio_idc);
757 sps->sar_width = kTableSarWidth[aspect_ratio_idc];
758 sps->sar_height = kTableSarHeight[aspect_ratio_idc];
764 READ_BOOL_OR_RETURN(&data);
766 READ_BOOL_OR_RETURN(&data);
768 READ_BOOL_OR_RETURN(&data);
770 READ_BITS_OR_RETURN(3, &data);
771 READ_BOOL_OR_RETURN(&data);
772 READ_BOOL_OR_RETURN(&data);
774 READ_BITS_OR_RETURN(24, &data);
777 READ_BOOL_OR_RETURN(&data);
779 READ_UE_OR_RETURN(&data);
780 READ_UE_OR_RETURN(&data);
784 READ_BOOL_OR_RETURN(&data);
786 READ_BITS_OR_RETURN(16, &data);
787 READ_BITS_OR_RETURN(16, &data);
788 READ_BITS_OR_RETURN(16, &data);
789 READ_BITS_OR_RETURN(16, &data);
790 READ_BOOL_OR_RETURN(&data);
794 bool hrd_parameters_present =
false;
795 Result res = ParseAndIgnoreHRDParameters(&hrd_parameters_present);
800 res = ParseAndIgnoreHRDParameters(&hrd_parameters_present);
804 if (hrd_parameters_present)
805 READ_BOOL_OR_RETURN(&data);
807 READ_BOOL_OR_RETURN(&data);
808 READ_BOOL_OR_RETURN(&sps->bitstream_restriction_flag);
809 if (sps->bitstream_restriction_flag) {
810 READ_BOOL_OR_RETURN(&data);
811 READ_UE_OR_RETURN(&data);
812 READ_UE_OR_RETURN(&data);
813 READ_UE_OR_RETURN(&data);
814 READ_UE_OR_RETURN(&data);
815 READ_UE_OR_RETURN(&sps->max_num_reorder_frames);
816 READ_UE_OR_RETURN(&sps->max_dec_frame_buffering);
817 TRUE_OR_RETURN(sps->max_dec_frame_buffering >= sps->max_num_ref_frames);
819 sps->max_num_reorder_frames, 0, sps->max_dec_frame_buffering);
825 static void FillDefaultSeqScalingLists(H264SPS* sps) {
826 for (
int i = 0; i < 6; ++i)
827 for (
int j = 0; j < kH264ScalingList4x4Length; ++j)
828 sps->scaling_list4x4[i][j] = 16;
830 for (
int i = 0; i < 6; ++i)
831 for (
int j = 0; j < kH264ScalingList8x8Length; ++j)
832 sps->scaling_list8x8[i][j] = 16;
835 H264Parser::Result H264Parser::ParseSPS(
int* sps_id) {
842 scoped_ptr<H264SPS> sps(
new H264SPS());
844 READ_BITS_OR_RETURN(8, &sps->profile_idc);
845 READ_BOOL_OR_RETURN(&sps->constraint_set0_flag);
846 READ_BOOL_OR_RETURN(&sps->constraint_set1_flag);
847 READ_BOOL_OR_RETURN(&sps->constraint_set2_flag);
848 READ_BOOL_OR_RETURN(&sps->constraint_set3_flag);
849 READ_BOOL_OR_RETURN(&sps->constraint_set4_flag);
850 READ_BOOL_OR_RETURN(&sps->constraint_set5_flag);
851 READ_BITS_OR_RETURN(2, &data);
852 READ_BITS_OR_RETURN(8, &sps->level_idc);
853 READ_UE_OR_RETURN(&sps->seq_parameter_set_id);
854 TRUE_OR_RETURN(sps->seq_parameter_set_id < 32);
856 if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
857 sps->profile_idc == 122 || sps->profile_idc == 244 ||
858 sps->profile_idc == 44 || sps->profile_idc == 83 ||
859 sps->profile_idc == 86 || sps->profile_idc == 118 ||
860 sps->profile_idc == 128) {
861 READ_UE_OR_RETURN(&sps->chroma_format_idc);
862 TRUE_OR_RETURN(sps->chroma_format_idc < 4);
864 if (sps->chroma_format_idc == 3)
865 READ_BOOL_OR_RETURN(&sps->separate_colour_plane_flag);
867 READ_UE_OR_RETURN(&sps->bit_depth_luma_minus8);
868 TRUE_OR_RETURN(sps->bit_depth_luma_minus8 < 7);
870 READ_UE_OR_RETURN(&sps->bit_depth_chroma_minus8);
871 TRUE_OR_RETURN(sps->bit_depth_chroma_minus8 < 7);
873 READ_BOOL_OR_RETURN(&sps->qpprime_y_zero_transform_bypass_flag);
874 READ_BOOL_OR_RETURN(&sps->seq_scaling_matrix_present_flag);
876 if (sps->seq_scaling_matrix_present_flag) {
877 DVLOG(4) <<
"Scaling matrix present";
878 res = ParseSPSScalingLists(sps.get());
882 FillDefaultSeqScalingLists(sps.get());
885 sps->chroma_format_idc = 1;
886 FillDefaultSeqScalingLists(sps.get());
889 if (sps->separate_colour_plane_flag)
890 sps->chroma_array_type = 0;
892 sps->chroma_array_type = sps->chroma_format_idc;
894 READ_UE_OR_RETURN(&sps->log2_max_frame_num_minus4);
895 TRUE_OR_RETURN(sps->log2_max_frame_num_minus4 < 13);
897 READ_UE_OR_RETURN(&sps->pic_order_cnt_type);
898 TRUE_OR_RETURN(sps->pic_order_cnt_type < 3);
900 sps->expected_delta_per_pic_order_cnt_cycle = 0;
901 if (sps->pic_order_cnt_type == 0) {
902 READ_UE_OR_RETURN(&sps->log2_max_pic_order_cnt_lsb_minus4);
903 TRUE_OR_RETURN(sps->log2_max_pic_order_cnt_lsb_minus4 < 13);
904 }
else if (sps->pic_order_cnt_type == 1) {
905 READ_BOOL_OR_RETURN(&sps->delta_pic_order_always_zero_flag);
906 READ_SE_OR_RETURN(&sps->offset_for_non_ref_pic);
907 READ_SE_OR_RETURN(&sps->offset_for_top_to_bottom_field);
908 READ_UE_OR_RETURN(&sps->num_ref_frames_in_pic_order_cnt_cycle);
909 TRUE_OR_RETURN(sps->num_ref_frames_in_pic_order_cnt_cycle < 255);
911 for (
int i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; ++i) {
912 READ_SE_OR_RETURN(&sps->offset_for_ref_frame[i]);
913 sps->expected_delta_per_pic_order_cnt_cycle +=
914 sps->offset_for_ref_frame[i];
918 READ_UE_OR_RETURN(&sps->max_num_ref_frames);
919 READ_BOOL_OR_RETURN(&sps->gaps_in_frame_num_value_allowed_flag);
921 if (sps->gaps_in_frame_num_value_allowed_flag)
922 return kUnsupportedStream;
924 READ_UE_OR_RETURN(&sps->pic_width_in_mbs_minus1);
925 READ_UE_OR_RETURN(&sps->pic_height_in_map_units_minus1);
927 READ_BOOL_OR_RETURN(&sps->frame_mbs_only_flag);
928 if (!sps->frame_mbs_only_flag)
929 READ_BOOL_OR_RETURN(&sps->mb_adaptive_frame_field_flag);
931 READ_BOOL_OR_RETURN(&sps->direct_8x8_inference_flag);
933 READ_BOOL_OR_RETURN(&sps->frame_cropping_flag);
934 if (sps->frame_cropping_flag) {
935 READ_UE_OR_RETURN(&sps->frame_crop_left_offset);
936 READ_UE_OR_RETURN(&sps->frame_crop_right_offset);
937 READ_UE_OR_RETURN(&sps->frame_crop_top_offset);
938 READ_UE_OR_RETURN(&sps->frame_crop_bottom_offset);
941 READ_BOOL_OR_RETURN(&sps->vui_parameters_present_flag);
942 if (sps->vui_parameters_present_flag) {
943 DVLOG(4) <<
"VUI parameters present";
944 res = ParseVUIParameters(sps.get());
950 *sps_id = sps->seq_parameter_set_id;
951 delete active_SPSes_[*sps_id];
952 active_SPSes_[*sps_id] = sps.release();
957 H264Parser::Result H264Parser::ParsePPS(
int* pps_id) {
964 scoped_ptr<H264PPS> pps(
new H264PPS());
966 READ_UE_OR_RETURN(&pps->pic_parameter_set_id);
967 READ_UE_OR_RETURN(&pps->seq_parameter_set_id);
968 TRUE_OR_RETURN(pps->seq_parameter_set_id < 32);
970 sps = GetSPS(pps->seq_parameter_set_id);
973 READ_BOOL_OR_RETURN(&pps->entropy_coding_mode_flag);
974 READ_BOOL_OR_RETURN(&pps->bottom_field_pic_order_in_frame_present_flag);
976 READ_UE_OR_RETURN(&pps->num_slice_groups_minus1);
977 if (pps->num_slice_groups_minus1 > 1) {
978 DVLOG(1) <<
"Slice groups not supported";
979 return kUnsupportedStream;
982 READ_UE_OR_RETURN(&pps->num_ref_idx_l0_default_active_minus1);
983 TRUE_OR_RETURN(pps->num_ref_idx_l0_default_active_minus1 < 32);
985 READ_UE_OR_RETURN(&pps->num_ref_idx_l1_default_active_minus1);
986 TRUE_OR_RETURN(pps->num_ref_idx_l1_default_active_minus1 < 32);
988 READ_BOOL_OR_RETURN(&pps->weighted_pred_flag);
989 READ_BITS_OR_RETURN(2, &pps->weighted_bipred_idc);
990 TRUE_OR_RETURN(pps->weighted_bipred_idc < 3);
992 READ_SE_OR_RETURN(&pps->pic_init_qp_minus26);
993 IN_RANGE_OR_RETURN(pps->pic_init_qp_minus26, -26, 25);
995 READ_SE_OR_RETURN(&pps->pic_init_qs_minus26);
996 IN_RANGE_OR_RETURN(pps->pic_init_qs_minus26, -26, 25);
998 READ_SE_OR_RETURN(&pps->chroma_qp_index_offset);
999 IN_RANGE_OR_RETURN(pps->chroma_qp_index_offset, -12, 12);
1000 pps->second_chroma_qp_index_offset = pps->chroma_qp_index_offset;
1002 READ_BOOL_OR_RETURN(&pps->deblocking_filter_control_present_flag);
1003 READ_BOOL_OR_RETURN(&pps->constrained_intra_pred_flag);
1004 READ_BOOL_OR_RETURN(&pps->redundant_pic_cnt_present_flag);
1006 if (br_.HasMoreRBSPData()) {
1007 READ_BOOL_OR_RETURN(&pps->transform_8x8_mode_flag);
1008 READ_BOOL_OR_RETURN(&pps->pic_scaling_matrix_present_flag);
1010 if (pps->pic_scaling_matrix_present_flag) {
1011 DVLOG(4) <<
"Picture scaling matrix present";
1012 res = ParsePPSScalingLists(*sps, pps.get());
1017 READ_SE_OR_RETURN(&pps->second_chroma_qp_index_offset);
1021 *pps_id = pps->pic_parameter_set_id;
1022 delete active_PPSes_[*pps_id];
1023 active_PPSes_[*pps_id] = pps.release();
1028 H264Parser::Result H264Parser::ParseSPSFromArray(
1029 const uint8_t* sps_data,
1030 size_t sps_data_length,
1032 br_.Initialize(sps_data, sps_data_length);
1035 READ_BITS_OR_RETURN(1, &data);
1037 TRUE_OR_RETURN(data == 0);
1039 READ_BITS_OR_RETURN(2, &nal_ref_idc);
1042 TRUE_OR_RETURN(nal_ref_idc != 0);
1044 READ_BITS_OR_RETURN(5, &nal_unit_type);
1045 TRUE_OR_RETURN(nal_unit_type == H264NALU::kSPS);
1047 return ParseSPS(sps_id);
1050 H264Parser::Result H264Parser::ParseRefPicListModification(
1051 int num_ref_idx_active_minus1,
1052 H264ModificationOfPicNum* ref_list_mods) {
1053 H264ModificationOfPicNum* pic_num_mod;
1055 if (num_ref_idx_active_minus1 >= 32)
1056 return kInvalidStream;
1058 for (
int i = 0; i < 32; ++i) {
1059 pic_num_mod = &ref_list_mods[i];
1060 READ_UE_OR_RETURN(&pic_num_mod->modification_of_pic_nums_idc);
1061 TRUE_OR_RETURN(pic_num_mod->modification_of_pic_nums_idc < 4);
1063 switch (pic_num_mod->modification_of_pic_nums_idc) {
1066 READ_UE_OR_RETURN(&pic_num_mod->abs_diff_pic_num_minus1);
1070 READ_UE_OR_RETURN(&pic_num_mod->long_term_pic_num);
1076 return kInvalidStream;
1080 return kInvalidStream;
1086 int modification_of_pic_nums_idc;
1087 READ_UE_OR_RETURN(&modification_of_pic_nums_idc);
1088 TRUE_OR_RETURN(modification_of_pic_nums_idc == 3);
1093 H264Parser::Result H264Parser::ParseRefPicListModifications(
1094 H264SliceHeader* shdr) {
1097 if (!shdr->IsISlice() && !shdr->IsSISlice()) {
1098 READ_BOOL_OR_RETURN(&shdr->ref_pic_list_modification_flag_l0);
1099 if (shdr->ref_pic_list_modification_flag_l0) {
1100 res = ParseRefPicListModification(shdr->num_ref_idx_l0_active_minus1,
1101 shdr->ref_list_l0_modifications);
1107 if (shdr->IsBSlice()) {
1108 READ_BOOL_OR_RETURN(&shdr->ref_pic_list_modification_flag_l1);
1109 if (shdr->ref_pic_list_modification_flag_l1) {
1110 res = ParseRefPicListModification(shdr->num_ref_idx_l1_active_minus1,
1111 shdr->ref_list_l1_modifications);
1120 H264Parser::Result H264Parser::ParseWeightingFactors(
1121 int num_ref_idx_active_minus1,
1122 int chroma_array_type,
1123 int luma_log2_weight_denom,
1124 int chroma_log2_weight_denom,
1125 H264WeightingFactors* w_facts) {
1127 int def_luma_weight = 1 << luma_log2_weight_denom;
1128 int def_chroma_weight = 1 << chroma_log2_weight_denom;
1130 for (
int i = 0; i < num_ref_idx_active_minus1 + 1; ++i) {
1131 READ_BOOL_OR_RETURN(&w_facts->luma_weight_flag);
1132 if (w_facts->luma_weight_flag) {
1133 READ_SE_OR_RETURN(&w_facts->luma_weight[i]);
1134 IN_RANGE_OR_RETURN(w_facts->luma_weight[i], -128, 127);
1136 READ_SE_OR_RETURN(&w_facts->luma_offset[i]);
1137 IN_RANGE_OR_RETURN(w_facts->luma_offset[i], -128, 127);
1139 w_facts->luma_weight[i] = def_luma_weight;
1140 w_facts->luma_offset[i] = 0;
1143 if (chroma_array_type != 0) {
1144 READ_BOOL_OR_RETURN(&w_facts->chroma_weight_flag);
1145 if (w_facts->chroma_weight_flag) {
1146 for (
int j = 0; j < 2; ++j) {
1147 READ_SE_OR_RETURN(&w_facts->chroma_weight[i][j]);
1148 IN_RANGE_OR_RETURN(w_facts->chroma_weight[i][j], -128, 127);
1150 READ_SE_OR_RETURN(&w_facts->chroma_offset[i][j]);
1151 IN_RANGE_OR_RETURN(w_facts->chroma_offset[i][j], -128, 127);
1154 for (
int j = 0; j < 2; ++j) {
1155 w_facts->chroma_weight[i][j] = def_chroma_weight;
1156 w_facts->chroma_offset[i][j] = 0;
1165 H264Parser::Result H264Parser::ParsePredWeightTable(
const H264SPS& sps,
1166 H264SliceHeader* shdr) {
1167 READ_UE_OR_RETURN(&shdr->luma_log2_weight_denom);
1168 TRUE_OR_RETURN(shdr->luma_log2_weight_denom < 8);
1170 if (sps.chroma_array_type != 0)
1171 READ_UE_OR_RETURN(&shdr->chroma_log2_weight_denom);
1172 TRUE_OR_RETURN(shdr->chroma_log2_weight_denom < 8);
1174 Result res = ParseWeightingFactors(shdr->num_ref_idx_l0_active_minus1,
1175 sps.chroma_array_type,
1176 shdr->luma_log2_weight_denom,
1177 shdr->chroma_log2_weight_denom,
1178 &shdr->pred_weight_table_l0);
1182 if (shdr->IsBSlice()) {
1183 res = ParseWeightingFactors(shdr->num_ref_idx_l1_active_minus1,
1184 sps.chroma_array_type,
1185 shdr->luma_log2_weight_denom,
1186 shdr->chroma_log2_weight_denom,
1187 &shdr->pred_weight_table_l1);
1195 H264Parser::Result H264Parser::ParseDecRefPicMarking(H264SliceHeader* shdr) {
1196 if (shdr->idr_pic_flag) {
1197 READ_BOOL_OR_RETURN(&shdr->no_output_of_prior_pics_flag);
1198 READ_BOOL_OR_RETURN(&shdr->long_term_reference_flag);
1200 READ_BOOL_OR_RETURN(&shdr->adaptive_ref_pic_marking_mode_flag);
1202 H264DecRefPicMarking* marking;
1203 if (shdr->adaptive_ref_pic_marking_mode_flag) {
1205 for (i = 0; i < arraysize(shdr->ref_pic_marking); ++i) {
1206 marking = &shdr->ref_pic_marking[i];
1208 READ_UE_OR_RETURN(&marking->memory_mgmnt_control_operation);
1209 if (marking->memory_mgmnt_control_operation == 0)
1212 if (marking->memory_mgmnt_control_operation == 1 ||
1213 marking->memory_mgmnt_control_operation == 3)
1214 READ_UE_OR_RETURN(&marking->difference_of_pic_nums_minus1);
1216 if (marking->memory_mgmnt_control_operation == 2)
1217 READ_UE_OR_RETURN(&marking->long_term_pic_num);
1219 if (marking->memory_mgmnt_control_operation == 3 ||
1220 marking->memory_mgmnt_control_operation == 6)
1221 READ_UE_OR_RETURN(&marking->long_term_frame_idx);
1223 if (marking->memory_mgmnt_control_operation == 4)
1224 READ_UE_OR_RETURN(&marking->max_long_term_frame_idx_plus1);
1226 if (marking->memory_mgmnt_control_operation > 6)
1227 return kInvalidStream;
1230 if (i == arraysize(shdr->ref_pic_marking)) {
1231 DVLOG(1) <<
"Ran out of dec ref pic marking fields";
1232 return kUnsupportedStream;
1240 H264Parser::Result H264Parser::ParseSliceHeader(
const H264NALU& nalu,
1241 H264SliceHeader* shdr) {
1247 memset(shdr, 0,
sizeof(*shdr));
1249 shdr->idr_pic_flag = (nalu.nal_unit_type == 5);
1250 shdr->nal_ref_idc = nalu.nal_ref_idc;
1251 shdr->nalu_data = nalu.data;
1252 shdr->nalu_size = nalu.size;
1254 READ_UE_OR_RETURN(&shdr->first_mb_in_slice);
1255 READ_UE_OR_RETURN(&shdr->slice_type);
1256 TRUE_OR_RETURN(shdr->slice_type < 10);
1258 READ_UE_OR_RETURN(&shdr->pic_parameter_set_id);
1260 pps = GetPPS(shdr->pic_parameter_set_id);
1261 TRUE_OR_RETURN(pps);
1263 sps = GetSPS(pps->seq_parameter_set_id);
1264 TRUE_OR_RETURN(sps);
1266 if (sps->separate_colour_plane_flag) {
1267 DVLOG(1) <<
"Interlaced streams not supported";
1268 return kUnsupportedStream;
1271 READ_BITS_OR_RETURN(sps->log2_max_frame_num_minus4 + 4, &shdr->frame_num);
1272 if (!sps->frame_mbs_only_flag) {
1273 READ_BOOL_OR_RETURN(&shdr->field_pic_flag);
1274 if (shdr->field_pic_flag) {
1275 DVLOG(1) <<
"Interlaced streams not supported";
1276 return kUnsupportedStream;
1280 if (shdr->idr_pic_flag)
1281 READ_UE_OR_RETURN(&shdr->idr_pic_id);
1283 if (sps->pic_order_cnt_type == 0) {
1284 READ_BITS_OR_RETURN(sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
1285 &shdr->pic_order_cnt_lsb);
1286 if (pps->bottom_field_pic_order_in_frame_present_flag &&
1287 !shdr->field_pic_flag)
1288 READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt_bottom);
1291 if (sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag) {
1292 READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt[0]);
1293 if (pps->bottom_field_pic_order_in_frame_present_flag &&
1294 !shdr->field_pic_flag)
1295 READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt[1]);
1298 if (pps->redundant_pic_cnt_present_flag) {
1299 READ_UE_OR_RETURN(&shdr->redundant_pic_cnt);
1300 TRUE_OR_RETURN(shdr->redundant_pic_cnt < 128);
1303 if (shdr->IsBSlice())
1304 READ_BOOL_OR_RETURN(&shdr->direct_spatial_mv_pred_flag);
1306 if (shdr->IsPSlice() || shdr->IsSPSlice() || shdr->IsBSlice()) {
1307 READ_BOOL_OR_RETURN(&shdr->num_ref_idx_active_override_flag);
1308 if (shdr->num_ref_idx_active_override_flag) {
1309 READ_UE_OR_RETURN(&shdr->num_ref_idx_l0_active_minus1);
1310 if (shdr->IsBSlice())
1311 READ_UE_OR_RETURN(&shdr->num_ref_idx_l1_active_minus1);
1313 shdr->num_ref_idx_l0_active_minus1 =
1314 pps->num_ref_idx_l0_default_active_minus1;
1315 if (shdr->IsBSlice()) {
1316 shdr->num_ref_idx_l1_active_minus1 =
1317 pps->num_ref_idx_l1_default_active_minus1;
1321 if (shdr->field_pic_flag) {
1322 TRUE_OR_RETURN(shdr->num_ref_idx_l0_active_minus1 < 32);
1323 TRUE_OR_RETURN(shdr->num_ref_idx_l1_active_minus1 < 32);
1325 TRUE_OR_RETURN(shdr->num_ref_idx_l0_active_minus1 < 16);
1326 TRUE_OR_RETURN(shdr->num_ref_idx_l1_active_minus1 < 16);
1329 if (nalu.nal_unit_type == H264NALU::kCodedSliceExtension) {
1330 return kUnsupportedStream;
1332 res = ParseRefPicListModifications(shdr);
1337 if ((pps->weighted_pred_flag && (shdr->IsPSlice() || shdr->IsSPSlice())) ||
1338 (pps->weighted_bipred_idc == 1 && shdr->IsBSlice())) {
1339 res = ParsePredWeightTable(*sps, shdr);
1344 if (nalu.nal_ref_idc != 0) {
1345 res = ParseDecRefPicMarking(shdr);
1350 if (pps->entropy_coding_mode_flag && !shdr->IsISlice() &&
1351 !shdr->IsSISlice()) {
1352 READ_UE_OR_RETURN(&shdr->cabac_init_idc);
1353 TRUE_OR_RETURN(shdr->cabac_init_idc < 3);
1356 READ_SE_OR_RETURN(&shdr->slice_qp_delta);
1358 if (shdr->IsSPSlice() || shdr->IsSISlice()) {
1359 if (shdr->IsSPSlice())
1360 READ_BOOL_OR_RETURN(&shdr->sp_for_switch_flag);
1361 READ_SE_OR_RETURN(&shdr->slice_qs_delta);
1364 if (pps->deblocking_filter_control_present_flag) {
1365 READ_UE_OR_RETURN(&shdr->disable_deblocking_filter_idc);
1366 TRUE_OR_RETURN(shdr->disable_deblocking_filter_idc < 3);
1368 if (shdr->disable_deblocking_filter_idc != 1) {
1369 READ_SE_OR_RETURN(&shdr->slice_alpha_c0_offset_div2);
1370 IN_RANGE_OR_RETURN(shdr->slice_alpha_c0_offset_div2, -6, 6);
1372 READ_SE_OR_RETURN(&shdr->slice_beta_offset_div2);
1373 IN_RANGE_OR_RETURN(shdr->slice_beta_offset_div2, -6, 6);
1377 if (pps->num_slice_groups_minus1 > 0) {
1378 DVLOG(1) <<
"Slice groups not supported";
1379 return kUnsupportedStream;
1382 size_t epb = br_.NumEmulationPreventionBytesRead();
1383 shdr->header_bit_size = (shdr->nalu_size - epb) * 8 - br_.NumBitsLeft();
1388 H264Parser::Result H264Parser::ParseSEI(H264SEIMessage* sei_msg) {
1391 memset(sei_msg, 0,
sizeof(*sei_msg));
1393 READ_BITS_OR_RETURN(8, &byte);
1394 while (byte == 0xff) {
1395 sei_msg->type += 255;
1396 READ_BITS_OR_RETURN(8, &byte);
1398 sei_msg->type += byte;
1400 READ_BITS_OR_RETURN(8, &byte);
1401 while (byte == 0xff) {
1402 sei_msg->payload_size += 255;
1403 READ_BITS_OR_RETURN(8, &byte);
1405 sei_msg->payload_size += byte;
1407 DVLOG(4) <<
"Found SEI message type: " << sei_msg->type
1408 <<
" payload size: " << sei_msg->payload_size;
1410 switch (sei_msg->type) {
1411 case H264SEIMessage::kSEIRecoveryPoint:
1412 READ_UE_OR_RETURN(&sei_msg->recovery_point.recovery_frame_cnt);
1413 READ_BOOL_OR_RETURN(&sei_msg->recovery_point.exact_match_flag);
1414 READ_BOOL_OR_RETURN(&sei_msg->recovery_point.broken_link_flag);
1415 READ_BITS_OR_RETURN(2, &sei_msg->recovery_point.changing_slice_group_idc);
1419 DVLOG(4) <<
"Unsupported SEI message";