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