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_pak_integrate_packet.h
24 //! \brief    Defines the implementation of pak integrate packet
25 //!
26 
27 #ifndef __CODECHAL_PAK_INTEGRATE_PACKET_H__
28 #define __CODECHAL_PAK_INTEGRATE_PACKET_H__
29 
30 #include "media_cmd_packet.h"
31 #include "encode_huc.h"
32 #include "media_pipeline.h"
33 #include "codec_hw_next.h"
34 #include "encode_utils.h"
35 #include "encode_hevc_vdenc_pipeline.h"
36 #include "encode_hevc_basic_feature.h"
37 
38 namespace encode
39 {
40 class HevcPakIntegratePkt : public EncodeHucPkt
41 {
42 public:
43 #define MAX_PAK_NUM          8
44 #define HUC_BATCH_BUFFER_END 0x05000000
45 #define HUC_CMD_LIST_MODE    1
46 
47     //!
48     //! \struct HucPakIntegrateDmem
49     //! \brief  The struct of Huc Pak integrate Dmem
50     //!
51     struct HucPakIntegrateDmem
52     {
53         uint32_t TileSizeRecord_offset[MAX_PAK_NUM + 1];    //!< Tile Size Records, start offset  in byte, 0xffffffff means unavailable
54         uint32_t VDENCSTAT_offset[MAX_PAK_NUM + 1];         //!< needed for HEVC VDEnc, VP9 VDEnc, start offset  in byte, 0xffffffff means unavailable
55         uint32_t HEVC_PAKSTAT_offset[MAX_PAK_NUM + 1];      //!< needed for HEVC VDEnc, start offset  in byte, 0xffffffff means unavailable
56         uint32_t HEVC_Streamout_offset[MAX_PAK_NUM + 1];    //!< needed for HEVC VDEnc, start offset  in byte, 0xffffffff means unavailable
57         uint32_t VP9_PAK_STAT_offset[MAX_PAK_NUM + 1];      //!< needed for VP9 VDEnc, start offset  in byte, 0xffffffff means unavailable
58         uint32_t Vp9CounterBuffer_offset[MAX_PAK_NUM + 1];  //!< needed for VP9 VDEnc, start offset  in byte, 0xffffffff means unavailable
59         uint32_t LastTileBS_StartInBytes;                   //!< last tile in bitstream for region 4 and region 5
60         uint32_t SliceHeaderSizeinBits;                     //!< needed for HEVC dual pipe BRC
61         uint16_t TotalSizeInCommandBuffer;                  //!< Total size in bytes of valid data in the command buffer
62         uint16_t OffsetInCommandBuffer;                     //!< Byte  offset of the to-be-updated Length (uint32_t) in the command buffer, 0xffff means unavailable
63         uint16_t PicWidthInPixel;                           //!< Picture width in pixel
64         uint16_t PicHeightInPixel;                          //!< Picture hieght in pixel
65         uint16_t TotalNumberOfPAKs;                         //!< [2..4]
66         uint16_t NumSlices[MAX_PAK_NUM];                    //!< this is number of slices from each PAK
67         uint16_t NumTiles[MAX_PAK_NUM];                     //!< this is number of tiles from each PAK
68         uint16_t PIC_STATE_StartInBytes;                    //!< offset for  region 7 and region 8
69         uint8_t  Codec;                                     //!< 1: HEVC DP; 2: HEVC VDEnc; 3: VP9 VDEnc
70         uint8_t  MAXPass;                                   //!< Max number of BRC pass >=1
71         uint8_t  CurrentPass;                               //!< Current BRC pass [1..MAXPass]
72         uint8_t  MinCUSize;                                 //!< Minimum CU size (3: 8x8, 4:16x16), HEVC only.
73         uint8_t  CabacZeroWordFlag;                         //!< cabac zero flag, HEVC only
74         uint8_t  bitdepth_luma;                             //!< luma bitdepth, HEVC only
75         uint8_t  bitdepth_chroma;                           //!< chroma bitdepth, HEVC only
76         uint8_t  ChromaFormatIdc;                           //!< chroma format idc, HEVC only
77         uint8_t  currFrameBRClevel;                         //!< Hevc dual pipe only
78         uint8_t  brcUnderFlowEnable;                        //!< Hevc dual pipe only
79         uint8_t  StitchEnable;                              //!< enable stitch cmd for Hevc dual pipe
80         uint8_t  reserved1;                                 //!< reserved field
81         uint16_t StitchCommandOffset;                       //!< offset in region 10 which is the second level batch buffer
82         uint16_t reserved2;                                 //!< reserved field
83         uint32_t BBEndforStitch;                            //!< Batch buffer end for stitch
84         uint8_t  RSVD[32];                                  //!< Reserved field for debug perpose
85     };
86 
87     //!
88     //! \struct HucInputCmd
89     //! \brief  The struct of Huc input command
90     //!
91     struct HucInputCmd
92     {
93         uint8_t  SelectionForIndData = 0;
94         uint8_t  CmdMode             = 0;
95         uint16_t LengthOfTable       = 0;
96 
97         uint32_t SrcBaseOffset  = 0;
98         uint32_t DestBaseOffset = 0;
99 
100         uint32_t Reserved[3] = {0};
101 
102         uint32_t CopySize = 0;
103 
104         uint32_t ReservedCounter[4] = {0};
105 
106         uint32_t SrcAddrBottom  = 0;
107         uint32_t SrcAddrTop     = 0;
108         uint32_t DestAddrBottom = 0;
109         uint32_t DestAddrTop    = 0;
110     };
111 
112     //!
113     //! \struct HucCommandData
114     //! \brief  The struct of Huc commands data
115     //!
116     struct HucCommandData
117     {
118         uint32_t TotalCommands;   //!< Total Commands in the Data buffer
119         struct
120         {
121             uint16_t ID;          //!< Command ID, defined and order must be same as that in DMEM
122             uint16_t SizeOfData;  //!< data size in uint32_t
123             uint32_t data[40];
124         } InputCOM[10];
125     };
126 
HevcPakIntegratePkt(MediaPipeline * pipeline,MediaTask * task,CodechalHwInterfaceNext * hwInterface)127     HevcPakIntegratePkt(MediaPipeline *pipeline, MediaTask *task, CodechalHwInterfaceNext *hwInterface) : EncodeHucPkt(pipeline, task, hwInterface)
128     {
129         m_hcpItf = std::static_pointer_cast<mhw::vdbox::hcp::Itf>(hwInterface->GetHcpInterfaceNext());
130         ENCODE_CHK_NULL_NO_STATUS_RETURN(m_hcpItf);
131     }
132 
~HevcPakIntegratePkt()133     virtual ~HevcPakIntegratePkt() { FreeResources(); };
134 
135     virtual MOS_STATUS Init() override;
136 
137     MOS_STATUS Submit(MOS_COMMAND_BUFFER *commandBuffer, uint8_t packetPhase = otherPacket) override;
138 
139     //!
140     //! \brief  Calculate Command Size
141     //!
142     //! \param  [in, out] commandBufferSize
143     //!         requested size
144     //! \param  [in, out] requestedPatchListSize
145     //!         requested size
146     //! \return MOS_STATUS
147     //!         status
148     //!
149     virtual MOS_STATUS CalculateCommandSize(
150         uint32_t &commandBufferSize,
151         uint32_t &requestedPatchListSize) override;
152 
153     //!
154     //! \brief  Get Packet Name
155     //! \return std::string
156     //!
GetPacketName()157     virtual std::string GetPacketName() override
158     {
159         return "PAKINTEGRATE";
160     }
161 
162     //!
163     //! \brief  One frame is completed
164     //! \param  [in] mfxStatus
165     //!         pointer to status buffer which for MFX
166     //! \param  [in] rcsStatus
167     //!         pointer to status buffer which for RCS
168     //! \param  [in, out] statusReport
169     //!         pointer of EncoderStatusReport
170     //! \return MOS_STATUS
171     //!         MOS_STATUS_SUCCESS if success, else fail reason
172     //!
173     virtual MOS_STATUS Completed(void *mfxStatus, void *rcsStatus, void *statusReport) override;
174 
175 protected:
176     MHW_SETPAR_DECL_HDR(HUC_IMEM_STATE);
177     MHW_SETPAR_DECL_HDR(HUC_DMEM_STATE);
178     MHW_SETPAR_DECL_HDR(HUC_VIRTUAL_ADDR_STATE);
179     virtual MOS_STATUS AllocateResources() override;
180 
181     virtual MOS_STATUS SetDmemBuffer() const;
182 
183     MOS_STATUS ReadSseStatistics(MOS_COMMAND_BUFFER &cmdBuffer);
184     MOS_STATUS ReadSliceSize(MOS_COMMAND_BUFFER &cmdBuffer);
185     MOS_STATUS ReadSliceSizeForSinglePipe(MOS_COMMAND_BUFFER &cmdBuffer);
186     MOS_STATUS ReadHcpStatus(
187         MHW_VDBOX_NODE_IND  vdboxIndex,
188         MediaStatusReport * statusReport,
189         MOS_COMMAND_BUFFER &cmdBuffer);
190 
191     //!
192     //! \brief    Retreive BRC Pak statistics
193     //!
194     //! \param    [in] cmdBuffer
195     //!           Pointer to command buffer
196     //! \param    [in] params
197     //!           BRC pak statistics parameters
198     //!
199     //! \return   MOS_STATUS
200     //!           MOS_STATUS_SUCCESS if success, else fail reason
201     //!
202     virtual MOS_STATUS ReadBrcPakStatistics(
203         PMOS_COMMAND_BUFFER          cmdBuffer,
204         EncodeReadBrcPakStatsParams *params);
205 
206             // Inline functions
207     MOS_STATUS ValidateVdboxIdx(const MHW_VDBOX_NODE_IND &vdboxIndex);
208 
209     MOS_STATUS EndStatusReport(
210         uint32_t            srType,
211         MOS_COMMAND_BUFFER *cmdBuffer) override;
212 
213     void UpdateParameters();
214 
215     //!
216     //! \brief  Setup status data of tiles when one frame is completed
217     //! \param  [in] mfxStatus
218     //!         pointer to status buffer which for MFX
219     //! \param  [in, out] statusReport
220     //!         pointer of EncoderStatusReport
221     //! \return MOS_STATUS
222     //!         MOS_STATUS_SUCCESS if success, else fail reason
223     //!
224     MOS_STATUS SetupTilesStatusData(void *mfxStatus, void *statusReport);
225 
226     //!
227     //! \brief  Perform Software Stitch for bitstream
228     //! \param  [in] tileReportData
229     //!         pointer of tile report data
230     //! \param  [in] tileStatusReport
231     //!         pointer of tile status report
232     //! \param  [in, out] statusReportData
233     //!         pointer to status report data
234     //! \return MOS_STATUS
235     //!         MOS_STATUS_SUCCESS if success, else fail reason
236     //!
237     MOS_STATUS PerformSwStitch(
238         const EncodeReportTileData *tileReportData,
239         PakHwTileSizeRecord *       tileStatusReport,
240         EncodeStatusReportData *    statusReportData);
241 
242     //!
243     //! \brief  Perform Hardware Stitch for bitstream
244     //! \param  [out] cmdBuffer
245     //!         Cmd buffer to add hardware stitch cmds
246     //! \return MOS_STATUS
247     //!         MOS_STATUS_SUCCESS if success, else fail reason
248     //!
249     MOS_STATUS PerformHwStitch(PMOS_COMMAND_BUFFER cmdBuffer);
250 
251     //!
252     //! \brief  Configure stitich data buffer
253     //!
254     //! \return MOS_STATUS
255     //!         MOS_STATUS_SUCCESS if success, else fail reason
256     //!
257     MOS_STATUS ConfigStitchDataBuffer() const;
258 
259     //!
260     //! \brief  Free resources
261     //!
262     //! \return MOS_STATUS
263     //!         MOS_STATUS_SUCCESS if success, else fail reason
264     //!
265     MOS_STATUS FreeResources();
266 
267 #if USE_CODECHAL_DEBUG_TOOL
268     virtual MOS_STATUS DumpInput() override;
269     virtual MOS_STATUS DumpOutput() override;
270 #endif
271 
272     MOS_STATUS AddCondBBEndForLastPass(MOS_COMMAND_BUFFER &cmdBuffer);
273 
274     static constexpr uint32_t m_vdboxHucPakIntKernelDescriptor = 15;  //!< Huc pak integrate kernel descriptor
275 
276     EncodeMemComp *m_mmcState = nullptr;
277 
278     uint32_t      m_vdencHucPakDmemBufferSize = sizeof(HucPakIntegrateDmem);    //!< Indicate the size of Dmem buffer of Huc pak integrate kernel
279     PMOS_RESOURCE m_resHucPakStitchDmemBuffer[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM][CODECHAL_VDENC_BRC_NUM_OF_PASSES] = {};  //!< HuC Pak Integration Dmem data for each pass
280 
281     MOS_RESOURCE     m_resHucStitchDataBuffer[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM][CODECHAL_VDENC_BRC_NUM_OF_PASSES] = {};
282     MHW_BATCH_BUFFER m_HucStitchCmdBatchBuffer = {};
283 
284     static constexpr const uint32_t m_hwStitchCmdSize = 20 * sizeof(uint32_t);  //!< Cmd size for hw stitch
285     bool m_vdencHucUsed = false;                 //!< Indicate if it is needed to use Huc pak integrate kernel
286     HevcBasicFeature *m_basicFeature = nullptr;  //!< Hevc Basic Feature used in each frame
287     MHW_VDBOX_NODE_IND              m_vdboxIndex      = MHW_VDBOX_NODE_1;      //!< Index of VDBOX
288 
289     std::shared_ptr<mhw::vdbox::hcp::Itf> m_hcpItf = nullptr;
290 
291 MEDIA_CLASS_DEFINE_END(encode__HevcPakIntegratePkt)
292 };
293 
294 }  // namespace encode
295 #endif
296