1 /*
2 * Copyright (c) 2020-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     encode_avc_reference_frames.h
24 //! \brief    Defines reference list related logic for encode avc
25 //!
26 #ifndef __ENCODE_AVC_REFERENCE_FRAMES_H__
27 #define __ENCODE_AVC_REFERENCE_FRAMES_H__
28 
29 #include "codec_def_encode_avc.h"
30 #include "mhw_vdbox.h"
31 #include "encode_allocator.h"
32 #include "mhw_vdbox_vdenc_itf.h"
33 #include "mhw_vdbox_mfx_itf.h"
34 
35 namespace encode
36 {
37 
38 class AvcBasicFeature;
39 
40 class AvcReferenceFrames : public mhw::vdbox::vdenc::Itf::ParSetting, public mhw::vdbox::mfx::Itf::ParSetting
41 {
42 public:
43 
44     //!
45     //! \brief  AvcReferenceFrames constructor
46     //!
AvcReferenceFrames()47     AvcReferenceFrames() {}
48 
49     //!
50     //! \brief  AvcReferenceFrames deconstructor
51     //!
52     virtual ~AvcReferenceFrames();
53 
54     //!
55     //! \brief  Initialize reference frame
56     //! \param  [in] params
57     //!         Pointer to AvcBasicFeature
58     //! \return  MOS_STATUS
59     //!         MOS_STATUS_SUCCESS if success, else fail reason
60     //!
61     MOS_STATUS Init(AvcBasicFeature *basicFeature, EncodeAllocator *allocator);
62 
63     //!
64     //! \brief  Update reference frame for picture
65     //! \return  MOS_STATUS
66     //!         MOS_STATUS_SUCCESS if success, else fail reason
67     //!
68     MOS_STATUS UpdatePicture();
69 
70     //!
71     //! \brief  Update reference frame for slice
72     //! \return  MOS_STATUS
73     //!         MOS_STATUS_SUCCESS if success, else fail reason
74     //!
75     MOS_STATUS UpdateSlice();
76 
77     //!
78     //! \brief  Get picture index
79     //! \param  [in] idx
80     //!         Index of pic index
81     //! \return  CODEC_PIC_ID
82     //!         CODEC_PIC_ID refer to the picure index
83     //!
GetPicIndex()84     PCODEC_PIC_ID GetPicIndex() { return m_picIdx; };
85 
86     //!
87     //! \brief  Get reference list index
88     //! \param  [in] idx
89     //!         Index of pic index
90     //! \return  uint8_t
91     //!         reference list index
92     //!
GetRefListIndex(uint8_t idx)93     uint8_t GetRefListIndex(uint8_t idx) { return m_picIdx[idx].ucPicIdx; };
94 
95     //!
96     //! \brief  Get current reference list
97     //! \return  PCODEC_REF_LIST
98     //!         Pointer of current reference list
99     //!
GetCurrRefList()100     PCODEC_REF_LIST GetCurrRefList() { return m_currRefList; };
101 
102     //!
103     //! \brief  Get reference list
104     //! \return  PCODEC_REF_LIST *
105     //!         Pointer of current reference list
106     //!
GetRefList()107     PCODEC_REF_LIST *GetRefList() { return m_refList; };
108 
109     //!
110     //! \brief  Get picture coding type
111     //! \return  uint16_t
112     //!         Picture coding type
113     //!
GetPictureCodingType()114     uint16_t GetPictureCodingType() { return m_pictureCodingType; };
115 
116     //!
117     //! \brief    Get bidirectional weight
118     //!
119     //! \return   int32_t
120     //!           Bidirectional weight
121     //!
GetBiWeight()122     int32_t GetBiWeight() { return m_biWeight; };
123 
124     MHW_SETPAR_DECL_HDR(VDENC_PIPE_BUF_ADDR_STATE);
125 
126     MHW_SETPAR_DECL_HDR(VDENC_AVC_IMG_STATE);
127 
128     MHW_SETPAR_DECL_HDR(MFX_PIPE_BUF_ADDR_STATE);
129 
130 protected:
131 
132     //!
133     //! \brief    Set frame store Id for avc codec.
134     //! \details
135     //! \return   frameIdx
136     //!           [in] frame index
137     //! \return   MOS_STATUS
138     //!           MOS_STATUS_SUCCESS if success, else fail reason
139     //!
140     MOS_STATUS SetFrameStoreIds(uint8_t frameIdx);
141 
142     //!
143     //! \brief    Validate reference list L0 and L1.
144     //!
145     //! \param    [in] params
146     //!           Pointer to CODECHAL_ENCODE_AVC_VALIDATE_NUM_REFS_PARAMS
147     //!
148     //! \return   MOS_STATUS
149     //!           MOS_STATUS_SUCCESS if success, else fail reason
150     //!
151     MOS_STATUS ValidateNumReferences(PCODECHAL_ENCODE_AVC_VALIDATE_NUM_REFS_PARAMS params);
152 
153     //!
154     //! \brief    Get Dist Scale factor
155     //!
156     //! \return   void
157     //!
158     void GetDistScaleFactor();
159 
160     //!
161     //! \brief    Get bidirectional weight
162     //!
163     //! \param    [in] distScaleFactorRefID0List0
164     //!           DistScaleFactorRefID0List0
165     //! \param    [in] weightedBiPredIdc
166     //!           Same as AVC syntax element.
167     //!
168     //! \return   int32_t
169     //!           Bidirectional weight
170     //!
171     int32_t GetBiWeight(
172         uint32_t distScaleFactorRefID0List0,
173         uint16_t weightedBiPredIdc);
174 
175     uint16_t                   m_pictureCodingType = 0;                                //!< I, P, or B frame
176     CODEC_PIC_ID               m_picIdx[CODEC_AVC_MAX_NUM_REF_FRAME] = {};             //!< Picture index
177     PCODEC_REF_LIST            m_refList[CODEC_AVC_NUM_UNCOMPRESSED_SURFACE] = {};     //!< Pointer to reference pictures
178     CODEC_AVC_FRAME_STORE_ID   m_frameStoreID[CODEC_AVC_MAX_NUM_REF_FRAME] = {};             //!< Refer to CODEC_AVC_FRAME_STORE_ID
179     PCODEC_REF_LIST            m_currRefList = nullptr;                                //!< Current reference list
180 
181     // B-frame
182     uint32_t                   m_distScaleFactorList0[CODEC_AVC_MAX_NUM_REF_FRAME * 2] = {};  //!< the DistScaleFactor used to derive temporal direct motion vector
183     uint32_t                   m_biWeight = 0;                     //!< Bidirectional Weight
184 
185     AvcBasicFeature *m_basicFeature = nullptr;  //!<  AVC paramter
186     EncodeAllocator *m_allocator    = nullptr;  //!< Encode allocator
187 
188 MEDIA_CLASS_DEFINE_END(encode__AvcReferenceFrames)
189 };
190 
191 }  // namespace encode
192 
193 #endif  // !__ENCODE_AVC_REFERENCE_FRAMES_H__
194