Mercurial > SDL_sound_CoreAudio
comparison decoders/libmpg123/parse.c @ 562:7e08477b0fc1
MP3 decoder upgrade work.
Ripped out SMPEG and mpglib support, replaced it with "mpg123.c" and libmpg123.
libmpg123 is a much better version of mpglib, so it should solve all the
problems about MP3's not seeking, or most modern MP3's not playing at all,
etc. Since you no longer have to make a tradeoff with SMPEG for features, and
SMPEG is basically rotting, I removed it from the project.
There is still work to be done with libmpg123...there are MMX, 3DNow, SSE,
Altivec, etc decoders which we don't have enabled at the moment, and the
build system could use some work to make this compile more cleanly, etc.
Still: huge win.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Fri, 30 Jan 2009 02:44:47 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
561:f2985e08589c | 562:7e08477b0fc1 |
---|---|
1 /* | |
2 parse: spawned from common; clustering around stream/frame parsing | |
3 | |
4 copyright ?-2008 by the mpg123 project - free software under the terms of the LGPL 2.1 | |
5 see COPYING and AUTHORS files in distribution or http://mpg123.org | |
6 initially written by Michael Hipp & Thomas Orgis | |
7 */ | |
8 | |
9 #include "mpg123lib_intern.h" | |
10 | |
11 #include <sys/stat.h> | |
12 #include <fcntl.h> | |
13 | |
14 #include "getbits.h" | |
15 | |
16 #ifdef WIN32 | |
17 #include <winsock.h> | |
18 #endif | |
19 | |
20 /* a limit for number of frames in a track; beyond that unsigned long may not be enough to hold byte addresses */ | |
21 #ifdef HAVE_LIMITS_H | |
22 #include <limits.h> | |
23 #endif | |
24 #ifndef ULONG_MAX | |
25 /* hm, is this portable across preprocessors? */ | |
26 #define ULONG_MAX ((unsigned long)-1) | |
27 #endif | |
28 #define TRACK_MAX_FRAMES ULONG_MAX/4/1152 | |
29 | |
30 #include "debug.h" | |
31 | |
32 #define bsbufid(fr) (fr)->bsbuf==(fr)->bsspace[0] ? 0 : ((fr)->bsbuf==fr->bsspace[1] ? 1 : ( (fr)->bsbuf==(fr)->bsspace[0]+512 ? 2 : ((fr)->bsbuf==fr->bsspace[1]+512 ? 3 : -1) ) ) | |
33 | |
34 /* | |
35 AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM | |
36 A: sync | |
37 B: mpeg version | |
38 C: layer | |
39 D: CRC | |
40 E: bitrate | |
41 F:sampling rate | |
42 G: padding | |
43 H: private | |
44 I: channel mode | |
45 J: mode ext | |
46 K: copyright | |
47 L: original | |
48 M: emphasis | |
49 | |
50 old compare mask 0xfffffd00: | |
51 11111111 11111111 11111101 00000000 | |
52 | |
53 means: everything must match excluding padding and channel mode, ext mode, ... | |
54 But a vbr stream's headers will differ in bitrate! | |
55 We are already strict in allowing only frames of same type in stream, we should at least watch out for VBR while being strict. | |
56 | |
57 So a better mask is: | |
58 11111111 11111111 00001101 00000000 | |
59 | |
60 Even more, I'll allow varying crc bit. | |
61 11111111 11111110 00001101 00000000 | |
62 | |
63 (still unsure about this private bit) | |
64 */ | |
65 #define HDRCMPMASK 0xfffe0d00 | |
66 #define HDRCHANMASK 0xc0 /* 11000000, selecting II bits (channel mode) */ | |
67 #define HDRSAMPMASK 0xc00 /* 1100 00000000, FF bits (sample rate) */ | |
68 | |
69 /* bitrates for [mpeg1/2][layer] */ | |
70 static const int tabsel_123[2][3][16] = | |
71 { | |
72 { | |
73 {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,}, | |
74 {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,}, | |
75 {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} | |
76 }, | |
77 { | |
78 {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,}, | |
79 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,}, | |
80 {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} | |
81 } | |
82 }; | |
83 | |
84 const long freqs[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 }; | |
85 | |
86 static int decode_header(mpg123_handle *fr,unsigned long newhead); | |
87 | |
88 int read_frame_init(mpg123_handle* fr) | |
89 { | |
90 if(frame_reset(fr) != 0) return -1; | |
91 return 0; | |
92 } | |
93 | |
94 /* These two are to be replaced by one function that gives all the frame parameters (for outsiders).*/ | |
95 | |
96 int frame_bitrate(mpg123_handle *fr) | |
97 { | |
98 return tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index]; | |
99 } | |
100 | |
101 long frame_freq(mpg123_handle *fr) | |
102 { | |
103 return freqs[fr->sampling_frequency]; | |
104 } | |
105 | |
106 #define free_format_header(head) ( ((head & 0xffe00000) == 0xffe00000) && ((head>>17)&3) && (((head>>12)&0xf) == 0x0) && (((head>>10)&0x3) != 0x3 )) | |
107 | |
108 /* compiler is smart enought to inline this one or should I really do it as macro...? */ | |
109 int head_check(unsigned long head) | |
110 { | |
111 if | |
112 ( | |
113 /* first 11 bits are set to 1 for frame sync */ | |
114 ((head & 0xffe00000) != 0xffe00000) | |
115 || | |
116 /* layer: 01,10,11 is 1,2,3; 00 is reserved */ | |
117 (!((head>>17)&3)) | |
118 || | |
119 /* 1111 means bad bitrate */ | |
120 (((head>>12)&0xf) == 0xf) | |
121 || | |
122 /* 0000 means free format... which should be supported in future. */ | |
123 (((head>>12)&0xf) == 0x0) | |
124 || | |
125 /* sampling freq: 11 is reserved */ | |
126 (((head>>10)&0x3) == 0x3 ) | |
127 /* here used to be a mpeg 2.5 check... re-enabled 2.5 decoding due to lack of evidence that it is really not good */ | |
128 ) | |
129 { | |
130 return FALSE; | |
131 } | |
132 /* if no check failed, the header is valid (hopefully)*/ | |
133 else | |
134 { | |
135 return TRUE; | |
136 } | |
137 } | |
138 | |
139 static int check_lame_tag(mpg123_handle *fr) | |
140 { | |
141 /* | |
142 going to look for Xing or Info at some position after the header | |
143 MPEG 1 MPEG 2/2.5 (LSF) | |
144 Stereo, Joint Stereo, Dual Channel 32 17 | |
145 Mono 17 9 | |
146 | |
147 Also, how to avoid false positives? I guess I should interpret more of the header to rule that out(?). | |
148 I hope that ensuring all zeros until tag start is enough. | |
149 */ | |
150 size_t lame_offset = (fr->stereo == 2) ? (fr->lsf ? 17 : 32 ) : (fr->lsf ? 9 : 17); | |
151 /* At least skip the decoder delay. */ | |
152 #ifdef GAPLESS | |
153 if(fr->begin_s == 0) frame_gapless_init(fr, GAPLESS_DELAY, 0); | |
154 #endif | |
155 | |
156 if(fr->framesize >= 120+lame_offset) /* traditional Xing header is 120 bytes */ | |
157 { | |
158 size_t i; | |
159 int lame_type = 0; | |
160 debug("do we have lame tag?"); | |
161 /* only search for tag when all zero before it (apart from checksum) */ | |
162 for(i=2; i < lame_offset; ++i) if(fr->bsbuf[i] != 0) break; | |
163 if(i == lame_offset) | |
164 { | |
165 debug("possibly..."); | |
166 if | |
167 ( | |
168 (fr->bsbuf[lame_offset] == 'I') | |
169 && (fr->bsbuf[lame_offset+1] == 'n') | |
170 && (fr->bsbuf[lame_offset+2] == 'f') | |
171 && (fr->bsbuf[lame_offset+3] == 'o') | |
172 ) | |
173 { | |
174 lame_type = 1; /* We still have to see what there is */ | |
175 } | |
176 else if | |
177 ( | |
178 (fr->bsbuf[lame_offset] == 'X') | |
179 && (fr->bsbuf[lame_offset+1] == 'i') | |
180 && (fr->bsbuf[lame_offset+2] == 'n') | |
181 && (fr->bsbuf[lame_offset+3] == 'g') | |
182 ) | |
183 { | |
184 lame_type = 2; | |
185 fr->vbr = MPG123_VBR; /* Xing header means always VBR */ | |
186 } | |
187 if(lame_type) | |
188 { | |
189 unsigned long xing_flags; | |
190 | |
191 /* we have one of these headers... */ | |
192 if(VERBOSE2) fprintf(stderr, "Note: Xing/Lame/Info header detected\n"); | |
193 /* now interpret the Xing part, I have 120 bytes total for sure */ | |
194 /* there are 4 bytes for flags, but only the last byte contains known ones */ | |
195 lame_offset += 4; /* now first byte after Xing/Name */ | |
196 /* 4 bytes dword for flags */ | |
197 #define make_long(a, o) ((((unsigned long) a[o]) << 24) | (((unsigned long) a[o+1]) << 16) | (((unsigned long) a[o+2]) << 8) | ((unsigned long) a[o+3])) | |
198 /* 16 bit */ | |
199 #define make_short(a,o) ((((unsigned short) a[o]) << 8) | ((unsigned short) a[o+1])) | |
200 xing_flags = make_long(fr->bsbuf, lame_offset); | |
201 lame_offset += 4; | |
202 debug1("Xing: flags 0x%08lx", xing_flags); | |
203 if(xing_flags & 1) /* frames */ | |
204 { | |
205 /* | |
206 In theory, one should use that value for skipping... | |
207 When I know the exact number of samples I could simply count in flush_output, | |
208 but that's problematic with seeking and such. | |
209 I still miss the real solution for detecting the end. | |
210 */ | |
211 fr->track_frames = (off_t) make_long(fr->bsbuf, lame_offset); | |
212 if(fr->track_frames > TRACK_MAX_FRAMES) fr->track_frames = 0; /* endless stream? */ | |
213 #ifdef GAPLESS | |
214 /* if no further info there, remove/add at least the decoder delay */ | |
215 if(fr->p.flags & MPG123_GAPLESS) | |
216 { | |
217 off_t length = fr->track_frames * spf(fr); | |
218 if(length > 1) | |
219 frame_gapless_init(fr, GAPLESS_DELAY, length+GAPLESS_DELAY); | |
220 } | |
221 #endif | |
222 if(VERBOSE3) fprintf(stderr, "Note: Xing: %lu frames\n", (long unsigned)fr->track_frames); | |
223 lame_offset += 4; | |
224 } | |
225 if(xing_flags & 0x2) /* bytes */ | |
226 { | |
227 unsigned long xing_bytes = make_long(fr->bsbuf, lame_offset); /* We assume that this is the _total_ size of the file, including Xing frame ... and ID3 frames... | |
228 It's not that clearly documented... */ | |
229 if(fr->rdat.filelen < 1) | |
230 fr->rdat.filelen = (off_t) xing_bytes; /* One could start caring for overflow here. */ | |
231 else | |
232 { | |
233 if((off_t) xing_bytes != fr->rdat.filelen && NOQUIET) | |
234 { | |
235 double diff = 1.0/fr->rdat.filelen * (fr->rdat.filelen - (off_t)xing_bytes); | |
236 if(diff < 0.) diff = -diff; | |
237 | |
238 if(VERBOSE3) | |
239 fprintf(stderr, "Note: Xing stream size %lu differs by %f%% from determined/given file size!\n", xing_bytes, diff); | |
240 | |
241 if(diff > 1.) | |
242 fprintf(stderr, "Warning: Xing stream size off by more than 1%%, fuzzy seeking may be even more fuzzy than by design!\n"); | |
243 } | |
244 } | |
245 | |
246 if(VERBOSE3) | |
247 fprintf(stderr, "Note: Xing: %lu bytes\n", (long unsigned)xing_bytes); | |
248 | |
249 lame_offset += 4; | |
250 } | |
251 if(xing_flags & 0x4) /* TOC */ | |
252 { | |
253 frame_fill_toc(fr, fr->bsbuf+lame_offset); | |
254 lame_offset += 100; /* just skip */ | |
255 } | |
256 if(xing_flags & 0x8) /* VBR quality */ | |
257 { | |
258 if(VERBOSE3) | |
259 { | |
260 unsigned long xing_quality = make_long(fr->bsbuf, lame_offset); | |
261 fprintf(stderr, "Note: Xing: quality = %lu\n", (long unsigned)xing_quality); | |
262 } | |
263 lame_offset += 4; | |
264 } | |
265 /* I guess that either 0 or LAME extra data follows */ | |
266 /* there may this crc16 be floating around... (?) */ | |
267 if(fr->bsbuf[lame_offset] != 0) | |
268 { | |
269 unsigned char lame_vbr; | |
270 float replay_gain[2] = {0,0}; | |
271 float peak = 0; | |
272 float gain_offset = 0; /* going to be +6 for old lame that used 83dB */ | |
273 char nb[10]; | |
274 memcpy(nb, fr->bsbuf+lame_offset, 9); | |
275 nb[9] = 0; | |
276 if(VERBOSE3) fprintf(stderr, "Note: Info: Encoder: %s\n", nb); | |
277 if(!strncmp("LAME", nb, 4)) | |
278 { | |
279 gain_offset = 6; | |
280 debug("TODO: finish lame detetcion..."); | |
281 } | |
282 lame_offset += 9; | |
283 /* the 4 big bits are tag revision, the small bits vbr method */ | |
284 lame_vbr = fr->bsbuf[lame_offset] & 15; | |
285 if(VERBOSE3) | |
286 { | |
287 fprintf(stderr, "Note: Info: rev %u\n", fr->bsbuf[lame_offset] >> 4); | |
288 fprintf(stderr, "Note: Info: vbr mode %u\n", lame_vbr); | |
289 } | |
290 lame_offset += 1; | |
291 switch(lame_vbr) | |
292 { | |
293 /* from rev1 proposal... not sure if all good in practice */ | |
294 case 1: | |
295 case 8: fr->vbr = MPG123_CBR; break; | |
296 case 2: | |
297 case 9: fr->vbr = MPG123_ABR; break; | |
298 default: fr->vbr = MPG123_VBR; /* 00==unknown is taken as VBR */ | |
299 } | |
300 /* skipping: lowpass filter value */ | |
301 lame_offset += 1; | |
302 /* replaygain */ | |
303 /* 32bit float: peak amplitude -- why did I parse it as int before??*/ | |
304 /* Ah, yes, lame seems to store it as int since some day in 2003; I've only seen zeros anyway until now, bah! */ | |
305 if | |
306 ( | |
307 (fr->bsbuf[lame_offset] != 0) | |
308 || (fr->bsbuf[lame_offset+1] != 0) | |
309 || (fr->bsbuf[lame_offset+2] != 0) | |
310 || (fr->bsbuf[lame_offset+3] != 0) | |
311 ) | |
312 { | |
313 debug("Wow! Is there _really_ a non-zero peak value? Now is it stored as float or int - how should I know?"); | |
314 /* byte*peak_bytes = (byte*) &peak; | |
315 ... endianess ... just copy bytes to avoid floating point operation on unaligned memory? | |
316 peak_bytes[0] = ... | |
317 peak = *(float*) (fr->bsbuf+lame_offset); */ | |
318 } | |
319 if(VERBOSE3) fprintf(stderr, "Note: Info: peak = %f (I won't use this)\n", peak); | |
320 peak = 0; /* until better times arrived */ | |
321 lame_offset += 4; | |
322 /* | |
323 ReplayGain values - lame only writes radio mode gain... | |
324 16bit gain, 3 bits name, 3 bits originator, sign (1=-, 0=+), dB value*10 in 9 bits (fixed point) | |
325 ignore the setting if name or originator == 000! | |
326 radio 0 0 1 0 1 1 1 0 0 1 1 1 1 1 0 1 | |
327 audiophile 0 1 0 0 1 0 0 0 0 0 0 1 0 1 0 0 | |
328 */ | |
329 | |
330 for(i =0; i < 2; ++i) | |
331 { | |
332 unsigned char origin = (fr->bsbuf[lame_offset] >> 2) & 0x7; /* the 3 bits after that... */ | |
333 if(origin != 0) | |
334 { | |
335 unsigned char gt = fr->bsbuf[lame_offset] >> 5; /* only first 3 bits */ | |
336 if(gt == 1) gt = 0; /* radio */ | |
337 else if(gt == 2) gt = 1; /* audiophile */ | |
338 else continue; | |
339 /* get the 9 bits into a number, divide by 10, multiply sign... happy bit banging */ | |
340 replay_gain[0] = ((fr->bsbuf[lame_offset] & 0x2) ? -0.1 : 0.1) * (make_short(fr->bsbuf, lame_offset) & 0x1f); | |
341 } | |
342 lame_offset += 2; | |
343 } | |
344 if(VERBOSE3) | |
345 { | |
346 fprintf(stderr, "Note: Info: Radio Gain = %03.1fdB\n", replay_gain[0]); | |
347 fprintf(stderr, "Note: Info: Audiophile Gain = %03.1fdB\n", replay_gain[1]); | |
348 } | |
349 for(i=0; i < 2; ++i) | |
350 { | |
351 if(fr->rva.level[i] <= 0) | |
352 { | |
353 fr->rva.peak[i] = 0; /* at some time the parsed peak should be used */ | |
354 fr->rva.gain[i] = replay_gain[i]; | |
355 fr->rva.level[i] = 0; | |
356 } | |
357 } | |
358 lame_offset += 1; /* skipping encoding flags byte */ | |
359 if(fr->vbr == MPG123_ABR) | |
360 { | |
361 fr->abr_rate = fr->bsbuf[lame_offset]; | |
362 if(VERBOSE3) fprintf(stderr, "Note: Info: ABR rate = %u\n", fr->abr_rate); | |
363 } | |
364 lame_offset += 1; | |
365 /* encoder delay and padding, two 12 bit values... lame does write them from int ...*/ | |
366 if(VERBOSE3) | |
367 fprintf(stderr, "Note: Encoder delay = %i; padding = %i\n", | |
368 ((((int) fr->bsbuf[lame_offset]) << 4) | (((int) fr->bsbuf[lame_offset+1]) >> 4)), | |
369 (((((int) fr->bsbuf[lame_offset+1]) << 8) | (((int) fr->bsbuf[lame_offset+2]))) & 0xfff) ); | |
370 #ifdef GAPLESS | |
371 if(fr->p.flags & MPG123_GAPLESS) | |
372 { | |
373 off_t length = fr->track_frames * spf(fr); | |
374 off_t skipbegin = GAPLESS_DELAY + ((((int) fr->bsbuf[lame_offset]) << 4) | (((int) fr->bsbuf[lame_offset+1]) >> 4)); | |
375 off_t skipend = -GAPLESS_DELAY + (((((int) fr->bsbuf[lame_offset+1]) << 8) | (((int) fr->bsbuf[lame_offset+2]))) & 0xfff); | |
376 debug3("preparing gapless mode for layer3: length %lu, skipbegin %lu, skipend %lu", | |
377 (long unsigned)length, (long unsigned)skipbegin, (long unsigned)skipend); | |
378 if(length > 1) | |
379 frame_gapless_init(fr, skipbegin, (skipend < length) ? length-skipend : length); | |
380 } | |
381 #endif | |
382 } | |
383 /* switch buffer back ... */ | |
384 fr->bsbuf = fr->bsspace[fr->bsnum]+512; | |
385 fr->bsnum = (fr->bsnum + 1) & 1; | |
386 return 1; /* got it! */ | |
387 } | |
388 } | |
389 } | |
390 return 0; /* no lame tag */ | |
391 } | |
392 | |
393 | |
394 /* | |
395 That's a big one: read the next frame. 1 is success, <= 0 is some error | |
396 Special error READER_MORE means: Please feed more data and try again. | |
397 */ | |
398 int read_frame(mpg123_handle *fr) | |
399 { | |
400 /* TODO: rework this thing */ | |
401 unsigned long newhead; | |
402 off_t framepos; | |
403 int ret; | |
404 /* stuff that needs resetting if complete frame reading fails */ | |
405 int oldsize = fr->framesize; | |
406 int oldphase = fr->halfphase; | |
407 fr->fsizeold=fr->framesize; /* for Layer3 */ | |
408 | |
409 /* Hm, I never tested this...*/ | |
410 if (fr->p.halfspeed) | |
411 { | |
412 if(fr->halfphase) /* repeat last frame */ | |
413 { | |
414 debug("repeat!"); | |
415 fr->to_decode = fr->to_ignore = TRUE; | |
416 --fr->halfphase; | |
417 fr->bitindex = 0; | |
418 fr->wordpointer = (unsigned char *) fr->bsbuf; | |
419 if(fr->lay == 3) memcpy (fr->bsbuf, fr->ssave, fr->ssize); | |
420 if(fr->error_protection) fr->crc = getbits(fr, 16); /* skip crc */ | |
421 return 1; | |
422 } | |
423 else | |
424 { | |
425 fr->halfphase = fr->p.halfspeed - 1; | |
426 } | |
427 } | |
428 | |
429 read_again: | |
430 debug2("trying to get frame %li at 0x%lx", (long)fr->num+1, (unsigned long)fr->rd->tell(fr)); | |
431 if((ret = fr->rd->head_read(fr,&newhead)) <= 0){ debug("need more?"); goto read_frame_bad;} | |
432 | |
433 init_resync: | |
434 | |
435 fr->header_change = 2; /* output format change is possible... */ | |
436 if(fr->oldhead) /* check a following header for change */ | |
437 { | |
438 /* If they have the same sample rate. Note that only is _not_ the case for the first header, as we enforce sample rate match for following frames. | |
439 So, during one stream, only change of stereoness is possible and indicated by header_change == 2. */ | |
440 if((fr->oldhead & HDRSAMPMASK) == (newhead & HDRSAMPMASK)) | |
441 { | |
442 /* Now if both channel modes are mono... */ | |
443 if( (fr->oldhead & HDRCHANMASK) == 0 && (newhead & HDRCHANMASK) == 0) | |
444 fr->header_change = 1; | |
445 /* ...or stereo (of sorts), then we have a small header change */ | |
446 else if( (fr->oldhead & HDRCHANMASK) > 0 && (newhead & HDRCHANMASK) > 0) | |
447 fr->header_change = 1; | |
448 } | |
449 } | |
450 | |
451 #ifdef SKIP_JUNK | |
452 /* watch out for junk/tags on beginning of stream by invalid header */ | |
453 if(!fr->firsthead && !head_check(newhead) && !free_format_header(newhead)) { | |
454 int i; | |
455 | |
456 /* check for id3v2; first three bytes (of 4) are "ID3" */ | |
457 if((newhead & (unsigned long) 0xffffff00) == (unsigned long) 0x49443300) | |
458 { | |
459 int id3ret = 0; | |
460 id3ret = parse_new_id3(fr, newhead); | |
461 if (id3ret < 0){ debug("need more?"); ret = id3ret; goto read_frame_bad; } | |
462 else if(id3ret > 0){ debug("got ID3v2"); fr->metaflags |= MPG123_NEW_ID3|MPG123_ID3; } | |
463 else debug("no useful ID3v2"); | |
464 | |
465 fr->oldhead = 0; | |
466 goto read_again; /* Also in case of invalid ID3 tag (ret==0), try to get on track again. */ | |
467 } | |
468 else if(VERBOSE2 && fr->silent_resync == 0) fprintf(stderr,"Note: Junk at the beginning (0x%08lx)\n",newhead); | |
469 | |
470 /* I even saw RIFF headers at the beginning of MPEG streams ;( */ | |
471 if(newhead == ('R'<<24)+('I'<<16)+('F'<<8)+'F') { | |
472 if(VERBOSE2 && fr->silent_resync == 0) fprintf(stderr, "Note: Looks like a RIFF header.\n"); | |
473 | |
474 if((ret=fr->rd->head_read(fr,&newhead))<=0){ debug("need more?"); goto read_frame_bad; } | |
475 | |
476 while(newhead != ('d'<<24)+('a'<<16)+('t'<<8)+'a') | |
477 { | |
478 if((ret=fr->rd->head_shift(fr,&newhead))<=0){ debug("need more?"); goto read_frame_bad; } | |
479 } | |
480 if((ret=fr->rd->head_read(fr,&newhead))<=0){ debug("need more?"); goto read_frame_bad; } | |
481 | |
482 if(VERBOSE2 && fr->silent_resync == 0) fprintf(stderr,"Note: Skipped RIFF header!\n"); | |
483 | |
484 fr->oldhead = 0; | |
485 goto read_again; | |
486 } | |
487 /* unhandled junk... just continue search for a header */ | |
488 /* step in byte steps through next 64K */ | |
489 debug("searching for header..."); | |
490 for(i=0;i<65536;i++) { | |
491 if((ret=fr->rd->head_shift(fr,&newhead))<=0){ debug("need more?"); goto read_frame_bad; } | |
492 /* if(head_check(newhead)) */ | |
493 if(head_check(newhead) && decode_header(fr, newhead)) | |
494 break; | |
495 } | |
496 if(i == 65536) | |
497 { | |
498 if(NOQUIET) error("Giving up searching valid MPEG header after 64K of junk."); | |
499 return 0; | |
500 } | |
501 else debug("hopefully found one..."); | |
502 /* | |
503 * should we additionaly check, whether a new frame starts at | |
504 * the next expected position? (some kind of read ahead) | |
505 * We could implement this easily, at least for files. | |
506 */ | |
507 } | |
508 #endif | |
509 | |
510 /* first attempt of read ahead check to find the real first header; cannot believe what junk is out there! */ | |
511 /* for now, a spurious first free format header screws up here; need free format support for detecting false free format headers... */ | |
512 if(!fr->firsthead && fr->rdat.flags & (READER_SEEKABLE|READER_BUFFERED) && head_check(newhead) && decode_header(fr, newhead)) | |
513 { | |
514 unsigned long nexthead = 0; | |
515 int hd = 0; | |
516 off_t start = fr->rd->tell(fr); | |
517 debug2("doing ahead check with BPF %d at %li", fr->framesize+4, (long)start); | |
518 /* step framesize bytes forward and read next possible header*/ | |
519 if((ret=fr->rd->skip_bytes(fr, fr->framesize))<0) | |
520 { | |
521 if(ret==READER_ERROR && NOQUIET) error("cannot seek!"); | |
522 goto read_frame_bad; | |
523 } | |
524 hd = fr->rd->head_read(fr,&nexthead); | |
525 if(hd==MPG123_NEED_MORE){ debug("need more?"); ret = hd; goto read_frame_bad; } | |
526 if((ret=fr->rd->back_bytes(fr, fr->rd->tell(fr)-start))<0) | |
527 { | |
528 if(ret==READER_ERROR && NOQUIET) error("cannot seek!"); | |
529 else debug("need more?"); | |
530 goto read_frame_bad; | |
531 } | |
532 debug1("After fetching next header, at %li", (long)fr->rd->tell(fr)); | |
533 if(!hd) | |
534 { | |
535 if(NOQUIET) warning("cannot read next header, a one-frame stream? Duh..."); | |
536 } | |
537 else | |
538 { | |
539 debug2("does next header 0x%08lx match first 0x%08lx?", nexthead, newhead); | |
540 /* not allowing free format yet */ | |
541 if(!head_check(nexthead) || (nexthead & HDRCMPMASK) != (newhead & HDRCMPMASK)) | |
542 { | |
543 debug("No, the header was not valid, start from beginning..."); | |
544 fr->oldhead = 0; /* start over */ | |
545 /* try next byte for valid header */ | |
546 if((ret=fr->rd->back_bytes(fr, 3))<0) | |
547 { | |
548 if(NOQUIET) error("cannot seek!"); | |
549 else debug("need more?"); | |
550 goto read_frame_bad; | |
551 } | |
552 goto read_again; | |
553 } | |
554 } | |
555 } | |
556 | |
557 /* why has this head check been avoided here before? */ | |
558 if(!head_check(newhead)) | |
559 { | |
560 if(!fr->firsthead && free_format_header(newhead)) | |
561 { | |
562 if(NOQUIET) error1("Header 0x%08lx seems to indicate a free format stream; I do not handle that yet", newhead); | |
563 | |
564 goto read_again; | |
565 } | |
566 /* and those ugly ID3 tags */ | |
567 if((newhead & 0xffffff00) == ('T'<<24)+('A'<<16)+('G'<<8)) | |
568 { | |
569 fr->id3buf[0] = (unsigned char) ((newhead >> 24) & 0xff); | |
570 fr->id3buf[1] = (unsigned char) ((newhead >> 16) & 0xff); | |
571 fr->id3buf[2] = (unsigned char) ((newhead >> 8) & 0xff); | |
572 fr->id3buf[3] = (unsigned char) ( newhead & 0xff); | |
573 if((ret=fr->rd->fullread(fr,fr->id3buf+4,124)) < 0){ debug("need more?"); goto read_frame_bad; } | |
574 fr->metaflags |= MPG123_NEW_ID3|MPG123_ID3; | |
575 fr->rdat.flags |= READER_ID3TAG; /* that marks id3v1 */ | |
576 if (VERBOSE2) fprintf(stderr,"Note: Skipped ID3 Tag!\n"); | |
577 goto read_again; | |
578 } | |
579 /* duplicated code from above! */ | |
580 /* check for id3v2; first three bytes (of 4) are "ID3" */ | |
581 if((newhead & (unsigned long) 0xffffff00) == (unsigned long) 0x49443300) | |
582 { | |
583 int id3length = 0; | |
584 id3length = parse_new_id3(fr, newhead); | |
585 if(id3length < 0){ debug("need more?"); ret = id3length; goto read_frame_bad; } | |
586 | |
587 fr->metaflags |= MPG123_NEW_ID3|MPG123_ID3; | |
588 goto read_again; | |
589 } | |
590 else if(NOQUIET && fr->silent_resync == 0) | |
591 { | |
592 fprintf(stderr,"Note: Illegal Audio-MPEG-Header 0x%08lx at offset 0x%lx.\n", | |
593 newhead, (long unsigned int)fr->rd->tell(fr)-4); | |
594 } | |
595 | |
596 if(NOQUIET && (newhead & 0xffffff00) == ('b'<<24)+('m'<<16)+('p'<<8)) fprintf(stderr,"Note: Could be a BMP album art.\n"); | |
597 /* Do resync if not forbidden by flag. | |
598 I used to have a check for not-icy-meta here, but concluded that the desync issues came from a reader bug, not the stream. */ | |
599 if( !(fr->p.flags & MPG123_NO_RESYNC) ) | |
600 { | |
601 long try = 0; | |
602 long limit = fr->p.resync_limit; | |
603 /* TODO: make this more robust, I'd like to cat two mp3 fragments together (in a dirty way) and still have mpg123 beign able to decode all it somehow. */ | |
604 if(NOQUIET && fr->silent_resync == 0) fprintf(stderr, "Note: Trying to resync...\n"); | |
605 /* Read more bytes until we find something that looks | |
606 reasonably like a valid header. This is not a | |
607 perfect strategy, but it should get us back on the | |
608 track within a short time (and hopefully without | |
609 too much distortion in the audio output). */ | |
610 do | |
611 { | |
612 ++try; | |
613 if(limit >= 0 && try >= limit) break; | |
614 | |
615 if((ret=fr->rd->head_shift(fr,&newhead)) <= 0) | |
616 { | |
617 debug("need more?"); | |
618 if(NOQUIET) fprintf (stderr, "Note: Hit end of (available) data during resync.\n"); | |
619 | |
620 goto read_frame_bad; | |
621 } | |
622 if(VERBOSE3) debug3("resync try %li at 0x%lx, got newhead 0x%08lx", try, (unsigned long)fr->rd->tell(fr), newhead); | |
623 | |
624 if(!fr->oldhead) | |
625 { | |
626 debug("going to init_resync..."); | |
627 goto init_resync; /* "considered harmful", eh? */ | |
628 } | |
629 /* we should perhaps collect a list of valid headers that occured in file... there can be more */ | |
630 /* Michael's new resync routine seems to work better with the one frame readahead (and some input buffering?) */ | |
631 } while | |
632 ( | |
633 !head_check(newhead) /* Simply check for any valid header... we have the readahead to get it straight now(?) */ | |
634 /* (newhead & HDRCMPMASK) != (fr->oldhead & HDRCMPMASK) | |
635 && (newhead & HDRCMPMASK) != (fr->firsthead & HDRCMPMASK)*/ | |
636 ); | |
637 /* too many false positives | |
638 }while (!(head_check(newhead) && decode_header(fr, newhead))); */ | |
639 if(NOQUIET && fr->silent_resync == 0) fprintf (stderr, "Note: Skipped %li bytes in input.\n", try); | |
640 | |
641 if(limit >= 0 && try >= limit) | |
642 { | |
643 if(NOQUIET) | |
644 error1("Giving up resync after %li bytes - your stream is not nice... (maybe increasing resync limit could help).", try); | |
645 | |
646 fr->err = MPG123_RESYNC_FAIL; | |
647 return READER_ERROR; | |
648 } | |
649 else | |
650 { | |
651 debug1("Found valid header 0x%lx... unsetting firsthead to reinit stream.", newhead); | |
652 fr->firsthead = 0; | |
653 goto init_resync; | |
654 } | |
655 } | |
656 else | |
657 { | |
658 if(NOQUIET) error("not attempting to resync..."); | |
659 | |
660 fr->err = MPG123_OUT_OF_SYNC; | |
661 return READER_ERROR; | |
662 } | |
663 } | |
664 | |
665 if (!fr->firsthead) | |
666 { | |
667 if(!decode_header(fr,newhead)) | |
668 { | |
669 if(NOQUIET) error("decode header failed before first valid one, going to read again"); | |
670 | |
671 goto read_again; | |
672 } | |
673 } | |
674 else | |
675 if(!decode_header(fr,newhead)) | |
676 { | |
677 if(NOQUIET) error("decode header failed - goto resync"); | |
678 /* return 0; */ | |
679 goto init_resync; | |
680 } | |
681 | |
682 /* if filepos is invalid, so is framepos */ | |
683 framepos = fr->rd->tell(fr) - 4; | |
684 /* flip/init buffer for Layer 3 */ | |
685 { | |
686 unsigned char *newbuf = fr->bsspace[fr->bsnum]+512; | |
687 /* read main data into memory */ | |
688 if((ret=fr->rd->read_frame_body(fr,newbuf,fr->framesize))<0) | |
689 { | |
690 /* if failed: flip back */ | |
691 debug("need more?"); | |
692 goto read_frame_bad; | |
693 } | |
694 fr->bsbufold = fr->bsbuf; | |
695 fr->bsbuf = newbuf; | |
696 } | |
697 fr->bsnum = (fr->bsnum + 1) & 1; | |
698 | |
699 if(!fr->firsthead) | |
700 { | |
701 fr->firsthead = newhead; /* _now_ it's time to store it... the first real header */ | |
702 /* This is the first header of our current stream segment. | |
703 It is only the actual first header of the whole stream when fr->num is still below zero! | |
704 Think of resyncs where firsthead has been reset for format flexibility. */ | |
705 if(fr->num < 0) | |
706 { | |
707 fr->audio_start = framepos; | |
708 /* Only check for LAME tag at beginning of whole stream | |
709 ... when there indeed is one in between, it's the user's problem. */ | |
710 if(fr->lay == 3 && check_lame_tag(fr) == 1) | |
711 { /* ...in practice, Xing/LAME tags are layer 3 only. */ | |
712 if(fr->rd->forget != NULL) fr->rd->forget(fr); | |
713 | |
714 fr->oldhead = 0; | |
715 goto read_again; | |
716 } | |
717 /* now adjust volume */ | |
718 do_rva(fr); | |
719 } | |
720 | |
721 debug2("fr->firsthead: %08lx, audio_start: %li", fr->firsthead, (long int)fr->audio_start); | |
722 } | |
723 | |
724 fr->bitindex = 0; | |
725 fr->wordpointer = (unsigned char *) fr->bsbuf; | |
726 /* Question: How bad does the floating point value get with repeated recomputation? | |
727 Also, considering that we can play the file or parts of many times. */ | |
728 if(++fr->mean_frames != 0) | |
729 { | |
730 fr->mean_framesize = ((fr->mean_frames-1)*fr->mean_framesize+compute_bpf(fr)) / fr->mean_frames ; | |
731 } | |
732 ++fr->num; /* 0 for first frame! */ | |
733 debug4("Frame %li %08lx %i, next filepos=0x%lx", | |
734 (long)fr->num, newhead, fr->framesize, (long unsigned)fr->rd->tell(fr)); | |
735 /* save for repetition */ | |
736 if(fr->p.halfspeed && fr->lay == 3) | |
737 { | |
738 debug("halfspeed - reusing old bsbuf "); | |
739 memcpy (fr->ssave, fr->bsbuf, fr->ssize); | |
740 } | |
741 | |
742 /* index the position */ | |
743 #ifdef FRAME_INDEX | |
744 /* Keep track of true frame positions in our frame index. | |
745 but only do so when we are sure that the frame number is accurate... */ | |
746 if(fr->accurate && FI_NEXT(fr->index, fr->num)) | |
747 fi_add(&fr->index, framepos); | |
748 #endif | |
749 | |
750 if(fr->silent_resync > 0) --fr->silent_resync; | |
751 | |
752 if(fr->rd->forget != NULL) fr->rd->forget(fr); | |
753 | |
754 fr->to_decode = fr->to_ignore = TRUE; | |
755 if(fr->error_protection) fr->crc = getbits(fr, 16); /* skip crc */ | |
756 | |
757 return 1; | |
758 read_frame_bad: | |
759 fr->silent_resync = 0; | |
760 if(fr->err == MPG123_OK) fr->err = MPG123_ERR_READER; | |
761 fr->framesize = oldsize; | |
762 fr->halfphase = oldphase; | |
763 return ret; | |
764 } | |
765 | |
766 | |
767 /* | |
768 * decode a header and write the information | |
769 * into the frame structure | |
770 */ | |
771 static int decode_header(mpg123_handle *fr,unsigned long newhead) | |
772 { | |
773 if(!head_check(newhead)) | |
774 { | |
775 if(NOQUIET) error("tried to decode obviously invalid header"); | |
776 | |
777 return 0; | |
778 } | |
779 if( newhead & (1<<20) ) | |
780 { | |
781 fr->lsf = (newhead & (1<<19)) ? 0x0 : 0x1; | |
782 fr->mpeg25 = 0; | |
783 } | |
784 else | |
785 { | |
786 fr->lsf = 1; | |
787 fr->mpeg25 = 1; | |
788 } | |
789 | |
790 if( (fr->p.flags & MPG123_NO_RESYNC) || !fr->oldhead | |
791 || (((fr->oldhead>>19)&0x3) ^ ((newhead>>19)&0x3)) ) | |
792 { | |
793 /* If "tryresync" is false, assume that certain | |
794 parameters do not change within the stream! | |
795 Force an update if lsf or mpeg25 settings | |
796 have changed. */ | |
797 fr->lay = 4-((newhead>>17)&3); | |
798 if( ((newhead>>10)&0x3) == 0x3) | |
799 { | |
800 if(NOQUIET) error("Stream error"); | |
801 | |
802 return 0; /* exit() here really is too much, isn't it? */ | |
803 } | |
804 if(fr->mpeg25) | |
805 fr->sampling_frequency = 6 + ((newhead>>10)&0x3); | |
806 else | |
807 fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3); | |
808 } | |
809 | |
810 #ifdef DEBUG | |
811 if((((newhead>>16)&0x1)^0x1) != fr->error_protection) debug("changed crc bit!"); | |
812 #endif | |
813 fr->error_protection = ((newhead>>16)&0x1)^0x1; /* seen a file where this varies (old lame tag without crc, track with crc) */ | |
814 fr->bitrate_index = ((newhead>>12)&0xf); | |
815 fr->padding = ((newhead>>9)&0x1); | |
816 fr->extension = ((newhead>>8)&0x1); | |
817 fr->mode = ((newhead>>6)&0x3); | |
818 fr->mode_ext = ((newhead>>4)&0x3); | |
819 fr->copyright = ((newhead>>3)&0x1); | |
820 fr->original = ((newhead>>2)&0x1); | |
821 fr->emphasis = newhead & 0x3; | |
822 | |
823 fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2; | |
824 | |
825 fr->oldhead = newhead; | |
826 | |
827 if(!fr->bitrate_index) | |
828 { | |
829 if(NOQUIET) error1("encountered free format header %08lx in decode_header - not supported yet",newhead); | |
830 | |
831 return 0; | |
832 } | |
833 | |
834 switch(fr->lay) | |
835 { | |
836 case 1: | |
837 fr->do_layer = do_layer1; | |
838 fr->framesize = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000; | |
839 fr->framesize /= freqs[fr->sampling_frequency]; | |
840 fr->framesize = ((fr->framesize+fr->padding)<<2)-4; | |
841 break; | |
842 case 2: | |
843 fr->do_layer = do_layer2; | |
844 debug2("bitrate index: %i (%i)", fr->bitrate_index, tabsel_123[fr->lsf][1][fr->bitrate_index] ); | |
845 fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000; | |
846 fr->framesize /= freqs[fr->sampling_frequency]; | |
847 fr->framesize += fr->padding - 4; | |
848 break; | |
849 case 3: | |
850 fr->do_layer = do_layer3; | |
851 if(fr->lsf) | |
852 fr->ssize = (fr->stereo == 1) ? 9 : 17; | |
853 else | |
854 fr->ssize = (fr->stereo == 1) ? 17 : 32; | |
855 | |
856 if(fr->error_protection) | |
857 fr->ssize += 2; | |
858 | |
859 fr->framesize = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000; | |
860 fr->framesize /= freqs[fr->sampling_frequency]<<(fr->lsf); | |
861 fr->framesize = fr->framesize + fr->padding - 4; | |
862 break; | |
863 default: | |
864 if(NOQUIET) error("unknown layer type (!!)"); | |
865 | |
866 return 0; | |
867 } | |
868 if (fr->framesize > MAXFRAMESIZE) | |
869 { | |
870 if(NOQUIET) error1("Frame size too big: %d", fr->framesize+4-fr->padding); | |
871 | |
872 return (0); | |
873 } | |
874 return 1; | |
875 } | |
876 | |
877 void set_pointer(mpg123_handle *fr, long backstep) | |
878 { | |
879 fr->wordpointer = fr->bsbuf + fr->ssize - backstep; | |
880 if (backstep) | |
881 memcpy(fr->wordpointer,fr->bsbufold+fr->fsizeold-backstep,backstep); | |
882 | |
883 fr->bitindex = 0; | |
884 } | |
885 | |
886 /********************************/ | |
887 | |
888 double compute_bpf(mpg123_handle *fr) | |
889 { | |
890 double bpf; | |
891 | |
892 switch(fr->lay) | |
893 { | |
894 case 1: | |
895 bpf = tabsel_123[fr->lsf][0][fr->bitrate_index]; | |
896 bpf *= 12000.0 * 4.0; | |
897 bpf /= freqs[fr->sampling_frequency] <<(fr->lsf); | |
898 break; | |
899 case 2: | |
900 case 3: | |
901 bpf = tabsel_123[fr->lsf][fr->lay-1][fr->bitrate_index]; | |
902 bpf *= 144000; | |
903 bpf /= freqs[fr->sampling_frequency] << (fr->lsf); | |
904 break; | |
905 default: | |
906 bpf = 1.0; | |
907 } | |
908 | |
909 return bpf; | |
910 } | |
911 | |
912 double attribute_align_arg mpg123_tpf(mpg123_handle *fr) | |
913 { | |
914 static int bs[4] = { 0,384,1152,1152 }; | |
915 double tpf; | |
916 if(fr == NULL) return -1; | |
917 | |
918 tpf = (double) bs[fr->lay]; | |
919 tpf /= freqs[fr->sampling_frequency] << (fr->lsf); | |
920 return tpf; | |
921 } | |
922 | |
923 int attribute_align_arg mpg123_position(mpg123_handle *fr, off_t no, off_t buffsize, | |
924 off_t *current_frame, off_t *frames_left, | |
925 double *current_seconds, double *seconds_left) | |
926 { | |
927 double tpf; | |
928 double dt = 0.0; | |
929 off_t cur, left; | |
930 double curs, lefts; | |
931 | |
932 if(!fr || !fr->rd) /* Isn't this too paranoid? */ | |
933 { | |
934 debug("reader troubles!"); | |
935 return MPG123_ERR; | |
936 } | |
937 | |
938 no += fr->num; /* no starts out as offset */ | |
939 cur = no; | |
940 tpf = mpg123_tpf(fr); | |
941 if(buffsize > 0 && fr->af.rate > 0 && fr->af.channels > 0) | |
942 { | |
943 dt = (double) buffsize / fr->af.rate / fr->af.channels; | |
944 if(fr->af.encoding & MPG123_ENC_16) dt *= 0.5; | |
945 } | |
946 | |
947 left = 0; | |
948 | |
949 if((fr->track_frames != 0) && (fr->track_frames >= fr->num)) left = no < fr->track_frames ? fr->track_frames - no : 0; | |
950 else | |
951 if(fr->rdat.filelen >= 0) | |
952 { | |
953 double bpf; | |
954 off_t t = fr->rd->tell(fr); | |
955 bpf = fr->mean_framesize ? fr->mean_framesize : compute_bpf(fr); | |
956 left = (off_t)((double)(fr->rdat.filelen-t)/bpf); | |
957 /* no can be different for prophetic purposes, file pointer is always associated with fr->num! */ | |
958 if(fr->num != no) | |
959 { | |
960 if(fr->num > no) left += fr->num - no; | |
961 else | |
962 { | |
963 if(left >= (no - fr->num)) left -= no - fr->num; | |
964 else left = 0; /* uh, oh! */ | |
965 } | |
966 } | |
967 /* I totally don't understand why we should re-estimate the given correct(?) value */ | |
968 /* fr->num = (unsigned long)((double)t/bpf); */ | |
969 } | |
970 | |
971 /* beginning with 0 or 1?*/ | |
972 curs = (double) no*tpf-dt; | |
973 lefts = (double)left*tpf+dt; | |
974 #if 0 | |
975 curs = curs < 0 ? 0.0 : curs; | |
976 #endif | |
977 if(left < 0 || lefts < 0) | |
978 { /* That is the case for non-seekable streams. */ | |
979 left = 0; | |
980 lefts = 0.0; | |
981 } | |
982 if(current_frame != NULL) *current_frame = cur; | |
983 if(frames_left != NULL) *frames_left = left; | |
984 if(current_seconds != NULL) *current_seconds = curs; | |
985 if(seconds_left != NULL) *seconds_left = lefts; | |
986 return MPG123_OK; | |
987 } | |
988 | |
989 int get_songlen(mpg123_handle *fr,int no) | |
990 { | |
991 double tpf; | |
992 | |
993 if(!fr) | |
994 return 0; | |
995 | |
996 if(no < 0) { | |
997 if(!fr->rd || fr->rdat.filelen < 0) | |
998 return 0; | |
999 no = (double) fr->rdat.filelen / compute_bpf(fr); | |
1000 } | |
1001 | |
1002 tpf = mpg123_tpf(fr); | |
1003 return no*tpf; | |
1004 } | |
1005 | |
1006 | |
1007 | |
1008 /* take into account: channels, bytes per sample -- NOT resampling!*/ | |
1009 off_t samples_to_bytes(mpg123_handle *fr , off_t s) | |
1010 { | |
1011 return s | |
1012 #ifdef FLOATOUT | |
1013 * 4 | |
1014 #else | |
1015 * ((fr->af.encoding & MPG123_ENC_16) ? 2 : 1) | |
1016 #endif | |
1017 * fr->af.channels; | |
1018 } | |
1019 | |
1020 off_t bytes_to_samples(mpg123_handle *fr , off_t b) | |
1021 { | |
1022 return b | |
1023 #ifdef FLOATOUT | |
1024 / 4 | |
1025 #else | |
1026 / ((fr->af.encoding & MPG123_ENC_16) ? 2 : 1) | |
1027 #endif | |
1028 / fr->af.channels; | |
1029 } |