Coverage Report

Created: 2024-06-24 06:30

/src/libmpeg2/decoder/impeg2d_pnb_pic.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_vld_tables.h"
42
#include "impeg2d_vld.h"
43
#include "impeg2d_pic_proc.h"
44
#include "impeg2d_debug.h"
45
#include "impeg2d_mc.h"
46
47
#define BLK_SIZE 8
48
#define LUMA_BLK_SIZE (2 * (BLK_SIZE))
49
#define CHROMA_BLK_SIZE (BLK_SIZE)
50
51
52
/*******************************************************************************
53
*
54
*  Function Name   : impeg2d_dec_p_mb_params
55
*
56
*  Description     : Decodes the parameters for P
57
*
58
*  Arguments       :
59
*  dec             : Decoder context
60
*
61
*  Values Returned : None
62
*******************************************************************************/
63
WORD32  impeg2d_dec_p_mb_params(dec_state_t *ps_dec)
64
518k
{
65
518k
    stream_t *ps_stream = &ps_dec->s_bit_stream;
66
518k
    UWORD16 u2_mb_addr_incr;
67
518k
    UWORD16 u2_total_len;
68
518k
    UWORD16 u2_len;
69
518k
    UWORD16 u2_mb_type;
70
518k
    UWORD32 u4_next_word;
71
518k
    const dec_mb_params_t *ps_dec_mb_params;
72
518k
    if(impeg2d_bit_stream_nxt(ps_stream,1) == 1)
73
75.9k
    {
74
75.9k
        impeg2d_bit_stream_flush(ps_stream,1);
75
76
75.9k
    }
77
442k
    else
78
442k
    {
79
442k
        u2_mb_addr_incr = impeg2d_get_mb_addr_incr(ps_stream);
80
81
442k
        if(!u2_mb_addr_incr)
82
62
        {
83
62
            return IV_FAIL;
84
62
        }
85
86
442k
        if(0 == ps_dec->u2_first_mb)
87
437k
        {
88
            /****************************************************************/
89
            /* If the 2nd member of a field picture pair is a P picture and */
90
            /* the first one was an I picture, there cannot be any skipped  */
91
            /* MBs in the second field picture                              */
92
            /****************************************************************/
93
            /*
94
            if((dec->picture_structure != FRAME_PICTURE) &&
95
                (dec->f->FieldFuncCall != 0) &&
96
                (dec->las->u1_last_coded_vop_type == I))
97
            {
98
                core0_err_handler((void *)(VOLParams),
99
                    ITTMPEG2_ERR_INVALID_MB_SKIP);
100
            }
101
            */
102
            /****************************************************************/
103
            /* In MPEG-2, the last MB of the row cannot be skipped and the  */
104
            /* MBAddrIncr cannot be such that it will take the current MB   */
105
            /* beyond the current row                                       */
106
            /* In MPEG-1, the slice could start and end anywhere and is not */
107
            /* restricted to a row like in MPEG-2. Hence this check should  */
108
            /* not be done for MPEG-1 streams.                              */
109
            /****************************************************************/
110
437k
            if(ps_dec->u2_is_mpeg2 && ((ps_dec->u2_mb_x + u2_mb_addr_incr) > ps_dec->u2_num_horiz_mb) )
111
486
            {
112
486
                u2_mb_addr_incr    = ps_dec->u2_num_horiz_mb - ps_dec->u2_mb_x;
113
486
            }
114
115
437k
            if ((u2_mb_addr_incr - 1) > ps_dec->u2_num_mbs_left)
116
331
            {
117
                /* If the number of skip MBs are more than the number of MBs
118
                 * left, indicate error.
119
                 */
120
331
                return IV_FAIL;
121
331
            }
122
123
437k
            impeg2d_dec_skip_mbs(ps_dec, (UWORD16)(u2_mb_addr_incr - 1));
124
437k
        }
125
4.87k
        else
126
4.87k
        {
127
128
            /****************************************************************/
129
            /* Section 6.3.17                                               */
130
            /* The first MB of a slice cannot be skipped                    */
131
            /* But the mb_addr_incr can be > 1, because at the beginning of */
132
            /* a slice, it indicates the offset from the last MB in the     */
133
            /* previous row. Hence for the first slice in a row, the        */
134
            /* mb_addr_incr needs to be 1.                                  */
135
            /****************************************************************/
136
            /* MB_x is set to zero whenever MB_y changes.                   */
137
4.87k
            ps_dec->u2_mb_x = u2_mb_addr_incr - 1;
138
            /* For error resilience */
139
4.87k
            ps_dec->u2_mb_x = MIN(ps_dec->u2_mb_x, (ps_dec->u2_num_horiz_mb - 1));
140
4.87k
            ps_dec->u2_num_mbs_left = ((ps_dec->u2_num_vert_mb - ps_dec->u2_mb_y)
141
4.87k
                            * ps_dec->u2_num_horiz_mb) - ps_dec->u2_mb_x;
142
143
            /****************************************************************/
144
            /* mb_addr_incr is forced to 1 because in this decoder it is used */
145
            /* more as an indicator of the number of MBs skipped than the   */
146
            /* as defined by the standard (Section 6.3.17)                  */
147
            /****************************************************************/
148
4.87k
            u2_mb_addr_incr = 1;
149
4.87k
            ps_dec->u2_first_mb = 0;
150
151
4.87k
        }
152
153
442k
    }
154
518k
    u4_next_word = (UWORD16)impeg2d_bit_stream_nxt(ps_stream,16);
155
    /*-----------------------------------------------------------------------*/
156
    /* MB type                                                               */
157
    /*-----------------------------------------------------------------------*/
158
518k
    {
159
518k
        u2_mb_type   = ps_dec->pu2_mb_type[BITS((UWORD16)u4_next_word,15,10)];
160
518k
        u2_len      = BITS(u2_mb_type,15,8);
161
518k
        u2_total_len = u2_len;
162
518k
        u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << u2_len);
163
518k
    }
