Home | History | Annotate | Download | only in src
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 #include "mp4dec_lib.h"
     19 #include "bitstream.h"
     20 #include "vlc_decode.h"
     21 #include "zigzag.h"
     22 
     23 #define OSCL_DISABLE_WARNING_CONV_POSSIBLE_LOSS_OF_DATA
     24 
     25 #ifdef PV_SUPPORT_MAIN_PROFILE
     26 /* INTRA */
     27 const static int mpeg_iqmat_def[NCOEFF_BLOCK] =
     28 {
     29     8, 17, 18, 19, 21, 23, 25, 27,
     30     17, 18, 19, 21, 23, 25, 27, 28,
     31     20, 21, 22, 23, 24, 26, 28, 30,
     32     21, 22, 23, 24, 26, 28, 30, 32,
     33     22, 23, 24, 26, 28, 30, 32, 35,
     34     23, 24, 26, 28, 30, 32, 35, 38,
     35     25, 26, 28, 30, 32, 35, 38, 41,
     36     27, 28, 30, 32, 35, 38, 41, 45
     37 };
     38 
     39 /* INTER */
     40 const static int mpeg_nqmat_def[64]  =
     41 {
     42     16, 17, 18, 19, 20, 21, 22, 23,
     43     17, 18, 19, 20, 21, 22, 23, 24,
     44     18, 19, 20, 21, 22, 23, 24, 25,
     45     19, 20, 21, 22, 23, 24, 26, 27,
     46     20, 21, 22, 23, 25, 26, 27, 28,
     47     21, 22, 23, 24, 26, 27, 28, 30,
     48     22, 23, 24, 26, 27, 28, 30, 31,
     49     23, 24, 25, 27, 28, 30, 31, 33
     50 };
     51 #endif
     52 
     53 /* ======================================================================== */
     54 /*  Function : CalcNumBits()                                                */
     55 /*  Purpose  :                                                              */
     56 /*  In/out   :                                                              */
     57 /*  Return   : Calculate the minimum number of bits required to             */
     58 /*              represent x.                                                */
     59 /*  Note     : This is an equivalent implementation of                      */
     60 /*                      (long)ceil(log((double)x)/log(2.0))                 */
     61 /*  Modified :                                                              */
     62 /* ======================================================================== */
     63 int CalcNumBits(uint x)
     64 {
     65     int i = 1;
     66     while (x >>= 1) i++;
     67     return i;
     68 }
     69 
     70 
     71 
     72 /***********************************************************CommentBegin******
     73 *
     74 * -- DecodeVolHeader -- Decode the header of a VOL
     75 *
     76 *   04/10/2000 : initial modification to the new PV-Decoder Lib format.
     77 *   10/12/2001 : reject non compliant bitstreams
     78 *
     79 ***********************************************************CommentEnd********/
     80 PV_STATUS DecodeVOLHeader(VideoDecData *video, int layer)
     81 {
     82     PV_STATUS status;
     83     Vol *currVol;
     84     BitstreamDecVideo *stream;
     85     uint32 tmpvar, vol_shape;
     86     uint32 startCode;
     87 #ifdef PV_SUPPORT_MAIN_PROFILE
     88     int *qmat, i, j;
     89 #endif
     90     int version_id = 1;
     91 #ifdef PV_TOLERATE_VOL_ERRORS
     92     uint32 profile = 0x01;
     93 #endif
     94     /*  There's a "currLayer" variable inside videoDecData.          */
     95     /*   However, we don't maintain it until we decode frame data.  04/05/2000 */
     96     currVol = video->vol[layer];
     97     stream  = currVol->bitstream;
     98     currVol->moduloTimeBase = 0;
     99 
    100     /* Determine which start code for the decoder to begin with */
    101     status = BitstreamShowBits32HC(stream, &startCode);
    102 
    103     if (startCode == VISUAL_OBJECT_SEQUENCE_START_CODE)
    104     {   /*  Bitstream Exhchange Fix 9/99 */
    105         /* Bitstream Exchange requires we allow start with Video Object Sequence */
    106         /* visual_object_sequence_start_code            */
    107         (void) BitstreamReadBits32HC(stream);
    108         tmpvar = (uint32) BitstreamReadBits16(stream,  8); /* profile */
    109 #ifndef PV_TOLERATE_VOL_ERRORS
    110         if (layer)                                                      /*    */
    111         {
    112             /* support SSPL0-2  */
    113             if (tmpvar != 0x10 && tmpvar != 0x11 && tmpvar != 0x12 &&
    114                     tmpvar != 0xA1 && tmpvar != 0xA2  && tmpvar != 0xA3/* Core SP@L1-L3 */)
    115                 return PV_FAIL;
    116         }
    117         else
    118         {
    119             /* support SPL0-3 & SSPL0-2   */
    120             if (tmpvar != 0x01 && tmpvar != 0x02 && tmpvar != 0x03 && tmpvar != 0x08 &&
    121                     tmpvar != 0x10 && tmpvar != 0x11 && tmpvar != 0x12 &&
    122                     tmpvar != 0x21 && tmpvar != 0x22 &&  /* Core Profile Levels */
    123                     tmpvar != 0xA1 && tmpvar != 0xA2 && tmpvar != 0xA3 &&
    124                     tmpvar != 0xF0 && tmpvar != 0xF1 && /* Advanced Simple Profile Levels*/
    125                     tmpvar != 0xF2 && tmpvar != 0xF3 &&
    126                     tmpvar != 0xF4 && tmpvar != 0xF5)
    127                 return PV_FAIL;
    128         }
    129 #else
    130         profile = tmpvar;
    131 #endif
    132 
    133         // save the profile and level for the query
    134         currVol->profile_level_id = (uint)tmpvar; //  6/10/04
    135 
    136 
    137 
    138         status = BitstreamShowBits32HC(stream, &tmpvar);
    139         if (tmpvar == USER_DATA_START_CODE)
    140         {
    141             /* Something has to be done with user data  11/11/99 */
    142             status = DecodeUserData(stream);
    143             if (status != PV_SUCCESS) return PV_FAIL;
    144         }
    145         /* visual_object_start_code                     */
    146         BitstreamShowBits32HC(stream, &tmpvar);
    147         if (tmpvar != VISUAL_OBJECT_START_CODE)
    148         {
    149             do
    150             {
    151                 /* Search for VOL_HEADER */
    152                 status = PVSearchNextM4VFrame(stream); /* search 0x00 0x00 0x01 */
    153                 if (status != PV_SUCCESS) return PV_FAIL; /* breaks the loop */
    154                 BitstreamShowBits32(stream, VOL_START_CODE_LENGTH, &tmpvar);
    155                 PV_BitstreamFlushBits(stream, 8);
    156             }
    157             while (tmpvar != VOL_START_CODE);
    158             goto decode_vol;
    159         }
    160         else
    161         {
    162             BitstreamReadBits32HC(stream);
    163         }
    164 
    165         /*  is_visual_object_identifier            */
    166         tmpvar = (uint32) BitstreamRead1Bits(stream);
    167         if (tmpvar)
    168         {
    169             /* visual_object_verid                            */
    170             tmpvar = (uint32) BitstreamReadBits16(stream, 4);
    171             /* visual_object_priority                         */
    172             tmpvar = (uint32) BitstreamReadBits16(stream, 3);
    173         }
    174         /* visual_object_type                                 */
    175         BitstreamShowBits32(stream, 4, &tmpvar);
    176         if (tmpvar == 1)
    177         { /* video_signal_type */
    178             PV_BitstreamFlushBits(stream, 4);
    179             tmpvar = (uint32) BitstreamRead1Bits(stream);
    180             if (tmpvar == 1)
    181             {
    182                 /* video_format */
    183                 tmpvar = (uint32) BitstreamReadBits16(stream, 3);
    184                 /* video_range  */
    185                 tmpvar = (uint32) BitstreamRead1Bits(stream);
    186                 /* color_description */
    187                 tmpvar = (uint32) BitstreamRead1Bits(stream);
    188                 if (tmpvar == 1)
    189                 {
    190                     /* color_primaries */
    191                     tmpvar = (uint32) BitstreamReadBits16(stream, 8);
    192                     /* transfer_characteristics */
    193                     tmpvar = (uint32) BitstreamReadBits16(stream, 8);
    194                     /* matrix_coefficients */
    195                     tmpvar = (uint32) BitstreamReadBits16(stream, 8);
    196                 }
    197             }
    198         }
    199         else
    200         {
    201             do
    202             {
    203                 /* Search for VOL_HEADER */
    204                 status = PVSearchNextM4VFrame(stream); /* search 0x00 0x00 0x01 */
    205                 if (status != PV_SUCCESS) return PV_FAIL; /* breaks the loop */
    206                 BitstreamShowBits32(stream, VOL_START_CODE_LENGTH, &tmpvar);
    207                 PV_BitstreamFlushBits(stream, 8);
    208             }
    209             while (tmpvar != VOL_START_CODE);
    210             goto decode_vol;
    211         }
    212 
    213         /* next_start_code() */
    214         status = PV_BitstreamByteAlign(stream);                            /*  10/12/01 */
    215         status = BitstreamShowBits32HC(stream, &tmpvar);
    216 
    217         if (tmpvar == USER_DATA_START_CODE)
    218         {
    219             /* Something has to be done to deal with user data (parse it)  11/11/99 */
    220             status = DecodeUserData(stream);
    221             if (status != PV_SUCCESS) return PV_FAIL;
    222         }
    223         status = BitstreamShowBits32(stream, 27, &tmpvar);   /*  10/12/01 */
    224     }
    225     else
    226     {
    227         /*      tmpvar = 0;   */                                             /*  10/12/01 */
    228         status = BitstreamShowBits32(stream, 27, &tmpvar);     /* uncomment this line if you want
    229                                                                      to start decoding with a
    230                                                                      video_object_start_code */
    231     }
    232 
    233     if (tmpvar == VO_START_CODE)
    234     {
    235         /*****
    236         *
    237         *   Read the VOL header entries from the bitstream
    238         *
    239         *****/
    240         /* video_object_start_code                         */
    241         tmpvar = BitstreamReadBits32(stream, 27);
    242         tmpvar = (uint32) BitstreamReadBits16(stream, 5);
    243 
    244 
    245         /* video_object_layer_start_code                   */
    246         BitstreamShowBits32(stream, VOL_START_CODE_LENGTH, &tmpvar);
    247         if (tmpvar != VOL_START_CODE)
    248         {
    249             status = BitstreamCheckEndBuffer(stream);
    250             if (status == PV_END_OF_VOP)
    251             {
    252                 video->shortVideoHeader = TRUE;
    253                 return PV_SUCCESS;
    254             }
    255             else
    256             {
    257                 do
    258                 {
    259                     /* Search for VOL_HEADER */
    260                     status = PVSearchNextM4VFrame(stream);/* search 0x00 0x00 0x01 */
    261                     if (status != PV_SUCCESS) return PV_FAIL; /* breaks the loop */
    262                     BitstreamShowBits32(stream, VOL_START_CODE_LENGTH, &tmpvar);
    263                     PV_BitstreamFlushBits(stream, 8); /* advance the byte ptr */
    264                 }
    265                 while (tmpvar != VOL_START_CODE);
    266             }
    267         }
    268         else
    269         {
    270             PV_BitstreamFlushBits(stream, 8);
    271         }
    272 
    273 decode_vol:
    274         PV_BitstreamFlushBits(stream, VOL_START_CODE_LENGTH - 8);
    275         video->shortVideoHeader = 0;
    276 
    277         /* vol_id (4 bits) */
    278         currVol->volID = (int) BitstreamReadBits16(stream, 4);
    279 
    280         /* RandomAccessible flag */
    281         tmpvar = (uint32) BitstreamRead1Bits(stream);
    282 
    283         /* object type */
    284         tmpvar = (uint32) BitstreamReadBits16(stream, 8);                /*  */
    285 
    286 #ifdef PV_TOLERATE_VOL_ERRORS
    287         if (tmpvar == 0)
    288         {
    289             if (layer)                                                      /*    */
    290             {
    291                 /* support SSPL0-2  */
    292                 if (profile != 0x10 && profile != 0x11 && profile != 0x12)
    293                     return PV_FAIL;
    294                 tmpvar = 0x02;
    295             }
    296             else
    297             {
    298                 /* support SPL0-3 & SSPL0-2   */
    299                 if (profile != 0x01 && profile != 0x02 && profile != 0x03 && profile != 0x08 &&
    300                         profile != 0x10 && profile != 0x11 && profile != 0x12)
    301                     return PV_FAIL;
    302                 tmpvar = 0x01;
    303             }
    304             profile |= 0x0100;
    305         }
    306 #endif
    307 
    308         if (layer)
    309         {
    310             if (tmpvar != 0x02) return PV_FAIL;
    311         }
    312         else
    313         {
    314             if (tmpvar != 0x01) return PV_FAIL;
    315         }
    316 
    317         /* version id specified? */
    318         tmpvar = (uint32) BitstreamRead1Bits(stream);
    319         if (tmpvar == 1)
    320         {
    321             /* version ID */
    322             version_id = (uint32) BitstreamReadBits16(stream, 4);
    323             /* priority */
    324             tmpvar = (uint32) BitstreamReadBits16(stream, 3);
    325 
    326         }
    327 
    328         /* aspect ratio info */
    329         tmpvar = (uint32) BitstreamReadBits16(stream, 4);
    330         if (tmpvar == 0) return PV_FAIL;
    331         if (tmpvar == 0xf /* extended_par */)
    332         {
    333             /* width */
    334             tmpvar = (uint32) BitstreamReadBits16(stream, 8);
    335             /* height */
    336             tmpvar = (uint32) BitstreamReadBits16(stream, 8);
    337         }
    338 
    339 
    340         /* control parameters present? */
    341         tmpvar = (uint32) BitstreamRead1Bits(stream);
    342 
    343         /*  Get the parameters (skipped) */
    344         /*  03/10/99 */
    345         if (tmpvar)
    346         {
    347             /* chroma_format                    */
    348             tmpvar = BitstreamReadBits16(stream, 2);
    349             if (tmpvar != 1) return PV_FAIL;
    350             /* low_delay  */
    351             tmpvar = BitstreamRead1Bits(stream);
    352 
    353             /* vbv_parameters present? */
    354             tmpvar = (uint32) BitstreamRead1Bits(stream);
    355             if (tmpvar)
    356             {
    357                 /* first_half_bit_rate    */
    358                 BitstreamReadBits16(stream, 15);
    359                 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
    360                 /* latter_half_bit_rate   */
    361                 BitstreamReadBits16(stream, 15);
    362                 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
    363                 /* first_half_vbv_buffer_size   */
    364                 BitstreamReadBits16(stream, 15);
    365                 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
    366                 /* latter_half_vbv_buffer_size   */
    367                 BitstreamReadBits16(stream,  3);
    368                 /* first_half_vbv_occupancy     */
    369                 BitstreamReadBits16(stream, 11);
    370                 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
    371                 /* latter_half_vbv_occupancy  */
    372                 BitstreamReadBits16(stream, 15);
    373                 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
    374             }
    375         }
    376 
    377         /* video_object_layer_shape (2 bits), only 00 (rect) is supported for now */
    378         vol_shape = (uint32) BitstreamReadBits16(stream, 2);
    379         if (vol_shape) return PV_FAIL;
    380 
    381         /* marker bit,  03/10/99 */
    382         if (!BitstreamRead1Bits(stream)) return PV_FAIL;
    383 
    384         /* vop_time_increment_resolution   */
    385         currVol->timeIncrementResolution = BitstreamReadBits16(stream, 16);
    386         if (currVol->timeIncrementResolution == 0) return PV_FAIL;
    387 
    388         /* . since nbitsTimeIncRes will be used over and over again, */
    389         /*    we should put it in Vol structure.  04/12/2000.          */
    390         currVol->nbitsTimeIncRes = CalcNumBits((uint)currVol->timeIncrementResolution - 1);
    391 
    392         if (!BitstreamRead1Bits(stream)) return PV_FAIL;
    393 
    394         /* fixed_vop_rate */
    395         currVol->fixedVopRate = (int) BitstreamRead1Bits(stream);
    396         if (currVol->fixedVopRate)
    397         {
    398             /* fixed_vop_time_increment */
    399             tmpvar = BitstreamReadBits16(stream, currVol->nbitsTimeIncRes);
    400         }
    401 
    402         /* marker bit */
    403         if (!BitstreamRead1Bits(stream)) return PV_FAIL;
    404 
    405         /* video_object_layer_width (13 bits) */
    406         video->displayWidth = video->width = (int) BitstreamReadBits16(stream, 13);
    407 
    408         /* round up to a multiple of MB_SIZE.   08/09/2000 */
    409         video->width = (video->width + 15) & -16;
    410 //      video->displayWidth += (video->displayWidth & 0x1);  /* displayed image should be even size */
    411 
    412         /* marker bit */
    413         if (!BitstreamRead1Bits(stream)) return PV_FAIL;
    414 
    415         /* video_object_layer_height (13 bits) */
    416         video->displayHeight = video->height = (int) BitstreamReadBits16(stream, 13);
    417 
    418         /* round up to a multiple of MB_SIZE.   08/09/2000 */
    419         video->height = (video->height + 15) & -16;
    420 //      video->displayHeight += (video->displayHeight & 0x1); /* displayed image should be even size */
    421         if (!BitstreamRead1Bits(stream)) return PV_FAIL;
    422 
    423         /*  03/10/99 */
    424         /* interlaced */
    425         tmpvar = (uint32) BitstreamRead1Bits(stream);
    426         if (tmpvar != 0)
    427         {
    428             mp4dec_log("DecodeVOLHeader(): Interlaced video is not supported.\n");
    429             return PV_FAIL;
    430         }
    431 
    432         /* obmc_disable */
    433         tmpvar = (uint32) BitstreamRead1Bits(stream);
    434         if (tmpvar == 0) return PV_FAIL;
    435 
    436         if (version_id == 1)
    437         {
    438             /*  sprite_enable (1 bits) */
    439             tmpvar = (uint32) BitstreamRead1Bits(stream);
    440             if (tmpvar)
    441             {
    442                 mp4dec_log("DecodeVOLHeader(): Sprite is not supported.\n");
    443                 return PV_FAIL;
    444             }
    445         }
    446         else
    447         {
    448             /* For version 2, vol_sprite_usage has two bits. */
    449             /* sprite_enable */
    450             tmpvar = (uint32) BitstreamReadBits16(stream, 2);
    451             if (tmpvar)
    452             {
    453                 mp4dec_log("DecodeVOLHeader(): Sprite is not supported.\n");
    454                 return PV_FAIL;
    455             }
    456         }
    457 
    458         /* not_8_bit */
    459         if (BitstreamRead1Bits(stream))
    460         {
    461             /* quant_precision */
    462             currVol->quantPrecision = BitstreamReadBits16(stream, 4);
    463             /* bits_per_pixel  */
    464             currVol->bitsPerPixel = BitstreamReadBits16(stream, 4);
    465             mp4dec_log("DecodeVOLHeader(): not an 8-bit stream.\n");    // For the time being we do not support != 8 bits
    466 
    467             return PV_FAIL;
    468         }
    469         else
    470         {
    471             currVol->quantPrecision = 5;
    472             currVol->bitsPerPixel = 8;
    473         }
    474 
    475         /* quant_type (1 bit) */
    476         currVol->quantType = BitstreamRead1Bits(stream);
    477         if (currVol->quantType)
    478         {
    479 #ifdef PV_SUPPORT_MAIN_PROFILE
    480             /* load quantization matrices.   5/22/2000 */
    481             /* load_intra_quant_mat (1 bit) */
    482             qmat = currVol->iqmat;
    483             currVol->loadIntraQuantMat = BitstreamRead1Bits(stream);
    484             if (currVol->loadIntraQuantMat)
    485             {
    486                 /* intra_quant_mat (8*64 bits) */
    487                 i = 0;
    488                 do
    489                 {
    490                     qmat[*(zigzag_inv+i)] = (int) BitstreamReadBits16(stream, 8);
    491                 }
    492                 while ((qmat[*(zigzag_inv+i)] != 0) && (++i < 64));
    493 
    494                 for (j = i; j < 64; j++)
    495                     qmat[*(zigzag_inv+j)] = qmat[*(zigzag_inv+i-1)];
    496             }
    497             else
    498             {
    499                 oscl_memcpy(qmat, mpeg_iqmat_def, 64*sizeof(int));
    500             }
    501 
    502             qmat[0] = 0;             /* necessary for switched && MPEG quant  07/09/01 */
    503 
    504             /* load_nonintra_quant_mat (1 bit) */
    505             qmat = currVol->niqmat;
    506             currVol->loadNonIntraQuantMat = BitstreamRead1Bits(stream);
    507             if (currVol->loadNonIntraQuantMat)
    508             {
    509                 /* nonintra_quant_mat (8*64 bits) */
    510                 i = 0;
    511                 do
    512                 {
    513                     qmat[*(zigzag_inv+i)] = (int) BitstreamReadBits16(stream, 8);
    514                 }
    515                 while ((qmat[*(zigzag_inv+i)] != 0) && (++i < 64));
    516 
    517                 for (j = i; j < 64; j++)
    518                     qmat[*(zigzag_inv+j)] = qmat[*(zigzag_inv+i-1)];
    519             }
    520             else
    521             {
    522                 oscl_memcpy(qmat, mpeg_nqmat_def, 64*sizeof(int));
    523             }
    524 #else
    525             return PV_FAIL;
    526 #endif
    527         }
    528 
    529         if (version_id != 1)
    530         {
    531             /* quarter_sample enabled */
    532             tmpvar = BitstreamRead1Bits(stream);
    533             if (tmpvar) return PV_FAIL;
    534         }
    535 
    536         /* complexity_estimation_disable */
    537         currVol->complexity_estDisable = BitstreamRead1Bits(stream);
    538         if (currVol->complexity_estDisable == 0)
    539         {
    540             currVol->complexity_estMethod = BitstreamReadBits16(stream, 2);
    541 
    542             if (currVol->complexity_estMethod < 2)
    543             {
    544                 /* shape_complexity_estimation_disable */
    545                 tmpvar = BitstreamRead1Bits(stream);
    546                 if (tmpvar == 0)
    547                 {
    548                     mp4dec_log("DecodeVOLHeader(): Shape Complexity estimation is not supported.\n");
    549                     return PV_FAIL;
    550                 }
    551                 /* texture_complexity_estimation_set_1_disable */
    552                 tmpvar = BitstreamRead1Bits(stream);
    553                 if (tmpvar == 0)
    554                 {
    555                     currVol->complexity.text_1 = BitstreamReadBits16(stream, 4);
    556                 }
    557                 /* marker bit */
    558                 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
    559                 /* texture_complexity_estimation_set_2_disable */
    560                 tmpvar = BitstreamRead1Bits(stream);
    561                 if (tmpvar == 0)
    562                 {
    563                     currVol->complexity.text_2 = BitstreamReadBits16(stream, 4);
    564                 }
    565                 /* motion_compensation_complexity_disable */
    566                 tmpvar = BitstreamRead1Bits(stream);
    567                 if (tmpvar == 0)
    568                 {
    569                     currVol->complexity.mc = BitstreamReadBits16(stream, 6);
    570                 }
    571                 /* marker bit */
    572                 if (!BitstreamRead1Bits(stream)) return PV_FAIL;
    573 
    574                 if (currVol->complexity_estMethod == 1)
    575                 {   /* version2_complexity_estimation_disable */
    576                     tmpvar = BitstreamRead1Bits(stream);
    577                     if (tmpvar == 0)
    578                     {
    579                         mp4dec_log("DecodeVOLHeader(): sadct, quarter pel not supported.\n");
    580                         return PV_FAIL;
    581                     }
    582                 }
    583             }
    584         }
    585 
    586         /*  03/10/99 */
    587         /* resync_marker_disable */
    588         currVol->errorResDisable = (int) BitstreamRead1Bits(stream);
    589         /* data_partititioned    */
    590         currVol->dataPartitioning = (int) BitstreamRead1Bits(stream);
    591 
    592         video->vlcDecCoeffIntra = &VlcDecTCOEFIntra;
    593         video->vlcDecCoeffInter = &VlcDecTCOEFInter;
    594 
    595         if (currVol->dataPartitioning)
    596         {
    597             if (layer) return PV_FAIL;                              /*  */
    598             /* reversible_vlc */
    599             currVol->useReverseVLC = (int)BitstreamRead1Bits(stream);
    600             if (currVol->useReverseVLC)
    601             {
    602                 video->vlcDecCoeffIntra = &RvlcDecTCOEFIntra;
    603                 video->vlcDecCoeffInter = &RvlcDecTCOEFInter;
    604             }
    605             currVol->errorResDisable = 0;
    606         }
    607         else
    608         {
    609             currVol->useReverseVLC = 0;
    610         }
    611 
    612         if (version_id != 1)
    613         {
    614             /* newpred_enable */
    615             tmpvar = BitstreamRead1Bits(stream);
    616             if (tmpvar) return PV_FAIL;
    617 
    618             /* reduced_resolution_vop */
    619             tmpvar = BitstreamRead1Bits(stream);
    620             if (tmpvar) return PV_FAIL;
    621 
    622         }
    623 
    624         /* Intra AC/DC prediction is always true */
    625         video->intra_acdcPredDisable = 0;
    626         /* scalability */
    627         currVol->scalability = (int) BitstreamRead1Bits(stream);
    628 
    629         if (currVol->scalability)
    630         {
    631             if (layer == 0)  return PV_FAIL;                     /*  */
    632             /* hierarchy_type: 1 : temporal, 0 : spatial */
    633             /*  03/10/99 */
    634             currVol->scalType = (int) BitstreamRead1Bits(stream);              /*  */
    635             if (!currVol->scalType) return PV_FAIL;
    636 
    637             /* ref_layer_id (4 bits) */
    638             currVol->refVolID = (int) BitstreamReadBits16(stream, 4);
    639             if (layer)                                                      /*  */
    640             {
    641                 if (currVol->refVolID != video->vol[0]->volID) return PV_FAIL;
    642             }
    643             /* ref_layer_sampling_direc (1 bits)              */
    644             /*   1 : ref. layer has higher resolution         */
    645             /*   0 : ref. layer has equal or lower resolution */
    646             currVol->refSampDir = (int) BitstreamRead1Bits(stream);
    647             if (currVol->refSampDir) return PV_FAIL;
    648 
    649             /* hor_sampling_factor_n (5 bits) */
    650             currVol->horSamp_n = (int) BitstreamReadBits16(stream, 5);
    651 
    652             /* hor_sampling_factor_m (5 bits) */
    653             currVol->horSamp_m = (int) BitstreamReadBits16(stream, 5);
    654 
    655             if (currVol->horSamp_m == 0) return PV_FAIL;
    656             if (currVol->horSamp_n != currVol->horSamp_m) return PV_FAIL;
    657 
    658             /* ver_sampling_factor_n (5 bits) */
    659             currVol->verSamp_n = (int) BitstreamReadBits16(stream, 5);
    660 
    661             /* ver_sampling_factor_m (5 bits) */
    662             currVol->verSamp_m = (int) BitstreamReadBits16(stream, 5);
    663 
    664             if (currVol->verSamp_m == 0) return PV_FAIL;
    665             if (currVol->verSamp_n != currVol->verSamp_m) return PV_FAIL;
    666 
    667 
    668             /* enhancement_type: 1 : partial region, 0 : full region */
    669             /* 04/10/2000: we only support full region enhancement layer. */
    670             if (BitstreamRead1Bits(stream)) return PV_FAIL;
    671         }
    672 
    673         PV_BitstreamByteAlign(stream);
    674 
    675         status = BitstreamShowBits32HC(stream, &tmpvar);
    676 
    677         /* if we hit the end of buffer, tmpvar == 0.   08/30/2000 */
    678         if (tmpvar == USER_DATA_START_CODE)
    679         {
    680             status = DecodeUserData(stream);
    681             /* you should not check for status here  03/19/2002 */
    682             status = PV_SUCCESS;
    683         }
    684 
    685         /* Compute some convenience variables:   04/13/2000 */
    686         video->nMBPerRow = video->width / MB_SIZE;
    687         video->nMBPerCol = video->height / MB_SIZE;
    688         video->nTotalMB = video->nMBPerRow * video->nMBPerCol;
    689         video->nBitsForMBID = CalcNumBits((uint)video->nTotalMB - 1);
    690 #ifdef PV_ANNEX_IJKT_SUPPORT
    691         video->modified_quant = 0;
    692         video->advanced_INTRA = 0;
    693         video->deblocking = 0;
    694         video->slice_structure = 0;
    695 #endif
    696     }
    697     else
    698     {
    699         /* SHORT_HEADER */
    700         status = BitstreamShowBits32(stream, SHORT_VIDEO_START_MARKER_LENGTH, &tmpvar);
    701 
    702         if (tmpvar == SHORT_VIDEO_START_MARKER)
    703         {
    704             video->shortVideoHeader = TRUE;
    705         }
    706         else
    707         {
    708             do
    709             {
    710                 /* Search for VOL_HEADER */
    711                 status = PVSearchNextM4VFrame(stream); /* search 0x00 0x00 0x01 */
    712                 if (status != PV_SUCCESS) return PV_FAIL; /* breaks the loop */
    713                 BitstreamShowBits32(stream, VOL_START_CODE_LENGTH, &tmpvar);
    714                 PV_BitstreamFlushBits(stream, 8);
    715             }
    716             while (tmpvar != VOL_START_CODE);
    717             goto decode_vol;
    718         }
    719     }
    720 #ifdef PV_TOLERATE_VOL_ERRORS
    721     if (profile > 0xFF || profile == 0)
    722     {
    723         return PV_BAD_VOLHEADER;
    724     }
    725 #endif
    726 
    727     return status;
    728 }
    729 
    730 
    731 /***********************************************************CommentBegin******
    732 *
    733 * -- DecodeGOV -- Decodes the Group of VOPs from bitstream
    734 *
    735 *   04/20/2000  initial modification to the new PV-Decoder Lib format.
    736 *
    737 ***********************************************************CommentEnd********/
    738 PV_STATUS DecodeGOVHeader(BitstreamDecVideo *stream, uint32 *time_base)
    739 {
    740     uint32 tmpvar, time_s;
    741     int closed_gov, broken_link;
    742 
    743     /* group_start_code (32 bits) */
    744 //   tmpvar = BitstreamReadBits32(stream, 32);
    745 
    746     /* hours */
    747     tmpvar = (uint32) BitstreamReadBits16(stream, 5);
    748     time_s = tmpvar * 3600;
    749 
    750     /* minutes */
    751     tmpvar = (uint32) BitstreamReadBits16(stream, 6);
    752     time_s += tmpvar * 60;
    753 
    754     /* marker bit */
    755     tmpvar = (uint32) BitstreamRead1Bits(stream);
    756 
    757     /* seconds */
    758     tmpvar = (uint32) BitstreamReadBits16(stream, 6);
    759     time_s += tmpvar;
    760 
    761     /* We have to check the timestamp here.  If the sync timestamp is */
    762     /*    earlier than the previous timestamp or longer than 60 sec.  */
    763     /*    after the previous timestamp, assume the GOV header is      */
    764     /*    corrupted.                                 05/12/2000     */
    765     *time_base = time_s;   /*  02/27/2002 */
    766 //  *time_base = *time_base/1000;
    767 //  tmpvar = time_s - *time_base;
    768 //  if (tmpvar <= 60) *time_base = time_s;
    769 //  else return PV_FAIL;
    770 
    771     tmpvar = (uint32) BitstreamRead1Bits(stream);
    772     closed_gov = tmpvar;
    773     tmpvar = (uint32) BitstreamRead1Bits(stream);
    774     broken_link = tmpvar;
    775 
    776     if ((closed_gov == 0) && (broken_link == 1))
    777     {
    778         return PV_SUCCESS;        /*  03/15/2002  you can also return PV_FAIL */
    779     }
    780 
    781     PV_BitstreamByteAlign(stream);
    782 
    783     BitstreamShowBits32HC(stream, &tmpvar);
    784 
    785     while (tmpvar == USER_DATA_START_CODE)       /*  03/15/2002 */
    786     {
    787         DecodeUserData(stream);
    788         BitstreamShowBits32HC(stream, &tmpvar);
    789     }
    790 
    791     return PV_SUCCESS;
    792 }
    793 
    794 /***********************************************************CommentBegin******
    795 *
    796 * -- DecodeVopHeader -- Decodes the VOPheader information from the bitstream
    797 *
    798 *   04/12/2000  Initial port to the new PV decoder library format.
    799 *   05/10/2000  Error resilient decoding of vop header.
    800 *
    801 ***********************************************************CommentEnd********/
    802 PV_STATUS DecodeVOPHeader(VideoDecData *video, Vop *currVop, Bool use_ext_timestamp)
    803 {
    804     PV_STATUS status = PV_SUCCESS;
    805     Vol *currVol = video->vol[video->currLayer];
    806     BitstreamDecVideo *stream = currVol->bitstream;
    807     uint32 tmpvar;
    808     int time_base;
    809 
    810     /*****
    811     *   Read the VOP header from the bitstream (No shortVideoHeader Mode here!)
    812     *****/
    813     BitstreamShowBits32HC(stream, &tmpvar);
    814 
    815     /* check if we have a GOV header here.   08/30/2000 */
    816     if (tmpvar == GROUP_START_CODE)
    817     {
    818         tmpvar = BitstreamReadBits32HC(stream);
    819 //      rewindBitstream(stream, START_CODE_LENGTH); /* for backward compatibility */
    820         status = DecodeGOVHeader(stream, &tmpvar);
    821         if (status != PV_SUCCESS)
    822         {
    823             return status;
    824         }
    825 //      use_ext_timestamp = TRUE;   /*  02/08/2002 */
    826         /* We should have a VOP header following the GOV header.  03/15/2001 */
    827         BitstreamShowBits32HC(stream, &tmpvar);
    828     }
    829 #ifdef PV_SUPPORT_TEMPORAL_SCALABILITY
    830     currVop->timeStamp = -1;
    831 #endif
    832     if (tmpvar == VOP_START_CODE)
    833     {
    834         tmpvar = BitstreamReadBits32HC(stream);
    835     }
    836     else
    837     {
    838         PV_BitstreamFlushBits(stream, 8); // advance by a byte
    839         status = PV_FAIL;
    840         goto return_point;
    841     }
    842 
    843 
    844 
    845     /* vop_prediction_type (2 bits) */
    846     currVop->predictionType = (int) BitstreamReadBits16(stream, 2);
    847 
    848     /* modulo_time_base (? bits) */
    849     time_base = -1;
    850     do
    851     {
    852         time_base++;
    853         tmpvar = (uint32) BitstreamRead1Bits(stream);
    854     }
    855     while (tmpvar == 1);
    856 
    857 
    858 
    859     if (!use_ext_timestamp)
    860     {
    861         currVol->moduloTimeBase += 1000 * time_base; /* milliseconds based MTB  11/12/01 */
    862     }
    863 
    864     /* marker_bit (1 bit) */
    865     if (!BitstreamRead1Bits(stream))
    866     {
    867         status = PV_FAIL;
    868         goto return_point;
    869     }
    870 
    871     /* vop_time_increment (1-15 bits) in Nov_Compliant (1-16 bits) */
    872     /*    we always assumes fixed vop rate here */
    873     currVop->timeInc = BitstreamReadBits16(stream, currVol->nbitsTimeIncRes);
    874 
    875 
    876     /* marker_bit (1 bit) */
    877     if (!BitstreamRead1Bits(stream))
    878     {
    879         status = PV_FAIL;
    880         goto return_point;
    881     }
    882 
    883     /* vop_coded */
    884     currVop->vopCoded = (int) BitstreamRead1Bits(stream);
    885 
    886 
    887     if (currVop->vopCoded == 0)
    888     {
    889         status = PV_SUCCESS;
    890         goto return_point;
    891     }
    892 
    893 
    894     /* read vop_rounding_type */
    895     if (currVop->predictionType == P_VOP)
    896     {
    897         currVop->roundingType = (int) BitstreamRead1Bits(stream);
    898     }
    899     else
    900     {
    901         currVop->roundingType = 0;
    902     }
    903 
    904     if (currVol->complexity_estDisable == 0)
    905     {
    906         if (currVol->complexity_estMethod < 2)   /*   OCT 2002 */
    907         {
    908             if ((currVol->complexity.text_1 >> 3) & 0x1)    /* intra        */
    909                 BitstreamReadBits16(stream, 8);
    910             if (currVol->complexity.text_1 & 0x1)           /* not_coded    */
    911                 BitstreamReadBits16(stream, 8);
    912             if ((currVol->complexity.text_2 >> 3) & 0x1)    /* dct_coefs    */
    913                 BitstreamReadBits16(stream, 8);
    914             if ((currVol->complexity.text_2 >> 2) & 0x1)    /* dct_lines    */
    915                 BitstreamReadBits16(stream, 8);
    916             if ((currVol->complexity.text_2 >> 1) & 0x1)    /* vlc_symbols  */
    917                 BitstreamReadBits16(stream, 8);
    918             if (currVol->complexity.text_2 & 0x1)           /* vlc_bits     */
    919                 BitstreamReadBits16(stream, 4);
    920 
    921             if (currVop->predictionType != I_VOP)
    922             {
    923                 if ((currVol->complexity.text_1 >> 2) & 0x1)    /* inter    */
    924                     BitstreamReadBits16(stream, 8);
    925                 if ((currVol->complexity.text_1 >> 1) & 0x1)    /* inter_4v */
    926                     BitstreamReadBits16(stream, 8);
    927                 if ((currVol->complexity.mc >> 5) & 0x1)        /* apm      */
    928                     BitstreamReadBits16(stream, 8);
    929                 if ((currVol->complexity.mc >> 4) & 0x1)        /* npm      */
    930                     BitstreamReadBits16(stream, 8);
    931                 /* interpolate_mc_q */
    932                 if ((currVol->complexity.mc >> 2) & 0x1)        /* forw_back_mc_q */
    933                     BitstreamReadBits16(stream, 8);
    934                 if ((currVol->complexity.mc >> 1) & 0x1)        /* halfpel2 */
    935                     BitstreamReadBits16(stream, 8);
    936                 if (currVol->complexity.mc & 0x1)               /* halfpel4 */
    937                     BitstreamReadBits16(stream, 8);
    938             }
    939             if (currVop->predictionType == B_VOP)
    940             {
    941                 if ((currVol->complexity.mc >> 3) & 0x1)        /* interpolate_mc_q */
    942                     BitstreamReadBits16(stream, 8);
    943             }
    944         }
    945     }
    946 
    947     /* read intra_dc_vlc_thr */
    948     currVop->intraDCVlcThr = (int) BitstreamReadBits16(stream, 3);
    949 
    950     /* read vop_quant (currVol->quantPrecision bits) */
    951     currVop->quantizer = (int16) BitstreamReadBits16(stream, currVol->quantPrecision);
    952     if (currVop->quantizer == 0)
    953     {
    954         currVop->quantizer = video->prevVop->quantizer;
    955         status = PV_FAIL;
    956         goto return_point;
    957     }
    958 
    959 
    960     /* read vop_fcode_forward */
    961     if (currVop->predictionType != I_VOP)
    962     {
    963         tmpvar = (uint32) BitstreamReadBits16(stream, 3);
    964         if (tmpvar < 1)
    965         {
    966             currVop->fcodeForward = 1;
    967             status = PV_FAIL;
    968             goto return_point;
    969         }
    970         currVop->fcodeForward = tmpvar;
    971     }
    972     else
    973     {
    974         currVop->fcodeForward = 0;
    975     }
    976 
    977     /* read vop_fcode_backward */
    978     if (currVop->predictionType == B_VOP)
    979     {
    980         tmpvar = (uint32) BitstreamReadBits16(stream, 3);
    981         if (tmpvar < 1)
    982         {
    983             currVop->fcodeBackward = 1;
    984             status = PV_FAIL;
    985             goto return_point;
    986         }
    987         currVop->fcodeBackward = tmpvar;
    988     }
    989     else
    990     {
    991         currVop->fcodeBackward = 0;
    992     }
    993 
    994     if (currVol->scalability)
    995     {
    996         currVop->refSelectCode = (int) BitstreamReadBits16(stream, 2);
    997     }
    998 
    999 return_point:
   1000     return status;
   1001 }
   1002 
   1003 
   1004 /***********************************************************CommentBegin******
   1005 *
   1006 * -- VideoPlaneWithShortHeader -- Decodes the short_video_header information from the bitstream
   1007 * Modified :
   1008              04/23/2001.  Remove the codes related to the
   1009                  "first pass" decoding.  We use a different function
   1010                  to set up the decoder now.
   1011 ***********************************************************CommentEnd********/
   1012 PV_STATUS DecodeShortHeader(VideoDecData *video, Vop *currVop)
   1013 {
   1014     PV_STATUS status = PV_SUCCESS;
   1015     Vol *currVol = video->vol[0];
   1016     BitstreamDecVideo *stream = currVol->bitstream;
   1017     uint32 tmpvar;
   1018     int32 size;
   1019 
   1020     int extended_PTYPE = FALSE;
   1021     int UFEP = 0, custom_PFMT = 0, custom_PCF = 0;
   1022 
   1023     status = BitstreamShowBits32(stream, SHORT_VIDEO_START_MARKER_LENGTH, &tmpvar);
   1024 
   1025     if (tmpvar !=  SHORT_VIDEO_START_MARKER)
   1026     {
   1027         status = PV_FAIL;
   1028         goto return_point;
   1029     }
   1030 
   1031 
   1032     PV_BitstreamFlushBits(stream, SHORT_VIDEO_START_MARKER_LENGTH);
   1033 
   1034     /* Temporal reference. Using vop_time_increment_resolution = 30000 */
   1035     tmpvar = (uint32) BitstreamReadBits16(stream, 8);
   1036     currVop->temporalRef = (int) tmpvar;
   1037 
   1038 
   1039     currVop->timeInc = 0xff & (256 + currVop->temporalRef - video->prevVop->temporalRef);
   1040     currVol->moduloTimeBase += currVop->timeInc; /* mseconds   11/12/01 */
   1041     /* Marker Bit */
   1042     if (!BitstreamRead1Bits(stream))
   1043     {
   1044         mp4dec_log("DecodeShortHeader(): Marker bit wrong.\n");
   1045         status = PV_FAIL;
   1046         goto return_point;
   1047     }
   1048 
   1049     /* Zero Bit */
   1050     if (BitstreamRead1Bits(stream))
   1051     {
   1052         mp4dec_log("DecodeShortHeader(): Zero bit wrong.\n");
   1053         status = PV_FAIL;
   1054         goto return_point;
   1055     }
   1056 
   1057     /*split_screen_indicator*/
   1058     if (BitstreamRead1Bits(stream))
   1059     {
   1060         mp4dec_log("DecodeShortHeader(): Split Screen not supported.\n");
   1061         VideoDecoderErrorDetected(video);
   1062     }
   1063 
   1064     /*document_freeze_camera*/
   1065     if (BitstreamRead1Bits(stream))
   1066     {
   1067         mp4dec_log("DecodeShortHeader(): Freeze Camera not supported.\n");
   1068         VideoDecoderErrorDetected(video);
   1069     }
   1070 
   1071     /*freeze_picture_release*/
   1072     if (BitstreamRead1Bits(stream))
   1073     {
   1074         mp4dec_log("DecodeShortHeader(): Freeze Release not supported.\n");
   1075         VideoDecoderErrorDetected(video);
   1076     }
   1077     /* source format */
   1078     switch (BitstreamReadBits16(stream, 3))
   1079     {
   1080         case 1:
   1081             if (video->size < 128*96)
   1082             {
   1083                 status = PV_FAIL;
   1084                 goto return_point;
   1085             }
   1086             video->displayWidth = video->width =  128;
   1087             video->displayHeight = video->height  = 96;
   1088             break;
   1089 
   1090         case 2:
   1091             if (video->size < 176*144)
   1092             {
   1093                 status = PV_FAIL;
   1094                 goto return_point;
   1095             }
   1096             video->displayWidth = video->width  = 176;
   1097             video->displayHeight = video->height  = 144;
   1098             break;
   1099 
   1100         case 3:
   1101             if (video->size < 352*288)
   1102             {
   1103                 status = PV_FAIL;
   1104                 goto return_point;
   1105             }
   1106             video->displayWidth = video->width = 352;
   1107             video->displayHeight = video->height = 288;
   1108             break;
   1109 
   1110         case 4:
   1111             if (video->size < 704*576)
   1112             {
   1113                 status = PV_FAIL;
   1114                 goto return_point;
   1115             }
   1116             video->displayWidth = video->width = 704;
   1117             video->displayHeight = video->height = 576;
   1118             break;
   1119 
   1120         case 5:
   1121             if (video->size < 1408*1152)
   1122             {
   1123                 status = PV_FAIL;
   1124                 goto return_point;
   1125             }
   1126             video->displayWidth = video->width = 1408;
   1127             video->displayHeight = video->height = 1152;
   1128             break;
   1129 
   1130         case 7:
   1131             extended_PTYPE = TRUE;
   1132             break;
   1133 
   1134         default:
   1135             /* Msg("H.263 source format not legal\n"); */
   1136             status = PV_FAIL;
   1137             goto return_point;
   1138     }
   1139 
   1140 
   1141     currVop->roundingType = 0;
   1142 
   1143     if (extended_PTYPE == FALSE)
   1144     {
   1145         currVop->predictionType = (int) BitstreamRead1Bits(stream);
   1146 
   1147         /* four_reserved_zero_bits */
   1148         if (BitstreamReadBits16(stream, 4))
   1149         {
   1150             mp4dec_log("DecodeShortHeader(): Reserved bits wrong.\n");
   1151             status = PV_FAIL;
   1152             goto return_point;
   1153         }
   1154     }
   1155     else
   1156     {
   1157         UFEP = BitstreamReadBits16(stream, 3);
   1158         if (UFEP == 1)
   1159         {
   1160             /* source format */
   1161             switch (BitstreamReadBits16(stream, 3))
   1162             {
   1163                 case 1:
   1164                     if (video->size < 128*96)
   1165                     {
   1166                         status = PV_FAIL;
   1167                         goto return_point;
   1168                     }
   1169                     video->displayWidth = video->width =  128;
   1170                     video->displayHeight = video->height  = 96;
   1171                     break;
   1172 
   1173                 case 2:
   1174                     if (video->size < 176*144)
   1175                     {
   1176                         status = PV_FAIL;
   1177                         goto return_point;
   1178                     }
   1179                     video->displayWidth = video->width  = 176;
   1180                     video->displayHeight = video->height  = 144;
   1181                     break;
   1182 
   1183                 case 3:
   1184                     if (video->size < 352*288)
   1185                     {
   1186                         status = PV_FAIL;
   1187                         goto return_point;
   1188                     }
   1189                     video->displayWidth = video->width = 352;
   1190                     video->displayHeight = video->height = 288;
   1191                     break;
   1192 
   1193                 case 4:
   1194                     if (video->size < 704*576)
   1195                     {
   1196                         status = PV_FAIL;
   1197                         goto return_point;
   1198                     }
   1199                     video->displayWidth = video->width = 704;
   1200                     video->displayHeight = video->height = 576;
   1201                     break;
   1202 
   1203                 case 5:
   1204                     if (video->size < 1408*1152)
   1205                     {
   1206                         status = PV_FAIL;
   1207                         goto return_point;
   1208                     }
   1209                     video->displayWidth = video->width = 1408;
   1210                     video->displayHeight = video->height = 1152;
   1211                     break;
   1212 
   1213                 case 6:
   1214                     custom_PFMT = TRUE;
   1215                     break;
   1216 
   1217                 default:
   1218                     /* Msg("H.263 source format not legal\n"); */
   1219                     status = PV_FAIL;
   1220                     goto return_point;
   1221             }
   1222 
   1223             custom_PCF = BitstreamRead1Bits(stream);
   1224             /* unrestricted MV */
   1225             if (BitstreamRead1Bits(stream))
   1226             {
   1227                 status = PV_FAIL;
   1228                 goto return_point;
   1229             }
   1230             /* SAC */
   1231             if (BitstreamRead1Bits(stream))
   1232             {
   1233                 status = PV_FAIL;
   1234                 goto return_point;
   1235             }
   1236 
   1237             /* AP */
   1238             if (BitstreamRead1Bits(stream))
   1239             {
   1240                 status = PV_FAIL;
   1241                 goto return_point;
   1242             }
   1243 
   1244             video->advanced_INTRA = BitstreamRead1Bits(stream);
   1245 
   1246             video->deblocking = BitstreamRead1Bits(stream);
   1247 
   1248             video->slice_structure = BitstreamRead1Bits(stream);
   1249 
   1250             /* RPS, ISD, AIV */
   1251             if (BitstreamReadBits16(stream, 3))
   1252             {
   1253                 status = PV_FAIL;
   1254                 goto return_point;
   1255             }
   1256             video->modified_quant = BitstreamRead1Bits(stream);
   1257 
   1258             /* Marker Bit and reserved*/
   1259             if (BitstreamReadBits16(stream, 4) != 8)
   1260             {
   1261                 status = PV_FAIL;
   1262                 goto return_point;
   1263             }
   1264         }
   1265 #ifndef PV_ANNEX_IJKT_SUPPORT
   1266         if (video->advanced_INTRA | video->deblocking | video->modified_quant | video->modified_quant)
   1267         {
   1268             status = PV_FAIL;
   1269             goto return_point;
   1270         }
   1271 #endif
   1272 
   1273         if (UFEP == 0 || UFEP == 1)
   1274         {
   1275             tmpvar = BitstreamReadBits16(stream, 3);
   1276             if (tmpvar > 1)
   1277             {
   1278                 status = PV_FAIL;
   1279                 goto return_point;
   1280             }
   1281             currVop->predictionType = tmpvar;
   1282             /* RPR */
   1283             if (BitstreamRead1Bits(stream))
   1284             {
   1285                 status = PV_FAIL;
   1286                 goto return_point;
   1287             }
   1288 
   1289             /* RRU */
   1290             if (BitstreamRead1Bits(stream))
   1291             {
   1292                 status = PV_FAIL;
   1293                 goto return_point;
   1294             }
   1295             currVop->roundingType = (int) BitstreamRead1Bits(stream);
   1296             if (BitstreamReadBits16(stream, 3) != 1)
   1297             {
   1298                 status = PV_FAIL;
   1299                 goto return_point;
   1300             }
   1301         }
   1302         else
   1303         {
   1304             status = PV_FAIL;
   1305             goto return_point;
   1306         }
   1307         /* CPM */
   1308         if (BitstreamRead1Bits(stream))
   1309         {
   1310             status = PV_FAIL;
   1311             goto return_point;
   1312         }
   1313         /* CPFMT */
   1314         if (custom_PFMT == 1 && UFEP == 1)
   1315         {
   1316             /* aspect ratio */
   1317             tmpvar = BitstreamReadBits16(stream, 4);
   1318             if (tmpvar == 0)
   1319             {
   1320                 status = PV_FAIL;
   1321                 goto return_point;
   1322             }
   1323             /* Extended PAR */
   1324             if (tmpvar == 0xF)
   1325             {
   1326                 /* Read par_width and par_height but do nothing */
   1327                 /* par_width */
   1328                 tmpvar = BitstreamReadBits16(stream, 8);
   1329 
   1330                 /* par_height */
   1331                 tmpvar = BitstreamReadBits16(stream, 8);
   1332             }
   1333             tmpvar = BitstreamReadBits16(stream, 9);
   1334 
   1335             video->displayWidth = (tmpvar + 1) << 2;
   1336             video->width = (video->displayWidth + 15) & -16;
   1337             /* marker bit */
   1338             if (!BitstreamRead1Bits(stream))
   1339             {
   1340                 status = PV_FAIL;
   1341                 goto return_point;
   1342             }
   1343             tmpvar = BitstreamReadBits16(stream, 9);
   1344             if (tmpvar == 0)
   1345             {
   1346                 status = PV_FAIL;
   1347                 goto return_point;
   1348             }
   1349             video->displayHeight = tmpvar << 2;
   1350             video->height = (video->displayHeight + 15) & -16;
   1351 
   1352             if (video->height * video->width > video->size)
   1353             {
   1354                 status = PV_FAIL;
   1355                 goto return_point;
   1356             }
   1357 
   1358             video->nTotalMB = video->width / MB_SIZE * video->height / MB_SIZE;
   1359 
   1360             if (video->nTotalMB <= 48)
   1361             {
   1362                 video->nBitsForMBID = 6;
   1363             }
   1364             else if (video->nTotalMB <= 99)
   1365             {
   1366                 video->nBitsForMBID = 7;
   1367             }
   1368             else if (video->nTotalMB <= 396)
   1369             {
   1370                 video->nBitsForMBID = 9;
   1371             }
   1372             else if (video->nTotalMB <= 1584)
   1373             {
   1374                 video->nBitsForMBID = 11;
   1375             }
   1376             else if (video->nTotalMB <= 6336)
   1377             {
   1378                 video->nBitsForMBID = 13 ;
   1379             }
   1380             else if (video->nTotalMB <= 9216)
   1381             {
   1382                 video->nBitsForMBID = 14 ;
   1383             }
   1384             else
   1385             {
   1386                 status = PV_FAIL;
   1387                 goto return_point;
   1388             }
   1389         }
   1390         if (UFEP == 1 && custom_PCF == 1)
   1391         {
   1392             BitstreamRead1Bits(stream);
   1393 
   1394             tmpvar = BitstreamReadBits16(stream, 7);
   1395             if (tmpvar == 0)
   1396             {
   1397                 status = PV_FAIL;
   1398                 goto return_point;
   1399             }
   1400         }
   1401 
   1402         if (custom_PCF == 1)
   1403         {
   1404             currVop->ETR = BitstreamReadBits16(stream, 2);
   1405         }
   1406 
   1407         if (UFEP == 1 && video->slice_structure == 1)
   1408         {
   1409             /* SSS */
   1410             tmpvar = BitstreamReadBits16(stream, 2);
   1411             if (tmpvar != 0)
   1412             {
   1413                 status = PV_FAIL;
   1414                 goto return_point;
   1415             }
   1416         }
   1417     }
   1418 
   1419     /* Recalculate number of macroblocks per row & col since */
   1420     /*  the frame size can change.           04/23/2001.   */
   1421     video->nMBinGOB = video->nMBPerRow = video->width / MB_SIZE;
   1422     video->nGOBinVop = video->nMBPerCol = video->height / MB_SIZE;
   1423     video->nTotalMB = video->nMBPerRow * video->nMBPerCol;
   1424     if (custom_PFMT == 0  || UFEP == 0)
   1425     {
   1426         video->nBitsForMBID = CalcNumBits((uint)video->nTotalMB - 1); /* otherwise calculate above */
   1427     }
   1428     size = (int32)video->width * video->height;
   1429     if (video->currVop->predictionType == P_VOP && size > video->videoDecControls->size)
   1430     {
   1431         status = PV_FAIL;
   1432         goto return_point;
   1433     }
   1434     video->videoDecControls->size = size;
   1435     video->currVop->uChan = video->currVop->yChan + size;
   1436     video->currVop->vChan = video->currVop->uChan + (size >> 2);
   1437     video->prevVop->uChan = video->prevVop->yChan + size;
   1438     video->prevVop->vChan = video->prevVop->uChan + (size >> 2);
   1439 
   1440 
   1441     currVop->quantizer = (int16) BitstreamReadBits16(stream, 5);
   1442 
   1443     if (currVop->quantizer == 0)                          /*  04/03/01 */
   1444     {
   1445         currVop->quantizer = video->prevVop->quantizer;
   1446         status = PV_FAIL;
   1447         goto return_point;
   1448     }
   1449 
   1450 
   1451     /* Zero bit */
   1452     if (extended_PTYPE == FALSE)
   1453     {
   1454         if (BitstreamRead1Bits(stream))
   1455         {
   1456             mp4dec_log("DecodeShortHeader(): Zero bit wrong.\n");
   1457             status = PV_FAIL;
   1458             goto return_point;
   1459         }
   1460     }
   1461     /* pei */
   1462     tmpvar = (uint32) BitstreamRead1Bits(stream);
   1463 
   1464     while (tmpvar)
   1465     {
   1466         tmpvar = (uint32) BitstreamReadBits16(stream, 8); /* "PSPARE" */
   1467         tmpvar = (uint32) BitstreamRead1Bits(stream); /* "PEI" */
   1468     }
   1469 
   1470     if (video->slice_structure)  /* ANNEX_K */
   1471     {
   1472         if (!BitstreamRead1Bits(stream))  /* SEPB1 */
   1473         {
   1474             status = PV_FAIL;
   1475             goto return_point;
   1476         }
   1477 
   1478         //  if (currVol->nBitsForMBID //
   1479         if (BitstreamReadBits16(stream, video->nBitsForMBID))
   1480         {
   1481             status = PV_FAIL;             /* no ASO, RS support for Annex K */
   1482             goto return_point;
   1483         }
   1484 
   1485         if (!BitstreamRead1Bits(stream))  /*SEPB3 */
   1486         {
   1487             status = PV_FAIL;
   1488             goto return_point;
   1489         }
   1490 
   1491     }
   1492     /* Setting of other VOP-header parameters */
   1493     currVop->gobNumber = 0;
   1494     currVop->vopCoded = 1;
   1495 
   1496     currVop->intraDCVlcThr = 0;
   1497     currVop->gobFrameID = 0; /* initial value,  05/22/00 */
   1498     currVol->errorResDisable = 0;
   1499     /*PutVopInterlaced(0,curr_vop); no implemented yet */
   1500     if (currVop->predictionType != I_VOP)
   1501         currVop->fcodeForward = 1;
   1502     else
   1503         currVop->fcodeForward = 0;
   1504 
   1505 return_point:
   1506 
   1507     return status;
   1508 }
   1509 /***********************************************************CommentBegin******
   1510 *
   1511 * -- PV_DecodeVop -- Decodes the VOP information from the bitstream
   1512 *
   1513 *   04/12/2000
   1514 *                   Initial port to the new PV decoder library format.
   1515 *                   This function is different from the one in MoMuSys MPEG-4
   1516 *                   visual decoder.  We handle combined mode with or withput
   1517 *                   error resilience and H.263 mode through the sam path now.
   1518 *
   1519 *   05/04/2000
   1520 *                   Added temporal scalability to the decoder.
   1521 *
   1522 ***********************************************************CommentEnd********/
   1523 PV_STATUS PV_DecodeVop(VideoDecData *video)
   1524 {
   1525     Vol *currVol = video->vol[video->currLayer];
   1526     PV_STATUS status;
   1527     uint32 tmpvar;
   1528 
   1529     /*****
   1530     *   Do scalable or non-scalable decoding of the current VOP
   1531     *****/
   1532 
   1533     if (!currVol->scalability)
   1534     {
   1535         if (currVol->dataPartitioning)
   1536         {
   1537             /* Data partitioning mode comes here */
   1538             status = DecodeFrameDataPartMode(video);
   1539         }
   1540         else
   1541         {
   1542             /* Combined mode with or without error resilience */
   1543             /*    and short video header comes here.          */
   1544             status = DecodeFrameCombinedMode(video);
   1545         }
   1546     }
   1547     else
   1548     {
   1549 #ifdef DO_NOT_FOLLOW_STANDARD
   1550         /* according to the standard, only combined mode is allowed */
   1551         /*    in the enhancement layer.          06/01/2000.        */
   1552         if (currVol->dataPartitioning)
   1553         {
   1554             /* Data partitioning mode comes here */
   1555             status = DecodeFrameDataPartMode(video);
   1556         }
   1557         else
   1558         {
   1559             /* Combined mode with or without error resilience */
   1560             /*    and short video header comes here.          */
   1561             status = DecodeFrameCombinedMode(video);
   1562         }
   1563 #else
   1564         status = DecodeFrameCombinedMode(video);
   1565 #endif
   1566     }
   1567 
   1568     /* This part is for consuming Visual_object_sequence_end_code and EOS Code */   /*  10/15/01 */
   1569     if (!video->shortVideoHeader)
   1570     {
   1571         /* at this point bitstream is expected to be byte aligned */
   1572         BitstreamByteAlignNoForceStuffing(currVol->bitstream);
   1573 
   1574         status = BitstreamShowBits32HC(currVol->bitstream, &tmpvar);  /*  07/07/01 */
   1575         if (tmpvar == VISUAL_OBJECT_SEQUENCE_END_CODE)/* VOS_END_CODE */
   1576         {
   1577             PV_BitstreamFlushBits(currVol->bitstream, 16);
   1578             PV_BitstreamFlushBits(currVol->bitstream, 16);
   1579         }
   1580 
   1581     }
   1582     else
   1583     {
   1584 #ifdef PV_ANNEX_IJKT_SUPPORT
   1585         if (video->deblocking)
   1586         {
   1587             H263_Deblock(video->currVop->yChan, video->width, video->height, video->QPMB, video->headerInfo.Mode, 0, 0);
   1588             H263_Deblock(video->currVop->uChan, video->width >> 1, video->height >> 1, video->QPMB, video->headerInfo.Mode, 1, video->modified_quant);
   1589             H263_Deblock(video->currVop->vChan, video->width >> 1, video->height >> 1, video->QPMB, video->headerInfo.Mode, 1, video->modified_quant);
   1590         }
   1591 #endif
   1592         /* Read EOS code for shortheader bitstreams    */
   1593         status = BitstreamShowBits32(currVol->bitstream, 22, &tmpvar);
   1594         if (tmpvar == SHORT_VIDEO_END_MARKER)
   1595         {
   1596             PV_BitstreamFlushBits(currVol->bitstream, 22);
   1597         }
   1598         else
   1599         {
   1600             status = PV_BitstreamShowBitsByteAlign(currVol->bitstream, 22, &tmpvar);
   1601             if (tmpvar == SHORT_VIDEO_END_MARKER)
   1602             {
   1603                 PV_BitstreamByteAlign(currVol->bitstream);
   1604                 PV_BitstreamFlushBits(currVol->bitstream, 22);
   1605             }
   1606         }
   1607     }
   1608     return status;
   1609 }
   1610 
   1611 
   1612 /***********************************************************CommentBegin******
   1613 *
   1614 * -- CalcVopDisplayTime -- calculate absolute time when VOP is to be displayed
   1615 *
   1616 *   04/12/2000 Initial port to the new PV decoder library format.
   1617 *
   1618 ***********************************************************CommentEnd********/
   1619 uint32 CalcVopDisplayTime(Vol *currVol, Vop *currVop, int shortVideoHeader)
   1620 {
   1621     uint32 display_time;
   1622 
   1623 
   1624     /*****
   1625     *   Calculate the time when the VOP is to be displayed next
   1626     *****/
   1627 
   1628     if (!shortVideoHeader)
   1629     {
   1630         display_time = (uint32)(currVol->moduloTimeBase + (((int32)currVop->timeInc - (int32)currVol->timeInc_offset) * 1000) / ((int32)currVol->timeIncrementResolution));  /*  11/12/2001 */
   1631         if (currVop->timeStamp >= display_time)
   1632         {
   1633             display_time += 1000;  /* this case is valid if GOVHeader timestamp is ignored */
   1634         }
   1635     }
   1636     else
   1637     {
   1638         display_time = (uint32)(currVol->moduloTimeBase * 33 + (currVol->moduloTimeBase * 11) / 30); /*  11/12/2001 */
   1639     }
   1640 
   1641     return(display_time);
   1642 }
   1643 
   1644