aforannano messagee8a400322 years agoHistory
807 lines · 23.3 KB · C++
RawDownload
/*CFAAC - set of classes to import/export .aac/.mp4 filesCopyright (C) 2004 Antonio Foranna This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation.	This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.		You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.			The author can be contacted at:ntnfrn_email-temp@yahoo.it*/ #include "Cfaad.h"   // *********************************************************************************************   Cfaad::Cfaad(HANDLE hIn){	ShowDlg4RawAAC=NULL;	pCfg=NULL; 	if(hIn)	{		hInput=hIn;		return;	} MYINPUT *mi; 	if(!(hInput=GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE|GMEM_ZEROINIT,sizeof(MYINPUT))))		MessageBox(0, "Memory allocation error: hInput", APP_NAME " plugin", MB_OK|MB_ICONSTOP); \	if(!(mi=(MYINPUT *)GlobalLock(hInput)))		MessageBox(0, "GlobalLock(hInput)", APP_NAME " plugin", MB_OK|MB_ICONSTOP); \/*	mi->mp4File=0;	mi->aacFile=0;    mi->hDecoder=0;    mi->buffer=0;	mi->bytes_read=0;*/	mi->BitsPerSample=16;	mi->FindBitrate=FALSE;	newpos_ms=-1;	mi->seek_table=NULL;	mi->seek_table_length=0;	mi->LockSeeking=false;	GlobalUnlock(hInput);}// ----------------------------------------------------------------------------------------------- Cfaad::~Cfaad(){MYINPUT *mi; 	if(!hInput)		return; 	GLOBALLOCK(mi,hInput,MYINPUT,return); 	if(mi->mp4File)		MP4Close(mi->mp4File);	if(mi->aacFile)		fclose(mi->aacFile);	if(mi->hDecoder)		faacDecClose(mi->hDecoder);	FREE_ARRAY(mi->buffer);	FREE_ARRAY(mi->seek_table); 	GlobalUnlock(hInput);	GlobalFree(hInput);} // *********************************************************************************************//									Utilities// ********************************************************************************************* int Cfaad::GetAACTrack(MP4FileHandle infile){// find AAC trackint i, rc;int numTracks = MP4GetNumberOfTracks(infile, NULL, 0); 	for (i = 0; i < numTracks; i++)    {    MP4TrackId trackId = MP4FindTrackId(infile, i, NULL, 0);    const char* trackType = MP4GetTrackType(infile, trackId);         if (!strcmp(trackType, MP4_AUDIO_TRACK_TYPE))        {        unsigned char *buff = NULL;        unsigned __int32 buff_size = 0;        mp4AudioSpecificConfig mp4ASC; 			MP4GetTrackESConfiguration(infile, trackId, (unsigned __int8 **)&buff, &buff_size);             if (buff)            {                rc = AudioSpecificConfig(buff, buff_size, &mp4ASC);                free(buff);                 if (rc < 0)                    return -1;                return trackId;            }        }    }     // can't decode this    return -1;}// ********************************************************************************************* int Cfaad::IsMP4(LPSTR lpstrFilename){DWORD	mp4file = 0;FILE	*hMP4File = fopen(lpstrFilename, "rb");BYTE	header[8];    if(!hMP4File)		return -1;    fread(header, 1, 8, hMP4File);    fclose(hMP4File);    if(header[4]=='f' && header[5]=='t' && header[6]=='y' && header[7]=='p')		return 1; 	return 0;}// *********************************************************************************************long Cfaad::id3v2_TagSize(unsigned char *buffer){	if(StringComp((const char *)buffer, "ID3", 3) == 0)	{	unsigned long tagsize; 	// high bit is not used		tagsize =	(buffer[6] << 21) | (buffer[7] << 14) |					(buffer[8] <<  7) | (buffer[9] <<  0);		tagsize += 10;		return tagsize;	}	return 0;}/*long Cfaad::id3v2_TagSize(aac_buffer *b){DWORD    tagsize = 0;    if (!memcmp(b->buffer, "ID3", 3))    {        // high bit is not used        tagsize = (b->buffer[6] << 21) | (b->buffer[7] << 14) |            (b->buffer[8] <<  7) | (b->buffer[9] <<  0);         tagsize += 10;        advance_buffer(b, tagsize);        fill_buffer(b);    }	return tagsize;}// ********************************************************************************************* int Cfaad::fill_buffer(aac_buffer *b){    int bread;     if (b->bytes_consumed > 0)    {        if (b->bytes_into_buffer)        {            memmove((void*)b->buffer, (void*)(b->buffer + b->bytes_consumed),                b->bytes_into_buffer*sizeof(unsigned char));        }         if (!b->at_eof)        {            bread = fread((void*)(b->buffer + b->bytes_into_buffer), 1,                b->bytes_consumed, b->infile);             if (bread != b->bytes_consumed)                b->at_eof = 1;             b->bytes_into_buffer += bread;        }         b->bytes_consumed = 0;         if (b->bytes_into_buffer > 3)        {            if (memcmp(b->buffer, "TAG", 3) == 0)                b->bytes_into_buffer = 0;        }        if (b->bytes_into_buffer > 11)        {            if (memcmp(b->buffer, "LYRICSBEGIN", 11) == 0)                b->bytes_into_buffer = 0;        }        if (b->bytes_into_buffer > 8)        {            if (memcmp(b->buffer, "APETAGEX", 8) == 0)                b->bytes_into_buffer = 0;        }    }     return 1;}// ********************************************************************************************* void Cfaad::advance_buffer(aac_buffer *b, int bytes){    b->file_offset += bytes;    b->bytes_consumed = bytes;    b->bytes_into_buffer -= bytes;}// ********************************************************************************************* static int adts_sample_rates[] = {96000,88200,64000,48000,44100,32000,24000,22050,16000,12000,11025,8000,7350,0,0,0}; int Cfaad::adts_parse(aac_buffer *b, int *bitrate, float *length){    int frames, frame_length;    int t_framelength = 0;    int samplerate;    float frames_per_sec, bytes_per_frame;     // Read all frames to ensure correct time and bitrate    for (frames = 0; ; frames++)    {        fill_buffer(b);         if (b->bytes_into_buffer > 7)        {            // check syncword             if (!((b->buffer[0] == 0xFF)&&((b->buffer[1] & 0xF6) == 0xF0)))                break;             if (frames == 0)                samplerate = adts_sample_rates[(b->buffer[2]&0x3c)>>2];             frame_length = ((((unsigned int)b->buffer[3] & 0x3)) << 11)                | (((unsigned int)b->buffer[4]) << 3) | (b->buffer[5] >> 5);             t_framelength += frame_length;             if (frame_length > b->bytes_into_buffer)                break;             advance_buffer(b, frame_length);        } else {            break;        }    }     frames_per_sec = (float)samplerate/1024.0f;    if (frames != 0)        bytes_per_frame = (float)t_framelength/(float)(frames*1000);    else        bytes_per_frame = 0;    *bitrate = (int)(8. * bytes_per_frame * frames_per_sec + 0.5);    if (frames_per_sec != 0)        *length = (float)frames/frames_per_sec;    else        *length = 1;     return 1;}// ********************************************************************************************* // get AAC infos for printing void Cfaad::GetAACInfos(aac_buffer *b, DWORD *header_type, float *song_length, int *pbitrate, long filesize){int		bitrate=0;float	length=0;int		bread;long	tagsize=id3v2_TagSize(b); 	*header_type = 0;	b->file_offset=tagsize;     if ((b->buffer[0] == 0xFF) && ((b->buffer[1] & 0xF6) == 0xF0))    {        adts_parse(b, &bitrate, &length);        fseek(b->infile, tagsize, SEEK_SET);         bread = fread(b->buffer, 1, FAAD_MIN_STREAMSIZE*MAX_CHANNELS, b->infile);        if (bread != FAAD_MIN_STREAMSIZE*MAX_CHANNELS)            b->at_eof = 1;        else            b->at_eof = 0;        b->bytes_into_buffer = bread;        b->bytes_consumed = 0;        b->file_offset = tagsize;         *header_type = 1;    } else if (memcmp(b->buffer, "ADIF", 4) == 0) {        int skip_size = (b->buffer[4] & 0x80) ? 9 : 0;        bitrate = ((unsigned int)(b->buffer[4 + skip_size] & 0x0F)<<19) |            ((unsigned int)b->buffer[5 + skip_size]<<11) |            ((unsigned int)b->buffer[6 + skip_size]<<3) |            ((unsigned int)b->buffer[7 + skip_size] & 0xE0);         length = (float)filesize;        if (length != 0)        {            length = ((float)length*8.f)/((float)bitrate) + 0.5f;        }         bitrate = (int)((float)bitrate/1000.0f + 0.5f);         *header_type = 2;    }     *song_length = length;	*pbitrate=bitrate;} void Cfaad::GetAACInfos(char *Filename, aac_buffer *b, DWORD *header_type, float *song_length, int *pbitrate){	if(!(b->infile=fopen(Filename,"rb")))	{		MessageBox(NULL,"Error opening file",NULL,MB_OK);		return;	}	fseek(b->infile, 0, SEEK_END);long	src_size=ftell(b->infile);	fseek(b->infile, 0, SEEK_SET);	if(!(b->buffer=(BYTE *)malloc(FAAD_STREAMSIZE)))	{		MessageBox(NULL,"Memory allocation error: b->buffer",NULL,MB_OK);		return;	} int	tread=src_size<FAAD_STREAMSIZE ? src_size : FAAD_STREAMSIZE;	b->bytes_into_buffer=fread(b->buffer, 1, tread, b->infile);	if(b->bytes_into_buffer!=tread)	{		MessageBox(NULL,"Read failed!",NULL,MB_OK);		return;	}	b->bytes_consumed=0;	b->file_offset=0;	b->at_eof=(b->bytes_into_buffer!=tread) ? 1 : 0; 	*header_type = 0;	b->file_offset=0; 	GetAACInfos(b,header_type,song_length,pbitrate,src_size); 	free(b->buffer);	fclose(b->infile);}*/// *********************************************************************************************//									Main functions// ********************************************************************************************* void CMyDecCfg::getCfg(CMyDecCfg *cfg) { CRegistry reg; if(reg.OpenCreate(HKEY_CURRENT_USER, REGISTRY_PROGRAM_NAME "\\FAAD"))	{		cfg->DefaultCfg=reg.GetSetBool(REG_DEFAULT,true);		cfg->DecCfg.defObjectType=reg.GetSetByte(REG_PROFILE,LC);		cfg->DecCfg.defSampleRate=reg.GetSetDword(REG_SAMPLERATE,44100);		cfg->DecCfg.outputFormat=reg.GetSetByte(REG_BPS,FAAD_FMT_16BIT);		cfg->DecCfg.downMatrix=reg.GetSetByte(REG_DOWNMATRIX,0);		cfg->DecCfg.useOldADTSFormat=reg.GetSetByte(REG_OLDADTS,0);		cfg->DecCfg.dontUpSampleImplicitSBR=reg.GetSetByte(REG_DONTUPSAMPLESBR,1);//		cfg->Channels=reg.GetSetByte("Channels",2);	}	else		MessageBox(0,"Can't open registry!",0,MB_OK|MB_ICONSTOP);}// ----------------------------------------------------------------------------------------------- void CMyDecCfg::setCfg(CMyDecCfg *cfg){ CRegistry reg; 	if(reg.OpenCreate(HKEY_CURRENT_USER, REGISTRY_PROGRAM_NAME "\\FAAD"))	{		reg.SetBool(REG_DEFAULT,cfg->DefaultCfg);		reg.SetByte(REG_PROFILE,cfg->DecCfg.defObjectType);		reg.SetDword(REG_SAMPLERATE,cfg->DecCfg.defSampleRate);		reg.SetByte(REG_BPS,cfg->DecCfg.outputFormat);		reg.SetByte(REG_DOWNMATRIX,cfg->DecCfg.downMatrix);		reg.SetByte(REG_OLDADTS,cfg->DecCfg.useOldADTSFormat);		reg.SetByte(REG_DONTUPSAMPLESBR,cfg->DecCfg.dontUpSampleImplicitSBR);//		reg.SetByte("Channels",cfg->Channels);	}	else		MessageBox(0,"Can't open registry!",0,MB_OK|MB_ICONSTOP);}// ********************************************************************************************* void Cfaad::setFaadCfg(faacDecHandle hDecoder, CMyDecCfg Cfg){faacDecConfigurationPtr	config=faacDecGetCurrentConfiguration(hDecoder); 	if(!Cfg.DefaultCfg)	{		config->defObjectType=Cfg.DecCfg.defObjectType;		config->outputFormat=Cfg.DecCfg.outputFormat;		config->defSampleRate=Cfg.DecCfg.defSampleRate;		config->downMatrix=Cfg.DecCfg.downMatrix;		config->useOldADTSFormat=Cfg.DecCfg.useOldADTSFormat;		config->dontUpSampleImplicitSBR=1;	}	else	{		config->defObjectType=LC;		config->outputFormat=FAAD_FMT_16BIT;		config->defSampleRate=44100;		config->downMatrix=0;		config->useOldADTSFormat=0;		config->dontUpSampleImplicitSBR=1;	}	faacDecSetConfiguration(hDecoder, config);}// ----------------------------------------------------------------------------------------------- void Cfaad::setDefaultFaadCfg(faacDecHandle hDecoder, BOOL showDlg){	if(showDlg && ShowDlg4RawAAC)		ShowDlg4RawAAC(); CMyDecCfg Cfg(false);	setFaadCfg(hDecoder,Cfg);}// ----------------------------------------------------------------------------------------------- void Cfaad::DisplayError(char *ProcName, char *str){MYINPUT *mi;char buf[100]=""; 	GlobalUnlock(hInput); // it wasn't done in getInfos()	GLOBALLOCK(mi,hInput,MYINPUT,return); 	if(ProcName && *ProcName)		sprintf(buf,"%s: ", ProcName);	if(str && *str)		strcat(buf,str);	if(*buf && str)		MessageBox(0, buf, APP_NAME " plugin", MB_OK|MB_ICONSTOP); 	mi->bytes_into_buffer=-1;	GlobalUnlock(hInput);}// ********************************************************************************************* HANDLE Cfaad::getInfos(LPSTR lpstrFilename){MYINPUT *mi; // test tags//CMyDecCfg cfg;	cfg.Tag.ReadMp4Tag(lpstrFilename);//CMyDecCfg cfg;	cfg.Tag.ReadAacTag(lpstrFilename); 	GLOBALLOCK(mi,hInput,MYINPUT,return NULL); //	mi->IsAAC=strcmpi(lpstrFilename+lstrlen(lpstrFilename)-4,".aac")==0;	if((mi->IsMP4=IsMP4(lpstrFilename))==-1)		return ERROR_getInfos("Error opening file"); 	if(mi->IsMP4) // MP4 file ---------------------------------------------------------------------	{	MP4Duration			length;	unsigned __int32	buffer_size;	DWORD				timeScale;	BYTE				sf;    mp4AudioSpecificConfig mp4ASC; 		if(!(mi->mp4File=MP4Read(lpstrFilename, 0)))			return ERROR_getInfos("Error opening file"); 		if((mi->track=GetAACTrack(mi->mp4File))<0)			return ERROR_getInfos(0); //"Unable to find correct AAC sound track"); 		if(!(mi->hDecoder=faacDecOpen()))			return ERROR_getInfos("Error initializing decoder library"); 		MP4GetTrackESConfiguration(mi->mp4File, mi->track, (unsigned __int8 **)&mi->buffer, &buffer_size);		if(!mi->buffer)			return ERROR_getInfos("MP4GetTrackESConfiguration()");		AudioSpecificConfig(mi->buffer, buffer_size, &mp4ASC);         timeScale = mp4ASC.samplingFrequency;        mi->Channels=mp4ASC.channelsConfiguration;        sf = mp4ASC.samplingFrequencyIndex;

Lines 1–500 of 807.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t