1 /*
2 * Copyright (c) 2021, 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     decode_vp9_basic_feature.h
24 //! \brief    Defines the common interface for decode vp9 basic feature
25 //!
26 #ifndef __DECODE_VP9_BASIC_FEATURE_H__
27 #define __DECODE_VP9_BASIC_FEATURE_H__
28 
29 #include "decode_basic_feature.h"
30 #include "codec_def_decode_vp9.h"
31 #include "codec_def_vp9_probs.h"
32 #include "decode_vp9_reference_frames.h"
33 
34 namespace decode
35 {
36 //!
37 //! \struct _CODECHAL_DECODE_VP9_PROB_UPDATE
38 //! \brief  Define variables for VP9 decode probabilty updated
39 //!
40 typedef struct _CODECHAL_DECODE_VP9_PROB_UPDATE
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 class Vp9BasicFeature : public DecodeBasicFeature
53 {
54 public:
55     //!
56     //! \brief  Vp9BasicFeature constructor
57     //!
58     Vp9BasicFeature(DecodeAllocator *allocator, void *hwInterface, PMOS_INTERFACE osInterface);
59 
60     //!
61     //! \brief  Vp9BasicFeature deconstructor
62     //!
63     virtual ~Vp9BasicFeature();
64 
65     //!
66     //! \brief  Initialize vp9 basic feature CodechalSetting
67     //! \return MOS_STATUS
68     //!         MOS_STATUS_SUCCESS if success, else fail reason
69     //!
70     virtual MOS_STATUS Init(void *setting) override;
71 
72     //!
73     //! \brief  Update vp9 decodeParams
74     //! \return MOS_STATUS
75     //!         MOS_STATUS_SUCCESS if success, else fail reason
76     //!
77     virtual MOS_STATUS Update(void *params) override;
78 
79     MOS_STATUS DetermineInternalBufferUpdate();
80 
81     MOS_STATUS InitDefaultProbBufferTable();
82 
83     MOS_STATUS AllocateVP9MVBuffer();
84 
85     MOS_STATUS AllocateSegmentBuffer();
86 
87     // Parameters passed from application
88     uint16_t                  m_frameWidthAlignedMinBlk  = 0;        //!< Picture Width aligned to minBlock
89     uint16_t                  m_frameHeightAlignedMinBlk = 0;        //!< Picture Height aligned to minBlock
90     uint8_t                   m_vp9DepthIndicator        = 0;        //!< Indicate it is 8/10/12 bit VP9
91     CODEC_VP9_PIC_PARAMS *    m_vp9PicParams             = nullptr;  //!< Pointer to VP9 picture parameter
92     CODEC_VP9_SEGMENT_PARAMS *m_vp9SegmentParams         = nullptr;  //!< Pointer to VP9 segments parameter
93     CODEC_VP9_SLICE_PARAMS *  m_vp9SliceParams           = nullptr;  //!< Pointer to Vp9 slice parameter
94     Vp9ReferenceFrames        m_refFrames;                           //!< Reference frames
95 
96     uint16_t m_prevFrmWidth        = 0;      //!< Frame width of the previous frame
97     uint16_t m_prevFrmHeight       = 0;      //!< Frame height of the previous frame
98     uint16_t m_allocatedWidthInSb  = 0;      //!< Frame width in super block
99     uint16_t m_allocatedHeightInSb = 0;      //!< Frame height in super block
100     bool     m_resetSegIdBuffer    = false;  //!< if segment id buffer need to do reset
101     bool     m_pendingResetPartial = false;  //!< indicating if there is pending partial reset operation on prob buffer 0.
102 
103     PMOS_BUFFER m_resVp9SegmentIdBuffer;  //!< Handle of VP9 Segment ID surface
104 
105     PMOS_BUFFER m_resVp9ProbBuffer[CODEC_VP9_NUM_CONTEXTS + 1]  = {};
106     PMOS_BUFFER m_resVp9MvTemporalBuffer[CODECHAL_VP9_NUM_MV_BUFFERS] = {};  //!< Handle of VP9 MV Temporal buffer
107     uint8_t     m_curMvTempBufIdx = 0;                                               //!< Current mv temporal buffer index
108     uint8_t     m_colMvTempBufIdx = 0;                                               //!< Colocated mv temporal buffer index
109 
110     PMOS_RESOURCE m_presLastRefSurface   = nullptr;  //!< Pointer to last reference surface
111     PMOS_RESOURCE m_presGoldenRefSurface = nullptr;  //!< Pointer to golden reference surface
112     PMOS_RESOURCE m_presAltRefSurface    = nullptr;  //!< Pointer to alternate reference surface
113     MOS_SURFACE   m_lastRefSurface;                  //!< MOS_SURFACE of last reference surface
114     MOS_SURFACE   m_goldenRefSurface;                //!< MOS_SURFACE of golden reference surface
115     MOS_SURFACE   m_altRefSurface;                   //!< MOS_SURFACE of alternate reference surface
116 
117     // Internally maintained
118     uint8_t                         m_frameCtxIdx          = 0;
119     bool                            m_fullProbBufferUpdate = false;  //!< indicating if prob buffer is a full buffer update
120     CODECHAL_DECODE_VP9_PROB_UPDATE m_probUpdateFlags;               //!< Prob update flags
121 
122     bool    m_pendingCopySegProbs[CODEC_VP9_NUM_CONTEXTS];     //!< indicating if there is pending seg probs copy operation on each prob buffers.
123     uint8_t m_segTreeProbs[7];                                 //!< saved seg tree probs for pending seg probs copy operation to use
124     uint8_t m_segPredProbs[3];                                 //!< saved seg pred probs for pending seg probs copy operation to use
125     bool    m_pendingResetFullTables[CODEC_VP9_NUM_CONTEXTS];  //!< indicating if there is pending full frame context table reset operation on each prob buffers except 0.
126     bool    m_saveInterProbs = false;                          //!< indicating if inter probs is saved for prob buffer 0.
127     uint8_t m_interProbSaved[CODECHAL_VP9_INTER_PROB_SIZE];    //!< indicating if internal prob buffer saved
128 
129     PMOS_INTERFACE m_osInterface = nullptr;
130     bool           m_secureMode  = false;
131 
132     union
133     {
134         struct
135         {
136             uint8_t KeyFrame : 1;       //!< [0..1]
137             uint8_t IntraOnly : 1;      //!< [0..1]
138             uint8_t Display : 1;        //!< [0..1]
139             uint8_t ReservedField : 5;  //!< [0]
140         } fields;
141         uint8_t value;
142     } m_prevFrameParams;  //!< Previous frame parameters
143 
144 protected:
145     virtual MOS_STATUS SetRequiredBitstreamSize(uint32_t requiredSize) override;
146     MOS_STATUS         SetPictureStructs();
147     MOS_STATUS         SetSegmentData();
148 
149     //! \brief Hcp Interface
150     std::shared_ptr<mhw::vdbox::hcp::Itf> m_hcpItf = nullptr;
151 
152 MEDIA_CLASS_DEFINE_END(decode__Vp9BasicFeature)
153 };
154 
155 }  // namespace decode
156 
157 #endif  // !__DECODE_VP9_BASIC_FEATURE_H__
158