Coverage Report

Created: 2026-10-03 07:09

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
#include "ihevcd_utils.h"
76
77
43.6k
#define COPY_DEFAULT_SCALING_LIST(pi2_scaling_mat)                                                                                      \
78
43.6k
{                                                                                                                                       \
79
43.6k
    WORD32 scaling_mat_offset[] = {0,   16,  32,  48,   64,   80,   96,   160,  224,  288,  352,  416,                                  \
80
43.6k
                                   480, 736, 992, 1248, 1504, 1760, 2016, 3040, 4064, 5088, 6112, 7136};                                \
81
43.6k
                                                                                                                                        \
82
43.6k
    /* scaling matrix for 4x4 */                                                                                                        \
83
43.6k
    memcpy(pi2_scaling_mat, gi2_flat_scale_mat_32x32, 6*16*sizeof(WORD16));                                                             \
84
43.6k
    /* scaling matrix for 8x8 */                                                                                                        \
85
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[6], gi2_intra_default_scale_mat_8x8, 64*sizeof(WORD16));                                \
86
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[7], gi2_intra_default_scale_mat_8x8, 64*sizeof(WORD16));                                \
87
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[8], gi2_intra_default_scale_mat_8x8, 64*sizeof(WORD16));                                \
88
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[9], gi2_inter_default_scale_mat_8x8, 64*sizeof(WORD16));                                \
89
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[10], gi2_inter_default_scale_mat_8x8, 64*sizeof(WORD16));                               \
90
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[11], gi2_inter_default_scale_mat_8x8, 64*sizeof(WORD16));                               \
91
43.6k
    /* scaling matrix for 16x16 */                                                                                                      \
92
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[12], gi2_intra_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
93
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[13], gi2_intra_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
94
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[14], gi2_intra_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
95
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[15], gi2_inter_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
96
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[16], gi2_inter_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
97
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[17], gi2_inter_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
98
43.6k
    /* scaling matrix for 32x32 */                                                                                                      \
99
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[18], gi2_intra_default_scale_mat_32x32, 1024*sizeof(WORD16));                           \
100
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[19], gi2_intra_default_scale_mat_32x32, 1024*sizeof(WORD16));                           \
101
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[20], gi2_intra_default_scale_mat_32x32, 1024*sizeof(WORD16));                           \
102
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[21], gi2_inter_default_scale_mat_32x32, 1024*sizeof(WORD16));                           \
103
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[22], gi2_inter_default_scale_mat_32x32, 1024*sizeof(WORD16));                           \
104
43.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[23], gi2_inter_default_scale_mat_32x32, 1024*sizeof(WORD16));                           \
105
43.6k
}
106
107
22.6k
#define COPY_FLAT_SCALING_LIST(pi2_scaling_mat)                                                                                         \
108
22.6k
{                                                                                                                                       \
109
22.6k
    WORD32 scaling_mat_offset[] = {0,   16,  32,  48,   64,   80,   96,   160,  224,  288,  352,  416,                                  \
110
22.6k
                                   480, 736, 992, 1248, 1504, 1760, 2016, 3040, 4064, 5088, 6112, 7136};                                \
111
22.6k
                                                                                                                                        \
112
22.6k
    /* scaling matrix for 4x4 */                                                                                                        \
113
22.6k
    memcpy(pi2_scaling_mat, gi2_flat_scale_mat_32x32, 6*16*sizeof(WORD16));                                                             \
114
22.6k
    /* scaling matrix for 8x8 */                                                                                                        \
115
22.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[6], gi2_flat_scale_mat_32x32, 6*64*sizeof(WORD16));                                     \
116
22.6k
    /* scaling matrix for 16x16 */                                                                                                      \
117
22.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[12], gi2_flat_scale_mat_32x32, 3*256*sizeof(WORD16));                                   \
118
22.6k
    memcpy(pi2_scaling_mat + scaling_mat_offset[15], gi2_flat_scale_mat_32x32, 3*256*sizeof(WORD16));                                   \
119
22.6k
    /* scaling matrix for 32x32 */                                                                                                      \
120
158k
    for (WORD32 i = 0; i < 6; i++)                                                                                                      \
121
136k
    {                                                                                                                                   \
122
136k
        memcpy(pi2_scaling_mat + scaling_mat_offset[18 + i], gi2_flat_scale_mat_32x32, 1024*sizeof(WORD16));                            \
123
136k
    }                                                                                                                                   \
124
22.6k
}
125
126
/* Function declarations */
127
128
/**
129
*******************************************************************************
130
*
131
* @brief
132
*  Parses Prediction weight table syntax
133
*
134
* @par Description:
135
*  Parse Prediction weight table syntax as per Section: 7.3.8.4
136
*
137
* @param[in] ps_bitstrm
138
*  Pointer to bitstream context
139
*
140
* @param[in] ps_sps
141
*  Current SPS
142
*
143
* @param[in] ps_pps
144
*  Current PPS
145
*
146
* @param[in] ps_slice_hdr
147
*  Current Slice header
148
*
149
* @returns  Error code from IHEVCD_ERROR_T
150
*
151
* @remarks
152
*
153
*
154
*******************************************************************************
155
*/
156
157
WORD32 ihevcd_parse_pred_wt_ofst(bitstrm_t *ps_bitstrm,
158
                                 sps_t *ps_sps,
159
                                 pps_t *ps_pps,
160
                                 slice_header_t *ps_slice_hdr)
161
19.6k
{
162
19.6k
    IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
163
19.6k
    WORD32 value;
164
19.6k
    UWORD32 u4_value;
165
19.6k
    WORD32 i;
166
167
19.6k
    WORD32 wp_ofst_half_rng_y;
168
19.6k
    WORD32 wp_ofst_half_rng_c;
169
170
19.6k
    pred_wt_ofst_t *ps_wt_ofst = &ps_slice_hdr->s_wt_ofst;
171
19.6k
    UNUSED(ps_pps);
172
173
19.6k
    wp_ofst_half_rng_y = 1 << (ps_sps->i1_use_high_precision_pred_wt ?
174
19.0k
        (ps_sps->i1_bit_depth_luma_minus8 + 8 - 1) : 7);
175
19.6k
    wp_ofst_half_rng_c = 1 << (ps_sps->i1_use_high_precision_pred_wt ?
176
19.0k
        (ps_sps->i1_bit_depth_chroma_minus8 + 8 - 1) : 7);
177
178
19.6k
    UEV_PARSE("luma_log2_weight_denom", u4_value, ps_bitstrm);
179
19.6k
    if(u4_value > 7)
180
3.63k
    {
181
3.63k
        return IHEVCD_INVALID_PARAMETER;
182
3.63k
    }
183
16.0k
    ps_wt_ofst->i1_luma_log2_weight_denom = u4_value;
184
185
16.0k
    if(ps_sps->i1_chroma_format_idc != CHROMA_FMT_IDC_MONOCHROME)
186
15.2k
    {
187
15.2k
        SEV_PARSE("delta_chroma_log2_weight_denom", value, ps_bitstrm);
188
15.2k
        if((value < -7) || (value > 7))
189
1.31k
        {
190
1.31k
            return IHEVCD_INVALID_PARAMETER;
191
1.31k
        }
192
193
13.9k
        if(((ps_wt_ofst->i1_luma_log2_weight_denom + value) < 0) ||
194
12.3k
                ((ps_wt_ofst->i1_luma_log2_weight_denom + value) > 7))
195
1.67k
        {
196
1.67k
            return IHEVCD_INVALID_PARAMETER;
197
1.67k
        }
198
12.2k
        ps_wt_ofst->i1_chroma_log2_weight_denom = ps_wt_ofst->i1_luma_log2_weight_denom + value;
199
12.2k
    }
200
201
44.3k
    for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
202
31.3k
    {
203
31.3k
        BITS_PARSE("luma_weight_l0_flag[ i ]", value, ps_bitstrm, 1);
204
31.3k
        ps_wt_ofst->i1_luma_weight_l0_flag[i] = value;
205
31.3k
    }
206
207
208
209
13.0k
    if(ps_sps->i1_chroma_format_idc != CHROMA_FMT_IDC_MONOCHROME)
210
12.2k
    {
211
42.6k
        for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
212
30.3k
        {
213
30.3k
            BITS_PARSE("chroma_weight_l0_flag[ i ]", value, ps_bitstrm, 1);
214
30.3k
            ps_wt_ofst->i1_chroma_weight_l0_flag[i] = value;
215
30.3k
        }
216
12.2k
    }
217
787
    else
218
787
    {
219
1.77k
        for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
220
985
        {
221
985
            ps_wt_ofst->i1_chroma_weight_l0_flag[i] = 0;
222
985
        }
223
787
    }
224
225
226
32.0k
    for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
227
22.9k
    {
228
22.9k
        if(ps_wt_ofst->i1_luma_weight_l0_flag[i])
229
12.8k
        {
230
12.8k
            SEV_PARSE("delta_luma_weight_l0[ i ]", value, ps_bitstrm);
231
12.8k
            if( value < -128 || value > 127 )
232
1.04k
            {
233
1.04k
                return IHEVCD_INVALID_PARAMETER;
234
1.04k
            }
235
236
11.8k
            ps_wt_ofst->i2_luma_weight_l0[i] = (1 << ps_wt_ofst->i1_luma_log2_weight_denom) + value;
237
238
11.8k
            SEV_PARSE("luma_offset_l0[ i ]", value, ps_bitstrm);
239
11.8k
            if((value < -wp_ofst_half_rng_y) || (value > (wp_ofst_half_rng_y - 1)))
240
730
            {
241
730
                return IHEVCD_INVALID_PARAMETER;
242
730
            }
243
11.1k
            ps_wt_ofst->i2_luma_offset_l0[i] = value;
244
245
11.1k
        }
246
10.0k
        else
247
10.0k
        {
248
10.0k
            ps_wt_ofst->i2_luma_weight_l0[i] = (1 << ps_wt_ofst->i1_luma_log2_weight_denom);
249
10.0k
            ps_wt_ofst->i2_luma_offset_l0[i] = 0;
250
10.0k
        }
251
21.1k
        if(ps_wt_ofst->i1_chroma_weight_l0_flag[i])
252
9.60k
        {
253
9.60k
            WORD32 ofst;
254
9.60k
            WORD32 shift = wp_ofst_half_rng_c;
255
9.60k
            SEV_PARSE("delta_chroma_weight_l0[ i ][ j ]", value, ps_bitstrm);
256
9.60k
            if(value < -128 || value > 127)
257
623
            {
258
623
                return IHEVCD_INVALID_PARAMETER;
259
623
            }
260
8.98k
            ps_wt_ofst->i2_chroma_weight_l0_cb[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom) + value;
261
262
263
8.98k
            SEV_PARSE("delta_chroma_offset_l0[ i ][ j ]", value, ps_bitstrm);
264
8.98k
            if((value < (-4 * wp_ofst_half_rng_c)) || (value > (4 * wp_ofst_half_rng_c - 1)))
265
601
            {
266
601
                return IHEVCD_INVALID_PARAMETER;
267
601
            }
268
8.38k
            ofst = ((shift * ps_wt_ofst->i2_chroma_weight_l0_cb[i]) >> ps_wt_ofst->i1_chroma_log2_weight_denom);
269
8.38k
            ofst = value - ofst + shift;
270
271
8.38k
            ps_wt_ofst->i2_chroma_offset_l0_cb[i] = CLIP3(ofst, -wp_ofst_half_rng_c, wp_ofst_half_rng_c - 1);
272
273
8.38k
            SEV_PARSE("delta_chroma_weight_l0[ i ][ j ]", value, ps_bitstrm);
274
8.38k
            if(value < -128 || value > 127)
275
549
            {
276
549
                return IHEVCD_INVALID_PARAMETER;
277
549
            }
278
7.83k
            ps_wt_ofst->i2_chroma_weight_l0_cr[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom) + value;
279
280
281
7.83k
            SEV_PARSE("delta_chroma_offset_l0[ i ][ j ]", value, ps_bitstrm);
282
7.83k
            if((value < (-4 * wp_ofst_half_rng_c)) || (value > (4 * wp_ofst_half_rng_c - 1)))
283
371
            {
284
371
                return IHEVCD_INVALID_PARAMETER;
285
371
            }
286
7.46k
            ofst = ((shift * ps_wt_ofst->i2_chroma_weight_l0_cr[i]) >> ps_wt_ofst->i1_chroma_log2_weight_denom);
287
7.46k
            ofst = value - ofst + shift;
288
289
7.46k
            ps_wt_ofst->i2_chroma_offset_l0_cr[i] = CLIP3(ofst, -wp_ofst_half_rng_c, wp_ofst_half_rng_c - 1);
290
291
7.46k
        }
292
11.5k
        else
293
11.5k
        {
294
11.5k
            ps_wt_ofst->i2_chroma_weight_l0_cb[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom);
295
11.5k
            ps_wt_ofst->i2_chroma_weight_l0_cr[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom);
296
297
11.5k
            ps_wt_ofst->i2_chroma_offset_l0_cb[i] = 0;
298
11.5k
            ps_wt_ofst->i2_chroma_offset_l0_cr[i] = 0;
299
11.5k
        }
300
21.1k
    }
301
9.12k
    if(BSLICE == ps_slice_hdr->i1_slice_type)
302
8.51k
    {
303
27.5k
        for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
304
19.0k
        {
305
19.0k
            BITS_PARSE("luma_weight_l1_flag[ i ]", value, ps_bitstrm, 1);
306
19.0k
            ps_wt_ofst->i1_luma_weight_l1_flag[i] = value;
307
19.0k
        }
308
309
8.51k
        if(ps_sps->i1_chroma_format_idc != CHROMA_FMT_IDC_MONOCHROME)
310
7.79k
        {
311
25.9k
            for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
312
18.1k
            {
313
18.1k
                BITS_PARSE("chroma_weight_l1_flag[ i ]", value, ps_bitstrm, 1);
314
18.1k
                ps_wt_ofst->i1_chroma_weight_l1_flag[i] = value;
315
18.1k
            }
316
7.79k
        }
317
713
        else
318
713
        {
319
1.56k
            for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
320
854
            {
321
854
                ps_wt_ofst->i1_chroma_weight_l1_flag[i] = 0;
322
854
            }
323
713
        }
324
325
20.2k
        for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
326
14.6k
        {
327
14.6k
            if(ps_wt_ofst->i1_luma_weight_l1_flag[i])
328
8.93k
            {
329
8.93k
                SEV_PARSE("delta_luma_weight_l1[ i ]", value, ps_bitstrm);
330
8.93k
                if( value < -128 || value > 127 )
331
432
                {
332
432
                    return IHEVCD_INVALID_PARAMETER;
333
432
                }
334
335
8.50k
                ps_wt_ofst->i2_luma_weight_l1[i] = (1 << ps_wt_ofst->i1_luma_log2_weight_denom) + value;
336
337
8.50k
                SEV_PARSE("luma_offset_l1[ i ]", value, ps_bitstrm);
338
8.50k
                if((value < -wp_ofst_half_rng_y) || (value > (wp_ofst_half_rng_y - 1)))
339
601
                {
340
601
                    return IHEVCD_INVALID_PARAMETER;
341
601
                }
342
7.90k
                ps_wt_ofst->i2_luma_offset_l1[i] = value;
343
344
7.90k
            }
345
5.71k
            else
346
5.71k
            {
347
5.71k
                ps_wt_ofst->i2_luma_weight_l1[i] = (1 << ps_wt_ofst->i1_luma_log2_weight_denom);
348
5.71k
                ps_wt_ofst->i2_luma_offset_l1[i] = 0;
349
5.71k
            }
350
351
13.6k
            if(ps_wt_ofst->i1_chroma_weight_l1_flag[i])
352
7.92k
            {
353
7.92k
                WORD32 ofst;
354
7.92k
                WORD32 shift = wp_ofst_half_rng_c;
355
7.92k
                SEV_PARSE("delta_chroma_weight_l1[ i ][ j ]", value, ps_bitstrm);
356
7.92k
                if(value < -128 || value > 127)
357
283
                {
358
283
                    return IHEVCD_INVALID_PARAMETER;
359
283
                }
360
7.64k
                ps_wt_ofst->i2_chroma_weight_l1_cb[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom) + value;;
361
362
363
7.64k
                SEV_PARSE("delta_chroma_offset_l1[ i ][ j ]", value, ps_bitstrm);
364
7.64k
                if((value < (-4 * wp_ofst_half_rng_c)) || (value > (4 * wp_ofst_half_rng_c - 1)))
365
765
                {
366
765
                    return IHEVCD_INVALID_PARAMETER;
367
765
                }
368
6.87k
                ofst = ((shift * ps_wt_ofst->i2_chroma_weight_l1_cb[i]) >> ps_wt_ofst->i1_chroma_log2_weight_denom);
369
6.87k
                ofst = value - ofst + shift;
370
371
6.87k
                ps_wt_ofst->i2_chroma_offset_l1_cb[i] = CLIP3(ofst, -wp_ofst_half_rng_c, wp_ofst_half_rng_c - 1);
372
373
6.87k
                SEV_PARSE("delta_chroma_weight_l1[ i ][ j ]", value, ps_bitstrm);
374
6.87k
                if(value < -128 || value > 127)
375
292
                {
376
292
                    return IHEVCD_INVALID_PARAMETER;
377
292
                }
378
6.58k
                ps_wt_ofst->i2_chroma_weight_l1_cr[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom) + value;
379
380
381
6.58k
                SEV_PARSE("delta_chroma_offset_l1[ i ][ j ]", value, ps_bitstrm);
382
6.58k
                if((value < (-4 * wp_ofst_half_rng_c)) || (value > (4 * wp_ofst_half_rng_c - 1)))
383
545
                {
384
545
                    return IHEVCD_INVALID_PARAMETER;
385
545
                }
386
6.03k
                ofst = ((shift * ps_wt_ofst->i2_chroma_weight_l1_cr[i]) >> ps_wt_ofst->i1_chroma_log2_weight_denom);
387
6.03k
                ofst = value - ofst + shift;
388
389
6.03k
                ps_wt_ofst->i2_chroma_offset_l1_cr[i] = CLIP3(ofst, -wp_ofst_half_rng_c, wp_ofst_half_rng_c - 1);
390
391
6.03k
            }
392
5.70k
            else
393
5.70k
            {
394
5.70k
                ps_wt_ofst->i2_chroma_weight_l1_cb[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom);
395
5.70k
                ps_wt_ofst->i2_chroma_weight_l1_cr[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom);
396
397
5.70k
                ps_wt_ofst->i2_chroma_offset_l1_cb[i] = 0;
398
5.70k
                ps_wt_ofst->i2_chroma_offset_l1_cr[i] = 0;
399
400
5.70k
            }
401
13.6k
        }
402
8.51k
    }
403
6.21k
    return ret;
404
9.12k
}
405
406
/**
407
*******************************************************************************
408
*
409
* @brief
410
*  Parses short term reference picture set
411
*
412
* @par   Description
413
*  Parses short term reference picture set as per section 7.3.8.2.
414
* Can be called by either SPS or Slice header parsing modules.
415
*
416
* @param[in] ps_bitstrm
417
*  Pointer to bitstream structure
418
*
419
* @param[out] ps_stref_picset_base
420
*  Pointer to first short term ref pic set structure
421
*
422
* @param[in] num_short_term_ref_pic_sets
423
*  Number of short term reference pic sets
424
*
425
* @param[in] idx
426
*  Current short term ref pic set id
427
*
428
* @returns Error code from IHEVCD_ERROR_T
429
*
430
* @remarks
431
*
432
*******************************************************************************
433
*/
434
IHEVCD_ERROR_T ihevcd_short_term_ref_pic_set(bitstrm_t *ps_bitstrm,
435
                                             stref_picset_t *ps_stref_picset_base,
436
                                             WORD32 num_short_term_ref_pic_sets,
437
                                             WORD32 idx,
438
                                             stref_picset_t *ps_stref_picset)
439
64.8k
{
440
64.8k
    IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
441
64.8k
    UWORD32 value;
442
64.8k
    stref_picset_t *ps_stref_picset_ref;
443
64.8k
    WORD32 delta_idx, delta_rps;
444
64.8k
    WORD32 r_idx;
445
64.8k
    WORD32 i;
446
64.8k
    WORD32 j, k, temp;
447
64.8k
    if(idx > 0)
448
44.8k
    {
449
44.8k
        BITS_PARSE("inter_ref_pic_set_prediction_flag", value, ps_bitstrm, 1);
450
44.8k
        ps_stref_picset->i1_inter_ref_pic_set_prediction_flag = value;
451
44.8k
    }
452
19.9k
    else
453
19.9k
        ps_stref_picset->i1_inter_ref_pic_set_prediction_flag = 0;
454
455
64.8k
    if(ps_stref_picset->i1_inter_ref_pic_set_prediction_flag)
456
36.7k
    {
457
36.7k
        WORD32 delta_rps_sign;
458
36.7k
        WORD32 abs_delta_rps;
459
36.7k
        WORD32 num_neg_pics = 0;
460
36.7k
        WORD32 num_pos_pics = 0;
461
36.7k
        WORD32 num_pics = 0;
462
463
36.7k
        if(idx == num_short_term_ref_pic_sets)
464
5.22k
        {
465
5.22k
            UEV_PARSE("delta_idx_minus1", value, ps_bitstrm);
466
5.22k
            if(value > num_short_term_ref_pic_sets - 1)
467
455
            {
468
455
                return IHEVCD_INVALID_PARAMETER;
469
455
            }
470
4.77k
            delta_idx = value + 1;
471
4.77k
        }
472
31.5k
        else
473
31.5k
        {
474
31.5k
            delta_idx = 1;
475
31.5k
        }
476
36.2k
        r_idx = idx - delta_idx;
477
36.2k
        r_idx = CLIP3(r_idx, 0, idx - 1);
478
479
36.2k
        ps_stref_picset_ref = ps_stref_picset_base + r_idx;
480
481
36.2k
        BITS_PARSE("delta_rps_sign", value, ps_bitstrm, 1);
482
36.2k
        delta_rps_sign = value;
483
484
36.2k
        UEV_PARSE("abs_delta_rps_minus1", value, ps_bitstrm);
485
36.2k
        if(value > 32767)
486
298
        {
487
298
            return IHEVCD_INVALID_PARAMETER;
488
298
        }
489
35.9k
        abs_delta_rps = value + 1;
490
491
35.9k
        delta_rps = (1 - 2 * delta_rps_sign) * (abs_delta_rps);
492
493
494
495
210k
        for(i = 0; i <= ps_stref_picset_ref->i1_num_delta_pocs; i++)
496
174k
        {
497
174k
            WORD32 ref_idc;
498
499
            /*****************************************************************/
500
            /* ref_idc is parsed as below                                    */
501
            /* bits "1" ref_idc 1                                            */
502
            /* bits "01" ref_idc 2                                           */
503
            /* bits "00" ref_idc 0                                           */
504
            /*****************************************************************/
505
174k
            BITS_PARSE("used_by_curr_pic_flag", value, ps_bitstrm, 1);
506
174k
            ref_idc = value;
507
174k
            ps_stref_picset->ai1_used[num_pics] = value;
508
            /* If ref_idc is zero check for next bit */
509
174k
            if(0 == ref_idc)
510
32.7k
            {
511
32.7k
                BITS_PARSE("use_delta_flag", value, ps_bitstrm, 1);
512
32.7k
                ref_idc = value << 1;
513
32.7k
            }
514
174k
            if((ref_idc == 1) || (ref_idc == 2))
515
151k
            {
516
151k
                WORD32 delta_poc;
517
151k
                delta_poc = delta_rps;
518
151k
                delta_poc +=
519
151k
                                ((i < ps_stref_picset_ref->i1_num_delta_pocs) ?
520
124k
                                ps_stref_picset_ref->ai2_delta_poc[i] :
521
151k
                                0);
522
523
151k
                ps_stref_picset->ai2_delta_poc[num_pics] = delta_poc;
524
525
151k
                if(delta_poc < 0)
526
91.7k
                {
527
91.7k
                    num_neg_pics++;
528
91.7k
                }
529
59.4k
                else
530
59.4k
                {
531
59.4k
                    num_pos_pics++;
532
59.4k
                }
533
151k
                num_pics++;
534
151k
            }
535
174k
            ps_stref_picset->ai1_ref_idc[i] = ref_idc;
536
174k
        }
537
538
35.9k
        num_neg_pics = CLIP3(num_neg_pics, 0, MAX_DPB_SIZE - 1);
539
35.9k
        num_pos_pics = CLIP3(num_pos_pics, 0, (MAX_DPB_SIZE - 1 - num_neg_pics));
540
35.9k
        num_pics = num_neg_pics + num_pos_pics;
541
542
35.9k
        ps_stref_picset->i1_num_ref_idc =
543
35.9k
                        ps_stref_picset_ref->i1_num_delta_pocs + 1;
544
35.9k
        ps_stref_picset->i1_num_delta_pocs = num_pics;
545
35.9k
        ps_stref_picset->i1_num_pos_pics = num_pos_pics;
546
35.9k
        ps_stref_picset->i1_num_neg_pics = num_neg_pics;
547
548
549
152k
        for(j = 1; j < num_pics; j++)
550
116k
        {
551
116k
            WORD32 delta_poc = ps_stref_picset->ai2_delta_poc[j];
552
116k
            WORD8 i1_used = ps_stref_picset->ai1_used[j];
553
478k
            for(k = j - 1; k >= 0; k--)
554
362k
            {
555
362k
                temp = ps_stref_picset->ai2_delta_poc[k];
556
362k
                if(delta_poc < temp)
557
153k
                {
558
153k
                    ps_stref_picset->ai2_delta_poc[k + 1] = temp;
559
153k
                    ps_stref_picset->ai1_used[k + 1] = ps_stref_picset->ai1_used[k];
560
153k
                    ps_stref_picset->ai2_delta_poc[k] = delta_poc;
561
153k
                    ps_stref_picset->ai1_used[k] = i1_used;
562
153k
                }
563
362k
            }
564
116k
        }
565
        // flip the negative values to largest first
566
73.7k
        for(j = 0, k = num_neg_pics - 1; j < num_neg_pics >> 1; j++, k--)
567
37.7k
        {
568
37.7k
            WORD32 delta_poc = ps_stref_picset->ai2_delta_poc[j];
569
37.7k
            WORD8 i1_used = ps_stref_picset->ai1_used[j];
570
37.7k
            ps_stref_picset->ai2_delta_poc[j] = ps_stref_picset->ai2_delta_poc[k];
571
37.7k
            ps_stref_picset->ai1_used[j] = ps_stref_picset->ai1_used[k];
572
37.7k
            ps_stref_picset->ai2_delta_poc[k] = delta_poc;
573
37.7k
            ps_stref_picset->ai1_used[k] = i1_used;
574
37.7k
        }
575
576
35.9k
    }
577
28.1k
    else
578
28.1k
    {
579
28.1k
        WORD32 prev_poc = 0;
580
28.1k
        WORD32 poc;
581
582
28.1k
        UEV_PARSE("num_negative_pics", value, ps_bitstrm);
583
28.1k
        if(value > MAX_DPB_SIZE - 1)
584
1.66k
        {
585
1.66k
            return IHEVCD_INVALID_PARAMETER;
586
1.66k
        }
587
26.4k
        ps_stref_picset->i1_num_neg_pics = value;
588
589
26.4k
        UEV_PARSE("num_positive_pics", value, ps_bitstrm);
590
26.4k
        if(value > (MAX_DPB_SIZE - 1 - ps_stref_picset->i1_num_neg_pics))
591
511
        {
592
511
            return IHEVCD_INVALID_PARAMETER;
593
511
        }
594
25.9k
        ps_stref_picset->i1_num_pos_pics = value;
595
596
25.9k
        ps_stref_picset->i1_num_delta_pocs =
597
25.9k
                        ps_stref_picset->i1_num_neg_pics +
598
25.9k
                        ps_stref_picset->i1_num_pos_pics;
599
600
601
59.7k
        for(i = 0; i < ps_stref_picset->i1_num_neg_pics; i++)
602
34.2k
        {
603
34.2k
            UEV_PARSE("delta_poc_s0_minus1", value, ps_bitstrm);
604
34.2k
            if(value > 32767)
605
435
            {
606
435
                return IHEVCD_INVALID_PARAMETER;
607
435
            }
608
33.8k
            poc = prev_poc - ((WORD32)(value + 1));
609
33.8k
            prev_poc = poc;
610
33.8k
            ps_stref_picset->ai2_delta_poc[i] = poc;
611
612
33.8k
            BITS_PARSE("used_by_curr_pic_s0_flag", value, ps_bitstrm, 1);
613
33.8k
            ps_stref_picset->ai1_used[i] = value;
614
615
33.8k
        }
616
25.4k
        prev_poc = 0;
617
25.4k
        for(i = ps_stref_picset->i1_num_neg_pics;
618
56.9k
                        i < ps_stref_picset->i1_num_delta_pocs;
619
31.4k
                        i++)
620
31.7k
        {
621
31.7k
            UEV_PARSE("delta_poc_s1_minus1", value, ps_bitstrm);
622
31.7k
            if(value > 32767)
623
284
            {
624
284
                return IHEVCD_INVALID_PARAMETER;
625
284
            }
626
31.4k
            poc = prev_poc + (value + 1);
627
31.4k
            prev_poc = poc;
628
31.4k
            ps_stref_picset->ai2_delta_poc[i] = poc;
629
630
31.4k
            BITS_PARSE("used_by_curr_pic_s1_flag", value, ps_bitstrm, 1);
631
31.4k
            ps_stref_picset->ai1_used[i] = value;
632
633
31.4k
        }
634
635
25.4k
    }
636
637
61.1k
    return ret;
638
64.8k
}
639
640
641
static WORD32 ihevcd_parse_sub_layer_hrd_parameters(bitstrm_t *ps_bitstrm,
642
                                                    sub_lyr_hrd_params_t *ps_sub_layer_hrd_params,
643
                                                    WORD32 cpb_cnt,
644
                                                    WORD32 sub_pic_cpb_params_present_flag)
