Coverage Report

Created: 2025-08-28 06:07

/src/libmpeg2/decoder/impeg2d_pnb_pic.c
Line
Count
Source (jump to first uncovered line)
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2015 The Android Open Source Project
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at:
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 *****************************************************************************
18
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
*/
20
#include <stdio.h>
21
#include <string.h>
22
23
#include "iv_datatypedef.h"
24
#include "iv.h"
25
26
#include "impeg2_buf_mgr.h"
27
#include "impeg2_disp_mgr.h"
28
#include "impeg2_defs.h"
29
#include "impeg2_platform_macros.h"
30
#include "impeg2_inter_pred.h"
31
#include "impeg2_idct.h"
32
#include "impeg2_globals.h"
33
#include "impeg2_mem_func.h"
34
#include "impeg2_format_conv.h"
35
#include "impeg2_macros.h"
36
37
#include "ivd.h"
38
#include "impeg2d.h"
39
#include "impeg2d_bitstream.h"
40
#include "impeg2d_structs.h"
41
#include "impeg2d_vld_tables.h"
42
#include "impeg2d_vld.h"
43
#include "impeg2d_pic_proc.h"
44
#include "impeg2d_debug.h"
45
#include "impeg2d_mc.h"
46
47
#define BLK_SIZE 8
48
#define LUMA_BLK_SIZE (2 * (BLK_SIZE))
49
#define CHROMA_BLK_SIZE (BLK_SIZE)
50
51
52
/*******************************************************************************
53
*
54
*  Function Name   : impeg2d_dec_p_mb_params
55
*
56
*  Description     : Decodes the parameters for P
57
*
58
*  Arguments       :
59
*  dec             : Decoder context
60
*
61
*  Values Returned : None
62
*******************************************************************************/
63
WORD32  impeg2d_dec_p_mb_params(dec_state_t *ps_dec)
64
4.65M
{
65
4.65M
    stream_t *ps_stream = &ps_dec->s_bit_stream;
66
4.65M
    UWORD16 u2_mb_addr_incr;
67
4.65M
    UWORD16 u2_total_len;
68
4.65M
    UWORD16 u2_len;
69
4.65M
    UWORD16 u2_mb_type;
70
4.65M
    UWORD32 u4_next_word;
71
4.65M
    const dec_mb_params_t *ps_dec_mb_params;
72
4.65M
    if(impeg2d_bit_stream_nxt(ps_stream,1) == 1)
73
3.77M
    {
74
3.77M
        impeg2d_bit_stream_flush(ps_stream,1);
75
76
3.77M
    }
77
886k
    else
78
886k
    {
79
886k
        u2_mb_addr_incr = impeg2d_get_mb_addr_incr(ps_stream);
80
81
886k
        if(!u2_mb_addr_incr)
82
291
        {
83
291
            return IV_FAIL;
84
291
        }
85
86
885k
        if(0 == ps_dec->u2_first_mb)
87
869k
        {
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
869k
            if(ps_dec->u2_is_mpeg2 && ((ps_dec->u2_mb_x + u2_mb_addr_incr) > ps_dec->u2_num_horiz_mb) )
111
1.77k
            {
112
1.77k
                u2_mb_addr_incr    = ps_dec->u2_num_horiz_mb - ps_dec->u2_mb_x;
113
1.77k
            }
114
115
869k
            if ((u2_mb_addr_incr - 1) > ps_dec->u2_num_mbs_left)
116
871
            {
117
                /* If the number of skip MBs are more than the number of MBs
118
                 * left, indicate error.
119
                 */
120
871
                return IV_FAIL;
121
871
            }
122
123
868k
            impeg2d_dec_skip_mbs(ps_dec, (UWORD16)(u2_mb_addr_incr - 1));
124
868k
        }
125
16.2k
        else
126
16.2k
        {
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
16.2k
            ps_dec->u2_mb_x = u2_mb_addr_incr - 1;
138
            /* For error resilience */
139
16.2k
            ps_dec->u2_mb_x = MIN(ps_dec->u2_mb_x, (ps_dec->u2_num_horiz_mb - 1));
140
16.2k
            ps_dec->u2_num_mbs_left = ((ps_dec->u2_num_vert_mb - ps_dec->u2_mb_y)
141
16.2k
                            * 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
16.2k
            u2_mb_addr_incr = 1;
149
16.2k
            ps_dec->u2_first_mb = 0;
150
151
16.2k
        }
152
153
885k
    }
154
4.65M
    u4_next_word = (UWORD16)impeg2d_bit_stream_nxt(ps_stream,16);
155
    /*-----------------------------------------------------------------------*/
156
    /* MB type                                                               */
157
    /*-----------------------------------------------------------------------*/
158
4.65M
    {
159
4.65M
        u2_mb_type   = ps_dec->pu2_mb_type[BITS((UWORD16)u4_next_word,15,10)];
160
4.65M
        u2_len      = BITS(u2_mb_type,15,8);
161
4.65M
        u2_total_len = u2_len;
162
4.65M
        u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << u2_len);
163
4.65M
    }
164
    /*-----------------------------------------------------------------------*/
165
    /* motion type                                                           */
166
    /*-----------------------------------------------------------------------*/
167
4.65M
    {
168
4.65M
        if((u2_mb_type & MB_FORW_OR_BACK) &&  ps_dec->u2_read_motion_type)
169
21.3k
        {
170
21.3k
            WORD32 i4_motion_type;
171
21.3k
            ps_dec->u2_motion_type = BITS((UWORD16)u4_next_word,15,14);
172
21.3k
            u2_total_len        += MB_MOTION_TYPE_LEN;
173
21.3k
            u4_next_word        = (UWORD16)LSW((UWORD16)u4_next_word << MB_MOTION_TYPE_LEN);
174
21.3k
            i4_motion_type     = ps_dec->u2_motion_type;
175
176
21.3k
            if((i4_motion_type == 0) ||
177
21.3k
                (i4_motion_type == 4) ||
178
21.3k
                (i4_motion_type >  7))
179
731
            {
180
                //TODO : VANG Check for validity
181
731
                i4_motion_type = 1;
182
731
            }
183
184
21.3k
        }
185
4.65M
    }
186
    /*-----------------------------------------------------------------------*/
187
    /* dct type                                                              */
188
    /*-----------------------------------------------------------------------*/
189
4.65M
    {
190
4.65M
        if((u2_mb_type & MB_CODED) && ps_dec->u2_read_dct_type)
191
6.90k
        {
192
6.90k
            ps_dec->u2_field_dct = BIT((UWORD16)u4_next_word,15);
193
6.90k
            u2_total_len += MB_DCT_TYPE_LEN;
194
6.90k
            u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << MB_DCT_TYPE_LEN);
195
6.90k
        }
196
4.65M
    }
