4f8329e7116879e8d7a945f4bf6635b6e5bc86e8
[reactos.git] / reactos / sdk / lib / 3rdparty / libmpg123 / libmpg123.c
1 /*
2 libmpg123: MPEG Audio Decoder library
3
4 copyright 1995-2014 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
7 */
8
9 #include "mpg123lib_intern.h"
10 #include "icy2utf8.h"
11 #include "debug.h"
12
13 #include "gapless.h"
14
15 #define SEEKFRAME(mh) ((mh)->ignoreframe < 0 ? 0 : (mh)->ignoreframe)
16
17 static int initialized = 0;
18
19 int attribute_align_arg mpg123_init(void)
20 {
21 if((sizeof(short) != 2) || (sizeof(long) < 4)) return MPG123_BAD_TYPES;
22
23 if(initialized) return MPG123_OK; /* no need to initialize twice */
24
25 #ifndef NO_LAYER12
26 init_layer12(); /* inits also shared tables with layer1 */
27 #endif
28 #ifndef NO_LAYER3
29 init_layer3();
30 #endif
31 prepare_decode_tables();
32 check_decoders();
33 initialized = 1;
34 return MPG123_OK;
35 }
36
37 void attribute_align_arg mpg123_exit(void)
38 {
39 /* nothing yet, but something later perhaps */
40 }
41
42 /* create a new handle with specified decoder, decoder can be "", "auto" or NULL for auto-detection */
43 mpg123_handle attribute_align_arg *mpg123_new(const char* decoder, int *error)
44 {
45 return mpg123_parnew(NULL, decoder, error);
46 }
47
48 /* ...the full routine with optional initial parameters to override defaults. */
49 mpg123_handle attribute_align_arg *mpg123_parnew(mpg123_pars *mp, const char* decoder, int *error)
50 {
51 mpg123_handle *fr = NULL;
52 int err = MPG123_OK;
53
54 if(initialized) fr = (mpg123_handle*) malloc(sizeof(mpg123_handle));
55 else err = MPG123_NOT_INITIALIZED;
56 if(fr != NULL)
57 {
58 frame_init_par(fr, mp);
59 debug("cpu opt setting");
60 if(frame_cpu_opt(fr, decoder) != 1)
61 {
62 err = MPG123_BAD_DECODER;
63 frame_exit(fr);
64 free(fr);
65 fr = NULL;
66 }
67 }
68 if(fr != NULL)
69 {
70 fr->decoder_change = 1;
71 }
72 else if(err == MPG123_OK) err = MPG123_OUT_OF_MEM;
73
74 if(error != NULL) *error = err;
75 return fr;
76 }
77
78 int attribute_align_arg mpg123_decoder(mpg123_handle *mh, const char* decoder)
79 {
80 enum optdec dt = dectype(decoder);
81
82 if(mh == NULL) return MPG123_BAD_HANDLE;
83
84 if(dt == nodec)
85 {
86 mh->err = MPG123_BAD_DECODER;
87 return MPG123_ERR;
88 }
89 if(dt == mh->cpu_opts.type) return MPG123_OK;
90
91 /* Now really change. */
92 /* frame_exit(mh);
93 frame_init(mh); */
94 debug("cpu opt setting");
95 if(frame_cpu_opt(mh, decoder) != 1)
96 {
97 mh->err = MPG123_BAD_DECODER;
98 frame_exit(mh);
99 return MPG123_ERR;
100 }
101 /* New buffers for decoder are created in frame_buffers() */
102 if((frame_outbuffer(mh) != 0))
103 {
104 mh->err = MPG123_NO_BUFFERS;
105 frame_exit(mh);
106 return MPG123_ERR;
107 }
108 /* Do _not_ call decode_update here! That is only allowed after a first MPEG frame has been met. */
109 mh->decoder_change = 1;
110 return MPG123_OK;
111 }
112
113 int attribute_align_arg mpg123_param(mpg123_handle *mh, enum mpg123_parms key, long val, double fval)
114 {
115 int r;
116
117 if(mh == NULL) return MPG123_BAD_HANDLE;
118 r = mpg123_par(&mh->p, key, val, fval);
119 if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
120 else
121 { /* Special treatment for some settings. */
122 #ifdef FRAME_INDEX
123 if(key == MPG123_INDEX_SIZE)
124 { /* Apply frame index size and grow property on the fly. */
125 r = frame_index_setup(mh);
126 if(r != MPG123_OK) mh->err = MPG123_INDEX_FAIL;
127 }
128 #endif
129 #ifndef NO_FEEDER
130 /* Feeder pool size is applied right away, reader will react to that. */
131 if(key == MPG123_FEEDPOOL || key == MPG123_FEEDBUFFER)
132 bc_poolsize(&mh->rdat.buffer, mh->p.feedpool, mh->p.feedbuffer);
133 #endif
134 }
135 return r;
136 }
137
138 int attribute_align_arg mpg123_par(mpg123_pars *mp, enum mpg123_parms key, long val, double fval)
139 {
140 int ret = MPG123_OK;
141
142 if(mp == NULL) return MPG123_BAD_PARS;
143 switch(key)
144 {
145 case MPG123_VERBOSE:
146 mp->verbose = val;
147 break;
148 case MPG123_FLAGS:
149 #ifndef GAPLESS
150 if(val & MPG123_GAPLESS) ret = MPG123_NO_GAPLESS;
151 #endif
152 if(ret == MPG123_OK) mp->flags = val;
153 debug1("set flags to 0x%lx", (unsigned long) mp->flags);
154 break;
155 case MPG123_ADD_FLAGS:
156 #ifndef GAPLESS
157 /* Enabling of gapless mode doesn't work when it's not there, but disabling (below) is no problem. */
158 if(val & MPG123_GAPLESS) ret = MPG123_NO_GAPLESS;
159 else
160 #endif
161 mp->flags |= val;
162 debug1("set flags to 0x%lx", (unsigned long) mp->flags);
163 break;
164 case MPG123_REMOVE_FLAGS:
165 mp->flags &= ~val;
166 debug1("set flags to 0x%lx", (unsigned long) mp->flags);
167 break;
168 case MPG123_FORCE_RATE: /* should this trigger something? */
169 #ifdef NO_NTOM
170 if(val > 0)
171 ret = MPG123_BAD_RATE;
172 #else
173 if(val > 96000) ret = MPG123_BAD_RATE;
174 else mp->force_rate = val < 0 ? 0 : val; /* >0 means enable, 0 disable */
175 #endif
176 break;
177 case MPG123_DOWN_SAMPLE:
178 #ifdef NO_DOWNSAMPLE
179 if(val != 0) ret = MPG123_BAD_RATE;
180 #else
181 if(val < 0 || val > 2) ret = MPG123_BAD_RATE;
182 else mp->down_sample = (int)val;
183 #endif
184 break;
185 case MPG123_RVA:
186 if(val < 0 || val > MPG123_RVA_MAX) ret = MPG123_BAD_RVA;
187 else mp->rva = (int)val;
188 break;
189 case MPG123_DOWNSPEED:
190 mp->halfspeed = val < 0 ? 0 : val;
191 break;
192 case MPG123_UPSPEED:
193 mp->doublespeed = val < 0 ? 0 : val;
194 break;
195 case MPG123_ICY_INTERVAL:
196 #ifndef NO_ICY
197 mp->icy_interval = val > 0 ? val : 0;
198 #else
199 if(val > 0) ret = MPG123_BAD_PARAM;
200 #endif
201 break;
202 case MPG123_OUTSCALE:
203 /* Choose the value that is non-zero, if any.
204 Downscaling integers to 1.0 . */
205 mp->outscale = val == 0 ? fval : (double)val/SHORT_SCALE;
206 break;
207 case MPG123_TIMEOUT:
208 #ifdef TIMEOUT_READ
209 mp->timeout = val >= 0 ? val : 0;
210 #else
211 if(val > 0) ret = MPG123_NO_TIMEOUT;
212 #endif
213 break;
214 case MPG123_RESYNC_LIMIT:
215 mp->resync_limit = val;
216 break;
217 case MPG123_INDEX_SIZE:
218 #ifdef FRAME_INDEX
219 mp->index_size = val;
220 #else
221 ret = MPG123_NO_INDEX;
222 #endif
223 break;
224 case MPG123_PREFRAMES:
225 if(val >= 0) mp->preframes = val;
226 else ret = MPG123_BAD_VALUE;
227 break;
228 case MPG123_FEEDPOOL:
229 #ifndef NO_FEEDER
230 if(val >= 0) mp->feedpool = val;
231 else ret = MPG123_BAD_VALUE;
232 #else
233 ret = MPG123_MISSING_FEATURE;
234 #endif
235 break;
236 case MPG123_FEEDBUFFER:
237 #ifndef NO_FEEDER
238 if(val > 0) mp->feedbuffer = val;
239 else ret = MPG123_BAD_VALUE;
240 #else
241 ret = MPG123_MISSING_FEATURE;
242 #endif
243 break;
244 default:
245 ret = MPG123_BAD_PARAM;
246 }
247 return ret;
248 }
249
250 int attribute_align_arg mpg123_getparam(mpg123_handle *mh, enum mpg123_parms key, long *val, double *fval)
251 {
252 int r;
253
254 if(mh == NULL) return MPG123_BAD_HANDLE;
255 r = mpg123_getpar(&mh->p, key, val, fval);
256 if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
257 return r;
258 }
259
260 int attribute_align_arg mpg123_getpar(mpg123_pars *mp, enum mpg123_parms key, long *val, double *fval)
261 {
262 int ret = 0;
263
264 if(mp == NULL) return MPG123_BAD_PARS;
265 switch(key)
266 {
267 case MPG123_VERBOSE:
268 if(val) *val = mp->verbose;
269 break;
270 case MPG123_FLAGS:
271 case MPG123_ADD_FLAGS:
272 if(val) *val = mp->flags;
273 break;
274 case MPG123_FORCE_RATE:
275 if(val)
276 #ifdef NO_NTOM
277 *val = 0;
278 #else
279 *val = mp->force_rate;
280 #endif
281 break;
282 case MPG123_DOWN_SAMPLE:
283 if(val) *val = mp->down_sample;
284 break;
285 case MPG123_RVA:
286 if(val) *val = mp->rva;
287 break;
288 case MPG123_DOWNSPEED:
289 if(val) *val = mp->halfspeed;
290 break;
291 case MPG123_UPSPEED:
292 if(val) *val = mp->doublespeed;
293 break;
294 case MPG123_ICY_INTERVAL:
295 #ifndef NO_ICY
296 if(val) *val = (long)mp->icy_interval;
297 #else
298 if(val) *val = 0;
299 #endif
300 break;
301 case MPG123_OUTSCALE:
302 if(fval) *fval = mp->outscale;
303 if(val) *val = (long)(mp->outscale*SHORT_SCALE);
304 break;
305 case MPG123_RESYNC_LIMIT:
306 if(val) *val = mp->resync_limit;
307 break;
308 case MPG123_INDEX_SIZE:
309 if(val)
310 #ifdef FRAME_INDEX
311 *val = mp->index_size;
312 #else
313 *val = 0; /* graceful fallback: no index is index of zero size */
314 #endif
315 break;
316 case MPG123_PREFRAMES:
317 *val = mp->preframes;
318 break;
319 case MPG123_FEEDPOOL:
320 #ifndef NO_FEEDER
321 *val = mp->feedpool;
322 #else
323 ret = MPG123_MISSING_FEATURE;
324 #endif
325 break;
326 case MPG123_FEEDBUFFER:
327 #ifndef NO_FEEDER
328 *val = mp->feedbuffer;
329 #else
330 ret = MPG123_MISSING_FEATURE;
331 #endif
332 break;
333 default:
334 ret = MPG123_BAD_PARAM;
335 }
336 return ret;
337 }
338
339 int attribute_align_arg mpg123_getstate(mpg123_handle *mh, enum mpg123_state key, long *val, double *fval)
340 {
341 int ret = MPG123_OK;
342 long theval = 0;
343 double thefval = 0.;
344
345 if(mh == NULL) return MPG123_BAD_HANDLE;
346
347 switch(key)
348 {
349 case MPG123_ACCURATE:
350 theval = mh->state_flags & FRAME_ACCURATE;
351 break;
352 case MPG123_FRANKENSTEIN:
353 theval = mh->state_flags & FRAME_FRANKENSTEIN;
354 break;
355 case MPG123_BUFFERFILL:
356 #ifndef NO_FEEDER
357 {
358 size_t sval = bc_fill(&mh->rdat.buffer);
359 theval = (long)sval;
360 if((size_t)theval != sval)
361 {
362 mh->err = MPG123_INT_OVERFLOW;
363 ret = MPG123_ERR;
364 }
365 }
366 #else
367 mh->err = MPG123_MISSING_FEATURE;
368 ret = MPG123_ERR;
369 #endif
370 break;
371 case MPG123_FRESH_DECODER:
372 theval = mh->state_flags & FRAME_FRESH_DECODER;
373 mh->state_flags &= ~FRAME_FRESH_DECODER;
374 break;
375 default:
376 mh->err = MPG123_BAD_KEY;
377 ret = MPG123_ERR;
378 }
379
380 if(val != NULL) *val = theval;
381 if(fval != NULL) *fval = thefval;
382
383 return ret;
384 }
385 int attribute_align_arg mpg123_eq(mpg123_handle *mh, enum mpg123_channels channel, int band, double val)
386 {
387 #ifndef NO_EQUALIZER
388 if(mh == NULL) return MPG123_BAD_HANDLE;
389 if(band < 0 || band > 31){ mh->err = MPG123_BAD_BAND; return MPG123_ERR; }
390 switch(channel)
391 {
392 case MPG123_LEFT|MPG123_RIGHT:
393 mh->equalizer[0][band] = mh->equalizer[1][band] = DOUBLE_TO_REAL(val);
394 break;
395 case MPG123_LEFT: mh->equalizer[0][band] = DOUBLE_TO_REAL(val); break;
396 case MPG123_RIGHT: mh->equalizer[1][band] = DOUBLE_TO_REAL(val); break;
397 default:
398 mh->err=MPG123_BAD_CHANNEL;
399 return MPG123_ERR;
400 }
401 mh->have_eq_settings = TRUE;
402 #endif
403 return MPG123_OK;
404 }
405
406 double attribute_align_arg mpg123_geteq(mpg123_handle *mh, enum mpg123_channels channel, int band)
407 {
408 double ret = 0.;
409 #ifndef NO_EQUALIZER
410
411 /* Handle this gracefully. When there is no band, it has no volume. */
412 if(mh != NULL && band > -1 && band < 32)
413 switch(channel)
414 {
415 case MPG123_LEFT|MPG123_RIGHT:
416 ret = 0.5*(REAL_TO_DOUBLE(mh->equalizer[0][band])+REAL_TO_DOUBLE(mh->equalizer[1][band]));
417 break;
418 case MPG123_LEFT: ret = REAL_TO_DOUBLE(mh->equalizer[0][band]); break;
419 case MPG123_RIGHT: ret = REAL_TO_DOUBLE(mh->equalizer[1][band]); break;
420 /* Default case is already handled: ret = 0 */
421 }
422 #endif
423 return ret;
424 }
425
426 /* plain file access, no http! */
427 int attribute_align_arg mpg123_open(mpg123_handle *mh, const char *path)
428 {
429 if(mh == NULL) return MPG123_BAD_HANDLE;
430
431 mpg123_close(mh);
432 return open_stream(mh, path, -1);
433 }
434
435 int attribute_align_arg mpg123_open_fd(mpg123_handle *mh, int fd)
436 {
437 if(mh == NULL) return MPG123_BAD_HANDLE;
438
439 mpg123_close(mh);
440 return open_stream(mh, NULL, fd);
441 }
442
443 int attribute_align_arg mpg123_open_handle(mpg123_handle *mh, void *iohandle)
444 {
445 if(mh == NULL) return MPG123_BAD_HANDLE;
446
447 mpg123_close(mh);
448 if(mh->rdat.r_read_handle == NULL)
449 {
450 mh->err = MPG123_BAD_CUSTOM_IO;
451 return MPG123_ERR;
452 }
453 return open_stream_handle(mh, iohandle);
454 }
455
456 int attribute_align_arg mpg123_open_feed(mpg123_handle *mh)
457 {
458 if(mh == NULL) return MPG123_BAD_HANDLE;
459
460 mpg123_close(mh);
461 return open_feed(mh);
462 }
463
464 int attribute_align_arg mpg123_replace_reader( mpg123_handle *mh,
465 ssize_t (*r_read) (int, void *, size_t),
466 off_t (*r_lseek)(int, off_t, int) )
467 {
468 if(mh == NULL) return MPG123_BAD_HANDLE;
469
470 mpg123_close(mh);
471 mh->rdat.r_read = r_read;
472 mh->rdat.r_lseek = r_lseek;
473 return MPG123_OK;
474 }
475
476 int attribute_align_arg mpg123_replace_reader_handle( mpg123_handle *mh,
477 ssize_t (*r_read) (void*, void *, size_t),
478 off_t (*r_lseek)(void*, off_t, int),
479 void (*cleanup)(void*) )
480 {
481 if(mh == NULL) return MPG123_BAD_HANDLE;
482
483 mpg123_close(mh);
484 mh->rdat.r_read_handle = r_read;
485 mh->rdat.r_lseek_handle = r_lseek;
486 mh->rdat.cleanup_handle = cleanup;
487 return MPG123_OK;
488 }
489
490 /* Update decoding engine for
491 a) a new choice of decoder
492 b) a changed native format of the MPEG stream
493 ... calls are only valid after parsing some MPEG frame! */
494 int decode_update(mpg123_handle *mh)
495 {
496 long native_rate;
497 int b;
498
499 if(mh->num < 0)
500 {
501 if(!(mh->p.flags & MPG123_QUIET)) error("decode_update() has been called before reading the first MPEG frame! Internal programming error.");
502
503 mh->err = MPG123_BAD_DECODER_SETUP;
504 return MPG123_ERR;
505 }
506
507 mh->state_flags |= FRAME_FRESH_DECODER;
508 native_rate = frame_freq(mh);
509
510 b = frame_output_format(mh); /* Select the new output format based on given constraints. */
511 if(b < 0) return MPG123_ERR;
512
513 if(b == 1) mh->new_format = 1; /* Store for later... */
514
515 debug3("updating decoder structure with native rate %li and af.rate %li (new format: %i)", native_rate, mh->af.rate, mh->new_format);
516 if(mh->af.rate == native_rate) mh->down_sample = 0;
517 else if(mh->af.rate == native_rate>>1) mh->down_sample = 1;
518 else if(mh->af.rate == native_rate>>2) mh->down_sample = 2;
519 else mh->down_sample = 3; /* flexible (fixed) rate */
520 switch(mh->down_sample)
521 {
522 case 0:
523 case 1:
524 case 2:
525 mh->down_sample_sblimit = SBLIMIT>>(mh->down_sample);
526 /* With downsampling I get less samples per frame */
527 mh->outblock = outblock_bytes(mh, (mh->spf>>mh->down_sample));
528 break;
529 #ifndef NO_NTOM
530 case 3:
531 {
532 if(synth_ntom_set_step(mh) != 0) return -1;
533 if(frame_freq(mh) > mh->af.rate)
534 {
535 mh->down_sample_sblimit = SBLIMIT * mh->af.rate;
536 mh->down_sample_sblimit /= frame_freq(mh);
537 }
538 else mh->down_sample_sblimit = SBLIMIT;
539 mh->outblock = outblock_bytes(mh,
540 ( ( NTOM_MUL-1+mh->spf
541 * (((size_t)NTOM_MUL*mh->af.rate)/frame_freq(mh))
542 )/NTOM_MUL ));
543 }
544 break;
545 #endif
546 }
547
548 if(!(mh->p.flags & MPG123_FORCE_MONO))
549 {
550 if(mh->af.channels == 1) mh->single = SINGLE_MIX;
551 else mh->single = SINGLE_STEREO;
552 }
553 else mh->single = (mh->p.flags & MPG123_FORCE_MONO)-1;
554 if(set_synth_functions(mh) != 0) return -1;;
555
556 /* The needed size of output buffer may have changed. */
557 if(frame_outbuffer(mh) != MPG123_OK) return -1;
558
559 do_rva(mh);
560 debug3("done updating decoder structure with native rate %li and af.rate %li and down_sample %i", frame_freq(mh), mh->af.rate, mh->down_sample);
561
562 return 0;
563 }
564
565 size_t attribute_align_arg mpg123_safe_buffer(void)
566 {
567 /* real is the largest possible output (it's 32bit float, 32bit int or 64bit double). */
568 return sizeof(real)*2*1152*NTOM_MAX;
569 }
570
571 size_t attribute_align_arg mpg123_outblock(mpg123_handle *mh)
572 {
573 /* Try to be helpful and never return zero output block size. */
574 if(mh != NULL && mh->outblock > 0) return mh->outblock;
575 else return mpg123_safe_buffer();
576 }
577
578 /* Read in the next frame we actually want for decoding.
579 This includes skipping/ignoring frames, in additon to skipping junk in the parser. */
580 static int get_next_frame(mpg123_handle *mh)
581 {
582 int change = mh->decoder_change;
583 /* Ensure we got proper decoder for ignoring frames.
584 Header can be changed from seeking around. But be careful: Only after at
585 least one frame got read, decoder update makes sense. */
586 if(mh->header_change > 1 && mh->num >= 0)
587 {
588 change = 1;
589 mh->header_change = 0;
590 debug("starting with big header change");
591 if(decode_update(mh) < 0)
592 return MPG123_ERR;
593 }
594
595 do
596 {
597 int b;
598 /* Decode & discard some frame(s) before beginning. */
599 if(mh->to_ignore && mh->num < mh->firstframe && mh->num >= mh->ignoreframe)
600 {
601 debug1("ignoring frame %li", (long)mh->num);
602 /* Decoder structure must be current! decode_update has been called before... */
603 (mh->do_layer)(mh); mh->buffer.fill = 0;
604 #ifndef NO_NTOM
605 /* The ignored decoding may have failed. Make sure ntom stays consistent. */
606 if(mh->down_sample == 3) ntom_set_ntom(mh, mh->num+1);
607 #endif
608 mh->to_ignore = mh->to_decode = FALSE;
609 }
610 /* Read new frame data; possibly breaking out here for MPG123_NEED_MORE. */
611 debug("read frame");
612 mh->to_decode = FALSE;
613 b = read_frame(mh); /* That sets to_decode only if a full frame was read. */
614 debug4("read of frame %li returned %i (to_decode=%i) at sample %li", (long)mh->num, b, mh->to_decode, (long)mpg123_tell(mh));
615 if(b == MPG123_NEED_MORE) return MPG123_NEED_MORE; /* need another call with data */
616 else if(b <= 0)
617 {
618 /* More sophisticated error control? */
619 if(b==0 || (mh->rdat.filelen >= 0 && mh->rdat.filepos == mh->rdat.filelen))
620 { /* We simply reached the end. */
621 mh->track_frames = mh->num + 1;
622 debug("What about updating/checking gapless sample count here?");
623 return MPG123_DONE;
624 }
625 else return MPG123_ERR; /* Some real error. */
626 }
627 /* Now, there should be new data to decode ... and also possibly new stream properties */
628 if(mh->header_change > 1)
629 {
630 debug("big header change");
631 change = 1;
632 mh->header_change = 0;
633 /* Need to update decoder structure right away since frame might need to
634 be decoded on next loop iteration for properly ignoring its output. */
635 if(decode_update(mh) < 0)
636 return MPG123_ERR;
637 }
638 /* Now some accounting: Look at the numbers and decide if we want this frame. */
639 ++mh->playnum;
640 /* Plain skipping without decoding, only when frame is not ignored on next cycle. */
641 if(mh->num < mh->firstframe || (mh->p.doublespeed && (mh->playnum % mh->p.doublespeed)))
642 {
643 if(!(mh->to_ignore && mh->num < mh->firstframe && mh->num >= mh->ignoreframe))
644 {
645 frame_skip(mh);
646 /* Should one fix NtoM here or not?
647 It is not work the trouble for doublespeed, but what with leading frames? */
648 }
649 }
650 /* Or, we are finally done and have a new frame. */
651 else break;
652 } while(1);
653
654 /* If we reach this point, we got a new frame ready to be decoded.
655 All other situations resulted in returns from the loop. */
656 if(change)
657 {
658 mh->decoder_change = 0;
659 if(mh->fresh)
660 {
661 #ifdef GAPLESS
662 int b=0;
663 /* Prepare offsets for gapless decoding. */
664 debug1("preparing gapless stuff with native rate %li", frame_freq(mh));
665 frame_gapless_realinit(mh);
666 frame_set_frameseek(mh, mh->num);
667 #endif
668 mh->fresh = 0;
669 #ifdef GAPLESS
670 /* Could this possibly happen? With a real big gapless offset... */
671 if(mh->num < mh->firstframe) b = get_next_frame(mh);
672 if(b < 0) return b; /* Could be error, need for more, new format... */
673 #endif
674 }
675 }
676 return MPG123_OK;
677 }
678
679 /* Assumption: A buffer full of zero samples can be constructed by repetition of this byte.
680 Oh, and it handles some format conversion.
681 Only to be used by decode_the_frame() ... */
682 static int zero_byte(mpg123_handle *fr)
683 {
684 #ifndef NO_8BIT
685 return fr->af.encoding & MPG123_ENC_8 ? fr->conv16to8[0] : 0;
686 #else
687 return 0; /* All normal signed formats have the zero here (even in byte form -- that may be an assumption for your funny machine...). */
688 #endif
689 }
690
691 /*
692 Not part of the api. This just decodes the frame and fills missing bits with zeroes.
693 There can be frames that are broken and thus make do_layer() fail.
694 */
695 static void decode_the_frame(mpg123_handle *fr)
696 {
697 size_t needed_bytes = decoder_synth_bytes(fr, frame_expect_outsamples(fr));
698 fr->clip += (fr->do_layer)(fr);
699 /*fprintf(stderr, "frame %"OFF_P": got %"SIZE_P" / %"SIZE_P"\n", fr->num,(size_p)fr->buffer.fill, (size_p)needed_bytes);*/
700 /* There could be less data than promised.
701 Also, then debugging, we look out for coding errors that could result in _more_ data than expected. */
702 #ifdef DEBUG
703 if(fr->buffer.fill != needed_bytes)
704 {
705 #endif
706 if(fr->buffer.fill < needed_bytes)
707 {
708 if(VERBOSE2)
709 fprintf(stderr, "Note: broken frame %li, filling up with %"SIZE_P" zeroes, from %"SIZE_P"\n", (long)fr->num, (size_p)(needed_bytes-fr->buffer.fill), (size_p)fr->buffer.fill);
710
711 /*
712 One could do a loop with individual samples instead... but zero is zero
713 Actually, that is wrong: zero is mostly a series of null bytes,
714 but we have funny 8bit formats that have a different opinion on zero...
715 Unsigned 16 or 32 bit formats are handled later.
716 */
717 memset( fr->buffer.data + fr->buffer.fill, zero_byte(fr), needed_bytes - fr->buffer.fill );
718
719 fr->buffer.fill = needed_bytes;
720 #ifndef NO_NTOM
721 /* ntom_val will be wrong when the decoding wasn't carried out completely */
722 ntom_set_ntom(fr, fr->num+1);
723 #endif
724 }
725 #ifdef DEBUG
726 else
727 {
728 if(NOQUIET)
729 error2("I got _more_ bytes than expected (%"SIZE_P" / %"SIZE_P"), that should not be possible!", (size_p)fr->buffer.fill, (size_p)needed_bytes);
730 }
731 }
732 #endif
733 postprocess_buffer(fr);
734 }
735
736 /*
737 Decode the current frame into the frame structure's buffer, accessible at the location stored in <audio>, with <bytes> bytes available.
738 <num> will contain the last decoded frame number. This function should be called after mpg123_framebyframe_next positioned the stream at a
739 valid mp3 frame. The buffer contents will get lost on the next call to mpg123_framebyframe_next or mpg123_framebyframe_decode.
740 returns
741 MPG123_OK -- successfully decoded or ignored the frame, you get your output data or in case of ignored frames 0 bytes
742 MPG123_DONE -- decoding finished, should not happen
743 MPG123_ERR -- some error occured.
744 MPG123_ERR_NULL -- audio or bytes are not pointing to valid storage addresses
745 MPG123_BAD_HANDLE -- mh has not been initialized
746 MPG123_NO_SPACE -- not enough space in buffer for safe decoding, should not happen
747 */
748 int attribute_align_arg mpg123_framebyframe_decode(mpg123_handle *mh, off_t *num, unsigned char **audio, size_t *bytes)
749 {
750 if(bytes == NULL) return MPG123_ERR_NULL;
751 if(audio == NULL) return MPG123_ERR_NULL;
752 if(mh == NULL) return MPG123_BAD_HANDLE;
753 if(mh->buffer.size < mh->outblock) return MPG123_NO_SPACE;
754
755 *bytes = 0;
756 mh->buffer.fill = 0; /* always start fresh */
757 if(!mh->to_decode) return MPG123_OK;
758
759 if(num != NULL) *num = mh->num;
760 debug("decoding");
761 decode_the_frame(mh);
762 mh->to_decode = mh->to_ignore = FALSE;
763 mh->buffer.p = mh->buffer.data;
764 FRAME_BUFFERCHECK(mh);
765 *audio = mh->buffer.p;
766 *bytes = mh->buffer.fill;
767 return MPG123_OK;
768 }
769
770 /*
771 Find, read and parse the next mp3 frame while skipping junk and parsing id3 tags, lame headers, etc.
772 Prepares everything for decoding using mpg123_framebyframe_decode.
773 returns
774 MPG123_OK -- new frame was read and parsed, call mpg123_framebyframe_decode to actually decode
775 MPG123_NEW_FORMAT -- new frame was read, it results in changed output format, call mpg123_framebyframe_decode to actually decode
776 MPG123_BAD_HANDLE -- mh has not been initialized
777 MPG123_NEED_MORE -- more input data is needed to advance to the next frame. supply more input data using mpg123_feed
778 */
779 int attribute_align_arg mpg123_framebyframe_next(mpg123_handle *mh)
780 {
781 int b;
782 if(mh == NULL) return MPG123_BAD_HANDLE;
783
784 mh->to_decode = mh->to_ignore = FALSE;
785 mh->buffer.fill = 0;
786
787 b = get_next_frame(mh);
788 if(b < 0) return b;
789 debug1("got next frame, %i", mh->to_decode);
790
791 /* mpg123_framebyframe_decode will return MPG123_OK with 0 bytes decoded if mh->to_decode is 0 */
792 if(!mh->to_decode)
793 return MPG123_OK;
794
795 if(mh->new_format)
796 {
797 debug("notifiying new format");
798 mh->new_format = 0;
799 return MPG123_NEW_FORMAT;
800 }
801
802 return MPG123_OK;
803 }
804
805 /*
806 Put _one_ decoded frame into the frame structure's buffer, accessible at the location stored in <audio>, with <bytes> bytes available.
807 The buffer contents will be lost on next call to mpg123_decode_frame.
808 MPG123_OK -- successfully decoded the frame, you get your output data
809 MPg123_DONE -- This is it. End.
810 MPG123_ERR -- some error occured...
811 MPG123_NEW_FORMAT -- new frame was read, it results in changed output format -> will be decoded on next call
812 MPG123_NEED_MORE -- that should not happen as this function is intended for in-library stream reader but if you force it...
813 MPG123_NO_SPACE -- not enough space in buffer for safe decoding, also should not happen
814
815 num will be updated to the last decoded frame number (may possibly _not_ increase, p.ex. when format changed).
816 */
817 int attribute_align_arg mpg123_decode_frame(mpg123_handle *mh, off_t *num, unsigned char **audio, size_t *bytes)
818 {
819 if(bytes != NULL) *bytes = 0;
820 if(mh == NULL) return MPG123_BAD_HANDLE;
821 if(mh->buffer.size < mh->outblock) return MPG123_NO_SPACE;
822 mh->buffer.fill = 0; /* always start fresh */
823 while(TRUE)
824 {
825 /* decode if possible */
826 if(mh->to_decode)
827 {
828 if(mh->new_format)
829 {
830 debug("notifiying new format");
831 mh->new_format = 0;
832 return MPG123_NEW_FORMAT;
833 }
834 if(num != NULL) *num = mh->num;
835 debug("decoding");
836
837 decode_the_frame(mh);
838
839 mh->to_decode = mh->to_ignore = FALSE;
840 mh->buffer.p = mh->buffer.data;
841 FRAME_BUFFERCHECK(mh);
842 if(audio != NULL) *audio = mh->buffer.p;
843 if(bytes != NULL) *bytes = mh->buffer.fill;
844
845 return MPG123_OK;
846 }
847 else
848 {
849 int b = get_next_frame(mh);
850 if(b < 0) return b;
851 debug1("got next frame, %i", mh->to_decode);
852 }
853 }
854 }
855
856 int attribute_align_arg mpg123_read(mpg123_handle *mh, unsigned char *out, size_t size, size_t *done)
857 {
858 return mpg123_decode(mh, NULL, 0, out, size, done);
859 }
860
861 int attribute_align_arg mpg123_feed(mpg123_handle *mh, const unsigned char *in, size_t size)
862 {
863 if(mh == NULL) return MPG123_BAD_HANDLE;
864 #ifndef NO_FEEDER
865 if(size > 0)
866 {
867 if(in != NULL)
868 {
869 if(feed_more(mh, in, size) != 0) return MPG123_ERR;
870 else
871 {
872 /* The need for more data might have triggered an error.
873 This one is outdated now with the new data. */
874 if(mh->err == MPG123_ERR_READER) mh->err = MPG123_OK;
875
876 return MPG123_OK;
877 }
878 }
879 else
880 {
881 mh->err = MPG123_NULL_BUFFER;
882 return MPG123_ERR;
883 }
884 }
885 return MPG123_OK;
886 #else
887 mh->err = MPG123_MISSING_FEATURE;
888 return MPG123_ERR;
889 #endif
890 }
891
892 /*
893 The old picture:
894 while(1) {
895 len = read(0,buf,16384);
896 if(len <= 0)
897 break;
898 ret = decodeMP3(&mp,buf,len,out,8192,&size);
899 while(ret == MP3_OK) {
900 write(1,out,size);
901 ret = decodeMP3(&mp,NULL,0,out,8192,&size);
902 }
903 }
904 */
905
906 int attribute_align_arg mpg123_decode(mpg123_handle *mh, const unsigned char *inmemory, size_t inmemsize, unsigned char *outmemory, size_t outmemsize, size_t *done)
907 {
908 int ret = MPG123_OK;
909 size_t mdone = 0;
910
911 if(done != NULL) *done = 0;
912 if(mh == NULL) return MPG123_BAD_HANDLE;
913 #ifndef NO_FEEDER
914 if(inmemsize > 0 && mpg123_feed(mh, inmemory, inmemsize) != MPG123_OK)
915 {
916 ret = MPG123_ERR;
917 goto decodeend;
918 }
919 if(outmemory == NULL) outmemsize = 0; /* Not just give error, give chance to get a status message. */
920
921 while(ret == MPG123_OK)
922 {
923 debug4("decode loop, fill %i (%li vs. %li); to_decode: %i", (int)mh->buffer.fill, (long)mh->num, (long)mh->firstframe, mh->to_decode);
924 /* Decode a frame that has been read before.
925 This only happens when buffer is empty! */
926 if(mh->to_decode)
927 {
928 if(mh->new_format)
929 {
930 debug("notifiying new format");
931 mh->new_format = 0;
932 ret = MPG123_NEW_FORMAT;
933 goto decodeend;
934 }
935 if(mh->buffer.size - mh->buffer.fill < mh->outblock)
936 {
937 ret = MPG123_NO_SPACE;
938 goto decodeend;
939 }
940 decode_the_frame(mh);
941 mh->to_decode = mh->to_ignore = FALSE;
942 mh->buffer.p = mh->buffer.data;
943 debug2("decoded frame %li, got %li samples in buffer", (long)mh->num, (long)(mh->buffer.fill / (samples_to_bytes(mh, 1))));
944 FRAME_BUFFERCHECK(mh);
945 }
946 if(mh->buffer.fill) /* Copy (part of) the decoded data to the caller's buffer. */
947 {
948 /* get what is needed - or just what is there */
949 int a = mh->buffer.fill > (outmemsize - mdone) ? outmemsize - mdone : mh->buffer.fill;
950 debug4("buffer fill: %i; copying %i (%i - %li)", (int)mh->buffer.fill, a, (int)outmemsize, (long)mdone);
951 memcpy(outmemory, mh->buffer.p, a);
952 /* less data in frame buffer, less needed, output pointer increase, more data given... */
953 mh->buffer.fill -= a;
954 outmemory += a;
955 mdone += a;
956 mh->buffer.p += a;
957 if(!(outmemsize > mdone)) goto decodeend;
958 }
959 else /* If we didn't have data, get a new frame. */
960 {
961 int b = get_next_frame(mh);
962 if(b < 0){ ret = b; goto decodeend; }
963 }
964 }
965 decodeend:
966 if(done != NULL) *done = mdone;
967 return ret;
968 #else
969 mh->err = MPG123_MISSING_FEATURE;
970 return MPG123_ERR;
971 #endif
972 }
973
974 long attribute_align_arg mpg123_clip(mpg123_handle *mh)
975 {
976 long ret = 0;
977
978 if(mh != NULL)
979 {
980 ret = mh->clip;
981 mh->clip = 0;
982 }
983 return ret;
984 }
985
986 /* Simples: Track needs initializtion if no initial frame has been read yet. */
987 #define track_need_init(mh) ((mh)->num < 0)
988
989 static int init_track(mpg123_handle *mh)
990 {
991 if(track_need_init(mh))
992 {
993 /* Fresh track, need first frame for basic info. */
994 int b = get_next_frame(mh);
995 if(b < 0) return b;
996 }
997 return 0;
998 }
999
1000 int attribute_align_arg mpg123_info(mpg123_handle *mh, struct mpg123_frameinfo *mi)
1001 {
1002 int b;
1003
1004 if(mh == NULL) return MPG123_BAD_HANDLE;
1005 if(mi == NULL)
1006 {
1007 mh->err = MPG123_ERR_NULL;
1008 return MPG123_ERR;
1009 }
1010 b = init_track(mh);
1011 if(b < 0) return b;
1012
1013 mi->version = mh->mpeg25 ? MPG123_2_5 : (mh->lsf ? MPG123_2_0 : MPG123_1_0);
1014 mi->layer = mh->lay;
1015 mi->rate = frame_freq(mh);
1016 switch(mh->mode)
1017 {
1018 case 0: mi->mode = MPG123_M_STEREO; break;
1019 case 1: mi->mode = MPG123_M_JOINT; break;
1020 case 2: mi->mode = MPG123_M_DUAL; break;
1021 case 3: mi->mode = MPG123_M_MONO; break;
1022 default: error("That mode cannot be!");
1023 }
1024 mi->mode_ext = mh->mode_ext;
1025 mi->framesize = mh->framesize+4; /* Include header. */
1026 mi->flags = 0;
1027 if(mh->error_protection) mi->flags |= MPG123_CRC;
1028 if(mh->copyright) mi->flags |= MPG123_COPYRIGHT;
1029 if(mh->extension) mi->flags |= MPG123_PRIVATE;
1030 if(mh->original) mi->flags |= MPG123_ORIGINAL;
1031 mi->emphasis = mh->emphasis;
1032 mi->bitrate = frame_bitrate(mh);
1033 mi->abr_rate = mh->abr_rate;
1034 mi->vbr = mh->vbr;
1035 return MPG123_OK;
1036 }
1037
1038 int attribute_align_arg mpg123_getformat(mpg123_handle *mh, long *rate, int *channels, int *encoding)
1039 {
1040 int b;
1041
1042 if(mh == NULL) return MPG123_BAD_HANDLE;
1043 b = init_track(mh);
1044 if(b < 0) return b;
1045
1046 if(rate != NULL) *rate = mh->af.rate;
1047 if(channels != NULL) *channels = mh->af.channels;
1048 if(encoding != NULL) *encoding = mh->af.encoding;
1049 mh->new_format = 0;
1050 return MPG123_OK;
1051 }
1052
1053 off_t attribute_align_arg mpg123_timeframe(mpg123_handle *mh, double seconds)
1054 {
1055 off_t b;
1056
1057 if(mh == NULL) return MPG123_ERR;
1058 b = init_track(mh);
1059 if(b<0) return b;
1060 return (off_t)(seconds/mpg123_tpf(mh));
1061 }
1062
1063 /*
1064 Now, where are we? We need to know the last decoded frame... and what's left of it in buffer.
1065 The current frame number can mean the last decoded frame or the to-be-decoded frame.
1066 If mh->to_decode, then mh->num frames have been decoded, the frame mh->num now coming next.
1067 If not, we have the possibility of mh->num+1 frames being decoded or nothing at all.
1068 Then, there is firstframe...when we didn't reach it yet, then the next data will come from there.
1069 mh->num starts with -1
1070 */
1071 off_t attribute_align_arg mpg123_tell(mpg123_handle *mh)
1072 {
1073 if(mh == NULL) return MPG123_ERR;
1074 if(track_need_init(mh)) return 0;
1075 /* Now we have all the info at hand. */
1076 debug5("tell: %li/%i first %li buffer %lu; frame_outs=%li", (long)mh->num, mh->to_decode, (long)mh->firstframe, (unsigned long)mh->buffer.fill, (long)frame_outs(mh, mh->num));
1077
1078 { /* Funny block to keep C89 happy. */
1079 off_t pos = 0;
1080 if((mh->num < mh->firstframe) || (mh->num == mh->firstframe && mh->to_decode))
1081 { /* We are at the beginning, expect output from firstframe on. */
1082 pos = frame_outs(mh, mh->firstframe);
1083 #ifdef GAPLESS
1084 pos += mh->firstoff;
1085 #endif
1086 }
1087 else if(mh->to_decode)
1088 { /* We start fresh with this frame. Buffer should be empty, but we make sure to count it in. */
1089 pos = frame_outs(mh, mh->num) - bytes_to_samples(mh, mh->buffer.fill);
1090 }
1091 else
1092 { /* We serve what we have in buffer and then the beginning of next frame... */
1093 pos = frame_outs(mh, mh->num+1) - bytes_to_samples(mh, mh->buffer.fill);
1094 }
1095 /* Substract padding and delay from the beginning. */
1096 pos = SAMPLE_ADJUST(mh,pos);
1097 /* Negative sample offsets are not right, less than nothing is still nothing. */
1098 return pos>0 ? pos : 0;
1099 }
1100 }
1101
1102 off_t attribute_align_arg mpg123_tellframe(mpg123_handle *mh)
1103 {
1104 if(mh == NULL) return MPG123_ERR;
1105 if(mh->num < mh->firstframe) return mh->firstframe;
1106 if(mh->to_decode) return mh->num;
1107 /* Consider firstoff? */
1108 return mh->buffer.fill ? mh->num : mh->num + 1;
1109 }
1110
1111 off_t attribute_align_arg mpg123_tell_stream(mpg123_handle *mh)
1112 {
1113 if(mh == NULL) return MPG123_ERR;
1114 /* mh->rd is at least a bad_reader, so no worry. */
1115 return mh->rd->tell(mh);
1116 }
1117
1118 static int do_the_seek(mpg123_handle *mh)
1119 {
1120 int b;
1121 off_t fnum = SEEKFRAME(mh);
1122 mh->buffer.fill = 0;
1123
1124 /* If we are inside the ignoreframe - firstframe window, we may get away without actual seeking. */
1125 if(mh->num < mh->firstframe)
1126 {
1127 mh->to_decode = FALSE; /* In any case, don't decode the current frame, perhaps ignore instead. */
1128 if(mh->num > fnum) return MPG123_OK;
1129 }
1130
1131 /* If we are already there, we are fine either for decoding or for ignoring. */
1132 if(mh->num == fnum && (mh->to_decode || fnum < mh->firstframe)) return MPG123_OK;
1133 /* We have the frame before... just go ahead as normal. */
1134 if(mh->num == fnum-1)
1135 {
1136 mh->to_decode = FALSE;
1137 return MPG123_OK;
1138 }
1139
1140 /* OK, real seeking follows... clear buffers and go for it. */
1141 frame_buffers_reset(mh);
1142 #ifndef NO_NTOM
1143 if(mh->down_sample == 3)
1144 {
1145 ntom_set_ntom(mh, fnum);
1146 debug3("fixed ntom for frame %"OFF_P" to %lu, num=%"OFF_P, (off_p)fnum, mh->ntom_val[0], (off_p)mh->num);
1147 }
1148 #endif
1149 b = mh->rd->seek_frame(mh, fnum);
1150 if(mh->header_change > 1)
1151 {
1152 if(decode_update(mh) < 0) return MPG123_ERR;
1153 mh->header_change = 0;
1154 }
1155 debug1("seek_frame returned: %i", b);
1156 if(b<0) return b;
1157 /* Only mh->to_ignore is TRUE. */
1158 if(mh->num < mh->firstframe) mh->to_decode = FALSE;
1159
1160 mh->playnum = mh->num;
1161 return 0;
1162 }
1163
1164 off_t attribute_align_arg mpg123_seek(mpg123_handle *mh, off_t sampleoff, int whence)
1165 {
1166 int b;
1167 off_t pos;
1168
1169 pos = mpg123_tell(mh); /* adjusted samples */
1170 /* pos < 0 also can mean that simply a former seek failed at the lower levels.
1171 In that case, we only allow absolute seeks. */
1172 if(pos < 0 && whence != SEEK_SET)
1173 { /* Unless we got the obvious error of NULL handle, this is a special seek failure. */
1174 if(mh != NULL) mh->err = MPG123_NO_RELSEEK;
1175 return MPG123_ERR;
1176 }
1177 if((b=init_track(mh)) < 0) return b;
1178 switch(whence)
1179 {
1180 case SEEK_CUR: pos += sampleoff; break;
1181 case SEEK_SET: pos = sampleoff; break;
1182 case SEEK_END:
1183 /* When we do not know the end already, we can try to find it. */
1184 if(mh->track_frames < 1 && (mh->rdat.flags & READER_SEEKABLE))
1185 mpg123_scan(mh);
1186 if(mh->track_frames > 0) pos = SAMPLE_ADJUST(mh,frame_outs(mh, mh->track_frames)) - sampleoff;
1187 #ifdef GAPLESS
1188 else if(mh->end_os > 0) pos = SAMPLE_ADJUST(mh,mh->end_os) - sampleoff;
1189 #endif
1190 else
1191 {
1192 mh->err = MPG123_NO_SEEK_FROM_END;
1193 return MPG123_ERR;
1194 }
1195 break;
1196 default: mh->err = MPG123_BAD_WHENCE; return MPG123_ERR;
1197 }
1198 if(pos < 0) pos = 0;
1199 /* pos now holds the wanted sample offset in adjusted samples */
1200 frame_set_seek(mh, SAMPLE_UNADJUST(mh,pos));
1201 pos = do_the_seek(mh);
1202 if(pos < 0) return pos;
1203
1204 return mpg123_tell(mh);
1205 }
1206
1207 /*
1208 A bit more tricky... libmpg123 does not do the seeking itself.
1209 All it can do is to ignore frames until the wanted one is there.
1210 The caller doesn't know where a specific frame starts and mpg123 also only knows the general region after it scanned the file.
1211 Well, it is tricky...
1212 */
1213 off_t attribute_align_arg mpg123_feedseek(mpg123_handle *mh, off_t sampleoff, int whence, off_t *input_offset)
1214 {
1215 int b;
1216 off_t pos;
1217
1218 pos = mpg123_tell(mh); /* adjusted samples */
1219 debug3("seek from %li to %li (whence=%i)", (long)pos, (long)sampleoff, whence);
1220 /* The special seek error handling does not apply here... there is no lowlevel I/O. */
1221 if(pos < 0) return pos; /* mh == NULL is covered in mpg123_tell() */
1222 #ifndef NO_FEEDER
1223 if(input_offset == NULL)
1224 {
1225 mh->err = MPG123_NULL_POINTER;
1226 return MPG123_ERR;
1227 }
1228
1229 if((b=init_track(mh)) < 0) return b; /* May need more to do anything at all. */
1230
1231 switch(whence)
1232 {
1233 case SEEK_CUR: pos += sampleoff; break;
1234 case SEEK_SET: pos = sampleoff; break;
1235 case SEEK_END:
1236 if(mh->track_frames > 0) pos = SAMPLE_ADJUST(mh,frame_outs(mh, mh->track_frames)) - sampleoff;
1237 #ifdef GAPLESS
1238 else if(mh->end_os >= 0) pos = SAMPLE_ADJUST(mh,mh->end_os) - sampleoff;
1239 #endif
1240 else
1241 {
1242 mh->err = MPG123_NO_SEEK_FROM_END;
1243 return MPG123_ERR;
1244 }
1245 break;
1246 default: mh->err = MPG123_BAD_WHENCE; return MPG123_ERR;
1247 }
1248 if(pos < 0) pos = 0;
1249 frame_set_seek(mh, SAMPLE_UNADJUST(mh,pos));
1250 pos = SEEKFRAME(mh);
1251 mh->buffer.fill = 0;
1252
1253 /* Shortcuts without modifying input stream. */
1254 *input_offset = mh->rdat.buffer.fileoff + mh->rdat.buffer.size;
1255 if(mh->num < mh->firstframe) mh->to_decode = FALSE;
1256 if(mh->num == pos && mh->to_decode) goto feedseekend;
1257 if(mh->num == pos-1) goto feedseekend;
1258 /* Whole way. */
1259 *input_offset = feed_set_pos(mh, frame_index_find(mh, SEEKFRAME(mh), &pos));
1260 mh->num = pos-1; /* The next read frame will have num = pos. */
1261 if(*input_offset < 0) return MPG123_ERR;
1262
1263 feedseekend:
1264 return mpg123_tell(mh);
1265 #else
1266 mh->err = MPG123_MISSING_FEATURE;
1267 return MPG123_ERR;
1268 #endif
1269 }
1270
1271 off_t attribute_align_arg mpg123_seek_frame(mpg123_handle *mh, off_t offset, int whence)
1272 {
1273 int b;
1274 off_t pos = 0;
1275
1276 if(mh == NULL) return MPG123_ERR;
1277 if((b=init_track(mh)) < 0) return b;
1278
1279 /* Could play games here with to_decode... */
1280 pos = mh->num;
1281 switch(whence)
1282 {
1283 case SEEK_CUR: pos += offset; break;
1284 case SEEK_SET: pos = offset; break;
1285 case SEEK_END:
1286 if(mh->track_frames > 0) pos = mh->track_frames - offset;
1287 else
1288 {
1289 mh->err = MPG123_NO_SEEK_FROM_END;
1290 return MPG123_ERR;
1291 }
1292 break;
1293 default:
1294 mh->err = MPG123_BAD_WHENCE;
1295 return MPG123_ERR;
1296 }
1297 if(pos < 0) pos = 0;
1298 /* Not limiting the possible position on end for the chance that there might be more to the stream than announced via track_frames. */
1299
1300 frame_set_frameseek(mh, pos);
1301 pos = do_the_seek(mh);
1302 if(pos < 0) return pos;
1303
1304 return mpg123_tellframe(mh);
1305 }
1306
1307 int attribute_align_arg mpg123_set_filesize(mpg123_handle *mh, off_t size)
1308 {
1309 if(mh == NULL) return MPG123_BAD_HANDLE;
1310
1311 mh->rdat.filelen = size;
1312 return MPG123_OK;
1313 }
1314
1315 off_t attribute_align_arg mpg123_framelength(mpg123_handle *mh)
1316 {
1317 int b;
1318 if(mh == NULL)
1319 return MPG123_ERR;
1320 b = init_track(mh);
1321 if(b<0)
1322 return b;
1323 if(mh->track_frames > 0)
1324 return mh->track_frames;
1325 if(mh->rdat.filelen > 0)
1326 { /* A bad estimate. Ignoring tags 'n stuff. */
1327 double bpf = mh->mean_framesize > 0.
1328 ? mh->mean_framesize
1329 : compute_bpf(mh);
1330 return (off_t)((double)(mh->rdat.filelen)/bpf+0.5);
1331 }
1332 /* Last resort: No view of the future, can at least count the frames that
1333 were already parsed. */
1334 if(mh->num > -1)
1335 return mh->num+1;
1336 /* Giving up. */
1337 return MPG123_ERR;
1338 }
1339
1340 off_t attribute_align_arg mpg123_length(mpg123_handle *mh)
1341 {
1342 int b;
1343 off_t length;
1344
1345 if(mh == NULL) return MPG123_ERR;
1346 b = init_track(mh);
1347 if(b<0) return b;
1348 if(mh->track_samples > -1) length = mh->track_samples;
1349 else if(mh->track_frames > 0) length = mh->track_frames*mh->spf;
1350 else if(mh->rdat.filelen > 0) /* Let the case of 0 length just fall through. */
1351 {
1352 /* A bad estimate. Ignoring tags 'n stuff. */
1353 double bpf = mh->mean_framesize ? mh->mean_framesize : compute_bpf(mh);
1354 length = (off_t)((double)(mh->rdat.filelen)/bpf*mh->spf);
1355 }
1356 else if(mh->rdat.filelen == 0) return mpg123_tell(mh); /* we could be in feeder mode */
1357 else return MPG123_ERR; /* No length info there! */
1358
1359 debug1("mpg123_length: internal sample length: %"OFF_P, (off_p)length);
1360
1361 length = frame_ins2outs(mh, length);
1362 debug1("mpg123_length: external sample length: %"OFF_P, (off_p)length);
1363 length = SAMPLE_ADJUST(mh,length);
1364 return length;
1365 }
1366
1367
1368 int attribute_align_arg mpg123_scan(mpg123_handle *mh)
1369 {
1370 int b;
1371 off_t oldpos;
1372 off_t track_frames = 0;
1373 off_t track_samples = 0;
1374
1375 if(mh == NULL) return MPG123_BAD_HANDLE;
1376 if(!(mh->rdat.flags & READER_SEEKABLE)){ mh->err = MPG123_NO_SEEK; return MPG123_ERR; }
1377 /* Scan through the _whole_ file, since the current position is no count but computed assuming constant samples per frame. */
1378 /* Also, we can just keep the current buffer and seek settings. Just operate on input frames here. */
1379 debug("issuing scan");
1380 b = init_track(mh); /* mh->num >= 0 !! */
1381 if(b<0)
1382 {
1383 if(b == MPG123_DONE) return MPG123_OK;
1384 else return MPG123_ERR; /* Must be error here, NEED_MORE is not for seekable streams. */
1385 }
1386 oldpos = mpg123_tell(mh);
1387 b = mh->rd->seek_frame(mh, 0);
1388 if(b<0 || mh->num != 0) return MPG123_ERR;
1389 /* One frame must be there now. */
1390 track_frames = 1;
1391 track_samples = mh->spf; /* Internal samples. */
1392 debug("TODO: We should disable gapless code when encountering inconsistent mh->spf!");
1393 debug(" ... at least unset MPG123_ACCURATE.");
1394 /* Do not increment mh->track_frames in the loop as tha would confuse Frankenstein detection. */
1395 while(read_frame(mh) == 1)
1396 {
1397 ++track_frames;
1398 track_samples += mh->spf;
1399 }
1400 mh->track_frames = track_frames;
1401 mh->track_samples = track_samples;
1402 debug2("Scanning yielded %"OFF_P" track samples, %"OFF_P" frames.", (off_p)mh->track_samples, (off_p)mh->track_frames);
1403 #ifdef GAPLESS
1404 /* Also, think about usefulness of that extra value track_samples ... it could be used for consistency checking. */
1405 if(mh->p.flags & MPG123_GAPLESS) frame_gapless_update(mh, mh->track_samples);
1406 #endif
1407 return mpg123_seek(mh, oldpos, SEEK_SET) >= 0 ? MPG123_OK : MPG123_ERR;
1408 }
1409
1410 int attribute_align_arg mpg123_meta_check(mpg123_handle *mh)
1411 {
1412 if(mh != NULL) return mh->metaflags;
1413 else return 0;
1414 }
1415
1416 void attribute_align_arg mpg123_meta_free(mpg123_handle *mh)
1417 {
1418 if(mh == NULL) return;
1419
1420 reset_id3(mh);
1421 reset_icy(&mh->icy);
1422 }
1423
1424 int attribute_align_arg mpg123_id3(mpg123_handle *mh, mpg123_id3v1 **v1, mpg123_id3v2 **v2)
1425 {
1426 if(v1 != NULL) *v1 = NULL;
1427 if(v2 != NULL) *v2 = NULL;
1428 if(mh == NULL) return MPG123_BAD_HANDLE;
1429
1430 if(mh->metaflags & MPG123_ID3)
1431 {
1432 id3_link(mh);
1433 if(v1 != NULL && mh->rdat.flags & READER_ID3TAG) *v1 = (mpg123_id3v1*) mh->id3buf;
1434 if(v2 != NULL)
1435 #ifdef NO_ID3V2
1436 *v2 = NULL;
1437 #else
1438 *v2 = &mh->id3v2;
1439 #endif
1440
1441 mh->metaflags |= MPG123_ID3;
1442 mh->metaflags &= ~MPG123_NEW_ID3;
1443 }
1444 return MPG123_OK;
1445 }
1446
1447 int attribute_align_arg mpg123_icy(mpg123_handle *mh, char **icy_meta)
1448 {
1449 if(mh == NULL) return MPG123_BAD_HANDLE;
1450 #ifndef NO_ICY
1451 if(icy_meta == NULL)
1452 {
1453 mh->err = MPG123_NULL_POINTER;
1454 return MPG123_ERR;
1455 }
1456 *icy_meta = NULL;
1457
1458 if(mh->metaflags & MPG123_ICY)
1459 {
1460 *icy_meta = mh->icy.data;
1461 mh->metaflags |= MPG123_ICY;
1462 mh->metaflags &= ~MPG123_NEW_ICY;
1463 }
1464 return MPG123_OK;
1465 #else
1466 mh->err = MPG123_MISSING_FEATURE;
1467 return MPG123_ERR;
1468 #endif
1469 }
1470
1471 char* attribute_align_arg mpg123_icy2utf8(const char* icy_text)
1472 {
1473 #ifndef NO_ICY
1474 return icy2utf8(icy_text, 0);
1475 #else
1476 return NULL;
1477 #endif
1478 }
1479
1480 /* That one is always defined... it's not worth it to remove it for NO_ID3V2. */
1481 enum mpg123_text_encoding attribute_align_arg mpg123_enc_from_id3(unsigned char id3_enc_byte)
1482 {
1483 switch(id3_enc_byte)
1484 {
1485 case mpg123_id3_latin1: return mpg123_text_latin1;
1486 case mpg123_id3_utf16bom: return mpg123_text_utf16bom; /* ID3v2.3 has UCS-2 with BOM here. */
1487 case mpg123_id3_utf16be: return mpg123_text_utf16be;
1488 case mpg123_id3_utf8: return mpg123_text_utf8;
1489 default: return mpg123_text_unknown;
1490 }
1491 }
1492
1493 #ifndef NO_STRING
1494 int mpg123_store_utf8(mpg123_string *sb, enum mpg123_text_encoding enc, const unsigned char *source, size_t source_size)
1495 {
1496 switch(enc)
1497 {
1498 #ifndef NO_ID3V2
1499 /* The encodings we get from ID3v2 tags. */
1500 case mpg123_text_utf8:
1501 id3_to_utf8(sb, mpg123_id3_utf8, source, source_size, 0);
1502 break;
1503 case mpg123_text_latin1:
1504 id3_to_utf8(sb, mpg123_id3_latin1, source, source_size, 0);
1505 break;
1506 case mpg123_text_utf16bom:
1507 case mpg123_text_utf16:
1508 id3_to_utf8(sb, mpg123_id3_utf16bom, source, source_size, 0);
1509 break;
1510 /* Special because one cannot skip zero bytes here. */
1511 case mpg123_text_utf16be:
1512 id3_to_utf8(sb, mpg123_id3_utf16be, source, source_size, 0);
1513 break;
1514 #endif
1515 #ifndef NO_ICY
1516 /* ICY encoding... */
1517 case mpg123_text_icy:
1518 case mpg123_text_cp1252:
1519 {
1520 mpg123_free_string(sb);
1521 /* Paranoia: Make sure that the string ends inside the buffer... */
1522 if(source[source_size-1] == 0)
1523 {
1524 /* Convert from ICY encoding... with force applied or not. */
1525 char *tmpstring = icy2utf8((const char*)source, enc == mpg123_text_cp1252 ? 1 : 0);
1526 if(tmpstring != NULL)
1527 {
1528 mpg123_set_string(sb, tmpstring);
1529 free(tmpstring);
1530 }
1531 }
1532 }
1533 break;
1534 #endif
1535 default:
1536 mpg123_free_string(sb);
1537 }
1538 /* At least a trailing null of some form should be there... */
1539 return (sb->fill > 0) ? 1 : 0;
1540 }
1541 #endif
1542
1543 int attribute_align_arg mpg123_index(mpg123_handle *mh, off_t **offsets, off_t *step, size_t *fill)
1544 {
1545 if(mh == NULL) return MPG123_BAD_HANDLE;
1546 if(offsets == NULL || step == NULL || fill == NULL)
1547 {
1548 mh->err = MPG123_BAD_INDEX_PAR;
1549 return MPG123_ERR;
1550 }
1551 #ifdef FRAME_INDEX
1552 *offsets = mh->index.data;
1553 *step = mh->index.step;
1554 *fill = mh->index.fill;
1555 #else
1556 *offsets = NULL;
1557 *step = 0;
1558 *fill = 0;
1559 #endif
1560 return MPG123_OK;
1561 }
1562
1563 int attribute_align_arg mpg123_set_index(mpg123_handle *mh, off_t *offsets, off_t step, size_t fill)
1564 {
1565 if(mh == NULL) return MPG123_BAD_HANDLE;
1566 #ifdef FRAME_INDEX
1567 if(step == 0)
1568 {
1569 mh->err = MPG123_BAD_INDEX_PAR;
1570 return MPG123_ERR;
1571 }
1572 if(fi_set(&mh->index, offsets, step, fill) == -1)
1573 {
1574 mh->err = MPG123_OUT_OF_MEM;
1575 return MPG123_ERR;
1576 }
1577 return MPG123_OK;
1578 #else
1579 mh->err = MPG123_MISSING_FEATURE;
1580 return MPG123_ERR;
1581 #endif
1582 }
1583
1584 int attribute_align_arg mpg123_close(mpg123_handle *mh)
1585 {
1586 if(mh == NULL) return MPG123_BAD_HANDLE;
1587
1588 /* mh->rd is never NULL! */
1589 if(mh->rd->close != NULL) mh->rd->close(mh);
1590
1591 if(mh->new_format)
1592 {
1593 debug("Hey, we are closing a track before the new format has been queried...");
1594 invalidate_format(&mh->af);
1595 mh->new_format = 0;
1596 }
1597 /* Always reset the frame buffers on close, so we cannot forget it in funky opening routines (wrappers, even). */
1598 frame_reset(mh);
1599 return MPG123_OK;
1600 }
1601
1602 void attribute_align_arg mpg123_delete(mpg123_handle *mh)
1603 {
1604 if(mh != NULL)
1605 {
1606 mpg123_close(mh);
1607 frame_exit(mh); /* free buffers in frame */
1608 free(mh); /* free struct; cast? */
1609 }
1610 }
1611
1612 static const char *mpg123_error[] =
1613 {
1614 "No error... (code 0)",
1615 "Unable to set up output format! (code 1)",
1616 "Invalid channel number specified. (code 2)",
1617 "Invalid sample rate specified. (code 3)",
1618 "Unable to allocate memory for 16 to 8 converter table! (code 4)",
1619 "Bad parameter id! (code 5)",
1620 "Bad buffer given -- invalid pointer or too small size. (code 6)",
1621 "Out of memory -- some malloc() failed. (code 7)",
1622 "You didn't initialize the library! (code 8)",
1623 "Invalid decoder choice. (code 9)",
1624 "Invalid mpg123 handle. (code 10)",
1625 "Unable to initialize frame buffers (out of memory?)! (code 11)",
1626 "Invalid RVA mode. (code 12)",
1627 "This build doesn't support gapless decoding. (code 13)",
1628 "Not enough buffer space. (code 14)",
1629 "Incompatible numeric data types. (code 15)",
1630 "Bad equalizer band. (code 16)",
1631 "Null pointer given where valid storage address needed. (code 17)",
1632 "Error reading the stream. (code 18)",
1633 "Cannot seek from end (end is not known). (code 19)",
1634 "Invalid 'whence' for seek function. (code 20)",
1635 "Build does not support stream timeouts. (code 21)",
1636 "File access error. (code 22)",
1637 "Seek not supported by stream. (code 23)",
1638 "No stream opened. (code 24)",
1639 "Bad parameter handle. (code 25)",
1640 "Invalid parameter addresses for index retrieval. (code 26)",
1641 "Lost track in the bytestream and did not attempt resync. (code 27)",
1642 "Failed to find valid MPEG data within limit on resync. (code 28)",
1643 "No 8bit encoding possible. (code 29)",
1644 "Stack alignment is not good. (code 30)",
1645 "You gave me a NULL buffer? (code 31)",
1646 "File position is screwed up, please do an absolute seek (code 32)",
1647 "Inappropriate NULL-pointer provided.",
1648 "Bad key value given.",
1649 "There is no frame index (disabled in this build).",
1650 "Frame index operation failed.",
1651 "Decoder setup failed (invalid combination of settings?)",
1652 "Feature not in this build."
1653 ,"Some bad value has been provided."
1654 ,"Low-level seeking has failed (call to lseek(), usually)."
1655 ,"Custom I/O obviously not prepared."
1656 ,"Overflow in LFS (large file support) conversion."
1657 ,"Overflow in integer conversion."
1658 };
1659
1660 const char* attribute_align_arg mpg123_plain_strerror(int errcode)
1661 {
1662 if(errcode >= 0 && errcode < sizeof(mpg123_error)/sizeof(char*))
1663 return mpg123_error[errcode];
1664 else switch(errcode)
1665 {
1666 case MPG123_ERR:
1667 return "A generic mpg123 error.";
1668 case MPG123_DONE:
1669 return "Message: I am done with this track.";
1670 case MPG123_NEED_MORE:
1671 return "Message: Feed me more input data!";
1672 case MPG123_NEW_FORMAT:
1673 return "Message: Prepare for a changed audio format (query the new one)!";
1674 default:
1675 return "I have no idea - an unknown error code!";
1676 }
1677 }
1678
1679 int attribute_align_arg mpg123_errcode(mpg123_handle *mh)
1680 {
1681 if(mh != NULL) return mh->err;
1682 return MPG123_BAD_HANDLE;
1683 }
1684
1685 const char* attribute_align_arg mpg123_strerror(mpg123_handle *mh)
1686 {
1687 return mpg123_plain_strerror(mpg123_errcode(mh));
1688 }