1 /*
2 * Copyright (c) 2016-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_hevc.h
24 //! \brief    Defines encode HEVC types and macros shared by CodecHal, MHW, and DDI layer
25 //! \details  Applies to HEVC encode only. Should not contain any DDI specific code.
26 //!
27 #ifndef __CODEC_DEF_ENCODE_HEVC_H__
28 #define __CODEC_DEF_ENCODE_HEVC_H__
29 
30 #include "codec_def_common_hevc.h"
31 #include "codec_def_common_encode.h"
32 
33 // HEVC
34 #define CODEC_MAX_NUM_REF_FRAME_HEVC        15
35 #define CODEC_NUM_REF_FRAME_HEVC_WP         3       // number of reference frames used for weighted prediction
36 #define CODEC_HEVC_MAX_QP                   51
37 #define CODEC_HEVC_MIN_QP_LP                10
38 #define HEVC_NUM_MAX_TILE_ROW               22
39 #define HEVC_NUM_MAX_TILE_COLUMN            20
40 #define CODECHAL_HEVC_MAX_NUM_SLICES_LVL_6  600
41 #define CODECHAL_HEVC_MAX_NUM_SLICES_LVL_5  200
42 #define CODECHAL_MAX_CUR_NUM_REF_FRAME_HEVC 8
43 #define CODECHAL_NUM_INTERNAL_NV12_RT_HEVC  16
44 #define CODECHAL_ENCODE_HEVC_MAX_NUM_ROI    16
45 #define CODECHAL_HEVC_FRAME_HEADER_SIZE     8192
46 #define CODECHAL_HEVC_MAX_LCU_SIZE_G10      64
47 #define CODECHAL_ENCODE_RECYCLED_BUFFER_NUM 6
48 #define CODECHAL_ENCODE_HEVC_MIN_STREAM_BUFFER_ALIGNMENT     4096
49 #define CODECHAL_ENCODE_HEVC_MI_COPY_MEM_MEM_CMD_ALIGNMENT 4
50 
51 #define CODECHAL_HEVC_BRC_MAX_QUALITY_LAYER   16
52 #define CODECHAL_HEVC_BRC_MAX_TEMPORAL_LAYER  3
53 #define CODECHAL_HEVC_MAX_BSTRUCTRUE_GOP_SIZE 16
54 #define CODECHAL_HEVC_MAX_BSTRUCTRUE_REF_NUM  8
55 #define CODECHAL_HEVC_NUM_MODE_MV_COSTS       171
56 
57 #define CODECHAL_HEVC_VDENC_MIN_TILE_WIDTH_SIZE  256
58 #define CODECHAL_HEVC_VDENC_MIN_TILE_HEIGHT_SIZE 128
59 #define CODECHAL_HEVC_SAO_STRMOUT_SIZE_PERLCU    16
60 #define CODECHAL_HEVC_VDENC_STATS_SIZE           1216
61 #define HEVC_BRC_PAK_STATISTCS_SIZE              32
62 
63 #define CODECHAL_VDENC_HEVC_BRC_HUC_STATUS_REENCODE_MASK     (1<<31)
64 #define CODECHAL_VDENC_HEVC_BRC_HISTORY_BUF_SIZE             964
65 
66 #define CODECHAL_HEVC_I_SLICE          2
67 #define CODECHAL_HEVC_P_SLICE          1
68 #define CODECHAL_HEVC_B_SLICE          0
69 #define CODECHAL_HEVC_NUM_SLICE_TYPES  3
70 
71 // HEVC VDENC
72 #define ENCODE_HEVC_VDENC_NUM_MAX_SLICES        70
73 #define ENCODE_VDENC_HEVC_MAX_STREAMINROI_G10   3
74 #define ENCODE_VDENC_HEVC_MAX_ROI_NUMBER_G10    8
75 #define ENCODE_VDENC_HEVC_MAX_DIRTYRECT_G10     256
76 #define ENCODE_VDENC_HEVC_ROI_BLOCKSIZE_G10     2        // 0:8x8, 1:16x16, 2:32x32, 3:64x64
77 #define ENCODE_VDENC_HEVC_MIN_ROI_DELTA_QP_G10  -8
78 #define ENCODE_VDENC_HEVC_MAX_ROI_DELTA_QP_G10  7        // Max delta QP for VDEnc ROI
79 #define ENCODE_VDENC_HEVC_PADDING_DW_SIZE       8
80 #define CODECHAL_ENCODE_HEVC_MAX_NUM_DIRTYRECT  16
81 
82 #define ENCODE_HEVC_MAX_16K_PIC_WIDTH 16384
83 #define ENCODE_HEVC_MAX_16K_PIC_HEIGHT 16384
84 #define CODECHAL_ENCODE_HEVC_NUM_MAX_VDENC_L0_REF_G10 3  // multiref, hevc vdenc
85 #define CODECHAL_ENCODE_HEVC_NUM_MAX_VDENC_L1_REF_G10 3  // multiref, hevc vdenc
86 
87 // HEVC DP
88 #define ENCODE_DP_HEVC_NUM_MAX_VME_L0_REF_G9  3
89 #define ENCODE_DP_HEVC_NUM_MAX_VME_L1_REF_G9  1
90 #define ENCODE_DP_HEVC_MAX_NUM_ROI            16
91 #define ENCODE_DP_HEVC_ROI_BLOCK_SIZE         1     //From DDI, 0:8x8, 1:16x16, 2:32x32, 3:64x64
92 #define ENCODE_DP_HEVC_ROI_BLOCK_Width        16
93 #define ENCODE_DP_HEVC_ROI_BLOCK_HEIGHT       16
94 
95 //!
96 //! \enum     HEVC_BRC_FRAME_TYPE
97 //! \brief    HEVC BRC frame type
98 //!
99 enum HEVC_BRC_FRAME_TYPE
100 {
101     HEVC_BRC_FRAME_TYPE_P_OR_LB = 0,
102     HEVC_BRC_FRAME_TYPE_B = 1,
103     HEVC_BRC_FRAME_TYPE_I = 2,
104     HEVC_BRC_FRAME_TYPE_B1 = 3,
105     HEVC_BRC_FRAME_TYPE_B2 = 4,
106     HEVC_BRC_FRAME_TYPE_INVALID
107 };
108 
109 typedef enum
110 {
111     ENCODE_HEVC_BIT_DEPTH_8     = 0,
112     ENCODE_HEVC_BIT_DEPTH_10    = 1,
113     ENCODE_HEVC_BIT_DEPTH_12    = 2,
114     ENCODE_HEVC_BIT_DEPTH_16    = 3,
115 } ENCODE_HEVC_BIT_DEPTH;
116 
117 //!
118 //! \enum HEVC_NAL_UNIT_TYPE
119 //! \brief HEVC NAL unit type
120 //!
121 typedef enum
122 {
123     HEVC_NAL_UT_TRAIL_N           = 0x00, //!< Coded slice segment of a non-TSA, non-STSA trailing picture - slice_segment_layer_rbsp, VLC
124     HEVC_NAL_UT_TRAIL_R           = 0x01, //!< Coded slice segment of a non-TSA, non-STSA trailing picture - slice_segment_layer_rbsp, VLC
125     HEVC_NAL_UT_TSA_N             = 0x02, //!< Coded slice segment of a TSA picture - slice_segment_layer_rbsp, VLC
126     HEVC_NAL_UT_TSA_R             = 0x03, //!< Coded slice segment of a TSA picture - slice_segment_layer_rbsp, VLC
127     HEVC_NAL_UT_STSA_N            = 0x04, //!< Coded slice of an STSA picture - slice_layer_rbsp, VLC
128     HEVC_NAL_UT_STSA_R            = 0x05, //!< Coded slice of an STSA picture - slice_layer_rbsp, VLC
129     HEVC_NAL_UT_RADL_N            = 0x06, //!< Coded slice of an RADL picture - slice_layer_rbsp, VLC
130     HEVC_NAL_UT_RADL_R            = 0x07, //!< Coded slice of an RADL picture - slice_layer_rbsp, VLC
131     HEVC_NAL_UT_RASL_N            = 0x08, //!< Coded slice of an RASL picture - slice_layer_rbsp, VLC
132     HEVC_NAL_UT_RASL_R            = 0x09, //!< Coded slice of an RASL picture - slice_layer_rbsp, VLC
133     /* 0x0a..0x0f - Reserved */
134     HEVC_NAL_UT_BLA_W_LP          = 0x10, //!< Coded slice segment of an BLA picture - slice_segment_layer_rbsp, VLC
135     HEVC_NAL_UT_BLA_W_DLP         = 0x11, //!< Coded slice segment of an BLA picture - slice_segment_layer_rbsp, VLC
136     HEVC_NAL_UT_BLA_N_LP          = 0x12, //!< Coded slice segment of an BLA picture - slice_segment_layer_rbsp, VLC
137     HEVC_NAL_UT_IDR_W_DLP         = 0x13, //!< Coded slice segment of an IDR picture - slice_segment_layer_rbsp, VLC
138     HEVC_NAL_UT_IDR_N_LP          = 0x14, //!< Coded slice segment of an IDR picture - slice_segment_layer_rbsp, VLC
139     HEVC_NAL_UT_CRA               = 0x15, //!< Coded slice segment of an CRA picture - slice_segment_layer_rbsp, VLC
140     HEVC_NAL_UT_RSV_IRAP_VCL23    = 0x17, //!< Reserved IRAP VCL NAL unit type
141     /* 0x16..0x1f - Reserved */
142     HEVC_NAL_UT_VPS               = 0x20, //!< Video parameter set - video_parameter_set_rbsp, non-VLC
143     HEVC_NAL_UT_SPS               = 0x21, //!< Sequence parameter set - seq_parameter_set_rbsp, non-VLC
144     HEVC_NAL_UT_PPS               = 0x22, //!< Picture parameter set - pic_parameter_set_rbsp, non-VLC
145     HEVC_NAL_UT_AUD               = 0x23, //!< Access unit delimiter - access_unit_delimiter_rbsp, non-VLC
146     HEVC_NAL_UT_EOS               = 0x24, //!< End of sequence - end_of_seq_rbsp, non-VLC
147     HEVC_NAL_UT_EOB               = 0x25, //!< End of bitsteam - end_of_bitsteam_rbsp, non-VLC
148     HEVC_NAL_UT_FD                = 0x26, //!< Filler data - filler_data_rbsp, non-VLC
149     HEVC_NAL_UT_PREFIX_SEI        = 0x27, //!< Supplemental enhancement information (SEI) - sei_rbsp, non_VLC
150     HEVC_NAL_UT_SUFFIX_SEI        = 0x28, //!< Supplemental enhancement information (SEI) - sei_rbsp, non_VLC
151 
152     //this should be the last element of this enum
153     //chagne this value if NAL unit type increased
154     HEVC_MAX_NAL_UNIT_TYPE        = 0x3f,
155 } HEVC_NAL_UNIT_TYPE;
156 
157 typedef struct _CODEC_FRAMERATE
158 {
159     uint32_t    Numerator;
160     uint32_t    Denominator;
161 }CODEC_FRAMERATE;
162 
163 /*! \brief Provides the picture-level parameters of a compressed picture for HEVC decoding.
164 *
165 *   The framework is expected to only send a sequence parameter compressed buffer for the first picture(first Execute() call) in a sequence, associated with a RAP(IDR, CRA, BLA) picture.
166 */
167 typedef struct _CODEC_HEVC_ENCODE_SEQUENCE_PARAMS
168 {
169     /*! \brief Plus 1 specifies the width of each encoded picture in units of minimum coding block size.
170     *
171     *   The encoded picture width in units of luma samples equals (wFrameWidthInMinCbMinus1 + 1) * (1 << (log2_min_coding_block_size_minus3 + 3))
172     *   Programming Note: HW requres surface allocation Y offset of chroma plain to be multiple of 32 pixels. And HEVC spec requires frame resolution to be multiple of minimal CU (could be 8 pixels) horizontally and vertically. Framework needs to pad accordingly. If source resolution is different from what is actually encoded (padding happens), cropping information should be provided in the SPS header accordingly.
173     */
174     uint16_t      wFrameWidthInMinCbMinus1;
175     /*! \brief Plus 1 specifies the height of each encoded picture in units of minimum coding block size.
176     *
177     *   The encoded picture height in units of luma samples equals (wFrameHeightInMinCbMinus1 + 1) * (1 << (log2_min_coding_block_size_minus3 + 3))
178     */
179     uint16_t      wFrameHeightInMinCbMinus1;
180     uint8_t       general_profile_idc;                //!< Same as HEVC syntax element
181     /*! \brief Equal to syntax element general_level_idc / 3
182     *
183     *   Value range of general_level_idc is 30 times [1, 2, 2.1, 3, 3.1, 4, 4.1, 5, 5.1, 5.2, 6, 6.1, 6.2].
184     *   Programming Note: HW requres surface allocation Y offset of chroma plain to be multiple of 32 pixels. And HEVC spec requires frame resolution to be multiple of minimal CU (could be 8 pixels) horizontally and vertically. Framework needs to pad accordingly. If source resolution is different from what is actually encoded (padding happens), cropping information should be provided in the SPS header accordingly.
185     */
186     uint8_t       Level;
187     uint8_t       general_tier_flag;                  //!< Same as HEVC syntax element
188 
189     uint16_t      GopPicSize;                         //!< Distance between IRAP pictures.
190     /*! \brief Distance between anchor frames (I or P).
191     *
192     *    Here, P may also means low delay B (GPB) frames.
193     *    Programming Note: GopPicSize > GopRefDist should be ensured by application. It is required by BRC.
194     */
195     uint8_t       GopRefDist;
196     uint8_t       GopOptFlag                  : 2;
197     uint8_t                                   : 6;
198 
199     uint8_t       TargetUsage;
200     /*! \brief Specifies rate control method.
201     *
202     *    \n 1: CBR
203     *    \n 2: VBR
204     *    \n 3: CQP, if set, internal BRC, multi-pass and panic mode will be disabled
205     *    \n 4: AVBR, if set, HRD compliance is not guaranteed. Buffering period SEI and picture timing SEI messages are not necessary for AVBR.
206     *    \n 5: QVBR
207     *    \n 6: Reserved
208     *    \n 7: Reserved
209     *    \n 8: VCM, defined for video converencing
210     *    \n 9: ICQ
211     *    \n Programming note: Define the minimum value as indicated above for AVBR accuracy & convergence, clamp any value that is less than the minimum value to the minimum value.  Define the maximum value for AVBR accuracy as 100 (10%) and for AVBR convergence as 500, clamp any value that is greater than the maximum value to the maximum value. The maximum & minimum value may be adjusted when necessary. If bResetBRC is set to 1 for a non-I picture, driver shall not insert SPS into bitstream.  Driver needs to calculate the maximum allowed frame size per profile/level for all RateControlMethod except CQP, and use the calculated value to program kernel for non AVBR modes; for AVBR mode, driver needs to clamp the upper bound of UserMaxFrameSize to the calculated value and use the clamped UserMaxFrameSize to program kernel.  If IWD_VBR is set, driver programs it the same as VBR except not to enable panic mode.
212     */
213     uint8_t       RateControlMethod;
214     uint32_t      TargetBitRate;                      //!< Target bit rate Kbit per second
215     uint32_t      MaxBitRate;                         //!< Maximum bit rate Kbit per second
216     /*! \brief Minimun bit rate Kbit per second.
217     *
218     *   This is used in VBR control. For CBR control, this field is ignored.
219     */
220     uint32_t          MinBitRate;
221     CODEC_FRAMERATE   FrameRate;                          //!< Actual frame rate is the decimal derivative of FrameRate.Numerator / FrameRate.Denominator.
222     uint32_t          InitVBVBufferFullnessInBit;         //!< Initial VBV buffer fullness in bits.
223     /*! \brief VBV buffer size in bit unit.
224     *
225     *   The HEVC spec defines a max coded picture buffer size for each level.
226     */
227     uint32_t       VBVBufferSizeInBit;
228 
229     union
230     {
231         struct
232         {
233             /*! \brief Indicate if a BRC reset is desired to set a new bit rate or frame rate.
234             *
235             *   This setting is only valid if RateControlMethod is AVBR or VBR and the current picture is an I picture. If the frame resolution is changed, it should be set with IDR picture. It should not be set when RateControlMethod is CBR or CQP. The following table indicates which BRC parameters can be changed via a BRC reset.
236             *
237             *  \n BRC Parameters       Changes allowed via reset
238             *  \n Profile & Level               Yes
239             *  \n UserMaxFrameSize              Yes
240             *  \n InitVBVBufferFullnessInBit    No
241             *  \n TargetBitRate                 Yes
242             *  \n VBVBufferSizeInBit            No
243             *  \n MaxBitRate                    Yes
244             *  \n FramesPer100Sec *             No
245             *  \n RateControlMethod             No
246             *  \n GopPicSize                    No
247             *  \n GopRefDist                    No
248             *  \n GopOptFlag                    Yes
249             *  \n FrameWidth                    No
250             *  \n FrameHeight                   No
251             *  \n AVBRAccuracy                  No
252             *  \n AVBRConvergence               No
253             *  \n Note: when resolution (FrameWidth and/or FrameHeight) changes, framework should re-start a new bit stream and not using BRC reset.
254             */
255             uint32_t        bResetBRC           : 1;
256             /*! \brief Indicates the global search options.
257             *
258             *   It is only valid if EnhancedEncInput is reported as a capability.
259             *       \n - 0 : Default - option internally selected based on target usage
260             *       \n - 1 : Long - SHME enabled
261             *       \n - 2 : Medium - HME only enabled, SHME disabled
262             *       \n - 3 : Short - SHME/HME disabled
263             */
264             uint32_t        GlobalSearch        : 2;
265 
266             /*! \brief Indicates the local search options.
267             *
268             *    It is only valid if EnhancedEncInput is reported as a capability.
269             *        \n - 0 : Default - option internally selected based on target usage
270             *        \n - 1 : Tiny – MaxLenSP = 4, Reference Window = 24x24 SP = Spiral
271             *        \n - 2 : Small – MaxLenSP = 9, Reference Window = 28x28 SP = Spiral
272             *        \n - 3 : Square – MaxLenSP = 16, Reference Window = 32x32 SP = Spiral
273             *        \n - 4 : Diamond – MaxLenSP = 16, Reference Window = 48x40 SP = Diamond
274             *        \n - 5 : Large Diamond – MaxLenSP = 30, Reference Window = 48x40 SP = Diamond
275             *        \n - 6 : Exhaustive – MaxLenSP = 57, Reference Window = 48x40 SP = Spiral
276             *        \n - 7 : Heavy Horizontal – MaxLenSP = 57, Reference Window = 64x32 SP = Spiral
277             *        \n - 8 : Heavy Vertical – MaxLenSP = 57, Reference Window = 32x64 SP = Spiral
278             */
279             uint32_t        LocalSearch         : 4;
280             /*! \brief Indicates the EarlySkip control.
281             *
282             *    It is only valid if EnhancedEncInput is reported as a capability.
283             *        \n - 0 : Default, decided internally
284             *        \n - 1 : EarlySkip enabled
285             *        \n - 2 : EarlySkip disabled
286             */
287             uint32_t        EarlySkip           : 2;
288             /*! \brief Indicates that MB BRC is enabled.
289             *
290             *    It is only valid if MBBRCSupport is reported as a capability. Currently MB BRC can be applied to all bit rate control methods except CQP.
291             *        \n - 0 : Default, decided internally based on target usage.
292             *        \n - 1 : MB BRC enabled.
293             *        \n - 2 : MB BRC disabled.
294             *        \n - Other values are reserved.
295             */
296             uint32_t        MBBRC               : 4;
297             /*! \brief Indicates that Parallel BRC is enabled.
298             *
299             *    It is only valid if ParallelBRC is reported as a capability.
300             *        \n - 0 : no parallel BRC required.
301             *        \n - 1 : request parallel BRC.
302             */
303             uint32_t        ParallelBRC         : 1;
304             /*! \brief Indicates that Dynamic Slice Size Control is enabled.
305             *
306             *    It is only valid if SliceByteSizeControl is reported as a capability. When enabled, the slice information that app provides will be applied as "supper slice". It means that GPU may further split the slice region that slice control data specifies into finer slice segments based on slice size upper limit (MaxSliceSize). GPU will duplicate the slice header bit stream for each splitted slice segments while updating the necessary fields such as slice_segment_address.
307             */
308             uint32_t        SliceSizeControl    : 1;
309             /*! \brief Specifies input source format
310             *
311             *    \n 0:    YUV420
312             *    \n 1:    YUV422
313             *    \n 2:    YUV444
314             *    \n 3:    RGB
315             *    \n Note1: Encoder cannot do up-sampling. For example, if source format is YUV422, the encoder can generates bit stream of 420 or 422 only. It cannot generate YUV444 format. But it may generates RGB format.
316             *    Note2: For RGB, the actual input source format is also determined by flag DisplayFormatSwizzle (formats below is in MSB->LSB order).
317             *        \n - If DisplayFromatSwizzle is 0, and 8b intut is specified the format is A8B8G8R8, if 10 bit is specified the format is A2B10G10R10
318             *        \n - If DisplayFromatSwizzle is 0, and 8b intut is specified the format is A8R8G8B8, if 10 bit is specified the format is A2R10G10B10
319             */
320             uint32_t        SourceFormat        : 2;
321             /*! \brief Specifies input source bit depth.
322             *
323             *    \n 0:    8b
324             *    \n 1:    10b
325             *    \n 2:    12b
326             *    \n 3:    16b
327             *    \n Note: Encoder cannot do up-sampling. For example, if source bit depth is 10b, the encoder can generates bit stream of 8b or 10b only, and that it cannot generate 12b format. It is subjected to the limit set by MaxEncodedBitDepth.
328             */
329             uint32_t        SourceBitDepth      : 2;
330             /*! \brief Enable QP Adjustment at CQP mode.
331             *
332             *        \n - 0 : no QP adjustment.
333             *        \n - 1 : enable QP adjustment.
334             *        \n Note: Basically ENC may define fine-tune QP adjustment based on motion search result, which is similar as MBBRC for BRC mode. With BRC modes, this flag should be set to 0.
335             */
336             uint32_t        QpAdjustment        : 1;
337             /*! \brief Indicates ROI[] value is in delta QP.
338             *
339             *    It is valid only when parameter NumROI is greater than 0 and either ROIDeltaQPSupport or ROIBRCPriorityLevelSupport equals to 1.
340             *        \n - 0 : ROI[] value is in priority.
341             *        \n - 1 : ROI[] value is in delta QP.
342             *        \n Note: ROIValueInDeltaQP must be set to 1 for CQP. Currently only ROIValueInDeltaQP equal 1 is validated.
343             */
344             uint32_t        ROIValueInDeltaQP        : 1;
345             /*! \brief Indicates block level absolute QP value is provided.
346             *
347             *        \n - 0 : block level absolute QP value is not provided.
348             *        \n - 1 : block level absolute QP value is provided.
349             */
350             uint32_t        BlockQPforNonRectROI     : 1;
351             /*! \brief Enables tile based encoding.
352             *
353             *        \n - 0 : tile based encoding disabled.
354             *        \n - 1 : tile based encoding enabled.
355             */
356             uint32_t        EnableTileBasedEncode    : 1;
357             /*! \brief Indicates if BRC can use larger P/B frame size than UserMaxPBFrameSize
358             *
359             *        \n - 0 : BRC can not use larger P/B frame size  than UserMaxPBFrameSize.
360             *        \n - 1 : BRC can use larger P/B frame size  than UserMaxPBFrameSize.
361             */
362             uint32_t        bAutoMaxPBFrameSizeForSceneChange : 1;
363             /*! \brief Enables streaming buffer in LLC
364             *
365             *        \n - 0 : streaming buffer by LLC is disabled.
366             *        \n - 1 : streaming buffer by LLC is enabled.
367             */
368             uint32_t        EnableStreamingBufferLLC : 1;
369             /*! \brief Enables streaming buffer in DDR
370             *
371             *        \n - 0 : streaming buffer by DDR is disabled.
372             *        \n - 1 : streaming buffer by DDR is enabled.
373             */
374             uint32_t        EnableStreamingBufferDDR : 1;
375 
376             /*! \brief Low Delay Mode
377             *
378             *        \n - 0 : Random Access B.
379             *        \n - 1 : Low delay encoding with P or LDB.
380             */
381             uint32_t        LowDelayMode            : 1;
382 
383             /*! \brief Disable HRD conformance
384             *
385             *        \n - 0 : HRD conformance is enabled.
386             *        \n - 1 : HRD conformance is disabled (aka no panic mode).
387             */
388             uint32_t        DisableHRDConformance    : 1;
389 
390             /*! \brief Hierarchical Mini GOP
391             *
392             *        \n - 0 : Flat GOP (No Hierarchical Mini GOP).
393             *        \n - 1 : Hierarchical Mini GOP.
394             */
395             uint32_t        HierarchicalFlag         : 1;
396 
397             /*! \brief Indicates if TCBRC is enabled.
398             *
399             *        \n - 0 : no TCBRC.
400             *        \n - 1 : enable TCBRC if TCBRCSupport in CAP is 1.
401             */
402             uint32_t        TCBRCEnable              : 1;
403 
404             /*! \brief Indicates if current encodin gis lookahead pass.
405             *
406             *        \n - 0 : the current encoding is in the actual encoding pass, and one of the BRC modes (CBR, VBR, etc.) should be selected.
407             *        \n - 1 : the current encoding is in the lookahead pass.
408             *    \n Valid only when LookAheadAnalysisSupport in CAP is on and LookAheadDepth > 0.
409             */
410             uint32_t        bLookAheadPhase          : 1;
411 
412             uint32_t        ReservedBits             : 1;
413         };
414         uint32_t    SeqFlags;
415     };
416 
417     /*! \brief Framework defined maximum frame size in bytes for I frames.
418     *
419     *    Applicable for all RateControlMethod values except CQP; guarantees that the compressed frame size will be less than this value. If UserMaxPBFrameSize equals 0, UserMaxIFrameSize will be used for all frame types. Maximum allowed frame size per profile/level will be calculated in driver and be used when UserMaxIFrameSize and UserMaxPBFrameSize are both set to 0.
420     */
421     uint32_t            UserMaxIFrameSize;
422     /*! \brief Framework defined maximum frame size in bytes for P & B frames.
423     *
424     *    Applicable for all RateControlMethod values except CQP; guarantees that the compressed frame size will be less than this value. If UserMaxPBFrameSize equals 0, UserMaxIFrameSize will be used for all frame types. Maximum allowed frame size per profile/level will be calculated in driver and be used when UserMaxIFrameSize and UserMaxPBFrameSize are both set to 0.
425     */
426     uint32_t            UserMaxPBFrameSize;
427     /*! \brief For Constant Rate Factor BRC method, it indicates the measure of quality.
428     *
429     *    The range is from 1 – 51, with 1 being the best quality.
430     */
431     uint8_t   ICQQualityFactor;
432 
433         /*! \brief Specigy session that IPU and GPU communicate on.
434     *
435     *    It is for streaming buffer.
436     */
437     uint8_t StreamBufferSessionID;
438 
439     uint8_t maxAdaptiveMiniGopSize;
440 
441     union
442     {
443         struct
444         {
445             uint8_t ClosedGop     : 1; // [0..1]
446             uint8_t StrictGop     : 1; // [0..1]
447             uint8_t AdaptiveGop   : 1; // [0..1]
448             uint8_t ReservedBits  : 5; // [0]
449         } fields;
450         uint8_t value;
451     } GopFlags;
452 
453     /*! \brief Number of B frames per level in BGOP (between each two consecutive anchor frames).
454     *
455     *   \n NumOfBInGop[0] – regular B, or no reference to other B frames.
456     *   \n NumOfBInGop[1] – B1, reference to only I, P or regular B frames.
457     *   \n NumOfBInGop[2] – B2, references include B1.
458     *   \n Invalid when ParallelBRC is disabled (value 0).
459     */
460     uint32_t NumOfBInGop[3];  // depricated
461 
462     union
463     {
464         struct
465         {
466             /*! \brief Same as syntax element.
467             *
468             *    When the scaling_list_enable_flag is set to disable, the scaling matrix is still sent to the PAK, and with all entries programmed to the same value of 16.
469             */
470             uint32_t    scaling_list_enable_flag            : 1;
471             uint32_t    sps_temporal_mvp_enable_flag        : 1;    //!< Same as HEVC syntax element
472             uint32_t    strong_intra_smoothing_enable_flag  : 1;    //!< Same as HEVC syntax element
473             uint32_t    amp_enabled_flag                    : 1;    //!< Same as HEVC syntax element
474             /*! \brief Same as syntax element sample_adaptive_offset_enabled_flag.
475             *
476             *    Programming notes: must be 0 on SKL
477             */
478             uint32_t    SAO_enabled_flag : 1;
479             /*! \brief Same as syntax element.
480             *
481             *    Programming note: must be 0 on SKL.
482             */
483             uint32_t    pcm_enabled_flag                    : 1;
484             uint32_t    pcm_loop_filter_disable_flag        : 1;    //!< Same as HEVC syntax element
485             uint32_t    reserved                            : 1;
486             uint32_t    chroma_format_idc                   : 2;    //!< Same as HEVC syntax element
487             uint32_t    separate_colour_plane_flag          : 1;    //!< Same as HEVC syntax element
488             uint32_t    palette_mode_enabled_flag           : 1;
489             uint32_t    RGBEncodingEnable                   : 1;
490             uint32_t    PrimaryChannelForRGBEncoding        : 2;
491             uint32_t    SecondaryChannelForRGBEncoding      : 2;
492             uint32_t    RGBInputStudioRange                 : 1;
493             uint32_t    ConvertedYUVStudioRange             : 1;
494             uint32_t                                        : 13;  // [0]
495         };
496         uint32_t    EncodeTools;
497     };
498 
499     /*! \brief Specifies the maximum size of a coding block
500     *
501     *   Corresponds to HEVC spec variable Log2CtbSize - 3. The full value range is [0..3], inclusive.
502     *   Programming note: On SKL, must be set at 2.
503     */
504     uint8_t    log2_max_coding_block_size_minus3;
505     /*! \brief Specifies the minimum size of a coding block.
506     *
507     *   Corresponds to HEVC spec variable Log2MinCbSize - 3. The full value range is [0..3], inclusive.
508     *   Programming note: On SKL, must be set at 0.
509     */
510     uint8_t    log2_min_coding_block_size_minus3;
511     /*! \brief Specifies the maximum size of a transform unit.
512     *
513     *   Corresponds to HEVC spec variable Log2MaxTrafoSize - 2. The value range is [0..3], inclusive.
514     *   Programming note: On SKL, must be set to 3 to indicate 32x32.
515     */
516     uint8_t    log2_max_transform_block_size_minus2;
517     /*! \brief Specifies the minimum size of a transform unit.
518     *
519     *   Corresponds to HEVC spec variable Log2MinTrafoSize - 2. The value range is [0..3], inclusive.
520     *   Programming note: On SKL, must be set to 0 to indicate 4x4.
521     */
522     uint8_t    log2_min_transform_block_size_minus2;
523     /*! \brief Same as HEVC syntax element.
524     *
525     *    Programming note: On SKL, must be set to 2.
526     */
527     uint8_t    max_transform_hierarchy_depth_intra;
528     /*! \brief Same as HEVC syntax element.
529     *
530     *    Programming note: On SKL, must be set to 2.
531     */
532     uint8_t    max_transform_hierarchy_depth_inter;
533     /*! \brief Specifies the minimum size of I_PCM coding blocks.
534     *
535     *    Corresponds to HEVC spec variable Log2MinIpcmCbSizeY.
536     */
537     uint8_t    log2_min_PCM_cb_size_minus3;
538     /*! \brief Specifies the maximum size of I_PCM coding blocks.
539     *
540     *   Corresponds to HEVC spec variable Log2MaxIpcmCbSizeY.
541     */
542     uint8_t    log2_max_PCM_cb_size_minus3;
543     uint8_t    bit_depth_luma_minus8;                 //!< Same as HEVC syntax element
544     uint8_t    bit_depth_chroma_minus8;               //!< Same as HEVC syntax element
545     uint8_t    pcm_sample_bit_depth_luma_minus1;      //!< Same as HEVC syntax element
546     uint8_t    pcm_sample_bit_depth_chroma_minus1;    //!< Same as HEVC syntax element
547 
548     uint8_t    bVideoSurveillance;
549 
550     /*! \brief This flag is used for compatibility between various DDIs.
551     *
552     *    Do NOT proram a kernel or otherwise make decisions based on this value.  Instead use the flag inside CODEC_HEVC_ENCODE_PICTURE_PARAMS.
553     */
554     uint8_t    bScreenContent;
555 
556     /*! \brief Indicates the uncompressed input color space
557     *
558     *    Valid only when input is ARGB format.
559     */
560     ENCODE_INPUT_COLORSPACE     InputColorSpace;
561 
562     /*! \brief Provides a hint to encoder about the scenario for the encoding session.
563     *
564     *   BRC algorithm may tune differently based on this info.
565     */
566     ENCODE_SCENARIO             ScenarioInfo;
567 
568     ENCODE_CONTENT              contentInfo;
569 
570     /*! \brief Indicates the tolerance the application has to variations in the frame size.
571     *
572     *   It affects the BRC algorithm used, but may or may not have an effect based on the combination of other BRC parameters.  Only valid when the driver reports support for FrameSizeToleranceSupport.
573     */
574     ENCODE_FRAMESIZE_TOLERANCE  FrameSizeTolerance;
575 
576 
577     uint16_t                   SlidingWindowSize;
578     uint32_t                   MaxBitRatePerSlidingWindow;
579     uint32_t                   MinBitRatePerSlidingWindow;
580 
581     /*! \brief Indicates number of frames to lookahead.
582     *
583     *    Range is [0~127]. Default is 0 which means lookahead disabled. Valid only when LookaheadBRCSupport is 1. When not 0, application should send LOOKAHEADDATA to driver.
584     */
585     uint8_t                   LookaheadDepth;
586 
587     /*! \brief Indicates minimal and maximal IDR distances used for adaptive GOP decision.
588     *
589     *    Applicable for LookAhead phase only.
590     */
591     uint16_t                  MinAdaptiveGopPicSize;
592     uint16_t                  MaxAdaptiveGopPicSize;
593 
594     uint32_t motion_vector_resolution_control_idc;
595     uint32_t intra_boundary_filtering_disabled_flag;
596     uint8_t     palette_max_size;
597     uint8_t     delta_palette_max_predictor_size;
598     uint8_t     FullPassCodecType;  // [0..4]
599 } CODEC_HEVC_ENCODE_SEQUENCE_PARAMS, *PCODEC_HEVC_ENCODE_SEQUENCE_PARAMS;
600 
601 /*! \brief Provides the picture-level parameters of a compressed picture for HEVC decoding.
602 */
603 typedef struct _CODEC_HEVC_ENCODE_PICTURE_PARAMS
604 {
605     /*! \brief Specifies the uncompressed source surface of the frame for the current picture to be encode.
606     *
607     *    The PicFlags regarding reference usage are expected to be valid at this time.
608     */
609     CODEC_PICTURE           CurrOriginalPic;
610     /*! \brief Specifies the uncompressed surface of the reconstructed frame for the current encoded picture.
611     *
612     *    The PicFlags regarding reference usage are expected to be valid at this time.
613     *    The recon surface may be of different format and different bit depth from that of source.
614     *    The framework needs to specify it through chroma_format_idc and bit_depth_luma_minus8
615     *    and bit_depth_chroma_minus8 in SPS data structure.
616     */
617     CODEC_PICTURE           CurrReconstructedPic;
618     /*! \brief Specifies the collocated reference picture’s index into the RefFrameList[].
619     *
620     *    Application should generate collocated_ref_idx from collocated_from_l0_flag value per slice. When  the HEVC syntax element slice_temporal_mvp_enable_flag takes value 0, CollocatedRefPicIndex should take value 0xFF.
621     */
622     uint8_t                 CollocatedRefPicIndex;
623     /*! \brief Each entry of the list specifies the frame index of the reference pictures.
624     *
625     *   The value of FrameIdx specifies the index of RefFrameList structure. And valid value range is [0..14, 0x7F]. Invalid entries are indicated by setting PicFlags to PICTURE_INVALID.
626     *   RefFrameList[] should include all the reference pictures in DPB, which means either the picture is referred by current picture or future pictures, it should have a valid entry in it.
627     */
628     CODEC_PICTURE           RefFrameList[CODEC_MAX_NUM_REF_FRAME_HEVC];
629     int32_t                 CurrPicOrderCnt;                            //!< Picture Order Count value of current picture.
630     /*! \brief Picture Order Count values of reference pictures corresponding to the entries of RefFrameList[].
631     *
632     *    For invalid entries of RefFrameList[], its RefFramePOCList value can be ignored.
633     */
634     int32_t                 RefFramePOCList[CODEC_MAX_NUM_REF_FRAME_HEVC];
635 
636     /*! \brief Specifies picture coding type.
637     *
638     *    \n 1: I picture
639     *    \n 2: P picture
640     *    \n 3: B picture
641     *    \n 4: B1 picutre
642     *    \n 5: B2 picture
643     *    \n For B1 and B2 explanation refer to NumOfBInGop[]
644     */
645     uint8_t                 CodingType;
646 
647     /*! \brief Specifies picture coding type.
648     *
649     *   Store pic coding type specified through DDI to retain pyramid level information. CodingType is updated to I/P/B when parsing slice-level parameters
650     */
651     uint8_t                 ppsCodingType;
652 
653     uint8_t                 HierarchLevelPlus1;
654     uint16_t                NumSlices;
655 
656     union
657     {
658         struct
659         {
660             uint32_t            tiles_enabled_flag                  : 1;    //!< Same as HEVC syntax element
661             uint32_t            entropy_coding_sync_enabled_flag    : 1;    //!< Same as HEVC syntax element
662             /*! \brief Same as HEVC syntax element.
663             *
664             *    Programming note: On SKL, must be set to 0.
665             */
666             uint32_t            sign_data_hiding_flag               : 1;
667             /*! \brief Same as HEVC syntax element.
668             *
669             *    Programming note: On SKL, must be set to 0.
670             */
671             uint32_t            constrained_intra_pred_flag         : 1;
672             /*! \brief Same as HEVC syntax element.
673             *
674             *    Programming note: On SKL, must be set to 0.
675             */
676             uint32_t            transform_skip_enabled_flag         : 1;
677             uint32_t            transquant_bypass_enabled_flag      : 1;    //!< Same as HEVC syntax element
678             uint32_t            cu_qp_delta_enabled_flag            : 1;    //!< Same as HEVC syntax element
679             uint32_t            weighted_pred_flag                  : 1;    //!< Same as HEVC syntax element
680             uint32_t            weighted_bipred_flag                : 1;    //!< Same as HEVC syntax element
681             uint32_t            loop_filter_across_slices_flag      : 1;    //!< Same as HEVC syntax element of seq_loop_filter_across_slices_enabled_flag
682             uint32_t            loop_filter_across_tiles_flag       : 1;    //!< Same as HEVC syntax element
683             /*! \brief A combination of HEVC syntax element of sps_scaling_list_data_present_flag and pps_scaling_list_data_present_flag.
684             *
685             *    If the value is 1, application will send a quantization matrix (scaling list) buffer to DDI. Otherwise not. If scaling_list_enable_flag is 0. This flag should also be 0.
686             */
687             uint32_t            scaling_list_data_present_flag      : 1;
688             uint32_t            dependent_slice_segments_enabled_flag : 1;  //!< Same as HEVC syntax element
689             uint32_t            bLastPicInSeq                       : 1;
690             uint32_t            bLastPicInStream                    : 1;
691             uint32_t            bUseRawPicForRef                    : 1;
692             uint32_t            bEmulationByteInsertion             : 1;
693             uint32_t            BRCPrecision                        : 2;
694             /*! \brief Specifies that slice level reporting may be requested for this frame.
695             *
696             *    If this flag is set, then slice level parameter reporting will be set up for this frame. Only valid if SliceLevelReportSupport is reported as a capability, else this flag is ignored.
697             */
698             uint32_t            bEnableSliceLevelReport             : 1;
699             /*! \brief Specifies whether intra refresh is enabled in colum/row, or disabled.
700             *
701             *    It applies to P picture only (not valid with IBP) and is effective only when RollingIntraRefresh capability is set..
702             *        \n - 0: disabled
703             *        \n - 1: enabled in colum
704             *        \n - 2: enabled in row
705             *    \n Programming Note: When RollingIntraRefresh is used, there are some restrictions the application must obey.
706             *        \n - No B frames.
707             *        \n - No multiple references.
708             *    \n Additionally the driver will disable Multiple Prediction (MultiPred).  This is to simplify the kernel when ensuring inter serach doesn’t refer to illegal regions.
709             */
710             uint32_t            bEnableRollingIntraRefresh          : 2;
711             /*! \brief Same as HEVC syntax element
712             *
713             */
714             uint32_t            no_output_of_prior_pics_flag        : 1;
715             /*! \brief Requests GPU to determine weighted prediction factors.
716             *
717             *    Is valid when either weighted_pred_flag or weighted_bipred_flag is turned on.
718             *    In this case, the following parameters in slice control data structure shall be ignored: luma_log2_weight_denom, delta_chroma_log2_weight_denom, luma_offset[2][15], delta_luma_weight[2][15], chroma_offset[2][15][2], and delta_chroma_weight[2][15][2].
719             */
720             uint32_t            bEnableGPUWeightedPrediction        : 1;
721             /*! \brief Indicates that the allocated source buffer format is a swizzle format from display.
722             *
723             *    Framework still allocates the buffer as a standard FOURCC format. The swizzled format will be reported as supported for the encoder configuration during capability reporting.
724             *    \n Source/Raw Allocated Buffer Format, DisplayFormatSwizzle, Layout Format in Buffer
725             *        \n - YUY2, 0, YUY2
726             *        \n - YUY2, 1, 2YUY (Y0U0Y1V0)
727             *        \n - AYUV, 0, AYUV
728             *        \n - AYUV, 1, YUXV
729             */
730             uint32_t            bDisplayFormatSwizzle                   : 1;
731             uint32_t            deblocking_filter_override_enabled_flag : 1;
732             uint32_t            pps_deblocking_filter_disabled_flag     : 1;
733             uint32_t            bEnableCTULevelReport                   : 1;  // [0..1]
734             uint32_t            bEnablePartialFrameUpdate               : 1;
735             uint32_t            reservedbits                            : 3;
736         };
737         uint32_t                PicFlags;
738     };
739 
740     char                    QpY;                                    //!< QpY = pic_init_qp_minus26 + 26
741     uint8_t                 diff_cu_qp_delta_depth;                 //!< Same as HEVC syntax element
742     char                    pps_cb_qp_offset;                       //!< Same as HEVC syntax element
743     char                    pps_cr_qp_offset;                       //!< Same as HEVC syntax element
744     uint8_t                 num_tile_columns_minus1;                //!< Same as HEVC syntax element
745     uint8_t                 num_tile_rows_minus1;                   //!< Same as HEVC syntax element
746     /*! \brief Same as HEVC syntax elements of column_width_minus1 + 1 in PPS of HEVC bitstreams.
747     *
748     *    Driver expected to calculate the size of last column from frame resolution.
749     */
750     uint16_t                tile_column_width[20];
751     /*! \brief Same as HEVC syntax elements of row_height_minus1 + 1 in PPS of HEVC bitstreams.
752     *
753     *    Driver expected to calculate the size of last row from frame resolution.
754     */
755     uint16_t                tile_row_height[22];
756     /*! \brief Same as HEVC syntax element.
757     *
758     *    Programming note: On SKL, must be set to 0.
759     */
760     uint8_t                 log2_parallel_merge_level_minus2;
761     uint8_t                 num_ref_idx_l0_default_active_minus1;   //!< Same as HEVC syntax element
762     uint8_t                 num_ref_idx_l1_default_active_minus1;   //!< Same as HEVC syntax element
763     /*! \brief Specifies the CTU bit size limit based on spec requirement, or other value for special purpose.
764     *
765     *    If the value is set 0, no bit size limit is checked.
766     */
767     uint32_t                LcuMaxBitsizeAllowed;
768     /*! \brief Indicates the column or row location in block unit which is dictated by IntraRefreshBlockUnitSize from encoding capability.
769     *
770     *    Ignored if bEnableRollingIntraRefresh is 0.
771     *    If IntraInsertionSize is equal or larger than the LCU size (CTB), IntraInsertionLocation has to be aligned with LCU boundaries. Otherwise, IntraInsertionLocation could have an offset of integer multiple of the block unit size from LCU boundary and must make sure that IntraInsertionLocation + IntraInsertionSize -1 would reside in the same LCU region.
772     */
773     uint16_t                IntraInsertionLocation;
774     /*! \brief Indicates the number of columns or rows in block unit which is dictated by IntraRefreshBlockUnitSize from encoding capability.
775     *
776     *    Ignored if bEnableRollingIntraRefresh is 0.
777     *    If IntraInsertionSize is equal or larger than the LCU size (CTB), it has to be multiple of LCU size. Otherwise, it can only be integer multiple of the block unit size (equal or smaller than LCU).
778     *    When RateControlMode takes VCM mode. IntraInsertionSize defines the number of LCUs to be turn into Intra LCUs when bEnableRollingIntraRefresh is 1.
779     */
780     uint16_t                IntraInsertionSize;
781     /*! \brief Indicates the Qp difference for inserted intra columns or rows.
782     *
783     *    Framework can use this to adjust intra Qp based on bitrate & max frame size. It is ignored if bEnableRollingIntraRefresh is 0. Value range [-8..7]. Driver will clamp it if out of range.
784     */
785     char                    QpDeltaForInsertedIntra;
786     /*! \brief Arbitrary number set by the host decoder to use as a tag in the status report feedback data.
787     *
788     *   The value should not equal 0, and should be different in each call to Execute.
789     */
790     uint32_t                StatusReportFeedbackNumber;
791 
792     /*! \brief Same as HEVC syntax element
793     *
794     */
795     uint8_t                 slice_pic_parameter_set_id;
796     /*! \brief Same as HEVC syntax element
797     *
798     */
799     uint8_t                 nal_unit_type;
800     bool                    bUsedAsRef;
801 
802     /*! \brief Slice byte size upper limit.
803     *
804     *    Used when SliceSizeControl is enabled. Currently only valid for VDENC.
805     */
806     uint32_t                MaxSliceSizeInBytes;
807 
808     /*! \brief Number of Region Of Interest (ROI).
809     *
810     *    Value range is 0 to 16 inclusively. If no ROI to be set, this value shall be set to 0.
811     */
812     uint8_t                 NumROI;
813     /*! \brief Defines ROI settings.
814     *
815     *    Value entries are ROI[0] up to ROI[NumROI – 1], inclusively, if NumROI > 0. And it can be ignored otherwise.
816     */
817     CODEC_ROI               ROI[CODECHAL_ENCODE_HEVC_MAX_NUM_ROI];
818     /*! \brief Distinct delta QP values assigned to the ROI
819     *
820     *    Value entries are distinct and within [MinDeltaQp..MaxDeltaQp].
821     */
822     int8_t                  ROIDistinctDeltaQp[CODECHAL_ENCODE_HEVC_MAX_NUM_ROI];
823     uint32_t                RollingIntraReferenceLocation[16];
824     /*! \brief Dictates the value of delta QP for any ROI should be within [MinDeltaQp..MaxDeltaQp]
825     *
826     *    Applies only to BRC case.
827     */
828     char                    MaxDeltaQp;
829     /*! \brief Dictates the value of delta QP for any ROI should be within [MinDeltaQp..MaxDeltaQp]
830     *
831     *    Applies only to BRC case.
832     */
833     char                    MinDeltaQp;
834 
835     union
836     {
837         struct
838         {
839             uint32_t        EnableCustomRoudingIntra : 1;
840             uint32_t        RoundingOffsetIntra : 7;
841             uint32_t        EnableCustomRoudingInter : 1;
842             uint32_t        RoundingOffsetInter : 7;
843             uint32_t        reservedbits : 16;
844         } fields;
845 
846         uint32_t            value;
847     } CustomRoundingOffsetsParams;
848 
849     /*! \brief Specifies skip frames.
850     *
851     *    0: Encode as normal, no skip.
852     *    1: One or more frames were skipped prior to the current frame, encode the current frame as normal.  The driver will pass the NumSkipFrames and SizeSkipFrames to BRC for adjustment.
853     */
854     uint8_t                 SkipFrameFlag;
855     /*! \brief The number of frames skipped prior to the current frame.
856     *
857     *    Valid when SkipFlag = 1.
858     */
859     uint8_t                 NumSkipFrames;
860     /*! \brief Differs in meaning based on value of SkipFrameFlag
861     *
862     *    SkipFrameFlag = 1, the size of the skipped frames in bits.
863     *    Note: Currently kernel only supports 16 bits for SizeSkipFrames.
864     */
865     uint32_t                SizeSkipFrames;
866 
867     uint8_t                 BRCMaxQp; //!< Specifies the maximum Qp to be used for BRC.
868     /*! \brief Specifies the minimum Qp to be used for BRC.
869     *
870     *    BRCMaxQp and BRCMinQp may be set at a per frame type (I, P, B) granularity.
871     *    BRCMaxQp and BRCMinQp should be set to zero if Qp control is not desired.
872     *    If non-zero min/max QP is passed for I-frame, it will be used for I, P and B frames.
873     *    If non-zero min/max QP is passed for P-frame also, then P and B frame will be updated to this. I-frame remains unchanged.
874     *    If non-zero min/max QP is passed for B-frame also, then B-frame will be updated to this. I and P frame remains unchanged.
875     *    If new QP values are passed in between the sequence, then it will be updated for that frame-type and any other frame types that are not explicitly set. Eg. if min/max QP for P-frame was passed separately, and an update for I-frame is received, then only I-frame values are updated.  P and B will continue to use old values.  But, if P-frame and B-frame were never explicitly set then updating I-frame will also update P and B.
876     *    If the application wants to keep the current non-zero min/max QP settings, then min/max QP should be set to zero, so the driver will not change previously set values and continue to use them.
877     *    Min QP is expected to be less than or equal to Max QP.  Driver clamps max QP to [1, 51] and min QP to [1, max QP].  Zero QP is not used.
878     *    Only single PAK pass is supported plus the IPCM pass.  Panic mode is disabled.  This is because min/maxQP requirement conflicts with the HRD compliancy requirement, so the HRD compliancy restriction is relaxed.
879     */
880     uint8_t                 BRCMinQp;
881 
882     /*! \brief Indicates that an HMEOffset will be sent by the application in HMEOffset for each reference.
883     *
884     *    This offset will be added to the co-located (0, 0) location before HME search is performed.  This is only valid if HMEOffsetSupport is reported as supported as a capability, else this flag is ignored.
885     */
886     uint8_t                 bEnableHMEOffset;
887     /*! \brief Specifies the HME offsets.
888     *
889     *    Curently the supported range is -128 to +127, programmed in 4x downscaled HME precision, not the original size. HMEOffset[i][j] where:
890     *        \n - i – RefFrameList Index
891     *        \n - j – 0 = x, 1 = y Coordinates
892     *    \n So for example, HMEOffset[i] specifies the (x, y) offset corresponding to the ith entry in RefFrameList.
893     *    Programming Note: The HME offset must be a multiple of 4x4 to align to the 4x4 HME block, so the driver must align the application supplied value.
894     */
895     int16_t                 HMEOffset[15][2];
896 
897     /*! \brief Number of dirty rectangles.
898     *
899     *    If no dirty rectangle to be set, this value shall be set to 0. Encoder does not have number of dirty rectangle limitation.
900     */
901     uint8_t                 NumDirtyRects;
902     /*! \brief Buffer containing an array of NumDirtyRects number of dirty rectangle elements
903     *
904     *    It’s framework's responsibility to ensure that the content in non-dirty rectangle region is the same as previous frame.
905     */
906     PCODEC_ROI              pDirtyRect;
907     /*! \brief Number of moving rectangles.
908     *
909     *    If no moving rectangle to be set, this value shall be set to 0.
910     */
911     uint8_t                 NumMoveRects;
912     //MOVE_RECT             *pMoveRect;     \\!< Buffer containing an array of NumMoveRects number of moving rectangle elements.
913 
914     /*! \brief Provides a hint to encoder about the content for the encoding session.
915     *
916     *   Hint is of the format CODEC_CONTENT.
917     */
918     uint32_t                bScreenContent;
919 
920     uint16_t                LcuMaxBitsizeAllowedHigh16b;
921     uint32_t                TargetFrameSize;
922 
923     /*! \brief Picture parameter, Same as syntax element.
924     *
925     */
926     uint32_t                pps_curr_pic_ref_enabled_flag;
927     uint32_t                residual_adaptive_colour_transform_enabled_flag;
928     uint32_t                pps_slice_act_qp_offsets_present_flag;
929     uint8_t                 PredictorPaletteSize;
930     uint16_t                PredictorPaletteEntries[3][128];
931     char                    pps_act_y_qp_offset_plus5;
932     char                    pps_act_cb_qp_offset_plus5;
933     char                    pps_act_cr_qp_offset_plus3;
934 
935     /*! \brief Source down scaling ratio for look ahead pass.
936     *
937     *    when bLookAheadPhase == 1, this parameter indicates the source down scaling ratio for look ahead pass. Otherwise, the parameter should be ignored.
938     *    (X16Minus1_X + 1) is the numerator of the horizontal downscaling ratio over 16.
939     *    (X16Minus1_Y + 1) is the numerator of the vertical downscaling ratio over 16.
940     */
941     union
942     {
943         struct
944         {
945             uint8_t X16Minus1_X : 4;
946             uint8_t X16Minus1_Y : 4;
947         } fields;
948         uint8_t value;
949     } DownScaleRatio;
950 
951     uint8_t QpModulationStrength;
952 
953     /*! \brief StatusReportEnable
954     *
955     *  Request features to be enabled at status report.
956     *  FrameStats: FRAME_STATS_INFO enabled in ENCODE_QUERY_STATUS_PARAMS.
957     *  BlockStats: BLOCK_STATS_INFO enabled in ENCODE_QUERY_STATUS_PARAMS.
958     */
959     union
960     {
961         struct
962         {
963             uint16_t FrameStats : 1;
964             uint16_t BlockStats : 1;
965             uint16_t reserved : 14;
966         } fields;
967         uint16_t value;
968     } StatusReportEnable;
969 
970     /*! \brief quality information report enable flags.
971     */
972     union
973     {
974         struct
975         {
976             uint8_t enable_frame : 1;
977             uint8_t reserved : 7;
978         } fields;
979         uint8_t value;
980     } QualityInfoSupportFlags;
981 } CODEC_HEVC_ENCODE_PICTURE_PARAMS, *PCODEC_HEVC_ENCODE_PICTURE_PARAMS;
982 
983 /*! \brief Slice-level parameters of a compressed picture for HEVC encoding.
984 */
985 typedef struct _CODEC_HEVC_ENCODE_SLICE_PARAMS
986 {
987     /*! \brief Same as HEVC syntax element.
988     *
989     *    For first slice in the picture, slice_segment_address shall be set to 0.
990     */
991     uint32_t            slice_segment_address;
992     uint32_t            NumLCUsInSlice;                                 //!< Specifies the number of LCUs in the current slice.
993     /*! \brief Specifies the surfaces of reference pictures.
994     *
995     *    The value of FrameIdx specifies the index of RefFrameList structure, PicFlags has no meaning.
996     *    RefPicIdx[0][15] corresponds to reference list 0 and RefPicIdx[1][15] corresponds to reference list 1.
997     *    Each list may contain duplicated reference picture indexes. Same as RefFrameList[] from picture parameter data structure, the bPicEntry of invalid entries should take value 0xFF.
998     *    Must be same across all slices.
999     */
1000     CODEC_PICTURE       RefPicList[2][CODEC_MAX_NUM_REF_FRAME_HEVC];
1001     /*! \brief Same as HEVC syntax element.
1002     *
1003     *    If num_ref_idx_active_override_flag == 0, host encoder shall set their values with num_ref_idx_l0_default_minus1.
1004     *    Must be same across all slices.
1005     */
1006     uint8_t             num_ref_idx_l0_active_minus1;
1007     /*! \brief Same as HEVC syntax element.
1008     *
1009     *    If num_ref_idx_active_override_flag == 0, host encoder shall set their values with num_ref_idx_l1_default_minus1.
1010     *    Must be same across all slices.
1011     */
1012     uint8_t             num_ref_idx_l1_active_minus1;
1013 
1014    union
1015     {
1016         struct
1017         {
1018             uint32_t        bLastSliceOfPic                         : 1;    //!< Specifies if current slice is the last slice of picture.
1019             uint32_t        dependent_slice_segment_flag            : 1;    //!< Same as HEVC syntax element
1020             uint32_t        slice_temporal_mvp_enable_flag          : 1;    //!< Same as HEVC syntax element
1021             /*! \brief Same as HEVC syntax element.
1022             *
1023             *    HEVC has three slice types, B, P, and I slices, and their corresponding values are 0, 1, and 2.
1024             */
1025             uint32_t        slice_type                              : 2;
1026             uint32_t        slice_sao_luma_flag                     : 1;    //!< Same as HEVC syntax element
1027             uint32_t        slice_sao_chroma_flag                   : 1;    //!< Same as HEVC syntax element
1028             /*! \brief Same as HEVC syntax element.
1029             *
1030             *    Programming note: On SKL, must be 0.
1031             */
1032             uint32_t        mvd_l1_zero_flag                        : 1;
1033             uint32_t        cabac_init_flag                         : 1;    //!< Same as HEVC syntax element
1034             /*! \brief Same as HEVC syntax element.
1035             *
1036             *    Affects the decoding process accordingly if it appears in slice header of the bitstream. Otherwise, host encode should set it to be the value of pic_disable_deblocking_filter_flag in picture parameter set bit stream.
1037             */
1038             uint32_t        slice_deblocking_filter_disable_flag    : 1;
1039             uint32_t        collocated_from_l0_flag                 : 1;    //!< Same as HEVC syntax element
1040             uint32_t                                                : 21;
1041         };
1042         uint32_t        SliceFlags;
1043     };
1044 
1045     /*! \brief Same as HEVC syntax element.
1046     *
1047     *    Must be the same across all slices.
1048     */
1049     char                slice_qp_delta;
1050     char                slice_cb_qp_offset;             //!< Same as HEVC syntax element, [-12..12]
1051     char                slice_cr_qp_offset;             //!< Same as HEVC syntax element, [-12..12]
1052     char                beta_offset_div2;               //!< Same as HEVC syntax element
1053     char                tc_offset_div2;                 //!< Same as HEVC syntax element
1054     /*! \brief Same as HEVC syntax element.
1055     *
1056     *    Specifies the base 2 logarithm of the denominator for all luma weighting factors. Value range: 0 to 7, inclusive.
1057     */
1058     uint8_t             luma_log2_weight_denom;
1059     /*! \brief Same as HEVC syntax element.
1060     *
1061     *    Framework is expected to ensure that the spec variable ChromaLog2WeightDenom, which is luma_log2_weight_denom plus delta_chroma_log2_weight_denom, specifies the base 2 logarithm of the denominator for all chroma weighting factors, with value range: 0 to 7, inclusive.
1062     */
1063     char                delta_chroma_log2_weight_denom;
1064     /*! \brief Specifies the additive offsets applied to the luma prediction values for list 0 and list 1.
1065     *
1066     *    Its values correspond to HEVC syntax elements luma_offset_l0, and luma_offset_l1. In luma_offset[i][j]:
1067     *        \n - i equal to 0 is for reference picture list 0, equal to 1 is for reference picture list 1.
1068     *        \n - j is for reference list index [0..14].
1069     *    \n Value range: [-128..127], inclusive. When luma_weight_l0_flag or luma_weight_l1_flag take value 0, corresponding luma_offset should be set 0.
1070     */
1071     char                luma_offset[2][CODEC_MAX_NUM_REF_FRAME_HEVC];
1072     /*! \brief Specifies the difference of the weighting factor applied to the luma prediction value for list 0/1 prediction using RefPicList.
1073     *
1074     *    Corresponds to syntax element delta_luma_weight_l0, and delta_luma_weight_l1. In delta_luma_weight[i][j]:
1075     *        \n - i equal to 0 is for reference picture list 0, equal to 1 is for reference picture list 1.
1076     *        \n - j is for reference list index [0..14].
1077     *    \n Value range: [-127..128], inclusive. When luma_weight_l0_flag or luma_weight_l1_flag take value 0, corresponding delta_luma_weight should be set 0.
1078     */
1079     char                delta_luma_weight[2][CODEC_MAX_NUM_REF_FRAME_HEVC];
1080     /*! \brief Corresponds to HEVC spec variable ChromaOffsetL0 and ChromaOffsetL1.
1081     *
1082     *    In chroma_offset[i][j][k]:
1083     *        \n - i equal to 0 is for reference picture list 0, equal to 1 is for reference picture list 1.
1084     *        \n - j is for reference list index [0..14].
1085     *        \n - k  equal to 0 is for Cb, and equal to 1 is for Cr.
1086     *    \n Value range: [-128..127], inclusive. When chroma_weight_l0_flag or chroma_weight_l1_flag take value 0, corresponding chroma_offset should be set 0.
1087     */
1088     char                chroma_offset[2][CODEC_MAX_NUM_REF_FRAME_HEVC][2];
1089     /*! \brief Specifies the difference of the weighting factor applied to the chroma prediction values for list 0/1 prediction using RefPicList.
1090     *
1091     *    Corresponds to HEVC syntax elements delta_chroma_weight_l0 and delta_chroma_weight_l1. In delta_chroma_weight[i][j][k]:
1092     *       \n - i equal to 0 is for reference picture list 0, equal to 1 is for reference picture list 1.
1093     *       \n - j is for reference list index [0..14].
1094     *       \n - k  equal to 0 is for Cb, and equal to 1 is for Cr.
1095     *    \n Value range: [-127..128], inclusive. When chroma_weight_l0_flag or chroma_weight_l1_flag take value 0, corresponding delta_chroma_weight should be set 0.
1096     */
1097     char                delta_chroma_weight[2][CODEC_MAX_NUM_REF_FRAME_HEVC][2];
1098 
1099     /*! \brief Specifies the maximum number of merging MVP candidates supported in the slice.
1100     *
1101     *    Corresponds to HEVC spec variable MaxNumMergeCand.
1102     */
1103     uint8_t             MaxNumMergeCand;
1104     /*! \brief ????
1105     *
1106     *    ????
1107     */
1108     uint16_t            slice_id;
1109     /*! \brief ????
1110     *
1111     *    ????
1112     */
1113     uint16_t            BitLengthSliceHeaderStartingPortion;
1114     /*! \brief ????
1115     *
1116     *    ????
1117     */
1118     uint32_t            SliceHeaderByteOffset;
1119     /*! \brief ????
1120     *
1121     *    ????
1122     */
1123     uint32_t            SliceQpDeltaBitOffset;
1124     /*! \brief Bit offset of syntax element pred_weight_table() in slice segment header.
1125     *
1126     *    It aligns with the starting position of the current packed slice header. It is used when encoder prefers to override the weighted prediction parameters passed in from DDI.
1127     */
1128     uint32_t            PredWeightTableBitOffset;
1129     /*! \brief Bit length of syntax element pred_weight_table() in slice segment header.
1130     *
1131     *    It is used when encoder prefers to override the weighted prediction parameters passed in from DDI.
1132     */
1133     uint32_t            PredWeightTableBitLength;
1134     /*! \brief Bit offset of syntax element slice_sao_luma_flag in slice segment header.
1135     *
1136     *    It aligns with the starting position of the current packed slice header. BRC kernel may use it to update the slice_sao_luma_flag and slice_sao_chroma_flag values. The value is invalid if SAO_enabled_flag is false.
1137     */
1138     uint32_t            SliceSAOFlagBitOffset;
1139 } CODEC_HEVC_ENCODE_SLICE_PARAMS, *PCODEC_HEVC_ENCODE_SLICE_PARAMS;
1140 
1141 //!
1142 //! \struct    CodecEncodeHevcFeiPicParams
1143 //! \brief     Codec encode HEVC FEI pic params
1144 //!
1145 struct CodecEncodeHevcFeiPicParams
1146 {
1147     MOS_RESOURCE                resCTBCtrl;              // input CTB control buffer
1148     MOS_RESOURCE                resCTBCmd;               // ENC CTB cmd output buffer or PAK CTB cmd input buffer
1149     MOS_RESOURCE                resCURecord;             // ENC CU record output buffer or PAK CU record input buffer
1150     MOS_RESOURCE                resMVPredictor;          // input external MV predictor surface
1151     MOS_RESOURCE                resCTBQp;                // input QP per CTB surface
1152     MOS_RESOURCE                resDistortion;           // ENC or ENC_PAK Distortion output surface
1153 
1154     uint32_t                    NumMVPredictorsL0;
1155     uint32_t                    NumMVPredictorsL1;
1156 
1157     bool                        bCTBCmdCuRecordEnable;
1158     bool                        bDistortionEnable;
1159 
1160     /** \brief control parameters */
1161     uint32_t                    SearchPath;
1162     uint32_t                    LenSP;
1163     uint32_t                    MultiPredL0;
1164     uint32_t                    MultiPredL1;
1165     uint32_t                    SubPelMode;
1166     uint32_t                    MVPredictorInput;
1167 
1168     bool                        AdaptiveSearch;
1169     bool                        bPerBlockQP;
1170     bool                        bPerCTBInput;
1171     bool                        bColocatedCTBDistortion;
1172     bool                        bForceLCUSplit;
1173     bool                        bEnableCU64Check;
1174     bool                        bEnableCU64AmpCheck;
1175     bool                        bCU64SkipCheckOnly;
1176 
1177     uint32_t                    RefWidth;
1178     uint32_t                    RefHeight;
1179     uint32_t                    SearchWindow;
1180     uint32_t                    MaxNumIMESearchCenter;
1181     uint32_t                    FastIntraMode;
1182     uint32_t                    NumConcurrentEncFramePartition;
1183 
1184     /** \brief add for mutlple pass pak */
1185     uint32_t                    dwMaxFrameSize;
1186     uint32_t                    dwNumPasses;    //number of QPs
1187     uint8_t                    *pDeltaQp;       //list of detla QPs
1188 };
1189 
1190 //!
1191 //! \struct    CodecEncodeHevcSSHParams
1192 //! \brief     Codec encode HEVC Slice Header params
1193 //!
1194 struct CodecEncodeHevcSliceHeaderParams
1195 {
1196     uint8_t                     log2_max_pic_order_cnt_lsb_minus4; //Hevc slice header packer use
1197     uint8_t                     num_long_term_pics;
1198     struct LongTermRef
1199     {
1200         uint8_t                 used_by_curr_pic_lt_flag : 1;
1201         uint8_t                 delta_poc_msb_present_flag : 1;
1202         uint32_t                poc_lsb_lt;
1203         uint32_t                delta_poc_msb_cycle_lt;
1204     } lt[8];
1205     uint8_t                     lists_modification_present_flag;
1206     uint8_t                     ref_pic_list_modification_flag_lx[2];
1207     uint8_t                     list_entry_lx[2][16];
1208     uint8_t                     num_negative_pics;
1209     uint8_t                     num_positive_pics;
1210     uint16_t                    delta_poc_minus1[2][16];
1211     bool                        used_by_curr_pic_flag[2][16];
1212 };
1213 
1214 //!
1215 //! \struct   HEVC_TILE_STATS_INFO
1216 //! \brief    HEVC tiles states info
1217 //!
1218 struct HEVC_TILE_STATS_INFO
1219 {
1220     uint32_t uiTileSizeRecord     = 0;
1221     uint32_t uiHevcPakStatistics  = 0;
1222     uint32_t uiVdencStatistics    = 0;
1223     uint32_t uiHevcSliceStreamout = 0;
1224 };
1225 using PHEVC_TILE_STATS_INFO = HEVC_TILE_STATS_INFO*;
1226 
1227 typedef enum
1228 {
1229     HEVC_CHROMA_FORMAT_MONOCHROME = 0,
1230     HEVC_CHROMA_FORMAT_YUV420 = 1,
1231     HEVC_CHROMA_FORMAT_YUV422 = 2,
1232     HEVC_CHROMA_FORMAT_YUV444 = 3
1233 } HEVC_CHROMA_FORMAT_IDC;
1234 
CodecHalHevcEncode_GetBitstreamBufferSize(uint32_t frameWidth,uint32_t frameHeight,uint8_t chromaFormat,bool is10Bits)1235 inline uint32_t CodecHalHevcEncode_GetBitstreamBufferSize(
1236            uint32_t    frameWidth,
1237            uint32_t    frameHeight,
1238            uint8_t     chromaFormat,
1239            bool        is10Bits)
1240 {
1241     // 4:2:0 uncompression buffer size
1242 
1243     frameHeight = (frameHeight * 3) / (is10Bits ? 1 : 2);
1244 
1245     if (chromaFormat == HEVC_CHROMA_FORMAT_YUV422)
1246     {
1247         frameWidth = (frameWidth * 8) / 6; //4:2:2 v.s 4:2:0
1248     }
1249     else if (chromaFormat == HEVC_CHROMA_FORMAT_YUV444)
1250     {
1251         frameWidth = (frameWidth * 12) / 6; //4:4:4 v.s 4:2:0
1252     }
1253 
1254     return frameWidth * frameHeight;
1255 }
1256 
1257 #endif  // __CODEC_DEF_ENCODE_HEVC_H__
1258