197
    /*-----------------------------------------------------------------------*/
198
    /* Quant scale code                                                      */
199
    /*-----------------------------------------------------------------------*/
200
4.65M
    if(u2_mb_type & MB_QUANT)
201
39.3k
    {
202
39.3k
        UWORD16 u2_quant_scale_code;
203
39.3k
        u2_quant_scale_code = BITS((UWORD16)u4_next_word,15,11);
204
205
39.3k
        ps_dec->u1_quant_scale = (ps_dec->u2_q_scale_type) ?
206
38.5k
            gau1_impeg2_non_linear_quant_scale[u2_quant_scale_code] : (u2_quant_scale_code << 1);
207
39.3k
        u2_total_len += MB_QUANT_SCALE_CODE_LEN;
208
39.3k
    }
209
4.65M
    impeg2d_bit_stream_flush(ps_stream,u2_total_len);
210
    /*-----------------------------------------------------------------------*/
211
    /* Set the function pointers                                             */
212
    /*-----------------------------------------------------------------------*/
213
4.65M
    ps_dec->u2_coded_mb    = (UWORD16)(u2_mb_type & MB_CODED);
214
215
4.65M
    if(u2_mb_type & MB_FORW_OR_BACK)
216
4.05M
    {
217
218
4.05M
        UWORD16 refPic      = !(u2_mb_type & MB_MV_FORW);
219
4.05M
        UWORD16 index       = (ps_dec->u2_motion_type);
220
4.05M
        ps_dec->u2_prev_intra_mb    = 0;
221
4.05M
        ps_dec->e_mb_pred         = (e_pred_direction_t)refPic;
222
4.05M
        ps_dec_mb_params = &ps_dec->ps_func_forw_or_back[index];
223
4.05M
        ps_dec->s_mb_type = ps_dec_mb_params->s_mb_type;
224
4.05M
        if(NULL == ps_dec_mb_params->pf_func_mb_params)
225
0
            return -1;
226
4.05M
        ps_dec_mb_params->pf_func_mb_params(ps_dec);
227
228
4.05M
    }
