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