xref: /aosp_15_r20/external/libhevc/decoder/ihevcd_parse_headers.c (revision c83a76b084498d55f252f48b2e3786804cdf24b7)
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         if (0 != ps_codec->u4_allocate_dynamic_done) {
1849 
1850             vui_t *ps_vui = &ps_sps->s_vui_parameters;
1851             sps_t *ps_sps_old = ps_codec->s_parse.ps_sps;
1852             vui_t *ps_vui_old = &ps_sps_old->s_vui_parameters;
1853 
1854             if (ps_vui->u1_video_full_range_flag != ps_vui_old->u1_video_full_range_flag ||
1855                 ps_vui->u1_colour_primaries != ps_vui_old->u1_colour_primaries ||
1856                 ps_vui->u1_transfer_characteristics != ps_vui_old->u1_transfer_characteristics ||
1857                 ps_vui->u1_matrix_coefficients != ps_vui_old->u1_matrix_coefficients) {
1858                 ps_codec->i4_reset_flag = 1;
1859                 return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
1860             }
1861         }
1862     }
1863 
1864     BITS_PARSE("sps_extension_flag", value, ps_bitstrm, 1);
1865 
1866     if((UWORD8 *)ps_bitstrm->pu4_buf > ps_bitstrm->pu1_buf_max)
1867     {
1868         return IHEVCD_INVALID_PARAMETER;
1869     }
1870 
1871     {
1872         WORD32 numerator;
1873         WORD32 ceil_offset;
1874 
1875         ceil_offset = (1 << ps_sps->i1_log2_ctb_size) - 1;
1876         numerator = ps_sps->i2_pic_width_in_luma_samples;
1877 
1878         ps_sps->i2_pic_wd_in_ctb = ((numerator + ceil_offset) /
1879                         (1 << ps_sps->i1_log2_ctb_size));
1880 
1881         numerator = ps_sps->i2_pic_height_in_luma_samples;
1882         ps_sps->i2_pic_ht_in_ctb = ((numerator + ceil_offset) /
1883                         (1 << ps_sps->i1_log2_ctb_size));
1884 
1885         ps_sps->i4_pic_size_in_ctb = ps_sps->i2_pic_ht_in_ctb *
1886                         ps_sps->i2_pic_wd_in_ctb;
1887 
1888         if(0 == ps_codec->i4_sps_done)
1889             ps_codec->s_parse.i4_next_ctb_indx = ps_sps->i4_pic_size_in_ctb;
1890 
1891         numerator = ps_sps->i2_pic_width_in_luma_samples;
1892         ps_sps->i2_pic_wd_in_min_cb = numerator  /
1893                         (1 << ps_sps->i1_log2_min_coding_block_size);
1894 
1895         numerator = ps_sps->i2_pic_height_in_luma_samples;
1896         ps_sps->i2_pic_ht_in_min_cb = numerator  /
1897                         (1 << ps_sps->i1_log2_min_coding_block_size);
1898     }
1899     if((0 != ps_codec->u4_allocate_dynamic_done) &&
1900                     ((ps_codec->i4_wd != ps_sps->i2_pic_width_in_luma_samples) ||
1901                     (ps_codec->i4_ht != ps_sps->i2_pic_height_in_luma_samples)))
1902     {
1903         if(0 == ps_codec->i4_first_pic_done)
1904         {
1905             return IHEVCD_INVALID_PARAMETER;
1906         }
1907         ps_codec->i4_reset_flag = 1;
1908         return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
1909     }
1910 
1911     // Ensure both i2_pic_width_in_luma_samples and i2_pic_height_in_luma_samples do
1912     // not exceed MAX_WD and their product doesn't exceed MAX_WD * MAX_HT
1913     if((ps_sps->i2_pic_width_in_luma_samples > MAX_WD) ||
1914                     (ps_sps->i2_pic_height_in_luma_samples > MAX_WD) ||
1915                     ((ps_sps->i2_pic_width_in_luma_samples * ps_sps->i2_pic_height_in_luma_samples) >
1916                     (MAX_WD * MAX_HT)))
1917     {
1918         return (IHEVCD_ERROR_T)IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
1919     }
1920 
1921     /* Update display width and display height */
1922     {
1923         WORD32 disp_wd, disp_ht;
1924         WORD32 crop_unit_x, crop_unit_y;
1925         crop_unit_x = 1;
1926         crop_unit_y = 1;
1927 
1928         if(CHROMA_FMT_IDC_YUV420 == ps_sps->i1_chroma_format_idc)
1929         {
1930             crop_unit_x = 2;
1931             crop_unit_y = 2;
1932         }
1933 
1934         disp_wd = ps_sps->i2_pic_width_in_luma_samples;
1935         disp_wd -= ps_sps->i2_pic_crop_left_offset * crop_unit_x;
1936         disp_wd -= ps_sps->i2_pic_crop_right_offset * crop_unit_x;
1937 
1938 
1939         disp_ht = ps_sps->i2_pic_height_in_luma_samples;
1940         disp_ht -= ps_sps->i2_pic_crop_top_offset * crop_unit_y;
1941         disp_ht -= ps_sps->i2_pic_crop_bottom_offset * crop_unit_y;
1942 
1943         if((0 >= disp_wd) || (0 >= disp_ht))
1944             return IHEVCD_INVALID_PARAMETER;
1945 
1946         if((0 != ps_codec->u4_allocate_dynamic_done) &&
1947                             ((ps_codec->i4_disp_wd != disp_wd) ||
1948                             (ps_codec->i4_disp_ht != disp_ht)))
1949         {
1950             if(0 == ps_codec->i4_first_pic_done)
1951             {
1952                 return IHEVCD_INVALID_PARAMETER;
1953             }
1954             ps_codec->i4_reset_flag = 1;
1955             return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
1956         }
1957 
1958         ps_codec->i4_disp_wd = disp_wd;
1959         ps_codec->i4_disp_ht = disp_ht;
1960 
1961 
1962         ps_codec->i4_wd = ps_sps->i2_pic_width_in_luma_samples;
1963         ps_codec->i4_ht = ps_sps->i2_pic_height_in_luma_samples;
1964         ps_codec->u4_num_8x8_blks = ALIGN64(ps_codec->i4_wd) * ALIGN64(ps_codec->i4_ht) >> 6;
1965 
1966         {
1967             WORD32 ref_strd;
1968             ref_strd = ALIGN32(ps_sps->i2_pic_width_in_luma_samples + PAD_WD);
1969             if(ps_codec->i4_strd < ref_strd)
1970             {
1971                 ps_codec->i4_strd = ref_strd;
1972             }
1973         }
1974 
1975         if(0 == ps_codec->i4_share_disp_buf)
1976         {
1977             if(ps_codec->i4_disp_strd < ps_codec->i4_disp_wd)
1978             {
1979                 ps_codec->i4_disp_strd = ps_codec->i4_disp_wd;
1980             }
1981         }
1982         else
1983         {
1984             if(ps_codec->i4_disp_strd < ps_codec->i4_strd)
1985             {
1986                 ps_codec->i4_disp_strd = ps_codec->i4_strd;
1987             }
1988         }
1989     }
1990 
1991     /* This is used only during initialization to get reorder count etc */
1992     ps_codec->i4_sps_id = sps_id;
1993 
1994     ps_codec->i4_sps_done = 1;
1995     return ret;
1996 }
1997 
1998 
ihevcd_unmark_pps(codec_t * ps_codec,WORD32 sps_id)1999 void ihevcd_unmark_pps(codec_t *ps_codec, WORD32 sps_id)
2000 {
2001     WORD32 pps_id = 0;
2002     pps_t *ps_pps = ps_codec->ps_pps_base;
2003 
2004     for(pps_id = 0; pps_id < MAX_PPS_CNT - 1; pps_id++, ps_pps++)
2005     {
2006         if((ps_pps->i1_pps_valid) &&
2007                         (ps_pps->i1_sps_id == sps_id))
2008             ps_pps->i1_pps_valid = 0;
2009     }
2010 }
2011 
2012 
ihevcd_copy_sps(codec_t * ps_codec,WORD32 sps_id,WORD32 sps_id_ref)2013 void ihevcd_copy_sps(codec_t *ps_codec, WORD32 sps_id, WORD32 sps_id_ref)
2014 {
2015     sps_t *ps_sps, *ps_sps_ref;
2016     WORD16 *pi2_scaling_mat_backup;
2017     WORD32 scaling_mat_size;
2018 
2019     SCALING_MAT_SIZE(scaling_mat_size);
2020     ps_sps_ref = ps_codec->ps_sps_base + sps_id_ref;
2021     ps_sps = ps_codec->ps_sps_base + sps_id;
2022 
2023     if(ps_sps->i1_sps_valid)
2024     {
2025         if((ps_sps->i1_log2_ctb_size != ps_sps_ref->i1_log2_ctb_size) ||
2026                         (ps_sps->i2_pic_wd_in_ctb != ps_sps_ref->i2_pic_wd_in_ctb) ||
2027                         (ps_sps->i2_pic_ht_in_ctb != ps_sps_ref->i2_pic_ht_in_ctb))
2028         {
2029             ihevcd_unmark_pps(ps_codec, sps_id);
2030         }
2031     }
2032 
2033     pi2_scaling_mat_backup = ps_sps->pi2_scaling_mat;
2034 
2035     memcpy(ps_sps, ps_sps_ref, sizeof(sps_t));
2036     ps_sps->pi2_scaling_mat = pi2_scaling_mat_backup;
2037     memcpy(ps_sps->pi2_scaling_mat, ps_sps_ref->pi2_scaling_mat, scaling_mat_size * sizeof(WORD16));
2038     ps_sps->i1_sps_valid = 1;
2039 
2040     ps_codec->s_parse.ps_sps = ps_sps;
2041 }
2042 
2043 
2044 /**
2045 *******************************************************************************
2046 *
2047 * @brief
2048 *  Parses PPS (Picture Parameter Set)
2049 *
2050 * @par Description:
2051 *  Parse Picture Parameter Set as per section  Section: 7.3.2.3
2052 * The pps is written to a temporary buffer and copied later to the
2053 * appropriate location
2054 *
2055 * @param[in] ps_codec
2056 *  Pointer to codec context
2057 *
2058 * @returns Error code from IHEVCD_ERROR_T
2059 *
2060 * @remarks
2061 *
2062 *
2063 *******************************************************************************
2064 */
ihevcd_parse_pps(codec_t * ps_codec)2065 IHEVCD_ERROR_T ihevcd_parse_pps(codec_t *ps_codec)
2066 {
2067     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2068     UWORD32 value;
2069     WORD32 i4_value;
2070     WORD32 pps_id;
2071 
2072     pps_t *ps_pps;
2073     sps_t *ps_sps;
2074     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
2075 
2076 
2077     if(0 == ps_codec->i4_sps_done)
2078         return IHEVCD_INVALID_HEADER;
2079 
2080     UEV_PARSE("pic_parameter_set_id", value, ps_bitstrm);
2081 
2082     pps_id = value;
2083     if((pps_id >= MAX_PPS_CNT) || (pps_id < 0))
2084     {
2085         if(ps_codec->i4_pps_done)
2086             return IHEVCD_UNSUPPORTED_PPS_ID;
2087         else
2088             pps_id = 0;
2089     }
2090 
2091 
2092     ps_pps = (ps_codec->s_parse.ps_pps_base + MAX_PPS_CNT - 1);
2093 
2094     ps_pps->i1_pps_id = pps_id;
2095 
2096     UEV_PARSE("seq_parameter_set_id", value, ps_bitstrm);
2097     if(value > MAX_SPS_CNT - 2)
2098     {
2099         return IHEVCD_INVALID_PARAMETER;
2100     }
2101     ps_pps->i1_sps_id = value;
2102 
2103     ps_sps = (ps_codec->s_parse.ps_sps_base + ps_pps->i1_sps_id);
2104 
2105     /* If the SPS that is being referred to has not been parsed,
2106      * copy an existing SPS to the current location */
2107     if(0 == ps_sps->i1_sps_valid)
2108     {
2109         return IHEVCD_INVALID_HEADER;
2110 
2111 /*
2112         sps_t *ps_sps_ref = ps_codec->ps_sps_base;
2113         while(0 == ps_sps_ref->i1_sps_valid)
2114             ps_sps_ref++;
2115         ihevcd_copy_sps(ps_codec, ps_pps->i1_sps_id, ps_sps_ref->i1_sps_id);
2116 */
2117     }
2118 
2119     BITS_PARSE("dependent_slices_enabled_flag", value, ps_bitstrm, 1);
2120     ps_pps->i1_dependent_slice_enabled_flag = value;
2121 
2122     BITS_PARSE("output_flag_present_flag", value, ps_bitstrm, 1);
2123     ps_pps->i1_output_flag_present_flag = value;
2124 
2125     BITS_PARSE("num_extra_slice_header_bits", value, ps_bitstrm, 3);
2126     ps_pps->i1_num_extra_slice_header_bits = value;
2127 
2128 
2129     BITS_PARSE("sign_data_hiding_flag", value, ps_bitstrm, 1);
2130     ps_pps->i1_sign_data_hiding_flag = value;
2131 
2132     BITS_PARSE("cabac_init_present_flag", value, ps_bitstrm, 1);
2133     ps_pps->i1_cabac_init_present_flag = value;
2134 
2135     UEV_PARSE("num_ref_idx_l0_default_active_minus1", value, ps_bitstrm);
2136     if(value > MAX_DPB_SIZE - 2)
2137     {
2138          return IHEVCD_INVALID_PARAMETER;
2139     }
2140     ps_pps->i1_num_ref_idx_l0_default_active = value + 1;
2141 
2142     UEV_PARSE("num_ref_idx_l1_default_active_minus1", value, ps_bitstrm);
2143     if(value > MAX_DPB_SIZE - 2)
2144     {
2145         return IHEVCD_INVALID_PARAMETER;
2146     }
2147     ps_pps->i1_num_ref_idx_l1_default_active = value + 1;
2148 
2149     SEV_PARSE("pic_init_qp_minus26", i4_value, ps_bitstrm);
2150     if(i4_value < -26 || i4_value > 25)
2151     {
2152         return IHEVCD_INVALID_PARAMETER;
2153     }
2154     ps_pps->i1_pic_init_qp = i4_value + 26;
2155 
2156     BITS_PARSE("constrained_intra_pred_flag", value, ps_bitstrm, 1);
2157     ps_pps->i1_constrained_intra_pred_flag = value;
2158 
2159     BITS_PARSE("transform_skip_enabled_flag", value, ps_bitstrm, 1);
2160     ps_pps->i1_transform_skip_enabled_flag = value;
2161 
2162     BITS_PARSE("cu_qp_delta_enabled_flag", value, ps_bitstrm, 1);
2163     ps_pps->i1_cu_qp_delta_enabled_flag = value;
2164 
2165     if(ps_pps->i1_cu_qp_delta_enabled_flag)
2166     {
2167         UEV_PARSE("diff_cu_qp_delta_depth", value, ps_bitstrm);
2168         if(value > ps_sps->i1_log2_diff_max_min_coding_block_size)
2169         {
2170             return IHEVCD_INVALID_PARAMETER;
2171         }
2172         ps_pps->i1_diff_cu_qp_delta_depth = value;
2173     }
2174     else
2175     {
2176         ps_pps->i1_diff_cu_qp_delta_depth = 0;
2177     }
2178     ps_pps->i1_log2_min_cu_qp_delta_size = ps_sps->i1_log2_ctb_size - ps_pps->i1_diff_cu_qp_delta_depth;
2179     /* Print different */
2180     SEV_PARSE("cb_qp_offset", i4_value, ps_bitstrm);
2181     if(i4_value < -12 || i4_value > 12)
2182     {
2183         return IHEVCD_INVALID_PARAMETER;
2184     }
2185     ps_pps->i1_pic_cb_qp_offset = i4_value;
2186 
2187     /* Print different */
2188     SEV_PARSE("cr_qp_offset", i4_value, ps_bitstrm);
2189     if(i4_value < -12 || i4_value > 12)
2190     {
2191         return IHEVCD_INVALID_PARAMETER;
2192     }
2193     ps_pps->i1_pic_cr_qp_offset = i4_value;
2194 
2195     /* Print different */
2196     BITS_PARSE("slicelevel_chroma_qp_flag", value, ps_bitstrm, 1);
2197     ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag = value;
2198 
2199     BITS_PARSE("weighted_pred_flag", value, ps_bitstrm, 1);
2200     ps_pps->i1_weighted_pred_flag = value;
2201 
2202     BITS_PARSE("weighted_bipred_flag", value, ps_bitstrm, 1);
2203     ps_pps->i1_weighted_bipred_flag = value;
2204 
2205     BITS_PARSE("transquant_bypass_enable_flag", value, ps_bitstrm, 1);
2206     ps_pps->i1_transquant_bypass_enable_flag = value;
2207 
2208     BITS_PARSE("tiles_enabled_flag", value, ps_bitstrm, 1);
2209     ps_pps->i1_tiles_enabled_flag = value;
2210 
2211     /* When tiles are enabled and width or height is >= 4096,
2212      * CTB Size should at least be 32 while if width or height is >= 8192,
2213      * CTB Size should at least be 64 and so on. 16x16 CTBs can result
2214      * in tile position greater than 255 for 4096 while 32x32 CTBs can result
2215      * in tile position greater than 255 for 8192,
2216      * which decoder does not support.
2217      */
2218     if (ps_pps->i1_tiles_enabled_flag)
2219     {
2220         if((ps_sps->i1_log2_ctb_size == 4) &&
2221             ((ps_sps->i2_pic_width_in_luma_samples >= 4096) ||
2222             (ps_sps->i2_pic_height_in_luma_samples >= 4096)))
2223         {
2224             return IHEVCD_INVALID_HEADER;
2225         }
2226         if((ps_sps->i1_log2_ctb_size == 5) &&
2227             ((ps_sps->i2_pic_width_in_luma_samples >= 8192) ||
2228             (ps_sps->i2_pic_height_in_luma_samples >= 8192)))
2229         {
2230             return IHEVCD_INVALID_HEADER;
2231         }
2232         if((ps_sps->i1_log2_ctb_size == 6) &&
2233             ((ps_sps->i2_pic_width_in_luma_samples >= 16384) ||
2234             (ps_sps->i2_pic_height_in_luma_samples >= 16384)))
2235         {
2236             return IHEVCD_INVALID_HEADER;
2237         }
2238     }
2239 
2240     BITS_PARSE("entropy_coding_sync_enabled_flag", value, ps_bitstrm, 1);
2241     ps_pps->i1_entropy_coding_sync_enabled_flag = value;
2242 
2243     ps_pps->i1_loop_filter_across_tiles_enabled_flag = 0;
2244     if(ps_pps->i1_tiles_enabled_flag)
2245     {
2246         WORD32 wd = ALIGN64(ps_codec->i4_wd);
2247         WORD32 ht = ALIGN64(ps_codec->i4_ht);
2248 
2249         WORD32 max_tile_cols = (wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
2250         WORD32 max_tile_rows = (ht + MIN_TILE_HT - 1) / MIN_TILE_HT;
2251 
2252         UEV_PARSE("num_tile_columns_minus1", value, ps_bitstrm);
2253         ps_pps->i1_num_tile_columns = value + 1;
2254 
2255         UEV_PARSE("num_tile_rows_minus1", value, ps_bitstrm);
2256         ps_pps->i1_num_tile_rows = value + 1;
2257 
2258         if((ps_pps->i1_num_tile_columns < 1) ||
2259                         (ps_pps->i1_num_tile_columns > max_tile_cols) ||
2260                         (ps_pps->i1_num_tile_rows < 1) ||
2261                         (ps_pps->i1_num_tile_rows > max_tile_rows))
2262             return IHEVCD_INVALID_HEADER;
2263 
2264         BITS_PARSE("uniform_spacing_flag", value, ps_bitstrm, 1);
2265         ps_pps->i1_uniform_spacing_flag = value;
2266 
2267 
2268         {
2269 
2270             UWORD32 start;
2271             WORD32 i, j;
2272 
2273 
2274             start = 0;
2275             for(i = 0; i < ps_pps->i1_num_tile_columns; i++)
2276             {
2277                 tile_t *ps_tile;
2278                 if(!ps_pps->i1_uniform_spacing_flag)
2279                 {
2280                     if(i < (ps_pps->i1_num_tile_columns - 1))
2281                     {
2282                         UEV_PARSE("column_width_minus1[ i ]", value, ps_bitstrm);
2283                         value += 1;
2284                         if (value >= ps_sps->i2_pic_wd_in_ctb - start)
2285                         {
2286                             return IHEVCD_INVALID_HEADER;
2287                         }
2288                     }
2289                     else
2290                     {
2291                         value = ps_sps->i2_pic_wd_in_ctb - start;
2292                     }
2293                 }
2294                 else
2295                 {
2296                     value = ((i + 1) * ps_sps->i2_pic_wd_in_ctb) / ps_pps->i1_num_tile_columns -
2297                                     (i * ps_sps->i2_pic_wd_in_ctb) / ps_pps->i1_num_tile_columns;
2298                 }
2299 
2300                 for(j = 0; j < ps_pps->i1_num_tile_rows; j++)
2301                 {
2302                     ps_tile = ps_pps->ps_tile + j * ps_pps->i1_num_tile_columns + i;
2303                     ps_tile->u1_pos_x = start;
2304                     ps_tile->u2_wd = value;
2305                 }
2306                 start += value;
2307 
2308                 if((start > ps_sps->i2_pic_wd_in_ctb) ||
2309                                 (value == 0))
2310                     return IHEVCD_INVALID_HEADER;
2311             }
2312 
2313             start = 0;
2314             for(i = 0; i < (ps_pps->i1_num_tile_rows); i++)
2315             {
2316                 tile_t *ps_tile;
2317                 if(!ps_pps->i1_uniform_spacing_flag)
2318                 {
2319                     if(i < (ps_pps->i1_num_tile_rows - 1))
2320                     {
2321 
2322                         UEV_PARSE("row_height_minus1[ i ]", value, ps_bitstrm);
2323                         value += 1;
2324                         if (value >= ps_sps->i2_pic_ht_in_ctb - start)
2325                         {
2326                             return IHEVCD_INVALID_HEADER;
2327                         }
2328                     }
2329                     else
2330                     {
2331                         value = ps_sps->i2_pic_ht_in_ctb - start;
2332                     }
2333                 }
2334                 else
2335                 {
2336                     value = ((i + 1) * ps_sps->i2_pic_ht_in_ctb) / ps_pps->i1_num_tile_rows -
2337                                     (i * ps_sps->i2_pic_ht_in_ctb) / ps_pps->i1_num_tile_rows;
2338                 }
2339 
2340                 for(j = 0; j < ps_pps->i1_num_tile_columns; j++)
2341                 {
2342                     ps_tile = ps_pps->ps_tile + i * ps_pps->i1_num_tile_columns + j;
2343                     ps_tile->u1_pos_y = start;
2344                     ps_tile->u2_ht = value;
2345                 }
2346                 start += value;
2347 
2348                 if((start > ps_sps->i2_pic_ht_in_ctb) ||
2349                                 (value == 0))
2350                     return IHEVCD_INVALID_HEADER;
2351             }
2352         }
2353 
2354 
2355         BITS_PARSE("loop_filter_across_tiles_enabled_flag", value, ps_bitstrm, 1);
2356         ps_pps->i1_loop_filter_across_tiles_enabled_flag = value;
2357 
2358     }
2359     else
2360     {
2361         /* If tiles are not present, set first tile in each PPS to have tile
2362         width and height equal to picture width and height */
2363         ps_pps->i1_num_tile_columns = 1;
2364         ps_pps->i1_num_tile_rows = 1;
2365         ps_pps->i1_uniform_spacing_flag = 1;
2366 
2367         ps_pps->ps_tile->u1_pos_x = 0;
2368         ps_pps->ps_tile->u1_pos_y = 0;
2369         ps_pps->ps_tile->u2_wd = ps_sps->i2_pic_wd_in_ctb;
2370         ps_pps->ps_tile->u2_ht = ps_sps->i2_pic_ht_in_ctb;
2371     }
2372 
2373     BITS_PARSE("loop_filter_across_slices_enabled_flag", value, ps_bitstrm, 1);
2374     ps_pps->i1_loop_filter_across_slices_enabled_flag = value;
2375 
2376     BITS_PARSE("deblocking_filter_control_present_flag", value, ps_bitstrm, 1);
2377     ps_pps->i1_deblocking_filter_control_present_flag = value;
2378 
2379     /* Default values */
2380     ps_pps->i1_pic_disable_deblocking_filter_flag = 0;
2381     ps_pps->i1_deblocking_filter_override_enabled_flag = 0;
2382     ps_pps->i1_beta_offset_div2 = 0;
2383     ps_pps->i1_tc_offset_div2 = 0;
2384 
2385     if(ps_pps->i1_deblocking_filter_control_present_flag)
2386     {
2387 
2388         BITS_PARSE("deblocking_filter_override_enabled_flag", value, ps_bitstrm, 1);
2389         ps_pps->i1_deblocking_filter_override_enabled_flag = value;
2390 
2391         BITS_PARSE("pic_disable_deblocking_filter_flag", value, ps_bitstrm, 1);
2392         ps_pps->i1_pic_disable_deblocking_filter_flag = value;
2393 
2394         if(!ps_pps->i1_pic_disable_deblocking_filter_flag)
2395         {
2396 
2397             SEV_PARSE("pps_beta_offset_div2", i4_value, ps_bitstrm);
2398             if(i4_value < -6 || i4_value > 6)
2399             {
2400                 return IHEVCD_INVALID_PARAMETER;
2401             }
2402             ps_pps->i1_beta_offset_div2 = i4_value;
2403 
2404             SEV_PARSE("pps_tc_offset_div2", i4_value, ps_bitstrm);
2405             if(i4_value < -6 || i4_value > 6)
2406             {
2407                 return IHEVCD_INVALID_PARAMETER;
2408             }
2409             ps_pps->i1_tc_offset_div2 = i4_value;
2410 
2411         }
2412     }
2413 
2414     BITS_PARSE("pps_scaling_list_data_present_flag", value, ps_bitstrm, 1);
2415     ps_pps->i1_pps_scaling_list_data_present_flag = value;
2416 
2417     if(ps_pps->i1_pps_scaling_list_data_present_flag)
2418     {
2419         COPY_DEFAULT_SCALING_LIST(ps_pps->pi2_scaling_mat);
2420         ihevcd_scaling_list_data(ps_codec, ps_pps->pi2_scaling_mat);
2421     }
2422 
2423     BITS_PARSE("lists_modification_present_flag", value, ps_bitstrm, 1);
2424     ps_pps->i1_lists_modification_present_flag = value;
2425     UEV_PARSE("log2_parallel_merge_level_minus2", value, ps_bitstrm);
2426     if(value > (ps_sps->i1_log2_min_coding_block_size + ps_sps->i1_log2_diff_max_min_coding_block_size))
2427     {
2428         return IHEVCD_INVALID_PARAMETER;
2429     }
2430     ps_pps->i1_log2_parallel_merge_level = value + 2;
2431 
2432     BITS_PARSE("slice_header_extension_present_flag", value, ps_bitstrm, 1);
2433     ps_pps->i1_slice_header_extension_present_flag = value;
2434     /* Not present in HM */
2435     BITS_PARSE("pps_extension_flag", value, ps_bitstrm, 1);
2436 
2437     if((UWORD8 *)ps_bitstrm->pu4_buf > ps_bitstrm->pu1_buf_max)
2438         return IHEVCD_INVALID_PARAMETER;
2439 
2440     ps_codec->i4_pps_done = 1;
2441     return ret;
2442 }
2443 
2444 
ihevcd_copy_pps(codec_t * ps_codec,WORD32 pps_id,WORD32 pps_id_ref)2445 void ihevcd_copy_pps(codec_t *ps_codec, WORD32 pps_id, WORD32 pps_id_ref)
2446 {
2447     pps_t *ps_pps, *ps_pps_ref;
2448     WORD16 *pi2_scaling_mat_backup;
2449     WORD32 scaling_mat_size;
2450     tile_t *ps_tile_backup;
2451     WORD32 max_tile_cols, max_tile_rows;
2452     WORD32 wd, ht;
2453     wd = ALIGN64(ps_codec->i4_wd);
2454     ht = ALIGN64(ps_codec->i4_ht);
2455 
2456     SCALING_MAT_SIZE(scaling_mat_size);
2457     max_tile_cols = (wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
2458     max_tile_rows = (ht + MIN_TILE_HT - 1) / MIN_TILE_HT;
2459 
2460     ps_pps_ref = ps_codec->ps_pps_base + pps_id_ref;
2461     ps_pps = ps_codec->ps_pps_base + pps_id;
2462 
2463     pi2_scaling_mat_backup = ps_pps->pi2_scaling_mat;
2464     ps_tile_backup = ps_pps->ps_tile;
2465 
2466     memcpy(ps_pps, ps_pps_ref, sizeof(pps_t));
2467     ps_pps->pi2_scaling_mat = pi2_scaling_mat_backup;
2468     ps_pps->ps_tile = ps_tile_backup;
2469     memcpy(ps_pps->pi2_scaling_mat, ps_pps_ref->pi2_scaling_mat, scaling_mat_size * sizeof(WORD16));
2470     memcpy(ps_pps->ps_tile, ps_pps_ref->ps_tile, max_tile_cols * max_tile_rows * sizeof(tile_t));
2471 
2472     ps_pps->i1_pps_valid = 1;
2473 
2474     ps_codec->s_parse.ps_pps = ps_pps;
2475 }
2476 
2477 
2478 #ifndef DISABLE_SEI
ihevcd_parse_buffering_period_sei(codec_t * ps_codec,sps_t * ps_sps)2479 IHEVCD_ERROR_T ihevcd_parse_buffering_period_sei(codec_t *ps_codec,
2480                                                  sps_t *ps_sps)
2481 {
2482     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2483     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2484     UWORD32 value;
2485     vui_t *ps_vui;
2486     buf_period_sei_params_t *ps_buf_period_sei_params;
2487     UWORD32 i;
2488     hrd_params_t *ps_vui_hdr;
2489     UWORD32 u4_cpb_cnt;
2490 
2491     ps_vui = &ps_sps->s_vui_parameters;
2492     ps_vui_hdr = &ps_vui->s_vui_hrd_parameters;
2493 
2494     ps_buf_period_sei_params = &ps_parse->s_sei_params.s_buf_period_sei_params;
2495 
2496     ps_parse->s_sei_params.i1_buf_period_params_present_flag = 1;
2497 
2498     UEV_PARSE("bp_seq_parameter_set_id", value, ps_bitstrm);
2499     if(value > MAX_SPS_CNT - 2)
2500     {
2501         return IHEVCD_INVALID_PARAMETER;
2502     }
2503     ps_buf_period_sei_params->u1_bp_seq_parameter_set_id = value;
2504 
2505     if(!ps_vui_hdr->u1_sub_pic_cpb_params_present_flag)
2506     {
2507         BITS_PARSE("irap_cpb_params_present_flag", value, ps_bitstrm, 1);
2508         ps_buf_period_sei_params->u1_rap_cpb_params_present_flag = value;
2509     }
2510 
2511     if(ps_buf_period_sei_params->u1_rap_cpb_params_present_flag)
2512     {
2513         BITS_PARSE("cpb_delay_offset",
2514                    value,
2515                    ps_bitstrm,
2516                    (ps_vui_hdr->u1_au_cpb_removal_delay_length_minus1
2517                                    + 1));
2518         ps_buf_period_sei_params->u4_cpb_delay_offset = value;
2519 
2520         BITS_PARSE("dpb_delay_offset",
2521                    value,
2522                    ps_bitstrm,
2523                    (ps_vui_hdr->u1_dpb_output_delay_length_minus1
2524                                    + 1));
2525         ps_buf_period_sei_params->u4_dpb_delay_offset = value;
2526     }
2527     else
2528     {
2529         ps_buf_period_sei_params->u4_cpb_delay_offset = 0;
2530         ps_buf_period_sei_params->u4_dpb_delay_offset = 0;
2531     }
2532 
2533     BITS_PARSE("concatenation_flag", value, ps_bitstrm, 1);
2534     ps_buf_period_sei_params->u1_concatenation_flag = value;
2535 
2536     BITS_PARSE("au_cpb_removal_delay_delta_minus1",
2537                value,
2538                ps_bitstrm,
2539                (ps_vui_hdr->u1_au_cpb_removal_delay_length_minus1
2540                                + 1));
2541     ps_buf_period_sei_params->u4_au_cpb_removal_delay_delta_minus1 = value;
2542 
2543     if(ps_vui_hdr->u1_nal_hrd_parameters_present_flag)
2544     {
2545         u4_cpb_cnt = ps_vui_hdr->au1_cpb_cnt_minus1[0];
2546 
2547         for(i = 0; i <= u4_cpb_cnt; i++)
2548         {
2549             BITS_PARSE("nal_initial_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_cpb_removal_delay[i] =
2555                             value;
2556 
2557             BITS_PARSE("nal_initial_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_cpb_removal_delay_offset[i] =
2563                             value;
2564 
2565             if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag
2566                             || ps_buf_period_sei_params->u1_rap_cpb_params_present_flag)
2567             {
2568                 BITS_PARSE("nal_initial_alt_cpb_removal_delay[i]",
2569                            value,
2570                            ps_bitstrm,
2571                            (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2572                                            + 1));
2573                 ps_buf_period_sei_params->au4_nal_initial_alt_cpb_removal_delay[i] =
2574                                 value;
2575 
2576                 BITS_PARSE("nal_initial_alt_cpb_removal_delay_offset",
2577                            value,
2578                            ps_bitstrm,
2579                            (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2580                                            + 1));
2581                 ps_buf_period_sei_params->au4_nal_initial_alt_cpb_removal_delay_offset[i] =
2582                                 value;
2583             }
2584         }
2585     }
2586 
2587     if(ps_vui_hdr->u1_vcl_hrd_parameters_present_flag)
2588     {
2589         u4_cpb_cnt = ps_vui_hdr->au1_cpb_cnt_minus1[0];
2590 
2591         for(i = 0; i <= u4_cpb_cnt; i++)
2592         {
2593             BITS_PARSE("vcl_initial_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_cpb_removal_delay[i] =
2599                             value;
2600 
2601             BITS_PARSE("vcl_initial_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_cpb_removal_delay_offset[i] =
2607                             value;
2608 
2609             if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag
2610                             || ps_buf_period_sei_params->u1_rap_cpb_params_present_flag)
2611             {
2612                 BITS_PARSE("vcl_initial_alt_cpb_removal_delay[i]",
2613                            value,
2614                            ps_bitstrm,
2615                            (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2616                                            + 1));
2617                 ps_buf_period_sei_params->au4_vcl_initial_alt_cpb_removal_delay[i] =
2618                                 value;
2619 
2620                 BITS_PARSE("vcl_initial_alt_cpb_removal_delay_offset",
2621                            value,
2622                            ps_bitstrm,
2623                            (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2624                                            + 1));
2625                 ps_buf_period_sei_params->au4_vcl_initial_alt_cpb_removal_delay_offset[i] =
2626                                 value;
2627             }
2628         }
2629     }
2630 
2631     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2632 }
2633 
ihevcd_parse_pic_timing_sei(codec_t * ps_codec,sps_t * ps_sps)2634 IHEVCD_ERROR_T ihevcd_parse_pic_timing_sei(codec_t *ps_codec, sps_t *ps_sps)
2635 {
2636     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2637     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2638     UWORD32 value;
2639     vui_t *ps_vui;
2640     UWORD32 i;
2641     hrd_params_t *ps_vui_hdr;
2642     UWORD32 u4_cpb_dpb_delays_present_flag = 0;
2643     pic_timing_sei_params_t *ps_pic_timing;
2644 
2645     ps_pic_timing = &ps_parse->s_sei_params.s_pic_timing_sei_params;
2646     ps_vui = &ps_sps->s_vui_parameters;
2647     ps_vui_hdr = &ps_vui->s_vui_hrd_parameters;
2648     ps_parse->s_sei_params.i1_pic_timing_params_present_flag = 1;
2649     if(ps_vui->u1_frame_field_info_present_flag)
2650     {
2651         BITS_PARSE("pic_struct", value, ps_bitstrm, 4);
2652         ps_pic_timing->u4_pic_struct = value;
2653 
2654         BITS_PARSE("source_scan_type", value, ps_bitstrm, 2);
2655         ps_pic_timing->u4_source_scan_type = value;
2656 
2657         BITS_PARSE("duplicate_flag", value, ps_bitstrm, 1);
2658         ps_pic_timing->u1_duplicate_flag = value;
2659     }
2660 
2661     if(ps_vui_hdr->u1_nal_hrd_parameters_present_flag
2662                     || ps_vui_hdr->u1_vcl_hrd_parameters_present_flag)
2663     {
2664         u4_cpb_dpb_delays_present_flag = 1;
2665     }
2666     else
2667     {
2668         u4_cpb_dpb_delays_present_flag = 0;
2669     }
2670 
2671     if(u4_cpb_dpb_delays_present_flag)
2672     {
2673         BITS_PARSE("au_cpb_removal_delay_minus1", value, ps_bitstrm,
2674                    (ps_vui_hdr->u1_au_cpb_removal_delay_length_minus1 + 1));
2675         ps_pic_timing->u4_au_cpb_removal_delay_minus1 = value;
2676 
2677         BITS_PARSE("pic_dpb_output_delay", value, ps_bitstrm,
2678                    (ps_vui_hdr->u1_dpb_output_delay_length_minus1 + 1));
2679         ps_pic_timing->u4_pic_dpb_output_delay = value;
2680 
2681         if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag)
2682         {
2683             BITS_PARSE("pic_dpb_output_du_delay", value, ps_bitstrm,
2684                        (ps_vui_hdr->u1_dpb_output_delay_du_length_minus1 + 1));
2685             ps_pic_timing->u4_pic_dpb_output_du_delay = value;
2686         }
2687 
2688         if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag
2689                         && ps_vui_hdr->u1_sub_pic_cpb_params_in_pic_timing_sei_flag)
2690         {
2691             UWORD32 num_units_minus1;
2692             UWORD32 array_size;
2693 
2694             UEV_PARSE("num_decoding_units_minus1", value, ps_bitstrm);
2695             if(value > (ps_sps->i4_pic_size_in_ctb -1))
2696             {
2697                 return IHEVCD_INVALID_PARAMETER;
2698             }
2699             ps_pic_timing->u4_num_decoding_units_minus1 = value;
2700 
2701             num_units_minus1 = ps_pic_timing->u4_num_decoding_units_minus1;
2702             array_size = (sizeof(ps_pic_timing->au4_num_nalus_in_du_minus1)
2703                        / sizeof(ps_pic_timing->au4_num_nalus_in_du_minus1[0]));
2704             num_units_minus1 = CLIP3(num_units_minus1, 0,(array_size - 1));
2705             ps_pic_timing->u4_num_decoding_units_minus1 = num_units_minus1;
2706 
2707             BITS_PARSE("du_common_cpb_removal_delay_flag", value, ps_bitstrm, 1);
2708             ps_pic_timing->u1_du_common_cpb_removal_delay_flag = value;
2709 
2710             if(ps_pic_timing->u1_du_common_cpb_removal_delay_flag)
2711             {
2712                 BITS_PARSE("du_common_cpb_removal_delay_increment_minus1",
2713                            value,
2714                            ps_bitstrm,
2715                            (ps_vui_hdr->u1_du_cpb_removal_delay_increment_length_minus1
2716                                            + 1));
2717                 ps_pic_timing->u4_du_common_cpb_removal_delay_increment_minus1 =
2718                                 value;
2719             }
2720 
2721             for(i = 0; i <= ps_pic_timing->u4_num_decoding_units_minus1; i++)
2722             {
2723                 UEV_PARSE("num_nalus_in_du_minus1", value, ps_bitstrm);
2724                 if(value > (ps_sps->i4_pic_size_in_ctb -1))
2725                 {
2726                     return IHEVCD_INVALID_PARAMETER;
2727                 }
2728                 ps_pic_timing->au4_num_nalus_in_du_minus1[i] = value;
2729 
2730                 if((!ps_pic_timing->u1_du_common_cpb_removal_delay_flag)
2731                                 && (i < ps_pic_timing->u4_num_decoding_units_minus1))
2732                 {
2733                     BITS_PARSE("du_common_cpb_removal_delay_increment_minus1",
2734                                value,
2735                                ps_bitstrm,
2736                                (ps_vui_hdr->u1_du_cpb_removal_delay_increment_length_minus1
2737                                                + 1));
2738                     ps_pic_timing->au4_du_cpb_removal_delay_increment_minus1[i] =
2739                                     value;
2740                 }
2741             }
2742         }
2743     }
2744 
2745     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2746 }
2747 
ihevcd_parse_time_code_sei(codec_t * ps_codec)2748 IHEVCD_ERROR_T ihevcd_parse_time_code_sei(codec_t *ps_codec)
2749 {
2750     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2751     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2752     UWORD32 value;
2753     time_code_t *ps_time_code;
2754     WORD32 i;
2755 
2756     ps_parse->s_sei_params.i1_time_code_present_flag = 1;
2757     ps_time_code = &ps_parse->s_sei_params.s_time_code;
2758 
2759     BITS_PARSE("num_clock_ts", value, ps_bitstrm, 2);
2760     ps_time_code->u1_num_clock_ts = value;
2761 
2762     for(i = 0; i < ps_time_code->u1_num_clock_ts; i++)
2763     {
2764         BITS_PARSE("clock_timestamp_flag[i]", value, ps_bitstrm, 1);
2765         ps_time_code->au1_clock_timestamp_flag[i] = value;
2766 
2767         if(ps_time_code->au1_clock_timestamp_flag[i])
2768         {
2769             BITS_PARSE("units_field_based_flag[i]", value, ps_bitstrm, 1);
2770             ps_time_code->au1_units_field_based_flag[i] = value;
2771 
2772             BITS_PARSE("counting_type[i]", value, ps_bitstrm, 5);
2773             ps_time_code->au1_counting_type[i] = value;
2774 
2775             BITS_PARSE("full_timestamp_flag[i]", value, ps_bitstrm, 1);
2776             ps_time_code->au1_full_timestamp_flag[i] = value;
2777 
2778             BITS_PARSE("discontinuity_flag[i]", value, ps_bitstrm, 1);
2779             ps_time_code->au1_discontinuity_flag[i] = value;
2780 
2781             BITS_PARSE("cnt_dropped_flag[i]", value, ps_bitstrm, 1);
2782             ps_time_code->au1_cnt_dropped_flag[i] = value;
2783 
2784             BITS_PARSE("n_frames[i]", value, ps_bitstrm, 9);
2785             ps_time_code->au2_n_frames[i] = value;
2786 
2787             if(ps_time_code->au1_full_timestamp_flag[i])
2788             {
2789                 BITS_PARSE("seconds_value[i]", value, ps_bitstrm, 6);
2790                 ps_time_code->au1_seconds_value[i] = value;
2791 
2792                 BITS_PARSE("minutes_value[i]", value, ps_bitstrm, 6);
2793                 ps_time_code->au1_minutes_value[i] = value;
2794 
2795                 BITS_PARSE("hours_value[i]", value, ps_bitstrm, 5);
2796                 ps_time_code->au1_hours_value[i] = value;
2797             }
2798             else
2799             {
2800                 BITS_PARSE("seconds_flag[i]", value, ps_bitstrm, 1);
2801                 ps_time_code->au1_seconds_flag[i] = value;
2802 
2803                 if(ps_time_code->au1_seconds_flag[i])
2804                 {
2805                     BITS_PARSE("seconds_value[i]", value, ps_bitstrm, 6);
2806                     ps_time_code->au1_seconds_value[i] = value;
2807 
2808                     BITS_PARSE("minutes_flag[i]", value, ps_bitstrm, 1);
2809                     ps_time_code->au1_minutes_flag[i] = value;
2810 
2811                     if(ps_time_code->au1_minutes_flag[i])
2812                     {
2813                         BITS_PARSE("minutes_value[i]", value, ps_bitstrm, 6);
2814                         ps_time_code->au1_minutes_value[i] = value;
2815 
2816                         BITS_PARSE("hours_flag[i]", value, ps_bitstrm, 1);
2817                         ps_time_code->au1_hours_flag[i] = value;
2818 
2819                         if(ps_time_code->au1_hours_flag[i])
2820                         {
2821                             BITS_PARSE("hours_value[i]", value, ps_bitstrm, 5);
2822                             ps_time_code->au1_hours_value[i] = value;
2823                         }
2824                     }
2825                 }
2826             }
2827 
2828             BITS_PARSE("time_offset_length[i]", value, ps_bitstrm, 5);
2829             ps_time_code->au1_time_offset_length[i] = value;
2830 
2831             if(ps_time_code->au1_time_offset_length[i] > 0)
2832             {
2833                 BITS_PARSE("time_offset_value[i]", value, ps_bitstrm,
2834                            ps_time_code->au1_time_offset_length[i]);
2835                 ps_time_code->au1_time_offset_value[i] = value;
2836             }
2837             else
2838             {
2839                 ps_time_code->au1_time_offset_value[i] = 0;
2840             }
2841         }
2842     }
2843 
2844     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2845 }
2846 
ihevcd_parse_mastering_disp_params_sei(codec_t * ps_codec)2847 IHEVCD_ERROR_T ihevcd_parse_mastering_disp_params_sei(codec_t *ps_codec)
2848 {
2849     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2850     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2851     UWORD32 value;
2852     mastering_dis_col_vol_sei_params_t *ps_mastering_dis_col_vol;
2853     WORD32 i;
2854 
2855     ps_parse->s_sei_params.i4_sei_mastering_disp_colour_vol_params_present_flags = 1;
2856 
2857     ps_mastering_dis_col_vol = &ps_parse->s_sei_params.s_mastering_dis_col_vol_sei_params;
2858 
2859     for(i = 0; i < 3; i++)
2860     {
2861         BITS_PARSE("display_primaries_x[c]", value, ps_bitstrm, 16);
2862         ps_mastering_dis_col_vol->au2_display_primaries_x[i] = value;
2863 
2864         BITS_PARSE("display_primaries_y[c]", value, ps_bitstrm, 16);
2865         ps_mastering_dis_col_vol->au2_display_primaries_y[i] = value;
2866     }
2867 
2868     BITS_PARSE("white_point_x", value, ps_bitstrm, 16);
2869     ps_mastering_dis_col_vol->u2_white_point_x = value;
2870 
2871     BITS_PARSE("white_point_y", value, ps_bitstrm, 16);
2872     ps_mastering_dis_col_vol->u2_white_point_y = value;
2873 
2874     BITS_PARSE("max_display_mastering_luminance", value, ps_bitstrm, 32);
2875     ps_mastering_dis_col_vol->u4_max_display_mastering_luminance = value;
2876 
2877     BITS_PARSE("min_display_mastering_luminance", value, ps_bitstrm, 32);
2878     ps_mastering_dis_col_vol->u4_min_display_mastering_luminance = value;
2879 
2880     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2881 }
2882 
ihevcd_parse_user_data_registered_itu_t_t35(codec_t * ps_codec,UWORD32 u4_payload_size)2883 IHEVCD_ERROR_T ihevcd_parse_user_data_registered_itu_t_t35(codec_t *ps_codec,
2884                                                            UWORD32 u4_payload_size)
2885 {
2886     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2887     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2888     UWORD32 value;
2889     user_data_registered_itu_t_t35_t *ps_user_data_registered_itu_t_t35;
2890     UWORD32 i;
2891     UWORD32 j = 0;
2892 
2893     ps_parse->s_sei_params.i1_user_data_registered_present_flag = 1;
2894     ps_user_data_registered_itu_t_t35 =
2895                     &ps_parse->s_sei_params.as_user_data_registered_itu_t_t35[ps_parse->s_sei_params.i4_sei_user_data_cnt];
2896     ps_parse->s_sei_params.i4_sei_user_data_cnt++;
2897 
2898     ps_user_data_registered_itu_t_t35->i4_payload_size = u4_payload_size;
2899 
2900     if(u4_payload_size > MAX_USERDATA_PAYLOAD)
2901     {
2902         u4_payload_size = MAX_USERDATA_PAYLOAD;
2903     }
2904 
2905     ps_user_data_registered_itu_t_t35->i4_valid_payload_size = u4_payload_size;
2906 
2907     BITS_PARSE("itu_t_t35_country_code", value, ps_bitstrm, 8);
2908     ps_user_data_registered_itu_t_t35->u1_itu_t_t35_country_code = value;
2909 
2910     if(0xFF != ps_user_data_registered_itu_t_t35->u1_itu_t_t35_country_code)
2911     {
2912         i = 1;
2913     }
2914     else
2915     {
2916         BITS_PARSE("itu_t_t35_country_code_extension_byte", value, ps_bitstrm,
2917                    8);
2918         ps_user_data_registered_itu_t_t35->u1_itu_t_t35_country_code_extension_byte =
2919                         value;
2920 
2921         i = 2;
2922     }
2923 
2924     do
2925     {
2926         BITS_PARSE("itu_t_t35_payload_byte", value, ps_bitstrm, 8);
2927         ps_user_data_registered_itu_t_t35->u1_itu_t_t35_payload_byte[j++] =
2928                         value;
2929 
2930         i++;
2931     }while(i < u4_payload_size);
2932 
2933     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2934 }
2935 
ihevcd_parse_sei_payload(codec_t * ps_codec,UWORD32 u4_payload_type,UWORD32 u4_payload_size,WORD8 i1_nal_type)2936 void ihevcd_parse_sei_payload(codec_t *ps_codec,
2937                               UWORD32 u4_payload_type,
2938                               UWORD32 u4_payload_size,
2939                               WORD8 i1_nal_type)
2940 {
2941     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2942     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2943     WORD32 payload_bits_remaining = 0;
2944     sps_t *ps_sps;
2945 
2946     UWORD32 i;
2947 
2948     for(i = 0; i < MAX_SPS_CNT; i++)
2949     {
2950         ps_sps = ps_codec->ps_sps_base + i;
2951         if(ps_sps->i1_sps_valid)
2952         {
2953             break;
2954         }
2955     }
2956     if(!ps_sps->i1_sps_valid)
2957     {
2958         return;
2959     }
2960 
2961     if(NAL_PREFIX_SEI == i1_nal_type)
2962     {
2963         switch(u4_payload_type)
2964         {
2965             case SEI_BUFFERING_PERIOD:
2966                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
2967                 ihevcd_parse_buffering_period_sei(ps_codec, ps_sps);
2968                 break;
2969 
2970             case SEI_PICTURE_TIMING:
2971                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
2972                 ihevcd_parse_pic_timing_sei(ps_codec, ps_sps);
2973                 break;
2974 
2975             case SEI_TIME_CODE:
2976                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
2977                 ihevcd_parse_time_code_sei(ps_codec);
2978                 break;
2979 
2980             case SEI_MASTERING_DISPLAY_COLOUR_VOLUME:
2981                 ps_parse->s_sei_params.i4_sei_mastering_disp_colour_vol_params_present_flags = 1;
2982                 ihevcd_parse_mastering_disp_params_sei(ps_codec);
2983                 break;
2984 
2985             case SEI_USER_DATA_REGISTERED_ITU_T_T35:
2986                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
2987                 if(ps_parse->s_sei_params.i4_sei_user_data_cnt >= USER_DATA_MAX)
2988                 {
2989                     for(i = 0; i < u4_payload_size / 4; i++)
2990                     {
2991                         ihevcd_bits_flush(ps_bitstrm, 4 * 8);
2992                     }
2993 
2994                     ihevcd_bits_flush(ps_bitstrm, (u4_payload_size - i * 4) * 8);
2995                 }
2996                 else
2997                 {
2998                     ihevcd_parse_user_data_registered_itu_t_t35(ps_codec,
2999                                                                 u4_payload_size);
3000                 }
3001                 break;
3002 
3003             default:
3004                 for(i = 0; i < u4_payload_size; i++)
3005                 {
3006                     ihevcd_bits_flush(ps_bitstrm, 8);
3007                 }
3008                 break;
3009         }
3010     }
3011     else /* NAL_SUFFIX_SEI */
3012     {
3013         switch(u4_payload_type)
3014         {
3015             case SEI_USER_DATA_REGISTERED_ITU_T_T35:
3016                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
3017                 if(ps_parse->s_sei_params.i4_sei_user_data_cnt >= USER_DATA_MAX)
3018                 {
3019                     for(i = 0; i < u4_payload_size / 4; i++)
3020                     {
3021                         ihevcd_bits_flush(ps_bitstrm, 4 * 8);
3022                     }
3023 
3024                     ihevcd_bits_flush(ps_bitstrm, (u4_payload_size - i * 4) * 8);
3025                 }
3026                 else
3027                 {
3028                     ihevcd_parse_user_data_registered_itu_t_t35(ps_codec,
3029                                                                 u4_payload_size);
3030                 }
3031                 break;
3032 
3033             default:
3034                 for(i = 0; i < u4_payload_size; i++)
3035                 {
3036                     ihevcd_bits_flush(ps_bitstrm, 8);
3037                 }
3038                 break;
3039         }
3040     }
3041 
3042     /**
3043      * By definition the underlying bitstream terminates in a byte-aligned manner.
3044      * 1. Extract all bar the last MIN(bitsremaining,nine) bits as reserved_payload_extension_data
3045      * 2. Examine the final 8 bits to determine the payload_bit_equal_to_one marker
3046      * 3. Extract the remainingreserved_payload_extension_data bits.
3047      *
3048      * If there are fewer than 9 bits available, extract them.
3049      */
3050 
3051     payload_bits_remaining = ihevcd_bits_num_bits_remaining(ps_bitstrm);
3052     if(payload_bits_remaining) /* more_data_in_payload() */
3053     {
3054         WORD32 final_bits;
3055         WORD32 final_payload_bits = 0;
3056         WORD32 mask = 0xFF;
3057         UWORD32 u4_dummy;
3058         UWORD32 u4_reserved_payload_extension_data;
3059         UNUSED(u4_dummy);
3060         UNUSED(u4_reserved_payload_extension_data);
3061 
3062         while(payload_bits_remaining > 9)
3063         {
3064             BITS_PARSE("reserved_payload_extension_data",
3065                        u4_reserved_payload_extension_data, ps_bitstrm, 1);
3066             payload_bits_remaining--;
3067         }
3068 
3069         final_bits = ihevcd_bits_nxt(ps_bitstrm, payload_bits_remaining);
3070 
3071         while(final_bits & (mask >> final_payload_bits))
3072         {
3073             final_payload_bits++;
3074             continue;
3075         }
3076 
3077         while(payload_bits_remaining > (9 - final_payload_bits))
3078         {
3079             BITS_PARSE("reserved_payload_extension_data",
3080                        u4_reserved_payload_extension_data, ps_bitstrm, 1);
3081             payload_bits_remaining--;
3082         }
3083 
3084         BITS_PARSE("payload_bit_equal_to_one", u4_dummy, ps_bitstrm, 1);
3085         payload_bits_remaining--;
3086         while(payload_bits_remaining)
3087         {
3088             BITS_PARSE("payload_bit_equal_to_zero", u4_dummy, ps_bitstrm, 1);
3089             payload_bits_remaining--;
3090         }
3091     }
3092 
3093     return;
3094 }
3095 
ihevcd_read_rbsp_trailing_bits(codec_t * ps_codec,UWORD32 u4_bits_left)3096 IHEVCD_ERROR_T ihevcd_read_rbsp_trailing_bits(codec_t *ps_codec,
3097                                               UWORD32 u4_bits_left)
3098 {
3099     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
3100     UWORD32 value;
3101     WORD32 cnt = 0;
3102     BITS_PARSE("rbsp_stop_one_bit", value, &ps_parse->s_bitstrm, 1);
3103     u4_bits_left--;
3104     if(value != 1)
3105     {
3106         return (IHEVCD_ERROR_T)IHEVCD_FAIL;
3107     }
3108     while(u4_bits_left)
3109     {
3110         BITS_PARSE("rbsp_alignment_zero_bit", value, &ps_parse->s_bitstrm, 1);
3111         u4_bits_left--;
3112         cnt++;
3113     }
3114     ASSERT(cnt < 8);
3115 
3116     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
3117 }
3118 /**
3119 *******************************************************************************
3120 *
3121 * @brief
3122 *  Parses SEI (Supplemental Enhancement Information)
3123 *
3124 * @par Description:
3125 *  Parses SEI (Supplemental Enhancement Information) as per Section: 7.3.7
3126 *
3127 * @param[in] ps_codec
3128 *  Pointer to codec context
3129 *
3130 * @returns Error code from IHEVCD_ERROR_T
3131 *
3132 * @remarks
3133 *
3134 *
3135 *******************************************************************************
3136 */
ihevcd_parse_sei(codec_t * ps_codec,nal_header_t * ps_nal)3137 IHEVCD_ERROR_T ihevcd_parse_sei(codec_t *ps_codec, nal_header_t *ps_nal)
3138 {
3139     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
3140     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
3141     UWORD32 u4_payload_type = 0, u4_last_payload_type_byte = 0;
3142     UWORD32 u4_payload_size = 0, u4_last_payload_size_byte = 0;
3143     UWORD32 value;
3144     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
3145     UWORD32 u4_bits_left;
3146 
3147     u4_bits_left = ihevcd_bits_num_bits_remaining(ps_bitstrm);
3148 
3149     while(u4_bits_left > 8)
3150     {
3151         while(ihevcd_bits_nxt(ps_bitstrm, 8) == 0xFF)
3152         {
3153             ihevcd_bits_flush(ps_bitstrm, 8); /* equal to 0xFF */
3154             u4_payload_type += 255;
3155         }
3156 
3157         BITS_PARSE("last_payload_type_byte", value, ps_bitstrm, 8);
3158         u4_last_payload_type_byte = value;
3159 
3160         u4_payload_type += u4_last_payload_type_byte;
3161 
3162         while(ihevcd_bits_nxt(ps_bitstrm, 8) == 0xFF)
3163         {
3164             ihevcd_bits_flush(ps_bitstrm, 8); /* equal to 0xFF */
3165             u4_payload_size += 255;
3166         }
3167 
3168         BITS_PARSE("last_payload_size_byte", value, ps_bitstrm, 8);
3169         u4_last_payload_size_byte = value;
3170 
3171         u4_payload_size += u4_last_payload_size_byte;
3172         u4_bits_left = ihevcd_bits_num_bits_remaining(ps_bitstrm);
3173         u4_payload_size = MIN(u4_payload_size, u4_bits_left / 8);
3174         ihevcd_parse_sei_payload(ps_codec, u4_payload_type, u4_payload_size,
3175                                  ps_nal->i1_nal_unit_type);
3176 
3177         /* Calculate the bits left in the current payload */
3178         u4_bits_left = ihevcd_bits_num_bits_remaining(ps_bitstrm);
3179     }
3180 
3181     // read rbsp_trailing_bits
3182     if(u4_bits_left)
3183     {
3184         ihevcd_read_rbsp_trailing_bits(ps_codec, u4_bits_left);
3185     }
3186 
3187     return ret;
3188 }
3189 #endif
3190 
3191 /**
3192 *******************************************************************************
3193 *
3194 * @brief
3195 *  Parses Access unit delimiter
3196 *
3197 * @par Description:
3198 *  Parses Access unit delimiter as per section  Section: 7.3.2.5
3199 *
3200 * @param[in] ps_codec
3201 *  Pointer to codec context
3202 *
3203 * @returns Error code from IHEVCD_ERROR_T
3204 *
3205 * @remarks
3206 *
3207 *
3208 *******************************************************************************
3209 */
ihevcd_parse_aud(codec_t * ps_codec)3210 WORD32 ihevcd_parse_aud(codec_t *ps_codec)
3211 {
3212     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
3213     UNUSED(ps_codec);
3214     return ret;
3215 }
3216 
ihevcd_extend_sign_bit(WORD32 value,WORD32 num_bits)3217 WORD32 ihevcd_extend_sign_bit(WORD32 value, WORD32 num_bits)
3218 {
3219     WORD32 ret_value = value;
3220     if(value >> (num_bits - 1))
3221     {
3222         ret_value |= (0xFFFFFFFF << num_bits);
3223     }
3224     return ret_value;
3225 }
3226 
3227 /**
3228 *******************************************************************************
3229 *
3230 * @brief
3231 *  Calculate POC of the current slice
3232 *
3233 * @par Description:
3234 *  Calculates the current POC using the previous POC lsb and previous POC msb
3235 *
3236 * @param[in] ps_codec
3237 *  Pointer to codec context
3238 *
3239 * @param[in] i1_pic_order_cnt_lsb
3240 *  Current POC lsb
3241 *
3242 * @returns  Current absolute POC
3243 *
3244 * @remarks
3245 *
3246 *
3247 *******************************************************************************
3248 */
3249 
ihevcd_calc_poc(codec_t * ps_codec,nal_header_t * ps_nal,WORD8 i1_log2_max_poc_lsb,WORD32 i2_poc_lsb)3250 WORD32 ihevcd_calc_poc(codec_t *ps_codec, nal_header_t *ps_nal, WORD8 i1_log2_max_poc_lsb, WORD32 i2_poc_lsb)
3251 {
3252     WORD32 i4_abs_poc, i4_poc_msb;
3253     WORD32 max_poc_lsb;
3254     WORD8 i1_nal_unit_type = ps_nal->i1_nal_unit_type;
3255     max_poc_lsb = (1 << i1_log2_max_poc_lsb);
3256 
3257     if((!ps_codec->i4_first_pic_done) && (!ps_codec->i4_pic_present))
3258         ps_codec->i4_prev_poc_msb = -2 * max_poc_lsb;
3259 
3260     if(NAL_IDR_N_LP == i1_nal_unit_type
3261                     || NAL_IDR_W_LP == i1_nal_unit_type
3262                     || NAL_BLA_N_LP == i1_nal_unit_type
3263                     || NAL_BLA_W_DLP == i1_nal_unit_type
3264                     || NAL_BLA_W_LP == i1_nal_unit_type
3265                     || (NAL_CRA == i1_nal_unit_type && !ps_codec->i4_first_pic_done))
3266     {
3267         i4_poc_msb = ps_codec->i4_prev_poc_msb + 2 * max_poc_lsb;
3268         ps_codec->i4_prev_poc_lsb = 0;
3269         ps_codec->i4_max_prev_poc_lsb = 0;
3270 //        ps_codec->i4_prev_poc_msb = 0;
3271     }
3272     else
3273     {
3274 
3275         if((i2_poc_lsb < ps_codec->i4_prev_poc_lsb)
3276                         && ((ps_codec->i4_prev_poc_lsb - i2_poc_lsb) >= max_poc_lsb / 2))
3277         {
3278             i4_poc_msb = ps_codec->i4_prev_poc_msb + max_poc_lsb;
3279         }
3280         else if((i2_poc_lsb > ps_codec->i4_prev_poc_lsb)
3281                         && ((i2_poc_lsb - ps_codec->i4_prev_poc_lsb) > max_poc_lsb / 2))
3282         {
3283             i4_poc_msb = ps_codec->i4_prev_poc_msb - max_poc_lsb;
3284         }
3285         else
3286         {
3287             i4_poc_msb = ps_codec->i4_prev_poc_msb;
3288         }
3289 
3290 
3291     }
3292 
3293     i4_abs_poc = i4_poc_msb + i2_poc_lsb;
3294     ps_codec->i4_max_prev_poc_lsb = MAX(ps_codec->i4_max_prev_poc_lsb, i2_poc_lsb);
3295 
3296     {
3297         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));
3298         WORD32 update_prev_poc = ((is_reference_nal) && ((i1_nal_unit_type < NAL_RADL_N) || (i1_nal_unit_type > NAL_RASL_R)));
3299 
3300         if((0 == ps_nal->i1_nuh_temporal_id) &&
3301                         (update_prev_poc))
3302         {
3303             ps_codec->i4_prev_poc_lsb = i2_poc_lsb;
3304             ps_codec->i4_prev_poc_msb = i4_poc_msb;
3305         }
3306     }
3307 
3308     return i4_abs_poc;
3309 }
3310 
3311 
ihevcd_copy_slice_hdr(codec_t * ps_codec,WORD32 slice_idx,WORD32 slice_idx_ref)3312 void ihevcd_copy_slice_hdr(codec_t *ps_codec, WORD32 slice_idx, WORD32 slice_idx_ref)
3313 {
3314     slice_header_t *ps_slice_hdr, *ps_slice_hdr_ref;
3315     WORD32 *pu4_entry_offset_backup;
3316 
3317     ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr_base + slice_idx;
3318     ps_slice_hdr_ref = ps_codec->s_parse.ps_slice_hdr_base + slice_idx_ref;
3319 
3320     pu4_entry_offset_backup = ps_slice_hdr->pu4_entry_point_offset;
3321     memcpy(ps_slice_hdr, ps_slice_hdr_ref, sizeof(slice_header_t));
3322     ps_slice_hdr->pu4_entry_point_offset = pu4_entry_offset_backup;
3323 }
3324 
3325 
3326 
3327