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 bool high_bitdepth = scratch != 0;
144   if (sequence_header->profile == kProfile2 && high_bitdepth) {
145     OBU_READ_BIT_OR_FAIL;
146     const bool is_twelve_bit = scratch != 0;
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 = scratch != 0;
156   }
157   OBU_READ_BIT_OR_FAIL;
158   const bool color_description_present_flag = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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       scratch != 0;
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] = scratch != 0;
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 = scratch != 0;
323   OBU_READ_BIT_OR_FAIL;
324   sequence_header.reduced_still_picture_header = scratch != 0;
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 bool initial_display_delay_present_flag = scratch != 0;
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 (scratch != 0) {
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 = scratch != 0;
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 = scratch != 0;
413   OBU_READ_BIT_OR_FAIL;
414   sequence_header.enable_filter_intra = scratch != 0;
415   OBU_READ_BIT_OR_FAIL;
416   sequence_header.enable_intra_edge_filter = scratch != 0;
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 = scratch != 0;
423     OBU_READ_BIT_OR_FAIL;
424     sequence_header.enable_masked_compound = scratch != 0;
425     OBU_READ_BIT_OR_FAIL;
426     sequence_header.enable_warped_motion = scratch != 0;
427     OBU_READ_BIT_OR_FAIL;
428     sequence_header.enable_dual_filter = scratch != 0;
429     OBU_READ_BIT_OR_FAIL;
430     sequence_header.enable_order_hint = scratch != 0;
431     if (sequence_header.enable_order_hint) {
432       OBU_READ_BIT_OR_FAIL;
433       sequence_header.enable_jnt_comp = scratch != 0;
434       OBU_READ_BIT_OR_FAIL;
435       sequence_header.enable_ref_frame_mvs = scratch != 0;
436     }
437     OBU_READ_BIT_OR_FAIL;
438     sequence_header.choose_screen_content_tools = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
466   OBU_READ_BIT_OR_FAIL;
467   sequence_header.enable_cdef = scratch != 0;
468   OBU_READ_BIT_OR_FAIL;
469   sequence_header.enable_restoration = scratch != 0;
470   if (!ParseColorConfig(&sequence_header)) return false;
471   OBU_READ_BIT_OR_FAIL;
472   sequence_header.film_grain_params_present = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
882   if (loop_filter->delta_enabled) {
883     OBU_READ_BIT_OR_FAIL;
884     loop_filter->delta_update = scratch != 0;
885     if (loop_filter->delta_update) {
886       for (auto& ref_delta : loop_filter->ref_deltas) {
887         OBU_READ_BIT_OR_FAIL;
888         const bool update_ref_delta = scratch != 0;
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 bool update_mode_delta = scratch != 0;
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 bool delta_coded = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
998     if (segmentation->update_map) {
999       OBU_READ_BIT_OR_FAIL;
1000       segmentation->temporal_update = scratch != 0;
1001     }
1002     OBU_READ_BIT_OR_FAIL;
1003     segmentation->update_data = scratch != 0;
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] = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 bool is_global = scratch != 0;
1352     if (is_global) {
1353       OBU_READ_BIT_OR_FAIL;
1354       const bool is_rot_zoom = scratch != 0;
1355       if (is_rot_zoom) {
1356         global_motion->type = kGlobalMotionTransformationTypeRotZoom;
1357       } else {
1358         OBU_READ_BIT_OR_FAIL;
1359         const bool is_translation = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
1601   OBU_READ_BIT_OR_FAIL;
1602   film_grain_params.clip_to_restricted_range = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch == 0;
1881   if (sequence_header_.force_screen_content_tools ==
1882       kSelectScreenContentTools) {
1883     OBU_READ_BIT_OR_FAIL;
1884     frame_header_.allow_screen_content_tools = scratch != 0;
1885   } else {
1886     frame_header_.allow_screen_content_tools =
1887         sequence_header_.force_screen_content_tools != 0;
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 = scratch != 0;
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 bool buffer_removal_time_present = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
2083     }
2084     OBU_READ_BIT_OR_FAIL;
2085     const bool is_filter_switchable = scratch != 0;
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 = scratch != 0;
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 = scratch != 0;
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 = scratch == 0;
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 = scratch != 0;
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 = scratch != 0;
2240     // spatial_layer_description_present_flag
2241     OBU_READ_BIT_OR_FAIL;
2242     const auto spatial_layer_description_present_flag = scratch != 0;
2243     // temporal_group_description_present_flag
2244     OBU_READ_BIT_OR_FAIL;
2245     const auto temporal_group_description_present_flag = scratch != 0;
2246     // scalability_structure_reserved_3bits
2247     OBU_READ_LITERAL_OR_FAIL(3);
2248     if (scratch != 0) {
2249       LIBGAV1_DLOG(WARNING,
2250                    "scalability_structure_reserved_3bits is not zero.");
2251     }
2252     if (spatial_layer_dimensions_present_flag) {
2253       for (int i = 0; i < spatial_layers_count; ++i) {
2254         // spatial_layer_max_width[i]
2255         OBU_READ_LITERAL_OR_FAIL(16);
2256         // spatial_layer_max_height[i]
2257         OBU_READ_LITERAL_OR_FAIL(16);
2258       }
2259     }
2260     if (spatial_layer_description_present_flag) {
2261       for (int i = 0; i < spatial_layers_count; ++i) {
2262         // spatial_layer_ref_id[i]
2263         OBU_READ_LITERAL_OR_FAIL(8);
2264       }
2265     }
2266     if (temporal_group_description_present_flag) {
2267       // temporal_group_size
2268       OBU_READ_LITERAL_OR_FAIL(8);
2269       const auto temporal_group_size = static_cast<int>(scratch);
2270       for (int i = 0; i < temporal_group_size; ++i) {
2271         // temporal_group_temporal_id[i]
2272         OBU_READ_LITERAL_OR_FAIL(3);
2273         // temporal_group_temporal_switching_up_point_flag[i]
2274         OBU_READ_BIT_OR_FAIL;
2275         // temporal_group_spatial_switching_up_point_flag[i]
2276         OBU_READ_BIT_OR_FAIL;
2277         // temporal_group_ref_cnt[i]
2278         OBU_READ_LITERAL_OR_FAIL(3);
2279         const auto temporal_group_ref_count = static_cast<int>(scratch);
2280         for (int j = 0; j < temporal_group_ref_count; ++j) {
2281           // temporal_group_ref_pic_diff[i][j]
2282           OBU_READ_LITERAL_OR_FAIL(8);
2283         }
2284       }
2285     }
2286   }
2287   return true;
2288 }
2289 
ParseMetadataTimecode()2290 bool ObuParser::ParseMetadataTimecode() {
2291   int64_t scratch;
2292   // counting_type: should be the same for all pictures in the coded video
2293   // sequence. 7..31 are reserved.
2294   OBU_READ_LITERAL_OR_FAIL(5);
2295   // full_timestamp_flag
2296   OBU_READ_BIT_OR_FAIL;
2297   const bool full_timestamp_flag = scratch != 0;
2298   // discontinuity_flag
2299   OBU_READ_BIT_OR_FAIL;
2300   // cnt_dropped_flag
2301   OBU_READ_BIT_OR_FAIL;
2302   // n_frames
2303   OBU_READ_LITERAL_OR_FAIL(9);
2304   if (full_timestamp_flag) {
2305     // seconds_value
2306     OBU_READ_LITERAL_OR_FAIL(6);
2307     const auto seconds_value = static_cast<int>(scratch);
2308     if (seconds_value > 59) {
2309       LIBGAV1_DLOG(ERROR, "Invalid seconds_value %d.", seconds_value);
2310       return false;
2311     }
2312     // minutes_value
2313     OBU_READ_LITERAL_OR_FAIL(6);
2314     const auto minutes_value = static_cast<int>(scratch);
2315     if (minutes_value > 59) {
2316       LIBGAV1_DLOG(ERROR, "Invalid minutes_value %d.", minutes_value);
2317       return false;
2318     }
2319     // hours_value
2320     OBU_READ_LITERAL_OR_FAIL(5);
2321     const auto hours_value = static_cast<int>(scratch);
2322     if (hours_value > 23) {
2323       LIBGAV1_DLOG(ERROR, "Invalid hours_value %d.", hours_value);
2324       return false;
2325     }
2326   } else {
2327     // seconds_flag
2328     OBU_READ_BIT_OR_FAIL;
2329     const bool seconds_flag = scratch != 0;
2330     if (seconds_flag) {
2331       // seconds_value
2332       OBU_READ_LITERAL_OR_FAIL(6);
2333       const auto seconds_value = static_cast<int>(scratch);
2334       if (seconds_value > 59) {
2335         LIBGAV1_DLOG(ERROR, "Invalid seconds_value %d.", seconds_value);
2336         return false;
2337       }
2338       // minutes_flag
2339       OBU_READ_BIT_OR_FAIL;
2340       const bool minutes_flag = scratch != 0;
2341       if (minutes_flag) {
2342         // minutes_value
2343         OBU_READ_LITERAL_OR_FAIL(6);
2344         const auto minutes_value = static_cast<int>(scratch);
2345         if (minutes_value > 59) {
2346           LIBGAV1_DLOG(ERROR, "Invalid minutes_value %d.", minutes_value);
2347           return false;
2348         }
2349         // hours_flag
2350         OBU_READ_BIT_OR_FAIL;
2351         const bool hours_flag = scratch != 0;
2352         if (hours_flag) {
2353           // hours_value
2354           OBU_READ_LITERAL_OR_FAIL(5);
2355           const auto hours_value = static_cast<int>(scratch);
2356           if (hours_value > 23) {
2357             LIBGAV1_DLOG(ERROR, "Invalid hours_value %d.", hours_value);
2358             return false;
2359           }
2360         }
2361       }
2362     }
2363   }
2364   // time_offset_length: should be the same for all pictures in the coded
2365   // video sequence.
2366   OBU_READ_LITERAL_OR_FAIL(5);
2367   const auto time_offset_length = static_cast<int>(scratch);
2368   if (time_offset_length > 0) {
2369     // time_offset_value
2370     OBU_READ_LITERAL_OR_FAIL(time_offset_length);
2371   }
2372   // Compute clockTimestamp. Section 6.7.7:
2373   //   When timing_info_present_flag is equal to 1 and discontinuity_flag is
2374   //   equal to 0, the value of clockTimestamp shall be greater than or equal
2375   //   to the value of clockTimestamp for the previous set of clock timestamp
2376   //   syntax elements in output order.
2377   return true;
2378 }
2379 
ParseMetadata(const uint8_t * data,size_t size)2380 bool ObuParser::ParseMetadata(const uint8_t* data, size_t size) {
2381   const size_t start_offset = bit_reader_->byte_offset();
2382   size_t metadata_type;
2383   if (!bit_reader_->ReadUnsignedLeb128(&metadata_type)) {
2384     LIBGAV1_DLOG(ERROR, "Could not read metadata_type.");
2385     return false;
2386   }
2387   const size_t metadata_type_size = bit_reader_->byte_offset() - start_offset;
2388   if (size < metadata_type_size) {
2389     LIBGAV1_DLOG(
2390         ERROR, "metadata_type is longer than metadata OBU payload %zu vs %zu.",
2391         metadata_type_size, size);
2392     return false;
2393   }
2394   data += metadata_type_size;
2395   size -= metadata_type_size;
2396   int64_t scratch;
2397   switch (metadata_type) {
2398     case kMetadataTypeHdrContentLightLevel:
2399       OBU_READ_LITERAL_OR_FAIL(16);
2400       metadata_.max_cll = scratch;
2401       OBU_READ_LITERAL_OR_FAIL(16);
2402       metadata_.max_fall = scratch;
2403       break;
2404     case kMetadataTypeHdrMasteringDisplayColorVolume:
2405       for (int i = 0; i < 3; ++i) {
2406         OBU_READ_LITERAL_OR_FAIL(16);
2407         metadata_.primary_chromaticity_x[i] = scratch;
2408         OBU_READ_LITERAL_OR_FAIL(16);
2409         metadata_.primary_chromaticity_y[i] = scratch;
2410       }
2411       OBU_READ_LITERAL_OR_FAIL(16);
2412       metadata_.white_point_chromaticity_x = scratch;
2413       OBU_READ_LITERAL_OR_FAIL(16);
2414       metadata_.white_point_chromaticity_y = scratch;
2415       OBU_READ_LITERAL_OR_FAIL(32);
2416       metadata_.luminance_max = static_cast<uint32_t>(scratch);
2417       OBU_READ_LITERAL_OR_FAIL(32);
2418       metadata_.luminance_min = static_cast<uint32_t>(scratch);
2419       break;
2420     case kMetadataTypeScalability:
2421       if (!ParseMetadataScalability()) return false;
2422       break;
2423     case kMetadataTypeItutT35: {
2424       OBU_READ_LITERAL_OR_FAIL(8);
2425       metadata_.itu_t_t35_country_code = static_cast<uint8_t>(scratch);
2426       ++data;
2427       --size;
2428       if (metadata_.itu_t_t35_country_code == 0xFF) {
2429         OBU_READ_LITERAL_OR_FAIL(8);
2430         metadata_.itu_t_t35_country_code_extension_byte =
2431             static_cast<uint8_t>(scratch);
2432         ++data;
2433         --size;
2434       }
2435       // Read itu_t_t35_payload_bytes. Section 6.7.2 of the spec says:
2436       //   itu_t_t35_payload_bytes shall be bytes containing data registered as
2437       //   specified in Recommendation ITU-T T.35.
2438       // Therefore itu_t_t35_payload_bytes is byte aligned and the first
2439       // trailing byte should be 0x80. Since the exact syntax of
2440       // itu_t_t35_payload_bytes is not defined in the AV1 spec, identify the
2441       // end of itu_t_t35_payload_bytes by searching for the trailing bit.
2442       const int i = GetLastNonzeroByteIndex(data, size);
2443       if (i < 0) {
2444         LIBGAV1_DLOG(ERROR, "Trailing bit is missing.");
2445         return false;
2446       }
2447       if (data[i] != 0x80) {
2448         LIBGAV1_DLOG(
2449             ERROR,
2450             "itu_t_t35_payload_bytes is not byte aligned. The last nonzero "
2451             "byte of the payload data is 0x%x, should be 0x80.",
2452             data[i]);
2453         return false;
2454       }
2455       if (i != 0) {
2456         // data[0]..data[i - 1] are itu_t_t35_payload_bytes.
2457         metadata_.itu_t_t35_payload_bytes.reset(new (std::nothrow) uint8_t[i]);
2458         if (metadata_.itu_t_t35_payload_bytes == nullptr) {
2459           LIBGAV1_DLOG(ERROR, "Allocation of itu_t_t35_payload_bytes failed.");
2460           return false;
2461         }
2462         memcpy(metadata_.itu_t_t35_payload_bytes.get(), data, i);
2463         metadata_.itu_t_t35_payload_size = i;
2464       }
2465       // Skip all bits before the trailing bit.
2466       bit_reader_->SkipBytes(i);
2467       break;
2468     }
2469     case kMetadataTypeTimecode:
2470       if (!ParseMetadataTimecode()) return false;
2471       break;
2472     default: {
2473       // metadata_type is equal to a value reserved for future use or a user
2474       // private value.
2475       //
2476       // The Note in Section 5.8.1 says "Decoders should ignore the entire OBU
2477       // if they do not understand the metadata_type." Find the trailing bit
2478       // and skip all bits before the trailing bit.
2479       const int i = GetLastNonzeroByteIndex(data, size);
2480       if (i >= 0) {
2481         // The last 1 bit in the last nonzero byte is the trailing bit. Skip
2482         // all bits before the trailing bit.
2483         const int n = CountTrailingZeros(data[i]);
2484         bit_reader_->SkipBits(i * 8 + 7 - n);
2485       }
2486       break;
2487     }
2488   }
2489   return true;
2490 }
2491 
AddTileBuffers(int start,int end,size_t total_size,size_t tg_header_size,size_t bytes_consumed_so_far)2492 bool ObuParser::AddTileBuffers(int start, int end, size_t total_size,
2493                                size_t tg_header_size,
2494                                size_t bytes_consumed_so_far) {
2495   // Validate that the tile group start and end are within the allowed range.
2496   if (start != next_tile_group_start_ || start > end ||
2497       end >= frame_header_.tile_info.tile_count) {
2498     LIBGAV1_DLOG(ERROR,
2499                  "Invalid tile group start %d or end %d: expected tile group "
2500                  "start %d, tile_count %d.",
2501                  start, end, next_tile_group_start_,
2502                  frame_header_.tile_info.tile_count);
2503     return false;
2504   }
2505   next_tile_group_start_ = end + 1;
2506 
2507   if (total_size < tg_header_size) {
2508     LIBGAV1_DLOG(ERROR, "total_size (%zu) is less than tg_header_size (%zu).)",
2509                  total_size, tg_header_size);
2510     return false;
2511   }
2512   size_t bytes_left = total_size - tg_header_size;
2513   const uint8_t* data = data_ + bytes_consumed_so_far + tg_header_size;
2514   for (int tile_number = start; tile_number <= end; ++tile_number) {
2515     size_t tile_size = 0;
2516     if (tile_number != end) {
2517       RawBitReader bit_reader(data, bytes_left);
2518       if (!bit_reader.ReadLittleEndian(frame_header_.tile_info.tile_size_bytes,
2519                                        &tile_size)) {
2520         LIBGAV1_DLOG(ERROR, "Could not read tile size for tile #%d",
2521                      tile_number);
2522         return false;
2523       }
2524       ++tile_size;
2525       data += frame_header_.tile_info.tile_size_bytes;
2526       bytes_left -= frame_header_.tile_info.tile_size_bytes;
2527       if (tile_size > bytes_left) {
2528         LIBGAV1_DLOG(ERROR, "Invalid tile size %zu for tile #%d", tile_size,
2529                      tile_number);
2530         return false;
2531       }
2532     } else {
2533       tile_size = bytes_left;
2534       if (tile_size == 0) {
2535         LIBGAV1_DLOG(ERROR, "Invalid tile size %zu for tile #%d", tile_size,
2536                      tile_number);
2537         return false;
2538       }
2539     }
2540     // The memory for this has been allocated in ParseTileInfoSyntax(). So it is
2541     // safe to use push_back_unchecked here.
2542     tile_buffers_.push_back_unchecked({data, tile_size});
2543     data += tile_size;
2544     bytes_left -= tile_size;
2545   }
2546   bit_reader_->SkipBytes(total_size - tg_header_size);
2547   return true;
2548 }
2549 
ParseTileGroup(size_t size,size_t bytes_consumed_so_far)2550 bool ObuParser::ParseTileGroup(size_t size, size_t bytes_consumed_so_far) {
2551   const TileInfo* const tile_info = &frame_header_.tile_info;
2552   const size_t start_offset = bit_reader_->byte_offset();
2553   const int tile_bits =
2554       tile_info->tile_columns_log2 + tile_info->tile_rows_log2;
2555   if (tile_bits == 0) {
2556     return AddTileBuffers(0, 0, size, 0, bytes_consumed_so_far);
2557   }
2558   int64_t scratch;
2559   OBU_READ_BIT_OR_FAIL;
2560   const bool tile_start_and_end_present_flag = scratch != 0;
2561   if (!tile_start_and_end_present_flag) {
2562     if (!bit_reader_->AlignToNextByte()) {
2563       LIBGAV1_DLOG(ERROR, "Byte alignment has non zero bits.");
2564       return false;
2565     }
2566     return AddTileBuffers(0, tile_info->tile_count - 1, size, 1,
2567                           bytes_consumed_so_far);
2568   }
2569   if (obu_headers_.back().type == kObuFrame) {
2570     // 6.10.1: If obu_type is equal to OBU_FRAME, it is a requirement of
2571     // bitstream conformance that the value of tile_start_and_end_present_flag
2572     // is equal to 0.
2573     LIBGAV1_DLOG(ERROR,
2574                  "tile_start_and_end_present_flag must be 0 in Frame OBU");
2575     return false;
2576   }
2577   OBU_READ_LITERAL_OR_FAIL(tile_bits);
2578   const int start = static_cast<int>(scratch);
2579   OBU_READ_LITERAL_OR_FAIL(tile_bits);
2580   const int end = static_cast<int>(scratch);
2581   if (!bit_reader_->AlignToNextByte()) {
2582     LIBGAV1_DLOG(ERROR, "Byte alignment has non zero bits.");
2583     return false;
2584   }
2585   const size_t tg_header_size = bit_reader_->byte_offset() - start_offset;
2586   return AddTileBuffers(start, end, size, tg_header_size,
2587                         bytes_consumed_so_far);
2588 }
2589 
ParseHeader()2590 bool ObuParser::ParseHeader() {
2591   ObuHeader obu_header;
2592   int64_t scratch = bit_reader_->ReadBit();
2593   if (scratch != 0) {
2594     LIBGAV1_DLOG(ERROR, "forbidden_bit is not zero.");
2595     return false;
2596   }
2597   OBU_READ_LITERAL_OR_FAIL(4);
2598   obu_header.type = static_cast<libgav1::ObuType>(scratch);
2599   OBU_READ_BIT_OR_FAIL;
2600   const bool extension_flag = scratch != 0;
2601   OBU_READ_BIT_OR_FAIL;
2602   obu_header.has_size_field = scratch != 0;
2603   OBU_READ_BIT_OR_FAIL;  // reserved.
2604   if (scratch != 0) {
2605     LIBGAV1_DLOG(WARNING, "obu_reserved_1bit is not zero.");
2606   }
2607   obu_header.has_extension = extension_flag;
2608   if (extension_flag) {
2609     if (extension_disallowed_) {
2610       LIBGAV1_DLOG(ERROR,
2611                    "OperatingPointIdc is 0, but obu_extension_flag is 1.");
2612       return false;
2613     }
2614     OBU_READ_LITERAL_OR_FAIL(3);
2615     obu_header.temporal_id = scratch;
2616     OBU_READ_LITERAL_OR_FAIL(2);
2617     obu_header.spatial_id = scratch;
2618     OBU_READ_LITERAL_OR_FAIL(3);  // reserved.
2619     if (scratch != 0) {
2620       LIBGAV1_DLOG(WARNING, "extension_header_reserved_3bits is not zero.");
2621     }
2622   } else {
2623     obu_header.temporal_id = 0;
2624     obu_header.spatial_id = 0;
2625   }
2626   return obu_headers_.push_back(obu_header);
2627 }
2628 
2629 #undef OBU_READ_UVLC_OR_FAIL
2630 #undef OBU_READ_LITERAL_OR_FAIL
2631 #undef OBU_READ_BIT_OR_FAIL
2632 #undef OBU_PARSER_FAIL
2633 #undef OBU_LOG_AND_RETURN_FALSE
2634 
InitBitReader(const uint8_t * const data,size_t size)2635 bool ObuParser::InitBitReader(const uint8_t* const data, size_t size) {
2636   bit_reader_.reset(new (std::nothrow) RawBitReader(data, size));
2637   return bit_reader_ != nullptr;
2638 }
2639 
HasData() const2640 bool ObuParser::HasData() const { return size_ > 0; }
2641 
ParseOneFrame(RefCountedBufferPtr * const current_frame)2642 StatusCode ObuParser::ParseOneFrame(RefCountedBufferPtr* const current_frame) {
2643   if (data_ == nullptr || size_ == 0) return kStatusInvalidArgument;
2644 
2645   assert(current_frame_ == nullptr);
2646   // This is used to release any references held in case of parsing failure.
2647   RefCountedBufferPtrCleanup current_frame_cleanup(&current_frame_);
2648 
2649   const uint8_t* data = data_;
2650   size_t size = size_;
2651 
2652   // Clear everything except the sequence header.
2653   obu_headers_.clear();
2654   frame_header_ = {};
2655   metadata_ = {};
2656   tile_buffers_.clear();
2657   next_tile_group_start_ = 0;
2658   sequence_header_changed_ = false;
2659 
2660   bool parsed_one_full_frame = false;
2661   bool seen_frame_header = false;
2662   const uint8_t* frame_header = nullptr;
2663   size_t frame_header_size_in_bits = 0;
2664   while (size > 0 && !parsed_one_full_frame) {
2665     if (!InitBitReader(data, size)) {
2666       LIBGAV1_DLOG(ERROR, "Failed to initialize bit reader.");
2667       return kStatusOutOfMemory;
2668     }
2669     if (!ParseHeader()) {
2670       LIBGAV1_DLOG(ERROR, "Failed to parse OBU Header.");
2671       return kStatusBitstreamError;
2672     }
2673     const ObuHeader& obu_header = obu_headers_.back();
2674     if (!obu_header.has_size_field) {
2675       LIBGAV1_DLOG(
2676           ERROR,
2677           "has_size_field is zero. libgav1 does not support such streams.");
2678       return kStatusUnimplemented;
2679     }
2680     const size_t obu_header_size = bit_reader_->byte_offset();
2681     size_t obu_size;
2682     if (!bit_reader_->ReadUnsignedLeb128(&obu_size)) {
2683       LIBGAV1_DLOG(ERROR, "Could not read OBU size.");
2684       return kStatusBitstreamError;
2685     }
2686     const size_t obu_length_size = bit_reader_->byte_offset() - obu_header_size;
2687     if (size - bit_reader_->byte_offset() < obu_size) {
2688       LIBGAV1_DLOG(ERROR, "Not enough bits left to parse OBU %zu vs %zu.",
2689                    size - bit_reader_->bit_offset(), obu_size);
2690       return kStatusBitstreamError;
2691     }
2692 
2693     const ObuType obu_type = obu_header.type;
2694     if (obu_type != kObuSequenceHeader && obu_type != kObuTemporalDelimiter &&
2695         has_sequence_header_ &&
2696         sequence_header_.operating_point_idc[operating_point_] != 0 &&
2697         obu_header.has_extension &&
2698         (!InTemporalLayer(
2699              sequence_header_.operating_point_idc[operating_point_],
2700              obu_header.temporal_id) ||
2701          !InSpatialLayer(sequence_header_.operating_point_idc[operating_point_],
2702                          obu_header.spatial_id))) {
2703       obu_headers_.pop_back();
2704       bit_reader_->SkipBytes(obu_size);
2705       data += bit_reader_->byte_offset();
2706       size -= bit_reader_->byte_offset();
2707       continue;
2708     }
2709 
2710     const size_t obu_start_position = bit_reader_->bit_offset();
2711     // The bit_reader_ is byte aligned after reading obu_header and obu_size.
2712     // Therefore the byte offset can be computed as obu_start_position >> 3
2713     // below.
2714     assert((obu_start_position & 7) == 0);
2715     bool obu_skipped = false;
2716     switch (obu_type) {
2717       case kObuTemporalDelimiter:
2718         break;
2719       case kObuSequenceHeader:
2720         if (!ParseSequenceHeader(seen_frame_header)) {
2721           LIBGAV1_DLOG(ERROR, "Failed to parse SequenceHeader OBU.");
2722           return kStatusBitstreamError;
2723         }
2724         if (sequence_header_.color_config.bitdepth > LIBGAV1_MAX_BITDEPTH) {
2725           LIBGAV1_DLOG(
2726               ERROR,
2727               "Bitdepth %d is not supported. The maximum bitdepth is %d.",
2728               sequence_header_.color_config.bitdepth, LIBGAV1_MAX_BITDEPTH);
2729           return kStatusUnimplemented;
2730         }
2731         break;
2732       case kObuFrameHeader:
2733         if (seen_frame_header) {
2734           LIBGAV1_DLOG(ERROR,
2735                        "Frame header found but frame header was already seen.");
2736           return kStatusBitstreamError;
2737         }
2738         if (!ParseFrameHeader()) {
2739           LIBGAV1_DLOG(ERROR, "Failed to parse FrameHeader OBU.");
2740           return kStatusBitstreamError;
2741         }
2742         frame_header = &data[obu_start_position >> 3];
2743         frame_header_size_in_bits =
2744             bit_reader_->bit_offset() - obu_start_position;
2745         seen_frame_header = true;
2746         parsed_one_full_frame = frame_header_.show_existing_frame;
2747         break;
2748       case kObuRedundantFrameHeader: {
2749         if (!seen_frame_header) {
2750           LIBGAV1_DLOG(ERROR,
2751                        "Redundant frame header found but frame header was not "
2752                        "yet seen.");
2753           return kStatusBitstreamError;
2754         }
2755         const size_t fh_size = (frame_header_size_in_bits + 7) >> 3;
2756         if (obu_size < fh_size ||
2757             memcmp(frame_header, &data[obu_start_position >> 3], fh_size) !=
2758                 0) {
2759           LIBGAV1_DLOG(ERROR,
2760                        "Redundant frame header differs from frame header.");
2761           return kStatusBitstreamError;
2762         }
2763         bit_reader_->SkipBits(frame_header_size_in_bits);
2764         break;
2765       }
2766       case kObuFrame: {
2767         const size_t fh_start_offset = bit_reader_->byte_offset();
2768         if (seen_frame_header) {
2769           LIBGAV1_DLOG(ERROR,
2770                        "Frame header found but frame header was already seen.");
2771           return kStatusBitstreamError;
2772         }
2773         if (!ParseFrameHeader()) {
2774           LIBGAV1_DLOG(ERROR, "Failed to parse FrameHeader in Frame OBU.");
2775           return kStatusBitstreamError;
2776         }
2777         // Section 6.8.2: If obu_type is equal to OBU_FRAME, it is a
2778         // requirement of bitstream conformance that show_existing_frame is
2779         // equal to 0.
2780         if (frame_header_.show_existing_frame) {
2781           LIBGAV1_DLOG(ERROR, "Frame OBU cannot set show_existing_frame to 1.");
2782           return kStatusBitstreamError;
2783         }
2784         if (!bit_reader_->AlignToNextByte()) {
2785           LIBGAV1_DLOG(ERROR, "Byte alignment has non zero bits.");
2786           return kStatusBitstreamError;
2787         }
2788         const size_t fh_size = bit_reader_->byte_offset() - fh_start_offset;
2789         if (fh_size >= obu_size) {
2790           LIBGAV1_DLOG(ERROR, "Frame header size (%zu) >= obu_size (%zu).",
2791                        fh_size, obu_size);
2792           return kStatusBitstreamError;
2793         }
2794         if (!ParseTileGroup(obu_size - fh_size,
2795                             size_ - size + bit_reader_->byte_offset())) {
2796           LIBGAV1_DLOG(ERROR, "Failed to parse TileGroup in Frame OBU.");
2797           return kStatusBitstreamError;
2798         }
2799         parsed_one_full_frame = true;
2800         break;
2801       }
2802       case kObuTileGroup:
2803         if (!ParseTileGroup(obu_size,
2804                             size_ - size + bit_reader_->byte_offset())) {
2805           LIBGAV1_DLOG(ERROR, "Failed to parse TileGroup OBU.");
2806           return kStatusBitstreamError;
2807         }
2808         parsed_one_full_frame =
2809             (next_tile_group_start_ == frame_header_.tile_info.tile_count);
2810         break;
2811       case kObuTileList:
2812         LIBGAV1_DLOG(ERROR, "Decoding of tile list OBUs is not supported.");
2813         return kStatusUnimplemented;
2814       case kObuPadding:
2815         if (!ParsePadding(&data[obu_start_position >> 3], obu_size)) {
2816           LIBGAV1_DLOG(ERROR, "Failed to parse Padding OBU.");
2817           return kStatusBitstreamError;
2818         }
2819         break;
2820       case kObuMetadata:
2821         if (!ParseMetadata(&data[obu_start_position >> 3], obu_size)) {
2822           LIBGAV1_DLOG(ERROR, "Failed to parse Metadata OBU.");
2823           return kStatusBitstreamError;
2824         }
2825         break;
2826       default:
2827         // Skip reserved OBUs. Section 6.2.2: Reserved units are for future use
2828         // and shall be ignored by AV1 decoder.
2829         bit_reader_->SkipBytes(obu_size);
2830         obu_skipped = true;
2831         break;
2832     }
2833     if (obu_size > 0 && !obu_skipped && obu_type != kObuFrame &&
2834         obu_type != kObuTileGroup) {
2835       const size_t parsed_obu_size_in_bits =
2836           bit_reader_->bit_offset() - obu_start_position;
2837       if (obu_size * 8 < parsed_obu_size_in_bits) {
2838         LIBGAV1_DLOG(
2839             ERROR,
2840             "Parsed OBU size (%zu bits) is greater than expected OBU size "
2841             "(%zu bytes) obu_type: %d.",
2842             parsed_obu_size_in_bits, obu_size, obu_type);
2843         return kStatusBitstreamError;
2844       }
2845       if (!bit_reader_->VerifyAndSkipTrailingBits(obu_size * 8 -
2846                                                   parsed_obu_size_in_bits)) {
2847         LIBGAV1_DLOG(ERROR,
2848                      "Error when verifying trailing bits for obu type: %d",
2849                      obu_type);
2850         return kStatusBitstreamError;
2851       }
2852     }
2853     const size_t bytes_consumed = bit_reader_->byte_offset();
2854     const size_t consumed_obu_size =
2855         bytes_consumed - obu_length_size - obu_header_size;
2856     if (consumed_obu_size != obu_size) {
2857       LIBGAV1_DLOG(ERROR,
2858                    "OBU size (%zu) and consumed size (%zu) does not match for "
2859                    "obu_type: %d.",
2860                    obu_size, consumed_obu_size, obu_type);
2861       return kStatusBitstreamError;
2862     }
2863     data += bytes_consumed;
2864     size -= bytes_consumed;
2865   }
2866   if (!parsed_one_full_frame && seen_frame_header) {
2867     LIBGAV1_DLOG(ERROR, "The last tile group in the frame was not received.");
2868     return kStatusBitstreamError;
2869   }
2870   data_ = data;
2871   size_ = size;
2872   *current_frame = std::move(current_frame_);
2873   return kStatusOk;
2874 }
2875 
2876 }  // namespace libgav1
2877