645
7.00k
{
646
7.00k
    WORD32 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
647
7.00k
    WORD32 i;
648
649
26.8k
    for(i = 0; i <= cpb_cnt; i++)
650
19.8k
    {
651
19.8k
        UEV_PARSE("bit_rate_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_bit_rate_value_minus1[i], ps_bitstrm);
652
19.8k
        if(ps_sub_layer_hrd_params->au4_bit_rate_value_minus1[i] > UINT_MAX - 1)
653
0
        {
654
0
            return IHEVCD_INVALID_PARAMETER;
655
0
        }
656
19.8k
        UEV_PARSE("cpb_size_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_cpb_size_value_minus1[i], ps_bitstrm);
657
19.8k
        if(ps_sub_layer_hrd_params->au4_cpb_size_value_minus1[i] > UINT_MAX - 1)
658
0
        {
659
0
            return IHEVCD_INVALID_PARAMETER;
660
0
        }
661
19.8k
        if(sub_pic_cpb_params_present_flag)
662
7.73k
        {
663
7.73k
            UEV_PARSE("cpb_size_du_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_cpb_size_du_value_minus1[i], ps_bitstrm);
664
7.73k
            if(ps_sub_layer_hrd_params->au4_cpb_size_du_value_minus1[i] > UINT_MAX - 1)
665
0
            {
666
0
                return IHEVCD_INVALID_PARAMETER;
667
0
            }
668
7.73k
            UEV_PARSE("bit_rate_du_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_bit_rate_du_value_minus1[i], ps_bitstrm);
669
7.73k
            if(ps_sub_layer_hrd_params->au4_bit_rate_du_value_minus1[i] > UINT_MAX - 1)
670
0
            {
671
0
                return IHEVCD_INVALID_PARAMETER;
672
0
            }
673
7.73k
        }
674
19.8k
        BITS_PARSE("cbr_flag[ i ]", ps_sub_layer_hrd_params->au1_cbr_flag[i], ps_bitstrm, 1);
675
19.8k
    }
676
677
7.00k
    return ret;
678
7.00k
}
679
680
681
static WORD32 ihevcd_parse_hrd_parameters(bitstrm_t *ps_bitstrm,
682
                                          hrd_params_t *ps_hrd,
683
                                          WORD32 common_info_present_flag,
684
                                          WORD32 max_num_sub_layers_minus1)
685
2.66k
{
686
2.66k
    WORD32 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
687
2.66k
    WORD32 i;
688
689
2.66k
    ps_hrd->u1_nal_hrd_parameters_present_flag = 0;
690
2.66k
    ps_hrd->u1_vcl_hrd_parameters_present_flag = 0;
691
692
2.66k
    ps_hrd->u1_sub_pic_cpb_params_present_flag = 0;
693
694
2.66k
    ps_hrd->u1_tick_divisor_minus2 = 0;
695
2.66k
    ps_hrd->u1_du_cpb_removal_delay_increment_length_minus1 = 0;
696
2.66k
    ps_hrd->u1_sub_pic_cpb_params_in_pic_timing_sei_flag = 0;
697
2.66k
    ps_hrd->u1_dpb_output_delay_du_length_minus1 = 0;
698
699
2.66k
    ps_hrd->u4_bit_rate_scale = 0;
700
2.66k
    ps_hrd->u4_cpb_size_scale = 0;
701
2.66k
    ps_hrd->u4_cpb_size_du_scale = 0;
702
703
2.66k
    ps_hrd->u1_initial_cpb_removal_delay_length_minus1 = 23;
704
2.66k
    ps_hrd->u1_au_cpb_removal_delay_length_minus1 = 23;
705
2.66k
    ps_hrd->u1_dpb_output_delay_length_minus1 = 23;
706
707
2.66k
    if(common_info_present_flag)
708
2.66k
    {
709
2.66k
        BITS_PARSE("nal_hrd_parameters_present_flag", ps_hrd->u1_nal_hrd_parameters_present_flag, ps_bitstrm, 1);
710
2.66k
        BITS_PARSE("vcl_hrd_parameters_present_flag", ps_hrd->u1_vcl_hrd_parameters_present_flag, ps_bitstrm, 1);
711
712
2.66k
        if(ps_hrd->u1_nal_hrd_parameters_present_flag  ||  ps_hrd->u1_vcl_hrd_parameters_present_flag)
713
2.37k
        {
714
2.37k
            BITS_PARSE("sub_pic_cpb_params_present_flag", ps_hrd->u1_sub_pic_cpb_params_present_flag, ps_bitstrm, 1);
715
2.37k
            if(ps_hrd->u1_sub_pic_cpb_params_present_flag)
716
1.38k
            {
717
1.38k
                BITS_PARSE("tick_divisor_minus2", ps_hrd->u1_tick_divisor_minus2, ps_bitstrm, 8);
718
1.38k
                BITS_PARSE("du_cpb_removal_delay_increment_length_minus1", ps_hrd->u1_du_cpb_removal_delay_increment_length_minus1, ps_bitstrm, 5);
719
1.38k
                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);
720
1.38k
                BITS_PARSE("dpb_output_delay_du_length_minus1", ps_hrd->u1_dpb_output_delay_du_length_minus1, ps_bitstrm, 5);
721
1.38k
            }
722
723
2.37k
            BITS_PARSE("bit_rate_scale", ps_hrd->u4_bit_rate_scale, ps_bitstrm, 4);
724
2.37k
            BITS_PARSE("cpb_size_scale", ps_hrd->u4_cpb_size_scale, ps_bitstrm, 4);
725
2.37k
            if(ps_hrd->u1_sub_pic_cpb_params_present_flag)
726
1.38k
                BITS_PARSE("cpb_size_du_scale", ps_hrd->u4_cpb_size_du_scale, ps_bitstrm, 4);
727
728
2.37k
            BITS_PARSE("initial_cpb_removal_delay_length_minus1", ps_hrd->u1_initial_cpb_removal_delay_length_minus1, ps_bitstrm, 5);
729
2.37k
            BITS_PARSE("au_cpb_removal_delay_length_minus1", ps_hrd->u1_au_cpb_removal_delay_length_minus1, ps_bitstrm, 5);
730
2.37k
            BITS_PARSE("dpb_output_delay_length_minus1", ps_hrd->u1_dpb_output_delay_length_minus1, ps_bitstrm, 5);
731
2.37k
        }
732
2.66k
    }
733
734
735
9.25k
    for(i = 0; i <= max_num_sub_layers_minus1; i++)
736
7.52k
    {
737
7.52k
        BITS_PARSE("fixed_pic_rate_general_flag[ i ]", ps_hrd->au1_fixed_pic_rate_general_flag[i], ps_bitstrm, 1);
738
739
7.52k
        ps_hrd->au1_fixed_pic_rate_within_cvs_flag[i] = 1;
740
7.52k
        ps_hrd->au2_elemental_duration_in_tc_minus1[i] = 0;
741
7.52k
        ps_hrd->au1_low_delay_hrd_flag[i] = 0;
742
7.52k
        ps_hrd->au1_cpb_cnt_minus1[i] = 0;
743
744
7.52k
        if(!ps_hrd->au1_fixed_pic_rate_general_flag[i])
745
3.96k
            BITS_PARSE("fixed_pic_rate_within_cvs_flag[ i ]", ps_hrd->au1_fixed_pic_rate_within_cvs_flag[i], ps_bitstrm, 1);
746
747
7.52k
        if(ps_hrd->au1_fixed_pic_rate_within_cvs_flag[i])
748
5.51k
        {
749
5.51k
            UEV_PARSE("elemental_duration_in_tc_minus1[ i ]", ps_hrd->au2_elemental_duration_in_tc_minus1[i], ps_bitstrm);
750
5.51k
            if(ps_hrd->au2_elemental_duration_in_tc_minus1[i] > 2047)
751
266
            {
752
266
                return IHEVCD_INVALID_PARAMETER;
753
266
            }
754
5.51k
        }
755
2.00k
        else
756
2.00k
        {
757
2.00k
            BITS_PARSE("low_delay_hrd_flag[ i ]", ps_hrd->au1_low_delay_hrd_flag[i], ps_bitstrm, 1);
758
2.00k
        }
759
760
7.25k
        if(!ps_hrd->au1_low_delay_hrd_flag[i])
761
6.20k
        {
762
6.20k
            UEV_PARSE("cpb_cnt_minus1[ i ]", ps_hrd->au1_cpb_cnt_minus1[i], ps_bitstrm);
763
6.20k
            if(ps_hrd->au1_cpb_cnt_minus1[i] > (MAX_CPB_CNT - 1))
764
668
            {
765
668
                return IHEVCD_INVALID_PARAMETER;
766
668
            }
767
6.20k
        }
768
769
6.59k
        if(ps_hrd->u1_nal_hrd_parameters_present_flag)
770
1.95k
            ihevcd_parse_sub_layer_hrd_parameters(ps_bitstrm,
771
1.95k
                                                  &ps_hrd->as_sub_layer_hrd_params[i],
772
1.95k
                                                  ps_hrd->au1_cpb_cnt_minus1[i],
773
1.95k
                                                  ps_hrd->u1_sub_pic_cpb_params_present_flag);
774
775
6.59k
        if(ps_hrd->u1_vcl_hrd_parameters_present_flag)
776
5.05k
            ihevcd_parse_sub_layer_hrd_parameters(ps_bitstrm,
777
5.05k
                                                  &ps_hrd->as_sub_layer_hrd_params[i],
778
5.05k
                                                  ps_hrd->au1_cpb_cnt_minus1[i],
779
5.05k
                                                  ps_hrd->u1_sub_pic_cpb_params_present_flag);
780
6.59k
    }
781
782
1.72k
    return ret;
783
2.66k
}
784
785
786
static WORD32 ihevcd_parse_vui_parameters(bitstrm_t *ps_bitstrm,
787
                                          vui_t *ps_vui,
788
                                          WORD32 sps_max_sub_layers_minus1)
789
18.3k
{
790
18.3k
    WORD32 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
791
18.3k
    UWORD16 u2_sar_width = 0;
792
18.3k
    UWORD16 u2_sar_height = 0;
793
794
18.3k
    BITS_PARSE("aspect_ratio_info_present_flag", ps_vui->u1_aspect_ratio_info_present_flag, ps_bitstrm, 1);
795
796
18.3k
    ps_vui->u1_aspect_ratio_idc = SAR_UNUSED;
797
18.3k
    u2_sar_width = 0;
798
18.3k
    u2_sar_height = 0;
799
18.3k
    if(ps_vui->u1_aspect_ratio_info_present_flag)
800
11.6k
    {
801
11.6k
        BITS_PARSE("aspect_ratio_idc", ps_vui->u1_aspect_ratio_idc, ps_bitstrm, 8);
802
11.6k
        switch(ps_vui->u1_aspect_ratio_idc)
803
11.6k
        {
804
241
            case SAR_1_1:
805
241
                u2_sar_width = 1;
806
241
                u2_sar_height = 1;
807
241
                break;
808
210
            case SAR_12_11:
809
210
                u2_sar_width = 12;
810
210
                u2_sar_height = 11;
811
210
                break;
812
216
            case SAR_10_11:
813
216
                u2_sar_width = 10;
814
216
                u2_sar_height = 11;
815
216
                break;
816
213
            case SAR_16_11:
817
213
                u2_sar_width = 16;
818
213
                u2_sar_height = 11;
819
213
                break;
820
68
            case SAR_40_33:
821
68
                u2_sar_width = 40;
822
68
                u2_sar_height = 33;
823
68
                break;
824
231
            case SAR_24_11:
825
231
                u2_sar_width = 24;
826
231
                u2_sar_height = 11;
827
231
                break;
828
238
            case SAR_20_11:
829
238
                u2_sar_width = 20;
830
238
                u2_sar_height = 11;
831
238
                break;
832
80
            case SAR_32_11:
833
80
                u2_sar_width = 32;
834
80
                u2_sar_height = 11;
835
80
                break;
836
1.17k
            case SAR_80_33:
837
1.17k
                u2_sar_width = 80;
838
1.17k
                u2_sar_height = 33;
839
1.17k
                break;
840
216
            case SAR_18_11:
841
216
                u2_sar_width = 18;
842
216
                u2_sar_height = 11;
843
216
                break;
844
278
            case SAR_15_11:
845
278
                u2_sar_width = 15;
846
278
                u2_sar_height = 11;
847
278
                break;
848
306
            case SAR_64_33:
849
306
                u2_sar_width = 64;
850
306
                u2_sar_height = 33;
851
306
                break;
852
210
            case SAR_160_99:
853
210
                u2_sar_width = 160;
854
210
                u2_sar_height = 99;
855
210
                break;
856
231
            case SAR_4_3:
857
231
                u2_sar_width = 4;
858
231
                u2_sar_height = 3;
859
231
                break;
860
95
            case SAR_3_2:
861
95
                u2_sar_width = 3;
862
95
                u2_sar_height = 2;
863
95
                break;
864
242
            case SAR_2_1:
865
242
                u2_sar_width = 2;
866
242
                u2_sar_height = 1;
867
242
                break;
868
2.36k
            case EXTENDED_SAR:
869
2.36k
                BITS_PARSE("sar_width", u2_sar_width, ps_bitstrm, 16);
870
2.36k
                BITS_PARSE("sar_height", u2_sar_height, ps_bitstrm, 16);
871
2.36k
                break;
872
5.01k
            default:
873
5.01k
                u2_sar_width = 0;
874
5.01k
                u2_sar_height = 0;
875
5.01k
                break;
876
11.6k
        }
877
11.6k
    }
878
879
18.3k
    ps_vui->u2_sar_width    = u2_sar_width;
880
18.3k
    ps_vui->u2_sar_height   = u2_sar_height;
881
882
18.3k
    BITS_PARSE("overscan_info_present_flag", ps_vui->u1_overscan_info_present_flag, ps_bitstrm, 1);
883
18.3k
    ps_vui->u1_overscan_appropriate_flag = 0;
884
18.3k
    if(ps_vui->u1_overscan_info_present_flag)
885
7.10k
        BITS_PARSE("overscan_appropriate_flag", ps_vui->u1_overscan_appropriate_flag, ps_bitstrm, 1);
886
887
18.3k
    BITS_PARSE("video_signal_type_present_flag", ps_vui->u1_video_signal_type_present_flag, ps_bitstrm, 1);
888
18.3k
    ps_vui->u1_video_format = VID_FMT_UNSPECIFIED;
889
18.3k
    ps_vui->u1_video_full_range_flag = 0;
890
18.3k
    ps_vui->u1_colour_description_present_flag = 0;
891
18.3k
    ps_vui->u1_colour_primaries = 2;
892
18.3k
    ps_vui->u1_transfer_characteristics = 2;
893
18.3k
    ps_vui->u1_matrix_coefficients = 2;
894
895
18.3k
    if(ps_vui->u1_video_signal_type_present_flag)
896
7.59k
    {
897
7.59k
        BITS_PARSE("video_format", ps_vui->u1_video_format, ps_bitstrm, 3);
898
7.59k
        BITS_PARSE("video_full_range_flag", ps_vui->u1_video_full_range_flag, ps_bitstrm, 1);
899
7.59k
        BITS_PARSE("colour_description_present_flag", ps_vui->u1_colour_description_present_flag, ps_bitstrm, 1);
900
7.59k
        if(ps_vui->u1_colour_description_present_flag)
901
5.37k
        {
902
5.37k
            BITS_PARSE("colour_primaries", ps_vui->u1_colour_primaries, ps_bitstrm, 8);
903
5.37k
            BITS_PARSE("transfer_characteristics", ps_vui->u1_transfer_characteristics, ps_bitstrm, 8);
904
5.37k
            BITS_PARSE("matrix_coeffs", ps_vui->u1_matrix_coefficients, ps_bitstrm, 8);
905
5.37k
        }
906
7.59k
    }
907
908
18.3k
    BITS_PARSE("chroma_loc_info_present_flag", ps_vui->u1_chroma_loc_info_present_flag, ps_bitstrm, 1);
909
18.3k
    ps_vui->u1_chroma_sample_loc_type_top_field = 0;
910
18.3k
    ps_vui->u1_chroma_sample_loc_type_bottom_field = 0;
911
18.3k
    if(ps_vui->u1_chroma_loc_info_present_flag)
912
3.69k
    {
913
3.69k
        UEV_PARSE("chroma_sample_loc_type_top_field", ps_vui->u1_chroma_sample_loc_type_top_field, ps_bitstrm);
914
3.69k
        if(ps_vui->u1_chroma_sample_loc_type_top_field > CHROMA_FMT_IDC_YUV444_PLANES + 1)
915
283
        {
916
283
            return IHEVCD_INVALID_PARAMETER;
917
283
        }
918
3.41k
        UEV_PARSE("chroma_sample_loc_type_bottom_field", ps_vui->u1_chroma_sample_loc_type_bottom_field, ps_bitstrm);
919
3.41k
        if(ps_vui->u1_chroma_sample_loc_type_bottom_field > CHROMA_FMT_IDC_YUV444_PLANES + 1)
920
235
        {
921
235
            return IHEVCD_INVALID_PARAMETER;
922
235
        }
923
3.41k
    }
924
925
17.8k
    BITS_PARSE("neutral_chroma_indication_flag", ps_vui->u1_neutral_chroma_indication_flag, ps_bitstrm, 1);
926
17.8k
    BITS_PARSE("field_seq_flag", ps_vui->u1_field_seq_flag, ps_bitstrm, 1);
927
17.8k
    BITS_PARSE("frame_field_info_present_flag", ps_vui->u1_frame_field_info_present_flag, ps_bitstrm, 1);
928
17.8k
    BITS_PARSE("default_display_window_flag", ps_vui->u1_default_display_window_flag, ps_bitstrm, 1);
929
17.8k
    ps_vui->u4_def_disp_win_left_offset = 0;
930
17.8k
    ps_vui->u4_def_disp_win_right_offset = 0;
931
17.8k
    ps_vui->u4_def_disp_win_top_offset = 0;
932
17.8k
    ps_vui->u4_def_disp_win_bottom_offset = 0;
933
17.8k
    if(ps_vui->u1_default_display_window_flag)
934
4.38k
    {
935
4.38k
        UEV_PARSE("def_disp_win_left_offset", ps_vui->u4_def_disp_win_left_offset, ps_bitstrm);
936
4.38k
        UEV_PARSE("def_disp_win_right_offset", ps_vui->u4_def_disp_win_right_offset, ps_bitstrm);
937
4.38k
        UEV_PARSE("def_disp_win_top_offset", ps_vui->u4_def_disp_win_top_offset, ps_bitstrm);
938
4.38k
        UEV_PARSE("def_disp_win_bottom_offset", ps_vui->u4_def_disp_win_bottom_offset, ps_bitstrm);
939
4.38k
    }
940
941
17.8k
    BITS_PARSE("vui_timing_info_present_flag", ps_vui->u1_vui_timing_info_present_flag, ps_bitstrm, 1);
942
17.8k
    if(ps_vui->u1_vui_timing_info_present_flag)
943
5.24k
    {
944
5.24k
        BITS_PARSE("vui_num_units_in_tick", ps_vui->u4_vui_num_units_in_tick, ps_bitstrm, 32);
945
5.24k
        BITS_PARSE("vui_time_scale", ps_vui->u4_vui_time_scale, ps_bitstrm, 32);
946
5.24k
        BITS_PARSE("vui_poc_proportional_to_timing_flag", ps_vui->u1_poc_proportional_to_timing_flag, ps_bitstrm, 1);
947
5.24k
        if(ps_vui->u1_poc_proportional_to_timing_flag)
948
2.58k
        {
949
2.58k
            UEV_PARSE("vui_num_ticks_poc_diff_one_minus1", ps_vui->u4_num_ticks_poc_diff_one_minus1, ps_bitstrm);
950
2.58k
            if(ps_vui->u4_num_ticks_poc_diff_one_minus1 > UINT_MAX - 1)
951
0
            {
952
0
                return IHEVCD_INVALID_PARAMETER;
953
0
            }
954
2.58k
        }
955
5.24k
        BITS_PARSE("vui_hrd_parameters_present_flag", ps_vui->u1_vui_hrd_parameters_present_flag, ps_bitstrm, 1);
956
5.24k
        if(ps_vui->u1_vui_hrd_parameters_present_flag)
957
2.66k
        {
958
2.66k
            ret = ihevcd_parse_hrd_parameters(ps_bitstrm, &ps_vui->s_vui_hrd_parameters, 1, sps_max_sub_layers_minus1);
959
2.66k
            RETURN_IF((ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS), ret);
960
1.72k
        }
961
5.24k
    }
962
963
16.8k
    BITS_PARSE("bitstream_restriction_flag", ps_vui->u1_bitstream_restriction_flag, ps_bitstrm, 1);
964
16.8k
    ps_vui->u1_tiles_fixed_structure_flag = 0;
965
16.8k
    ps_vui->u1_motion_vectors_over_pic_boundaries_flag = 1;
966
16.8k
    ps_vui->u1_restricted_ref_pic_lists_flag = 0;
967
16.8k
    ps_vui->u4_min_spatial_segmentation_idc = 0;
968
16.8k
    ps_vui->u1_max_bytes_per_pic_denom = 2;
969
16.8k
    ps_vui->u1_max_bits_per_mincu_denom = 1;
970
16.8k
    ps_vui->u1_log2_max_mv_length_horizontal = 15;
971
16.8k
    ps_vui->u1_log2_max_mv_length_vertical = 15;
972
16.8k
    if(ps_vui->u1_bitstream_restriction_flag)
973
4.05k
    {
974
4.05k
        BITS_PARSE("tiles_fixed_structure_flag", ps_vui->u1_tiles_fixed_structure_flag, ps_bitstrm, 1);
975
4.05k
        BITS_PARSE("motion_vectors_over_pic_boundaries_flag", ps_vui->u1_motion_vectors_over_pic_boundaries_flag, ps_bitstrm, 1);
976
4.05k
        BITS_PARSE("restricted_ref_pic_lists_flag", ps_vui->u1_restricted_ref_pic_lists_flag, ps_bitstrm, 1);
977
978
4.05k
        UEV_PARSE("min_spatial_segmentation_idc", ps_vui->u4_min_spatial_segmentation_idc, ps_bitstrm);
979
4.05k
        if(ps_vui->u4_min_spatial_segmentation_idc > 4095)
980
234
        {
981
234
            return IHEVCD_INVALID_PARAMETER;
982
234
        }
983
3.81k
        UEV_PARSE("max_bytes_per_pic_denom", ps_vui->u1_max_bytes_per_pic_denom, ps_bitstrm);
984
3.81k
        if(ps_vui->u1_max_bytes_per_pic_denom > 16)
985
243
        {
986
243
            return IHEVCD_INVALID_PARAMETER;
987
243
        }
988
3.57k
        UEV_PARSE("max_bits_per_min_cu_denom", ps_vui->u1_max_bits_per_mincu_denom, ps_bitstrm);
989
3.57k
        if(ps_vui->u1_max_bits_per_mincu_denom > 16)
990
301
        {
991
301
            return IHEVCD_INVALID_PARAMETER;
992
301
        }
993
3.27k
        UEV_PARSE("log2_max_mv_length_horizontal", ps_vui->u1_log2_max_mv_length_horizontal, ps_bitstrm);
994
3.27k
        if(ps_vui->u1_max_bits_per_mincu_denom > 16)
995
0
        {
996
0
            return IHEVCD_INVALID_PARAMETER;
997
0
        }
998
3.27k
        UEV_PARSE("log2_max_mv_length_vertical", ps_vui->u1_log2_max_mv_length_vertical, ps_bitstrm);
999
3.27k
        if(ps_vui->u1_max_bits_per_mincu_denom > 15)
1000
214
        {
1001
214
            return IHEVCD_INVALID_PARAMETER;
1002
214
        }
1003
3.27k
    }
1004
1005
15.8k
    return ret;
1006
16.8k
}
1007
1008
static WORD32 ihevcd_get_profile(profile_tier_lvl_t *ps_ptl)
1009
133k
{
1010
133k
    WORD32 profile = IHEVC_PROFILE_UNKNOWN;
1011
1012
133k
    if(ps_ptl->i1_profile_idc == 1 || ps_ptl->ai1_profile_compatibility_flag[1] == 1 ||
1013
66.0k
       ps_ptl->i1_profile_idc == 2 || ps_ptl->ai1_profile_compatibility_flag[2] == 1)
1014
78.8k
        profile = IHEVC_PROFILE_MAIN;
1015
55.0k
    else if(ps_ptl->i1_profile_idc == 3 || ps_ptl->ai1_profile_compatibility_flag[3] == 1)
1016
24.8k
        profile = IHEVC_PROFILE_MAIN_STILL;
1017
30.2k
#ifdef ENABLE_MAIN_REXT_PROFILE
1018
30.2k
    else if(ps_ptl->i1_profile_idc == 4 || ps_ptl->ai1_profile_compatibility_flag[4] == 1)
1019
25.3k
        profile = IHEVC_PROFILE_MAIN_REXT;
1020
133k
#endif
1021
1022
133k
    return profile;
1023
133k
}
1024
1025
/**
1026
*******************************************************************************
1027
*
1028
* @brief
1029
*  Parses profile tier and level info for either general layer of sub_layer
1030
*
1031
* @par   Description
1032
*  Parses profile tier and level info for either general layer of sub_layer
1033
* as per section 7.3.3
1034
*
1035
* Since the same function is called for parsing general_profile and
1036
* sub_layer_profile etc, variables do not specify whether the syntax is
1037
* for general or sub_layer. Similarly trace functions also do not differentiate
1038
*
1039
* @param[in] ps_bitstrm
1040
*  Pointer to bitstream structure
1041
*
1042
* @param[out] ps_ptl
1043
*  Pointer to profile, tier level structure
1044
*
1045
* @returns Error code from IHEVCD_ERROR_T
1046
*
1047
* @remarks
1048
*
1049
*******************************************************************************
1050
*/
1051
1052
static IHEVCD_ERROR_T ihevcd_parse_profile_tier_level_layer(bitstrm_t *ps_bitstrm,
1053
                                                            profile_tier_lvl_t *ps_ptl)
1054
111k
{
1055
111k
    WORD32 value;
1056
111k
    WORD32 i;
1057
111k
    IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
1058
1059
111k
    BITS_PARSE("XXX_profile_space[]", value, ps_bitstrm, 2);
1060
111k
    ps_ptl->i1_profile_space = value;
1061
1062
111k
    BITS_PARSE("XXX_tier_flag[]", value, ps_bitstrm, 1);
1063
111k
    ps_ptl->i1_tier_flag = value;
1064
1065
111k
    BITS_PARSE("XXX_profile_idc[]", value, ps_bitstrm, 5);
1066
111k
    ps_ptl->i1_profile_idc = value;
1067
1068
3.68M
    for(i = 0; i < MAX_PROFILE_COMPATBLTY; i++)
1069
3.56M
    {
1070
3.56M
        BITS_PARSE("XXX_profile_compatibility_flag[][j]", value, ps_bitstrm, 1);
1071
3.56M
        ps_ptl->ai1_profile_compatibility_flag[i] = value;
1072
3.56M
    }
1073
1074
111k
    BITS_PARSE("general_progressive_source_flag", value, ps_bitstrm, 1);
1075
111k
    ps_ptl->i1_general_progressive_source_flag = value;
1076
1077
111k
    BITS_PARSE("general_interlaced_source_flag", value, ps_bitstrm, 1);
1078
111k
    ps_ptl->i1_general_interlaced_source_flag = value;
1079
1080
111k
    BITS_PARSE("general_non_packed_constraint_flag", value, ps_bitstrm, 1);
1081
111k
    ps_ptl->i1_general_non_packed_constraint_flag = value;
1082
1083
111k
    BITS_PARSE("general_frame_only_constraint_flag", value, ps_bitstrm, 1);
1084
111k
    ps_ptl->i1_frame_only_constraint_flag = value;
1085
1086
111k
    BITS_PARSE("XXX_reserved_zero_44bits[0..15]", value, ps_bitstrm, 16);
1087
1088
111k
    BITS_PARSE("XXX_reserved_zero_44bits[16..31]", value, ps_bitstrm, 16);
1089
1090
111k
    BITS_PARSE("XXX_reserved_zero_44bits[32..43]", value, ps_bitstrm, 12);
1091
111k
    return ret;
1092
111k
}
1093
1094
1095
/**
1096
*******************************************************************************
1097
*
1098
* @brief
1099
*  Parses profile tier and level info
1100
*
1101
* @par   Description
1102
*  Parses profile tier and level info as per section 7.3.3
1103
* Called during VPS and SPS parsing
1104
* calls ihevcd_parse_profile_tier_level() for general layer and each sub_layers
1105
*
1106
* @param[in] ps_bitstrm
1107
*  Pointer to bitstream structure
1108
*
1109
* @param[out] ps_ptl
1110
*  Pointer to structure that contains profile, tier level for each layers
1111
*
1112
* @param[in] profile_present
1113
*  Flag to indicate if profile data is present
1114
*
1115
* @param[in] max_num_sub_layers
1116
*  Number of sub layers present
1117
*
1118
* @returns Error code from IHEVCD_ERROR_T
1119
*
1120
* @remarks
1121
*
1122
*******************************************************************************
1123
*/
1124
1125
static IHEVCD_ERROR_T ihevcd_profile_tier_level(bitstrm_t *ps_bitstrm,
1126
                                                profile_tier_lvl_info_t *ps_ptl,
1127
                                                WORD32 profile_present,
1128
                                                WORD32 max_num_sub_layers)
1129
105k
{
1130
105k
    WORD32 value;
1131
105k
    IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
1132
105k
    WORD32 i;
1133
1134
105k
    if(profile_present)
1135
105k
    {
1136
105k
        ret = ihevcd_parse_profile_tier_level_layer(ps_bitstrm, &ps_ptl->s_ptl_gen);
1137
105k
        if((IHEVCD_ERROR_T)IHEVCD_SUCCESS != ret)
1138
0
        {
1139
0
            return ret;
1140
0
        }
1141
105k
    }
1142
1143
105k
    BITS_PARSE("general_level_idc", value, ps_bitstrm, 8);
1144
105k
    ps_ptl->s_ptl_gen.u1_level_idc = value;
1145
1146
1147
202k
    for(i = 0; i < max_num_sub_layers; i++)
1148
96.3k
    {
1149
96.3k
        BITS_PARSE("sub_layer_profile_present_flag[i]", value, ps_bitstrm, 1);
1150
96.3k
        ps_ptl->ai1_sub_layer_profile_present_flag[i] = value;
1151
1152
96.3k
        BITS_PARSE("sub_layer_level_present_flag[i]", value, ps_bitstrm, 1);
1153
96.3k
        ps_ptl->ai1_sub_layer_level_present_flag[i] = value;
1154
96.3k
    }
1155
1156
105k
    if(max_num_sub_layers > 0)
1157
25.8k
    {
1158
136k
        for(i = max_num_sub_layers; i < 8; i++)
1159
110k
        {
1160
110k
            BITS_PARSE("reserved_zero_2bits", value, ps_bitstrm, 2);
1161
110k
        }
1162
25.8k
    }
1163
1164
202k
    for(i = 0; i < max_num_sub_layers; i++)
1165
96.3k
    {
1166
96.3k
        if(ps_ptl->ai1_sub_layer_profile_present_flag[i])
1167
5.57k
        {
1168
5.57k
            ret = ihevcd_parse_profile_tier_level_layer(ps_bitstrm,
1169
5.57k
                                                        &ps_ptl->as_ptl_sub[i]);
1170
5.57k
        }
1171
96.3k
        if(ps_ptl->ai1_sub_layer_level_present_flag[i])
1172
5.86k
        {
1173
5.86k
            BITS_PARSE("sub_layer_level_idc[i]", value, ps_bitstrm, 8);
1174
5.86k
            ps_ptl->as_ptl_sub[i].u1_level_idc = value;
1175
1176
5.86k
        }
1177
96.3k
    }
1178
1179
1180
1181
105k
    return ret;
1182
105k
}
1183
1184
/**
1185
*******************************************************************************
1186
*
1187
* @brief
1188
*  Parses Scaling List Data syntax
1189
*
1190
* @par Description:
1191
*  Parses Scaling List Data syntax as per Section: 7.3.6
1192
*
1193
* @param[in] ps_codec
1194
*  Pointer to codec context
1195
*
1196
* @returns  Error code from IHEVCD_ERROR_T
1197
*
1198
* @remarks
1199
*
1200
*
1201
*******************************************************************************
1202
*/
1203
IHEVCD_ERROR_T ihevcd_scaling_list_data(codec_t *ps_codec, WORD16 *pi2_scaling_mat, WORD8 chroma_format_idc)
1204
25.0k
{
1205
25.0k
    IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
1206
25.0k
    WORD32 size_id;
1207
25.0k
    WORD32 matrix_id;
1208
25.0k
    WORD32 value, dc_value = 0;
1209
25.0k
    UWORD32 u4_value;
1210
25.0k
    WORD32 next_coef;
1211
25.0k
    WORD32 coef_num;
1212
25.0k
    WORD32 i, j, k, offset;
1213
25.0k
    bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
1214
25.0k
    WORD16 *pi2_scaling_mat_offset;
1215
25.0k
    WORD32 scaling_mat_offset[] = {0,   16,  32,  48,   64,   80,   96,   160,  224,  288,  352,  416,
1216
25.0k
                                   480, 736, 992, 1248, 1504, 1760, 2016, 3040, 4064, 5088, 6112, 7136};
1217
25.0k
    UWORD8 *scan_table;
1218
1219
40.5k
    for(size_id = 0; size_id < 4; size_id++)
1220
39.6k
    {
1221
139k
        for(matrix_id = 0; matrix_id < 6; matrix_id += (size_id == 3) ? 3 : 1)
1222
124k
        {
1223
124k
            WORD32 scaling_list_pred_mode_flag;
1224
124k
            WORD32 scaling_list_delta_coef;
1225
124k
            BITS_PARSE("scaling_list_pred_mode_flag", scaling_list_pred_mode_flag, ps_bitstrm, 1);
1226
1227
124k
            offset = size_id * 6 + matrix_id;
1228
124k
            pi2_scaling_mat_offset = pi2_scaling_mat + scaling_mat_offset[offset];
1229
1230
124k
            if(!scaling_list_pred_mode_flag)
1231
78.2k
            {
1232
78.2k
                WORD32 num_elements;
1233
78.2k
                UEV_PARSE("scaling_list_pred_matrix_id_delta", u4_value,
1234
78.2k
                          ps_bitstrm);
1235
78.2k
                if(size_id <= 2)
1236
77.4k
                {
1237
77.4k
                    if(u4_value > matrix_id)
1238
14.8k
                        return IHEVCD_INVALID_PARAMETER;
1239
77.4k
                }
1240
834
                else
1241
834
                {
1242
834
                    if(u4_value > matrix_id / 3)
1243
92
                        return IHEVCD_INVALID_PARAMETER;
1244
834
                }
1245
1246
63.3k
                num_elements = (1 << (4 + (size_id << 1)));
1247
63.3k
                if(0 != u4_value)
1248
50.1k
                {
1249
50.1k
                    memmove(pi2_scaling_mat_offset,
1250
50.1k
                            pi2_scaling_mat_offset - (u4_value * (size_id == 3 ? 3 : 1)) * num_elements,
1251
50.1k
                            num_elements * sizeof(WORD16));
1252
50.1k
                }
1253
63.3k
            }
1254
45.9k
            else
1255
45.9k
            {
1256
45.9k
                next_coef = 8;
1257
45.9k
                coef_num = MIN(64, (1 << (4 + (size_id << 1))));
1258
1259
45.9k
                if(size_id > 1)
1260
6.51k
                {
1261
6.51k
                    SEV_PARSE("scaling_list_dc_coef_minus8", value,
1262
6.51k
                              ps_bitstrm);
1263
6.51k
                    if(value < -7 || value > 247)
1264
415
                    {
1265
415
                        return IHEVCD_INVALID_PARAMETER;
1266
415
                    }
1267
6.10k
                    next_coef = value + 8;
1268
6.10k
                    dc_value = next_coef;
1269
6.10k
                }
1270
45.5k
                if(size_id < 2)
1271
39.4k
                {
1272
39.4k
                    scan_table = (UWORD8 *)gapv_ihevc_invscan[size_id + 1];
1273
1274
856k
                    for(i = 0; i < coef_num; i++)
1275
824k
                    {
1276
824k
                        SEV_PARSE("scaling_list_delta_coef",
1277
824k
                                  scaling_list_delta_coef, ps_bitstrm);
1278
824k
                        if((scaling_list_delta_coef < -256) || (scaling_list_delta_coef > 255))
1279
7.05k
                        {
1280
7.05k
                            return IHEVCD_INVALID_PARAMETER;
1281
7.05k
                        }
1282
817k
                        next_coef = (next_coef + scaling_list_delta_coef + 256)
1283
817k
                                        % 256;
1284
817k
                        pi2_scaling_mat_offset[scan_table[i]] = next_coef;
1285
817k
                    }
1286
39.4k
                }
1287
6.10k
                else if(size_id == 2)
1288
4.15k
                {
1289
4.15k
                    scan_table = (UWORD8 *)gapv_ihevc_invscan[2];
1290
1291
224k
                    for(i = 0; i < coef_num; i++)
1292
220k
                    {
1293
220k
                        SEV_PARSE("scaling_list_delta_coef",
1294
220k
                                  scaling_list_delta_coef, ps_bitstrm);
1295
220k
                        if((scaling_list_delta_coef < -256) || (scaling_list_delta_coef > 255))
1296
1.12k
                        {
1297
1.12k
                            return IHEVCD_INVALID_PARAMETER;
1298
1.12k
                        }
1299
219k
                        next_coef = (next_coef + scaling_list_delta_coef + 256)
1300
219k
                                        % 256;
1301
1302
219k
                        offset = scan_table[i];
1303
219k
                        offset = (offset >> 3) * 16 * 2 + (offset & 0x7) * 2;
1304
219k
                        pi2_scaling_mat_offset[offset] = next_coef;
1305
219k
                        pi2_scaling_mat_offset[offset + 1] = next_coef;
1306
219k
                        pi2_scaling_mat_offset[offset + 16] = next_coef;
1307
219k
                        pi2_scaling_mat_offset[offset + 16 + 1] = next_coef;
1308
219k
                    }
1309
3.03k
                    pi2_scaling_mat_offset[0] = dc_value;
1310
3.03k
                }
1311
1.94k
                else
1312
1.94k
                {
1313
1.94k
                    scan_table = (UWORD8 *)gapv_ihevc_invscan[2];
1314
1315
104k
                    for(i = 0; i < coef_num; i++)
1316
103k
                    {
1317
103k
                        SEV_PARSE("scaling_list_delta_coef",
1318
103k
                                  scaling_list_delta_coef, ps_bitstrm);
1319
103k
                        if((scaling_list_delta_coef < -256) || (scaling_list_delta_coef > 255))
1320
579
                        {
1321
579
                            return IHEVCD_INVALID_PARAMETER;
1322
579
                        }
1323
102k
                        next_coef = (next_coef + scaling_list_delta_coef + 256)
1324
102k
                                        % 256;
1325
1326
102k
                        offset = scan_table[i];
1327
102k
                        offset = (offset >> 3) * 32 * 4 + (offset & 0x7) * 4;
1328
1329
513k
                        for(j = 0; j < 4; j++)
1330
410k
                        {
1331
410k
                            pi2_scaling_mat_offset[offset + j * 32] = next_coef;
1332
410k
                            pi2_scaling_mat_offset[offset + 1 + j * 32] = next_coef;
1333
410k
                            pi2_scaling_mat_offset[offset + 2 + j * 32] = next_coef;
1334
410k
                            pi2_scaling_mat_offset[offset + 3 + j * 32] = next_coef;
1335
410k
                        }
1336
102k
                        pi2_scaling_mat_offset[0] = dc_value;
1337
102k
                    }
1338
1.94k
                }
1339
45.5k
            }
1340
124k
        }
1341
39.6k
    }
1342
    // derive 32x32 CbCr scaling list from 16x16 CbCr scaling list
1343
921
    if(chroma_format_idc == CHROMA_FMT_IDC_YUV444)
1344
0
    {
1345
0
        WORD32 matrix_ids[] = { 1, 2, 4, 5 };
1346
0
        WORD16 *pi2_ref_scaling_mat;
1347
1348
0
        scan_table = (UWORD8 *)gapv_ihevc_invscan[2];
1349
0
        for(i = 0; i < 4; i++)
1350
0
        {
1351
0
            matrix_id = matrix_ids[i];
1352
0
            pi2_ref_scaling_mat = pi2_scaling_mat + scaling_mat_offset[2 * 6 + matrix_id];
1353
0
            pi2_scaling_mat_offset = pi2_scaling_mat + scaling_mat_offset[3 * 6 + matrix_id];
1354
1355
0
            for(j = 0; j < 64; j++)
1356
0
            {
1357
0
                offset = scan_table[j];
1358
0
                offset = (offset >> 3) * 16 * 2 + (offset & 0x7) * 2;
1359
                // the index 0 value may be overwritten by scaling_list_dc_coef. pick its alternative
1360
0
                next_coef = offset == 0 ? pi2_ref_scaling_mat[1] : pi2_ref_scaling_mat[offset];
1361
1362
0
                offset = scan_table[j];
1363
0
                offset = (offset >> 3) * 32 * 4 + (offset & 0x7) * 4;
1364
1365
0
                for(k = 0; k < 4; k++)
1366
0
                {
1367
0
                    pi2_scaling_mat_offset[offset + k * 32] = next_coef;
1368
0
                    pi2_scaling_mat_offset[offset + 1 + k * 32] = next_coef;
1369
0
                    pi2_scaling_mat_offset[offset + 2 + k * 32] = next_coef;
1370
0
                    pi2_scaling_mat_offset[offset + 3 + k * 32] = next_coef;
1371
0
                }
1372
0
            }
1373
0
            pi2_scaling_mat_offset[0] = pi2_ref_scaling_mat[0];
1374
0
        }
1375
0
    }
1376
1377
921
    return ret;
1378
25.0k
}
1379
1380
/**
1381
*******************************************************************************
1382
*
1383
* @brief
1384
*  Parses VPS (Video Parameter Set)
1385
*
1386
* @par Description:
1387
*  Parse Video Parameter Set as per Section 7.3.2.1
1388
* update vps structure corresponding to vps ID
1389
* Till parsing VPS id, the elements are stored in local variables and are copied
1390
* later
1391
*
1392
* @param[in] ps_codec
1393
*  Pointer to codec context.
1394
*
1395
* @returns Error code from IHEVCD_ERROR_T
1396
*
1397
* @remarks
1398
*
1399
*
1400
*******************************************************************************
1401
*/
1402
IHEVCD_ERROR_T ihevcd_parse_vps(codec_t *ps_codec)
1403
0
{
1404
0
    IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
1405
0
    WORD32 i;
1406
0
    UWORD32 value;
1407
0
    WORD32 vps_id;
1408
0
    vps_t *ps_vps;
1409
0
    bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
1410
0
    BITS_PARSE("vps_video_parameter_set_id", value, ps_bitstrm, 4);
1411
0
    vps_id = value;
1412
1413
0
    if(vps_id >= MAX_VPS_CNT)
1414
0
    {
1415
0
        ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_VPS_ID;
1416
0
        return IHEVCD_UNSUPPORTED_VPS_ID;
1417
0
    }
1418
1419
1420
0
    ps_vps = (ps_codec->s_parse.ps_vps_base + vps_id);
1421
1422
0
    ps_vps->i1_vps_id = vps_id;
1423
1424
0
    BITS_PARSE("vps_reserved_three_2bits", value, ps_bitstrm, 2);
1425
0
    ASSERT(value == 3);
1426
1427
0
    BITS_PARSE("vps_max_layers_minus1", value, ps_bitstrm, 6);
1428
    //ps_vps->i1_vps_max_layers = value + 1;
1429
1430
1431
1432
0
    BITS_PARSE("vps_max_sub_layers_minus1", value, ps_bitstrm, 3);
1433
0
    if(value > SPS_MAX_SUB_LAYERS - 1)
1434
0
    {
1435
0
        return IHEVCD_INVALID_PARAMETER;
1436
0
    }
1437
0
    ps_vps->i1_vps_max_sub_layers = value + 1;
1438
1439
0
    ASSERT(ps_vps->i1_vps_max_sub_layers < VPS_MAX_SUB_LAYERS);
1440
1441
0
    BITS_PARSE("vps_temporal_id_nesting_flag", value, ps_bitstrm, 1);
1442
0
    ps_vps->i1_vps_temporal_id_nesting_flag = value;
1443
1444
0
    BITS_PARSE("vps_reserved_ffff_16bits", value, ps_bitstrm, 16);
1445
0
    ASSERT(value == 0xFFFF);
1446
    // profile_and_level( 1, vps_max_sub_layers_minus1 )
1447
0
    ret = ihevcd_profile_tier_level(ps_bitstrm, &(ps_vps->s_ptl),
1448
0
                                    1, (ps_vps->i1_vps_max_sub_layers - 1));
1449
1450
0
    BITS_PARSE("vps_sub_layer_ordering_info_present_flag", value, ps_bitstrm, 1);
1451
0
    ps_vps->i1_sub_layer_ordering_info_present_flag = value;
1452
0
    i = (ps_vps->i1_sub_layer_ordering_info_present_flag ?
1453
0
                    0 : (ps_vps->i1_vps_max_sub_layers - 1));
1454
0
    for(; i < ps_vps->i1_vps_max_sub_layers; i++)
1455
0
    {
1456
0
        UEV_PARSE("vps_max_dec_pic_buffering[i]", value, ps_bitstrm);
1457
0
        if(value > MAX_DPB_SIZE)
1458
0
        {
1459
0
            return IHEVCD_INVALID_PARAMETER;
1460
0
        }
1461
0
        ps_vps->ai1_vps_max_dec_pic_buffering[i] = value;
1462
1463
        /* vps_num_reorder_pics (no max) used in print in order to match with HM */
1464
0
        UEV_PARSE("vps_num_reorder_pics[i]", value, ps_bitstrm);
1465
0
        if(value >= ps_vps->ai1_vps_max_dec_pic_buffering[i])
1466
0
        {
1467
0
            return IHEVCD_INVALID_PARAMETER;
1468
0
        }
1469
0
        ps_vps->ai1_vps_max_num_reorder_pics[i] = value;
1470
1471
0
        UEV_PARSE("vps_max_latency_increase[i]", value, ps_bitstrm);
1472
0
        if(value > UINT_MAX - 2)
1473
0
        {
1474
0
            return IHEVCD_INVALID_PARAMETER;
1475
0
        }
1476
0
        ps_vps->ai1_vps_max_latency_increase[i] = value;
1477
0
    }
1478
1479
1480
1481
0
    BITS_PARSE("vps_max_layer_id", value, ps_bitstrm, 6);
1482
    //ps_vps->i1_vps_max_layer_id  = value;
1483
1484
0
    UEV_PARSE("vps_num_layer_sets_minus1", value, ps_bitstrm);
1485
0
    if(value > 1023)
1486
0
    {
1487
0
        return IHEVCD_INVALID_PARAMETER;
1488
0
    }
1489
    //ps_vps->i1_vps_num_layer_sets  = value + 1;
1490
1491
0
    BITS_PARSE("vps_timing_info_present_flag", value, ps_bitstrm, 1);
1492
    //ps_vps->i1_vps_timing_info_present_flag  = value;
1493
1494
1495
1496
0
    return ret;
1497
0
}
1498
1499
/**
1500
*******************************************************************************
1501
*
1502
* @brief
1503
*  Parses SPS (Sequence Parameter Set)
1504
* sequence_parameter_set_rbsp()
1505
*
1506
* @par Description:
1507
*  Parse Sequence Parameter Set as per section  Section: 7.3.2.2
1508
* The sps is written to a temporary buffer and copied later to the
1509
* appropriate location
1510
*
1511
* @param[in] ps_codec
1512
*  Pointer to codec context
1513
*
1514
* @returns Error code from IHEVCD_ERROR_T
1515
*
1516
* @remarks
1517
*
1518
*
1519
*******************************************************************************
1520
*/
1521
IHEVCD_ERROR_T ihevcd_parse_sps(codec_t *ps_codec)
1522
106k
{
1523
106k
    IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
1524
106k
    UWORD32 value;
1525
1526
106k
    WORD32 i;
1527
106k
    WORD32 vps_id;
1528
106k
    WORD32 sps_max_sub_layers;
1529
106k
    WORD32 sps_id;
1530
106k
    WORD32 sps_temporal_id_nesting_flag;
1531
106k
    WORD32 i4_profile_idc;
1532
106k
    sps_t *ps_sps;
1533
106k
    profile_tier_lvl_info_t s_ptl = {0};
1534
106k
    bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
1535
106k
    WORD32 ctb_log2_size_y = 0;
1536
1537
1538
106k
    BITS_PARSE("video_parameter_set_id", value, ps_bitstrm, 4);
1539
106k
    if(value > MAX_VPS_CNT - 1)
1540
0
    {
1541
0
        return IHEVCD_INVALID_PARAMETER;
1542
0
    }
1543
106k
    vps_id = value;
1544
1545
106k
    BITS_PARSE("sps_max_sub_layers_minus1", value, ps_bitstrm, 3);
1546
106k
    if(value > SPS_MAX_SUB_LAYERS - 1)
1547
622
    {
1548
622
        return IHEVCD_INVALID_PARAMETER;
1549
622
    }
1550
105k
    sps_max_sub_layers = value + 1;
1551
1552
105k
    BITS_PARSE("sps_temporal_id_nesting_flag", value, ps_bitstrm, 1);
1553
105k
    sps_temporal_id_nesting_flag = value;
1554
1555
    //profile_and_level( 1, sps_max_sub_layers_minus1 )
1556
105k
    ret = ihevcd_profile_tier_level(ps_bitstrm, &(s_ptl), 1,
1557
105k
                                    (sps_max_sub_layers - 1));
1558
105k
    if ((IHEVCD_ERROR_T)IHEVCD_SUCCESS != ret)
1559
0
    {
1560
0
        ps_codec->s_parse.i4_error_code = ret;
1561
0
        return ret;
1562
0
    }
1563
1564
105k
    i4_profile_idc = (WORD32)s_ptl.s_ptl_gen.i1_profile_idc;
1565
1566
105k
    UEV_PARSE("seq_parameter_set_id", value, ps_bitstrm);
1567
105k
    sps_id = value;
1568
105k
    if((sps_id >= MAX_SPS_CNT) || (sps_id < 0))
1569
51.4k
    {
1570
51.4k
        if(ps_codec->i4_sps_done)
1571
22.6k
            return IHEVCD_UNSUPPORTED_SPS_ID;
1572
28.7k
        else
1573
28.7k
            sps_id = 0;
1574
51.4k
    }
1575
1576
1577
83.2k
    ps_sps = (ps_codec->s_parse.ps_sps_base + MAX_SPS_CNT - 1);
1578
    /* Reset SPS to zero */
1579
83.2k
    {
1580
83.2k
        WORD16 *pi2_scaling_mat = ps_sps->pi2_scaling_mat;
1581
83.2k
        memset(ps_sps, 0, sizeof(sps_t));
1582
83.2k
        ps_sps->pi2_scaling_mat = pi2_scaling_mat;
1583
83.2k
    }
1584
83.2k
    ps_sps->i1_sps_id = sps_id;
1585
83.2k
    ps_sps->i1_vps_id = vps_id;
1586
83.2k
    ps_sps->i1_sps_max_sub_layers = sps_max_sub_layers;
1587
83.2k
    ps_sps->i1_sps_temporal_id_nesting_flag = sps_temporal_id_nesting_flag;
1588
1589
83.2k
    memcpy(&ps_sps->s_ptl, &s_ptl, sizeof(profile_tier_lvl_info_t));
1590
1591
83.2k
    UEV_PARSE("chroma_format_idc", value, ps_bitstrm);
1592
83.2k
    if(value > 3)
1593
8.03k
    {
1594
8.03k
        return IHEVCD_INVALID_PARAMETER;
1595
8.03k
    }
1596
1597
75.2k
    WORD32 profile = ihevcd_get_profile(&ps_sps->s_ptl.s_ptl_gen);
1598
75.2k
    if(profile == IHEVC_PROFILE_UNKNOWN)
1599
4.87k
    {
1600
4.87k
        return IHEVCD_UNSUPPORTED_TOOL_SET;
1601
4.87k
    }
1602
1603
70.3k
    switch(value) {
1604
6.82k
        case CHROMA_FMT_IDC_MONOCHROME: {
1605
6.82k
                if (!(ps_codec->u4_enable_yuv_formats & (1 << CHROMA_FMT_IDC_MONOCHROME))) {
1606
0
                    ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
1607
0
                    return (IHEVCD_ERROR_T)IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
1608
0
                }
1609
6.82k
                if(profile != IHEVC_PROFILE_MAIN_REXT)
1610
4.38k
                    return IHEVCD_INVALID_PARAMETER;
1611
6.82k
            }
1612
2.43k
            break;
1613
57.9k
        case CHROMA_FMT_IDC_YUV420:
1614
57.9k
            break;
1615
826
        case CHROMA_FMT_IDC_YUV444: {
1616
826
                if (!(ps_codec->u4_enable_yuv_formats & (1 << CHROMA_FMT_IDC_YUV444))) {
1617
0
                    ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
1618
0
                    return (IHEVCD_ERROR_T)IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
1619
0
                }
1620
826
                if(profile != IHEVC_PROFILE_MAIN_REXT)
1621
457
                    return IHEVCD_INVALID_PARAMETER;
1622
826
            }
1623
369
            break;
1624
4.74k
        case CHROMA_FMT_IDC_YUV422: {
1625
4.74k
                if (!(ps_codec->u4_enable_yuv_formats & (1 << CHROMA_FMT_IDC_YUV422))) {
1626
0
                    ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
1627
0
                    return (IHEVCD_ERROR_T)IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
1628
0
                }
1629
4.74k
                if(profile != IHEVC_PROFILE_MAIN_REXT)
1630
957
                    return IHEVCD_INVALID_PARAMETER;
1631
4.74k
            }
1632
3.78k
            break;
1633
3.78k
        default: {
1634
0
                ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
1635
0
                return (IHEVCD_ERROR_T)IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
1636
4.74k
            }
1637
0
            break;
1638
70.3k
    }
1639
64.5k
    ps_sps->i1_chroma_format_idc = value;
1640
1641
64.5k
    if(CHROMA_FMT_IDC_YUV444 == ps_sps->i1_chroma_format_idc)
1642
369
    {
1643
369
        BITS_PARSE("separate_colour_plane_flag", value, ps_bitstrm, 1);
1644
369
        ps_sps->i1_separate_colour_plane_flag = value;
1645
369
    }
1646
64.2k
    else
1647
64.2k
    {
1648
64.2k
        ps_sps->i1_separate_colour_plane_flag = 0;
1649
64.2k
    }
1650
1651
64.5k
    UEV_PARSE("pic_width_in_luma_samples", value, ps_bitstrm);
1652
64.5k
    if(value > INT16_MAX)
1653
300
    {
1654
300
        return (IHEVCD_ERROR_T)IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
1655
300
    }
1656
64.2k
    ps_sps->i2_pic_width_in_luma_samples = value;
1657
1658
64.2k
    UEV_PARSE("pic_height_in_luma_samples", value, ps_bitstrm);
1659
64.2k
    if(value > INT16_MAX)
1660
452
    {
1661
452
        return (IHEVCD_ERROR_T)IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
1662
452
    }
1663
63.8k
    ps_sps->i2_pic_height_in_luma_samples = value;
1664
1665
63.8k
    if((0 >= ps_sps->i2_pic_width_in_luma_samples) || (0 >= ps_sps->i2_pic_height_in_luma_samples))
1666
2.50k
        return IHEVCD_INVALID_PARAMETER;
1667
1668
61.3k
    BITS_PARSE("pic_cropping_flag", value, ps_bitstrm, 1);
1669
61.3k
    ps_sps->i1_pic_cropping_flag = value;
1670
1671
61.3k
    if(ps_sps->i1_pic_cropping_flag)
1672
39.4k
    {
1673
1674
39.4k
        UEV_PARSE("pic_crop_left_offset", value, ps_bitstrm);
1675
39.4k
        if (value >= ps_sps->i2_pic_width_in_luma_samples)
1676
354
        {
1677
354
            return IHEVCD_INVALID_PARAMETER;
1678
354
        }
1679
39.0k
        ps_sps->i2_pic_crop_left_offset = value;
1680
1681
39.0k
        UEV_PARSE("pic_crop_right_offset", value, ps_bitstrm);
1682
39.0k
        if (value >= ps_sps->i2_pic_width_in_luma_samples)
1683
323
        {
1684
323
            return IHEVCD_INVALID_PARAMETER;
1685
323
        }
1686
38.7k
        ps_sps->i2_pic_crop_right_offset = value;
1687
1688
38.7k
        UEV_PARSE("pic_crop_top_offset", value, ps_bitstrm);
1689
38.7k
        if (value >= ps_sps->i2_pic_height_in_luma_samples)
1690
326
        {
1691
326
            return IHEVCD_INVALID_PARAMETER;
1692
326
        }
1693
38.4k
        ps_sps->i2_pic_crop_top_offset = value;
1694
1695
38.4k
        UEV_PARSE("pic_crop_bottom_offset", value, ps_bitstrm);
1696
38.4k
        if (value >= ps_sps->i2_pic_height_in_luma_samples)
1697
311
        {
1698
311
            return IHEVCD_INVALID_PARAMETER;
1699
311
        }
1700
38.1k
        ps_sps->i2_pic_crop_bottom_offset = value;
1701
38.1k
    }
1702
21.8k
    else
1703
21.8k
    {
1704
21.8k
        ps_sps->i2_pic_crop_left_offset = 0;
1705
21.8k
        ps_sps->i2_pic_crop_right_offset = 0;
1706
21.8k
        ps_sps->i2_pic_crop_top_offset = 0;
1707
21.8k
        ps_sps->i2_pic_crop_bottom_offset = 0;
1708
21.8k
    }
1709
1710
1711
60.0k
    UEV_PARSE("bit_depth_luma_minus8", value, ps_bitstrm);
1712
60.0k
    if ((0 == ps_codec->u4_enable_hbd) && (0 != value))
1713
0
    {
1714
0
        ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_BIT_DEPTH;
1715
0
        return IHEVCD_UNSUPPORTED_BIT_DEPTH;
1716
0
    }
1717
    // Limit the support to max bit depth of 10-bit
1718
60.0k
    if (value > 2)
1719
1.21k
        return IHEVCD_UNSUPPORTED_BIT_DEPTH;
1720
1721
58.7k
    if (((1 == i4_profile_idc) && (0 != value)) ||
1722
58.6k
        ((2 == i4_profile_idc) && (2 < value)) ||
1723
58.6k
        ((4 == i4_profile_idc) && (4 < value)))
1724
121
    {
1725
121
        return IHEVCD_UNSUPPORTED_BIT_DEPTH;
1726
121
    }
1727
58.6k
    ps_sps->i1_bit_depth_luma_minus8 = value;
1728
1729
58.6k
    UEV_PARSE("bit_depth_chroma_minus8", value, ps_bitstrm);
1730
58.6k
    if ((0 == ps_codec->u4_enable_hbd) && (0 != value))
1731
0
    {
1732
0
        ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_BIT_DEPTH;
1733
0
        return IHEVCD_UNSUPPORTED_BIT_DEPTH;
1734
0
    }
1735
    // Limit the support to max bit depth of 10-bit
1736
58.6k
    if (value > 2)
1737
457
        return IHEVCD_UNSUPPORTED_BIT_DEPTH;
1738
1739
58.2k
    if (((1 == i4_profile_idc) && (0 != value)) ||
1740
58.1k
        ((2 == i4_profile_idc) && (2 < value)) ||
1741
58.1k
        ((4 == i4_profile_idc) && (4 < value)))
1742
84
    {
1743
84
        return IHEVCD_UNSUPPORTED_BIT_DEPTH;
1744
84
    }
1745
58.1k
    ps_sps->i1_bit_depth_chroma_minus8 = value;
1746
1747
58.1k
    if(ps_sps->i1_chroma_format_idc != CHROMA_FMT_IDC_MONOCHROME)
1748
56.8k
    {
1749
56.8k
        if (ps_sps->i1_bit_depth_luma_minus8 != ps_sps->i1_bit_depth_chroma_minus8)
1750
332
            return IHEVCD_UNSUPPORTED_BIT_DEPTH;
1751
56.8k
    }
1752
1753
57.7k
    UEV_PARSE("log2_max_pic_order_cnt_lsb_minus4", value, ps_bitstrm);
1754
57.7k
    if(value > 12)
1755
258
        return IHEVCD_INVALID_PARAMETER;
1756
57.5k
    ps_sps->i1_log2_max_pic_order_cnt_lsb = value + 4;
1757
1758
57.5k
    BITS_PARSE("sps_sub_layer_ordering_info_present_flag", value, ps_bitstrm, 1);
1759
1760
57.5k
    ps_sps->i1_sps_sub_layer_ordering_info_present_flag = value;
1761
1762
1763
57.5k
    i = (ps_sps->i1_sps_sub_layer_ordering_info_present_flag ? 0 : (ps_sps->i1_sps_max_sub_layers - 1));
1764
162k
    for(; i < ps_sps->i1_sps_max_sub_layers; i++)
1765
106k
    {
1766
106k
        UEV_PARSE("max_dec_pic_buffering", value, ps_bitstrm);
1767
106k
        if(value > (MAX_DPB_SIZE - 1))
1768
531
        {
1769
531
            return IHEVCD_INVALID_PARAMETER;
1770
531
        }
1771
105k
        ps_sps->ai1_sps_max_dec_pic_buffering[i] = value + 1;
1772
1773
105k
        UEV_PARSE("num_reorder_pics", value, ps_bitstrm);
1774
105k
        if(value >= ps_sps->ai1_sps_max_dec_pic_buffering[i])
1775
554
        {
1776
554
            return IHEVCD_INVALID_PARAMETER;
1777
554
        }
1778
104k
        ps_sps->ai1_sps_max_num_reorder_pics[i] = value;
1779
1780
104k
        UEV_PARSE("max_latency_increase", value, ps_bitstrm);
1781
104k
        if(value > UINT_MAX - 2)
1782
36
        {
1783
36
            return IHEVCD_INVALID_PARAMETER;
1784
36
        }
1785
104k
        ps_sps->ai1_sps_max_latency_increase[i] = value;
1786
104k
    }
1787
1788
    /* Check if sps_max_dec_pic_buffering or sps_max_num_reorder_pics
1789
       has changed */
1790
56.4k
    if(0 != ps_codec->u4_allocate_dynamic_done)
1791
17.7k
    {
1792
17.7k
        sps_t *ps_sps_old = ps_codec->s_parse.ps_sps;
1793
17.7k
        if(ps_sps_old->ai1_sps_max_dec_pic_buffering[ps_sps_old->i1_sps_max_sub_layers - 1] !=
1794
17.7k
                    ps_sps->ai1_sps_max_dec_pic_buffering[ps_sps->i1_sps_max_sub_layers - 1])
1795
867
        {
1796
867
            if(0 == ps_codec->i4_first_pic_done)
1797
325
            {
1798
325
                return IHEVCD_INVALID_PARAMETER;
1799
325
            }
1800
542
            ps_codec->i4_reset_flag = 1;
1801
542
            return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
1802
867
        }
1803
1804
16.9k
        if(ps_sps_old->ai1_sps_max_num_reorder_pics[ps_sps_old->i1_sps_max_sub_layers - 1] !=
1805
16.9k
                    ps_sps->ai1_sps_max_num_reorder_pics[ps_sps->i1_sps_max_sub_layers - 1])
1806
356
        {
1807
356
            if(0 == ps_codec->i4_first_pic_done)
1808
119
            {
1809
119
                return IHEVCD_INVALID_PARAMETER;
1810
119
            }
1811
237
            ps_codec->i4_reset_flag = 1;
1812
237
            return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
1813
356
        }
1814
16.9k
    }
1815
1816
55.1k
    UEV_PARSE("log2_min_coding_block_size_minus3", value, ps_bitstrm);
1817
55.1k
    if(value > (LOG2_MAX_CU_SIZE - 3))
1818
509
    {
1819
509
        return IHEVCD_INVALID_PARAMETER;
1820
509
    }
1821
54.6k
    ps_sps->i1_log2_min_coding_block_size = value + 3;
1822
54.6k
    if((ps_sps->i2_pic_width_in_luma_samples % (1 << ps_sps->i1_log2_min_coding_block_size) != 0) ||
1823
54.3k
                    (ps_sps->i2_pic_height_in_luma_samples % (1 << ps_sps->i1_log2_min_coding_block_size) != 0))
1824
589
    {
1825
589
        return IHEVCD_INVALID_PARAMETER;
1826
589
    }
1827
1828
54.0k
    UEV_PARSE("log2_diff_max_min_coding_block_size", value, ps_bitstrm);
1829
54.0k
    if(value > (LOG2_MAX_CU_SIZE - ps_sps->i1_log2_min_coding_block_size))
1830
459
    {
1831
459
        return IHEVCD_INVALID_PARAMETER;
1832
459
    }
1833
53.6k
    ps_sps->i1_log2_diff_max_min_coding_block_size = value;
1834
1835
53.6k
    ctb_log2_size_y = ps_sps->i1_log2_min_coding_block_size + ps_sps->i1_log2_diff_max_min_coding_block_size;
1836
1837
53.6k
    if((ctb_log2_size_y < LOG2_MIN_CTB_SIZE) || (ctb_log2_size_y > LOG2_MAX_CTB_SIZE))
1838
264
    {
1839
264
        return IHEVCD_INVALID_PARAMETER;
1840
264
    }
1841
    /* Check if CTB size is different in case of multiple SPS with same ID */
1842
53.3k
    {
1843
53.3k
        sps_t *ps_sps_old = (ps_codec->s_parse.ps_sps_base + sps_id);
1844
53.3k
        if(ps_sps_old->i1_sps_valid && ps_sps_old->i1_log2_ctb_size != ctb_log2_size_y)
1845
294
        {
1846
294
            if(0 == ps_codec->i4_first_pic_done)
1847
141
            {
1848
141
                return IHEVCD_INVALID_PARAMETER;
1849
141
            }
1850
153
            ps_codec->i4_reset_flag = 1;
1851
153
            return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
1852
294
        }
1853
53.3k
    }
1854
53.0k
    ps_sps->i1_log2_ctb_size = ctb_log2_size_y;
1855
1856
53.0k
    UEV_PARSE("log2_min_transform_block_size_minus2", value, ps_bitstrm);
1857
53.0k
    if(value > (LOG2_MAX_TU_SIZE - 2))
1858
369
    {
1859
369
        return IHEVCD_INVALID_PARAMETER;
1860
369
    }
1861
52.7k
    ps_sps->i1_log2_min_transform_block_size = value + 2;
1862
52.7k
    if(ps_sps->i1_log2_min_transform_block_size >= ps_sps->i1_log2_min_coding_block_size)
1863
271
    {
1864
271
        return IHEVCD_INVALID_PARAMETER;
1865
271
    }
1866
1867
52.4k
    UEV_PARSE("log2_diff_max_min_transform_block_size", value, ps_bitstrm);
1868
52.4k
    if(value > (LOG2_MAX_TU_SIZE - ps_sps->i1_log2_min_transform_block_size))
1869
297
    {
1870
297
        return IHEVCD_INVALID_PARAMETER;
1871
297
    }
1872
52.1k
    ps_sps->i1_log2_diff_max_min_transform_block_size = value;
1873
1874
52.1k
    ps_sps->i1_log2_max_transform_block_size = ps_sps->i1_log2_min_transform_block_size +
1875
52.1k
                    ps_sps->i1_log2_diff_max_min_transform_block_size;
1876
1877
52.1k
    if(ps_sps->i1_log2_max_transform_block_size > ps_sps->i1_log2_ctb_size)
1878
76
    {
1879
76
        return IHEVCD_INVALID_PARAMETER;
1880
76
    }
1881
1882
52.0k
    ps_sps->i1_log2_min_pcm_coding_block_size = 0;
1883
52.0k
    ps_sps->i1_log2_diff_max_min_pcm_coding_block_size = 0;
1884
1885
52.0k
    UEV_PARSE("max_transform_hierarchy_depth_inter", value, ps_bitstrm);
1886
52.0k
    if(value > (ps_sps->i1_log2_ctb_size - ps_sps->i1_log2_min_transform_block_size))
1887
293
    {
1888
293
        return IHEVCD_INVALID_PARAMETER;
1889
293
    }
1890
51.7k
    ps_sps->i1_max_transform_hierarchy_depth_inter = value;
1891
1892
51.7k
    UEV_PARSE("max_transform_hierarchy_depth_intra", value, ps_bitstrm);
1893
51.7k
    if(value > (ps_sps->i1_log2_ctb_size - ps_sps->i1_log2_min_transform_block_size))
1894
517
    {
1895
517
        return IHEVCD_INVALID_PARAMETER;
1896
517
    }
1897
51.2k
    ps_sps->i1_max_transform_hierarchy_depth_intra = value;
1898
1899
    /* String has a d (enabled) in order to match with HM */
1900
51.2k
    BITS_PARSE("scaling_list_enabled_flag", value, ps_bitstrm, 1);
1901
51.2k
    ps_sps->i1_scaling_list_enable_flag = value;
1902
1903
51.2k
    if(ps_sps->i1_scaling_list_enable_flag)
1904
28.5k
    {
1905
28.5k
        COPY_DEFAULT_SCALING_LIST(ps_sps->pi2_scaling_mat);
1906
28.5k
        BITS_PARSE("sps_scaling_list_data_present_flag", value, ps_bitstrm, 1);
1907
28.5k
        ps_sps->i1_sps_scaling_list_data_present_flag = value;
1908
1909
28.5k
        if(ps_sps->i1_sps_scaling_list_data_present_flag)
1910
9.98k
            ihevcd_scaling_list_data(ps_codec, ps_sps->pi2_scaling_mat, ps_sps->i1_chroma_format_idc);
1911
28.5k
    }
1912
22.6k
    else
1913
22.6k
    {
1914
22.6k
        COPY_FLAT_SCALING_LIST(ps_sps->pi2_scaling_mat);
1915
22.6k
    }
1916
    /* String is asymmetric_motion_partitions_enabled_flag instead of amp_enabled_flag in order to match with HM */
1917
51.2k
    BITS_PARSE("asymmetric_motion_partitions_enabled_flag", value, ps_bitstrm, 1);
1918
51.2k
    ps_sps->i1_amp_enabled_flag = value;
1919
1920
51.2k
    BITS_PARSE("sample_adaptive_offset_enabled_flag", value, ps_bitstrm, 1);
1921
51.2k
    ps_sps->i1_sample_adaptive_offset_enabled_flag = value;
1922
1923
51.2k
    BITS_PARSE("pcm_enabled_flag", value, ps_bitstrm, 1);
1924
51.2k
    ps_sps->i1_pcm_enabled_flag = value;
1925
1926
51.2k
    if(ps_sps->i1_pcm_enabled_flag)
1927
18.7k
    {
1928
18.7k
        BITS_PARSE("pcm_sample_bit_depth_luma", value, ps_bitstrm, 4);
1929
18.7k
        ps_sps->i1_pcm_sample_bit_depth_luma = value + 1;
1930
1931
18.7k
        BITS_PARSE("pcm_sample_bit_depth_chroma", value, ps_bitstrm, 4);
1932
18.7k
        ps_sps->i1_pcm_sample_bit_depth_chroma = value + 1;
1933
1934
18.7k
        UEV_PARSE("log2_min_pcm_coding_block_size_minus3", value, ps_bitstrm);
1935
18.7k
        if(value < (MIN(ps_sps->i1_log2_min_coding_block_size, 5) - 3) || value > (MIN(ctb_log2_size_y, 5) - 3))
1936
592
        {
1937
592
            return IHEVCD_INVALID_PARAMETER;
1938
592
        }
1939
18.1k
        ps_sps->i1_log2_min_pcm_coding_block_size = value + 3;
1940
1941
18.1k
        UEV_PARSE("log2_diff_max_min_pcm_coding_block_size", value, ps_bitstrm);
1942
18.1k
        if(value > MIN(ctb_log2_size_y, 5) - ps_sps->i1_log2_min_pcm_coding_block_size)
1943
276
        {
1944
276
            return IHEVCD_INVALID_PARAMETER;
1945
276
        }
1946
17.8k
        ps_sps->i1_log2_diff_max_min_pcm_coding_block_size = value;
1947
17.8k
        BITS_PARSE("pcm_loop_filter_disable_flag", value, ps_bitstrm, 1);
1948
17.8k
        ps_sps->i1_pcm_loop_filter_disable_flag = value;
1949
1950
17.8k
    }
1951
50.3k
    UEV_PARSE("num_short_term_ref_pic_sets", value, ps_bitstrm);
1952
50.3k
    if(value > MAX_STREF_PICS_SPS)
1953
962
    {
1954
962
        return IHEVCD_INVALID_PARAMETER;
1955
962
    }
1956
49.4k
    ps_sps->i1_num_short_term_ref_pic_sets = value;
1957
1958
99.9k
    for(i = 0; i < ps_sps->i1_num_short_term_ref_pic_sets; i++)
1959
51.9k
    {
1960
51.9k
        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]);
1961
51.9k
        if (ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS)
1962
1.46k
        {
1963
1.46k
            return ret;
1964
1.46k
        }
1965
51.9k
    }
1966
1967
47.9k
    BITS_PARSE("long_term_ref_pics_present_flag", value, ps_bitstrm, 1);
1968
47.9k
    ps_sps->i1_long_term_ref_pics_present_flag = value;
1969
1970
47.9k
    if(ps_sps->i1_long_term_ref_pics_present_flag)
1971
12.1k
    {
1972
12.1k
        UEV_PARSE("num_long_term_ref_pics_sps", value, ps_bitstrm);
1973
12.1k
        if(value > MAX_LTREF_PICS_SPS)
1974
232
        {
1975
232
            return IHEVCD_INVALID_PARAMETER;
1976
232
        }
1977
11.9k
        ps_sps->i1_num_long_term_ref_pics_sps = value;
1978
1979
16.4k
        for(i = 0; i < ps_sps->i1_num_long_term_ref_pics_sps; i++)
1980
4.53k
        {
1981
4.53k
            BITS_PARSE("lt_ref_pic_poc_lsb_sps[ i ]", value, ps_bitstrm, ps_sps->i1_log2_max_pic_order_cnt_lsb);
1982
4.53k
            ps_sps->au2_lt_ref_pic_poc_lsb_sps[i] = value;
1983
1984
4.53k
            BITS_PARSE("used_by_curr_pic_lt_sps_flag[ i ]", value, ps_bitstrm, 1);
1985
4.53k
            ps_sps->ai1_used_by_curr_pic_lt_sps_flag[i] = value;
1986
4.53k
        }
1987
11.9k
    }
1988
1989
47.7k
    BITS_PARSE("sps_temporal_mvp_enable_flag", value, ps_bitstrm, 1);
1990
47.7k
    ps_sps->i1_sps_temporal_mvp_enable_flag = value;
1991
1992
    /* Print matches HM 8-2 */
1993
47.7k
    BITS_PARSE("sps_strong_intra_smoothing_enable_flag", value, ps_bitstrm, 1);
1994
47.7k
    ps_sps->i1_strong_intra_smoothing_enable_flag = value;
1995
1996
47.7k
    BITS_PARSE("vui_parameters_present_flag", value, ps_bitstrm, 1);
1997
47.7k
    ps_sps->i1_vui_parameters_present_flag = value;
1998
1999
47.7k
    if(ps_sps->i1_vui_parameters_present_flag)
2000
18.3k
    {
2001
18.3k
        ret = ihevcd_parse_vui_parameters(ps_bitstrm,
2002
18.3k
                                          &ps_sps->s_vui_parameters,
2003
18.3k
                                          ps_sps->i1_sps_max_sub_layers - 1);
2004
18.3k
        RETURN_IF((ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS), ret);
2005
2006
15.8k
        if (0 != ps_codec->u4_allocate_dynamic_done) {
2007
2008
4.82k
            vui_t *ps_vui = &ps_sps->s_vui_parameters;
2009
4.82k
            sps_t *ps_sps_old = ps_codec->s_parse.ps_sps;
2010
4.82k
            vui_t *ps_vui_old = &ps_sps_old->s_vui_parameters;
2011
2012
4.82k
            if (ps_vui->u1_video_full_range_flag != ps_vui_old->u1_video_full_range_flag ||
2013
4.28k
                ps_vui->u1_colour_primaries != ps_vui_old->u1_colour_primaries ||
2014
3.28k
                ps_vui->u1_transfer_characteristics != ps_vui_old->u1_transfer_characteristics ||
2015
3.16k
                ps_vui->u1_matrix_coefficients != ps_vui_old->u1_matrix_coefficients) {
2016
1.79k
                ps_codec->i4_reset_flag = 1;
2017
1.79k
                return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
2018
1.79k
            }
2019
4.82k
        }
2020
15.8k
    }
2021
2022
43.4k
    BITS_PARSE("sps_extension_present_flag", value, ps_bitstrm, 1);
2023
43.4k
    ps_sps->i1_sps_extension_present_flag = value;
2024
2025
43.4k
    if(ps_sps->i1_sps_extension_present_flag)
2026
4.39k
    {
2027
4.39k
        BITS_PARSE("sps_range_extension_flag", value, ps_bitstrm, 1);
2028
4.39k
        ps_sps->i1_sps_range_extension_flag = value;
2029
2030
4.39k
        BITS_PARSE("sps_multilayer_extension_flag", value, ps_bitstrm, 1);
2031
4.39k
        ps_sps->i1_sps_multilayer_extension_flag = value;
2032
2033
4.39k
        BITS_PARSE("sps_3d_extension_flag", value, ps_bitstrm, 1);
2034
4.39k
        ps_sps->i1_sps_3d_extension_flag = value;
2035
2036
4.39k
        BITS_PARSE("sps_scc_extension_flag", value, ps_bitstrm, 1);
2037
4.39k
        ps_sps->i1_sps_scc_extension_flag = value;
2038
2039
4.39k
        BITS_PARSE("sps_extension_4bits", value, ps_bitstrm, 4);
2040
4.39k
        ps_sps->i1_sps_extension_4bits = value;
2041
4.39k
    }
2042
39.0k
    else
2043
39.0k
    {
2044
39.0k
        ps_sps->i1_sps_range_extension_flag = 0;
2045
39.0k
        ps_sps->i1_sps_multilayer_extension_flag = 0;
2046
39.0k
        ps_sps->i1_sps_3d_extension_flag = 0;
2047
39.0k
        ps_sps->i1_sps_scc_extension_flag = 0;
2048
39.0k
        ps_sps->i1_sps_extension_4bits = 0;
2049
39.0k
    }
2050
2051
43.4k
#ifdef ENABLE_MAIN_REXT_PROFILE
2052
43.4k
    if(ps_sps->i1_sps_range_extension_flag)
2053
3.25k
    {
2054
        /* ITU-T H.265 Section 7.3.2.2.2 Range extension sequence parameter set syntax */
2055
3.25k
        BITS_PARSE("transform_skip_rotation_enabled_flag", value, ps_bitstrm, 1);
2056
3.25k
        ps_sps->i1_transform_skip_rotation_enabled_flag = value;
2057
2058
3.25k
        BITS_PARSE("transform_skip_context_enabled_flag", value, ps_bitstrm, 1);
2059
3.25k
        ps_sps->i1_transform_skip_context_enabled_flag = value;
2060
2061
3.25k
        BITS_PARSE("implicit_rdpcm_enabled_flag", value, ps_bitstrm, 1);
2062
3.25k
        ps_sps->i1_implicit_rdpcm_enabled_flag = value;
2063
2064
3.25k
        BITS_PARSE("explicit_rdpcm_enabled_flag", value, ps_bitstrm, 1);
2065
3.25k
        ps_sps->i1_explicit_rdpcm_enabled_flag = value;
2066
2067
3.25k
        BITS_PARSE("extended_precision_processing_flag", value, ps_bitstrm, 1);
2068
3.25k
        ps_sps->i1_extended_precision_processing_flag = value;
2069
2070
3.25k
        BITS_PARSE("intra_smoothing_disabled_flag", value, ps_bitstrm, 1);
2071
3.25k
        ps_sps->i1_intra_smoothing_disabled_flag = value;
2072
2073
3.25k
        BITS_PARSE("high_precision_offsets_enabled_flag", value, ps_bitstrm, 1);
2074
3.25k
        ps_sps->i1_use_high_precision_pred_wt = value;
2075
2076
3.25k
        BITS_PARSE("persistent_rice_adaptation_enabled_flag", value, ps_bitstrm, 1);
2077
3.25k
        ps_sps->i1_persistent_rice_adaptation_enabled_flag = value;
2078
2079
3.25k
        BITS_PARSE("cabac_bypass_alignment_enabled_flag", value, ps_bitstrm, 1);
2080
3.25k
        ps_sps->i1_align_cabac_before_bypass = value;
2081
3.25k
    }
2082
43.4k
    if(profile != IHEVC_PROFILE_MAIN_REXT)
2083
36.9k
    {
2084
36.9k
        if(ps_sps->i1_transform_skip_rotation_enabled_flag
2085
36.0k
                        || ps_sps->i1_transform_skip_context_enabled_flag
2086
35.8k
                        || ps_sps->i1_implicit_rdpcm_enabled_flag
2087
35.6k
                        || ps_sps->i1_explicit_rdpcm_enabled_flag
2088
35.3k
                        || ps_sps->i1_extended_precision_processing_flag
2089
35.1k
                        || ps_sps->i1_intra_smoothing_disabled_flag
2090
34.8k
                        || ps_sps->i1_persistent_rice_adaptation_enabled_flag
2091
34.7k
                        || ps_sps->i1_align_cabac_before_bypass)
2092
2.37k
        {
2093
2.37k
            return IHEVCD_INVALID_PARAMETER;
2094
2.37k
        }
2095
36.9k
    }
2096
41.1k
    if(ps_sps->i1_extended_precision_processing_flag || ps_sps->i1_align_cabac_before_bypass)
2097
161
    {
2098
        // main-rext 8-bit profiles require these fields to be off
2099
161
        return IHEVCD_INVALID_PARAMETER;
2100
161
    }
2101
40.9k
    if(ps_sps->i1_sps_multilayer_extension_flag || ps_sps->i1_sps_3d_extension_flag
2102
40.2k
                    || ps_sps->i1_sps_scc_extension_flag)
2103
888
    {
2104
888
        return IHEVCD_UNSUPPORTED_TOOL_SET;
2105
888
    }
2106
#else
2107
    if(ps_sps->i1_sps_range_extension_flag || ps_sps->i1_sps_multilayer_extension_flag
2108
                    || ps_sps->i1_sps_3d_extension_flag || ps_sps->i1_sps_scc_extension_flag)
2109
    {
2110
        // TODO: add support for parsing these syntax elements
2111
        return IHEVCD_UNSUPPORTED_TOOL_SET;
2112
    }
2113
#endif
2114
2115
40.0k
    if((UWORD8 *)ps_bitstrm->pu4_buf > ps_bitstrm->pu1_buf_max)
2116
2.04k
    {
2117
2.04k
        return IHEVCD_INVALID_PARAMETER;
2118
2.04k
    }
2119
2120
38.0k
    {
2121
38.0k
        WORD32 numerator;
2122
38.0k
        WORD32 ceil_offset;
2123
2124
38.0k
        ceil_offset = (1 << ps_sps->i1_log2_ctb_size) - 1;
2125
38.0k
        numerator = ps_sps->i2_pic_width_in_luma_samples;
2126
2127
38.0k
        ps_sps->i2_pic_wd_in_ctb = ((numerator + ceil_offset) /
2128
38.0k
                        (1 << ps_sps->i1_log2_ctb_size));
2129
2130
38.0k
        numerator = ps_sps->i2_pic_height_in_luma_samples;
2131
38.0k
        ps_sps->i2_pic_ht_in_ctb = ((numerator + ceil_offset) /
2132
38.0k
                        (1 << ps_sps->i1_log2_ctb_size));
2133
2134
38.0k
        ps_sps->i4_pic_size_in_ctb = ps_sps->i2_pic_ht_in_ctb *
2135
38.0k
                        ps_sps->i2_pic_wd_in_ctb;
2136
2137
38.0k
        if(0 == ps_codec->i4_sps_done)
2138
23.9k
            ps_codec->s_parse.i4_next_ctb_indx = ps_sps->i4_pic_size_in_ctb;
2139
2140
38.0k
        numerator = ps_sps->i2_pic_width_in_luma_samples;
2141
38.0k
        ps_sps->i2_pic_wd_in_min_cb = numerator  /
2142
38.0k
                        (1 << ps_sps->i1_log2_min_coding_block_size);
2143
2144
38.0k
        numerator = ps_sps->i2_pic_height_in_luma_samples;
2145
38.0k
        ps_sps->i2_pic_ht_in_min_cb = numerator  /
2146
38.0k
                        (1 << ps_sps->i1_log2_min_coding_block_size);
2147
38.0k
    }
2148
38.0k
    if((0 != ps_codec->u4_allocate_dynamic_done) &&
2149
14.0k
                    ((ps_codec->i4_wd != ps_sps->i2_pic_width_in_luma_samples) ||
2150
13.9k
                    (ps_codec->i4_ht != ps_sps->i2_pic_height_in_luma_samples)))
2151
201
    {
2152
201
        if(0 == ps_codec->i4_first_pic_done)
2153
145
        {
2154
145
            return IHEVCD_INVALID_PARAMETER;
2155
145
        }
2156
56
        ps_codec->i4_reset_flag = 1;
2157
56
        return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
2158
201
    }
2159
2160
    // Ensure both i2_pic_width_in_luma_samples and i2_pic_height_in_luma_samples do
2161
    // not exceed MAX_WD and their product doesn't exceed MAX_WD * MAX_HT
2162
37.8k
    if((ps_sps->i2_pic_width_in_luma_samples > MAX_WD) ||
2163
37.7k
                    (ps_sps->i2_pic_height_in_luma_samples > MAX_WD) ||
2164
37.5k
                    ((ps_sps->i2_pic_width_in_luma_samples * ps_sps->i2_pic_height_in_luma_samples) >
2165
37.5k
                    (MAX_WD * MAX_HT)))
2166
273
    {
2167
273
        return (IHEVCD_ERROR_T)IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
2168
273
    }
2169
2170
    // limit format conversions
2171
37.5k
    if(ps_codec->e_chroma_fmt == IV_YUV_444P
2172
226
                    && ps_sps->i1_chroma_format_idc != CHROMA_FMT_IDC_YUV444)
2173
226
    {
2174
226
        return (IHEVCD_ERROR_T)IVD_INIT_DEC_COL_FMT_NOT_SUPPORTED;
2175
226
    }
2176
37.3k
    if(ps_codec->e_chroma_fmt == IV_YUV_422P
2177
569
                    && ps_sps->i1_chroma_format_idc != CHROMA_FMT_IDC_YUV422)
2178
247
    {
2179
247
        return (IHEVCD_ERROR_T)IVD_INIT_DEC_COL_FMT_NOT_SUPPORTED;
2180
247
    }
2181
37.0k
    if((ps_codec->e_chroma_fmt == IV_YUV_420SP_UV || ps_codec->e_chroma_fmt == IV_YUV_420SP_VU)
2182
12.9k
                    && ps_sps->i1_chroma_format_idc != CHROMA_FMT_IDC_YUV420)
2183
264
    {
2184
264
        return (IHEVCD_ERROR_T)IVD_INIT_DEC_COL_FMT_NOT_SUPPORTED;
2185
264
    }
2186
2187
    /* Update display width and display height */
2188
36.8k
    {
2189
36.8k
        WORD32 disp_wd, disp_ht;
2190
36.8k
        WORD32 crop_unit_x, crop_unit_y;
2191
36.8k
        crop_unit_x = 1;
2192
36.8k
        crop_unit_y = 1;
2193
2194
36.8k
        if(CHROMA_FMT_IDC_YUV420 == ps_sps->i1_chroma_format_idc)
2195
33.0k
        {
2196
33.0k
            crop_unit_x = 2;
2197
33.0k
            crop_unit_y = 2;
2198
33.0k
        }
2199
3.76k
        else if(CHROMA_FMT_IDC_YUV422 == ps_sps->i1_chroma_format_idc)
2200
3.12k
        {
2201
3.12k
            crop_unit_x = 2;
2202
3.12k
            crop_unit_y = 1;
2203
3.12k
        }
2204
2205
36.8k
        disp_wd = ps_sps->i2_pic_width_in_luma_samples;
2206
36.8k
        disp_wd -= ps_sps->i2_pic_crop_left_offset * crop_unit_x;
2207
36.8k
        disp_wd -= ps_sps->i2_pic_crop_right_offset * crop_unit_x;
2208
2209
2210
36.8k
        disp_ht = ps_sps->i2_pic_height_in_luma_samples;
2211
36.8k
        disp_ht -= ps_sps->i2_pic_crop_top_offset * crop_unit_y;
2212
36.8k
        disp_ht -= ps_sps->i2_pic_crop_bottom_offset * crop_unit_y;
2213
2214
36.8k
        if((0 >= disp_wd) || (0 >= disp_ht))
2215
275
            return IHEVCD_INVALID_PARAMETER;
2216
2217
36.5k
        if(CHROMA_FMT_IDC_YUV420 == ps_sps->i1_chroma_format_idc)
2218
32.7k
        {
2219
32.7k
            if(disp_wd % 2 != 0 || disp_ht % 2 != 0)
2220
0
                return IHEVCD_INVALID_PARAMETER;
2221
32.7k
        }
2222
3.76k
        else if(CHROMA_FMT_IDC_YUV422 == ps_sps->i1_chroma_format_idc)
2223
3.12k
        {
2224
3.12k
            if(disp_wd % 2 != 0)
2225
0
                return IHEVCD_INVALID_PARAMETER;
2226
3.12k
        }
2227
2228
36.5k
        if((0 != ps_codec->u4_allocate_dynamic_done) &&
2229
13.8k
                            ((ps_codec->i4_disp_wd != disp_wd) ||
2230
13.6k
                            (ps_codec->i4_disp_ht != disp_ht) ||
2231
13.1k
                            (ps_codec->i4_bit_depth_luma != ps_sps->i1_bit_depth_luma_minus8 + 8) ||
2232
13.1k
                            (ps_codec->i4_bit_depth_chroma != ps_sps->i1_bit_depth_chroma_minus8 + 8) ||
2233
13.1k
                            (ps_codec->i4_chroma_format_idc != ps_sps->i1_chroma_format_idc)))
2234
777
        {
2235
777
            if(0 == ps_codec->i4_first_pic_done)
2236
192
            {
2237
192
                return IHEVCD_INVALID_PARAMETER;
2238
192
            }
2239
2240
            // Do not support change in bit-depth once decoder is initialized
2241
585
            if(ps_codec->i4_bit_depth_luma != ps_sps->i1_bit_depth_luma_minus8 + 8 ||
2242
584
               ps_codec->i4_bit_depth_chroma != ps_sps->i1_bit_depth_chroma_minus8 + 8)
2243
20
            {
2244
20
                return (IHEVCD_ERROR_T)IHEVCD_UNSUPPORTED_BIT_DEPTH;
2245
20
            }
2246
2247
            // Do not support change in chroma format once decoder is initialized
2248
565
            if(ps_codec->i4_chroma_format_idc != ps_sps->i1_chroma_format_idc)
2249
39
            {
2250
39
                return (IHEVCD_ERROR_T)IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
2251
39
            }
2252
2253
526
            ps_codec->i4_reset_flag = 1;
2254
526
            return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
2255
565
        }
2256
2257
35.7k
        ps_codec->i4_disp_wd = disp_wd;
2258
35.7k
        ps_codec->i4_disp_ht = disp_ht;
2259
2260
35.7k
        ps_codec->i4_chroma_format_idc = ps_sps->i1_chroma_format_idc;
2261
35.7k
        ps_codec->i4_bit_depth_luma = ps_sps->i1_bit_depth_luma_minus8 + 8;
2262
35.7k
        ps_codec->i4_pixel_size_y   = 1 + (ps_sps->i1_bit_depth_luma_minus8 > 0);
2263
35.7k
        ps_codec->i4_qp_bd_offset_y = 6 * ps_sps->i1_bit_depth_luma_minus8;
2264
2265
35.7k
        ps_codec->i4_bit_depth_chroma = ps_sps->i1_bit_depth_chroma_minus8 + 8;
2266
35.7k
        ps_codec->i4_pixel_size_uv    = 1 + (ps_sps->i1_bit_depth_chroma_minus8 > 0);
2267
35.7k
        ps_codec->i4_qp_bd_offset_uv  = 6 * ps_sps->i1_bit_depth_chroma_minus8;
2268
2269
2270
35.7k
        ps_codec->i4_wd = ps_sps->i2_pic_width_in_luma_samples;
2271
35.7k
        ps_codec->i4_ht = ps_sps->i2_pic_height_in_luma_samples;
2272
35.7k
        ps_codec->u4_num_8x8_blks = ALIGN64(ps_codec->i4_wd) * ALIGN64(ps_codec->i4_ht) >> 6;
2273
2274
35.7k
        {
2275
35.7k
            WORD32 ref_strd;
2276
35.7k
            ref_strd = ALIGN32(ps_sps->i2_pic_width_in_luma_samples + PAD_WD);
2277
35.7k
            if(ps_codec->i4_strd < ref_strd)
2278
22.6k
            {
2279
22.6k
                ps_codec->i4_strd = ref_strd;
2280
22.6k
            }
2281
35.7k
        }
2282
2283
35.7k
        if(0 == ps_codec->i4_share_disp_buf)
2284
35.7k
        {
2285
35.7k
            if(ps_codec->i4_disp_strd < ps_codec->i4_disp_wd)
2286
22.6k
            {
2287
22.6k
                ps_codec->i4_disp_strd = ps_codec->i4_disp_wd;
2288
22.6k
            }
2289
35.7k
        }
2290
0
        else
2291
0
        {
2292
0
            if(ps_codec->i4_disp_strd < ps_codec->i4_strd)
2293
0
            {
2294
0
                ps_codec->i4_disp_strd = ps_codec->i4_strd;
2295
0
            }
2296
0
        }
2297
35.7k
    }
2298
2299
    /* This is used only during initialization to get reorder count etc */
2300
0
    ps_codec->i4_sps_id = sps_id;
2301
2302
35.7k
    ps_codec->i4_sps_done = 1;
2303
35.7k
    return ret;
2304
36.5k
}
2305
2306
2307
void ihevcd_unmark_pps(codec_t *ps_codec, WORD32 sps_id)
2308
0
{
2309
0
    WORD32 pps_id = 0;
2310
0
    pps_t *ps_pps = ps_codec->ps_pps_base;
2311
2312
0
    for(pps_id = 0; pps_id < MAX_PPS_CNT - 1; pps_id++, ps_pps++)
2313
0
    {
2314
0
        if((ps_pps->i1_pps_valid) &&
2315
0
                        (ps_pps->i1_sps_id == sps_id))
2316
0
            ps_pps->i1_pps_valid = 0;
2317
0
    }
2318
0
}
2319
2320
2321
void ihevcd_copy_sps(codec_t *ps_codec, WORD32 sps_id, WORD32 sps_id_ref)
2322
35.7k
{
2323
35.7k
    sps_t *ps_sps, *ps_sps_ref;
2324
35.7k
    WORD16 *pi2_scaling_mat_backup;
2325
35.7k
    WORD32 scaling_mat_size;
2326
2327
35.7k
    SCALING_MAT_SIZE(scaling_mat_size);
2328
35.7k
    ps_sps_ref = ps_codec->ps_sps_base + sps_id_ref;
2329
35.7k
    ps_sps = ps_codec->ps_sps_base + sps_id;
2330
2331
35.7k
    if(ps_sps->i1_sps_valid)
2332
13.0k
    {
2333
13.0k
        if((ps_sps->i1_log2_ctb_size != ps_sps_ref->i1_log2_ctb_size) ||
2334
13.0k
                        (ps_sps->i2_pic_wd_in_ctb != ps_sps_ref->i2_pic_wd_in_ctb) ||
2335
13.0k
                        (ps_sps->i2_pic_ht_in_ctb != ps_sps_ref->i2_pic_ht_in_ctb))
2336
0
        {
2337
0
            ihevcd_unmark_pps(ps_codec, sps_id);
2338
0
        }
2339
13.0k
    }
2340
2341
35.7k
    pi2_scaling_mat_backup = ps_sps->pi2_scaling_mat;
2342
2343
35.7k
    memcpy(ps_sps, ps_sps_ref, sizeof(sps_t));
2344
35.7k
    ps_sps->pi2_scaling_mat = pi2_scaling_mat_backup;
2345
35.7k
    memcpy(ps_sps->pi2_scaling_mat, ps_sps_ref->pi2_scaling_mat, scaling_mat_size * sizeof(WORD16));
2346
35.7k
    ps_sps->i1_sps_valid = 1;
2347
2348
35.7k
    ps_codec->s_parse.ps_sps = ps_sps;
2349
35.7k
}
2350
2351
2352
/**
2353
*******************************************************************************
2354
*
2355
* @brief
2356
*  Parses PPS (Picture Parameter Set)
2357
*
2358
* @par Description:
2359
*  Parse Picture Parameter Set as per section  Section: 7.3.2.3
2360
* The pps is written to a temporary buffer and copied later to the
2361
* appropriate location
2362
*
2363
* @param[in] ps_codec
2364
*  Pointer to codec context
2365
*
2366
* @returns Error code from IHEVCD_ERROR_T
2367
*
2368
* @remarks
2369
*
2370
*
2371
*******************************************************************************
2372
*/
2373
IHEVCD_ERROR_T ihevcd_parse_pps(codec_t *ps_codec)
2374
78.9k
{
2375
78.9k
    IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
2376
78.9k
    UWORD32 value;
2377
78.9k
    WORD32 i4_value;
2378
78.9k
    WORD32 pps_id;
2379
2380
78.9k
    pps_t *ps_pps;
2381
78.9k
    sps_t *ps_sps;
2382
78.9k
    bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
2383
2384
2385
78.9k
    if(0 == ps_codec->i4_sps_done)
2386
9.76k
        return IHEVCD_INVALID_HEADER;
2387
2388
69.2k
    UEV_PARSE("pic_parameter_set_id", value, ps_bitstrm);
2389
2390
69.2k
    pps_id = value;
2391
69.2k
    if((pps_id >= MAX_PPS_CNT) || (pps_id < 0))
2392
5.65k
    {
2393
5.65k
        if(ps_codec->i4_pps_done)
2394
1.08k
            return IHEVCD_UNSUPPORTED_PPS_ID;
2395
4.57k
        else
2396
4.57k
            pps_id = 0;
2397
5.65k
    }
2398
2399
2400
68.1k
    ps_pps = (ps_codec->s_parse.ps_pps_base + MAX_PPS_CNT - 1);
2401
    /* Reset PPS to zero */
2402
68.1k
    {
2403
68.1k
        WORD16 *pi2_scaling_mat = ps_pps->pi2_scaling_mat;
2404
68.1k
        tile_t *ps_tile = ps_pps->ps_tile;
2405
68.1k
        memset(ps_pps, 0, sizeof(pps_t));
2406
68.1k
        ps_pps->pi2_scaling_mat = pi2_scaling_mat;
2407
68.1k
        ps_pps->ps_tile = ps_tile;
2408
68.1k
    }
2409
2410
68.1k
    ps_pps->i1_pps_id = pps_id;
2411
2412
68.1k
    UEV_PARSE("seq_parameter_set_id", value, ps_bitstrm);
2413
68.1k
    if(value > MAX_SPS_CNT - 2)
2414
721
    {
2415
721
        return IHEVCD_INVALID_PARAMETER;
2416
721
    }
2417
67.4k
    ps_pps->i1_sps_id = value;
2418
2419
67.4k
    ps_sps = (ps_codec->s_parse.ps_sps_base + ps_pps->i1_sps_id);
2420
2421
    /* If the SPS that is being referred to has not been parsed,
2422
     * copy an existing SPS to the current location */
2423
67.4k
    if(0 == ps_sps->i1_sps_valid)
2424
2.61k
    {
2425
2.61k
        return IHEVCD_INVALID_HEADER;
2426
2427
/*
2428
        sps_t *ps_sps_ref = ps_codec->ps_sps_base;
2429
        while(0 == ps_sps_ref->i1_sps_valid)
2430
            ps_sps_ref++;
2431
        ihevcd_copy_sps(ps_codec, ps_pps->i1_sps_id, ps_sps_ref->i1_sps_id);
2432
*/
2433
2.61k
    }
2434
2435
64.8k
    BITS_PARSE("dependent_slices_enabled_flag", value, ps_bitstrm, 1);
2436
64.8k
    ps_pps->i1_dependent_slice_enabled_flag = value;
2437
2438
64.8k
    BITS_PARSE("output_flag_present_flag", value, ps_bitstrm, 1);
2439
64.8k
    ps_pps->i1_output_flag_present_flag = value;
2440
2441
64.8k
    BITS_PARSE("num_extra_slice_header_bits", value, ps_bitstrm, 3);
2442
64.8k
    ps_pps->i1_num_extra_slice_header_bits = value;
2443
2444
2445
64.8k
    BITS_PARSE("sign_data_hiding_flag", value, ps_bitstrm, 1);
2446
64.8k
    ps_pps->i1_sign_data_hiding_flag = value;
2447
2448
64.8k
    BITS_PARSE("cabac_init_present_flag", value, ps_bitstrm, 1);
2449
64.8k
    ps_pps->i1_cabac_init_present_flag = value;
2450
2451
64.8k
    UEV_PARSE("num_ref_idx_l0_default_active_minus1", value, ps_bitstrm);
2452
64.8k
    if(value > MAX_DPB_SIZE - 2)
2453
720
    {
2454
720
         return IHEVCD_INVALID_PARAMETER;
2455
720
    }
2456
64.0k
    ps_pps->i1_num_ref_idx_l0_default_active = value + 1;
2457
2458
64.0k
    UEV_PARSE("num_ref_idx_l1_default_active_minus1", value, ps_bitstrm);
2459
64.0k
    if(value > MAX_DPB_SIZE - 2)
2460
292
    {
2461
292
        return IHEVCD_INVALID_PARAMETER;
2462
292
    }
2463
63.7k
    ps_pps->i1_num_ref_idx_l1_default_active = value + 1;
2464
2465
63.7k
    SEV_PARSE("pic_init_qp_minus26", i4_value, ps_bitstrm);
2466
63.7k
    if(i4_value < -26 || i4_value > 25)
2467
486
    {
2468
486
        return IHEVCD_INVALID_PARAMETER;
2469
486
    }
2470
63.3k
    ps_pps->i1_pic_init_qp = i4_value + 26;
2471
2472
63.3k
    BITS_PARSE("constrained_intra_pred_flag", value, ps_bitstrm, 1);
2473
63.3k
    ps_pps->i1_constrained_intra_pred_flag = value;
2474
2475
63.3k
    BITS_PARSE("transform_skip_enabled_flag", value, ps_bitstrm, 1);
2476
63.3k
    ps_pps->i1_transform_skip_enabled_flag = value;
2477
2478
63.3k
    BITS_PARSE("cu_qp_delta_enabled_flag", value, ps_bitstrm, 1);
2479
63.3k
    ps_pps->i1_cu_qp_delta_enabled_flag = value;
2480
2481
63.3k
    if(ps_pps->i1_cu_qp_delta_enabled_flag)
2482
18.9k
    {
2483
18.9k
        UEV_PARSE("diff_cu_qp_delta_depth", value, ps_bitstrm);
2484
18.9k
        if(value > ps_sps->i1_log2_diff_max_min_coding_block_size)
2485
755
        {
2486
755
            return IHEVCD_INVALID_PARAMETER;
2487
755
        }
2488
18.2k
        ps_pps->i1_diff_cu_qp_delta_depth = value;
2489
18.2k
    }
2490
44.3k
    else
2491
44.3k
    {
2492
44.3k
        ps_pps->i1_diff_cu_qp_delta_depth = 0;
2493
44.3k
    }
2494
62.5k
    ps_pps->i1_log2_min_cu_qp_delta_size = ps_sps->i1_log2_ctb_size - ps_pps->i1_diff_cu_qp_delta_depth;
2495
    /* Print different */
2496
62.5k
    SEV_PARSE("cb_qp_offset", i4_value, ps_bitstrm);
2497
62.5k
    if(i4_value < -12 || i4_value > 12)
2498
320
    {
2499
320
        return IHEVCD_INVALID_PARAMETER;
2500
320
    }
2501
62.2k
    ps_pps->i1_pic_cb_qp_offset = i4_value;
2502
2503
    /* Print different */
2504
62.2k
    SEV_PARSE("cr_qp_offset", i4_value, ps_bitstrm);
2505
62.2k
    if(i4_value < -12 || i4_value > 12)
2506
351
    {
2507
351
        return IHEVCD_INVALID_PARAMETER;
2508
351
    }
2509
61.8k
    ps_pps->i1_pic_cr_qp_offset = i4_value;
2510
2511
    /* Print different */
2512
61.8k
    BITS_PARSE("slicelevel_chroma_qp_flag", value, ps_bitstrm, 1);
2513
61.8k
    ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag = value;
2514
2515
61.8k
    BITS_PARSE("weighted_pred_flag", value, ps_bitstrm, 1);
2516
61.8k
    ps_pps->i1_weighted_pred_flag = value;
2517
2518
61.8k
    BITS_PARSE("weighted_bipred_flag", value, ps_bitstrm, 1);
2519
61.8k
    ps_pps->i1_weighted_bipred_flag = value;
2520
2521
61.8k
    BITS_PARSE("transquant_bypass_enable_flag", value, ps_bitstrm, 1);
2522
61.8k
    ps_pps->i1_transquant_bypass_enable_flag = value;
2523
2524
61.8k
    BITS_PARSE("tiles_enabled_flag", value, ps_bitstrm, 1);
2525
61.8k
    ps_pps->i1_tiles_enabled_flag = value;
2526
2527
    /* When tiles are enabled and width or height is >= 4096,
2528
     * CTB Size should at least be 32 while if width or height is >= 8192,
2529
     * CTB Size should at least be 64 and so on. 16x16 CTBs can result
2530
     * in tile position greater than 255 for 4096 while 32x32 CTBs can result
2531
     * in tile position greater than 255 for 8192,
2532
     * which decoder does not support.
2533
     */
2534
61.8k
    if (ps_pps->i1_tiles_enabled_flag)
2535
9.16k
    {
2536
9.16k
        if((ps_sps->i1_log2_ctb_size == 4) &&
2537
1.48k
            ((ps_sps->i2_pic_width_in_luma_samples >= 4096) ||
2538
1.40k
            (ps_sps->i2_pic_height_in_luma_samples >= 4096)))
2539
157
        {
2540
157
            return IHEVCD_INVALID_HEADER;
2541
157
        }
2542
9.00k
        if((ps_sps->i1_log2_ctb_size == 5) &&
2543
3.08k
            ((ps_sps->i2_pic_width_in_luma_samples >= 8192) ||
2544
3.00k
            (ps_sps->i2_pic_height_in_luma_samples >= 8192)))
2545
150
        {
2546
150
            return IHEVCD_INVALID_HEADER;
2547
150
        }
2548
8.85k
        if((ps_sps->i1_log2_ctb_size == 6) &&
2549
4.59k
            ((ps_sps->i2_pic_width_in_luma_samples >= 16384) ||
2550
4.59k
            (ps_sps->i2_pic_height_in_luma_samples >= 16384)))
2551
0
        {
2552
0
            return IHEVCD_INVALID_HEADER;
2553
0
        }
2554
8.85k
    }
2555
2556
61.5k
    BITS_PARSE("entropy_coding_sync_enabled_flag", value, ps_bitstrm, 1);
2557
61.5k
    ps_pps->i1_entropy_coding_sync_enabled_flag = value;
2558
2559
61.5k
    ps_pps->i1_loop_filter_across_tiles_enabled_flag = 0;
2560
61.5k
    if(ps_pps->i1_tiles_enabled_flag)
2561
8.85k
    {
2562
8.85k
        WORD32 wd = ALIGN64(ps_codec->i4_wd);
2563
8.85k
        WORD32 ht = ALIGN64(ps_codec->i4_ht);
2564
2565
8.85k
        WORD32 max_tile_cols = (wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
2566
8.85k
        WORD32 max_tile_rows = (ht + MIN_TILE_HT - 1) / MIN_TILE_HT;
2567
2568
8.85k
        UEV_PARSE("num_tile_columns_minus1", value, ps_bitstrm);
2569
8.85k
        ps_pps->i1_num_tile_columns = value + 1;
2570
2571
8.85k
        UEV_PARSE("num_tile_rows_minus1", value, ps_bitstrm);
2572
8.85k
        ps_pps->i1_num_tile_rows = value + 1;
2573
2574
8.85k
        if((ps_pps->i1_num_tile_columns < 1) ||
2575
8.74k
                        (ps_pps->i1_num_tile_columns > max_tile_cols) ||
2576
8.20k
                        (ps_pps->i1_num_tile_rows < 1) ||
2577
8.09k
                        (ps_pps->i1_num_tile_rows > max_tile_rows))
2578
909
            return IHEVCD_INVALID_HEADER;
2579
2580
7.94k
        BITS_PARSE("uniform_spacing_flag", value, ps_bitstrm, 1);
2581
7.94k
        ps_pps->i1_uniform_spacing_flag = value;
2582
2583
2584
7.94k
        {
2585
2586
7.94k
            UWORD32 start;
2587
7.94k
            WORD32 i, j;
2588
2589
2590
7.94k
            start = 0;
2591
24.4k
            for(i = 0; i < ps_pps->i1_num_tile_columns; i++)
2592
16.6k
            {
2593
16.6k
                tile_t *ps_tile;
2594
16.6k
                if(!ps_pps->i1_uniform_spacing_flag)
2595
6.54k
                {
2596
6.54k
                    if(i < (ps_pps->i1_num_tile_columns - 1))
2597
3.64k
                    {
2598
3.64k
                        UEV_PARSE("column_width_minus1[ i ]", value, ps_bitstrm);
2599
3.64k
                        value += 1;
2600
3.64k
                        if (value >= ps_sps->i2_pic_wd_in_ctb - start)
2601
125
                        {
2602
125
                            return IHEVCD_INVALID_HEADER;
2603
125
                        }
2604
3.64k
                    }
2605
2.89k
                    else
2606
2.89k
                    {
2607
2.89k
                        value = ps_sps->i2_pic_wd_in_ctb - start;
2608
2.89k
                    }
2609
6.54k
                }
2610
10.1k
                else
2611
10.1k
                {
2612
10.1k
                    value = ((i + 1) * ps_sps->i2_pic_wd_in_ctb) / ps_pps->i1_num_tile_columns -
2613
10.1k
                                    (i * ps_sps->i2_pic_wd_in_ctb) / ps_pps->i1_num_tile_columns;
2614
10.1k
                }
2615
2616
85.1k
                for(j = 0; j < ps_pps->i1_num_tile_rows; j++)
2617
68.5k
                {
2618
68.5k
                    ps_tile = ps_pps->ps_tile + j * ps_pps->i1_num_tile_columns + i;
2619
68.5k
                    ps_tile->u1_pos_x = start;
2620
68.5k
                    ps_tile->u2_wd = value;
2621
68.5k
                }
2622
16.5k
                start += value;
2623
2624
16.5k
                if((start > ps_sps->i2_pic_wd_in_ctb) ||
2625
16.5k
                                (value == 0))
2626
0
                    return IHEVCD_INVALID_HEADER;
2627
16.5k
            }
2628
2629
7.81k
            start = 0;
2630
46.9k
            for(i = 0; i < (ps_pps->i1_num_tile_rows); i++)
2631
39.2k
            {
2632
39.2k
                tile_t *ps_tile;
2633
39.2k
                if(!ps_pps->i1_uniform_spacing_flag)
2634
4.99k
                {
2635
4.99k
                    if(i < (ps_pps->i1_num_tile_rows - 1))
2636
2.23k
                    {
2637
2638
2.23k
                        UEV_PARSE("row_height_minus1[ i ]", value, ps_bitstrm);
2639
2.23k
                        value += 1;
2640
2.23k
                        if (value >= ps_sps->i2_pic_ht_in_ctb - start)
2641
137
                        {
2642
137
                            return IHEVCD_INVALID_HEADER;
2643
137
                        }
2644
2.23k
                    }
2645
2.76k
                    else
2646
2.76k
                    {
2647
2.76k
                        value = ps_sps->i2_pic_ht_in_ctb - start;
2648
2.76k
                    }
2649
4.99k
                }
2650
34.2k
                else
2651
34.2k
                {
2652
34.2k
                    value = ((i + 1) * ps_sps->i2_pic_ht_in_ctb) / ps_pps->i1_num_tile_rows -
2653
34.2k
                                    (i * ps_sps->i2_pic_ht_in_ctb) / ps_pps->i1_num_tile_rows;
2654
34.2k
                }
2655
2656
106k
                for(j = 0; j < ps_pps->i1_num_tile_columns; j++)
2657
67.3k
                {
2658
67.3k
                    ps_tile = ps_pps->ps_tile + i * ps_pps->i1_num_tile_columns + j;
2659
67.3k
                    ps_tile->u1_pos_y = start;
2660
67.3k
                    ps_tile->u2_ht = value;
2661
67.3k
                }
2662
39.1k
                start += value;
2663
2664
39.1k
                if((start > ps_sps->i2_pic_ht_in_ctb) ||
2665
39.1k
                                (value == 0))
2666
0
                    return IHEVCD_INVALID_HEADER;
2667
39.1k
            }
2668
7.81k
        }
2669
2670
2671
7.68k
        BITS_PARSE("loop_filter_across_tiles_enabled_flag", value, ps_bitstrm, 1);
2672
7.68k
        ps_pps->i1_loop_filter_across_tiles_enabled_flag = value;
2673
2674
7.68k
    }
2675
52.7k
    else
2676
52.7k
    {
2677
        /* If tiles are not present, set first tile in each PPS to have tile
2678
        width and height equal to picture width and height */
2679
52.7k
        ps_pps->i1_num_tile_columns = 1;
2680
52.7k
        ps_pps->i1_num_tile_rows = 1;
2681
52.7k
        ps_pps->i1_uniform_spacing_flag = 1;
2682
2683
52.7k
        ps_pps->ps_tile->u1_pos_x = 0;
2684
52.7k
        ps_pps->ps_tile->u1_pos_y = 0;
2685
52.7k
        ps_pps->ps_tile->u2_wd = ps_sps->i2_pic_wd_in_ctb;
2686
52.7k
        ps_pps->ps_tile->u2_ht = ps_sps->i2_pic_ht_in_ctb;
2687
52.7k
    }
2688
2689
60.3k
    BITS_PARSE("loop_filter_across_slices_enabled_flag", value, ps_bitstrm, 1);
2690
60.3k
    ps_pps->i1_loop_filter_across_slices_enabled_flag = value;
2691
2692
60.3k
    BITS_PARSE("deblocking_filter_control_present_flag", value, ps_bitstrm, 1);
2693
60.3k
    ps_pps->i1_deblocking_filter_control_present_flag = value;
2694
2695
    /* Default values */
2696
60.3k
    ps_pps->i1_pic_disable_deblocking_filter_flag = 0;
2697
60.3k
    ps_pps->i1_deblocking_filter_override_enabled_flag = 0;
2698
60.3k
    ps_pps->i1_beta_offset_div2 = 0;
2699
60.3k
    ps_pps->i1_tc_offset_div2 = 0;
2700
2701
60.3k
    if(ps_pps->i1_deblocking_filter_control_present_flag)
2702
9.33k
    {
2703
2704
9.33k
        BITS_PARSE("deblocking_filter_override_enabled_flag", value, ps_bitstrm, 1);
2705
9.33k
        ps_pps->i1_deblocking_filter_override_enabled_flag = value;
2706
2707
9.33k
        BITS_PARSE("pic_disable_deblocking_filter_flag", value, ps_bitstrm, 1);
2708
9.33k
        ps_pps->i1_pic_disable_deblocking_filter_flag = value;
2709
2710
9.33k
        if(!ps_pps->i1_pic_disable_deblocking_filter_flag)
2711
4.93k
        {
2712
2713
4.93k
            SEV_PARSE("pps_beta_offset_div2", i4_value, ps_bitstrm);
2714
4.93k
            if(i4_value < -6 || i4_value > 6)
2715
270
            {
2716
270
                return IHEVCD_INVALID_PARAMETER;
2717
270
            }
2718
4.66k
            ps_pps->i1_beta_offset_div2 = i4_value;
2719
2720
4.66k
            SEV_PARSE("pps_tc_offset_div2", i4_value, ps_bitstrm);
2721
4.66k
            if(i4_value < -6 || i4_value > 6)
2722
227
            {
2723
227
                return IHEVCD_INVALID_PARAMETER;
2724
227
            }
2725
4.43k
            ps_pps->i1_tc_offset_div2 = i4_value;
2726
2727
4.43k
        }
2728
9.33k
    }
2729
2730
59.9k
    BITS_PARSE("pps_scaling_list_data_present_flag", value, ps_bitstrm, 1);
2731
59.9k
    ps_pps->i1_pps_scaling_list_data_present_flag = value;
2732
2733
59.9k
    if(ps_pps->i1_pps_scaling_list_data_present_flag)
2734
15.1k
    {
2735
15.1k
        COPY_DEFAULT_SCALING_LIST(ps_pps->pi2_scaling_mat);
2736
15.1k
        ihevcd_scaling_list_data(ps_codec, ps_pps->pi2_scaling_mat, ps_sps->i1_chroma_format_idc);
2737
15.1k
    }
2738
2739
59.9k
    BITS_PARSE("lists_modification_present_flag", value, ps_bitstrm, 1);
2740
59.9k
    ps_pps->i1_lists_modification_present_flag = value;
2741
59.9k
    UEV_PARSE("log2_parallel_merge_level_minus2", value, ps_bitstrm);
2742
59.9k
    if(value > (ps_sps->i1_log2_min_coding_block_size + ps_sps->i1_log2_diff_max_min_coding_block_size))
2743
1.05k
    {
2744
1.05k
        return IHEVCD_INVALID_PARAMETER;
2745
1.05k
    }
2746
58.8k
    ps_pps->i1_log2_parallel_merge_level = value + 2;
2747
2748
58.8k
    BITS_PARSE("slice_header_extension_present_flag", value, ps_bitstrm, 1);
2749
58.8k
    ps_pps->i1_slice_header_extension_present_flag = value;
2750
2751
58.8k
    BITS_PARSE("pps_extension_present_flag", value, ps_bitstrm, 1);
2752
58.8k
    ps_pps->i1_pps_extension_present_flag = value;
2753
2754
58.8k
    if(ps_pps->i1_pps_extension_present_flag)
2755
5.68k
    {
2756
5.68k
        BITS_PARSE("pps_range_extension_flag", value, ps_bitstrm, 1);
2757
5.68k
        ps_pps->i1_pps_range_extension_flag = value;
2758
2759
5.68k
        BITS_PARSE("pps_multilayer_extension_flag", value, ps_bitstrm, 1);
2760
5.68k
        ps_pps->i1_pps_multilayer_extension_flag = value;
2761
2762
5.68k
        BITS_PARSE("pps_3d_extension_flag", value, ps_bitstrm, 1);
2763
5.68k
        ps_pps->i1_pps_3d_extension_flag = value;
2764
2765
5.68k
        BITS_PARSE("pps_scc_extension_flag", value, ps_bitstrm, 1);
2766
5.68k
        ps_pps->i1_pps_scc_extension_flag = value;
2767
2768
5.68k
        BITS_PARSE("pps_extension_4bits", value, ps_bitstrm, 4);
2769
5.68k
        ps_pps->i1_pps_extension_4bits = value;
2770
5.68k
    }
2771
53.1k
    else
2772
53.1k
    {
2773
53.1k
        ps_pps->i1_pps_range_extension_flag = 0;
2774
53.1k
        ps_pps->i1_pps_multilayer_extension_flag = 0;
2775
53.1k
        ps_pps->i1_pps_3d_extension_flag = 0;
2776
53.1k
        ps_pps->i1_pps_scc_extension_flag = 0;
2777
53.1k
        ps_pps->i1_pps_extension_4bits = 0;
2778
53.1k
    }
2779
2780
58.8k
#ifdef ENABLE_MAIN_REXT_PROFILE
2781
58.8k
    if(ps_pps->i1_pps_range_extension_flag)
2782
3.20k
    {
2783
        /* ITU-T H.265 Section 7.3.2.3.2 Range extension PPS syntax */
2784
3.20k
        if(ps_pps->i1_transform_skip_enabled_flag)
2785
1.50k
        {
2786
1.50k
            UEV_PARSE("log2_max_transform_skip_block_size_minus2", value, ps_bitstrm);
2787
1.50k
            ps_pps->i1_log2_max_transform_skip_block_size_minus2 = value;
2788
1.50k
        }
2789
2790
3.20k
        BITS_PARSE("cross_component_prediction_enabled_flag", value, ps_bitstrm, 1);
2791
3.20k
        ps_pps->i1_cross_component_prediction_enabled_flag = value;
2792
2793
        // Parse other bits to maintain bitstream alignment
2794
3.20k
        BITS_PARSE("chroma_qp_offset_list_enabled_flag", value, ps_bitstrm, 1);
2795
3.20k
        ps_pps->i1_chroma_qp_offset_list_enabled_flag = value;
2796
2797
3.20k
        if(ps_pps->i1_chroma_qp_offset_list_enabled_flag)
2798
2.53k
        {
2799
2.53k
            UEV_PARSE("diff_cu_chroma_qp_offset_depth", value, ps_bitstrm);
2800
2.53k
            ps_pps->i4_diff_cu_chroma_qp_offset_depth = value;
2801
2802
2.53k
            UEV_PARSE("chroma_qp_offset_list_len_minus1", value, ps_bitstrm);
2803
2.53k
            ps_pps->i4_chroma_qp_offset_list_len_minus1 = value;
2804
2.53k
            if(ps_pps->i4_chroma_qp_offset_list_len_minus1 < 0
2805
2.52k
                            || ps_pps->i4_chroma_qp_offset_list_len_minus1 > 5)
2806
154
            {
2807
154
                return IHEVCD_INVALID_PARAMETER;
2808
154
            }
2809
2810
5.30k
            for(int i = 0; i <= ps_pps->i4_chroma_qp_offset_list_len_minus1; i++)
2811
2.92k
            {
2812
                // cb_qp_offset_list[i] (se-v)
2813
2.92k
                SEV_PARSE("cb_qp_offset_list", value, ps_bitstrm);
2814
2.92k
                ps_pps->i4_cb_qp_offset_list[i] = value;
2815
2816
                // cr_qp_offset_list[i] (se-v)
2817
2.92k
                SEV_PARSE("cr_qp_offset_list", value, ps_bitstrm);
2818
2.92k
                ps_pps->i4_cr_qp_offset_list[i] = value;
2819
2.92k
            }
2820
2.38k
        }
2821
2822
        // log2_sao_offset_scale_luma (ue-v)
2823
3.04k
        UEV_PARSE("log2_sao_ofst_scale_luma", value, ps_bitstrm);
2824
3.04k
        ps_pps->i1_log2_sao_ofst_scale_luma = value;
2825
2826
        // log2_sao_offset_scale_chroma (ue-v)
2827
3.04k
        UEV_PARSE("log2_sao_ofst_scale_chroma", value, ps_bitstrm);
2828
3.04k
        ps_pps->i1_log2_sao_ofst_scale_chroma = value;
2829
3.04k
    }
2830
2831
58.6k
    WORD32 profile = ihevcd_get_profile(&ps_sps->s_ptl.s_ptl_gen);
2832
58.6k
    if(profile != IHEVC_PROFILE_MAIN_REXT)
2833
43.2k
    {
2834
43.2k
        if(ps_pps->i1_log2_max_transform_skip_block_size_minus2
2835
43.0k
                        || ps_pps->i1_cross_component_prediction_enabled_flag
2836
42.7k
                        || ps_pps->i1_chroma_qp_offset_list_enabled_flag
2837
42.7k
                        || ps_pps->i1_log2_sao_ofst_scale_luma
2838
42.6k
                        || ps_pps->i1_log2_sao_ofst_scale_chroma)
2839
754
        {
2840
754
            return IHEVCD_INVALID_PARAMETER;
2841
754
        }
2842
43.2k
    }
2843
57.9k
    if(ps_pps->i1_log2_sao_ofst_scale_luma
2844
57.7k
                   || ps_pps->i1_log2_sao_ofst_scale_chroma)
2845
272
    {
2846
        // main-rext 8-bit profiles require these fields to be off
2847
272
        return IHEVCD_INVALID_PARAMETER;
2848
272
    }
2849
2850
57.6k
    if(ps_pps->i1_pps_multilayer_extension_flag || ps_pps->i1_pps_3d_extension_flag
2851
57.1k
                    || ps_pps->i1_pps_scc_extension_flag)
2852
631
    {
2853
        // TODO: add support for parsing these syntax elements
2854
631
        return IHEVCD_UNSUPPORTED_TOOL_SET;
2855
631
    }
2856
#else
2857
    if(ps_pps->i1_pps_range_extension_flag || ps_pps->i1_pps_multilayer_extension_flag
2858
                    || ps_pps->i1_pps_3d_extension_flag || ps_pps->i1_pps_scc_extension_flag)
2859
    {
2860
        // TODO: add support for parsing these syntax elements
2861
        return IHEVCD_UNSUPPORTED_TOOL_SET;
2862
    }
2863
#endif
2864
2865
57.0k
    if((UWORD8 *)ps_bitstrm->pu4_buf > ps_bitstrm->pu1_buf_max)
2866
164
        return IHEVCD_INVALID_PARAMETER;
2867
2868
56.8k
    ps_codec->i4_pps_done = 1;
2869
56.8k
    return ret;
2870
57.0k
}
2871
2872
2873
void ihevcd_copy_pps(codec_t *ps_codec, WORD32 pps_id, WORD32 pps_id_ref)
2874
71.7k
{
2875
71.7k
    pps_t *ps_pps, *ps_pps_ref;
2876
71.7k
    WORD16 *pi2_scaling_mat_backup;
2877
71.7k
    WORD32 scaling_mat_size;
2878
71.7k
    tile_t *ps_tile_backup;
2879
71.7k
    WORD32 max_tile_cols, max_tile_rows;
2880
71.7k
    WORD32 wd, ht;
2881
71.7k
    wd = ALIGN64(ps_codec->i4_wd);
2882
71.7k
    ht = ALIGN64(ps_codec->i4_ht);
2883
2884
71.7k
    SCALING_MAT_SIZE(scaling_mat_size);
2885
71.7k
    max_tile_cols = (wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
2886
71.7k
    max_tile_rows = (ht + MIN_TILE_HT - 1) / MIN_TILE_HT;
2887
2888
71.7k
    ps_pps_ref = ps_codec->ps_pps_base + pps_id_ref;
2889
71.7k
    ps_pps = ps_codec->ps_pps_base + pps_id;
2890
2891
71.7k
    pi2_scaling_mat_backup = ps_pps->pi2_scaling_mat;
2892
71.7k
    ps_tile_backup = ps_pps->ps_tile;
2893
2894
71.7k
    memcpy(ps_pps, ps_pps_ref, sizeof(pps_t));
2895
71.7k
    ps_pps->pi2_scaling_mat = pi2_scaling_mat_backup;
2896
71.7k
    ps_pps->ps_tile = ps_tile_backup;
2897
71.7k
    memcpy(ps_pps->pi2_scaling_mat, ps_pps_ref->pi2_scaling_mat, scaling_mat_size * sizeof(WORD16));
2898
71.7k
    memcpy(ps_pps->ps_tile, ps_pps_ref->ps_tile, max_tile_cols * max_tile_rows * sizeof(tile_t));
2899
2900
71.7k
    ps_pps->i1_pps_valid = 1;
2901
2902
71.7k
    ps_codec->s_parse.ps_pps = ps_pps;
2903
71.7k
}
2904
2905
2906
#ifndef DISABLE_SEI
2907
IHEVCD_ERROR_T ihevcd_parse_buffering_period_sei(codec_t *ps_codec,
2908
                                                 sps_t *ps_sps)
2909
1.79k
{
2910
1.79k
    parse_ctxt_t *ps_parse = &ps_codec->s_parse;
2911
1.79k
    bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
2912
1.79k
    UWORD32 value;
2913
1.79k
    vui_t *ps_vui;
2914
1.79k
    buf_period_sei_params_t *ps_buf_period_sei_params;
2915
1.79k
    UWORD32 i;
2916
1.79k
    hrd_params_t *ps_vui_hdr;
2917
1.79k
    UWORD32 u4_cpb_cnt;
2918
2919
1.79k
    ps_vui = &ps_sps->s_vui_parameters;
2920
1.79k
    ps_vui_hdr = &ps_vui->s_vui_hrd_parameters;
2921
2922
1.79k
    ps_buf_period_sei_params = &ps_parse->s_sei_params.s_buf_period_sei_params;
2923
2924
1.79k
    ps_parse->s_sei_params.i1_buf_period_params_present_flag = 1;
2925
2926
1.79k
    UEV_PARSE("bp_seq_parameter_set_id", value, ps_bitstrm);
2927
1.79k
    if(value > MAX_SPS_CNT - 2)
2928
729
    {
2929
729
        return IHEVCD_INVALID_PARAMETER;
2930
729
    }
2931
1.06k
    ps_buf_period_sei_params->u1_bp_seq_parameter_set_id = value;
2932
2933
1.06k
    if(!ps_vui_hdr->u1_sub_pic_cpb_params_present_flag)
2934
947
    {
2935
947
        BITS_PARSE("irap_cpb_params_present_flag", value, ps_bitstrm, 1);
2936
947
        ps_buf_period_sei_params->u1_rap_cpb_params_present_flag = value;
2937
947
    }
2938
2939
1.06k
    if(ps_buf_period_sei_params->u1_rap_cpb_params_present_flag)
2940
481
    {
2941
481
        BITS_PARSE("cpb_delay_offset",
2942
481
                   value,
2943
481
                   ps_bitstrm,
2944
481
                   (ps_vui_hdr->u1_au_cpb_removal_delay_length_minus1
2945
481
                                   + 1));
2946
481
        ps_buf_period_sei_params->u4_cpb_delay_offset = value;
2947
2948
481
        BITS_PARSE("dpb_delay_offset",
2949
481
                   value,
2950
481
                   ps_bitstrm,
2951
481
                   (ps_vui_hdr->u1_dpb_output_delay_length_minus1
2952
481
                                   + 1));
2953
481
        ps_buf_period_sei_params->u4_dpb_delay_offset = value;
2954
481
    }
2955
588
    else
2956
588
    {
2957
588
        ps_buf_period_sei_params->u4_cpb_delay_offset = 0;
2958
588
        ps_buf_period_sei_params->u4_dpb_delay_offset = 0;
2959
588
    }
2960
2961
1.06k
    BITS_PARSE("concatenation_flag", value, ps_bitstrm, 1);
2962
1.06k
    ps_buf_period_sei_params->u1_concatenation_flag = value;
2963
2964
1.06k
    BITS_PARSE("au_cpb_removal_delay_delta_minus1",
2965
1.06k
               value,
2966
1.06k
               ps_bitstrm,
2967
1.06k
               (ps_vui_hdr->u1_au_cpb_removal_delay_length_minus1
2968
1.06k
                               + 1));
2969
1.06k
    ps_buf_period_sei_params->u4_au_cpb_removal_delay_delta_minus1 = value;
2970
2971
1.06k
    if(ps_vui_hdr->u1_nal_hrd_parameters_present_flag)
2972
332
    {
2973
332
        u4_cpb_cnt = ps_vui_hdr->au1_cpb_cnt_minus1[0];
2974
2975
2.40k
        for(i = 0; i <= u4_cpb_cnt; i++)
2976
2.07k
        {
2977
2.07k
            BITS_PARSE("nal_initial_cpb_removal_delay[i]",
2978
2.07k
                       value,
2979
2.07k
                       ps_bitstrm,
2980
2.07k
                       (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2981
2.07k
                                       + 1));
2982
2.07k
            ps_buf_period_sei_params->au4_nal_initial_cpb_removal_delay[i] =
2983
2.07k
                            value;
2984
2985
2.07k
            BITS_PARSE("nal_initial_cpb_removal_delay_offset",
2986
2.07k
                       value,
2987
2.07k
                       ps_bitstrm,
2988
2.07k
                       (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
2989
2.07k
                                       + 1));
2990
2.07k
            ps_buf_period_sei_params->au4_nal_initial_cpb_removal_delay_offset[i] =
2991
2.07k
                            value;
2992
2993
2.07k
            if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag
2994
1.28k
                            || ps_buf_period_sei_params->u1_rap_cpb_params_present_flag)
2995
1.17k
            {
2996
1.17k
                BITS_PARSE("nal_initial_alt_cpb_removal_delay[i]",
2997
1.17k
                           value,
2998
1.17k
                           ps_bitstrm,
2999
1.17k
                           (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
3000
1.17k
                                           + 1));
3001
1.17k
                ps_buf_period_sei_params->au4_nal_initial_alt_cpb_removal_delay[i] =
3002
1.17k
                                value;
3003
3004
1.17k
                BITS_PARSE("nal_initial_alt_cpb_removal_delay_offset",
3005
1.17k
                           value,
3006
1.17k
                           ps_bitstrm,
3007
1.17k
                           (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
3008
1.17k
                                           + 1));
3009
1.17k
                ps_buf_period_sei_params->au4_nal_initial_alt_cpb_removal_delay_offset[i] =
3010
1.17k
                                value;
3011
1.17k
            }
3012
2.07k
        }
3013
332
    }
3014
3015
1.06k
    if(ps_vui_hdr->u1_vcl_hrd_parameters_present_flag)
3016
293
    {
3017
293
        u4_cpb_cnt = ps_vui_hdr->au1_cpb_cnt_minus1[0];
3018
3019
1.55k
        for(i = 0; i <= u4_cpb_cnt; i++)
3020
1.25k
        {
3021
1.25k
            BITS_PARSE("vcl_initial_cpb_removal_delay[i]",
3022
1.25k
                       value,
3023
1.25k
                       ps_bitstrm,
3024
1.25k
                       (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
3025
1.25k
                                       + 1));
3026
1.25k
            ps_buf_period_sei_params->au4_vcl_initial_cpb_removal_delay[i] =
3027
1.25k
                            value;
3028
3029
1.25k
            BITS_PARSE("vcl_initial_cpb_removal_delay_offset",
3030
1.25k
                       value,
3031
1.25k
                       ps_bitstrm,
3032
1.25k
                       (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
3033
1.25k
                                       + 1));
3034
1.25k
            ps_buf_period_sei_params->au4_vcl_initial_cpb_removal_delay_offset[i] =
3035
1.25k
                            value;
3036
3037
1.25k
            if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag
3038
767
                            || ps_buf_period_sei_params->u1_rap_cpb_params_present_flag)
3039
706
            {
3040
706
                BITS_PARSE("vcl_initial_alt_cpb_removal_delay[i]",
3041
706
                           value,
3042
706
                           ps_bitstrm,
3043
706
                           (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
3044
706
                                           + 1));
3045
706
                ps_buf_period_sei_params->au4_vcl_initial_alt_cpb_removal_delay[i] =
3046
706
                                value;
3047
3048
706
                BITS_PARSE("vcl_initial_alt_cpb_removal_delay_offset",
3049
706
                           value,
3050
706
                           ps_bitstrm,
3051
706
                           (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
3052
706
                                           + 1));
3053
706
                ps_buf_period_sei_params->au4_vcl_initial_alt_cpb_removal_delay_offset[i] =
3054
706
                                value;
3055
706
            }
3056
1.25k
        }
3057
293
    }
3058
3059
1.06k
    return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
3060
1.79k
}
3061
3062
IHEVCD_ERROR_T ihevcd_parse_pic_timing_sei(codec_t *ps_codec, sps_t *ps_sps)
3063
2.27k
{
3064
2.27k
    parse_ctxt_t *ps_parse = &ps_codec->s_parse;
3065
2.27k
    bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
3066
2.27k
    UWORD32 value;
3067
2.27k
    vui_t *ps_vui;
3068
2.27k
    UWORD32 i;
3069
2.27k
    hrd_params_t *ps_vui_hdr;
3070
2.27k
    UWORD32 u4_cpb_dpb_delays_present_flag = 0;
3071
2.27k
    pic_timing_sei_params_t *ps_pic_timing;
3072
3073
2.27k
    ps_pic_timing = &ps_parse->s_sei_params.s_pic_timing_sei_params;
3074
2.27k
    ps_vui = &ps_sps->s_vui_parameters;
3075
2.27k
    ps_vui_hdr = &ps_vui->s_vui_hrd_parameters;
3076
2.27k
    ps_parse->s_sei_params.i1_pic_timing_params_present_flag = 1;
3077
2.27k
    if(ps_vui->u1_frame_field_info_present_flag)
3078
868
    {
3079
868
        BITS_PARSE("pic_struct", value, ps_bitstrm, 4);
3080
868
        ps_pic_timing->u4_pic_struct = value;
3081
3082
868
        BITS_PARSE("source_scan_type", value, ps_bitstrm, 2);
3083
868
        ps_pic_timing->u4_source_scan_type = value;
3084
3085
868
        BITS_PARSE("duplicate_flag", value, ps_bitstrm, 1);
3086
868
        ps_pic_timing->u1_duplicate_flag = value;
3087
868
    }
3088
3089
2.27k
    if(ps_vui_hdr->u1_nal_hrd_parameters_present_flag
3090
1.11k
                    || ps_vui_hdr->u1_vcl_hrd_parameters_present_flag)
3091
1.77k
    {
3092
1.77k
        u4_cpb_dpb_delays_present_flag = 1;
3093
1.77k
    }
3094
494
    else
3095
494
    {
3096
494
        u4_cpb_dpb_delays_present_flag = 0;
3097
494
    }
3098
3099
2.27k
    if(u4_cpb_dpb_delays_present_flag)
3100
1.77k
    {
3101
1.77k
        BITS_PARSE("au_cpb_removal_delay_minus1", value, ps_bitstrm,
3102
1.77k
                   (ps_vui_hdr->u1_au_cpb_removal_delay_length_minus1 + 1));
3103
1.77k
        ps_pic_timing->u4_au_cpb_removal_delay_minus1 = value;
3104
3105
1.77k
        BITS_PARSE("pic_dpb_output_delay", value, ps_bitstrm,
3106
1.77k
                   (ps_vui_hdr->u1_dpb_output_delay_length_minus1 + 1));
3107
1.77k
        ps_pic_timing->u4_pic_dpb_output_delay = value;
3108
3109
1.77k
        if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag)
3110
1.70k
        {
3111
1.70k
            BITS_PARSE("pic_dpb_output_du_delay", value, ps_bitstrm,
3112
1.70k
                       (ps_vui_hdr->u1_dpb_output_delay_du_length_minus1 + 1));
3113
1.70k
            ps_pic_timing->u4_pic_dpb_output_du_delay = value;
3114
1.70k
        }
3115
3116
1.77k
        if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag
3117
1.70k
                        && ps_vui_hdr->u1_sub_pic_cpb_params_in_pic_timing_sei_flag)
3118
1.50k
        {
3119
1.50k
            UWORD32 num_units_minus1;
3120
1.50k
            UWORD32 array_size;
3121
3122
1.50k
            UEV_PARSE("num_decoding_units_minus1", value, ps_bitstrm);
3123
1.50k
            if(value > (ps_sps->i4_pic_size_in_ctb -1))
3124
502
            {
3125
502
                return IHEVCD_INVALID_PARAMETER;
3126
502
            }
3127
1.00k
            ps_pic_timing->u4_num_decoding_units_minus1 = value;
3128
3129
1.00k
            num_units_minus1 = ps_pic_timing->u4_num_decoding_units_minus1;
3130
1.00k
            array_size = (sizeof(ps_pic_timing->au4_num_nalus_in_du_minus1)
3131
1.00k
                       / sizeof(ps_pic_timing->au4_num_nalus_in_du_minus1[0]));
3132
1.00k
            num_units_minus1 = CLIP3(num_units_minus1, 0,(array_size - 1));
3133
1.00k
            ps_pic_timing->u4_num_decoding_units_minus1 = num_units_minus1;
3134
3135
1.00k
            BITS_PARSE("du_common_cpb_removal_delay_flag", value, ps_bitstrm, 1);
3136
1.00k
            ps_pic_timing->u1_du_common_cpb_removal_delay_flag = value;
3137
3138
1.00k
            if(ps_pic_timing->u1_du_common_cpb_removal_delay_flag)
3139
417
            {
3140
417
                BITS_PARSE("du_common_cpb_removal_delay_increment_minus1",
3141
417
                           value,
3142
417
                           ps_bitstrm,
3143
417
                           (ps_vui_hdr->u1_du_cpb_removal_delay_increment_length_minus1
3144
417
                                           + 1));
3145
417
                ps_pic_timing->u4_du_common_cpb_removal_delay_increment_minus1 =
3146
417
                                value;
3147
417
            }
3148
3149
5.90k
            for(i = 0; i <= ps_pic_timing->u4_num_decoding_units_minus1; i++)
3150
5.28k
            {
3151
5.28k
                UEV_PARSE("num_nalus_in_du_minus1", value, ps_bitstrm);
3152
5.28k
                if(value > (ps_sps->i4_pic_size_in_ctb -1))
3153
383
                {
3154
383
                    return IHEVCD_INVALID_PARAMETER;
3155
383
                }
3156
4.89k
                ps_pic_timing->au4_num_nalus_in_du_minus1[i] = value;
3157
3158
4.89k
                if((!ps_pic_timing->u1_du_common_cpb_removal_delay_flag)
3159
1.99k
                                && (i < ps_pic_timing->u4_num_decoding_units_minus1))
3160
1.72k
                {
3161
1.72k
                    BITS_PARSE("du_common_cpb_removal_delay_increment_minus1",
3162
1.72k
                               value,
3163
1.72k
                               ps_bitstrm,
3164
1.72k
                               (ps_vui_hdr->u1_du_cpb_removal_delay_increment_length_minus1
3165
1.72k
                                               + 1));
3166
1.72k
                    ps_pic_timing->au4_du_cpb_removal_delay_increment_minus1[i] =
3167
1.72k
                                    value;
3168
1.72k
                }
3169
4.89k
            }
3170
1.00k
        }
3171
1.77k
    }
