summaryrefslogtreecommitdiffstats
path: root/decoder/impeg2d_pnb_pic.c
blob: a3ae436cd9d6991f66393c9ffbaa3e9ab52ac99b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/******************************************************************************
 *
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
#include <stdio.h>
#include <string.h>

#include "iv_datatypedef.h"
#include "iv.h"

#include "impeg2_buf_mgr.h"
#include "impeg2_disp_mgr.h"
#include "impeg2_defs.h"
#include "impeg2_platform_macros.h"
#include "impeg2_inter_pred.h"
#include "impeg2_idct.h"
#include "impeg2_globals.h"
#include "impeg2_mem_func.h"
#include "impeg2_format_conv.h"
#include "impeg2_macros.h"

#include "ivd.h"
#include "impeg2d.h"
#include "impeg2d_bitstream.h"
#include "impeg2d_structs.h"
#include "impeg2d_vld_tables.h"
#include "impeg2d_vld.h"
#include "impeg2d_pic_proc.h"
#include "impeg2d_debug.h"
#include "impeg2d_mc.h"

#define BLK_SIZE 8
#define LUMA_BLK_SIZE (2 * (BLK_SIZE))
#define CHROMA_BLK_SIZE (BLK_SIZE)


/*******************************************************************************
*
*  Function Name   : impeg2d_dec_p_mb_params
*
*  Description     : Decodes the parameters for P
*
*  Arguments       :
*  dec             : Decoder context
*
*  Values Returned : None
*******************************************************************************/
WORD32  impeg2d_dec_p_mb_params(dec_state_t *ps_dec)
{
    stream_t *ps_stream = &ps_dec->s_bit_stream;
    UWORD16 u2_mb_addr_incr;
    UWORD16 u2_total_len;
    UWORD16 u2_len;
    UWORD16 u2_mb_type;
    UWORD32 u4_next_word;
    const dec_mb_params_t *ps_dec_mb_params;
    if(impeg2d_bit_stream_nxt(ps_stream,1) == 1)
    {
        impeg2d_bit_stream_flush(ps_stream,1);

    }
    else
    {
        u2_mb_addr_incr = impeg2d_get_mb_addr_incr(ps_stream);

        if(!u2_mb_addr_incr)
        {
            return IV_FAIL;
        }

        if(0 == ps_dec->u2_first_mb)
        {
            /****************************************************************/
            /* If the 2nd member of a field picture pair is a P picture and */
            /* the first one was an I picture, there cannot be any skipped  */
            /* MBs in the second field picture                              */
            /****************************************************************/
            /*
            if((dec->picture_structure != FRAME_PICTURE) &&
                (dec->f->FieldFuncCall != 0) &&
                (dec->las->u1_last_coded_vop_type == I))
            {
                core0_err_handler((void *)(VOLParams),
                    ITTMPEG2_ERR_INVALID_MB_SKIP);
            }
            */
            /****************************************************************/
            /* In MPEG-2, the last MB of the row cannot be skipped and the  */
            /* MBAddrIncr cannot be such that it will take the current MB   */
            /* beyond the current row                                       */
            /* In MPEG-1, the slice could start and end anywhere and is not */
            /* restricted to a row like in MPEG-2. Hence this check should  */
            /* not be done for MPEG-1 streams.                              */
            /****************************************************************/
            if(ps_dec->u2_is_mpeg2 && ((ps_dec->u2_mb_x + u2_mb_addr_incr) > ps_dec->u2_num_horiz_mb) )
            {
                u2_mb_addr_incr    = ps_dec->u2_num_horiz_mb - ps_dec->u2_mb_x;
            }

            if ((u2_mb_addr_incr - 1) > ps_dec->u2_num_mbs_left)
            {
                /* If the number of skip MBs are more than the number of MBs
                 * left, indicate error.
                 */
                return IV_FAIL;
            }

            impeg2d_dec_skip_mbs(ps_dec, (UWORD16)(u2_mb_addr_incr - 1));
        }
        else
        {

            /****************************************************************/
            /* Section 6.3.17                                               */
            /* The first MB of a slice cannot be skipped                    */
            /* But the mb_addr_incr can be > 1, because at the beginning of */
            /* a slice, it indicates the offset from the last MB in the     */
            /* previous row. Hence for the first slice in a row, the        */
            /* mb_addr_incr needs to be 1.                                  */
            /****************************************************************/
            /* MB_x is set to zero whenever MB_y changes.                   */
            ps_dec->u2_mb_x = u2_mb_addr_incr - 1;
            /* For error resilience */
            ps_dec->u2_mb_x = MIN(ps_dec->u2_mb_x, (ps_dec->u2_num_horiz_mb - 1));
            ps_dec->u2_num_mbs_left = ((ps_dec->u2_num_vert_mb - ps_dec->u2_mb_y)
                            * ps_dec->u2_num_horiz_mb) - ps_dec->u2_mb_x;

            /****************************************************************/
            /* mb_addr_incr is forced to 1 because in this decoder it is used */
            /* more as an indicator of the number of MBs skipped than the   */
            /* as defined by the standard (Section 6.3.17)                  */
            /****************************************************************/
            u2_mb_addr_incr = 1;
            ps_dec->u2_first_mb = 0;

        }

    }
    u4_next_word = (UWORD16)impeg2d_bit_stream_nxt(ps_stream,16);
    /*-----------------------------------------------------------------------*/
    /* MB type                                                               */
    /*-----------------------------------------------------------------------*/
    {
        u2_mb_type   = ps_dec->pu2_mb_type[BITS((UWORD16)u4_next_word,15,10)];
        u2_len      = BITS(u2_mb_type,15,8);
        u2_total_len = u2_len;
        u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << u2_len);
    }
    /*-----------------------------------------------------------------------*/
    /* motion type                                                           */
    /*-----------------------------------------------------------------------*/
    {
        if((u2_mb_type & MB_FORW_OR_BACK) &&  ps_dec->u2_read_motion_type)
        {
            WORD32 i4_motion_type;
            ps_dec->u2_motion_type = BITS((UWORD16)u4_next_word,15,14);
            u2_total_len        += MB_MOTION_TYPE_LEN;
            u4_next_word        = (UWORD16)LSW((UWORD16)u4_next_word << MB_MOTION_TYPE_LEN);
            i4_motion_type     = ps_dec->u2_motion_type;

            if((i4_motion_type == 0) ||
                (i4_motion_type == 4) ||
                (i4_motion_type >  7))
            {
                //TODO : VANG Check for validity
                i4_motion_type = 1;
            }

        }
    }
    /*-----------------------------------------------------------------------*/
    /* dct type                                                              */
    /*-----------------------------------------------------------------------*/
    {
        if((u2_mb_type & MB_CODED) && ps_dec->u2_read_dct_type)
        {
            ps_dec->u2_field_dct = BIT((UWORD16)u4_next_word,15);
            u2_total_len += MB_DCT_TYPE_LEN;
            u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << MB_DCT_TYPE_LEN);
        }
    }
    /*-----------------------------------------------------------------------*/
    /* Quant scale code                                                      */
    /*-----------------------------------------------------------------------*/
    if(u2_mb_type & MB_QUANT)
    {
        UWORD16 u2_quant_scale_code;
        u2_quant_scale_code = BITS((UWORD16)u4_next_word,15,11);

        ps_dec->u1_quant_scale = (ps_dec->u2_q_scale_type) ?
            gau1_impeg2_non_linear_quant_scale[u2_quant_scale_code] : (u2_quant_scale_code << 1);
        u2_total_len += MB_QUANT_SCALE_CODE_LEN;
    }
    impeg2d_bit_stream_flush(ps_stream,u2_total_len);
    /*-----------------------------------------------------------------------*/
    /* Set the function pointers                                             */
    /*-----------------------------------------------------------------------*/
    ps_dec->u2_coded_mb    = (UWORD16)(u2_mb_type & MB_CODED);

    if(u2_mb_type & MB_FORW_OR_BACK)
    {

        UWORD16 refPic      = !(u2_mb_type & MB_MV_FORW);
        UWORD16 index       = (ps_dec->u2_motion_type);
        ps_dec->u2_prev_intra_mb    = 0;
        ps_dec->e_mb_pred         = (e_pred_direction_t)refPic;
        ps_dec_mb_params = &ps_dec->ps_func_forw_or_back[index];
        ps_dec->s_mb_type = ps_dec_mb_params->s_mb_type;
        if(NULL == ps_dec_mb_params->pf_func_mb_params)
            return -1;
        ps_dec_mb_params->pf_func_mb_params(ps_dec);

    }
    else if(u2_mb_type & MB_TYPE_INTRA)
    {
        ps_dec->u2_prev_intra_mb    = 1;
        impeg2d_dec_intra_mb(ps_dec);

    }
    else
    {
        ps_dec->u2_prev_intra_mb    = 0;
        ps_dec->e_mb_pred = FORW;
        ps_dec->u2_motion_type = 0;
        impeg2d_dec_0mv_coded_mb(ps_dec);
    }

    /*-----------------------------------------------------------------------*/
    /* decode cbp                                                            */
    /*-----------------------------------------------------------------------*/
    if((u2_mb_type & MB_TYPE_INTRA))
    {
        ps_dec->u2_cbp  = 0x3f;
        ps_dec->u2_prev_intra_mb    = 1;
    }
    else
    {
        ps_dec->u2_prev_intra_mb  = 0;
        ps_dec->u2_def_dc_pred[Y_LUMA] = 128 << ps_dec->u2_intra_dc_precision;
        ps_dec->u2_def_dc_pred[U_CHROMA] = 128 << ps_dec->u2_intra_dc_precision;
        ps_dec->u2_def_dc_pred[V_CHROMA] = 128 << ps_dec->u2_intra_dc_precision;
        if((ps_dec->u2_coded_mb))
        {
            UWORD16 cbpValue;
            cbpValue  = gau2_impeg2d_cbp_code[impeg2d_bit_stream_nxt(ps_stream,MB_CBP_LEN)];
            ps_dec->u2_cbp  = cbpValue & 0xFF;
            impeg2d_bit_stream_flush(ps_stream,(cbpValue >> 8) & 0x0FF);
        }
        else
        {
            ps_dec->u2_cbp  = 0;
        }
    }
    return 0;
}


