⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vlc.cpp

📁 H.263的编码程序,加了CPU指令优化,VC版.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*!
********************************************************
*\file
*    vlc.cpp
*\brief
*    implementation for VLC(Variable Length Coding) funtions.
*\date
*    12/6/2002
*
********************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "vlc.h"
#include "bitstream.h"
#include "mot_est.h"


//! the sturcture of VLC table
typedef struct
{
	unsigned int cdwd;  //!< codeword get from h.263draft
	unsigned int len;   //!< length of codeword
	
}vlcstr;
//VLC CODEWORD TABLE

//generating index : MB type == 3, index = 0 | cbpc;
//                   MB type == 4, index = 1 | cbpc;
//                   last stuffing's index = 0x08;
//! MCPBC table for I frame
static vlcstr mcbpctabI[9] =
{
	{1, 1}, {1, 3}, {2, 3}, {3, 3}, {1, 4}, 
	{1, 6}, {2, 6}, {3, 6}, {1, 9}
};

//generating index : mmmcc: MB type(3 bits) CBPC (2 bit)
//                   last stuffing's index =20
//! MCPBC table for P frame 
static vlcstr mcbpctabP[21] =
{
	{1, 1}, {3, 4}, {2, 4}, {5, 6},
	{3, 3}, {7, 7}, {6, 7}, {5, 9},
	{2, 3}, {5, 7}, {4, 7}, {5, 8},
	{3, 5}, {4, 8}, {3, 8}, {3, 7},
	{4, 6}, {4, 9}, {3, 9}, {2, 9},
	{1, 9}
};

//! MBTYPE table for B picture
static vlcstr mbtypetab[14] =
{
	{3, 2}, {1, 4},
	{4, 3}, {5, 3}, {6, 5},
	{2, 3}, {3, 3}, {7, 5},
	{4, 5}, {5, 5}, {1, 5},
	{1, 6}, {1, 7},
	{1, 9}
};

//! CBPC table(only for B picture)
static vlcstr cbpctab[4] =
{
	{0, 1}, {2, 2}, {7, 3}, {6, 3}
};
//generating index : INTRA index = CBPY
//                   INTER index = CBPY XOR 0x0f  ,not last four bits of cbpy
//! CBPY table
static vlcstr cbpytab[16] =
{
	{3, 4}, {5, 5}, {4, 5}, {9, 4},
	{3, 5}, {7, 4}, {2, 6}, {11, 4},
	{2, 5}, {3, 6}, {5, 4}, {10, 4},
	{4, 4}, {8, 4}, {6, 4}, {3, 2}
};

//MV table, the length is 1 less than that in the table 11 except vector difference = 0
/* Motion vectors */
//! MV table
static vlcstr mvtab[33] =
{
	{1,1}, {1,2}, {1,3}, {1,4}, {3,6}, {5,7}, {4,7}, {3,7},
	{11,9}, {10,9}, {9,9}, {17,10}, {16,10}, {15,10}, {14,10}, {13,10},
	{12,10}, {11,10}, {10,10}, {9,10}, {8,10}, {7,10}, {6,10}, {5,10},
	{4,10}, {7,11}, {6,11}, {5,11}, {4,11}, {3,11}, {2,11}, {3,12},
	{2,12}
};

//! COEFF table0
/*! first part of coeffs for last = 0. Indexed by [run][level-1] */
static vlcstr coeff_tab0[2][12] =
{
	/* run = 0 */
	{
		{0x02, 2}, {0x0f, 4}, {0x15, 6}, {0x17, 7},
		{0x1f, 8}, {0x25, 9}, {0x24, 9}, {0x21,10},
		{0x20,10}, {0x07,11}, {0x06,11}, {0x20,11}
	},
		/* run = 1 */
	{
		{0x06, 3}, {0x14, 6}, {0x1e, 8}, {0x0f,10},
		{0x21,11}, {0x50,12}, {0x00, 0}, {0x00, 0},
		{0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}
	}
};

