Coverage Report

Created: 2025-12-29 07:02

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