1 /*
2 * Copyright (c) 2012-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_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     //!
190     //! \brief  Sets cenc decoder batch buffer
191     //! \param    [in] cmdBuffer
192     //!           Pointer of command buffer.
193     //! \return   MOS_STATUS
194     //!           MOS_STATUS_SUCCESS if success, else fail reason
195     //!
196     virtual MOS_STATUS SetCencBatchBuffer( PMOS_COMMAND_BUFFER cmdBuffer) override;
197 
198     // Parameters passed by application
199     uint16_t                  m_usFrameWidthAlignedMinBlk;   //!< Picture Width aligned to minBlock
200     uint16_t                  m_usFrameHeightAlignedMinBlk;  //!< Picture Height aligned to minBlock
201     uint8_t                   m_vp9DepthIndicator;           //!< Indicate it is 8/10/12 bit VP9
202     uint8_t                   m_chromaFormatinProfile;       //!< Chroma format with current profilce
203     uint32_t                  m_dataSize;                    //!< Data size
204     uint32_t                  m_dataOffset;                  //!< Date offset
205     PCODEC_VP9_PIC_PARAMS     m_vp9PicParams = nullptr;                //!< Pointer to VP9 picture parameter
206     PCODEC_VP9_SEGMENT_PARAMS m_vp9SegmentParams = nullptr;            //!< Pointer to VP9 segment parameter
207     PCODEC_VP9_SLICE_PARAMS   m_vp9SliceParams = nullptr;              //!< Pointer to VP9 slice parameter
208     MOS_SURFACE               m_destSurface;                 //!< MOS_SURFACE of render surface
209     PMOS_RESOURCE             m_presLastRefSurface = nullptr;          //!< Pointer to last reference surface
210     PMOS_RESOURCE             m_presGoldenRefSurface = nullptr;        //!< Pointer to golden reference surface
211     PMOS_RESOURCE             m_presAltRefSurface = nullptr;           //!< Pointer to alternate reference surface
212     MOS_SURFACE               m_lastRefSurface;              //!< MOS_SURFACE of last reference surface
213     MOS_SURFACE               m_goldenRefSurface;            //!< MOS_SURFACE of golden reference surface
214     MOS_SURFACE               m_altRefSurface;               //!< MOS_SURFACE of alternate reference surface
215     MOS_RESOURCE              m_resDataBuffer;               //!< Handle of bitstream data surface
216     MOS_RESOURCE              m_resCoefProbBuffer;           //!< Handle of Coefficient Probability Data surface
217 
218     // Internally maintained
219     uint8_t         m_frameCtxIdx;
220     MOS_RESOURCE    m_resDeblockingFilterLineRowStoreScratchBuffer;         //!< Handle of Deblocking Filter Line Row Store Scratch data surface
221     MOS_RESOURCE    m_resDeblockingFilterTileRowStoreScratchBuffer;         //!< Handle of Deblocking Filter Tile Row Store Scratch data surface
222     MOS_RESOURCE    m_resDeblockingFilterColumnRowStoreScratchBuffer;       //!< Handle of Deblocking Filter Column Row Store Scratch data surface
223     MOS_RESOURCE    m_resMetadataLineBuffer;                                //!< Handle of Metadata Line data buffer
224     MOS_RESOURCE    m_resMetadataTileLineBuffer;                            //!< Handle of Metadata Tile Line data buffer
225     MOS_RESOURCE    m_resMetadataTileColumnBuffer;                          //!< Handle of Metadata Tile Column data buffer
226     MOS_RESOURCE    m_resHvcLineRowstoreBuffer;                             //!< Handle of HVC Line Row Store surface
227     MOS_RESOURCE    m_resHvcTileRowstoreBuffer;                             //!< Handle of HVC Tile Row Store surface
228     MOS_RESOURCE    m_resVp9ProbBuffer[CODEC_VP9_NUM_CONTEXTS + 1];         //!< Handle of VP9 Probability surface
229     MOS_RESOURCE    m_resVp9SegmentIdBuffer;                                //!< Handle of VP9 Segment ID surface
230     MOS_RESOURCE    m_resVp9MvTemporalBuffer[CODECHAL_VP9_NUM_MV_BUFFERS];  //!< Handle of VP9 MV Temporal buffer
231     PCODEC_REF_LIST m_vp9RefList[CODECHAL_NUM_UNCOMPRESSED_SURFACE_VP9];    //!< Pointer to reference list
232     MOS_RESOURCE    m_resSyncObject;                                        //!< Handle of synce object
233     uint8_t         m_curMvTempBufIdx;                                      //!< Current mv temporal buffer index
234     uint8_t         m_colMvTempBufIdx;                                      //!< Colocated mv temporal buffer index
235     MOS_RESOURCE    m_resCopyDataBuffer;                                    //!< Handle of buffer to store copy data
236     uint32_t        m_copyDataBufferSize;                                   //!< Buffer size of copy data
237     uint32_t        m_copyDataOffset;                                       //!< Data offset of copy data
238     bool            m_copyDataBufferInUse;                                  //!< Indicate if copy data buffer is in use
239     MOS_RESOURCE    m_resSyncObjectWaContextInUse;                          //!< signals on the video WA context
240     MOS_RESOURCE    m_resSyncObjectVideoContextInUse;                       //!< signals on the video contex
241     uint32_t        m_hcpDecPhase;                                          //!< Hcp Decode phase
242 
243     union
244     {
245         struct
246         {
247             uint8_t                 KeyFrame            : 1;                    //!< [0..1]
248             uint8_t                 IntraOnly           : 1;                    //!< [0..1]
249             uint8_t                 Display             : 1;                    //!< [0..1]
250             uint8_t                 ReservedField       : 5;                    //!< [0]
251         } fields;
252         uint8_t                     value;
253     } m_prevFrameParams;  //!< Previous frame parameters
254 
255     uint32_t m_prevFrmWidth;         //!< Frame width of the previous frame
256     uint32_t m_prevFrmHeight;        //!< Frame height of the previous frame
257     uint32_t m_allocatedWidthInSb;   //!< Frame width in super block
258     uint32_t m_allocatedHeightInSb;  //!< Frame height in super block
259     uint32_t m_mvBufferSize;         //!< MV buffer size
260 
261     //for Internal buffer upating
262     bool    m_resetSegIdBuffer;                                //!< if segment id buffer need to do reset
263     bool    m_pendingResetPartial;                             //!< indicating if there is pending partial reset operation on prob buffer 0.
264     bool    m_saveInterProbs;                                  //!< indicating if inter probs is saved for prob buffer 0.
265     bool    m_pendingResetFullTables[CODEC_VP9_NUM_CONTEXTS];  //!< indicating if there is pending full frame context table reset operation on each prob buffers except 0.
266     bool    m_pendingCopySegProbs[CODEC_VP9_NUM_CONTEXTS];     //!< indicating if there is pending seg probs copy operation on each prob buffers.
267     uint8_t m_segTreeProbs[7];                                 //!< saved seg tree probs for pending seg probs copy operation to use
268     uint8_t m_segPredProbs[3];                                 //!< saved seg pred probs for pending seg probs copy operation to use
269     bool    m_fullProbBufferUpdate;                            //!< indicating if prob buffer is a full buffer update
270     uint8_t m_interProbSaved[CODECHAL_VP9_INTER_PROB_SIZE];    //!< indicating if internal prob buffer saved
271 
272     uint32_t                        m_dmemBufferSize;          //!< Dmem buffer size
273     MOS_RESOURCE                    m_resDmemBuffer;           //!< Handle of Dmem buffer
274     MOS_RESOURCE                    m_resInterProbSaveBuffer;  //!< Handle of inter prob saved buffer
275     CODECHAL_DECODE_VP9_PROB_UPDATE m_probUpdateFlags;         //!< Prob update flags
276     MOS_RESOURCE                    m_resSegmentIdBuffReset;   //!< Handle of segment Id reset buffer
277     MOS_RESOURCE                    m_resHucSharedBuffer;      //!< Handle of Huc shared buffer
278 
279 protected:
280 
281     //!
282     //! \struct CodechalDecodeVp9:: PIC_STATE_MHW_PARAMS
283     //! \brief  Define MHW parameters for picture state
284     //!
285     typedef struct
286     {
287         PMHW_VDBOX_PIPE_MODE_SELECT_PARAMS   PipeModeSelectParams;
288         PMHW_VDBOX_SURFACE_PARAMS            SurfaceParams[4];
289         PMHW_VDBOX_PIPE_BUF_ADDR_PARAMS      PipeBufAddrParams;
290         PMHW_VDBOX_IND_OBJ_BASE_ADDR_PARAMS  IndObjBaseAddrParams;
291         PMHW_VDBOX_VP9_PIC_STATE             Vp9PicState;
292         PMHW_VDBOX_VP9_SEGMENT_STATE         Vp9SegmentState;
293     } PIC_STATE_MHW_PARAMS;
294 
295     PIC_STATE_MHW_PARAMS            m_picMhwParams;   //!< Picture state params
296 
297     //!
298     //! \brief    VP9 Prob buffer partial update with driver
299     //! \details  using driver to do the parital prob buffer update
300     //!           for clear decode
301     //! \return   MOS_STATUS
302     //!           MOS_STATUS_SUCCESS if success, else fail reason
303     //!
304     MOS_STATUS ProbBufferPartialUpdatewithDrv();
305 
306     //!
307     //! \brief    VP9 Prob buffer full update with driver
308     //! \details  full prob buffer udpate in clear decode
309     //! \return   MOS_STATUS
310     //!           MOS_STATUS_SUCCESS if success, else fail reason
311     //!
312     MOS_STATUS ProbBufFullUpdatewithDrv();
313 
314     //!
315     //! \brief    VP9 Segment id buffer reset with driver
316     //! \details  VP9 Segment id buffer reset with driver in clear decode
317     //! \return   MOS_STATUS
318     //!           MOS_STATUS_SUCCESS if success, else fail reason
319     //!
320     MOS_STATUS ResetSegIdBufferwithDrv();
321 
322     //!
323     //! \brief    VP9 Prob buffer full update with Huc Streamout command
324     //! \details  VP9 prob buffer full update with Huc Streamout
325     //! \param    cmdBuffer
326     //!           [in] command buffer to hold HW commands
327     //! \return   MOS_STATUS
328     //!           MOS_STATUS_SUCCESS if success, else fail reason
329     //!
330     virtual MOS_STATUS ProbBufFullUpdatewithHucStreamout(
331         PMOS_COMMAND_BUFFER cmdBuffer);
332 
333     //!
334     //! \brief    VP9 Segment Id buffer reset with HucStreamout command
335     //! \details  vp9 segment id buffer reset using Huc Streamout command
336     //! \param    cmdBuffer
337     //!           [in] command buffer to hold HW commands
338     //! \return   MOS_STATUS
339     //!           MOS_STATUS_SUCCESS if success, else fail reason
340     //!
341     MOS_STATUS ResetSegIdBufferwithHucStreamout(
342         PMOS_COMMAND_BUFFER cmdBuffer);
343 
344     //!
345     //! \brief    Determine the internal bufer update
346     //! \details  Decide if vp9 prob buffer or segment id buffer need to do
347     //!           update and what updat type it is
348     //! \return   MOS_STATUS
349     //!           MOS_STATUS_SUCCESS if success, else fail reason
350     //!
351     MOS_STATUS DetermineInternalBufferUpdate();
352 
353     //!
354     //! \brief    Initialize during begin frame
355     //! \details  Initialize during begin frame in VP9 decode driver
356     //! \return   MOS_STATUS
357     //!           MOS_STATUS_SUCCESS if success, else fail reason
358     //!
359     MOS_STATUS InitializeBeginFrame();
360 
361     //!
362     //! \brief    Copy data surface
363     //! \details  Copy data surface in VP9 decode driver
364     //! \return   MOS_STATUS
365     //!           MOS_STATUS_SUCCESS if success, else fail reason
366     //!
367     MOS_STATUS CopyDataSurface();
368 
369     //!
370     //! \brief    Check and copy bit stream
371     //! \details  Check and copy bit stream in VP9 decode driver
372     //! \return   MOS_STATUS
373     //!           MOS_STATUS_SUCCESS if success, else fail reason
374     //!
375     MOS_STATUS CheckAndCopyBitStream();
376 
377     //!
378     //! \brief    Initialize picture state mhw parameters
379     //! \details  Initialize picture state mhw parameters for VP9 decoder
380     //!
381     //! \return   MOS_STATUS
382     //!           MOS_STATUS_SUCCESS if success, else fail reason
383     //!
384     MOS_STATUS          InitPicStateMhwParams();
385 
386     //!
387     //! \brief    Add picture state mhw parameters
388     //! \details  Add picture state mhw parameters to command buffer for VP9 decoder
389     //! \param    [in] cmdBuffer
390     //!           Command buffer to hold HW commands
391     //!
392     //! \return   MOS_STATUS
393     //!           MOS_STATUS_SUCCESS if success, else fail reason
394     //!
395     virtual MOS_STATUS  AddPicStateMhwCmds(
396         PMOS_COMMAND_BUFFER       cmdBuffer
397     );
398 
399     //!
400     //! \brief    Update picture state buffers
401     //! \details  Update picture state buffers for VP9 decoder
402     //! \param    [in] cmdBuffer
403     //!           Command buffer to hold HW commands
404     //!
405     //! \return   MOS_STATUS
406     //!           MOS_STATUS_SUCCESS if success, else fail reason
407     //!
408     MOS_STATUS UpdatePicStateBuffers(
409         PMOS_COMMAND_BUFFER       cmdBuffer);
410 
411 #if USE_CODECHAL_DEBUG_TOOL
412     MOS_STATUS DumpDecodePicParams(
413         PCODEC_VP9_PIC_PARAMS picParams);
414 
415     MOS_STATUS DumpDecodeSegmentParams(
416         PCODEC_VP9_SEGMENT_PARAMS segmentParams);
417 #endif
418 };
419 
420 #endif  // __CODECHAL_DECODE_VP9_H__
421