3172
3173
1.38k
    return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
3174
2.27k
}
3175
3176
IHEVCD_ERROR_T ihevcd_parse_time_code_sei(codec_t *ps_codec)
3177
1.31k
{
3178
1.31k
    parse_ctxt_t *ps_parse = &ps_codec->s_parse;
3179
1.31k
    bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
3180
1.31k
    UWORD32 value;
3181
1.31k
    time_code_t *ps_time_code;
3182
1.31k
    WORD32 i;
3183
3184
1.31k
    ps_parse->s_sei_params.i1_time_code_present_flag = 1;
3185
1.31k
    ps_time_code = &ps_parse->s_sei_params.s_time_code;
3186
3187
1.31k
    BITS_PARSE("num_clock_ts", value, ps_bitstrm, 2);
3188
1.31k
    ps_time_code->u1_num_clock_ts = value;
3189
3190
3.84k
    for(i = 0; i < ps_time_code->u1_num_clock_ts; i++)
3191
2.52k
    {
3192
2.52k
        BITS_PARSE("clock_timestamp_flag[i]", value, ps_bitstrm, 1);
3193
2.52k
        ps_time_code->au1_clock_timestamp_flag[i] = value;
3194
3195
2.52k
        if(ps_time_code->au1_clock_timestamp_flag[i])
3196
1.37k
        {
3197
1.37k
            BITS_PARSE("units_field_based_flag[i]", value, ps_bitstrm, 1);
3198
1.37k
            ps_time_code->au1_units_field_based_flag[i] = value;
3199
3200
1.37k
            BITS_PARSE("counting_type[i]", value, ps_bitstrm, 5);
3201
1.37k
            ps_time_code->au1_counting_type[i] = value;
3202
3203
1.37k
            BITS_PARSE("full_timestamp_flag[i]", value, ps_bitstrm, 1);
3204
1.37k
            ps_time_code->au1_full_timestamp_flag[i] = value;
3205
3206
1.37k
            BITS_PARSE("discontinuity_flag[i]", value, ps_bitstrm, 1);
3207
1.37k
            ps_time_code->au1_discontinuity_flag[i] = value;
3208
3209
1.37k
            BITS_PARSE("cnt_dropped_flag[i]", value, ps_bitstrm, 1);
3210
1.37k
            ps_time_code->au1_cnt_dropped_flag[i] = value;
3211
3212
1.37k
            BITS_PARSE("n_frames[i]", value, ps_bitstrm, 9);
3213
1.37k
            ps_time_code->au2_n_frames[i] = value;
3214
3215
1.37k
            if(ps_time_code->au1_full_timestamp_flag[i])
3216
173
            {
3217
173
                BITS_PARSE("seconds_value[i]", value, ps_bitstrm, 6);
3218
173
                ps_time_code->au1_seconds_value[i] = value;
3219
3220
173
                BITS_PARSE("minutes_value[i]", value, ps_bitstrm, 6);
3221
173
                ps_time_code->au1_minutes_value[i] = value;
3222
3223
173
                BITS_PARSE("hours_value[i]", value, ps_bitstrm, 5);
3224
173
                ps_time_code->au1_hours_value[i] = value;
3225
173
            }
3226
1.19k
            else
3227
1.19k
            {
3228
1.19k
                BITS_PARSE("seconds_flag[i]", value, ps_bitstrm, 1);
3229
1.19k
                ps_time_code->au1_seconds_flag[i] = value;
3230
3231
1.19k
                if(ps_time_code->au1_seconds_flag[i])
3232
727
                {
3233
727
                    BITS_PARSE("seconds_value[i]", value, ps_bitstrm, 6);
3234
727
                    ps_time_code->au1_seconds_value[i] = value;
3235
3236
727
                    BITS_PARSE("minutes_flag[i]", value, ps_bitstrm, 1);
3237
727
                    ps_time_code->au1_minutes_flag[i] = value;
3238
3239
727
                    if(ps_time_code->au1_minutes_flag[i])
3240
193
                    {
3241
193
                        BITS_PARSE("minutes_value[i]", value, ps_bitstrm, 6);
3242
193
                        ps_time_code->au1_minutes_value[i] = value;
3243
3244
193
                        BITS_PARSE("hours_flag[i]", value, ps_bitstrm, 1);
3245
193
                        ps_time_code->au1_hours_flag[i] = value;
3246
3247
193
                        if(ps_time_code->au1_hours_flag[i])
3248
139
                        {
3249
139
                            BITS_PARSE("hours_value[i]", value, ps_bitstrm, 5);
3250
139
                            ps_time_code->au1_hours_value[i] = value;
3251
139
                        }
3252
193
                    }
3253
727
                }
3254
1.19k
            }
3255
3256
1.37k
            BITS_PARSE("time_offset_length[i]", value, ps_bitstrm, 5);
3257
1.37k
            ps_time_code->au1_time_offset_length[i] = value;
3258
3259
1.37k
            if(ps_time_code->au1_time_offset_length[i] > 0)
3260
1.00k
            {
3261
1.00k
                BITS_PARSE("time_offset_value[i]", value, ps_bitstrm,
3262
1.00k
                           ps_time_code->au1_time_offset_length[i]);
3263
1.00k
                ps_time_code->au1_time_offset_value[i] = value;
3264
1.00k
            }
3265
367
            else
3266
367
            {
3267
367
                ps_time_code->au1_time_offset_value[i] = 0;
3268
367
            }
3269
1.37k
        }
3270
2.52k
    }
3271
3272
1.31k
    return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
3273
1.31k
}
3274
3275
IHEVCD_ERROR_T ihevcd_parse_mastering_disp_params_sei(codec_t *ps_codec)
3276
139
{
3277
139
    parse_ctxt_t *ps_parse = &ps_codec->s_parse;
3278
139
    bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
3279
139
    UWORD32 value;
3280
139
    mastering_dis_col_vol_sei_params_t *ps_mastering_dis_col_vol;
3281
139
    WORD32 i;
3282
3283
139
    ps_parse->s_sei_params.i4_sei_mastering_disp_colour_vol_params_present_flags = 1;
3284
3285
139
    ps_mastering_dis_col_vol = &ps_parse->s_sei_params.s_mastering_dis_col_vol_sei_params;
3286
3287
556
    for(i = 0; i < 3; i++)
3288
417
    {
3289
417
        BITS_PARSE("display_primaries_x[c]", value, ps_bitstrm, 16);
3290
417
        ps_mastering_dis_col_vol->au2_display_primaries_x[i] = value;
3291
3292
417
        BITS_PARSE("display_primaries_y[c]", value, ps_bitstrm, 16);
3293
417
        ps_mastering_dis_col_vol->au2_display_primaries_y[i] = value;
3294
417
    }
3295
3296
139
    BITS_PARSE("white_point_x", value, ps_bitstrm, 16);
3297
139
    ps_mastering_dis_col_vol->u2_white_point_x = value;
3298
3299
139
    BITS_PARSE("white_point_y", value, ps_bitstrm, 16);
3300
139
    ps_mastering_dis_col_vol->u2_white_point_y = value;
3301
3302
139
    BITS_PARSE("max_display_mastering_luminance", value, ps_bitstrm, 32);
3303
139
    ps_mastering_dis_col_vol->u4_max_display_mastering_luminance = value;
3304
3305
139
    BITS_PARSE("min_display_mastering_luminance", value, ps_bitstrm, 32);
3306
139
    ps_mastering_dis_col_vol->u4_min_display_mastering_luminance = value;
3307
3308
139
    return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
3309
139
}
3310
3311
IHEVCD_ERROR_T ihevcd_parse_user_data_registered_itu_t_t35(codec_t *ps_codec,
3312
                                                           UWORD32 u4_payload_size)