229
604k
    else if(u2_mb_type & MB_TYPE_INTRA)
230
6.73k
    {
231
6.73k
        ps_dec->u2_prev_intra_mb    = 1;
232
6.73k
        impeg2d_dec_intra_mb(ps_dec);
233
234
6.73k
    }
235
597k
    else
236
597k
    {
237
597k
        ps_dec->u2_prev_intra_mb    = 0;
238
597k
        ps_dec->e_mb_pred = FORW;
239
597k
        ps_dec->u2_motion_type = 0;
240
597k
        impeg2d_dec_0mv_coded_mb(ps_dec);
241
597k
    }
242
243
    /*-----------------------------------------------------------------------*/
244
    /* decode cbp                                                            */
245
    /*-----------------------------------------------------------------------*/
246
4.65M
    if((u2_mb_type & MB_TYPE_INTRA))
247
6.73k
    {
248
6.73k
        ps_dec->u2_cbp  = 0x3f;
249
6.73k
        ps_dec->u2_prev_intra_mb    = 1;
250
6.73k
    }
251
4.64M
    else
252
4.64M
    {
253
4.64M
        ps_dec->u2_prev_intra_mb  = 0;
254
4.64M
        ps_dec->u2_def_dc_pred[Y_LUMA] = 128 << ps_dec->u2_intra_dc_precision;
255
4.64M
        ps_dec->u2_def_dc_pred[U_CHROMA] = 128 << ps_dec->u2_intra_dc_precision;
256
4.64M
        ps_dec->u2_def_dc_pred[V_CHROMA] = 128 << ps_dec->u2_intra_dc_precision;
257
4.64M
        if((ps_dec->u2_coded_mb))
258
4.17M
        {
259
4.17M
            UWORD16 cbpValue;
260
4.17M
            cbpValue  = gau2_impeg2d_cbp_code[impeg2d_bit_stream_nxt(ps_stream,MB_CBP_LEN)];
261
4.17M
            ps_dec->u2_cbp  = cbpValue & 0xFF;
262
4.17M
            impeg2d_bit_stream_flush(ps_stream,(cbpValue >> 8) & 0x0FF);
263
4.17M
        }
264
472k
        else
265
472k
        {
266
472k
            ps_dec->u2_cbp  = 0;
267
472k
        }
268
4.64M
    }
269
4.65M
    return 0;
