]> git.sesse.net Git - ffmpeg/blob - libavcodec/cbs_av1.h
cbs: Ensure that reference fields always follow the associated pointer
[ffmpeg] / libavcodec / cbs_av1.h
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #ifndef AVCODEC_CBS_AV1_H
20 #define AVCODEC_CBS_AV1_H
21
22 #include <stddef.h>
23 #include <stdint.h>
24
25 #include "av1.h"
26 #include "cbs.h"
27
28
29 typedef struct AV1RawOBUHeader {
30     uint8_t obu_forbidden_bit;
31     uint8_t obu_type;
32     uint8_t obu_extension_flag;
33     uint8_t obu_has_size_field;
34     uint8_t obu_reserved_1bit;
35
36     uint8_t temporal_id;
37     uint8_t spatial_id;
38     uint8_t extension_header_reserved_3bits;
39 } AV1RawOBUHeader;
40
41 typedef struct AV1RawColorConfig {
42     uint8_t high_bitdepth;
43     uint8_t twelve_bit;
44     uint8_t mono_chrome;
45
46     uint8_t color_description_present_flag;
47     uint8_t color_primaries;
48     uint8_t transfer_characteristics;
49     uint8_t matrix_coefficients;
50
51     uint8_t color_range;
52     uint8_t subsampling_x;
53     uint8_t subsampling_y;
54     uint8_t chroma_sample_position;
55     uint8_t separate_uv_delta_q;
56 } AV1RawColorConfig;
57
58 typedef struct AV1RawTimingInfo {
59     uint32_t num_units_in_display_tick;
60     uint32_t time_scale;
61
62     uint8_t equal_picture_interval;
63     uint32_t num_ticks_per_picture_minus_1;
64 } AV1RawTimingInfo;
65
66 typedef struct AV1RawDecoderModelInfo {
67     uint8_t  buffer_delay_length_minus_1;
68     uint32_t num_units_in_decoding_tick;
69     uint8_t  buffer_removal_time_length_minus_1;
70     uint8_t  frame_presentation_time_length_minus_1;
71 } AV1RawDecoderModelInfo;
72
73 typedef struct AV1RawSequenceHeader {
74     uint8_t seq_profile;
75     uint8_t still_picture;
76     uint8_t reduced_still_picture_header;
77
78     uint8_t timing_info_present_flag;
79     uint8_t decoder_model_info_present_flag;
80     uint8_t initial_display_delay_present_flag;
81     uint8_t operating_points_cnt_minus_1;
82
83     AV1RawTimingInfo       timing_info;
84     AV1RawDecoderModelInfo decoder_model_info;
85
86     uint16_t operating_point_idc[AV1_MAX_OPERATING_POINTS];
87     uint8_t  seq_level_idx[AV1_MAX_OPERATING_POINTS];
88     uint8_t  seq_tier[AV1_MAX_OPERATING_POINTS];
89     uint8_t  decoder_model_present_for_this_op[AV1_MAX_OPERATING_POINTS];
90     uint32_t decoder_buffer_delay[AV1_MAX_OPERATING_POINTS];
91     uint32_t encoder_buffer_delay[AV1_MAX_OPERATING_POINTS];
92     uint8_t  low_delay_mode_flag[AV1_MAX_OPERATING_POINTS];
93     uint8_t  initial_display_delay_present_for_this_op[AV1_MAX_OPERATING_POINTS];
94     uint8_t  initial_display_delay_minus_1[AV1_MAX_OPERATING_POINTS];
95
96     uint8_t  frame_width_bits_minus_1;
97     uint8_t  frame_height_bits_minus_1;
98     uint16_t max_frame_width_minus_1;
99     uint16_t max_frame_height_minus_1;
100
101     uint8_t frame_id_numbers_present_flag;
102     uint8_t delta_frame_id_length_minus_2;
103     uint8_t additional_frame_id_length_minus_1;
104
105     uint8_t use_128x128_superblock;
106     uint8_t enable_filter_intra;
107     uint8_t enable_intra_edge_filter;
108     uint8_t enable_interintra_compound;
109     uint8_t enable_masked_compound;
110     uint8_t enable_warped_motion;
111     uint8_t enable_dual_filter;
112
113     uint8_t enable_order_hint;
114     uint8_t enable_jnt_comp;
115     uint8_t enable_ref_frame_mvs;
116
117     uint8_t seq_choose_screen_content_tools;
118     uint8_t seq_force_screen_content_tools;
119     uint8_t seq_choose_integer_mv;
120     uint8_t seq_force_integer_mv;
121
122     uint8_t order_hint_bits_minus_1;
123
124     uint8_t enable_superres;
125     uint8_t enable_cdef;
126     uint8_t enable_restoration;
127
128     AV1RawColorConfig color_config;
129
130     uint8_t film_grain_params_present;
131 } AV1RawSequenceHeader;
132
133 typedef struct AV1RawFrameHeader {
134     uint8_t  show_existing_frame;
135     uint8_t  frame_to_show_map_idx;
136     uint32_t frame_presentation_time;
137     uint32_t display_frame_id;
138
139     uint8_t frame_type;
140     uint8_t show_frame;
141     uint8_t showable_frame;
142
143     uint8_t error_resilient_mode;
144     uint8_t disable_cdf_update;
145     uint8_t allow_screen_content_tools;
146     uint8_t force_integer_mv;
147
148     uint32_t current_frame_id;
149     uint8_t  frame_size_override_flag;
150     uint8_t  order_hint;
151
152     uint8_t  buffer_removal_time_present_flag;
153     uint32_t buffer_removal_time[AV1_MAX_OPERATING_POINTS];
154
155     uint8_t  primary_ref_frame;
156     uint16_t frame_width_minus_1;
157     uint16_t frame_height_minus_1;
158     uint8_t  use_superres;
159     uint8_t  coded_denom;
160     uint8_t  render_and_frame_size_different;
161     uint16_t render_width_minus_1;
162     uint16_t render_height_minus_1;
163
164     uint8_t found_ref[AV1_REFS_PER_FRAME];
165
166     uint8_t refresh_frame_flags;
167     uint8_t allow_intrabc;
168     uint8_t ref_order_hint[AV1_NUM_REF_FRAMES];
169     uint8_t frame_refs_short_signaling;
170     uint8_t last_frame_idx;
171     uint8_t golden_frame_idx;
172     int8_t  ref_frame_idx[AV1_REFS_PER_FRAME];
173     uint32_t delta_frame_id_minus1[AV1_REFS_PER_FRAME];
174
175     uint8_t allow_high_precision_mv;
176     uint8_t is_filter_switchable;
177     uint8_t interpolation_filter;
178     uint8_t is_motion_mode_switchable;
179     uint8_t use_ref_frame_mvs;
180
181     uint8_t disable_frame_end_update_cdf;
182
183     uint8_t uniform_tile_spacing_flag;
184     uint8_t tile_cols_log2;
185     uint8_t tile_rows_log2;
186     uint8_t width_in_sbs_minus_1[AV1_MAX_TILE_COLS];
187     uint8_t height_in_sbs_minus_1[AV1_MAX_TILE_ROWS];
188     uint16_t context_update_tile_id;
189     uint8_t tile_size_bytes_minus1;
190
191     // These are derived values, but it's very unhelpful to have to
192     // recalculate them all the time so we store them here.
193     uint16_t tile_cols;
194     uint16_t tile_rows;
195
196     uint8_t base_q_idx;
197     int8_t  delta_q_y_dc;
198     uint8_t diff_uv_delta;
199     int8_t  delta_q_u_dc;
200     int8_t  delta_q_u_ac;
201     int8_t  delta_q_v_dc;
202     int8_t  delta_q_v_ac;
203     uint8_t using_qmatrix;
204     uint8_t qm_y;
205     uint8_t qm_u;
206     uint8_t qm_v;
207
208     uint8_t segmentation_enabled;
209     uint8_t segmentation_update_map;
210     uint8_t segmentation_temporal_update;
211     uint8_t segmentation_update_data;
212     uint8_t feature_enabled[AV1_MAX_SEGMENTS][AV1_SEG_LVL_MAX];
213     int16_t feature_value[AV1_MAX_SEGMENTS][AV1_SEG_LVL_MAX];
214
215     uint8_t delta_q_present;
216     uint8_t delta_q_res;
217     uint8_t delta_lf_present;
218     uint8_t delta_lf_res;
219     uint8_t delta_lf_multi;
220
221     uint8_t loop_filter_level[4];
222     uint8_t loop_filter_sharpness;
223     uint8_t loop_filter_delta_enabled;
224     uint8_t loop_filter_delta_update;
225     uint8_t update_ref_delta[AV1_TOTAL_REFS_PER_FRAME];
226     int8_t  loop_filter_ref_deltas[AV1_TOTAL_REFS_PER_FRAME];
227     uint8_t update_mode_delta[2];
228     int8_t  loop_filter_mode_deltas[2];
229
230     uint8_t cdef_damping_minus_3;
231     uint8_t cdef_bits;
232     uint8_t cdef_y_pri_strength[8];
233     uint8_t cdef_y_sec_strength[8];
234     uint8_t cdef_uv_pri_strength[8];
235     uint8_t cdef_uv_sec_strength[8];
236
237     uint8_t lr_type[3];
238     uint8_t lr_unit_shift;
239     uint8_t lr_uv_shift;
240
241     uint8_t tx_mode;
242     uint8_t reference_select;
243     uint8_t skip_mode_present;
244
245     uint8_t allow_warped_motion;
246     uint8_t reduced_tx_set;
247
248     uint8_t is_global[AV1_TOTAL_REFS_PER_FRAME];
249     uint8_t is_rot_zoom[AV1_TOTAL_REFS_PER_FRAME];
250     uint8_t is_translation[AV1_TOTAL_REFS_PER_FRAME];
251     //AV1RawSubexp gm_params[AV1_TOTAL_REFS_PER_FRAME][6];
252     uint32_t gm_params[AV1_TOTAL_REFS_PER_FRAME][6];
253
254     uint8_t  apply_grain;
255     uint16_t grain_seed;
256     uint8_t  update_grain;
257     uint8_t  film_grain_params_ref_idx;
258     uint8_t  num_y_points;
259     uint8_t  point_y_value[14];
260     uint8_t  point_y_scaling[14];
261     uint8_t  chroma_scaling_from_luma;
262     uint8_t  num_cb_points;
263     uint8_t  point_cb_value[10];
264     uint8_t  point_cb_scaling[10];
265     uint8_t  num_cr_points;
266     uint8_t  point_cr_value[10];
267     uint8_t  point_cr_scaling[10];
268     uint8_t  grain_scaling_minus_8;
269     uint8_t  ar_coeff_lag;
270     uint8_t  ar_coeffs_y_plus_128[24];
271     uint8_t  ar_coeffs_cb_plus_128[25];
272     uint8_t  ar_coeffs_cr_plus_128[25];
273     uint8_t  ar_coeff_shift_minus_6;
274     uint8_t  grain_scale_shift;
275     uint8_t  cb_mult;
276     uint8_t  cb_luma_mult;
277     uint16_t cb_offset;
278     uint8_t  cr_mult;
279     uint8_t  cr_luma_mult;
280     uint16_t cr_offset;
281     uint8_t  overlap_flag;
282     uint8_t  clip_to_restricted_range;
283 } AV1RawFrameHeader;
284
285 typedef struct AV1RawTileData {
286     uint8_t     *data;
287     AVBufferRef *data_ref;
288     size_t       data_size;
289 } AV1RawTileData;
290
291 typedef struct AV1RawTileGroup {
292     uint8_t  tile_start_and_end_present_flag;
293     uint16_t tg_start;
294     uint16_t tg_end;
295
296     AV1RawTileData tile_data;
297 } AV1RawTileGroup;
298
299 typedef struct AV1RawFrame {
300     AV1RawFrameHeader header;
301     AV1RawTileGroup   tile_group;
302 } AV1RawFrame;
303
304 typedef struct AV1RawTileList {
305     uint8_t output_frame_width_in_tiles_minus_1;
306     uint8_t output_frame_height_in_tiles_minus_1;
307     uint16_t tile_count_minus_1;
308
309     AV1RawTileData tile_data;
310 } AV1RawTileList;
311
312 typedef struct AV1RawMetadataHDRCLL {
313     uint16_t max_cll;
314     uint16_t max_fall;
315 } AV1RawMetadataHDRCLL;
316
317 typedef struct AV1RawMetadataHDRMDCV {
318     uint16_t primary_chromaticity_x[3];
319     uint16_t primary_chromaticity_y[3];
320     uint16_t white_point_chromaticity_x;
321     uint16_t white_point_chromaticity_y;
322     uint32_t luminance_max;
323     uint32_t luminance_min;
324 } AV1RawMetadataHDRMDCV;
325
326 typedef struct AV1RawMetadataScalability {
327     uint8_t scalability_mode_idc;
328     uint8_t spatial_layers_cnt_minus_1;
329     uint8_t spatial_layer_dimensions_present_flag;
330     uint8_t spatial_layer_description_present_flag;
331     uint8_t temporal_group_description_present_flag;
332     uint8_t scalability_structure_reserved_3bits;
333     uint16_t spatial_layer_max_width[4];
334     uint16_t spatial_layer_max_height[4];
335     uint8_t spatial_layer_ref_id[4];
336     uint8_t temporal_group_size;
337     uint8_t temporal_group_temporal_id[255];
338     uint8_t temporal_group_temporal_switching_up_point_flag[255];
339     uint8_t temporal_group_spatial_switching_up_point_flag[255];
340     uint8_t temporal_group_ref_cnt[255];
341     uint8_t temporal_group_ref_pic_diff[255][7];
342 } AV1RawMetadataScalability;
343
344 typedef struct AV1RawMetadataITUTT35 {
345     uint8_t itu_t_t35_country_code;
346     uint8_t itu_t_t35_country_code_extension_byte;
347
348     uint8_t     *payload;
349     AVBufferRef *payload_ref;
350     size_t       payload_size;
351 } AV1RawMetadataITUTT35;
352
353 typedef struct AV1RawMetadataTimecode {
354     uint8_t  counting_type;
355     uint8_t  full_timestamp_flag;
356     uint8_t  discontinuity_flag;
357     uint8_t  cnt_dropped_flag;
358     uint16_t n_frames;
359     uint8_t  seconds_value;
360     uint8_t  minutes_value;
361     uint8_t  hours_value;
362     uint8_t  seconds_flag;
363     uint8_t  minutes_flag;
364     uint8_t  hours_flag;
365     uint8_t  time_offset_length;
366     uint32_t time_offset_value;
367 } AV1RawMetadataTimecode;
368
369 typedef struct AV1RawMetadata {
370     uint64_t metadata_type;
371     union {
372         AV1RawMetadataHDRCLL      hdr_cll;
373         AV1RawMetadataHDRMDCV     hdr_mdcv;
374         AV1RawMetadataScalability scalability;
375         AV1RawMetadataITUTT35     itut_t35;
376         AV1RawMetadataTimecode    timecode;
377     } metadata;
378 } AV1RawMetadata;
379
380 typedef struct AV1RawPadding {
381     uint8_t     *payload;
382     AVBufferRef *payload_ref;
383     size_t       payload_size;
384 } AV1RawPadding;
385
386
387 typedef struct AV1RawOBU {
388     AV1RawOBUHeader header;
389
390     size_t obu_size;
391
392     union {
393         AV1RawSequenceHeader sequence_header;
394         AV1RawFrameHeader    frame_header;
395         AV1RawFrame          frame;
396         AV1RawTileGroup      tile_group;
397         AV1RawTileList       tile_list;
398         AV1RawMetadata       metadata;
399         AV1RawPadding        padding;
400     } obu;
401 } AV1RawOBU;
402
403 typedef struct AV1ReferenceFrameState {
404     int valid;          // RefValid
405     int frame_id;       // RefFrameId
406     int upscaled_width; // RefUpscaledWidth
407     int frame_width;    // RefFrameWidth
408     int frame_height;   // RefFrameHeight
409     int render_width;   // RefRenderWidth
410     int render_height;  // RefRenderHeight
411     int frame_type;     // RefFrameType
412     int subsampling_x;  // RefSubsamplingX
413     int subsampling_y;  // RefSubsamplingY
414     int bit_depth;      // RefBitDepth
415     int order_hint;     // RefOrderHint
416 } AV1ReferenceFrameState;
417
418 typedef struct CodedBitstreamAV1Context {
419     AV1RawSequenceHeader *sequence_header;
420     AVBufferRef          *sequence_header_ref;
421
422     int     seen_frame_header;
423     AVBufferRef *frame_header_ref;
424     uint8_t     *frame_header;
425     size_t       frame_header_size;
426
427     int temporal_id;
428     int spatial_id;
429     int operating_point_idc;
430
431     int bit_depth;
432     int order_hint;
433     int frame_width;
434     int frame_height;
435     int upscaled_width;
436     int render_width;
437     int render_height;
438
439     int num_planes;
440     int coded_lossless;
441     int all_lossless;
442     int tile_cols;
443     int tile_rows;
444
445     AV1ReferenceFrameState ref[AV1_NUM_REF_FRAMES];
446 } CodedBitstreamAV1Context;
447
448
449 #endif /* AVCODEC_CBS_AV1_H */