Coverage Report

Created: 2026-12-01 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libhevc/decoder/ihevcd_decode.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_decode.c
22
 *
23
 * @brief
24
 *  Contains codecs main decode function
25
 *
26
 * @author
27
 *  Harish
28
 *
29
 * @par List of Functions:
30
 * - fill_outargs()
31
 * - ihevcd_decode
32
 * @remarks
33
 *  None
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
#include "ithread.h"
51
52
#include "ihevc_defs.h"
53
#include "ihevc_debug.h"
54
#include "ihevc_structs.h"
55
#include "ihevc_macros.h"
56
#include "ihevc_platform_macros.h"
57
#include "ihevc_cabac_tables.h"
58
#include "ihevc_disp_mgr.h"
59
#include "ihevc_buf_mgr.h"
60
#include "ihevc_dpb_mgr.h"
61
#include "ihevc_error.h"
62
63
#include "ihevcd_defs.h"
64
#include "ihevcd_function_selector.h"
65
#include "ihevcd_structs.h"
66
#include "ihevcd_error.h"
67
#include "ihevcd_nal.h"
68
#include "ihevcd_bitstream.h"
69
#include "ihevcd_fmt_conv.h"
70
#include "ihevcd_job_queue.h"
71
#include "ihevcd_debug.h"
72
#include "ihevcd_parse_slice.h"
73
#include "ihevcd_process_slice.h"
74
#include "ihevcd_ittiam_logo.h"
75
#include "ihevcd_profile.h"
76
77
#define NUM_FRAMES_LIMIT_ENABLED 0
78
79
#if NUM_FRAMES_LIMIT_ENABLED
80
#define NUM_FRAMES_LIMIT 10000
81
#else
82
394k
#define NUM_FRAMES_LIMIT 0x7FFFFFFF
83
#endif
84
85
IHEVCD_ERROR_T ihevcd_check_out_buf_size(codec_t *ps_codec);
86
IHEVCD_ERROR_T ihevcd_fmt_conv(codec_t *ps_codec,
87
                               process_ctxt_t *ps_proc,
88
                               UWORD8 *pu1_y_dst,
89
                               UWORD8 *pu1_u_dst,
90
                               UWORD8 *pu1_v_dst,
91
                               WORD32 cur_row,
92
                               WORD32 num_rows);
93
WORD32 ihevcd_init(codec_t *ps_codec);
94
95
WORD32 ihevcd_allocate_dynamic_bufs(codec_t *ps_codec);
96
WORD32 ihevcd_free_dynamic_bufs(codec_t *ps_codec);
97
/*****************************************************************************/
98
/* Function Prototypes                                                       */
99
/*****************************************************************************/
100
101
102
/**
103
 *******************************************************************************
104
 *
105
 * @brief Fills output arguments for decode process
106
 *
107
 * @par   Description
108
 * Fills elements in the output structure based on the current state
109
 *
110
 * @param[in] ps_codec
111
 * Codec context
112
 *
113
 * @param[in] ps_dec_ip
114
 * Pointer to input structure
115
 *
116
 * @param[in] ps_dec_op
117
 * Pointer to output structure
118
 *
119
 * @returns none
120
 *
121
 * @remarks
122
 *
123
 *******************************************************************************
124
 */
125
static UWORD32 ihevcd_map_error(IHEVCD_ERROR_T e_error)
126
393k
{
127
393k
    UWORD32 error_code = 0;
128
393k
    error_code = e_error;
129
393k
    switch(error_code)
130
393k
    {
131
289k
        case IHEVCD_SUCCESS :
132
289k
            break;
133
0
        case IHEVCD_INIT_NOT_DONE:
134
0
        case IHEVCD_LEVEL_UNSUPPORTED:
135
0
        case IHEVCD_NUM_REF_UNSUPPORTED:
136
0
        case IHEVCD_NUM_REORDER_UNSUPPORTED:
137
0
        case IHEVCD_NUM_EXTRA_DISP_UNSUPPORTED:
138
0
        case IHEVCD_INSUFFICIENT_MEM_MVBANK:
139
0
        case IHEVCD_INSUFFICIENT_MEM_PICBUF:
140
66
        case IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC:
141
3.50k
        case IHEVCD_UNSUPPORTED_BIT_DEPTH:
142
3.50k
        case IVD_MEM_ALLOC_FAILED:
143
4.66k
        case IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED:
144
4.66k
            error_code |= 1 << IVD_FATALERROR;
145
4.66k
            break;
146
0
        case IHEVCD_INVALID_DISP_STRD:
147
0
        case IHEVCD_CXA_VERS_BUF_INSUFFICIENT:
148
0
        case IHEVCD_UNSUPPORTED_VPS_ID:
149
27.5k
        case IHEVCD_UNSUPPORTED_SPS_ID:
150
28.9k
        case IHEVCD_UNSUPPORTED_PPS_ID:
151
28.9k
        case IHEVCD_BUF_MGR_ERROR:
152
29.3k
        case IHEVCD_NO_FREE_MVBANK:
153
29.3k
        case IHEVCD_NO_FREE_PICBUF:
154
29.3k
        case IHEVCD_SLICE_IN_HEADER_MODE:
155
29.3k
        case IHEVCD_END_OF_SEQUENCE:
156
29.3k
        case IVD_DEC_FRM_SKIPPED:
157
29.3k
            break;
158
69.9k
        default:
159
69.9k
            break;
160
393k
    }
161
393k
    return error_code;
162
393k
}
163
/**
164
 *******************************************************************************
165
 *
166
 * @brief Fills output arguments for decode process
167
 *
168
 * @par   Description
169
 * Fills elements in the output structure based on the current state
170
 *
171
 * @param[in] ps_codec
172
 * Codec context
173
 *
174
 * @param[in] ps_dec_ip
175
 * Pointer to input structure
176
 *
177
 * @param[in] ps_dec_op
178
 * Pointer to output structure
179
 *
180
 * @returns none
181
 *
182
 * @remarks
183
 *
184
 *******************************************************************************
185
 */
186
static void ihevcd_fill_outargs(codec_t *ps_codec,
187
                                void *pv_api_ip,
188
                                void *pv_api_op)
