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

📄 p642.c

📁 h261协议的h261编码码,lib_261.h里面提供了详细的接口和说明.
💻 C
📖 第 1 页 / 共 3 页
字号:
//	     BufferContents_en(),
//	     BufferSize_en());
//    }
//  printf("MB Attribute Bits: %6d  MV2 Bits: %6d   EOB Bits: %6d\n",
//	 MacroAttributeBits2,MotionVectorBits2,EOBBits2);
//  printf("Y Bits: %7d  U Bits: %7d  V Bits: %7d  Total Bits: %7d\n",
//	 YCoefBits2,UCoefBits2,VCoefBits2,(YCoefBits2+UCoefBits2+VCoefBits2));
//  printf("MV2 StepSize: %f  MV2 NumberNonZero: %f  MV2 NumberZero: %f\n",
//	 (double) ((double) QSum2)/((double)(QUse2)),
//	 (double) ((double) NumberNZ2)/
//	 ((double)(NumberGOB2*NumberMDU2*6)),
//	 (double) ((double) (NumberGOB2*NumberMDU2*6*64)- NumberNZ2)/
//	 ((double)(NumberGOB2*NumberMDU2*6)));
//  RCStore2[CurrentFrame2-StartFrame2].size = LastBits2;
//  printf("Code MType2: ");
//  for(x=0;x<10;x++) printf("%5d",x);
//  printf("\n");
//  printf("Macro Freq: ");
//  for(x=0;x<10;x++) printf("%5d",MacroTypeFrequency2[x]);
//  printf("\n");
//  printf("Y     Freq: ");
//  for(x=0;x<10;x++) printf("%5d",YTypeFrequency2[x]);
//  printf("\n");
//  printf("UV    Freq: ");
//  for(x=0;x<10;x++) printf("%5d",UVTypeFrequency2[x]);
//  printf("\n");
//
//  SwapFS_en(CFS2,OFS2);
//
//  Statistics_en();
//  printf("END>Frame: %d\n",CurrentFrame2);
//  if (Rate2)
//    {
//      if (CurrentFrame2==StartFrame2)
//	{/* Begin Buffer at 0.5 size */
//	  FirstFrameBits2 = TotalBits2;
//	  BufferOffset2 = (BufferSize_en()/2) - BufferContents_en();
//	  printf("First Frame Reset Buffer by delta bits: %d\n",
//		 BufferOffset2);
//	}
//      /* Take off standard deduction afterwards. */
//      BufferOffset2 -= (Rate2*FrameSkip2/FrameRate2);
//    }
//  else if (CurrentFrame2==StartFrame2)
//    FirstFrameBits2 = TotalBits2;
//  CurrentGOB2=0;TransmittedFrames2++;
//  CurrentFrame2+=FrameSkip2;    /* Change GOB & Frame at same time */
//}
//

/*BFUNC

p64EncodeGOB_en() encodes a group of blocks within a frame.

EFUNC*/