3313
267
{
3314
267
    parse_ctxt_t *ps_parse = &ps_codec->s_parse;
3315
267
    bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
3316
267
    UWORD32 value;
3317
267
    user_data_registered_itu_t_t35_t *ps_user_data_registered_itu_t_t35;
3318
267
    UWORD32 i;
3319
267
    UWORD32 j = 0;
3320
3321
267
    ps_parse->s_sei_params.i1_user_data_registered_present_flag = 1;
3322
267
    ps_user_data_registered_itu_t_t35 =
3323
267
                    &ps_parse->s_sei_params.as_user_data_registered_itu_t_t35[ps_parse->s_sei_params.i4_sei_user_data_cnt];
3324
267
    ps_parse->s_sei_params.i4_sei_user_data_cnt++;
3325
3326
267
    ps_user_data_registered_itu_t_t35->i4_payload_size = u4_payload_size;
3327
3328
267
    if(u4_payload_size > MAX_USERDATA_PAYLOAD)
3329
12
    {
3330
12
        u4_payload_size = MAX_USERDATA_PAYLOAD;
3331
12
    }
3332
3333
267
    ps_user_data_registered_itu_t_t35->i4_valid_payload_size = u4_payload_size;
3334
3335
267
    BITS_PARSE("itu_t_t35_country_code", value, ps_bitstrm, 8);
3336
267
    ps_user_data_registered_itu_t_t35->u1_itu_t_t35_country_code = value;
3337
3338
267
    if(0xFF != ps_user_data_registered_itu_t_t35->u1_itu_t_t35_country_code)
3339
248
    {
3340
248
        i = 1;
3341
248
    }
3342
19
    else
3343
19
    {
3344
19
        BITS_PARSE("itu_t_t35_country_code_extension_byte", value, ps_bitstrm,
3345
19
                   8);
3346
19
        ps_user_data_registered_itu_t_t35->u1_itu_t_t35_country_code_extension_byte =
3347
19
                        value;
3348
3349
19
        i = 2;
3350
19
    }
3351
3352
267
    do
3353
5.60k
    {
3354
5.60k
        BITS_PARSE("itu_t_t35_payload_byte", value, ps_bitstrm, 8);
3355
5.60k
        ps_user_data_registered_itu_t_t35->u1_itu_t_t35_payload_byte[j++] =
3356
5.60k
                        value;
3357
3358
5.60k
        i++;
3359
5.60k
    }while(i < u4_payload_size);
3360
3361
267
    return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
3362
267
}
3363
3364
void ihevcd_parse_sei_payload(codec_t *ps_codec,
3365
                              UWORD32 u4_payload_type,
3366
                              UWORD32 u4_payload_size,
3367
                              WORD8 i1_nal_type)
