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

📄 p64.c

📁 h261协议的h261编码码,lib_261.h里面提供了详细的接口和说明.
💻 C
📖 第 1 页 / 共 3 页
字号:
PrintFrame() prints the frame structure to stdout.

EFUNC*/

//void PrintFrame()
//{
//  BEGIN("PrintFrame");
//  int i;
//
//  printf("*** Frame ID: %x ***\n",CFrame);
//  if (CFrame)
//    {
//      printf("NumberComponents %d\n",
//	     CFrame->NumberComponents);
//      for(i=0;i<CFrame->NumberComponents;i++)
//	{
//	  printf("Component: FilePrefix: %s FileSuffix: %s\n",
//		 ((*CFrame->ComponentFilePrefix[i]) ?
//		  CFrame->ComponentFilePrefix[i] : "Null"),
//		 ((*CFrame->ComponentFileSuffix[i]) ?
//		  CFrame->ComponentFileSuffix[i] : "Null"));
//	  printf("Height: %d  Width: %d\n",
//		 CFrame->Height[i],CFrame->Width[i]);
//	  printf("HorizontalFrequency: %d  VerticalFrequency: %d\n",
//		 CFrame->hf[i],CFrame->vf[i]);
//	  InstallIob_en(i);
//	  PrintIob();
//	}
//    }
//}

/*BFUNC

MakeImage_en() makes an image structure and installs it as the current
image.

EFUNC*/

void MakeImage_en()
{
  BEGIN("MakeImage_en");

  if(!CImage)
  {
	  if (!(CImage = MakeStructure(IMAGE)))
	  {
		  WHEREAMI();
		  printf("Cannot make an image structure.\n");
	  }
  }
  CImage->StreamFileName = NULL;
  CImage->p64Mode = 0;
  CImage->Height = 0;
  CImage->Width = 0;
}

/*BFUNC

MakeFrame_en() makes a frame structure and installs it as the current
frame structure.

EFUNC*/

void MakeFrame_en()
{
  BEGIN("MakeFrame_en");
  int i;

  if(!CFrame)
  {
	  if (!(CFrame = MakeStructure(FRAME)))
	  {
		  WHEREAMI();
		  printf("Cannot make an frame structure.\n");
	  }
  }
  else
  {
	  for(i=0;i<MAXIMUM_SOURCES;i++)
	  {
		  if(CFrame->Iob[i])
		  {
			  if(CFrame->Iob[i]->mem)
			  {
				  FREE(CFrame->Iob[i]->mem->data);
			  }
			  FREE(CFrame->Iob[i]->mem);
		  }
		  FREE(CFrame->Iob[i]);
	  }
  }
  CFrame->NumberComponents = 3;
  for(i=0;i<MAXIMUM_SOURCES;i++)
    {
      CFrame->Height[i] = 0;
      CFrame->Width[i] = 0;
      CFrame->hf[i] = 1;
      CFrame->vf[i] = 1;
      *CFrame->ComponentFileName[i]='\0';
      *CFrame->ComponentFilePrefix[i]='\0';
      *CFrame->ComponentFileSuffix[i]='\0';
	  CFrame->yuv[i] = '\0';
    }
}

/*BFUNC

MakeFstore_en() makes and installs the frame stores for the motion
estimation and compensation.

EFUNC*/


void MakeFstore_en()
{
	int i;
	if(!CFS)///////////////////////////////////qjz
	{
		//CFS = (FSTORE *) malloc(sizeof(FSTORE));
		CFS = (FSTORE *) calloc(sizeof(FSTORE),1);
	}////////////////////qjz
	else
	{
		for(i=0;i<MAXIMUM_SOURCES;i++)
		{
			if( CFS->fs[i] )
			{
				if( CFS->fs[i]->mem )
				{
					if(CFS->fs[i]->mem->data)
					{
						free(CFS->fs[i]->mem->data); CFS->fs[i]->mem->data = NULL;
					}
					free(CFS->fs[i]->mem); CFS->fs[i]->mem = NULL;
				}
				free(CFS->fs[i]);
				CFS->fs[i] = NULL;
			}
		}        
	}

	CFS->NumberComponents = 0;

	if( !OFS )
	{
		//OFS = (FSTORE *) malloc(sizeof(FSTORE));
		OFS = (FSTORE *) calloc(sizeof(FSTORE),1);
	}
	else
	{
		for(i=0;i<MAXIMUM_SOURCES;i++)
		{
			if(OFS->fs[i])
			{
				if(OFS->fs[i]->mem)
				{
					if(OFS->fs[i]->mem->data)
					{
						free(OFS->fs[i]->mem->data);	OFS->fs[i]->mem->data = NULL;
					}
					free(OFS->fs[i]->mem);				OFS->fs[i]->mem = NULL;
				}
				free(OFS->fs[i]);
				OFS->fs[i] = NULL;
			}
			
		}
	}
	OFS->NumberComponents = 0;
}