//void p64EncodeGOB_en()
//{
//  BEGIN("p64EncodeGOB_en");
//  double xValue,yValue;
//
//  MQuantEnable2=0;                         /* Disable MQuant2 */
//  if ((Rate2)&&(CurrentFrame2!=StartFrame2)) /* Change Quantization */
//    { 
//      ExecuteQuantization_en();
//      GQuant2=OracleGQuant2;
//    }
//  switch (ImageType2)
//    {
//    case IT_NTSC:
//    case IT_CIF:
//      GRead2=CurrentGOB2;
//      break;
//    case IT_QCIF:
//      GRead2=(CurrentGOB2<<1);
//      break;
//    default:
//      WHEREAMI();
//      printf("Unknown Image Type: %d\n",ImageType2);
//      break;
//    }
//  WriteGOBHeader_en();
//  
//  LastMBA2 = -1; MType2=0;
//  for(;CurrentMDU2<NumberMDU2;CurrentMDU2++)
//    { /* MAIN LOOP */
//      LastMType2=MType2;
//      if ((Rate2)&&(CurrentMDU2)&&!(CurrentMDU2%QUpdateFrequency2)&&
//	  (CurrentFrame2!=StartFrame2))
//	{  /* Begin Buffer control */
//	  ExecuteQuantization_en();
//	  if (OracleMQuant2!= GQuant2)
//	    {
//	      if (MQuantEnable2=OracleMQuantEnable2)
//		MQuant2=OracleMQuant2;
//	    }
//	  else MQuantEnable2=0;
//	}
//      xValue = (double) MeOVal2[Bpos_en(CurrentGOB2,CurrentMDU2,0,0)];
//      yValue = (double) MeVal2[Bpos_en(CurrentGOB2,CurrentMDU2,0,0)];
//      xValue = xValue/256;
//      yValue = yValue/256;
//      MVDH2 = MeX2[Bpos_en(CurrentGOB2,CurrentMDU2,0,0)];
//      MVDV2 = MeY2[Bpos_en(CurrentGOB2,CurrentMDU2,0,0)];
//      VAR2  = MeVAR2[Bpos_en(CurrentGOB2,CurrentMDU2,0,0)];
//      VAROR2 = MeVAROR2[Bpos_en(CurrentGOB2,CurrentMDU2,0,0)];
//      MWOR2 = MeMWOR2[Bpos_en(CurrentGOB2,CurrentMDU2,0,0)];
//      if (CurrentFrame2!=StartFrame2)   /* Intra vs. Inter decision */
//	  {
//		  if ((VAR2 < 64) || (VAROR2 > VAR2))
//		  {                               /* (MC+Inter)mode */
//			  if ((xValue < 1.0) ||  
//				  ((xValue < 3.0) && (yValue > (xValue*0.5))) || 
//				  ((yValue > (xValue/1.1))))
//				  MType2 = 2;                   /* Inter mode */
//			  else if (VAR2 < (double) D_FILTERTHRESHOLD) /* MC mode */
//				  MType2 = 5;                    /* No Filter MC */
//			  else
//				  MType2 = 8;                    /* Filter MC */
//		  }
//		  else MType2 = 0; /*Intramode */
//		  if (MQuantEnable2)
//		  {
//			  MType2++;
//		  }
//		  if (Oracle2)  /* If oracle, then consult oracle */
//		  {
//			  CallOracle(0);
//			  /* Maybe also look at Q? */
//			  /* MQuant2 = (int)  Memory2[L_MQuant]; */
//			  /* MQuantEnable2 = (int)  Memory2[L_Q2ENABLE]; */
//			  MType2 = (int) Memory2[L_MTYPE];             /* Oracle2's Type 3 */
//		  }
//	  }
//	  else
//		  MType2 = 0; /* We always start with Intramode */
//	  if (LastIntra2[CurrentGOB2][CurrentMDU2]>SEQUENCE_INTRA_THRESHOLD)
//		  MType2=0;  /* Code intra every 132 blocks */
//
//      /* printf("[State %d]",MType2);*/
//	  if ((Rate2)&&(BufferContents_en()>BufferSize_en()))
//	  {
//		  MVDH2=MVDV2=0; /* Motion vectors 0 */
//		  MType2=4;     /* No coefficient transmission */
//		  NumberOvfl2++;
//		  WHEREAMI();
//		  printf("Buffer Overflow!\n");
//	  }
//	  p64EncodeMDU_en();
//	}
//}

/*BFUNC

p64EncodeMDU_en(MDU,)
     ) encodes the MDU by read/compressing the MDU; encodes the MDU by read/compressing the MDU, then
writing it, then decoding it and accumulating statistics.

EFUNC*/
//
//static void p64EncodeMDU_en()
//{
//  BEGIN("p64EncodeMDU_en");
//
//  ReadCompressMDU_en();
//  WriteMDU_en();
//  DecodeSaveMDU2();
//
//  QUse2++;                  /* Accumulate statistics */
//  QSum2+=UseQuant2;
//  if (MType2 < 10)
//    MacroTypeFrequency2[MType2]++;
//  else
//    {
//      WHEREAMI();
//      printf("Illegal MType2: %d\n",MType2);
//    }
//}

/*BFUNC

ReadCompressMDU_en(MDU,)
     ) reads in the MDU; reads in the MDU, and attempts to compress it.
If the chosen MType2 is invalid, it finds the closest match.

EFUNC*/

