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

📄 avi2mpg1.c

📁 avi2mpg1_src 中包含了mpeg1编码的源程序
💻 C
📖 第 1 页 / 共 2 页
字号:
            if(thisPfi.dwWidth>4095)
	        {
		        fprintf(stderr, "\nHorizontal width must < 4096!");
	    	    err = 1;
	        }
	        if(thisPfi.dwHeight>4095)
	        {
		        fprintf(stderr, "\nVerical height must be < 4096!");
		        err = 1;
	        }
        }
        else
        {
            if (thisPfi.dwWidth != masterPfi.dwWidth)
            {
                fprintf(stderr, "\nWidth of all AVI files must match!");
                err=1;
            }
            if (thisPfi.dwHeight != masterPfi.dwHeight)
            {
                fprintf(stderr, "\nHeight of all AVI files must match!");
                err=1;
            }
            if (fabs(fps - (float)masterPfi.dwRate/(float)masterPfi.dwScale) > 0.1)
            {
                fprintf(stderr, "\nFrame rate must be the same for all AVI files!");
                err=1;
            }
        }

	    if(err)
		    exit(11);

	    fprintf(stderr, "\nTotal Video Frames = %u, ", thisPfi.dwLength);
	    fprintf(stderr, "Length = %u sec.", 
			    			(unsigned int)thisPfi.dwLength/(int)(fps+.5));

        if (j==0)
            nextFileFrames[j]=thisPfi.dwLength;
        else
            nextFileFrames[j]=thisPfi.dwLength+nextFileFrames[j-1];


	    if (lpbi->biCompression != BI_RGB)
	    {
		    fprintf(stderr, "\ncan not handle compressed DIBs from codec!\n");
		    err = 1;
	    }

	    if(!video_only)
	    {

		    if (AVIFileGetStream(pfile, &pAudioStreams[j], streamtypeAUDIO, 0))
		    {
		    	fprintf(stderr, "\nCould not open avi audio stream!");
		    	fprintf(stderr, "\nIf this avi has no audio, or you want to generate an");
		    	fprintf(stderr, "\nmpeg video only stream, use the -n option.");
		    	exit(12);
	    	}

		    if (AVIStreamInfo(pAudioStreams[j], &pAudioStreamInfo[j], sizeof(pAudioStreamInfo[j])))
		    {
			    fprintf(stderr, "\nCould not retrieve avi audio stream info!");
			    exit(13);
		    }

		    AVIStreamReadFormat(pAudioStreams[j], 0, 0, &length); // get length of format data
		    if(AVIStreamReadFormat(pAudioStreams[j], 0, &pWavFormat, &length))
		    {
			    fprintf(stderr, "\nCould not retrieve avi audio format info!");
			    exit(14);
		    }

		    fprintf(stderr, "\nAudio Channels = %i, ", pWavFormat.nChannels);
		    fprintf(stderr, "Sample rate = %6.3f KHz, bits per sample = %u\n",
							    (float)pWavFormat.nSamplesPerSec/1000.0, pWavFormat.wBitsPerSample);

            if (j==0)
                nextFileSamples[j]=pAudioStreamInfo[j].dwLength;
            else
                nextFileSamples[j]=pAudioStreamInfo[j].dwLength+nextFileSamples[j-1];

		    if(vcd_parm&&(pWavFormat.nChannels!=2))
		    {
			    fprintf(stderr, "\n for Video CD audio must be stereo!\n"); 
			    err = 1;
		    }

		    if (pWavFormat.wFormatTag != WAVE_FORMAT_PCM)
		    {
			    fprintf(stderr, "\ninput avi audio must be uncompressed PCM!\n");
			    err = 1;
		    }

		    if ((pWavFormat.nSamplesPerSec != 11025) && (pWavFormat.nSamplesPerSec != 22050) && (pWavFormat.nSamplesPerSec != 44100))
		    {
			    fprintf(stderr, "\ninput avi audio sample rate must = 11.025, 22.05, or 44.1 kHz!\n");
			    err = 1;
		    }
		    if ((pWavFormat.wBitsPerSample != 16) && (pWavFormat.wBitsPerSample != 8))
		    {
			    fprintf(stderr, "\ninput avi audio must be 8 or 16 bit!, this file is %i bits\n", pWavFormat.wBitsPerSample);
			    err = 1;
		    }
	    }
	    else
		    fprintf(stderr, "\n");

    }

	if (err == 1)
		exit(15);

    totalFrames=nextFileFrames[numAviFiles-1];

	StripPath(basefilename, basepathname);

	if(mpegfilename[0] != '\0') 
	{
		StripExtension(testfilename, mpegfilename);
		StripPath(intfilename, testfilename); 
	}
	else 
	{
		StripPath(intfilename, basepathname);
	}

	if(use_v_param_file)
	{
		// if parameter file used, add .par extension if none supplied
		StripExtension(testfilename, video_param_filename);
		if(strlen(video_param_filename) == strlen(testfilename))
			strcat(video_param_filename, ".par");
	}

	// call avi2m1v to encode video stream
	strcpy(VideoTempFile, intfilename);
	strcat(VideoTempFile, ".m1v");
	fprintf(stderr, "encoding video stream to %s", VideoTempFile);
	avi2m1v(VideoTempFile);
    for(j=0;j<numAviFiles;j++)
	    AVIStreamGetFrameClose(pget[j]);
	fprintf(stderr, "                                                                              \r");

	// if there were errors during encoding of video, print them
	if(vbv_ovflow!=0)
		fprintf(stderr, "*** WARNING: VBV_BUFFER OVERFLOWED %u TIMES.\n", vbv_ovflow);
	if(vbv_unflow!=0)
		fprintf(stderr, "*** WARNING: VBV_BUFFER UNDERFLOWED %u TIMES.\n", vbv_unflow);
	if(bad_frame_count)
		fprintf(stderr, "*** WARNING: %u CORRUPT VIDEO FRAMES REPLACED BY PREVIOUS GOOD FRAME!\n", bad_frame_count);
	fprintf(stderr, "video encoding complete,");
	time(&current_time);
	el_time = current_time - start_time;
	if(el_time <= 60)
		fprintf(stderr, " elapsed time = %i seconds\n", el_time);
	else if(el_time < 3600)
		fprintf(stderr, " elapsed time = %i minutes\n", (el_time/60)+1);
	else
		fprintf(stderr, " elapsed time = %i hour(s) %i minute(s)\n", el_time/3600, (el_time%3600)/60);
	fprintf(stderr, "average seconds per frame = %5.3f\n", (float)el_time/(float)(totalFrames*frame_repl));

	if(!video_only)
	{
		// call avi2mp2 to encode audio stream

		strcpy(AudioTempFile, intfilename);
		strcat(AudioTempFile, ".mp2");
		fprintf(stderr, "encoding audio stream to %s", AudioTempFile);
		fprintf(stderr, " using bitrate of %u:\n", audio_bitrate_parm*1024);
		avi2mp2(AudioTempFile);
		fprintf(stderr, "\raudio stream encoding complete.\n");
		if(bad_audio_count)
			fprintf(stderr, "***WARNING: %u CORRUPT AUDIO SAMPLES REPLACED BY 0 DATA!\n", bad_audio_count);
	}

	AVIFileRelease(pfile);
	AVIFileExit();

	if(!video_only)
	{
		// check if user supplied outputfile (if any) has an extension,
		// if not, add .mpg
		if(mpegfilename[0] != '\0')
		{
			StripExtension(testfilename, mpegfilename);
			if(strlen(mpegfilename) == strlen(testfilename))
				strcat(mpegfilename, ".mpg");
		}

		// call mplex to create .mpg file
		if(mpegfilename[0] == '\0')
		{
			strcpy(mpegfilename, basefilename); // no output file name supplied,
			strcat(mpegfilename, ".mpg");       // use input filename with .mpg
		}
		fprintf(stderr, "multiplexing video and audio streams to %s:\n", mpegfilename);
		mplex(VideoTempFile, AudioTempFile, mpegfilename);
		fprintf(stderr, "\rmultiplexing complete.\n");
		if(video_end_early)
			fprintf(stderr, "***WARNING: video stream ended early!\n");
		if(audio_end_early)
			fprintf(stderr, "***WARNING: audio stream ended early!\n");
		if(video_time_out)
			fprintf(stderr, "***WARNING: video stream timed out %u times.\n", video_time_out);
		if(audio_time_out)
			fprintf(stderr, "***WARNING: audio strean timed out %u times.\n", audio_time_out);

		// delete temporary files.
		fprintf(stderr, "deleting temporary video and audio files.\n");
		DeleteFile(VideoTempFile);
		DeleteFile(AudioTempFile);
	}

	return(0);
}


