File: umc_mpeg2_decoder.h

package info (click to toggle)
intel-mediasdk 22.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 86,508 kB
  • sloc: cpp: 1,055,709; ansic: 25,847; asm: 17,754; python: 8,951; cs: 965; sh: 543; makefile: 528; lisp: 52
file content (301 lines) | stat: -rw-r--r-- 9,674 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright (c) 2018-2020 Intel Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include "umc_defs.h"

#if defined (MFX_ENABLE_MPEG2_VIDEO_DECODE)

#include <mutex>
#include <memory>
#include <vector>
#include <deque>

#include "umc_mpeg2_defs.h"
#include "umc_mpeg2_frame.h"
#include "umc_video_decoder.h"
#include "umc_mpeg2_splitter.h"

namespace UMC
{ class FrameAllocator; }

namespace UMC_MPEG2_DECODER
{
    class Payload_Storage;
    class MPEG2HeadersBitstream;

/****************************************************************************************************/
// Skipping_MPEG2 class routine
/****************************************************************************************************/
    class Skipping_MPEG2
    {
    public:
        virtual ~Skipping_MPEG2()
        {}

        // Check if frame should be skipped to decrease decoding delays
        bool IsShouldSkipFrame(const MPEG2DecoderFrame&);
        void ChangeVideoDecodingSpeed(int32_t& num);
        void Reset();

         // Get current skip mode state
        uint32_t GetNumSkipFrames() const
        { return m_NumberOfSkippedFrames; }

    private:

        enum SkipLevel
        {
            SKIP_NONE = 0,
            SKIP_B,
            SKIP_PB,
            SKIP_ALL
        };

        int32_t m_SkipLevel = SKIP_NONE;
        uint32_t m_NumberOfSkippedFrames = 0;
    };

    class MPEG2DecoderParams
        : public UMC::VideoDecoderParams
    {
        DYNAMIC_CAST_DECL(MPEG2DecoderParams, UMC::VideoDecoderParams)

    public:

        UMC::FrameAllocator* allocator   = nullptr;
        uint32_t             async_depth = 0;
    };

