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