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