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

📄 encoder.c

📁 AAC音频解码算法程序
💻 C
📖 第 1 页 / 共 2 页
字号:
		BsPutBit(bitHeader,0,1);
		// element_list
		BsPutBit(bitHeader,(as->channels == 2),1);
		BsPutBit(bitHeader,0,4);

		ByteAlign(bitHeader);
		// Comment
		BsPutBit(bitHeader,0,8);

		bits = BsBufferNumBit(bitHeader);

		// Copy bitBuf into bitBuffer here
		bytes = (int)((bits+7)/8);
		for (i = 0; i < bytes; i++)
			headerBuf[i] = bitHeader->data[i];
		BsClose(bitHeader);
	}

	for (ch=0; ch < as->channels; ch++)
		if(as->inputBuffer[ch]) free(as->inputBuffer[ch]);
	if(as->inputBuffer) free(as->inputBuffer);
	if (as) free(as);

	return FNO_ERROR;
}

faacVersion *faacEncodeVersion(void)
{
	faacVersion *faacv = malloc(sizeof(faacVersion));

	faacv->DLLMajorVersion = 2;
	faacv->DLLMinorVersion = 20;
	faacv->MajorVersion = 0;
	faacv->MinorVersion = 60;
	strcpy(faacv->HomePage, "http://www.slimline.net/aac/");

	return faacv;
}

#ifdef FAAC_DLL

BOOL APIENTRY DllMain(HANDLE hModule, 
                      DWORD  ul_reason_for_call, 
                      LPVOID lpReserved)
{
    return TRUE;
}

#else

/* You can download libsndfile from http://www.zip.com.au/~erikd/libsndfile/ */
#ifdef WIN32
#include <windows.h>
#else
#include <time.h>
#endif

#include "wave.h"

char *get_filename(char *pPath)
{
    char *pT;

    for (pT = pPath; *pPath; pPath++) {
        if ((pPath[0] == '\\' || pPath[0] == ':') && pPath[1] && (pPath[1] != '\\'))
            pT = pPath + 1;
    }

    return pT;
}

void combine_path(char *path, char *dir, char *file)
{
	/* Should be a bit more sophisticated
	   This code assumes that the path exists */

	/* Assume dir is allocated big enough */
	if (dir[strlen(dir)-1] != '\\')
		strcat(dir, "\\");
	strcat(dir, get_filename(file));
	strcpy(path, dir);
}

void usage(void)
{
	printf("Usage:\n");
	printf("faac.exe -options file ...\n");
	printf("Options:\n");
	printf(" -h    Shows this help screen.\n");
	printf(" -pX   AAC profile (X can be LOW, or MAIN (default).\n");
	printf(" -bX   Bitrate in kbps (in steps of 1kbps, min. 16kbps)\n");
	printf(" -pns  Use PNS (Perceptual Noise Substitution).\n");
	printf(" -is   Use intensity stereo coding.\n");
	printf(" -ms   Use mid/side stereo coding.\n");
	printf(" -nm   Don't use mid/side stereo coding.\n");
	printf("       The default for MS is intelligent switching.\n");
	printf(" -nt   Don't use TNS (Temporal Noise Shaping).\n");
	printf(" -np   Don't use LTP (Long Term Prediction).\n");
	printf(" -nh   No header will be written to the AAC file.\n");
	printf(" -oX   Set output directory.\n");
	printf(" -sX   Set output sampling rate.\n");
	printf(" -cX   Set cut-off frequency.\n");
	printf(" -r    Use raw data input file.\n");
	printf(" file  Multiple files can be given as well as wild cards.\n");
	printf("       Can be any of the filetypes supported by libsndfile\n");
	printf("       (http://www.zip.com.au/~erikd/libsndfile/).\n");
	printf("Example:\n");
	printf("       faac.exe -b96 -oc:\\aac\\ *.wav\n");
	return;
}

FILE *log_file;

