1 /*
2 * Copyright (c) 2017-2018, 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_huc_cmd_initializer.h
24 //! \brief    Defines base class for HUC cmd initializer encoder.
25 //!
26 
27 #ifndef __CODECHAL_CMD_INITIALIZER_H__
28 #define __CODECHAL_CMD_INITIALIZER_H__
29 
30 #if defined (_HEVC_ENCODE_VME_SUPPORTED) || defined (_HEVC_ENCODE_VDENC_SUPPORTED)
31 #include "codechal_encode_hevc_base.h"
32 #endif
33 #include "codechal_encoder_base.h"
34 
35 #ifdef _VP9_ENCODE_VDENC_SUPPORTED
36 class CodechalVdencVp9State;
37 #endif
38 
39 // Command initializer command
40 typedef enum _CODECHAL_CMD_INITIALIZER_CMDTYPE
41 {
42     CODECHAL_CMD1 = 1,
43     CODECHAL_CMD2 = 2,
44     CODECHAL_CMD3 = 3,
45     CODECHAL_CMD5 = 5,
46 } CODECHAL_CMD_INITIALIZER_CMDTYPE;
47 
48 //!
49 //! \struct HucComDmem
50 //! \brief  The struct of Huc Com Dmem
51 //!
52 struct HucComDmem
53 {
54     uint32_t    OutputSize;               //!< Total size in byte of the Output SLB
55     uint32_t    TotalOutputCommands;      //!< Total Commands in the output SLB
56     uint8_t     TargetUsage;              //!< TU number
57     uint8_t     Codec;                    //!< 0-HEVC VDEnc; 1-VP9 VDEnc; 2-AVC VDEnc
58     uint8_t     FrameType;                //!< 0-I Frame; 1-P Frame; 2-B Frame
59     uint8_t     Reserved[37];
60     struct
61     {
62         uint16_t    StartInBytes;        //!< Command starts offset in bytes in Output SLB
63         uint8_t     ID;                  //!< Command ID
64         uint8_t     Type;                //!< Command Type
65         uint32_t    BBEnd;
66     } OutputCOM[50];
67 };
68 
69 //!
70 //! \struct HucComData
71 //! \brief  The struct of Huc commands data
72 //!
73 struct HucComData
74 {
75     uint32_t        TotalCommands;       //!< Total Commands in the Data buffer
76     struct
77     {
78         uint16_t    ID;              //!< Command ID, defined and order must be same as that in DMEM
79         uint16_t    SizeOfData;      //!< data size in uint32_t
80         uint32_t    data[40];
81     } InputCOM[50];
82 };
83 
84 //!
85 //! \struct HucInputCmd1
86 //! \brief  The struct of Huc input command 1
87 //!
88 struct HucInputCmd1
89 {
90     // Shared
91     uint32_t FrameWidthInMinCbMinus1;
92 
93     uint32_t FrameHeightInMinCbMinus1;
94 
95     uint32_t log2_min_coding_block_size_minus3;
96 
97     uint8_t  VdencStreamInEnabled;
98     uint8_t  PakOnlyMultipassEnable;
99     uint16_t num_ref_idx_l0_active_minus1;
100 
101     uint16_t SADQPLambda;
102     uint16_t RDQPLambda;
103 
104     // HEVC
105     uint16_t num_ref_idx_l1_active_minus1;
106     uint8_t  RSVD0;
107     uint8_t  ROIStreamInEnabled;
108 
109     int8_t   ROIDeltaQp[8]; // [-3..3] or [-51..51]
110 
111     uint8_t  FwdPocNumForRefId0inL0;
112     uint8_t  FwdPocNumForRefId0inL1;
113     uint8_t  FwdPocNumForRefId1inL0;
114     uint8_t  FwdPocNumForRefId1inL1;
115 
116     uint8_t  FwdPocNumForRefId2inL0;
117     uint8_t  FwdPocNumForRefId2inL1;
118     uint8_t  FwdPocNumForRefId3inL0;
119     uint8_t  FwdPocNumForRefId3inL1;
120 
121     uint8_t  EnableRollingIntraRefresh;
122     int8_t   QpDeltaForInsertedIntra;
123     uint16_t IntraInsertionSize;
124 
125     uint32_t  IntraInsertionReferenceLocation[3];
126 
127     uint16_t IntraInsertionLocation;
128     int8_t   QpY;
129     uint8_t  RoundingEnabled;
130 
131     uint8_t  UseDefaultQpDeltas;
132     uint8_t  PanicEnabled;
133     uint8_t  TemporalMvpEnableFlag;
134     uint8_t  RSVD[1];
135 
136     // VP9
137     uint16_t SrcFrameWidthMinus1;
138     uint16_t SrcFrameHeightMinus1;
139 
140     uint8_t  SegmentationEnabled;
141     uint8_t  PrevFrameSegEnabled;
142     uint8_t  SegMapStreamInEnabled;
143     uint8_t  LumaACQIndex;
144 
145     int8_t   LumaDCQIndexDelta;
146     uint8_t  RESERVED[3];
147 
148     int16_t  SegmentQIndexDelta[8];
149 };
150 
151 //!
152 //! \struct HucInputCmd2
153 //! \brief  The struct of Huc input command 2
154 //!
155 struct HucInputCmd2
156 {
157     uint32_t SADQPLambda;
158     uint8_t RoiEnabled;
159     uint8_t RSVD[3];
160 };
161 
162 #ifdef _VP9_ENCODE_VDENC_SUPPORTED
163 //!
164 //! \struct Vp9CmdInitializerParams
165 //! \brief  VP9 Params struct for huc initializer
166 //!
167 struct Vp9CmdInitializerParams
168 {
169     uint8_t                             vdencMvCosts[12] = { 0 };
170     uint8_t                             vdencRdMvCosts[12] = { 0 };
171     uint8_t                             vdencHmeMvCosts[8] = { 0 };
172     uint8_t                             vdencModeCosts[CODEC_VDENC_NUM_MODE_COST] = { 0 };
173 
174     uint16_t                            pictureCodingType;
175 
176     PCODEC_VP9_ENCODE_SEQUENCE_PARAMS   seqParams;
177     PCODEC_VP9_ENCODE_PIC_PARAMS        picParams;
178     bool                                segmentationEnabled;
179     bool                                segmentMapProvided;
180     PCODEC_VP9_ENCODE_SEGMENT_PARAMS    segmentParams;
181     bool                                prevFrameSegEnabled;
182     uint8_t                             numRefFrames;
183     bool                                me16Enabled;
184     uint8_t                             dysRefFrameFlags;
185     bool                                dysVdencMultiPassEnabled;
186     int                                 currentPass;
187     bool                                singleTaskPhaseSupported;
188     bool                                lastTaskInPhase;
189     bool                                firstTaskInPhase;
190     uint32_t                            mode;
191     bool                                videoContextUsesNullHw;
192     CodechalDebugInterface*             debugInterface;
193     bool                                dynamicScalingEnabled;
194     bool                                bPrevFrameKey;
195     // Common
196     uint16_t                            sadQpLambda;
197     uint16_t                            rdQpLambda;
198     bool                                vdencPakOnlyMultipassEnabled;
199 
200 };
201 #endif
202 
203 //!
204 //! \class  CodechalCmdInitializer
205 //! \brief  Command Initializer class
206 //!
207 class CodechalCmdInitializer
208 {
209 public:
210     static constexpr uint32_t m_hucCmdInitializerKernelDescriptor = 14; //!< VDBox Huc cmd initializer kernel descriptoer
211 
212     bool                                        m_pakOnlyPass = false;
213     bool                                        m_acqpEnabled = false;
214     bool                                        m_brcEnabled = false;
215     bool                                        m_streamInEnabled = false;
216     bool                                        m_roundingEnabled = false;
217     bool                                        m_panicEnabled = false;
218     bool                                        m_roiStreamInEnabled = false;
219     int32_t                                     m_currentPass = 0;
220     int32_t                                     m_cmdCount = 0 ;
221 
222     CodechalEncoderState                        *m_encoder = nullptr;                //!< Pointer to ENCODER base class
223     MOS_INTERFACE                               *m_osInterface = nullptr;            //!< OS interface
224     MhwMiInterface                              *m_miInterface = nullptr;            //!< Common Mi Interface
225     MhwVdboxVdencInterface                      *m_vdencInterface = nullptr;
226     void*                                       m_picParams = nullptr;               //!< Pointer to picture parameter
227     void*                                       m_seqParams = nullptr;               //!< Pointer to sequence parameter
228     void*                                       m_sliceParams = nullptr;             //!< Pointer to slice parameter
229 
230 #ifdef _VP9_ENCODE_VDENC_SUPPORTED
231     //VP9 related changes
232     Vp9CmdInitializerParams                     m_vp9Params;
233 #endif
234     CodechalHwInterface*                        m_hwInterface = nullptr;
235     MOS_RESOURCE                                m_cmdInitializerDmemBuffer[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM][3];
236     MOS_RESOURCE                                m_cmdInitializerDataBuffer[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM][3];
237     MOS_RESOURCE                                m_cmdInitializerDysScalingDmemBuffer = {0};
238     MOS_RESOURCE                                m_cmdInitializerDysScalingDataBuffer = {0};
239 
240     static constexpr uint32_t CODECHAL_CMD1_SIZE = 120;
241     static constexpr uint32_t CODECHAL_CMD2_SIZE = 148;
242 
243     bool m_hevcVisualQualityImprovementEnableFlag = false;  //!< VQI enable flag
244 
245 public:
246     //!
247     //! \brief    Constructor
248     //!
249 
250     CodechalCmdInitializer(CodechalEncoderState *encoder);
251 
252     //!
253     //! \brief   Default Constructor
254     //!
CodechalCmdInitializer()255     CodechalCmdInitializer() {};
256     //!
257 
258     //!
259     //! \brief    Destructor
260     //!
~CodechalCmdInitializer()261     virtual ~CodechalCmdInitializer() {};
262 
263     //!
264     //! \brief    Free Resources
265     //!
266     virtual void CmdInitializerFreeResources();
267 
268     //!
269     //! \brief    Allocate resources for VP9
270     //!
271     //! \return   MOS_STATUS
272     //!           MOS_STATUS_SUCCESS if success, else fail reason
273     //!
274     virtual MOS_STATUS CmdInitializerAllocateResources(CodechalHwInterface*    m_hwInterface);
275 
276     //!
277     //! \brief    Set all the data of the InputCom of command initializer HuC FW
278     //!
279     //! \return   MOS_STATUS
280     //!           MOS_STATUS_SUCCESS if success, else fail reason
281     //!
282     MOS_STATUS CmdInitializerSetConstData(
283         PMOS_INTERFACE                              osInterface,
284         MhwMiInterface                              *miInterface,
285         MhwVdboxVdencInterface                      *vdencInterface,
286         void*                                       seqParams,
287         void*                                       picParams,
288         void*                                       sliceParams,
289         bool                                        pakOnlyPass,
290         bool                                        acqpEnabled,
291         bool                                        brcEnabled,
292         bool                                        streaminEnabled,
293         bool                                        roiStreamInEnabled,
294         bool                                        roundingEnabled,
295         bool                                        panicEnabled,
296         int32_t                                     currentPass
297         );
298 
299     //!
300     //! \brief    Set DMEM of command initializer HuC FW
301     //!
302     //! \param    [in] brcEnabled
303     //!           Indicate if brc is enabled
304     //!
305     //! \return   MOS_STATUS
306     //!           MOS_STATUS_SUCCESS if success, else fail reason
307     //!
308     virtual MOS_STATUS CmdInitializerSetDmem(bool brcEnabled);
309 
310     //!
311     //! \brief    Executes command initializer HuC FW
312     //!
313     //! \param    [in] brcEnabled
314     //!           Indicate if brc is enabled
315     //! \param    [in] secondlevelBB
316     //!           Second level batch buffer
317     //! \param    [in] cmdBuffer
318     //!           cmdBuffer provided by outside
319     //!
320     //! \return   MOS_STATUS
321     //!           MOS_STATUS_SUCCESS if success, else fail reason
322     //!
323     MOS_STATUS CmdInitializerExecute(
324             bool brcEnabled,
325             PMOS_RESOURCE secondlevelBB,
326             MOS_COMMAND_BUFFER* cmdBuffer = nullptr);
327 
328     //!
329     //! \brief    Set Add Commands to BatchBuffer
330     //!
331     //! \param    [in] commandtype
332     //!           Command type, type 1 or type 2 command
333     //! \param    [in] cmdBuffer
334     //!           Command buffer
335     //! \param    [in] addToBatchBufferHuCBRC
336     //!           Flag to mention if the scenario is BRC or CQP
337     //! \param    [in] isLowDelayB
338     //!           Flag to indicate if it is LDB or not
339     //!
340     //! \return   MOS_STATUS
341     //!           MOS_STATUS_SUCCESS if success, else fail reason
342     //!
343     virtual MOS_STATUS SetAddCommands(uint32_t commandtype, PMOS_COMMAND_BUFFER cmdBuffer, bool addToBatchBufferHuCBRC, uint32_t roundInterValue, uint32_t roundIntraValue, bool isLowDelayB = true, int8_t * pRefIdxMapping = nullptr, int8_t recNotFilteredID = 0)
344     {
345         MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
346         return eStatus;
347     }
348 #ifdef _VP9_ENCODE_VDENC_SUPPORTED
349     //!
350     //! \brief    Set all const VP9 data of the InputCom of command initializer HuC FW
351     //!
352     //! \param    [in] state
353     //!           Pointer to CodechalVdencVp9State
354     //!
355     //! \return   MOS_STATUS
356     //!           MOS_STATUS_SUCCESS if success, else fail reason
357     //!
358     MOS_STATUS CommandInitializerSetVp9Params(CodechalVdencVp9State *state);
359 
360     //!
361     //! \brief    Set DMEM of command initializer HuC FW for VP9
362     //! \return   MOS_STATUS
363     //!           MOS_STATUS_SUCCESS if success, else fail reason
364     //!
365     virtual MOS_STATUS CmdInitializerVp9SetDmem();
366 
367     //!
368     //! \brief    Executes VP9 command initializer HuC FW
369     //!
370     //! \param    [in] cmdBuffer
371     //!           Command buffer
372     //! \param    [in] picStateBuffer
373     //!           Picture state buffer
374     //!
375     //! \return   MOS_STATUS
376     //!           MOS_STATUS_SUCCESS if success, else fail reason
377     //!
378     virtual MOS_STATUS CmdInitializerVp9Execute(PMOS_COMMAND_BUFFER cmdBuffer, PMOS_RESOURCE picStateBuffer);
379 
380     //!
381     //! \brief    Set Add Commands to BatchBuffer for VP9
382     //!
383     //! \param    [in] commandtype
384     //!           Command type, type 1 or type 2 command
385     //!           [in] cmdBuffer
386     //!           Command buffer
387     //!
388     //! \return   MOS_STATUS
389     //!           MOS_STATUS_SUCCESS if success, else fail reason
390     //!
AddCommandsVp9(uint32_t commandtype,PMOS_COMMAND_BUFFER cmdBuffer)391     virtual MOS_STATUS AddCommandsVp9(uint32_t commandtype, PMOS_COMMAND_BUFFER cmdBuffer)
392     {
393         return MOS_STATUS_SUCCESS;
394     }
395 #endif
396 #if USE_CODECHAL_DEBUG_TOOL
397     //!
398     //! \brief    Dump HuC Cmd Initializer
399     //!
400     //! \param    [in] secondlevelBB
401     //!           Kernel output commands is stored in secondlevelBB
402     //!
403     //! \return   MOS_STATUS
404     //!           MOS_STATUS_SUCCESS if success, else fail reason
405     //!
406     MOS_STATUS DumpHucCmdInit(PMOS_RESOURCE secondlevelBB);
407 #endif
408 
409 protected:
410 #if defined (_HEVC_ENCODE_VME_SUPPORTED) || defined (_HEVC_ENCODE_VDENC_SUPPORTED)
411     virtual MOS_STATUS ConstructHevcHucCmd1ConstData(
412         PCODEC_HEVC_ENCODE_SEQUENCE_PARAMS seqParams,
413         PCODEC_HEVC_ENCODE_PICTURE_PARAMS  picParams,
414         PCODEC_HEVC_ENCODE_SLICE_PARAMS    sliceParams,
415         struct HucComData *                hucConstData);
416 
417     virtual MOS_STATUS ConstructHevcHucCmd2ConstData(
418         PCODEC_HEVC_ENCODE_SEQUENCE_PARAMS seqParams,
419         PCODEC_HEVC_ENCODE_PICTURE_PARAMS  picParams,
420         PCODEC_HEVC_ENCODE_SLICE_PARAMS    sliceParams,
421         struct HucComData *                hucConstData);
422 
423     virtual uint16_t GetCmd1StartOffset(bool brcEnabled);
424     virtual uint16_t GetCmd2StartOffset(bool brcEnabled);
425 #endif
426 };
427 using PCODECHAL_CMD_INITIALIZER = class CodechalCmdInitializer*;
428 
429 #endif  //__CODECHAL_CMD_INITIALIZER_H__
430