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