//static void ReadCompressMDU_en()
//{
//  BEGIN("ReadCompressMDU_en");
//  int c,j,h,v,x;
//  int *input;
//  int total,accum,pmask;
//
//  while(1)                        /* READ AND COMPRESS */
//  {
//	  if (QuantMType2[MType2])
//	  {
//		  UseQuant2=MQuant2;
//		  GQuant2=MQuant2; /* Future MB Quant is now MQuant2 */
//	  }
//	  else UseQuant2=GQuant2;
//	  for(c=0;c<6;c++)
//	  {
//		  input = &inputbuf2[c][0];
//		  j = BlockJ2[c];
//		  v = BlockV2[c];
//		  h = BlockH2[c];
//		  if (TCoeffMType2[MType2])
//		  {
//			  InstallIob2(j);
//			  MoveTo2(CurrentGOB2,CurrentMDU2,h,v);
//			  ReadBlock_en(input);
//			  if (!IntraMType2[MType2])
//			  {
//				  InstallFS2(j,CFS2);
//				  MoveTo2(CurrentGOB2,CurrentMDU2,h,v);
//				  if (FilterMType2[MType2])
//				  {
//					  if (j)
//						  HalfSubFCompensate_en(input);
//					  else
//						  SubFCompensate_en(input);
//				  }
//				  else if (MFMType2[MType2])
//				  {
//					  if (j)
//						  HalfSubCompensate_en(input);
//					  else
//						  SubCompensate_en(input);
//				  }
//				  else
//					  SubOverlay_en(input);
//			  }
//			  DefaultDct_en(input,output2);
//			  BoundDctMatrix_en(output2);
//			  if (IntraMType2[MType2])
//			  {
//				  CCITTFlatQuantize_en(output2,8,UseQuant2);
//				  FlatBoundQuantizeMatrix_en(output2);
//			  }
//			  else
//			  {
//				  CCITTQuantize_en(output2,UseQuant2,UseQuant2);
//				  BoundQuantizeMatrix_en(output2);
//			  }
//			  ZigzagMatrix_en(output2,input);
//		  }
//		  else
//			  for(x=0;x<64;x++) 
//				  input[x] = 0;
//	  }
//	  if (!CBPMType2[MType2]) CBP2 = 0x3f;  /* VERIFY MType2 CBP2 */
//	  else
//	  {
//		  for(pmask=0,CBP2=0,total=0,c=0;c<6;c++)
//		  {
//			  input = &inputbuf2[c][0];
//			  for(accum=0,x=0;x<64;x++) 
//				  accum += abs(input[x]);
//			  if ((accum)&&(pmask==0)) 
//				  pmask|=bit_set_mask2[5-c];
//			  if (accum>CBPThreshold2) 
//				  CBP2 |= bit_set_mask2[5-c];
//			  total+= accum;
//		  }
//		  if (!CBP2)
//		  {
//			  if (pmask) 
//				  CBP2=pmask;
//			  else 
//			  {
//				  if (!FilterMType2[MType2])
//				  {
//					  CBP2=0;
//					  MType2=4;
//					  continue;
//				  }
//				  else 
//				  {
//					  CBP2=0;
//					  MType2=7;
//					  continue;
//				  }
//			  }
//		  }
//	  }
//	  if (IntraMType2[MType2])
//		  LastIntra2[CurrentGOB2][CurrentMDU2]=0;
//	  else 
//		  LastIntra2[CurrentGOB2][CurrentMDU2]++;
//	  return;                              /* IF HERE, THEN EXIT LOOP */
//  }                                     /* GOOD ENCODING TYPE */
//}

/*BFUNC

WriteMDU_en() writes out the MDU to the stream. The input buffer and
MType2 must already be set once this function is called.

EFUNC*/

//static void WriteMDU_en()
//{
//  BEGIN("WriteMDU_en");
//  int c,j,x;
//  int *input;
//
//  MBA2 = (CurrentMDU2-LastMBA2);     /* WRITE */
//  WriteMBHeader_en();
//  LastMBA2 = CurrentMDU2;
//  for(c=0;c<6;c++)
//    {
//      j = BlockJ2[c];
//      input = &inputbuf2[c][0];
//      if ((CBP2 & bit_set_mask2[5-c])&&(TCoeffMType2[MType2]))
//	{
//	  if(j) {UVTypeFrequency2[MType2]++;}
//	  else {YTypeFrequency2[MType2]++;}
//	  CodedBlockBits2=0;
//	  if (CBPMType2[MType2])
//	    {
//	      CBPEncodeAC_en(0,input);
//	    }
//	  else
//	    {
//	      EncodeDC_en(*input);
//	      EncodeAC_en(1,input);
//	    }
//	  if(!j){YCoefBits2+=CodedBlockBits2;}
//	  else if(j==1){UCoefBits2+=CodedBlockBits2;}
//	  else{VCoefBits2+=CodedBlockBits2;}
//	  IZigzagMatrix2(input,output2);
//	  if (IntraMType2[MType2])
//	    ICCITTFlatQuantize2(output2,8,UseQuant2);
//	  else
//	    ICCITTQuantize2(output2,UseQuant2,UseQuant2);
//	  DefaultIDct(output2,input);
//	}
//      else for(x=0;x<64;x++) input[x]=0;
//    }
//}

