1 /*
2 * Copyright (c) 2022, 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     decode_vp9_picture_packet.h
24 //! \brief    Defines the implementation of vp9 decode picture packet
25 //!
26 
27 #ifndef __DECODE_VP9_PICTURE_PACKET_H__
28 #define __DECODE_VP9_PICTURE_PACKET_H__
29 
30 #include "media_cmd_packet.h"
31 #include "decode_vp9_pipeline.h"
32 #include "decode_utils.h"
33 #include "decode_vp9_basic_feature.h"
34 #include "decode_downsampling_packet.h"
35 #include "mhw_vdbox_hcp_itf.h"
36 
37 using namespace mhw::vdbox::hcp;
38 
39 namespace decode
40 {
41     class Vp9DecodePicPkt : public DecodeSubPacket, public mhw::vdbox::hcp::Itf::ParSetting
42     {
43     public:
44         //!
45         //! \brief  vp9DecodePicPkt constructor
46         //!
Vp9DecodePicPkt(Vp9Pipeline * pipeline,CodechalHwInterfaceNext * hwInterface)47         Vp9DecodePicPkt(Vp9Pipeline *pipeline, CodechalHwInterfaceNext *hwInterface)
48             : DecodeSubPacket(pipeline, hwInterface), m_vp9Pipeline(pipeline)
49         {
50             if (m_hwInterface != nullptr)
51             {
52                 m_hcpItf = std::static_pointer_cast<mhw::vdbox::hcp::Itf>(m_hwInterface->GetHcpInterfaceNext());
53                 m_miItf  = std::static_pointer_cast<mhw::mi::Itf>(m_hwInterface->GetMiInterfaceNext());
54             }
55         }
56 
57         //!
58         //! \brief  Vp9DecodePicPkt deconstructor
59         //!
60         virtual ~Vp9DecodePicPkt();
61 
62         //!
63         //! \brief  Initialize the media packet, allocate required resources
64         //! \return MOS_STATUS
65         //!         MOS_STATUS_SUCCESS if success, else fail reason
66         //!
67         virtual MOS_STATUS Init() override;
68 
69         //!
70         //! \brief  Prepare interal parameters, should be invoked for each frame
71         //! \return MOS_STATUS
72         //!         MOS_STATUS_SUCCESS if success, else fail reason
73         //!
74         virtual MOS_STATUS Prepare() override;
75 
76         //! \brief   Set current phase for packet
77         //!
78         //! \return   MOS_STATUS
79         //!           MOS_STATUS_SUCCESS if success, else fail reason
80         //!
81         MOS_STATUS SetPhase(DecodePhase *phase);
82 
83         //! \brief   Store cabac stream out size to memory
84         //!
85         //! \return   MOS_STATUS
86         //!           MOS_STATUS_SUCCESS if success, else fail reason
87         //!
88         MOS_STATUS ReportCabacStreamOutSize(MOS_COMMAND_BUFFER &cmdBuffer);
89 
90         //!
91         //! \brief  Execute vp9 picture packet
92         //! \return MOS_STATUS
93         //!         MOS_STATUS_SUCCESS if success, else fail reason
94         //!
95         virtual MOS_STATUS Execute(MOS_COMMAND_BUFFER& cmdBuffer) = 0;
96 
97         //!
98         //! \brief  Calculate Command Size
99         //!
100         //! \param  [in, out] commandBufferSize
101         //!         requested size
102         //! \param  [in, out] requestedPatchListSize
103         //!         requested size
104         //! \return MOS_STATUS
105         //!         status
106         //!
107         MOS_STATUS CalculateCommandSize(
108             uint32_t &commandBufferSize,
109             uint32_t &requestedPatchListSize) override;
110 
111 #ifdef _MMC_SUPPORTED
112         MOS_STATUS SetRefMmcStatus(uint8_t surfaceID, PMOS_SURFACE pSurface);
113 #endif
114 
115         enum CodechalDecodeRefAddrIndex
116         {
117             // MPEG2/VC1 reference address indexes
118             CodechalDecodeFwdRefTop    = 0,  //!< forward reference top field
119             CodechalDecodeBwdRefTop    = 1,  //!< backward reference top field
120             CodechalDecodeFwdRefBottom = 2,  //!< forward reference bottom field
121             CodechalDecodeBwdRefBottom = 3,  //!< backward reference bottom field
122             // VP8/VP9 reference address indexes
123             CodechalDecodeLastRef      = 0,  //!< last reference
124             CodechalDecodeGoldenRef    = 1,  //!< golden reference
125             CodechalDecodeAlternateRef = 2   //!< alternate reference
126         };
127 
128     protected:
129 
130         virtual MOS_STATUS AllocateFixedResources();
131         virtual MOS_STATUS AllocateVariableResources();
132 
133         MHW_SETPAR_DECL_HDR(HCP_SURFACE_STATE);
134         MOS_STATUS AddAllCmds_HCP_SURFACE_STATE(MOS_COMMAND_BUFFER &cmdBuffer);
135         MOS_STATUS AddAllCmds_HCP_VP9_SEGMENT_STATE(MOS_COMMAND_BUFFER &cmdBuffer);
136         MOS_STATUS FixHcpPipeBufAddrParams() const;
137         MHW_SETPAR_DECL_HDR(HCP_PIPE_BUF_ADDR_STATE);
138         MHW_SETPAR_DECL_HDR(HCP_IND_OBJ_BASE_ADDR_STATE);
139         MHW_SETPAR_DECL_HDR(HCP_PIPE_MODE_SELECT);
140         MHW_SETPAR_DECL_HDR(HCP_VP9_PIC_STATE);
141         MHW_SETPAR_DECL_HDR(HCP_BSD_OBJECT);
142 
143         //! \brief    Set Rowstore Cache offset
144         //!
145         //! \return   MOS_STATUS
146         //!           MOS_STATUS_SUCCESS if success, else fail reason
147         //!
148         virtual MOS_STATUS SetRowstoreCachingOffsets();
149 
150         virtual bool IsFrontEndPhase();
151         virtual bool IsBackEndPhase();
152 
153         //!
154         //! \brief  Free resources
155         //! \return MOS_STATUS
156         //!         MOS_STATUS_SUCCESS if success, else fail reason
157         //!
158         MOS_STATUS FreeResources();
159 
160         //!
161         //! \brief  Dump resources
162         //! \return MOS_STATUS
163         //!         MOS_STATUS_SUCCESS if success, else fail reason
164         //!
165         MOS_STATUS DumpResources(HCP_PIPE_BUF_ADDR_STATE_PAR &params, uint32_t refSize, uint32_t mvBufferSize) const;
166 
167         DecodePhase *m_phase = nullptr;
168 
169         // Interfaces
170         Vp9Pipeline                *m_vp9Pipeline      = nullptr;
171         std::shared_ptr<mhw::vdbox::hcp::Itf> m_hcpItf = nullptr;
172         Vp9BasicFeature            *m_vp9BasicFeature  = nullptr;
173         DecodeAllocator            *m_allocator        = nullptr;
174         DecodeMemComp *             m_mmcState         = nullptr;
175 
176 #ifdef _DECODE_PROCESSING_SUPPORTED
177         DecodeDownSamplingFeature *m_downSamplingFeature = nullptr;
178         DecodeDownSamplingPkt     *m_downSamplingPkt     = nullptr;
179 #endif
180 
181         CODEC_VP9_PIC_PARAMS *m_vp9PicParams = nullptr;  //!< Pointer to picture parameter
182 
183         //streamout buffer
184         PMOS_BUFFER m_decodedFrameStatusErrorBuffer   = nullptr;  //!< DW176..177, Decoded Frame Status/Error Buffer Base Address
185         PMOS_BUFFER m_decodedBlockDataStreamoutBuffer = nullptr;  //!< DW179..180, Decoded Block Data Streamout Buffer Address
186 
187         PMOS_BUFFER m_resMfdDeblockingFilterRowStoreScratchBuffer    = nullptr;  //!< Handle of MFD Deblocking Filter Row Store Scratch data surface
188         PMOS_BUFFER m_resDeblockingFilterTileRowStoreScratchBuffer   = nullptr;  //!< Handle of Deblocking Filter Tile Row Store Scratch data surface
189         PMOS_BUFFER m_resDeblockingFilterColumnRowStoreScratchBuffer = nullptr;  //!< Handle of Deblocking Filter Column Row Store Scratch data surface
190         PMOS_BUFFER m_resMetadataLineBuffer                          = nullptr;  //!< Handle of Metadata Line data buffer
191         PMOS_BUFFER m_resMetadataTileLineBuffer                      = nullptr;  //!< Handle of Metadata Tile Line data buffer
192         PMOS_BUFFER m_resMetadataTileColumnBuffer                    = nullptr;  //!< Handle of Metadata Tile Column data buffer
193         PMOS_BUFFER m_resSaoLineBuffer                               = nullptr;  //!< Handle of SAO Line data buffer
194         PMOS_BUFFER m_resSaoTileLineBuffer                           = nullptr;  //!< Handle of SAO Tile Line data buffer
195         PMOS_BUFFER m_resSaoTileColumnBuffer                         = nullptr;  //!< Handle of SAO Tile Column data buffer
196 
197         uint32_t m_pictureStatesSize    = 0;    //!< Picture states size
198         uint32_t m_picturePatchListSize = 0;    //!< Picture patch list size
199         uint16_t chromaSamplingFormat   = 0;    //!< Chroma sampling fromat
200         uint32_t m_widthInSb            = 0;    //!< Width in unit of SB
201         uint32_t m_heightInSb           = 0;    //!< Height in unit of SB
202 
203         PMOS_BUFFER m_resDmemBuffer = nullptr;
204         PMOS_BUFFER m_resDeblockingFilterLineRowStoreScratchBuffer = nullptr;
205         PMOS_BUFFER m_resHvcLineRowstoreBuffer                     = nullptr;
206         PMOS_BUFFER m_resHvcTileRowstoreBuffer                     = nullptr;
207         PMOS_BUFFER m_resIntraPredUpRightColStoreBuffer            = nullptr;  //!< Handle of intra prediction up right column store buffer
208         PMOS_BUFFER m_resIntraPredLeftReconColStoreBuffer          = nullptr;  //!< Handle of intra prediction left recon column store buffer
209         PMOS_BUFFER m_resCABACSyntaxStreamOutBuffer                = nullptr;  //!< Handle of CABAC syntax stream out buffer
210         PMOS_BUFFER m_resCABACStreamOutSizeBuffer                  = nullptr;  //!< Handle of CABAC stream out size buffer
211 
212         mutable uint8_t m_curHcpSurfStateId            = 0;
213         PMOS_SURFACE    psSurface                      = nullptr;; // 2D surface parameters
214         static const uint32_t m_vp9ScalingFactor       = (1 << 14);
215         static const uint32_t m_rawUVPlaneAlignment    = 4;  //! starting Gen9 the alignment is relaxed to 4x instead of 16x
216         static const uint32_t m_reconUVPlaneAlignment  = 8;
217         static const uint32_t m_uvPlaneAlignmentLegacy = 8;  //! starting Gen9 the alignment is relaxed to 4x instead of 16x
218 
219 #ifdef _MMC_SUPPORTED
220         uint8_t m_refsMmcEnable = 0;
221         uint8_t m_refsMmcType   = 0;
222         uint32_t m_mmcFormat    = 0;
223 #endif
224 
225         typedef union
226         {
227             struct
228             {
229                 uint8_t KeyFrame : 1;       // [0..1]
230                 uint8_t IntraOnly : 1;      // [0..1]
231                 uint8_t Display : 1;        // [0..1]
232                 uint8_t ReservedField : 5;  // [0]
233             } fields;
234             uint8_t value;
235         } PrevFrameParams;
236 
237         MEDIA_CLASS_DEFINE_END(decode__Vp9DecodePicPkt)
238     };
239 
240 }  // namespace decode
241 #endif
242