1 /*
2 * Copyright (c) 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     decode_mpeg2_basic_feature.h
24 //! \brief    Defines the common interface for decode mpeg2 basic feature
25 //!
26 #ifndef __DECODE_MPEG2_BASIC_FEATURE_H__
27 #define __DECODE_MPEG2_BASIC_FEATURE_H__
28 
29 #include "decode_basic_feature.h"
30 #include "codec_def_decode_mpeg2.h"
31 #include "decode_mpeg2_reference_frames.h"
32 
33 namespace decode {
34 
35 class Mpeg2BasicFeature : public DecodeBasicFeature
36 {
37 public:
38     //!
39     //! \brief  Mpeg2BasicFeature constructor
40     //!
Mpeg2BasicFeature(DecodeAllocator * allocator,void * hwInterface,PMOS_INTERFACE osInterface)41     Mpeg2BasicFeature(DecodeAllocator *allocator, void *hwInterface, PMOS_INTERFACE osInterface) :
42         DecodeBasicFeature(allocator, hwInterface, osInterface)
43     {
44         if (osInterface != nullptr)
45         {
46             m_osInterface = osInterface;
47         }
48 
49         MOS_ZeroMemory(&m_savedMpeg2MbParam, sizeof(m_savedMpeg2MbParam));
50     };
51 
52     //!
53     //! \brief  Mpeg2BasicFeature deconstructor
54     //!
55     virtual ~Mpeg2BasicFeature();
56 
57     //!
58     //! \brief  Initialize mpeg2 basic feature CodechalSetting
59     //! \return MOS_STATUS
60     //!         MOS_STATUS_SUCCESS if success, else fail reason
61     //!
62     virtual MOS_STATUS Init(void *setting) override;
63 
64     //!
65     //! \brief  Update mpeg2 decodeParams
66     //! \return MOS_STATUS
67     //!         MOS_STATUS_SUCCESS if success, else fail reason
68     //!
69     virtual MOS_STATUS Update(void *params) override;
70 
71     //!
72     //! \brief  Re-Initialize MPEG2 incomplete frame values in the first execution call
73     //! \return MOS_STATUS
74     //!         MOS_STATUS_SUCCESS if success, else fail reason
75     //!
76     MOS_STATUS ResetFrameValues();
77 
78     struct VldSliceRecord
79     {
80         uint32_t   skip;
81         uint32_t   offset;
82         uint32_t   length;
83         uint32_t   sliceStartMbOffset;
84         uint32_t   prevSliceMbEnd;
85         bool       isLastSlice;
86         CodecDecodeMpeg2SliceParams recordSliceParam;
87     };
88 
89     struct ItMbRecord
90     {
91         uint16_t skippedMBs;
92         uint16_t expectedMBAddr;
93         CodecDecodeMpeg2MbParams recordMbParam;
94     };
95 
96     // One MB size (16x16) intra MB, color = RGB[4, 4, 4]
97     const uint32_t Mpeg2DummyBsBuf[12] =
98     {
99         0x01010000, 0x54e29752, 0x002022a5, //Dummy Slice 0, q_scale_type = 0, intra_vlc_format = 0
100         0x01010000, 0x4de29752, 0x8661341a, //Dummy Slice 1, q_scale_type = 0, intra_vlc_format = 1
101         0x01010000, 0x54e2a772, 0x002022a5, //Dummy Slice 2, q_scale_type = 1, intra_vlc_format = 0
102         0x01010000, 0x4de2a772, 0x8661341a  //Dummy Slice 3, q_scale_type = 1, intra_vlc_format = 1
103     };
104 
105     const uint32_t Mpeg2DummySliceLengths[4] = {0x8, 0x8, 0x8, 0x8};
106     const uint32_t Mpeg2DummySliceOffsets[4] = {0x4, 0x10, 0x1c, 0x28};
107 
108     //!
109     //! \enum     Mpeg2QmTypes
110     //! \brief    MPEG2 qm types
111     //!
112     enum Mpeg2QmTypes
113     {
114         mpeg2QmIntra = 0,
115         mpeg2QmNonIntra,
116     };
117 
118     const uint8_t m_mpeg2QuantMatrixScan[64] =
119     {
120         // Inverse Zig-Zag scan pattern
121         0, 1, 5, 6, 14, 15, 27, 28,
122         2, 4, 7, 13, 16, 26, 29, 42,
123         3, 8, 12, 17, 25, 30, 41, 43,
124         9, 11, 18, 24, 31, 40, 44, 53,
125         10, 19, 23, 32, 39, 45, 52, 54,
126         20, 22, 33, 38, 46, 51, 55, 60,
127         21, 34, 37, 47, 50, 56, 59, 61,
128         35, 36, 48, 49, 57, 58, 62, 63
129     };
130 
131     const uint16_t m_mpeg2DefaultIntraQuantizerMatrix[64] =
132     {
133         8, 16, 19, 22, 26, 27, 29, 34,
134         16, 16, 22, 24, 27, 29, 34, 37,
135         19, 22, 26, 27, 29, 34, 34, 38,
136         22, 22, 26, 27, 29, 34, 37, 40,
137         22, 26, 27, 29, 32, 35, 40, 48,
138         26, 27, 29, 32, 35, 40, 48, 58,
139         26, 27, 29, 34, 38, 46, 56, 69,
140         27, 29, 35, 38, 46, 56, 69, 83
141     };
142 
143     const uint16_t m_mpeg2DefaultNonIntraQuantizerMatrix[64] =
144     {
145         16, 16, 16, 16, 16, 16, 16, 16,
146         16, 16, 16, 16, 16, 16, 16, 16,
147         16, 16, 16, 16, 16, 16, 16, 16,
148         16, 16, 16, 16, 16, 16, 16, 16,
149         16, 16, 16, 16, 16, 16, 16, 16,
150         16, 16, 16, 16, 16, 16, 16, 16,
151         16, 16, 16, 16, 16, 16, 16, 16,
152         16, 16, 16, 16, 16, 16, 16, 16
153     };
154 
155     CodecDecodeMpeg2PicParams      *m_mpeg2PicParams          = nullptr;          //!< Pointer to MPEG2 picture parameter
156     CodecDecodeMpeg2SliceParams    *m_mpeg2SliceParams        = nullptr;          //!< Pointer to MPEG2 slice parameter
157     CodecDecodeMpeg2MbParams       *m_mpeg2MbParams           = nullptr;          //!< Pointer to MPEG2 MB parameter
158     CodecMpeg2IqMatrix             *m_mpeg2IqMatrixParams     = nullptr;          //!< Pointer to MPEG2 IQ matrix parameter
159     CodecMpeg2IqMatrix             *m_mpeg2IqMatrixBuffer     = nullptr;          //!< Pointer to MPEG2 IQ matrix parameter
160     uint32_t                        m_numMacroblocks          = 0;                //!< Number of macro blocks
161 
162     bool                            m_deblockingEnabled       = false;            //!< Indicate Deblocking is enabled
163     bool                            m_streamOutEnabled        = false;            //!< Indicates if stream out enabled
164     PMOS_RESOURCE                   m_streamOutBuffer         = nullptr;          //!< Stream out buffer from HW
165     PMOS_BUFFER                     m_resMpeg2DummyBistream   = nullptr;          //!< Handle of MPEG2 dummy bitstream buffer
166     uint32_t                        m_copiedDataBufferSize    = 0;                //!< Size of copied bitstream buffer
167     uint32_t                        m_copiedDataOffset        = 0;                //!< Offset of copied bitstream
168     uint32_t                        m_nextCopiedDataOffset    = 0;                //!< Offset of next copied bitstream
169     uint32_t                        m_dummySliceDataOffset    = 0;                //!< Offset of dummy slice bitstream
170     bool                            m_copiedDataBufferInUse   = false;            //!< Indicate copied bistream is in use
171     bool                            m_copyDummySlicePresent   = false;            //!< Indicate if dummy slice has been copied
172     bool                            m_incompletePicture       = false;            //!< Indicates if current input bitstream is incomplete
173     bool                            m_copiedDataNeeded        = false;            //!< Indicates if copied data is needed
174     bool                            m_slicesInvalid           = false;            //!< Indicate slices are invalid
175     uint32_t                        m_mpeg2ISliceConcealmentMode          = 0;    //!< Mpeg2 I slice concealment mode
176     uint32_t                        m_mpeg2PbSliceConcealmentMode         = 0;    //!< Mpeg2 P/B slice concealment mode
177     uint32_t                        m_mpeg2PbSlicePredBiDirMvTypeOverride = 0;    //!< Mpeg2 P/B Slice Predicted BiDir Motion Type Override
178     uint32_t                        m_mpeg2PbSlicePredMvOverride          = 0;    //!< Mpeg2 P/B Slice Predicted Motion Vector Overrid
179     uint16_t                        m_totalNumSlicesRecv                  = 0;    //!< Total slice number received in pervious execution call
180     uint32_t                        m_totalNumMbsRecv                     = 0;    //!< Total macroblocks number received in pervious execution call
181     uint16_t                        m_lastMbAddress                       = 0;    //!< Address of last macro block
182     uint32_t                        m_lastSliceMbEnd                      = 0;    //!< Address of last slice MB address for current execution call.
183 
184     Mpeg2ReferenceFrames            m_refFrames;                                  //!< Reference frames
185     std::vector<uint32_t>           m_refFrameIndexList;                          //!< Reference frame index list
186     std::vector<VldSliceRecord>     m_sliceRecord;                                //!< Record VLD mode slice info
187     std::vector<ItMbRecord>         m_mbRecord;                                   //!< Record IT mode macroblock info
188     uint8_t                         m_fwdRefIdx          = 0;
189     uint8_t                         m_bwdRefIdx          = 0;
190     static const uint32_t           m_mpeg2NumCopiedBufs = 3;
191 
192     BufferArray                    *m_copiedDataBufArray = nullptr;               //!< Handles of copied bitstream buffer array
193     PMOS_BUFFER                     m_copiedDataBuf      = nullptr;
194     CodecDecodeMpeg2MbParams        m_savedMpeg2MbParam;                          //!< save last MB parameters to reconstruct MPEG2 IT Object Command for Skipped MBs.
195 
196 protected:
SetRequiredBitstreamSize(uint32_t requiredSize)197     virtual MOS_STATUS SetRequiredBitstreamSize(uint32_t requiredSize) override
198     {
199         return MOS_STATUS_SUCCESS;
200     };
201 
202     MOS_STATUS SetPictureStructs();
203     MOS_STATUS SetSliceStructs();
204     MOS_STATUS SetMbStructs();
205     MOS_STATUS CheckCompletePicture(uint16_t startSliceIdx);
206     bool DetectSliceError(CodecDecodeMpeg2SliceParams &slc, uint32_t slcIdx, bool firstValidSlice);
207 
208     PMOS_INTERFACE        m_osInterface  = nullptr;
209 
210 MEDIA_CLASS_DEFINE_END(decode__Mpeg2BasicFeature)
211 };
212 
213 }//decode
214 
215 #endif // !__DECODE_MPEG2_BASIC_FEATURE_H__
216