Coverage Report

Created: 2026-10-28 06:50

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