3368
47.5k
{
3369
47.5k
    parse_ctxt_t *ps_parse = &ps_codec->s_parse;
3370
47.5k
    bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
3371
47.5k
    WORD32 payload_bits_remaining = 0;
3372
47.5k
    sps_t *ps_sps = NULL;
3373
3374
47.5k
    UWORD32 i;
3375
3376
723k
    for(i = 0; i < MAX_SPS_CNT; i++)
3377
684k
    {
3378
684k
        if(ps_codec->ps_sps_base[i].i1_sps_valid)
3379
8.93k
        {
3380
8.93k
            ps_sps = ps_codec->ps_sps_base + i;
3381
8.93k
            break;
3382
8.93k
        }
3383
684k
    }
3384
47.5k
    if(!ps_sps)
3385
38.5k
    {
3386
38.5k
        return;
3387
38.5k
    }
3388
3389
8.93k
    if(NAL_PREFIX_SEI == i1_nal_type)
3390
7.37k
    {
3391
7.37k
        switch(u4_payload_type)
3392
7.37k
        {
3393
1.79k
            case SEI_BUFFERING_PERIOD:
3394
1.79k
                ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
3395
1.79k
                ihevcd_parse_buffering_period_sei(ps_codec, ps_sps);
3396
1.79k
                break;
3397
3398
2.27k
            case SEI_PICTURE_TIMING:
3399
2.27k
                ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
3400
2.27k
                ihevcd_parse_pic_timing_sei(ps_codec, ps_sps);
3401
2.27k
                break;
3402
3403
1.31k
            case SEI_TIME_CODE:
3404
1.31k
                ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
3405
1.31k
                ihevcd_parse_time_code_sei(ps_codec);
3406
1.31k
                break;
3407
3408
139
            case SEI_MASTERING_DISPLAY_COLOUR_VOLUME:
3409
139
                ps_parse->s_sei_params.i4_sei_mastering_disp_colour_vol_params_present_flags = 1;
3410
139
                ihevcd_parse_mastering_disp_params_sei(ps_codec);
3411
139
                break;
3412
3413
407
            case SEI_USER_DATA_REGISTERED_ITU_T_T35:
3414
407
                ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
3415
407
                if(ps_parse->s_sei_params.i4_sei_user_data_cnt >= USER_DATA_MAX)
3416
301
                {
3417
729
                    for(i = 0; i < u4_payload_size / 4; i++)
3418
428
                    {
3419
428
                        ihevcd_bits_flush(ps_bitstrm, 4 * 8);
3420
428
                    }
3421
3422
301
                    ihevcd_bits_flush(ps_bitstrm, (u4_payload_size - i * 4) * 8);
3423
301
                }
3424
106
                else
3425
106
                {
3426
106
                    ihevcd_parse_user_data_registered_itu_t_t35(ps_codec,
3427
106
                                                                u4_payload_size);
3428
106
                }
3429
407
                break;
3430
3431
1.44k
            default:
3432
11.2k
                for(i = 0; i < u4_payload_size; i++)
3433
9.83k
                {
3434
9.83k
                    ihevcd_bits_flush(ps_bitstrm, 8);
3435
9.83k
                }
3436
1.44k
                break;
3437
7.37k
        }
3438
7.37k
    }
3439
1.55k
    else /* NAL_SUFFIX_SEI */
3440
1.55k
    {
3441
1.55k
        switch(u4_payload_type)
3442
1.55k
        {
3443
629
            case SEI_USER_DATA_REGISTERED_ITU_T_T35:
3444
629
                ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
3445
629
                if(ps_parse->s_sei_params.i4_sei_user_data_cnt >= USER_DATA_MAX)
3446
468
                {
3447
1.27k
                    for(i = 0; i < u4_payload_size / 4; i++)
3448
804
                    {
3449
804
                        ihevcd_bits_flush(ps_bitstrm, 4 * 8);
3450
804
                    }
3451
3452
468
                    ihevcd_bits_flush(ps_bitstrm, (u4_payload_size - i * 4) * 8);
3453
468
                }
3454
161
                else
3455
161
                {
3456
161
                    ihevcd_parse_user_data_registered_itu_t_t35(ps_codec,
3457
161
                                                                u4_payload_size);
3458
161
                }
3459
629
                break;
3460
3461
929
            default:
3462
31.4k
                for(i = 0; i < u4_payload_size; i++)
3463
30.5k
                {
3464
30.5k
                    ihevcd_bits_flush(ps_bitstrm, 8);
3465
30.5k
                }
3466
929
                break;
3467
1.55k
        }
3468
1.55k
    }
3469
3470
    /**
3471
     * By definition the underlying bitstream terminates in a byte-aligned manner.
3472
     * 1. Extract all bar the last MIN(bitsremaining,nine) bits as reserved_payload_extension_data
3473
     * 2. Examine the final 8 bits to determine the payload_bit_equal_to_one marker
3474
     * 3. Extract the remainingreserved_payload_extension_data bits.
3475
     *
3476
     * If there are fewer than 9 bits available, extract them.
3477
     */
3478
3479
8.93k
    payload_bits_remaining = ihevcd_bits_num_bits_remaining(ps_bitstrm);
3480
8.93k
    if(payload_bits_remaining) /* more_data_in_payload() */
3481
7.84k
    {
3482
7.84k
        WORD32 final_bits;
3483
7.84k
        WORD32 final_payload_bits = 0;
3484
7.84k
        WORD32 mask = 0xFF;
3485
7.84k
        UWORD32 u4_dummy;
3486
7.84k
        UWORD32 u4_reserved_payload_extension_data;
3487
7.84k
        UNUSED(u4_dummy);
3488
7.84k
        UNUSED(u4_reserved_payload_extension_data);
3489
3490
1.42M
        while(payload_bits_remaining > 9)
3491
1.42M
        {
3492
1.42M
            BITS_PARSE("reserved_payload_extension_data",
3493
1.42M
                       u4_reserved_payload_extension_data, ps_bitstrm, 1);
3494
1.42M
            payload_bits_remaining--;
3495
1.42M
        }
3496
3497
7.84k
        final_bits = ihevcd_bits_nxt(ps_bitstrm, payload_bits_remaining);
3498
3499
59.0k
        while(final_bits & (mask >> final_payload_bits))
3500
51.2k
        {
3501
51.2k
            final_payload_bits++;
3502
51.2k
            continue;
3503
51.2k
        }
3504
3505
54.6k
        while(payload_bits_remaining > (9 - final_payload_bits))
3506
46.8k
        {
3507
46.8k
            BITS_PARSE("reserved_payload_extension_data",
3508
46.8k
                       u4_reserved_payload_extension_data, ps_bitstrm, 1);
3509
46.8k
            payload_bits_remaining--;
3510
46.8k
        }
3511
3512
7.84k
        BITS_PARSE("payload_bit_equal_to_one", u4_dummy, ps_bitstrm, 1);
3513
7.84k
        payload_bits_remaining--;
3514
18.3k
        while(payload_bits_remaining)
3515
10.5k
        {
3516
10.5k
            BITS_PARSE("payload_bit_equal_to_zero", u4_dummy, ps_bitstrm, 1);
3517
10.5k
            payload_bits_remaining--;
3518
10.5k
        }
3519
7.84k
    }
3520
3521
8.93k
    return;
3522
8.93k
}
3523
3524
IHEVCD_ERROR_T ihevcd_read_rbsp_trailing_bits(codec_t *ps_codec,
3525
                                              UWORD32 u4_bits_left)
