Shaka Packager SDK
av1_parser.cc
1 // Copyright 2018 Google LLC. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/codecs/av1_parser.h"
8 
9 #include <algorithm>
10 
11 #include "packager/base/logging.h"
12 #include "packager/media/base/bit_reader.h"
13 #include "packager/media/base/rcheck.h"
14 
15 namespace shaka {
16 namespace media {
17 namespace {
18 
19 // 3. Symbols and abbreviated terms.
20 enum MotionType {
21  IDENTITY = 0,
22  TRANSLATION,
23  ROTZOOM,
24  AFFINE,
25 };
26 
27 const int kSelectScreenContentTools = 2;
28 const int kSelectIntegerMv = 2;
29 const int kPrimaryRefNone = 7;
30 const int kNumRefFrames = 8;
31 const int kAllFrames = (1 << kNumRefFrames) - 1;
32 
33 // 6.2.2. OBU header semantics.
34 enum ObuType {
35  OBU_SEQUENCE_HEADER = 1,
36  OBU_TEMPORAL_DELIMITER,
37  OBU_FRAME_HEADER,
38  OBU_TILE_GROUP,
39  OBU_METADATA,
40  OBU_FRAME,
41  OBU_REDUNDENT_FRAME_HEADER,
42  OBU_TILE_LIST,
43  // Reserved types between OBU_TILE_LIST and OBU_PADDING.
44  OBU_PADDING = 15,
45 };
46 
47 // 6.4.2. Color config semantics.
48 enum ColorPrimaries {
49  CP_BT_709 = 1,
50  CP_UNSPECIFIED = 2,
51  // We are not interested in the others.
52 };
53 enum TransferCharacteristics {
54  TC_UNSPECIFIED = 2,
55  TC_SRGB = 13,
56  // We are not interested in the others.
57 };
58 enum MatrixCoefficients {
59  MC_IDENTITY = 0,
60  MC_UNSPECIFIED = 2,
61  // We are not interested in the others.
62 };
63 enum ChromaSamplePosition {
64  CSP_UNKNOWN = 0,
65  CSP_VERTICAL,
66  CSP_COLOCATED,
67  CSP_RESERVED,
68 };
69 
70 // 6.8.2. Uncompressed header semantics.
71 enum FrameType {
72  KEY_FRAME = 0,
73  INTER_FRAME,
74  INTRA_ONLY_FRAME,
75  SWITCH_FRAME,
76 };
77 
78 // 6.10.24. Ref frames semantics.
79 enum RefFrameName {
80  INTRA_FRAME = 0,
81  LAST_FRAME,
82  LAST2_FRAME,
83  LAST3_FRAME,
84  GOLDEN_FRAME,
85  BWDREF_FRAME,
86  ALTREF2_FRAME,
87  ALTREF_FRAME,
88 };
89 
90 // 4.7. Mathematical functions.
91 int Clip3(int min_value, int max_value, int value) {
92  if (value < min_value)
93  return min_value;
94  if (value > max_value)
95  return max_value;
96  return value;
97 }
98 
99 // 4.7. Mathematical functions. The FloorLog2(x) function is defined to be the
100 // floor of the base 2 logarithm of the input x.
101 int FloorLog2(int x) {
102  int s = 0;
103  while (x != 0) {
104  x = x >> 1;
105  s++;
106  }
107  return s - 1;
108 }
109 
110 // 4.10.3. uvlc(). This is a modified form of Exponential-Golomb coding.
111 bool ReadUvlc(BitReader* reader, uint32_t* val) {
112  // Count the number of contiguous zero bits.
113  int leading_zeros = 0;
114  while (true) {
115  bool done = false;
116  RCHECK(reader->ReadBits(1, &done));
117  if (done)
118  break;
119  leading_zeros++;
120  }
121 
122  if (leading_zeros >= 32) {
123  *val = (1ull << 32) - 1;
124  return true;
125  }
126 
127  int value = 0;
128  if (leading_zeros > 0)
129  RCHECK(reader->ReadBits(leading_zeros, &value));
130 
131  *val = value + (1 << leading_zeros) - 1;
132  return true;
133 }
134 
135 // 4.10.4. le(n). Unsigned little-endian n-byte number appearing directly in the
136 // bitstream.
137 bool ReadLe(int n, BitReader* reader, size_t* val) {
138  size_t t = 0;
139  for (int i = 0; i < n; i++) {
140  size_t byte = 0;
141  RCHECK(reader->ReadBits(8, &byte));
142  t += (byte << (i * 8));
143  }
144  *val = t;
145  return true;
146 }
147 
148 // 4.10.5. leb128(). Unsigned integer represented by a variable number of
149 // little-endian bytes.
150 bool ReadLeb128(BitReader* reader, size_t* size) {
151  size_t value = 0;
152  for (int i = 0; i < 8; i++) {
153  size_t leb128_byte = 0;
154  RCHECK(reader->ReadBits(8, &leb128_byte));
155  value |= (leb128_byte & 0x7f) << (i * 7);
156  if (!(leb128_byte & 0x80))
157  break;
158  }
159  // It is a requirement of bitstream conformance that the value returned from
160  // the leb128 parsing process is less than or equal to (1<<32) - 1.
161  RCHECK(value <= ((1ull << 32) - 1));
162  *size = value;
163  return true;
164 }
165 
166 // 4.10.6. su(n). Signed integer converted from an n bits unsigned integer in
167 // the bitstream.
168 bool ReadSu(int n, BitReader* reader, int* value) {
169  RCHECK(reader->ReadBits(n, value));
170  int sign_mask = 1 << (n - 1);
171  if (*value & sign_mask)
172  *value = *value - 2 * sign_mask;
173  return true;
174 }
175 
176 // 4.10.7. ns(n). Unsigned encoded integer with maximum number of values in n
177 // (i.e. output in range 0..n-1).
178 bool ReadNs(int n, BitReader* reader, int* value) {
179  const int w = FloorLog2(n) + 1;
180  const int m = (1 << w) - n;
181  RCHECK(reader->ReadBits(w - 1, value));
182  if (*value < m)
183  return true;
184  int extra_bit = 0;
185  RCHECK(reader->ReadBits(1, &extra_bit));
186  *value = (*value << 1) - m + extra_bit;
187  return true;
188 }
189 
190 // 5.9.16. Tile size calculation function: returns the smallest value for k such
191 // that blk_size << k is greater than or equal to target.
192 int TileLog2(int blk_size, int target) {
193  int k = 0;
194  for (k = 0; (blk_size << k) < target; k++)
195  continue;
196  return k;
197 }
198 
199 // See 7.8. Set frame refs process.
200 int FindLatestBackward(int shifted_order_hints[],
201  bool used_frame[],
202  int cur_frame_hint) {
203  int ref = -1;
204  int latest_order_hint = 0;
205  for (int i = 0; i < kNumRefFrames; i++) {
206  const int hint = shifted_order_hints[i];
207  if (!used_frame[i] && hint >= cur_frame_hint &&
208  (ref < 0 || hint >= latest_order_hint)) {
209  ref = i;
210  latest_order_hint = hint;
211  }
212  }
213  return ref;
214 }
215 
216 // See 7.8. Set frame refs process.
217 int FindEarliestBackward(int shifted_order_hints[],
218  bool used_frame[],
219  int cur_frame_hint) {
220  int ref = -1;
221  int earliest_order_hint = 0;
222  for (int i = 0; i < kNumRefFrames; i++) {
223  const int hint = shifted_order_hints[i];
224  if (!used_frame[i] && hint >= cur_frame_hint &&
225  (ref < 0 || hint < earliest_order_hint)) {
226  ref = i;
227  earliest_order_hint = hint;
228  }
229  }
230  return ref;
231 }
232 
233 // See 7.8. Set frame refs process.
234 int FindLatestForward(int shifted_order_hints[],
235  bool used_frame[],
236  int cur_frame_hint) {
237  int ref = -1;
238  int latest_order_hint = 0;
239  for (int i = 0; i < kNumRefFrames; i++) {
240  const int hint = shifted_order_hints[i];
241  if (!used_frame[i] && hint < cur_frame_hint &&
242  (ref < 0 || hint >= latest_order_hint)) {
243  ref = i;
244  latest_order_hint = hint;
245  }
246  }
247  return ref;
248 }
249 
250 } // namespace
251 
252 AV1Parser::AV1Parser() = default;
253 AV1Parser::~AV1Parser() = default;
254 
255 bool AV1Parser::Parse(const uint8_t* data,
256  size_t data_size,
257  std::vector<Tile>* tiles) {
258  tiles->clear();
259 
260  BitReader reader(data, data_size);
261  while (reader.bits_available() > 0) {
262  if (!ParseOpenBitstreamUnit(&reader, tiles))
263  return false;
264  }
265  return true;
266 }
267 
268 // 5.3.1. General OBU syntax.
269 bool AV1Parser::ParseOpenBitstreamUnit(BitReader* reader,
270  std::vector<Tile>* tiles) {
271  ObuHeader obu_header;
272  RCHECK(ParseObuHeader(reader, &obu_header));
273 
274  size_t obu_size = 0;
275  if (obu_header.obu_has_size_field)
276  RCHECK(ReadLeb128(reader, &obu_size));
277  else
278  obu_size = reader->bits_available() / 8;
279 
280  VLOG(4) << "OBU " << obu_header.obu_type << " size " << obu_size;
281 
282  const size_t start_position = reader->bit_position();
283  switch (obu_header.obu_type) {
284  case OBU_SEQUENCE_HEADER:
285  RCHECK(ParseSequenceHeaderObu(reader));
286  break;
287  case OBU_FRAME_HEADER:
288  case OBU_REDUNDENT_FRAME_HEADER:
289  RCHECK(ParseFrameHeaderObu(obu_header, reader));
290  break;
291  case OBU_TILE_GROUP:
292  RCHECK(ParseTileGroupObu(obu_size, reader, tiles));
293  break;
294  case OBU_FRAME:
295  RCHECK(ParseFrameObu(obu_header, obu_size, reader, tiles));
296  break;
297  default:
298  // Skip all OBUs we are not interested.
299  RCHECK(reader->SkipBits(obu_size * 8));
300  break;
301  }
302 
303  const size_t current_position = reader->bit_position();
304  const size_t payload_bits = current_position - start_position;
305  if (obu_header.obu_type == OBU_TILE_GROUP ||
306  obu_header.obu_type == OBU_FRAME) {
307  RCHECK(payload_bits == obu_size * 8);
308  } else if (obu_size > 0) {
309  RCHECK(payload_bits <= obu_size * 8);
310  RCHECK(ParseTrailingBits(obu_size * 8 - payload_bits, reader));
311  }
312  return true;
313 }
314 
315 // 5.3.2. OBU header syntax.
316 bool AV1Parser::ParseObuHeader(BitReader* reader, ObuHeader* obu_header) {
317  int obu_forbidden_bit = 0;
318  RCHECK(reader->ReadBits(1, &obu_forbidden_bit));
319  RCHECK(obu_forbidden_bit == 0);
320  RCHECK(reader->ReadBits(4, &obu_header->obu_type));
321  bool obu_extension_flag = false;
322  RCHECK(reader->ReadBits(1, &obu_extension_flag));
323  RCHECK(reader->ReadBits(1, &obu_header->obu_has_size_field));
324  RCHECK(reader->SkipBits(1)); // Skip obu_reserved_1bit.
325 
326  if (obu_extension_flag)
327  RCHECK(ParseObuExtensionHeader(reader, &obu_header->extension_header));
328 
329  return true;
330 }
331 
332 // 5.3.3. OBU extension header syntax.
333 bool AV1Parser::ParseObuExtensionHeader(
334  BitReader* reader,
335  ObuExtensionHeader* obu_extension_header) {
336  RCHECK(reader->ReadBits(3, &obu_extension_header->temporal_id));
337  RCHECK(reader->ReadBits(2, &obu_extension_header->spatial_id));
338  RCHECK(reader->SkipBits(3)); // Skip extension_header_reserved_3bits.
339  return true;
340 }
341 
342 // 5.3.4. Trailing bits syntax.
343 bool AV1Parser::ParseTrailingBits(size_t nb_bits, BitReader* reader) {
344  int trailing_one_bit = 0;
345  RCHECK(reader->ReadBits(1, &trailing_one_bit));
346  RCHECK(trailing_one_bit == 1);
347  nb_bits--;
348  while (nb_bits > 0) {
349  int trailing_zero_bit = 0;
350  RCHECK(reader->ReadBits(1, &trailing_zero_bit));
351  RCHECK(trailing_zero_bit == 0);
352  nb_bits--;
353  }
354  return true;
355 }
356 
357 bool AV1Parser::ByteAlignment(BitReader* reader) {
358  while (reader->bit_position() & 7) {
359  int zero_bit = 0;
360  RCHECK(reader->ReadBits(1, &zero_bit));
361  RCHECK(zero_bit == 0);
362  }
363  return true;
364 }
365 
366 // 5.5.1. General sequence header OBU syntax.
367 bool AV1Parser::ParseSequenceHeaderObu(BitReader* reader) {
368  RCHECK(reader->ReadBits(3, &sequence_header_.seq_profile));
369  // Skip still_picture.
370  RCHECK(reader->SkipBits(1));
371 
372  RCHECK(reader->ReadBits(1, &sequence_header_.reduced_still_picture_header));
373  if (sequence_header_.reduced_still_picture_header) {
374  sequence_header_.decoder_model_info_present_flag = false;
375  sequence_header_.operating_points_cnt_minus_1 = 0;
376  sequence_header_.operating_point_idc[0] = 0;
377  // Skip seq_level_idx[0].
378  RCHECK(reader->SkipBits(5));
379  sequence_header_.decoder_model_present_for_this_op[0] = false;
380  } else {
381  bool timing_info_present_flag = false;
382  RCHECK(reader->ReadBits(1, &timing_info_present_flag));
383 
384  bool decoder_model_info_present_flag = false;
385  if (timing_info_present_flag) {
386  RCHECK(ParseTimingInfo(reader));
387  RCHECK(reader->ReadBits(1, &decoder_model_info_present_flag));
388  if (decoder_model_info_present_flag)
389  RCHECK(ParseDecoderModelInfo(reader));
390  }
391  sequence_header_.decoder_model_info_present_flag =
392  decoder_model_info_present_flag;
393 
394  bool initial_display_delay_present_flag = false;
395  RCHECK(reader->ReadBits(1, &initial_display_delay_present_flag));
396 
397  RCHECK(reader->ReadBits(5, &sequence_header_.operating_points_cnt_minus_1));
398  for (int i = 0; i <= sequence_header_.operating_points_cnt_minus_1; i++) {
399  RCHECK(reader->ReadBits(12, &sequence_header_.operating_point_idc[i]));
400  int seq_level_idx_i = 0;
401  RCHECK(reader->ReadBits(5, &seq_level_idx_i));
402  if (seq_level_idx_i > 7) {
403  // Skip seq_tier[i].
404  RCHECK(reader->SkipBits(1));
405  }
406 
407  if (sequence_header_.decoder_model_info_present_flag) {
408  RCHECK(reader->ReadBits(
409  1, &sequence_header_.decoder_model_present_for_this_op[i]));
410  if (sequence_header_.decoder_model_present_for_this_op[i]) {
411  RCHECK(SkipOperatingParametersInfo(reader));
412  }
413  } else {
414  sequence_header_.decoder_model_present_for_this_op[i] = false;
415  }
416 
417  if (initial_display_delay_present_flag) {
418  // Skip initial_display_delay_present_for_this_op[i],
419  // initial_display_delay_minus_1[i].
420  RCHECK(reader->SkipBitsConditional(true, 4));
421  }
422  }
423  }
424 
425  RCHECK(reader->ReadBits(4, &sequence_header_.frame_width_bits_minus_1));
426  RCHECK(reader->ReadBits(4, &sequence_header_.frame_height_bits_minus_1));
427  RCHECK(reader->ReadBits(sequence_header_.frame_width_bits_minus_1 + 1,
428  &sequence_header_.max_frame_width_minus_1));
429  RCHECK(reader->ReadBits(sequence_header_.frame_height_bits_minus_1 + 1,
430  &sequence_header_.max_frame_height_minus_1));
431 
432  if (sequence_header_.reduced_still_picture_header) {
433  sequence_header_.frame_id_numbers_present_flag = false;
434  } else {
435  RCHECK(
436  reader->ReadBits(1, &sequence_header_.frame_id_numbers_present_flag));
437  }
438  if (sequence_header_.frame_id_numbers_present_flag) {
439  RCHECK(
440  reader->ReadBits(4, &sequence_header_.delta_frame_id_length_minus_2));
441  RCHECK(reader->ReadBits(
442  3, &sequence_header_.additional_frame_id_length_minus_1));
443  }
444 
445  RCHECK(reader->ReadBits(1, &sequence_header_.use_128x128_superblock));
446  // Skip enable_filter_intra, enable_intra_edge_filter.
447  RCHECK(reader->SkipBits(1 + 1));
448 
449  if (sequence_header_.reduced_still_picture_header) {
450  sequence_header_.enable_warped_motion = false;
451  sequence_header_.enable_order_hint = false;
452  sequence_header_.enable_ref_frame_mvs = false;
453  sequence_header_.order_hint_bits = 0;
454  sequence_header_.seq_force_screen_content_tools = kSelectScreenContentTools;
455  sequence_header_.seq_force_integer_mv = kSelectIntegerMv;
456  } else {
457  // Skip enable_interintra_compound, enable_masked_compound,
458  RCHECK(reader->SkipBits(1 + 1));
459 
460  RCHECK(reader->ReadBits(1, &sequence_header_.enable_warped_motion));
461  RCHECK(reader->SkipBits(1)); // Skip enable_dual_filter.
462  RCHECK(reader->ReadBits(1, &sequence_header_.enable_order_hint));
463  if (sequence_header_.enable_order_hint) {
464  // Skip enable_jnt_comp.
465  RCHECK(reader->SkipBits(1));
466  RCHECK(reader->ReadBits(1, &sequence_header_.enable_ref_frame_mvs));
467  } else {
468  sequence_header_.enable_ref_frame_mvs = false;
469  }
470 
471  bool seq_choose_screen_content_tools = false;
472  RCHECK(reader->ReadBits(1, &seq_choose_screen_content_tools));
473 
474  if (seq_choose_screen_content_tools) {
475  sequence_header_.seq_force_screen_content_tools =
476  kSelectScreenContentTools;
477  } else {
478  RCHECK(reader->ReadBits(
479  1, &sequence_header_.seq_force_screen_content_tools));
480  }
481 
482  if (sequence_header_.seq_force_screen_content_tools > 0) {
483  bool seq_choose_integer_mv = false;
484  RCHECK(reader->ReadBits(1, &seq_choose_integer_mv));
485  if (seq_choose_integer_mv)
486  sequence_header_.seq_force_integer_mv = kSelectIntegerMv;
487  else
488  RCHECK(reader->ReadBits(1, &sequence_header_.seq_force_integer_mv));
489  } else {
490  sequence_header_.seq_force_integer_mv = kSelectIntegerMv;
491  }
492 
493  if (sequence_header_.enable_order_hint) {
494  int order_hint_bits_minus_1 = 0;
495  RCHECK(reader->ReadBits(3, &order_hint_bits_minus_1));
496  sequence_header_.order_hint_bits = order_hint_bits_minus_1 + 1;
497  } else {
498  sequence_header_.order_hint_bits = 0;
499  }
500  }
501 
502  RCHECK(reader->ReadBits(1, &sequence_header_.enable_superres));
503  RCHECK(reader->ReadBits(1, &sequence_header_.enable_cdef));
504  RCHECK(reader->ReadBits(1, &sequence_header_.enable_restoration));
505  RCHECK(ParseColorConfig(reader));
506  RCHECK(reader->ReadBits(1, &sequence_header_.film_grain_params_present));
507  return true;
508 }
509 
510 // 5.5.2. Color config syntax.
511 bool AV1Parser::ParseColorConfig(BitReader* reader) {
512  ColorConfig& color_config = sequence_header_.color_config;
513 
514  bool high_bitdepth = false;
515  RCHECK(reader->ReadBits(1, &high_bitdepth));
516  if (sequence_header_.seq_profile == 2 && high_bitdepth) {
517  bool twelve_bit = false;
518  RCHECK(reader->ReadBits(1, &twelve_bit));
519  color_config.bit_depth = twelve_bit ? 12 : 10;
520  } else if (sequence_header_.seq_profile <= 2) {
521  color_config.bit_depth = high_bitdepth ? 10 : 8;
522  }
523 
524  if (sequence_header_.seq_profile == 1)
525  color_config.mono_chrome = 0;
526  else
527  RCHECK(reader->ReadBits(1, &color_config.mono_chrome));
528  color_config.num_planes = color_config.mono_chrome ? 1 : 3;
529 
530  bool color_description_present_flag = false;
531  RCHECK(reader->ReadBits(1, &color_description_present_flag));
532 
533  if (color_description_present_flag) {
534  RCHECK(reader->ReadBits(8, &color_config.color_primaries));
535  RCHECK(reader->ReadBits(8, &color_config.transfer_chracteristics));
536  RCHECK(reader->ReadBits(8, &color_config.matrix_coefficients));
537  } else {
538  color_config.color_primaries = CP_UNSPECIFIED;
539  color_config.transfer_chracteristics = TC_UNSPECIFIED;
540  color_config.matrix_coefficients = MC_UNSPECIFIED;
541  }
542 
543  if (color_config.mono_chrome) {
544  RCHECK(reader->ReadBits(1, &color_config.color_range));
545  color_config.subsampling_x = true;
546  color_config.subsampling_y = true;
547  color_config.chroma_sampling_position = CSP_UNKNOWN;
548  color_config.separate_uv_delta_q = false;
549  return true;
550  } else if (color_config.color_primaries == CP_BT_709 &&
551  color_config.transfer_chracteristics == TC_SRGB &&
552  color_config.matrix_coefficients == MC_IDENTITY) {
553  color_config.color_range = true;
554  color_config.subsampling_x = false;
555  color_config.subsampling_y = false;
556  } else {
557  RCHECK(reader->ReadBits(1, &color_config.color_range));
558  if (sequence_header_.seq_profile == 0) {
559  color_config.subsampling_x = true;
560  color_config.subsampling_y = true;
561  } else if (sequence_header_.seq_profile == 1) {
562  color_config.subsampling_x = false;
563  color_config.subsampling_y = false;
564  } else {
565  if (color_config.bit_depth == 12) {
566  RCHECK(reader->ReadBits(1, &color_config.subsampling_x));
567  if (color_config.subsampling_x)
568  RCHECK(reader->ReadBits(1, &color_config.subsampling_y));
569  else
570  color_config.subsampling_y = false;
571  } else {
572  color_config.subsampling_x = true;
573  color_config.subsampling_y = false;
574  }
575  }
576 
577  if (color_config.subsampling_x && color_config.subsampling_y)
578  RCHECK(reader->ReadBits(2, &color_config.chroma_sampling_position));
579  }
580 
581  RCHECK(reader->ReadBits(1, &color_config.separate_uv_delta_q));
582  return true;
583 }
584 
585 // 5.5.3.Timing info syntax.
586 bool AV1Parser::ParseTimingInfo(BitReader* reader) {
587  // Skip num_units_in_display_tick, time_scale.
588  RCHECK(reader->SkipBits(32 + 32));
589  bool equal_picture_interval = false;
590  RCHECK(reader->ReadBits(1, &equal_picture_interval));
591  sequence_header_.timing_info.equal_picture_interval = equal_picture_interval;
592  if (equal_picture_interval) {
593  uint32_t num_ticks_per_picture_minus_1 = 0;
594  RCHECK(ReadUvlc(reader, &num_ticks_per_picture_minus_1));
595  }
596  return true;
597 }
598 
599 // 5.5.4. Decoder model info syntax.
600 bool AV1Parser::ParseDecoderModelInfo(BitReader* reader) {
601  DecoderModelInfo& decoder_model_info = sequence_header_.decoder_model_info;
602 
603  RCHECK(reader->ReadBits(5, &decoder_model_info.buffer_delay_length_minus_1));
604  // Skip num_units_in_decoding_tick.
605  RCHECK(reader->SkipBits(32));
606  RCHECK(reader->ReadBits(
607  5, &decoder_model_info.buffer_removal_time_length_minus_1));
608  RCHECK(reader->ReadBits(
609  5, &decoder_model_info.frame_presentation_time_length_minus_1));
610  return true;
611 }
612 
613 // 5.5.5. Operating parameters info syntax.
614 bool AV1Parser::SkipOperatingParametersInfo(BitReader* reader) {
615  const int n =
616  sequence_header_.decoder_model_info.buffer_delay_length_minus_1 + 1;
617  // Skip decoder_buffer_delay[op], encoder_buffer_delay[op],
618  // low_delay_mode_flag[op].
619  RCHECK(reader->SkipBits(n + n + 1));
620  return true;
621 }
622 
623 // 5.9.1. General frame header OBU syntax.
624 bool AV1Parser::ParseFrameHeaderObu(const ObuHeader& obu_header,
625  BitReader* reader) {
626  if (frame_header_.seen_frame_header)
627  return true;
628 
629  frame_header_.seen_frame_header = true;
630  RCHECK(ParseUncompressedHeader(obu_header, reader));
631  if (frame_header_.show_existing_frame) {
632  DecodeFrameWrapup();
633  frame_header_.seen_frame_header = false;
634  } else {
635  frame_header_.seen_frame_header = true;
636  }
637  return true;
638 }
639 
640 // 5.9.2. Uncompressed header syntax.
641 bool AV1Parser::ParseUncompressedHeader(const ObuHeader& obu_header,
642  BitReader* reader) {
643  int id_len = 0;
644  if (sequence_header_.frame_id_numbers_present_flag) {
645  id_len = sequence_header_.additional_frame_id_length_minus_1 + 1 +
646  sequence_header_.delta_frame_id_length_minus_2 + 2;
647  }
648 
649  bool frame_is_intra = false;
650  bool show_frame = false;
651  bool showable_frame = false;
652  bool error_resilient_mode = false;
653 
654  if (sequence_header_.reduced_still_picture_header) {
655  frame_header_.show_existing_frame = false;
656  frame_header_.frame_type = KEY_FRAME;
657  frame_is_intra = true;
658  show_frame = true;
659  showable_frame = false;
660  } else {
661  RCHECK(reader->ReadBits(1, &frame_header_.show_existing_frame));
662  if (frame_header_.show_existing_frame) {
663  RCHECK(reader->ReadBits(3, &frame_header_.frame_to_show_map_idx));
664  if (sequence_header_.decoder_model_info_present_flag &&
665  !sequence_header_.timing_info.equal_picture_interval) {
666  RCHECK(SkipTemporalPointInfo(reader));
667  }
668  frame_header_.refresh_frame_flags = 0;
669  if (sequence_header_.frame_id_numbers_present_flag) {
670  // Skip display_frame_id.
671  RCHECK(reader->SkipBits(id_len));
672  }
673  frame_header_.frame_type =
674  reference_frames_[frame_header_.frame_to_show_map_idx].frame_type;
675  if (frame_header_.frame_type == KEY_FRAME) {
676  frame_header_.refresh_frame_flags = kAllFrames;
677  }
678  return true;
679  }
680 
681  RCHECK(reader->ReadBits(2, &frame_header_.frame_type));
682  frame_is_intra = frame_header_.frame_type == INTRA_ONLY_FRAME ||
683  frame_header_.frame_type == KEY_FRAME;
684  RCHECK(reader->ReadBits(1, &show_frame));
685  if (show_frame && sequence_header_.decoder_model_info_present_flag &&
686  !sequence_header_.timing_info.equal_picture_interval) {
687  RCHECK(SkipTemporalPointInfo(reader));
688  }
689  if (show_frame)
690  showable_frame = frame_header_.frame_type != KEY_FRAME;
691  else
692  RCHECK(reader->ReadBits(1, &showable_frame));
693 
694  if (frame_header_.frame_type == SWITCH_FRAME ||
695  (frame_header_.frame_type == KEY_FRAME && show_frame)) {
696  error_resilient_mode = true;
697  } else {
698  RCHECK(reader->ReadBits(1, &error_resilient_mode));
699  }
700  }
701 
702  if (frame_header_.frame_type == KEY_FRAME && show_frame) {
703  for (int i = 0; i < kNumRefFrames; i++) {
704  reference_frames_[i].order_hint = 0;
705  }
706  }
707 
708  bool disable_cdf_update = false;
709  RCHECK(reader->ReadBits(1, &disable_cdf_update));
710 
711  bool allow_screen_content_tools = false;
712  if (sequence_header_.seq_force_screen_content_tools ==
713  kSelectScreenContentTools) {
714  RCHECK(reader->ReadBits(1, &allow_screen_content_tools));
715  } else {
716  allow_screen_content_tools =
717  sequence_header_.seq_force_screen_content_tools != 0;
718  }
719 
720  int force_integer_mv = 0;
721  if (allow_screen_content_tools) {
722  if (sequence_header_.seq_force_integer_mv == kSelectIntegerMv)
723  RCHECK(reader->ReadBits(1, &force_integer_mv));
724  else
725  force_integer_mv = sequence_header_.seq_force_integer_mv;
726  }
727  if (frame_is_intra)
728  force_integer_mv = 1;
729 
730  if (sequence_header_.frame_id_numbers_present_flag) {
731  // Skip current_frame_id.
732  RCHECK(reader->SkipBits(id_len));
733  }
734 
735  bool frame_size_override_flag = false;
736  if (frame_header_.frame_type == SWITCH_FRAME)
737  frame_size_override_flag = true;
738  else if (sequence_header_.reduced_still_picture_header)
739  frame_size_override_flag = false;
740  else
741  RCHECK(reader->ReadBits(1, &frame_size_override_flag));
742 
743  RCHECK(reader->ReadBits(sequence_header_.order_hint_bits,
744  &frame_header_.order_hint));
745  int primary_ref_frame = 0;
746  if (frame_is_intra || error_resilient_mode) {
747  primary_ref_frame = kPrimaryRefNone;
748  } else {
749  RCHECK(reader->ReadBits(3, &primary_ref_frame));
750  }
751  if (sequence_header_.decoder_model_info_present_flag) {
752  bool buffer_removal_time_present_flag = false;
753  RCHECK(reader->ReadBits(1, &buffer_removal_time_present_flag));
754  if (buffer_removal_time_present_flag) {
755  for (int op_num = 0;
756  op_num <= sequence_header_.operating_points_cnt_minus_1; op_num++) {
757  if (sequence_header_.decoder_model_present_for_this_op[op_num]) {
758  const int op_pt_idc = sequence_header_.operating_point_idc[op_num];
759  const int in_temporal_layer =
760  (op_pt_idc >> obu_header.extension_header.temporal_id) & 1;
761  const int in_spatial_layer =
762  (op_pt_idc >> (obu_header.extension_header.spatial_id + 8)) & 1;
763  if (op_pt_idc == 0 || (in_temporal_layer && in_spatial_layer)) {
764  // Skip buffer_removal_time[ opNum ].
765  RCHECK(reader->SkipBits(sequence_header_.decoder_model_info
766  .buffer_removal_time_length_minus_1 +
767  1));
768  }
769  }
770  }
771  }
772  }
773 
774  bool allow_high_precision_mv = false;
775  bool allow_intrabc = false;
776 
777  if (frame_header_.frame_type == SWITCH_FRAME ||
778  (frame_header_.frame_type == KEY_FRAME && show_frame)) {
779  frame_header_.refresh_frame_flags = kAllFrames;
780  } else {
781  RCHECK(reader->ReadBits(8, &frame_header_.refresh_frame_flags));
782  }
783  if (!frame_is_intra || frame_header_.refresh_frame_flags != kAllFrames) {
784  if (error_resilient_mode && sequence_header_.enable_order_hint) {
785  for (int i = 0; i < kNumRefFrames; i++) {
786  // Skip ref_order_hint[ i ].
787  RCHECK(reader->SkipBits(sequence_header_.order_hint_bits));
788  }
789  }
790  }
791 
792  if (frame_is_intra) {
793  RCHECK(ParseFrameSize(frame_size_override_flag, reader));
794  RCHECK(ParseRenderSize(reader));
795  if (allow_screen_content_tools &&
796  frame_header_.upscaled_width == frame_header_.frame_width)
797  RCHECK(reader->ReadBits(1, &allow_intrabc));
798  } else {
799  bool frame_refs_short_signaling = false;
800  if (sequence_header_.enable_order_hint) {
801  RCHECK(reader->ReadBits(1, &frame_refs_short_signaling));
802  if (frame_refs_short_signaling) {
803  int last_frame_idx = 0;
804  RCHECK(reader->ReadBits(3, &last_frame_idx));
805  int gold_frame_idx = 0;
806  RCHECK(reader->ReadBits(3, &gold_frame_idx));
807  RCHECK(SetFrameRefs(last_frame_idx, gold_frame_idx));
808  }
809  }
810  for (int i = 0; i < kRefsPerFrame; i++) {
811  if (!frame_refs_short_signaling)
812  RCHECK(reader->ReadBits(3, &frame_header_.ref_frame_idx[i]));
813  if (sequence_header_.frame_id_numbers_present_flag) {
814  // Skip delta_frame_id_minus_1.
815  RCHECK(reader->SkipBits(sequence_header_.delta_frame_id_length_minus_2 +
816  2));
817  }
818  }
819  if (frame_size_override_flag && !error_resilient_mode) {
820  RCHECK(ParseFrameSizeWithRefs(frame_size_override_flag, reader));
821  } else {
822  RCHECK(ParseFrameSize(frame_size_override_flag, reader));
823  RCHECK(ParseRenderSize(reader));
824  }
825 
826  if (force_integer_mv)
827  allow_high_precision_mv = false;
828  else
829  RCHECK(reader->ReadBits(1, &allow_high_precision_mv));
830 
831  RCHECK(SkipInterpolationFilter(reader));
832  // Skip is_motion_mode_switchable.
833  RCHECK(reader->SkipBits(1));
834  if (!error_resilient_mode && sequence_header_.enable_ref_frame_mvs) {
835  // Skip use_ref_frame_mvs.
836  RCHECK(reader->SkipBits(1));
837  }
838  }
839 
840  if (!sequence_header_.reduced_still_picture_header && !disable_cdf_update) {
841  // Skip disable_frame_end_update_cdf.
842  RCHECK(reader->SkipBits(1));
843  }
844 
845  RCHECK(ParseTileInfo(reader));
846  RCHECK(ParseQuantizationParams(reader));
847  RCHECK(ParseSegmentationParams(primary_ref_frame, reader));
848 
849  bool delta_q_present = false;
850  RCHECK(SkipDeltaQParams(reader, &delta_q_present));
851  RCHECK(SkipDeltaLfParams(delta_q_present, allow_intrabc, reader));
852 
853  const auto& quantization_params = frame_header_.quantization_params;
854  bool coded_lossless = true;
855  for (int segment_id = 0; segment_id < kMaxSegments; segment_id++) {
856  const int qindex = GetQIndex(true, segment_id);
857  const bool lossless = qindex == 0 && quantization_params.delta_qydc == 0 &&
858  quantization_params.delta_quac == 0 &&
859  quantization_params.delta_qudc == 0 &&
860  quantization_params.delta_qvac == 0 &&
861  quantization_params.delta_qvdc == 0;
862  if (!lossless)
863  coded_lossless = false;
864  }
865  const bool all_lossless = coded_lossless && (frame_header_.frame_width ==
866  frame_header_.upscaled_width);
867 
868  RCHECK(ParseLoopFilterParams(coded_lossless, allow_intrabc, reader));
869  RCHECK(ParseCdefParams(coded_lossless, allow_intrabc, reader));
870  RCHECK(ParseLrParams(all_lossless, allow_intrabc, reader));
871  RCHECK(SkipTxMode(coded_lossless, reader));
872  bool reference_select = false;
873  RCHECK(ParseFrameReferenceMode(frame_is_intra, reader, &reference_select));
874  RCHECK(SkipSkipModeParams(frame_is_intra, reference_select, reader));
875 
876  bool allow_warped_motion = false;
877  if (frame_is_intra || error_resilient_mode ||
878  !sequence_header_.enable_warped_motion) {
879  allow_warped_motion = false;
880  } else {
881  RCHECK(reader->ReadBits(1, &allow_warped_motion));
882  }
883  // Skip reduced_tx_set.
884  RCHECK(reader->SkipBits(1));
885 
886  RCHECK(
887  SkipGlobalMotionParams(frame_is_intra, allow_high_precision_mv, reader));
888  RCHECK(SkipFilmGrainParams(show_frame, showable_frame, reader));
889  return true;
890 }
891 
892 // 5.9.3. Get relative distance function.
893 int AV1Parser::GetRelativeDist(int a, int b) {
894  if (!sequence_header_.enable_order_hint)
895  return 0;
896  int diff = a - b;
897  const int m = 1 << (sequence_header_.order_hint_bits - 1);
898  diff = (diff & (m - 1)) - (diff & m);
899  return diff;
900 }
901 
902 // 5.9.5. Frame size syntax.
903 bool AV1Parser::ParseFrameSize(bool frame_size_override_flag,
904  BitReader* reader) {
905  if (frame_size_override_flag) {
906  int frame_width_minus_1 = 0;
907  RCHECK(reader->ReadBits(sequence_header_.frame_width_bits_minus_1 + 1,
908  &frame_width_minus_1));
909  int frame_height_minus_1 = 0;
910  RCHECK(reader->ReadBits(sequence_header_.frame_height_bits_minus_1 + 1,
911  &frame_height_minus_1));
912  frame_header_.frame_width = frame_width_minus_1 + 1;
913  frame_header_.frame_height = frame_height_minus_1 + 1;
914  } else {
915  frame_header_.frame_width = sequence_header_.max_frame_width_minus_1 + 1;
916  frame_header_.frame_height = sequence_header_.max_frame_height_minus_1 + 1;
917  }
918  RCHECK(ParseSuperresParams(reader));
919  ComputeImageSize();
920  return true;
921 }
922 
923 // 5.9.6. Render size syntax.
924 bool AV1Parser::ParseRenderSize(BitReader* reader) {
925  bool render_and_frame_size_different = false;
926  RCHECK(reader->ReadBits(1, &render_and_frame_size_different));
927  if (render_and_frame_size_different) {
928  int render_width_minus_1 = 0;
929  RCHECK(reader->ReadBits(16, &render_width_minus_1));
930  int render_height_minus_1 = 0;
931  RCHECK(reader->ReadBits(16, &render_height_minus_1));
932  frame_header_.render_width = render_width_minus_1 + 1;
933  frame_header_.render_height = render_height_minus_1 + 1;
934  } else {
935  frame_header_.render_width = frame_header_.upscaled_width;
936  frame_header_.render_height = frame_header_.frame_height;
937  }
938  return true;
939 }
940 
941 // 5.9.7. Frame size with refs syntax.
942 bool AV1Parser::ParseFrameSizeWithRefs(bool frame_size_override_flag,
943  BitReader* reader) {
944  bool found_ref = false;
945  for (int i = 0; i < kRefsPerFrame; i++) {
946  RCHECK(reader->ReadBits(1, &found_ref));
947  if (found_ref) {
948  const ReferenceFrame& reference_frame =
949  reference_frames_[frame_header_.ref_frame_idx[i]];
950  frame_header_.upscaled_width = reference_frame.upscaled_width;
951  frame_header_.frame_width = frame_header_.upscaled_width;
952  frame_header_.frame_height = reference_frame.frame_height;
953  frame_header_.render_width = reference_frame.render_width;
954  frame_header_.render_height = reference_frame.render_height;
955  break;
956  }
957  }
958  if (!found_ref) {
959  RCHECK(ParseFrameSize(frame_size_override_flag, reader));
960  RCHECK(ParseRenderSize(reader));
961  } else {
962  RCHECK(ParseSuperresParams(reader));
963  ComputeImageSize();
964  }
965  return true;
966 }
967 
968 // 5.9.8. Superres params syntax.
969 bool AV1Parser::ParseSuperresParams(BitReader* reader) {
970  const int kSuperresNum = 8;
971  const int kSuperresDenomMin = 9;
972  const int kSuperresDenomBits = 3;
973 
974  bool use_superres = false;
975  if (sequence_header_.enable_superres)
976  RCHECK(reader->ReadBits(1, &use_superres));
977 
978  int superres_denom = 0;
979  if (use_superres) {
980  int coded_denom = 0;
981  RCHECK(reader->ReadBits(kSuperresDenomBits, &coded_denom));
982  superres_denom = coded_denom + kSuperresDenomMin;
983  } else {
984  superres_denom = kSuperresNum;
985  }
986 
987  const int upscaled_width = frame_header_.frame_width;
988  frame_header_.upscaled_width =
989  (upscaled_width * kSuperresNum + superres_denom / 2) / superres_denom;
990  return true;
991 }
992 
993 // 5.9.9. Compute image size function.
994 void AV1Parser::ComputeImageSize() {
995  frame_header_.mi_cols = 2 * ((frame_header_.frame_width + 7) >> 3);
996  frame_header_.mi_rows = 2 * ((frame_header_.frame_height + 7) >> 3);
997 }
998 
999 // 5.9.10. Interpolation filter syntax.
1000 bool AV1Parser::SkipInterpolationFilter(BitReader* reader) {
1001  // SKip is_filter_switchable, interpolation_filter.
1002  RCHECK(reader->SkipBitsConditional(false, 2));
1003  return true;
1004 }
1005 
1006 // 5.9.11. Loop filter parms syntax.
1007 bool AV1Parser::ParseLoopFilterParams(bool coded_lossless,
1008  bool allow_intrabc,
1009  BitReader* reader) {
1010  if (coded_lossless || allow_intrabc)
1011  return true;
1012 
1013  int loop_filter_level[] = {0, 0};
1014  RCHECK(reader->ReadBits(6, &loop_filter_level[0]));
1015  RCHECK(reader->ReadBits(6, &loop_filter_level[1]));
1016  if (sequence_header_.color_config.num_planes > 1) {
1017  if (loop_filter_level[0] || loop_filter_level[1]) {
1018  // Skip loop_filter_level[2], loop_filter_level[3].
1019  RCHECK(reader->SkipBits(6 + 6));
1020  }
1021  }
1022  // Skip loop_filter_sharpness.
1023  RCHECK(reader->SkipBits(3));
1024  bool loop_filter_delta_enabled = false;
1025  RCHECK(reader->ReadBits(1, &loop_filter_delta_enabled));
1026  if (loop_filter_delta_enabled) {
1027  bool loop_filter_delta_update = false;
1028  RCHECK(reader->ReadBits(1, &loop_filter_delta_update));
1029  if (loop_filter_delta_update) {
1030  const int kTotalRefsPerFrame = 8;
1031  for (int i = 0; i < kTotalRefsPerFrame; i++) {
1032  // Skip update_ref_delta, loop_filter_ref_delta[ i ].
1033  RCHECK(reader->SkipBitsConditional(true, 1 + 6));
1034  }
1035  for (int i = 0; i < 2; i++) {
1036  // Skip update_mode_delta, loop_filter_mode_delta[ i ].
1037  RCHECK(reader->SkipBitsConditional(true, 1 + 6));
1038  }
1039  }
1040  }
1041  return true;
1042 }
1043 
1044 // 5.9.12. Quantization params syntax.
1045 bool AV1Parser::ParseQuantizationParams(BitReader* reader) {
1046  QuantizationParams& quantization_params = frame_header_.quantization_params;
1047 
1048  RCHECK(reader->ReadBits(8, &quantization_params.base_q_idx));
1049  RCHECK(ReadDeltaQ(reader, &quantization_params.delta_qydc));
1050 
1051  const ColorConfig& color_config = sequence_header_.color_config;
1052  if (color_config.num_planes > 1) {
1053  bool diff_uv_delta = false;
1054  if (color_config.separate_uv_delta_q)
1055  RCHECK(reader->ReadBits(1, &diff_uv_delta));
1056  RCHECK(ReadDeltaQ(reader, &quantization_params.delta_qudc));
1057  RCHECK(ReadDeltaQ(reader, &quantization_params.delta_quac));
1058  if (diff_uv_delta) {
1059  RCHECK(ReadDeltaQ(reader, &quantization_params.delta_qvdc));
1060  RCHECK(ReadDeltaQ(reader, &quantization_params.delta_qvac));
1061  } else {
1062  quantization_params.delta_qvdc = quantization_params.delta_qudc;
1063  quantization_params.delta_qvac = quantization_params.delta_quac;
1064  }
1065  } else {
1066  quantization_params.delta_qudc = 0;
1067  quantization_params.delta_quac = 0;
1068  quantization_params.delta_qvdc = 0;
1069  quantization_params.delta_qvac = 0;
1070  }
1071  bool using_qmatrix = false;
1072  RCHECK(reader->ReadBits(1, &using_qmatrix));
1073  if (using_qmatrix) {
1074  // Skip qm_y, qm_u.
1075  RCHECK(reader->SkipBits(4 + 4));
1076  if (color_config.separate_uv_delta_q) {
1077  // Skip qm_v.
1078  RCHECK(reader->SkipBits(4));
1079  }
1080  }
1081  return true;
1082 }
1083 
1084 // 5.9.13. Delta quantizer syntax.
1085 bool AV1Parser::ReadDeltaQ(BitReader* reader, int* delta_q) {
1086  bool delta_coded = false;
1087  RCHECK(reader->ReadBits(1, &delta_coded));
1088  if (delta_coded)
1089  RCHECK(ReadSu(1 + 6, reader, delta_q));
1090  else
1091  *delta_q = 0;
1092  return true;
1093 }
1094 
1095 // 5.9.14. Segmentation params syntax.
1096 bool AV1Parser::ParseSegmentationParams(int primary_ref_frame,
1097  BitReader* reader) {
1098  SegmentationParams& segmentation_params = frame_header_.segmentation_params;
1099 
1100  RCHECK(reader->ReadBits(1, &segmentation_params.segmentation_enabled));
1101  if (segmentation_params.segmentation_enabled) {
1102  bool segmentation_update_data = false;
1103  if (primary_ref_frame == kPrimaryRefNone) {
1104  segmentation_update_data = true;
1105  } else {
1106  // Skip segmentation_update_map, segmentation_temporal_update.
1107  RCHECK(reader->SkipBitsConditional(true, 1));
1108  RCHECK(reader->ReadBits(1, &segmentation_update_data));
1109  }
1110  if (segmentation_update_data) {
1111  static const int kSegmentationFeatureBits[kSegLvlMax] = {8, 6, 6, 6,
1112  6, 3, 0, 0};
1113  static const int kSegmentationFeatureSigned[kSegLvlMax] = {1, 1, 1, 1,
1114  1, 0, 0, 0};
1115  const int kMaxLoopFilter = 63;
1116  static const int kSegmentationFeatureMax[kSegLvlMax] = {255,
1117  kMaxLoopFilter,
1118  kMaxLoopFilter,
1119  kMaxLoopFilter,
1120  kMaxLoopFilter,
1121  7,
1122  0,
1123  0};
1124 
1125  for (int i = 0; i < kMaxSegments; i++) {
1126  for (int j = 0; j < kSegLvlMax; j++) {
1127  bool feature_enabled = false;
1128  RCHECK(reader->ReadBits(1, &feature_enabled));
1129  segmentation_params.feature_enabled[i][j] = feature_enabled;
1130  int clipped_value = 0;
1131  if (feature_enabled) {
1132  const int bits_to_read = kSegmentationFeatureBits[j];
1133  const int limit = kSegmentationFeatureMax[j];
1134  if (kSegmentationFeatureSigned[j]) {
1135  int feature_value = 0;
1136  RCHECK(ReadSu(1 + bits_to_read, reader, &feature_value));
1137  clipped_value = Clip3(-limit, limit, feature_value);
1138  } else {
1139  int feature_value = 0;
1140  RCHECK(reader->ReadBits(bits_to_read, &feature_value));
1141  clipped_value = Clip3(0, limit, feature_value);
1142  }
1143  }
1144  segmentation_params.feature_data[i][j] = clipped_value;
1145  }
1146  }
1147  }
1148  } else {
1149  for (int i = 0; i < kMaxSegments; i++) {
1150  for (int j = 0; j < kSegLvlMax; j++) {
1151  segmentation_params.feature_enabled[i][j] = false;
1152  segmentation_params.feature_data[i][j] = 0;
1153  }
1154  }
1155  }
1156  return true;
1157 }
1158 
1159 // 5.9.15. Tile info syntax.
1160 bool AV1Parser::ParseTileInfo(BitReader* reader) {
1161  const int kMaxTileWidth = 4096;
1162  const int kMaxTileArea = 4096 * 2304;
1163  const int kMaxTileRows = 64;
1164  const int kMaxTileCols = 64;
1165 
1166  TileInfo& tile_info = frame_header_.tile_info;
1167 
1168  const int sb_cols = sequence_header_.use_128x128_superblock
1169  ? ((frame_header_.mi_cols + 31) >> 5)
1170  : ((frame_header_.mi_cols + 15) >> 4);
1171  const int sb_rows = sequence_header_.use_128x128_superblock
1172  ? ((frame_header_.mi_rows + 31) >> 5)
1173  : ((frame_header_.mi_rows + 15) >> 4);
1174  const int sb_shift = sequence_header_.use_128x128_superblock ? 5 : 4;
1175  const int sb_size = sb_shift + 2;
1176  const int max_tile_width_sb = kMaxTileWidth >> sb_size;
1177  int max_tile_area_sb = kMaxTileArea >> (2 * sb_size);
1178  const int min_log2_tile_cols = TileLog2(max_tile_width_sb, sb_cols);
1179  const int max_log2_tile_cols = TileLog2(1, std::min(sb_cols, kMaxTileCols));
1180  const int max_log2_tile_rows = TileLog2(1, std::min(sb_rows, kMaxTileRows));
1181  const int min_log2_tiles = std::max(
1182  min_log2_tile_cols, TileLog2(max_tile_area_sb, sb_rows * sb_cols));
1183 
1184  bool uniform_tile_spacing_flag = false;
1185  RCHECK(reader->ReadBits(1, &uniform_tile_spacing_flag));
1186  if (uniform_tile_spacing_flag) {
1187  tile_info.tile_cols_log2 = min_log2_tile_cols;
1188  while (tile_info.tile_cols_log2 < max_log2_tile_cols) {
1189  bool increment_tile_cols_log2 = false;
1190  RCHECK(reader->ReadBits(1, &increment_tile_cols_log2));
1191  if (increment_tile_cols_log2)
1192  tile_info.tile_cols_log2++;
1193  else
1194  break;
1195  }
1196  const int tile_width_sb = (sb_cols + (1 << tile_info.tile_cols_log2) - 1) >>
1197  tile_info.tile_cols_log2;
1198  int i = 0;
1199  for (int start_sb = 0; start_sb < sb_cols; start_sb += tile_width_sb) {
1200  i += 1;
1201  }
1202  tile_info.tile_cols = i;
1203 
1204  const int min_log2_tile_rows =
1205  std::max(min_log2_tiles - tile_info.tile_cols_log2, 0);
1206  tile_info.tile_rows_log2 = min_log2_tile_rows;
1207  while (tile_info.tile_rows_log2 < max_log2_tile_rows) {
1208  bool increment_tile_rows_log2 = false;
1209  RCHECK(reader->ReadBits(1, &increment_tile_rows_log2));
1210  if (increment_tile_rows_log2)
1211  tile_info.tile_rows_log2++;
1212  else
1213  break;
1214  }
1215  const int tile_height_sb =
1216  (sb_rows + (1 << tile_info.tile_rows_log2) - 1) >>
1217  tile_info.tile_rows_log2;
1218  i = 0;
1219  for (int start_sb = 0; start_sb < sb_rows; start_sb += tile_height_sb) {
1220  i += 1;
1221  }
1222  tile_info.tile_rows = i;
1223  } else {
1224  int widest_tile_sb = 0;
1225  int start_sb = 0;
1226  int i = 0;
1227  for (; start_sb < sb_cols; i++) {
1228  const int max_width = std::min(sb_cols - start_sb, max_tile_width_sb);
1229  int width_in_sbs_minus_1 = 0;
1230  RCHECK(ReadNs(max_width, reader, &width_in_sbs_minus_1));
1231  const int size_sb = width_in_sbs_minus_1 + 1;
1232  widest_tile_sb = std::max(size_sb, widest_tile_sb);
1233  start_sb += size_sb;
1234  }
1235  tile_info.tile_cols = i;
1236  tile_info.tile_cols_log2 = TileLog2(1, tile_info.tile_cols);
1237 
1238  if (min_log2_tiles > 0)
1239  max_tile_area_sb = (sb_rows * sb_cols) >> (min_log2_tiles + 1);
1240  else
1241  max_tile_area_sb = sb_rows * sb_cols;
1242  const int max_tile_height_sb =
1243  std::max(max_tile_area_sb / widest_tile_sb, 1);
1244 
1245  start_sb = 0;
1246  i = 0;
1247  for (; start_sb < sb_rows; i++) {
1248  const int max_height = std::min(sb_rows - start_sb, max_tile_height_sb);
1249  int height_in_sbs_minus_1 = 0;
1250  RCHECK(ReadNs(max_height, reader, &height_in_sbs_minus_1));
1251  const int size_sb = height_in_sbs_minus_1 + 1;
1252  start_sb += size_sb;
1253  }
1254  tile_info.tile_rows = i;
1255  tile_info.tile_rows_log2 = TileLog2(1, tile_info.tile_rows);
1256  }
1257  if (tile_info.tile_cols_log2 > 0 || tile_info.tile_rows_log2 > 0) {
1258  // Skip context_update_tile_id.
1259  RCHECK(
1260  reader->SkipBits(tile_info.tile_rows_log2 + tile_info.tile_cols_log2));
1261  int tile_size_bytes_minus_1 = 0;
1262  RCHECK(reader->ReadBits(2, &tile_size_bytes_minus_1));
1263  tile_info.tile_size_bytes = tile_size_bytes_minus_1 + 1;
1264  }
1265  return true;
1266 }
1267 
1268 // 5.9.17. Quantizer index delta parameters syntax.
1269 bool AV1Parser::SkipDeltaQParams(BitReader* reader, bool* delta_q_present) {
1270  *delta_q_present = false;
1271  if (frame_header_.quantization_params.base_q_idx > 0)
1272  RCHECK(reader->ReadBits(1, delta_q_present));
1273  if (*delta_q_present) {
1274  // Skip delta_q_res.
1275  RCHECK(reader->SkipBits(2));
1276  }
1277  return true;
1278 }
1279 
1280 // 5.9.18. Loop filter delta parameters syntax.
1281 bool AV1Parser::SkipDeltaLfParams(bool delta_q_present,
1282  bool allow_intrabc,
1283  BitReader* reader) {
1284  bool delta_lf_present = false;
1285  if (delta_q_present) {
1286  if (!allow_intrabc)
1287  RCHECK(reader->ReadBits(1, &delta_lf_present));
1288  if (delta_lf_present) {
1289  // Skip delta_lf_res, delta_lf_multi.
1290  RCHECK(reader->SkipBits(2 + 1));
1291  }
1292  }
1293  return true;
1294 }
1295 
1296 // 5.9.19. CDEF params syntax.
1297 bool AV1Parser::ParseCdefParams(bool coded_lossless,
1298  bool allow_intrabc,
1299  BitReader* reader) {
1300  if (coded_lossless || allow_intrabc || !sequence_header_.enable_cdef)
1301  return true;
1302 
1303  // Skip cdef_damping_minus_3.
1304  RCHECK(reader->SkipBits(2));
1305  int cdef_bits = 0;
1306  RCHECK(reader->ReadBits(2, &cdef_bits));
1307  for (int i = 0; i < (1 << cdef_bits); i++) {
1308  // Skip cdef_y_pri_strength[i], Skip cdef_y_sec_strength[i].
1309  RCHECK(reader->SkipBits(4 + 2));
1310  if (sequence_header_.color_config.num_planes > 1) {
1311  // Skip cdef_uv_pri_strength[i], Skip cdef_uv_sec_strength[i].
1312  RCHECK(reader->SkipBits(4 + 2));
1313  }
1314  }
1315  return true;
1316 }
1317 
1318 // 5.9.20. Loop restoration params syntax.
1319 bool AV1Parser::ParseLrParams(bool all_lossless,
1320  bool allow_intrabc,
1321  BitReader* reader) {
1322  if (all_lossless || allow_intrabc || !sequence_header_.enable_restoration)
1323  return true;
1324 
1325  enum FrameRestorationType {
1326  RESTORE_NONE = 0,
1327  RESTORE_SWITCHABLE = 3,
1328  RESTORE_WIENER = 1,
1329  RESTORE_SGRPROJ = 2,
1330  };
1331  static const int kRemapLrType[4] = {RESTORE_NONE, RESTORE_SWITCHABLE,
1332  RESTORE_WIENER, RESTORE_SGRPROJ};
1333  bool uses_lr = false;
1334  bool uses_chroma_lr = false;
1335  for (int i = 0; i < sequence_header_.color_config.num_planes; i++) {
1336  int lr_type = 0;
1337  RCHECK(reader->ReadBits(2, &lr_type));
1338  const int frame_restoration_type = kRemapLrType[lr_type];
1339  if (frame_restoration_type != RESTORE_NONE) {
1340  uses_lr = true;
1341  if (i > 0)
1342  uses_chroma_lr = true;
1343  }
1344  }
1345 
1346  if (uses_lr) {
1347  if (sequence_header_.use_128x128_superblock) {
1348  // Skip lr_unit_shift.
1349  RCHECK(reader->SkipBits(1));
1350  } else {
1351  // Skip lr_unit_shift, lr_unit_extra_shift.
1352  RCHECK(reader->SkipBitsConditional(true, 1));
1353  }
1354  if (sequence_header_.color_config.subsampling_x &&
1355  sequence_header_.color_config.subsampling_y && uses_chroma_lr) {
1356  // Skip lr_uv_shift.
1357  RCHECK(reader->SkipBits(1));
1358  }
1359  }
1360  return true;
1361 }
1362 
1363 // 5.9.21. TX mode syntax.
1364 bool AV1Parser::SkipTxMode(bool coded_lossless, BitReader* reader) {
1365  if (!coded_lossless) {
1366  // Skip tx_mode_select.
1367  RCHECK(reader->SkipBits(1));
1368  }
1369  return true;
1370 }
1371 
1372 // 5.9.22. Skip mode params syntax.
1373 bool AV1Parser::SkipSkipModeParams(bool frame_is_intra,
1374  bool reference_select,
1375  BitReader* reader) {
1376  bool skip_mode_allowed = false;
1377  if (frame_is_intra || !reference_select ||
1378  !sequence_header_.enable_order_hint) {
1379  skip_mode_allowed = false;
1380  } else {
1381  int forward_idx = -1;
1382  int forward_hint = 0;
1383  int backward_idx = -1;
1384  int backward_hint = 0;
1385  for (int i = 0; i < kRefsPerFrame; i++) {
1386  const int ref_hint =
1387  reference_frames_[frame_header_.ref_frame_idx[i]].order_hint;
1388  if (GetRelativeDist(ref_hint, frame_header_.order_hint) < 0) {
1389  if (forward_idx < 0 || GetRelativeDist(ref_hint, forward_hint) > 0) {
1390  forward_idx = i;
1391  forward_hint = ref_hint;
1392  }
1393  } else if (GetRelativeDist(ref_hint, frame_header_.order_hint) > 0) {
1394  if (backward_idx < 0 || GetRelativeDist(ref_hint, backward_hint) < 0) {
1395  backward_idx = i;
1396  backward_hint = ref_hint;
1397  }
1398  }
1399  }
1400  if (forward_idx < 0) {
1401  skip_mode_allowed = false;
1402  } else if (backward_idx >= 0) {
1403  skip_mode_allowed = true;
1404  } else {
1405  int second_forward_idx = -1;
1406  int second_forward_hint = 0;
1407  for (int i = 0; i < kRefsPerFrame; i++) {
1408  const int ref_hint =
1409  reference_frames_[frame_header_.ref_frame_idx[i]].order_hint;
1410  if (GetRelativeDist(ref_hint, forward_hint) < 0) {
1411  if (second_forward_idx < 0 ||
1412  GetRelativeDist(ref_hint, second_forward_hint) > 0) {
1413  second_forward_idx = i;
1414  second_forward_hint = ref_hint;
1415  }
1416  }
1417  }
1418  skip_mode_allowed = second_forward_idx >= 0;
1419  }
1420  }
1421 
1422  if (skip_mode_allowed) {
1423  // Skip skip_mode_present.
1424  RCHECK(reader->SkipBits(1));
1425  }
1426  return true;
1427 }
1428 
1429 // 5.9.23. Frame reference mode syntax.
1430 bool AV1Parser::ParseFrameReferenceMode(bool frame_is_intra,
1431  BitReader* reader,
1432  bool* reference_select) {
1433  if (frame_is_intra)
1434  *reference_select = false;
1435  else
1436  RCHECK(reader->ReadBits(1, reference_select));
1437  return true;
1438 }
1439 
1440 // 5.9.24. Global motion params syntax.
1441 bool AV1Parser::SkipGlobalMotionParams(bool frame_is_intra,
1442  bool allow_high_precision_mv,
1443  BitReader* reader) {
1444  if (frame_is_intra)
1445  return true;
1446 
1447  for (int ref = LAST_FRAME; ref <= ALTREF_FRAME; ref++) {
1448  int type = 0;
1449 
1450  bool is_global = false;
1451  RCHECK(reader->ReadBits(1, &is_global));
1452  if (is_global) {
1453  bool is_rot_zoom = false;
1454  RCHECK(reader->ReadBits(1, &is_rot_zoom));
1455  if (is_rot_zoom) {
1456  type = ROTZOOM;
1457  } else {
1458  bool is_translation = false;
1459  RCHECK(reader->ReadBits(1, &is_translation));
1460  type = is_translation ? TRANSLATION : AFFINE;
1461  }
1462  } else {
1463  type = IDENTITY;
1464  }
1465 
1466  if (type >= ROTZOOM) {
1467  RCHECK(SkipGlobalParam(type, ref, 2, allow_high_precision_mv, reader));
1468  RCHECK(SkipGlobalParam(type, ref, 3, allow_high_precision_mv, reader));
1469  if (type == AFFINE) {
1470  RCHECK(SkipGlobalParam(type, ref, 4, allow_high_precision_mv, reader));
1471  RCHECK(SkipGlobalParam(type, ref, 5, allow_high_precision_mv, reader));
1472  }
1473  }
1474  if (type >= TRANSLATION) {
1475  RCHECK(SkipGlobalParam(type, ref, 0, allow_high_precision_mv, reader));
1476  RCHECK(SkipGlobalParam(type, ref, 1, allow_high_precision_mv, reader));
1477  }
1478  }
1479  return true;
1480 }
1481 
1482 // 5.9.25. Global param syntax.
1483 bool AV1Parser::SkipGlobalParam(int type,
1484  int ref,
1485  int idx,
1486  bool allow_high_precision_mv,
1487  BitReader* reader) {
1488  const int kGmAbsTransBits = 12;
1489  const int kGmAbsTransOnlyBits = 9;
1490  const int kGmAbsAlphaBits = 12;
1491 
1492  int abs_bits = kGmAbsAlphaBits;
1493  if (idx < 2) {
1494  if (type == TRANSLATION) {
1495  abs_bits = kGmAbsTransOnlyBits - (allow_high_precision_mv ? 0 : 1);
1496  } else {
1497  abs_bits = kGmAbsTransBits;
1498  }
1499  }
1500  const int mx = 1 << abs_bits;
1501  RCHECK(SkipDecodeSignedSubexpWithRef(-mx, mx + 1, reader));
1502  return true;
1503 }
1504 
1505 // 5.9.26. Decode signed subexp with ref syntax.
1506 bool AV1Parser::SkipDecodeSignedSubexpWithRef(int low,
1507  int high,
1508  BitReader* reader) {
1509  RCHECK(SkipDecodeUnsignedSubexpWithRef(high - low, reader));
1510  return true;
1511 }
1512 
1513 // 5.9.27. Decode unsigned subbexp with ref syntax.
1514 bool AV1Parser::SkipDecodeUnsignedSubexpWithRef(int mx, BitReader* reader) {
1515  RCHECK(SkipDecodeSubexp(mx, reader));
1516  return true;
1517 }
1518 
1519 // 5.9.28. Decode subexp syntax.
1520 bool AV1Parser::SkipDecodeSubexp(int num_syms, BitReader* reader) {
1521  int i = 0;
1522  int mk = 0;
1523  int k = 3;
1524  while (true) {
1525  const int b2 = i ? (k + i - 1) : k;
1526  const int a = 1 << b2;
1527  if (num_syms <= mk + 3 * a) {
1528  int subexp_final_bits = 0;
1529  RCHECK(ReadNs(num_syms - mk, reader, &subexp_final_bits));
1530  return true;
1531  } else {
1532  bool subexp_more_bits = false;
1533  RCHECK(reader->ReadBits(1, &subexp_more_bits));
1534  if (subexp_more_bits) {
1535  i++;
1536  mk += a;
1537  } else {
1538  // Skip subexp_bits.
1539  RCHECK(reader->SkipBits(b2));
1540  return true;
1541  }
1542  }
1543  }
1544  return true;
1545 }
1546 
1547 // 5.9.30. Film grain params syntax.
1548 bool AV1Parser::SkipFilmGrainParams(bool show_frame,
1549  bool showable_frame,
1550  BitReader* reader) {
1551  if (!sequence_header_.film_grain_params_present ||
1552  (!show_frame && !showable_frame)) {
1553  return true;
1554  }
1555 
1556  bool apply_grain = false;
1557  RCHECK(reader->ReadBits(1, &apply_grain));
1558  if (!apply_grain)
1559  return true;
1560 
1561  // Skip grain_seed.
1562  RCHECK(reader->SkipBits(16));
1563  bool update_grain = true;
1564  if (frame_header_.frame_type == INTER_FRAME)
1565  RCHECK(reader->ReadBits(1, &update_grain));
1566  if (!update_grain) {
1567  // Skip film_grain_params_ref_idx.
1568  RCHECK(reader->SkipBits(3));
1569  return true;
1570  }
1571 
1572  int num_y_points = 0;
1573  RCHECK(reader->ReadBits(4, &num_y_points));
1574  // Skip point_y_value, point_y_scaling.
1575  RCHECK(reader->SkipBits((8 + 8) * num_y_points));
1576 
1577  const ColorConfig& color_config = sequence_header_.color_config;
1578  bool chroma_scaling_from_luma = false;
1579  if (!color_config.mono_chrome)
1580  RCHECK(reader->ReadBits(1, &chroma_scaling_from_luma));
1581  int num_cb_points = 0;
1582  int num_cr_points = 0;
1583  if (color_config.mono_chrome || chroma_scaling_from_luma ||
1584  (color_config.subsampling_x && color_config.subsampling_y &&
1585  num_y_points == 0)) {
1586  num_cb_points = 0;
1587  num_cr_points = 0;
1588  } else {
1589  RCHECK(reader->ReadBits(4, &num_cb_points));
1590  // Skip point_cb_value, point_cb_scaling.
1591  RCHECK(reader->SkipBits((8 + 8) * num_cb_points));
1592  RCHECK(reader->ReadBits(4, &num_cr_points));
1593  // Skip point_cr_value, point_cr_scaling.
1594  RCHECK(reader->SkipBits((8 + 8) * num_cr_points));
1595  }
1596 
1597  // Skip grain_scaling_minus_8.
1598  RCHECK(reader->SkipBits(2));
1599  int ar_coeff_lag = 0;
1600  RCHECK(reader->ReadBits(2, &ar_coeff_lag));
1601  const int num_pos_luma = 2 * ar_coeff_lag * (ar_coeff_lag + 1);
1602  int num_pos_chroma = num_pos_luma;
1603  if (num_y_points) {
1604  num_pos_chroma = num_pos_luma + 1;
1605  // Skip ar_coeffs_y_plus_128.
1606  RCHECK(reader->SkipBits(8 * num_pos_luma));
1607  }
1608  if (chroma_scaling_from_luma || num_cb_points) {
1609  // Skip ar_coeffs_cb_plus_128.
1610  RCHECK(reader->SkipBits(8 * num_pos_chroma));
1611  }
1612  if (chroma_scaling_from_luma || num_cr_points) {
1613  // Skip ar_coeffs_cb_plus_128.
1614  RCHECK(reader->SkipBits(8 * num_pos_chroma));
1615  }
1616 
1617  // Skip ar_coeff_shift_minus_6, grain_scale_shift.
1618  RCHECK(reader->SkipBits(2 + 2));
1619  if (num_cb_points) {
1620  // Skip cb_mult, cb_luma_mult, cb_offset.
1621  RCHECK(reader->SkipBits(8 + 8 + 9));
1622  }
1623  if (num_cr_points) {
1624  // Skip cr_mult, cr_luma_mult, cr_offset.
1625  RCHECK(reader->SkipBits(8 + 8 + 9));
1626  }
1627  // Skip overlap_flag, clip_restricted_range.
1628  RCHECK(reader->SkipBits(1 + 1));
1629  return true;
1630 }
1631 
1632 // 5.9.31. Temporal point info syntax.
1633 bool AV1Parser::SkipTemporalPointInfo(BitReader* reader) {
1634  const int frame_presentation_time_length =
1635  sequence_header_.decoder_model_info
1636  .frame_presentation_time_length_minus_1 +
1637  1;
1638  // Skip frame_presentation_time.
1639  RCHECK(reader->SkipBits(frame_presentation_time_length));
1640  return true;
1641 }
1642 
1643 // 5.10. Frame OBU syntax.
1644 bool AV1Parser::ParseFrameObu(const ObuHeader& obu_header,
1645  size_t size,
1646  BitReader* reader,
1647  std::vector<Tile>* tiles) {
1648  const size_t start_bit_pos = reader->bit_position();
1649  RCHECK(ParseFrameHeaderObu(obu_header, reader));
1650  RCHECK(ByteAlignment(reader));
1651  const size_t end_bit_pos = reader->bit_position();
1652  const size_t header_bytes = (end_bit_pos - start_bit_pos) / 8;
1653  RCHECK(ParseTileGroupObu(size - header_bytes, reader, tiles));
1654  return true;
1655 }
1656 
1657 // 5.11.1. General tile group OBU syntax.
1658 bool AV1Parser::ParseTileGroupObu(size_t size,
1659  BitReader* reader,
1660  std::vector<Tile>* tiles) {
1661  const TileInfo& tile_info = frame_header_.tile_info;
1662  const size_t start_bit_pos = reader->bit_position();
1663 
1664  const int num_tiles = tile_info.tile_cols * tile_info.tile_rows;
1665  bool tile_start_and_end_present_flag = false;
1666  if (num_tiles > 1)
1667  RCHECK(reader->ReadBits(1, &tile_start_and_end_present_flag));
1668 
1669  int tg_start = 0;
1670  int tg_end = num_tiles - 1;
1671  if (num_tiles > 1 && tile_start_and_end_present_flag) {
1672  const int tile_bits = tile_info.tile_cols_log2 + tile_info.tile_rows_log2;
1673  RCHECK(reader->ReadBits(tile_bits, &tg_start));
1674  RCHECK(reader->ReadBits(tile_bits, &tg_end));
1675  }
1676  RCHECK(ByteAlignment(reader));
1677 
1678  const size_t end_bit_pos = reader->bit_position();
1679  const size_t header_bytes = (end_bit_pos - start_bit_pos) / 8;
1680  size -= header_bytes;
1681 
1682  for (int tile_num = tg_start; tile_num <= tg_end; tile_num++) {
1683  const bool last_tile = tile_num == tg_end;
1684  size_t tile_size = size;
1685  if (!last_tile) {
1686  size_t tile_size_minus_1 = 0;
1687  RCHECK(ReadLe(tile_info.tile_size_bytes, reader, &tile_size_minus_1));
1688  tile_size = tile_size_minus_1 + 1;
1689  size -= tile_size + tile_info.tile_size_bytes;
1690  }
1691  tiles->push_back({reader->bit_position() / 8, tile_size});
1692  RCHECK(reader->SkipBits(tile_size * 8)); // Skip the tile.
1693  }
1694 
1695  if (tg_end == num_tiles - 1) {
1696  DecodeFrameWrapup();
1697  frame_header_.seen_frame_header = false;
1698  }
1699  return true;
1700 }
1701 
1702 // 5.11.14. Segmentation feature active function.
1703 bool AV1Parser::SegFeatureActiveIdx(int idx, int feature) {
1704  const SegmentationParams& segmentation_params =
1705  frame_header_.segmentation_params;
1706  return segmentation_params.segmentation_enabled &&
1707  segmentation_params.feature_enabled[idx][feature];
1708 }
1709 
1710 // 7.4. Decode frame wrapup process.
1711 void AV1Parser::DecodeFrameWrapup() {
1712  const int refresh_frame_flags = frame_header_.refresh_frame_flags;
1713  if (frame_header_.show_existing_frame &&
1714  frame_header_.frame_type == KEY_FRAME) {
1715  // 7.21. Reference frame loading process.
1716  const ReferenceFrame& reference_frame =
1717  reference_frames_[frame_header_.frame_to_show_map_idx];
1718 
1719  frame_header_.upscaled_width = reference_frame.upscaled_width;
1720  frame_header_.frame_width = reference_frame.frame_width;
1721  frame_header_.frame_height = reference_frame.frame_height;
1722  frame_header_.render_width = reference_frame.render_width;
1723  frame_header_.render_height = reference_frame.render_height;
1724  frame_header_.mi_cols = reference_frame.mi_cols;
1725  frame_header_.mi_rows = reference_frame.mi_rows;
1726 
1727  ColorConfig& color_config = sequence_header_.color_config;
1728  color_config.subsampling_x = reference_frame.subsampling_x;
1729  color_config.subsampling_y = reference_frame.subsampling_y;
1730  color_config.bit_depth = reference_frame.bit_depth;
1731 
1732  frame_header_.order_hint = reference_frame.order_hint;
1733  }
1734  // 7.20. Reference frame update process.
1735  for (int i = 0; i <= kNumRefFrames - 1; i++) {
1736  if ((refresh_frame_flags >> i) & 1) {
1737  ReferenceFrame& reference_frame = reference_frames_[i];
1738 
1739  reference_frame.upscaled_width = frame_header_.upscaled_width;
1740  reference_frame.frame_width = frame_header_.frame_width;
1741  reference_frame.frame_height = frame_header_.frame_height;
1742  reference_frame.render_width = frame_header_.render_width;
1743  reference_frame.render_height = frame_header_.render_height;
1744  reference_frame.mi_cols = frame_header_.mi_cols;
1745  reference_frame.mi_rows = frame_header_.mi_rows;
1746  reference_frame.frame_type = frame_header_.frame_type;
1747 
1748  const ColorConfig& color_config = sequence_header_.color_config;
1749  reference_frame.subsampling_x = color_config.subsampling_x;
1750  reference_frame.subsampling_y = color_config.subsampling_y;
1751  reference_frame.bit_depth = color_config.bit_depth;
1752 
1753  reference_frame.order_hint = frame_header_.order_hint;
1754  }
1755  }
1756 }
1757 
1758 // 7.8. Set frame refs process.
1759 bool AV1Parser::SetFrameRefs(int last_frame_idx, int gold_frame_idx) {
1760  for (int i = 0; i < kRefsPerFrame; i++)
1761  frame_header_.ref_frame_idx[i] = -1;
1762  frame_header_.ref_frame_idx[LAST_FRAME - LAST_FRAME] = last_frame_idx;
1763  frame_header_.ref_frame_idx[GOLDEN_FRAME - LAST_FRAME] = gold_frame_idx;
1764 
1765  bool used_frame[kNumRefFrames] = {};
1766  used_frame[last_frame_idx] = true;
1767  used_frame[gold_frame_idx] = true;
1768 
1769  const int cur_frame_hint = 1 << (sequence_header_.order_hint_bits - 1);
1770 
1771  // An array containing the expected output order shifted such that the
1772  // current frame has hint equal to |cur_frame_hint| is prepared.
1773  int shifted_order_hints[kNumRefFrames];
1774  for (int i = 0; i < kNumRefFrames; i++) {
1775  shifted_order_hints[i] =
1776  cur_frame_hint + GetRelativeDist(reference_frames_[i].order_hint,
1777  frame_header_.order_hint);
1778  }
1779 
1780  const int last_order_hint = shifted_order_hints[last_frame_idx];
1781  RCHECK(last_order_hint < cur_frame_hint);
1782  const int gold_order_hint = shifted_order_hints[gold_frame_idx];
1783  RCHECK(gold_order_hint < cur_frame_hint);
1784 
1785  // The ALTREF_FRAME reference is set to be a backward reference to the frame
1786  // with highest output order.
1787  int ref = FindLatestBackward(shifted_order_hints, used_frame, cur_frame_hint);
1788  if (ref >= 0) {
1789  frame_header_.ref_frame_idx[ALTREF_FRAME - LAST_FRAME] = ref;
1790  used_frame[ref] = true;
1791  }
1792 
1793  // The BWDREF_FRAME reference is set to be a backward reference to the cloest
1794  // frame.
1795  ref = FindEarliestBackward(shifted_order_hints, used_frame, cur_frame_hint);
1796  if (ref >= 0) {
1797  frame_header_.ref_frame_idx[BWDREF_FRAME - LAST_FRAME] = ref;
1798  used_frame[ref] = true;
1799  }
1800 
1801  // The ALTREF2_FRAME reference is set to the next closest backward reference.
1802  ref = FindEarliestBackward(shifted_order_hints, used_frame, cur_frame_hint);
1803  if (ref >= 0) {
1804  frame_header_.ref_frame_idx[ALTREF2_FRAME - LAST_FRAME] = ref;
1805  used_frame[ref] = true;
1806  }
1807 
1808  // The remaining references are set to be forward references in
1809  // anti-chronological order.
1810  static const int kRefFrameList[] = {
1811  LAST2_FRAME, LAST3_FRAME, BWDREF_FRAME, ALTREF2_FRAME, ALTREF_FRAME,
1812  };
1813  static_assert(arraysize(kRefFrameList) == kRefsPerFrame - 2,
1814  "Unexpected kRefFrameList size.");
1815  for (const int ref_frame : kRefFrameList) {
1816  if (frame_header_.ref_frame_idx[ref_frame - LAST_FRAME] < 0) {
1817  ref = FindLatestForward(shifted_order_hints, used_frame, cur_frame_hint);
1818  if (ref >= 0) {
1819  frame_header_.ref_frame_idx[ref_frame - LAST_FRAME] = ref;
1820  used_frame[ref] = true;
1821  }
1822  }
1823  }
1824 
1825  // Finally, any remaining references are set to the reference frame with
1826  // smallest output order.
1827  ref = -1;
1828  int earliest_order_hint = 0;
1829  for (int i = 0; i < kNumRefFrames; i++) {
1830  const int hint = shifted_order_hints[i];
1831  if (ref < 0 || hint < earliest_order_hint) {
1832  ref = i;
1833  earliest_order_hint = hint;
1834  }
1835  }
1836  for (int i = 0; i < kRefsPerFrame; i++) {
1837  if (frame_header_.ref_frame_idx[i] < 0) {
1838  frame_header_.ref_frame_idx[i] = ref;
1839  }
1840  }
1841 
1842  return true;
1843 }
1844 
1845 // 7.12.2. Dequantization functions. The function returns the quantizer index
1846 // for the current block.
1847 int AV1Parser::GetQIndex(bool ignore_delta_q, int segment_id) {
1848  // We do not have use case for ignore_delta_q false case.
1849  CHECK(ignore_delta_q) << "ignoreDeltaQ equal to 0 is not supported.";
1850 
1851  const int base_q_idx = frame_header_.quantization_params.base_q_idx;
1852 
1853  const int kSegLvlAltQ = 0;
1854  if (SegFeatureActiveIdx(segment_id, kSegLvlAltQ)) {
1855  const int data =
1856  frame_header_.segmentation_params.feature_data[segment_id][kSegLvlAltQ];
1857  const int qindex = base_q_idx + data;
1858  return Clip3(0, 255, qindex);
1859  } else {
1860  return base_q_idx;
1861  }
1862 }
1863 
1864 } // namespace media
1865 } // namespace shaka
virtual bool Parse(const uint8_t *data, size_t data_size, std::vector< Tile > *tiles)
Definition: av1_parser.cc:255
A class to read bit streams.
Definition: bit_reader.h:17
size_t bit_position() const
Definition: bit_reader.h:94
bool SkipBits(size_t num_bits)
Definition: bit_reader.cc:24
size_t bits_available() const
Definition: bit_reader.h:89
All the methods that are virtual are virtual for mocking.