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