www.pudn.com > wm2.5.zip > vlc.c


/* 
*********************************************************************** 
* COPYRIGHT AND WARRANTY INFORMATION 
* 
* Copyright 2004, Advanced Audio Video Coding Standard, Part II 
* 
* DISCLAIMER OF WARRANTY 
* 
* These software programs are available to the users without any 
* license fee or royalty on an "as is" basis. The AVS disclaims 
* any and all warranties, whether express, implied, or statutory, 
* including any implied warranties of merchantability or of fitness 
* for a particular purpose. In no event shall the contributors or  
* the AVS be liable for any incidental, punitive, or consequential 
* damages of any kind whatsoever arising from the use of this program. 
* 
* This disclaimer of warranty extends to the user of this program 
* and user's customers, employees, agents, transferees, successors, 
* and assigns. 
* 
* The AVS does not represent or warrant that the program furnished 
* hereunder are free of infringement of any third-party patents. 
* Commercial implementations of AVS, including shareware, may be 
* subject to royalty fees to patent holders. Information regarding 
* the AVS patent policy is available from the AVS Web site at 
* http://www.avs.org.cn 
* 
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE AVS PATENT POLICY. 
************************************************************************ 
*/ 
 
#include  
#include "types.h" 
#include "global.h" 
#include "math.h" 
#include "block_const.h" 
 
///////////////////////////////////////////////////////////////////////// 
extern int writeSyntaxElement_GOLOMB(int golomb_grad, int golomb_maxlevels, int symbol2D); 
 
int cofAC[6][4][2][18];        //[block num][subblknum][level/run][coeff count]    //qwang 2004-3-8 
int cofAC_B8[4][2][65]; 
//! array used to find expencive coefficients 
const Byte COEFF_COST[16] = 
{ 
  3,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0 
}; 
 
// dongjie 
extern const char VLC_INTRA[7][16][16]; 
extern const char VLC_INTER[7][16][14];  
extern const char VLC_CHROMA[4][16][13]; 
extern const char VLC_GC_Order_INTRA[7][2]; 
extern const char VLC_GC_Order_INTER[7][2]; 
extern const char VLC_GC_Order_CHROMA[4][2]; 
// end 
 
const Byte QP_RANGE[64] = 
{ 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 
  1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 
  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
  3, 3, 3, 3 
}; 
 
const Byte VLC_TABLE_INTRA[4] =  
{ 
  0, 1, 2, 3 
}; 
 