//! COEFF table1
/*! rest of coeffs for last = 0. indexing by [run-2][level-1] */
static vlcstr coeff_tab1[25][4] =
{
	/* run = 2 */
	{
		{0x0e, 4}, {0x1d, 8}, {0x0e,10}, {0x51,12}
	},
		/* run = 3 */
	{
		{0x0d, 5}, {0x23, 9}, {0x0d,10}, {0x00, 0}
	},
		/* run = 4-26 */
	{
		{0x0c, 5}, {0x22, 9}, {0x52,12}, {0x00, 0}
	},
	{
		{0x0b, 5}, {0x0c,10}, {0x53,12}, {0x00, 0}
	},
	{
		{0x13, 6}, {0x0b,10}, {0x54,12}, {0x00, 0}
	},
	{
		{0x12, 6}, {0x0a,10}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x11, 6}, {0x09,10}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x10, 6}, {0x08,10}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x16, 7}, {0x55,12}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x15, 7}, {0x00, 0}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x14, 7}, {0x00, 0}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x1c, 8}, {0x00, 0}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x1b, 8}, {0x00, 0}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x21, 9}, {0x00, 0}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x20, 9}, {0x00, 0}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x1f, 9}, {0x00, 0}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x1e, 9}, {0x00, 0}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x1d, 9}, {0x00, 0}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x1c, 9}, {0x00, 0}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x1b, 9}, {0x00, 0}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x1a, 9}, {0x00, 0}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x22,11}, {0x00, 0}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x23,11}, {0x00, 0}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x56,12}, {0x00, 0}, {0x00, 0}, {0x00, 0}
	},
	{
		{0x57,12}, {0x00, 0}, {0x00, 0}, {0x00, 0}
	}
};

//! COEFF table2
/*! first coeffs of last = 1. indexing by [run][level-1] */
static vlcstr coeff_tab2[2][3] =
{
	/* run = 0 */
	{
		{0x07, 4}, {0x19, 9}, {0x05,11}
	},
		/* run = 1 */
	{
		{0x0f, 6}, {0x04,11}, {0x00, 0}
	}
};

//! COEFF table3
/*! rest of coeffs for last = 1. indexing by [run-2] */
static vlcstr coeff_tab3[40] =
{
	{0x0e, 6}, {0x0d, 6}, {0x0c, 6},
	{0x13, 7}, {0x12, 7}, {0x11, 7}, {0x10, 7},
	{0x1a, 8}, {0x19, 8}, {0x18, 8}, {0x17, 8},
	{0x16, 8}, {0x15, 8}, {0x14, 8}, {0x13, 8},
	{0x18, 9}, {0x17, 9}, {0x16, 9}, {0x15, 9},    
	{0x14, 9}, {0x13, 9}, {0x12, 9}, {0x11, 9},    
	{0x07,10}, {0x06,10}, {0x05,10}, {0x04,10},    
	{0x24,11}, {0x25,11}, {0x26,11}, {0x27,11},    
	{0x58,12}, {0x59,12}, {0x5a,12}, {0x5b,12},    
	{0x5c,12}, {0x5d,12}, {0x5e,12}, {0x5f,12},
	{0x00, 0}               
};

/* DCT coefficients for INTRA TCOEFF in advanced intra coding mode. 
   Four tables, two for last = 0, two for last = 1.
   the sign bit must be added afterwards. */

/* first part of INTRA_TCOEFF with last = 0 */
/* indexed by [run][level-1] */
static vlcstr intra_coeff_tab0[2][25] =
{
  /* run = 0 */
  {
    {0x02,2}, {0x06,3}, {0x0e,4}, {0x0c,5},
    {0x0d,5}, {0x10,6}, {0x11,6}, {0x12,6},
    {0x16,7}, {0x1b,8}, {0x20,9}, {0x21,9},
    {0x1a,9}, {0x1b,9}, {0x1c,9}, {0x1d,9},
    {0x1e,9}, {0x1f,9}, {0x23,11}, {0x22,11},
    {0x57,12}, {0x56,12}, {0x55,12}, {0x54,12},
    {0x53,12}


  },
  /* run = 1 */
  {
    {0x0f, 4}, {0x14, 6}, {0x14, 7}, {0x1e,8},
    {0x0f,10}, {0x21,11}, {0x50, 12}, {0x00, 0},
    {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0},
    {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0},
    {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0},
    {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0},
    {0x00, 0}
  }
};