270
4.65M
}
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
213k
{
286
213k
    stream_t *ps_stream = &ps_dec->s_bit_stream;
287
213k
    UWORD16 u2_mb_addr_incr;
288
213k
    UWORD16 u2_total_len;
289
213k
    UWORD16 u2_len;
290
213k
    UWORD16 u2_mb_type;
291
213k
    UWORD32 u4_next_word;
292
213k
    const dec_mb_params_t *ps_dec_mb_params;
293
213k
    if(impeg2d_bit_stream_nxt(ps_stream,1) == 1)
294
101k
    {
295
101k
        impeg2d_bit_stream_flush(ps_stream,1);
296
297
101k
    }
298
112k
    else
299
112k
    {
300
112k
        u2_mb_addr_incr = impeg2d_get_mb_addr_incr(ps_stream);
301
302
112k
        if(0 == u2_mb_addr_incr)
303
229
        {
304
229
            return IV_FAIL;
305
229
        }
306
307
111k
        if(ps_dec->u2_first_mb)
308
6.54k
        {
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
6.54k
            ps_dec->u2_mb_x = u2_mb_addr_incr - 1;
319
            /* For error resilience */
320
6.54k
            ps_dec->u2_mb_x = MIN(ps_dec->u2_mb_x, (ps_dec->u2_num_horiz_mb - 1));
321
6.54k
            ps_dec->u2_num_mbs_left = ((ps_dec->u2_num_vert_mb - ps_dec->u2_mb_y)
322
6.54k
                            * 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
6.54k
            u2_mb_addr_incr = 1;
330
6.54k
            ps_dec->u2_first_mb = 0;
331
6.54k
        }
332
105k
        else
333
105k
        {
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
105k
            if(ps_dec->u2_is_mpeg2 &&
343
105k
                ((ps_dec->u2_mb_x + u2_mb_addr_incr) > ps_dec->u2_num_horiz_mb))
344
15.5k
            {
345
15.5k
                u2_mb_addr_incr    = ps_dec->u2_num_horiz_mb - ps_dec->u2_mb_x;
346
15.5k
            }
347
348
105k
            if ((u2_mb_addr_incr - 1) > ps_dec->u2_num_mbs_left)
349
247
            {
350
                /* If the number of skip MBs are more than the number of MBs
351
                 * left, indicate error.
352
                 */
353
247
                return IV_FAIL;
354
247
            }
355
356
104k
            impeg2d_dec_skip_mbs(ps_dec, (UWORD16)(u2_mb_addr_incr - 1));
357
104k
        }
358
359
111k
    }
360
212k
    u4_next_word = (UWORD16)impeg2d_bit_stream_nxt(ps_stream,16);
361
    /*-----------------------------------------------------------------------*/
362
    /* MB type                                                               */
363
    /*-----------------------------------------------------------------------*/
364
212k
    {
365
212k
        u2_mb_type   = ps_dec->pu2_mb_type[BITS((UWORD16)u4_next_word,15,10)];
366
212k
        u2_len      = BITS(u2_mb_type,15,8);
367
212k
        u2_total_len = u2_len;
368
212k
        u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << u2_len);
369
212k
    }
370
    /*-----------------------------------------------------------------------*/
371
    /* motion type                                                           */
372
    /*-----------------------------------------------------------------------*/
373
212k
    {
374
212k
        WORD32 i4_motion_type = ps_dec->u2_motion_type;
375
376
212k
        if((u2_mb_type & MB_FORW_OR_BACK) &&  ps_dec->u2_read_motion_type)
377
179k
        {
378
179k
            ps_dec->u2_motion_type = BITS((UWORD16)u4_next_word,15,14);
379
179k
            u2_total_len += MB_MOTION_TYPE_LEN;
380
179k
            u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << MB_MOTION_TYPE_LEN);
381
179k
            i4_motion_type     = ps_dec->u2_motion_type;
382
383
179k
        }
384
385
386
212k
        if ((u2_mb_type & MB_FORW_OR_BACK) &&
387
212k
            ((i4_motion_type == 0) ||
388
193k
            (i4_motion_type == 3) ||
389
193k
            (i4_motion_type == 4) ||
390
193k
            (i4_motion_type >= 7)))
391
47.2k
        {
392
            //TODO: VANG Check for validity
393
47.2k
            i4_motion_type = 1;
394
47.2k
        }
395
396
212k
    }
397
    /*-----------------------------------------------------------------------*/
398
    /* dct type                                                              */
399
    /*-----------------------------------------------------------------------*/
400
212k
    {
401
212k
        if((u2_mb_type & MB_CODED) && ps_dec->u2_read_dct_type)
402
56.3k
        {
403
56.3k
            ps_dec->u2_field_dct = BIT((UWORD16)u4_next_word,15);
404
56.3k
            u2_total_len += MB_DCT_TYPE_LEN;
405
56.3k
            u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << MB_DCT_TYPE_LEN);
406
56.3k
        }
407
212k
    }
408
    /*-----------------------------------------------------------------------*/
409
    /* Quant scale code                                                      */
410
    /*-----------------------------------------------------------------------*/
411
212k
    if(u2_mb_type & MB_QUANT)
412
28.9k
    {
413
28.9k
        UWORD16 u2_quant_scale_code;
414
28.9k
        u2_quant_scale_code = BITS((UWORD16)u4_next_word,15,11);
415
416
28.9k
        ps_dec->u1_quant_scale = (ps_dec->u2_q_scale_type) ?
417
14.8k
            gau1_impeg2_non_linear_quant_scale[u2_quant_scale_code] : (u2_quant_scale_code << 1);
418
28.9k
        u2_total_len += MB_QUANT_SCALE_CODE_LEN;
419
28.9k
    }
420
212k
    impeg2d_bit_stream_flush(ps_stream,u2_total_len);
421
    /*-----------------------------------------------------------------------*/
422
    /* Set the function pointers                                             */
423
    /*-----------------------------------------------------------------------*/
