Coverage Report

Created: 2025-07-11 06:43

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