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 "mp4enc_lib.h"
     19 #include "fastquant_inline.h"
     20 
     21 #define siz 63
     22 #define LSL 18
     23 
     24 
     25 const static UChar imask[8] = {128, 64, 32, 16, 8, 4, 2, 1};
     26 #define SIGN0(a)        ( ((a)<0) ? -1 : (((a)>0) ? 1  : 0) )
     27 
     28 /* variable bit precision quantization scale */
     29 /* used to avoid using 32-bit multiplication */
     30 const static Short scaleArrayV[32] = {0, 16384, 8192, 5462,  /* 15 */
     31                                       4096, 3277, 2731, 2341,
     32                                       4096, 3641, 3277, 2979,  /* 16 */
     33                                       2731, 2521, 2341, 2185,
     34                                       4096, 3856, 3641, 3450,  /* 17 */
     35                                       3277, 3121, 2979, 2850,
     36                                       5462, 5243, 5042, 4855,  /* 18 */
     37                                       4682, 4520, 4370, 4229
     38                                      };
     39 
     40 /* scale for dc_scaler and qmat, note, no value smaller than 8 */
     41 const static Short scaleArrayV2[47] = {0, 0, 0, 0, 0, 0, 0, 0, /* 15 */
     42                                        4096, 3641, 3277, 2979, 2731, 2521, 2341, 2185,
     43                                        4096, 3856, 3641, 3450, 3277, 3121, 2979, 2850,  /* 16 */
     44                                        2731, 2622, 2521, 2428, 2341, 2260, 2185, 2115,
     45                                        4096, 3972, 3856, 3745, 3641, 3543, 3450, 3361,  /* 17 */
     46                                        3277, 3197, 3121, 3049, 2979, 2913, 2850
     47                                       };
     48 
     49 /* AAN scale and zigzag */
     50 const static Short AANScale[64] =
     51 {
     52     /* 0 */ 0x1000, 0x0B89, 0x0C3E, 0x0D9B, 0x1000, 0x0A2E, 0x0EC8, 0x0E7F,
     53     /* 1 */ 0x0B89, 0x0851, 0x08D4, 0x09CF, 0x0B89, 0x0757, 0x0AA8, 0x0A73,
     54     /* 2 */ 0x0C3E, 0x08D4, 0x095F, 0x0A6A, 0x0C3E, 0x07CB, 0x0B50, 0x0B18,
     55     /* 3 */ 0x0D9B, 0x09CF, 0x0A6A, 0x0B92, 0x0D9B, 0x08A8, 0x0C92, 0x0C54,
     56     /* 4 */ 0x1000, 0x0B89, 0x0C3E, 0x0D9B, 0x1000, 0x0A2E, 0x0EC8, 0x0E7F,
     57     /* 5 */ 0x0A2E, 0x0757, 0x07CB, 0x08A8, 0x0A2E, 0x067A, 0x0968, 0x0939,
     58     /* 6 */ 0x0EC8, 0x0AA8, 0x0B50, 0x0C92, 0x0EC8, 0x0968, 0x0DA8, 0x0D64,
     59     /* 7 */ 0x0E7F, 0x0A73, 0x0B18, 0x0C54, 0x0E7F, 0x0939, 0x0D64, 0x0D23
     60 };
     61 
     62 const static UShort ZZTab[64] =
     63 {
     64     /* 0 */ 0x0, 0x2, 0xA, 0xC, 0x1C, 0x1E, 0x36, 0x38,
     65     /* 1 */ 0x4, 0x8, 0xE, 0x1A, 0x20, 0x34, 0x3A, 0x54,
     66     /* 2 */ 0x6, 0x10, 0x18, 0x22, 0x32, 0x3C, 0x52, 0x56,
     67     /* 3 */ 0x12, 0x16, 0x24, 0x30, 0x3E, 0x50, 0x58, 0x6A,
     68     /* 4 */ 0x14, 0x26, 0x2E, 0x40, 0x4E, 0x5A, 0x68, 0x6C,
     69     /* 5 */ 0x28, 0x2C, 0x42, 0x4C, 0x5C, 0x66, 0x6E, 0x78,
     70     /* 6 */ 0x2A, 0x44, 0x4A, 0x5E, 0x64, 0x70, 0x76, 0x7A,
     71     /* 7 */ 0x46, 0x48, 0x60, 0x62, 0x72, 0x74, 0x7C, 0x7E
     72 };
     73 
     74 
     75 //Tao need to remove, write another version of abs
     76 //#include <math.h>
     77 
     78 /* ======================================================================== */
     79 /*  Function : cal_dc_scalerENC                                             */
     80 /*  Date     : 01/25/2000                                                   */
     81 /*  Purpose  : calculation of DC quantization scale according to the
     82                incoming Q and type;                                         */
     83 /*  In/out   :                                                              */
     84 /*      Int Qp      Quantizer                                               */
     85 /*  Return   :                                                              */
     86 /*          DC Scaler                                                       */
     87 /*  Modified :                                                              */
     88 /* ======================================================================== */
     89 /* ======================================================================== */
     90 Int cal_dc_scalerENC(Int QP, Int type)
     91 {
     92 
     93     Int dc_scaler;
     94     if (type == 1)
     95     {
     96         if (QP > 0 && QP < 5)
     97             dc_scaler = 8;
     98         else if (QP > 4 && QP < 9)
     99             dc_scaler = 2 * QP;
    100         else if (QP > 8 && QP < 25)
    101             dc_scaler = QP + 8;
    102         else
    103             dc_scaler = 2 * QP - 16;
    104     }
    105     else
    106     {
    107         if (QP > 0 && QP < 5)
    108             dc_scaler = 8;
    109         else if (QP > 4 && QP < 25)
    110             dc_scaler = (QP + 13) / 2;
    111         else
    112             dc_scaler = QP - 6;
    113     }
    114     return dc_scaler;
    115 }
    116 
    117 
    118 /***********************************************************************
    119  Function: BlckQuantDequantH263
    120  Date:     June 15, 1999
    121  Purpose:  Combine BlockQuantH263 and BlockDequantH263ENC
    122  Input:   coeff=> DCT coefficient
    123  Output:  qcoeff=> quantized coefficient
    124           rcoeff=> reconstructed coefficient
    125           return CBP for this block
    126           4/2/01,  correct dc_scaler for short_header mode.
    127           5/14/01,
    128           changed the division into LUT multiplication/shift and other
    129           modifications to speed up fastQuant/DeQuant (check for zero 1st, rowq LUT,
    130           fast bitmaprow mask and borrowed Addition method instead of ifs from , ).
    131           6/25/01,
    132           Further optimization (~100K/QCIF), need more testing/comment before integration.
    133 
    134           7/4/01,  break up Inter / Intra function and merge for different cases.
    135           7/22/01,  combine AAN scaling here and reordering.
    136           7/24/01, , reorder already done in FDCT, the input here is in the next block and
    137             it's the
    138             transpose of the raster scan. Output the same order (for proof of concenpt).
    139           8/1/01, , change FDCT to do row/column FDCT without reordering, input is still
    140             in the next block. The reconstructed DCT output is current block in normal
    141             order. The quantized output is in zigzag scan order for INTER, row/column for
    142             INTRA. Use bitmapzz for zigzag RunLevel for INTER.  The quantization is done
    143             in column/row scanning order.
    144           8/2/01, , change IDCT to do column/row, change bitmaprow/col to the opposite.
    145           8/3/01, , add clipping to the reconstructed coefficient [-2047,2047]
    146           9/4/05, , removed scaling for AAN IDCT, use Chen IDCT instead.
    147  ********************************************************************/
    148 
    149 Int BlockQuantDequantH263Inter(Short *rcoeff, Short *qcoeff, struct QPstruct *QuantParam,
    150                                UChar bitmapcol[ ], UChar *bitmaprow, UInt *bitmapzz,
    151                                Int dctMode, Int comp, Int dummy, UChar shortHeader)
    152 {
    153     Int i, zz;
    154     Int tmp, coeff, q_value;
    155     Int QPdiv2 = QuantParam->QPdiv2;
    156     Int QPx2 = QuantParam->QPx2;
    157     Int Addition = QuantParam->Addition;
    158     Int QPx2plus = QuantParam->QPx2plus;
    159     Int round = 1 << 15;
    160     Int q_scale = scaleArrayV[QuantParam->QP];
    161     Int shift = 15 + (QPx2 >> 4);
    162     Int *temp;
    163     UChar *bcolptr = bitmapcol;
    164     Int ac_clip;    /* quantized coeff bound */
    165 
    166     OSCL_UNUSED_ARG(comp);
    167     OSCL_UNUSED_ARG(dummy);
    168 
    169 
    170     if (shortHeader) ac_clip = 126; /* clip between [-127,126] (standard allows 127!) */
    171     else ac_clip = 2047;  /* clip between [-2048,2047] */
    172 
    173     /* reset all bitmap to zero */
    174     temp = (Int*) bitmapcol;
    175     temp[0] = temp[1] = 0;
    176     bitmapzz[0] = bitmapzz[1] = 0;
    177     *bitmaprow = 0;
    178     QPx2plus <<= 4;
    179     QPx2plus -= 8;
    180 
    181     rcoeff += 64; /* actual data is 64 item ahead */
    182     //end  = rcoeff + dctMode - 1;
    183     //rcoeff--;
    184     bcolptr--;
    185     i = 0;
    186 
    187     do
    188     {
    189         bcolptr++;
    190         //rcoeff++;
    191         //i=0;
    192         coeff = rcoeff[i];
    193         if (coeff == 0x7fff) /* all zero column */
    194         {
    195             i++;
    196             continue;
    197         }
    198 
    199         do
    200         {
    201             if (coeff >= -QPx2plus && coeff < QPx2plus)  /* quantize to zero */
    202             {
    203                 i += 8;
    204                 if (i < (dctMode << 3))
    205                 {
    206                     coeff = rcoeff[i];
    207                     if (coeff > -QPx2plus && coeff < QPx2plus)  /* quantize to zero */
    208                     {
    209                         i += 8;
    210                         coeff = rcoeff[i];
    211                         continue;
    212                     }
    213                     else
    214                         goto NONZERO1;
    215                 }
    216             }
    217             else
    218             {
    219 NONZERO1:
    220                 /* scaling */
    221                 q_value = AANScale[i];  /* load scale AAN */
    222                 zz = ZZTab[i];  /* zigzag order */
    223 
    224                 coeff = aan_scale(q_value, coeff, round, QPdiv2);
    225                 q_value = coeff_quant(coeff, q_scale, shift);
    226 
    227                 /* dequantization  */
    228                 if (q_value)
    229                 {
    230 
    231                     //coeff = PV_MIN(ac_clip,PV_MAX(-ac_clip-1, q_value));
    232                     q_value = coeff_clip(q_value, ac_clip);
    233                     qcoeff[zz>>1] = q_value;
    234 
    235                     // dequant and clip
    236                     //coeff = PV_MIN(2047,PV_MAX(-2048, q_value));
    237                     tmp = 2047;
    238                     coeff = coeff_dequant(q_value, QPx2, Addition, tmp);
    239                     rcoeff[i-64] = coeff;
    240 
    241                     (*bcolptr) |= imask[i>>3];
    242                     if ((zz >> 1) > 31) bitmapzz[1] |= (1 << (63 - (zz >> 1)));
    243                     else        bitmapzz[0] |= (1 << (31 - (zz >> 1)));
    244                 }
    245                 i += 8;
    246                 coeff = rcoeff[i];
    247             }
    248         }
    249         while (i < (dctMode << 3));
    250 
    251         i += (1 - (dctMode << 3));
    252     }
    253     while (i < dctMode) ;
    254 
    255     i = dctMode;
    256     tmp = 1 << (8 - i);
    257     while (i--)
    258     {
    259         if (bitmapcol[i])(*bitmaprow) |= tmp;
    260         tmp <<= 1;
    261     }
    262 
    263     if (*bitmaprow)
    264         return 1;
    265     else
    266         return 0;
    267 }
    268 
    269 Int BlockQuantDequantH263Intra(Short *rcoeff, Short *qcoeff, struct QPstruct *QuantParam,
    270                                UChar bitmapcol[ ], UChar *bitmaprow, UInt *bitmapzz,
    271                                Int dctMode, Int comp, Int dc_scaler, UChar shortHeader)
    272 {
    273     Int i;
    274     Int tmp, coeff, q_value;
    275     Int QPx2 = QuantParam->QPx2;
    276     Int Addition = QuantParam->Addition;
    277     Int QPx2plus = QuantParam->QPx2plus;
    278     Int round = 1 << 15;
    279     Int q_scale = scaleArrayV[QuantParam->QP];
    280     Int shift = 15 + (QPx2 >> 4);
    281     UChar *bmcolptr = bitmapcol;
    282     Int ac_clip;    /* quantized coeff bound */
    283 
    284     OSCL_UNUSED_ARG(bitmapzz);
    285     OSCL_UNUSED_ARG(comp);
    286 
    287 
    288     if (shortHeader) ac_clip = 126; /* clip between [-127,126] (standard allows 127!) */
    289     else ac_clip = 2047;  /* clip between [-2048,2047] */
    290 
    291     *((Int*)bitmapcol) = *((Int*)(bitmapcol + 4)) = 0;
    292     *bitmaprow = 0;
    293 
    294     QPx2plus = QPx2 << 4;
    295     QPx2plus -= 8;
    296 
    297     rcoeff += 64; /* actual data is 64 element ahead */
    298     i = 0;
    299 
    300     /* DC value */
    301     coeff = *rcoeff;
    302     /* scaling */
    303     if (coeff == 0x7fff && !shortHeader) /* all zero column */
    304     {
    305         bmcolptr++;
    306         i++;
    307     }
    308     else
    309     {
    310         if (coeff == 0x7fff) /* shortHeader on */
    311         {
    312             coeff = 1; /* can't be zero */
    313             qcoeff[0] = coeff;
    314             coeff = coeff * dc_scaler;
    315             coeff = PV_MAX(-2048, PV_MIN(2047, coeff));
    316             rcoeff[-64] = coeff;
    317             bitmapcol[0] |= 128;
    318             bmcolptr++;
    319             //qcoeff++;
    320             //rcoeff++;
    321             //i=0;
    322             i++;
    323         }
    324         else
    325         {
    326             q_value = round + (coeff << 12);
    327             coeff = q_value >> 16;
    328             if (coeff >= 0) coeff += (dc_scaler >> 1) ;
    329             else            coeff -= (dc_scaler >> 1) ;
    330             q_value = scaleArrayV2[dc_scaler];
    331             coeff = coeff * q_value;
    332             coeff >>= (15 + (dc_scaler >> 4));
    333             coeff += ((UInt)coeff >> 31);
    334 
    335             if (shortHeader)
    336                 coeff = PV_MAX(1, PV_MIN(254, coeff));
    337 
    338             if (coeff)
    339             {
    340                 qcoeff[0] = coeff;
    341                 coeff = coeff * dc_scaler;
    342                 coeff = PV_MAX(-2048, PV_MIN(2047, coeff));
    343                 rcoeff[-64] = coeff;
    344                 bitmapcol[0] |= 128;
    345             }
    346             i += 8;
    347         }
    348     }
    349     /* AC values */
    350     do
    351     {
    352         coeff = rcoeff[i];
    353         if (coeff == 0x7fff) /* all zero row */
    354         {
    355             bmcolptr++;
    356             i++;
    357             continue;
    358         }
    359         do
    360         {
    361             if (coeff >= -QPx2plus && coeff < QPx2plus)  /* quantize to zero */
    362             {
    363                 i += 8;
    364                 if (i < dctMode << 3)
    365                 {
    366                     coeff = rcoeff[i];
    367                     if (coeff > -QPx2plus && coeff < QPx2plus)  /* quantize to zero */
    368                     {
    369                         i += 8;
    370                         coeff = rcoeff[i];
    371                         continue;
    372                     }
    373                     else
    374                         goto NONZERO2;
    375                 }
    376             }
    377             else
    378             {
    379 NONZERO2:   /* scaling */
    380                 q_value = AANScale[i]; /*  09/02/05 */
    381 
    382                 /* scale aan */
    383                 q_value = smlabb(q_value, coeff, round);
    384                 coeff = q_value >> 16;
    385                 /* quant */
    386                 q_value = smulbb(q_scale, coeff); /*mov     q_value, coeff, lsl #14 */
    387                 /*smull tmp, coeff, q_value, q_scale*/
    388                 q_value >>= shift;
    389                 q_value += ((UInt)q_value >> 31); /* add 1 if negative */
    390 
    391                 if (q_value)
    392                 {
    393                     //coeff = PV_MIN(ac_clip,PV_MAX(-ac_clip-1, q_value));
    394                     q_value = coeff_clip(q_value, ac_clip);
    395                     qcoeff[i] = q_value;
    396 
    397                     // dequant and clip
    398                     //coeff = PV_MIN(2047,PV_MAX(-2048, q_value));
    399                     tmp = 2047;
    400                     coeff = coeff_dequant(q_value, QPx2, Addition, tmp);
    401                     rcoeff[i-64] = coeff;
    402 
    403                     (*bmcolptr) |= imask[i>>3];
    404                 }
    405                 i += 8;
    406                 coeff = rcoeff[i];
    407             }
    408         }
    409         while (i < (dctMode << 3)) ;
    410 
    411         //qcoeff++; /* next column */
    412         bmcolptr++;
    413         //rcoeff++;
    414         i += (1 - (dctMode << 3)); //i = 0;
    415     }
    416     while (i < dctMode);//while(rcoeff < end) ;
    417 
    418     i = dctMode;
    419     tmp = 1 << (8 - i);
    420     while (i--)
    421     {
    422         if (bitmapcol[i])(*bitmaprow) |= tmp;
    423         tmp <<= 1;
    424     }
    425 
    426     if (((*bitmaprow)&127) || (bitmapcol[0]&127)) /* exclude DC */
    427         return 1;
    428     else
    429         return 0;
    430 }
    431 
    432 
    433 /***********************************************************************
    434  Function: BlckQuantDequantH263DC
    435  Date:     5/3/2001
    436  Purpose:   H.263 quantization mode, only for DC component
    437  6/25/01,
    438           Further optimization (~100K/QCIF), need more testing/comment before integration.
    439 
    440  ********************************************************************/
    441 Int BlockQuantDequantH263DCInter(Short *rcoeff, Short *qcoeff, struct QPstruct *QuantParam,
    442                                  UChar *bitmaprow, UInt *bitmapzz, Int dummy, UChar shortHeader)
    443 {
    444     Int coeff, scale_q;
    445     Int CBP = 0;
    446     Int QP = QuantParam->QP;
    447     Int QPx2plus = QuantParam->QPx2plus;
    448     Int Addition = QuantParam->Addition;
    449     Int shift = 15 + (QP >> 3);
    450     Int ac_clip;    /* quantized coeff bound */
    451     Int tmp;
    452 
    453     OSCL_UNUSED_ARG(dummy);
    454 
    455     if (shortHeader) ac_clip = 126; /* clip between [-127,126] (standard allows 127!) */
    456     else ac_clip = 2047;  /* clip between [-2048,2047] */
    457 
    458     *bitmaprow = 0;
    459     bitmapzz[0] = bitmapzz[1] = 0;
    460     coeff = rcoeff[0];
    461 
    462     if (coeff >= -QPx2plus && coeff < QPx2plus)
    463     {
    464         rcoeff[0] = 0;
    465         return CBP;//rcoeff[0] = 0; not needed since CBP will be zero
    466     }
    467     else
    468     {
    469         scale_q = scaleArrayV[QP];
    470 
    471         coeff = aan_dc_scale(coeff, QP);
    472 
    473         scale_q = coeff_quant(coeff, scale_q, shift);
    474 
    475         //coeff = PV_MIN(ac_clip,PV_MAX(-ac_clip-1, tmp));
    476         scale_q = coeff_clip(scale_q, ac_clip);
    477 
    478         qcoeff[0] = scale_q;
    479 
    480         QP <<= 1;
    481         //coeff = PV_MIN(2047,PV_MAX(-2048, tmp));
    482         tmp = 2047;
    483         coeff = coeff_dequant(scale_q, QP, Addition, tmp);
    484 
    485         rcoeff[0] = coeff;
    486 
    487         (*bitmaprow) = 128;
    488         bitmapzz[0] = (ULong)1 << 31;
    489         CBP = 1;
    490     }
    491     return CBP;
    492 }
    493 
    494 
    495 Int BlockQuantDequantH263DCIntra(Short *rcoeff, Short *qcoeff, struct QPstruct *QuantParam,
    496                                  UChar *bitmaprow, UInt *bitmapzz, Int dc_scaler, UChar shortHeader)
    497 {
    498     Int tmp, coeff;
    499 
    500     OSCL_UNUSED_ARG(QuantParam);
    501 
    502     *bitmaprow = 0;
    503     coeff = rcoeff[0];
    504 
    505     if (coeff >= 0) coeff += (dc_scaler >> 1) ;
    506     else            coeff -= (dc_scaler >> 1) ;
    507     tmp = scaleArrayV2[dc_scaler];
    508     tmp = coeff * tmp;
    509     tmp >>= (15 + (dc_scaler >> 4));
    510     tmp += ((UInt)tmp >> 31);
    511 
    512     if (shortHeader)
    513         tmp = PV_MAX(1, PV_MIN(254, tmp));
    514 
    515     if (tmp)
    516     {
    517         qcoeff[0] = tmp;
    518         coeff = tmp * dc_scaler;
    519         coeff = PV_MAX(-2048, PV_MIN(2047, coeff));
    520         rcoeff[0] = coeff;
    521         *bitmaprow = 128;
    522         bitmapzz[0] = (ULong)1 << 31;
    523     }
    524 
    525     return 0;
    526 }
    527 
    528 #ifndef NO_MPEG_QUANT
    529 /***********************************************************************
    530  Function: BlckQuantDequantMPEG
    531  Date:     June 15, 1999
    532  Purpose:  Combine BlockQuantMPEG and BlockDequantMPEGENC
    533  Input:   coeff=> DCT coefficient
    534  Output:  qcoeff=> quantized coefficient
    535           rcoeff=> reconstructed coefficient
    536  Modified:  7/5/01, break up function for Intra/Inter
    537           8/3/01,  update with changes from H263 quant mode.
    538           8/3/01,  add clipping to the reconstructed coefficient [-2048,2047]
    539           8/6/01,  optimize using multiplicative lookup-table.
    540                      can be further optimized using ARM assembly, e.g.,
    541                      clipping, 16-bit mult., etc !!!!!!!!!!!!!
    542  ********************************************************************/
    543 
    544 Int BlockQuantDequantMPEGInter(Short *rcoeff, Short *qcoeff, Int QP, Int *qmat,
    545                                UChar bitmapcol[ ], UChar *bitmaprow, UInt *bitmapzz,
    546                                Int dctMode, Int comp, Int dc_scaler)
    547 {
    548     Int i, zz;
    549     Int tmp, coeff, q_value = 0;
    550     Int sum = 0;
    551     Int stepsize, QPx2 = QP << 1;
    552     Int CBP = 0;
    553     Int round = 1 << 15;
    554     Int q_scale = scaleArrayV[QP];
    555     Int shift = 15 + (QP >> 3);
    556     UChar *bcolptr = bitmapcol;
    557 
    558     OSCL_UNUSED_ARG(dc_scaler);
    559     OSCL_UNUSED_ARG(comp);
    560 
    561 
    562     *((Int*)bitmapcol) = *((Int*)(bitmapcol + 4)) = 0;
    563     bitmapzz[0] = bitmapzz[1] = 0;
    564     *bitmaprow = 0;
    565 
    566     rcoeff += 64;
    567     i = 0;
    568     bcolptr--;
    569 
    570     do
    571     {
    572         bcolptr++;
    573         coeff = rcoeff[i];
    574         if (coeff == 0x7fff) /* all zero column */
    575         {
    576             i++;
    577             continue;
    578         }
    579         do
    580         {
    581             q_value = AANScale[i];  /*  09/02/05 scaling for AAN*/
    582             /* aan scaling */
    583             q_value = smlabb(q_value, coeff, round);
    584 
    585             coeff = q_value >> 16;
    586 
    587             stepsize = qmat[i];
    588 //          if(coeff>0)     coeff = (16*coeff + (stepsize/2)) / stepsize;
    589 //          else            coeff = (16*coeff - (stepsize/2)) / stepsize;
    590             coeff <<= 4;
    591             if (coeff >= 0) coeff += (stepsize >> 1) ;
    592             else            coeff -= (stepsize >> 1) ;
    593             q_value = scaleArrayV2[stepsize];
    594             /* mpeg quant table scale */
    595             coeff = smulbb(coeff, q_value);
    596 
    597             coeff >>= (15 + (stepsize >> 4));
    598             coeff += ((UInt)coeff >> 31);
    599 
    600             /* QP scale */
    601             if (coeff >= -QPx2 && coeff < QPx2)  /* quantized to zero*/
    602             {
    603                 i += 8;
    604             }
    605             else
    606             {
    607 //              q_value = coeff/(QPx2);
    608                 q_value = coeff_quant(coeff, q_scale, shift);
    609 
    610                 if (q_value)                /* dequant */
    611                 {
    612 
    613                     zz = ZZTab[i];  /* zigzag order */
    614 
    615                     tmp = 2047;
    616 
    617                     q_value = clip_2047(q_value, tmp);
    618 
    619                     qcoeff[zz>>1] = q_value;
    620 
    621                     //q_value=(((coeff*2)+SIGN0(coeff))*stepsize*QP)/16;
    622                     /* no need for SIGN0, no zero coming in this {} */
    623                     q_value = coeff_dequant_mpeg(q_value, stepsize, QP, tmp);
    624 
    625                     rcoeff[i-64] = q_value;
    626 
    627                     sum += q_value;
    628                     (*bcolptr) |= imask[i>>3];
    629                     if ((zz >> 1) > 31) bitmapzz[1] |= (1 << (63 - (zz >> 1)));
    630                     else        bitmapzz[0] |= (1 << (31 - (zz >> 1)));
    631                 }
    632                 i += 8;
    633             }
    634             coeff = rcoeff[i];
    635         }
    636         while (i < (dctMode << 3)) ;
    637 
    638         i += (1 - (dctMode << 3));
    639     }
    640     while (i < dctMode) ;
    641 
    642     i = dctMode;
    643     tmp = 1 << (8 - i);
    644     while (i--)
    645     {
    646         if (bitmapcol[i])(*bitmaprow) |= tmp;
    647         tmp <<= 1;
    648     }
    649 
    650     if (*bitmaprow)
    651         CBP = 1;   /* check CBP before mismatch control,  7/5/01 */
    652 
    653     /* Mismatch control,  5/3/01 */
    654     if (CBP)
    655     {
    656         if ((sum&0x1) == 0)
    657         {
    658             rcoeff--;  /* rcoeff[63] */
    659             coeff = *rcoeff;
    660             coeff ^= 0x1;
    661             *rcoeff = coeff;
    662             if (coeff)
    663             {
    664                 bitmapcol[7] |= 1;
    665                 (*bitmaprow) |= 1;
    666             }
    667         }
    668     }
    669 
    670     return CBP;
    671 }
    672 
    673 Int BlockQuantDequantMPEGIntra(Short *rcoeff, Short *qcoeff, Int QP, Int *qmat,
    674                                UChar bitmapcol[ ], UChar *bitmaprow, UInt *bitmapzz,
    675                                Int dctMode, Int comp, Int dc_scaler)
    676 {
    677     Int i;
    678     Int tmp, coeff, q_value = 0;
    679     Int sum = 0;
    680     Int stepsize;
    681     Int CBP = 0;
    682     Int round = 1 << 15;
    683     Int q_scale = scaleArrayV[QP];
    684     Int shift = 15 + (QP >> 3);
    685     Int round2 = (3 * QP + 2) >> 2;
    686     Int QPx2plus = (QP << 1) - round2;
    687     UChar *bmcolptr = bitmapcol;
    688 
    689     OSCL_UNUSED_ARG(bitmapzz);
    690     OSCL_UNUSED_ARG(comp);
    691 
    692     *((Int*)bitmapcol) = *((Int*)(bitmapcol + 4)) = 0;
    693     *bitmaprow = 0;
    694 
    695     rcoeff += 64;
    696     i = 0;
    697 
    698     /* DC value */
    699     coeff = *rcoeff;
    700 
    701     if (coeff == 0x7fff) /* all zero column */
    702     {
    703         bmcolptr++;
    704         i++;
    705     }
    706     else
    707     {
    708         q_value = round + (coeff << 12);
    709         coeff = q_value >> 16;
    710         /*if (coeff >= 0)   coeff = (coeff + (dc_scaler/2)) / dc_scaler;
    711         else            coeff = (coeff - (dc_scaler/2)) / dc_scaler;*/
    712         if (coeff >= 0) coeff += (dc_scaler >> 1) ;
    713         else            coeff -= (dc_scaler >> 1) ;
    714         q_value = scaleArrayV2[dc_scaler];
    715 
    716         /* mpeg quant table scale */
    717         coeff = smulbb(coeff, q_value);
    718 
    719         coeff >>= (15 + (dc_scaler >> 4));
    720         coeff += ((UInt)coeff >> 31);
    721 
    722         if (coeff)
    723         {
    724             coeff = PV_MAX(1, PV_MIN(254, coeff));
    725             qcoeff[0] = coeff;
    726 
    727             coeff = smulbb(coeff, dc_scaler);
    728 
    729             q_value = clip_2047(coeff, 2047);
    730 
    731             sum = q_value;
    732 
    733             rcoeff[-64] = q_value;
    734 
    735             bitmapcol[0] |= 128;
    736         }
    737         i += 8;
    738     }
    739     /* AC values */
    740     do
    741     {
    742         coeff = rcoeff[i];
    743         if (coeff == 0x7fff) /* all zero row */
    744         {
    745             bmcolptr++;
    746             i++;
    747             continue;
    748         }
    749         do
    750         {
    751             /* scaling */
    752             q_value = AANScale[i]; /*  09/02/05 */
    753 
    754             /* q_value = coeff*q_value + round */
    755             q_value = smlabb(coeff, q_value, round);
    756             coeff = q_value >> 16;
    757 
    758             stepsize = qmat[i];
    759             /*if(coeff>0)       coeff = (16*coeff + (stepsize/2)) / stepsize;
    760             else            coeff = (16*coeff - (stepsize/2)) / stepsize;*/
    761             coeff <<= 4;
    762             if (coeff >= 0) coeff += (stepsize >> 1) ;
    763             else            coeff -= (stepsize >> 1) ;
    764             q_value = scaleArrayV2[stepsize];
    765 
    766             /* scale mpeg quant */
    767             coeff = smulbb(coeff, q_value);
    768 
    769             coeff >>= (15 + (stepsize >> 4));
    770             coeff += ((UInt)coeff >> 31);
    771 
    772             if (coeff >= -QPx2plus && coeff < QPx2plus)
    773             {
    774                 i += 8;
    775             }
    776             else
    777             {
    778                 //q_value = ( coeff + SIGN0(coeff)*((3*QP+2)/4))/(2*QP);
    779                 if (coeff > 0) coeff += round2;
    780                 else if (coeff < 0) coeff -= round2;
    781 
    782                 q_value = smulbb(coeff, q_scale);
    783                 q_value >>= shift;
    784                 q_value += ((UInt)q_value >> 31);
    785 
    786                 if (q_value)
    787                 {
    788                     tmp = 2047;
    789                     q_value = clip_2047(q_value, tmp);
    790 
    791                     qcoeff[i] = q_value;
    792 
    793                     stepsize = smulbb(stepsize, QP);
    794                     q_value =  smulbb(q_value, stepsize);
    795 
    796                     q_value = coeff_dequant_mpeg_intra(q_value, tmp);
    797                     //q_value = (coeff*stepsize*QP*2)/16;
    798 
    799                     rcoeff[i-64] = q_value;
    800 
    801                     sum += q_value;
    802                     (*bmcolptr) |= imask[i>>3];
    803                 }
    804                 i += 8;
    805             }
    806             coeff = rcoeff[i];
    807         }
    808         while (i < (dctMode << 3)) ;
    809 
    810         bmcolptr++;
    811         i += (1 - (dctMode << 3));
    812     }
    813     while (i < dctMode) ;
    814 
    815     i = dctMode;
    816     tmp = 1 << (8 - i);
    817     while (i--)
    818     {
    819         if (bitmapcol[i])(*bitmaprow) |= tmp;
    820         tmp <<= 1;
    821     }
    822 
    823     if (((*bitmaprow) &127) || (bitmapcol[0]&127))
    824         CBP = 1;  /* check CBP before mismatch control,  7/5/01 */
    825 
    826     /* Mismatch control,  5/3/01 */
    827     if (CBP || bitmapcol[0])
    828     {
    829         if ((sum&0x1) == 0)
    830         {
    831             rcoeff--;  /* rcoeff[63] */
    832             coeff = *rcoeff;
    833             coeff ^= 0x1;
    834             *rcoeff = coeff;
    835             if (coeff)
    836             {
    837                 bitmapcol[7] |= 1;
    838                 (*bitmaprow) |= 1;
    839             }
    840         }
    841     }
    842 
    843     return CBP;
    844 }
    845 
    846 
    847 /***********************************************************************
    848  Function: BlckQuantDequantMPEGDC
    849  Date:     5/3/2001
    850  Purpose:  MPEG Quant/Dequant for DC only block.
    851  ********************************************************************/
    852 Int BlockQuantDequantMPEGDCInter(Short *rcoeff, Short *qcoeff, Int QP, Int *qmat,
    853                                  UChar bitmapcol[ ], UChar *bitmaprow, UInt *bitmapzz, Int dummy)
    854 {
    855     Int q_value, coeff, stepsize;
    856     Int CBP = 0;
    857     Int q_scale = scaleArrayV[QP];
    858     Int shift = 15 + (QP >> 3);
    859     Int QPx2 = QP << 1;
    860 
    861     OSCL_UNUSED_ARG(dummy);
    862 
    863     *((Int*)bitmapcol) = *((Int*)(bitmapcol + 4)) = 0;
    864     *bitmaprow = 0;
    865     bitmapzz[0] = bitmapzz[1] = 0;
    866     coeff = rcoeff[0];
    867     stepsize = qmat[0];
    868 
    869     /*if(coeff>0)       coeff = (16*coeff + (stepsize/2)) / stepsize;
    870     else            coeff = (16*coeff - (stepsize/2)) / stepsize;*/
    871     coeff <<= 4;
    872     if (coeff >= 0) coeff += (stepsize >> 1) ;
    873     else            coeff -= (stepsize >> 1) ;
    874     q_value = scaleArrayV2[stepsize];
    875 
    876     coeff = smulbb(coeff, q_value);
    877 
    878     coeff >>= (15 + (stepsize >> 4));
    879     coeff += ((UInt)coeff >> 31);
    880 
    881     if (coeff >= -QPx2 && coeff < QPx2)
    882     {
    883         rcoeff[0] = 0;
    884         return CBP;
    885     }
    886     else
    887     {
    888 //      q_value = coeff/(QPx2);
    889         q_value = coeff_quant(coeff, q_scale, shift);
    890 
    891         if (q_value)
    892         {
    893 
    894             //PV_MIN(2047,PV_MAX(-2048, q_value));
    895             q_value = clip_2047(q_value, 2047);
    896             qcoeff[0] = q_value;
    897             q_value = coeff_dequant_mpeg(q_value, stepsize, QP, 2047);
    898             //q_value=(((coeff*2)+SIGN0(coeff))*stepsize*QP)/16;
    899             rcoeff[0] = q_value;
    900 
    901             bitmapcol[0] = 128;
    902             (*bitmaprow) = 128;
    903             bitmapzz[0] = (UInt)1 << 31;
    904             CBP = 1;
    905 
    906             /* Mismatch control,  5/3/01 */
    907             if ((q_value&0x1) == 0)
    908             {
    909                 rcoeff[63] = 1; /* after scaling it remains the same */
    910                 bitmapcol[7] |= 1;
    911                 (*bitmaprow) |= 1;
    912             }
    913         }
    914     }
    915     return CBP;
    916 }
    917 
    918 
    919 Int BlockQuantDequantMPEGDCIntra(Short *rcoeff, Short *qcoeff, Int QP, Int *qmat,
    920                                  UChar bitmapcol[ ], UChar *bitmaprow, UInt *bitmapzz,
    921                                  Int dc_scaler)
    922 {
    923     Int tmp, coeff, q_value;
    924 
    925     OSCL_UNUSED_ARG(QP);
    926     OSCL_UNUSED_ARG(qmat);
    927 
    928 
    929     *((Int*)bitmapcol) = *((Int*)(bitmapcol + 4)) = 0;
    930     *bitmaprow = 0;
    931     coeff = rcoeff[0];
    932 
    933     /*if (coeff >= 0)   tmp = (coeff + dc_scaler/2) / dc_scaler;
    934     else            tmp = (coeff - dc_scaler/2) / dc_scaler;*/
    935     if (coeff >= 0) coeff += (dc_scaler >> 1) ;
    936     else            coeff -= (dc_scaler >> 1) ;
    937     tmp = scaleArrayV2[dc_scaler];
    938 
    939     tmp = smulbb(tmp, coeff);
    940     tmp >>= (15 + (dc_scaler >> 4));
    941     tmp += ((UInt)tmp >> 31);
    942 
    943     if (tmp)
    944     {
    945         coeff = PV_MAX(1, PV_MIN(254, tmp));
    946         qcoeff[0] = coeff;
    947 
    948         q_value = smulbb(coeff, dc_scaler);
    949         q_value = clip_2047(q_value, 2047);
    950         rcoeff[0] = q_value;
    951         bitmapcol[0] = 128;
    952         *bitmaprow = 128;
    953         bitmapzz[0] = (UInt)1 << 31;
    954 
    955         /* Mismatch control,  5/3/01 */
    956         if ((q_value&0x1) == 0)
    957         {
    958             rcoeff[63] = 1; /* after scaling it remains the same */
    959             bitmapcol[7] |= 1;
    960             (*bitmaprow) |= 1;
    961         }
    962     }
    963 
    964     return 0;
    965 }
    966 #endif
    967 
    968