3526
2.07k
{
3527
2.07k
    parse_ctxt_t *ps_parse = &ps_codec->s_parse;
3528
2.07k
    UWORD32 value;
3529
2.07k
    WORD32 cnt = 0;
3530
2.07k
    BITS_PARSE("rbsp_stop_one_bit", value, &ps_parse->s_bitstrm, 1);
3531
2.07k
    u4_bits_left--;
3532
2.07k
    if(value != 1)
3533
237
    {
3534
237
        return (IHEVCD_ERROR_T)IHEVCD_FAIL;
3535
237
    }
3536
14.6k
    while(u4_bits_left)
3537
12.8k
    {
3538
12.8k
        BITS_PARSE("rbsp_alignment_zero_bit", value, &ps_parse->s_bitstrm, 1);
3539
12.8k
        u4_bits_left--;
3540
12.8k
        cnt++;
3541
12.8k
    }
3542
1.83k
    ASSERT(cnt < 8);
3543
3544
1.83k
    return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
3545
2.07k
}
3546
/**
3547
*******************************************************************************
3548
*
3549
* @brief
3550
*  Parses SEI (Supplemental Enhancement Information)
3551
*
3552
* @par Description:
3553
*  Parses SEI (Supplemental Enhancement Information) as per Section: 7.3.7
3554
*
3555
* @param[in] ps_codec
3556
*  Pointer to codec context
3557
*
3558
* @returns Error code from IHEVCD_ERROR_T
3559
*
3560
* @remarks
3561
*
3562
*
3563
*******************************************************************************
3564
*/
3565
IHEVCD_ERROR_T ihevcd_parse_sei(codec_t *ps_codec, nal_header_t *ps_nal)
3566
15.3k
{
3567
15.3k
    IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
3568
15.3k
    parse_ctxt_t *ps_parse = &ps_codec->s_parse;
3569
15.3k
    UWORD32 u4_payload_type = 0, u4_last_payload_type_byte = 0;
3570
15.3k
    UWORD32 u4_payload_size = 0, u4_last_payload_size_byte = 0;
3571
15.3k
    UWORD32 value;
3572
15.3k
    bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
3573
15.3k
    UWORD32 u4_bits_left;
3574
3575
15.3k
    u4_bits_left = ihevcd_bits_num_bits_remaining(ps_bitstrm);
3576
3577
62.9k
    while(u4_bits_left > 8)
3578
50.9k
    {
3579
52.7k
        while(ihevcd_bits_nxt(ps_bitstrm, 8) == 0xFF)
3580
1.71k
        {
3581
1.71k
            ihevcd_bits_flush(ps_bitstrm, 8); /* equal to 0xFF */
3582
1.71k
            u4_payload_type += 255;
3583
1.71k
        }
3584
3585
50.9k
        BITS_PARSE("last_payload_type_byte", value, ps_bitstrm, 8);
3586
50.9k
        u4_last_payload_type_byte = value;
3587
3588
50.9k
        u4_payload_type += u4_last_payload_type_byte;
3589
3590
53.1k
        while(ihevcd_bits_nxt(ps_bitstrm, 8) == 0xFF)
3591
2.17k
        {
3592
2.17k
            ihevcd_bits_flush(ps_bitstrm, 8); /* equal to 0xFF */
3593
2.17k
            u4_payload_size += 255;
3594
2.17k
        }
3595
3596
50.9k
        BITS_PARSE("last_payload_size_byte", value, ps_bitstrm, 8);
3597
50.9k
        u4_last_payload_size_byte = value;
3598
3599
50.9k
        u4_payload_size += u4_last_payload_size_byte;
3600
50.9k
        u4_bits_left = ihevcd_bits_num_bits_remaining(ps_bitstrm);
3601
50.9k
        if(u4_payload_size > (u4_bits_left / 8))
3602
3.46k
        {
3603
3.46k
            return (IHEVCD_ERROR_T)IHEVCD_INVALID_PARAMETER;
3604
3.46k
        }
3605
47.5k
        ihevcd_parse_sei_payload(ps_codec, u4_payload_type, u4_payload_size,
3606
47.5k
                                 ps_nal->i1_nal_unit_type);
3607
3608
        /* Calculate the bits left in the current payload */
3609
47.5k
        u4_bits_left = ihevcd_bits_num_bits_remaining(ps_bitstrm);
3610
47.5k
    }
3611
3612
    // read rbsp_trailing_bits
3613
11.9k
    if(u4_bits_left)
3614
2.07k
    {
3615
2.07k
        ihevcd_read_rbsp_trailing_bits(ps_codec, u4_bits_left);
3616
2.07k
    }
3617
3618
11.9k
    return ret;
3619
15.3k
}
3620
#endif
3621
3622
/**
3623
*******************************************************************************
3624
*
3625
* @brief
3626
*  Parses Access unit delimiter
3627
*
3628
* @par Description:
3629
*  Parses Access unit delimiter as per section  Section: 7.3.2.5
3630
*
3631
* @param[in] ps_codec
3632
*  Pointer to codec context
3633
*
3634
* @returns Error code from IHEVCD_ERROR_T
3635
*
3636
* @remarks
3637
*
3638
*
3639
*******************************************************************************
3640
*/
3641
WORD32 ihevcd_parse_aud(codec_t *ps_codec)
3642
0
{
3643
0
    IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
3644
0
    UNUSED(ps_codec);
3645
0
    return ret;
3646
0
}
3647
3648
WORD32 ihevcd_extend_sign_bit(WORD32 value, WORD32 num_bits)
3649
0
{
3650
0
    WORD32 ret_value = value;
3651
0
    if(value >> (num_bits - 1))
3652
0
    {
3653
0
        ret_value |= (0xFFFFFFFF << num_bits);
3654
0
    }
3655
0
    return ret_value;
3656
0
}
3657
3658
/**
3659
*******************************************************************************
3660
*
3661
* @brief
3662
*  Calculate POC of the current slice
3663
*
3664
* @par Description:
3665
*  Calculates the current POC using the previous POC lsb and previous POC msb
3666
*
3667
* @param[in] ps_codec
3668
*  Pointer to codec context
3669
*
3670
* @param[in] i1_pic_order_cnt_lsb
3671
*  Current POC lsb
3672
*
3673
* @returns  Current absolute POC
3674
*
3675
* @remarks
3676
*
3677
*
3678
*******************************************************************************
3679
*/
3680
3681
WORD32 ihevcd_calc_poc(codec_t *ps_codec, nal_header_t *ps_nal, WORD8 i1_log2_max_poc_lsb, WORD32 i2_poc_lsb)
3682
113k
{
3683
113k
    WORD32 i4_abs_poc, i4_poc_msb;
3684
113k
    WORD32 max_poc_lsb;
3685
113k
    WORD8 i1_nal_unit_type = ps_nal->i1_nal_unit_type;
3686
113k
    max_poc_lsb = (1 << i1_log2_max_poc_lsb);
3687
3688
113k
    if((!ps_codec->i4_first_pic_done) && (!ps_codec->i4_pic_present))
3689
27.9k
        ps_codec->i4_prev_poc_msb = -2 * max_poc_lsb;
3690
3691
113k
    if(NAL_IDR_N_LP == i1_nal_unit_type
3692
63.4k
                    || NAL_IDR_W_LP == i1_nal_unit_type
3693
49.2k
                    || NAL_BLA_N_LP == i1_nal_unit_type
3694
46.3k
                    || NAL_BLA_W_DLP == i1_nal_unit_type
3695
36.1k
                    || NAL_BLA_W_LP == i1_nal_unit_type
3696
34.9k
                    || (NAL_CRA == i1_nal_unit_type && !ps_codec->i4_first_pic_done))
3697
79.0k
    {
3698
79.0k
        i4_poc_msb = ps_codec->i4_prev_poc_msb + 2 * max_poc_lsb;
3699
79.0k
        ps_codec->i4_prev_poc_lsb = 0;
3700
79.0k
        ps_codec->i4_max_prev_poc_lsb = 0;
3701
//        ps_codec->i4_prev_poc_msb = 0;
3702
79.0k
    }
3703
34.5k
    else
3704
34.5k
    {
3705
3706
34.5k
        if((i2_poc_lsb < ps_codec->i4_prev_poc_lsb)
3707
2.20k
                        && ((ps_codec->i4_prev_poc_lsb - i2_poc_lsb) >= max_poc_lsb / 2))
3708
111
        {
3709
111
            i4_poc_msb = ps_codec->i4_prev_poc_msb + max_poc_lsb;
3710
111
        }
3711
34.3k
        else if((i2_poc_lsb > ps_codec->i4_prev_poc_lsb)
3712
30.1k
                        && ((i2_poc_lsb - ps_codec->i4_prev_poc_lsb) > max_poc_lsb / 2))
3713
18.4k
        {
3714
18.4k
            i4_poc_msb = ps_codec->i4_prev_poc_msb - max_poc_lsb;
3715
18.4k
        }
3716
15.9k
        else
3717
15.9k
        {
3718
15.9k
            i4_poc_msb = ps_codec->i4_prev_poc_msb;
3719
15.9k
        }
3720
3721
3722
34.5k
    }
3723
3724
113k
    i4_abs_poc = i4_poc_msb + i2_poc_lsb;
3725
113k
    ps_codec->i4_max_prev_poc_lsb = MAX(ps_codec->i4_max_prev_poc_lsb, i2_poc_lsb);
3726
3727
113k
    {
3728
113k
        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));
