1 /*
2 * Copyright (c) 2011-2017, 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     codechal_decode_mpeg2.h
24 //! \brief    Defines the decode interface extension for MPEG2.
25 //! \details  Defines all types, macros, and functions required by CodecHal for MPEG2 decoding. Definitions are not externally facing.
26 //!
27 
28 #ifndef __CODECHAL_DECODER_MPEG2_H__
29 #define __CODECHAL_DECODER_MPEG2_H__
30 
31 #include "codechal_decoder.h"
32 
33 //!
34 //! \def CODECHAL_DECODE_MPEG2_MAXIMUM_BATCH_BUFFERS
35 //! maximum number of batch buffer
36 //!
37 #define CODECHAL_DECODE_MPEG2_MAXIMUM_BATCH_BUFFERS     120
38 
39 //!
40 //! \def CODECHAL_DECODE_MPEG2_BATCH_BUFFERS_PER_GROUP
41 //! number of batch buffers per group
42 //!
43 #define CODECHAL_DECODE_MPEG2_BATCH_BUFFERS_PER_GROUP   3
44 
45 //!
46 //! \def CODECHAL_DECODE_MPEG2_COPIED_SURFACES
47 //! number of surfaces to save the bitstream in multiple execution call case
48 //!
49 #define CODECHAL_DECODE_MPEG2_COPIED_SURFACES           3
50 
51 //!
52 //! \def CODECHAL_DECODE_MPEG2_BYTES_PER_MB
53 //! bitstream size per macroblock
54 //!
55 #define CODECHAL_DECODE_MPEG2_BYTES_PER_MB              512
56 
57 //!
58 //! \def CODECHAL_DECODE_MPEG2_MB_MOTION_FORWARD
59 //! definition for forward motion of macroblock
60 //!
61 #define CODECHAL_DECODE_MPEG2_MB_MOTION_FORWARD         2   //!< Bit 1
62 
63 //!
64 //! \def CODECHAL_DECODE_MPEG2_MB_MOTION_BACKWARD
65 //! definition for backward motion of macroblock
66 //!
67 #define CODECHAL_DECODE_MPEG2_MB_MOTION_BACKWARD        4   //!< Bit 2
68 
69 //!
70 //! \enum CODECHAL_MPEG2_IMT_TYPE
71 //! \brief Mpeg2 image type
72 //!
73 typedef enum _CODECHAL_MPEG2_IMT_TYPE
74 {
75     CODECHAL_MPEG2_IMT_NONE = 0,         //!< triple GFXBlocks
76     CODECHAL_MPEG2_IMT_FRAME_FRAME,      //!< triple
77     CODECHAL_MPEG2_IMT_FIELD_FIELD,      //!< triple
78     CODECHAL_MPEG2_IMT_FIELD_DUAL_PRIME, //!< triple
79     CODECHAL_MPEG2_IMT_FRAME_FIELD,      //!< hex
80     CODECHAL_MPEG2_IMT_FRAME_DUAL_PRIME, //!< hex
81     CODECHAL_MPEG2_IMT_16X8              //!< hex
82 } CODECHAL_MPEG2_IMT_TYPE;
83 
84 typedef class CodechalDecodeMpeg2 *PCODECHAL_DECODE_MPEG2_STATE;
85 
86 //!
87 //! \class CodechalDecodeMpeg2
88 //! \brief This class defines the member fields, functions etc used by MPEG2 decoder.
89 //!
90 class CodechalDecodeMpeg2 : public CodechalDecode
91 {
92 public:
93 
94     //!
95     //! \brief  Constructor
96     //! \param    [in] hwInterface
97     //!           Hardware interface
98     //! \param    [in] debugInterface
99     //!           Debug interface
100     //! \param    [in] standardInfo
101     //!           The information of decode standard for this instance
102     //!
103     CodechalDecodeMpeg2(
104         CodechalHwInterface   *hwInterface,
105         CodechalDebugInterface* debugInterface,
106         PCODECHAL_STANDARD_INFO standardInfo);
107 
108     //!
109     //! \brief    Copy constructor
110     //!
111     CodechalDecodeMpeg2(const CodechalDecodeMpeg2&) = delete;
112 
113     //!
114     //! \brief    Copy assignment operator
115     //!
116     CodechalDecodeMpeg2& operator=(const CodechalDecodeMpeg2&) = delete;
117 
118     //!
119     //! \brief    Destructor
120     //!
121     ~CodechalDecodeMpeg2 ();
122 
123     //!
124     //! \brief    Allocate and initialize MPEG2 decoder standard
125     //! \param    [in] settings
126     //!           Pointer to CodechalSetting
127     //! \return   MOS_STATUS
128     //!           MOS_STATUS_SUCCESS if success, else fail reason
129     //!
130     MOS_STATUS  AllocateStandard (
131         CodechalSetting *          settings) override;
132 
133     //!
134     //! \brief  Set states for each frame to prepare for MPEG2 decode
135     //! \return MOS_STATUS
136     //!         MOS_STATUS_SUCCESS if success, else fail reason
137     //!
138     MOS_STATUS  SetFrameStates () override;
139 
140     //!
141     //! \brief    MPEG2 decoder state level function
142     //! \details  State level function for MPEG2 decoder
143     //!
144     //! \return   MOS_STATUS
145     //!           MOS_STATUS_SUCCESS if success, else fail reason
146     //!
147     MOS_STATUS  DecodeStateLevel () override;
148 
149     //!
150     //! \brief    MPEG2 decoder primitive level function
151     //! \details  Primitive level function for GEN specific MPEG2 decoder
152     //!
153     //! \return   MOS_STATUS
154     //!           MOS_STATUS_SUCCESS if success, else fail reason
155     //!
156     MOS_STATUS  DecodePrimitiveLevel () override;
157 
158     MOS_STATUS  InitMmcState() override;
159 
160     //!
161     //! \brief    Allocate resources
162     //! \details  Allocate resources in MPEG2 decode driver
163     //! \return   MOS_STATUS
164     //!           MOS_STATUS_SUCCESS if success, else fail reason
165     //!
166     MOS_STATUS          AllocateResources();
167 
168     //!
169     //! \brief    MPEG2 decoder slice level function
170     //! \details  Primitive level function in VLD mode for GEN specific MPEG2 decoder
171     //!
172     //! \return   MOS_STATUS
173     //!           MOS_STATUS_SUCCESS if success, else fail reason
174     //!
175     virtual MOS_STATUS  SliceLevel();
176 
177     //!
178     //! \brief    MPEG2 decoder macro block level function
179     //! \details  Primitive level function in IT mode for GEN specific MPEG2 decoder
180     //!
181     //! \return   MOS_STATUS
182     //!           MOS_STATUS_SUCCESS if success, else fail reason
183     //!
184     MOS_STATUS MacroblockLevel();
185 
186     //!
187     //! \brief    Detect slice error
188     //! \details  Detect slice error in MPEG2 decode driver
189     //! \return   MOS_STATUS
190     //!           MOS_STATUS_SUCCESS if success, else fail reason
191     //!
192     bool                DetectSliceError(
193         uint16_t                        slcNum,
194         uint32_t                        prevSliceMbEnd,
195         bool                            firstValidSlice);
196 
197     //!
198     //! \brief    Insert dummy slices
199     //! \details  Insert dummy slices in MPEG2 decode driver
200     //! \return   MOS_STATUS
201     //!           MOS_STATUS_SUCCESS if success, else fail reason
202     //!
203     MOS_STATUS          InsertDummySlices(
204         PMHW_BATCH_BUFFER               batchBuffer,
205         uint16_t                        startMB,
206         uint16_t                        endMB);
207 
208     //!
209     //! \brief    Pack motion vectors
210     //! \details  Pack motion vectors in MPEG2 decode driver
211     //! \return   None
212     //!
213     void                PackMotionVectors(
214         CODEC_PICTURE_FLAG              pic_flag,
215         PMHW_VDBOX_MPEG2_MB_STATE       mpeg2MbState);
216 
217     //!
218     //! \brief    Insert skipped macro blocks
219     //! \details  Insert skipped macro blocks in MPEG2 decode driver
220     //! \return   MOS_STATUS
221     //!           MOS_STATUS_SUCCESS if success, else fail reason
222     //!
223     MOS_STATUS          InsertSkippedMacroblocks(
224         PMHW_BATCH_BUFFER               batchBuffer,
225         PMHW_VDBOX_MPEG2_MB_STATE       params,
226         uint16_t                        nextMBStart,
227         uint16_t                        skippedMBs);
228 
229     //!
230     //! \brief    Initialize MPEG2 incomplete frame values
231     //! \details  Initialize MPEG2 incomplete frame values in MPEG2 decode driver
232     //! \return   MOS_STATUS
233     //!           MOS_STATUS_SUCCESS if success, else fail reason
234     //!
235     MOS_STATUS InitializeBeginFrame();
236 
237     //!
238     //! \brief    Copy bitstream to local buffer
239     //! \details  Copy bitstream to local buffer in MPEG2 decode driver
240     //!
241     //! \return   MOS_STATUS
242     //!           MOS_STATUS_SUCCESS if success, else fail reason
243     //!
244     MOS_STATUS          CopyDataSurface(
245         uint32_t                        dataSize,
246         MOS_RESOURCE                    sourceSurface,
247         PMOS_RESOURCE                   copiedSurface,
248         uint32_t                        *currOffset);
249 
250 #if USE_CODECHAL_DEBUG_TOOL
251     MOS_STATUS DumpPicParams(
252         CodecDecodeMpeg2PicParams *picParams);
253 
254     MOS_STATUS DumpSliceParams(
255         CodecDecodeMpeg2SliceParams *sliceParams,
256         uint32_t                     numSlices);
257 
258     MOS_STATUS DumpIQParams(
259         CodecMpeg2IqMatrix *matrixData);
260 
261     MOS_STATUS DumpMbParams(
262         CodecDecodeMpeg2MbParams *mbParams);
263 #endif
264     // Parameters passed by application
265     uint16_t                     m_picWidthInMb   = 0;                               //!< Picture Width in MB width count
266     uint16_t                     m_picHeightInMb  = 0;                               //!< Picture Height in MB height count
267     uint32_t                     m_dataSize       = 0;                               //!< Size of bitstream
268     uint32_t                     m_dataOffset     = 0;                               //!< Offset of bitstream
269     uint32_t                     m_numSlices      = 0;                               //!< Number of slices
270     uint32_t                     m_numMacroblocks = 0;                               //!< Number of macro blocks
271     CodecDecodeMpeg2PicParams *  m_picParams      = nullptr;                         //!< Pointer to MPEG2 picture parameter
272     CodecDecodeMpeg2SliceParams *m_sliceParams    = nullptr;                         //!< Pointer to MPEG2 slice parameter
273     CodecDecodeMpeg2MbParams *   m_mbParams       = nullptr;                         //!< Pointer to MPEG2 macro block parameter
274     CodecMpeg2IqMatrix *         m_iqMatrixBuffer = nullptr;                         //!< Pointer to MPEG2 IQ matrix parameter
275     MOS_SURFACE                  m_destSurface;                                      //!< Handle of render surface
276     PMOS_RESOURCE                m_presReferences[CODEC_MAX_NUM_REF_FRAME_NON_AVC];  //!< Pointer to Handle of Reference Frames
277     MOS_RESOURCE                 m_resDataBuffer;                                    //!< Handle of residual difference surface
278     bool                         m_deblockingEnabled = false;                        //!< Indicate Deblocking is enabled
279 
280     MOS_RESOURCE               m_resMfdDeblockingFilterRowStoreScratchBuffer;                          //!< Handle of MFD Deblocking Filter Row Store Scratch data surface
281     MOS_RESOURCE               m_resBsdMpcRowStoreScratchBuffer;                                       //!< Handle of MPR Row Store Scratch data surface
282     PCODECHAL_VLD_SLICE_RECORD m_vldSliceRecord = nullptr;                                             //!< Pointer to internal record of slices
283     PCODEC_REF_LIST            m_mpeg2RefList[CODECHAL_NUM_UNCOMPRESSED_SURFACE_MPEG2];                //!< Pointer to MPEG2 Ref List
284     MHW_BATCH_BUFFER           m_mediaObjectBatchBuffer[CODECHAL_DECODE_MPEG2_MAXIMUM_BATCH_BUFFERS];  //!< Handles of second level batch buffer
285     uint16_t                   m_bbInUse         = 0;                                                  //!< Current index of second level batch buffer in the allocated array
286     uint16_t                   m_bbAllocated     = 0;                                                  //!< Total number of second level batch buffers allocated
287     uint16_t                   m_bbInUsePerFrame = 0;                                                  //!< Current index of second level batch buffers used for a frame
288 
289     // MPEG2 WAs
290     bool         m_slicesInvalid = false;                                       //!< Indicate slices are invalid
291     MOS_RESOURCE m_resMpeg2DummyBistream;                                       //!< Handle of MPEG2 dummy bitstream buffer
292     MOS_RESOURCE m_resCopiedDataBuffer[CODECHAL_DECODE_MPEG2_COPIED_SURFACES];  //!< Handles of copied bitstream buffer
293     uint32_t     m_copiedDataBufferSize                = 0;                     //!< Size of copied bitstream buffer
294     uint32_t     m_currCopiedData                      = 0;                     //!< Index of current copied buffer
295     bool         m_copiedDataBufferInUse               = false;                 //!< Indicate copied bistream is inuse
296     bool         m_dummySliceDataPresent               = false;                 //!< Indicate dummy slice is present
297     uint32_t     m_copiedDataOffset                    = 0;                     //!< Offset of copied bitstream
298     uint32_t     m_nextCopiedDataOffset                = 0;                     //!< Offset of next copied bitstream
299     uint32_t     m_dummySliceDataOffset                = 0;                     //!< Offset of dummy slice bitstream
300     uint16_t     m_lastMbAddress                       = 0;                     //!< Address of last macro block
301     uint32_t     m_mpeg2ISliceConcealmentMode          = 0;                     //!< Mpeg2 I slice concealment mode
302     uint32_t     m_mpeg2PbSliceConcealmentMode         = 0;                     //!< Mpeg2 P/B slice concealment mode
303     uint32_t     m_mpeg2PbSlicePredBiDirMvTypeOverride = 0;                     //!< Mpeg2 P/B Slice Predicted BiDir Motion Type Override
304     uint32_t     m_mpeg2PbSlicePredMvOverride          = 0;                     //!< Mpeg2 P/B Slice Predicted Motion Vector Override
305 
306     MOS_RESOURCE m_resSyncObjectWaContextInUse;     //!< signals on the video WA context
307     MOS_RESOURCE m_resSyncObjectVideoContextInUse;  //!< signals on the video context
308 
309     CodecDecodeMpeg2MbParams m_savedMpeg2MbParam;  //!< save last MB parameters to be able to reconstruct MPEG2 IT Object Command for Skipped MBs.
310 };
311 
312 #endif  // __CODECHAL_DECODER_MPEG2_H__
313