/*BFUNC

DecodeSaveMDU2() does a decode on the MDU that was just encoded/decoded
and left on the inputbuf2 array.  The device is OFS2 if encoding mode is
on, else it is the Iob2 if decoding mode is on.

EFUNC*/

static void DecodeSaveMDU2()
{
	BEGIN("DecodeSaveMDU2");
	int c,j,h,v;
	int *input;
    
	for(c=0;c<6;c++)
	{
		j = BlockJ2[c];
		v = BlockV2[c];
		h = BlockH2[c];
		input = &inputbuf2[c][0];
        
		if (!IntraMType2[MType2])     /* DECODE */
		{
			InstallFS2(j,CFS2);
			MoveTo2(CurrentGOB2,CurrentMDU2,h,v);
			if (FilterMType2[MType2])
			{
				if (j)
					HalfAddFCompensate2(input);
				else
					AddFCompensate2(input);
			}
			else if (MFMType2[MType2])
			{
				if (j)
					HalfAddCompensate2(input);
				else
					AddCompensate2(input);
			}
			else
				AddOverlay2(input);
		}
		BoundIDctMatrix2(input);       /* SAVE */
		if (!(GetFlag(CImage2->p64Mode,P_DECODER)))
			InstallFS2(j,OFS2);
		else
			InstallIob2(j);
		MoveTo2(CurrentGOB2,CurrentMDU2,h,v);
		WriteBlock2(input);
	}
}


/*BFUNC

p64DecodeSequence2() decodes the sequence defined in the CImage2 and
CFrame2 structures.

EFUNC*/

void p64DecodeSequence2()
{
	BEGIN("p64DecodeSequence2");
	//int SelfParity;
	int Active;
	int EndFrame=0;
    
	//sropen2(CImage2->StreamFileName);
	sropen2(" ");
	if (ReadHeaderHeader2()) /* nonzero on error or eof */
	{
		//srclose();
		exit(ErrorValue2);
	}
	Active=0;
	while(1)
    {
		if (!EndFrame)
			ReadHeaderTrailer2();
		if ((GRead2 < 0)||(EndFrame))  /* End Of Frame */
		{
			if (!EndFrame)
				ReadPictureHeader2();
			else
				TemporalReference2++;
			if (Active)
			{
				CopyIob2FS2(CFS2);
				while(((CurrentFrame2+TemporalOffset2)%32) !=	TemporalReference2)
				{
					printf("END> Frame: %d\n",CurrentFrame2);
					MakeFileNames2();
					WriteIob2();
					CurrentFrame2++;
				}
				/* Might still be "Filler Frame" sent at the end of file */
				//if (ParityEnable2)
				//{
				//	SelfParity = ParityFS(CFS2);
				//	if (Parity2 != SelfParity)
				//	{
				//		printf("Bad Parity2: Self: %x  Sent: %x\n",SelfParity,Parity2);
				//	}
				//}
			}
			else
			{	      /* First Frame */
				if (ForceCIF2)
					ImageType2=IT_CIF;
				else
				{
					if (PType2&0x04)
					{
						if (PSpareEnable2&&PSpare2==0x8c) 
							ImageType2=IT_NTSC;
						else ImageType2=IT_CIF;
					}
					else ImageType2=IT_QCIF;
				}
				SetCCITT2();
				//if (Loud2 > MUTE)
				//{
				//	PrintImage();
				//	PrintFrame();
				//}
				MakeIob2(WRITE_IOB);
				InitFS2(CFS2);
				ClearFS2(CFS2);
				TemporalOffset2=(TemporalReference2-CurrentFrame2)%32;
				Active=1;
			}
			if ((EndFrame)||(ReadHeaderHeader2())) /* nonzero on error or eof */
				break; /* Could be end of file */
			printf("START>Frame: %d\n",CurrentFrame2); /* Frame is for real */
			continue;
		}
		EndFrame = p64DecodeGOB2();                     /* Else decode the GOB */
	}
	//srclose();
}

