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

📄 endecode.cpp

📁 设计并实现了两种分层多描述视频编码器.通过对小波域的运动估计算法进行了分析和研究
💻 CPP
📖 第 1 页 / 共 4 页
字号:
				}
			}
			i0 <<= 1; j0 <<= 1;	i1 <<= 1; j1 <<= 1;
			for (j2=0; j2<4; j2++) {//计算中间层的参考图像块
				for (i2=0; i2<4; i2++) {
					Pcoeff2[j0+j2][i1+i2] = w[1][0][y+dy+(j2<<2)][x+dx+(i2<<2)];
					Pcoeff2[j1+j2][i0+i2] = w[1][1][y+dy+(j2<<2)][x+dx+(i2<<2)];
					Pcoeff2[j1+j2][i1+i2] = w[1][2][y+dy+(j2<<2)][x+dx+(i2<<2)];
				}
			}
			i0 <<= 1; j0 <<= 1;	i1 <<= 1; j1 <<= 1;
			for (j2=0; j2<8; j2++) {//计算中间层的参考图像块
				for (i2=0; i2<8; i2++) {
					Pcoeff2[j0+j2][i1+i2] = w[0][0][y+dy+(j2<<1)][x+dx+(i2<<1)];
					Pcoeff2[j1+j2][i0+i2] = w[0][1][y+dy+(j2<<1)][x+dx+(i2<<1)];
					Pcoeff2[j1+j2][i1+i2] = w[0][2][y+dy+(j2<<1)][x+dx+(i2<<1)];
				}
			}
//对运动向量编码
/*			c1 = char(dx1);
			fwrite(&c1, sizeof(char), 1, mvfile);
			c1 = char(dy1);
			fwrite(&c1, sizeof(char), 1, mvfile);*/
		    if((x%8==0)&&(y%8==0))
			{
				mv[y/16][x/16][0]=dy1;
				mv[y/16][x/16][1]=dx1;
			}
		}	
	}
/*	for(i=0;i<9;i++)
		for(j=0;j<11;j++)
		{   printf("mv[%d][%d][0]=%d",i,j,mv[i][j][0]);
		    printf("mv[%d][%d][1]=%d\n",i,j,mv[i][j][1]);
		}*/
//	fclose(mvfile);
//	return 	arithmeticCoding();
}

//---------------------------------------------------------------------

void Deall_phase_motion_Estimation(Image_Coord dimension, Pel_Type ** DePcoeff2) //double
{
	int i, j, i0, j0, dx, dy, i2, j2, i1, j1;
	int x, y;//搜索窗口大小
    int Width=dimension.y,Height=dimension.x;


  //------------- Do motion estimation and store result in array------------------------------------------- 
	for ( y=0, j=0; y<Height; j++, y+=16) {//最大子带每块8x8,全相分解变成16x16
	    for ( x=0, i=0; x<Width; i++, x+=16) {
			dx = remv[y/16][x/16][1]; dy = remv[y/16][x/16][0];
			i0=x>>3;j0=y>>3;
            i1=i0+Width/8;j1=j0+Height/8;
            for (j2=0; j2<2; j2++) {//计算最小子带中的参考图像块
				for (i2=0; i2<2; i2++) {
					DePcoeff2[j0+j2][i0+i2] = w1[0][0][y+dy+(j2<<3)][x+dx+(i2<<3)];
					DePcoeff2[j0+j2][i1+i2] = w1[0][1][y+dy+(j2<<3)][x+dx+(i2<<3)];
					DePcoeff2[j1+j2][i0+i2] = w1[1][0][y+dy+(j2<<3)][x+dx+(i2<<3)];
					DePcoeff2[j1+j2][i1+i2] = w1[1][1][y+dy+(j2<<3)][x+dx+(i2<<3)];
				}
			}
			i0 <<= 1; j0 <<= 1;	i1 <<= 1; j1 <<= 1;
			for (j2=0; j2<4; j2++) {//计算中间层的参考图像块
				for (i2=0; i2<4; i2++) {
					DePcoeff2[j0+j2][i1+i2] = w[1][0][y+dy+(j2<<2)][x+dx+(i2<<2)];
					DePcoeff2[j1+j2][i0+i2] = w[1][1][y+dy+(j2<<2)][x+dx+(i2<<2)];
					DePcoeff2[j1+j2][i1+i2] = w[1][2][y+dy+(j2<<2)][x+dx+(i2<<2)];
				}
			}
			i0 <<= 1; j0 <<= 1;	i1 <<= 1; j1 <<= 1;
			for (j2=0; j2<8; j2++) {//计算中间层的参考图像块
				for (i2=0; i2<8; i2++) {
					DePcoeff2[j0+j2][i1+i2] = w[0][0][y+dy+(j2<<1)][x+dx+(i2<<1)];
					DePcoeff2[j1+j2][i0+i2] = w[0][1][y+dy+(j2<<1)][x+dx+(i2<<1)];
					DePcoeff2[j1+j2][i1+i2] = w[0][2][y+dy+(j2<<1)][x+dx+(i2<<1)];
				}
			}
		}	
	}
}