189
393k
{
190
191
393k
    ihevcd_cxa_video_decode_ip_t *ps_hevcd_dec_ip;
192
393k
    ihevcd_cxa_video_decode_op_t *ps_hevcd_dec_op;
193
393k
    ivd_video_decode_ip_t *ps_dec_ip;
194
393k
    ivd_video_decode_op_t *ps_dec_op;
195
196
393k
    ps_hevcd_dec_ip = (ihevcd_cxa_video_decode_ip_t *)pv_api_ip;
197
393k
    ps_hevcd_dec_op = (ihevcd_cxa_video_decode_op_t *)pv_api_op;
198
393k
    ps_dec_ip = &ps_hevcd_dec_ip->s_ivd_video_decode_ip_t;
199
393k
    ps_dec_op = &ps_hevcd_dec_op->s_ivd_video_decode_op_t;
200
201
393k
    ps_dec_op->u4_error_code = ihevcd_map_error((IHEVCD_ERROR_T)ps_codec->i4_error_code);
202
393k
    ps_dec_op->u4_num_bytes_consumed = ps_dec_ip->u4_num_Bytes
203
393k
                    - ps_codec->i4_bytes_remaining;
204
393k
    if(ps_codec->i4_sps_done)
205
226k
    {
206
226k
        ps_dec_op->u4_pic_wd = ps_codec->i4_disp_wd;
207
226k
        ps_dec_op->u4_pic_ht = ps_codec->i4_disp_ht;
208
226k
        ps_dec_op->u4_bit_depth = ps_codec->i4_bit_depth_luma;
209
226k
    }
210
167k
    else
211
167k
    {
212
167k
        ps_dec_op->u4_pic_wd = 0;
213
167k
        ps_dec_op->u4_pic_ht = 0;
214
167k
        ps_dec_op->u4_bit_depth = 0;
215
167k
    }
216
217
393k
    ps_dec_op->e_pic_type = ps_codec->e_dec_pic_type;
218
393k
    ps_dec_op->u4_frame_decoded_flag = ps_codec->i4_pic_present;
219
393k
    ps_dec_op->u4_new_seq = 0;
220
221
393k
    ps_dec_op->u4_output_present = 0;
222
393k
    ps_dec_op->u4_progressive_frame_flag = 1;
223
393k
    ps_dec_op->i4_display_index = -1;
224
393k
    ps_dec_op->i4_reorder_depth = -1;
225
393k
    if(ps_codec->i4_sps_done)
226
226k
    {
227
226k
        sps_t *ps_sps = (ps_codec->s_parse.ps_sps_base + ps_codec->i4_sps_id);
228
226k
        profile_tier_lvl_info_t *ps_ptl;
229
226k
        ps_ptl = &ps_sps->s_ptl;
230
226k
        if((0 == ps_ptl->s_ptl_gen.i1_general_progressive_source_flag) &&
231
179k
           (1 == ps_ptl->s_ptl_gen.i1_general_interlaced_source_flag))
232
17.3k
        {
233
17.3k
            ps_dec_op->u4_progressive_frame_flag = 0;
234
17.3k
        }
235
226k
        ps_dec_op->i4_reorder_depth = ps_codec->u4_disable_ref_retention ? 0 :
236
226k
                        ps_sps->ai1_sps_max_num_reorder_pics[ps_sps->i1_sps_max_sub_layers - 1];
237
226k
    }
238
239
393k
    ps_dec_op->u4_is_ref_flag = 1;
240
393k
    ps_dec_op->e_output_format = ps_codec->e_chroma_fmt;
241
393k
    ps_dec_op->u4_is_ref_flag = 1;
242
243
393k
    ps_dec_op->e4_fld_type = IV_FLD_TYPE_DEFAULT;
244
393k
    ps_dec_op->u4_ts = (UWORD32)(-1);
245
393k
    ps_dec_op->u4_disp_buf_id = ps_codec->i4_disp_buf_id;
246
393k
    if(ps_codec->i4_flush_mode)
247
692
    {
248
692
        ps_dec_op->u4_num_bytes_consumed = 0;
249
        /*In the case of flush ,since no frame is decoded set pic type as invalid*/
250
692
        ps_dec_op->u4_is_ref_flag = 0;
251
692
        ps_dec_op->e_pic_type = IV_NA_FRAME;
252
692
        ps_dec_op->u4_frame_decoded_flag = 0;
253
254
692
    }
255
393k
    ps_dec_op->s_disp_frm_buf.u4_bit_depth      = ps_dec_op->u4_bit_depth;
256
257
    /* If there is a display buffer */
258
393k
    if(ps_codec->ps_disp_buf)
259
86.4k
    {
260
86.4k
        pic_buf_t *ps_disp_buf = ps_codec->ps_disp_buf;
261
86.4k
#ifndef DISABLE_SEI
262
86.4k
        sei_params_t *ps_sei = &ps_disp_buf->s_sei_params;
263
264
86.4k
        if(ps_sei->i1_sei_parameters_present_flag &&
265
1.12k
           ps_sei->i1_pic_timing_params_present_flag)
266
988
        {
267
988
            UWORD32 u4_pic_struct;
268
988
            u4_pic_struct = ps_sei->s_pic_timing_sei_params.u4_pic_struct;
269
988
            switch(u4_pic_struct)
270
988
            {
271
66
                case 1:
272
66
                    ps_dec_op->e4_fld_type = IV_TOP_FLD;
273
66
                    ps_dec_op->u4_progressive_frame_flag = 0;
274
66
                    break;
275
67
                case 2:
276
67
                    ps_dec_op->e4_fld_type = IV_BOT_FLD;
277
67
                    ps_dec_op->u4_progressive_frame_flag = 0;
278
67
                    break;
279
853
                case 0:
280
855
                default:
281
855
                    ps_dec_op->e4_fld_type = IV_FLD_TYPE_DEFAULT;
282
855
                    ps_dec_op->u4_progressive_frame_flag = 1;
283
855
                    break;
284
988
            }
285
988
        }
286
86.4k
#endif
287
86.4k
        ps_dec_op->i4_display_index = ps_disp_buf->i4_abs_poc;
288
86.4k
        ps_dec_op->u4_output_present = 1;
289
86.4k
        ps_dec_op->u4_ts = ps_disp_buf->u4_ts;
290
86.4k
        if((ps_codec->i4_flush_mode == 0) && (ps_codec->s_parse.i4_end_of_frame == 0))
291
0
            ps_dec_op->u4_output_present = 0;
292
86.4k
        ps_dec_op->s_disp_frm_buf.u4_y_wd = ps_codec->i4_disp_wd;
293
86.4k
        ps_dec_op->s_disp_frm_buf.u4_y_ht = ps_codec->i4_disp_ht;
294
295
86.4k
        if(ps_codec->i4_share_disp_buf)
296
0
        {
297
0
            ps_dec_op->s_disp_frm_buf.pv_y_buf = ps_disp_buf->pu1_luma;
298
0
            if(ps_codec->e_chroma_fmt != IV_YUV_420P)
299
0
            {
300
0
                ps_dec_op->s_disp_frm_buf.pv_u_buf = ps_disp_buf->pu1_chroma;
301
0
                ps_dec_op->s_disp_frm_buf.pv_v_buf = NULL;
302
0
            }
303
0
            else
304
0
            {
305
0
                WORD32 i;
306
0
                UWORD8 *pu1_u_dst = NULL, *pu1_v_dst = NULL;
307
0
                for(i = 0; i < ps_codec->i4_share_disp_buf_cnt; i++)
308
0
                {
309
0
                    WORD32 diff = ps_disp_buf->pu1_luma - ps_codec->s_disp_buffer[i].pu1_bufs[0];
310
0
                    if(diff == (ps_codec->i4_strd * PAD_TOP + PAD_LEFT))
311
0
                    {
312
0
                        pu1_u_dst = ps_codec->s_disp_buffer[i].pu1_bufs[1];
313
0
                        pu1_u_dst += (ps_codec->i4_strd * PAD_TOP) / 4 + (PAD_LEFT / 2);
314
315
0
                        pu1_v_dst = ps_codec->s_disp_buffer[i].pu1_bufs[2];
316
0
                        pu1_v_dst += (ps_codec->i4_strd * PAD_TOP) / 4 + (PAD_LEFT / 2);
317
0
                        break;
318
0
                    }
319
0
                }
320
0
                ps_dec_op->s_disp_frm_buf.pv_u_buf = pu1_u_dst;
321
0
                ps_dec_op->s_disp_frm_buf.pv_v_buf = pu1_v_dst;
322
0
            }
323
0
            ps_dec_op->s_disp_frm_buf.u4_y_strd = ps_codec->i4_strd;
324
0
        }
325
86.4k
        else
326
86.4k
        {
327
86.4k
            ps_dec_op->s_disp_frm_buf.pv_y_buf =
328
86.4k
                            ps_dec_ip->s_out_buffer.pu1_bufs[0];
329
86.4k
            ps_dec_op->s_disp_frm_buf.pv_u_buf =
330
86.4k
                            ps_dec_ip->s_out_buffer.pu1_bufs[1];
331
86.4k
            ps_dec_op->s_disp_frm_buf.pv_v_buf =
332
86.4k
                            ps_dec_ip->s_out_buffer.pu1_bufs[2];
333
86.4k
            ps_dec_op->s_disp_frm_buf.u4_y_strd = ps_codec->i4_disp_strd;
334
86.4k
        }
335
336
86.4k
        if((IV_YUV_420SP_VU == ps_codec->e_chroma_fmt)
337
77.1k
                        || (IV_YUV_420SP_UV == ps_codec->e_chroma_fmt))
338
21.1k
        {
339
21.1k
            ps_dec_op->s_disp_frm_buf.u4_u_strd = ALIGN2(ps_dec_op->s_disp_frm_buf.u4_y_strd);
340
21.1k
            ps_dec_op->s_disp_frm_buf.u4_v_strd = 0;
341
21.1k
            ps_dec_op->s_disp_frm_buf.u4_u_wd = ALIGN2(ps_dec_op->s_disp_frm_buf.u4_y_wd);
342
21.1k
            ps_dec_op->s_disp_frm_buf.u4_v_wd = 0;
343
21.1k
            ps_dec_op->s_disp_frm_buf.u4_u_ht = (ps_dec_op->s_disp_frm_buf.u4_y_ht + 1) / 2;
344
21.1k
            ps_dec_op->s_disp_frm_buf.u4_v_ht = 0;
345
21.1k
        }
346
65.3k
        else if(IV_YUV_420P == ps_codec->e_chroma_fmt)
347
40.3k
        {
348
40.3k
            ps_dec_op->s_disp_frm_buf.u4_u_strd = (ps_dec_op->s_disp_frm_buf.u4_y_strd + 1) / 2;
349
40.3k
            ps_dec_op->s_disp_frm_buf.u4_v_strd = (ps_dec_op->s_disp_frm_buf.u4_y_strd + 1) / 2;
350
40.3k
            ps_dec_op->s_disp_frm_buf.u4_u_wd = (ps_dec_op->s_disp_frm_buf.u4_y_wd + 1) / 2;
351
40.3k
            ps_dec_op->s_disp_frm_buf.u4_v_wd = (ps_dec_op->s_disp_frm_buf.u4_y_wd + 1) / 2;
352
40.3k
            ps_dec_op->s_disp_frm_buf.u4_u_ht = (ps_dec_op->s_disp_frm_buf.u4_y_ht + 1) / 2;
353
40.3k
            ps_dec_op->s_disp_frm_buf.u4_v_ht = (ps_dec_op->s_disp_frm_buf.u4_y_ht + 1) / 2;
354
40.3k
        }
355
24.9k
        else if(IV_YUV_444P == ps_codec->e_chroma_fmt)
356
8.71k
        {
357
8.71k
            ps_dec_op->s_disp_frm_buf.u4_u_strd = ps_dec_op->s_disp_frm_buf.u4_y_strd;
358
8.71k
            ps_dec_op->s_disp_frm_buf.u4_v_strd = ps_dec_op->s_disp_frm_buf.u4_y_strd;
359
8.71k
            ps_dec_op->s_disp_frm_buf.u4_u_wd = ps_dec_op->s_disp_frm_buf.u4_y_wd;
360
8.71k
            ps_dec_op->s_disp_frm_buf.u4_v_wd = ps_dec_op->s_disp_frm_buf.u4_y_wd;
361
8.71k
            ps_dec_op->s_disp_frm_buf.u4_u_ht = ps_dec_op->s_disp_frm_buf.u4_y_ht;
362
8.71k
            ps_dec_op->s_disp_frm_buf.u4_v_ht = ps_dec_op->s_disp_frm_buf.u4_y_ht;
363
8.71k
        }
364
16.2k
        else if(IV_GRAY == ps_codec->e_chroma_fmt)
365
13.7k
        {
366
13.7k
            ps_dec_op->s_disp_frm_buf.u4_u_strd = 0;
367
13.7k
            ps_dec_op->s_disp_frm_buf.u4_v_strd = 0;
368
13.7k
            ps_dec_op->s_disp_frm_buf.u4_u_wd = 0;
369
13.7k
            ps_dec_op->s_disp_frm_buf.u4_v_wd = 0;
370
13.7k
            ps_dec_op->s_disp_frm_buf.u4_u_ht = 0;
371
13.7k
            ps_dec_op->s_disp_frm_buf.u4_v_ht = 0;
372
13.7k
        }
373
2.44k
        else if(IV_YUV_422P == ps_codec->e_chroma_fmt)
374
2.44k
        {
375
2.44k
            ps_dec_op->s_disp_frm_buf.u4_u_strd = (ps_dec_op->s_disp_frm_buf.u4_y_strd + 1) / 2;
376
2.44k
            ps_dec_op->s_disp_frm_buf.u4_v_strd = (ps_dec_op->s_disp_frm_buf.u4_y_strd + 1) / 2;
377
2.44k
            ps_dec_op->s_disp_frm_buf.u4_u_wd = (ps_dec_op->s_disp_frm_buf.u4_y_wd + 1) / 2;
378
2.44k
            ps_dec_op->s_disp_frm_buf.u4_v_wd = (ps_dec_op->s_disp_frm_buf.u4_y_wd + 1) / 2;
379
2.44k
            ps_dec_op->s_disp_frm_buf.u4_u_ht = ps_dec_op->s_disp_frm_buf.u4_y_ht;
380
2.44k
            ps_dec_op->s_disp_frm_buf.u4_v_ht = ps_dec_op->s_disp_frm_buf.u4_y_ht;
381
2.44k
        }
382
383
86.4k
    }
384
307k
    else if(ps_codec->i4_flush_mode)
385
692
    {
386
692
        ps_dec_op->u4_error_code = IHEVCD_END_OF_SEQUENCE;
387
        /* Come out of flush mode and reset parser POC state so a post-flush
388
         * IDR/CRA frame is not ignored as a duplicate slice. */
389
692
        ps_codec->i4_flush_mode = 0;
390
692
        ps_codec->i4_prev_poc_msb = 0;
391
692
        ps_codec->i4_prev_poc_lsb = -1;
392
692
        ps_codec->i4_max_prev_poc_lsb = -1;
393
692
        ps_codec->s_parse.i4_abs_pic_order_cnt = -1;
394
692
        ps_codec->i4_slice_error = 0;
395
692
    }
396
397
393k
    if(ps_codec->u1_enable_cu_info && ps_dec_op->u4_output_present)
398
0
    {
399
0
        WORD32 info_map_dst_strd = ALIGN8(ps_codec->i4_wd) >> 3;
400
0
        WORD32 info_map_src_strd = ALIGN64(ps_codec->i4_wd) >> 3;
401
0
        WORD32 info_map_ht = ALIGN8(ps_codec->i4_ht);
402
0
        UWORD32 info_map_size = (ALIGN8(ps_codec->i4_wd) * info_map_ht) >> 6;
403
0
        WORD32 vert_8x8;
404
0
        UWORD8 *pu1_out_qp_map, *pu1_qp_map;
405
0
        UWORD8 *pu1_out_blk_type_map, *pu1_type_map;
406
407
0
        if(ps_hevcd_dec_ip->pu1_8x8_blk_qp_map)
408
0
        {
409
0
            ps_hevcd_dec_op->pu1_8x8_blk_qp_map = ps_hevcd_dec_ip->pu1_8x8_blk_qp_map;
410
0
            ps_hevcd_dec_op->u4_8x8_blk_qp_map_size = info_map_size;
411
412
0
            pu1_out_qp_map = ps_hevcd_dec_op->pu1_8x8_blk_qp_map;
413
0
            pu1_qp_map = ps_codec->as_buf_id_info_map[ps_codec->i4_disp_buf_id].pu1_qp_map;
414
0
            for(vert_8x8 = 0; vert_8x8 < info_map_ht; vert_8x8++)
415
0
            {
416
0
                memcpy(pu1_out_qp_map, pu1_qp_map, info_map_dst_strd);
417
0
                pu1_out_qp_map += info_map_dst_strd;
418
0
                pu1_qp_map += info_map_src_strd;
419
0
            }
420
0
        }
421
422
0
        if(ps_hevcd_dec_ip->pu1_8x8_blk_type_map)
423
0
        {
424
0
            ps_hevcd_dec_op->pu1_8x8_blk_type_map = ps_hevcd_dec_ip->pu1_8x8_blk_type_map;
425
0
            ps_hevcd_dec_op->u4_8x8_blk_type_map_size = info_map_size;
426
427
0
            pu1_out_blk_type_map = ps_hevcd_dec_op->pu1_8x8_blk_type_map;
428
0
            pu1_type_map =
429
0
                ps_codec->as_buf_id_info_map[ps_codec->i4_disp_buf_id].pu1_cu_type_map;
430
0
            for(vert_8x8 = 0; vert_8x8 < info_map_ht; vert_8x8++)
431
0
            {
432
0
                memcpy(pu1_out_blk_type_map, pu1_type_map, info_map_dst_strd);
433
0
                pu1_out_blk_type_map += info_map_dst_strd;
434
0
                pu1_type_map += info_map_src_strd;
435
0
            }
436
0
        }
437
0
    }
438
393k
}
439
440
/**
441
 *******************************************************************************
442
 *
443
 * @brief
444
 *  Codec process call
445
 *
446
 * @par Description:
447
 *  Codec process call  Tests for few error checks  Handle flush and decode
448
 * header related code  Parse bitstream for start codes  For each NAL unit
449
 * call decode NAL function  Once a complete frame is decoded (in frame
450
 * decode mode)  Fill output arguments and return
451
 *
452
 * @param[in] ps_codec_obj
453
 *  Pointer to codec object at API level
454
 *
455
 * @param[in] pv_api_ip
456
 *  Pointer to input argument structure
457
 *
458
 * @param[in] pv_api_op
459
 *  Pointer to output argument structure
460
 *
461
 * @returns  Status
462
 *
463
 * @remarks
464
 *
465
 *
466
 *******************************************************************************
467
 */
