Coverage Report

Created: 2025-11-24 06:08

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_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
8.39M
{
65
8.39M
    stream_t *ps_stream = &ps_dec->s_bit_stream;
66
8.39M
    UWORD16 u2_mb_addr_incr;
67
8.39M
    UWORD16 u2_total_len;
68
8.39M
    UWORD16 u2_len;
69
8.39M
    UWORD16 u2_mb_type;
70
8.39M
    UWORD32 u4_next_word;
71
8.39M
    const dec_mb_params_t *ps_dec_mb_params;
72
8.39M
    if(impeg2d_bit_stream_nxt(ps_stream,1) == 1)
73
7.41M
    {
74
7.41M
        impeg2d_bit_stream_flush(ps_stream,1);
75
76
7.41M
    }
77
978k
    else
78
978k
    {
79
978k
        u2_mb_addr_incr = impeg2d_get_mb_addr_incr(ps_stream);
80
81
978k
        if(!u2_mb_addr_incr)
82
330
        {
83
330
            return IV_FAIL;
84
330
        }
85
86
978k
        if(0 == ps_dec->u2_first_mb)
87
911k
        {
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
911k
            if(ps_dec->u2_is_mpeg2 && ((ps_dec->u2_mb_x + u2_mb_addr_incr) > ps_dec->u2_num_horiz_mb) )
111
1.39k
            {
112
1.39k
                u2_mb_addr_incr    = ps_dec->u2_num_horiz_mb - ps_dec->u2_mb_x;
113
1.39k
            }
114
115
911k
            if ((u2_mb_addr_incr - 1) > ps_dec->u2_num_mbs_left)
116
298
            {
117
                /* If the number of skip MBs are more than the number of MBs
118
                 * left, indicate error.
119
                 */
120
298
                return IV_FAIL;
121
298
            }
122
123
911k
            impeg2d_dec_skip_mbs(ps_dec, (UWORD16)(u2_mb_addr_incr - 1));
124
911k
        }
125
67.0k
        else
126
67.0k
        {
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
67.0k
            ps_dec->u2_mb_x = u2_mb_addr_incr - 1;
138
            /* For error resilience */
139
67.0k
            ps_dec->u2_mb_x = MIN(ps_dec->u2_mb_x, (ps_dec->u2_num_horiz_mb - 1));
140
67.0k
            ps_dec->u2_num_mbs_left = ((ps_dec->u2_num_vert_mb - ps_dec->u2_mb_y)
141
67.0k
                            * 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
67.0k
            u2_mb_addr_incr = 1;
149
67.0k
            ps_dec->u2_first_mb = 0;
150
151
67.0k
        }
152
153
978k
    }
154
8.39M
    u4_next_word = (UWORD16)impeg2d_bit_stream_nxt(ps_stream,16);
155
    /*-----------------------------------------------------------------------*/
156
    /* MB type                                                               */
157
    /*-----------------------------------------------------------------------*/
158
8.39M
    {
159
8.39M
        u2_mb_type   = ps_dec->pu2_mb_type[BITS((UWORD16)u4_next_word,15,10)];
160
8.39M
        u2_len      = BITS(u2_mb_type,15,8);
161
8.39M
        u2_total_len = u2_len;
162
8.39M
        u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << u2_len);
163
8.39M
    }
164
    /*-----------------------------------------------------------------------*/
165
    /* motion type                                                           */
166
    /*-----------------------------------------------------------------------*/
167
8.39M
    {
168
8.39M
        if((u2_mb_type & MB_FORW_OR_BACK) &&  ps_dec->u2_read_motion_type)
169
17.3k
        {
170
17.3k
            WORD32 i4_motion_type;
171
17.3k
            ps_dec->u2_motion_type = BITS((UWORD16)u4_next_word,15,14);
172
17.3k
            u2_total_len        += MB_MOTION_TYPE_LEN;
173
17.3k
            u4_next_word        = (UWORD16)LSW((UWORD16)u4_next_word << MB_MOTION_TYPE_LEN);
174
17.3k
            i4_motion_type     = ps_dec->u2_motion_type;
175
176
17.3k
            if((i4_motion_type == 0) ||
177
16.8k
                (i4_motion_type == 4) ||
178
16.8k
                (i4_motion_type >  7))
179
538
            {
180
                //TODO : VANG Check for validity
181
538
                i4_motion_type = 1;
182
538
            }
183
184
17.3k
        }
185
8.39M
    }
186
    /*-----------------------------------------------------------------------*/
187
    /* dct type                                                              */
188
    /*-----------------------------------------------------------------------*/
189
8.39M
    {
190
8.39M
        if((u2_mb_type & MB_CODED) && ps_dec->u2_read_dct_type)
191
3.45k
        {
192
3.45k
            ps_dec->u2_field_dct = BIT((UWORD16)u4_next_word,15);
193
3.45k
            u2_total_len += MB_DCT_TYPE_LEN;
194
3.45k
            u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << MB_DCT_TYPE_LEN);
195
3.45k
        }
196
8.39M
    }
