Coverage Report

Created: 2025-08-29 06:21

/src/libmpeg2/decoder/impeg2d_pic_proc.c
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2015 The Android Open Source Project
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
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
*/
20
#include <stdio.h>
21
#include <string.h>
22
23
#include "iv_datatypedef.h"
24
#include "iv.h"
25
26
#include "impeg2_buf_mgr.h"
27
#include "impeg2_disp_mgr.h"
28
#include "impeg2_defs.h"
29
#include "impeg2_platform_macros.h"
30
#include "impeg2_inter_pred.h"
31
#include "impeg2_idct.h"
32
#include "impeg2_globals.h"
33
#include "impeg2_mem_func.h"
34
#include "impeg2_format_conv.h"
35
#include "impeg2_macros.h"
36
37
#include "ivd.h"
38
#include "impeg2d.h"
39
#include "impeg2d_bitstream.h"
40
#include "impeg2d_structs.h"
41
#include "impeg2d_globals.h"
42
#include "impeg2d_vld_tables.h"
43
#include "impeg2d_vld.h"
44
#include "impeg2d_pic_proc.h"
45
#include "impeg2d_debug.h"
46
47
void impeg2d_init_function_ptr(void *pv_codec);
48
void impeg2d_format_convert(dec_state_t *ps_dec,
49
                            pic_buf_t *ps_src_pic,
50
                            iv_yuv_buf_t    *ps_disp_frm_buf,
51
                            UWORD32 u4_start_row, UWORD32 u4_num_rows)
