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