197
    /*-----------------------------------------------------------------------*/
198
    /* Quant scale code                                                      */
199
    /*-----------------------------------------------------------------------*/
200
8.39M
    if(u2_mb_type & MB_QUANT)
201
81.6k
    {
202
81.6k
        UWORD16 u2_quant_scale_code;
203
81.6k
        u2_quant_scale_code = BITS((UWORD16)u4_next_word,15,11);
204
205
81.6k
        ps_dec->u1_quant_scale = (ps_dec->u2_q_scale_type) ?
206
81.2k
            gau1_impeg2_non_linear_quant_scale[u2_quant_scale_code] : (u2_quant_scale_code << 1);
207
81.6k
        u2_total_len += MB_QUANT_SCALE_CODE_LEN;
208
81.6k
    }
209
8.39M
    impeg2d_bit_stream_flush(ps_stream,u2_total_len);
210
    /*-----------------------------------------------------------------------*/
211
    /* Set the function pointers                                             */
212
    /*-----------------------------------------------------------------------*/
213
8.39M
    ps_dec->u2_coded_mb    = (UWORD16)(u2_mb_type & MB_CODED);
214
215
8.39M
    if(u2_mb_type & MB_FORW_OR_BACK)
216
7.68M
    {
217
218
7.68M
        UWORD16 refPic      = !(u2_mb_type & MB_MV_FORW);
219
7.68M
        UWORD16 index       = (ps_dec->u2_motion_type);
220
7.68M
        ps_dec->u2_prev_intra_mb    = 0;
221
7.68M
        ps_dec->e_mb_pred         = (e_pred_direction_t)refPic;
222
7.68M
        ps_dec_mb_params = &ps_dec->ps_func_forw_or_back[index];
223
7.68M
        ps_dec->s_mb_type = ps_dec_mb_params->s_mb_type;
224
7.68M
        if(NULL == ps_dec_mb_params->pf_func_mb_params)
225
0
            return -1;
226
7.68M
        ps_dec_mb_params->pf_func_mb_params(ps_dec);
227
228
7.68M
    }
229
707k
    else if(u2_mb_type & MB_TYPE_INTRA)
230
2.56k
    {
231
2.56k
        ps_dec->u2_prev_intra_mb    = 1;
232
2.56k
        impeg2d_dec_intra_mb(ps_dec);
233
234
2.56k
    }
235
704k
    else
236
704k
    {
237
704k
        ps_dec->u2_prev_intra_mb    = 0;
238
704k
        ps_dec->e_mb_pred = FORW;
239
704k
        ps_dec->u2_motion_type = 0;
240
704k
        impeg2d_dec_0mv_coded_mb(ps_dec);
241
704k
    }
242
243
    /*-----------------------------------------------------------------------*/
244
    /* decode cbp                                                            */
245
    /*-----------------------------------------------------------------------*/
246
8.39M
    if((u2_mb_type & MB_TYPE_INTRA))
247
2.56k
    {
248
2.56k
        ps_dec->u2_cbp  = 0x3f;
249
2.56k
        ps_dec->u2_prev_intra_mb    = 1;
250
2.56k
    }
251
8.38M
    else