int main(int argc, char *argv[])
{
	int readNumSample;

	short *sampleBuffer;
	unsigned char *bitBuffer = NULL;
	FILE *aacfile;
	FILE *wavefile;
	WAVE_INFO wave_info;
	int noSamples;
	int error;
	int bitBufSize;
	int curBitBufSize;
	int headerSize;
	int i, frames, cfr;
	int profile = MAIN_PROFILE;
	int no_header = 0;
	int use_IS = 0, use_MS = 0, use_TNS = 1, use_LTP = 1, use_PNS = 0;
	int cut_off = 0;
	int bit_rate = 96;
	int out_rate = 0;
	char out_dir[255];
	int out_dir_set = 0;
	int raw_audio = 0;
	char *argp;
	char *FileNames[200];
	int FileCount = 0;
	char filetopro[50] = "e:\\3.wav"; 
	int counter;

	faacAACStream *as;
	faacAACConfig ac;
	faacVersion *faacv;

	long begin, end;

	faacv = NULL;
	faacv = faacEncodeVersion();
	printf("FAAC cl (Freeware AAC Encoder)\n");
	printf("FAAC homepage: %s\n", faacv->HomePage);
	printf("Encoder engine version: %d.%d\n\n",
		faacv->MajorVersion, faacv->MinorVersion);
	if (faacv) free(faacv);


   FileCount=1;
   
   FileNames[0] = (char*)malloc((strlen(filetopro)+1)*sizeof(char));
   sprintf(FileNames[0], filetopro);

	if (FileCount == 0) {
		return 1;
	}
   
	for (i = 0; i < FileCount; i++) {
		char aac_fn[255];
		char *fnp;

		printf("0%\tBusy encoding %s.\r", FileNames[i]);

#ifdef WIN32
		begin = GetTickCount();
#else
		begin = clock();
#endif

/*		if (raw_audio) {
			wave_info.format =  SF_FORMAT_RAW;
			wave_info.format |= SF_FORMAT_PCM_BE;
			wave_info.channels = 2;
			wave_info.pcmbitwidth = 16;
			wave_info.samplerate = 44100;
		}*/

		wavefile = wave_open("e:\\3.wav"/*FileNames[i]*/, &wave_info);
		if (wavefile==NULL) {
			printf("Error while encoding %s.\n", FileNames[i]);
			continue;
		}

		frames = (int)(wave_info.samples/1024+0.5);

		if (out_dir_set)
			combine_path(aac_fn, out_dir, FileNames[i]);
		else
			strcpy(aac_fn, FileNames[i]);
		fnp = strrchr(aac_fn,'.');
		fnp[0] = '\0';
		strcat(aac_fn,".aac");

		aacfile = fopen("e:\\3.aac", "wb");
		log_file = fopen("e:\\log_file.txt", "wb");
		if (aacfile==NULL) {
			printf("Error while encoding %s.\n", FileNames[i]);
			continue;
		}
		if(log_file==NULL){
			printf("Error in creating log file\n");
		}

		ac.channels = wave_info.channels;
		ac.in_sampling_rate = wave_info.samplerate;
		ac.out_sampling_rate = out_rate ? out_rate : wave_info.samplerate;
		ac.bit_rate = bit_rate * 1000;
		ac.cut_off = cut_off ? cut_off : (ac.out_sampling_rate>>1);
		ac.profile = profile;
		ac.use_MS = use_MS;
		ac.use_IS = use_IS;
		ac.use_TNS = use_TNS;
		ac.use_LTP = use_LTP;
		ac.use_PNS = use_PNS;
		ac.write_header = !no_header;

		as = faacEncodeInit(&ac, &readNumSample, &bitBufSize, &headerSize);
		if (as == NULL) {
			printf("Error while encoding %s.\n", FileNames[i]);
			continue;
		}
		sampleBuffer = malloc(readNumSample*sizeof(short));

		bitBuffer = malloc((bitBufSize+100)*sizeof(char));

		if (headerSize > 0) {
			memset(bitBuffer, 0, headerSize*sizeof(char));
			// Skip headerSize bytes
			// They should be written after calling faacEncodeFree
			fwrite(bitBuffer, 1, headerSize, aacfile);
		}

		cfr = 0;
		counter=0;
		// Keep encoding frames until the end of the audio file
		do {
			printf("this is %d-th frame\n", cfr);
			cfr++;


			noSamples = wave_get(wavefile, sampleBuffer, readNumSample, wave_info);

			error = faacEncodeFrame(as, sampleBuffer, noSamples, bitBuffer, &curBitBufSize);
			if (error == FERROR) {
				printf("Error while encoding %s.\n", FileNames[i]);
				break;
			}

			fwrite(bitBuffer, 1, curBitBufSize, aacfile);
			
			printf("%.2f%%\tBusy encoding %s.\r", min(((double)cfr/(double)frames)*100,100),FileNames[i]);

		} while (noSamples == readNumSample);

		if (error == FERROR) {
			continue;
		}

		error = faacEncodeFinish(as, bitBuffer, &curBitBufSize);
		if (error == FERROR) {
			printf("Error while encoding %s.\n", FileNames[i]);
			continue;
		}

		fwrite(bitBuffer, 1, curBitBufSize, aacfile);

		wave_close(wavefile);

		error = faacEncodeFree(as, bitBuffer);
		if (error == FERROR) {
			printf("Error while encoding %s.\n", FileNames[i]);
			continue;
		}

		// Write the header to the beginning of the file now
		if (headerSize > 0) {
			fseek(aacfile, 0, SEEK_SET);
			fwrite(bitBuffer, 1, headerSize, aacfile);
		}

		fclose(aacfile);
		fclose(log_file);
		if (bitBuffer) { free(bitBuffer); bitBuffer = NULL; }
		if (sampleBuffer) { free(sampleBuffer); sampleBuffer = NULL; }

#ifdef WIN32
		end = GetTickCount();
#else
		end = clock();
#endif
		printf("Encoding %s took: %d sec.\n", FileNames[i], (end-begin)/1000);
	}

	return FNO_ERROR;
}


#endif

⌨️ 快捷键说明

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