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