void usage()
{
		fprintf(stderr, "\navi2mpg1 - avi to mpeg-1 encoder version %s", VERSION);
		fprintf(stderr, "\nCopyright (C) 1997, 1998, 1999 by John Schlichther");
		fprintf(stderr, "\navi2mpg1 comes with ABSOLUTELY NO WARRANTY");
		fprintf(stderr, "\nThis is free software, and you are welcome to redistribute it");
		fprintf(stderr, "\nunder certain conditions; see file COPYING for details.");
		fprintf(stderr, "\n\n usage: avi2mpg1 [-options] inputfile.avi [inputfile2.avi ...] [outputfile.mpg]");
		fprintf(stderr, "\n  where:  -b byterate (total stream - default 150 KBytes/s)");
		fprintf(stderr, "\n          -s bitrate (total stream - default 1200 Kbits/s)");
		fprintf(stderr, "\n          -a bitrate (audio - default 128Kbits/s)");
		fprintf(stderr, "\n          -l layer (audio - default 2)");
		fprintf(stderr, "\n          -j  (joint stereo)");
		//fprintf(stderr, "\n          -v  (use Video-cd parameters - overides -b, -s, -a, -l, -n, -c)");
		fprintf(stderr, "\n          -p filename.par  video encoding parameter file (optional)");
		fprintf(stderr, "\n          -e ignore avi file errors");
		fprintf(stderr, "\n          -n generate video (.m1v) stream only");
		fprintf(stderr, "\n          -y vbv_buffer_size");
        fprintf(stderr, "\n          -m motion search magnitude, range 0 - 4, default 0");
		fprintf(stderr, "\n          -c horz_size vert_size cropping");
		fprintf(stderr, "\n          -f frame_rate_code 1->5, force frame rate");
		fprintf(stderr, "\n          -h n write sequence header every n-th GOP");
		fprintf(stderr, "\n          -x suppress usage of MMX extensions");
		fprintf(stderr, "\n          -d use double precision math (slower)");
		fprintf(stderr, "\n          -r remove noise from the input video (slow!)");
		fprintf(stderr, "\n          -q fast quantization (less accurate)");
		fprintf(stderr, "\n          -t fast motion compensation level, range 0 - 3, default 0 (normal)");
		fprintf(stderr, "\n          inputfile.avi  any valid avi file to encode");
		fprintf(stderr, "\n          outputfile.mpg  must be specified if there is more than 1 input file\n");
		exit(16);
}

