1 /*
2 frame: Heap of routines dealing with the core mpg123 data structure.
3
4 copyright 2008-2020 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written by Thomas Orgis
7 */
8
9 #include "mpg123lib_intern.h"
10 #include "getcpuflags.h"
11 #include "debug.h"
12
13 static void frame_fixed_reset(mpg123_handle *fr);
14
15 /* that's doubled in decode_ntom.c */
16 #define NTOM_MUL (32768)
17
18 #define aligned_pointer(p, type, alignment) align_the_pointer(p, alignment)
align_the_pointer(void * base,unsigned int alignment)19 static void *align_the_pointer(void *base, unsigned int alignment)
20 {
21 /*
22 Work in unsigned integer realm, explicitly.
23 Tricking the compiler into integer operations like % by invoking base-NULL is dangerous: It results into ptrdiff_t, which gets negative on big addresses. Big screw up, that.
24 I try to do it "properly" here: Casting only to uintptr_t and no artihmethic with void*.
25 */
26 uintptr_t baseval = (uintptr_t)(char*)base;
27 uintptr_t aoff = baseval % alignment;
28
29 debug3("align_the_pointer: pointer %p is off by %u from %u",
30 base, (unsigned int)aoff, alignment);
31
32 if(aoff) return (char*)base+alignment-aoff;
33 else return base;
34 }
35
frame_default_pars(mpg123_pars * mp)36 static void frame_default_pars(mpg123_pars *mp)
37 {
38 mp->outscale = 1.0;
39 mp->flags = 0;
40 #ifdef GAPLESS
41 mp->flags |= MPG123_GAPLESS;
42 #endif
43 mp->flags |= MPG123_AUTO_RESAMPLE|MPG123_FLOAT_FALLBACK;
44 #ifndef NO_NTOM
45 mp->force_rate = 0;
46 #endif
47 mp->down_sample = 0;
48 mp->rva = 0;
49 mp->halfspeed = 0;
50 mp->doublespeed = 0;
51 mp->verbose = 0;
52 #ifndef NO_ICY
53 mp->icy_interval = 0;
54 #endif
55 mp->timeout = 0;
56 mp->resync_limit = 1024;
57 #ifdef FRAME_INDEX
58 mp->index_size = INDEX_SIZE;
59 #endif
60 mp->preframes = 4; /* That's good for layer 3 ISO compliance bitstream. */
61 mpg123_fmt_all(mp);
62 /* Default of keeping some 4K buffers at hand, should cover the "usual" use case (using 16K pipe buffers as role model). */
63 #ifndef NO_FEEDER
64 mp->feedpool = 5;
65 mp->feedbuffer = 4096;
66 #endif
67 mp->freeformat_framesize = -1;
68 }
69
frame_init(mpg123_handle * fr)70 void frame_init(mpg123_handle *fr)
71 {
72 frame_init_par(fr, NULL);
73 }
74
frame_init_par(mpg123_handle * fr,mpg123_pars * mp)75 void frame_init_par(mpg123_handle *fr, mpg123_pars *mp)
76 {
77 fr->own_buffer = TRUE;
78 fr->buffer.data = NULL;
79 fr->buffer.rdata = NULL;
80 fr->buffer.fill = 0;
81 fr->buffer.size = 0;
82 fr->rawbuffs = NULL;
83 fr->rawbuffss = 0;
84 fr->rawdecwin = NULL;
85 fr->rawdecwins = 0;
86 #ifndef NO_8BIT
87 fr->conv16to8_buf = NULL;
88 #endif
89 #ifdef OPT_DITHER
90 fr->dithernoise = NULL;
91 #endif
92 fr->layerscratch = NULL;
93 fr->xing_toc = NULL;
94 fr->cpu_opts.type = defdec();
95 fr->cpu_opts.class = decclass(fr->cpu_opts.type);
96 #ifndef NO_NTOM
97 /* these two look unnecessary, check guarantee for synth_ntom_set_step (in control_generic, even)! */
98 fr->ntom_val[0] = NTOM_MUL>>1;
99 fr->ntom_val[1] = NTOM_MUL>>1;
100 fr->ntom_step = NTOM_MUL;
101 #endif
102 /* unnecessary: fr->buffer.size = fr->buffer.fill = 0; */
103 mpg123_reset_eq(fr);
104 init_icy(&fr->icy);
105 init_id3(fr);
106 /* frame_outbuffer is missing... */
107 /* frame_buffers is missing... that one needs cpu opt setting! */
108 /* after these... frame_reset is needed before starting full decode */
109 invalidate_format(&fr->af);
110 fr->rdat.r_read = NULL;
111 fr->rdat.r_lseek = NULL;
112 fr->rdat.iohandle = NULL;
113 fr->rdat.r_read_handle = NULL;
114 fr->rdat.r_lseek_handle = NULL;
115 fr->rdat.cleanup_handle = NULL;
116 fr->wrapperdata = NULL;
117 fr->wrapperclean = NULL;
118 fr->decoder_change = 1;
119 fr->err = MPG123_OK;
120 if(mp == NULL) frame_default_pars(&fr->p);
121 else memcpy(&fr->p, mp, sizeof(struct mpg123_pars_struct));
122
123 #ifndef NO_FEEDER
124 bc_prepare(&fr->rdat.buffer, fr->p.feedpool, fr->p.feedbuffer);
125 #endif
126
127 fr->down_sample = 0; /* Initialize to silence harmless errors when debugging. */
128 fr->id3v2_raw = NULL;
129 frame_fixed_reset(fr); /* Reset only the fixed data, dynamic buffers are not there yet! */
130 fr->synth = NULL;
131 fr->synth_mono = NULL;
132 fr->make_decode_tables = NULL;
133 #ifdef FRAME_INDEX
134 fi_init(&fr->index);
135 frame_index_setup(fr); /* Apply the size setting. */
136 #endif
137 #ifndef NO_MOREINFO
138 fr->pinfo = NULL;
139 #endif
140 }
141
142 #ifdef OPT_DITHER
143 /* Also, only allocate the memory for the table on demand.
144 In future, one could create special noise for different sampling frequencies(?). */
frame_dither_init(mpg123_handle * fr)145 int frame_dither_init(mpg123_handle *fr)
146 {
147 /* run-time dither noise table generation */
148 if(fr->dithernoise == NULL)
149 {
150 fr->dithernoise = malloc(sizeof(float)*DITHERSIZE);
151 if(fr->dithernoise == NULL) return 0;
152
153 dither_table_init(fr->dithernoise);
154 }
155 return 1;
156 }
157 #endif
158
mpg123_new_pars(int * error)159 mpg123_pars attribute_align_arg *mpg123_new_pars(int *error)
160 {
161 mpg123_pars *mp = malloc(sizeof(struct mpg123_pars_struct));
162 if(mp != NULL){ frame_default_pars(mp); if(error != NULL) *error = MPG123_OK; }
163 else if(error != NULL) *error = MPG123_OUT_OF_MEM;
164 return mp;
165 }
166
mpg123_delete_pars(mpg123_pars * mp)167 void attribute_align_arg mpg123_delete_pars(mpg123_pars* mp)
168 {
169 if(mp != NULL) free(mp);
170 }
171
mpg123_reset_eq(mpg123_handle * mh)172 int attribute_align_arg mpg123_reset_eq(mpg123_handle *mh)
173 {
174 int i;
175 if(mh == NULL) return MPG123_BAD_HANDLE;
176 #ifndef NO_EQUALIZER
177 mh->have_eq_settings = 0;
178 for(i=0; i < 32; ++i) mh->equalizer[0][i] = mh->equalizer[1][i] = DOUBLE_TO_REAL(1.0);
179 #endif
180 return MPG123_OK;
181 }
182
frame_outbuffer(mpg123_handle * fr)183 int frame_outbuffer(mpg123_handle *fr)
184 {
185 size_t size = fr->outblock;
186 if(!fr->own_buffer)
187 {
188 if(fr->buffer.size < size)
189 {
190 fr->err = MPG123_BAD_BUFFER;
191 if(NOQUIET)
192 merror( "have external buffer of size %"SIZE_P", need %"SIZE_P
193 , (size_p)fr->buffer.size, (size_p)size );
194 return MPG123_ERR;
195 }
196 }
197
198 debug1("need frame buffer of %"SIZE_P, (size_p)size);
199 if(fr->buffer.rdata != NULL && fr->buffer.size != size)
200 {
201 free(fr->buffer.rdata);
202 fr->buffer.rdata = NULL;
203 }
204 fr->buffer.size = size;
205 fr->buffer.data = NULL;
206 /* be generous: use 16 byte alignment */
207 if(fr->buffer.rdata == NULL) fr->buffer.rdata = (unsigned char*) malloc(fr->buffer.size+15);
208 if(fr->buffer.rdata == NULL)
209 {
210 fr->err = MPG123_OUT_OF_MEM;
211 return MPG123_ERR;
212 }
213 fr->buffer.data = aligned_pointer(fr->buffer.rdata, unsigned char*, 16);
214 fr->own_buffer = TRUE;
215 fr->buffer.fill = 0;
216 return MPG123_OK;
217 }
218
mpg123_replace_buffer(mpg123_handle * mh,void * data,size_t size)219 int attribute_align_arg mpg123_replace_buffer(mpg123_handle *mh, void *data, size_t size)
220 {
221 debug2("replace buffer with %p size %"SIZE_P, data, (size_p)size);
222 if(mh == NULL) return MPG123_BAD_HANDLE;
223 /* Will accept any size, the error comes later... */
224 if(data == NULL)
225 {
226 mh->err = MPG123_BAD_BUFFER;
227 return MPG123_ERR;
228 }
229 if(mh->buffer.rdata != NULL) free(mh->buffer.rdata);
230 mh->own_buffer = FALSE;
231 mh->buffer.rdata = NULL;
232 mh->buffer.data = data;
233 mh->buffer.size = size;
234 mh->buffer.fill = 0;
235 return MPG123_OK;
236 }
237
238 #ifdef FRAME_INDEX
frame_index_setup(mpg123_handle * fr)239 int frame_index_setup(mpg123_handle *fr)
240 {
241 int ret = MPG123_ERR;
242 if(fr->p.index_size >= 0)
243 { /* Simple fixed index. */
244 fr->index.grow_size = 0;
245 ret = fi_resize(&fr->index, (size_t)fr->p.index_size);
246 }
247 else
248 { /* A growing index. We give it a start, though. */
249 fr->index.grow_size = (size_t)(- fr->p.index_size);
250 if(fr->index.size < fr->index.grow_size)
251 ret = fi_resize(&fr->index, fr->index.grow_size);
252 else
253 ret = MPG123_OK; /* We have minimal size already... and since growing is OK... */
254 }
255 debug2("set up frame index of size %lu (ret=%i)", (unsigned long)fr->index.size, ret);
256 if(ret && NOQUIET)
257 error("frame index setup (initial resize) failed");
258 return ret;
259 }
260 #endif
261
frame_decode_buffers_reset(mpg123_handle * fr)262 static void frame_decode_buffers_reset(mpg123_handle *fr)
263 {
264 if(fr->rawbuffs) /* memset(NULL, 0, 0) not desired */
265 memset(fr->rawbuffs, 0, fr->rawbuffss);
266 }
267
frame_buffers(mpg123_handle * fr)268 int frame_buffers(mpg123_handle *fr)
269 {
270 int buffssize = 0;
271 debug1("frame %p buffer", (void*)fr);
272 /*
273 the used-to-be-static buffer of the synth functions, has some subtly different types/sizes
274
275 2to1, 4to1, ntom, generic, i386: real[2][2][0x110]
276 mmx, sse: short[2][2][0x110]
277 i586(_dither): 4352 bytes; int/long[2][2][0x110]
278 i486: int[2][2][17*FIR_BUFFER_SIZE]
279 altivec: static real __attribute__ ((aligned (16))) buffs[4][4][0x110]
280
281 Huh, altivec looks like fun. Well, let it be large... then, the 16 byte alignment seems to be implicit on MacOSX malloc anyway.
282 Let's make a reasonable attempt to allocate enough memory...
283 Keep in mind: biggest ones are i486 and altivec (mutually exclusive!), then follows i586 and normal real.
284 mmx/sse use short but also real for resampling.
285 Thus, minimum is 2*2*0x110*sizeof(real).
286 */
287 if(fr->cpu_opts.type == altivec) buffssize = 4*4*0x110*sizeof(real);
288 #ifdef OPT_I486
289 else if(fr->cpu_opts.type == ivier) buffssize = 2*2*17*FIR_BUFFER_SIZE*sizeof(int);
290 #endif
291 else if(fr->cpu_opts.type == ifuenf || fr->cpu_opts.type == ifuenf_dither || fr->cpu_opts.type == dreidnow)
292 buffssize = 2*2*0x110*4; /* don't rely on type real, we need 4352 bytes */
293
294 if(2*2*0x110*sizeof(real) > buffssize)
295 buffssize = 2*2*0x110*sizeof(real);
296 buffssize += 15; /* For 16-byte alignment (SSE likes that). */
297
298 if(fr->rawbuffs != NULL && fr->rawbuffss != buffssize)
299 {
300 free(fr->rawbuffs);
301 fr->rawbuffs = NULL;
302 }
303
304 if(fr->rawbuffs == NULL) fr->rawbuffs = (unsigned char*) malloc(buffssize);
305 if(fr->rawbuffs == NULL) return -1;
306 fr->rawbuffss = buffssize;
307 fr->short_buffs[0][0] = aligned_pointer(fr->rawbuffs,short,16);
308 fr->short_buffs[0][1] = fr->short_buffs[0][0] + 0x110;
309 fr->short_buffs[1][0] = fr->short_buffs[0][1] + 0x110;
310 fr->short_buffs[1][1] = fr->short_buffs[1][0] + 0x110;
311 fr->real_buffs[0][0] = aligned_pointer(fr->rawbuffs,real,16);
312 fr->real_buffs[0][1] = fr->real_buffs[0][0] + 0x110;
313 fr->real_buffs[1][0] = fr->real_buffs[0][1] + 0x110;
314 fr->real_buffs[1][1] = fr->real_buffs[1][0] + 0x110;
315 #ifdef OPT_I486
316 if(fr->cpu_opts.type == ivier)
317 {
318 fr->int_buffs[0][0] = (int*) fr->rawbuffs;
319 fr->int_buffs[0][1] = fr->int_buffs[0][0] + 17*FIR_BUFFER_SIZE;
320 fr->int_buffs[1][0] = fr->int_buffs[0][1] + 17*FIR_BUFFER_SIZE;
321 fr->int_buffs[1][1] = fr->int_buffs[1][0] + 17*FIR_BUFFER_SIZE;
322 }
323 #endif
324 #ifdef OPT_ALTIVEC
325 if(fr->cpu_opts.type == altivec)
326 {
327 int i,j;
328 fr->areal_buffs[0][0] = (real*) fr->rawbuffs;
329 for(i=0; i<4; ++i) for(j=0; j<4; ++j)
330 fr->areal_buffs[i][j] = fr->areal_buffs[0][0] + (i*4+j)*0x110;
331 }
332 #endif
333 /* now the different decwins... all of the same size, actually */
334 /* The MMX ones want 32byte alignment, which I'll try to ensure manually */
335 {
336 int decwin_size = (512+32)*sizeof(real);
337 #ifdef OPT_MMXORSSE
338 #ifdef OPT_MULTI
339 if(fr->cpu_opts.class == mmxsse)
340 {
341 #endif
342 /* decwin_mmx will share, decwins will be appended ... sizeof(float)==4 */
343 if(decwin_size < (512+32)*4) decwin_size = (512+32)*4;
344
345 /* the second window + alignment zone -- we align for 32 bytes for SSE as
346 requirement, 64 byte for matching cache line size (that matters!) */
347 decwin_size += (512+32)*4 + 63;
348 /* (512+32)*4/32 == 2176/32 == 68, so one decwin block retains alignment for 32 or 64 bytes */
349 #ifdef OPT_MULTI
350 }
351 #endif
352 #endif
353 #if defined(OPT_ALTIVEC) || defined(OPT_ARM)
354 /* sizeof(real) >= 4 ... yes, it could be 8, for example.
355 We got it intialized to at least (512+32)*sizeof(real).*/
356 decwin_size += 512*sizeof(real);
357 #endif
358 /* Hm, that's basically realloc() ... */
359 if(fr->rawdecwin != NULL && fr->rawdecwins != decwin_size)
360 {
361 free(fr->rawdecwin);
362 fr->rawdecwin = NULL;
363 }
364
365 if(fr->rawdecwin == NULL)
366 fr->rawdecwin = (unsigned char*) malloc(decwin_size);
367
368 if(fr->rawdecwin == NULL) return -1;
369
370 fr->rawdecwins = decwin_size;
371 fr->decwin = (real*) fr->rawdecwin;
372 #ifdef OPT_MMXORSSE
373 #ifdef OPT_MULTI
374 if(fr->cpu_opts.class == mmxsse)
375 {
376 #endif
377 /* align decwin, assign that to decwin_mmx, append decwins */
378 /* I need to add to decwin what is missing to the next full 64 byte -- also I want to make gcc -pedantic happy... */
379 fr->decwin = aligned_pointer(fr->rawdecwin,real,64);
380 debug1("aligned decwin: %p", (void*)fr->decwin);
381 fr->decwin_mmx = (float*)fr->decwin;
382 fr->decwins = fr->decwin_mmx+512+32;
383 #ifdef OPT_MULTI
384 }
385 else debug("no decwins/decwin_mmx for that class");
386 #endif
387 #endif
388 }
389
390 /* Layer scratch buffers are of compile-time fixed size, so allocate only once. */
391 if(fr->layerscratch == NULL)
392 {
393 /* Allocate specific layer1/2/3 buffers, so that we know they'll work for SSE. */
394 size_t scratchsize = 0;
395 real *scratcher;
396 #ifndef NO_LAYER1
397 scratchsize += sizeof(real) * 2 * SBLIMIT;
398 #endif
399 #ifndef NO_LAYER2
400 scratchsize += sizeof(real) * 2 * 4 * SBLIMIT;
401 #endif
402 #ifndef NO_LAYER3
403 scratchsize += sizeof(real) * 2 * SBLIMIT * SSLIMIT; /* hybrid_in */
404 scratchsize += sizeof(real) * 2 * SSLIMIT * SBLIMIT; /* hybrid_out */
405 #endif
406 /*
407 Now figure out correct alignment:
408 We need 16 byte minimum, smallest unit of the blocks is 2*SBLIMIT*sizeof(real), which is 64*4=256. Let's do 64bytes as heuristic for cache line (as proven useful in buffs above).
409 */
410 fr->layerscratch = malloc(scratchsize+63);
411 if(fr->layerscratch == NULL) return -1;
412
413 /* Get aligned part of the memory, then divide it up. */
414 scratcher = aligned_pointer(fr->layerscratch,real,64);
415 /* Those funky pointer casts silence compilers...
416 One might change the code at hand to really just use 1D arrays, but in practice, that would not make a (positive) difference. */
417 #ifndef NO_LAYER1
418 fr->layer1.fraction = (real(*)[SBLIMIT])scratcher;
419 scratcher += 2 * SBLIMIT;
420 #endif
421 #ifndef NO_LAYER2
422 fr->layer2.fraction = (real(*)[4][SBLIMIT])scratcher;
423 scratcher += 2 * 4 * SBLIMIT;
424 #endif
425 #ifndef NO_LAYER3
426 fr->layer3.hybrid_in = (real(*)[SBLIMIT][SSLIMIT])scratcher;
427 scratcher += 2 * SBLIMIT * SSLIMIT;
428 fr->layer3.hybrid_out = (real(*)[SSLIMIT][SBLIMIT])scratcher;
429 scratcher += 2 * SSLIMIT * SBLIMIT;
430 #endif
431 /* Note: These buffers don't need resetting here. */
432 }
433
434 /* Only reset the buffers we created just now. */
435 frame_decode_buffers_reset(fr);
436
437 debug1("frame %p buffer done", (void*)fr);
438 return 0;
439 }
440
frame_buffers_reset(mpg123_handle * fr)441 int frame_buffers_reset(mpg123_handle *fr)
442 {
443 fr->buffer.fill = 0; /* hm, reset buffer fill... did we do a flush? */
444 fr->bsnum = 0;
445 /* Wondering: could it be actually _wanted_ to retain buffer contents over different files? (special gapless / cut stuff) */
446 fr->bsbuf = fr->bsspace[1];
447 fr->bsbufold = fr->bsbuf;
448 fr->bitreservoir = 0;
449 frame_decode_buffers_reset(fr);
450 memset(fr->bsspace, 0, 2*(MAXFRAMESIZE+512));
451 memset(fr->ssave, 0, 34);
452 fr->hybrid_blc[0] = fr->hybrid_blc[1] = 0;
453 memset(fr->hybrid_block, 0, sizeof(real)*2*2*SBLIMIT*SSLIMIT);
454 return 0;
455 }
456
frame_icy_reset(mpg123_handle * fr)457 static void frame_icy_reset(mpg123_handle* fr)
458 {
459 #ifndef NO_ICY
460 if(fr->icy.data != NULL) free(fr->icy.data);
461 fr->icy.data = NULL;
462 fr->icy.interval = 0;
463 fr->icy.next = 0;
464 #endif
465 }
466
frame_free_toc(mpg123_handle * fr)467 static void frame_free_toc(mpg123_handle *fr)
468 {
469 if(fr->xing_toc != NULL){ free(fr->xing_toc); fr->xing_toc = NULL; }
470 }
471
472 /* Just copy the Xing TOC over... */
frame_fill_toc(mpg123_handle * fr,unsigned char * in)473 int frame_fill_toc(mpg123_handle *fr, unsigned char* in)
474 {
475 if(fr->xing_toc == NULL) fr->xing_toc = malloc(100);
476 if(fr->xing_toc != NULL)
477 {
478 memcpy(fr->xing_toc, in, 100);
479 #ifdef DEBUG
480 debug("Got a TOC! Showing the values...");
481 {
482 int i;
483 for(i=0; i<100; ++i)
484 debug2("entry %i = %i", i, fr->xing_toc[i]);
485 }
486 #endif
487 return TRUE;
488 }
489 return FALSE;
490 }
491
492 /* Prepare the handle for a new track.
493 Reset variables, buffers... */
frame_reset(mpg123_handle * fr)494 int frame_reset(mpg123_handle* fr)
495 {
496 frame_buffers_reset(fr);
497 frame_fixed_reset(fr);
498 frame_free_toc(fr);
499 #ifdef FRAME_INDEX
500 fi_reset(&fr->index);
501 #endif
502
503 return 0;
504 }
505
506 /* Reset everythign except dynamic memory. */
frame_fixed_reset(mpg123_handle * fr)507 static void frame_fixed_reset(mpg123_handle *fr)
508 {
509 frame_icy_reset(fr);
510 open_bad(fr);
511 fr->to_decode = FALSE;
512 fr->to_ignore = FALSE;
513 fr->metaflags = 0;
514 fr->outblock = 0; /* This will be set before decoding! */
515 fr->num = -1;
516 fr->input_offset = -1;
517 fr->playnum = -1;
518 fr->state_flags = FRAME_ACCURATE;
519 fr->silent_resync = 0;
520 fr->audio_start = 0;
521 fr->clip = 0;
522 fr->oldhead = 0;
523 fr->firsthead = 0;
524 fr->lay = 0;
525 fr->vbr = MPG123_CBR;
526 fr->abr_rate = 0;
527 fr->track_frames = 0;
528 fr->track_samples = -1;
529 fr->framesize=0;
530 fr->mean_frames = 0;
531 fr->mean_framesize = 0;
532 fr->freesize = 0;
533 fr->lastscale = -1;
534 fr->rva.level[0] = -1;
535 fr->rva.level[1] = -1;
536 fr->rva.gain[0] = 0;
537 fr->rva.gain[1] = 0;
538 fr->rva.peak[0] = 0;
539 fr->rva.peak[1] = 0;
540 fr->fsizeold = 0;
541 fr->firstframe = 0;
542 fr->ignoreframe = fr->firstframe-fr->p.preframes;
543 fr->header_change = 0;
544 fr->lastframe = -1;
545 fr->fresh = 1;
546 fr->new_format = 0;
547 #ifdef GAPLESS
548 frame_gapless_init(fr,-1,0,0);
549 fr->lastoff = 0;
550 fr->firstoff = 0;
551 #endif
552 #ifdef OPT_I486
553 fr->i486bo[0] = fr->i486bo[1] = FIR_SIZE-1;
554 #endif
555 fr->bo = 1; /* the usual bo */
556 #ifdef OPT_DITHER
557 fr->ditherindex = 0;
558 #endif
559 reset_id3(fr);
560 reset_icy(&fr->icy);
561 /* ICY stuff should go into icy.c, eh? */
562 #ifndef NO_ICY
563 fr->icy.interval = 0;
564 fr->icy.next = 0;
565 #endif
566 fr->halfphase = 0; /* here or indeed only on first-time init? */
567 fr->error_protection = 0;
568 fr->freeformat_framesize = fr->p.freeformat_framesize;
569 fr->enc_delay = -1;
570 fr->enc_padding = -1;
571 memset(fr->id3buf, 0, sizeof(fr->id3buf));
572 if(fr->id3v2_raw)
573 free(fr->id3v2_raw);
574 fr->id3v2_raw = NULL;
575 fr->id3v2_size = 0;
576 }
577
frame_free_buffers(mpg123_handle * fr)578 static void frame_free_buffers(mpg123_handle *fr)
579 {
580 if(fr->rawbuffs != NULL) free(fr->rawbuffs);
581 fr->rawbuffs = NULL;
582 fr->rawbuffss = 0;
583 if(fr->rawdecwin != NULL) free(fr->rawdecwin);
584 fr->rawdecwin = NULL;
585 fr->rawdecwins = 0;
586 #ifndef NO_8BIT
587 if(fr->conv16to8_buf != NULL) free(fr->conv16to8_buf);
588 fr->conv16to8_buf = NULL;
589 #endif
590 if(fr->layerscratch != NULL) free(fr->layerscratch);
591 }
592
frame_exit(mpg123_handle * fr)593 void frame_exit(mpg123_handle *fr)
594 {
595 if(fr->buffer.rdata != NULL)
596 {
597 debug1("freeing buffer at %p", (void*)fr->buffer.rdata);
598 free(fr->buffer.rdata);
599 }
600 fr->buffer.rdata = NULL;
601 frame_free_buffers(fr);
602 frame_free_toc(fr);
603 #ifdef FRAME_INDEX
604 fi_exit(&fr->index);
605 #endif
606 #ifdef OPT_DITHER
607 if(fr->dithernoise != NULL)
608 {
609 free(fr->dithernoise);
610 fr->dithernoise = NULL;
611 }
612 #endif
613 exit_id3(fr);
614 clear_icy(&fr->icy);
615 /* Clean up possible mess from LFS wrapper. */
616 if(fr->wrapperclean != NULL)
617 {
618 fr->wrapperclean(fr->wrapperdata);
619 fr->wrapperdata = NULL;
620 }
621 #ifndef NO_FEEDER
622 bc_cleanup(&fr->rdat.buffer);
623 #endif
624 }
625
mpg123_framedata(mpg123_handle * mh,unsigned long * header,unsigned char ** bodydata,size_t * bodybytes)626 int attribute_align_arg mpg123_framedata(mpg123_handle *mh, unsigned long *header, unsigned char **bodydata, size_t *bodybytes)
627 {
628 if(mh == NULL) return MPG123_BAD_HANDLE;
629 if(!mh->to_decode) return MPG123_ERR;
630
631 if(header != NULL) *header = mh->oldhead;
632 if(bodydata != NULL) *bodydata = mh->bsbuf;
633 if(bodybytes != NULL) *bodybytes = mh->framesize;
634
635 return MPG123_OK;
636 }
637
mpg123_set_moreinfo(mpg123_handle * mh,struct mpg123_moreinfo * mi)638 int attribute_align_arg mpg123_set_moreinfo( mpg123_handle *mh
639 , struct mpg123_moreinfo *mi)
640 {
641 #ifndef NO_MOREINFO
642 mh->pinfo = mi;
643 return MPG123_OK;
644 #else
645 mh->err = MPG123_MISSING_FEATURE;
646 return MPG123_ERR;
647 #endif
648 }
649
650 /*
651 Fuzzy frame offset searching (guessing).
652 When we don't have an accurate position, we may use an inaccurate one.
653 Possibilities:
654 - use approximate positions from Xing TOC (not yet parsed)
655 - guess wildly from mean framesize and offset of first frame / beginning of file.
656 */
657
frame_fuzzy_find(mpg123_handle * fr,off_t want_frame,off_t * get_frame)658 static off_t frame_fuzzy_find(mpg123_handle *fr, off_t want_frame, off_t* get_frame)
659 {
660 /* Default is to go to the beginning. */
661 off_t ret = fr->audio_start;
662 *get_frame = 0;
663
664 /* But we try to find something better. */
665 /* Xing VBR TOC works with relative positions, both in terms of audio frames and stream bytes.
666 Thus, it only works when whe know the length of things.
667 Oh... I assume the offsets are relative to the _total_ file length. */
668 if(fr->xing_toc != NULL && fr->track_frames > 0 && fr->rdat.filelen > 0)
669 {
670 /* One could round... */
671 int toc_entry = (int) ((double)want_frame*100./fr->track_frames);
672 /* It is an index in the 100-entry table. */
673 if(toc_entry < 0) toc_entry = 0;
674 if(toc_entry > 99) toc_entry = 99;
675
676 /* Now estimate back what frame we get. */
677 *get_frame = (off_t) ((double)toc_entry/100. * fr->track_frames);
678 fr->state_flags &= ~FRAME_ACCURATE;
679 fr->silent_resync = 1;
680 /* Question: Is the TOC for whole file size (with/without ID3) or the "real" audio data only?
681 ID3v1 info could also matter. */
682 ret = (off_t) ((double)fr->xing_toc[toc_entry]/256.* fr->rdat.filelen);
683 }
684 else if(fr->mean_framesize > 0)
685 { /* Just guess with mean framesize (may be exact with CBR files). */
686 /* Query filelen here or not? */
687 fr->state_flags &= ~FRAME_ACCURATE; /* Fuzzy! */
688 fr->silent_resync = 1;
689 *get_frame = want_frame;
690 ret = (off_t) (fr->audio_start+fr->mean_framesize*want_frame);
691 }
692 debug5("fuzzy: want %li of %li, get %li at %li B of %li B",
693 (long)want_frame, (long)fr->track_frames, (long)*get_frame, (long)ret, (long)(fr->rdat.filelen-fr->audio_start));
694 return ret;
695 }
696
697 /*
698 find the best frame in index just before the wanted one, seek to there
699 then step to just before wanted one with read_frame
700 do not care tabout the stuff that was in buffer but not played back
701 everything that left the decoder is counted as played
702
703 Decide if you want low latency reaction and accurate timing info or stable long-time playback with buffer!
704 */
705
frame_index_find(mpg123_handle * fr,off_t want_frame,off_t * get_frame)706 off_t frame_index_find(mpg123_handle *fr, off_t want_frame, off_t* get_frame)
707 {
708 /* default is file start if no index position */
709 off_t gopos = 0;
710 *get_frame = 0;
711 #ifdef FRAME_INDEX
712 /* Possibly use VBRI index, too? I'd need an example for this... */
713 if(fr->index.fill)
714 {
715 /* find in index */
716 size_t fi;
717 /* at index fi there is frame step*fi... */
718 fi = want_frame/fr->index.step;
719 if(fi >= fr->index.fill) /* If we are beyond the end of frame index...*/
720 {
721 /* When fuzzy seek is allowed, we have some limited tolerance for the frames we want to read rather then jump over. */
722 if(fr->p.flags & MPG123_FUZZY && want_frame - (fr->index.fill-1)*fr->index.step > 10)
723 {
724 gopos = frame_fuzzy_find(fr, want_frame, get_frame);
725 if(gopos > fr->audio_start) return gopos; /* Only in that case, we have a useful guess. */
726 /* Else... just continue, fuzzyness didn't help. */
727 }
728 /* Use the last available position, slowly advancing from that one. */
729 fi = fr->index.fill - 1;
730 }
731 /* We have index position, that yields frame and byte offsets. */
732 *get_frame = fi*fr->index.step;
733 gopos = fr->index.data[fi];
734 fr->state_flags |= FRAME_ACCURATE; /* When using the frame index, we are accurate. */
735 }
736 else
737 {
738 #endif
739 if(fr->p.flags & MPG123_FUZZY)
740 return frame_fuzzy_find(fr, want_frame, get_frame);
741 /* A bit hackish here... but we need to be fresh when looking for the first header again. */
742 fr->firsthead = 0;
743 fr->oldhead = 0;
744 #ifdef FRAME_INDEX
745 }
746 #endif
747 debug2("index: 0x%lx for frame %li", (unsigned long)gopos, (long) *get_frame);
748 return gopos;
749 }
750
frame_ins2outs(mpg123_handle * fr,off_t ins)751 off_t frame_ins2outs(mpg123_handle *fr, off_t ins)
752 {
753 off_t outs = 0;
754 switch(fr->down_sample)
755 {
756 case 0:
757 # ifndef NO_DOWNSAMPLE
758 case 1:
759 case 2:
760 # endif
761 outs = ins>>fr->down_sample;
762 break;
763 # ifndef NO_NTOM
764 case 3: outs = ntom_ins2outs(fr, ins); break;
765 # endif
766 default: if(NOQUIET)
767 merror( "Bad down_sample (%i) ... should not be possible!!"
768 , fr->down_sample );
769 }
770 return outs;
771 }
772
frame_outs(mpg123_handle * fr,off_t num)773 off_t frame_outs(mpg123_handle *fr, off_t num)
774 {
775 off_t outs = 0;
776 switch(fr->down_sample)
777 {
778 case 0:
779 # ifndef NO_DOWNSAMPLE
780 case 1:
781 case 2:
782 # endif
783 outs = (fr->spf>>fr->down_sample)*num;
784 break;
785 #ifndef NO_NTOM
786 case 3: outs = ntom_frmouts(fr, num); break;
787 #endif
788 default: if(NOQUIET)
789 merror( "Bad down_sample (%i) ... should not be possible!!"
790 , fr->down_sample );
791 }
792 return outs;
793 }
794
795 /* Compute the number of output samples we expect from this frame.
796 This is either simple spf() or a tad more elaborate for ntom. */
frame_expect_outsamples(mpg123_handle * fr)797 off_t frame_expect_outsamples(mpg123_handle *fr)
798 {
799 off_t outs = 0;
800 switch(fr->down_sample)
801 {
802 case 0:
803 # ifndef NO_DOWNSAMPLE
804 case 1:
805 case 2:
806 # endif
807 outs = fr->spf>>fr->down_sample;
808 break;
809 #ifndef NO_NTOM
810 case 3: outs = ntom_frame_outsamples(fr); break;
811 #endif
812 default: if(NOQUIET)
813 merror( "Bad down_sample (%i) ... should not be possible!!"
814 , fr->down_sample );
815 }
816 return outs;
817 }
818
frame_offset(mpg123_handle * fr,off_t outs)819 off_t frame_offset(mpg123_handle *fr, off_t outs)
820 {
821 off_t num = 0;
822 switch(fr->down_sample)
823 {
824 case 0:
825 # ifndef NO_DOWNSAMPLE
826 case 1:
827 case 2:
828 # endif
829 num = outs/(fr->spf>>fr->down_sample);
830 break;
831 #ifndef NO_NTOM
832 case 3: num = ntom_frameoff(fr, outs); break;
833 #endif
834 default: if(NOQUIET)
835 error("Bad down_sample ... should not be possible!!");
836 }
837 return num;
838 }
839
840 #ifdef GAPLESS
841 /* input in _input_ samples */
frame_gapless_init(mpg123_handle * fr,off_t framecount,off_t bskip,off_t eskip)842 void frame_gapless_init(mpg123_handle *fr, off_t framecount, off_t bskip, off_t eskip)
843 {
844 debug3("frame_gapless_init: given %"OFF_P" frames, skip %"OFF_P" and %"OFF_P, (off_p)framecount, (off_p)bskip, (off_p)eskip);
845 fr->gapless_frames = framecount;
846 if(fr->gapless_frames > 0 && bskip >=0 && eskip >= 0)
847 {
848 fr->begin_s = bskip+GAPLESS_DELAY;
849 fr->end_s = framecount*fr->spf-eskip+GAPLESS_DELAY;
850 }
851 else fr->begin_s = fr->end_s = 0;
852 /* These will get proper values later, from above plus resampling info. */
853 fr->begin_os = 0;
854 fr->end_os = 0;
855 fr->fullend_os = 0;
856 debug2("frame_gapless_init: from %"OFF_P" to %"OFF_P" samples", (off_p)fr->begin_s, (off_p)fr->end_s);
857 }
858
frame_gapless_realinit(mpg123_handle * fr)859 void frame_gapless_realinit(mpg123_handle *fr)
860 {
861 fr->begin_os = frame_ins2outs(fr, fr->begin_s);
862 fr->end_os = frame_ins2outs(fr, fr->end_s);
863 if(fr->gapless_frames > 0)
864 fr->fullend_os = frame_ins2outs(fr, fr->gapless_frames*fr->spf);
865 else fr->fullend_os = 0;
866
867 debug4("frame_gapless_realinit: from %"OFF_P" to %"OFF_P" samples (%"OFF_P", %"OFF_P")", (off_p)fr->begin_os, (off_p)fr->end_os, (off_p)fr->fullend_os, (off_p)fr->gapless_frames);
868 }
869
870 /* At least note when there is trouble... */
frame_gapless_update(mpg123_handle * fr,off_t total_samples)871 void frame_gapless_update(mpg123_handle *fr, off_t total_samples)
872 {
873 off_t gapless_samples = fr->gapless_frames*fr->spf;
874 if(fr->gapless_frames < 1) return;
875
876 debug2("gapless update with new sample count %"OFF_P" as opposed to known %"OFF_P, total_samples, gapless_samples);
877 if(NOQUIET && total_samples != gapless_samples)
878 fprintf(stderr, "\nWarning: Real sample count %"OFF_P" differs from given gapless sample count %"OFF_P". Frankenstein stream?\n"
879 , total_samples, gapless_samples);
880
881 if(gapless_samples > total_samples)
882 {
883 if(NOQUIET)
884 merror( "End sample count smaller than gapless end! (%"OFF_P
885 " < %"OFF_P"). Disabling gapless mode from now on."
886 , (off_p)total_samples, (off_p)fr->end_s );
887 /* This invalidates the current position... but what should I do? */
888 frame_gapless_init(fr, -1, 0, 0);
889 frame_gapless_realinit(fr);
890 fr->lastframe = -1;
891 fr->lastoff = 0;
892 }
893 }
894
895 #endif
896
897 /* Compute the needed frame to ignore from, for getting accurate/consistent output for intended firstframe. */
ignoreframe(mpg123_handle * fr)898 static off_t ignoreframe(mpg123_handle *fr)
899 {
900 off_t preshift = fr->p.preframes;
901 /* Layer 3 _really_ needs at least one frame before. */
902 if(fr->lay==3 && preshift < 1) preshift = 1;
903 /* Layer 1 & 2 reall do not need more than 2. */
904 if(fr->lay!=3 && preshift > 2) preshift = 2;
905
906 return fr->firstframe - preshift;
907 }
908
909 /* The frame seek... This is not simply the seek to fe*fr->spf samples in output because we think of _input_ frames here.
910 Seek to frame offset 1 may be just seek to 200 samples offset in output since the beginning of first frame is delay/padding.
911 Hm, is that right? OK for the padding stuff, but actually, should the decoder delay be better totally hidden or not?
912 With gapless, even the whole frame position could be advanced further than requested (since Homey don't play dat). */
frame_set_frameseek(mpg123_handle * fr,off_t fe)913 void frame_set_frameseek(mpg123_handle *fr, off_t fe)
914 {
915 fr->firstframe = fe;
916 #ifdef GAPLESS
917 if(fr->p.flags & MPG123_GAPLESS && fr->gapless_frames > 0)
918 {
919 /* Take care of the beginning... */
920 off_t beg_f = frame_offset(fr, fr->begin_os);
921 if(fe <= beg_f)
922 {
923 fr->firstframe = beg_f;
924 fr->firstoff = fr->begin_os - frame_outs(fr, beg_f);
925 }
926 else fr->firstoff = 0;
927 /* The end is set once for a track at least, on the frame_set_frameseek called in get_next_frame() */
928 if(fr->end_os > 0)
929 {
930 fr->lastframe = frame_offset(fr,fr->end_os);
931 fr->lastoff = fr->end_os - frame_outs(fr, fr->lastframe);
932 } else {fr->lastframe = -1; fr->lastoff = 0; }
933 } else { fr->firstoff = fr->lastoff = 0; fr->lastframe = -1; }
934 #endif
935 fr->ignoreframe = ignoreframe(fr);
936 #ifdef GAPLESS
937 debug5("frame_set_frameseek: begin at %li frames and %li samples, end at %li and %li; ignore from %li",
938 (long) fr->firstframe, (long) fr->firstoff,
939 (long) fr->lastframe, (long) fr->lastoff, (long) fr->ignoreframe);
940 #else
941 debug3("frame_set_frameseek: begin at %li frames, end at %li; ignore from %li",
942 (long) fr->firstframe, (long) fr->lastframe, (long) fr->ignoreframe);
943 #endif
944 }
945
frame_skip(mpg123_handle * fr)946 void frame_skip(mpg123_handle *fr)
947 {
948 #ifndef NO_LAYER3
949 if(fr->lay == 3) set_pointer(fr, 1, 512);
950 #endif
951 }
952
953 /* Sample accurate seek prepare for decoder. */
954 /* This gets unadjusted output samples and takes resampling into account */
frame_set_seek(mpg123_handle * fr,off_t sp)955 void frame_set_seek(mpg123_handle *fr, off_t sp)
956 {
957 fr->firstframe = frame_offset(fr, sp);
958 debug1("frame_set_seek: from %"OFF_P, fr->num);
959 #ifndef NO_NTOM
960 if(fr->down_sample == 3) ntom_set_ntom(fr, fr->firstframe);
961 #endif
962 fr->ignoreframe = ignoreframe(fr);
963 #ifdef GAPLESS /* The sample offset is used for non-gapless mode, too! */
964 fr->firstoff = sp - frame_outs(fr, fr->firstframe);
965 debug5("frame_set_seek: begin at %li frames and %li samples, end at %li and %li; ignore from %li",
966 (long) fr->firstframe, (long) fr->firstoff,
967 (long) fr->lastframe, (long) fr->lastoff, (long) fr->ignoreframe);
968 #else
969 debug3("frame_set_seek: begin at %li frames, end at %li; ignore from %li",
970 (long) fr->firstframe, (long) fr->lastframe, (long) fr->ignoreframe);
971 #endif
972 }
973
mpg123_volume_change(mpg123_handle * mh,double change)974 int attribute_align_arg mpg123_volume_change(mpg123_handle *mh, double change)
975 {
976 if(mh == NULL) return MPG123_ERR;
977 return mpg123_volume(mh, change + (double) mh->p.outscale);
978 }
979
mpg123_volume(mpg123_handle * mh,double vol)980 int attribute_align_arg mpg123_volume(mpg123_handle *mh, double vol)
981 {
982 if(mh == NULL) return MPG123_ERR;
983
984 if(vol >= 0) mh->p.outscale = vol;
985 else mh->p.outscale = 0.;
986
987 do_rva(mh);
988 return MPG123_OK;
989 }
990
get_rva(mpg123_handle * fr,double * peak,double * gain)991 static int get_rva(mpg123_handle *fr, double *peak, double *gain)
992 {
993 double p = -1;
994 double g = 0;
995 int ret = 0;
996 if(fr->p.rva)
997 {
998 int rt = 0;
999 /* Should one assume a zero RVA as no RVA? */
1000 if(fr->p.rva == 2 && fr->rva.level[1] != -1) rt = 1;
1001 if(fr->rva.level[rt] != -1)
1002 {
1003 p = fr->rva.peak[rt];
1004 g = fr->rva.gain[rt];
1005 ret = 1; /* Success. */
1006 }
1007 }
1008 if(peak != NULL) *peak = p;
1009 if(gain != NULL) *gain = g;
1010 return ret;
1011 }
1012
1013 /* adjust the volume, taking both fr->outscale and rva values into account */
do_rva(mpg123_handle * fr)1014 void do_rva(mpg123_handle *fr)
1015 {
1016 double peak = 0;
1017 double gain = 0;
1018 double newscale;
1019 double rvafact = 1;
1020 if(get_rva(fr, &peak, &gain))
1021 {
1022 if(NOQUIET && fr->p.verbose > 1) fprintf(stderr, "Note: doing RVA with gain %f\n", gain);
1023 rvafact = pow(10,gain/20);
1024 }
1025
1026 newscale = fr->p.outscale*rvafact;
1027
1028 /* if peak is unknown (== 0) this check won't hurt */
1029 if((peak*newscale) > 1.0)
1030 {
1031 newscale = 1.0/peak;
1032 warning2("limiting scale value to %f to prevent clipping with indicated peak factor of %f", newscale, peak);
1033 }
1034 /* first rva setting is forced with fr->lastscale < 0 */
1035 if(newscale != fr->lastscale || fr->decoder_change)
1036 {
1037 debug3("changing scale value from %f to %f (peak estimated to %f)", fr->lastscale != -1 ? fr->lastscale : fr->p.outscale, newscale, (double) (newscale*peak));
1038 fr->lastscale = newscale;
1039 /* It may be too early, actually. */
1040 if(fr->make_decode_tables != NULL) fr->make_decode_tables(fr); /* the actual work */
1041 }
1042 }
1043
1044
mpg123_getvolume(mpg123_handle * mh,double * base,double * really,double * rva_db)1045 int attribute_align_arg mpg123_getvolume(mpg123_handle *mh, double *base, double *really, double *rva_db)
1046 {
1047 if(mh == NULL) return MPG123_ERR;
1048 if(base) *base = mh->p.outscale;
1049 if(really) *really = mh->lastscale;
1050 get_rva(mh, NULL, rva_db);
1051 return MPG123_OK;
1052 }
1053
mpg123_framepos(mpg123_handle * mh)1054 off_t attribute_align_arg mpg123_framepos(mpg123_handle *mh)
1055 {
1056 if(mh == NULL) return MPG123_ERR;
1057
1058 return mh->input_offset;
1059 }
1060