comparison main/imafdecoder.cpp @ 634:ba338234c001 imaf_enc

IMAF load code from Jesus Corral Garcia
author Chris Cannam
date Mon, 04 Nov 2013 17:15:52 +0000
parents
children
comparison
equal deleted inserted replaced
633:a8da6db5a2c9 634:ba338234c001
1 //Jesús Corral García
2 //Universidad de Málaga
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <QTextStream>
8 #include <QString>
9 #include "MainWindow.h"
10
11
12 int audiotracks; //number of audio tracks contained inside the IM AF file
13
14 int mainIMAFdecoder(QString outimaf){
15
16 FILE *imf,*audiotrack;
17 int d=0,i,j,sizemdat,audiosize;
18 int chunkoffset[64]; // if you want more than 64 tracks , change this value.
19 unsigned char dat,dat1,dat2,dat3;
20 QTextStream out(stdout);
21
22 imf = fopen (outimaf.toStdString().c_str(),"rb");
23
24 fseek (imf,0,SEEK_SET);
25 fseek (imf,24,SEEK_CUR); //jump to 'mdat'
26
27 fread(&dat, sizeof(unsigned char), 1, imf);
28 fread(&dat1, sizeof(unsigned char), 1, imf);
29 fread(&dat2, sizeof(unsigned char), 1, imf);
30 fread(&dat3, sizeof(unsigned char), 1, imf);
31
32 sizemdat = (dat<<24) | (dat1<<16) | (dat2<<8) | (dat3);
33
34 if (sizemdat==1){ // if conformance file
35 fread(&dat, sizeof(unsigned char), 1, imf);
36 fread(&dat1, sizeof(unsigned char), 1, imf);
37 fread(&dat2, sizeof(unsigned char), 1, imf);
38 fread(&dat3, sizeof(unsigned char), 1, imf);
39 fread(&dat, sizeof(unsigned char), 1, imf);
40 fread(&dat1, sizeof(unsigned char), 1, imf);
41 fread(&dat2, sizeof(unsigned char), 1, imf);
42 fread(&dat3, sizeof(unsigned char), 1, imf);
43 fread(&dat, sizeof(unsigned char), 1, imf);
44 fread(&dat1, sizeof(unsigned char), 1, imf);
45 fread(&dat2, sizeof(unsigned char), 1, imf);
46 fread(&dat3, sizeof(unsigned char), 1, imf);
47 sizemdat = (dat<<24) | (dat1<<16) | (dat2<<8) | (dat3);
48
49 fseek(imf,sizemdat-16,SEEK_CUR);
50 }
51 else {
52
53 fseek(imf, sizemdat-4, SEEK_CUR); // -4 because we have to sub the 4 bytes of size
54 }
55
56 fseek (imf,16,SEEK_CUR);
57 fseek (imf,96,SEEK_CUR); // next track id is placed 96 bytes after the last byte of type 'mvhd'
58
59 fread(&dat, sizeof(unsigned char), 1, imf);
60 fread(&dat1, sizeof(unsigned char), 1, imf);
61 fread(&dat2, sizeof(unsigned char), 1, imf);
62 fread(&dat3, sizeof(unsigned char), 1, imf);
63
64 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
65
66
67 if (audiotracks == 12345678){
68
69 audiotracks = 6; // for the conformance file 2
70
71 }
72 for (j=1;j<=audiotracks;j++){
73
74 d=0;
75 while (d==0){
76
77 fread(&dat, sizeof(unsigned char), 1, imf);
78 fread(&dat1, sizeof(unsigned char), 1, imf);
79 fread(&dat2, sizeof(unsigned char), 1, imf);
80 fread(&dat3, sizeof(unsigned char), 1, imf);
81
82
83 if (dat == 0x68 && dat1 == 0x64 && dat2 == 0x6C && dat3 == 0x72) { // 68646C72 = hdlr
84 d=1;
85 }
86
87 else{
88 fseek(imf, -3, SEEK_CUR); //if we have not readen ´hdlr´
89 }
90
91 } //close while
92
93 for (i=1;i<=8;i++){ //handler type is placed eight bytes after the last byte of type 'hdlr'
94
95 fread(&dat, sizeof(unsigned char), 1, imf);
96 }
97
98 fread(&dat, sizeof(unsigned char), 1, imf);//dat could be ´s´ (soun) or ´t´(text)
99
100 d=0;
101 if (dat==0x73){ //73 = ´s´
102
103 while (d==0){
104
105 fread(&dat, sizeof(unsigned char), 1, imf);
106 fread(&dat1, sizeof(unsigned char), 1, imf);
107 fread(&dat2, sizeof(unsigned char), 1, imf);
108 fread(&dat3, sizeof(unsigned char), 1, imf);
109
110 if (dat == 0x63 && dat1 == 0x6F && dat2 == 0x36 && dat3 == 0x34) { // 636F3634 = co64
111 d=1;
112 }
113 else if (dat == 0x73 && dat1 == 0x74 && dat2 == 0x63 && dat3 == 0x6F){ //7374636F = stco
114 d=2;
115 }
116
117 else{
118 fseek(imf, -3, SEEK_CUR); //if we have not readen ´stco´
119 }
120
121 } //close while
122 if (d==1){ // if co64
123 for (i=1;i<=12;i++){
124
125 fread(&dat, sizeof(unsigned char), 1, imf);
126 }
127 }
128 if (d==2){// if stco
129 for (i=1;i<=8;i++){
130
131 fread(&dat, sizeof(unsigned char), 1, imf);
132 }
133 }
134
135
136 fread(&dat, sizeof(unsigned char), 1, imf);
137 fread(&dat1, sizeof(unsigned char), 1, imf);
138 fread(&dat2, sizeof(unsigned char), 1, imf);
139 fread(&dat3, sizeof(unsigned char), 1, imf);
140
141 chunkoffset[j-1] = (dat<<24) | (dat1<<16) | (dat2<<8) | (dat3);
142
143
144 }//close if
145
146
147 } //close for
148 //At this point, we will look for a text track. If d=2 there are no lyrics
149 d=0;
150 while (d==0){
151
152 fread(&dat, sizeof(unsigned char), 1, imf);
153 fread(&dat1, sizeof(unsigned char), 1, imf);
154 fread(&dat2, sizeof(unsigned char), 1, imf);
155 fread(&dat3, sizeof(unsigned char), 1, imf);
156
157 if (feof (imf )){// if end of file -> no lyrics
158 d=2;
159 }
160 else if (dat == 0x68 && dat1 == 0x64 && dat2 == 0x6C && dat3 == 0x72) { // 68646C72 = hdlr
161 d=1;
162 }
163 else{
164 fseek(imf, -3, SEEK_CUR); //if we have not readen ´hdlr´
165 }
166
167 } //close while
168
169
170 // At this point, we know the position of the audio tracks in mdat and the number of tracks.
171 // Now we will separate the audio tracks in several MP3.
172
173 fseek(imf, 0, SEEK_SET);
174 for (j=0;j<chunkoffset[0];j++) //advance to the position of the first audio track contained in mdat
175 {
176 fread(&dat, sizeof(unsigned char), 1, imf);
177 }
178
179 for(i=0;i<audiotracks-1;i++){
180
181
182 char buf[2];
183 sprintf(buf, "%d", i);// convert int to char
184 audiotrack =fopen (buf,"wb");
185
186 for (j=chunkoffset[i];j<chunkoffset[i+1];j++){
187 fread(&dat,sizeof(unsigned char),1,imf);
188 fwrite (&dat,sizeof(unsigned char),1,audiotrack);
189 }
190 fclose (audiotrack);
191
192 } // close for
193
194 //last audio track
195
196 char buf[2];
197 sprintf(buf, "%d", audiotracks-1); //convert int to char
198 audiotrack =fopen (buf,"wb");
199 audiosize = chunkoffset[2]-chunkoffset[1]; //calculate the size of one audio track. Every audio track must be the same size
200 for (i=1;i<=audiosize;i++)
201 {
202 fread(&dat,sizeof(unsigned char),1,imf);
203 fwrite (&dat,sizeof(unsigned char),1,audiotrack);
204 }
205
206 fclose (audiotrack);
207 fclose (imf);
208 return d;
209
210
211
212 }