1 /*
2 * Copyright (c) 2019-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_av1_reference_frames_g12.h
24 //! \brief    Defines reference list related logic for av1 decode
25 //!
26 #ifndef __DECODE_AV1_REFERENCE_FRAMES_G12_H__
27 #define __DECODE_AV1_REFERENCE_FRAMES_G12_H__
28 
29 #include "codec_def_decode_av1.h"
30 #include "mhw_vdbox.h"
31 #include "decode_allocator.h"
32 
33 namespace decode
34 {
35 class Av1BasicFeatureG12;
36 
37 class Av1ReferenceFramesG12
38 {
39 public:
40     //!
41     //! \brief  Av1ReferenceFramesG12 constructor
42     //!
43     Av1ReferenceFramesG12();
44 
45     //!
46     //! \brief  Av1ReferenceFramesG12 deconstructor
47     //!
48     ~Av1ReferenceFramesG12();
49 
50     //!
51     //! \brief  Init Av1 reference frames
52     //! \param  [in] basicFeature
53     //!         Pointer to Av1 basic feature
54     //! \return  MOS_STATUS
55     //!         MOS_STATUS_SUCCESS if success, else fail reason
56     //!
57     MOS_STATUS Init(Av1BasicFeatureG12 *basicFeature, DecodeAllocator& allocator);
58 
59     //!
60     //! \brief  Update reference frames for picture
61     //! \param  [in] picParams
62     //!         Picture parameters
63     //! \return  MOS_STATUS
64     //!         MOS_STATUS_SUCCESS if success, else fail reason
65     //!
66     MOS_STATUS UpdatePicture(CodecAv1PicParams & picParams);
67 
68     //!
69     //! \brief  Insert one anchor frame for Large Scale Tile decoding
70     //! \param  [in] picParams
71     //!         Picture parameters
72     //! \return  MOS_STATUS
73     //!         MOS_STATUS_SUCCESS if success, else fail reason
74     //!
75     MOS_STATUS InsertAnchorFrame(CodecAv1PicParams & picParams);
76 
77     //!
78     //! \brief  Get active reference list for current frame
79     //! \param  [in] picParams
80     //!         Picture parameters
81     //! \param  [in] tileParams
82     //!         Tile parameters
83     //! \return  std::vector<uint8_t> &
84     //!         Active reference list indices for current frame
85     //!
86     const std::vector<uint8_t> &GetActiveReferenceList(CodecAv1PicParams &picParams, CodecAv1TileParams &tileParams);
87 
88     //!
89     //! \brief  Get active reference list for current frame
90     //! \param  [in] frameIndex
91     //!         Frame index for reference
92     //! \return  PMOS_RESOURCE
93     //!         Active reference list for current frame
94     //!
95     PMOS_RESOURCE GetReferenceByFrameIndex(uint8_t frameIndex);
96 
97     //!
98     //! \brief  Get valid reference for error concealment.
99     //! \return  PMOS_RESOURCE
100     //!         Valid reference resource
101     //!
102     PMOS_RESOURCE GetValidReference();
103 
104     //!
105     //! \brief  Get valid reference index for error concealment.
106     //! \param  [out] uint8_t
107     //!         Valid reference resource index
108     //! \return   MOS_STATUS
109     //!           MOS_STATUS_SUCCESS if success, else fail reason
110     MOS_STATUS GetValidReferenceIndex(uint8_t *validRefIndex);
111 
112     //! \brief    Identify the first nearest reference frame
113     //! \return   MOS_STATUS
114     //!           MOS_STATUS_SUCCESS if success, else fail reason
115     //!
116     MOS_STATUS Identify1stNearRef(const CodecAv1PicParams & picParams,
117         int32_t curFrameOffset,int32_t* refFrameOffset,int32_t* refIdx);
118 
119     //! \brief    Identify the second nearest reference frame
120     //! \return   MOS_STATUS
121     //!           MOS_STATUS_SUCCESS if success, else fail reason
122     //!
123     MOS_STATUS Identify2ndNearRef(const CodecAv1PicParams & picParams,
124         int32_t curFrameOffset,int32_t* refFrameOffset,int32_t* refIdx);
125 
126     //!
127     //! \brief    Calculate the relative distance between a and b
128     //! \return   Distance
129     //!           Relative distance between a and b
130     //!
131     int32_t GetRelativeDist(const CodecAv1PicParams & picParams, int32_t a, int32_t b);
132 
133     //!
134     //! \brief    Setup Motion Field for each reference
135     //! \return   MOS_STATUS
136     //!           MOS_STATUS_SUCCESS if success, else fail reason
137     //!
138     MOS_STATUS SetupMotionFieldProjection(CodecAv1PicParams & picParams);
139 
140     //!
141     //! \brief    Setup Motion Field for each reference
142     //! \return   MOS_STATUS
143     //!           MOS_STATUS_SUCCESS if success, else fail reason
144     //!
145     bool MotionFieldProjection(CodecAv1PicParams & picParams, int32_t ref, int32_t dir);
146 
147     //!
148     //! \brief    Check Segmentation for primary reference frame
149     //! \return   MOS_STATUS
150     //!           MOS_STATUS_SUCCESS if success, else fail reason
151     //!
152     bool CheckSegForPrimFrame(CodecAv1PicParams & picParams);
153 
154     //!
155     //! \brief    Get Primary Reference Frame Index
156     //! \return   Frame Index
157     //!           Primary Reference Frame Index
158     //!
GetPrimaryRefIdx()159     uint8_t GetPrimaryRefIdx() {return m_prevFrameIdx;};
160 
161     //!
162     //! \brief  Update the current frame entry on m_refList
163     //! \param  [in] picParams
164     //!         Picture parameters
165     //! \return  MOS_STATUS
166     //!         MOS_STATUS_SUCCESS if success, else fail reason
167     //!
168     MOS_STATUS UpdateCurFrame(const CodecAv1PicParams &picParams);
169 
170     //!
171     //! \brief  Error detect and concealment for reference list for picture
172     //! \param  [in] picParams
173     //!         Picture parameters
174     //! \return  MOS_STATUS
175     //!         MOS_STATUS_SUCCESS if success, else fail reason
176     //!
177     MOS_STATUS ErrorConcealment(CodecAv1PicParams &picParams);
178 
179     //!
180     //! \brief  Update the reference list for current frame
181     //! \param  [in] picParams
182     //!         Picture parameters
183     //! \return MOS_STATUS
184     //!         MOS_STATUS_SUCCESS if success, else fail reason
185     //!
186     MOS_STATUS UpdateCurRefList(const CodecAv1PicParams &picParams);
187 
188     //!
189     //! \brief  Update the current resource for currnet ref list
190     //! \param  [in] pCurRefList
191     //!         pointer of current ref list
192     //! \return  MOS_STATUS
193     //!         MOS_STATUS_SUCCESS if success, else fail reason
194     //!
195     MOS_STATUS             UpdateCurResource(const PCODEC_REF_LIST_AV1 pCurRefList);
196 
197     PCODEC_REF_LIST_AV1    m_refList[CODECHAL_MAX_DPB_NUM_LST_AV1]; //!< Pointer to reference list, actually the DPB
198     PCODEC_REF_LIST_AV1    m_currRefList = nullptr;                 //!< Current frame reference list
199 
200 protected:
201 
202     uint8_t m_prevFrameIdx = 0;                    //!< primary reference frame index
203     Av1BasicFeatureG12 *m_basicFeature = nullptr;  //!< AV1 basic feature
204     DecodeAllocator *m_allocator = nullptr;        //!< Decode allocator
205     std::vector<uint8_t> m_activeReferenceList;    //!< Active reference list of current picture
206 MEDIA_CLASS_DEFINE_END(decode__Av1ReferenceFramesG12)
207 };
208 
209 }  // namespace decode
210 
211 #endif  // !__DECODE_AV1_REFERENCE_FRAMES_G12_H__
212