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