• 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     /* This is used only during initialization to get reorder count etc */
1322     ps_codec->i4_sps_id = sps_id;
1323     memcpy(&ps_sps->s_ptl, &s_ptl, sizeof(profile_tier_lvl_info_t));
1324 
1325     UEV_PARSE("chroma_format_idc", value, ps_bitstrm);
1326     ps_sps->i1_chroma_format_idc = value;
1327 
1328     if(ps_sps->i1_chroma_format_idc != CHROMA_FMT_IDC_YUV420)
1329     {
1330         ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
1331         return (IHEVCD_ERROR_T)IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
1332     }
1333 
1334     if(CHROMA_FMT_IDC_YUV444_PLANES == ps_sps->i1_chroma_format_idc)
1335     {
1336         BITS_PARSE("separate_colour_plane_flag", value, ps_bitstrm, 1);
1337         ps_sps->i1_separate_colour_plane_flag = value;
1338     }
1339     else
1340     {
1341         ps_sps->i1_separate_colour_plane_flag = 0;
1342     }
1343 
1344     UEV_PARSE("pic_width_in_luma_samples", value, ps_bitstrm);
1345     ps_sps->i2_pic_width_in_luma_samples = value;
1346 
1347     UEV_PARSE("pic_height_in_luma_samples", value, ps_bitstrm);
1348     ps_sps->i2_pic_height_in_luma_samples = value;
1349 
1350     if((0 >= ps_sps->i2_pic_width_in_luma_samples) || (0 >= ps_sps->i2_pic_height_in_luma_samples))
1351         return IHEVCD_INVALID_PARAMETER;
1352 
1353     /* i2_pic_width_in_luma_samples and i2_pic_height_in_luma_samples
1354        should be multiples of min_cb_size. Here these are aligned to 8,
1355        i.e. smallest CB size */
1356     ps_sps->i2_pic_width_in_luma_samples = ALIGN8(ps_sps->i2_pic_width_in_luma_samples);
1357     ps_sps->i2_pic_height_in_luma_samples = ALIGN8(ps_sps->i2_pic_height_in_luma_samples);
1358 
1359     BITS_PARSE("pic_cropping_flag", value, ps_bitstrm, 1);
1360     ps_sps->i1_pic_cropping_flag = value;
1361 
1362     if(ps_sps->i1_pic_cropping_flag)
1363     {
1364 
1365         UEV_PARSE("pic_crop_left_offset", value, ps_bitstrm);
1366         if (value >= ps_sps->i2_pic_width_in_luma_samples)
1367         {
1368             return IHEVCD_INVALID_PARAMETER;
1369         }
1370         ps_sps->i2_pic_crop_left_offset = value;
1371 
1372         UEV_PARSE("pic_crop_right_offset", value, ps_bitstrm);
1373         if (value >= ps_sps->i2_pic_width_in_luma_samples)
1374         {
1375             return IHEVCD_INVALID_PARAMETER;
1376         }
1377         ps_sps->i2_pic_crop_right_offset = value;
1378 
1379         UEV_PARSE("pic_crop_top_offset", value, ps_bitstrm);
1380         if (value >= ps_sps->i2_pic_height_in_luma_samples)
1381         {
1382             return IHEVCD_INVALID_PARAMETER;
1383         }
1384         ps_sps->i2_pic_crop_top_offset = value;
1385 
1386         UEV_PARSE("pic_crop_bottom_offset", value, ps_bitstrm);
1387         if (value >= ps_sps->i2_pic_height_in_luma_samples)
1388         {
1389             return IHEVCD_INVALID_PARAMETER;
1390         }
1391         ps_sps->i2_pic_crop_bottom_offset = value;
1392     }
1393     else
1394     {
1395         ps_sps->i2_pic_crop_left_offset = 0;
1396         ps_sps->i2_pic_crop_right_offset = 0;
1397         ps_sps->i2_pic_crop_top_offset = 0;
1398         ps_sps->i2_pic_crop_bottom_offset = 0;
1399     }
1400 
1401 
1402     UEV_PARSE("bit_depth_luma_minus8", value, ps_bitstrm);
1403     if(0 != value)
1404         return IHEVCD_UNSUPPORTED_BIT_DEPTH;
1405 
1406     UEV_PARSE("bit_depth_chroma_minus8", value, ps_bitstrm);
1407     if(0 != value)
1408         return IHEVCD_UNSUPPORTED_BIT_DEPTH;
1409 
1410     UEV_PARSE("log2_max_pic_order_cnt_lsb_minus4", value, ps_bitstrm);
1411     ps_sps->i1_log2_max_pic_order_cnt_lsb = value + 4;
1412 
1413     BITS_PARSE("sps_sub_layer_ordering_info_present_flag", value, ps_bitstrm, 1);
1414     ps_sps->i1_sps_sub_layer_ordering_info_present_flag = value;
1415 
1416 
1417     i = (ps_sps->i1_sps_sub_layer_ordering_info_present_flag ? 0 : (ps_sps->i1_sps_max_sub_layers - 1));
1418     for(; i < ps_sps->i1_sps_max_sub_layers; i++)
1419     {
1420         UEV_PARSE("max_dec_pic_buffering", value, ps_bitstrm);
1421         ps_sps->ai1_sps_max_dec_pic_buffering[i] = value + 1;
1422 
1423         if(ps_sps->ai1_sps_max_dec_pic_buffering[i] > MAX_DPB_SIZE)
1424         {
1425             return IHEVCD_INVALID_PARAMETER;
1426         }
1427 
1428         UEV_PARSE("num_reorder_pics", value, ps_bitstrm);
1429         ps_sps->ai1_sps_max_num_reorder_pics[i] = value;
1430 
1431         if(ps_sps->ai1_sps_max_num_reorder_pics[i] > ps_sps->ai1_sps_max_dec_pic_buffering[i])
1432         {
1433             return IHEVCD_INVALID_PARAMETER;
1434         }
1435 
1436         UEV_PARSE("max_latency_increase", value, ps_bitstrm);
1437         ps_sps->ai1_sps_max_latency_increase[i] = value;
1438     }
1439 
1440     /* Check if sps_max_dec_pic_buffering or sps_max_num_reorder_pics
1441        has changed */
1442     if(0 != ps_codec->i4_first_pic_done)
1443     {
1444         sps_t *ps_sps_old = ps_codec->s_parse.ps_sps;
1445         if(ps_sps_old->ai1_sps_max_dec_pic_buffering[ps_sps_old->i1_sps_max_sub_layers - 1] !=
1446                     ps_sps->ai1_sps_max_dec_pic_buffering[ps_sps->i1_sps_max_sub_layers - 1])
1447         {
1448             ps_codec->i4_reset_flag = 1;
1449             return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
1450         }
1451 
1452         if(ps_sps_old->ai1_sps_max_num_reorder_pics[ps_sps_old->i1_sps_max_sub_layers - 1] !=
1453                     ps_sps->ai1_sps_max_num_reorder_pics[ps_sps->i1_sps_max_sub_layers - 1])
1454         {
1455             ps_codec->i4_reset_flag = 1;
1456             return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
1457         }
1458     }
1459 
1460     UEV_PARSE("log2_min_coding_block_size_minus3", value, ps_bitstrm);
1461     ps_sps->i1_log2_min_coding_block_size = value + 3;
1462 
1463     UEV_PARSE("log2_diff_max_min_coding_block_size", value, ps_bitstrm);
1464     ps_sps->i1_log2_diff_max_min_coding_block_size = value;
1465 
1466     ctb_log2_size_y = ps_sps->i1_log2_min_coding_block_size + ps_sps->i1_log2_diff_max_min_coding_block_size;
1467 
1468     UEV_PARSE("log2_min_transform_block_size_minus2", value, ps_bitstrm);
1469     ps_sps->i1_log2_min_transform_block_size = value + 2;
1470 
1471     UEV_PARSE("log2_diff_max_min_transform_block_size", value, ps_bitstrm);
1472     ps_sps->i1_log2_diff_max_min_transform_block_size = value;
1473 
1474     ps_sps->i1_log2_max_transform_block_size = ps_sps->i1_log2_min_transform_block_size +
1475                     ps_sps->i1_log2_diff_max_min_transform_block_size;
1476 
1477     if ((ps_sps->i1_log2_max_transform_block_size < 0) ||
1478                     (ps_sps->i1_log2_max_transform_block_size > MIN(ctb_log2_size_y, 5)))
1479     {
1480         return IHEVCD_INVALID_PARAMETER;
1481     }
1482 
1483     ps_sps->i1_log2_ctb_size = ps_sps->i1_log2_min_coding_block_size +
1484                     ps_sps->i1_log2_diff_max_min_coding_block_size;
1485 
1486     if((ps_sps->i1_log2_min_coding_block_size < 3) ||
1487                     (ps_sps->i1_log2_min_transform_block_size < 2) ||
1488                     (ps_sps->i1_log2_diff_max_min_transform_block_size < 0) ||
1489                     (ps_sps->i1_log2_max_transform_block_size > ps_sps->i1_log2_ctb_size) ||
1490                     (ps_sps->i1_log2_ctb_size < 4) ||
1491                     (ps_sps->i1_log2_ctb_size > 6))
1492     {
1493         return IHEVCD_INVALID_PARAMETER;
1494     }
1495 
1496     ps_sps->i1_log2_min_pcm_coding_block_size = 0;
1497     ps_sps->i1_log2_diff_max_min_pcm_coding_block_size = 0;
1498 
1499     UEV_PARSE("max_transform_hierarchy_depth_inter", value, ps_bitstrm);
1500     ps_sps->i1_max_transform_hierarchy_depth_inter = value;
1501 
1502     UEV_PARSE("max_transform_hierarchy_depth_intra", value, ps_bitstrm);
1503     ps_sps->i1_max_transform_hierarchy_depth_intra = value;
1504 
1505     /* String has a d (enabled) in order to match with HM */
1506     BITS_PARSE("scaling_list_enabled_flag", value, ps_bitstrm, 1);
1507     ps_sps->i1_scaling_list_enable_flag = value;
1508 
1509     if(ps_sps->i1_scaling_list_enable_flag)
1510     {
1511         COPY_DEFAULT_SCALING_LIST(ps_sps->pi2_scaling_mat);
1512         BITS_PARSE("sps_scaling_list_data_present_flag", value, ps_bitstrm, 1);
1513         ps_sps->i1_sps_scaling_list_data_present_flag = value;
1514 
1515         if(ps_sps->i1_sps_scaling_list_data_present_flag)
1516             ihevcd_scaling_list_data(ps_codec, ps_sps->pi2_scaling_mat);
1517     }
1518     else
1519     {
1520         COPY_FLAT_SCALING_LIST(ps_sps->pi2_scaling_mat);
1521     }
1522     /* String is asymmetric_motion_partitions_enabled_flag instead of amp_enabled_flag in order to match with HM */
1523     BITS_PARSE("asymmetric_motion_partitions_enabled_flag", value, ps_bitstrm, 1);
1524     ps_sps->i1_amp_enabled_flag = value;
1525 
1526     BITS_PARSE("sample_adaptive_offset_enabled_flag", value, ps_bitstrm, 1);
1527     ps_sps->i1_sample_adaptive_offset_enabled_flag = value;
1528 
1529     BITS_PARSE("pcm_enabled_flag", value, ps_bitstrm, 1);
1530     ps_sps->i1_pcm_enabled_flag = value;
1531 
1532     if(ps_sps->i1_pcm_enabled_flag)
1533     {
1534         BITS_PARSE("pcm_sample_bit_depth_luma", value, ps_bitstrm, 4);
1535         ps_sps->i1_pcm_sample_bit_depth_luma = value + 1;
1536 
1537         BITS_PARSE("pcm_sample_bit_depth_chroma", value, ps_bitstrm, 4);
1538         ps_sps->i1_pcm_sample_bit_depth_chroma = value + 1;
1539 
1540         UEV_PARSE("log2_min_pcm_coding_block_size_minus3", value, ps_bitstrm);
1541         ps_sps->i1_log2_min_pcm_coding_block_size = value + 3;
1542 
1543         UEV_PARSE("log2_diff_max_min_pcm_coding_block_size", value, ps_bitstrm);
1544         ps_sps->i1_log2_diff_max_min_pcm_coding_block_size = value;
1545         BITS_PARSE("pcm_loop_filter_disable_flag", value, ps_bitstrm, 1);
1546         ps_sps->i1_pcm_loop_filter_disable_flag = value;
1547 
1548     }
1549     UEV_PARSE("num_short_term_ref_pic_sets", value, ps_bitstrm);
1550     ps_sps->i1_num_short_term_ref_pic_sets = value;
1551 
1552     ps_sps->i1_num_short_term_ref_pic_sets = CLIP3(ps_sps->i1_num_short_term_ref_pic_sets, 0, MAX_STREF_PICS_SPS);
1553 
1554     for(i = 0; i < ps_sps->i1_num_short_term_ref_pic_sets; i++)
1555         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]);
1556 
1557     BITS_PARSE("long_term_ref_pics_present_flag", value, ps_bitstrm, 1);
1558     ps_sps->i1_long_term_ref_pics_present_flag = value;
1559 
1560     if(ps_sps->i1_long_term_ref_pics_present_flag)
1561     {
1562         UEV_PARSE("num_long_term_ref_pics_sps", value, ps_bitstrm);
1563         ps_sps->i1_num_long_term_ref_pics_sps = value;
1564 
1565         for(i = 0; i < ps_sps->i1_num_long_term_ref_pics_sps; i++)
1566         {
1567             BITS_PARSE("lt_ref_pic_poc_lsb_sps[ i ]", value, ps_bitstrm, ps_sps->i1_log2_max_pic_order_cnt_lsb);
1568             ps_sps->au2_lt_ref_pic_poc_lsb_sps[i] = value;
1569 
1570             BITS_PARSE("used_by_curr_pic_lt_sps_flag[ i ]", value, ps_bitstrm, 1);
1571             ps_sps->ai1_used_by_curr_pic_lt_sps_flag[i] = value;
1572         }
1573     }
1574 
1575     BITS_PARSE("sps_temporal_mvp_enable_flag", value, ps_bitstrm, 1);
1576     ps_sps->i1_sps_temporal_mvp_enable_flag = value;
1577 
1578     /* Print matches HM 8-2 */
1579     BITS_PARSE("sps_strong_intra_smoothing_enable_flag", value, ps_bitstrm, 1);
1580     ps_sps->i1_strong_intra_smoothing_enable_flag = value;
1581 
1582     BITS_PARSE("vui_parameters_present_flag", value, ps_bitstrm, 1);
1583     ps_sps->i1_vui_parameters_present_flag = value;
1584 
1585     if(ps_sps->i1_vui_parameters_present_flag)
1586     {
1587         ret = ihevcd_parse_vui_parameters(ps_bitstrm,
1588                                           &ps_sps->s_vui_parameters,
1589                                           ps_sps->i1_sps_max_sub_layers - 1);
1590         RETURN_IF((ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS), ret);
1591     }
1592 
1593     BITS_PARSE("sps_extension_flag", value, ps_bitstrm, 1);
1594 
1595     if((UWORD8 *)ps_bitstrm->pu4_buf > ps_bitstrm->pu1_buf_max)
1596     {
1597         return IHEVCD_INVALID_PARAMETER;
1598     }
1599 
1600     {
1601         WORD32 numerator;
1602         WORD32 ceil_offset;
1603 
1604         ceil_offset = (1 << ps_sps->i1_log2_ctb_size) - 1;
1605         numerator = ps_sps->i2_pic_width_in_luma_samples;
1606 
1607         ps_sps->i2_pic_wd_in_ctb = ((numerator + ceil_offset) /
1608                         (1 << ps_sps->i1_log2_ctb_size));
1609 
1610         numerator = ps_sps->i2_pic_height_in_luma_samples;
1611         ps_sps->i2_pic_ht_in_ctb = ((numerator + ceil_offset) /
1612                         (1 << ps_sps->i1_log2_ctb_size));
1613 
1614         ps_sps->i4_pic_size_in_ctb = ps_sps->i2_pic_ht_in_ctb *
1615                         ps_sps->i2_pic_wd_in_ctb;
1616 
1617         if(0 == ps_codec->i4_sps_done)
1618             ps_codec->s_parse.i4_next_ctb_indx = ps_sps->i4_pic_size_in_ctb;
1619 
1620         numerator = ps_sps->i2_pic_width_in_luma_samples;
1621         ps_sps->i2_pic_wd_in_min_cb = numerator  /
1622                         (1 << ps_sps->i1_log2_min_coding_block_size);
1623 
1624         numerator = ps_sps->i2_pic_height_in_luma_samples;
1625         ps_sps->i2_pic_ht_in_min_cb = numerator  /
1626                         (1 << ps_sps->i1_log2_min_coding_block_size);
1627     }
1628     if((0 != ps_codec->i4_first_pic_done) &&
1629                     ((ps_codec->i4_wd != ps_sps->i2_pic_width_in_luma_samples) ||
1630                     (ps_codec->i4_ht != ps_sps->i2_pic_height_in_luma_samples)))
1631     {
1632         ps_codec->i4_reset_flag = 1;
1633         return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
1634     }
1635 
1636     if((ps_sps->i2_pic_width_in_luma_samples > MAX_WD) ||
1637                     ((ps_sps->i2_pic_width_in_luma_samples * ps_sps->i2_pic_height_in_luma_samples) >
1638                     (MAX_WD * MAX_HT)))
1639     {
1640         return (IHEVCD_ERROR_T)IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
1641     }
1642 
1643     /* Update display width and display height */
1644     {
1645         WORD32 disp_wd, disp_ht;
1646         WORD32 crop_unit_x, crop_unit_y;
1647         crop_unit_x = 1;
1648         crop_unit_y = 1;
1649 
1650         if(CHROMA_FMT_IDC_YUV420 == ps_sps->i1_chroma_format_idc)
1651         {
1652             crop_unit_x = 2;
1653             crop_unit_y = 2;
1654         }
1655 
1656         disp_wd = ps_sps->i2_pic_width_in_luma_samples;
1657         disp_wd -= ps_sps->i2_pic_crop_left_offset * crop_unit_x;
1658         disp_wd -= ps_sps->i2_pic_crop_right_offset * crop_unit_x;
1659 
1660 
1661         disp_ht = ps_sps->i2_pic_height_in_luma_samples;
1662         disp_ht -= ps_sps->i2_pic_crop_top_offset * crop_unit_y;
1663         disp_ht -= ps_sps->i2_pic_crop_bottom_offset * crop_unit_y;
1664 
1665         if((0 >= disp_wd) || (0 >= disp_ht))
1666             return IHEVCD_INVALID_PARAMETER;
1667 
1668         ps_codec->i4_disp_wd = disp_wd;
1669         ps_codec->i4_disp_ht = disp_ht;
1670 
1671 
1672         ps_codec->i4_wd = ps_sps->i2_pic_width_in_luma_samples;
1673         ps_codec->i4_ht = ps_sps->i2_pic_height_in_luma_samples;
1674 
1675         {
1676             WORD32 ref_strd;
1677             ref_strd = ALIGN32(ps_sps->i2_pic_width_in_luma_samples + PAD_WD);
1678             if(ps_codec->i4_strd < ref_strd)
1679             {
1680                 ps_codec->i4_strd = ref_strd;
1681             }
1682         }
1683 
1684         if(0 == ps_codec->i4_share_disp_buf)
1685         {
1686             if(ps_codec->i4_disp_strd < ps_codec->i4_disp_wd)
1687             {
1688                 ps_codec->i4_disp_strd = ps_codec->i4_disp_wd;
1689             }
1690         }
1691         else
1692         {
1693             if(ps_codec->i4_disp_strd < ps_codec->i4_strd)
1694             {
1695                 ps_codec->i4_disp_strd = ps_codec->i4_strd;
1696             }
1697         }
1698     }
1699 
1700     ps_codec->i4_sps_done = 1;
1701     return ret;
1702 }
1703 
1704 
ihevcd_unmark_pps(codec_t * ps_codec,WORD32 sps_id)1705 void ihevcd_unmark_pps(codec_t *ps_codec, WORD32 sps_id)
1706 {
1707     WORD32 pps_id = 0;
1708     pps_t *ps_pps = ps_codec->ps_pps_base;
1709 
1710     for(pps_id = 0; pps_id < MAX_PPS_CNT - 1; pps_id++, ps_pps++)
1711     {
1712         if((ps_pps->i1_pps_valid) &&
1713                         (ps_pps->i1_sps_id == sps_id))
1714             ps_pps->i1_pps_valid = 0;
1715     }
1716 }
1717 
1718 
ihevcd_copy_sps(codec_t * ps_codec,WORD32 sps_id,WORD32 sps_id_ref)1719 void ihevcd_copy_sps(codec_t *ps_codec, WORD32 sps_id, WORD32 sps_id_ref)
1720 {
1721     sps_t *ps_sps, *ps_sps_ref;
1722     WORD16 *pi2_scaling_mat_backup;
1723     WORD32 scaling_mat_size;
1724 
1725     SCALING_MAT_SIZE(scaling_mat_size);
1726     ps_sps_ref = ps_codec->ps_sps_base + sps_id_ref;
1727     ps_sps = ps_codec->ps_sps_base + sps_id;
1728 
1729     if(ps_sps->i1_sps_valid)
1730     {
1731         if((ps_sps->i1_log2_ctb_size != ps_sps_ref->i1_log2_ctb_size) ||
1732                         (ps_sps->i2_pic_wd_in_ctb != ps_sps_ref->i2_pic_wd_in_ctb) ||
1733                         (ps_sps->i2_pic_ht_in_ctb != ps_sps_ref->i2_pic_ht_in_ctb))
1734         {
1735             ihevcd_unmark_pps(ps_codec, sps_id);
1736         }
1737     }
1738 
1739     pi2_scaling_mat_backup = ps_sps->pi2_scaling_mat;
1740 
1741     memcpy(ps_sps, ps_sps_ref, sizeof(sps_t));
1742     ps_sps->pi2_scaling_mat = pi2_scaling_mat_backup;
1743     memcpy(ps_sps->pi2_scaling_mat, ps_sps_ref->pi2_scaling_mat, scaling_mat_size * sizeof(WORD16));
1744     ps_sps->i1_sps_valid = 1;
1745 
1746     ps_codec->s_parse.ps_sps = ps_sps;
1747 }
1748 
1749 
1750 /**
1751 *******************************************************************************
1752 *
1753 * @brief
1754 *  Parses PPS (Picture Parameter Set)
1755 *
1756 * @par Description:
1757 *  Parse Picture Parameter Set as per section  Section: 7.3.2.3
1758 * The pps is written to a temporary buffer and copied later to the
1759 * appropriate location
1760 *
1761 * @param[in] ps_codec
1762 *  Pointer to codec context
1763 *
1764 * @returns Error code from IHEVCD_ERROR_T
1765 *
1766 * @remarks
1767 *
1768 *
1769 *******************************************************************************
1770 */
ihevcd_parse_pps(codec_t * ps_codec)1771 IHEVCD_ERROR_T ihevcd_parse_pps(codec_t *ps_codec)
1772 {
1773     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
1774     WORD32 value;
1775     WORD32 pps_id;
1776 
1777     pps_t *ps_pps;
1778     sps_t *ps_sps;
1779     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
1780 
1781 
1782     if(0 == ps_codec->i4_sps_done)
1783         return IHEVCD_INVALID_HEADER;
1784 
1785     UEV_PARSE("pic_parameter_set_id", value, ps_bitstrm);
1786 
1787     pps_id = value;
1788     if((pps_id >= MAX_PPS_CNT) || (pps_id < 0))
1789     {
1790         if(ps_codec->i4_pps_done)
1791             return IHEVCD_UNSUPPORTED_PPS_ID;
1792         else
1793             pps_id = 0;
1794     }
1795 
1796 
1797     ps_pps = (ps_codec->s_parse.ps_pps_base + MAX_PPS_CNT - 1);
1798 
1799     ps_pps->i1_pps_id = pps_id;
1800 
1801     UEV_PARSE("seq_parameter_set_id", value, ps_bitstrm);
1802     ps_pps->i1_sps_id = value;
1803     ps_pps->i1_sps_id = CLIP3(ps_pps->i1_sps_id, 0, MAX_SPS_CNT - 2);
1804 
1805     ps_sps = (ps_codec->s_parse.ps_sps_base + ps_pps->i1_sps_id);
1806 
1807     /* If the SPS that is being referred to has not been parsed,
1808      * copy an existing SPS to the current location */
1809     if(0 == ps_sps->i1_sps_valid)
1810     {
1811         return IHEVCD_INVALID_HEADER;
1812 
1813 /*
1814         sps_t *ps_sps_ref = ps_codec->ps_sps_base;
1815         while(0 == ps_sps_ref->i1_sps_valid)
1816             ps_sps_ref++;
1817         ihevcd_copy_sps(ps_codec, ps_pps->i1_sps_id, ps_sps_ref->i1_sps_id);
1818 */
1819     }
1820 
1821     BITS_PARSE("dependent_slices_enabled_flag", value, ps_bitstrm, 1);
1822     ps_pps->i1_dependent_slice_enabled_flag = value;
1823 
1824     BITS_PARSE("output_flag_present_flag", value, ps_bitstrm, 1);
1825     ps_pps->i1_output_flag_present_flag = value;
1826 
1827     BITS_PARSE("num_extra_slice_header_bits", value, ps_bitstrm, 3);
1828     ps_pps->i1_num_extra_slice_header_bits = value;
1829 
1830 
1831     BITS_PARSE("sign_data_hiding_flag", value, ps_bitstrm, 1);
1832     ps_pps->i1_sign_data_hiding_flag = value;
1833 
1834     BITS_PARSE("cabac_init_present_flag", value, ps_bitstrm, 1);
1835     ps_pps->i1_cabac_init_present_flag = value;
1836 
1837     UEV_PARSE("num_ref_idx_l0_default_active_minus1", value, ps_bitstrm);
1838     ps_pps->i1_num_ref_idx_l0_default_active = value + 1;
1839 
1840     UEV_PARSE("num_ref_idx_l1_default_active_minus1", value, ps_bitstrm);
1841     ps_pps->i1_num_ref_idx_l1_default_active = value + 1;
1842 
1843     SEV_PARSE("pic_init_qp_minus26", value, ps_bitstrm);
1844     ps_pps->i1_pic_init_qp = value + 26;
1845 
1846     BITS_PARSE("constrained_intra_pred_flag", value, ps_bitstrm, 1);
1847     ps_pps->i1_constrained_intra_pred_flag = value;
1848 
1849     BITS_PARSE("transform_skip_enabled_flag", value, ps_bitstrm, 1);
1850     ps_pps->i1_transform_skip_enabled_flag = value;
1851 
1852     BITS_PARSE("cu_qp_delta_enabled_flag", value, ps_bitstrm, 1);
1853     ps_pps->i1_cu_qp_delta_enabled_flag = value;
1854 
1855     if(ps_pps->i1_cu_qp_delta_enabled_flag)
1856     {
1857         UEV_PARSE("diff_cu_qp_delta_depth", value, ps_bitstrm);
1858         ps_pps->i1_diff_cu_qp_delta_depth = value;
1859     }
1860     else
1861     {
1862         ps_pps->i1_diff_cu_qp_delta_depth = 0;
1863     }
1864     ps_pps->i1_log2_min_cu_qp_delta_size = ps_sps->i1_log2_ctb_size - ps_pps->i1_diff_cu_qp_delta_depth;
1865     /* Print different */
1866     SEV_PARSE("cb_qp_offset", value, ps_bitstrm);
1867     ps_pps->i1_pic_cb_qp_offset = value;
1868 
1869     /* Print different */
1870     SEV_PARSE("cr_qp_offset", value, ps_bitstrm);
1871     ps_pps->i1_pic_cr_qp_offset = value;
1872 
1873     /* Print different */
1874     BITS_PARSE("slicelevel_chroma_qp_flag", value, ps_bitstrm, 1);
1875     ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag = value;
1876 
1877     BITS_PARSE("weighted_pred_flag", value, ps_bitstrm, 1);
1878     ps_pps->i1_weighted_pred_flag = value;
1879 
1880     BITS_PARSE("weighted_bipred_flag", value, ps_bitstrm, 1);
1881     ps_pps->i1_weighted_bipred_flag = value;
1882 
1883     BITS_PARSE("transquant_bypass_enable_flag", value, ps_bitstrm, 1);
1884     ps_pps->i1_transquant_bypass_enable_flag = value;
1885 
1886     BITS_PARSE("tiles_enabled_flag", value, ps_bitstrm, 1);
1887     ps_pps->i1_tiles_enabled_flag = value;
1888 
1889     /* When tiles are enabled and width or height is >= 4096,
1890      * CTB Size should at least be 32. 16x16 CTBs can result
1891      * in tile position greater than 255 for 4096,
1892      * which decoder does not support.
1893      */
1894     if((ps_pps->i1_tiles_enabled_flag) &&
1895                     (ps_sps->i1_log2_ctb_size == 4) &&
1896                     ((ps_sps->i2_pic_width_in_luma_samples >= 4096) ||
1897                     (ps_sps->i2_pic_height_in_luma_samples >= 4096)))
1898     {
1899         return IHEVCD_INVALID_HEADER;
1900     }
1901 
1902     BITS_PARSE("entropy_coding_sync_enabled_flag", value, ps_bitstrm, 1);
1903     ps_pps->i1_entropy_coding_sync_enabled_flag = value;
1904 
1905     ps_pps->i1_loop_filter_across_tiles_enabled_flag = 0;
1906     if(ps_pps->i1_tiles_enabled_flag)
1907     {
1908         WORD32 wd = ALIGN64(ps_codec->i4_wd);
1909         WORD32 ht = ALIGN64(ps_codec->i4_ht);
1910 
1911         WORD32 max_tile_cols = (wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
1912         WORD32 max_tile_rows = (ht + MIN_TILE_HT - 1) / MIN_TILE_HT;
1913 
1914         UEV_PARSE("num_tile_columns_minus1", value, ps_bitstrm);
1915         ps_pps->i1_num_tile_columns = value + 1;
1916 
1917         UEV_PARSE("num_tile_rows_minus1", value, ps_bitstrm);
1918         ps_pps->i1_num_tile_rows = value + 1;
1919 
1920         if((ps_pps->i1_num_tile_columns < 1) ||
1921                         (ps_pps->i1_num_tile_columns > max_tile_cols) ||
1922                         (ps_pps->i1_num_tile_rows < 1) ||
1923                         (ps_pps->i1_num_tile_rows > max_tile_rows))
1924             return IHEVCD_INVALID_HEADER;
1925 
1926         BITS_PARSE("uniform_spacing_flag", value, ps_bitstrm, 1);
1927         ps_pps->i1_uniform_spacing_flag = value;
1928 
1929 
1930         {
1931 
1932             WORD32 start;
1933             WORD32 i, j;
1934 
1935 
1936             start = 0;
1937             for(i = 0; i < ps_pps->i1_num_tile_columns; i++)
1938             {
1939                 tile_t *ps_tile;
1940                 if(!ps_pps->i1_uniform_spacing_flag)
1941                 {
1942                     if(i < (ps_pps->i1_num_tile_columns - 1))
1943                     {
1944                         UEV_PARSE("column_width_minus1[ i ]", value, ps_bitstrm);
1945                         value += 1;
1946                     }
1947                     else
1948                     {
1949                         value = ps_sps->i2_pic_wd_in_ctb - start;
1950                     }
1951                 }
1952                 else
1953                 {
1954                     value = ((i + 1) * ps_sps->i2_pic_wd_in_ctb) / ps_pps->i1_num_tile_columns -
1955                                     (i * ps_sps->i2_pic_wd_in_ctb) / ps_pps->i1_num_tile_columns;
1956                 }
1957 
1958                 for(j = 0; j < ps_pps->i1_num_tile_rows; j++)
1959                 {
1960                     ps_tile = ps_pps->ps_tile + j * ps_pps->i1_num_tile_columns + i;
1961                     ps_tile->u1_pos_x = start;
1962                     ps_tile->u2_wd = value;
1963                 }
1964                 start += value;
1965 
1966                 if((start > ps_sps->i2_pic_wd_in_ctb) ||
1967                                 (value <= 0))
1968                     return IHEVCD_INVALID_HEADER;
1969             }
1970 
1971             start = 0;
1972             for(i = 0; i < (ps_pps->i1_num_tile_rows); i++)
1973             {
1974                 tile_t *ps_tile;
1975                 if(!ps_pps->i1_uniform_spacing_flag)
1976                 {
1977                     if(i < (ps_pps->i1_num_tile_rows - 1))
1978                     {
1979 
1980                         UEV_PARSE("row_height_minus1[ i ]", value, ps_bitstrm);
1981                         value += 1;
1982                     }
1983                     else
1984                     {
1985                         value = ps_sps->i2_pic_ht_in_ctb - start;
1986                     }
1987                 }
1988                 else
1989                 {
1990                     value = ((i + 1) * ps_sps->i2_pic_ht_in_ctb) / ps_pps->i1_num_tile_rows -
1991                                     (i * ps_sps->i2_pic_ht_in_ctb) / ps_pps->i1_num_tile_rows;
1992                 }
1993 
1994                 for(j = 0; j < ps_pps->i1_num_tile_columns; j++)
1995                 {
1996                     ps_tile = ps_pps->ps_tile + i * ps_pps->i1_num_tile_columns + j;
1997                     ps_tile->u1_pos_y = start;
1998                     ps_tile->u2_ht = value;
1999                 }
2000                 start += value;
2001 
2002                 if((start > ps_sps->i2_pic_ht_in_ctb) ||
2003                                 (value <= 0))
2004                     return IHEVCD_INVALID_HEADER;
2005             }
2006         }
2007 
2008 
2009         BITS_PARSE("loop_filter_across_tiles_enabled_flag", value, ps_bitstrm, 1);
2010         ps_pps->i1_loop_filter_across_tiles_enabled_flag = value;
2011 
2012     }
2013     else
2014     {
2015         /* If tiles are not present, set first tile in each PPS to have tile
2016         width and height equal to picture width and height */
2017         ps_pps->i1_num_tile_columns = 1;
2018         ps_pps->i1_num_tile_rows = 1;
2019         ps_pps->i1_uniform_spacing_flag = 1;
2020 
2021         ps_pps->ps_tile->u1_pos_x = 0;
2022         ps_pps->ps_tile->u1_pos_y = 0;
2023         ps_pps->ps_tile->u2_wd = ps_sps->i2_pic_wd_in_ctb;
2024         ps_pps->ps_tile->u2_ht = ps_sps->i2_pic_ht_in_ctb;
2025     }
2026 
2027     BITS_PARSE("loop_filter_across_slices_enabled_flag", value, ps_bitstrm, 1);
2028     ps_pps->i1_loop_filter_across_slices_enabled_flag = value;
2029 
2030     BITS_PARSE("deblocking_filter_control_present_flag", value, ps_bitstrm, 1);
2031     ps_pps->i1_deblocking_filter_control_present_flag = value;
2032 
2033     /* Default values */
2034     ps_pps->i1_pic_disable_deblocking_filter_flag = 0;
2035     ps_pps->i1_deblocking_filter_override_enabled_flag = 0;
2036     ps_pps->i1_beta_offset_div2 = 0;
2037     ps_pps->i1_tc_offset_div2 = 0;
2038 
2039     if(ps_pps->i1_deblocking_filter_control_present_flag)
2040     {
2041 
2042         BITS_PARSE("deblocking_filter_override_enabled_flag", value, ps_bitstrm, 1);
2043         ps_pps->i1_deblocking_filter_override_enabled_flag = value;
2044 
2045         BITS_PARSE("pic_disable_deblocking_filter_flag", value, ps_bitstrm, 1);
2046         ps_pps->i1_pic_disable_deblocking_filter_flag = value;
2047 
2048         if(!ps_pps->i1_pic_disable_deblocking_filter_flag)
2049         {
2050 
2051             SEV_PARSE("pps_beta_offset_div2", value, ps_bitstrm);
2052             ps_pps->i1_beta_offset_div2 = value;
2053 
2054             SEV_PARSE("pps_tc_offset_div2", value, ps_bitstrm);
2055             ps_pps->i1_tc_offset_div2 = value;
2056 
2057         }
2058     }
2059 
2060     BITS_PARSE("pps_scaling_list_data_present_flag", value, ps_bitstrm, 1);
2061     ps_pps->i1_pps_scaling_list_data_present_flag = value;
2062 
2063     if(ps_pps->i1_pps_scaling_list_data_present_flag)
2064     {
2065         COPY_DEFAULT_SCALING_LIST(ps_pps->pi2_scaling_mat);
2066         ihevcd_scaling_list_data(ps_codec, ps_pps->pi2_scaling_mat);
2067     }
2068 
2069     BITS_PARSE("lists_modification_present_flag", value, ps_bitstrm, 1);
2070     ps_pps->i1_lists_modification_present_flag = value;
2071     UEV_PARSE("log2_parallel_merge_level_minus2", value, ps_bitstrm);
2072     ps_pps->i1_log2_parallel_merge_level = value + 2;
2073 
2074     BITS_PARSE("slice_header_extension_present_flag", value, ps_bitstrm, 1);
2075     ps_pps->i1_slice_header_extension_present_flag = value;
2076     /* Not present in HM */
2077     BITS_PARSE("pps_extension_flag", value, ps_bitstrm, 1);
2078 
2079     if((UWORD8 *)ps_bitstrm->pu4_buf > ps_bitstrm->pu1_buf_max)
2080         return IHEVCD_INVALID_PARAMETER;
2081 
2082     ps_codec->i4_pps_done = 1;
2083     return ret;
2084 }
2085 
2086 
ihevcd_copy_pps(codec_t * ps_codec,WORD32 pps_id,WORD32 pps_id_ref)2087 void ihevcd_copy_pps(codec_t *ps_codec, WORD32 pps_id, WORD32 pps_id_ref)
2088 {
2089     pps_t *ps_pps, *ps_pps_ref;
2090     WORD16 *pi2_scaling_mat_backup;
2091     WORD32 scaling_mat_size;
2092     tile_t *ps_tile_backup;
2093     WORD32 max_tile_cols, max_tile_rows;
2094     WORD32 wd, ht;
2095     wd = ALIGN64(ps_codec->i4_wd);
2096     ht = ALIGN64(ps_codec->i4_ht);
2097 
2098     SCALING_MAT_SIZE(scaling_mat_size);
2099     max_tile_cols = (wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
2100     max_tile_rows = (ht + MIN_TILE_HT - 1) / MIN_TILE_HT;
2101 
2102     ps_pps_ref = ps_codec->ps_pps_base + pps_id_ref;
2103     ps_pps = ps_codec->ps_pps_base + pps_id;
2104 
2105     pi2_scaling_mat_backup = ps_pps->pi2_scaling_mat;
2106     ps_tile_backup = ps_pps->ps_tile;
2107 
2108     memcpy(ps_pps, ps_pps_ref, sizeof(pps_t));
2109     ps_pps->pi2_scaling_mat = pi2_scaling_mat_backup;
2110     ps_pps->ps_tile = ps_tile_backup;
2111     memcpy(ps_pps->pi2_scaling_mat, ps_pps_ref->pi2_scaling_mat, scaling_mat_size * sizeof(WORD16));
2112     memcpy(ps_pps->ps_tile, ps_pps_ref->ps_tile, max_tile_cols * max_tile_rows * sizeof(tile_t));
2113 
2114     ps_pps->i1_pps_valid = 1;
2115 
2116     ps_codec->s_parse.ps_pps = ps_pps;
2117 }
2118 
2119 
ihevcd_parse_buffering_period_sei(codec_t * ps_codec,sps_t * ps_sps)2120 IHEVCD_ERROR_T ihevcd_parse_buffering_period_sei(codec_t *ps_codec,
2121                                                  sps_t *ps_sps)
2122 {
2123     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2124     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2125     UWORD32 value;
2126     vui_t *ps_vui;
2127     buf_period_sei_params_t *ps_buf_period_sei_params;
2128     UWORD32 i;
2129     hrd_params_t *ps_vui_hdr;
2130     UWORD32 u4_cpb_cnt;
2131 
2132     ps_vui = &ps_sps->s_vui_parameters;
2133     ps_vui_hdr = &ps_vui->s_vui_hrd_parameters;
2134 
2135     ps_buf_period_sei_params = &ps_parse->s_sei_params.s_buf_period_sei_params;
2136 
2137     ps_parse->s_sei_params.i1_buf_period_params_present_flag = 1;
2138 
2139     UEV_PARSE("bp_seq_parameter_set_id", value, ps_bitstrm);
2140     ps_buf_period_sei_params->u1_bp_seq_parameter_set_id = value;
2141 
2142     if(!ps_vui_hdr->u1_sub_pic_cpb_params_present_flag)
2143     {
2144         BITS_PARSE("irap_cpb_params_present_flag", value, ps_bitstrm, 1);
2145         ps_buf_period_sei_params->u1_rap_cpb_params_present_flag = value;
2146     }
2147 
2148     if(ps_buf_period_sei_params->u1_rap_cpb_params_present_flag)
2149     {
2150         BITS_PARSE("cpb_delay_offset",
2151                    value,
2152                    ps_bitstrm,
2153                    (ps_vui_hdr->u1_au_cpb_removal_delay_length_minus1
2154                                    + 1));
2155         ps_buf_period_sei_params->u4_cpb_delay_offset = value;
2156 
2157         BITS_PARSE("dpb_delay_offset",
2158                    value,
2159                    ps_bitstrm,
2160                    (ps_vui_hdr->u1_dpb_output_delay_length_minus1
2161                                    + 1));
2162         ps_buf_period_sei_params->u4_dpb_delay_offset = value;
2163     }
2164     else
2165     {
2166         ps_buf_period_sei_params->u4_cpb_delay_offset = 0;
2167         ps_buf_period_sei_params->u4_dpb_delay_offset = 0;
2168     }
2169 
2170     BITS_PARSE("concatenation_flag", value, ps_bitstrm, 1);
2171     ps_buf_period_sei_params->u1_concatenation_flag = value;
2172 
2173     BITS_PARSE("au_cpb_removal_delay_delta_minus1",
2174                value,
2175                ps_bitstrm,
2176                (ps_vui_hdr->u1_au_cpb_removal_delay_length_minus1
2177                                + 1));
2178     ps_buf_period_sei_params->u4_au_cpb_removal_delay_delta_minus1 = value;
2179 
2180     if(ps_vui_hdr->u1_nal_hrd_parameters_present_flag)
2181     {
2182         u4_cpb_cnt = ps_vui_hdr->au1_cpb_cnt_minus1[0];
2183 
2184         for(i = 0; i <= u4_cpb_cnt; i++)
2185         {
2186             BITS_PARSE("nal_initial_cpb_removal_delay[i]",
2187                        value,
2188                        ps_bitstrm,
2189                        (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2190                                        + 1));
2191             ps_buf_period_sei_params->au4_nal_initial_cpb_removal_delay[i] =
2192                             value;
2193 
2194             BITS_PARSE("nal_initial_cpb_removal_delay_offset",
2195                        value,
2196                        ps_bitstrm,
2197                        (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2198                                        + 1));
2199             ps_buf_period_sei_params->au4_nal_initial_cpb_removal_delay_offset[i] =
2200                             value;
2201 
2202             if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag
2203                             || ps_buf_period_sei_params->u1_rap_cpb_params_present_flag)
2204             {
2205                 BITS_PARSE("nal_initial_alt_cpb_removal_delay[i]",
2206                            value,
2207                            ps_bitstrm,
2208                            (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2209                                            + 1));
2210                 ps_buf_period_sei_params->au4_nal_initial_alt_cpb_removal_delay[i] =
2211                                 value;
2212 
2213                 BITS_PARSE("nal_initial_alt_cpb_removal_delay_offset",
2214                            value,
2215                            ps_bitstrm,
2216                            (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2217                                            + 1));
2218                 ps_buf_period_sei_params->au4_nal_initial_alt_cpb_removal_delay_offset[i] =
2219                                 value;
2220             }
2221         }
2222     }
2223 
2224     if(ps_vui_hdr->u1_vcl_hrd_parameters_present_flag)
2225     {
2226         u4_cpb_cnt = ps_vui_hdr->au1_cpb_cnt_minus1[0];
2227 
2228         for(i = 0; i <= u4_cpb_cnt; i++)
2229         {
2230             BITS_PARSE("vcl_initial_cpb_removal_delay[i]",
2231                        value,
2232                        ps_bitstrm,
2233                        (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2234                                        + 1));
2235             ps_buf_period_sei_params->au4_vcl_initial_cpb_removal_delay[i] =
2236                             value;
2237 
2238             BITS_PARSE("vcl_initial_cpb_removal_delay_offset",
2239                        value,
2240                        ps_bitstrm,
2241                        (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2242                                        + 1));
2243             ps_buf_period_sei_params->au4_vcl_initial_cpb_removal_delay_offset[i] =
2244                             value;
2245 
2246             if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag
2247                             || ps_buf_period_sei_params->u1_rap_cpb_params_present_flag)
2248             {
2249                 BITS_PARSE("vcl_initial_alt_cpb_removal_delay[i]",
2250                            value,
2251                            ps_bitstrm,
2252                            (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2253                                            + 1));
2254                 ps_buf_period_sei_params->au4_vcl_initial_alt_cpb_removal_delay[i] =
2255                                 value;
2256 
2257                 BITS_PARSE("vcl_initial_alt_cpb_removal_delay_offset",
2258                            value,
2259                            ps_bitstrm,
2260                            (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2261                                            + 1));
2262                 ps_buf_period_sei_params->au4_vcl_initial_alt_cpb_removal_delay_offset[i] =
2263                                 value;
2264             }
2265         }
2266     }
2267 
2268     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2269 }
2270 
ihevcd_parse_pic_timing_sei(codec_t * ps_codec,sps_t * ps_sps)2271 IHEVCD_ERROR_T ihevcd_parse_pic_timing_sei(codec_t *ps_codec, sps_t *ps_sps)
2272 {
2273     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2274     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2275     UWORD32 value;
2276     vui_t *ps_vui;
2277     UWORD32 i;
2278     hrd_params_t *ps_vui_hdr;
2279     UWORD32 u4_cpb_dpb_delays_present_flag = 0;
2280     pic_timing_sei_params_t *ps_pic_timing;
2281 
2282     ps_pic_timing = &ps_parse->s_sei_params.s_pic_timing_sei_params;
2283     ps_vui = &ps_sps->s_vui_parameters;
2284     ps_vui_hdr = &ps_vui->s_vui_hrd_parameters;
2285     ps_parse->s_sei_params.i1_pic_timing_params_present_flag = 1;
2286     if(ps_vui->u1_frame_field_info_present_flag)
2287     {
2288         BITS_PARSE("pic_struct", value, ps_bitstrm, 4);
2289         ps_pic_timing->u4_pic_struct = value;
2290 
2291         BITS_PARSE("source_scan_type", value, ps_bitstrm, 2);
2292         ps_pic_timing->u4_source_scan_type = value;
2293 
2294         BITS_PARSE("duplicate_flag", value, ps_bitstrm, 1);
2295         ps_pic_timing->u1_duplicate_flag = value;
2296     }
2297 
2298     if(ps_vui_hdr->u1_nal_hrd_parameters_present_flag
2299                     || ps_vui_hdr->u1_vcl_hrd_parameters_present_flag)
2300     {
2301         u4_cpb_dpb_delays_present_flag = 1;
2302     }
2303     else
2304     {
2305         u4_cpb_dpb_delays_present_flag = 0;
2306     }
2307 
2308     if(u4_cpb_dpb_delays_present_flag)
2309     {
2310         BITS_PARSE("au_cpb_removal_delay_minus1", value, ps_bitstrm,
2311                    (ps_vui_hdr->u1_au_cpb_removal_delay_length_minus1 + 1));
2312         ps_pic_timing->u4_au_cpb_removal_delay_minus1 = value;
2313 
2314         BITS_PARSE("pic_dpb_output_delay", value, ps_bitstrm,
2315                    (ps_vui_hdr->u1_dpb_output_delay_length_minus1 + 1));
2316         ps_pic_timing->u4_pic_dpb_output_delay = value;
2317 
2318         if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag)
2319         {
2320             BITS_PARSE("pic_dpb_output_du_delay", value, ps_bitstrm,
2321                        (ps_vui_hdr->u1_dpb_output_delay_du_length_minus1 + 1));
2322             ps_pic_timing->u4_pic_dpb_output_du_delay = value;
2323         }
2324 
2325         if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag
2326                         && ps_vui_hdr->u1_sub_pic_cpb_params_in_pic_timing_sei_flag)
2327         {
2328             UEV_PARSE("num_decoding_units_minus1", value, ps_bitstrm);
2329             ps_pic_timing->u4_num_decoding_units_minus1 = value;
2330 
2331             BITS_PARSE("du_common_cpb_removal_delay_flag", value, ps_bitstrm, 1);
2332             ps_pic_timing->u1_du_common_cpb_removal_delay_flag = value;
2333 
2334             if(ps_pic_timing->u1_du_common_cpb_removal_delay_flag)
2335             {
2336                 BITS_PARSE("du_common_cpb_removal_delay_increment_minus1",
2337                            value,
2338                            ps_bitstrm,
2339                            (ps_vui_hdr->u1_du_cpb_removal_delay_increment_length_minus1
2340                                            + 1));
2341                 ps_pic_timing->u4_du_common_cpb_removal_delay_increment_minus1 =
2342                                 value;
2343             }
2344 
2345             for(i = 0; i <= ps_pic_timing->u4_num_decoding_units_minus1; i++)
2346             {
2347                 UEV_PARSE("num_nalus_in_du_minus1", value, ps_bitstrm);
2348                 ps_pic_timing->au4_num_nalus_in_du_minus1[i] = value;
2349 
2350                 if((!ps_pic_timing->u1_du_common_cpb_removal_delay_flag)
2351                                 && (i < ps_pic_timing->u4_num_decoding_units_minus1))
2352                 {
2353                     BITS_PARSE("du_common_cpb_removal_delay_increment_minus1",
2354                                value,
2355                                ps_bitstrm,
2356                                (ps_vui_hdr->u1_du_cpb_removal_delay_increment_length_minus1
2357                                                + 1));
2358                     ps_pic_timing->au4_du_cpb_removal_delay_increment_minus1[i] =
2359                                     value;
2360                 }
2361             }
2362         }
2363     }
2364 
2365     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2366 }
2367 
ihevcd_parse_time_code_sei(codec_t * ps_codec)2368 IHEVCD_ERROR_T ihevcd_parse_time_code_sei(codec_t *ps_codec)
2369 {
2370     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2371     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2372     UWORD32 value;
2373     time_code_t *ps_time_code;
2374     WORD32 i;
2375 
2376     ps_parse->s_sei_params.i1_time_code_present_flag = 1;
2377     ps_time_code = &ps_parse->s_sei_params.s_time_code;
2378 
2379     BITS_PARSE("num_clock_ts", value, ps_bitstrm, 2);
2380     ps_time_code->u1_num_clock_ts = value;
2381 
2382     for(i = 0; i < ps_time_code->u1_num_clock_ts; i++)
2383     {
2384         BITS_PARSE("clock_timestamp_flag[i]", value, ps_bitstrm, 1);
2385         ps_time_code->au1_clock_timestamp_flag[i] = value;
2386 
2387         if(ps_time_code->au1_clock_timestamp_flag[i])
2388         {
2389             BITS_PARSE("units_field_based_flag[i]", value, ps_bitstrm, 1);
2390             ps_time_code->au1_units_field_based_flag[i] = value;
2391 
2392             BITS_PARSE("counting_type[i]", value, ps_bitstrm, 5);
2393             ps_time_code->au1_counting_type[i] = value;
2394 
2395             BITS_PARSE("full_timestamp_flag[i]", value, ps_bitstrm, 1);
2396             ps_time_code->au1_full_timestamp_flag[i] = value;
2397 
2398             BITS_PARSE("discontinuity_flag[i]", value, ps_bitstrm, 1);
2399             ps_time_code->au1_discontinuity_flag[i] = value;
2400 
2401             BITS_PARSE("cnt_dropped_flag[i]", value, ps_bitstrm, 1);
2402             ps_time_code->au1_cnt_dropped_flag[i] = value;
2403 
2404             BITS_PARSE("n_frames[i]", value, ps_bitstrm, 9);
2405             ps_time_code->au2_n_frames[i] = value;
2406 
2407             if(ps_time_code->au1_full_timestamp_flag[i])
2408             {
2409                 BITS_PARSE("seconds_value[i]", value, ps_bitstrm, 6);
2410                 ps_time_code->au1_seconds_value[i] = value;
2411 
2412                 BITS_PARSE("minutes_value[i]", value, ps_bitstrm, 6);
2413                 ps_time_code->au1_minutes_value[i] = value;
2414 
2415                 BITS_PARSE("hours_value[i]", value, ps_bitstrm, 5);
2416                 ps_time_code->au1_hours_value[i] = value;
2417             }
2418             else
2419             {
2420                 BITS_PARSE("seconds_flag[i]", value, ps_bitstrm, 1);
2421                 ps_time_code->au1_seconds_flag[i] = value;
2422 
2423                 if(ps_time_code->au1_seconds_flag[i])
2424                 {
2425                     BITS_PARSE("seconds_value[i]", value, ps_bitstrm, 6);
2426                     ps_time_code->au1_seconds_value[i] = value;
2427 
2428                     BITS_PARSE("minutes_flag[i]", value, ps_bitstrm, 1);
2429                     ps_time_code->au1_minutes_flag[i] = value;
2430 
2431                     if(ps_time_code->au1_minutes_flag[i])
2432                     {
2433                         BITS_PARSE("minutes_value[i]", value, ps_bitstrm, 6);
2434                         ps_time_code->au1_minutes_value[i] = value;
2435 
2436                         BITS_PARSE("hours_flag[i]", value, ps_bitstrm, 1);
2437                         ps_time_code->au1_hours_flag[i] = value;
2438 
2439                         if(ps_time_code->au1_hours_flag[i])
2440                         {
2441                             BITS_PARSE("hours_value[i]", value, ps_bitstrm, 5);
2442                             ps_time_code->au1_hours_value[i] = value;
2443                         }
2444                     }
2445                 }
2446             }
2447 
2448             BITS_PARSE("time_offset_length[i]", value, ps_bitstrm, 5);
2449             ps_time_code->au1_time_offset_length[i] = value;
2450 
2451             if(ps_time_code->au1_time_offset_length[i] > 0)
2452             {
2453                 BITS_PARSE("time_offset_value[i]", value, ps_bitstrm,
2454                            ps_time_code->au1_time_offset_length[i]);
2455                 ps_time_code->au1_time_offset_value[i] = value;
2456             }
2457             else
2458             {
2459                 ps_time_code->au1_time_offset_value[i] = 0;
2460             }
2461         }
2462     }
2463 
2464     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2465 }
2466 
ihevcd_parse_mastering_disp_params_sei(codec_t * ps_codec)2467 IHEVCD_ERROR_T ihevcd_parse_mastering_disp_params_sei(codec_t *ps_codec)
2468 {
2469     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2470     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2471     UWORD32 value;
2472     mastering_dis_col_vol_sei_params_t *ps_mastering_dis_col_vol;
2473     WORD32 i;
2474 
2475     ps_parse->s_sei_params.i4_sei_mastering_disp_colour_vol_params_present_flags = 1;
2476 
2477     ps_mastering_dis_col_vol = &ps_parse->s_sei_params.s_mastering_dis_col_vol_sei_params;
2478 
2479     for(i = 0; i < 3; i++)
2480     {
2481         BITS_PARSE("display_primaries_x[c]", value, ps_bitstrm, 16);
2482         ps_mastering_dis_col_vol->au2_display_primaries_x[i] = value;
2483 
2484         BITS_PARSE("display_primaries_y[c]", value, ps_bitstrm, 16);
2485         ps_mastering_dis_col_vol->au2_display_primaries_y[i] = value;
2486     }
2487 
2488     BITS_PARSE("white_point_x", value, ps_bitstrm, 16);
2489     ps_mastering_dis_col_vol->u2_white_point_x = value;
2490 
2491     BITS_PARSE("white_point_y", value, ps_bitstrm, 16);
2492     ps_mastering_dis_col_vol->u2_white_point_y = value;
2493 
2494     BITS_PARSE("max_display_mastering_luminance", value, ps_bitstrm, 32);
2495     ps_mastering_dis_col_vol->u4_max_display_mastering_luminance = value;
2496 
2497     BITS_PARSE("min_display_mastering_luminance", value, ps_bitstrm, 32);
2498     ps_mastering_dis_col_vol->u4_min_display_mastering_luminance = value;
2499 
2500     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2501 }
2502 
ihevcd_parse_user_data_registered_itu_t_t35(codec_t * ps_codec,UWORD32 u4_payload_size)2503 IHEVCD_ERROR_T ihevcd_parse_user_data_registered_itu_t_t35(codec_t *ps_codec,
2504                                                            UWORD32 u4_payload_size)
2505 {
2506     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2507     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2508     UWORD32 value;
2509     user_data_registered_itu_t_t35_t *ps_user_data_registered_itu_t_t35;
2510     UWORD32 i;
2511     UWORD32 j = 0;
2512 
2513     ps_parse->s_sei_params.i1_user_data_registered_present_flag = 1;
2514     ps_user_data_registered_itu_t_t35 =
2515                     &ps_parse->s_sei_params.as_user_data_registered_itu_t_t35[ps_parse->s_sei_params.i4_sei_user_data_cnt];
2516     ps_parse->s_sei_params.i4_sei_user_data_cnt++;
2517 
2518     ps_user_data_registered_itu_t_t35->i4_payload_size = u4_payload_size;
2519 
2520     if(u4_payload_size > MAX_USERDATA_PAYLOAD)
2521     {
2522         u4_payload_size = MAX_USERDATA_PAYLOAD;
2523     }
2524 
2525     ps_user_data_registered_itu_t_t35->i4_valid_payload_size = u4_payload_size;
2526 
2527     BITS_PARSE("itu_t_t35_country_code", value, ps_bitstrm, 8);
2528     ps_user_data_registered_itu_t_t35->u1_itu_t_t35_country_code = value;
2529 
2530     if(0xFF != ps_user_data_registered_itu_t_t35->u1_itu_t_t35_country_code)
2531     {
2532         i = 1;
2533     }
2534     else
2535     {
2536         BITS_PARSE("itu_t_t35_country_code_extension_byte", value, ps_bitstrm,
2537                    8);
2538         ps_user_data_registered_itu_t_t35->u1_itu_t_t35_country_code_extension_byte =
2539                         value;
2540 
2541         i = 2;
2542     }
2543 
2544     do
2545     {
2546         BITS_PARSE("itu_t_t35_payload_byte", value, ps_bitstrm, 8);
2547         ps_user_data_registered_itu_t_t35->u1_itu_t_t35_payload_byte[j++] =
2548                         value;
2549 
2550         i++;
2551     }while(i < u4_payload_size);
2552 
2553     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2554 }
2555 
ihevcd_parse_sei_payload(codec_t * ps_codec,UWORD32 u4_payload_type,UWORD32 u4_payload_size,WORD8 i1_nal_type)2556 void ihevcd_parse_sei_payload(codec_t *ps_codec,
2557                               UWORD32 u4_payload_type,
2558                               UWORD32 u4_payload_size,
2559                               WORD8 i1_nal_type)
2560 {
2561     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2562     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2563     WORD32 payload_bits_remaining = 0;
2564     sps_t *ps_sps;
2565 
2566     UWORD32 i;
2567 
2568     for(i = 0; i < MAX_SPS_CNT; i++)
2569     {
2570         ps_sps = ps_codec->ps_sps_base + i;
2571         if(ps_sps->i1_sps_valid)
2572         {
2573             break;
2574         }
2575     }
2576     if(NULL == ps_sps)
2577     {
2578         return;
2579     }
2580 
2581     if(NAL_PREFIX_SEI == i1_nal_type)
2582     {
2583         switch(u4_payload_type)
2584         {
2585             case SEI_BUFFERING_PERIOD:
2586                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
2587                 ihevcd_parse_buffering_period_sei(ps_codec, ps_sps);
2588                 break;
2589 
2590             case SEI_PICTURE_TIMING:
2591                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
2592                 ihevcd_parse_pic_timing_sei(ps_codec, ps_sps);
2593                 break;
2594 
2595             case SEI_TIME_CODE:
2596                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
2597                 ihevcd_parse_time_code_sei(ps_codec);
2598                 break;
2599 
2600             case SEI_MASTERING_DISPLAY_COLOUR_VOLUME:
2601                 ps_parse->s_sei_params.i4_sei_mastering_disp_colour_vol_params_present_flags = 1;
2602                 ihevcd_parse_mastering_disp_params_sei(ps_codec);
2603                 break;
2604 
2605             case SEI_USER_DATA_REGISTERED_ITU_T_T35:
2606                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
2607                 ihevcd_parse_user_data_registered_itu_t_t35(ps_codec,
2608                                                             u4_payload_size);
2609                 break;
2610 
2611             default:
2612                 for(i = 0; i < u4_payload_size; i++)
2613                 {
2614                     ihevcd_bits_flush(ps_bitstrm, 8);
2615                 }
2616                 break;
2617         }
2618     }
2619     else /* NAL_SUFFIX_SEI */
2620     {
2621         switch(u4_payload_type)
2622         {
2623             case SEI_USER_DATA_REGISTERED_ITU_T_T35:
2624                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
2625                 ihevcd_parse_user_data_registered_itu_t_t35(ps_codec,
2626                                                             u4_payload_size);
2627                 break;
2628 
2629             default:
2630                 for(i = 0; i < u4_payload_size; i++)
2631                 {
2632                     ihevcd_bits_flush(ps_bitstrm, 8);
2633                 }
2634                 break;
2635         }
2636     }
2637 
2638     /**
2639      * By definition the underlying bitstream terminates in a byte-aligned manner.
2640      * 1. Extract all bar the last MIN(bitsremaining,nine) bits as reserved_payload_extension_data
2641      * 2. Examine the final 8 bits to determine the payload_bit_equal_to_one marker
2642      * 3. Extract the remainingreserved_payload_extension_data bits.
2643      *
2644      * If there are fewer than 9 bits available, extract them.
2645      */
2646 
2647     payload_bits_remaining = ihevcd_bits_num_bits_remaining(ps_bitstrm);
2648     if(payload_bits_remaining) /* more_data_in_payload() */
2649     {
2650         WORD32 final_bits;
2651         WORD32 final_payload_bits = 0;
2652         WORD32 mask = 0xFF;
2653         UWORD32 u4_dummy;
2654         UWORD32 u4_reserved_payload_extension_data;
2655         UNUSED(u4_dummy);
2656         UNUSED(u4_reserved_payload_extension_data);
2657 
2658         while(payload_bits_remaining > 9)
2659         {
2660             BITS_PARSE("reserved_payload_extension_data",
2661                        u4_reserved_payload_extension_data, ps_bitstrm, 1);
2662             payload_bits_remaining--;
2663         }
2664 
2665         final_bits = ihevcd_bits_nxt(ps_bitstrm, payload_bits_remaining);
2666 
2667         while(final_bits & (mask >> final_payload_bits))
2668         {
2669             final_payload_bits++;
2670             continue;
2671         }
2672 
2673         while(payload_bits_remaining > (9 - final_payload_bits))
2674         {
2675             BITS_PARSE("reserved_payload_extension_data",
2676                        u4_reserved_payload_extension_data, ps_bitstrm, 1);
2677             payload_bits_remaining--;
2678         }
2679 
2680         BITS_PARSE("payload_bit_equal_to_one", u4_dummy, ps_bitstrm, 1);
2681         payload_bits_remaining--;
2682         while(payload_bits_remaining)
2683         {
2684             BITS_PARSE("payload_bit_equal_to_zero", u4_dummy, ps_bitstrm, 1);
2685             payload_bits_remaining--;
2686         }
2687     }
2688 
2689     return;
2690 }
2691 
ihevcd_read_rbsp_trailing_bits(codec_t * ps_codec,UWORD32 u4_bits_left)2692 IHEVCD_ERROR_T ihevcd_read_rbsp_trailing_bits(codec_t *ps_codec,
2693                                               UWORD32 u4_bits_left)
2694 {
2695     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2696     UWORD32 value;
2697     WORD32 cnt = 0;
2698     BITS_PARSE("rbsp_stop_one_bit", value, &ps_parse->s_bitstrm, 1);
2699     u4_bits_left--;
2700     if(value != 1)
2701     {
2702         return (IHEVCD_ERROR_T)IHEVCD_FAIL;
2703     }
2704     while(u4_bits_left)
2705     {
2706         BITS_PARSE("rbsp_alignment_zero_bit", value, &ps_parse->s_bitstrm, 1);
2707         u4_bits_left--;
2708         cnt++;
2709     }
2710     ASSERT(cnt < 8);
2711 
2712     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2713 }
2714 /**
2715 *******************************************************************************
2716 *
2717 * @brief
2718 *  Parses SEI (Supplemental Enhancement Information)
2719 *
2720 * @par Description:
2721 *  Parses SEI (Supplemental Enhancement Information) as per Section: 7.3.7
2722 *
2723 * @param[in] ps_codec
2724 *  Pointer to codec context
2725 *
2726 * @returns Error code from IHEVCD_ERROR_T
2727 *
2728 * @remarks
2729 *
2730 *
2731 *******************************************************************************
2732 */
ihevcd_parse_sei(codec_t * ps_codec,nal_header_t * ps_nal)2733 IHEVCD_ERROR_T ihevcd_parse_sei(codec_t *ps_codec, nal_header_t *ps_nal)
2734 {
2735     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2736     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2737     UWORD32 u4_payload_type = 0, u4_last_payload_type_byte = 0;
2738     UWORD32 u4_payload_size = 0, u4_last_payload_size_byte = 0;
2739     UWORD32 value;
2740     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2741     UWORD32 u4_bits_left;
2742 
2743     u4_bits_left = ihevcd_bits_num_bits_remaining(ps_bitstrm);
2744 
2745     while(u4_bits_left > 8)
2746     {
2747         while(ihevcd_bits_nxt(ps_bitstrm, 8) == 0xFF)
2748         {
2749             ihevcd_bits_flush(ps_bitstrm, 8); /* equal to 0xFF */
2750             u4_payload_type += 255;
2751         }
2752 
2753         BITS_PARSE("last_payload_type_byte", value, ps_bitstrm, 8);
2754         u4_last_payload_type_byte = value;
2755 
2756         u4_payload_type += u4_last_payload_type_byte;
2757 
2758         while(ihevcd_bits_nxt(ps_bitstrm, 8) == 0xFF)
2759         {
2760             ihevcd_bits_flush(ps_bitstrm, 8); /* equal to 0xFF */
2761             u4_payload_size += 255;
2762         }
2763 
2764         BITS_PARSE("last_payload_size_byte", value, ps_bitstrm, 8);
2765         u4_last_payload_size_byte = value;
2766 
2767         u4_payload_size += u4_last_payload_size_byte;
2768         u4_bits_left = ihevcd_bits_num_bits_remaining(ps_bitstrm);
2769         u4_payload_size = MIN(u4_payload_size, u4_bits_left / 8);
2770         ihevcd_parse_sei_payload(ps_codec, u4_payload_type, u4_payload_size,
2771                                  ps_nal->i1_nal_unit_type);
2772 
2773         /* Calculate the bits left in the current payload */
2774         u4_bits_left = ihevcd_bits_num_bits_remaining(ps_bitstrm);
2775     }
2776 
2777     // read rbsp_trailing_bits
2778     if(u4_bits_left)
2779     {
2780         ihevcd_read_rbsp_trailing_bits(ps_codec, u4_bits_left);
2781     }
2782 
2783     return ret;
2784 }
2785 
2786 /**
2787 *******************************************************************************
2788 *
2789 * @brief
2790 *  Parses Access unit delimiter
2791 *
2792 * @par Description:
2793 *  Parses Access unit delimiter as per section  Section: 7.3.2.5
2794 *
2795 * @param[in] ps_codec
2796 *  Pointer to codec context
2797 *
2798 * @returns Error code from IHEVCD_ERROR_T
2799 *
2800 * @remarks
2801 *
2802 *
2803 *******************************************************************************
2804 */
ihevcd_parse_aud(codec_t * ps_codec)2805 WORD32 ihevcd_parse_aud(codec_t *ps_codec)
2806 {
2807     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2808     UNUSED(ps_codec);
2809     return ret;
2810 }
2811 
ihevcd_extend_sign_bit(WORD32 value,WORD32 num_bits)2812 WORD32 ihevcd_extend_sign_bit(WORD32 value, WORD32 num_bits)
2813 {
2814     WORD32 ret_value = value;
2815     if(value >> (num_bits - 1))
2816     {
2817         ret_value |= (0xFFFFFFFF << num_bits);
2818     }
2819     return ret_value;
2820 }
2821 
2822 /**
2823 *******************************************************************************
2824 *
2825 * @brief
2826 *  Calculate POC of the current slice
2827 *
2828 * @par Description:
2829 *  Calculates the current POC using the previous POC lsb and previous POC msb
2830 *
2831 * @param[in] ps_codec
2832 *  Pointer to codec context
2833 *
2834 * @param[in] i1_pic_order_cnt_lsb
2835 *  Current POC lsb
2836 *
2837 * @returns  Current absolute POC
2838 *
2839 * @remarks
2840 *
2841 *
2842 *******************************************************************************
2843 */
2844 
ihevcd_calc_poc(codec_t * ps_codec,nal_header_t * ps_nal,WORD8 i1_log2_max_poc_lsb,WORD32 i2_poc_lsb)2845 WORD32 ihevcd_calc_poc(codec_t *ps_codec, nal_header_t *ps_nal, WORD8 i1_log2_max_poc_lsb, WORD32 i2_poc_lsb)
2846 {
2847     WORD32 i4_abs_poc, i4_poc_msb;
2848     WORD32 max_poc_lsb;
2849     WORD8 i1_nal_unit_type = ps_nal->i1_nal_unit_type;
2850     max_poc_lsb = (1 << i1_log2_max_poc_lsb);
2851 
2852     if((!ps_codec->i4_first_pic_done) && (!ps_codec->i4_pic_present))
2853         ps_codec->i4_prev_poc_msb = -2 * max_poc_lsb;
2854 
2855     if(NAL_IDR_N_LP == i1_nal_unit_type
2856                     || NAL_IDR_W_LP == i1_nal_unit_type
2857                     || NAL_BLA_N_LP == i1_nal_unit_type
2858                     || NAL_BLA_W_DLP == i1_nal_unit_type
2859                     || NAL_BLA_W_LP == i1_nal_unit_type
2860                     || (NAL_CRA == i1_nal_unit_type && !ps_codec->i4_first_pic_done))
2861     {
2862         i4_poc_msb = ps_codec->i4_prev_poc_msb + 2 * max_poc_lsb;
2863         ps_codec->i4_prev_poc_lsb = 0;
2864         ps_codec->i4_max_prev_poc_lsb = 0;
2865 //        ps_codec->i4_prev_poc_msb = 0;
2866     }
2867     else
2868     {
2869 
2870         if((i2_poc_lsb < ps_codec->i4_prev_poc_lsb)
2871                         && ((ps_codec->i4_prev_poc_lsb - i2_poc_lsb) >= max_poc_lsb / 2))
2872         {
2873             i4_poc_msb = ps_codec->i4_prev_poc_msb + max_poc_lsb;
2874         }
2875         else if((i2_poc_lsb > ps_codec->i4_prev_poc_lsb)
2876                         && ((i2_poc_lsb - ps_codec->i4_prev_poc_lsb) > max_poc_lsb / 2))
2877         {
2878             i4_poc_msb = ps_codec->i4_prev_poc_msb - max_poc_lsb;
2879         }
2880         else
2881         {
2882             i4_poc_msb = ps_codec->i4_prev_poc_msb;
2883         }
2884 
2885 
2886     }
2887 
2888     i4_abs_poc = i4_poc_msb + i2_poc_lsb;
2889     ps_codec->i4_max_prev_poc_lsb = MAX(ps_codec->i4_max_prev_poc_lsb, i2_poc_lsb);
2890 
2891     {
2892         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));
2893         WORD32 update_prev_poc = ((is_reference_nal) && ((i1_nal_unit_type < NAL_RADL_N) || (i1_nal_unit_type > NAL_RASL_R)));
2894 
2895         if((0 == ps_nal->i1_nuh_temporal_id) &&
2896                         (update_prev_poc))
2897         {
2898             ps_codec->i4_prev_poc_lsb = i2_poc_lsb;
2899             ps_codec->i4_prev_poc_msb = i4_poc_msb;
2900         }
2901     }
2902 
2903     return i4_abs_poc;
2904 }
2905 
2906 
ihevcd_copy_slice_hdr(codec_t * ps_codec,WORD32 slice_idx,WORD32 slice_idx_ref)2907 void ihevcd_copy_slice_hdr(codec_t *ps_codec, WORD32 slice_idx, WORD32 slice_idx_ref)
2908 {
2909     slice_header_t *ps_slice_hdr, *ps_slice_hdr_ref;
2910     WORD32 *pu4_entry_offset_backup;
2911 
2912     ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr_base + slice_idx;
2913     ps_slice_hdr_ref = ps_codec->s_parse.ps_slice_hdr_base + slice_idx_ref;
2914 
2915     pu4_entry_offset_backup = ps_slice_hdr->pu4_entry_point_offset;
2916     memcpy(ps_slice_hdr, ps_slice_hdr_ref, sizeof(slice_header_t));
2917     ps_slice_hdr->pu4_entry_point_offset = pu4_entry_offset_backup;
2918 }
2919 
2920 
2921 
2922