void videocd()
{
		fprintf(stderr, "\nStarting with version 1.9 of avi2mpg1, the -v option");
		fprintf(stderr, "\nhas been disabled. Numerous issues with the -v option");
		fprintf(stderr, "\nprevented it from generating an accurate VideoCD file.");
		fprintf(stderr, "\n");
		fprintf(stderr, "\nA new program, avi2vcd has been written whose only");
		fprintf(stderr, "\npurpose is the generation of VideoCD 2.0 compliant files.");
		fprintf(stderr, "\nThe output of avi2vcd has been used to sucessfully create");
		fprintf(stderr, "\nVideoCD's which have been played back on a DVD player");
		fprintf(stderr, "\n");
		fprintf(stderr, "\nYou can download the latest version of avi2vcd from:");
		fprintf(stderr, "\n         www.mnsi.net/~jschlic1");
		fprintf(stderr, "\n");
		exit(16);
}

unsigned int abitrate[2][14] = {
          {32,64,96,128,160,192,224,256,288,320,352,384,416,448},
          {32,48,56,64,80,96,112,128,160,192,224,256,320,384},
        };

int ValidBitrate(layr, bRate)   /* return 1 if legal bit rate for layer, 0 otherwise */
unsigned int layr;           /* 1 or 2 */
unsigned int bRate;          /* legal rates from 32 to 448 */
{
int     index = 0;
int     found = 0;

    while(!found &&(index < 14))
	{
        if(abitrate[layr-1][index] == bRate)
            found = 1;
        else
            ++index;
    }
    if(found)
        return(1);
    else
        return(0);
}