//---------------------------------------------------------------------
void reconstruct(FILE * output,Pel_Type** recoeff)
{
  double max_rate = 0.0, peak_2 = 0.0;
  int i, level1;


  unsigned char ** Wcoeff;
  NEW_VECTOR(Wcoeff, 144, unsigned char *, "error");
  for (i = 0; i < 144; i++) NEW_VECTOR(Wcoeff[i], 176, unsigned char, "error");


//  Input_Line("Name of the compressed image file", cod_f_name);
//  Input_Line("File with original image", pic_f_name);
  
  min_bits=2;
  factor1=0.6962;//1;//0.88;//1.078;//.967;//0.6962;//0.504;//0.437;//1;//1.9;//
//  factor1=2.006;//1.024;//0.53;//去掉final_bit的倍乘因子
//  pic_f_name="input.yuv";//"lena.512"
  bit_rate=0.8;//2.25;//0.5;//
  for (i=0; i<2; i++) {
	if (i>0) {
		cod_f_name="input2.cmp"; 
		mdc=2;
	}
	else {
		cod_f_name="input1.cmp"; 
		mdc=1;
	}
    dedata_file.open_file(cod_f_name);
    dimension.x = (1 + dedata_file.decode_bits(11)) << 2;
    dimension.y = (1 + dedata_file.decode_bits(11)) << 2;
    rate_mult = 8.0 / (double(dimension.x) * double(dimension.y));
    threshold_bits = dedata_file.decode_bits(5);
    pel_bytes = 1 + dedata_file.decode_bits(1);
    mean = dedata_file.decode_bits(8);
    mean += dedata_file.decode_bits(8) << 8;
    if (pel_bytes == 2)
      mean += dedata_file.decode_bits(8) << 16;

    image.reset(dimension, mean, pel_bytes);


    DeProcess_Image();
//	if (mdc==1) image.coecmp();

    dedata_file.close_file();
	image.coe(1./factor1); 
	if (!i) level1 = image.pyramid_levels();
    image.recover();

  }
  image.recall(level1);
//image.coecmp();
  image.coe(1./factor1);
//  image.recover();
  image.recover(Wcoeff);

/*  for(int j=0;j<144;j++)
      fwrite(Wcoeff[j],sizeof(unsigned char),176,output);*/


  for (i=0;i<144;i++)
	  for (int j=0;j<176;j++)
		  recoeff[i][j]=image.readdress(i,j);
  remean = image.transform_mean();
/*    for(i=0;i<10;i++)
	  for(int j=0;j<10;j++)
		  printf("recoeff[%d][%d]=%f",i,j,recoeff[i][j]);*/
}

