xref: /aosp_15_r20/external/intel-media-driver/media_common/agnostic/common/codec/shared/codec_def_encode.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2018-2023, 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.h
24 //! \brief    Defines encode types and macros shared by CodecHal and DDI layer
25 //! \details  Applies to encode only. Should not contain any DDI specific code.
26 //!
27 
28 #ifndef __CODEC_DEF_ENCODE_H__
29 #define __CODEC_DEF_ENCODE_H__
30 #include "mos_os.h"
31 #include "codec_def_common.h"
32 
33 #define CODECHAL_VDENC_BRC_NUM_OF_PASSES        2
34 #define CODECHAL_PAK_OBJ_EACH_CU                66
35 #define CODECHAL_LPLA_NUM_OF_PASSES             2
36 #define CODECHAL_ENCODE_BRC_KBPS                1000  // 1000bps for disk storage, aligned with industry usage
37 
38 //!
39 //! \struct AtomicScratchBuffer
40 //! \brief  The sturct of Atomic Scratch Buffer
41 //!
42 struct AtomicScratchBuffer
43 {
44     MOS_RESOURCE                            resAtomicScratchBuffer;     //!> Handle of eStatus buffer
45     uint32_t                                *pData;                     //!> Pointer of the buffer of actual data
46     uint16_t                                wEncodeUpdateIndex;         //!> used for VDBOX update encode status
47     uint16_t                                wTearDownIndex;             //!> Reserved for future extension
48     uint32_t                                dwZeroValueOffset;          //!> Store the result of the ATOMIC_CMP
49     uint32_t                                dwOperand1Offset;           //!> Operand 1 of the ATOMIC_CMP
50     uint32_t                                dwOperand2Offset;           //!> Operand 2 of the ATOMIC_CMP
51     uint32_t                                dwOperand3Offset;           //!> Copy of the operand 1
52 
53     uint32_t                                dwSize;                     //!> Size of the buffer
54     uint32_t                                dwOperandSetSize;           //!> Size of Operand set
55 };
56 
57 //!
58 //! \struct CodechalEncodeSeiData
59 //! \brief  Indicate the SeiData parameters
60 //!
61 struct CodechalEncodeSeiData
62 {
63     bool       newSEIData;
64     uint32_t   dwSEIDataSize;
65     uint32_t   dwSEIBufSize;
66     uint8_t*   pSEIBuffer;
67 };
68 
69 typedef struct _CODEC_ENCODE_MB_CONTROL
70 {
71     union
72     {
73         struct
74         {
75             uint32_t        bForceIntra : 1;
76             uint32_t        Reserved : 31;
77         };
78         uint32_t            value;
79     } MBParams;
80 } CODEC_ENCODE_MB_CONTROL, *PCODEC_ENCODE_MB_CONTROL;
81 
82 struct MetaDataOffset
83 {
84     uint32_t dwEncodeErrorFlags                         = 0;
85     uint32_t dwEncodedBitstreamWrittenBytesCount        = 0;
86     uint32_t dwWrittenSubregionsCount                   = 0;
87 
88     uint32_t dwEncodeStats                       = 0;
89     uint32_t dwAverageQP                         = 0;
90     uint32_t dwIntraCodingUnitsCount             = 0;
91     uint32_t dwInterCodingUnitsCount             = 0;
92     uint32_t dwSkipCodingUnitsCount              = 0;
93     uint32_t dwAverageMotionEstimationXDirection = 0;
94     uint32_t dwAverageMotionEstimationYDirection = 0;
95 
96     uint32_t dwbSize        = 0;
97     uint32_t dwbStartOffset = 0;
98     uint32_t dwbHeaderSize  = 0;
99 
100     uint32_t dwMetaDataSize             = 0;
101     uint32_t dwMetaDataSubRegionSize    = 0;
102 
103     uint32_t dwTilePartitionSize = 0;
104     uint32_t dwPostFeatueSize    = 0;
105 };
106 
107 //!
108 //! \struct EncoderParams
109 //! \brief  Encoder parameters
110 //!
111 struct EncoderParams
112 {
113     CODECHAL_FUNCTION               ExecCodecFunction       = CODECHAL_FUNCTION_INVALID;    //!< High level codec functionality requested.
114 
115     PMOS_SURFACE                    psRawSurface            = nullptr;                      //!< Raw surface
116     PMOS_SURFACE                    psReconSurface          = nullptr;                      //!< reconstructed surface
117     PMOS_RESOURCE                   presBitstreamBuffer     = nullptr;                      //!< Output buffer for bitstream data.
118     PMOS_RESOURCE                   presMetadataBuffer      = nullptr;                      //!< Output buffer for meta data.
119     PMOS_RESOURCE                   presMbCodeSurface       = nullptr;                      //!< PAK objects provided by framework.
120     PMOS_SURFACE                    psMbSegmentMapSurface   = nullptr;                      //!< [VP9]
121     /* \brief [AVC & MPEG2] MB QP data provided by framework.
122     *
123     *    When in CQP mode, the framework can provide this surface that contains a single QpY value for each macroblock to be used for encoding. If it is not provided, the frame level QpY(QpY + slice_qp_delta) will be used for all macroblocks.
124     */
125     PMOS_SURFACE                    psMbQpDataSurface               = nullptr;                  //!< pointer to surface of Mb QP Data
126     PCODEC_ENCODE_MB_CONTROL        pMbCtrlBuffer                   = nullptr;                  //!< [AVC] MB contrl map provided by framework
127     PMOS_SURFACE                    psMbDisableSkipMapSurface       = nullptr;                  //!< [AVC] MB disable skip map provided by framework
128     PMOS_SURFACE                    psCoeffSurface                  = nullptr;                  //!< [VP9]
129     PMOS_RESOURCE                   presCoeffProbabilityBuffer      = nullptr;                  //!< [VP9] Coefficient probabilities provided by framework.
130     bool                            bNewSeq                         = false;                    //!< Indicates the start of a new sequence.
131     bool                            bPicQuant                       = false;                    //!< Indicates whether the scaling list is for SPS (0) or PPS (1).
132     bool                            bNewQmatrixData                 = false;                    //!< Indicates that new QM data was provided by framework.
133     CodechalEncodeSeiData           *pSeiData                       = nullptr;                  //!< [AVC & HEVC] Information pertaining to pSeiParamBuffer.
134     uint32_t                        dwSEIDataOffset                 = 0;                        //!< [AVC & HEVC] Offset to the first SEI message within pSeiParamBuffer.
135     uint8_t                         *pSeiParamBuffer                = nullptr;                  //!< [AVC & HEVC] Packed SEI messages provided by the framework
136     uint32_t                        dwNumSlices                     = 0;                        //!< Number of slice data structures in pSliceParams.
137     uint32_t                        dwAppDataSize                   = 0;                        //!< [JPEG]
138     uint32_t                        dwNumHuffBuffers                = 0;                        //!< [JPEG]
139     uint32_t                        dwMbDataBufferSize              = 0;                        //!< Size of the data contained in presMbCodeSurface
140     uint32_t                        dwBitstreamSize                 = 0;                        //!< Maximum amount of data to be output to presBitstreamBuffer.
141     bool                            bNewVuiData                     = false;                    //!< [AVC & MPEG2] Indicates that pVuiParams is present and expected to be valid.
142     bool                            bJpegQuantMatrixSent            = false;                    //!< [JPEG] Indicates whether a quant matrix was sent by the framework.
143     PBSBuffer                       pBSBuffer                       = nullptr;                  //!< Packed header data provided by the framework to be inserted in the bitstream.
144     PCODECHAL_NAL_UNIT_PARAMS       *ppNALUnitParams                = nullptr;                  //!< Information about the packed header data in pBSBuffer.
145     uint32_t                        uiNumNalUnits                   = 0;                        //!< Number of NAL units in ppNALUnitParams.
146     void                            *pSlcHeaderData                 = nullptr;                  //!< [AVC, HEVC, & MPEG2] Packed slice header data provided by the framework.
147     bool                            bAcceleratorHeaderPackingCaps   = false;                    //!< [AVC] Indicates whether or not the driver is packing the slice headers.
148     uint32_t                        uiSlcStructCaps                 = 0;                        //!< [AVC] Slice capability information, formatted as CODEC_SLICE_STRUCTS
149     bool                            bMADEnabled                     = false;                    //!< MAD is enabled
150     bool                            bMbQpDataEnabled                = false;                    //!< [AVC & MPEG2] Indicates that psMbQpDataSurface is present.
151     bool                            bMbDisableSkipMapEnabled        = false;                    //!< [AVC] Indicates that psMbDisableSkipMapSurface is present.
152     bool                            bReportStatisticsEnabled        = false;                    //!< [HEVC] Indicates whether statistic reporting is enabled, disabled by default.
153     bool                            bQualityImprovementEnable       = false;                    //!< [HEVC] Indicates whether quality improvement is enabled, disabled by default.
154     bool                            newSeqHeader                    = false;                    //!< [AVC] Flag for new Sequence Header.
155     bool                            newPpsHeader                    = false;                    //!< [AVC] Flag for new PPS Header.
156     bool                            arbitraryNumMbsInSlice          = false;                    //!< [AVC] Flag to indicate if the sliceMapSurface needs to be programmed or not.
157 
158     void                            *pSeqParams                     = nullptr;                  //!< Sequence parameter set structure defined per standard.
159     void                            *pPicParams                     = nullptr;                  //!< Picture parameter set structure defined per standard.
160     void                            *pVuiParams                     = nullptr;                  //!< [AVC & MPEG2] Picture parameter set structure defined per standard.
161     void                            *pSliceParams                   = nullptr;                  //!< Slice data array defined per standard, expect dwNumSlices entries.
162     void                            *pSegmentParams                 = nullptr;                  //!< [VP9]
163     void                            *pIQMatrixBuffer                = nullptr;                  //!< [AVC, HEVC, & MPEG2] IQ parameter structure defined per standard.
164 
165     // AVC Specific Parameters
166     void                            *pIQWeightScaleLists            = nullptr;                  //!< list of IQ Weight scale
167     void                            *pAVCQCParams                   = nullptr;                  //!< AVC QC parameters
168     void                            *pAVCRoundingParams             = nullptr;                  //!< AVC rounding parameters
169 
170     void                            *pQuantData                     = nullptr;
171     PMOS_RESOURCE                   presDistortionDataSurface       = nullptr;
172     uint32_t                        uiFrameRate                     = 0;
173 
174     bool                            bSegmentMapProvided             = false;                    //!< [VP9]
175 
176     void                            *pMpeg2UserDataListHead         = nullptr;                  //!< [MPEG2]
177 
178     void                            *pHuffmanTable                  = nullptr;                  //!< [JPEG]
179     void                            *pQuantizationTable             = nullptr;                  //!< [JPEG]
180     void                            *pApplicationData               = nullptr;                  //!< [JPEG]
181 
182     void                            *pFeiPicParams                  = nullptr;                  //!< [FEI]
183     void                            *pPreEncParams                  = nullptr;                  //!< [FEI]
184 
185     // HEVC Specific Parameters
186     bool                            bVdencActive                    = false;                    //!< Indicate if vdenc is active
187     bool                            advanced                        = false;                    //!< Indicate if vdenc is active
188 
189     MOS_SURFACE                     rawSurface                      = {};                       //!< Raw surface
190     MOS_SURFACE                     reconSurface                    = {};                       //!< reconstructed surface
191     MOS_RESOURCE                    resBitstreamBuffer              = {};                       //!< Output buffer for bitstream data.
192     MOS_SURFACE                     mbQpSurface                     = {};
193     MOS_SURFACE                     disableSkipMapSurface           = {};                       //!< [AVC] MB disable skip map provided by framework
194     HANDLE                          gpuAppTaskEvent                 = nullptr;                  // MSDK event handling
195     //Call back to application. This informs the application  all ENC kernel workload is submitted(in case of HEVC VME)
196     //such that Application can make use of render engine when encoder is working on PAK. this helps in efficient utilisation of
197     //Render engine for improving the performance as the render engine will be idle when encoder is working on PAK.
198     void *                          plastEncKernelSubmissionCompleteCallback = nullptr;
199 
200 
201     bool                            bStreamOutEnable    = false;
202     PMOS_RESOURCE                   pStreamOutBuffer    = nullptr;          // StreamOut buffer
203     bool                            bCoeffRoundTag      = false;
204     uint32_t                        uiRoundIntra        = 0;
205     uint32_t                        uiRoundInter        = 0;
206 
207     PMOS_RESOURCE                   presMbInlineData    = nullptr;
208     PMOS_RESOURCE                   presMbConstSurface  = nullptr;
209     PMOS_RESOURCE                   presVMEOutSurface   = nullptr;
210     uint32_t                        uiMVoffset          = 0;                // App provides PAK objects and MV data in the same surface. This is offset to MV Data.
211     bool                            fullHeaderInAppData = false;            //!< [JPEG]
212     uint32_t                        uiOverallNALPayload = 0;
213     MetaDataOffset                  metaDataOffset      = {};
214     void *                          pSliceHeaderParams  = nullptr;          //!< [HEVC]
215 
216     /*! \brief Specifies motion search modes that will be used.
217     *
218     *    SubPelMode is only valid when bEnableSubPelMode is true. Following are valid values of SubPelMode:
219     *    0:Integer mode searching
220     *    1:Half-pel mode searching
221     *    2:Reserved
222     *    3:Quarter-pel mode searching
223     */
224     bool                            bEnableSubPelMode = false;
225     uint8_t                         SubPelMode        = 0;
226 
227     PMOS_RESOURCE                   m_presPredication           = nullptr;      //! \brief [Predication] Resource for predication
228     uint64_t                        m_predicationResOffset      = 0;            //! \brief [Predication] Offset for Predication resource
229     bool                            m_predicationNotEqualZero   = false;        //! \brief [Predication] Predication mode
230     bool                            m_predicationEnabled        = false;        //! \brief [Predication] Indicates whether or not Predication is enabled
231     PMOS_RESOURCE                   *m_tempPredicationBuffer    = nullptr;      //! \brief [Predication] Temp buffer for Predication
232 
233     bool                            m_setMarkerEnabled          = false;        //! \brief [SetMarker] Indicates whether or not SetMarker is enabled
234     PMOS_RESOURCE                   m_presSetMarker             = nullptr;      //! \brief [SetMarker] Resource for SetMarker
235 };
236 
237 //!
238 //! \struct PerfTagSetting
239 //! \brief  Setting of performance tags
240 //!
241 struct PerfTagSetting
242 {
243     union
244     {
245         struct
246         {
247             uint16_t    PictureCodingType   : 2;
248             uint16_t    CallType            : 6;
249             uint16_t    Mode                : 4;
250             uint16_t                        : 4;
251         };
252         uint16_t        Value;
253     };
254 };
255 
256 //!
257 //! \enum   MbBrcSetting
258 //! \brief  Indicate the MBBRC settings
259 //!
260 enum MbBrcSetting
261 {
262     mbBrcInternal = 0,
263     mbBrcEnabled  = 1,
264     mbBrcDisabled = 2,
265 };
266 
267 //!
268 //! \struct VdencBrcPakMmio
269 //! \brief  MMIO of BRC and PAK
270 //!
271 struct VdencBrcPakMmio
272 {
273     uint32_t                dwReEncode[4];
274 };
275 
276 //!
277 //! \struct    EncodeStatusReadParams
278 //! \brief     Read encode states parameters
279 //!
280 struct EncodeStatusReadParams
281 {
282     bool          vdencBrcEnabled                          = false;
283     bool          waReadVDEncOverflowStatus                = false;
284     uint32_t      mode                                     = 0;
285 
286     uint32_t      vdencBrcNumOfSliceOffset                 = 0;
287     PMOS_RESOURCE *resVdencBrcUpdateDmemBufferPtr          = nullptr;
288 
289     PMOS_RESOURCE resBitstreamByteCountPerFrame            = nullptr;
290     uint32_t      bitstreamByteCountPerFrameOffset         = 0;
291 
292     PMOS_RESOURCE resBitstreamSyntaxElementOnlyBitCount    = nullptr;
293     uint32_t      bitstreamSyntaxElementOnlyBitCountOffset = 0;
294 
295     PMOS_RESOURCE resQpStatusCount                         = nullptr;
296     uint32_t      qpStatusCountOffset                      = 0;
297 
298     PMOS_RESOURCE resNumSlices                             = nullptr;
299     uint32_t      numSlicesOffset                          = 0;
300 
301     PMOS_RESOURCE resImageStatusMask                       = nullptr;
302     uint32_t      imageStatusMaskOffset                    = 0;
303 
304     PMOS_RESOURCE resImageStatusCtrl                       = nullptr;
305     uint32_t      imageStatusCtrlOffset                    = 0;
306 };
307 
308 //!
309 //! \struct    FeiPreEncParams
310 //! \brief     Fei pre-encode parameters
311 //!
312 struct FeiPreEncParams
313 {
314     MOS_RESOURCE                    resMvPredBuffer;
315     MOS_RESOURCE                    resMbQpBuffer;
316     MOS_RESOURCE                    resMvBuffer;
317     MOS_RESOURCE                    resStatsBuffer;
318     MOS_RESOURCE                    resStatsBotFieldBuffer;
319 
320     PMOS_SURFACE                    psCurrOriginalSurface;
321 
322     bool                            bInterlaced;
323     uint32_t                        dwNumPastReferences;
324     uint32_t                        dwNumFutureReferences;
325 
326     bool                            bCurPicUpdated;
327     CODEC_PICTURE                   CurrOriginalPicture;
328 
329     CODEC_PICTURE                   PastRefPicture;
330     bool                            bPastRefUpdated;
331     MOS_SURFACE                     sPastRefSurface;
332     bool                            bPastRefStatsNeeded;
333     MOS_RESOURCE                    sPastRefStatsBuffer;
334     MOS_RESOURCE                    sPastRefStatsBotFieldBuffer;
335 
336     CODEC_PICTURE                   FutureRefPicture;
337     bool                            bFutureRefUpdated;
338     MOS_SURFACE                     sFutureRefSurface;
339     bool                            bFutureRefStatsNeeded;
340     MOS_RESOURCE                    sFutureRefStatsBuffer;
341     MOS_RESOURCE                    sFutureRefStatsBotFieldBuffer;
342 
343     uint32_t                        dwFrameQp;
344     uint32_t                        dwLenSP;
345     uint32_t                        dwSearchPath;
346     uint32_t                        dwSubMBPartMask;
347     uint32_t                        dwIntraPartMask;
348     uint32_t                        dwSubPelMode;
349     uint32_t                        dwInterSAD;
350     uint32_t                        dwIntraSAD;
351     bool                            bAdaptiveSearch;
352 
353     uint32_t                        dwMVPredictorCtrl;
354     bool                            bMBQp;
355     bool                            bFTEnable;
356     uint32_t                        dwRefWidth;
357     uint32_t                        dwRefHeight;
358     uint32_t                        dwSearchWindow;
359     bool                            bDisableMVOutput;
360     bool                            bDisableStatisticsOutput;
361     bool                            bEnable8x8Statistics;
362     bool                            bInputUpdated;
363 };
364 
365 #endif // !__CODEC_DEF_ENCODE_H__
366