void StripExtension(char *base, char *source)
{
	int i, period = 0, backslash = 0;

	strcpy(base, source);

	i = strlen(base);
	while((i > 0) && !period && !backslash)
	{
		i--;
		if(base[i] == '.')
			period = 1;
		else if (base[i] == '\\')
			backslash = 1;
	}

	if(i == 0)
		return; // scanned all the way to begining, there is no extension

	if(backslash)
		return; // backslash found before period, there can't be an extension

	// if we're not at i=0, and we didn't find a backslash,
	// then we have to have found a period!

	base[i] = '\0'; // strip off extension
}
//
// StripPath - copy source filename to base and strip
//                 path if any.
//
void StripPath(char *base, char *source)
{
	int i, backslash = 0;
	char *pSource;

	pSource = source;

	i = strlen(source);
	while((i > 0) && !backslash)
	{
		i--;
		if ((source[i] == '\\')||(source[i] == ':'))
			backslash = 1;
	}

	if(backslash)
	{
		pSource = pSource + i + 1;
		strcpy(base, pSource);
	}
	else
		strcpy(base, source);
	return;
}

//测试CPU是否支持MMX
void TestCPU(void)
{

	cpu_MMX = 0;
	cpu_3DNow = 0;


	#define cpuid __asm _emit 0x0F __asm _emit 0xA2
    __asm
    {
		pushfd                          ; 
		pop   eax                       ; 
		mov   ebx, eax                  ; 
		xor   eax, 0x00200000           ; 
		push  eax                       ; 
		popfd                           ; 
		pushfd                          ; 
		pop   eax                       ; 
		cmp   eax, ebx                  ; 
		jz    NO_CPUID                  ; 

	    mov   eax, 0x1
        cpuid
	    test  edx, 0x800000
	    je    NO_MMX
	    mov   cpu_MMX, 0x1


		mov   eax, 0x0
		cpuid
		mov eax, 0x68747541             ; reversed ASCII of "Auth"
		cmp eax, ebx
		jne NO_3DNOW
		mov eax, 0x69746E65             ; reversed ASCII of "enti"
		cmp eax, edx
		jne NO_3DNOW
		mov eax, 0x444D4163             ; reversed ASCII of "cAMD"
		cmp eax, ecx
		jne NO_3DNOW


		mov eax, 0x80000000           ; 
		cpuid
		cmp eax, 0x80000000
		jbe NO_3DNOW
		mov eax, 0x80000001
		cpuid
		test edx, 0x80000000          ; 
		jz NO_3DNOW                   ; 
		mov cpu_3DNow, 0x1            ; 

NO_MMX:
NO_3DNOW:
NO_CPUID:
	}
}

⌨️ 快捷键说明

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