Coverage Report

Created: 2026-12-01 06:52

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