Skip to content

Latest commit

 

History

History
1662 lines (1512 loc) · 47.1 KB

libmpg123.c

File metadata and controls

1662 lines (1512 loc) · 47.1 KB
 
Nov 10, 2019
Nov 10, 2019
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
libmpg123: MPEG Audio Decoder library
copyright 1995-2014 by the mpg123 project - free software under the terms of the LGPL 2.1
see COPYING and AUTHORS files in distribution or http://mpg123.org
*/
#include "mpg123lib_intern.h"
#include "icy2utf8.h"
/* this also includes debug.h: */
#include "gapless.h"
#define SEEKFRAME(mh) ((mh)->ignoreframe < 0 ? 0 : (mh)->ignoreframe)
static int initialized = 0;
int attribute_align_arg mpg123_init(void)
{
if((sizeof(short) != 2) || (sizeof(long) < 4)) return MPG123_BAD_TYPES;
if(initialized) return MPG123_OK; /* no need to initialize twice */
#ifndef NO_LAYER12
init_layer12(); /* inits also shared tables with layer1 */
#endif
#ifndef NO_LAYER3
init_layer3();
#endif
prepare_decode_tables();
check_decoders();
initialized = 1;
return MPG123_OK;
}
void attribute_align_arg mpg123_exit(void)
{
/* nothing yet, but something later perhaps */
}
/* create a new handle with specified decoder, decoder can be "", "auto" or NULL for auto-detection */
mpg123_handle attribute_align_arg *mpg123_new(const char* decoder, int *error)
{
return mpg123_parnew(NULL, decoder, error);
}
/* ...the full routine with optional initial parameters to override defaults. */
mpg123_handle attribute_align_arg *mpg123_parnew(mpg123_pars *mp, const char* decoder, int *error)
{
mpg123_handle *fr = NULL;
int err = MPG123_OK;
if(initialized) fr = (mpg123_handle*) malloc(sizeof(mpg123_handle));
else err = MPG123_NOT_INITIALIZED;
if(fr != NULL)
{
frame_init_par(fr, mp);
debug("cpu opt setting");
if(frame_cpu_opt(fr, decoder) != 1)
{
err = MPG123_BAD_DECODER;
frame_exit(fr);
free(fr);
fr = NULL;
}
}
if(fr != NULL)
{
fr->decoder_change = 1;
}
else if(err == MPG123_OK) err = MPG123_OUT_OF_MEM;
if(error != NULL) *error = err;
return fr;
}
int attribute_align_arg mpg123_decoder(mpg123_handle *mh, const char* decoder)
{
enum optdec dt = dectype(decoder);
if(mh == NULL) return MPG123_BAD_HANDLE;
if(dt == nodec)
{
mh->err = MPG123_BAD_DECODER;
return MPG123_ERR;
}
if(dt == mh->cpu_opts.type) return MPG123_OK;
/* Now really change. */
/* frame_exit(mh);
frame_init(mh); */
debug("cpu opt setting");
if(frame_cpu_opt(mh, decoder) != 1)
{
mh->err = MPG123_BAD_DECODER;
frame_exit(mh);
return MPG123_ERR;
}
/* New buffers for decoder are created in frame_buffers() */
if((frame_outbuffer(mh) != 0))
{
mh->err = MPG123_NO_BUFFERS;
frame_exit(mh);
return MPG123_ERR;
}
/* Do _not_ call decode_update here! That is only allowed after a first MPEG frame has been met. */
mh->decoder_change = 1;
return MPG123_OK;
}
int attribute_align_arg mpg123_param(mpg123_handle *mh, enum mpg123_parms key, long val, double fval)
{
int r;
if(mh == NULL) return MPG123_BAD_HANDLE;
r = mpg123_par(&mh->p, key, val, fval);
if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
else
{ /* Special treatment for some settings. */
#ifdef FRAME_INDEX
if(key == MPG123_INDEX_SIZE)
{ /* Apply frame index size and grow property on the fly. */
r = frame_index_setup(mh);
if(r != MPG123_OK) mh->err = MPG123_INDEX_FAIL;
}
#endif
#ifndef NO_FEEDER
/* Feeder pool size is applied right away, reader will react to that. */
if(key == MPG123_FEEDPOOL || key == MPG123_FEEDBUFFER)
bc_poolsize(&mh->rdat.buffer, mh->p.feedpool, mh->p.feedbuffer);
#endif
}
return r;
}
int attribute_align_arg mpg123_par(mpg123_pars *mp, enum mpg123_parms key, long val, double fval)
{
int ret = MPG123_OK;
if(mp == NULL) return MPG123_BAD_PARS;
switch(key)
{
case MPG123_VERBOSE:
mp->verbose = val;
break;
case MPG123_FLAGS:
#ifndef GAPLESS
if(val & MPG123_GAPLESS) ret = MPG123_NO_GAPLESS;
#endif
if(ret == MPG123_OK) mp->flags = val;
debug1("set flags to 0x%lx", (unsigned long) mp->flags);
break;
case MPG123_ADD_FLAGS:
#ifndef GAPLESS
/* Enabling of gapless mode doesn't work when it's not there, but disabling (below) is no problem. */
if(val & MPG123_GAPLESS) ret = MPG123_NO_GAPLESS;
else
#endif
mp->flags |= val;
debug1("set flags to 0x%lx", (unsigned long) mp->flags);
break;
case MPG123_REMOVE_FLAGS:
mp->flags &= ~val;
debug1("set flags to 0x%lx", (unsigned long) mp->flags);
break;
case MPG123_FORCE_RATE: /* should this trigger something? */
#ifdef NO_NTOM
if(val > 0)
ret = MPG123_BAD_RATE;
#else
if(val > 96000) ret = MPG123_BAD_RATE;
else mp->force_rate = val < 0 ? 0 : val; /* >0 means enable, 0 disable */
#endif
break;
case MPG123_DOWN_SAMPLE:
#ifdef NO_DOWNSAMPLE
if(val != 0) ret = MPG123_BAD_RATE;
#else
if(val < 0 || val > 2) ret = MPG123_BAD_RATE;
else mp->down_sample = (int)val;
#endif
break;
case MPG123_RVA:
if(val < 0 || val > MPG123_RVA_MAX) ret = MPG123_BAD_RVA;
else mp->rva = (int)val;
break;
case MPG123_DOWNSPEED:
mp->halfspeed = val < 0 ? 0 : val;
break;
case MPG123_UPSPEED:
mp->doublespeed = val < 0 ? 0 : val;
break;
case MPG123_ICY_INTERVAL:
#ifndef NO_ICY
mp->icy_interval = val > 0 ? val : 0;
#else
if(val > 0) ret = MPG123_BAD_PARAM;
#endif
break;
case MPG123_OUTSCALE:
/* Choose the value that is non-zero, if any.
Downscaling integers to 1.0 . */
mp->outscale = val == 0 ? fval : (double)val/SHORT_SCALE;
break;
case MPG123_TIMEOUT:
#ifdef TIMEOUT_READ
mp->timeout = val >= 0 ? val : 0;
#else
if(val > 0) ret = MPG123_NO_TIMEOUT;
#endif
break;
case MPG123_RESYNC_LIMIT:
mp->resync_limit = val;
break;
case MPG123_INDEX_SIZE:
#ifdef FRAME_INDEX
mp->index_size = val;
#else
ret = MPG123_NO_INDEX;
#endif
break;
case MPG123_PREFRAMES:
if(val >= 0) mp->preframes = val;
else ret = MPG123_BAD_VALUE;
break;
case MPG123_FEEDPOOL:
#ifndef NO_FEEDER
if(val >= 0) mp->feedpool = val;
else ret = MPG123_BAD_VALUE;
#else
ret = MPG123_MISSING_FEATURE;
#endif
break;
case MPG123_FEEDBUFFER:
#ifndef NO_FEEDER
if(val > 0) mp->feedbuffer = val;
else ret = MPG123_BAD_VALUE;
#else
ret = MPG123_MISSING_FEATURE;
#endif
break;
default:
ret = MPG123_BAD_PARAM;
}
return ret;
}
int attribute_align_arg mpg123_getparam(mpg123_handle *mh, enum mpg123_parms key, long *val, double *fval)
{
int r;
if(mh == NULL) return MPG123_BAD_HANDLE;
r = mpg123_getpar(&mh->p, key, val, fval);
if(r != MPG123_OK){ mh->err = r; r = MPG123_ERR; }
return r;
}
int attribute_align_arg mpg123_getpar(mpg123_pars *mp, enum mpg123_parms key, long *val, double *fval)
{
int ret = 0;
if(mp == NULL) return MPG123_BAD_PARS;
switch(key)
{
case MPG123_VERBOSE:
if(val) *val = mp->verbose;
break;
case MPG123_FLAGS:
case MPG123_ADD_FLAGS:
if(val) *val = mp->flags;
break;
case MPG123_FORCE_RATE:
if(val)
#ifdef NO_NTOM
*val = 0;
#else
*val = mp->force_rate;
#endif
break;
case MPG123_DOWN_SAMPLE:
if(val) *val = mp->down_sample;
break;
case MPG123_RVA:
if(val) *val = mp->rva;
break;
case MPG123_DOWNSPEED:
if(val) *val = mp->halfspeed;
break;
case MPG123_UPSPEED:
if(val) *val = mp->doublespeed;
break;
case MPG123_ICY_INTERVAL:
#ifndef NO_ICY
if(val) *val = (long)mp->icy_interval;
#else
if(val) *val = 0;
#endif
break;
case MPG123_OUTSCALE:
if(fval) *fval = mp->outscale;
if(val) *val = (long)(mp->outscale*SHORT_SCALE);
break;
case MPG123_RESYNC_LIMIT:
if(val) *val = mp->resync_limit;
break;
case MPG123_INDEX_SIZE:
if(val)
#ifdef FRAME_INDEX
*val = mp->index_size;
#else
*val = 0; /* graceful fallback: no index is index of zero size */
#endif
break;
case MPG123_PREFRAMES:
*val = mp->preframes;
break;
case MPG123_FEEDPOOL:
#ifndef NO_FEEDER
*val = mp->feedpool;
#else
ret = MPG123_MISSING_FEATURE;
#endif
break;
case MPG123_FEEDBUFFER:
#ifndef NO_FEEDER
*val = mp->feedbuffer;
#else
ret = MPG123_MISSING_FEATURE;
#endif
break;
default:
ret = MPG123_BAD_PARAM;
}
return ret;
}
int attribute_align_arg mpg123_getstate(mpg123_handle *mh, enum mpg123_state key, long *val, double *fval)
{
int ret = MPG123_OK;
long theval = 0;
double thefval = 0.;
if(mh == NULL) return MPG123_BAD_HANDLE;
switch(key)
{
case MPG123_ACCURATE:
theval = mh->state_flags & FRAME_ACCURATE;
break;
case MPG123_FRANKENSTEIN:
theval = mh->state_flags & FRAME_FRANKENSTEIN;
break;
case MPG123_BUFFERFILL:
#ifndef NO_FEEDER
{
size_t sval = bc_fill(&mh->rdat.buffer);
theval = (long)sval;
if(theval < 0 || (size_t)theval != sval)
{
mh->err = MPG123_INT_OVERFLOW;
ret = MPG123_ERR;
}
}
#else
mh->err = MPG123_MISSING_FEATURE;
ret = MPG123_ERR;
#endif
break;
case MPG123_FRESH_DECODER:
theval = mh->state_flags & FRAME_FRESH_DECODER;
mh->state_flags &= ~FRAME_FRESH_DECODER;
break;
default:
mh->err = MPG123_BAD_KEY;
ret = MPG123_ERR;
}
if(val != NULL) *val = theval;
if(fval != NULL) *fval = thefval;
return ret;
}
int attribute_align_arg mpg123_eq(mpg123_handle *mh, enum mpg123_channels channel, int band, double val)
{
#ifndef NO_EQUALIZER
if(mh == NULL) return MPG123_BAD_HANDLE;
if(band < 0 || band > 31){ mh->err = MPG123_BAD_BAND; return MPG123_ERR; }
switch(channel)
{
case MPG123_LEFT|MPG123_RIGHT:
mh->equalizer[0][band] = mh->equalizer[1][band] = DOUBLE_TO_REAL(val);
break;
case MPG123_LEFT: mh->equalizer[0][band] = DOUBLE_TO_REAL(val); break;
case MPG123_RIGHT: mh->equalizer[1][band] = DOUBLE_TO_REAL(val); break;
default:
mh->err=MPG123_BAD_CHANNEL;
return MPG123_ERR;
}
mh->have_eq_settings = TRUE;
#endif
return MPG123_OK;
}
double attribute_align_arg mpg123_geteq(mpg123_handle *mh, enum mpg123_channels channel, int band)
{
double ret = 0.;
#ifndef NO_EQUALIZER
/* Handle this gracefully. When there is no band, it has no volume. */
if(mh != NULL && band > -1 && band < 32)
switch(channel)
{
case MPG123_LEFT|MPG123_RIGHT:
ret = 0.5*(REAL_TO_DOUBLE(mh->equalizer[0][band])+REAL_TO_DOUBLE(mh->equalizer[1][band]));
break;
case MPG123_LEFT: ret = REAL_TO_DOUBLE(mh->equalizer[0][band]); break;
case MPG123_RIGHT: ret = REAL_TO_DOUBLE(mh->equalizer[1][band]); break;
/* Default case is already handled: ret = 0 */
}
#endif
return ret;
}
/* plain file access, no http! */
int attribute_align_arg mpg123_open(mpg123_handle *mh, const char *path)
{
if(mh == NULL) return MPG123_BAD_HANDLE;
mpg123_close(mh);
return open_stream(mh, path, -1);
}
int attribute_align_arg mpg123_open_fd(mpg123_handle *mh, int fd)
{
if(mh == NULL) return MPG123_BAD_HANDLE;
mpg123_close(mh);
return open_stream(mh, NULL, fd);
}
int attribute_align_arg mpg123_open_handle(mpg123_handle *mh, void *iohandle)
{
if(mh == NULL) return MPG123_BAD_HANDLE;
mpg123_close(mh);
if(mh->rdat.r_read_handle == NULL)
{
mh->err = MPG123_BAD_CUSTOM_IO;
return MPG123_ERR;
}
return open_stream_handle(mh, iohandle);
}
int attribute_align_arg mpg123_open_feed(mpg123_handle *mh)
{
if(mh == NULL) return MPG123_BAD_HANDLE;
mpg123_close(mh);
return open_feed(mh);
}
int attribute_align_arg mpg123_replace_reader( mpg123_handle *mh,
ssize_t (*r_read) (int, void *, size_t),
off_t (*r_lseek)(int, off_t, int) )
{
if(mh == NULL) return MPG123_BAD_HANDLE;
mpg123_close(mh);
mh->rdat.r_read = r_read;
mh->rdat.r_lseek = r_lseek;
return MPG123_OK;
}
int attribute_align_arg 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*) )
{
if(mh == NULL) return MPG123_BAD_HANDLE;
mpg123_close(mh);
mh->rdat.r_read_handle = r_read;
mh->rdat.r_lseek_handle = r_lseek;
mh->rdat.cleanup_handle = cleanup;
return MPG123_OK;
}
/* Update decoding engine for
a) a new choice of decoder
b) a changed native format of the MPEG stream
... calls are only valid after parsing some MPEG frame! */
int decode_update(mpg123_handle *mh)
{
long native_rate;
int b;
if(mh->num < 0)
{
if(!(mh->p.flags & MPG123_QUIET)) error("decode_update() has been called before reading the first MPEG frame! Internal programming error.");
mh->err = MPG123_BAD_DECODER_SETUP;
return MPG123_ERR;
}
mh->state_flags |= FRAME_FRESH_DECODER;
native_rate = frame_freq(mh);
b = frame_output_format(mh); /* Select the new output format based on given constraints. */
if(b < 0) return MPG123_ERR;
if(b == 1) mh->new_format = 1; /* Store for later... */
debug3("updating decoder structure with native rate %li and af.rate %li (new format: %i)", native_rate, mh->af.rate, mh->new_format);
if(mh->af.rate == native_rate) mh->down_sample = 0;
else if(mh->af.rate == native_rate>>1) mh->down_sample = 1;
else if(mh->af.rate == native_rate>>2) mh->down_sample = 2;
else mh->down_sample = 3; /* flexible (fixed) rate */
switch(mh->down_sample)
{
case 0:
case 1:
case 2:
mh->down_sample_sblimit = SBLIMIT>>(mh->down_sample);
/* With downsampling I get less samples per frame */
mh->outblock = outblock_bytes(mh, (mh->spf>>mh->down_sample));
break;
#ifndef NO_NTOM
case 3:
{
if(synth_ntom_set_step(mh) != 0) return -1;
if(frame_freq(mh) > mh->af.rate)
{
mh->down_sample_sblimit = SBLIMIT * mh->af.rate;
mh->down_sample_sblimit /= frame_freq(mh);
}
else mh->down_sample_sblimit = SBLIMIT;
mh->outblock = outblock_bytes(mh,
( ( NTOM_MUL-1+mh->spf
* (((size_t)NTOM_MUL*mh->af.rate)/frame_freq(mh))
)/NTOM_MUL ));
}
break;
#endif
}
if(!(mh->p.flags & MPG123_FORCE_MONO))
{
if(mh->af.channels == 1) mh->single = SINGLE_MIX;
else mh->single = SINGLE_STEREO;
}
else mh->single = (mh->p.flags & MPG123_FORCE_MONO)-1;
if(set_synth_functions(mh) != 0) return -1;;
/* The needed size of output buffer may have changed. */
if(frame_outbuffer(mh) != MPG123_OK) return -1;
do_rva(mh);
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);
return 0;
}
size_t attribute_align_arg mpg123_safe_buffer(void)
{
/* real is the largest possible output (it's 32bit float, 32bit int or 64bit double). */
return sizeof(real)*2*1152*NTOM_MAX;
}
size_t attribute_align_arg mpg123_outblock(mpg123_handle *mh)
{
/* Try to be helpful and never return zero output block size. */
if(mh != NULL && mh->outblock > 0) return mh->outblock;
else return mpg123_safe_buffer();
}
/* Read in the next frame we actually want for decoding.
This includes skipping/ignoring frames, in additon to skipping junk in the parser. */
static int get_next_frame(mpg123_handle *mh)
{
int change = mh->decoder_change;
/* Ensure we got proper decoder for ignoring frames.
Header can be changed from seeking around. But be careful: Only after at
least one frame got read, decoder update makes sense. */
if(mh->header_change > 1 && mh->num >= 0)
{
change = 1;
mh->header_change = 0;
debug("starting with big header change");
if(decode_update(mh) < 0)
return MPG123_ERR;
}
do
{
int b;
/* Decode & discard some frame(s) before beginning. */
if(mh->to_ignore && mh->num < mh->firstframe && mh->num >= mh->ignoreframe)
{
debug1("ignoring frame %li", (long)mh->num);
/* Decoder structure must be current! decode_update has been called before... */
(mh->do_layer)(mh); mh->buffer.fill = 0;
#ifndef NO_NTOM
/* The ignored decoding may have failed. Make sure ntom stays consistent. */
if(mh->down_sample == 3) ntom_set_ntom(mh, mh->num+1);
#endif
mh->to_ignore = mh->to_decode = FALSE;
}
/* Read new frame data; possibly breaking out here for MPG123_NEED_MORE. */
debug("read frame");
mh->to_decode = FALSE;
b = read_frame(mh); /* That sets to_decode only if a full frame was read. */
debug4("read of frame %li returned %i (to_decode=%i) at sample %li", (long)mh->num, b, mh->to_decode, (long)mpg123_tell(mh));
if(b == MPG123_NEED_MORE) return MPG123_NEED_MORE; /* need another call with data */
else if(b <= 0)
{
/* More sophisticated error control? */
if(b==0 || (mh->rdat.filelen >= 0 && mh->rdat.filepos == mh->rdat.filelen))
{ /* We simply reached the end. */
mh->track_frames = mh->num + 1;
debug("What about updating/checking gapless sample count here?");
return MPG123_DONE;
}
else return MPG123_ERR; /* Some real error. */
}
/* Now, there should be new data to decode ... and also possibly new stream properties */
if(mh->header_change > 1)
{
debug("big header change");
change = 1;
mh->header_change = 0;
/* Need to update decoder structure right away since frame might need to
be decoded on next loop iteration for properly ignoring its output. */
if(decode_update(mh) < 0)
return MPG123_ERR;
}
/* Now some accounting: Look at the numbers and decide if we want this frame. */
++mh->playnum;
/* Plain skipping without decoding, only when frame is not ignored on next cycle. */
if(mh->num < mh->firstframe || (mh->p.doublespeed && (mh->playnum % mh->p.doublespeed)))
{
if(!(mh->to_ignore && mh->num < mh->firstframe && mh->num >= mh->ignoreframe))
{
frame_skip(mh);
/* Should one fix NtoM here or not?
It is not work the trouble for doublespeed, but what with leading frames? */
}
}
/* Or, we are finally done and have a new frame. */
else break;
} while(1);
/* If we reach this point, we got a new frame ready to be decoded.
All other situations resulted in returns from the loop. */
if(change)
{
mh->decoder_change = 0;
if(mh->fresh)
{
#ifdef GAPLESS
int b=0;
/* Prepare offsets for gapless decoding. */
debug1("preparing gapless stuff with native rate %li", frame_freq(mh));
frame_gapless_realinit(mh);
frame_set_frameseek(mh, mh->num);
#endif
mh->fresh = 0;
#ifdef GAPLESS
/* Could this possibly happen? With a real big gapless offset... */
if(mh->num < mh->firstframe) b = get_next_frame(mh);
if(b < 0) return b; /* Could be error, need for more, new format... */
#endif
}
}
return MPG123_OK;
}
/* Assumption: A buffer full of zero samples can be constructed by repetition of this byte.
Oh, and it handles some format conversion.
Only to be used by decode_the_frame() ... */
static int zero_byte(mpg123_handle *fr)
{
#ifndef NO_8BIT
return fr->af.encoding & MPG123_ENC_8 ? fr->conv16to8[0] : 0;
#else
return 0; /* All normal signed formats have the zero here (even in byte form -- that may be an assumption for your funny machine...). */
#endif
}
/*
Not part of the api. This just decodes the frame and fills missing bits with zeroes.
There can be frames that are broken and thus make do_layer() fail.
*/
static void decode_the_frame(mpg123_handle *fr)
{
size_t needed_bytes = decoder_synth_bytes(fr, frame_expect_outsamples(fr));
fr->clip += (fr->do_layer)(fr);
/*fprintf(stderr, "frame %"OFF_P": got %"SIZE_P" / %"SIZE_P"\n", fr->num,(size_p)fr->buffer.fill, (size_p)needed_bytes);*/
/* There could be less data than promised.
Also, then debugging, we look out for coding errors that could result in _more_ data than expected. */
#ifdef DEBUG
if(fr->buffer.fill != needed_bytes)
{
#endif
if(fr->buffer.fill < needed_bytes)
{
if(VERBOSE2)
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);
/*
One could do a loop with individual samples instead... but zero is zero
Actually, that is wrong: zero is mostly a series of null bytes,
but we have funny 8bit formats that have a different opinion on zero...
Unsigned 16 or 32 bit formats are handled later.
*/
memset( fr->buffer.data + fr->buffer.fill, zero_byte(fr), needed_bytes - fr->buffer.fill );
fr->buffer.fill = needed_bytes;
#ifndef NO_NTOM
/* ntom_val will be wrong when the decoding wasn't carried out completely */
ntom_set_ntom(fr, fr->num+1);
#endif
}
#ifdef DEBUG
else
{
if(NOQUIET)
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);
}
}
#endif
postprocess_buffer(fr);
}
/*
Decode the current frame into the frame structure's buffer, accessible at the location stored in <audio>, with <bytes> bytes available.
<num> will contain the last decoded frame number. This function should be called after mpg123_framebyframe_next positioned the stream at a
valid mp3 frame. The buffer contents will get lost on the next call to mpg123_framebyframe_next or mpg123_framebyframe_decode.
returns
MPG123_OK -- successfully decoded or ignored the frame, you get your output data or in case of ignored frames 0 bytes
MPG123_DONE -- decoding finished, should not happen
MPG123_ERR -- some error occured.
MPG123_ERR_NULL -- audio or bytes are not pointing to valid storage addresses
MPG123_BAD_HANDLE -- mh has not been initialized
MPG123_NO_SPACE -- not enough space in buffer for safe decoding, should not happen
*/
int attribute_align_arg mpg123_framebyframe_decode(mpg123_handle *mh, off_t *num, unsigned char **audio, size_t *bytes)
{
if(bytes == NULL) return MPG123_ERR_NULL;
if(audio == NULL) return MPG123_ERR_NULL;
if(mh == NULL) return MPG123_BAD_HANDLE;
if(mh->buffer.size < mh->outblock) return MPG123_NO_SPACE;
*bytes = 0;
mh->buffer.fill = 0; /* always start fresh */
if(!mh->to_decode) return MPG123_OK;
if(num != NULL) *num = mh->num;
debug("decoding");
decode_the_frame(mh);
mh->to_decode = mh->to_ignore = FALSE;
mh->buffer.p = mh->buffer.data;
FRAME_BUFFERCHECK(mh);
*audio = mh->buffer.p;
*bytes = mh->buffer.fill;
return MPG123_OK;
}
/*
Find, read and parse the next mp3 frame while skipping junk and parsing id3 tags, lame headers, etc.
Prepares everything for decoding using mpg123_framebyframe_decode.
returns
MPG123_OK -- new frame was read and parsed, call mpg123_framebyframe_decode to actually decode
MPG123_NEW_FORMAT -- new frame was read, it results in changed output format, call mpg123_framebyframe_decode to actually decode
MPG123_BAD_HANDLE -- mh has not been initialized
MPG123_NEED_MORE -- more input data is needed to advance to the next frame. supply more input data using mpg123_feed
*/
int attribute_align_arg mpg123_framebyframe_next(mpg123_handle *mh)
{
int b;
if(mh == NULL) return MPG123_BAD_HANDLE;
mh->to_decode = mh->to_ignore = FALSE;
mh->buffer.fill = 0;
b = get_next_frame(mh);
if(b < 0) return b;
debug1("got next frame, %i", mh->to_decode);
/* mpg123_framebyframe_decode will return MPG123_OK with 0 bytes decoded if mh->to_decode is 0 */
if(!mh->to_decode)
return MPG123_OK;
if(mh->new_format)
{
debug("notifiying new format");
mh->new_format = 0;
return MPG123_NEW_FORMAT;
}
return MPG123_OK;
}
/*
Put _one_ decoded frame into the frame structure's buffer, accessible at the location stored in <audio>, with <bytes> bytes available.
The buffer contents will be lost on next call to mpg123_decode_frame.
MPG123_OK -- successfully decoded the frame, you get your output data
MPg123_DONE -- This is it. End.
MPG123_ERR -- some error occured...
MPG123_NEW_FORMAT -- new frame was read, it results in changed output format -> will be decoded on next call
MPG123_NEED_MORE -- that should not happen as this function is intended for in-library stream reader but if you force it...
MPG123_NO_SPACE -- not enough space in buffer for safe decoding, also should not happen
num will be updated to the last decoded frame number (may possibly _not_ increase, p.ex. when format changed).
*/
int attribute_align_arg mpg123_decode_frame(mpg123_handle *mh, off_t *num, unsigned char **audio, size_t *bytes)
{
if(bytes != NULL) *bytes = 0;
if(mh == NULL) return MPG123_BAD_HANDLE;
if(mh->buffer.size < mh->outblock) return MPG123_NO_SPACE;
mh->buffer.fill = 0; /* always start fresh */
while(TRUE)
{
/* decode if possible */
if(mh->to_decode)
{
if(mh->new_format)
{
debug("notifiying new format");
mh->new_format = 0;
return MPG123_NEW_FORMAT;
}
if(num != NULL) *num = mh->num;
debug("decoding");
decode_the_frame(mh);
mh->to_decode = mh->to_ignore = FALSE;
mh->buffer.p = mh->buffer.data;
FRAME_BUFFERCHECK(mh);
if(audio != NULL) *audio = mh->buffer.p;
if(bytes != NULL) *bytes = mh->buffer.fill;
return MPG123_OK;
}
else
{
int b = get_next_frame(mh);
if(b < 0) return b;
debug1("got next frame, %i", mh->to_decode);
}
}
}
int attribute_align_arg mpg123_read(mpg123_handle *mh, unsigned char *out, size_t size, size_t *done)
{
return mpg123_decode(mh, NULL, 0, out, size, done);
}
int attribute_align_arg mpg123_feed(mpg123_handle *mh, const unsigned char *in, size_t size)
{
if(mh == NULL) return MPG123_BAD_HANDLE;
#ifndef NO_FEEDER
if(size > 0)
{
if(in != NULL)
{
if(feed_more(mh, in, size) != 0) return MPG123_ERR;
else
{
/* The need for more data might have triggered an error.
This one is outdated now with the new data. */
if(mh->err == MPG123_ERR_READER) mh->err = MPG123_OK;
return MPG123_OK;
}
}
else
{
mh->err = MPG123_NULL_BUFFER;
return MPG123_ERR;
}
}
return MPG123_OK;
#else
mh->err = MPG123_MISSING_FEATURE;
return MPG123_ERR;
#endif
}
/*
The old picture:
while(1) {
len = read(0,buf,16384);
if(len <= 0)
break;
ret = decodeMP3(&mp,buf,len,out,8192,&size);
while(ret == MP3_OK) {
write(1,out,size);
ret = decodeMP3(&mp,NULL,0,out,8192,&size);
}
}
*/
int attribute_align_arg mpg123_decode(mpg123_handle *mh, const unsigned char *inmemory, size_t inmemsize, unsigned char *outmemory, size_t outmemsize, size_t *done)
{
int ret = MPG123_OK;
size_t mdone = 0;
if(done != NULL) *done = 0;
if(mh == NULL) return MPG123_BAD_HANDLE;
#ifndef NO_FEEDER
if(inmemsize > 0 && mpg123_feed(mh, inmemory, inmemsize) != MPG123_OK)
{
ret = MPG123_ERR;
goto decodeend;
}
if(outmemory == NULL) outmemsize = 0; /* Not just give error, give chance to get a status message. */
while(ret == MPG123_OK)
{
debug4("decode loop, fill %i (%li vs. %li); to_decode: %i", (int)mh->buffer.fill, (long)mh->num, (long)mh->firstframe, mh->to_decode);
/* Decode a frame that has been read before.
This only happens when buffer is empty! */
if(mh->to_decode)
{
if(mh->new_format)
{
debug("notifiying new format");
mh->new_format = 0;
ret = MPG123_NEW_FORMAT;
goto decodeend;
}
if(mh->buffer.size - mh->buffer.fill < mh->outblock)
{
ret = MPG123_NO_SPACE;
goto decodeend;
}
decode_the_frame(mh);
mh->to_decode = mh->to_ignore = FALSE;
mh->buffer.p = mh->buffer.data;
debug2("decoded frame %li, got %li samples in buffer", (long)mh->num, (long)(mh->buffer.fill / (samples_to_bytes(mh, 1))));
FRAME_BUFFERCHECK(mh);
}
if(mh->buffer.fill) /* Copy (part of) the decoded data to the caller's buffer. */
{
/* get what is needed - or just what is there */
int a = mh->buffer.fill > (outmemsize - mdone) ? outmemsize - mdone : mh->buffer.fill;
debug4("buffer fill: %i; copying %i (%i - %li)", (int)mh->buffer.fill, a, (int)outmemsize, (long)mdone);
memcpy(outmemory, mh->buffer.p, a);
/* less data in frame buffer, less needed, output pointer increase, more data given... */
mh->buffer.fill -= a;
outmemory += a;
mdone += a;
mh->buffer.p += a;
if(!(outmemsize > mdone)) goto decodeend;
}
else /* If we didn't have data, get a new frame. */
{
int b = get_next_frame(mh);
if(b < 0){ ret = b; goto decodeend; }
}
}
decodeend:
if(done != NULL) *done = mdone;
return ret;
#else
mh->err = MPG123_MISSING_FEATURE;
return MPG123_ERR;
#endif
}
long attribute_align_arg mpg123_clip(mpg123_handle *mh)
{
long ret = 0;
if(mh != NULL)
{
ret = mh->clip;
mh->clip = 0;
}
return ret;
}
/* Simples: Track needs initializtion if no initial frame has been read yet. */
#define track_need_init(mh) ((mh)->num < 0)
static int init_track(mpg123_handle *mh)
{
if(track_need_init(mh))
{
/* Fresh track, need first frame for basic info. */
int b = get_next_frame(mh);
if(b < 0) return b;
}
return 0;
}
int attribute_align_arg mpg123_info(mpg123_handle *mh, struct mpg123_frameinfo *mi)