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

📄 main.c

📁 文件内包含H.263视频编码算法和解码算法2个文件
💻 C
📖 第 1 页 / 共 2 页
字号:
	{
		memcpy(Pict->lum+i*pels,buf,pels);
		buf += yScanWidth/2;
	}
	buf = uTemp[bufNum];
	for( i=0; i<lines/2; i++ )
	{
		memcpy(Pict->Cb+i*pels/2,buf,pels/2);
		buf += uvScanWidth;
	}
	buf = vTemp[bufNum];
	for( i=0; i<lines/2; i++ )
	{
		memcpy(Pict->Cr+i*pels/2,buf,pels/2);
		buf += uvScanWidth;
	}
	
	icpDisplay();
	
	return(Pict);
}
void main(Int argc, Char **argv){		PictImage *prev_image = NULL;	PictImage *curr_image = NULL;	PictImage *curr_recon = NULL;	PictImage *prev_recon = NULL;		/* PB-frame specific */	PictImage *B_recon = NULL;	PictImage *B_image = NULL;		Pict *pic = (Pict *)malloc(sizeof(Pict));	unsigned char *image;	FILE *cleared;		int i;	float mean_frame_rate, ref_frame_rate, frame_rate, seconds;	int first_loop_finished=0;	int total_frames_passed, PPFlag = 0, targetrate;	int frames,bframes,pframes,wcopies,icopies, write_repeated,pdist=0,bdist=0;	int start, end, frame_no, writediff;  	int first_frameskip, chosen_frameskip, orig_frameskip, frameskip;	int QP,QPI;	Bits *bits = (Bits *)malloc(sizeof(Bits));	Bits *total_bits = (Bits *)malloc(sizeof(Bits));	Bits *intra_bits = (Bits *)malloc(sizeof(Bits));	Results *res = (Results *)malloc(sizeof(Results));	Results *total_res = (Results *)malloc(sizeof(Results));	Results *b_res = (Results *)malloc(sizeof(Results));	char *seqfilename = (char *)malloc(sizeof(char)*20);	char *streamname = (char *)malloc(sizeof(char)*20);	char *outputfile =  (char *)malloc(sizeof(char)*20);	char *diff_filename=DEF_DIFFILENAME;	char *tracefile =  (char *)malloc(sizeof(char)*20);
	#ifndef OFFLINE_RATE_CONTROL	float DelayBetweenFramesInSeconds;	int CommBacklog;#else	PictImage *stored_image = NULL;	int start_rate_control;#endif		extern int arith_used;		fprintf (stdout,"\nTMN (H.263) coder version 1.7, Copyright (C) 1995, 1996 Telenor R&D.\n");	fprintf (stdout, "TMN comes with ABSOLUTELY NO WARRANTY; This is free software, and \nyou are welcome to redistribute it under certain conditions; \nSee accompanying COPYING file for details.\n\n");		headerlength = DEF_HEADERLENGTH;
		/* Default variable values */	advanced = DEF_ADV_MODE;	syntax_arith_coding = DEF_SAC_MODE;	pic->unrestricted_mv_mode = DEF_UMV_MODE;	mv_outside_frame = DEF_UMV_MODE || DEF_ADV_MODE;	long_vectors = DEF_UMV_MODE;	pb_frames = DEF_PBF_MODE;		QP = DEF_INTER_QUANT;	QPI = DEF_INTRA_QUANT; 	pic->BQUANT = DEF_BQUANT; 	pic->source_format = DEF_CODING_FORMAT;		ref_frame_rate = (float)DEF_REF_FRAME_RATE;	chosen_frameskip = DEF_FRAMESKIP + 1;	orig_frameskip = DEF_ORIG_SKIP + 1;#ifdef OFFLINE_RATE_CONTROL	start_rate_control = DEF_START_RATE_CONTROL;#else	pic->target_frame_rate = (float)DEF_TARGET_FRAME_RATE;#endif		seqfilename[0] = '\0';	strcpy(streamname, DEF_STREAMNAME);	strcpy(outputfile, DEF_OUTFILENAME);		writediff = DEF_WRITE_DIFF;	trace = DEF_WRITE_TRACE;	write_repeated = DEF_WRITE_REPEATED;	pic->seek_dist = DEF_SEEK_DIST;	pic->use_gobsync = DEF_INSERT_SYNC;	start = DEF_START_FRAME;	end = DEF_STOP_FRAME;		targetrate = 0; 	/* default is variable bit rate (fixed quantizer) will be used */		frames = 0;	pframes = 0;	bframes = 0;	total_frames_passed = 0;	pic->PB = 0;	wcopies = icopies = 1;		pic->TR = 0;	pic->QP_mean = (float)0.0;	
	/************************************/
	
	DPsize(64000);
    DP((Header));
    DP(("Running on ")); tmHelpReportSystem(Null);
    printf(Header);
    printf("Running on "); tmHelpReportSystem(stdout);
	
#ifndef __TCS_nohost__
    viCheckArgcv(argc, argv);
#else
    argc = 0;
    argv = NULL;
#endif
	
    AllocBuffers();
    SetupICP();
    SetupVI();
	
    if (err = viStart(viInst))
        my_abort("viStart", err);
	
	/***************************************************************/
	
	start = 0;
	end = LOOPCOUNT;	
    memcpy(streamname, "streamout.263", 14);
	pic->source_format = 2;
	
	chosen_frameskip = 1;
	orig_frameskip = 1;
	pic->seek_dist = 4;
	
	/*	trace = ON;
    write_repeated = ON;
	writediff = ON;
    outputfile = "output.263";
	*/
	pic->unrestricted_mv_mode = OFF;
	mv_outside_frame = OFF;
	long_vectors = OFF;
	syntax_arith_coding = OFF;
	advanced = OFF;
	pb_frames = OFF;
	
	/*********************************************************************/
	
	total_temp = clock();
		switch (pic->source_format) 
	{	case (SF_SQCIF):		fprintf(stdout, "Encoding format: SQCIF (128x96)\n");		pels = 128;		lines = 96;		break;	case (SF_QCIF):		fprintf(stdout, "Encoding format: QCIF (176x144)\n");		pels = 176;		lines = 144;		break;	case (SF_CIF):		fprintf(stdout, "Encoding format: CIF (352x288)\n");		pels = 352;		lines = 288;		break;	case (SF_4CIF):		fprintf(stdout, "Encoding format: 4CIF (704x576)\n");		pels = 704;		lines = 576;		break;	case (SF_16CIF):		fprintf(stdout, "Encoding format: 16CIF (1408x1152)\n");		pels = 1408;		lines = 1152;		break;	default:		fprintf(stderr,"Illegal coding format\n");		exit(-1);	}	cpels = pels/2;	#ifndef OFFLINE_RATE_CONTROL	/* rate control variables */	pic->bit_rate = targetrate;	pic->src_frame_rate = (int)(ref_frame_rate / orig_frameskip);	DelayBetweenFramesInSeconds = (float) 1.0/(float)pic->src_frame_rate;	InitializeRateControl();#endif		if (QP == 0 || QPI == 0) 
	{		fprintf(stderr,"Warning:");		fprintf(stderr,"QP is zero. Bitstream will not be correctly decodable\n");	}		if (ref_frame_rate != 25.0 && ref_frame_rate != 30.0) 
	{		fprintf(stderr,"Warning: Reference frame rate should be 25 or 30 fps\n");	}		frame_rate =  ref_frame_rate / (float)(orig_frameskip * chosen_frameskip);#ifdef OFFLINE_RATE_CONTROL	fprintf(stdout,"Encoding frame rate  : %.2f\n", frame_rate);#else	if (pic->bit_rate == 0)		fprintf(stdout,"Encoding frame rate  : %.2f\n", frame_rate);	else		fprintf(stdout,"Encoding frame rate  : variable\n");#endif	fprintf(stdout,"Reference frame rate : %.2f\n", ref_frame_rate);	fprintf(stdout,"Orig. seq. frame rate: %.2f\n\n", 		ref_frame_rate / (float)orig_frameskip);		/* Open stream for writing */	streamfile = fopen (streamname, "wb");	if (streamname == NULL) 
	{		fprintf(stderr,"Unable to open streamfile\n");		exit(-1);	}	 	/* Initialize bitcounters */	initbits ();		/* Clear output files */	if ((cleared = fopen(outputfile,"wb")) == NULL) 
	{		fprintf(stderr,"Couldn't open outputfile: %s\n",outputfile);		exit(-1);	}	else 		fclose(cleared);	if (writediff) 
	{		if ((cleared = fopen(diff_filename,"wb")) == NULL) 
		{			fprintf(stderr,"Couldn't open diff-file: %s\n",diff_filename);			exit(-1);		}		else 			fclose(cleared);	} 	/* Intra image */		fprintf(stderr,"Coding...\n");	/*  ReadImage(image);*/
	curr_image = ReadImage();	pic->picture_coding_type = PCT_INTRA;	pic->QUANT = QPI;		curr_recon = CodeOneIntra(curr_image, QPI, bits, pic);		bits->header += alignbits (); /* pictures shall be byte aligned */		fprintf(stdout,"Finished INTRA\n");	
	memcpy(intra_bits,bits,sizeof(Bits));	ZeroBits(total_bits);	ZeroRes(total_res);	ZeroRes(b_res);		/* number of seconds to encode */	seconds = (end - start + chosen_frameskip) * orig_frameskip/ ref_frame_rate;		/* compute first frameskip */#ifdef OFFLINE_RATE_CONTROL	first_frameskip = chosen_frameskip;	frameskip = chosen_frameskip;#else	CommBacklog = intra_bits->total - (int)(DelayBetweenFramesInSeconds * pic->bit_rate);		if (pic->bit_rate == 0) 
	{		frameskip = chosen_frameskip;	}	else 
	{  /* rate control is used */		frameskip = 1;		while ( (int)(DelayBetweenFramesInSeconds*pic->bit_rate) <= CommBacklog) 
		{			CommBacklog -= (int) ( DelayBetweenFramesInSeconds * pic->bit_rate );			frameskip += 1;		}	}	first_frameskip = frameskip;#endif		if (first_frameskip > 256)		fprintf(stderr,"Warning: frameskip > 256\n");		pic->picture_coding_type = PCT_INTER;	pic->QUANT = QP;	bdist = chosen_frameskip;			if (write_repeated) 		icopies = chosen_frameskip;		/*  for (i = 0; i < icopies; i++)		WriteImage(curr_recon,outputfile); 
	*/  /* write wcopies frames to disk */	
		/******************* Main loop ******************************/
		for (frame_no = start + first_frameskip;  frame_no <= end;  frame_no += frameskip) 
	{		prev_image = curr_image;		prev_recon = curr_recon;				/* Set QP to pic->QUANT from previous encoded picture */		QP = pic->QUANT;				if (!PPFlag) 
		{			curr_image = ReadImage();		  		}		else 
		{		/* PPFlag is set when the second of the two P-frames 			is due to be coded */#ifdef OFFLINE_RATE_CONTROL			curr_image = stored_image;#else			curr_image = ReadImage();#endif			pic->PB = 0;			PPFlag = 0;		}				/* Temporal Reference is the distance between encoded frames compared		the reference picture rate which is 25.0 or 30 fps */		pic->TR += (( (frameskip+(pic->PB?pdist:0)) *orig_frameskip) % 256); 		if (frameskip+(pic->PB?pdist:0) > 256)			fprintf(stdout,"Warning: frameskip > 256\n");	  				frames += (pic->PB ? 2: 1);		bframes += (pic->PB ? 1 : 0);		pframes += 1;				curr_recon = InitImage(pels*lines); 				CodeOneOrTwo(curr_image, B_image, prev_image, prev_recon, 			QP, (bdist+pdist)*orig_frameskip, bits, pic, 			B_recon, curr_recon);		
		if (targetrate != 0)			fprintf(stdout,"Inter QP: %d\n", QP);		fflush(stdout);				bits->header += alignbits ();  /* pictures shall be byte aligned */		AddBitsPicture(bits);		AddBits(total_bits, bits);		#ifndef OFFLINE_RATE_CONTROL		if (pic->bit_rate != 0 && pic->PB)			CommBacklog -= (int) 			( DelayBetweenFramesInSeconds*pic->bit_rate ) * pdist;				if (pic->bit_rate != 0)
		{			UpdateRateControl(bits->total);						CommBacklog += bits->total;			frameskip = 1;			CommBacklog -= (int) 				(frameskip * DelayBetweenFramesInSeconds *pic->bit_rate);						while ( (int)(DelayBetweenFramesInSeconds*pic->bit_rate) <= CommBacklog)			{				CommBacklog -= (int) ( DelayBetweenFramesInSeconds * pic->bit_rate );				frameskip += 1;			}		}#else		/* Aim for the targetrate with a once per frame rate control scheme */		if (targetrate != 0) 			if (frame_no - start > (end - start) * start_rate_control/100.0) 								pic->QUANT = FrameUpdateQP(total_bits->total + intra_bits->total,				bits->total / (pic->PB?2:1),				(end-frame_no) / chosen_frameskip + PPFlag,				QP, targetrate, seconds);			frameskip = chosen_frameskip;#endif						if (write_repeated) 				wcopies = (pb_frames ? bdist : frameskip);				/*		  for (i = 0; i < wcopies; i++)				WriteImage(curr_recon,outputfile); 
			*/		  /* write wcopies frames to disk */			
			FreeImage(prev_image);			FreeImage(prev_recon);			fflush(stdout);	}
		/**************************** end of main loop **************************/		/* Closing files */	fclose (streamfile);	
#if 0
	if( (i + 1) % 100 == 0 )
		viSetSaturation(viInst, 0);
	if( (i + 1) % 200 == 0 )
		viSetHue(viInst, 10);
	if( (i + 1) % 300 == 0 )
		viSetContrast(viInst, 32);
	if( (i + 1) % 400 == 0 )
		viSetBrightness(viInst, 32);
#endif
	if (err = viStop(viInst))
		my_abort("viStop", err);
	
	printf("vitest: Completed %d frames\n", LOOPCOUNT);
	
	if (err = icpClose(icpInst))
		my_abort("icpClose", err);
	
	if (err = viClose(viInst))
		my_abort("viClose", err);
	
	FreeBuffer();
		/* Free memory */	FreeImage(curr_recon);	FreeImage(curr_image);	free(streamname);	free(seqfilename);	free(outputfile);	free(tracefile);	free(bits);	free(total_bits);	free(intra_bits);	free(res);	free(total_res);	free(b_res);	free(pic);
	
	total_time += clock()-total_temp;
	
	printf("\n\n");
	printf("Total time is %d !\n", total_time );
	
	exit(0);}int NextTwoPB(PictImage *next2, PictImage *next1, PictImage *prev,               int bskip, int pskip, int seek_dist){	int adv_is_on = 0, mof_is_on = 0, lv_is_on = 0;	int psad1=0, psad2=0, bsad=0, psad;	MotionVector *MV[6][MBR+1][MBC+2];	MotionVector *mvp=NULL, *mvbf=NULL, *mvbb=NULL;	int x,y;	int i,j,k,tmp;		/* Temporarily disable some options to simplify motion estimation */	if (advanced) 
	{		advanced = OFF;		adv_is_on = ON;	}	if (mv_outside_frame) 
	{		mv_outside_frame = OFF;		mof_is_on = ON;	}	if (long_vectors) 
	{		long_vectors = OFF;		lv_is_on = ON;	}		for (j = 1; j <= (lines>>4); j++) 
	{		for (i = 1; i <= (pels>>4); i++) 
		{			for (k = 0; k < 3; k++) 
			{				MV[k][j][i] = (MotionVector *)calloc(1,sizeof(MotionVector));			}						mvbf = (MotionVector *)malloc(sizeof(MotionVector));			mvbb = (MotionVector *)malloc(sizeof(MotionVector));						psad = 0;			psad1 = 0;			psad2 = 0;			bsad = 0;						/* Integer motion estimation */			for ( j = 1; j < lines/MB_SIZE - 1; j++) 
			{				for ( i = 1; i < pels/MB_SIZE - 1 ; i++)
				{					x = i*MB_SIZE;					y = j*MB_SIZE;										MotionEstimation(next2->lum,prev->lum,x,y,0,0,seek_dist,MV,&tmp);					if (MV[0][j+1][i+1]->x == 0 && MV[0][j+1][i+1]->y == 0)						MV[0][j+1][i+1]->min_error += PREF_NULL_VEC;					/* not necessary to prefer zero vector here */					memcpy(MV[2][j+1][i+1],MV[0][j+1][i+1],sizeof(MotionVector));										/* computes sad(prev <- next1) */					MotionEstimation(next1->lum,prev->lum,x,y,0,0,seek_dist,MV,&tmp);					if (MV[0][j+1][i+1]->x == 0 && MV[0][j+1][i+1]->y == 0)						MV[0][j+1][i+1]->min_error += PREF_NULL_VEC;					memcpy(MV[1][j+1][i+1],MV[0][j+1][i+1],sizeof(MotionVector));										/* computes vectors for (next1 <- next2) */					MotionEstimation(next2->lum,next1->lum,x,y,0,0,seek_dist,MV,&tmp);					if (MV[0][j+1][i+1]->x == 0 && MV[0][j+1][i+1]->y == 0)						MV[0][j+1][i+1]->min_error += PREF_NULL_VEC;										/* scales vectors for (prev <- next2 ) */					mvp = MV[2][j+1][i+1];					mvbf->x =   bskip * mvp->x / (bskip + pskip);					mvbb->x = - pskip * mvp->x / (bskip + pskip);					mvbf->y =   bskip * mvp->y / (bskip + pskip);					mvbb->y = - pskip * mvp->y / (bskip + pskip);										psad1 += MV[0][j+1][i+1]->min_error;					psad2 += MV[1][j+1][i+1]->min_error;					psad +=  mvp->min_error;										/* computes sad(prev <- next1 -> next2) */					bsad += SAD_MB_Bidir(next1->lum + x + y*pels,						next2->lum + x + mvbb->x + (y + mvbb->y)*pels,						prev->lum + x + mvbf->x + (y + mvbf->y)*pels,						pels, INT_MAX);				}			}
		}
	}		for (j = 1; j <= (lines>>4); j++)
	{		for (i = 1; i <= (pels>>4); i++) 
		{			for (k = 0; k < 3; k++) 
			{				free(MV[k][j][i]);
			}
		}
	}
		free(mvbf);	free(mvbb);		/* restore advanced parameters */	advanced = adv_is_on;	mv_outside_frame = mof_is_on;	long_vectors = lv_is_on;		/* do the decision */	if (bsad < (psad1+psad2)/2)		fprintf(stdout,"Chose PB - bsad %d, psad %d\n", 		bsad, (psad1+psad2)/2);	else		fprintf(stdout,"Chose PP  - bsad %d, psad %d\n", 		bsad, (psad1+psad2)/2);		if (bsad < (psad1 + psad2)/2)		return 1;	else 		return 0;}

⌨️ 快捷键说明

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