/*BFUNC

MakeStat_en() makes the statistics structure to hold all of the current
statistics. (CStat and RStat).

EFUNC*/


void MakeStat_en()
{
	FREE(CStat);
	CStat = MakeStructure(STAT);
	FREE(RStat);
	RStat = MakeStructure(STAT);
}

/*BFUNC

MakeRate_en() makes some statistics and book-keeping structures for
advanced rate tracking through the frames.

EFUNC*/

void MakeRate_en()
{
	FREE(RCStore);
	RCStore = (RATE *) calloc(NumberFrames,sizeof(RATE));
}

/*BFUNC

SetCCITT_en() sets the CImage and CFrame parameters for CCITT coding.

EFUNC*/

void SetCCITT_en()
{
	BEGIN("SetCCITT_en");
	int i;
	//if (*CFrame->ComponentFilePrefix[0]=='\0')
	//{
	//	WHEREAMI();
	//	printf("A file prefix should be specified.\n");
	//	exit(ERROR_BOUNDS);
	//}
	//for(i=0;i<3;i++)
	//{
	//	if (*CFrame->ComponentFilePrefix[i]=='\0')
	//	{
	//		strcpy(CFrame->ComponentFilePrefix[i],CFrame->ComponentFilePrefix[0]);
	//	}
	//	if (*CFrame->ComponentFileSuffix[i]=='\0')
	//	{
	//		strcpy(CFrame->ComponentFileSuffix[i],DefaultSuffix[i]);
	//	}
	//}
	CFS->NumberComponents = 3;
	OFS->NumberComponents = 3;
	CFrame->NumberComponents = 3;
	CFrame->hf[0] = 2;
	CFrame->vf[0] = 2;
	CFrame->hf[1] = 1;
	CFrame->vf[1] = 1;
	CFrame->hf[2] = 1;
	CFrame->vf[2] = 1;
	switch(ImageType)
	{
	case IT_NTSC:
		NumberGOB = 10;  /* Parameters for NTSC design */
		NumberMDU = 33;
		CImage->Width = 352;
		CImage->Height = 240;
		CFrame->Width[0] = 352;
		CFrame->Height[0] = 240;
		CFrame->Width[1] = 176;
		CFrame->Height[1] = 120;
		CFrame->Width[2] = 176;
		CFrame->Height[2] = 120;
		break;
	case IT_CIF:
		NumberGOB = 12;  /* Parameters for NTSC design */
		NumberMDU = 33;
		CImage->Width = 352;
		CImage->Height = 288;
		CFrame->Width[0] = 352;
		CFrame->Height[0] = 288;
		CFrame->Width[1] = 176;
		CFrame->Height[1] = 144;
		CFrame->Width[2] = 176;
		CFrame->Height[2] = 144;
		break;
	case IT_QCIF:
		NumberGOB = 3;  /* Parameters for NTSC design */
		NumberMDU = 33;
		CImage->Width = 176;
		CImage->Height = 144;
		CFrame->Width[0] = 176;
		CFrame->Height[0] = 144;
		CFrame->Width[1] = 88;
		CFrame->Height[1] = 72;
		CFrame->Width[2] = 88;
		CFrame->Height[2] = 72;
		break;
	default:
		WHEREAMI();
		printf("Unknown ImageType: %d\n",ImageType);
		exit(ERROR_BOUNDS);
		break;
	}

	//yuv = (unsigned char *) calloc(sizeof(char) * ( CImage->Width * CImage->Height + CImage->Width * CImage->Height /2 ));
	//if( yuv ) 
	//	exit(1);

	if(!LastIntra)
		LastIntra = (unsigned char **) calloc(NumberGOB,sizeof(unsigned char *));
	for(i=0;i<NumberGOB;i++)
	{
		/* Should be assigned to all zeroes */
		if(!LastIntra[i])
			LastIntra[i] = (unsigned char *) calloc(NumberMDU,sizeof(unsigned char));
		memset(LastIntra[i],0,NumberMDU);  /* just in case */
	}
}

/*BFUNC

Help() prints out help information about the p64 program.

EFUNC*/

