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/******************************************************************************
1959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*
2059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* This software module was originally developed by
2159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*
2259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* Robert Danielsen (Telenor / ACTS-MoMuSys).
2359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*
2459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* and edited by
2559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*
2659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* Luis Ducla-Soares (IST / ACTS-MoMuSys).
2759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* Cor Quist (KPN / ACTS-MoMuSys).
2859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*
2959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* in the course of development of the MPEG-4 Video (ISO/IEC 14496-2) standard.
3059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* This software module is an implementation of a part of one or more MPEG-4
3159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* Video (ISO/IEC 14496-2) tools as specified by the MPEG-4 Video (ISO/IEC
3259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* 14496-2) standard.
3359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*
3459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* ISO/IEC gives users of the MPEG-4 Video (ISO/IEC 14496-2) standard free
3559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* license to this software module or modifications thereof for use in hardware
3659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* or software products claiming conformance to the MPEG-4 Video (ISO/IEC
3759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* 14496-2) standard.
3859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*
3959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* Those intending to use this software module in hardware or software products
4059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* are advised that its use may infringe existing patents. The original
4159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* developer of this software module and his/her company, the subsequent
4259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* editors and their companies, and ISO/IEC have no liability for use of this
4359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* software module or modifications thereof in an implementation. Copyright is
4459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* not released for non MPEG-4 Video (ISO/IEC 14496-2) standard conforming
4559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* products.
4659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*
4759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* ACTS-MoMuSys partners retain full right to use the code for his/her own
4859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* purpose, assign or donate the code to a third party and to inhibit third
4959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* parties from using the code for non MPEG-4 Video (ISO/IEC 14496-2) standard
5059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* conforming products. This copyright notice must be included in all copies or
5159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* derivative works.
5259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*
5359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* Copyright (c) 1997
5459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*
5559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*****************************************************************************/
5659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
5759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/***********************************************************HeaderBegin*******
5859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*
5959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* File: putvlc.c
6059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*
6159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* Author:   Robert Danielsen, Telenor R&D
6259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* Created:  07.07.96
6359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*
6459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* Description: Functions for writing to bitstream
6559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*
6659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* Notes:    Same kind of tables as in the MPEG-2 software simulation
6759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*       group software.
6859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*
6959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong* Modified:
7059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*   28.10.96 Robert Danielsen: Added PutCoeff_Intra(), renamed
7159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*           PutCoeff() to PutCoeff_Inter().
7259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*   06.11.96 Robert Danielsen: Added PutMCBPC_sep()
7359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*      01.05.97 Luis Ducla-Soares: added PutCoeff_Intra_RVLC() and
7459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*                                  PutCoeff_Inter_RVLC().
7559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong*
7659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong***********************************************************HeaderEnd*********/
7759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
7859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/************************    INCLUDE FILES    ********************************/
7959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
8059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
8159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#include "mp4lib_int.h"
8259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#include "mp4enc_lib.h"
8359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#include "vlc_enc_tab.h"
8459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#include "bitstream_io.h"
8559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#include "m4venc_oscl.h"
8659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#include "vlc_encode_inline.h"
8759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
8859f566c4ec3dfc097ad8163523e522280b27e5c3James Dongtypedef void (*BlockCodeCoeffPtr)(RunLevelBlock*, BitstreamEncVideo*, Int, Int, UChar) ;
8959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
9059f566c4ec3dfc097ad8163523e522280b27e5c3James Dongconst static Int mode_MBtype[] =
9159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
9259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    3,
9359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    0,
9459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    4,
9559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    1,
9659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    2,
9759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong};
9859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
9959f566c4ec3dfc097ad8163523e522280b27e5c3James Dongconst static Int zigzag_inv[NCOEFF_BLOCK] =
10059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
10159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    0,  1,  8, 16,  9,  2,  3, 10,
10259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    17, 24, 32, 25, 18, 11,  4,  5,
10359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    12, 19, 26, 33, 40, 48, 41, 34,
10459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    27, 20, 13,  6,  7, 14, 21, 28,
10559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    35, 42, 49, 56, 57, 50, 43, 36,
10659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    29, 22, 15, 23, 30, 37, 44, 51,
10759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    58, 59, 52, 45, 38, 31, 39, 46,
10859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    53, 60, 61, 54, 47, 55, 62, 63
10959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong};
11059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
11159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* Horizontal zigzag inverse */
11259f566c4ec3dfc097ad8163523e522280b27e5c3James Dongconst static Int zigzag_h_inv[NCOEFF_BLOCK] =
11359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
11459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    0, 1, 2, 3, 8, 9, 16, 17,
11559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    10, 11, 4, 5, 6, 7, 15, 14,
11659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    13, 12, 19, 18, 24, 25, 32, 33,
11759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    26, 27, 20, 21, 22, 23, 28, 29,
11859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    30, 31, 34, 35, 40, 41, 48, 49,
11959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    42, 43, 36, 37, 38, 39, 44, 45,
12059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    46, 47, 50, 51, 56, 57, 58, 59,
12159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    52, 53, 54, 55, 60, 61, 62, 63
12259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong};
12359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
12459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* Vertical zigzag inverse */
12559f566c4ec3dfc097ad8163523e522280b27e5c3James Dongconst static Int zigzag_v_inv[NCOEFF_BLOCK] =
12659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
12759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    0, 8, 16, 24, 1, 9, 2, 10,
12859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    17, 25, 32, 40, 48, 56, 57, 49,
12959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    41, 33, 26, 18, 3, 11, 4, 12,
13059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    19, 27, 34, 42, 50, 58, 35, 43,
13159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    51, 59, 20, 28, 5, 13, 6, 14,
13259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    21, 29, 36, 44, 52, 60, 37, 45,
13359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    53, 61, 22, 30, 7, 15, 23, 31,
13459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    38, 46, 54, 62, 39, 47, 55, 63
13559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong};
13659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
13759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifdef __cplusplus
13859f566c4ec3dfc097ad8163523e522280b27e5c3James Dongextern "C"
13959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
14059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif
14159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
14259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutCoeff_Inter(Int run, Int level, BitstreamEncVideo *bitstream);
14359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutCoeff_Inter_Last(Int run, Int level, BitstreamEncVideo *bitstream);
14459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutCoeff_Intra(Int run, Int level, BitstreamEncVideo *bitstream);
14559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutCoeff_Intra_Last(Int run, Int level, BitstreamEncVideo *bitstream);
14659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutCBPY(Int cbpy, Char intra, BitstreamEncVideo *bitstream);
14759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutMCBPC_Inter(Int cbpc, Int mode, BitstreamEncVideo *bitstream);
14859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutMCBPC_Intra(Int cbpc, Int mode, BitstreamEncVideo *bitstream);
14959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutMV(Int mvint, BitstreamEncVideo *bitstream);
15059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutDCsize_chrom(Int size, BitstreamEncVideo *bitstream);
15159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutDCsize_lum(Int size, BitstreamEncVideo *bitstream);
15259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutDCsize_lum(Int size, BitstreamEncVideo *bitstream);
15359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifndef NO_RVLC
15459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutCoeff_Inter_RVLC(Int run, Int level, BitstreamEncVideo *bitstream);
15559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutCoeff_Inter_RVLC_Last(Int run, Int level, BitstreamEncVideo *bitstream);
15659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutCoeff_Intra_RVLC(Int run, Int level, BitstreamEncVideo *bitstream);
15759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutCoeff_Intra_RVLC_Last(Int run, Int level, BitstreamEncVideo *bitstream);
15859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif
15959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutRunCoeff_Inter(Int run, Int level, BitstreamEncVideo *bitstream);
16059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutRunCoeff_Inter_Last(Int run, Int level, BitstreamEncVideo *bitstream);
16159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutRunCoeff_Intra(Int run, Int level, BitstreamEncVideo *bitstream);
16259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutRunCoeff_Intra_Last(Int run, Int level, BitstreamEncVideo *bitstream);
16359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutLevelCoeff_Inter(Int run, Int level, BitstreamEncVideo *bitstream);
16459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutLevelCoeff_Inter_Last(Int run, Int level, BitstreamEncVideo *bitstream);
16559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutLevelCoeff_Intra(Int run, Int level, BitstreamEncVideo *bitstream);
16659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int PutLevelCoeff_Intra_Last(Int run, Int level, BitstreamEncVideo *bitstream);
16759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
16859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    void RunLevel(VideoEncData *video, Int intra, Int intraDC_decision, Int ncoefblck[]);
16959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int IntraDC_dpcm(Int val, Int lum, BitstreamEncVideo *bitstream);
17059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Void DCACPred(VideoEncData *video, UChar Mode, Int *intraDC_decision, Int intraDCVlcQP);
17159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Void find_pmvs(VideoEncData *video, Int block, Int *mvx, Int *mvy);
17259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Void  WriteMVcomponent(Int f_code, Int dmv, BitstreamEncVideo *bs);
17359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    static Bool IntraDCSwitch_Decision(Int Mode, Int intra_dc_vlc_threshold, Int intraDCVlcQP);
17459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
17559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Void ScaleMVD(Int  f_code, Int  diff_vector, Int  *residual, Int  *vlc_code_mag);
17659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
17759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifdef __cplusplus
17859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
17959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif
18059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
18159f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt
18259f566c4ec3dfc097ad8163523e522280b27e5c3James DongPutDCsize_lum(Int size, BitstreamEncVideo *bitstream)
18359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
18459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length;
18559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
18659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (!(size >= 0 && size < 13))
18759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return -1;
18859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
18959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    length = DCtab_lum[size].len;
19059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (length)
19159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bitstream, length, DCtab_lum[size].code);
19259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
19359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
19459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
19559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
19659f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt
19759f566c4ec3dfc097ad8163523e522280b27e5c3James DongPutDCsize_chrom(Int size, BitstreamEncVideo *bitstream)
19859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
19959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length;
20059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
20159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (!(size >= 0 && size < 13))
20259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return -1;
20359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    length = DCtab_chrom[size].len;
20459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (length)
20559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bitstream, length, DCtab_chrom[size].code);
20659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
20759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
20859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
20959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
21059f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt
21159f566c4ec3dfc097ad8163523e522280b27e5c3James DongPutMV(Int mvint, BitstreamEncVideo *bitstream)
21259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
21359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int sign = 0;
21459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int absmv;
21559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length;
21659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
21759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (mvint > 32)
21859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
21959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        absmv = -mvint + 65;
22059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        sign = 1;
22159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
22259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
22359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        absmv = mvint;
22459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
22559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    length = mvtab[absmv].len;
22659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (length)
22759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bitstream, length, mvtab[absmv].code);
22859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
22959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (mvint != 0)
23059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
23159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPut1Bits(bitstream, sign);
23259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return (length + 1);
23359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
23459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
23559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return length;
23659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
23759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
23859f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt
23959f566c4ec3dfc097ad8163523e522280b27e5c3James DongPutMCBPC_Intra(Int cbp, Int mode, BitstreamEncVideo *bitstream)
24059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
24159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int ind;
24259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length;
24359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
24459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    ind = ((mode_MBtype[mode] >> 1) & 3) | ((cbp & 3) << 2);
24559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
24659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    length = mcbpc_intra_tab[ind].len;
24759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (length)
24859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bitstream, length, mcbpc_intra_tab[ind].code);
24959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
25059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
25159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
25259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
25359f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt
25459f566c4ec3dfc097ad8163523e522280b27e5c3James DongPutMCBPC_Inter(Int cbp, Int mode, BitstreamEncVideo *bitstream)
25559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
25659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int ind;
25759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length;
25859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
25959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    ind = (mode_MBtype[mode] & 7) | ((cbp & 3) << 3);
26059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
26159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    length = mcbpc_inter_tab[ind].len;
26259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (length)
26359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bitstream, length, mcbpc_inter_tab[ind].code);
26459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
26559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
26659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
26759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
26859f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt
26959f566c4ec3dfc097ad8163523e522280b27e5c3James DongPutCBPY(Int cbpy, Char intra, BitstreamEncVideo *bitstream)
27059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
27159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int ind;
27259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length;
27359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
27459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if ((intra == 0))
27559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        cbpy = 15 - cbpy;
27659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
27759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    ind = cbpy;
27859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
27959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    length = cbpy_tab[ind].len;
28059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (length)
28159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bitstream, length, (UInt)cbpy_tab[ind].code);
28259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
28359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
28459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
28559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
28659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* 5/16/01, break up function for last and not-last coefficient */
28759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* Note:::: I checked the ARM assembly for if( run > x && run < y) type
28859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    of code, they do a really good job compiling it to if( (UInt)(run-x) < y-x).
28959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    No need to hand-code it!!!!!, 6/1/2001 */
29059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
29159f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt PutCoeff_Inter(Int run, Int level, BitstreamEncVideo *bitstream)
29259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
29359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length = 0;
29459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
29559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (run < 2 && level < 13)
29659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
29759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab0[run][level-1].len;
29859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
29959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab0[run][level-1].code);
30059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
30159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 1 && run < 27 && level < 5)
30259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
30359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab1[run-2][level-1].len;
30459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
30559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab1[run-2][level-1].code);
30659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
30759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
30859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
30959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
31059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
31159f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt PutCoeff_Inter_Last(Int run, Int level, BitstreamEncVideo *bitstream)
31259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
31359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length = 0;
31459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
31559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (run < 2 && level < 4)
31659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
31759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab2[run][level-1].len;
31859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
31959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab2[run][level-1].code);
32059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
32159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 1 && run < 42 && level == 1)
32259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
32359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab3[run-2].len;
32459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
32559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab3[run-2].code);
32659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
32759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
32859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
32959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
33059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
33159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* 5/16/01, break up function for last and not-last coefficient */
33259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
33359f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt PutCoeff_Intra(Int run, Int level, BitstreamEncVideo *bitstream)
33459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
33559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length = 0;
33659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
33759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (run == 0 && level < 28)
33859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
33959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab4[level-1].len;
34059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
34159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab4[level-1].code);
34259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
34359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run == 1 && level < 11)
34459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
34559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab5[level-1].len;
34659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
34759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab5[level-1].code);
34859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
34959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 1 && run < 10 && level < 6)
35059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
35159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab6[run-2][level-1].len;
35259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
35359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab6[run-2][level-1].code);
35459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
35559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 9 && run < 15 && level == 1)
35659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
35759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab7[run-10].len;
35859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
35959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab7[run-10].code);
36059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
36159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
36259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
36359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
36459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
36559f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt PutCoeff_Intra_Last(Int run, Int level, BitstreamEncVideo *bitstream)
36659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
36759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length = 0;
36859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
36959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (run == 0 && level < 9)
37059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
37159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab8[level-1].len;
37259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
37359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab8[level-1].code);
37459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
37559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 0 && run < 7 && level < 4)
37659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
37759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab9[run-1][level-1].len;
37859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
37959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab9[run-1][level-1].code);
38059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
38159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 6 && run < 21 && level == 1)
38259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
38359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab10[run-7].len;
38459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
38559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab10[run-7].code);
38659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
38759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
38859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
38959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
39059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
39159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* 5/16/01, break up function for last and not-last coefficient */
39259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifndef NO_RVLC
39359f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt PutCoeff_Inter_RVLC(Int run, Int level, BitstreamEncVideo *bitstream)
39459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
39559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length = 0;
39659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
39759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (run == 0 && level < 20)
39859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
39959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length =  coeff_RVLCtab14[level-1].len;
40059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
40159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab14[level-1].code);
40259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
40359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run == 1 && level < 11)
40459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
40559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab15[level-1].len;
40659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
40759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab15[level-1].code);
40859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
40959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 1 && run < 4 && level < 8)
41059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
41159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab16[run-2][level-1].len;
41259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
41359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab16[run-2][level-1].code);
41459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
41559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run == 4 && level < 6)
41659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
41759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab17[level-1].len;
41859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
41959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab17[level-1].code);
42059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
42159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 4 && run < 8 && level < 5)
42259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
42359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab18[run-5][level-1].len;
42459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
42559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab18[run-5][level-1].code);
42659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
42759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 7 && run < 10 && level < 4)
42859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
42959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab19[run-8][level-1].len;
43059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
43159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab19[run-8][level-1].code);
43259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
43359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 9 && run < 18 && level < 3)
43459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
43559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab20[run-10][level-1].len;
43659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
43759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab20[run-10][level-1].code);
43859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
43959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 17 && run < 39 && level == 1)
44059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
44159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab21[run-18].len;
44259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
44359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab21[run-18].code);
44459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
44559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
44659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
44759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
44859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
44959f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt PutCoeff_Inter_RVLC_Last(Int run, Int level, BitstreamEncVideo *bitstream)
45059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
45159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length = 0;
45259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
45359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (run >= 0 && run < 2 && level < 6)
45459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
45559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab22[run][level-1].len;
45659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
45759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab22[run][level-1].code);
45859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
45959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run == 2 && level < 4)
46059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
46159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab23[level-1].len;
46259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
46359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab23[level-1].code);
46459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
46559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 2 && run < 14 && level < 3)
46659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
46759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab24[run-3][level-1].len;
46859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
46959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab24[run-3][level-1].code);
47059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
47159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 13 && run < 45 && level == 1)
47259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
47359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab25[run-14].len;
47459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
47559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab25[run-14].code);
47659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
47759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
47859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
47959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
48059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
48159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* 5/16/01, break up function for last and not-last coefficient */
48259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
48359f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt PutCoeff_Intra_RVLC(Int run, Int level, BitstreamEncVideo *bitstream)
48459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
48559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length = 0;
48659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
48759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (run == 0 && level < 28)
48859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
48959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab1[level-1].len;
49059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
49159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab1[level-1].code);
49259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
49359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run == 1 && level < 14)
49459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
49559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab2[level-1].len;
49659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
49759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab2[level-1].code);
49859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
49959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run == 2 && level < 12)
50059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
50159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab3[level-1].len;
50259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
50359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab3[level-1].code);
50459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
50559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run == 3 && level < 10)
50659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
50759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab4[level-1].len;
50859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
50959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab4[level-1].code);
51059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
51159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 3 && run < 6 && level < 7)
51259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
51359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab5[run-4][level-1].len;
51459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
51559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab5[run-4][level-1].code);
51659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
51759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 5 && run < 8 && level < 6)
51859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
51959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab6[run-6][level-1].len;
52059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
52159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab6[run-6][level-1].code);
52259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
52359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 7 && run < 10 && level < 5)
52459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
52559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab7[run-8][level-1].len;
52659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
52759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab7[run-8][level-1].code);
52859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
52959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
53059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 9 && run < 13 && level < 3)
53159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
53259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab8[run-10][level-1].len;
53359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
53459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab8[run-10][level-1].code);
53559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
53659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 12 && run < 20 && level == 1)
53759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
53859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab9[run-13].len;
53959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
54059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab9[run-13].code);
54159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
54259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
54359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
54459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
54559f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt PutCoeff_Intra_RVLC_Last(Int run, Int level, BitstreamEncVideo *bitstream)
54659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
54759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length = 0;
54859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
54959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (run >= 0 && run < 2 && level < 6)
55059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
55159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab10[run][level-1].len;
55259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
55359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab10[run][level-1].code);
55459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
55559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run == 2 && level < 4)
55659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
55759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab11[level-1].len;
55859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
55959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab11[level-1].code);
56059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
56159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 2 && run < 14 && level < 3)
56259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
56359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab12[run-3][level-1].len;
56459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
56559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab12[run-3][level-1].code);
56659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
56759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 13 && run < 45 && level == 1)
56859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
56959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_RVLCtab13[run-14].len;
57059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
57159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_RVLCtab13[run-14].code);
57259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
57359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
57459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
57559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif
57659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
57759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* The following is for 3-mode VLC */
57859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
57959f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt
58059f566c4ec3dfc097ad8163523e522280b27e5c3James DongPutRunCoeff_Inter(Int run, Int level, BitstreamEncVideo *bitstream)
58159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
58259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length = 0;
58359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
58459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (run < 2 && level < 13)
58559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
58659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab0[run][level-1].len;
58759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
58859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
58959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutGT8Bits(bitstream, 7 + 2, 14/*3*/);
59059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //BitstreamPutBits(bitstream, 2, 2);
59159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab0[run][level-1].code);
59259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 9;
59359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
59459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
59559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 1 && run < 27 && level < 5)
59659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
59759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab1[run-2][level-1].len;
59859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
59959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
60059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutGT8Bits(bitstream, 7 + 2, 14/*3*/);
60159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //BitstreamPutBits(bitstream, 2, 2);
60259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab1[run-2][level-1].code);
60359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 9;
60459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
60559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
60659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
60759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
60859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
60959f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt PutRunCoeff_Inter_Last(Int run, Int level, BitstreamEncVideo *bitstream)
61059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
61159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length = 0;
61259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
61359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (run < 2 && level < 4)
61459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
61559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab2[run][level-1].len;
61659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
61759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
61859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutGT8Bits(bitstream, 7 + 2, 14/*3*/);
61959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //BitstreamPutBits(bitstream, 2, 2);
62059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab2[run][level-1].code);
62159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 9;
62259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
62359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
62459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 1 && run < 42 && level == 1)
62559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
62659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab3[run-2].len;
62759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
62859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
62959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutGT8Bits(bitstream, 7 + 2, 14/*3*/);
63059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //BitstreamPutBits(bitstream, 2, 2);
63159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab3[run-2].code);
63259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 9;
63359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
63459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
63559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
63659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
63759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
63859f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt PutRunCoeff_Intra(Int run, Int level, BitstreamEncVideo *bitstream)
63959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
64059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length = 0;
64159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
64259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (run == 0 && level < 28)
64359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
64459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab4[level-1].len;
64559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
64659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
64759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutGT8Bits(bitstream, 7 + 2, 14/*3*/);
64859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //BitstreamPutBits(bitstream, 2, 2);
64959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab4[level-1].code);
65059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 9;
65159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
65259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
65359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run == 1 && level < 11)
65459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
65559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab5[level-1].len;
65659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
65759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
65859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutGT8Bits(bitstream, 7 + 2, 14/*3*/);
65959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //BitstreamPutBits(bitstream, 2, 2);
66059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab5[level-1].code);
66159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 9;
66259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
66359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
66459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 1 && run < 10 && level < 6)
66559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
66659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab6[run-2][level-1].len;
66759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
66859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
66959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutGT8Bits(bitstream, 7 + 2, 14/*3*/);
67059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //BitstreamPutBits(bitstream, 2, 2);
67159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab6[run-2][level-1].code);
67259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 9;
67359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
67459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
67559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 9 && run < 15 && level == 1)
67659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
67759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab7[run-10].len;
67859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
67959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
68059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutGT8Bits(bitstream, 7 + 2, 14/*3*/);
68159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //BitstreamPutBits(bitstream, 2, 2);
68259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab7[run-10].code);
68359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 9;
68459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
68559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
68659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
68759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
68859f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt PutRunCoeff_Intra_Last(Int run, Int level, BitstreamEncVideo *bitstream)
68959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
69059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length = 0;
69159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
69259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (run == 0 && level < 9)
69359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
69459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab8[level-1].len;
69559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
69659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
69759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutGT8Bits(bitstream, 7 + 2, 14/*3*/);
69859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //BitstreamPutBits(bitstream, 2, 2);
69959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab8[level-1].code);
70059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 9;
70159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
70259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
70359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 0 && run < 7 && level < 4)
70459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
70559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab9[run-1][level-1].len;
70659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
70759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
70859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutGT8Bits(bitstream, 7 + 2, 14/*3*/);
70959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //BitstreamPutBits(bitstream, 2, 2);
71059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab9[run-1][level-1].code);
71159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 9;
71259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
71359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
71459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 6 && run < 21 && level == 1)
71559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
71659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab10[run-7].len;
71759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
71859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
71959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutGT8Bits(bitstream, 7 + 2, 14/*3*/);
72059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //BitstreamPutBits(bitstream, 2, 2);
72159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab10[run-7].code);
72259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 9;
72359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
72459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
72559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
72659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
72759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
72859f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt
72959f566c4ec3dfc097ad8163523e522280b27e5c3James DongPutLevelCoeff_Inter(Int run, Int level, BitstreamEncVideo *bitstream)
73059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
73159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length = 0;
73259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
73359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (run < 2 && level < 13)
73459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
73559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab0[run][level-1].len;
73659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
73759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
73859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, 7 + 1, 6/*3*/);
73959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab0[run][level-1].code);
74059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 8;
74159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
74259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
74359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 1 && run < 27 && level < 5)
74459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
74559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab1[run-2][level-1].len;
74659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
74759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
74859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, 7 + 1, 6/*3*/);
74959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab1[run-2][level-1].code);
75059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 8;
75159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
75259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
75359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
75459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
75559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
75659f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt PutLevelCoeff_Inter_Last(Int run, Int level, BitstreamEncVideo *bitstream)
75759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
75859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length = 0;
75959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
76059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (run < 2 && level < 4)
76159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
76259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab2[run][level-1].len;
76359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
76459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
76559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, 7 + 1, 6/*3*/);
76659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab2[run][level-1].code);
76759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 8;
76859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
76959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
77059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 1 && run < 42 && level == 1)
77159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
77259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab3[run-2].len;
77359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
77459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
77559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, 7 + 1, 6/*3*/);
77659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab3[run-2].code);
77759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 8;
77859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
77959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
78059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
78159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
78259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
78359f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt PutLevelCoeff_Intra(Int run, Int level, BitstreamEncVideo *bitstream)
78459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
78559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length = 0;
78659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
78759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (run == 0 && level < 28)
78859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
78959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab4[level-1].len;
79059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
79159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
79259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, 7 + 1, 6/*3*/);
79359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab4[level-1].code);
79459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 8;
79559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
79659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
79759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run == 1 && level < 11)
79859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
79959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab5[level-1].len;
80059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
80159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
80259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, 7 + 1, 6/*3*/);
80359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab5[level-1].code);
80459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 8;
80559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
80659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
80759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 1 && run < 10 && level < 6)
80859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
80959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab6[run-2][level-1].len;
81059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
81159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
81259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, 7 + 1, 6/*3*/);
81359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab6[run-2][level-1].code);
81459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 8;
81559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
81659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
81759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 9 && run < 15 && level == 1)
81859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
81959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab7[run-10].len;
82059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
82159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
82259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, 7 + 1, 6/*3*/);
82359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab7[run-10].code);
82459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 8;
82559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
82659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
82759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
82859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
82959f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt PutLevelCoeff_Intra_Last(Int run, Int level, BitstreamEncVideo *bitstream)
83059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
83159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int length = 0;
83259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
83359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (run == 0 && level < 9)
83459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
83559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab8[level-1].len;
83659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
83759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
83859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, 7 + 1, 6/*3*/);
83959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab8[level-1].code);
84059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 8;
84159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
84259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
84359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 0 && run < 7 && level < 4)
84459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
84559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab9[run-1][level-1].len;
84659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
84759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
84859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, 7 + 1, 6/*3*/);
84959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab9[run-1][level-1].code);
85059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 8;
85159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
85259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
85359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (run > 6 && run < 21 && level == 1)
85459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
85559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = coeff_tab10[run-7].len;
85659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length)
85759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
85859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, 7 + 1, 6/*3*/);
85959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bitstream, length, (UInt)coeff_tab10[run-7].code);
86059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length += 8;
86159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
86259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
86359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return length;
86459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
86559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
86659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
86759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
86859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* ======================================================================== */
86959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Function : MBVlcEncode()                                                */
87059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Date     : 09/10/2000                                                   */
87159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Purpose  : Encode GOV Header                                            */
87259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  In/out   :                                                              */
87359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Return   :                                                              */
87459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Modified : 5/21/01, break up into smaller functions                     */
87559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* ======================================================================== */
87659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifndef H263_ONLY
87759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/**************************************/
87859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* Data Partitioning I-VOP Encoding   */
87959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/**************************************/
88059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
88159f566c4ec3dfc097ad8163523e522280b27e5c3James Dongvoid MBVlcEncodeDataPar_I_VOP(
88259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    VideoEncData *video,
88359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int ncoefblck[],
88459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    void *blkCodePtr)
88559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
88659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
88759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    BitstreamEncVideo *bs1 = video->bitstream1;
88859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    BitstreamEncVideo *bs2 = video->bitstream2;
88959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    BitstreamEncVideo *bs3 = video->bitstream3;
89059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    int i;
89159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar Mode = video->headerInfo.Mode[video->mbnum];
89259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar CBP;
89359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//  MacroBlock *MB=video->outputMB;
89459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int mbnum = video->mbnum;
89559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int intraDC_decision, DC;
89659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//  int temp;
89759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int dquant; /* 3/15/01 */
89859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    RunLevelBlock *RLB = video->RLB;
89959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    BlockCodeCoeffPtr BlockCodeCoeff = (BlockCodeCoeffPtr) blkCodePtr;
90059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
90159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* DC and AC Prediction, 5/28/01, compute CBP, intraDC_decision*/
90259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    DCACPred(video, Mode, &intraDC_decision, video->QP_prev);
90359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
90459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* CBP, Run, Level, and Sign */
90559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    RunLevel(video, 1, intraDC_decision, ncoefblck);
90659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    CBP = video->headerInfo.CBP[mbnum];
90759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
90859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* Compute DQuant */
90959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    dquant = video->QPMB[mbnum] - video->QP_prev; /* 3/15/01, QP_prev may not equal QPMB[mbnum-1] if mbnum-1 is skipped*/
91059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
91159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    video->QP_prev = video->QPMB[mbnum];
91259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
91359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (dquant && Mode == MODE_INTRA)
91459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
91559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Mode = MODE_INTRA_Q;
91659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
91759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
91859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (dquant >= 0)
91959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        dquant = (PV_ABS(dquant) + 1);
92059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
92159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        dquant = (PV_ABS(dquant) - 1);
92259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
92359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* FIRST PART: ALL TO BS1 */
92459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
92559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    PutMCBPC_Intra(CBP, Mode, bs1); /* MCBPC */
92659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
92759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (Mode == MODE_INTRA_Q)
92859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /*  MAY NEED TO CHANGE DQUANT HERE  */
92959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bs1, 2, dquant);  /* dquant*/
93059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
93159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
93259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (intraDC_decision == 0)
93359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
93459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        for (i = 0; i < 6; i++)
93559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
93659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DC = video->RLB[i].level[0];
93759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (video->RLB[i].s[0])
93859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                DC = -DC;
93959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (i < 4)
94059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                /*temp =*/ IntraDC_dpcm(DC, 1, bs1);        /* dct_dc_size_luminance, */
94159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else                                    /* dct_dc_differential, and */
94259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                /*temp =*/ IntraDC_dpcm(DC, 0, bs1);        /* marker bit */
94359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
94459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
94559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
94659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* SECOND PART: ALL TO BS2*/
94759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
94859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    BitstreamPut1Bits(bs2, video->acPredFlag[video->mbnum]);    /* ac_pred_flag */
94959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
95059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /*temp=*/
95159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    PutCBPY(CBP >> 2, (Char)(1), bs2); /* cbpy */
95259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
95359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
95459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* THIRD PART:  ALL TO BS3*/
95559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* MB_CodeCoeff(video,bs3); */ /* 5/22/01, replaced with below */
95659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    for (i = 0; i < 6; i++)
95759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
95859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (CBP&(1 << (5 - i)))
95959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            (*BlockCodeCoeff)(&(RLB[i]), bs3, 1 - intraDC_decision, ncoefblck[i], Mode);/* Code Intra AC*/
96059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
96159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
96259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return ;
96359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
96459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
96559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/************************************/
96659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* Data Partitioning P-VOP Encoding */
96759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/************************************/
96859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
96959f566c4ec3dfc097ad8163523e522280b27e5c3James Dongvoid MBVlcEncodeDataPar_P_VOP(
97059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    VideoEncData *video,
97159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int ncoefblck[],
97259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    void *blkCodePtr)
97359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
97459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
97559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    BitstreamEncVideo *bs1 = video->bitstream1;
97659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    BitstreamEncVideo *bs2 = video->bitstream2;
97759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    BitstreamEncVideo *bs3 = video->bitstream3;
97859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    int i;
97959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int mbnum = video->mbnum;
98059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar Mode = video->headerInfo.Mode[mbnum];
98159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int QP_tmp = video->QPMB[mbnum];
98259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar CBP;
98359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//  MacroBlock *MB=video->outputMB;
98459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int intra, intraDC_decision, DC;
98559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int pmvx, pmvy;
98659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//  int temp;
98759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int dquant; /* 3/15/01 */
98859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    RunLevelBlock *RLB = video->RLB;
98959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    BlockCodeCoeffPtr BlockCodeCoeff = (BlockCodeCoeffPtr) blkCodePtr;
99059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
99159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    intra = (Mode == MODE_INTRA || Mode == MODE_INTRA_Q);
99259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
99359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* DC and AC Prediction, 5/28/01, compute CBP, intraDC_decision*/
99459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
99559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (intra)
99659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
99759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (video->usePrevQP)
99859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
99959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            QP_tmp = video->QPMB[mbnum-1];
100059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
100159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
100259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        DCACPred(video, Mode, &intraDC_decision, QP_tmp);
100359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
100459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
100559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        intraDC_decision = 0; /* used in RunLevel */
100659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
100759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* CBP, Run, Level, and Sign */
100859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    RunLevel(video, intra, intraDC_decision, ncoefblck);
100959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    CBP = video->headerInfo.CBP[mbnum];
101059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
101159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* Compute DQuant */
101259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    dquant = video->QPMB[mbnum] - video->QP_prev; /* 3/15/01, QP_prev may not equal QPMB[mbnum-1] if mbnum-1 is skipped*/
101359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
101459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (dquant && (Mode == MODE_INTRA || Mode == MODE_INTER))
101559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
101659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Mode += 2;  /* make it MODE_INTRA_Q and MODE_INTER_Q */
101759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
101859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
101959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (dquant >= 0)
102059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        dquant = (PV_ABS(dquant) + 1);
102159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
102259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        dquant = (PV_ABS(dquant) - 1);
102359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
102459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* FIRST PART: ALL TO BS1 */
102559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
102659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (CBP == 0 && intra == 0)  /* Determine if Skipped MB */
102759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
102859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if ((Mode == MODE_INTER) && (video->mot[mbnum][0].x == 0) && (video->mot[mbnum][0].y == 0))
102959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            Mode = video->headerInfo.Mode[video->mbnum] = MODE_SKIPPED;
103059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else if ((Mode == MODE_INTER4V) && (video->mot[mbnum][1].x == 0) && (video->mot[mbnum][1].y == 0)
103159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                 && (video->mot[mbnum][2].x == 0) && (video->mot[mbnum][2].y == 0)
103259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                 && (video->mot[mbnum][3].x == 0) && (video->mot[mbnum][3].y == 0)
103359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                 && (video->mot[mbnum][4].x == 0) && (video->mot[mbnum][4].y == 0))
103459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            Mode = video->headerInfo.Mode[video->mbnum] = MODE_SKIPPED;
103559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
103659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
103759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
103859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (Mode == MODE_SKIPPED)
103959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
104059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPut1Bits(bs1, 1); /* not_coded = 1 */
104159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return;
104259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
104359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
104459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPut1Bits(bs1, 0); /* not_coded =0 */
104559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
104659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    video->QP_prev = video->QPMB[mbnum];
104759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    video->usePrevQP = 1;
104859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
104959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    PutMCBPC_Inter(CBP, Mode, bs1); /* MCBPC */
105059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
105159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    video->header_bits -= BitstreamGetPos(bs1); /* Header Bits */
105259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
105359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (Mode == MODE_INTER || Mode == MODE_INTER_Q)
105459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
105559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        find_pmvs(video, 0, &pmvx, &pmvy); /* Get predicted motion vectors */
105659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        WriteMVcomponent(video->currVop->fcodeForward, video->mot[mbnum][0].x - pmvx, bs1); /* Write x to bitstream */
105759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        WriteMVcomponent(video->currVop->fcodeForward, video->mot[mbnum][0].y - pmvy, bs1);     /* Write y to bitstream */
105859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
105959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (Mode == MODE_INTER4V)
106059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
106159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        for (i = 1; i < 5; i++)
106259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
106359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            find_pmvs(video, i, &pmvx, &pmvy);
106459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            WriteMVcomponent(video->currVop->fcodeForward, video->mot[mbnum][i].x - pmvx, bs1);
106559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            WriteMVcomponent(video->currVop->fcodeForward, video->mot[mbnum][i].y - pmvy, bs1);
106659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
106759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
106859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    video->header_bits += BitstreamGetPos(bs1); /* Header Bits */
106959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
107059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* SECOND PART: ALL TO BS2 */
107159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
107259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
107359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (intra)
107459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
107559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPut1Bits(bs2, video->acPredFlag[video->mbnum]);    /* ac_pred_flag */
107659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /*temp=*/
107759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        PutCBPY(CBP >> 2, (Char)(Mode == MODE_INTRA || Mode == MODE_INTRA_Q), bs2); /* cbpy */
107859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
107959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (Mode == MODE_INTRA_Q)
108059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bs2, 2, dquant);  /* dquant, 3/15/01*/
108159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
108259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (intraDC_decision == 0)
108359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
108459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            for (i = 0; i < 6; i++)
108559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
108659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                DC = video->RLB[i].level[0];
108759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (video->RLB[i].s[0])
108859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    DC = -DC;
108959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (i < 4)
109059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    /*temp =*/ IntraDC_dpcm(DC, 1, bs2);        /* dct_dc_size_luminance, */
109159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                else                                    /* dct_dc_differential, and */
109259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    /*temp =*/ IntraDC_dpcm(DC, 0, bs2);        /* marker bit */
109359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
109459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
109559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
109659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /****************************/  /* THIRD PART: ALL TO BS3 */
109759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        for (i = 0; i < 6; i++)
109859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
109959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (CBP&(1 << (5 - i)))
110059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                (*BlockCodeCoeff)(&(RLB[i]), bs3, 1 - intraDC_decision, ncoefblck[i], Mode);/* Code Intra AC*/
110159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
110259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
110359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
110459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
110559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /*temp=*/
110659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        PutCBPY(CBP >> 2, (Char)(Mode == MODE_INTRA || Mode == MODE_INTRA_Q), bs2); /* cbpy */
110759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (Mode == MODE_INTER_Q)
110859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /*  MAY NEED TO CHANGE DQUANT HERE  */
110959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bs2, 2, dquant);  /* dquant, 3/15/01*/
111059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
111159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /****************************/  /* THIRD PART: ALL TO BS3 */
111259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        for (i = 0; i < 6; i++)
111359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
111459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (CBP&(1 << (5 - i)))
111559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                (*BlockCodeCoeff)(&(RLB[i]), bs3, 0, ncoefblck[i], Mode);/* Code Intra AC*/
111659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
111759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
111859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
111959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return ;
112059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
112159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif /* H263_ONLY */
112259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/****************************************************************************************/
112359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* Short Header/Combined Mode with or without Error Resilience I-VOP and P-VOP Encoding */
112459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* 5/21/01, B-VOP is not implemented yet!!!!                                            */
112559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/****************************************************************************************/
112659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
112759f566c4ec3dfc097ad8163523e522280b27e5c3James Dongvoid MBVlcEncodeCombined_I_VOP(
112859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    VideoEncData *video,
112959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int ncoefblck[],
113059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    void *blkCodePtr)
113159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
113259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
113359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    BitstreamEncVideo *bs1 = video->bitstream1;
113459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//  BitstreamEncVideo *bs2 = video->bitstream2;
113559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//  BitstreamEncVideo *bs3 = video->bitstream3;
113659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    int i;
113759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar Mode = video->headerInfo.Mode[video->mbnum];
113859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar CBP = video->headerInfo.CBP[video->mbnum];
113959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//  MacroBlock *MB=video->outputMB;
114059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int mbnum = video->mbnum;
114159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int intraDC_decision;
114259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//  int temp;
114359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int dquant; /* 3/15/01 */
114459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    RunLevelBlock *RLB = video->RLB;
114559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int DC;
114659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int shortVideoHeader = video->vol[video->currLayer]->shortVideoHeader;
114759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    BlockCodeCoeffPtr BlockCodeCoeff = (BlockCodeCoeffPtr) blkCodePtr;
114859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
114959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* DC and AC Prediction, 5/28/01, compute CBP, intraDC_decision*/
115059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
115159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifndef H263_ONLY
115259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (!shortVideoHeader)
115359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        DCACPred(video, Mode, &intraDC_decision, video->QP_prev);
115459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
115559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif
115659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
115759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        intraDC_decision = 0;
115859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
115959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
116059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* CBP, Run, Level, and Sign */
116159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
116259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    RunLevel(video, 1, intraDC_decision, ncoefblck);
116359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    CBP = video->headerInfo.CBP[mbnum];
116459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
116559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* Compute DQuant */
116659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    dquant = video->QPMB[mbnum] - video->QP_prev; /* 3/15/01, QP_prev may not equal QPMB[mbnum-1] if mbnum-1 is skipped*/
116759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
116859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    video->QP_prev = video->QPMB[mbnum];
116959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
117059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (dquant && Mode == MODE_INTRA)
117159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
117259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Mode = MODE_INTRA_Q;
117359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
117459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
117559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (dquant >= 0)
117659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        dquant = (PV_ABS(dquant) + 1);
117759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
117859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        dquant = (PV_ABS(dquant) - 1);
117959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
118059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    PutMCBPC_Intra(CBP, Mode, bs1); /* mcbpc I_VOP */
118159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
118259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (!video->vol[video->currLayer]->shortVideoHeader)
118359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
118459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPut1Bits(bs1, video->acPredFlag[video->mbnum]);    /* ac_pred_flag */
118559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
118659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
118759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /*temp=*/
118859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    PutCBPY(CBP >> 2, (Char)(1), bs1); /* cbpy */
118959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
119059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (Mode == MODE_INTRA_Q)
119159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /*  MAY NEED TO CHANGE DQUANT HERE */
119259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bs1, 2, dquant);  /* dquant, 3/15/01*/
119359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
119459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /*MB_CodeCoeff(video,bs1); 5/21/01, replaced by below */
119559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /*******************/
119659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifndef H263_ONLY
119759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (shortVideoHeader) /* Short Header DC coefficients */
119859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
119959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif
120059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        for (i = 0; i < 6; i++)
120159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
120259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DC = RLB[i].level[0];
120359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (RLB[i].s[0])
120459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                DC = -DC;
120559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (DC != 128)
120659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                BitstreamPutBits(bs1, 8, DC);   /* intra_dc_size_luminance */
120759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else
120859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                BitstreamPutBits(bs1, 8, 255);          /* intra_dc_size_luminance */
120959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (CBP&(1 << (5 - i)))
121059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                (*BlockCodeCoeff)(&(RLB[i]), bs1, 1, ncoefblck[i], Mode); /* Code short header Intra AC*/
121159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
121259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifndef H263_ONLY
121359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
121459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (intraDC_decision == 0)   /* Combined Intra Mode DC and AC coefficients */
121559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
121659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        for (i = 0; i < 6; i++)
121759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
121859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DC = RLB[i].level[0];
121959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (RLB[i].s[0])
122059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                DC = -DC;
122159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
122259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (i < 4)
122359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                /*temp =*/ IntraDC_dpcm(DC, 1, bs1);        /* dct_dc_size_luminance, */
122459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else                                                /* dct_dc_differential, and */
122559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                /*temp =*/ IntraDC_dpcm(DC, 0, bs1);        /* marker bit */
122659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (CBP&(1 << (5 - i)))
122759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                (*BlockCodeCoeff)(&(RLB[i]), bs1, 1, ncoefblck[i], Mode);/* Code Intra AC */
122859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
122959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
123059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else   /* Combined Mode Intra DC/AC coefficients */
123159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
123259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        for (i = 0; i < 6; i++)
123359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
123459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (CBP&(1 << (5 - i)))
123559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                (*BlockCodeCoeff)(&(RLB[i]), bs1, 0, ncoefblck[i], Mode);/* Code Intra AC */
123659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
123759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
123859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif
123959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /*******************/
124059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return ;
124159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
124259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
124359f566c4ec3dfc097ad8163523e522280b27e5c3James Dongvoid MBVlcEncodeCombined_P_VOP(
124459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    VideoEncData *video,
124559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int ncoefblck[],
124659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    void *blkCodePtr)
124759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
124859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
124959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    BitstreamEncVideo *bs1 = video->bitstream1;
125059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//  BitstreamEncVideo *bs2 = video->bitstream2;
125159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//  BitstreamEncVideo *bs3 = video->bitstream3;
125259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    int i;
125359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int mbnum = video->mbnum;
125459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar Mode = video->headerInfo.Mode[mbnum];
125559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int QP_tmp = video->QPMB[mbnum];
125659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar CBP ;
125759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//  MacroBlock *MB=video->outputMB;
125859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int intra, intraDC_decision;
125959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int pmvx, pmvy;
126059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//  int temp;
126159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int dquant; /* 3/15/01 */
126259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    RunLevelBlock *RLB = video->RLB;
126359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int DC;
126459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int shortVideoHeader = video->vol[video->currLayer]->shortVideoHeader;
126559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    BlockCodeCoeffPtr BlockCodeCoeff = (BlockCodeCoeffPtr) blkCodePtr;
126659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
126759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    intra = (Mode == MODE_INTRA || Mode == MODE_INTRA_Q);
126859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
126959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* DC and AC Prediction, 5/28/01, compute intraDC_decision*/
127059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifndef H263_ONLY
127159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (!shortVideoHeader && intra)
127259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
127359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (video->usePrevQP)
127459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
127559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            QP_tmp = video->QPMB[mbnum-1];
127659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
127759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        DCACPred(video, Mode, &intraDC_decision, QP_tmp);
127859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
127959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
128059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif
128159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        intraDC_decision = 0;
128259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
128359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* CBP, Run, Level, and Sign */
128459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
128559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    RunLevel(video, intra, intraDC_decision, ncoefblck);
128659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    CBP = video->headerInfo.CBP[mbnum];
128759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
128859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* Compute DQuant */
128959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    dquant = video->QPMB[mbnum] - video->QP_prev; /* 3/15/01, QP_prev may not equal QPMB[mbnum-1] if mbnum-1 is skipped*/
129059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (dquant && (Mode == MODE_INTRA || Mode == MODE_INTER))
129159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
129259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Mode += 2;  /* make it MODE_INTRA_Q and MODE_INTER_Q */
129359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
129459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
129559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (dquant >= 0)
129659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        dquant = (PV_ABS(dquant) + 1);
129759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
129859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        dquant = (PV_ABS(dquant) - 1);
129959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
130059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (CBP == 0 && intra == 0)  /* Determine if Skipped MB */
130159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
130259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if ((Mode == MODE_INTER) && (video->mot[mbnum][0].x == 0) && (video->mot[mbnum][0].y == 0))
130359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            Mode = video->headerInfo.Mode[video->mbnum] = MODE_SKIPPED;
130459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else if ((Mode == MODE_INTER4V) && (video->mot[mbnum][1].x == 0) && (video->mot[mbnum][1].y == 0)
130559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                 && (video->mot[mbnum][2].x == 0) && (video->mot[mbnum][2].y == 0)
130659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                 && (video->mot[mbnum][3].x == 0) && (video->mot[mbnum][3].y == 0)
130759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                 && (video->mot[mbnum][4].x == 0) && (video->mot[mbnum][4].y == 0))
130859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            Mode = video->headerInfo.Mode[video->mbnum] = MODE_SKIPPED;
130959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
131059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
131159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (Mode == MODE_SKIPPED)
131259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
131359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPut1Bits(bs1, 1); /* not_coded = 1 */
131459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return;
131559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
131659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
131759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPut1Bits(bs1, 0); /* not_coded =0 */
131859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
131959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    video->QP_prev = video->QPMB[mbnum];
132059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    video->usePrevQP = 1;
132159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
132259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    PutMCBPC_Inter(CBP, Mode, bs1); /* mcbpc P_VOP */
132359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
132459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (!video->vol[video->currLayer]->shortVideoHeader && intra)
132559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
132659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPut1Bits(bs1, video->acPredFlag[video->mbnum]);    /* ac_pred_flag */
132759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
132859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
132959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /*temp=*/
133059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    PutCBPY(CBP >> 2, (Char)(intra), bs1); /* cbpy */
133159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
133259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (Mode == MODE_INTRA_Q || Mode == MODE_INTER_Q)
133359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /*  MAY NEED TO CHANGE DQUANT HERE  */
133459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bs1, 2, dquant);  /* dquant, 3/15/01*/
133559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
133659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    video->header_bits -= BitstreamGetPos(bs1); /* Header Bits */
133759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
133859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (!((video->vol[video->currLayer]->scalability) && (video->currVop->refSelectCode == 3)))
133959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
134059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (Mode == MODE_INTER || Mode == MODE_INTER_Q)
134159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
134259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            find_pmvs(video, 0, &pmvx, &pmvy); /* Get predicted motion vectors */
134359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            WriteMVcomponent(video->currVop->fcodeForward, video->mot[mbnum][0].x - pmvx, bs1); /* Write x to bitstream */
134459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            WriteMVcomponent(video->currVop->fcodeForward, video->mot[mbnum][0].y - pmvy, bs1);     /* Write y to bitstream */
134559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
134659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else if (Mode == MODE_INTER4V)
134759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
134859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            for (i = 1; i < 5; i++)
134959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
135059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                find_pmvs(video, i, &pmvx, &pmvy);
135159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                WriteMVcomponent(video->currVop->fcodeForward, video->mot[mbnum][i].x - pmvx, bs1);
135259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                WriteMVcomponent(video->currVop->fcodeForward, video->mot[mbnum][i].y - pmvy, bs1);
135359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
135459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
135559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
135659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    video->header_bits += BitstreamGetPos(bs1); /* Header Bits */
135759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
135859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* MB_CodeCoeff(video,bs1); */ /* 5/22/01, replaced with below */
135959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /****************************/
136059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (intra)
136159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
136259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifndef H263_ONLY
136359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (shortVideoHeader) /* Short Header DC coefficients */
136459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
136559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif
136659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            for (i = 0; i < 6; i++)
136759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
136859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                DC = RLB[i].level[0];
136959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (RLB[i].s[0])
137059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    DC = -DC;
137159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (DC != 128)
137259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    BitstreamPutBits(bs1, 8, DC);   /* intra_dc_size_luminance */
137359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                else
137459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    BitstreamPutBits(bs1, 8, 255);          /* intra_dc_size_luminance */
137559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (CBP&(1 << (5 - i)))
137659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    (*BlockCodeCoeff)(&(RLB[i]), bs1, 1, ncoefblck[i], Mode); /* Code short header Intra AC*/
137759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
137859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifndef H263_ONLY
137959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
138059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else if (intraDC_decision == 0)   /* Combined Intra Mode DC and AC coefficients */
138159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
138259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            for (i = 0; i < 6; i++)
138359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
138459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                DC = RLB[i].level[0];
138559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (RLB[i].s[0])
138659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    DC = -DC;
138759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
138859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (i < 4)
138959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    /*temp =*/ IntraDC_dpcm(DC, 1, bs1);        /* dct_dc_size_luminance, */
139059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                else                                                /* dct_dc_differential, and */
139159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    /*temp =*/ IntraDC_dpcm(DC, 0, bs1);        /* marker bit */
139259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (CBP&(1 << (5 - i)))
139359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    (*BlockCodeCoeff)(&(RLB[i]), bs1, 1, ncoefblck[i], Mode);/* Code Intra AC */
139459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
139559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
139659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else   /* Combined Mode Intra DC/AC coefficients */
139759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
139859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            for (i = 0; i < 6; i++)
139959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
140059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (CBP&(1 << (5 - i)))
140159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    (*BlockCodeCoeff)(&(RLB[i]), bs1, 0, ncoefblck[i], Mode);/* Code Intra AC */
140259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
140359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
140459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif
140559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
140659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else   /* Shortheader or Combined INTER Mode AC coefficients */
140759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
140859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        for (i = 0; i < 6; i++)
140959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
141059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (CBP&(1 << (5 - i)))
141159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                (*BlockCodeCoeff)(&(RLB[i]), bs1, 0, ncoefblck[i], Mode);/* Code Inter AC*/
141259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
141359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
141459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /****************************/
141559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
141659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return ;
141759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
141859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
141959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* ======================================================================== */
142059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Function : BlockCodeCoeff()                                         */
142159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Date     : 09/18/2000                                                   */
142259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Purpose  : VLC Encode  AC/DC coeffs                                     */
142359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  In/out   :                                                              */
142459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Return   :                                                              */
142559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Modified :  5/16/01  grouping BitstreamPutBits calls                    */
142659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*              5/22/01  break up function                              */
142759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* ======================================================================== */
142859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifndef NO_RVLC
142959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*****************/
143059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* RVLC ENCODING */
143159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*****************/
143259f566c4ec3dfc097ad8163523e522280b27e5c3James DongVoid BlockCodeCoeff_RVLC(RunLevelBlock *RLB, BitstreamEncVideo *bs, Int j_start, Int j_stop, UChar Mode)
143359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
143459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    int length = 0;
143559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    int i;
143659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int level;
143759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int run;
143859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int intra = (Mode == MODE_INTRA || Mode == MODE_INTRA_Q);
143959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
144059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* Not Last Coefficient */
144159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    for (i = j_start; i < j_stop - 1; i++)
144259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
144359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        run = RLB->run[i];
144459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        level = RLB->level[i];
144559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //if(i==63||RLB->run[i+1] == -1)    /* Don't Code Last Coefficient Here */
144659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //  break;
144759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /*ENCODE RUN LENGTH */
144859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (level < 28 && run < 39)
144959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
145059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (intra)
145159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                length = PutCoeff_Intra_RVLC(run, level, bs);
145259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else
145359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                length = PutCoeff_Inter_RVLC(run, level, bs);
145459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
145559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
145659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length = 0;
145759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* ESCAPE CODING */
145859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length == 0)
145959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
146059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bs, 5 + 1, 2); /* ESCAPE + Not Last Coefficient */
146159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //BitstreamPutBits(bs,1,0); /* Not Last Coefficient */
146259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bs, 6 + 1, (run << 1) | 1); /* RUN + MARKER BIT*/
146359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //BitstreamPutBits(bs,1,1);  /* MARKER BIT */
146459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutGT8Bits(bs, 11, level); /* LEVEL */
146559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bs, 1 + 4, 16); /* MARKER BIT */
146659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //BitstreamPutBits(bs,4,0);  /* RVLC TRAILING ESCAPE */
146759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
146859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bs, 1, RLB->s[i]); /* SIGN BIT */
146959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
147059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* Last Coefficient!!! */
147159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    run = RLB->run[i];
147259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    level = RLB->level[i];
147359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
147459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /*ENCODE RUN LENGTH */
147559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (level < 6 && run < 45)
147659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
147759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (intra)
147859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length = PutCoeff_Intra_RVLC_Last(run, level, bs);
147959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
148059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length = PutCoeff_Inter_RVLC_Last(run, level, bs);
148159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
148259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
148359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = 0;
148459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* ESCAPE CODING */
148559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (length == 0)
148659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
148759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bs, 5 + 1, 3); /* ESCAPE CODE + Last Coefficient*/
148859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //BitstreamPutBits(bs,1,1); /* Last Coefficient !*/
148959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bs, 6 + 1, (run << 1) | 1); /* RUN + MARKER BIT*/
149059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //BitstreamPutBits(bs,1,1);  /* MARKER BIT */
149159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutGT8Bits(bs, 11, level); /* LEVEL */
149259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bs, 1 + 4, 16); /* MARKER BIT + RVLC TRAILING ESCAPE */
149359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //BitstreamPutBits(bs,4,0);  /* */
149459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
149559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    BitstreamPut1Bits(bs, RLB->s[i]); /* SIGN BIT */
149659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
149759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return ;
149859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
149959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif
150059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*******************************/
150159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* SHORT VIDEO HEADER ENCODING */
150259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*******************************/
150359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
150459f566c4ec3dfc097ad8163523e522280b27e5c3James DongVoid BlockCodeCoeff_ShortHeader(RunLevelBlock *RLB, BitstreamEncVideo *bs, Int j_start, Int j_stop, UChar Mode)
150559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
150659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    int length = 0;
150759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    int i;
150859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//  int temp;
150959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int level;
151059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int run;
151159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
151259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    OSCL_UNUSED_ARG(Mode);
151359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
151459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* Not Last Coefficient */
151559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    for (i = j_start; i < j_stop - 1; i++)
151659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
151759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        run = RLB->run[i];
151859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        level = RLB->level[i];
151959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//      if(i==63 ||RLB->run[i+1] == -1) /* Don't Code Last Coefficient Here */
152059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//          break;
152159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /*ENCODE RUN LENGTH */
152259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (level < 13)
152359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
152459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length = PutCoeff_Inter(run, level, bs);
152559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (length != 0)
152659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                /*temp =*/ BitstreamPut1Bits(bs, RLB->s[i]); /* Sign Bit */
152759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
152859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
152959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length = 0;
153059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* ESCAPE CODING */
153159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length == 0)
153259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
153359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (RLB->s[i])
153459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                level = -level;
153559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bs, 7 + 1, 6); /* ESCAPE CODE + Not Last Coefficient */
153659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //BitstreamPutBits(bs,1,0); /* Not Last Coefficient */
153759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bs, 6, run); /* RUN */
153859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPutBits(bs, 8, level&0xFF); /* LEVEL, mask to make sure length 8 */
153959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
154059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
154159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* Last Coefficient!!! */
154259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    run = RLB->run[i];
154359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    level = RLB->level[i];
154459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
154559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /*ENCODE RUN LENGTH */
154659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (level < 13)
154759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
154859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = PutCoeff_Inter_Last(run, level, bs);
154959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length != 0)
155059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /*temp =*/ BitstreamPut1Bits(bs, RLB->s[i]); /* Sign Bit */
155159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
155259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
155359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = 0;
155459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* ESCAPE CODING */
155559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (length == 0)
155659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
155759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (RLB->s[i])
155859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            level = -level;
155959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bs, 7 + 1, 7); /* ESCAPE CODE + Last Coefficient */
156059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //BitstreamPutBits(bs,1,1); /* Last Coefficient !!!*/
156159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bs, 6, run); /* RUN */
156259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bs, 8, level&0xFF); /* LEVEL, mask to make sure length 8  */
156359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
156459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
156559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return ;
156659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
156759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
156859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
156959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifndef H263_ONLY
157059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/****************/
157159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* VLC ENCODING */
157259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/****************/
157359f566c4ec3dfc097ad8163523e522280b27e5c3James DongVoid BlockCodeCoeff_Normal(RunLevelBlock *RLB, BitstreamEncVideo *bs, Int j_start, Int j_stop, UChar Mode)
157459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
157559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    int length = 0;
157659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    int i;
157759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    //int temp;
157859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int level;
157959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int run;
158059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int intra = (Mode == MODE_INTRA || Mode == MODE_INTRA_Q);
158159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int level_minus_max;
158259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int run_minus_max;
158359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int(*PutCoeff)(Int, Int, BitstreamEncVideo *); /* pointer to functions, 5/28/01 */
158459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
158559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* Not Last Coefficient!!! */
158659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
158759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (intra)
158859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        PutCoeff = &PutCoeff_Intra;
158959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
159059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        PutCoeff = &PutCoeff_Inter;
159159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
159259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    for (i = j_start; i < j_stop - 1; i++)
159359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
159459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        run = RLB->run[i];
159559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        level = RLB->level[i];
159659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
159759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* Encode Run Length */
159859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (level < 28)
159959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
160059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length = (*PutCoeff)(run, level, bs); /* 5/28/01 replaces above */
160159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
160259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
160359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
160459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length = 0;
160559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
160659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
160759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* First escape mode: LEVEL OFFSET */
160859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length == 0)
160959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
161059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (intra)
161159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
161259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                level_minus_max = level - intra_max_level[0][run];
161359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (level_minus_max < 28)
161459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    length = PutLevelCoeff_Intra(run, level_minus_max, bs);
161559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                else
161659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    length = 0;
161759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
161859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else
161959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
162059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                level_minus_max = level - inter_max_level[0][run];
162159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (level_minus_max < 13)
162259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    length = PutLevelCoeff_Inter(run, level_minus_max, bs);
162359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                else
162459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    length = 0;
162559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
162659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
162759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /* Second escape mode: RUN OFFSET */
162859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (length == 0)
162959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
163059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (level < 28)
163159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
163259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if (intra)
163359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
163459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        run_minus_max = run - (intra_max_run0[level] + 1);
163559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        length = PutRunCoeff_Intra(run_minus_max, level, bs);
163659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
163759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    else if (level < 13)
163859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
163959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        run_minus_max = run - (inter_max_run0[level] + 1);
164059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        length = PutRunCoeff_Inter(run_minus_max, level, bs);
164159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
164259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    else
164359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
164459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        length = 0;
164559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
164659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
164759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                else
164859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
164959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    length = 0;
165059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
165159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
165259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                /* Third escape mode: FIXED LENGTH CODE */
165359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (length == 0)
165459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
165559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if (RLB->s[i])
165659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        level = -level;
165759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    /*temp =*/
165859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    BitstreamPutBits(bs, 7 + 2 + 1, 30); /* ESCAPE CODE + Followed by 11 + Not Last Coefficient*/
165959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    //temp = BitstreamPutBits(bs,2,3); /* Followed by 11 */
166059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    //temp = BitstreamPutBits(bs, 1, 0); /* Not Last Coefficient*/
166159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    /*temp =*/
166259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    BitstreamPutBits(bs, 6 + 1, (run << 1) | 1); /* Encode Run + Marker Bit */
166359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    //temp = BitstreamPutBits(bs,1,1); /* Marker Bit */
166459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    /*temp =*/
166559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    BitstreamPutGT8Bits(bs, 12 + 1, ((level << 1) | 1)&0x1FFF); /* Encode Level, mask to make sure length 12  */
166659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    //temp = BitstreamPutBits(bs,1,1); /* Marker Bit */
166759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
166859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
166959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
167059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
167159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* Encode Sign Bit */
167259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length != 0)
167359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /*temp =*/ BitstreamPut1Bits(bs, RLB->s[i]); /* Sign Bit */
167459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
167559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
167659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* Last Coefficient */
167759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    run = RLB->run[i];
167859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    level = RLB->level[i];
167959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
168059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* Encode Run Length */
168159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (level < 9)
168259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
168359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (intra)
168459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
168559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length = PutCoeff_Intra_Last(run, level, bs);
168659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
168759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else if (level < 4)
168859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
168959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length = PutCoeff_Inter_Last(run, level, bs);
169059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
169159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
169259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
169359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            length = 0;
169459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
169559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
169659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
169759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
169859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        length = 0;
169959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
170059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
170159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* First escape mode: LEVEL OFFSET */
170259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (length == 0)
170359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
170459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (intra)
170559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
170659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            level_minus_max = level - intra_max_level[1][run];
170759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (level_minus_max < 9)
170859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                length = PutLevelCoeff_Intra_Last(run, level_minus_max, bs);
170959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else
171059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                length = 0;
171159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
171259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
171359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
171459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            level_minus_max = level - inter_max_level[1][run];
171559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (level_minus_max < 4)
171659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                length = PutLevelCoeff_Inter_Last(run, level_minus_max, bs);
171759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else
171859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                length = 0;
171959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
172059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* Second escape mode: RUN OFFSET */
172159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (length == 0)
172259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
172359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (level < 9)
172459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
172559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (intra)
172659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
172759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    run_minus_max = run - (intra_max_run1[level] + 1);
172859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    length = PutRunCoeff_Intra_Last(run_minus_max, level, bs);
172959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
173059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                else if (level < 4)
173159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
173259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    run_minus_max = run - (inter_max_run1[level] + 1);
173359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    length = PutRunCoeff_Inter_Last(run_minus_max, level, bs);
173459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
173559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                else
173659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
173759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    length = 0;
173859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
173959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
174059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else
174159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
174259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                length = 0;
174359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
174459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /* Third escape mode: FIXED LENGTH CODE */
174559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (length == 0)
174659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
174759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (RLB->s[i])
174859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    level = -level;
174959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                /*temp =*/
175059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                BitstreamPutGT8Bits(bs, 7 + 2 + 1, 31); /* ESCAPE CODE + Followed by 11 + Last Coefficient*/
175159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                //temp = BitstreamPutBits(bs,2,3); /* Followed by 11 */
175259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                //temp = BitstreamPutBits(bs, 1, 1); /* Last Coefficient!!!*/
175359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                /*temp =*/
175459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                BitstreamPutBits(bs, 6 + 1, (run << 1) | 1); /* Encode Run + Marker Bit */
175559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                //temp = BitstreamPutBits(bs,1,1); /* Marker Bit */
175659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                /*temp =*/
175759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                BitstreamPutGT8Bits(bs, 12 + 1, ((level << 1) | 1)&0x1FFF); /* Encode Level, mask to make sure length 8 */
175859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                //temp = BitstreamPutBits(bs,1,1); /* Marker Bit */
175959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
176059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
176159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
176259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
176359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* Encode Sign Bit */
176459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (length != 0)
176559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /*temp =*/ BitstreamPut1Bits(bs, RLB->s[i]);
176659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
176759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
176859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return ;
176959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
177059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
177159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif /* H263_ONLY */
177259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* ======================================================================== */
177359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Function : RUNLevel                                                     */
177459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Date     : 09/20/2000                                                   */
177559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Purpose  : Get the Coded Block Pattern for each block                   */
177659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  In/out   :                                                              */
177759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*      Int* qcoeff     Quantized DCT coefficients
177859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int Mode        Coding Mode
177959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Int ncoeffs     Number of coefficients                              */
178059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Return   :                                                              */
178159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*      Int CBP         Coded Block Pattern                                 */
178259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Modified :                                                              */
178359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* ======================================================================== */
178459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
178559f566c4ec3dfc097ad8163523e522280b27e5c3James Dongvoid RunLevel(VideoEncData *video, Int intra, Int intraDC_decision, Int ncoefblck[])
178659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
178759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int i, j;
178859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int CBP = video->headerInfo.CBP[video->mbnum];
178959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int ShortNacNintra = (!(video->vol[video->currLayer]->shortVideoHeader) && video->acPredFlag[video->mbnum] && intra);
179059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    MacroBlock *MB = video->outputMB;
179159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Short *dataBlock;
179259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int level;
179359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    RunLevelBlock *RLB;
179459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int run, idx;
179559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int *zz, nc, zzorder;
179659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar imask[6] = {0x1F, 0x2F, 0x37, 0x3B, 0x3D, 0x3E};
179759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UInt *bitmapzz;
179859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
179959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* Set Run, Level and CBP for this Macroblock */
180059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* ZZ scan is done here.  */
180159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
180259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (intra)
180359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
180459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
180559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (intraDC_decision != 0)
180659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            intra = 0;              /* DC/AC in Run/Level */
180759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
180859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        for (i = 0; i < 6 ; i++)
180959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
181059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
181159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            zz = (Int *) zigzag_inv;
181259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
181359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            RLB = video->RLB + i;
181459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
181559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            dataBlock = MB->block[i];
181659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
181759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (intra)
181859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
181959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                RLB->run[0] = 0;
182059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                level = dataBlock[0];
182159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                dataBlock[0] = 0; /* reset to zero */
182259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (level < 0)
182359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
182459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    RLB->level[0] = -level;
182559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    RLB->s[0] = 1;
182659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
182759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                else
182859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
182959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    RLB->level[0] = level;
183059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    RLB->s[0] = 0;
183159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
183259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
183359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
183459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            idx = intra;
183559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
183659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if ((CBP >> (5 - i)) & 1)
183759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
183859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (ShortNacNintra)
183959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
184059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    switch ((video->zz_direction >> (5 - i))&1)
184159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
184259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        case 0:
184359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            zz = (Int *)zigzag_v_inv;
184459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            break;
184559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        case 1:
184659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            zz = (Int *)zigzag_h_inv;
184759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            break;
184859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
184959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
185059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                run = 0;
185159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                nc = ncoefblck[i];
185259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                for (j = intra, zz += intra; j < nc; j++, zz++)
185359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
185459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    zzorder = *zz;
185559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    level = dataBlock[zzorder];
185659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if (level == 0)
185759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        run++;
185859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    else
185959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
186059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        dataBlock[zzorder] = 0; /* reset output */
186159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        if (level < 0)
186259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
186359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            RLB->level[idx] = -level;
186459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            RLB->s[idx] = 1;
186559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            RLB->run[idx] = run;
186659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            run = 0;
186759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            idx++;
186859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
186959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        else
187059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
187159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            RLB->level[idx] = level;
187259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            RLB->s[idx] = 0;
187359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            RLB->run[idx] = run;
187459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            run = 0;
187559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            idx++;
187659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
187759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
187859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
187959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
188059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
188159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            ncoefblck[i] = idx; /* 5/22/01, reuse ncoefblck */
188259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
188359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (idx == intra) /* reset CBP, nothing to be coded */
188459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                CBP &= imask[i];
188559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
188659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
188759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        video->headerInfo.CBP[video->mbnum] = CBP;
188859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
188959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return ;
189059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
189159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
189259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
189359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//      zz = (Int *) zigzag_inv;  no need to use it, default
189459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
189559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (CBP)
189659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
189759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            for (i = 0; i < 6 ; i++)
189859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
189959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                RLB = video->RLB + i;
190059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                idx = 0;
190159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
190259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if ((CBP >> (5 - i)) & 1)
190359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {   /* 7/30/01 */
190459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    /* Use bitmapzz to find the Run,Level,Sign symbols */
190559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    bitmapzz = video->bitmapzz[i];
190659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    dataBlock = MB->block[i];
190759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    nc  = ncoefblck[i];
190859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
190959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    idx = zero_run_search(bitmapzz, dataBlock, RLB, nc);
191059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
191159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                ncoefblck[i] = idx; /* 5/22/01, reuse ncoefblck */
191259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (idx == 0) /* reset CBP, nothing to be coded */
191359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    CBP &= imask[i];
191459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
191559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            video->headerInfo.CBP[video->mbnum] = CBP;
191659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
191759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return ;
191859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
191959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
192059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
192159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifndef H263_ONLY
192259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifdef __cplusplus
192359f566c4ec3dfc097ad8163523e522280b27e5c3James Dongextern "C"
192459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
192559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif
192659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    static Bool IntraDCSwitch_Decision(Int Mode, Int intra_dc_vlc_thr, Int intraDCVlcQP)
192759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
192859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Bool switched = FALSE;
192959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
193059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
193159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
193259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (intra_dc_vlc_thr != 0)
193359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
193459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                switched = (intra_dc_vlc_thr == 7 || intraDCVlcQP >= intra_dc_vlc_thr * 2 + 11);
193559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
193659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
193759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
193859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        return switched;
193959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
194059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#ifdef __cplusplus
194159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
194259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif
194359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
194459f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt IntraDC_dpcm(Int val, Int lum, BitstreamEncVideo *bitstream)
194559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
194659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int n_bits;
194759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int absval, size = 0;
194859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
194959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    absval = (val < 0) ? -val : val;    /* abs(val) */
195059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
195159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
195259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* compute dct_dc_size */
195359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
195459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    size = 0;
195559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    while (absval)
195659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
195759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        absval >>= 1;
195859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        size++;
195959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
196059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
196159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (lum)
196259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {   /* luminance */
196359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        n_bits = PutDCsize_lum(size, bitstream);
196459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
196559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
196659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {   /* chrominance */
196759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        n_bits = PutDCsize_chrom(size, bitstream);
196859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
196959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
197059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (size != 0)
197159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
197259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (val >= 0)
197359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
197459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            ;
197559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
197659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
197759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
197859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            absval = -val; /* set to "-val" MW 14-NOV-1996 */
197959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            val = absval ^((1 << size) - 1);
198059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
198159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bitstream, (size), (UInt)(val));
198259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        n_bits += size;
198359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
198459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (size > 8)
198559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            BitstreamPut1Bits(bitstream, 1);
198659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
198759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
198859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return n_bits;  /* # bits for intra_dc dpcm */
198959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
199059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
199159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
199259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* ======================================================================== */
199359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Function : DC_AC_PRED                                                   */
199459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Date     : 09/24/2000                                                   */
199559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Purpose  : DC and AC encoding of Intra Blocks                           */
199659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  In/out   :                                                              */
199759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*      VideoEncData    *video
199859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        UChar           Mode                                                */
199959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*  Return   :                                                              */
200059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/*                                                                          */
200159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong/* ======================================================================== */
200259f566c4ec3dfc097ad8163523e522280b27e5c3James DongInt cal_dc_scalerENC(Int QP, Int type) ;
200359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
200459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
200559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#define PREDICT_AC  for (m = 0; m < 7; m++){ \
200659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        tmp = DCAC[0]*QPtmp;\
200759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        if(tmp<0)   tmp = (tmp-(QP/2))/QP;\
200859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        else        tmp = (tmp+(QP/2))/QP;\
200959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        pred[m] = tmp;\
201059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        DCAC++;\
201159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
201259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
201359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
201459f566c4ec3dfc097ad8163523e522280b27e5c3James DongVoid DCACPred(VideoEncData *video, UChar Mode, Int *intraDC_decision, Int intraDCVlcQP)
201559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
201659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    MacroBlock *MB = video->outputMB;
201759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int mbnum = video->mbnum;
201859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    typeDCStore *DC_store = video->predDC + mbnum;
201959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    typeDCACStore *DCAC_row = video->predDCAC_row;
202059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    typeDCACStore *DCAC_col = video->predDCAC_col;
202159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Short   *DCAC;
202259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar Mode_top, Mode_left;
202359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
202459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Vol *currVol = video->vol[video->currLayer];
202559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int nMBPerRow = currVol->nMBPerRow;
202659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int x_pos = video->outputMB->mb_x; /* 5/28/01 */
202759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int y_pos = video->outputMB->mb_y;
202859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar QP = video->QPMB[mbnum];
202959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar *QPMB = video->QPMB;
203059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar *slice_nb = video->sliceNo;
203159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Bool bACPredEnable = video->encParams->ACDCPrediction;
203259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int *ACpred_flag = video->acPredFlag;
203359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int mid_grey = 128 << 3;
203459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int m;
203559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int comp;
203659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int dc_scale = 8, tmp;
203759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
203859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    static const Int Xpos[6] = { -1, 0, -1, 0, -1, -1};
203959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    static const Int Ypos[6] = { -1, -1, 0, 0, -1, -1};
204059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    static const Int Xtab[6] = {1, 0, 3, 2, 4, 5};
204159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    static const Int Ytab[6] = {2, 3, 0, 1, 4, 5};
204259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    static const Int Ztab[6] = {3, 2, 1, 0, 4, 5};
204359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
204459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* I added these to speed up comparisons */
204559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    static const Int Pos0[6] = { 1, 1, 0, 0, 1, 1};
204659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    static const Int Pos1[6] = { 1, 0, 1, 0, 1, 1};
204759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    static const Int B_Xtab[6] = {0, 1, 0, 1, 2, 3};
204859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    static const Int B_Ytab[6] = {0, 0, 1, 1, 2, 3};
204959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
205059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int direction[6];       /* 0: HORIZONTAL, 1: VERTICAL */
205159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int block_A, block_B, block_C;
205259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int grad_hor, grad_ver, DC_pred;
205359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Short pred[7], *predptr;
205459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Short pcoeff[42];
205559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Short *qcoeff;
205659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int S = 0, S1, S2;
205759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int diff, QPtmp;
205859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int newCBP[6];
205959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar mask1[6] = {0x20, 0x10, 0x8, 0x4, 0x2, 0x1};
206059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//  UChar mask2[6] = {0x1f,0x2f,0x37,0x3b,0x3d,0x3e};
206159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
206259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int y_offset, x_offset, x_tab, y_tab, z_tab;    /* speedup coefficients */
206359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int b_xtab, b_ytab;
206459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
206559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    video->zz_direction = 0;
206659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
206759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* Standard MPEG-4 Headers do DC/AC prediction*/
206859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /* check whether neighbors are INTER */
206959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (y_pos > 0)
207059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
207159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Mode_top = video->headerInfo.Mode[mbnum-nMBPerRow];
207259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (!(Mode_top == MODE_INTRA || Mode_top == MODE_INTRA_Q))
207359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
207459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC = DC_store[-nMBPerRow];
207559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            *DCAC++ = mid_grey;
207659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            *DCAC++ = mid_grey;
207759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            *DCAC++ = mid_grey;
207859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            *DCAC++ = mid_grey;
207959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            *DCAC++ = mid_grey;
208059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            *DCAC++ = mid_grey;
208159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /* set to 0 DCAC_row[x_pos][0..3] */
208259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (bACPredEnable == TRUE)
208359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
208459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                M4VENC_MEMSET(DCAC_row[x_pos][0], 0, sizeof(Short) << 5);
208559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
208659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
208759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
208859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (x_pos > 0)
208959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
209059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Mode_left = video->headerInfo.Mode[mbnum-1];
209159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (!(Mode_left == MODE_INTRA || Mode_left == MODE_INTRA_Q))
209259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
209359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC = DC_store[-1];
209459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            *DCAC++ = mid_grey;
209559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            *DCAC++ = mid_grey;
209659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            *DCAC++ = mid_grey;
209759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            *DCAC++ = mid_grey;
209859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            *DCAC++ = mid_grey;
209959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            *DCAC++ = mid_grey;
210059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /* set to 0 DCAC_col[x_pos][0..3] */
210159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (bACPredEnable == TRUE)
210259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
210359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                M4VENC_MEMSET(DCAC_col[0][0], 0, sizeof(Short) << 5);
210459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
210559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
210659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
210759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
210859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    S1 = 0;
210959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    S2 = 0;
211059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
211159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    for (comp = 0; comp < 6; comp++)
211259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
211359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
211459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (Ypos[comp] != 0)        y_offset = -nMBPerRow;
211559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else                    y_offset = 0;
211659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        x_offset = Xpos[comp];
211759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        x_tab = Xtab[comp];
211859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        y_tab = Ytab[comp];
211959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        z_tab = Ztab[comp];
212059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
212159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        b_xtab = B_Xtab[comp];
212259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        b_ytab = B_Ytab[comp];
212359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
212459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        qcoeff = MB->block[comp];
212559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
212659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /****************************/
212759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /*  Store DC coefficients */
212859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /****************************/
212959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* Store coeff values for Intra MB */
213059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (comp == 0) dc_scale = cal_dc_scalerENC(QP, 1) ;
213159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (comp == 4) dc_scale = cal_dc_scalerENC(QP, 2) ;
213259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
213359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        QPtmp = qcoeff[0] * dc_scale; /* DC value */
213459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
213559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (QPtmp > 2047)   /* 10/10/01, add clipping (bug fixed) */
213659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DC_store[0][comp] = 2047;
213759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else if (QPtmp < -2048)
213859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DC_store[0][comp] = -2048;
213959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
214059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DC_store[0][comp] = QPtmp;
214159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
214259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /**************************************************************/
214359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* Find the direction of the prediction and the DC prediction */
214459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /**************************************************************/
214559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
214659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if ((x_pos == 0) && y_pos == 0)
214759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {   /* top left corner */
214859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            block_A = (comp == 1 || comp == 3) ? DC_store[0][x_tab] : mid_grey;
214959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            block_B = (comp == 3) ? DC_store[x_offset][z_tab] : mid_grey;
215059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            block_C = (comp == 2 || comp == 3) ? DC_store[0][y_tab] : mid_grey;
215159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
215259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else if (x_pos == 0)
215359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {   /* left edge */
215459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            block_A = (comp == 1 || comp == 3) ? DC_store[0][x_tab] : mid_grey;
215559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            block_B = ((comp == 1 && (slice_nb[mbnum] == slice_nb[mbnum-nMBPerRow])) || comp == 3) ?
215659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                      DC_store[y_offset+x_offset][z_tab] : mid_grey;
215759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            block_C = (comp == 2 || comp == 3 ||
215859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                       (Pos0[comp] && (slice_nb[mbnum] == slice_nb[mbnum-nMBPerRow]))) ?
215959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                      DC_store[y_offset][y_tab] : mid_grey;
216059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
216159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else if (y_pos == 0)
216259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        { /* top row */
216359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            block_A = (comp == 1 || comp == 3 || (Pos1[comp] && (slice_nb[mbnum] == slice_nb[mbnum-1]))) ?
216459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                      DC_store[x_offset][x_tab] : mid_grey;
216559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            block_B = ((comp == 2 && (slice_nb[mbnum] == slice_nb[mbnum-1])) || comp == 3) ?
216659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                      DC_store[y_offset + x_offset][z_tab] : mid_grey;
216759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            block_C = (comp == 2 || comp == 3) ?
216859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                      DC_store[y_offset][y_tab] : mid_grey;
216959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
217059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
217159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
217259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            block_A = (comp == 1 || comp == 3 || (Pos1[comp] && (slice_nb[mbnum] == slice_nb[mbnum-1]))) ?
217359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                      DC_store[x_offset][x_tab] : mid_grey;
217459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            block_B = (((comp == 0 || comp == 4 || comp == 5) &&
217559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        (slice_nb[mbnum] == slice_nb[mbnum-1-nMBPerRow])) ||
217659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                       (comp == 1 && (slice_nb[mbnum] == slice_nb[mbnum-nMBPerRow])) ||
217759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                       (comp == 2 && (slice_nb[mbnum] == slice_nb[mbnum-1])) || (comp == 3)) ?
217859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                      (DC_store[y_offset + x_offset][z_tab]) : mid_grey;
217959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            block_C = (comp == 2 || comp == 3 || (Pos0[comp] && (slice_nb[mbnum] == slice_nb[mbnum-nMBPerRow]))) ?
218059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                      DC_store[y_offset][y_tab] : mid_grey;
218159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
218259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        grad_hor = block_B - block_C;
218359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        grad_ver = block_A - block_B;
218459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
218559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if ((PV_ABS(grad_ver)) < (PV_ABS(grad_hor)))
218659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
218759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DC_pred = block_C;
218859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            direction[comp] = 1;
218959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            video->zz_direction = (video->zz_direction) | mask1[comp];
219059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
219159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
219259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
219359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
219459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DC_pred = block_A;
219559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            direction[comp] = 0;
219659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            //video->zz_direction=video->zz_direction<<1;
219759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
219859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
219959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* DC prediction */
220059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        QPtmp = dc_scale; /* 5/28/01 */
220159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        qcoeff[0] -= (DC_pred + QPtmp / 2) / QPtmp;
220259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
220359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
220459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (bACPredEnable)
220559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
220659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /***********************/
220759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /* Find AC prediction  */
220859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /***********************/
220959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
221059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if ((x_pos == 0) && y_pos == 0)     /* top left corner */
221159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
221259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (direction[comp] == 0)
221359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
221459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if (comp == 1 || comp == 3)
221559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
221659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        QPtmp = QPMB[mbnum+x_offset];
221759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        DCAC = DCAC_col[0][b_ytab];
221859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        if (QPtmp != QP)
221959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
222059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            predptr = pred;
222159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            PREDICT_AC
222259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
222359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        else
222459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
222559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            predptr = DCAC;
222659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
222759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
222859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    else
222959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
223059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        predptr = pred;
223159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        pred[0] = pred[1] = pred[2] = pred[3] = pred[4] = pred[5] = pred[6] = 0;
223259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
223359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
223459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                else
223559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
223659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if (comp == 2 || comp == 3)
223759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
223859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        QPtmp = QPMB[mbnum+ y_offset];
223959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        DCAC = DCAC_row[x_pos][b_xtab];
224059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        if (QPtmp != QP)
224159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
224259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            predptr = pred;
224359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            PREDICT_AC
224459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
224559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        else
224659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
224759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            predptr = DCAC;
224859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
224959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
225059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    else
225159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
225259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        predptr = pred;
225359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        pred[0] = pred[1] = pred[2] = pred[3] = pred[4] = pred[5] = pred[6] = 0;
225459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
225559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
225659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
225759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else if (x_pos == 0)    /* left edge */
225859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
225959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (direction[comp] == 0)
226059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
226159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if (comp == 1 || comp == 3)
226259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
226359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        QPtmp = QPMB[mbnum+x_offset];
226459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        DCAC = DCAC_col[0][b_ytab];
226559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        if (QPtmp != QP)
226659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
226759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            predptr = pred;
226859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            PREDICT_AC
226959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
227059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        else
227159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
227259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            predptr = DCAC;
227359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
227459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
227559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    else
227659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
227759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        predptr = pred;
227859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        pred[0] = pred[1] = pred[2] = pred[3] = pred[4] = pred[5] = pred[6] = 0;
227959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
228059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
228159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                else
228259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
228359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
228459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if ((Pos0[comp] && (slice_nb[mbnum] == slice_nb[mbnum-nMBPerRow]))
228559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            || comp == 2 || comp == 3)
228659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
228759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        QPtmp = QPMB[mbnum+y_offset];
228859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        DCAC = DCAC_row[x_pos][b_xtab];
228959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        if (QPtmp != QP)
229059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
229159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            predptr = pred;
229259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            PREDICT_AC
229359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
229459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        else
229559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
229659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            predptr = DCAC;
229759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
229859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
229959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    else
230059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
230159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        predptr = pred;
230259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        pred[0] = pred[1] = pred[2] = pred[3] = pred[4] = pred[5] = pred[6] = 0;
230359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
230459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
230559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
230659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else if (y_pos == 0)  /* top row */
230759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
230859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (direction[comp] == 0)
230959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
231059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if ((Pos1[comp] && (slice_nb[mbnum] == slice_nb[mbnum-1]))
231159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            || comp == 1 || comp == 3)
231259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
231359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        QPtmp = QPMB[mbnum+x_offset];
231459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        DCAC = DCAC_col[0][b_ytab];
231559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        if (QPtmp != QP)
231659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
231759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            predptr = pred;
231859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            PREDICT_AC
231959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
232059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        else
232159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
232259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            predptr = DCAC;
232359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
232459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
232559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    else
232659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
232759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        predptr = pred;
232859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        pred[0] = pred[1] = pred[2] = pred[3] = pred[4] = pred[5] = pred[6] = 0;
232959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
233059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
233159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                else
233259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
233359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if (comp == 2 || comp == 3)
233459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
233559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        QPtmp = QPMB[mbnum+y_offset];
233659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        DCAC = DCAC_row[x_pos][b_xtab];
233759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        if (QPtmp != QP)
233859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
233959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            predptr = pred;
234059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            PREDICT_AC
234159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
234259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        else
234359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
234459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            predptr = DCAC;
234559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
234659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
234759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    else
234859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
234959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        predptr = pred;
235059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        pred[0] = pred[1] = pred[2] = pred[3] = pred[4] = pred[5] = pred[6] = 0;
235159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
235259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
235359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
235459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else
235559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
235659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                if (direction[comp] == 0)
235759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
235859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if ((Pos1[comp] && (slice_nb[mbnum] == slice_nb[mbnum-1]))
235959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            || comp == 1 || comp == 3)
236059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
236159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        QPtmp = QPMB[mbnum+x_offset];
236259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        DCAC = DCAC_col[0][b_ytab];
236359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        if (QPtmp != QP)
236459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
236559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            predptr = pred;
236659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            PREDICT_AC
236759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
236859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        else
236959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
237059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            predptr = DCAC;
237159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
237259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
237359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    else
237459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
237559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        predptr = pred;
237659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        pred[0] = pred[1] = pred[2] = pred[3] = pred[4] = pred[5] = pred[6] = 0;
237759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
237859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
237959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                else
238059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
238159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if ((Pos0[comp] && (slice_nb[mbnum] == slice_nb[mbnum-nMBPerRow]))
238259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            || comp  == 2 || comp == 3)
238359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
238459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        QPtmp = QPMB[mbnum+y_offset];
238559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        DCAC = DCAC_row[x_pos][b_xtab];
238659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        if (QPtmp != QP)
238759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
238859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            predptr = pred;
238959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            PREDICT_AC
239059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
239159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        else
239259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        {
239359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                            predptr = DCAC;
239459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        }
239559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
239659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    else
239759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    {
239859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        predptr = pred;
239959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                        pred[0] = pred[1] = pred[2] = pred[3] = pred[4] = pred[5] = pred[6] = 0;
240059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    }
240159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
240259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
240359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
240459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /************************************/
240559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /* Decide and Perform AC prediction */
240659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /************************************/
240759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            newCBP[comp] = 0;
240859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
240959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (direction[comp] == 0)   /* Horizontal, left COLUMN of block A */
241059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
241159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                DCAC = pcoeff + comp * 7; /* re-use DCAC as local var */
241259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                qcoeff += 8;
241359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                for (m = 0; m < 7; m++)
241459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
241559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    QPtmp = qcoeff[m<<3];
241659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if (QPtmp > 0)  S1 += QPtmp;
241759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    else        S1 -= QPtmp;
241859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    QPtmp -= predptr[m];
241959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    DCAC[m] = QPtmp; /* save prediction residue to pcoeff*/
242059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if (QPtmp)  newCBP[comp] = 1;
242159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    diff = PV_ABS(QPtmp);
242259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    S2 += diff;
242359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
242459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
242559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else            /* Vertical, top ROW of block C */
242659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
242759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                qcoeff++;
242859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                DCAC = pcoeff + comp * 7; /* re-use DCAC as local var */
242959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                for (m = 0; m < 7; m++)
243059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                {
243159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    QPtmp = qcoeff[m];
243259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if (QPtmp > 0)  S1 += QPtmp;
243359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    else        S1 -= QPtmp;
243459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    QPtmp -= predptr[m];
243559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    DCAC[m] = QPtmp; /* save prediction residue to pcoeff*/
243659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    if (QPtmp)  newCBP[comp] = 1;
243759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    diff = PV_ABS(QPtmp);
243859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                    S2 += diff;
243959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                }
244059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
244159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
244259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /****************************/
244359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /*  Store DCAC coefficients */
244459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /****************************/
244559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            /* Store coeff values for Intra MB */
244659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            qcoeff = MB->block[comp];
244759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC = DCAC_row[x_pos][b_xtab];
244859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC[0] = qcoeff[1];
244959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC[1] = qcoeff[2];
245059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC[2] = qcoeff[3];
245159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC[3] = qcoeff[4];
245259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC[4] = qcoeff[5];
245359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC[5] = qcoeff[6];
245459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC[6] = qcoeff[7];
245559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
245659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC = DCAC_col[0][b_ytab];
245759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC[0] = qcoeff[8];
245859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC[1] = qcoeff[16];
245959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC[2] = qcoeff[24];
246059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC[3] = qcoeff[32];
246159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC[4] = qcoeff[40];
246259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC[5] = qcoeff[48];
246359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC[6] = qcoeff[56];
246459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
246559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
246659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        } /* bACPredEnable */
246759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
246859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    } /* END COMP FOR LOOP */
246959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
247059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    //if (diff > 2047)
247159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    //    break;
247259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    S += (S1 - S2);
247359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
247459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
247559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (S >= 0 && bACPredEnable == TRUE)
247659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
247759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        ACpred_flag[mbnum] = 1;
247859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        DCAC = pcoeff; /* prediction residue */
247959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        qcoeff = MB->block[0];
248059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
248159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        for (comp = 0; comp < 6; comp++)
248259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
248359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (direction[comp] == 0)
248459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
248559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                qcoeff[8] = DCAC[0];
248659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                qcoeff[16] = DCAC[1];
248759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                qcoeff[24] = DCAC[2];
248859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                qcoeff[32] = DCAC[3];
248959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                qcoeff[40] = DCAC[4];
249059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                qcoeff[48] = DCAC[5];
249159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                qcoeff[56] = DCAC[6];
249259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
249359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
249459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            else
249559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            {
249659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                qcoeff[1] = DCAC[0];
249759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                qcoeff[2] = DCAC[1];
249859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                qcoeff[3] = DCAC[2];
249959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                qcoeff[4] = DCAC[3];
250059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                qcoeff[5] = DCAC[4];
250159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                qcoeff[6] = DCAC[5];
250259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                qcoeff[7] = DCAC[6];
250359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            }
250459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (newCBP[comp]) /* 5/28/01, update CBP */
250559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                video->headerInfo.CBP[mbnum] |= mask1[comp];
250659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            DCAC += 7;
250759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            qcoeff += 64;
250859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
250959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
251059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else  /* Only DC Prediction */
251159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
251259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        ACpred_flag[mbnum] = 0;
251359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
251459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
251559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    *intraDC_decision = IntraDCSwitch_Decision(Mode, video->currVop->intraDCVlcThr, intraDCVlcQP);
251659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (*intraDC_decision) /* code DC with AC , 5/28/01*/
251759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
251859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        qcoeff = MB->block[0];
251959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        for (comp = 0; comp < 6; comp++)
252059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        {
252159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            if (*qcoeff)
252259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                video->headerInfo.CBP[mbnum] |= mask1[comp];
252359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            qcoeff += 64;
252459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        }
252559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
252659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return;
252759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
252859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong#endif /* H263_ONLY */
252959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
253059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
253159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
253259f566c4ec3dfc097ad8163523e522280b27e5c3James DongVoid find_pmvs(VideoEncData *video, Int block, Int *mvx, Int *mvy)
253359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
253459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Vol *currVol = video->vol[video->currLayer];
253559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong//  UChar *Mode = video->headerInfo.Mode; /* modes for MBs */
253659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    UChar *slice_nb = video->sliceNo;
253759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int nMBPerRow = currVol->nMBPerRow;
253859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int mbnum = video->mbnum;
253959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
254059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int   p1x, p2x, p3x;
254159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int   p1y, p2y, p3y;
254259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int   xin1, xin2, xin3;
254359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int   yin1, yin2, yin3;
254459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int   vec1, vec2, vec3;
254559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int   rule1, rule2, rule3;
254659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    MOT   **motdata = video->mot;
254759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int   x = mbnum % nMBPerRow;
254859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int   y = mbnum / nMBPerRow;
254959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
255059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    /*
255159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        In a previous version, a MB vector (block = 0) was predicted the same way
255259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        as block 1, which is the most likely interpretation of the VM.
255359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
255459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        Therefore, if we have advanced pred. mode, and if all MBs around have
255559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        only one 16x16 vector each, we chose the appropiate block as if these
255659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        MBs have 4 vectors.
255759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
255859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        This different prediction affects only 16x16 vectors of MBs with
255959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        transparent blocks.
256059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
256159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        In the current version, we choose for the 16x16 mode the first
256259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        non-transparent block in the surrounding MBs
256359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    */
256459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
256559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    switch (block)
256659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
256759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        case 0:
256859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            vec1 = 2 ;
256959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            yin1 = y  ;
257059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            xin1 = x - 1;
257159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            vec2 = 3 ;
257259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            yin2 = y - 1;
257359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            xin2 = x;
257459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            vec3 = 3 ;
257559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            yin3 = y - 1;
257659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            xin3 = x + 1;
257759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            break;
257859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
257959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        case 1:
258059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            vec1 = 2 ;
258159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            yin1 = y  ;
258259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            xin1 = x - 1;
258359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            vec2 = 3 ;
258459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            yin2 = y - 1;
258559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            xin2 = x;
258659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            vec3 = 3 ;
258759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            yin3 = y - 1;
258859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            xin3 = x + 1;
258959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            break;
259059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
259159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        case 2:
259259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            vec1 = 1 ;
259359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            yin1 = y  ;
259459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            xin1 = x;
259559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            vec2 = 4 ;
259659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            yin2 = y - 1;
259759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            xin2 = x;
259859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            vec3 = 3 ;
259959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            yin3 = y - 1;
260059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            xin3 = x + 1;
260159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            break;
260259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
260359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        case 3:
260459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            vec1 = 4 ;
260559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            yin1 = y  ;
260659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            xin1 = x - 1;
260759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            vec2 = 1 ;
260859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            yin2 = y  ;
260959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            xin2 = x;
261059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            vec3 = 2 ;
261159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            yin3 = y  ;
261259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            xin3 = x;
261359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            break;
261459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
261559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        default: /* case 4 */
261659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            vec1 = 3 ;
261759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            yin1 = y  ;
261859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            xin1 = x;
261959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            vec2 = 1 ;
262059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            yin2 = y  ;
262159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            xin2 = x;
262259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            vec3 = 2 ;
262359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            yin3 = y  ;
262459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            xin3 = x;
262559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            break;
262659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
262759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
262859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (block == 0)
262959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
263059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* according to the motion encoding, we must choose a first non-transparent
263159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        block in the surrounding MBs (16-mode)
263259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            */
263359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
263459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (x > 0 && slice_nb[mbnum] == slice_nb[mbnum-1])
263559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rule1 = 0;
263659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
263759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rule1 = 1;
263859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
263959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (y > 0 && slice_nb[mbnum] == slice_nb[mbnum-nMBPerRow])
264059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rule2 = 0;
264159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
264259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rule2 = 1;
264359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
264459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if ((x != nMBPerRow - 1) && (y > 0) && slice_nb[mbnum] == slice_nb[mbnum+1-nMBPerRow])
264559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rule3 = 0;
264659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
264759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rule3 = 1;
264859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
264959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
265059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
265159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* check borders for single blocks (advanced mode) */
265259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* rule 1 */
265359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (((block == 1 || block == 3) &&
265459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                (x == 0 || slice_nb[mbnum] != slice_nb[mbnum-1])))
265559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rule1 = 1;
265659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
265759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rule1 = 0;
265859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
265959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* rule 2 */
266059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (((block == 1 || block == 2) &&
266159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                (y == 0 || slice_nb[mbnum] != slice_nb[mbnum-nMBPerRow])))
266259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rule2 = 1;
266359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
266459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rule2 = 0;
266559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
266659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* rule 3 */
266759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (((block == 1 || block == 2) &&
266859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong                (x == nMBPerRow - 1 || y == 0 || slice_nb[mbnum] != slice_nb[mbnum+1-nMBPerRow])))
266959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rule3 = 1;
267059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        else
267159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            rule3 = 0;
267259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
267359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
267459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (rule1)
267559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
267659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        p1x = p1y = 0;
267759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
267859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
267959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
268059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
268159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        p1x = motdata[yin1*nMBPerRow+xin1][vec1].x;
268259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        p1y = motdata[yin1*nMBPerRow+xin1][vec1].y;
268359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //p1x = motxdata[xin1*2+(vec1&0x1) + (yin1*2+(vec1>>1))*xB];
268459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //p1y = motydata[xin1*2+(vec1&0x1) + (yin1*2+(vec1>>1))*xB];
268559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
268659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
268759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (rule2)
268859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
268959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        p2x = p2y = 0;
269059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
269159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
269259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
269359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        p2x = motdata[yin2*nMBPerRow+xin2][vec2].x;
269459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        p2y = motdata[yin2*nMBPerRow+xin2][vec2].y;
269559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //p2x = motxdata[xin2*2+(vec2&0x1) + (yin2*2+(vec2>>1))*xB];
269659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //p2y = motydata[xin2*2+(vec2&0x1) + (yin2*2+(vec2>>1))*xB];
269759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
269859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
269959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (rule3)
270059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
270159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        p3x = p3y = 0;
270259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
270359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
270459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
270559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        p3x = motdata[yin3*nMBPerRow+xin3][vec3].x;
270659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        p3y = motdata[yin3*nMBPerRow+xin3][vec3].y;
270759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //p3x = motxdata[xin3*2+ (vec3&0x1) + (yin3*2+(vec3>>1))*xB];
270859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        //p3y = motydata[xin3*2+ (vec3&0x1) + (yin3*2+(vec3>>1))*xB];
270959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
271059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
271159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (rule1 && rule2 && rule3)
271259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
271359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* all MBs are outside the VOP */
271459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        *mvx = *mvy = 0;
271559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
271659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (rule1 + rule2 + rule3 == 2)
271759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
271859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        /* two of three are zero */
271959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        *mvx = (p1x + p2x + p3x);
272059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        *mvy = (p1y + p2y + p3y);
272159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
272259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
272359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
272459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        *mvx = ((p1x + p2x + p3x - PV_MAX(p1x, PV_MAX(p2x, p3x)) - PV_MIN(p1x, PV_MIN(p2x, p3x))));
272559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        *mvy = ((p1y + p2y + p3y - PV_MAX(p1y, PV_MAX(p2y, p3y)) - PV_MIN(p1y, PV_MIN(p2y, p3y))));
272659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
272759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
272859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return;
272959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
273059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
273159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
273259f566c4ec3dfc097ad8163523e522280b27e5c3James DongVoid WriteMVcomponent(Int f_code, Int dmv, BitstreamEncVideo *bs)
273359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
273459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int residual, vlc_code_mag, bits, entry;
273559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
273659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    ScaleMVD(f_code, dmv, &residual, &vlc_code_mag);
273759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
273859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (vlc_code_mag < 0)
273959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        entry = vlc_code_mag + 65;
274059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
274159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        entry = vlc_code_mag;
274259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
274359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    bits = PutMV(entry, bs);
274459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
274559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if ((f_code != 1) && (vlc_code_mag != 0))
274659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
274759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        BitstreamPutBits(bs, f_code - 1, residual);
274859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        bits += f_code - 1;
274959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
275059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    return;
275159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
275259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
275359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
275459f566c4ec3dfc097ad8163523e522280b27e5c3James DongVoid
275559f566c4ec3dfc097ad8163523e522280b27e5c3James DongScaleMVD(
275659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int  f_code,       /* <-- MV range in 1/2 units: 1=32,2=64,...,7=2048     */
275759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int  diff_vector,  /* <-- MV Difference commponent in 1/2 units           */
275859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int  *residual,    /* --> value to be FLC coded                           */
275959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int  *vlc_code_mag /* --> value to be VLC coded                           */
276059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong)
276159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong{
276259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int   range;
276359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int   scale_factor;
276459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int   r_size;
276559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int   low;
276659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int   high;
276759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    Int   aux;
276859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
276959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    r_size = f_code - 1;
277059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    scale_factor = 1 << r_size;
277159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    range = 32 * scale_factor;
277259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    low   = -range;
277359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    high  =  range - 1;
277459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
277559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (diff_vector < low)
277659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        diff_vector += 2 * range;
277759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (diff_vector > high)
277859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        diff_vector -= 2 * range;
277959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
278059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    if (diff_vector == 0)
278159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
278259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        *vlc_code_mag = 0;
278359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        *residual = 0;
278459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
278559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else if (scale_factor == 1)
278659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
278759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        *vlc_code_mag = diff_vector;
278859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        *residual = 0;
278959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
279059f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    else
279159f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    {
279259f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        aux = PV_ABS(diff_vector) + scale_factor - 1;
279359f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        *vlc_code_mag = aux >> r_size;
279459f566c4ec3dfc097ad8163523e522280b27e5c3James Dong
279559f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        if (diff_vector < 0)
279659f566c4ec3dfc097ad8163523e522280b27e5c3James Dong            *vlc_code_mag = -*vlc_code_mag;
279759f566c4ec3dfc097ad8163523e522280b27e5c3James Dong        *residual = aux & (scale_factor - 1);
279859f566c4ec3dfc097ad8163523e522280b27e5c3James Dong    }
279959f566c4ec3dfc097ad8163523e522280b27e5c3James Dong}
2800