252
8.38M
    {
253
8.38M
        ps_dec->u2_prev_intra_mb  = 0;
254
8.38M
        ps_dec->u2_def_dc_pred[Y_LUMA] = 128 << ps_dec->u2_intra_dc_precision;
255
8.38M
        ps_dec->u2_def_dc_pred[U_CHROMA] = 128 << ps_dec->u2_intra_dc_precision;
256
8.38M
        ps_dec->u2_def_dc_pred[V_CHROMA] = 128 << ps_dec->u2_intra_dc_precision;
257
8.38M
        if((ps_dec->u2_coded_mb))
258
7.99M
        {
259
7.99M
            UWORD16 cbpValue;
260
7.99M
            cbpValue  = gau2_impeg2d_cbp_code[impeg2d_bit_stream_nxt(ps_stream,MB_CBP_LEN)];
261
7.99M
            ps_dec->u2_cbp  = cbpValue & 0xFF;
262
7.99M
            impeg2d_bit_stream_flush(ps_stream,(cbpValue >> 8) & 0x0FF);
263
7.99M
        }
264
388k
        else
265
388k
        {
266
388k
            ps_dec->u2_cbp  = 0;
267
388k
        }
268
8.38M
    }
269
8.39M
    return 0;
270
8.39M
}
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
220k
{
286
220k
    stream_t *ps_stream = &ps_dec->s_bit_stream;
287
220k
    UWORD16 u2_mb_addr_incr;
288
220k
    UWORD16 u2_total_len;
289
220k
    UWORD16 u2_len;
290
220k
    UWORD16 u2_mb_type;
291
220k
    UWORD32 u4_next_word;
292
220k
    const dec_mb_params_t *ps_dec_mb_params;
293
220k
    if(impeg2d_bit_stream_nxt(ps_stream,1) == 1)
294
100k
    {
295
100k
        impeg2d_bit_stream_flush(ps_stream,1);
296
297
100k
    }
298
120k
    else
299
120k
    {
300
120k
        u2_mb_addr_incr = impeg2d_get_mb_addr_incr(ps_stream);
301
302
120k
        if(0 == u2_mb_addr_incr)
303
148
        {
304
148
            return IV_FAIL;
305
148
        }
306
307
120k
        if(ps_dec->u2_first_mb)
308
5.67k
        {
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
5.67k
            ps_dec->u2_mb_x = u2_mb_addr_incr - 1;
319
            /* For error resilience */
320
5.67k
            ps_dec->u2_mb_x = MIN(ps_dec->u2_mb_x, (ps_dec->u2_num_horiz_mb - 1));
321
5.67k
            ps_dec->u2_num_mbs_left = ((ps_dec->u2_num_vert_mb - ps_dec->u2_mb_y)
322
5.67k
                            * 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
5.67k
            u2_mb_addr_incr = 1;
330
5.67k
            ps_dec->u2_first_mb = 0;
331
5.67k
        }
332
114k
        else
333
114k
        {
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
114k
            if(ps_dec->u2_is_mpeg2 &&
343
93.4k
                ((ps_dec->u2_mb_x + u2_mb_addr_incr) > ps_dec->u2_num_horiz_mb))
344
13.8k
            {
345
13.8k
                u2_mb_addr_incr    = ps_dec->u2_num_horiz_mb - ps_dec->u2_mb_x;
346
13.8k
            }
347
348
114k
            if ((u2_mb_addr_incr - 1) > ps_dec->u2_num_mbs_left)
349
245
            {
350
                /* If the number of skip MBs are more than the number of MBs
351
                 * left, indicate error.
352
                 */
353
245
                return IV_FAIL;
354
245
            }
355
356
114k
            impeg2d_dec_skip_mbs(ps_dec, (UWORD16)(u2_mb_addr_incr - 1));
357
114k
        }
358
359
120k
    }
360
219k
    u4_next_word = (UWORD16)impeg2d_bit_stream_nxt(ps_stream,16);
361
    /*-----------------------------------------------------------------------*/
362
    /* MB type                                                               */
363
    /*-----------------------------------------------------------------------*/
364
219k
    {
365
219k
        u2_mb_type   = ps_dec->pu2_mb_type[BITS((UWORD16)u4_next_word,15,10)];
366
219k
        u2_len      = BITS(u2_mb_type,15,8);
367
219k
        u2_total_len = u2_len;
368
219k
        u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << u2_len);
369
219k
    }
370
    /*-----------------------------------------------------------------------*/
371
    /* motion type                                                           */
372
    /*-----------------------------------------------------------------------*/
373
219k
    {
374
219k
        WORD32 i4_motion_type = ps_dec->u2_motion_type;
375
376
219k
        if((u2_mb_type & MB_FORW_OR_BACK) &&  ps_dec->u2_read_motion_type)
377
174k
        {
378
174k
            ps_dec->u2_motion_type = BITS((UWORD16)u4_next_word,15,14);
379
174k
            u2_total_len += MB_MOTION_TYPE_LEN;
380
174k
            u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << MB_MOTION_TYPE_LEN);
381
174k
            i4_motion_type     = ps_dec->u2_motion_type;
382
383
174k
        }
384
385
386
219k
        if ((u2_mb_type & MB_FORW_OR_BACK) &&
387
202k
            ((i4_motion_type == 0) ||
388
173k
            (i4_motion_type == 3) ||
389
156k
            (i4_motion_type == 4) ||
390
156k
            (i4_motion_type >= 7)))
391
47.6k
        {
392
            //TODO: VANG Check for validity
393
47.6k
            i4_motion_type = 1;
394
47.6k
        }
395
396
219k
    }
