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