1 /*
2 * Copyright (c) 2018-2020, 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_hevc_vdenc_roi.h
24 //! \brief    Defines of the ROI feature of HEVC VDENC
25 //!
26 
27 #ifndef __CODECHAL_HEVC_VDENC_ROI_H__
28 #define __CODECHAL_HEVC_VDENC_ROI_H__
29 
30 #include "media_feature.h"
31 #include "encode_hevc_vdenc_roi_overlap.h"
32 #include "encode_hevc_vdenc_roi_strategy.h"
33 #include "encode_hevc_brc.h"
34 #include "mhw_vdbox_vdenc_itf.h"
35 #include "mhw_vdbox_huc_itf.h"
36 
37 namespace encode
38 {
39 
40 //!
41 //! \class    HevcVdencRoi
42 //!
43 //! \brief    HevcVdencRoi is unified interface of ROI and Dirty ROI.
44 //!
45 //! \detail   This class is Facade for Native ROI, ForceQP ROI, HuC based
46 //!           ForceQP ROI and Dirty ROI. At the same time, Navtive ROI,
47 //!           ForceQP ROI and Huc based ForceQP, we can only use one of them in
48 //!           one frame, which we can treat them as traditional ROI, different
49 //!           from dirty ROI. Dirty ROI can exist with them at the same time.
50 //!           All of them as a strategy inherit from RoiStrategy, which are
51 //!           responsable for dealing with the ROI related operations.
52 //!
53 
54 class HevcVdencRoi : public MediaFeature, public mhw::vdbox::vdenc::Itf::ParSetting, public mhw::vdbox::huc::Itf::ParSetting
55 {
56 public:
57     HevcVdencRoi(
58         MediaFeatureManager *featureManager,
59         EncodeAllocator *allocator,
60         CodechalHwInterfaceNext *hwInterface,
61         void *constSettings);
62 
~HevcVdencRoi()63     virtual ~HevcVdencRoi() {}
64 
65     //!
66     //! \brief  Init encode parameter
67     //! \param  [in] settings
68     //!         Pointer to settings
69     //! \return MOS_STATUS
70     //!         MOS_STATUS_SUCCESS if success, else fail reason
71     //!
72     virtual MOS_STATUS Init(void *setting) override;
73 
74     //!
75     //! \brief  Update encode parameter
76     //! \param  [in] params
77     //!         Pointer to parameters
78     //! \return MOS_STATUS
79     //!         MOS_STATUS_SUCCESS if success, else fail reason
80     //!
81     virtual MOS_STATUS Update(void *params) override;
82 
83     //!
84     //! \brief    Set VDENC_PIPE_BUF_ADDR parameters
85     //!
86     //! \param    [in, out] PipeBufAddrParams
87     //!           Pipe buf addr parameters
88     //!
89     //! \return   void
90     //!
91     MOS_STATUS SetVdencPipeBufAddrParams(
92         MHW_VDBOX_PIPE_BUF_ADDR_PARAMS &pipeBufAddrParams);
93 
94     //!
95     //! \brief    Setup HuC BRC init/reset parameters
96     //!
97     //! \param    [in,out] hucVdencBrcInitDmem
98     //!           pointer of PCODECHAL_VDENC_HEVC_HUC_BRC_INIT_DMEM_G12
99     //! \return   MOS_STATUS
100     //!           MOS_STATUS_SUCCESS if success, else fail reason
101     //!
102     MOS_STATUS SetDmemHuCBrcInitReset(
103         VdencHevcHucBrcInitDmem *hucVdencBrcInitDmem);
104 
105     //!
106     //! \brief    Sort and set distinct delta QPs
107     //!
108     //! \param    [in] numRoi
109     //!           Number of ROI
110     //! \param    [in] roiRegions
111     //!           ROI regions
112     //! \param    [in] numDistinctDeltaQp
113     //!           number of distinct delta QPs
114     //! \param    [in] roiDistinctDeltaQp
115     //!           data of distinct delta QPs
116     //! \return   bool
117     //!           true if native ROI, otherwise false
118     //!
119     bool ProcessRoiDeltaQp(
120         uint8_t    numRoi,
121         CODEC_ROI  *roiRegions,
122         uint8_t    numDistinctDeltaQp,
123         int8_t     *roiDistinctDeltaQp);
124 
IsArbRoi()125     bool IsArbRoi() const { return m_isArbRoi; }
126 
127 protected:
128     using SeqParams = CODEC_HEVC_ENCODE_SEQUENCE_PARAMS;
129     using PicParams = CODEC_HEVC_ENCODE_PICTURE_PARAMS;
130     using SlcParams = CODEC_HEVC_ENCODE_SLICE_PARAMS;
131 
132     //!
133     //! \brief    Setup ROI
134     //!
135     //! \param    [in] hevcSeqParams
136     //!           pointer of sequence parameters
137     //! \param    [in] hevcPicParams
138     //!           pointer of picture parameters
139     //! \param    [in] hevcSlcParams
140     //!           pointer of slice parameters
141     //! \return   MOS_STATUS
142     //!           MOS_STATUS_SUCCESS if success, else fail reason
143     //!
144     MOS_STATUS ExecuteRoi(
145         SeqParams *hevcSeqParams,
146         PicParams *hevcPicParams,
147         SlcParams *hevcSlcParams);
148 
149     //!
150     //! \brief    Setup Force Delta QP ROI
151     //!
152     //! \param    [in] hevcSeqParams
153     //!           pointer of sequence parameters
154     //! \param    [in] hevcPicParams
155     //!           pointer of picture parameters
156     //! \param    [in] hevcSlcParams
157     //!           pointer of slice parameters
158     //! \return   MOS_STATUS
159     //!           MOS_STATUS_SUCCESS if success, else fail reason
160     //!
161     MOS_STATUS ExecuteRoiExt(
162         SeqParams *hevcSeqParams,
163         PicParams *hevcPicParams,
164         SlcParams *hevcSlcParams);
165 
166     //!
167     //! \brief    Setup Dirty ROI
168     //!
169     //! \param    [in] hevcSeqParams
170     //!           pointer of sequence parameters
171     //! \param    [in] hevcPicParams
172     //!           pointer of picture parameters
173     //! \param    [in] hevcSlcParams
174     //!           pointer of slice parameters
175     //! \return   MOS_STATUS
176     //!           MOS_STATUS_SUCCESS if success, else fail reason
177     //!
178     MOS_STATUS ExecuteDirtyRoi(
179         SeqParams *hevcSeqParams,
180         PicParams *hevcPicParams,
181         SlcParams *hevcSlcParams);
182 
183 
184 
185     //!
186     //! \brief    Write the Streamin data according to the overlap settings.
187     //! \return   MOS_STATUS
188     //!           MOS_STATUS_SUCCESS if success, else fail reason
189     //!
190     MOS_STATUS WriteStreaminData();
191 
192     //!
193     //! \brief    Get the LCU number
194     //! \return   uint32_t
195     //!           LCU number
196     //!
GetLCUNumber()197     uint32_t GetLCUNumber()
198     {
199         uint32_t streamInWidth  =
200             (MOS_ALIGN_CEIL(m_basicFeature->m_frameWidth, 64) / 32);
201         uint32_t streamInHeight =
202             (MOS_ALIGN_CEIL(m_basicFeature->m_frameHeight, 64) / 32) + 8;
203 
204         return (streamInWidth * streamInHeight);
205     }
206 
207     //!
208     //! \brief    ClearStreaminBuffer
209     //! \return   MOS_STATUS
210     //!           MOS_STATUS_SUCCESS if success, else fail reason
211     //!
212     MOS_STATUS ClearStreaminBuffer(uint32_t lucNumber);
213 
214     //!
215     //! \brief    Get strategy for setting command parameters
216     //!
217     //! \detail   When set command parameters, sometimes we need to know ROI or
218     //!           dirty ROI to do this. Here, if ROI enabled, we always use ROI
219     //!           to set these parameters, otherwise, we use dirty ROI.
220     //!
221     //! \return   RoiStrategy *
222     //!           ponter of RoiStrategy
223     //!
GetStrategyForParamsSetting()224     RoiStrategy *GetStrategyForParamsSetting() const
225     {
226         return (m_roiEnabled ?
227             m_strategyFactory.GetRoi() :
228             m_strategyFactory.GetDirtyRoi());
229     }
230 
231     MHW_SETPAR_DECL_HDR(VDENC_PIPE_BUF_ADDR_STATE);
232 
233     MHW_SETPAR_DECL_HDR(VDENC_CMD2);
234 
235     static constexpr uint8_t m_maxNumRoi       = 16;  //!< VDEnc maximum number of ROI supported
236     static constexpr uint8_t m_maxNumNativeRoi = 3;   //!< Number of native ROI supported by VDEnc HW
237 
238     bool m_roiEnabled        = false;    //!< ROI enabled
239     bool m_dirtyRoiEnabled   = false;    //!< dirty ROI enabled
240     bool m_mbQpDataEnabled   = false;    //!< mb qp map enabled
241     bool m_isNativeRoi       = false;    //!< Whether is Native ROI
242     bool m_isArbRoi          = false;    //!< Whether is Adaptive Region Boost ROI
243     bool m_roiMode           = false;    //!< 0 Force qp mode, 1 force delta qp mode
244     bool m_isArbRoiSupported = true;     //!< Whether is Adaptive Region Boost ROI Supported
245 
246     PMOS_RESOURCE      m_streamIn = nullptr; //!< Stream in buffer
247     uint8_t *          m_streamInTemp = nullptr;
248     uint32_t           m_streamInSize = 0;
249     RoiStrategyFactory m_strategyFactory;    //!< Factory of strategy
250     RoiOverlap         m_roiOverlap;         //!< ROI and dirty ROI overlap
251 
252     EncodeAllocator *m_allocator = nullptr;
253     EncodeBasicFeature *m_basicFeature = nullptr;
254     CodechalHwInterfaceNext *m_hwInterface = nullptr;
255     PMOS_INTERFACE m_osInterface = nullptr;
256 
257     bool IFrameIsSet = false;
258     bool PBFrameIsSet = false;
259 
260 MEDIA_CLASS_DEFINE_END(encode__HevcVdencRoi)
261 };
262 
263 }  // namespace encode
264 #endif  //<! __CODECHAL_HEVC_VDENC_ROI_H__