1 // Copyright (c) 2017-2019 Intel Corporation
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #include "umc_defs.h"
22 #ifdef MFX_ENABLE_H265_VIDEO_DECODE
23 
24 #ifndef __UMC_H265_NAL_SPL_H
25 #define __UMC_H265_NAL_SPL_H
26 
27 #include <vector>
28 #include "umc_h265_dec_defs.h"
29 #include "umc_media_data_ex.h"
30 #include "umc_h265_heap.h"
31 
32 namespace UMC_HEVC_DECODER
33 {
34 
35 // Big endian to little endian converter class
36 class SwapperBase
37 {
38 public:
~SwapperBase()39     virtual ~SwapperBase() {}
40 
41     virtual void SwapMemory(uint8_t *pDestination, size_t &nDstSize, uint8_t *pSource, size_t nSrcSize, std::vector<uint32_t> *pRemovedOffsets) = 0;
42     virtual void SwapMemory(MemoryPiece * pMemDst, MemoryPiece * pMemSrc, std::vector<uint32_t> *pRemovedOffsets) = 0;
43 };
44 
45 // NAL unit start code search class
46 class StartCodeIteratorBase
47 {
48 public:
49 
StartCodeIteratorBase()50     StartCodeIteratorBase()
51         : m_pSource(0)
52         , m_nSourceSize(0)
53         , m_pSourceBase(0)
54         , m_nSourceBaseSize(0)
55         , m_suggestedSize(10 * 1024) // Actual size is calculated in CalculateSuggestedSize
56     {
57     }
58 
~StartCodeIteratorBase()59     virtual ~StartCodeIteratorBase()
60     {
61     }
62 
Init(UMC::MediaData * pSource)63     virtual int32_t Init(UMC::MediaData * pSource)
64     {
65         m_pSourceBase = m_pSource = (uint8_t *) pSource->GetDataPointer();
66         m_nSourceBaseSize = m_nSourceSize = pSource->GetDataSize();
67         return 0;
68     }
69 
GetCurrentOffset()70     int32_t GetCurrentOffset()
71     {
72         return (int32_t)(m_pSource - m_pSourceBase);
73     }
74 
75     // Set maximum NAL unit size
SetSuggestedSize(size_t size)76     virtual void SetSuggestedSize(size_t size)
77     {
78         if (size > m_suggestedSize)
79             m_suggestedSize = size;
80     }
81 
82     // Returns first NAL unit ID in memory buffer
83     virtual int32_t CheckNalUnitType(UMC::MediaData * pSource) = 0;
84     // Set bitstream pointer to start code address
85     virtual int32_t MoveToStartCode(UMC::MediaData * pSource) = 0;
86     // Set destination bitstream pointer and size to NAL unit
87     virtual int32_t GetNALUnit(UMC::MediaData * pSource, UMC::MediaData * pDst) = 0;
88 
89     virtual void Reset() = 0;
90 
91 protected:
92     uint8_t * m_pSource;
93     size_t  m_nSourceSize;
94 
95     uint8_t * m_pSourceBase;
96     size_t  m_nSourceBaseSize;
97 
98     size_t  m_suggestedSize;
99 };
100 
101 // NAL unit splitter utility class
102 class NALUnitSplitter_H265
103 {
104 public:
105 
106     NALUnitSplitter_H265();
107 
108     virtual ~NALUnitSplitter_H265();
109 
110     // Initialize splitter with default values
111     virtual void Init();
112     // Free resources
113     virtual void Release();
114 
115     // Returns first NAL unit ID in memory buffer
116     virtual int32_t CheckNalUnitType(UMC::MediaData * pSource);
117     // Set bitstream pointer to start code address
118     virtual int32_t MoveToStartCode(UMC::MediaData * pSource);
119     // Set destination bitstream pointer and size to NAL unit
120     virtual UMC::MediaDataEx * GetNalUnits(UMC::MediaData * in);
121 
122     // Reset state
123     virtual void Reset();
124 
125     // Set maximum NAL unit size
SetSuggestedSize(size_t size)126     virtual void SetSuggestedSize(size_t size)
127     {
128         if (!m_pStartCodeIter)
129             return;
130 
131         m_pStartCodeIter->SetSuggestedSize(size);
132     }
133 
GetSwapper()134     SwapperBase * GetSwapper()
135     {
136         return m_pSwapper;
137     }
138 
139 protected:
140 
141     SwapperBase *   m_pSwapper;
142     StartCodeIteratorBase * m_pStartCodeIter;
143 
144     UMC::MediaDataEx m_MediaData;
145     UMC::MediaDataEx::_MediaDataEx m_MediaDataEx;
146 };
147 
148 } // namespace UMC_HEVC_DECODER
149 
150 #endif // __UMC_H265_NAL_SPL_H
151 #endif // MFX_ENABLE_H265_VIDEO_DECODE
152