52
235k
{
53
235k
    UWORD8  *pu1_src_y,*pu1_src_u,*pu1_src_v;
54
235k
    UWORD8  *pu1_dst_y,*pu1_dst_u,*pu1_dst_v;
55
56
57
58
235k
    if((NULL == ps_src_pic) || (NULL == ps_src_pic->pu1_y) || (0 == u4_num_rows))
59
0
            return;
60
61
235k
    pu1_src_y   = ps_src_pic->pu1_y + (u4_start_row * ps_dec->u2_frame_width);
62
235k
    pu1_src_u   = ps_src_pic->pu1_u + ((u4_start_row >> 1) * (ps_dec->u2_frame_width >> 1));
63
235k
    pu1_src_v   = ps_src_pic->pu1_v + ((u4_start_row >> 1) *(ps_dec->u2_frame_width >> 1));
64
65
235k
    pu1_dst_y  =  (UWORD8 *)ps_disp_frm_buf->pv_y_buf + (u4_start_row *  ps_dec->u4_frm_buf_stride);
66
235k
    pu1_dst_u =   (UWORD8 *)ps_disp_frm_buf->pv_u_buf +((u4_start_row >> 1)*(ps_dec->u4_frm_buf_stride >> 1));
67
235k
    pu1_dst_v =   (UWORD8 *)ps_disp_frm_buf->pv_v_buf +((u4_start_row >> 1)*(ps_dec->u4_frm_buf_stride >> 1));
68
69
235k
    if (IV_YUV_420P == ps_dec->i4_chromaFormat)
70
110k
    {
71
110k
        ps_dec->pf_copy_yuv420p_buf(pu1_src_y, pu1_src_u, pu1_src_v, pu1_dst_y,
72
110k
                                    pu1_dst_u, pu1_dst_v,
73
110k
                                    ps_dec->u2_horizontal_size,
74
110k
                                    u4_num_rows,
75
110k
                                    ps_dec->u2_frame_width,
76
110k
                                    (ps_dec->u2_frame_width >> 1),
77
110k
                                    (ps_dec->u2_frame_width >> 1),
78
110k
                                    ps_dec->u4_frm_buf_stride,
79
110k
                                    (ps_dec->u4_frm_buf_stride >> 1),
80
110k
                                    (ps_dec->u4_frm_buf_stride >> 1));
81
110k
    }
82
125k
    else if (IV_YUV_422ILE == ps_dec->i4_chromaFormat)
83
0
    {
84
0
        void    *pv_yuv422i;
85
0
        UWORD32 u2_height,u2_width,u2_stride_y,u2_stride_u,u2_stride_v;
86
0
        UWORD32 u2_stride_yuv422i;
87
88
89
0
        pv_yuv422i          = (UWORD8 *)ps_disp_frm_buf->pv_y_buf + ((ps_dec->u2_vertical_size)*(ps_dec->u4_frm_buf_stride));
90
0
        u2_height           = u4_num_rows;
91
0
        u2_width            = ps_dec->u2_horizontal_size;
92
0
        u2_stride_y         = ps_dec->u2_frame_width;
93
0
        u2_stride_u         = u2_stride_y >> 1;
94
0
        u2_stride_v         = u2_stride_u;
95
0
        u2_stride_yuv422i   = (0 == ps_dec->u4_frm_buf_stride) ? ps_dec->u2_horizontal_size : ps_dec->u4_frm_buf_stride;
96
97
0
        ps_dec->pf_fmt_conv_yuv420p_to_yuv422ile(pu1_src_y,
98
0
            pu1_src_u,
99
0
            pu1_src_v,
100
0
            pv_yuv422i,
101
0
            u2_width,
102
0
            u2_height,
103
0
            u2_stride_y,
104
0
            u2_stride_u,
105
0
            u2_stride_v,
106
0
            u2_stride_yuv422i);
107
108
0
    }
109
125k
    else if((ps_dec->i4_chromaFormat == IV_YUV_420SP_UV) ||
110
125k
            (ps_dec->i4_chromaFormat == IV_YUV_420SP_VU))
111
125k
    {
112
113
125k
        UWORD32 dest_inc_Y=0,dest_inc_UV=0;
114
125k
        WORD32 convert_uv_only;
115
116
125k
        pu1_dst_u =   (UWORD8 *)ps_disp_frm_buf->pv_u_buf +((u4_start_row >> 1)*(ps_dec->u4_frm_buf_stride));
117
125k
        dest_inc_Y =    ps_dec->u4_frm_buf_stride;
118
125k
        dest_inc_UV =   ((ps_dec->u4_frm_buf_stride + 1) >> 1) << 1;
119
125k
        convert_uv_only = 0;
120
121
125k
        if(1 == ps_dec->u4_share_disp_buf)
122
0
            convert_uv_only = 1;
123
124
125k
        if(ps_src_pic->pu1_y == ps_disp_frm_buf->pv_y_buf)
125
65.0k
            convert_uv_only = 1;
126
127
125k
        if(ps_dec->i4_chromaFormat == IV_YUV_420SP_UV)
128
75.2k
        {
129
75.2k
            ps_dec->pf_fmt_conv_yuv420p_to_yuv420sp_uv(pu1_src_y,
130
75.2k
                pu1_src_u,
131
75.2k
                pu1_src_v,
132
75.2k
                pu1_dst_y,
133
75.2k
                pu1_dst_u,
134
75.2k
                u4_num_rows,
135
75.2k
                ps_dec->u2_horizontal_size,
136
75.2k
                ps_dec->u2_frame_width,
137
75.2k
                ps_dec->u2_frame_width >> 1,
138
75.2k
                ps_dec->u2_frame_width >> 1,
139
75.2k
                dest_inc_Y,
140
75.2k
                dest_inc_UV,
141
75.2k
                convert_uv_only);
142
75.2k
        }
143
49.9k
        else
144
49.9k
        {
145
49.9k
            ps_dec->pf_fmt_conv_yuv420p_to_yuv420sp_vu(pu1_src_y,
146
49.9k
                    pu1_src_u,
147
49.9k
                    pu1_src_v,
148
49.9k
                    pu1_dst_y,
149
49.9k
                    pu1_dst_u,
150
49.9k
                    u4_num_rows,
151
49.9k
                    ps_dec->u2_horizontal_size,
152
49.9k
                    ps_dec->u2_frame_width,
153
49.9k
                    ps_dec->u2_frame_width >> 1,
154
49.9k
                    ps_dec->u2_frame_width >> 1,
155
49.9k
                    dest_inc_Y,
156
49.9k
                    dest_inc_UV,
157
49.9k
                    convert_uv_only);
158
49.9k
        }
159
160
161
162
125k
    }
163
164
235k
}
165
166
167
/*******************************************************************************
168
*
169
*  Function Name   : impeg2d_get_frm_buf
170
*
171
*  Description     : Gets YUV component buffers for the frame
172
*
173
*  Arguments       :
174
*  frm_buf         : YUV buffer
175
*  frm             : Reference frame
176
*  width           : Width of the frame
177
*  Height          : Height of the frame
178
*
179
*  Values Returned : None
180
*******************************************************************************/
181
void impeg2d_get_frm_buf(yuv_buf_t *ps_frm_buf,UWORD8 *pu1_frm,UWORD32 u4_width,UWORD32 u4_height)
182
0
{
183
0
   UWORD32 u4_luma_size = u4_width * u4_height;
184
0
   UWORD32 u4_chroma_size = (u4_width * u4_height)>>2;
185
186
0
   ps_frm_buf->pu1_y = pu1_frm;
187
0
   ps_frm_buf->pu1_u = pu1_frm + u4_luma_size;
188
0
   ps_frm_buf->pu1_v = pu1_frm + u4_luma_size + u4_chroma_size;
189
190
0
}
191
/*******************************************************************************
192
*
193
*  Function Name   : impeg2d_get_bottom_field_buf
194
*
195
*  Description     : Gets YUV component buffers for bottom field of the frame
196
*
197
*  Arguments       :
198
*  frm_buf         : YUV buffer
199
*  frm             : Reference frame
200
*  width           : Width of the frame
201
*  Height          : Height of the frame
202
*
203
*  Values Returned : None
204
*******************************************************************************/
205
void impeg2d_get_bottom_field_buf(yuv_buf_t *ps_src_buf,yuv_buf_t *ps_dst_buf,
206
                      UWORD32 u4_width)
