Rev 576 | Rev 799 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
646 | serge | 1 | #include "mpg123.h" |
2 | #include "..\kolibri.h" |
||
3 | |||
4 | #define MAXFRAMESIZE 3456 |
||
5 | |||
6 | static int fsizeold=0,ssize; |
||
7 | static unsigned char bsspace[2][MAXFRAMESIZE+512]; /* MAXFRAMESIZE */ |
||
8 | static unsigned char *bsbuf=bsspace[1],*bsbufold; |
||
9 | static int bsnum=0; |
||
10 | |||
11 | static unsigned long oldhead = 0; |
||
12 | unsigned long firsthead=0; |
||
13 | |||
14 | struct bitstream_info bsi; |
||
15 | |||
16 | int tabsel_123[2][3][16] = { |
||
17 | { {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448,}, |
||
18 | {0,32,48,56, 64, 80, 96,112,128,160,192,224,256,320,384,}, |
||
19 | {0,32,40,48, 56, 64, 80, 96,112,128,160,192,224,256,320,} }, |
||
20 | |||
21 | { {0,32,48,56,64,80,96,112,128,144,160,176,192,224,256,}, |
||
22 | {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,}, |
||
23 | {0,8,16,24,32,40,48,56,64,80,96,112,128,144,160,} } |
||
24 | }; |
||
25 | |||
26 | int freqs[9] = { 44100, 48000, 32000, 22050, 24000, 16000 , 11025 , 12000 , 8000 }; |
||
27 | |||
28 | int stream_head_read(struct reader *rd,unsigned long *newhead); |
||
29 | int stream_read_raw(struct reader *rd,unsigned char *buf, int size); |
||
30 | |||
31 | void set_synth_functions(struct frame *fr) |
||
32 | { |
||
33 | #ifdef USE_3DNOW |
||
34 | static func_dct36 funcs_dct36[2] = {dct36 , dct36_3dnow}; |
||
35 | #endif |
||
36 | |||
37 | fr->synth = synth_1to1; |
||
38 | fr->synth_mono = synth_1to1_mono2stereo;; |
||
39 | |||
40 | /* TODO: make autodetection for _all_ x86 optimizations (maybe just for i586+ and keep separate 486 build?) */ |
||
41 | #ifdef USE_3DNOW |
||
42 | /* check cpuflags bit 31 (3DNow!) and 23 (MMX) */ |
||
43 | if((param.stat_3dnow < 2) && |
||
44 | ((param.stat_3dnow == 1) || |
||
45 | (getcpuflags() & 0x80800000) == 0x80800000)) |
||
46 | { |
||
47 | fr->synth = funcs[2][ds]; /* 3DNow! optimized synth_1to1() */ |
||
48 | fr->dct36 = funcs_dct36[1]; /* 3DNow! optimized dct36() */ |
||
49 | } |
||
50 | else |
||
51 | { |
||
52 | fr->dct36 = funcs_dct36[0]; |
||
53 | } |
||
54 | #endif |
||
55 | } |
||
56 | |||
57 | int __stdcall create_reader(struct reader *rd,byte *buffer, int buffsize) |
||
58 | { rd->head_read = stream_head_read; |
||
59 | rd->read_frame_body = stream_read_raw; |
||
60 | |||
61 | rd->buffer = buffer; |
||
62 | rd->stream = buffer; |
||
63 | rd->strpos = 0; |
||
64 | |||
65 | rd->strremain = 0; |
||
66 | rd->filepos = 0; |
||
67 | return 1; |
||
68 | }; |
||
69 | |||
70 | int __stdcall init_reader(struct reader *rd, char *file) |
||
71 | { FILEINFO fileinfo; |
||
72 | int retval; |
||
73 | int bytes; |
||
74 | |||
75 | rd->hFile = file; |
||
576 | serge | 76 | get_fileinfo(file, &fileinfo); |
77 | |||
646 | serge | 78 | rd->filelen = fileinfo.size; |
79 | rd->strpos = 0; |
||
576 | serge | 80 | retval=read_file (file,rd->buffer,0,0x10000,&bytes); |
646 | serge | 81 | |
82 | if( (retval==0)||(retval==6)) |
||
83 | { |
||
84 | rd->strremain=bytes; |
||
85 | rd->filepos=bytes; |
||
86 | rd->strpos = 0; |
||
87 | return 1; |
||
88 | }; |
||
89 | return 0; |
||
90 | }; |
||
91 | |||
92 | static int fill_reader(struct reader *rd) |
||
93 | { int retval; |
||
94 | int bytes; |
||
95 | |||
96 | if(rd->strremain > 0) |
||
97 | mem_cpy(rd->buffer,rd->stream,rd->strremain); |
||
98 | |||
99 | rd->stream = rd->buffer; |
||
100 | retval=read_file (rd->hFile,rd->buffer+rd->strremain,rd->filepos, |
||
101 | 0x10000-rd->strremain,&bytes); |
||
102 | if( (retval==0)||(retval==6)) |
||
103 | { |
||
104 | rd->strremain+=bytes; |
||
105 | rd->filepos+=bytes; |
||
106 | rd->strpos = 0; |
||
107 | return bytes; |
||
108 | }; |
||
109 | return 0; |
||
110 | }; |
||
111 | |||
112 | int __stdcall set_reader(struct reader *rd, unsigned int filepos) |
||
113 | { int retval; |
||
114 | unsigned int bytes; |
||
115 | retval=read_file (rd->hFile,rd->buffer,filepos,0x10000,&bytes); |
||
116 | if( (retval==0)||(retval==6)) |
||
117 | { |
||
118 | rd->stream = rd->buffer; |
||
119 | rd->strremain=bytes; |
||
120 | rd->filepos=filepos+bytes; |
||
121 | rd->strpos = 0; |
||
122 | |||
123 | fsizeold=0; |
||
124 | firsthead=0; |
||
125 | bsbufold = 0; |
||
126 | bsbuf = bsspace[1]; |
||
127 | bsnum = 0; |
||
128 | ssize=0; |
||
129 | oldhead=0; |
||
130 | memset(bsspace,0,sizeof(bsspace)); |
||
131 | return 1; |
||
132 | }; |
||
133 | return 0; |
||
134 | }; |
||
135 | |||
136 | static int stream_head_read(struct reader *rd,unsigned long *newhead) |
||
137 | { |
||
138 | if(rd->strremain < 4) |
||
139 | if( !fill_reader(rd)) |
||
140 | return 0; |
||
141 | *newhead = (rd->stream[0]<<24)|(rd->stream[1] << 16)| |
||
142 | (rd->stream[2] << 8)| rd->stream[3]; |
||
143 | rd->strpos+=4; |
||
144 | rd->stream+=4; |
||
145 | rd->strremain-=4; |
||
146 | return TRUE; |
||
147 | }; |
||
148 | |||
149 | int stream_read_raw(struct reader *rd,unsigned char *buf, int size) |
||
150 | { |
||
151 | |||
152 | if(rd->strremain < size) fill_reader(rd); |
||
153 | |||
154 | if(size > rd->strremain) size=rd->strremain; |
||
155 | |||
156 | if(size>0) |
||
157 | { |
||
158 | mem_cpy(buf,rd->stream,size); |
||
159 | rd->strpos+=size; |
||
160 | rd->stream+=size; |
||
161 | rd->strremain-=size; |
||
162 | return size; |
||
163 | }; |
||
164 | return 0; |
||
165 | }; |
||
166 | |||
167 | void set_pointer(long backstep) |
||
168 | { |
||
169 | bsi.wordpointer = bsbuf + ssize - backstep; |
||
170 | if (backstep) |
||
171 | mem_cpy(bsi.wordpointer,bsbufold+fsizeold-backstep,backstep); |
||
172 | bsi.bitindex = 0; |
||
173 | } |
||
174 | |||
175 | int head_check(unsigned long head) |
||
176 | { if |
||
177 | ( |
||
178 | /* first 11 bits are set to 1 for frame sync */ |
||
179 | ((head & 0xffe00000) != 0xffe00000) |
||
180 | || |
||
181 | /* layer: 01,10,11 is 1,2,3; 00 is reserved */ |
||
182 | (!((head>>17)&3)) |
||
183 | || |
||
184 | /* 1111 means bad bitrate */ |
||
185 | (((head>>12)&0xf) == 0xf) |
||
186 | || |
||
187 | /* 0000 means free format... */ |
||
188 | (((head>>12)&0xf) == 0x0) |
||
189 | || |
||
190 | /* sampling freq: 11 is reserved */ |
||
191 | (((head>>10)&0x3) == 0x3 ) |
||
192 | /* 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 */ |
||
193 | ) |
||
194 | { |
||
195 | return FALSE; |
||
196 | } |
||
197 | /* if no check failed, the header is valid (hopefully)*/ |
||
198 | else |
||
199 | { |
||
200 | return TRUE; |
||
201 | } |
||
202 | } |
||
203 | |||
204 | int __stdcall decode_header(struct frame *fr,unsigned long newhead) |
||
205 | { |
||
206 | if(!head_check(newhead)) |
||
207 | return 0; |
||
208 | if( newhead & (1<<20) ) |
||
209 | { fr->lsf = (newhead & (1<<19)) ? 0x0 : 0x1; |
||
210 | fr->mpeg25 = 0; |
||
211 | } |
||
212 | else |
||
213 | { fr->lsf = 1; |
||
214 | fr->mpeg25 = 1; |
||
215 | }; |
||
216 | |||
217 | fr->lay = 4-((newhead>>17)&3); |
||
218 | if(fr->mpeg25) |
||
219 | fr->sampling_frequency = 6 + ((newhead>>10)&0x3); |
||
220 | else |
||
221 | fr->sampling_frequency = ((newhead>>10)&0x3) + (fr->lsf*3); |
||
222 | fr->error_protection = ((newhead>>16)&0x1)^0x1; |
||
223 | |||
224 | fr->bitrate_index = ((newhead>>12)&0xf); |
||
225 | fr->padding = ((newhead>>9)&0x1); |
||
226 | fr->extension = ((newhead>>8)&0x1); |
||
227 | fr->mode = ((newhead>>6)&0x3); |
||
228 | fr->mode_ext = ((newhead>>4)&0x3); |
||
229 | fr->copyright = ((newhead>>3)&0x1); |
||
230 | fr->original = ((newhead>>2)&0x1); |
||
231 | fr->emphasis = newhead & 0x3; |
||
232 | |||
233 | fr->stereo = (fr->mode == MPG_MD_MONO) ? 1 : 2; |
||
234 | |||
235 | oldhead = newhead; |
||
236 | |||
237 | if(!fr->bitrate_index) |
||
238 | return (0); |
||
239 | |||
240 | switch(fr->lay) |
||
241 | { case 1: |
||
242 | fr->do_layer = do_layer1; |
||
243 | #ifdef VARMODESUPPORT |
||
244 | if (varmode) { |
||
245 | error("Sorry, layer-1 not supported in varmode."); |
||
246 | return (0); |
||
247 | } |
||
248 | #endif |
||
249 | fr->framesize = (long) tabsel_123[fr->lsf][0][fr->bitrate_index] * 12000; |
||
250 | fr->framesize /= freqs[fr->sampling_frequency]; |
||
251 | fr->framesize = ((fr->framesize+fr->padding)<<2)-4; |
||
252 | break; |
||
253 | case 2: |
||
254 | fr->do_layer = do_layer2; |
||
255 | #ifdef VARMODESUPPORT |
||
256 | if (varmode) { |
||
257 | error("Sorry, layer-2 not supported in varmode."); |
||
258 | return (0); |
||
259 | } |
||
260 | #endif |
||
261 | fr->framesize = (long) tabsel_123[fr->lsf][1][fr->bitrate_index] * 144000; |
||
262 | fr->framesize /= freqs[fr->sampling_frequency]; |
||
263 | fr->framesize += fr->padding - 4; |
||
264 | break; |
||
265 | case 3: |
||
266 | fr->do_layer = do_layer3; |
||
267 | if(fr->lsf) |
||
268 | ssize = (fr->stereo == 1) ? 9 : 17; |
||
269 | else |
||
270 | ssize = (fr->stereo == 1) ? 17 : 32; |
||
271 | if(fr->error_protection) |
||
272 | ssize += 2; |
||
273 | fr->framesize = (long) tabsel_123[fr->lsf][2][fr->bitrate_index] * 144000; |
||
274 | fr->framesize /= freqs[fr->sampling_frequency]<<(fr->lsf); |
||
275 | fr->framesize = fr->framesize + fr->padding - 4; |
||
276 | break; |
||
277 | default: |
||
278 | return (0); |
||
279 | } |
||
280 | if (fr->framesize > MAXFRAMESIZE) |
||
281 | return (0); |
||
282 | return 1; |
||
283 | } |
||
284 | |||
285 | |||
286 | int read_frame(struct reader *rd, struct frame *fr) |
||
287 | { unsigned long newhead; |
||
288 | static unsigned char ssave[34]; |
||
289 | //off_t framepos; |
||
290 | fsizeold=fr->framesize; /* for Layer3 */ |
||
291 | |||
292 | read_again: |
||
293 | |||
294 | if(!rd->head_read(rd,&newhead)) |
||
295 | return FALSE; |
||
296 | |||
297 | if(!decode_header(fr,newhead)) |
||
298 | { |
||
299 | if((newhead & 0xffffff00) == 0x49443300) |
||
300 | { |
||
301 | int id3length = 0; |
||
302 | id3length = parse_new_id3(rd, newhead); |
||
303 | goto read_again; |
||
304 | }; |
||
305 | rd->strpos-=3; |
||
306 | rd->stream-=3; |
||
307 | rd->strremain+=3; |
||
308 | goto read_again; |
||
309 | }; |
||
310 | |||
311 | #if 0 |
||
312 | if(1 || oldhead != newhead || !oldhead) |
||
313 | { |
||
314 | |||
315 | init_resync: |
||
316 | |||
317 | #ifdef SKIP_JUNK |
||
318 | /* watch out for junk/tags on beginning of stream by invalid header */ |
||
319 | if(!firsthead && !head_check(newhead) && !free_format_header(newhead)) { |
||
320 | int i; |
||
321 | |||
322 | /* check for id3v2; first three bytes (of 4) are "ID3" */ |
||
323 | if((newhead & (unsigned long) 0xffffff00) == (unsigned long) 0x49443300) |
||
324 | { |
||
325 | int id3length = 0; |
||
326 | id3length = parse_new_id3(newhead, rd); |
||
327 | goto read_again; |
||
328 | } |
||
329 | else if(param.verbose > 1) fprintf(stderr,"Note: Junk at the beginning (0x%08lx)\n",newhead); |
||
330 | |||
331 | /* I even saw RIFF headers at the beginning of MPEG streams ;( */ |
||
332 | if(newhead == ('R'<<24)+('I'<<16)+('F'<<8)+'F') { |
||
333 | if(param.verbose > 1) fprintf(stderr, "Note: Looks like a RIFF header.\n"); |
||
334 | if(!rd->head_read(rd,&newhead)) |
||
335 | return 0; |
||
336 | while(newhead != ('d'<<24)+('a'<<16)+('t'<<8)+'a') { |
||
337 | if(!rd->head_shift(rd,&newhead)) |
||
338 | return 0; |
||
339 | } |
||
340 | if(!rd->head_read(rd,&newhead)) |
||
341 | return 0; |
||
342 | if(param.verbose > 1) fprintf(stderr,"Note: Skipped RIFF header!\n"); |
||
343 | goto read_again; |
||
344 | } |
||
345 | /* unhandled junk... just continue search for a header */ |
||
346 | /* step in byte steps through next 64K */ |
||
347 | for(i=0;i<65536;i++) { |
||
348 | if(!rd->head_shift(rd,&newhead)) |
||
349 | return 0; |
||
350 | /* if(head_check(newhead)) */ |
||
351 | if(head_check(newhead) && decode_header(fr, newhead)) |
||
352 | break; |
||
353 | } |
||
354 | if(i == 65536) { |
||
355 | if(!param.quiet) error("Giving up searching valid MPEG header after 64K of junk."); |
||
356 | return 0; |
||
357 | } |
||
358 | /* |
||
359 | * should we additionaly check, whether a new frame starts at |
||
360 | * the next expected position? (some kind of read ahead) |
||
361 | * We could implement this easily, at least for files. |
||
362 | */ |
||
363 | } |
||
364 | #endif |
||
365 | |||
366 | /* first attempt of read ahead check to find the real first header; cannot believe what junk is out there! */ |
||
367 | /* for now, a spurious first free format header screws up here; need free format support for detecting false free format headers... */ |
||
368 | if(!firsthead && rd->flags & READER_SEEKABLE && head_check(newhead) && decode_header(fr, newhead)) |
||
369 | { |
||
370 | unsigned long nexthead = 0; |
||
371 | int hd = 0; |
||
372 | off_t start = rd->tell(rd); |
||
373 | debug1("doing ahead check with BPF %d", fr->framesize+4); |
||
374 | /* step framesize bytes forward and read next possible header*/ |
||
375 | if(rd->back_bytes(rd, -fr->framesize)) |
||
376 | { |
||
377 | error("cannot seek!"); |
||
378 | return 0; |
||
379 | } |
||
380 | hd = rd->head_read(rd,&nexthead); |
||
381 | if(rd->back_bytes(rd, rd->tell(rd)-start)) |
||
382 | { |
||
383 | error("cannot seek!"); |
||
384 | return 0; |
||
385 | } |
||
386 | if(!hd) |
||
387 | { |
||
388 | warning("cannot read next header, a one-frame stream? Duh..."); |
||
389 | } |
||
390 | else |
||
391 | { |
||
392 | debug2("does next header 0x%08lx match first 0x%08lx?", nexthead, newhead); |
||
393 | /* not allowing free format yet */ |
||
394 | if(!head_check(nexthead) || (nexthead & HDRCMPMASK) != (newhead & HDRCMPMASK)) |
||
395 | { |
||
396 | debug("No, the header was not valid, start from beginning..."); |
||
397 | /* try next byte for valid header */ |
||
398 | if(rd->back_bytes(rd, 3)) |
||
399 | { |
||
400 | error("cannot seek!"); |
||
401 | return 0; |
||
402 | } |
||
403 | goto read_again; |
||
404 | } |
||
405 | } |
||
406 | } |
||
407 | |||
408 | /* why has this head check been avoided here before? */ |
||
409 | if(!head_check(newhead)) |
||
410 | { |
||
411 | if(!firsthead && free_format_header(newhead)) |
||
412 | { |
||
413 | error1("Header 0x%08lx seems to indicate a free format stream; I do not handle that yet", newhead); |
||
414 | goto read_again; |
||
415 | return 0; |
||
416 | } |
||
417 | /* and those ugly ID3 tags */ |
||
418 | if((newhead & 0xffffff00) == ('T'<<24)+('A'<<16)+('G'<<8)) { |
||
419 | rd->skip_bytes(rd,124); |
||
420 | if (param.verbose > 1) fprintf(stderr,"Note: Skipped ID3 Tag!\n"); |
||
421 | goto read_again; |
||
422 | } |
||
423 | /* duplicated code from above! */ |
||
424 | /* check for id3v2; first three bytes (of 4) are "ID3" */ |
||
425 | if((newhead & (unsigned long) 0xffffff00) == (unsigned long) 0x49443300) |
||
426 | { |
||
427 | int id3length = 0; |
||
428 | id3length = parse_new_id3(newhead, rd); |
||
429 | goto read_again; |
||
430 | } |
||
431 | else if (give_note) |
||
432 | { |
||
433 | fprintf(stderr,"Note: Illegal Audio-MPEG-Header 0x%08lx at offset 0x%lx.\n", newhead,rd->tell(rd)-4); |
||
434 | } |
||
435 | |||
436 | if(give_note && (newhead & 0xffffff00) == ('b'<<24)+('m'<<16)+('p'<<8)) fprintf(stderr,"Note: Could be a BMP album art.\n"); |
||
437 | if (param.tryresync || do_recover) { |
||
438 | int try = 0; |
||
439 | /* 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. */ |
||
440 | if(give_note) fprintf(stderr, "Note: Trying to resync...\n"); |
||
441 | /* Read more bytes until we find something that looks |
||
442 | reasonably like a valid header. This is not a |
||
443 | perfect strategy, but it should get us back on the |
||
444 | track within a short time (and hopefully without |
||
445 | too much distortion in the audio output). */ |
||
446 | do { |
||
447 | if(!rd->head_shift(rd,&newhead)) |
||
448 | return 0; |
||
449 | /* debug2("resync try %i, got newhead 0x%08lx", try, newhead); */ |
||
450 | if (!oldhead) |
||
451 | { |
||
452 | debug("going to init_resync..."); |
||
453 | goto init_resync; /* "considered harmful", eh? */ |
||
454 | } |
||
455 | /* we should perhaps collect a list of valid headers that occured in file... there can be more */ |
||
456 | /* Michael's new resync routine seems to work better with the one frame readahead (and some input buffering?) */ |
||
457 | } while |
||
458 | ( |
||
459 | ++try < RESYNC_LIMIT |
||
460 | && (newhead & HDRCMPMASK) != (oldhead & HDRCMPMASK) |
||
461 | && (newhead & HDRCMPMASK) != (firsthead & HDRCMPMASK) |
||
462 | ); |
||
463 | /* too many false positives |
||
464 | }while (!(head_check(newhead) && decode_header(fr, newhead))); */ |
||
465 | if(try == RESYNC_LIMIT) |
||
466 | { |
||
467 | error("giving up resync - your stream is not nice... perhaps an improved routine could catch up"); |
||
468 | return 0; |
||
469 | } |
||
470 | |||
471 | if (give_note) |
||
472 | fprintf (stderr, "Note: Skipped %d bytes in input.\n", try); |
||
473 | } |
||
474 | else |
||
475 | { |
||
476 | error("not attempting to resync..."); |
||
477 | return (0); |
||
478 | } |
||
479 | } |
||
480 | |||
481 | if (!firsthead) { |
||
482 | if(!decode_header(fr,newhead)) |
||
483 | { |
||
484 | error("decode header failed before first valid one, going to read again"); |
||
485 | goto read_again; |
||
486 | } |
||
487 | } |
||
488 | else |
||
489 | if(!decode_header(fr,newhead)) |
||
490 | { |
||
491 | error("decode header failed - goto resync"); |
||
492 | /* return 0; */ |
||
493 | goto init_resync; |
||
494 | } |
||
495 | } |
||
496 | else |
||
497 | fr->header_change = 0; |
||
498 | #endif |
||
499 | |||
500 | bsbufold = bsbuf; |
||
501 | bsbuf = bsspace[bsnum]+512; |
||
502 | bsnum = (bsnum + 1) & 1; |
||
503 | /* if filepos is invalid, so is framepos */ |
||
504 | //framepos = rd->filepos - 4; |
||
505 | /* read main data into memory */ |
||
506 | /* 0 is error! */ |
||
507 | |||
508 | if(rd->read_frame_body(rd,bsbuf,fr->framesize) < fr->framesize) |
||
509 | return 0; |
||
510 | |||
511 | #if 0 |
||
512 | if(!firsthead) |
||
513 | { |
||
514 | /* following stuff is actually layer3 specific (in practice, not in theory) */ |
||
515 | if(fr->lay == 3) |
||
516 | { |
||
517 | /* |
||
518 | going to look for Xing or Info at some position after the header |
||
519 | MPEG 1 MPEG 2/2.5 (LSF) |
||
520 | Stereo, Joint Stereo, Dual Channel 32 17 |
||
521 | Mono 17 9 |
||
522 | |||
523 | Also, how to avoid false positives? I guess I should interpret more of the header to rule that out(?). |
||
524 | I hope that ensuring all zeros until tag start is enough. |
||
525 | */ |
||
526 | size_t lame_offset = (fr->stereo == 2) ? (fr->lsf ? 17 : 32 ) : (fr->lsf ? 9 : 17); |
||
527 | if(fr->framesize >= 120+lame_offset) /* traditional Xing header is 120 bytes */ |
||
528 | { |
||
529 | size_t i; |
||
530 | int lame_type = 0; |
||
531 | /* only search for tag when all zero before it (apart from checksum) */ |
||
532 | for(i=2; i < lame_offset; ++i) if(bsbuf[i] != 0) break; |
||
533 | if(i == lame_offset) |
||
534 | { |
||
535 | if |
||
536 | ( |
||
537 | (bsbuf[lame_offset] == 'I') |
||
538 | && (bsbuf[lame_offset+1] == 'n') |
||
539 | && (bsbuf[lame_offset+2] == 'f') |
||
540 | && (bsbuf[lame_offset+3] == 'o') |
||
541 | ) |
||
542 | { |
||
543 | lame_type = 1; /* We still have to see what there is */ |
||
544 | } |
||
545 | else if |
||
546 | ( |
||
547 | (bsbuf[lame_offset] == 'X') |
||
548 | && (bsbuf[lame_offset+1] == 'i') |
||
549 | && (bsbuf[lame_offset+2] == 'n') |
||
550 | && (bsbuf[lame_offset+3] == 'g') |
||
551 | ) |
||
552 | { |
||
553 | lame_type = 2; |
||
554 | vbr = VBR; /* Xing header means always VBR */ |
||
555 | } |
||
556 | if(lame_type) |
||
557 | { |
||
558 | unsigned long xing_flags; |
||
559 | |||
560 | /* we have one of these headers... */ |
||
561 | if(param.verbose > 1) fprintf(stderr, "Note: Xing/Lame/Info header detected\n"); |
||
562 | /* now interpret the Xing part, I have 120 bytes total for sure */ |
||
563 | /* there are 4 bytes for flags, but only the last byte contains known ones */ |
||
564 | lame_offset += 4; /* now first byte after Xing/Name */ |
||
565 | /* 4 bytes dword for flags */ |
||
566 | #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])) |
||
567 | /* 16 bit */ |
||
568 | #define make_short(a,o) ((((unsigned short) a[o]) << 8) | ((unsigned short) a[o+1])) |
||
569 | xing_flags = make_long(bsbuf, lame_offset); |
||
570 | lame_offset += 4; |
||
571 | debug1("Xing: flags 0x%08lx", xing_flags); |
||
572 | if(xing_flags & 1) /* frames */ |
||
573 | { |
||
574 | /* |
||
575 | In theory, one should use that value for skipping... |
||
576 | When I know the exact number of samples I could simply count in audio_flush, |
||
577 | but that's problematic with seeking and such. |
||
578 | I still miss the real solution for detecting the end. |
||
579 | */ |
||
580 | track_frames = make_long(bsbuf, lame_offset); |
||
581 | if(track_frames > TRACK_MAX_FRAMES) track_frames = 0; /* endless stream? */ |
||
582 | #ifdef GAPLESS |
||
583 | /* if no further info there, remove/add at least the decoder delay */ |
||
584 | if(param.gapless) |
||
585 | { |
||
586 | unsigned long length = track_frames * spf(fr); |
||
587 | if(length > 1) |
||
588 | layer3_gapless_init(DECODER_DELAY+GAP_SHIFT, length+DECODER_DELAY+GAP_SHIFT); |
||
589 | } |
||
590 | #endif |
||
591 | debug1("Xing: %lu frames", track_frames); |
||
592 | lame_offset += 4; |
||
593 | } |
||
594 | if(xing_flags & 0x2) /* bytes */ |
||
595 | { |
||
596 | #ifdef DEBUG |
||
597 | unsigned long xing_bytes = make_long(bsbuf, lame_offset); |
||
598 | debug1("Xing: %lu bytes", xing_bytes); |
||
599 | #endif |
||
600 | lame_offset += 4; |
||
601 | } |
||
602 | if(xing_flags & 0x4) /* TOC */ |
||
603 | { |
||
604 | lame_offset += 100; /* just skip */ |
||
605 | } |
||
606 | if(xing_flags & 0x8) /* VBR quality */ |
||
607 | { |
||
608 | #ifdef DEBUG |
||
609 | unsigned long xing_quality = make_long(bsbuf, lame_offset); |
||
610 | debug1("Xing: quality = %lu", xing_quality); |
||
611 | #endif |
||
612 | lame_offset += 4; |
||
613 | } |
||
614 | /* I guess that either 0 or LAME extra data follows */ |
||
615 | /* there may this crc16 be floating around... (?) */ |
||
616 | if(bsbuf[lame_offset] != 0) |
||
617 | { |
||
618 | unsigned char lame_vbr; |
||
619 | float replay_gain[2] = {0,0}; |
||
620 | float peak = 0; |
||
621 | float gain_offset = 0; /* going to be +6 for old lame that used 83dB */ |
||
622 | char nb[10]; |
||
623 | memcpy(nb, bsbuf+lame_offset, 9); |
||
624 | nb[9] = 0; |
||
625 | debug1("Info: Encoder: %s", nb); |
||
626 | if(!strncmp("LAME", nb, 4)) |
||
627 | { |
||
628 | gain_offset = 6; |
||
629 | debug("TODO: finish lame detetcion..."); |
||
630 | } |
||
631 | lame_offset += 9; |
||
632 | /* the 4 big bits are tag revision, the small bits vbr method */ |
||
633 | lame_vbr = bsbuf[lame_offset] & 15; |
||
634 | debug1("Info: rev %u", bsbuf[lame_offset] >> 4); |
||
635 | debug1("Info: vbr mode %u", lame_vbr); |
||
636 | lame_offset += 1; |
||
637 | switch(lame_vbr) |
||
638 | { |
||
639 | /* from rev1 proposal... not sure if all good in practice */ |
||
640 | case 1: |
||
641 | case 8: vbr = CBR; break; |
||
642 | case 2: |
||
643 | case 9: vbr = ABR; break; |
||
644 | default: vbr = VBR; /* 00==unknown is taken as VBR */ |
||
645 | } |
||
646 | /* skipping: lowpass filter value */ |
||
647 | lame_offset += 1; |
||
648 | /* replaygain */ |
||
649 | /* 32bit float: peak amplitude -- why did I parse it as int before??*/ |
||
650 | /* Ah, yes, lame seems to store it as int since some day in 2003; I've only seen zeros anyway until now, bah! */ |
||
651 | if |
||
652 | ( |
||
653 | (bsbuf[lame_offset] != 0) |
||
654 | || (bsbuf[lame_offset+1] != 0) |
||
655 | || (bsbuf[lame_offset+2] != 0) |
||
656 | || (bsbuf[lame_offset+3] != 0) |
||
657 | ) |
||
658 | { |
||
659 | debug("Wow! Is there _really_ a non-zero peak value? Now is it stored as float or int - how should I know?"); |
||
660 | peak = *(float*) (bsbuf+lame_offset); |
||
661 | } |
||
662 | debug1("Info: peak = %f (I won't use this)", peak); |
||
663 | peak = 0; /* until better times arrived */ |
||
664 | lame_offset += 4; |
||
665 | /* |
||
666 | ReplayGain values - lame only writes radio mode gain... |
||
667 | 16bit gain, 3 bits name, 3 bits originator, sign (1=-, 0=+), dB value*10 in 9 bits (fixed point) |
||
668 | ignore the setting if name or originator == 000! |
||
669 | radio 0 0 1 0 1 1 1 0 0 1 1 1 1 1 0 1 |
||
670 | audiophile 0 1 0 0 1 0 0 0 0 0 0 1 0 1 0 0 |
||
671 | */ |
||
672 | |||
673 | for(i =0; i < 2; ++i) |
||
674 | { |
||
675 | unsigned char origin = (bsbuf[lame_offset] >> 2) & 0x7; /* the 3 bits after that... */ |
||
676 | if(origin != 0) |
||
677 | { |
||
678 | unsigned char gt = bsbuf[lame_offset] >> 5; /* only first 3 bits */ |
||
679 | if(gt == 1) gt = 0; /* radio */ |
||
680 | else if(gt == 2) gt = 1; /* audiophile */ |
||
681 | else continue; |
||
682 | /* get the 9 bits into a number, divide by 10, multiply sign... happy bit banging */ |
||
683 | replay_gain[0] = ((bsbuf[lame_offset] & 0x2) ? -0.1 : 0.1) * (make_short(bsbuf, lame_offset) & 0x1f); |
||
684 | } |
||
685 | lame_offset += 2; |
||
686 | } |
||
687 | debug1("Info: Radio Gain = %03.1fdB", replay_gain[0]); |
||
688 | debug1("Info: Audiophile Gain = %03.1fdB", replay_gain[1]); |
||
689 | for(i=0; i < 2; ++i) |
||
690 | { |
||
691 | if(rva_level[i] <= 0) |
||
692 | { |
||
693 | rva_peak[i] = 0; /* at some time the parsed peak should be used */ |
||
694 | rva_gain[i] = replay_gain[i]; |
||
695 | rva_level[i] = 0; |
||
696 | } |
||
697 | } |
||
698 | lame_offset += 1; /* skipping encoding flags byte */ |
||
699 | if(vbr == ABR) |
||
700 | { |
||
701 | abr_rate = bsbuf[lame_offset]; |
||
702 | debug1("Info: ABR rate = %u", abr_rate); |
||
703 | } |
||
704 | lame_offset += 1; |
||
705 | /* encoder delay and padding, two 12 bit values... lame does write them from int ...*/ |
||
706 | #ifdef GAPLESS |
||
707 | if(param.gapless) |
||
708 | { |
||
709 | /* |
||
710 | Temporary hack that doesn't work with seeking and also is not waterproof but works most of the time; |
||
711 | in future the lame delay/padding and frame number info should be passed to layer3.c and the junk samples avoided at the source. |
||
712 | */ |
||
713 | unsigned long length = track_frames * spf(fr); |
||
714 | unsigned long skipbegin = DECODER_DELAY + ((((int) bsbuf[lame_offset]) << 4) | (((int) bsbuf[lame_offset+1]) >> 4)); |
||
715 | unsigned long skipend = -DECODER_DELAY + (((((int) bsbuf[lame_offset+1]) << 8) | (((int) bsbuf[lame_offset+2]))) & 0xfff); |
||
716 | debug3("preparing gapless mode for layer3: length %lu, skipbegin %lu, skipend %lu", length, skipbegin, skipend); |
||
717 | if(length > 1) |
||
718 | layer3_gapless_init(skipbegin+GAP_SHIFT, (skipend < length) ? length-skipend+GAP_SHIFT : length+GAP_SHIFT); |
||
719 | } |
||
720 | #endif |
||
721 | } |
||
722 | /* switch buffer back ... */ |
||
723 | bsbuf = bsspace[bsnum]+512; |
||
724 | bsnum = (bsnum + 1) & 1; |
||
725 | goto read_again; |
||
726 | } |
||
727 | } |
||
728 | } |
||
729 | } /* end block for Xing/Lame/Info tag */ |
||
730 | firsthead = newhead; /* _now_ it's time to store it... the first real header */ |
||
731 | debug1("firsthead: %08lx", firsthead); |
||
732 | /* now adjust volume */ |
||
733 | do_rva(); |
||
734 | /* and print id3 info */ |
||
735 | if(!param.quiet) print_id3_tag(rd->flags & READER_ID3TAG ? rd->id3buf : NULL); |
||
736 | } |
||
737 | #endif |
||
738 | |||
739 | bsi.bitindex = 0; |
||
740 | bsi.wordpointer = (unsigned char *) bsbuf; |
||
741 | set_synth_functions(fr); |
||
742 | if (fr->error_protection) |
||
743 | getbits(16); |
||
744 | return 1; |
||
745 | } |
||
746 | |||
747 | |||
748 | #if 0 |
||
749 | |||
750 | static int stream_back_bytes(struct reader *rds, off_t bytes) |
||
751 | { |
||
752 | if(stream_lseek(rds,-bytes,SEEK_CUR) < 0) |
||
753 | return -1; |
||
754 | /* you sure you want the buffer to resync here? */ |
||
755 | if(param.usebuffer) |
||
756 | buffer_resync(); |
||
757 | return 0; |
||
758 | } |
||
759 | |||
760 | |||
761 | /* this function strangely is define to seek num frames _back_ (and is called with -offset - duh!) */ |
||
762 | /* also... let that int be a long in future! */ |
||
763 | static int stream_back_frame(struct reader *rds,struct frame *fr,long num) |
||
764 | { |
||
765 | if(rds->flags & READER_SEEKABLE) |
||
766 | { |
||
767 | unsigned long newframe, preframe; |
||
768 | if(num > 0) /* back! */ |
||
769 | { |
||
770 | if(num > fr->num) newframe = 0; |
||
771 | else newframe = fr->num-num; |
||
772 | } |
||
773 | else newframe = fr->num-num; |
||
774 | |||
775 | /* two leading frames? hm, doesn't seem to be really needed... */ |
||
776 | /*if(newframe > 1) newframe -= 2; |
||
777 | else newframe = 0;*/ |
||
778 | |||
779 | /* now seek to nearest leading index position and read from there until newframe is reached */ |
||
780 | if(stream_lseek(rds,frame_index_find(newframe, &preframe),SEEK_SET) < 0) |
||
781 | return -1; |
||
782 | |||
783 | debug2("going to %lu; just got %lu", newframe, preframe); |
||
784 | |||
785 | fr->num = preframe; |
||
786 | |||
787 | while(fr->num < newframe) |
||
788 | { |
||
789 | /* try to be non-fatal now... frameNum only gets advanced on success anyway */ |
||
790 | if(!read_frame(fr)) break; |
||
791 | } |
||
792 | |||
793 | /* this is not needed at last? */ |
||
794 | /*read_frame(fr); |
||
795 | read_frame(fr);*/ |
||
796 | |||
797 | if(fr->lay == 3) { |
||
798 | set_pointer(512); |
||
799 | } |
||
800 | |||
801 | debug1("arrived at %lu", fr->num); |
||
802 | |||
803 | if(param.usebuffer) |
||
804 | buffer_resync(); |
||
805 | |||
806 | return 0; |
||
807 | |||
808 | } |
||
809 | else return -1; /* invalid, no seek happened */ |
||
810 | } |
||
811 | |||
812 | static int stream_head_read(struct reader *rds,unsigned long *newhead) |
||
813 | { |
||
814 | unsigned char hbuf[4]; |
||
815 | |||
816 | if(fullread(rds,hbuf,4) != 4) |
||
817 | return FALSE; |
||
576 | serge | 818 | |
646 | serge | 819 | *newhead = ((unsigned long) hbuf[0] << 24) | |
820 | ((unsigned long) hbuf[1] << 16) | |
||
821 | ((unsigned long) hbuf[2] << 8) | |
||
822 | (unsigned long) hbuf[3]; |
||
823 | |||
824 | return TRUE; |
||
825 | } |
||
826 | |||
827 | static int stream_head_shift(struct reader *rds,unsigned long *head) |
||
828 | { |
||
829 | unsigned char hbuf; |
||
830 | |||
831 | if(fullread(rds,&hbuf,1) != 1) |
||
832 | return 0; |
||
833 | *head <<= 8; |
||
834 | *head |= hbuf; |
||
835 | *head &= 0xffffffff; |
||
836 | return 1; |
||
837 | } |
||
838 | |||
839 | static off_t stream_skip_bytes(struct reader *rds,off_t len) |
||
840 | { |
||
841 | if (rds->filelen >= 0) { |
||
842 | off_t ret = stream_lseek(rds, len, SEEK_CUR); |
||
843 | if (param.usebuffer) |
||
844 | buffer_resync(); |
||
845 | return ret; |
||
846 | } else if (len >= 0) { |
||
847 | unsigned char buf[1024]; /* ThOr: Compaq cxx complained and it makes sense to me... or should one do a cast? What for? */ |
||
848 | off_t ret; |
||
849 | while (len > 0) { |
||
850 | off_t num = len < sizeof(buf) ? len : sizeof(buf); |
||
851 | ret = fullread(rds, buf, num); |
||
852 | if (ret < 0) |
||
853 | return ret; |
||
854 | len -= ret; |
||
855 | } |
||
856 | return rds->filepos; |
||
857 | } else |
||
858 | return -1; |
||
859 | } |
||
860 | |||
861 | static int stream_read_frame_body(struct reader *rds,unsigned char *buf, |
||
862 | int size) |
||
863 | { |
||
864 | long l; |
||
865 | |||
866 | if( (l=fullread(rds,buf,size)) != size) |
||
867 | { |
||
868 | if(l <= 0) |
||
869 | return 0; |
||
870 | memset(buf+l,0,size-l); |
||
871 | } |
||
872 | |||
873 | return 1; |
||
874 | } |
||
875 | |||
876 | static off_t stream_tell(struct reader *rds) |
||
877 | { |
||
878 | return rds->filepos; |
||
879 | } |
||
880 | |||
881 | static void stream_rewind(struct reader *rds) |
||
882 | { |
||
883 | stream_lseek(rds,0,SEEK_SET); |
||
884 | if(param.usebuffer) |
||
885 | buffer_resync(); |
||
886 | } |
||
887 | |||
888 | static off_t get_fileinfo(struct reader *rds,char *buf) |
||
889 | { |
||
890 | off_t len; |
||
891 | |||
892 | if((len=lseek(rds->filept,0,SEEK_END)) < 0) { |
||
893 | return -1; |
||
894 | } |
||
895 | if(lseek(rds->filept,-128,SEEK_END) < 0) |
||
896 | return -1; |
||
897 | if(fullread(rds,(unsigned char *)buf,128) != 128) { |
||
898 | return -1; |
||
899 | } |
||
900 | if(!strncmp(buf,"TAG",3)) { |
||
901 | len -= 128; |
||
902 | } |
||
903 | if(lseek(rds->filept,0,SEEK_SET) < 0) |
||
904 | return -1; |
||
905 | if(len <= 0) |
||
906 | return -1; |
||
907 | return len; |
||
908 | } |
||
909 | |||
910 | #endif |
||
911 | |||
912 | #define syncsafe_to_long(buf,res) \ |
||
913 | ( \ |
||
914 | (((buf)[0]|(buf)[1]|(buf)[2]|(buf)[3]) & 0x80) ? 0 : \ |
||
915 | (res = (((unsigned long) (buf)[0]) << 27) \ |
||
916 | | (((unsigned long) (buf)[1]) << 14) \ |
||
917 | | (((unsigned long) (buf)[2]) << 7) \ |
||
918 | | ((unsigned long) (buf)[3]) \ |
||
919 | ,1) \ |
||
920 | ) |
||
921 | |||
576 | serge | 922 | int parse_new_id3(struct reader *rd, unsigned long header) |
923 | { |
||
646 | serge | 924 | #define UNSYNC_FLAG 128 |
925 | #define EXTHEAD_FLAG 64 |
||
926 | #define EXP_FLAG 32 |
||
927 | #define FOOTER_FLAG 16 |
||
928 | #define UNKNOWN_FLAGS 15 /* 00001111*/ |
||
929 | unsigned char buf[6]; |
||
930 | unsigned long length=0; |
||
931 | unsigned char flags = 0; |
||
932 | int ret = 1; |
||
933 | unsigned char* tagdata = NULL; |
||
934 | unsigned char major = header & 0xff; |
||
576 | serge | 935 | |
646 | serge | 936 | if(major == 0xff) return -1; |
937 | |||
938 | if (rd->read_frame_body(rd,buf,6)<6) |
||
939 | return 0; |
||
940 | if(buf[0] == 0xff) /* major version, will never be 0xff */ |
||
941 | return -1; |
||
942 | |||
943 | /* second new byte are some nice flags, if these are invalid skip the whole thing */ |
||
944 | flags = buf[1]; |
||
945 | /* use 4 bytes from buf to construct 28bit uint value and return 1; return 0 if bytes are not syncsafe */ |
||
946 | |||
947 | if(!syncsafe_to_long(buf+2,length)) |
||
948 | return -1; |
||
949 | |||
950 | |||
951 | rd->strpos+=length; |
||
952 | rd->stream+=length; |
||
953 | rd->strremain-=length; |
||
954 | |||
955 | #if 0 |
||
956 | /* skip if unknown version/scary flags, parse otherwise */ |
||
957 | if((flags & UNKNOWN_FLAGS) || (major > 4)) |
||
958 | { |
||
959 | /* going to skip because there are unknown flags set */ |
||
960 | if(!rds->skip_bytes(rds,length)) /* will not store data in backbuff! */ |
||
961 | |||
962 | rd->strpos+=length; |
||
963 | rd->stream+=length; |
||
964 | rd->strremain-=length; |
||
965 | ret = 0; |
||
966 | }; |
||
967 | #endif |
||
576 | serge | 968 | return length; |
969 | };6) |