397
    /*-----------------------------------------------------------------------*/
398
    /* dct type                                                              */
399
    /*-----------------------------------------------------------------------*/
400
219k
    {
401
219k
        if((u2_mb_type & MB_CODED) && ps_dec->u2_read_dct_type)
402
52.9k
        {
403
52.9k
            ps_dec->u2_field_dct = BIT((UWORD16)u4_next_word,15);
404
52.9k
            u2_total_len += MB_DCT_TYPE_LEN;
405
52.9k
            u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << MB_DCT_TYPE_LEN);
406
52.9k
        }
407
219k
    }
408
    /*-----------------------------------------------------------------------*/
409
    /* Quant scale code                                                      */
410
    /*-----------------------------------------------------------------------*/
411
219k
    if(u2_mb_type & MB_QUANT)
412
29.0k
    {
413
29.0k
        UWORD16 u2_quant_scale_code;
414
29.0k
        u2_quant_scale_code = BITS((UWORD16)u4_next_word,15,11);
415
416
29.0k
        ps_dec->u1_quant_scale = (ps_dec->u2_q_scale_type) ?
417
14.7k
            gau1_impeg2_non_linear_quant_scale[u2_quant_scale_code] : (u2_quant_scale_code << 1);
418
29.0k
        u2_total_len += MB_QUANT_SCALE_CODE_LEN;
419
29.0k
    }
420
219k
    impeg2d_bit_stream_flush(ps_stream,u2_total_len);
421
    /*-----------------------------------------------------------------------*/
422
    /* Set the function pointers                                             */
423
    /*-----------------------------------------------------------------------*/
424
219k
    ps_dec->u2_coded_mb    = (UWORD16)(u2_mb_type & MB_CODED);
425
426
219k
    if(u2_mb_type & MB_BIDRECT)
427
75.5k
    {
428
75.5k
        UWORD16 u2_index       = (ps_dec->u2_motion_type);
429
430
75.5k
        ps_dec->u2_prev_intra_mb    = 0;
431
75.5k
        ps_dec->e_mb_pred         = BIDIRECT;
432
75.5k
        ps_dec_mb_params = &ps_dec->ps_func_bi_direct[u2_index];
433
75.5k
        ps_dec->s_mb_type = ps_dec_mb_params->s_mb_type;
434
75.5k
        if(NULL == ps_dec_mb_params->pf_func_mb_params)
435
2.16k
            return -1;
436
73.3k
        ps_dec_mb_params->pf_func_mb_params(ps_dec);
437
73.3k
    }