207
20.1k
{
208
20.1k
   ps_dst_buf->pu1_y = ps_src_buf->pu1_y + u4_width;
209
20.1k
   ps_dst_buf->pu1_u = ps_src_buf->pu1_u + (u4_width>>1);
210
20.1k
   ps_dst_buf->pu1_v = ps_src_buf->pu1_v + (u4_width>>1);
211
212
20.1k
}
213
/*******************************************************************************
214
*  Function Name   : impeg2d_get_mb_addr_incr
215
*
216
*  Description     : Decodes the Macroblock address increment
217
*
218
*  Arguments       :
219
*  stream          : Bitstream
220
*
221
*  Values Returned : Macroblock address increment
222
*******************************************************************************/
223
UWORD16 impeg2d_get_mb_addr_incr(stream_t *ps_stream)
224
931k
{
225
931k
    UWORD16 u2_mb_addr_incr = 0;
226
1.00M
    while (impeg2d_bit_stream_nxt(ps_stream,MB_ESCAPE_CODE_LEN) == MB_ESCAPE_CODE &&
227
1.00M
            ps_stream->u4_offset < ps_stream->u4_max_offset)
228
70.5k
    {
229
70.5k
        impeg2d_bit_stream_flush(ps_stream,MB_ESCAPE_CODE_LEN);
230
70.5k
        u2_mb_addr_incr += 33;
231
70.5k
    }
232
931k
    u2_mb_addr_incr += impeg2d_dec_vld_symbol(ps_stream,gai2_impeg2d_mb_addr_incr,MB_ADDR_INCR_LEN) +
233
931k
        MB_ADDR_INCR_OFFSET;
234
931k
    return(u2_mb_addr_incr);
235
931k
}
236
237
/*******************************************************************************
238
*
239
*  Function Name   : impeg2d_init_video_state
240
*
241
*  Description     : Initializes the Video decoder state
242
*
243
*  Arguments       :
244
*  dec             : Decoder context
245
*  videoType       : MPEG_2_Video / MPEG_1_Video
246
*
247
*  Values Returned : None
248
*******************************************************************************/
249
IMPEG2D_ERROR_CODES_T impeg2d_init_video_state(dec_state_t *ps_dec, e_video_type_t e_video_type)
250
8.05k
{
251
    /*-----------------------------------------------------------------------*/
252
    /* Bit Stream  that conforms to MPEG-1 <ISO/IEC 11172-2> standard        */
253
    /*-----------------------------------------------------------------------*/
254
8.05k
    if(e_video_type == MPEG_1_VIDEO)
255
5.00k
    {
256
5.00k
        ps_dec->u2_is_mpeg2 = 0;
257
258
        /*-------------------------------------------------------------------*/
259
        /* force MPEG-1 parameters for proper decoder behavior               */
260
        /* see ISO/IEC 13818-2 section D.9.14                                */
261
        /*-------------------------------------------------------------------*/
262
5.00k
        ps_dec->u2_progressive_sequence         = 1;
263
5.00k
        ps_dec->u2_intra_dc_precision           = 0;
264
5.00k
        ps_dec->u2_picture_structure            = FRAME_PICTURE;
265
5.00k
        ps_dec->u2_frame_pred_frame_dct         = 1;
266
5.00k
        ps_dec->u2_concealment_motion_vectors   = 0;
267
5.00k
        ps_dec->u2_q_scale_type                 = 0;
268
5.00k
        ps_dec->u2_intra_vlc_format             = 0;
269
5.00k
        ps_dec->u2_alternate_scan               = 0;
270
5.00k
        ps_dec->u2_repeat_first_field           = 0;
271
5.00k
        ps_dec->u2_progressive_frame            = 1;
272
5.00k
        ps_dec->u2_frame_rate_extension_n       = 0;
273
5.00k
        ps_dec->u2_frame_rate_extension_d       = 0;
274
5.00k
        ps_dec->u2_forw_f_code                  = 7;
275
5.00k
        ps_dec->u2_back_f_code                  = 7;
276
277
5.00k
        ps_dec->pf_vld_inv_quant                  = impeg2d_vld_inv_quant_mpeg1;
278
        /*-------------------------------------------------------------------*/
279
        /* Setting of parameters other than those mentioned in MPEG2 standard*/
280
        /* but used in decoding process.                                     */
281
        /*-------------------------------------------------------------------*/
282
5.00k
    }
283
    /*-----------------------------------------------------------------------*/
284
    /* Bit Stream  that conforms to MPEG-2                                   */
285
    /*-----------------------------------------------------------------------*/
286
3.04k
    else
287
3.04k
    {
288
3.04k
        ps_dec->u2_is_mpeg2                  = 1;
289
3.04k
        ps_dec->u2_full_pel_forw_vector   = 0;
290
3.04k
        ps_dec->u2_forw_f_code            = 7;
291
3.04k
        ps_dec->u2_full_pel_back_vector   = 0;
292
3.04k
        ps_dec->u2_back_f_code            = 7;
293
3.04k
        ps_dec->pf_vld_inv_quant       = impeg2d_vld_inv_quant_mpeg2;
294
295
296
3.04k
    }
297
298
299
8.05k
    impeg2d_init_function_ptr(ps_dec);
300
301
    /* Set the frame Width and frame Height */
302
8.05k
    ps_dec->u2_frame_height        = ALIGN16(ps_dec->u2_vertical_size);
303
8.05k
    ps_dec->u2_frame_width         = ALIGN16(ps_dec->u2_horizontal_size);
304
8.05k
    ps_dec->u2_num_horiz_mb         = (ps_dec->u2_horizontal_size + 15) >> 4;
305
   // dec->u4_frm_buf_stride    = dec->frameWidth;
306
8.05k
    if (ps_dec->u2_frame_height > ps_dec->u2_create_max_height || ps_dec->u2_frame_width > ps_dec->u2_create_max_width)
307
0
    {
308
0
        return IMPEG2D_PIC_SIZE_NOT_SUPPORTED;
309
0
    }
310
311
8.05k
    ps_dec->u2_num_flds_decoded = 0;
312
313
    /* Calculate the frame period */
314
8.05k
    {
315
8.05k
        UWORD32 numer;
316
8.05k
        UWORD32 denom;
317
8.05k
        numer = (UWORD32)gau2_impeg2_frm_rate_code[ps_dec->u2_frame_rate_code][1] *
318
8.05k
                                (UWORD32)(ps_dec->u2_frame_rate_extension_d + 1);
319
320
8.05k
        denom = (UWORD32)gau2_impeg2_frm_rate_code[ps_dec->u2_frame_rate_code][0] *
321
8.05k
                                (UWORD32)(ps_dec->u2_frame_rate_extension_n + 1);
322
8.05k
        ps_dec->u2_framePeriod =  (numer * 1000 * 100) / denom;
323
8.05k
    }
324
325
326
8.05k
   if(VERTICAL_SCAN == ps_dec->u2_alternate_scan)
327
363
   {
328
363
    ps_dec->pu1_inv_scan_matrix = (UWORD8 *)gau1_impeg2_inv_scan_vertical;
329
363
   }
330
7.68k
   else
331
7.68k
   {
332
7.68k
    ps_dec->pu1_inv_scan_matrix = (UWORD8 *)gau1_impeg2_inv_scan_zig_zag;
333
7.68k
   }
334
8.05k
   return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
335
8.05k
}
336
/*******************************************************************************
337
*
338
*  Function Name   : impeg2d_pre_pic_dec_proc
339
*
340
*  Description     : Does the processing neccessary before picture decoding
341
*
342
*  Arguments       :
343
*  dec             : Decoder context
344
*
345
*  Values Returned : None
346
*******************************************************************************/
347
IMPEG2D_ERROR_CODES_T impeg2d_pre_pic_dec_proc(dec_state_t *ps_dec)
348
27.8k
{
349
27.8k
    WORD32 u4_get_disp;
350
27.8k
    pic_buf_t *ps_disp_pic;
351
27.8k
    IMPEG2D_ERROR_CODES_T e_error = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
352
353
27.8k
    u4_get_disp = 0;
354
27.8k
    ps_disp_pic = NULL;
355
356
    /* Field Picture */
357
27.8k
    if(ps_dec->u2_picture_structure != FRAME_PICTURE)
358
4.74k
    {
359
4.74k
        ps_dec->u2_num_vert_mb       = (ps_dec->u2_vertical_size + 31) >> 5;
360
361
4.74k
        if(ps_dec->u2_num_flds_decoded == 0)
362
3.03k
        {
363
3.03k
            pic_buf_t *ps_pic_buf;
364
3.03k
            u4_get_disp = 1;
365
366
3.03k
            ps_pic_buf = impeg2_buf_mgr_get_next_free(ps_dec->pv_pic_buf_mg, &ps_dec->i4_cur_buf_id);
367
368
3.03k
            if (NULL == ps_pic_buf)
369
329
            {
370
329
                return IMPEG2D_NO_FREE_BUF_ERR;
371
329
            }
372
373
2.70k
            impeg2_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mg, ps_dec->i4_cur_buf_id, BUF_MGR_DISP);
374
2.70k
            impeg2_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mg, ps_dec->i4_cur_buf_id, BUF_MGR_REF);
375
2.70k
            if(ps_dec->u4_deinterlace)
376
2.70k
                impeg2_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mg, ps_dec->i4_cur_buf_id, MPEG2_BUF_MGR_DEINT);
