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