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