1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
|
/*
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);
}
// *********************************************************************************************
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;
mi->type = mp4ASC.objectTypeIndex;
// mi->SBR=mp4ASC.sbr_present_flag;
if(faacDecInit2(mi->hDecoder, mi->buffer, buffer_size, &mi->Samprate, &mi->Channels) < 0)
return ERROR_getInfos("Error initializing decoder library");
FREE_ARRAY(mi->buffer);
length=MP4GetTrackDuration(mi->mp4File, mi->track);
mi->len_ms=(DWORD)MP4ConvertFromTrackDuration(mi->mp4File, mi->track, length, MP4_MSECS_TIME_SCALE);
mi->file_info.bitrate=MP4GetTrackBitRate(mi->mp4File, mi->track);
mi->file_info.version=MP4GetTrackAudioType(mi->mp4File, mi->track)==MP4_MPEG4_AUDIO_TYPE ? 4 : 2;
mi->numSamples=MP4GetTrackNumberOfSamples(mi->mp4File, mi->track);
mi->sampleId=1;
mi->IsSeekable=true;
mi->LockSeeking=!mi->IsSeekable;
}
else // AAC file ------------------------------------------------------------------------------
{
DWORD read,
tmp;
BYTE Channels4Raw=0;
if(!(mi->aacFile=fopen(lpstrFilename,"rb")))
return ERROR_getInfos("Error opening file");
// use bufferized stream
setvbuf(mi->aacFile,NULL,_IOFBF,32767);
// get size of file
fseek(mi->aacFile, 0, SEEK_END);
mi->src_size=ftell(mi->aacFile);
fseek(mi->aacFile, 0, SEEK_SET);
if(!(mi->buffer=(BYTE *)malloc(FAAD_STREAMSIZE)))
return ERROR_getInfos("Memory allocation error: mi->buffer");
tmp=mi->src_size<FAAD_STREAMSIZE ? mi->src_size : FAAD_STREAMSIZE;
read=fread(mi->buffer, 1, tmp, mi->aacFile);
if(read==tmp)
{
mi->bytes_read=read;
mi->bytes_into_buffer=read;
}
else
return ERROR_getInfos("Read failed!");
// skip Tag
long tagsize;
if(tagsize=id3v2_TagSize(mi->buffer))
{
if(tagsize>(long)mi->src_size)
ERROR_getInfos("Corrupt stream!");
if(tagsize<mi->bytes_into_buffer)
{
mi->bytes_into_buffer-=tagsize;
memcpy(mi->buffer,mi->buffer+tagsize,mi->bytes_into_buffer);
}
else
{
mi->bytes_read=tagsize;
mi->bytes_into_buffer=0;
if(tagsize>mi->bytes_into_buffer)
fseek(mi->aacFile, tagsize, SEEK_SET);
}
if(mi->src_size<mi->bytes_read+FAAD_STREAMSIZE-mi->bytes_into_buffer)
tmp=mi->src_size-mi->bytes_read;
else
tmp=FAAD_STREAMSIZE-mi->bytes_into_buffer;
read=fread(mi->buffer+mi->bytes_into_buffer, 1, tmp, mi->aacFile);
if(read==tmp)
{
mi->bytes_read+=read;
mi->bytes_into_buffer+=read;
}
else
ERROR_getInfos("Read failed!");
}
if(get_AAC_format(lpstrFilename, &mi->file_info, &mi->seek_table, &mi->seek_table_length, 0))
ERROR_getInfos("get_AAC_format()");
mi->IsSeekable=mi->file_info.headertype==ADTS && mi->seek_table && mi->seek_table_length>0;
mi->LockSeeking=!mi->IsSeekable;
/*
aac_buffer b;
float fLength;
DWORD headertype;
b.infile=mi->aacFile;
b.buffer=mi->buffer;
b.bytes_into_buffer=read;
b.bytes_consumed=mi->bytes_consumed;
b.file_offset=0;
b.at_eof=(read!=tmp) ? 1 : 0;
GetAACInfos(&b,&headertype,&fLength,&mi->file_info.bitrate,mi->src_size);
mi->file_info.bitrate*=1024;
mi->file_info.headertype=headertype;
mi->bytes_into_buffer=b.bytes_into_buffer;
mi->bytes_consumed=b.bytes_consumed;
IsSeekable=false; // only mp4 can be seeked
*/
if(!mi->FindBitrate) // open a new instance to get info from decoder
{
MYINPUT *miTmp;
Cfaad *NewInst;
if(!(NewInst=new Cfaad()))
return ERROR_getInfos("Memory allocation error: NewInst");
GLOBALLOCK(miTmp,NewInst->hInput,MYINPUT,return 0);
miTmp->FindBitrate=TRUE;
NewInst->ShowDlg4RawAAC=ShowDlg4RawAAC;
NewInst->pCfg=pCfg;
if(!NewInst->getInfos(lpstrFilename))
return ERROR_getInfos(0);
mi->Channels=miTmp->frameInfo.channels;
if(mi->file_info.headertype==RAW)
mi->file_info.bitrate=miTmp->file_info.bitrate;//*mi->Channels;
mi->Samprate=miTmp->Samprate;
mi->file_info.headertype=miTmp->file_info.headertype;
mi->file_info.object_type=miTmp->file_info.object_type;
mi->file_info.version=miTmp->file_info.version;
GlobalUnlock(NewInst->hInput);
delete NewInst;
}
if(!(mi->hDecoder=faacDecOpen()))
return ERROR_getInfos("Can't open library");
if(mi->file_info.headertype==RAW)
if(pCfg)
setFaadCfg(mi->hDecoder,*pCfg);
else
setDefaultFaadCfg(mi->hDecoder,mi->FindBitrate);
BYTE Channels; // faacDecInit doesn't report correctly the number of channels in raw aac files
if((mi->bytes_consumed=faacDecInit(mi->hDecoder, mi->buffer, mi->bytes_into_buffer, &mi->Samprate, &Channels))<0)
return ERROR_getInfos("faacDecInit()");
mi->bytes_into_buffer-=mi->bytes_consumed;
if(mi->FindBitrate) // get info from decoder
{
DWORD Samples,
BytesConsumed;
if(!processData(hInput,0,0))
return ERROR_getInfos(0);
Samples=mi->frameInfo.samples/sizeof(short);
BytesConsumed=mi->frameInfo.bytesconsumed;
if(mi->file_info.headertype==RAW || !mi->file_info.bitrate)
mi->file_info.bitrate=(BytesConsumed*8*mi->Samprate)/(Samples*2);
if(!mi->file_info.bitrate)
return ERROR_getInfos("Can't determine the bitrate");
}
mi->len_ms=(DWORD)((mi->src_size<<3)/(mi->file_info.bitrate>>10));
// mi->len_ms=(DWORD)((1000*((float)mi->src_size*8))/mi->file_info.bitrate);
}
if(mi->len_ms)
mi->dst_size=(DWORD)(mi->len_ms*((float)mi->Samprate/1000)*mi->Channels*(mi->BitsPerSample/8));
else
return ERROR_getInfos("Can't determine the length");
showInfo(mi);
GlobalUnlock(hInput);
return hInput;
}
// *********************************************************************************************
int Cfaad::processData(HANDLE hInput, unsigned char far *bufout, long lBytes)
{
BYTE *buffer;
DWORD BytesDecoded=0;
char *sample_buffer=0;
int read;
MYINPUT *mi;
GLOBALLOCK(mi,hInput,MYINPUT,return 0);
if(mi->LockSeeking)
{
NoSeek();
mi->LockSeeking=false;
}
if(mi->IsMP4) // MP4 file --------------------------------------------------------------------------
{
unsigned __int32 buffer_size=0;
int rc;
if(newpos_ms>-1)
{
MP4Duration duration=MP4ConvertToTrackDuration(mi->mp4File,mi->track,newpos_ms,MP4_MSECS_TIME_SCALE);
MP4SampleId sampleId=MP4GetSampleIdFromTime(mi->mp4File,mi->track,duration,0);
mi->bytes_read=(DWORD)(((float)newpos_ms*mi->file_info.bitrate)/(8*1000));
if(seek(mi->bytes_read)) // update the slider
return ERROR_processData(0);
newpos_ms=-1;
}
do
{
buffer=NULL;
if(mi->sampleId>=mi->numSamples)
return ERROR_processData(0);
rc=MP4ReadSample(mi->mp4File, mi->track, mi->sampleId++, (unsigned __int8 **)&buffer, &buffer_size, NULL, NULL, NULL, NULL);
if(rc==0 || buffer==NULL)
{
FREE_ARRAY(buffer);
return ERROR_processData("MP4ReadSample()");
}
sample_buffer=(char *)faacDecDecode(mi->hDecoder,&mi->frameInfo,buffer,buffer_size);
BytesDecoded=mi->frameInfo.samples*sizeof(short);
if(BytesDecoded>(DWORD)lBytes)
BytesDecoded=lBytes;
memmove(bufout,sample_buffer,BytesDecoded);
FREE_ARRAY(buffer);
// to update the slider
mi->bytes_read+=buffer_size;
if(seek(mi->bytes_read))
return ERROR_processData(0);
}while(!BytesDecoded && !mi->frameInfo.error);
}
else // AAC file --------------------------------------------------------------------------
{
if(newpos_ms>-1)
{
if(mi->IsSeekable)
{
DWORD normalized=mi->len_ms/(mi->seek_table_length-1);
if(normalized<1000)
normalized=1000;
mi->bytes_read=mi->seek_table[newpos_ms/normalized];
fseek(mi->aacFile, mi->bytes_read, SEEK_SET);
if(seek(mi->bytes_read)) // update the slider
return ERROR_processData(0);
mi->bytes_into_buffer=0;
mi->bytes_consumed=FAAD_STREAMSIZE;
}
newpos_ms=-1;
}
buffer=mi->buffer;
do
{
if(mi->bytes_consumed>0)
{
if(mi->bytes_into_buffer)
memmove(buffer,buffer+mi->bytes_consumed,mi->bytes_into_buffer);
if(mi->bytes_read<mi->src_size)
{
int tmp;
if(mi->bytes_read+mi->bytes_consumed<mi->src_size)
tmp=mi->bytes_consumed;
else
tmp=mi->src_size-mi->bytes_read;
read=fread(buffer+mi->bytes_into_buffer, 1, tmp, mi->aacFile);
if(read==tmp)
{
mi->bytes_read+=read;
mi->bytes_into_buffer+=read;
}
}
else
if(mi->bytes_into_buffer)
memset(buffer+mi->bytes_into_buffer, 0, mi->bytes_consumed);
mi->bytes_consumed=0;
if( (mi->bytes_into_buffer>3 && !memcmp(mi->buffer, "TAG", 3)) ||
(mi->bytes_into_buffer>11 && !memcmp(mi->buffer, "LYRICSBEGIN", 11)) ||
(mi->bytes_into_buffer>8 && !memcmp(mi->buffer, "APETAGEX", 8)))
return ERROR_processData(0);
}
if(mi->bytes_into_buffer<1)
if(mi->bytes_read<mi->src_size)
return ERROR_processData("Buffer empty!");
else
return ERROR_processData(0);
sample_buffer=(char *)faacDecDecode(mi->hDecoder,&mi->frameInfo,buffer,mi->bytes_into_buffer);
BytesDecoded=mi->frameInfo.samples*sizeof(short);
if(bufout)
{
if(BytesDecoded>(DWORD)lBytes)
BytesDecoded=lBytes;
if(sample_buffer && BytesDecoded && !mi->frameInfo.error)
memmove(bufout,sample_buffer,BytesDecoded);
}
else // Data needed to decode Raw files
{
mi->Channels=mi->frameInfo.channels;
mi->file_info.object_type=mi->frameInfo.object_type;
}
mi->bytes_consumed+=mi->frameInfo.bytesconsumed;
mi->bytes_into_buffer-=mi->bytes_consumed;
}while(!BytesDecoded && !mi->frameInfo.error);
} // END AAC file --------------------------------------------------------------------------
if(mi->frameInfo.error)
return ERROR_processData((char *)faacDecGetErrorMessage(mi->frameInfo.error));
showProgress(mi);
GlobalUnlock(hInput);
return BytesDecoded;
}
|