    class MPEG2Decoder
        : public UMC::VideoDecoder, public Skipping_MPEG2
    {

    public:

        MPEG2Decoder();
        ~MPEG2Decoder() override;

    public:

        static UMC::Status DecodeHeader(mfxBitstream&, mfxVideoParam&);

        /* UMC::BaseCodec interface */
        UMC::Status Init(UMC::BaseCodecParams*) override;

        // Close all codec resources
        UMC::Status Close(void) override;

        UMC::Status GetFrame(UMC::MediaData*, UMC::MediaData*) override
        { return UMC::UMC_ERR_NOT_IMPLEMENTED; }

        UMC::Status Reset() override;

        UMC::Status GetInfo(UMC::BaseCodecParams*) override;

    public:

        /* UMC::VideoDecoder interface */
        UMC::Status ResetSkipCount() override
        { return UMC::UMC_ERR_NOT_IMPLEMENTED; }
        UMC::Status SkipVideoFrame(int32_t) override
        { return UMC::UMC_ERR_NOT_IMPLEMENTED; }
        uint32_t GetNumOfSkippedFrames() override
        { return 0; }

    public:

        // Find NAL units in new bitstream buffer and process them
        virtual UMC::Status AddOneFrame(UMC::MediaData * source);

        // Add a new bitstream data buffer to decoding
        virtual UMC::Status AddSource(UMC::MediaData * source);

        // Decode slice header start, set slice links to seq and picture
        virtual MPEG2Slice * DecodeSliceHeader(const RawUnit & unit);

        // Add a new slice to picture
        virtual UMC::Status AddSlice(MPEG2Slice*);

        // Decode a bitstream header
        virtual UMC::Status DecodeHeaders(const RawUnit &);

        // Decode sequence header (and extension)
        virtual UMC::Status DecodeSeqHeader(const RawUnit &);

        // Decode picture header (and extension)
        virtual UMC::Status DecodePicHeader(const RawUnit &);

        // Decode group of pictures header
        virtual UMC::Status DecodeGroupHeader(const RawUnit &);

        // Decode display and matrix extensions
        virtual UMC::Status DecodeExtension(const RawUnit &);

        virtual MPEG2DecoderFrame* FindFrameByMemID(UMC::FrameMemID);

        // Find a next frame ready to be output from decoder
        virtual MPEG2DecoderFrame* GetFrameToDisplay();

        // Check for frame completeness and get decoding errors
        virtual bool QueryFrames(MPEG2DecoderFrame&) = 0;

        // Initialize mfxVideoParam structure based on decoded bitstream header values
        virtual UMC::Status FillVideoParam(mfxVideoParam *par, bool full);

        // Return raw sequence and sequence extension headers
        virtual RawHeader_MPEG2 * GetSeqAndSeqExtHdr()
        { return &m_rawHeaders; }

        // Return a number of cached frames
        virtual uint8_t GetNumCachedFrames() const;

        // Set frame display time
        virtual void PostProcessDisplayFrame(MPEG2DecoderFrame*);

        // Set MFX video params
        virtual void SetVideoParams(const mfxVideoParam&);

        Payload_Storage * GetPayloadStorage() const { return m_messages.get();}

        MPEG2DecoderParams* GetMpeg2DecoderParams() {return &m_params;}

    protected:

        virtual void SetDPBSize(uint32_t);
        virtual MPEG2DecoderFrame* GetFreeFrame();
        virtual MPEG2DecoderFrame* GetFrameBuffer(MPEG2Slice*);

        // Action on new slice
        virtual UMC::Status OnNewSlice(const RawUnit & data);

        // Action on new picture
        virtual bool OnNewPicture(); // returns true on full frame
        virtual void EliminateSliceErrors(MPEG2DecoderFrame& frame, uint8_t fieldIndex);
        virtual UMC::Status CompletePicture(MPEG2DecoderFrame& frame, uint8_t fieldIndex);
        bool IsFieldOfCurrentFrame() const;

        // DPB update
        virtual void UpdateDPB(MPEG2DecoderFrame&, uint8_t);

        // Allocate frame internals
        virtual void AllocateFrameData(UMC::VideoDataInfo const&, UMC::FrameMemID, MPEG2DecoderFrame&) = 0;
        virtual void CompleteDecodedFrames();
        // Pass picture to driver
        virtual UMC::Status Submit(MPEG2DecoderFrame&, uint8_t) = 0;

    private:

        // Find a reusable frame initialize it with slice parameters
        MPEG2DecoderFrame* StartFrame(MPEG2Slice * slice);

    protected:

        std::mutex                      m_guard;
        UMC::FrameAllocator*            m_allocator;
        uint32_t                        m_counter;
        MPEG2DecoderParams              m_params;
        mfxVideoParam                   m_firstMfxVideoParams;

        DPBType                         m_dpb;     // storage of decoded frames

        MPEG2DecoderFrame*              m_currFrame;

        struct Headers
        {
            std::shared_ptr<MPEG2SequenceHeader>           seqHdr;
            std::shared_ptr<MPEG2SequenceExtension>        seqExtHdr;
            std::shared_ptr<MPEG2SequenceDisplayExtension> displayExtHdr;
            std::shared_ptr<MPEG2QuantMatrix>              qmatrix;
            std::shared_ptr<MPEG2GroupOfPictures>          group;
            std::shared_ptr<MPEG2PictureHeader>            picHdr;
            std::shared_ptr<MPEG2PictureCodingExtension>   picExtHdr;
        };

        Headers                         m_currHeaders; // current active stream headers
        RawHeader_MPEG2                 m_rawHeaders;  // raw binary headers

        uint32_t                        m_dpbSize;

        Splitter                        m_splitter;

        double                          m_localDeltaFrameTime;
        bool                            m_useExternalFramerate;
        double                          m_localFrameTime;

        std::unique_ptr<Payload_Storage> m_messages;

        std::unique_ptr<MPEG2Slice>     m_lastSlice; // slice which could't be processed
    };

    class Payload_Storage
    {
    public:

        struct Message
        {
            size_t             msg_size  = 0;
            size_t             offset    = 0;
            uint8_t            * data    = nullptr;
            double             timestamp = 0.0;
            int32_t             isUsed   = 0;
            int32_t             auID     = 0;
            int32_t             inputID  = 0;
            MPEG2DecoderFrame * frame    = nullptr;
        };

        Payload_Storage();

        ~Payload_Storage();

        void Init();

        void Close();

        void Reset();

        void SetTimestamp(MPEG2DecoderFrame * frame);

        Message* AddMessage(const RawUnit & data, int32_t auIndex);

        const Message * GetPayloadMessage();

        void SetFrame(MPEG2DecoderFrame * frame, int32_t auIndex);
        void SetAUID(int32_t auIndex);

    private:

        enum
        {
            MAX_BUFFERED_SIZE = 16 * 1024, // 16 kb
            START_ELEMENTS = 10,
            MAX_ELEMENTS = 128
        };

        std::vector<uint8_t>  m_data;
        std::vector<Message>  m_payloads;

        size_t m_offset;
        int32_t m_lastUsed;
    };
}

#endif // MFX_ENABLE_MPEG2_VIDEO_DECODE