/*******************************************************************************
*
*  Function Name   : impeg2d_dec_pnb_mb_params
*
*  Description     : Decodes the parameters for P and B pictures
*
*  Arguments       :
*  dec             : Decoder context
*
*  Values Returned : None
*******************************************************************************/
WORD32 impeg2d_dec_pnb_mb_params(dec_state_t *ps_dec)
{
    stream_t *ps_stream = &ps_dec->s_bit_stream;
    UWORD16 u2_mb_addr_incr;
    UWORD16 u2_total_len;
    UWORD16 u2_len;
    UWORD16 u2_mb_type;
    UWORD32 u4_next_word;
    const dec_mb_params_t *ps_dec_mb_params;
    if(impeg2d_bit_stream_nxt(ps_stream,1) == 1)
    {
        impeg2d_bit_stream_flush(ps_stream,1);

    }
    else
    {
        u2_mb_addr_incr = impeg2d_get_mb_addr_incr(ps_stream);

        if(ps_dec->u2_first_mb)
        {
            /****************************************************************/
            /* Section 6.3.17                                               */
            /* The first MB of a slice cannot be skipped                    */
            /* But the mb_addr_incr can be > 1, because at the beginning of */
            /* a slice, it indicates the offset from the last MB in the     */
            /* previous row. Hence for the first slice in a row, the        */
            /* mb_addr_incr needs to be 1.                                  */
            /****************************************************************/
            /* MB_x is set to zero whenever MB_y changes.                   */
            ps_dec->u2_mb_x = u2_mb_addr_incr - 1;
            /* For error resilience */
            ps_dec->u2_mb_x = MIN(ps_dec->u2_mb_x, (ps_dec->u2_num_horiz_mb - 1));
            ps_dec->u2_num_mbs_left = ((ps_dec->u2_num_vert_mb - ps_dec->u2_mb_y)
                            * ps_dec->u2_num_horiz_mb) - ps_dec->u2_mb_x;

            /****************************************************************/
            /* mb_addr_incr is forced to 1 because in this decoder it is used */
            /* more as an indicator of the number of MBs skipped than the   */
            /* as defined by the standard (Section 6.3.17)                  */
            /****************************************************************/
            u2_mb_addr_incr = 1;
            ps_dec->u2_first_mb = 0;
        }
        else
        {
            /****************************************************************/
            /* In MPEG-2, the last MB of the row cannot be skipped and the  */
            /* mb_addr_incr cannot be such that it will take the current MB   */
            /* beyond the current row                                       */
            /* In MPEG-1, the slice could start and end anywhere and is not */
            /* restricted to a row like in MPEG-2. Hence this check should  */
            /* not be done for MPEG-1 streams.                              */
            /****************************************************************/
            if(ps_dec->u2_is_mpeg2 &&
                ((ps_dec->u2_mb_x + u2_mb_addr_incr) > ps_dec->u2_num_horiz_mb))
            {
                u2_mb_addr_incr    = ps_dec->u2_num_horiz_mb - ps_dec->u2_mb_x;
            }

            if ((u2_mb_addr_incr - 1) > ps_dec->u2_num_mbs_left)
            {
                /* If the number of skip MBs are more than the number of MBs
                 * left, indicate error.
                 */
                return IV_FAIL;
            }

            impeg2d_dec_skip_mbs(ps_dec, (UWORD16)(u2_mb_addr_incr - 1));
        }

    }
    u4_next_word = (UWORD16)impeg2d_bit_stream_nxt(ps_stream,16);
    /*-----------------------------------------------------------------------*/
    /* MB type                                                               */
    /*-----------------------------------------------------------------------*/
    {
        u2_mb_type   = ps_dec->pu2_mb_type[BITS((UWORD16)u4_next_word,15,10)];
        u2_len      = BITS(u2_mb_type,15,8);
        u2_total_len = u2_len;
        u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << u2_len);
    }
    /*-----------------------------------------------------------------------*/
    /* motion type                                                           */
    /*-----------------------------------------------------------------------*/
    {
        WORD32 i4_motion_type = ps_dec->u2_motion_type;

        if((u2_mb_type & MB_FORW_OR_BACK) &&  ps_dec->u2_read_motion_type)
        {
            ps_dec->u2_motion_type = BITS((UWORD16)u4_next_word,15,14);
            u2_total_len += MB_MOTION_TYPE_LEN;
            u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << MB_MOTION_TYPE_LEN);
            i4_motion_type     = ps_dec->u2_motion_type;

        }


        if ((u2_mb_type & MB_FORW_OR_BACK) &&
            ((i4_motion_type == 0) ||
            (i4_motion_type == 3) ||
            (i4_motion_type == 4) ||
            (i4_motion_type >= 7)))
        {
            //TODO: VANG Check for validity
            i4_motion_type = 1;
        }

    }
    /*-----------------------------------------------------------------------*/
    /* dct type                                                              */
    /*-----------------------------------------------------------------------*/
    {
        if((u2_mb_type & MB_CODED) && ps_dec->u2_read_dct_type)
        {
            ps_dec->u2_field_dct = BIT((UWORD16)u4_next_word,15);
            u2_total_len += MB_DCT_TYPE_LEN;
            u4_next_word = (UWORD16)LSW((UWORD16)u4_next_word << MB_DCT_TYPE_LEN);
        }
    }
    /*-----------------------------------------------------------------------*/
    /* Quant scale code                                                      */
    /*-----------------------------------------------------------------------*/
    if(u2_mb_type & MB_QUANT)
    {
        UWORD16 u2_quant_scale_code;
        u2_quant_scale_code = BITS((UWORD16)u4_next_word,15,11);

        ps_dec->u1_quant_scale = (ps_dec->u2_q_scale_type) ?
            gau1_impeg2_non_linear_quant_scale[u2_quant_scale_code] : (u2_quant_scale_code << 1);
        u2_total_len += MB_QUANT_SCALE_CODE_LEN;
    }
    impeg2d_bit_stream_flush(ps_stream,u2_total_len);
    /*-----------------------------------------------------------------------*/
    /* Set the function pointers                                             */
    /*-----------------------------------------------------------------------*/
    ps_dec->u2_coded_mb    = (UWORD16)(u2_mb_type & MB_CODED);

    if(u2_mb_type & MB_BIDRECT)
    {
        UWORD16 u2_index       = (ps_dec->u2_motion_type);

        ps_dec->u2_prev_intra_mb    = 0;
        ps_dec->e_mb_pred         = BIDIRECT;
        ps_dec_mb_params = &ps_dec->ps_func_bi_direct[u2_index];
        ps_dec->s_mb_type = ps_dec_mb_params->s_mb_type;
        if(NULL == ps_dec_mb_params->pf_func_mb_params)
            return -1;
        ps_dec_mb_params->pf_func_mb_params(ps_dec);
    }
    else if(u2_mb_type & MB_FORW_OR_BACK)
    {

        UWORD16 u2_refPic      = !(u2_mb_type & MB_MV_FORW);
        UWORD16 u2_index       = (ps_dec->u2_motion_type);
        ps_dec->u2_prev_intra_mb    = 0;
        ps_dec->e_mb_pred         = (e_pred_direction_t)u2_refPic;
        ps_dec_mb_params = &ps_dec->ps_func_forw_or_back[u2_index];
        ps_dec->s_mb_type = ps_dec_mb_params->s_mb_type;
        if(NULL == ps_dec_mb_params->pf_func_mb_params)
            return -1;
        ps_dec_mb_params->pf_func_mb_params(ps_dec);

    }
    else if(u2_mb_type & MB_TYPE_INTRA)
    {
        ps_dec->u2_prev_intra_mb    = 1;
        impeg2d_dec_intra_mb(ps_dec);

    }
    else
    {
        ps_dec->u2_prev_intra_mb =0;
        ps_dec->e_mb_pred = FORW;
        ps_dec->u2_motion_type = 0;
        impeg2d_dec_0mv_coded_mb(ps_dec);
    }

    /*-----------------------------------------------------------------------*/
    /* decode cbp                                                            */
    /*-----------------------------------------------------------------------*/
    if((u2_mb_type & MB_TYPE_INTRA))
    {
        ps_dec->u2_cbp  = 0x3f;
        ps_dec->u2_prev_intra_mb    = 1;
    }
    else
    {
        ps_dec->u2_prev_intra_mb  = 0;
        ps_dec->u2_def_dc_pred[Y_LUMA] = 128 << ps_dec->u2_intra_dc_precision;
        ps_dec->u2_def_dc_pred[U_CHROMA] = 128 << ps_dec->u2_intra_dc_precision;
        ps_dec->u2_def_dc_pred[V_CHROMA] = 128 << ps_dec->u2_intra_dc_precision;
        if((ps_dec->u2_coded_mb))
        {
            UWORD16 cbpValue;
            cbpValue  = gau2_impeg2d_cbp_code[impeg2d_bit_stream_nxt(ps_stream,MB_CBP_LEN)];
            ps_dec->u2_cbp  = cbpValue & 0xFF;
            impeg2d_bit_stream_flush(ps_stream,(cbpValue >> 8) & 0x0FF);
        }
        else
        {
            ps_dec->u2_cbp  = 0;
        }
    }
    return 0;
}

