xref: /aosp_15_r20/external/libgav1/src/obu_parser.h (revision 095378508e87ed692bf8dfeb34008b65b3735891)
1*09537850SAkhilesh Sanikop /*
2*09537850SAkhilesh Sanikop  * Copyright 2019 The libgav1 Authors
3*09537850SAkhilesh Sanikop  *
4*09537850SAkhilesh Sanikop  * Licensed under the Apache License, Version 2.0 (the "License");
5*09537850SAkhilesh Sanikop  * you may not use this file except in compliance with the License.
6*09537850SAkhilesh Sanikop  * You may obtain a copy of the License at
7*09537850SAkhilesh Sanikop  *
8*09537850SAkhilesh Sanikop  *      http://www.apache.org/licenses/LICENSE-2.0
9*09537850SAkhilesh Sanikop  *
10*09537850SAkhilesh Sanikop  * Unless required by applicable law or agreed to in writing, software
11*09537850SAkhilesh Sanikop  * distributed under the License is distributed on an "AS IS" BASIS,
12*09537850SAkhilesh Sanikop  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*09537850SAkhilesh Sanikop  * See the License for the specific language governing permissions and
14*09537850SAkhilesh Sanikop  * limitations under the License.
15*09537850SAkhilesh Sanikop  */
16*09537850SAkhilesh Sanikop 
17*09537850SAkhilesh Sanikop #ifndef LIBGAV1_SRC_OBU_PARSER_H_
18*09537850SAkhilesh Sanikop #define LIBGAV1_SRC_OBU_PARSER_H_
19*09537850SAkhilesh Sanikop 
20*09537850SAkhilesh Sanikop #include <array>
21*09537850SAkhilesh Sanikop #include <cstddef>
22*09537850SAkhilesh Sanikop #include <cstdint>
23*09537850SAkhilesh Sanikop #include <memory>
24*09537850SAkhilesh Sanikop #include <type_traits>
25*09537850SAkhilesh Sanikop #include <utility>
26*09537850SAkhilesh Sanikop 
27*09537850SAkhilesh Sanikop #include "src/buffer_pool.h"
28*09537850SAkhilesh Sanikop #include "src/decoder_state.h"
29*09537850SAkhilesh Sanikop #include "src/dsp/common.h"
30*09537850SAkhilesh Sanikop #include "src/gav1/decoder_buffer.h"
31*09537850SAkhilesh Sanikop #include "src/gav1/status_code.h"
32*09537850SAkhilesh Sanikop #include "src/quantizer.h"
33*09537850SAkhilesh Sanikop #include "src/utils/common.h"
34*09537850SAkhilesh Sanikop #include "src/utils/compiler_attributes.h"
35*09537850SAkhilesh Sanikop #include "src/utils/constants.h"
36*09537850SAkhilesh Sanikop #include "src/utils/raw_bit_reader.h"
37*09537850SAkhilesh Sanikop #include "src/utils/segmentation.h"
38*09537850SAkhilesh Sanikop #include "src/utils/vector.h"
39*09537850SAkhilesh Sanikop 
40*09537850SAkhilesh Sanikop namespace libgav1 {
41*09537850SAkhilesh Sanikop 
42*09537850SAkhilesh Sanikop // structs and enums related to Open Bitstream Units (OBU).
43*09537850SAkhilesh Sanikop 
44*09537850SAkhilesh Sanikop enum {
45*09537850SAkhilesh Sanikop   kMinimumMajorBitstreamLevel = 2,
46*09537850SAkhilesh Sanikop   kSelectScreenContentTools = 2,
47*09537850SAkhilesh Sanikop   kSelectIntegerMv = 2,
48*09537850SAkhilesh Sanikop   kLoopRestorationTileSizeMax = 256,
49*09537850SAkhilesh Sanikop   kGlobalMotionAlphaBits = 12,
50*09537850SAkhilesh Sanikop   kGlobalMotionTranslationBits = 12,
51*09537850SAkhilesh Sanikop   kGlobalMotionTranslationOnlyBits = 9,
52*09537850SAkhilesh Sanikop   kGlobalMotionAlphaPrecisionBits = 15,
53*09537850SAkhilesh Sanikop   kGlobalMotionTranslationPrecisionBits = 6,
54*09537850SAkhilesh Sanikop   kGlobalMotionTranslationOnlyPrecisionBits = 3,
55*09537850SAkhilesh Sanikop   kMaxTileWidth = 4096,
56*09537850SAkhilesh Sanikop   kMaxTileArea = 4096 * 2304,
57*09537850SAkhilesh Sanikop   kPrimaryReferenceNone = 7,
58*09537850SAkhilesh Sanikop   // A special value of the scalability_mode_idc syntax element that indicates
59*09537850SAkhilesh Sanikop   // the picture prediction structure is specified in scalability_structure().
60*09537850SAkhilesh Sanikop   kScalabilitySS = 14
61*09537850SAkhilesh Sanikop };  // anonymous enum
62*09537850SAkhilesh Sanikop 
63*09537850SAkhilesh Sanikop struct ObuHeader {
64*09537850SAkhilesh Sanikop   ObuType type;
65*09537850SAkhilesh Sanikop   bool has_extension;
66*09537850SAkhilesh Sanikop   bool has_size_field;
67*09537850SAkhilesh Sanikop   int8_t temporal_id;
68*09537850SAkhilesh Sanikop   int8_t spatial_id;
69*09537850SAkhilesh Sanikop };
70*09537850SAkhilesh Sanikop 
71*09537850SAkhilesh Sanikop enum BitstreamProfile : uint8_t {
72*09537850SAkhilesh Sanikop   kProfile0,
73*09537850SAkhilesh Sanikop   kProfile1,
74*09537850SAkhilesh Sanikop   kProfile2,
75*09537850SAkhilesh Sanikop   kMaxProfiles
76*09537850SAkhilesh Sanikop };
77*09537850SAkhilesh Sanikop 
78*09537850SAkhilesh Sanikop // In the bitstream the level is encoded in five bits: the first three bits
79*09537850SAkhilesh Sanikop // encode |major| - 2 and the last two bits encode |minor|.
80*09537850SAkhilesh Sanikop //
81*09537850SAkhilesh Sanikop // If the mapped level (major.minor) is in the tables in Annex A.3, there are
82*09537850SAkhilesh Sanikop // bitstream conformance requirements on the maximum or minimum values of
83*09537850SAkhilesh Sanikop // several variables. The encoded value of 31 (which corresponds to the mapped
84*09537850SAkhilesh Sanikop // level 9.3) is the "maximum parameters" level and imposes no level-based
85*09537850SAkhilesh Sanikop // constraints on the bitstream.
86*09537850SAkhilesh Sanikop struct BitStreamLevel {
87*09537850SAkhilesh Sanikop   uint8_t major;  // Range: 2-9.
88*09537850SAkhilesh Sanikop   uint8_t minor;  // Range: 0-3.
89*09537850SAkhilesh Sanikop };
90*09537850SAkhilesh Sanikop 
91*09537850SAkhilesh Sanikop struct ColorConfig {
92*09537850SAkhilesh Sanikop   int8_t bitdepth;
93*09537850SAkhilesh Sanikop   bool is_monochrome;
94*09537850SAkhilesh Sanikop   ColorPrimary color_primary;
95*09537850SAkhilesh Sanikop   TransferCharacteristics transfer_characteristics;
96*09537850SAkhilesh Sanikop   MatrixCoefficients matrix_coefficients;
97*09537850SAkhilesh Sanikop   // A binary value (0 or 1) that is associated with the VideoFullRangeFlag
98*09537850SAkhilesh Sanikop   // variable specified in ISO/IEC 23091-4/ITUT H.273.
99*09537850SAkhilesh Sanikop   // * 0: the studio swing representation.
100*09537850SAkhilesh Sanikop   // * 1: the full swing representation.
101*09537850SAkhilesh Sanikop   ColorRange color_range;
102*09537850SAkhilesh Sanikop   int8_t subsampling_x;
103*09537850SAkhilesh Sanikop   int8_t subsampling_y;
104*09537850SAkhilesh Sanikop   ChromaSamplePosition chroma_sample_position;
105*09537850SAkhilesh Sanikop   bool separate_uv_delta_q;
106*09537850SAkhilesh Sanikop };
107*09537850SAkhilesh Sanikop 
108*09537850SAkhilesh Sanikop struct TimingInfo {
109*09537850SAkhilesh Sanikop   uint32_t num_units_in_tick;
110*09537850SAkhilesh Sanikop   uint32_t time_scale;
111*09537850SAkhilesh Sanikop   bool equal_picture_interval;
112*09537850SAkhilesh Sanikop   uint32_t num_ticks_per_picture;
113*09537850SAkhilesh Sanikop };
114*09537850SAkhilesh Sanikop 
115*09537850SAkhilesh Sanikop struct DecoderModelInfo {
116*09537850SAkhilesh Sanikop   uint8_t encoder_decoder_buffer_delay_length;
117*09537850SAkhilesh Sanikop   uint32_t num_units_in_decoding_tick;
118*09537850SAkhilesh Sanikop   uint8_t buffer_removal_time_length;
119*09537850SAkhilesh Sanikop   uint8_t frame_presentation_time_length;
120*09537850SAkhilesh Sanikop };
121*09537850SAkhilesh Sanikop 
122*09537850SAkhilesh Sanikop struct OperatingParameters {
123*09537850SAkhilesh Sanikop   uint32_t decoder_buffer_delay[kMaxOperatingPoints];
124*09537850SAkhilesh Sanikop   uint32_t encoder_buffer_delay[kMaxOperatingPoints];
125*09537850SAkhilesh Sanikop   bool low_delay_mode_flag[kMaxOperatingPoints];
126*09537850SAkhilesh Sanikop };
127*09537850SAkhilesh Sanikop 
128*09537850SAkhilesh Sanikop struct ObuSequenceHeader {
129*09537850SAkhilesh Sanikop   // Section 7.5:
130*09537850SAkhilesh Sanikop   //   Within a particular coded video sequence, the contents of
131*09537850SAkhilesh Sanikop   //   sequence_header_obu must be bit-identical each time the sequence header
132*09537850SAkhilesh Sanikop   //   appears except for the contents of operating_parameters_info. A new
133*09537850SAkhilesh Sanikop   //   coded video sequence is required if the sequence header parameters
134*09537850SAkhilesh Sanikop   //   change.
135*09537850SAkhilesh Sanikop   //
136*09537850SAkhilesh Sanikop   // IMPORTANT: ParametersChanged() is implemented with a memcmp() call. For
137*09537850SAkhilesh Sanikop   // this to work, this object and the |old| object must be initialized with
138*09537850SAkhilesh Sanikop   // an empty brace-enclosed list, which initializes any padding to zero bits.
139*09537850SAkhilesh Sanikop   // See https://en.cppreference.com/w/cpp/language/zero_initialization.
140*09537850SAkhilesh Sanikop   bool ParametersChanged(const ObuSequenceHeader& old) const;
141*09537850SAkhilesh Sanikop 
142*09537850SAkhilesh Sanikop   BitstreamProfile profile;
143*09537850SAkhilesh Sanikop   bool still_picture;
144*09537850SAkhilesh Sanikop   bool reduced_still_picture_header;
145*09537850SAkhilesh Sanikop   int operating_points;
146*09537850SAkhilesh Sanikop   int operating_point_idc[kMaxOperatingPoints];
147*09537850SAkhilesh Sanikop   BitStreamLevel level[kMaxOperatingPoints];
148*09537850SAkhilesh Sanikop   int8_t tier[kMaxOperatingPoints];
149*09537850SAkhilesh Sanikop   int8_t frame_width_bits;
150*09537850SAkhilesh Sanikop   int8_t frame_height_bits;
151*09537850SAkhilesh Sanikop   int32_t max_frame_width;
152*09537850SAkhilesh Sanikop   int32_t max_frame_height;
153*09537850SAkhilesh Sanikop   bool frame_id_numbers_present;
154*09537850SAkhilesh Sanikop   int8_t frame_id_length_bits;
155*09537850SAkhilesh Sanikop   int8_t delta_frame_id_length_bits;
156*09537850SAkhilesh Sanikop   bool use_128x128_superblock;
157*09537850SAkhilesh Sanikop   bool enable_filter_intra;
158*09537850SAkhilesh Sanikop   bool enable_intra_edge_filter;
159*09537850SAkhilesh Sanikop   bool enable_interintra_compound;
160*09537850SAkhilesh Sanikop   bool enable_masked_compound;
161*09537850SAkhilesh Sanikop   bool enable_warped_motion;
162*09537850SAkhilesh Sanikop   bool enable_dual_filter;
163*09537850SAkhilesh Sanikop   bool enable_order_hint;
164*09537850SAkhilesh Sanikop   // If enable_order_hint is true, order_hint_bits is in the range [1, 8].
165*09537850SAkhilesh Sanikop   // If enable_order_hint is false, order_hint_bits is 0.
166*09537850SAkhilesh Sanikop   int8_t order_hint_bits;
167*09537850SAkhilesh Sanikop   // order_hint_shift_bits equals (32 - order_hint_bits) % 32.
168*09537850SAkhilesh Sanikop   // This is used frequently in GetRelativeDistance().
169*09537850SAkhilesh Sanikop   uint8_t order_hint_shift_bits;
170*09537850SAkhilesh Sanikop   bool enable_jnt_comp;
171*09537850SAkhilesh Sanikop   bool enable_ref_frame_mvs;
172*09537850SAkhilesh Sanikop   bool choose_screen_content_tools;
173*09537850SAkhilesh Sanikop   int8_t force_screen_content_tools;
174*09537850SAkhilesh Sanikop   bool choose_integer_mv;
175*09537850SAkhilesh Sanikop   int8_t force_integer_mv;
176*09537850SAkhilesh Sanikop   bool enable_superres;
177*09537850SAkhilesh Sanikop   bool enable_cdef;
178*09537850SAkhilesh Sanikop   bool enable_restoration;
179*09537850SAkhilesh Sanikop   ColorConfig color_config;
180*09537850SAkhilesh Sanikop   bool timing_info_present_flag;
181*09537850SAkhilesh Sanikop   TimingInfo timing_info;
182*09537850SAkhilesh Sanikop   bool decoder_model_info_present_flag;
183*09537850SAkhilesh Sanikop   DecoderModelInfo decoder_model_info;
184*09537850SAkhilesh Sanikop   bool decoder_model_present_for_operating_point[kMaxOperatingPoints];
185*09537850SAkhilesh Sanikop   bool initial_display_delay_present_flag;
186*09537850SAkhilesh Sanikop   uint8_t initial_display_delay[kMaxOperatingPoints];
187*09537850SAkhilesh Sanikop   bool film_grain_params_present;
188*09537850SAkhilesh Sanikop 
189*09537850SAkhilesh Sanikop   // IMPORTANT: the operating_parameters member must be at the end of the
190*09537850SAkhilesh Sanikop   // struct so that ParametersChanged() can be implemented with a memcmp()
191*09537850SAkhilesh Sanikop   // call.
192*09537850SAkhilesh Sanikop   OperatingParameters operating_parameters;
193*09537850SAkhilesh Sanikop };
194*09537850SAkhilesh Sanikop // Verify it is safe to use offsetof with ObuSequenceHeader and to use memcmp
195*09537850SAkhilesh Sanikop // to compare two ObuSequenceHeader objects.
196*09537850SAkhilesh Sanikop static_assert(std::is_standard_layout<ObuSequenceHeader>::value, "");
197*09537850SAkhilesh Sanikop // Verify operating_parameters is the last member of ObuSequenceHeader. The
198*09537850SAkhilesh Sanikop // second assertion assumes that ObuSequenceHeader has no padding after the
199*09537850SAkhilesh Sanikop // operating_parameters field. The first assertion is a sufficient condition
200*09537850SAkhilesh Sanikop // for ObuSequenceHeader to have no padding after the operating_parameters
201*09537850SAkhilesh Sanikop // field.
202*09537850SAkhilesh Sanikop static_assert(alignof(ObuSequenceHeader) == alignof(OperatingParameters), "");
203*09537850SAkhilesh Sanikop static_assert(sizeof(ObuSequenceHeader) ==
204*09537850SAkhilesh Sanikop                   offsetof(ObuSequenceHeader, operating_parameters) +
205*09537850SAkhilesh Sanikop                       sizeof(OperatingParameters),
206*09537850SAkhilesh Sanikop               "");
207*09537850SAkhilesh Sanikop 
208*09537850SAkhilesh Sanikop struct TileBuffer {
209*09537850SAkhilesh Sanikop   const uint8_t* data;
210*09537850SAkhilesh Sanikop   size_t size;
211*09537850SAkhilesh Sanikop };
212*09537850SAkhilesh Sanikop 
213*09537850SAkhilesh Sanikop enum MetadataType : uint8_t {
214*09537850SAkhilesh Sanikop   // 0 is reserved for AOM use.
215*09537850SAkhilesh Sanikop   kMetadataTypeHdrContentLightLevel = 1,
216*09537850SAkhilesh Sanikop   kMetadataTypeHdrMasteringDisplayColorVolume = 2,
217*09537850SAkhilesh Sanikop   kMetadataTypeScalability = 3,
218*09537850SAkhilesh Sanikop   kMetadataTypeItutT35 = 4,
219*09537850SAkhilesh Sanikop   kMetadataTypeTimecode = 5,
220*09537850SAkhilesh Sanikop   // 6-31 are unregistered user private.
221*09537850SAkhilesh Sanikop   // 32 and greater are reserved for AOM use.
222*09537850SAkhilesh Sanikop };
223*09537850SAkhilesh Sanikop 
224*09537850SAkhilesh Sanikop class ObuParser : public Allocable {
225*09537850SAkhilesh Sanikop  public:
ObuParser(const uint8_t * const data,size_t size,int operating_point,BufferPool * const buffer_pool,DecoderState * const decoder_state)226*09537850SAkhilesh Sanikop   ObuParser(const uint8_t* const data, size_t size, int operating_point,
227*09537850SAkhilesh Sanikop             BufferPool* const buffer_pool, DecoderState* const decoder_state)
228*09537850SAkhilesh Sanikop       : data_(data),
229*09537850SAkhilesh Sanikop         size_(size),
230*09537850SAkhilesh Sanikop         operating_point_(operating_point),
231*09537850SAkhilesh Sanikop         buffer_pool_(buffer_pool),
232*09537850SAkhilesh Sanikop         decoder_state_(*decoder_state) {}
233*09537850SAkhilesh Sanikop 
234*09537850SAkhilesh Sanikop   // Not copyable or movable.
235*09537850SAkhilesh Sanikop   ObuParser(const ObuParser& rhs) = delete;
236*09537850SAkhilesh Sanikop   ObuParser& operator=(const ObuParser& rhs) = delete;
237*09537850SAkhilesh Sanikop 
238*09537850SAkhilesh Sanikop   // Returns true if there is more data that needs to be parsed.
239*09537850SAkhilesh Sanikop   bool HasData() const;
240*09537850SAkhilesh Sanikop 
241*09537850SAkhilesh Sanikop   // Parses a sequence of Open Bitstream Units until a decodable frame is found
242*09537850SAkhilesh Sanikop   // (or until the end of stream is reached). A decodable frame is considered to
243*09537850SAkhilesh Sanikop   // be found when one of the following happens:
244*09537850SAkhilesh Sanikop   //   * A kObuFrame is seen.
245*09537850SAkhilesh Sanikop   //   * The kObuTileGroup containing the last tile is seen.
246*09537850SAkhilesh Sanikop   //   * A kFrameHeader with show_existing_frame = true is seen.
247*09537850SAkhilesh Sanikop   //
248*09537850SAkhilesh Sanikop   // If the parsing is successful, relevant fields will be populated. The fields
249*09537850SAkhilesh Sanikop   // are valid only if the return value is kStatusOk. Returns kStatusOk on
250*09537850SAkhilesh Sanikop   // success, an error status otherwise. On success, |current_frame| will be
251*09537850SAkhilesh Sanikop   // populated with a valid frame buffer.
252*09537850SAkhilesh Sanikop   StatusCode ParseOneFrame(RefCountedBufferPtr* current_frame);
253*09537850SAkhilesh Sanikop 
254*09537850SAkhilesh Sanikop   // Get the AV1CodecConfigurationBox as described in
255*09537850SAkhilesh Sanikop   // https://aomediacodec.github.io/av1-isobmff/#av1codecconfigurationbox. This
256*09537850SAkhilesh Sanikop   // does minimal bitstream parsing to obtain the necessary information to
257*09537850SAkhilesh Sanikop   // generate the av1c box. Returns a std::unique_ptr that contains the av1c
258*09537850SAkhilesh Sanikop   // data on success, nullptr otherwise. |av1c_size| must not be nullptr and
259*09537850SAkhilesh Sanikop   // will contain the size of the buffer pointed to by the std::unique_ptr.
260*09537850SAkhilesh Sanikop   static std::unique_ptr<uint8_t[]> GetAV1CodecConfigurationBox(
261*09537850SAkhilesh Sanikop       const uint8_t* data, size_t size, size_t* av1c_size);
262*09537850SAkhilesh Sanikop 
263*09537850SAkhilesh Sanikop   // Getters. Only valid if ParseOneFrame() completes successfully.
obu_headers()264*09537850SAkhilesh Sanikop   const Vector<ObuHeader>& obu_headers() const { return obu_headers_; }
sequence_header()265*09537850SAkhilesh Sanikop   const ObuSequenceHeader& sequence_header() const { return sequence_header_; }
frame_header()266*09537850SAkhilesh Sanikop   const ObuFrameHeader& frame_header() const { return frame_header_; }
tile_buffers()267*09537850SAkhilesh Sanikop   const Vector<TileBuffer>& tile_buffers() const { return tile_buffers_; }
268*09537850SAkhilesh Sanikop   // Returns true if the last call to ParseOneFrame() encountered a sequence
269*09537850SAkhilesh Sanikop   // header change.
sequence_header_changed()270*09537850SAkhilesh Sanikop   bool sequence_header_changed() const { return sequence_header_changed_; }
271*09537850SAkhilesh Sanikop 
272*09537850SAkhilesh Sanikop   // Setters.
set_sequence_header(const ObuSequenceHeader & sequence_header)273*09537850SAkhilesh Sanikop   void set_sequence_header(const ObuSequenceHeader& sequence_header) {
274*09537850SAkhilesh Sanikop     sequence_header_ = sequence_header;
275*09537850SAkhilesh Sanikop     has_sequence_header_ = true;
276*09537850SAkhilesh Sanikop   }
277*09537850SAkhilesh Sanikop 
278*09537850SAkhilesh Sanikop   // Moves |tile_buffers_| into |tile_buffers|.
MoveTileBuffers(Vector<TileBuffer> * tile_buffers)279*09537850SAkhilesh Sanikop   void MoveTileBuffers(Vector<TileBuffer>* tile_buffers) {
280*09537850SAkhilesh Sanikop     *tile_buffers = std::move(tile_buffers_);
281*09537850SAkhilesh Sanikop   }
282*09537850SAkhilesh Sanikop 
283*09537850SAkhilesh Sanikop  private:
284*09537850SAkhilesh Sanikop   // Initializes the bit reader. This is a function of its own to make unit
285*09537850SAkhilesh Sanikop   // testing of private functions simpler.
286*09537850SAkhilesh Sanikop   LIBGAV1_MUST_USE_RESULT bool InitBitReader(const uint8_t* data, size_t size);
287*09537850SAkhilesh Sanikop 
288*09537850SAkhilesh Sanikop   // Parse helper functions.
289*09537850SAkhilesh Sanikop   bool ParseHeader();  // 5.3.2 and 5.3.3.
290*09537850SAkhilesh Sanikop   bool ParseColorConfig(ObuSequenceHeader* sequence_header);       // 5.5.2.
291*09537850SAkhilesh Sanikop   bool ParseTimingInfo(ObuSequenceHeader* sequence_header);        // 5.5.3.
292*09537850SAkhilesh Sanikop   bool ParseDecoderModelInfo(ObuSequenceHeader* sequence_header);  // 5.5.4.
293*09537850SAkhilesh Sanikop   bool ParseOperatingParameters(ObuSequenceHeader* sequence_header,
294*09537850SAkhilesh Sanikop                                 int index);          // 5.5.5.
295*09537850SAkhilesh Sanikop   bool ParseSequenceHeader(bool seen_frame_header);  // 5.5.1.
296*09537850SAkhilesh Sanikop   bool ParseFrameParameters();                       // 5.9.2, 5.9.7 and 5.9.10.
297*09537850SAkhilesh Sanikop   void MarkInvalidReferenceFrames();                 // 5.9.4.
298*09537850SAkhilesh Sanikop   bool ParseFrameSizeAndRenderSize();                // 5.9.5 and 5.9.6.
299*09537850SAkhilesh Sanikop   bool ParseSuperResParametersAndComputeImageSize();  // 5.9.8 and 5.9.9.
300*09537850SAkhilesh Sanikop   // Checks the bitstream conformance requirement in Section 6.8.6.
301*09537850SAkhilesh Sanikop   bool ValidateInterFrameSize() const;
302*09537850SAkhilesh Sanikop   bool ParseReferenceOrderHint();
303*09537850SAkhilesh Sanikop   static int FindLatestBackwardReference(
304*09537850SAkhilesh Sanikop       const int current_frame_hint,
305*09537850SAkhilesh Sanikop       const std::array<int, kNumReferenceFrameTypes>& shifted_order_hints,
306*09537850SAkhilesh Sanikop       const std::array<bool, kNumReferenceFrameTypes>& used_frame);
307*09537850SAkhilesh Sanikop   static int FindEarliestBackwardReference(
308*09537850SAkhilesh Sanikop       const int current_frame_hint,
309*09537850SAkhilesh Sanikop       const std::array<int, kNumReferenceFrameTypes>& shifted_order_hints,
310*09537850SAkhilesh Sanikop       const std::array<bool, kNumReferenceFrameTypes>& used_frame);
311*09537850SAkhilesh Sanikop   static int FindLatestForwardReference(
312*09537850SAkhilesh Sanikop       const int current_frame_hint,
313*09537850SAkhilesh Sanikop       const std::array<int, kNumReferenceFrameTypes>& shifted_order_hints,
314*09537850SAkhilesh Sanikop       const std::array<bool, kNumReferenceFrameTypes>& used_frame);
315*09537850SAkhilesh Sanikop   static int FindReferenceWithSmallestOutputOrder(
316*09537850SAkhilesh Sanikop       const std::array<int, kNumReferenceFrameTypes>& shifted_order_hints);
317*09537850SAkhilesh Sanikop   bool SetFrameReferences(int8_t last_frame_idx,
318*09537850SAkhilesh Sanikop                           int8_t gold_frame_idx);  // 7.8.
319*09537850SAkhilesh Sanikop   bool ParseLoopFilterParameters();                // 5.9.11.
320*09537850SAkhilesh Sanikop   bool ParseDeltaQuantizer(int8_t* delta);         // 5.9.13.
321*09537850SAkhilesh Sanikop   bool ParseQuantizerParameters();                 // 5.9.12.
322*09537850SAkhilesh Sanikop   bool ParseSegmentationParameters();              // 5.9.14.
323*09537850SAkhilesh Sanikop   bool ParseQuantizerIndexDeltaParameters();       // 5.9.17.
324*09537850SAkhilesh Sanikop   bool ParseLoopFilterDeltaParameters();           // 5.9.18.
325*09537850SAkhilesh Sanikop   void ComputeSegmentLosslessAndQIndex();
326*09537850SAkhilesh Sanikop   bool ParseCdefParameters();             // 5.9.19.
327*09537850SAkhilesh Sanikop   bool ParseLoopRestorationParameters();  // 5.9.20.
328*09537850SAkhilesh Sanikop   bool ParseTxModeSyntax();               // 5.9.21.
329*09537850SAkhilesh Sanikop   bool ParseFrameReferenceModeSyntax();   // 5.9.23.
330*09537850SAkhilesh Sanikop   // Returns whether skip mode is allowed. When it returns true, it also sets
331*09537850SAkhilesh Sanikop   // the frame_header_.skip_mode_frame array.
332*09537850SAkhilesh Sanikop   bool IsSkipModeAllowed();
333*09537850SAkhilesh Sanikop   bool ParseSkipModeParameters();  // 5.9.22.
334*09537850SAkhilesh Sanikop   bool ReadAllowWarpedMotion();
335*09537850SAkhilesh Sanikop   bool ParseGlobalParamSyntax(
336*09537850SAkhilesh Sanikop       int ref, int index,
337*09537850SAkhilesh Sanikop       const std::array<GlobalMotion, kNumReferenceFrameTypes>&
338*09537850SAkhilesh Sanikop           prev_global_motions);        // 5.9.25.
339*09537850SAkhilesh Sanikop   bool ParseGlobalMotionParameters();  // 5.9.24.
340*09537850SAkhilesh Sanikop   bool ParseFilmGrainParameters();     // 5.9.30.
341*09537850SAkhilesh Sanikop   bool ParseTileInfoSyntax();          // 5.9.15.
342*09537850SAkhilesh Sanikop   bool ParseFrameHeader();             // 5.9.
343*09537850SAkhilesh Sanikop   // |data| and |size| specify the payload data of the padding OBU.
344*09537850SAkhilesh Sanikop   // NOTE: Although the payload data is available in the bit_reader_ member,
345*09537850SAkhilesh Sanikop   // it is also passed to ParsePadding() as function parameters so that
346*09537850SAkhilesh Sanikop   // ParsePadding() can find the trailing bit of the OBU and skip over the
347*09537850SAkhilesh Sanikop   // payload data as an opaque chunk of data.
348*09537850SAkhilesh Sanikop   bool ParsePadding(const uint8_t* data, size_t size);  // 5.7.
349*09537850SAkhilesh Sanikop   bool ParseMetadataScalability();                      // 5.8.5 and 5.8.6.
350*09537850SAkhilesh Sanikop   bool ParseMetadataTimecode();                         // 5.8.7.
351*09537850SAkhilesh Sanikop   // |data| and |size| specify the payload data of the metadata OBU.
352*09537850SAkhilesh Sanikop   // NOTE: Although the payload data is available in the bit_reader_ member,
353*09537850SAkhilesh Sanikop   // it is also passed to ParseMetadata() as function parameters so that
354*09537850SAkhilesh Sanikop   // ParseMetadata() can find the trailing bit of the OBU and either extract
355*09537850SAkhilesh Sanikop   // or skip over the payload data as an opaque chunk of data.
356*09537850SAkhilesh Sanikop   bool ParseMetadata(const uint8_t* data, size_t size);  // 5.8.
357*09537850SAkhilesh Sanikop   // Adds and populates the TileBuffer for each tile in the tile group and
358*09537850SAkhilesh Sanikop   // updates |next_tile_group_start_|
359*09537850SAkhilesh Sanikop   bool AddTileBuffers(int start, int end, size_t total_size,
360*09537850SAkhilesh Sanikop                       size_t tg_header_size, size_t bytes_consumed_so_far);
361*09537850SAkhilesh Sanikop   bool ParseTileGroup(size_t size, size_t bytes_consumed_so_far);  // 5.11.1.
362*09537850SAkhilesh Sanikop 
363*09537850SAkhilesh Sanikop   // Populates |current_frame_| from the |buffer_pool_| if |current_frame_| is
364*09537850SAkhilesh Sanikop   // nullptr. Does not do anything otherwise. Returns true on success, false
365*09537850SAkhilesh Sanikop   // otherwise.
366*09537850SAkhilesh Sanikop   bool EnsureCurrentFrameIsNotNull();
367*09537850SAkhilesh Sanikop 
368*09537850SAkhilesh Sanikop   // Parses the basic bitstream information from the given AV1 stream in |data|.
369*09537850SAkhilesh Sanikop   // This is used for generating the AV1CodecConfigurationBox.
370*09537850SAkhilesh Sanikop   static StatusCode ParseBasicStreamInfo(const uint8_t* data, size_t size,
371*09537850SAkhilesh Sanikop                                          ObuSequenceHeader* sequence_header,
372*09537850SAkhilesh Sanikop                                          size_t* sequence_header_offset,
373*09537850SAkhilesh Sanikop                                          size_t* sequence_header_size);
374*09537850SAkhilesh Sanikop 
375*09537850SAkhilesh Sanikop   // Parser elements.
376*09537850SAkhilesh Sanikop   std::unique_ptr<RawBitReader> bit_reader_;
377*09537850SAkhilesh Sanikop   const uint8_t* data_;
378*09537850SAkhilesh Sanikop   size_t size_;
379*09537850SAkhilesh Sanikop   const int operating_point_;
380*09537850SAkhilesh Sanikop 
381*09537850SAkhilesh Sanikop   // OBU elements. Only valid if ParseOneFrame() completes successfully.
382*09537850SAkhilesh Sanikop   Vector<ObuHeader> obu_headers_;
383*09537850SAkhilesh Sanikop   ObuSequenceHeader sequence_header_ = {};
384*09537850SAkhilesh Sanikop   ObuFrameHeader frame_header_ = {};
385*09537850SAkhilesh Sanikop   Vector<TileBuffer> tile_buffers_;
386*09537850SAkhilesh Sanikop   // The expected starting tile number of the next Tile Group.
387*09537850SAkhilesh Sanikop   int next_tile_group_start_ = 0;
388*09537850SAkhilesh Sanikop   // If true, the sequence_header_ field is valid.
389*09537850SAkhilesh Sanikop   bool has_sequence_header_ = false;
390*09537850SAkhilesh Sanikop   // If true, it means that the last call to ParseOneFrame() encountered a
391*09537850SAkhilesh Sanikop   // sequence header change.
392*09537850SAkhilesh Sanikop   bool sequence_header_changed_ = false;
393*09537850SAkhilesh Sanikop   // If true, the obu_extension_flag syntax element in the OBU header must be
394*09537850SAkhilesh Sanikop   // 0. Set to true when parsing a sequence header if OperatingPointIdc is 0.
395*09537850SAkhilesh Sanikop   bool extension_disallowed_ = false;
396*09537850SAkhilesh Sanikop 
397*09537850SAkhilesh Sanikop   BufferPool* const buffer_pool_;
398*09537850SAkhilesh Sanikop   DecoderState& decoder_state_;
399*09537850SAkhilesh Sanikop   // Used by ParseOneFrame() to populate the current frame that is being
400*09537850SAkhilesh Sanikop   // decoded. The invariant maintained is that this variable will be nullptr at
401*09537850SAkhilesh Sanikop   // the beginning and at the end of each call to ParseOneFrame(). This ensures
402*09537850SAkhilesh Sanikop   // that the ObuParser is not holding on to any references to the current
403*09537850SAkhilesh Sanikop   // frame once the ParseOneFrame() call is complete.
404*09537850SAkhilesh Sanikop   RefCountedBufferPtr current_frame_;
405*09537850SAkhilesh Sanikop 
406*09537850SAkhilesh Sanikop   // For unit testing private functions.
407*09537850SAkhilesh Sanikop   friend class ObuParserTest;
408*09537850SAkhilesh Sanikop };
409*09537850SAkhilesh Sanikop 
410*09537850SAkhilesh Sanikop }  // namespace libgav1
411*09537850SAkhilesh Sanikop 
412*09537850SAkhilesh Sanikop #endif  // LIBGAV1_SRC_OBU_PARSER_H_
413