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