/*******************************************************************************
*  Function Name   : impeg2d_dec_p_b_slice
*
*  Description     : Decodes P and B slices
*
*  Arguments       :
*  dec             : Decoder state
*
*  Values Returned : None
*******************************************************************************/
IMPEG2D_ERROR_CODES_T impeg2d_dec_p_b_slice(dec_state_t *ps_dec)
{
    WORD16 *pi2_vld_out;
    UWORD32 i;
    yuv_buf_t *ps_cur_frm_buf      = &ps_dec->s_cur_frm_buf;

    UWORD32 u4_frm_offset          = 0;
    const dec_mb_params_t *ps_dec_mb_params;
    IMPEG2D_ERROR_CODES_T e_error   = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;

    pi2_vld_out = ps_dec->ai2_vld_buf;
    memset(ps_dec->ai2_pred_mv,0,sizeof(ps_dec->ai2_pred_mv));

    ps_dec->u2_prev_intra_mb    = 0;
    ps_dec->u2_first_mb       = 1;

    ps_dec->u2_picture_width = ps_dec->u2_frame_width;

    if(ps_dec->u2_picture_structure != FRAME_PICTURE)
    {
        ps_dec->u2_picture_width <<= 1;
        if(ps_dec->u2_picture_structure == BOTTOM_FIELD)
        {
            u4_frm_offset = ps_dec->u2_frame_width;
        }
    }

    do
    {
        UWORD32 u4_x_offset, u4_y_offset;
        WORD32 ret;


        UWORD32 u4_x_dst_offset = 0;
        UWORD32 u4_y_dst_offset = 0;
        UWORD8  *pu1_out_p;
        UWORD8  *pu1_pred;
        WORD32 u4_pred_strd;

        IMPEG2D_TRACE_MB_START(ps_dec->u2_mb_x, ps_dec->u2_mb_y);

        if(ps_dec->e_pic_type == B_PIC)
            ret = impeg2d_dec_pnb_mb_params(ps_dec);
        else
            ret = impeg2d_dec_p_mb_params(ps_dec);

        if(ret)
            return IMPEG2D_MB_TEX_DECODE_ERR;

        if(0 >= ps_dec->u2_num_mbs_left)
        {
            break;
        }

        IMPEG2D_TRACE_MB_START(ps_dec->u2_mb_x, ps_dec->u2_mb_y);

        u4_x_dst_offset = u4_frm_offset + (ps_dec->u2_mb_x << 4);
        u4_y_dst_offset = (ps_dec->u2_mb_y << 4) * ps_dec->u2_picture_width;
        pu1_out_p = ps_cur_frm_buf->pu1_y + u4_x_dst_offset + u4_y_dst_offset;
        if(ps_dec->u2_prev_intra_mb == 0)
        {
            UWORD32 offset_x, offset_y, stride;
            UWORD16 index = (ps_dec->u2_motion_type);
            /*only for non intra mb's*/
            if(ps_dec->e_mb_pred == BIDIRECT)
            {
                ps_dec_mb_params = &ps_dec->ps_func_bi_direct[index];
            }
            else
            {
                ps_dec_mb_params = &ps_dec->ps_func_forw_or_back[index];
            }

            stride = ps_dec->u2_picture_width;

            offset_x = u4_frm_offset + (ps_dec->u2_mb_x << 4);

            offset_y = (ps_dec->u2_mb_y << 4);

            ps_dec->s_dest_buf.pu1_y = ps_cur_frm_buf->pu1_y + offset_y * stride + offset_x;

            stride = stride >> 1;

            ps_dec->s_dest_buf.pu1_u = ps_cur_frm_buf->pu1_u + (offset_y >> 1) * stride
                            + (offset_x >> 1);

            ps_dec->s_dest_buf.pu1_v = ps_cur_frm_buf->pu1_v + (offset_y >> 1) * stride
                            + (offset_x >> 1);

            PROFILE_DISABLE_MC_IF0
            ps_dec_mb_params->pf_mc(ps_dec);

        }
        for(i = 0; i < NUM_LUMA_BLKS; ++i)
        {
            if((ps_dec->u2_cbp & (1 << (BLOCKS_IN_MB - 1 - i))) != 0)
            {
                e_error = ps_dec->pf_vld_inv_quant(ps_dec, pi2_vld_out, ps_dec->pu1_inv_scan_matrix,
                              ps_dec->u2_prev_intra_mb, Y_LUMA, 0);
                if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
                {
                    return e_error;
                }

                u4_x_offset = gai2_impeg2_blk_x_off[i];

                if(ps_dec->u2_field_dct == 0)
                    u4_y_offset = gai2_impeg2_blk_y_off_frm[i] ;
                else
                    u4_y_offset = gai2_impeg2_blk_y_off_fld[i] ;





                IMPEG2D_IDCT_INP_STATISTICS(pi2_vld_out, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows);

                PROFILE_DISABLE_IDCT_IF0
                {
                    WORD32 idx;
                    if(1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
                        idx = 0;
                    else
                        idx = 1;

                    if(0 == ps_dec->u2_prev_intra_mb)
                    {
                        pu1_pred = pu1_out_p + u4_y_offset * ps_dec->u2_picture_width + u4_x_offset;
                        u4_pred_strd = ps_dec->u2_picture_width << ps_dec->u2_field_dct;
                    }
                    else
                    {
                        pu1_pred = (UWORD8 *)gau1_impeg2_zerobuf;
                        u4_pred_strd = 8;
                    }

                    ps_dec->pf_idct_recon[idx * 2 + ps_dec->i4_last_value_one](pi2_vld_out,
                                                            ps_dec->ai2_idct_stg1,
                                                            pu1_pred,
                                                            pu1_out_p + u4_y_offset * ps_dec->u2_picture_width + u4_x_offset,
                                                            8,
                                                            u4_pred_strd,
                                                            ps_dec->u2_picture_width << ps_dec->u2_field_dct,
                                                            ~ps_dec->u4_non_zero_cols, ~ps_dec->u4_non_zero_rows);
                }
            }

        }

        /* For U and V blocks, divide the x and y offsets by 2. */
        u4_x_dst_offset >>= 1;
        u4_y_dst_offset >>= 2;


        /* In case of chrominance blocks the DCT will be frame DCT */
        /* i = 0, U component and i = 1 is V componet */
        if((ps_dec->u2_cbp & 0x02) != 0)
        {
            pu1_out_p = ps_cur_frm_buf->pu1_u + u4_x_dst_offset + u4_y_dst_offset;
            e_error = ps_dec->pf_vld_inv_quant(ps_dec, pi2_vld_out, ps_dec->pu1_inv_scan_matrix,
                          ps_dec->u2_prev_intra_mb, U_CHROMA, 0);
            if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
            {
                return e_error;
            }


            IMPEG2D_IDCT_INP_STATISTICS(pi2_vld_out, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows);

            PROFILE_DISABLE_IDCT_IF0
            {
                WORD32 idx;
                if(1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
                    idx = 0;
                else
                    idx = 1;

                if(0 == ps_dec->u2_prev_intra_mb)
                {
                    pu1_pred = pu1_out_p;
                    u4_pred_strd = ps_dec->u2_picture_width >> 1;
                }
                else
                {
                    pu1_pred = (UWORD8 *)gau1_impeg2_zerobuf;
                    u4_pred_strd = 8;
                }

                ps_dec->pf_idct_recon[idx * 2 + ps_dec->i4_last_value_one](pi2_vld_out,
                                                        ps_dec->ai2_idct_stg1,
                                                        pu1_pred,
                                                        pu1_out_p,
                                                        8,
                                                        u4_pred_strd,
                                                        ps_dec->u2_picture_width >> 1,
                                                        ~ps_dec->u4_non_zero_cols, ~ps_dec->u4_non_zero_rows);

            }

        }


        if((ps_dec->u2_cbp & 0x01) != 0)
        {
            pu1_out_p = ps_cur_frm_buf->pu1_v + u4_x_dst_offset + u4_y_dst_offset;
            e_error = ps_dec->pf_vld_inv_quant(ps_dec, pi2_vld_out, ps_dec->pu1_inv_scan_matrix,
                          ps_dec->u2_prev_intra_mb, V_CHROMA, 0);
            if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
            {
                return e_error;
            }


            IMPEG2D_IDCT_INP_STATISTICS(pi2_vld_out, ps_dec->u4_non_zero_cols, ps_dec->u4_non_zero_rows);

            PROFILE_DISABLE_IDCT_IF0
            {
                WORD32 idx;
                if(1 == (ps_dec->u4_non_zero_cols | ps_dec->u4_non_zero_rows))
                    idx = 0;
                else
                    idx = 1;
                if(0 == ps_dec->u2_prev_intra_mb)
                {
                    pu1_pred = pu1_out_p;
                    u4_pred_strd = ps_dec->u2_picture_width >> 1;
                }
                else
                {
                    pu1_pred = (UWORD8 *)gau1_impeg2_zerobuf;
                    u4_pred_strd = 8;
                }

                ps_dec->pf_idct_recon[idx * 2 + ps_dec->i4_last_value_one](pi2_vld_out,
                                                        ps_dec->ai2_idct_stg1,
                                                        pu1_pred,
                                                        pu1_out_p,
                                                        8,
                                                        u4_pred_strd,
                                                        ps_dec->u2_picture_width >> 1,
                                                        ~ps_dec->u4_non_zero_cols, ~ps_dec->u4_non_zero_rows);

            }
        }

        ps_dec->u2_num_mbs_left--;
        ps_dec->u2_first_mb = 0;
        ps_dec->u2_mb_x++;

        if(ps_dec->s_bit_stream.u4_offset > ps_dec->s_bit_stream.u4_max_offset)
        {
            return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
        }
        else if (ps_dec->u2_mb_x == ps_dec->u2_num_horiz_mb)
        {
            ps_dec->u2_mb_x = 0;
            ps_dec->u2_mb_y++;

        }
    }
    while(ps_dec->u2_num_mbs_left != 0 && impeg2d_bit_stream_nxt(&ps_dec->s_bit_stream,23) != 0x0);
    return e_error;
}