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

📄 coder.c

📁 h.263 c编码源码。目录下有input。qcif原始未压缩文件
💻 C
📖 第 1 页 / 共 3 页
字号:
/************************************************* * libr263: fast H.263 encoder library * * Copyright (C) 1996, Roalt Aalmoes, Twente University * SPA multimedia group * * Based on Telenor TMN 1.6 encoder (Copyright (C) 1995, Telenor R&D) * created by Karl Lillevold  * * Author encoder: Roalt Aalmoes, <aalmoes@huygens.nl> *  * Date: 31-07-96 **************************************************/     /* Modyfied by Roalt Aalmoes with better algorithms and performance * objectives * * Warning: Although I tried to remove all advanced options code, * there might still be some artifacts in the code. Please do not * blame me for not removing all of it.  * Removing all advanced and alternating quantization code was done * for performance reasons. I'm sorry these options are not  * implemented here, but see the tmn1.7 code for a slow functioning * advanced H.263 compression algorithm.  *****************************************************************//* Notes for clear code: *//* var. j is used for row indexing of MB in frame *//* var. i is used for column indexing of MB in frame *//*  pic is declared global *//* MV is changed: it is now a real array of MV instead of array of pointers   to MV. Its range is also equal to MVs encoded, without border MVs    Advantages: No more mallocs and frees in the code, disadvantages: ?? *//* PictImage structure is replaced by int pointer. Drawback: not flexible for   other format. */#include"sim.h"/********************************************************************** * *	Name:		Clip *	Description:    clips recontructed data 0-255 *	 *	Input:	        pointer to recon. data structure *	Side effects:   data structure clipped * *	Date: 960715 	Author: Roalt Aalmoes * ***********************************************************************/ void Clip(MB_Structure *data) {  int n;  int *mb_ptr = (int *) data;  for (n = 0; n < 256 + 64 + 64; n++) {    *mb_ptr = mmin(255,mmax(0,*mb_ptr));    mb_ptr++;  }}/* 编码I帧图像 */void CodeIntraH263(CParam *params, Bits *bits){  unsigned int *new_recon;  MB_Structure *data = (MB_Structure *)malloc(sizeof(MB_Structure));  int *qcoeff;  int Mode = MODE_INTRA;  int CBP;  int i,j;  new_recon = params->recon;    ZeroBits(bits);    pic->QUANT = params->Q_intra;  pic->picture_coding_type = PCT_INTRA;  bits->header += CountBitsPicture(pic);   // 50   for ( j = 0; j < mbr; j++) {                                   // 列    /* insert sync in *every* slice if use_gobsync is chosen */
	  // use_gobsync 0 不需加入slice的头    if (pic->use_gobsync && j != 0)      bits->header += CountBitsSlice(j,params->Q_intra);          for ( i = 0; i < mbc; i++) {                                 //行      pic->MB = i + j * mbc;      bits->no_intra++;      FillLumBlock(i*MB_SIZE, j*MB_SIZE, params->data, data); //MB_size 16      FillChromBlock(i*MB_SIZE, j*MB_SIZE, params->data, data);

	  //dct变换和量化,返回量化后系数的值qcoeff, 和CBP值(每个小宏块是否全0)      qcoeff = MB_EncodeAndFindCBP(data, params->Q_intra, Mode, &CBP);       /* Do standard VLC encoding */ // 变长编码      /* COD = 0 ,Every block is coded as Intra frame */ // COD=0 宏块为帧内

	  //计算宏块信息需要的bit数,编码cod,  对宏块的COD,CBPCM,CBPY 编码      CountBitsMB(Mode,0,CBP,0,pic,bits);//intra中没有skip宏块

	  //宏块, 对qcoeff编码(16*16宏块),写入bit流,并将写入bit数加入到 bits->Y,和 bits->U中      CountBitsCoeff(qcoeff, Mode, CBP,bits,64);     
	  //重建      MB_Decode(qcoeff, data, params->Q_intra, Mode);

	  //限定到0和255间      Clip(data);

	  //重建图像,把data(16*16)的数据放回到new recon(width*height) 中      ReconImage(i,j,data,new_recon);
      free(qcoeff);    }  }  pic->QP_mean = params->Q_intra;  params->recon = new_recon;  AddBitsPicture(bits);  free(data);  return;}/********************************************************************** * *	Name:		MB_Encode *	Description:	DCT and quantization of Macroblocks * *	Input:		MB data struct, mquant (1-31, 0 = no quant), *			MB info struct *	Returns:	Pointer to quantized coefficients  *	Side effects:	 * *	Date: 930128	Author: Robert.Danielsen@nta.no * **********************************************************************//* If you compare original quant with FindCBP, you see they both act   on the same range of coefficients in the cases INTRA (1..63) or    INTER (0..63) */

