summaryrefslogtreecommitdiffstats
path: root/decoder/impeg2d_pic_proc.c
blob: 6826def957464a943585e0c23e16396f41160156 (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
/******************************************************************************
 *
 * 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_globals.h"
#include "impeg2d_vld_tables.h"
#include "impeg2d_vld.h"
#include "impeg2d_pic_proc.h"
#include "impeg2d_debug.h"

void impeg2d_init_function_ptr(void *pv_codec);
void impeg2d_format_convert(dec_state_t *ps_dec,
                            pic_buf_t *ps_src_pic,
                            iv_yuv_buf_t    *ps_disp_frm_buf,
                            UWORD32 u4_start_row, UWORD32 u4_num_rows)
{
    UWORD8  *pu1_src_y,*pu1_src_u,*pu1_src_v;
    UWORD8  *pu1_dst_y,*pu1_dst_u,*pu1_dst_v;



    if((NULL == ps_src_pic) || (NULL == ps_src_pic->pu1_y) || (0 == u4_num_rows))
            return;

    pu1_src_y   = ps_src_pic->pu1_y + (u4_start_row * ps_dec->u2_frame_width);
    pu1_src_u   = ps_src_pic->pu1_u + ((u4_start_row >> 1) * (ps_dec->u2_frame_width >> 1));
    pu1_src_v   = ps_src_pic->pu1_v + ((u4_start_row >> 1) *(ps_dec->u2_frame_width >> 1));

    pu1_dst_y  =  (UWORD8 *)ps_disp_frm_buf->pv_y_buf + (u4_start_row *  ps_dec->u4_frm_buf_stride);
    pu1_dst_u =   (UWORD8 *)ps_disp_frm_buf->pv_u_buf +((u4_start_row >> 1)*(ps_dec->u4_frm_buf_stride >> 1));
    pu1_dst_v =   (UWORD8 *)ps_disp_frm_buf->pv_v_buf +((u4_start_row >> 1)*(ps_dec->u4_frm_buf_stride >> 1));

    if (IV_YUV_420P == ps_dec->i4_chromaFormat)
    {
        ps_dec->pf_copy_yuv420p_buf(pu1_src_y, pu1_src_u, pu1_src_v, pu1_dst_y,
                                    pu1_dst_u, pu1_dst_v,
                                    ps_dec->u2_horizontal_size,
                                    u4_num_rows,
                                    ps_dec->u2_frame_width,
                                    (ps_dec->u2_frame_width >> 1),
                                    (ps_dec->u2_frame_width >> 1),
                                    ps_dec->u4_frm_buf_stride,
                                    (ps_dec->u4_frm_buf_stride >> 1),
                                    (ps_dec->u4_frm_buf_stride >> 1));
    }
    else if (IV_YUV_422ILE == ps_dec->i4_chromaFormat)
    {
        void    *pv_yuv422i;
        UWORD32 u2_height,u2_width,u2_stride_y,u2_stride_u,u2_stride_v;
        UWORD32 u2_stride_yuv422i;


        pv_yuv422i          = (UWORD8 *)ps_disp_frm_buf->pv_y_buf + ((ps_dec->u2_vertical_size)*(ps_dec->u4_frm_buf_stride));
        u2_height           = u4_num_rows;
        u2_width            = ps_dec->u2_horizontal_size;
        u2_stride_y         = ps_dec->u2_frame_width;
        u2_stride_u         = u2_stride_y >> 1;
        u2_stride_v         = u2_stride_u;
        u2_stride_yuv422i   = (0 == ps_dec->u4_frm_buf_stride) ? ps_dec->u2_horizontal_size : ps_dec->u4_frm_buf_stride;

        ps_dec->pf_fmt_conv_yuv420p_to_yuv422ile(pu1_src_y,
            pu1_src_u,
            pu1_src_v,
            pv_yuv422i,
            u2_width,
            u2_height,
            u2_stride_y,
            u2_stride_u,
            u2_stride_v,
            u2_stride_yuv422i);

    }
    else if((ps_dec->i4_chromaFormat == IV_YUV_420SP_UV) ||
            (ps_dec->i4_chromaFormat == IV_YUV_420SP_VU))
    {

        UWORD32 dest_inc_Y=0,dest_inc_UV=0;
        WORD32 convert_uv_only;

        pu1_dst_u =   (UWORD8 *)ps_disp_frm_buf->pv_u_buf +((u4_start_row >> 1)*(ps_dec->u4_frm_buf_stride));
        dest_inc_Y =    ps_dec->u4_frm_buf_stride;
        dest_inc_UV =   ((ps_dec->u4_frm_buf_stride + 1) >> 1) << 1;
        convert_uv_only = 0;

        if(1 == ps_dec->u4_share_disp_buf)
            convert_uv_only = 1;

        if(pu1_src_y == pu1_dst_y)
            convert_uv_only = 1;

        if(ps_dec->i4_chromaFormat == IV_YUV_420SP_UV)
        {
            ps_dec->pf_fmt_conv_yuv420p_to_yuv420sp_uv(pu1_src_y,
                pu1_src_u,
                pu1_src_v,
                pu1_dst_y,
                pu1_dst_u,
                u4_num_rows,
                ps_dec->u2_horizontal_size,
                ps_dec->u2_frame_width,
                ps_dec->u2_frame_width >> 1,
                ps_dec->u2_frame_width >> 1,
                dest_inc_Y,
                dest_inc_UV,
                convert_uv_only);
        }
        else
        {
            ps_dec->pf_fmt_conv_yuv420p_to_yuv420sp_vu(pu1_src_y,
                    pu1_src_u,
                    pu1_src_v,
                    pu1_dst_y,
                    pu1_dst_u,
                    u4_num_rows,
                    ps_dec->u2_horizontal_size,
                    ps_dec->u2_frame_width,
                    ps_dec->u2_frame_width >> 1,
                    ps_dec->u2_frame_width >> 1,
                    dest_inc_Y,
                    dest_inc_UV,
                    convert_uv_only);
        }



    }

}


/*******************************************************************************
*
*  Function Name   : impeg2d_get_frm_buf
*
*  Description     : Gets YUV component buffers for the frame
*
*  Arguments       :
*  frm_buf         : YUV buffer
*  frm             : Reference frame
*  width           : Width of the frame
*  Height          : Height of the frame
*
*  Values Returned : None
*******************************************************************************/
void impeg2d_get_frm_buf(yuv_buf_t *ps_frm_buf,UWORD8 *pu1_frm,UWORD32 u4_width,UWORD32 u4_height)
{
   UWORD32 u4_luma_size = u4_width * u4_height;
   UWORD32 u4_chroma_size = (u4_width * u4_height)>>2;

   ps_frm_buf->pu1_y = pu1_frm;
   ps_frm_buf->pu1_u = pu1_frm + u4_luma_size;
   ps_frm_buf->pu1_v = pu1_frm + u4_luma_size + u4_chroma_size;

}
/*******************************************************************************
*
*  Function Name   : impeg2d_get_bottom_field_buf
*
*  Description     : Gets YUV component buffers for bottom field of the frame
*
*  Arguments       :
*  frm_buf         : YUV buffer
*  frm             : Reference frame
*  width           : Width of the frame
*  Height          : Height of the frame
*
*  Values Returned : None
*******************************************************************************/
void impeg2d_get_bottom_field_buf(yuv_buf_t *ps_src_buf,yuv_buf_t *ps_dst_buf,
                      UWORD32 u4_width)
{
   ps_dst_buf->pu1_y = ps_src_buf->pu1_y + u4_width;
   ps_dst_buf->pu1_u = ps_src_buf->pu1_u + (u4_width>>1);
   ps_dst_buf->pu1_v = ps_src_buf->pu1_v + (u4_width>>1);

}
/*******************************************************************************
*  Function Name   : impeg2d_get_mb_addr_incr
*
*  Description     : Decodes the Macroblock address increment
*
*  Arguments       :
*  stream          : Bitstream
*
*  Values Returned : Macroblock address increment
*******************************************************************************/
UWORD16 impeg2d_get_mb_addr_incr(stream_t *ps_stream)
{
    UWORD16 u2_mb_addr_incr = 0;
    while (impeg2d_bit_stream_nxt(ps_stream,MB_ESCAPE_CODE_LEN) == MB_ESCAPE_CODE &&
            ps_stream->u4_offset < ps_stream->u4_max_offset)
    {
        impeg2d_bit_stream_flush(ps_stream,MB_ESCAPE_CODE_LEN);
        u2_mb_addr_incr += 33;
    }
    u2_mb_addr_incr += impeg2d_dec_vld_symbol(ps_stream,gai2_impeg2d_mb_addr_incr,MB_ADDR_INCR_LEN) +
        MB_ADDR_INCR_OFFSET;
    return(u2_mb_addr_incr);
}

/*******************************************************************************
*
*  Function Name   : impeg2d_init_video_state
*
*  Description     : Initializes the Video decoder state
*
*  Arguments       :
*  dec             : Decoder context
*  videoType       : MPEG_2_Video / MPEG_1_Video
*
*  Values Returned : None
*******************************************************************************/
IMPEG2D_ERROR_CODES_T impeg2d_init_video_state(dec_state_t *ps_dec, e_video_type_t e_video_type)
{
    /*-----------------------------------------------------------------------*/
    /* Bit Stream  that conforms to MPEG-1 <ISO/IEC 11172-2> standard        */
    /*-----------------------------------------------------------------------*/
    if(e_video_type == MPEG_1_VIDEO)
    {
        ps_dec->u2_is_mpeg2 = 0;

        /*-------------------------------------------------------------------*/
        /* force MPEG-1 parameters for proper decoder behavior               */
        /* see ISO/IEC 13818-2 section D.9.14                                */
        /*-------------------------------------------------------------------*/
        ps_dec->u2_progressive_sequence         = 1;
        ps_dec->u2_intra_dc_precision           = 0;
        ps_dec->u2_picture_structure            = FRAME_PICTURE;
        ps_dec->u2_frame_pred_frame_dct         = 1;
        ps_dec->u2_concealment_motion_vectors   = 0;
        ps_dec->u2_q_scale_type                 = 0;
        ps_dec->u2_intra_vlc_format             = 0;
        ps_dec->u2_alternate_scan               = 0;
        ps_dec->u2_repeat_first_field           = 0;
        ps_dec->u2_progressive_frame            = 1;
        ps_dec->u2_frame_rate_extension_n       = 0;
        ps_dec->u2_frame_rate_extension_d       = 0;
        ps_dec->u2_forw_f_code                  = 7;
        ps_dec->u2_back_f_code                  = 7;

        ps_dec->pf_vld_inv_quant                  = impeg2d_vld_inv_quant_mpeg1;
        /*-------------------------------------------------------------------*/
        /* Setting of parameters other than those mentioned in MPEG2 standard*/
        /* but used in decoding process.                                     */
        /*-------------------------------------------------------------------*/
    }
    /*-----------------------------------------------------------------------*/
    /* Bit Stream  that conforms to MPEG-2                                   */
    /*-----------------------------------------------------------------------*/
    else
    {
        ps_dec->u2_is_mpeg2                  = 1;
        ps_dec->u2_full_pel_forw_vector   = 0;
        ps_dec->u2_forw_f_code            = 7;
        ps_dec->u2_full_pel_back_vector   = 0;
        ps_dec->u2_back_f_code            = 7;
        ps_dec->pf_vld_inv_quant       = impeg2d_vld_inv_quant_mpeg2;


    }


    impeg2d_init_function_ptr(ps_dec);

    /* Set the frame Width and frame Height */
    ps_dec->u2_frame_height        = ALIGN16(ps_dec->u2_vertical_size);
    ps_dec->u2_frame_width         = ALIGN16(ps_dec->u2_horizontal_size);
    ps_dec->u2_num_horiz_mb         = (ps_dec->u2_horizontal_size + 15) >> 4;
   // dec->u4_frm_buf_stride    = dec->frameWidth;
    if (ps_dec->u2_frame_height > ps_dec->u2_create_max_height || ps_dec->u2_frame_width > ps_dec->u2_create_max_width)
    {
        return IMPEG2D_PIC_SIZE_NOT_SUPPORTED;
    }

    ps_dec->u2_num_flds_decoded = 0;

    /* Calculate the frame period */
    {
        UWORD32 numer;
        UWORD32 denom;
        numer = (UWORD32)gau2_impeg2_frm_rate_code[ps_dec->u2_frame_rate_code][1] *
                                (UWORD32)(ps_dec->u2_frame_rate_extension_d + 1);

        denom = (UWORD32)gau2_impeg2_frm_rate_code[ps_dec->u2_frame_rate_code][0] *
                                (UWORD32)(ps_dec->u2_frame_rate_extension_n + 1);
        ps_dec->u2_framePeriod =  (numer * 1000 * 100) / denom;
    }


   if(VERTICAL_SCAN == ps_dec->u2_alternate_scan)
   {
    ps_dec->pu1_inv_scan_matrix = (UWORD8 *)gau1_impeg2_inv_scan_vertical;
   }
   else
   {
    ps_dec->pu1_inv_scan_matrix = (UWORD8 *)gau1_impeg2_inv_scan_zig_zag;
   }
   return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
}
/*******************************************************************************
*
*  Function Name   : impeg2d_pre_pic_dec_proc
*
*  Description     : Does the processing neccessary before picture decoding
*
*  Arguments       :
*  dec             : Decoder context
*
*  Values Returned : None
*******************************************************************************/
IMPEG2D_ERROR_CODES_T impeg2d_pre_pic_dec_proc(dec_state_t *ps_dec)
{
    WORD32 u4_get_disp;
    pic_buf_t *ps_disp_pic;
    IMPEG2D_ERROR_CODES_T e_error = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;

    u4_get_disp = 0;
    ps_disp_pic = NULL;

    /* Field Picture */
    if(ps_dec->u2_picture_structure != FRAME_PICTURE)
    {
        ps_dec->u2_num_vert_mb       = (ps_dec->u2_vertical_size + 31) >> 5;

        if(ps_dec->u2_num_flds_decoded == 0)
        {
            pic_buf_t *ps_pic_buf;
            u4_get_disp = 1;

            ps_pic_buf = impeg2_buf_mgr_get_next_free(ps_dec->pv_pic_buf_mg, &ps_dec->i4_cur_buf_id);

            if (NULL == ps_pic_buf)
            {
                return IMPEG2D_NO_FREE_BUF_ERR;
            }

            impeg2_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mg, ps_dec->i4_cur_buf_id, BUF_MGR_DISP);
            impeg2_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mg, ps_dec->i4_cur_buf_id, BUF_MGR_REF);
            if(ps_dec->u4_deinterlace)
                impeg2_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mg, ps_dec->i4_cur_buf_id, MPEG2_BUF_MGR_DEINT);

            ps_pic_buf->u4_ts = ps_dec->u4_inp_ts;
            ps_pic_buf->e_pic_type = ps_dec->e_pic_type;
            ps_dec->ps_cur_pic = ps_pic_buf;
            ps_dec->s_cur_frm_buf.pu1_y = ps_pic_buf->pu1_y;
            ps_dec->s_cur_frm_buf.pu1_u = ps_pic_buf->pu1_u;
            ps_dec->s_cur_frm_buf.pu1_v = ps_pic_buf->pu1_v;
        }

        if(ps_dec->u2_picture_structure == TOP_FIELD)
        {
            ps_dec->u2_fld_parity = TOP;
        }
        else
        {
            ps_dec->u2_fld_parity = BOTTOM;
        }
        ps_dec->u2_field_dct           = 0;
        ps_dec->u2_read_dct_type        = 0;
        ps_dec->u2_read_motion_type     = 1;
        ps_dec->u2_fld_pic             = 1;
        ps_dec->u2_frm_pic             = 0;
        ps_dec->ps_func_forw_or_back     = gas_impeg2d_func_fld_fw_or_bk;
        ps_dec->ps_func_bi_direct       = gas_impeg2d_func_fld_bi_direct;
   }
    /* Frame Picture */
    else
    {
        pic_buf_t *ps_pic_buf;


        ps_dec->u2_num_vert_mb       = (ps_dec->u2_vertical_size + 15) >> 4;
        u4_get_disp = 1;
        ps_pic_buf = impeg2_buf_mgr_get_next_free(ps_dec->pv_pic_buf_mg, &ps_dec->i4_cur_buf_id);

        if (NULL == ps_pic_buf)
        {
            return IMPEG2D_NO_FREE_BUF_ERR;
        }
        impeg2_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mg, ps_dec->i4_cur_buf_id, BUF_MGR_DISP);
        impeg2_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mg, ps_dec->i4_cur_buf_id, BUF_MGR_REF);
        if(ps_dec->u4_deinterlace)
            impeg2_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mg, ps_dec->i4_cur_buf_id, MPEG2_BUF_MGR_DEINT);

        ps_pic_buf->u4_ts = ps_dec->u4_inp_ts;
        ps_pic_buf->e_pic_type = ps_dec->e_pic_type;
        ps_dec->ps_cur_pic = ps_pic_buf;
        ps_dec->s_cur_frm_buf.pu1_y = ps_pic_buf->pu1_y;
        ps_dec->s_cur_frm_buf.pu1_u = ps_pic_buf->pu1_u;
        ps_dec->s_cur_frm_buf.pu1_v = ps_pic_buf->pu1_v;


        if(ps_dec->u2_frame_pred_frame_dct == 0)
        {
            ps_dec->u2_read_dct_type    = 1;
            ps_dec->u2_read_motion_type = 1;
        }
        else
        {
            ps_dec->u2_read_dct_type    = 0;
            ps_dec->u2_read_motion_type = 0;
            ps_dec->u2_motion_type     = 2;
            ps_dec->u2_field_dct       = 0;
        }

        ps_dec->u2_fld_parity          = TOP;
        ps_dec->u2_fld_pic             = 0;
        ps_dec->u2_frm_pic             = 1;
        ps_dec->ps_func_forw_or_back     = gas_impeg2d_func_frm_fw_or_bk;
        ps_dec->ps_func_bi_direct       = gas_impeg2d_func_frm_bi_direct;
   }
    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;
    ps_dec->u2_num_mbs_left  = ps_dec->u2_num_horiz_mb * ps_dec->u2_num_vert_mb;
    if(u4_get_disp)
    {
        if(ps_dec->u4_num_frames_decoded > 1)
        {
            ps_disp_pic = impeg2_disp_mgr_get(&ps_dec->s_disp_mgr, &ps_dec->i4_disp_buf_id);
        }
        ps_dec->ps_disp_pic = ps_disp_pic;
        if(ps_disp_pic)
        {
            if(1 == ps_dec->u4_share_disp_buf)
            {
                ps_dec->ps_disp_frm_buf->pv_y_buf  = ps_disp_pic->pu1_y;
                if(IV_YUV_420P == ps_dec->i4_chromaFormat)
                {
                    ps_dec->ps_disp_frm_buf->pv_u_buf  = ps_disp_pic->pu1_u;
                    ps_dec->ps_disp_frm_buf->pv_v_buf  = ps_disp_pic->pu1_v;
                }
                else
                {
                    UWORD8 *pu1_buf;

                    pu1_buf = ps_dec->as_disp_buffers[ps_disp_pic->i4_buf_id].pu1_bufs[1];
                    ps_dec->ps_disp_frm_buf->pv_u_buf  = pu1_buf;

                    pu1_buf = ps_dec->as_disp_buffers[ps_disp_pic->i4_buf_id].pu1_bufs[2];
                    ps_dec->ps_disp_frm_buf->pv_v_buf  = pu1_buf;
                }
            }
        }
    }


    switch(ps_dec->e_pic_type)
    {
    case I_PIC:
        {
            ps_dec->pf_decode_slice = impeg2d_dec_i_slice;
            break;
        }
    case D_PIC:
        {
            ps_dec->pf_decode_slice = impeg2d_dec_d_slice;
            break;
        }
    case P_PIC:
        {
            ps_dec->pf_decode_slice = impeg2d_dec_p_b_slice;
            ps_dec->pu2_mb_type       = gau2_impeg2d_p_mb_type;
            break;
        }
    case B_PIC:
        {
            ps_dec->pf_decode_slice = impeg2d_dec_p_b_slice;
            ps_dec->pu2_mb_type       = gau2_impeg2d_b_mb_type;
            break;
        }
    default:
        return IMPEG2D_INVALID_PIC_TYPE;
    }

    /*************************************************************************/
    /* Set the reference pictures                                            */
    /*************************************************************************/

    /* Error resilience: If forward and backward pictures are going to be NULL*/
    /* then assign both to the current                                        */
    /* if one of them NULL then we will assign the non null to the NULL one   */

    if(ps_dec->e_pic_type == P_PIC)
    {
        if (NULL == ps_dec->as_recent_fld[1][0].pu1_y)
        {
            ps_dec->as_recent_fld[1][0] = ps_dec->s_cur_frm_buf;
        }
        if (NULL == ps_dec->as_recent_fld[1][1].pu1_y)
        {
            impeg2d_get_bottom_field_buf(&ps_dec->s_cur_frm_buf, &ps_dec->as_recent_fld[1][1],
                ps_dec->u2_frame_width);
        }

        ps_dec->as_ref_buf[FORW][TOP]    = ps_dec->as_recent_fld[1][0];
        ps_dec->as_ref_buf[FORW][BOTTOM] = ps_dec->as_recent_fld[1][1];


    }
    else if(ps_dec->e_pic_type == B_PIC)
    {
        if((NULL == ps_dec->as_recent_fld[1][0].pu1_y) && (NULL == ps_dec->as_recent_fld[0][0].pu1_y))
        {
            // assign the current picture to both
            ps_dec->as_recent_fld[1][0] = ps_dec->s_cur_frm_buf;
            impeg2d_get_bottom_field_buf(&ps_dec->s_cur_frm_buf, &ps_dec->as_recent_fld[1][1],
                ps_dec->u2_frame_width);
            ps_dec->as_recent_fld[0][0] = ps_dec->s_cur_frm_buf;
            ps_dec->as_recent_fld[0][1] = ps_dec->as_recent_fld[1][1];
        }
        //Assign the non-null picture to the null picture
        else if ((NULL != ps_dec->as_recent_fld[1][0].pu1_y) && (NULL == ps_dec->as_recent_fld[0][0].pu1_y))
        {
            ps_dec->as_recent_fld[0][0] = ps_dec->as_recent_fld[1][0];
            ps_dec->as_recent_fld[0][1] = ps_dec->as_recent_fld[1][1];
        }
        else if ((NULL == ps_dec->as_recent_fld[1][0].pu1_y) && (NULL != ps_dec->as_recent_fld[0][0].pu1_y))
        {
            ps_dec->as_recent_fld[1][0] = ps_dec->as_recent_fld[0][0];
            ps_dec->as_recent_fld[1][1] = ps_dec->as_recent_fld[0][1];
        }

        /* Error resilience: If forward and backward pictures are going to be NULL*/
        /* then assign both to the current                                        */
        /* if one of them NULL then we will assign the non null to the NULL one   */

        if((NULL == ps_dec->as_recent_fld[0][1].pu1_y) && (NULL == ps_dec->as_recent_fld[1][1].pu1_y))
        {
            // assign the current picture to both
            ps_dec->as_recent_fld[1][0] = ps_dec->s_cur_frm_buf;
            impeg2d_get_bottom_field_buf(&ps_dec->s_cur_frm_buf, &ps_dec->as_recent_fld[1][1],
                                         ps_dec->u2_frame_width);
            ps_dec->as_recent_fld[0][0] = ps_dec->s_cur_frm_buf;
            ps_dec->as_recent_fld[0][1] = ps_dec->as_recent_fld[1][1];
        }
        //Assign the non-null picture to the null picture

        else if((NULL == ps_dec->as_recent_fld[0][1].pu1_y) && (NULL != ps_dec->as_recent_fld[1][1].pu1_y))
        {
            ps_dec->as_recent_fld[0][0] = ps_dec->as_recent_fld[1][0];
            ps_dec->as_recent_fld[0][1] = ps_dec->as_recent_fld[1][1];
        }

        else if((NULL == ps_dec->as_recent_fld[1][1].pu1_y) && (NULL != ps_dec->as_recent_fld[0][1].pu1_y))
        {
            ps_dec->as_recent_fld[1][0] = ps_dec->as_recent_fld[0][0];
            ps_dec->as_recent_fld[1][1] = ps_dec->as_recent_fld[0][1];
        }
        ps_dec->as_ref_buf[FORW][TOP]    = ps_dec->as_recent_fld[0][0];
        ps_dec->as_ref_buf[FORW][BOTTOM] = ps_dec->as_recent_fld[0][1];
        ps_dec->as_ref_buf[BACK][TOP]    = ps_dec->as_recent_fld[1][0];
        ps_dec->as_ref_buf[BACK][BOTTOM] = ps_dec->as_recent_fld[1][1];


    }

    return e_error;
}