377
378
2.70k
            ps_pic_buf->e_pic_type = ps_dec->e_pic_type;
379
2.70k
            ps_dec->ps_cur_pic = ps_pic_buf;
380
2.70k
            ps_dec->s_cur_frm_buf.pu1_y = ps_pic_buf->pu1_y;
381
2.70k
            ps_dec->s_cur_frm_buf.pu1_u = ps_pic_buf->pu1_u;
382
2.70k
            ps_dec->s_cur_frm_buf.pu1_v = ps_pic_buf->pu1_v;
383
2.70k
        }
384
1.70k
        else
385
1.70k
        {
386
            /* Timestamp associated with second field is associated with the current picture */
387
1.70k
            ps_dec->ps_cur_pic->u4_ts = ps_dec->u4_inp_ts;
388
1.70k
        }
389
390
4.41k
        if(ps_dec->u2_picture_structure == TOP_FIELD)
391
3.21k
        {
392
3.21k
            ps_dec->u2_fld_parity = TOP;
393
3.21k
        }
394
1.19k
        else
395
1.19k
        {
396
1.19k
            ps_dec->u2_fld_parity = BOTTOM;
397
1.19k
        }
398
4.41k
        ps_dec->u2_field_dct           = 0;
399
4.41k
        ps_dec->u2_read_dct_type        = 0;