/* rest of intra coeffs for last = 0. indexing by [run-2][level-1] */
static vlcstr intra_coeff_tab1[12][4] =
{
  /* run = 2 */
  {
    {0x0b, 5}, {0x15, 7}, {0x0e,10}, {0x09,10}
  },
  /* run = 3 */
  {
    {0x15, 6}, {0x1d, 8}, {0x0d,10}, {0x51, 12}
  },
  /* run = 4-13 */
  {
    {0x13, 6}, {0x23, 9}, {0x07,11}, {0x00, 0}
  },
  {
    {0x17, 7}, {0x22,9}, {0x52,12}, {0x00, 0}
  },
  {
    {0x1c, 8}, {0x0c,10}, {0x00,0}, {0x00, 0}
  },
  {
    {0x1f, 8}, {0x0b,10}, {0x00, 0}, {0x00, 0}
  },
  {
    {0x25, 9}, {0x0a,10}, {0x00, 0}, {0x00, 0}
  },
  {
    {0x24, 9}, {0x06,11}, {0x00, 0}, {0x00, 0}
  },
  {
    {0x21, 10}, {0x00,0}, {0x00, 0}, {0x00, 0}
  },
  {
    {0x20, 10}, {0x00, 0}, {0x00, 0}, {0x00, 0}
  },
  {
    {0x08, 10}, {0x00, 0}, {0x00, 0}, {0x00, 0}
  },
  {
    {0x20, 11}, {0x00, 0}, {0x00, 0}, {0x00, 0}
  },

};

/* first intra coeffs of last = 1. indexing by [run][level-1] */

static vlcstr intra_coeff_tab2[2][10] =
{
  /* run = 0 */

  {
  {0x07, 4}, {0x0c, 6}, {0x10,7}, {0x13,8},
  {0x11, 9}, {0x12, 9}, {0x04,10}, {0x27,11},
  {0x26, 11}, {0x5f, 12}
  },
  /* run = 1 */
  {
  {0x0f, 6}, {0x13,9}, {0x05, 10}, {0x25, 11},
  {0x00, 0}, {0x00, 0}, {0x00,0}, {0x00,0},
  {0x00, 0}, {0x00, 0}
  }

};

/* rest of intra coeffs for last = 1. indexing by [run-2][level-1] */

static vlcstr intra_coeff_tab3[22][3] =
{
  /* run = 2 */
  {
    {0x0e, 6}, {0x14, 9}, {0x24,11}
  },
  /* run = 3 */
  {
    {0x0d, 6}, {0x06, 10}, {0x5e,12}
  },
  /* run = 4-23 */
  {
    {0x11, 7}, {0x07, 10}, {0x00, 0}
  },
  {
    {0x13, 7}, {0x5d,12}, {0x00, 0}
  },
  {
    {0x12, 7}, {0x5c,12}, {0x00, 0}
  },
  {
    {0x14, 8}, {0x5b,12}, {0x00, 0}
  },
  {
    {0x15, 8}, {0x00,0}, {0x00, 0}
  },              
  {
    {0x1a, 8}, {0x00,0}, {0x00, 0}
  },              
  {
    {0x19, 8}, {0x00,0}, {0x00, 0}
  },              
  {
    {0x18, 8}, {0x00,0}, {0x00, 0}
  },              
  {
    {0x17, 8}, {0x00,0}, {0x00, 0}
  },              
  {
    {0x16, 8}, {0x00,0}, {0x00, 0}
  },              
  {
    {0x19, 9}, {0x00,0}, {0x00, 0}
  },              
  {
    {0x15, 9}, {0x00,0}, {0x00, 0}
  },              
  {
    {0x16, 9}, {0x00,0}, {0x00, 0}
  },              
  {
    {0x18, 9}, {0x00,0}, {0x00, 0}
  },              
  {
    {0x17, 9}, {0x00,0}, {0x00, 0}
  },              
  {
    {0x04, 11}, {0x00,0}, {0x00, 0}
  },              
  {
    {0x05, 11}, {0x00,0}, {0x00, 0}
  },              
  {
    {0x58, 12}, {0x00,0}, {0x00, 0}
  },              
  {
    {0x59, 12}, {0x00,0}, {0x00, 0}
  },              
  {
    {0x5a, 12}, {0x00,0}, {0x00, 0}
  },
};

