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