438
144k
    else if(u2_mb_type & MB_FORW_OR_BACK)
439
127k
    {
440
441
127k
        UWORD16 u2_refPic      = !(u2_mb_type & MB_MV_FORW);
442
127k
        UWORD16 u2_index       = (ps_dec->u2_motion_type);
443
127k
        ps_dec->u2_prev_intra_mb    = 0;
444
127k
        ps_dec->e_mb_pred         = (e_pred_direction_t)u2_refPic;
445
127k
        ps_dec_mb_params = &ps_dec->ps_func_forw_or_back[u2_index];
446
127k
        ps_dec->s_mb_type = ps_dec_mb_params->s_mb_type;
447
127k
        if(NULL == ps_dec_mb_params->pf_func_mb_params)
448
0
            return -1;
449
127k
        ps_dec_mb_params->pf_func_mb_params(ps_dec);
450
451
127k
    }
452
16.7k
    else if(u2_mb_type & MB_TYPE_INTRA)
453
5.49k
    {
454
5.49k
        ps_dec->u2_prev_intra_mb    = 1;
455
5.49k
        impeg2d_dec_intra_mb(ps_dec);
456
457
5.49k
    }
458
11.2k
    else
459
11.2k
    {
460
11.2k
        ps_dec->u2_prev_intra_mb =0;
461
11.2k
        ps_dec->e_mb_pred = FORW;
462
11.2k
        ps_dec->u2_motion_type = 0;
463
11.2k
        impeg2d_dec_0mv_coded_mb(ps_dec);
464
11.2k
    }
465
466
    /*-----------------------------------------------------------------------*/
467
    /* decode cbp                                                            */
468
    /*-----------------------------------------------------------------------*/
469
217k
    if((u2_mb_type & MB_TYPE_INTRA))
470
5.48k
    {
471
5.48k
        ps_dec->u2_cbp  = 0x3f;
472
5.48k
        ps_dec->u2_prev_intra_mb    = 1;
473
5.48k
    }
474
212k
    else
475
212k
    {
476
212k
        ps_dec->u2_prev_intra_mb  = 0;
477
212k
        ps_dec->u2_def_dc_pred[Y_LUMA] = 128 << ps_dec->u2_intra_dc_precision;
478
212k
        ps_dec->u2_def_dc_pred[U_CHROMA] = 128 << ps_dec->u2_intra_dc_precision;
479
212k
        ps_dec->u2_def_dc_pred[V_CHROMA] = 128 << ps_dec->u2_intra_dc_precision;
480
212k
        if((ps_dec->u2_coded_mb))
481
62.8k
        {
482
62.8k
            UWORD16 cbpValue;
483
62.8k
            cbpValue  = gau2_impeg2d_cbp_code[impeg2d_bit_stream_nxt(ps_stream,MB_CBP_LEN)];
484
62.8k
            ps_dec->u2_cbp  = cbpValue & 0xFF;
485
62.8k
            impeg2d_bit_stream_flush(ps_stream,(cbpValue >> 8) & 0x0FF);
486
62.8k
        }
487
149k
        else
488
149k
        {
489
149k
            ps_dec->u2_cbp  = 0;
490
149k
        }
491
212k
    }
492
217k
    return 0;