164
    /*-----------------------------------------------------------------------*/
165
    /* motion type                                                           */
166
    /*-----------------------------------------------------------------------*/
167
518k
    {
168
518k
        if((u2_mb_type & MB_FORW_OR_BACK) &&  ps_dec->u2_read_motion_type)
169
55.6k
        {
170
55.6k
            WORD32 i4_motion_type;
171
55.6k
            ps_dec->u2_motion_type = BITS((UWORD16)u4_next_word,15,14);
172
55.6k
            u2_total_len        += MB_MOTION_TYPE_LEN;
173
55.6k
            u4_next_word        = (UWORD16)LSW((UWORD16)u4_next_word << MB_MOTION_TYPE_LEN);
174
55.6k
            i4_motion_type     = ps_dec->u2_motion_type;
175
176
55.6k
            if((i4_motion_type == 0) ||
177
55.6k
                (i4_motion_type == 4) ||
178
55.6k
                (i4_motion_type >  7))
179
294
            {
180
                //TODO : VANG Check for validity
181
294
                i4_motion_type = 1;
182
294
            }
183
184
55.6k
        }
185
518k
    }
186
    /*-----------------------------------------------------------------------*/
187
    /* dct type                                                              */
188
    /*-----------------------------------------------------------------------*/
189
518k
    {
190
518k
        if((u2_mb_type & MB_CODED) && ps_dec->u2_read_dct_type)
191
52.2k
        {
192
52.2k
            ps_dec->u2_field_dct = BIT((UWORD16)u4_next_word,15);
193
52.2k
            u2_total_len += MB_DCT_TYPE_LEN;
194
52.2k
            u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << MB_DCT_TYPE_LEN);
195
52.2k
        }
196
518k
    }
197
    /*-----------------------------------------------------------------------*/
198
    /* Quant scale code                                                      */
199
    /*-----------------------------------------------------------------------*/
200
518k
    if(u2_mb_type & MB_QUANT)
201
2.96k
    {
202
2.96k
        UWORD16 u2_quant_scale_code;
203
2.96k
        u2_quant_scale_code = BITS((UWORD16)u4_next_word,15,11);
204
205
2.96k
        ps_dec->u1_quant_scale = (ps_dec->u2_q_scale_type) ?
206
2.55k
            gau1_impeg2_non_linear_quant_scale[u2_quant_scale_code] : (u2_quant_scale_code << 1);
207
2.96k
        u2_total_len += MB_QUANT_SCALE_CODE_LEN;
208
2.96k
    }
209
518k
    impeg2d_bit_stream_flush(ps_stream,u2_total_len);
210
    /*-----------------------------------------------------------------------*/
211
    /* Set the function pointers                                             */
212
    /*-----------------------------------------------------------------------*/
213
518k
    ps_dec->u2_coded_mb    = (UWORD16)(u2_mb_type & MB_CODED);
214
215
518k
    if(u2_mb_type & MB_FORW_OR_BACK)
216
221k
    {
217
218
221k
        UWORD16 refPic      = !(u2_mb_type & MB_MV_FORW);
219
221k
        UWORD16 index       = (ps_dec->u2_motion_type);
220
221k
        ps_dec->u2_prev_intra_mb    = 0;
221
221k
        ps_dec->e_mb_pred         = (e_pred_direction_t)refPic;
222
221k
        ps_dec_mb_params = &ps_dec->ps_func_forw_or_back[index];
223
221k
        ps_dec->s_mb_type = ps_dec_mb_params->s_mb_type;
224
221k
        if(NULL == ps_dec_mb_params->pf_func_mb_params)
225
0
            return -1;
226
221k
        ps_dec_mb_params->pf_func_mb_params(ps_dec);
227
228
221k
    }
