• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The libgav1 Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/obu_parser.h"
16 
17 #include <algorithm>
18 #include <array>
19 #include <cassert>
20 #include <climits>
21 #include <cstddef>
22 #include <cstdint>
23 #include <cstring>
24 #include <memory>
25 #include <new>
26 #include <utility>
27 
28 #include "src/buffer_pool.h"
29 #include "src/decoder_state.h"
30 #include "src/gav1/decoder_buffer.h"
31 #include "src/gav1/status_code.h"
32 #include "src/quantizer.h"
33 #include "src/utils/common.h"
34 #include "src/utils/constants.h"
35 #include "src/utils/logging.h"
36 #include "src/utils/raw_bit_reader.h"
37 #include "src/utils/reference_info.h"
38 #include "src/utils/segmentation.h"
39 #include "src/utils/types.h"
40 
41 namespace libgav1 {
42 namespace {
43 
44 // 5.9.16.
45 // Find the smallest value of k such that block_size << k is greater than or
46 // equal to target.
47 //
48 // NOTE: TileLog2(block_size, target) is equal to
49 //   CeilLog2(ceil((double)target / block_size))
50 // where the division is a floating-point number division. (This equality holds
51 // even when |target| is equal to 0.) In the special case of block_size == 1,
52 // TileLog2(1, target) is equal to CeilLog2(target).
TileLog2(int block_size,int target)53 int TileLog2(int block_size, int target) {
54   int k = 0;
55   for (; (block_size << k) < target; ++k) {
56   }
57   return k;
58 }
59 
ParseBitStreamLevel(BitStreamLevel * const level,uint8_t level_bits)60 void ParseBitStreamLevel(BitStreamLevel* const level, uint8_t level_bits) {
61   level->major = kMinimumMajorBitstreamLevel + (level_bits >> 2);
62   level->minor = level_bits & 3;
63 }
64 
65 // This function assumes loop_filter is zero-initialized, so only it needs to
66 // set the nonzero default values.
SetDefaultRefDeltas(LoopFilter * const loop_filter)67 void SetDefaultRefDeltas(LoopFilter* const loop_filter) {
68   loop_filter->ref_deltas[kReferenceFrameIntra] = 1;
69   loop_filter->ref_deltas[kReferenceFrameGolden] = -1;
70   loop_filter->ref_deltas[kReferenceFrameAlternate] = -1;
71   loop_filter->ref_deltas[kReferenceFrameAlternate2] = -1;
72 }
73 
InTemporalLayer(int operating_point_idc,int temporal_id)74 bool InTemporalLayer(int operating_point_idc, int temporal_id) {
75   return ((operating_point_idc >> temporal_id) & 1) != 0;
76 }
77 
InSpatialLayer(int operating_point_idc,int spatial_id)78 bool InSpatialLayer(int operating_point_idc, int spatial_id) {
79   return ((operating_point_idc >> (spatial_id + 8)) & 1) != 0;
80 }
81 
82 // Returns the index of the last nonzero byte in the |data| buffer of |size|
83 // bytes. If there is no nonzero byte in the |data| buffer, returns -1.
GetLastNonzeroByteIndex(const uint8_t * data,size_t size)84 int GetLastNonzeroByteIndex(const uint8_t* data, size_t size) {
85   // Scan backward for a nonzero byte.
86   if (size > INT_MAX) return -1;
87   int i = static_cast<int>(size) - 1;
88   while (i >= 0 && data[i] == 0) {
89     --i;
90   }
91   return i;
92 }
93 
94 // A cleanup helper class that releases the frame buffer reference held in
95 // |frame| in the destructor.
96 class RefCountedBufferPtrCleanup {
97  public:
RefCountedBufferPtrCleanup(RefCountedBufferPtr * frame)98   explicit RefCountedBufferPtrCleanup(RefCountedBufferPtr* frame)
99       : frame_(*frame) {}
100 
101   // Not copyable or movable.
102   RefCountedBufferPtrCleanup(const RefCountedBufferPtrCleanup&) = delete;
103   RefCountedBufferPtrCleanup& operator=(const RefCountedBufferPtrCleanup&) =
104       delete;
105 
~RefCountedBufferPtrCleanup()106   ~RefCountedBufferPtrCleanup() { frame_ = nullptr; }
107 
108  private:
109   RefCountedBufferPtr& frame_;
110 };
111 
112 }  // namespace
113 
ParametersChanged(const ObuSequenceHeader & old) const114 bool ObuSequenceHeader::ParametersChanged(const ObuSequenceHeader& old) const {
115   // Note that the operating_parameters field is not compared per Section 7.5:
116   //   Within a particular coded video sequence, the contents of
117   //   sequence_header_obu must be bit-identical each time the sequence header
118   //   appears except for the contents of operating_parameters_info.
119   return memcmp(this, &old,
120                 offsetof(ObuSequenceHeader, operating_parameters)) != 0;
121 }
122 
123 // Macros to avoid repeated error checks in the parser code.
124 #define OBU_LOG_AND_RETURN_FALSE                                            \
125   do {                                                                      \
126     LIBGAV1_DLOG(ERROR, "%s:%d (%s): Not enough bits.", __FILE__, __LINE__, \
127                  __func__);                                                 \
128     return false;                                                           \
129   } while (false)
130 #define OBU_PARSER_FAIL         \
131   do {                          \
132     if (scratch == -1) {        \
133       OBU_LOG_AND_RETURN_FALSE; \
134     }                           \
135   } while (false)
136 #define OBU_READ_BIT_OR_FAIL        \
137   scratch = bit_reader_->ReadBit(); \
138   OBU_PARSER_FAIL
139 #define OBU_READ_LITERAL_OR_FAIL(n)      \
140   scratch = bit_reader_->ReadLiteral(n); \
141   OBU_PARSER_FAIL
142 #define OBU_READ_UVLC_OR_FAIL(x)        \
143   do {                                  \
144     if (!bit_reader_->ReadUvlc(&(x))) { \
145       OBU_LOG_AND_RETURN_FALSE;         \
146     }                                   \
147   } while (false)
148 
ParseColorConfig(ObuSequenceHeader * sequence_header)149 bool ObuParser::ParseColorConfig(ObuSequenceHeader* sequence_header) {
150   int64_t scratch;
151   ColorConfig* const color_config = &sequence_header->color_config;
152   OBU_READ_BIT_OR_FAIL;
153   const bool high_bitdepth = scratch != 0;
154   if (sequence_header->profile == kProfile2 && high_bitdepth) {
155     OBU_READ_BIT_OR_FAIL;
156     const bool is_twelve_bit = scratch != 0;
157     color_config->bitdepth = is_twelve_bit ? 12 : 10;
158   } else {
159     color_config->bitdepth = high_bitdepth ? 10 : 8;
160   }
161   if (sequence_header->profile == kProfile1) {
162     color_config->is_monochrome = false;
163   } else {
164     OBU_READ_BIT_OR_FAIL;
165     color_config->is_monochrome = scratch != 0;
166   }
167   OBU_READ_BIT_OR_FAIL;
168   const bool color_description_present_flag = scratch != 0;
169   if (color_description_present_flag) {
170     OBU_READ_LITERAL_OR_FAIL(8);
171     color_config->color_primary = static_cast<ColorPrimary>(scratch);
172     OBU_READ_LITERAL_OR_FAIL(8);
173     color_config->transfer_characteristics =
174         static_cast<TransferCharacteristics>(scratch);
175     OBU_READ_LITERAL_OR_FAIL(8);
176     color_config->matrix_coefficients =
177         static_cast<MatrixCoefficients>(scratch);
178   } else {
179     color_config->color_primary = kColorPrimaryUnspecified;
180     color_config->transfer_characteristics =
181         kTransferCharacteristicsUnspecified;
182     color_config->matrix_coefficients = kMatrixCoefficientsUnspecified;
183   }
184   if (color_config->is_monochrome) {
185     OBU_READ_BIT_OR_FAIL;
186     color_config->color_range = static_cast<ColorRange>(scratch);
187     // Set subsampling_x and subsampling_y to 1 for monochrome. This makes it
188     // easy to allow monochrome to be supported in profile 0. Profile 0
189     // requires subsampling_x and subsampling_y to be 1.
190     color_config->subsampling_x = 1;
191     color_config->subsampling_y = 1;
192     color_config->chroma_sample_position = kChromaSamplePositionUnknown;
193   } else {
194     if (color_config->color_primary == kColorPrimaryBt709 &&
195         color_config->transfer_characteristics ==
196             kTransferCharacteristicsSrgb &&
197         color_config->matrix_coefficients == kMatrixCoefficientsIdentity) {
198       color_config->color_range = kColorRangeFull;
199       color_config->subsampling_x = 0;
200       color_config->subsampling_y = 0;
201       // YUV 4:4:4 is only allowed in profile 1, or profile 2 with bit depth 12.
202       // See the table at the beginning of Section 6.4.1.
203       if (sequence_header->profile != kProfile1 &&
204           (sequence_header->profile != kProfile2 ||
205            color_config->bitdepth != 12)) {
206         LIBGAV1_DLOG(ERROR,
207                      "YUV 4:4:4 is not allowed in profile %d for bitdepth %d.",
208                      sequence_header->profile, color_config->bitdepth);
209         return false;
210       }
211     } else {
212       OBU_READ_BIT_OR_FAIL;
213       color_config->color_range = static_cast<ColorRange>(scratch);
214       if (sequence_header->profile == kProfile0) {
215         color_config->subsampling_x = 1;
216         color_config->subsampling_y = 1;
217       } else if (sequence_header->profile == kProfile1) {
218         color_config->subsampling_x = 0;
219         color_config->subsampling_y = 0;
220       } else {
221         if (color_config->bitdepth == 12) {
222           OBU_READ_BIT_OR_FAIL;
223           color_config->subsampling_x = scratch;
224           if (color_config->subsampling_x == 1) {
225             OBU_READ_BIT_OR_FAIL;
226             color_config->subsampling_y = scratch;
227           } else {
228             color_config->subsampling_y = 0;
229           }
230         } else {
231           color_config->subsampling_x = 1;
232           color_config->subsampling_y = 0;
233         }
234       }
235       if (color_config->subsampling_x == 1 &&
236           color_config->subsampling_y == 1) {
237         OBU_READ_LITERAL_OR_FAIL(2);
238         color_config->chroma_sample_position =
239             static_cast<ChromaSamplePosition>(scratch);
240       }
241     }
242     OBU_READ_BIT_OR_FAIL;
243     color_config->separate_uv_delta_q = scratch != 0;
244   }
245   if (color_config->matrix_coefficients == kMatrixCoefficientsIdentity &&
246       (color_config->subsampling_x != 0 || color_config->subsampling_y != 0)) {
247     LIBGAV1_DLOG(ERROR,
248                  "matrix_coefficients is MC_IDENTITY, but subsampling_x (%d) "
249                  "and subsampling_y (%d) are not both 0.",
250                  color_config->subsampling_x, color_config->subsampling_y);
251     return false;
252   }
253   return true;
254 }
255 
ParseTimingInfo(ObuSequenceHeader * sequence_header)256 bool ObuParser::ParseTimingInfo(ObuSequenceHeader* sequence_header) {
257   int64_t scratch;
258   OBU_READ_BIT_OR_FAIL;
259   sequence_header->timing_info_present_flag = scratch != 0;
260   if (!sequence_header->timing_info_present_flag) return true;
261   TimingInfo* const info = &sequence_header->timing_info;
262   OBU_READ_LITERAL_OR_FAIL(32);
263   info->num_units_in_tick = static_cast<uint32_t>(scratch);
264   if (info->num_units_in_tick == 0) {
265     LIBGAV1_DLOG(ERROR, "num_units_in_tick is 0.");
266     return false;
267   }
268   OBU_READ_LITERAL_OR_FAIL(32);
269   info->time_scale = static_cast<uint32_t>(scratch);
270   if (info->time_scale == 0) {
271     LIBGAV1_DLOG(ERROR, "time_scale is 0.");
272     return false;
273   }
274   OBU_READ_BIT_OR_FAIL;
275   info->equal_picture_interval = scratch != 0;
276   if (info->equal_picture_interval) {
277     OBU_READ_UVLC_OR_FAIL(info->num_ticks_per_picture);
278     ++info->num_ticks_per_picture;
279   }
280   return true;
281 }
282 
ParseDecoderModelInfo(ObuSequenceHeader * sequence_header)283 bool ObuParser::ParseDecoderModelInfo(ObuSequenceHeader* sequence_header) {
284   if (!sequence_header->timing_info_present_flag) return true;
285   int64_t scratch;
286   OBU_READ_BIT_OR_FAIL;
287   sequence_header->decoder_model_info_present_flag = scratch != 0;
288   if (!sequence_header->decoder_model_info_present_flag) return true;
289   DecoderModelInfo* const info = &sequence_header->decoder_model_info;
290   OBU_READ_LITERAL_OR_FAIL(5);
291   info->encoder_decoder_buffer_delay_length = 1 + scratch;
292   OBU_READ_LITERAL_OR_FAIL(32);
293   info->num_units_in_decoding_tick = static_cast<uint32_t>(scratch);
294   OBU_READ_LITERAL_OR_FAIL(5);
295   info->buffer_removal_time_length = 1 + scratch;
296   OBU_READ_LITERAL_OR_FAIL(5);
297   info->frame_presentation_time_length = 1 + scratch;
298   return true;
299 }
300 
ParseOperatingParameters(ObuSequenceHeader * sequence_header,int index)301 bool ObuParser::ParseOperatingParameters(ObuSequenceHeader* sequence_header,
302                                          int index) {
303   int64_t scratch;
304   OBU_READ_BIT_OR_FAIL;
305   sequence_header->decoder_model_present_for_operating_point[index] =
306       scratch != 0;
307   if (!sequence_header->decoder_model_present_for_operating_point[index]) {
308     return true;
309   }
310   OperatingParameters* const params = &sequence_header->operating_parameters;
311   OBU_READ_LITERAL_OR_FAIL(
312       sequence_header->decoder_model_info.encoder_decoder_buffer_delay_length);
313   params->decoder_buffer_delay[index] = static_cast<uint32_t>(scratch);
314   OBU_READ_LITERAL_OR_FAIL(
315       sequence_header->decoder_model_info.encoder_decoder_buffer_delay_length);
316   params->encoder_buffer_delay[index] = static_cast<uint32_t>(scratch);
317   OBU_READ_BIT_OR_FAIL;
318   params->low_delay_mode_flag[index] = scratch != 0;
319   return true;
320 }
321 
ParseSequenceHeader(bool seen_frame_header)322 bool ObuParser::ParseSequenceHeader(bool seen_frame_header) {
323   ObuSequenceHeader sequence_header = {};
324   int64_t scratch;
325   OBU_READ_LITERAL_OR_FAIL(3);
326   if (scratch >= kMaxProfiles) {
327     LIBGAV1_DLOG(ERROR, "Invalid profile: %d.", static_cast<int>(scratch));
328     return false;
329   }
330   sequence_header.profile = static_cast<BitstreamProfile>(scratch);
331   OBU_READ_BIT_OR_FAIL;
332   sequence_header.still_picture = scratch != 0;
333   OBU_READ_BIT_OR_FAIL;
334   sequence_header.reduced_still_picture_header = scratch != 0;
335   if (sequence_header.reduced_still_picture_header) {
336     if (!sequence_header.still_picture) {
337       LIBGAV1_DLOG(
338           ERROR, "reduced_still_picture_header is 1, but still_picture is 0.");
339       return false;
340     }
341     sequence_header.operating_points = 1;
342     sequence_header.operating_point_idc[0] = 0;
343     OBU_READ_LITERAL_OR_FAIL(5);
344     ParseBitStreamLevel(&sequence_header.level[0], scratch);
345   } else {
346     if (!ParseTimingInfo(&sequence_header) ||
347         !ParseDecoderModelInfo(&sequence_header)) {
348       return false;
349     }
350     OBU_READ_BIT_OR_FAIL;
351     const bool initial_display_delay_present_flag = scratch != 0;
352     OBU_READ_LITERAL_OR_FAIL(5);
353     sequence_header.operating_points = static_cast<int>(1 + scratch);
354     if (operating_point_ >= sequence_header.operating_points) {
355       LIBGAV1_DLOG(
356           ERROR,
357           "Invalid operating point: %d (valid range is [0,%d] inclusive).",
358           operating_point_, sequence_header.operating_points - 1);
359       return false;
360     }
361     for (int i = 0; i < sequence_header.operating_points; ++i) {
362       OBU_READ_LITERAL_OR_FAIL(12);
363       sequence_header.operating_point_idc[i] = static_cast<int>(scratch);
364       for (int j = 0; j < i; ++j) {
365         if (sequence_header.operating_point_idc[i] ==
366             sequence_header.operating_point_idc[j]) {
367           LIBGAV1_DLOG(ERROR,
368                        "operating_point_idc[%d] (%d) is equal to "
369                        "operating_point_idc[%d] (%d).",
370                        i, sequence_header.operating_point_idc[i], j,
371                        sequence_header.operating_point_idc[j]);
372           return false;
373         }
374       }
375       OBU_READ_LITERAL_OR_FAIL(5);
376       ParseBitStreamLevel(&sequence_header.level[i], scratch);
377       if (sequence_header.level[i].major > 3) {
378         OBU_READ_BIT_OR_FAIL;
379         sequence_header.tier[i] = scratch;
380       }
381       if (sequence_header.decoder_model_info_present_flag &&
382           !ParseOperatingParameters(&sequence_header, i)) {
383         return false;
384       }
385       if (initial_display_delay_present_flag) {
386         OBU_READ_BIT_OR_FAIL;
387         if (scratch != 0) {
388           OBU_READ_LITERAL_OR_FAIL(4);
389           sequence_header.initial_display_delay[i] = 1 + scratch;
390         }
391       }
392     }
393   }
394   OBU_READ_LITERAL_OR_FAIL(4);
395   sequence_header.frame_width_bits = 1 + scratch;
396   OBU_READ_LITERAL_OR_FAIL(4);
397   sequence_header.frame_height_bits = 1 + scratch;
398   OBU_READ_LITERAL_OR_FAIL(sequence_header.frame_width_bits);
399   sequence_header.max_frame_width = static_cast<int32_t>(1 + scratch);
400   OBU_READ_LITERAL_OR_FAIL(sequence_header.frame_height_bits);
401   sequence_header.max_frame_height = static_cast<int32_t>(1 + scratch);
402   if (!sequence_header.reduced_still_picture_header) {
403     OBU_READ_BIT_OR_FAIL;
404     sequence_header.frame_id_numbers_present = scratch != 0;
405   }
406   if (sequence_header.frame_id_numbers_present) {
407     OBU_READ_LITERAL_OR_FAIL(4);
408     sequence_header.delta_frame_id_length_bits = 2 + scratch;
409     OBU_READ_LITERAL_OR_FAIL(3);
410     sequence_header.frame_id_length_bits =
411         sequence_header.delta_frame_id_length_bits + 1 + scratch;
412     // Section 6.8.2: It is a requirement of bitstream conformance that the
413     // number of bits needed to read display_frame_id does not exceed 16. This
414     // is equivalent to the constraint that idLen <= 16.
415     if (sequence_header.frame_id_length_bits > 16) {
416       LIBGAV1_DLOG(ERROR, "Invalid frame_id_length_bits: %d.",
417                    sequence_header.frame_id_length_bits);
418       return false;
419     }
420   }
421   OBU_READ_BIT_OR_FAIL;
422   sequence_header.use_128x128_superblock = scratch != 0;
423   OBU_READ_BIT_OR_FAIL;
424   sequence_header.enable_filter_intra = scratch != 0;
425   OBU_READ_BIT_OR_FAIL;
426   sequence_header.enable_intra_edge_filter = scratch != 0;
427   if (sequence_header.reduced_still_picture_header) {
428     sequence_header.force_screen_content_tools = kSelectScreenContentTools;
429     sequence_header.force_integer_mv = kSelectIntegerMv;
430   } else {
431     OBU_READ_BIT_OR_FAIL;
432     sequence_header.enable_interintra_compound = scratch != 0;
433     OBU_READ_BIT_OR_FAIL;
434     sequence_header.enable_masked_compound = scratch != 0;
435     OBU_READ_BIT_OR_FAIL;
436     sequence_header.enable_warped_motion = scratch != 0;
437     OBU_READ_BIT_OR_FAIL;
438     sequence_header.enable_dual_filter = scratch != 0;
439     OBU_READ_BIT_OR_FAIL;
440     sequence_header.enable_order_hint = scratch != 0;
441     if (sequence_header.enable_order_hint) {
442       OBU_READ_BIT_OR_FAIL;
443       sequence_header.enable_jnt_comp = scratch != 0;
444       OBU_READ_BIT_OR_FAIL;
445       sequence_header.enable_ref_frame_mvs = scratch != 0;
446     }
447     OBU_READ_BIT_OR_FAIL;
448     sequence_header.choose_screen_content_tools = scratch != 0;
449     if (sequence_header.choose_screen_content_tools) {
450       sequence_header.force_screen_content_tools = kSelectScreenContentTools;
451     } else {
452       OBU_READ_BIT_OR_FAIL;
453       sequence_header.force_screen_content_tools = scratch;
454     }
455     if (sequence_header.force_screen_content_tools > 0) {
456       OBU_READ_BIT_OR_FAIL;
457       sequence_header.choose_integer_mv = scratch != 0;
458       if (sequence_header.choose_integer_mv) {
459         sequence_header.force_integer_mv = kSelectIntegerMv;
460       } else {
461         OBU_READ_BIT_OR_FAIL;
462         sequence_header.force_integer_mv = scratch;
463       }
464     } else {
465       sequence_header.force_integer_mv = kSelectIntegerMv;
466     }
467     if (sequence_header.enable_order_hint) {
468       OBU_READ_LITERAL_OR_FAIL(3);
469       sequence_header.order_hint_bits = 1 + scratch;
470       sequence_header.order_hint_shift_bits =
471           Mod32(32 - sequence_header.order_hint_bits);
472     }
473   }
474   OBU_READ_BIT_OR_FAIL;
475   sequence_header.enable_superres = scratch != 0;
476   OBU_READ_BIT_OR_FAIL;
477   sequence_header.enable_cdef = scratch != 0;
478   OBU_READ_BIT_OR_FAIL;
479   sequence_header.enable_restoration = scratch != 0;
480   if (!ParseColorConfig(&sequence_header)) return false;
481   OBU_READ_BIT_OR_FAIL;
482   sequence_header.film_grain_params_present = scratch != 0;
483   // Compare new sequence header with old sequence header.
484   if (has_sequence_header_ &&
485       sequence_header.ParametersChanged(sequence_header_)) {
486     // Between the frame header OBU and the last tile group OBU of the frame,
487     // do not allow the sequence header to change.
488     if (seen_frame_header) {
489       LIBGAV1_DLOG(ERROR, "Sequence header changed in the middle of a frame.");
490       return false;
491     }
492     sequence_header_changed_ = true;
493     decoder_state_.ClearReferenceFrames();
494   }
495   sequence_header_ = sequence_header;
496   if (!has_sequence_header_) {
497     sequence_header_changed_ = true;
498   }
499   has_sequence_header_ = true;
500   // Section 6.4.1: It is a requirement of bitstream conformance that if
501   // OperatingPointIdc is equal to 0, then obu_extension_flag is equal to 0 for
502   // all OBUs that follow this sequence header until the next sequence header.
503   extension_disallowed_ =
504       (sequence_header_.operating_point_idc[operating_point_] == 0);
505   return true;
506 }
507 
508 // Marks reference frames as invalid for referencing when they are too far in
509 // the past to be referenced by the frame id mechanism.
MarkInvalidReferenceFrames()510 void ObuParser::MarkInvalidReferenceFrames() {
511   // The current lower bound of the frame ids for reference frames.
512   int lower_bound = decoder_state_.current_frame_id -
513                     (1 << sequence_header_.delta_frame_id_length_bits);
514   // True if lower_bound is smaller than current_frame_id. False if lower_bound
515   // wraps around (in modular arithmetic) to the other side of current_frame_id.
516   bool lower_bound_is_smaller = true;
517   if (lower_bound <= 0) {
518     lower_bound += 1 << sequence_header_.frame_id_length_bits;
519     lower_bound_is_smaller = false;
520   }
521   for (int i = 0; i < kNumReferenceFrameTypes; ++i) {
522     const uint16_t reference_frame_id = decoder_state_.reference_frame_id[i];
523     if (lower_bound_is_smaller) {
524       if (reference_frame_id > decoder_state_.current_frame_id ||
525           reference_frame_id < lower_bound) {
526         decoder_state_.reference_frame[i] = nullptr;
527       }
528     } else {
529       if (reference_frame_id > decoder_state_.current_frame_id &&
530           reference_frame_id < lower_bound) {
531         decoder_state_.reference_frame[i] = nullptr;
532       }
533     }
534   }
535 }
536 
ParseFrameSizeAndRenderSize()537 bool ObuParser::ParseFrameSizeAndRenderSize() {
538   int64_t scratch;
539   // Frame Size.
540   if (frame_header_.frame_size_override_flag) {
541     OBU_READ_LITERAL_OR_FAIL(sequence_header_.frame_width_bits);
542     frame_header_.width = static_cast<int32_t>(1 + scratch);
543     OBU_READ_LITERAL_OR_FAIL(sequence_header_.frame_height_bits);
544     frame_header_.height = static_cast<int32_t>(1 + scratch);
545     if (frame_header_.width > sequence_header_.max_frame_width ||
546         frame_header_.height > sequence_header_.max_frame_height) {
547       LIBGAV1_DLOG(ERROR,
548                    "Frame dimensions are larger than the maximum values");
549       return false;
550     }
551   } else {
552     frame_header_.width = sequence_header_.max_frame_width;
553     frame_header_.height = sequence_header_.max_frame_height;
554   }
555   if (!ParseSuperResParametersAndComputeImageSize()) return false;
556 
557   // Render Size.
558   OBU_READ_BIT_OR_FAIL;
559   frame_header_.render_and_frame_size_different = scratch != 0;
560   if (frame_header_.render_and_frame_size_different) {
561     OBU_READ_LITERAL_OR_FAIL(16);
562     frame_header_.render_width = static_cast<int32_t>(1 + scratch);
563     OBU_READ_LITERAL_OR_FAIL(16);
564     frame_header_.render_height = static_cast<int32_t>(1 + scratch);
565   } else {
566     frame_header_.render_width = frame_header_.upscaled_width;
567     frame_header_.render_height = frame_header_.height;
568   }
569 
570   return true;
571 }
572 
ParseSuperResParametersAndComputeImageSize()573 bool ObuParser::ParseSuperResParametersAndComputeImageSize() {
574   int64_t scratch;
575   // SuperRes.
576   frame_header_.upscaled_width = frame_header_.width;
577   frame_header_.use_superres = false;
578   if (sequence_header_.enable_superres) {
579     OBU_READ_BIT_OR_FAIL;
580     frame_header_.use_superres = scratch != 0;
581   }
582   if (frame_header_.use_superres) {
583     OBU_READ_LITERAL_OR_FAIL(3);
584     // 9 is the smallest value for the denominator.
585     frame_header_.superres_scale_denominator = scratch + 9;
586     frame_header_.width =
587         (frame_header_.upscaled_width * kSuperResScaleNumerator +
588          (frame_header_.superres_scale_denominator / 2)) /
589         frame_header_.superres_scale_denominator;
590   } else {
591     frame_header_.superres_scale_denominator = kSuperResScaleNumerator;
592   }
593   assert(frame_header_.width != 0);
594   assert(frame_header_.height != 0);
595   // Check if multiplying upscaled_width by height would overflow.
596   assert(frame_header_.upscaled_width >= frame_header_.width);
597   if (frame_header_.upscaled_width > INT32_MAX / frame_header_.height) {
598     LIBGAV1_DLOG(ERROR, "Frame dimensions too big: width=%d height=%d.",
599                  frame_header_.width, frame_header_.height);
600     return false;
601   }
602   frame_header_.columns4x4 = ((frame_header_.width + 7) >> 3) << 1;
603   frame_header_.rows4x4 = ((frame_header_.height + 7) >> 3) << 1;
604   return true;
605 }
606 
ValidateInterFrameSize() const607 bool ObuParser::ValidateInterFrameSize() const {
608   for (int index : frame_header_.reference_frame_index) {
609     const RefCountedBuffer* reference_frame =
610         decoder_state_.reference_frame[index].get();
611     if (2 * frame_header_.width < reference_frame->upscaled_width() ||
612         2 * frame_header_.height < reference_frame->frame_height() ||
613         frame_header_.width > 16 * reference_frame->upscaled_width() ||
614         frame_header_.height > 16 * reference_frame->frame_height()) {
615       LIBGAV1_DLOG(ERROR,
616                    "Invalid inter frame size: width=%d, height=%d. Reference "
617                    "frame: index=%d, upscaled width=%d, height=%d.",
618                    frame_header_.width, frame_header_.height, index,
619                    reference_frame->upscaled_width(),
620                    reference_frame->frame_height());
621       return false;
622     }
623   }
624   return true;
625 }
626 
ParseReferenceOrderHint()627 bool ObuParser::ParseReferenceOrderHint() {
628   if (!frame_header_.error_resilient_mode ||
629       !sequence_header_.enable_order_hint) {
630     return true;
631   }
632   int64_t scratch;
633   for (int i = 0; i < kNumReferenceFrameTypes; ++i) {
634     OBU_READ_LITERAL_OR_FAIL(sequence_header_.order_hint_bits);
635     frame_header_.reference_order_hint[i] = scratch;
636     if (frame_header_.reference_order_hint[i] !=
637         decoder_state_.reference_order_hint[i]) {
638       decoder_state_.reference_frame[i] = nullptr;
639     }
640   }
641   return true;
642 }
643 
644 // static
FindLatestBackwardReference(const int current_frame_hint,const std::array<int,kNumReferenceFrameTypes> & shifted_order_hints,const std::array<bool,kNumReferenceFrameTypes> & used_frame)645 int ObuParser::FindLatestBackwardReference(
646     const int current_frame_hint,
647     const std::array<int, kNumReferenceFrameTypes>& shifted_order_hints,
648     const std::array<bool, kNumReferenceFrameTypes>& used_frame) {
649   int ref = -1;
650   int latest_order_hint = INT_MIN;
651   for (int i = 0; i < kNumReferenceFrameTypes; ++i) {
652     const int hint = shifted_order_hints[i];
653     if (!used_frame[i] && hint >= current_frame_hint &&
654         hint >= latest_order_hint) {
655       ref = i;
656       latest_order_hint = hint;
657     }
658   }
659   return ref;
660 }
661 
662 // static
FindEarliestBackwardReference(const int current_frame_hint,const std::array<int,kNumReferenceFrameTypes> & shifted_order_hints,const std::array<bool,kNumReferenceFrameTypes> & used_frame)663 int ObuParser::FindEarliestBackwardReference(
664     const int current_frame_hint,
665     const std::array<int, kNumReferenceFrameTypes>& shifted_order_hints,
666     const std::array<bool, kNumReferenceFrameTypes>& used_frame) {
667   int ref = -1;
668   int earliest_order_hint = INT_MAX;
669   for (int i = 0; i < kNumReferenceFrameTypes; ++i) {
670     const int hint = shifted_order_hints[i];
671     if (!used_frame[i] && hint >= current_frame_hint &&
672         hint < earliest_order_hint) {
673       ref = i;
674       earliest_order_hint = hint;
675     }
676   }
677   return ref;
678 }
679 
680 // static
FindLatestForwardReference(const int current_frame_hint,const std::array<int,kNumReferenceFrameTypes> & shifted_order_hints,const std::array<bool,kNumReferenceFrameTypes> & used_frame)681 int ObuParser::FindLatestForwardReference(
682     const int current_frame_hint,
683     const std::array<int, kNumReferenceFrameTypes>& shifted_order_hints,
684     const std::array<bool, kNumReferenceFrameTypes>& used_frame) {
685   int ref = -1;
686   int latest_order_hint = INT_MIN;
687   for (int i = 0; i < kNumReferenceFrameTypes; ++i) {
688     const int hint = shifted_order_hints[i];
689     if (!used_frame[i] && hint < current_frame_hint &&
690         hint >= latest_order_hint) {
691       ref = i;
692       latest_order_hint = hint;
693     }
694   }
695   return ref;
696 }
697 
698 // static
FindReferenceWithSmallestOutputOrder(const std::array<int,kNumReferenceFrameTypes> & shifted_order_hints)699 int ObuParser::FindReferenceWithSmallestOutputOrder(
700     const std::array<int, kNumReferenceFrameTypes>& shifted_order_hints) {
701   int ref = -1;
702   int earliest_order_hint = INT_MAX;
703   for (int i = 0; i < kNumReferenceFrameTypes; ++i) {
704     const int hint = shifted_order_hints[i];
705     if (hint < earliest_order_hint) {
706       ref = i;
707       earliest_order_hint = hint;
708     }
709   }
710   return ref;
711 }
712 
713 // Computes the elements in the frame_header_.reference_frame_index array
714 // based on:
715 // * the syntax elements last_frame_idx and gold_frame_idx, and
716 // * the values stored within the decoder_state_.reference_order_hint array
717 //   (these values represent the least significant bits of the expected output
718 //   order of the frames).
719 //
720 // Frame type: {
721 //       libgav1_name              spec_name              int
722 //   kReferenceFrameLast,          LAST_FRAME              1
723 //   kReferenceFrameLast2,         LAST2_FRAME             2
724 //   kReferenceFrameLast3,         LAST3_FRAME             3
725 //   kReferenceFrameGolden,        GOLDEN_FRAME            4
726 //   kReferenceFrameBackward,      BWDREF_FRAME            5
727 //   kReferenceFrameAlternate2,    ALTREF2_FRAME           6
728 //   kReferenceFrameAlternate,     ALTREF_FRAME            7
729 // }
730 //
731 // A typical case of a group of pictures (frames) in display order:
732 // (However, more complex cases are possibly allowed in terms of
733 // bitstream conformance.)
734 //
735 // |         |         |         |         |         |         |         |
736 // |         |         |         |         |         |         |         |
737 // |         |         |         |         |         |         |         |
738 // |         |         |         |         |         |         |         |
739 //
740 // 4         3         2         1   current_frame   5         6         7
741 //
SetFrameReferences(const int8_t last_frame_idx,const int8_t gold_frame_idx)742 bool ObuParser::SetFrameReferences(const int8_t last_frame_idx,
743                                    const int8_t gold_frame_idx) {
744   // Set the ref_frame_idx entries for kReferenceFrameLast and
745   // kReferenceFrameGolden to last_frame_idx and gold_frame_idx. Initialize
746   // the other entries to -1.
747   for (int8_t& reference_frame_index : frame_header_.reference_frame_index) {
748     reference_frame_index = -1;
749   }
750   frame_header_
751       .reference_frame_index[kReferenceFrameLast - kReferenceFrameLast] =
752       last_frame_idx;
753   frame_header_
754       .reference_frame_index[kReferenceFrameGolden - kReferenceFrameLast] =
755       gold_frame_idx;
756 
757   // used_frame records which reference frames have been used.
758   std::array<bool, kNumReferenceFrameTypes> used_frame;
759   used_frame.fill(false);
760   used_frame[last_frame_idx] = true;
761   used_frame[gold_frame_idx] = true;
762 
763   assert(sequence_header_.order_hint_bits >= 1);
764   const int current_frame_hint = 1 << (sequence_header_.order_hint_bits - 1);
765   // shifted_order_hints contains the expected output order shifted such that
766   // the current frame has hint equal to current_frame_hint.
767   std::array<int, kNumReferenceFrameTypes> shifted_order_hints;
768   for (int i = 0; i < kNumReferenceFrameTypes; ++i) {
769     const int relative_distance = GetRelativeDistance(
770         decoder_state_.reference_order_hint[i], frame_header_.order_hint,
771         sequence_header_.order_hint_shift_bits);
772     shifted_order_hints[i] = current_frame_hint + relative_distance;
773   }
774 
775   // The expected output orders for kReferenceFrameLast and
776   // kReferenceFrameGolden.
777   const int last_order_hint = shifted_order_hints[last_frame_idx];
778   const int gold_order_hint = shifted_order_hints[gold_frame_idx];
779 
780   // Section 7.8: It is a requirement of bitstream conformance that
781   // lastOrderHint and goldOrderHint are strictly less than curFrameHint.
782   if (last_order_hint >= current_frame_hint ||
783       gold_order_hint >= current_frame_hint) {
784     return false;
785   }
786 
787   // Find a backward reference to the frame with highest output order. If
788   // found, set the kReferenceFrameAlternate reference to that backward
789   // reference.
790   int ref = FindLatestBackwardReference(current_frame_hint, shifted_order_hints,
791                                         used_frame);
792   if (ref >= 0) {
793     frame_header_
794         .reference_frame_index[kReferenceFrameAlternate - kReferenceFrameLast] =
795         ref;
796     used_frame[ref] = true;
797   }
798 
799   // Find a backward reference to the closest frame. If found, set the
800   // kReferenceFrameBackward reference to that backward reference.
801   ref = FindEarliestBackwardReference(current_frame_hint, shifted_order_hints,
802                                       used_frame);
803   if (ref >= 0) {
804     frame_header_
805         .reference_frame_index[kReferenceFrameBackward - kReferenceFrameLast] =
806         ref;
807     used_frame[ref] = true;
808   }
809 
810   // Set the kReferenceFrameAlternate2 reference to the next closest backward
811   // reference.
812   ref = FindEarliestBackwardReference(current_frame_hint, shifted_order_hints,
813                                       used_frame);
814   if (ref >= 0) {
815     frame_header_.reference_frame_index[kReferenceFrameAlternate2 -
816                                         kReferenceFrameLast] = ref;
817     used_frame[ref] = true;
818   }
819 
820   // The remaining references are set to be forward references in
821   // reverse chronological order.
822   static constexpr ReferenceFrameType
823       kRefFrameList[kNumInterReferenceFrameTypes - 2] = {
824           kReferenceFrameLast2, kReferenceFrameLast3, kReferenceFrameBackward,
825           kReferenceFrameAlternate2, kReferenceFrameAlternate};
826   for (const ReferenceFrameType ref_frame : kRefFrameList) {
827     if (frame_header_.reference_frame_index[ref_frame - kReferenceFrameLast] <
828         0) {
829       ref = FindLatestForwardReference(current_frame_hint, shifted_order_hints,
830                                        used_frame);
831       if (ref >= 0) {
832         frame_header_.reference_frame_index[ref_frame - kReferenceFrameLast] =
833             ref;
834         used_frame[ref] = true;
835       }
836     }
837   }
838 
839   // Finally, any remaining references are set to the reference frame with
840   // smallest output order.
841   ref = FindReferenceWithSmallestOutputOrder(shifted_order_hints);
842   assert(ref >= 0);
843   for (int8_t& reference_frame_index : frame_header_.reference_frame_index) {
844     if (reference_frame_index < 0) {
845       reference_frame_index = ref;
846     }
847   }
848 
849   return true;
850 }
851 
ParseLoopFilterParameters()852 bool ObuParser::ParseLoopFilterParameters() {
853   LoopFilter* const loop_filter = &frame_header_.loop_filter;
854   if (frame_header_.coded_lossless || frame_header_.allow_intrabc) {
855     SetDefaultRefDeltas(loop_filter);
856     return true;
857   }
858   // IsIntraFrame implies kPrimaryReferenceNone.
859   assert(!IsIntraFrame(frame_header_.frame_type) ||
860          frame_header_.primary_reference_frame == kPrimaryReferenceNone);
861   if (frame_header_.primary_reference_frame == kPrimaryReferenceNone) {
862     // Part of the setup_past_independence() function in the spec. It is not
863     // necessary to set loop_filter->delta_enabled to true. See
864     // https://crbug.com/aomedia/2305.
865     SetDefaultRefDeltas(loop_filter);
866   } else {
867     // Part of the load_previous() function in the spec.
868     const int prev_frame_index =
869         frame_header_
870             .reference_frame_index[frame_header_.primary_reference_frame];
871     const RefCountedBuffer* prev_frame =
872         decoder_state_.reference_frame[prev_frame_index].get();
873     loop_filter->ref_deltas = prev_frame->loop_filter_ref_deltas();
874     loop_filter->mode_deltas = prev_frame->loop_filter_mode_deltas();
875   }
876   int64_t scratch;
877   for (int i = 0; i < 2; ++i) {
878     OBU_READ_LITERAL_OR_FAIL(6);
879     loop_filter->level[i] = scratch;
880   }
881   if (!sequence_header_.color_config.is_monochrome &&
882       (loop_filter->level[0] != 0 || loop_filter->level[1] != 0)) {
883     for (int i = 2; i < 4; ++i) {
884       OBU_READ_LITERAL_OR_FAIL(6);
885       loop_filter->level[i] = scratch;
886     }
887   }
888   OBU_READ_LITERAL_OR_FAIL(3);
889   loop_filter->sharpness = scratch;
890   OBU_READ_BIT_OR_FAIL;
891   loop_filter->delta_enabled = scratch != 0;
892   if (loop_filter->delta_enabled) {
893     OBU_READ_BIT_OR_FAIL;
894     loop_filter->delta_update = scratch != 0;
895     if (loop_filter->delta_update) {
896       for (auto& ref_delta : loop_filter->ref_deltas) {
897         OBU_READ_BIT_OR_FAIL;
898         const bool update_ref_delta = scratch != 0;
899         if (update_ref_delta) {
900           int scratch_int;
901           if (!bit_reader_->ReadInverseSignedLiteral(6, &scratch_int)) {
902             LIBGAV1_DLOG(ERROR, "Not enough bits.");
903             return false;
904           }
905           ref_delta = scratch_int;
906         }
907       }
908       for (auto& mode_delta : loop_filter->mode_deltas) {
909         OBU_READ_BIT_OR_FAIL;
910         const bool update_mode_delta = scratch != 0;
911         if (update_mode_delta) {
912           int scratch_int;
913           if (!bit_reader_->ReadInverseSignedLiteral(6, &scratch_int)) {
914             LIBGAV1_DLOG(ERROR, "Not enough bits.");
915             return false;
916           }
917           mode_delta = scratch_int;
918         }
919       }
920     }
921   } else {
922     loop_filter->delta_update = false;
923   }
924   return true;
925 }
926 
ParseDeltaQuantizer(int8_t * const delta)927 bool ObuParser::ParseDeltaQuantizer(int8_t* const delta) {
928   int64_t scratch;
929   *delta = 0;
930   OBU_READ_BIT_OR_FAIL;
931   const bool delta_coded = scratch != 0;
932   if (delta_coded) {
933     int scratch_int;
934     if (!bit_reader_->ReadInverseSignedLiteral(6, &scratch_int)) {
935       LIBGAV1_DLOG(ERROR, "Not enough bits.");
936       return false;
937     }
938     *delta = scratch_int;
939   }
940   return true;
941 }
942 
ParseQuantizerParameters()943 bool ObuParser::ParseQuantizerParameters() {
944   int64_t scratch;
945   QuantizerParameters* const quantizer = &frame_header_.quantizer;
946   OBU_READ_LITERAL_OR_FAIL(8);
947   quantizer->base_index = scratch;
948   if (!ParseDeltaQuantizer(&quantizer->delta_dc[kPlaneY])) return false;
949   if (!sequence_header_.color_config.is_monochrome) {
950     bool diff_uv_delta = false;
951     if (sequence_header_.color_config.separate_uv_delta_q) {
952       OBU_READ_BIT_OR_FAIL;
953       diff_uv_delta = scratch != 0;
954     }
955     if (!ParseDeltaQuantizer(&quantizer->delta_dc[kPlaneU]) ||
956         !ParseDeltaQuantizer(&quantizer->delta_ac[kPlaneU])) {
957       return false;
958     }
959     if (diff_uv_delta) {
960       if (!ParseDeltaQuantizer(&quantizer->delta_dc[kPlaneV]) ||
961           !ParseDeltaQuantizer(&quantizer->delta_ac[kPlaneV])) {
962         return false;
963       }
964     } else {
965       quantizer->delta_dc[kPlaneV] = quantizer->delta_dc[kPlaneU];
966       quantizer->delta_ac[kPlaneV] = quantizer->delta_ac[kPlaneU];
967     }
968   }
969   OBU_READ_BIT_OR_FAIL;
970   quantizer->use_matrix = scratch != 0;
971   if (quantizer->use_matrix) {
972     OBU_READ_LITERAL_OR_FAIL(4);
973     quantizer->matrix_level[kPlaneY] = scratch;
974     OBU_READ_LITERAL_OR_FAIL(4);
975     quantizer->matrix_level[kPlaneU] = scratch;
976     if (sequence_header_.color_config.separate_uv_delta_q) {
977       OBU_READ_LITERAL_OR_FAIL(4);
978       quantizer->matrix_level[kPlaneV] = scratch;
979     } else {
980       quantizer->matrix_level[kPlaneV] = quantizer->matrix_level[kPlaneU];
981     }
982   }
983   return true;
984 }
985 
986 // This method implements the following functions in the spec:
987 // - segmentation_params()
988 // - part of setup_past_independence(): Set the FeatureData and FeatureEnabled
989 //   arrays to all 0.
990 // - part of load_previous(): Call load_segmentation_params().
991 //
992 // A careful analysis of the spec shows the part of setup_past_independence()
993 // can be optimized away and the part of load_previous() only needs to be
994 // invoked under a specific condition. Although the logic looks different from
995 // the spec, it is equivalent and more efficient.
ParseSegmentationParameters()996 bool ObuParser::ParseSegmentationParameters() {
997   int64_t scratch;
998   Segmentation* const segmentation = &frame_header_.segmentation;
999   OBU_READ_BIT_OR_FAIL;
1000   segmentation->enabled = scratch != 0;
1001   if (!segmentation->enabled) return true;
1002   if (frame_header_.primary_reference_frame == kPrimaryReferenceNone) {
1003     segmentation->update_map = true;
1004     segmentation->update_data = true;
1005   } else {
1006     OBU_READ_BIT_OR_FAIL;
1007     segmentation->update_map = scratch != 0;
1008     if (segmentation->update_map) {
1009       OBU_READ_BIT_OR_FAIL;
1010       segmentation->temporal_update = scratch != 0;
1011     }
1012     OBU_READ_BIT_OR_FAIL;
1013     segmentation->update_data = scratch != 0;
1014     if (!segmentation->update_data) {
1015       // Part of the load_previous() function in the spec.
1016       const int prev_frame_index =
1017           frame_header_
1018               .reference_frame_index[frame_header_.primary_reference_frame];
1019       decoder_state_.reference_frame[prev_frame_index]
1020           ->GetSegmentationParameters(segmentation);
1021       return true;
1022     }
1023   }
1024   for (int8_t i = 0; i < kMaxSegments; ++i) {
1025     for (int8_t j = 0; j < kSegmentFeatureMax; ++j) {
1026       OBU_READ_BIT_OR_FAIL;
1027       segmentation->feature_enabled[i][j] = scratch != 0;
1028       if (segmentation->feature_enabled[i][j]) {
1029         if (Segmentation::FeatureSigned(static_cast<SegmentFeature>(j))) {
1030           int scratch_int;
1031           if (!bit_reader_->ReadInverseSignedLiteral(
1032                   kSegmentationFeatureBits[j], &scratch_int)) {
1033             LIBGAV1_DLOG(ERROR, "Not enough bits.");
1034             return false;
1035           }
1036           segmentation->feature_data[i][j] =
1037               Clip3(scratch_int, -kSegmentationFeatureMaxValues[j],
1038                     kSegmentationFeatureMaxValues[j]);
1039         } else {
1040           if (kSegmentationFeatureBits[j] > 0) {
1041             OBU_READ_LITERAL_OR_FAIL(kSegmentationFeatureBits[j]);
1042             segmentation->feature_data[i][j] = Clip3(
1043                 static_cast<int>(scratch), 0, kSegmentationFeatureMaxValues[j]);
1044           } else {
1045             segmentation->feature_data[i][j] = 0;
1046           }
1047         }
1048         segmentation->last_active_segment_id = i;
1049         if (j >= kSegmentFeatureReferenceFrame) {
1050           segmentation->segment_id_pre_skip = true;
1051         }
1052       }
1053     }
1054   }
1055   return true;
1056 }
1057 
ParseQuantizerIndexDeltaParameters()1058 bool ObuParser::ParseQuantizerIndexDeltaParameters() {
1059   int64_t scratch;
1060   if (frame_header_.quantizer.base_index > 0) {
1061     OBU_READ_BIT_OR_FAIL;
1062     frame_header_.delta_q.present = scratch != 0;
1063     if (frame_header_.delta_q.present) {
1064       OBU_READ_LITERAL_OR_FAIL(2);
1065       frame_header_.delta_q.scale = scratch;
1066     }
1067   }
1068   return true;
1069 }
1070 
ParseLoopFilterDeltaParameters()1071 bool ObuParser::ParseLoopFilterDeltaParameters() {
1072   int64_t scratch;
1073   if (frame_header_.delta_q.present) {
1074     if (!frame_header_.allow_intrabc) {
1075       OBU_READ_BIT_OR_FAIL;
1076       frame_header_.delta_lf.present = scratch != 0;
1077     }
1078     if (frame_header_.delta_lf.present) {
1079       OBU_READ_LITERAL_OR_FAIL(2);
1080       frame_header_.delta_lf.scale = scratch;
1081       OBU_READ_BIT_OR_FAIL;
1082       frame_header_.delta_lf.multi = scratch != 0;
1083     }
1084   }
1085   return true;
1086 }
1087 
ComputeSegmentLosslessAndQIndex()1088 void ObuParser::ComputeSegmentLosslessAndQIndex() {
1089   frame_header_.coded_lossless = true;
1090   Segmentation* const segmentation = &frame_header_.segmentation;
1091   const QuantizerParameters* const quantizer = &frame_header_.quantizer;
1092   for (int i = 0; i < kMaxSegments; ++i) {
1093     segmentation->qindex[i] =
1094         GetQIndex(*segmentation, i, quantizer->base_index);
1095     segmentation->lossless[i] =
1096         segmentation->qindex[i] == 0 && quantizer->delta_dc[kPlaneY] == 0 &&
1097         quantizer->delta_dc[kPlaneU] == 0 &&
1098         quantizer->delta_ac[kPlaneU] == 0 &&
1099         quantizer->delta_dc[kPlaneV] == 0 && quantizer->delta_ac[kPlaneV] == 0;
1100     if (!segmentation->lossless[i]) frame_header_.coded_lossless = false;
1101     // The spec calls for setting up a two-dimensional SegQMLevel array here.
1102     // We avoid the SegQMLevel array by using segmentation->lossless[i] and
1103     // quantizer->matrix_level[plane] directly in the reconstruct process of
1104     // Section 7.12.3.
1105   }
1106   frame_header_.upscaled_lossless =
1107       frame_header_.coded_lossless &&
1108       frame_header_.width == frame_header_.upscaled_width;
1109 }
1110 
ParseCdefParameters()1111 bool ObuParser::ParseCdefParameters() {
1112   const int coeff_shift = sequence_header_.color_config.bitdepth - 8;
1113   if (frame_header_.coded_lossless || frame_header_.allow_intrabc ||
1114       !sequence_header_.enable_cdef) {
1115     frame_header_.cdef.damping = 3 + coeff_shift;
1116     return true;
1117   }
1118   Cdef* const cdef = &frame_header_.cdef;
1119   int64_t scratch;
1120   OBU_READ_LITERAL_OR_FAIL(2);
1121   cdef->damping = scratch + 3 + coeff_shift;
1122   OBU_READ_LITERAL_OR_FAIL(2);
1123   cdef->bits = scratch;
1124   for (int i = 0; i < (1 << cdef->bits); ++i) {
1125     OBU_READ_LITERAL_OR_FAIL(4);
1126     cdef->y_primary_strength[i] = scratch << coeff_shift;
1127     OBU_READ_LITERAL_OR_FAIL(2);
1128     cdef->y_secondary_strength[i] = scratch;
1129     if (cdef->y_secondary_strength[i] == 3) ++cdef->y_secondary_strength[i];
1130     cdef->y_secondary_strength[i] <<= coeff_shift;
1131     if (sequence_header_.color_config.is_monochrome) continue;
1132     OBU_READ_LITERAL_OR_FAIL(4);
1133     cdef->uv_primary_strength[i] = scratch << coeff_shift;
1134     OBU_READ_LITERAL_OR_FAIL(2);
1135     cdef->uv_secondary_strength[i] = scratch;
1136     if (cdef->uv_secondary_strength[i] == 3) ++cdef->uv_secondary_strength[i];
1137     cdef->uv_secondary_strength[i] <<= coeff_shift;
1138   }
1139   return true;
1140 }
1141 
ParseLoopRestorationParameters()1142 bool ObuParser::ParseLoopRestorationParameters() {
1143   if (frame_header_.upscaled_lossless || frame_header_.allow_intrabc ||
1144       !sequence_header_.enable_restoration) {
1145     return true;
1146   }
1147   int64_t scratch;
1148   bool uses_loop_restoration = false;
1149   bool uses_chroma_loop_restoration = false;
1150   LoopRestoration* const loop_restoration = &frame_header_.loop_restoration;
1151   const int num_planes = sequence_header_.color_config.is_monochrome
1152                              ? kMaxPlanesMonochrome
1153                              : kMaxPlanes;
1154   for (int i = 0; i < num_planes; ++i) {
1155     OBU_READ_LITERAL_OR_FAIL(2);
1156     loop_restoration->type[i] = static_cast<LoopRestorationType>(scratch);
1157     if (loop_restoration->type[i] != kLoopRestorationTypeNone) {
1158       uses_loop_restoration = true;
1159       if (i > 0) uses_chroma_loop_restoration = true;
1160     }
1161   }
1162   if (uses_loop_restoration) {
1163     uint8_t unit_shift;
1164     if (sequence_header_.use_128x128_superblock) {
1165       OBU_READ_BIT_OR_FAIL;
1166       unit_shift = scratch + 1;
1167     } else {
1168       OBU_READ_BIT_OR_FAIL;
1169       unit_shift = scratch;
1170       if (unit_shift != 0) {
1171         OBU_READ_BIT_OR_FAIL;
1172         const uint8_t unit_extra_shift = scratch;
1173         unit_shift += unit_extra_shift;
1174       }
1175     }
1176     loop_restoration->unit_size_log2[kPlaneY] = 6 + unit_shift;
1177     uint8_t uv_shift = 0;
1178     if (sequence_header_.color_config.subsampling_x != 0 &&
1179         sequence_header_.color_config.subsampling_y != 0 &&
1180         uses_chroma_loop_restoration) {
1181       OBU_READ_BIT_OR_FAIL;
1182       uv_shift = scratch;
1183     }
1184     loop_restoration->unit_size_log2[kPlaneU] =
1185         loop_restoration->unit_size_log2[kPlaneV] =
1186             loop_restoration->unit_size_log2[0] - uv_shift;
1187   }
1188   return true;
1189 }
1190 
ParseTxModeSyntax()1191 bool ObuParser::ParseTxModeSyntax() {
1192   if (frame_header_.coded_lossless) {
1193     frame_header_.tx_mode = kTxModeOnly4x4;
1194     return true;
1195   }
1196   int64_t scratch;
1197   OBU_READ_BIT_OR_FAIL;
1198   frame_header_.tx_mode = (scratch == 1) ? kTxModeSelect : kTxModeLargest;
1199   return true;
1200 }
1201 
ParseFrameReferenceModeSyntax()1202 bool ObuParser::ParseFrameReferenceModeSyntax() {
1203   int64_t scratch;
1204   if (!IsIntraFrame(frame_header_.frame_type)) {
1205     OBU_READ_BIT_OR_FAIL;
1206     frame_header_.reference_mode_select = scratch != 0;
1207   }
1208   return true;
1209 }
1210 
IsSkipModeAllowed()1211 bool ObuParser::IsSkipModeAllowed() {
1212   if (IsIntraFrame(frame_header_.frame_type) ||
1213       !frame_header_.reference_mode_select ||
1214       !sequence_header_.enable_order_hint) {
1215     return false;
1216   }
1217   // Identify the nearest forward and backward references.
1218   int forward_index = -1;
1219   int backward_index = -1;
1220   int forward_hint = -1;
1221   int backward_hint = -1;
1222   for (int i = 0; i < kNumInterReferenceFrameTypes; ++i) {
1223     const unsigned int reference_hint =
1224         decoder_state_
1225             .reference_order_hint[frame_header_.reference_frame_index[i]];
1226     // TODO(linfengz): |relative_distance| equals
1227     // current_frame_->reference_info()->
1228     //     relative_distance_from[i + kReferenceFrameLast];
1229     // However, the unit test ObuParserTest.SkipModeParameters() would fail.
1230     // Will figure out how to initialize |current_frame_.reference_info_| in the
1231     // RefCountedBuffer later.
1232     const int relative_distance =
1233         GetRelativeDistance(reference_hint, frame_header_.order_hint,
1234                             sequence_header_.order_hint_shift_bits);
1235     if (relative_distance < 0) {
1236       if (forward_index < 0 ||
1237           GetRelativeDistance(reference_hint, forward_hint,
1238                               sequence_header_.order_hint_shift_bits) > 0) {
1239         forward_index = i;
1240         forward_hint = reference_hint;
1241       }
1242     } else if (relative_distance > 0) {
1243       if (backward_index < 0 ||
1244           GetRelativeDistance(reference_hint, backward_hint,
1245                               sequence_header_.order_hint_shift_bits) < 0) {
1246         backward_index = i;
1247         backward_hint = reference_hint;
1248       }
1249     }
1250   }
1251   if (forward_index < 0) return false;
1252   if (backward_index >= 0) {
1253     // Bidirectional prediction.
1254     frame_header_.skip_mode_frame[0] = static_cast<ReferenceFrameType>(
1255         kReferenceFrameLast + std::min(forward_index, backward_index));
1256     frame_header_.skip_mode_frame[1] = static_cast<ReferenceFrameType>(
1257         kReferenceFrameLast + std::max(forward_index, backward_index));
1258     return true;
1259   }
1260   // Forward prediction only. Identify the second nearest forward reference.
1261   int second_forward_index = -1;
1262   int second_forward_hint = -1;
1263   for (int i = 0; i < kNumInterReferenceFrameTypes; ++i) {
1264     const unsigned int reference_hint =
1265         decoder_state_
1266             .reference_order_hint[frame_header_.reference_frame_index[i]];
1267     if (GetRelativeDistance(reference_hint, forward_hint,
1268                             sequence_header_.order_hint_shift_bits) < 0) {
1269       if (second_forward_index < 0 ||
1270           GetRelativeDistance(reference_hint, second_forward_hint,
1271                               sequence_header_.order_hint_shift_bits) > 0) {
1272         second_forward_index = i;
1273         second_forward_hint = reference_hint;
1274       }
1275     }
1276   }
1277   if (second_forward_index < 0) return false;
1278   frame_header_.skip_mode_frame[0] = static_cast<ReferenceFrameType>(
1279       kReferenceFrameLast + std::min(forward_index, second_forward_index));
1280   frame_header_.skip_mode_frame[1] = static_cast<ReferenceFrameType>(
1281       kReferenceFrameLast + std::max(forward_index, second_forward_index));
1282   return true;
1283 }
1284 
ParseSkipModeParameters()1285 bool ObuParser::ParseSkipModeParameters() {
1286   if (!IsSkipModeAllowed()) return true;
1287   int64_t scratch;
1288   OBU_READ_BIT_OR_FAIL;
1289   frame_header_.skip_mode_present = scratch != 0;
1290   return true;
1291 }
1292 
1293 // Sets frame_header_.global_motion[ref].params[index].
ParseGlobalParamSyntax(int ref,int index,const std::array<GlobalMotion,kNumReferenceFrameTypes> & prev_global_motions)1294 bool ObuParser::ParseGlobalParamSyntax(
1295     int ref, int index,
1296     const std::array<GlobalMotion, kNumReferenceFrameTypes>&
1297         prev_global_motions) {
1298   GlobalMotion* const global_motion = &frame_header_.global_motion[ref];
1299   const GlobalMotion* const prev_global_motion = &prev_global_motions[ref];
1300   int abs_bits = kGlobalMotionAlphaBits;
1301   int precision_bits = kGlobalMotionAlphaPrecisionBits;
1302   if (index < 2) {
1303     if (global_motion->type == kGlobalMotionTransformationTypeTranslation) {
1304       const auto high_precision_mv_factor =
1305           static_cast<int>(!frame_header_.allow_high_precision_mv);
1306       abs_bits = kGlobalMotionTranslationOnlyBits - high_precision_mv_factor;
1307       precision_bits =
1308           kGlobalMotionTranslationOnlyPrecisionBits - high_precision_mv_factor;
1309     } else {
1310       abs_bits = kGlobalMotionTranslationBits;
1311       precision_bits = kGlobalMotionTranslationPrecisionBits;
1312     }
1313   }
1314   const int precision_diff = kWarpedModelPrecisionBits - precision_bits;
1315   const int round = (index % 3 == 2) ? 1 << kWarpedModelPrecisionBits : 0;
1316   const int sub = (index % 3 == 2) ? 1 << precision_bits : 0;
1317   const int mx = 1 << abs_bits;
1318   const int reference =
1319       (prev_global_motion->params[index] >> precision_diff) - sub;
1320   int scratch;
1321   if (!bit_reader_->DecodeSignedSubexpWithReference(
1322           -mx, mx + 1, reference, kGlobalMotionReadControl, &scratch)) {
1323     LIBGAV1_DLOG(ERROR, "Not enough bits.");
1324     return false;
1325   }
1326   global_motion->params[index] = LeftShift(scratch, precision_diff) + round;
1327   return true;
1328 }
1329 
ParseGlobalMotionParameters()1330 bool ObuParser::ParseGlobalMotionParameters() {
1331   for (int ref = kReferenceFrameLast; ref <= kReferenceFrameAlternate; ++ref) {
1332     frame_header_.global_motion[ref].type =
1333         kGlobalMotionTransformationTypeIdentity;
1334     for (int i = 0; i < 6; ++i) {
1335       frame_header_.global_motion[ref].params[i] =
1336           (i % 3 == 2) ? 1 << kWarpedModelPrecisionBits : 0;
1337     }
1338   }
1339   if (IsIntraFrame(frame_header_.frame_type)) return true;
1340   const std::array<GlobalMotion, kNumReferenceFrameTypes>* prev_global_motions =
1341       nullptr;
1342   if (frame_header_.primary_reference_frame == kPrimaryReferenceNone) {
1343     // Part of the setup_past_independence() function in the spec. The value
1344     // that the spec says PrevGmParams[ref][i] should be set to is exactly
1345     // the value frame_header_.global_motion[ref].params[i] is set to by the
1346     // for loop above. Therefore prev_global_motions can simply point to
1347     // frame_header_.global_motion.
1348     prev_global_motions = &frame_header_.global_motion;
1349   } else {
1350     // Part of the load_previous() function in the spec.
1351     const int prev_frame_index =
1352         frame_header_
1353             .reference_frame_index[frame_header_.primary_reference_frame];
1354     prev_global_motions =
1355         &decoder_state_.reference_frame[prev_frame_index]->GlobalMotions();
1356   }
1357   for (int ref = kReferenceFrameLast; ref <= kReferenceFrameAlternate; ++ref) {
1358     GlobalMotion* const global_motion = &frame_header_.global_motion[ref];
1359     int64_t scratch;
1360     OBU_READ_BIT_OR_FAIL;
1361     const bool is_global = scratch != 0;
1362     if (is_global) {
1363       OBU_READ_BIT_OR_FAIL;
1364       const bool is_rot_zoom = scratch != 0;
1365       if (is_rot_zoom) {
1366         global_motion->type = kGlobalMotionTransformationTypeRotZoom;
1367       } else {
1368         OBU_READ_BIT_OR_FAIL;
1369         const bool is_translation = scratch != 0;
1370         global_motion->type = is_translation
1371                                   ? kGlobalMotionTransformationTypeTranslation
1372                                   : kGlobalMotionTransformationTypeAffine;
1373       }
1374     } else {
1375       global_motion->type = kGlobalMotionTransformationTypeIdentity;
1376     }
1377     if (global_motion->type >= kGlobalMotionTransformationTypeRotZoom) {
1378       if (!ParseGlobalParamSyntax(ref, 2, *prev_global_motions) ||
1379           !ParseGlobalParamSyntax(ref, 3, *prev_global_motions)) {
1380         return false;
1381       }
1382       if (global_motion->type == kGlobalMotionTransformationTypeAffine) {
1383         if (!ParseGlobalParamSyntax(ref, 4, *prev_global_motions) ||
1384             !ParseGlobalParamSyntax(ref, 5, *prev_global_motions)) {
1385           return false;
1386         }
1387       } else {
1388         global_motion->params[4] = -global_motion->params[3];
1389         global_motion->params[5] = global_motion->params[2];
1390       }
1391     }
1392     if (global_motion->type >= kGlobalMotionTransformationTypeTranslation) {
1393       if (!ParseGlobalParamSyntax(ref, 0, *prev_global_motions) ||
1394           !ParseGlobalParamSyntax(ref, 1, *prev_global_motions)) {
1395         return false;
1396       }
1397     }
1398   }
1399   return true;
1400 }
1401 
ParseFilmGrainParameters()1402 bool ObuParser::ParseFilmGrainParameters() {
1403   if (!sequence_header_.film_grain_params_present ||
1404       (!frame_header_.show_frame && !frame_header_.showable_frame)) {
1405     // frame_header_.film_grain_params is already zero-initialized.
1406     return true;
1407   }
1408 
1409   FilmGrainParams& film_grain_params = frame_header_.film_grain_params;
1410   int64_t scratch;
1411   OBU_READ_BIT_OR_FAIL;
1412   film_grain_params.apply_grain = scratch != 0;
1413   if (!film_grain_params.apply_grain) {
1414     // film_grain_params is already zero-initialized.
1415     return true;
1416   }
1417 
1418   OBU_READ_LITERAL_OR_FAIL(16);
1419   film_grain_params.grain_seed = static_cast<int>(scratch);
1420   film_grain_params.update_grain = true;
1421   if (frame_header_.frame_type == kFrameInter) {
1422     OBU_READ_BIT_OR_FAIL;
1423     film_grain_params.update_grain = scratch != 0;
1424   }
1425   if (!film_grain_params.update_grain) {
1426     OBU_READ_LITERAL_OR_FAIL(3);
1427     film_grain_params.reference_index = static_cast<int>(scratch);
1428     bool found = false;
1429     for (const auto index : frame_header_.reference_frame_index) {
1430       if (film_grain_params.reference_index == index) {
1431         found = true;
1432         break;
1433       }
1434     }
1435     if (!found) {
1436       static_assert(sizeof(frame_header_.reference_frame_index) /
1437                             sizeof(frame_header_.reference_frame_index[0]) ==
1438                         7,
1439                     "");
1440       LIBGAV1_DLOG(ERROR,
1441                    "Invalid value for film_grain_params_ref_idx (%d). "
1442                    "ref_frame_idx = {%d, %d, %d, %d, %d, %d, %d}",
1443                    film_grain_params.reference_index,
1444                    frame_header_.reference_frame_index[0],
1445                    frame_header_.reference_frame_index[1],
1446                    frame_header_.reference_frame_index[2],
1447                    frame_header_.reference_frame_index[3],
1448                    frame_header_.reference_frame_index[4],
1449                    frame_header_.reference_frame_index[5],
1450                    frame_header_.reference_frame_index[6]);
1451       return false;
1452     }
1453     const RefCountedBuffer* grain_params_reference_frame =
1454         decoder_state_.reference_frame[film_grain_params.reference_index].get();
1455     if (grain_params_reference_frame == nullptr) {
1456       LIBGAV1_DLOG(ERROR, "Buffer %d does not contain a decoded frame",
1457                    film_grain_params.reference_index);
1458       return false;
1459     }
1460     const int temp_grain_seed = film_grain_params.grain_seed;
1461     const bool temp_update_grain = film_grain_params.update_grain;
1462     const int temp_reference_index = film_grain_params.reference_index;
1463     film_grain_params = grain_params_reference_frame->film_grain_params();
1464     film_grain_params.grain_seed = temp_grain_seed;
1465     film_grain_params.update_grain = temp_update_grain;
1466     film_grain_params.reference_index = temp_reference_index;
1467     return true;
1468   }
1469 
1470   OBU_READ_LITERAL_OR_FAIL(4);
1471   film_grain_params.num_y_points = scratch;
1472   if (film_grain_params.num_y_points > 14) {
1473     LIBGAV1_DLOG(ERROR, "Invalid value for num_y_points (%d).",
1474                  film_grain_params.num_y_points);
1475     return false;
1476   }
1477   for (int i = 0; i < film_grain_params.num_y_points; ++i) {
1478     OBU_READ_LITERAL_OR_FAIL(8);
1479     film_grain_params.point_y_value[i] = scratch;
1480     if (i != 0 && film_grain_params.point_y_value[i - 1] >=
1481                       film_grain_params.point_y_value[i]) {
1482       LIBGAV1_DLOG(ERROR, "point_y_value[%d] (%d) >= point_y_value[%d] (%d).",
1483                    i - 1, film_grain_params.point_y_value[i - 1], i,
1484                    film_grain_params.point_y_value[i]);
1485       return false;
1486     }
1487     OBU_READ_LITERAL_OR_FAIL(8);
1488     film_grain_params.point_y_scaling[i] = scratch;
1489   }
1490   if (sequence_header_.color_config.is_monochrome) {
1491     film_grain_params.chroma_scaling_from_luma = false;
1492   } else {
1493     OBU_READ_BIT_OR_FAIL;
1494     film_grain_params.chroma_scaling_from_luma = scratch != 0;
1495   }
1496   if (sequence_header_.color_config.is_monochrome ||
1497       film_grain_params.chroma_scaling_from_luma ||
1498       (sequence_header_.color_config.subsampling_x == 1 &&
1499        sequence_header_.color_config.subsampling_y == 1 &&
1500        film_grain_params.num_y_points == 0)) {
1501     film_grain_params.num_u_points = 0;
1502     film_grain_params.num_v_points = 0;
1503   } else {
1504     OBU_READ_LITERAL_OR_FAIL(4);
1505     film_grain_params.num_u_points = scratch;
1506     if (film_grain_params.num_u_points > 10) {
1507       LIBGAV1_DLOG(ERROR, "Invalid value for num_u_points (%d).",
1508                    film_grain_params.num_u_points);
1509       return false;
1510     }
1511     for (int i = 0; i < film_grain_params.num_u_points; ++i) {
1512       OBU_READ_LITERAL_OR_FAIL(8);
1513       film_grain_params.point_u_value[i] = scratch;
1514       if (i != 0 && film_grain_params.point_u_value[i - 1] >=
1515                         film_grain_params.point_u_value[i]) {
1516         LIBGAV1_DLOG(ERROR, "point_u_value[%d] (%d) >= point_u_value[%d] (%d).",
1517                      i - 1, film_grain_params.point_u_value[i - 1], i,
1518                      film_grain_params.point_u_value[i]);
1519         return false;
1520       }
1521       OBU_READ_LITERAL_OR_FAIL(8);
1522       film_grain_params.point_u_scaling[i] = scratch;
1523     }
1524     OBU_READ_LITERAL_OR_FAIL(4);
1525     film_grain_params.num_v_points = scratch;
1526     if (film_grain_params.num_v_points > 10) {
1527       LIBGAV1_DLOG(ERROR, "Invalid value for num_v_points (%d).",
1528                    film_grain_params.num_v_points);
1529       return false;
1530     }
1531     if (sequence_header_.color_config.subsampling_x == 1 &&
1532         sequence_header_.color_config.subsampling_y == 1 &&
1533         (film_grain_params.num_u_points == 0) !=
1534             (film_grain_params.num_v_points == 0)) {
1535       LIBGAV1_DLOG(ERROR,
1536                    "Invalid values for num_u_points (%d) and num_v_points (%d) "
1537                    "for 4:2:0 chroma subsampling.",
1538                    film_grain_params.num_u_points,
1539                    film_grain_params.num_v_points);
1540       return false;
1541     }
1542     for (int i = 0; i < film_grain_params.num_v_points; ++i) {
1543       OBU_READ_LITERAL_OR_FAIL(8);
1544       film_grain_params.point_v_value[i] = scratch;
1545       if (i != 0 && film_grain_params.point_v_value[i - 1] >=
1546                         film_grain_params.point_v_value[i]) {
1547         LIBGAV1_DLOG(ERROR, "point_v_value[%d] (%d) >= point_v_value[%d] (%d).",
1548                      i - 1, film_grain_params.point_v_value[i - 1], i,
1549                      film_grain_params.point_v_value[i]);
1550         return false;
1551       }
1552       OBU_READ_LITERAL_OR_FAIL(8);
1553       film_grain_params.point_v_scaling[i] = scratch;
1554     }
1555   }
1556   OBU_READ_LITERAL_OR_FAIL(2);
1557   film_grain_params.chroma_scaling = scratch + 8;
1558   OBU_READ_LITERAL_OR_FAIL(2);
1559   film_grain_params.auto_regression_coeff_lag = scratch;
1560 
1561   const int num_pos_y =
1562       MultiplyBy2(film_grain_params.auto_regression_coeff_lag) *
1563       (film_grain_params.auto_regression_coeff_lag + 1);
1564   int num_pos_uv = num_pos_y;
1565   if (film_grain_params.num_y_points > 0) {
1566     ++num_pos_uv;
1567     for (int i = 0; i < num_pos_y; ++i) {
1568       OBU_READ_LITERAL_OR_FAIL(8);
1569       film_grain_params.auto_regression_coeff_y[i] =
1570           static_cast<int8_t>(scratch - 128);
1571     }
1572   }
1573   if (film_grain_params.chroma_scaling_from_luma ||
1574       film_grain_params.num_u_points > 0) {
1575     for (int i = 0; i < num_pos_uv; ++i) {
1576       OBU_READ_LITERAL_OR_FAIL(8);
1577       film_grain_params.auto_regression_coeff_u[i] =
1578           static_cast<int8_t>(scratch - 128);
1579     }
1580   }
1581   if (film_grain_params.chroma_scaling_from_luma ||
1582       film_grain_params.num_v_points > 0) {
1583     for (int i = 0; i < num_pos_uv; ++i) {
1584       OBU_READ_LITERAL_OR_FAIL(8);
1585       film_grain_params.auto_regression_coeff_v[i] =
1586           static_cast<int8_t>(scratch - 128);
1587     }
1588   }
1589   OBU_READ_LITERAL_OR_FAIL(2);
1590   film_grain_params.auto_regression_shift = static_cast<uint8_t>(scratch + 6);
1591   OBU_READ_LITERAL_OR_FAIL(2);
1592   film_grain_params.grain_scale_shift = static_cast<int>(scratch);
1593   if (film_grain_params.num_u_points > 0) {
1594     OBU_READ_LITERAL_OR_FAIL(8);
1595     film_grain_params.u_multiplier = static_cast<int8_t>(scratch - 128);
1596     OBU_READ_LITERAL_OR_FAIL(8);
1597     film_grain_params.u_luma_multiplier = static_cast<int8_t>(scratch - 128);
1598     OBU_READ_LITERAL_OR_FAIL(9);
1599     film_grain_params.u_offset = static_cast<int16_t>(scratch - 256);
1600   }
1601   if (film_grain_params.num_v_points > 0) {
1602     OBU_READ_LITERAL_OR_FAIL(8);
1603     film_grain_params.v_multiplier = static_cast<int8_t>(scratch - 128);
1604     OBU_READ_LITERAL_OR_FAIL(8);
1605     film_grain_params.v_luma_multiplier = static_cast<int8_t>(scratch - 128);
1606     OBU_READ_LITERAL_OR_FAIL(9);
1607     film_grain_params.v_offset = static_cast<int16_t>(scratch - 256);
1608   }
1609   OBU_READ_BIT_OR_FAIL;
1610   film_grain_params.overlap_flag = scratch != 0;
1611   OBU_READ_BIT_OR_FAIL;
1612   film_grain_params.clip_to_restricted_range = scratch != 0;
1613   return true;
1614 }
1615 
ParseTileInfoSyntax()1616 bool ObuParser::ParseTileInfoSyntax() {
1617   TileInfo* const tile_info = &frame_header_.tile_info;
1618   const int sb_columns = sequence_header_.use_128x128_superblock
1619                              ? ((frame_header_.columns4x4 + 31) >> 5)
1620                              : ((frame_header_.columns4x4 + 15) >> 4);
1621   const int sb_rows = sequence_header_.use_128x128_superblock
1622                           ? ((frame_header_.rows4x4 + 31) >> 5)
1623                           : ((frame_header_.rows4x4 + 15) >> 4);
1624   tile_info->sb_columns = sb_columns;
1625   tile_info->sb_rows = sb_rows;
1626   const int sb_shift = sequence_header_.use_128x128_superblock ? 5 : 4;
1627   const int sb_size = 2 + sb_shift;
1628   const int sb_max_tile_width = kMaxTileWidth >> sb_size;
1629   const int sb_max_tile_area = kMaxTileArea >> MultiplyBy2(sb_size);
1630   const int minlog2_tile_columns = TileLog2(sb_max_tile_width, sb_columns);
1631   const int maxlog2_tile_columns =
1632       CeilLog2(std::min(sb_columns, static_cast<int>(kMaxTileColumns)));
1633   const int maxlog2_tile_rows =
1634       CeilLog2(std::min(sb_rows, static_cast<int>(kMaxTileRows)));
1635   const int min_log2_tiles = std::max(
1636       minlog2_tile_columns, TileLog2(sb_max_tile_area, sb_rows * sb_columns));
1637   int64_t scratch;
1638   OBU_READ_BIT_OR_FAIL;
1639   tile_info->uniform_spacing = scratch != 0;
1640   if (tile_info->uniform_spacing) {
1641     // Read tile columns.
1642     tile_info->tile_columns_log2 = minlog2_tile_columns;
1643     while (tile_info->tile_columns_log2 < maxlog2_tile_columns) {
1644       OBU_READ_BIT_OR_FAIL;
1645       if (scratch == 0) break;
1646       ++tile_info->tile_columns_log2;
1647     }
1648 
1649     // Compute tile column starts.
1650     const int sb_tile_width =
1651         (sb_columns + (1 << tile_info->tile_columns_log2) - 1) >>
1652         tile_info->tile_columns_log2;
1653     if (sb_tile_width <= 0) return false;
1654     int i = 0;
1655     for (int sb_start = 0; sb_start < sb_columns; sb_start += sb_tile_width) {
1656       if (i >= kMaxTileColumns) {
1657         LIBGAV1_DLOG(ERROR,
1658                      "tile_columns would be greater than kMaxTileColumns.");
1659         return false;
1660       }
1661       tile_info->tile_column_start[i++] = sb_start << sb_shift;
1662     }
1663     tile_info->tile_column_start[i] = frame_header_.columns4x4;
1664     tile_info->tile_columns = i;
1665 
1666     // Read tile rows.
1667     const int minlog2_tile_rows =
1668         std::max(min_log2_tiles - tile_info->tile_columns_log2, 0);
1669     tile_info->tile_rows_log2 = minlog2_tile_rows;
1670     while (tile_info->tile_rows_log2 < maxlog2_tile_rows) {
1671       OBU_READ_BIT_OR_FAIL;
1672       if (scratch == 0) break;
1673       ++tile_info->tile_rows_log2;
1674     }
1675 
1676     // Compute tile row starts.
1677     const int sb_tile_height =
1678         (sb_rows + (1 << tile_info->tile_rows_log2) - 1) >>
1679         tile_info->tile_rows_log2;
1680     if (sb_tile_height <= 0) return false;
1681     i = 0;
1682     for (int sb_start = 0; sb_start < sb_rows; sb_start += sb_tile_height) {
1683       if (i >= kMaxTileRows) {
1684         LIBGAV1_DLOG(ERROR, "tile_rows would be greater than kMaxTileRows.");
1685         return false;
1686       }
1687       tile_info->tile_row_start[i++] = sb_start << sb_shift;
1688     }
1689     tile_info->tile_row_start[i] = frame_header_.rows4x4;
1690     tile_info->tile_rows = i;
1691   } else {
1692     int widest_tile_sb = 1;
1693     int i = 0;
1694     for (int sb_start = 0; sb_start < sb_columns; ++i) {
1695       if (i >= kMaxTileColumns) {
1696         LIBGAV1_DLOG(ERROR,
1697                      "tile_columns would be greater than kMaxTileColumns.");
1698         return false;
1699       }
1700       tile_info->tile_column_start[i] = sb_start << sb_shift;
1701       const int max_width =
1702           std::min(sb_columns - sb_start, static_cast<int>(sb_max_tile_width));
1703       if (!bit_reader_->DecodeUniform(
1704               max_width, &tile_info->tile_column_width_in_superblocks[i])) {
1705         LIBGAV1_DLOG(ERROR, "Not enough bits.");
1706         return false;
1707       }
1708       ++tile_info->tile_column_width_in_superblocks[i];
1709       widest_tile_sb = std::max(tile_info->tile_column_width_in_superblocks[i],
1710                                 widest_tile_sb);
1711       sb_start += tile_info->tile_column_width_in_superblocks[i];
1712     }
1713     tile_info->tile_column_start[i] = frame_header_.columns4x4;
1714     tile_info->tile_columns = i;
1715     tile_info->tile_columns_log2 = CeilLog2(tile_info->tile_columns);
1716 
1717     int max_tile_area_sb = sb_rows * sb_columns;
1718     if (min_log2_tiles > 0) max_tile_area_sb >>= min_log2_tiles + 1;
1719     const int max_tile_height_sb =
1720         std::max(max_tile_area_sb / widest_tile_sb, 1);
1721 
1722     i = 0;
1723     for (int sb_start = 0; sb_start < sb_rows; ++i) {
1724       if (i >= kMaxTileRows) {
1725         LIBGAV1_DLOG(ERROR, "tile_rows would be greater than kMaxTileRows.");
1726         return false;
1727       }
1728       tile_info->tile_row_start[i] = sb_start << sb_shift;
1729       const int max_height = std::min(sb_rows - sb_start, max_tile_height_sb);
1730       if (!bit_reader_->DecodeUniform(
1731               max_height, &tile_info->tile_row_height_in_superblocks[i])) {
1732         LIBGAV1_DLOG(ERROR, "Not enough bits.");
1733         return false;
1734       }
1735       ++tile_info->tile_row_height_in_superblocks[i];
1736       sb_start += tile_info->tile_row_height_in_superblocks[i];
1737     }
1738     tile_info->tile_row_start[i] = frame_header_.rows4x4;
1739     tile_info->tile_rows = i;
1740     tile_info->tile_rows_log2 = CeilLog2(tile_info->tile_rows);
1741   }
1742   tile_info->tile_count = tile_info->tile_rows * tile_info->tile_columns;
1743   if (!tile_buffers_.reserve(tile_info->tile_count)) {
1744     LIBGAV1_DLOG(ERROR, "Unable to allocate memory for tile_buffers_.");
1745     return false;
1746   }
1747   tile_info->context_update_id = 0;
1748   const int tile_bits =
1749       tile_info->tile_columns_log2 + tile_info->tile_rows_log2;
1750   if (tile_bits != 0) {
1751     OBU_READ_LITERAL_OR_FAIL(tile_bits);
1752     tile_info->context_update_id = static_cast<int16_t>(scratch);
1753     if (tile_info->context_update_id >= tile_info->tile_count) {
1754       LIBGAV1_DLOG(ERROR, "Invalid context_update_tile_id (%d) >= %d.",
1755                    tile_info->context_update_id, tile_info->tile_count);
1756       return false;
1757     }
1758     OBU_READ_LITERAL_OR_FAIL(2);
1759     tile_info->tile_size_bytes = 1 + scratch;
1760   }
1761   return true;
1762 }
1763 
ReadAllowWarpedMotion()1764 bool ObuParser::ReadAllowWarpedMotion() {
1765   if (IsIntraFrame(frame_header_.frame_type) ||
1766       frame_header_.error_resilient_mode ||
1767       !sequence_header_.enable_warped_motion) {
1768     return true;
1769   }
1770   int64_t scratch;
1771   OBU_READ_BIT_OR_FAIL;
1772   frame_header_.allow_warped_motion = scratch != 0;
1773   return true;
1774 }
1775 
ParseFrameParameters()1776 bool ObuParser::ParseFrameParameters() {
1777   int64_t scratch;
1778   if (sequence_header_.reduced_still_picture_header) {
1779     frame_header_.show_frame = true;
1780     if (!EnsureCurrentFrameIsNotNull()) return false;
1781   } else {
1782     OBU_READ_BIT_OR_FAIL;
1783     frame_header_.show_existing_frame = scratch != 0;
1784     if (frame_header_.show_existing_frame) {
1785       OBU_READ_LITERAL_OR_FAIL(3);
1786       frame_header_.frame_to_show = scratch;
1787       if (sequence_header_.decoder_model_info_present_flag &&
1788           !sequence_header_.timing_info.equal_picture_interval) {
1789         OBU_READ_LITERAL_OR_FAIL(
1790             sequence_header_.decoder_model_info.frame_presentation_time_length);
1791         frame_header_.frame_presentation_time = static_cast<uint32_t>(scratch);
1792       }
1793       if (sequence_header_.frame_id_numbers_present) {
1794         OBU_READ_LITERAL_OR_FAIL(sequence_header_.frame_id_length_bits);
1795         frame_header_.display_frame_id = static_cast<uint16_t>(scratch);
1796         // Section 6.8.2: It is a requirement of bitstream conformance that
1797         // whenever display_frame_id is read, the value matches
1798         // RefFrameId[ frame_to_show_map_idx ] ..., and that
1799         // RefValid[ frame_to_show_map_idx ] is equal to 1.
1800         //
1801         // The current_frame_ == nullptr check below is equivalent to checking
1802         // if RefValid[ frame_to_show_map_idx ] is equal to 1.
1803         if (frame_header_.display_frame_id !=
1804             decoder_state_.reference_frame_id[frame_header_.frame_to_show]) {
1805           LIBGAV1_DLOG(ERROR,
1806                        "Reference buffer %d has a frame id number mismatch.",
1807                        frame_header_.frame_to_show);
1808           return false;
1809         }
1810       }
1811       // Section 7.18.2. Note: This is also needed for Section 7.21 if
1812       // frame_type is kFrameKey.
1813       current_frame_ =
1814           decoder_state_.reference_frame[frame_header_.frame_to_show];
1815       if (current_frame_ == nullptr) {
1816         LIBGAV1_DLOG(ERROR, "Buffer %d does not contain a decoded frame",
1817                      frame_header_.frame_to_show);
1818         return false;
1819       }
1820       // Section 6.8.2: It is a requirement of bitstream conformance that
1821       // when show_existing_frame is used to show a previous frame, that the
1822       // value of showable_frame for the previous frame was equal to 1.
1823       if (!current_frame_->showable_frame()) {
1824         LIBGAV1_DLOG(ERROR, "Buffer %d does not contain a showable frame",
1825                      frame_header_.frame_to_show);
1826         return false;
1827       }
1828       if (current_frame_->frame_type() == kFrameKey) {
1829         frame_header_.refresh_frame_flags = 0xff;
1830         // Section 6.8.2: It is a requirement of bitstream conformance that
1831         // when show_existing_frame is used to show a previous frame with
1832         // RefFrameType[ frame_to_show_map_idx ] equal to KEY_FRAME, that
1833         // the frame is output via the show_existing_frame mechanism at most
1834         // once.
1835         current_frame_->set_showable_frame(false);
1836 
1837         // Section 7.21. Note: decoder_state_.current_frame_id must be set
1838         // only when frame_type is kFrameKey per the spec. Among all the
1839         // variables set in Section 7.21, current_frame_id is the only one
1840         // whose value lives across frames. (PrevFrameID is set equal to the
1841         // current_frame_id value for the previous frame.)
1842         decoder_state_.current_frame_id =
1843             decoder_state_.reference_frame_id[frame_header_.frame_to_show];
1844         decoder_state_.order_hint =
1845             decoder_state_.reference_order_hint[frame_header_.frame_to_show];
1846       }
1847       return true;
1848     }
1849     if (!EnsureCurrentFrameIsNotNull()) return false;
1850     OBU_READ_LITERAL_OR_FAIL(2);
1851     frame_header_.frame_type = static_cast<FrameType>(scratch);
1852     current_frame_->set_frame_type(frame_header_.frame_type);
1853     OBU_READ_BIT_OR_FAIL;
1854     frame_header_.show_frame = scratch != 0;
1855     if (frame_header_.show_frame &&
1856         sequence_header_.decoder_model_info_present_flag &&
1857         !sequence_header_.timing_info.equal_picture_interval) {
1858       OBU_READ_LITERAL_OR_FAIL(
1859           sequence_header_.decoder_model_info.frame_presentation_time_length);
1860       frame_header_.frame_presentation_time = static_cast<uint32_t>(scratch);
1861     }
1862     if (frame_header_.show_frame) {
1863       frame_header_.showable_frame = (frame_header_.frame_type != kFrameKey);
1864     } else {
1865       OBU_READ_BIT_OR_FAIL;
1866       frame_header_.showable_frame = scratch != 0;
1867     }
1868     current_frame_->set_showable_frame(frame_header_.showable_frame);
1869     if (frame_header_.frame_type == kFrameSwitch ||
1870         (frame_header_.frame_type == kFrameKey && frame_header_.show_frame)) {
1871       frame_header_.error_resilient_mode = true;
1872     } else {
1873       OBU_READ_BIT_OR_FAIL;
1874       frame_header_.error_resilient_mode = scratch != 0;
1875     }
1876   }
1877   if (frame_header_.frame_type == kFrameKey && frame_header_.show_frame) {
1878     decoder_state_.reference_order_hint.fill(0);
1879     decoder_state_.reference_frame.fill(nullptr);
1880   }
1881   OBU_READ_BIT_OR_FAIL;
1882   frame_header_.enable_cdf_update = scratch == 0;
1883   if (sequence_header_.force_screen_content_tools ==
1884       kSelectScreenContentTools) {
1885     OBU_READ_BIT_OR_FAIL;
1886     frame_header_.allow_screen_content_tools = scratch != 0;
1887   } else {
1888     frame_header_.allow_screen_content_tools =
1889         sequence_header_.force_screen_content_tools != 0;
1890   }
1891   if (frame_header_.allow_screen_content_tools) {
1892     if (sequence_header_.force_integer_mv == kSelectIntegerMv) {
1893       OBU_READ_BIT_OR_FAIL;
1894       frame_header_.force_integer_mv = scratch;
1895     } else {
1896       frame_header_.force_integer_mv = sequence_header_.force_integer_mv;
1897     }
1898   } else {
1899     frame_header_.force_integer_mv = 0;
1900   }
1901   if (IsIntraFrame(frame_header_.frame_type)) {
1902     frame_header_.force_integer_mv = 1;
1903   }
1904   if (sequence_header_.frame_id_numbers_present) {
1905     OBU_READ_LITERAL_OR_FAIL(sequence_header_.frame_id_length_bits);
1906     frame_header_.current_frame_id = static_cast<uint16_t>(scratch);
1907     const int previous_frame_id = decoder_state_.current_frame_id;
1908     decoder_state_.current_frame_id = frame_header_.current_frame_id;
1909     if (frame_header_.frame_type != kFrameKey || !frame_header_.show_frame) {
1910       if (previous_frame_id >= 0) {
1911         // Section 6.8.2: ..., it is a requirement of bitstream conformance
1912         // that all of the following conditions are true:
1913         //   * current_frame_id is not equal to PrevFrameID,
1914         //   * DiffFrameID is less than 1 << ( idLen - 1 )
1915         int diff_frame_id = decoder_state_.current_frame_id - previous_frame_id;
1916         const int id_length_max_value =
1917             1 << sequence_header_.frame_id_length_bits;
1918         if (diff_frame_id <= 0) {
1919           diff_frame_id += id_length_max_value;
1920         }
1921         if (diff_frame_id >= DivideBy2(id_length_max_value)) {
1922           LIBGAV1_DLOG(ERROR,
1923                        "current_frame_id (%d) equals or differs too much from "
1924                        "previous_frame_id (%d).",
1925                        decoder_state_.current_frame_id, previous_frame_id);
1926           return false;
1927         }
1928       }
1929       MarkInvalidReferenceFrames();
1930     }
1931   } else {
1932     frame_header_.current_frame_id = 0;
1933     decoder_state_.current_frame_id = frame_header_.current_frame_id;
1934   }
1935   if (frame_header_.frame_type == kFrameSwitch) {
1936     frame_header_.frame_size_override_flag = true;
1937   } else if (!sequence_header_.reduced_still_picture_header) {
1938     OBU_READ_BIT_OR_FAIL;
1939     frame_header_.frame_size_override_flag = scratch != 0;
1940   }
1941   if (sequence_header_.order_hint_bits > 0) {
1942     OBU_READ_LITERAL_OR_FAIL(sequence_header_.order_hint_bits);
1943     frame_header_.order_hint = scratch;
1944   }
1945   decoder_state_.order_hint = frame_header_.order_hint;
1946   if (IsIntraFrame(frame_header_.frame_type) ||
1947       frame_header_.error_resilient_mode) {
1948     frame_header_.primary_reference_frame = kPrimaryReferenceNone;
1949   } else {
1950     OBU_READ_LITERAL_OR_FAIL(3);
1951     frame_header_.primary_reference_frame = scratch;
1952   }
1953   if (sequence_header_.decoder_model_info_present_flag) {
1954     OBU_READ_BIT_OR_FAIL;
1955     const bool buffer_removal_time_present = scratch != 0;
1956     if (buffer_removal_time_present) {
1957       for (int i = 0; i < sequence_header_.operating_points; ++i) {
1958         if (!sequence_header_.decoder_model_present_for_operating_point[i]) {
1959           continue;
1960         }
1961         const int index = sequence_header_.operating_point_idc[i];
1962         if (index == 0 ||
1963             (InTemporalLayer(index, obu_headers_.back().temporal_id) &&
1964              InSpatialLayer(index, obu_headers_.back().spatial_id))) {
1965           OBU_READ_LITERAL_OR_FAIL(
1966               sequence_header_.decoder_model_info.buffer_removal_time_length);
1967           frame_header_.buffer_removal_time[i] = static_cast<uint32_t>(scratch);
1968         }
1969       }
1970     }
1971   }
1972   if (frame_header_.frame_type == kFrameSwitch ||
1973       (frame_header_.frame_type == kFrameKey && frame_header_.show_frame)) {
1974     frame_header_.refresh_frame_flags = 0xff;
1975   } else {
1976     OBU_READ_LITERAL_OR_FAIL(8);
1977     frame_header_.refresh_frame_flags = scratch;
1978     // Section 6.8.2: If frame_type is equal to INTRA_ONLY_FRAME, it is a
1979     // requirement of bitstream conformance that refresh_frame_flags is not
1980     // equal to 0xff.
1981     if (frame_header_.frame_type == kFrameIntraOnly &&
1982         frame_header_.refresh_frame_flags == 0xff) {
1983       LIBGAV1_DLOG(ERROR, "Intra only frames cannot have refresh flags 0xFF.");
1984       return false;
1985     }
1986   }
1987   if ((!IsIntraFrame(frame_header_.frame_type) ||
1988        frame_header_.refresh_frame_flags != 0xff) &&
1989       !ParseReferenceOrderHint()) {
1990     return false;
1991   }
1992   if (IsIntraFrame(frame_header_.frame_type)) {
1993     if (!ParseFrameSizeAndRenderSize()) return false;
1994     if (frame_header_.allow_screen_content_tools &&
1995         frame_header_.width == frame_header_.upscaled_width) {
1996       OBU_READ_BIT_OR_FAIL;
1997       frame_header_.allow_intrabc = scratch != 0;
1998     }
1999   } else {
2000     if (!sequence_header_.enable_order_hint) {
2001       frame_header_.frame_refs_short_signaling = false;
2002     } else {
2003       OBU_READ_BIT_OR_FAIL;
2004       frame_header_.frame_refs_short_signaling = scratch != 0;
2005       if (frame_header_.frame_refs_short_signaling) {
2006         OBU_READ_LITERAL_OR_FAIL(3);
2007         const int8_t last_frame_idx = scratch;
2008         OBU_READ_LITERAL_OR_FAIL(3);
2009         const int8_t gold_frame_idx = scratch;
2010         if (!SetFrameReferences(last_frame_idx, gold_frame_idx)) {
2011           return false;
2012         }
2013       }
2014     }
2015     for (int i = 0; i < kNumInterReferenceFrameTypes; ++i) {
2016       if (!frame_header_.frame_refs_short_signaling) {
2017         OBU_READ_LITERAL_OR_FAIL(3);
2018         frame_header_.reference_frame_index[i] = scratch;
2019       }
2020       const int reference_frame_index = frame_header_.reference_frame_index[i];
2021       assert(reference_frame_index >= 0);
2022       // Section 6.8.2: It is a requirement of bitstream conformance that
2023       // RefValid[ ref_frame_idx[ i ] ] is equal to 1 ...
2024       // The remainder of the statement is handled by ParseSequenceHeader().
2025       // Note if support for Annex C: Error resilience behavior is added this
2026       // check should be omitted per C.5 Decoder consequences of processable
2027       // frames.
2028       if (decoder_state_.reference_frame[reference_frame_index] == nullptr) {
2029         LIBGAV1_DLOG(ERROR, "ref_frame_idx[%d] (%d) is not valid.", i,
2030                      reference_frame_index);
2031         return false;
2032       }
2033       if (sequence_header_.frame_id_numbers_present) {
2034         OBU_READ_LITERAL_OR_FAIL(sequence_header_.delta_frame_id_length_bits);
2035         const int delta_frame_id = static_cast<int>(1 + scratch);
2036         const int id_length_max_value =
2037             1 << sequence_header_.frame_id_length_bits;
2038         frame_header_.expected_frame_id[i] =
2039             (frame_header_.current_frame_id + id_length_max_value -
2040              delta_frame_id) %
2041             id_length_max_value;
2042         // Section 6.8.2: It is a requirement of bitstream conformance that
2043         // whenever expectedFrameId[ i ] is calculated, the value matches
2044         // RefFrameId[ ref_frame_idx[ i ] ] ...
2045         if (frame_header_.expected_frame_id[i] !=
2046             decoder_state_.reference_frame_id[reference_frame_index]) {
2047           LIBGAV1_DLOG(ERROR,
2048                        "Reference buffer %d has a frame id number mismatch.",
2049                        reference_frame_index);
2050           return false;
2051         }
2052       }
2053     }
2054     if (frame_header_.frame_size_override_flag &&
2055         !frame_header_.error_resilient_mode) {
2056       // Section 5.9.7.
2057       for (int index : frame_header_.reference_frame_index) {
2058         OBU_READ_BIT_OR_FAIL;
2059         frame_header_.found_reference = scratch != 0;
2060         if (frame_header_.found_reference) {
2061           const RefCountedBuffer* reference_frame =
2062               decoder_state_.reference_frame[index].get();
2063           // frame_header_.upscaled_width will be set in the
2064           // ParseSuperResParametersAndComputeImageSize() call below.
2065           frame_header_.width = reference_frame->upscaled_width();
2066           frame_header_.height = reference_frame->frame_height();
2067           frame_header_.render_width = reference_frame->render_width();
2068           frame_header_.render_height = reference_frame->render_height();
2069           if (!ParseSuperResParametersAndComputeImageSize()) return false;
2070           break;
2071         }
2072       }
2073       if (!frame_header_.found_reference && !ParseFrameSizeAndRenderSize()) {
2074         return false;
2075       }
2076     } else {
2077       if (!ParseFrameSizeAndRenderSize()) return false;
2078     }
2079     if (!ValidateInterFrameSize()) return false;
2080     if (frame_header_.force_integer_mv != 0) {
2081       frame_header_.allow_high_precision_mv = false;
2082     } else {
2083       OBU_READ_BIT_OR_FAIL;
2084       frame_header_.allow_high_precision_mv = scratch != 0;
2085     }
2086     OBU_READ_BIT_OR_FAIL;
2087     const bool is_filter_switchable = scratch != 0;
2088     if (is_filter_switchable) {
2089       frame_header_.interpolation_filter = kInterpolationFilterSwitchable;
2090     } else {
2091       OBU_READ_LITERAL_OR_FAIL(2);
2092       frame_header_.interpolation_filter =
2093           static_cast<InterpolationFilter>(scratch);
2094     }
2095     OBU_READ_BIT_OR_FAIL;
2096     frame_header_.is_motion_mode_switchable = scratch != 0;
2097     if (frame_header_.error_resilient_mode ||
2098         !sequence_header_.enable_ref_frame_mvs) {
2099       frame_header_.use_ref_frame_mvs = false;
2100     } else {
2101       OBU_READ_BIT_OR_FAIL;
2102       frame_header_.use_ref_frame_mvs = scratch != 0;
2103     }
2104   }
2105   // At this point, we have parsed the frame and render sizes and computed
2106   // the image size, whether it's an intra or inter frame. So we can save
2107   // the sizes in the current frame now.
2108   if (!current_frame_->SetFrameDimensions(frame_header_)) {
2109     LIBGAV1_DLOG(ERROR, "Setting current frame dimensions failed.");
2110     return false;
2111   }
2112   if (!IsIntraFrame(frame_header_.frame_type)) {
2113     // Initialize the kReferenceFrameIntra type reference frame information to
2114     // simplify the frame type validation in motion field projection.
2115     // Set the kReferenceFrameIntra type |order_hint_| to
2116     // |frame_header_.order_hint|. This guarantees that in SIMD implementations,
2117     // the other reference frame information of the kReferenceFrameIntra type
2118     // could be correctly initialized using the following loop with
2119     // |frame_header_.order_hint| being the |hint|.
2120     ReferenceInfo* const reference_info = current_frame_->reference_info();
2121     reference_info->order_hint[kReferenceFrameIntra] = frame_header_.order_hint;
2122     reference_info->relative_distance_from[kReferenceFrameIntra] = 0;
2123     reference_info->relative_distance_to[kReferenceFrameIntra] = 0;
2124     reference_info->skip_references[kReferenceFrameIntra] = true;
2125     reference_info->projection_divisions[kReferenceFrameIntra] = 0;
2126 
2127     for (int i = kReferenceFrameLast; i <= kNumInterReferenceFrameTypes; ++i) {
2128       const auto reference_frame = static_cast<ReferenceFrameType>(i);
2129       const uint8_t hint =
2130           decoder_state_.reference_order_hint
2131               [frame_header_.reference_frame_index[i - kReferenceFrameLast]];
2132       reference_info->order_hint[reference_frame] = hint;
2133       const int relative_distance_from =
2134           GetRelativeDistance(hint, frame_header_.order_hint,
2135                               sequence_header_.order_hint_shift_bits);
2136       const int relative_distance_to =
2137           GetRelativeDistance(frame_header_.order_hint, hint,
2138                               sequence_header_.order_hint_shift_bits);
2139       reference_info->relative_distance_from[reference_frame] =
2140           relative_distance_from;
2141       reference_info->relative_distance_to[reference_frame] =
2142           relative_distance_to;
2143       reference_info->skip_references[reference_frame] =
2144           relative_distance_to > kMaxFrameDistance || relative_distance_to <= 0;
2145       reference_info->projection_divisions[reference_frame] =
2146           reference_info->skip_references[reference_frame]
2147               ? 0
2148               : kProjectionMvDivisionLookup[relative_distance_to];
2149       decoder_state_.reference_frame_sign_bias[reference_frame] =
2150           relative_distance_from > 0;
2151     }
2152   }
2153   if (frame_header_.enable_cdf_update &&
2154       !sequence_header_.reduced_still_picture_header) {
2155     OBU_READ_BIT_OR_FAIL;
2156     frame_header_.enable_frame_end_update_cdf = scratch == 0;
2157   } else {
2158     frame_header_.enable_frame_end_update_cdf = false;
2159   }
2160   return true;
2161 }
2162 
ParseFrameHeader()2163 bool ObuParser::ParseFrameHeader() {
2164   // Section 6.8.1: It is a requirement of bitstream conformance that a
2165   // sequence header OBU has been received before a frame header OBU.
2166   if (!has_sequence_header_) return false;
2167   if (!ParseFrameParameters()) return false;
2168   if (frame_header_.show_existing_frame) return true;
2169   assert(!obu_headers_.empty());
2170   current_frame_->set_spatial_id(obu_headers_.back().spatial_id);
2171   current_frame_->set_temporal_id(obu_headers_.back().temporal_id);
2172   bool status = ParseTileInfoSyntax() && ParseQuantizerParameters() &&
2173                 ParseSegmentationParameters();
2174   if (!status) return false;
2175   current_frame_->SetSegmentationParameters(frame_header_.segmentation);
2176   status =
2177       ParseQuantizerIndexDeltaParameters() && ParseLoopFilterDeltaParameters();
2178   if (!status) return false;
2179   ComputeSegmentLosslessAndQIndex();
2180   // Section 6.8.2: It is a requirement of bitstream conformance that
2181   // delta_q_present is equal to 0 when CodedLossless is equal to 1.
2182   if (frame_header_.coded_lossless && frame_header_.delta_q.present) {
2183     return false;
2184   }
2185   status = ParseLoopFilterParameters();
2186   if (!status) return false;
2187   current_frame_->SetLoopFilterDeltas(frame_header_.loop_filter);
2188   status = ParseCdefParameters() && ParseLoopRestorationParameters() &&
2189            ParseTxModeSyntax() && ParseFrameReferenceModeSyntax() &&
2190            ParseSkipModeParameters() && ReadAllowWarpedMotion();
2191   if (!status) return false;
2192   int64_t scratch;
2193   OBU_READ_BIT_OR_FAIL;
2194   frame_header_.reduced_tx_set = scratch != 0;
2195   status = ParseGlobalMotionParameters();
2196   if (!status) return false;
2197   current_frame_->SetGlobalMotions(frame_header_.global_motion);
2198   status = ParseFilmGrainParameters();
2199   if (!status) return false;
2200   if (sequence_header_.film_grain_params_present) {
2201     current_frame_->set_film_grain_params(frame_header_.film_grain_params);
2202   }
2203   if (sequence_header_changed_ &&
2204       (frame_header_.frame_type != kFrameKey || !frame_header_.show_frame ||
2205        frame_header_.show_existing_frame ||
2206        current_frame_->temporal_id() != 0)) {
2207     // Section 7.5. Ordering of OBUs: A new coded video sequence is defined to
2208     // start at each temporal unit which satisfies both of the following
2209     // conditions:
2210     //   * A sequence header OBU appears before the first frame header.
2211     //   * The first frame header has frame_type equal to KEY_FRAME, show_frame
2212     //     equal to 1, show_existing_frame equal to 0, and temporal_id equal to
2213     //     0.
2214     LIBGAV1_DLOG(
2215         WARNING,
2216         "The first frame successive to sequence header OBU should be a "
2217         "keyframe with show_frame=1, show_existing_frame=0 and "
2218         "temporal_id=0");
2219   }
2220 
2221   return true;
2222 }
2223 
ParsePadding(const uint8_t * data,size_t size)2224 bool ObuParser::ParsePadding(const uint8_t* data, size_t size) {
2225   // The spec allows a padding OBU to be header-only (i.e., |size| = 0). So
2226   // check trailing bits only if |size| > 0.
2227   if (size == 0) return true;
2228   // The payload of a padding OBU is byte aligned. Therefore the first
2229   // trailing byte should be 0x80. See https://crbug.com/aomedia/2393.
2230   const int i = GetLastNonzeroByteIndex(data, size);
2231   if (i < 0) {
2232     LIBGAV1_DLOG(ERROR, "Trailing bit is missing.");
2233     return false;
2234   }
2235   if (data[i] != 0x80) {
2236     LIBGAV1_DLOG(
2237         ERROR,
2238         "The last nonzero byte of the payload data is 0x%x, should be 0x80.",
2239         data[i]);
2240     return false;
2241   }
2242   // Skip all bits before the trailing bit.
2243   bit_reader_->SkipBytes(i);
2244   return true;
2245 }
2246 
ParseMetadataScalability()2247 bool ObuParser::ParseMetadataScalability() {
2248   int64_t scratch;
2249   // scalability_mode_idc
2250   OBU_READ_LITERAL_OR_FAIL(8);
2251   const auto scalability_mode_idc = static_cast<int>(scratch);
2252   if (scalability_mode_idc == kScalabilitySS) {
2253     // Parse scalability_structure().
2254     // spatial_layers_cnt_minus_1
2255     OBU_READ_LITERAL_OR_FAIL(2);
2256     const auto spatial_layers_count = static_cast<int>(scratch) + 1;
2257     // spatial_layer_dimensions_present_flag
2258     OBU_READ_BIT_OR_FAIL;
2259     const auto spatial_layer_dimensions_present_flag = scratch != 0;
2260     // spatial_layer_description_present_flag
2261     OBU_READ_BIT_OR_FAIL;
2262     const auto spatial_layer_description_present_flag = scratch != 0;
2263     // temporal_group_description_present_flag
2264     OBU_READ_BIT_OR_FAIL;
2265     const auto temporal_group_description_present_flag = scratch != 0;
2266     // scalability_structure_reserved_3bits
2267     OBU_READ_LITERAL_OR_FAIL(3);
2268     if (scratch != 0) {
2269       LIBGAV1_DLOG(WARNING,
2270                    "scalability_structure_reserved_3bits is not zero.");
2271     }
2272     if (spatial_layer_dimensions_present_flag) {
2273       for (int i = 0; i < spatial_layers_count; ++i) {
2274         // spatial_layer_max_width[i]
2275         OBU_READ_LITERAL_OR_FAIL(16);
2276         // spatial_layer_max_height[i]
2277         OBU_READ_LITERAL_OR_FAIL(16);
2278       }
2279     }
2280     if (spatial_layer_description_present_flag) {
2281       for (int i = 0; i < spatial_layers_count; ++i) {
2282         // spatial_layer_ref_id[i]
2283         OBU_READ_LITERAL_OR_FAIL(8);
2284       }
2285     }
2286     if (temporal_group_description_present_flag) {
2287       // temporal_group_size
2288       OBU_READ_LITERAL_OR_FAIL(8);
2289       const auto temporal_group_size = static_cast<int>(scratch);
2290       for (int i = 0; i < temporal_group_size; ++i) {
2291         // temporal_group_temporal_id[i]
2292         OBU_READ_LITERAL_OR_FAIL(3);
2293         // temporal_group_temporal_switching_up_point_flag[i]
2294         OBU_READ_BIT_OR_FAIL;
2295         // temporal_group_spatial_switching_up_point_flag[i]
2296         OBU_READ_BIT_OR_FAIL;
2297         // temporal_group_ref_cnt[i]
2298         OBU_READ_LITERAL_OR_FAIL(3);
2299         const auto temporal_group_ref_count = static_cast<int>(scratch);
2300         for (int j = 0; j < temporal_group_ref_count; ++j) {
2301           // temporal_group_ref_pic_diff[i][j]
2302           OBU_READ_LITERAL_OR_FAIL(8);
2303         }
2304       }
2305     }
2306   }
2307   return true;
2308 }
2309 
ParseMetadataTimecode()2310 bool ObuParser::ParseMetadataTimecode() {
2311   int64_t scratch;
2312   // counting_type: should be the same for all pictures in the coded video
2313   // sequence. 7..31 are reserved.
2314   OBU_READ_LITERAL_OR_FAIL(5);
2315   // full_timestamp_flag
2316   OBU_READ_BIT_OR_FAIL;
2317   const bool full_timestamp_flag = scratch != 0;
2318   // discontinuity_flag
2319   OBU_READ_BIT_OR_FAIL;
2320   // cnt_dropped_flag
2321   OBU_READ_BIT_OR_FAIL;
2322   // n_frames
2323   OBU_READ_LITERAL_OR_FAIL(9);
2324   if (full_timestamp_flag) {
2325     // seconds_value
2326     OBU_READ_LITERAL_OR_FAIL(6);
2327     const auto seconds_value = static_cast<int>(scratch);
2328     if (seconds_value > 59) {
2329       LIBGAV1_DLOG(ERROR, "Invalid seconds_value %d.", seconds_value);
2330       return false;
2331     }
2332     // minutes_value
2333     OBU_READ_LITERAL_OR_FAIL(6);
2334     const auto minutes_value = static_cast<int>(scratch);
2335     if (minutes_value > 59) {
2336       LIBGAV1_DLOG(ERROR, "Invalid minutes_value %d.", minutes_value);
2337       return false;
2338     }
2339     // hours_value
2340     OBU_READ_LITERAL_OR_FAIL(5);
2341     const auto hours_value = static_cast<int>(scratch);
2342     if (hours_value > 23) {
2343       LIBGAV1_DLOG(ERROR, "Invalid hours_value %d.", hours_value);
2344       return false;
2345     }
2346   } else {
2347     // seconds_flag
2348     OBU_READ_BIT_OR_FAIL;
2349     const bool seconds_flag = scratch != 0;
2350     if (seconds_flag) {
2351       // seconds_value
2352       OBU_READ_LITERAL_OR_FAIL(6);
2353       const auto seconds_value = static_cast<int>(scratch);
2354       if (seconds_value > 59) {
2355         LIBGAV1_DLOG(ERROR, "Invalid seconds_value %d.", seconds_value);
2356         return false;
2357       }
2358       // minutes_flag
2359       OBU_READ_BIT_OR_FAIL;
2360       const bool minutes_flag = scratch != 0;
2361       if (minutes_flag) {
2362         // minutes_value
2363         OBU_READ_LITERAL_OR_FAIL(6);
2364         const auto minutes_value = static_cast<int>(scratch);
2365         if (minutes_value > 59) {
2366           LIBGAV1_DLOG(ERROR, "Invalid minutes_value %d.", minutes_value);
2367           return false;
2368         }
2369         // hours_flag
2370         OBU_READ_BIT_OR_FAIL;
2371         const bool hours_flag = scratch != 0;
2372         if (hours_flag) {
2373           // hours_value
2374           OBU_READ_LITERAL_OR_FAIL(5);
2375           const auto hours_value = static_cast<int>(scratch);
2376           if (hours_value > 23) {
2377             LIBGAV1_DLOG(ERROR, "Invalid hours_value %d.", hours_value);
2378             return false;
2379           }
2380         }
2381       }
2382     }
2383   }
2384   // time_offset_length: should be the same for all pictures in the coded
2385   // video sequence.
2386   OBU_READ_LITERAL_OR_FAIL(5);
2387   const auto time_offset_length = static_cast<int>(scratch);
2388   if (time_offset_length > 0) {
2389     // time_offset_value
2390     OBU_READ_LITERAL_OR_FAIL(time_offset_length);
2391   }
2392   // Compute clockTimestamp. Section 6.7.7:
2393   //   When timing_info_present_flag is equal to 1 and discontinuity_flag is
2394   //   equal to 0, the value of clockTimestamp shall be greater than or equal
2395   //   to the value of clockTimestamp for the previous set of clock timestamp
2396   //   syntax elements in output order.
2397   return true;
2398 }
2399 
ParseMetadata(const uint8_t * data,size_t size)2400 bool ObuParser::ParseMetadata(const uint8_t* data, size_t size) {
2401   const size_t start_offset = bit_reader_->byte_offset();
2402   size_t metadata_type;
2403   if (!bit_reader_->ReadUnsignedLeb128(&metadata_type)) {
2404     LIBGAV1_DLOG(ERROR, "Could not read metadata_type.");
2405     return false;
2406   }
2407   const size_t metadata_type_size = bit_reader_->byte_offset() - start_offset;
2408   if (size < metadata_type_size) {
2409     LIBGAV1_DLOG(
2410         ERROR, "metadata_type is longer than metadata OBU payload %zu vs %zu.",
2411         metadata_type_size, size);
2412     return false;
2413   }
2414   data += metadata_type_size;
2415   size -= metadata_type_size;
2416   int64_t scratch;
2417   switch (metadata_type) {
2418     case kMetadataTypeHdrContentLightLevel: {
2419       ObuMetadataHdrCll hdr_cll;
2420       OBU_READ_LITERAL_OR_FAIL(16);
2421       hdr_cll.max_cll = scratch;
2422       OBU_READ_LITERAL_OR_FAIL(16);
2423       hdr_cll.max_fall = scratch;
2424       if (!EnsureCurrentFrameIsNotNull()) return false;
2425       current_frame_->set_hdr_cll(hdr_cll);
2426       break;
2427     }
2428     case kMetadataTypeHdrMasteringDisplayColorVolume: {
2429       ObuMetadataHdrMdcv hdr_mdcv;
2430       for (int i = 0; i < 3; ++i) {
2431         OBU_READ_LITERAL_OR_FAIL(16);
2432         hdr_mdcv.primary_chromaticity_x[i] = scratch;
2433         OBU_READ_LITERAL_OR_FAIL(16);
2434         hdr_mdcv.primary_chromaticity_y[i] = scratch;
2435       }
2436       OBU_READ_LITERAL_OR_FAIL(16);
2437       hdr_mdcv.white_point_chromaticity_x = scratch;
2438       OBU_READ_LITERAL_OR_FAIL(16);
2439       hdr_mdcv.white_point_chromaticity_y = scratch;
2440       OBU_READ_LITERAL_OR_FAIL(32);
2441       hdr_mdcv.luminance_max = static_cast<uint32_t>(scratch);
2442       OBU_READ_LITERAL_OR_FAIL(32);
2443       hdr_mdcv.luminance_min = static_cast<uint32_t>(scratch);
2444       if (!EnsureCurrentFrameIsNotNull()) return false;
2445       current_frame_->set_hdr_mdcv(hdr_mdcv);
2446       break;
2447     }
2448     case kMetadataTypeScalability:
2449       if (!ParseMetadataScalability()) return false;
2450       break;
2451     case kMetadataTypeItutT35: {
2452       ObuMetadataItutT35 itut_t35;
2453       OBU_READ_LITERAL_OR_FAIL(8);
2454       itut_t35.country_code = static_cast<uint8_t>(scratch);
2455       ++data;
2456       --size;
2457       if (itut_t35.country_code == 0xFF) {
2458         OBU_READ_LITERAL_OR_FAIL(8);
2459         itut_t35.country_code_extension_byte = static_cast<uint8_t>(scratch);
2460         ++data;
2461         --size;
2462       }
2463       // Read itut_t35.payload_bytes. Section 6.7.2 of the spec says:
2464       //   itut_t35.payload_bytes shall be bytes containing data registered as
2465       //   specified in Recommendation ITU-T T.35.
2466       // Therefore itut_t35.payload_bytes is byte aligned and the first trailing
2467       // byte should be 0x80. Since the exact syntax of itut_t35.payload_bytes
2468       // is not defined in the AV1 spec, identify the end of
2469       // itut_t35.payload_bytes by searching for the trailing bit.
2470       const int i = GetLastNonzeroByteIndex(data, size);
2471       if (i < 0) {
2472         LIBGAV1_DLOG(ERROR, "Trailing bit is missing.");
2473         return false;
2474       }
2475       if (data[i] != 0x80) {
2476         LIBGAV1_DLOG(
2477             ERROR,
2478             "itut_t35.payload_bytes is not byte aligned. The last nonzero byte "
2479             "of the payload data is 0x%x, should be 0x80.",
2480             data[i]);
2481         return false;
2482       }
2483       itut_t35.payload_size = i;
2484       if (!EnsureCurrentFrameIsNotNull() ||
2485           !current_frame_->set_itut_t35(itut_t35, data)) {
2486         return false;
2487       }
2488       // Skip all bits before the trailing bit.
2489       bit_reader_->SkipBytes(i);
2490       break;
2491     }
2492     case kMetadataTypeTimecode:
2493       if (!ParseMetadataTimecode()) return false;
2494       break;
2495     default: {
2496       // metadata_type is equal to a value reserved for future use or a user
2497       // private value.
2498       //
2499       // The Note in Section 5.8.1 says "Decoders should ignore the entire OBU
2500       // if they do not understand the metadata_type." Find the trailing bit
2501       // and skip all bits before the trailing bit.
2502       const int i = GetLastNonzeroByteIndex(data, size);
2503       if (i >= 0) {
2504         // The last 1 bit in the last nonzero byte is the trailing bit. Skip
2505         // all bits before the trailing bit.
2506         const int n = CountTrailingZeros(data[i]);
2507         bit_reader_->SkipBits(i * 8 + 7 - n);
2508       }
2509       break;
2510     }
2511   }
2512   return true;
2513 }
2514 
AddTileBuffers(int start,int end,size_t total_size,size_t tg_header_size,size_t bytes_consumed_so_far)2515 bool ObuParser::AddTileBuffers(int start, int end, size_t total_size,
2516                                size_t tg_header_size,
2517                                size_t bytes_consumed_so_far) {
2518   // Validate that the tile group start and end are within the allowed range.
2519   if (start != next_tile_group_start_ || start > end ||
2520       end >= frame_header_.tile_info.tile_count) {
2521     LIBGAV1_DLOG(ERROR,
2522                  "Invalid tile group start %d or end %d: expected tile group "
2523                  "start %d, tile_count %d.",
2524                  start, end, next_tile_group_start_,
2525                  frame_header_.tile_info.tile_count);
2526     return false;
2527   }
2528   next_tile_group_start_ = end + 1;
2529 
2530   if (total_size < tg_header_size) {
2531     LIBGAV1_DLOG(ERROR, "total_size (%zu) is less than tg_header_size (%zu).)",
2532                  total_size, tg_header_size);
2533     return false;
2534   }
2535   size_t bytes_left = total_size - tg_header_size;
2536   const uint8_t* data = data_ + bytes_consumed_so_far + tg_header_size;
2537   for (int tile_number = start; tile_number <= end; ++tile_number) {
2538     size_t tile_size = 0;
2539     if (tile_number != end) {
2540       RawBitReader bit_reader(data, bytes_left);
2541       if (!bit_reader.ReadLittleEndian(frame_header_.tile_info.tile_size_bytes,
2542                                        &tile_size)) {
2543         LIBGAV1_DLOG(ERROR, "Could not read tile size for tile #%d",
2544                      tile_number);
2545         return false;
2546       }
2547       ++tile_size;
2548       data += frame_header_.tile_info.tile_size_bytes;
2549       bytes_left -= frame_header_.tile_info.tile_size_bytes;
2550       if (tile_size > bytes_left) {
2551         LIBGAV1_DLOG(ERROR, "Invalid tile size %zu for tile #%d", tile_size,
2552                      tile_number);
2553         return false;
2554       }
2555     } else {
2556       tile_size = bytes_left;
2557       if (tile_size == 0) {
2558         LIBGAV1_DLOG(ERROR, "Invalid tile size %zu for tile #%d", tile_size,
2559                      tile_number);
2560         return false;
2561       }
2562     }
2563     // The memory for this has been allocated in ParseTileInfoSyntax(). So it is
2564     // safe to use push_back_unchecked here.
2565     tile_buffers_.push_back_unchecked({data, tile_size});
2566     data += tile_size;
2567     bytes_left -= tile_size;
2568   }
2569   bit_reader_->SkipBytes(total_size - tg_header_size);
2570   return true;
2571 }
2572 
ParseTileGroup(size_t size,size_t bytes_consumed_so_far)2573 bool ObuParser::ParseTileGroup(size_t size, size_t bytes_consumed_so_far) {
2574   const TileInfo* const tile_info = &frame_header_.tile_info;
2575   const size_t start_offset = bit_reader_->byte_offset();
2576   const int tile_bits =
2577       tile_info->tile_columns_log2 + tile_info->tile_rows_log2;
2578   if (tile_bits == 0) {
2579     return AddTileBuffers(0, 0, size, 0, bytes_consumed_so_far);
2580   }
2581   int64_t scratch;
2582   OBU_READ_BIT_OR_FAIL;
2583   const bool tile_start_and_end_present_flag = scratch != 0;
2584   if (!tile_start_and_end_present_flag) {
2585     if (!bit_reader_->AlignToNextByte()) {
2586       LIBGAV1_DLOG(ERROR, "Byte alignment has non zero bits.");
2587       return false;
2588     }
2589     return AddTileBuffers(0, tile_info->tile_count - 1, size, 1,
2590                           bytes_consumed_so_far);
2591   }
2592   if (obu_headers_.back().type == kObuFrame) {
2593     // 6.10.1: If obu_type is equal to OBU_FRAME, it is a requirement of
2594     // bitstream conformance that the value of tile_start_and_end_present_flag
2595     // is equal to 0.
2596     LIBGAV1_DLOG(ERROR,
2597                  "tile_start_and_end_present_flag must be 0 in Frame OBU");
2598     return false;
2599   }
2600   OBU_READ_LITERAL_OR_FAIL(tile_bits);
2601   const int start = static_cast<int>(scratch);
2602   OBU_READ_LITERAL_OR_FAIL(tile_bits);
2603   const int end = static_cast<int>(scratch);
2604   if (!bit_reader_->AlignToNextByte()) {
2605     LIBGAV1_DLOG(ERROR, "Byte alignment has non zero bits.");
2606     return false;
2607   }
2608   const size_t tg_header_size = bit_reader_->byte_offset() - start_offset;
2609   return AddTileBuffers(start, end, size, tg_header_size,
2610                         bytes_consumed_so_far);
2611 }
2612 
ParseHeader()2613 bool ObuParser::ParseHeader() {
2614   ObuHeader obu_header;
2615   int64_t scratch = bit_reader_->ReadBit();
2616   if (scratch != 0) {
2617     LIBGAV1_DLOG(ERROR, "forbidden_bit is not zero.");
2618     return false;
2619   }
2620   OBU_READ_LITERAL_OR_FAIL(4);
2621   obu_header.type = static_cast<libgav1::ObuType>(scratch);
2622   OBU_READ_BIT_OR_FAIL;
2623   const bool extension_flag = scratch != 0;
2624   OBU_READ_BIT_OR_FAIL;
2625   obu_header.has_size_field = scratch != 0;
2626   OBU_READ_BIT_OR_FAIL;  // reserved.
2627   if (scratch != 0) {
2628     LIBGAV1_DLOG(WARNING, "obu_reserved_1bit is not zero.");
2629   }
2630   obu_header.has_extension = extension_flag;
2631   if (extension_flag) {
2632     if (extension_disallowed_) {
2633 #ifdef CHROMIUM
2634       LIBGAV1_DLOG(WARNING,
2635                    "OperatingPointIdc is 0, but obu_extension_flag is 1.");
2636 #else   // !CHROMIUM
2637       LIBGAV1_DLOG(ERROR,
2638                    "OperatingPointIdc is 0, but obu_extension_flag is 1.");
2639       return false;
2640 #endif  // CHROMIUM
2641     }
2642     OBU_READ_LITERAL_OR_FAIL(3);
2643     obu_header.temporal_id = scratch;
2644     OBU_READ_LITERAL_OR_FAIL(2);
2645     obu_header.spatial_id = scratch;
2646     OBU_READ_LITERAL_OR_FAIL(3);  // reserved.
2647     if (scratch != 0) {
2648       LIBGAV1_DLOG(WARNING, "extension_header_reserved_3bits is not zero.");
2649     }
2650   } else {
2651     obu_header.temporal_id = 0;
2652     obu_header.spatial_id = 0;
2653   }
2654   return obu_headers_.push_back(obu_header);
2655 }
2656 
2657 #undef OBU_READ_UVLC_OR_FAIL
2658 #undef OBU_READ_LITERAL_OR_FAIL
2659 #undef OBU_READ_BIT_OR_FAIL
2660 #undef OBU_PARSER_FAIL
2661 #undef OBU_LOG_AND_RETURN_FALSE
2662 
InitBitReader(const uint8_t * const data,size_t size)2663 bool ObuParser::InitBitReader(const uint8_t* const data, size_t size) {
2664   bit_reader_.reset(new (std::nothrow) RawBitReader(data, size));
2665   return bit_reader_ != nullptr;
2666 }
2667 
EnsureCurrentFrameIsNotNull()2668 bool ObuParser::EnsureCurrentFrameIsNotNull() {
2669   if (current_frame_ != nullptr) return true;
2670   current_frame_ = buffer_pool_->GetFreeBuffer();
2671   if (current_frame_ == nullptr) {
2672     LIBGAV1_DLOG(ERROR, "Could not get current_frame from the buffer pool.");
2673     return false;
2674   }
2675   return true;
2676 }
2677 
HasData() const2678 bool ObuParser::HasData() const { return size_ > 0; }
2679 
ParseOneFrame(RefCountedBufferPtr * const current_frame)2680 StatusCode ObuParser::ParseOneFrame(RefCountedBufferPtr* const current_frame) {
2681   if (data_ == nullptr || size_ == 0) return kStatusInvalidArgument;
2682 
2683   assert(current_frame_ == nullptr);
2684   // This is used to release any references held in case of parsing failure.
2685   RefCountedBufferPtrCleanup current_frame_cleanup(&current_frame_);
2686 
2687   const uint8_t* data = data_;
2688   size_t size = size_;
2689 
2690   // Clear everything except the sequence header.
2691   obu_headers_.clear();
2692   frame_header_ = {};
2693   tile_buffers_.clear();
2694   next_tile_group_start_ = 0;
2695   sequence_header_changed_ = false;
2696 
2697   bool parsed_one_full_frame = false;
2698   bool seen_frame_header = false;
2699   const uint8_t* frame_header = nullptr;
2700   size_t frame_header_size_in_bits = 0;
2701   while (size > 0 && !parsed_one_full_frame) {
2702     if (!InitBitReader(data, size)) {
2703       LIBGAV1_DLOG(ERROR, "Failed to initialize bit reader.");
2704       return kStatusOutOfMemory;
2705     }
2706     if (!ParseHeader()) {
2707       LIBGAV1_DLOG(ERROR, "Failed to parse OBU Header.");
2708       return kStatusBitstreamError;
2709     }
2710     const ObuHeader& obu_header = obu_headers_.back();
2711     if (!obu_header.has_size_field) {
2712       LIBGAV1_DLOG(
2713           ERROR,
2714           "has_size_field is zero. libgav1 does not support such streams.");
2715       return kStatusUnimplemented;
2716     }
2717     const size_t obu_header_size = bit_reader_->byte_offset();
2718     size_t obu_size;
2719     if (!bit_reader_->ReadUnsignedLeb128(&obu_size)) {
2720       LIBGAV1_DLOG(ERROR, "Could not read OBU size.");
2721       return kStatusBitstreamError;
2722     }
2723     const size_t obu_length_size = bit_reader_->byte_offset() - obu_header_size;
2724     if (size - bit_reader_->byte_offset() < obu_size) {
2725       LIBGAV1_DLOG(ERROR, "Not enough bits left to parse OBU %zu vs %zu.",
2726                    size - bit_reader_->bit_offset(), obu_size);
2727       return kStatusBitstreamError;
2728     }
2729 
2730     const ObuType obu_type = obu_header.type;
2731     if (obu_type != kObuSequenceHeader && obu_type != kObuTemporalDelimiter &&
2732         has_sequence_header_ &&
2733         sequence_header_.operating_point_idc[operating_point_] != 0 &&
2734         obu_header.has_extension &&
2735         (!InTemporalLayer(
2736              sequence_header_.operating_point_idc[operating_point_],
2737              obu_header.temporal_id) ||
2738          !InSpatialLayer(sequence_header_.operating_point_idc[operating_point_],
2739                          obu_header.spatial_id))) {
2740       obu_headers_.pop_back();
2741       bit_reader_->SkipBytes(obu_size);
2742       data += bit_reader_->byte_offset();
2743       size -= bit_reader_->byte_offset();
2744       continue;
2745     }
2746 
2747     const size_t obu_start_position = bit_reader_->bit_offset();
2748     // The bit_reader_ is byte aligned after reading obu_header and obu_size.
2749     // Therefore the byte offset can be computed as obu_start_position >> 3
2750     // below.
2751     assert((obu_start_position & 7) == 0);
2752     bool obu_skipped = false;
2753     switch (obu_type) {
2754       case kObuTemporalDelimiter:
2755         break;
2756       case kObuSequenceHeader:
2757         if (!ParseSequenceHeader(seen_frame_header)) {
2758           LIBGAV1_DLOG(ERROR, "Failed to parse SequenceHeader OBU.");
2759           return kStatusBitstreamError;
2760         }
2761         if (sequence_header_.color_config.bitdepth > LIBGAV1_MAX_BITDEPTH) {
2762           LIBGAV1_DLOG(
2763               ERROR,
2764               "Bitdepth %d is not supported. The maximum bitdepth is %d.",
2765               sequence_header_.color_config.bitdepth, LIBGAV1_MAX_BITDEPTH);
2766           return kStatusUnimplemented;
2767         }
2768         break;
2769       case kObuFrameHeader:
2770         if (seen_frame_header) {
2771           LIBGAV1_DLOG(ERROR,
2772                        "Frame header found but frame header was already seen.");
2773           return kStatusBitstreamError;
2774         }
2775         if (!ParseFrameHeader()) {
2776           LIBGAV1_DLOG(ERROR, "Failed to parse FrameHeader OBU.");
2777           return kStatusBitstreamError;
2778         }
2779         frame_header = &data[obu_start_position >> 3];
2780         frame_header_size_in_bits =
2781             bit_reader_->bit_offset() - obu_start_position;
2782         seen_frame_header = true;
2783         parsed_one_full_frame = frame_header_.show_existing_frame;
2784         break;
2785       case kObuRedundantFrameHeader: {
2786         if (!seen_frame_header) {
2787           LIBGAV1_DLOG(ERROR,
2788                        "Redundant frame header found but frame header was not "
2789                        "yet seen.");
2790           return kStatusBitstreamError;
2791         }
2792         const size_t fh_size = (frame_header_size_in_bits + 7) >> 3;
2793         if (obu_size < fh_size ||
2794             memcmp(frame_header, &data[obu_start_position >> 3], fh_size) !=
2795                 0) {
2796           LIBGAV1_DLOG(ERROR,
2797                        "Redundant frame header differs from frame header.");
2798           return kStatusBitstreamError;
2799         }
2800         bit_reader_->SkipBits(frame_header_size_in_bits);
2801         break;
2802       }
2803       case kObuFrame: {
2804         const size_t fh_start_offset = bit_reader_->byte_offset();
2805         if (seen_frame_header) {
2806           LIBGAV1_DLOG(ERROR,
2807                        "Frame header found but frame header was already seen.");
2808           return kStatusBitstreamError;
2809         }
2810         if (!ParseFrameHeader()) {
2811           LIBGAV1_DLOG(ERROR, "Failed to parse FrameHeader in Frame OBU.");
2812           return kStatusBitstreamError;
2813         }
2814         // Section 6.8.2: If obu_type is equal to OBU_FRAME, it is a
2815         // requirement of bitstream conformance that show_existing_frame is
2816         // equal to 0.
2817         if (frame_header_.show_existing_frame) {
2818           LIBGAV1_DLOG(ERROR, "Frame OBU cannot set show_existing_frame to 1.");
2819           return kStatusBitstreamError;
2820         }
2821         if (!bit_reader_->AlignToNextByte()) {
2822           LIBGAV1_DLOG(ERROR, "Byte alignment has non zero bits.");
2823           return kStatusBitstreamError;
2824         }
2825         const size_t fh_size = bit_reader_->byte_offset() - fh_start_offset;
2826         if (fh_size >= obu_size) {
2827           LIBGAV1_DLOG(ERROR, "Frame header size (%zu) >= obu_size (%zu).",
2828                        fh_size, obu_size);
2829           return kStatusBitstreamError;
2830         }
2831         if (!ParseTileGroup(obu_size - fh_size,
2832                             size_ - size + bit_reader_->byte_offset())) {
2833           LIBGAV1_DLOG(ERROR, "Failed to parse TileGroup in Frame OBU.");
2834           return kStatusBitstreamError;
2835         }
2836         parsed_one_full_frame = true;
2837         break;
2838       }
2839       case kObuTileGroup:
2840         if (!ParseTileGroup(obu_size,
2841                             size_ - size + bit_reader_->byte_offset())) {
2842           LIBGAV1_DLOG(ERROR, "Failed to parse TileGroup OBU.");
2843           return kStatusBitstreamError;
2844         }
2845         parsed_one_full_frame =
2846             (next_tile_group_start_ == frame_header_.tile_info.tile_count);
2847         break;
2848       case kObuTileList:
2849         LIBGAV1_DLOG(ERROR, "Decoding of tile list OBUs is not supported.");
2850         return kStatusUnimplemented;
2851       case kObuPadding:
2852         if (!ParsePadding(&data[obu_start_position >> 3], obu_size)) {
2853           LIBGAV1_DLOG(ERROR, "Failed to parse Padding OBU.");
2854           return kStatusBitstreamError;
2855         }
2856         break;
2857       case kObuMetadata:
2858         if (!ParseMetadata(&data[obu_start_position >> 3], obu_size)) {
2859           LIBGAV1_DLOG(ERROR, "Failed to parse Metadata OBU.");
2860           return kStatusBitstreamError;
2861         }
2862         break;
2863       default:
2864         // Skip reserved OBUs. Section 6.2.2: Reserved units are for future use
2865         // and shall be ignored by AV1 decoder.
2866         bit_reader_->SkipBytes(obu_size);
2867         obu_skipped = true;
2868         break;
2869     }
2870     if (obu_size > 0 && !obu_skipped && obu_type != kObuFrame &&
2871         obu_type != kObuTileGroup) {
2872       const size_t parsed_obu_size_in_bits =
2873           bit_reader_->bit_offset() - obu_start_position;
2874       if (obu_size * 8 < parsed_obu_size_in_bits) {
2875         LIBGAV1_DLOG(
2876             ERROR,
2877             "Parsed OBU size (%zu bits) is greater than expected OBU size "
2878             "(%zu bytes) obu_type: %d.",
2879             parsed_obu_size_in_bits, obu_size, obu_type);
2880         return kStatusBitstreamError;
2881       }
2882       if (!bit_reader_->VerifyAndSkipTrailingBits(obu_size * 8 -
2883                                                   parsed_obu_size_in_bits)) {
2884         LIBGAV1_DLOG(ERROR,
2885                      "Error when verifying trailing bits for obu type: %d",
2886                      obu_type);
2887         return kStatusBitstreamError;
2888       }
2889     }
2890     const size_t bytes_consumed = bit_reader_->byte_offset();
2891     const size_t consumed_obu_size =
2892         bytes_consumed - obu_length_size - obu_header_size;
2893     if (consumed_obu_size != obu_size) {
2894       LIBGAV1_DLOG(ERROR,
2895                    "OBU size (%zu) and consumed size (%zu) does not match for "
2896                    "obu_type: %d.",
2897                    obu_size, consumed_obu_size, obu_type);
2898       return kStatusBitstreamError;
2899     }
2900     data += bytes_consumed;
2901     size -= bytes_consumed;
2902   }
2903   if (!parsed_one_full_frame && seen_frame_header) {
2904     LIBGAV1_DLOG(ERROR, "The last tile group in the frame was not received.");
2905     return kStatusBitstreamError;
2906   }
2907   data_ = data;
2908   size_ = size;
2909   *current_frame = std::move(current_frame_);
2910   return kStatusOk;
2911 }
2912 
2913 // AV1CodecConfigurationBox specification:
2914 // https://aomediacodec.github.io/av1-isobmff/#av1codecconfigurationbox.
2915 // static
GetAV1CodecConfigurationBox(const uint8_t * data,size_t size,size_t * const av1c_size)2916 std::unique_ptr<uint8_t[]> ObuParser::GetAV1CodecConfigurationBox(
2917     const uint8_t* data, size_t size, size_t* const av1c_size) {
2918   if (data == nullptr || av1c_size == nullptr) return nullptr;
2919 
2920   ObuSequenceHeader sequence_header;
2921   size_t sequence_header_offset;
2922   size_t sequence_header_size;
2923   const StatusCode status =
2924       ParseBasicStreamInfo(data, size, &sequence_header,
2925                            &sequence_header_offset, &sequence_header_size);
2926   if (status != kStatusOk) {
2927     *av1c_size = 0;
2928     return nullptr;
2929   }
2930 
2931   *av1c_size = 4 + sequence_header_size;
2932   std::unique_ptr<uint8_t[]> av1c_ptr(new (std::nothrow) uint8_t[*av1c_size]);
2933   if (av1c_ptr == nullptr) {
2934     *av1c_size = 0;
2935     return nullptr;
2936   }
2937   uint8_t* av1c = av1c_ptr.get();
2938   // unsigned int (1) marker = 1;
2939   // unsigned int (7) version = 1;
2940   av1c[0] = 0x81;
2941 
2942   // unsigned int (3) seq_profile;
2943   // unsigned int (5) seq_level_idx_0;
2944   const uint8_t seq_level_idx_0 = ((sequence_header.level[0].major - 2) << 2) |
2945                                   sequence_header.level[0].minor;
2946   av1c[1] = (sequence_header.profile << 5) | seq_level_idx_0;
2947 
2948   // unsigned int (1) seq_tier_0;
2949   // unsigned int (1) high_bitdepth;
2950   // unsigned int (1) twelve_bit;
2951   // unsigned int (1) monochrome;
2952   // unsigned int (1) chroma_subsampling_x;
2953   // unsigned int (1) chroma_subsampling_y;
2954   // unsigned int (2) chroma_sample_position;
2955   const auto high_bitdepth =
2956       static_cast<uint8_t>(sequence_header.color_config.bitdepth > 8);
2957   const auto twelve_bit =
2958       static_cast<uint8_t>(sequence_header.color_config.bitdepth == 12);
2959   av1c[2] =
2960       (sequence_header.tier[0] << 7) | (high_bitdepth << 6) |
2961       (twelve_bit << 5) |
2962       (static_cast<uint8_t>(sequence_header.color_config.is_monochrome) << 4) |
2963       (sequence_header.color_config.subsampling_x << 3) |
2964       (sequence_header.color_config.subsampling_y << 2) |
2965       sequence_header.color_config.chroma_sample_position;
2966 
2967   // unsigned int (3) reserved = 0;
2968   // unsigned int (1) initial_presentation_delay_present;
2969   // if (initial_presentation_delay_present) {
2970   //   unsigned int (4) initial_presentation_delay_minus_one;
2971   // } else {
2972   //   unsigned int (4) reserved = 0;
2973   // }
2974   av1c[3] = 0;
2975 
2976   // unsigned int (8) configOBUs[];
2977   memcpy(av1c + 4, data + sequence_header_offset, sequence_header_size);
2978 
2979   return av1c_ptr;
2980 }
2981 
2982 // static
ParseBasicStreamInfo(const uint8_t * data,size_t size,ObuSequenceHeader * sequence_header,size_t * sequence_header_offset,size_t * sequence_header_size)2983 StatusCode ObuParser::ParseBasicStreamInfo(const uint8_t* data, size_t size,
2984                                            ObuSequenceHeader* sequence_header,
2985                                            size_t* sequence_header_offset,
2986                                            size_t* sequence_header_size) {
2987   DecoderState state;
2988   ObuParser parser(nullptr, 0, 0, nullptr, &state);
2989   if (!parser.InitBitReader(data, size)) {
2990     LIBGAV1_DLOG(ERROR, "Failed to initialize bit reader.");
2991     return kStatusOutOfMemory;
2992   }
2993   while (!parser.bit_reader_->Finished()) {
2994     const size_t obu_start_offset = parser.bit_reader_->byte_offset();
2995     if (!parser.ParseHeader()) {
2996       LIBGAV1_DLOG(ERROR, "Failed to parse OBU Header.");
2997       return kStatusBitstreamError;
2998     }
2999     const ObuHeader& obu_header = parser.obu_headers_.back();
3000     if (!obu_header.has_size_field) {
3001       LIBGAV1_DLOG(
3002           ERROR,
3003           "has_size_field is zero. libgav1 does not support such streams.");
3004       return kStatusUnimplemented;
3005     }
3006     size_t obu_size;
3007     if (!parser.bit_reader_->ReadUnsignedLeb128(&obu_size)) {
3008       LIBGAV1_DLOG(ERROR, "Could not read OBU size.");
3009       return kStatusBitstreamError;
3010     }
3011     if (size - parser.bit_reader_->byte_offset() < obu_size) {
3012       LIBGAV1_DLOG(ERROR, "Not enough bits left to parse OBU %zu vs %zu.",
3013                    size - parser.bit_reader_->bit_offset(), obu_size);
3014       return kStatusBitstreamError;
3015     }
3016     if (obu_header.type != kObuSequenceHeader) {
3017       parser.obu_headers_.pop_back();
3018       parser.bit_reader_->SkipBytes(obu_size);
3019       continue;
3020     }
3021     const size_t obu_start_position = parser.bit_reader_->bit_offset();
3022     if (!parser.ParseSequenceHeader(false)) {
3023       LIBGAV1_DLOG(ERROR, "Failed to parse SequenceHeader OBU.");
3024       return kStatusBitstreamError;
3025     }
3026     const size_t parsed_obu_size_in_bits =
3027         parser.bit_reader_->bit_offset() - obu_start_position;
3028     const uint64_t obu_size_in_bits = static_cast<uint64_t>(obu_size) * 8;
3029     if (obu_size_in_bits < parsed_obu_size_in_bits) {
3030       LIBGAV1_DLOG(
3031           ERROR,
3032           "Parsed OBU size (%zu bits) is greater than expected OBU size "
3033           "(%zu bytes).",
3034           parsed_obu_size_in_bits, obu_size);
3035       return kStatusBitstreamError;
3036     }
3037     if (!parser.bit_reader_->VerifyAndSkipTrailingBits(
3038             static_cast<size_t>(obu_size_in_bits - parsed_obu_size_in_bits))) {
3039       LIBGAV1_DLOG(
3040           ERROR, "Error when verifying trailing bits for the sequence header.");
3041       return kStatusBitstreamError;
3042     }
3043     *sequence_header = parser.sequence_header_;
3044     *sequence_header_offset = obu_start_offset;
3045     *sequence_header_size =
3046         parser.bit_reader_->byte_offset() - obu_start_offset;
3047     return kStatusOk;
3048   }
3049 
3050   LIBGAV1_DLOG(ERROR, "Sequence header was never found.");
3051   return kStatusBitstreamError;
3052 }
3053 
3054 }  // namespace libgav1
3055