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