const Byte VLC_TABLE_INTER[4][7] =  
{ 
  {  2,  2,  2,  3,  3,  3,  0}, 
  {  6,  6,  6,  7,  8,  8,  1}, 
  {  6,  6,  6,  7,  8,  8,  4}, 
  {  6,  6,  6,  7,  8,  8,  5} 
}; 
const int AVS_SCAN[2][64][2] =  
{ 
	{ 
		{0,0},{0,1},{0,2},{1,0},{0,3},{0,4},{1,1},{1,2}, 
		{0,5},{0,6},{1,3},{2,0},{2,1},{0,7},{1,4},{2,2}, 
		{3,0},{1,5},{1,6},{2,3},{3,1},{3,2},{4,0},{1,7}, 
		{2,4},{4,1},{2,5},{3,3},{4,2},{2,6},{3,4},{4,3}, 
		{5,0},{5,1},{2,7},{3,5},{4,4},{5,2},{6,0},{5,3}, 
		{3,6},{4,5},{6,1},{6,2},{5,4},{3,7},{4,6},{6,3}, 
		{5,5},{4,7},{6,4},{5,6},{6,5},{5,7},{6,6},{7,0}, 
		{6,7},{7,1},{7,2},{7,3},{7,4},{7,5},{7,6},{7,7} 
	}, 
	{ 
		{ 0, 0}, { 1, 0}, { 0, 1}, { 0, 2}, { 1, 1}, { 2, 0}, { 3, 0}, { 2, 1}, 
		{ 1, 2}, { 0, 3}, { 0, 4}, { 1, 3}, { 2, 2}, { 3, 1}, { 4, 0}, { 5, 0}, 
		{ 4, 1}, { 3, 2}, { 2, 3}, { 1, 4}, { 0, 5}, { 0, 6}, { 1, 5}, { 2, 4}, 
		{ 3, 3}, { 4, 2}, { 5, 1}, { 6, 0}, { 7, 0}, { 6, 1}, { 5, 2}, { 4, 3}, 
		{ 3, 4}, { 2, 5}, { 1, 6}, { 0, 7}, { 1, 7}, { 2, 6}, { 3, 5}, { 4, 4}, 
		{ 5, 3}, { 6, 2}, { 7, 1}, { 7, 2}, { 6, 3}, { 5, 4}, { 4, 5}, { 3, 6}, 
		{ 2, 7}, { 3, 7}, { 4, 6}, { 5, 5}, { 6, 4}, { 7, 3}, { 7, 4}, { 6, 5}, 
		{ 5, 6}, { 4, 7}, { 5, 7}, { 6, 6}, { 7, 5}, { 7, 6}, { 6, 7}, { 7, 7} 
	} 
}; 
const char AVS_2DVLC_INTER[7][26][27] =  
{ 
  { 
	{ 0, 26, 40, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{ 2, 46, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{ 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{ 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{ 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{16, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{18, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{20, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{22, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{24, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{28, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{30, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{32, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{34, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{36, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{38, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{42, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{44, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{48, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{50, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{52, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{54, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{56, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
  }, 
  { 
	{ 2,  0, 13, 29, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1,  3, 23, 57, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1,  5, 35, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1,  7, 39, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1,  9, 43, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 11, 49, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 15, 55, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 19, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 21, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 27, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 33, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 37, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 41, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 45, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 53, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
  }, 
  { 
	{ 2,  0,  5, 11, 23, 35, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1,  3, 13, 27, 49, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1,  7, 21, 45, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1,  9, 29, 55, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 15, 37, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 17, 41, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 19, 53, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 33, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 39, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 43, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 57, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
  }, 
  { 
	{ 2,  0,  3,  7, 13, 17, 27, 35, 43, 55, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1,  5, 11, 21, 33, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1,  9, 23, 37, 57, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 15, 29, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 19, 41, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 25, 49, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 31, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 39, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 45, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 53, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
  }, 
  { 
	{ 2,  0,  3,  5,  9, 11, 17, 21, 25, 33, 41, 45, 55, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1,  7, 13, 19, 29, 35, 49, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 15, 27, 43, 57, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 23, 37, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 31, 53, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 39, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
  }, 
  { 
	{ 0,  1,  3,  5,  7,  9, 13, 15, 17, 21, 25, 29, 33, 39, 43, 49, 53, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 11, 19, 27, 31, 41, 45, 57, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 23, 37, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 35, 55, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 47, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
  }, 
  { 
	{ 0,  1,  3,  5,  7,  9, 11, 13, 17, 19, 21, 23, 25, 29, 33, 35, 39, 41, 43, 47, 49, 57, -1, -1, -1, -1, -1}, 
	{-1, 15, 27, 37, 45, 55, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 31, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, 53, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
	{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
  } 
}; 
const char VLC_Golomb_Order[2][3][7][2] =       //[interlace/frame][intra/inter/chroma][tablenum][grad/maxlevel] 
// maxlevel changed by dj 
{ 
	{  //interlace coding 
		{ 
			{2,9}, {2,9}, {1,9}, {2,9}, {2,9}, {3,9}, {-1,-1}, 
		}, 
		{ 
			{3,9}, {2,9}, {1,9}, {2,9}, {2,9}, {2,9}, {3,9}, 
		}, 
		{ 
			{2,9}, {2,9}, {1,9}, {1,9}, {2,9}, {-1,-1}, {-1,-1}, 
		}, 
	},  
	{  //frame coding 
		{ 
			{2,9}, {2,9}, {2,9}, {2,9}, {2,9}, {2,9}, {2,9}, 
		}, 
		{ 
			{3,9}, {2,9}, {2,9}, {2,9}, {2,9}, {2,9}, {2,9}, 
		}, 
		{ 
			{2,9}, {0,9}, {1,9}, {1,9}, {0,9}, {-1,-1}, {-1,-1}, 
		}, 
	} 
}; 
const char MaxRun[3][7] =  
{ 
	{22,14,9,6,4,2,1}, 
	{25,18,13,9,6,4,3}, 
	{24,19,10,7,4,-1,-1} 
}; 
const char RefAbsLevel[19][26] =  
{ 
	{ 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,-1,-1,-1}, 
	{ 7, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 
	{10, 6, 4, 4, 3, 3, 3, 2, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 
	{13, 7, 5, 4, 3, 2, 2,-1,-1,-1 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 
	{18, 8, 4, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 
	{22, 7, 3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 
	{27, 4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 
	{ 4, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, 
	{ 5, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,-1,-1,-1,-1,-1,-1,-1}, 
	{ 7, 5, 4, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 
	{10, 6, 5, 4, 3, 3, 2, 2, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 
	{13, 7, 5, 4, 3, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 
	{17, 8, 4, 3, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 
	{22, 6, 3, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 
	{ 5, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,-1}, 
	{ 6, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,-1,-1,-1,-1,-1,-1}, 
	{10, 6, 4, 4, 3, 3, 2, 2, 2, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 
	{14, 7, 4, 3, 3, 2, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 
	{20, 7, 3, 2, 2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} 
}; 
//#endif 
 
/* 
************************************************************************* 
* Function:u_v, writes a a n bit fixed length syntax element, returns the length in bits,  
* Input: 
tracestring 
the string for the trace file 
value 
the value to be coded 
bitstream 
the Bitstream the value should be coded into 
* Output: 
* Return: Number of bits used by the coded syntax element  
* Attention:This function writes always the bit buffer for the progressive scan flag, and 
should not be used (or should be modified appropriately) for the interlace crap 
When used in the context of the Parameter Sets, this is obviously not a 
problem. 
************************************************************************* 
*/ 
int u_v (int n, char *tracestring, int value) 
{ 
	int i; 
	UInt mask = 1 << (n-1); 
	 
	// Add the new bits to the bitstream. 
	// Write out a byte if it is full 
	for (i=0; ibyte_buf <<= 1; 
		if (value & mask) 
			pgcurrBitStream->byte_buf |= 1; 
		pgcurrBitStream->bits_to_go--; 
		mask >>= 1; 
		if (pgcurrBitStream->bits_to_go==0) 
		{ 
			pgcurrBitStream->bits_to_go = 8; 
			pgcurrBitStream->streamBuffer[pgcurrBitStream->byte_pos++]=pgcurrBitStream->byte_buf; 
			pgcurrBitStream->byte_buf = 0; 
		} 
	}	 
 
	return n; 
} 
 
/* 
************************************************************************* 
* Function:writes a glombal syntax element, returns the length in bits,  
* Input: 
* Output: 
* Return:   
* Attention: 
************************************************************************* 
*/ 
int ue_linfo(int value1) 
{ 
  int i,nn; 
  int len,info; 
  unsigned int bitpattern;			   
  int suffix_len; 
  unsigned int mask; 
   
  nn=(value1+1)/2; 
   
  for (i=0; i < 16 && nn != 0; i++) 
  { 
    nn /= 2; 
  } 
   
  len= 2*i + 1; 
  info=value1+1-(int)pow(2,i); 
   
  suffix_len= len/2; 
  mask = 1 << (len-1); 
  bitpattern = (1<byte_buf <<= 1; 
      if (bitpattern & mask) 
        pgcurrBitStream->byte_buf |= 1; 
      pgcurrBitStream->bits_to_go--; 
      mask >>= 1; 
      if (pgcurrBitStream->bits_to_go==0) 
      { 
        pgcurrBitStream->bits_to_go = 8; 
        pgcurrBitStream->streamBuffer[pgcurrBitStream->byte_pos++]=pgcurrBitStream->byte_buf; 
        pgcurrBitStream->byte_buf = 0; 
      } 
  }				   
  return len; 
} 
 
/* 
************************************************************************* 
* Function:generates code and passes the codeword to the buffer 
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
int write_Intra4x4PredictionMode(int IntraPredMode)   //qwang 2004-3-8 
{ 
	int len,info; 
	int i; 
	unsigned int mask; 
  
 
	if (IntraPredMode == -1) 
	{ 
		len = 1; 
//		info = 1;	mismatch with FCD  added by MAZHAN 
		info = 0; 
 
	} 
	else  
	{ 
		//len = 3;   
		len = 4;	// for intra pre 9 modes add by jx 
		//info = IntraPredMode;     mismatch with FCD   added by MAZHAN 
		info = IntraPredMode+8; 
	}  
   
	mask = 1 << (len-1); 
////////////////////////////////////////////////////////////////////////// 
 
	  // Add the new bits to the bitstream. 
	  // Write out a byte if it is full 
	  for (i=0; ibyte_buf <<= 1; 
		  if (info & mask) 
			  pgcurrBitStream->byte_buf |= 1; 
		  pgcurrBitStream->bits_to_go--; 
		  mask >>= 1; 
		  if (pgcurrBitStream->bits_to_go==0) 
		  { 
			  pgcurrBitStream->bits_to_go = 8; 
			  pgcurrBitStream->streamBuffer[pgcurrBitStream->byte_pos++]=pgcurrBitStream->byte_buf; 
			  pgcurrBitStream->byte_buf = 0; 
		  } 
	  } 
   
  return (len); 
} 
 
/* 
************************************************************************* 
* Function:generates code and passes the codeword to the buffer 
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
void se_linfo(int se) 
{	 
	int i,n,sign,nn; 
	int len,info; 
	 
	sign=0;	 
	if (se <= 0) 
	{ 
		sign=1; 
	} 
	n=abs(se) << 1; 
	 
	//n+1 is the number in the code table.  Based on this we find length and info 
	 
	nn=n/2; 
	for (i=0; i < 16 && nn != 0; i++) 
	{ 
		nn /= 2; 
	} 
	len=i*2 + 1; 
	info=n - (int)pow(2,i) + sign; 
	{ 
		int suffix_len=len/2; 
		unsigned int bitpattern= (1<byte_buf <<= 1; 
			if (bitpattern & mask) 
				pgcurrBitStream->byte_buf |= 1; 
			pgcurrBitStream->bits_to_go--; 
			mask >>= 1; 
			if (pgcurrBitStream->bits_to_go==0) 
			{ 
				pgcurrBitStream->bits_to_go = 8; 
				pgcurrBitStream->streamBuffer[pgcurrBitStream->byte_pos++]=pgcurrBitStream->byte_buf; 
				pgcurrBitStream->byte_buf = 0; 
			} 
		}		 
	} 
} 
 
////////////////////////////////golomb////////////////////////////////////////// 
/* 
************************************************************************* 
* Function: 
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
void encode_hybridgolomb_word(unsigned int symbol,unsigned int grad0,unsigned int max_levels,unsigned int *res_bits,unsigned int *res_len) 
{ 
  unsigned int level,res,numbits, numcodes_level; 
  int first_layer; 
   
  res=1UL<=numcodes_level && level=max_levels) 
  { 
    printf("error in max_golomb_levels.\n"); 
    exit(0); 
  } 
 
  if(level>=2 && first_layer==0) 
    symbol += ( (1<<(grad0+1)) * ((1<<(level-1))-1) ); 
 
  //set data bits 
  *res_bits=res|symbol; 
  *res_len=numbits; 
} 
 
/* 
************************************************************************* 
* Function:generates code and passes the codeword to the buffer 
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
void encode_golomb_word(unsigned int symbol,unsigned int grad0,unsigned int max_levels,unsigned int *res_bits,unsigned int *res_len) 
{ 
  unsigned int level,res,numbits; 
   
  res=1UL<=res && level=max_levels) 
  { 
    if(symbol>=res) 
      symbol=res-1UL;  //crop if too large. 
  } 
 
  //set data bits 
  *res_bits=res|symbol; 
  *res_len=numbits; 
} 
 
/* 
************************************************************************* 
* Function: 
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
 
void encode_multilayer_golomb_word(unsigned int symbol,const unsigned int *grad,const unsigned int *max_levels,unsigned int *res_bits,unsigned int *res_len) 
{ 
  unsigned accbits,acclen,bits,len,tmp; 
 
  accbits=acclen=0UL; 
   
  while(1) 
  { 
    encode_golomb_word(symbol,*grad,*max_levels,&bits,&len); 
    accbits=(accbits<byte_buf <<= 1; 
	  if (bits & mask) 
		  pgcurrBitStream->byte_buf |= 1; 
	  pgcurrBitStream->bits_to_go--; 
	  mask >>= 1; 
	  if (pgcurrBitStream->bits_to_go==0) 
	  { 
		  pgcurrBitStream->bits_to_go = 8; 
		  pgcurrBitStream->streamBuffer[pgcurrBitStream->byte_pos++]=pgcurrBitStream->byte_buf; 
		  pgcurrBitStream->byte_buf = 0; 
	  } 
  } 
 
  return (len); 
} 
 
 
/* 
************************************************************************* 
* Function: 
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
int write_GOLOMB(int golomb_maxlevels,int golomb_grad,int value1) 
{ 
 unsigned int bits,len,i; 
 unsigned int grad[4],max_lev[4]; 
 unsigned int mask; 
 
  if(!( golomb_maxlevels&~0xFF ))    //only bits 0-7 used? This means normal Golomb word. 
    encode_golomb_word(value1,golomb_grad,golomb_maxlevels,&bits,&len); 
  else 
  { 
    for(i=0UL;i<4UL;i++) 
    { 
      grad[i]=(golomb_grad>>(i<<3))&0xFFUL; 
      max_lev[i]=(golomb_maxlevels>>(i<<3))&0xFFUL; 
    } 
    encode_multilayer_golomb_word(value1,grad,max_lev,&bits,&len); 
  }  
   
  mask = 1 << (len-1); 
 
  // Add the new bits to the bitstream. 
  // Write out a byte if it is full 
  for (i=0; ibyte_buf <<= 1; 
	  if (bits & mask) 
		  pgcurrBitStream->byte_buf |= 1; 
	  pgcurrBitStream->bits_to_go--; 
	  mask >>= 1; 
	  if (pgcurrBitStream->bits_to_go==0) 
	  { 
		  pgcurrBitStream->bits_to_go = 8; 
		  pgcurrBitStream->streamBuffer[pgcurrBitStream->byte_pos++]=pgcurrBitStream->byte_buf; 
		  pgcurrBitStream->byte_buf = 0; 
	  } 
  } 
 
  return (len); 
} 
 
/* 
************************************************************************* 
* Function: 
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
int outlevrun_symbol2D_chroma(int level, int run, int tableindex/* dongjie */)      //qwang 2004-4-27 
{ 
  const Byte LEVRUN_CHROMA[4][16]= // dongjie 
  { 
    { 6, 2, 2, 2, 1, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, 
    { 7, 3, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, 
    { 9, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {12, 6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 
  }; 
 
  int levabs,n,sign; 
 
  if (level < 0) 
    sign=1; 
  else 
    sign=0; 
 
  levabs=abs(level); 
 
  n=(levabs-LEVRUN_CHROMA[tableindex][run])*32 + 8 + run*2; // dongjie 
 
  //if(sign==0) 
	if(sign == 1) 
    n--; 
 
  return n; 
} 
 
// added by PSL 20041228 
int outrun_symbol1D_chroma(int level, int run, int tableindex/* dongjie */)      //qwang 2004-4-27 
{ 
  const Byte LEVRUN_CHROMA[4][16]= // dongjie 
  { 
    { 6, 2, 2, 2, 1, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, 
    { 7, 3, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, 
    { 9, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {12, 6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 
  }; 
 
  int levabs,n,sign; 
 
  if (level < 0) 
    sign=1; 
  else 
    sign=0; 
 
  levabs=abs(level); 
 
  n= 40 + run*2; // dongjie 
 
  //if(sign==0) 
  if(sign == 1) 
    n--; 
 
  return n; 
} 
// added by PSL 20041228 
int outrun_symbol1D_chroma_2(int level, int run, int tableindex/* dongjie */)      //qwang 2004-4-27 
{ 
  const Byte LEVRUN_CHROMA[4][16]= // dongjie 
  { 
    { 6, 2, 2, 2, 1, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, 
    { 7, 3, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, 
    { 9, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {12, 6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 
  }; 
 
  int n; 
 
  n=  run; // dongjie 
 
  return n; 
} 
// added by PSL 20041228 
int find_abs_level_diff_chroma(int level, int run, int tableindex, int* sign) 
{ 
  const Byte LEVRUN_CHROMA[4][16]= // dongjie 
  { 
    { 6, 2, 2, 2, 1, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, 
    { 7, 3, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, 
    { 9, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {12, 6, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 
  }; 
 
  int levabs,n; 
 
  if (level < 0) 
    *sign=1; 
  else 
    *sign=0; 
 
  levabs=abs(level); 
 
  n=(levabs-LEVRUN_CHROMA[tableindex][run]); // dongjie 
 
 
  return n; 
} 
// added by PSL 20041228 
int outlev_symbol1D_chroma_2(int level, int run, int tableindex, int sign) 
{ 
	int n; 
 
    n= 40+(level)*2 +32; 
 
	//if(sign==0) 
	if(sign == 1) 
		n--; 
 
  return n; 
} 
 
 
/* 
************************************************************************* 
* Function: 
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
int outlevrun_symbol2D(int level, int run, int intra, int tableindex) 
{ 
  const Byte LEVRUN_INTRA[7][16]= // dongjie 
  { 
    { 4, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, 
    { 6, 4, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 6, 4, 4, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 8, 5, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {10, 5, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {12, 5, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {15, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 
  }; 
	 
  const Byte LEVRUN_INTER[7][16]= // dongjie 
  { 
    { 4, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, 
    { 5, 3, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, 
    { 5, 3, 3, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 6, 4, 3, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 7, 4, 3, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 9, 5, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {13, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 
  }; 
 
  int levabs,n,sign; 
 
  if (level < 0) 
    sign=1; 
  else 
    sign=0; 
 
  levabs=abs(level); 
 
  if(intra) 
    n=(levabs-LEVRUN_INTRA[tableindex][run])*32 + 8 + run*2; 
  else 
    n=(levabs-LEVRUN_INTER[tableindex][run])*32 + 8 + run*2; 
 
  //if(sign==0) 
	if(sign == 1) 
    n--; 
 
  return n; 
} 
 
// added by PSL 20041228 
int outrun_symbol1D(int level, int run, int intra, int tableindex) 
{ 
  const Byte LEVRUN_INTRA[7][16]= // dongjie 
  { 
    { 4, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, 
    { 6, 4, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 6, 4, 4, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 8, 5, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {10, 5, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {12, 5, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {15, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 
  }; 
	 
  const Byte LEVRUN_INTER[7][16]= // dongjie 
  { 
    { 4, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, 
    { 5, 3, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, 
    { 5, 3, 3, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 6, 4, 3, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 7, 4, 3, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 9, 5, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {13, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 
  }; 
 
  int n; 
  int sign; 
 
  if(level<0) 
	  sign=1; 
  else 
	  sign=0; 
 
  n= run*2 +40; 
  
//if(sign==0) 
	if(sign == 1) 
    n--; 
 
  return n; 
} 
// added by PSL 20041228 
int outlev_symbol1D_2(int level, int run, int intra, int tableindex, int sign) 
{ 
 
	int n; 
 
    n= 40+(level)*2 +32; 
	//if(sign==0) 
	if(sign == 1) 
		n--; 
 
  return n; 
} 
 
// added by PSL 20041228 
int outrun_symbol1D_2(int level, int run, int intra, int tableindex) 
{ 
  const Byte LEVRUN_INTRA[7][16]= // dongjie 
  { 
    { 4, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, 
    { 6, 4, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 6, 4, 4, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 8, 5, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {10, 5, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {12, 5, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {15, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 
  }; 
	 
  const Byte LEVRUN_INTER[7][16]= // dongjie 
  { 
    { 4, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, 
    { 5, 3, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, 
    { 5, 3, 3, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 6, 4, 3, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 7, 4, 3, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 9, 5, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {13, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 
  }; 
 
  int n; 
 
  n= run; 
  
  return n; 
} 
// added by PSL 20041228 
int find_abs_level_diff(int level, int run, int intra, int tableindex, int* sign) 
{ 
  const Byte LEVRUN_INTRA[7][16]= // dongjie 
  { 
    { 4, 3, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, 
    { 6, 4, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 6, 4, 4, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 8, 5, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {10, 5, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {12, 5, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {15, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 
  }; 
	 
  const Byte LEVRUN_INTER[7][16]= // dongjie 
  { 
    { 4, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0}, 
    { 5, 3, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, 
    { 5, 3, 3, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 6, 4, 3, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 7, 4, 3, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    { 9, 5, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
    {13, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 
  }; 
 
  int levabs,n; 
 
  if (level < 0) 
    *sign=1; 
  else 
    *sign=0; 
 
  levabs=abs(level); 
  if(intra) 
    n= (levabs-LEVRUN_INTRA[tableindex][run]); 
  else 
    n= (levabs-LEVRUN_INTER[tableindex][run]); 
 
  return n; 
} 
 
/* 
************************************************************************* 
* Function: write luma coeff of a 4x4 block 
* Input:  
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
int writeLumaCoeffAVS_B4_UVLC(int b8, int b4, int intra) 
{ 
  int run, level; 
  int* ACLevel; 
  int* ACRun; 
  int symbol2D; 
  int icoef, abslevel; 
  int tableindex; 
  int golomb_grab, golomb_maxlevels; 
	int incLevel_intra[7]={0,1,2,3,4,6,3000},incRun_intra[7]={-1,3,17,17,17,17,17}; 
	int incLevel_inter[7]={0,1,1,2,3,5,3000},incRun_inter[7]={-1,3,6,17,17,17,17}; 
	int level1_inter[16]={1,1,1,1,2,2,2,3,3,3,3,3,3,3,3,3}; 
	int level1_intra[16]={1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2}; 
	 
  ACLevel = cofAC[b8][b4][0]; 
  ACRun   = cofAC[b8][b4][1]; 
	 
	for(icoef=0;icoef<17;icoef++) //count coeffs 
		if(ACLevel[icoef]==0) //meet eob 
			break; 
	tableindex = 0; 
  if(intra) 
	{ 
	  int previousRunCnt = 0; // added by PSL 20041228 
	  level = 1; // get inside loop 
		for(icoef; icoef>=0; icoef--) 
		{ 
			if(icoef == 0)   //EOB 
			{ 
				level = 0; 
				run   = 0; 
				// added by PSL 20041228 
				if (previousRunCnt==16) 
					break; 
			} 
			else 
			{ 
				level = ACLevel[icoef-1]; 
				run   = ACRun[icoef-1]; 
			}  
			// added by PSL 20041228 
			previousRunCnt += run+1; 
			symbol2D=CODE2D_ESCAPE_SYMBOL;  
			if(level>-16 && level<16) 
			{ 
				if(tableindex == 0) 
					symbol2D = VLC_INTRA[tableindex][run][abs(level)-1];  
				else 
					symbol2D = VLC_INTRA[tableindex][run][abs(level)];    
				if(symbol2D >= 0 && level < 0) 
					symbol2D++; 
				if(symbol2D < 0) 
					symbol2D = CODE2D_ESCAPE_SYMBOL;  
			} 
			golomb_grab = VLC_GC_Order_INTRA[tableindex][0];         
			golomb_maxlevels = VLC_GC_Order_INTRA[tableindex][1]; 
			 
			if(symbol2D < CODE2D_ESCAPE_SYMBOL) 
			{ 
				write_GOLOMB(golomb_maxlevels, golomb_grab, symbol2D); 
			} 
			else 
			{ 
				// commented by PSL 20041228 
				//symbol2D = outlevrun_symbol2D(level, run, intra, tableindex); 
				//write_GOLOMB(golomb_maxlevels, golomb_grab, symbol2D); 
				// added by PSL 20041228 
				int sign; 
				int abslevel_diff; 
				 
				abslevel_diff= find_abs_level_diff(level, run, intra, tableindex, &sign); 
				 
				if(abslevel_diff>1) 
				{ 
					symbol2D = outlev_symbol1D_2(abslevel_diff-2, run, intra, tableindex, sign); 
					write_GOLOMB(golomb_maxlevels, golomb_grab, symbol2D); 
					symbol2D = outrun_symbol1D_2(level, run, intra, tableindex); 
					write_GOLOMB(golomb_maxlevels, 0, symbol2D); 
				} 
				else 
				{ 
					symbol2D = outrun_symbol1D(level, run, intra, tableindex); 
					write_GOLOMB(golomb_maxlevels, golomb_grab, symbol2D); 
				}; 
				 
			} 
			abslevel = abs(level); 
			if(abslevel>=incLevel_intra[tableindex]) 
			{ 
				if(abslevel == 1) 
					tableindex = (run>incRun_intra[tableindex])?level1_intra[run]:tableindex; 
				else if(abslevel < 4) 
                    tableindex = (abslevel+1); 
				else if(abslevel < 6) 
					tableindex = 5; 
				else 
					tableindex = 6; 
			} 
		} 
	} 
	else 
	{ 
		int previousRunCnt = 0; // added by PSL 20041228 
		level = 1; // get inside loop 
		for(icoef; icoef>=0; icoef--) 
		{ 
			if(icoef == 0)   //EOB 
			{ 
				level = 0; 
				run   = 0; 
				// added by PSL 20041228 
				if (previousRunCnt==16) 
					break; 
			} 
			else 
			{ 
				level = ACLevel[icoef-1]; 
				run   = ACRun[icoef-1]; 
			}  
			// added by PSL 20041228 
			previousRunCnt += run+1; 
			symbol2D=CODE2D_ESCAPE_SYMBOL;  
			if(level>-14 && level<14) 
			{ 
				if(tableindex == 0) 
					symbol2D = VLC_INTER[tableindex][run][abs(level)-1];  
				else 
					symbol2D = VLC_INTER[tableindex][run][abs(level)];    
				if(symbol2D >= 0 && level < 0) 
					symbol2D++; 
				if(symbol2D < 0) 
					symbol2D = CODE2D_ESCAPE_SYMBOL;  
			} 
			golomb_grab = VLC_GC_Order_INTER[tableindex][0];         
			golomb_maxlevels = VLC_GC_Order_INTER[tableindex][1]; 
 
			if(symbol2D < CODE2D_ESCAPE_SYMBOL) 
			{ 
				write_GOLOMB(golomb_maxlevels, golomb_grab, symbol2D); 
			} 
			else 
			{	 
				// commented by PSL 20041228 
				//symbol2D = outlevrun_symbol2D(level, run, intra, tableindex); 
				//write_GOLOMB(golomb_maxlevels, golomb_grab, symbol2D); 
				// added by PSL 20041228 
				int sign; 
				int abslevel_diff; 
				 
				abslevel_diff = find_abs_level_diff(level, run, intra, tableindex, &sign); 
							 
				if(abslevel_diff>1) 
				{ 
					symbol2D = outlev_symbol1D_2(abslevel_diff-2, run, intra, tableindex, sign); 
					write_GOLOMB(golomb_maxlevels, golomb_grab, symbol2D); 
					symbol2D = outrun_symbol1D_2(level, run, intra, tableindex); 
					write_GOLOMB(golomb_maxlevels, 0, symbol2D); 
				} 
				else 
				{ 
					symbol2D = outrun_symbol1D(level, run, intra, tableindex); 
					write_GOLOMB(golomb_maxlevels, golomb_grab, symbol2D); 
				} 
			} 
 
			abslevel = abs(level); 
			if(abslevel>=incLevel_inter[tableindex]) 
			{ 
				if(abslevel == 1) 
					tableindex = (run>incRun_inter[tableindex])?level1_inter[run]:tableindex; 
				else if(abslevel == 2) 
					tableindex = 4; 
				else if(abslevel <= 4) 
					tableindex = 5; 
				else 
					tableindex = 6; 
			} 
		} 
	} 
	return 0; 
} 
// end 
/* 
************************************************************************* 
* Function:Write chroma coefficients of one 4x4 block 
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
 
// dongjie 
int writeChromaCoeff_B4_UVLC(int b8, int b4)    //qwang 2004-4-27 
{ 
  int run, level; 
  int* ACLevel; 
  int* ACRun; 
  int symbol2D; 
  int tableindex,icoef; 
  int golomb_grab, golomb_maxlevels, abslevel; 
  int previousRunCnt = 0; // added by PSL 20041228 
  int incLevel_chroma[4]={0,1,2,3000}; 
	 
  ACLevel = cofAC[b8][b4][0]; 
  ACRun   = cofAC[b8][b4][1]; 
	for(icoef=0;icoef<17;icoef++) //count coeffs 
		if(ACLevel[icoef]==0) //meet eob 
			break; 
		 
		tableindex = 0; 
		for(icoef; icoef>=0; icoef--) 
		{ 
			if(icoef == 0)   //EOB 
			{ 
				level = 0; 
				run   = 0; 
				// added by PSL 20041228 
		    	if (previousRunCnt==16) 
				break; 
			} 
			else 
			{ 
				level = ACLevel[icoef-1]; 
				run   = ACRun[icoef-1]; 
			}   
				// added by PSL 20041228 
	    	previousRunCnt += run+1; 
			symbol2D=CODE2D_ESCAPE_SYMBOL;  
			if(level>-13 && level<13) 
			{ 
				if(tableindex == 0) 
					symbol2D = VLC_CHROMA[tableindex][run][abs(level)-1];  
				else 
					symbol2D = VLC_CHROMA[tableindex][run][abs(level)];    
				if(symbol2D >= 0 && level < 0) 
					symbol2D++; 
				if(symbol2D < 0) 
					symbol2D = CODE2D_ESCAPE_SYMBOL;  
			} 
			golomb_grab = VLC_GC_Order_CHROMA[tableindex][0];         
			golomb_maxlevels = VLC_GC_Order_CHROMA[tableindex][1]; 
			 
			if(symbol2D < CODE2D_ESCAPE_SYMBOL) 
			{ 
				write_GOLOMB(golomb_maxlevels, golomb_grab, symbol2D); 
			} 
			else 
			{ 
				// commented by PSL 20041228 
				//symbol2D = outlevrun_symbol2D_chroma(level, run, tableindex); 
				//write_GOLOMB(golomb_maxlevels, golomb_grab, symbol2D); 
				// added by PSL 20041228 
				int sign; 
				int abslevel_diff; 
				 
				abslevel_diff = find_abs_level_diff_chroma(level, run, tableindex, &sign); 
				 
				if(abslevel_diff>1) 
				{ 
					symbol2D = outlev_symbol1D_chroma_2(abslevel_diff-2, run,  tableindex, sign); 
					write_GOLOMB(golomb_maxlevels, golomb_grab, symbol2D); 
					symbol2D = outrun_symbol1D_chroma_2(level, run,  tableindex); 
					write_GOLOMB(golomb_maxlevels, 0, symbol2D); 
				} 
				else 
				{ 
					symbol2D = outrun_symbol1D_chroma(level, run,  tableindex); 
					write_GOLOMB(golomb_maxlevels, golomb_grab, symbol2D); 
				} 
			} 
			abslevel = abs(level); 
			if(abslevel>incLevel_chroma[tableindex]) 
			{ 
				if(abslevel<3) 
					tableindex = abslevel; 
				else 
					tableindex = 3; 
			} 
		} 
		return 0; 
} 
 // end 
/* 
************************************************************************* 
* Function: 
scan a 8x8 block and return coeff cost, and prepare for CBP  
calculation. 
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************* 
*/ 
int zigzag_scan_B4(int b8, int b4, int* coeff_cost)         //qwang 2004-4-23 
{ 
	int* ACLevel; 
	int* ACRun; 
	int  icoef, ipos; 
	int  xx, yy; 
	int  run; 
 
	ACLevel  = cofAC[b8][b4][0]; 
	ACRun    = cofAC[b8][b4][1]; 
	for (xx=0; xx<18; xx++) 
		ACRun[xx] = ACLevel[xx] = 0; 
 
	run  = -1; 
	ipos = 0; 
   
	for (icoef=0; icoef<16; icoef++) 
	{ 
		run++; 
     
		xx = ZIGZAG_SCAN[icoef][0]; 
		yy = ZIGZAG_SCAN[icoef][1]; 
     
		if (pgMbHeader->pred_residual[b8][b4][yy][xx] != 0) 
		{ 
			ACLevel[ipos] = pgMbHeader->pred_residual[b8][b4][yy][xx]; 
			ACRun[ipos]   = run; 
       
			//if (ACLevel[ipos] > 1) 
			if (abs(ACLevel[ipos]) > 1)//WJP 
			{ 
				*coeff_cost += MAX_VALUE;                // set high cost, shall not be discarded 
			  coeff_cost_4x4[b4] += MAX_VALUE; // dongjie 
			} 
			else 
			{ 
				*coeff_cost += COEFF_COST[run]; 
			  coeff_cost_4x4[b4] += COEFF_COST[run]; // dongjie 
			} 
       
			run = -1; 
			ipos++; 
		} 
	} 
   
	return ipos; 
} 
 
 
/* 
************************************************************************ 
* Function: scan a 8x8 block and return coeff cost, and prepare for CBP  
  calculation. 
* Input: 
* Output: 
* Return:  
* Attention: 
************************************************************************ 
*/ 
int abt_zigzag_B8(int b8, int* coeff_cost) 
{ 
	int* ACLevel; 
	int* ACRun; 
	int  curr_val,curr_blk[8][8]; 
	int  icoef, ipos; 
	int  xx, yy; 
	int  run; 
	int  b4; 
	const int AVS_COEFF_COST[64] =    // qhg for abt 
	{ 
		// 8 x 8 
		3,3,3,3,2,2,2,2,2,2,2,2,1,1,1,1, 
		1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0, 
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 
		0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0			 
	}; 
	ACLevel  = cofAC_B8[b8][0]; 
	ACRun    = cofAC_B8[b8][1]; 
	for (xx=0; xx<65; xx++) 
		ACRun[xx] = ACLevel[xx] = 0; 
	for(yy=0; yy<8; yy++) 
	for(xx=0; xx<8; xx++) 
	{ 
		b4=((yy>>2)<<1)+(xx>>2); 
		curr_blk[yy][xx] = pgMbHeader->pred_residual[b8][b4][yy&3][xx&3]; 
	} 
	run  = -1; 
	ipos = 0; 
	 
	for (icoef=0; icoef<64; icoef++) 
	{ 
		run++; 
		xx = AVS_SCAN[1][icoef][0]; 
		yy = AVS_SCAN[1][icoef][1]; 
		 
		curr_val = curr_blk[yy][xx]; 
		 
		if (curr_val != 0) 
		{ 
			ACLevel[ipos] = curr_val; 
			ACRun[ipos]   = run; 
			 
			if (absm(ACLevel[ipos])==1) 
				*coeff_cost += AVS_COEFF_COST[run]; 
			else 
				*coeff_cost += MAX_VALUE;  
			run = -1; 
			ipos++; 
		}	      
	} 
	return ipos; 
} 
/************************************************************************* 
* Function: write luma coeff of a 8x8 block 
* Input:  
* Output: 
* Return:  
* Attention: 
*************************************************************************/ 
int writeLumaCoeffAVS_B8(int b8)   
{ 
	int no_bits        = 0; 
	int icoef;                      /* current coefficient */ 
	int ipos;                       /* current position in cof_AVS */ 
	int run, level; 
	int* ACLevel; 
	int* ACRun;	 
	int symbol2D;                       
	int tablenum;    
	static const int incVlc_inter[2][7] = { {0,1,2,3,5,8,3000}, {0,1,2,3,6,9,3000} };   //qwang 11.29 
	int golomb_grad, golomb_maxlevels; 
	if (pgMbHeader->cbp & (1<=0; icoef--) 
			{ 
				if(icoef == 0)   //EOB 
				{ 
					level = 0; 
					run   = 0; 
				} 
				else 
				{ 
					level = ACLevel[icoef-1]; 
					run   = ACRun[icoef-1]; 
				}         
				symbol2D=ABTCODE2D_ESCAPE_SYMBOL;        //symbol for out-of-table 
				if(level>-27 && level<27 && run<26) 
				{ 
					if(tablenum == 0) 
						symbol2D = AVS_2DVLC_INTER[tablenum][run][abs(level)-1];     //qwang 11.29 
					else 
						symbol2D = AVS_2DVLC_INTER[tablenum][run][abs(level)];       //qwang 11.29 
					if(symbol2D >= 0 && level < 0) 
						symbol2D++; 
					if(symbol2D < 0) 
						symbol2D = (ABTCODE2D_ESCAPE_SYMBOL + (run<<1) + ((level>0)?0:1)); // added by dj 
				} 
				else 
				{ 
					symbol2D = (ABTCODE2D_ESCAPE_SYMBOL + (run<<1) + ((level>0)?0:1)); 
				} 
				golomb_grad = VLC_Golomb_Order[1][1][tablenum][0];         //qwang 11.29 
				golomb_maxlevels = VLC_Golomb_Order[1][1][tablenum][1];    //qwang 11.29 
				writeSyntaxElement_GOLOMB(golomb_grad, golomb_maxlevels, symbol2D);         
				if(icoef == 0) break;         
				if(symbol2D>=ABTCODE2D_ESCAPE_SYMBOL)    
				{ 
					golomb_grad = 0;   
					golomb_maxlevels = 11;           
					writeSyntaxElement_GOLOMB(golomb_grad, golomb_maxlevels, abs(level)-((run>MaxRun[1][tablenum])?1:RefAbsLevel[tablenum+7][run])); 
				} 
				if(abs(level) > incVlc_inter[1][tablenum])   //qwang 11.29 
				{ 
					if(abs(level) <= 3) 
						tablenum = abs(level); 
					else if(abs(level) <= 6) 
						tablenum = 4; 
					else if(abs(level) <= 9)  
						tablenum = 5; 
					else 
						tablenum = 6; 
				} 
			} 
	}//if ( cbp & (1<>(i<<3))&0xFFUL; 
			max_lev[i]=(golomb_maxlevels>>(i<<3))&0xFFUL; 
		} 
		encode_multilayer_golomb_word(symbol2D,grad,max_lev,&bits,&len); 
	}	 
	mask = 1 << (len-1); 
	for (i=0; ibyte_buf <<= 1; 
		if (bits & mask) 
			pgcurrBitStream->byte_buf |= 1; 
		pgcurrBitStream->bits_to_go--; 
		mask >>= 1; 
		if (pgcurrBitStream->bits_to_go==0) 
		{ 
			pgcurrBitStream->bits_to_go = 8; 
			pgcurrBitStream->streamBuffer[pgcurrBitStream->byte_pos++]=pgcurrBitStream->byte_buf; 
			pgcurrBitStream->byte_buf = 0; 
		} 
	}	 
	return (len); 
} 
//#endif