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.SkipBytes(4));
39 RCHECK(reader.Read1(&value));
41 const uint8_t num_sps = value & 0x1F;
43 LOG(ERROR) <<
"No SPS found.";
46 uint16_t sps_length = 0;
47 RCHECK(reader.Read2(&sps_length));
49 return ExtractResolutionFromSpsData(reader.data() + reader.pos(), sps_length,
50 coded_width, coded_height, pixel_width,
57 bool ExtractResolutionFromSpsData(
const uint8_t* sps_data,
59 uint32_t* coded_width,
60 uint32_t* coded_height,
61 uint32_t* pixel_width,
62 uint32_t* pixel_height) {
65 RCHECK(parser.ParseSPSFromArray(sps_data, sps_data_size, &sps_id) ==
67 return ExtractResolutionFromSps(*parser.GetSPS(sps_id), coded_width,
68 coded_height, pixel_width, pixel_height);
73 bool ExtractResolutionFromSps(
const H264SPS& sps,
74 uint32_t* coded_width,
75 uint32_t* coded_height,
76 uint32_t* pixel_width,
77 uint32_t* pixel_height) {
80 if (sps.frame_cropping_flag) {
84 switch (sps.chroma_format_idc) {
104 LOG(ERROR) <<
"Unexpected chroma_format_idc " << sps.chroma_format_idc;
109 int crop_unit_x = sub_width_c;
110 int crop_unit_y = sub_height_c * (2 - (sps.frame_mbs_only_flag ? 1 : 0));
111 crop_x = crop_unit_x *
112 (sps.frame_crop_left_offset + sps.frame_crop_right_offset);
113 crop_y = crop_unit_y *
114 (sps.frame_crop_top_offset + sps.frame_crop_bottom_offset);
118 int pic_width_in_mbs = sps.pic_width_in_mbs_minus1 + 1;
119 *coded_width = pic_width_in_mbs * 16 - crop_x;
122 int pic_height_in_mbs = (2 - (sps.frame_mbs_only_flag ? 1 : 0)) *
123 (sps.pic_height_in_map_units_minus1 + 1);
124 *coded_height = pic_height_in_mbs * 16 - crop_y;
127 *pixel_width = sps.sar_width == 0 ? 1 : sps.sar_width;
128 *pixel_height = sps.sar_height == 0 ? 1 : sps.sar_height;
129 DVLOG(2) <<
"Found coded_width: " << *coded_width
130 <<
" coded_height: " << *coded_height
131 <<
" pixel_width: " << *pixel_width
132 <<
" pixel_height: " << *pixel_height;
138 bool H264SliceHeader::IsPSlice()
const {
139 return (slice_type % 5 == kPSlice);
142 bool H264SliceHeader::IsBSlice()
const {
143 return (slice_type % 5 == kBSlice);
146 bool H264SliceHeader::IsISlice()
const {
147 return (slice_type % 5 == kISlice);
150 bool H264SliceHeader::IsSPSlice()
const {
151 return (slice_type % 5 == kSPSlice);
154 bool H264SliceHeader::IsSISlice()
const {
155 return (slice_type % 5 == kSISlice);
158 H264NALU::H264NALU() {
159 memset(
this, 0,
sizeof(*
this));
163 memset(
this, 0,
sizeof(*
this));
167 memset(
this, 0,
sizeof(*
this));
170 H264SliceHeader::H264SliceHeader() {
171 memset(
this, 0,
sizeof(*
this));
174 H264SEIMessage::H264SEIMessage() {
175 memset(
this, 0,
sizeof(*
this));
178 #define READ_BITS_OR_RETURN(num_bits, out) \
181 if (!br_.ReadBits(num_bits, &_out)) { \
183 << "Error in stream: unexpected EOS while trying to read " #out; \
184 return kInvalidStream; \
189 #define READ_BOOL_OR_RETURN(out) \
192 if (!br_.ReadBits(1, &_out)) { \
194 << "Error in stream: unexpected EOS while trying to read " #out; \
195 return kInvalidStream; \
200 #define READ_UE_OR_RETURN(out) \
202 if (ReadUE(out) != kOk) { \
203 DVLOG(1) << "Error in stream: invalid value while trying to read " #out; \
204 return kInvalidStream; \
208 #define READ_SE_OR_RETURN(out) \
210 if (ReadSE(out) != kOk) { \
211 DVLOG(1) << "Error in stream: invalid value while trying to read " #out; \
212 return kInvalidStream; \
216 #define IN_RANGE_OR_RETURN(val, min, max) \
218 if ((val) < (min) || (val) > (max)) { \
219 DVLOG(1) << "Error in stream: invalid value, expected " #val " to be" \
220 << " in range [" << (min) << ":" << (max) << "]" \
221 << " found " << (val) << " instead"; \
222 return kInvalidStream; \
226 #define TRUE_OR_RETURN(a) \
229 DVLOG(1) << "Error in stream: invalid value, expected " << #a; \
230 return kInvalidStream; \
234 enum AspectRatioIdc {
240 static const int kTableSarWidth[] = {
241 0, 1, 12, 10, 16, 40, 24, 20, 32, 80, 18, 15, 64, 160, 4, 3, 2
243 static const int kTableSarHeight[] = {
244 0, 1, 11, 11, 11, 33, 11, 11, 11, 33, 11, 11, 33, 99, 3, 2, 1
246 COMPILE_ASSERT(arraysize(kTableSarWidth) == arraysize(kTableSarHeight),
247 sar_tables_must_have_same_size);
249 H264Parser::H264Parser() {
253 H264Parser::~H264Parser() {
254 STLDeleteValues(&active_SPSes_);
255 STLDeleteValues(&active_PPSes_);
258 void H264Parser::Reset() {
263 void H264Parser::SetStream(
const uint8_t* stream, off_t stream_size) {
265 DCHECK_GT(stream_size, 0);
268 bytes_left_ = stream_size;
271 const H264PPS* H264Parser::GetPPS(
int pps_id) {
272 return active_PPSes_[pps_id];
275 const H264SPS* H264Parser::GetSPS(
int sps_id) {
276 return active_SPSes_[sps_id];
279 static inline bool IsStartCode(
const uint8_t* data) {
280 return data[0] == 0x00 && data[1] == 0x00 && data[2] == 0x01;
284 bool H264Parser::FindStartCode(
const uint8_t* data,
287 off_t* start_code_size) {
288 DCHECK_GE(data_size, 0);
289 off_t bytes_left = data_size;
291 while (bytes_left >= 3) {
292 if (IsStartCode(data)) {
294 *offset = data_size - bytes_left;
295 *start_code_size = 3;
299 if (*offset > 0 && *(data - 1) == 0x00) {
301 ++(*start_code_size);
316 *offset = data_size - bytes_left;
317 *start_code_size = 0;
321 bool H264Parser::LocateNALU(off_t* nalu_size, off_t* start_code_size) {
323 off_t nalu_start_off = 0;
324 off_t annexb_start_code_size = 0;
325 if (!FindStartCode(stream_, bytes_left_,
326 &nalu_start_off, &annexb_start_code_size)) {
327 DVLOG(4) <<
"Could not find start code, end of stream?";
332 stream_ += nalu_start_off;
333 bytes_left_ -= nalu_start_off;
335 const uint8_t* nalu_data = stream_ + annexb_start_code_size;
336 off_t max_nalu_data_size = bytes_left_ - annexb_start_code_size;
337 if (max_nalu_data_size <= 0) {
338 DVLOG(3) <<
"End of stream";
348 off_t next_start_code_size = 0;
349 off_t nalu_size_without_start_code = 0;
350 if (!FindStartCode(nalu_data, max_nalu_data_size,
351 &nalu_size_without_start_code, &next_start_code_size)) {
352 nalu_size_without_start_code = max_nalu_data_size;
354 *nalu_size = nalu_size_without_start_code + annexb_start_code_size;
355 *start_code_size = annexb_start_code_size;
359 H264Parser::Result H264Parser::ReadUE(
int* val) {
366 READ_BITS_OR_RETURN(1, &bit);
371 return kInvalidStream;
374 *val = (1 << num_bits) - 1;
377 READ_BITS_OR_RETURN(num_bits, &rest);
384 H264Parser::Result H264Parser::ReadSE(
int* val) {
401 H264Parser::Result H264Parser::AdvanceToNextNALU(H264NALU* nalu) {
402 off_t start_code_size;
403 off_t nalu_size_with_start_code;
404 if (!LocateNALU(&nalu_size_with_start_code, &start_code_size)) {
405 DVLOG(4) <<
"Could not find next NALU, bytes left in stream: "
410 nalu->data = stream_ + start_code_size;
411 nalu->size = nalu_size_with_start_code - start_code_size;
412 DVLOG(4) <<
"NALU found: size=" << nalu_size_with_start_code;
415 if (!br_.Initialize(nalu->data, nalu->size))
422 stream_ += nalu_size_with_start_code;
423 bytes_left_ -= nalu_size_with_start_code;
427 READ_BITS_OR_RETURN(1, &data);
428 TRUE_OR_RETURN(data == 0);
430 READ_BITS_OR_RETURN(2, &nalu->nal_ref_idc);
431 READ_BITS_OR_RETURN(5, &nalu->nal_unit_type);
433 DVLOG(4) <<
"NALU type: " <<
static_cast<int>(nalu->nal_unit_type)
434 <<
" at: " << reinterpret_cast<const void*>(nalu->data)
435 <<
" size: " << nalu->size
436 <<
" ref: " <<
static_cast<int>(nalu->nal_ref_idc);
442 static const int kDefault4x4Intra[kH264ScalingList4x4Length] = {
443 6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32, 32, 37, 37, 42, };
445 static const int kDefault4x4Inter[kH264ScalingList4x4Length] = {
446 10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27, 27, 30, 30, 34, };
448 static const int kDefault8x8Intra[kH264ScalingList8x8Length] = {
449 6, 10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18, 18, 18, 18, 23,
450 23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27,
451 27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31,
452 31, 33, 33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42, };
454 static const int kDefault8x8Inter[kH264ScalingList8x8Length] = {
455 9, 13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19, 19, 19, 19, 21,
456 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24, 24,
457 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27,
458 27, 28, 28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35, };
460 static inline void DefaultScalingList4x4(
462 int scaling_list4x4[][kH264ScalingList4x4Length]) {
466 memcpy(scaling_list4x4[i], kDefault4x4Intra,
sizeof(kDefault4x4Intra));
468 memcpy(scaling_list4x4[i], kDefault4x4Inter,
sizeof(kDefault4x4Inter));
471 static inline void DefaultScalingList8x8(
473 int scaling_list8x8[][kH264ScalingList8x8Length]) {
477 memcpy(scaling_list8x8[i], kDefault8x8Intra,
sizeof(kDefault8x8Intra));
479 memcpy(scaling_list8x8[i], kDefault8x8Inter,
sizeof(kDefault8x8Inter));
482 static void FallbackScalingList4x4(
484 const int default_scaling_list_intra[],
485 const int default_scaling_list_inter[],
486 int scaling_list4x4[][kH264ScalingList4x4Length]) {
487 static const int kScalingList4x4ByteSize =
488 sizeof(scaling_list4x4[0][0]) * kH264ScalingList4x4Length;
492 memcpy(scaling_list4x4[i], default_scaling_list_intra,
493 kScalingList4x4ByteSize);
497 memcpy(scaling_list4x4[i], scaling_list4x4[0], kScalingList4x4ByteSize);
501 memcpy(scaling_list4x4[i], scaling_list4x4[1], kScalingList4x4ByteSize);
505 memcpy(scaling_list4x4[i], default_scaling_list_inter,
506 kScalingList4x4ByteSize);
510 memcpy(scaling_list4x4[i], scaling_list4x4[3], kScalingList4x4ByteSize);
514 memcpy(scaling_list4x4[i], scaling_list4x4[4], kScalingList4x4ByteSize);
523 static void FallbackScalingList8x8(
525 const int default_scaling_list_intra[],
526 const int default_scaling_list_inter[],
527 int scaling_list8x8[][kH264ScalingList8x8Length]) {
528 static const int kScalingList8x8ByteSize =
529 sizeof(scaling_list8x8[0][0]) * kH264ScalingList8x8Length;
533 memcpy(scaling_list8x8[i], default_scaling_list_intra,
534 kScalingList8x8ByteSize);
538 memcpy(scaling_list8x8[i], default_scaling_list_inter,
539 kScalingList8x8ByteSize);
543 memcpy(scaling_list8x8[i], scaling_list8x8[0], kScalingList8x8ByteSize);
547 memcpy(scaling_list8x8[i], scaling_list8x8[1], kScalingList8x8ByteSize);
551 memcpy(scaling_list8x8[i], scaling_list8x8[2], kScalingList8x8ByteSize);
555 memcpy(scaling_list8x8[i], scaling_list8x8[3], kScalingList8x8ByteSize);
564 H264Parser::Result H264Parser::ParseScalingList(
int size,
572 *use_default =
false;
574 for (
int j = 0; j < size; ++j) {
575 if (next_scale != 0) {
576 READ_SE_OR_RETURN(&delta_scale);
577 IN_RANGE_OR_RETURN(delta_scale, -128, 127);
578 next_scale = (last_scale + delta_scale + 256) & 0xff;
580 if (j == 0 && next_scale == 0) {
586 scaling_list[j] = (next_scale == 0) ? last_scale : next_scale;
587 last_scale = scaling_list[j];
593 H264Parser::Result H264Parser::ParseSPSScalingLists(H264SPS* sps) {
595 bool seq_scaling_list_present_flag;
600 for (
int i = 0; i < 6; ++i) {
601 READ_BOOL_OR_RETURN(&seq_scaling_list_present_flag);
603 if (seq_scaling_list_present_flag) {
604 res = ParseScalingList(arraysize(sps->scaling_list4x4[i]),
605 sps->scaling_list4x4[i],
611 DefaultScalingList4x4(i, sps->scaling_list4x4);
614 FallbackScalingList4x4(
615 i, kDefault4x4Intra, kDefault4x4Inter, sps->scaling_list4x4);
620 for (
int i = 0; i < ((sps->chroma_format_idc != 3) ? 2 : 6); ++i) {
621 READ_BOOL_OR_RETURN(&seq_scaling_list_present_flag);
623 if (seq_scaling_list_present_flag) {
624 res = ParseScalingList(arraysize(sps->scaling_list8x8[i]),
625 sps->scaling_list8x8[i],
631 DefaultScalingList8x8(i, sps->scaling_list8x8);
634 FallbackScalingList8x8(
635 i, kDefault8x8Intra, kDefault8x8Inter, sps->scaling_list8x8);
642 H264Parser::Result H264Parser::ParsePPSScalingLists(
const H264SPS& sps,
645 bool pic_scaling_list_present_flag;
649 for (
int i = 0; i < 6; ++i) {
650 READ_BOOL_OR_RETURN(&pic_scaling_list_present_flag);
652 if (pic_scaling_list_present_flag) {
653 res = ParseScalingList(arraysize(pps->scaling_list4x4[i]),
654 pps->scaling_list4x4[i],
660 DefaultScalingList4x4(i, pps->scaling_list4x4);
663 if (sps.seq_scaling_matrix_present_flag) {
665 FallbackScalingList4x4(
666 i, kDefault4x4Intra, kDefault4x4Inter, pps->scaling_list4x4);
669 FallbackScalingList4x4(i,
670 sps.scaling_list4x4[0],
671 sps.scaling_list4x4[3],
672 pps->scaling_list4x4);
677 if (pps->transform_8x8_mode_flag) {
678 for (
int i = 0; i < ((sps.chroma_format_idc != 3) ? 2 : 6); ++i) {
679 READ_BOOL_OR_RETURN(&pic_scaling_list_present_flag);
681 if (pic_scaling_list_present_flag) {
682 res = ParseScalingList(arraysize(pps->scaling_list8x8[i]),
683 pps->scaling_list8x8[i],
689 DefaultScalingList8x8(i, pps->scaling_list8x8);
692 if (sps.seq_scaling_matrix_present_flag) {
694 FallbackScalingList8x8(
695 i, kDefault8x8Intra, kDefault8x8Inter, pps->scaling_list8x8);
698 FallbackScalingList8x8(i,
699 sps.scaling_list8x8[0],
700 sps.scaling_list8x8[1],
701 pps->scaling_list8x8);
709 H264Parser::Result H264Parser::ParseAndIgnoreHRDParameters(
710 bool* hrd_parameters_present) {
712 READ_BOOL_OR_RETURN(&data);
716 *hrd_parameters_present =
true;
719 READ_UE_OR_RETURN(&cpb_cnt_minus1);
720 IN_RANGE_OR_RETURN(cpb_cnt_minus1, 0, 31);
721 READ_BITS_OR_RETURN(8, &data);
722 for (
int i = 0; i <= cpb_cnt_minus1; ++i) {
723 READ_UE_OR_RETURN(&data);
724 READ_UE_OR_RETURN(&data);
725 READ_BOOL_OR_RETURN(&data);
727 READ_BITS_OR_RETURN(20, &data);
732 H264Parser::Result H264Parser::ParseVUIParameters(H264SPS* sps) {
733 bool aspect_ratio_info_present_flag;
734 READ_BOOL_OR_RETURN(&aspect_ratio_info_present_flag);
735 if (aspect_ratio_info_present_flag) {
736 int aspect_ratio_idc;
737 READ_BITS_OR_RETURN(8, &aspect_ratio_idc);
738 if (aspect_ratio_idc == kExtendedSar) {
739 READ_BITS_OR_RETURN(16, &sps->sar_width);
740 READ_BITS_OR_RETURN(16, &sps->sar_height);
742 const int max_aspect_ratio_idc = arraysize(kTableSarWidth) - 1;
743 IN_RANGE_OR_RETURN(aspect_ratio_idc, 0, max_aspect_ratio_idc);
744 sps->sar_width = kTableSarWidth[aspect_ratio_idc];
745 sps->sar_height = kTableSarHeight[aspect_ratio_idc];
751 READ_BOOL_OR_RETURN(&data);
753 READ_BOOL_OR_RETURN(&data);
755 READ_BOOL_OR_RETURN(&data);
757 READ_BITS_OR_RETURN(3, &data);
758 READ_BOOL_OR_RETURN(&data);
759 READ_BOOL_OR_RETURN(&data);
761 READ_BITS_OR_RETURN(24, &data);
764 READ_BOOL_OR_RETURN(&data);
766 READ_UE_OR_RETURN(&data);
767 READ_UE_OR_RETURN(&data);
771 READ_BOOL_OR_RETURN(&data);
773 READ_BITS_OR_RETURN(16, &data);
774 READ_BITS_OR_RETURN(16, &data);
775 READ_BITS_OR_RETURN(16, &data);
776 READ_BITS_OR_RETURN(16, &data);
777 READ_BOOL_OR_RETURN(&data);
781 bool hrd_parameters_present =
false;
782 Result res = ParseAndIgnoreHRDParameters(&hrd_parameters_present);
787 res = ParseAndIgnoreHRDParameters(&hrd_parameters_present);
791 if (hrd_parameters_present)
792 READ_BOOL_OR_RETURN(&data);
794 READ_BOOL_OR_RETURN(&data);
795 READ_BOOL_OR_RETURN(&sps->bitstream_restriction_flag);
796 if (sps->bitstream_restriction_flag) {
797 READ_BOOL_OR_RETURN(&data);
798 READ_UE_OR_RETURN(&data);
799 READ_UE_OR_RETURN(&data);
800 READ_UE_OR_RETURN(&data);
801 READ_UE_OR_RETURN(&data);
802 READ_UE_OR_RETURN(&sps->max_num_reorder_frames);
803 READ_UE_OR_RETURN(&sps->max_dec_frame_buffering);
804 TRUE_OR_RETURN(sps->max_dec_frame_buffering >= sps->max_num_ref_frames);
806 sps->max_num_reorder_frames, 0, sps->max_dec_frame_buffering);
812 static void FillDefaultSeqScalingLists(H264SPS* sps) {
813 for (
int i = 0; i < 6; ++i)
814 for (
int j = 0; j < kH264ScalingList4x4Length; ++j)
815 sps->scaling_list4x4[i][j] = 16;
817 for (
int i = 0; i < 6; ++i)
818 for (
int j = 0; j < kH264ScalingList8x8Length; ++j)
819 sps->scaling_list8x8[i][j] = 16;
822 H264Parser::Result H264Parser::ParseSPS(
int* sps_id) {
829 scoped_ptr<H264SPS> sps(
new H264SPS());
831 READ_BITS_OR_RETURN(8, &sps->profile_idc);
832 READ_BOOL_OR_RETURN(&sps->constraint_set0_flag);
833 READ_BOOL_OR_RETURN(&sps->constraint_set1_flag);
834 READ_BOOL_OR_RETURN(&sps->constraint_set2_flag);
835 READ_BOOL_OR_RETURN(&sps->constraint_set3_flag);
836 READ_BOOL_OR_RETURN(&sps->constraint_set4_flag);
837 READ_BOOL_OR_RETURN(&sps->constraint_set5_flag);
838 READ_BITS_OR_RETURN(2, &data);
839 READ_BITS_OR_RETURN(8, &sps->level_idc);
840 READ_UE_OR_RETURN(&sps->seq_parameter_set_id);
841 TRUE_OR_RETURN(sps->seq_parameter_set_id < 32);
843 if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
844 sps->profile_idc == 122 || sps->profile_idc == 244 ||
845 sps->profile_idc == 44 || sps->profile_idc == 83 ||
846 sps->profile_idc == 86 || sps->profile_idc == 118 ||
847 sps->profile_idc == 128) {
848 READ_UE_OR_RETURN(&sps->chroma_format_idc);
849 TRUE_OR_RETURN(sps->chroma_format_idc < 4);
851 if (sps->chroma_format_idc == 3)
852 READ_BOOL_OR_RETURN(&sps->separate_colour_plane_flag);
854 READ_UE_OR_RETURN(&sps->bit_depth_luma_minus8);
855 TRUE_OR_RETURN(sps->bit_depth_luma_minus8 < 7);
857 READ_UE_OR_RETURN(&sps->bit_depth_chroma_minus8);
858 TRUE_OR_RETURN(sps->bit_depth_chroma_minus8 < 7);
860 READ_BOOL_OR_RETURN(&sps->qpprime_y_zero_transform_bypass_flag);
861 READ_BOOL_OR_RETURN(&sps->seq_scaling_matrix_present_flag);
863 if (sps->seq_scaling_matrix_present_flag) {
864 DVLOG(4) <<
"Scaling matrix present";
865 res = ParseSPSScalingLists(sps.get());
869 FillDefaultSeqScalingLists(sps.get());
872 sps->chroma_format_idc = 1;
873 FillDefaultSeqScalingLists(sps.get());
876 if (sps->separate_colour_plane_flag)
877 sps->chroma_array_type = 0;
879 sps->chroma_array_type = sps->chroma_format_idc;
881 READ_UE_OR_RETURN(&sps->log2_max_frame_num_minus4);
882 TRUE_OR_RETURN(sps->log2_max_frame_num_minus4 < 13);
884 READ_UE_OR_RETURN(&sps->pic_order_cnt_type);
885 TRUE_OR_RETURN(sps->pic_order_cnt_type < 3);
887 sps->expected_delta_per_pic_order_cnt_cycle = 0;
888 if (sps->pic_order_cnt_type == 0) {
889 READ_UE_OR_RETURN(&sps->log2_max_pic_order_cnt_lsb_minus4);
890 TRUE_OR_RETURN(sps->log2_max_pic_order_cnt_lsb_minus4 < 13);
891 }
else if (sps->pic_order_cnt_type == 1) {
892 READ_BOOL_OR_RETURN(&sps->delta_pic_order_always_zero_flag);
893 READ_SE_OR_RETURN(&sps->offset_for_non_ref_pic);
894 READ_SE_OR_RETURN(&sps->offset_for_top_to_bottom_field);
895 READ_UE_OR_RETURN(&sps->num_ref_frames_in_pic_order_cnt_cycle);
896 TRUE_OR_RETURN(sps->num_ref_frames_in_pic_order_cnt_cycle < 255);
898 for (
int i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; ++i) {
899 READ_SE_OR_RETURN(&sps->offset_for_ref_frame[i]);
900 sps->expected_delta_per_pic_order_cnt_cycle +=
901 sps->offset_for_ref_frame[i];
905 READ_UE_OR_RETURN(&sps->max_num_ref_frames);
906 READ_BOOL_OR_RETURN(&sps->gaps_in_frame_num_value_allowed_flag);
908 if (sps->gaps_in_frame_num_value_allowed_flag)
909 return kUnsupportedStream;
911 READ_UE_OR_RETURN(&sps->pic_width_in_mbs_minus1);
912 READ_UE_OR_RETURN(&sps->pic_height_in_map_units_minus1);
914 READ_BOOL_OR_RETURN(&sps->frame_mbs_only_flag);
915 if (!sps->frame_mbs_only_flag)
916 READ_BOOL_OR_RETURN(&sps->mb_adaptive_frame_field_flag);
918 READ_BOOL_OR_RETURN(&sps->direct_8x8_inference_flag);
920 READ_BOOL_OR_RETURN(&sps->frame_cropping_flag);
921 if (sps->frame_cropping_flag) {
922 READ_UE_OR_RETURN(&sps->frame_crop_left_offset);
923 READ_UE_OR_RETURN(&sps->frame_crop_right_offset);
924 READ_UE_OR_RETURN(&sps->frame_crop_top_offset);
925 READ_UE_OR_RETURN(&sps->frame_crop_bottom_offset);
928 READ_BOOL_OR_RETURN(&sps->vui_parameters_present_flag);
929 if (sps->vui_parameters_present_flag) {
930 DVLOG(4) <<
"VUI parameters present";
931 res = ParseVUIParameters(sps.get());
937 *sps_id = sps->seq_parameter_set_id;
938 delete active_SPSes_[*sps_id];
939 active_SPSes_[*sps_id] = sps.release();
944 H264Parser::Result H264Parser::ParsePPS(
int* pps_id) {
951 scoped_ptr<H264PPS> pps(
new H264PPS());
953 READ_UE_OR_RETURN(&pps->pic_parameter_set_id);
954 READ_UE_OR_RETURN(&pps->seq_parameter_set_id);
955 TRUE_OR_RETURN(pps->seq_parameter_set_id < 32);
957 sps = GetSPS(pps->seq_parameter_set_id);
960 READ_BOOL_OR_RETURN(&pps->entropy_coding_mode_flag);
961 READ_BOOL_OR_RETURN(&pps->bottom_field_pic_order_in_frame_present_flag);
963 READ_UE_OR_RETURN(&pps->num_slice_groups_minus1);
964 if (pps->num_slice_groups_minus1 > 1) {
965 DVLOG(1) <<
"Slice groups not supported";
966 return kUnsupportedStream;
969 READ_UE_OR_RETURN(&pps->num_ref_idx_l0_default_active_minus1);
970 TRUE_OR_RETURN(pps->num_ref_idx_l0_default_active_minus1 < 32);
972 READ_UE_OR_RETURN(&pps->num_ref_idx_l1_default_active_minus1);
973 TRUE_OR_RETURN(pps->num_ref_idx_l1_default_active_minus1 < 32);
975 READ_BOOL_OR_RETURN(&pps->weighted_pred_flag);
976 READ_BITS_OR_RETURN(2, &pps->weighted_bipred_idc);
977 TRUE_OR_RETURN(pps->weighted_bipred_idc < 3);
979 READ_SE_OR_RETURN(&pps->pic_init_qp_minus26);
980 IN_RANGE_OR_RETURN(pps->pic_init_qp_minus26, -26, 25);
982 READ_SE_OR_RETURN(&pps->pic_init_qs_minus26);
983 IN_RANGE_OR_RETURN(pps->pic_init_qs_minus26, -26, 25);
985 READ_SE_OR_RETURN(&pps->chroma_qp_index_offset);
986 IN_RANGE_OR_RETURN(pps->chroma_qp_index_offset, -12, 12);
987 pps->second_chroma_qp_index_offset = pps->chroma_qp_index_offset;
989 READ_BOOL_OR_RETURN(&pps->deblocking_filter_control_present_flag);
990 READ_BOOL_OR_RETURN(&pps->constrained_intra_pred_flag);
991 READ_BOOL_OR_RETURN(&pps->redundant_pic_cnt_present_flag);
993 if (br_.HasMoreRBSPData()) {
994 READ_BOOL_OR_RETURN(&pps->transform_8x8_mode_flag);
995 READ_BOOL_OR_RETURN(&pps->pic_scaling_matrix_present_flag);
997 if (pps->pic_scaling_matrix_present_flag) {
998 DVLOG(4) <<
"Picture scaling matrix present";
999 res = ParsePPSScalingLists(*sps, pps.get());
1004 READ_SE_OR_RETURN(&pps->second_chroma_qp_index_offset);
1008 *pps_id = pps->pic_parameter_set_id;
1009 delete active_PPSes_[*pps_id];
1010 active_PPSes_[*pps_id] = pps.release();
1015 H264Parser::Result H264Parser::ParseSPSFromArray(
1016 const uint8_t* sps_data,
1017 size_t sps_data_length,
1019 br_.Initialize(sps_data, sps_data_length);
1022 READ_BITS_OR_RETURN(1, &data);
1024 TRUE_OR_RETURN(data == 0);
1026 READ_BITS_OR_RETURN(2, &nal_ref_idc);
1029 TRUE_OR_RETURN(nal_ref_idc != 0);
1031 READ_BITS_OR_RETURN(5, &nal_unit_type);
1032 TRUE_OR_RETURN(nal_unit_type == H264NALU::kSPS);
1034 return ParseSPS(sps_id);
1037 H264Parser::Result H264Parser::ParseRefPicListModification(
1038 int num_ref_idx_active_minus1,
1039 H264ModificationOfPicNum* ref_list_mods) {
1040 H264ModificationOfPicNum* pic_num_mod;
1042 if (num_ref_idx_active_minus1 >= 32)
1043 return kInvalidStream;
1045 for (
int i = 0; i < 32; ++i) {
1046 pic_num_mod = &ref_list_mods[i];
1047 READ_UE_OR_RETURN(&pic_num_mod->modification_of_pic_nums_idc);
1048 TRUE_OR_RETURN(pic_num_mod->modification_of_pic_nums_idc < 4);
1050 switch (pic_num_mod->modification_of_pic_nums_idc) {
1053 READ_UE_OR_RETURN(&pic_num_mod->abs_diff_pic_num_minus1);
1057 READ_UE_OR_RETURN(&pic_num_mod->long_term_pic_num);
1063 return kInvalidStream;
1067 return kInvalidStream;
1073 int modification_of_pic_nums_idc;
1074 READ_UE_OR_RETURN(&modification_of_pic_nums_idc);
1075 TRUE_OR_RETURN(modification_of_pic_nums_idc == 3);
1080 H264Parser::Result H264Parser::ParseRefPicListModifications(
1081 H264SliceHeader* shdr) {
1084 if (!shdr->IsISlice() && !shdr->IsSISlice()) {
1085 READ_BOOL_OR_RETURN(&shdr->ref_pic_list_modification_flag_l0);
1086 if (shdr->ref_pic_list_modification_flag_l0) {
1087 res = ParseRefPicListModification(shdr->num_ref_idx_l0_active_minus1,
1088 shdr->ref_list_l0_modifications);
1094 if (shdr->IsBSlice()) {
1095 READ_BOOL_OR_RETURN(&shdr->ref_pic_list_modification_flag_l1);
1096 if (shdr->ref_pic_list_modification_flag_l1) {
1097 res = ParseRefPicListModification(shdr->num_ref_idx_l1_active_minus1,
1098 shdr->ref_list_l1_modifications);
1107 H264Parser::Result H264Parser::ParseWeightingFactors(
1108 int num_ref_idx_active_minus1,
1109 int chroma_array_type,
1110 int luma_log2_weight_denom,
1111 int chroma_log2_weight_denom,
1112 H264WeightingFactors* w_facts) {
1114 int def_luma_weight = 1 << luma_log2_weight_denom;
1115 int def_chroma_weight = 1 << chroma_log2_weight_denom;
1117 for (
int i = 0; i < num_ref_idx_active_minus1 + 1; ++i) {
1118 READ_BOOL_OR_RETURN(&w_facts->luma_weight_flag);
1119 if (w_facts->luma_weight_flag) {
1120 READ_SE_OR_RETURN(&w_facts->luma_weight[i]);
1121 IN_RANGE_OR_RETURN(w_facts->luma_weight[i], -128, 127);
1123 READ_SE_OR_RETURN(&w_facts->luma_offset[i]);
1124 IN_RANGE_OR_RETURN(w_facts->luma_offset[i], -128, 127);
1126 w_facts->luma_weight[i] = def_luma_weight;
1127 w_facts->luma_offset[i] = 0;
1130 if (chroma_array_type != 0) {
1131 READ_BOOL_OR_RETURN(&w_facts->chroma_weight_flag);
1132 if (w_facts->chroma_weight_flag) {
1133 for (
int j = 0; j < 2; ++j) {
1134 READ_SE_OR_RETURN(&w_facts->chroma_weight[i][j]);
1135 IN_RANGE_OR_RETURN(w_facts->chroma_weight[i][j], -128, 127);
1137 READ_SE_OR_RETURN(&w_facts->chroma_offset[i][j]);
1138 IN_RANGE_OR_RETURN(w_facts->chroma_offset[i][j], -128, 127);
1141 for (
int j = 0; j < 2; ++j) {
1142 w_facts->chroma_weight[i][j] = def_chroma_weight;
1143 w_facts->chroma_offset[i][j] = 0;
1152 H264Parser::Result H264Parser::ParsePredWeightTable(
const H264SPS& sps,
1153 H264SliceHeader* shdr) {
1154 READ_UE_OR_RETURN(&shdr->luma_log2_weight_denom);
1155 TRUE_OR_RETURN(shdr->luma_log2_weight_denom < 8);
1157 if (sps.chroma_array_type != 0)
1158 READ_UE_OR_RETURN(&shdr->chroma_log2_weight_denom);
1159 TRUE_OR_RETURN(shdr->chroma_log2_weight_denom < 8);
1161 Result res = ParseWeightingFactors(shdr->num_ref_idx_l0_active_minus1,
1162 sps.chroma_array_type,
1163 shdr->luma_log2_weight_denom,
1164 shdr->chroma_log2_weight_denom,
1165 &shdr->pred_weight_table_l0);
1169 if (shdr->IsBSlice()) {
1170 res = ParseWeightingFactors(shdr->num_ref_idx_l1_active_minus1,
1171 sps.chroma_array_type,
1172 shdr->luma_log2_weight_denom,
1173 shdr->chroma_log2_weight_denom,
1174 &shdr->pred_weight_table_l1);
1182 H264Parser::Result H264Parser::ParseDecRefPicMarking(H264SliceHeader* shdr) {
1183 if (shdr->idr_pic_flag) {
1184 READ_BOOL_OR_RETURN(&shdr->no_output_of_prior_pics_flag);
1185 READ_BOOL_OR_RETURN(&shdr->long_term_reference_flag);
1187 READ_BOOL_OR_RETURN(&shdr->adaptive_ref_pic_marking_mode_flag);
1189 H264DecRefPicMarking* marking;
1190 if (shdr->adaptive_ref_pic_marking_mode_flag) {
1192 for (i = 0; i < arraysize(shdr->ref_pic_marking); ++i) {
1193 marking = &shdr->ref_pic_marking[i];
1195 READ_UE_OR_RETURN(&marking->memory_mgmnt_control_operation);
1196 if (marking->memory_mgmnt_control_operation == 0)
1199 if (marking->memory_mgmnt_control_operation == 1 ||
1200 marking->memory_mgmnt_control_operation == 3)
1201 READ_UE_OR_RETURN(&marking->difference_of_pic_nums_minus1);
1203 if (marking->memory_mgmnt_control_operation == 2)
1204 READ_UE_OR_RETURN(&marking->long_term_pic_num);
1206 if (marking->memory_mgmnt_control_operation == 3 ||
1207 marking->memory_mgmnt_control_operation == 6)
1208 READ_UE_OR_RETURN(&marking->long_term_frame_idx);
1210 if (marking->memory_mgmnt_control_operation == 4)
1211 READ_UE_OR_RETURN(&marking->max_long_term_frame_idx_plus1);
1213 if (marking->memory_mgmnt_control_operation > 6)
1214 return kInvalidStream;
1217 if (i == arraysize(shdr->ref_pic_marking)) {
1218 DVLOG(1) <<
"Ran out of dec ref pic marking fields";
1219 return kUnsupportedStream;
1227 H264Parser::Result H264Parser::ParseSliceHeader(
const H264NALU& nalu,
1228 H264SliceHeader* shdr) {
1234 memset(shdr, 0,
sizeof(*shdr));
1236 shdr->idr_pic_flag = (nalu.nal_unit_type == 5);
1237 shdr->nal_ref_idc = nalu.nal_ref_idc;
1238 shdr->nalu_data = nalu.data;
1239 shdr->nalu_size = nalu.size;
1241 READ_UE_OR_RETURN(&shdr->first_mb_in_slice);
1242 READ_UE_OR_RETURN(&shdr->slice_type);
1243 TRUE_OR_RETURN(shdr->slice_type < 10);
1245 READ_UE_OR_RETURN(&shdr->pic_parameter_set_id);
1247 pps = GetPPS(shdr->pic_parameter_set_id);
1248 TRUE_OR_RETURN(pps);
1250 sps = GetSPS(pps->seq_parameter_set_id);
1251 TRUE_OR_RETURN(sps);
1253 if (sps->separate_colour_plane_flag) {
1254 DVLOG(1) <<
"Interlaced streams not supported";
1255 return kUnsupportedStream;
1258 READ_BITS_OR_RETURN(sps->log2_max_frame_num_minus4 + 4, &shdr->frame_num);
1259 if (!sps->frame_mbs_only_flag) {
1260 READ_BOOL_OR_RETURN(&shdr->field_pic_flag);
1261 if (shdr->field_pic_flag) {
1262 DVLOG(1) <<
"Interlaced streams not supported";
1263 return kUnsupportedStream;
1267 if (shdr->idr_pic_flag)
1268 READ_UE_OR_RETURN(&shdr->idr_pic_id);
1270 if (sps->pic_order_cnt_type == 0) {
1271 READ_BITS_OR_RETURN(sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
1272 &shdr->pic_order_cnt_lsb);
1273 if (pps->bottom_field_pic_order_in_frame_present_flag &&
1274 !shdr->field_pic_flag)
1275 READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt_bottom);
1278 if (sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag) {
1279 READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt[0]);
1280 if (pps->bottom_field_pic_order_in_frame_present_flag &&
1281 !shdr->field_pic_flag)
1282 READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt[1]);
1285 if (pps->redundant_pic_cnt_present_flag) {
1286 READ_UE_OR_RETURN(&shdr->redundant_pic_cnt);
1287 TRUE_OR_RETURN(shdr->redundant_pic_cnt < 128);
1290 if (shdr->IsBSlice())
1291 READ_BOOL_OR_RETURN(&shdr->direct_spatial_mv_pred_flag);
1293 if (shdr->IsPSlice() || shdr->IsSPSlice() || shdr->IsBSlice()) {
1294 READ_BOOL_OR_RETURN(&shdr->num_ref_idx_active_override_flag);
1295 if (shdr->num_ref_idx_active_override_flag) {
1296 READ_UE_OR_RETURN(&shdr->num_ref_idx_l0_active_minus1);
1297 if (shdr->IsBSlice())
1298 READ_UE_OR_RETURN(&shdr->num_ref_idx_l1_active_minus1);
1300 shdr->num_ref_idx_l0_active_minus1 =
1301 pps->num_ref_idx_l0_default_active_minus1;
1302 if (shdr->IsBSlice()) {
1303 shdr->num_ref_idx_l1_active_minus1 =
1304 pps->num_ref_idx_l1_default_active_minus1;
1308 if (shdr->field_pic_flag) {
1309 TRUE_OR_RETURN(shdr->num_ref_idx_l0_active_minus1 < 32);
1310 TRUE_OR_RETURN(shdr->num_ref_idx_l1_active_minus1 < 32);
1312 TRUE_OR_RETURN(shdr->num_ref_idx_l0_active_minus1 < 16);
1313 TRUE_OR_RETURN(shdr->num_ref_idx_l1_active_minus1 < 16);
1316 if (nalu.nal_unit_type == H264NALU::kCodedSliceExtension) {
1317 return kUnsupportedStream;
1319 res = ParseRefPicListModifications(shdr);
1324 if ((pps->weighted_pred_flag && (shdr->IsPSlice() || shdr->IsSPSlice())) ||
1325 (pps->weighted_bipred_idc == 1 && shdr->IsBSlice())) {
1326 res = ParsePredWeightTable(*sps, shdr);
1331 if (nalu.nal_ref_idc != 0) {
1332 res = ParseDecRefPicMarking(shdr);
1337 if (pps->entropy_coding_mode_flag && !shdr->IsISlice() &&
1338 !shdr->IsSISlice()) {
1339 READ_UE_OR_RETURN(&shdr->cabac_init_idc);
1340 TRUE_OR_RETURN(shdr->cabac_init_idc < 3);
1343 READ_SE_OR_RETURN(&shdr->slice_qp_delta);
1345 if (shdr->IsSPSlice() || shdr->IsSISlice()) {
1346 if (shdr->IsSPSlice())
1347 READ_BOOL_OR_RETURN(&shdr->sp_for_switch_flag);
1348 READ_SE_OR_RETURN(&shdr->slice_qs_delta);
1351 if (pps->deblocking_filter_control_present_flag) {
1352 READ_UE_OR_RETURN(&shdr->disable_deblocking_filter_idc);
1353 TRUE_OR_RETURN(shdr->disable_deblocking_filter_idc < 3);
1355 if (shdr->disable_deblocking_filter_idc != 1) {
1356 READ_SE_OR_RETURN(&shdr->slice_alpha_c0_offset_div2);
1357 IN_RANGE_OR_RETURN(shdr->slice_alpha_c0_offset_div2, -6, 6);
1359 READ_SE_OR_RETURN(&shdr->slice_beta_offset_div2);
1360 IN_RANGE_OR_RETURN(shdr->slice_beta_offset_div2, -6, 6);
1364 if (pps->num_slice_groups_minus1 > 0) {
1365 DVLOG(1) <<
"Slice groups not supported";
1366 return kUnsupportedStream;
1369 size_t epb = br_.NumEmulationPreventionBytesRead();
1370 shdr->header_bit_size = (shdr->nalu_size - epb) * 8 - br_.NumBitsLeft();
1375 H264Parser::Result H264Parser::ParseSEI(H264SEIMessage* sei_msg) {
1378 memset(sei_msg, 0,
sizeof(*sei_msg));
1380 READ_BITS_OR_RETURN(8, &byte);
1381 while (byte == 0xff) {
1382 sei_msg->type += 255;
1383 READ_BITS_OR_RETURN(8, &byte);
1385 sei_msg->type += byte;
1387 READ_BITS_OR_RETURN(8, &byte);
1388 while (byte == 0xff) {
1389 sei_msg->payload_size += 255;
1390 READ_BITS_OR_RETURN(8, &byte);
1392 sei_msg->payload_size += byte;
1394 DVLOG(4) <<
"Found SEI message type: " << sei_msg->type
1395 <<
" payload size: " << sei_msg->payload_size;
1397 switch (sei_msg->type) {
1398 case H264SEIMessage::kSEIRecoveryPoint:
1399 READ_UE_OR_RETURN(&sei_msg->recovery_point.recovery_frame_cnt);
1400 READ_BOOL_OR_RETURN(&sei_msg->recovery_point.exact_match_flag);
1401 READ_BOOL_OR_RETURN(&sei_msg->recovery_point.broken_link_flag);
1402 READ_BITS_OR_RETURN(2, &sei_msg->recovery_point.changing_slice_group_idc);
1406 DVLOG(4) <<
"Unsupported SEI message";