400
4.41k
        ps_dec->u2_read_motion_type     = 1;
401
4.41k
        ps_dec->u2_fld_pic             = 1;
402
4.41k
        ps_dec->u2_frm_pic             = 0;
403
4.41k
        ps_dec->ps_func_forw_or_back     = gas_impeg2d_func_fld_fw_or_bk;
404
4.41k
        ps_dec->ps_func_bi_direct       = gas_impeg2d_func_fld_bi_direct;
405
4.41k
   }
406
    /* Frame Picture */
407
23.0k
    else
408
23.0k
    {
409
23.0k
        pic_buf_t *ps_pic_buf;
410
411
412
23.0k
        ps_dec->u2_num_vert_mb       = (ps_dec->u2_vertical_size + 15) >> 4;
413
23.0k
        u4_get_disp = 1;
414
23.0k
        ps_pic_buf = impeg2_buf_mgr_get_next_free(ps_dec->pv_pic_buf_mg, &ps_dec->i4_cur_buf_id);
415
416
23.0k
        if (NULL == ps_pic_buf)
417
262
        {
418
262
            return IMPEG2D_NO_FREE_BUF_ERR;
419
262
        }
420
22.8k
        impeg2_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mg, ps_dec->i4_cur_buf_id, BUF_MGR_DISP);
421
22.8k
        impeg2_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mg, ps_dec->i4_cur_buf_id, BUF_MGR_REF);
422
22.8k
        if(ps_dec->u4_deinterlace)
423
22.8k
            impeg2_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mg, ps_dec->i4_cur_buf_id, MPEG2_BUF_MGR_DEINT);
424
425
22.8k
        ps_pic_buf->u4_ts = ps_dec->u4_inp_ts;
426
22.8k
        ps_pic_buf->e_pic_type = ps_dec->e_pic_type;
427
22.8k
        ps_dec->ps_cur_pic = ps_pic_buf;
428
22.8k
        ps_dec->s_cur_frm_buf.pu1_y = ps_pic_buf->pu1_y;
429
22.8k
        ps_dec->s_cur_frm_buf.pu1_u = ps_pic_buf->pu1_u;
430
22.8k
        ps_dec->s_cur_frm_buf.pu1_v = ps_pic_buf->pu1_v;
431
432
433
22.8k
        if(ps_dec->u2_frame_pred_frame_dct == 0)
434
5.85k
        {
435
5.85k
            ps_dec->u2_read_dct_type    = 1;
436
5.85k
            ps_dec->u2_read_motion_type = 1;
437
5.85k
        }
438
16.9k
        else
439
16.9k
        {
440
16.9k
            ps_dec->u2_read_dct_type    = 0;
441
16.9k
            ps_dec->u2_read_motion_type = 0;
442
16.9k
            ps_dec->u2_motion_type     = 2;
443
16.9k
            ps_dec->u2_field_dct       = 0;
444
16.9k
        }
445
446
22.8k
        ps_dec->u2_fld_parity          = TOP;
447
22.8k
        ps_dec->u2_fld_pic             = 0;
448
22.8k
        ps_dec->u2_frm_pic             = 1;
449
22.8k
        ps_dec->ps_func_forw_or_back     = gas_impeg2d_func_frm_fw_or_bk;
450
22.8k
        ps_dec->ps_func_bi_direct       = gas_impeg2d_func_frm_bi_direct;
451
22.8k
   }
452
27.2k
    ps_dec->u2_def_dc_pred[Y_LUMA]   = 128 << ps_dec->u2_intra_dc_precision;
