1 /*
2 * Copyright (c) 2018, 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_overlap.h
24 //! \brief    Defines of the ROI overlap
25 //! \brief    Defines of the ROI overlap
26 //!
27 
28 #ifndef __CODECHAL_HEVC_VDENC_ROI_OVERLAP_H__
29 #define __CODECHAL_HEVC_VDENC_ROI_OVERLAP_H__
30 
31 namespace encode
32 {
33 
34 using UintVector = std::vector<uint32_t>;
35 
36 class RoiStrategy;
37 
38 //!
39 //! \class    RoiOverlap
40 //!
41 //! \brief    Handle the overlap between ROI and Dirty ROI.
42 //!
43 //! \detail   The main purpose of this class is handling the overlap between
44 //!           ROI and dirty ROI. But in order to deal with all cases in a same
45 //!           way, we will use this class although there is no overlap between
46 //!           ROI and dirty ROI.
47 //!           In this class will hold a overlap map to store the description of
48 //!           each LCU. The description include overlap marker and ROI region
49 //!           which the LCU belong to. For detail of overlap mark, please check
50 //!           the definition of OverlapMarker.
51 //!
52 class RoiOverlap
53 {
54 public:
55     enum OverlapMarker
56     {
57         mkRoi = 1,
58         mkRoiNone64Align,
59         mkRoiBk,
60         mkRoiBkNone64Align,
61         mkDirtyRoi,
62         mkDirtyRoiNone64Align,
63         mkDirtyRoiBk,
64         mkDirtyRoiBkNone64Align
65     };
66 
67 
68     RoiOverlap() = default;
69 
70     ~RoiOverlap();
71 
72     //!
73     //! \brief  Update the state of overlap
74     //!
75     //! \param  [in] lcuNumber
76     //!         Number of LCU in stream-in buffer
77     //! \return void
78     //!
79     void Update(uint32_t lcuNumber);
80 
81     //!
82     //! \brief  Save the index of LCUs
83     //!
84     //! \param  [in] lcus
85     //!         the vector of LCU which keeping the LCUs' index
86     //! \return void
87     //!
88     void MarkLcus(
89         const UintVector lcus,
90         OverlapMarker marker,
91         int32_t roiRegionIndex = m_maskRoiRegionIndex)
92     {
93         for (auto lcu : lcus)
94         {
95             MarkLcu(lcu, marker, roiRegionIndex);
96         }
97     }
98 
99     //!
100     //! \brief  mark the specific LCU with provided marker
101     //!
102     //! \param  [in] lcus
103     //!         Index of LCU
104     //! \param  [in] marker
105     //!         overlap marker
106     //! \return void
107     //!
108     void MarkLcu(uint32_t lcu, OverlapMarker marker);
109 
110     //!
111     //! \brief  Write streamin data according to the overlap map
112     //!
113     //! \param  [in] roi
114     //!         ROI strategy
115     //! \param  [in] dirtyRoi
116     //!         Dirty ROI strategy
117     //! \param  [in, out] streaminBuffer
118     //!         streamin buffer
119     //! \return MOS_STATUS
120     //!         MOS_STATUS_SUCCESS if success, else fail reason
121     //!
122     MOS_STATUS WriteStreaminData(
123         RoiStrategy *roi,
124         RoiStrategy *dirtyRoi,
125         uint8_t *streaminBuffer);
126 
127 private:
128     //!
129     //! \brief  mark the specific LCU with provided marker and region index
130     //!
131     //! \param  [in] lcus
132     //!         Index of LCU
133     //! \param  [in] marker
134     //!         overlap marker
135     //! \param  [in] roiRegionIndex
136     //!         Index of ROI region
137     //! \return void
138     //!
139     void MarkLcu(uint32_t lcu, OverlapMarker marker, int32_t roiRegionIndex);
140 
141     //!
142     //! \brief  Check whether the marker can be written to the specific LCU.
143     //!
144     //! \param  [in] lcu
145     //!         index of LCU
146     //! \param  [in] marker
147     //!         overlap marker
148     //! \return bool
149     //!         true if can write the marker, otherwise false
150     //!
151     bool CanWriteMark(uint32_t lcu, OverlapMarker marker);
152 
153     //!
154     //! \brief  Get the ROI region index from the overlap map data.
155     //!
156     //! \param  [in] data
157     //!         overlap map data
158     //! \return uint32_t
159     //!         ROI region index
160     //!
GetRoiRegionIndex(uint16_t data)161     uint32_t GetRoiRegionIndex(uint16_t data)
162     {
163         return (data >> m_bitNumberOfOverlapMarker) & m_maskRoiRegionIndex;
164     }
165 
166     //!
167     //! \brief  Check whether the specific mark is for ROI or not
168     //!
169     //! \param  [in] marker
170     //!         overlap marker
171     //! \return bool
172     //!         true if the marker is for ROI, otherwise false
173     //!
IsRoiMarker(OverlapMarker marker)174     bool IsRoiMarker(OverlapMarker marker)
175     {
176         return (marker == mkRoi ||
177                 marker == mkRoiBk ||
178                 marker == mkRoiNone64Align ||
179                 marker == mkRoiBkNone64Align);
180     }
181 
182     //!
183     //! \brief  Check whether the specific mark is for dirty ROI or not
184     //!
185     //! \param  [in] marker
186     //!         overlap marker
187     //! \return bool
188     //!         true if the marker is for dirty ROI, otherwise false
189     //!
IsDirtyRoiMarker(OverlapMarker marker)190     bool IsDirtyRoiMarker(OverlapMarker marker)
191     {
192         return (marker == mkDirtyRoi ||
193             marker == mkDirtyRoiBk ||
194             marker == mkDirtyRoiNone64Align ||
195             marker == mkDirtyRoiBkNone64Align);
196     }
197 
198     static const uint16_t m_maskRoiRegionIndex = 0x7FF; //<! Mask for ROI region index in overlap map
199     static const uint16_t m_maskOverlapMarker  = 0x1F;  //<! Mask for overlap marker in overlap map
200     static const uint8_t  m_bitNumberOfOverlapMarker  = 5;   //<! Bit number of overlap marker in overlap map
201 
202     uint32_t   m_lcuNumber  = 0;       //<! Number of LCU
203 
204 protected:
205     //! This map is a array of LCU description. The description is a unsigned
206     //! 16 bit integer data, In each description includes overlap marker and
207     //! which ROI region the LCU belong to. The structure of each descrioption
208     //! as Following.
209     //!
210     //! 15 14 13 12 11 10 9 8 7 6 5 | 4 3 2 1 0
211     //!   ROI     region     index  |  Marker
212     //!
213     //! We can use GetRoiRegionIndex and GetMarker functions to get the region
214     //! index marker from description
215     //!
216     uint16_t *m_overlapMap = nullptr;  //<! Overlap map buffer
217 
218     //!
219     //! \brief  Get the marker from the overlap map data.
220     //!
221     //! \param  [in] data
222     //!         overlap map data
223     //! \return OverlapMarker
224     //!         overlap marker
225     //!
GetMarker(uint16_t data)226     OverlapMarker GetMarker(uint16_t data)
227     {
228         return (OverlapMarker)(data & m_maskOverlapMarker);
229     }
230 
231 MEDIA_CLASS_DEFINE_END(encode__RoiOverlap)
232 };
233 
234 }  // namespace encode
235 #endif  //<! __CODECHAL_HEVC_VDENC_ROI_OVERLAP_H__