Coverage Report

Created: 2026-01-17 06:25

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