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