• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2 *
3 * Copyright (C) 2012 Ittiam Systems Pvt Ltd, Bangalore
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18 /**
19 *******************************************************************************
20 * @file
21 *  ihevcd_parse_headers.c
22 *
23 * @brief
24 *  Contains functions for parsing headers
25 *
26 * @author
27 *  Harish
28 *
29 * @par List of Functions:
30 *
31 * @remarks
32 *  None
33 *
34 *******************************************************************************
35 */
36 
37 /*****************************************************************************/
38 /* File Includes                                                             */
39 /*****************************************************************************/
40 #include <stdio.h>
41 #include <stddef.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <assert.h>
45 #include <limits.h>
46 #include <stdint.h>
47 
48 #include "ihevc_typedefs.h"
49 #include "iv.h"
50 #include "ivd.h"
51 #include "ihevcd_cxa.h"
52 
53 #include "ihevc_defs.h"
54 #include "ihevc_debug.h"
55 #include "ihevc_defs.h"
56 #include "ihevc_structs.h"
57 #include "ihevc_buf_mgr.h"
58 #include "ihevc_dpb_mgr.h"
59 #include "ihevc_macros.h"
60 #include "ihevc_platform_macros.h"
61 #include "ihevc_cabac_tables.h"
62 #include "ihevc_common_tables.h"
63 #include "ihevc_quant_tables.h"
64 
65 #include "ihevcd_trace.h"
66 #include "ihevcd_defs.h"
67 #include "ihevcd_function_selector.h"
68 #include "ihevcd_structs.h"
69 #include "ihevcd_error.h"
70 #include "ihevcd_debug.h"
71 #include "ihevcd_nal.h"
72 #include "ihevcd_bitstream.h"
73 #include "ihevcd_parse_headers.h"
74 #include "ihevcd_ref_list.h"
75 
76 #define COPY_DEFAULT_SCALING_LIST(pi2_scaling_mat)                                                                                      \
77 {                                                                                                                                       \
78     WORD32 scaling_mat_offset[]={0, 16, 32, 48, 64, 80, 96, 160, 224, 288, 352, 416, 480, 736, 992, 1248, 1504, 1760, 2016, 3040};      \
79                                                                                                                                         \
80     /* scaling matrix for 4x4 */                                                                                                        \
81     memcpy(pi2_scaling_mat, gi2_flat_scale_mat_32x32, 6*16*sizeof(WORD16));                                                             \
82 /* scaling matrix for 8x8 */                                                                                                            \
83     memcpy(pi2_scaling_mat + scaling_mat_offset[6], gi2_intra_default_scale_mat_8x8, 64*sizeof(WORD16));                                \
84     memcpy(pi2_scaling_mat + scaling_mat_offset[7], gi2_intra_default_scale_mat_8x8, 64*sizeof(WORD16));                                \
85     memcpy(pi2_scaling_mat + scaling_mat_offset[8], gi2_intra_default_scale_mat_8x8, 64*sizeof(WORD16));                                \
86     memcpy(pi2_scaling_mat + scaling_mat_offset[9], gi2_inter_default_scale_mat_8x8, 64*sizeof(WORD16));                                \
87     memcpy(pi2_scaling_mat + scaling_mat_offset[10], gi2_inter_default_scale_mat_8x8, 64*sizeof(WORD16));                               \
88     memcpy(pi2_scaling_mat + scaling_mat_offset[11], gi2_inter_default_scale_mat_8x8, 64*sizeof(WORD16));                               \
89     /* scaling matrix for 16x16 */                                                                                                      \
90     memcpy(pi2_scaling_mat + scaling_mat_offset[12], gi2_intra_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
91     memcpy(pi2_scaling_mat + scaling_mat_offset[13], gi2_intra_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
92     memcpy(pi2_scaling_mat + scaling_mat_offset[14], gi2_intra_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
93     memcpy(pi2_scaling_mat + scaling_mat_offset[15], gi2_inter_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
94     memcpy(pi2_scaling_mat + scaling_mat_offset[16], gi2_inter_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
95     memcpy(pi2_scaling_mat + scaling_mat_offset[17], gi2_inter_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
96     /* scaling matrix for 32x32 */                                                                                                      \
97     memcpy(pi2_scaling_mat + scaling_mat_offset[18], gi2_intra_default_scale_mat_32x32, 1024*sizeof(WORD16));                           \
98     memcpy(pi2_scaling_mat + scaling_mat_offset[19], gi2_inter_default_scale_mat_32x32, 1024*sizeof(WORD16));                           \
99 }
100 
101 #define COPY_FLAT_SCALING_LIST(pi2_scaling_mat)                                                                                         \
102 {                                                                                                                                       \
103     WORD32 scaling_mat_offset[]={0, 16, 32, 48, 64, 80, 96, 160, 224, 288, 352, 416, 480, 736, 992, 1248, 1504, 1760, 2016, 3040};      \
104                                                                                                                                         \
105     /* scaling matrix for 4x4 */                                                                                                        \
106     memcpy(pi2_scaling_mat, gi2_flat_scale_mat_32x32, 6*16*sizeof(WORD16));                                                             \
107     /* scaling matrix for 8x8 */                                                                                                        \
108     memcpy(pi2_scaling_mat + scaling_mat_offset[6], gi2_flat_scale_mat_32x32, 6*64*sizeof(WORD16));                                     \
109     /* scaling matrix for 16x16 */                                                                                                      \
110     memcpy(pi2_scaling_mat + scaling_mat_offset[12], gi2_flat_scale_mat_32x32, 3*256*sizeof(WORD16));                                   \
111     memcpy(pi2_scaling_mat + scaling_mat_offset[15], gi2_flat_scale_mat_32x32, 3*256*sizeof(WORD16));                                   \
112     /* scaling matrix for 32x32 */                                                                                                      \
113     memcpy(pi2_scaling_mat + scaling_mat_offset[18], gi2_flat_scale_mat_32x32, 1024*sizeof(WORD16));                                    \
114     memcpy(pi2_scaling_mat + scaling_mat_offset[19], gi2_flat_scale_mat_32x32, 1024*sizeof(WORD16));                                    \
115 }
116 
117 /* Function declarations */
118 
119 /**
120 *******************************************************************************
121 *
122 * @brief
123 *  Parses Prediction weight table syntax
124 *
125 * @par Description:
126 *  Parse Prediction weight table syntax as per Section: 7.3.8.4
127 *
128 * @param[in] ps_bitstrm
129 *  Pointer to bitstream context
130 *
131 * @param[in] ps_sps
132 *  Current SPS
133 *
134 * @param[in] ps_pps
135 *  Current PPS
136 *
137 * @param[in] ps_slice_hdr
138 *  Current Slice header
139 *
140 * @returns  Error code from IHEVCD_ERROR_T
141 *
142 * @remarks
143 *
144 *
145 *******************************************************************************
146 */
147 
ihevcd_parse_pred_wt_ofst(bitstrm_t * ps_bitstrm,sps_t * ps_sps,pps_t * ps_pps,slice_header_t * ps_slice_hdr)148 WORD32 ihevcd_parse_pred_wt_ofst(bitstrm_t *ps_bitstrm,
149                                  sps_t *ps_sps,
150                                  pps_t *ps_pps,
151                                  slice_header_t *ps_slice_hdr)
152 {
153     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
154     WORD32 value;
155     UWORD32 u4_value;
156     WORD32 i;
157 
158     pred_wt_ofst_t *ps_wt_ofst = &ps_slice_hdr->s_wt_ofst;
159     UNUSED(ps_pps);
160 
161     UEV_PARSE("luma_log2_weight_denom", u4_value, ps_bitstrm);
162     if(u4_value > 7)
163     {
164         return IHEVCD_INVALID_PARAMETER;
165     }
166     ps_wt_ofst->i1_luma_log2_weight_denom = u4_value;
167 
168     if(ps_sps->i1_chroma_format_idc != 0)
169     {
170         SEV_PARSE("delta_chroma_log2_weight_denom", value, ps_bitstrm);
171         if(((ps_wt_ofst->i1_luma_log2_weight_denom + value) < 0) ||
172                 ((ps_wt_ofst->i1_luma_log2_weight_denom + value) > 7))
173         {
174             return IHEVCD_INVALID_PARAMETER;
175         }
176         ps_wt_ofst->i1_chroma_log2_weight_denom = ps_wt_ofst->i1_luma_log2_weight_denom + value;
177     }
178 
179     for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
180     {
181         BITS_PARSE("luma_weight_l0_flag[ i ]", value, ps_bitstrm, 1);
182         ps_wt_ofst->i1_luma_weight_l0_flag[i] = value;
183     }
184 
185 
186 
187     if(ps_sps->i1_chroma_format_idc != 0)
188     {
189         for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
190         {
191             BITS_PARSE("chroma_weight_l0_flag[ i ]", value, ps_bitstrm, 1);
192             ps_wt_ofst->i1_chroma_weight_l0_flag[i] = value;
193         }
194     }
195     else
196     {
197         for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
198         {
199             ps_wt_ofst->i1_chroma_weight_l0_flag[i] = 0;
200         }
201     }
202 
203 
204     for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
205     {
206         if(ps_wt_ofst->i1_luma_weight_l0_flag[i])
207         {
208             SEV_PARSE("delta_luma_weight_l0[ i ]", value, ps_bitstrm);
209             if( value < -128 || value > 127 )
210             {
211                 return IHEVCD_INVALID_PARAMETER;
212             }
213 
214             ps_wt_ofst->i2_luma_weight_l0[i] = (1 << ps_wt_ofst->i1_luma_log2_weight_denom) + value;
215 
216             SEV_PARSE("luma_offset_l0[ i ]", value, ps_bitstrm);
217             if( value < -128 || value > 127 )
218             {
219                 return IHEVCD_INVALID_PARAMETER;
220             }
221             ps_wt_ofst->i2_luma_offset_l0[i] = value;
222 
223         }
224         else
225         {
226             ps_wt_ofst->i2_luma_weight_l0[i] = (1 << ps_wt_ofst->i1_luma_log2_weight_denom);
227             ps_wt_ofst->i2_luma_offset_l0[i] = 0;
228         }
229         if(ps_wt_ofst->i1_chroma_weight_l0_flag[i])
230         {
231             WORD32 ofst;
232             WORD32 shift = (1 << (BIT_DEPTH_CHROMA - 1));
233             SEV_PARSE("delta_chroma_weight_l0[ i ][ j ]", value, ps_bitstrm);
234             if(value < -128 || value > 127)
235             {
236                 return IHEVCD_INVALID_PARAMETER;
237             }
238             ps_wt_ofst->i2_chroma_weight_l0_cb[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom) + value;
239 
240 
241             SEV_PARSE("delta_chroma_offset_l0[ i ][ j ]", value, ps_bitstrm);
242             if( value < -512 || value > 511 )
243             {
244                 return IHEVCD_INVALID_PARAMETER;
245             }
246             ofst = ((shift * ps_wt_ofst->i2_chroma_weight_l0_cb[i]) >> ps_wt_ofst->i1_chroma_log2_weight_denom);
247             ofst = value - ofst + shift;
248 
249             ps_wt_ofst->i2_chroma_offset_l0_cb[i] = CLIP_S8(ofst);
250 
251             SEV_PARSE("delta_chroma_weight_l0[ i ][ j ]", value, ps_bitstrm);
252             if(value < -128 || value > 127)
253             {
254                 return IHEVCD_INVALID_PARAMETER;
255             }
256             ps_wt_ofst->i2_chroma_weight_l0_cr[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom) + value;
257 
258 
259             SEV_PARSE("delta_chroma_offset_l0[ i ][ j ]", value, ps_bitstrm);
260             if( value < -512 || value > 511 )
261             {
262                 return IHEVCD_INVALID_PARAMETER;
263             }
264             ofst = ((shift * ps_wt_ofst->i2_chroma_weight_l0_cr[i]) >> ps_wt_ofst->i1_chroma_log2_weight_denom);
265             ofst = value - ofst + shift;
266 
267             ps_wt_ofst->i2_chroma_offset_l0_cr[i] = CLIP_S8(ofst);
268 
269         }
270         else
271         {
272             ps_wt_ofst->i2_chroma_weight_l0_cb[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom);
273             ps_wt_ofst->i2_chroma_weight_l0_cr[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom);
274 
275             ps_wt_ofst->i2_chroma_offset_l0_cb[i] = 0;
276             ps_wt_ofst->i2_chroma_offset_l0_cr[i] = 0;
277         }
278     }
279     if(BSLICE == ps_slice_hdr->i1_slice_type)
280     {
281         for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
282         {
283             BITS_PARSE("luma_weight_l1_flag[ i ]", value, ps_bitstrm, 1);
284             ps_wt_ofst->i1_luma_weight_l1_flag[i] = value;
285         }
286 
287         if(ps_sps->i1_chroma_format_idc != 0)
288         {
289             for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
290             {
291                 BITS_PARSE("chroma_weight_l1_flag[ i ]", value, ps_bitstrm, 1);
292                 ps_wt_ofst->i1_chroma_weight_l1_flag[i] = value;
293             }
294         }
295         else
296         {
297             for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
298             {
299                 ps_wt_ofst->i1_chroma_weight_l1_flag[i] = 0;
300             }
301         }
302 
303         for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
304         {
305             if(ps_wt_ofst->i1_luma_weight_l1_flag[i])
306             {
307                 SEV_PARSE("delta_luma_weight_l1[ i ]", value, ps_bitstrm);
308                 if( value < -128 || value > 127 )
309                 {
310                     return IHEVCD_INVALID_PARAMETER;
311                 }
312 
313                 ps_wt_ofst->i2_luma_weight_l1[i] = (1 << ps_wt_ofst->i1_luma_log2_weight_denom) + value;
314 
315                 SEV_PARSE("luma_offset_l1[ i ]", value, ps_bitstrm);
316                 if( value < -128 || value > 127 )
317                 {
318                     return IHEVCD_INVALID_PARAMETER;
319                 }
320                 ps_wt_ofst->i2_luma_offset_l1[i] = value;
321 
322             }
323             else
324             {
325                 ps_wt_ofst->i2_luma_weight_l1[i] = (1 << ps_wt_ofst->i1_luma_log2_weight_denom);
326                 ps_wt_ofst->i2_luma_offset_l1[i] = 0;
327             }
328 
329             if(ps_wt_ofst->i1_chroma_weight_l1_flag[i])
330             {
331                 WORD32 ofst;
332                 WORD32 shift = (1 << (BIT_DEPTH_CHROMA - 1));
333                 SEV_PARSE("delta_chroma_weight_l1[ i ][ j ]", value, ps_bitstrm);
334                 if(value < -128 || value > 127)
335                 {
336                     return IHEVCD_INVALID_PARAMETER;
337                 }
338                 ps_wt_ofst->i2_chroma_weight_l1_cb[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom) + value;;
339 
340 
341                 SEV_PARSE("delta_chroma_offset_l1[ i ][ j ]", value, ps_bitstrm);
342                 if( value < -512 || value > 511 )
343                 {
344                     return IHEVCD_INVALID_PARAMETER;
345                 }
346                 ofst = ((shift * ps_wt_ofst->i2_chroma_weight_l1_cb[i]) >> ps_wt_ofst->i1_chroma_log2_weight_denom);
347                 ofst = value - ofst + shift;
348 
349                 ps_wt_ofst->i2_chroma_offset_l1_cb[i] = CLIP_S8(ofst);;
350 
351                 SEV_PARSE("delta_chroma_weight_l1[ i ][ j ]", value, ps_bitstrm);
352                 if(value < -128 || value > 127)
353                 {
354                     return IHEVCD_INVALID_PARAMETER;
355                 }
356                 ps_wt_ofst->i2_chroma_weight_l1_cr[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom) + value;
357 
358 
359                 SEV_PARSE("delta_chroma_offset_l1[ i ][ j ]", value, ps_bitstrm);
360                 if( value < -512 || value > 511 )
361                 {
362                     return IHEVCD_INVALID_PARAMETER;
363                 }
364                 ofst = ((shift * ps_wt_ofst->i2_chroma_weight_l1_cr[i]) >> ps_wt_ofst->i1_chroma_log2_weight_denom);
365                 ofst = value - ofst + shift;
366 
367                 ps_wt_ofst->i2_chroma_offset_l1_cr[i] = CLIP_S8(ofst);;
368 
369             }
370             else
371             {
372                 ps_wt_ofst->i2_chroma_weight_l1_cb[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom);
373                 ps_wt_ofst->i2_chroma_weight_l1_cr[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom);
374 
375                 ps_wt_ofst->i2_chroma_offset_l1_cb[i] = 0;
376                 ps_wt_ofst->i2_chroma_offset_l1_cr[i] = 0;
377 
378             }
379         }
380     }
381     return ret;
382 }
383 
384 /**
385 *******************************************************************************
386 *
387 * @brief
388 *  Parses short term reference picture set
389 *
390 * @par   Description
391 *  Parses short term reference picture set as per section 7.3.8.2.
392 * Can be called by either SPS or Slice header parsing modules.
393 *
394 * @param[in] ps_bitstrm
395 *  Pointer to bitstream structure
396 *
397 * @param[out] ps_stref_picset_base
398 *  Pointer to first short term ref pic set structure
399 *
400 * @param[in] num_short_term_ref_pic_sets
401 *  Number of short term reference pic sets
402 *
403 * @param[in] idx
404 *  Current short term ref pic set id
405 *
406 * @returns Error code from IHEVCD_ERROR_T
407 *
408 * @remarks
409 *
410 *******************************************************************************
411 */
ihevcd_short_term_ref_pic_set(bitstrm_t * ps_bitstrm,stref_picset_t * ps_stref_picset_base,WORD32 num_short_term_ref_pic_sets,WORD32 idx,stref_picset_t * ps_stref_picset)412 IHEVCD_ERROR_T ihevcd_short_term_ref_pic_set(bitstrm_t *ps_bitstrm,
413                                              stref_picset_t *ps_stref_picset_base,
414                                              WORD32 num_short_term_ref_pic_sets,
415                                              WORD32 idx,
416                                              stref_picset_t *ps_stref_picset)
417 {
418     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
419     UWORD32 value;
420     stref_picset_t *ps_stref_picset_ref;
421     WORD32 delta_idx, delta_rps;
422     WORD32 r_idx;
423     WORD32 i;
424     WORD32 j, k, temp;
425     if(idx > 0)
426     {
427         BITS_PARSE("inter_ref_pic_set_prediction_flag", value, ps_bitstrm, 1);
428         ps_stref_picset->i1_inter_ref_pic_set_prediction_flag = value;
429     }
430     else
431         ps_stref_picset->i1_inter_ref_pic_set_prediction_flag = 0;
432 
433     if(ps_stref_picset->i1_inter_ref_pic_set_prediction_flag)
434     {
435         WORD32 delta_rps_sign;
436         WORD32 abs_delta_rps;
437         WORD32 num_neg_pics = 0;
438         WORD32 num_pos_pics = 0;
439         WORD32 num_pics = 0;
440 
441         if(idx == num_short_term_ref_pic_sets)
442         {
443             UEV_PARSE("delta_idx_minus1", value, ps_bitstrm);
444             if(value > num_short_term_ref_pic_sets - 1)
445             {
446                 return IHEVCD_INVALID_PARAMETER;
447             }
448             delta_idx = value + 1;
449         }
450         else
451         {
452             delta_idx = 1;
453         }
454         r_idx = idx - delta_idx;
455         r_idx = CLIP3(r_idx, 0, idx - 1);
456 
457         ps_stref_picset_ref = ps_stref_picset_base + r_idx;
458 
459         BITS_PARSE("delta_rps_sign", value, ps_bitstrm, 1);
460         delta_rps_sign = value;
461 
462         UEV_PARSE("abs_delta_rps_minus1", value, ps_bitstrm);
463         if(value > 32767)
464         {
465             return IHEVCD_INVALID_PARAMETER;
466         }
467         abs_delta_rps = value + 1;
468 
469         delta_rps = (1 - 2 * delta_rps_sign) * (abs_delta_rps);
470 
471 
472 
473         for(i = 0; i <= ps_stref_picset_ref->i1_num_delta_pocs; i++)
474         {
475             WORD32 ref_idc;
476 
477             /*****************************************************************/
478             /* ref_idc is parsed as below                                    */
479             /* bits "1" ref_idc 1                                            */
480             /* bits "01" ref_idc 2                                           */
481             /* bits "00" ref_idc 0                                           */
482             /*****************************************************************/
483             BITS_PARSE("used_by_curr_pic_flag", value, ps_bitstrm, 1);
484             ref_idc = value;
485             ps_stref_picset->ai1_used[num_pics] = value;
486             /* If ref_idc is zero check for next bit */
487             if(0 == ref_idc)
488             {
489                 BITS_PARSE("use_delta_flag", value, ps_bitstrm, 1);
490                 ref_idc = value << 1;
491             }
492             if((ref_idc == 1) || (ref_idc == 2))
493             {
494                 WORD32 delta_poc;
495                 delta_poc = delta_rps;
496                 delta_poc +=
497                                 ((i < ps_stref_picset_ref->i1_num_delta_pocs) ?
498                                 ps_stref_picset_ref->ai2_delta_poc[i] :
499                                 0);
500 
501                 ps_stref_picset->ai2_delta_poc[num_pics] = delta_poc;
502 
503                 if(delta_poc < 0)
504                 {
505                     num_neg_pics++;
506                 }
507                 else
508                 {
509                     num_pos_pics++;
510                 }
511                 num_pics++;
512             }
513             ps_stref_picset->ai1_ref_idc[i] = ref_idc;
514         }
515 
516         num_neg_pics = CLIP3(num_neg_pics, 0, MAX_DPB_SIZE - 1);
517         num_pos_pics = CLIP3(num_pos_pics, 0, (MAX_DPB_SIZE - 1 - num_neg_pics));
518         num_pics = num_neg_pics + num_pos_pics;
519 
520         ps_stref_picset->i1_num_ref_idc =
521                         ps_stref_picset_ref->i1_num_delta_pocs + 1;
522         ps_stref_picset->i1_num_delta_pocs = num_pics;
523         ps_stref_picset->i1_num_pos_pics = num_pos_pics;
524         ps_stref_picset->i1_num_neg_pics = num_neg_pics;
525 
526 
527         for(j = 1; j < num_pics; j++)
528         {
529             WORD32 delta_poc = ps_stref_picset->ai2_delta_poc[j];
530             WORD8 i1_used = ps_stref_picset->ai1_used[j];
531             for(k = j - 1; k >= 0; k--)
532             {
533                 temp = ps_stref_picset->ai2_delta_poc[k];
534                 if(delta_poc < temp)
535                 {
536                     ps_stref_picset->ai2_delta_poc[k + 1] = temp;
537                     ps_stref_picset->ai1_used[k + 1] = ps_stref_picset->ai1_used[k];
538                     ps_stref_picset->ai2_delta_poc[k] = delta_poc;
539                     ps_stref_picset->ai1_used[k] = i1_used;
540                 }
541             }
542         }
543         // flip the negative values to largest first
544         for(j = 0, k = num_neg_pics - 1; j < num_neg_pics >> 1; j++, k--)
545         {
546             WORD32 delta_poc = ps_stref_picset->ai2_delta_poc[j];
547             WORD8 i1_used = ps_stref_picset->ai1_used[j];
548             ps_stref_picset->ai2_delta_poc[j] = ps_stref_picset->ai2_delta_poc[k];
549             ps_stref_picset->ai1_used[j] = ps_stref_picset->ai1_used[k];
550             ps_stref_picset->ai2_delta_poc[k] = delta_poc;
551             ps_stref_picset->ai1_used[k] = i1_used;
552         }
553 
554     }
555     else
556     {
557         WORD32 prev_poc = 0;
558         WORD32 poc;
559 
560         UEV_PARSE("num_negative_pics", value, ps_bitstrm);
561         if(value > MAX_DPB_SIZE - 1)
562         {
563             return IHEVCD_INVALID_PARAMETER;
564         }
565         ps_stref_picset->i1_num_neg_pics = value;
566 
567         UEV_PARSE("num_positive_pics", value, ps_bitstrm);
568         if(value > (MAX_DPB_SIZE - 1 - ps_stref_picset->i1_num_neg_pics))
569         {
570             return IHEVCD_INVALID_PARAMETER;
571         }
572         ps_stref_picset->i1_num_pos_pics = value;
573 
574         ps_stref_picset->i1_num_delta_pocs =
575                         ps_stref_picset->i1_num_neg_pics +
576                         ps_stref_picset->i1_num_pos_pics;
577 
578 
579         for(i = 0; i < ps_stref_picset->i1_num_neg_pics; i++)
580         {
581             UEV_PARSE("delta_poc_s0_minus1", value, ps_bitstrm);
582             if(value > 32767)
583             {
584                 return IHEVCD_INVALID_PARAMETER;
585             }
586             poc = prev_poc - ((WORD32)(value + 1));
587             prev_poc = poc;
588             ps_stref_picset->ai2_delta_poc[i] = poc;
589 
590             BITS_PARSE("used_by_curr_pic_s0_flag", value, ps_bitstrm, 1);
591             ps_stref_picset->ai1_used[i] = value;
592 
593         }
594         prev_poc = 0;
595         for(i = ps_stref_picset->i1_num_neg_pics;
596                         i < ps_stref_picset->i1_num_delta_pocs;
597                         i++)
598         {
599             UEV_PARSE("delta_poc_s1_minus1", value, ps_bitstrm);
600             if(value > 32767)
601             {
602                 return IHEVCD_INVALID_PARAMETER;
603             }
604             poc = prev_poc + (value + 1);
605             prev_poc = poc;
606             ps_stref_picset->ai2_delta_poc[i] = poc;
607 
608             BITS_PARSE("used_by_curr_pic_s1_flag", value, ps_bitstrm, 1);
609             ps_stref_picset->ai1_used[i] = value;
610 
611         }
612 
613     }
614 
615     return ret;
616 }
617 
618 
ihevcd_parse_sub_layer_hrd_parameters(bitstrm_t * ps_bitstrm,sub_lyr_hrd_params_t * ps_sub_layer_hrd_params,WORD32 cpb_cnt,WORD32 sub_pic_cpb_params_present_flag)619 static WORD32 ihevcd_parse_sub_layer_hrd_parameters(bitstrm_t *ps_bitstrm,
620                                                     sub_lyr_hrd_params_t *ps_sub_layer_hrd_params,
621                                                     WORD32 cpb_cnt,
622                                                     WORD32 sub_pic_cpb_params_present_flag)
623 {
624     WORD32 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
625     WORD32 i;
626 
627     for(i = 0; i <= cpb_cnt; i++)
628     {
629         UEV_PARSE("bit_rate_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_bit_rate_value_minus1[i], ps_bitstrm);
630         if(ps_sub_layer_hrd_params->au4_bit_rate_value_minus1[i] > UINT_MAX - 1)
631         {
632             return IHEVCD_INVALID_PARAMETER;
633         }
634         UEV_PARSE("cpb_size_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_cpb_size_value_minus1[i], ps_bitstrm);
635         if(ps_sub_layer_hrd_params->au4_cpb_size_value_minus1[i] > UINT_MAX - 1)
636         {
637             return IHEVCD_INVALID_PARAMETER;
638         }
639         if(sub_pic_cpb_params_present_flag)
640         {
641             UEV_PARSE("cpb_size_du_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_cpb_size_du_value_minus1[i], ps_bitstrm);
642             if(ps_sub_layer_hrd_params->au4_cpb_size_du_value_minus1[i] > UINT_MAX - 1)
643             {
644                 return IHEVCD_INVALID_PARAMETER;
645             }
646             UEV_PARSE("bit_rate_du_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_bit_rate_du_value_minus1[i], ps_bitstrm);
647             if(ps_sub_layer_hrd_params->au4_bit_rate_du_value_minus1[i] > UINT_MAX - 1)
648             {
649                 return IHEVCD_INVALID_PARAMETER;
650             }
651         }
652         BITS_PARSE("cbr_flag[ i ]", ps_sub_layer_hrd_params->au1_cbr_flag[i], ps_bitstrm, 1);
653     }
654 
655     return ret;
656 }
657 
658 
ihevcd_parse_hrd_parameters(bitstrm_t * ps_bitstrm,hrd_params_t * ps_hrd,WORD32 common_info_present_flag,WORD32 max_num_sub_layers_minus1)659 static WORD32 ihevcd_parse_hrd_parameters(bitstrm_t *ps_bitstrm,
660                                           hrd_params_t *ps_hrd,
661                                           WORD32 common_info_present_flag,
662                                           WORD32 max_num_sub_layers_minus1)
663 {
664     WORD32 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
665     WORD32 i;
666 
667     ps_hrd->u1_nal_hrd_parameters_present_flag = 0;
668     ps_hrd->u1_vcl_hrd_parameters_present_flag = 0;
669 
670     ps_hrd->u1_sub_pic_cpb_params_present_flag = 0;
671 
672     ps_hrd->u1_tick_divisor_minus2 = 0;
673     ps_hrd->u1_du_cpb_removal_delay_increment_length_minus1 = 0;
674     ps_hrd->u1_sub_pic_cpb_params_in_pic_timing_sei_flag = 0;
675     ps_hrd->u1_dpb_output_delay_du_length_minus1 = 0;
676 
677     ps_hrd->u4_bit_rate_scale = 0;
678     ps_hrd->u4_cpb_size_scale = 0;
679     ps_hrd->u4_cpb_size_du_scale = 0;
680 
681     ps_hrd->u1_initial_cpb_removal_delay_length_minus1 = 23;
682     ps_hrd->u1_au_cpb_removal_delay_length_minus1 = 23;
683     ps_hrd->u1_dpb_output_delay_length_minus1 = 23;
684 
685     if(common_info_present_flag)
686     {
687         BITS_PARSE("nal_hrd_parameters_present_flag", ps_hrd->u1_nal_hrd_parameters_present_flag, ps_bitstrm, 1);
688         BITS_PARSE("vcl_hrd_parameters_present_flag", ps_hrd->u1_vcl_hrd_parameters_present_flag, ps_bitstrm, 1);
689 
690         if(ps_hrd->u1_nal_hrd_parameters_present_flag  ||  ps_hrd->u1_vcl_hrd_parameters_present_flag)
691         {
692             BITS_PARSE("sub_pic_cpb_params_present_flag", ps_hrd->u1_sub_pic_cpb_params_present_flag, ps_bitstrm, 1);
693             if(ps_hrd->u1_sub_pic_cpb_params_present_flag)
694             {
695                 BITS_PARSE("tick_divisor_minus2", ps_hrd->u1_tick_divisor_minus2, ps_bitstrm, 8);
696                 BITS_PARSE("du_cpb_removal_delay_increment_length_minus1", ps_hrd->u1_du_cpb_removal_delay_increment_length_minus1, ps_bitstrm, 5);
697                 BITS_PARSE("sub_pic_cpb_params_in_pic_timing_sei_flag", ps_hrd->u1_sub_pic_cpb_params_in_pic_timing_sei_flag, ps_bitstrm, 1);
698                 BITS_PARSE("dpb_output_delay_du_length_minus1", ps_hrd->u1_dpb_output_delay_du_length_minus1, ps_bitstrm, 5);
699             }
700 
701             BITS_PARSE("bit_rate_scale", ps_hrd->u4_bit_rate_scale, ps_bitstrm, 4);
702             BITS_PARSE("cpb_size_scale", ps_hrd->u4_cpb_size_scale, ps_bitstrm, 4);
703             if(ps_hrd->u1_sub_pic_cpb_params_present_flag)
704                 BITS_PARSE("cpb_size_du_scale", ps_hrd->u4_cpb_size_du_scale, ps_bitstrm, 4);
705 
706             BITS_PARSE("initial_cpb_removal_delay_length_minus1", ps_hrd->u1_initial_cpb_removal_delay_length_minus1, ps_bitstrm, 5);
707             BITS_PARSE("au_cpb_removal_delay_length_minus1", ps_hrd->u1_au_cpb_removal_delay_length_minus1, ps_bitstrm, 5);
708             BITS_PARSE("dpb_output_delay_length_minus1", ps_hrd->u1_dpb_output_delay_length_minus1, ps_bitstrm, 5);
709         }
710     }
711 
712 
713     for(i = 0; i <= max_num_sub_layers_minus1; i++)
714     {
715         BITS_PARSE("fixed_pic_rate_general_flag[ i ]", ps_hrd->au1_fixed_pic_rate_general_flag[i], ps_bitstrm, 1);
716 
717         ps_hrd->au1_fixed_pic_rate_within_cvs_flag[i] = 1;
718         ps_hrd->au2_elemental_duration_in_tc_minus1[i] = 0;
719         ps_hrd->au1_low_delay_hrd_flag[i] = 0;
720         ps_hrd->au1_cpb_cnt_minus1[i] = 0;
721 
722         if(!ps_hrd->au1_fixed_pic_rate_general_flag[i])
723             BITS_PARSE("fixed_pic_rate_within_cvs_flag[ i ]", ps_hrd->au1_fixed_pic_rate_within_cvs_flag[i], ps_bitstrm, 1);
724 
725         if(ps_hrd->au1_fixed_pic_rate_within_cvs_flag[i])
726         {
727             UEV_PARSE("elemental_duration_in_tc_minus1[ i ]", ps_hrd->au2_elemental_duration_in_tc_minus1[i], ps_bitstrm);
728             if(ps_hrd->au2_elemental_duration_in_tc_minus1[i] > 2047)
729             {
730                 return IHEVCD_INVALID_PARAMETER;
731             }
732         }
733         else
734         {
735             BITS_PARSE("low_delay_hrd_flag[ i ]", ps_hrd->au1_low_delay_hrd_flag[i], ps_bitstrm, 1);
736         }
737 
738         if(!ps_hrd->au1_low_delay_hrd_flag[i])
739         {
740             UEV_PARSE("cpb_cnt_minus1[ i ]", ps_hrd->au1_cpb_cnt_minus1[i], ps_bitstrm);
741             if(ps_hrd->au1_cpb_cnt_minus1[i] > (MAX_CPB_CNT - 1))
742             {
743                 return IHEVCD_INVALID_PARAMETER;
744             }
745         }
746 
747         if(ps_hrd->u1_nal_hrd_parameters_present_flag)
748             ihevcd_parse_sub_layer_hrd_parameters(ps_bitstrm,
749                                                   &ps_hrd->as_sub_layer_hrd_params[i],
750                                                   ps_hrd->au1_cpb_cnt_minus1[i],
751                                                   ps_hrd->u1_sub_pic_cpb_params_present_flag);
752 
753         if(ps_hrd->u1_vcl_hrd_parameters_present_flag)
754             ihevcd_parse_sub_layer_hrd_parameters(ps_bitstrm,
755                                                   &ps_hrd->as_sub_layer_hrd_params[i],
756                                                   ps_hrd->au1_cpb_cnt_minus1[i],
757                                                   ps_hrd->u1_sub_pic_cpb_params_present_flag);
758     }
759 
760     return ret;
761 }
762 
763 
ihevcd_parse_vui_parameters(bitstrm_t * ps_bitstrm,vui_t * ps_vui,WORD32 sps_max_sub_layers_minus1)764 static WORD32 ihevcd_parse_vui_parameters(bitstrm_t *ps_bitstrm,
765                                           vui_t *ps_vui,
766                                           WORD32 sps_max_sub_layers_minus1)
767 {
768     WORD32 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
769     UWORD16 u2_sar_width = 0;
770     UWORD16 u2_sar_height = 0;
771 
772     BITS_PARSE("aspect_ratio_info_present_flag", ps_vui->u1_aspect_ratio_info_present_flag, ps_bitstrm, 1);
773 
774     ps_vui->u1_aspect_ratio_idc = SAR_UNUSED;
775     u2_sar_width = 0;
776     u2_sar_height = 0;
777     if(ps_vui->u1_aspect_ratio_info_present_flag)
778     {
779         BITS_PARSE("aspect_ratio_idc", ps_vui->u1_aspect_ratio_idc, ps_bitstrm, 8);
780         switch(ps_vui->u1_aspect_ratio_idc)
781         {
782             case SAR_1_1:
783                 u2_sar_width = 1;
784                 u2_sar_height = 1;
785                 break;
786             case SAR_12_11:
787                 u2_sar_width = 12;
788                 u2_sar_height = 11;
789                 break;
790             case SAR_10_11:
791                 u2_sar_width = 10;
792                 u2_sar_height = 11;
793                 break;
794             case SAR_16_11:
795                 u2_sar_width = 16;
796                 u2_sar_height = 11;
797                 break;
798             case SAR_40_33:
799                 u2_sar_width = 40;
800                 u2_sar_height = 33;
801                 break;
802             case SAR_24_11:
803                 u2_sar_width = 24;
804                 u2_sar_height = 11;
805                 break;
806             case SAR_20_11:
807                 u2_sar_width = 20;
808                 u2_sar_height = 11;
809                 break;
810             case SAR_32_11:
811                 u2_sar_width = 32;
812                 u2_sar_height = 11;
813                 break;
814             case SAR_80_33:
815                 u2_sar_width = 80;
816                 u2_sar_height = 33;
817                 break;
818             case SAR_18_11:
819                 u2_sar_width = 18;
820                 u2_sar_height = 11;
821                 break;
822             case SAR_15_11:
823                 u2_sar_width = 15;
824                 u2_sar_height = 11;
825                 break;
826             case SAR_64_33:
827                 u2_sar_width = 64;
828                 u2_sar_height = 33;
829                 break;
830             case SAR_160_99:
831                 u2_sar_width = 160;
832                 u2_sar_height = 99;
833                 break;
834             case SAR_4_3:
835                 u2_sar_width = 4;
836                 u2_sar_height = 3;
837                 break;
838             case SAR_3_2:
839                 u2_sar_width = 3;
840                 u2_sar_height = 2;
841                 break;
842             case SAR_2_1:
843                 u2_sar_width = 2;
844                 u2_sar_height = 1;
845                 break;
846             case EXTENDED_SAR:
847                 BITS_PARSE("sar_width", u2_sar_width, ps_bitstrm, 16);
848                 BITS_PARSE("sar_height", u2_sar_height, ps_bitstrm, 16);
849                 break;
850             default:
851                 u2_sar_width = 0;
852                 u2_sar_height = 0;
853                 break;
854         }
855     }
856 
857     ps_vui->u2_sar_width    = u2_sar_width;
858     ps_vui->u2_sar_height   = u2_sar_height;
859 
860     BITS_PARSE("overscan_info_present_flag", ps_vui->u1_overscan_info_present_flag, ps_bitstrm, 1);
861     ps_vui->u1_overscan_appropriate_flag = 0;
862     if(ps_vui->u1_overscan_info_present_flag)
863         BITS_PARSE("overscan_appropriate_flag", ps_vui->u1_overscan_appropriate_flag, ps_bitstrm, 1);
864 
865     BITS_PARSE("video_signal_type_present_flag", ps_vui->u1_video_signal_type_present_flag, ps_bitstrm, 1);
866     ps_vui->u1_video_format = VID_FMT_UNSPECIFIED;
867     ps_vui->u1_video_full_range_flag = 0;
868     ps_vui->u1_colour_description_present_flag = 0;
869     ps_vui->u1_colour_primaries = 2;
870     ps_vui->u1_transfer_characteristics = 2;
871     ps_vui->u1_matrix_coefficients = 2;
872 
873     if(ps_vui->u1_video_signal_type_present_flag)
874     {
875         BITS_PARSE("video_format", ps_vui->u1_video_format, ps_bitstrm, 3);
876         BITS_PARSE("video_full_range_flag", ps_vui->u1_video_full_range_flag, ps_bitstrm, 1);
877         BITS_PARSE("colour_description_present_flag", ps_vui->u1_colour_description_present_flag, ps_bitstrm, 1);
878         if(ps_vui->u1_colour_description_present_flag)
879         {
880             BITS_PARSE("colour_primaries", ps_vui->u1_colour_primaries, ps_bitstrm, 8);
881             BITS_PARSE("transfer_characteristics", ps_vui->u1_transfer_characteristics, ps_bitstrm, 8);
882             BITS_PARSE("matrix_coeffs", ps_vui->u1_matrix_coefficients, ps_bitstrm, 8);
883         }
884     }
885 
886     BITS_PARSE("chroma_loc_info_present_flag", ps_vui->u1_chroma_loc_info_present_flag, ps_bitstrm, 1);
887     ps_vui->u1_chroma_sample_loc_type_top_field = 0;
888     ps_vui->u1_chroma_sample_loc_type_bottom_field = 0;
889     if(ps_vui->u1_chroma_loc_info_present_flag)
890     {
891         UEV_PARSE("chroma_sample_loc_type_top_field", ps_vui->u1_chroma_sample_loc_type_top_field, ps_bitstrm);
892         if(ps_vui->u1_chroma_sample_loc_type_top_field > CHROMA_FMT_IDC_YUV444_PLANES + 1)
893         {
894             return IHEVCD_INVALID_PARAMETER;
895         }
896         UEV_PARSE("chroma_sample_loc_type_bottom_field", ps_vui->u1_chroma_sample_loc_type_bottom_field, ps_bitstrm);
897         if(ps_vui->u1_chroma_sample_loc_type_bottom_field > CHROMA_FMT_IDC_YUV444_PLANES + 1)
898         {
899             return IHEVCD_INVALID_PARAMETER;
900         }
901     }
902 
903     BITS_PARSE("neutral_chroma_indication_flag", ps_vui->u1_neutral_chroma_indication_flag, ps_bitstrm, 1);
904     BITS_PARSE("field_seq_flag", ps_vui->u1_field_seq_flag, ps_bitstrm, 1);
905     BITS_PARSE("frame_field_info_present_flag", ps_vui->u1_frame_field_info_present_flag, ps_bitstrm, 1);
906     BITS_PARSE("default_display_window_flag", ps_vui->u1_default_display_window_flag, ps_bitstrm, 1);
907     ps_vui->u4_def_disp_win_left_offset = 0;
908     ps_vui->u4_def_disp_win_right_offset = 0;
909     ps_vui->u4_def_disp_win_top_offset = 0;
910     ps_vui->u4_def_disp_win_bottom_offset = 0;
911     if(ps_vui->u1_default_display_window_flag)
912     {
913         UEV_PARSE("def_disp_win_left_offset", ps_vui->u4_def_disp_win_left_offset, ps_bitstrm);
914         UEV_PARSE("def_disp_win_right_offset", ps_vui->u4_def_disp_win_right_offset, ps_bitstrm);
915         UEV_PARSE("def_disp_win_top_offset", ps_vui->u4_def_disp_win_top_offset, ps_bitstrm);
916         UEV_PARSE("def_disp_win_bottom_offset", ps_vui->u4_def_disp_win_bottom_offset, ps_bitstrm);
917     }
918 
919     BITS_PARSE("vui_timing_info_present_flag", ps_vui->u1_vui_timing_info_present_flag, ps_bitstrm, 1);
920     if(ps_vui->u1_vui_timing_info_present_flag)
921     {
922         BITS_PARSE("vui_num_units_in_tick", ps_vui->u4_vui_num_units_in_tick, ps_bitstrm, 32);
923         BITS_PARSE("vui_time_scale", ps_vui->u4_vui_time_scale, ps_bitstrm, 32);
924         BITS_PARSE("vui_poc_proportional_to_timing_flag", ps_vui->u1_poc_proportional_to_timing_flag, ps_bitstrm, 1);
925         if(ps_vui->u1_poc_proportional_to_timing_flag)
926         {
927             UEV_PARSE("vui_num_ticks_poc_diff_one_minus1", ps_vui->u4_num_ticks_poc_diff_one_minus1, ps_bitstrm);
928             if(ps_vui->u4_num_ticks_poc_diff_one_minus1 > UINT_MAX - 1)
929             {
930                 return IHEVCD_INVALID_PARAMETER;
931             }
932         }
933         BITS_PARSE("vui_hrd_parameters_present_flag", ps_vui->u1_vui_hrd_parameters_present_flag, ps_bitstrm, 1);
934         if(ps_vui->u1_vui_hrd_parameters_present_flag)
935         {
936             ret = ihevcd_parse_hrd_parameters(ps_bitstrm, &ps_vui->s_vui_hrd_parameters, 1, sps_max_sub_layers_minus1);
937             RETURN_IF((ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS), ret);
938         }
939     }
940 
941     BITS_PARSE("bitstream_restriction_flag", ps_vui->u1_bitstream_restriction_flag, ps_bitstrm, 1);
942     ps_vui->u1_tiles_fixed_structure_flag = 0;
943     ps_vui->u1_motion_vectors_over_pic_boundaries_flag = 1;
944     ps_vui->u1_restricted_ref_pic_lists_flag = 0;
945     ps_vui->u4_min_spatial_segmentation_idc = 0;
946     ps_vui->u1_max_bytes_per_pic_denom = 2;
947     ps_vui->u1_max_bits_per_mincu_denom = 1;
948     ps_vui->u1_log2_max_mv_length_horizontal = 15;
949     ps_vui->u1_log2_max_mv_length_vertical = 15;
950     if(ps_vui->u1_bitstream_restriction_flag)
951     {
952         BITS_PARSE("tiles_fixed_structure_flag", ps_vui->u1_tiles_fixed_structure_flag, ps_bitstrm, 1);
953         BITS_PARSE("motion_vectors_over_pic_boundaries_flag", ps_vui->u1_motion_vectors_over_pic_boundaries_flag, ps_bitstrm, 1);
954         BITS_PARSE("restricted_ref_pic_lists_flag", ps_vui->u1_restricted_ref_pic_lists_flag, ps_bitstrm, 1);
955 
956         UEV_PARSE("min_spatial_segmentation_idc", ps_vui->u4_min_spatial_segmentation_idc, ps_bitstrm);
957         if(ps_vui->u4_min_spatial_segmentation_idc > 4095)
958         {
959             return IHEVCD_INVALID_PARAMETER;
960         }
961         UEV_PARSE("max_bytes_per_pic_denom", ps_vui->u1_max_bytes_per_pic_denom, ps_bitstrm);
962         if(ps_vui->u1_max_bytes_per_pic_denom > 16)
963         {
964             return IHEVCD_INVALID_PARAMETER;
965         }
966         UEV_PARSE("max_bits_per_min_cu_denom", ps_vui->u1_max_bits_per_mincu_denom, ps_bitstrm);
967         if(ps_vui->u1_max_bits_per_mincu_denom > 16)
968         {
969             return IHEVCD_INVALID_PARAMETER;
970         }
971         UEV_PARSE("log2_max_mv_length_horizontal", ps_vui->u1_log2_max_mv_length_horizontal, ps_bitstrm);
972         if(ps_vui->u1_max_bits_per_mincu_denom > 16)
973         {
974             return IHEVCD_INVALID_PARAMETER;
975         }
976         UEV_PARSE("log2_max_mv_length_vertical", ps_vui->u1_log2_max_mv_length_vertical, ps_bitstrm);
977         if(ps_vui->u1_max_bits_per_mincu_denom > 15)
978         {
979             return IHEVCD_INVALID_PARAMETER;
980         }
981     }
982 
983     return ret;
984 }
985 
986 /**
987 *******************************************************************************
988 *
989 * @brief
990 *  Parses profile tier and level info for either general layer of sub_layer
991 *
992 * @par   Description
993 *  Parses profile tier and level info for either general layer of sub_layer
994 * as per section 7.3.3
995 *
996 * Since the same function is called for parsing general_profile and
997 * sub_layer_profile etc, variables do not specify whether the syntax is
998 * for general or sub_layer. Similarly trace functions also do not differentiate
999 *
1000 * @param[in] ps_bitstrm
1001 *  Pointer to bitstream structure
1002 *
1003 * @param[out] ps_ptl
1004 *  Pointer to profile, tier level structure
1005 *
1006 * @returns Error code from IHEVCD_ERROR_T
1007 *
1008 * @remarks
1009 *
1010 *******************************************************************************
1011 */
1012 
ihevcd_parse_profile_tier_level_layer(bitstrm_t * ps_bitstrm,profile_tier_lvl_t * ps_ptl)1013 static IHEVCD_ERROR_T ihevcd_parse_profile_tier_level_layer(bitstrm_t *ps_bitstrm,
1014                                                             profile_tier_lvl_t *ps_ptl)
1015 {
1016     WORD32 value;
1017     WORD32 i;
1018     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
1019 
1020     BITS_PARSE("XXX_profile_space[]", value, ps_bitstrm, 2);
1021     ps_ptl->i1_profile_space = value;
1022 
1023     BITS_PARSE("XXX_tier_flag[]", value, ps_bitstrm, 1);
1024     ps_ptl->i1_tier_flag = value;
1025 
1026     BITS_PARSE("XXX_profile_idc[]", value, ps_bitstrm, 5);
1027     ps_ptl->i1_profile_idc = value;
1028 
1029     for(i = 0; i < MAX_PROFILE_COMPATBLTY; i++)
1030     {
1031         BITS_PARSE("XXX_profile_compatibility_flag[][j]", value, ps_bitstrm, 1);
1032         ps_ptl->ai1_profile_compatibility_flag[i] = value;
1033     }
1034 
1035     BITS_PARSE("general_progressive_source_flag", value, ps_bitstrm, 1);
1036     ps_ptl->i1_general_progressive_source_flag = value;
1037 
1038     BITS_PARSE("general_interlaced_source_flag", value, ps_bitstrm, 1);
1039     ps_ptl->i1_general_interlaced_source_flag = value;
1040 
1041     BITS_PARSE("general_non_packed_constraint_flag", value, ps_bitstrm, 1);
1042     ps_ptl->i1_general_non_packed_constraint_flag = value;
1043 
1044     BITS_PARSE("general_frame_only_constraint_flag", value, ps_bitstrm, 1);
1045     ps_ptl->i1_frame_only_constraint_flag = value;
1046 
1047     BITS_PARSE("XXX_reserved_zero_44bits[0..15]", value, ps_bitstrm, 16);
1048 
1049     BITS_PARSE("XXX_reserved_zero_44bits[16..31]", value, ps_bitstrm, 16);
1050 
1051     BITS_PARSE("XXX_reserved_zero_44bits[32..43]", value, ps_bitstrm, 12);
1052     return ret;
1053 }
1054 
1055 
1056 /**
1057 *******************************************************************************
1058 *
1059 * @brief
1060 *  Parses profile tier and level info
1061 *
1062 * @par   Description
1063 *  Parses profile tier and level info as per section 7.3.3
1064 * Called during VPS and SPS parsing
1065 * calls ihevcd_parse_profile_tier_level() for general layer and each sub_layers
1066 *
1067 * @param[in] ps_bitstrm
1068 *  Pointer to bitstream structure
1069 *
1070 * @param[out] ps_ptl
1071 *  Pointer to structure that contains profile, tier level for each layers
1072 *
1073 * @param[in] profile_present
1074 *  Flag to indicate if profile data is present
1075 *
1076 * @param[in] max_num_sub_layers
1077 *  Number of sub layers present
1078 *
1079 * @returns Error code from IHEVCD_ERROR_T
1080 *
1081 * @remarks
1082 *
1083 *******************************************************************************
1084 */
1085 
ihevcd_profile_tier_level(bitstrm_t * ps_bitstrm,profile_tier_lvl_info_t * ps_ptl,WORD32 profile_present,WORD32 max_num_sub_layers)1086 static IHEVCD_ERROR_T ihevcd_profile_tier_level(bitstrm_t *ps_bitstrm,
1087                                                 profile_tier_lvl_info_t *ps_ptl,
1088                                                 WORD32 profile_present,
1089                                                 WORD32 max_num_sub_layers)
1090 {
1091     WORD32 value;
1092     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
1093     WORD32 i;
1094 
1095     if(profile_present)
1096     {
1097         ret = ihevcd_parse_profile_tier_level_layer(ps_bitstrm, &ps_ptl->s_ptl_gen);
1098         if((IHEVCD_ERROR_T)IHEVCD_SUCCESS != ret)
1099         {
1100             return ret;
1101         }
1102     }
1103 
1104     BITS_PARSE("general_level_idc", value, ps_bitstrm, 8);
1105     ps_ptl->s_ptl_gen.u1_level_idc = value;
1106 
1107 
1108     for(i = 0; i < max_num_sub_layers; i++)
1109     {
1110         BITS_PARSE("sub_layer_profile_present_flag[i]", value, ps_bitstrm, 1);
1111         ps_ptl->ai1_sub_layer_profile_present_flag[i] = value;
1112 
1113         BITS_PARSE("sub_layer_level_present_flag[i]", value, ps_bitstrm, 1);
1114         ps_ptl->ai1_sub_layer_level_present_flag[i] = value;
1115     }
1116 
1117     if(max_num_sub_layers > 0)
1118     {
1119         for(i = max_num_sub_layers; i < 8; i++)
1120         {
1121             BITS_PARSE("reserved_zero_2bits", value, ps_bitstrm, 2);
1122         }
1123     }
1124 
1125     for(i = 0; i < max_num_sub_layers; i++)
1126     {
1127         if(ps_ptl->ai1_sub_layer_profile_present_flag[i])
1128         {
1129             ret = ihevcd_parse_profile_tier_level_layer(ps_bitstrm,
1130                                                         &ps_ptl->as_ptl_sub[i]);
1131         }
1132         if(ps_ptl->ai1_sub_layer_level_present_flag[i])
1133         {
1134             BITS_PARSE("sub_layer_level_idc[i]", value, ps_bitstrm, 8);
1135             ps_ptl->as_ptl_sub[i].u1_level_idc = value;
1136 
1137         }
1138     }
1139 
1140 
1141 
1142     return ret;
1143 }
1144 
1145 /**
1146 *******************************************************************************
1147 *
1148 * @brief
1149 *  Parses Scaling List Data syntax
1150 *
1151 * @par Description:
1152 *  Parses Scaling List Data syntax as per Section: 7.3.6
1153 *
1154 * @param[in] ps_codec
1155 *  Pointer to codec context
1156 *
1157 * @returns  Error code from IHEVCD_ERROR_T
1158 *
1159 * @remarks
1160 *
1161 *
1162 *******************************************************************************
1163 */
ihevcd_scaling_list_data(codec_t * ps_codec,WORD16 * pi2_scaling_mat)1164 IHEVCD_ERROR_T  ihevcd_scaling_list_data(codec_t *ps_codec, WORD16 *pi2_scaling_mat)
1165 {
1166     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
1167     WORD32 size_id;
1168     WORD32 matrix_id;
1169     WORD32 value, dc_value = 0;
1170     UWORD32 u4_value;
1171     WORD32 next_coef;
1172     WORD32 coef_num;
1173     WORD32 i, j, offset;
1174     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
1175     WORD16 *pi2_scaling_mat_offset;
1176     WORD32 scaling_mat_offset[] = { 0, 16, 32, 48, 64, 80, 96, 160, 224, 288, 352, 416, 480, 736, 992, 1248, 1504, 1760, 2016, 3040 };
1177     UWORD8 *scan_table;
1178 
1179     for(size_id = 0; size_id < 4; size_id++)
1180     {
1181         for(matrix_id = 0; matrix_id < ((size_id == 3) ? 2 : 6); matrix_id++)
1182         {
1183             WORD32 scaling_list_pred_mode_flag;
1184             WORD32 scaling_list_delta_coef;
1185             BITS_PARSE("scaling_list_pred_mode_flag", scaling_list_pred_mode_flag, ps_bitstrm, 1);
1186 
1187             offset = size_id * 6 + matrix_id;
1188             pi2_scaling_mat_offset = pi2_scaling_mat + scaling_mat_offset[offset];
1189 
1190             if(!scaling_list_pred_mode_flag)
1191             {
1192                 WORD32 num_elements;
1193                 UEV_PARSE("scaling_list_pred_matrix_id_delta", u4_value,
1194                           ps_bitstrm);
1195                 if(u4_value > matrix_id)
1196                 {
1197                     return IHEVCD_INVALID_PARAMETER;
1198                 }
1199 
1200                 num_elements = (1 << (4 + (size_id << 1)));
1201                 if(0 != u4_value)
1202                     memmove(pi2_scaling_mat_offset, pi2_scaling_mat_offset - u4_value * num_elements, num_elements * sizeof(WORD16));
1203             }
1204             else
1205             {
1206                 next_coef = 8;
1207                 coef_num = MIN(64, (1 << (4 + (size_id << 1))));
1208 
1209                 if(size_id > 1)
1210                 {
1211                     SEV_PARSE("scaling_list_dc_coef_minus8", value,
1212                               ps_bitstrm);
1213                     if(value < -7 || value > 247)
1214                     {
1215                         return IHEVCD_INVALID_PARAMETER;
1216                     }
1217                     next_coef = value + 8;
1218                     dc_value = next_coef;
1219                 }
1220                 if(size_id < 2)
1221                 {
1222                     scan_table = (UWORD8 *)gapv_ihevc_invscan[size_id + 1];
1223 
1224                     for(i = 0; i < coef_num; i++)
1225                     {
1226                         SEV_PARSE("scaling_list_delta_coef",
1227                                   scaling_list_delta_coef, ps_bitstrm);
1228                         if((scaling_list_delta_coef < -256) || (scaling_list_delta_coef > 255))
1229                         {
1230                             return IHEVCD_INVALID_PARAMETER;
1231                         }
1232                         next_coef = (next_coef + scaling_list_delta_coef + 256)
1233                                         % 256;
1234                         pi2_scaling_mat_offset[scan_table[i]] = next_coef;
1235                     }
1236                 }
1237                 else if(size_id == 2)
1238                 {
1239                     scan_table = (UWORD8 *)gapv_ihevc_invscan[2];
1240 
1241                     for(i = 0; i < coef_num; i++)
1242                     {
1243                         SEV_PARSE("scaling_list_delta_coef",
1244                                   scaling_list_delta_coef, ps_bitstrm);
1245                         if((scaling_list_delta_coef < -256) || (scaling_list_delta_coef > 255))
1246                         {
1247                             return IHEVCD_INVALID_PARAMETER;
1248                         }
1249                         next_coef = (next_coef + scaling_list_delta_coef + 256)
1250                                         % 256;
1251 
1252                         offset = scan_table[i];
1253                         offset = (offset >> 3) * 16 * 2 + (offset & 0x7) * 2;
1254                         pi2_scaling_mat_offset[offset] = next_coef;
1255                         pi2_scaling_mat_offset[offset + 1] = next_coef;
1256                         pi2_scaling_mat_offset[offset + 16] = next_coef;
1257                         pi2_scaling_mat_offset[offset + 16 + 1] = next_coef;
1258                     }
1259                     pi2_scaling_mat_offset[0] = dc_value;
1260                 }
1261                 else
1262                 {
1263                     scan_table = (UWORD8 *)gapv_ihevc_invscan[2];
1264 
1265                     for(i = 0; i < coef_num; i++)
1266                     {
1267                         SEV_PARSE("scaling_list_delta_coef",
1268                                   scaling_list_delta_coef, ps_bitstrm);
1269                         if((scaling_list_delta_coef < -256) || (scaling_list_delta_coef > 255))
1270                         {
1271                             return IHEVCD_INVALID_PARAMETER;
1272                         }
1273                         next_coef = (next_coef + scaling_list_delta_coef + 256)
1274                                         % 256;
1275 
1276                         offset = scan_table[i];
1277                         offset = (offset >> 3) * 32 * 4 + (offset & 0x7) * 4;
1278 
1279                         for(j = 0; j < 4; j++)
1280                         {
1281                             pi2_scaling_mat_offset[offset + j * 32] = next_coef;
1282                             pi2_scaling_mat_offset[offset + 1 + j * 32] = next_coef;
1283                             pi2_scaling_mat_offset[offset + 2 + j * 32] = next_coef;
1284                             pi2_scaling_mat_offset[offset + 3 + j * 32] = next_coef;
1285                         }
1286                         pi2_scaling_mat_offset[0] = dc_value;
1287                     }
1288                 }
1289             }
1290         }
1291     }
1292 
1293     return ret;
1294 }
1295 
1296 /**
1297 *******************************************************************************
1298 *
1299 * @brief
1300 *  Parses VPS (Video Parameter Set)
1301 *
1302 * @par Description:
1303 *  Parse Video Parameter Set as per Section 7.3.2.1
1304 * update vps structure corresponding to vps ID
1305 * Till parsing VPS id, the elements are stored in local variables and are copied
1306 * later
1307 *
1308 * @param[in] ps_codec
1309 *  Pointer to codec context.
1310 *
1311 * @returns Error code from IHEVCD_ERROR_T
1312 *
1313 * @remarks
1314 *
1315 *
1316 *******************************************************************************
1317 */
ihevcd_parse_vps(codec_t * ps_codec)1318 IHEVCD_ERROR_T ihevcd_parse_vps(codec_t *ps_codec)
1319 {
1320     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
1321     WORD32 i;
1322     UWORD32 value;
1323     WORD32 vps_id;
1324     vps_t *ps_vps;
1325     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
1326     BITS_PARSE("vps_video_parameter_set_id", value, ps_bitstrm, 4);
1327     vps_id = value;
1328 
1329     if(vps_id >= MAX_VPS_CNT)
1330     {
1331         ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_VPS_ID;
1332         return IHEVCD_UNSUPPORTED_VPS_ID;
1333     }
1334 
1335 
1336     ps_vps = (ps_codec->s_parse.ps_vps_base + vps_id);
1337 
1338     ps_vps->i1_vps_id = vps_id;
1339 
1340     BITS_PARSE("vps_reserved_three_2bits", value, ps_bitstrm, 2);
1341     ASSERT(value == 3);
1342 
1343     BITS_PARSE("vps_max_layers_minus1", value, ps_bitstrm, 6);
1344     //ps_vps->i1_vps_max_layers = value + 1;
1345 
1346 
1347 
1348     BITS_PARSE("vps_max_sub_layers_minus1", value, ps_bitstrm, 3);
1349     if(value > SPS_MAX_SUB_LAYERS - 1)
1350     {
1351         return IHEVCD_INVALID_PARAMETER;
1352     }
1353     ps_vps->i1_vps_max_sub_layers = value + 1;
1354 
1355     ASSERT(ps_vps->i1_vps_max_sub_layers < VPS_MAX_SUB_LAYERS);
1356 
1357     BITS_PARSE("vps_temporal_id_nesting_flag", value, ps_bitstrm, 1);
1358     ps_vps->i1_vps_temporal_id_nesting_flag = value;
1359 
1360     BITS_PARSE("vps_reserved_ffff_16bits", value, ps_bitstrm, 16);
1361     ASSERT(value == 0xFFFF);
1362     // profile_and_level( 1, vps_max_sub_layers_minus1 )
1363     ret = ihevcd_profile_tier_level(ps_bitstrm, &(ps_vps->s_ptl),
1364                                     1, (ps_vps->i1_vps_max_sub_layers - 1));
1365 
1366     BITS_PARSE("vps_sub_layer_ordering_info_present_flag", value, ps_bitstrm, 1);
1367     ps_vps->i1_sub_layer_ordering_info_present_flag = value;
1368     i = (ps_vps->i1_sub_layer_ordering_info_present_flag ?
1369                     0 : (ps_vps->i1_vps_max_sub_layers - 1));
1370     for(; i < ps_vps->i1_vps_max_sub_layers; i++)
1371     {
1372         UEV_PARSE("vps_max_dec_pic_buffering[i]", value, ps_bitstrm);
1373         if(value > MAX_DPB_SIZE)
1374         {
1375             return IHEVCD_INVALID_PARAMETER;
1376         }
1377         ps_vps->ai1_vps_max_dec_pic_buffering[i] = value;
1378 
1379         /* vps_num_reorder_pics (no max) used in print in order to match with HM */
1380         UEV_PARSE("vps_num_reorder_pics[i]", value, ps_bitstrm);
1381         if(value >= ps_vps->ai1_vps_max_dec_pic_buffering[i])
1382         {
1383             return IHEVCD_INVALID_PARAMETER;
1384         }
1385         ps_vps->ai1_vps_max_num_reorder_pics[i] = value;
1386 
1387         UEV_PARSE("vps_max_latency_increase[i]", value, ps_bitstrm);
1388         if(value > UINT_MAX - 2)
1389         {
1390             return IHEVCD_INVALID_PARAMETER;
1391         }
1392         ps_vps->ai1_vps_max_latency_increase[i] = value;
1393     }
1394 
1395 
1396 
1397     BITS_PARSE("vps_max_layer_id", value, ps_bitstrm, 6);
1398     //ps_vps->i1_vps_max_layer_id  = value;
1399 
1400     UEV_PARSE("vps_num_layer_sets_minus1", value, ps_bitstrm);
1401     if(value > 1023)
1402     {
1403         return IHEVCD_INVALID_PARAMETER;
1404     }
1405     //ps_vps->i1_vps_num_layer_sets  = value + 1;
1406 
1407     BITS_PARSE("vps_timing_info_present_flag", value, ps_bitstrm, 1);
1408     //ps_vps->i1_vps_timing_info_present_flag  = value;
1409 
1410 
1411 
1412     return ret;
1413 }
1414 
1415 /**
1416 *******************************************************************************
1417 *
1418 * @brief
1419 *  Parses SPS (Sequence Parameter Set)
1420 * sequence_parameter_set_rbsp()
1421 *
1422 * @par Description:
1423 *  Parse Sequence Parameter Set as per section  Section: 7.3.2.2
1424 * The sps is written to a temporary buffer and copied later to the
1425 * appropriate location
1426 *
1427 * @param[in] ps_codec
1428 *  Pointer to codec context
1429 *
1430 * @returns Error code from IHEVCD_ERROR_T
1431 *
1432 * @remarks
1433 *
1434 *
1435 *******************************************************************************
1436 */
ihevcd_parse_sps(codec_t * ps_codec)1437 IHEVCD_ERROR_T ihevcd_parse_sps(codec_t *ps_codec)
1438 {
1439     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
1440     UWORD32 value;
1441 
1442     WORD32 i;
1443     WORD32 vps_id;
1444     WORD32 sps_max_sub_layers;
1445     WORD32 sps_id;
1446     WORD32 sps_temporal_id_nesting_flag;
1447     sps_t *ps_sps;
1448     profile_tier_lvl_info_t s_ptl;
1449     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
1450     WORD32 ctb_log2_size_y = 0;
1451 
1452 
1453     BITS_PARSE("video_parameter_set_id", value, ps_bitstrm, 4);
1454     if(value > MAX_VPS_CNT - 1)
1455     {
1456         return IHEVCD_INVALID_PARAMETER;
1457     }
1458     vps_id = value;
1459 
1460     BITS_PARSE("sps_max_sub_layers_minus1", value, ps_bitstrm, 3);
1461     if(value > SPS_MAX_SUB_LAYERS - 1)
1462     {
1463         return IHEVCD_INVALID_PARAMETER;
1464     }
1465     sps_max_sub_layers = value + 1;
1466 
1467     BITS_PARSE("sps_temporal_id_nesting_flag", value, ps_bitstrm, 1);
1468     sps_temporal_id_nesting_flag = value;
1469 
1470     //profile_and_level( 1, sps_max_sub_layers_minus1 )
1471     ret = ihevcd_profile_tier_level(ps_bitstrm, &(s_ptl), 1,
1472                                     (sps_max_sub_layers - 1));
1473 
1474     UEV_PARSE("seq_parameter_set_id", value, ps_bitstrm);
1475     sps_id = value;
1476     if((sps_id >= MAX_SPS_CNT) || (sps_id < 0))
1477     {
1478         if(ps_codec->i4_sps_done)
1479             return IHEVCD_UNSUPPORTED_SPS_ID;
1480         else
1481             sps_id = 0;
1482     }
1483 
1484 
1485     ps_sps = (ps_codec->s_parse.ps_sps_base + MAX_SPS_CNT - 1);
1486     /* Reset SPS to zero */
1487     {
1488         WORD16 *pi2_scaling_mat = ps_sps->pi2_scaling_mat;
1489         memset(ps_sps, 0, sizeof(sps_t));
1490         ps_sps->pi2_scaling_mat = pi2_scaling_mat;
1491     }
1492     ps_sps->i1_sps_id = sps_id;
1493     ps_sps->i1_vps_id = vps_id;
1494     ps_sps->i1_sps_max_sub_layers = sps_max_sub_layers;
1495     ps_sps->i1_sps_temporal_id_nesting_flag = sps_temporal_id_nesting_flag;
1496 
1497     memcpy(&ps_sps->s_ptl, &s_ptl, sizeof(profile_tier_lvl_info_t));
1498 
1499     UEV_PARSE("chroma_format_idc", value, ps_bitstrm);
1500     if(value > 3)
1501     {
1502         return IHEVCD_INVALID_PARAMETER;
1503     }
1504     ps_sps->i1_chroma_format_idc = value;
1505 
1506     if(ps_sps->i1_chroma_format_idc != CHROMA_FMT_IDC_YUV420)
1507     {
1508         ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
1509         return (IHEVCD_ERROR_T)IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
1510     }
1511 
1512     if(CHROMA_FMT_IDC_YUV444_PLANES == ps_sps->i1_chroma_format_idc)
1513     {
1514         BITS_PARSE("separate_colour_plane_flag", value, ps_bitstrm, 1);
1515         ps_sps->i1_separate_colour_plane_flag = value;
1516     }
1517     else
1518     {
1519         ps_sps->i1_separate_colour_plane_flag = 0;
1520     }
1521 
1522     UEV_PARSE("pic_width_in_luma_samples", value, ps_bitstrm);
1523     if(value > INT16_MAX)
1524     {
1525         return (IHEVCD_ERROR_T)IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
1526     }
1527     ps_sps->i2_pic_width_in_luma_samples = value;
1528 
1529     UEV_PARSE("pic_height_in_luma_samples", value, ps_bitstrm);
1530     if(value > INT16_MAX)
1531     {
1532         return (IHEVCD_ERROR_T)IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
1533     }
1534     ps_sps->i2_pic_height_in_luma_samples = value;
1535 
1536     if((0 >= ps_sps->i2_pic_width_in_luma_samples) || (0 >= ps_sps->i2_pic_height_in_luma_samples))
1537         return IHEVCD_INVALID_PARAMETER;
1538 
1539     BITS_PARSE("pic_cropping_flag", value, ps_bitstrm, 1);
1540     ps_sps->i1_pic_cropping_flag = value;
1541 
1542     if(ps_sps->i1_pic_cropping_flag)
1543     {
1544 
1545         UEV_PARSE("pic_crop_left_offset", value, ps_bitstrm);
1546         if (value >= ps_sps->i2_pic_width_in_luma_samples)
1547         {
1548             return IHEVCD_INVALID_PARAMETER;
1549         }
1550         ps_sps->i2_pic_crop_left_offset = value;
1551 
1552         UEV_PARSE("pic_crop_right_offset", value, ps_bitstrm);
1553         if (value >= ps_sps->i2_pic_width_in_luma_samples)
1554         {
1555             return IHEVCD_INVALID_PARAMETER;
1556         }
1557         ps_sps->i2_pic_crop_right_offset = value;
1558 
1559         UEV_PARSE("pic_crop_top_offset", value, ps_bitstrm);
1560         if (value >= ps_sps->i2_pic_height_in_luma_samples)
1561         {
1562             return IHEVCD_INVALID_PARAMETER;
1563         }
1564         ps_sps->i2_pic_crop_top_offset = value;
1565 
1566         UEV_PARSE("pic_crop_bottom_offset", value, ps_bitstrm);
1567         if (value >= ps_sps->i2_pic_height_in_luma_samples)
1568         {
1569             return IHEVCD_INVALID_PARAMETER;
1570         }
1571         ps_sps->i2_pic_crop_bottom_offset = value;
1572     }
1573     else
1574     {
1575         ps_sps->i2_pic_crop_left_offset = 0;
1576         ps_sps->i2_pic_crop_right_offset = 0;
1577         ps_sps->i2_pic_crop_top_offset = 0;
1578         ps_sps->i2_pic_crop_bottom_offset = 0;
1579     }
1580 
1581 
1582     UEV_PARSE("bit_depth_luma_minus8", value, ps_bitstrm);
1583     if(0 != value)
1584         return IHEVCD_UNSUPPORTED_BIT_DEPTH;
1585 
1586     UEV_PARSE("bit_depth_chroma_minus8", value, ps_bitstrm);
1587     if(0 != value)
1588         return IHEVCD_UNSUPPORTED_BIT_DEPTH;
1589 
1590     UEV_PARSE("log2_max_pic_order_cnt_lsb_minus4", value, ps_bitstrm);
1591     if(value > 12)
1592         return IHEVCD_INVALID_PARAMETER;
1593     ps_sps->i1_log2_max_pic_order_cnt_lsb = value + 4;
1594 
1595     BITS_PARSE("sps_sub_layer_ordering_info_present_flag", value, ps_bitstrm, 1);
1596 
1597     ps_sps->i1_sps_sub_layer_ordering_info_present_flag = value;
1598 
1599 
1600     i = (ps_sps->i1_sps_sub_layer_ordering_info_present_flag ? 0 : (ps_sps->i1_sps_max_sub_layers - 1));
1601     for(; i < ps_sps->i1_sps_max_sub_layers; i++)
1602     {
1603         UEV_PARSE("max_dec_pic_buffering", value, ps_bitstrm);
1604         if(value > (MAX_DPB_SIZE - 1))
1605         {
1606             return IHEVCD_INVALID_PARAMETER;
1607         }
1608         ps_sps->ai1_sps_max_dec_pic_buffering[i] = value + 1;
1609 
1610         UEV_PARSE("num_reorder_pics", value, ps_bitstrm);
1611         if(value >= ps_sps->ai1_sps_max_dec_pic_buffering[i])
1612         {
1613             return IHEVCD_INVALID_PARAMETER;
1614         }
1615         ps_sps->ai1_sps_max_num_reorder_pics[i] = value;
1616 
1617         UEV_PARSE("max_latency_increase", value, ps_bitstrm);
1618         if(value > UINT_MAX - 2)
1619         {
1620             return IHEVCD_INVALID_PARAMETER;
1621         }
1622         ps_sps->ai1_sps_max_latency_increase[i] = value;
1623     }
1624 
1625     /* Check if sps_max_dec_pic_buffering or sps_max_num_reorder_pics
1626        has changed */
1627     if(0 != ps_codec->u4_allocate_dynamic_done)
1628     {
1629         sps_t *ps_sps_old = ps_codec->s_parse.ps_sps;
1630         if(ps_sps_old->ai1_sps_max_dec_pic_buffering[ps_sps_old->i1_sps_max_sub_layers - 1] !=
1631                     ps_sps->ai1_sps_max_dec_pic_buffering[ps_sps->i1_sps_max_sub_layers - 1])
1632         {
1633             if(0 == ps_codec->i4_first_pic_done)
1634             {
1635                 return IHEVCD_INVALID_PARAMETER;
1636             }
1637             ps_codec->i4_reset_flag = 1;
1638             return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
1639         }
1640 
1641         if(ps_sps_old->ai1_sps_max_num_reorder_pics[ps_sps_old->i1_sps_max_sub_layers - 1] !=
1642                     ps_sps->ai1_sps_max_num_reorder_pics[ps_sps->i1_sps_max_sub_layers - 1])
1643         {
1644             if(0 == ps_codec->i4_first_pic_done)
1645             {
1646                 return IHEVCD_INVALID_PARAMETER;
1647             }
1648             ps_codec->i4_reset_flag = 1;
1649             return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
1650         }
1651     }
1652 
1653     UEV_PARSE("log2_min_coding_block_size_minus3", value, ps_bitstrm);
1654     if(value > (LOG2_MAX_CU_SIZE - 3))
1655     {
1656         return IHEVCD_INVALID_PARAMETER;
1657     }
1658     ps_sps->i1_log2_min_coding_block_size = value + 3;
1659     if((ps_sps->i2_pic_width_in_luma_samples % (1 << ps_sps->i1_log2_min_coding_block_size) != 0) ||
1660                     (ps_sps->i2_pic_height_in_luma_samples % (1 << ps_sps->i1_log2_min_coding_block_size) != 0))
1661     {
1662         return IHEVCD_INVALID_PARAMETER;
1663     }
1664 
1665     UEV_PARSE("log2_diff_max_min_coding_block_size", value, ps_bitstrm);
1666     if(value > (LOG2_MAX_CU_SIZE - ps_sps->i1_log2_min_coding_block_size))
1667     {
1668         return IHEVCD_INVALID_PARAMETER;
1669     }
1670     ps_sps->i1_log2_diff_max_min_coding_block_size = value;
1671 
1672     ctb_log2_size_y = ps_sps->i1_log2_min_coding_block_size + ps_sps->i1_log2_diff_max_min_coding_block_size;
1673 
1674     if((ctb_log2_size_y < LOG2_MIN_CTB_SIZE) || (ctb_log2_size_y > LOG2_MAX_CTB_SIZE))
1675     {
1676         return IHEVCD_INVALID_PARAMETER;
1677     }
1678     /* Check if CTB size is different in case of multiple SPS with same ID */
1679     {
1680         sps_t *ps_sps_old = (ps_codec->s_parse.ps_sps_base + sps_id);
1681         if(ps_sps_old->i1_sps_valid && ps_sps_old->i1_log2_ctb_size != ctb_log2_size_y)
1682         {
1683             if(0 == ps_codec->i4_first_pic_done)
1684             {
1685                 return IHEVCD_INVALID_PARAMETER;
1686             }
1687             ps_codec->i4_reset_flag = 1;
1688             return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
1689         }
1690     }
1691     ps_sps->i1_log2_ctb_size = ctb_log2_size_y;
1692 
1693     UEV_PARSE("log2_min_transform_block_size_minus2", value, ps_bitstrm);
1694     if(value > (LOG2_MAX_TU_SIZE - 2))
1695     {
1696         return IHEVCD_INVALID_PARAMETER;
1697     }
1698     ps_sps->i1_log2_min_transform_block_size = value + 2;
1699     if(ps_sps->i1_log2_min_transform_block_size >= ps_sps->i1_log2_min_coding_block_size)
1700     {
1701         return IHEVCD_INVALID_PARAMETER;
1702     }
1703 
1704     UEV_PARSE("log2_diff_max_min_transform_block_size", value, ps_bitstrm);
1705     if(value > (LOG2_MAX_TU_SIZE - ps_sps->i1_log2_min_transform_block_size))
1706     {
1707         return IHEVCD_INVALID_PARAMETER;
1708     }
1709     ps_sps->i1_log2_diff_max_min_transform_block_size = value;
1710 
1711     ps_sps->i1_log2_max_transform_block_size = ps_sps->i1_log2_min_transform_block_size +
1712                     ps_sps->i1_log2_diff_max_min_transform_block_size;
1713 
1714     if(ps_sps->i1_log2_max_transform_block_size > ps_sps->i1_log2_ctb_size)
1715     {
1716         return IHEVCD_INVALID_PARAMETER;
1717     }
1718 
1719     ps_sps->i1_log2_min_pcm_coding_block_size = 0;
1720     ps_sps->i1_log2_diff_max_min_pcm_coding_block_size = 0;
1721 
1722     UEV_PARSE("max_transform_hierarchy_depth_inter", value, ps_bitstrm);
1723     if(value > (ps_sps->i1_log2_ctb_size - ps_sps->i1_log2_min_transform_block_size))
1724     {
1725         return IHEVCD_INVALID_PARAMETER;
1726     }
1727     ps_sps->i1_max_transform_hierarchy_depth_inter = value;
1728 
1729     UEV_PARSE("max_transform_hierarchy_depth_intra", value, ps_bitstrm);
1730     if(value > (ps_sps->i1_log2_ctb_size - ps_sps->i1_log2_min_transform_block_size))
1731     {
1732         return IHEVCD_INVALID_PARAMETER;
1733     }
1734     ps_sps->i1_max_transform_hierarchy_depth_intra = value;
1735 
1736     /* String has a d (enabled) in order to match with HM */
1737     BITS_PARSE("scaling_list_enabled_flag", value, ps_bitstrm, 1);
1738     ps_sps->i1_scaling_list_enable_flag = value;
1739 
1740     if(ps_sps->i1_scaling_list_enable_flag)
1741     {
1742         COPY_DEFAULT_SCALING_LIST(ps_sps->pi2_scaling_mat);
1743         BITS_PARSE("sps_scaling_list_data_present_flag", value, ps_bitstrm, 1);
1744         ps_sps->i1_sps_scaling_list_data_present_flag = value;
1745 
1746         if(ps_sps->i1_sps_scaling_list_data_present_flag)
1747             ihevcd_scaling_list_data(ps_codec, ps_sps->pi2_scaling_mat);
1748     }
1749     else
1750     {
1751         COPY_FLAT_SCALING_LIST(ps_sps->pi2_scaling_mat);
1752     }
1753     /* String is asymmetric_motion_partitions_enabled_flag instead of amp_enabled_flag in order to match with HM */
1754     BITS_PARSE("asymmetric_motion_partitions_enabled_flag", value, ps_bitstrm, 1);
1755     ps_sps->i1_amp_enabled_flag = value;
1756 
1757     BITS_PARSE("sample_adaptive_offset_enabled_flag", value, ps_bitstrm, 1);
1758     ps_sps->i1_sample_adaptive_offset_enabled_flag = value;
1759 
1760     BITS_PARSE("pcm_enabled_flag", value, ps_bitstrm, 1);
1761     ps_sps->i1_pcm_enabled_flag = value;
1762 
1763     if(ps_sps->i1_pcm_enabled_flag)
1764     {
1765         BITS_PARSE("pcm_sample_bit_depth_luma", value, ps_bitstrm, 4);
1766         ps_sps->i1_pcm_sample_bit_depth_luma = value + 1;
1767 
1768         BITS_PARSE("pcm_sample_bit_depth_chroma", value, ps_bitstrm, 4);
1769         ps_sps->i1_pcm_sample_bit_depth_chroma = value + 1;
1770 
1771         UEV_PARSE("log2_min_pcm_coding_block_size_minus3", value, ps_bitstrm);
1772         if(value < (ps_sps->i1_log2_min_coding_block_size - 3) || value > (MIN(ctb_log2_size_y, 5) - 3))
1773         {
1774             return IHEVCD_INVALID_PARAMETER;
1775         }
1776         ps_sps->i1_log2_min_pcm_coding_block_size = value + 3;
1777 
1778         UEV_PARSE("log2_diff_max_min_pcm_coding_block_size", value, ps_bitstrm);
1779         if(value > MIN(ctb_log2_size_y, 5) - ps_sps->i1_log2_min_pcm_coding_block_size)
1780         {
1781             return IHEVCD_INVALID_PARAMETER;
1782         }
1783         ps_sps->i1_log2_diff_max_min_pcm_coding_block_size = value;
1784         BITS_PARSE("pcm_loop_filter_disable_flag", value, ps_bitstrm, 1);
1785         ps_sps->i1_pcm_loop_filter_disable_flag = value;
1786 
1787     }
1788     UEV_PARSE("num_short_term_ref_pic_sets", value, ps_bitstrm);
1789     if(value > MAX_STREF_PICS_SPS)
1790     {
1791         return IHEVCD_INVALID_PARAMETER;
1792     }
1793     ps_sps->i1_num_short_term_ref_pic_sets = value;
1794 
1795     for(i = 0; i < ps_sps->i1_num_short_term_ref_pic_sets; i++)
1796     {
1797         ret = ihevcd_short_term_ref_pic_set(ps_bitstrm, &ps_sps->as_stref_picset[0], ps_sps->i1_num_short_term_ref_pic_sets, i, &ps_sps->as_stref_picset[i]);
1798         if (ret != IHEVCD_SUCCESS)
1799         {
1800             return ret;
1801         }
1802     }
1803 
1804     BITS_PARSE("long_term_ref_pics_present_flag", value, ps_bitstrm, 1);
1805     ps_sps->i1_long_term_ref_pics_present_flag = value;
1806 
1807     if(ps_sps->i1_long_term_ref_pics_present_flag)
1808     {
1809         UEV_PARSE("num_long_term_ref_pics_sps", value, ps_bitstrm);
1810         if(value > MAX_LTREF_PICS_SPS)
1811         {
1812             return IHEVCD_INVALID_PARAMETER;
1813         }
1814         ps_sps->i1_num_long_term_ref_pics_sps = value;
1815 
1816         for(i = 0; i < ps_sps->i1_num_long_term_ref_pics_sps; i++)
1817         {
1818             BITS_PARSE("lt_ref_pic_poc_lsb_sps[ i ]", value, ps_bitstrm, ps_sps->i1_log2_max_pic_order_cnt_lsb);
1819             ps_sps->au2_lt_ref_pic_poc_lsb_sps[i] = value;
1820 
1821             BITS_PARSE("used_by_curr_pic_lt_sps_flag[ i ]", value, ps_bitstrm, 1);
1822             ps_sps->ai1_used_by_curr_pic_lt_sps_flag[i] = value;
1823         }
1824     }
1825 
1826     BITS_PARSE("sps_temporal_mvp_enable_flag", value, ps_bitstrm, 1);
1827     ps_sps->i1_sps_temporal_mvp_enable_flag = value;
1828 
1829     /* Print matches HM 8-2 */
1830     BITS_PARSE("sps_strong_intra_smoothing_enable_flag", value, ps_bitstrm, 1);
1831     ps_sps->i1_strong_intra_smoothing_enable_flag = value;
1832 
1833     BITS_PARSE("vui_parameters_present_flag", value, ps_bitstrm, 1);
1834     ps_sps->i1_vui_parameters_present_flag = value;
1835 
1836     if(ps_sps->i1_vui_parameters_present_flag)
1837     {
1838         ret = ihevcd_parse_vui_parameters(ps_bitstrm,
1839                                           &ps_sps->s_vui_parameters,
1840                                           ps_sps->i1_sps_max_sub_layers - 1);
1841         RETURN_IF((ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS), ret);
1842     }
1843 
1844     BITS_PARSE("sps_extension_flag", value, ps_bitstrm, 1);
1845 
1846     if((UWORD8 *)ps_bitstrm->pu4_buf > ps_bitstrm->pu1_buf_max)
1847     {
1848         return IHEVCD_INVALID_PARAMETER;
1849     }
1850 
1851     {
1852         WORD32 numerator;
1853         WORD32 ceil_offset;
1854 
1855         ceil_offset = (1 << ps_sps->i1_log2_ctb_size) - 1;
1856         numerator = ps_sps->i2_pic_width_in_luma_samples;
1857 
1858         ps_sps->i2_pic_wd_in_ctb = ((numerator + ceil_offset) /
1859                         (1 << ps_sps->i1_log2_ctb_size));
1860 
1861         numerator = ps_sps->i2_pic_height_in_luma_samples;
1862         ps_sps->i2_pic_ht_in_ctb = ((numerator + ceil_offset) /
1863                         (1 << ps_sps->i1_log2_ctb_size));
1864 
1865         ps_sps->i4_pic_size_in_ctb = ps_sps->i2_pic_ht_in_ctb *
1866                         ps_sps->i2_pic_wd_in_ctb;
1867 
1868         if(0 == ps_codec->i4_sps_done)
1869             ps_codec->s_parse.i4_next_ctb_indx = ps_sps->i4_pic_size_in_ctb;
1870 
1871         numerator = ps_sps->i2_pic_width_in_luma_samples;
1872         ps_sps->i2_pic_wd_in_min_cb = numerator  /
1873                         (1 << ps_sps->i1_log2_min_coding_block_size);
1874 
1875         numerator = ps_sps->i2_pic_height_in_luma_samples;
1876         ps_sps->i2_pic_ht_in_min_cb = numerator  /
1877                         (1 << ps_sps->i1_log2_min_coding_block_size);
1878     }
1879     if((0 != ps_codec->u4_allocate_dynamic_done) &&
1880                     ((ps_codec->i4_wd != ps_sps->i2_pic_width_in_luma_samples) ||
1881                     (ps_codec->i4_ht != ps_sps->i2_pic_height_in_luma_samples)))
1882     {
1883         if(0 == ps_codec->i4_first_pic_done)
1884         {
1885             return IHEVCD_INVALID_PARAMETER;
1886         }
1887         ps_codec->i4_reset_flag = 1;
1888         return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
1889     }
1890 
1891     if((ps_sps->i2_pic_width_in_luma_samples > MAX_WD) ||
1892                     ((ps_sps->i2_pic_width_in_luma_samples * ps_sps->i2_pic_height_in_luma_samples) >
1893                     (MAX_WD * MAX_HT)))
1894     {
1895         return (IHEVCD_ERROR_T)IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
1896     }
1897 
1898     /* Update display width and display height */
1899     {
1900         WORD32 disp_wd, disp_ht;
1901         WORD32 crop_unit_x, crop_unit_y;
1902         crop_unit_x = 1;
1903         crop_unit_y = 1;
1904 
1905         if(CHROMA_FMT_IDC_YUV420 == ps_sps->i1_chroma_format_idc)
1906         {
1907             crop_unit_x = 2;
1908             crop_unit_y = 2;
1909         }
1910 
1911         disp_wd = ps_sps->i2_pic_width_in_luma_samples;
1912         disp_wd -= ps_sps->i2_pic_crop_left_offset * crop_unit_x;
1913         disp_wd -= ps_sps->i2_pic_crop_right_offset * crop_unit_x;
1914 
1915 
1916         disp_ht = ps_sps->i2_pic_height_in_luma_samples;
1917         disp_ht -= ps_sps->i2_pic_crop_top_offset * crop_unit_y;
1918         disp_ht -= ps_sps->i2_pic_crop_bottom_offset * crop_unit_y;
1919 
1920         if((0 >= disp_wd) || (0 >= disp_ht))
1921             return IHEVCD_INVALID_PARAMETER;
1922 
1923         if((0 != ps_codec->u4_allocate_dynamic_done) &&
1924                             ((ps_codec->i4_disp_wd != disp_wd) ||
1925                             (ps_codec->i4_disp_ht != disp_ht)))
1926         {
1927             if(0 == ps_codec->i4_first_pic_done)
1928             {
1929                 return IHEVCD_INVALID_PARAMETER;
1930             }
1931             ps_codec->i4_reset_flag = 1;
1932             return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
1933         }
1934 
1935         ps_codec->i4_disp_wd = disp_wd;
1936         ps_codec->i4_disp_ht = disp_ht;
1937 
1938 
1939         ps_codec->i4_wd = ps_sps->i2_pic_width_in_luma_samples;
1940         ps_codec->i4_ht = ps_sps->i2_pic_height_in_luma_samples;
1941 
1942         {
1943             WORD32 ref_strd;
1944             ref_strd = ALIGN32(ps_sps->i2_pic_width_in_luma_samples + PAD_WD);
1945             if(ps_codec->i4_strd < ref_strd)
1946             {
1947                 ps_codec->i4_strd = ref_strd;
1948             }
1949         }
1950 
1951         if(0 == ps_codec->i4_share_disp_buf)
1952         {
1953             if(ps_codec->i4_disp_strd < ps_codec->i4_disp_wd)
1954             {
1955                 ps_codec->i4_disp_strd = ps_codec->i4_disp_wd;
1956             }
1957         }
1958         else
1959         {
1960             if(ps_codec->i4_disp_strd < ps_codec->i4_strd)
1961             {
1962                 ps_codec->i4_disp_strd = ps_codec->i4_strd;
1963             }
1964         }
1965     }
1966 
1967     /* This is used only during initialization to get reorder count etc */
1968     ps_codec->i4_sps_id = sps_id;
1969 
1970     ps_codec->i4_sps_done = 1;
1971     return ret;
1972 }
1973 
1974 
ihevcd_unmark_pps(codec_t * ps_codec,WORD32 sps_id)1975 void ihevcd_unmark_pps(codec_t *ps_codec, WORD32 sps_id)
1976 {
1977     WORD32 pps_id = 0;
1978     pps_t *ps_pps = ps_codec->ps_pps_base;
1979 
1980     for(pps_id = 0; pps_id < MAX_PPS_CNT - 1; pps_id++, ps_pps++)
1981     {
1982         if((ps_pps->i1_pps_valid) &&
1983                         (ps_pps->i1_sps_id == sps_id))
1984             ps_pps->i1_pps_valid = 0;
1985     }
1986 }
1987 
1988 
ihevcd_copy_sps(codec_t * ps_codec,WORD32 sps_id,WORD32 sps_id_ref)1989 void ihevcd_copy_sps(codec_t *ps_codec, WORD32 sps_id, WORD32 sps_id_ref)
1990 {
1991     sps_t *ps_sps, *ps_sps_ref;
1992     WORD16 *pi2_scaling_mat_backup;
1993     WORD32 scaling_mat_size;
1994 
1995     SCALING_MAT_SIZE(scaling_mat_size);
1996     ps_sps_ref = ps_codec->ps_sps_base + sps_id_ref;
1997     ps_sps = ps_codec->ps_sps_base + sps_id;
1998 
1999     if(ps_sps->i1_sps_valid)
2000     {
2001         if((ps_sps->i1_log2_ctb_size != ps_sps_ref->i1_log2_ctb_size) ||
2002                         (ps_sps->i2_pic_wd_in_ctb != ps_sps_ref->i2_pic_wd_in_ctb) ||
2003                         (ps_sps->i2_pic_ht_in_ctb != ps_sps_ref->i2_pic_ht_in_ctb))
2004         {
2005             ihevcd_unmark_pps(ps_codec, sps_id);
2006         }
2007     }
2008 
2009     pi2_scaling_mat_backup = ps_sps->pi2_scaling_mat;
2010 
2011     memcpy(ps_sps, ps_sps_ref, sizeof(sps_t));
2012     ps_sps->pi2_scaling_mat = pi2_scaling_mat_backup;
2013     memcpy(ps_sps->pi2_scaling_mat, ps_sps_ref->pi2_scaling_mat, scaling_mat_size * sizeof(WORD16));
2014     ps_sps->i1_sps_valid = 1;
2015 
2016     ps_codec->s_parse.ps_sps = ps_sps;
2017 }
2018 
2019 
2020 /**
2021 *******************************************************************************
2022 *
2023 * @brief
2024 *  Parses PPS (Picture Parameter Set)
2025 *
2026 * @par Description:
2027 *  Parse Picture Parameter Set as per section  Section: 7.3.2.3
2028 * The pps is written to a temporary buffer and copied later to the
2029 * appropriate location
2030 *
2031 * @param[in] ps_codec
2032 *  Pointer to codec context
2033 *
2034 * @returns Error code from IHEVCD_ERROR_T
2035 *
2036 * @remarks
2037 *
2038 *
2039 *******************************************************************************
2040 */
ihevcd_parse_pps(codec_t * ps_codec)2041 IHEVCD_ERROR_T ihevcd_parse_pps(codec_t *ps_codec)
2042 {
2043     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2044     UWORD32 value;
2045     WORD32 i4_value;
2046     WORD32 pps_id;
2047 
2048     pps_t *ps_pps;
2049     sps_t *ps_sps;
2050     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
2051 
2052 
2053     if(0 == ps_codec->i4_sps_done)
2054         return IHEVCD_INVALID_HEADER;
2055 
2056     UEV_PARSE("pic_parameter_set_id", value, ps_bitstrm);
2057 
2058     pps_id = value;
2059     if((pps_id >= MAX_PPS_CNT) || (pps_id < 0))
2060     {
2061         if(ps_codec->i4_pps_done)
2062             return IHEVCD_UNSUPPORTED_PPS_ID;
2063         else
2064             pps_id = 0;
2065     }
2066 
2067 
2068     ps_pps = (ps_codec->s_parse.ps_pps_base + MAX_PPS_CNT - 1);
2069 
2070     ps_pps->i1_pps_id = pps_id;
2071 
2072     UEV_PARSE("seq_parameter_set_id", value, ps_bitstrm);
2073     if(value > MAX_SPS_CNT - 2)
2074     {
2075         return IHEVCD_INVALID_PARAMETER;
2076     }
2077     ps_pps->i1_sps_id = value;
2078 
2079     ps_sps = (ps_codec->s_parse.ps_sps_base + ps_pps->i1_sps_id);
2080 
2081     /* If the SPS that is being referred to has not been parsed,
2082      * copy an existing SPS to the current location */
2083     if(0 == ps_sps->i1_sps_valid)
2084     {
2085         return IHEVCD_INVALID_HEADER;
2086 
2087 /*
2088         sps_t *ps_sps_ref = ps_codec->ps_sps_base;
2089         while(0 == ps_sps_ref->i1_sps_valid)
2090             ps_sps_ref++;
2091         ihevcd_copy_sps(ps_codec, ps_pps->i1_sps_id, ps_sps_ref->i1_sps_id);
2092 */
2093     }
2094 
2095     BITS_PARSE("dependent_slices_enabled_flag", value, ps_bitstrm, 1);
2096     ps_pps->i1_dependent_slice_enabled_flag = value;
2097 
2098     BITS_PARSE("output_flag_present_flag", value, ps_bitstrm, 1);
2099     ps_pps->i1_output_flag_present_flag = value;
2100 
2101     BITS_PARSE("num_extra_slice_header_bits", value, ps_bitstrm, 3);
2102     ps_pps->i1_num_extra_slice_header_bits = value;
2103 
2104 
2105     BITS_PARSE("sign_data_hiding_flag", value, ps_bitstrm, 1);
2106     ps_pps->i1_sign_data_hiding_flag = value;
2107 
2108     BITS_PARSE("cabac_init_present_flag", value, ps_bitstrm, 1);
2109     ps_pps->i1_cabac_init_present_flag = value;
2110 
2111     UEV_PARSE("num_ref_idx_l0_default_active_minus1", value, ps_bitstrm);
2112     if(value > MAX_DPB_SIZE - 2)
2113     {
2114          return IHEVCD_INVALID_PARAMETER;
2115     }
2116     ps_pps->i1_num_ref_idx_l0_default_active = value + 1;
2117 
2118     UEV_PARSE("num_ref_idx_l1_default_active_minus1", value, ps_bitstrm);
2119     if(value > MAX_DPB_SIZE - 2)
2120     {
2121         return IHEVCD_INVALID_PARAMETER;
2122     }
2123     ps_pps->i1_num_ref_idx_l1_default_active = value + 1;
2124 
2125     SEV_PARSE("pic_init_qp_minus26", i4_value, ps_bitstrm);
2126     if(i4_value < -26 || i4_value > 25)
2127     {
2128         return IHEVCD_INVALID_PARAMETER;
2129     }
2130     ps_pps->i1_pic_init_qp = i4_value + 26;
2131 
2132     BITS_PARSE("constrained_intra_pred_flag", value, ps_bitstrm, 1);
2133     ps_pps->i1_constrained_intra_pred_flag = value;
2134 
2135     BITS_PARSE("transform_skip_enabled_flag", value, ps_bitstrm, 1);
2136     ps_pps->i1_transform_skip_enabled_flag = value;
2137 
2138     BITS_PARSE("cu_qp_delta_enabled_flag", value, ps_bitstrm, 1);
2139     ps_pps->i1_cu_qp_delta_enabled_flag = value;
2140 
2141     if(ps_pps->i1_cu_qp_delta_enabled_flag)
2142     {
2143         UEV_PARSE("diff_cu_qp_delta_depth", value, ps_bitstrm);
2144         if(value > ps_sps->i1_log2_diff_max_min_coding_block_size)
2145         {
2146             return IHEVCD_INVALID_PARAMETER;
2147         }
2148         ps_pps->i1_diff_cu_qp_delta_depth = value;
2149     }
2150     else
2151     {
2152         ps_pps->i1_diff_cu_qp_delta_depth = 0;
2153     }
2154     ps_pps->i1_log2_min_cu_qp_delta_size = ps_sps->i1_log2_ctb_size - ps_pps->i1_diff_cu_qp_delta_depth;
2155     /* Print different */
2156     SEV_PARSE("cb_qp_offset", i4_value, ps_bitstrm);
2157     if(i4_value < -12 || i4_value > 12)
2158     {
2159         return IHEVCD_INVALID_PARAMETER;
2160     }
2161     ps_pps->i1_pic_cb_qp_offset = i4_value;
2162 
2163     /* Print different */
2164     SEV_PARSE("cr_qp_offset", i4_value, ps_bitstrm);
2165     if(i4_value < -12 || i4_value > 12)
2166     {
2167         return IHEVCD_INVALID_PARAMETER;
2168     }
2169     ps_pps->i1_pic_cr_qp_offset = i4_value;
2170 
2171     /* Print different */
2172     BITS_PARSE("slicelevel_chroma_qp_flag", value, ps_bitstrm, 1);
2173     ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag = value;
2174 
2175     BITS_PARSE("weighted_pred_flag", value, ps_bitstrm, 1);
2176     ps_pps->i1_weighted_pred_flag = value;
2177 
2178     BITS_PARSE("weighted_bipred_flag", value, ps_bitstrm, 1);
2179     ps_pps->i1_weighted_bipred_flag = value;
2180 
2181     BITS_PARSE("transquant_bypass_enable_flag", value, ps_bitstrm, 1);
2182     ps_pps->i1_transquant_bypass_enable_flag = value;
2183 
2184     BITS_PARSE("tiles_enabled_flag", value, ps_bitstrm, 1);
2185     ps_pps->i1_tiles_enabled_flag = value;
2186 
2187     /* When tiles are enabled and width or height is >= 4096,
2188      * CTB Size should at least be 32 while if width or height is >= 8192,
2189      * CTB Size should at least be 64 and so on. 16x16 CTBs can result
2190      * in tile position greater than 255 for 4096 while 32x32 CTBs can result
2191      * in tile position greater than 255 for 8192,
2192      * which decoder does not support.
2193      */
2194     if (ps_pps->i1_tiles_enabled_flag)
2195     {
2196         if((ps_sps->i1_log2_ctb_size == 4) &&
2197             ((ps_sps->i2_pic_width_in_luma_samples >= 4096) ||
2198             (ps_sps->i2_pic_height_in_luma_samples >= 4096)))
2199         {
2200             return IHEVCD_INVALID_HEADER;
2201         }
2202         if((ps_sps->i1_log2_ctb_size == 5) &&
2203             ((ps_sps->i2_pic_width_in_luma_samples >= 8192) ||
2204             (ps_sps->i2_pic_height_in_luma_samples >= 8192)))
2205         {
2206             return IHEVCD_INVALID_HEADER;
2207         }
2208         if((ps_sps->i1_log2_ctb_size == 6) &&
2209             ((ps_sps->i2_pic_width_in_luma_samples >= 16384) ||
2210             (ps_sps->i2_pic_height_in_luma_samples >= 16384)))
2211         {
2212             return IHEVCD_INVALID_HEADER;
2213         }
2214     }
2215 
2216     BITS_PARSE("entropy_coding_sync_enabled_flag", value, ps_bitstrm, 1);
2217     ps_pps->i1_entropy_coding_sync_enabled_flag = value;
2218 
2219     ps_pps->i1_loop_filter_across_tiles_enabled_flag = 0;
2220     if(ps_pps->i1_tiles_enabled_flag)
2221     {
2222         WORD32 wd = ALIGN64(ps_codec->i4_wd);
2223         WORD32 ht = ALIGN64(ps_codec->i4_ht);
2224 
2225         WORD32 max_tile_cols = (wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
2226         WORD32 max_tile_rows = (ht + MIN_TILE_HT - 1) / MIN_TILE_HT;
2227 
2228         UEV_PARSE("num_tile_columns_minus1", value, ps_bitstrm);
2229         ps_pps->i1_num_tile_columns = value + 1;
2230 
2231         UEV_PARSE("num_tile_rows_minus1", value, ps_bitstrm);
2232         ps_pps->i1_num_tile_rows = value + 1;
2233 
2234         if((ps_pps->i1_num_tile_columns < 1) ||
2235                         (ps_pps->i1_num_tile_columns > max_tile_cols) ||
2236                         (ps_pps->i1_num_tile_rows < 1) ||
2237                         (ps_pps->i1_num_tile_rows > max_tile_rows))
2238             return IHEVCD_INVALID_HEADER;
2239 
2240         BITS_PARSE("uniform_spacing_flag", value, ps_bitstrm, 1);
2241         ps_pps->i1_uniform_spacing_flag = value;
2242 
2243 
2244         {
2245 
2246             UWORD32 start;
2247             WORD32 i, j;
2248 
2249 
2250             start = 0;
2251             for(i = 0; i < ps_pps->i1_num_tile_columns; i++)
2252             {
2253                 tile_t *ps_tile;
2254                 if(!ps_pps->i1_uniform_spacing_flag)
2255                 {
2256                     if(i < (ps_pps->i1_num_tile_columns - 1))
2257                     {
2258                         UEV_PARSE("column_width_minus1[ i ]", value, ps_bitstrm);
2259                         value += 1;
2260                         if (value >= ps_sps->i2_pic_wd_in_ctb - start)
2261                         {
2262                             return IHEVCD_INVALID_HEADER;
2263                         }
2264                     }
2265                     else
2266                     {
2267                         value = ps_sps->i2_pic_wd_in_ctb - start;
2268                     }
2269                 }
2270                 else
2271                 {
2272                     value = ((i + 1) * ps_sps->i2_pic_wd_in_ctb) / ps_pps->i1_num_tile_columns -
2273                                     (i * ps_sps->i2_pic_wd_in_ctb) / ps_pps->i1_num_tile_columns;
2274                 }
2275 
2276                 for(j = 0; j < ps_pps->i1_num_tile_rows; j++)
2277                 {
2278                     ps_tile = ps_pps->ps_tile + j * ps_pps->i1_num_tile_columns + i;
2279                     ps_tile->u1_pos_x = start;
2280                     ps_tile->u2_wd = value;
2281                 }
2282                 start += value;
2283 
2284                 if((start > ps_sps->i2_pic_wd_in_ctb) ||
2285                                 (value == 0))
2286                     return IHEVCD_INVALID_HEADER;
2287             }
2288 
2289             start = 0;
2290             for(i = 0; i < (ps_pps->i1_num_tile_rows); i++)
2291             {
2292                 tile_t *ps_tile;
2293                 if(!ps_pps->i1_uniform_spacing_flag)
2294                 {
2295                     if(i < (ps_pps->i1_num_tile_rows - 1))
2296                     {
2297 
2298                         UEV_PARSE("row_height_minus1[ i ]", value, ps_bitstrm);
2299                         value += 1;
2300                         if (value >= ps_sps->i2_pic_ht_in_ctb - start)
2301                         {
2302                             return IHEVCD_INVALID_HEADER;
2303                         }
2304                     }
2305                     else
2306                     {
2307                         value = ps_sps->i2_pic_ht_in_ctb - start;
2308                     }
2309                 }
2310                 else
2311                 {
2312                     value = ((i + 1) * ps_sps->i2_pic_ht_in_ctb) / ps_pps->i1_num_tile_rows -
2313                                     (i * ps_sps->i2_pic_ht_in_ctb) / ps_pps->i1_num_tile_rows;
2314                 }
2315 
2316                 for(j = 0; j < ps_pps->i1_num_tile_columns; j++)
2317                 {
2318                     ps_tile = ps_pps->ps_tile + i * ps_pps->i1_num_tile_columns + j;
2319                     ps_tile->u1_pos_y = start;
2320                     ps_tile->u2_ht = value;
2321                 }
2322                 start += value;
2323 
2324                 if((start > ps_sps->i2_pic_ht_in_ctb) ||
2325                                 (value == 0))
2326                     return IHEVCD_INVALID_HEADER;
2327             }
2328         }
2329 
2330 
2331         BITS_PARSE("loop_filter_across_tiles_enabled_flag", value, ps_bitstrm, 1);
2332         ps_pps->i1_loop_filter_across_tiles_enabled_flag = value;
2333 
2334     }
2335     else
2336     {
2337         /* If tiles are not present, set first tile in each PPS to have tile
2338         width and height equal to picture width and height */
2339         ps_pps->i1_num_tile_columns = 1;
2340         ps_pps->i1_num_tile_rows = 1;
2341         ps_pps->i1_uniform_spacing_flag = 1;
2342 
2343         ps_pps->ps_tile->u1_pos_x = 0;
2344         ps_pps->ps_tile->u1_pos_y = 0;
2345         ps_pps->ps_tile->u2_wd = ps_sps->i2_pic_wd_in_ctb;
2346         ps_pps->ps_tile->u2_ht = ps_sps->i2_pic_ht_in_ctb;
2347     }
2348 
2349     BITS_PARSE("loop_filter_across_slices_enabled_flag", value, ps_bitstrm, 1);
2350     ps_pps->i1_loop_filter_across_slices_enabled_flag = value;
2351 
2352     BITS_PARSE("deblocking_filter_control_present_flag", value, ps_bitstrm, 1);
2353     ps_pps->i1_deblocking_filter_control_present_flag = value;
2354 
2355     /* Default values */
2356     ps_pps->i1_pic_disable_deblocking_filter_flag = 0;
2357     ps_pps->i1_deblocking_filter_override_enabled_flag = 0;
2358     ps_pps->i1_beta_offset_div2 = 0;
2359     ps_pps->i1_tc_offset_div2 = 0;
2360 
2361     if(ps_pps->i1_deblocking_filter_control_present_flag)
2362     {
2363 
2364         BITS_PARSE("deblocking_filter_override_enabled_flag", value, ps_bitstrm, 1);
2365         ps_pps->i1_deblocking_filter_override_enabled_flag = value;
2366 
2367         BITS_PARSE("pic_disable_deblocking_filter_flag", value, ps_bitstrm, 1);
2368         ps_pps->i1_pic_disable_deblocking_filter_flag = value;
2369 
2370         if(!ps_pps->i1_pic_disable_deblocking_filter_flag)
2371         {
2372 
2373             SEV_PARSE("pps_beta_offset_div2", i4_value, ps_bitstrm);
2374             if(i4_value < -6 || i4_value > 6)
2375             {
2376                 return IHEVCD_INVALID_PARAMETER;
2377             }
2378             ps_pps->i1_beta_offset_div2 = i4_value;
2379 
2380             SEV_PARSE("pps_tc_offset_div2", i4_value, ps_bitstrm);
2381             if(i4_value < -6 || i4_value > 6)
2382             {
2383                 return IHEVCD_INVALID_PARAMETER;
2384             }
2385             ps_pps->i1_tc_offset_div2 = i4_value;
2386 
2387         }
2388     }
2389 
2390     BITS_PARSE("pps_scaling_list_data_present_flag", value, ps_bitstrm, 1);
2391     ps_pps->i1_pps_scaling_list_data_present_flag = value;
2392 
2393     if(ps_pps->i1_pps_scaling_list_data_present_flag)
2394     {
2395         COPY_DEFAULT_SCALING_LIST(ps_pps->pi2_scaling_mat);
2396         ihevcd_scaling_list_data(ps_codec, ps_pps->pi2_scaling_mat);
2397     }
2398 
2399     BITS_PARSE("lists_modification_present_flag", value, ps_bitstrm, 1);
2400     ps_pps->i1_lists_modification_present_flag = value;
2401     UEV_PARSE("log2_parallel_merge_level_minus2", value, ps_bitstrm);
2402     if(value > (ps_sps->i1_log2_min_coding_block_size + ps_sps->i1_log2_diff_max_min_coding_block_size))
2403     {
2404         return IHEVCD_INVALID_PARAMETER;
2405     }
2406     ps_pps->i1_log2_parallel_merge_level = value + 2;
2407 
2408     BITS_PARSE("slice_header_extension_present_flag", value, ps_bitstrm, 1);
2409     ps_pps->i1_slice_header_extension_present_flag = value;
2410     /* Not present in HM */
2411     BITS_PARSE("pps_extension_flag", value, ps_bitstrm, 1);
2412 
2413     if((UWORD8 *)ps_bitstrm->pu4_buf > ps_bitstrm->pu1_buf_max)
2414         return IHEVCD_INVALID_PARAMETER;
2415 
2416     ps_codec->i4_pps_done = 1;
2417     return ret;
2418 }
2419 
2420 
ihevcd_copy_pps(codec_t * ps_codec,WORD32 pps_id,WORD32 pps_id_ref)2421 void ihevcd_copy_pps(codec_t *ps_codec, WORD32 pps_id, WORD32 pps_id_ref)
2422 {
2423     pps_t *ps_pps, *ps_pps_ref;
2424     WORD16 *pi2_scaling_mat_backup;
2425     WORD32 scaling_mat_size;
2426     tile_t *ps_tile_backup;
2427     WORD32 max_tile_cols, max_tile_rows;
2428     WORD32 wd, ht;
2429     wd = ALIGN64(ps_codec->i4_wd);
2430     ht = ALIGN64(ps_codec->i4_ht);
2431 
2432     SCALING_MAT_SIZE(scaling_mat_size);
2433     max_tile_cols = (wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
2434     max_tile_rows = (ht + MIN_TILE_HT - 1) / MIN_TILE_HT;
2435 
2436     ps_pps_ref = ps_codec->ps_pps_base + pps_id_ref;
2437     ps_pps = ps_codec->ps_pps_base + pps_id;
2438 
2439     pi2_scaling_mat_backup = ps_pps->pi2_scaling_mat;
2440     ps_tile_backup = ps_pps->ps_tile;
2441 
2442     memcpy(ps_pps, ps_pps_ref, sizeof(pps_t));
2443     ps_pps->pi2_scaling_mat = pi2_scaling_mat_backup;
2444     ps_pps->ps_tile = ps_tile_backup;
2445     memcpy(ps_pps->pi2_scaling_mat, ps_pps_ref->pi2_scaling_mat, scaling_mat_size * sizeof(WORD16));
2446     memcpy(ps_pps->ps_tile, ps_pps_ref->ps_tile, max_tile_cols * max_tile_rows * sizeof(tile_t));
2447 
2448     ps_pps->i1_pps_valid = 1;
2449 
2450     ps_codec->s_parse.ps_pps = ps_pps;
2451 }
2452 
2453 
ihevcd_parse_buffering_period_sei(codec_t * ps_codec,sps_t * ps_sps)2454 IHEVCD_ERROR_T ihevcd_parse_buffering_period_sei(codec_t *ps_codec,
2455                                                  sps_t *ps_sps)
2456 {
2457     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2458     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2459     UWORD32 value;
2460     vui_t *ps_vui;
2461     buf_period_sei_params_t *ps_buf_period_sei_params;
2462     UWORD32 i;
2463     hrd_params_t *ps_vui_hdr;
2464     UWORD32 u4_cpb_cnt;
2465 
2466     ps_vui = &ps_sps->s_vui_parameters;
2467     ps_vui_hdr = &ps_vui->s_vui_hrd_parameters;
2468 
2469     ps_buf_period_sei_params = &ps_parse->s_sei_params.s_buf_period_sei_params;
2470 
2471     ps_parse->s_sei_params.i1_buf_period_params_present_flag = 1;
2472 
2473     UEV_PARSE("bp_seq_parameter_set_id", value, ps_bitstrm);
2474     if(value > MAX_SPS_CNT - 2)
2475     {
2476         return IHEVCD_INVALID_PARAMETER;
2477     }
2478     ps_buf_period_sei_params->u1_bp_seq_parameter_set_id = value;
2479 
2480     if(!ps_vui_hdr->u1_sub_pic_cpb_params_present_flag)
2481     {
2482         BITS_PARSE("irap_cpb_params_present_flag", value, ps_bitstrm, 1);
2483         ps_buf_period_sei_params->u1_rap_cpb_params_present_flag = value;
2484     }
2485 
2486     if(ps_buf_period_sei_params->u1_rap_cpb_params_present_flag)
2487     {
2488         BITS_PARSE("cpb_delay_offset",
2489                    value,
2490                    ps_bitstrm,
2491                    (ps_vui_hdr->u1_au_cpb_removal_delay_length_minus1
2492                                    + 1));
2493         ps_buf_period_sei_params->u4_cpb_delay_offset = value;
2494 
2495         BITS_PARSE("dpb_delay_offset",
2496                    value,
2497                    ps_bitstrm,
2498                    (ps_vui_hdr->u1_dpb_output_delay_length_minus1
2499                                    + 1));
2500         ps_buf_period_sei_params->u4_dpb_delay_offset = value;
2501     }
2502     else
2503     {
2504         ps_buf_period_sei_params->u4_cpb_delay_offset = 0;
2505         ps_buf_period_sei_params->u4_dpb_delay_offset = 0;
2506     }
2507 
2508     BITS_PARSE("concatenation_flag", value, ps_bitstrm, 1);
2509     ps_buf_period_sei_params->u1_concatenation_flag = value;
2510 
2511     BITS_PARSE("au_cpb_removal_delay_delta_minus1",
2512                value,
2513                ps_bitstrm,
2514                (ps_vui_hdr->u1_au_cpb_removal_delay_length_minus1
2515                                + 1));
2516     ps_buf_period_sei_params->u4_au_cpb_removal_delay_delta_minus1 = value;
2517 
2518     if(ps_vui_hdr->u1_nal_hrd_parameters_present_flag)
2519     {
2520         u4_cpb_cnt = ps_vui_hdr->au1_cpb_cnt_minus1[0];
2521 
2522         for(i = 0; i <= u4_cpb_cnt; i++)
2523         {
2524             BITS_PARSE("nal_initial_cpb_removal_delay[i]",
2525                        value,
2526                        ps_bitstrm,
2527                        (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2528                                        + 1));
2529             ps_buf_period_sei_params->au4_nal_initial_cpb_removal_delay[i] =
2530                             value;
2531 
2532             BITS_PARSE("nal_initial_cpb_removal_delay_offset",
2533                        value,
2534                        ps_bitstrm,
2535                        (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2536                                        + 1));
2537             ps_buf_period_sei_params->au4_nal_initial_cpb_removal_delay_offset[i] =
2538                             value;
2539 
2540             if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag
2541                             || ps_buf_period_sei_params->u1_rap_cpb_params_present_flag)
2542             {
2543                 BITS_PARSE("nal_initial_alt_cpb_removal_delay[i]",
2544                            value,
2545                            ps_bitstrm,
2546                            (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2547                                            + 1));
2548                 ps_buf_period_sei_params->au4_nal_initial_alt_cpb_removal_delay[i] =
2549                                 value;
2550 
2551                 BITS_PARSE("nal_initial_alt_cpb_removal_delay_offset",
2552                            value,
2553                            ps_bitstrm,
2554                            (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2555                                            + 1));
2556                 ps_buf_period_sei_params->au4_nal_initial_alt_cpb_removal_delay_offset[i] =
2557                                 value;
2558             }
2559         }
2560     }
2561 
2562     if(ps_vui_hdr->u1_vcl_hrd_parameters_present_flag)
2563     {
2564         u4_cpb_cnt = ps_vui_hdr->au1_cpb_cnt_minus1[0];
2565 
2566         for(i = 0; i <= u4_cpb_cnt; i++)
2567         {
2568             BITS_PARSE("vcl_initial_cpb_removal_delay[i]",
2569                        value,
2570                        ps_bitstrm,
2571                        (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2572                                        + 1));
2573             ps_buf_period_sei_params->au4_vcl_initial_cpb_removal_delay[i] =
2574                             value;
2575 
2576             BITS_PARSE("vcl_initial_cpb_removal_delay_offset",
2577                        value,
2578                        ps_bitstrm,
2579                        (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2580                                        + 1));
2581             ps_buf_period_sei_params->au4_vcl_initial_cpb_removal_delay_offset[i] =
2582                             value;
2583 
2584             if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag
2585                             || ps_buf_period_sei_params->u1_rap_cpb_params_present_flag)
2586             {
2587                 BITS_PARSE("vcl_initial_alt_cpb_removal_delay[i]",
2588                            value,
2589                            ps_bitstrm,
2590                            (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2591                                            + 1));
2592                 ps_buf_period_sei_params->au4_vcl_initial_alt_cpb_removal_delay[i] =
2593                                 value;
2594 
2595                 BITS_PARSE("vcl_initial_alt_cpb_removal_delay_offset",
2596                            value,
2597                            ps_bitstrm,
2598                            (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2599                                            + 1));
2600                 ps_buf_period_sei_params->au4_vcl_initial_alt_cpb_removal_delay_offset[i] =
2601                                 value;
2602             }
2603         }
2604     }
2605 
2606     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2607 }
2608 
ihevcd_parse_pic_timing_sei(codec_t * ps_codec,sps_t * ps_sps)2609 IHEVCD_ERROR_T ihevcd_parse_pic_timing_sei(codec_t *ps_codec, sps_t *ps_sps)
2610 {
2611     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2612     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2613     UWORD32 value;
2614     vui_t *ps_vui;
2615     UWORD32 i;
2616     hrd_params_t *ps_vui_hdr;
2617     UWORD32 u4_cpb_dpb_delays_present_flag = 0;
2618     pic_timing_sei_params_t *ps_pic_timing;
2619 
2620     ps_pic_timing = &ps_parse->s_sei_params.s_pic_timing_sei_params;
2621     ps_vui = &ps_sps->s_vui_parameters;
2622     ps_vui_hdr = &ps_vui->s_vui_hrd_parameters;
2623     ps_parse->s_sei_params.i1_pic_timing_params_present_flag = 1;
2624     if(ps_vui->u1_frame_field_info_present_flag)
2625     {
2626         BITS_PARSE("pic_struct", value, ps_bitstrm, 4);
2627         ps_pic_timing->u4_pic_struct = value;
2628 
2629         BITS_PARSE("source_scan_type", value, ps_bitstrm, 2);
2630         ps_pic_timing->u4_source_scan_type = value;
2631 
2632         BITS_PARSE("duplicate_flag", value, ps_bitstrm, 1);
2633         ps_pic_timing->u1_duplicate_flag = value;
2634     }
2635 
2636     if(ps_vui_hdr->u1_nal_hrd_parameters_present_flag
2637                     || ps_vui_hdr->u1_vcl_hrd_parameters_present_flag)
2638     {
2639         u4_cpb_dpb_delays_present_flag = 1;
2640     }
2641     else
2642     {
2643         u4_cpb_dpb_delays_present_flag = 0;
2644     }
2645 
2646     if(u4_cpb_dpb_delays_present_flag)
2647     {
2648         BITS_PARSE("au_cpb_removal_delay_minus1", value, ps_bitstrm,
2649                    (ps_vui_hdr->u1_au_cpb_removal_delay_length_minus1 + 1));
2650         ps_pic_timing->u4_au_cpb_removal_delay_minus1 = value;
2651 
2652         BITS_PARSE("pic_dpb_output_delay", value, ps_bitstrm,
2653                    (ps_vui_hdr->u1_dpb_output_delay_length_minus1 + 1));
2654         ps_pic_timing->u4_pic_dpb_output_delay = value;
2655 
2656         if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag)
2657         {
2658             BITS_PARSE("pic_dpb_output_du_delay", value, ps_bitstrm,
2659                        (ps_vui_hdr->u1_dpb_output_delay_du_length_minus1 + 1));
2660             ps_pic_timing->u4_pic_dpb_output_du_delay = value;
2661         }
2662 
2663         if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag
2664                         && ps_vui_hdr->u1_sub_pic_cpb_params_in_pic_timing_sei_flag)
2665         {
2666             UWORD32 num_units_minus1;
2667             UWORD32 array_size;
2668 
2669             UEV_PARSE("num_decoding_units_minus1", value, ps_bitstrm);
2670             if(value > (ps_sps->i4_pic_size_in_ctb -1))
2671             {
2672                 return IHEVCD_INVALID_PARAMETER;
2673             }
2674             ps_pic_timing->u4_num_decoding_units_minus1 = value;
2675 
2676             num_units_minus1 = ps_pic_timing->u4_num_decoding_units_minus1;
2677             array_size = (sizeof(ps_pic_timing->au4_num_nalus_in_du_minus1)
2678                        / sizeof(ps_pic_timing->au4_num_nalus_in_du_minus1[0]));
2679             num_units_minus1 = CLIP3(num_units_minus1, 0,(array_size - 1));
2680             ps_pic_timing->u4_num_decoding_units_minus1 = num_units_minus1;
2681 
2682             BITS_PARSE("du_common_cpb_removal_delay_flag", value, ps_bitstrm, 1);
2683             ps_pic_timing->u1_du_common_cpb_removal_delay_flag = value;
2684 
2685             if(ps_pic_timing->u1_du_common_cpb_removal_delay_flag)
2686             {
2687                 BITS_PARSE("du_common_cpb_removal_delay_increment_minus1",
2688                            value,
2689                            ps_bitstrm,
2690                            (ps_vui_hdr->u1_du_cpb_removal_delay_increment_length_minus1
2691                                            + 1));
2692                 ps_pic_timing->u4_du_common_cpb_removal_delay_increment_minus1 =
2693                                 value;
2694             }
2695 
2696             for(i = 0; i <= ps_pic_timing->u4_num_decoding_units_minus1; i++)
2697             {
2698                 UEV_PARSE("num_nalus_in_du_minus1", value, ps_bitstrm);
2699                 if(value > (ps_sps->i4_pic_size_in_ctb -1))
2700                 {
2701                     return IHEVCD_INVALID_PARAMETER;
2702                 }
2703                 ps_pic_timing->au4_num_nalus_in_du_minus1[i] = value;
2704 
2705                 if((!ps_pic_timing->u1_du_common_cpb_removal_delay_flag)
2706                                 && (i < ps_pic_timing->u4_num_decoding_units_minus1))
2707                 {
2708                     BITS_PARSE("du_common_cpb_removal_delay_increment_minus1",
2709                                value,
2710                                ps_bitstrm,
2711                                (ps_vui_hdr->u1_du_cpb_removal_delay_increment_length_minus1
2712                                                + 1));
2713                     ps_pic_timing->au4_du_cpb_removal_delay_increment_minus1[i] =
2714                                     value;
2715                 }
2716             }
2717         }
2718     }
2719 
2720     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2721 }
2722 
ihevcd_parse_time_code_sei(codec_t * ps_codec)2723 IHEVCD_ERROR_T ihevcd_parse_time_code_sei(codec_t *ps_codec)
2724 {
2725     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2726     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2727     UWORD32 value;
2728     time_code_t *ps_time_code;
2729     WORD32 i;
2730 
2731     ps_parse->s_sei_params.i1_time_code_present_flag = 1;
2732     ps_time_code = &ps_parse->s_sei_params.s_time_code;
2733 
2734     BITS_PARSE("num_clock_ts", value, ps_bitstrm, 2);
2735     ps_time_code->u1_num_clock_ts = value;
2736 
2737     for(i = 0; i < ps_time_code->u1_num_clock_ts; i++)
2738     {
2739         BITS_PARSE("clock_timestamp_flag[i]", value, ps_bitstrm, 1);
2740         ps_time_code->au1_clock_timestamp_flag[i] = value;
2741 
2742         if(ps_time_code->au1_clock_timestamp_flag[i])
2743         {
2744             BITS_PARSE("units_field_based_flag[i]", value, ps_bitstrm, 1);
2745             ps_time_code->au1_units_field_based_flag[i] = value;
2746 
2747             BITS_PARSE("counting_type[i]", value, ps_bitstrm, 5);
2748             ps_time_code->au1_counting_type[i] = value;
2749 
2750             BITS_PARSE("full_timestamp_flag[i]", value, ps_bitstrm, 1);
2751             ps_time_code->au1_full_timestamp_flag[i] = value;
2752 
2753             BITS_PARSE("discontinuity_flag[i]", value, ps_bitstrm, 1);
2754             ps_time_code->au1_discontinuity_flag[i] = value;
2755 
2756             BITS_PARSE("cnt_dropped_flag[i]", value, ps_bitstrm, 1);
2757             ps_time_code->au1_cnt_dropped_flag[i] = value;
2758 
2759             BITS_PARSE("n_frames[i]", value, ps_bitstrm, 9);
2760             ps_time_code->au2_n_frames[i] = value;
2761 
2762             if(ps_time_code->au1_full_timestamp_flag[i])
2763             {
2764                 BITS_PARSE("seconds_value[i]", value, ps_bitstrm, 6);
2765                 ps_time_code->au1_seconds_value[i] = value;
2766 
2767                 BITS_PARSE("minutes_value[i]", value, ps_bitstrm, 6);
2768                 ps_time_code->au1_minutes_value[i] = value;
2769 
2770                 BITS_PARSE("hours_value[i]", value, ps_bitstrm, 5);
2771                 ps_time_code->au1_hours_value[i] = value;
2772             }
2773             else
2774             {
2775                 BITS_PARSE("seconds_flag[i]", value, ps_bitstrm, 1);
2776                 ps_time_code->au1_seconds_flag[i] = value;
2777 
2778                 if(ps_time_code->au1_seconds_flag[i])
2779                 {
2780                     BITS_PARSE("seconds_value[i]", value, ps_bitstrm, 6);
2781                     ps_time_code->au1_seconds_value[i] = value;
2782 
2783                     BITS_PARSE("minutes_flag[i]", value, ps_bitstrm, 1);
2784                     ps_time_code->au1_minutes_flag[i] = value;
2785 
2786                     if(ps_time_code->au1_minutes_flag[i])
2787                     {
2788                         BITS_PARSE("minutes_value[i]", value, ps_bitstrm, 6);
2789                         ps_time_code->au1_minutes_value[i] = value;
2790 
2791                         BITS_PARSE("hours_flag[i]", value, ps_bitstrm, 1);
2792                         ps_time_code->au1_hours_flag[i] = value;
2793 
2794                         if(ps_time_code->au1_hours_flag[i])
2795                         {
2796                             BITS_PARSE("hours_value[i]", value, ps_bitstrm, 5);
2797                             ps_time_code->au1_hours_value[i] = value;
2798                         }
2799                     }
2800                 }
2801             }
2802 
2803             BITS_PARSE("time_offset_length[i]", value, ps_bitstrm, 5);
2804             ps_time_code->au1_time_offset_length[i] = value;
2805 
2806             if(ps_time_code->au1_time_offset_length[i] > 0)
2807             {
2808                 BITS_PARSE("time_offset_value[i]", value, ps_bitstrm,
2809                            ps_time_code->au1_time_offset_length[i]);
2810                 ps_time_code->au1_time_offset_value[i] = value;
2811             }
2812             else
2813             {
2814                 ps_time_code->au1_time_offset_value[i] = 0;
2815             }
2816         }
2817     }
2818 
2819     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2820 }
2821 
ihevcd_parse_mastering_disp_params_sei(codec_t * ps_codec)2822 IHEVCD_ERROR_T ihevcd_parse_mastering_disp_params_sei(codec_t *ps_codec)
2823 {
2824     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2825     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2826     UWORD32 value;
2827     mastering_dis_col_vol_sei_params_t *ps_mastering_dis_col_vol;
2828     WORD32 i;
2829 
2830     ps_parse->s_sei_params.i4_sei_mastering_disp_colour_vol_params_present_flags = 1;
2831 
2832     ps_mastering_dis_col_vol = &ps_parse->s_sei_params.s_mastering_dis_col_vol_sei_params;
2833 
2834     for(i = 0; i < 3; i++)
2835     {
2836         BITS_PARSE("display_primaries_x[c]", value, ps_bitstrm, 16);
2837         ps_mastering_dis_col_vol->au2_display_primaries_x[i] = value;
2838 
2839         BITS_PARSE("display_primaries_y[c]", value, ps_bitstrm, 16);
2840         ps_mastering_dis_col_vol->au2_display_primaries_y[i] = value;
2841     }
2842 
2843     BITS_PARSE("white_point_x", value, ps_bitstrm, 16);
2844     ps_mastering_dis_col_vol->u2_white_point_x = value;
2845 
2846     BITS_PARSE("white_point_y", value, ps_bitstrm, 16);
2847     ps_mastering_dis_col_vol->u2_white_point_y = value;
2848 
2849     BITS_PARSE("max_display_mastering_luminance", value, ps_bitstrm, 32);
2850     ps_mastering_dis_col_vol->u4_max_display_mastering_luminance = value;
2851 
2852     BITS_PARSE("min_display_mastering_luminance", value, ps_bitstrm, 32);
2853     ps_mastering_dis_col_vol->u4_min_display_mastering_luminance = value;
2854 
2855     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2856 }
2857 
ihevcd_parse_user_data_registered_itu_t_t35(codec_t * ps_codec,UWORD32 u4_payload_size)2858 IHEVCD_ERROR_T ihevcd_parse_user_data_registered_itu_t_t35(codec_t *ps_codec,
2859                                                            UWORD32 u4_payload_size)
2860 {
2861     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2862     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2863     UWORD32 value;
2864     user_data_registered_itu_t_t35_t *ps_user_data_registered_itu_t_t35;
2865     UWORD32 i;
2866     UWORD32 j = 0;
2867 
2868     ps_parse->s_sei_params.i1_user_data_registered_present_flag = 1;
2869     ps_user_data_registered_itu_t_t35 =
2870                     &ps_parse->s_sei_params.as_user_data_registered_itu_t_t35[ps_parse->s_sei_params.i4_sei_user_data_cnt];
2871     ps_parse->s_sei_params.i4_sei_user_data_cnt++;
2872 
2873     ps_user_data_registered_itu_t_t35->i4_payload_size = u4_payload_size;
2874 
2875     if(u4_payload_size > MAX_USERDATA_PAYLOAD)
2876     {
2877         u4_payload_size = MAX_USERDATA_PAYLOAD;
2878     }
2879 
2880     ps_user_data_registered_itu_t_t35->i4_valid_payload_size = u4_payload_size;
2881 
2882     BITS_PARSE("itu_t_t35_country_code", value, ps_bitstrm, 8);
2883     ps_user_data_registered_itu_t_t35->u1_itu_t_t35_country_code = value;
2884 
2885     if(0xFF != ps_user_data_registered_itu_t_t35->u1_itu_t_t35_country_code)
2886     {
2887         i = 1;
2888     }
2889     else
2890     {
2891         BITS_PARSE("itu_t_t35_country_code_extension_byte", value, ps_bitstrm,
2892                    8);
2893         ps_user_data_registered_itu_t_t35->u1_itu_t_t35_country_code_extension_byte =
2894                         value;
2895 
2896         i = 2;
2897     }
2898 
2899     do
2900     {
2901         BITS_PARSE("itu_t_t35_payload_byte", value, ps_bitstrm, 8);
2902         ps_user_data_registered_itu_t_t35->u1_itu_t_t35_payload_byte[j++] =
2903                         value;
2904 
2905         i++;
2906     }while(i < u4_payload_size);
2907 
2908     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2909 }
2910 
ihevcd_parse_sei_payload(codec_t * ps_codec,UWORD32 u4_payload_type,UWORD32 u4_payload_size,WORD8 i1_nal_type)2911 void ihevcd_parse_sei_payload(codec_t *ps_codec,
2912                               UWORD32 u4_payload_type,
2913                               UWORD32 u4_payload_size,
2914                               WORD8 i1_nal_type)
2915 {
2916     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2917     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2918     WORD32 payload_bits_remaining = 0;
2919     sps_t *ps_sps;
2920 
2921     UWORD32 i;
2922 
2923     for(i = 0; i < MAX_SPS_CNT; i++)
2924     {
2925         ps_sps = ps_codec->ps_sps_base + i;
2926         if(ps_sps->i1_sps_valid)
2927         {
2928             break;
2929         }
2930     }
2931     if(!ps_sps->i1_sps_valid)
2932     {
2933         return;
2934     }
2935 
2936     if(NAL_PREFIX_SEI == i1_nal_type)
2937     {
2938         switch(u4_payload_type)
2939         {
2940             case SEI_BUFFERING_PERIOD:
2941                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
2942                 ihevcd_parse_buffering_period_sei(ps_codec, ps_sps);
2943                 break;
2944 
2945             case SEI_PICTURE_TIMING:
2946                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
2947                 ihevcd_parse_pic_timing_sei(ps_codec, ps_sps);
2948                 break;
2949 
2950             case SEI_TIME_CODE:
2951                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
2952                 ihevcd_parse_time_code_sei(ps_codec);
2953                 break;
2954 
2955             case SEI_MASTERING_DISPLAY_COLOUR_VOLUME:
2956                 ps_parse->s_sei_params.i4_sei_mastering_disp_colour_vol_params_present_flags = 1;
2957                 ihevcd_parse_mastering_disp_params_sei(ps_codec);
2958                 break;
2959 
2960             case SEI_USER_DATA_REGISTERED_ITU_T_T35:
2961                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
2962                 if(ps_parse->s_sei_params.i4_sei_user_data_cnt >= USER_DATA_MAX)
2963                 {
2964                     for(i = 0; i < u4_payload_size / 4; i++)
2965                     {
2966                         ihevcd_bits_flush(ps_bitstrm, 4 * 8);
2967                     }
2968 
2969                     ihevcd_bits_flush(ps_bitstrm, (u4_payload_size - i * 4) * 8);
2970                 }
2971                 else
2972                 {
2973                     ihevcd_parse_user_data_registered_itu_t_t35(ps_codec,
2974                                                                 u4_payload_size);
2975                 }
2976                 break;
2977 
2978             default:
2979                 for(i = 0; i < u4_payload_size; i++)
2980                 {
2981                     ihevcd_bits_flush(ps_bitstrm, 8);
2982                 }
2983                 break;
2984         }
2985     }
2986     else /* NAL_SUFFIX_SEI */
2987     {
2988         switch(u4_payload_type)
2989         {
2990             case SEI_USER_DATA_REGISTERED_ITU_T_T35:
2991                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
2992                 if(ps_parse->s_sei_params.i4_sei_user_data_cnt >= USER_DATA_MAX)
2993                 {
2994                     for(i = 0; i < u4_payload_size / 4; i++)
2995                     {
2996                         ihevcd_bits_flush(ps_bitstrm, 4 * 8);
2997                     }
2998 
2999                     ihevcd_bits_flush(ps_bitstrm, (u4_payload_size - i * 4) * 8);
3000                 }
3001                 else
3002                 {
3003                     ihevcd_parse_user_data_registered_itu_t_t35(ps_codec,
3004                                                                 u4_payload_size);
3005                 }
3006                 break;
3007 
3008             default:
3009                 for(i = 0; i < u4_payload_size; i++)
3010                 {
3011                     ihevcd_bits_flush(ps_bitstrm, 8);
3012                 }
3013                 break;
3014         }
3015     }
3016 
3017     /**
3018      * By definition the underlying bitstream terminates in a byte-aligned manner.
3019      * 1. Extract all bar the last MIN(bitsremaining,nine) bits as reserved_payload_extension_data
3020      * 2. Examine the final 8 bits to determine the payload_bit_equal_to_one marker
3021      * 3. Extract the remainingreserved_payload_extension_data bits.
3022      *
3023      * If there are fewer than 9 bits available, extract them.
3024      */
3025 
3026     payload_bits_remaining = ihevcd_bits_num_bits_remaining(ps_bitstrm);
3027     if(payload_bits_remaining) /* more_data_in_payload() */
3028     {
3029         WORD32 final_bits;
3030         WORD32 final_payload_bits = 0;
3031         WORD32 mask = 0xFF;
3032         UWORD32 u4_dummy;
3033         UWORD32 u4_reserved_payload_extension_data;
3034         UNUSED(u4_dummy);
3035         UNUSED(u4_reserved_payload_extension_data);
3036 
3037         while(payload_bits_remaining > 9)
3038         {
3039             BITS_PARSE("reserved_payload_extension_data",
3040                        u4_reserved_payload_extension_data, ps_bitstrm, 1);
3041             payload_bits_remaining--;
3042         }
3043 
3044         final_bits = ihevcd_bits_nxt(ps_bitstrm, payload_bits_remaining);
3045 
3046         while(final_bits & (mask >> final_payload_bits))
3047         {
3048             final_payload_bits++;
3049             continue;
3050         }
3051 
3052         while(payload_bits_remaining > (9 - final_payload_bits))
3053         {
3054             BITS_PARSE("reserved_payload_extension_data",
3055                        u4_reserved_payload_extension_data, ps_bitstrm, 1);
3056             payload_bits_remaining--;
3057         }
3058 
3059         BITS_PARSE("payload_bit_equal_to_one", u4_dummy, ps_bitstrm, 1);
3060         payload_bits_remaining--;
3061         while(payload_bits_remaining)
3062         {
3063             BITS_PARSE("payload_bit_equal_to_zero", u4_dummy, ps_bitstrm, 1);
3064             payload_bits_remaining--;
3065         }
3066     }
3067 
3068     return;
3069 }
3070 
ihevcd_read_rbsp_trailing_bits(codec_t * ps_codec,UWORD32 u4_bits_left)3071 IHEVCD_ERROR_T ihevcd_read_rbsp_trailing_bits(codec_t *ps_codec,
3072                                               UWORD32 u4_bits_left)
3073 {
3074     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
3075     UWORD32 value;
3076     WORD32 cnt = 0;
3077     BITS_PARSE("rbsp_stop_one_bit", value, &ps_parse->s_bitstrm, 1);
3078     u4_bits_left--;
3079     if(value != 1)
3080     {
3081         return (IHEVCD_ERROR_T)IHEVCD_FAIL;
3082     }
3083     while(u4_bits_left)
3084     {
3085         BITS_PARSE("rbsp_alignment_zero_bit", value, &ps_parse->s_bitstrm, 1);
3086         u4_bits_left--;
3087         cnt++;
3088     }
3089     ASSERT(cnt < 8);
3090 
3091     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
3092 }
3093 /**
3094 *******************************************************************************
3095 *
3096 * @brief
3097 *  Parses SEI (Supplemental Enhancement Information)
3098 *
3099 * @par Description:
3100 *  Parses SEI (Supplemental Enhancement Information) as per Section: 7.3.7
3101 *
3102 * @param[in] ps_codec
3103 *  Pointer to codec context
3104 *
3105 * @returns Error code from IHEVCD_ERROR_T
3106 *
3107 * @remarks
3108 *
3109 *
3110 *******************************************************************************
3111 */
ihevcd_parse_sei(codec_t * ps_codec,nal_header_t * ps_nal)3112 IHEVCD_ERROR_T ihevcd_parse_sei(codec_t *ps_codec, nal_header_t *ps_nal)
3113 {
3114     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
3115     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
3116     UWORD32 u4_payload_type = 0, u4_last_payload_type_byte = 0;
3117     UWORD32 u4_payload_size = 0, u4_last_payload_size_byte = 0;
3118     UWORD32 value;
3119     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
3120     UWORD32 u4_bits_left;
3121 
3122     u4_bits_left = ihevcd_bits_num_bits_remaining(ps_bitstrm);
3123 
3124     while(u4_bits_left > 8)
3125     {
3126         while(ihevcd_bits_nxt(ps_bitstrm, 8) == 0xFF)
3127         {
3128             ihevcd_bits_flush(ps_bitstrm, 8); /* equal to 0xFF */
3129             u4_payload_type += 255;
3130         }
3131 
3132         BITS_PARSE("last_payload_type_byte", value, ps_bitstrm, 8);
3133         u4_last_payload_type_byte = value;
3134 
3135         u4_payload_type += u4_last_payload_type_byte;
3136 
3137         while(ihevcd_bits_nxt(ps_bitstrm, 8) == 0xFF)
3138         {
3139             ihevcd_bits_flush(ps_bitstrm, 8); /* equal to 0xFF */
3140             u4_payload_size += 255;
3141         }
3142 
3143         BITS_PARSE("last_payload_size_byte", value, ps_bitstrm, 8);
3144         u4_last_payload_size_byte = value;
3145 
3146         u4_payload_size += u4_last_payload_size_byte;
3147         u4_bits_left = ihevcd_bits_num_bits_remaining(ps_bitstrm);
3148         u4_payload_size = MIN(u4_payload_size, u4_bits_left / 8);
3149         ihevcd_parse_sei_payload(ps_codec, u4_payload_type, u4_payload_size,
3150                                  ps_nal->i1_nal_unit_type);
3151 
3152         /* Calculate the bits left in the current payload */
3153         u4_bits_left = ihevcd_bits_num_bits_remaining(ps_bitstrm);
3154     }
3155 
3156     // read rbsp_trailing_bits
3157     if(u4_bits_left)
3158     {
3159         ihevcd_read_rbsp_trailing_bits(ps_codec, u4_bits_left);
3160     }
3161 
3162     return ret;
3163 }
3164 
3165 /**
3166 *******************************************************************************
3167 *
3168 * @brief
3169 *  Parses Access unit delimiter
3170 *
3171 * @par Description:
3172 *  Parses Access unit delimiter as per section  Section: 7.3.2.5
3173 *
3174 * @param[in] ps_codec
3175 *  Pointer to codec context
3176 *
3177 * @returns Error code from IHEVCD_ERROR_T
3178 *
3179 * @remarks
3180 *
3181 *
3182 *******************************************************************************
3183 */
ihevcd_parse_aud(codec_t * ps_codec)3184 WORD32 ihevcd_parse_aud(codec_t *ps_codec)
3185 {
3186     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
3187     UNUSED(ps_codec);
3188     return ret;
3189 }
3190 
ihevcd_extend_sign_bit(WORD32 value,WORD32 num_bits)3191 WORD32 ihevcd_extend_sign_bit(WORD32 value, WORD32 num_bits)
3192 {
3193     WORD32 ret_value = value;
3194     if(value >> (num_bits - 1))
3195     {
3196         ret_value |= (0xFFFFFFFF << num_bits);
3197     }
3198     return ret_value;
3199 }
3200 
3201 /**
3202 *******************************************************************************
3203 *
3204 * @brief
3205 *  Calculate POC of the current slice
3206 *
3207 * @par Description:
3208 *  Calculates the current POC using the previous POC lsb and previous POC msb
3209 *
3210 * @param[in] ps_codec
3211 *  Pointer to codec context
3212 *
3213 * @param[in] i1_pic_order_cnt_lsb
3214 *  Current POC lsb
3215 *
3216 * @returns  Current absolute POC
3217 *
3218 * @remarks
3219 *
3220 *
3221 *******************************************************************************
3222 */
3223 
ihevcd_calc_poc(codec_t * ps_codec,nal_header_t * ps_nal,WORD8 i1_log2_max_poc_lsb,WORD32 i2_poc_lsb)3224 WORD32 ihevcd_calc_poc(codec_t *ps_codec, nal_header_t *ps_nal, WORD8 i1_log2_max_poc_lsb, WORD32 i2_poc_lsb)
3225 {
3226     WORD32 i4_abs_poc, i4_poc_msb;
3227     WORD32 max_poc_lsb;
3228     WORD8 i1_nal_unit_type = ps_nal->i1_nal_unit_type;
3229     max_poc_lsb = (1 << i1_log2_max_poc_lsb);
3230 
3231     if((!ps_codec->i4_first_pic_done) && (!ps_codec->i4_pic_present))
3232         ps_codec->i4_prev_poc_msb = -2 * max_poc_lsb;
3233 
3234     if(NAL_IDR_N_LP == i1_nal_unit_type
3235                     || NAL_IDR_W_LP == i1_nal_unit_type
3236                     || NAL_BLA_N_LP == i1_nal_unit_type
3237                     || NAL_BLA_W_DLP == i1_nal_unit_type
3238                     || NAL_BLA_W_LP == i1_nal_unit_type
3239                     || (NAL_CRA == i1_nal_unit_type && !ps_codec->i4_first_pic_done))
3240     {
3241         i4_poc_msb = ps_codec->i4_prev_poc_msb + 2 * max_poc_lsb;
3242         ps_codec->i4_prev_poc_lsb = 0;
3243         ps_codec->i4_max_prev_poc_lsb = 0;
3244 //        ps_codec->i4_prev_poc_msb = 0;
3245     }
3246     else
3247     {
3248 
3249         if((i2_poc_lsb < ps_codec->i4_prev_poc_lsb)
3250                         && ((ps_codec->i4_prev_poc_lsb - i2_poc_lsb) >= max_poc_lsb / 2))
3251         {
3252             i4_poc_msb = ps_codec->i4_prev_poc_msb + max_poc_lsb;
3253         }
3254         else if((i2_poc_lsb > ps_codec->i4_prev_poc_lsb)
3255                         && ((i2_poc_lsb - ps_codec->i4_prev_poc_lsb) > max_poc_lsb / 2))
3256         {
3257             i4_poc_msb = ps_codec->i4_prev_poc_msb - max_poc_lsb;
3258         }
3259         else
3260         {
3261             i4_poc_msb = ps_codec->i4_prev_poc_msb;
3262         }
3263 
3264 
3265     }
3266 
3267     i4_abs_poc = i4_poc_msb + i2_poc_lsb;
3268     ps_codec->i4_max_prev_poc_lsb = MAX(ps_codec->i4_max_prev_poc_lsb, i2_poc_lsb);
3269 
3270     {
3271         WORD32 is_reference_nal = ((i1_nal_unit_type <= NAL_RSV_VCL_R15) && (i1_nal_unit_type % 2 != 0)) || ((i1_nal_unit_type >= NAL_BLA_W_LP) && (i1_nal_unit_type <= NAL_RSV_RAP_VCL23));
3272         WORD32 update_prev_poc = ((is_reference_nal) && ((i1_nal_unit_type < NAL_RADL_N) || (i1_nal_unit_type > NAL_RASL_R)));
3273 
3274         if((0 == ps_nal->i1_nuh_temporal_id) &&
3275                         (update_prev_poc))
3276         {
3277             ps_codec->i4_prev_poc_lsb = i2_poc_lsb;
3278             ps_codec->i4_prev_poc_msb = i4_poc_msb;
3279         }
3280     }
3281 
3282     return i4_abs_poc;
3283 }
3284 
3285 
ihevcd_copy_slice_hdr(codec_t * ps_codec,WORD32 slice_idx,WORD32 slice_idx_ref)3286 void ihevcd_copy_slice_hdr(codec_t *ps_codec, WORD32 slice_idx, WORD32 slice_idx_ref)
3287 {
3288     slice_header_t *ps_slice_hdr, *ps_slice_hdr_ref;
3289     WORD32 *pu4_entry_offset_backup;
3290 
3291     ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr_base + slice_idx;
3292     ps_slice_hdr_ref = ps_codec->s_parse.ps_slice_hdr_base + slice_idx_ref;
3293 
3294     pu4_entry_offset_backup = ps_slice_hdr->pu4_entry_point_offset;
3295     memcpy(ps_slice_hdr, ps_slice_hdr_ref, sizeof(slice_header_t));
3296     ps_slice_hdr->pu4_entry_point_offset = pu4_entry_offset_backup;
3297 }
3298 
3299 
3300 
3301