xref: /aosp_15_r20/external/intel-media-driver/media_driver/agnostic/gen9/codec/hal/codechal_fei_avc_g9.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2011-2017, 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     codechal_fei_avc_g9.h
24 //! \brief    This file defines the C++ class/interface for Gen9 platform's AVC
25 //!           FEI encoding to be used across CODECHAL components.
26 //!
27 
28 #ifndef __CODECHAL_FEI_AVC_G9_H__
29 #define __CODECHAL_FEI_AVC_G9_H__
30 
31 #include "codechal_encode_avc_g9.h"
32 
33 enum MbEncBufIndex
34 {
35     mbEncMbCodeBuffer           = 0,
36     mbEncMvDataBuffer           = 1,
37     mbEncFwdMbCodeBuffer        = 2,
38     mbEncFwdMvDataBuffer        = 3,
39     mbEncMADDataBuffer          = 4,
40     mbEncMBSpecficDataBuffer    = 5,
41     mbEncMVPredictorBuffer      = 6,
42     mbEncAuxVmeOutBuffer        = 7,
43     mbEncMBBRCLutBuffer         = 8,
44     mbEncMBQPBuffer             = 9,
45     mbEncBRCCurbeBuffer         = 10,
46 };
47 
48 enum MbEncSurfIndex
49 {
50     mbEncSrcYSurf                       = 0,
51     mbEncHMEMVPredFwdBwdSurf            = 1,
52     mbEncHMEDistSurf                    = 2,
53     mbEncVMEInterPredictionSurf         = 3,
54     mbEncVMEInterPredictionMRSurf       = 9,
55     mbEncMBDistSurf                     = 11,
56     mbEncCurrRefPicSelSurf              = 12,
57     mbEncMBstatsSurf                    = 13,
58     mbEncSliceMapSurf                   = 14,
59 };
60 
61 struct CodechalEncodeAvcSurfaceIdx{
62     union{
63         struct{
64             SurfaceIndex * pCmSurfIdx[28];
65         };
66         struct{
67             SurfaceIndex * MBDataSurfIndex;
68             SurfaceIndex * MVDataSurfIndex;
69             SurfaceIndex * MBDistIndex;
70             SurfaceIndex * SrcYSurfIndex;
71             SurfaceIndex * MBSpecficDataSurfIndex;
72             SurfaceIndex * AuxVmeOutSurfIndex;
73             SurfaceIndex * CurrRefPicSelSurfIndex;
74             SurfaceIndex * HMEMVPredFwdBwdSurfIndex;
75             SurfaceIndex * HMEDistSurfIndex;
76             SurfaceIndex * SliceMapSurfIndex;
77             SurfaceIndex * FwdFrmMBDataSurfIndex;
78             SurfaceIndex * FwdFrmMVSurfIndex;
79             SurfaceIndex * MBQPBuffer;
80             SurfaceIndex * MBBRCLut;
81             SurfaceIndex * MBVMEInterPredictionSurfIndex;
82             SurfaceIndex * MBVMEInterPredictionMRSurfIndex;
83             SurfaceIndex * MBstats;
84             SurfaceIndex * MADSurfIndex;
85             SurfaceIndex * ForceNonSkipMBMap;
86             SurfaceIndex * ReservedIndex;
87             SurfaceIndex * BRCCurbeSurfIndex;
88             SurfaceIndex * StaticDetectionCostTableIndex;
89             SurfaceIndex * FEI_MVPredSurfIndex;
90             SurfaceIndex * L1RefPicSelSurfIndex;
91             SurfaceIndex * Flatnesschk;
92             SurfaceIndex * InterDistortionIndex;
93             SurfaceIndex * BestInterIntraSurfIndex;
94             SurfaceIndex * Reserved0;
95         };
96     };
97 };
98 
99 class CodechalEncodeAvcEncFeiG9 : public CodechalEncodeAvcEncG9
100 {
101 public:
102     static const uint32_t ModeMvCost_Cm_PreProc[3][CODEC_AVC_NUM_QP][8];
103     static const uint32_t ME_CURBE_CM_FEI[39];
104     static const uint32_t PreProc_CURBE_CM_normal_I_frame[49];
105     static const uint32_t PreProc_CURBE_CM_normal_I_field[49];
106     static const uint32_t PreProc_CURBE_CM_normal_P_frame[49];
107     static const uint32_t PreProc_CURBE_CM_normal_P_field[49];
108     static const uint32_t PreProc_CURBE_CM_normal_B_frame[49];
109     static const uint32_t PreProc_CURBE_CM_normal_B_field[49];
110     static const uint16_t RefCost_MultiRefQp_Fei[NUM_PIC_TYPES][64];
111     static const uint32_t FEI_MBEnc_CURBE_normal_I_frame[104];
112     static const uint32_t FEI_MBEnc_CURBE_normal_I_field[104];
113     static const uint32_t FEI_MBEnc_CURBE_normal_P_frame[104];
114     static const uint32_t FEI_MBEnc_CURBE_normal_P_field[104];
115     static const uint32_t FEI_MBEnc_CURBE_normal_B_frame[104];
116     static const uint32_t FEI_MBEnc_CURBE_normal_B_field[104];
117     static const uint32_t FEI_MBEnc_CURBE_I_frame_DIST[104];
118     static const uint32_t HMEBCombineLen_fei[NUM_TARGET_USAGE_MODES];
119     static const uint32_t HMECombineLen_fei[NUM_TARGET_USAGE_MODES];
120     static const uint32_t m_brcConstantSurfaceHeightFei = 44;
121     static const uint32_t m_refThresholdFei = 400;
122     static const uint32_t m_mbencNumTargetUsagesCmFei = 1;
123     static const uint32_t m_meCurbeDataSizeFei = 128;
124     static const uint32_t m_mbencCurbeDataSizeFei = 320;
125     static const uint32_t m_preProcCurbeDataSizeFei = 160;
126 
127 #ifdef FEI_ENABLE_CMRT
128     static const uint32_t m_mdfDsBufSize = 2;           //ds use 2 buffer & 4 surface for each channel,  6 buffer and 12 totally
129     static const uint32_t m_mdfDsSurfSize = 4;
130     static const uint32_t m_mdfDsVmeSurfSize = 0;
131 
132     static const uint32_t m_mdfMeBufSize = 0;           //me use 0 buffer and 12 surface
133     static const uint32_t m_mdfMeSurfSize = 12;
134     static const uint32_t m_mdfMeVmeSurfSize = 2;
135 
136     static const uint32_t m_mdfPreProcBufSize = 6;           //preproc use 5 buffers and  4 surface
137     static const uint32_t m_mdfPreProcSurfSize  = 4;
138     static const uint32_t m_mdfPreProcVmeSurfSize = 2;
139     CodechalEncodeMdfKernelResource m_resPreProcKernel;
140     CodechalEncodeMdfKernelResource m_resMeKernel;
141     uint32_t                            m_dsIdx;
142 #endif
143 
144     static const uint32_t m_mdfMbencBufSize = 12;           //MBEnc is not in a context with preenc
145     static const uint32_t m_mdfMbencSurfSize = 16;
146     static const uint32_t m_mdfMbencVmeSurfSize = 2;
147     static const uint32_t m_vmeSurfacePerStreamSize = 2;
148     static const uint32_t m_commonSurfacePerStreamSize = 24;
149     static const uint32_t m_vmeSurfaceSize = m_vmeSurfacePerStreamSize * CODECHAL_ENCODE_AVC_MFE_MAX_FRAMES_G9;
150     static const uint32_t m_commonSurfaceSize = m_commonSurfacePerStreamSize * CODECHAL_ENCODE_AVC_MFE_MAX_FRAMES_G9;
151 
152     struct CodechalEncodeAvcSurfaceIdx *m_cmSurfIdx = nullptr;
153     CodechalEncodeMdfKernelResource    *m_resMbencKernel = nullptr;
154     CodechalEncodeMdfKernelResource    *m_origResMbencKernel = nullptr;
155 
156     CmQueue *m_origCmQueue            = nullptr;
157     CmTask  *m_origCmTask             = nullptr;
158 
159     SurfaceIndex *m_origVmeSurface    = nullptr;
160     SurfaceIndex *m_origCommonSurface = nullptr;
161 
162     SurfaceIndex *m_vmeSurface        = nullptr;
163     SurfaceIndex *m_commonSurface     = nullptr;
164 
165     CodechalEncodeAvcEncFeiG9(
166         CodechalHwInterface *   hwInterface,
167         CodechalDebugInterface *debugInterface,
168         PCODECHAL_STANDARD_INFO standardInfo);
169 
170     CodechalEncodeAvcEncFeiG9(const CodechalEncodeAvcEncFeiG9&) = delete;
171 
172     CodechalEncodeAvcEncFeiG9& operator=(const CodechalEncodeAvcEncFeiG9&) = delete;
173 
174     ~CodechalEncodeAvcEncFeiG9();
175 
176     //!
177     //! \brief    Initializes the kernel.
178     //! \details
179     //!
180     //! \return   MOS_STATUS
181     //!           MOS_STATUS_SUCCESS if success
182     //!
183     MOS_STATUS InitializePicture(const EncoderParams& params);
184 
185     //!
186     //! \brief    Initializes the FEI PreEnc kernel.
187     //! \details  If PreEnc mode, initial PreEnc kernel.
188     //!
189     //! \return   MOS_STATUS
190     //!           MOS_STATUS_SUCCESS if success
191     //!
192     MOS_STATUS EncodePreEncInitialize(const EncoderParams& params);
193 
194     //!
195     //! \brief    Encode kernel functions
196     //!
197     //! \return   MOS_STATUS
198     //!           MOS_STATUS_SUCCESS if success, else fail reason
199     //!
200     MOS_STATUS ExecuteKernelFunctions();
201 
202     //!
203     //! \brief    Init ME kernel state
204     //!
205     //! \return   MOS_STATUS
206     //!           MOS_STATUS_SUCCESS if success, else fail reason
207     //!
208     virtual MOS_STATUS InitKernelStateMe();
209 #ifdef FEI_ENABLE_CMRT
210         // GenericEncodeMeKernel functions
211         //!
212         //! \brief    Run Encode ME kernel
213         //!
214         //! \param [in] brcBuffers
215         //!           Pointer to the brc buffer
216         //! \param   [in] hmeLevel
217         //!           Hme level
218         //! \return   MOS_STATUS
219         //!           MOS_STATUS_SUCCESS if success
220         //!
221         virtual MOS_STATUS GenericEncodeMeKernel(EncodeBrcBuffers* brcBuffers, HmeLevel hmeLevel);
222 
223         //!
224         //! \brief    Dispatch MDF Encode ME kernel
225         //!
226         //! \param [in] surfIndex
227         //!            Pointer to the SurfaceIndex * list which is used to Surfaces
228         //! \param [in] width
229         //!            input picture width
230         //!\param  [in]  height
231         //!            input picture height
232         //!\param  [in]   isBFrame
233         //!           if input picture is B frame
234         //! \return   MOS_STATUS
235         //!           MOS_STATUS_SUCCESS if success
236         //!
237 
238         MOS_STATUS DispatchKernelMe(
239             SurfaceIndex** surfIndex,
240             uint16_t       width,
241             uint16_t       height,
242             bool           isBFrame);
243 
244         //!
245         //! \brief    Dispatch MDF FEI preproc
246         //!
247         //! \param  [in] surfIndex
248         //!           Pointer to the SurfaceIndex * list which is used to Surfaces
249         //! \param  [in] width
250         //!           input picture width
251         //!\param   [in] height
252         //!           input picture height
253         //! \return   MOS_STATUS
254         //!           MOS_STATUS_SUCCESS if success
255         //!
256 
257         MOS_STATUS DispatchKernelPreProc(
258             SurfaceIndex**  surfIndex,
259             uint16_t        width,
260             uint16_t        height);
261 
262         //!
263         //! \brief    Init Scaling kernel state
264         //!
265         //! \return   MOS_STATUS
266         //!           MOS_STATUS_SUCCESS if success, else fail reason
267         //!
268 
269         static MOS_STATUS InitKernelStateScaling(PCODECHAL_ENCODER avcEncoder);
270 
271         //!
272         //! \brief    Dispatch MDF FEI 4x DownScalling
273         //! \param  [in]  flatnessThreshold
274         //!           flatness threshold
275         //! \param [in]  option
276         //!           down scaling option
277         //!             bit0 enable/disable flatness check
278         //!             bit1 enable/disable variance output
279         //!             bit2 enable/disable average output
280         //!             bit3 eanble/disable 8x8 statistics output
281         //! \param  [in]  sourceWidth
282         //!           input picture width
283         //!\param   [in]  sourceHeight
284         //!           input picture height
285         //!\param   [in] kernelType
286         //!           if input picture frame (0) or field (1)
287         //!\param   [in]  surfIndex
288         //!           Pointer to the SurfaceIndex * list which is used to Surfaces
289         //! \return   MOS_STATUS
290         //!           MOS_STATUS_SUCCESS if success
291         //!
292         MOS_STATUS DispatchKernelScaling(
293             uint32_t flatnessThreshold,
294             uint32_t options,
295             uint16_t sourceWidth,
296             uint16_t sourceHeight,
297             uint32_t kernelType,
298             SurfaceIndex** surfIdx);
299 
300         //!
301         //! \brief    MBEnc Encode kernel functions
302         //!
303         //! \param [in]  params
304         //!           downscalling parameters
305         //!
306         //! \return   MOS_STATUS
307         //!           MOS_STATUS_SUCCESS if success, else fail reason
308         //!
309 
310         MOS_STATUS EncodeScalingKernel(CodechalEncodeCscDs::KernelParams* params);
311 
312 #endif
313 
314         MOS_STATUS InitMfe();
315 
316         MOS_STATUS InitKernelStateMfeMbEnc();
317 
318         //!
319         //! \brief    Send AVC Mfe MbEnc kernel Curbe data.
320         //!
321         //! \return   MOS_STATUS
322         //!           MOS_STATUS_SUCCESS if success, else fail reason
323         //!
324         MOS_STATUS SendCurbeAvcMfeMbEnc();
325 
326         //!
327         //! \brief    Dispatch MDF Encode MBEnc kernel
328         //!
329         //! \param  [in]  params
330         //!           Dispatch Parameters
331         //! \return   MOS_STATUS
332         //!           MOSs_STATUS_SUCCESS if success
333         //!
334         MOS_STATUS DispatchKernelMbEnc(
335             void      *params);
336 
337         //!
338         //! \brief    MBEnc Encode kernel functions
339         //!
340         //! \return   MOS_STATUS
341         //!           MOS_STATUS_SUCCESS if success, else fail reason
342         //!
343         MOS_STATUS EncodeMbEncKernelFunctions();
344 
345         //!
346         //! \brief    Set AVC MbEnc kernel Surface data.
347         //!
348         //! \param    [in] cmdBuffer
349         //!           Pointer to the MOS_COMMAND_BUFFER
350         //! \param    [in] params
351         //!           Pointer to the CODECHAL_ENCODE_AVC_MBENC_SURFACE_PARAMS
352         //!
353         //! \return   MOS_STATUS
354         //!           MOS_STATUS_SUCCESS if success, else fail reason
355         //!
356         virtual MOS_STATUS SendAvcMfeMbEncSurfaces(
357             PMOS_COMMAND_BUFFER cmdBuffer,
358             PCODECHAL_ENCODE_AVC_MBENC_SURFACE_PARAMS params);
359 
360         //!
361         //! \brief    Set AVC ME kernel curbe
362         //!
363         //! \param    [in] params
364         //!           Pointer to the MeCurbeParams
365         //! \return   MOS_STATUS
366         //!           MOS_STATUS_SUCCESS if success, else fail reason
367         //!
368         virtual MOS_STATUS SetCurbeMe (
369             MeCurbeParams* params);
370 
371     //!
372     //! \brief    Send surface cmd to AVC ME kernel.
373     //!
374     //! \return   MOS_STATUS
375     //!           MOS_STATUS_SUCCESS if success, else fail reason
376     //!
377     virtual MOS_STATUS SendMeSurfaces (
378         PMOS_COMMAND_BUFFER cmdBuffer,
379         MeSurfaceParams* params);
380 
381     //!
382     //! \brief    Get encoder kernel header and kernel size
383     //!
384     //! \param    [in] pvBinary
385     //!           Pointer to kernel binary
386     //! \param    [in] operation
387     //!           Enc kernel operation
388     //! \param    [in] krnStateIdx
389     //!           Kernel state index
390     //! \param    [out] pvKrnHeader
391     //!           Pointer to kernel header
392     //! \param    [out] pdwKrnSize
393     //!           Pointer to kernel size
394     //!
395     //! \return   MOS_STATUS
396     //!           MOS_STATUS_SUCCESS if success, else fail reason
397     //!
398     static MOS_STATUS EncodeGetKernelHeaderAndSize(
399         void                           *binary,
400         EncOperation                   operation,
401         uint32_t                       krnStateIdx,
402         void                           *krnHeader,
403         uint32_t                       *krnSize);
404 
405     //!
406     //! \brief    PreEnc Encode kernel functions
407     //!
408     //! \return   MOS_STATUS
409     //!           MOS_STATUS_SUCCESS if success, else fail reason
410     //!
411     MOS_STATUS EncodePreEncKernelFunctions();
412 
413     // state related functions
414     //!
415     //! \brief    Initialize related states
416     //!
417     //! \return   MOS_STATUS
418     //!           MOS_STATUS_SUCCESS if success, else fail reason
419     //!
420     virtual MOS_STATUS InitializeState();
421 
422     //!
423     //! \brief    Validate reference list L0 and L1.
424     //!
425     //! \param    [in] params
426     //!           pointer to CODECHAL_ENCODE_AVC_VALIDATE_NUM_REFS_PARAMS
427     //!
428     //! \return   MOS_STATUS
429     //!           MOS_STATUS_SUCCESS if success, else fail reason
430     //!
431     virtual MOS_STATUS ValidateNumReferences(
432         PCODECHAL_ENCODE_AVC_VALIDATE_NUM_REFS_PARAMS params);
433 
434     //!
435     //! \brief    Init MbEnc kernel State.
436     //!
437     //! \return   MOS_STATUS
438     //!           MOS_STATUS_SUCCESS if success, else fail reason
439     //!
440     virtual MOS_STATUS InitKernelStateMbEnc();
441 
442     //!
443     //! \brief    Initialize PreProc kernel state
444     //!
445     //! \return   MOS_STATUS
446     //!           MOS_STATUS_SUCCESS if success, else fail reason
447     //!
448     virtual MOS_STATUS InitKernelStatePreProc();
449 
450     //!
451     //! \brief    Initialize WP kernel state
452     //!
453     //! \return   MOS_STATUS
454     //!           MOS_STATUS_SUCCESS if success, else fail reason
455     //!
456     virtual MOS_STATUS InitKernelStateWP();
457 
458     //!
459     //! \brief    Get MbEnc kernel state idx
460     //!
461     //! \param    [in] params
462     //!           Pointer to the CodechalEncodeIdOffsetParams
463     //! \param    [in] kernelOffset
464     //!           kernel offset
465     //!
466     //! \return   MOS_STATUS
467     //!           MOS_STATUS_SUCCESS if success, else fail reason
468     //!
469     virtual MOS_STATUS GetMbEncKernelStateIdx(
470         CodechalEncodeIdOffsetParams       *params,
471         uint32_t                           *kernelOffset);
472 
473     //!
474     //! \brief    Set AVC MbEnc kernel Curbe data.
475     //!
476     //! \param    [in] params
477     //!           Pointer to the CODECHAL_ENCODE_AVC_MBENC_CURBE_PARAMS
478     //!
479     //! \return   MOS_STATUS
480     //!           MOS_STATUS_SUCCESS if success, else fail reason
481     //!
482     virtual MOS_STATUS SetCurbeAvcMbEnc(
483         PCODECHAL_ENCODE_AVC_MBENC_CURBE_PARAMS params);
484 
485     //!
486     //! \brief    Set AVC PreProc kernel Curbe data.
487     //!
488     //! \param    [in] params
489     //!           Pointer to the CODECHAL_ENCODE_AVC_PREPROC_CURBE_PARAMS
490     //!
491     //! \return   MOS_STATUS
492     //!           MOS_STATUS_SUCCESS if success, else fail reason
493     //!
494     virtual MOS_STATUS SetCurbeAvcPreProc(
495         PCODECHAL_ENCODE_AVC_PREPROC_CURBE_PARAMS params);
496 
497     //!
498     //! \brief    Set AVC MbEnc kernel Surface data.
499     //!
500     //! \param    [in] cmdBuffer
501     //!           Pointer to the MOS_COMMAND_BUFFER
502     //! \param    [in] params
503     //!           Pointer to the CODECHAL_ENCODE_AVC_MBENC_SURFACE_PARAMS
504     //!
505     //! \return   MOS_STATUS
506     //!           MOS_STATUS_SUCCESS if success, else fail reason
507     //!
508     virtual MOS_STATUS SendAvcMbEncSurfaces(
509         PMOS_COMMAND_BUFFER cmdBuffer,
510         PCODECHAL_ENCODE_AVC_MBENC_SURFACE_PARAMS params);
511 
512     //!
513     //! \brief    Set AVC PreProc kernel Surface data.
514     //!
515     //! \param    [in] cmdBuffer
516     //!           Pointer to the MOS_COMMAND_BUFFER
517     //! \param    [in]  params
518     //!           Pointer to the CODECHAL_ENCODE_AVC_PREPROC_SURFACE_PARAMS
519     //!
520     //! \return   MOS_STATUS
521     //!           MOS_STATUS_SUCCESS if success, else fail reason
522     //!
523     virtual MOS_STATUS SendAvcPreProcSurfaces(
524         PMOS_COMMAND_BUFFER cmdBuffer,
525         PCODECHAL_ENCODE_AVC_PREPROC_SURFACE_PARAMS params);
526 
527     //!
528     //! \brief    Invoke FEI PreProc kernel.
529     //!
530     //! \return   MOS_STATUS
531     //!           MOS_STATUS_SUCCESS if success, else fail reason
532     //!
533     MOS_STATUS PreProcKernel();
534 };
535 
536 #endif  // __CODECHAL_ENC_AVC_FEI_G9_H__
537