493
219k
}
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
23.7k
{
507
23.7k
    WORD16 *pi2_vld_out;
508
23.7k
    UWORD32 i;
509
23.7k
    yuv_buf_t *ps_cur_frm_buf      = &ps_dec->s_cur_frm_buf;
510
511
23.7k
    UWORD32 u4_frm_offset          = 0;
512
23.7k
    const dec_mb_params_t *ps_dec_mb_params;
513
23.7k
    IMPEG2D_ERROR_CODES_T e_error   = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
514
515
23.7k
    pi2_vld_out = ps_dec->ai2_vld_buf;
516
23.7k
    memset(ps_dec->ai2_pred_mv,0,sizeof(ps_dec->ai2_pred_mv));
517
518
23.7k
    ps_dec->u2_prev_intra_mb    = 0;
519
23.7k
    ps_dec->u2_first_mb       = 1;
520
521
23.7k
    ps_dec->u2_picture_width = ps_dec->u2_frame_width;
522
523
23.7k
    if(ps_dec->u2_picture_structure != FRAME_PICTURE)
524
1.04k
    {
525
1.04k
        ps_dec->u2_picture_width <<= 1;
526
1.04k
        if(ps_dec->u2_picture_structure == BOTTOM_FIELD)
527
494
        {
528
494
            u4_frm_offset = ps_dec->u2_frame_width;
529
494
        }
530
1.04k
    }
531
532
23.7k
    do
533
8.61M
    {
534
8.61M
        UWORD32 u4_x_offset, u4_y_offset;
535
8.61M
        WORD32 ret;
536
537
538
8.61M
        UWORD32 u4_x_dst_offset = 0;
539
8.61M
        UWORD32 u4_y_dst_offset = 0;
540
8.61M
        UWORD8  *pu1_out_p;
541
8.61M
        UWORD8  *pu1_pred;
542
8.61M
        WORD32 u4_pred_strd;
543
544
8.61M
        IMPEG2D_TRACE_MB_START(ps_dec->u2_mb_x, ps_dec->u2_mb_y);
545
546
8.61M
        if(ps_dec->e_pic_type == B_PIC)
547
220k
            ret = impeg2d_dec_pnb_mb_params(ps_dec);
548
8.39M
        else
549
8.39M
            ret = impeg2d_dec_p_mb_params(ps_dec);
550
551
8.61M
        if(ret)
552
3.18k
            return IMPEG2D_MB_TEX_DECODE_ERR;
553
554
8.61M
        if(0 >= ps_dec->u2_num_mbs_left)
555
386
        {
556
386
            break;
557
386
        }
558
559
8.61M
        IMPEG2D_TRACE_MB_START(ps_dec->u2_mb_x, ps_dec->u2_mb_y);
560
561
8.61M
        u4_x_dst_offset = u4_frm_offset + (ps_dec->u2_mb_x << 4);
562
8.61M
        u4_y_dst_offset = (ps_dec->u2_mb_y << 4) * ps_dec->u2_picture_width;
563
8.61M
        pu1_out_p = ps_cur_frm_buf->pu1_y + u4_x_dst_offset + u4_y_dst_offset;
564
8.61M
        if(ps_dec->u2_prev_intra_mb == 0)
565
8.65M
        {
566
8.65M
            UWORD32 offset_x, offset_y, stride;
567
8.65M
            UWORD16 index = (ps_dec->u2_motion_type);
568
            /*only for non intra mb's*/
569
8.65M
            if(ps_dec->e_mb_pred == BIDIRECT)
570
73.7k
            {
571
73.7k
                ps_dec_mb_params = &ps_dec->ps_func_bi_direct[index];
572
73.7k
            }
573
8.57M
            else
574
8.57M
            {
575
8.57M
                ps_dec_mb_params = &ps_dec->ps_func_forw_or_back[index];
576
8.57M
            }
577
578
8.65M
            stride = ps_dec->u2_picture_width;
579
580
8.65M
            offset_x = u4_frm_offset + (ps_dec->u2_mb_x << 4);
581
582
8.65M
            offset_y = (ps_dec->u2_mb_y << 4);
583
584
8.65M
            ps_dec->s_dest_buf.pu1_y = ps_cur_frm_buf->pu1_y + offset_y * stride + offset_x;
585
586
8.65M
            stride = stride >> 1;
587
588
8.65M
            ps_dec->s_dest_buf.pu1_u = ps_cur_frm_buf->pu1_u + (offset_y >> 1) * stride
589
8.65M
                            + (offset_x >> 1);
590
591
8.65M
            ps_dec->s_dest_buf.pu1_v = ps_cur_frm_buf->pu1_v + (offset_y >> 1) * stride
592
8.65M
                            + (offset_x >> 1);
593
594
8.65M
            PROFILE_DISABLE_MC_IF0
595
8.65M
            ps_dec_mb_params->pf_mc(ps_dec);
596
597
8.65M
        }
598
41.8M
        for(i = 0; i < NUM_LUMA_BLKS; ++i)
599
33.2M
        {
600
33.2M
            if((ps_dec->u2_cbp & (1 << (BLOCKS_IN_MB - 1 - i))) != 0)
601
19.1M
            {
602
19.1M
                e_error = ps_dec->pf_vld_inv_quant(ps_dec, pi2_vld_out, ps_dec->pu1_inv_scan_matrix,
603
19.1M
                              ps_dec->u2_prev_intra_mb, Y_LUMA, 0);
604
19.1M
                if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
605
6.78k
                {
606
6.78k
                    return e_error;
607
6.78k
                }
608
609
19.1M
                u4_x_offset = gai2_impeg2_blk_x_off[i];
610
611
19.1M
                if(ps_dec->u2_field_dct == 0)
612
18.1M
                    u4_y_offset = gai2_impeg2_blk_y_off_frm[i] ;
613
999k
                else
614
999k
                    u4_y_offset = gai2_impeg2_blk_y_off_fld[i] ;
615
616
617
618
619
620
19.1M
                IMPEG2D_IDCT_INP_STATISTICS(pi2_vld_out, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows);
621
622
19.1M
                PROFILE_DISABLE_IDCT_IF0
623
19.1M
                {
624
19.1M
                    WORD32 idx;
625
19.1M
                    if(1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
626
868k
                        idx = 0;
627
18.2M
                    else
628
18.2M
                        idx = 1;
629
630
19.1M
                    if(0 == ps_dec->u2_prev_intra_mb)
631
18.2M
                    {
632
18.2M
                        pu1_pred = pu1_out_p + u4_y_offset * ps_dec->u2_picture_width + u4_x_offset;
633
18.2M
                        u4_pred_strd = ps_dec->u2_picture_width << ps_dec->u2_field_dct;
634
18.2M
                    }
635
944k
                    else
636
944k
                    {
637
944k
                        pu1_pred = (UWORD8 *)gau1_impeg2_zerobuf;
638
944k
                        u4_pred_strd = 8;
639
944k
                    }
640
641
19.1M
                    ps_dec->pf_idct_recon[idx * 2 + ps_dec->i4_last_value_one](pi2_vld_out,
642
19.1M
                                                            ps_dec->ai2_idct_stg1,
643
19.1M
                                                            pu1_pred,
644
19.1M
                                                            pu1_out_p + u4_y_offset * ps_dec->u2_picture_width + u4_x_offset,
645
19.1M
                                                            8,
646
19.1M
                                                            u4_pred_strd,
647
19.1M
                                                            ps_dec->u2_picture_width << ps_dec->u2_field_dct,
648
19.1M
                                                            ~ps_dec->u4_non_zero_cols, ~ps_dec->u4_non_zero_rows);
649
19.1M
                }
650
19.1M
            }
651
652
33.2M
        }
653
654
        /* For U and V blocks, divide the x and y offsets by 2. */
655
8.60M
        u4_x_dst_offset >>= 1;
656
8.60M
        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
8.60M
        if((ps_dec->u2_cbp & 0x02) != 0)
662
43.8k
        {
663
43.8k
            pu1_out_p = ps_cur_frm_buf->pu1_u + u4_x_dst_offset + u4_y_dst_offset;
664
43.8k
            e_error = ps_dec->pf_vld_inv_quant(ps_dec, pi2_vld_out, ps_dec->pu1_inv_scan_matrix,
665
43.8k
                          ps_dec->u2_prev_intra_mb, U_CHROMA, 0);
666
43.8k
            if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
667
1.37k
            {
668
1.37k
                return e_error;
669
1.37k
            }
670
671
672
42.4k
            IMPEG2D_IDCT_INP_STATISTICS(pi2_vld_out, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows);
673
674
42.4k
            PROFILE_DISABLE_IDCT_IF0
675
42.4k
            {
676
42.4k
                WORD32 idx;
677
42.4k
                if(1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
678
20.6k
                    idx = 0;
679
21.8k
                else
680
21.8k
                    idx = 1;
681
682
42.4k
                if(0 == ps_dec->u2_prev_intra_mb)
683
36.8k
                {
684
36.8k
                    pu1_pred = pu1_out_p;
685
36.8k
                    u4_pred_strd = ps_dec->u2_picture_width >> 1;
686
36.8k
                }
687
5.56k
                else
688
5.56k
                {
689
5.56k
                    pu1_pred = (UWORD8 *)gau1_impeg2_zerobuf;
690
5.56k
                    u4_pred_strd = 8;
691
5.56k
                }
692
693
42.4k
                ps_dec->pf_idct_recon[idx * 2 + ps_dec->i4_last_value_one](pi2_vld_out,
694
42.4k
                                                        ps_dec->ai2_idct_stg1,
695
42.4k
                                                        pu1_pred,
696
42.4k
                                                        pu1_out_p,
697
42.4k
                                                        8,
698
42.4k
                                                        u4_pred_strd,
699
42.4k
                                                        ps_dec->u2_picture_width >> 1,
700
42.4k
                                                        ~ps_dec->u4_non_zero_cols, ~ps_dec->u4_non_zero_rows);
701
702
42.4k
            }
703
704
42.4k
        }
705
706
707
8.60M
        if((ps_dec->u2_cbp & 0x01) != 0)
708
769k
        {
709
769k
            pu1_out_p = ps_cur_frm_buf->pu1_v + u4_x_dst_offset + u4_y_dst_offset;
710
769k
            e_error = ps_dec->pf_vld_inv_quant(ps_dec, pi2_vld_out, ps_dec->pu1_inv_scan_matrix,
711
769k
                          ps_dec->u2_prev_intra_mb, V_CHROMA, 0);
712
769k
            if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
713
906
            {
714
906
                return e_error;
715
906
            }
716
717
718
768k
            IMPEG2D_IDCT_INP_STATISTICS(pi2_vld_out, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows);
719
720
768k
            PROFILE_DISABLE_IDCT_IF0
721
768k
            {
722
768k
                WORD32 idx;
723
768k
                if(1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
724
10.7k
                    idx = 0;
725
757k
                else
726
757k
                    idx = 1;
727
768k
                if(0 == ps_dec->u2_prev_intra_mb)
728
762k
                {
729
762k
                    pu1_pred = pu1_out_p;
730
762k
                    u4_pred_strd = ps_dec->u2_picture_width >> 1;
731
762k
                }
732
5.56k
                else
733
5.56k
                {
734
5.56k
                    pu1_pred = (UWORD8 *)gau1_impeg2_zerobuf;
735
5.56k
                    u4_pred_strd = 8;
736
5.56k
                }
737
738
768k
                ps_dec->pf_idct_recon[idx * 2 + ps_dec->i4_last_value_one](pi2_vld_out,
739
768k
                                                        ps_dec->ai2_idct_stg1,
740
768k
                                                        pu1_pred,
741
768k
                                                        pu1_out_p,
742
768k
                                                        8,
743
768k
                                                        u4_pred_strd,
744
768k
                                                        ps_dec->u2_picture_width >> 1,
745
768k
                                                        ~ps_dec->u4_non_zero_cols, ~ps_dec->u4_non_zero_rows);
746
747
768k
            }
748
768k
        }
749
750
8.60M
        ps_dec->u2_num_mbs_left--;
751
8.60M
        ps_dec->u2_first_mb = 0;
752
8.60M
        ps_dec->u2_mb_x++;
753
754
8.60M
        if(ps_dec->s_bit_stream.u4_offset > ps_dec->s_bit_stream.u4_max_offset)
755
890
        {
756
890
            return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
757
890
        }
758
8.60M
        else if (ps_dec->u2_mb_x == ps_dec->u2_num_horiz_mb)
759
74.0k
        {
760
74.0k
            ps_dec->u2_mb_x = 0;
761
74.0k
            ps_dec->u2_mb_y++;
762
763
74.0k
        }
764
8.60M
    }
765
8.61M
    while(ps_dec->u2_num_mbs_left != 0 && impeg2d_bit_stream_nxt(&ps_dec->s_bit_stream,23) != 0x0);
766
10.5k
    return e_error;
767
23.7k
}