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_tile.h
24 //! \brief    Defines the common interface for encode tile
25 //!
26 #ifndef __ENCODE_TILE_H__
27 #define __ENCODE_TILE_H__
28 #include <array>
29 
30 #include "encode_basic_feature.h"
31 #include "encode_pipeline.h"
32 
33 #define __THIRD_LVL_BB_DEFAULT_COUNT 4
34 
35 namespace encode
36 {
37 struct EncodeTileCodingParams
38 {
39     uint32_t        NumOfTilesInFrame;
40     uint32_t        NumOfTileColumnsInFrame;
41     uint32_t        TileStartLCUX;
42     uint32_t        TileStartLCUY;
43     uint16_t        TileHeightInMinCbMinus1;
44     uint16_t        TileWidthInMinCbMinus1;
45     bool            IsLastTileofColumn;
46     bool            IsLastTileofRow;
47     uint32_t        TileRowStoreSelect;
48     uint32_t        TileColumnStoreSelect;
49     uint32_t        Mode;
50     bool            IsFirstPass;
51     bool            IsLastPass;
52     bool            bTileReplayEnable;
53 
54     // Offsets for scalability
55     uint32_t            NumberOfActiveBePipes;
56     uint32_t            BitstreamByteOffset;
57     uint32_t            PakTileStatisticsOffset;
58     uint32_t            CuLevelStreamoutOffset;
59     uint32_t            SliceSizeStreamoutOffset;
60     uint32_t            CuRecordOffset;
61     uint32_t            SseRowstoreOffset;
62     uint32_t            SaoRowstoreOffset;
63     uint32_t            TileSizeStreamoutOffset;
64     uint32_t            Vp9ProbabilityCounterStreamoutOffset;
65     uint32_t            TileStreaminOffset;
66     uint32_t            CumulativeCUTileOffset;
67     uint32_t            TileLCUStreamOutOffset;
68 
69     PMOS_RESOURCE   presHcpSyncBuffer; // this buffer is not used for either HEVC/VP9 encoder and decoder.
70 
71     //Decode specific sparameters
72     uint8_t                           ucNumDecodePipes;
73     uint8_t                           ucPipeIdx;
74 };
75 
76 static constexpr uint32_t m_maxTileBdNum = 100;  //!> Max number of tile boundaries in row or column
77 static constexpr uint32_t m_codecHalNumTileLevelBatchBuffers = 3;
78 //!
79 //! \struct   EncodeTileData
80 //! \brief    Encode tile data for each tile
81 //!
82 struct EncodeTileData
83 {
84     uint32_t numOfTilesInFrame;
85     uint32_t numOfTileColumnsInFrame;
86     union
87     {
88         uint32_t tileStartXInLCU;
89         uint32_t tileStartXInSb;
90         uint32_t tileStartX;
91     };
92     union
93     {
94         uint32_t tileEndXInLCU;
95         uint32_t tileEndXInSb;
96         uint32_t tileEndX;
97     };
98     union
99     {
100         uint32_t tileStartYInLCU;
101         uint32_t tileStartYInSb;
102         uint32_t tileStartY;
103     };
104     union
105     {
106         uint32_t tileEndYInLCU;
107         uint32_t tileEndYInSb;
108         uint32_t tileEndY;
109     };
110     union
111     {
112         uint16_t tileHeightInMinCbMinus1;
113         uint16_t tileHeightInMinSbMinus1;
114         uint16_t tileHeightInMinMinus1;
115     };
116     union
117     {
118         uint16_t tileWidthInMinCbMinus1;
119         uint16_t tileWidthInMinSbMinus1;
120         uint16_t tileWidthInMinMinus1;
121     };
122     uint32_t tileRowStoreSelect;
123     uint32_t tileColumnStoreSelect;
124     uint32_t mode;
125     bool     isLastTileofColumn;
126     bool     isLastTileofRow;
127     bool     isFirstPass;  // for tile replay
128     bool     isLastPass;   // for tile replay
129     bool     tileReplayEnable;
130 
131     // Offsets for scalability
132     uint32_t bitstreamByteOffset;
133     uint32_t pakTileStatisticsOffset;
134     uint32_t cuLevelStreamoutOffset;
135     uint32_t sliceSizeStreamoutOffset;
136     uint32_t cuRecordOffset;
137     uint32_t sseRowstoreOffset;
138     uint32_t saoRowstoreOffset;
139     uint32_t tileSizeStreamoutOffset;
140     uint32_t vp9ProbabilityCounterStreamoutOffset;
141     uint32_t tileStreaminOffset;
142     uint32_t cumulativeCUTileOffset;
143     uint32_t tileLCUStreamOutOffset;
144 };
145 
146 struct EncodeReportTileData
147 {
148     bool     reportValid = false;
149     uint16_t tileHeightInMinCbMinus1 = 0;
150     uint16_t tileWidthInMinCbMinus1 = 0;
151     uint32_t bitstreamByteOffset = 0;
152     uint32_t numTileColumns = 1;
153 };
154 
155 class EncodePipeline;
156 
157 class EncodeTile : public MediaFeature
158 {
159 public:
160     EncodeTile(MediaFeatureManager *featureManager,
161         EncodeAllocator *allocator,
162         CodechalHwInterfaceNext *hwInterface,
163         void *constSettings);
164 
165     virtual ~EncodeTile();
166 
167     //!
168     //! \brief  Init encode parameter
169     //! \param  [in] settings
170     //!         Pointer to settings
171     //! \return MOS_STATUS
172     //!         MOS_STATUS_SUCCESS if success, else fail reason
173     //!
174     virtual MOS_STATUS Init(void *settings);
175 
176     //!
177     //! \brief  Update encode parameter
178     //! \param  [in] params
179     //!         Pointer to parameters
180     //! \return MOS_STATUS
181     //!         MOS_STATUS_SUCCESS if success, else fail reason
182     //!
183     virtual MOS_STATUS Update(void *params);
184 
185     //! \brief  Allocate feature related resources
186     //! \return MOS_STATUS
187     //!         MOS_STATUS_SUCCESS if success, else fail reason
188     //!
189     virtual MOS_STATUS AllocateResources();
190 
191     //!
192     //! \brief  Begin Patching third Level Batch Buffer
193     //! \param  [out] cmdBuffer
194     //!         Reference of cmdBuffer constructed from tile feature
195     //! \return MOS_STATUS
196     //!         MOS_STATUS_SUCCESS if success, else fail reason
197     //!
198     virtual MOS_STATUS BeginPatch3rdLevelBatch(
199         MOS_COMMAND_BUFFER &cmdBuffer);
200 
201     //!
202     //! \brief  End Patching third Level Batch Buffer
203     //!
204     //! \return MOS_STATUS
205     //!         MOS_STATUS_SUCCESS if success, else fail reason
206     //!
207     virtual MOS_STATUS EndPatch3rdLevelBatch();
208 
209     //!
210     //! \brief  Get third Level Batch Buffer from Encode Tile features
211     //! \param  [out] thirdLevelBatchBuffer
212     //!         Pointer thirdLevelBatchBuffer got from tile feature
213     //! \return MOS_STATUS
214     //!         MOS_STATUS_SUCCESS if success, else fail reason
215     //!
216     virtual MOS_STATUS GetThirdLevelBatchBuffer(
217         PMHW_BATCH_BUFFER &thirdLevelBatchBuffer);
218 
219     //!
220     //! \brief  Increment the current third Level Batch Buffer from Encode Tile features
221     //! \return MOS_STATUS
222     //!         MOS_STATUS_SUCCESS if success, else fail reason
223     //!
224     virtual MOS_STATUS IncrementThirdLevelBatchBuffer();
225 
226     //!
227     //! \brief  Begin Patching tile Level Batch Buffer
228     //! \param  [in] tileRowPass
229     //!         Current tile Row Pass
230     //! \param  [out] cmdBuffer
231     //!         Reference of cmdBuffer constructed from tile feature
232     //! \return MOS_STATUS
233     //!         MOS_STATUS_SUCCESS if success, else fail reason
234     //!
235     virtual MOS_STATUS BeginPatchTileLevelBatch(
236         uint32_t            tileRowPass,
237         MOS_COMMAND_BUFFER &cmdBuffer);
238 
239     //!
240     //! \brief  End Patching tile Level Batch Buffer
241     //!
242     //! \return MOS_STATUS
243     //!         MOS_STATUS_SUCCESS if success, else fail reason
244     //!
245     virtual MOS_STATUS EndPatchTileLevelBatch();
246 
247     //!
248     //! \brief  Get tile Level Batch Buffer from Encode Tile features
249     //! \param  [in] tileRowPass
250     //!         Tile Row Pass used to get batch buffer
251     //! \param  [out] tileLevelBatchBuffer
252     //!         Pointer tileLevelBatchBuffer got from tile feature
253     //! \param  [out] tileLevelBatchSize
254     //!         Tile level batch buffer size
255     //! \return MOS_STATUS
256     //!         MOS_STATUS_SUCCESS if success, else fail reason
257     //!
258     virtual MOS_STATUS GetTileLevelBatchBuffer(
259         PMHW_BATCH_BUFFER &tileLevelBatchBuffer);
260 
261     //!
262     //! \brief  Get tile data from Encode Tile features by tile index
263     //! \param  [out] tileData
264     //!         Current tile data got from tile feature
265     //! \param  [in] index
266     //!         Current tile data got from tile feature
267     //! \return MOS_STATUS
268     //!         MOS_STATUS_SUCCESS if success, else fail reason
269     //!
270     virtual MOS_STATUS GetTileByIndex(
271         EncodeTileData &tileData,
272         uint32_t  index);
273 
GetTileIdx(uint32_t & idx)274     MOS_STATUS GetTileIdx(uint32_t &idx)
275     {
276         idx = m_tileIdx;
277         return MOS_STATUS_SUCCESS;
278     }
279 
280     //!
281     //! \brief  Get current codechal tile data Encode Tile features
282     //! \param  [out] tileData
283     //!         Current tile data got from tile feature
284     //! \return MOS_STATUS
285     //!         MOS_STATUS_SUCCESS if success, else fail reason
286     //!
287     virtual MOS_STATUS GetCurrentTile(
288         EncodeTileData &tileData);
289 
290     //!
291     //! \brief  Set current tile data Encode Tile feature refers to
292     //! \param  [in] tileRow
293     //!         Tile row idx to set
294     //! \param  [in] tileCol
295     //!         Tile column idx to set
296     //! \param  [in] pipeline
297     //!         Pipeline used
298     //! \return MOS_STATUS
299     //!         MOS_STATUS_SUCCESS if success, else fail reason
300     //!
301     virtual MOS_STATUS SetCurrentTile(
302         uint32_t        tileRow,
303         uint32_t        tileCol,
304         EncodePipeline *pipeline);
305 
306     //!
307     //! \brief  Get tile based statistics buffer
308     //! \param  [in] idx
309     //!         Index of buffer to get
310     //! \param  [out] buffer
311     //!         Reference to the buffer get from Tile feature
312     //! \return MOS_STATUS
313     //!         MOS_STATUS_SUCCESS if success, else fail reason
314     //!
315     virtual MOS_STATUS GetTileBasedStatisticsBuffer(uint32_t idx, MOS_RESOURCE*& buffer) const;
316 
317     //!
318     //! \brief  Get tile record buffer of PAK integration kernel output
319     //! \param  [in] idx
320     //!         Index of buffer to get
321     //! \param  [out] buffer
322     //!         Reference to the buffer get from Tile feature
323     //! \return MOS_STATUS
324     //!         MOS_STATUS_SUCCESS if success, else fail reason
325     //!
326     virtual MOS_STATUS GetTileRecordBuffer(uint32_t idx, MOS_RESOURCE *&buffer);
327 
328     //!
329     //! \brief  Get Pak Aggregated Frame Stats Buffer buffer as PAK integration kernel output
330     //! \param  [out] buffer
331     //!         Reference to the buffer get from Tile feature
332     //! \return MOS_STATUS
333     //!         MOS_STATUS_SUCCESS if success, else fail reason
334     //!
335     virtual MOS_STATUS GetHucPakAggregatedFrameStatsBuffer(MOS_RESOURCE *&buffer);
336 
337     //!
338     //! \brief  Get tile statistic index
339     //! \param  [out] idx
340     //!         Reference to the buffer index get from Tile feature
341     //! \return MOS_STATUS
342     //!         MOS_STATUS_SUCCESS if success, else fail reason
343     //!
344     virtual MOS_STATUS GetStatisticsBufferIndex(uint32_t &idx);
345 
346     //!
347     //! \brief  Get tile statistic index of previous frame
348     //! \param  [out] idx
349     //!         Reference to the buffer index get from Tile feature
350     //! \return MOS_STATUS
351     //!         MOS_STATUS_SUCCESS if success, else fail reason
352     //!
353     virtual MOS_STATUS GetPrevStatisticsBufferIndex(uint32_t& idx);
354 
355     //!
356     //! \brief  Get tile statistic index
357     //! \param  [out] idx
358     //!         Reference to the buffer index get from Tile feature
359     //! \return MOS_STATUS
360     //!         MOS_STATUS_SUCCESS if success, else fail reason
361     //!
GetTileNum(uint32_t & num)362     virtual MOS_STATUS GetTileNum(uint32_t &num)
363     {
364         num = m_numTiles;
365         return MOS_STATUS_SUCCESS;
366     }
367 
368     //!
369     //! \brief  Get tile report data buffer
370     //! \param  [in] idx
371     //!         Index of buffer to get
372     //! \param  [out] buffer
373     //!         Reference to the buffer get from Tile feature
374     //! \return MOS_STATUS
375     //!         MOS_STATUS_SUCCESS if success, else fail reason
376     //!
377     virtual MOS_STATUS GetReportTileData(uint32_t idx, const EncodeReportTileData *&reportTileData);
378 
379     //!
380     //! \brief  Get tile row and columns number from Tile feature
381     //! \param  [out] row
382     //!         Tile row number
383     //! \param  [out] col
384     //!         Tile column number
385     //! \return MOS_STATUS
386     //!         MOS_STATUS_SUCCESS if success, else fail reason
387     //!
388     virtual MOS_STATUS GetTileRowColumns(uint16_t &row, uint16_t &col);
389 
390     //!
391     //! \brief    Setup if Codechal Tile data
392     //!
393     //! \param  [in] isValid
394     //!         Indicate if in current frame tile report data valid
395     //!
396     //! \return   MOS_STATUS
397     //!           MOS_STATUS_SUCCESS if success, else fail reason
398     //!
399     virtual MOS_STATUS SetTileReportDataVaild(bool isValid);
400 
401     //!
402     //! \brief    Check if tile feature is enabled
403     //! \param    [out] enabled
404     //!           Enabled flag
405     //! \return   MOS_STATUS
406     //!           MOS_STATUS_SUCCESS if success, else fail reason
407     //!
IsEnabled(bool & enabled)408     virtual MOS_STATUS IsEnabled(bool &enabled)
409     {
410         enabled = m_enabled;
411         return MOS_STATUS_SUCCESS;
412     }
413 
414 
415     //!
416     //! \brief    Check if tile replay feature is enabled
417     //! \param    [out] enabled
418     //!           Enabled flag
419     //! \return   MOS_STATUS
420     //!           MOS_STATUS_SUCCESS if success, else fail reason
421     //!
IsTileReplayEnabled(bool & enabled)422     MOS_STATUS IsTileReplayEnabled(bool& enabled)
423     {
424         enabled = m_enableTileReplay;
425         return MOS_STATUS_SUCCESS;
426     }
427 
428 protected:
429     //! \brief    Allocate the batch buffer for each tile
430     //!
431     //! \return   MOS_STATUS
432     //!           MOS_STATUS_SUCCESS if success, else fail reason
433     //!
434     virtual MOS_STATUS AllocateTileLevelBatch();
435 
436     //! \brief    Allocate the BRC batch buffer for each tile row
437     //!
438     //! \return   MOS_STATUS
439     //!           MOS_STATUS_SUCCESS if success, else fail reason
440     //!
AllocateTileRowLevelBRCBatch()441     virtual MOS_STATUS AllocateTileRowLevelBRCBatch() { return MOS_STATUS_SUCCESS; }
442 
443     //!
444     //! \brief    Free the batch buffer for each tile row level BRC
445     //!
446     //! \return   MOS_STATUS
447     //!           MOS_STATUS_SUCCESS if success, else fail reason
448     //!
FreeTileRowLevelBRCBatch()449     virtual MOS_STATUS FreeTileRowLevelBRCBatch() { return MOS_STATUS_SUCCESS; }
450 
451     //!
452     //! \brief    Free the batch buffer for each tile
453     //!
454     //! \return   MOS_STATUS
455     //!           MOS_STATUS_SUCCESS if success, else fail reason
456     //!
457     virtual MOS_STATUS FreeTileLevelBatch();
458 
459     //!
460     //! \brief    Setup Codechal Tile data for each tile
461     //!
462     //! \param  [in] params
463     //!         Pointer to parameters
464     //!
465     //! \return   MOS_STATUS
466     //!           MOS_STATUS_SUCCESS if success, else fail reason
467     //!
468     virtual MOS_STATUS SetTileData(void *params) = 0;
469 
470     //!
471     //! \brief    Setup Codechal Tile data to report
472     //! \return   MOS_STATUS
473     //!           MOS_STATUS_SUCCESS if success, else fail reason
474     //!
475     virtual MOS_STATUS SetTileReportData();
476 
477     //!
478     //! \brief    Allocate Tile Statistics
479     //!
480     //! \param  [in] params
481     //!         Pointer to parameters
482     //!
483     //! \return   MOS_STATUS
484     //!           MOS_STATUS_SUCCESS if success, else fail reason
485     //!
486     virtual MOS_STATUS AllocateTileStatistics(void *params) = 0;
487 
488     EncodeAllocator          *m_allocator         = nullptr;  //!< Allocator used in tile feature
489     EncodeBasicFeature       *m_basicFeature      = nullptr;  //!< EncodeBasicFeature
490     CodechalHwInterfaceNext  *m_hwInterface       = nullptr;  //!< Hw interface as utilities
491     MediaFeatureManager      *m_featureManager    = nullptr;  //!< Pointer to feature manager
492 
493     EncodeTileData       *m_tileData          = nullptr;  //!< Pointer to the Tile data array
494     uint32_t              m_numTiles          = 1;        //!< Total tile numbers
495     uint32_t              m_tileIdx           = 0;        //!< Indicate current tile index
496     uint32_t              m_maxTileNumber     = 0;        //!< Max tile number for curent frame
497     uint32_t              m_maxTileNumberUsed = 0;        //!< Max tile number used for create tile data buffers
498     uint32_t              m_tileRowPass       = 0;        //!< Indicate current tile row pass
499     uint16_t              m_numTileRows       = 1;        //!< Total number of tile rows
500     uint16_t              m_numTileColumns    = 1;        //!< Total number of tile columns
501 
502     //MOS_RESOURCE      m_vdencTileRowStoreBuffer;         //!< Tile row store buffer
503     //MOS_RESOURCE      m_vdencPaletteModeStreamOutBuffer; //!< Palette mode stream out buffer
504     //MOS_RESOURCE      m_resHwCountTileReplay;            //!< Tile based HW Counter buffer
505 
506     EncodeTileCodingParams m_curTileCodingParams = {};  //! Current tile coding paramters
507 
508     // 3rd Level Batch buffer
509     uint32_t                                                                m_thirdLevelBatchSize   = 0;   //!< Size of the 3rd level batch buffer
510     std::array<MHW_BATCH_BUFFER, __THIRD_LVL_BB_DEFAULT_COUNT>              m_thirdLevelBatchBuffers = { 0 };  //!< 3rd level batch buffer
511     std::array<MHW_BATCH_BUFFER, __THIRD_LVL_BB_DEFAULT_COUNT>::iterator    m_currentThirdLevelBatchBuffer;
512 
513     // Tile level batch buffer
514     uint32_t          m_tileLevelBatchSize    = 0;    //!< Size of the 2rd level batch buffer for each tile
515     uint32_t          m_numTileBatchAllocated[m_codecHalNumTileLevelBatchBuffers] ={0};    //!< The number of allocated batch buffer for tiles
516     uint32_t          m_tileBatchBufferIndex = 0;     //!< Current index for tile batch buffer of same frame, updated per frame
517     PMHW_BATCH_BUFFER m_tileLevelBatchBuffer[m_codecHalNumTileLevelBatchBuffers][EncodeBasicFeature::m_vdencBrcPassNum] = {{0}};  //!< Tile level batch buffer for each tile
518     MOS_RESOURCE      m_resTileBasedStatisticsBuffer[EncodeBasicFeature::m_uncompressedSurfaceNum] = {};
519     MOS_RESOURCE      m_resHuCPakAggregatedFrameStatsBuffer = {};
520     MOS_RESOURCE      m_tileRecordBuffer[EncodeBasicFeature::m_uncompressedSurfaceNum] = {};
521 
522     EncodeReportTileData *m_reportTileData[EncodeBasicFeature::m_uncompressedSurfaceNum] = {};
523     uint8_t           m_prevStatisticsBufIndex = 0;
524     uint8_t           m_statisticsBufIndex     = 0;
525 
526     bool              m_enableTileReplay = false;  //!< TileReplay Enable
527 
528 MEDIA_CLASS_DEFINE_END(encode__EncodeTile)
529 };
530 
531 }  // namespace encode
532 
533 #endif  // !__ENCODE_TILE_H__
534