453
27.2k
    ps_dec->u2_def_dc_pred[U_CHROMA]   = 128 << ps_dec->u2_intra_dc_precision;
454
27.2k
    ps_dec->u2_def_dc_pred[V_CHROMA]   = 128 << ps_dec->u2_intra_dc_precision;
455
27.2k
    ps_dec->u2_num_mbs_left  = ps_dec->u2_num_horiz_mb * ps_dec->u2_num_vert_mb;
456
27.2k
    if(u4_get_disp)
457
25.5k
    {
458
25.5k
        if(ps_dec->u4_num_frames_decoded > 1)
459
20.3k
        {
460
20.3k
            ps_disp_pic = impeg2_disp_mgr_get(&ps_dec->s_disp_mgr, &ps_dec->i4_disp_buf_id);
461
20.3k
        }
462
25.5k
        ps_dec->ps_disp_pic = ps_disp_pic;
463
25.5k
        if(ps_disp_pic)
464
16.7k
        {
465
16.7k
            if(1 == ps_dec->u4_share_disp_buf)
466
0
            {
467
0
                ps_dec->ps_disp_frm_buf->pv_y_buf  = ps_disp_pic->pu1_y;
468
0
                if(IV_YUV_420P == ps_dec->i4_chromaFormat)
469
0
                {
470
0
                    ps_dec->ps_disp_frm_buf->pv_u_buf  = ps_disp_pic->pu1_u;
471
0
                    ps_dec->ps_disp_frm_buf->pv_v_buf  = ps_disp_pic->pu1_v;
472
0
                }
473
0
                else
474
0
                {
475
0
                    UWORD8 *pu1_buf;
476
477
0
                    pu1_buf = ps_dec->as_disp_buffers[ps_disp_pic->i4_buf_id].pu1_bufs[1];
478
0
                    ps_dec->ps_disp_frm_buf->pv_u_buf  = pu1_buf;
479
480
0
                    pu1_buf = ps_dec->as_disp_buffers[ps_disp_pic->i4_buf_id].pu1_bufs[2];
481
0
                    ps_dec->ps_disp_frm_buf->pv_v_buf  = pu1_buf;
482
0
                }
483
0
            }
484
16.7k
        }
485
25.5k
    }
486
487
488
27.2k
    switch(ps_dec->e_pic_type)
489
27.2k
    {
490
6.90k
    case I_PIC:
491
6.90k
        {
492
6.90k
            ps_dec->pf_decode_slice = impeg2d_dec_i_slice;
493
6.90k
            break;
494
0
        }
495
9.07k
    case D_PIC:
496
9.07k
        {
497
9.07k
            ps_dec->pf_decode_slice = impeg2d_dec_d_slice;
498
9.07k
            break;
499
0
        }
500
5.09k
    case P_PIC:
501
5.09k
        {
502
5.09k
            ps_dec->pf_decode_slice = impeg2d_dec_p_b_slice;
503
5.09k
            ps_dec->pu2_mb_type       = gau2_impeg2d_p_mb_type;
504
5.09k
            break;
505
0
        }
506
6.16k
    case B_PIC:
507
6.16k
        {
508
6.16k
            ps_dec->pf_decode_slice = impeg2d_dec_p_b_slice;
509
6.16k
            ps_dec->pu2_mb_type       = gau2_impeg2d_b_mb_type;
510
6.16k
            break;
511
0
        }
512
0
    default:
513
0
        return IMPEG2D_INVALID_PIC_TYPE;
514
27.2k
    }
515
516
    /*************************************************************************/
517
    /* Set the reference pictures                                            */
518
    /*************************************************************************/
519
520
    /* Error resilience: If forward and backward pictures are going to be NULL*/
521
    /* then assign both to the current                                        */
522
    /* if one of them NULL then we will assign the non null to the NULL one   */
523
524
27.2k
    if(ps_dec->e_pic_type == P_PIC)
525
5.09k
    {
526
5.09k
        if (NULL == ps_dec->as_recent_fld[1][0].pu1_y)
527
893
        {
528
893
            ps_dec->as_recent_fld[1][0] = ps_dec->s_cur_frm_buf;
529
893
        }
530
5.09k
        if (NULL == ps_dec->as_recent_fld[1][1].pu1_y)
531
895
        {
532
895
            impeg2d_get_bottom_field_buf(&ps_dec->s_cur_frm_buf, &ps_dec->as_recent_fld[1][1],
533
895
                ps_dec->u2_frame_width);
534
895
        }
535
536
5.09k
        ps_dec->as_ref_buf[FORW][TOP]    = ps_dec->as_recent_fld[1][0];
537
5.09k
        ps_dec->as_ref_buf[FORW][BOTTOM] = ps_dec->as_recent_fld[1][1];
538
539
540
5.09k
    }
541
22.1k
    else if(ps_dec->e_pic_type == B_PIC)