468
WORD32 ihevcd_decode(iv_obj_t *ps_codec_obj, void *pv_api_ip, void *pv_api_op)
469
394k
{
470
394k
    WORD32 ret = IV_SUCCESS;
471
394k
    codec_t *ps_codec = (codec_t *)(ps_codec_obj->pv_codec_handle);
472
394k
    ihevcd_cxa_video_decode_ip_t s_hevcd_dec_ip = {};
473
394k
    ihevcd_cxa_video_decode_ip_t *ps_hevcd_dec_ip;
474
394k
    ihevcd_cxa_video_decode_op_t *ps_hevcd_dec_op;
475
394k
    ivd_video_decode_ip_t *ps_dec_ip;
476
394k
    ivd_video_decode_op_t *ps_dec_op;
477
478
394k
    WORD32 proc_idx = 0;
479
394k
    WORD32 prev_proc_idx = 0;
480
394k
    WORD32 frm_skipped = 0;
481
482
    /* Initialize error code */
483
394k
    ps_codec->i4_error_code = 0;
484
    /* Initialize bytes remaining */
485
394k
    ps_codec->i4_bytes_remaining = 0;
486
487
394k
    ps_dec_ip = (ivd_video_decode_ip_t *)pv_api_ip;
488
394k
    memcpy(&s_hevcd_dec_ip, ps_dec_ip, ps_dec_ip->u4_size);
489
394k
    s_hevcd_dec_ip.s_ivd_video_decode_ip_t.u4_size = sizeof(ihevcd_cxa_video_decode_ip_t);
490
491
394k
    ps_hevcd_dec_ip = &s_hevcd_dec_ip;
492
394k
    ps_dec_ip = &ps_hevcd_dec_ip->s_ivd_video_decode_ip_t;
493
494
394k
    ps_hevcd_dec_op = (ihevcd_cxa_video_decode_op_t *)pv_api_op;
495
394k
    ps_dec_op = &ps_hevcd_dec_op->s_ivd_video_decode_op_t;
496
497
394k
    {
498
394k
        UWORD32 u4_size = ps_dec_op->u4_size;
499
394k
        memset(ps_hevcd_dec_op, 0, u4_size);
500
394k
        ps_dec_op->u4_size = u4_size; //Restore size field
501
394k
    }
502
394k
    if(ps_codec->i4_init_done != 1)
503
0
    {
504
0
        ps_dec_op->u4_error_code |= 1 << IVD_FATALERROR;
505
0
        ps_dec_op->u4_error_code |= IHEVCD_INIT_NOT_DONE;
506
0
        return IV_FAIL;
507
0
    }
508
509
394k
    if(ps_codec->u4_pic_cnt >= NUM_FRAMES_LIMIT)
510
0
    {
511
0
        ps_dec_op->u4_error_code |= 1 << IVD_FATALERROR;
512
0
        ps_dec_op->u4_error_code |= IHEVCD_NUM_FRAMES_LIMIT_REACHED;
513
0
        return IV_FAIL;
514
0
    }
515
516
394k
    if(ps_codec->u1_enable_cu_info && ps_codec->i4_sps_done)
517
0
    {
518
0
        UWORD32 blk_qp_map_size = ps_hevcd_dec_ip->u4_8x8_blk_qp_map_size;
519
0
        UWORD32 blk_type_map_size = ps_hevcd_dec_ip->u4_8x8_blk_type_map_size;
520
0
        UWORD32 blk_8x8_map_size = (ALIGN8(ps_codec->i4_wd) * ALIGN8(ps_codec->i4_ht)) >> 6;
521
522
0
        if ((ps_hevcd_dec_ip->pu1_8x8_blk_qp_map && blk_qp_map_size < blk_8x8_map_size) ||
523
0
            (ps_hevcd_dec_ip->pu1_8x8_blk_type_map && blk_type_map_size < blk_8x8_map_size))
524
0
        {
525
0
            ps_dec_op->u4_error_code |= 1 << IVD_UNSUPPORTEDPARAM;
526
0
            ps_dec_op->u4_error_code |= IHEVCD_INSUFFICIENT_METADATA_BUFFER;
527
0
            return IV_FAIL;
528
0
        }
529
0
     }
530
531
    /* If reset flag is set, flush the existing buffers */
532
394k
    if(ps_codec->i4_reset_flag)
533
692
    {
534
692
        ps_codec->i4_flush_mode = 1;
535
692
    }
536
537
    /*Data memory barries instruction,so that bitstream write by the application is complete*/
538
    //arm_dsb();
539
    /* In case the decoder is not in flush mode check for input buffer validity */
540
394k
    if(0 == ps_codec->i4_flush_mode)
541
394k
    {
542
394k
        if(ps_dec_ip->pv_stream_buffer == NULL)
543
0
        {
544
0
            ps_dec_op->u4_error_code |= 1 << IVD_UNSUPPORTEDPARAM;
545
0
            ps_dec_op->u4_error_code |= IVD_DEC_FRM_BS_BUF_NULL;
546
0
            return IV_FAIL;
547
0
        }
548
394k
        if(ps_dec_ip->u4_num_Bytes <= MIN_START_CODE_LEN)
549
1.01k
        {
550
1.01k
            if((WORD32)ps_dec_ip->u4_num_Bytes > 0)
551
1.01k
                ps_dec_op->u4_num_bytes_consumed = ps_dec_ip->u4_num_Bytes;
552
0
            else
553
0
                ps_dec_op->u4_num_bytes_consumed = 0;
554
555
1.01k
            ps_dec_op->u4_error_code |= 1 << IVD_UNSUPPORTEDPARAM;
556
1.01k
            ps_dec_op->u4_error_code |= IVD_DEC_NUMBYTES_INV;
557
1.01k
            return IV_FAIL;
558
559
1.01k
        }
560
394k
    }
561
562
#ifdef APPLY_CONCEALMENT
563
    {
564
        WORD32 num_mbs;
565
566
        num_mbs = (ps_codec->i4_wd * ps_codec->i4_ht + 255) >> 8;
567
        /* Reset MB Count at the beginning of every process call */
568
        ps_codec->mb_count = 0;
569
        memset(ps_codec->mb_map, 0, ((num_mbs + 7) >> 3));
570
    }
571
#endif
572
573
393k
    if(0 == ps_codec->i4_share_disp_buf && ps_codec->i4_header_mode == 0)
574
224k
    {
575
224k
        UWORD32 i;
576
224k
        if((ps_dec_ip->s_out_buffer.u4_num_bufs <= 0) ||
577
224k
           (ps_dec_ip->s_out_buffer.u4_num_bufs > IVD_VIDDEC_MAX_IO_BUFFERS))
578
0
        {
579
0
            ps_dec_op->u4_error_code |= 1 << IVD_UNSUPPORTEDPARAM;
580
0
            ps_dec_op->u4_error_code |= IVD_DISP_FRM_ZERO_OP_BUFS;
581
0
            return IV_FAIL;
582
0
        }
583
584
772k
        for(i = 0; i < ps_dec_ip->s_out_buffer.u4_num_bufs; i++)
585
547k
        {
586
547k
            if(ps_dec_ip->s_out_buffer.pu1_bufs[i] == NULL)
587
0
            {
588
0
                ps_dec_op->u4_error_code |= 1 << IVD_UNSUPPORTEDPARAM;
589
0
                ps_dec_op->u4_error_code |= IVD_DISP_FRM_OP_BUF_NULL;
590
0
                return IV_FAIL;
591
0
            }
592
593
547k
            if(ps_dec_ip->s_out_buffer.u4_min_out_buf_size[i] == 0)
594
0
            {
595
0
                ps_dec_op->u4_error_code |= 1 << IVD_UNSUPPORTEDPARAM;
596
0
                ps_dec_op->u4_error_code |= IVD_DISP_FRM_ZERO_OP_BUF_SIZE;
597
0
                return IV_FAIL;
598
0
            }
599
547k
        }
600
224k
    }
601
602
393k
    ps_codec->ps_out_buffer = &ps_dec_ip->s_out_buffer;
603
393k
    ps_codec->u4_ts = ps_dec_ip->u4_ts;
604
393k
    if(ps_codec->i4_flush_mode)
605
692
    {
606
607
692
        ps_dec_op->u4_pic_wd = ps_codec->i4_disp_wd;
608
692
        ps_dec_op->u4_pic_ht = ps_codec->i4_disp_ht;
609
610
692
        ps_dec_op->u4_new_seq = 0;
611
612
692
        ps_codec->ps_disp_buf = (pic_buf_t *)ihevc_disp_mgr_get(
613
692
                        (disp_mgr_t *)ps_codec->pv_disp_buf_mgr, &ps_codec->i4_disp_buf_id);
614
        /* In case of non-shared mode, then convert/copy the frame to output buffer */
615
        /* Only if the codec is in non-shared mode or in shared mode but needs 420P output */
616
692
        if((ps_codec->ps_disp_buf)
617
0
                        && ((0 == ps_codec->i4_share_disp_buf)
618
0
                                        || (IV_YUV_420P
619
0
                                                        == ps_codec->e_chroma_fmt)))
620
0
        {
621
622
0
            process_ctxt_t *ps_proc = &ps_codec->as_process[prev_proc_idx];
623
0
            if(0 == ps_proc->i4_init_done)
624
0
            {
625
0
                ihevcd_init_proc_ctxt(ps_proc, 0);
626
0
            }
627
628
            /* Output buffer check */
629
0
            ret = ihevcd_check_out_buf_size(ps_codec);
630
0
            RETURN_IF((ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS), ret);
631
632
            /* Set remaining number of rows to be processed */
633
0
            ret = ihevcd_fmt_conv(ps_codec, &ps_codec->as_process[prev_proc_idx],
634
0
                                  ps_dec_ip->s_out_buffer.pu1_bufs[0],
635
0
                                  ps_dec_ip->s_out_buffer.pu1_bufs[1],
636
0
                                  ps_dec_ip->s_out_buffer.pu1_bufs[2], 0,
637
0
                                  ps_codec->i4_disp_ht);
638
639
0
            ihevc_buf_mgr_release((buf_mgr_t *)ps_codec->pv_pic_buf_mgr,
640
0
                                  ps_codec->i4_disp_buf_id, BUF_MGR_DISP);
641
0
        }
642
643
692
        ihevcd_fill_outargs(ps_codec, ps_hevcd_dec_ip, ps_hevcd_dec_op);
644
645
692
        if(1 == ps_dec_op->u4_output_present)
646
0
        {
647
0
            WORD32 xpos = ps_codec->i4_disp_wd - 32 - LOGO_WD;
648
0
            WORD32 ypos = ps_codec->i4_disp_ht - 32 - LOGO_HT;
649
650
0
            if(ypos < 0)
651
0
                ypos = 0;
652
653
0
            if(xpos < 0)
654
0
                xpos = 0;
655
656
0
            INSERT_LOGO(ps_dec_ip->s_out_buffer.pu1_bufs[0],
657
0
                        ps_dec_ip->s_out_buffer.pu1_bufs[1],
658
0
                        ps_dec_ip->s_out_buffer.pu1_bufs[2], ps_codec->i4_disp_strd,
659
0
                        xpos,
660
0
                        ypos,
661
0
                        ps_codec->e_chroma_fmt,
662
0
                        ps_codec->i4_disp_wd,
663
0
                        ps_codec->i4_disp_ht);
664
0
        }
665
666
667
692
        if(NULL == ps_codec->ps_disp_buf)
668
692
        {
669
            /* If in flush mode and there are no more buffers to flush,
670
             * check for the reset flag and reset the decoder */
671
692
            if(ps_codec->i4_reset_flag)
672
692
            {
673
692
                ihevcd_init(ps_codec);
674
692
            }
675
692
            return (IV_FAIL);
676
692
        }
677
678
0
        return (IV_SUCCESS);
679
680
692
    }
681
    /* In case of shared mode, check if there is a free buffer for reconstruction */
682
393k
    if((0 == ps_codec->i4_header_mode) && (1 == ps_codec->i4_share_disp_buf))
683
0
    {
684
0
        WORD32 buf_status;
685
0
        buf_status = 1;
686
0
        if(ps_codec->pv_pic_buf_mgr)
687
0
            buf_status = ihevc_buf_mgr_check_free((buf_mgr_t *)ps_codec->pv_pic_buf_mgr);
688
689
        /* If there is no free buffer, then return with an error code */
690
0
        if(0 == buf_status)
691
0
        {
692
0
            ps_dec_op->u4_error_code = IVD_DEC_REF_BUF_NULL;
693
0
            ps_dec_op->u4_error_code |= (1 << IVD_UNSUPPORTEDPARAM);
694
0
            return IV_FAIL;
695
0
        }
696
0
    }
697
393k
    ps_codec->i4_bytes_remaining = ps_dec_ip->u4_num_Bytes;
698
393k
    ps_codec->pu1_inp_bitsbuf = (UWORD8 *)ps_dec_ip->pv_stream_buffer;
699
393k
    ps_codec->s_parse.i4_end_of_frame = 0;
700
701
393k
    ps_codec->i4_pic_present = 0;
702
393k
    ps_codec->i4_slice_error = 0;
703
393k
    ps_codec->ps_disp_buf = NULL;
704
393k
    ps_codec->e_dec_pic_type = IV_NA_FRAME;
705
706
393k
    if(ps_codec->i4_num_cores > 1)
707
268k
    {
708
268k
        ithread_set_affinity(0);
709
268k
    }
710
704k
    while(MIN_START_CODE_LEN < ps_codec->i4_bytes_remaining)
711
699k
    {
712
699k
        WORD32 nal_len;
713
699k
        WORD32 nal_ofst;
714
699k
        WORD32 bits_len;
715
716
699k
        if(ps_codec->i4_slice_error)
717
38.4k
        {
718
            /* Use masked index to support wrap-around in the 256-entry ring buffer */
719
38.4k
            slice_header_t *ps_slice_hdr_next = ps_codec->s_parse.ps_slice_hdr_base +
720
38.4k
                            ((ps_codec->s_parse.i4_cur_slice_idx + 1) & (MAX_SLICE_HDR_CNT - 1));
721
            /* Ensure next slice header belongs to current picture (prevents using stale data) */
722
38.4k
            if(ps_slice_hdr_next->i4_abs_pic_order_cnt == ps_codec->s_parse.i4_abs_pic_order_cnt)
723
10.2k
            {
724
10.2k
                slice_header_t *ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr_base +
725
10.2k
                                (ps_codec->s_parse.i4_cur_slice_idx & (MAX_SLICE_HDR_CNT - 1));
726
10.2k
                WORD32 next_slice_addr = ps_slice_hdr_next->i2_ctb_x +
727
10.2k
                            ps_slice_hdr_next->i2_ctb_y * ps_codec->s_parse.ps_sps->i2_pic_wd_in_ctb;
728
10.2k
                WORD32 cur_slice_addr = ps_slice_hdr->i2_ctb_x +
729
10.2k
                            ps_slice_hdr->i2_ctb_y * ps_codec->s_parse.ps_sps->i2_pic_wd_in_ctb;
730
                /* Ensure next slice start address is ahead of current slice (prevents matching
731
                 * old slice 0 on wrap-around) */
732
10.2k
                if(next_slice_addr > cur_slice_addr)
733
9.68k
                {
734
9.68k
                    if(ps_codec->s_parse.i4_next_ctb_indx == next_slice_addr)
735
0
                        ps_codec->i4_slice_error = 0;
736
9.68k
                }
737
10.2k
            }
738
38.4k
        }
739
740
699k
        if(ps_codec->pu1_bitsbuf_dynamic)
741
90.2k
        {
742
90.2k
            ps_codec->pu1_bitsbuf = ps_codec->pu1_bitsbuf_dynamic;
743
90.2k
            ps_codec->u4_bitsbuf_size = ps_codec->u4_bitsbuf_size_dynamic;
744
90.2k
        }
745
609k
        else
746
609k
        {
747
609k
            ps_codec->pu1_bitsbuf = ps_codec->pu1_bitsbuf_static;
748
609k
            ps_codec->u4_bitsbuf_size = ps_codec->u4_bitsbuf_size_static;
749
609k
        }
750
751
699k
        nal_ofst = ihevcd_nal_search_start_code(ps_codec->pu1_inp_bitsbuf,
752
699k
                                                ps_codec->i4_bytes_remaining);
753
        /* If there is no start code found, consume the data and break */
754
699k
        if(nal_ofst == ps_codec->i4_bytes_remaining)
755
404
        {
756
404
            ps_codec->pu1_inp_bitsbuf += nal_ofst;
757
404
            ps_codec->i4_bytes_remaining -= nal_ofst;
758
404
            break;
759
404
        }
760
761
699k
        ps_codec->i4_nal_ofst = nal_ofst;
762
699k
        {
763
699k
            WORD32 bytes_remaining = ps_codec->i4_bytes_remaining - nal_ofst;
764
765
699k
            bytes_remaining = MIN((UWORD32)bytes_remaining, ps_codec->u4_bitsbuf_size);
766
699k
            ihevcd_nal_remv_emuln_bytes(ps_codec->pu1_inp_bitsbuf + nal_ofst,
767
699k
                                        ps_codec->pu1_bitsbuf,
768
699k
                                        bytes_remaining,
769
699k
                                        &nal_len, &bits_len);
770
771
            /* Decoder may read upto 8 extra bytes at the end of frame */
772
            /* These are not used, but still set them to zero to avoid uninitialized reads */
773
699k
            if(bits_len < (WORD32)(ps_codec->u4_bitsbuf_size - 8))
774
699k
            {
775
699k
                memset(ps_codec->pu1_bitsbuf + bits_len, 0, 2 * sizeof(UWORD32));
776
699k
            }
777
699k
        }
778
        /* This may be used to update the offsets for tiles and entropy sync row offsets */
779
699k
        ps_codec->i4_num_emln_bytes = nal_len - bits_len;
780
699k
        ps_codec->i4_nal_len = nal_len;
781
782
699k
        ihevcd_bits_init(&ps_codec->s_parse.s_bitstrm, ps_codec->pu1_bitsbuf,
783
699k
                         bits_len);
784
785
699k
        ret = ihevcd_nal_unit(ps_codec);
786
787
        /* If the frame is incomplete and
788
         * the bytes remaining is zero or a header is received,
789
         * complete the frame treating it to be in error */
790
699k
        if(ps_codec->i4_pic_present &&
791
225k
                        (ps_codec->s_parse.i4_next_ctb_indx != ps_codec->s_parse.ps_sps->i4_pic_size_in_ctb))
792
163k
        {
793
163k
            if((ps_codec->i4_bytes_remaining - (nal_len + nal_ofst) <= MIN_START_CODE_LEN) ||
794
148k
                            (ps_codec->i4_header_in_slice_mode))
795
37.9k
            {
796
37.9k
                slice_header_t *ps_slice_hdr_next;
797
798
37.9k
                ps_codec->s_parse.i4_cur_slice_idx--;
799
37.9k
                if(ps_codec->s_parse.i4_cur_slice_idx < 0)
800
26
                    ps_codec->s_parse.i4_cur_slice_idx = 0;
801
802
37.9k
                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));
803
37.9k
                ps_slice_hdr_next->i2_ctb_x = 0;
804
37.9k
                ps_slice_hdr_next->i2_ctb_y = ps_codec->s_parse.ps_sps->i2_pic_ht_in_ctb;
805
37.9k
                ps_codec->i4_slice_error = 1;
806
37.9k
                continue;
807
37.9k
            }
808
163k
        }
