1 // Copyright (c) 2017-2018 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 #if defined (MFX_ENABLE_H264_VIDEO_DECODE)
23 
24 #ifndef __UMC_H264_HEADERS_H
25 #define __UMC_H264_HEADERS_H
26 
27 #include "umc_h264_dec_defs_dec.h"
28 #include "umc_h264_heap.h"
29 #include "umc_h264_slice_decoding.h"
30 
31 namespace UMC
32 {
33 
34 template <typename T>
35 class HeaderSet
36 {
37 public:
38 
HeaderSet(H264_Heap_Objects * pObjHeap)39     HeaderSet(H264_Heap_Objects  *pObjHeap)
40         : m_pObjHeap(pObjHeap)
41         , m_currentID(-1)
42     {
43     }
44 
~HeaderSet()45     virtual ~HeaderSet()
46     {
47         Reset(false);
48     }
49 
AddHeader(T * hdr)50     T * AddHeader(T* hdr)
51     {
52         uint32_t id = hdr->GetID();
53 
54         if (id >= m_Header.size())
55         {
56             m_Header.resize(id + 1);
57         }
58 
59         m_currentID = id;
60 
61         if (m_Header[id])
62         {
63             m_Header[id]->DecrementReference();
64         }
65 
66         T * header = m_pObjHeap->AllocateObject<T>();
67         *header = *hdr;
68 
69         //ref. counter may not be 0 here since it can be copied from given [hdr] object
70         header->ResetRefCounter();
71         header->IncrementReference();
72 
73         m_Header[id] = header;
74 
75         return header;
76     }
77 
GetHeader(int32_t id)78     T * GetHeader(int32_t id)
79     {
80         if ((uint32_t)id >= m_Header.size())
81         {
82             return 0;
83         }
84 
85         return m_Header[id];
86     }
87 
GetHeader(int32_t id)88     const T * GetHeader(int32_t id) const
89     {
90         if ((uint32_t)id >= m_Header.size())
91         {
92             return 0;
93         }
94 
95         return m_Header[id];
96     }
97 
RemoveHeader(void * hdr)98     void RemoveHeader(void * hdr)
99     {
100         T * tmp = (T *)hdr;
101         if (!tmp)
102         {
103             VM_ASSERT(false);
104             return;
105         }
106 
107         uint32_t id = tmp->GetID();
108 
109         if (id >= m_Header.size())
110         {
111             VM_ASSERT(false);
112             return;
113         }
114 
115         if (!m_Header[id])
116         {
117             VM_ASSERT(false);
118             return;
119         }
120 
121         VM_ASSERT(m_Header[id] == hdr);
122         m_Header[id]->DecrementReference();
123         m_Header[id] = 0;
124     }
125 
126     void Reset(bool isPartialReset = false)
127     {
128         if (!isPartialReset)
129         {
130             for (uint32_t i = 0; i < m_Header.size(); i++)
131             {
132                 if (m_Header[i])
133                     m_Header[i]->DecrementReference();
134             }
135 
136             m_Header.clear();
137             m_currentID = -1;
138         }
139     }
140 
SetCurrentID(int32_t id)141     void SetCurrentID(int32_t id)
142     {
143         if (GetHeader(id))
144             m_currentID = id;
145     }
146 
GetCurrentID()147     int32_t GetCurrentID() const
148     {
149         return m_currentID;
150     }
151 
GetCurrentHeader()152     T * GetCurrentHeader()
153     {
154         if (m_currentID == -1)
155             return 0;
156 
157         return GetHeader(m_currentID);
158     }
159 
GetCurrentHeader()160     const T * GetCurrentHeader() const
161     {
162         if (m_currentID == -1)
163             return 0;
164 
165         return GetHeader(m_currentID);
166     }
167 
168 private:
169     std::vector<T*>           m_Header;
170     H264_Heap_Objects        *m_pObjHeap;
171 
172     int32_t                    m_currentID;
173 };
174 
175 /****************************************************************************************************/
176 // Headers stuff
177 /****************************************************************************************************/
178 class Headers
179 {
180 public:
181 
Headers(H264_Heap_Objects * pObjHeap)182     Headers(H264_Heap_Objects  *pObjHeap)
183         : m_SeqParams(pObjHeap)
184         , m_SeqExParams(pObjHeap)
185         , m_SeqParamsMvcExt(pObjHeap)
186         , m_SeqParamsSvcExt(pObjHeap)
187         , m_PicParams(pObjHeap)
188         , m_SEIParams(pObjHeap)
189     {
190         memset(&m_nalExtension, 0, sizeof(m_nalExtension));
191     }
192 
193     void Reset(bool isPartialReset = false)
194     {
195         m_SeqParams.Reset(isPartialReset);
196         m_SeqExParams.Reset(isPartialReset);
197         m_SeqParamsMvcExt.Reset(isPartialReset);
198         m_SeqParamsSvcExt.Reset(isPartialReset);
199         m_PicParams.Reset(isPartialReset);
200         m_SEIParams.Reset(isPartialReset);
201     }
202 
203     HeaderSet<UMC_H264_DECODER::H264SeqParamSet>             m_SeqParams;
204     HeaderSet<UMC_H264_DECODER::H264SeqParamSetExtension>    m_SeqExParams;
205     HeaderSet<UMC_H264_DECODER::H264SeqParamSetMVCExtension> m_SeqParamsMvcExt;
206     HeaderSet<UMC_H264_DECODER::H264SeqParamSetSVCExtension> m_SeqParamsSvcExt;
207     HeaderSet<UMC_H264_DECODER::H264PicParamSet>             m_PicParams;
208     HeaderSet<UMC_H264_DECODER::H264SEIPayLoad>              m_SEIParams;
209     UMC_H264_DECODER::H264NalExtension                       m_nalExtension;
210 };
211 
212 } // namespace UMC
213 
214 #endif // __UMC_H264_HEADERS_H
215 #endif // MFX_ENABLE_H264_VIDEO_DECODE
216