Coverage Report

Created: 2026-10-28 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libhevc/decoder/ihevcd_parse_slice_header.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
46
#include "ihevc_typedefs.h"
47
#include "iv.h"
48
#include "ivd.h"
49
#include "ihevcd_cxa.h"
50
51
#include "ihevc_defs.h"
52
#include "ihevc_debug.h"
53
#include "ihevc_defs.h"
54
#include "ihevc_structs.h"
55
#include "ihevc_buf_mgr.h"
56
#include "ihevc_dpb_mgr.h"
57
#include "ihevc_macros.h"
58
#include "ihevc_platform_macros.h"
59
#include "ihevc_cabac_tables.h"
60
#include "ihevc_common_tables.h"
61
#include "ihevc_quant_tables.h"
62
63
#include "ihevcd_trace.h"
64
#include "ihevcd_defs.h"
65
#include "ihevcd_function_selector.h"
66
#include "ihevcd_structs.h"
67
#include "ihevcd_error.h"
68
#include "ihevcd_debug.h"
69
#include "ihevcd_nal.h"
70
#include "ihevcd_bitstream.h"
71
#include "ihevcd_parse_headers.h"
72
#include "ihevcd_parse_slice_header.h"
73
#include "ihevcd_ref_list.h"
74
75
mv_buf_t* ihevcd_mv_mgr_get_poc(buf_mgr_t *ps_mv_buf_mgr, UWORD32 abs_poc);
76
77
/**
78
*******************************************************************************
79
*
80
* @brief
81
*  Parses VPS operation point
82
*
83
* @par   Description
84
* Parses VPS operation point as per section 7.3.5
85
*
86
* @param[out] ps_vps
87
*  Pointer to VPS structure
88
*
89
* @param[in] ps_bitstrm
90
*  Pointer to bitstream structure
91
*
92
* @param[in] ops_idx
93
*  Operating point index
94
*
95
* @returns Error code from IHEVCD_ERROR_T
96
*
97
* @remarks
98
*
99
*******************************************************************************
100
*/
101
IHEVCD_ERROR_T ihevcd_operation_point_set(vps_t *ps_vps, bitstrm_t *ps_bitstrm, WORD32 ops_idx)
102
0
{
103
0
    WORD32 i;
104
0
    WORD32 value;
105
106
0
    IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
107
0
    UNUSED(ops_idx);
108
0
    for(i = 0; i <= ps_vps->i1_vps_max_nuh_reserved_zero_layer_id; i++)
109
0
    {
110
0
        BITS_PARSE("list_entry_l0[ i ]", value, ps_bitstrm, 1);
111
        //ps_vps->ai1_layer_id_included_flag[ops_idx][i] = value;
112
113
0
    }
114
0
    UNUSED(value);
115
116
0
    return ret;
117
0
}
118
119
/**
120
*******************************************************************************
121
*
122
* @brief
123
*  Parses pic_lismod_t (picture list mod syntax)  Section:7.3.8.3 Reference
124
* picture list mod syntax
125
*
126
* @par Description:
127
*  Parse pict list mod synt and update pic_lismod_t struct
128
*
129
* @param[in] ps_codec
130
*  Pointer to codec context
131
*
132
* @returns  Error code from IHEVCD_ERROR_T
133
*
134
* @remarks
135
*
136
*
137
*******************************************************************************
138
*/
139
140
WORD32 ihevcd_ref_pic_list_modification(bitstrm_t *ps_bitstrm,
141
                                        slice_header_t *ps_slice_hdr,
142
                                        WORD32 num_poc_total_curr)
143
9.41k
{
144
9.41k
    WORD32 ret = IHEVCD_SUCCESS;
145
9.41k
    WORD32 value;
146
9.41k
    WORD32 i;
147
9.41k
    rplm_t *ps_rplm;
148
9.41k
    WORD32 num_bits_list_entry;
149
150
9.41k
    ps_rplm = &(ps_slice_hdr->s_rplm);
151
152
    /* Calculate Ceil(Log2(num_poc_total_curr)) */
153
9.41k
    {
154
9.41k
        num_bits_list_entry = 32 - CLZ(num_poc_total_curr);
155
        /* Check if num_poc_total_curr is power of 2 */
156
9.41k
        if(0 == (num_poc_total_curr & (num_poc_total_curr - 1)))
157
5.30k
        {
158
5.30k
            num_bits_list_entry--;
159
5.30k
        }
160
9.41k
    }
161
162
9.41k
    if(ps_slice_hdr->i1_slice_type  == PSLICE || ps_slice_hdr->i1_slice_type  == BSLICE)
163
9.41k
    {
164
9.41k
        BITS_PARSE("ref_pic_list_modification_flag_l0", value, ps_bitstrm, 1);
165
9.41k
        ps_rplm->i1_ref_pic_list_modification_flag_l0 = value;
166
167
9.41k
        if(ps_rplm->i1_ref_pic_list_modification_flag_l0)
168
16.9k
            for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
169
12.3k
            {
170
12.3k
                BITS_PARSE("list_entry_l0", value, ps_bitstrm, num_bits_list_entry);
171
12.3k
                ps_rplm->i1_list_entry_l0[i] = value;
172
173
12.3k
                ps_rplm->i1_list_entry_l0[i] = CLIP3(ps_rplm->i1_list_entry_l0[i], 0, num_poc_total_curr - 1);
174
12.3k
            }
175
9.41k
    }
176
177
9.41k
    if(ps_slice_hdr->i1_slice_type  == BSLICE)
178
8.53k
    {
179
8.53k
        BITS_PARSE("ref_pic_list_modification_flag_l1", value, ps_bitstrm, 1);
180
8.53k
        ps_rplm->i1_ref_pic_list_modification_flag_l1 = value;
181
182
8.53k
        if(ps_rplm->i1_ref_pic_list_modification_flag_l1)
183
16.1k
            for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
184
10.6k
            {
185
10.6k
                BITS_PARSE("list_entry_l1", value, ps_bitstrm, num_bits_list_entry);
186
10.6k
                ps_rplm->i1_list_entry_l1[i] = value;
187
188
10.6k
                ps_rplm->i1_list_entry_l1[i] = CLIP3(ps_rplm->i1_list_entry_l1[i], 0, num_poc_total_curr - 1);
189
10.6k
            }
190
191
8.53k
    }
192
193
9.41k
    return ret;
194
9.41k
}
195
196
/**
197
*******************************************************************************
198
*
199
* @brief
200
*  Parse Slice Header
201
* slice_header_syntax()
202
*
203
* @par Description:
204
*  Parse Slice Header as per  Section: 7.3.8
205
*
206
* @param[in] ps_codec
207
*  Pointer to codec context
208
*
209
* @returns  Error code from IHEVCD_ERROR_T
210
*
211
* @remarks
212
*
213
*
214
*******************************************************************************
215
*/
216
217
IHEVCD_ERROR_T ihevcd_parse_slice_header(codec_t *ps_codec,
218
                                         nal_header_t *ps_nal)
