Coverage Report

Created: 2025-11-24 06:46

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