Shaka Packager SDK
av1_parser.h
1 // Copyright 2018 Google LLC. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #ifndef PACKAGER_MEDIA_CODECS_AV1_PARSER_H_
8 #define PACKAGER_MEDIA_CODECS_AV1_PARSER_H_
9 
10 #include <cstdint>
11 #include <cstdlib>
12 #include <vector>
13 
14 namespace shaka {
15 namespace media {
16 
17 class BitReader;
18 
21 class AV1Parser {
22  public:
23  struct Tile {
24  size_t start_offset_in_bytes;
25  size_t size_in_bytes;
26  };
27 
28  AV1Parser();
29  virtual ~AV1Parser();
30 
39  virtual bool Parse(const uint8_t* data,
40  size_t data_size,
41  std::vector<Tile>* tiles);
42 
43  private:
44  AV1Parser(const AV1Parser&) = delete;
45  AV1Parser& operator=(const AV1Parser&) = delete;
46 
47  // The structure names and the method names match the names in the spec but in
48  // CamelCase.
49  // Not all fields are populated. In particular, fields not referenced and not
50  // needed to parse other parts of the bitstream are not populated.
51 
52  struct ObuExtensionHeader {
53  int temporal_id = 0;
54  int spatial_id = 0;
55  };
56 
57  struct ObuHeader {
58  int obu_type = 0;
59  bool obu_has_size_field = false;
60  ObuExtensionHeader extension_header;
61  };
62 
63  struct ColorConfig {
64  int bit_depth = 0;
65  bool mono_chrome = false;
66  int num_planes = 0;
67  int color_primaries = 0;
68  int transfer_chracteristics = 0;
69  int matrix_coefficients = 0;
70  bool color_range = false;
71  bool subsampling_x = false;
72  bool subsampling_y = false;
73  int chroma_sampling_position = 0;
74  bool separate_uv_delta_q = false;
75  };
76 
77  struct TimingInfo {
78  bool equal_picture_interval = false;
79  };
80 
81  struct DecoderModelInfo {
82  int buffer_delay_length_minus_1 = 0;
83  int buffer_removal_time_length_minus_1 = 0;
84  int frame_presentation_time_length_minus_1 = 0;
85  };
86 
87  struct SequenceHeaderObu {
88  int seq_profile = 0;
89  bool reduced_still_picture_header = false;
90 
91  TimingInfo timing_info;
92  bool decoder_model_info_present_flag = false;
93  DecoderModelInfo decoder_model_info;
94 
95  int operating_points_cnt_minus_1 = 0;
96  static constexpr int kMaxOperatingPointsCount = 1 << 5;
97  int operating_point_idc[kMaxOperatingPointsCount] = {};
98  bool decoder_model_present_for_this_op[kMaxOperatingPointsCount] = {};
99 
100  int frame_width_bits_minus_1 = 0;
101  int frame_height_bits_minus_1 = 0;
102  int max_frame_width_minus_1 = 0;
103  int max_frame_height_minus_1 = 0;
104 
105  bool frame_id_numbers_present_flag = false;
106  int delta_frame_id_length_minus_2 = 0;
107  int additional_frame_id_length_minus_1 = 0;
108 
109  bool use_128x128_superblock = false;
110 
111  bool enable_warped_motion = false;
112  bool enable_order_hint = false;
113  bool enable_ref_frame_mvs = false;
114  int order_hint_bits = 0;
115 
116  int seq_force_screen_content_tools = 0;
117  int seq_force_integer_mv = 0;
118 
119  bool enable_superres = false;
120  bool enable_cdef = false;
121  bool enable_restoration = false;
122  ColorConfig color_config;
123  bool film_grain_params_present = false;
124  };
125 
126  struct TileInfo {
127  int tile_cols = 0;
128  int tile_rows = 0;
129  int tile_cols_log2 = 0;
130  int tile_rows_log2 = 0;
131  int tile_size_bytes = 0;
132  };
133 
134  struct QuantizationParams {
135  int base_q_idx = 0;
136  int delta_qydc = 0;
137  int delta_quac = 0;
138  int delta_qudc = 0;
139  int delta_qvac = 0;
140  int delta_qvdc = 0;
141  };
142 
143  static constexpr int kMaxSegments = 8;
144  static constexpr int kSegLvlMax = 8;
145  struct SegmentationParams {
146  bool segmentation_enabled = false;
147  bool feature_enabled[kMaxSegments][kSegLvlMax] = {};
148  int feature_data[kMaxSegments][kSegLvlMax] = {};
149  };
150 
151  static constexpr int kRefsPerFrame = 7;
152  struct FrameHeaderObu {
153  bool seen_frame_header = false;
154 
155  bool show_existing_frame = false;
156  int frame_to_show_map_idx = 0;
157 
158  int frame_type = 0;
159  int refresh_frame_flags = 0;
160 
161  int ref_frame_idx[kRefsPerFrame] = {};
162 
163  int order_hint = 0;
164 
165  int frame_width = 0;
166  int frame_height = 0;
167  int upscaled_width = 0;
168  int render_width = 0;
169  int render_height = 0;
170 
171  int mi_cols = 0;
172  int mi_rows = 0;
173 
174  TileInfo tile_info;
175  QuantizationParams quantization_params;
176  SegmentationParams segmentation_params;
177  };
178 
179  struct ReferenceFrame {
180  int frame_type = 0;
181  int order_hint = 0;
182 
183  int frame_width = 0;
184  int frame_height = 0;
185  int upscaled_width = 0;
186  int render_width = 0;
187  int render_height = 0;
188 
189  int mi_cols = 0;
190  int mi_rows = 0;
191 
192  int bit_depth = 0;
193  bool subsampling_x = false;
194  bool subsampling_y = false;
195  };
196 
197  bool ParseOpenBitstreamUnit(BitReader* reader, std::vector<Tile>* tiles);
198  bool ParseObuHeader(BitReader* reader, ObuHeader* obu_header);
199  bool ParseObuExtensionHeader(BitReader* reader,
200  ObuExtensionHeader* obu_extension_header);
201  bool ParseTrailingBits(size_t nb_bits, BitReader* reader);
202  bool ByteAlignment(BitReader* reader);
203 
204  // SequenceHeader OBU and children structures.
205  bool ParseSequenceHeaderObu(BitReader* reader);
206  bool ParseColorConfig(BitReader* reader);
207  bool ParseTimingInfo(BitReader* reader);
208  bool ParseDecoderModelInfo(BitReader* reader);
209  bool SkipOperatingParametersInfo(BitReader* reader);
210 
211  // FrameHeader OBU and children structures.
212  bool ParseFrameHeaderObu(const ObuHeader& obu_header, BitReader* reader);
213  bool ParseUncompressedHeader(const ObuHeader& obu_header, BitReader* reader);
214  int GetRelativeDist(int a, int b);
215  bool ParseFrameSize(bool frame_size_override_flag, BitReader* reader);
216  bool ParseRenderSize(BitReader* reader);
217  bool ParseFrameSizeWithRefs(bool frame_size_override_flag, BitReader* reader);
218  bool ParseSuperresParams(BitReader* reader);
219  void ComputeImageSize();
220  bool SkipInterpolationFilter(BitReader* reader);
221  bool ParseLoopFilterParams(bool coded_lossless,
222  bool allow_intrabc,
223  BitReader* reader);
224  bool ParseTileInfo(BitReader* reader);
225  bool ParseQuantizationParams(BitReader* reader);
226  bool ReadDeltaQ(BitReader* reader, int* delta_q);
227  bool ParseSegmentationParams(int primary_ref_frame, BitReader* reader);
228  bool SkipDeltaQParams(BitReader* reader, bool* delta_q_present);
229  bool SkipDeltaLfParams(bool delta_q_present,
230  bool allow_intrabc,
231  BitReader* reader);
232  bool ParseCdefParams(bool coded_lossless,
233  bool allow_intrabc,
234  BitReader* reader);
235  bool ParseLrParams(bool all_lossless, bool allow_intrabc, BitReader* reader);
236  bool SkipTxMode(bool coded_lossless, BitReader* reader);
237  bool SkipSkipModeParams(bool frame_is_intra,
238  bool reference_select,
239  BitReader* reader);
240  bool ParseFrameReferenceMode(bool frame_is_intra,
241  BitReader* reader,
242  bool* reference_select);
243  bool SkipGlobalMotionParams(bool frame_is_intra,
244  bool allow_high_precision_mv,
245  BitReader* reader);
246  bool SkipGlobalParam(int type,
247  int ref,
248  int idx,
249  bool allow_high_precision_mv,
250  BitReader* reader);
251  bool SkipDecodeSignedSubexpWithRef(int low, int high, BitReader* reader);
252  bool SkipDecodeUnsignedSubexpWithRef(int mx, BitReader* reader);
253  bool SkipDecodeSubexp(int num_syms, BitReader* reader);
254  bool SkipFilmGrainParams(bool show_frame,
255  bool showable_frame,
256  BitReader* reader);
257  bool SkipTemporalPointInfo(BitReader* reader);
258 
259  // Frame OBU.
260  bool ParseFrameObu(const ObuHeader& obu_header,
261  size_t size,
262  BitReader* reader,
263  std::vector<Tile>* tiles);
264 
265  // TileGroup OBU.
266  bool ParseTileGroupObu(size_t size,
267  BitReader* reader,
268  std::vector<Tile>* tiles);
269  bool SegFeatureActiveIdx(int idx, int feature);
270 
271  // Decoding process related helper functions.
272  // We do not care about decoding itself, but we need to take care of reference
273  // frame states.
274  void DecodeFrameWrapup();
275  bool SetFrameRefs(int last_frame_idx, int gold_frame_idx);
276  int GetQIndex(bool ignore_delta_q, int segment_id);
277 
278  SequenceHeaderObu sequence_header_;
279  FrameHeaderObu frame_header_;
280  static constexpr int kNumRefFrames = 8;
281  ReferenceFrame reference_frames_[kNumRefFrames];
282 };
283 
284 } // namespace media
285 } // namespace shaka
286 
287 #endif // PACKAGER_MEDIA_CODECS_AV1_PARSER_H_
virtual bool Parse(const uint8_t *data, size_t data_size, std::vector< Tile > *tiles)
Definition: av1_parser.cc:258
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66