229
296k
    else if(u2_mb_type & MB_TYPE_INTRA)
230
1.49k
    {
231
1.49k
        ps_dec->u2_prev_intra_mb    = 1;
232
1.49k
        impeg2d_dec_intra_mb(ps_dec);
233
234
1.49k
    }
235
294k
    else
236
294k
    {
237
294k
        ps_dec->u2_prev_intra_mb    = 0;
238
294k
        ps_dec->e_mb_pred = FORW;
239
294k
        ps_dec->u2_motion_type = 0;
240
294k
        impeg2d_dec_0mv_coded_mb(ps_dec);
241
294k
    }
242
243
    /*-----------------------------------------------------------------------*/
244
    /* decode cbp                                                            */
245
    /*-----------------------------------------------------------------------*/
246
518k
    if((u2_mb_type & MB_TYPE_INTRA))
247
1.49k
    {
248
1.49k
        ps_dec->u2_cbp  = 0x3f;
249
1.49k
        ps_dec->u2_prev_intra_mb    = 1;
250
1.49k
    }
251
516k
    else
252
516k
    {
253
516k
        ps_dec->u2_prev_intra_mb  = 0;
254
516k
        ps_dec->u2_def_dc_pred[Y_LUMA] = 128 << ps_dec->u2_intra_dc_precision;
255
516k
        ps_dec->u2_def_dc_pred[U_CHROMA] = 128 << ps_dec->u2_intra_dc_precision;
256
516k
        ps_dec->u2_def_dc_pred[V_CHROMA] = 128 << ps_dec->u2_intra_dc_precision;
257
516k
        if((ps_dec->u2_coded_mb))
258
222k
        {
259
222k
            UWORD16 cbpValue;
260
222k
            cbpValue  = gau2_impeg2d_cbp_code[impeg2d_bit_stream_nxt(ps_stream,MB_CBP_LEN)];
261
222k
            ps_dec->u2_cbp  = cbpValue & 0xFF;
262
222k
            impeg2d_bit_stream_flush(ps_stream,(cbpValue >> 8) & 0x0FF);
263
222k
        }
264
294k
        else
265
294k
        {
266
294k
            ps_dec->u2_cbp  = 0;
267
294k
        }
268
516k
    }
269
518k
    return 0;
