• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2 *
3 * Copyright (C) 2012 Ittiam Systems Pvt Ltd, Bangalore
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18 /**
19 *******************************************************************************
20 * @file
21 *  ihevcd_parse_headers.c
22 *
23 * @brief
24 *  Contains functions for parsing headers
25 *
26 * @author
27 *  Harish
28 *
29 * @par List of Functions:
30 *
31 * @remarks
32 *  None
33 *
34 *******************************************************************************
35 */
36 
37 /*****************************************************************************/
38 /* File Includes                                                             */
39 /*****************************************************************************/
40 #include <stdio.h>
41 #include <stddef.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <assert.h>
45 
46 #include "ihevc_typedefs.h"
47 #include "iv.h"
48 #include "ivd.h"
49 #include "ihevcd_cxa.h"
50 
51 #include "ihevc_defs.h"
52 #include "ihevc_debug.h"
53 #include "ihevc_defs.h"
54 #include "ihevc_structs.h"
55 #include "ihevc_buf_mgr.h"
56 #include "ihevc_dpb_mgr.h"
57 #include "ihevc_macros.h"
58 #include "ihevc_platform_macros.h"
59 #include "ihevc_cabac_tables.h"
60 #include "ihevc_common_tables.h"
61 #include "ihevc_quant_tables.h"
62 
63 #include "ihevcd_trace.h"
64 #include "ihevcd_defs.h"
65 #include "ihevcd_function_selector.h"
66 #include "ihevcd_structs.h"
67 #include "ihevcd_error.h"
68 #include "ihevcd_debug.h"
69 #include "ihevcd_nal.h"
70 #include "ihevcd_bitstream.h"
71 #include "ihevcd_parse_headers.h"
72 #include "ihevcd_ref_list.h"
73 
74 #define COPY_DEFAULT_SCALING_LIST(pi2_scaling_mat)                                                                                      \
75 {                                                                                                                                       \
76     WORD32 scaling_mat_offset[]={0, 16, 32, 48, 64, 80, 96, 160, 224, 288, 352, 416, 480, 736, 992, 1248, 1504, 1760, 2016, 3040};      \
77                                                                                                                                         \
78     /* scaling matrix for 4x4 */                                                                                                        \
79     memcpy(pi2_scaling_mat, gi2_flat_scale_mat_32x32, 6*16*sizeof(WORD16));                                                             \
80 /* scaling matrix for 8x8 */                                                                                                            \
81     memcpy(pi2_scaling_mat + scaling_mat_offset[6], gi2_intra_default_scale_mat_8x8, 64*sizeof(WORD16));                                \
82     memcpy(pi2_scaling_mat + scaling_mat_offset[7], gi2_intra_default_scale_mat_8x8, 64*sizeof(WORD16));                                \
83     memcpy(pi2_scaling_mat + scaling_mat_offset[8], gi2_intra_default_scale_mat_8x8, 64*sizeof(WORD16));                                \
84     memcpy(pi2_scaling_mat + scaling_mat_offset[9], gi2_inter_default_scale_mat_8x8, 64*sizeof(WORD16));                                \
85     memcpy(pi2_scaling_mat + scaling_mat_offset[10], gi2_inter_default_scale_mat_8x8, 64*sizeof(WORD16));                               \
86     memcpy(pi2_scaling_mat + scaling_mat_offset[11], gi2_inter_default_scale_mat_8x8, 64*sizeof(WORD16));                               \
87     /* scaling matrix for 16x16 */                                                                                                      \
88     memcpy(pi2_scaling_mat + scaling_mat_offset[12], gi2_intra_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
89     memcpy(pi2_scaling_mat + scaling_mat_offset[13], gi2_intra_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
90     memcpy(pi2_scaling_mat + scaling_mat_offset[14], gi2_intra_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
91     memcpy(pi2_scaling_mat + scaling_mat_offset[15], gi2_inter_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
92     memcpy(pi2_scaling_mat + scaling_mat_offset[16], gi2_inter_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
93     memcpy(pi2_scaling_mat + scaling_mat_offset[17], gi2_inter_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
94     /* scaling matrix for 32x32 */                                                                                                      \
95     memcpy(pi2_scaling_mat + scaling_mat_offset[18], gi2_intra_default_scale_mat_32x32, 1024*sizeof(WORD16));                           \
96     memcpy(pi2_scaling_mat + scaling_mat_offset[19], gi2_inter_default_scale_mat_32x32, 1024*sizeof(WORD16));                           \
97 }
98 
99 #define COPY_FLAT_SCALING_LIST(pi2_scaling_mat)                                                                                         \
100 {                                                                                                                                       \
101     WORD32 scaling_mat_offset[]={0, 16, 32, 48, 64, 80, 96, 160, 224, 288, 352, 416, 480, 736, 992, 1248, 1504, 1760, 2016, 3040};      \
102                                                                                                                                         \
103     /* scaling matrix for 4x4 */                                                                                                        \
104     memcpy(pi2_scaling_mat, gi2_flat_scale_mat_32x32, 6*16*sizeof(WORD16));                                                             \
105     /* scaling matrix for 8x8 */                                                                                                        \
106     memcpy(pi2_scaling_mat + scaling_mat_offset[6], gi2_flat_scale_mat_32x32, 6*64*sizeof(WORD16));                                     \
107     /* scaling matrix for 16x16 */                                                                                                      \
108     memcpy(pi2_scaling_mat + scaling_mat_offset[12], gi2_flat_scale_mat_32x32, 3*256*sizeof(WORD16));                                   \
109     memcpy(pi2_scaling_mat + scaling_mat_offset[15], gi2_flat_scale_mat_32x32, 3*256*sizeof(WORD16));                                   \
110     /* scaling matrix for 32x32 */                                                                                                      \
111     memcpy(pi2_scaling_mat + scaling_mat_offset[18], gi2_flat_scale_mat_32x32, 1024*sizeof(WORD16));                                    \
112     memcpy(pi2_scaling_mat + scaling_mat_offset[19], gi2_flat_scale_mat_32x32, 1024*sizeof(WORD16));                                    \
113 }
114 
115 /* Function declarations */
116 
117 /**
118 *******************************************************************************
119 *
120 * @brief
121 *  Parses Prediction weight table syntax
122 *
123 * @par Description:
124 *  Parse Prediction weight table syntax as per Section: 7.3.8.4
125 *
126 * @param[in] ps_bitstrm
127 *  Pointer to bitstream context
128 *
129 * @param[in] ps_sps
130 *  Current SPS
131 *
132 * @param[in] ps_pps
133 *  Current PPS
134 *
135 * @param[in] ps_slice_hdr
136 *  Current Slice header
137 *
138 * @returns  Error code from IHEVCD_ERROR_T
139 *
140 * @remarks
141 *
142 *
143 *******************************************************************************
144 */
145 
ihevcd_parse_pred_wt_ofst(bitstrm_t * ps_bitstrm,sps_t * ps_sps,pps_t * ps_pps,slice_header_t * ps_slice_hdr)146 WORD32 ihevcd_parse_pred_wt_ofst(bitstrm_t *ps_bitstrm,
147                                  sps_t *ps_sps,
148                                  pps_t *ps_pps,
149                                  slice_header_t *ps_slice_hdr)
150 {
151     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
152     WORD32 value;
153     WORD32 i;
154 
155     pred_wt_ofst_t *ps_wt_ofst = &ps_slice_hdr->s_wt_ofst;
156     UNUSED(ps_pps);
157 
158     UEV_PARSE("luma_log2_weight_denom", value, ps_bitstrm);
159     ps_wt_ofst->i1_luma_log2_weight_denom = value;
160 
161     if(ps_sps->i1_chroma_format_idc != 0)
162     {
163         SEV_PARSE("delta_chroma_log2_weight_denom", value, ps_bitstrm);
164         ps_wt_ofst->i1_chroma_log2_weight_denom = ps_wt_ofst->i1_luma_log2_weight_denom + value;
165     }
166 
167     for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
168     {
169         BITS_PARSE("luma_weight_l0_flag[ i ]", value, ps_bitstrm, 1);
170         ps_wt_ofst->i1_luma_weight_l0_flag[i] = value;
171     }
172 
173 
174 
175     if(ps_sps->i1_chroma_format_idc != 0)
176     {
177         for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
178         {
179             BITS_PARSE("chroma_weight_l0_flag[ i ]", value, ps_bitstrm, 1);
180             ps_wt_ofst->i1_chroma_weight_l0_flag[i] = value;
181         }
182     }
183     else
184     {
185         for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
186         {
187             ps_wt_ofst->i1_chroma_weight_l0_flag[i] = 0;
188         }
189     }
190 
191 
192     for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
193     {
194         if(ps_wt_ofst->i1_luma_weight_l0_flag[i])
195         {
196             SEV_PARSE("delta_luma_weight_l0[ i ]", value, ps_bitstrm);
197 
198 
199             ps_wt_ofst->i2_luma_weight_l0[i] = (1 << ps_wt_ofst->i1_luma_log2_weight_denom) + value;
200 
201             SEV_PARSE("luma_offset_l0[ i ]", value, ps_bitstrm);
202             ps_wt_ofst->i2_luma_offset_l0[i] = value;
203 
204         }
205         else
206         {
207             ps_wt_ofst->i2_luma_weight_l0[i] = (1 << ps_wt_ofst->i1_luma_log2_weight_denom);
208             ps_wt_ofst->i2_luma_offset_l0[i] = 0;
209         }
210         if(ps_wt_ofst->i1_chroma_weight_l0_flag[i])
211         {
212             WORD32 ofst;
213             WORD32 shift = (1 << (BIT_DEPTH_CHROMA - 1));
214             SEV_PARSE("delta_chroma_weight_l0[ i ][ j ]", value, ps_bitstrm);
215             ps_wt_ofst->i2_chroma_weight_l0_cb[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom) + value;
216 
217 
218             SEV_PARSE("delta_chroma_offset_l0[ i ][ j ]", value, ps_bitstrm);
219             ofst = ((shift * ps_wt_ofst->i2_chroma_weight_l0_cb[i]) >> ps_wt_ofst->i1_chroma_log2_weight_denom);
220             ofst = value - ofst + shift;
221 
222             ps_wt_ofst->i2_chroma_offset_l0_cb[i] = CLIP_S8(ofst);
223 
224             SEV_PARSE("delta_chroma_weight_l0[ i ][ j ]", value, ps_bitstrm);
225             ps_wt_ofst->i2_chroma_weight_l0_cr[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom) + value;
226 
227 
228             SEV_PARSE("delta_chroma_offset_l0[ i ][ j ]", value, ps_bitstrm);
229             ofst = ((shift * ps_wt_ofst->i2_chroma_weight_l0_cr[i]) >> ps_wt_ofst->i1_chroma_log2_weight_denom);
230             ofst = value - ofst + shift;
231 
232             ps_wt_ofst->i2_chroma_offset_l0_cr[i] = CLIP_S8(ofst);
233 
234         }
235         else
236         {
237             ps_wt_ofst->i2_chroma_weight_l0_cb[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom);
238             ps_wt_ofst->i2_chroma_weight_l0_cr[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom);
239 
240             ps_wt_ofst->i2_chroma_offset_l0_cb[i] = 0;
241             ps_wt_ofst->i2_chroma_offset_l0_cr[i] = 0;
242         }
243     }
244     if(BSLICE == ps_slice_hdr->i1_slice_type)
245     {
246         for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
247         {
248             BITS_PARSE("luma_weight_l1_flag[ i ]", value, ps_bitstrm, 1);
249             ps_wt_ofst->i1_luma_weight_l1_flag[i] = value;
250         }
251 
252         if(ps_sps->i1_chroma_format_idc != 0)
253         {
254             for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
255             {
256                 BITS_PARSE("chroma_weight_l1_flag[ i ]", value, ps_bitstrm, 1);
257                 ps_wt_ofst->i1_chroma_weight_l1_flag[i] = value;
258             }
259         }
260         else
261         {
262             for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
263             {
264                 ps_wt_ofst->i1_chroma_weight_l1_flag[i] = 0;
265             }
266         }
267 
268         for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
269         {
270             if(ps_wt_ofst->i1_luma_weight_l1_flag[i])
271             {
272                 SEV_PARSE("delta_luma_weight_l1[ i ]", value, ps_bitstrm);
273 
274 
275                 ps_wt_ofst->i2_luma_weight_l1[i] = (1 << ps_wt_ofst->i1_luma_log2_weight_denom) + value;
276 
277                 SEV_PARSE("luma_offset_l1[ i ]", value, ps_bitstrm);
278                 ps_wt_ofst->i2_luma_offset_l1[i] = value;
279 
280             }
281             else
282             {
283                 ps_wt_ofst->i2_luma_weight_l1[i] = (1 << ps_wt_ofst->i1_luma_log2_weight_denom);
284                 ps_wt_ofst->i2_luma_offset_l1[i] = 0;
285             }
286 
287             if(ps_wt_ofst->i1_chroma_weight_l1_flag[i])
288             {
289                 WORD32 ofst;
290                 WORD32 shift = (1 << (BIT_DEPTH_CHROMA - 1));
291                 SEV_PARSE("delta_chroma_weight_l1[ i ][ j ]", value, ps_bitstrm);
292                 ps_wt_ofst->i2_chroma_weight_l1_cb[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom) + value;;
293 
294 
295                 SEV_PARSE("delta_chroma_offset_l1[ i ][ j ]", value, ps_bitstrm);
296                 ofst = ((shift * ps_wt_ofst->i2_chroma_weight_l1_cb[i]) >> ps_wt_ofst->i1_chroma_log2_weight_denom);
297                 ofst = value - ofst + shift;
298 
299                 ps_wt_ofst->i2_chroma_offset_l1_cb[i] = CLIP_S8(ofst);;
300 
301                 SEV_PARSE("delta_chroma_weight_l1[ i ][ j ]", value, ps_bitstrm);
302                 ps_wt_ofst->i2_chroma_weight_l1_cr[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom) + value;
303 
304 
305                 SEV_PARSE("delta_chroma_offset_l1[ i ][ j ]", value, ps_bitstrm);
306                 ofst = ((shift * ps_wt_ofst->i2_chroma_weight_l1_cr[i]) >> ps_wt_ofst->i1_chroma_log2_weight_denom);
307                 ofst = value - ofst + shift;
308 
309                 ps_wt_ofst->i2_chroma_offset_l1_cr[i] = CLIP_S8(ofst);;
310 
311             }
312             else
313             {
314                 ps_wt_ofst->i2_chroma_weight_l1_cb[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom);
315                 ps_wt_ofst->i2_chroma_weight_l1_cr[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom);
316 
317                 ps_wt_ofst->i2_chroma_offset_l1_cb[i] = 0;
318                 ps_wt_ofst->i2_chroma_offset_l1_cr[i] = 0;
319 
320             }
321         }
322     }
323     return ret;
324 }
325 
326 /**
327 *******************************************************************************
328 *
329 * @brief
330 *  Parses short term reference picture set
331 *
332 * @par   Description
333 *  Parses short term reference picture set as per section 7.3.8.2.
334 * Can be called by either SPS or Slice header parsing modules.
335 *
336 * @param[in] ps_bitstrm
337 *  Pointer to bitstream structure
338 *
339 * @param[out] ps_stref_picset_base
340 *  Pointer to first short term ref pic set structure
341 *
342 * @param[in] num_short_term_ref_pic_sets
343 *  Number of short term reference pic sets
344 *
345 * @param[in] idx
346 *  Current short term ref pic set id
347 *
348 * @returns Error code from IHEVCD_ERROR_T
349 *
350 * @remarks
351 *
352 *******************************************************************************
353 */
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)354 IHEVCD_ERROR_T ihevcd_short_term_ref_pic_set(bitstrm_t *ps_bitstrm,
355                                              stref_picset_t *ps_stref_picset_base,
356                                              WORD32 num_short_term_ref_pic_sets,
357                                              WORD32 idx,
358                                              stref_picset_t *ps_stref_picset)
359 {
360     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
361     WORD32 value;
362     stref_picset_t *ps_stref_picset_ref;
363     WORD32 delta_idx, delta_rps;
364     WORD32 r_idx;
365     WORD32 i;
366     WORD32 j, k, temp;
367     if(idx > 0)
368     {
369         BITS_PARSE("inter_ref_pic_set_prediction_flag", value, ps_bitstrm, 1);
370         ps_stref_picset->i1_inter_ref_pic_set_prediction_flag = value;
371     }
372     else
373         ps_stref_picset->i1_inter_ref_pic_set_prediction_flag = 0;
374 
375     if(ps_stref_picset->i1_inter_ref_pic_set_prediction_flag)
376     {
377         WORD32 delta_rps_sign;
378         WORD32 abs_delta_rps;
379         WORD32 num_neg_pics = 0;
380         WORD32 num_pos_pics = 0;
381         WORD32 num_pics = 0;
382 
383         if(idx == num_short_term_ref_pic_sets)
384         {
385             UEV_PARSE("delta_idx_minus1", value, ps_bitstrm);
386             delta_idx = value + 1;
387         }
388         else
389         {
390             delta_idx = 1;
391         }
392         r_idx = idx - delta_idx;
393         r_idx = CLIP3(r_idx, 0, idx - 1);
394 
395         ps_stref_picset_ref = ps_stref_picset_base + r_idx;
396 
397         BITS_PARSE("delta_rps_sign", value, ps_bitstrm, 1);
398         delta_rps_sign = value;
399 
400         UEV_PARSE("abs_delta_rps_minus1", value, ps_bitstrm);
401         abs_delta_rps = value + 1;
402 
403         delta_rps = (1 - 2 * delta_rps_sign) * (abs_delta_rps);
404 
405 
406 
407         for(i = 0; i <= ps_stref_picset_ref->i1_num_delta_pocs; i++)
408         {
409             WORD32 ref_idc;
410 
411             /*****************************************************************/
412             /* ref_idc is parsed as below                                    */
413             /* bits "1" ref_idc 1                                            */
414             /* bits "01" ref_idc 2                                           */
415             /* bits "00" ref_idc 0                                           */
416             /*****************************************************************/
417             BITS_PARSE("used_by_curr_pic_flag", value, ps_bitstrm, 1);
418             ref_idc = value;
419             ps_stref_picset->ai1_used[num_pics] = value;
420             /* If ref_idc is zero check for next bit */
421             if(0 == ref_idc)
422             {
423                 BITS_PARSE("use_delta_flag", value, ps_bitstrm, 1);
424                 ps_stref_picset->ai1_used[i] = value;
425                 ref_idc = value << 1;
426             }
427             if((ref_idc == 1) || (ref_idc == 2))
428             {
429                 WORD32 delta_poc;
430                 delta_poc = delta_rps;
431                 delta_poc +=
432                                 ((i < ps_stref_picset_ref->i1_num_delta_pocs) ?
433                                 ps_stref_picset_ref->ai2_delta_poc[i] :
434                                 0);
435 
436                 ps_stref_picset->ai2_delta_poc[num_pics] = delta_poc;
437 
438                 if(delta_poc < 0)
439                 {
440                     num_neg_pics++;
441                 }
442                 else
443                 {
444                     num_pos_pics++;
445                 }
446                 num_pics++;
447             }
448             ps_stref_picset->ai1_ref_idc[i] = ref_idc;
449         }
450 
451         num_neg_pics = CLIP3(num_neg_pics, 0, MAX_DPB_SIZE - 1);
452         num_pos_pics = CLIP3(num_pos_pics, 0, (MAX_DPB_SIZE - 1 - num_neg_pics));
453         num_pics = num_neg_pics + num_pos_pics;
454 
455         ps_stref_picset->i1_num_ref_idc =
456                         ps_stref_picset_ref->i1_num_delta_pocs + 1;
457         ps_stref_picset->i1_num_delta_pocs = num_pics;
458         ps_stref_picset->i1_num_pos_pics = num_pos_pics;
459         ps_stref_picset->i1_num_neg_pics = num_neg_pics;
460 
461 
462         for(j = 1; j < num_pics; j++)
463         {
464             WORD32 delta_poc = ps_stref_picset->ai2_delta_poc[j];
465             WORD8 i1_used = ps_stref_picset->ai1_used[j];
466             for(k = j - 1; k >= 0; k--)
467             {
468                 temp = ps_stref_picset->ai2_delta_poc[k];
469                 if(delta_poc < temp)
470                 {
471                     ps_stref_picset->ai2_delta_poc[k + 1] = temp;
472                     ps_stref_picset->ai1_used[k + 1] = ps_stref_picset->ai1_used[k];
473                     ps_stref_picset->ai2_delta_poc[k] = delta_poc;
474                     ps_stref_picset->ai1_used[k] = i1_used;
475                 }
476             }
477         }
478         // flip the negative values to largest first
479         for(j = 0, k = num_neg_pics - 1; j < num_neg_pics >> 1; j++, k--)
480         {
481             WORD32 delta_poc = ps_stref_picset->ai2_delta_poc[j];
482             WORD8 i1_used = ps_stref_picset->ai1_used[j];
483             ps_stref_picset->ai2_delta_poc[j] = ps_stref_picset->ai2_delta_poc[k];
484             ps_stref_picset->ai1_used[j] = ps_stref_picset->ai1_used[k];
485             ps_stref_picset->ai2_delta_poc[k] = delta_poc;
486             ps_stref_picset->ai1_used[k] = i1_used;
487         }
488 
489     }
490     else
491     {
492         WORD32 prev_poc = 0;
493         WORD32 poc;
494 
495         UEV_PARSE("num_negative_pics", value, ps_bitstrm);
496         ps_stref_picset->i1_num_neg_pics = value;
497         ps_stref_picset->i1_num_neg_pics = CLIP3(ps_stref_picset->i1_num_neg_pics,
498                                                  0,
499                                                  MAX_DPB_SIZE - 1);
500 
501         UEV_PARSE("num_positive_pics", value, ps_bitstrm);
502         ps_stref_picset->i1_num_pos_pics = value;
503         ps_stref_picset->i1_num_pos_pics = CLIP3(ps_stref_picset->i1_num_pos_pics,
504                                                  0,
505                                                  (MAX_DPB_SIZE - 1 - ps_stref_picset->i1_num_neg_pics));
506 
507         ps_stref_picset->i1_num_delta_pocs =
508                         ps_stref_picset->i1_num_neg_pics +
509                         ps_stref_picset->i1_num_pos_pics;
510 
511 
512         for(i = 0; i < ps_stref_picset->i1_num_neg_pics; i++)
513         {
514             UEV_PARSE("delta_poc_s0_minus1", value, ps_bitstrm);
515             poc = prev_poc - (value + 1);
516             prev_poc = poc;
517             ps_stref_picset->ai2_delta_poc[i] = poc;
518 
519             BITS_PARSE("used_by_curr_pic_s0_flag", value, ps_bitstrm, 1);
520             ps_stref_picset->ai1_used[i] = value;
521 
522         }
523         prev_poc = 0;
524         for(i = ps_stref_picset->i1_num_neg_pics;
525                         i < ps_stref_picset->i1_num_delta_pocs;
526                         i++)
527         {
528             UEV_PARSE("delta_poc_s1_minus1", value, ps_bitstrm);
529             poc = prev_poc + (value + 1);
530             prev_poc = poc;
531             ps_stref_picset->ai2_delta_poc[i] = poc;
532 
533             BITS_PARSE("used_by_curr_pic_s1_flag", value, ps_bitstrm, 1);
534             ps_stref_picset->ai1_used[i] = value;
535 
536         }
537 
538     }
539 
540     return ret;
541 }
542 
543 
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)544 static WORD32 ihevcd_parse_sub_layer_hrd_parameters(bitstrm_t *ps_bitstrm,
545                                                     sub_lyr_hrd_params_t *ps_sub_layer_hrd_params,
546                                                     WORD32 cpb_cnt,
547                                                     WORD32 sub_pic_cpb_params_present_flag)
548 {
549     WORD32 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
550     WORD32 i;
551 
552     for(i = 0; i <= cpb_cnt; i++)
553     {
554         UEV_PARSE("bit_rate_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_bit_rate_value_minus1[i], ps_bitstrm);
555         UEV_PARSE("cpb_size_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_cpb_size_value_minus1[i], ps_bitstrm);
556 
557         if(sub_pic_cpb_params_present_flag)
558         {
559             UEV_PARSE("cpb_size_du_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_cpb_size_du_value_minus1[i], ps_bitstrm);
560             UEV_PARSE("bit_rate_du_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_bit_rate_du_value_minus1[i], ps_bitstrm);
561         }
562         BITS_PARSE("cbr_flag[ i ]", ps_sub_layer_hrd_params->au1_cbr_flag[i], ps_bitstrm, 1);
563     }
564 
565     return ret;
566 }
567 
568 
ihevcd_parse_hrd_parameters(bitstrm_t * ps_bitstrm,hrd_params_t * ps_hrd,WORD32 common_info_present_flag,WORD32 max_num_sub_layers_minus1)569 static WORD32 ihevcd_parse_hrd_parameters(bitstrm_t *ps_bitstrm,
570                                           hrd_params_t *ps_hrd,
571                                           WORD32 common_info_present_flag,
572                                           WORD32 max_num_sub_layers_minus1)
573 {
574     WORD32 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
575     WORD32 i;
576 
577     ps_hrd->u1_nal_hrd_parameters_present_flag = 0;
578     ps_hrd->u1_vcl_hrd_parameters_present_flag = 0;
579 
580     ps_hrd->u1_sub_pic_cpb_params_present_flag = 0;
581 
582     ps_hrd->u1_tick_divisor_minus2 = 0;
583     ps_hrd->u1_du_cpb_removal_delay_increment_length_minus1 = 0;
584     ps_hrd->u1_sub_pic_cpb_params_in_pic_timing_sei_flag = 0;
585     ps_hrd->u1_dpb_output_delay_du_length_minus1 = 0;
586 
587     ps_hrd->u4_bit_rate_scale = 0;
588     ps_hrd->u4_cpb_size_scale = 0;
589     ps_hrd->u4_cpb_size_du_scale = 0;
590 
591     ps_hrd->u1_initial_cpb_removal_delay_length_minus1 = 23;
592     ps_hrd->u1_au_cpb_removal_delay_length_minus1 = 23;
593     ps_hrd->u1_dpb_output_delay_length_minus1 = 23;
594 
595     if(common_info_present_flag)
596     {
597         BITS_PARSE("nal_hrd_parameters_present_flag", ps_hrd->u1_nal_hrd_parameters_present_flag, ps_bitstrm, 1);
598         BITS_PARSE("vcl_hrd_parameters_present_flag", ps_hrd->u1_vcl_hrd_parameters_present_flag, ps_bitstrm, 1);
599 
600         if(ps_hrd->u1_nal_hrd_parameters_present_flag  ||  ps_hrd->u1_vcl_hrd_parameters_present_flag)
601         {
602             BITS_PARSE("sub_pic_cpb_params_present_flag", ps_hrd->u1_sub_pic_cpb_params_present_flag, ps_bitstrm, 1);
603             if(ps_hrd->u1_sub_pic_cpb_params_present_flag)
604             {
605                 BITS_PARSE("tick_divisor_minus2", ps_hrd->u1_tick_divisor_minus2, ps_bitstrm, 8);
606                 BITS_PARSE("du_cpb_removal_delay_increment_length_minus1", ps_hrd->u1_du_cpb_removal_delay_increment_length_minus1, ps_bitstrm, 5);
607                 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);
608                 BITS_PARSE("dpb_output_delay_du_length_minus1", ps_hrd->u1_dpb_output_delay_du_length_minus1, ps_bitstrm, 5);
609             }
610 
611             BITS_PARSE("bit_rate_scale", ps_hrd->u4_bit_rate_scale, ps_bitstrm, 4);
612             BITS_PARSE("cpb_size_scale", ps_hrd->u4_cpb_size_scale, ps_bitstrm, 4);
613             if(ps_hrd->u1_sub_pic_cpb_params_present_flag)
614                 BITS_PARSE("cpb_size_du_scale", ps_hrd->u4_cpb_size_du_scale, ps_bitstrm, 4);
615 
616             BITS_PARSE("initial_cpb_removal_delay_length_minus1", ps_hrd->u1_initial_cpb_removal_delay_length_minus1, ps_bitstrm, 5);
617             BITS_PARSE("au_cpb_removal_delay_length_minus1", ps_hrd->u1_au_cpb_removal_delay_length_minus1, ps_bitstrm, 5);
618             BITS_PARSE("dpb_output_delay_length_minus1", ps_hrd->u1_dpb_output_delay_length_minus1, ps_bitstrm, 5);
619         }
620     }
621 
622 
623     for(i = 0; i <= max_num_sub_layers_minus1; i++)
624     {
625         BITS_PARSE("fixed_pic_rate_general_flag[ i ]", ps_hrd->au1_fixed_pic_rate_general_flag[i], ps_bitstrm, 1);
626 
627         ps_hrd->au1_fixed_pic_rate_within_cvs_flag[i] = 1;
628         ps_hrd->au1_elemental_duration_in_tc_minus1[i] = 0;
629         ps_hrd->au1_low_delay_hrd_flag[i] = 0;
630         ps_hrd->au1_cpb_cnt_minus1[i] = 0;
631 
632         if(!ps_hrd->au1_fixed_pic_rate_general_flag[i])
633             BITS_PARSE("fixed_pic_rate_within_cvs_flag[ i ]", ps_hrd->au1_fixed_pic_rate_within_cvs_flag[i], ps_bitstrm, 1);
634 
635         if(ps_hrd->au1_fixed_pic_rate_within_cvs_flag[i])
636         {
637             UEV_PARSE("elemental_duration_in_tc_minus1[ i ]", ps_hrd->au1_elemental_duration_in_tc_minus1[i], ps_bitstrm);
638         }
639         else
640         {
641             BITS_PARSE("low_delay_hrd_flag[ i ]", ps_hrd->au1_low_delay_hrd_flag[i], ps_bitstrm, 1);
642         }
643 
644         if(!ps_hrd->au1_low_delay_hrd_flag[i])
645             UEV_PARSE("cpb_cnt_minus1[ i ]", ps_hrd->au1_cpb_cnt_minus1[i], ps_bitstrm);
646 
647         if(ps_hrd->au1_cpb_cnt_minus1[i] >= (MAX_CPB_CNT - 1))
648             return IHEVCD_INVALID_PARAMETER;
649 
650         if(ps_hrd->u1_nal_hrd_parameters_present_flag)
651             ihevcd_parse_sub_layer_hrd_parameters(ps_bitstrm,
652                                                   &ps_hrd->as_sub_layer_hrd_params[i],
653                                                   ps_hrd->au1_cpb_cnt_minus1[i],
654                                                   ps_hrd->u1_sub_pic_cpb_params_present_flag);
655 
656         if(ps_hrd->u1_vcl_hrd_parameters_present_flag)
657             ihevcd_parse_sub_layer_hrd_parameters(ps_bitstrm,
658                                                   &ps_hrd->as_sub_layer_hrd_params[i],
659                                                   ps_hrd->au1_cpb_cnt_minus1[i],
660                                                   ps_hrd->u1_sub_pic_cpb_params_present_flag);
661     }
662 
663     return ret;
664 }
665 
666 
ihevcd_parse_vui_parameters(bitstrm_t * ps_bitstrm,vui_t * ps_vui,WORD32 sps_max_sub_layers_minus1)667 static WORD32 ihevcd_parse_vui_parameters(bitstrm_t *ps_bitstrm,
668                                           vui_t *ps_vui,
669                                           WORD32 sps_max_sub_layers_minus1)
670 {
671     WORD32 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
672 
673     BITS_PARSE("aspect_ratio_info_present_flag", ps_vui->u1_aspect_ratio_info_present_flag, ps_bitstrm, 1);
674 
675     ps_vui->u1_aspect_ratio_idc = SAR_UNUSED;
676     ps_vui->u2_sar_width = 0;
677     ps_vui->u2_sar_height = 0;
678     if(ps_vui->u1_aspect_ratio_info_present_flag)
679     {
680         BITS_PARSE("aspect_ratio_idc", ps_vui->u1_aspect_ratio_idc, ps_bitstrm, 8);
681         if(ps_vui->u1_aspect_ratio_idc  ==  EXTENDED_SAR)
682         {
683             BITS_PARSE("sar_width", ps_vui->u2_sar_width, ps_bitstrm, 16);
684             BITS_PARSE("sar_height", ps_vui->u2_sar_height, ps_bitstrm, 16);
685         }
686     }
687 
688     BITS_PARSE("overscan_info_present_flag", ps_vui->u1_overscan_info_present_flag, ps_bitstrm, 1);
689     ps_vui->u1_overscan_appropriate_flag = 0;
690     if(ps_vui->u1_overscan_info_present_flag)
691         BITS_PARSE("overscan_appropriate_flag", ps_vui->u1_overscan_appropriate_flag, ps_bitstrm, 1);
692 
693     BITS_PARSE("video_signal_type_present_flag", ps_vui->u1_video_signal_type_present_flag, ps_bitstrm, 1);
694     ps_vui->u1_video_format = VID_FMT_UNSPECIFIED;
695     ps_vui->u1_video_full_range_flag = 0;
696     ps_vui->u1_colour_description_present_flag = 0;
697     ps_vui->u1_colour_primaries = 2;
698     ps_vui->u1_transfer_characteristics = 2;
699     ps_vui->u1_matrix_coefficients = 2;
700 
701     if(ps_vui->u1_video_signal_type_present_flag)
702     {
703         BITS_PARSE("video_format", ps_vui->u1_video_format, ps_bitstrm, 3);
704         BITS_PARSE("video_full_range_flag", ps_vui->u1_video_full_range_flag, ps_bitstrm, 1);
705         BITS_PARSE("colour_description_present_flag", ps_vui->u1_colour_description_present_flag, ps_bitstrm, 1);
706         if(ps_vui->u1_colour_description_present_flag)
707         {
708             BITS_PARSE("colour_primaries", ps_vui->u1_colour_primaries, ps_bitstrm, 8);
709             BITS_PARSE("transfer_characteristics", ps_vui->u1_transfer_characteristics, ps_bitstrm, 8);
710             BITS_PARSE("matrix_coeffs", ps_vui->u1_matrix_coefficients, ps_bitstrm, 8);
711         }
712     }
713 
714     BITS_PARSE("chroma_loc_info_present_flag", ps_vui->u1_chroma_loc_info_present_flag, ps_bitstrm, 1);
715     ps_vui->u1_chroma_sample_loc_type_top_field = 0;
716     ps_vui->u1_chroma_sample_loc_type_bottom_field = 0;
717     if(ps_vui->u1_chroma_loc_info_present_flag)
718     {
719         UEV_PARSE("chroma_sample_loc_type_top_field", ps_vui->u1_chroma_sample_loc_type_top_field, ps_bitstrm);
720         UEV_PARSE("chroma_sample_loc_type_bottom_field", ps_vui->u1_chroma_sample_loc_type_bottom_field, ps_bitstrm);
721     }
722 
723     BITS_PARSE("neutral_chroma_indication_flag", ps_vui->u1_neutral_chroma_indication_flag, ps_bitstrm, 1);
724     BITS_PARSE("field_seq_flag", ps_vui->u1_field_seq_flag, ps_bitstrm, 1);
725     BITS_PARSE("frame_field_info_present_flag", ps_vui->u1_frame_field_info_present_flag, ps_bitstrm, 1);
726     BITS_PARSE("default_display_window_flag", ps_vui->u1_default_display_window_flag, ps_bitstrm, 1);
727     ps_vui->u4_def_disp_win_left_offset = 0;
728     ps_vui->u4_def_disp_win_right_offset = 0;
729     ps_vui->u4_def_disp_win_top_offset = 0;
730     ps_vui->u4_def_disp_win_bottom_offset = 0;
731     if(ps_vui->u1_default_display_window_flag)
732     {
733         UEV_PARSE("def_disp_win_left_offset", ps_vui->u4_def_disp_win_left_offset, ps_bitstrm);
734         UEV_PARSE("def_disp_win_right_offset", ps_vui->u4_def_disp_win_right_offset, ps_bitstrm);
735         UEV_PARSE("def_disp_win_top_offset", ps_vui->u4_def_disp_win_top_offset, ps_bitstrm);
736         UEV_PARSE("def_disp_win_bottom_offset", ps_vui->u4_def_disp_win_bottom_offset, ps_bitstrm);
737     }
738 
739     BITS_PARSE("vui_timing_info_present_flag", ps_vui->u1_vui_timing_info_present_flag, ps_bitstrm, 1);
740     if(ps_vui->u1_vui_timing_info_present_flag)
741     {
742         BITS_PARSE("vui_num_units_in_tick", ps_vui->u4_vui_num_units_in_tick, ps_bitstrm, 32);
743         BITS_PARSE("vui_time_scale", ps_vui->u4_vui_time_scale, ps_bitstrm, 32);
744         BITS_PARSE("vui_poc_proportional_to_timing_flag", ps_vui->u1_poc_proportional_to_timing_flag, ps_bitstrm, 1);
745         if(ps_vui->u1_poc_proportional_to_timing_flag)
746             UEV_PARSE("vui_num_ticks_poc_diff_one_minus1", ps_vui->u1_num_ticks_poc_diff_one_minus1, ps_bitstrm);
747 
748         BITS_PARSE("vui_hrd_parameters_present_flag", ps_vui->u1_vui_hrd_parameters_present_flag, ps_bitstrm, 1);
749         if(ps_vui->u1_vui_hrd_parameters_present_flag)
750         {
751             ret = ihevcd_parse_hrd_parameters(ps_bitstrm, &ps_vui->s_vui_hrd_parameters, 1, sps_max_sub_layers_minus1);
752             RETURN_IF((ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS), ret);
753         }
754     }
755 
756     BITS_PARSE("bitstream_restriction_flag", ps_vui->u1_bitstream_restriction_flag, ps_bitstrm, 1);
757     ps_vui->u1_tiles_fixed_structure_flag = 0;
758     ps_vui->u1_motion_vectors_over_pic_boundaries_flag = 1;
759     ps_vui->u1_restricted_ref_pic_lists_flag = 0;
760     ps_vui->u4_min_spatial_segmentation_idc = 0;
761     ps_vui->u1_max_bytes_per_pic_denom = 2;
762     ps_vui->u1_max_bits_per_mincu_denom = 1;
763     ps_vui->u1_log2_max_mv_length_horizontal = 15;
764     ps_vui->u1_log2_max_mv_length_vertical = 15;
765     if(ps_vui->u1_bitstream_restriction_flag)
766     {
767         BITS_PARSE("tiles_fixed_structure_flag", ps_vui->u1_tiles_fixed_structure_flag, ps_bitstrm, 1);
768         BITS_PARSE("motion_vectors_over_pic_boundaries_flag", ps_vui->u1_motion_vectors_over_pic_boundaries_flag, ps_bitstrm, 1);
769         BITS_PARSE("restricted_ref_pic_lists_flag", ps_vui->u1_restricted_ref_pic_lists_flag, ps_bitstrm, 1);
770 
771         UEV_PARSE("min_spatial_segmentation_idc", ps_vui->u4_min_spatial_segmentation_idc, ps_bitstrm);
772         UEV_PARSE("max_bytes_per_pic_denom", ps_vui->u1_max_bytes_per_pic_denom, ps_bitstrm);
773         UEV_PARSE("max_bits_per_min_cu_denom", ps_vui->u1_max_bits_per_mincu_denom, ps_bitstrm);
774         UEV_PARSE("log2_max_mv_length_horizontal", ps_vui->u1_log2_max_mv_length_horizontal, ps_bitstrm);
775         UEV_PARSE("log2_max_mv_length_vertical", ps_vui->u1_log2_max_mv_length_vertical, ps_bitstrm);
776     }
777 
778     return ret;
779 }
780 
781 /**
782 *******************************************************************************
783 *
784 * @brief
785 *  Parses profile tier and level info for either general layer of sub_layer
786 *
787 * @par   Description
788 *  Parses profile tier and level info for either general layer of sub_layer
789 * as per section 7.3.3
790 *
791 * Since the same function is called for parsing general_profile and
792 * sub_layer_profile etc, variables do not specify whether the syntax is
793 * for general or sub_layer. Similarly trace functions also do not differentiate
794 *
795 * @param[in] ps_bitstrm
796 *  Pointer to bitstream structure
797 *
798 * @param[out] ps_ptl
799 *  Pointer to profile, tier level structure
800 *
801 * @returns Error code from IHEVCD_ERROR_T
802 *
803 * @remarks
804 *
805 *******************************************************************************
806 */
807 
ihevcd_parse_profile_tier_level_layer(bitstrm_t * ps_bitstrm,profile_tier_lvl_t * ps_ptl)808 static IHEVCD_ERROR_T ihevcd_parse_profile_tier_level_layer(bitstrm_t *ps_bitstrm,
809                                                             profile_tier_lvl_t *ps_ptl)
810 {
811     WORD32 value;
812     WORD32 i;
813     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
814 
815     BITS_PARSE("XXX_profile_space[]", value, ps_bitstrm, 2);
816     ps_ptl->i1_profile_space = value;
817 
818     BITS_PARSE("XXX_tier_flag[]", value, ps_bitstrm, 1);
819     ps_ptl->i1_tier_flag = value;
820 
821     BITS_PARSE("XXX_profile_idc[]", value, ps_bitstrm, 5);
822     ps_ptl->i1_profile_idc = value;
823 
824     for(i = 0; i < MAX_PROFILE_COMPATBLTY; i++)
825     {
826         BITS_PARSE("XXX_profile_compatibility_flag[][j]", value, ps_bitstrm, 1);
827         ps_ptl->ai1_profile_compatibility_flag[i] = value;
828     }
829 
830     BITS_PARSE("general_progressive_source_flag", value, ps_bitstrm, 1);
831     ps_ptl->i1_general_progressive_source_flag = value;
832 
833     BITS_PARSE("general_interlaced_source_flag", value, ps_bitstrm, 1);
834     ps_ptl->i1_general_progressive_source_flag = value;
835 
836     BITS_PARSE("general_non_packed_constraint_flag", value, ps_bitstrm, 1);
837     ps_ptl->i1_general_progressive_source_flag = value;
838 
839     BITS_PARSE("general_frame_only_constraint_flag", value, ps_bitstrm, 1);
840     ps_ptl->i1_general_progressive_source_flag = value;
841 
842     BITS_PARSE("XXX_reserved_zero_44bits[0..15]", value, ps_bitstrm, 16);
843 
844     BITS_PARSE("XXX_reserved_zero_44bits[16..31]", value, ps_bitstrm, 16);
845 
846     BITS_PARSE("XXX_reserved_zero_44bits[32..43]", value, ps_bitstrm, 12);
847     return ret;
848 }
849 
850 
851 /**
852 *******************************************************************************
853 *
854 * @brief
855 *  Parses profile tier and level info
856 *
857 * @par   Description
858 *  Parses profile tier and level info as per section 7.3.3
859 * Called during VPS and SPS parsing
860 * calls ihevcd_parse_profile_tier_level() for general layer and each sub_layers
861 *
862 * @param[in] ps_bitstrm
863 *  Pointer to bitstream structure
864 *
865 * @param[out] ps_ptl
866 *  Pointer to structure that contains profile, tier level for each layers
867 *
868 * @param[in] profile_present
869 *  Flag to indicate if profile data is present
870 *
871 * @param[in] max_num_sub_layers
872 *  Number of sub layers present
873 *
874 * @returns Error code from IHEVCD_ERROR_T
875 *
876 * @remarks
877 *
878 *******************************************************************************
879 */
880 
ihevcd_profile_tier_level(bitstrm_t * ps_bitstrm,profile_tier_lvl_info_t * ps_ptl,WORD32 profile_present,WORD32 max_num_sub_layers)881 static IHEVCD_ERROR_T ihevcd_profile_tier_level(bitstrm_t *ps_bitstrm,
882                                                 profile_tier_lvl_info_t *ps_ptl,
883                                                 WORD32 profile_present,
884                                                 WORD32 max_num_sub_layers)
885 {
886     WORD32 value;
887     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
888     WORD32 i;
889 
890     if(profile_present)
891     {
892         ret = ihevcd_parse_profile_tier_level_layer(ps_bitstrm, &ps_ptl->s_ptl_gen);
893     }
894 
895     BITS_PARSE("general_level_idc", value, ps_bitstrm, 8);
896     ps_ptl->s_ptl_gen.u1_level_idc = value;
897 
898 
899     for(i = 0; i < max_num_sub_layers; i++)
900     {
901         BITS_PARSE("sub_layer_profile_present_flag[i]", value, ps_bitstrm, 1);
902         ps_ptl->ai1_sub_layer_profile_present_flag[i] = value;
903 
904         BITS_PARSE("sub_layer_level_present_flag[i]", value, ps_bitstrm, 1);
905         ps_ptl->ai1_sub_layer_level_present_flag[i] = value;
906     }
907 
908     if(max_num_sub_layers > 0)
909     {
910         for(i = max_num_sub_layers; i < 8; i++)
911         {
912             BITS_PARSE("reserved_zero_2bits", value, ps_bitstrm, 2);
913         }
914     }
915 
916     for(i = 0; i < max_num_sub_layers; i++)
917     {
918         if(ps_ptl->ai1_sub_layer_profile_present_flag[i])
919         {
920             ret = ihevcd_parse_profile_tier_level_layer(ps_bitstrm,
921                                                         &ps_ptl->as_ptl_sub[i]);
922         }
923         if(ps_ptl->ai1_sub_layer_level_present_flag[i])
924         {
925             BITS_PARSE("sub_layer_level_idc[i]", value, ps_bitstrm, 8);
926             ps_ptl->as_ptl_sub[i].u1_level_idc = value;
927 
928         }
929     }
930 
931 
932 
933     return ret;
934 }
935 
936 /**
937 *******************************************************************************
938 *
939 * @brief
940 *  Parses Scaling List Data syntax
941 *
942 * @par Description:
943 *  Parses Scaling List Data syntax as per Section: 7.3.6
944 *
945 * @param[in] ps_codec
946 *  Pointer to codec context
947 *
948 * @returns  Error code from IHEVCD_ERROR_T
949 *
950 * @remarks
951 *
952 *
953 *******************************************************************************
954 */
ihevcd_scaling_list_data(codec_t * ps_codec,WORD16 * pi2_scaling_mat)955 IHEVCD_ERROR_T  ihevcd_scaling_list_data(codec_t *ps_codec, WORD16 *pi2_scaling_mat)
956 {
957     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
958     WORD32 size_id;
959     WORD32 matrix_id;
960     WORD32 value, dc_value = 0;
961     WORD32 next_coef;
962     WORD32 coef_num;
963     WORD32 i, j, offset;
964     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
965     WORD16 *pi2_scaling_mat_offset;
966     WORD32 scaling_mat_offset[] = { 0, 16, 32, 48, 64, 80, 96, 160, 224, 288, 352, 416, 480, 736, 992, 1248, 1504, 1760, 2016, 3040 };
967     UWORD8 *scan_table;
968 
969     for(size_id = 0; size_id < 4; size_id++)
970     {
971         for(matrix_id = 0; matrix_id < ((size_id == 3) ? 2 : 6); matrix_id++)
972         {
973             WORD32 scaling_list_pred_mode_flag;
974             WORD32 scaling_list_delta_coef;
975             BITS_PARSE("scaling_list_pred_mode_flag", scaling_list_pred_mode_flag, ps_bitstrm, 1);
976 
977             offset = size_id * 6 + matrix_id;
978             pi2_scaling_mat_offset = pi2_scaling_mat + scaling_mat_offset[offset];
979 
980             if(!scaling_list_pred_mode_flag)
981             {
982                 WORD32 num_elements;
983                 UEV_PARSE("scaling_list_pred_matrix_id_delta", value,
984                           ps_bitstrm);
985                 value = CLIP3(value, 0, matrix_id);
986 
987                 num_elements = (1 << (4 + (size_id << 1)));
988                 if(0 != value)
989                     memcpy(pi2_scaling_mat_offset, pi2_scaling_mat_offset - value * num_elements, num_elements * sizeof(WORD16));
990             }
991             else
992             {
993                 next_coef = 8;
994                 coef_num = MIN(64, (1 << (4 + (size_id << 1))));
995 
996                 if(size_id > 1)
997                 {
998                     SEV_PARSE("scaling_list_dc_coef_minus8", value,
999                               ps_bitstrm);
1000 
1001                     next_coef = value + 8;
1002                     dc_value = next_coef;
1003                 }
1004                 if(size_id < 2)
1005                 {
1006                     scan_table = (UWORD8 *)gapv_ihevc_invscan[size_id + 1];
1007 
1008                     for(i = 0; i < coef_num; i++)
1009                     {
1010                         SEV_PARSE("scaling_list_delta_coef",
1011                                   scaling_list_delta_coef, ps_bitstrm);
1012                         next_coef = (next_coef + scaling_list_delta_coef + 256)
1013                                         % 256;
1014                         pi2_scaling_mat_offset[scan_table[i]] = next_coef;
1015                     }
1016                 }
1017                 else if(size_id == 2)
1018                 {
1019                     scan_table = (UWORD8 *)gapv_ihevc_invscan[2];
1020 
1021                     for(i = 0; i < coef_num; i++)
1022                     {
1023                         SEV_PARSE("scaling_list_delta_coef",
1024                                   scaling_list_delta_coef, ps_bitstrm);
1025                         next_coef = (next_coef + scaling_list_delta_coef + 256)
1026                                         % 256;
1027 
1028                         offset = scan_table[i];
1029                         offset = (offset >> 3) * 16 * 2 + (offset & 0x7) * 2;
1030                         pi2_scaling_mat_offset[offset] = next_coef;
1031                         pi2_scaling_mat_offset[offset + 1] = next_coef;
1032                         pi2_scaling_mat_offset[offset + 16] = next_coef;
1033                         pi2_scaling_mat_offset[offset + 16 + 1] = next_coef;
1034                     }
1035                     pi2_scaling_mat_offset[0] = dc_value;
1036                 }
1037                 else
1038                 {
1039                     scan_table = (UWORD8 *)gapv_ihevc_invscan[2];
1040 
1041                     for(i = 0; i < coef_num; i++)
1042                     {
1043                         SEV_PARSE("scaling_list_delta_coef",
1044                                   scaling_list_delta_coef, ps_bitstrm);
1045                         next_coef = (next_coef + scaling_list_delta_coef + 256)
1046                                         % 256;
1047 
1048                         offset = scan_table[i];
1049                         offset = (offset >> 3) * 32 * 4 + (offset & 0x7) * 4;
1050 
1051                         for(j = 0; j < 4; j++)
1052                         {
1053                             pi2_scaling_mat_offset[offset + j * 32] = next_coef;
1054                             pi2_scaling_mat_offset[offset + 1 + j * 32] = next_coef;
1055                             pi2_scaling_mat_offset[offset + 2 + j * 32] = next_coef;
1056                             pi2_scaling_mat_offset[offset + 3 + j * 32] = next_coef;
1057                         }
1058                         pi2_scaling_mat_offset[0] = dc_value;
1059                     }
1060                 }
1061             }
1062         }
1063     }
1064 
1065     return ret;
1066 }
1067 
1068 /**
1069 *******************************************************************************
1070 *
1071 * @brief
1072 *  Parses VPS (Video Parameter Set)
1073 *
1074 * @par Description:
1075 *  Parse Video Parameter Set as per Section 7.3.2.1
1076 * update vps structure corresponding to vps ID
1077 * Till parsing VPS id, the elements are stored in local variables and are copied
1078 * later
1079 *
1080 * @param[in] ps_codec
1081 *  Pointer to codec context.
1082 *
1083 * @returns Error code from IHEVCD_ERROR_T
1084 *
1085 * @remarks
1086 *
1087 *
1088 *******************************************************************************
1089 */
ihevcd_parse_vps(codec_t * ps_codec)1090 IHEVCD_ERROR_T ihevcd_parse_vps(codec_t *ps_codec)
1091 {
1092     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
1093     WORD32 i;
1094     WORD32 value;
1095     WORD32 vps_id;
1096     vps_t *ps_vps;
1097     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
1098     BITS_PARSE("vps_video_parameter_set_id", value, ps_bitstrm, 4);
1099     vps_id = value;
1100 
1101     if(vps_id >= MAX_VPS_CNT)
1102     {
1103         ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_VPS_ID;
1104         return IHEVCD_UNSUPPORTED_VPS_ID;
1105     }
1106 
1107 
1108     ps_vps = (ps_codec->s_parse.ps_vps_base + vps_id);
1109 
1110     ps_vps->i1_vps_id = vps_id;
1111 
1112     BITS_PARSE("vps_reserved_three_2bits", value, ps_bitstrm, 2);
1113     ASSERT(value == 3);
1114 
1115     BITS_PARSE("vps_max_layers_minus1", value, ps_bitstrm, 6);
1116     //ps_vps->i1_vps_max_layers = value + 1;
1117 
1118 
1119 
1120     BITS_PARSE("vps_max_sub_layers_minus1", value, ps_bitstrm, 3);
1121     ps_vps->i1_vps_max_sub_layers = value + 1;
1122 
1123     ASSERT(ps_vps->i1_vps_max_sub_layers < VPS_MAX_SUB_LAYERS);
1124 
1125     BITS_PARSE("vps_temporal_id_nesting_flag", value, ps_bitstrm, 1);
1126     ps_vps->i1_vps_temporal_id_nesting_flag = value;
1127 
1128     BITS_PARSE("vps_reserved_ffff_16bits", value, ps_bitstrm, 16);
1129     ASSERT(value == 0xFFFF);
1130     // profile_and_level( 1, vps_max_sub_layers_minus1 )
1131     ret = ihevcd_profile_tier_level(ps_bitstrm, &(ps_vps->s_ptl),
1132                                     1, (ps_vps->i1_vps_max_sub_layers - 1));
1133 
1134     BITS_PARSE("vps_sub_layer_ordering_info_present_flag", value, ps_bitstrm, 1);
1135     ps_vps->i1_sub_layer_ordering_info_present_flag = value;
1136     i = (ps_vps->i1_sub_layer_ordering_info_present_flag ?
1137                     0 : (ps_vps->i1_vps_max_sub_layers - 1));
1138     for(; i < ps_vps->i1_vps_max_sub_layers; i++)
1139     {
1140         UEV_PARSE("vps_max_dec_pic_buffering[i]", value, ps_bitstrm);
1141         ps_vps->ai1_vps_max_dec_pic_buffering[i] = value;
1142 
1143         /* vps_num_reorder_pics (no max) used in print in order to match with HM */
1144         UEV_PARSE("vps_num_reorder_pics[i]", value, ps_bitstrm);
1145         ps_vps->ai1_vps_max_num_reorder_pics[i] = value;
1146 
1147         UEV_PARSE("vps_max_latency_increase[i]", value, ps_bitstrm);
1148         ps_vps->ai1_vps_max_latency_increase[i] = value;
1149     }
1150 
1151 
1152 
1153     BITS_PARSE("vps_max_layer_id", value, ps_bitstrm, 6);
1154     //ps_vps->i1_vps_max_layer_id  = value;
1155 
1156     UEV_PARSE("vps_num_layer_sets_minus1", value, ps_bitstrm);
1157     //ps_vps->i1_vps_num_layer_sets  = value + 1;
1158 
1159     BITS_PARSE("vps_timing_info_present_flag", value, ps_bitstrm, 1);
1160     //ps_vps->i1_vps_timing_info_present_flag  = value;
1161 
1162 
1163 
1164     return ret;
1165 }
1166 
1167 /**
1168 *******************************************************************************
1169 *
1170 * @brief
1171 *  Parses SPS (Sequence Parameter Set)
1172 * sequence_parameter_set_rbsp()
1173 *
1174 * @par Description:
1175 *  Parse Sequence Parameter Set as per section  Section: 7.3.2.2
1176 * The sps is written to a temporary buffer and copied later to the
1177 * appropriate location
1178 *
1179 * @param[in] ps_codec
1180 *  Pointer to codec context
1181 *
1182 * @returns Error code from IHEVCD_ERROR_T
1183 *
1184 * @remarks
1185 *
1186 *
1187 *******************************************************************************
1188 */
ihevcd_parse_sps(codec_t * ps_codec)1189 IHEVCD_ERROR_T ihevcd_parse_sps(codec_t *ps_codec)
1190 {
1191     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
1192     WORD32 value;
1193 
1194     WORD32 i;
1195     WORD32 vps_id;
1196     WORD32 sps_max_sub_layers;
1197     WORD32 sps_id;
1198     WORD32 sps_temporal_id_nesting_flag;
1199     sps_t *ps_sps;
1200     profile_tier_lvl_info_t s_ptl;
1201     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
1202     WORD32 ctb_log2_size_y = 0;
1203 
1204 
1205     BITS_PARSE("video_parameter_set_id", value, ps_bitstrm, 4);
1206     vps_id = value;
1207     vps_id = CLIP3(vps_id, 0, MAX_VPS_CNT - 1);
1208 
1209     BITS_PARSE("sps_max_sub_layers_minus1", value, ps_bitstrm, 3);
1210     sps_max_sub_layers = value + 1;
1211     sps_max_sub_layers = CLIP3(sps_max_sub_layers, 1, 7);
1212 
1213     BITS_PARSE("sps_temporal_id_nesting_flag", value, ps_bitstrm, 1);
1214     sps_temporal_id_nesting_flag = value;
1215 
1216     //profile_and_level( 1, sps_max_sub_layers_minus1 )
1217     ret = ihevcd_profile_tier_level(ps_bitstrm, &(s_ptl), 1,
1218                                     (sps_max_sub_layers - 1));
1219 
1220     UEV_PARSE("seq_parameter_set_id", value, ps_bitstrm);
1221     sps_id = value;
1222 
1223     if((sps_id >= MAX_SPS_CNT) || (sps_id < 0))
1224     {
1225         if(ps_codec->i4_sps_done)
1226             return IHEVCD_UNSUPPORTED_SPS_ID;
1227         else
1228             sps_id = 0;
1229     }
1230 
1231 
1232     ps_sps = (ps_codec->s_parse.ps_sps_base + MAX_SPS_CNT - 1);
1233     /* Reset SPS to zero */
1234     {
1235         WORD16 *pi2_scaling_mat = ps_sps->pi2_scaling_mat;
1236         memset(ps_sps, 0, sizeof(sps_t));
1237         ps_sps->pi2_scaling_mat = pi2_scaling_mat;
1238     }
1239     ps_sps->i1_sps_id = sps_id;
1240     ps_sps->i1_vps_id = vps_id;
1241     ps_sps->i1_sps_max_sub_layers = sps_max_sub_layers;
1242     ps_sps->i1_sps_temporal_id_nesting_flag = sps_temporal_id_nesting_flag;
1243     /* This is used only during initialization to get reorder count etc */
1244     ps_codec->i4_sps_id = sps_id;
1245     memcpy(&ps_sps->s_ptl, &s_ptl, sizeof(profile_tier_lvl_info_t));
1246 
1247     UEV_PARSE("chroma_format_idc", value, ps_bitstrm);
1248     ps_sps->i1_chroma_format_idc = value;
1249 
1250     if(ps_sps->i1_chroma_format_idc != CHROMA_FMT_IDC_YUV420)
1251     {
1252         ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
1253         return (IHEVCD_ERROR_T)IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
1254     }
1255 
1256     if(CHROMA_FMT_IDC_YUV444_PLANES == ps_sps->i1_chroma_format_idc)
1257     {
1258         BITS_PARSE("separate_colour_plane_flag", value, ps_bitstrm, 1);
1259         ps_sps->i1_separate_colour_plane_flag = value;
1260     }
1261     else
1262     {
1263         ps_sps->i1_separate_colour_plane_flag = 0;
1264     }
1265 
1266     UEV_PARSE("pic_width_in_luma_samples", value, ps_bitstrm);
1267     ps_sps->i2_pic_width_in_luma_samples = value;
1268 
1269     UEV_PARSE("pic_height_in_luma_samples", value, ps_bitstrm);
1270     ps_sps->i2_pic_height_in_luma_samples = value;
1271 
1272     if((0 >= ps_sps->i2_pic_width_in_luma_samples) || (0 >= ps_sps->i2_pic_height_in_luma_samples))
1273         return IHEVCD_INVALID_PARAMETER;
1274 
1275     /* i2_pic_width_in_luma_samples and i2_pic_height_in_luma_samples
1276        should be multiples of min_cb_size. Here these are aligned to 8,
1277        i.e. smallest CB size */
1278     ps_sps->i2_pic_width_in_luma_samples = ALIGN8(ps_sps->i2_pic_width_in_luma_samples);
1279     ps_sps->i2_pic_height_in_luma_samples = ALIGN8(ps_sps->i2_pic_height_in_luma_samples);
1280 
1281     BITS_PARSE("pic_cropping_flag", value, ps_bitstrm, 1);
1282     ps_sps->i1_pic_cropping_flag = value;
1283 
1284     if(ps_sps->i1_pic_cropping_flag)
1285     {
1286 
1287         UEV_PARSE("pic_crop_left_offset", value, ps_bitstrm);
1288         ps_sps->i2_pic_crop_left_offset = value;
1289 
1290         UEV_PARSE("pic_crop_right_offset", value, ps_bitstrm);
1291         ps_sps->i2_pic_crop_right_offset = value;
1292 
1293         UEV_PARSE("pic_crop_top_offset", value, ps_bitstrm);
1294         ps_sps->i2_pic_crop_top_offset = value;
1295 
1296         UEV_PARSE("pic_crop_bottom_offset", value, ps_bitstrm);
1297         ps_sps->i2_pic_crop_bottom_offset = value;
1298     }
1299     else
1300     {
1301         ps_sps->i2_pic_crop_left_offset = 0;
1302         ps_sps->i2_pic_crop_right_offset = 0;
1303         ps_sps->i2_pic_crop_top_offset = 0;
1304         ps_sps->i2_pic_crop_bottom_offset = 0;
1305     }
1306 
1307 
1308     UEV_PARSE("bit_depth_luma_minus8", value, ps_bitstrm);
1309     if(0 != value)
1310         return IHEVCD_UNSUPPORTED_BIT_DEPTH;
1311 
1312     UEV_PARSE("bit_depth_chroma_minus8", value, ps_bitstrm);
1313     if(0 != value)
1314         return IHEVCD_UNSUPPORTED_BIT_DEPTH;
1315 
1316     UEV_PARSE("log2_max_pic_order_cnt_lsb_minus4", value, ps_bitstrm);
1317     ps_sps->i1_log2_max_pic_order_cnt_lsb = value + 4;
1318 
1319     BITS_PARSE("sps_sub_layer_ordering_info_present_flag", value, ps_bitstrm, 1);
1320     ps_sps->i1_sps_sub_layer_ordering_info_present_flag = value;
1321 
1322 
1323     i = (ps_sps->i1_sps_sub_layer_ordering_info_present_flag ? 0 : (ps_sps->i1_sps_max_sub_layers - 1));
1324     for(; i < ps_sps->i1_sps_max_sub_layers; i++)
1325     {
1326         UEV_PARSE("max_dec_pic_buffering", value, ps_bitstrm);
1327         ps_sps->ai1_sps_max_dec_pic_buffering[i] = value + 1;
1328 
1329         if(ps_sps->ai1_sps_max_dec_pic_buffering[i] > MAX_DPB_SIZE)
1330         {
1331             return IHEVCD_INVALID_PARAMETER;
1332         }
1333 
1334         UEV_PARSE("num_reorder_pics", value, ps_bitstrm);
1335         ps_sps->ai1_sps_max_num_reorder_pics[i] = value;
1336 
1337         if(ps_sps->ai1_sps_max_num_reorder_pics[i] > ps_sps->ai1_sps_max_dec_pic_buffering[i])
1338         {
1339             return IHEVCD_INVALID_PARAMETER;
1340         }
1341 
1342         UEV_PARSE("max_latency_increase", value, ps_bitstrm);
1343         ps_sps->ai1_sps_max_latency_increase[i] = value;
1344     }
1345 
1346     /* Check if sps_max_dec_pic_buffering or sps_max_num_reorder_pics
1347        has changed */
1348     if(0 != ps_codec->u4_allocate_dynamic_done)
1349     {
1350         sps_t *ps_sps_old = ps_codec->s_parse.ps_sps;
1351         if(ps_sps_old->ai1_sps_max_dec_pic_buffering[ps_sps_old->i1_sps_max_sub_layers - 1] !=
1352                     ps_sps->ai1_sps_max_dec_pic_buffering[ps_sps->i1_sps_max_sub_layers - 1])
1353         {
1354             if(0 == ps_codec->i4_first_pic_done)
1355             {
1356                 return IHEVCD_INVALID_PARAMETER;
1357             }
1358             ps_codec->i4_reset_flag = 1;
1359             return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
1360         }
1361 
1362         if(ps_sps_old->ai1_sps_max_num_reorder_pics[ps_sps_old->i1_sps_max_sub_layers - 1] !=
1363                     ps_sps->ai1_sps_max_num_reorder_pics[ps_sps->i1_sps_max_sub_layers - 1])
1364         {
1365             if(0 == ps_codec->i4_first_pic_done)
1366             {
1367                 return IHEVCD_INVALID_PARAMETER;
1368             }
1369             ps_codec->i4_reset_flag = 1;
1370             return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
1371         }
1372     }
1373 
1374     UEV_PARSE("log2_min_coding_block_size_minus3", value, ps_bitstrm);
1375     ps_sps->i1_log2_min_coding_block_size = value + 3;
1376 
1377     UEV_PARSE("log2_diff_max_min_coding_block_size", value, ps_bitstrm);
1378     ps_sps->i1_log2_diff_max_min_coding_block_size = value;
1379 
1380     ctb_log2_size_y = ps_sps->i1_log2_min_coding_block_size + ps_sps->i1_log2_diff_max_min_coding_block_size;
1381 
1382     UEV_PARSE("log2_min_transform_block_size_minus2", value, ps_bitstrm);
1383     ps_sps->i1_log2_min_transform_block_size = value + 2;
1384 
1385     UEV_PARSE("log2_diff_max_min_transform_block_size", value, ps_bitstrm);
1386     ps_sps->i1_log2_diff_max_min_transform_block_size = value;
1387 
1388     ps_sps->i1_log2_max_transform_block_size = ps_sps->i1_log2_min_transform_block_size +
1389                     ps_sps->i1_log2_diff_max_min_transform_block_size;
1390 
1391     if ((ps_sps->i1_log2_max_transform_block_size < 0) ||
1392                     (ps_sps->i1_log2_max_transform_block_size > MIN(ctb_log2_size_y, 5)))
1393     {
1394         return IHEVCD_INVALID_PARAMETER;
1395     }
1396 
1397     ps_sps->i1_log2_ctb_size = ps_sps->i1_log2_min_coding_block_size +
1398                     ps_sps->i1_log2_diff_max_min_coding_block_size;
1399 
1400     if((ps_sps->i1_log2_min_coding_block_size < 3) ||
1401                     (ps_sps->i1_log2_min_transform_block_size < 2) ||
1402                     (ps_sps->i1_log2_diff_max_min_transform_block_size < 0) ||
1403                     (ps_sps->i1_log2_max_transform_block_size > ps_sps->i1_log2_ctb_size) ||
1404                     (ps_sps->i1_log2_ctb_size < 4) ||
1405                     (ps_sps->i1_log2_ctb_size > 6))
1406     {
1407         return IHEVCD_INVALID_PARAMETER;
1408     }
1409 
1410     ps_sps->i1_log2_min_pcm_coding_block_size = 0;
1411     ps_sps->i1_log2_diff_max_min_pcm_coding_block_size = 0;
1412 
1413     UEV_PARSE("max_transform_hierarchy_depth_inter", value, ps_bitstrm);
1414     ps_sps->i1_max_transform_hierarchy_depth_inter = value;
1415 
1416     UEV_PARSE("max_transform_hierarchy_depth_intra", value, ps_bitstrm);
1417     ps_sps->i1_max_transform_hierarchy_depth_intra = value;
1418 
1419     /* String has a d (enabled) in order to match with HM */
1420     BITS_PARSE("scaling_list_enabled_flag", value, ps_bitstrm, 1);
1421     ps_sps->i1_scaling_list_enable_flag = value;
1422 
1423     if(ps_sps->i1_scaling_list_enable_flag)
1424     {
1425         COPY_DEFAULT_SCALING_LIST(ps_sps->pi2_scaling_mat);
1426         BITS_PARSE("sps_scaling_list_data_present_flag", value, ps_bitstrm, 1);
1427         ps_sps->i1_sps_scaling_list_data_present_flag = value;
1428 
1429         if(ps_sps->i1_sps_scaling_list_data_present_flag)
1430             ihevcd_scaling_list_data(ps_codec, ps_sps->pi2_scaling_mat);
1431     }
1432     else
1433     {
1434         COPY_FLAT_SCALING_LIST(ps_sps->pi2_scaling_mat);
1435     }
1436     /* String is asymmetric_motion_partitions_enabled_flag instead of amp_enabled_flag in order to match with HM */
1437     BITS_PARSE("asymmetric_motion_partitions_enabled_flag", value, ps_bitstrm, 1);
1438     ps_sps->i1_amp_enabled_flag = value;
1439 
1440     BITS_PARSE("sample_adaptive_offset_enabled_flag", value, ps_bitstrm, 1);
1441     ps_sps->i1_sample_adaptive_offset_enabled_flag = value;
1442 
1443     BITS_PARSE("pcm_enabled_flag", value, ps_bitstrm, 1);
1444     ps_sps->i1_pcm_enabled_flag = value;
1445 
1446     if(ps_sps->i1_pcm_enabled_flag)
1447     {
1448         BITS_PARSE("pcm_sample_bit_depth_luma", value, ps_bitstrm, 4);
1449         ps_sps->i1_pcm_sample_bit_depth_luma = value + 1;
1450 
1451         BITS_PARSE("pcm_sample_bit_depth_chroma", value, ps_bitstrm, 4);
1452         ps_sps->i1_pcm_sample_bit_depth_chroma = value + 1;
1453 
1454         UEV_PARSE("log2_min_pcm_coding_block_size_minus3", value, ps_bitstrm);
1455         ps_sps->i1_log2_min_pcm_coding_block_size = value + 3;
1456 
1457         UEV_PARSE("log2_diff_max_min_pcm_coding_block_size", value, ps_bitstrm);
1458         ps_sps->i1_log2_diff_max_min_pcm_coding_block_size = value;
1459         BITS_PARSE("pcm_loop_filter_disable_flag", value, ps_bitstrm, 1);
1460         ps_sps->i1_pcm_loop_filter_disable_flag = value;
1461 
1462     }
1463     UEV_PARSE("num_short_term_ref_pic_sets", value, ps_bitstrm);
1464     ps_sps->i1_num_short_term_ref_pic_sets = value;
1465 
1466     ps_sps->i1_num_short_term_ref_pic_sets = CLIP3(ps_sps->i1_num_short_term_ref_pic_sets, 0, MAX_STREF_PICS_SPS);
1467 
1468     for(i = 0; i < ps_sps->i1_num_short_term_ref_pic_sets; i++)
1469         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]);
1470 
1471     BITS_PARSE("long_term_ref_pics_present_flag", value, ps_bitstrm, 1);
1472     ps_sps->i1_long_term_ref_pics_present_flag = value;
1473 
1474     if(ps_sps->i1_long_term_ref_pics_present_flag)
1475     {
1476         UEV_PARSE("num_long_term_ref_pics_sps", value, ps_bitstrm);
1477         ps_sps->i1_num_long_term_ref_pics_sps = value;
1478 
1479         for(i = 0; i < ps_sps->i1_num_long_term_ref_pics_sps; i++)
1480         {
1481             BITS_PARSE("lt_ref_pic_poc_lsb_sps[ i ]", value, ps_bitstrm, ps_sps->i1_log2_max_pic_order_cnt_lsb);
1482             ps_sps->ai1_lt_ref_pic_poc_lsb_sps[i] = value;
1483 
1484             BITS_PARSE("used_by_curr_pic_lt_sps_flag[ i ]", value, ps_bitstrm, 1);
1485             ps_sps->ai1_used_by_curr_pic_lt_sps_flag[i] = value;
1486         }
1487     }
1488 
1489     BITS_PARSE("sps_temporal_mvp_enable_flag", value, ps_bitstrm, 1);
1490     ps_sps->i1_sps_temporal_mvp_enable_flag = value;
1491 
1492     /* Print matches HM 8-2 */
1493     BITS_PARSE("sps_strong_intra_smoothing_enable_flag", value, ps_bitstrm, 1);
1494     ps_sps->i1_strong_intra_smoothing_enable_flag = value;
1495 
1496     BITS_PARSE("vui_parameters_present_flag", value, ps_bitstrm, 1);
1497     ps_sps->i1_vui_parameters_present_flag = value;
1498 
1499     if(ps_sps->i1_vui_parameters_present_flag)
1500     {
1501         ret = ihevcd_parse_vui_parameters(ps_bitstrm,
1502                                           &ps_sps->s_vui_parameters,
1503                                           ps_sps->i1_sps_max_sub_layers - 1);
1504         RETURN_IF((ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS), ret);
1505     }
1506 
1507     BITS_PARSE("sps_extension_flag", value, ps_bitstrm, 1);
1508 
1509     if((UWORD8 *)ps_bitstrm->pu4_buf > ps_bitstrm->pu1_buf_max)
1510     {
1511         return IHEVCD_INVALID_PARAMETER;
1512     }
1513 
1514     {
1515         WORD32 numerator;
1516         WORD32 ceil_offset;
1517 
1518         ceil_offset = (1 << ps_sps->i1_log2_ctb_size) - 1;
1519         numerator = ps_sps->i2_pic_width_in_luma_samples;
1520 
1521         ps_sps->i2_pic_wd_in_ctb = ((numerator + ceil_offset) /
1522                         (1 << ps_sps->i1_log2_ctb_size));
1523 
1524         numerator = ps_sps->i2_pic_height_in_luma_samples;
1525         ps_sps->i2_pic_ht_in_ctb = ((numerator + ceil_offset) /
1526                         (1 << ps_sps->i1_log2_ctb_size));
1527 
1528         ps_sps->i4_pic_size_in_ctb = ps_sps->i2_pic_ht_in_ctb *
1529                         ps_sps->i2_pic_wd_in_ctb;
1530 
1531         if(0 == ps_codec->i4_sps_done)
1532             ps_codec->s_parse.i4_next_ctb_indx = ps_sps->i4_pic_size_in_ctb;
1533 
1534         numerator = ps_sps->i2_pic_width_in_luma_samples;
1535         ps_sps->i2_pic_wd_in_min_cb = numerator  /
1536                         (1 << ps_sps->i1_log2_min_coding_block_size);
1537 
1538         numerator = ps_sps->i2_pic_height_in_luma_samples;
1539         ps_sps->i2_pic_ht_in_min_cb = numerator  /
1540                         (1 << ps_sps->i1_log2_min_coding_block_size);
1541     }
1542     if((0 != ps_codec->u4_allocate_dynamic_done) &&
1543                     ((ps_codec->i4_wd != ps_sps->i2_pic_width_in_luma_samples) ||
1544                     (ps_codec->i4_ht != ps_sps->i2_pic_height_in_luma_samples)))
1545     {
1546         if(0 == ps_codec->i4_first_pic_done)
1547         {
1548             return IHEVCD_INVALID_PARAMETER;
1549         }
1550         ps_codec->i4_reset_flag = 1;
1551         return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
1552     }
1553 
1554     /* Update display width and display height */
1555     {
1556         WORD32 disp_wd, disp_ht;
1557         WORD32 crop_unit_x, crop_unit_y;
1558         crop_unit_x = 1;
1559         crop_unit_y = 1;
1560 
1561         if(CHROMA_FMT_IDC_YUV420 == ps_sps->i1_chroma_format_idc)
1562         {
1563             crop_unit_x = 2;
1564             crop_unit_y = 2;
1565         }
1566 
1567         disp_wd = ps_sps->i2_pic_width_in_luma_samples;
1568         disp_wd -= ps_sps->i2_pic_crop_left_offset * crop_unit_x;
1569         disp_wd -= ps_sps->i2_pic_crop_right_offset * crop_unit_x;
1570 
1571 
1572         disp_ht = ps_sps->i2_pic_height_in_luma_samples;
1573         disp_ht -= ps_sps->i2_pic_crop_top_offset * crop_unit_y;
1574         disp_ht -= ps_sps->i2_pic_crop_bottom_offset * crop_unit_y;
1575 
1576         if((0 >= disp_wd) || (0 >= disp_ht))
1577             return IHEVCD_INVALID_PARAMETER;
1578 
1579         ps_codec->i4_disp_wd = disp_wd;
1580         ps_codec->i4_disp_ht = disp_ht;
1581 
1582 
1583         ps_codec->i4_wd = ps_sps->i2_pic_width_in_luma_samples;
1584         ps_codec->i4_ht = ps_sps->i2_pic_height_in_luma_samples;
1585 
1586         {
1587             WORD32 ref_strd;
1588             ref_strd = ALIGN32(ps_sps->i2_pic_width_in_luma_samples + PAD_WD);
1589             if(ps_codec->i4_strd < ref_strd)
1590             {
1591                 ps_codec->i4_strd = ref_strd;
1592             }
1593         }
1594 
1595         if(0 == ps_codec->i4_share_disp_buf)
1596         {
1597             if(ps_codec->i4_disp_strd < ps_codec->i4_disp_wd)
1598             {
1599                 ps_codec->i4_disp_strd = ps_codec->i4_disp_wd;
1600             }
1601         }
1602         else
1603         {
1604             if(ps_codec->i4_disp_strd < ps_codec->i4_strd)
1605             {
1606                 ps_codec->i4_disp_strd = ps_codec->i4_strd;
1607             }
1608         }
1609     }
1610 
1611     ps_codec->i4_sps_done = 1;
1612     return ret;
1613 }
1614 
1615 
ihevcd_unmark_pps(codec_t * ps_codec,WORD32 sps_id)1616 void ihevcd_unmark_pps(codec_t *ps_codec, WORD32 sps_id)
1617 {
1618     WORD32 pps_id = 0;
1619     pps_t *ps_pps = ps_codec->ps_pps_base;
1620 
1621     for(pps_id = 0; pps_id < MAX_PPS_CNT - 1; pps_id++, ps_pps++)
1622     {
1623         if((ps_pps->i1_pps_valid) &&
1624                         (ps_pps->i1_sps_id == sps_id))
1625             ps_pps->i1_pps_valid = 0;
1626     }
1627 }
1628 
1629 
ihevcd_copy_sps(codec_t * ps_codec,WORD32 sps_id,WORD32 sps_id_ref)1630 void ihevcd_copy_sps(codec_t *ps_codec, WORD32 sps_id, WORD32 sps_id_ref)
1631 {
1632     sps_t *ps_sps, *ps_sps_ref;
1633     WORD16 *pi2_scaling_mat_backup;
1634     WORD32 scaling_mat_size;
1635 
1636     SCALING_MAT_SIZE(scaling_mat_size);
1637     ps_sps_ref = ps_codec->ps_sps_base + sps_id_ref;
1638     ps_sps = ps_codec->ps_sps_base + sps_id;
1639 
1640     if(ps_sps->i1_sps_valid)
1641     {
1642         if((ps_sps->i1_log2_ctb_size != ps_sps_ref->i1_log2_ctb_size) ||
1643                         (ps_sps->i2_pic_wd_in_ctb != ps_sps_ref->i2_pic_wd_in_ctb) ||
1644                         (ps_sps->i2_pic_ht_in_ctb != ps_sps_ref->i2_pic_ht_in_ctb))
1645         {
1646             ihevcd_unmark_pps(ps_codec, sps_id);
1647         }
1648     }
1649 
1650     pi2_scaling_mat_backup = ps_sps->pi2_scaling_mat;
1651 
1652     memcpy(ps_sps, ps_sps_ref, sizeof(sps_t));
1653     ps_sps->pi2_scaling_mat = pi2_scaling_mat_backup;
1654     memcpy(ps_sps->pi2_scaling_mat, ps_sps_ref->pi2_scaling_mat, scaling_mat_size * sizeof(WORD16));
1655     ps_sps->i1_sps_valid = 1;
1656 
1657     ps_codec->s_parse.ps_sps = ps_sps;
1658 }
1659 
1660 
1661 /**
1662 *******************************************************************************
1663 *
1664 * @brief
1665 *  Parses PPS (Picture Parameter Set)
1666 *
1667 * @par Description:
1668 *  Parse Picture Parameter Set as per section  Section: 7.3.2.3
1669 * The pps is written to a temporary buffer and copied later to the
1670 * appropriate location
1671 *
1672 * @param[in] ps_codec
1673 *  Pointer to codec context
1674 *
1675 * @returns Error code from IHEVCD_ERROR_T
1676 *
1677 * @remarks
1678 *
1679 *
1680 *******************************************************************************
1681 */
ihevcd_parse_pps(codec_t * ps_codec)1682 IHEVCD_ERROR_T ihevcd_parse_pps(codec_t *ps_codec)
1683 {
1684     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
1685     WORD32 value;
1686     WORD32 pps_id;
1687 
1688     pps_t *ps_pps;
1689     sps_t *ps_sps;
1690     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
1691 
1692 
1693     if(0 == ps_codec->i4_sps_done)
1694         return IHEVCD_INVALID_HEADER;
1695 
1696     UEV_PARSE("pic_parameter_set_id", value, ps_bitstrm);
1697 
1698     pps_id = value;
1699     if((pps_id >= MAX_PPS_CNT) || (pps_id < 0))
1700     {
1701         if(ps_codec->i4_pps_done)
1702             return IHEVCD_UNSUPPORTED_PPS_ID;
1703         else
1704             pps_id = 0;
1705     }
1706 
1707 
1708     ps_pps = (ps_codec->s_parse.ps_pps_base + MAX_PPS_CNT - 1);
1709 
1710     ps_pps->i1_pps_id = pps_id;
1711 
1712     UEV_PARSE("seq_parameter_set_id", value, ps_bitstrm);
1713     ps_pps->i1_sps_id = value;
1714     ps_pps->i1_sps_id = CLIP3(ps_pps->i1_sps_id, 0, MAX_SPS_CNT - 2);
1715 
1716     ps_sps = (ps_codec->s_parse.ps_sps_base + ps_pps->i1_sps_id);
1717 
1718     /* If the SPS that is being referred to has not been parsed,
1719      * copy an existing SPS to the current location */
1720     if(0 == ps_sps->i1_sps_valid)
1721     {
1722         return IHEVCD_INVALID_HEADER;
1723 
1724 /*
1725         sps_t *ps_sps_ref = ps_codec->ps_sps_base;
1726         while(0 == ps_sps_ref->i1_sps_valid)
1727             ps_sps_ref++;
1728         ihevcd_copy_sps(ps_codec, ps_pps->i1_sps_id, ps_sps_ref->i1_sps_id);
1729 */
1730     }
1731 
1732     BITS_PARSE("dependent_slices_enabled_flag", value, ps_bitstrm, 1);
1733     ps_pps->i1_dependent_slice_enabled_flag = value;
1734 
1735     BITS_PARSE("output_flag_present_flag", value, ps_bitstrm, 1);
1736     ps_pps->i1_output_flag_present_flag = value;
1737 
1738     BITS_PARSE("num_extra_slice_header_bits", value, ps_bitstrm, 3);
1739     ps_pps->i1_num_extra_slice_header_bits = value;
1740 
1741 
1742     BITS_PARSE("sign_data_hiding_flag", value, ps_bitstrm, 1);
1743     ps_pps->i1_sign_data_hiding_flag = value;
1744 
1745     BITS_PARSE("cabac_init_present_flag", value, ps_bitstrm, 1);
1746     ps_pps->i1_cabac_init_present_flag = value;
1747 
1748     UEV_PARSE("num_ref_idx_l0_default_active_minus1", value, ps_bitstrm);
1749     ps_pps->i1_num_ref_idx_l0_default_active = value + 1;
1750 
1751     UEV_PARSE("num_ref_idx_l1_default_active_minus1", value, ps_bitstrm);
1752     ps_pps->i1_num_ref_idx_l1_default_active = value + 1;
1753 
1754     SEV_PARSE("pic_init_qp_minus26", value, ps_bitstrm);
1755     ps_pps->i1_pic_init_qp = value + 26;
1756 
1757     BITS_PARSE("constrained_intra_pred_flag", value, ps_bitstrm, 1);
1758     ps_pps->i1_constrained_intra_pred_flag = value;
1759 
1760     BITS_PARSE("transform_skip_enabled_flag", value, ps_bitstrm, 1);
1761     ps_pps->i1_transform_skip_enabled_flag = value;
1762 
1763     BITS_PARSE("cu_qp_delta_enabled_flag", value, ps_bitstrm, 1);
1764     ps_pps->i1_cu_qp_delta_enabled_flag = value;
1765 
1766     if(ps_pps->i1_cu_qp_delta_enabled_flag)
1767     {
1768         UEV_PARSE("diff_cu_qp_delta_depth", value, ps_bitstrm);
1769         ps_pps->i1_diff_cu_qp_delta_depth = value;
1770     }
1771     else
1772     {
1773         ps_pps->i1_diff_cu_qp_delta_depth = 0;
1774     }
1775     ps_pps->i1_log2_min_cu_qp_delta_size = ps_sps->i1_log2_ctb_size - ps_pps->i1_diff_cu_qp_delta_depth;
1776     /* Print different */
1777     SEV_PARSE("cb_qp_offset", value, ps_bitstrm);
1778     ps_pps->i1_pic_cb_qp_offset = value;
1779 
1780     /* Print different */
1781     SEV_PARSE("cr_qp_offset", value, ps_bitstrm);
1782     ps_pps->i1_pic_cr_qp_offset = value;
1783 
1784     /* Print different */
1785     BITS_PARSE("slicelevel_chroma_qp_flag", value, ps_bitstrm, 1);
1786     ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag = value;
1787 
1788     BITS_PARSE("weighted_pred_flag", value, ps_bitstrm, 1);
1789     ps_pps->i1_weighted_pred_flag = value;
1790 
1791     BITS_PARSE("weighted_bipred_flag", value, ps_bitstrm, 1);
1792     ps_pps->i1_weighted_bipred_flag = value;
1793 
1794     BITS_PARSE("transquant_bypass_enable_flag", value, ps_bitstrm, 1);
1795     ps_pps->i1_transquant_bypass_enable_flag = value;
1796 
1797     BITS_PARSE("tiles_enabled_flag", value, ps_bitstrm, 1);
1798     ps_pps->i1_tiles_enabled_flag = value;
1799 
1800     BITS_PARSE("entropy_coding_sync_enabled_flag", value, ps_bitstrm, 1);
1801     ps_pps->i1_entropy_coding_sync_enabled_flag = value;
1802 
1803     ps_pps->i1_loop_filter_across_tiles_enabled_flag = 0;
1804     if(ps_pps->i1_tiles_enabled_flag)
1805     {
1806         WORD32 wd = ALIGN64(ps_codec->i4_wd);
1807         WORD32 ht = ALIGN64(ps_codec->i4_ht);
1808 
1809         WORD32 max_tile_cols = (wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
1810         WORD32 max_tile_rows = (ht + MIN_TILE_HT - 1) / MIN_TILE_HT;
1811 
1812         UEV_PARSE("num_tile_columns_minus1", value, ps_bitstrm);
1813         ps_pps->i1_num_tile_columns = value + 1;
1814 
1815         UEV_PARSE("num_tile_rows_minus1", value, ps_bitstrm);
1816         ps_pps->i1_num_tile_rows = value + 1;
1817 
1818         if((ps_pps->i1_num_tile_columns < 1) ||
1819                         (ps_pps->i1_num_tile_columns > max_tile_cols) ||
1820                         (ps_pps->i1_num_tile_rows < 1) ||
1821                         (ps_pps->i1_num_tile_rows > max_tile_rows))
1822             return IHEVCD_INVALID_HEADER;
1823 
1824         BITS_PARSE("uniform_spacing_flag", value, ps_bitstrm, 1);
1825         ps_pps->i1_uniform_spacing_flag = value;
1826 
1827 
1828         {
1829 
1830             WORD32 start;
1831             WORD32 i, j;
1832 
1833 
1834             start = 0;
1835             for(i = 0; i < ps_pps->i1_num_tile_columns; i++)
1836             {
1837                 tile_t *ps_tile;
1838                 if(!ps_pps->i1_uniform_spacing_flag)
1839                 {
1840                     if(i < (ps_pps->i1_num_tile_columns - 1))
1841                     {
1842                         UEV_PARSE("column_width_minus1[ i ]", value, ps_bitstrm);
1843                         value += 1;
1844                     }
1845                     else
1846                     {
1847                         value = ps_sps->i2_pic_wd_in_ctb - start;
1848                     }
1849                 }
1850                 else
1851                 {
1852                     value = ((i + 1) * ps_sps->i2_pic_wd_in_ctb) / ps_pps->i1_num_tile_columns -
1853                                     (i * ps_sps->i2_pic_wd_in_ctb) / ps_pps->i1_num_tile_columns;
1854                 }
1855 
1856                 for(j = 0; j < ps_pps->i1_num_tile_rows; j++)
1857                 {
1858                     ps_tile = ps_pps->ps_tile + j * ps_pps->i1_num_tile_columns + i;
1859                     ps_tile->u1_pos_x = start;
1860                     ps_tile->u2_wd = value;
1861                 }
1862                 start += value;
1863 
1864                 if((start > ps_sps->i2_pic_wd_in_ctb) ||
1865                                 (value <= 0))
1866                     return IHEVCD_INVALID_HEADER;
1867             }
1868 
1869             start = 0;
1870             for(i = 0; i < (ps_pps->i1_num_tile_rows); i++)
1871             {
1872                 tile_t *ps_tile;
1873                 if(!ps_pps->i1_uniform_spacing_flag)
1874                 {
1875                     if(i < (ps_pps->i1_num_tile_rows - 1))
1876                     {
1877 
1878                         UEV_PARSE("row_height_minus1[ i ]", value, ps_bitstrm);
1879                         value += 1;
1880                     }
1881                     else
1882                     {
1883                         value = ps_sps->i2_pic_ht_in_ctb - start;
1884                     }
1885                 }
1886                 else
1887                 {
1888                     value = ((i + 1) * ps_sps->i2_pic_ht_in_ctb) / ps_pps->i1_num_tile_rows -
1889                                     (i * ps_sps->i2_pic_ht_in_ctb) / ps_pps->i1_num_tile_rows;
1890                 }
1891 
1892                 for(j = 0; j < ps_pps->i1_num_tile_columns; j++)
1893                 {
1894                     ps_tile = ps_pps->ps_tile + i * ps_pps->i1_num_tile_columns + j;
1895                     ps_tile->u1_pos_y = start;
1896                     ps_tile->u2_ht = value;
1897                 }
1898                 start += value;
1899 
1900                 if((start > ps_sps->i2_pic_ht_in_ctb) ||
1901                                 (value <= 0))
1902                     return IHEVCD_INVALID_HEADER;
1903             }
1904         }
1905 
1906 
1907         BITS_PARSE("loop_filter_across_tiles_enabled_flag", value, ps_bitstrm, 1);
1908         ps_pps->i1_loop_filter_across_tiles_enabled_flag = value;
1909 
1910     }
1911     else
1912     {
1913         /* If tiles are not present, set first tile in each PPS to have tile
1914         width and height equal to picture width and height */
1915         ps_pps->i1_num_tile_columns = 1;
1916         ps_pps->i1_num_tile_rows = 1;
1917         ps_pps->i1_uniform_spacing_flag = 1;
1918 
1919         ps_pps->ps_tile->u1_pos_x = 0;
1920         ps_pps->ps_tile->u1_pos_y = 0;
1921         ps_pps->ps_tile->u2_wd = ps_sps->i2_pic_wd_in_ctb;
1922         ps_pps->ps_tile->u2_ht = ps_sps->i2_pic_ht_in_ctb;
1923     }
1924 
1925     BITS_PARSE("loop_filter_across_slices_enabled_flag", value, ps_bitstrm, 1);
1926     ps_pps->i1_loop_filter_across_slices_enabled_flag = value;
1927 
1928     BITS_PARSE("deblocking_filter_control_present_flag", value, ps_bitstrm, 1);
1929     ps_pps->i1_deblocking_filter_control_present_flag = value;
1930 
1931     /* Default values */
1932     ps_pps->i1_pic_disable_deblocking_filter_flag = 0;
1933     ps_pps->i1_deblocking_filter_override_enabled_flag = 0;
1934     ps_pps->i1_beta_offset_div2 = 0;
1935     ps_pps->i1_tc_offset_div2 = 0;
1936 
1937     if(ps_pps->i1_deblocking_filter_control_present_flag)
1938     {
1939 
1940         BITS_PARSE("deblocking_filter_override_enabled_flag", value, ps_bitstrm, 1);
1941         ps_pps->i1_deblocking_filter_override_enabled_flag = value;
1942 
1943         BITS_PARSE("pic_disable_deblocking_filter_flag", value, ps_bitstrm, 1);
1944         ps_pps->i1_pic_disable_deblocking_filter_flag = value;
1945 
1946         if(!ps_pps->i1_pic_disable_deblocking_filter_flag)
1947         {
1948 
1949             SEV_PARSE("pps_beta_offset_div2", value, ps_bitstrm);
1950             ps_pps->i1_beta_offset_div2 = value;
1951 
1952             SEV_PARSE("pps_tc_offset_div2", value, ps_bitstrm);
1953             ps_pps->i1_tc_offset_div2 = value;
1954 
1955         }
1956     }
1957 
1958     BITS_PARSE("pps_scaling_list_data_present_flag", value, ps_bitstrm, 1);
1959     ps_pps->i1_pps_scaling_list_data_present_flag = value;
1960 
1961     if(ps_pps->i1_pps_scaling_list_data_present_flag)
1962     {
1963         COPY_DEFAULT_SCALING_LIST(ps_pps->pi2_scaling_mat);
1964         ihevcd_scaling_list_data(ps_codec, ps_pps->pi2_scaling_mat);
1965     }
1966 
1967     BITS_PARSE("lists_modification_present_flag", value, ps_bitstrm, 1);
1968     ps_pps->i1_lists_modification_present_flag = value;
1969     UEV_PARSE("log2_parallel_merge_level_minus2", value, ps_bitstrm);
1970     ps_pps->i1_log2_parallel_merge_level = value + 2;
1971 
1972     BITS_PARSE("slice_header_extension_present_flag", value, ps_bitstrm, 1);
1973     ps_pps->i1_slice_header_extension_present_flag = value;
1974     /* Not present in HM */
1975     BITS_PARSE("pps_extension_flag", value, ps_bitstrm, 1);
1976 
1977     if((UWORD8 *)ps_bitstrm->pu4_buf > ps_bitstrm->pu1_buf_max)
1978         return IHEVCD_INVALID_PARAMETER;
1979 
1980     ps_codec->i4_pps_done = 1;
1981     return ret;
1982 }
1983 
1984 
ihevcd_copy_pps(codec_t * ps_codec,WORD32 pps_id,WORD32 pps_id_ref)1985 void ihevcd_copy_pps(codec_t *ps_codec, WORD32 pps_id, WORD32 pps_id_ref)
1986 {
1987     pps_t *ps_pps, *ps_pps_ref;
1988     WORD16 *pi2_scaling_mat_backup;
1989     WORD32 scaling_mat_size;
1990     tile_t *ps_tile_backup;
1991     WORD32 max_tile_cols, max_tile_rows;
1992     WORD32 wd, ht;
1993     wd = ALIGN64(ps_codec->i4_wd);
1994     ht = ALIGN64(ps_codec->i4_ht);
1995 
1996     SCALING_MAT_SIZE(scaling_mat_size);
1997     max_tile_cols = (wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
1998     max_tile_rows = (ht + MIN_TILE_HT - 1) / MIN_TILE_HT;
1999 
2000     ps_pps_ref = ps_codec->ps_pps_base + pps_id_ref;
2001     ps_pps = ps_codec->ps_pps_base + pps_id;
2002 
2003     pi2_scaling_mat_backup = ps_pps->pi2_scaling_mat;
2004     ps_tile_backup = ps_pps->ps_tile;
2005 
2006     memcpy(ps_pps, ps_pps_ref, sizeof(pps_t));
2007     ps_pps->pi2_scaling_mat = pi2_scaling_mat_backup;
2008     ps_pps->ps_tile = ps_tile_backup;
2009     memcpy(ps_pps->pi2_scaling_mat, ps_pps_ref->pi2_scaling_mat, scaling_mat_size * sizeof(WORD16));
2010     memcpy(ps_pps->ps_tile, ps_pps_ref->ps_tile, max_tile_cols * max_tile_rows * sizeof(tile_t));
2011 
2012     ps_pps->i1_pps_valid = 1;
2013 
2014     ps_codec->s_parse.ps_pps = ps_pps;
2015 }
2016 
2017 
2018 
2019 /**
2020 *******************************************************************************
2021 *
2022 * @brief
2023 *  Parses SEI (Supplemental Enhancement Information)
2024 *
2025 * @par Description:
2026 *  Parses SEI (Supplemental Enhancement Information) as per Section: 7.3.7
2027 *
2028 * @param[in] ps_codec
2029 *  Pointer to codec context
2030 *
2031 * @returns Error code from IHEVCD_ERROR_T
2032 *
2033 * @remarks
2034 *
2035 *
2036 *******************************************************************************
2037 */
ihevcd_parse_sei(codec_t * ps_codec)2038 IHEVCD_ERROR_T ihevcd_parse_sei(codec_t *ps_codec)
2039 {
2040     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2041     UNUSED(ps_codec);
2042     return ret;
2043 }
2044 
2045 /**
2046 *******************************************************************************
2047 *
2048 * @brief
2049 *  Parses Access unit delimiter
2050 *
2051 * @par Description:
2052 *  Parses Access unit delimiter as per section  Section: 7.3.2.5
2053 *
2054 * @param[in] ps_codec
2055 *  Pointer to codec context
2056 *
2057 * @returns Error code from IHEVCD_ERROR_T
2058 *
2059 * @remarks
2060 *
2061 *
2062 *******************************************************************************
2063 */
ihevcd_parse_aud(codec_t * ps_codec)2064 WORD32 ihevcd_parse_aud(codec_t *ps_codec)
2065 {
2066     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2067     UNUSED(ps_codec);
2068     return ret;
2069 }
2070 
ihevcd_extend_sign_bit(WORD32 value,WORD32 num_bits)2071 WORD32 ihevcd_extend_sign_bit(WORD32 value, WORD32 num_bits)
2072 {
2073     WORD32 ret_value = value;
2074     if(value >> (num_bits - 1))
2075     {
2076         ret_value |= (0xFFFFFFFF << num_bits);
2077     }
2078     return ret_value;
2079 }
2080 
2081 /**
2082 *******************************************************************************
2083 *
2084 * @brief
2085 *  Calculate POC of the current slice
2086 *
2087 * @par Description:
2088 *  Calculates the current POC using the previous POC lsb and previous POC msb
2089 *
2090 * @param[in] ps_codec
2091 *  Pointer to codec context
2092 *
2093 * @param[in] i1_pic_order_cnt_lsb
2094 *  Current POC lsb
2095 *
2096 * @returns  Current absolute POC
2097 *
2098 * @remarks
2099 *
2100 *
2101 *******************************************************************************
2102 */
2103 
ihevcd_calc_poc(codec_t * ps_codec,nal_header_t * ps_nal,WORD8 i1_log2_max_poc_lsb,WORD32 i2_poc_lsb)2104 WORD32 ihevcd_calc_poc(codec_t *ps_codec, nal_header_t *ps_nal, WORD8 i1_log2_max_poc_lsb, WORD32 i2_poc_lsb)
2105 {
2106     WORD32 i4_abs_poc, i4_poc_msb;
2107     WORD32 max_poc_lsb;
2108     WORD8 i1_nal_unit_type = ps_nal->i1_nal_unit_type;
2109     max_poc_lsb = (1 << i1_log2_max_poc_lsb);
2110 
2111     if((!ps_codec->i4_first_pic_done) && (!ps_codec->i4_pic_present))
2112         ps_codec->i4_prev_poc_msb = -2 * max_poc_lsb;
2113 
2114     if(NAL_IDR_N_LP == i1_nal_unit_type
2115                     || NAL_IDR_W_LP == i1_nal_unit_type
2116                     || NAL_BLA_N_LP == i1_nal_unit_type
2117                     || NAL_BLA_W_DLP == i1_nal_unit_type
2118                     || NAL_BLA_W_LP == i1_nal_unit_type
2119                     || (NAL_CRA == i1_nal_unit_type && !ps_codec->i4_first_pic_done))
2120     {
2121         i4_poc_msb = ps_codec->i4_prev_poc_msb + 2 * max_poc_lsb;
2122         ps_codec->i4_prev_poc_lsb = 0;
2123         ps_codec->i4_max_prev_poc_lsb = 0;
2124 //        ps_codec->i4_prev_poc_msb = 0;
2125     }
2126     else
2127     {
2128 
2129         if((i2_poc_lsb < ps_codec->i4_prev_poc_lsb)
2130                         && ((ps_codec->i4_prev_poc_lsb - i2_poc_lsb) >= max_poc_lsb / 2))
2131         {
2132             i4_poc_msb = ps_codec->i4_prev_poc_msb + max_poc_lsb;
2133         }
2134         else if((i2_poc_lsb > ps_codec->i4_prev_poc_lsb)
2135                         && ((i2_poc_lsb - ps_codec->i4_prev_poc_lsb) > max_poc_lsb / 2))
2136         {
2137             i4_poc_msb = ps_codec->i4_prev_poc_msb - max_poc_lsb;
2138         }
2139         else
2140         {
2141             i4_poc_msb = ps_codec->i4_prev_poc_msb;
2142         }
2143 
2144 
2145     }
2146 
2147     i4_abs_poc = i4_poc_msb + i2_poc_lsb;
2148     ps_codec->i4_max_prev_poc_lsb = MAX(ps_codec->i4_max_prev_poc_lsb, i2_poc_lsb);
2149 
2150     {
2151         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));
2152         WORD32 update_prev_poc = ((is_reference_nal) && ((i1_nal_unit_type < NAL_RADL_N) || (i1_nal_unit_type > NAL_RASL_R)));
2153 
2154         if((0 == ps_nal->i1_nuh_temporal_id) &&
2155                         (update_prev_poc))
2156         {
2157             ps_codec->i4_prev_poc_lsb = i2_poc_lsb;
2158             ps_codec->i4_prev_poc_msb = i4_poc_msb;
2159         }
2160     }
2161 
2162     return i4_abs_poc;
2163 }
2164 
2165 
ihevcd_copy_slice_hdr(codec_t * ps_codec,WORD32 slice_idx,WORD32 slice_idx_ref)2166 void ihevcd_copy_slice_hdr(codec_t *ps_codec, WORD32 slice_idx, WORD32 slice_idx_ref)
2167 {
2168     slice_header_t *ps_slice_hdr, *ps_slice_hdr_ref;
2169     WORD32 *pu4_entry_offset_backup;
2170 
2171     ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr_base + slice_idx;
2172     ps_slice_hdr_ref = ps_codec->s_parse.ps_slice_hdr_base + slice_idx_ref;
2173 
2174     pu4_entry_offset_backup = ps_slice_hdr->pu4_entry_point_offset;
2175     memcpy(ps_slice_hdr, ps_slice_hdr_ref, sizeof(slice_header_t));
2176     ps_slice_hdr->pu4_entry_point_offset = pu4_entry_offset_backup;
2177 }
2178 
2179 
2180 
2181