424
212k
    ps_dec->u2_coded_mb    = (UWORD16)(u2_mb_type & MB_CODED);
425
426
212k
    if(u2_mb_type & MB_BIDRECT)
427
74.9k
    {
428
74.9k
        UWORD16 u2_index       = (ps_dec->u2_motion_type);
429
430
74.9k
        ps_dec->u2_prev_intra_mb    = 0;
431
74.9k
        ps_dec->e_mb_pred         = BIDIRECT;
432
74.9k
        ps_dec_mb_params = &ps_dec->ps_func_bi_direct[u2_index];
433
74.9k
        ps_dec->s_mb_type = ps_dec_mb_params->s_mb_type;
434
74.9k
        if(NULL == ps_dec_mb_params->pf_func_mb_params)
435
2.45k
            return -1;
436
72.4k
        ps_dec_mb_params->pf_func_mb_params(ps_dec);
437
72.4k
    }
438
137k
    else if(u2_mb_type & MB_FORW_OR_BACK)
439
120k
    {
440
441
120k
        UWORD16 u2_refPic      = !(u2_mb_type & MB_MV_FORW);
442
120k
        UWORD16 u2_index       = (ps_dec->u2_motion_type);
443
120k
        ps_dec->u2_prev_intra_mb    = 0;
444
120k
        ps_dec->e_mb_pred         = (e_pred_direction_t)u2_refPic;
445
120k
        ps_dec_mb_params = &ps_dec->ps_func_forw_or_back[u2_index];
446
120k
        ps_dec->s_mb_type = ps_dec_mb_params->s_mb_type;
447
120k
        if(NULL == ps_dec_mb_params->pf_func_mb_params)
448
0
            return -1;
449
120k
        ps_dec_mb_params->pf_func_mb_params(ps_dec);
450
451
120k
    }
452
17.7k
    else if(u2_mb_type & MB_TYPE_INTRA)
453
5.72k
    {
454
5.72k
        ps_dec->u2_prev_intra_mb    = 1;
455
5.72k
        impeg2d_dec_intra_mb(ps_dec);
456
457
5.72k
    }
458
12.0k
    else
459
12.0k
    {
460
12.0k
        ps_dec->u2_prev_intra_mb =0;
461
12.0k
        ps_dec->e_mb_pred = FORW;
462
12.0k
        ps_dec->u2_motion_type = 0;
463
12.0k
        impeg2d_dec_0mv_coded_mb(ps_dec);
464
12.0k
    }
465
466
    /*-----------------------------------------------------------------------*/
467
    /* decode cbp                                                            */
468
    /*-----------------------------------------------------------------------*/
469
210k
    if((u2_mb_type & MB_TYPE_INTRA))
470
5.73k
    {
471
5.73k
        ps_dec->u2_cbp  = 0x3f;
472
5.73k
        ps_dec->u2_prev_intra_mb    = 1;
473
5.73k
    }
474
204k
    else
475
204k
    {
476
204k
        ps_dec->u2_prev_intra_mb  = 0;
477
204k
        ps_dec->u2_def_dc_pred[Y_LUMA] = 128 << ps_dec->u2_intra_dc_precision;
478
204k
        ps_dec->u2_def_dc_pred[U_CHROMA] = 128 << ps_dec->u2_intra_dc_precision;
479
204k
        ps_dec->u2_def_dc_pred[V_CHROMA] = 128 << ps_dec->u2_intra_dc_precision;
480
204k
        if((ps_dec->u2_coded_mb))
481
64.0k
        {
482
64.0k
            UWORD16 cbpValue;
483
64.0k
            cbpValue  = gau2_impeg2d_cbp_code[impeg2d_bit_stream_nxt(ps_stream,MB_CBP_LEN)];
484
64.0k
            ps_dec->u2_cbp  = cbpValue & 0xFF;
485
64.0k
            impeg2d_bit_stream_flush(ps_stream,(cbpValue >> 8) & 0x0FF);
486
64.0k
        }
487
140k
        else
488
140k
        {
489
140k
            ps_dec->u2_cbp  = 0;
490
140k
        }
491
204k
    }
492
210k
    return 0;