542
6.16k
    {
543
6.16k
        if((NULL == ps_dec->as_recent_fld[1][0].pu1_y) && (NULL == ps_dec->as_recent_fld[0][0].pu1_y))
544
780
        {
545
            // assign the current picture to both
546
780
            ps_dec->as_recent_fld[1][0] = ps_dec->s_cur_frm_buf;
547
780
            impeg2d_get_bottom_field_buf(&ps_dec->s_cur_frm_buf, &ps_dec->as_recent_fld[1][1],
548
780
                ps_dec->u2_frame_width);
549
780
            ps_dec->as_recent_fld[0][0] = ps_dec->s_cur_frm_buf;
550
780
            ps_dec->as_recent_fld[0][1] = ps_dec->as_recent_fld[1][1];
551
780
        }
552
        //Assign the non-null picture to the null picture
553
5.38k
        else if ((NULL != ps_dec->as_recent_fld[1][0].pu1_y) && (NULL == ps_dec->as_recent_fld[0][0].pu1_y))
554
225
        {
555
225
            ps_dec->as_recent_fld[0][0] = ps_dec->as_recent_fld[1][0];
556
225
            ps_dec->as_recent_fld[0][1] = ps_dec->as_recent_fld[1][1];
557
225
        }
558
5.16k
        else if ((NULL == ps_dec->as_recent_fld[1][0].pu1_y) && (NULL != ps_dec->as_recent_fld[0][0].pu1_y))
559
0
        {
560
0
            ps_dec->as_recent_fld[1][0] = ps_dec->as_recent_fld[0][0];
561
0
            ps_dec->as_recent_fld[1][1] = ps_dec->as_recent_fld[0][1];
562
0
        }
563
564
        /* Error resilience: If forward and backward pictures are going to be NULL*/
565
        /* then assign both to the current                                        */
566
        /* if one of them NULL then we will assign the non null to the NULL one   */
567
568
6.16k
        if((NULL == ps_dec->as_recent_fld[0][1].pu1_y) && (NULL == ps_dec->as_recent_fld[1][1].pu1_y))
569
11
        {
570
            // assign the current picture to both
571
11
            ps_dec->as_recent_fld[1][0] = ps_dec->s_cur_frm_buf;
572
11
            impeg2d_get_bottom_field_buf(&ps_dec->s_cur_frm_buf, &ps_dec->as_recent_fld[1][1],
573
11
                                         ps_dec->u2_frame_width);
574
11
            ps_dec->as_recent_fld[0][0] = ps_dec->s_cur_frm_buf;
575
11
            ps_dec->as_recent_fld[0][1] = ps_dec->as_recent_fld[1][1];
576
11
        }
577
        //Assign the non-null picture to the null picture
578
579
6.15k
        else if((NULL == ps_dec->as_recent_fld[0][1].pu1_y) && (NULL != ps_dec->as_recent_fld[1][1].pu1_y))
580
17
        {
581
17
            ps_dec->as_recent_fld[0][0] = ps_dec->as_recent_fld[1][0];
582
17
            ps_dec->as_recent_fld[0][1] = ps_dec->as_recent_fld[1][1];
583
17
        }
584
585
6.13k
        else if((NULL == ps_dec->as_recent_fld[1][1].pu1_y) && (NULL != ps_dec->as_recent_fld[0][1].pu1_y))
586
0
        {
587
0
            ps_dec->as_recent_fld[1][0] = ps_dec->as_recent_fld[0][0];
588
0
            ps_dec->as_recent_fld[1][1] = ps_dec->as_recent_fld[0][1];
589
0
        }
590
6.16k
        ps_dec->as_ref_buf[FORW][TOP]    = ps_dec->as_recent_fld[0][0];
591
6.16k
        ps_dec->as_ref_buf[FORW][BOTTOM] = ps_dec->as_recent_fld[0][1];
592
6.16k
        ps_dec->as_ref_buf[BACK][TOP]    = ps_dec->as_recent_fld[1][0];
593
6.16k
        ps_dec->as_ref_buf[BACK][BOTTOM] = ps_dec->as_recent_fld[1][1];
594
595
596
6.16k
    }
597
598
27.2k
    return e_error;
