Home | History | Annotate | Download | only in vda
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "h264_parser.h"
      6 
      7 #include <limits>
      8 #include <memory>
      9 
     10 #include "base/logging.h"
     11 #include "base/macros.h"
     12 #include "base/numerics/safe_math.h"
     13 
     14 namespace media {
     15 
     16 bool H264SliceHeader::IsPSlice() const {
     17   return (slice_type % 5 == kPSlice);
     18 }
     19 
     20 bool H264SliceHeader::IsBSlice() const {
     21   return (slice_type % 5 == kBSlice);
     22 }
     23 
     24 bool H264SliceHeader::IsISlice() const {
     25   return (slice_type % 5 == kISlice);
     26 }
     27 
     28 bool H264SliceHeader::IsSPSlice() const {
     29   return (slice_type % 5 == kSPSlice);
     30 }
     31 
     32 bool H264SliceHeader::IsSISlice() const {
     33   return (slice_type % 5 == kSISlice);
     34 }
     35 
     36 H264NALU::H264NALU() {
     37   memset(this, 0, sizeof(*this));
     38 }
     39 
     40 H264SPS::H264SPS() {
     41   memset(this, 0, sizeof(*this));
     42 }
     43 
     44 // Based on T-REC-H.264 7.4.2.1.1, "Sequence parameter set data semantics",
     45 // available from http://www.itu.int/rec/T-REC-H.264.
     46 base::Optional<Size> H264SPS::GetCodedSize() const {
     47   // Interlaced frames are twice the height of each field.
     48   const int mb_unit = 16;
     49   int map_unit = frame_mbs_only_flag ? 16 : 32;
     50 
     51   // Verify that the values are not too large before multiplying them.
     52   // TODO(sandersd): These limits could be much smaller. The currently-largest
     53   // specified limit (excluding SVC, multiview, etc., which I didn't bother to
     54   // read) is 543 macroblocks (section A.3.1).
     55   int max_mb_minus1 = std::numeric_limits<int>::max() / mb_unit - 1;
     56   int max_map_units_minus1 = std::numeric_limits<int>::max() / map_unit - 1;
     57   if (pic_width_in_mbs_minus1 > max_mb_minus1 ||
     58       pic_height_in_map_units_minus1 > max_map_units_minus1) {
     59     DVLOG(1) << "Coded size is too large.";
     60     return base::nullopt;
     61   }
     62 
     63   return Size(mb_unit * (pic_width_in_mbs_minus1 + 1),
     64               map_unit * (pic_height_in_map_units_minus1 + 1));
     65 }
     66 
     67 H264PPS::H264PPS() {
     68   memset(this, 0, sizeof(*this));
     69 }
     70 
     71 H264SliceHeader::H264SliceHeader() {
     72   memset(this, 0, sizeof(*this));
     73 }
     74 
     75 H264SEIMessage::H264SEIMessage() {
     76   memset(this, 0, sizeof(*this));
     77 }
     78 
     79 #define READ_BITS_OR_RETURN(num_bits, out)                                 \
     80   do {                                                                     \
     81     int _out;                                                              \
     82     if (!br_.ReadBits(num_bits, &_out)) {                                  \
     83       DVLOG(1)                                                             \
     84           << "Error in stream: unexpected EOS while trying to read " #out; \
     85       return kInvalidStream;                                               \
     86     }                                                                      \
     87     *out = _out;                                                           \
     88   } while (0)
     89 
     90 #define READ_BOOL_OR_RETURN(out)                                           \
     91   do {                                                                     \
     92     int _out;                                                              \
     93     if (!br_.ReadBits(1, &_out)) {                                         \
     94       DVLOG(1)                                                             \
     95           << "Error in stream: unexpected EOS while trying to read " #out; \
     96       return kInvalidStream;                                               \
     97     }                                                                      \
     98     *out = _out != 0;                                                      \
     99   } while (0)
    100 
    101 #define READ_UE_OR_RETURN(out)                                                 \
    102   do {                                                                         \
    103     if (ReadUE(out) != kOk) {                                                  \
    104       DVLOG(1) << "Error in stream: invalid value while trying to read " #out; \
    105       return kInvalidStream;                                                   \
    106     }                                                                          \
    107   } while (0)
    108 
    109 #define READ_SE_OR_RETURN(out)                                                 \
    110   do {                                                                         \
    111     if (ReadSE(out) != kOk) {                                                  \
    112       DVLOG(1) << "Error in stream: invalid value while trying to read " #out; \
    113       return kInvalidStream;                                                   \
    114     }                                                                          \
    115   } while (0)
    116 
    117 #define IN_RANGE_OR_RETURN(val, min, max)                                   \
    118   do {                                                                      \
    119     if ((val) < (min) || (val) > (max)) {                                   \
    120       DVLOG(1) << "Error in stream: invalid value, expected " #val " to be" \
    121                << " in range [" << (min) << ":" << (max) << "]"             \
    122                << " found " << (val) << " instead";                         \
    123       return kInvalidStream;                                                \
    124     }                                                                       \
    125   } while (0)
    126 
    127 #define TRUE_OR_RETURN(a)                                            \
    128   do {                                                               \
    129     if (!(a)) {                                                      \
    130       DVLOG(1) << "Error in stream: invalid value, expected " << #a; \
    131       return kInvalidStream;                                         \
    132     }                                                                \
    133   } while (0)
    134 
    135 // ISO 14496 part 10
    136 // VUI parameters: Table E-1 "Meaning of sample aspect ratio indicator"
    137 static const int kTableSarWidth[] = {
    138   0, 1, 12, 10, 16, 40, 24, 20, 32, 80, 18, 15, 64, 160, 4, 3, 2
    139 };
    140 static const int kTableSarHeight[] = {
    141   0, 1, 11, 11, 11, 33, 11, 11, 11, 33, 11, 11, 33, 99, 3, 2, 1
    142 };
    143 static_assert(arraysize(kTableSarWidth) == arraysize(kTableSarHeight),
    144               "sar tables must have the same size");
    145 
    146 H264Parser::H264Parser() {
    147   Reset();
    148 }
    149 
    150 H264Parser::~H264Parser() {
    151 }
    152 
    153 void H264Parser::Reset() {
    154   stream_ = NULL;
    155   bytes_left_ = 0;
    156   encrypted_ranges_.clear();
    157 }
    158 
    159 void H264Parser::SetStream(const uint8_t* stream, off_t stream_size) {
    160   std::vector<SubsampleEntry> subsamples;
    161   SetEncryptedStream(stream, stream_size, subsamples);
    162 }
    163 
    164 void H264Parser::SetEncryptedStream(
    165     const uint8_t* stream,
    166     off_t stream_size,
    167     const std::vector<SubsampleEntry>& subsamples) {
    168   DCHECK(stream);
    169   DCHECK_GT(stream_size, 0);
    170 
    171   stream_ = stream;
    172   bytes_left_ = stream_size;
    173 
    174   encrypted_ranges_.clear();
    175   const uint8_t* start = stream;
    176   const uint8_t* stream_end = stream_ + bytes_left_;
    177   for (size_t i = 0; i < subsamples.size() && start < stream_end; ++i) {
    178     start += subsamples[i].clear_bytes;
    179 
    180     const uint8_t* end =
    181         std::min(start + subsamples[i].cypher_bytes, stream_end);
    182     encrypted_ranges_.Add(start, end);
    183     start = end;
    184   }
    185 }
    186 
    187 const H264PPS* H264Parser::GetPPS(int pps_id) const {
    188   auto it = active_PPSes_.find(pps_id);
    189   if (it == active_PPSes_.end()) {
    190     DVLOG(1) << "Requested a nonexistent PPS id " << pps_id;
    191     return nullptr;
    192   }
    193 
    194   return it->second.get();
    195 }
    196 
    197 const H264SPS* H264Parser::GetSPS(int sps_id) const {
    198   auto it = active_SPSes_.find(sps_id);
    199   if (it == active_SPSes_.end()) {
    200     DVLOG(1) << "Requested a nonexistent SPS id " << sps_id;
    201     return nullptr;
    202   }
    203 
    204   return it->second.get();
    205 }
    206 
    207 static inline bool IsStartCode(const uint8_t* data) {
    208   return data[0] == 0x00 && data[1] == 0x00 && data[2] == 0x01;
    209 }
    210 
    211 // static
    212 bool H264Parser::FindStartCode(const uint8_t* data,
    213                                off_t data_size,
    214                                off_t* offset,
    215                                off_t* start_code_size) {
    216   DCHECK_GE(data_size, 0);
    217   off_t bytes_left = data_size;
    218 
    219   while (bytes_left >= 3) {
    220     if (IsStartCode(data)) {
    221       // Found three-byte start code, set pointer at its beginning.
    222       *offset = data_size - bytes_left;
    223       *start_code_size = 3;
    224 
    225       // If there is a zero byte before this start code,
    226       // then it's actually a four-byte start code, so backtrack one byte.
    227       if (*offset > 0 && *(data - 1) == 0x00) {
    228         --(*offset);
    229         ++(*start_code_size);
    230       }
    231 
    232       return true;
    233     }
    234 
    235     ++data;
    236     --bytes_left;
    237   }
    238 
    239   // End of data: offset is pointing to the first byte that was not considered
    240   // as a possible start of a start code.
    241   // Note: there is no security issue when receiving a negative |data_size|
    242   // since in this case, |bytes_left| is equal to |data_size| and thus
    243   // |*offset| is equal to 0 (valid offset).
    244   *offset = data_size - bytes_left;
    245   *start_code_size = 0;
    246   return false;
    247 }
    248 
    249 bool H264Parser::LocateNALU(off_t* nalu_size, off_t* start_code_size) {
    250   // Find the start code of next NALU.
    251   off_t nalu_start_off = 0;
    252   off_t annexb_start_code_size = 0;
    253 
    254   if (!FindStartCodeInClearRanges(stream_, bytes_left_,
    255                                   encrypted_ranges_,
    256                                   &nalu_start_off, &annexb_start_code_size)) {
    257     DVLOG(4) << "Could not find start code, end of stream?";
    258     return false;
    259   }
    260 
    261   // Move the stream to the beginning of the NALU (pointing at the start code).
    262   stream_ += nalu_start_off;
    263   bytes_left_ -= nalu_start_off;
    264 
    265   const uint8_t* nalu_data = stream_ + annexb_start_code_size;
    266   off_t max_nalu_data_size = bytes_left_ - annexb_start_code_size;
    267   if (max_nalu_data_size <= 0) {
    268     DVLOG(3) << "End of stream";
    269     return false;
    270   }
    271 
    272   // Find the start code of next NALU;
    273   // if successful, |nalu_size_without_start_code| is the number of bytes from
    274   // after previous start code to before this one;
    275   // if next start code is not found, it is still a valid NALU since there
    276   // are some bytes left after the first start code: all the remaining bytes
    277   // belong to the current NALU.
    278   off_t next_start_code_size = 0;
    279   off_t nalu_size_without_start_code = 0;
    280   if (!FindStartCodeInClearRanges(nalu_data, max_nalu_data_size,
    281                                   encrypted_ranges_,
    282                                   &nalu_size_without_start_code,
    283                                   &next_start_code_size)) {
    284     nalu_size_without_start_code = max_nalu_data_size;
    285   }
    286   *nalu_size = nalu_size_without_start_code + annexb_start_code_size;
    287   *start_code_size = annexb_start_code_size;
    288   return true;
    289 }
    290 
    291 bool H264Parser::FindStartCodeInClearRanges(
    292     const uint8_t* data,
    293     off_t data_size,
    294     const Ranges<const uint8_t*>& encrypted_ranges,
    295     off_t* offset,
    296     off_t* start_code_size) {
    297   if (encrypted_ranges.size() == 0)
    298     return FindStartCode(data, data_size, offset, start_code_size);
    299 
    300   DCHECK_GE(data_size, 0);
    301   const uint8_t* start = data;
    302   do {
    303     off_t bytes_left = data_size - (start - data);
    304 
    305     if (!FindStartCode(start, bytes_left, offset, start_code_size))
    306       return false;
    307 
    308     // Construct a Ranges object that represents the region occupied
    309     // by the start code and the 1 byte needed to read the NAL unit type.
    310     const uint8_t* start_code = start + *offset;
    311     const uint8_t* start_code_end = start_code + *start_code_size;
    312     Ranges<const uint8_t*> start_code_range;
    313     start_code_range.Add(start_code, start_code_end + 1);
    314 
    315     if (encrypted_ranges.IntersectionWith(start_code_range).size() > 0) {
    316       // The start code is inside an encrypted section so we need to scan
    317       // for another start code.
    318       *start_code_size = 0;
    319       start += std::min(*offset + 1, bytes_left);
    320     }
    321   } while (*start_code_size == 0);
    322 
    323   // Update |*offset| to include the data we skipped over.
    324   *offset += start - data;
    325   return true;
    326 }
    327 
    328 H264Parser::Result H264Parser::ReadUE(int* val) {
    329   int num_bits = -1;
    330   int bit;
    331   int rest;
    332 
    333   // Count the number of contiguous zero bits.
    334   do {
    335     READ_BITS_OR_RETURN(1, &bit);
    336     num_bits++;
    337   } while (bit == 0);
    338 
    339   if (num_bits > 31)
    340     return kInvalidStream;
    341 
    342   // Calculate exp-Golomb code value of size num_bits.
    343   // Special case for |num_bits| == 31 to avoid integer overflow. The only
    344   // valid representation as an int is 2^31 - 1, so the remaining bits must
    345   // be 0 or else the number is too large.
    346   *val = (1u << num_bits) - 1u;
    347 
    348   if (num_bits == 31) {
    349     READ_BITS_OR_RETURN(num_bits, &rest);
    350     return (rest == 0) ? kOk : kInvalidStream;
    351   }
    352 
    353   if (num_bits > 0) {
    354     READ_BITS_OR_RETURN(num_bits, &rest);
    355     *val += rest;
    356   }
    357 
    358   return kOk;
    359 }
    360 
    361 H264Parser::Result H264Parser::ReadSE(int* val) {
    362   int ue;
    363   Result res;
    364 
    365   // See Chapter 9 in the spec.
    366   res = ReadUE(&ue);
    367   if (res != kOk)
    368     return res;
    369 
    370   if (ue % 2 == 0)
    371     *val = -(ue / 2);
    372   else
    373     *val = ue / 2 + 1;
    374 
    375   return kOk;
    376 }
    377 
    378 H264Parser::Result H264Parser::AdvanceToNextNALU(H264NALU* nalu) {
    379   off_t start_code_size;
    380   off_t nalu_size_with_start_code;
    381   if (!LocateNALU(&nalu_size_with_start_code, &start_code_size)) {
    382     DVLOG(4) << "Could not find next NALU, bytes left in stream: "
    383              << bytes_left_;
    384     return kEOStream;
    385   }
    386 
    387   nalu->data = stream_ + start_code_size;
    388   nalu->size = nalu_size_with_start_code - start_code_size;
    389   DVLOG(4) << "NALU found: size=" << nalu_size_with_start_code;
    390 
    391   // Initialize bit reader at the start of found NALU.
    392   if (!br_.Initialize(nalu->data, nalu->size))
    393     return kEOStream;
    394 
    395   // Move parser state to after this NALU, so next time AdvanceToNextNALU
    396   // is called, we will effectively be skipping it;
    397   // other parsing functions will use the position saved
    398   // in bit reader for parsing, so we don't have to remember it here.
    399   stream_ += nalu_size_with_start_code;
    400   bytes_left_ -= nalu_size_with_start_code;
    401 
    402   // Read NALU header, skip the forbidden_zero_bit, but check for it.
    403   int data;
    404   READ_BITS_OR_RETURN(1, &data);
    405   TRUE_OR_RETURN(data == 0);
    406 
    407   READ_BITS_OR_RETURN(2, &nalu->nal_ref_idc);
    408   READ_BITS_OR_RETURN(5, &nalu->nal_unit_type);
    409 
    410   DVLOG(4) << "NALU type: " << static_cast<int>(nalu->nal_unit_type)
    411            << " at: " << reinterpret_cast<const void*>(nalu->data)
    412            << " size: " << nalu->size
    413            << " ref: " << static_cast<int>(nalu->nal_ref_idc);
    414 
    415   return kOk;
    416 }
    417 
    418 // Default scaling lists (per spec).
    419 static const int kDefault4x4Intra[kH264ScalingList4x4Length] = {
    420     6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32, 32, 37, 37, 42, };
    421 
    422 static const int kDefault4x4Inter[kH264ScalingList4x4Length] = {
    423     10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27, 27, 30, 30, 34, };
    424 
    425 static const int kDefault8x8Intra[kH264ScalingList8x8Length] = {
    426     6,  10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18, 18, 18, 18, 23,
    427     23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27,
    428     27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31,
    429     31, 33, 33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42, };
    430 
    431 static const int kDefault8x8Inter[kH264ScalingList8x8Length] = {
    432     9,  13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19, 19, 19, 19, 21,
    433     21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24, 24,
    434     24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27,
    435     27, 28, 28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35, };
    436 
    437 static inline void DefaultScalingList4x4(
    438     int i,
    439     int scaling_list4x4[][kH264ScalingList4x4Length]) {
    440   DCHECK_LT(i, 6);
    441 
    442   if (i < 3)
    443     memcpy(scaling_list4x4[i], kDefault4x4Intra, sizeof(kDefault4x4Intra));
    444   else if (i < 6)
    445     memcpy(scaling_list4x4[i], kDefault4x4Inter, sizeof(kDefault4x4Inter));
    446 }
    447 
    448 static inline void DefaultScalingList8x8(
    449     int i,
    450     int scaling_list8x8[][kH264ScalingList8x8Length]) {
    451   DCHECK_LT(i, 6);
    452 
    453   if (i % 2 == 0)
    454     memcpy(scaling_list8x8[i], kDefault8x8Intra, sizeof(kDefault8x8Intra));
    455   else
    456     memcpy(scaling_list8x8[i], kDefault8x8Inter, sizeof(kDefault8x8Inter));
    457 }
    458 
    459 static void FallbackScalingList4x4(
    460     int i,
    461     const int default_scaling_list_intra[],
    462     const int default_scaling_list_inter[],
    463     int scaling_list4x4[][kH264ScalingList4x4Length]) {
    464   static const int kScalingList4x4ByteSize =
    465       sizeof(scaling_list4x4[0][0]) * kH264ScalingList4x4Length;
    466 
    467   switch (i) {
    468     case 0:
    469       memcpy(scaling_list4x4[i], default_scaling_list_intra,
    470              kScalingList4x4ByteSize);
    471       break;
    472 
    473     case 1:
    474       memcpy(scaling_list4x4[i], scaling_list4x4[0], kScalingList4x4ByteSize);
    475       break;
    476 
    477     case 2:
    478       memcpy(scaling_list4x4[i], scaling_list4x4[1], kScalingList4x4ByteSize);
    479       break;
    480 
    481     case 3:
    482       memcpy(scaling_list4x4[i], default_scaling_list_inter,
    483              kScalingList4x4ByteSize);
    484       break;
    485 
    486     case 4:
    487       memcpy(scaling_list4x4[i], scaling_list4x4[3], kScalingList4x4ByteSize);
    488       break;
    489 
    490     case 5:
    491       memcpy(scaling_list4x4[i], scaling_list4x4[4], kScalingList4x4ByteSize);
    492       break;
    493 
    494     default:
    495       NOTREACHED();
    496       break;
    497   }
    498 }
    499 
    500 static void FallbackScalingList8x8(
    501     int i,
    502     const int default_scaling_list_intra[],
    503     const int default_scaling_list_inter[],
    504     int scaling_list8x8[][kH264ScalingList8x8Length]) {
    505   static const int kScalingList8x8ByteSize =
    506       sizeof(scaling_list8x8[0][0]) * kH264ScalingList8x8Length;
    507 
    508   switch (i) {
    509     case 0:
    510       memcpy(scaling_list8x8[i], default_scaling_list_intra,
    511              kScalingList8x8ByteSize);
    512       break;
    513 
    514     case 1:
    515       memcpy(scaling_list8x8[i], default_scaling_list_inter,
    516              kScalingList8x8ByteSize);
    517       break;
    518 
    519     case 2:
    520       memcpy(scaling_list8x8[i], scaling_list8x8[0], kScalingList8x8ByteSize);
    521       break;
    522 
    523     case 3:
    524       memcpy(scaling_list8x8[i], scaling_list8x8[1], kScalingList8x8ByteSize);
    525       break;
    526 
    527     case 4:
    528       memcpy(scaling_list8x8[i], scaling_list8x8[2], kScalingList8x8ByteSize);
    529       break;
    530 
    531     case 5:
    532       memcpy(scaling_list8x8[i], scaling_list8x8[3], kScalingList8x8ByteSize);
    533       break;
    534 
    535     default:
    536       NOTREACHED();
    537       break;
    538   }
    539 }
    540 
    541 H264Parser::Result H264Parser::ParseScalingList(int size,
    542                                                 int* scaling_list,
    543                                                 bool* use_default) {
    544   // See chapter 7.3.2.1.1.1.
    545   int last_scale = 8;
    546   int next_scale = 8;
    547   int delta_scale;
    548 
    549   *use_default = false;
    550 
    551   for (int j = 0; j < size; ++j) {
    552     if (next_scale != 0) {
    553       READ_SE_OR_RETURN(&delta_scale);
    554       IN_RANGE_OR_RETURN(delta_scale, -128, 127);
    555       next_scale = (last_scale + delta_scale + 256) & 0xff;
    556 
    557       if (j == 0 && next_scale == 0) {
    558         *use_default = true;
    559         return kOk;
    560       }
    561     }
    562 
    563     scaling_list[j] = (next_scale == 0) ? last_scale : next_scale;
    564     last_scale = scaling_list[j];
    565   }
    566 
    567   return kOk;
    568 }
    569 
    570 H264Parser::Result H264Parser::ParseSPSScalingLists(H264SPS* sps) {
    571   // See 7.4.2.1.1.
    572   bool seq_scaling_list_present_flag;
    573   bool use_default;
    574   Result res;
    575 
    576   // Parse scaling_list4x4.
    577   for (int i = 0; i < 6; ++i) {
    578     READ_BOOL_OR_RETURN(&seq_scaling_list_present_flag);
    579 
    580     if (seq_scaling_list_present_flag) {
    581       res = ParseScalingList(arraysize(sps->scaling_list4x4[i]),
    582                              sps->scaling_list4x4[i],
    583                              &use_default);
    584       if (res != kOk)
    585         return res;
    586 
    587       if (use_default)
    588         DefaultScalingList4x4(i, sps->scaling_list4x4);
    589 
    590     } else {
    591       FallbackScalingList4x4(
    592           i, kDefault4x4Intra, kDefault4x4Inter, sps->scaling_list4x4);
    593     }
    594   }
    595 
    596   // Parse scaling_list8x8.
    597   for (int i = 0; i < ((sps->chroma_format_idc != 3) ? 2 : 6); ++i) {
    598     READ_BOOL_OR_RETURN(&seq_scaling_list_present_flag);
    599 
    600     if (seq_scaling_list_present_flag) {
    601       res = ParseScalingList(arraysize(sps->scaling_list8x8[i]),
    602                              sps->scaling_list8x8[i],
    603                              &use_default);
    604       if (res != kOk)
    605         return res;
    606 
    607       if (use_default)
    608         DefaultScalingList8x8(i, sps->scaling_list8x8);
    609 
    610     } else {
    611       FallbackScalingList8x8(
    612           i, kDefault8x8Intra, kDefault8x8Inter, sps->scaling_list8x8);
    613     }
    614   }
    615 
    616   return kOk;
    617 }
    618 
    619 H264Parser::Result H264Parser::ParsePPSScalingLists(const H264SPS& sps,
    620                                                     H264PPS* pps) {
    621   // See 7.4.2.2.
    622   bool pic_scaling_list_present_flag;
    623   bool use_default;
    624   Result res;
    625 
    626   for (int i = 0; i < 6; ++i) {
    627     READ_BOOL_OR_RETURN(&pic_scaling_list_present_flag);
    628 
    629     if (pic_scaling_list_present_flag) {
    630       res = ParseScalingList(arraysize(pps->scaling_list4x4[i]),
    631                              pps->scaling_list4x4[i],
    632                              &use_default);
    633       if (res != kOk)
    634         return res;
    635 
    636       if (use_default)
    637         DefaultScalingList4x4(i, pps->scaling_list4x4);
    638 
    639     } else {
    640       if (!sps.seq_scaling_matrix_present_flag) {
    641         // Table 7-2 fallback rule A in spec.
    642         FallbackScalingList4x4(
    643             i, kDefault4x4Intra, kDefault4x4Inter, pps->scaling_list4x4);
    644       } else {
    645         // Table 7-2 fallback rule B in spec.
    646         FallbackScalingList4x4(i,
    647                                sps.scaling_list4x4[0],
    648                                sps.scaling_list4x4[3],
    649                                pps->scaling_list4x4);
    650       }
    651     }
    652   }
    653 
    654   if (pps->transform_8x8_mode_flag) {
    655     for (int i = 0; i < ((sps.chroma_format_idc != 3) ? 2 : 6); ++i) {
    656       READ_BOOL_OR_RETURN(&pic_scaling_list_present_flag);
    657 
    658       if (pic_scaling_list_present_flag) {
    659         res = ParseScalingList(arraysize(pps->scaling_list8x8[i]),
    660                                pps->scaling_list8x8[i],
    661                                &use_default);
    662         if (res != kOk)
    663           return res;
    664 
    665         if (use_default)
    666           DefaultScalingList8x8(i, pps->scaling_list8x8);
    667 
    668       } else {
    669         if (!sps.seq_scaling_matrix_present_flag) {
    670           // Table 7-2 fallback rule A in spec.
    671           FallbackScalingList8x8(
    672               i, kDefault8x8Intra, kDefault8x8Inter, pps->scaling_list8x8);
    673         } else {
    674           // Table 7-2 fallback rule B in spec.
    675           FallbackScalingList8x8(i,
    676                                  sps.scaling_list8x8[0],
    677                                  sps.scaling_list8x8[1],
    678                                  pps->scaling_list8x8);
    679         }
    680       }
    681     }
    682   }
    683   return kOk;
    684 }
    685 
    686 H264Parser::Result H264Parser::ParseAndIgnoreHRDParameters(
    687     bool* hrd_parameters_present) {
    688   int data;
    689   READ_BOOL_OR_RETURN(&data);  // {nal,vcl}_hrd_parameters_present_flag
    690   if (!data)
    691     return kOk;
    692 
    693   *hrd_parameters_present = true;
    694 
    695   int cpb_cnt_minus1;
    696   READ_UE_OR_RETURN(&cpb_cnt_minus1);
    697   IN_RANGE_OR_RETURN(cpb_cnt_minus1, 0, 31);
    698   READ_BITS_OR_RETURN(8, &data);  // bit_rate_scale, cpb_size_scale
    699   for (int i = 0; i <= cpb_cnt_minus1; ++i) {
    700     READ_UE_OR_RETURN(&data);  // bit_rate_value_minus1[i]
    701     READ_UE_OR_RETURN(&data);  // cpb_size_value_minus1[i]
    702     READ_BOOL_OR_RETURN(&data);  // cbr_flag
    703   }
    704   READ_BITS_OR_RETURN(20, &data);  // cpb/dpb delays, etc.
    705 
    706   return kOk;
    707 }
    708 
    709 H264Parser::Result H264Parser::ParseVUIParameters(H264SPS* sps) {
    710   bool aspect_ratio_info_present_flag;
    711   READ_BOOL_OR_RETURN(&aspect_ratio_info_present_flag);
    712   if (aspect_ratio_info_present_flag) {
    713     int aspect_ratio_idc;
    714     READ_BITS_OR_RETURN(8, &aspect_ratio_idc);
    715     if (aspect_ratio_idc == H264SPS::kExtendedSar) {
    716       READ_BITS_OR_RETURN(16, &sps->sar_width);
    717       READ_BITS_OR_RETURN(16, &sps->sar_height);
    718     } else {
    719       const int max_aspect_ratio_idc = arraysize(kTableSarWidth) - 1;
    720       IN_RANGE_OR_RETURN(aspect_ratio_idc, 0, max_aspect_ratio_idc);
    721       sps->sar_width = kTableSarWidth[aspect_ratio_idc];
    722       sps->sar_height = kTableSarHeight[aspect_ratio_idc];
    723     }
    724   }
    725 
    726   int data;
    727   // Read and ignore overscan and video signal type info.
    728   READ_BOOL_OR_RETURN(&data);  // overscan_info_present_flag
    729   if (data)
    730     READ_BOOL_OR_RETURN(&data);  // overscan_appropriate_flag
    731 
    732   READ_BOOL_OR_RETURN(&sps->video_signal_type_present_flag);
    733   if (sps->video_signal_type_present_flag) {
    734     READ_BITS_OR_RETURN(3, &sps->video_format);
    735     READ_BOOL_OR_RETURN(&sps->video_full_range_flag);
    736     READ_BOOL_OR_RETURN(&sps->colour_description_present_flag);
    737     if (sps->colour_description_present_flag) {
    738       // color description syntax elements
    739       READ_BITS_OR_RETURN(8, &sps->colour_primaries);
    740       READ_BITS_OR_RETURN(8, &sps->transfer_characteristics);
    741       READ_BITS_OR_RETURN(8, &sps->matrix_coefficients);
    742     }
    743   }
    744 
    745   READ_BOOL_OR_RETURN(&data);  // chroma_loc_info_present_flag
    746   if (data) {
    747     READ_UE_OR_RETURN(&data);  // chroma_sample_loc_type_top_field
    748     READ_UE_OR_RETURN(&data);  // chroma_sample_loc_type_bottom_field
    749   }
    750 
    751   // Read and ignore timing info.
    752   READ_BOOL_OR_RETURN(&data);  // timing_info_present_flag
    753   if (data) {
    754     READ_BITS_OR_RETURN(16, &data);  // num_units_in_tick
    755     READ_BITS_OR_RETURN(16, &data);  // num_units_in_tick
    756     READ_BITS_OR_RETURN(16, &data);  // time_scale
    757     READ_BITS_OR_RETURN(16, &data);  // time_scale
    758     READ_BOOL_OR_RETURN(&data);  // fixed_frame_rate_flag
    759   }
    760 
    761   // Read and ignore NAL HRD parameters, if present.
    762   bool hrd_parameters_present = false;
    763   Result res = ParseAndIgnoreHRDParameters(&hrd_parameters_present);
    764   if (res != kOk)
    765     return res;
    766 
    767   // Read and ignore VCL HRD parameters, if present.
    768   res = ParseAndIgnoreHRDParameters(&hrd_parameters_present);
    769   if (res != kOk)
    770     return res;
    771 
    772   if (hrd_parameters_present)  // One of NAL or VCL params present is enough.
    773     READ_BOOL_OR_RETURN(&data);  // low_delay_hrd_flag
    774 
    775   READ_BOOL_OR_RETURN(&data);  // pic_struct_present_flag
    776   READ_BOOL_OR_RETURN(&sps->bitstream_restriction_flag);
    777   if (sps->bitstream_restriction_flag) {
    778     READ_BOOL_OR_RETURN(&data);  // motion_vectors_over_pic_boundaries_flag
    779     READ_UE_OR_RETURN(&data);  // max_bytes_per_pic_denom
    780     READ_UE_OR_RETURN(&data);  // max_bits_per_mb_denom
    781     READ_UE_OR_RETURN(&data);  // log2_max_mv_length_horizontal
    782     READ_UE_OR_RETURN(&data);  // log2_max_mv_length_vertical
    783     READ_UE_OR_RETURN(&sps->max_num_reorder_frames);
    784     READ_UE_OR_RETURN(&sps->max_dec_frame_buffering);
    785     TRUE_OR_RETURN(sps->max_dec_frame_buffering >= sps->max_num_ref_frames);
    786     IN_RANGE_OR_RETURN(
    787         sps->max_num_reorder_frames, 0, sps->max_dec_frame_buffering);
    788   }
    789 
    790   return kOk;
    791 }
    792 
    793 static void FillDefaultSeqScalingLists(H264SPS* sps) {
    794   for (int i = 0; i < 6; ++i)
    795     for (int j = 0; j < kH264ScalingList4x4Length; ++j)
    796       sps->scaling_list4x4[i][j] = 16;
    797 
    798   for (int i = 0; i < 6; ++i)
    799     for (int j = 0; j < kH264ScalingList8x8Length; ++j)
    800       sps->scaling_list8x8[i][j] = 16;
    801 }
    802 
    803 H264Parser::Result H264Parser::ParseSPS(int* sps_id) {
    804   // See 7.4.2.1.
    805   int data;
    806   Result res;
    807 
    808   *sps_id = -1;
    809 
    810   std::unique_ptr<H264SPS> sps(new H264SPS());
    811 
    812   READ_BITS_OR_RETURN(8, &sps->profile_idc);
    813   READ_BOOL_OR_RETURN(&sps->constraint_set0_flag);
    814   READ_BOOL_OR_RETURN(&sps->constraint_set1_flag);
    815   READ_BOOL_OR_RETURN(&sps->constraint_set2_flag);
    816   READ_BOOL_OR_RETURN(&sps->constraint_set3_flag);
    817   READ_BOOL_OR_RETURN(&sps->constraint_set4_flag);
    818   READ_BOOL_OR_RETURN(&sps->constraint_set5_flag);
    819   READ_BITS_OR_RETURN(2, &data);  // reserved_zero_2bits
    820   READ_BITS_OR_RETURN(8, &sps->level_idc);
    821   READ_UE_OR_RETURN(&sps->seq_parameter_set_id);
    822   TRUE_OR_RETURN(sps->seq_parameter_set_id < 32);
    823 
    824   if (sps->profile_idc == 100 || sps->profile_idc == 110 ||
    825       sps->profile_idc == 122 || sps->profile_idc == 244 ||
    826       sps->profile_idc == 44 || sps->profile_idc == 83 ||
    827       sps->profile_idc == 86 || sps->profile_idc == 118 ||
    828       sps->profile_idc == 128) {
    829     READ_UE_OR_RETURN(&sps->chroma_format_idc);
    830     TRUE_OR_RETURN(sps->chroma_format_idc < 4);
    831 
    832     if (sps->chroma_format_idc == 3)
    833       READ_BOOL_OR_RETURN(&sps->separate_colour_plane_flag);
    834 
    835     READ_UE_OR_RETURN(&sps->bit_depth_luma_minus8);
    836     TRUE_OR_RETURN(sps->bit_depth_luma_minus8 < 7);
    837 
    838     READ_UE_OR_RETURN(&sps->bit_depth_chroma_minus8);
    839     TRUE_OR_RETURN(sps->bit_depth_chroma_minus8 < 7);
    840 
    841     READ_BOOL_OR_RETURN(&sps->qpprime_y_zero_transform_bypass_flag);
    842     READ_BOOL_OR_RETURN(&sps->seq_scaling_matrix_present_flag);
    843 
    844     if (sps->seq_scaling_matrix_present_flag) {
    845       DVLOG(4) << "Scaling matrix present";
    846       res = ParseSPSScalingLists(sps.get());
    847       if (res != kOk)
    848         return res;
    849     } else {
    850       FillDefaultSeqScalingLists(sps.get());
    851     }
    852   } else {
    853     sps->chroma_format_idc = 1;
    854     FillDefaultSeqScalingLists(sps.get());
    855   }
    856 
    857   if (sps->separate_colour_plane_flag)
    858     sps->chroma_array_type = 0;
    859   else
    860     sps->chroma_array_type = sps->chroma_format_idc;
    861 
    862   READ_UE_OR_RETURN(&sps->log2_max_frame_num_minus4);
    863   TRUE_OR_RETURN(sps->log2_max_frame_num_minus4 < 13);
    864 
    865   READ_UE_OR_RETURN(&sps->pic_order_cnt_type);
    866   TRUE_OR_RETURN(sps->pic_order_cnt_type < 3);
    867 
    868   if (sps->pic_order_cnt_type == 0) {
    869     READ_UE_OR_RETURN(&sps->log2_max_pic_order_cnt_lsb_minus4);
    870     TRUE_OR_RETURN(sps->log2_max_pic_order_cnt_lsb_minus4 < 13);
    871     sps->expected_delta_per_pic_order_cnt_cycle = 0;
    872   } else if (sps->pic_order_cnt_type == 1) {
    873     READ_BOOL_OR_RETURN(&sps->delta_pic_order_always_zero_flag);
    874     READ_SE_OR_RETURN(&sps->offset_for_non_ref_pic);
    875     READ_SE_OR_RETURN(&sps->offset_for_top_to_bottom_field);
    876     READ_UE_OR_RETURN(&sps->num_ref_frames_in_pic_order_cnt_cycle);
    877     TRUE_OR_RETURN(sps->num_ref_frames_in_pic_order_cnt_cycle < 255);
    878 
    879     base::CheckedNumeric<int> offset_acc = 0;
    880     for (int i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; ++i) {
    881       READ_SE_OR_RETURN(&sps->offset_for_ref_frame[i]);
    882       offset_acc += sps->offset_for_ref_frame[i];
    883     }
    884     if (!offset_acc.IsValid())
    885       return kInvalidStream;
    886     sps->expected_delta_per_pic_order_cnt_cycle = offset_acc.ValueOrDefault(0);
    887   }
    888 
    889   READ_UE_OR_RETURN(&sps->max_num_ref_frames);
    890   READ_BOOL_OR_RETURN(&sps->gaps_in_frame_num_value_allowed_flag);
    891 
    892   READ_UE_OR_RETURN(&sps->pic_width_in_mbs_minus1);
    893   READ_UE_OR_RETURN(&sps->pic_height_in_map_units_minus1);
    894 
    895   READ_BOOL_OR_RETURN(&sps->frame_mbs_only_flag);
    896   if (!sps->frame_mbs_only_flag)
    897     READ_BOOL_OR_RETURN(&sps->mb_adaptive_frame_field_flag);
    898 
    899   READ_BOOL_OR_RETURN(&sps->direct_8x8_inference_flag);
    900 
    901   READ_BOOL_OR_RETURN(&sps->frame_cropping_flag);
    902   if (sps->frame_cropping_flag) {
    903     READ_UE_OR_RETURN(&sps->frame_crop_left_offset);
    904     READ_UE_OR_RETURN(&sps->frame_crop_right_offset);
    905     READ_UE_OR_RETURN(&sps->frame_crop_top_offset);
    906     READ_UE_OR_RETURN(&sps->frame_crop_bottom_offset);
    907   }
    908 
    909   READ_BOOL_OR_RETURN(&sps->vui_parameters_present_flag);
    910   if (sps->vui_parameters_present_flag) {
    911     DVLOG(4) << "VUI parameters present";
    912     res = ParseVUIParameters(sps.get());
    913     if (res != kOk)
    914       return res;
    915   }
    916 
    917   // If an SPS with the same id already exists, replace it.
    918   *sps_id = sps->seq_parameter_set_id;
    919   active_SPSes_[*sps_id] = std::move(sps);
    920 
    921   return kOk;
    922 }
    923 
    924 H264Parser::Result H264Parser::ParsePPS(int* pps_id) {
    925   // See 7.4.2.2.
    926   const H264SPS* sps;
    927   Result res;
    928 
    929   *pps_id = -1;
    930 
    931   std::unique_ptr<H264PPS> pps(new H264PPS());
    932 
    933   READ_UE_OR_RETURN(&pps->pic_parameter_set_id);
    934   READ_UE_OR_RETURN(&pps->seq_parameter_set_id);
    935   TRUE_OR_RETURN(pps->seq_parameter_set_id < 32);
    936 
    937   if (active_SPSes_.find(pps->seq_parameter_set_id) == active_SPSes_.end()) {
    938     DVLOG(1) << "Invalid stream, no SPS id: " << pps->seq_parameter_set_id;
    939     return kInvalidStream;
    940   }
    941 
    942   sps = GetSPS(pps->seq_parameter_set_id);
    943   TRUE_OR_RETURN(sps);
    944 
    945   READ_BOOL_OR_RETURN(&pps->entropy_coding_mode_flag);
    946   READ_BOOL_OR_RETURN(&pps->bottom_field_pic_order_in_frame_present_flag);
    947 
    948   READ_UE_OR_RETURN(&pps->num_slice_groups_minus1);
    949   if (pps->num_slice_groups_minus1 > 1) {
    950     DVLOG(1) << "Slice groups not supported";
    951     return kUnsupportedStream;
    952   }
    953 
    954   READ_UE_OR_RETURN(&pps->num_ref_idx_l0_default_active_minus1);
    955   TRUE_OR_RETURN(pps->num_ref_idx_l0_default_active_minus1 < 32);
    956 
    957   READ_UE_OR_RETURN(&pps->num_ref_idx_l1_default_active_minus1);
    958   TRUE_OR_RETURN(pps->num_ref_idx_l1_default_active_minus1 < 32);
    959 
    960   READ_BOOL_OR_RETURN(&pps->weighted_pred_flag);
    961   READ_BITS_OR_RETURN(2, &pps->weighted_bipred_idc);
    962   TRUE_OR_RETURN(pps->weighted_bipred_idc < 3);
    963 
    964   READ_SE_OR_RETURN(&pps->pic_init_qp_minus26);
    965   IN_RANGE_OR_RETURN(pps->pic_init_qp_minus26, -26, 25);
    966 
    967   READ_SE_OR_RETURN(&pps->pic_init_qs_minus26);
    968   IN_RANGE_OR_RETURN(pps->pic_init_qs_minus26, -26, 25);
    969 
    970   READ_SE_OR_RETURN(&pps->chroma_qp_index_offset);
    971   IN_RANGE_OR_RETURN(pps->chroma_qp_index_offset, -12, 12);
    972   pps->second_chroma_qp_index_offset = pps->chroma_qp_index_offset;
    973 
    974   READ_BOOL_OR_RETURN(&pps->deblocking_filter_control_present_flag);
    975   READ_BOOL_OR_RETURN(&pps->constrained_intra_pred_flag);
    976   READ_BOOL_OR_RETURN(&pps->redundant_pic_cnt_present_flag);
    977 
    978   if (br_.HasMoreRBSPData()) {
    979     READ_BOOL_OR_RETURN(&pps->transform_8x8_mode_flag);
    980     READ_BOOL_OR_RETURN(&pps->pic_scaling_matrix_present_flag);
    981 
    982     if (pps->pic_scaling_matrix_present_flag) {
    983       DVLOG(4) << "Picture scaling matrix present";
    984       res = ParsePPSScalingLists(*sps, pps.get());
    985       if (res != kOk)
    986         return res;
    987     }
    988 
    989     READ_SE_OR_RETURN(&pps->second_chroma_qp_index_offset);
    990   }
    991 
    992   // If a PPS with the same id already exists, replace it.
    993   *pps_id = pps->pic_parameter_set_id;
    994   active_PPSes_[*pps_id] = std::move(pps);
    995 
    996   return kOk;
    997 }
    998 
    999 H264Parser::Result H264Parser::ParseRefPicListModification(
   1000     int num_ref_idx_active_minus1,
   1001     H264ModificationOfPicNum* ref_list_mods) {
   1002   H264ModificationOfPicNum* pic_num_mod;
   1003 
   1004   if (num_ref_idx_active_minus1 >= 32)
   1005     return kInvalidStream;
   1006 
   1007   for (int i = 0; i < 32; ++i) {
   1008     pic_num_mod = &ref_list_mods[i];
   1009     READ_UE_OR_RETURN(&pic_num_mod->modification_of_pic_nums_idc);
   1010     TRUE_OR_RETURN(pic_num_mod->modification_of_pic_nums_idc < 4);
   1011 
   1012     switch (pic_num_mod->modification_of_pic_nums_idc) {
   1013       case 0:
   1014       case 1:
   1015         READ_UE_OR_RETURN(&pic_num_mod->abs_diff_pic_num_minus1);
   1016         break;
   1017 
   1018       case 2:
   1019         READ_UE_OR_RETURN(&pic_num_mod->long_term_pic_num);
   1020         break;
   1021 
   1022       case 3:
   1023         // Per spec, list cannot be empty.
   1024         if (i == 0)
   1025           return kInvalidStream;
   1026         return kOk;
   1027 
   1028       default:
   1029         return kInvalidStream;
   1030     }
   1031   }
   1032 
   1033   // If we got here, we didn't get loop end marker prematurely,
   1034   // so make sure it is there for our client.
   1035   int modification_of_pic_nums_idc;
   1036   READ_UE_OR_RETURN(&modification_of_pic_nums_idc);
   1037   TRUE_OR_RETURN(modification_of_pic_nums_idc == 3);
   1038 
   1039   return kOk;
   1040 }
   1041 
   1042 H264Parser::Result H264Parser::ParseRefPicListModifications(
   1043     H264SliceHeader* shdr) {
   1044   Result res;
   1045 
   1046   if (!shdr->IsISlice() && !shdr->IsSISlice()) {
   1047     READ_BOOL_OR_RETURN(&shdr->ref_pic_list_modification_flag_l0);
   1048     if (shdr->ref_pic_list_modification_flag_l0) {
   1049       res = ParseRefPicListModification(shdr->num_ref_idx_l0_active_minus1,
   1050                                         shdr->ref_list_l0_modifications);
   1051       if (res != kOk)
   1052         return res;
   1053     }
   1054   }
   1055 
   1056   if (shdr->IsBSlice()) {
   1057     READ_BOOL_OR_RETURN(&shdr->ref_pic_list_modification_flag_l1);
   1058     if (shdr->ref_pic_list_modification_flag_l1) {
   1059       res = ParseRefPicListModification(shdr->num_ref_idx_l1_active_minus1,
   1060                                         shdr->ref_list_l1_modifications);
   1061       if (res != kOk)
   1062         return res;
   1063     }
   1064   }
   1065 
   1066   return kOk;
   1067 }
   1068 
   1069 H264Parser::Result H264Parser::ParseWeightingFactors(
   1070     int num_ref_idx_active_minus1,
   1071     int chroma_array_type,
   1072     int luma_log2_weight_denom,
   1073     int chroma_log2_weight_denom,
   1074     H264WeightingFactors* w_facts) {
   1075 
   1076   int def_luma_weight = 1 << luma_log2_weight_denom;
   1077   int def_chroma_weight = 1 << chroma_log2_weight_denom;
   1078 
   1079   for (int i = 0; i < num_ref_idx_active_minus1 + 1; ++i) {
   1080     READ_BOOL_OR_RETURN(&w_facts->luma_weight_flag);
   1081     if (w_facts->luma_weight_flag) {
   1082       READ_SE_OR_RETURN(&w_facts->luma_weight[i]);
   1083       IN_RANGE_OR_RETURN(w_facts->luma_weight[i], -128, 127);
   1084 
   1085       READ_SE_OR_RETURN(&w_facts->luma_offset[i]);
   1086       IN_RANGE_OR_RETURN(w_facts->luma_offset[i], -128, 127);
   1087     } else {
   1088       w_facts->luma_weight[i] = def_luma_weight;
   1089       w_facts->luma_offset[i] = 0;
   1090     }
   1091 
   1092     if (chroma_array_type != 0) {
   1093       READ_BOOL_OR_RETURN(&w_facts->chroma_weight_flag);
   1094       if (w_facts->chroma_weight_flag) {
   1095         for (int j = 0; j < 2; ++j) {
   1096           READ_SE_OR_RETURN(&w_facts->chroma_weight[i][j]);
   1097           IN_RANGE_OR_RETURN(w_facts->chroma_weight[i][j], -128, 127);
   1098 
   1099           READ_SE_OR_RETURN(&w_facts->chroma_offset[i][j]);
   1100           IN_RANGE_OR_RETURN(w_facts->chroma_offset[i][j], -128, 127);
   1101         }
   1102       } else {
   1103         for (int j = 0; j < 2; ++j) {
   1104           w_facts->chroma_weight[i][j] = def_chroma_weight;
   1105           w_facts->chroma_offset[i][j] = 0;
   1106         }
   1107       }
   1108     }
   1109   }
   1110 
   1111   return kOk;
   1112 }
   1113 
   1114 H264Parser::Result H264Parser::ParsePredWeightTable(const H264SPS& sps,
   1115                                                     H264SliceHeader* shdr) {
   1116   READ_UE_OR_RETURN(&shdr->luma_log2_weight_denom);
   1117   TRUE_OR_RETURN(shdr->luma_log2_weight_denom < 8);
   1118 
   1119   if (sps.chroma_array_type != 0)
   1120     READ_UE_OR_RETURN(&shdr->chroma_log2_weight_denom);
   1121   TRUE_OR_RETURN(shdr->chroma_log2_weight_denom < 8);
   1122 
   1123   Result res = ParseWeightingFactors(shdr->num_ref_idx_l0_active_minus1,
   1124                                      sps.chroma_array_type,
   1125                                      shdr->luma_log2_weight_denom,
   1126                                      shdr->chroma_log2_weight_denom,
   1127                                      &shdr->pred_weight_table_l0);
   1128   if (res != kOk)
   1129     return res;
   1130 
   1131   if (shdr->IsBSlice()) {
   1132     res = ParseWeightingFactors(shdr->num_ref_idx_l1_active_minus1,
   1133                                 sps.chroma_array_type,
   1134                                 shdr->luma_log2_weight_denom,
   1135                                 shdr->chroma_log2_weight_denom,
   1136                                 &shdr->pred_weight_table_l1);
   1137     if (res != kOk)
   1138       return res;
   1139   }
   1140 
   1141   return kOk;
   1142 }
   1143 
   1144 H264Parser::Result H264Parser::ParseDecRefPicMarking(H264SliceHeader* shdr) {
   1145   size_t bits_left_at_start = br_.NumBitsLeft();
   1146 
   1147   if (shdr->idr_pic_flag) {
   1148     READ_BOOL_OR_RETURN(&shdr->no_output_of_prior_pics_flag);
   1149     READ_BOOL_OR_RETURN(&shdr->long_term_reference_flag);
   1150   } else {
   1151     READ_BOOL_OR_RETURN(&shdr->adaptive_ref_pic_marking_mode_flag);
   1152 
   1153     H264DecRefPicMarking* marking;
   1154     if (shdr->adaptive_ref_pic_marking_mode_flag) {
   1155       size_t i;
   1156       for (i = 0; i < arraysize(shdr->ref_pic_marking); ++i) {
   1157         marking = &shdr->ref_pic_marking[i];
   1158 
   1159         READ_UE_OR_RETURN(&marking->memory_mgmnt_control_operation);
   1160         if (marking->memory_mgmnt_control_operation == 0)
   1161           break;
   1162 
   1163         if (marking->memory_mgmnt_control_operation == 1 ||
   1164             marking->memory_mgmnt_control_operation == 3)
   1165           READ_UE_OR_RETURN(&marking->difference_of_pic_nums_minus1);
   1166 
   1167         if (marking->memory_mgmnt_control_operation == 2)
   1168           READ_UE_OR_RETURN(&marking->long_term_pic_num);
   1169 
   1170         if (marking->memory_mgmnt_control_operation == 3 ||
   1171             marking->memory_mgmnt_control_operation == 6)
   1172           READ_UE_OR_RETURN(&marking->long_term_frame_idx);
   1173 
   1174         if (marking->memory_mgmnt_control_operation == 4)
   1175           READ_UE_OR_RETURN(&marking->max_long_term_frame_idx_plus1);
   1176 
   1177         if (marking->memory_mgmnt_control_operation > 6)
   1178           return kInvalidStream;
   1179       }
   1180 
   1181       if (i == arraysize(shdr->ref_pic_marking)) {
   1182         DVLOG(1) << "Ran out of dec ref pic marking fields";
   1183         return kUnsupportedStream;
   1184       }
   1185     }
   1186   }
   1187 
   1188   shdr->dec_ref_pic_marking_bit_size = bits_left_at_start - br_.NumBitsLeft();
   1189   return kOk;
   1190 }
   1191 
   1192 H264Parser::Result H264Parser::ParseSliceHeader(const H264NALU& nalu,
   1193                                                 H264SliceHeader* shdr) {
   1194   // See 7.4.3.
   1195   const H264SPS* sps;
   1196   const H264PPS* pps;
   1197   Result res;
   1198 
   1199   memset(shdr, 0, sizeof(*shdr));
   1200 
   1201   shdr->idr_pic_flag = (nalu.nal_unit_type == 5);
   1202   shdr->nal_ref_idc = nalu.nal_ref_idc;
   1203   shdr->nalu_data = nalu.data;
   1204   shdr->nalu_size = nalu.size;
   1205 
   1206   READ_UE_OR_RETURN(&shdr->first_mb_in_slice);
   1207   READ_UE_OR_RETURN(&shdr->slice_type);
   1208   TRUE_OR_RETURN(shdr->slice_type < 10);
   1209 
   1210   READ_UE_OR_RETURN(&shdr->pic_parameter_set_id);
   1211 
   1212   pps = GetPPS(shdr->pic_parameter_set_id);
   1213   TRUE_OR_RETURN(pps);
   1214 
   1215   sps = GetSPS(pps->seq_parameter_set_id);
   1216   TRUE_OR_RETURN(sps);
   1217 
   1218   if (sps->separate_colour_plane_flag) {
   1219     DVLOG(1) << "Interlaced streams not supported";
   1220     return kUnsupportedStream;
   1221   }
   1222 
   1223   READ_BITS_OR_RETURN(sps->log2_max_frame_num_minus4 + 4, &shdr->frame_num);
   1224   if (!sps->frame_mbs_only_flag) {
   1225     READ_BOOL_OR_RETURN(&shdr->field_pic_flag);
   1226     if (shdr->field_pic_flag) {
   1227       DVLOG(1) << "Interlaced streams not supported";
   1228       return kUnsupportedStream;
   1229     }
   1230   }
   1231 
   1232   if (shdr->idr_pic_flag)
   1233     READ_UE_OR_RETURN(&shdr->idr_pic_id);
   1234 
   1235   size_t bits_left_at_pic_order_cnt_start = br_.NumBitsLeft();
   1236   if (sps->pic_order_cnt_type == 0) {
   1237     READ_BITS_OR_RETURN(sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
   1238                         &shdr->pic_order_cnt_lsb);
   1239     if (pps->bottom_field_pic_order_in_frame_present_flag &&
   1240         !shdr->field_pic_flag)
   1241       READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt_bottom);
   1242   }
   1243 
   1244   if (sps->pic_order_cnt_type == 1 && !sps->delta_pic_order_always_zero_flag) {
   1245     READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt0);
   1246     if (pps->bottom_field_pic_order_in_frame_present_flag &&
   1247         !shdr->field_pic_flag)
   1248       READ_SE_OR_RETURN(&shdr->delta_pic_order_cnt1);
   1249   }
   1250 
   1251   shdr->pic_order_cnt_bit_size =
   1252       bits_left_at_pic_order_cnt_start - br_.NumBitsLeft();
   1253 
   1254   if (pps->redundant_pic_cnt_present_flag) {
   1255     READ_UE_OR_RETURN(&shdr->redundant_pic_cnt);
   1256     TRUE_OR_RETURN(shdr->redundant_pic_cnt < 128);
   1257   }
   1258 
   1259   if (shdr->IsBSlice())
   1260     READ_BOOL_OR_RETURN(&shdr->direct_spatial_mv_pred_flag);
   1261 
   1262   if (shdr->IsPSlice() || shdr->IsSPSlice() || shdr->IsBSlice()) {
   1263     READ_BOOL_OR_RETURN(&shdr->num_ref_idx_active_override_flag);
   1264     if (shdr->num_ref_idx_active_override_flag) {
   1265       READ_UE_OR_RETURN(&shdr->num_ref_idx_l0_active_minus1);
   1266       if (shdr->IsBSlice())
   1267         READ_UE_OR_RETURN(&shdr->num_ref_idx_l1_active_minus1);
   1268     } else {
   1269       shdr->num_ref_idx_l0_active_minus1 =
   1270           pps->num_ref_idx_l0_default_active_minus1;
   1271       if (shdr->IsBSlice()) {
   1272         shdr->num_ref_idx_l1_active_minus1 =
   1273             pps->num_ref_idx_l1_default_active_minus1;
   1274       }
   1275     }
   1276   }
   1277   if (shdr->field_pic_flag) {
   1278     TRUE_OR_RETURN(shdr->num_ref_idx_l0_active_minus1 < 32);
   1279     TRUE_OR_RETURN(shdr->num_ref_idx_l1_active_minus1 < 32);
   1280   } else {
   1281     TRUE_OR_RETURN(shdr->num_ref_idx_l0_active_minus1 < 16);
   1282     TRUE_OR_RETURN(shdr->num_ref_idx_l1_active_minus1 < 16);
   1283   }
   1284 
   1285   if (nalu.nal_unit_type == H264NALU::kCodedSliceExtension) {
   1286     return kUnsupportedStream;
   1287   } else {
   1288     res = ParseRefPicListModifications(shdr);
   1289     if (res != kOk)
   1290       return res;
   1291   }
   1292 
   1293   if ((pps->weighted_pred_flag && (shdr->IsPSlice() || shdr->IsSPSlice())) ||
   1294       (pps->weighted_bipred_idc == 1 && shdr->IsBSlice())) {
   1295     res = ParsePredWeightTable(*sps, shdr);
   1296     if (res != kOk)
   1297       return res;
   1298   }
   1299 
   1300   if (nalu.nal_ref_idc != 0) {
   1301     res = ParseDecRefPicMarking(shdr);
   1302     if (res != kOk)
   1303       return res;
   1304   }
   1305 
   1306   if (pps->entropy_coding_mode_flag && !shdr->IsISlice() &&
   1307       !shdr->IsSISlice()) {
   1308     READ_UE_OR_RETURN(&shdr->cabac_init_idc);
   1309     TRUE_OR_RETURN(shdr->cabac_init_idc < 3);
   1310   }
   1311 
   1312   READ_SE_OR_RETURN(&shdr->slice_qp_delta);
   1313 
   1314   if (shdr->IsSPSlice() || shdr->IsSISlice()) {
   1315     if (shdr->IsSPSlice())
   1316       READ_BOOL_OR_RETURN(&shdr->sp_for_switch_flag);
   1317     READ_SE_OR_RETURN(&shdr->slice_qs_delta);
   1318   }
   1319 
   1320   if (pps->deblocking_filter_control_present_flag) {
   1321     READ_UE_OR_RETURN(&shdr->disable_deblocking_filter_idc);
   1322     TRUE_OR_RETURN(shdr->disable_deblocking_filter_idc < 3);
   1323 
   1324     if (shdr->disable_deblocking_filter_idc != 1) {
   1325       READ_SE_OR_RETURN(&shdr->slice_alpha_c0_offset_div2);
   1326       IN_RANGE_OR_RETURN(shdr->slice_alpha_c0_offset_div2, -6, 6);
   1327 
   1328       READ_SE_OR_RETURN(&shdr->slice_beta_offset_div2);
   1329       IN_RANGE_OR_RETURN(shdr->slice_beta_offset_div2, -6, 6);
   1330     }
   1331   }
   1332 
   1333   if (pps->num_slice_groups_minus1 > 0) {
   1334     DVLOG(1) << "Slice groups not supported";
   1335     return kUnsupportedStream;
   1336   }
   1337 
   1338   size_t epb = br_.NumEmulationPreventionBytesRead();
   1339   shdr->header_bit_size = (shdr->nalu_size - epb) * 8 - br_.NumBitsLeft();
   1340 
   1341   return kOk;
   1342 }
   1343 
   1344 H264Parser::Result H264Parser::ParseSEI(H264SEIMessage* sei_msg) {
   1345   int byte;
   1346 
   1347   memset(sei_msg, 0, sizeof(*sei_msg));
   1348 
   1349   READ_BITS_OR_RETURN(8, &byte);
   1350   while (byte == 0xff) {
   1351     sei_msg->type += 255;
   1352     READ_BITS_OR_RETURN(8, &byte);
   1353   }
   1354   sei_msg->type += byte;
   1355 
   1356   READ_BITS_OR_RETURN(8, &byte);
   1357   while (byte == 0xff) {
   1358     sei_msg->payload_size += 255;
   1359     READ_BITS_OR_RETURN(8, &byte);
   1360   }
   1361   sei_msg->payload_size += byte;
   1362 
   1363   DVLOG(4) << "Found SEI message type: " << sei_msg->type
   1364            << " payload size: " << sei_msg->payload_size;
   1365 
   1366   switch (sei_msg->type) {
   1367     case H264SEIMessage::kSEIRecoveryPoint:
   1368       READ_UE_OR_RETURN(&sei_msg->recovery_point.recovery_frame_cnt);
   1369       READ_BOOL_OR_RETURN(&sei_msg->recovery_point.exact_match_flag);
   1370       READ_BOOL_OR_RETURN(&sei_msg->recovery_point.broken_link_flag);
   1371       READ_BITS_OR_RETURN(2, &sei_msg->recovery_point.changing_slice_group_idc);
   1372       break;
   1373 
   1374     default:
   1375       DVLOG(4) << "Unsupported SEI message";
   1376       break;
   1377   }
   1378 
   1379   return kOk;
   1380 }
   1381 
   1382 }  // namespace media
   1383