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