1 // Copyright (c) 2017-2020 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 #include "umc_h264_dec.h"
25 #include "umc_h264_bitstream_headers.h"
26 #include "umc_h264_headers.h"
27 #include <limits.h>
28 
29 #define SCLFLAT16     0
30 #define SCLDEFAULT    1
31 #define SCLREDEFINED  2
32 
33 #define PeakNextBits(current_data, bp, nbits, data) \
34 { \
35     uint32_t x; \
36  \
37     VM_ASSERT((nbits) > 0 && (nbits) <= 32); \
38     VM_ASSERT(nbits >= 0 && nbits <= 31); \
39  \
40     int32_t offset = bp - (nbits); \
41  \
42     if (offset >= 0) \
43     { \
44         x = current_data[0] >> (offset + 1); \
45     } \
46     else \
47     { \
48         offset += 32; \
49  \
50         x = current_data[1] >> (offset); \
51         x >>= 1; \
52         x += current_data[0] << (31 - offset); \
53     } \
54  \
55     VM_ASSERT(offset >= 0 && offset <= 31); \
56  \
57     (data) = x & bits_data[nbits]; \
58 }
59 
60 using namespace UMC_H264_DECODER;
61 
62 namespace UMC
63 {
64 static const int32_t pre_norm_adjust_index4x4[16] =
65 {// 0 1 2 3
66     0,2,0,2,//0
67     2,1,2,1,//1
68     0,2,0,2,//2
69     2,1,2,1 //3
70 };
71 
72 static const int32_t pre_norm_adjust4x4[6][3] =
73 {
74     {10,16,13},
75     {11,18,14},
76     {13,20,16},
77     {14,23,18},
78     {16,25,20},
79     {18,29,23}
80 };
81 
82 static const int32_t pre_norm_adjust8x8[6][6] =
83 {
84     {20, 18, 32, 19, 25, 24},
85     {22, 19, 35, 21, 28, 26},
86     {26, 23, 42, 24, 33, 31},
87     {28, 25, 45, 26, 35, 33},
88     {32, 28, 51, 30, 40, 38},
89     {36, 32, 58, 34, 46, 43}
90 };
91 
92 static const int32_t pre_norm_adjust_index8x8[64] =
93 {// 0 1 2 3 4 5 6 7
94     0,3,4,3,0,3,4,3,//0
95     3,1,5,1,3,1,5,1,//1
96     4,5,2,5,4,5,2,5,//2
97     3,1,5,1,3,1,5,1,//3
98     0,3,4,3,0,3,4,3,//4
99     3,1,5,1,3,1,5,1,//5
100     4,5,2,5,4,5,2,5,//6
101     3,1,5,1,3,1,5,1 //7
102 };
103 
104 const
105 int32_t mp_scan4x4[2][16] =
106 {
107     {
108         0,  1,  4,  8,
109         5,  2,  3,  6,
110         9,  12, 13, 10,
111         7,  11, 14, 15
112     },
113     {
114         0,  4,  1,  8,
115         12, 5,  9,  13,
116         2,  6,  10, 14,
117         3,  7,  11, 15
118     }
119 };
120 
121 const int32_t hp_scan8x8[2][64] =
122 {
123     //8x8 zigzag scan
124     {
125         0, 1, 8,16, 9, 2, 3,10,
126         17,24,32,25,18,11, 4, 5,
127         12,19,26,33,40,48,41,34,
128         27,20,13, 6, 7,14,21,28,
129         35,42,49,56,57,50,43,36,
130         29,22,15,23,30,37,44,51,
131         58,59,52,45,38,31,39,46,
132         53,60,61,54,47,55,62,63
133     },
134 //8x8 field scan
135     {
136         0, 8,16, 1, 9,24,32,17,
137         2,25,40,48,56,33,10, 3,
138         18,41,49,57,26,11, 4,19,
139         34,42,50,58,27,12, 5,20,
140         35,43,51,59,28,13, 6,21,
141         36,44,52,60,29,14,22,37,
142         45,53,61,30, 7,15,38,46,
143         54,62,23,31,39,47,55,63
144     }
145 };
146 
147 const uint32_t bits_data[] =
148 {
149     (((uint32_t)0x01 << (0)) - 1),
150     (((uint32_t)0x01 << (1)) - 1),
151     (((uint32_t)0x01 << (2)) - 1),
152     (((uint32_t)0x01 << (3)) - 1),
153     (((uint32_t)0x01 << (4)) - 1),
154     (((uint32_t)0x01 << (5)) - 1),
155     (((uint32_t)0x01 << (6)) - 1),
156     (((uint32_t)0x01 << (7)) - 1),
157     (((uint32_t)0x01 << (8)) - 1),
158     (((uint32_t)0x01 << (9)) - 1),
159     (((uint32_t)0x01 << (10)) - 1),
160     (((uint32_t)0x01 << (11)) - 1),
161     (((uint32_t)0x01 << (12)) - 1),
162     (((uint32_t)0x01 << (13)) - 1),
163     (((uint32_t)0x01 << (14)) - 1),
164     (((uint32_t)0x01 << (15)) - 1),
165     (((uint32_t)0x01 << (16)) - 1),
166     (((uint32_t)0x01 << (17)) - 1),
167     (((uint32_t)0x01 << (18)) - 1),
168     (((uint32_t)0x01 << (19)) - 1),
169     (((uint32_t)0x01 << (20)) - 1),
170     (((uint32_t)0x01 << (21)) - 1),
171     (((uint32_t)0x01 << (22)) - 1),
172     (((uint32_t)0x01 << (23)) - 1),
173     (((uint32_t)0x01 << (24)) - 1),
174     (((uint32_t)0x01 << (25)) - 1),
175     (((uint32_t)0x01 << (26)) - 1),
176     (((uint32_t)0x01 << (27)) - 1),
177     (((uint32_t)0x01 << (28)) - 1),
178     (((uint32_t)0x01 << (29)) - 1),
179     (((uint32_t)0x01 << (30)) - 1),
180     (((uint32_t)0x01 << (31)) - 1),
181     ((uint32_t)0xFFFFFFFF),
182 };
183 
184 const uint16_t SAspectRatio[17][2] =
185 {
186     { 0,  0}, { 1,  1}, {12, 11}, {10, 11}, {16, 11}, {40, 33}, { 24, 11},
187     {20, 11}, {32, 11}, {80, 33}, {18, 11}, {15, 11}, {64, 33}, {160, 99},
188     {4,   3}, {3,   2}, {2,   1}
189 };
190 
191 const uint32_t SubWidthC[4]  = { 1, 2, 2, 1 };
192 const uint32_t SubHeightC[4] = { 1, 2, 1, 1 };
193 
194 const uint8_t default_intra_scaling_list4x4[16]=
195 {
196      6, 13, 20, 28, 13, 20, 28, 32, 20, 28, 32, 37, 28, 32, 37, 42
197 };
198 const uint8_t default_inter_scaling_list4x4[16]=
199 {
200     10, 14, 20, 24, 14, 20, 24, 27, 20, 24, 27, 30, 24, 27, 30, 34
201 };
202 
203 const uint8_t default_intra_scaling_list8x8[64]=
204 {
205      6, 10, 13, 16, 18, 23, 25, 27, 10, 11, 16, 18, 23, 25, 27, 29,
206     13, 16, 18, 23, 25, 27, 29, 31, 16, 18, 23, 25, 27, 29, 31, 33,
207     18, 23, 25, 27, 29, 31, 33, 36, 23, 25, 27, 29, 31, 33, 36, 38,
208     25, 27, 29, 31, 33, 36, 38, 40, 27, 29, 31, 33, 36, 38, 40, 42
209 };
210 const uint8_t default_inter_scaling_list8x8[64]=
211 {
212      9, 13, 15, 17, 19, 21, 22, 24, 13, 13, 17, 19, 21, 22, 24, 25,
213     15, 17, 19, 21, 22, 24, 25, 27, 17, 19, 21, 22, 24, 25, 27, 28,
214     19, 21, 22, 24, 25, 27, 28, 30, 21, 22, 24, 25, 27, 28, 30, 32,
215     22, 24, 25, 27, 28, 30, 32, 33, 24, 25, 27, 28, 30, 32, 33, 35
216 };
217 
H264BaseBitstream()218 H264BaseBitstream::H264BaseBitstream()
219 {
220     Reset(0, 0);
221 }
222 
H264BaseBitstream(uint8_t * const pb,const uint32_t maxsize)223 H264BaseBitstream::H264BaseBitstream(uint8_t * const pb, const uint32_t maxsize)
224 {
225     Reset(pb, maxsize);
226 }
227 
~H264BaseBitstream()228 H264BaseBitstream::~H264BaseBitstream()
229 {
230 }
231 
Reset(uint8_t * const pb,const uint32_t maxsize)232 void H264BaseBitstream::Reset(uint8_t * const pb, const uint32_t maxsize)
233 {
234     m_pbs       = (uint32_t*)pb;
235     m_pbsBase   = (uint32_t*)pb;
236     m_bitOffset = 31;
237     m_maxBsSize    = maxsize;
238 
239 } // void Reset(uint8_t * const pb, const uint32_t maxsize)
240 
Reset(uint8_t * const pb,int32_t offset,const uint32_t maxsize)241 void H264BaseBitstream::Reset(uint8_t * const pb, int32_t offset, const uint32_t maxsize)
242 {
243     m_pbs       = (uint32_t*)pb;
244     m_pbsBase   = (uint32_t*)pb;
245     m_bitOffset = offset;
246     m_maxBsSize = maxsize;
247 
248 } // void Reset(uint8_t * const pb, int32_t offset, const uint32_t maxsize)
249 
250 
More_RBSP_Data()251 bool H264BaseBitstream::More_RBSP_Data()
252 {
253     int32_t code, tmp;
254     uint32_t* ptr_state = m_pbs;
255     int32_t  bit_state = m_bitOffset;
256 
257     VM_ASSERT(m_bitOffset >= 0 && m_bitOffset <= 31);
258 
259     int32_t remaining_bytes = (int32_t)BytesLeft();
260 
261     if (remaining_bytes <= 0)
262         return false;
263 
264     // get top bit, it can be "rbsp stop" bit
265     h264GetBits(m_pbs, m_bitOffset, 1, code);
266 
267     // get remain bits, which is less then byte
268     tmp = (m_bitOffset + 1) % 8;
269 
270     if(tmp)
271     {
272         h264GetBits(m_pbs, m_bitOffset, tmp, code);
273         if ((code << (8 - tmp)) & 0x7f)    // most sig bit could be rbsp stop bit
274         {
275             m_pbs = ptr_state;
276             m_bitOffset = bit_state;
277             // there are more data
278             return true;
279         }
280     }
281 
282     remaining_bytes = (int32_t)BytesLeft();
283 
284     // run through remain bytes
285     while (0 < remaining_bytes)
286     {
287         h264GetBits(m_pbs, m_bitOffset, 8, code);
288 
289         if (code)
290         {
291             m_pbs = ptr_state;
292             m_bitOffset = bit_state;
293             // there are more data
294             return true;
295         }
296 
297         remaining_bytes -= 1;
298     }
299 
300     return false;
301 }
302 
GetOrg(uint32_t ** pbs,uint32_t * size)303 void H264BaseBitstream::GetOrg(uint32_t **pbs, uint32_t *size)
304 {
305     *pbs  = m_pbsBase;
306     *size = m_maxBsSize;
307 }
308 
GetState(uint32_t ** pbs,uint32_t * bitOffset)309 void H264BaseBitstream::GetState(uint32_t** pbs,uint32_t* bitOffset)
310 {
311     *pbs       = m_pbs;
312     *bitOffset = m_bitOffset;
313 }
314 
SetState(uint32_t * pbs,uint32_t bitOffset)315 void H264BaseBitstream::SetState(uint32_t* pbs,uint32_t bitOffset)
316 {
317     m_pbs = pbs;
318     m_bitOffset = bitOffset;
319 }
320 
SetDecodedBytes(size_t nBytes)321 void H264BaseBitstream::SetDecodedBytes(size_t nBytes)
322 {
323     m_pbs = m_pbsBase + (nBytes / 4);
324     m_bitOffset = 31 - ((int32_t) ((nBytes % sizeof(uint32_t)) * 8));
325 }
326 
327 
H264HeadersBitstream()328 H264HeadersBitstream::H264HeadersBitstream()
329     : H264BaseBitstream()
330 {
331 }
332 
H264HeadersBitstream(uint8_t * const pb,const uint32_t maxsize)333 H264HeadersBitstream::H264HeadersBitstream(uint8_t * const pb, const uint32_t maxsize)
334     : H264BaseBitstream(pb, maxsize)
335 {
336 }
337 
CheckLevel(uint8_t level_idc,bool ignore_level_constrain=false)338 inline bool CheckLevel(uint8_t level_idc, bool ignore_level_constrain = false)
339 {
340 #if (MFX_VERSION < 1035)
341     std::ignore = ignore_level_constrain;
342 #endif
343 
344     switch(level_idc)
345     {
346     case H264VideoDecoderParams::H264_LEVEL_1:
347     case H264VideoDecoderParams::H264_LEVEL_11:
348     case H264VideoDecoderParams::H264_LEVEL_12:
349     case H264VideoDecoderParams::H264_LEVEL_13:
350 
351     case H264VideoDecoderParams::H264_LEVEL_2:
352     case H264VideoDecoderParams::H264_LEVEL_21:
353     case H264VideoDecoderParams::H264_LEVEL_22:
354 
355     case H264VideoDecoderParams::H264_LEVEL_3:
356     case H264VideoDecoderParams::H264_LEVEL_31:
357     case H264VideoDecoderParams::H264_LEVEL_32:
358 
359     case H264VideoDecoderParams::H264_LEVEL_4:
360     case H264VideoDecoderParams::H264_LEVEL_41:
361     case H264VideoDecoderParams::H264_LEVEL_42:
362 
363     case H264VideoDecoderParams::H264_LEVEL_5:
364     case H264VideoDecoderParams::H264_LEVEL_51:
365     case H264VideoDecoderParams::H264_LEVEL_52:
366 
367     case H264VideoDecoderParams::H264_LEVEL_9:
368         return true;
369 
370 #if (MFX_VERSION >= 1035)
371     case H264VideoDecoderParams::H264_LEVEL_6:
372     case H264VideoDecoderParams::H264_LEVEL_61:
373     case H264VideoDecoderParams::H264_LEVEL_62:
374         return ignore_level_constrain;
375 #endif
376 
377     default:
378         return false;
379     }
380 }
381 
382 // ---------------------------------------------------------------------------
383 //  H264Bitstream::GetSequenceParamSet()
384 //    Read sequence parameter set data from bitstream.
385 // ---------------------------------------------------------------------------
GetSequenceParamSet(H264SeqParamSet * sps,bool ignore_level_constrain)386 Status H264HeadersBitstream::GetSequenceParamSet(H264SeqParamSet *sps, bool ignore_level_constrain)
387 {
388     // Not all members of the seq param set structure are contained in all
389     // seq param sets. So start by init all to zero.
390     Status ps = UMC_OK;
391     sps->Reset();
392 
393     // profile
394     // TBD: add rejection of unsupported profile
395     sps->profile_idc = (uint8_t)GetBits(8);
396 
397     switch (sps->profile_idc)
398     {
399     case H264VideoDecoderParams::H264_PROFILE_BASELINE:
400     case H264VideoDecoderParams::H264_PROFILE_MAIN:
401     case H264VideoDecoderParams::H264_PROFILE_SCALABLE_BASELINE:
402     case H264VideoDecoderParams::H264_PROFILE_SCALABLE_HIGH:
403     case H264VideoDecoderParams::H264_PROFILE_EXTENDED:
404     case H264VideoDecoderParams::H264_PROFILE_HIGH:
405     case H264VideoDecoderParams::H264_PROFILE_HIGH10:
406     case H264VideoDecoderParams::H264_PROFILE_MULTIVIEW_HIGH:
407     case H264VideoDecoderParams::H264_PROFILE_HIGH422:
408     case H264VideoDecoderParams::H264_PROFILE_STEREO_HIGH:
409     case H264VideoDecoderParams::H264_PROFILE_HIGH444:
410     case H264VideoDecoderParams::H264_PROFILE_ADVANCED444_INTRA:
411     case H264VideoDecoderParams::H264_PROFILE_ADVANCED444:
412     case H264VideoDecoderParams::H264_PROFILE_HIGH444_PRED:
413     case H264VideoDecoderParams::H264_PROFILE_CAVLC444_INTRA:
414         break;
415     default:
416         return UMC_ERR_INVALID_STREAM;
417     }
418 
419     sps->constraint_set0_flag = Get1Bit();
420     sps->constraint_set1_flag = Get1Bit();
421     sps->constraint_set2_flag = Get1Bit();
422     sps->constraint_set3_flag = Get1Bit();
423     sps->constraint_set4_flag = Get1Bit();
424     sps->constraint_set5_flag = Get1Bit();
425 
426     // skip 2 zero bits
427     GetBits(2);
428 
429     sps->level_idc = (uint8_t)GetBits(8);
430 
431     if (sps->level_idc == H264VideoDecoderParams::H264_LEVEL_UNKNOWN)
432         sps->level_idc = H264VideoDecoderParams::H264_LEVEL_52;
433 
434     MFX_CHECK(CheckLevel(sps->level_idc, ignore_level_constrain), UMC_ERR_INVALID_STREAM);
435 
436     if (sps->level_idc == H264VideoDecoderParams::H264_LEVEL_9 &&
437         sps->profile_idc != H264VideoDecoderParams::H264_PROFILE_BASELINE &&
438         sps->profile_idc != H264VideoDecoderParams::H264_PROFILE_MAIN &&
439         sps->profile_idc != H264VideoDecoderParams::H264_PROFILE_EXTENDED)
440     {
441         sps->level_idc = H264VideoDecoderParams::H264_LEVEL_1b;
442     }
443 
444     // id
445     int32_t sps_id = GetVLCElement(false);
446     if (sps_id > (int32_t)(MAX_NUM_SEQ_PARAM_SETS - 1))
447     {
448         return UMC_ERR_INVALID_STREAM;
449     }
450 
451     sps->seq_parameter_set_id = (uint8_t)sps_id;
452 
453     // see 7.3.2.1.1 "Sequence parameter set data syntax"
454     // chapter of H264 standard for full list of profiles with chrominance
455     if ((H264VideoDecoderParams::H264_PROFILE_SCALABLE_BASELINE == sps->profile_idc) ||
456         (H264VideoDecoderParams::H264_PROFILE_SCALABLE_HIGH == sps->profile_idc) ||
457         (H264VideoDecoderParams::H264_PROFILE_HIGH == sps->profile_idc) ||
458         (H264VideoDecoderParams::H264_PROFILE_HIGH10 == sps->profile_idc) ||
459         (H264VideoDecoderParams::H264_PROFILE_HIGH422 == sps->profile_idc) ||
460         (H264VideoDecoderParams::H264_PROFILE_MULTIVIEW_HIGH == sps->profile_idc) ||
461         (H264VideoDecoderParams::H264_PROFILE_STEREO_HIGH == sps->profile_idc) ||
462         /* what're these profiles ??? */
463         (H264VideoDecoderParams::H264_PROFILE_HIGH444 == sps->profile_idc) ||
464         (H264VideoDecoderParams::H264_PROFILE_HIGH444_PRED == sps->profile_idc) ||
465         (H264VideoDecoderParams::H264_PROFILE_CAVLC444_INTRA == sps->profile_idc))
466     {
467         uint32_t chroma_format_idc = GetVLCElement(false);
468 
469         if (chroma_format_idc > 3)
470             return UMC_ERR_INVALID_STREAM;
471 
472         sps->chroma_format_idc = (uint8_t)chroma_format_idc;
473         if (sps->chroma_format_idc==3)
474         {
475             sps->residual_colour_transform_flag = Get1Bit();
476         }
477 
478         uint32_t bit_depth_luma = GetVLCElement(false) + 8;
479         uint32_t bit_depth_chroma = GetVLCElement(false) + 8;
480 
481         if (bit_depth_luma > 16 || bit_depth_chroma > 16)
482             return UMC_ERR_INVALID_STREAM;
483 
484         sps->bit_depth_luma = (uint8_t)bit_depth_luma;
485         sps->bit_depth_chroma = (uint8_t)bit_depth_chroma;
486 
487         if (!chroma_format_idc)
488             sps->bit_depth_chroma = sps->bit_depth_luma;
489 
490         VM_ASSERT(!sps->residual_colour_transform_flag);
491         if (sps->residual_colour_transform_flag == 1)
492         {
493             return UMC_ERR_INVALID_STREAM;
494         }
495 
496         sps->qpprime_y_zero_transform_bypass_flag = Get1Bit();
497         sps->seq_scaling_matrix_present_flag = Get1Bit();
498         if(sps->seq_scaling_matrix_present_flag)
499         {
500             // 0
501             if(Get1Bit())
502             {
503                 GetScalingList4x4(&sps->ScalingLists4x4[0],(uint8_t*)default_intra_scaling_list4x4,&sps->type_of_scaling_list_used[0]);
504             }
505             else
506             {
507                 FillScalingList4x4(&sps->ScalingLists4x4[0],(uint8_t*) default_intra_scaling_list4x4);
508                 sps->type_of_scaling_list_used[0] = SCLDEFAULT;
509             }
510             // 1
511             if(Get1Bit())
512             {
513                 GetScalingList4x4(&sps->ScalingLists4x4[1],(uint8_t*) default_intra_scaling_list4x4,&sps->type_of_scaling_list_used[1]);
514             }
515             else
516             {
517                 FillScalingList4x4(&sps->ScalingLists4x4[1],(uint8_t*) sps->ScalingLists4x4[0].ScalingListCoeffs);
518                 sps->type_of_scaling_list_used[1] = SCLDEFAULT;
519             }
520             // 2
521             if(Get1Bit())
522             {
523                 GetScalingList4x4(&sps->ScalingLists4x4[2],(uint8_t*) default_intra_scaling_list4x4,&sps->type_of_scaling_list_used[2]);
524             }
525             else
526             {
527                 FillScalingList4x4(&sps->ScalingLists4x4[2],(uint8_t*) sps->ScalingLists4x4[1].ScalingListCoeffs);
528                 sps->type_of_scaling_list_used[2] = SCLDEFAULT;
529             }
530             // 3
531             if(Get1Bit())
532             {
533                 GetScalingList4x4(&sps->ScalingLists4x4[3],(uint8_t*)default_inter_scaling_list4x4,&sps->type_of_scaling_list_used[3]);
534             }
535             else
536             {
537                 FillScalingList4x4(&sps->ScalingLists4x4[3],(uint8_t*) default_inter_scaling_list4x4);
538                 sps->type_of_scaling_list_used[3] = SCLDEFAULT;
539             }
540             // 4
541             if(Get1Bit())
542             {
543                 GetScalingList4x4(&sps->ScalingLists4x4[4],(uint8_t*) default_inter_scaling_list4x4,&sps->type_of_scaling_list_used[4]);
544             }
545             else
546             {
547                 FillScalingList4x4(&sps->ScalingLists4x4[4],(uint8_t*) sps->ScalingLists4x4[3].ScalingListCoeffs);
548                 sps->type_of_scaling_list_used[4] = SCLDEFAULT;
549             }
550             // 5
551             if(Get1Bit())
552             {
553                 GetScalingList4x4(&sps->ScalingLists4x4[5],(uint8_t*) default_inter_scaling_list4x4,&sps->type_of_scaling_list_used[5]);
554             }
555             else
556             {
557                 FillScalingList4x4(&sps->ScalingLists4x4[5],(uint8_t*) sps->ScalingLists4x4[4].ScalingListCoeffs);
558                 sps->type_of_scaling_list_used[5] = SCLDEFAULT;
559             }
560 
561             // 0
562             if(Get1Bit())
563             {
564                 GetScalingList8x8(&sps->ScalingLists8x8[0],(uint8_t*)default_intra_scaling_list8x8,&sps->type_of_scaling_list_used[6]);
565             }
566             else
567             {
568                 FillScalingList8x8(&sps->ScalingLists8x8[0],(uint8_t*) default_intra_scaling_list8x8);
569                 sps->type_of_scaling_list_used[6] = SCLDEFAULT;
570             }
571             // 1
572             if(Get1Bit())
573             {
574                 GetScalingList8x8(&sps->ScalingLists8x8[1],(uint8_t*) default_inter_scaling_list8x8,&sps->type_of_scaling_list_used[7]);
575             }
576             else
577             {
578                 FillScalingList8x8(&sps->ScalingLists8x8[1],(uint8_t*) default_inter_scaling_list8x8);
579                 sps->type_of_scaling_list_used[7] = SCLDEFAULT;
580             }
581 
582         }
583         else
584         {
585             int32_t i;
586 
587             for (i = 0; i < 6; i += 1)
588             {
589                 FillFlatScalingList4x4(&sps->ScalingLists4x4[i]);
590             }
591             for (i = 0; i < 2; i += 1)
592             {
593                 FillFlatScalingList8x8(&sps->ScalingLists8x8[i]);
594             }
595         }
596     }
597     else
598     {
599         sps->chroma_format_idc = 1;
600         sps->bit_depth_luma = 8;
601         sps->bit_depth_chroma = 8;
602 
603         SetDefaultScalingLists(sps);
604     }
605 
606     // log2 max frame num (bitstream contains value - 4)
607     uint32_t log2_max_frame_num = GetVLCElement(false) + 4;
608     sps->log2_max_frame_num = (uint8_t)log2_max_frame_num;
609 
610     if (log2_max_frame_num > 16 || log2_max_frame_num < 4)
611         return UMC_ERR_INVALID_STREAM;
612 
613     // pic order cnt type (0..2)
614     uint32_t pic_order_cnt_type = GetVLCElement(false);
615     sps->pic_order_cnt_type = (uint8_t)pic_order_cnt_type;
616     if (pic_order_cnt_type > 2)
617     {
618         return UMC_ERR_INVALID_STREAM;
619     }
620 
621     if (sps->pic_order_cnt_type == 0)
622     {
623         // log2 max pic order count lsb (bitstream contains value - 4)
624         uint32_t log2_max_pic_order_cnt_lsb = GetVLCElement(false) + 4;
625         sps->log2_max_pic_order_cnt_lsb = (uint8_t)log2_max_pic_order_cnt_lsb;
626 
627         if (log2_max_pic_order_cnt_lsb > 16 || log2_max_pic_order_cnt_lsb < 4)
628             return UMC_ERR_INVALID_STREAM;
629 
630         sps->MaxPicOrderCntLsb = (1 << sps->log2_max_pic_order_cnt_lsb);
631     }
632     else if (sps->pic_order_cnt_type == 1)
633     {
634         sps->delta_pic_order_always_zero_flag = Get1Bit();
635         sps->offset_for_non_ref_pic = GetVLCElement(true);
636         sps->offset_for_top_to_bottom_field = GetVLCElement(true);
637         sps->num_ref_frames_in_pic_order_cnt_cycle = GetVLCElement(false);
638 
639         if (sps->num_ref_frames_in_pic_order_cnt_cycle > 255)
640             return UMC_ERR_INVALID_STREAM;
641 
642         // get offsets
643         for (uint32_t i = 0; i < sps->num_ref_frames_in_pic_order_cnt_cycle; i++)
644         {
645             sps->poffset_for_ref_frame[i] = GetVLCElement(true);
646         }
647     }    // pic order count type 1
648 
649     // num ref frames
650     sps->num_ref_frames = GetVLCElement(false);
651     if (sps->num_ref_frames > 16)
652         return UMC_ERR_INVALID_STREAM;
653 
654     sps->gaps_in_frame_num_value_allowed_flag = Get1Bit();
655 
656     // picture width in MBs (bitstream contains value - 1)
657     sps->frame_width_in_mbs = GetVLCElement(false) + 1;
658 
659     // picture height in MBs (bitstream contains value - 1)
660     sps->frame_height_in_mbs = GetVLCElement(false) + 1;
661 
662     if (!(sps->frame_width_in_mbs * 16 < USHRT_MAX) ||
663         !(sps->frame_height_in_mbs * 16 < USHRT_MAX))
664         return UMC_ERR_INVALID_STREAM;
665 
666     sps->frame_mbs_only_flag = Get1Bit();
667     sps->frame_height_in_mbs = (2-sps->frame_mbs_only_flag)*sps->frame_height_in_mbs;
668     if (sps->frame_mbs_only_flag == 0)
669     {
670         sps->mb_adaptive_frame_field_flag = Get1Bit();
671     }
672     sps->direct_8x8_inference_flag = Get1Bit();
673     if (sps->frame_mbs_only_flag==0)
674     {
675         sps->direct_8x8_inference_flag = 1;
676     }
677     sps->frame_cropping_flag = Get1Bit();
678 
679     if (sps->frame_cropping_flag)
680     {
681         sps->frame_cropping_rect_left_offset      = GetVLCElement(false);
682         sps->frame_cropping_rect_right_offset     = GetVLCElement(false);
683         sps->frame_cropping_rect_top_offset       = GetVLCElement(false);
684         sps->frame_cropping_rect_bottom_offset    = GetVLCElement(false);
685 
686         // check cropping parameters
687         int32_t cropX = SubWidthC[sps->chroma_format_idc] * sps->frame_cropping_rect_left_offset;
688         int32_t cropY = SubHeightC[sps->chroma_format_idc] * sps->frame_cropping_rect_top_offset * (2 - sps->frame_mbs_only_flag);
689         int32_t cropH = sps->frame_height_in_mbs * 16 -
690             SubHeightC[sps->chroma_format_idc]*(2 - sps->frame_mbs_only_flag) *
691             (sps->frame_cropping_rect_top_offset + sps->frame_cropping_rect_bottom_offset);
692 
693         int32_t cropW = sps->frame_width_in_mbs * 16 - SubWidthC[sps->chroma_format_idc] *
694             (sps->frame_cropping_rect_left_offset + sps->frame_cropping_rect_right_offset);
695 
696         if (cropX < 0 || cropY < 0 || cropW < 0 || cropH < 0)
697             return UMC_ERR_INVALID_STREAM;
698 
699         if (cropX > (int32_t)sps->frame_width_in_mbs * 16)
700             return UMC_ERR_INVALID_STREAM;
701 
702         if (cropY > (int32_t)sps->frame_height_in_mbs * 16)
703             return UMC_ERR_INVALID_STREAM;
704 
705         if (cropX + cropW > (int32_t)sps->frame_width_in_mbs * 16)
706             return UMC_ERR_INVALID_STREAM;
707 
708         if (cropY + cropH > (int32_t)sps->frame_height_in_mbs * 16)
709             return UMC_ERR_INVALID_STREAM;
710 
711     } // don't need else because we zeroid structure
712 
713     CheckBSLeft();
714 
715     sps->vui_parameters_present_flag = Get1Bit();
716     if (sps->vui_parameters_present_flag)
717     {
718         H264VUI vui;
719         Status vui_ps = GetVUIParam(sps, &vui);
720 
721         if (vui_ps == UMC_OK && IsBSLeft())
722         {
723             sps->vui = vui;
724         }
725     }
726 
727     return ps;
728 }    // GetSequenceParamSet
729 
GetVUIParam(H264SeqParamSet * sps,H264VUI * vui)730 Status H264HeadersBitstream::GetVUIParam(H264SeqParamSet *sps, H264VUI *vui)
731 {
732     Status ps = UMC_OK;
733 
734     vui->Reset();
735 
736     vui->aspect_ratio_info_present_flag = Get1Bit();
737 
738     vui->sar_width = 1; // default values
739     vui->sar_height = 1;
740 
741     if (vui->aspect_ratio_info_present_flag)
742     {
743         vui->aspect_ratio_idc = (uint8_t) GetBits(8);
744         if (vui->aspect_ratio_idc  ==  255) {
745             vui->sar_width = (uint16_t) GetBits(16);
746             vui->sar_height = (uint16_t) GetBits(16);
747         }
748         else
749         {
750             if (!vui->aspect_ratio_idc || vui->aspect_ratio_idc >= sizeof(SAspectRatio)/sizeof(SAspectRatio[0]))
751             {
752                 vui->aspect_ratio_info_present_flag = 0;
753             }
754             else
755             {
756                 vui->sar_width = SAspectRatio[vui->aspect_ratio_idc][0];
757                 vui->sar_height = SAspectRatio[vui->aspect_ratio_idc][1];
758             }
759         }
760     }
761 
762     vui->overscan_info_present_flag = Get1Bit();
763     if (vui->overscan_info_present_flag)
764         vui->overscan_appropriate_flag = Get1Bit();
765 
766     vui->video_signal_type_present_flag = Get1Bit();
767     if( vui->video_signal_type_present_flag ) {
768         vui->video_format = (uint8_t) GetBits(3);
769         vui->video_full_range_flag = Get1Bit();
770         vui->colour_description_present_flag = Get1Bit();
771         if( vui->colour_description_present_flag ) {
772             vui->colour_primaries = (uint8_t) GetBits(8);
773             vui->transfer_characteristics = (uint8_t) GetBits(8);
774             vui->matrix_coefficients = (uint8_t) GetBits(8);
775         }
776     }
777     vui->chroma_loc_info_present_flag = Get1Bit();
778     if( vui->chroma_loc_info_present_flag ) {
779         vui->chroma_sample_loc_type_top_field = (uint8_t) GetVLCElement(false);
780         vui->chroma_sample_loc_type_bottom_field = (uint8_t) GetVLCElement(false);
781     }
782     vui->timing_info_present_flag = Get1Bit();
783 
784     if (vui->timing_info_present_flag)
785     {
786         vui->num_units_in_tick = GetBits(32);
787         vui->time_scale = GetBits(32);
788         vui->fixed_frame_rate_flag = Get1Bit();
789 
790         if (!vui->num_units_in_tick || !vui->time_scale)
791             vui->timing_info_present_flag = 0;
792     }
793 
794     vui->nal_hrd_parameters_present_flag = Get1Bit();
795     if( vui->nal_hrd_parameters_present_flag )
796         ps=GetHRDParam(sps, vui);
797     vui->vcl_hrd_parameters_present_flag = Get1Bit();
798     if( vui->vcl_hrd_parameters_present_flag )
799         ps=GetHRDParam(sps, vui);
800     if( vui->nal_hrd_parameters_present_flag  ||  vui->vcl_hrd_parameters_present_flag )
801         vui->low_delay_hrd_flag = Get1Bit();
802     vui->pic_struct_present_flag  = Get1Bit();
803     vui->bitstream_restriction_flag = Get1Bit();
804     if( vui->bitstream_restriction_flag ) {
805         vui->motion_vectors_over_pic_boundaries_flag = Get1Bit();
806         vui->max_bytes_per_pic_denom = (uint8_t) GetVLCElement(false);
807         vui->max_bits_per_mb_denom = (uint8_t) GetVLCElement(false);
808         vui->log2_max_mv_length_horizontal = (uint8_t) GetVLCElement(false);
809         vui->log2_max_mv_length_vertical = (uint8_t) GetVLCElement(false);
810         vui->num_reorder_frames = (uint8_t) GetVLCElement(false);
811 
812         int32_t value = GetVLCElement(false);
813         if (value < (int32_t)sps->num_ref_frames || value < 0)
814         {
815             return UMC_ERR_INVALID_STREAM;
816         }
817 
818         vui->max_dec_frame_buffering = (uint8_t)value;
819         if (vui->max_dec_frame_buffering > 16)
820         {
821             return UMC_ERR_INVALID_STREAM;
822         }
823     }
824 
825     return ps;
826 }
827 
GetHRDParam(H264SeqParamSet *,H264VUI * vui)828 Status H264HeadersBitstream::GetHRDParam(H264SeqParamSet *, H264VUI *vui)
829 {
830     uint32_t cpb_cnt = (uint32_t) (GetVLCElement(false) + 1);
831 
832     if (cpb_cnt >= 32)
833     {
834         return UMC_ERR_INVALID_STREAM;
835     }
836 
837     vui->cpb_cnt = (uint8_t)cpb_cnt;
838 
839     vui->bit_rate_scale = (uint8_t) GetBits(4);
840     vui->cpb_size_scale = (uint8_t) GetBits(4);
841     for( int32_t idx= 0; idx < vui->cpb_cnt; idx++ ) {
842         vui->bit_rate_value[ idx ] = (uint32_t) (GetVLCElement(false)+1);
843         vui->cpb_size_value[ idx ] = (uint32_t) ((GetVLCElement(false)+1));
844         vui->cbr_flag[ idx ] = Get1Bit();
845     }
846     vui->initial_cpb_removal_delay_length = (uint8_t)(GetBits(5)+1);
847     vui->cpb_removal_delay_length = (uint8_t)(GetBits(5)+1);
848     vui->dpb_output_delay_length = (uint8_t) (GetBits(5)+1);
849     vui->time_offset_length = (uint8_t) GetBits(5);
850     return UMC_OK;
851 }
852 
853 // ---------------------------------------------------------------------------
854 //    Read sequence parameter set extension data from bitstream.
855 // ---------------------------------------------------------------------------
GetSequenceParamSetExtension(H264SeqParamSetExtension * sps_ex)856 Status H264HeadersBitstream::GetSequenceParamSetExtension(H264SeqParamSetExtension *sps_ex)
857 {
858     sps_ex->Reset();
859 
860     uint32_t seq_parameter_set_id = GetVLCElement(false);
861     sps_ex->seq_parameter_set_id = (uint8_t)seq_parameter_set_id;
862     if (seq_parameter_set_id > MAX_NUM_SEQ_PARAM_SETS-1)
863     {
864         return UMC_ERR_INVALID_STREAM;
865     }
866 
867     uint32_t aux_format_idc = GetVLCElement(false);
868     sps_ex->aux_format_idc = (uint8_t)aux_format_idc;
869     if (aux_format_idc > 3)
870     {
871         return UMC_ERR_INVALID_STREAM;
872     }
873 
874     if (sps_ex->aux_format_idc != 1 && sps_ex->aux_format_idc != 2)
875         sps_ex->aux_format_idc = 0;
876 
877     if (sps_ex->aux_format_idc)
878     {
879         uint32_t bit_depth_aux = GetVLCElement(false) + 8;
880         sps_ex->bit_depth_aux = (uint8_t)bit_depth_aux;
881         if (bit_depth_aux > 12)
882         {
883             return UMC_ERR_INVALID_STREAM;
884         }
885 
886         sps_ex->alpha_incr_flag = Get1Bit();
887         sps_ex->alpha_opaque_value = (uint8_t)GetBits(sps_ex->bit_depth_aux + 1);
888         sps_ex->alpha_transparent_value = (uint8_t)GetBits(sps_ex->bit_depth_aux + 1);
889     }
890 
891     sps_ex->additional_extension_flag = Get1Bit();
892 
893     CheckBSLeft();
894     return UMC_OK;
895 }    // GetSequenceParamSetExtension
896 
897 template <class num_t, class items_t> static
DecodeViewReferenceInfo(num_t & numItems,items_t * pItems,H264HeadersBitstream & bitStream)898 Status DecodeViewReferenceInfo(num_t &numItems, items_t *pItems, H264HeadersBitstream &bitStream)
899 {
900     uint32_t j;
901 
902     // decode number of items
903     numItems = (num_t) bitStream.GetVLCElement(false);
904     if (H264_MAX_NUM_VIEW_REF <= numItems)
905     {
906         return UMC_ERR_INVALID_STREAM;
907     }
908 
909     // decode items
910     for (j = 0; j < numItems; j += 1)
911     {
912         pItems[j] = (items_t) bitStream.GetVLCElement(false);
913         if (H264_MAX_NUM_VIEW <= pItems[j])
914         {
915             return UMC_ERR_INVALID_STREAM;
916         }
917     }
918 
919     return UMC_OK;
920 
921 } // Status DecodeViewReferenceInfo(num_t &numItems, items_t *pItems, H264HeadersBitstream &bitStream)
922 
GetSequenceParamSetSvcExt(H264SeqParamSetSVCExtension * pSPSSvcExt)923 Status H264HeadersBitstream::GetSequenceParamSetSvcExt(H264SeqParamSetSVCExtension *pSPSSvcExt)
924 {
925     pSPSSvcExt->Reset();
926 
927     if ((pSPSSvcExt->profile_idc != H264VideoDecoderParams::H264_PROFILE_SCALABLE_BASELINE) &&
928         (pSPSSvcExt->profile_idc != H264VideoDecoderParams::H264_PROFILE_SCALABLE_HIGH))
929         return UMC_OK;
930 
931     pSPSSvcExt->inter_layer_deblocking_filter_control_present_flag = Get1Bit();
932     pSPSSvcExt->extended_spatial_scalability = (uint8_t)GetBits(2);
933 
934     if (pSPSSvcExt->chroma_format_idc == 1 || pSPSSvcExt->chroma_format_idc == 2)
935     {
936         pSPSSvcExt->chroma_phase_x = Get1Bit() - 1;
937         pSPSSvcExt->seq_ref_layer_chroma_phase_x = pSPSSvcExt->chroma_phase_x;
938     }
939 
940     if (pSPSSvcExt->chroma_format_idc == 1)
941     {
942         pSPSSvcExt->chroma_phase_y = (int8_t)(GetBits(2) - 1);
943         pSPSSvcExt->seq_ref_layer_chroma_phase_y = pSPSSvcExt->chroma_phase_y;
944     }
945 
946     if (pSPSSvcExt->extended_spatial_scalability == 1)
947     {
948         if (pSPSSvcExt->chroma_format_idc > 0)
949         {
950             pSPSSvcExt->seq_ref_layer_chroma_phase_x = Get1Bit() - 1;
951             pSPSSvcExt->seq_ref_layer_chroma_phase_y = (int8_t)(GetBits(2) - 1);
952         }
953 
954         pSPSSvcExt->seq_scaled_ref_layer_left_offset = GetVLCElement(true);
955         pSPSSvcExt->seq_scaled_ref_layer_top_offset = GetVLCElement(true);
956         pSPSSvcExt->seq_scaled_ref_layer_right_offset = GetVLCElement(true);
957         pSPSSvcExt->seq_scaled_ref_layer_bottom_offset = GetVLCElement(true);
958     }
959 
960     pSPSSvcExt->seq_tcoeff_level_prediction_flag = Get1Bit();
961 
962     if (pSPSSvcExt->seq_tcoeff_level_prediction_flag)
963     {
964         pSPSSvcExt->adaptive_tcoeff_level_prediction_flag = Get1Bit();
965     }
966 
967     pSPSSvcExt->slice_header_restriction_flag = Get1Bit();
968 
969     CheckBSLeft();
970 
971     /* Reference has SVC VUI extension here, but there is no mention about it in standard */
972     uint32_t svc_vui_parameters_present_flag = Get1Bit();
973     if (svc_vui_parameters_present_flag)
974     {
975         GetSequenceParamSetSvcVuiExt(pSPSSvcExt); // ignore return status
976     }
977 
978     Get1Bit(); // additional_extension2_flag
979     return UMC_OK;
980 }
981 
982 struct VUI_Entry
983 {
984     uint32_t vui_ext_dependency_id;
985     uint32_t vui_ext_quality_id;
986     uint32_t vui_ext_temporal_id;
987     uint32_t vui_ext_timing_info_present_flag;
988     uint32_t vui_ext_num_units_in_tick;
989     uint32_t vui_ext_time_scale;
990     uint32_t vui_ext_fixed_frame_rate_flag;
991 
992     uint32_t vui_ext_nal_hrd_parameters_present_flag;
993     uint32_t vui_ext_vcl_hrd_parameters_present_flag;
994 
995     uint32_t vui_ext_low_delay_hrd_flag;
996     uint32_t vui_ext_pic_struct_present_flag;
997 };
998 
999 struct SVC_VUI
1000 {
1001     uint32_t vui_ext_num_entries;
1002     VUI_Entry vui_entry[1023];
1003 };
1004 
GetSequenceParamSetSvcVuiExt(H264SeqParamSetSVCExtension * pSPSSvcExt)1005 Status H264HeadersBitstream::GetSequenceParamSetSvcVuiExt(H264SeqParamSetSVCExtension *pSPSSvcExt)
1006 {
1007     SVC_VUI vui;
1008     vui.vui_ext_num_entries = GetVLCElement(false) + 1;
1009 
1010     for(uint32_t i = 0; i < vui.vui_ext_num_entries; i++)
1011     {
1012         vui.vui_entry[i].vui_ext_dependency_id      = GetBits(3);
1013         vui.vui_entry[i].vui_ext_quality_id         = GetBits(4);
1014         vui.vui_entry[i].vui_ext_temporal_id        = GetBits(3);
1015 
1016         vui.vui_entry[i].vui_ext_timing_info_present_flag = Get1Bit();
1017 
1018         if (vui.vui_entry[i].vui_ext_timing_info_present_flag)
1019         {
1020             vui.vui_entry[i].vui_ext_num_units_in_tick  = GetBits(32);
1021             vui.vui_entry[i].vui_ext_time_scale         = GetBits(32);
1022             vui.vui_entry[i].vui_ext_fixed_frame_rate_flag  = Get1Bit();
1023         }
1024 
1025         vui.vui_entry[i].vui_ext_nal_hrd_parameters_present_flag = Get1Bit();
1026         if (vui.vui_entry[i].vui_ext_nal_hrd_parameters_present_flag)
1027         {
1028             Status sts = GetHRDParam(pSPSSvcExt, &pSPSSvcExt->vui);
1029             if (sts != UMC_OK)
1030                 return sts;
1031         }
1032 
1033         vui.vui_entry[i].vui_ext_vcl_hrd_parameters_present_flag = Get1Bit();
1034         if (vui.vui_entry[i].vui_ext_vcl_hrd_parameters_present_flag)
1035         {
1036             Status sts = GetHRDParam(pSPSSvcExt, &pSPSSvcExt->vui);
1037             if (sts != UMC_OK)
1038                 return sts;
1039         }
1040 
1041         if (vui.vui_entry[i].vui_ext_nal_hrd_parameters_present_flag || vui.vui_entry[i].vui_ext_vcl_hrd_parameters_present_flag)
1042             vui.vui_entry[i].vui_ext_low_delay_hrd_flag = Get1Bit();
1043 
1044         vui.vui_entry[i].vui_ext_pic_struct_present_flag = Get1Bit();
1045     }
1046 
1047     return UMC_OK;
1048 }
1049 
GetSequenceParamSetMvcExt(H264SeqParamSetMVCExtension * pSPSMvcExt)1050 Status H264HeadersBitstream::GetSequenceParamSetMvcExt(H264SeqParamSetMVCExtension *pSPSMvcExt)
1051 {
1052     pSPSMvcExt->Reset();
1053 
1054     // decode the number of available views
1055     pSPSMvcExt->num_views_minus1 = GetVLCElement(false);
1056     if (H264_MAX_NUM_VIEW <= pSPSMvcExt->num_views_minus1)
1057     {
1058         return UMC_ERR_INVALID_STREAM;
1059     }
1060 
1061     // allocate the views' info structs
1062     pSPSMvcExt->viewInfo.resize(pSPSMvcExt->num_views_minus1 + 1, H264ViewRefInfo());
1063 
1064     // parse view IDs
1065     for (uint32_t i = 0; i <= pSPSMvcExt->num_views_minus1; i += 1)
1066     {
1067         H264ViewRefInfo &viewRefInfo = pSPSMvcExt->viewInfo[i];
1068 
1069         // get the view ID
1070         viewRefInfo.view_id = GetVLCElement(false);
1071         if (H264_MAX_NUM_VIEW <= viewRefInfo.view_id)
1072         {
1073             return UMC_ERR_INVALID_STREAM;
1074         }
1075     }
1076 
1077     // parse anchor refs
1078     for (uint32_t i = 1; i <= pSPSMvcExt->num_views_minus1; i += 1)
1079     {
1080         H264ViewRefInfo &viewRefInfo = pSPSMvcExt->viewInfo[i];
1081         uint32_t listNum;
1082 
1083         for (listNum = LIST_0; listNum <= LIST_1; listNum += 1)
1084         {
1085             // decode LX anchor refs info
1086             Status umcRes = DecodeViewReferenceInfo(viewRefInfo.num_anchor_refs_lx[listNum],
1087                                              viewRefInfo.anchor_refs_lx[listNum],
1088                                              *this);
1089             if (UMC_OK != umcRes)
1090             {
1091                 return umcRes;
1092             }
1093         }
1094     }
1095 
1096     // parse non-anchor refs
1097     for (uint32_t i = 1; i <= pSPSMvcExt->num_views_minus1; i += 1)
1098     {
1099         H264ViewRefInfo &viewRefInfo = pSPSMvcExt->viewInfo[i];
1100         uint32_t listNum;
1101 
1102         for (listNum = LIST_0; listNum <= LIST_1; listNum += 1)
1103         {
1104 
1105             // decode L0 non-anchor refs info
1106             Status umcRes = DecodeViewReferenceInfo(viewRefInfo.num_non_anchor_refs_lx[listNum],
1107                                              viewRefInfo.non_anchor_refs_lx[listNum],
1108                                              *this);
1109             if (UMC_OK != umcRes)
1110             {
1111                 return umcRes;
1112             }
1113         }
1114     }
1115 
1116     // decode the number of applicable signal points
1117     pSPSMvcExt->num_level_values_signalled_minus1 = GetVLCElement(false);
1118     if (H264_MAX_NUM_OPS <= pSPSMvcExt->num_level_values_signalled_minus1)
1119     {
1120         return UMC_ERR_INVALID_STREAM;
1121     }
1122 
1123     // allocate the level info structure
1124     pSPSMvcExt->levelInfo.resize(pSPSMvcExt->num_level_values_signalled_minus1 + 1, H264LevelValueSignaled());
1125 
1126     // decode all ops
1127     for (uint32_t i = 0; i <= pSPSMvcExt->num_level_values_signalled_minus1; i += 1)
1128     {
1129         H264LevelValueSignaled &levelInfo = pSPSMvcExt->levelInfo[i];
1130         uint32_t j;
1131 
1132         // decode the level's profile idc
1133         levelInfo.level_idc = (uint8_t) GetBits(8);
1134         MFX_CHECK(CheckLevel(levelInfo.level_idc), UMC_ERR_INVALID_STREAM);
1135 
1136         // decode the number of operation points
1137         levelInfo.num_applicable_ops_minus1 = (uint16_t) GetVLCElement(false);
1138         if (H264_MAX_NUM_VIEW <= levelInfo.num_applicable_ops_minus1)
1139         {
1140             return UMC_ERR_INVALID_STREAM;
1141         }
1142 
1143         // allocate the operation points structures
1144         levelInfo.opsInfo.resize(levelInfo.num_applicable_ops_minus1 + 1, H264ApplicableOp());
1145 
1146         // decode operation points
1147         for (j = 0; j <= levelInfo.num_applicable_ops_minus1; j += 1)
1148         {
1149             H264ApplicableOp &op = levelInfo.opsInfo[j];
1150             uint32_t k;
1151 
1152             // decode the temporal ID of the op
1153             op.applicable_op_temporal_id = (uint8_t) GetBits(3);
1154 
1155             // decode the number of target views
1156             op.applicable_op_num_target_views_minus1 = (uint16_t) GetVLCElement(false);
1157             if (H264_MAX_NUM_VIEW <= op.applicable_op_num_target_views_minus1)
1158             {
1159                 return UMC_ERR_INVALID_STREAM;
1160             }
1161 
1162             // allocate the target view ID array
1163             op.applicable_op_target_view_id.resize(op.applicable_op_num_target_views_minus1 + 1, 0);
1164 
1165             // read target view IDs
1166             for (k = 0; k <= op.applicable_op_num_target_views_minus1; k += 1)
1167             {
1168                 op.applicable_op_target_view_id[k] = (uint16_t) GetVLCElement(false);
1169                 if (H264_MAX_NUM_VIEW <= op.applicable_op_target_view_id[k])
1170                 {
1171                     return UMC_ERR_INVALID_STREAM;
1172                 }
1173             }
1174 
1175             // decode applicable number of views
1176             op.applicable_op_num_views_minus1 = (uint16_t) GetVLCElement(false);
1177             if (H264_MAX_NUM_VIEW <= op.applicable_op_num_views_minus1)
1178             {
1179                 return UMC_ERR_INVALID_STREAM;
1180             }
1181         }
1182     }
1183 
1184     CheckBSLeft();
1185     return UMC_OK;
1186 
1187 } // Status H264HeadersBitstream::GetSequenceParamSetMvcExt(H264SeqParamSetMVCExtension *pSPSMvcExt)
1188 
GetPictureParamSetPart1(H264PicParamSet * pps)1189 Status H264HeadersBitstream::GetPictureParamSetPart1(H264PicParamSet *pps)
1190 {
1191     // Not all members of the pic param set structure are contained in all
1192     // pic param sets. So start by init all to zero.
1193     pps->Reset();
1194 
1195     // id
1196     uint32_t pic_parameter_set_id = GetVLCElement(false);
1197     pps->pic_parameter_set_id = (uint16_t)pic_parameter_set_id;
1198     if (pic_parameter_set_id > MAX_NUM_PIC_PARAM_SETS-1)
1199     {
1200         return UMC_ERR_INVALID_STREAM;
1201     }
1202 
1203     // seq param set referred to by this pic param set
1204     uint32_t seq_parameter_set_id = GetVLCElement(false);
1205     pps->seq_parameter_set_id = (uint8_t)seq_parameter_set_id;
1206     if (seq_parameter_set_id > MAX_NUM_SEQ_PARAM_SETS-1)
1207     {
1208         return UMC_ERR_INVALID_STREAM;
1209     }
1210 
1211     CheckBSLeft();
1212     return UMC_OK;
1213 }    // GetPictureParamSetPart1
1214 
1215 // Number of bits required to code slice group ID, index is num_slice_groups - 2
1216 static const uint8_t SGIdBits[7] = {1,2,2,3,3,3,3};
1217 
1218 // ---------------------------------------------------------------------------
1219 //    Read picture parameter set data from bitstream.
1220 // ---------------------------------------------------------------------------
GetPictureParamSetPart2(H264PicParamSet * pps,H264SeqParamSet const * sps)1221 Status H264HeadersBitstream::GetPictureParamSetPart2(H264PicParamSet  *pps, H264SeqParamSet const* sps)
1222 {
1223     if (!sps)
1224         return UMC_ERR_NULL_PTR;
1225 
1226     pps->entropy_coding_mode = Get1Bit();
1227 
1228     pps->bottom_field_pic_order_in_frame_present_flag = Get1Bit();
1229 
1230     // number of slice groups, bitstream has value - 1
1231     pps->num_slice_groups = GetVLCElement(false) + 1;
1232     if (pps->num_slice_groups != 1)
1233     {
1234         if (pps->num_slice_groups > MAX_NUM_SLICE_GROUPS)
1235         {
1236             return UMC_ERR_INVALID_STREAM;
1237         }
1238 
1239         uint32_t slice_group_map_type = GetVLCElement(false);
1240         pps->SliceGroupInfo.slice_group_map_type = (uint8_t)slice_group_map_type;
1241 
1242         if (slice_group_map_type > 6)
1243             return UMC_ERR_INVALID_STREAM;
1244 
1245         uint32_t const PicSizeInMapUnits = sps->frame_width_in_mbs * sps->frame_height_in_mbs;
1246         // Get additional, map type dependent slice group data
1247         switch (pps->SliceGroupInfo.slice_group_map_type)
1248         {
1249         case 0:
1250             for (uint32_t slice_group = 0; slice_group < pps->num_slice_groups; slice_group++)
1251             {
1252                 // run length, bitstream has value - 1
1253                 pps->SliceGroupInfo.run_length[slice_group] = GetVLCElement(false) + 1;
1254                 if (pps->SliceGroupInfo.run_length[slice_group] > PicSizeInMapUnits)
1255                     return UMC_ERR_INVALID_STREAM;
1256             }
1257             break;
1258 
1259         case 1:
1260             // no additional info
1261             break;
1262 
1263         case 2:
1264             for (uint32_t slice_group = 0; slice_group < (uint32_t)(pps->num_slice_groups-1); slice_group++)
1265             {
1266                 pps->SliceGroupInfo.t1.top_left[slice_group] = GetVLCElement(false);
1267                 if (pps->SliceGroupInfo.t1.top_left[slice_group] >= PicSizeInMapUnits)
1268                     return UMC_ERR_INVALID_STREAM;
1269 
1270                 pps->SliceGroupInfo.t1.bottom_right[slice_group] = GetVLCElement(false);
1271                 if (pps->SliceGroupInfo.t1.bottom_right[slice_group] >= PicSizeInMapUnits)
1272                     return UMC_ERR_INVALID_STREAM;
1273 
1274                 // check for legal values
1275                 if (pps->SliceGroupInfo.t1.top_left[slice_group] >
1276                     pps->SliceGroupInfo.t1.bottom_right[slice_group])
1277                 {
1278                     return UMC_ERR_INVALID_STREAM;
1279                 }
1280             }
1281             break;
1282 
1283         case 3:
1284         case 4:
1285         case 5:
1286             // For map types 3..5, number of slice groups must be 2
1287             if (pps->num_slice_groups != 2)
1288             {
1289                 return UMC_ERR_INVALID_STREAM;
1290             }
1291             pps->SliceGroupInfo.t2.slice_group_change_direction_flag = Get1Bit();
1292             pps->SliceGroupInfo.t2.slice_group_change_rate = GetVLCElement(false) + 1;
1293             if (pps->SliceGroupInfo.t2.slice_group_change_rate > PicSizeInMapUnits)
1294                 return UMC_ERR_INVALID_STREAM;
1295             break;
1296 
1297         case 6:
1298             // mapping of slice group to map unit (macroblock if not fields) is
1299             // per map unit, read from bitstream
1300             {
1301                 // number of map units, bitstream has value - 1
1302                 pps->SliceGroupInfo.t3.pic_size_in_map_units = GetVLCElement(false) + 1;
1303                 if (pps->SliceGroupInfo.t3.pic_size_in_map_units != PicSizeInMapUnits)
1304                     return UMC_ERR_INVALID_STREAM;
1305 
1306                 uint32_t const len = std::max(1u, pps->SliceGroupInfo.t3.pic_size_in_map_units);
1307 
1308                 pps->SliceGroupInfo.pSliceGroupIDMap.resize(len);
1309 
1310                 // num_bits is Ceil(log2(num_groups)) - number of bits used to code each slice group id
1311                 uint32_t const num_bits = SGIdBits[pps->num_slice_groups - 2];
1312 
1313                 for (uint32_t map_unit = 0; map_unit < pps->SliceGroupInfo.t3.pic_size_in_map_units; map_unit++)
1314                 {
1315                     uint8_t const slice_group_id = (uint8_t)GetBits(num_bits);
1316                     if (slice_group_id >  pps->num_slice_groups - 1)
1317                         return UMC_ERR_INVALID_STREAM;
1318 
1319                     pps->SliceGroupInfo.pSliceGroupIDMap[map_unit] = slice_group_id;
1320                 }
1321             }
1322             break;
1323 
1324         default:
1325             return UMC_ERR_INVALID_STREAM;
1326 
1327         }    // switch
1328     }    // slice group info
1329 
1330     // number of list 0 ref pics used to decode picture, bitstream has value - 1
1331     pps->num_ref_idx_l0_active = GetVLCElement(false) + 1;
1332 
1333     // number of list 1 ref pics used to decode picture, bitstream has value - 1
1334     pps->num_ref_idx_l1_active = GetVLCElement(false) + 1;
1335 
1336     if (pps->num_ref_idx_l1_active > MAX_NUM_REF_FRAMES || pps->num_ref_idx_l0_active > MAX_NUM_REF_FRAMES)
1337         return UMC_ERR_INVALID_STREAM;
1338 
1339     // weighted pediction
1340     pps->weighted_pred_flag = Get1Bit();
1341     pps->weighted_bipred_idc = (uint8_t)GetBits(2);
1342 
1343     // default slice QP, bitstream has value - 26
1344     int32_t pic_init_qp = GetVLCElement(true) + 26;
1345     pps->pic_init_qp = (int8_t)pic_init_qp;
1346     if (pic_init_qp > QP_MAX)
1347     {
1348         //return UMC_ERR_INVALID_STREAM;
1349     }
1350 
1351     // default SP/SI slice QP, bitstream has value - 26
1352     pps->pic_init_qs = (uint8_t)(GetVLCElement(true) + 26);
1353     if (pps->pic_init_qs > QP_MAX)
1354     {
1355         //return UMC_ERR_INVALID_STREAM;
1356     }
1357 
1358     pps->chroma_qp_index_offset[0] = (int8_t)GetVLCElement(true);
1359     if ((pps->chroma_qp_index_offset[0] < -12) || (pps->chroma_qp_index_offset[0] > 12))
1360     {
1361         //return UMC_ERR_INVALID_STREAM;
1362     }
1363 
1364     pps->deblocking_filter_variables_present_flag = Get1Bit();
1365     pps->constrained_intra_pred_flag = Get1Bit();
1366     pps->redundant_pic_cnt_present_flag = Get1Bit();
1367 
1368     if (More_RBSP_Data())
1369     {
1370         pps->transform_8x8_mode_flag = Get1Bit();
1371         pps->pic_scaling_matrix_present_flag = Get1Bit();
1372 
1373         H264ScalingPicParams * scaling = &pps->scaling[0];
1374 
1375         if (pps->pic_scaling_matrix_present_flag)
1376         {
1377             for (int32_t i = 0; i < 3; i++)
1378             {
1379                 pps->pic_scaling_list_present_flag[i] = Get1Bit();
1380                 if (pps->pic_scaling_list_present_flag[i])
1381                 {
1382                     GetScalingList4x4(&scaling->ScalingLists4x4[i], (uint8_t*)default_intra_scaling_list4x4, &pps->type_of_scaling_list_used[i]);
1383                 }
1384                 else
1385                 {
1386                     pps->type_of_scaling_list_used[i] = SCLDEFAULT;
1387                 }
1388             }
1389 
1390             for (int32_t i = 3; i < 6; i++)
1391             {
1392                 pps->pic_scaling_list_present_flag[i] = Get1Bit();
1393                 if (pps->pic_scaling_list_present_flag[i])
1394                 {
1395                     GetScalingList4x4(&scaling->ScalingLists4x4[i], (uint8_t*)default_inter_scaling_list4x4, &pps->type_of_scaling_list_used[i]);
1396                 }
1397                 else
1398                 {
1399                     pps->type_of_scaling_list_used[i] = SCLDEFAULT;
1400                 }
1401             }
1402 
1403 
1404             if (pps->transform_8x8_mode_flag)
1405             {
1406                 pps->pic_scaling_list_present_flag[6] = Get1Bit();
1407                 if (pps->pic_scaling_list_present_flag[6])
1408                 {
1409                     GetScalingList8x8(&scaling->ScalingLists8x8[0], (uint8_t*)default_intra_scaling_list8x8, &pps->type_of_scaling_list_used[6]);
1410                 }
1411                 else
1412                 {
1413                     pps->type_of_scaling_list_used[6] = SCLDEFAULT;
1414                 }
1415 
1416                 pps->pic_scaling_list_present_flag[7] = Get1Bit();
1417                 if (pps->pic_scaling_list_present_flag[7])
1418                 {
1419                     GetScalingList8x8(&scaling->ScalingLists8x8[1], (uint8_t*)default_inter_scaling_list8x8, &pps->type_of_scaling_list_used[7]);
1420                 }
1421                 else
1422                 {
1423                     pps->type_of_scaling_list_used[7] = SCLDEFAULT;
1424                 }
1425             }
1426 
1427             MFX_INTERNAL_CPY(&pps->scaling[1], &pps->scaling[0], sizeof(H264ScalingPicParams));
1428         }
1429 
1430         pps->chroma_qp_index_offset[1] = (int8_t)GetVLCElement(true);
1431     }
1432     else
1433     {
1434         pps->chroma_qp_index_offset[1] = pps->chroma_qp_index_offset[0];
1435     }
1436 
1437     CheckBSLeft();
1438     return UMC_OK;
1439 }    // GetPictureParamSet
1440 
InitializePictureParamSet(H264PicParamSet * pps,const H264SeqParamSet * sps,bool isExtension)1441 Status InitializePictureParamSet(H264PicParamSet *pps, const H264SeqParamSet *sps, bool isExtension)
1442 {
1443     if (!pps || !sps || pps->initialized[isExtension])
1444         return UMC_OK;
1445 
1446     if (pps->num_slice_groups != 1)
1447     {
1448         uint32_t PicSizeInMapUnits = sps->frame_width_in_mbs * sps->frame_height_in_mbs; // for range checks
1449 
1450         // Get additional, map type dependent slice group data
1451         switch (pps->SliceGroupInfo.slice_group_map_type)
1452         {
1453         case 0:
1454             for (uint32_t slice_group=0; slice_group<pps->num_slice_groups; slice_group++)
1455             {
1456                 if (pps->SliceGroupInfo.run_length[slice_group] > PicSizeInMapUnits)
1457                 {
1458                     return UMC_ERR_INVALID_STREAM;
1459                 }
1460             }
1461             break;
1462         case 1:
1463             // no additional info
1464             break;
1465         case 2:
1466             for (uint32_t slice_group=0; slice_group<(uint32_t)(pps->num_slice_groups-1); slice_group++)
1467             {
1468                 if (pps->SliceGroupInfo.t1.bottom_right[slice_group] >= PicSizeInMapUnits)
1469                 {
1470                     return UMC_ERR_INVALID_STREAM;
1471                 }
1472                 if ((pps->SliceGroupInfo.t1.top_left[slice_group] %
1473                     sps->frame_width_in_mbs) >
1474                     (pps->SliceGroupInfo.t1.bottom_right[slice_group] %
1475                     sps->frame_width_in_mbs))
1476                 {
1477                     return UMC_ERR_INVALID_STREAM;
1478                 }
1479             }
1480             break;
1481         case 3:
1482         case 4:
1483         case 5:
1484             if (pps->SliceGroupInfo.t2.slice_group_change_rate > PicSizeInMapUnits)
1485             {
1486                 return UMC_ERR_INVALID_STREAM;
1487             }
1488             break;
1489         case 6:
1490             // mapping of slice group to map unit (macroblock if not fields) is
1491             // per map unit, read from bitstream
1492             {
1493                 if (pps->SliceGroupInfo.t3.pic_size_in_map_units != PicSizeInMapUnits)
1494                 {
1495                     return UMC_ERR_INVALID_STREAM;
1496                 }
1497                 for (uint32_t map_unit = 0;
1498                      map_unit < pps->SliceGroupInfo.t3.pic_size_in_map_units;
1499                      map_unit++)
1500                 {
1501                     if (pps->SliceGroupInfo.pSliceGroupIDMap[map_unit] >
1502                         pps->num_slice_groups - 1)
1503                     {
1504                         return UMC_ERR_INVALID_STREAM;
1505                     }
1506                 }
1507             }
1508             break;
1509         default:
1510             return UMC_ERR_INVALID_STREAM;
1511 
1512         }    // switch
1513     }    // slice group info
1514 
1515     H264ScalingPicParams * scaling = &pps->scaling[isExtension];
1516     if (pps->pic_scaling_matrix_present_flag)
1517     {
1518         uint8_t *default_scaling = (uint8_t*)default_intra_scaling_list4x4;
1519         for (int32_t i = 0; i < 6; i += 3)
1520         {
1521             if (!pps->pic_scaling_list_present_flag[i])
1522             {
1523                 if (sps->seq_scaling_matrix_present_flag) {
1524                     FillScalingList4x4(&scaling->ScalingLists4x4[i], (uint8_t*)sps->ScalingLists4x4[i].ScalingListCoeffs);
1525                 }
1526                 else
1527                 {
1528                     FillScalingList4x4(&scaling->ScalingLists4x4[i], (uint8_t*)default_scaling);
1529                 }
1530             }
1531 
1532             if (!pps->pic_scaling_list_present_flag[i+1])
1533             {
1534                 FillScalingList4x4(&scaling->ScalingLists4x4[i+1], (uint8_t*)scaling->ScalingLists4x4[i].ScalingListCoeffs);
1535             }
1536 
1537             if (!pps->pic_scaling_list_present_flag[i+2])
1538             {
1539                 FillScalingList4x4(&scaling->ScalingLists4x4[i+2], (uint8_t*)scaling->ScalingLists4x4[i+1].ScalingListCoeffs);
1540             }
1541 
1542             default_scaling = (uint8_t*)default_inter_scaling_list4x4;
1543         }
1544 
1545         if (pps->transform_8x8_mode_flag)
1546         {
1547             if (sps->seq_scaling_matrix_present_flag) {
1548                 for (int32_t i = 6; i < 8; i++)
1549                 {
1550                     if (!pps->pic_scaling_list_present_flag[i])
1551                     {
1552                         FillScalingList8x8(&scaling->ScalingLists8x8[i-6], (uint8_t*)sps->ScalingLists8x8[i-6].ScalingListCoeffs);
1553                     }
1554                 }
1555             }
1556             else
1557             {
1558                 if (!pps->pic_scaling_list_present_flag[6])
1559                 {
1560                     FillScalingList8x8(&scaling->ScalingLists8x8[0], (uint8_t*)default_intra_scaling_list8x8);
1561                 }
1562 
1563                 if (!pps->pic_scaling_list_present_flag[7])
1564                 {
1565                     FillScalingList8x8(&scaling->ScalingLists8x8[1], (uint8_t*)default_inter_scaling_list8x8);
1566                 }
1567             }
1568         }
1569     }
1570     else
1571     {
1572         for (int32_t i = 0; i < 6; i++)
1573         {
1574             FillScalingList4x4(&scaling->ScalingLists4x4[i],(uint8_t *)sps->ScalingLists4x4[i].ScalingListCoeffs);
1575             pps->type_of_scaling_list_used[i] = sps->type_of_scaling_list_used[i];
1576         }
1577 
1578         if (pps->transform_8x8_mode_flag)
1579         {
1580             for (int32_t i = 0; i < 2; i++)
1581             {
1582                 FillScalingList8x8(&scaling->ScalingLists8x8[i],(uint8_t *)sps->ScalingLists8x8[i].ScalingListCoeffs);
1583                 pps->type_of_scaling_list_used[i] = sps->type_of_scaling_list_used[i];
1584             }
1585         }
1586     }
1587 
1588     // calculate level scale matrices
1589 
1590     //start DC first
1591     //to do: reduce th anumber of matrices (in fact 1 is enough)
1592     // now process other 4x4 matrices
1593     for (int32_t i = 0; i < 6; i++)
1594     {
1595         for (int32_t j = 0; j < 88; j++)
1596             for (int32_t k = 0; k < 16; k++)
1597             {
1598                 uint32_t level_scale = scaling->ScalingLists4x4[i].ScalingListCoeffs[k]*pre_norm_adjust4x4[j%6][pre_norm_adjust_index4x4[k]];
1599                 scaling->m_LevelScale4x4[i].LevelScaleCoeffs[j][k] = (int16_t) level_scale;
1600             }
1601     }
1602 
1603     // process remaining 8x8  matrices
1604     for (int32_t i = 0; i < 2; i++)
1605     {
1606         for (int32_t j = 0; j < 88; j++)
1607             for (int32_t k = 0; k < 64; k++)
1608             {
1609                 uint32_t level_scale = scaling->ScalingLists8x8[i].ScalingListCoeffs[k]*pre_norm_adjust8x8[j%6][pre_norm_adjust_index8x8[k]];
1610                 scaling->m_LevelScale8x8[i].LevelScaleCoeffs[j][k] = (int16_t) level_scale;
1611             }
1612     }
1613 
1614     pps->initialized[isExtension] = 1;
1615     return UMC_OK;
1616 }
1617 
GetNalUnitPrefix(H264NalExtension * pExt,uint32_t NALRef_idc)1618 Status H264HeadersBitstream::GetNalUnitPrefix(H264NalExtension *pExt, uint32_t NALRef_idc)
1619 {
1620     Status ps = UMC_OK;
1621 
1622     ps = GetNalUnitExtension(pExt);
1623 
1624     if (ps != UMC_OK || !pExt->svc_extension_flag)
1625         return ps;
1626 
1627     if (pExt->svc.dependency_id || pExt->svc.quality_id) // shall be equals zero for prefix NAL units
1628         return UMC_ERR_INVALID_STREAM;
1629 
1630     pExt->svc.adaptiveMarkingInfo.num_entries = 0;
1631 
1632     if (NALRef_idc != 0)
1633     {
1634         pExt->svc.store_ref_base_pic_flag = Get1Bit();
1635 
1636         if ((pExt->svc.use_ref_base_pic_flag || pExt->svc.store_ref_base_pic_flag) && !pExt->svc.idr_flag)
1637         {
1638             ps = DecRefBasePicMarking(&pExt->svc.adaptiveMarkingInfo, pExt->svc.adaptive_ref_base_pic_marking_mode_flag);
1639             if (ps != UMC_OK)
1640                 return ps;
1641         }
1642 
1643         /*int32_t additional_prefix_nal_unit_extension_flag = (int32_t) Get1Bit();
1644 
1645         if (additional_prefix_nal_unit_extension_flag == 1)
1646         {
1647             additional_prefix_nal_unit_extension_flag = Get1Bit();
1648         }*/
1649     }
1650 
1651     CheckBSLeft();
1652     return ps;
1653 } // Status H264HeadersBitstream::GetNalUnitPrefix(void)
1654 
GetNalUnitExtension(H264NalExtension * pExt)1655 Status H264HeadersBitstream::GetNalUnitExtension(H264NalExtension *pExt)
1656 {
1657     pExt->Reset();
1658     pExt->extension_present = 1;
1659 
1660     // decode the type of the extension
1661     pExt->svc_extension_flag = (uint8_t) GetBits(1);
1662 
1663     // decode SVC extension
1664     if (pExt->svc_extension_flag)
1665     {
1666         pExt->svc.idr_flag = Get1Bit();
1667         pExt->svc.priority_id = (uint8_t) GetBits(6);
1668         pExt->svc.no_inter_layer_pred_flag = Get1Bit();
1669         pExt->svc.dependency_id = (uint8_t) GetBits(3);
1670         pExt->svc.quality_id = (uint8_t) GetBits(4);
1671         pExt->svc.temporal_id = (uint8_t) GetBits(3);
1672         pExt->svc.use_ref_base_pic_flag = Get1Bit();
1673         pExt->svc.discardable_flag = Get1Bit();
1674         pExt->svc.output_flag = Get1Bit();
1675         GetBits(2);
1676     }
1677     // decode MVC extension
1678     else
1679     {
1680         pExt->mvc.non_idr_flag = Get1Bit();
1681         pExt->mvc.priority_id = (uint16_t) GetBits(6);
1682         pExt->mvc.view_id = (uint16_t) GetBits(10);
1683         pExt->mvc.temporal_id = (uint8_t) GetBits(3);
1684         pExt->mvc.anchor_pic_flag = Get1Bit();
1685         pExt->mvc.inter_view_flag = Get1Bit();
1686         GetBits(1);
1687     }
1688 
1689     CheckBSLeft();
1690     return UMC_OK;
1691 
1692 } // Status H264HeadersBitstream::GetNalUnitExtension(H264NalExtension *pExt)
1693 
1694 // ---------------------------------------------------------------------------
1695 //    Read H.264 first part of slice header
1696 //
1697 //  Reading the rest of the header requires info in the picture and sequence
1698 //  parameter sets referred to by this slice header.
1699 //
1700 //    Do not print debug messages when IsSearch is true. In that case the function
1701 //    is being used to find the next compressed frame, errors may occur and should
1702 //    not be reported.
1703 //
1704 // ---------------------------------------------------------------------------
GetSliceHeaderPart1(H264SliceHeader * hdr)1705 Status H264HeadersBitstream::GetSliceHeaderPart1(H264SliceHeader *hdr)
1706 {
1707     uint32_t val;
1708 
1709     // decode NAL extension
1710     if (NAL_UT_CODED_SLICE_EXTENSION == hdr->nal_unit_type)
1711     {
1712         GetNalUnitExtension(&hdr->nal_ext);
1713 
1714         // set the IDR flag
1715         if (hdr->nal_ext.svc_extension_flag)
1716         {
1717             hdr->IdrPicFlag = hdr->nal_ext.svc.idr_flag;
1718         }
1719         else
1720         {
1721             hdr->IdrPicFlag = hdr->nal_ext.mvc.non_idr_flag ^ 1;
1722         }
1723     }
1724     else
1725     {
1726         if (hdr->nal_ext.extension_present)
1727         {
1728             if (hdr->nal_ext.svc_extension_flag)
1729             {
1730                 //hdr->IdrPicFlag = hdr->nal_ext.svc.idr_flag;
1731             }
1732             else
1733             {
1734                 //hdr->IdrPicFlag = hdr->nal_ext.mvc.non_idr_flag ^ 1;
1735             }
1736         }
1737         else
1738         {
1739             //hdr->IdrPicFlag = (NAL_UT_IDR_SLICE == hdr->nal_unit_type) ? 1 : 0;
1740             hdr->nal_ext.mvc.anchor_pic_flag = (uint8_t) hdr->IdrPicFlag ? 1 : 0;
1741             hdr->nal_ext.mvc.inter_view_flag = (uint8_t) 1;
1742         }
1743 
1744         hdr->IdrPicFlag = (NAL_UT_IDR_SLICE == hdr->nal_unit_type) ? 1 : 0;
1745     }
1746 
1747     hdr->first_mb_in_slice = GetVLCElement(false);
1748     if (0 > hdr->first_mb_in_slice) // upper bound is checked in H264Slice
1749         return UMC_ERR_INVALID_STREAM;
1750 
1751     // slice type
1752     val = GetVLCElement(false);
1753     if (val > S_INTRASLICE)
1754     {
1755         if (val > S_INTRASLICE + S_INTRASLICE + 1)
1756         {
1757             return UMC_ERR_INVALID_STREAM;
1758         }
1759         else
1760         {
1761             // Slice type is specifying type of not only this but all remaining
1762             // slices in the picture. Since slice type is always present, this bit
1763             // of info is not used in our implementation. Adjust (just shift range)
1764             // and return type without this extra info.
1765             val -= (S_INTRASLICE + 1);
1766         }
1767     }
1768 
1769     if (val > INTRASLICE) // all other doesn't support
1770         return UMC_ERR_INVALID_STREAM;
1771 
1772     hdr->slice_type = (EnumSliceCodType)val;
1773     if (NAL_UT_IDR_SLICE == hdr->nal_unit_type && hdr->slice_type != INTRASLICE)
1774         return UMC_ERR_INVALID_STREAM;
1775 
1776     uint32_t pic_parameter_set_id = GetVLCElement(false);
1777     hdr->pic_parameter_set_id = (uint16_t)pic_parameter_set_id;
1778     if (pic_parameter_set_id > MAX_NUM_PIC_PARAM_SETS - 1)
1779     {
1780         return UMC_ERR_INVALID_STREAM;
1781     }
1782 
1783     CheckBSLeft();
1784     return UMC_OK;
1785 } // Status GetSliceHeaderPart1(H264SliceHeader *pSliceHeader)
1786 
GetSliceHeaderPart2(H264SliceHeader * hdr,const H264PicParamSet * pps,const H264SeqParamSet * sps)1787 Status H264HeadersBitstream::GetSliceHeaderPart2(H264SliceHeader *hdr,
1788                                                  const H264PicParamSet *pps,
1789                                                  const H264SeqParamSet *sps)
1790 {
1791     hdr->frame_num = GetBits(sps->log2_max_frame_num);
1792 
1793     hdr->bottom_field_flag = 0;
1794     if (sps->frame_mbs_only_flag == 0)
1795     {
1796         hdr->field_pic_flag = Get1Bit();
1797         hdr->MbaffFrameFlag = !hdr->field_pic_flag && sps->mb_adaptive_frame_field_flag;
1798         if (hdr->field_pic_flag != 0)
1799         {
1800             hdr->bottom_field_flag = Get1Bit();
1801         }
1802     }
1803 
1804     if (hdr->MbaffFrameFlag)
1805     {
1806         uint32_t const first_mb_in_slice
1807             = hdr->first_mb_in_slice;
1808 
1809         if (first_mb_in_slice * 2 > INT_MAX)
1810             return UMC_ERR_INVALID_STREAM;
1811 
1812         // correct frst_mb_in_slice in order to handle MBAFF
1813         hdr->first_mb_in_slice *= 2;
1814     }
1815 
1816     if (hdr->IdrPicFlag)
1817     {
1818         int32_t pic_id = hdr->idr_pic_id = GetVLCElement(false);
1819         if (pic_id < 0 || pic_id > 65535)
1820             return UMC_ERR_INVALID_STREAM;
1821     }
1822 
1823     if (sps->pic_order_cnt_type == 0)
1824     {
1825         hdr->pic_order_cnt_lsb = GetBits(sps->log2_max_pic_order_cnt_lsb);
1826         if (pps->bottom_field_pic_order_in_frame_present_flag && (!hdr->field_pic_flag))
1827             hdr->delta_pic_order_cnt_bottom = GetVLCElement(true);
1828     }
1829 
1830     if ((sps->pic_order_cnt_type == 1) && (sps->delta_pic_order_always_zero_flag == 0))
1831     {
1832         hdr->delta_pic_order_cnt[0] = GetVLCElement(true);
1833         if (pps->bottom_field_pic_order_in_frame_present_flag && (!hdr->field_pic_flag))
1834             hdr->delta_pic_order_cnt[1] = GetVLCElement(true);
1835     }
1836 
1837     if (pps->redundant_pic_cnt_present_flag)
1838     {
1839         hdr->hw_wa_redundant_elimination_bits[0] = (uint32_t)BitsDecoded();
1840         // redundant pic count
1841         hdr->redundant_pic_cnt = GetVLCElement(false);
1842         if (hdr->redundant_pic_cnt > 127)
1843             return UMC_ERR_INVALID_STREAM;
1844 
1845         hdr->hw_wa_redundant_elimination_bits[1] = (uint32_t)BitsDecoded();
1846     }
1847 
1848     CheckBSLeft();
1849     return UMC_OK;
1850 }
1851 
DecRefBasePicMarking(AdaptiveMarkingInfo * pAdaptiveMarkingInfo,uint8_t & adaptive_ref_pic_marking_mode_flag)1852 Status H264HeadersBitstream::DecRefBasePicMarking(AdaptiveMarkingInfo *pAdaptiveMarkingInfo,
1853                                     uint8_t &adaptive_ref_pic_marking_mode_flag)
1854 {
1855     uint32_t memory_management_control_operation;
1856     uint32_t num_entries = 0;
1857 
1858     adaptive_ref_pic_marking_mode_flag = Get1Bit();
1859 
1860     while (adaptive_ref_pic_marking_mode_flag != 0) {
1861         memory_management_control_operation = (uint8_t)GetVLCElement(false);
1862         if (memory_management_control_operation == 0)
1863             break;
1864 
1865         if (memory_management_control_operation > 6)
1866             return UMC_ERR_INVALID_STREAM;
1867 
1868         pAdaptiveMarkingInfo->mmco[num_entries] =
1869             (uint8_t)memory_management_control_operation;
1870 
1871         if (memory_management_control_operation != 5)
1872             pAdaptiveMarkingInfo->value[num_entries*2] = GetVLCElement(false);
1873 
1874         // Only mmco 3 requires 2 values
1875         if (memory_management_control_operation == 3)
1876             pAdaptiveMarkingInfo->value[num_entries*2+1] = GetVLCElement(false);
1877 
1878         num_entries++;
1879         if (num_entries >= MAX_NUM_REF_FRAMES) {
1880             return UMC_ERR_INVALID_STREAM;
1881         }
1882     }    // while
1883     pAdaptiveMarkingInfo->num_entries = num_entries;
1884     return UMC_OK;
1885 }
1886 
1887 
GetPredWeightTable(H264SliceHeader * hdr,const H264SeqParamSet * sps,PredWeightTable * pPredWeight_L0,PredWeightTable * pPredWeight_L1)1888 Status H264HeadersBitstream::GetPredWeightTable(
1889     H264SliceHeader *hdr,        // slice header read goes here
1890     const H264SeqParamSet *sps,
1891     PredWeightTable *pPredWeight_L0, // L0 weight table goes here
1892     PredWeightTable *pPredWeight_L1) // L1 weight table goes here
1893 {
1894     uint32_t luma_log2_weight_denom = GetVLCElement(false);
1895     hdr->luma_log2_weight_denom = (uint8_t)luma_log2_weight_denom;
1896     if (luma_log2_weight_denom > 7)
1897         return UMC_ERR_INVALID_STREAM;
1898 
1899     if (sps->chroma_format_idc != 0)
1900     {
1901         uint32_t chroma_log2_weight_denom = GetVLCElement(false);
1902         hdr->chroma_log2_weight_denom = (uint8_t)chroma_log2_weight_denom;
1903         if (chroma_log2_weight_denom > 7)
1904             return UMC_ERR_INVALID_STREAM;
1905     }
1906 
1907     for (int32_t refindex = 0; refindex < hdr->num_ref_idx_l0_active; refindex++)
1908     {
1909         pPredWeight_L0[refindex].luma_weight_flag = Get1Bit();
1910         if (pPredWeight_L0[refindex].luma_weight_flag)
1911         {
1912             pPredWeight_L0[refindex].luma_weight = (int8_t)GetVLCElement(true);
1913             pPredWeight_L0[refindex].luma_offset = (int8_t)GetVLCElement(true);
1914             pPredWeight_L0[refindex].luma_offset <<= (sps->bit_depth_luma - 8);
1915         }
1916         else
1917         {
1918             pPredWeight_L0[refindex].luma_weight = (int8_t)(1 << hdr->luma_log2_weight_denom);
1919             pPredWeight_L0[refindex].luma_offset = 0;
1920         }
1921 
1922         pPredWeight_L0[refindex].chroma_weight_flag = 0;
1923         if (sps->chroma_format_idc != 0)
1924         {
1925             pPredWeight_L0[refindex].chroma_weight_flag = Get1Bit();
1926         }
1927 
1928         if (pPredWeight_L0[refindex].chroma_weight_flag)
1929         {
1930             pPredWeight_L0[refindex].chroma_weight[0] = (int8_t)GetVLCElement(true);
1931             pPredWeight_L0[refindex].chroma_offset[0] = (int8_t)GetVLCElement(true);
1932             pPredWeight_L0[refindex].chroma_weight[1] = (int8_t)GetVLCElement(true);
1933             pPredWeight_L0[refindex].chroma_offset[1] = (int8_t)GetVLCElement(true);
1934 
1935             pPredWeight_L0[refindex].chroma_offset[0] <<= (sps->bit_depth_chroma - 8);
1936             pPredWeight_L0[refindex].chroma_offset[1] <<= (sps->bit_depth_chroma - 8);
1937         }
1938         else
1939         {
1940             pPredWeight_L0[refindex].chroma_weight[0] = (int8_t)(1 << hdr->chroma_log2_weight_denom);
1941             pPredWeight_L0[refindex].chroma_weight[1] = (int8_t)(1 << hdr->chroma_log2_weight_denom);
1942             pPredWeight_L0[refindex].chroma_offset[0] = 0;
1943             pPredWeight_L0[refindex].chroma_offset[1] = 0;
1944         }
1945     }
1946 
1947     if (BPREDSLICE == hdr->slice_type)
1948     {
1949         for (int32_t refindex = 0; refindex < hdr->num_ref_idx_l1_active; refindex++)
1950         {
1951             pPredWeight_L1[refindex].luma_weight_flag = Get1Bit();
1952             if (pPredWeight_L1[refindex].luma_weight_flag)
1953             {
1954                 pPredWeight_L1[refindex].luma_weight = (int8_t)GetVLCElement(true);
1955                 pPredWeight_L1[refindex].luma_offset = (int8_t)GetVLCElement(true);
1956                 pPredWeight_L1[refindex].luma_offset <<= (sps->bit_depth_luma - 8);
1957             }
1958             else
1959             {
1960                 pPredWeight_L1[refindex].luma_weight = (int8_t)(1 << hdr->luma_log2_weight_denom);
1961                 pPredWeight_L1[refindex].luma_offset = 0;
1962             }
1963 
1964             pPredWeight_L1[refindex].chroma_weight_flag = 0;
1965             if (sps->chroma_format_idc != 0)
1966                 pPredWeight_L1[refindex].chroma_weight_flag = Get1Bit();
1967 
1968             if (pPredWeight_L1[refindex].chroma_weight_flag)
1969             {
1970                 pPredWeight_L1[refindex].chroma_weight[0] = (int8_t)GetVLCElement(true);
1971                 pPredWeight_L1[refindex].chroma_offset[0] = (int8_t)GetVLCElement(true);
1972                 pPredWeight_L1[refindex].chroma_weight[1] = (int8_t)GetVLCElement(true);
1973                 pPredWeight_L1[refindex].chroma_offset[1] = (int8_t)GetVLCElement(true);
1974 
1975                 pPredWeight_L1[refindex].chroma_offset[0] <<= (sps->bit_depth_chroma - 8);
1976                 pPredWeight_L1[refindex].chroma_offset[1] <<= (sps->bit_depth_chroma - 8);
1977             }
1978             else
1979             {
1980                 pPredWeight_L1[refindex].chroma_weight[0] = (int8_t)(1 << hdr->chroma_log2_weight_denom);
1981                 pPredWeight_L1[refindex].chroma_weight[1] = (int8_t)(1 << hdr->chroma_log2_weight_denom);
1982                 pPredWeight_L1[refindex].chroma_offset[0] = 0;
1983                 pPredWeight_L1[refindex].chroma_offset[1] = 0;
1984             }
1985         }
1986     }    // B slice
1987 
1988     return UMC_OK;
1989 }
1990 
GetSliceHeaderPart3(H264SliceHeader * hdr,PredWeightTable * pPredWeight_L0,PredWeightTable * pPredWeight_L1,RefPicListReorderInfo * pReorderInfo_L0,RefPicListReorderInfo * pReorderInfo_L1,AdaptiveMarkingInfo * pAdaptiveMarkingInfo,AdaptiveMarkingInfo * pBaseAdaptiveMarkingInfo,const H264PicParamSet * pps,const H264SeqParamSet * sps,const H264SeqParamSetSVCExtension * spsSvcExt)1991 Status H264HeadersBitstream::GetSliceHeaderPart3(
1992     H264SliceHeader *hdr,        // slice header read goes here
1993     PredWeightTable *pPredWeight_L0, // L0 weight table goes here
1994     PredWeightTable *pPredWeight_L1, // L1 weight table goes here
1995     RefPicListReorderInfo *pReorderInfo_L0,
1996     RefPicListReorderInfo *pReorderInfo_L1,
1997     AdaptiveMarkingInfo *pAdaptiveMarkingInfo,
1998     AdaptiveMarkingInfo *pBaseAdaptiveMarkingInfo,
1999     const H264PicParamSet *pps,
2000     const H264SeqParamSet *sps,
2001     const H264SeqParamSetSVCExtension *spsSvcExt)
2002 {
2003     uint8_t ref_pic_list_reordering_flag_l0 = 0;
2004     uint8_t ref_pic_list_reordering_flag_l1 = 0;
2005     Status ps = UMC_OK;
2006 
2007     pReorderInfo_L0->num_entries = 0;
2008     pReorderInfo_L1->num_entries = 0;
2009     hdr->num_ref_idx_l0_active = pps->num_ref_idx_l0_active;
2010     hdr->num_ref_idx_l1_active = pps->num_ref_idx_l1_active;
2011 
2012     hdr->direct_spatial_mv_pred_flag = 1;
2013 
2014     if (!hdr->nal_ext.svc_extension_flag || hdr->nal_ext.svc.quality_id == 0)
2015     {
2016         if (BPREDSLICE == hdr->slice_type)
2017         {
2018             // direct mode prediction method
2019             hdr->direct_spatial_mv_pred_flag = Get1Bit();
2020         }
2021 
2022         if (PREDSLICE == hdr->slice_type ||
2023             S_PREDSLICE == hdr->slice_type ||
2024             BPREDSLICE == hdr->slice_type)
2025         {
2026             hdr->num_ref_idx_active_override_flag = Get1Bit();
2027             if (hdr->num_ref_idx_active_override_flag != 0)
2028             // ref idx active l0 and l1
2029             {
2030                 hdr->num_ref_idx_l0_active = GetVLCElement(false) + 1;
2031                 if (BPREDSLICE == hdr->slice_type)
2032                     hdr->num_ref_idx_l1_active = GetVLCElement(false) + 1;
2033             }
2034         }    // ref idx override
2035 
2036         if (hdr->num_ref_idx_l0_active < 0 || hdr->num_ref_idx_l0_active > (int32_t)MAX_NUM_REF_FRAMES ||
2037             hdr->num_ref_idx_l1_active < 0 || hdr->num_ref_idx_l1_active > (int32_t)MAX_NUM_REF_FRAMES)
2038             return UMC_ERR_INVALID_STREAM;
2039 
2040         if (hdr->slice_type != INTRASLICE && hdr->slice_type != S_INTRASLICE)
2041         {
2042             uint32_t reordering_of_pic_nums_idc;
2043             uint32_t reorder_idx;
2044 
2045             // Reference picture list reordering
2046             ref_pic_list_reordering_flag_l0 = Get1Bit();
2047             if (ref_pic_list_reordering_flag_l0)
2048             {
2049                 reorder_idx = 0;
2050                 reordering_of_pic_nums_idc = 0;
2051 
2052                 // Get reorder idc,pic_num pairs until idc==3
2053                 for (;;)
2054                 {
2055                     reordering_of_pic_nums_idc = (uint8_t)GetVLCElement(false);
2056                     if (reordering_of_pic_nums_idc > 5)
2057                       return UMC_ERR_INVALID_STREAM;
2058 
2059                     if (reordering_of_pic_nums_idc == 3)
2060                         break;
2061 
2062                     if (reorder_idx >= MAX_NUM_REF_FRAMES)
2063                     {
2064                         return UMC_ERR_INVALID_STREAM;
2065                     }
2066 
2067                     pReorderInfo_L0->reordering_of_pic_nums_idc[reorder_idx] =
2068                                                 (uint8_t)reordering_of_pic_nums_idc;
2069                     pReorderInfo_L0->reorder_value[reorder_idx]  =
2070                                                         GetVLCElement(false);
2071                   if (reordering_of_pic_nums_idc != 2)
2072                         // abs_diff_pic_num is coded minus 1
2073                         pReorderInfo_L0->reorder_value[reorder_idx]++;
2074                     reorder_idx++;
2075                 }    // while
2076 
2077                 pReorderInfo_L0->num_entries = reorder_idx;
2078             }    // L0 reordering info
2079             else
2080                 pReorderInfo_L0->num_entries = 0;
2081 
2082             if (BPREDSLICE == hdr->slice_type)
2083             {
2084                 ref_pic_list_reordering_flag_l1 = Get1Bit();
2085                 if (ref_pic_list_reordering_flag_l1)
2086                 {
2087                     // Get reorder idc,pic_num pairs until idc==3
2088                     reorder_idx = 0;
2089                     reordering_of_pic_nums_idc = 0;
2090                     for (;;)
2091                     {
2092                         reordering_of_pic_nums_idc = GetVLCElement(false);
2093                         if (reordering_of_pic_nums_idc > 5)
2094                             return UMC_ERR_INVALID_STREAM;
2095 
2096                         if (reordering_of_pic_nums_idc == 3)
2097                             break;
2098 
2099                         if (reorder_idx >= MAX_NUM_REF_FRAMES)
2100                         {
2101                             return UMC_ERR_INVALID_STREAM;
2102                         }
2103 
2104                         pReorderInfo_L1->reordering_of_pic_nums_idc[reorder_idx] =
2105                                                     (uint8_t)reordering_of_pic_nums_idc;
2106                         pReorderInfo_L1->reorder_value[reorder_idx]  =
2107                                                             GetVLCElement(false);
2108                         if (reordering_of_pic_nums_idc != 2)
2109                             // abs_diff_pic_num is coded minus 1
2110                             pReorderInfo_L1->reorder_value[reorder_idx]++;
2111                         reorder_idx++;
2112                     }    // while
2113                     pReorderInfo_L1->num_entries = reorder_idx;
2114                 }    // L1 reordering info
2115                 else
2116                     pReorderInfo_L1->num_entries = 0;
2117             }    // B slice
2118         }    // reordering info
2119 
2120         hdr->luma_log2_weight_denom = 0;
2121         hdr->chroma_log2_weight_denom = 0;
2122 
2123         // prediction weight table
2124         if ( (pps->weighted_pred_flag &&
2125               ((PREDSLICE == hdr->slice_type) || (S_PREDSLICE == hdr->slice_type))) ||
2126              ((pps->weighted_bipred_idc == 1) && (BPREDSLICE == hdr->slice_type)))
2127         {
2128             ps = GetPredWeightTable(hdr, sps, pPredWeight_L0, pPredWeight_L1);
2129             if (ps != UMC_OK)
2130                 return ps;
2131         }
2132 
2133         // dec_ref_pic_marking
2134         pAdaptiveMarkingInfo->num_entries = 0;
2135         pBaseAdaptiveMarkingInfo->num_entries = 0;
2136 
2137         if (hdr->nal_ref_idc)
2138         {
2139             ps = DecRefPicMarking(hdr, pAdaptiveMarkingInfo);
2140             if (ps != UMC_OK)
2141             {
2142                 return ps;
2143             }
2144 
2145             if (hdr->nal_unit_type == NAL_UT_CODED_SLICE_EXTENSION && hdr->nal_ext.svc_extension_flag &&
2146                 spsSvcExt && !spsSvcExt->slice_header_restriction_flag)
2147             {
2148                 hdr->nal_ext.svc.store_ref_base_pic_flag = Get1Bit();
2149                 if ((hdr->nal_ext.svc.use_ref_base_pic_flag ||
2150                     hdr->nal_ext.svc.store_ref_base_pic_flag) &&
2151                     (!hdr->nal_ext.svc.idr_flag))
2152                 {
2153                     ps = DecRefBasePicMarking(pBaseAdaptiveMarkingInfo, hdr->nal_ext.svc.adaptive_ref_base_pic_marking_mode_flag);
2154                     if (ps != UMC_OK)
2155                         return ps;
2156                 }
2157             }
2158         }    // def_ref_pic_marking
2159     }
2160 
2161     if (pps->entropy_coding_mode == 1  &&    // CABAC
2162         (hdr->slice_type != INTRASLICE && hdr->slice_type != S_INTRASLICE))
2163         hdr->cabac_init_idc = GetVLCElement(false);
2164     else
2165         hdr->cabac_init_idc = 0;
2166 
2167     if (hdr->cabac_init_idc > 2)
2168         return UMC_ERR_INVALID_STREAM;
2169 
2170     hdr->slice_qp_delta = GetVLCElement(true);
2171 
2172     if (S_PREDSLICE == hdr->slice_type ||
2173         S_INTRASLICE == hdr->slice_type)
2174     {
2175         if (S_PREDSLICE == hdr->slice_type)
2176             hdr->sp_for_switch_flag = Get1Bit();
2177         hdr->slice_qs_delta = GetVLCElement(true);
2178     }
2179 
2180     if (pps->deblocking_filter_variables_present_flag)
2181     {
2182         // deblock filter flag and offsets
2183         hdr->disable_deblocking_filter_idc = GetVLCElement(false);
2184         if (hdr->disable_deblocking_filter_idc > DEBLOCK_FILTER_ON_NO_SLICE_EDGES)
2185             return UMC_ERR_INVALID_STREAM;
2186 
2187         if (hdr->disable_deblocking_filter_idc != DEBLOCK_FILTER_OFF)
2188         {
2189             hdr->slice_alpha_c0_offset = GetVLCElement(true)<<1;
2190             hdr->slice_beta_offset = GetVLCElement(true)<<1;
2191 
2192             if (hdr->slice_alpha_c0_offset < -12 || hdr->slice_alpha_c0_offset > 12)
2193             {
2194                 return UMC_ERR_INVALID_STREAM;
2195             }
2196 
2197             if (hdr->slice_beta_offset < -12 || hdr->slice_beta_offset > 12)
2198             {
2199                 return UMC_ERR_INVALID_STREAM;
2200             }
2201         }
2202         else
2203         {
2204             // set filter offsets to max values to disable filter
2205             hdr->slice_alpha_c0_offset = (int8_t)(0 - QP_MAX);
2206             hdr->slice_beta_offset = (int8_t)(0 - QP_MAX);
2207         }
2208     }
2209 
2210     if (pps->num_slice_groups > 1 &&
2211         pps->SliceGroupInfo.slice_group_map_type >= 3 &&
2212         pps->SliceGroupInfo.slice_group_map_type <= 5)
2213     {
2214         uint32_t num_bits;    // number of bits used to code slice_group_change_cycle
2215         uint32_t val;
2216         uint32_t pic_size_in_map_units;
2217         uint32_t max_slice_group_change_cycle=0;
2218 
2219         // num_bits is Ceil(log2(picsizeinmapunits/slicegroupchangerate + 1))
2220         pic_size_in_map_units = sps->frame_width_in_mbs * sps->frame_height_in_mbs;
2221             // TBD: change above to support fields
2222 
2223         max_slice_group_change_cycle = pic_size_in_map_units /
2224                         pps->SliceGroupInfo.t2.slice_group_change_rate;
2225         if (pic_size_in_map_units %
2226                         pps->SliceGroupInfo.t2.slice_group_change_rate)
2227             max_slice_group_change_cycle++;
2228 
2229         val = max_slice_group_change_cycle;// + 1;
2230         num_bits = 0;
2231         while (val)
2232         {
2233             num_bits++;
2234             val >>= 1;
2235         }
2236         hdr->slice_group_change_cycle = GetBits(num_bits);
2237         if (hdr->slice_group_change_cycle > max_slice_group_change_cycle)
2238         {
2239             //return UMC_ERR_INVALID_STREAM; don't see any reasons for that
2240         }
2241     }
2242 
2243     CheckBSLeft();
2244     return UMC_OK;
2245 } // GetSliceHeaderPart3()
2246 
DecRefPicMarking(H264SliceHeader * hdr,AdaptiveMarkingInfo * pAdaptiveMarkingInfo)2247 Status H264HeadersBitstream::DecRefPicMarking(H264SliceHeader *hdr,        // slice header read goes here
2248                     AdaptiveMarkingInfo *pAdaptiveMarkingInfo)
2249 {
2250     if (hdr->IdrPicFlag)
2251     {
2252         hdr->no_output_of_prior_pics_flag = Get1Bit();
2253         hdr->long_term_reference_flag = Get1Bit();
2254     }
2255     else
2256     {
2257         return DecRefBasePicMarking(pAdaptiveMarkingInfo, hdr->adaptive_ref_pic_marking_mode_flag);
2258     }
2259 
2260     return UMC_OK;
2261 }
2262 
GetSliceHeaderPart4(H264SliceHeader * hdr,const H264SeqParamSetSVCExtension *)2263 Status H264HeadersBitstream::GetSliceHeaderPart4(H264SliceHeader *hdr,
2264                                           const H264SeqParamSetSVCExtension *)
2265 {
2266     hdr->scan_idx_start = 0;
2267     hdr->scan_idx_end = 15;
2268     return UMC_OK;
2269 }// GetSliceHeaderPart4()
2270 
GetScalingList4x4(H264ScalingList4x4 * scl,uint8_t * def,uint8_t * scl_type)2271 void H264HeadersBitstream::GetScalingList4x4(H264ScalingList4x4 *scl, uint8_t *def, uint8_t *scl_type)
2272 {
2273     uint32_t lastScale = 8;
2274     uint32_t nextScale = 8;
2275     bool DefaultMatrix = false;
2276     int32_t j;
2277 
2278     for (j = 0; j < 16; j++ )
2279     {
2280         if (nextScale != 0)
2281         {
2282             int32_t delta_scale  = GetVLCElement(true);
2283             if (delta_scale < -128 || delta_scale > 127)
2284                 throw h264_exception(UMC_ERR_INVALID_STREAM);
2285             nextScale = ( lastScale + delta_scale + 256 ) & 0xff;
2286             DefaultMatrix = ( j == 0 && nextScale == 0 );
2287         }
2288         scl->ScalingListCoeffs[ mp_scan4x4[0][j] ] = ( nextScale == 0 ) ? (uint8_t)lastScale : (uint8_t)nextScale;
2289         lastScale = scl->ScalingListCoeffs[ mp_scan4x4[0][j] ];
2290     }
2291     if (!DefaultMatrix)
2292     {
2293         *scl_type=SCLREDEFINED;
2294         return;
2295     }
2296     *scl_type= SCLDEFAULT;
2297     FillScalingList4x4(scl,def);
2298     return;
2299 }
2300 
GetScalingList8x8(H264ScalingList8x8 * scl,uint8_t * def,uint8_t * scl_type)2301 void H264HeadersBitstream::GetScalingList8x8(H264ScalingList8x8 *scl, uint8_t *def, uint8_t *scl_type)
2302 {
2303     uint32_t lastScale = 8;
2304     uint32_t nextScale = 8;
2305     bool DefaultMatrix=false;
2306     int32_t j;
2307 
2308     for (j = 0; j < 64; j++ )
2309     {
2310         if (nextScale != 0)
2311         {
2312             int32_t delta_scale  = GetVLCElement(true);
2313             if (delta_scale < -128 || delta_scale > 127)
2314                 throw h264_exception(UMC_ERR_INVALID_STREAM);
2315             nextScale = ( lastScale + delta_scale + 256 ) & 0xff;
2316             DefaultMatrix = ( j == 0 && nextScale == 0 );
2317         }
2318         scl->ScalingListCoeffs[ hp_scan8x8[0][j] ] = ( nextScale == 0 ) ? (uint8_t)lastScale : (uint8_t)nextScale;
2319         lastScale = scl->ScalingListCoeffs[ hp_scan8x8[0][j] ];
2320     }
2321     if (!DefaultMatrix)
2322     {
2323         *scl_type=SCLREDEFINED;
2324         return;
2325     }
2326     *scl_type= SCLDEFAULT;
2327     FillScalingList8x8(scl,def);
2328     return;
2329 
2330 }
2331 
GetNALUnitType(NAL_Unit_Type & nal_unit_type,uint32_t & nal_ref_idc)2332 Status H264HeadersBitstream::GetNALUnitType(NAL_Unit_Type &nal_unit_type, uint32_t &nal_ref_idc)
2333 {
2334     uint32_t code;
2335 
2336     h264GetBits(m_pbs, m_bitOffset, 8, code);
2337 
2338     nal_ref_idc = (uint32_t) ((code & NAL_STORAGE_IDC_BITS)>>5);
2339     nal_unit_type = (NAL_Unit_Type) (code & NAL_UNITTYPE_BITS);
2340     return UMC_OK;
2341 
2342 }
2343 
GetSeqParams(const Headers & headers,int32_t seq_parameter_set_id)2344 const H264SeqParamSet *GetSeqParams(const Headers & headers, int32_t seq_parameter_set_id)
2345 {
2346     if (seq_parameter_set_id == -1)
2347         return 0;
2348 
2349     const H264SeqParamSet *csps = headers.m_SeqParams.GetHeader(seq_parameter_set_id);
2350     if (csps)
2351         return csps;
2352 
2353     csps = headers.m_SeqParamsMvcExt.GetHeader(seq_parameter_set_id);
2354     if (csps)
2355         return csps;
2356 
2357     csps = headers.m_SeqParamsSvcExt.GetHeader(seq_parameter_set_id);
2358     if (csps)
2359         return csps;
2360 
2361     return 0;
2362 }
2363 
ParseSEI(const Headers & headers,H264SEIPayLoad * spl)2364 int32_t H264HeadersBitstream::ParseSEI(const Headers & headers, H264SEIPayLoad *spl)
2365 {
2366     int32_t current_sps = headers.m_SeqParams.GetCurrentID();
2367     return sei_message(headers, current_sps, spl);
2368 }
2369 
sei_message(const Headers & headers,int32_t current_sps,H264SEIPayLoad * spl)2370 int32_t H264HeadersBitstream::sei_message(const Headers & headers, int32_t current_sps, H264SEIPayLoad *spl)
2371 {
2372     uint32_t code;
2373     int32_t payloadType = 0;
2374 
2375     PeakNextBits(m_pbs, m_bitOffset, 8, code);
2376     while (code  ==  0xFF)
2377     {
2378         /* fixed-pattern bit string using 8 bits written equal to 0xFF */
2379         h264GetBits(m_pbs, m_bitOffset, 8, code);
2380         payloadType += 255;
2381         PeakNextBits(m_pbs, m_bitOffset, 8, code);
2382     }
2383 
2384     int32_t last_payload_type_byte;    //uint32_t integer using 8 bits
2385     h264GetBits(m_pbs, m_bitOffset, 8, last_payload_type_byte);
2386 
2387     payloadType += last_payload_type_byte;
2388 
2389     int32_t payloadSize = 0;
2390 
2391     PeakNextBits(m_pbs, m_bitOffset, 8, code);
2392     while( code  ==  0xFF )
2393     {
2394         /* fixed-pattern bit string using 8 bits written equal to 0xFF */
2395         h264GetBits(m_pbs, m_bitOffset, 8, code);
2396         payloadSize += 255;
2397         PeakNextBits(m_pbs, m_bitOffset, 8, code);
2398     }
2399 
2400     int32_t last_payload_size_byte;    //uint32_t integer using 8 bits
2401 
2402     h264GetBits(m_pbs, m_bitOffset, 8, last_payload_size_byte);
2403     payloadSize += last_payload_size_byte;
2404     spl->Reset();
2405     spl->payLoadSize = payloadSize;
2406 
2407     if (payloadType < 0 || payloadType > SEI_RESERVED)
2408         payloadType = SEI_RESERVED;
2409 
2410     spl->payLoadType = (SEI_TYPE)payloadType;
2411 
2412     if (spl->payLoadSize > BytesLeft())
2413     {
2414         throw h264_exception(UMC_ERR_INVALID_STREAM);
2415     }
2416 
2417     uint32_t * pbs;
2418     uint32_t bitOffsetU;
2419     int32_t bitOffset;
2420 
2421     GetState(&pbs, &bitOffsetU);
2422     bitOffset = bitOffsetU;
2423 
2424     CheckBSLeft(spl->payLoadSize);
2425 
2426     spl->isValid = 1;
2427     int32_t ret = sei_payload(headers, current_sps, spl);
2428 
2429     for (uint32_t i = 0; i < spl->payLoadSize; i++)
2430     {
2431         SkipNBits(pbs, bitOffset, 8);
2432     }
2433 
2434     SetState(pbs, bitOffset);
2435     return ret;
2436 }
2437 
sei_payload(const Headers & headers,int32_t current_sps,H264SEIPayLoad * spl)2438 int32_t H264HeadersBitstream::sei_payload(const Headers & headers, int32_t current_sps,H264SEIPayLoad *spl)
2439 {
2440     uint32_t payloadType =spl->payLoadType;
2441     switch( payloadType)
2442     {
2443     case SEI_BUFFERING_PERIOD_TYPE:
2444         buffering_period(headers, current_sps,spl);
2445         break;
2446     case SEI_PIC_TIMING_TYPE:
2447         pic_timing(headers,current_sps,spl);
2448         break;
2449     case SEI_DEC_REF_PIC_MARKING_TYPE:
2450         dec_ref_pic_marking_repetition(headers,current_sps,spl);
2451         break;
2452 
2453     case SEI_USER_DATA_REGISTERED_TYPE:
2454         user_data_registered_itu_t_t35(spl);
2455         break;
2456     case SEI_RECOVERY_POINT_TYPE:
2457         recovery_point(spl);
2458         break;
2459 
2460     case SEI_SCALABILITY_INFO:
2461         scalability_info(spl);
2462         break;
2463     case SEI_SCALABLE_NESTING:
2464     case SEI_USER_DATA_UNREGISTERED_TYPE:
2465     case SEI_PAN_SCAN_RECT_TYPE:
2466     case SEI_FILLER_TYPE:
2467     case SEI_SPARE_PIC_TYPE:
2468     case SEI_SCENE_INFO_TYPE:
2469     case SEI_SUB_SEQ_INFO_TYPE:
2470     case SEI_SUB_SEQ_LAYER_TYPE:
2471     case SEI_SUB_SEQ_TYPE:
2472     case SEI_FULL_FRAME_FREEZE_TYPE:
2473     case SEI_FULL_FRAME_FREEZE_RELEASE_TYPE:
2474     case SEI_FULL_FRAME_SNAPSHOT_TYPE:
2475     case SEI_PROGRESSIVE_REF_SEGMENT_START_TYPE:
2476     case SEI_PROGRESSIVE_REF_SEGMENT_END_TYPE:
2477     case SEI_MOTION_CONSTRAINED_SG_SET_TYPE:
2478     default:
2479         unparsed_sei_message(spl);
2480         break;
2481     }
2482 
2483     return current_sps;
2484 }
2485 
buffering_period(const Headers & headers,int32_t,H264SEIPayLoad * spl)2486 int32_t H264HeadersBitstream::buffering_period(const Headers & headers, int32_t , H264SEIPayLoad *spl)
2487 {
2488     int32_t seq_parameter_set_id = (uint8_t) GetVLCElement(false);
2489     const H264SeqParamSet *csps = GetSeqParams(headers, seq_parameter_set_id);
2490     H264SEIPayLoad::SEIMessages::BufferingPeriod &bps = spl->SEI_messages.buffering_period;
2491 
2492     // touch unreferenced parameters
2493     if (!csps)
2494     {
2495         throw h264_exception(UMC_ERR_INVALID_STREAM);
2496     }
2497 
2498     if (csps->vui.nal_hrd_parameters_present_flag)
2499     {
2500         if (csps->vui.cpb_cnt >= 32)
2501             throw h264_exception(UMC_ERR_INVALID_STREAM);
2502 
2503         for(int32_t i=0; i < csps->vui.cpb_cnt; i++)
2504         {
2505             bps.initial_cpb_removal_delay[0][i] = GetBits(csps->vui.initial_cpb_removal_delay_length);
2506             bps.initial_cpb_removal_delay_offset[0][i] = GetBits(csps->vui.initial_cpb_removal_delay_length);
2507         }
2508     }
2509 
2510     if (csps->vui.vcl_hrd_parameters_present_flag)
2511     {
2512         if (csps->vui.cpb_cnt >= 32)
2513             throw h264_exception(UMC_ERR_INVALID_STREAM);
2514 
2515         for(int32_t i=0; i < csps->vui.cpb_cnt; i++)
2516         {
2517             bps.initial_cpb_removal_delay[1][i] = GetBits(csps->vui.cpb_removal_delay_length);
2518             bps.initial_cpb_removal_delay_offset[1][i] = GetBits(csps->vui.cpb_removal_delay_length);
2519         }
2520     }
2521 
2522     AlignPointerRight();
2523     return seq_parameter_set_id;
2524 }
2525 
pic_timing(const Headers & headers,int32_t current_sps,H264SEIPayLoad * spl)2526 int32_t H264HeadersBitstream::pic_timing(const Headers & headers, int32_t current_sps, H264SEIPayLoad *spl)
2527 {
2528     if (current_sps == -1)
2529         throw h264_exception(UMC_ERR_INVALID_STREAM);
2530 
2531     const uint8_t NumClockTS[]={1,1,1,2,2,3,3,2,3};
2532     const H264SeqParamSet *csps = GetSeqParams(headers, current_sps);
2533     H264SEIPayLoad::SEIMessages::PicTiming &pts = spl->SEI_messages.pic_timing;
2534 
2535     if (!csps)
2536         throw h264_exception(UMC_ERR_INVALID_STREAM);
2537 
2538     if (csps->vui.nal_hrd_parameters_present_flag || csps->vui.vcl_hrd_parameters_present_flag)
2539     {
2540         pts.cbp_removal_delay = GetBits(csps->vui.cpb_removal_delay_length);
2541         pts.dpb_output_delay = GetBits(csps->vui.dpb_output_delay_length);
2542     }
2543     else
2544     {
2545         pts.cbp_removal_delay = INVALID_DPB_OUTPUT_DELAY;
2546         pts.dpb_output_delay = INVALID_DPB_OUTPUT_DELAY;
2547     }
2548 
2549     if (csps->vui.pic_struct_present_flag)
2550     {
2551         uint8_t picStruct = (uint8_t)GetBits(4);
2552 
2553         if (picStruct > 8)
2554             return UMC_ERR_INVALID_STREAM;
2555 
2556         pts.pic_struct = (DisplayPictureStruct)picStruct;
2557 
2558         for (int32_t i = 0; i < NumClockTS[pts.pic_struct]; i++)
2559         {
2560             pts.clock_timestamp_flag[i] = (uint8_t)Get1Bit();
2561             if (pts.clock_timestamp_flag[i])
2562             {
2563                 pts.clock_timestamps[i].ct_type = (uint8_t)GetBits(2);
2564                 pts.clock_timestamps[i].nunit_field_based_flag = (uint8_t)Get1Bit();
2565                 pts.clock_timestamps[i].counting_type = (uint8_t)GetBits(5);
2566                 pts.clock_timestamps[i].full_timestamp_flag = (uint8_t)Get1Bit();
2567                 pts.clock_timestamps[i].discontinuity_flag = (uint8_t)Get1Bit();
2568                 pts.clock_timestamps[i].cnt_dropped_flag = (uint8_t)Get1Bit();
2569                 pts.clock_timestamps[i].n_frames = (uint8_t)GetBits(8);
2570 
2571                 if (pts.clock_timestamps[i].full_timestamp_flag)
2572                 {
2573                     pts.clock_timestamps[i].seconds_value = (uint8_t)GetBits(6);
2574                     pts.clock_timestamps[i].minutes_value = (uint8_t)GetBits(6);
2575                     pts.clock_timestamps[i].hours_value = (uint8_t)GetBits(5);
2576                 }
2577                 else
2578                 {
2579                     if (Get1Bit())
2580                     {
2581                         pts.clock_timestamps[i].seconds_value = (uint8_t)GetBits(6);
2582                         if (Get1Bit())
2583                         {
2584                             pts.clock_timestamps[i].minutes_value = (uint8_t)GetBits(6);
2585                             if (Get1Bit())
2586                             {
2587                                 pts.clock_timestamps[i].hours_value = (uint8_t)GetBits(5);
2588                             }
2589                         }
2590                     }
2591                 }
2592 
2593                 if (csps->vui.time_offset_length > 0)
2594                     pts.clock_timestamps[i].time_offset = (uint8_t)GetBits(csps->vui.time_offset_length);
2595             }
2596         }
2597     }
2598 
2599     AlignPointerRight();
2600     return current_sps;
2601 }
2602 
user_data_registered_itu_t_t35(H264SEIPayLoad * spl)2603 void H264HeadersBitstream::user_data_registered_itu_t_t35(H264SEIPayLoad *spl)
2604 {
2605     H264SEIPayLoad::SEIMessages::UserDataRegistered * user_data = &(spl->SEI_messages.user_data_registered);
2606     uint32_t code;
2607     h264GetBits(m_pbs, m_bitOffset, 8, code);
2608     user_data->itu_t_t35_country_code = (uint8_t)code;
2609 
2610     uint32_t i = 1;
2611 
2612     user_data->itu_t_t35_country_code_extension_byte = 0;
2613     if (user_data->itu_t_t35_country_code == 0xff)
2614     {
2615         h264GetBits(m_pbs, m_bitOffset, 8, code);
2616         user_data->itu_t_t35_country_code_extension_byte = (uint8_t)code;
2617         i++;
2618     }
2619 
2620     spl->user_data.resize(spl->payLoadSize + 1);
2621 
2622     for(int32_t k = 0; i < spl->payLoadSize; i++, k++)
2623     {
2624         h264GetBits(m_pbs, m_bitOffset, 8, code);
2625         spl->user_data[k] = (uint8_t) code;
2626     }
2627 }
2628 
recovery_point(H264SEIPayLoad * spl)2629 void H264HeadersBitstream::recovery_point(H264SEIPayLoad *spl)
2630 {
2631     H264SEIPayLoad::SEIMessages::RecoveryPoint * recPoint = &(spl->SEI_messages.recovery_point);
2632 
2633     recPoint->recovery_frame_cnt = (uint8_t)GetVLCElement(false);
2634 
2635     recPoint->exact_match_flag = (uint8_t)Get1Bit();
2636     recPoint->broken_link_flag = (uint8_t)Get1Bit();
2637     recPoint->changing_slice_group_idc = (uint8_t)GetBits(2);
2638 
2639     if (recPoint->changing_slice_group_idc > 2)
2640     {
2641         spl->isValid = 0;
2642     }
2643 }
2644 
dec_ref_pic_marking_repetition(const Headers & headers,int32_t current_sps,H264SEIPayLoad * spl)2645 int32_t H264HeadersBitstream::dec_ref_pic_marking_repetition(const Headers & headers, int32_t current_sps, H264SEIPayLoad *spl)
2646 {
2647     if (current_sps == -1)
2648         throw h264_exception(UMC_ERR_INVALID_STREAM);
2649 
2650     const H264SeqParamSet *csps = GetSeqParams(headers, current_sps);
2651     if (!csps)
2652         throw h264_exception(UMC_ERR_INVALID_STREAM);
2653 
2654     spl->SEI_messages.dec_ref_pic_marking_repetition.original_idr_flag = (uint8_t)Get1Bit();
2655     spl->SEI_messages.dec_ref_pic_marking_repetition.original_frame_num = (uint8_t)GetVLCElement(false);
2656 
2657     if (!csps->frame_mbs_only_flag)
2658     {
2659         spl->SEI_messages.dec_ref_pic_marking_repetition.original_field_pic_flag = (uint8_t)Get1Bit();
2660 
2661         if (spl->SEI_messages.dec_ref_pic_marking_repetition.original_field_pic_flag)
2662         {
2663             spl->SEI_messages.dec_ref_pic_marking_repetition.original_bottom_field_flag = (uint8_t)Get1Bit();
2664         }
2665     }
2666 
2667     H264SliceHeader hdr;
2668     memset(&hdr, 0, sizeof(H264SliceHeader));
2669 
2670     hdr.IdrPicFlag = spl->SEI_messages.dec_ref_pic_marking_repetition.original_idr_flag;
2671 
2672     Status sts = DecRefPicMarking(&hdr, &spl->SEI_messages.dec_ref_pic_marking_repetition.adaptiveMarkingInfo);
2673     if (sts != UMC_OK)
2674         throw h264_exception(UMC_ERR_INVALID_STREAM);
2675 
2676     spl->SEI_messages.dec_ref_pic_marking_repetition.long_term_reference_flag = hdr.long_term_reference_flag;
2677     return current_sps;
2678 }
2679 
unparsed_sei_message(H264SEIPayLoad * spl)2680 void H264HeadersBitstream::unparsed_sei_message(H264SEIPayLoad *spl)
2681 {
2682     for(uint32_t i = 0; i < spl->payLoadSize; i++)
2683         SkipNBits(m_pbs, m_bitOffset, 8)
2684     AlignPointerRight();
2685 }
2686 
2687 #ifdef _MSVC_LANG
2688 #pragma warning(disable : 4189)
2689 #endif
scalability_info(H264SEIPayLoad * spl)2690 void H264HeadersBitstream::scalability_info(H264SEIPayLoad *spl)
2691 {
2692     spl->SEI_messages.scalability_info.temporal_id_nesting_flag = (uint8_t)Get1Bit();
2693     spl->SEI_messages.scalability_info.priority_layer_info_present_flag = (uint8_t)Get1Bit();
2694     spl->SEI_messages.scalability_info.priority_id_setting_flag = (uint8_t)Get1Bit();
2695 
2696     spl->SEI_messages.scalability_info.num_layers = GetVLCElement(false) + 1;
2697 
2698     if (spl->SEI_messages.scalability_info.num_layers > 1024)
2699         throw h264_exception(UMC_ERR_INVALID_STREAM);
2700 
2701     spl->user_data.resize(sizeof(scalability_layer_info) * spl->SEI_messages.scalability_info.num_layers);
2702     scalability_layer_info * layers = (scalability_layer_info *) (&spl->user_data[0]);
2703 
2704     for (uint32_t i = 0; i < spl->SEI_messages.scalability_info.num_layers; i++)
2705     {
2706         layers[i].layer_id = GetVLCElement(false);
2707         layers[i].priority_id = (uint8_t)GetBits(6);
2708         layers[i].discardable_flag = (uint8_t)Get1Bit();
2709         layers[i].dependency_id = (uint8_t)GetBits(3);
2710         layers[i].quality_id = (uint8_t)GetBits(4);
2711         layers[i].temporal_id = (uint8_t)GetBits(3);
2712 
2713         uint8_t   sub_pic_layer_flag = (uint8_t)Get1Bit();  // Need to check
2714         uint8_t   sub_region_layer_flag = (uint8_t)Get1Bit(); // Need to check
2715 
2716         //if (sub_pic_layer_flag && !sub_region_layer_flag)
2717           //  throw h264_exception(UMC_ERR_INVALID_STREAM);
2718 
2719         uint8_t   iroi_division_info_present_flag = (uint8_t)Get1Bit(); // Need to check
2720 
2721         //if (sub_pic_layer_flag && iroi_division_info_present_flag)
2722           //  throw h264_exception(UMC_ERR_INVALID_STREAM);
2723 
2724         uint8_t   profile_level_info_present_flag = (uint8_t)Get1Bit();
2725         uint8_t   bitrate_info_present_flag = (uint8_t)Get1Bit();
2726         uint8_t   frm_rate_info_present_flag = (uint8_t)Get1Bit();
2727         uint8_t   frm_size_info_present_flag = (uint8_t)Get1Bit();
2728         uint8_t   layer_dependency_info_present_flag = (uint8_t)Get1Bit();
2729         uint8_t   parameter_sets_info_present_flag = (uint8_t)Get1Bit();
2730         uint8_t   bitstream_restriction_info_present_flag = (uint8_t)Get1Bit();
2731         /*uint8_t   exact_inter_layer_pred_flag = (uint8_t)*/Get1Bit();
2732 
2733         if (sub_pic_layer_flag || iroi_division_info_present_flag)
2734         {
2735             /*uint8_t   exact_sample_value_match_flag = (uint8_t)*/Get1Bit();
2736         }
2737 
2738         uint8_t   layer_conversion_flag = (uint8_t)Get1Bit();
2739         /*uint8_t   layer_output_flag = (uint8_t)*/Get1Bit();
2740 
2741         if (profile_level_info_present_flag)
2742         {
2743             /*uint32_t   layer_profile_level_idc = (uint8_t)*/GetBits(24);
2744         }
2745 
2746         if (bitrate_info_present_flag)
2747         {
2748             /*uint32_t  avg_bitrate = */GetBits(16);
2749             /*uint32_t  max_bitrate_layer = */GetBits(16);
2750             /*uint32_t  max_bitrate_layer_representation = */GetBits(16);
2751             /*uint32_t  max_bitrate_calc_window = */GetBits(16);
2752         }
2753 
2754         if (frm_rate_info_present_flag)
2755         {
2756             layers[i].constant_frm_rate_idc = GetBits(2);
2757             layers[i].avg_frm_rate = GetBits(16);
2758         }
2759 
2760         if (frm_size_info_present_flag || iroi_division_info_present_flag)
2761         {
2762             layers[i].frm_width_in_mbs = GetVLCElement(false) + 1;
2763             layers[i].frm_height_in_mbs = GetVLCElement(false) + 1;
2764         }
2765 
2766         if (sub_region_layer_flag)
2767         {
2768             /*uint32_t base_region_layer_id = */GetVLCElement(false);
2769             uint8_t dynamic_rect_flag = (uint8_t)Get1Bit();
2770             if (!dynamic_rect_flag)
2771             {
2772                 /*uint32_t horizontal_offset = */GetBits(16);
2773                 /*uint32_t vertical_offset = */GetBits(16);
2774                 /*uint32_t region_width = */GetBits(16);
2775                 /*uint32_t region_height = */GetBits(16);
2776             }
2777         }
2778 
2779         if(sub_pic_layer_flag)
2780         {
2781             /*uint32_t roi_id = */GetVLCElement(false);
2782         }
2783 
2784         if (iroi_division_info_present_flag)
2785         {
2786             uint8_t iroi_grid_flag = (uint8_t)Get1Bit();
2787             if (iroi_grid_flag)
2788             {
2789                 /*uint32_t grid_width_in_mbs_minus1 = */GetVLCElement(false);
2790                 /*uint32_t grid_height_in_mbs_minus1 = */GetVLCElement(false);
2791             } else {
2792                 int32_t num_rois_minus1 = GetVLCElement(false);
2793                 int32_t FrmSizeInMbs = layers[i].frm_height_in_mbs * layers[i].frm_width_in_mbs;
2794 
2795                 if (num_rois_minus1 < 0 || num_rois_minus1 > FrmSizeInMbs)
2796                     throw h264_exception(UMC_ERR_INVALID_STREAM);
2797                 for (int32_t j = 0; j <= num_rois_minus1; j++)
2798                 {
2799                     /*uint32_t first_mb_in_roi = */GetVLCElement(false);
2800                     /*uint32_t roi_width_in_mbs_minus1 = */GetVLCElement(false);
2801                     /*uint32_t roi_height_in_mbs_minus1 = */GetVLCElement(false);
2802                 }
2803             }
2804         }
2805 
2806         if (layer_dependency_info_present_flag)
2807         {
2808             layers[i].num_directly_dependent_layers = GetVLCElement(false);
2809 
2810             if (layers[i].num_directly_dependent_layers > 255)
2811                 throw h264_exception(UMC_ERR_INVALID_STREAM);
2812 
2813             for(uint32_t j = 0; j < layers[i].num_directly_dependent_layers; j++)
2814             {
2815                 layers[i].directly_dependent_layer_id_delta_minus1[j] = GetVLCElement(false);
2816             }
2817         }
2818         else
2819         {
2820             layers[i].layer_dependency_info_src_layer_id_delta = GetVLCElement(false);
2821         }
2822 
2823         if (parameter_sets_info_present_flag)
2824         {
2825             int32_t num_seq_parameter_set_minus1 = GetVLCElement(false);
2826             if (num_seq_parameter_set_minus1 < 0 || num_seq_parameter_set_minus1 > 32)
2827                 throw h264_exception(UMC_ERR_INVALID_STREAM);
2828 
2829             for(int32_t j = 0; j <= num_seq_parameter_set_minus1; j++ )
2830                 /*uint32_t seq_parameter_set_id_delta = */GetVLCElement(false);
2831 
2832             int32_t num_subset_seq_parameter_set_minus1 = GetVLCElement(false);
2833             if (num_subset_seq_parameter_set_minus1 < 0 || num_subset_seq_parameter_set_minus1 > 32)
2834                 throw h264_exception(UMC_ERR_INVALID_STREAM);
2835 
2836             for(int32_t j = 0; j <= num_subset_seq_parameter_set_minus1; j++ )
2837                 /*uint32_t subset_seq_parameter_set_id_delta = */GetVLCElement(false);
2838 
2839             int32_t num_pic_parameter_set_minus1 = GetVLCElement(false);
2840             if (num_pic_parameter_set_minus1 < 0 || num_pic_parameter_set_minus1 > 255)
2841                 throw h264_exception(UMC_ERR_INVALID_STREAM);
2842 
2843             for(int32_t j = 0; j <= num_pic_parameter_set_minus1; j++)
2844                 /*uint32_t pic_parameter_set_id_delta = */GetVLCElement(false);
2845         }
2846         else
2847         {
2848             /*uint32_t parameter_sets_info_src_layer_id_delta = */GetVLCElement(false);
2849         }
2850 
2851         if (bitstream_restriction_info_present_flag)
2852         {
2853             /*uint8_t motion_vectors_over_pic_boundaries_flag = (uint8_t)*/Get1Bit();
2854             /*uint32_t max_bytes_per_pic_denom = */GetVLCElement(false);
2855             /*uint32_t max_bits_per_mb_denom = */GetVLCElement(false);
2856             /*uint32_t log2_max_mv_length_horizontal = */GetVLCElement(false);
2857             /*uint32_t log2_max_mv_length_vertical = */GetVLCElement(false);
2858             /*uint32_t num_reorder_frames = */GetVLCElement(false);
2859             /*uint32_t max_dec_frame_buffering = */GetVLCElement(false);
2860         }
2861 
2862         if (layer_conversion_flag)
2863         {
2864             /*uint32_t conversion_type_idc = */GetVLCElement(false);
2865             for(uint32_t j = 0; j < 2; j++)
2866             {
2867                 uint8_t rewriting_info_flag = (uint8_t)Get1Bit();
2868                 if (rewriting_info_flag)
2869                 {
2870                     /*uint32_t rewriting_profile_level_idc = */GetBits(24);
2871                     /*uint32_t rewriting_avg_bitrate = */GetBits(16);
2872                     /*uint32_t rewriting_max_bitrate = */GetBits(16);
2873                 }
2874             }
2875         }
2876     } // for 0..num_layers
2877 
2878     if (spl->SEI_messages.scalability_info.priority_layer_info_present_flag)
2879     {
2880         uint32_t pr_num_dId_minus1 = GetVLCElement(false);
2881         for(uint32_t i = 0; i <= pr_num_dId_minus1; i++)
2882         {
2883             /*uint32_t pr_dependency_id = */GetBits(3);
2884             int32_t pr_num_minus1 = GetVLCElement(false);
2885 
2886             if (pr_num_minus1 < 0 || pr_num_minus1 > 63)
2887                 throw h264_exception(UMC_ERR_INVALID_STREAM);
2888 
2889             for(int32_t j = 0; j <= pr_num_minus1; j++)
2890             {
2891                 /*uint32_t pr_id = */GetVLCElement(false);
2892                 /*uint32_t pr_profile_level_idc = */GetBits(24);
2893                 /*uint32_t pr_avg_bitrate = */GetBits(16);
2894                 /*uint32_t pr_max_bitrate = */GetBits(16);
2895             }
2896         }
2897     }
2898 
2899     if (spl->SEI_messages.scalability_info.priority_id_setting_flag)
2900     {
2901         std::vector<char> priority_id_setting_uri; // it is string
2902         uint32_t PriorityIdSettingUriIdx = 0;
2903         do
2904         {
2905             priority_id_setting_uri.push_back(1);
2906             priority_id_setting_uri[PriorityIdSettingUriIdx] = (uint8_t)GetBits(8);
2907         } while (priority_id_setting_uri[PriorityIdSettingUriIdx++] != 0);
2908     }
2909 }
2910 
2911 #ifdef _MSVC_LANG
2912 #pragma warning(default : 4189)
2913 #endif
SetDefaultScalingLists(H264SeqParamSet * sps)2914 void SetDefaultScalingLists(H264SeqParamSet * sps)
2915 {
2916     int32_t i;
2917 
2918     for (i = 0; i < 6; i += 1)
2919     {
2920         FillFlatScalingList4x4(&sps->ScalingLists4x4[i]);
2921     }
2922     for (i = 0; i < 2; i += 1)
2923     {
2924         FillFlatScalingList8x8(&sps->ScalingLists8x8[i]);
2925     }
2926 }
2927 
2928 } // namespace UMC
2929 
2930 
2931 #endif // MFX_ENABLE_H264_VIDEO_DECODE
2932