3729
113k
        WORD32 update_prev_poc = ((is_reference_nal) && ((i1_nal_unit_type < NAL_RADL_N) || (i1_nal_unit_type > NAL_RASL_R)));
3730
3731
113k
        if((0 == ps_nal->i1_nuh_temporal_id) &&
3732
62.9k
                        (update_prev_poc))
3733
58.4k
        {
3734
58.4k
            ps_codec->i4_prev_poc_lsb = i2_poc_lsb;
3735
58.4k
            ps_codec->i4_prev_poc_msb = i4_poc_msb;
3736
58.4k
        }
3737
113k
    }
3738
3739
113k
    return i4_abs_poc;
3740
113k
}
3741
3742
3743
void ihevcd_copy_slice_hdr(codec_t *ps_codec, WORD32 slice_idx, WORD32 slice_idx_ref)
3744
16.9k
{
3745
16.9k
    slice_header_t *ps_slice_hdr, *ps_slice_hdr_ref;
3746
16.9k
    WORD32 *pu4_entry_offset_backup;
3747
3748
16.9k
    ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr_base + slice_idx;
3749
16.9k
    ps_slice_hdr_ref = ps_codec->s_parse.ps_slice_hdr_base + slice_idx_ref;
3750
3751
16.9k
    pu4_entry_offset_backup = ps_slice_hdr->pu4_entry_point_offset;
3752
16.9k
    memcpy(ps_slice_hdr, ps_slice_hdr_ref, sizeof(slice_header_t));
3753
16.9k
    ps_slice_hdr->pu4_entry_point_offset = pu4_entry_offset_backup;
3754
16.9k
    ps_slice_hdr->i2_ctb_x = -1;
3755
16.9k
    ps_slice_hdr->i2_ctb_y = -1;
3756
16.9k
}
3757
3758
3759