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