809
810
661k
        if((IHEVCD_IGNORE_SLICE == ret) || ((WORD32)IVD_DEC_FRM_SKIPPED == ret))
811
38.1k
        {
812
38.1k
            if((WORD32)IVD_DEC_FRM_SKIPPED == ret)
813
0
                frm_skipped = 1;
814
38.1k
            ps_codec->pu1_inp_bitsbuf += (nal_ofst + nal_len);
815
38.1k
            ps_codec->i4_bytes_remaining -= (nal_ofst + nal_len);
816
817
38.1k
            ret = IHEVCD_SUCCESS;
818
38.1k
            continue;
819
38.1k
        }
820
821
623k
        if(IVD_RES_CHANGED == ret)
822
3.40k
        {
823
3.40k
            break;
824
3.40k
        }
825
826
        /* Update bytes remaining and bytes consumed and input bitstream pointer */
827
        /* Do not consume the NAL in the following cases */
828
        /* Slice header reached during header decode mode */
829
        /* TODO: Next picture's slice reached */
830
619k
        if(ret != IHEVCD_SLICE_IN_HEADER_MODE)
831
489k
        {
832
489k
            if((0 == ps_codec->i4_slice_error) ||
833
225
                            (ps_codec->i4_bytes_remaining - (nal_len + nal_ofst) <= MIN_START_CODE_LEN))
834
489k
            {
835
489k
                ps_codec->pu1_inp_bitsbuf += (nal_ofst + nal_len);
836
489k
                ps_codec->i4_bytes_remaining -= (nal_ofst + nal_len);
837
489k
            }
838
489k
            if(ret != IHEVCD_SUCCESS)
839
191k
                break;
840
841
297k
            if(ps_codec->s_parse.i4_end_of_frame)
842
62.0k
                break;
843
297k
        }