//-----------------------P_Frame------------------------------------------------
void P_Frame(int num)
{
  int i, check, j, k, p, c, sign,h,u;
  long Pmean=0;
  min_bits=3;//2;//4;//1;//
  factor1=1.09;//0.9;//0.78;//1.2;//0.855;//0.962;//1.56;//2.55;//3.0;//0.638;//0.67;//0.7;//0.74;//0.89;//0.6;//0.504;//0.79;//0.415;//.967;//0.429;//0.85;//1.5;//1.48;//1.02;//1.024;//1;//0.99;//1.9;//0.69;//0.93;//0.7;//0.6;//0.99;//1.078;//0.95;//有final_bit的倍乘因子
//  factor1=2.006;//0.53;//去掉final_bit的倍乘因子
//  pic_f_name="input.y";//"lena.512"
  dimension.x=144;//512
  dimension.y=176;//512
  pel_bytes=1;
  bit_rate=0.8;//2.25

  Pel_Type **Pcoeff;
  NEW_VECTOR(Pcoeff, 144, Pel_Type *, "erroe");
  for (i = 0; i < 144; i++) NEW_VECTOR(Pcoeff[i], 176, Pel_Type, "error");

  Pel_Type **Pcoeff2;
  NEW_VECTOR(Pcoeff2, 144, Pel_Type *, "erroe");
  for (i = 0; i < 144; i++) NEW_VECTOR(Pcoeff2[i], 176, Pel_Type, "error");


  FILE * in_file = fopen(pic_f_name, "rb");
  if (in_file == NULL) Error("error");
  
  check=fseek(in_file,(dimension.x*dimension.y*num),SEEK_SET);//*3/2
  if(check)
	  printf("can't open the %c",pic_f_name);

  Pel_Type s=0;

  for (i = 0; i < dimension.x; i++)
    for (j = 0; j < dimension.y; j++) {
      for (p = k = 0; k < 1; k++) {
        if ((c = getc(in_file)) == EOF) Error("error");
        p = (p << 8) | c; }
      Pcoeff[i][j] = p;
	  s+=Pcoeff[i][j];
			}
  fclose(in_file);
  
  Pmean = Round((s/ double(dimension.x)) / dimension.y);
  for(i=0;i<dimension.x;i++)
	  for(j=0;j<dimension.y;j++)
	        Pcoeff[i][j]-=Pmean;

  image.transform(Pcoeff);
//运动估计
  all_phase_motion_Estimation(dimension, Pcoeff, Pcoeff2);
//求运动估计后的残差
  	for (i=0;i<144;i++) //DWT not take mean into account
		for (j=0;j<176;j++) Pcoeff[i][j]-=Pcoeff2[i][j];

	double maxco=0;
    for(i=0;i<144;i++)
		for(j=0;j<176;j++)
		{
			if(fabs(Pcoeff[i][j])>maxco)
			{
				maxco=Pcoeff[i][j];
				h=i;
				u=j;
			}
		}
	printf("maxco=%f, h=%d, u=%d",maxco,h,u);

//计算残差最大绝对值
/*   int maxx=0,maxy=0;
   double maxP=0;
   for(i=0;i<144;i++)
	   for(j=0;j<176;j++)
	   {
		   if(maxP<fabs(Pcoeff[i][j]))
		   {
			   maxP=fabs(Pcoeff[i][j]);
			   maxx=i;
			   maxy=j;
		   }
	   }
  printf("maxP=%f,maxx=%d,maxy=%d",maxP,maxx,maxy);*/
//

  rate_mult = 8.0 / (double(dimension.x) * double(dimension.y));
  if (Max(dimension.x, dimension.y) > 8192) Error("image is too large!");
  byte_budget = 65535;//long(0.125 * (bit_rate * dimension.x) *dimension.y) - 1;

  


  for (i=0; i<2; i++) {
	if (i>0) {
		cod_f_name="input_P2.cmp"; 
		mdc=2;
	}
	else {
		cod_f_name="input_P1.cmp"; 
		mdc=1;
	}
    total_time.start();
/*  image.P_read_pic(dimension, pic_f_name, pel_bytes);
    image.transform();
    mean = image.transform_mean();*/

    image.send_coeff(dimension,Pcoeff);

    image.coe(factor1); 
//	if (mdc==1) image.coewrite(); //调试用,存系数中心重建时读出比较

//	if (i) image.decimate2(1<<(min_bits+3));
//	image.coeprintf();
    Chronometer max_time;
    max_time.start("\n  Starting computation of maximum magnitudes...");
    threshold_bits = Calc_Max_Image();
    max_time.display(" Maximum magnitudes computed in");
  
    data_file.open_file(cod_f_name);
    data_file.code_bits(11, (dimension.x >> 2) - 1);
    data_file.code_bits(11, (dimension.y >> 2) - 1);
    data_file.code_bits(5, threshold_bits);
    data_file.code_bits(1, pel_bytes - 1);
    data_file.code_bits(8, int(Pmean & 0xFFL));
//    data_file.code_bits(8, int((Pmean >> 8) & 0xFFL));
    if (pel_bytes == 2)
    data_file.code_bits(8, int((Pmean >> 16) & 0xFFL));

    code_time.start("\n  Starting image compression...");
    Process_Image();
    //运动向量编码
	for(int l=0;l<9;l++)
		for(int m=0;m<11;m++)
		{
			if((mv[l][m][0]<0)&(mv[l][m][1]<0))
				sign=3;
			else if((mv[l][m][0]>=0)&(mv[l][m][1]<0))
				sign=2;
			else if((mv[l][m][0]<0)&(mv[l][m][1]>=0))
				sign=1;
			else
				sign=0;
			data_file.code_bits(2,sign);
			data_file.code_bits(4,abs(mv[l][m][0]));
			data_file.code_bits(4,abs(mv[l][m][1]));
		}/**/
	
	code_time.display(" Image compressed in");



    data_file.close_file();

    printf("\n  Compressed file size = %ld bytes (%6.3f bits/pixel).\n",
      data_file.bytes_used(), rate_mult * data_file.bytes_used());

    total_time.display("\n  Total execution time (I/O included) =");
    puts(" ");
    total_time.stop();    
  }
}

