1 /*
2  * Copyright © 2018-2020, VideoLAN and dav1d authors
3  * Copyright © 2018, Two Orioles, LLC
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  *    list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef DAV1D_HEADERS_H
29 #define DAV1D_HEADERS_H
30 
31 #include <stdint.h>
32 #include <stddef.h>
33 
34 // Constants from Section 3. "Symbols and abbreviated terms"
35 #define DAV1D_MAX_CDEF_STRENGTHS 8
36 #define DAV1D_MAX_OPERATING_POINTS 32
37 #define DAV1D_MAX_TILE_COLS 64
38 #define DAV1D_MAX_TILE_ROWS 64
39 #define DAV1D_MAX_SEGMENTS 8
40 #define DAV1D_NUM_REF_FRAMES 8
41 #define DAV1D_PRIMARY_REF_NONE 7
42 #define DAV1D_REFS_PER_FRAME 7
43 #define DAV1D_TOTAL_REFS_PER_FRAME (DAV1D_REFS_PER_FRAME + 1)
44 
45 enum Dav1dObuType {
46     DAV1D_OBU_SEQ_HDR   = 1,
47     DAV1D_OBU_TD        = 2,
48     DAV1D_OBU_FRAME_HDR = 3,
49     DAV1D_OBU_TILE_GRP  = 4,
50     DAV1D_OBU_METADATA  = 5,
51     DAV1D_OBU_FRAME     = 6,
52     DAV1D_OBU_REDUNDANT_FRAME_HDR = 7,
53     DAV1D_OBU_PADDING   = 15,
54 };
55 
56 enum Dav1dTxfmMode {
57     DAV1D_TX_4X4_ONLY,
58     DAV1D_TX_LARGEST,
59     DAV1D_TX_SWITCHABLE,
60     DAV1D_N_TX_MODES,
61 };
62 
63 enum Dav1dFilterMode {
64     DAV1D_FILTER_8TAP_REGULAR,
65     DAV1D_FILTER_8TAP_SMOOTH,
66     DAV1D_FILTER_8TAP_SHARP,
67     DAV1D_N_SWITCHABLE_FILTERS,
68     DAV1D_FILTER_BILINEAR = DAV1D_N_SWITCHABLE_FILTERS,
69     DAV1D_N_FILTERS,
70     DAV1D_FILTER_SWITCHABLE = DAV1D_N_FILTERS,
71 };
72 
73 enum Dav1dAdaptiveBoolean {
74     DAV1D_OFF = 0,
75     DAV1D_ON = 1,
76     DAV1D_ADAPTIVE = 2,
77 };
78 
79 enum Dav1dRestorationType {
80     DAV1D_RESTORATION_NONE,
81     DAV1D_RESTORATION_SWITCHABLE,
82     DAV1D_RESTORATION_WIENER,
83     DAV1D_RESTORATION_SGRPROJ,
84 };
85 
86 enum Dav1dWarpedMotionType {
87     DAV1D_WM_TYPE_IDENTITY,
88     DAV1D_WM_TYPE_TRANSLATION,
89     DAV1D_WM_TYPE_ROT_ZOOM,
90     DAV1D_WM_TYPE_AFFINE,
91 };
92 
93 typedef struct Dav1dWarpedMotionParams {
94     enum Dav1dWarpedMotionType type;
95     int32_t matrix[6];
96     union {
97         struct {
98             int16_t alpha, beta, gamma, delta;
99         } p;
100         int16_t abcd[4];
101     } u;
102 } Dav1dWarpedMotionParams;
103 
104 enum Dav1dPixelLayout {
105     DAV1D_PIXEL_LAYOUT_I400, ///< monochrome
106     DAV1D_PIXEL_LAYOUT_I420, ///< 4:2:0 planar
107     DAV1D_PIXEL_LAYOUT_I422, ///< 4:2:2 planar
108     DAV1D_PIXEL_LAYOUT_I444, ///< 4:4:4 planar
109 };
110 
111 enum Dav1dFrameType {
112     DAV1D_FRAME_TYPE_KEY = 0,    ///< Key Intra frame
113     DAV1D_FRAME_TYPE_INTER = 1,  ///< Inter frame
114     DAV1D_FRAME_TYPE_INTRA = 2,  ///< Non key Intra frame
115     DAV1D_FRAME_TYPE_SWITCH = 3, ///< Switch Inter frame
116 };
117 
118 enum Dav1dColorPrimaries {
119     DAV1D_COLOR_PRI_BT709 = 1,
120     DAV1D_COLOR_PRI_UNKNOWN = 2,
121     DAV1D_COLOR_PRI_BT470M = 4,
122     DAV1D_COLOR_PRI_BT470BG = 5,
123     DAV1D_COLOR_PRI_BT601 = 6,
124     DAV1D_COLOR_PRI_SMPTE240 = 7,
125     DAV1D_COLOR_PRI_FILM = 8,
126     DAV1D_COLOR_PRI_BT2020 = 9,
127     DAV1D_COLOR_PRI_XYZ = 10,
128     DAV1D_COLOR_PRI_SMPTE431 = 11,
129     DAV1D_COLOR_PRI_SMPTE432 = 12,
130     DAV1D_COLOR_PRI_EBU3213 = 22,
131     DAV1D_COLOR_PRI_RESERVED = 255,
132 };
133 
134 enum Dav1dTransferCharacteristics {
135     DAV1D_TRC_BT709 = 1,
136     DAV1D_TRC_UNKNOWN = 2,
137     DAV1D_TRC_BT470M = 4,
138     DAV1D_TRC_BT470BG = 5,
139     DAV1D_TRC_BT601 = 6,
140     DAV1D_TRC_SMPTE240 = 7,
141     DAV1D_TRC_LINEAR = 8,
142     DAV1D_TRC_LOG100 = 9,         ///< logarithmic (100:1 range)
143     DAV1D_TRC_LOG100_SQRT10 = 10, ///< lograithmic (100*sqrt(10):1 range)
144     DAV1D_TRC_IEC61966 = 11,
145     DAV1D_TRC_BT1361 = 12,
146     DAV1D_TRC_SRGB = 13,
147     DAV1D_TRC_BT2020_10BIT = 14,
148     DAV1D_TRC_BT2020_12BIT = 15,
149     DAV1D_TRC_SMPTE2084 = 16,     ///< PQ
150     DAV1D_TRC_SMPTE428 = 17,
151     DAV1D_TRC_HLG = 18,           ///< hybrid log/gamma (BT.2100 / ARIB STD-B67)
152     DAV1D_TRC_RESERVED = 255,
153 };
154 
155 enum Dav1dMatrixCoefficients {
156     DAV1D_MC_IDENTITY = 0,
157     DAV1D_MC_BT709 = 1,
158     DAV1D_MC_UNKNOWN = 2,
159     DAV1D_MC_FCC = 4,
160     DAV1D_MC_BT470BG = 5,
161     DAV1D_MC_BT601 = 6,
162     DAV1D_MC_SMPTE240 = 7,
163     DAV1D_MC_SMPTE_YCGCO = 8,
164     DAV1D_MC_BT2020_NCL = 9,
165     DAV1D_MC_BT2020_CL = 10,
166     DAV1D_MC_SMPTE2085 = 11,
167     DAV1D_MC_CHROMAT_NCL = 12, ///< Chromaticity-derived
168     DAV1D_MC_CHROMAT_CL = 13,
169     DAV1D_MC_ICTCP = 14,
170     DAV1D_MC_RESERVED = 255,
171 };
172 
173 enum Dav1dChromaSamplePosition {
174     DAV1D_CHR_UNKNOWN = 0,
175     DAV1D_CHR_VERTICAL = 1,  ///< Horizontally co-located with luma(0, 0)
176                            ///< sample, between two vertical samples
177     DAV1D_CHR_COLOCATED = 2, ///< Co-located with luma(0, 0) sample
178 };
179 
180 typedef struct Dav1dContentLightLevel {
181     int max_content_light_level;
182     int max_frame_average_light_level;
183 } Dav1dContentLightLevel;
184 
185 typedef struct Dav1dMasteringDisplay {
186     ///< 0.16 fixed point
187     uint16_t primaries[3][2];
188     ///< 0.16 fixed point
189     uint16_t white_point[2];
190     ///< 24.8 fixed point
191     uint32_t max_luminance;
192     ///< 18.14 fixed point
193     uint32_t min_luminance;
194 } Dav1dMasteringDisplay;
195 
196 typedef struct Dav1dITUTT35 {
197     uint8_t  country_code;
198     uint8_t  country_code_extension_byte;
199     size_t   payload_size;
200     uint8_t *payload;
201 } Dav1dITUTT35;
202 
203 typedef struct Dav1dSequenceHeader {
204     /**
205      * Stream profile, 0 for 8-10 bits/component 4:2:0 or monochrome;
206      * 1 for 8-10 bits/component 4:4:4; 2 for 4:2:2 at any bits/component,
207      * or 12 bits/component at any chroma subsampling.
208      */
209     int profile;
210     /**
211      * Maximum dimensions for this stream. In non-scalable streams, these
212      * are often the actual dimensions of the stream, although that is not
213      * a normative requirement.
214      */
215     int max_width, max_height;
216     enum Dav1dPixelLayout layout; ///< format of the picture
217     enum Dav1dColorPrimaries pri; ///< color primaries (av1)
218     enum Dav1dTransferCharacteristics trc; ///< transfer characteristics (av1)
219     enum Dav1dMatrixCoefficients mtrx; ///< matrix coefficients (av1)
220     enum Dav1dChromaSamplePosition chr; ///< chroma sample position (av1)
221     /**
222      * 0, 1 and 2 mean 8, 10 or 12 bits/component, respectively. This is not
223      * exactly the same as 'hbd' from the spec; the spec's hbd distinguishes
224      * between 8 (0) and 10-12 (1) bits/component, and another element
225      * (twelve_bit) to distinguish between 10 and 12 bits/component. To get
226      * the spec's hbd, use !!our_hbd, and to get twelve_bit, use hbd == 2.
227      */
228     int hbd;
229     /**
230      * Pixel data uses JPEG pixel range ([0,255] for 8bits) instead of
231      * MPEG pixel range ([16,235] for 8bits luma, [16,240] for 8bits chroma).
232      */
233     int color_range;
234 
235     int num_operating_points;
236     struct Dav1dSequenceHeaderOperatingPoint {
237         int major_level, minor_level;
238         int initial_display_delay;
239         int idc;
240         int tier;
241         int decoder_model_param_present;
242         int display_model_param_present;
243     } operating_points[DAV1D_MAX_OPERATING_POINTS];
244 
245     int still_picture;
246     int reduced_still_picture_header;
247     int timing_info_present;
248     int num_units_in_tick;
249     int time_scale;
250     int equal_picture_interval;
251     unsigned num_ticks_per_picture;
252     int decoder_model_info_present;
253     int encoder_decoder_buffer_delay_length;
254     int num_units_in_decoding_tick;
255     int buffer_removal_delay_length;
256     int frame_presentation_delay_length;
257     int display_model_info_present;
258     int width_n_bits, height_n_bits;
259     int frame_id_numbers_present;
260     int delta_frame_id_n_bits;
261     int frame_id_n_bits;
262     int sb128;
263     int filter_intra;
264     int intra_edge_filter;
265     int inter_intra;
266     int masked_compound;
267     int warped_motion;
268     int dual_filter;
269     int order_hint;
270     int jnt_comp;
271     int ref_frame_mvs;
272     enum Dav1dAdaptiveBoolean screen_content_tools;
273     enum Dav1dAdaptiveBoolean force_integer_mv;
274     int order_hint_n_bits;
275     int super_res;
276     int cdef;
277     int restoration;
278     int ss_hor, ss_ver, monochrome;
279     int color_description_present;
280     int separate_uv_delta_q;
281     int film_grain_present;
282 
283     // Dav1dSequenceHeaders of the same sequence are required to be
284     // bit-identical until this offset. See 7.5 "Ordering of OBUs":
285     //   Within a particular coded video sequence, the contents of
286     //   sequence_header_obu must be bit-identical each time the
287     //   sequence header appears except for the contents of
288     //   operating_parameters_info.
289     struct Dav1dSequenceHeaderOperatingParameterInfo {
290         int decoder_buffer_delay;
291         int encoder_buffer_delay;
292         int low_delay_mode;
293     } operating_parameter_info[DAV1D_MAX_OPERATING_POINTS];
294 } Dav1dSequenceHeader;
295 
296 typedef struct Dav1dSegmentationData {
297     int delta_q;
298     int delta_lf_y_v, delta_lf_y_h, delta_lf_u, delta_lf_v;
299     int ref;
300     int skip;
301     int globalmv;
302 } Dav1dSegmentationData;
303 
304 typedef struct Dav1dSegmentationDataSet {
305     Dav1dSegmentationData d[DAV1D_MAX_SEGMENTS];
306     int preskip;
307     int last_active_segid;
308 } Dav1dSegmentationDataSet;
309 
310 typedef struct Dav1dLoopfilterModeRefDeltas {
311     int mode_delta[2 /* is_zeromv */];
312     int ref_delta[DAV1D_TOTAL_REFS_PER_FRAME];
313 } Dav1dLoopfilterModeRefDeltas;
314 
315 typedef struct Dav1dFilmGrainData {
316     unsigned seed;
317     int num_y_points;
318     uint8_t y_points[14][2 /* value, scaling */];
319     int chroma_scaling_from_luma;
320     int num_uv_points[2];
321     uint8_t uv_points[2][10][2 /* value, scaling */];
322     int scaling_shift;
323     int ar_coeff_lag;
324     int8_t ar_coeffs_y[24];
325     int8_t ar_coeffs_uv[2][25 + 3 /* padding for alignment purposes */];
326     uint64_t ar_coeff_shift;
327     int grain_scale_shift;
328     int uv_mult[2];
329     int uv_luma_mult[2];
330     int uv_offset[2];
331     int overlap_flag;
332     int clip_to_restricted_range;
333 } Dav1dFilmGrainData;
334 
335 typedef struct Dav1dFrameHeader {
336     struct {
337         Dav1dFilmGrainData data;
338         int present, update;
339     } film_grain; ///< film grain parameters
340     enum Dav1dFrameType frame_type; ///< type of the picture
341     int width[2 /* { coded_width, superresolution_upscaled_width } */], height;
342     int frame_offset; ///< frame number
343     int temporal_id; ///< temporal id of the frame for SVC
344     int spatial_id; ///< spatial id of the frame for SVC
345 
346     int show_existing_frame;
347     int existing_frame_idx;
348     int frame_id;
349     int frame_presentation_delay;
350     int show_frame;
351     int showable_frame;
352     int error_resilient_mode;
353     int disable_cdf_update;
354     int allow_screen_content_tools;
355     int force_integer_mv;
356     int frame_size_override;
357     int primary_ref_frame;
358     int buffer_removal_time_present;
359     struct Dav1dFrameHeaderOperatingPoint {
360         int buffer_removal_time;
361     } operating_points[DAV1D_MAX_OPERATING_POINTS];
362     int refresh_frame_flags;
363     int render_width, render_height;
364     struct {
365         int width_scale_denominator;
366         int enabled;
367     } super_res;
368     int have_render_size;
369     int allow_intrabc;
370     int frame_ref_short_signaling;
371     int refidx[DAV1D_REFS_PER_FRAME];
372     int hp;
373     enum Dav1dFilterMode subpel_filter_mode;
374     int switchable_motion_mode;
375     int use_ref_frame_mvs;
376     int refresh_context;
377     struct {
378         int uniform;
379         unsigned n_bytes;
380         int min_log2_cols, max_log2_cols, log2_cols, cols;
381         int min_log2_rows, max_log2_rows, log2_rows, rows;
382         uint16_t col_start_sb[DAV1D_MAX_TILE_COLS + 1];
383         uint16_t row_start_sb[DAV1D_MAX_TILE_ROWS + 1];
384         int update;
385     } tiling;
386     struct {
387         int yac;
388         int ydc_delta;
389         int udc_delta, uac_delta, vdc_delta, vac_delta;
390         int qm, qm_y, qm_u, qm_v;
391     } quant;
392     struct {
393         int enabled, update_map, temporal, update_data;
394         Dav1dSegmentationDataSet seg_data;
395         int lossless[DAV1D_MAX_SEGMENTS], qidx[DAV1D_MAX_SEGMENTS];
396     } segmentation;
397     struct {
398         struct {
399             int present;
400             int res_log2;
401         } q;
402         struct {
403             int present;
404             int res_log2;
405             int multi;
406         } lf;
407     } delta;
408     int all_lossless;
409     struct {
410         int level_y[2 /* dir */];
411         int level_u, level_v;
412         int mode_ref_delta_enabled;
413         int mode_ref_delta_update;
414         Dav1dLoopfilterModeRefDeltas mode_ref_deltas;
415         int sharpness;
416     } loopfilter;
417     struct {
418         int damping;
419         int n_bits;
420         int y_strength[DAV1D_MAX_CDEF_STRENGTHS];
421         int uv_strength[DAV1D_MAX_CDEF_STRENGTHS];
422     } cdef;
423     struct {
424         enum Dav1dRestorationType type[3 /* plane */];
425         int unit_size[2 /* y, uv */];
426     } restoration;
427     enum Dav1dTxfmMode txfm_mode;
428     int switchable_comp_refs;
429     int skip_mode_allowed, skip_mode_enabled, skip_mode_refs[2];
430     int warp_motion;
431     int reduced_txtp_set;
432     Dav1dWarpedMotionParams gmv[DAV1D_REFS_PER_FRAME];
433 } Dav1dFrameHeader;
434 
435 #endif /* DAV1D_HEADERS_H */
436