1 /*
2 * Copyright (c) 2018-2023, 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_parameter.h
24 //! \brief    Defines the common interface for encode parameter
25 //!
26 #ifndef __ENCODE_BASIC_FEATURE_H__
27 #define __ENCODE_BASIC_FEATURE_H__
28 
29 #include "codec_hw_next.h"
30 #include "codec_def_encode.h"
31 #include "codechal_setting.h"
32 #include "encode_tracked_buffer.h"
33 #include "encode_recycle_resource.h"
34 #include "media_feature.h"
35 #include "mhw_vdbox.h"
36 
37 namespace encode
38 {
39 class EncodeBasicFeature:public MediaFeature
40 {
41 public:
42     EncodeBasicFeature(EncodeAllocator *allocator,
43                         CodechalHwInterfaceNext *hwInterface,
44                         TrackedBuffer *trackedBuf,
45                         RecycleResource *recycleBuf);
~EncodeBasicFeature()46     virtual ~EncodeBasicFeature() { }
47 
48     //!
49     //! \brief  Init encode parameter
50     //! \param  [in] setting
51     //!         Pointer to CodechalSetting
52     //! \return MOS_STATUS
53     //!         MOS_STATUS_SUCCESS if success, else fail reason
54     //!
55     virtual MOS_STATUS Init(void *setting);
56 
57     //!
58     //! \brief  Update encode basic feature
59     //! \param  [in] params
60     //!         Pointer to EncoderParams
61     //! \return MOS_STATUS
62     //!         MOS_STATUS_SUCCESS if success, else fail reason
63     //!
64     virtual MOS_STATUS Update(void *params);
65 
66     //!
67     //! \brief  Update encode basic format
68     //! \param  [in] params
69     //!         Pointer to EncoderParams
70     //! \return MOS_STATUS
71     //!         MOS_STATUS_SUCCESS if success, else fail reason
72     //!
73     virtual MOS_STATUS UpdateFormat(void *params);
74 
75     //!
76     //! \brief  Reset encode basic feature.
77     //! \return MOS_STATUS
78     //!         MOS_STATUS_SUCCESS if success, else fail reason
79     //!
80     virtual MOS_STATUS Reset(CODEC_REF_LIST *refList);
81 
82     //!
83     //! \brief  Get the waTable
84     //! \return The active WA table
85     //!
GetWaTable()86     MEDIA_WA_TABLE *GetWaTable() { return m_osInterface? m_osInterface->pfnGetWaTable(m_osInterface):nullptr; }
87 
88     virtual uint32_t GetProfileLevelMaxFrameSize() = 0;
89 
90     static constexpr uint32_t m_vdencBrcPassNum = 3;  //!< Vdenc Brc Number of passes
91     static constexpr uint32_t m_uncompressedSurfaceNum = 127;  //!< Hevc/Av1 uncompressed surface number, 7 bits, 0x7f is invalid one
92     static constexpr uint32_t m_sizeOfHcpPakFrameStats = 9 * CODECHAL_CACHELINE_SIZE;  //!> Size of HEVC PAK frame statistics
93     static constexpr uint32_t m_hevcPakStatsSSEOffset = 32;
94     static constexpr uint32_t m_codecHalHevcNumPakSliceBatchBuffers = 3;
95 
96     uint32_t                    m_frameWidth = 0;             //!< Frame width in luma samples
97     uint32_t                    m_frameHeight = 0;            //!< Frame height in luma samples
98     uint32_t                    m_frameFieldHeight = 0;       //!< Frame height in luma samples
99     uint32_t                    m_oriFrameHeight = 0;         //!< Original frame height
100     uint32_t                    m_oriFrameWidth = 0;          //!< Original frame width
101     uint16_t                    m_frame_crop_bottom_offset = 0;                       //!< frame_crop_bottom_offset
102     uint16_t                    m_frame_mbs_only_flag      = 0;                       //!< frame_mbs_only_flag
103     uint16_t                    m_frame_cropping_flag    = 0;                         //!< frame_cropping_flag
104     uint16_t                    m_picWidthInMb = 0;           //!< Picture Width in MB width count
105     uint16_t                    m_picHeightInMb = 0;          //!< Picture Height in MB height count
106     uint16_t                    m_frameFieldHeightInMb = 0;   //!< Frame/field Height in MB
107     uint32_t                    m_downscaledWidthInMb4x = 0;                          //!< Downscale width in Mb 4x
108     uint32_t                    m_downscaledHeightInMb4x = 0;                         //!< Downscale height in Mb 4x
109 
110     CODEC_PICTURE               m_currOriginalPic = {};       //!< Raw.
111     CODEC_PICTURE               m_currReconstructedPic = {};  //!< RECON.
112     uint16_t                    m_pictureCodingType = 0;      //!< I, P, or B frame
113     uint32_t                    m_frameNum             = 0;   //!< Frame number
114     bool                        m_firstField = true;          //!< Flag to indicate if it is first field
115     bool                        m_resolutionChanged = false;  //!< Flag to indicate if resolution is changed
116 
117     uint32_t                    m_standard = 0;               //!< The encode state's standard
118     uint32_t                    m_mode = 0;                   //!< The encode mode
119     CODECHAL_FUNCTION           m_codecFunction = CODECHAL_FUNCTION_INVALID;           //!< The encode state's codec function used
120 
121     PCODECHAL_NAL_UNIT_PARAMS   *m_nalUnitParams = nullptr;   //!< The encode nal unit
122     uint32_t                     m_NumNalUnits = 0;           //!< Number of NAL units in ppNALUnitParams.
123 
124     bool                        m_panicEnable = false;        //!< Indicate if panic is enabled
125 
126     bool                        m_newSeqHeader = false;       //!< New sequence header flag
127     bool                        m_newPpsHeader = false;       //!< New PPS header flag
128     bool                        m_newVuiData = false;         //!< New VUI data flag
129     bool                        m_newSeq = false;             //!< New sequence flag
130     bool                        m_lastPicInSeq = false;       //!< Flag to indicate if it is last picture in sequence
131     bool                        m_lastPicInStream = false;    //!< Flag to indicate if it is last picture in stream
132 
133     bool                        m_picQuant = false;           //!< picture quant
134     bool                        m_newQmatrixData = false;     //!< New Qmatrix data
135     PCODEC_ENCODER_SLCDATA      m_slcData = nullptr;          //!< record slice header size & position
136     uint32_t                    m_numSlices = 0;              //!< Number of slices
137 
138     uint32_t                    m_mbDataBufferSize = 0;
139 
140     TrackedBuffer               *m_trackedBuf         = nullptr;  //!< Encode tracked buffer
141     RecycleResource             *m_recycleBuf         = nullptr;  //!< Recycle resources
142 
143     uint32_t                    m_mbCodeSize          = 0;        //!< MB code buffer size
144     uint32_t                    m_mvDataSize          = 0;        //!< MV data size
145     PMOS_RESOURCE               m_resMbCodeBuffer     = nullptr;  //!< Pointer to MOS_RESOURCE of MbCode buffer
146     PMOS_RESOURCE               m_resMvDataBuffer     = nullptr;  //!< Pointer to MOS_RESOURCE of MvData buffer
147     bool                        m_isMbCodeRegistered  = false;
148 
149     uint32_t                    m_downscaledWidth4x   = 0;
150     uint32_t                    m_downscaledHeight4x  = 0;
151     PMOS_SURFACE                m_4xDSSurface         = nullptr;
152     PMOS_SURFACE                m_8xDSSurface         = nullptr;
153 
154     PMOS_SURFACE                m_rawSurfaceToPak = nullptr;
155     PMOS_SURFACE                m_rawSurfaceToEnc = nullptr;       //!< raw surf to enc
156     MOS_SURFACE                 m_rawSurface = {};                 //!< Pointer to MOS_SURFACE of raw surface
157     MOS_SURFACE                 m_reconSurface = {};               //!< Pointer to MOS_SURFACE of reconstructed surface
158     MOS_RESOURCE                m_resBitstreamBuffer = {};         //!< Pointer to MOS_SURFACE of bitstream surface
159     PMOS_RESOURCE               m_resMetadataBuffer = nullptr;
160     MetaDataOffset              m_metaDataOffset = {};
161 
162     BSBuffer                    m_bsBuffer = {};                   //!< Bit-stream buffer
163 
164     uint32_t                    m_bitstreamSize = 0;               //!< Maximum amount of data to be output to presBitstreamBuffer.
165     bool                        m_mbQpDataEnabled = false;         //!< [AVC & MPEG2] Indicates that psMbQpDataSurface is present.
166     bool                        m_mbDisableSkipMapEnabled = false; //!< [AVC] Indicates that psMbDisableSkipMapSurface is present.
167     MOS_SURFACE                 m_mbDisableSkipMapSurface = {};    //!< [AVC] MB disable skip map provided by framework
168     MOS_SURFACE                 m_mbQpDataSurface = {};            //!< pointer to surface of Mb QP Data
169     bool                        m_rgbEncodingEnable = false;       //!< Enable RGB encoding
170     bool                        m_captureModeEnable = false;       //!< Enable Capture mode with display
171     bool                        m_predicationNotEqualZero = false;       //!< [Predication] Predication mode
172     bool                        m_predicationEnabled      = false;       //!< [Predication] Indicates whether or not Predication is enabled
173     bool                        m_setMarkerEnabled        = false;       //!< [SetMarker] Indicates whether or not SetMarker is enabled
174     uint64_t                    m_predicationResOffset    = 0;           //!< [Predication] Offset for Predication resource
175     PMOS_RESOURCE               m_presPredication         = nullptr;     //!< [Predication] Resource for predication
176     PMOS_RESOURCE              *m_tempPredicationBuffer   = nullptr;     //!< [Predication] Temp buffer for Predication
177     PMOS_RESOURCE               m_predicationBuffer       = nullptr;     //!< [Predication] Internal buffer for predication
178 
179     uint8_t                     m_targetUsage =0;
180 
181     uint8_t  m_chromaFormat       = HCP_CHROMA_FORMAT_YUV420;           //!< Chroma format(420, 422 etc)
182     uint8_t  m_bitDepth           = 8;                                  //!< Bit depth
183     bool     m_is10Bit            = false;
184     uint8_t  m_outputChromaFormat = (uint8_t)HCP_CHROMA_FORMAT_YUV420;  //!< 1: 420 2: 422 3: 444
185 
186     uint32_t m_currPakSliceIdx = 0;                                     //!< Current pak slice index
187 
188     bool m_enableTileStitchByHW = false;                                //!< Enable HW to stitch commands in scalable mode
189 
190     bool m_lockableResource = false;                                    //!< disable / enable lockable surface
191 
192     uint8_t m_par65Inter = 0;
193     uint8_t m_par65Intra = 0;
194 
195     /*! \brief Specifies motion search modes that will be used.
196     *
197     *    SubPelMode is only valid when bEnableSubPelMode is true. Following are valid values of SubPelMode:
198     *    0:Integer mode searching
199     *    1:Half-pel mode searching
200     *    2:Reserved
201     *    3:Quarter-pel mode searching
202     */
203     bool    m_bEnableSubPelMode = false;
204     uint8_t m_SubPelMode        = 3;
205     bool    m_dualEncEnable     = false;
206 
207 protected:
208     //!
209     //! \brief  Update the parameters of tracked buffers
210     //! \return MOS_STATUS
211     //!         MOS_STATUS_SUCCESS if success, else fail reason
212     //!
213     virtual MOS_STATUS UpdateTrackedBufferParameters();
214 
215     //!
216     //! \brief  Get the buffers from tracked buffer manager
217     //! \return MOS_STATUS
218     //!         MOS_STATUS_SUCCESS if success, else fail reason
219     //!
GetTrackedBuffers()220     virtual MOS_STATUS GetTrackedBuffers() { return MOS_STATUS_SUCCESS; }
221 
222     EncodeAllocator *m_allocator = nullptr;
223     MOS_INTERFACE   *m_osInterface = nullptr;
224 
225 MEDIA_CLASS_DEFINE_END(encode__EncodeBasicFeature)
226 };
227 
228 }  // namespace encode
229 
230 #endif  // !__ENCODE_BASIC_FEATURE_H__
231