//---------------------------Decoder P_Frame----------------------------
void DeP_Frame(int num, Pel_Type** recoeff)
{
  double mse, psnr, max_rate = 0.0, peak_2 = 0.0;
  int i, level1,design, l, m;

//  Input_Line("Name of the compressed image file", cod_f_name);
//  Input_Line("File with original image", pic_f_name);


  Pel_Type ** DePcoeff;
  NEW_VECTOR(DePcoeff, 144, Pel_Type *, "erroe");
  for (i = 0; i < 144; i++) NEW_VECTOR(DePcoeff[i], 176, Pel_Type, "error");

  Pel_Type ** DePcoeff2;
  NEW_VECTOR(DePcoeff2, 144, Pel_Type *, "erroe");
  for (i = 0; i < 144; i++) NEW_VECTOR(DePcoeff2[i], 176, Pel_Type, "error");

  unsigned char ** Wcoeff;
  NEW_VECTOR(Wcoeff, 144, unsigned char *, "error");
  for (i = 0; i < 144; i++) NEW_VECTOR(Wcoeff[i], 176, unsigned char, "error");
  
  min_bits=3;//2;//4;//1;//
  factor1=1.09;//0.9;//0.78;//1.2;//0.855;//0.962;//1.56;//2.55;//3.0;//0.638;//0.67;//0.7;//0.74;//0.89;//0.6;//0.504;//0.79;//0.415;//.967;//0.429;//0.85;//1.5;//1.48;//1.02;//1.024;//1;//0.99;//1.9;//.967;//0.69;//0.93;//0.7;//0.6;//0.99;//0.95;//1.078;//
//  factor1=2.006;//0.53;//去掉final_bit的倍乘因子
/*  if(num==1)
      pic_f_name="inputp.y";
  else
	  pic_f_name="inputp1.y";//"lena.512"*/
  
  pic_f_name="input.y";

  bit_rate=0.8;
  for (i=0; i<2; i++) {
	if (i>0) {
		cod_f_name="input_P2.cmp"; 
		mdc=2;
	}
	else {
		cod_f_name="input_P1.cmp"; 
		mdc=1;
	}
    dedata_file.open_file(cod_f_name);
    dimension.x = (1 + dedata_file.decode_bits(11)) << 2;
    dimension.y = (1 + dedata_file.decode_bits(11)) << 2;
    rate_mult = 8.0 / (double(dimension.x) * double(dimension.y));
    threshold_bits = dedata_file.decode_bits(5);
    pel_bytes = 1 + dedata_file.decode_bits(1);
    mean = dedata_file.decode_bits(8);
//    mean += dedata_file.decode_bits(8) << 8;
    if (pel_bytes == 2)
      mean += dedata_file.decode_bits(8) << 16;

    printf("\n  Image size = %d x %d, %d byte/pixel\n",
      dimension.x, dimension.y, pel_bytes);

    if (peak_2 == 0.0)
      peak_2 = (pel_bytes == 1 ? 65025.0 :
        Sqr(Input_double("Peak value for PSNR computations")));

    code_time.reset();  total_time.reset();  total_time.start();
    
    if(!i) image.lvset();

    image.reset(dimension, mean, pel_bytes);

    code_time.start("\n  Starting image recovery...");
    DeProcess_Image();
	code_time.stop();
//	if (mdc==1) image.coecmp();
    code_time.display(" Image recovered in");

	//运动向量解码    
	for(l=0;l<9;l++)
		for(m=0;m<11;m++)
		{
			design=dedata_file.decode_bits(2);
			remv[l][m][0]=dedata_file.decode_bits(4);
			remv[l][m][1]=dedata_file.decode_bits(4);
			if(design==3)
			{
				remv[l][m][0]=-remv[l][m][0];
				remv[l][m][1]=-remv[l][m][1];
			}
			else if(design==2)
			{	remv[l][m][1]=-remv[l][m][1];
			}
			else if(design==1)
            {    remv[l][m][0]=-remv[l][m][0];
			}
		/*	printf("remv[%d][%d][0]=%d",l,m,remv[l][m][0]);
			printf("remv[%d][%d][1]=%d\n",l,m,remv[l][m][1]);*/
		}
	

⌨️ 快捷键说明

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