599
27.2k
}
600
601
/*******************************************************************************
602
*
603
*  Function Name   : impeg2d_post_pic_dec_proc
604
*
605
*  Description     : Performs processing that is needed at the end of picture
606
*                    decode
607
*
608
*  Arguments       :
609
*  dec             : Decoder context
610
*
611
*  Values Returned : None
612
*******************************************************************************/
613
void impeg2d_post_pic_dec_proc(dec_state_t *ps_dec)
614
27.2k
{
615
616
27.2k
   WORD32 u4_update_pic_buf = 0;
617
    /*************************************************************************/
618
    /* Processing at the end of picture                                      */
619
    /*************************************************************************/
620
27.2k
    if(ps_dec->u2_picture_structure != FRAME_PICTURE)
621
4.41k
    {
622
4.41k
        ps_dec->u2_num_vert_mb       = (ps_dec->u2_vertical_size + 31) >> 5;
623
624
4.41k
        if(ps_dec->u2_num_flds_decoded == 1)
625
1.70k
        {
626
1.70k
            ps_dec->u2_num_flds_decoded = 0;
627
1.70k
            u4_update_pic_buf = 1;
628
1.70k
        }
629
2.70k
        else
630
2.70k
        {
631
2.70k
            ps_dec->u2_num_flds_decoded = 1;
632
2.70k
        }
633
4.41k
    }
634
22.8k
    else
635
22.8k
    {
636
22.8k
        u4_update_pic_buf = 1;
637
22.8k
    }
638
639
27.2k
    if(u4_update_pic_buf)
640
24.5k
    {
641
24.5k
        ps_dec->i4_frame_decoded = 1;
642
24.5k
        if(ps_dec->e_pic_type != B_PIC)
643
19.0k
        {
644
            /* In any sequence first two pictures have to be reference pictures */
645
            /* Adding of first picture in the sequence */
646
19.0k
            if(ps_dec->aps_ref_pics[0] == NULL)
647
5.10k
            {
648
5.10k
                ps_dec->aps_ref_pics[0] = ps_dec->ps_cur_pic;
649
5.10k
            }
650
651
            /* Adding of second picture in the sequence */
652
13.9k
            else if(ps_dec->aps_ref_pics[1] == NULL)
653
1.76k
            {
654
1.76k
                ps_dec->aps_ref_pics[1] = ps_dec->ps_cur_pic;
655
1.76k
                impeg2_disp_mgr_add(&ps_dec->s_disp_mgr, ps_dec->aps_ref_pics[0], ps_dec->aps_ref_pics[0]->i4_buf_id);
656
1.76k
            }
657
12.1k
            else
658
12.1k
            {
659
660
12.1k
                impeg2_disp_mgr_add(&ps_dec->s_disp_mgr, ps_dec->aps_ref_pics[1], ps_dec->aps_ref_pics[1]->i4_buf_id);
661
12.1k
                impeg2_buf_mgr_release(ps_dec->pv_pic_buf_mg, ps_dec->aps_ref_pics[0]->i4_buf_id, BUF_MGR_REF);
662
12.1k
                ps_dec->aps_ref_pics[0] = ps_dec->aps_ref_pics[1];
663
12.1k
                ps_dec->aps_ref_pics[1] = ps_dec->ps_cur_pic;
664
665
12.1k
            }
666
19.0k
        }
667
5.46k
        else
668
5.46k
        {
669
5.46k
            impeg2_disp_mgr_add(&ps_dec->s_disp_mgr, ps_dec->ps_cur_pic, ps_dec->ps_cur_pic->i4_buf_id);
670
671
5.46k
            impeg2_buf_mgr_release(ps_dec->pv_pic_buf_mg, ps_dec->ps_cur_pic->i4_buf_id, BUF_MGR_REF);
672
5.46k
        }
673
674
24.5k
    }
675
    /*************************************************************************/
676
    /* Update the list of recent reference pictures                          */
677
    /*************************************************************************/
678
27.2k
    if(ps_dec->e_pic_type != B_PIC)
679
21.0k
    {
680
21.0k
        switch(ps_dec->u2_picture_structure)
681
21.0k
        {
682
17.6k
        case FRAME_PICTURE:
683
17.6k
            {
684
17.6k
                ps_dec->as_recent_fld[0][0] = ps_dec->as_recent_fld[1][0];
685
17.6k
                ps_dec->as_recent_fld[0][1] = ps_dec->as_recent_fld[1][1];
686
687
17.6k
                ps_dec->as_recent_fld[1][0] = ps_dec->s_cur_frm_buf;
688
17.6k
                impeg2d_get_bottom_field_buf(&ps_dec->s_cur_frm_buf, &ps_dec->as_recent_fld[1][1],
689
17.6k
                ps_dec->u2_frame_width);
690
17.6k
                break;
691
0
            }
692
2.64k
        case TOP_FIELD:
693
2.64k
            {
694
2.64k
                ps_dec->as_recent_fld[0][0] = ps_dec->as_recent_fld[1][0];
695
2.64k
                ps_dec->as_recent_fld[1][0] = ps_dec->s_cur_frm_buf;
696
2.64k
                break;
697
0
            }
698
786
        case BOTTOM_FIELD:
699
786
            {
700
786
                ps_dec->as_recent_fld[0][1] = ps_dec->as_recent_fld[1][1];
701
786
                impeg2d_get_bottom_field_buf(&ps_dec->s_cur_frm_buf, &ps_dec->as_recent_fld[1][1],
702
786
                ps_dec->u2_frame_width);
703
786
                break;
704
0
            }
705
21.0k
        }
706
21.0k
    }
707
27.2k
}