Coverage Report

Created: 2025-10-10 06:51

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