1 /*
2 * Copyright (c) 2019-2024, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 //!
23 //! \file     codec_def_encode_av1.h
24 //! \brief    Defines encode AV1 types and macros shared by CodecHal, MHW, and DDI layer
25 //! \details  Applies to AV1 encode only. Should not contain any DDI specific code.
26 //!
27 #ifndef __CODEC_DEF_ENCODE_AV1_H__
28 #define __CODEC_DEF_ENCODE_AV1_H__
29 
30 #include "codec_def_common_av1.h"
31 #include "codec_def_common_encode.h"
32 #include "codec_def_encode_avc.h"
33 #include "codec_def_encode.h"
34 
35 #define CODECHAL_AV1_FRAME_HEADER_SIZE           8192
36 #define CODECHAL_ENCODE_AV1_PAK_INSERT_UNCOMPRESSED_HEADER 80
37 #define CODEC_AV1_NUM_REF_FRAMES 8
38 #define CODEC_AV1_NUM_REFL0P_FRAMES 3
39 #define CODEC_AV1_NUM_REFL0B_FRAMES 2
40 #define CODEC_AV1_NUM_REFL1B_FRAMES 1
41 #define CODEC_AV1_NUM_UNCOMPRESSED_SURFACE 127  // Maximum number of uncompressed decoded buffers that driver supports
42 #define ENCODE_VDENC_AV1_MAX_DIRTYRECT     16
43 #define ENCODE_VDENC_AV1_MAX_MOVERECT      16
44 #define ENCODE_AV1_MAX_NUM_TEMPORAL_LAYERS 8
45 #define ENCODE_VDENC_AV1_MAX_TILE_GROUP_NUM      128
46 #define ENCODE_MIM_SEGID_BLOCK_SIZE 32
47 #define ENCODE_AV1_MIN_ICQ_QUALITYFACTOR      0
48 #define ENCODE_AV1_MAX_ICQ_QUALITYFACTOR      255
49 #define ENCODE_AV1_ORDER_HINT_SIZE            256
50 // for VAConfigAttribValEncAV1Ext2.tx_mode_support
51 #define AV1_TX_MODE_ONLY_4X4_SUPPORTED 0x01
52 #define AV1_TX_MODE_LARGEST_SUPPORTED 0x02
53 #define AV1_TX_MODE_SELECT_SUPPORTED 0x04
54 #define AV1_MAX_NUM_OF_BATCH_BUFFER 5
55 #define AV1_MAX_NUM_OF_SEGMENTS 8
56 constexpr uint32_t TILE_SIZE_BYTES = 4;
57 const uint8_t OBU_LEB128_SIZE  = 4;
58 const uint8_t LEB128_BYTE_MASK = 0x7f;
59 const uint8_t av1CuReordSize   = 32;
60 static const uint32_t codechalEncodeModeAv1    = CODECHAL_ENCODE_MODE_AV1;           // codechal encode mode AV1
61 
62 enum {
63     AV1_Y_DC,
64     AV1_U_DC,
65     AV1_U_AC,
66     AV1_V_DC,
67     AV1_V_AC,
68     AV1_NUM_COEFF_QUANTIZERS
69 };
70 
71 typedef enum
72 {
73     AVP_CHROMA_FORMAT_MONOCHROME = 0,
74     AVP_CHROMA_FORMAT_YUV420 = 1,
75     AVP_CHROMA_FORMAT_YUV422 = 2,
76     AVP_CHROMA_FORMAT_YUV444 = 3
77 } AVP_CHROMA_FORMAT_IDC;
78 
79 typedef enum
80 {
81     SEQUENCE_HEADER_OBU        = 0,
82     TEMPORAL_DELIMITER_OBU     = 1,
83     PADDING_OBU                = 2,
84     META_DATA_OBU              = 3,
85     FRAME_HEADER_OBU           = 4,
86     FRAME_OBU                  = 5,
87     TILE_GROUP_OBU             = 6,
88     TILE_LIST_OBU              = 7,
89     REDUNDANT_FRAME_HEADER_OBU = 8,
90     MAX_NUM_OBU_TYPES          = 9
91 } AV1_OBU_TYPES;
92 
93 typedef enum
94 {
95     AV1_ENCODED_BIT_DEPTH_8 = 0,
96     AV1_ENCODED_BIT_DEPTH_10 = 1
97 } AV1_ENCODED_BIT_DEPTH;
98 
99 typedef enum  //VDEnc Frame Types
100 {
101     AV1_I_FRAME   = 0,  // I (Intra)
102     AV1_P_FRAME   = 1,  // P (Inter/Pred)
103     AV1_B_FRAME   = 2,  // B (BiPred/Random Access)
104     AV1_GPB_FRAME = 3,  // B (GPB/LowDelay)
105 } VDEncFrameType;
106 
107 enum TABLE_A1_COLS_INDEX
108 {
109     MAX_PIC_SIZE_INDEX = 0,
110     MAX_H_SIZE_INDEX,
111     MAX_V_SIZE_INDEX,
112     MAX_DISPLAY_RATE_INDEX,
113     MAX_DECODE_RATE_INDEX,
114     TABLE_A1_COLS_NUM,
115 };
116 
117 enum TABLE_A2_COLS_INDEX
118 {
119     MAX_HEADER_RATE_INDEX = 0,
120     MAIN_BPS_INDEX,
121     HIGH_BPS_INDEX,
122     MAIN_CR_INDEX,
123     HIGH_CR_INDEX,
124     MAX_TILES_INDEX,
125     MAX_TILE_COLS_INDEX,
126     TABLE_A2_COLS_NUM,
127 };
128 const uint64_t TableA1[][TABLE_A1_COLS_NUM] =
129     {
130         //  Level   |MaxPicSize | MaxHSize | MaxVSize | MaxDiaplayRate | MaxDecodeRate
131         /*  2.0 */ {147456, 2048, 1152, 4423680, 5529600},
132         /*  2.1 */ {278784, 2816, 1584, 8363520, 10454400},
133         /*  3.0 */ {665856, 4352, 2448, 19975680, 24969600},
134         /*  3.1 */ {1065024, 5504, 3096, 31950720, 39938400},
135         /*  4.0 */ {2359296, 6144, 3456, 70778880, 77856768},
136         /*  4.1 */ {2359296, 6144, 3456, 141557760, 155713536},
137         /*  5.0 */ {8912896, 8192, 4352, 267386880, 273705200},
138         /*  5.1 */ {8912896, 8192, 4352, 534773760, 547430400},
139         /*  5.2 */ {8912896, 8192, 4352, 1069547520, 1094860800},
140         /*  5.3 */ {8912896, 8192, 4352, 1069547520, 1176502272},
141         /*  6.0 */ {35651584, 16384, 8704, 1069547520, 1176502272},
142         /*  6.1 */ {35651584, 16384, 8704, 2139095040, 2189721600},
143         /*  6.2 */ {35651584, 16384, 8704, 4278190080, 4379443200},
144         /*  6.3 */ {35651584, 16384, 8704, 4278190080, 4706009088},
145 };
146 const uint32_t TableA2[][TABLE_A2_COLS_NUM] =
147     {
148         //  Level   | MaxHeaderRate |    Mainbps    |    Highbps    | MainCR | HighCR | MaxTiles | MaxTileCols
149         /*  2.0  */ {150, 1500000, 0, 2, 0, 8, 4},
150         /*  2.1  */ {150, 3000000, 0, 2, 0, 8, 4},
151         /*  3.0  */ {150, 6000000, 0, 2, 0, 16, 6},
152         /*  3.1  */ {150, 10000000, 0, 2, 0, 16, 6},
153         /*  4.0  */ {300, 12000000, 30000000, 4, 4, 32, 8},
154         /*  4.1  */ {300, 20000000, 50000000, 4, 4, 32, 8},
155         /*  5.0  */ {300, 30000000, 100000000, 6, 4, 64, 8},
156         /*  5.1  */ {300, 40000000, 160000000, 8, 4, 64, 8},
157         /*  5.2  */ {300, 60000000, 240000000, 8, 4, 64, 8},
158         /*  5.3  */ {300, 60000000, 240000000, 8, 4, 64, 8},
159         /*  6.0  */ {300, 60000000, 240000000, 8, 4, 128, 16},
160         /*  6.1  */ {300, 100000000, 480000000, 8, 4, 128, 16},
161         /*  6.2  */ {300, 160000000, 800000000, 8, 4, 128, 16},
162         /*  6.3  */ {300, 160000000, 800000000, 8, 4, 128, 16},
163 };
164 const uint32_t RdMultLUT[2][256] =
165     {
166         {58, 234, 234, 297, 366, 443, 528,
167         528, 619, 718, 825, 938, 1059, 1188, 1323, 1323, 1466, 1617, 1774,
168         1939, 2112, 2291, 2478, 2478, 2673, 2874, 3083, 3300, 3523, 3754,
169         3754, 3993, 4238, 4491, 4752, 5019, 5294, 5294, 5577, 5866, 6163,
170         6468, 6779, 6779, 7098, 7425, 7758, 8099, 8448, 8448, 8803, 9166,
171         9537, 9914, 10299, 10299, 10692, 11091, 11498, 11913, 11913, 12334,
172         12763, 13200, 13643, 14094, 14094, 14553, 15018, 15491, 15972,
173         15972, 16459, 16954, 17457, 17966, 17966, 18483, 19008, 19539,
174         20078, 20078, 20625, 21178, 21739, 22308, 22308, 22883, 23466,
175         24057, 24057, 24654, 25259, 25872, 26491, 26491, 27753, 28394,
176         29700, 31034, 31713, 33091, 33792, 35214, 35937, 37403, 38148,
177         39658, 40425, 41979, 42768, 44366, 45177, 46819, 47652, 49338,
178         50193, 51054, 52800, 53683, 55473, 57291, 59139, 61017, 62923,
179         65838, 67818, 69828, 71866, 73934, 76032, 78158, 80314, 82500,
180         84714, 86958, 89232, 91534, 95043, 98618, 101038, 104723, 108474,
181         111012, 114873, 118800, 121454, 125491, 128219, 132366, 135168,
182         139425, 145203, 149614, 154091, 158634, 163243, 167918, 172659,
183         177466, 182339, 187278, 193966, 199059, 205953, 211200, 216513,
184         223699, 229166, 234699, 242179, 249777, 257491, 265323, 271274,
185         279312, 287466, 295738, 304128, 312634, 321258, 330000, 338858,
186         350097, 359219, 368459, 380174, 389678, 399300, 411491, 423866,
187         433898, 446603, 459492, 472563, 485818, 499257, 512878, 526683,
188         540672, 554843, 572091, 586666, 604398, 619377, 637593, 656073,
189         674817, 693825, 713097, 732633, 755758, 779243, 799659, 827291,
190         851854, 876777, 905699, 935091, 964953, 999108, 1029966, 1065243,
191         1105137, 1145763, 1187123, 1229217, 1276366, 1328814, 1382318,
192         1436878, 1501866, 1568292, 1636154, 1715472, 1796666, 1884993,
193         1986218, 2090091, 2202291, 2323258, 2459457, 2605713, 2768923,
194         2943658, 3137291, 3344091, 3579194, 3829774, 4104334, 4420548,
195         4756843, 5140138, 5565354, 6026254, 6544618},
196         {4, 19, 23, 39, 52, 66, 92, 111, 143, 180, 220, 265, 314, 367,
197         424, 506, 573, 644, 745, 825, 939, 1060, 1155, 1289, 1394, 1541,
198         1695, 1856, 1982, 2156, 2338, 2527, 2723, 2926, 3084, 3300, 3524,
199         3755, 3993, 4239, 4492, 4686, 4952, 5225, 5506, 5794, 6089, 6315,
200         6623, 6938, 7261, 7591, 7843, 8186, 8536, 8894, 9167, 9537, 9915,
201         10300, 10593, 10991, 11396, 11705, 12123, 12441, 12872, 13310, 13644,
202         14095, 14438, 14902, 15373, 15731, 16215, 16583, 17080, 17457, 17967,
203         18354, 18876, 19273, 19674, 20215, 20625, 21179, 21599, 22023, 22595,
204         23029, 23614, 24057, 24505, 25108, 25565, 26026, 26961, 28073, 29044,
205         30031, 31204, 32227, 33266, 34322, 35575, 36667, 37775, 38900, 40041,
206         41199, 42373, 43564, 44771, 45995, 47235, 48492, 49765, 51055, 52361,
207         53684, 55023, 57063, 58907, 61017, 63164, 65104, 67321, 69323, 71610,
208         73675, 76032, 78159, 80315, 82775, 84994, 87241, 89518, 92115, 95044,
209         98318, 101648, 104724, 108160, 111651, 114873, 118141, 121789, 125153,
210         128563, 132019, 135873, 140141, 144839, 149245, 153716, 158254, 163244,
211         167919, 172660, 177467, 181931, 188108, 193967, 199487, 205519, 211640,
212         217852, 223700, 229625, 236093, 243123, 250256, 257978, 265324, 272273,
213         279818, 287467, 296260, 304656, 313706, 322345, 331101, 339974, 350097,
214         359794, 370205, 380175, 390875, 401117, 412721, 424490, 435793, 447884,
215         459492, 472564, 485819, 499257, 512879, 526684, 541376, 556985, 572092,
216         587400, 604399, 621640, 639123, 656073, 675604, 694623, 714715, 735094,
217         756591, 779244, 802230, 827292, 852739, 878571, 907523, 936018, 966835,
218         999108, 1032884, 1068210, 1106144, 1145764, 1187124, 1232404, 1279614,
219         1331023, 1384571, 1441473, 1503040, 1568292, 1639831, 1716726, 1799234,
220         1888939, 1986219, 2090092, 2205134, 2329100, 2465467, 2610352, 2772111,
221         2946945, 3140684, 3349346, 3581006, 3831649, 4112097, 4424575, 4763110,
222         5142310, 5567614, 4289813442, 4290334454}
223 };
224 
225 //DDI version 0.20
226 typedef struct _CODEC_AV1_ENCODE_SEQUENCE_PARAMS
227 {
228     uint8_t     seq_profile;    // [0]
229     uint8_t     seq_level_idx;  // [0..23, 31]
230     uint16_t    GopPicSize;
231     uint8_t     TargetUsage;
232     uint8_t     RateControlMethod;
233     uint8_t     GopRefDist;
234     uint8_t     GopOptFlag                      : 2;
235     uint8_t     reserved6b                      : 6;
236     uint32_t    TargetBitRate[8];   // One per temporal layer
237     uint32_t    MaxBitRate;
238     uint32_t    MinBitRate;
239     uint32_t    InitVBVBufferFullnessInBit;
240     uint32_t    VBVBufferSizeInBit;
241     uint32_t    OptimalVBVBufferLevelInBit;
242     uint32_t    UpperVBVBufferLevelThresholdInBit;
243     uint32_t    LowerVBVBufferLevelThresholdInBit;
244 
245     union
246     {
247         struct
248         {
249             uint32_t    ResetBRC                : 1;
250             uint32_t    StillPicture            : 1;
251             uint32_t    UseRawReconRef          : 1;
252             uint32_t    DisplayFormatSwizzle    : 1;    //[0]
253             uint32_t    bLookAheadPhase         : 1;
254             uint32_t    HierarchicalFlag        : 1;
255             uint32_t    RGBInputStudioRange     : 1;    // [0, 1]
256             uint32_t    ConvertedYUVStudioRange : 1;    // [0, 1]
257             uint32_t    Reserved0               : 24;
258         } fields;
259         uint32_t    value;
260     } SeqFlags;
261 
262     uint32_t    UserMaxIFrameSize;
263     uint32_t    UserMaxPBFrameSize;
264     FRAMERATE   FrameRate[8];   // One per temporal layer
265     uint8_t NumTemporalLayersMinus1;
266     uint8_t ICQQualityFactor;   // [0..255], with 0 being the best quality
267 
268     ENCODE_INPUT_COLORSPACE     InputColorSpace;
269     ENCODE_SCENARIO             ScenarioInfo;
270     ENCODE_CONTENT              ContentInfo;
271     ENCODE_FRAMESIZE_TOLERANCE  FrameSizeTolerance;
272     uint16_t                    SlidingWindowSize;
273     uint32_t                    MaxBitRatePerSlidingWindow;
274     uint32_t                    MinBitRatePerSlidingWindow;
275 
276     union
277     {
278         struct
279         {
280             uint32_t    enable_order_hint           : 1;
281             uint32_t    enable_superres             : 1;
282             uint32_t    enable_cdef                 : 1;
283             uint32_t    enable_restoration          : 1;
284             uint32_t    enable_warped_motion        : 1;    //[0]
285             uint32_t    enable_filter_intra         : 1;
286             uint32_t    enable_intra_edge_filter    : 1;
287             uint32_t    enable_interintra_compound  : 1;
288             uint32_t    enable_masked_compound      : 1;
289             uint32_t    enable_dual_filter          : 1;
290             uint32_t    enable_jnt_comp             : 1;
291             uint32_t    enable_ref_frame_mvs        : 1;
292             uint32_t    Reserved3                   : 20;
293         } fields;
294         uint32_t    value;
295     } CodingToolFlags;
296 
297     uint8_t     order_hint_bits_minus_1;    // [0..7]
298     union
299     {
300         uint8_t LookaheadDepth;               // [0..100]
301         uint8_t TargetFrameSizeConfidence;    // [0..100]
302     };
303 
304     uint8_t     Reserved8b2;
305     uint8_t     Reserved8b3;
306     uint32_t    Reserved32b[16];
307 } CODEC_AV1_ENCODE_SEQUENCE_PARAMS, *PCODEC_AV1_ENCODE_SEQUENCE_PARAMS;
308 
309 struct CODEC_Intel_Seg_AV1
310 {
311     union
312     {
313         struct
314         {
315             uint8_t   segmentation_enabled    : 1;
316             uint8_t   SegmentNumber           : 4;    //[0..8]
317             uint8_t   update_map              : 1;
318             uint8_t   temporal_update         : 1;
319             uint8_t   Reserved0               : 1;
320         } fields;
321         uint8_t   value;
322     } SegmentFlags;
323 
324     int16_t     feature_data[8][8];
325     uint8_t     feature_mask[8];
326     uint32_t    Reserved1[4];
327 };
328 
329 struct CODEC_Warped_Motion_Params_AV1
330 {
331     uint32_t    wmtype;
332     int32_t     wmmat[8];
333     int8_t      invalid;
334 };
335 
336 struct CODEC_Ref_Frame_Ctrl_AV1
337 {
338     union
339     {
340         struct
341         {
342             uint32_t search_idx0 : 3;
343             uint32_t search_idx1 : 3;
344             uint32_t search_idx2 : 3;
345             uint32_t search_idx3 : 3;
346             uint32_t search_idx4 : 3;
347             uint32_t search_idx5 : 3;
348             uint32_t search_idx6 : 3;
349             uint32_t ReservedField : 11;  //[0]
350         } fields;
351         uint32_t value;
352     } RefFrameCtrl;
353 };
354 
355 /*! \brief Provides the picture-level parameters of a compressed picture for AV1 decoding.
356 */
357 typedef struct _CODEC_AV1_ENCODE_PICTURE_PARAMS
358 {
359     uint16_t        frame_width_minus1;     // [15..2^16-1]
360     uint16_t        frame_height_minus1;    // [15..2^16-1]
361     uint8_t         NumTileGroupsMinus1;    // [0..255]
362     uint8_t         Reserved8b;              // [0]
363     CODEC_PICTURE   CurrOriginalPic;        // [0..127]
364     CODEC_PICTURE   CurrReconstructedPic;   // [0..11]
365     CODEC_PICTURE   RefFrameList [8];       // [0..11, 0xFF]
366     uint8_t         ref_frame_idx[7];       // [0..6]
367     uint8_t         HierarchLevelPlus1;
368     uint8_t         primary_ref_frame;      // [0..7]
369     uint8_t         Reserved8b3;
370     uint8_t         Reserved8b4;
371     uint8_t         order_hint;
372 
373     CODEC_Ref_Frame_Ctrl_AV1 ref_frame_ctrl_l0;
374     CODEC_Ref_Frame_Ctrl_AV1 ref_frame_ctrl_l1;
375 
376     union
377     {
378         struct
379         {
380             uint32_t    frame_type                      : 2;    // [0..3]
381             uint32_t    error_resilient_mode            : 1;    // [0..1]
382             uint32_t    disable_cdf_update              : 1;    // [0..1]
383             uint32_t    use_superres                    : 1;    // [0..1]
384             uint32_t    allow_high_precision_mv         : 1;    // [0..1]
385             uint32_t    use_ref_frame_mvs               : 1;    // [0..1]
386             uint32_t    disable_frame_end_update_cdf    : 1;    // [0..1]
387             uint32_t    reduced_tx_set_used             : 1;    // [0..1]
388             uint32_t    reserved1b                      : 1;    // [0..1]
389             uint32_t    SegIdBlockSize                  : 2;    // [0..3]
390             uint32_t    EnableFrameOBU                  : 1;
391             uint32_t    DisableFrameRecon               : 1;
392             uint32_t    LongTermReference               : 1;
393             uint32_t    allow_intrabc                   : 1;
394             uint32_t    PaletteModeEnable               : 1;
395             uint32_t    Reserved2                       : 15;
396         } fields;
397         uint32_t    value;
398     } PicFlags;
399 
400     // deblocking filter
401     uint8_t   filter_level[2];    // [0..63]
402     uint8_t   filter_level_u;     // [0..63]
403     uint8_t   filter_level_v;     // [0..63]
404 
405     union
406     {
407         struct
408         {
409             uint8_t   sharpness_level         : 3;    // [0..7]
410             uint8_t   mode_ref_delta_enabled  : 1;
411             uint8_t   mode_ref_delta_update   : 1;
412             uint8_t   Reserved3               : 3;    // [0]
413         } fields;
414         uint8_t   value;
415     } cLoopFilterInfoFlags;
416 
417     uint8_t   superres_scale_denominator; // [9..16]
418     uint8_t   interp_filter;              // [0..9]
419     uint8_t   Reserved4;                  // [0]
420     int8_t    ref_deltas[8];              // [-63..63]
421     int8_t    mode_deltas[2];             // [-63..63]
422 
423     // quantization
424     uint16_t  base_qindex;    // [0..255]
425     int8_t    y_dc_delta_q;   // [-15..15]
426     int8_t    u_dc_delta_q;   // [-63..63]
427     int8_t    u_ac_delta_q;   // [-63..63]
428     int8_t    v_dc_delta_q;   // [-63..63]
429     int8_t    v_ac_delta_q;   // [-63..63]
430     uint8_t   MinBaseQIndex;  // [1..255]
431     uint8_t   MaxBaseQIndex;  // [1..255]
432     uint8_t   Reserved5;      // [0]
433 
434     // quantization_matrix
435     union {
436         struct {
437             uint16_t  using_qmatrix   : 1;   // verify if supported
438 
439             // valid only when using_qmatrix is 1.
440             uint16_t  qm_y            : 4;   // [0..15]
441             uint16_t  qm_u            : 4;   // [0..15]
442             uint16_t  qm_v            : 4;   // [0..15]
443             uint16_t  Reserved6       : 3;   // [0]
444         } fields;
445         uint16_t  value;
446     } wQMatrixFlags;
447 
448     uint16_t  Reserved7;   // [0]
449 
450     union
451     {
452         struct
453         {
454             // delta_q parameters
455             uint32_t    delta_q_present_flag    : 1;    // [0..1]
456             uint32_t    log2_delta_q_res        : 2;    // [0..3]
457 
458             // delta_lf parameters
459             uint32_t    delta_lf_present_flag   : 1;    // [0..1]
460             uint32_t    log2_delta_lf_res       : 2;    // [0..3]
461             uint32_t    delta_lf_multi          : 1;    // [0..1]
462 
463             // read_tx_mode
464             uint32_t    tx_mode                 : 2;    // [0..3]
465 
466             // read_frame_reference_mode
467             uint32_t    reference_mode          : 2;    // [0..3]
468             uint32_t    reduced_tx_set_used     : 1;    // [0..1]
469 
470             uint32_t    skip_mode_present       : 1;    // [0..1]
471             uint32_t    Reserved8               : 19;   // [0]
472         } fields;
473         uint32_t    value;
474     } dwModeControlFlags;
475 
476     CODEC_Intel_Seg_AV1 stAV1Segments;
477     uint16_t            tile_cols;
478     uint16_t            width_in_sbs_minus_1[63];
479     uint16_t            tile_rows;
480     uint16_t            height_in_sbs_minus_1[63];
481     uint8_t             context_update_tile_id; // [0..127]
482     uint8_t             temporal_id;
483 
484     // CDEF
485     uint8_t   cdef_damping_minus_3;   // [0..3]
486     uint8_t   cdef_bits;              // [0..3]
487     uint8_t   cdef_y_strengths[8];    // [0..63]
488     uint8_t   cdef_uv_strengths[8];   // [0..63]
489 
490     union
491     {
492         struct
493         {
494             uint16_t  yframe_restoration_type     : 2;    // [0..3]
495             uint16_t  cbframe_restoration_type    : 2;    // [0..3]
496             uint16_t  crframe_restoration_type    : 2;    // [0..3]
497             uint16_t  lr_unit_shift               : 2;    // [0..2]
498             uint16_t  lr_uv_shift                 : 1;    // [0..1]
499             uint16_t  Reserved9                   : 7;    // [0]
500         } fields;
501         uint16_t  value;
502     } LoopRestorationFlags;
503 
504     // global motion
505     CODEC_Warped_Motion_Params_AV1   wm[7];
506 
507     uint32_t    QIndexBitOffset;
508     uint32_t    SegmentationBitOffset;
509     uint32_t    LoopFilterParamsBitOffset;
510     uint32_t    CDEFParamsBitOffset;
511     uint8_t     CDEFParamsSizeInBits;
512     uint8_t     reserved8bits0;
513     uint16_t    FrameHdrOBUSizeInBits;
514     uint32_t    FrameHdrOBUSizeByteOffset;
515     uint32_t    StatusReportFeedbackNumber;
516 
517     // Tile Group OBU header
518     union
519     {
520         struct
521         {
522             uint8_t obu_extension_flag : 1;  // [0..1]
523             uint8_t obu_has_size_field : 1;  // [0..1]
524             uint8_t temporal_id : 3;         // [0..7]
525             uint8_t spatial_id : 2;          // [0..3]
526             uint8_t ReservedField : 1;       // [0]
527         } fields;
528         uint8_t value;
529     } TileGroupOBUHdrInfo; //DDI 0.06
530 
531     uint8_t reserved8bs1;  // [0]
532     uint8_t reserved8bs2;  // [0]
533     // Skip Frames
534     uint8_t     NumSkipFrames;
535     int32_t     FrameSizeReducedInBytes;
536 
537     uint16_t    NumDirtyRects;
538     ENCODE_RECT *pDirtyRect;
539     uint16_t    NumMoveRects;
540     MOVE_RECT   *pMoveRect;
541     uint32_t    InputType;
542     uint32_t    TargetFrameSize;
543     uint8_t     QpModulationStrength;
544 
545     /*! \brief quality information report enable flags.
546     */
547     union
548     {
549         struct
550         {
551             uint8_t enable_frame : 1;
552             uint8_t enable_block : 1;
553             uint8_t reserved     : 6;
554         } fields;
555         uint8_t value;
556     } QualityInfoSupportFlags;
557     uint8_t     reserved8b[2];
558     uint32_t    Reserved10[14];
559 } CODEC_AV1_ENCODE_PICTURE_PARAMS, *PCODEC_AV1_ENCODE_PICTURE_PARAMS;
560 
561 /*! \brief Slice-level parameters of a compressed picture for AV1 encoding.
562 */
563 typedef struct _CODEC_AV1_ENCODE_TILE_GROUP_PARAMS
564 {
565     uint8_t  TileGroupStart;
566     uint8_t  TileGroupEnd;
567     uint16_t Reserved16b;
568     uint32_t Reserved32b[9];
569 } CODEC_AV1_ENCODE_TILE_GROUP_PARAMS, *PCODEC_AV1_ENCODE_TILE_GROUP_PARAMS;
570 
571 typedef struct _CODEC_AV1_ENCODE_PACKEDHEADER_DATA
572 {
573     uint8_t *pData;
574     uint32_t   BufferSize;
575     uint32_t   DataLength;
576     uint32_t   DataOffset;
577     uint32_t   SkipEmulationByteCount;
578     uint32_t   Reserved;
579 } CODEC_AV1ENCODE_PACKEDHEADER_DATA, *PCODEC_AV1ENCODE_PACKEDHEADER_DATA;
580 
581 #define MAX_BSTRUCTURE_GOP_SIZE 16
582 #define MAX_BSTRUCTURE_REF_NUM 8
583 #define MAX_TLEVEL  16
584 #define AV1_NUM_OF_REF_LF_DELTAS 8
585 #define AV1_NUM_OF_MODE_LF_DELTAS 2
586 #define AV1_NUM_OF_DUAL_CTX 2
587 struct EncodeAv1Par
588 {
589 
590     /* Configurable parameters */
591     uint32_t NumFrames;
592     uint32_t GopRefDist;
593     uint32_t GopOptFlag;
594     uint32_t Width;
595     uint32_t Height;
596     uint32_t FrameRateNom;
597     uint32_t FrameRateDeNom;
598     uint32_t EncMode;
599     uint32_t NumP;
600 
601     uint32_t Profile;
602     uint32_t InternalBitDepth;
603     uint32_t InputBitDepth;
604     uint32_t OutputBitDepth;
605     uint32_t ChromaFormatIDC;
606     uint32_t InputFileFormat;
607     uint32_t AdaptiveRounding;
608     uint32_t ColorSpace;
609     uint32_t DisableCdfUpdate;
610     uint32_t EnableSeg;
611     uint32_t SegMapUpdateCycle;
612     uint32_t SegTemporalUpdate;
613     uint32_t BaseKeyFrameQP;
614     uint32_t BasePFrameQP;
615     uint32_t KeyFrameQP[8];      /* Fixed QP for K frame, all segments */
616     uint32_t PFrameQP[8];        /* Fixed QP for P frame, all segments */
617     int32_t KeyCTQPDelta[AV1_NUM_COEFF_QUANTIZERS]; // Key frame QP deltas for coefficient type [YAC, UDC, UAC]
618     int32_t PCTQPDelta[AV1_NUM_COEFF_QUANTIZERS];   //P frame  QP deltas for coefficient type [YAC, UDC, UAC]
619     uint32_t BaseLoopFilterLevel[2];    /* Loop filter level, all segments */
620     uint32_t SegLoopFilterLevel[2][8];  /* Loop filter level, all segments */
621     int32_t RefTypeLFDelta[AV1_NUM_OF_REF_LF_DELTAS];  // Loop filter level delta for ref types
622     int32_t MBTypeLFDelta[AV1_NUM_OF_MODE_LF_DELTAS];  // Look filter level delta for MB types
623     int32_t SegmentRef[8];     //Fixed reference per segment: -1 = not fixed
624     uint32_t SegmentSkip[8];   //Segment skip flag, per segment.
625     uint32_t SegmentGlobalMV[8];     //Segment global MV flag, per segment.
626     uint32_t NumTileCols;
627     uint32_t NumTileRows;
628     uint32_t UniformSpacingFlag;
629     uint32_t TileWidths[av1MaxTileColumn];
630     uint32_t TileHeights[av1MaxTileColumn];
631     uint32_t Log2TileCols;
632     uint32_t Log2TileRows;
633     uint32_t LoopFilterAcrossTiles;
634     uint32_t NumTileGroup;
635     uint32_t MaxNumTileCols;
636     uint32_t MaxNumTileRows;
637     uint32_t MinNumTileCols;
638     uint32_t MinNumTileRows;
639     uint32_t TxMode;
640     uint32_t IntraOnly;
641     uint32_t McMode;
642     uint32_t AllowHpMv;
643     uint32_t CompPredMode;
644     uint32_t FrameParallel;
645     uint32_t ContextUpdateTileId;
646 
647     //RDO
648     uint32_t RdoEnable;
649     //intra prediction control
650     uint32_t KeyIntraPrediction;
651     uint32_t PIntraPrediction;
652 
653     //adaptive deadzone
654     uint32_t AdaptiveDeadZoneEnable;
655 
656     //VDEnc Mode
657     uint32_t VDEncSpeedMode;
658 
659     //Super Resolution
660     uint32_t    EnableSuperResolution;
661     uint32_t    SuperresScaleDenominator;
662     uint32_t    SuperresUsePreScaleRef;
663     uint32_t    SuperresStartFrame;
664     uint32_t    SuperresEndFrame;
665 
666     uint32_t    FrameIdNumbersPresentFlag;
667     int32_t     GroupTileHeaderNumBytes;
668 
669     uint32_t    LoopFilterLevelFormulaType;
670     uint32_t    DoSwapInRef1List;
671     uint32_t    PrimaryRefFrameSelection;
672 
673     uint32_t    UsingQmatrix;
674     uint32_t    QMatrixY;
675     uint32_t    QMatrixU;
676     uint32_t    QMatrixV;
677 
678     //Ref ctrl
679     uint32_t    RefCtrl;
680 
681     bool        AdditionalFWDAlphaSearchEnable;
682     bool        AdditionalFWDBetaSearchEnable;
683 
684     uint32_t    EnableStatistics;
685     uint32_t    RDOQEnable;
686     uint32_t    CDEFMode;
687     uint32_t    LRMode;
688     uint32_t    LRFilterTypeY;
689     uint32_t    LRFilterTypeU;
690     uint32_t    LRFilterTypeV;
691     uint32_t    EnableCDEFSearchForRandEnc;
692     uint32_t    EnableLRSearchForRandEnc;
693 
694     uint32_t    CDEFYStrength[8];
695     uint32_t    CDEFUVStrength[8];
696     uint32_t    CDEFBits;
697     uint32_t    UseDefaultCDEFStrengths;
698 
699     // StreamIn
700     uint32_t    StreamInEnable;
701     uint32_t    StreamInSegEnable;
702     uint32_t    StreamInMaxCuSize;
703     uint32_t    StreamInMaxTuSize;
704     uint32_t    StreamInNumImePredictors;
705     uint32_t    StreamInNumMergeCandidateCu8x8;
706     uint32_t    StreamInNumMergeCandidateCu16x16;
707     uint32_t    StreamInNumMergeCandidateCu32x32;
708     uint32_t    StreamInNumMergeCandidateCu64x64;
709 
710     //B Frame Coding Structure
711     uint32_t    BGOPSize;
712     int32_t     IntraPeriod;
713     uint32_t    PerBFramePOC[MAX_BSTRUCTURE_GOP_SIZE];
714     uint32_t    PerBFrameQPOffset[MAX_BSTRUCTURE_GOP_SIZE];
715     double      PerBFrameLambdaQPOffset[MAX_BSTRUCTURE_GOP_SIZE];
716     uint32_t    PerBFrameRoundingInter[MAX_BSTRUCTURE_GOP_SIZE];
717     uint32_t    PerBFrameRoundingIntra[MAX_BSTRUCTURE_GOP_SIZE];
718     double      PerBFrameQPFactor[MAX_BSTRUCTURE_GOP_SIZE];
719     uint32_t    PerBFrameTemporalID[MAX_BSTRUCTURE_GOP_SIZE];
720     uint32_t    PerBFrameNumRefPicsActiveL0[MAX_BSTRUCTURE_GOP_SIZE];
721     uint32_t    PerBFrameNumRefPicsActiveL1[MAX_BSTRUCTURE_GOP_SIZE];
722     uint32_t    PerBFrameNumRefPics[MAX_BSTRUCTURE_GOP_SIZE];
723     int32_t     PerBFrameRefPics[MAX_BSTRUCTURE_GOP_SIZE * MAX_BSTRUCTURE_REF_NUM];
724     uint32_t    PerBFrameEnableCurrPicInL1[MAX_BSTRUCTURE_GOP_SIZE];
725 
726 };
727 
728 struct MetadataAV1PostFeature
729 {
730     struct
731     {
732         uint64_t RowCount;
733         uint64_t ColCount;
734         uint64_t RowHeights[64];
735         uint64_t ColWidths[64];
736         uint64_t ContextUpdateTileId;
737     } tilePartition;
738 
739     struct
740     {
741         uint64_t CompoundPredictionType;
742         struct
743         {
744             uint64_t LoopFilterLevel[2];
745             uint64_t LoopFilterLevelU;
746             uint64_t LoopFilterLevelV;
747             uint64_t LoopFilterSharpnessLevel;
748             uint64_t LoopFilterDeltaEnabled;
749             uint64_t UpdateRefDelta;
750             int64_t  RefDeltas[8];
751             uint64_t UpdateModeDelta;
752             int64_t  ModeDeltas[2];
753         } LoopFilter;
754         struct
755         {
756             uint64_t DeltaLFPresent;
757             uint64_t DeltaLFMulti;
758             uint64_t DeltaLFRes;
759         } LoopFilterDelta;
760         struct
761         {
762             uint64_t BaseQIndex;
763             int64_t  YDCDeltaQ;
764             int64_t  UDCDeltaQ;
765             int64_t  UACDeltaQ;
766             int64_t  VDCDeltaQ;
767             int64_t  VACDeltaQ;
768             uint64_t UsingQMatrix;
769             uint64_t QMY;
770             uint64_t QMU;
771             uint64_t QMV;
772         } Quantization;
773         struct
774         {
775             uint64_t DeltaQPresent;
776             uint64_t DeltaQRes;
777         } QuantizationDelta;
778         struct
779         {
780             uint64_t CdefBits;
781             uint64_t CdefDampingMinus3;
782             uint64_t CdefYPriStrength[8];
783             uint64_t CdefUVPriStrength[8];
784             uint64_t CdefYSecStrength[8];
785             uint64_t CdefUVSecStrength[8];
786         } CDEF;
787         struct
788         {
789             uint64_t UpdateMap;
790             uint64_t TemporalUpdate;
791             uint64_t UpdateData;
792             uint64_t NumSegments;
793             struct
794             {
795                 uint64_t EnabledFeatures;
796                 int64_t  FeatureValue[8];
797             } SegmentsData[8];
798         } SegmentationConfig;
799         uint64_t PrimaryRefFrame;
800         uint64_t ReferenceIndices[7];
801     } postFeature;
802 };
803 
804 struct AV1MetaDataOffset
805 {
806     //tile partition
807     uint32_t dwRowCount            = 0;
808     uint32_t dwColCount            = 0;
809     uint32_t dwRowHeights          = 0;
810     uint32_t dwColWidths           = 0;
811     uint32_t dwContextUpdateTileId = 0;
812     //post feature
813     uint32_t dwCompoundPredictionType = 0;
814     uint32_t dwLoopFilter             = 0;
815     uint32_t dwLoopFilterDelta        = 0;
816     uint32_t dwQuantization           = 0;
817     uint32_t dwQuantizationDelta      = 0;
818     uint32_t dwCDEF                   = 0;
819     uint32_t dwSegmentationConfig     = 0;
820     uint32_t dwPrimaryRefFrame        = 0;
821     uint32_t dwReferenceIndices       = 0;
822     //loop filter
823     uint32_t dwLoopFilterLevel          = 0;
824     uint32_t dwLoopFilterLevelU         = 0;
825     uint32_t dwLoopFilterLevelV         = 0;
826     uint32_t dwLoopFilterSharpnessLevel = 0;
827     uint32_t dwLoopFilterDeltaEnabled   = 0;
828     uint32_t dwUpdateRefDelta           = 0;
829     uint32_t dwRefDeltas                = 0;
830     uint32_t dwUpdateModeDelta          = 0;
831     uint32_t dwModeDeltas               = 0;
832     //loop filter delta
833     uint32_t dwDeltaLFPresent = 0;
834     uint32_t dwDeltaLFMulti   = 0;
835     uint32_t dwDeltaLFRes     = 0;
836     //Quantization
837     uint32_t dwBaseQIndex   = 0;
838     uint32_t dwYDCDeltaQ    = 0;
839     uint32_t dwUDCDeltaQ    = 0;
840     uint32_t dwUACDeltaQ    = 0;
841     uint32_t dwVDCDeltaQ    = 0;
842     uint32_t dwVACDeltaQ    = 0;
843     uint32_t dwUsingQMatrix = 0;
844     uint32_t dwQMY          = 0;
845     uint32_t dwQMU          = 0;
846     uint32_t dwQMV          = 0;
847     //QuantizationDelta
848     uint32_t dwDeltaQPresent = 0;
849     uint32_t dwDeltaQRes     = 0;
850     //CDEF
851     uint32_t dwCdefBits          = 0;
852     uint32_t dwCdefDampingMinus3 = 0;
853     uint32_t dwCdefYPriStrength  = 0;
854     uint32_t dwCdefUVPriStrength = 0;
855     uint32_t dwCdefYSecStrength  = 0;
856     uint32_t dwCdefUVSecStrength = 0;
857     //SegmentationConfig
858     uint32_t dwUpdateMap      = 0;
859     uint32_t dwTemporalUpdate = 0;
860     uint32_t dwUpdateData     = 0;
861     uint32_t dwNumSegments    = 0;
862     uint32_t dwSegmentsData   = 0;
863     //dwSegmentsData
864     uint32_t dwEnabledFeatures  = 0;
865     uint32_t dwFeatureValue     = 0;
866     uint32_t dwSegmentsDataSize = 0;
867 };
868 
869 struct EncoderParamsAV1 : EncoderParams
870 {
871     uint32_t segmentMapDataSize = 0;     //!< [AV1] size of data in segment map buffer
872     uint8_t  *pSegmentMap = nullptr;     //!< [AV1] pointer to segment map buffer from DDI
873     AV1MetaDataOffset AV1metaDataOffset  = {};       //!< [AV1] AV1 Specific metadata offset
874 };
875 
876 enum RoundingMethod
877 {
878     fixedRounding = 0,
879     adaptiveRounding,
880     lookUpTableRounding
881 };
882 #endif  // __CODEC_DEF_ENCODE_AV1_H__
883