270
518k
}
271
272
273
/*******************************************************************************
274
*
275
*  Function Name   : impeg2d_dec_pnb_mb_params
276
*
277
*  Description     : Decodes the parameters for P and B pictures
278
*
279
*  Arguments       :
280
*  dec             : Decoder context
281
*
282
*  Values Returned : None
283
*******************************************************************************/
284
WORD32 impeg2d_dec_pnb_mb_params(dec_state_t *ps_dec)
285
60.9k
{
286
60.9k
    stream_t *ps_stream = &ps_dec->s_bit_stream;
287
60.9k
    UWORD16 u2_mb_addr_incr;
288
60.9k
    UWORD16 u2_total_len;
289
60.9k
    UWORD16 u2_len;
290
60.9k
    UWORD16 u2_mb_type;
291
60.9k
    UWORD32 u4_next_word;
292
60.9k
    const dec_mb_params_t *ps_dec_mb_params;
293
60.9k
    if(impeg2d_bit_stream_nxt(ps_stream,1) == 1)
294
26.2k
    {
295
26.2k
        impeg2d_bit_stream_flush(ps_stream,1);
296
297
26.2k
    }
298
34.7k
    else
299
34.7k
    {
300
34.7k
        u2_mb_addr_incr = impeg2d_get_mb_addr_incr(ps_stream);
301
302
34.7k
        if(0 == u2_mb_addr_incr)
303
64
        {
304
64
            return IV_FAIL;
305
64
        }
306
307
34.6k
        if(ps_dec->u2_first_mb)
308
3.53k
        {
309
            /****************************************************************/
310
            /* Section 6.3.17                                               */
311
            /* The first MB of a slice cannot be skipped                    */
312
            /* But the mb_addr_incr can be > 1, because at the beginning of */
313
            /* a slice, it indicates the offset from the last MB in the     */
314
            /* previous row. Hence for the first slice in a row, the        */
315
            /* mb_addr_incr needs to be 1.                                  */
316
            /****************************************************************/
317
            /* MB_x is set to zero whenever MB_y changes.                   */
318
3.53k
            ps_dec->u2_mb_x = u2_mb_addr_incr - 1;
319
            /* For error resilience */
320
3.53k
            ps_dec->u2_mb_x = MIN(ps_dec->u2_mb_x, (ps_dec->u2_num_horiz_mb - 1));
321
3.53k
            ps_dec->u2_num_mbs_left = ((ps_dec->u2_num_vert_mb - ps_dec->u2_mb_y)
322
3.53k
                            * ps_dec->u2_num_horiz_mb) - ps_dec->u2_mb_x;
323
324
            /****************************************************************/
325
            /* mb_addr_incr is forced to 1 because in this decoder it is used */
326
            /* more as an indicator of the number of MBs skipped than the   */
327
            /* as defined by the standard (Section 6.3.17)                  */
328
            /****************************************************************/
329
3.53k
            u2_mb_addr_incr = 1;
330
3.53k
            ps_dec->u2_first_mb = 0;
331
3.53k
        }
332
31.1k
        else
333
31.1k
        {
334
            /****************************************************************/
335
            /* In MPEG-2, the last MB of the row cannot be skipped and the  */
336
            /* mb_addr_incr cannot be such that it will take the current MB   */
337
            /* beyond the current row                                       */
338
            /* In MPEG-1, the slice could start and end anywhere and is not */
339
            /* restricted to a row like in MPEG-2. Hence this check should  */
340
            /* not be done for MPEG-1 streams.                              */
341
            /****************************************************************/
342
31.1k
            if(ps_dec->u2_is_mpeg2 &&
343
31.1k
                ((ps_dec->u2_mb_x + u2_mb_addr_incr) > ps_dec->u2_num_horiz_mb))
344
4.06k
            {
345
4.06k
                u2_mb_addr_incr    = ps_dec->u2_num_horiz_mb - ps_dec->u2_mb_x;
346
4.06k
            }
347
348
31.1k
            if ((u2_mb_addr_incr - 1) > ps_dec->u2_num_mbs_left)
349
219
            {
350
                /* If the number of skip MBs are more than the number of MBs
351
                 * left, indicate error.
352
                 */
353
219
                return IV_FAIL;
354
219
            }
355
356
30.9k
            impeg2d_dec_skip_mbs(ps_dec, (UWORD16)(u2_mb_addr_incr - 1));
357
30.9k
        }
358
359
34.6k
    }
360
60.6k
    u4_next_word = (UWORD16)impeg2d_bit_stream_nxt(ps_stream,16);
361
    /*-----------------------------------------------------------------------*/
362
    /* MB type                                                               */
363
    /*-----------------------------------------------------------------------*/
364
60.6k
    {
365
60.6k
        u2_mb_type   = ps_dec->pu2_mb_type[BITS((UWORD16)u4_next_word,15,10)];
366
60.6k
        u2_len      = BITS(u2_mb_type,15,8);
367
60.6k
        u2_total_len = u2_len;
368
60.6k
        u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << u2_len);
369
60.6k
    }
370
    /*-----------------------------------------------------------------------*/
371
    /* motion type                                                           */
372
    /*-----------------------------------------------------------------------*/
373
60.6k
    {
374
60.6k
        WORD32 i4_motion_type = ps_dec->u2_motion_type;
375
376
60.6k
        if((u2_mb_type & MB_FORW_OR_BACK) &&  ps_dec->u2_read_motion_type)
377
43.5k
        {
378
43.5k
            ps_dec->u2_motion_type = BITS((UWORD16)u4_next_word,15,14);
379
43.5k
            u2_total_len += MB_MOTION_TYPE_LEN;
380
43.5k
            u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << MB_MOTION_TYPE_LEN);
381
43.5k
            i4_motion_type     = ps_dec->u2_motion_type;
382
383
43.5k
        }
384
385
386
60.6k
        if ((u2_mb_type & MB_FORW_OR_BACK) &&
387
60.6k
            ((i4_motion_type == 0) ||
388
52.4k
            (i4_motion_type == 3) ||
389
52.4k
            (i4_motion_type == 4) ||
390
52.4k
            (i4_motion_type >= 7)))
391
13.8k
        {
392
            //TODO: VANG Check for validity
393
13.8k
            i4_motion_type = 1;
394
13.8k
        }
395
396
60.6k
    }
397
    /*-----------------------------------------------------------------------*/
398
    /* dct type                                                              */
399
    /*-----------------------------------------------------------------------*/
400
60.6k
    {
401
60.6k
        if((u2_mb_type & MB_CODED) && ps_dec->u2_read_dct_type)
402
13.4k
        {
403
13.4k
            ps_dec->u2_field_dct = BIT((UWORD16)u4_next_word,15);
404
13.4k
            u2_total_len += MB_DCT_TYPE_LEN;
405
13.4k
            u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << MB_DCT_TYPE_LEN);
406
13.4k
        }