844
130k
        else
845
130k
        {
846
130k
            ret = IHEVCD_SUCCESS;
847
130k
            break;
848
130k
        }
849
850
        /* Allocate dynamic bitstream buffer once SPS is decoded */
851
235k
        if((ps_codec->u4_allocate_dynamic_done == 0) && ps_codec->i4_sps_done)
852
26.2k
        {
853
26.2k
            WORD32 ret;
854
26.2k
            ret = ihevcd_allocate_dynamic_bufs(ps_codec);
855
26.2k
            if(ret != IV_SUCCESS)
856
0
            {
857
                /* Free any dynamic buffers that are allocated */
858
0
                ihevcd_free_dynamic_bufs(ps_codec);
859
0
                ps_codec->i4_error_code = IVD_MEM_ALLOC_FAILED;
860
0
                ps_dec_op->u4_error_code = 1 << IVD_FATALERROR;
861
0
                ps_dec_op->u4_error_code |= IVD_MEM_ALLOC_FAILED;
862
863
0
                return IV_FAIL;
864
0
            }
865
26.2k
        }
866
867
235k
        BREAK_AFTER_SLICE_NAL();
868
235k
    }
869
870
393k
    if(1 == ps_codec->i4_pic_present && 0 == ps_codec->s_parse.i4_end_of_frame)
