159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* ------------------------------------------------------------------
259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * Copyright (C) 1998-2009 PacketVideo
359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong *
459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * Licensed under the Apache License, Version 2.0 (the "License");
559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * you may not use this file except in compliance with the License.
659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * You may obtain a copy of the License at
759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong *
859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong *      http://www.apache.org/licenses/LICENSE-2.0
959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong *
1059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * Unless required by applicable law or agreed to in writing, software
1159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * distributed under the License is distributed on an "AS IS" BASIS,
1259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
1359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * express or implied.
1459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * See the License for the specific language governing permissions
1559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * and limitations under the License.
1659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong * -------------------------------------------------------------------
1759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong */
1859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#include "mp4enc_lib.h"
1959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#include "fastquant_inline.h"
2059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
2159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#define siz 63
2259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#define LSL 18
2359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
2459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
2559f566c4ec3dfc097ad8163523e522280b27e5c3James Dongconst static UChar imask[8] = {128, 64, 32, 16, 8, 4, 2, 1};
2659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#define SIGN0(a)        ( ((a)<0) ? -1 : (((a)>0) ? 1  : 0) )
2759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
2859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* variable bit precision quantization scale */
2959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* used to avoid using 32-bit multiplication */
3059f566c4ec3dfc097ad8163523e522280b27e5c3James Dongconst static Short scaleArrayV[32] = {0, 16384, 8192, 5462,  /* 15 */
3159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                      4096, 3277, 2731, 2341,
3259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                      4096, 3641, 3277, 2979,  /* 16 */
3359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                      2731, 2521, 2341, 2185,
3459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                      4096, 3856, 3641, 3450,  /* 17 */
3559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                      3277, 3121, 2979, 2850,
3659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                      5462, 5243, 5042, 4855,  /* 18 */
3759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                      4682, 4520, 4370, 4229
3859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                     };
3959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
4059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* scale for dc_scaler and qmat, note, no value smaller than 8 */
4159f566c4ec3dfc097ad8163523e522280b27e5c3James Dongconst static Short scaleArrayV2[47] = {0, 0, 0, 0, 0, 0, 0, 0, /* 15 */
4259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                       4096, 3641, 3277, 2979, 2731, 2521, 2341, 2185,
4359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                       4096, 3856, 3641, 3450, 3277, 3121, 2979, 2850,  /* 16 */
4459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                       2731, 2622, 2521, 2428, 2341, 2260, 2185, 2115,
4559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                       4096, 3972, 3856, 3745, 3641, 3543, 3450, 3361,  /* 17 */
4659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                       3277, 3197, 3121, 3049, 2979, 2913, 2850
4759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                      };
4859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
4959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* AAN scale and zigzag */
5059f566c4ec3dfc097ad8163523e522280b27e5c3James Dongconst static Short AANScale[64] =
5159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
5259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* 0 */ 0x1000, 0x0B89, 0x0C3E, 0x0D9B, 0x1000, 0x0A2E, 0x0EC8, 0x0E7F,
5359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* 1 */ 0x0B89, 0x0851, 0x08D4, 0x09CF, 0x0B89, 0x0757, 0x0AA8, 0x0A73,
5459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* 2 */ 0x0C3E, 0x08D4, 0x095F, 0x0A6A, 0x0C3E, 0x07CB, 0x0B50, 0x0B18,
5559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* 3 */ 0x0D9B, 0x09CF, 0x0A6A, 0x0B92, 0x0D9B, 0x08A8, 0x0C92, 0x0C54,
5659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* 4 */ 0x1000, 0x0B89, 0x0C3E, 0x0D9B, 0x1000, 0x0A2E, 0x0EC8, 0x0E7F,
5759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* 5 */ 0x0A2E, 0x0757, 0x07CB, 0x08A8, 0x0A2E, 0x067A, 0x0968, 0x0939,
5859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* 6 */ 0x0EC8, 0x0AA8, 0x0B50, 0x0C92, 0x0EC8, 0x0968, 0x0DA8, 0x0D64,
5959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* 7 */ 0x0E7F, 0x0A73, 0x0B18, 0x0C54, 0x0E7F, 0x0939, 0x0D64, 0x0D23
6059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong};
6159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
6259f566c4ec3dfc097ad8163523e522280b27e5c3James Dongconst static UShort ZZTab[64] =
6359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
6459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* 0 */ 0x0, 0x2, 0xA, 0xC, 0x1C, 0x1E, 0x36, 0x38,
6559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* 1 */ 0x4, 0x8, 0xE, 0x1A, 0x20, 0x34, 0x3A, 0x54,
6659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* 2 */ 0x6, 0x10, 0x18, 0x22, 0x32, 0x3C, 0x52, 0x56,
6759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* 3 */ 0x12, 0x16, 0x24, 0x30, 0x3E, 0x50, 0x58, 0x6A,
6859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* 4 */ 0x14, 0x26, 0x2E, 0x40, 0x4E, 0x5A, 0x68, 0x6C,
6959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* 5 */ 0x28, 0x2C, 0x42, 0x4C, 0x5C, 0x66, 0x6E, 0x78,
7059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* 6 */ 0x2A, 0x44, 0x4A, 0x5E, 0x64, 0x70, 0x76, 0x7A,
7159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* 7 */ 0x46, 0x48, 0x60, 0x62, 0x72, 0x74, 0x7C, 0x7E
7259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong};
7359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
7459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
7559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//Tao need to remove, write another version of abs
7659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//#include <math.h>
7759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
7859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* ======================================================================== */
7959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Function : cal_dc_scalerENC                                             */
8059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Date     : 01/25/2000                                                   */
8159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Purpose  : calculation of DC quantization scale according to the
8259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong               incoming Q and type;                                         */
8359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  In/out   :                                                              */
8459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*      Int Qp      Quantizer                                               */
8559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Return   :                                                              */
8659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*          DC Scaler                                                       */
8759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Modified :                                                              */
8859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* ======================================================================== */
8959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* ======================================================================== */
9059f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt cal_dc_scalerENC(Int QP, Int type)
9159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
9259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
9359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int dc_scaler;
9459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (type == 1)
9559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
9659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (QP > 0 && QP < 5)
9759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            dc_scaler = 8;
9859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else if (QP > 4 && QP < 9)
9959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            dc_scaler = 2 * QP;
10059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else if (QP > 8 && QP < 25)
10159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            dc_scaler = QP + 8;
10259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
10359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            dc_scaler = 2 * QP - 16;
10459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
10559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
10659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
10759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (QP > 0 && QP < 5)
10859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            dc_scaler = 8;
10959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else if (QP > 4 && QP < 25)
11059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            dc_scaler = (QP + 13) / 2;
11159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
11259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            dc_scaler = QP - 6;
11359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
11459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return dc_scaler;
11559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
11659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
11759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
11859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/***********************************************************************
11959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong Function: BlckQuantDequantH263
12059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong Date:     June 15, 1999
12159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong Purpose:  Combine BlockQuantH263 and BlockDequantH263ENC
12259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong Input:   coeff=> DCT coefficient
12359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong Output:  qcoeff=> quantized coefficient
12459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          rcoeff=> reconstructed coefficient
12559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          return CBP for this block
12659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          4/2/01,  correct dc_scaler for short_header mode.
12759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          5/14/01,
12859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          changed the division into LUT multiplication/shift and other
12959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          modifications to speed up fastQuant/DeQuant (check for zero 1st, rowq LUT,
13059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          fast bitmaprow mask and borrowed Addition method instead of ifs from , ).
13159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          6/25/01,
13259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          Further optimization (~100K/QCIF), need more testing/comment before integration.
13359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
13459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          7/4/01,  break up Inter / Intra function and merge for different cases.
13559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          7/22/01,  combine AAN scaling here and reordering.
13659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          7/24/01, , reorder already done in FDCT, the input here is in the next block and
13759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            it's the
13859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            transpose of the raster scan. Output the same order (for proof of concenpt).
13959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          8/1/01, , change FDCT to do row/column FDCT without reordering, input is still
14059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            in the next block. The reconstructed DCT output is current block in normal
14159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            order. The quantized output is in zigzag scan order for INTER, row/column for
14259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            INTRA. Use bitmapzz for zigzag RunLevel for INTER.  The quantization is done
14359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            in column/row scanning order.
14459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          8/2/01, , change IDCT to do column/row, change bitmaprow/col to the opposite.
14559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          8/3/01, , add clipping to the reconstructed coefficient [-2047,2047]
14659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          9/4/05, , removed scaling for AAN IDCT, use Chen IDCT instead.
14759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong ********************************************************************/
14859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
14959f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt BlockQuantDequantH263Inter(Short *rcoeff, Short *qcoeff, struct QPstruct *QuantParam,
15059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                               UChar bitmapcol[ ], UChar *bitmaprow, UInt *bitmapzz,
15159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                               Int dctMode, Int comp, Int dummy, UChar shortHeader)
15259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
15359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int i, zz;
15459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int tmp, coeff, q_value;
15559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int QPdiv2 = QuantParam->QPdiv2;
15659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int QPx2 = QuantParam->QPx2;
15759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int Addition = QuantParam->Addition;
15859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int QPx2plus = QuantParam->QPx2plus;
15959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int round = 1 << 15;
16059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int q_scale = scaleArrayV[QuantParam->QP];
16159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int shift = 15 + (QPx2 >> 4);
16259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int *temp;
16359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar *bcolptr = bitmapcol;
16459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int ac_clip;    /* quantized coeff bound */
16559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
16659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    OSCL_UNUSED_ARG(comp);
16759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    OSCL_UNUSED_ARG(dummy);
16859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
16959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
17059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (shortHeader) ac_clip = 126; /* clip between [-127,126] (standard allows 127!) */
17159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else ac_clip = 2047;  /* clip between [-2048,2047] */
17259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
17359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* reset all bitmap to zero */
17459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    temp = (Int*) bitmapcol;
17559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    temp[0] = temp[1] = 0;
17659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    bitmapzz[0] = bitmapzz[1] = 0;
17759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    *bitmaprow = 0;
17859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    QPx2plus <<= 4;
17959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    QPx2plus -= 8;
18059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
18159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    rcoeff += 64; /* actual data is 64 item ahead */
18259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    //end  = rcoeff + dctMode - 1;
18359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    //rcoeff--;
18459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    bcolptr--;
18559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    i = 0;
18659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
18759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    do
18859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
18959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        bcolptr++;
19059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //rcoeff++;
19159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //i=0;
19259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        coeff = rcoeff[i];
19359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (coeff == 0x7fff) /* all zero column */
19459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
19559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            i++;
19659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            continue;
19759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
19859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
19959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        do
20059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
20159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (coeff >= -QPx2plus && coeff < QPx2plus)  /* quantize to zero */
20259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
20359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                i += 8;
20459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (i < (dctMode << 3))
20559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
20659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    coeff = rcoeff[i];
20759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if (coeff > -QPx2plus && coeff < QPx2plus)  /* quantize to zero */
20859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
20959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        i += 8;
21059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        coeff = rcoeff[i];
21159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        continue;
21259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
21359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    else
21459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        goto NONZERO1;
21559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
21659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
21759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else
21859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
21959f566c4ec3dfc097ad8163523e522280b27e5c3James DongNONZERO1:
22059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                /* scaling */
22159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                q_value = AANScale[i];  /* load scale AAN */
22259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                zz = ZZTab[i];  /* zigzag order */
22359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
22459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                coeff = aan_scale(q_value, coeff, round, QPdiv2);
22559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                q_value = coeff_quant(coeff, q_scale, shift);
22659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
22759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                /* dequantization  */
22859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (q_value)
22959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
23059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
23159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    //coeff = PV_MIN(ac_clip,PV_MAX(-ac_clip-1, q_value));
23259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    q_value = coeff_clip(q_value, ac_clip);
23359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    qcoeff[zz>>1] = q_value;
23459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
23559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    // dequant and clip
23659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    //coeff = PV_MIN(2047,PV_MAX(-2048, q_value));
23759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    tmp = 2047;
23859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    coeff = coeff_dequant(q_value, QPx2, Addition, tmp);
23959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    rcoeff[i-64] = coeff;
24059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
24159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    (*bcolptr) |= imask[i>>3];
24259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if ((zz >> 1) > 31) bitmapzz[1] |= (1 << (63 - (zz >> 1)));
24359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    else        bitmapzz[0] |= (1 << (31 - (zz >> 1)));
24459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
24559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                i += 8;
24659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                coeff = rcoeff[i];
24759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
24859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
24959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        while (i < (dctMode << 3));
25059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
25159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        i += (1 - (dctMode << 3));
25259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
25359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    while (i < dctMode) ;
25459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
25559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    i = dctMode;
25659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    tmp = 1 << (8 - i);
25759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    while (i--)
25859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
25959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (bitmapcol[i])(*bitmaprow) |= tmp;
26059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        tmp <<= 1;
26159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
26259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
26359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (*bitmaprow)
26459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return 1;
26559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
26659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return 0;
26759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
26859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
26959f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt BlockQuantDequantH263Intra(Short *rcoeff, Short *qcoeff, struct QPstruct *QuantParam,
27059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                               UChar bitmapcol[ ], UChar *bitmaprow, UInt *bitmapzz,
27159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                               Int dctMode, Int comp, Int dc_scaler, UChar shortHeader)
27259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
27359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int i;
27459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int tmp, coeff, q_value;
27559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int QPx2 = QuantParam->QPx2;
27659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int Addition = QuantParam->Addition;
27759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int QPx2plus = QuantParam->QPx2plus;
27859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int round = 1 << 15;
27959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int q_scale = scaleArrayV[QuantParam->QP];
28059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int shift = 15 + (QPx2 >> 4);
28159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar *bmcolptr = bitmapcol;
28259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int ac_clip;    /* quantized coeff bound */
28359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
28459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    OSCL_UNUSED_ARG(bitmapzz);
28559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    OSCL_UNUSED_ARG(comp);
28659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
28759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
28859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (shortHeader) ac_clip = 126; /* clip between [-127,126] (standard allows 127!) */
28959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else ac_clip = 2047;  /* clip between [-2048,2047] */
29059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
29159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    *((Int*)bitmapcol) = *((Int*)(bitmapcol + 4)) = 0;
29259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    *bitmaprow = 0;
29359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
29459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    QPx2plus = QPx2 << 4;
29559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    QPx2plus -= 8;
29659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
29759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    rcoeff += 64; /* actual data is 64 element ahead */
29859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    i = 0;
29959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
30059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* DC value */
30159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    coeff = *rcoeff;
30259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* scaling */
30359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (coeff == 0x7fff && !shortHeader) /* all zero column */
30459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
30559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        bmcolptr++;
30659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        i++;
30759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
30859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
30959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
31059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (coeff == 0x7fff) /* shortHeader on */
31159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
31259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff = 1; /* can't be zero */
31359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            qcoeff[0] = coeff;
31459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff = coeff * dc_scaler;
31559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff = PV_MAX(-2048, PV_MIN(2047, coeff));
31659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rcoeff[-64] = coeff;
31759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            bitmapcol[0] |= 128;
31859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            bmcolptr++;
31959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //qcoeff++;
32059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //rcoeff++;
32159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //i=0;
32259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            i++;
32359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
32459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
32559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
32659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            q_value = round + (coeff << 12);
32759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff = q_value >> 16;
32859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (coeff >= 0) coeff += (dc_scaler >> 1) ;
32959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else            coeff -= (dc_scaler >> 1) ;
33059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            q_value = scaleArrayV2[dc_scaler];
33159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff = coeff * q_value;
33259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff >>= (15 + (dc_scaler >> 4));
33359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff += ((UInt)coeff >> 31);
33459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
33559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (shortHeader)
33659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                coeff = PV_MAX(1, PV_MIN(254, coeff));
33759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
33859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (coeff)
33959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
34059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                qcoeff[0] = coeff;
34159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                coeff = coeff * dc_scaler;
34259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                coeff = PV_MAX(-2048, PV_MIN(2047, coeff));
34359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                rcoeff[-64] = coeff;
34459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                bitmapcol[0] |= 128;
34559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
34659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            i += 8;
34759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
34859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
34959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* AC values */
35059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    do
35159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
35259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        coeff = rcoeff[i];
35359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (coeff == 0x7fff) /* all zero row */
35459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
35559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            bmcolptr++;
35659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            i++;
35759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            continue;
35859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
35959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        do
36059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
36159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (coeff >= -QPx2plus && coeff < QPx2plus)  /* quantize to zero */
36259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
36359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                i += 8;
36459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (i < dctMode << 3)
36559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
36659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    coeff = rcoeff[i];
36759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if (coeff > -QPx2plus && coeff < QPx2plus)  /* quantize to zero */
36859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
36959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        i += 8;
37059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        coeff = rcoeff[i];
37159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        continue;
37259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
37359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    else
37459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        goto NONZERO2;
37559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
37659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
37759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else
37859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
37959f566c4ec3dfc097ad8163523e522280b27e5c3James DongNONZERO2:   /* scaling */
38059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                q_value = AANScale[i]; /*  09/02/05 */
38159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
38259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                /* scale aan */
38359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                q_value = smlabb(q_value, coeff, round);
38459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                coeff = q_value >> 16;
38559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                /* quant */
38659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                q_value = smulbb(q_scale, coeff); /*mov     q_value, coeff, lsl #14 */
38759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                /*smull tmp, coeff, q_value, q_scale*/
38859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                q_value >>= shift;
38959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                q_value += ((UInt)q_value >> 31); /* add 1 if negative */
39059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
39159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (q_value)
39259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
39359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    //coeff = PV_MIN(ac_clip,PV_MAX(-ac_clip-1, q_value));
39459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    q_value = coeff_clip(q_value, ac_clip);
39559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    qcoeff[i] = q_value;
39659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
39759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    // dequant and clip
39859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    //coeff = PV_MIN(2047,PV_MAX(-2048, q_value));
39959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    tmp = 2047;
40059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    coeff = coeff_dequant(q_value, QPx2, Addition, tmp);
40159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    rcoeff[i-64] = coeff;
40259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
40359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    (*bmcolptr) |= imask[i>>3];
40459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
40559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                i += 8;
40659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                coeff = rcoeff[i];
40759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
40859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
40959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        while (i < (dctMode << 3)) ;
41059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
41159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //qcoeff++; /* next column */
41259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        bmcolptr++;
41359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //rcoeff++;
41459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        i += (1 - (dctMode << 3)); //i = 0;
41559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
41659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    while (i < dctMode);//while(rcoeff < end) ;
41759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
41859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    i = dctMode;
41959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    tmp = 1 << (8 - i);
42059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    while (i--)
42159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
42259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (bitmapcol[i])(*bitmaprow) |= tmp;
42359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        tmp <<= 1;
42459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
42559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
42659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (((*bitmaprow)&127) || (bitmapcol[0]&127)) /* exclude DC */
42759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return 1;
42859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
42959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return 0;
43059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
43159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
43259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
43359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/***********************************************************************
43459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong Function: BlckQuantDequantH263DC
43559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong Date:     5/3/2001
43659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong Purpose:   H.263 quantization mode, only for DC component
43759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong 6/25/01,
43859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          Further optimization (~100K/QCIF), need more testing/comment before integration.
43959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
44059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong ********************************************************************/
44159f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt BlockQuantDequantH263DCInter(Short *rcoeff, Short *qcoeff, struct QPstruct *QuantParam,
44259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                 UChar *bitmaprow, UInt *bitmapzz, Int dummy, UChar shortHeader)
44359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
44459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int coeff, scale_q;
44559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int CBP = 0;
44659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int QP = QuantParam->QP;
44759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int QPx2plus = QuantParam->QPx2plus;
44859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int Addition = QuantParam->Addition;
44959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int shift = 15 + (QP >> 3);
45059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int ac_clip;    /* quantized coeff bound */
45159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int tmp;
45259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
45359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    OSCL_UNUSED_ARG(dummy);
45459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
45559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (shortHeader) ac_clip = 126; /* clip between [-127,126] (standard allows 127!) */
45659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else ac_clip = 2047;  /* clip between [-2048,2047] */
45759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
45859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    *bitmaprow = 0;
45959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    bitmapzz[0] = bitmapzz[1] = 0;
46059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    coeff = rcoeff[0];
46159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
46259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (coeff >= -QPx2plus && coeff < QPx2plus)
46359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
46459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        rcoeff[0] = 0;
46559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return CBP;//rcoeff[0] = 0; not needed since CBP will be zero
46659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
46759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
46859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
46959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        scale_q = scaleArrayV[QP];
47059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
47159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        coeff = aan_dc_scale(coeff, QP);
47259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
47359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        scale_q = coeff_quant(coeff, scale_q, shift);
47459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
47559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //coeff = PV_MIN(ac_clip,PV_MAX(-ac_clip-1, tmp));
47659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        scale_q = coeff_clip(scale_q, ac_clip);
47759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
47859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        qcoeff[0] = scale_q;
47959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
48059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        QP <<= 1;
48159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //coeff = PV_MIN(2047,PV_MAX(-2048, tmp));
48259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        tmp = 2047;
48359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        coeff = coeff_dequant(scale_q, QP, Addition, tmp);
48459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
48559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        rcoeff[0] = coeff;
48659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
48759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        (*bitmaprow) = 128;
48859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        bitmapzz[0] = (ULong)1 << 31;
48959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        CBP = 1;
49059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
49159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return CBP;
49259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
49359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
49459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
49559f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt BlockQuantDequantH263DCIntra(Short *rcoeff, Short *qcoeff, struct QPstruct *QuantParam,
49659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                 UChar *bitmaprow, UInt *bitmapzz, Int dc_scaler, UChar shortHeader)
49759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
49859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int tmp, coeff;
49959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
50059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    OSCL_UNUSED_ARG(QuantParam);
50159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
50259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    *bitmaprow = 0;
50359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    coeff = rcoeff[0];
50459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
50559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (coeff >= 0) coeff += (dc_scaler >> 1) ;
50659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else            coeff -= (dc_scaler >> 1) ;
50759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    tmp = scaleArrayV2[dc_scaler];
50859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    tmp = coeff * tmp;
50959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    tmp >>= (15 + (dc_scaler >> 4));
51059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    tmp += ((UInt)tmp >> 31);
51159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
51259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (shortHeader)
51359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        tmp = PV_MAX(1, PV_MIN(254, tmp));
51459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
51559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (tmp)
51659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
51759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        qcoeff[0] = tmp;
51859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        coeff = tmp * dc_scaler;
51959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        coeff = PV_MAX(-2048, PV_MIN(2047, coeff));
52059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        rcoeff[0] = coeff;
52159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        *bitmaprow = 128;
52259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        bitmapzz[0] = (ULong)1 << 31;
52359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
52459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
52559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return 0;
52659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
52759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
52859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifndef NO_MPEG_QUANT
52959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/***********************************************************************
53059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong Function: BlckQuantDequantMPEG
53159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong Date:     June 15, 1999
53259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong Purpose:  Combine BlockQuantMPEG and BlockDequantMPEGENC
53359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong Input:   coeff=> DCT coefficient
53459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong Output:  qcoeff=> quantized coefficient
53559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          rcoeff=> reconstructed coefficient
53659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong Modified:  7/5/01, break up function for Intra/Inter
53759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          8/3/01,  update with changes from H263 quant mode.
53859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          8/3/01,  add clipping to the reconstructed coefficient [-2048,2047]
53959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong          8/6/01,  optimize using multiplicative lookup-table.
54059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                     can be further optimized using ARM assembly, e.g.,
54159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                     clipping, 16-bit mult., etc !!!!!!!!!!!!!
54259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong ********************************************************************/
54359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
54459f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt BlockQuantDequantMPEGInter(Short *rcoeff, Short *qcoeff, Int QP, Int *qmat,
54559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                               UChar bitmapcol[ ], UChar *bitmaprow, UInt *bitmapzz,
54659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                               Int dctMode, Int comp, Int dc_scaler)
54759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
54859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int i, zz;
54959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int tmp, coeff, q_value = 0;
55059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int sum = 0;
55159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int stepsize, QPx2 = QP << 1;
55259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int CBP = 0;
55359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int round = 1 << 15;
55459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int q_scale = scaleArrayV[QP];
55559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int shift = 15 + (QP >> 3);
55659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar *bcolptr = bitmapcol;
55759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
55859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    OSCL_UNUSED_ARG(dc_scaler);
55959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    OSCL_UNUSED_ARG(comp);
56059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
56159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
56259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    *((Int*)bitmapcol) = *((Int*)(bitmapcol + 4)) = 0;
56359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    bitmapzz[0] = bitmapzz[1] = 0;
56459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    *bitmaprow = 0;
56559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
56659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    rcoeff += 64;
56759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    i = 0;
56859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    bcolptr--;
56959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
57059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    do
57159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
57259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        bcolptr++;
57359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        coeff = rcoeff[i];
57459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (coeff == 0x7fff) /* all zero column */
57559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
57659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            i++;
57759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            continue;
57859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
57959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        do
58059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
58159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            q_value = AANScale[i];  /*  09/02/05 scaling for AAN*/
58259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /* aan scaling */
58359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            q_value = smlabb(q_value, coeff, round);
58459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
58559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff = q_value >> 16;
58659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
58759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            stepsize = qmat[i];
58859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//          if(coeff>0)     coeff = (16*coeff + (stepsize/2)) / stepsize;
58959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//          else            coeff = (16*coeff - (stepsize/2)) / stepsize;
59059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff <<= 4;
59159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (coeff >= 0) coeff += (stepsize >> 1) ;
59259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else            coeff -= (stepsize >> 1) ;
59359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            q_value = scaleArrayV2[stepsize];
59459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /* mpeg quant table scale */
59559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff = smulbb(coeff, q_value);
59659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
59759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff >>= (15 + (stepsize >> 4));
59859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff += ((UInt)coeff >> 31);
59959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
60059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /* QP scale */
60159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (coeff >= -QPx2 && coeff < QPx2)  /* quantized to zero*/
60259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
60359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                i += 8;
60459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
60559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else
60659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
60759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//              q_value = coeff/(QPx2);
60859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                q_value = coeff_quant(coeff, q_scale, shift);
60959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
61059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (q_value)                /* dequant */
61159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
61259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
61359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    zz = ZZTab[i];  /* zigzag order */
61459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
61559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    tmp = 2047;
61659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
61759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    q_value = clip_2047(q_value, tmp);
61859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
61959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    qcoeff[zz>>1] = q_value;
62059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
62159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    //q_value=(((coeff*2)+SIGN0(coeff))*stepsize*QP)/16;
62259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    /* no need for SIGN0, no zero coming in this {} */
62359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    q_value = coeff_dequant_mpeg(q_value, stepsize, QP, tmp);
62459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
62559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    rcoeff[i-64] = q_value;
62659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
62759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    sum += q_value;
62859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    (*bcolptr) |= imask[i>>3];
62959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if ((zz >> 1) > 31) bitmapzz[1] |= (1 << (63 - (zz >> 1)));
63059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    else        bitmapzz[0] |= (1 << (31 - (zz >> 1)));
63159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
63259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                i += 8;
63359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
63459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff = rcoeff[i];
63559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
63659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        while (i < (dctMode << 3)) ;
63759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
63859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        i += (1 - (dctMode << 3));
63959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
64059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    while (i < dctMode) ;
64159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
64259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    i = dctMode;
64359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    tmp = 1 << (8 - i);
64459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    while (i--)
64559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
64659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (bitmapcol[i])(*bitmaprow) |= tmp;
64759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        tmp <<= 1;
64859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
64959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
65059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (*bitmaprow)
65159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        CBP = 1;   /* check CBP before mismatch control,  7/5/01 */
65259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
65359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* Mismatch control,  5/3/01 */
65459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (CBP)
65559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
65659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if ((sum&0x1) == 0)
65759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
65859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rcoeff--;  /* rcoeff[63] */
65959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff = *rcoeff;
66059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff ^= 0x1;
66159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            *rcoeff = coeff;
66259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (coeff)
66359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
66459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                bitmapcol[7] |= 1;
66559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                (*bitmaprow) |= 1;
66659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
66759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
66859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
66959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
67059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return CBP;
67159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
67259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
67359f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt BlockQuantDequantMPEGIntra(Short *rcoeff, Short *qcoeff, Int QP, Int *qmat,
67459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                               UChar bitmapcol[ ], UChar *bitmaprow, UInt *bitmapzz,
67559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                               Int dctMode, Int comp, Int dc_scaler)
67659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
67759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int i;
67859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int tmp, coeff, q_value = 0;
67959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int sum = 0;
68059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int stepsize;
68159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int CBP = 0;
68259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int round = 1 << 15;
68359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int q_scale = scaleArrayV[QP];
68459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int shift = 15 + (QP >> 3);
68559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int round2 = (3 * QP + 2) >> 2;
68659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int QPx2plus = (QP << 1) - round2;
68759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar *bmcolptr = bitmapcol;
68859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
68959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    OSCL_UNUSED_ARG(bitmapzz);
69059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    OSCL_UNUSED_ARG(comp);
69159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
69259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    *((Int*)bitmapcol) = *((Int*)(bitmapcol + 4)) = 0;
69359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    *bitmaprow = 0;
69459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
69559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    rcoeff += 64;
69659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    i = 0;
69759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
69859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* DC value */
69959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    coeff = *rcoeff;
70059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
70159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (coeff == 0x7fff) /* all zero column */
70259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
70359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        bmcolptr++;
70459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        i++;
70559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
70659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
70759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
70859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        q_value = round + (coeff << 12);
70959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        coeff = q_value >> 16;
71059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /*if (coeff >= 0)   coeff = (coeff + (dc_scaler/2)) / dc_scaler;
71159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else            coeff = (coeff - (dc_scaler/2)) / dc_scaler;*/
71259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (coeff >= 0) coeff += (dc_scaler >> 1) ;
71359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else            coeff -= (dc_scaler >> 1) ;
71459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        q_value = scaleArrayV2[dc_scaler];
71559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
71659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* mpeg quant table scale */
71759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        coeff = smulbb(coeff, q_value);
71859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
71959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        coeff >>= (15 + (dc_scaler >> 4));
72059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        coeff += ((UInt)coeff >> 31);
72159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
72259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (coeff)
72359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
72459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff = PV_MAX(1, PV_MIN(254, coeff));
72559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            qcoeff[0] = coeff;
72659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
72759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff = smulbb(coeff, dc_scaler);
72859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
72959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            q_value = clip_2047(coeff, 2047);
73059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
73159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            sum = q_value;
73259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
73359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rcoeff[-64] = q_value;
73459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
73559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            bitmapcol[0] |= 128;
73659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
73759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        i += 8;
73859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
73959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* AC values */
74059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    do
74159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
74259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        coeff = rcoeff[i];
74359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (coeff == 0x7fff) /* all zero row */
74459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
74559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            bmcolptr++;
74659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            i++;
74759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            continue;
74859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
74959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        do
75059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
75159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /* scaling */
75259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            q_value = AANScale[i]; /*  09/02/05 */
75359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
75459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /* q_value = coeff*q_value + round */
75559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            q_value = smlabb(coeff, q_value, round);
75659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff = q_value >> 16;
75759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
75859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            stepsize = qmat[i];
75959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /*if(coeff>0)       coeff = (16*coeff + (stepsize/2)) / stepsize;
76059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else            coeff = (16*coeff - (stepsize/2)) / stepsize;*/
76159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff <<= 4;
76259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (coeff >= 0) coeff += (stepsize >> 1) ;
76359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else            coeff -= (stepsize >> 1) ;
76459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            q_value = scaleArrayV2[stepsize];
76559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
76659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /* scale mpeg quant */
76759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff = smulbb(coeff, q_value);
76859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
76959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff >>= (15 + (stepsize >> 4));
77059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff += ((UInt)coeff >> 31);
77159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
77259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (coeff >= -QPx2plus && coeff < QPx2plus)
77359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
77459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                i += 8;
77559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
77659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else
77759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
77859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                //q_value = ( coeff + SIGN0(coeff)*((3*QP+2)/4))/(2*QP);
77959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (coeff > 0) coeff += round2;
78059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                else if (coeff < 0) coeff -= round2;
78159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
78259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                q_value = smulbb(coeff, q_scale);
78359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                q_value >>= shift;
78459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                q_value += ((UInt)q_value >> 31);
78559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
78659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (q_value)
78759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
78859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    tmp = 2047;
78959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    q_value = clip_2047(q_value, tmp);
79059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
79159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    qcoeff[i] = q_value;
79259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
79359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    stepsize = smulbb(stepsize, QP);
79459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    q_value =  smulbb(q_value, stepsize);
79559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
79659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    q_value = coeff_dequant_mpeg_intra(q_value, tmp);
79759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    //q_value = (coeff*stepsize*QP*2)/16;
79859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
79959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    rcoeff[i-64] = q_value;
80059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
80159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    sum += q_value;
80259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    (*bmcolptr) |= imask[i>>3];
80359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
80459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                i += 8;
80559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
80659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff = rcoeff[i];
80759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
80859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        while (i < (dctMode << 3)) ;
80959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
81059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        bmcolptr++;
81159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        i += (1 - (dctMode << 3));
81259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
81359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    while (i < dctMode) ;
81459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
81559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    i = dctMode;
81659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    tmp = 1 << (8 - i);
81759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    while (i--)
81859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
81959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (bitmapcol[i])(*bitmaprow) |= tmp;
82059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        tmp <<= 1;
82159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
82259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
82359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (((*bitmaprow) &127) || (bitmapcol[0]&127))
82459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        CBP = 1;  /* check CBP before mismatch control,  7/5/01 */
82559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
82659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* Mismatch control,  5/3/01 */
82759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (CBP || bitmapcol[0])
82859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
82959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if ((sum&0x1) == 0)
83059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
83159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rcoeff--;  /* rcoeff[63] */
83259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff = *rcoeff;
83359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            coeff ^= 0x1;
83459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            *rcoeff = coeff;
83559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (coeff)
83659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
83759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                bitmapcol[7] |= 1;
83859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                (*bitmaprow) |= 1;
83959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
84059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
84159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
84259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
84359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return CBP;
84459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
84559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
84659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
84759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/***********************************************************************
84859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong Function: BlckQuantDequantMPEGDC
84959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong Date:     5/3/2001
85059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong Purpose:  MPEG Quant/Dequant for DC only block.
85159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong ********************************************************************/
85259f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt BlockQuantDequantMPEGDCInter(Short *rcoeff, Short *qcoeff, Int QP, Int *qmat,
85359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                 UChar bitmapcol[ ], UChar *bitmaprow, UInt *bitmapzz, Int dummy)
85459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
85559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int q_value, coeff, stepsize;
85659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int CBP = 0;
85759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int q_scale = scaleArrayV[QP];
85859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int shift = 15 + (QP >> 3);
85959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int QPx2 = QP << 1;
86059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
86159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    OSCL_UNUSED_ARG(dummy);
86259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
86359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    *((Int*)bitmapcol) = *((Int*)(bitmapcol + 4)) = 0;
86459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    *bitmaprow = 0;
86559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    bitmapzz[0] = bitmapzz[1] = 0;
86659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    coeff = rcoeff[0];
86759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    stepsize = qmat[0];
86859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
86959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /*if(coeff>0)       coeff = (16*coeff + (stepsize/2)) / stepsize;
87059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else            coeff = (16*coeff - (stepsize/2)) / stepsize;*/
87159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    coeff <<= 4;
87259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (coeff >= 0) coeff += (stepsize >> 1) ;
87359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else            coeff -= (stepsize >> 1) ;
87459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    q_value = scaleArrayV2[stepsize];
87559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
87659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    coeff = smulbb(coeff, q_value);
87759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
87859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    coeff >>= (15 + (stepsize >> 4));
87959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    coeff += ((UInt)coeff >> 31);
88059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
88159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (coeff >= -QPx2 && coeff < QPx2)
88259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
88359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        rcoeff[0] = 0;
88459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return CBP;
88559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
88659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
88759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
88859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//      q_value = coeff/(QPx2);
88959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        q_value = coeff_quant(coeff, q_scale, shift);
89059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
89159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (q_value)
89259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
89359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
89459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //PV_MIN(2047,PV_MAX(-2048, q_value));
89559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            q_value = clip_2047(q_value, 2047);
89659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            qcoeff[0] = q_value;
89759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            q_value = coeff_dequant_mpeg(q_value, stepsize, QP, 2047);
89859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //q_value=(((coeff*2)+SIGN0(coeff))*stepsize*QP)/16;
89959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rcoeff[0] = q_value;
90059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
90159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            bitmapcol[0] = 128;
90259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            (*bitmaprow) = 128;
90359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            bitmapzz[0] = (UInt)1 << 31;
90459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            CBP = 1;
90559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
90659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /* Mismatch control,  5/3/01 */
90759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if ((q_value&0x1) == 0)
90859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
90959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                rcoeff[63] = 1; /* after scaling it remains the same */
91059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                bitmapcol[7] |= 1;
91159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                (*bitmaprow) |= 1;
91259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
91359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
91459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
91559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return CBP;
91659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
91759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
91859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
91959f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt BlockQuantDequantMPEGDCIntra(Short *rcoeff, Short *qcoeff, Int QP, Int *qmat,
92059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                 UChar bitmapcol[ ], UChar *bitmaprow, UInt *bitmapzz,
92159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                                 Int dc_scaler)
92259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
92359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int tmp, coeff, q_value;
92459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
92559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    OSCL_UNUSED_ARG(QP);
92659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    OSCL_UNUSED_ARG(qmat);
92759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
92859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
92959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    *((Int*)bitmapcol) = *((Int*)(bitmapcol + 4)) = 0;
93059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    *bitmaprow = 0;
93159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    coeff = rcoeff[0];
93259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
93359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /*if (coeff >= 0)   tmp = (coeff + dc_scaler/2) / dc_scaler;
93459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else            tmp = (coeff - dc_scaler/2) / dc_scaler;*/
93559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (coeff >= 0) coeff += (dc_scaler >> 1) ;
93659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else            coeff -= (dc_scaler >> 1) ;
93759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    tmp = scaleArrayV2[dc_scaler];
93859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
93959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    tmp = smulbb(tmp, coeff);
94059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    tmp >>= (15 + (dc_scaler >> 4));
94159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    tmp += ((UInt)tmp >> 31);
94259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
94359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (tmp)
94459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
94559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        coeff = PV_MAX(1, PV_MIN(254, tmp));
94659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        qcoeff[0] = coeff;
94759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
94859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        q_value = smulbb(coeff, dc_scaler);
94959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        q_value = clip_2047(q_value, 2047);
95059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        rcoeff[0] = q_value;
95159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        bitmapcol[0] = 128;
95259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        *bitmaprow = 128;
95359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        bitmapzz[0] = (UInt)1 << 31;
95459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
95559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* Mismatch control,  5/3/01 */
95659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if ((q_value&0x1) == 0)
95759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
95859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rcoeff[63] = 1; /* after scaling it remains the same */
95959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            bitmapcol[7] |= 1;
96059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            (*bitmaprow) |= 1;
96159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
96259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
96359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
96459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return 0;
96559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
96659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif
96759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
968