/* Local funtions */
int vlcmcbpcI (putstrm *putstrmctrl, int CBP, int mode);
int vlcmcbpcP(putstrm *putstrmctrl, int CBP, int mode);
int vlccbpy (putstrm *putstrmctrl, int CBP, int mode);
int vlcmv (putstrm *putstrmctrl, int dmv);
int CodeCoeff(putstrm *putstrmctrl, int intra, INT16 *qcoeff, int block, int ncoeffs);
int put_coeff (putstrm *putstrmctrl, int run, int level, int last);
int put_coeff_AI(putstrm *putstrmctrl, int run, int level, int last);

/*!
*******************************************************************************
*
*   Name:          EncPicHdr 
*   Description:   Encode picture header and send bits to stream 
*   Input:         Encoder status structure 
*   Output:       
*   Return:        The number of bits 
*   Side effect:   Write bits to global stream structure putstrmctrl 
*   Last modified: 2002/12/5
*
*******************************************************************************/
int EncPicHdr(H263VencStatus *encoder)
{
	int bits = 0;
	int QUANT = encoder->PTYPE?encoder->QP:encoder->QI;
	
	/*! PSC(Picture Start Code) */
	putbits(&(encoder->putstrmctrl), 1, 17);
	bits += 17;
	putbits(&(encoder->putstrmctrl), 0, 5);
	bits += 5;
	
	/*! TR(Temporal Reference) */
	putbits(&(encoder->putstrmctrl), encoder->TR, 8);
	bits += 8;
	
	/*! PTYPE(Type Information) */
	putbits(&(encoder->putstrmctrl), 1, 1);  //!< always '1', in order to avoid code emulation
	putbits(&(encoder->putstrmctrl), 0, 1);  //!< always '0', for distinction with H.261
	putbits(&(encoder->putstrmctrl), 0, 1);  //!< spilt screen indicator, set off currently
	putbits(&(encoder->putstrmctrl), 0, 1);  //!< document camera indicator, set off currently
	putbits(&(encoder->putstrmctrl), 0, 1);  //!< freeze picture release, set off currently
	putbits(&(encoder->putstrmctrl), encoder->sourceFormat, 3);
	putbits(&(encoder->putstrmctrl), encoder->PTYPE, 1);
	putbits(&(encoder->putstrmctrl), 0, 1);  //!< optional unrestricted motion vector mode, set off currently
	putbits(&(encoder->putstrmctrl), 0, 1);  //!< optional syntax-based arithmetic coding mode, set off currently
	putbits(&(encoder->putstrmctrl), encoder->annex.Advanced_Prediction ? 1 : 0, 1);  
	putbits(&(encoder->putstrmctrl), 0, 1);  //!< optional pb-frame mode, set off currently
	bits += 13;
	
	/*! PQUANT(Quantizer Information) */
	putbits(&(encoder->putstrmctrl), QUANT, 5);
	bits += 5;
	
	/*! CPM(Continuous Presence Multipoint) */
	putbits(&(encoder->putstrmctrl), 0, 1);
	bits ++;
	
	/*! PEI(Extra Insertion Information) */
	putbits(&(encoder->putstrmctrl), 0, 1);
	bits ++;
	
	return bits;
	
}

/*!
*******************************************************************************
*
*   Name:          EncPicHdrPlus 
*   Description:   Encode picture header and send bits to stream(for h263+) 
*   Input:         Encoder status structure 
*   Output:       
*   Return:        The number of bits 
*   Side effect:   Write bits to global stream structure putstrmctrl 
*   Last modified: 2002/12/27
*
*******************************************************************************/
int EncPicHdrPlus(H263VencStatus *encoder)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -