1 /*
2 * Copyright (c) 2012-2020, 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_decode_vp9.h
24 //! \brief    Defines the decode interface extension for VP9.
25 //! \details  Defines all types, macros, and functions required by CodecHal for VP9 decoding.
26 //!           Definitions are not externally facing.
27 //!
28 
29 #ifndef __CODECHAL_DECODE_VP9_H__
30 #define __CODECHAL_DECODE_VP9_H__
31 
32 #include "codechal_decoder.h"
33 #include "codec_def_vp9_probs.h"
34 
35 //!
36 //! \struct _CODECHAL_DECODE_VP9_PROB_UPDATE
37 //! \brief  Define variables for VP9 decode probabilty updated
38 //!
39 typedef struct _CODECHAL_DECODE_VP9_PROB_UPDATE
40 {
41 
42     int32_t      bSegProbCopy;     //!< seg tree and pred prob update with values from App.
43     int32_t      bProbSave;        //!< Save prob buffer
44     int32_t      bProbRestore;     //!< Restore prob buffer
45     int32_t      bProbReset;       //!<  1: reset; 0: not reset
46     int32_t      bResetFull;       //!< full reset or partial (section A) reset
47     int32_t      bResetKeyDefault; //!<  reset to key or inter default
48     uint8_t      SegTreeProbs[7];  //!< Segment tree prob buffers
49     uint8_t      SegPredProbs[3];  //!< Segment predict prob buffers
50 } CODECHAL_DECODE_VP9_PROB_UPDATE, *PCODECHAL_DECODE_VP9_PROB_UPDATE;
51 
52 //!
53 //! \class CodechalDecodeVp9
54 //! \brief This class defines the member fields, functions etc used by VP9 decoder.
55 //!
56 class CodechalDecodeVp9 : public CodechalDecode
57 {
58 public:
59     //!
60     //! \brief    Constructor
61     //! \param    [in] hwInterface
62     //!           Hardware interface
63     //! \param    [in] debugInterface
64     //!           Debug interface
65     //! \param    [in] standardInfo
66     //!           The information of decode standard for this instance
67     //!
68     CodechalDecodeVp9(
69         CodechalHwInterface   *hwInterface,
70         CodechalDebugInterface* debugInterface,
71         PCODECHAL_STANDARD_INFO standardInfo);
72 
73     //!
74     //! \brief    Destructor
75     //!
76     ~CodechalDecodeVp9();
77 
78     //!
79     //! \brief    Allocate and initialize VP9 decoder standard
80     //! \param    [in] settings
81     //!           Pointer to CodechalSetting
82     //! \return   MOS_STATUS
83     //!           MOS_STATUS_SUCCESS if success, else fail reason
84     //!
85     MOS_STATUS  AllocateStandard (
86         CodechalSetting *          settings) override;
87 
88     //!
89     //! \brief  Set states for each frame to prepare for VP9 decode
90     //! \return MOS_STATUS
91     //!         MOS_STATUS_SUCCESS if success, else fail reason
92     //!
93     MOS_STATUS  SetFrameStates () override;
94 
95     //!
96     //! \brief    VP9 decoder state level function
97     //! \details  State level function for VP9 decoder
98     //!
99     //! \return   MOS_STATUS
100     //!           MOS_STATUS_SUCCESS if success, else fail reason
101     //!
102     MOS_STATUS  DecodeStateLevel () override;
103 
104     //!
105     //! \brief    VP9 decoder primitive level function
106     //! \details  Primitive level function for GEN specific VP9 decoder
107     //!
108     //! \return   MOS_STATUS
109     //!           MOS_STATUS_SUCCESS if success, else fail reason
110     //!
111     MOS_STATUS  DecodePrimitiveLevel () override;
112 
113     MOS_STATUS  InitMmcState() override;
114 
115     //!
116     //! \brief    Init context buffer
117     //! \details
118     //! \param    [in,out] ctxBuffer
119     //!           Pointer to context buffer
120     //!
121     //! \param    [in] setToKey
122     //!           Specify if it's key frame
123     //!
124     //! \return   MOS_STATUS
125     //!           MOS_STATUS_SUCCESS if success, else fail reason
126     //!
127     MOS_STATUS ContextBufferInit(
128             uint8_t *ctxBuffer,
129             bool setToKey);
130 
131     //!
132     //! \brief    Populate prob values which are different between Key and Non-Key frame
133     //! \details
134     //! \param    [in,out] ctxBuffer
135     //!           Pointer to context buffer
136     //!
137     //! \param    [in] setToKey
138     //!           Specify if it's key frame
139     //!
140     //! \return   MOS_STATUS
141     //!           MOS_STATUS_SUCCESS if success, else fail reason
142     //!
143     MOS_STATUS CtxBufDiffInit(
144             uint8_t *ctxBuffer,
145             bool setToKey);
146 
147     //!
148     //! \brief    Intialize VP9 decode mode
149     //! \details  Do nothing for base class, will be overloaded by inheritted class for dynamic mode switch support
150     //!
151     //! \return   MOS_STATUS
152     //!           MOS_STATUS_SUCCESS if success, else fail reason
153     //!
154     virtual MOS_STATUS  InitializeDecodeMode ();
155 
156     //!
157     //! \brief    Check and initialize SFC support and state
158     //! \details  Check whether SFC can be used and initialize SFC if it is supported in VP9 decode
159     //!
160     //! \return   MOS_STATUS
161     //!           MOS_STATUS_SUCCESS if success, else fail reason
162     //!
163     virtual MOS_STATUS InitSfcState();
164 
165     //!
166     //! \brief    Allocate fixed sized resources
167     //! \details  Allocate fixed sized resources VP9 decode driver
168     //! \return   MOS_STATUS
169     //!           MOS_STATUS_SUCCESS if success, else fail reason
170     //!
171     virtual MOS_STATUS AllocateResourcesFixedSizes();
172 
173     //!
174     //! \brief    Allocate variable sized resources
175     //! \details  Allocate variable sized resources in VP9 decode driver
176     //! \return   MOS_STATUS
177     //!           MOS_STATUS_SUCCESS if success, else fail reason
178     //!
179     virtual MOS_STATUS AllocateResourcesVariableSizes();
180 
181     //!
182     //! \brief    Determine Decode Phase
183     //! \details  Determine decode phase in VP9 decode
184     //! \return   MOS_STATUS
185     //!           MOS_STATUS_SUCCESS if success, else fail reason
186     //!
187     virtual MOS_STATUS DetermineDecodePhase();
188 
189     // Parameters passed by application
190     uint16_t                  m_usFrameWidthAlignedMinBlk;   //!< Picture Width aligned to minBlock
191     uint16_t                  m_usFrameHeightAlignedMinBlk;  //!< Picture Height aligned to minBlock
192     uint8_t                   m_vp9DepthIndicator;           //!< Indicate it is 8/10/12 bit VP9
193     uint8_t                   m_chromaFormatinProfile;       //!< Chroma format with current profilce
194     uint32_t                  m_dataSize;                    //!< Data size
195     uint32_t                  m_dataOffset;                  //!< Date offset
196     PCODEC_VP9_PIC_PARAMS     m_vp9PicParams = nullptr;                //!< Pointer to VP9 picture parameter
197     PCODEC_VP9_SEGMENT_PARAMS m_vp9SegmentParams = nullptr;            //!< Pointer to VP9 segment parameter
198     PCODEC_VP9_SLICE_PARAMS   m_vp9SliceParams = nullptr;              //!< Pointer to VP9 slice parameter
199     MOS_SURFACE               m_destSurface;                 //!< MOS_SURFACE of render surface
200     PMOS_RESOURCE             m_presLastRefSurface = nullptr;          //!< Pointer to last reference surface
201     PMOS_RESOURCE             m_presGoldenRefSurface = nullptr;        //!< Pointer to golden reference surface
202     PMOS_RESOURCE             m_presAltRefSurface = nullptr;           //!< Pointer to alternate reference surface
203     MOS_SURFACE               m_lastRefSurface;              //!< MOS_SURFACE of last reference surface
204     MOS_SURFACE               m_goldenRefSurface;            //!< MOS_SURFACE of golden reference surface
205     MOS_SURFACE               m_altRefSurface;               //!< MOS_SURFACE of alternate reference surface
206     MOS_RESOURCE              m_resDataBuffer;               //!< Handle of bitstream data surface
207     MOS_RESOURCE              m_resCoefProbBuffer;           //!< Handle of Coefficient Probability Data surface
208 
209     // Internally maintained
210     uint8_t         m_frameCtxIdx;
211     MOS_RESOURCE    m_resDeblockingFilterLineRowStoreScratchBuffer;         //!< Handle of Deblocking Filter Line Row Store Scratch data surface
212     MOS_RESOURCE    m_resDeblockingFilterTileRowStoreScratchBuffer;         //!< Handle of Deblocking Filter Tile Row Store Scratch data surface
213     MOS_RESOURCE    m_resDeblockingFilterColumnRowStoreScratchBuffer;       //!< Handle of Deblocking Filter Column Row Store Scratch data surface
214     MOS_RESOURCE    m_resMetadataLineBuffer;                                //!< Handle of Metadata Line data buffer
215     MOS_RESOURCE    m_resMetadataTileLineBuffer;                            //!< Handle of Metadata Tile Line data buffer
216     MOS_RESOURCE    m_resMetadataTileColumnBuffer;                          //!< Handle of Metadata Tile Column data buffer
217     MOS_RESOURCE    m_resHvcLineRowstoreBuffer;                             //!< Handle of HVC Line Row Store surface
218     MOS_RESOURCE    m_resHvcTileRowstoreBuffer;                             //!< Handle of HVC Tile Row Store surface
219     MOS_RESOURCE    m_resVp9ProbBuffer[CODEC_VP9_NUM_CONTEXTS + 1];         //!< Handle of VP9 Probability surface
220     MOS_RESOURCE    m_resVp9SegmentIdBuffer;                                //!< Handle of VP9 Segment ID surface
221     MOS_RESOURCE    m_resVp9MvTemporalBuffer[CODECHAL_VP9_NUM_MV_BUFFERS];  //!< Handle of VP9 MV Temporal buffer
222     PCODEC_REF_LIST m_vp9RefList[CODECHAL_NUM_UNCOMPRESSED_SURFACE_VP9];    //!< Pointer to reference list
223     MOS_RESOURCE    m_resSyncObject;                                        //!< Handle of synce object
224     uint8_t         m_curMvTempBufIdx;                                      //!< Current mv temporal buffer index
225     uint8_t         m_colMvTempBufIdx;                                      //!< Colocated mv temporal buffer index
226     MOS_RESOURCE    m_resCopyDataBuffer;                                    //!< Handle of buffer to store copy data
227     uint32_t        m_copyDataBufferSize;                                   //!< Buffer size of copy data
228     uint32_t        m_copyDataOffset;                                       //!< Data offset of copy data
229     bool            m_copyDataBufferInUse;                                  //!< Indicate if copy data buffer is in use
230     MOS_RESOURCE    m_resSyncObjectWaContextInUse;                          //!< signals on the video WA context
231     MOS_RESOURCE    m_resSyncObjectVideoContextInUse;                       //!< signals on the video contex
232     uint32_t        m_hcpDecPhase;                                          //!< Hcp Decode phase
233 
234     union
235     {
236         struct
237         {
238             uint8_t                 KeyFrame            : 1;                    //!< [0..1]
239             uint8_t                 IntraOnly           : 1;                    //!< [0..1]
240             uint8_t                 Display             : 1;                    //!< [0..1]
241             uint8_t                 ReservedField       : 5;                    //!< [0]
242         } fields;
243         uint8_t                     value;
244     } m_prevFrameParams;  //!< Previous frame parameters
245 
246     uint32_t m_prevFrmWidth;         //!< Frame width of the previous frame
247     uint32_t m_prevFrmHeight;        //!< Frame height of the previous frame
248     uint32_t m_allocatedWidthInSb;   //!< Frame width in super block
249     uint32_t m_allocatedHeightInSb;  //!< Frame height in super block
250     uint32_t m_mvBufferSize;         //!< MV buffer size
251 
252     //for Internal buffer upating
253     bool    m_resetSegIdBuffer;                                //!< if segment id buffer need to do reset
254     bool    m_pendingResetPartial;                             //!< indicating if there is pending partial reset operation on prob buffer 0.
255     bool    m_saveInterProbs;                                  //!< indicating if inter probs is saved for prob buffer 0.
256     bool    m_pendingResetFullTables[CODEC_VP9_NUM_CONTEXTS];  //!< indicating if there is pending full frame context table reset operation on each prob buffers except 0.
257     bool    m_pendingCopySegProbs[CODEC_VP9_NUM_CONTEXTS];     //!< indicating if there is pending seg probs copy operation on each prob buffers.
258     uint8_t m_segTreeProbs[7];                                 //!< saved seg tree probs for pending seg probs copy operation to use
259     uint8_t m_segPredProbs[3];                                 //!< saved seg pred probs for pending seg probs copy operation to use
260     bool    m_fullProbBufferUpdate;                            //!< indicating if prob buffer is a full buffer update
261     uint8_t m_interProbSaved[CODECHAL_VP9_INTER_PROB_SIZE];    //!< indicating if internal prob buffer saved
262 
263     uint32_t                        m_dmemBufferSize;          //!< Dmem buffer size
264     MOS_RESOURCE                    m_resDmemBuffer;           //!< Handle of Dmem buffer
265     MOS_RESOURCE                    m_resInterProbSaveBuffer;  //!< Handle of inter prob saved buffer
266     CODECHAL_DECODE_VP9_PROB_UPDATE m_probUpdateFlags;         //!< Prob update flags
267     MOS_RESOURCE                    m_resSegmentIdBuffReset;   //!< Handle of segment Id reset buffer
268     MOS_RESOURCE                    m_resHucSharedBuffer;      //!< Handle of Huc shared buffer
269 
270 protected:
271 
272     //!
273     //! \struct CodechalDecodeVp9:: PIC_STATE_MHW_PARAMS
274     //! \brief  Define MHW parameters for picture state
275     //!
276     typedef struct
277     {
278         PMHW_VDBOX_PIPE_MODE_SELECT_PARAMS   PipeModeSelectParams;
279         PMHW_VDBOX_SURFACE_PARAMS            SurfaceParams[4];
280         PMHW_VDBOX_PIPE_BUF_ADDR_PARAMS      PipeBufAddrParams;
281         PMHW_VDBOX_IND_OBJ_BASE_ADDR_PARAMS  IndObjBaseAddrParams;
282         PMHW_VDBOX_VP9_PIC_STATE             Vp9PicState;
283         PMHW_VDBOX_VP9_SEGMENT_STATE         Vp9SegmentState;
284     } PIC_STATE_MHW_PARAMS;
285 
286     PIC_STATE_MHW_PARAMS            m_picMhwParams;   //!< Picture state params
287 
288     //!
289     //! \brief    VP9 Prob buffer partial update with driver
290     //! \details  using driver to do the parital prob buffer update
291     //!           for clear decode
292     //! \return   MOS_STATUS
293     //!           MOS_STATUS_SUCCESS if success, else fail reason
294     //!
295     MOS_STATUS ProbBufferPartialUpdatewithDrv();
296 
297     //!
298     //! \brief    VP9 Prob buffer full update with driver
299     //! \details  full prob buffer udpate in clear decode
300     //! \return   MOS_STATUS
301     //!           MOS_STATUS_SUCCESS if success, else fail reason
302     //!
303     MOS_STATUS ProbBufFullUpdatewithDrv();
304 
305     //!
306     //! \brief    VP9 Segment id buffer reset with driver
307     //! \details  VP9 Segment id buffer reset with driver in clear decode
308     //! \return   MOS_STATUS
309     //!           MOS_STATUS_SUCCESS if success, else fail reason
310     //!
311     MOS_STATUS ResetSegIdBufferwithDrv();
312 
313     //!
314     //! \brief    VP9 Prob buffer full update with Huc Streamout command
315     //! \details  VP9 prob buffer full update with Huc Streamout
316     //! \param    cmdBuffer
317     //!           [in] command buffer to hold HW commands
318     //! \return   MOS_STATUS
319     //!           MOS_STATUS_SUCCESS if success, else fail reason
320     //!
321     virtual MOS_STATUS ProbBufFullUpdatewithHucStreamout(
322         PMOS_COMMAND_BUFFER cmdBuffer);
323 
324     //!
325     //! \brief    VP9 Segment Id buffer reset with HucStreamout command
326     //! \details  vp9 segment id buffer reset using Huc Streamout command
327     //! \param    cmdBuffer
328     //!           [in] command buffer to hold HW commands
329     //! \return   MOS_STATUS
330     //!           MOS_STATUS_SUCCESS if success, else fail reason
331     //!
332     MOS_STATUS ResetSegIdBufferwithHucStreamout(
333         PMOS_COMMAND_BUFFER cmdBuffer);
334 
335     //!
336     //! \brief    Determine the internal bufer update
337     //! \details  Decide if vp9 prob buffer or segment id buffer need to do
338     //!           update and what updat type it is
339     //! \return   MOS_STATUS
340     //!           MOS_STATUS_SUCCESS if success, else fail reason
341     //!
342     MOS_STATUS DetermineInternalBufferUpdate();
343 
344     //!
345     //! \brief    Initialize during begin frame
346     //! \details  Initialize during begin frame in VP9 decode driver
347     //! \return   MOS_STATUS
348     //!           MOS_STATUS_SUCCESS if success, else fail reason
349     //!
350     MOS_STATUS InitializeBeginFrame();
351 
352     //!
353     //! \brief    Copy data surface
354     //! \details  Copy data surface in VP9 decode driver
355     //! \return   MOS_STATUS
356     //!           MOS_STATUS_SUCCESS if success, else fail reason
357     //!
358     MOS_STATUS CopyDataSurface();
359 
360     //!
361     //! \brief    Check and copy bit stream
362     //! \details  Check and copy bit stream in VP9 decode driver
363     //! \return   MOS_STATUS
364     //!           MOS_STATUS_SUCCESS if success, else fail reason
365     //!
366     MOS_STATUS CheckAndCopyBitStream();
367 
368     //!
369     //! \brief    Initialize picture state mhw parameters
370     //! \details  Initialize picture state mhw parameters for VP9 decoder
371     //!
372     //! \return   MOS_STATUS
373     //!           MOS_STATUS_SUCCESS if success, else fail reason
374     //!
375     MOS_STATUS          InitPicStateMhwParams();
376 
377     //!
378     //! \brief    Add picture state mhw parameters
379     //! \details  Add picture state mhw parameters to command buffer for VP9 decoder
380     //! \param    [in] cmdBuffer
381     //!           Command buffer to hold HW commands
382     //!
383     //! \return   MOS_STATUS
384     //!           MOS_STATUS_SUCCESS if success, else fail reason
385     //!
386     virtual MOS_STATUS  AddPicStateMhwCmds(
387         PMOS_COMMAND_BUFFER       cmdBuffer
388     );
389 
390     //!
391     //! \brief    Update picture state buffers
392     //! \details  Update picture state buffers for VP9 decoder
393     //! \param    [in] cmdBuffer
394     //!           Command buffer to hold HW commands
395     //!
396     //! \return   MOS_STATUS
397     //!           MOS_STATUS_SUCCESS if success, else fail reason
398     //!
399     MOS_STATUS UpdatePicStateBuffers(
400         PMOS_COMMAND_BUFFER       cmdBuffer);
401 
402 #if USE_CODECHAL_DEBUG_TOOL
403     MOS_STATUS DumpDecodePicParams(
404         PCODEC_VP9_PIC_PARAMS picParams);
405 
406     MOS_STATUS DumpDecodeSegmentParams(
407         PCODEC_VP9_SEGMENT_PARAMS segmentParams);
408 #endif
409 };
410 
411 #endif  // __CODECHAL_DECODE_VP9_H__
412