//void Help()
//{
//  BEGIN("Help");
//
//  printf("p64  [-d [-c]] [-NTSC] [-CIF] [-QCIF]\n");
//  printf("     [-a StartNumber] [-b EndNumber]\n");
//  printf("     [-f FrameRate] [-k FrameskipNumber] [-o] [-p]\n");
//  printf("     [-i MCSearchLimit] [-q Quantization] [-v] [-y]\n");
//  printf("     [-r Target Rate] [-x Target Filesize]\n");
//  printf("     [-s StreamFile] [-z ComponentFileSuffix i]\n");
//  printf("     ComponentFilePrefix1 [ComponentFilePrefix2 ComponentFilePrefix3]\n");
//  printf("-NTSC (352x240)  -CIF (352x288) -QCIF (176x144) base filesizes.\n");
//  printf("-a is the start filename index. [inclusive] Defaults to 0.\n");
//  printf("-b is the end filename index. [inclusive] Defaults to 0.\n");
//  printf("-c forces cif large-frame decoding (can be used with all input modes).\n");
//  printf("-d enables the decoder\n");
//  printf("-f gives the frame rate (default 30).\n");
//  printf("-i gives the MC search area: between 1 and 31 (default 15).\n");
//  printf("-k is the frame skip index. Frames/s = FrameRate/FrameSkip.\n");
//  printf("-o enables the interpreter.\n");
//  printf("-p enables parity checking (disabled for 1992 CCITT specs).\n");
//  printf("-q denotes Quantization, between 1 and 31.\n");
//  printf("-r gives the target rate in bps.\n");
//  printf("-s denotes StreamFile, which defaults to ComponentFilePrefix1.p64\n");
//  printf("-v denotes verbose mode, showing quantization changes.\n");
//  printf("-x gives the target filesize in kilobits. (overrides -r option.)\n");
//  printf("-y enables Reference DCT.\n");
//  printf("-z gives the ComponentFileSuffixes (repeatable).\n");
//}

/*BFUNC

MakeFileNames_en() creates the filenames for the component files
from the appropriate prefix and suffix commands.

EFUNC*/

void MakeFileNames_en()
{
	BEGIN("MakeFileNames_en");
	//int i;
    

	//CFrame->yuv_y = &yuv[0];
	//CFrame->yuv_u = &yuv[ CFrame->Height[0] * CFrame->Width[0] ];
	//CFrame->yuv_v = &yuv[ CFrame->Height[1] * CFrame->Width[1] ];

	//begin
	CFrame->yuv[0] = &yuv[0];
	CFrame->yuv[1] = &yuv[ CFrame->Height[0] * CFrame->Width[0] ];
	CFrame->yuv[2] = &yuv[ CFrame->Height[0] * CFrame->Width[0] + CFrame->Height[1] * CFrame->Width[1] ];
	//end

	//for(i=0;i<3;i++)
 //   {
	//	sprintf(CFrame->ComponentFileName[i],"%s%d%s",
	//		CFrame->ComponentFilePrefix[i],
	//		CurrentFrame,
	//		CFrame->ComponentFileSuffix[i]);        
	//}
}

/*BFUNC

VerifyFiles_en() checks to see if the component files are present and
of the correct length.

EFUNC*/

void VerifyFiles_en()
{
  BEGIN("VerifyFiles_en");
  int i,FileSize;
  //FILE *test;
  unsigned char *test_data;
  
  for(i=0;i<CFrame->NumberComponents;i++)
  {
	  //if ((test = fopen(CFrame->ComponentFileName[i],"r")) == NULL)
	  //if ((test = fopen(CFrame->ComponentFileName[i],"rb")) == NULL)
	  //{
		 // WHEREAMI();
		 // printf("Cannot Open FileName %s\n",CFrame->ComponentFileName[i]);
		 // exit(ERROR_BOUNDS);
	  //}
	  //fseek(test,0,2);
	  //FileSize = ftell(test);
   //   rewind(test);
	  //begin
	  test_data = CFrame->yuv[i];
	  FileSize = CFrame->Height[i] * CFrame->Width[i];
	  //end
      if (CFrame->Height[i] == 0)
	  {
		  if (CFrame->Width[i] == 0)
		  {
			  WHEREAMI();
			  printf("Bad File Specification for file %s\n",CFrame->ComponentFileName[i]);
		  }
		  else
		  {
			  CFrame->Height[i] = FileSize / CFrame->Width[i];
			  printf("Autosizing Height to %d\n",CFrame->Height[i]);
		  }
	  }
      if (FileSize != CFrame->Width[i] * CFrame->Height[i]) 
	  {
		  WHEREAMI();
		  printf("Inaccurate File Sizes: Estimated %d: %s: %d \n",
			  CFrame->Width[i] * CFrame->Height[i],CFrame->ComponentFileName[i],FileSize);
		  exit(ERROR_BOUNDS);
	  }
	  //fclose(test);
  }
}

/*END*/