219
219k
{
220
219k
    IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
221
219k
    UWORD32 value;
222
219k
    WORD32 i4_value;
223
219k
    WORD32 i, j;
224
219k
    WORD32 sps_id;
225
226
219k
    pps_t *ps_pps;
227
219k
    sps_t *ps_sps;
228
219k
    slice_header_t *ps_slice_hdr;
229
219k
    WORD32 disable_deblocking_filter_flag;
230
219k
    bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
231
219k
    WORD32 idr_pic_flag;
232
219k
    WORD32 pps_id;
233
219k
    WORD32 first_slice_in_pic_flag;
234
219k
    WORD32 no_output_of_prior_pics_flag = 0;
235
219k
    WORD8 i1_nal_unit_type = ps_nal->i1_nal_unit_type;
236
219k
    WORD32 num_poc_total_curr = 0;
237
219k
    UWORD32 slice_address;
238
219k
    WORD32 prev_slice_incomplete_flag = 0;
239
240
219k
    if(ps_codec->i4_slice_error == 1)
241
31.1k
        return ret;
242
243
188k
    idr_pic_flag = (NAL_IDR_W_LP == i1_nal_unit_type) ||
244
166k
                    (NAL_IDR_N_LP == i1_nal_unit_type);
245
246
247
188k
    BITS_PARSE("first_slice_in_pic_flag", first_slice_in_pic_flag, ps_bitstrm, 1);
248
188k
    if((NAL_BLA_W_LP <= i1_nal_unit_type) &&
249
90.4k
       (NAL_RSV_RAP_VCL23          >= i1_nal_unit_type))
250
90.4k
    {
251
90.4k
        BITS_PARSE("no_output_of_prior_pics_flag", no_output_of_prior_pics_flag, ps_bitstrm, 1);
252
90.4k
    }
253
188k
    UEV_PARSE("pic_parameter_set_id", pps_id, ps_bitstrm);
254
188k
    if(pps_id < 0 || pps_id > MAX_PPS_CNT - 2)
255
31.6k
    {
256
31.6k
        return IHEVCD_INVALID_PARAMETER;
257
31.6k
    }
258
259
    /* Get the current PPS structure */
260
157k
    ps_pps = ps_codec->s_parse.ps_pps_base + pps_id;
261
157k
    if(0 == ps_pps->i1_pps_valid)
262
15.9k
    {
263
15.9k
        pps_t *ps_pps_ref = ps_codec->ps_pps_base;
264
36.1k
        while(0 == ps_pps_ref->i1_pps_valid)
265
20.3k
        {
266
20.3k
            ps_pps_ref++;
267
20.3k
            if((ps_pps_ref - ps_codec->ps_pps_base >= MAX_PPS_CNT - 1))
268
66
                return IHEVCD_INVALID_HEADER;
269
20.3k
        }
270
271
15.8k
        ihevcd_copy_pps(ps_codec, pps_id, ps_pps_ref->i1_pps_id);
272
15.8k
    }
273
274
    /* Get SPS id for the current PPS */
275
157k
    sps_id = ps_pps->i1_sps_id;
276
277
    /* Get the current SPS structure */
278
157k
    ps_sps = ps_codec->s_parse.ps_sps_base + sps_id;
279
280
    /* When the current slice is the first in a pic,
281
     *  check whether the previous frame is complete
282
     *  If the previous frame is incomplete -
283
     *  treat the remaining CTBs as skip */
284
157k
    if((0 != ps_codec->u4_pic_cnt || ps_codec->i4_pic_present) &&
285
119k
                    first_slice_in_pic_flag)
286
82.5k
    {
287
82.5k
        if(ps_codec->i4_pic_present)
288
9.10k
        {
289
9.10k
            slice_header_t *ps_slice_hdr_next;
290
9.10k
            ps_codec->i4_slice_error = 1;
291
9.10k
            ps_codec->s_parse.i4_cur_slice_idx--;
292
9.10k
            if(ps_codec->s_parse.i4_cur_slice_idx < 0)
293
0
                ps_codec->s_parse.i4_cur_slice_idx = 0;
294
295
9.10k
            ps_slice_hdr_next = ps_codec->s_parse.ps_slice_hdr_base + ((ps_codec->s_parse.i4_cur_slice_idx + 1) & (MAX_SLICE_HDR_CNT - 1));
296
9.10k
            ps_slice_hdr_next->i2_ctb_x = 0;
297
9.10k
            ps_slice_hdr_next->i2_ctb_y = ps_codec->s_parse.ps_sps->i2_pic_ht_in_ctb;
298
9.10k
            return ret;
299
9.10k
        }
300
73.4k
        else
301
73.4k
        {
302
73.4k
            ps_codec->i4_slice_error = 0;
303
73.4k
        }
304
82.5k
    }
305
306
147k
    if(first_slice_in_pic_flag)
307
106k
    {
308
106k
        ps_codec->s_parse.i4_cur_slice_idx = 0;
309
106k
    }
310
41.6k
    else
311
41.6k
    {
312
        /* If the current slice is not the first slice in the pic,
313
         * but the first one to be parsed, set the current slice indx to 1
314
         * Treat the first slice to be missing and copy the current slice header
315
         * to the first one */
316
41.6k
        if(0 == ps_codec->i4_pic_present)
317
19.6k
            ps_codec->s_parse.i4_cur_slice_idx = 1;
318
41.6k
    }
319
320
147k
    ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr_base + (ps_codec->s_parse.i4_cur_slice_idx & (MAX_SLICE_HDR_CNT - 1));
321
147k
    memset(ps_slice_hdr, 0, sizeof(*ps_slice_hdr));
322
147k
    ps_slice_hdr->i2_ctb_x = -1;
323
147k
    ps_slice_hdr->i2_ctb_y = -1;
324
325
147k
    if((ps_pps->i1_dependent_slice_enabled_flag) &&
326
96.3k
       (!first_slice_in_pic_flag))
327
28.7k
    {
328
28.7k
        BITS_PARSE("dependent_slice_flag", value, ps_bitstrm, 1);
329
330
        /* First slice to be decoded in the current picture can't be dependent slice */
331
28.7k
        if (value && 0 == ps_codec->i4_pic_present)
332
3.68k
        {
333
3.68k
             return IHEVCD_IGNORE_SLICE;
334
3.68k
        }
335
336
        /* If dependendent slice, copy slice header from previous slice */
337
25.0k
        if(value && (ps_codec->s_parse.i4_cur_slice_idx > 0))
338
11.5k
        {
339
11.5k
            ihevcd_copy_slice_hdr(ps_codec,
340
11.5k
                                  (ps_codec->s_parse.i4_cur_slice_idx & (MAX_SLICE_HDR_CNT - 1)),
341
11.5k
                                  ((ps_codec->s_parse.i4_cur_slice_idx - 1) & (MAX_SLICE_HDR_CNT - 1)));
342
11.5k
        }
343
25.0k
        ps_slice_hdr->i1_dependent_slice_flag = value;
344
25.0k
    }
345
119k
    else
346
119k
    {
347
119k
        ps_slice_hdr->i1_dependent_slice_flag = 0;
348
119k
    }
349
144k
    ps_slice_hdr->i1_nal_unit_type = i1_nal_unit_type;
350
144k
    ps_slice_hdr->i1_pps_id = pps_id;
351
144k
    ps_slice_hdr->i1_first_slice_in_pic_flag = first_slice_in_pic_flag;
352
353
144k
    ps_slice_hdr->i1_no_output_of_prior_pics_flag = 1;
354
144k
    if((NAL_BLA_W_LP <= i1_nal_unit_type) &&
355
81.6k
                    (NAL_RSV_RAP_VCL23          >= i1_nal_unit_type))
356
81.6k
    {
357
81.6k
        ps_slice_hdr->i1_no_output_of_prior_pics_flag = no_output_of_prior_pics_flag;
358
81.6k
    }
359
144k
    ps_slice_hdr->i1_pps_id = pps_id;
360
361
144k
    if(!ps_slice_hdr->i1_first_slice_in_pic_flag)
362
38.0k
    {
363
38.0k
        WORD32 num_bits;
364
365
        /* Use CLZ to compute Ceil( Log2( PicSizeInCtbsY ) ) */
366
38.0k
        num_bits = 32 - CLZ(ps_sps->i4_pic_size_in_ctb - 1);
367
38.0k
        BITS_PARSE("slice_address", value, ps_bitstrm, num_bits);
368
369
38.0k
        slice_address = value;
370
        /* If slice address is greater than the number of CTBs in a picture,
371
         * ignore the slice */
372
38.0k
        if(value >= ps_sps->i4_pic_size_in_ctb || value == 0)
373
3.76k
            return IHEVCD_IGNORE_SLICE;
374
38.0k
    }
375
106k
    else
376
106k
    {
377
106k
        slice_address = 0;
378
106k
    }
379
380
140k
    if(!ps_slice_hdr->i1_dependent_slice_flag)
381
129k
    {
382
129k
        ps_slice_hdr->i1_pic_output_flag = 1;
383
129k
        ps_slice_hdr->i4_pic_order_cnt_lsb = 0;
384
129k
        ps_slice_hdr->i1_num_long_term_sps = 0;
385
129k
        ps_slice_hdr->i1_num_long_term_pics = 0;
386
387
319k
        for(i = 0; i < ps_pps->i1_num_extra_slice_header_bits; i++)
388
190k
        {
389
190k
            BITS_PARSE("slice_reserved_undetermined_flag[ i ]", value, ps_bitstrm, 1);
390
            //slice_reserved_undetermined_flag[ i ]
391
190k
        }
392
129k
        UEV_PARSE("slice_type", value, ps_bitstrm);
393
129k
        if(value > 2)
394
4.16k
        {
395
4.16k
            return IHEVCD_INVALID_PARAMETER;
396
4.16k
        }
397
124k
        ps_slice_hdr->i1_slice_type = value;
398
399
        /* If the picture is IRAP, slice type must be equal to ISLICE */
400
124k
        if((ps_slice_hdr->i1_nal_unit_type >= NAL_BLA_W_LP) &&
401
77.3k
                        (ps_slice_hdr->i1_nal_unit_type <= NAL_RSV_RAP_VCL23))
402
77.3k
            ps_slice_hdr->i1_slice_type = ISLICE;
403
404
124k
        if((ps_slice_hdr->i1_slice_type < 0) ||
405
124k
                        (ps_slice_hdr->i1_slice_type > 2))
406
0
            return IHEVCD_IGNORE_SLICE;
407
408
409
124k
        if(ps_pps->i1_output_flag_present_flag)
410
24.2k
        {
411
24.2k
            BITS_PARSE("pic_output_flag", value, ps_bitstrm, 1);
412
24.2k
            ps_slice_hdr->i1_pic_output_flag = value;
413
24.2k
        }
414
124k
        ps_slice_hdr->i1_colour_plane_id = 0;
415
124k
        if(1 == ps_sps->i1_separate_colour_plane_flag)
416
0
        {
417
0
            BITS_PARSE("colour_plane_id", value, ps_bitstrm, 2);
418
0
            ps_slice_hdr->i1_colour_plane_id = value;
419
0
        }
420
124k
        ps_slice_hdr->i1_slice_temporal_mvp_enable_flag = 0;
421
422
124k
        if(!idr_pic_flag)
423
65.4k
        {
424
425
65.4k
            WORD32 st_rps_idx;
426
65.4k
            WORD32 num_neg_pics;
427
65.4k
            WORD32 num_pos_pics;
428
65.4k
            WORD8 *pi1_used;
429
430
65.4k
            BITS_PARSE("pic_order_cnt_lsb", value, ps_bitstrm, ps_sps->i1_log2_max_pic_order_cnt_lsb);
431
            //value = ihevcd_extend_sign_bit(value, ps_sps->i1_log2_max_pic_order_cnt_lsb);
432
65.4k
            ps_slice_hdr->i4_pic_order_cnt_lsb = value;
433
434
65.4k
            BITS_PARSE("short_term_ref_pic_set_sps_flag", value, ps_bitstrm, 1);
435
65.4k
            ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag = value;
436
437
65.4k
            if(1 == ps_slice_hdr->i1_short_term_ref_pic_set_sps_flag)
438
51.3k
            {
439
51.3k
                WORD32 numbits;
440
441
51.3k
                ps_slice_hdr->i1_short_term_ref_pic_set_idx = 0;
442
51.3k
                if(ps_sps->i1_num_short_term_ref_pic_sets > 1)
443
11.3k
                {
444
11.3k
                    numbits = 32 - CLZ(ps_sps->i1_num_short_term_ref_pic_sets - 1);
445
11.3k
                    BITS_PARSE("short_term_ref_pic_set_idx", value, ps_bitstrm, numbits);
446
11.3k
                    if (value >= ps_sps->i1_num_short_term_ref_pic_sets) {
447
378
                        return IHEVCD_INVALID_PARAMETER;
448
378
                    }
449
10.9k
                    ps_slice_hdr->i1_short_term_ref_pic_set_idx = value;
450
10.9k
                }
451
452
50.9k
                st_rps_idx = ps_slice_hdr->i1_short_term_ref_pic_set_idx;
453
50.9k
                num_neg_pics = ps_sps->as_stref_picset[st_rps_idx].i1_num_neg_pics;
454
50.9k
                num_pos_pics = ps_sps->as_stref_picset[st_rps_idx].i1_num_pos_pics;
455
50.9k
                pi1_used = ps_sps->as_stref_picset[st_rps_idx].ai1_used;
456
50.9k
            }
457
14.1k
            else
458
14.1k
            {
459
14.1k
                ret = ihevcd_short_term_ref_pic_set(ps_bitstrm,
460
14.1k
                                                    &ps_sps->as_stref_picset[0],
461
14.1k
                                                    ps_sps->i1_num_short_term_ref_pic_sets,
462
14.1k
                                                    ps_sps->i1_num_short_term_ref_pic_sets,
463
14.1k
                                                    &ps_slice_hdr->s_stref_picset);
464
14.1k
                if (ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS)
465
1.64k
                {
466
1.64k
                    return ret;
467
1.64k
                }
468
12.4k
                st_rps_idx = ps_sps->i1_num_short_term_ref_pic_sets;
469
12.4k
                num_neg_pics = ps_slice_hdr->s_stref_picset.i1_num_neg_pics;
470
12.4k
                num_pos_pics = ps_slice_hdr->s_stref_picset.i1_num_pos_pics;
471
12.4k
                pi1_used = ps_slice_hdr->s_stref_picset.ai1_used;
472
12.4k
            }
473
474
63.4k
            if(ps_sps->i1_long_term_ref_pics_present_flag)
475
19.7k
            {
476
19.7k
                if(ps_sps->i1_num_long_term_ref_pics_sps > 0)
477
9.53k
                {
478
9.53k
                    UEV_PARSE("num_long_term_sps", value, ps_bitstrm);
479
9.53k
                    if(value > ps_sps->i1_num_long_term_ref_pics_sps)
480
222
                    {
481
222
                        return IHEVCD_INVALID_PARAMETER;
482
222
                    }
483
9.31k
                    ps_slice_hdr->i1_num_long_term_sps = value;
484
9.31k
                }
485
19.4k
                UEV_PARSE("num_long_term_pics", value, ps_bitstrm);
486
19.4k
                if(((ULWORD64)value + ps_slice_hdr->i1_num_long_term_sps + num_neg_pics +
487
19.4k
                    num_pos_pics) > (MAX_DPB_SIZE - 1))
488
167
                {
489
167
                    return IHEVCD_INVALID_PARAMETER;
490
167
                }
491
19.3k
                ps_slice_hdr->i1_num_long_term_pics = value;
492
493
140k
                for(i = 0; i < (ps_slice_hdr->i1_num_long_term_sps +
494
140k
                                ps_slice_hdr->i1_num_long_term_pics); i++)
495
121k
                {
496
121k
                    if(i < ps_slice_hdr->i1_num_long_term_sps)
497
1.07k
                    {
498
                        /* Use CLZ to compute Ceil( Log2( num_long_term_ref_pics_sps ) ) */
499
1.07k
                        if (ps_sps->i1_num_long_term_ref_pics_sps > 1)
500
676
                        {
501
676
                            WORD32 num_bits = 32 - CLZ(ps_sps->i1_num_long_term_ref_pics_sps - 1);
502
676
                            BITS_PARSE("lt_idx_sps[ i ]", value, ps_bitstrm, num_bits);
503
676
                            if(value >= ps_sps->i1_num_long_term_ref_pics_sps)
504
117
                            {
505
117
                                return IHEVCD_INVALID_PARAMETER;
506
117
                            }
507
676
                        }
508
396
                        else
509
396
                        {
510
396
                            value = 0;
511
396
                        }
512
955
                        ps_slice_hdr->ai4_poc_lsb_lt[i] = ps_sps->au2_lt_ref_pic_poc_lsb_sps[value];
513
955
                        ps_slice_hdr->ai1_used_by_curr_pic_lt_flag[i] = ps_sps->ai1_used_by_curr_pic_lt_sps_flag[value];
514
515
955
                    }
516
120k
                    else
517
120k
                    {
518
120k
                        BITS_PARSE("poc_lsb_lt[ i ]", value, ps_bitstrm, ps_sps->i1_log2_max_pic_order_cnt_lsb);
519
120k
                        ps_slice_hdr->ai4_poc_lsb_lt[i] = value;
520
521
120k
                        BITS_PARSE("used_by_curr_pic_lt_flag[ i ]", value, ps_bitstrm, 1);
522
120k
                        ps_slice_hdr->ai1_used_by_curr_pic_lt_flag[i] = value;
523
524
120k
                    }
525
121k
                    BITS_PARSE("delta_poc_msb_present_flag[ i ]", value, ps_bitstrm, 1);
526
121k
                    ps_slice_hdr->ai1_delta_poc_msb_present_flag[i] = value;
527
528
529
121k
                    ps_slice_hdr->ai1_delta_poc_msb_cycle_lt[i] = 0;
530
121k
                    if(ps_slice_hdr->ai1_delta_poc_msb_present_flag[i])
531
16.0k
                    {
532
533
16.0k
                        UEV_PARSE("delata_poc_msb_cycle_lt[ i ]", value, ps_bitstrm);
534
16.0k
                        ps_slice_hdr->ai1_delta_poc_msb_cycle_lt[i] = value;
535
16.0k
                    }
536
537
121k
                    if((i != 0) && (i != ps_slice_hdr->i1_num_long_term_sps))
538
108k
                    {
539
108k
                        ps_slice_hdr->ai1_delta_poc_msb_cycle_lt[i] += ps_slice_hdr->ai1_delta_poc_msb_cycle_lt[i - 1];
540
108k
                    }
541
542
121k
                }
543
19.3k
            }
544
545
203k
            for(i = 0; i < num_neg_pics + num_pos_pics; i++)
546
141k
            {
547
141k
                if(pi1_used[i])
548
116k
                {
549
116k
                    num_poc_total_curr++;
550
116k
                }
551
141k
            }
552
184k
            for(i = 0; i < ps_slice_hdr->i1_num_long_term_sps + ps_slice_hdr->i1_num_long_term_pics; i++)
553
121k
            {
554
121k
                if(ps_slice_hdr->ai1_used_by_curr_pic_lt_flag[i])
555
23.4k
                {
556
23.4k
                    num_poc_total_curr++;
557
23.4k
                }
558
121k
            }
559
560
561
62.9k
            if(ps_sps->i1_sps_temporal_mvp_enable_flag)
562
28.9k
            {
563
28.9k
                BITS_PARSE("enable_temporal_mvp_flag", value, ps_bitstrm, 1);
564
28.9k
                ps_slice_hdr->i1_slice_temporal_mvp_enable_flag = value;
565
28.9k
            }
566
567
62.9k
        }
568
122k
        ps_slice_hdr->i1_slice_sao_luma_flag = 0;
569
122k
        ps_slice_hdr->i1_slice_sao_chroma_flag = 0;
570
122k
        if(ps_sps->i1_sample_adaptive_offset_enabled_flag)
571
88.9k
        {
572
88.9k
            BITS_PARSE("slice_sao_luma_flag", value, ps_bitstrm, 1);
573
88.9k
            ps_slice_hdr->i1_slice_sao_luma_flag = value;
574
575
88.9k
            if (CHROMA_FMT_IDC_MONOCHROME != ps_sps->i1_chroma_format_idc) {
576
87.5k
                BITS_PARSE("slice_sao_chroma_flag", value, ps_bitstrm, 1);
577
87.5k
                ps_slice_hdr->i1_slice_sao_chroma_flag = value;
578
87.5k
            }
579
580
88.9k
        }
581
582
122k
        ps_slice_hdr->i1_max_num_merge_cand = 1;
583
122k
        ps_slice_hdr->i1_cabac_init_flag = 0;
584
585
122k
        ps_slice_hdr->i1_num_ref_idx_l0_active = 0;
586
122k
        ps_slice_hdr->i1_num_ref_idx_l1_active = 0;
587
122k
        ps_slice_hdr->i1_slice_cb_qp_offset = 0;
588
122k
        ps_slice_hdr->i1_slice_cr_qp_offset = 0;
589
122k
        if((PSLICE == ps_slice_hdr->i1_slice_type) ||
590
119k
           (BSLICE == ps_slice_hdr->i1_slice_type))
591
41.6k
        {
592
41.6k
            BITS_PARSE("num_ref_idx_active_override_flag", value, ps_bitstrm, 1);
593
41.6k
            ps_slice_hdr->i1_num_ref_idx_active_override_flag = value;
594
595
41.6k
            if(ps_slice_hdr->i1_num_ref_idx_active_override_flag)
596
20.4k
            {
597
20.4k
                UEV_PARSE("num_ref_idx_l0_active_minus1", value, ps_bitstrm);
598
20.4k
                if(value > MAX_DPB_SIZE - 2)
599
144
                {
600
144
                    return IHEVCD_INVALID_PARAMETER;
601
144
                }
602
20.2k
                ps_slice_hdr->i1_num_ref_idx_l0_active = value + 1;
603
604
20.2k
                if(BSLICE == ps_slice_hdr->i1_slice_type)
605
18.7k
                {
606
18.7k
                    UEV_PARSE("num_ref_idx_l1_active_minus1", value, ps_bitstrm);
607
18.7k
                    if(value > MAX_DPB_SIZE - 2)
608
212
                    {
609
212
                        return IHEVCD_INVALID_PARAMETER;
610
212
                    }
611
18.5k
                    ps_slice_hdr->i1_num_ref_idx_l1_active = value + 1;
612
18.5k
                }
613
614
20.2k
            }
615
21.2k
            else
616
21.2k
            {
617
21.2k
                ps_slice_hdr->i1_num_ref_idx_l0_active = ps_pps->i1_num_ref_idx_l0_default_active;
618
21.2k
                if(BSLICE == ps_slice_hdr->i1_slice_type)
619
19.8k
                {
620
19.8k
                    ps_slice_hdr->i1_num_ref_idx_l1_active = ps_pps->i1_num_ref_idx_l1_default_active;
621
19.8k
                }
622
21.2k
            }
623
624
41.3k
            if(0 == num_poc_total_curr)
625
4.16k
                return IHEVCD_IGNORE_SLICE;
626
37.1k
            if((ps_pps->i1_lists_modification_present_flag) && (num_poc_total_curr > 1))
627
9.41k
            {
628
9.41k
                ihevcd_ref_pic_list_modification(ps_bitstrm,
629
9.41k
                                                 ps_slice_hdr, num_poc_total_curr);
630
9.41k
            }
631
27.7k
            else
632
27.7k
            {
633
27.7k
                ps_slice_hdr->s_rplm.i1_ref_pic_list_modification_flag_l0 = 0;
634
27.7k
                ps_slice_hdr->s_rplm.i1_ref_pic_list_modification_flag_l1 = 0;
635
27.7k
            }
636
637
37.1k
            if(BSLICE == ps_slice_hdr->i1_slice_type)
638
34.6k
            {
639
34.6k
                BITS_PARSE("mvd_l1_zero_flag", value, ps_bitstrm, 1);
640
34.6k
                ps_slice_hdr->i1_mvd_l1_zero_flag = value;
641
34.6k
            }
642
643
37.1k
            ps_slice_hdr->i1_cabac_init_flag = 0;
644
37.1k
            if(ps_pps->i1_cabac_init_present_flag)
645
26.0k
            {
646
26.0k
                BITS_PARSE("cabac_init_flag", value, ps_bitstrm, 1);
647
26.0k
                ps_slice_hdr->i1_cabac_init_flag = value;
648
649
26.0k
            }
650
37.1k
            ps_slice_hdr->i1_collocated_from_l0_flag = 1;
651
37.1k
            ps_slice_hdr->i1_collocated_ref_idx = 0;
652
37.1k
            if(ps_slice_hdr->i1_slice_temporal_mvp_enable_flag)
653
16.8k
            {
654
16.8k
                if(BSLICE == ps_slice_hdr->i1_slice_type)
655
16.0k
                {
656
16.0k
                    BITS_PARSE("collocated_from_l0_flag", value, ps_bitstrm, 1);
657
16.0k
                    ps_slice_hdr->i1_collocated_from_l0_flag = value;
658
16.0k
                }
659
660
16.8k
                if((ps_slice_hdr->i1_collocated_from_l0_flag  &&  (ps_slice_hdr->i1_num_ref_idx_l0_active > 1)) ||
661
12.3k
                   (!ps_slice_hdr->i1_collocated_from_l0_flag  && (ps_slice_hdr->i1_num_ref_idx_l1_active > 1)))
662
10.1k
                {
663
10.1k
                    UEV_PARSE("collocated_ref_idx", value, ps_bitstrm);
664
10.1k
                    if((PSLICE == ps_slice_hdr->i1_slice_type || BSLICE == ps_slice_hdr->i1_slice_type) &&
665
10.1k
                                    ps_slice_hdr->i1_collocated_from_l0_flag)
666
4.56k
                    {
667
4.56k
                        if(value >= ps_slice_hdr->i1_num_ref_idx_l0_active)
668
332
                        {
669
332
                            return IHEVCD_INVALID_PARAMETER;
670
332
                        }
671
4.56k
                    }
672
9.84k
                    if(BSLICE == ps_slice_hdr->i1_slice_type && !ps_slice_hdr->i1_collocated_from_l0_flag)
673
5.61k
                    {
674
5.61k
                        if(value >= ps_slice_hdr->i1_num_ref_idx_l1_active)
675
141
                        {
676
141
                            return IHEVCD_INVALID_PARAMETER;
677
141
                        }
678
5.61k
                    }
679
9.70k
                    ps_slice_hdr->i1_collocated_ref_idx = value;
680
9.70k
                }
681
682
16.8k
            }
683
684
36.6k
            if((ps_pps->i1_weighted_pred_flag  &&   (PSLICE == ps_slice_hdr->i1_slice_type)) ||
685
35.1k
               (ps_pps->i1_weighted_bipred_flag  &&  (BSLICE == ps_slice_hdr->i1_slice_type)))
686
16.8k
            {
687
16.8k
                ihevcd_parse_pred_wt_ofst(ps_bitstrm, ps_sps, ps_pps, ps_slice_hdr);
688
16.8k
            }
689
36.6k
            UEV_PARSE("five_minus_max_num_merge_cand", value, ps_bitstrm);
690
36.6k
            if(value > 4)
691
3.32k
            {
692
3.32k
                return IHEVCD_INVALID_PARAMETER;
693
3.32k
            }
694
33.3k
            ps_slice_hdr->i1_max_num_merge_cand = 5 - value;
695
696
33.3k
        }
697
114k
        SEV_PARSE("slice_qp_delta", i4_value, ps_bitstrm);
698
114k
        if((i4_value < (MIN_HEVC_QP - ps_pps->i1_pic_init_qp - (6 * ps_sps->i1_bit_depth_luma_minus8))) ||
699
113k
           (i4_value > (MAX_HEVC_QP - ps_pps->i1_pic_init_qp)))
700
928
        {
701
928
            return IHEVCD_INVALID_PARAMETER;
702
928
        }
703
113k
        ps_slice_hdr->i1_slice_qp_delta = i4_value;
704
705
113k
        if(ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag)
706
50.0k
        {
707
50.0k
            SEV_PARSE("slice_cb_qp_offset", i4_value, ps_bitstrm);
708
50.0k
            if(i4_value < -12 || i4_value > 12)
709
506
            {
710
506
                return IHEVCD_INVALID_PARAMETER;
711
506
            }
712
49.5k
            ps_slice_hdr->i1_slice_cb_qp_offset = i4_value;
713
714
49.5k
            SEV_PARSE("slice_cr_qp_offset", i4_value, ps_bitstrm);
715
49.5k
            if(i4_value < -12 || i4_value > 12)
716
460
            {
717
460
                return IHEVCD_INVALID_PARAMETER;
718
460
            }
719
49.0k
            ps_slice_hdr->i1_slice_cr_qp_offset = i4_value;
720
721
49.0k
        }
722
112k
#ifdef ENABLE_MAIN_REXT_PROFILE
723
112k
        ps_slice_hdr->i1_cu_chroma_qp_offset_enabled_flag = 0;
724
112k
        if(ps_pps->i1_chroma_qp_offset_list_enabled_flag)
725
9.64k
        {
726
9.64k
            BITS_PARSE("cu_chroma_qp_offset_enabled_flag", value, ps_bitstrm, 1);
727
9.64k
            ps_slice_hdr->i1_cu_chroma_qp_offset_enabled_flag = value;
728
9.64k
        }
729
112k
#endif
730
112k
        ps_slice_hdr->i1_deblocking_filter_override_flag = 0;
731
112k
        ps_slice_hdr->i1_slice_disable_deblocking_filter_flag  = ps_pps->i1_pic_disable_deblocking_filter_flag;
732
112k
        ps_slice_hdr->i1_beta_offset_div2 = ps_pps->i1_beta_offset_div2;
733
112k
        ps_slice_hdr->i1_tc_offset_div2 = ps_pps->i1_tc_offset_div2;
734
735
112k
        disable_deblocking_filter_flag = ps_pps->i1_pic_disable_deblocking_filter_flag;
736
737
112k
        if(ps_pps->i1_deblocking_filter_control_present_flag)
738
19.3k
        {
739
740
19.3k
            if(ps_pps->i1_deblocking_filter_override_enabled_flag)
741
8.60k
            {
742
8.60k
                BITS_PARSE("deblocking_filter_override_flag", value, ps_bitstrm, 1);
743
8.60k
                ps_slice_hdr->i1_deblocking_filter_override_flag = value;
744
8.60k
            }
745
746
19.3k
            if(ps_slice_hdr->i1_deblocking_filter_override_flag)
747
4.00k
            {
748
4.00k
                BITS_PARSE("slice_disable_deblocking_filter_flag", value, ps_bitstrm, 1);
749
4.00k
                ps_slice_hdr->i1_slice_disable_deblocking_filter_flag = value;
750
4.00k
                disable_deblocking_filter_flag = ps_slice_hdr->i1_slice_disable_deblocking_filter_flag;
751
752
4.00k
                if(!ps_slice_hdr->i1_slice_disable_deblocking_filter_flag)
753
1.29k
                {
754
1.29k
                    SEV_PARSE("beta_offset_div2", i4_value, ps_bitstrm);
755
1.29k
                    if(i4_value < -6 || i4_value > 6)
756
311
                    {
757
311
                        return IHEVCD_INVALID_PARAMETER;
758
311
                    }
759
979
                    ps_slice_hdr->i1_beta_offset_div2 = i4_value;
760
761
979
                    SEV_PARSE("tc_offset_div2", i4_value, ps_bitstrm);
762
979
                    if(i4_value < -6 || i4_value > 6)
763
253
                    {
764
253
                        return IHEVCD_INVALID_PARAMETER;
765
253
                    }
766
726
                    ps_slice_hdr->i1_tc_offset_div2 = i4_value;
767
768
726
                }
769
4.00k
            }
770
19.3k
        }
771
772
111k
        ps_slice_hdr->i1_slice_loop_filter_across_slices_enabled_flag = ps_pps->i1_loop_filter_across_slices_enabled_flag;
773
111k
        if(ps_pps->i1_loop_filter_across_slices_enabled_flag  &&
774
42.7k
                        (ps_slice_hdr->i1_slice_sao_luma_flag  ||  ps_slice_hdr->i1_slice_sao_chroma_flag  || !disable_deblocking_filter_flag))
775
40.0k
        {
776
40.0k
            BITS_PARSE("slice_loop_filter_across_slices_enabled_flag", value, ps_bitstrm, 1);
777
40.0k
            ps_slice_hdr->i1_slice_loop_filter_across_slices_enabled_flag = value;
778
40.0k
        }
779
780
111k
    }
781
782
    /* Check sanity of slice */
783
123k
    if((!first_slice_in_pic_flag) &&
784
30.3k
                    (ps_codec->i4_pic_present))
785
20.0k
    {
786
20.0k
        slice_header_t *ps_slice_hdr_base = ps_codec->ps_slice_hdr_base;
787
788
789
        /* According to the standard, the above conditions must be satisfied - But for error resilience,
790
         * only the following conditions are checked */
791
20.0k
        if((ps_slice_hdr_base->i1_pps_id != ps_slice_hdr->i1_pps_id) ||
792
18.9k
                        (ps_slice_hdr_base->i4_pic_order_cnt_lsb != ps_slice_hdr->i4_pic_order_cnt_lsb))
793
1.73k
        {
794
1.73k
            return IHEVCD_IGNORE_SLICE;
795
1.73k
        }
796
797
20.0k
    }
798
799
800
121k
    if(0 == ps_codec->i4_pic_present)
801
103k
    {
802
103k
        ps_slice_hdr->i4_abs_pic_order_cnt = ihevcd_calc_poc(ps_codec, ps_nal, ps_sps->i1_log2_max_pic_order_cnt_lsb, ps_slice_hdr->i4_pic_order_cnt_lsb);
803
103k
    }
804
18.2k
    else
805
18.2k
    {
806
18.2k
        ps_slice_hdr->i4_abs_pic_order_cnt = ps_codec->s_parse.i4_abs_pic_order_cnt;
807
18.2k
    }
808
809
810
121k
    if(!first_slice_in_pic_flag)
811
28.5k
    {
812
        /* Check if the current slice belongs to the same pic (Pic being parsed) */
813
28.5k
        if(ps_codec->s_parse.i4_abs_pic_order_cnt == ps_slice_hdr->i4_abs_pic_order_cnt)
814
20.4k
        {
815
816
            /* If the Next CTB's index is less than the slice address,
817
             * the previous slice is incomplete.
818
             * Indicate slice error, and treat the remaining CTBs as skip */
819
20.4k
            if(slice_address > ps_codec->s_parse.i4_next_ctb_indx)
820
799
            {
821
799
                if(ps_codec->i4_pic_present)
822
704
                {
823
704
                    prev_slice_incomplete_flag = 1;
824
704
                    ps_codec->i4_slice_error = 1;
825
704
                    ps_codec->s_parse.i4_cur_slice_idx--;
826
704
                    if(ps_codec->s_parse.i4_cur_slice_idx < 0)
827
0
                        ps_codec->s_parse.i4_cur_slice_idx = 0;
828
704
                }
829
799
                return IHEVCD_IGNORE_SLICE;
830
799
            }
831
            /* If the slice address is less than the next CTB's index,
832
             * extra CTBs have been decoded in the previous slice.
833
             * Ignore the current slice. Treat it as incomplete */
834
19.6k
            else if(slice_address < ps_codec->s_parse.i4_next_ctb_indx)
835
3.82k
            {
836
3.82k
                return IHEVCD_IGNORE_SLICE;
837
3.82k
            }
838
15.8k
            else
839
15.8k
            {
840
15.8k
                ps_codec->i4_slice_error = 0;
841
15.8k
            }
842
20.4k
        }
843
844
        /* The current slice does not belong to the pic that is being parsed */
845
8.10k
        else
846
8.10k
        {
847
            /* The previous pic is incomplete.
848
             * Treat the remaining CTBs as skip */
849
8.10k
            if(ps_codec->i4_pic_present)
850
0
            {
851
0
                slice_header_t *ps_slice_hdr_next;
852
0
                ps_codec->i4_slice_error = 1;
853
0
                ps_codec->s_parse.i4_cur_slice_idx--;
854
0
                if(ps_codec->s_parse.i4_cur_slice_idx < 0)
855
0
                    ps_codec->s_parse.i4_cur_slice_idx = 0;
856
857
0
                ps_slice_hdr_next = ps_codec->s_parse.ps_slice_hdr_base + ((ps_codec->s_parse.i4_cur_slice_idx + 1) & (MAX_SLICE_HDR_CNT - 1));
858
0
                ps_slice_hdr_next->i2_ctb_x = 0;
859
0
                ps_slice_hdr_next->i2_ctb_y = ps_codec->s_parse.ps_sps->i2_pic_ht_in_ctb;
860
0
                return ret;
861
0
            }
862
863
            /* If the previous pic is complete,
864
             * return if the current slice is dependant
865
             * otherwise, update the parse context's POC */
866
8.10k
            else
867
8.10k
            {
868
8.10k
                if(ps_slice_hdr->i1_dependent_slice_flag)
869
0
                    return IHEVCD_IGNORE_SLICE;
870
871
8.10k
                ps_codec->s_parse.i4_abs_pic_order_cnt = ps_slice_hdr->i4_abs_pic_order_cnt;
872
8.10k
            }
873
8.10k
        }
874
28.5k
    }
875
876
    /* If the slice is the first slice in the pic, update the parse context's POC */
877
92.7k
    else
878
92.7k
    {
879
        /* If the first slice is repeated, ignore the second occurrence
880
         * If any other slice is repeated, the CTB addr will be greater than the slice addr,
881
         * and hence the second occurrence is ignored */
882
92.7k
        if(ps_codec->s_parse.i4_abs_pic_order_cnt == ps_slice_hdr->i4_abs_pic_order_cnt)
883
21.3k
            return IHEVCD_IGNORE_SLICE;
884
885
71.4k
        ps_codec->s_parse.i4_abs_pic_order_cnt = ps_slice_hdr->i4_abs_pic_order_cnt;
886
71.4k
    }
887
888
    // printf("POC: %d\n", ps_slice_hdr->i4_abs_pic_order_cnt);
889
    // AEV_TRACE("POC", ps_slice_hdr->i4_abs_pic_order_cnt, 0);
890
95.3k
    ps_slice_hdr->i4_num_entry_point_offsets = 0;
891
95.3k
    if((ps_pps->i1_tiles_enabled_flag) ||
892
78.3k
       (ps_pps->i1_entropy_coding_sync_enabled_flag))
893
22.9k
    {
894
22.9k
        UEV_PARSE("num_entry_point_offsets", value, ps_bitstrm);
895
22.9k
        ps_slice_hdr->i4_num_entry_point_offsets = value;
896
897
22.9k
        {
898
22.9k
            WORD32 max_num_entry_point_offsets;
899
22.9k
            if((ps_pps->i1_tiles_enabled_flag) &&
900
17.0k
                            (ps_pps->i1_entropy_coding_sync_enabled_flag))
901
7.12k
            {
902
7.12k
                max_num_entry_point_offsets = ps_pps->i1_num_tile_columns * ps_sps->i2_pic_ht_in_ctb - 1;
903
7.12k
            }
904
15.8k
            else if(ps_pps->i1_tiles_enabled_flag)
905
9.90k
            {
906
9.90k
                max_num_entry_point_offsets = ps_pps->i1_num_tile_columns * ps_pps->i1_num_tile_rows - 1 ;
907
9.90k
            }
908
5.94k
            else
909
5.94k
            {
910
5.94k
                max_num_entry_point_offsets = (ps_sps->i2_pic_ht_in_ctb - 1);
911
5.94k
            }
912
913
22.9k
            if(ps_slice_hdr->i4_num_entry_point_offsets < 0 || ps_slice_hdr->i4_num_entry_point_offsets > max_num_entry_point_offsets)
914
813
            {
915
813
                return IHEVCD_INVALID_PARAMETER;
916
813
            }
917
22.9k
        }
918
919
22.1k
        if(ps_slice_hdr->i4_num_entry_point_offsets > 0)
920
4.44k
        {
921
4.44k
            UEV_PARSE("offset_len_minus1", value, ps_bitstrm);
922
4.44k
            if(value > 31)
923
214
            {
924
214
                return IHEVCD_INVALID_PARAMETER;
925
214
            }
926
4.23k
            ps_slice_hdr->i1_offset_len = value + 1;
927
928
15.8k
            for(i = 0; i < ps_slice_hdr->i4_num_entry_point_offsets; i++)
929
11.6k
            {
930
11.6k
                BITS_PARSE("entry_point_offset", value, ps_bitstrm, ps_slice_hdr->i1_offset_len);
931
932
                /* TODO: pu4_entry_point_offset needs to be initialized */
933
                //ps_slice_hdr->pu4_entry_point_offset[i] = value;
934
11.6k
            }
935
936
4.23k
        }
937
22.1k
    }
938
939
94.3k
    if(ps_pps->i1_slice_header_extension_present_flag)
940
10.4k
    {
941
10.4k
        UEV_PARSE("slice_header_extension_length", value, ps_bitstrm);
942
10.4k
        if(value > 256)
943
450
        {
944
450
            return IHEVCD_INVALID_PARAMETER;
945
450
        }
946
10.0k
        ps_slice_hdr->i2_slice_header_extension_length = value;
947
948
949
33.1k
        for(i = 0; i < ps_slice_hdr->i2_slice_header_extension_length; i++)
950
23.1k
        {
951
23.1k
            BITS_PARSE("slice_header_extension_data_byte", value, ps_bitstrm, 8);
952
23.1k
        }
953
954
10.0k
    }
955
956
93.8k
    ihevcd_bits_flush_to_byte_boundary(ps_bitstrm);
957
958
93.8k
    if((UWORD8 *)ps_bitstrm->pu4_buf > ps_bitstrm->pu1_buf_max)
959
2.73k
        return IHEVCD_INVALID_PARAMETER;
960
961
91.1k
    {
962
91.1k
        dpb_mgr_t *ps_dpb_mgr = (dpb_mgr_t *)ps_codec->pv_dpb_mgr;
963
91.1k
        WORD32 r_idx;
964
965
91.1k
        if((NAL_IDR_W_LP == ps_slice_hdr->i1_nal_unit_type) ||
966
75.0k
                        (NAL_IDR_N_LP == ps_slice_hdr->i1_nal_unit_type)  ||
967
38.6k
                        (NAL_BLA_N_LP == ps_slice_hdr->i1_nal_unit_type)  ||
968
37.5k
                        (NAL_BLA_W_DLP == ps_slice_hdr->i1_nal_unit_type) ||
969
35.7k
                        (NAL_BLA_W_LP == ps_slice_hdr->i1_nal_unit_type)  ||
970
34.4k
                        (0 == ps_codec->u4_pic_cnt))
971
68.1k
        {
972
4.42M
            for(i = 0; i < MAX_DPB_BUFS; i++)
973
4.35M
            {
974
4.35M
                if(ps_dpb_mgr->as_dpb_info[i].ps_pic_buf)
975
41.8k
                {
976
41.8k
                    pic_buf_t *ps_pic_buf = ps_dpb_mgr->as_dpb_info[i].ps_pic_buf;
977
41.8k
                    mv_buf_t *ps_mv_buf;
978
979
                    /* Long term index is set to MAX_DPB_BUFS to ensure it is not added as LT */
980
41.8k
                    ihevc_dpb_mgr_del_ref((dpb_mgr_t *)ps_codec->pv_dpb_mgr, (buf_mgr_t *)ps_codec->pv_pic_buf_mgr, ps_pic_buf->i4_abs_poc);
981
                    /* Find buffer id of the MV bank corresponding to the buffer being freed (Buffer with POC of u4_abs_poc) */
982
41.8k
                    ps_mv_buf = (mv_buf_t *)ps_codec->ps_mv_buf;
983
42.7k
                    for(j = 0; j < ps_codec->i4_max_dpb_size; j++)
984
42.7k
                    {
985
42.7k
                        if(ps_mv_buf && ps_mv_buf->i4_abs_poc == ps_pic_buf->i4_abs_poc)
986
41.8k
                        {
987
41.8k
                            ihevc_buf_mgr_release((buf_mgr_t *)ps_codec->pv_mv_buf_mgr, j, BUF_MGR_REF);
988
41.8k
                            break;
989
41.8k
                        }
990
856
                        ps_mv_buf++;
991
856
                    }
992
993
41.8k
                }
994
995
4.35M
            }
996
997
            /* Initialize the reference lists to NULL
998
             * This is done to take care of the cases where the first pic is not IDR
999
             * but the reference list is not created for the first pic because
1000
             * pic count is zero leaving the reference list uninitialised  */
1001
1.15M
            for(r_idx = 0; r_idx < MAX_DPB_SIZE; r_idx++)
1002
1.08M
            {
1003
1.08M
                ps_slice_hdr->as_ref_pic_list0[r_idx].pv_pic_buf = NULL;
1004
1.08M
                ps_slice_hdr->as_ref_pic_list0[r_idx].pv_mv_buf = NULL;
1005
1006
1.08M
                ps_slice_hdr->as_ref_pic_list1[r_idx].pv_pic_buf = NULL;
1007
1.08M
                ps_slice_hdr->as_ref_pic_list1[r_idx].pv_mv_buf = NULL;
1008
1.08M
            }
1009
1010
68.1k
        }
1011
23.0k
        else
1012
23.0k
        {
1013
23.0k
            ret = ihevcd_ref_list(ps_codec, ps_pps, ps_sps, ps_slice_hdr);
1014
23.0k
            if ((WORD32)IHEVCD_SUCCESS != ret)
1015
1.18k
            {
1016
1.18k
                return ret;
1017
1.18k
            }
1018
1019
23.0k
        }
1020
1021
91.1k
    }
1022
1023
    /* Fill the remaining entries of the reference lists with the nearest POC
1024
     * This is done to handle cases where there is a corruption in the reference index */
1025
89.9k
    if(ps_codec->i4_pic_present)
1026
15.8k
    {
1027
15.8k
        pic_buf_t *ps_pic_buf_ref;
1028
15.8k
        mv_buf_t *ps_mv_buf_ref;
1029
15.8k
        WORD32 r_idx;
1030
15.8k
        dpb_mgr_t *ps_dpb_mgr = (dpb_mgr_t *)ps_codec->pv_dpb_mgr;
1031
15.8k
        buf_mgr_t *ps_mv_buf_mgr = (buf_mgr_t *)ps_codec->pv_mv_buf_mgr;
1032
1033
15.8k
        ps_pic_buf_ref = ihevc_dpb_mgr_get_ref_by_nearest_poc(ps_dpb_mgr, ps_slice_hdr->i4_abs_pic_order_cnt);
1034
15.8k
        if(NULL == ps_pic_buf_ref)
1035
8.71k
        {
1036
8.71k
            ps_pic_buf_ref = ps_codec->as_process[0].ps_cur_pic;
1037
8.71k
            ps_mv_buf_ref = ps_codec->s_parse.ps_cur_mv_buf;
1038
8.71k
        }
1039
7.11k
        else
1040
7.11k
        {
1041
7.11k
            ps_mv_buf_ref = ihevcd_mv_mgr_get_poc(ps_mv_buf_mgr, ps_pic_buf_ref->i4_abs_poc);
1042
7.11k
        }
1043
1044
32.3k
        for(r_idx = 0; r_idx < ps_slice_hdr->i1_num_ref_idx_l0_active; r_idx++)
1045
16.5k
        {
1046
16.5k
            if(NULL == ps_slice_hdr->as_ref_pic_list0[r_idx].pv_pic_buf)
1047
3.83k
            {
1048
3.83k
                ps_slice_hdr->as_ref_pic_list0[r_idx].pv_pic_buf = (void *)ps_pic_buf_ref;
1049
3.83k
                ps_slice_hdr->as_ref_pic_list0[r_idx].pv_mv_buf = (void *)ps_mv_buf_ref;
1050
3.83k
            }
1051
16.5k
        }
1052
1053
252k
        for(r_idx = ps_slice_hdr->i1_num_ref_idx_l0_active; r_idx < MAX_DPB_SIZE; r_idx++)
1054
236k
        {
1055
236k
            ps_slice_hdr->as_ref_pic_list0[r_idx].pv_pic_buf = (void *)ps_pic_buf_ref;
1056
236k
            ps_slice_hdr->as_ref_pic_list0[r_idx].pv_mv_buf = (void *)ps_mv_buf_ref;
1057
236k
        }
1058
1059
31.8k
        for(r_idx = 0; r_idx < ps_slice_hdr->i1_num_ref_idx_l1_active; r_idx++)
1060
16.0k
        {
1061
16.0k
            if(NULL == ps_slice_hdr->as_ref_pic_list1[r_idx].pv_pic_buf)
1062
4.29k
            {
1063
4.29k
                ps_slice_hdr->as_ref_pic_list1[r_idx].pv_pic_buf = (void *)ps_pic_buf_ref;
1064
4.29k
                ps_slice_hdr->as_ref_pic_list1[r_idx].pv_mv_buf = (void *)ps_mv_buf_ref;
1065
4.29k
            }
1066
16.0k
        }
1067
1068
252k
        for(r_idx = ps_slice_hdr->i1_num_ref_idx_l1_active; r_idx < MAX_DPB_SIZE; r_idx++)
1069
237k
        {
1070
237k
            ps_slice_hdr->as_ref_pic_list1[r_idx].pv_pic_buf = (void *)ps_pic_buf_ref;
1071
237k
            ps_slice_hdr->as_ref_pic_list1[r_idx].pv_mv_buf = (void *)ps_mv_buf_ref;
1072
237k
        }
1073
15.8k
    }
1074
1075
    /* Update slice address in the header */
1076
89.9k
    if(!ps_slice_hdr->i1_first_slice_in_pic_flag)
1077
23.2k
    {
1078
23.2k
        ps_slice_hdr->i2_ctb_x = slice_address % ps_sps->i2_pic_wd_in_ctb;
1079
23.2k
        ps_slice_hdr->i2_ctb_y = slice_address / ps_sps->i2_pic_wd_in_ctb;
1080
1081
23.2k
        if(!ps_slice_hdr->i1_dependent_slice_flag)
1082
13.9k
        {
1083
13.9k
            ps_slice_hdr->i2_independent_ctb_x = ps_slice_hdr->i2_ctb_x;
1084
13.9k
            ps_slice_hdr->i2_independent_ctb_y = ps_slice_hdr->i2_ctb_y;
1085
13.9k
        }
1086
23.2k
    }
1087
66.7k
    else
1088
66.7k
    {
1089
66.7k
        ps_slice_hdr->i2_ctb_x = 0;
1090
66.7k
        ps_slice_hdr->i2_ctb_y = 0;
1091
1092
66.7k
        ps_slice_hdr->i2_independent_ctb_x = 0;
1093
66.7k
        ps_slice_hdr->i2_independent_ctb_y = 0;
1094
66.7k
    }
1095
1096
    /* If the first slice in the pic is missing, copy the current slice header to
1097
     * the first slice's header */
1098
89.9k
    if((!first_slice_in_pic_flag) &&
1099
23.2k
                    (0 == ps_codec->i4_pic_present))
1100
7.41k
    {
1101
7.41k
        slice_header_t *ps_slice_hdr_prev = ps_codec->s_parse.ps_slice_hdr_base;
1102
7.41k
        ihevcd_copy_slice_hdr(ps_codec, 0, (ps_codec->s_parse.i4_cur_slice_idx & (MAX_SLICE_HDR_CNT - 1)));
1103
1104
7.41k
        prev_slice_incomplete_flag = 1;
1105
7.41k
        ASSERT(ps_codec->s_parse.i4_cur_slice_idx == 1);
1106
1107
7.41k
        ps_slice_hdr_prev->i2_ctb_x = 0;
1108
7.41k
        ps_slice_hdr_prev->i2_ctb_y = 0;
1109
1110
7.41k
        ps_codec->s_parse.i4_ctb_x = 0;
1111
7.41k
        ps_codec->s_parse.i4_ctb_y = 0;
1112
1113
7.41k
        ps_codec->s_parse.i4_cur_slice_idx = 0;
1114
1115
7.41k
        if((ps_slice_hdr->i2_ctb_x == 0) &&
1116
1.49k
                        (ps_slice_hdr->i2_ctb_y == 0))
1117
0
        {
1118
0
            ps_slice_hdr->i2_ctb_x++;
1119
0
        }
1120
7.41k
    }
1121
1122
89.9k
    {
1123
        /* If skip B is enabled,
1124
         * ignore pictures that are non-reference
1125
         * TODO: (i1_nal_unit_type < NAL_BLA_W_LP) && (i1_nal_unit_type % 2 == 0) only says it is
1126
         * sub-layer non-reference slice. May need to find a way to detect actual non-reference pictures*/
1127
1128
89.9k
        if((i1_nal_unit_type < NAL_BLA_W_LP) &&
1129
31.8k
                        (i1_nal_unit_type % 2 == 0))
1130
25.5k
        {
1131
25.5k
            if(IVD_SKIP_B == ps_codec->e_pic_skip_mode)
1132
0
                return IHEVCD_IGNORE_SLICE;
1133
25.5k
        }
1134
1135
        /* If skip PB is enabled,
1136
         * decode only I slices */
1137
89.9k
        if((IVD_SKIP_PB == ps_codec->e_pic_skip_mode) &&
1138
0
                        (ISLICE != ps_slice_hdr->i1_slice_type))
1139
0
        {
1140
0
            return IHEVCD_IGNORE_SLICE;
1141
0
        }
1142
1143
89.9k
        if(ps_codec->u4_thumbnail_mode && (ps_codec->u4_pic_cnt >= 1))
1144
0
        {
1145
0
            return IHEVCD_IGNORE_SLICE;
1146
0
        }
1147
89.9k
    }
1148
1149
89.9k
    if(prev_slice_incomplete_flag)
1150
7.41k
    {
1151
7.41k
        ps_codec->i4_slice_error = 1;
1152
7.41k
        ps_codec->s_parse.i4_cur_slice_idx--;
1153
7.41k
        if(ps_codec->s_parse.i4_cur_slice_idx < 0)
1154
7.41k
            ps_codec->s_parse.i4_cur_slice_idx = 0;
1155
7.41k
    }
1156
1157
89.9k
    return ret;
1158
89.9k
}