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