493
212k
}
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
24.5k
{
507
24.5k
    WORD16 *pi2_vld_out;
508
24.5k
    UWORD32 i;
509
24.5k
    yuv_buf_t *ps_cur_frm_buf      = &ps_dec->s_cur_frm_buf;
510
511
24.5k
    UWORD32 u4_frm_offset          = 0;
512
24.5k
    const dec_mb_params_t *ps_dec_mb_params;
513
24.5k
    IMPEG2D_ERROR_CODES_T e_error   = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
514
515
24.5k
    pi2_vld_out = ps_dec->ai2_vld_buf;
516
24.5k
    memset(ps_dec->ai2_pred_mv,0,sizeof(ps_dec->ai2_pred_mv));
517
518
24.5k
    ps_dec->u2_prev_intra_mb    = 0;
519
24.5k
    ps_dec->u2_first_mb       = 1;
520
521
24.5k
    ps_dec->u2_picture_width = ps_dec->u2_frame_width;
522
523
24.5k
    if(ps_dec->u2_picture_structure != FRAME_PICTURE)
524
892
    {
525
892
        ps_dec->u2_picture_width <<= 1;
526
892
        if(ps_dec->u2_picture_structure == BOTTOM_FIELD)
527
450
        {
528
450
            u4_frm_offset = ps_dec->u2_frame_width;
529
450
        }
530
892
    }
531
532
24.5k
    do
533
4.87M
    {
534
4.87M
        UWORD32 u4_x_offset, u4_y_offset;
535
4.87M
        WORD32 ret;
536
537
538
4.87M
        UWORD32 u4_x_dst_offset = 0;
539
4.87M
        UWORD32 u4_y_dst_offset = 0;
540
4.87M
        UWORD8  *pu1_out_p;
541
4.87M
        UWORD8  *pu1_pred;
542
4.87M
        WORD32 u4_pred_strd;
543
544
4.87M
        IMPEG2D_TRACE_MB_START(ps_dec->u2_mb_x, ps_dec->u2_mb_y);
545
546
4.87M
        if(ps_dec->e_pic_type == B_PIC)
547
213k
            ret = impeg2d_dec_pnb_mb_params(ps_dec);
548
4.66M
        else
549
4.66M
            ret = impeg2d_dec_p_mb_params(ps_dec);
550
551
4.87M
        if(ret)
552
4.08k
            return IMPEG2D_MB_TEX_DECODE_ERR;
553
554
4.87M
        if(0 >= ps_dec->u2_num_mbs_left)
555
378
        {
556
378
            break;
557
378
        }
558
559
4.87M
        IMPEG2D_TRACE_MB_START(ps_dec->u2_mb_x, ps_dec->u2_mb_y);
560
561
4.87M
        u4_x_dst_offset = u4_frm_offset + (ps_dec->u2_mb_x << 4);
562
4.87M
        u4_y_dst_offset = (ps_dec->u2_mb_y << 4) * ps_dec->u2_picture_width;
563
4.87M
        pu1_out_p = ps_cur_frm_buf->pu1_y + u4_x_dst_offset + u4_y_dst_offset;
564
4.87M
        if(ps_dec->u2_prev_intra_mb == 0)
565
4.89M
        {
566
4.89M
            UWORD32 offset_x, offset_y, stride;
567
4.89M
            UWORD16 index = (ps_dec->u2_motion_type);
568
            /*only for non intra mb's*/
569
4.89M
            if(ps_dec->e_mb_pred == BIDIRECT)
570
72.9k
            {
571
72.9k
                ps_dec_mb_params = &ps_dec->ps_func_bi_direct[index];
572
72.9k
            }
573
4.82M
            else
574
4.82M
            {
575
4.82M
                ps_dec_mb_params = &ps_dec->ps_func_forw_or_back[index];
576
4.82M
            }
577
578
4.89M
            stride = ps_dec->u2_picture_width;
579
580
4.89M
            offset_x = u4_frm_offset + (ps_dec->u2_mb_x << 4);
581
582
4.89M
            offset_y = (ps_dec->u2_mb_y << 4);
583
584
4.89M
            ps_dec->s_dest_buf.pu1_y = ps_cur_frm_buf->pu1_y + offset_y * stride + offset_x;
585
586
4.89M
            stride = stride >> 1;
587
588
4.89M
            ps_dec->s_dest_buf.pu1_u = ps_cur_frm_buf->pu1_u + (offset_y >> 1) * stride
589
4.89M
                            + (offset_x >> 1);
590
591
4.89M
            ps_dec->s_dest_buf.pu1_v = ps_cur_frm_buf->pu1_v + (offset_y >> 1) * stride
592
4.89M
                            + (offset_x >> 1);
593
594
4.89M
            PROFILE_DISABLE_MC_IF0
595
4.89M
            ps_dec_mb_params->pf_mc(ps_dec);
596
597
4.89M
        }
598
23.5M
        for(i = 0; i < NUM_LUMA_BLKS; ++i)
599
18.6M
        {
600
18.6M
            if((ps_dec->u2_cbp & (1 << (BLOCKS_IN_MB - 1 - i))) != 0)
601
9.65M
            {
602
9.65M
                e_error = ps_dec->pf_vld_inv_quant(ps_dec, pi2_vld_out, ps_dec->pu1_inv_scan_matrix,
603
9.65M
                              ps_dec->u2_prev_intra_mb, Y_LUMA, 0);
604
9.65M
                if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
605
6.82k
                {
606
6.82k
                    return e_error;
607
6.82k
                }
608
609
9.65M
                u4_x_offset = gai2_impeg2_blk_x_off[i];
610
611
9.65M
                if(ps_dec->u2_field_dct == 0)
612
9.19M
                    u4_y_offset = gai2_impeg2_blk_y_off_frm[i] ;
613
459k
                else
614
459k
                    u4_y_offset = gai2_impeg2_blk_y_off_fld[i] ;
615
616
617
618
619
620
9.65M
                IMPEG2D_IDCT_INP_STATISTICS(pi2_vld_out, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows);
621
622
9.65M
                PROFILE_DISABLE_IDCT_IF0
623
9.65M
                {
624
9.65M
                    WORD32 idx;
625
9.65M
                    if(1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
626
464k
                        idx = 0;
627
9.18M
                    else
628
9.18M
                        idx = 1;
629
630
9.65M
                    if(0 == ps_dec->u2_prev_intra_mb)
631
9.23M
                    {
632
9.23M
                        pu1_pred = pu1_out_p + u4_y_offset * ps_dec->u2_picture_width + u4_x_offset;
633
9.23M
                        u4_pred_strd = ps_dec->u2_picture_width << ps_dec->u2_field_dct;
634
9.23M
                    }
635
418k
                    else
636
418k
                    {
637
418k
                        pu1_pred = (UWORD8 *)gau1_impeg2_zerobuf;
638
418k
                        u4_pred_strd = 8;
639
418k
                    }
640
641
9.65M
                    ps_dec->pf_idct_recon[idx * 2 + ps_dec->i4_last_value_one](pi2_vld_out,
642
9.65M
                                                            ps_dec->ai2_idct_stg1,
643
9.65M
                                                            pu1_pred,
644
9.65M
                                                            pu1_out_p + u4_y_offset * ps_dec->u2_picture_width + u4_x_offset,
645
9.65M
                                                            8,
646
9.65M
                                                            u4_pred_strd,
647
9.65M
                                                            ps_dec->u2_picture_width << ps_dec->u2_field_dct,
648
9.65M
                                                            ~ps_dec->u4_non_zero_cols, ~ps_dec->u4_non_zero_rows);
649
9.65M
                }
650
9.65M
            }
651
652
18.6M
        }
653
654
        /* For U and V blocks, divide the x and y offsets by 2. */
655
4.86M
        u4_x_dst_offset >>= 1;
656
4.86M
        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
4.86M
        if((ps_dec->u2_cbp & 0x02) != 0)
662
53.0k
        {
663
53.0k
            pu1_out_p = ps_cur_frm_buf->pu1_u + u4_x_dst_offset + u4_y_dst_offset;
664
53.0k
            e_error = ps_dec->pf_vld_inv_quant(ps_dec, pi2_vld_out, ps_dec->pu1_inv_scan_matrix,
665
53.0k
                          ps_dec->u2_prev_intra_mb, U_CHROMA, 0);
666
53.0k
            if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
667
1.11k
            {
668
1.11k
                return e_error;
669
1.11k
            }
670
671
672
51.8k
            IMPEG2D_IDCT_INP_STATISTICS(pi2_vld_out, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows);
673
674
51.8k
            PROFILE_DISABLE_IDCT_IF0
675
51.8k
            {
676
51.8k
                WORD32 idx;
677
51.8k
                if(1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
678
28.5k
                    idx = 0;
679
23.3k
                else
680
23.3k
                    idx = 1;
681
682
51.8k
                if(0 == ps_dec->u2_prev_intra_mb)
683
41.7k
                {
684
41.7k
                    pu1_pred = pu1_out_p;
685
41.7k
                    u4_pred_strd = ps_dec->u2_picture_width >> 1;
686
41.7k
                }
687
10.1k
                else
688
10.1k
                {
689
10.1k
                    pu1_pred = (UWORD8 *)gau1_impeg2_zerobuf;
690
10.1k
                    u4_pred_strd = 8;
691
10.1k
                }
692
693
51.8k
                ps_dec->pf_idct_recon[idx * 2 + ps_dec->i4_last_value_one](pi2_vld_out,
694
51.8k
                                                        ps_dec->ai2_idct_stg1,
695
51.8k
                                                        pu1_pred,
696
51.8k
                                                        pu1_out_p,
697
51.8k
                                                        8,
698
51.8k
                                                        u4_pred_strd,
699
51.8k
                                                        ps_dec->u2_picture_width >> 1,
700
51.8k
                                                        ~ps_dec->u4_non_zero_cols, ~ps_dec->u4_non_zero_rows);
701
702
51.8k
            }
703
704
51.8k
        }
705
706
707
4.86M
        if((ps_dec->u2_cbp & 0x01) != 0)
708
433k
        {
709
433k
            pu1_out_p = ps_cur_frm_buf->pu1_v + u4_x_dst_offset + u4_y_dst_offset;
710
433k
            e_error = ps_dec->pf_vld_inv_quant(ps_dec, pi2_vld_out, ps_dec->pu1_inv_scan_matrix,
711
433k
                          ps_dec->u2_prev_intra_mb, V_CHROMA, 0);
712
433k
            if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
713
791
            {
714
791
                return e_error;
715
791
            }
716
717
718
432k
            IMPEG2D_IDCT_INP_STATISTICS(pi2_vld_out, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows);
719
720
432k
            PROFILE_DISABLE_IDCT_IF0
721
432k
            {
722
432k
                WORD32 idx;
723
432k
                if(1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
724
13.3k
                    idx = 0;
725
419k
                else
726
419k
                    idx = 1;
727
432k
                if(0 == ps_dec->u2_prev_intra_mb)
728
422k
                {
729
422k
                    pu1_pred = pu1_out_p;
730
422k
                    u4_pred_strd = ps_dec->u2_picture_width >> 1;
731
422k
                }
732
9.93k
                else
733
9.93k
                {
734
9.93k
                    pu1_pred = (UWORD8 *)gau1_impeg2_zerobuf;
735
9.93k
                    u4_pred_strd = 8;
736
9.93k
                }
737
738
432k
                ps_dec->pf_idct_recon[idx * 2 + ps_dec->i4_last_value_one](pi2_vld_out,
739
432k
                                                        ps_dec->ai2_idct_stg1,
740
432k
                                                        pu1_pred,
741
432k
                                                        pu1_out_p,
742
432k
                                                        8,
743
432k
                                                        u4_pred_strd,
744
432k
                                                        ps_dec->u2_picture_width >> 1,
745
432k
                                                        ~ps_dec->u4_non_zero_cols, ~ps_dec->u4_non_zero_rows);
746
747
432k
            }
748
432k
        }
749
750
4.86M
        ps_dec->u2_num_mbs_left--;
751
4.86M
        ps_dec->u2_first_mb = 0;
752
4.86M
        ps_dec->u2_mb_x++;
753
754
4.86M
        if(ps_dec->s_bit_stream.u4_offset > ps_dec->s_bit_stream.u4_max_offset)
755
797
        {
756
797
            return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
757
797
        }
758
4.86M
        else if (ps_dec->u2_mb_x == ps_dec->u2_num_horiz_mb)
759
62.9k
        {
760
62.9k
            ps_dec->u2_mb_x = 0;
761
62.9k
            ps_dec->u2_mb_y++;
762
763
62.9k
        }
764
4.86M
    }
765
4.87M
    while(ps_dec->u2_num_mbs_left != 0 && impeg2d_bit_stream_nxt(&ps_dec->s_bit_stream,23) != 0x0);
766
10.8k
    return e_error;
767
24.5k
}