871
27.3k
    {
872
27.3k
        slice_header_t *ps_slice_hdr_next;
873
27.3k
        ps_codec->i4_slice_error = 1;
874
27.3k
        ps_codec->s_parse.i4_cur_slice_idx--;
875
27.3k
        if(ps_codec->s_parse.i4_cur_slice_idx < 0)
876
1
            ps_codec->s_parse.i4_cur_slice_idx = 0;
877
878
27.3k
        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));
879
27.3k
        ps_slice_hdr_next->i2_ctb_x = -1;
880
27.3k
        ps_slice_hdr_next->i2_ctb_y = -1;
881
882
27.3k
        ihevcd_parse_slice_data(ps_codec);
883
27.3k
        ASSERT(ps_codec->s_parse.i4_end_of_frame != 0);
884
27.3k
    }
885
886
393k
    if(1 == ps_codec->i4_pic_present)
887
89.4k
    {
888
89.4k
        WORD32 i;
889
89.4k
        sps_t *ps_sps = ps_codec->s_parse.ps_sps;
890
89.4k
        ps_codec->i4_first_pic_done = 1;
891
892
        /*TODO temporary fix: end_of_frame is checked before adding format conversion to job queue         */
893
89.4k
        if(ps_codec->i4_num_cores > 1 && ps_codec->s_parse.i4_end_of_frame)
894
59.4k
        {
895
896
            /* Add job queue for format conversion / frame copy for each ctb row */
897
            /* Only if the codec is in non-shared mode or in shared mode but needs 420P output */
898
59.4k
            process_ctxt_t *ps_proc;
899
900
            /* i4_num_cores - 1 contexts are currently being used by other threads */
901
59.4k
            ps_proc = &ps_codec->as_process[ps_codec->i4_num_cores - 1];
902
903
59.4k
            if((ps_codec->ps_disp_buf) &&
904
57.6k
               ((0 == ps_codec->i4_share_disp_buf) || (IV_YUV_420P == ps_codec->e_chroma_fmt)))
905
57.6k
            {
906
                /* If format conversion jobs were not issued in pic_init() add them here */
907
57.6k
                if((0 == ps_codec->u4_enable_fmt_conv_ahead) ||
908
0
                                (ps_codec->i4_disp_buf_id == ps_proc->i4_cur_pic_buf_id))
909
657k
                    for(i = 0; i < ps_sps->i2_pic_ht_in_ctb; i++)
910
599k
                    {
911
599k
                        proc_job_t s_job;
912
599k
                        IHEVCD_ERROR_T ret;
913
599k
                        s_job.i4_cmd = CMD_FMTCONV;
914
599k
                        s_job.i2_ctb_cnt = 0;
915
599k
                        s_job.i2_ctb_x = 0;
916
599k
                        s_job.i2_ctb_y = i;
917
599k
                        s_job.i2_slice_idx = 0;
918
599k
                        s_job.i4_tu_coeff_data_ofst = 0;
919
599k
                        ret = ihevcd_jobq_queue((jobq_t *)ps_codec->s_parse.pv_proc_jobq,
920
599k
                                                &s_job, sizeof(proc_job_t), 1);
921
599k
                        if(ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS)
922
0
                            return (WORD32)ret;
923
599k
                    }
924
57.6k
            }
925
            /* Reached end of frame : Signal terminate */
926
            /* The terminate flag is checked only after all the jobs are dequeued */
927
59.4k
            ret = ihevcd_jobq_terminate((jobq_t *)ps_codec->s_parse.pv_proc_jobq);
928
929
878k
            while(1)
930
878k
            {
931
878k
                IHEVCD_ERROR_T ret;
932
878k
                proc_job_t s_job;
933
878k
                process_ctxt_t *ps_proc;
934
935
                /* i4_num_cores - 1 contexts are currently being used by other threads */
936
878k
                ps_proc = &ps_codec->as_process[ps_codec->i4_num_cores - 1];
937
938
878k
                ret = ihevcd_jobq_dequeue((jobq_t *)ps_proc->pv_proc_jobq, &s_job,
939
878k
                                          sizeof(proc_job_t), 1);
940
878k
                if((IHEVCD_ERROR_T)IHEVCD_SUCCESS != ret)
941
59.4k
                    break;
942
943
819k
                ps_proc->i4_ctb_cnt = s_job.i2_ctb_cnt;
944
819k
                ps_proc->i4_ctb_x = s_job.i2_ctb_x;
945
819k
                ps_proc->i4_ctb_y = s_job.i2_ctb_y;
946
819k
                ps_proc->i4_cur_slice_idx = s_job.i2_slice_idx;
947
948
819k
                if(CMD_PROCESS == s_job.i4_cmd)
949
594k
                {
950
594k
                    ihevcd_init_proc_ctxt(ps_proc, s_job.i4_tu_coeff_data_ofst);
951
952
594k
                    ihevcd_process(ps_proc);
953
594k
                }
954
224k
                else if(CMD_FMTCONV == s_job.i4_cmd)
955
224k
                {
956
224k
                    sps_t *ps_sps = ps_codec->s_parse.ps_sps;
957
224k
                    WORD32 num_rows = 1 << ps_sps->i1_log2_ctb_size;
958
224k
                    if(0 == ps_proc->i4_init_done)
959
6.48k
                    {
960
6.48k
                        ihevcd_init_proc_ctxt(ps_proc, 0);
961
6.48k
                    }
962
963
224k
                    num_rows = MIN(num_rows, (ps_codec->i4_disp_ht - (s_job.i2_ctb_y << ps_sps->i1_log2_ctb_size)));
964
224k
                    if(num_rows < 0)
965
958
                        num_rows = 0;
966
967
224k
                    ihevcd_fmt_conv(ps_codec, ps_proc,
968
224k
                                    ps_dec_ip->s_out_buffer.pu1_bufs[0],
969
224k
                                    ps_dec_ip->s_out_buffer.pu1_bufs[1],
970
224k
                                    ps_dec_ip->s_out_buffer.pu1_bufs[2],
971
224k
                                    s_job.i2_ctb_y << ps_sps->i1_log2_ctb_size,
972
224k
                                    num_rows);
973
224k
                }
974
819k
            }
975
59.4k
        }
976
        /* In case of non-shared mode and while running in single core mode, then convert/copy the frame to output buffer */
977
        /* Only if the codec is in non-shared mode or in shared mode but needs 420P output */
978
29.9k
        else if((ps_codec->ps_disp_buf) && ((0 == ps_codec->i4_share_disp_buf) ||
979
0
                                            (IV_YUV_420P == ps_codec->e_chroma_fmt)) &&
980
28.7k
                        (ps_codec->s_parse.i4_end_of_frame))
981
28.7k
        {
982
28.7k
            process_ctxt_t *ps_proc = &ps_codec->as_process[proc_idx];
983
            /* Set remaining number of rows to be processed */
984
28.7k
            ps_codec->s_fmt_conv.i4_num_rows = ps_codec->i4_disp_ht
985
28.7k
                            - ps_codec->s_fmt_conv.i4_cur_row;
986
28.7k
            if(0 == ps_proc->i4_init_done)
987
0
            {
988
0
                ihevcd_init_proc_ctxt(ps_proc, 0);
989
0
            }
990
991
28.7k
            if(ps_codec->s_fmt_conv.i4_num_rows < 0)
992
0
                ps_codec->s_fmt_conv.i4_num_rows = 0;
993
994
28.7k
            ret = ihevcd_fmt_conv(ps_codec, ps_proc,
995
28.7k
                                  ps_dec_ip->s_out_buffer.pu1_bufs[0],
996
28.7k
                                  ps_dec_ip->s_out_buffer.pu1_bufs[1],
997
28.7k
                                  ps_dec_ip->s_out_buffer.pu1_bufs[2],
998
28.7k
                                  ps_codec->s_fmt_conv.i4_cur_row,
999
28.7k
                                  ps_codec->s_fmt_conv.i4_num_rows);
1000
28.7k
            ps_codec->s_fmt_conv.i4_cur_row += ps_codec->s_fmt_conv.i4_num_rows;
1001
1002
28.7k
        }
1003
1004
1005
89.4k
        DEBUG_DUMP_MV_MAP(ps_codec);
1006
1007
89.4k
        if(ps_codec->u4_disable_ref_retention)
1008
0
        {
1009
            /* Do not retain current picture or MV bank as reference */
1010
0
            ihevc_buf_mgr_release((buf_mgr_t *)ps_codec->pv_mv_buf_mgr,
1011
0
                                  ps_codec->as_process[proc_idx].i4_cur_mv_bank_buf_id,
1012
0
                                  BUF_MGR_REF);
1013
1014
            /* Mark pic buf as needed for display */
1015
0
            ihevc_buf_mgr_set_status((buf_mgr_t *)ps_codec->pv_pic_buf_mgr,
1016
0
                                     ps_codec->as_process[proc_idx].i4_cur_pic_buf_id,
1017
0
                                     BUF_MGR_DISP);
1018
0
        }
1019
89.4k
        else
1020
89.4k
        {
1021
            /* Mark MV Buf as needed for reference */
1022
89.4k
            ihevc_buf_mgr_set_status((buf_mgr_t *)ps_codec->pv_mv_buf_mgr,
1023
89.4k
                                     ps_codec->as_process[proc_idx].i4_cur_mv_bank_buf_id,
1024
89.4k
                                     BUF_MGR_REF);
1025
1026
            /* Mark pic buf as needed for reference */
1027
89.4k
            ihevc_buf_mgr_set_status((buf_mgr_t *)ps_codec->pv_pic_buf_mgr,
1028
89.4k
                                     ps_codec->as_process[proc_idx].i4_cur_pic_buf_id,
1029
89.4k
                                     BUF_MGR_REF);
1030
1031
            /* Mark pic buf as needed for display */
1032
89.4k
            ihevc_buf_mgr_set_status((buf_mgr_t *)ps_codec->pv_pic_buf_mgr,
1033
89.4k
                                     ps_codec->as_process[proc_idx].i4_cur_pic_buf_id,
1034
89.4k
                                     BUF_MGR_DISP);
1035
1036
            /* Insert the current picture as short term reference */
1037
89.4k
            ihevc_dpb_mgr_insert_ref((dpb_mgr_t *)ps_codec->pv_dpb_mgr,
1038
89.4k
                                     ps_codec->as_process[proc_idx].ps_cur_pic,
1039
89.4k
                                     ps_codec->as_process[proc_idx].i4_cur_pic_buf_id);
1040
89.4k
        }
1041
1042
        /* If a frame was displayed (in non-shared mode), then release it from display manager */
1043
89.4k
        if((0 == ps_codec->i4_share_disp_buf) && (ps_codec->ps_disp_buf))
1044
86.4k
            ihevc_buf_mgr_release((buf_mgr_t *)ps_codec->pv_pic_buf_mgr,
1045
86.4k
                                  ps_codec->i4_disp_buf_id, BUF_MGR_DISP);
1046
2.99k
        else if((0 == ps_codec->i4_share_disp_buf) && ps_codec->u4_disable_ref_retention)
1047
0
        {
1048
            /* In disable_ref_retention mode, ihevc_disp_mgr_get() normally pops the
1049
             * current picture immediately (IVD_DECODE_FRAME_OUT). If it was not popped
1050
             * (e.g. abs_poc == DEFAULT_POC), clear the display manager slot as well as
1051
             * BUF_MGR_DISP so future ihevc_disp_mgr_add() calls do not stall. */
1052
0
            WORD32 cur_buf_id = ps_codec->as_process[proc_idx].i4_cur_pic_buf_id;
1053
0
            disp_mgr_t *ps_disp_mgr = (disp_mgr_t *)ps_codec->pv_disp_buf_mgr;
1054
0
            if(cur_buf_id >= 0 && cur_buf_id < DISP_MGR_MAX_CNT)
1055
0
            {
1056
0
                ps_disp_mgr->apv_ptr[cur_buf_id] = NULL;
1057
0
                ps_disp_mgr->ai4_abs_poc[cur_buf_id] = DEFAULT_POC;
1058
0
            }
1059
0
            ihevc_buf_mgr_release((buf_mgr_t *)ps_codec->pv_pic_buf_mgr,
1060
0
                                  cur_buf_id, BUF_MGR_DISP);
1061
0
        }
1062
1063
        /* Wait for threads */
1064
203k
        for(i = 0; i < (ps_codec->i4_num_cores - 1); i++)
1065
114k
        {
1066
114k
            if(ps_codec->ai4_process_thread_created[i])
1067
114k
            {
1068
114k
                if(ps_codec->i4_threads_active)
1069
114k
                {
1070
114k
                    ret = ithread_mutex_lock(ps_codec->apv_proc_done_mutex[i]);
1071
114k
                    RETURN_IF((ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS), ret);
1072
1073
154k
                    while(!ps_codec->ai4_process_done[i])
1074
39.6k
                    {
1075
39.6k
                        ithread_cond_wait(ps_codec->apv_proc_done_condition[i],
1076
39.6k
                                          ps_codec->apv_proc_done_mutex[i]);
1077
39.6k
                    }
1078
114k
                    ps_codec->ai4_process_done[i] = 0;
1079
114k
                    ret = ithread_mutex_unlock(ps_codec->apv_proc_done_mutex[i]);
1080
114k
                    RETURN_IF((ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS), ret);
1081
114k
                }
1082
0
                else
1083
0
                {
1084
0
                    ithread_join(ps_codec->apv_process_thread_handle[i], NULL);
1085
0
                    ps_codec->ai4_process_thread_created[i] = 0;
1086
0
                }
1087
114k
            }
1088
114k
        }
1089
1090
89.4k
        DEBUG_VALIDATE_PADDED_REGION(&ps_codec->as_process[proc_idx]);
1091
89.4k
        if(ps_codec->u4_pic_cnt > 0)
1092
67.6k
        {
1093
67.6k
            DEBUG_DUMP_PIC_PU(ps_codec);
1094
67.6k
        }
1095
89.4k
        DEBUG_DUMP_PIC_BUFFERS(ps_codec);
1096
1097
        /* Increment the number of pictures decoded */
1098
89.4k
        ps_codec->u4_pic_cnt++;
1099
89.4k
        if(ps_codec->u4_disable_ref_retention)
1100
0
        {
1101
            /* Reset parser POC state after each decoded picture so consecutive
1102
             * POC=0 intra frames/tiles are not ignored as duplicate slices; use
1103
             * DEFAULT_POC rather than -1 to avoid colliding with valid POC == -1
1104
             * open-GOP leading pictures. */
1105
0
            ps_codec->s_parse.i4_abs_pic_order_cnt = DEFAULT_POC;
1106
0
        }
1107
89.4k
    }
