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 <string.h>
21#include <cutils/log.h>
22
23#include "iv_datatypedef.h"
24#include "iv.h"
25#include "ivd.h"
26#include "impeg2_macros.h"
27#include "impeg2_buf_mgr.h"
28#include "impeg2_disp_mgr.h"
29#include "impeg2_defs.h"
30#include "impeg2_inter_pred.h"
31#include "impeg2_idct.h"
32#include "impeg2_format_conv.h"
33#include "impeg2_mem_func.h"
34#include "impeg2_platform_macros.h"
35#include "ithread.h"
36#include "impeg2_job_queue.h"
37
38#include "impeg2d.h"
39#include "impeg2d_bitstream.h"
40#include "impeg2d_api.h"
41#include "impeg2d_structs.h"
42#include "impeg2_globals.h"
43#include "impeg2d_pic_proc.h"
44#include "impeg2d_deinterlace.h"
45
46
47
48/******************************************************************************
49*  Function Name   : impeg2d_next_start_code
50*
51*  Description     : Peek for next_start_code from the stream_t.
52*
53*  Arguments       :
54*  dec             : Decoder Context
55*
56*  Values Returned : None
57******************************************************************************/
58void impeg2d_next_start_code(dec_state_t *ps_dec)
59{
60    stream_t *ps_stream;
61    ps_stream = &ps_dec->s_bit_stream;
62    impeg2d_bit_stream_flush_to_byte_boundary(ps_stream);
63
64    while ((impeg2d_bit_stream_nxt(ps_stream,START_CODE_PREFIX_LEN) != START_CODE_PREFIX)
65        && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
66    {
67        impeg2d_bit_stream_get(ps_stream,8);
68    }
69    return;
70}
71/******************************************************************************
72*  Function Name   : impeg2d_next_code
73*
74*  Description     : Peek for next_start_code from the stream_t.
75*
76*  Arguments       :
77*  dec             : Decoder Context
78*
79*  Values Returned : None
80******************************************************************************/
81void impeg2d_next_code(dec_state_t *ps_dec, UWORD32 u4_start_code_val)
82{
83    stream_t *ps_stream;
84    ps_stream = &ps_dec->s_bit_stream;
85    impeg2d_bit_stream_flush_to_byte_boundary(ps_stream);
86
87    while ((impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) != u4_start_code_val) &&
88            (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
89    {
90
91        if (impeg2d_bit_stream_get(ps_stream,8) != 0)
92        {
93            /* Ignore stuffing bit errors. */
94        }
95
96    }
97    return;
98}
99/******************************************************************************
100*  Function Name   : impeg2d_peek_next_start_code
101*
102*  Description     : Peek for next_start_code from the stream_t.
103*
104*  Arguments       :
105*  dec             : Decoder Context
106*
107*  Values Returned : None
108******************************************************************************/
109void impeg2d_peek_next_start_code(dec_state_t *ps_dec)
110{
111    stream_t *ps_stream;
112    ps_stream = &ps_dec->s_bit_stream;
113    impeg2d_bit_stream_flush_to_byte_boundary(ps_stream);
114
115    while ((impeg2d_bit_stream_nxt(ps_stream,START_CODE_PREFIX_LEN) != START_CODE_PREFIX)
116        && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
117    {
118        impeg2d_bit_stream_get(ps_stream,8);
119    }
120    return;
121}
122/******************************************************************************
123*
124*  Function Name   : impeg2d_dec_seq_hdr
125*
126*  Description     : Decodes Sequence header information
127*
128*  Arguments       :
129*  dec             : Decoder Context
130*
131*  Values Returned : None
132******************************************************************************/
133IMPEG2D_ERROR_CODES_T impeg2d_dec_seq_hdr(dec_state_t *ps_dec)
134{
135    stream_t *ps_stream;
136    ps_stream = &ps_dec->s_bit_stream;
137    UWORD16 u2_height;
138    UWORD16 u2_width;
139
140    if (impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) != SEQUENCE_HEADER_CODE)
141    {
142        impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
143        return IMPEG2D_FRM_HDR_START_CODE_NOT_FOUND;
144
145    }
146    impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
147
148    u2_width    = impeg2d_bit_stream_get(ps_stream,12);
149    u2_height   = impeg2d_bit_stream_get(ps_stream,12);
150
151    if ((u2_width != ps_dec->u2_horizontal_size)
152                    || (u2_height != ps_dec->u2_vertical_size))
153    {
154        if (0 == ps_dec->u2_header_done)
155        {
156            /* This is the first time we are reading the resolution */
157            ps_dec->u2_horizontal_size = u2_width;
158            ps_dec->u2_vertical_size = u2_height;
159            if (0 == ps_dec->u4_frm_buf_stride)
160            {
161                ps_dec->u4_frm_buf_stride  = (UWORD32) (u2_width);
162            }
163        }
164        else
165        {
166            if((u2_width > ps_dec->u2_create_max_width)
167                            || (u2_height > ps_dec->u2_create_max_height))
168            {
169                IMPEG2D_ERROR_CODES_T e_error = IMPEG2D_UNSUPPORTED_DIMENSIONS;
170
171                ps_dec->u2_reinit_max_height   = u2_height;
172                ps_dec->u2_reinit_max_width    = u2_width;
173
174                return e_error;
175            }
176            else
177            {
178                /* The resolution has changed */
179                return (IMPEG2D_ERROR_CODES_T)IVD_RES_CHANGED;
180            }
181        }
182    }
183
184    if((ps_dec->u2_horizontal_size > ps_dec->u2_create_max_width)
185                    || (ps_dec->u2_vertical_size > ps_dec->u2_create_max_height))
186    {
187        IMPEG2D_ERROR_CODES_T e_error = IMPEG2D_UNSUPPORTED_DIMENSIONS;
188        return SET_IVD_FATAL_ERROR(e_error);
189    }
190
191
192    /*------------------------------------------------------------------------*/
193    /* Flush the following as they are not being used                         */
194    /* aspect_ratio_info (4 bits)                                             */
195    /*------------------------------------------------------------------------*/
196    ps_dec->u2_aspect_ratio_info = impeg2d_bit_stream_get(ps_stream,4);
197
198    /*------------------------------------------------------------------------*/
199    /* Frame rate code(4 bits)                                                */
200    /*------------------------------------------------------------------------*/
201    ps_dec->u2_frame_rate_code = impeg2d_bit_stream_get(ps_stream,4);
202    /*------------------------------------------------------------------------*/
203    /* Flush the following as they are not being used                         */
204    /* bit_rate_value (18 bits)                                               */
205    /*------------------------------------------------------------------------*/
206    impeg2d_bit_stream_flush(ps_stream,18);
207    GET_MARKER_BIT(ps_dec,ps_stream);
208    /*------------------------------------------------------------------------*/
209    /* Flush the following as they are not being used                         */
210    /* vbv_buffer_size_value(10 bits), constrained_parameter_flag (1 bit)     */
211    /*------------------------------------------------------------------------*/
212    impeg2d_bit_stream_flush(ps_stream,11);
213
214    /*------------------------------------------------------------------------*/
215    /* Quantization matrix for the intra blocks                               */
216    /*------------------------------------------------------------------------*/
217    if(impeg2d_bit_stream_get_bit(ps_stream) == 1)
218    {
219        UWORD16 i;
220        for(i = 0; i < NUM_PELS_IN_BLOCK; i++)
221        {
222            ps_dec->au1_intra_quant_matrix[gau1_impeg2_inv_scan_zig_zag[i]] =  (UWORD8)impeg2d_bit_stream_get(ps_stream,8);
223        }
224
225    }
226    else
227    {
228        memcpy(ps_dec->au1_intra_quant_matrix,gau1_impeg2_intra_quant_matrix_default,
229                NUM_PELS_IN_BLOCK);
230    }
231
232    /*------------------------------------------------------------------------*/
233    /* Quantization matrix for the inter blocks                               */
234    /*------------------------------------------------------------------------*/
235    if(impeg2d_bit_stream_get_bit(ps_stream) == 1)
236    {
237        UWORD16 i;
238        for(i = 0; i < NUM_PELS_IN_BLOCK; i++)
239        {
240            ps_dec->au1_inter_quant_matrix[gau1_impeg2_inv_scan_zig_zag[i]] =   (UWORD8)impeg2d_bit_stream_get(ps_stream,8);
241        }
242    }
243    else
244    {
245        memcpy(ps_dec->au1_inter_quant_matrix,gau1_impeg2_inter_quant_matrix_default,
246            NUM_PELS_IN_BLOCK);
247    }
248    impeg2d_next_start_code(ps_dec);
249
250    return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
251}
252
253/******************************************************************************
254*
255*  Function Name   : impeg2d_dec_seq_ext
256*
257*  Description     : Gets additional sequence data.
258*
259*  Arguments       :
260*  dec             : Decoder Context
261*
262*  Values Returned : None
263******************************************************************************/
264IMPEG2D_ERROR_CODES_T impeg2d_dec_seq_ext(dec_state_t *ps_dec)
265{
266    stream_t *ps_stream;
267
268    ps_stream = &ps_dec->s_bit_stream;
269
270    if (impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) != EXTENSION_START_CODE)
271    {
272        impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
273        return IMPEG2D_FRM_HDR_START_CODE_NOT_FOUND;
274
275    }
276    /* Flush the extension start code */
277    impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
278
279    /* Flush extension start code identifier */
280    impeg2d_bit_stream_flush(ps_stream,4);
281
282    /*----------------------------------------------------------------------*/
283    /* Profile and Level information                                        */
284    /*----------------------------------------------------------------------*/
285    {
286        UWORD32   u4_esc_bit, u4_profile, u4_level;
287
288        /* Read the profile and level information */
289        /* check_profile_and_level: Table 8-1     */
290        /* [7:7] 1 Escape bit                     */
291        /* [6:4] 3 Profile identification         */
292        /* [3:0] 4 Level identification           */
293
294        u4_esc_bit   = impeg2d_bit_stream_get_bit(ps_stream);
295        u4_profile   = impeg2d_bit_stream_get(ps_stream,3);
296        u4_level     = impeg2d_bit_stream_get(ps_stream,4);
297        UNUSED(u4_profile);
298        UNUSED(u4_level);
299        /*
300        if( escBit == 1                   ||
301            profile < MPEG2_MAIN_PROFILE  ||
302            level < MPEG2_MAIN_LEVEL)
303            */
304        if (1 == u4_esc_bit)
305        {
306            return IMPEG2D_PROF_LEVEL_NOT_SUPPORTED;
307        }
308    }
309
310    ps_dec->u2_progressive_sequence = impeg2d_bit_stream_get_bit(ps_stream);
311
312    /* Read the chrominance format */
313    if(impeg2d_bit_stream_get(ps_stream,2) != 0x1)
314        return IMPEG2D_CHROMA_FMT_NOT_SUP;
315
316    /* Read the 2 most significant bits from horizontal_size */
317    ps_dec->u2_horizontal_size    += (impeg2d_bit_stream_get(ps_stream,2) << 12);
318
319    /* Read the 2 most significant bits from vertical_size */
320    ps_dec->u2_vertical_size      += (impeg2d_bit_stream_get(ps_stream,2) << 12);
321
322    /*-----------------------------------------------------------------------*/
323    /* Flush the following as they are not used now                          */
324    /* bit_rate_extension          12                                        */
325    /* marker_bit                   1                                        */
326    /* vbv_buffer_size_extension    8                                        */
327    /* low_delay                    1                                        */
328    /*-----------------------------------------------------------------------*/
329    impeg2d_bit_stream_flush(ps_stream,12);
330    GET_MARKER_BIT(ps_dec,ps_stream);
331    impeg2d_bit_stream_flush(ps_stream,9);
332    /*-----------------------------------------------------------------------*/
333    /* frame_rate_extension_n       2                                        */
334    /* frame_rate_extension_d       5                                        */
335    /*-----------------------------------------------------------------------*/
336    ps_dec->u2_frame_rate_extension_n = impeg2d_bit_stream_get(ps_stream,2);
337    ps_dec->u2_frame_rate_extension_d = impeg2d_bit_stream_get(ps_stream,5);
338
339    return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
340}
341
342/*******************************************************************************
343*
344*  Function Name   : impeg2d_dec_seq_disp_ext
345*
346*  Description     : This function is eqvt to sequence_display_extension() of
347*                    standard. It flushes data present as it is not being used
348*
349*  Arguments       :
350*  dec             : Decoder Context
351*
352*  Values Returned : None
353******************************************************************************/
354void impeg2d_dec_seq_disp_ext(dec_state_t *ps_dec)
355{
356    stream_t *ps_stream;
357    ps_stream = &ps_dec->s_bit_stream;
358
359    /*
360    sequence_display_extension()
361    {
362        extension_start_code_identifier 4
363        video_format                    3
364        colour_description              1
365        if (colour_description)
366        {
367            colour_primaries            8
368            transfer_characteristics    8
369            matrix_coefficients         8
370        }
371        display_horizontal_size         14
372        marker_bit                      1
373        display_vertical_size           14
374        next_start_code()
375    }
376    */
377
378    impeg2d_bit_stream_get(ps_stream,7);
379    if (impeg2d_bit_stream_get_bit(ps_stream) == 1)
380    {
381        impeg2d_bit_stream_get(ps_stream,24);
382    }
383
384    /* display_horizontal_size and display_vertical_size */
385    ps_dec->u2_display_horizontal_size = impeg2d_bit_stream_get(ps_stream,14);;
386    GET_MARKER_BIT(ps_dec,ps_stream);
387    ps_dec->u2_display_vertical_size   = impeg2d_bit_stream_get(ps_stream,14);
388
389    impeg2d_next_start_code(ps_dec);
390}
391
392
393/*******************************************************************************
394*
395*  Function Name   : impeg2d_dec_seq_scale_ext
396*
397*  Description     : This function is eqvt to sequence_scalable_extension() of
398*                    standard.
399*
400*  Arguments       : Decoder context
401*
402*  Values Returned : None
403*******************************************************************************/
404IMPEG2D_ERROR_CODES_T impeg2d_dec_seq_scale_ext(dec_state_t *ps_dec)
405{
406    UNUSED(ps_dec);
407    return IMPEG2D_SCALABILITIY_NOT_SUPPORTED;
408}
409
410/*******************************************************************************
411*
412*  Function Name   : impeg2d_dec_quant_matrix_ext
413*
414*  Description     : Gets Intra and NonIntra quantizer matrix from the stream.
415*
416*  Arguments       : Decoder context
417*
418*  Values Returned : None
419*******************************************************************************/
420void impeg2d_dec_quant_matrix_ext(dec_state_t *ps_dec)
421{
422    stream_t *ps_stream;
423
424    ps_stream = &ps_dec->s_bit_stream;
425    /* Flush extension_start_code_identifier */
426    impeg2d_bit_stream_flush(ps_stream,4);
427
428    /*------------------------------------------------------------------------*/
429    /* Quantization matrix for the intra blocks                               */
430    /*------------------------------------------------------------------------*/
431    if(impeg2d_bit_stream_get(ps_stream,1) == 1)
432    {
433        UWORD16 i;
434        for(i = 0; i < NUM_PELS_IN_BLOCK; i++)
435        {
436            ps_dec->au1_intra_quant_matrix[gau1_impeg2_inv_scan_zig_zag[i]] =  (UWORD8)impeg2d_bit_stream_get(ps_stream,8);
437        }
438
439    }
440
441
442    /*------------------------------------------------------------------------*/
443    /* Quantization matrix for the inter blocks                               */
444    /*------------------------------------------------------------------------*/
445    if(impeg2d_bit_stream_get(ps_stream,1) == 1)
446    {
447        UWORD16 i;
448        for(i = 0; i < NUM_PELS_IN_BLOCK; i++)
449        {
450            ps_dec->au1_inter_quant_matrix[gau1_impeg2_inv_scan_zig_zag[i]] =   (UWORD8)impeg2d_bit_stream_get(ps_stream,8);
451        }
452    }
453
454    /* Note : chroma intra quantizer matrix and chroma non
455    intra quantizer matrix are not needed for 4:2:0 format */
456    impeg2d_next_start_code(ps_dec);
457}
458/*******************************************************************************
459*
460*  Function Name   : impeg2d_dec_pic_disp_ext
461*
462*  Description     : This function is eqvt to picture_display_extension() of
463*                    standard.The parameters are not used by decoder
464*
465*  Arguments       : Pointer to dec_state_t
466*
467*  Values Returned : Decoder context
468*
469*  Values Returned : None
470*******************************************************************************/
471void impeg2d_dec_pic_disp_ext(dec_state_t *ps_dec)
472{
473    WORD16 i2_number_of_frame_centre_offsets ;
474    stream_t *ps_stream;
475
476    ps_stream = &ps_dec->s_bit_stream;
477    impeg2d_bit_stream_flush(ps_stream,4);
478
479    if (ps_dec->u2_progressive_sequence)
480    {
481        i2_number_of_frame_centre_offsets = (ps_dec->u2_repeat_first_field) ?
482            2 + ps_dec->u2_top_field_first : 1;
483    }
484    else
485    {
486        i2_number_of_frame_centre_offsets =
487            (ps_dec->u2_picture_structure != FRAME_PICTURE) ?
488            1 : 2 + ps_dec->u2_repeat_first_field;
489    }
490    while(i2_number_of_frame_centre_offsets--)
491    {
492        /* frame_centre_horizontal_offset */
493        impeg2d_bit_stream_get(ps_stream,16);
494        GET_MARKER_BIT(ps_dec,ps_stream);
495        /* frame_centre_vertical_offset */
496        impeg2d_bit_stream_get(ps_stream,16);
497        GET_MARKER_BIT(ps_dec,ps_stream);
498    }
499    impeg2d_next_start_code(ps_dec);
500}
501
502/*******************************************************************************
503*
504*  Function Name   : impeg2d_dec_itu_t_ext
505*
506*  Description     : This function is eqvt to ITU-T_extension() of
507*                    standard.The parameters are not used by decoder
508*
509*  Arguments       : Decoder context
510*
511*  Values Returned : None
512*******************************************************************************/
513void impeg2d_dec_itu_t_ext(dec_state_t *ps_dec)
514{
515  impeg2d_bit_stream_flush(&ps_dec->s_bit_stream,EXT_ID_LEN);
516  impeg2d_next_start_code(ps_dec);
517}
518
519/*******************************************************************************
520*  Function Name   : impeg2d_dec_copyright_ext
521*
522*  Description     : This function is eqvt to copyright_extension() of
523*                    standard. The parameters are not used by decoder
524*
525*  Arguments       : Decoder context
526*
527*  Values Returned : None
528*******************************************************************************/
529
530
531void impeg2d_dec_copyright_ext(dec_state_t *ps_dec)
532{
533    UWORD32 u4_bits_to_flush;
534
535    u4_bits_to_flush = COPYRIGHT_EXTENSION_LEN;
536
537    while(u4_bits_to_flush >= 32 )
538    {
539        impeg2d_bit_stream_flush(&ps_dec->s_bit_stream,32);
540        u4_bits_to_flush = u4_bits_to_flush - 32;
541    }
542
543    if(u4_bits_to_flush > 0)
544    {
545        impeg2d_bit_stream_flush(&ps_dec->s_bit_stream,u4_bits_to_flush);
546    }
547
548
549  impeg2d_next_start_code(ps_dec);
550}
551/*******************************************************************************
552*  Function Name   : impeg2d_dec_cam_param_ext
553*
554*  Description     : This function is eqvt to camera_parameters_extension() of
555*                    standard. The parameters are not used by decoder
556*
557*  Arguments       : Decoder context
558*
559*  Values Returned : None
560*******************************************************************************/
561
562
563void impeg2d_dec_cam_param_ext(dec_state_t *ps_dec)
564{
565
566    UWORD32 u4_bits_to_flush;
567
568    u4_bits_to_flush = CAMERA_PARAMETER_EXTENSION_LEN;
569
570    while(u4_bits_to_flush >= 32 )
571    {
572        impeg2d_bit_stream_flush(&ps_dec->s_bit_stream,32);
573        u4_bits_to_flush = u4_bits_to_flush - 32;
574    }
575
576    if(u4_bits_to_flush > 0)
577    {
578        impeg2d_bit_stream_flush(&ps_dec->s_bit_stream,u4_bits_to_flush);
579    }
580
581  impeg2d_next_start_code(ps_dec);
582}
583
584/*******************************************************************************
585*
586*  Function Name   : impeg2d_dec_grp_of_pic_hdr
587*
588*  Description     : Gets information at the GOP level.
589*
590*  Arguments       : Decoder context
591*
592*  Values Returned : None
593*******************************************************************************/
594
595
596void impeg2d_dec_grp_of_pic_hdr(dec_state_t *ps_dec)
597{
598
599    UWORD32 u4_bits_to_flush;
600
601    u4_bits_to_flush = GROUP_OF_PICTURE_LEN;
602
603    while(u4_bits_to_flush >= 32 )
604    {
605        impeg2d_bit_stream_flush(&ps_dec->s_bit_stream,32);
606        u4_bits_to_flush = u4_bits_to_flush - 32;
607    }
608
609    if(u4_bits_to_flush > 0)
610    {
611        impeg2d_bit_stream_flush(&ps_dec->s_bit_stream,u4_bits_to_flush);
612    }
613
614}
615
616
617/*******************************************************************************
618*
619*  Function Name   : impeg2d_dec_pic_hdr
620*
621*  Description     : Gets the picture header information.
622*
623*  Arguments       : Decoder context
624*
625*  Values Returned : None
626*******************************************************************************/
627IMPEG2D_ERROR_CODES_T impeg2d_dec_pic_hdr(dec_state_t *ps_dec)
628{
629    stream_t *ps_stream;
630    ps_stream = &ps_dec->s_bit_stream;
631
632    impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
633    /* Flush temporal reference */
634    impeg2d_bit_stream_get(ps_stream,10);
635
636    /* Picture type */
637    ps_dec->e_pic_type = (e_pic_type_t)impeg2d_bit_stream_get(ps_stream,3);
638    if((ps_dec->e_pic_type < I_PIC) || (ps_dec->e_pic_type > D_PIC))
639    {
640        impeg2d_next_code(ps_dec, PICTURE_START_CODE);
641        return IMPEG2D_INVALID_PIC_TYPE;
642    }
643
644    /* Flush vbv_delay */
645    impeg2d_bit_stream_get(ps_stream,16);
646
647    if(ps_dec->e_pic_type == P_PIC || ps_dec->e_pic_type == B_PIC)
648    {
649        ps_dec->u2_full_pel_forw_vector = impeg2d_bit_stream_get_bit(ps_stream);
650        ps_dec->u2_forw_f_code          = impeg2d_bit_stream_get(ps_stream,3);
651    }
652    if(ps_dec->e_pic_type == B_PIC)
653    {
654        ps_dec->u2_full_pel_back_vector = impeg2d_bit_stream_get_bit(ps_stream);
655        ps_dec->u2_back_f_code          = impeg2d_bit_stream_get(ps_stream,3);
656    }
657
658    if(ps_dec->u2_is_mpeg2 == 0)
659    {
660        ps_dec->au2_f_code[0][0] = ps_dec->au2_f_code[0][1] = ps_dec->u2_forw_f_code;
661        ps_dec->au2_f_code[1][0] = ps_dec->au2_f_code[1][1] = ps_dec->u2_back_f_code;
662    }
663
664    /*-----------------------------------------------------------------------*/
665    /*  Flush the extra bit value                                            */
666    /*                                                                       */
667    /*  while(impeg2d_bit_stream_nxt() == '1')                                  */
668    /*  {                                                                    */
669    /*      extra_bit_picture         1                                      */
670    /*      extra_information_picture 8                                      */
671    /*  }                                                                    */
672    /*  extra_bit_picture             1                                      */
673    /*-----------------------------------------------------------------------*/
674    while (impeg2d_bit_stream_nxt(ps_stream,1) == 1 &&
675           ps_stream->u4_offset < ps_stream->u4_max_offset)
676    {
677        impeg2d_bit_stream_get(ps_stream,9);
678    }
679    impeg2d_bit_stream_get_bit(ps_stream);
680    impeg2d_next_start_code(ps_dec);
681
682    return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
683}
684
685
686/*******************************************************************************
687*
688*  Function Name   : impeg2d_dec_pic_coding_ext
689*
690*  Description     : Reads more picture level parameters
691*
692*  Arguments       :
693*  dec             : Decoder context
694*
695*  Values Returned : None
696*******************************************************************************/
697void impeg2d_dec_pic_coding_ext(dec_state_t *ps_dec)
698{
699    stream_t *ps_stream;
700
701    ps_stream = &ps_dec->s_bit_stream;
702    impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
703    /* extension code identifier */
704    impeg2d_bit_stream_get(ps_stream,4);
705
706    ps_dec->au2_f_code[0][0]             = impeg2d_bit_stream_get(ps_stream,4);
707    ps_dec->au2_f_code[0][1]             = impeg2d_bit_stream_get(ps_stream,4);
708    ps_dec->au2_f_code[1][0]             = impeg2d_bit_stream_get(ps_stream,4);
709    ps_dec->au2_f_code[1][1]             = impeg2d_bit_stream_get(ps_stream,4);
710    ps_dec->u2_intra_dc_precision        = impeg2d_bit_stream_get(ps_stream,2);
711    ps_dec->u2_picture_structure            = impeg2d_bit_stream_get(ps_stream,2);
712    ps_dec->u2_top_field_first              = impeg2d_bit_stream_get_bit(ps_stream);
713    ps_dec->u2_frame_pred_frame_dct         = impeg2d_bit_stream_get_bit(ps_stream);
714    ps_dec->u2_concealment_motion_vectors   = impeg2d_bit_stream_get_bit(ps_stream);
715    ps_dec->u2_q_scale_type                 = impeg2d_bit_stream_get_bit(ps_stream);
716    ps_dec->u2_intra_vlc_format             = impeg2d_bit_stream_get_bit(ps_stream);
717    ps_dec->u2_alternate_scan               = impeg2d_bit_stream_get_bit(ps_stream);
718    ps_dec->u2_repeat_first_field           = impeg2d_bit_stream_get_bit(ps_stream);
719    /* Flush chroma_420_type */
720    impeg2d_bit_stream_get_bit(ps_stream);
721
722    ps_dec->u2_progressive_frame            = impeg2d_bit_stream_get_bit(ps_stream);
723    if (impeg2d_bit_stream_get_bit(ps_stream))
724    {
725        /* Flush v_axis, field_sequence, burst_amplitude, sub_carrier_phase */
726        impeg2d_bit_stream_flush(ps_stream,20);
727    }
728    impeg2d_next_start_code(ps_dec);
729
730
731    if(VERTICAL_SCAN == ps_dec->u2_alternate_scan)
732    {
733        ps_dec->pu1_inv_scan_matrix = (UWORD8 *)gau1_impeg2_inv_scan_vertical;
734    }
735    else
736    {
737        ps_dec->pu1_inv_scan_matrix = (UWORD8 *)gau1_impeg2_inv_scan_zig_zag;
738    }
739}
740
741/*******************************************************************************
742*
743*  Function Name   : impeg2d_dec_slice
744*
745*  Description     : Reads Slice level parameters and calls functions that
746*                    decode individual MBs of slice
747*
748*  Arguments       :
749*  dec             : Decoder context
750*
751*  Values Returned : None
752*******************************************************************************/
753IMPEG2D_ERROR_CODES_T impeg2d_dec_slice(dec_state_t *ps_dec)
754{
755    stream_t *ps_stream;
756    UWORD32 u4_slice_vertical_position;
757    UWORD32 u4_slice_vertical_position_extension;
758    IMPEG2D_ERROR_CODES_T e_error;
759
760    ps_stream = &ps_dec->s_bit_stream;
761
762    /*------------------------------------------------------------------------*/
763    /* All the profiles supported require restricted slice structure. Hence   */
764    /* there is no need to store slice_vertical_position. Note that max       */
765    /* height supported does not exceed 2800 and scalablity is not supported  */
766    /*------------------------------------------------------------------------*/
767
768    /* Remove the slice start code */
769    impeg2d_bit_stream_flush(ps_stream,START_CODE_PREFIX_LEN);
770    u4_slice_vertical_position = impeg2d_bit_stream_get(ps_stream, 8);
771    if(u4_slice_vertical_position > 2800)
772    {
773        u4_slice_vertical_position_extension = impeg2d_bit_stream_get(ps_stream, 3);
774        u4_slice_vertical_position += (u4_slice_vertical_position_extension << 7);
775    }
776
777    if((u4_slice_vertical_position > ps_dec->u2_num_vert_mb) ||
778       (u4_slice_vertical_position == 0))
779    {
780        return IMPEG2D_INVALID_VERT_SIZE;
781    }
782
783    // change the mb_y to point to slice_vertical_position
784    u4_slice_vertical_position--;
785    if (ps_dec->u2_mb_y != u4_slice_vertical_position)
786    {
787        ps_dec->u2_mb_y    = u4_slice_vertical_position;
788        ps_dec->u2_mb_x    = 0;
789    }
790    ps_dec->u2_first_mb = 1;
791
792    /*------------------------------------------------------------------------*/
793    /* Quant scale code decoding                                              */
794    /*------------------------------------------------------------------------*/
795    {
796        UWORD16 u2_quant_scale_code;
797        u2_quant_scale_code = impeg2d_bit_stream_get(ps_stream,5);
798        ps_dec->u1_quant_scale = (ps_dec->u2_q_scale_type) ?
799            gau1_impeg2_non_linear_quant_scale[u2_quant_scale_code] : (u2_quant_scale_code << 1);
800    }
801
802    if (impeg2d_bit_stream_nxt(ps_stream,1) == 1)
803    {
804        impeg2d_bit_stream_flush(ps_stream,9);
805        /* Flush extra bit information */
806        while (impeg2d_bit_stream_nxt(ps_stream,1) == 1 &&
807               ps_stream->u4_offset < ps_stream->u4_max_offset)
808        {
809            impeg2d_bit_stream_flush(ps_stream,9);
810        }
811    }
812    impeg2d_bit_stream_get_bit(ps_stream);
813
814    /* Reset the DC predictors to reset values given in Table 7.2 at the start*/
815    /* of slice data */
816    ps_dec->u2_def_dc_pred[Y_LUMA]   = 128 << ps_dec->u2_intra_dc_precision;
817    ps_dec->u2_def_dc_pred[U_CHROMA]   = 128 << ps_dec->u2_intra_dc_precision;
818    ps_dec->u2_def_dc_pred[V_CHROMA]   = 128 << ps_dec->u2_intra_dc_precision;
819    /*------------------------------------------------------------------------*/
820    /* dec->DecMBsinSlice() implements the following psuedo code from standard*/
821    /* do                                                                     */
822    /* {                                                                      */
823    /*      macroblock()                                                      */
824    /* } while (impeg2d_bit_stream_nxt() != '000 0000 0000 0000 0000 0000')      */
825    /*------------------------------------------------------------------------*/
826
827    e_error = ps_dec->pf_decode_slice(ps_dec);
828    if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
829    {
830        return e_error;
831    }
832
833    /* Check for the MBy index instead of number of MBs left, because the
834     * number of MBs left in case of multi-thread decode is the number of MBs
835     * in that row only
836     */
837    if(ps_dec->u2_mb_y < ps_dec->u2_num_vert_mb)
838        impeg2d_next_start_code(ps_dec);
839
840    return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
841}
842
843void impeg2d_dec_pic_data_thread(dec_state_t *ps_dec)
844{
845    WORD32 i4_continue_decode;
846
847    WORD32 i4_cur_row, temp;
848    UWORD32 u4_bits_read;
849    WORD32 i4_dequeue_job;
850    IMPEG2D_ERROR_CODES_T e_error;
851
852    i4_cur_row = ps_dec->u2_mb_y + 1;
853
854    i4_continue_decode = 1;
855
856    i4_dequeue_job = 1;
857    do
858    {
859        if(i4_cur_row > ps_dec->u2_num_vert_mb)
860        {
861            i4_continue_decode = 0;
862            break;
863        }
864
865        {
866            if((ps_dec->i4_num_cores> 1) && (i4_dequeue_job))
867            {
868                job_t s_job;
869                IV_API_CALL_STATUS_T e_ret;
870                UWORD8 *pu1_buf;
871
872                e_ret = impeg2_jobq_dequeue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 1);
873                if(e_ret != IV_SUCCESS)
874                    break;
875
876                if(CMD_PROCESS == s_job.i4_cmd)
877                {
878                    pu1_buf = ps_dec->pu1_inp_bits_buf + s_job.i4_bistream_ofst;
879                    impeg2d_bit_stream_init(&(ps_dec->s_bit_stream), pu1_buf,
880                            (ps_dec->u4_num_inp_bytes - s_job.i4_bistream_ofst) + 8);
881                    i4_cur_row      = s_job.i2_start_mb_y;
882                    ps_dec->i4_start_mb_y = s_job.i2_start_mb_y;
883                    ps_dec->i4_end_mb_y = s_job.i2_end_mb_y;
884                    ps_dec->u2_mb_x = 0;
885                    ps_dec->u2_mb_y = ps_dec->i4_start_mb_y;
886                    ps_dec->u2_num_mbs_left = (ps_dec->i4_end_mb_y - ps_dec->i4_start_mb_y) * ps_dec->u2_num_horiz_mb;
887
888                }
889                else
890                {
891                    WORD32 start_row;
892                    WORD32 num_rows;
893                    start_row = s_job.i2_start_mb_y << 4;
894                    num_rows = MIN((s_job.i2_end_mb_y << 4), ps_dec->u2_vertical_size);
895                    num_rows -= start_row;
896
897                    if(ps_dec->u4_deinterlace && (0 == ps_dec->u2_progressive_frame))
898                    {
899                        impeg2d_deinterlace(ps_dec,
900                                            ps_dec->ps_disp_pic,
901                                            ps_dec->ps_disp_frm_buf,
902                                            start_row,
903                                            num_rows);
904
905                    }
906                    else
907                    {
908                        impeg2d_format_convert(ps_dec, ps_dec->ps_disp_pic,
909                                               ps_dec->ps_disp_frm_buf,
910                                               start_row, num_rows);
911                    }
912                    break;
913
914                }
915
916            }
917            e_error = impeg2d_dec_slice(ps_dec);
918
919            if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
920            {
921                impeg2d_next_start_code(ps_dec);
922            }
923        }
924
925        /* Detecting next slice start code */
926        while(1)
927        {
928            // skip (dec->u4_num_cores-1) rows
929            u4_bits_read = impeg2d_bit_stream_nxt(&ps_dec->s_bit_stream,START_CODE_LEN);
930            temp = u4_bits_read & 0xFF;
931            i4_continue_decode = (((u4_bits_read >> 8) == 0x01) && (temp) && (temp <= 0xAF));
932
933            if (1 == ps_dec->i4_num_cores && 0 == ps_dec->u2_num_mbs_left)
934            {
935                i4_continue_decode = 0;
936                android_errorWriteLog(0x534e4554, "26070014");
937            }
938
939            if(i4_continue_decode)
940            {
941                /* If the slice is from the same row, then continue decoding without dequeue */
942                if((temp - 1) == i4_cur_row)
943                {
944                    i4_dequeue_job = 0;
945                    break;
946                }
947
948                if(temp < ps_dec->i4_end_mb_y)
949                {
950                    i4_cur_row = ps_dec->u2_mb_y;
951                }
952                else
953                {
954                    i4_dequeue_job = 1;
955                }
956                break;
957
958            }
959            else
960                break;
961        }
962
963    }while(i4_continue_decode);
964    if(ps_dec->i4_num_cores > 1)
965    {
966        while(1)
967        {
968            job_t s_job;
969            IV_API_CALL_STATUS_T e_ret;
970
971            e_ret = impeg2_jobq_dequeue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 1);
972            if(e_ret != IV_SUCCESS)
973                break;
974            if(CMD_FMTCONV == s_job.i4_cmd)
975            {
976                WORD32 start_row;
977                WORD32 num_rows;
978                start_row = s_job.i2_start_mb_y << 4;
979                num_rows = MIN((s_job.i2_end_mb_y << 4), ps_dec->u2_vertical_size);
980                num_rows -= start_row;
981                if(ps_dec->u4_deinterlace && (0 == ps_dec->u2_progressive_frame))
982                {
983                    impeg2d_deinterlace(ps_dec,
984                                        ps_dec->ps_disp_pic,
985                                        ps_dec->ps_disp_frm_buf,
986                                        start_row,
987                                        num_rows);
988
989                }
990                else
991                {
992                    impeg2d_format_convert(ps_dec,
993                                           ps_dec->ps_disp_pic,
994                                           ps_dec->ps_disp_frm_buf,
995                                           start_row,
996                                           num_rows);
997                }
998            }
999        }
1000    }
1001    else
1002    {
1003        if((NULL != ps_dec->ps_disp_pic) && ((0 == ps_dec->u4_share_disp_buf) || (IV_YUV_420P != ps_dec->i4_chromaFormat)))
1004        {
1005            if(ps_dec->u4_deinterlace && (0 == ps_dec->u2_progressive_frame))
1006            {
1007                impeg2d_deinterlace(ps_dec,
1008                                    ps_dec->ps_disp_pic,
1009                                    ps_dec->ps_disp_frm_buf,
1010                                    0,
1011                                    ps_dec->u2_vertical_size);
1012
1013            }
1014            else
1015            {
1016                impeg2d_format_convert(ps_dec, ps_dec->ps_disp_pic,
1017                                        ps_dec->ps_disp_frm_buf,
1018                                        0, ps_dec->u2_vertical_size);
1019            }
1020        }
1021    }
1022}
1023
1024static WORD32 impeg2d_init_thread_dec_ctxt(dec_state_t *ps_dec,
1025                                           dec_state_t *ps_dec_thd,
1026                                           WORD32 i4_min_mb_y)
1027{
1028    UNUSED(i4_min_mb_y);
1029    ps_dec_thd->i4_start_mb_y = 0;
1030    ps_dec_thd->i4_end_mb_y = ps_dec->u2_num_vert_mb;
1031    ps_dec_thd->u2_mb_x = 0;
1032    ps_dec_thd->u2_mb_y = 0;
1033    ps_dec_thd->u2_is_mpeg2 = ps_dec->u2_is_mpeg2;
1034    ps_dec_thd->u2_frame_width = ps_dec->u2_frame_width;
1035    ps_dec_thd->u2_frame_height = ps_dec->u2_frame_height;
1036    ps_dec_thd->u2_picture_width = ps_dec->u2_picture_width;
1037    ps_dec_thd->u2_horizontal_size = ps_dec->u2_horizontal_size;
1038    ps_dec_thd->u2_vertical_size = ps_dec->u2_vertical_size;
1039    ps_dec_thd->u2_create_max_width = ps_dec->u2_create_max_width;
1040    ps_dec_thd->u2_create_max_height = ps_dec->u2_create_max_height;
1041    ps_dec_thd->u2_header_done = ps_dec->u2_header_done;
1042    ps_dec_thd->u2_decode_header = ps_dec->u2_decode_header;
1043
1044    ps_dec_thd->u2_num_horiz_mb = ps_dec->u2_num_horiz_mb;
1045    ps_dec_thd->u2_num_vert_mb = ps_dec->u2_num_vert_mb;
1046    ps_dec_thd->u2_num_flds_decoded = ps_dec->u2_num_flds_decoded;
1047
1048    ps_dec_thd->u4_frm_buf_stride = ps_dec->u4_frm_buf_stride;
1049
1050    ps_dec_thd->u2_field_dct = ps_dec->u2_field_dct;
1051    ps_dec_thd->u2_read_dct_type = ps_dec->u2_read_dct_type;
1052
1053    ps_dec_thd->u2_read_motion_type = ps_dec->u2_read_motion_type;
1054    ps_dec_thd->u2_motion_type = ps_dec->u2_motion_type;
1055
1056    ps_dec_thd->pu2_mb_type = ps_dec->pu2_mb_type;
1057    ps_dec_thd->u2_fld_pic = ps_dec->u2_fld_pic;
1058    ps_dec_thd->u2_frm_pic = ps_dec->u2_frm_pic;
1059
1060    ps_dec_thd->u2_fld_parity = ps_dec->u2_fld_parity;
1061
1062    ps_dec_thd->au2_fcode_data[0] = ps_dec->au2_fcode_data[0];
1063    ps_dec_thd->au2_fcode_data[1] = ps_dec->au2_fcode_data[1];
1064
1065    ps_dec_thd->u1_quant_scale = ps_dec->u1_quant_scale;
1066
1067    ps_dec_thd->u2_num_mbs_left = ps_dec->u2_num_mbs_left;
1068    ps_dec_thd->u2_first_mb = ps_dec->u2_first_mb;
1069    ps_dec_thd->u2_num_skipped_mbs = ps_dec->u2_num_skipped_mbs;
1070
1071    memcpy(&ps_dec_thd->s_cur_frm_buf, &ps_dec->s_cur_frm_buf, sizeof(yuv_buf_t));
1072    memcpy(&ps_dec_thd->as_recent_fld[0][0], &ps_dec->as_recent_fld[0][0], sizeof(yuv_buf_t));
1073    memcpy(&ps_dec_thd->as_recent_fld[0][1], &ps_dec->as_recent_fld[0][1], sizeof(yuv_buf_t));
1074    memcpy(&ps_dec_thd->as_recent_fld[1][0], &ps_dec->as_recent_fld[1][0], sizeof(yuv_buf_t));
1075    memcpy(&ps_dec_thd->as_recent_fld[1][1], &ps_dec->as_recent_fld[1][1], sizeof(yuv_buf_t));
1076    memcpy(&ps_dec_thd->as_ref_buf, &ps_dec->as_ref_buf, sizeof(yuv_buf_t) * 2 * 2);
1077
1078
1079    ps_dec_thd->pf_decode_slice = ps_dec->pf_decode_slice;
1080
1081    ps_dec_thd->pf_vld_inv_quant = ps_dec->pf_vld_inv_quant;
1082
1083    memcpy(ps_dec_thd->pf_idct_recon, ps_dec->pf_idct_recon, sizeof(ps_dec->pf_idct_recon));
1084
1085    memcpy(ps_dec_thd->pf_mc, ps_dec->pf_mc, sizeof(ps_dec->pf_mc));
1086    ps_dec_thd->pf_interpolate = ps_dec->pf_interpolate;
1087    ps_dec_thd->pf_copy_mb = ps_dec->pf_copy_mb;
1088    ps_dec_thd->pf_fullx_halfy_8x8              =  ps_dec->pf_fullx_halfy_8x8;
1089    ps_dec_thd->pf_halfx_fully_8x8              =  ps_dec->pf_halfx_fully_8x8;
1090    ps_dec_thd->pf_halfx_halfy_8x8              =  ps_dec->pf_halfx_halfy_8x8;
1091    ps_dec_thd->pf_fullx_fully_8x8              =  ps_dec->pf_fullx_fully_8x8;
1092
1093    ps_dec_thd->pf_memset_8bit_8x8_block        =  ps_dec->pf_memset_8bit_8x8_block;
1094    ps_dec_thd->pf_memset_16bit_8x8_linear_block        =  ps_dec->pf_memset_16bit_8x8_linear_block;
1095    ps_dec_thd->pf_copy_yuv420p_buf             =   ps_dec->pf_copy_yuv420p_buf;
1096    ps_dec_thd->pf_fmt_conv_yuv420p_to_yuv422ile    =   ps_dec->pf_fmt_conv_yuv420p_to_yuv422ile;
1097    ps_dec_thd->pf_fmt_conv_yuv420p_to_yuv420sp_uv  =   ps_dec->pf_fmt_conv_yuv420p_to_yuv420sp_uv;
1098    ps_dec_thd->pf_fmt_conv_yuv420p_to_yuv420sp_vu  =   ps_dec->pf_fmt_conv_yuv420p_to_yuv420sp_vu;
1099
1100
1101    memcpy(ps_dec_thd->au1_intra_quant_matrix, ps_dec->au1_intra_quant_matrix, NUM_PELS_IN_BLOCK * sizeof(UWORD8));
1102    memcpy(ps_dec_thd->au1_inter_quant_matrix, ps_dec->au1_inter_quant_matrix, NUM_PELS_IN_BLOCK * sizeof(UWORD8));
1103    ps_dec_thd->pu1_inv_scan_matrix = ps_dec->pu1_inv_scan_matrix;
1104
1105
1106    ps_dec_thd->u2_progressive_sequence = ps_dec->u2_progressive_sequence;
1107    ps_dec_thd->e_pic_type =  ps_dec->e_pic_type;
1108    ps_dec_thd->u2_full_pel_forw_vector = ps_dec->u2_full_pel_forw_vector;
1109    ps_dec_thd->u2_forw_f_code =   ps_dec->u2_forw_f_code;
1110    ps_dec_thd->u2_full_pel_back_vector = ps_dec->u2_full_pel_back_vector;
1111    ps_dec_thd->u2_back_f_code = ps_dec->u2_back_f_code;
1112
1113    memcpy(ps_dec_thd->ai2_mv, ps_dec->ai2_mv, (2*2*2)*sizeof(WORD16));
1114    memcpy(ps_dec_thd->au2_f_code, ps_dec->au2_f_code, (2*2)*sizeof(UWORD16));
1115    ps_dec_thd->u2_intra_dc_precision = ps_dec->u2_intra_dc_precision;
1116    ps_dec_thd->u2_picture_structure = ps_dec->u2_picture_structure;
1117    ps_dec_thd->u2_top_field_first = ps_dec->u2_top_field_first;
1118    ps_dec_thd->u2_frame_pred_frame_dct = ps_dec->u2_frame_pred_frame_dct;
1119    ps_dec_thd->u2_concealment_motion_vectors = ps_dec->u2_concealment_motion_vectors;
1120    ps_dec_thd->u2_q_scale_type =  ps_dec->u2_q_scale_type;
1121    ps_dec_thd->u2_intra_vlc_format = ps_dec->u2_intra_vlc_format;
1122    ps_dec_thd->u2_alternate_scan = ps_dec->u2_alternate_scan;
1123    ps_dec_thd->u2_repeat_first_field = ps_dec->u2_repeat_first_field;
1124    ps_dec_thd->u2_progressive_frame = ps_dec->u2_progressive_frame;
1125    ps_dec_thd->pu1_inp_bits_buf = ps_dec->pu1_inp_bits_buf;
1126    ps_dec_thd->u4_num_inp_bytes = ps_dec->u4_num_inp_bytes;
1127    ps_dec_thd->pv_jobq = ps_dec->pv_jobq;
1128    ps_dec_thd->pv_jobq_buf = ps_dec->pv_jobq_buf;
1129    ps_dec_thd->i4_jobq_buf_size = ps_dec->i4_jobq_buf_size;
1130
1131
1132    ps_dec_thd->u2_frame_rate_code = ps_dec->u2_frame_rate_code;
1133    ps_dec_thd->u2_frame_rate_extension_n = ps_dec->u2_frame_rate_extension_n;
1134    ps_dec_thd->u2_frame_rate_extension_d = ps_dec->u2_frame_rate_extension_d;
1135    ps_dec_thd->u2_framePeriod =   ps_dec->u2_framePeriod;
1136    ps_dec_thd->u2_display_horizontal_size = ps_dec->u2_display_horizontal_size;
1137    ps_dec_thd->u2_display_vertical_size = ps_dec->u2_display_vertical_size;
1138    ps_dec_thd->u2_aspect_ratio_info = ps_dec->u2_aspect_ratio_info;
1139
1140    ps_dec_thd->ps_func_bi_direct = ps_dec->ps_func_bi_direct;
1141    ps_dec_thd->ps_func_forw_or_back = ps_dec->ps_func_forw_or_back;
1142    ps_dec_thd->pv_deinterlacer_ctxt = ps_dec->pv_deinterlacer_ctxt;
1143    ps_dec_thd->ps_deint_pic = ps_dec->ps_deint_pic;
1144
1145    return 0;
1146}
1147
1148
1149WORD32 impeg2d_get_slice_pos(dec_state_multi_core_t *ps_dec_state_multi_core)
1150{
1151    WORD32 u4_bits;
1152    WORD32 i4_row;
1153
1154
1155    dec_state_t *ps_dec = ps_dec_state_multi_core->ps_dec_state[0];
1156    WORD32 i4_prev_row;
1157    stream_t s_bitstrm;
1158    WORD32 i4_start_row;
1159    WORD32 i4_slice_bistream_ofst;
1160    WORD32 i;
1161    s_bitstrm = ps_dec->s_bit_stream;
1162    i4_prev_row = -1;
1163
1164    ps_dec_state_multi_core->ps_dec_state[0]->i4_start_mb_y = 0;
1165    ps_dec_state_multi_core->ps_dec_state[1]->i4_start_mb_y = -1;
1166    ps_dec_state_multi_core->ps_dec_state[2]->i4_start_mb_y = -1;
1167    ps_dec_state_multi_core->ps_dec_state[3]->i4_start_mb_y = -1;
1168
1169    ps_dec_state_multi_core->ps_dec_state[0]->i4_end_mb_y = ps_dec->u2_num_vert_mb;
1170    ps_dec_state_multi_core->ps_dec_state[1]->i4_end_mb_y = -1;
1171    ps_dec_state_multi_core->ps_dec_state[2]->i4_end_mb_y = -1;
1172    ps_dec_state_multi_core->ps_dec_state[3]->i4_end_mb_y = -1;
1173
1174    if(ps_dec->i4_num_cores == 1)
1175        return 0;
1176    /* Reset the jobq to start of the jobq buffer */
1177    impeg2_jobq_reset((jobq_t *)ps_dec->pv_jobq);
1178
1179    i4_start_row = -1;
1180    i4_slice_bistream_ofst = 0;
1181    while(1)
1182    {
1183        WORD32 i4_is_slice;
1184
1185        if(s_bitstrm.u4_offset + START_CODE_LEN >= s_bitstrm.u4_max_offset)
1186        {
1187            break;
1188        }
1189        u4_bits = impeg2d_bit_stream_nxt(&s_bitstrm,START_CODE_LEN);
1190
1191        i4_row = u4_bits & 0xFF;
1192
1193        /* Detect end of frame */
1194        i4_is_slice = (((u4_bits >> 8) == 0x01) && (i4_row) && (i4_row <= ps_dec->u2_num_vert_mb));
1195        if(!i4_is_slice)
1196            break;
1197
1198        i4_row -= 1;
1199
1200
1201        if(i4_prev_row < i4_row)
1202        {
1203            /* Create a job for previous slice row */
1204            if(i4_start_row != -1)
1205            {
1206                job_t s_job;
1207                IV_API_CALL_STATUS_T ret;
1208                s_job.i2_start_mb_y = i4_start_row;
1209                s_job.i2_end_mb_y = i4_row;
1210                s_job.i4_cmd = CMD_PROCESS;
1211                s_job.i4_bistream_ofst = i4_slice_bistream_ofst;
1212                ret = impeg2_jobq_queue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 0);
1213                if(ret != IV_SUCCESS)
1214                    return ret;
1215
1216            }
1217            /* Store current slice's bitstream offset */
1218            i4_slice_bistream_ofst = s_bitstrm.u4_offset >> 3;
1219            i4_slice_bistream_ofst -= (size_t)s_bitstrm.pv_bs_buf & 3;
1220            i4_prev_row = i4_row;
1221
1222            /* Store current slice's row position */
1223            i4_start_row = i4_row;
1224
1225        } else if (i4_prev_row > i4_row) {
1226            android_errorWriteLog(0x534e4554, "26070014");
1227        }
1228
1229
1230        impeg2d_bit_stream_flush(&s_bitstrm, START_CODE_LEN);
1231
1232        // flush bytes till next start code
1233        /* Flush the bytes till a  start code is encountered  */
1234        while(impeg2d_bit_stream_nxt(&s_bitstrm, 24) != START_CODE_PREFIX)
1235        {
1236            impeg2d_bit_stream_get(&s_bitstrm, 8);
1237
1238            if(s_bitstrm.u4_offset >= s_bitstrm.u4_max_offset)
1239            {
1240                break;
1241            }
1242        }
1243    }
1244
1245    /* Create job for the last slice row */
1246    {
1247        job_t s_job;
1248        IV_API_CALL_STATUS_T e_ret;
1249        s_job.i2_start_mb_y = i4_start_row;
1250        s_job.i2_end_mb_y = ps_dec->u2_num_vert_mb;
1251        s_job.i4_cmd = CMD_PROCESS;
1252        s_job.i4_bistream_ofst = i4_slice_bistream_ofst;
1253        e_ret = impeg2_jobq_queue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 0);
1254        if(e_ret != IV_SUCCESS)
1255            return e_ret;
1256
1257    }
1258    if((NULL != ps_dec->ps_disp_pic) && ((0 == ps_dec->u4_share_disp_buf) || (IV_YUV_420P != ps_dec->i4_chromaFormat)))
1259    {
1260        for(i = 0; i < ps_dec->u2_vertical_size; i+=64)
1261        {
1262            job_t s_job;
1263            IV_API_CALL_STATUS_T ret;
1264            s_job.i2_start_mb_y = i;
1265            s_job.i2_start_mb_y >>= 4;
1266            s_job.i2_end_mb_y = (i + 64);
1267            s_job.i2_end_mb_y >>= 4;
1268            s_job.i4_cmd = CMD_FMTCONV;
1269            s_job.i4_bistream_ofst = 0;
1270            ret = impeg2_jobq_queue(ps_dec->pv_jobq, &s_job, sizeof(s_job), 1, 0);
1271            if(ret != IV_SUCCESS)
1272                return ret;
1273
1274        }
1275    }
1276
1277    impeg2_jobq_terminate(ps_dec->pv_jobq);
1278    ps_dec->i4_bytes_consumed = s_bitstrm.u4_offset >> 3;
1279    ps_dec->i4_bytes_consumed -= ((size_t)s_bitstrm.pv_bs_buf & 3);
1280
1281    return 0;
1282}
1283
1284/*******************************************************************************
1285*
1286*  Function Name   : impeg2d_dec_pic_data
1287*
1288*  Description     : It intializes several parameters and decodes a Picture
1289*                    till any slice is left.
1290*
1291*  Arguments       :
1292*  dec             : Decoder context
1293*
1294*  Values Returned : None
1295*******************************************************************************/
1296
1297void impeg2d_dec_pic_data(dec_state_t *ps_dec)
1298{
1299
1300    WORD32 i;
1301    dec_state_multi_core_t *ps_dec_state_multi_core;
1302
1303    UWORD32  u4_error_code;
1304
1305    dec_state_t *ps_dec_thd;
1306    WORD32 i4_status;
1307    WORD32 i4_min_mb_y;
1308
1309
1310    /* Resetting the MB address and MB coordinates at the start of the Frame */
1311    ps_dec->u2_mb_x = ps_dec->u2_mb_y = 0;
1312    u4_error_code = 0;
1313
1314    ps_dec_state_multi_core = ps_dec->ps_dec_state_multi_core;
1315    impeg2d_get_slice_pos(ps_dec_state_multi_core);
1316
1317    i4_min_mb_y = 1;
1318    for(i=0; i < ps_dec->i4_num_cores - 1; i++)
1319    {
1320        // initialize decoder context for thread
1321        // launch dec->u4_num_cores-1 threads
1322
1323        ps_dec_thd = ps_dec_state_multi_core->ps_dec_state[i+1];
1324
1325        ps_dec_thd->ps_disp_pic = ps_dec->ps_disp_pic;
1326        ps_dec_thd->ps_disp_frm_buf = ps_dec->ps_disp_frm_buf;
1327
1328        i4_status = impeg2d_init_thread_dec_ctxt(ps_dec, ps_dec_thd, i4_min_mb_y);
1329        //impeg2d_dec_pic_data_thread(ps_dec_thd);
1330
1331        if(i4_status == 0)
1332        {
1333            ithread_create(ps_dec_thd->pv_codec_thread_handle, NULL, (void *)impeg2d_dec_pic_data_thread, ps_dec_thd);
1334            ps_dec_state_multi_core->au4_thread_launched[i + 1] = 1;
1335            i4_min_mb_y = ps_dec_thd->u2_mb_y + 1;
1336        }
1337        else
1338        {
1339            ps_dec_state_multi_core->au4_thread_launched[i + 1] = 0;
1340            break;
1341        }
1342    }
1343
1344    impeg2d_dec_pic_data_thread(ps_dec);
1345
1346    // wait for threads to complete
1347    for(i=0; i < (ps_dec->i4_num_cores - 1); i++)
1348    {
1349        if(ps_dec_state_multi_core->au4_thread_launched[i + 1] == 1)
1350        {
1351            ps_dec_thd = ps_dec_state_multi_core->ps_dec_state[i+1];
1352            ithread_join(ps_dec_thd->pv_codec_thread_handle, NULL);
1353        }
1354    }
1355
1356    ps_dec->u4_error_code = u4_error_code;
1357
1358}
1359/*******************************************************************************
1360*
1361*  Function Name   : impeg2d_flush_ext_and_user_data
1362*
1363*  Description     : Flushes the extension and user data present in the
1364*                    stream_t
1365*
1366*  Arguments       :
1367*  dec             : Decoder context
1368*
1369*  Values Returned : None
1370*******************************************************************************/
1371void impeg2d_flush_ext_and_user_data(dec_state_t *ps_dec)
1372{
1373    UWORD32 u4_start_code;
1374    stream_t *ps_stream;
1375
1376    ps_stream    = &ps_dec->s_bit_stream;
1377    u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1378
1379    while((u4_start_code == EXTENSION_START_CODE || u4_start_code == USER_DATA_START_CODE) &&
1380            (ps_stream->u4_offset < ps_stream->u4_max_offset))
1381    {
1382        impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1383        while(impeg2d_bit_stream_nxt(ps_stream,START_CODE_PREFIX_LEN) != START_CODE_PREFIX &&
1384                (ps_stream->u4_offset < ps_stream->u4_max_offset))
1385        {
1386            impeg2d_bit_stream_flush(ps_stream,8);
1387        }
1388        u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1389    }
1390}
1391/*******************************************************************************
1392*
1393*  Function Name   : impeg2d_dec_user_data
1394*
1395*  Description     : Flushes the user data present in the stream_t
1396*
1397*  Arguments       :
1398*  dec             : Decoder context
1399*
1400*  Values Returned : None
1401*******************************************************************************/
1402void impeg2d_dec_user_data(dec_state_t *ps_dec)
1403{
1404    UWORD32 u4_start_code;
1405    stream_t *ps_stream;
1406
1407    ps_stream    = &ps_dec->s_bit_stream;
1408    u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1409
1410    while(u4_start_code == USER_DATA_START_CODE)
1411    {
1412        impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1413        while((impeg2d_bit_stream_nxt(ps_stream,START_CODE_PREFIX_LEN) != START_CODE_PREFIX) &&
1414                (ps_stream->u4_offset < ps_stream->u4_max_offset))
1415        {
1416            impeg2d_bit_stream_flush(ps_stream,8);
1417        }
1418        u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1419    }
1420}
1421/*******************************************************************************
1422*  Function Name   : impeg2d_dec_seq_ext_data
1423*
1424*  Description     : Decodes the extension data following Sequence
1425*                    Extension. It flushes any user data if present
1426*
1427*  Arguments       :
1428*  dec             : Decoder context
1429*
1430*  Values Returned : None
1431*******************************************************************************/
1432IMPEG2D_ERROR_CODES_T impeg2d_dec_seq_ext_data(dec_state_t *ps_dec)
1433{
1434    stream_t   *ps_stream;
1435    UWORD32     u4_start_code;
1436    IMPEG2D_ERROR_CODES_T e_error;
1437
1438    e_error = (IMPEG2D_ERROR_CODES_T) IVD_ERROR_NONE;
1439
1440    ps_stream      = &ps_dec->s_bit_stream;
1441    u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1442    while( (u4_start_code == EXTENSION_START_CODE ||
1443            u4_start_code == USER_DATA_START_CODE) &&
1444            (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE == e_error &&
1445            (ps_stream->u4_offset < ps_stream->u4_max_offset))
1446    {
1447        if(u4_start_code == USER_DATA_START_CODE)
1448        {
1449            impeg2d_dec_user_data(ps_dec);
1450        }
1451        else
1452        {
1453            impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1454            u4_start_code   = impeg2d_bit_stream_nxt(ps_stream,EXT_ID_LEN);
1455            switch(u4_start_code)
1456            {
1457            case SEQ_DISPLAY_EXT_ID:
1458                impeg2d_dec_seq_disp_ext(ps_dec);
1459                break;
1460            case SEQ_SCALABLE_EXT_ID:
1461                e_error = IMPEG2D_SCALABILITIY_NOT_SUPPORTED;
1462                break;
1463            default:
1464                /* In case its a reserved extension code */
1465                impeg2d_bit_stream_flush(ps_stream,EXT_ID_LEN);
1466                impeg2d_peek_next_start_code(ps_dec);
1467                break;
1468            }
1469        }
1470        u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1471    }
1472    return e_error;
1473}
1474/*******************************************************************************
1475*  Function Name   : impeg2d_dec_pic_ext_data
1476*
1477*  Description     : Decodes the extension data following Picture Coding
1478*                    Extension. It flushes any user data if present
1479*
1480*  Arguments       :
1481*  dec             : Decoder context
1482*
1483*  Values Returned : None
1484*******************************************************************************/
1485IMPEG2D_ERROR_CODES_T impeg2d_dec_pic_ext_data(dec_state_t *ps_dec)
1486{
1487    stream_t   *ps_stream;
1488    UWORD32     u4_start_code;
1489    IMPEG2D_ERROR_CODES_T e_error;
1490
1491    e_error = (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
1492
1493    ps_stream      = &ps_dec->s_bit_stream;
1494    u4_start_code   = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1495    while ( (u4_start_code == EXTENSION_START_CODE ||
1496            u4_start_code == USER_DATA_START_CODE) &&
1497            (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE == e_error &&
1498            (ps_stream->u4_offset < ps_stream->u4_max_offset))
1499    {
1500        if(u4_start_code == USER_DATA_START_CODE)
1501        {
1502            impeg2d_dec_user_data(ps_dec);
1503        }
1504        else
1505        {
1506            impeg2d_bit_stream_flush(ps_stream,START_CODE_LEN);
1507            u4_start_code   = impeg2d_bit_stream_nxt(ps_stream,EXT_ID_LEN);
1508            switch(u4_start_code)
1509            {
1510            case QUANT_MATRIX_EXT_ID:
1511                impeg2d_dec_quant_matrix_ext(ps_dec);
1512                break;
1513            case COPYRIGHT_EXT_ID:
1514                impeg2d_dec_copyright_ext(ps_dec);
1515                break;
1516            case PIC_DISPLAY_EXT_ID:
1517                impeg2d_dec_pic_disp_ext(ps_dec);
1518                break;
1519            case CAMERA_PARAM_EXT_ID:
1520                impeg2d_dec_cam_param_ext(ps_dec);
1521                break;
1522            case ITU_T_EXT_ID:
1523                impeg2d_dec_itu_t_ext(ps_dec);
1524                break;
1525            case PIC_SPATIAL_SCALABLE_EXT_ID:
1526            case PIC_TEMPORAL_SCALABLE_EXT_ID:
1527                e_error = IMPEG2D_SCALABLITY_NOT_SUP;
1528                break;
1529            default:
1530                /* In case its a reserved extension code */
1531                impeg2d_bit_stream_flush(ps_stream,EXT_ID_LEN);
1532                impeg2d_next_start_code(ps_dec);
1533                break;
1534            }
1535        }
1536        u4_start_code = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1537    }
1538    return e_error;
1539}
1540
1541/*******************************************************************************
1542*
1543*  Function Name   : impeg2d_process_video_header
1544*
1545*  Description     : Processes video sequence header information
1546*
1547*  Arguments       :
1548*  dec             : Decoder context
1549*
1550*  Values Returned : None
1551*******************************************************************************/
1552IMPEG2D_ERROR_CODES_T impeg2d_process_video_header(dec_state_t *ps_dec)
1553{
1554    stream_t *ps_stream;
1555    ps_stream = &ps_dec->s_bit_stream;
1556    IMPEG2D_ERROR_CODES_T e_error;
1557
1558    impeg2d_next_code(ps_dec, SEQUENCE_HEADER_CODE);
1559    if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1560    {
1561        e_error = impeg2d_dec_seq_hdr(ps_dec);
1562        if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1563        {
1564            return e_error;
1565        }
1566    }
1567    else
1568    {
1569      return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1570    }
1571    if (impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == EXTENSION_START_CODE)
1572    {
1573        /* MPEG2 Decoder */
1574        if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1575        {
1576            e_error = impeg2d_dec_seq_ext(ps_dec);
1577            if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1578            {
1579                return e_error;
1580            }
1581        }
1582        else
1583        {
1584          return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1585        }
1586        if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1587        {
1588            e_error = impeg2d_dec_seq_ext_data(ps_dec);
1589            if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1590            {
1591                return e_error;
1592            }
1593        }
1594        return impeg2d_init_video_state(ps_dec,MPEG_2_VIDEO);
1595    }
1596    else
1597    {
1598         /* MPEG1 Decoder */
1599        if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1600        {
1601            impeg2d_flush_ext_and_user_data(ps_dec);
1602        }
1603        return impeg2d_init_video_state(ps_dec,MPEG_1_VIDEO);
1604    }
1605}
1606/*******************************************************************************
1607*
1608*  Function Name   : impeg2d_process_video_bit_stream
1609*
1610*  Description     : Processes video sequence header information
1611*
1612*  Arguments       :
1613*  dec             : Decoder context
1614*
1615*  Values Returned : None
1616*******************************************************************************/
1617IMPEG2D_ERROR_CODES_T impeg2d_process_video_bit_stream(dec_state_t *ps_dec)
1618{
1619    stream_t *ps_stream;
1620    UWORD32 u4_next_bits, u4_start_code_found;
1621    IMPEG2D_ERROR_CODES_T e_error;
1622
1623    ps_stream = &ps_dec->s_bit_stream;
1624    impeg2d_next_start_code(ps_dec);
1625    /* If the stream is MPEG-2 compliant stream */
1626    u4_start_code_found = 0;
1627
1628    if(ps_dec->u2_is_mpeg2)
1629    {
1630        /* MPEG2 decoding starts */
1631        while((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1632        {
1633            u4_next_bits = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1634
1635            if(u4_next_bits == SEQUENCE_HEADER_CODE)
1636            {
1637                if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1638                {
1639                    e_error = impeg2d_dec_seq_hdr(ps_dec);
1640                    if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1641                    {
1642                        return e_error;
1643                    }
1644
1645                    u4_start_code_found = 0;
1646
1647                }
1648                else
1649                {
1650                    return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1651                }
1652
1653
1654                if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1655                {
1656                    IMPEG2D_ERROR_CODES_T e_error;
1657                    e_error = impeg2d_dec_seq_ext(ps_dec);
1658                    if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1659                    {
1660                        return e_error;
1661                    }
1662                    u4_start_code_found = 0;
1663
1664                }
1665                else
1666                {
1667                    return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1668                }
1669            }
1670            else if((u4_next_bits == USER_DATA_START_CODE) || (u4_next_bits == EXTENSION_START_CODE))
1671            {
1672                if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1673                {
1674                    impeg2d_dec_seq_ext_data(ps_dec);
1675                    u4_start_code_found = 0;
1676
1677                }
1678
1679            }
1680            else if((ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1681                    && (u4_next_bits == GOP_START_CODE))
1682            {
1683                impeg2d_dec_grp_of_pic_hdr(ps_dec);
1684                impeg2d_dec_user_data(ps_dec);
1685                u4_start_code_found = 0;
1686
1687            }
1688            else if((ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1689                    && (u4_next_bits == PICTURE_START_CODE))
1690            {
1691
1692                e_error = impeg2d_dec_pic_hdr(ps_dec);
1693                if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1694                {
1695                    return e_error;
1696                }
1697                impeg2d_dec_pic_coding_ext(ps_dec);
1698                e_error = impeg2d_dec_pic_ext_data(ps_dec);
1699                if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1700                {
1701                    return e_error;
1702                }
1703                impeg2d_pre_pic_dec_proc(ps_dec);
1704                impeg2d_dec_pic_data(ps_dec);
1705                impeg2d_post_pic_dec_proc(ps_dec);
1706                u4_start_code_found = 1;
1707            }
1708            else
1709
1710            {
1711                FLUSH_BITS(ps_dec->s_bit_stream.u4_offset, ps_dec->s_bit_stream.u4_buf, ps_dec->s_bit_stream.u4_buf_nxt, 8, ps_dec->s_bit_stream.pu4_buf_aligned);
1712
1713            }
1714            if(u4_start_code_found == 0)
1715            {
1716                impeg2d_next_start_code(ps_dec);
1717            }
1718        }
1719        if((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset > ps_dec->s_bit_stream.u4_max_offset))
1720        {
1721            return IMPEG2D_FRM_HDR_START_CODE_NOT_FOUND;
1722        }
1723
1724    }
1725        /* If the stream is MPEG-1 compliant stream */
1726    else
1727    {
1728        while((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1729        {
1730            u4_next_bits = impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN);
1731
1732            if(impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == SEQUENCE_HEADER_CODE)
1733            {
1734                if(ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset)
1735                {
1736                    e_error = impeg2d_dec_seq_hdr(ps_dec);
1737                    if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1738                    {
1739                        return e_error;
1740                    }
1741
1742                    u4_start_code_found = 0;
1743                }
1744                else
1745                {
1746                    return IMPEG2D_BITSTREAM_BUFF_EXCEEDED_ERR;
1747                }
1748            }
1749            else if((ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset) && (u4_next_bits == EXTENSION_START_CODE || u4_next_bits == USER_DATA_START_CODE))
1750            {
1751                impeg2d_flush_ext_and_user_data(ps_dec);
1752                u4_start_code_found = 0;
1753            }
1754
1755
1756            else if ((impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == GOP_START_CODE)
1757                    && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1758            {
1759                impeg2d_dec_grp_of_pic_hdr(ps_dec);
1760                impeg2d_flush_ext_and_user_data(ps_dec);
1761                u4_start_code_found = 0;
1762            }
1763            else if ((impeg2d_bit_stream_nxt(ps_stream,START_CODE_LEN) == PICTURE_START_CODE)
1764                    && (ps_dec->s_bit_stream.u4_offset < ps_dec->s_bit_stream.u4_max_offset))
1765            {
1766
1767                e_error = impeg2d_dec_pic_hdr(ps_dec);
1768                if ((IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE != e_error)
1769                {
1770                    return e_error;
1771                }
1772                impeg2d_flush_ext_and_user_data(ps_dec);
1773                impeg2d_pre_pic_dec_proc(ps_dec);
1774                impeg2d_dec_pic_data(ps_dec);
1775                impeg2d_post_pic_dec_proc(ps_dec);
1776                u4_start_code_found = 1;
1777            }
1778            else
1779            {
1780                FLUSH_BITS(ps_dec->s_bit_stream.u4_offset, ps_dec->s_bit_stream.u4_buf, ps_dec->s_bit_stream.u4_buf_nxt, 8, ps_dec->s_bit_stream.pu4_buf_aligned);
1781            }
1782            impeg2d_next_start_code(ps_dec);
1783
1784        }
1785        if((u4_start_code_found == 0) && (ps_dec->s_bit_stream.u4_offset > ps_dec->s_bit_stream.u4_max_offset))
1786        {
1787           return IMPEG2D_FRM_HDR_START_CODE_NOT_FOUND;
1788        }
1789    }
1790
1791    return (IMPEG2D_ERROR_CODES_T)IVD_ERROR_NONE;
1792}
1793