1 /*
2 * Copyright (c) 2020-2021, 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_vp9_pak_integrate_packet.h
24 //! \brief    Defines the implementation of vp9 pak integrate packet
25 //!
26 
27 #ifndef __CODECHAL_VP9_PAK_INTEGRATE_PACKET_H__
28 #define __CODECHAL_VP9_PAK_INTEGRATE_PACKET_H__
29 
30 #include "media_cmd_packet.h"
31 #include "encode_huc.h"
32 #include "encode_vp9_basic_feature.h"
33 #include "encode_vp9_vdenc_packet.h"
34 #include "encode_vp9_brc.h"
35 #include "encode_vp9_tile.h"
36 
37 namespace encode
38 {
39 #define HUC_CMD_LIST_MODE     1
40 #define HUC_BATCH_BUFFER_END  0x05000000
41 #define PAK_OBJECT_NUM        4
42 
43 //!
44 //! \struct HucPakIntDmem
45 //! \brief  The struct of Huc Com Dmem
46 //!
47 struct HucPakIntDmem
48 {
49     uint32_t tileSizeRecordOffset[PAK_OBJECT_NUM + 1];    // Tile Size Records, start offset  in byte, 0xffffffff means unavailable
50     uint32_t vdencStatOffset[PAK_OBJECT_NUM + 1];         // Needed for HEVC VDEnc, VP9 VDEnc, start offset  in byte, 0xffffffff means unavailable
51     uint32_t hevcPakStatOffset[PAK_OBJECT_NUM + 1];       // Needed for HEVC VDEnc, start offset  in byte, 0xffffffff means unavailable
52     uint32_t hevcStreamoutOffset[PAK_OBJECT_NUM + 1];     // Needed for HEVC VDEnc, start offset  in byte, 0xffffffff means unavailable
53     uint32_t vp9PakStatOffset[PAK_OBJECT_NUM + 1];        // Needed for VP9 VDEnc, start offset  in byte, 0xffffffff means unavailable
54     uint32_t vp9CounterBufferOffset[PAK_OBJECT_NUM + 1];  // Needed for VP9 VDEnc, start offset  in byte, 0xffffffff means unavailable
55     uint32_t lastTileBSStartInBytes;                      // Last tile in bitstream for region 4 and region 5
56     uint32_t SliceHeaderSizeinBits;                       // Needed for HEVC dual pipe BRC
57     uint16_t totalSizeInCommandBuffer;                    // Total size in bytes of valid data in the command buffer
58     uint16_t offsetInCommandBuffer;                       // Byte  offset of the to-be-updated Length (uint32_t ) in the command buffer, 0xffff means unavailable
59     uint16_t picWidthInPixel;                             // Picture width in pixel
60     uint16_t picHeightInPixel;                            // Picture hieght in pixel
61     uint16_t totalNumberOfPaks;                           // [2..4] for Gen11
62     uint16_t numSlices[PAK_OBJECT_NUM];                   // This is number of slices in each PAK
63     uint16_t numTiles[PAK_OBJECT_NUM];                    // This is number of tiles from each PAK
64     uint16_t picStateStartInBytes;                        // Offset for  region 7 and region 8
65     uint8_t  codec;                                       // 1: HEVC DP; 2: HEVC VDEnc; 3: VP9 VDEnc
66     uint8_t  maxPass;                                     // Max number of BRC pass >=1
67     uint8_t  currentPass;                                 // Current BRC pass [1..MAXPass]
68     uint8_t  minCUSize;                                   // Minimum CU size (3: 8x8, 4:16x16), HEVC only.
69     uint8_t  cabacZeroWordFlag;                           // Cabac zero flag, HEVC only
70     uint8_t  bitdepthLuma;                                // Luma bitdepth, HEVC only
71     uint8_t  bitdepthChroma;                              // Chroma bitdepth, HEVC only
72     uint8_t  chromaFormatIdc;                             // Chroma format idc, HEVC only
73     uint8_t  currFrameBRClevel;                           // Hevc dual pipe only
74     uint8_t  brcUnderFlowEnable;                          // Hevc dual pipe only
75     uint8_t  StitchEnable;                                // Enable stitch cmd for Hevc dual pipe
76     uint8_t  reserved1;
77     uint16_t StitchCommandOffset;                         // Offset in region 10 which is the second level batch buffer
78     uint16_t reserved2;
79     uint32_t BBEndforStitch;
80     uint8_t  RSVD[16];
81 };
82 
83 //!
84 //! \struct HucInputCmdG12
85 //! \brief  The struct of Huc input command
86 //!
87 struct HucInputCmdG12
88 {
89     uint8_t  SelectionForIndData = 0;
90     uint8_t  CmdMode             = 0;
91     uint16_t LengthOfTable       = 0;
92 
93     uint32_t SrcBaseOffset  = 0;
94     uint32_t DestBaseOffset = 0;
95 
96     uint32_t Reserved[3] = {0};
97 
98     uint32_t CopySize = 0;
99 
100     uint32_t ReservedCounter[4] = {0};
101 
102     uint32_t SrcAddrBottom  = 0;
103     uint32_t SrcAddrTop     = 0;
104     uint32_t DestAddrBottom = 0;
105     uint32_t DestAddrTop    = 0;
106 };
107 
108 //!
109 //! \struct HucCommandData
110 //! \brief  The struct of Huc commands data
111 //!
112 struct HucCommandData
113 {
114     uint32_t TotalCommands;   //!< Total Commands in the Data buffer
115     struct
116     {
117         uint16_t ID;          //!< Command ID, defined and order must be same as that in DMEM
118         uint16_t SizeOfData;  //!< data size in uint32_t
119         uint32_t data[40];
120     } InputCOM[10];
121 };
122 
123 class Vp9PakIntegratePkt : public EncodeHucPkt
124 {
125 public:
126     //!
127     //! \brief  Vp9PakIntegratePkt constructor
128     //!
129     //! \param  [in] pipeline
130     //!         Pointer to the media pipeline
131     //! \param  [in] task
132     //!         Pointer to media task
133     //! \param  [in] hwInterface
134     //!         Pointer to HW interface
135     //!
Vp9PakIntegratePkt(MediaPipeline * pipeline,MediaTask * task,CodechalHwInterfaceNext * hwInterface)136     Vp9PakIntegratePkt(MediaPipeline *pipeline, MediaTask *task, CodechalHwInterfaceNext *hwInterface) : EncodeHucPkt(pipeline, task, hwInterface)
137     {
138         ENCODE_CHK_NULL_NO_STATUS_RETURN(m_hwInterface);
139         m_osInterface = m_hwInterface->GetOsInterface();
140         ENCODE_CHK_NULL_NO_STATUS_RETURN(m_osInterface);
141     }
142 
143     //!
144     //! \brief  Vp9PakIntegratePkt destructor
145     //!
146     virtual ~Vp9PakIntegratePkt();
147 
148     //!
149     //! \brief  Initialize the media packet, allocate required resources
150     //!
151     //! \return MOS_STATUS
152     //!         MOS_STATUS_SUCCESS if success, else fail reason
153     //!
154     virtual MOS_STATUS Init() override;
155 
156     //!
157     //! \brief  Add the command sequence into the commandBuffer and
158     //!         and return to the caller task
159     //!
160     //! \param  [in] commandBuffer
161     //!         Pointer to the command buffer which is allocated by caller
162     //! \param  [in] packetPhase
163     //!         Indicate packet phase stage
164     //!
165     //! \return MOS_STATUS
166     //!         MOS_STATUS_SUCCESS if success, else fail reason
167     //!
168     MOS_STATUS Submit(MOS_COMMAND_BUFFER *commandBuffer, uint8_t packetPhase = otherPacket) override;
169 
170     //!
171     //! \brief  Calculate Command Size
172     //!
173     //! \param  [in, out] commandBufferSize
174     //!         requested size
175     //! \param  [in, out] requestedPatchListSize
176     //!         requested size
177     //!
178     //! \return MOS_STATUS
179     //!         status
180     //!
181     virtual MOS_STATUS CalculateCommandSize(
182         uint32_t &commandBufferSize,
183         uint32_t &requestedPatchListSize) override;
184 
185     //!
186     //! \brief  Dump output resources or infomation after submit
187     //! \return MOS_STATUS
188     //!         MOS_STATUS_SUCCESS if success, else fail reason
189     //!
190     virtual MOS_STATUS DumpOutput() override;
191 
192     //!
193     //! \brief  Get Packet Name
194     //!
195     //! \return std::string
196     //!
GetPacketName()197     virtual std::string GetPacketName() override
198     {
199         return "PAKINT_Pass" + std::to_string((uint32_t)m_pipeline->GetCurrentPass());
200     }
201 
202     //!
203     //! \brief  One frame is completed
204     //!
205     //! \param  [in] mfxStatus
206     //!         pointer to status buffer which for MFX
207     //! \param  [in] rcsStatus
208     //!         pointer to status buffer which for RCS
209     //! \param  [in, out] statusReport
210     //!         pointer of EncoderStatusReport
211     //!
212     //! \return MOS_STATUS
213     //!         MOS_STATUS_SUCCESS if success, else fail reason
214     //!
215     virtual MOS_STATUS Completed(void *mfxStatus, void *rcsStatus, void *statusReport) override;
216 
217     MHW_SETPAR_DECL_HDR(HUC_IMEM_STATE);
218     MHW_SETPAR_DECL_HDR(HUC_DMEM_STATE);
219     MHW_SETPAR_DECL_HDR(HUC_VIRTUAL_ADDR_STATE);
220 
221 protected:
222     //!
223     //! \brief  Allocate resources
224     //!
225     //! \return MOS_STATUS
226     //!         MOS_STATUS_SUCCESS if success, else fail reason
227     //!
228     virtual MOS_STATUS AllocateResources() override;
229 
230     //!
231     //! \brief  Set huc dmem buffer
232     //!
233     //! \return MOS_STATUS
234     //!         MOS_STATUS_SUCCESS if success, else fail reason
235     //!
236     virtual MOS_STATUS SetDmemBuffer() const;
237 
238     //!
239     //! \brief    Retreive Hcp statistics
240     //!
241     //! \param    [in] vdboxIndex
242     //!           vdbox's index
243     //! \param    [in] statusReport
244     //!           Pointer to status report interface
245     //! \param    [in] cmdBuffer
246     //!           Pointer to command buffer
247     //!
248     //! \return   MOS_STATUS
249     //!           MOS_STATUS_SUCCESS if success, else fail reason
250     //!
251     MOS_STATUS ReadHcpStatus(
252         MHW_VDBOX_NODE_IND  vdboxIndex,
253         MediaStatusReport * statusReport,
254         MOS_COMMAND_BUFFER &cmdBuffer);
255 
256     //!
257     //! \brief  End Status Report
258     //!
259     //! \param  [in] srType
260     //!         status report type for send cmds
261     //! \param  [in, out] cmdBuffer
262     //!         cmdbuffer to send cmds
263     //!
264     //! \return MOS_STATUS
265     //!         MOS_STATUS_SUCCESS if success, else fail reason
266     //!
267     virtual MOS_STATUS EndStatusReport(
268         uint32_t            srType,
269         MOS_COMMAND_BUFFER *cmdBuffer) override;
270 
271     //!
272     //! \brief  Update parameters
273     //!
274     void UpdateParameters();
275 
276     //!
277     //! \brief  Setup status data of tiles when one frame is completed
278     //!
279     //! \param  [in] mfxStatus
280     //!         pointer to status buffer which for MFX
281     //! \param  [in, out] statusReport
282     //!         pointer of EncoderStatusReport
283     //!
284     //! \return MOS_STATUS
285     //!         MOS_STATUS_SUCCESS if success, else fail reason
286     //!
287     MOS_STATUS SetupTilesStatusData(void *mfxStatus, void *statusReport);
288 
289     //!
290     //! \brief  Free resources
291     //!
292     //! \return MOS_STATUS
293     //!         MOS_STATUS_SUCCESS if success, else fail reason
294     //!
295     MOS_STATUS FreeResources();
296 
297     //!
298     //! \brief  Configure stitich data buffer
299     //!
300     //! \return MOS_STATUS
301     //!         MOS_STATUS_SUCCESS if success, else fail reason
302     //!
303     MOS_STATUS ConfigStitchDataBuffer() const;
304 
305 #if USE_CODECHAL_DEBUG_TOOL
306     //!
307     //! \brief  Dump input resources or infomation after submit
308     //! \return MOS_STATUS
309     //!         MOS_STATUS_SUCCESS if success, else fail reason
310     //!
311     virtual MOS_STATUS DumpInput() override;
312 #endif
313 
314     static constexpr uint32_t m_vdboxHucPakIntegrationKernelDescriptor = 15;  //!< VDBox Huc PAK integration kernel descriptor
315 
316     // VDENC Pak Int related constants
317     static constexpr uint32_t m_pakIntDmemOffsetsSize = 120;
318     static constexpr uint32_t m_pakIntVp9CodecId      = 3;
319     static constexpr uint32_t m_hwStitchCmdSize       = 20 * sizeof(uint32_t);  //!< Cmd size for hw stitch
320 
321     MOS_RESOURCE m_hucPakIntDmemBuffer[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM][Vp9EncodeBrc::m_brcMaxNumPasses] = {};  //!< HUC PAK INT DMEM buffers
322     MOS_RESOURCE m_hucPakIntDummyBuffer    = {0};                    //!< HuC PAK Integrateion dummy buffer
323     uint32_t     m_hucPakIntDmemBufferSize = sizeof(HucPakIntDmem);  //!< Indicate the size of Dmem buffer of Huc pak integrate kernel
324 
325     MOS_RESOURCE     m_resHucStitchDataBuffer[CODECHAL_ENCODE_RECYCLED_BUFFER_NUM][CODECHAL_ENCODE_VP9_BRC_MAX_NUM_OF_PASSES] = {};
326     MHW_BATCH_BUFFER m_HucStitchCmdBatchBuffer = {};
327 
328     PMOS_INTERFACE     m_osInterface = nullptr;  //!< Pointer to the os interface
329     EncodeMemComp *    m_mmcState    = nullptr;  //!< Pointer to medai memory compression state
330 
331     Vp9BasicFeature *m_basicFeature = nullptr;  //!< Vp9 basic feature used in each frame
332 
333     std::shared_ptr<mhw::vdbox::hcp::Itf> m_hcpInterfaceNew = nullptr;
334 
335 MEDIA_CLASS_DEFINE_END(encode__Vp9PakIntegratePkt)
336 };
337 }  // namespace encode
338 
339 #endif
340