//编码
void h261Encoder(unsigned char * _yuv, unsigned char * _data, unsigned int *_count)
{
	int i = 0;
	BEGIN("h261Encoder");
/*	int i,p,s;  */  
	yuv = _yuv;
	data = _data;
	CurrentMDU = 0;

	for(i=0;i<10;i++)
	{
		memset(inputbuf[i], 0, 64*sizeof(int));
	}
	memset(output, 0, 64*sizeof(int));
	i=0;
	count = 0;

	//init
	//if(i == 0)
	//{
		MakeImage_en();   /* Initialize storage */
		MakeFrame_en();
		MakeFstore_en();
		inithuff_en();    /* Put Huffman tables on */
		initmc_en();      /* Put Motion Comp stuff on */
		//i=0;
	//}
	CurrentFrame = 0;
	StartFrame = CurrentFrame;
	LastFrame = 0;
	//strcpy(CFrame->ComponentFilePrefix[0],"short");
	//CImage->StreamFileName = "qjz.p64";

	ImageType = IT_QCIF;
	switch(ImageType)
	{
	case IT_NTSC:
		PType=0x04;
		PSpareEnable=1;
		PSpare=0x8c;
		break;
	case IT_CIF:
		PType=0x04;
		break;
	case IT_QCIF:
		PType=0x00;//ImageType = IT_QCIF;
		break;
	default:
		WHEREAMI();
		printf("Image Type not supported: %d\n",ImageType);
		break;
	}

	SetCCITT_en();
	NumberFrames = LastFrame-CurrentFrame+1;
	p64EncodeSequence_en();
	*_count = count;

}


//解码
//void h261Decoder(unsigned char * _data, unsigned char * _yuv, int imageType)
//{
//	BEGIN("h261Decoder");
//
//	int i =0;
//	data = _data;
//	yuv = _yuv;
//
//	CurrentMDU=0;
//
//
//	for(i=0;i<10;i++)
//	{
//		memset(inputbuf[i], 0, 64*sizeof(int));
//	}
//	memset(output, 0, 64*sizeof(int));
//	i=0;
//
//
//
//	//if(i == 0)
//	//{
//		MakeImage_en();   /* Initialize storage */
//		MakeFrame_en();
//		MakeFstore_en();
//		inithuff_en();    /* Put Huffman tables on */
//		initmc_en();      /* Put Motion Comp stuff on */
//		//i=0;
//	//}
//
//	CImage->p64Mode |= P_DECODER;//-d
//	//CImage->StreamFileName = str;//-s
//	//strcpy(CFrame->ComponentFilePrefix[0],"short");
//
//	ImageType = imageType;
//
//	switch(ImageType)
//	{
//	case IT_NTSC:
//		PType=0x04;
//		PSpareEnable=1;
//		PSpare=0x8c;
//		break;
//	case IT_CIF:
//		PType=0x04;
//		break;
//	case IT_QCIF:
//		PType=0x00;//ImageType = IT_QCIF;
//		break;
//	default:
//		WHEREAMI();
//		printf("Image Type not supported: %d\n",ImageType);
//		break;
//	}
//
//
//	p64DecodeSequence();
//
//}

void freeMemory_en()
{
	int i=0;

	//IMAGE
    FREE(CImage);

	//FRAME
	if(CFrame)
	{
		for(i=0;i<MAXIMUM_SOURCES;i++)
		{
			if(CFrame->Iob[i])
			{
				if(CFrame->Iob[i]->mem)
				{
					FREE(CFrame->Iob[i]->mem->data);
				}
				FREE(CFrame->Iob[i]->mem);
			}
			FREE(CFrame->Iob[i]);
		}
		FREE(CFrame);
	}

	//FSTORE
	if(CFS)
	{
		for(i=0;i<MAXIMUM_SOURCES;i++)
		{
			if( CFS->fs[i] )
			{
				if( CFS->fs[i]->mem )
				{
					if(CFS->fs[i]->mem->data)
					{
						FREE(CFS->fs[i]->mem->data);
					}
					FREE(CFS->fs[i]->mem);
				}
				FREE(CFS->fs[i]);
			}
		}
		FREE(CFS);
	}

	if(OFS)
	{
		for(i=0;i<MAXIMUM_SOURCES;i++)
		{
			if( OFS->fs[i] )
			{
				if( OFS->fs[i]->mem )
				{
					if(OFS->fs[i]->mem->data)
					{
						FREE(OFS->fs[i]->mem->data);
					}
					FREE(OFS->fs[i]->mem);
				}
				FREE(OFS->fs[i]);
			}
		}
		FREE(OFS);
	}

	//STAT
    FREE(CStat);
	FREE(RStat);

	//RATE
	FREE(RCStore);

	//Huffman
	freeHuffman_en();

	//BLOCK
	freeBLOCK_en();
}

⌨️ 快捷键说明

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