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