/*BFUNC

p64DecodeGOB2() decodes the GOB block of the current frame.

EFUNC*/

int p64DecodeGOB2()
{
	BEGIN("p64DecodeGOB2");
    
	ReadGOBHeader2();             /* Read the group of blocks header  */
	switch(ImageType2)
	{
	case IT_NTSC:
	case IT_CIF:
		CurrentGOB2 = GRead2;
		break;
	case IT_QCIF:
		CurrentGOB2 = (GRead2>>1);
		break;
	default:
		WHEREAMI();
		printf("Unknown Image Type: %d.\n",ImageType2);
		break;
	}
	if (CurrentGOB2 > NumberGOB2)
	{
		WHEREAMI();
		printf("Buffer Overflow: Current:%d  Number:%d\n",CurrentGOB2, NumberGOB2);
		return 0;
		//return;
	}
	LastMBA2 = -1;               /* Reset the MBA2 and the other predictors  */
	LastMVDH2 = 0;
	LastMVDV2 = 0;
	
	while(ReadMBHeader2()==0)
	{
		if (DecompressMDU2())
			return(1);
		DecodeSaveMDU2();
	}
	return(0);
}

/*BFUNC

DecompressMDU2() decompresses the current MDU of which the header has
already been read off of the stream.  It leaves the decoded result in
the inputbuf2 array.  It returns a 1 if an end of file has occurred.

EFUNC*/

static int DecompressMDU2()
{
	BEGIN("DecompressMDU2");
	int c,j,x;
	int *input;

	LastMBA2 = LastMBA2 + MBA2;
	CurrentMDU2 = LastMBA2;
	if (CurrentMDU2 >= NumberMDU2)
	{
		if ((CurrentGOB2 == NumberGOB2-1)&&(seof2()))
			return(1);

		WHEREAMI();
		printf("Apparent MDU out of range: %d > %d.\n",CurrentMDU2,NumberMDU2);
		printf("CurrentGOB2: %d LastMBA2 %d, MBA2: %d\n",CurrentGOB2,LastMBA2,MBA2);
		//printf("at bit position %d in stream\n",mrtell());
		CurrentMDU2=0;
		LastMBA2=0;
		return(0);
	}
	if (!CBPMType2[MType2]) CBP2 = 0x3f;
	if (QuantMType2[MType2])
	{
		UseQuant2=MQuant2;
		GQuant2=MQuant2;
	}
	else UseQuant2=GQuant2;
	for(c=0;c<6;c++)
	{
		j=BlockJ2[c];
		input = &inputbuf2[c][0];
		if ((CBP2 & bit_set_mask2[5-c])&&(TCoeffMType2[MType2]))
		{
			if (CBPMType2[MType2])
				CBPDecodeAC2(0,input);		      
			else
			{
				*input = DecodeDC2();
				DecodeAC2(1,input);
			}
			if (Loud2 > TALK)
			{
				printf("Cooked Input\n");
				//PrintMatrix(input);
			}			
			IZigzagMatrix2(input,output2);
			if (IntraMType2[MType2])
				ICCITTFlatQuantize2(output2,8,UseQuant2);
			else
				ICCITTQuantize2(output2,UseQuant2,UseQuant2);
			DefaultIDct2(output2,input);
		}
		else  
			for(x=0;x<64;x++) 
				input[x]=0; 
	}
	return(0);
}

/*BFUNC

PrintImage() prints the image structure to stdout.

EFUNC*/

//void PrintImage()
//{
//  BEGIN("PrintImage");
//
//  printf("*** Image ID: %x ***\n",CImage2);
//  if (CImage2)
//    {
//      if (CImage2->StreamFileName)
//	{
//	  printf("StreamFileName %s\n",CImage2->StreamFileName);
//	}
//      printf("InternalMode: %d   Height: %d   Width: %d\n",
//	     CImage2->p64Mode,CImage2->Height,CImage2->Width);
//    }
//}

/*BFUNC

PrintFrame() prints the frame structure to stdout.

⌨️ 快捷键说明

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