1108
1109
    /* Report IVD_DEC_FRM_SKIPPED only when disable_ref_retention skipped
1110
     * slice(s) without producing a picture or encountering another error,
1111
     * so preceding skipped slices do not fail a decoded I-frame or mask
1112
     * header/slice errors (e.g., IVD_RES_CHANGED). Mapped without
1113
     * IVD_FATALERROR in ihevcd_map_error(). */
1114
393k
    if((0 == ps_codec->i4_pic_present) && (0 == ps_codec->i4_error_code) &&
1115
199k
       (IHEVCD_SUCCESS == ret) && frm_skipped)
1116
0
    {
1117
0
        ps_codec->i4_error_code = IVD_DEC_FRM_SKIPPED;
1118
0
        ret = IV_FAIL;
1119
0
    }
1120
393k
    ihevcd_fill_outargs(ps_codec, ps_hevcd_dec_ip, ps_hevcd_dec_op);
1121
1122
393k
    if(1 == ps_dec_op->u4_output_present)
1123
86.4k
    {
1124
86.4k
        WORD32 xpos = ps_codec->i4_disp_wd - 32 - LOGO_WD;
1125
86.4k
        WORD32 ypos = ps_codec->i4_disp_ht - 32 - LOGO_HT;
1126
1127
86.4k
        if(ypos < 0)
1128
15.8k
            ypos = 0;
1129
1130
86.4k
        if(xpos < 0)
1131
40.6k
            xpos = 0;
1132
1133
86.4k
        INSERT_LOGO(ps_dec_ip->s_out_buffer.pu1_bufs[0],
1134
86.4k
                    ps_dec_ip->s_out_buffer.pu1_bufs[1],
1135
86.4k
                    ps_dec_ip->s_out_buffer.pu1_bufs[2], ps_codec->i4_disp_strd,
1136
86.4k
                    xpos,
1137
86.4k
                    ypos,
1138
86.4k
                    ps_codec->e_chroma_fmt,
1139
86.4k
                    ps_codec->i4_disp_wd,
1140
86.4k
                    ps_codec->i4_disp_ht);
1141
86.4k
    }
1142
1143
1144
393k
    return ret;
1145
393k
}
1146