cfaad.cpp
来自「faac-1.25.rar音频编解码器demo」· C++ 代码 · 共 808 行 · 第 1/2 页
CPP
808 行
/*
CFAAC - set of classes to import/export .aac/.mp4 files
Copyright (C) 2004 Antonio Foranna
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 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 track
int 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);
}
// *********************************************************************************************
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?