/*******************************************************************************
*
*  Function Name   : impeg2d_post_pic_dec_proc
*
*  Description     : Performs processing that is needed at the end of picture
*                    decode
*
*  Arguments       :
*  dec             : Decoder context
*
*  Values Returned : None
*******************************************************************************/
void impeg2d_post_pic_dec_proc(dec_state_t *ps_dec)
{

   WORD32 u4_update_pic_buf = 0;
    /*************************************************************************/
    /* Processing at the end of picture                                      */
    /*************************************************************************/
    if(ps_dec->u2_picture_structure != FRAME_PICTURE)
    {
        ps_dec->u2_num_vert_mb       = (ps_dec->u2_vertical_size + 31) >> 5;

        if(ps_dec->u2_num_flds_decoded == 1)
        {
            ps_dec->u2_num_flds_decoded = 0;
            u4_update_pic_buf = 1;
        }
        else
        {
            ps_dec->u2_num_flds_decoded = 1;
        }
    }
    else
    {
        u4_update_pic_buf = 1;
    }

    if(u4_update_pic_buf)
    {
        ps_dec->i4_frame_decoded = 1;
        if(ps_dec->e_pic_type != B_PIC)
        {
            /* In any sequence first two pictures have to be reference pictures */
            /* Adding of first picture in the sequence */
            if(ps_dec->aps_ref_pics[0] == NULL)
            {
                ps_dec->aps_ref_pics[0] = ps_dec->ps_cur_pic;
            }

            /* Adding of second picture in the sequence */
            else if(ps_dec->aps_ref_pics[1] == NULL)
            {
                ps_dec->aps_ref_pics[1] = ps_dec->ps_cur_pic;
                impeg2_disp_mgr_add(&ps_dec->s_disp_mgr, ps_dec->aps_ref_pics[0], ps_dec->aps_ref_pics[0]->i4_buf_id);
            }
            else
            {

                impeg2_disp_mgr_add(&ps_dec->s_disp_mgr, ps_dec->aps_ref_pics[1], ps_dec->aps_ref_pics[1]->i4_buf_id);
                impeg2_buf_mgr_release(ps_dec->pv_pic_buf_mg, ps_dec->aps_ref_pics[0]->i4_buf_id, BUF_MGR_REF);
                ps_dec->aps_ref_pics[0] = ps_dec->aps_ref_pics[1];
                ps_dec->aps_ref_pics[1] = ps_dec->ps_cur_pic;

            }
        }
        else
        {
            impeg2_disp_mgr_add(&ps_dec->s_disp_mgr, ps_dec->ps_cur_pic, ps_dec->ps_cur_pic->i4_buf_id);

            impeg2_buf_mgr_release(ps_dec->pv_pic_buf_mg, ps_dec->ps_cur_pic->i4_buf_id, BUF_MGR_REF);
        }

    }
    /*************************************************************************/
    /* Update the list of recent reference pictures                          */
    /*************************************************************************/
    if(ps_dec->e_pic_type != B_PIC)
    {
        switch(ps_dec->u2_picture_structure)
        {
        case FRAME_PICTURE:
            {
                ps_dec->as_recent_fld[0][0] = ps_dec->as_recent_fld[1][0];
                ps_dec->as_recent_fld[0][1] = ps_dec->as_recent_fld[1][1];

                ps_dec->as_recent_fld[1][0] = ps_dec->s_cur_frm_buf;
                impeg2d_get_bottom_field_buf(&ps_dec->s_cur_frm_buf, &ps_dec->as_recent_fld[1][1],
                ps_dec->u2_frame_width);
                break;
            }
        case TOP_FIELD:
            {
                ps_dec->as_recent_fld[0][0] = ps_dec->as_recent_fld[1][0];
                ps_dec->as_recent_fld[1][0] = ps_dec->s_cur_frm_buf;
                break;
            }
        case BOTTOM_FIELD:
            {
                ps_dec->as_recent_fld[0][1] = ps_dec->as_recent_fld[1][1];
                impeg2d_get_bottom_field_buf(&ps_dec->s_cur_frm_buf, &ps_dec->as_recent_fld[1][1],
                ps_dec->u2_frame_width);
                break;
            }
        }
    }
}