407
60.6k
    }
408
    /*-----------------------------------------------------------------------*/
409
    /* Quant scale code                                                      */
410
    /*-----------------------------------------------------------------------*/
411
60.6k
    if(u2_mb_type & MB_QUANT)
412
6.09k
    {
413
6.09k
        UWORD16 u2_quant_scale_code;
414
6.09k
        u2_quant_scale_code = BITS((UWORD16)u4_next_word,15,11);
415
416
6.09k
        ps_dec->u1_quant_scale = (ps_dec->u2_q_scale_type) ?
417
3.11k
            gau1_impeg2_non_linear_quant_scale[u2_quant_scale_code] : (u2_quant_scale_code << 1);
418
6.09k
        u2_total_len += MB_QUANT_SCALE_CODE_LEN;
419
6.09k
    }
420
60.6k
    impeg2d_bit_stream_flush(ps_stream,u2_total_len);
421
    /*-----------------------------------------------------------------------*/
422
    /* Set the function pointers                                             */
423
    /*-----------------------------------------------------------------------*/
424
60.6k
    ps_dec->u2_coded_mb    = (UWORD16)(u2_mb_type & MB_CODED);
425
426
60.6k
    if(u2_mb_type & MB_BIDRECT)
427
18.2k
    {
428
18.2k
        UWORD16 u2_index       = (ps_dec->u2_motion_type);
429
430
18.2k
        ps_dec->u2_prev_intra_mb    = 0;
431
18.2k
        ps_dec->e_mb_pred         = BIDIRECT;
432
18.2k
        ps_dec_mb_params = &ps_dec->ps_func_bi_direct[u2_index];
433
18.2k
        ps_dec->s_mb_type = ps_dec_mb_params->s_mb_type;
434
18.2k
        if(NULL == ps_dec_mb_params->pf_func_mb_params)
435
981
            return -1;
436
17.2k
        ps_dec_mb_params->pf_func_mb_params(ps_dec);
437
17.2k
    }
438
42.4k
    else if(u2_mb_type & MB_FORW_OR_BACK)
439
34.2k
    {
440
441
34.2k
        UWORD16 u2_refPic      = !(u2_mb_type & MB_MV_FORW);
442
34.2k
        UWORD16 u2_index       = (ps_dec->u2_motion_type);
443
34.2k
        ps_dec->u2_prev_intra_mb    = 0;
444
34.2k
        ps_dec->e_mb_pred         = (e_pred_direction_t)u2_refPic;
445
34.2k
        ps_dec_mb_params = &ps_dec->ps_func_forw_or_back[u2_index];
446
34.2k
        ps_dec->s_mb_type = ps_dec_mb_params->s_mb_type;
447
34.2k
        if(NULL == ps_dec_mb_params->pf_func_mb_params)
448
0
            return -1;
449
34.2k
        ps_dec_mb_params->pf_func_mb_params(ps_dec);
450
451
34.2k
    }
452
8.17k
    else if(u2_mb_type & MB_TYPE_INTRA)
453
1.68k
    {
454
1.68k
        ps_dec->u2_prev_intra_mb    = 1;
455
1.68k
        impeg2d_dec_intra_mb(ps_dec);
456
457
1.68k
    }
458
6.48k
    else
459
6.48k
    {
460
6.48k
        ps_dec->u2_prev_intra_mb =0;
461
6.48k
        ps_dec->e_mb_pred = FORW;
462
6.48k
        ps_dec->u2_motion_type = 0;
463
6.48k
        impeg2d_dec_0mv_coded_mb(ps_dec);
464
6.48k
    }
465
466
    /*-----------------------------------------------------------------------*/
467
    /* decode cbp                                                            */
468
    /*-----------------------------------------------------------------------*/
469
59.7k
    if((u2_mb_type & MB_TYPE_INTRA))
470
1.68k
    {
471
1.68k
        ps_dec->u2_cbp  = 0x3f;
472
1.68k
        ps_dec->u2_prev_intra_mb    = 1;
473
1.68k
    }
474
58.0k
    else
475
58.0k
    {
476
58.0k
        ps_dec->u2_prev_intra_mb  = 0;
477
58.0k
        ps_dec->u2_def_dc_pred[Y_LUMA] = 128 << ps_dec->u2_intra_dc_precision;
478
58.0k
        ps_dec->u2_def_dc_pred[U_CHROMA] = 128 << ps_dec->u2_intra_dc_precision;
479
58.0k
        ps_dec->u2_def_dc_pred[V_CHROMA] = 128 << ps_dec->u2_intra_dc_precision;
480
58.0k
        if((ps_dec->u2_coded_mb))
481
15.5k
        {
482
15.5k
            UWORD16 cbpValue;
483
15.5k
            cbpValue  = gau2_impeg2d_cbp_code[impeg2d_bit_stream_nxt(ps_stream,MB_CBP_LEN)];
484
15.5k
            ps_dec->u2_cbp  = cbpValue & 0xFF;
485
15.5k
            impeg2d_bit_stream_flush(ps_stream,(cbpValue >> 8) & 0x0FF);
486
15.5k
        }
487
42.4k
        else
488
42.4k
        {
489
42.4k
            ps_dec->u2_cbp  = 0;
490
42.4k
        }
491
58.0k
    }
