• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 AV1RawFilmGrainParams {
134     uint8_t  apply_grain;
135     uint16_t grain_seed;
136     uint8_t  update_grain;
137     uint8_t  film_grain_params_ref_idx;
138     uint8_t  num_y_points;
139     uint8_t  point_y_value[14];
140     uint8_t  point_y_scaling[14];
141     uint8_t  chroma_scaling_from_luma;
142     uint8_t  num_cb_points;
143     uint8_t  point_cb_value[10];
144     uint8_t  point_cb_scaling[10];
145     uint8_t  num_cr_points;
146     uint8_t  point_cr_value[10];
147     uint8_t  point_cr_scaling[10];
148     uint8_t  grain_scaling_minus_8;
149     uint8_t  ar_coeff_lag;
150     uint8_t  ar_coeffs_y_plus_128[24];
151     uint8_t  ar_coeffs_cb_plus_128[25];
152     uint8_t  ar_coeffs_cr_plus_128[25];
153     uint8_t  ar_coeff_shift_minus_6;
154     uint8_t  grain_scale_shift;
155     uint8_t  cb_mult;
156     uint8_t  cb_luma_mult;
157     uint16_t cb_offset;
158     uint8_t  cr_mult;
159     uint8_t  cr_luma_mult;
160     uint16_t cr_offset;
161     uint8_t  overlap_flag;
162     uint8_t  clip_to_restricted_range;
163 } AV1RawFilmGrainParams;
164 
165 typedef struct AV1RawFrameHeader {
166     uint8_t  show_existing_frame;
167     uint8_t  frame_to_show_map_idx;
168     uint32_t frame_presentation_time;
169     uint32_t display_frame_id;
170 
171     uint8_t frame_type;
172     uint8_t show_frame;
173     uint8_t showable_frame;
174 
175     uint8_t error_resilient_mode;
176     uint8_t disable_cdf_update;
177     uint8_t allow_screen_content_tools;
178     uint8_t force_integer_mv;
179 
180     uint32_t current_frame_id;
181     uint8_t  frame_size_override_flag;
182     uint8_t  order_hint;
183 
184     uint8_t  buffer_removal_time_present_flag;
185     uint32_t buffer_removal_time[AV1_MAX_OPERATING_POINTS];
186 
187     uint8_t  primary_ref_frame;
188     uint16_t frame_width_minus_1;
189     uint16_t frame_height_minus_1;
190     uint8_t  use_superres;
191     uint8_t  coded_denom;
192     uint8_t  render_and_frame_size_different;
193     uint16_t render_width_minus_1;
194     uint16_t render_height_minus_1;
195 
196     uint8_t found_ref[AV1_REFS_PER_FRAME];
197 
198     uint8_t refresh_frame_flags;
199     uint8_t allow_intrabc;
200     uint8_t ref_order_hint[AV1_NUM_REF_FRAMES];
201     uint8_t frame_refs_short_signaling;
202     uint8_t last_frame_idx;
203     uint8_t golden_frame_idx;
204     int8_t  ref_frame_idx[AV1_REFS_PER_FRAME];
205     uint32_t delta_frame_id_minus1[AV1_REFS_PER_FRAME];
206 
207     uint8_t allow_high_precision_mv;
208     uint8_t is_filter_switchable;
209     uint8_t interpolation_filter;
210     uint8_t is_motion_mode_switchable;
211     uint8_t use_ref_frame_mvs;
212 
213     uint8_t disable_frame_end_update_cdf;
214 
215     uint8_t uniform_tile_spacing_flag;
216     uint8_t tile_cols_log2;
217     uint8_t tile_rows_log2;
218     uint8_t width_in_sbs_minus_1[AV1_MAX_TILE_COLS];
219     uint8_t height_in_sbs_minus_1[AV1_MAX_TILE_ROWS];
220     uint16_t context_update_tile_id;
221     uint8_t tile_size_bytes_minus1;
222 
223     // These are derived values, but it's very unhelpful to have to
224     // recalculate them all the time so we store them here.
225     uint16_t tile_cols;
226     uint16_t tile_rows;
227 
228     uint8_t base_q_idx;
229     int8_t  delta_q_y_dc;
230     uint8_t diff_uv_delta;
231     int8_t  delta_q_u_dc;
232     int8_t  delta_q_u_ac;
233     int8_t  delta_q_v_dc;
234     int8_t  delta_q_v_ac;
235     uint8_t using_qmatrix;
236     uint8_t qm_y;
237     uint8_t qm_u;
238     uint8_t qm_v;
239 
240     uint8_t segmentation_enabled;
241     uint8_t segmentation_update_map;
242     uint8_t segmentation_temporal_update;
243     uint8_t segmentation_update_data;
244     uint8_t feature_enabled[AV1_MAX_SEGMENTS][AV1_SEG_LVL_MAX];
245     int16_t feature_value[AV1_MAX_SEGMENTS][AV1_SEG_LVL_MAX];
246 
247     uint8_t delta_q_present;
248     uint8_t delta_q_res;
249     uint8_t delta_lf_present;
250     uint8_t delta_lf_res;
251     uint8_t delta_lf_multi;
252 
253     uint8_t loop_filter_level[4];
254     uint8_t loop_filter_sharpness;
255     uint8_t loop_filter_delta_enabled;
256     uint8_t loop_filter_delta_update;
257     uint8_t update_ref_delta[AV1_TOTAL_REFS_PER_FRAME];
258     int8_t  loop_filter_ref_deltas[AV1_TOTAL_REFS_PER_FRAME];
259     uint8_t update_mode_delta[2];
260     int8_t  loop_filter_mode_deltas[2];
261 
262     uint8_t cdef_damping_minus_3;
263     uint8_t cdef_bits;
264     uint8_t cdef_y_pri_strength[8];
265     uint8_t cdef_y_sec_strength[8];
266     uint8_t cdef_uv_pri_strength[8];
267     uint8_t cdef_uv_sec_strength[8];
268 
269     uint8_t lr_type[3];
270     uint8_t lr_unit_shift;
271     uint8_t lr_uv_shift;
272 
273     uint8_t tx_mode;
274     uint8_t reference_select;
275     uint8_t skip_mode_present;
276 
277     uint8_t allow_warped_motion;
278     uint8_t reduced_tx_set;
279 
280     uint8_t is_global[AV1_TOTAL_REFS_PER_FRAME];
281     uint8_t is_rot_zoom[AV1_TOTAL_REFS_PER_FRAME];
282     uint8_t is_translation[AV1_TOTAL_REFS_PER_FRAME];
283     //AV1RawSubexp gm_params[AV1_TOTAL_REFS_PER_FRAME][6];
284     uint32_t gm_params[AV1_TOTAL_REFS_PER_FRAME][6];
285 
286     AV1RawFilmGrainParams film_grain;
287 } AV1RawFrameHeader;
288 
289 typedef struct AV1RawTileData {
290     uint8_t     *data;
291     AVBufferRef *data_ref;
292     size_t       data_size;
293 } AV1RawTileData;
294 
295 typedef struct AV1RawTileGroup {
296     uint8_t  tile_start_and_end_present_flag;
297     uint16_t tg_start;
298     uint16_t tg_end;
299 
300     AV1RawTileData tile_data;
301 } AV1RawTileGroup;
302 
303 typedef struct AV1RawFrame {
304     AV1RawFrameHeader header;
305     AV1RawTileGroup   tile_group;
306 } AV1RawFrame;
307 
308 typedef struct AV1RawTileList {
309     uint8_t output_frame_width_in_tiles_minus_1;
310     uint8_t output_frame_height_in_tiles_minus_1;
311     uint16_t tile_count_minus_1;
312 
313     AV1RawTileData tile_data;
314 } AV1RawTileList;
315 
316 typedef struct AV1RawMetadataHDRCLL {
317     uint16_t max_cll;
318     uint16_t max_fall;
319 } AV1RawMetadataHDRCLL;
320 
321 typedef struct AV1RawMetadataHDRMDCV {
322     uint16_t primary_chromaticity_x[3];
323     uint16_t primary_chromaticity_y[3];
324     uint16_t white_point_chromaticity_x;
325     uint16_t white_point_chromaticity_y;
326     uint32_t luminance_max;
327     uint32_t luminance_min;
328 } AV1RawMetadataHDRMDCV;
329 
330 typedef struct AV1RawMetadataScalability {
331     uint8_t scalability_mode_idc;
332     uint8_t spatial_layers_cnt_minus_1;
333     uint8_t spatial_layer_dimensions_present_flag;
334     uint8_t spatial_layer_description_present_flag;
335     uint8_t temporal_group_description_present_flag;
336     uint8_t scalability_structure_reserved_3bits;
337     uint16_t spatial_layer_max_width[4];
338     uint16_t spatial_layer_max_height[4];
339     uint8_t spatial_layer_ref_id[4];
340     uint8_t temporal_group_size;
341     uint8_t temporal_group_temporal_id[255];
342     uint8_t temporal_group_temporal_switching_up_point_flag[255];
343     uint8_t temporal_group_spatial_switching_up_point_flag[255];
344     uint8_t temporal_group_ref_cnt[255];
345     uint8_t temporal_group_ref_pic_diff[255][7];
346 } AV1RawMetadataScalability;
347 
348 typedef struct AV1RawMetadataITUTT35 {
349     uint8_t itu_t_t35_country_code;
350     uint8_t itu_t_t35_country_code_extension_byte;
351 
352     uint8_t     *payload;
353     AVBufferRef *payload_ref;
354     size_t       payload_size;
355 } AV1RawMetadataITUTT35;
356 
357 typedef struct AV1RawMetadataTimecode {
358     uint8_t  counting_type;
359     uint8_t  full_timestamp_flag;
360     uint8_t  discontinuity_flag;
361     uint8_t  cnt_dropped_flag;
362     uint16_t n_frames;
363     uint8_t  seconds_value;
364     uint8_t  minutes_value;
365     uint8_t  hours_value;
366     uint8_t  seconds_flag;
367     uint8_t  minutes_flag;
368     uint8_t  hours_flag;
369     uint8_t  time_offset_length;
370     uint32_t time_offset_value;
371 } AV1RawMetadataTimecode;
372 
373 typedef struct AV1RawMetadata {
374     uint64_t metadata_type;
375     union {
376         AV1RawMetadataHDRCLL      hdr_cll;
377         AV1RawMetadataHDRMDCV     hdr_mdcv;
378         AV1RawMetadataScalability scalability;
379         AV1RawMetadataITUTT35     itut_t35;
380         AV1RawMetadataTimecode    timecode;
381     } metadata;
382 } AV1RawMetadata;
383 
384 typedef struct AV1RawPadding {
385     uint8_t     *payload;
386     AVBufferRef *payload_ref;
387     size_t       payload_size;
388 } AV1RawPadding;
389 
390 
391 typedef struct AV1RawOBU {
392     AV1RawOBUHeader header;
393 
394     size_t obu_size;
395 
396     union {
397         AV1RawSequenceHeader sequence_header;
398         AV1RawFrameHeader    frame_header;
399         AV1RawFrame          frame;
400         AV1RawTileGroup      tile_group;
401         AV1RawTileList       tile_list;
402         AV1RawMetadata       metadata;
403         AV1RawPadding        padding;
404     } obu;
405 } AV1RawOBU;
406 
407 typedef struct AV1ReferenceFrameState {
408     int valid;          // RefValid
409     int frame_id;       // RefFrameId
410     int upscaled_width; // RefUpscaledWidth
411     int frame_width;    // RefFrameWidth
412     int frame_height;   // RefFrameHeight
413     int render_width;   // RefRenderWidth
414     int render_height;  // RefRenderHeight
415     int frame_type;     // RefFrameType
416     int subsampling_x;  // RefSubsamplingX
417     int subsampling_y;  // RefSubsamplingY
418     int bit_depth;      // RefBitDepth
419     int order_hint;     // RefOrderHint
420 
421     int8_t  loop_filter_ref_deltas[AV1_TOTAL_REFS_PER_FRAME];
422     int8_t  loop_filter_mode_deltas[2];
423     uint8_t feature_enabled[AV1_MAX_SEGMENTS][AV1_SEG_LVL_MAX];
424     int16_t feature_value[AV1_MAX_SEGMENTS][AV1_SEG_LVL_MAX];
425 } AV1ReferenceFrameState;
426 
427 typedef struct CodedBitstreamAV1Context {
428     const AVClass *class;
429 
430     AV1RawSequenceHeader *sequence_header;
431     AVBufferRef          *sequence_header_ref;
432 
433     int     seen_frame_header;
434     AVBufferRef *frame_header_ref;
435     uint8_t     *frame_header;
436     size_t       frame_header_size;
437 
438     int temporal_id;
439     int spatial_id;
440     int operating_point_idc;
441 
442     int bit_depth;
443     int order_hint;
444     int frame_width;
445     int frame_height;
446     int upscaled_width;
447     int render_width;
448     int render_height;
449 
450     int num_planes;
451     int coded_lossless;
452     int all_lossless;
453     int tile_cols;
454     int tile_rows;
455     int tile_num;
456 
457     AV1ReferenceFrameState ref[AV1_NUM_REF_FRAMES];
458 
459     // AVOptions
460     int operating_point;
461 } CodedBitstreamAV1Context;
462 
463 
464 #endif /* AVCODEC_CBS_AV1_H */
465