// dct变换后,系数量化,得到量化系数.返回地址.// mb_orig 原图像yuv数值, QP 量化参数值, I 帧内或帧间模式, CBP值

//CBP从32+16+8+4+2+1,共6个8*8块的情况,qcoeff系数中有非零值,则加上相对应的值
//对mb_orig中数值,dct变换,并量化得到量化系数
//inter状态下,为图像宏块和预测宏块的差值int *MB_EncodeAndFindCBP(MB_Structure *mb_orig, int QP, int I, int *CBP){				  int		i, j, k, l, row, col;  int		fblock[64];                        //暂存8*8小块的值  int		coeff[384];                        // 16*16的宏块yuv值dct变换后系数共384个  int		*coeff_ind;  int 	        *qcoeff;  int		*qcoeff_ind;  int CBP_Mask = 32;  *CBP = 0;			/* CBP gives bit pattern of lowest 6 bits				   that specify which coordinates are not 				   zero. Bits 6 (32) to 2 (4) repr. four				   8x8 Y parts of macroblock, while bits				   1 (2) and 0 (1) repr. resp. the U and				   V component */  if ((qcoeff=(int *)malloc(sizeof(int)*384)) == 0) {        fprintf(stderr,"mb_encode(): Couldn't allocate qcoeff.\n");    exit(0);  }  coeff_ind = coeff;  qcoeff_ind = qcoeff;

  //Y值  for (k=0;k<16;k+=8) {    for (l=0;l<16;l+=8) {      for (i=k,row=0;row<64;i++,row+=8) 
	  {		#if LONGISDOUBLEINT			for (j=l,col=0;col<8;j += 2,col +=2 ) {			  *(long *) (fblock+row+col) = * (long *) &(mb_orig->lum[i][j]);			}		#else			for (j=l,col=0;col<8;j++ , col++ ) {			  *(int *) (fblock+row+col) = * (int *) &(mb_orig->lum[i][j]);                 // 8*8的块			} 		#endif      }
	  //dct变换得到系数,在coeff_ind地址处, coeff_ind 值为DC值       Dct(fblock,coeff_ind);
	  //量化放入qcoeff_ind,并找到CBP值(63个AC系数 qcoeff是否全0)      *CBP |= QuantAndFindCBP(coeff_ind,qcoeff_ind,QP,I,CBP_Mask);//CBP_Mask 32,16,8,4,2,1分别对应6个8*8小块      coeff_ind += 64;      qcoeff_ind += 64;      CBP_Mask = CBP_Mask>>1;   // 除以2       } /* end l */  } /* End k */


  //U值  for (i=0;i<8;i++) {#ifdef LONGISDOUBLEINT    for (j=0;j<8;j += 2) {      *(long *) (fblock+i*8+j) = *(long *) &(mb_orig->Cb[i][j]);    }#else    for (j=0;j<8;j++) {      *(int *) (fblock+i*8+j) = *(int *) &(mb_orig->Cb[i][j]);    }    #endif  }  Dct(fblock,coeff_ind);  *CBP |= QuantAndFindCBP(coeff_ind,qcoeff_ind,QP,I,CBP_Mask /* i == 4 */);   coeff_ind += 64;  qcoeff_ind += 64;  CBP_Mask = CBP_Mask>>1;


  //V值  for (i=0;i<8;i++) {#ifdef LONGISDOUBLEINT    for (j=0;j<8;j += 2) {      * (long *) (fblock+i*8+j) = *(long *) &(mb_orig->Cr[i][j]);    }#else    for (j=0;j<8;j ++) {      * (int *) (fblock+i*8+j) = *(int *) &(mb_orig->Cr[i][j]);    }#endif  }  Dct(fblock,coeff_ind);  *CBP |= QuantAndFindCBP(coeff_ind,qcoeff_ind,QP,I, CBP_Mask /* i == 5 */);   
  return qcoeff;}/********************************************************************** * *	Name:		MB_Decode *	Description:	Reconstruction of quantized DCT-coded Macroblocks * *	Input:		Quantized coefficients, MB data *			QP (1-31, 0 = no quant), MB info block *	Returns:	int (just 0) *	Side effects:	 * *	Date: 930128	Author: Robert.Danielsen@nta.no * **********************************************************************/     int MB_Decode(int *qcoeff, MB_Structure *mb_recon, int QP, int I){  int	i, j, k, l, row, col;  int	*iblock;  int	*qcoeff_ind;  int	*rcoeff, *rcoeff_ind;  if ((iblock = (int *)malloc(sizeof(int)*64)) == NULL) {    fprintf(stderr,"MB_Coder: Could not allocate space for iblock\n");    exit(-1);  }  if ((rcoeff = (int *)malloc(sizeof(int)*384)) == NULL) {    fprintf(stderr,"MB_Coder: Could not allocate space for rcoeff\n");    exit(-1);  }    /* For control purposes */  /* Zero data */  for (i = 0; i < 16; i++)#ifdef LONGISDOUBLEINT    for (j = 0; j < 8; j+=2)      *(long *) &(mb_recon->lum[i][j]) = 0L;#else    for (j = 0; j < 8; j++)      *(int *) &(mb_recon->lum[i][j]) = 0;#endif  for (i = 0; i < 8; i++) #ifdef LONGISDOUBLEINT    for (j = 0; j < 8; j += 2) {      *(long *) &(mb_recon->Cb[i][j]) = 0L;      *(long *) &(mb_recon->Cr[i][j]) = 0L;    }#else    for (j = 0; j < 8; j ++) {      *(int *) &(mb_recon->Cb[i][j]) = 0;      *(int *) &(mb_recon->Cr[i][j]) = 0;    }#endif  qcoeff_ind = qcoeff;  rcoeff_ind = rcoeff;  for (k=0;k<16;k+=8) {    for (l=0;l<16;l+=8) {      Dequant(qcoeff_ind,rcoeff_ind,QP,I);#ifdef STANDARDIDCT  idctref(rcoeff_ind,iblock); #else  idct(rcoeff_ind,iblock); #endif      qcoeff_ind += 64;      rcoeff_ind += 64;      for (i=k,row=0;row<64;i++,row+=8) {#ifdef LONGISDOUBLEINT	for (j=l,col=0;col<8;j += 2,col += 2) {	  *(long *) &(mb_recon->lum[i][j]) = * (long *) (iblock+row+col);       	}#else	for (j=l,col=0;col<8; j++, col++) {	  *(int *) &(mb_recon->lum[i][j]) = * (int *) (iblock+row+col);       	}#endif      }    }  }  Dequant(qcoeff_ind,rcoeff_ind,QP,I);#ifdef STANDARDIDCT  idctref(rcoeff_ind,iblock); #else  idct(rcoeff_ind,iblock); #endif  qcoeff_ind += 64;  rcoeff_ind += 64;  for (i=0;i<8;i++) {#ifdef LONGISDOUBLEINT    for (j=0;j<8;j +=2 ) {      *(long *) &(mb_recon->Cb[i][j]) = *(long *) (iblock+i*8+j);    }#else    for (j=0;j<8;j++ ) {      *(int *) &(mb_recon->Cb[i][j]) = *(int *) (iblock+i*8+j);    }#endif  }  Dequant(qcoeff_ind,rcoeff_ind,QP,I);
#ifdef STANDARDIDCT  idctref(rcoeff_ind,iblock); #else  idct(rcoeff_ind,iblock); #endif  for (i=0;i<8;i++) {#ifdef LONGISDOUBLEINT    for (j=0;j<8;j += 2) {      *(long *) &(mb_recon->Cr[i][j]) = *(long *) (iblock+i*8+j);    }#else    for (j=0;j<8;j++) {      *(int *) &(mb_recon->Cr[i][j]) = *(int *) (iblock+i*8+j);

⌨️ 快捷键说明

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