492
59.7k
    return 0;
493
60.6k
}
494
495
/*******************************************************************************
496
*  Function Name   : impeg2d_dec_p_b_slice
497
*
498
*  Description     : Decodes P and B slices
499
*
500
*  Arguments       :
501
*  dec             : Decoder state
502
*
503
*  Values Returned : None
504
*******************************************************************************/
505
IMPEG2D_ERROR_CODES_T impeg2d_dec_p_b_slice(dec_state_t *ps_dec)
506
11.1k
{
507
11.1k
    WORD16 *pi2_vld_out;
508
11.1k
    UWORD32 i;
509
11.1k
    yuv_buf_t *ps_cur_frm_buf      = &ps_dec->s_cur_frm_buf;
510
511
11.1k
    UWORD32 u4_frm_offset          = 0;
512
11.1k
    const dec_mb_params_t *ps_dec_mb_params;
513
11.1k
    IMPEG2D_ERROR_CODES_T e_error   = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
514
515
11.1k
    pi2_vld_out = ps_dec->ai2_vld_buf;
516
11.1k
    memset(ps_dec->ai2_pred_mv,0,sizeof(ps_dec->ai2_pred_mv));
517
518
11.1k
    ps_dec->u2_prev_intra_mb    = 0;
519
11.1k
    ps_dec->u2_first_mb       = 1;
520
521
11.1k
    ps_dec->u2_picture_width = ps_dec->u2_frame_width;
522
523
11.1k
    if(ps_dec->u2_picture_structure != FRAME_PICTURE)
524
1.22k
    {
525
1.22k
        ps_dec->u2_picture_width <<= 1;
526
1.22k
        if(ps_dec->u2_picture_structure == BOTTOM_FIELD)
527
387
        {
528
387
            u4_frm_offset = ps_dec->u2_frame_width;
529
387
        }
530
1.22k
    }
531
532
11.1k
    do
533
579k
    {
534
579k
        UWORD32 u4_x_offset, u4_y_offset;
535
579k
        WORD32 ret;
536
537
538
579k
        UWORD32 u4_x_dst_offset = 0;
539
579k
        UWORD32 u4_y_dst_offset = 0;
540
579k
        UWORD8  *pu1_out_p;
541
579k
        UWORD8  *pu1_pred;
542
579k
        WORD32 u4_pred_strd;
543
544
579k
        IMPEG2D_TRACE_MB_START(ps_dec->u2_mb_x, ps_dec->u2_mb_y);
545
546
579k
        if(ps_dec->e_pic_type == B_PIC)
547
60.9k
            ret = impeg2d_dec_pnb_mb_params(ps_dec);
548
518k
        else
549
518k
            ret = impeg2d_dec_p_mb_params(ps_dec);
550
551
579k
        if(ret)
552
1.65k
            return IMPEG2D_MB_TEX_DECODE_ERR;
553
554
578k
        if(0 >= ps_dec->u2_num_mbs_left)
555
100
        {
556
100
            break;
557
100
        }
558
559
577k
        IMPEG2D_TRACE_MB_START(ps_dec->u2_mb_x, ps_dec->u2_mb_y);
560
561
577k
        u4_x_dst_offset = u4_frm_offset + (ps_dec->u2_mb_x << 4);
562
577k
        u4_y_dst_offset = (ps_dec->u2_mb_y << 4) * ps_dec->u2_picture_width;
563
577k
        pu1_out_p = ps_cur_frm_buf->pu1_y + u4_x_dst_offset + u4_y_dst_offset;
564
577k
        if(ps_dec->u2_prev_intra_mb == 0)
565
575k
        {
566
575k
            UWORD32 offset_x, offset_y, stride;
567
575k
            UWORD16 index = (ps_dec->u2_motion_type);
568
            /*only for non intra mb's*/
569
575k
            if(ps_dec->e_mb_pred == BIDIRECT)
570
17.2k
            {
571
17.2k
                ps_dec_mb_params = &ps_dec->ps_func_bi_direct[index];
572
17.2k
            }
573
557k
            else
574
557k
            {
575
557k
                ps_dec_mb_params = &ps_dec->ps_func_forw_or_back[index];
576
557k
            }
577
578
575k
            stride = ps_dec->u2_picture_width;
579
580
575k
            offset_x = u4_frm_offset + (ps_dec->u2_mb_x << 4);
581
582
575k
            offset_y = (ps_dec->u2_mb_y << 4);
583
584
575k
            ps_dec->s_dest_buf.pu1_y = ps_cur_frm_buf->pu1_y + offset_y * stride + offset_x;
585
586
575k
            stride = stride >> 1;
587
588
575k
            ps_dec->s_dest_buf.pu1_u = ps_cur_frm_buf->pu1_u + (offset_y >> 1) * stride
589
575k
                            + (offset_x >> 1);
590
591
575k
            ps_dec->s_dest_buf.pu1_v = ps_cur_frm_buf->pu1_v + (offset_y >> 1) * stride
592
575k
                            + (offset_x >> 1);
593
594
575k
            PROFILE_DISABLE_MC_IF0
595
575k
            ps_dec_mb_params->pf_mc(ps_dec);
596
597
575k
        }
598
2.87M
        for(i = 0; i < NUM_LUMA_BLKS; ++i)
599
2.30M
        {
600
2.30M
            if((ps_dec->u2_cbp & (1 << (BLOCKS_IN_MB - 1 - i))) != 0)
601
343k
            {
602
343k
                e_error = ps_dec->pf_vld_inv_quant(ps_dec, pi2_vld_out, ps_dec->pu1_inv_scan_matrix,
603
343k
                              ps_dec->u2_prev_intra_mb, Y_LUMA, 0);
604
343k
                if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
605
3.95k
                {
606
3.95k
                    return e_error;
607
3.95k
                }
608
609
339k
                u4_x_offset = gai2_impeg2_blk_x_off[i];
610
611
339k
                if(ps_dec->u2_field_dct == 0)
612
134k
                    u4_y_offset = gai2_impeg2_blk_y_off_frm[i] ;
613
205k
                else
614
205k
                    u4_y_offset = gai2_impeg2_blk_y_off_fld[i] ;
615
616
617
618
619
620
339k
                IMPEG2D_IDCT_INP_STATISTICS(pi2_vld_out, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows);
621
622
339k
                PROFILE_DISABLE_IDCT_IF0
623
339k
                {
624
339k
                    WORD32 idx;
625
339k
                    if(1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
626
24.2k
                        idx = 0;
627
315k
                    else
628
315k
                        idx = 1;
629
630
339k
                    if(0 == ps_dec->u2_prev_intra_mb)
631
330k
                    {
632
330k
                        pu1_pred = pu1_out_p + u4_y_offset * ps_dec->u2_picture_width + u4_x_offset;
633
330k
                        u4_pred_strd = ps_dec->u2_picture_width << ps_dec->u2_field_dct;
634
330k
                    }
635
8.96k
                    else
636
8.96k
                    {
637
8.96k
                        pu1_pred = (UWORD8 *)gau1_impeg2_zerobuf;
638
8.96k
                        u4_pred_strd = 8;
639
8.96k
                    }
640
641
339k
                    ps_dec->pf_idct_recon[idx * 2 + ps_dec->i4_last_value_one](pi2_vld_out,
642
339k
                                                            ps_dec->ai2_idct_stg1,
643
339k
                                                            pu1_pred,
644
339k
                                                            pu1_out_p + u4_y_offset * ps_dec->u2_picture_width + u4_x_offset,
645
339k
                                                            8,
646
339k
                                                            u4_pred_strd,
647
339k
                                                            ps_dec->u2_picture_width << ps_dec->u2_field_dct,
648
339k
                                                            ~ps_dec->u4_non_zero_cols, ~ps_dec->u4_non_zero_rows);
649
339k
                }
650
339k
            }
651
652
2.30M
        }
653
654
        /* For U and V blocks, divide the x and y offsets by 2. */
655
573k
        u4_x_dst_offset >>= 1;
656
573k
        u4_y_dst_offset >>= 2;
657
658
659
        /* In case of chrominance blocks the DCT will be frame DCT */
660
        /* i = 0, U component and i = 1 is V componet */
661
573k
        if((ps_dec->u2_cbp & 0x02) != 0)
662
16.4k
        {
663
16.4k
            pu1_out_p = ps_cur_frm_buf->pu1_u + u4_x_dst_offset + u4_y_dst_offset;
664
16.4k
            e_error = ps_dec->pf_vld_inv_quant(ps_dec, pi2_vld_out, ps_dec->pu1_inv_scan_matrix,
665
16.4k
                          ps_dec->u2_prev_intra_mb, U_CHROMA, 0);
666
16.4k
            if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
667
426
            {
668
426
                return e_error;
669
426
            }
670
671
672
16.0k
            IMPEG2D_IDCT_INP_STATISTICS(pi2_vld_out, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows);
673
674
16.0k
            PROFILE_DISABLE_IDCT_IF0
675
16.0k
            {
676
16.0k
                WORD32 idx;
677
16.0k
                if(1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
678
5.01k
                    idx = 0;
679
11.0k
                else
680
11.0k
                    idx = 1;
681
682
16.0k
                if(0 == ps_dec->u2_prev_intra_mb)
683
14.0k
                {
684
14.0k
                    pu1_pred = pu1_out_p;
685
14.0k
                    u4_pred_strd = ps_dec->u2_picture_width >> 1;
686
14.0k
                }
687
1.98k
                else
688
1.98k
                {
689
1.98k
                    pu1_pred = (UWORD8 *)gau1_impeg2_zerobuf;
690
1.98k
                    u4_pred_strd = 8;
691
1.98k
                }
692
693
16.0k
                ps_dec->pf_idct_recon[idx * 2 + ps_dec->i4_last_value_one](pi2_vld_out,
694
16.0k
                                                        ps_dec->ai2_idct_stg1,
695
16.0k
                                                        pu1_pred,
696
16.0k
                                                        pu1_out_p,
697
16.0k
                                                        8,
698
16.0k
                                                        u4_pred_strd,
699
16.0k
                                                        ps_dec->u2_picture_width >> 1,
700
16.0k
                                                        ~ps_dec->u4_non_zero_cols, ~ps_dec->u4_non_zero_rows);
701
702
16.0k
            }
703
704
16.0k
        }
705
706
707
573k
        if((ps_dec->u2_cbp & 0x01) != 0)
708
9.51k
        {
709
9.51k
            pu1_out_p = ps_cur_frm_buf->pu1_v + u4_x_dst_offset + u4_y_dst_offset;
710
9.51k
            e_error = ps_dec->pf_vld_inv_quant(ps_dec, pi2_vld_out, ps_dec->pu1_inv_scan_matrix,
711
9.51k
                          ps_dec->u2_prev_intra_mb, V_CHROMA, 0);
712
9.51k
            if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
713
303
            {
714
303
                return e_error;
715
303
            }
716
717
718
9.21k
            IMPEG2D_IDCT_INP_STATISTICS(pi2_vld_out, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows);
719
720
9.21k
            PROFILE_DISABLE_IDCT_IF0
721
9.21k
            {
722
9.21k
                WORD32 idx;
723
9.21k
                if(1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
724
3.55k
                    idx = 0;
725
5.66k
                else
726
5.66k
                    idx = 1;
727
9.21k
                if(0 == ps_dec->u2_prev_intra_mb)
728
7.37k
                {
729
7.37k
                    pu1_pred = pu1_out_p;
730
7.37k
                    u4_pred_strd = ps_dec->u2_picture_width >> 1;
731
7.37k
                }
732
1.83k
                else
733
1.83k
                {
734
1.83k
                    pu1_pred = (UWORD8 *)gau1_impeg2_zerobuf;
735
1.83k
                    u4_pred_strd = 8;
736
1.83k
                }
737
738
9.21k
                ps_dec->pf_idct_recon[idx * 2 + ps_dec->i4_last_value_one](pi2_vld_out,
739
9.21k
                                                        ps_dec->ai2_idct_stg1,
740
9.21k
                                                        pu1_pred,
741
9.21k
                                                        pu1_out_p,
742
9.21k
                                                        8,
743
9.21k
                                                        u4_pred_strd,
744
9.21k
                                                        ps_dec->u2_picture_width >> 1,
745
9.21k
                                                        ~ps_dec->u4_non_zero_cols, ~ps_dec->u4_non_zero_rows);
746
747
9.21k
            }
748
9.21k
        }
749
750
573k
        ps_dec->u2_num_mbs_left--;
751
573k
        ps_dec->u2_first_mb = 0;
752
573k
        ps_dec->u2_mb_x++;
753
754
573k
        if(ps_dec->s_bit_stream.u4_offset > ps_dec->s_bit_stream.u4_max_offset)
755
702
        {
756
702
            return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
757
702
        }
758
572k
        else if (ps_dec->u2_mb_x == ps_dec->u2_num_horiz_mb)
759
12.7k
        {
760
12.7k
            ps_dec->u2_mb_x = 0;
761
12.7k
            ps_dec->u2_mb_y++;
762
763
12.7k
        }
764
573k
    }
765
572k
    while(ps_dec->u2_num_mbs_left != 0 && impeg2d_bit_stream_nxt(&ps_dec->s_bit_stream,23) != 0x0);
766
4.12k
    return e_error;
767
11.1k
}