Chris@634: //Jesús Corral García Chris@634: //Universidad de Málaga Chris@634: Chris@634: #include Chris@634: #include Chris@634: #include Chris@634: #include Chris@634: #include Chris@634: #include "MainWindow.h" Chris@634: Chris@634: Chris@634: int audiotracks; //number of audio tracks contained inside the IM AF file Chris@634: Chris@634: int mainIMAFdecoder(QString outimaf){ Chris@634: Chris@634: FILE *imf,*audiotrack; Chris@634: int d=0,i,j,sizemdat,audiosize; Chris@634: int chunkoffset[64]; // if you want more than 64 tracks , change this value. Chris@634: unsigned char dat,dat1,dat2,dat3; Chris@634: QTextStream out(stdout); Chris@634: Chris@634: imf = fopen (outimaf.toStdString().c_str(),"rb"); Chris@634: Chris@634: fseek (imf,0,SEEK_SET); Chris@634: fseek (imf,24,SEEK_CUR); //jump to 'mdat' Chris@634: Chris@634: fread(&dat, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat1, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat2, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat3, sizeof(unsigned char), 1, imf); Chris@634: Chris@634: sizemdat = (dat<<24) | (dat1<<16) | (dat2<<8) | (dat3); Chris@634: Chris@634: if (sizemdat==1){ // if conformance file Chris@634: fread(&dat, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat1, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat2, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat3, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat1, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat2, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat3, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat1, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat2, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat3, sizeof(unsigned char), 1, imf); Chris@634: sizemdat = (dat<<24) | (dat1<<16) | (dat2<<8) | (dat3); Chris@634: Chris@634: fseek(imf,sizemdat-16,SEEK_CUR); Chris@634: } Chris@634: else { Chris@634: Chris@634: fseek(imf, sizemdat-4, SEEK_CUR); // -4 because we have to sub the 4 bytes of size Chris@634: } Chris@634: Chris@634: fseek (imf,16,SEEK_CUR); Chris@634: fseek (imf,96,SEEK_CUR); // next track id is placed 96 bytes after the last byte of type 'mvhd' Chris@634: Chris@634: fread(&dat, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat1, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat2, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat3, sizeof(unsigned char), 1, imf); Chris@634: Chris@634: audiotracks = ((dat<<24) | (dat1<<16) | (dat2<<8) | (dat3)) -1 ; //read the number of audio tracks.It is ´-1´ because the field indicates the number of tracks +1 Chris@634: Chris@634: Chris@634: if (audiotracks == 12345678){ Chris@634: Chris@634: audiotracks = 6; // for the conformance file 2 Chris@634: Chris@634: } Chris@634: for (j=1;j<=audiotracks;j++){ Chris@634: Chris@634: d=0; Chris@634: while (d==0){ Chris@634: Chris@634: fread(&dat, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat1, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat2, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat3, sizeof(unsigned char), 1, imf); Chris@634: Chris@634: Chris@634: if (dat == 0x68 && dat1 == 0x64 && dat2 == 0x6C && dat3 == 0x72) { // 68646C72 = hdlr Chris@634: d=1; Chris@634: } Chris@634: Chris@634: else{ Chris@634: fseek(imf, -3, SEEK_CUR); //if we have not readen ´hdlr´ Chris@634: } Chris@634: Chris@634: } //close while Chris@634: Chris@634: for (i=1;i<=8;i++){ //handler type is placed eight bytes after the last byte of type 'hdlr' Chris@634: Chris@634: fread(&dat, sizeof(unsigned char), 1, imf); Chris@634: } Chris@634: Chris@634: fread(&dat, sizeof(unsigned char), 1, imf);//dat could be ´s´ (soun) or ´t´(text) Chris@634: Chris@634: d=0; Chris@634: if (dat==0x73){ //73 = ´s´ Chris@634: Chris@634: while (d==0){ Chris@634: Chris@634: fread(&dat, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat1, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat2, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat3, sizeof(unsigned char), 1, imf); Chris@634: Chris@634: if (dat == 0x63 && dat1 == 0x6F && dat2 == 0x36 && dat3 == 0x34) { // 636F3634 = co64 Chris@634: d=1; Chris@634: } Chris@634: else if (dat == 0x73 && dat1 == 0x74 && dat2 == 0x63 && dat3 == 0x6F){ //7374636F = stco Chris@634: d=2; Chris@634: } Chris@634: Chris@634: else{ Chris@634: fseek(imf, -3, SEEK_CUR); //if we have not readen ´stco´ Chris@634: } Chris@634: Chris@634: } //close while Chris@634: if (d==1){ // if co64 Chris@634: for (i=1;i<=12;i++){ Chris@634: Chris@634: fread(&dat, sizeof(unsigned char), 1, imf); Chris@634: } Chris@634: } Chris@634: if (d==2){// if stco Chris@634: for (i=1;i<=8;i++){ Chris@634: Chris@634: fread(&dat, sizeof(unsigned char), 1, imf); Chris@634: } Chris@634: } Chris@634: Chris@634: Chris@634: fread(&dat, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat1, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat2, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat3, sizeof(unsigned char), 1, imf); Chris@634: Chris@634: chunkoffset[j-1] = (dat<<24) | (dat1<<16) | (dat2<<8) | (dat3); Chris@634: Chris@634: Chris@634: }//close if Chris@634: Chris@634: Chris@634: } //close for Chris@634: //At this point, we will look for a text track. If d=2 there are no lyrics Chris@634: d=0; Chris@634: while (d==0){ Chris@634: Chris@634: fread(&dat, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat1, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat2, sizeof(unsigned char), 1, imf); Chris@634: fread(&dat3, sizeof(unsigned char), 1, imf); Chris@634: Chris@634: if (feof (imf )){// if end of file -> no lyrics Chris@634: d=2; Chris@634: } Chris@634: else if (dat == 0x68 && dat1 == 0x64 && dat2 == 0x6C && dat3 == 0x72) { // 68646C72 = hdlr Chris@634: d=1; Chris@634: } Chris@634: else{ Chris@634: fseek(imf, -3, SEEK_CUR); //if we have not readen ´hdlr´ Chris@634: } Chris@634: Chris@634: } //close while Chris@634: Chris@634: Chris@634: // At this point, we know the position of the audio tracks in mdat and the number of tracks. Chris@634: // Now we will separate the audio tracks in several MP3. Chris@634: Chris@634: fseek(imf, 0, SEEK_SET); Chris@634: for (j=0;j