1 /*
2 * Copyright (c) 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_pak_integrate_packet.h
24 //! \brief    Defines the implementation of pak integrate packet
25 //!
26 
27 #ifndef __CODECHAL_AV1_PAK_INTEGRATE_PACKET_H__
28 #define __CODECHAL_AV1_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_av1_basic_feature.h"
36 #include "encode_av1_tile.h"
37 
38 namespace encode
39 {
40 class Av1PakIntegratePkt : 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 4: AV1
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 
Av1PakIntegratePkt(MediaPipeline * pipeline,MediaTask * task,CodechalHwInterfaceNext * hwInterface)127     Av1PakIntegratePkt(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 
~Av1PakIntegratePkt()133     virtual ~Av1PakIntegratePkt() { 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 "AV1PAKINTEGRATE";
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 
187     MOS_STATUS EndStatusReport(
188         uint32_t            srType,
189         MOS_COMMAND_BUFFER *cmdBuffer) override;
190 
191     void UpdateParameters();
192 
193     //!
194     //! \brief  Setup status data of tiles when one frame is completed
195     //! \param  [in] mfxStatus
196     //!         pointer to status buffer which for MFX
197     //! \param  [in, out] statusReport
198     //!         pointer of EncoderStatusReport
199     //! \return MOS_STATUS
200     //!         MOS_STATUS_SUCCESS if success, else fail reason
201     //!
202     MOS_STATUS SetupTilesStatusData(void *mfxStatus, void *statusReport);
203 
204     //!
205     //! \brief  Perform Software Stitch for bitstream
206     //! \param  [in] tileReportData
207     //!         pointer of tile report data
208     //! \param  [in] tileStatusReport
209     //!         pointer of tile status report
210     //! \param  [in, out] statusReportData
211     //!         pointer to status report data
212     //! \return MOS_STATUS
213     //!         MOS_STATUS_SUCCESS if success, else fail reason
214     //!
215     MOS_STATUS PerformSwStitch(
216         const EncodeReportTileData *    tileReportData,
217         PakHwTileSizeRecord *           tileStatusReport,
218         EncodeStatusReportData *        statusReportData);
219 
220     //!
221     //! \brief  Perform Hardware Stitch for bitstream
222     //! \param  [out] cmdBuffer
223     //!         Cmd buffer to add hardware stitch cmds
224     //! \return MOS_STATUS
225     //!         MOS_STATUS_SUCCESS if success, else fail reason
226     //!
227     MOS_STATUS PerformHwStitch(PMOS_COMMAND_BUFFER cmdBuffer);
228 
229     //!
230     //! \brief  Configure stitich data buffer
231     //!
232     //! \return MOS_STATUS
233     //!         MOS_STATUS_SUCCESS if success, else fail reason
234     //!
235     MOS_STATUS ConfigStitchDataBuffer() const;
236 
237     //!
238     //! \brief  Free resources
239     //!
240     //! \return MOS_STATUS
241     //!         MOS_STATUS_SUCCESS if success, else fail reason
242     //!
243     MOS_STATUS FreeResources();
244 
245     MOS_STATUS AddCondBBEndFor2ndPass(MOS_COMMAND_BUFFER &cmdBuffer);
246 
247 #if USE_CODECHAL_DEBUG_TOOL
248     virtual MOS_STATUS DumpInput() override;
249     virtual MOS_STATUS DumpOutput() override;
250 #endif
251     static constexpr uint32_t m_vdboxHucPakIntKernelDescriptor = 15;  //!< Huc pak integrate kernel descriptor
252 
253     EncodeMemComp *m_mmcState = nullptr;
254 
255     uint32_t      m_vdencHucPakDmemBufferSize = sizeof(HucPakIntegrateDmem);    //!< Indicate the size of Dmem buffer of Huc pak integrate kernel
256     PMOS_RESOURCE m_resHucPakStitchDmemBuffer[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM][CODECHAL_VDENC_BRC_NUM_OF_PASSES] = {};  //!< HuC Pak Integration Dmem data for each pass
257 
258     MOS_RESOURCE     m_resHucStitchDataBuffer[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM][CODECHAL_VDENC_BRC_NUM_OF_PASSES] = {};
259     MHW_BATCH_BUFFER m_HucStitchCmdBatchBuffer = {};
260 
261     static constexpr const uint32_t m_hwStitchCmdSize = 20 * sizeof(uint32_t);  //!< Cmd size for hw stitch
262     bool m_vdencHucUsed = false;                 //!< Indicate if it is needed to use Huc pak integrate kernel
263     Av1BasicFeature *m_basicFeature = nullptr;  //!< Hevc Basic Feature used in each frame
264     MHW_VDBOX_NODE_IND              m_vdboxIndex      = MHW_VDBOX_NODE_1;      //!< Index of VDBOX
265 
266     std::shared_ptr<mhw::vdbox::hcp::Itf> m_hcpItf = nullptr;
267 
268 MEDIA_CLASS_DEFINE_END(encode__Av1PakIntegratePkt)
269 };
270 
271 }  // namespace encode
272 #endif
273