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 "vlc_decode.h"
     20 #include "zigzag.h"
     21 
     22 
     23 typedef PV_STATUS(*VlcDecFuncP)(BitstreamDecVideo *stream, Tcoef *pTcoef);
     24 static const uint8 AC_rowcol[64] = {    0, 0, 0, 0, 0, 0, 0, 0,
     25                                         0, 1, 1, 1, 1, 1, 1, 1,
     26                                         0, 1, 1, 1, 1, 1, 1, 1,
     27                                         0, 1, 1, 1, 1, 1, 1, 1,
     28                                         0, 1, 1, 1, 1, 1, 1, 1,
     29                                         0, 1, 1, 1, 1, 1, 1, 1,
     30                                         0, 1, 1, 1, 1, 1, 1, 1,
     31                                         0, 1, 1, 1, 1, 1, 1, 1,
     32                                    };
     33 static const uint8 mask[8] = /*  for fast bitmap */
     34     {128, 64, 32, 16, 8, 4, 2, 1};
     35 
     36 
     37 
     38 /***********************************************************CommentBegin******
     39 *
     40 * -- VlcDequantMpegBlock -- Decodes the DCT coefficients of one 8x8 block and perform
     41             dequantization using Mpeg mode.
     42     Date:       08/08/2000
     43 
     44     Modified:      3/21/01
     45                 Added pre IDCT clipping, new ACDC prediction structure, ACDC prediction clipping,
     46                 16-bit int case, removed multiple zigzaging
     47 ******************************************************************************/
     48 
     49 #ifdef PV_SUPPORT_MAIN_PROFILE
     50 int VlcDequantMpegIntraBlock(void *vid, int comp, int switched,
     51                              uint8 *bitmapcol, uint8 *bitmaprow)
     52 {
     53     VideoDecData *video = (VideoDecData*) vid;
     54     Vol *currVol = video->vol[video->currLayer];
     55     BitstreamDecVideo *stream = video->bitstream;
     56     int16 *datablock = video->mblock->block[comp]; /* 10/20/2000, assume it has been reset of all-zero !!!*/
     57     int mbnum = video->mbnum;
     58     uint CBP = video->headerInfo.CBP[mbnum];
     59     int QP = video->QPMB[mbnum];
     60     typeDCStore *DC = video->predDC + mbnum;
     61     int x_pos = video->mbnum_col;
     62     typeDCACStore *DCAC_row = video->predDCAC_row + x_pos;
     63     typeDCACStore *DCAC_col = video->predDCAC_col;
     64     uint ACpred_flag = (uint) video->acPredFlag[mbnum];
     65 
     66     /*** VLC *****/
     67     int i, j, k;
     68     Tcoef run_level;
     69     int last, return_status;
     70     VlcDecFuncP vlcDecCoeff;
     71     int direction;
     72     const int *inv_zigzag;
     73     /*** Quantizer ****/
     74     int dc_scaler;
     75     int sum;
     76     int *qmat;
     77     int32 temp;
     78 
     79     const int B_Xtab[6] = {0, 1, 0, 1, 2, 3};
     80     const int B_Ytab[6] = {0, 0, 1, 1, 2, 3};
     81 
     82     int16 *dcac_row, *dcac_col;
     83 
     84     dcac_row = (*DCAC_row)[B_Xtab[comp]];
     85     dcac_col = (*DCAC_col)[B_Ytab[comp]];
     86 
     87 
     88     i = 1 - switched;
     89 
     90 #ifdef FAST_IDCT
     91     *((uint32*)bitmapcol) = *((uint32*)(bitmapcol + 4)) = 0;
     92     *bitmaprow = 0;
     93 #endif
     94 
     95 
     96     /* select which Huffman table to be used */
     97     vlcDecCoeff = video->vlcDecCoeffIntra;
     98 
     99     dc_scaler = (comp < 4) ? video->mblock->DCScalarLum : video->mblock->DCScalarChr;
    100 
    101     /* enter the zero run decoding loop */
    102     sum = 0;
    103     qmat = currVol->iqmat;
    104 
    105     /* perform only VLC decoding */
    106     /* We cannot do DCACrecon before VLC decoding.  10/17/2000 */
    107     doDCACPrediction(video, comp, datablock, &direction);
    108     if (!ACpred_flag) direction = 0;
    109     inv_zigzag = zigzag_inv + (ACpred_flag << 6) + (direction << 6);
    110     if (CBP & (1 << (5 - comp)))
    111     {
    112         do
    113         {
    114             return_status = (*vlcDecCoeff)(stream, &run_level);
    115             if (return_status != PV_SUCCESS)
    116             {
    117                 last = 1;/*  11/1/2000 let it slips undetected, just like
    118                          in original version */
    119                 i = VLC_ERROR;
    120                 ACpred_flag = 0;    /* no of coefficients should not get reset   03/07/2002 */
    121                 break;
    122             }
    123 
    124             i += run_level.run;
    125             last = run_level.last;
    126             if (i >= 64)
    127             {
    128                 /*  i = NCOEFF_BLOCK; */    /*  11/1/00 */
    129                 ACpred_flag = 0;    /* no of coefficients should not get reset   03/07/2002 */
    130                 i = VLC_NO_LAST_BIT;
    131                 last = 1;
    132                 break;
    133             }
    134 
    135             k = inv_zigzag[i];
    136 
    137             if (run_level.sign == 1)
    138             {
    139                 datablock[k] -= run_level.level;
    140             }
    141             else
    142             {
    143                 datablock[k] += run_level.level;
    144             }
    145 
    146             if (AC_rowcol[k])
    147             {
    148                 temp = (int32)datablock[k] * qmat[k] * QP;
    149                 temp = (temp + (0x7 & (temp >> 31))) >> 3;
    150                 if (temp > 2047) temp = 2047;
    151                 else if (temp < -2048) temp = -2048;
    152                 datablock[k] = (int) temp;
    153 
    154 #ifdef FAST_IDCT
    155                 bitmapcol[k&0x7] |= mask[k>>3];
    156 #endif
    157                 sum ^= temp;
    158             }
    159 
    160             i++;
    161         }
    162         while (!last);
    163 
    164     }
    165     else
    166     {
    167         i = 1;       /*  04/26/01  needed for switched case */
    168     }
    169     ///// NEED TO DEQUANT THOSE PREDICTED AC COEFF
    170     /* dequantize the rest of AC predicted coeff that haven't been dequant */
    171     if (ACpred_flag)
    172     {
    173 
    174         i = NCOEFF_BLOCK; /* otherwise, FAST IDCT won't work correctly,  10/18/2000 */
    175 
    176         if (!direction) /* check vertical */
    177         {
    178             dcac_row[0]  = datablock[1];
    179             dcac_row[1]  = datablock[2];
    180             dcac_row[2]  = datablock[3];
    181             dcac_row[3]  = datablock[4];
    182             dcac_row[4]  = datablock[5];
    183             dcac_row[5]  = datablock[6];
    184             dcac_row[6]  = datablock[7];
    185 
    186             for (j = 0, k = 8; k < 64; k += 8, j++)
    187             {
    188                 if (dcac_col[j] = datablock[k])
    189                 {     /* ACDC clipping  03/26/01 */
    190                     if (datablock[k] > 2047) dcac_col[j] = 2047;
    191                     else if (datablock[k] < -2048) dcac_col[j] = -2048;
    192 
    193                     temp = (int32)dcac_col[j] * qmat[k] * QP;
    194                     temp = (temp + (0x7 & (temp >> 31))) >> 3;  /*  03/26/01*/
    195                     if (temp > 2047) temp = 2047;
    196                     else if (temp < -2048) temp = -2048;
    197                     datablock[k] = (int)temp;
    198                     sum ^= temp; /*  7/5/01 */
    199 #ifdef FAST_IDCT
    200                     bitmapcol[0] |= mask[k>>3];
    201 #endif
    202 
    203                 }
    204             }
    205             for (k = 1; k < 8; k++)
    206             {
    207                 if (datablock[k])
    208                 {
    209                     temp = (int32)datablock[k] * qmat[k] * QP;
    210                     temp = (temp + (0x7 & (temp >> 31))) >> 3;  /*  03/26/01*/
    211                     if (temp > 2047) temp = 2047;
    212                     else if (temp < -2048) temp = -2048;
    213                     datablock[k] = (int)temp;
    214                     sum ^= temp; /*  7/5/01 */
    215 #ifdef FAST_IDCT
    216                     bitmapcol[k] |= 128;
    217 #endif
    218 
    219                 }
    220             }
    221 
    222         }
    223         else
    224         {
    225 
    226             dcac_col[0]  = datablock[8];
    227             dcac_col[1]  = datablock[16];
    228             dcac_col[2]  = datablock[24];
    229             dcac_col[3]  = datablock[32];
    230             dcac_col[4]  = datablock[40];
    231             dcac_col[5]  = datablock[48];
    232             dcac_col[6]  = datablock[56];
    233 
    234 
    235             for (j = 0, k = 1; k < 8; k++, j++)
    236             {
    237                 if (dcac_row[j] = datablock[k])
    238                 {     /* ACDC clipping  03/26/01 */
    239                     if (datablock[k] > 2047) dcac_row[j] = 2047;
    240                     else if (datablock[k] < -2048) dcac_row[j] = -2048;
    241 
    242                     temp = (int32)dcac_row[j] * qmat[k] * QP;
    243                     temp = (temp + (0x7 & (temp >> 31))) >> 3;  /*  03/26/01 */
    244                     if (temp > 2047) temp = 2047;
    245                     else if (temp < -2048) temp = -2048;
    246                     datablock[k] = (int)temp;
    247                     sum ^= temp;
    248 #ifdef FAST_IDCT
    249                     bitmapcol[k] |= 128;
    250 #endif
    251 
    252                 }
    253             }
    254 
    255             for (k = 8; k < 64; k += 8)
    256             {
    257                 if (datablock[k])
    258                 {
    259                     temp = (int32)datablock[k] * qmat[k] * QP;
    260                     temp = (temp + (0x7 & (temp >> 31))) >> 3;  /*  03/26/01 */
    261                     if (temp > 2047) temp = 2047;
    262                     else if (temp < -2048) temp = -2048;
    263                     datablock[k] = (int)temp;
    264                     sum ^= temp;
    265 #ifdef FAST_IDCT
    266                     bitmapcol[0] |= mask[k>>3];
    267 #endif
    268                 }
    269             }
    270 
    271         }
    272     }
    273     else
    274     {
    275 
    276         /* Store the qcoeff-values needed later for prediction */
    277 
    278         dcac_row[0] = datablock[1];                /*  ACDC, no need for clipping */
    279         dcac_row[1] = datablock[2];
    280         dcac_row[2] = datablock[3];
    281         dcac_row[3] = datablock[4];
    282         dcac_row[4] = datablock[5];
    283         dcac_row[5] = datablock[6];
    284         dcac_row[6] = datablock[7];
    285 
    286         dcac_col[0] = datablock[8];
    287         dcac_col[1] = datablock[16];
    288         dcac_col[2] = datablock[24];
    289         dcac_col[3] = datablock[32];
    290         dcac_col[4] = datablock[40];
    291         dcac_col[5] = datablock[48];
    292         dcac_col[6] = datablock[56];
    293 
    294         for (k = 1; k < 8; k++)
    295         {
    296             if (datablock[k])
    297             {
    298                 temp = (int32)datablock[k] * qmat[k] * QP;
    299                 temp = (temp + (0x7 & (temp >> 31))) >> 3;  /*  03/26/01*/
    300                 if (temp > 2047) temp = 2047;
    301                 else if (temp < -2048) temp = -2048;
    302                 datablock[k] = (int)temp;
    303                 sum ^= temp; /*  7/5/01 */
    304 #ifdef FAST_IDCT
    305                 bitmapcol[k] |= 128;
    306 #endif
    307 
    308             }
    309         }
    310         for (k = 8; k < 64; k += 8)
    311         {
    312             if (datablock[k])
    313             {
    314                 temp = (int32)datablock[k] * qmat[k] * QP;
    315                 temp = (temp + (0x7 & (temp >> 31))) >> 3;  /*  03/26/01 */
    316                 if (temp > 2047) temp = 2047;
    317                 else if (temp < -2048) temp = -2048;
    318                 datablock[k] = (int)temp;
    319                 sum ^= temp;
    320 #ifdef FAST_IDCT
    321                 bitmapcol[0] |= mask[k>>3];
    322 #endif
    323             }
    324         }
    325 
    326     }
    327 
    328 
    329 
    330     if (datablock[0])
    331     {
    332         temp = (int32)datablock[0] * dc_scaler;
    333         if (temp > 2047) temp = 2047;            /*  03/14/01 */
    334         else if (temp < -2048)  temp = -2048;
    335         datablock[0] = (int)temp;
    336         sum ^= temp;
    337 #ifdef FAST_IDCT
    338         bitmapcol[0] |= 128;
    339 #endif
    340     }
    341 
    342     if ((sum & 1) == 0)
    343     {
    344         datablock[63] = datablock[63] ^ 0x1;
    345 #ifdef FAST_IDCT   /*  7/5/01, need to update bitmap */
    346         if (datablock[63])
    347             bitmapcol[7] |= 1;
    348 #endif
    349         i = (-64 & i) | NCOEFF_BLOCK;   /*  if i > -1 then i is set to NCOEFF_BLOCK */
    350     }
    351 
    352 
    353 #ifdef FAST_IDCT
    354     if (i > 10)
    355     {
    356         for (k = 1; k < 4; k++)
    357         {
    358             if (bitmapcol[k] != 0)
    359             {
    360                 (*bitmaprow) |= mask[k];
    361             }
    362         }
    363     }
    364 #endif
    365 
    366     /* Store the qcoeff-values needed later for prediction */
    367     (*DC)[comp] = datablock[0];
    368     return i;
    369 
    370 }
    371 
    372 
    373 /***********************************************************CommentBegin******
    374 *
    375 * -- VlcDequantMpegInterBlock -- Decodes the DCT coefficients of one 8x8 block and perform
    376             dequantization using Mpeg mode for INTER block.
    377     Date:       08/08/2000
    378     Modified:              3/21/01
    379                 clean up, added clipping, 16-bit int case, new ACDC prediction
    380 ******************************************************************************/
    381 
    382 
    383 int VlcDequantMpegInterBlock(void *vid, int comp,
    384                              uint8 *bitmapcol, uint8 *bitmaprow)
    385 {
    386     VideoDecData *video = (VideoDecData*) vid;
    387     BitstreamDecVideo *stream = video->bitstream;
    388     Vol *currVol = video->vol[video->currLayer];
    389     int16 *datablock = video->mblock->block[comp]; /* 10/20/2000, assume it has been reset of all-zero !!!*/
    390     int mbnum = video->mbnum;
    391     int QP = video->QPMB[mbnum];
    392     /*** VLC *****/
    393     int i, k;
    394     Tcoef run_level;
    395     int last, return_status;
    396     VlcDecFuncP vlcDecCoeff;
    397 
    398     /*** Quantizer ****/
    399     int sum;
    400     int *qmat;
    401 
    402     int32 temp;
    403 
    404     i = 0 ;
    405 
    406 #ifdef FAST_IDCT
    407     *((uint32*)bitmapcol) = *((uint32*)(bitmapcol + 4)) = 0;
    408     *bitmaprow = 0;
    409 #endif
    410 
    411     /* select which Huffman table to be used */
    412     vlcDecCoeff = video->vlcDecCoeffInter;
    413 
    414     /* enter the zero run decoding loop */
    415     sum = 0;
    416     qmat = currVol->niqmat;
    417     do
    418     {
    419         return_status = (*vlcDecCoeff)(stream, &run_level);
    420         if (return_status != PV_SUCCESS)
    421         {
    422             last = 1;/*  11/1/2000 let it slips undetected, just like
    423                      in original version */
    424             i = VLC_ERROR;
    425             sum = 1;    /* no of coefficients should not get reset   03/07/2002 */
    426             break;
    427         }
    428 
    429         i += run_level.run;
    430         last = run_level.last;
    431         if (i >= 64)
    432         {
    433             /*  i = NCOEFF_BLOCK; */    /*  11/1/00 */
    434             //return VLC_NO_LAST_BIT;
    435             i = VLC_NO_LAST_BIT;
    436             last = 1;
    437             sum = 1;    /* no of coefficients should not get reset   03/07/2002 */
    438             break;
    439         }
    440 
    441         k = zigzag_inv[i];
    442 
    443         if (run_level.sign == 1)
    444         {
    445             temp = (-(int32)(2 * run_level.level + 1) * qmat[k] * QP + 15) >> 4; /*  03/23/01 */
    446             if (temp < -2048) temp = - 2048;
    447         }
    448         else
    449         {
    450             temp = ((int32)(2 * run_level.level + 1) * qmat[k] * QP) >> 4; /*  03/23/01 */
    451             if (temp > 2047) temp = 2047;
    452         }
    453 
    454         datablock[k] = (int)temp;
    455 
    456 #ifdef FAST_IDCT
    457         bitmapcol[k&0x7] |= mask[k>>3];
    458 #endif
    459         sum ^= temp;
    460 
    461         i++;
    462     }
    463     while (!last);
    464 
    465     if ((sum & 1) == 0)
    466     {
    467         datablock[63] = datablock[63] ^ 0x1;
    468 #ifdef FAST_IDCT   /*  7/5/01, need to update bitmap */
    469         if (datablock[63])
    470             bitmapcol[7] |= 1;
    471 #endif
    472         i = NCOEFF_BLOCK;
    473     }
    474 
    475 
    476 #ifdef FAST_IDCT
    477     if (i > 10)
    478     {
    479         for (k = 1; k < 4; k++)               /*  07/19/01 */
    480         {
    481             if (bitmapcol[k] != 0)
    482             {
    483                 (*bitmaprow) |= mask[k];
    484             }
    485         }
    486     }
    487 #endif
    488 
    489     return i;
    490 }
    491 #endif
    492 /***********************************************************CommentBegin******
    493 *
    494 * -- VlcDequantIntraH263Block -- Decodes the DCT coefficients of one 8x8 block and perform
    495             dequantization in H.263 mode for INTRA block.
    496     Date:       08/08/2000
    497     Modified:               3/21/01
    498                 clean up, added clipping, 16-bit int case, removed multiple zigzaging
    499 ******************************************************************************/
    500 
    501 
    502 int VlcDequantH263IntraBlock(VideoDecData *video, int comp, int switched,
    503                              uint8 *bitmapcol, uint8 *bitmaprow)
    504 {
    505     BitstreamDecVideo *stream = video->bitstream;
    506     int16 *datablock = video->mblock->block[comp]; /* 10/20/2000, assume it has been reset of all-zero !!!*/
    507     int32 temp;
    508     int mbnum = video->mbnum;
    509     uint CBP = video->headerInfo.CBP[mbnum];
    510     int QP = video->QPMB[mbnum];
    511     typeDCStore *DC = video->predDC + mbnum;
    512     int x_pos = video->mbnum_col;
    513     typeDCACStore *DCAC_row = video->predDCAC_row + x_pos;
    514     typeDCACStore *DCAC_col = video->predDCAC_col;
    515     uint ACpred_flag = (uint) video->acPredFlag[mbnum];
    516 
    517     /*** VLC *****/
    518     int i, j, k;
    519     Tcoef run_level;
    520     int last, return_status;
    521     VlcDecFuncP vlcDecCoeff;
    522     int direction;
    523     const int *inv_zigzag;
    524 
    525     /*** Quantizer ****/
    526     int dc_scaler;
    527     int sgn_coeff;
    528 
    529 
    530 
    531     const int B_Xtab[6] = {0, 1, 0, 1, 2, 3};
    532     const int B_Ytab[6] = {0, 0, 1, 1, 2, 3};
    533 
    534     int16 *dcac_row, *dcac_col;
    535 
    536     dcac_row = (*DCAC_row)[B_Xtab[comp]];
    537     dcac_col = (*DCAC_col)[B_Ytab[comp]];
    538 
    539 #ifdef FAST_IDCT
    540     *((uint32*)bitmapcol) = *((uint32*)(bitmapcol + 4)) = 0;
    541     *bitmaprow = 0;
    542 #endif
    543     /* select which Huffman table to be used */
    544     vlcDecCoeff = video->vlcDecCoeffIntra;
    545 
    546     dc_scaler = (comp < 4) ? video->mblock->DCScalarLum : video->mblock->DCScalarChr;
    547 
    548     /* perform only VLC decoding */
    549     doDCACPrediction(video, comp, datablock, &direction);
    550     if (!ACpred_flag) direction = 0;
    551 
    552     inv_zigzag = zigzag_inv + (ACpred_flag << 6) + (direction << 6);  /*  04/17/01 */
    553 
    554     i = 1;
    555     if (CBP & (1 << (5 - comp)))
    556     {
    557         i = 1 - switched;
    558         do
    559         {
    560             return_status = (*vlcDecCoeff)(stream, &run_level);
    561             if (return_status != PV_SUCCESS)
    562             {
    563                 last = 1;/* 11/1/2000 let it slips undetected, just like
    564                          in original version */
    565                 i = VLC_ERROR;
    566                 ACpred_flag = 0;   /* no of coefficients should not get reset   03/07/2002 */
    567                 break;
    568             }
    569 
    570             i += run_level.run;
    571             last = run_level.last;
    572             if (i >= 64)
    573             {
    574                 ACpred_flag = 0;    /* no of coefficients should not get reset   03/07/2002 */
    575                 i = VLC_NO_LAST_BIT;
    576                 last = 1;
    577                 break;
    578             }
    579 
    580             k = inv_zigzag[i];
    581 
    582             if (run_level.sign == 1)
    583             {
    584                 datablock[k] -= run_level.level;
    585                 sgn_coeff = -1;
    586             }
    587             else
    588             {
    589                 datablock[k] += run_level.level;
    590                 sgn_coeff = 1;
    591             }
    592 
    593 
    594             if (AC_rowcol[k])   /*  10/25/2000 */
    595             {
    596                 temp = (int32)QP * (2 * datablock[k] + sgn_coeff) - sgn_coeff + (QP & 1) * sgn_coeff;
    597                 if (temp > 2047) temp = 2047;            /*  03/14/01 */
    598                 else if (temp < -2048)  temp = -2048;
    599                 datablock[k] = (int16) temp;
    600 
    601 #ifdef FAST_IDCT
    602                 bitmapcol[k&0x7] |= mask[k>>3];
    603 #endif
    604             }
    605 
    606             i++;
    607         }
    608         while (!last);
    609 
    610     }
    611 
    612     ///// NEED TO DEQUANT THOSE PREDICTED AC COEFF
    613     /* dequantize the rest of AC predicted coeff that haven't been dequant */
    614     if (ACpred_flag)
    615     {
    616 
    617         i = NCOEFF_BLOCK; /* otherwise, FAST IDCT won't work correctly,  10/18/2000 */
    618 
    619         if (!direction) /* check vertical */
    620         {
    621 
    622             dcac_row[0]  = datablock[1];
    623             dcac_row[1]  = datablock[2];
    624             dcac_row[2]  = datablock[3];
    625             dcac_row[3]  = datablock[4];
    626             dcac_row[4]  = datablock[5];
    627             dcac_row[5]  = datablock[6];
    628             dcac_row[6]  = datablock[7];
    629 
    630             for (j = 0, k = 8; k < 64; k += 8, j++)
    631             {
    632                 dcac_col[j] = datablock[k];
    633                 if (dcac_col[j])
    634                 {
    635                     if (datablock[k] > 0)
    636                     {
    637                         if (datablock[k] > 2047) dcac_col[j] = 2047;
    638                         sgn_coeff = 1;
    639                     }
    640                     else
    641                     {
    642                         if (datablock[k] < -2048) dcac_col[j] = -2048;
    643                         sgn_coeff = -1;
    644                     }
    645                     temp = (int32)QP * (2 * datablock[k] + sgn_coeff) - sgn_coeff + (QP & 1) * sgn_coeff;
    646                     if (temp > 2047) temp = 2047;            /*  03/14/01 */
    647                     else if (temp < -2048)  temp = -2048;
    648                     datablock[k] = (int16) temp;
    649 #ifdef FAST_IDCT
    650                     bitmapcol[0] |= mask[k>>3];
    651 #endif
    652 
    653                 }
    654             }
    655 
    656             for (k = 1; k < 8; k++)
    657             {
    658                 if (datablock[k])
    659                 {
    660                     sgn_coeff = (datablock[k] > 0) ? 1 : -1;
    661                     temp = (int32)QP * (2 * datablock[k] + sgn_coeff) - sgn_coeff + (QP & 1) * sgn_coeff;
    662                     if (temp > 2047) temp = 2047;            /*  03/14/01 */
    663                     else if (temp < -2048)  temp = -2048;
    664                     datablock[k] = (int16) temp;
    665 #ifdef FAST_IDCT
    666                     bitmapcol[k] |= 128;
    667 #endif
    668 
    669                 }
    670             }
    671         }
    672         else
    673         {
    674 
    675             dcac_col[0]  = datablock[8];
    676             dcac_col[1]  = datablock[16];
    677             dcac_col[2]  = datablock[24];
    678             dcac_col[3]  = datablock[32];
    679             dcac_col[4]  = datablock[40];
    680             dcac_col[5]  = datablock[48];
    681             dcac_col[6]  = datablock[56];
    682 
    683 
    684             for (j = 0, k = 1; k < 8; k++, j++)
    685             {
    686                 dcac_row[j] = datablock[k];
    687                 if (dcac_row[j])
    688                 {
    689                     if (datablock[k] > 0)
    690                     {
    691                         if (datablock[k] > 2047) dcac_row[j] = 2047;
    692                         sgn_coeff = 1;
    693                     }
    694                     else
    695                     {
    696                         if (datablock[k] < -2048) dcac_row[j] = -2048;
    697                         sgn_coeff = -1;
    698                     }
    699 
    700                     temp = (int32)QP * (2 * datablock[k] + sgn_coeff) - sgn_coeff + (QP & 1) * sgn_coeff;
    701                     if (temp > 2047) temp = 2047;            /*  03/14/01 */
    702                     else if (temp < -2048)  temp = -2048;
    703                     datablock[k] = (int) temp;
    704 #ifdef FAST_IDCT
    705                     bitmapcol[k] |= 128;
    706 #endif
    707 
    708                 }
    709             }
    710             for (k = 8; k < 64; k += 8)
    711             {
    712                 if (datablock[k])
    713                 {
    714                     sgn_coeff = (datablock[k] > 0) ? 1 : -1;
    715                     temp = (int32)QP * (2 * datablock[k] + sgn_coeff) - sgn_coeff + (QP & 1) * sgn_coeff;
    716                     if (temp > 2047) temp = 2047;            /*  03/14/01 */
    717                     else if (temp < -2048)  temp = -2048;
    718                     datablock[k] = (int16) temp;
    719 #ifdef FAST_IDCT
    720                     bitmapcol[0] |= mask[k>>3];
    721 #endif
    722                 }
    723             }
    724 
    725         }
    726     }
    727     else
    728     {
    729         dcac_row[0]  = datablock[1];
    730         dcac_row[1]  = datablock[2];
    731         dcac_row[2]  = datablock[3];
    732         dcac_row[3]  = datablock[4];
    733         dcac_row[4]  = datablock[5];
    734         dcac_row[5]  = datablock[6];
    735         dcac_row[6]  = datablock[7];
    736 
    737         dcac_col[0]  = datablock[8];
    738         dcac_col[1]  = datablock[16];
    739         dcac_col[2]  = datablock[24];
    740         dcac_col[3]  = datablock[32];
    741         dcac_col[4]  = datablock[40];
    742         dcac_col[5]  = datablock[48];
    743         dcac_col[6]  = datablock[56];
    744 
    745         for (k = 1; k < 8; k++)
    746         {
    747             if (datablock[k])
    748             {
    749                 sgn_coeff = (datablock[k] > 0) ? 1 : -1;
    750                 temp = (int32)QP * (2 * datablock[k] + sgn_coeff) - sgn_coeff + (QP & 1) * sgn_coeff;
    751                 if (temp > 2047) temp = 2047;            /*  03/14/01 */
    752                 else if (temp < -2048)  temp = -2048;
    753                 datablock[k] = (int16) temp;
    754 #ifdef FAST_IDCT
    755                 bitmapcol[k] |= 128;
    756 #endif
    757             }
    758         }
    759         for (k = 8; k < 64; k += 8)
    760         {
    761             if (datablock[k])
    762             {
    763                 sgn_coeff = (datablock[k] > 0) ? 1 : -1;
    764                 temp = (int32)QP * (2 * datablock[k] + sgn_coeff) - sgn_coeff + (QP & 1) * sgn_coeff;
    765                 if (temp > 2047) temp = 2047;            /*  03/14/01 */
    766                 else if (temp < -2048)  temp = -2048;
    767                 datablock[k] = (int16) temp;
    768 #ifdef FAST_IDCT
    769                 bitmapcol[0] |= mask[k>>3];
    770 #endif
    771             }
    772         }
    773     }
    774     if (datablock[0])
    775     {
    776 #ifdef FAST_IDCT
    777         bitmapcol[0] |= 128;
    778 #endif
    779 
    780         temp = (int32)datablock[0] * dc_scaler;
    781         if (temp > 2047) temp = 2047;            /*  03/14/01 */
    782         else if (temp < -2048)  temp = -2048;
    783         datablock[0] = (int16)temp;
    784     }
    785 
    786 
    787 #ifdef FAST_IDCT
    788     if (i > 10)
    789     {
    790         for (k = 1; k < 4; k++)  /* if i > 10 then k = 0 does not matter  */
    791         {
    792             if (bitmapcol[k] != 0)
    793             {
    794                 (*bitmaprow) |= mask[k]; /* (1<<(7-i)); */
    795             }
    796         }
    797     }
    798 #endif
    799 
    800     /* Store the qcoeff-values needed later for prediction */
    801     (*DC)[comp] = datablock[0];
    802     return i;
    803 }
    804 
    805 int VlcDequantH263IntraBlock_SH(VideoDecData *video, int comp, uint8 *bitmapcol, uint8 *bitmaprow)
    806 {
    807     BitstreamDecVideo *stream = video->bitstream;
    808     int16 *datablock = video->mblock->block[comp]; /*, 10/20/2000, assume it has been reset of all-zero !!!*/
    809     int32 temp;
    810     int mbnum = video->mbnum;
    811     uint CBP = video->headerInfo.CBP[mbnum];
    812     int16 QP = video->QPMB[mbnum];
    813     typeDCStore *DC = video->predDC + mbnum;
    814     int x_pos = video->mbnum_col;
    815     typeDCACStore *DCAC_row = video->predDCAC_row + x_pos;
    816     typeDCACStore *DCAC_col = video->predDCAC_col;
    817     uint ACpred_flag = (uint) video->acPredFlag[mbnum];
    818 
    819     /*** VLC *****/
    820     int i, k;
    821     Tcoef run_level;
    822     int last, return_status;
    823     VlcDecFuncP vlcDecCoeff;
    824 #ifdef PV_ANNEX_IJKT_SUPPORT
    825     int direction;
    826     const int *inv_zigzag;
    827 #endif
    828     /*** Quantizer ****/
    829 
    830 
    831 
    832     const int B_Xtab[6] = {0, 1, 0, 1, 2, 3};
    833     const int B_Ytab[6] = {0, 0, 1, 1, 2, 3};
    834 
    835     int16 *dcac_row, *dcac_col;
    836 
    837     dcac_row = (*DCAC_row)[B_Xtab[comp]];
    838     dcac_col = (*DCAC_col)[B_Ytab[comp]];
    839     i = 1;
    840 
    841 #ifdef FAST_IDCT
    842     *((uint32*)bitmapcol) = *((uint32*)(bitmapcol + 4)) = 0;
    843     *bitmaprow = 0;
    844 #endif
    845 
    846     /* select which Huffman table to be used */
    847     vlcDecCoeff = video->vlcDecCoeffIntra;
    848 
    849 #ifdef PV_ANNEX_IJKT_SUPPORT
    850     if (comp > 3)        /* ANNEX_T */
    851     {
    852         QP = video->QP_CHR;
    853     }
    854     if (!video->advanced_INTRA)
    855     {
    856 #endif
    857 
    858         if ((CBP & (1 << (5 - comp))) == 0)
    859         {
    860 #ifdef FAST_IDCT
    861             bitmapcol[0] = 128;
    862             bitmapcol[1] = bitmapcol[2] = bitmapcol[3] = bitmapcol[4] = bitmapcol[5] = bitmapcol[6] = bitmapcol[7] = 0;
    863 #endif
    864             datablock[0] <<= 3;  /* no need to clip */
    865             return 1;//ncoeffs;
    866         }
    867         else
    868         {
    869             /* enter the zero run decoding loop */
    870             do
    871             {
    872                 return_status = (*vlcDecCoeff)(stream, &run_level);
    873                 if (return_status != PV_SUCCESS)
    874                 {
    875                     last = 1;/*  11/1/2000 let it slips undetected, just like
    876                              in original version */
    877                     i = VLC_ERROR;
    878                     break;
    879                 }
    880 
    881                 i += run_level.run;
    882                 last = run_level.last;
    883                 if (i >= 64)
    884                 {
    885                     /*  i = NCOEFF_BLOCK; */    /*  11/1/00 */
    886                     i = VLC_NO_LAST_BIT;
    887                     last = 1;
    888                     break;
    889                 }
    890                 k = zigzag_inv[i];
    891 
    892                 if (run_level.sign == 0)
    893                 {
    894                     temp = (int32)QP * (2 * run_level.level + 1) - 1 + (QP & 1);
    895                     if (temp > 2047) temp = 2047;
    896                 }
    897                 else
    898                 {
    899                     temp = -(int32)QP * (2 * run_level.level + 1) + 1 - (QP & 1);
    900                     if (temp < -2048) temp = -2048;
    901                 }
    902 
    903 
    904                 datablock[k] = (int16) temp;
    905 
    906 #ifdef FAST_IDCT
    907                 bitmapcol[k&0x7] |= mask[k>>3];
    908 #endif
    909                 i++;
    910             }
    911             while (!last);
    912 
    913         }
    914         /* no ACDC prediction when ACDC disable  */
    915         if (datablock[0])
    916         {
    917 #ifdef FAST_IDCT
    918             bitmapcol[0] |= 128;
    919 #endif
    920             datablock[0] <<= 3;        /* no need to clip  09/18/2001 */
    921         }
    922 #ifdef PV_ANNEX_IJKT_SUPPORT
    923     }
    924     else  /* advanced_INTRA mode */
    925     {
    926         i = 1;
    927         doDCACPrediction_I(video, comp, datablock);
    928         /* perform only VLC decoding */
    929         if (!ACpred_flag)
    930         {
    931             direction = 0;
    932         }
    933         else
    934         {
    935             direction = video->mblock->direction;
    936         }
    937 
    938         inv_zigzag = zigzag_inv + (ACpred_flag << 6) + (direction << 6);  /*  04/17/01 */
    939 
    940         if (CBP & (1 << (5 - comp)))
    941         {
    942             i = 0;
    943             do
    944             {
    945                 return_status = (*vlcDecCoeff)(stream, &run_level);
    946                 if (return_status != PV_SUCCESS)
    947                 {
    948                     last = 1;/*  11/1/2000 let it slips undetected, just like
    949                                  in original version */
    950                     i = VLC_ERROR;
    951                     ACpred_flag = 0;   /* no of coefficients should not get reset   03/07/2002 */
    952                     break;
    953                 }
    954 
    955                 i += run_level.run;
    956                 last = run_level.last;
    957                 if (i >= 64)
    958                 {
    959                     /*                  i = NCOEFF_BLOCK; */    /*  11/1/00 */
    960                     ACpred_flag = 0;    /* no of coefficients should not get reset   03/07/2002 */
    961                     i = VLC_NO_LAST_BIT;
    962                     last = 1;
    963                     break;
    964                 }
    965 
    966                 k = inv_zigzag[i];
    967 
    968                 if (run_level.sign == 0)
    969                 {
    970                     datablock[k] += (int16)QP * 2 * run_level.level;
    971                     if (datablock[k] > 2047) datablock[k] = 2047;
    972                 }
    973                 else
    974                 {
    975                     datablock[k] -= (int16)QP * 2 * run_level.level;
    976                     if (datablock[k] < -2048) datablock[k] = -2048;
    977                 }
    978 #ifdef FAST_IDCT
    979                 bitmapcol[k&0x7] |= mask[k>>3];
    980 #endif
    981 
    982                 i++;
    983             }
    984             while (!last);
    985 
    986         }
    987         ///// NEED TO DEQUANT THOSE PREDICTED AC COEFF
    988         /* dequantize the rest of AC predicted coeff that haven't been dequant */
    989 
    990         if (ACpred_flag)
    991         {
    992             i = NCOEFF_BLOCK;
    993             for (k = 1; k < 8; k++)
    994             {
    995                 if (datablock[k])
    996                 {
    997                     bitmapcol[k] |= 128;
    998                 }
    999 
   1000                 if (datablock[k<<3])
   1001                 {
   1002                     bitmapcol[0] |= mask[k];
   1003                 }
   1004             }
   1005         }
   1006 
   1007         dcac_row[0]  = datablock[1];
   1008         dcac_row[1]  = datablock[2];
   1009         dcac_row[2]  = datablock[3];
   1010         dcac_row[3]  = datablock[4];
   1011         dcac_row[4]  = datablock[5];
   1012         dcac_row[5]  = datablock[6];
   1013         dcac_row[6]  = datablock[7];
   1014 
   1015         dcac_col[0]  = datablock[8];
   1016         dcac_col[1]  = datablock[16];
   1017         dcac_col[2]  = datablock[24];
   1018         dcac_col[3]  = datablock[32];
   1019         dcac_col[4]  = datablock[40];
   1020         dcac_col[5]  = datablock[48];
   1021         dcac_col[6]  = datablock[56];
   1022 
   1023         if (datablock[0])
   1024         {
   1025 #ifdef FAST_IDCT
   1026             bitmapcol[0] |= 128;
   1027 #endif
   1028 
   1029             datablock[0] |= 1;
   1030             if (datablock[0] < 0)
   1031             {
   1032                 datablock[0] = 0;
   1033             }
   1034         }
   1035     }
   1036 #endif
   1037 
   1038 #ifdef FAST_IDCT
   1039     if (i > 10)
   1040     {
   1041         for (k = 1; k < 4; k++)  /* if i > 10 then k = 0 does not matter  */
   1042         {
   1043             if (bitmapcol[k] != 0)
   1044             {
   1045                 (*bitmaprow) |= mask[k]; /* (1<<(7-i)); */
   1046             }
   1047         }
   1048     }
   1049 #endif
   1050 
   1051     /* Store the qcoeff-values needed later for prediction */
   1052     (*DC)[comp] = datablock[0];
   1053     return i;
   1054 }
   1055 
   1056 /***********************************************************CommentBegin******
   1057 *
   1058 * -- VlcDequantInterH263Block -- Decodes the DCT coefficients of one 8x8 block and perform
   1059             dequantization in H.263 mode for INTER block.
   1060     Date:       08/08/2000
   1061     Modified:             3/21/01
   1062                 clean up, added clipping, 16-bit int case
   1063 ******************************************************************************/
   1064 
   1065 
   1066 int VlcDequantH263InterBlock(VideoDecData *video, int comp,
   1067                              uint8 *bitmapcol, uint8 *bitmaprow)
   1068 {
   1069     BitstreamDecVideo *stream = video->bitstream;
   1070     int16 *datablock = video->mblock->block[comp]; /* 10/20/2000, assume it has been reset of all-zero !!!*/
   1071     int32 temp;
   1072     int mbnum = video->mbnum;
   1073     int QP = video->QPMB[mbnum];
   1074 
   1075     /*** VLC *****/
   1076     int i, k;
   1077     Tcoef run_level;
   1078     int last, return_status;
   1079     VlcDecFuncP vlcDecCoeff;
   1080 
   1081     /*** Quantizer ****/
   1082 
   1083 
   1084     i = 0;
   1085 
   1086 #ifdef FAST_IDCT
   1087     *((uint32*)bitmapcol) = *((uint32*)(bitmapcol + 4)) = 0;
   1088     *bitmaprow = 0;
   1089 #endif
   1090 
   1091     /* select which Huffman table to be used */
   1092     vlcDecCoeff = video->vlcDecCoeffInter;
   1093 
   1094     /* enter the zero run decoding loop */
   1095     do
   1096     {
   1097         return_status = (*vlcDecCoeff)(stream, &run_level);
   1098         if (return_status != PV_SUCCESS)
   1099         {
   1100 
   1101 
   1102             last = 1;/*  11/1/2000 let it slips undetected, just like
   1103                      in original version */
   1104             i = -1;
   1105             break;
   1106         }
   1107 
   1108         i += run_level.run;
   1109         last = run_level.last;
   1110         if (i >= 64)
   1111         {
   1112             i = -1;
   1113             last = 1;
   1114             break;
   1115         }
   1116 
   1117         if (run_level.sign == 0)
   1118         {
   1119             temp = (int32)QP * (2 * run_level.level + 1) - 1 + (QP & 1);
   1120             if (temp > 2047) temp = 2047;
   1121 
   1122         }
   1123         else
   1124         {
   1125             temp = -(int32)QP * (2 * run_level.level + 1) + 1 - (QP & 1);
   1126             if (temp < -2048) temp = -2048;
   1127         }
   1128 
   1129         k = zigzag_inv[i];
   1130         datablock[k] = (int16)temp;
   1131 #ifdef FAST_IDCT
   1132         bitmapcol[k&0x7] |= mask[k>>3];
   1133 #endif
   1134         i++;
   1135     }
   1136     while (!last);
   1137 
   1138 #ifdef FAST_IDCT
   1139     if (i > 10)         /*  07/19/01 */
   1140     {
   1141         for (k = 1; k < 4; k++)       /*  if (i > 10 ) k = 0 does not matter */
   1142         {
   1143             if (bitmapcol[k] != 0)
   1144             {
   1145                 (*bitmaprow) |= mask[k]; /* (1<<(7-i)); */
   1146             }
   1147         }
   1148     }
   1149 #endif
   1150     return i;
   1151 }
   1152 
   1153