1 /*
2 * Copyright (c) 2019-2022, 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_vp9_basic_feature.h
24 //! \brief    Defines the common interface for encode vp9 basic feature
25 //!
26 #ifndef __ENCODE_VP9_BASIC_FEATURE_H__
27 #define __ENCODE_VP9_BASIC_FEATURE_H__
28 
29 #include "encode_basic_feature.h"
30 #include "encode_mem_compression.h"
31 
32 #include "codec_def_encode_vp9.h"
33 
34 #include "encode_vp9_reference_frames.h"
35 #include "media_vp9_packet_defs.h"
36 
37 #include "mhw_vdbox_huc_itf.h"
38 #include "mhw_vdbox_hcp_itf.h"
39 #include "mhw_vdbox_mfx_itf.h"
40 
41 #include "mhw_vdbox_vdenc_cmdpar.h"
42 
43 #include "mhw_vdbox_vdenc_itf.h"
44 #include "mhw_vdbox_hcp_itf.h"
45 
46 #include "media_packet.h"
47 
48 namespace encode
49 {
50 #define CODECHAL_ENCODE_VP9_MIN_TILE_SIZE_WIDTH  256
51 #define CODECHAL_ENCODE_VP9_MIN_TILE_SIZE_HEIGHT 128
52 #define CODECHAL_ENCODE_MIN_SCALED_SURFACE_SIZE  48
53 
54 
55 namespace TargetUsage
56 {
57 //!
58 //! \enum  MODE
59 //! \brief TargetUsage mode
60 //!
61 enum MODE
62 {
63     QUALITY     = 1,
64     QUALITY_2   = 2,
65     NORMAL      = 4,
66     PERFORMANCE = 7
67 };
68 
69 //! \brief  Check whether the target usage mode is valid.
70 //! \return bool
71 //!         'true' if the target usage mode is valid and 'false' otherwise.
72 //!
isValid(uint8_t targetUsage)73 inline bool isValid(uint8_t targetUsage)
74 {
75     bool res = true;
76 
77     switch (targetUsage)
78     {
79     case QUALITY:
80     case QUALITY_2:
81     case NORMAL:
82     case PERFORMANCE:
83         break;
84     default:
85         MHW_NORMALMESSAGE("Invalid TU provided!");
86         res = false;
87         break;
88     }
89 
90     return res;
91 }
92 
93 //!
94 //! \brief  Check whether the target usage mode is the TU1 or TU2 quality mode.
95 //! \return bool
96 //!         'true' if the target usage mode is the TU1 or TU2 quality mode and 'false' otherwise.
97 //!
isQuality(uint8_t targetUsage)98 inline bool isQuality(uint8_t targetUsage)
99 {
100     if (QUALITY == targetUsage || QUALITY_2 == targetUsage)
101     {
102         return true;
103     }
104     return false;
105 }
106 
107 //!
108 //! \brief  Check whether the target usage mode is the TU4 normal mode.
109 //! \return bool
110 //!         'true' if the target usage mode is the TU4 normal mode and 'false' otherwise.
111 //!
isNormal(uint8_t targetUsage)112 inline bool isNormal(uint8_t targetUsage)
113 {
114     if (NORMAL == targetUsage)
115     {
116         return true;
117     }
118     return false;
119 }
120 
121 //!
122 //! \brief  Check whether the target usage mode is the TU7 speed mode.
123 //! \return bool
124 //!         'true' if the target usage mode is the TU7 speed mode and 'false' otherwise.
125 //!
isSpeed(uint8_t targetUsage)126 inline bool isSpeed(uint8_t targetUsage)
127 {
128     if (PERFORMANCE == targetUsage)
129     {
130         return true;
131     }
132     return false;
133 }
134 };  // namespace TargetUsage
135 
136 
137 class Vp9BasicFeature : public EncodeBasicFeature,
138     public mhw::vdbox::huc::Itf::ParSetting,
139     public mhw::vdbox::hcp::Itf::ParSetting,
140     public mhw::vdbox::vdenc::Itf::ParSetting,
141     public mhw::vdbox::mfx::Itf::ParSetting,
142     public mhw::mi::Itf::ParSetting
143 {
144 public:
145     //!
146     //! \brief  Vp9BasicFeature constructor
147     //! \param  [in] allocator
148     //!         Pointer to EncodeAllocator
149     //! \param  [in] hwInterface
150     //!         Pointer to CodechalHwInterface
151     //! \param  [in] trackedBuf
152     //!         Pointer to TrackedBuffer
153     //! \param  [in] recycleBuf
154     //!         Pointer to RecycleResource
155     //!
156     Vp9BasicFeature(EncodeAllocator *allocator,
157                     CodechalHwInterfaceNext *hwInterface,
158                     TrackedBuffer *trackedBuf,
159                     RecycleResource *recycleBuf,
EncodeBasicFeature(allocator,hwInterface,trackedBuf,recycleBuf)160                     void *constSettings = nullptr) : EncodeBasicFeature(allocator, hwInterface, trackedBuf, recycleBuf) { m_constSettings = constSettings; };
161 
162     //!
163     //! \brief  Vp9BasicFeature destructor
164     //!
~Vp9BasicFeature()165     virtual ~Vp9BasicFeature(){};
166 
167     //!
168     //! \brief  Init VP9 encode basic feature's parameter setting
169     //! \param  [in] setting
170     //!         Pointer to CodechalSetting
171     //! \return MOS_STATUS
172     //!         MOS_STATUS_SUCCESS if success, else fail reason
173     //!
174     virtual MOS_STATUS Init(void *setting) override;
175 
176     //!
177     //! \brief  Update VP9 encode basic feature parameters
178     //! \param  [in] params
179     //!         Pointer to EncoderParams
180     //! \return MOS_STATUS
181     //!         MOS_STATUS_SUCCESS if success, else fail reason
182     //!
183     virtual MOS_STATUS Update(void *params) override;
184 
185     //!
186     //! \brief  Get profile level max frame size
187     //! \return uint32_t
188     //!         Profile level max frame size
189     //!
190     virtual uint32_t GetProfileLevelMaxFrameSize() override;
191 
192     //!
193     //! \brief    Get Os interface
194     //! \details  Get Os interface in codechal hw interface
195     //! \return   [out] PMOS_INTERFACE
196     //!           Interface got.
197     //!
GetOsInterface()198     inline PMOS_INTERFACE GetOsInterface() { return m_osInterface; }
199 
200     //!
201     //! \brief    Get encode allocator interfacae
202     //! \details  Get encode allocator interface
203     //! \return   [out] EncodeAllocator
204     //!           Encode allocator interface
205     //!
GetAllocator()206     inline EncodeAllocator* GetAllocator() { return m_allocator; }
207 
208     //!
209     //! \brief      Resize 4x and 8x DS recon Surfaces to VDEnc
210     //! \param      [in] bufIdx
211     //!             Index of the surface
212     //! \return     MOS_STATUS
213     //!             MOS_STATUS_SUCCESS if success, else fail reason
214     //!
215     MOS_STATUS Resize4x8xforDS(uint8_t bufIdx);
216 
217     //!
218     //! \brief  Update parameters
219     //! \return MOS_STATUS
220     //!         MOS_STATUS_SUCCESS if success, else fail reason
221     //!
222     virtual MOS_STATUS UpdateParameters();
223 
224     //!
225     //! \brief  Convert to Sign Magnitude
226     //! \return uint32_t
227     //!         value calculated
228     //!
229     uint32_t Convert2SignMagnitude(int32_t val, uint32_t signBitPos) const;
230 
231     //!
232     //! \brief  Enable Decode mode for MFX
233     //!
SetDecodeInUse(bool decodeInUse)234     void SetDecodeInUse(bool decodeInUse) { m_decodeInUse = decodeInUse; }
235 
236     //!
237     //! \brief  Check whether HME is enabled for the target usage mode.
238     //! \return bool
239     //!         'true' if HME is enabled for the target usage mode and 'false' otherwise.
240     //!
241     virtual bool isHmeEnabledForTargetUsage(uint8_t targetUsage) const;
242 
243     //!
244     //! \brief MHW parameters declaration
245     //!
246     MHW_SETPAR_DECL_HDR(HCP_VP9_PIC_STATE);
247     MHW_SETPAR_DECL_HDR(VDENC_CMD1);
248     MHW_SETPAR_DECL_HDR(VDENC_SRC_SURFACE_STATE);
249     MHW_SETPAR_DECL_HDR(VDENC_DS_REF_SURFACE_STATE);
250     MHW_SETPAR_DECL_HDR(VDENC_PIPE_MODE_SELECT);
251     MHW_SETPAR_DECL_HDR(VDENC_PIPE_BUF_ADDR_STATE);
252     MHW_SETPAR_DECL_HDR(MFX_WAIT);
253     MHW_SETPAR_DECL_HDR(MFX_PIPE_MODE_SELECT);
254     MHW_SETPAR_DECL_HDR(VDENC_CMD2);
255 
256     EncodeMemComp *m_mmcState = nullptr;
257 
258     // Parameters passed from application
259     PCODEC_VP9_ENCODE_SEQUENCE_PARAMS m_vp9SeqParams     = nullptr;  //!< Pointer to sequence parameters
260     PCODEC_VP9_ENCODE_PIC_PARAMS      m_vp9PicParams     = nullptr;  //!< Pointer to picture parameters
261     PCODEC_VP9_ENCODE_SEGMENT_PARAMS  m_vp9SegmentParams = nullptr;  //!< Pointer to segment parameters
262     Vp9ReferenceFrames                m_ref              = {};       //!< Reference List
263 
264     bool m_pakEnabled = false;  //!< flag to indicate if PAK is enabled
265     bool m_encEnabled = false;  //!< flag to indicate if ENC is enabled
266 
267     bool m_dysCqp = false;
268     bool m_dysBrc = false;
269     bool m_tsEnabled                      = false;
270     bool m_vdencPakonlyMultipassEnabled   = false;  //!< Flag to signal (VDEnc + PAK) vs. (PAK Only)
271 
272     uint8_t m_txMode = 0;  //!< TX mode
273 
274     // User feature key capabilities
275     bool m_hucEnabled     = true;   //!< HUC usage flag
276     bool m_hmeEnabled     = false;  //!< Flag indicate if HME is enabled
277     bool m_hmeSupported   = false;  //!< Flag indicate if HME is supported
278     bool m_16xMeEnabled   = false;  //!< Flag indicate if 16x ME is enabled
279     bool m_16xMeSupported = false;  //!< Flag indicate if 16x ME is supported
280     bool m_32xMeSupported = false;  //!< Flag indicate if 32x ME is supported
281 
282     uint32_t m_maxPicWidth      = 0;  //!< Max picture width
283     uint32_t m_maxPicHeight     = 0;  //!< Max picture height
284     uint32_t m_maxPicWidthInSb  = 0;  //!< Max picture width in SB unit
285     uint32_t m_maxPicHeightInSb = 0;  //!< Max picture height in SB unit
286     uint32_t m_maxPicSizeInSb   = 0;  //!< Max picture size in SB unit
287 
288     uint32_t m_maxTileNumber = 1;  //!< max number of tile
289     uint32_t m_picWidthInSb  = 0;  //!< Picture width in SB unit
290     uint32_t m_picHeightInSb = 0;  //!< Picture height in SB unit
291     uint32_t m_picSizeInSb   = 0;  //!< Picture size in SB unit
292 
293     uint16_t m_slbbImgStateOffset = 0;
294     uint16_t m_hucPicStateOffset  = 0;
295     uint16_t m_hucSlbbSize        = 0;
296 
297     uint32_t m_bitstreamUpperBound = 0;  //!< Bitstream upper bound
298 
299     uint32_t m_sizeOfSseSrcPixelRowStoreBufferPerLcu = 0;  //!< Size of SSE row store buffer per LCU
300 
301     uint8_t  m_minScaledDimension                    = 0;  //!< min scaled dimension
302     uint8_t  m_minScaledDimensionInMb                = 0;  //!< min scaled dimension in Mb
303 
304     uint32_t m_downscaledFrameFieldHeightInMb4x  = 0;  //!< Downscale frame field height in Mb 4x
305 
306     uint32_t m_downscaledWidth16x                = 0;  //!< Downscale width 16x
307     uint32_t m_downscaledHeight16x               = 0;  //!< Downscale height 16x
308     uint32_t m_downscaledWidthInMb16x            = 0;  //!< Downscale width in Mb 16x
309     uint32_t m_downscaledHeightInMb16x           = 0;  //!< Downscale height in Mb 16x
310     uint32_t m_downscaledFrameFieldHeightInMb16x = 0;  //!< Downscale frame field height in Mb 16x
311 
312     uint32_t m_downscaledWidth32x      = 0;  //!< Downscale width 32x
313     uint32_t m_downscaledHeight32x     = 0;  //!< Downscale height 32x
314     uint32_t m_downscaledWidthInMb32x  = 0;  //!< Downscale width 32x
315     uint32_t m_downscaledHeightInMb32x = 0;  //!< Downscale height 32x
316 
317     PMOS_RESOURCE m_resMvTemporalBuffer    = nullptr;  //!< Pointer to MOS_RESOURCE of MvTemporal Buffer
318     PMOS_RESOURCE m_hucPakIntBrcDataBuffer = nullptr;  //!< HuC PAK integration BRC data buffer
319     PMOS_RESOURCE m_resSegmentIdBuffer     = nullptr;  //!< Segment ID buffer
320 
321     uint8_t m_contextFrameTypes[CODEC_VP9_NUM_CONTEXTS] = {0};
322     bool    m_prevFrameSegEnabled                       = false;
323 
324     bool m_scalableMode             = false;  //!< Flag indicate scalable mode
325     bool m_lastFrameScalableMode    = false;  //!< Flag indicate last frame scalable mode
326     bool m_dysVdencMultiPassEnabled = false;  //!< Flag DYS vdenc multiple pass enabled
327     bool m_pakOnlyModeEnabledForLastPass = false;  //!< Pak only mode enaled for last pass
328 
329     uint8_t m_currMvTemporalBufferIndex = 0;  //!< Current MV temporal buffer index
330     uint8_t m_lastMvTemporalBufferIndex = 0;  //!< Last MV temporal buffer index
331 
332     uint8_t m_currRecycledBufIdx = 0;  //!< Current recycled buffer index update from EncodePipeline::ResetParams()
333 
334     uint32_t m_mvOffset = 0;  //!< MV data offset, in 64 byte
335 
336     HucPrevFrameInfo m_prevFrameInfo = {0};
337 
338     bool     m_adaptiveRepakSupported                 = false;
339     uint32_t m_rePakThreshold[CODEC_VP9_QINDEX_RANGE] = {0};
340 
341     uint8_t m_oriTargetUsage = 0; //!< Store original target usage pass from app. Use for CalculateRePakThresholds()
342 
343 protected:
344     //!
345     //! \brief  Update the parameters of tracked buffers
346     //! \return MOS_STATUS
347     //!         MOS_STATUS_SUCCESS if success, else fail reason
348     //!
349     virtual MOS_STATUS UpdateTrackedBufferParameters() override;
350 
351     //!
352     //! \brief  Get the buffers from tracked buffer manager
353     //! \return MOS_STATUS
354     //!         MOS_STATUS_SUCCESS if success, else fail reason
355     //!
356     virtual MOS_STATUS GetTrackedBuffers() override;
357 
358     //!
359     //! \brief  Set sequence structures
360     //! \return MOS_STATUS
361     //!         MOS_STATUS_SUCCESS if success, else fail reason
362     //!
363     MOS_STATUS SetSequenceStructs();
364 
365     //!
366     //! \brief  Set the picture structs
367     //! \return MOS_STATUS
368     //!         MOS_STATUS_SUCCESS if success, else fail reason
369     //!
370     MOS_STATUS SetPictureStructs();
371 
372     //!
373     //! \brief  Motion estimation disable check
374     //!
375     void MotionEstimationDisableCheck();
376 
377     //!
378     //! \brief      Resize DS recon surface for vdenc
379     //! \return     MOS_STATUS
380     //!             MOS_STATUS_SUCCESS if success, else fail reason
381     //!
382     MOS_STATUS ResizeDsReconSurfacesVdenc();
383 
384     //!
385     //! \brief    Calculate rePak thresholds
386     //! \details
387     //!
388     //! \return   MOS_STATUS
389     //!           MOS_STATUS_SUCCESS if success, else fail reason
390     //!
391     MOS_STATUS CalculateRePakThresholds();
392 
393     static constexpr uint32_t m_maxLCUSize = 64;  //!< Max LCU size
394     static constexpr uint32_t m_minLCUSize = 8;   //!< Min LCU size
395 
396     bool m_decodeInUse = false;
397 
398     enum CodecSelect
399     {
400         decoderCodec = 0,
401         encoderCodec = 1
402     };
403 
404 MEDIA_CLASS_DEFINE_END(encode__Vp9BasicFeature)
405 };
406 
407 }  // namespace encode
408 
409 #endif  // !__ENCODE_VP9_BASIC_FEATURE_H__
410