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_basic_feature.h
24 //! \brief    Defines the common interface for encode avc basic feature
25 //!
26 #ifndef __ENCODE_AVC_BASIC_FEATURE_H__
27 #define __ENCODE_AVC_BASIC_FEATURE_H__
28 
29 #include "encode_basic_feature.h"
30 #include "encode_avc_reference_frames.h"
31 #include "mhw_vdbox_vdenc_itf.h"
32 #include "mhw_vdbox_mfx_itf.h"
33 #include "mhw_vdbox_huc_itf.h"
34 #include "encode_mem_compression.h"
35 #include "media_copy_wrapper.h"
36 #include "codechal_debug.h"
37 
38 namespace encode
39 {
40 //!
41 //! \enum     AvcImgStructure
42 //! \brief    Average image structure
43 //!
44 enum AvcImgStructure
45 {
46     avcFrame        = 0,
47     avcTopField     = 1,
48     avcBottomField  = 3
49 };
50 
51 //!
52 //! \enum     CodecSelect
53 //! \brief    Codec select
54 //!
55 enum CodecSelect
56 {
57     decoderCodec    = 0,
58     encoderCodec    = 1
59 };
60 
61 //!
62 //! \enum     AvcQmTypes
63 //! \brief    Average qm types
64 //!
65 enum AvcQmTypes
66 {
67     avcQmIntra4x4 = 0,
68     avcQmInter4x4 = 1,
69     avcQmIntra8x8 = 2,
70     avcQmInter8x8 = 3
71 };
72 
73 //!
74 //! \struct   AvcRefListWrite
75 //! \brief    Average reference list write
76 //!
77 struct AvcRefListWrite
78 {
79     union
80     {
81         struct
82         {
83             uint8_t bottomField     : 1;
84             uint8_t frameStoreID    : 4;
85             uint8_t fieldPicFlag    : 1;
86             uint8_t longTermFlag    : 1;
87             uint8_t nonExisting     : 1;
88         };
89         struct
90         {
91             uint8_t value;
92         };
93     } UC[32];
94 };
95 
96 class AvcBasicFeature : public EncodeBasicFeature,
97     public mhw::vdbox::vdenc::Itf::ParSetting,
98     public mhw::vdbox::mfx::Itf::ParSetting,
99     public mhw::vdbox::huc::Itf::ParSetting,
100     public mhw::mi::Itf::ParSetting
101 {
102 public:
103     AvcBasicFeature(EncodeAllocator * allocator,
104                     CodechalHwInterfaceNext *hwInterface,
105                     TrackedBuffer *trackedBuf,
106                     RecycleResource *recycleBuf,
107                     MediaCopyWrapper *mediaCopyWrapper,
108                     void *constSettings = nullptr) :
EncodeBasicFeature(allocator,hwInterface,trackedBuf,recycleBuf)109                     EncodeBasicFeature(allocator, hwInterface, trackedBuf, recycleBuf),
110                     m_mediaCopyWrapper(mediaCopyWrapper) { m_constSettings = constSettings; }
111 
112     virtual ~AvcBasicFeature();
113 
114     virtual MOS_STATUS Init(void *setting) override;
115 
116     virtual MOS_STATUS Update(void *params) override;
117 
118     virtual uint32_t GetProfileLevelMaxFrameSize() override;
119 
120     bool IsAvcPSlice(uint8_t sliceType) const;
121     bool IsAvcBSlice(uint8_t sliceType) const;
122 
123     MHW_SETPAR_DECL_HDR(VDENC_PIPE_MODE_SELECT);
124 
125     MHW_SETPAR_DECL_HDR(VDENC_SRC_SURFACE_STATE);
126 
127     MHW_SETPAR_DECL_HDR(VDENC_REF_SURFACE_STATE);
128 
129     MHW_SETPAR_DECL_HDR(VDENC_DS_REF_SURFACE_STATE);
130 
131     MHW_SETPAR_DECL_HDR(VDENC_PIPE_BUF_ADDR_STATE);
132 
133     MHW_SETPAR_DECL_HDR(VDENC_AVC_IMG_STATE);
134 
135     MHW_SETPAR_DECL_HDR(VDENC_CMD3);
136 
137     MHW_SETPAR_DECL_HDR(VDENC_AVC_SLICE_STATE);
138 
139     MHW_SETPAR_DECL_HDR(VDENC_WALKER_STATE);
140 
141     MHW_SETPAR_DECL_HDR(MFX_PIPE_MODE_SELECT);
142 
143     MHW_SETPAR_DECL_HDR(MFX_SURFACE_STATE);
144 
145     MHW_SETPAR_DECL_HDR(MFX_PIPE_BUF_ADDR_STATE);
146 
147     MHW_SETPAR_DECL_HDR(MFX_IND_OBJ_BASE_ADDR_STATE);
148 
149     MHW_SETPAR_DECL_HDR(MFX_AVC_IMG_STATE);
150 
151     MHW_SETPAR_DECL_HDR(MFX_AVC_REF_IDX_STATE);
152 
153     MHW_SETPAR_DECL_HDR(MFX_AVC_SLICE_STATE);
154 
155     MHW_SETPAR_DECL_HDR(MFX_AVC_DIRECTMODE_STATE);
156 
157     MHW_SETPAR_DECL_HDR(MI_FORCE_WAKEUP);
158 
159     MHW_SETPAR_DECL_HDR(MFX_WAIT);
160 
161     MHW_SETPAR_DECL_HDR(HUC_PIPE_MODE_SELECT);
162 
163     EncodeMemComp *m_mmcState = nullptr;
164 
165     // Parameters passed from application
166     PCODEC_AVC_ENCODE_PIC_PARAMS                m_picParams[CODEC_AVC_MAX_PPS_NUM] = {};  //!< Pointer to array of picture parameter, could be removed
167     PCODEC_AVC_ENCODE_SEQUENCE_PARAMS           m_seqParams[CODEC_AVC_MAX_SPS_NUM] = {};  //!< Pointer to array of sequence parameter, could be removed
168     PCODEC_AVC_ENCODE_PIC_PARAMS                m_picParam           = nullptr;           //!< Pointer to AVC picture parameter
169     PCODEC_AVC_ENCODE_SEQUENCE_PARAMS           m_seqParam           = nullptr;           //!< Pointer to AVC sequence parameter
170     PCODECHAL_ENCODE_AVC_VUI_PARAMS             m_vuiParams          = nullptr;           //!< Pointer to AVC Uvi parameter
171     PCODEC_AVC_ENCODE_SLICE_PARAMS              m_sliceParams        = nullptr;           //!< Pointer to AVC slice parameter
172     PCODEC_AVC_IQ_MATRIX_PARAMS                 m_iqMatrixParams     = nullptr;           //!< Pointer to IQMaxtrix parameter
173     PCODEC_AVC_ENCODE_IQ_WEIGTHSCALE_LISTS      m_iqWeightScaleLists = nullptr;           //!< Pointer to IQWidght ScaleLists
174     CODEC_AVC_ENCODE_USER_FLAGS                 m_userFlags;                              //!< Encoder user flag settings
175 
176     std::shared_ptr<AvcReferenceFrames>  m_ref = nullptr;             //! Reference List
177 
178     uint32_t                          m_curNumSlices = 0;             //!< Number of current slice
179 
180 #if USE_CODECHAL_DEBUG_TOOL
181     MHW_VDBOX_AVC_SLICE_STATE sliceState{};
182 #endif  // USE_CODECHAL_DEBUG_TOOL
183 
184     CODECHAL_ENCODE_AVC_NAL_UNIT_TYPE m_nalUnitType         = CODECHAL_ENCODE_AVC_NAL_UT_RESERVED;      //!< Nal unit type
185 
186     uint16_t                          m_sliceHeight         = 0;      //!< Slice height
187     bool                              m_deblockingEnabled   = false;  //!< Enable deblocking flag
188     bool                              m_mbaffEnabled        = false;  //!< Enable MBAFF flag
189     uint32_t                          m_sliceStructCaps     = 0;      //!< Slice struct
190     uint8_t                           m_targetUsageOverride = 0;      //!< Target usage override
191 
192     bool                              m_useRawForRef = false;         //!< Flag to indicate if using raw surface for reference
193     uint8_t                           m_prevReconFrameIdx = 0;        //!< Previous reconstruct frame index
194     uint8_t                           m_currReconFrameIdx = 0;        //!< Current reconstruct frame index
195 
196     bool                              m_adaptiveRoundingInterEnable = false;  //!< Adaptive Rounding Inter Enable Flag.
197 
198     // VDEnc params
199     bool                            m_vdencNoTailInsertion = false;  //!< Vdenc no tail insertion enabled flag
200 
201     bool                            m_perMBStreamOutEnable = false;
202 
203     // ENC input/output buffers
204     bool                            m_madEnabled = false;            //!< Mad enabled flag
205 
206     // Maximum number of slices allowed by video spec
207     uint32_t                        m_maxNumSlicesAllowed = 0;       //!< Max number of slices allowed
208 
209     // CMD buffer sizes
210     uint32_t                        m_extraPictureStatesSize = 0;    //!< Picture states size extra
211 
212     // Skip frame params
213     uint8_t                         m_skipFrameFlag = 0;             //!< Skip frame flag
214 
215     bool                            m_acceleratorHeaderPackingCaps = false;  //!< Flag set by driver from driver caps.
216     bool                            m_suppressReconPicSupported    = false;  //!< Suppress reconstructed picture supported flag
217 
218     // Lookahead
219     uint8_t  m_lookaheadDepth      = 0;  //!< Number of frames to lookahead
220     uint32_t m_averageFrameSize    = 0;  //!< Average frame size based on targed bitrate and frame rate, in unit of bits
221     uint32_t m_prevTargetFrameSize = 0;  //!< Target frame size of previous frame.
222     uint32_t m_targetBufferFulness = 0;  //!< Target encode buffer fulness in bits, used by BRC and calculated from initial buffer fulness, target frame size (from DDI) and average frame size
223 
224     // Below values will be set if qp control params are sent by app
225     bool                            m_minMaxQpControlEnabled = false;  //!< Flag to indicate if min/max QP feature is enabled or not.
226     uint8_t                         m_iMinQp                 = 0;      //!< I frame Minimum QP.
227     uint8_t                         m_iMaxQp                 = 0;      //!< I frame Maximum QP.
228     uint8_t                         m_pMinQp                 = 0;      //!< P frame Minimum QP.
229     uint8_t                         m_pMaxQp                 = 0;      //!< P frame Maximum QP.
230     uint8_t                         m_bMinQp                 = 0;      //!< B frame Minimum QP.
231     uint8_t                         m_bMaxQp                 = 0;      //!< B frame Maximum QP.
232     bool                            m_pFrameMinMaxQpControl  = false;  //!< Indicates min/max QP values for P-frames are set separately or not.
233     bool                            m_bFrameMinMaxQpControl  = false;  //!< Indicates min/max QP values for B-frames are set separately or not.
234 
235     uint32_t                        m_colocatedMVBufferSize       = 0;
236     PMOS_RESOURCE                   m_colocatedMVBufferForIFrames = nullptr;
237 
238     // TCBRC related flags
239     bool                            m_forcedTCBRC                     = false;  //!< TCBRC forced instead of LowDelayBRC
240     bool                            m_brcAdaptiveRegionBoostSupported = false;  //!< Adaptive Region Boost supported flag
241     bool                            m_brcAdaptiveRegionBoostEnabled   = false;  //!< Adaptive Region Boost enabled flag
242 
243 protected:
244     MOS_STATUS SetSequenceStructs();
245     MOS_STATUS SetPictureStructs();
246     MOS_STATUS UpdateSeiParameters(EncoderParams* params);
247     void UpdateMinMaxQp();
248     int32_t GetMaxMBPS(uint8_t levelIdc);
249     MOS_STATUS SetSliceStructs();
250     void CheckResolutionChange();
251     MOS_STATUS PackPictureHeader();
252     virtual bool InputSurfaceNeedsExtraCopy(const MOS_SURFACE &input);
253 
254     virtual MOS_STATUS UpdateTrackedBufferParameters() override;
255     virtual MOS_STATUS GetTrackedBuffers() override;
256 
257     //!
258     //! \brief    Initialize reference frames class
259     //!
260     //! \return   MOS_STATUS
261     //!           MOS_STATUS_SUCCESS if success, else fail reason
262     //!
263     virtual MOS_STATUS InitRefFrames();
264 
265     //!
266     //! \brief    Calculate scaling list
267     //!
268     //! \return   void
269     //!
270     void ScalingListFlat();
271 
272     //!
273     //! \brief    Calculate scaling list
274     //!
275     //! \return   void
276     //!
277     void ScalingListFallbackRuleA();
278 
279     //! \brief    Get the max number of allowed slice
280     //! \param    [in] profileIdc
281     //!           AVC profile idc
282     //! \param    [in] levelIdc
283     //!           AVC level idc
284     //! \param    [in] framesPer100Sec
285     //!           frame Per 100Sec
286     //! \return   uint16_t
287     //!           return uiMaxAllowedNumSlices
288     //!
289     uint16_t GetMaxNumSlicesAllowed(
290         CODEC_AVC_PROFILE_IDC profileIdc,
291         CODEC_AVC_LEVEL_IDC   levelIdc,
292         uint32_t              framesPer100Sec);
293 
294     // SEI
295     CodechalEncodeSeiData m_seiData        = {};       //!< Encode SEI data parameter.
296     uint32_t              m_seiDataOffset  = false;    //!< Encode SEI data offset.
297     uint8_t *             m_seiParamBuffer = nullptr;  //!< Encode SEI data buffer.
298 
299     MediaCopyWrapper *m_mediaCopyWrapper = nullptr;
300 
301 MEDIA_CLASS_DEFINE_END(encode__AvcBasicFeature)
302 };
303 
304 }  // namespace encode
305 
306 #endif  // !__ENCODE_AVC_BASIC_FEATURE_H__
307