2 libmpg123: MPEG Audio Decoder library
4 copyright 1995-2014 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
9 #include "mpg123lib_intern.h"
14 /* Want accurate rounding function regardless of decoder setup. */
15 #define FORCE_ACCURATE
18 #define SEEKFRAME(mh) ((mh)->ignoreframe < 0 ? 0 : (mh)->ignoreframe)
20 static int initialized
= 0;
22 int attribute_align_arg
mpg123_init(void)
24 if((sizeof(short) != 2) || (sizeof(long) < 4)) return MPG123_BAD_TYPES
;
26 if(initialized
) return MPG123_OK
; /* no need to initialize twice */
29 init_layer12(); /* inits also shared tables with layer1 */
34 prepare_decode_tables();
37 #if (defined REAL_IS_FLOAT) && (defined IEEE_FLOAT)
38 /* This is rather pointless but it eases my mind to check that we did
39 not enable the special rounding on a VAX or something. */
40 if(12346 != REAL_TO_SHORT_ACCURATE(12345.67f
))
42 error("Bad IEEE 754 rounding. Re-build libmpg123 properly.");
49 void attribute_align_arg
mpg123_exit(void)
51 /* nothing yet, but something later perhaps */
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
)
57 return mpg123_parnew(NULL
, decoder
, error
);
60 /* ...the full routine with optional initial parameters to override defaults. */
61 mpg123_handle attribute_align_arg
*mpg123_parnew(mpg123_pars
*mp
, const char* decoder
, int *error
)
63 mpg123_handle
*fr
= NULL
;
66 if(initialized
) fr
= (mpg123_handle
*) malloc(sizeof(mpg123_handle
));
67 else err
= MPG123_NOT_INITIALIZED
;
70 frame_init_par(fr
, mp
);
71 debug("cpu opt setting");
72 if(frame_cpu_opt(fr
, decoder
) != 1)
74 err
= MPG123_BAD_DECODER
;
82 fr
->decoder_change
= 1;
84 else if(err
== MPG123_OK
) err
= MPG123_OUT_OF_MEM
;
86 if(error
!= NULL
) *error
= err
;
90 int attribute_align_arg
mpg123_decoder(mpg123_handle
*mh
, const char* decoder
)
92 enum optdec dt
= dectype(decoder
);
94 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
98 mh
->err
= MPG123_BAD_DECODER
;
101 if(dt
== mh
->cpu_opts
.type
) return MPG123_OK
;
103 /* Now really change. */
106 debug("cpu opt setting");
107 if(frame_cpu_opt(mh
, decoder
) != 1)
109 mh
->err
= MPG123_BAD_DECODER
;
113 /* New buffers for decoder are created in frame_buffers() */
114 if((frame_outbuffer(mh
) != 0))
116 mh
->err
= MPG123_NO_BUFFERS
;
120 /* Do _not_ call decode_update here! That is only allowed after a first MPEG frame has been met. */
121 mh
->decoder_change
= 1;
125 int attribute_align_arg
mpg123_param(mpg123_handle
*mh
, enum mpg123_parms key
, long val
, double fval
)
129 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
130 r
= mpg123_par(&mh
->p
, key
, val
, fval
);
131 if(r
!= MPG123_OK
){ mh
->err
= r
; r
= MPG123_ERR
; }
133 { /* Special treatment for some settings. */
135 if(key
== MPG123_INDEX_SIZE
)
136 { /* Apply frame index size and grow property on the fly. */
137 r
= frame_index_setup(mh
);
138 if(r
!= MPG123_OK
) mh
->err
= MPG123_INDEX_FAIL
;
142 /* Feeder pool size is applied right away, reader will react to that. */
143 if(key
== MPG123_FEEDPOOL
|| key
== MPG123_FEEDBUFFER
)
144 bc_poolsize(&mh
->rdat
.buffer
, mh
->p
.feedpool
, mh
->p
.feedbuffer
);
150 int attribute_align_arg
mpg123_par(mpg123_pars
*mp
, enum mpg123_parms key
, long val
, double fval
)
154 if(mp
== NULL
) return MPG123_BAD_PARS
;
162 if(val
& MPG123_GAPLESS
) ret
= MPG123_NO_GAPLESS
;
164 if(ret
== MPG123_OK
) mp
->flags
= val
;
165 debug1("set flags to 0x%lx", (unsigned long) mp
->flags
);
167 case MPG123_ADD_FLAGS
:
169 /* Enabling of gapless mode doesn't work when it's not there, but disabling (below) is no problem. */
170 if(val
& MPG123_GAPLESS
) ret
= MPG123_NO_GAPLESS
;
174 debug1("set flags to 0x%lx", (unsigned long) mp
->flags
);
176 case MPG123_REMOVE_FLAGS
:
178 debug1("set flags to 0x%lx", (unsigned long) mp
->flags
);
180 case MPG123_FORCE_RATE
: /* should this trigger something? */
183 ret
= MPG123_BAD_RATE
;
185 if(val
> 96000) ret
= MPG123_BAD_RATE
;
186 else mp
->force_rate
= val
< 0 ? 0 : val
; /* >0 means enable, 0 disable */
189 case MPG123_DOWN_SAMPLE
:
191 if(val
!= 0) ret
= MPG123_BAD_RATE
;
193 if(val
< 0 || val
> 2) ret
= MPG123_BAD_RATE
;
194 else mp
->down_sample
= (int)val
;
198 if(val
< 0 || val
> MPG123_RVA_MAX
) ret
= MPG123_BAD_RVA
;
199 else mp
->rva
= (int)val
;
201 case MPG123_DOWNSPEED
:
202 mp
->halfspeed
= val
< 0 ? 0 : val
;
205 mp
->doublespeed
= val
< 0 ? 0 : val
;
207 case MPG123_ICY_INTERVAL
:
209 mp
->icy_interval
= val
> 0 ? val
: 0;
211 if(val
> 0) ret
= MPG123_BAD_PARAM
;
214 case MPG123_OUTSCALE
:
215 /* Choose the value that is non-zero, if any.
216 Downscaling integers to 1.0 . */
217 mp
->outscale
= val
== 0 ? fval
: (double)val
/SHORT_SCALE
;
221 mp
->timeout
= val
>= 0 ? val
: 0;
223 if(val
> 0) ret
= MPG123_NO_TIMEOUT
;
226 case MPG123_RESYNC_LIMIT
:
227 mp
->resync_limit
= val
;
229 case MPG123_INDEX_SIZE
:
231 mp
->index_size
= val
;
233 ret
= MPG123_NO_INDEX
;
236 case MPG123_PREFRAMES
:
237 if(val
>= 0) mp
->preframes
= val
;
238 else ret
= MPG123_BAD_VALUE
;
240 case MPG123_FEEDPOOL
:
242 if(val
>= 0) mp
->feedpool
= val
;
243 else ret
= MPG123_BAD_VALUE
;
245 ret
= MPG123_MISSING_FEATURE
;
248 case MPG123_FEEDBUFFER
:
250 if(val
> 0) mp
->feedbuffer
= val
;
251 else ret
= MPG123_BAD_VALUE
;
253 ret
= MPG123_MISSING_FEATURE
;
257 ret
= MPG123_BAD_PARAM
;
262 int attribute_align_arg
mpg123_getparam(mpg123_handle
*mh
, enum mpg123_parms key
, long *val
, double *fval
)
266 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
267 r
= mpg123_getpar(&mh
->p
, key
, val
, fval
);
268 if(r
!= MPG123_OK
){ mh
->err
= r
; r
= MPG123_ERR
; }
272 int attribute_align_arg
mpg123_getpar(mpg123_pars
*mp
, enum mpg123_parms key
, long *val
, double *fval
)
276 if(mp
== NULL
) return MPG123_BAD_PARS
;
280 if(val
) *val
= mp
->verbose
;
283 case MPG123_ADD_FLAGS
:
284 if(val
) *val
= mp
->flags
;
286 case MPG123_FORCE_RATE
:
291 *val
= mp
->force_rate
;
294 case MPG123_DOWN_SAMPLE
:
295 if(val
) *val
= mp
->down_sample
;
298 if(val
) *val
= mp
->rva
;
300 case MPG123_DOWNSPEED
:
301 if(val
) *val
= mp
->halfspeed
;
304 if(val
) *val
= mp
->doublespeed
;
306 case MPG123_ICY_INTERVAL
:
308 if(val
) *val
= (long)mp
->icy_interval
;
313 case MPG123_OUTSCALE
:
314 if(fval
) *fval
= mp
->outscale
;
315 if(val
) *val
= (long)(mp
->outscale
*SHORT_SCALE
);
317 case MPG123_RESYNC_LIMIT
:
318 if(val
) *val
= mp
->resync_limit
;
320 case MPG123_INDEX_SIZE
:
323 *val
= mp
->index_size
;
325 *val
= 0; /* graceful fallback: no index is index of zero size */
328 case MPG123_PREFRAMES
:
329 *val
= mp
->preframes
;
331 case MPG123_FEEDPOOL
:
335 ret
= MPG123_MISSING_FEATURE
;
338 case MPG123_FEEDBUFFER
:
340 *val
= mp
->feedbuffer
;
342 ret
= MPG123_MISSING_FEATURE
;
346 ret
= MPG123_BAD_PARAM
;
351 int attribute_align_arg
mpg123_getstate(mpg123_handle
*mh
, enum mpg123_state key
, long *val
, double *fval
)
357 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
361 case MPG123_ACCURATE
:
362 theval
= mh
->state_flags
& FRAME_ACCURATE
;
364 case MPG123_FRANKENSTEIN
:
365 theval
= mh
->state_flags
& FRAME_FRANKENSTEIN
;
367 case MPG123_BUFFERFILL
:
370 size_t sval
= bc_fill(&mh
->rdat
.buffer
);
372 if(theval
< 0 || (size_t)theval
!= sval
)
374 mh
->err
= MPG123_INT_OVERFLOW
;
379 mh
->err
= MPG123_MISSING_FEATURE
;
383 case MPG123_FRESH_DECODER
:
384 theval
= mh
->state_flags
& FRAME_FRESH_DECODER
;
385 mh
->state_flags
&= ~FRAME_FRESH_DECODER
;
388 mh
->err
= MPG123_BAD_KEY
;
392 if(val
!= NULL
) *val
= theval
;
393 if(fval
!= NULL
) *fval
= thefval
;
397 int attribute_align_arg
mpg123_eq(mpg123_handle
*mh
, enum mpg123_channels channel
, int band
, double val
)
400 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
401 if(band
< 0 || band
> 31){ mh
->err
= MPG123_BAD_BAND
; return MPG123_ERR
; }
404 case MPG123_LEFT
|MPG123_RIGHT
:
405 mh
->equalizer
[0][band
] = mh
->equalizer
[1][band
] = DOUBLE_TO_REAL(val
);
407 case MPG123_LEFT
: mh
->equalizer
[0][band
] = DOUBLE_TO_REAL(val
); break;
408 case MPG123_RIGHT
: mh
->equalizer
[1][band
] = DOUBLE_TO_REAL(val
); break;
410 mh
->err
=MPG123_BAD_CHANNEL
;
413 mh
->have_eq_settings
= TRUE
;
418 double attribute_align_arg
mpg123_geteq(mpg123_handle
*mh
, enum mpg123_channels channel
, int band
)
423 /* Handle this gracefully. When there is no band, it has no volume. */
424 if(mh
!= NULL
&& band
> -1 && band
< 32)
427 case MPG123_LEFT
|MPG123_RIGHT
:
428 ret
= 0.5*(REAL_TO_DOUBLE(mh
->equalizer
[0][band
])+REAL_TO_DOUBLE(mh
->equalizer
[1][band
]));
430 case MPG123_LEFT
: ret
= REAL_TO_DOUBLE(mh
->equalizer
[0][band
]); break;
431 case MPG123_RIGHT
: ret
= REAL_TO_DOUBLE(mh
->equalizer
[1][band
]); break;
432 /* Default case is already handled: ret = 0 */
438 /* plain file access, no http! */
439 int attribute_align_arg
mpg123_open(mpg123_handle
*mh
, const char *path
)
441 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
444 return open_stream(mh
, path
, -1);
447 int attribute_align_arg
mpg123_open_fd(mpg123_handle
*mh
, int fd
)
449 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
452 return open_stream(mh
, NULL
, fd
);
455 int attribute_align_arg
mpg123_open_handle(mpg123_handle
*mh
, void *iohandle
)
457 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
460 if(mh
->rdat
.r_read_handle
== NULL
)
462 mh
->err
= MPG123_BAD_CUSTOM_IO
;
465 return open_stream_handle(mh
, iohandle
);
468 int attribute_align_arg
mpg123_open_feed(mpg123_handle
*mh
)
470 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
473 return open_feed(mh
);
476 int attribute_align_arg
mpg123_replace_reader( mpg123_handle
*mh
,
477 ssize_t (*r_read
) (int, void *, size_t),
478 off_t (*r_lseek
)(int, off_t
, int) )
480 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
483 mh
->rdat
.r_read
= r_read
;
484 mh
->rdat
.r_lseek
= r_lseek
;
488 int attribute_align_arg
mpg123_replace_reader_handle( mpg123_handle
*mh
,
489 ssize_t (*r_read
) (void*, void *, size_t),
490 off_t (*r_lseek
)(void*, off_t
, int),
491 void (*cleanup
)(void*) )
493 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
496 mh
->rdat
.r_read_handle
= r_read
;
497 mh
->rdat
.r_lseek_handle
= r_lseek
;
498 mh
->rdat
.cleanup_handle
= cleanup
;
502 /* Update decoding engine for
503 a) a new choice of decoder
504 b) a changed native format of the MPEG stream
505 ... calls are only valid after parsing some MPEG frame! */
506 int decode_update(mpg123_handle
*mh
)
513 if(!(mh
->p
.flags
& MPG123_QUIET
)) error("decode_update() has been called before reading the first MPEG frame! Internal programming error.");
515 mh
->err
= MPG123_BAD_DECODER_SETUP
;
519 mh
->state_flags
|= FRAME_FRESH_DECODER
;
520 native_rate
= frame_freq(mh
);
522 b
= frame_output_format(mh
); /* Select the new output format based on given constraints. */
523 if(b
< 0) return MPG123_ERR
;
525 if(b
== 1) mh
->new_format
= 1; /* Store for later... */
527 debug3("updating decoder structure with native rate %li and af.rate %li (new format: %i)", native_rate
, mh
->af
.rate
, mh
->new_format
);
528 if(mh
->af
.rate
== native_rate
) mh
->down_sample
= 0;
529 else if(mh
->af
.rate
== native_rate
>>1) mh
->down_sample
= 1;
530 else if(mh
->af
.rate
== native_rate
>>2) mh
->down_sample
= 2;
531 else mh
->down_sample
= 3; /* flexible (fixed) rate */
532 switch(mh
->down_sample
)
537 mh
->down_sample_sblimit
= SBLIMIT
>>(mh
->down_sample
);
538 /* With downsampling I get less samples per frame */
539 mh
->outblock
= outblock_bytes(mh
, (mh
->spf
>>mh
->down_sample
));
544 if(synth_ntom_set_step(mh
) != 0) return -1;
545 if(frame_freq(mh
) > mh
->af
.rate
)
547 mh
->down_sample_sblimit
= SBLIMIT
* mh
->af
.rate
;
548 mh
->down_sample_sblimit
/= frame_freq(mh
);
550 else mh
->down_sample_sblimit
= SBLIMIT
;
551 mh
->outblock
= outblock_bytes(mh
,
552 ( ( NTOM_MUL
-1+mh
->spf
553 * (((size_t)NTOM_MUL
*mh
->af
.rate
)/frame_freq(mh
))
560 if(!(mh
->p
.flags
& MPG123_FORCE_MONO
))
562 if(mh
->af
.channels
== 1) mh
->single
= SINGLE_MIX
;
563 else mh
->single
= SINGLE_STEREO
;
565 else mh
->single
= (mh
->p
.flags
& MPG123_FORCE_MONO
)-1;
566 if(set_synth_functions(mh
) != 0) return -1;;
568 /* The needed size of output buffer may have changed. */
569 if(frame_outbuffer(mh
) != MPG123_OK
) return -1;
572 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
);
577 size_t attribute_align_arg
mpg123_safe_buffer(void)
579 /* real is the largest possible output (it's 32bit float, 32bit int or 64bit double). */
580 return sizeof(real
)*2*1152*NTOM_MAX
;
583 size_t attribute_align_arg
mpg123_outblock(mpg123_handle
*mh
)
585 /* Try to be helpful and never return zero output block size. */
586 if(mh
!= NULL
&& mh
->outblock
> 0) return mh
->outblock
;
587 else return mpg123_safe_buffer();
590 /* Read in the next frame we actually want for decoding.
591 This includes skipping/ignoring frames, in additon to skipping junk in the parser. */
592 static int get_next_frame(mpg123_handle
*mh
)
594 int change
= mh
->decoder_change
;
595 /* Ensure we got proper decoder for ignoring frames.
596 Header can be changed from seeking around. But be careful: Only after at
597 least one frame got read, decoder update makes sense. */
598 if(mh
->header_change
> 1 && mh
->num
>= 0)
601 mh
->header_change
= 0;
602 debug("starting with big header change");
603 if(decode_update(mh
) < 0)
610 /* Decode & discard some frame(s) before beginning. */
611 if(mh
->to_ignore
&& mh
->num
< mh
->firstframe
&& mh
->num
>= mh
->ignoreframe
)
613 debug1("ignoring frame %li", (long)mh
->num
);
614 /* Decoder structure must be current! decode_update has been called before... */
615 (mh
->do_layer
)(mh
); mh
->buffer
.fill
= 0;
617 /* The ignored decoding may have failed. Make sure ntom stays consistent. */
618 if(mh
->down_sample
== 3) ntom_set_ntom(mh
, mh
->num
+1);
620 mh
->to_ignore
= mh
->to_decode
= FALSE
;
622 /* Read new frame data; possibly breaking out here for MPG123_NEED_MORE. */
624 mh
->to_decode
= FALSE
;
625 b
= read_frame(mh
); /* That sets to_decode only if a full frame was read. */
626 debug4("read of frame %li returned %i (to_decode=%i) at sample %li", (long)mh
->num
, b
, mh
->to_decode
, (long)mpg123_tell(mh
));
627 if(b
== MPG123_NEED_MORE
) return MPG123_NEED_MORE
; /* need another call with data */
630 /* More sophisticated error control? */
631 if(b
==0 || (mh
->rdat
.filelen
>= 0 && mh
->rdat
.filepos
== mh
->rdat
.filelen
))
632 { /* We simply reached the end. */
633 mh
->track_frames
= mh
->num
+ 1;
634 debug("What about updating/checking gapless sample count here?");
637 else return MPG123_ERR
; /* Some real error. */
639 /* Now, there should be new data to decode ... and also possibly new stream properties */
640 if(mh
->header_change
> 1)
642 debug("big header change");
644 mh
->header_change
= 0;
645 /* Need to update decoder structure right away since frame might need to
646 be decoded on next loop iteration for properly ignoring its output. */
647 if(decode_update(mh
) < 0)
650 /* Now some accounting: Look at the numbers and decide if we want this frame. */
652 /* Plain skipping without decoding, only when frame is not ignored on next cycle. */
653 if(mh
->num
< mh
->firstframe
|| (mh
->p
.doublespeed
&& (mh
->playnum
% mh
->p
.doublespeed
)))
655 if(!(mh
->to_ignore
&& mh
->num
< mh
->firstframe
&& mh
->num
>= mh
->ignoreframe
))
658 /* Should one fix NtoM here or not?
659 It is not work the trouble for doublespeed, but what with leading frames? */
662 /* Or, we are finally done and have a new frame. */
666 /* If we reach this point, we got a new frame ready to be decoded.
667 All other situations resulted in returns from the loop. */
670 mh
->decoder_change
= 0;
675 /* Prepare offsets for gapless decoding. */
676 debug1("preparing gapless stuff with native rate %li", frame_freq(mh
));
677 frame_gapless_realinit(mh
);
678 frame_set_frameseek(mh
, mh
->num
);
682 /* Could this possibly happen? With a real big gapless offset... */
683 if(mh
->num
< mh
->firstframe
) b
= get_next_frame(mh
);
684 if(b
< 0) return b
; /* Could be error, need for more, new format... */
691 /* Assumption: A buffer full of zero samples can be constructed by repetition of this byte.
692 Oh, and it handles some format conversion.
693 Only to be used by decode_the_frame() ... */
694 static int zero_byte(mpg123_handle
*fr
)
697 return fr
->af
.encoding
& MPG123_ENC_8
? fr
->conv16to8
[0] : 0;
699 return 0; /* All normal signed formats have the zero here (even in byte form -- that may be an assumption for your funny machine...). */
704 Not part of the api. This just decodes the frame and fills missing bits with zeroes.
705 There can be frames that are broken and thus make do_layer() fail.
707 static void decode_the_frame(mpg123_handle
*fr
)
709 size_t needed_bytes
= decoder_synth_bytes(fr
, frame_expect_outsamples(fr
));
710 fr
->clip
+= (fr
->do_layer
)(fr
);
711 /*fprintf(stderr, "frame %"OFF_P": got %"SIZE_P" / %"SIZE_P"\n", fr->num,(size_p)fr->buffer.fill, (size_p)needed_bytes);*/
712 /* There could be less data than promised.
713 Also, then debugging, we look out for coding errors that could result in _more_ data than expected. */
715 if(fr
->buffer
.fill
!= needed_bytes
)
718 if(fr
->buffer
.fill
< needed_bytes
)
721 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
);
724 One could do a loop with individual samples instead... but zero is zero
725 Actually, that is wrong: zero is mostly a series of null bytes,
726 but we have funny 8bit formats that have a different opinion on zero...
727 Unsigned 16 or 32 bit formats are handled later.
729 memset( fr
->buffer
.data
+ fr
->buffer
.fill
, zero_byte(fr
), needed_bytes
- fr
->buffer
.fill
);
731 fr
->buffer
.fill
= needed_bytes
;
733 /* ntom_val will be wrong when the decoding wasn't carried out completely */
734 ntom_set_ntom(fr
, fr
->num
+1);
741 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
);
745 postprocess_buffer(fr
);
749 Decode the current frame into the frame structure's buffer, accessible at the location stored in <audio>, with <bytes> bytes available.
750 <num> will contain the last decoded frame number. This function should be called after mpg123_framebyframe_next positioned the stream at a
751 valid mp3 frame. The buffer contents will get lost on the next call to mpg123_framebyframe_next or mpg123_framebyframe_decode.
753 MPG123_OK -- successfully decoded or ignored the frame, you get your output data or in case of ignored frames 0 bytes
754 MPG123_DONE -- decoding finished, should not happen
755 MPG123_ERR -- some error occured.
756 MPG123_ERR_NULL -- audio or bytes are not pointing to valid storage addresses
757 MPG123_BAD_HANDLE -- mh has not been initialized
758 MPG123_NO_SPACE -- not enough space in buffer for safe decoding, should not happen
760 int attribute_align_arg
mpg123_framebyframe_decode(mpg123_handle
*mh
, off_t
*num
, unsigned char **audio
, size_t *bytes
)
762 if(bytes
== NULL
) return MPG123_ERR_NULL
;
763 if(audio
== NULL
) return MPG123_ERR_NULL
;
764 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
765 if(mh
->buffer
.size
< mh
->outblock
) return MPG123_NO_SPACE
;
768 mh
->buffer
.fill
= 0; /* always start fresh */
769 if(!mh
->to_decode
) return MPG123_OK
;
771 if(num
!= NULL
) *num
= mh
->num
;
773 decode_the_frame(mh
);
774 mh
->to_decode
= mh
->to_ignore
= FALSE
;
775 mh
->buffer
.p
= mh
->buffer
.data
;
776 FRAME_BUFFERCHECK(mh
);
777 *audio
= mh
->buffer
.p
;
778 *bytes
= mh
->buffer
.fill
;
783 Find, read and parse the next mp3 frame while skipping junk and parsing id3 tags, lame headers, etc.
784 Prepares everything for decoding using mpg123_framebyframe_decode.
786 MPG123_OK -- new frame was read and parsed, call mpg123_framebyframe_decode to actually decode
787 MPG123_NEW_FORMAT -- new frame was read, it results in changed output format, call mpg123_framebyframe_decode to actually decode
788 MPG123_BAD_HANDLE -- mh has not been initialized
789 MPG123_NEED_MORE -- more input data is needed to advance to the next frame. supply more input data using mpg123_feed
791 int attribute_align_arg
mpg123_framebyframe_next(mpg123_handle
*mh
)
794 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
796 mh
->to_decode
= mh
->to_ignore
= FALSE
;
799 b
= get_next_frame(mh
);
801 debug1("got next frame, %i", mh
->to_decode
);
803 /* mpg123_framebyframe_decode will return MPG123_OK with 0 bytes decoded if mh->to_decode is 0 */
809 debug("notifiying new format");
811 return MPG123_NEW_FORMAT
;
818 Put _one_ decoded frame into the frame structure's buffer, accessible at the location stored in <audio>, with <bytes> bytes available.
819 The buffer contents will be lost on next call to mpg123_decode_frame.
820 MPG123_OK -- successfully decoded the frame, you get your output data
821 MPg123_DONE -- This is it. End.
822 MPG123_ERR -- some error occured...
823 MPG123_NEW_FORMAT -- new frame was read, it results in changed output format -> will be decoded on next call
824 MPG123_NEED_MORE -- that should not happen as this function is intended for in-library stream reader but if you force it...
825 MPG123_NO_SPACE -- not enough space in buffer for safe decoding, also should not happen
827 num will be updated to the last decoded frame number (may possibly _not_ increase, p.ex. when format changed).
829 int attribute_align_arg
mpg123_decode_frame(mpg123_handle
*mh
, off_t
*num
, unsigned char **audio
, size_t *bytes
)
831 if(bytes
!= NULL
) *bytes
= 0;
832 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
833 if(mh
->buffer
.size
< mh
->outblock
) return MPG123_NO_SPACE
;
834 mh
->buffer
.fill
= 0; /* always start fresh */
837 /* decode if possible */
842 debug("notifiying new format");
844 return MPG123_NEW_FORMAT
;
846 if(num
!= NULL
) *num
= mh
->num
;
849 decode_the_frame(mh
);
851 mh
->to_decode
= mh
->to_ignore
= FALSE
;
852 mh
->buffer
.p
= mh
->buffer
.data
;
853 FRAME_BUFFERCHECK(mh
);
854 if(audio
!= NULL
) *audio
= mh
->buffer
.p
;
855 if(bytes
!= NULL
) *bytes
= mh
->buffer
.fill
;
861 int b
= get_next_frame(mh
);
863 debug1("got next frame, %i", mh
->to_decode
);
868 int attribute_align_arg
mpg123_read(mpg123_handle
*mh
, unsigned char *out
, size_t size
, size_t *done
)
870 return mpg123_decode(mh
, NULL
, 0, out
, size
, done
);
873 int attribute_align_arg
mpg123_feed(mpg123_handle
*mh
, const unsigned char *in
, size_t size
)
875 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
881 if(feed_more(mh
, in
, size
) != 0) return MPG123_ERR
;
884 /* The need for more data might have triggered an error.
885 This one is outdated now with the new data. */
886 if(mh
->err
== MPG123_ERR_READER
) mh
->err
= MPG123_OK
;
893 mh
->err
= MPG123_NULL_BUFFER
;
899 mh
->err
= MPG123_MISSING_FEATURE
;
907 len = read(0,buf,16384);
910 ret = decodeMP3(&mp,buf,len,out,8192,&size);
911 while(ret == MP3_OK) {
913 ret = decodeMP3(&mp,NULL,0,out,8192,&size);
918 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
)
923 if(done
!= NULL
) *done
= 0;
924 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
926 if(inmemsize
> 0 && mpg123_feed(mh
, inmemory
, inmemsize
) != MPG123_OK
)
931 if(outmemory
== NULL
) outmemsize
= 0; /* Not just give error, give chance to get a status message. */
933 while(ret
== MPG123_OK
)
935 debug4("decode loop, fill %i (%li vs. %li); to_decode: %i", (int)mh
->buffer
.fill
, (long)mh
->num
, (long)mh
->firstframe
, mh
->to_decode
);
936 /* Decode a frame that has been read before.
937 This only happens when buffer is empty! */
942 debug("notifiying new format");
944 ret
= MPG123_NEW_FORMAT
;
947 if(mh
->buffer
.size
- mh
->buffer
.fill
< mh
->outblock
)
949 ret
= MPG123_NO_SPACE
;
952 decode_the_frame(mh
);
953 mh
->to_decode
= mh
->to_ignore
= FALSE
;
954 mh
->buffer
.p
= mh
->buffer
.data
;
955 debug2("decoded frame %li, got %li samples in buffer", (long)mh
->num
, (long)(mh
->buffer
.fill
/ (samples_to_bytes(mh
, 1))));
956 FRAME_BUFFERCHECK(mh
);
958 if(mh
->buffer
.fill
) /* Copy (part of) the decoded data to the caller's buffer. */
960 /* get what is needed - or just what is there */
961 int a
= mh
->buffer
.fill
> (outmemsize
- mdone
) ? outmemsize
- mdone
: mh
->buffer
.fill
;
962 debug4("buffer fill: %i; copying %i (%i - %li)", (int)mh
->buffer
.fill
, a
, (int)outmemsize
, (long)mdone
);
963 memcpy(outmemory
, mh
->buffer
.p
, a
);
964 /* less data in frame buffer, less needed, output pointer increase, more data given... */
965 mh
->buffer
.fill
-= a
;
969 if(!(outmemsize
> mdone
)) goto decodeend
;
971 else /* If we didn't have data, get a new frame. */
973 int b
= get_next_frame(mh
);
974 if(b
< 0){ ret
= b
; goto decodeend
; }
978 if(done
!= NULL
) *done
= mdone
;
981 mh
->err
= MPG123_MISSING_FEATURE
;
986 long attribute_align_arg
mpg123_clip(mpg123_handle
*mh
)
998 /* Simples: Track needs initializtion if no initial frame has been read yet. */
999 #define track_need_init(mh) ((mh)->num < 0)
1001 static int init_track(mpg123_handle
*mh
)
1003 if(track_need_init(mh
))
1005 /* Fresh track, need first frame for basic info. */
1006 int b
= get_next_frame(mh
);
1012 int attribute_align_arg
mpg123_info(mpg123_handle
*mh
, struct mpg123_frameinfo
*mi
)
1016 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
1019 mh
->err
= MPG123_ERR_NULL
;
1025 mi
->version
= mh
->mpeg25
? MPG123_2_5
: (mh
->lsf
? MPG123_2_0
: MPG123_1_0
);
1026 mi
->layer
= mh
->lay
;
1027 mi
->rate
= frame_freq(mh
);
1030 case 0: mi
->mode
= MPG123_M_STEREO
; break;
1031 case 1: mi
->mode
= MPG123_M_JOINT
; break;
1032 case 2: mi
->mode
= MPG123_M_DUAL
; break;
1033 case 3: mi
->mode
= MPG123_M_MONO
; break;
1034 default: error("That mode cannot be!");
1036 mi
->mode_ext
= mh
->mode_ext
;
1037 mi
->framesize
= mh
->framesize
+4; /* Include header. */
1039 if(mh
->error_protection
) mi
->flags
|= MPG123_CRC
;
1040 if(mh
->copyright
) mi
->flags
|= MPG123_COPYRIGHT
;
1041 if(mh
->extension
) mi
->flags
|= MPG123_PRIVATE
;
1042 if(mh
->original
) mi
->flags
|= MPG123_ORIGINAL
;
1043 mi
->emphasis
= mh
->emphasis
;
1044 mi
->bitrate
= frame_bitrate(mh
);
1045 mi
->abr_rate
= mh
->abr_rate
;
1050 int attribute_align_arg
mpg123_getformat2( mpg123_handle
*mh
1051 , long *rate
, int *channels
, int *encoding
, int clear_flag
)
1055 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
1059 if(rate
!= NULL
) *rate
= mh
->af
.rate
;
1060 if(channels
!= NULL
) *channels
= mh
->af
.channels
;
1061 if(encoding
!= NULL
) *encoding
= mh
->af
.encoding
;
1062 if(clear_flag
) mh
->new_format
= 0;
1066 int attribute_align_arg
mpg123_getformat(mpg123_handle
*mh
, long *rate
, int *channels
, int *encoding
)
1068 return mpg123_getformat2(mh
, rate
, channels
, encoding
, 1);
1071 off_t attribute_align_arg
mpg123_timeframe(mpg123_handle
*mh
, double seconds
)
1075 if(mh
== NULL
) return MPG123_ERR
;
1078 return (off_t
)(seconds
/mpg123_tpf(mh
));
1082 Now, where are we? We need to know the last decoded frame... and what's left of it in buffer.
1083 The current frame number can mean the last decoded frame or the to-be-decoded frame.
1084 If mh->to_decode, then mh->num frames have been decoded, the frame mh->num now coming next.
1085 If not, we have the possibility of mh->num+1 frames being decoded or nothing at all.
1086 Then, there is firstframe...when we didn't reach it yet, then the next data will come from there.
1087 mh->num starts with -1
1089 off_t attribute_align_arg
mpg123_tell(mpg123_handle
*mh
)
1091 if(mh
== NULL
) return MPG123_ERR
;
1092 if(track_need_init(mh
)) return 0;
1093 /* Now we have all the info at hand. */
1094 debug5("tell: %li/%i first %li buffer %lu; frame_outs=%li", (long)mh
->num
, mh
->to_decode
, (long)mh
->firstframe
, (unsigned long)mh
->buffer
.fill
, (long)frame_outs(mh
, mh
->num
));
1096 { /* Funny block to keep C89 happy. */
1098 if((mh
->num
< mh
->firstframe
) || (mh
->num
== mh
->firstframe
&& mh
->to_decode
))
1099 { /* We are at the beginning, expect output from firstframe on. */
1100 pos
= frame_outs(mh
, mh
->firstframe
);
1102 pos
+= mh
->firstoff
;
1105 else if(mh
->to_decode
)
1106 { /* We start fresh with this frame. Buffer should be empty, but we make sure to count it in. */
1107 pos
= frame_outs(mh
, mh
->num
) - bytes_to_samples(mh
, mh
->buffer
.fill
);
1110 { /* We serve what we have in buffer and then the beginning of next frame... */
1111 pos
= frame_outs(mh
, mh
->num
+1) - bytes_to_samples(mh
, mh
->buffer
.fill
);
1113 /* Substract padding and delay from the beginning. */
1114 pos
= SAMPLE_ADJUST(mh
,pos
);
1115 /* Negative sample offsets are not right, less than nothing is still nothing. */
1116 return pos
>0 ? pos
: 0;
1120 off_t attribute_align_arg
mpg123_tellframe(mpg123_handle
*mh
)
1122 if(mh
== NULL
) return MPG123_ERR
;
1123 if(mh
->num
< mh
->firstframe
) return mh
->firstframe
;
1124 if(mh
->to_decode
) return mh
->num
;
1125 /* Consider firstoff? */
1126 return mh
->buffer
.fill
? mh
->num
: mh
->num
+ 1;
1129 off_t attribute_align_arg
mpg123_tell_stream(mpg123_handle
*mh
)
1131 if(mh
== NULL
) return MPG123_ERR
;
1132 /* mh->rd is at least a bad_reader, so no worry. */
1133 return mh
->rd
->tell(mh
);
1136 static int do_the_seek(mpg123_handle
*mh
)
1139 off_t fnum
= SEEKFRAME(mh
);
1140 mh
->buffer
.fill
= 0;
1142 /* If we are inside the ignoreframe - firstframe window, we may get away without actual seeking. */
1143 if(mh
->num
< mh
->firstframe
)
1145 mh
->to_decode
= FALSE
; /* In any case, don't decode the current frame, perhaps ignore instead. */
1146 if(mh
->num
> fnum
) return MPG123_OK
;
1149 /* If we are already there, we are fine either for decoding or for ignoring. */
1150 if(mh
->num
== fnum
&& (mh
->to_decode
|| fnum
< mh
->firstframe
)) return MPG123_OK
;
1151 /* We have the frame before... just go ahead as normal. */
1152 if(mh
->num
== fnum
-1)
1154 mh
->to_decode
= FALSE
;
1158 /* OK, real seeking follows... clear buffers and go for it. */
1159 frame_buffers_reset(mh
);
1161 if(mh
->down_sample
== 3)
1163 ntom_set_ntom(mh
, fnum
);
1164 debug3("fixed ntom for frame %"OFF_P
" to %lu, num=%"OFF_P
, (off_p
)fnum
, mh
->ntom_val
[0], (off_p
)mh
->num
);
1167 b
= mh
->rd
->seek_frame(mh
, fnum
);
1168 if(mh
->header_change
> 1)
1170 if(decode_update(mh
) < 0) return MPG123_ERR
;
1171 mh
->header_change
= 0;
1173 debug1("seek_frame returned: %i", b
);
1175 /* Only mh->to_ignore is TRUE. */
1176 if(mh
->num
< mh
->firstframe
) mh
->to_decode
= FALSE
;
1178 mh
->playnum
= mh
->num
;
1182 off_t attribute_align_arg
mpg123_seek(mpg123_handle
*mh
, off_t sampleoff
, int whence
)
1187 pos
= mpg123_tell(mh
); /* adjusted samples */
1188 /* pos < 0 also can mean that simply a former seek failed at the lower levels.
1189 In that case, we only allow absolute seeks. */
1190 if(pos
< 0 && whence
!= SEEK_SET
)
1191 { /* Unless we got the obvious error of NULL handle, this is a special seek failure. */
1192 if(mh
!= NULL
) mh
->err
= MPG123_NO_RELSEEK
;
1195 if((b
=init_track(mh
)) < 0) return b
;
1198 case SEEK_CUR
: pos
+= sampleoff
; break;
1199 case SEEK_SET
: pos
= sampleoff
; break;
1201 /* When we do not know the end already, we can try to find it. */
1202 if(mh
->track_frames
< 1 && (mh
->rdat
.flags
& READER_SEEKABLE
))
1204 if(mh
->track_frames
> 0) pos
= SAMPLE_ADJUST(mh
,frame_outs(mh
, mh
->track_frames
)) - sampleoff
;
1206 else if(mh
->end_os
> 0) pos
= SAMPLE_ADJUST(mh
,mh
->end_os
) - sampleoff
;
1210 mh
->err
= MPG123_NO_SEEK_FROM_END
;
1214 default: mh
->err
= MPG123_BAD_WHENCE
; return MPG123_ERR
;
1216 if(pos
< 0) pos
= 0;
1217 /* pos now holds the wanted sample offset in adjusted samples */
1218 frame_set_seek(mh
, SAMPLE_UNADJUST(mh
,pos
));
1219 pos
= do_the_seek(mh
);
1220 if(pos
< 0) return pos
;
1222 return mpg123_tell(mh
);
1226 A bit more tricky... libmpg123 does not do the seeking itself.
1227 All it can do is to ignore frames until the wanted one is there.
1228 The caller doesn't know where a specific frame starts and mpg123 also only knows the general region after it scanned the file.
1229 Well, it is tricky...
1231 off_t attribute_align_arg
mpg123_feedseek(mpg123_handle
*mh
, off_t sampleoff
, int whence
, off_t
*input_offset
)
1236 pos
= mpg123_tell(mh
); /* adjusted samples */
1237 debug3("seek from %li to %li (whence=%i)", (long)pos
, (long)sampleoff
, whence
);
1238 /* The special seek error handling does not apply here... there is no lowlevel I/O. */
1239 if(pos
< 0) return pos
; /* mh == NULL is covered in mpg123_tell() */
1241 if(input_offset
== NULL
)
1243 mh
->err
= MPG123_NULL_POINTER
;
1247 if((b
=init_track(mh
)) < 0) return b
; /* May need more to do anything at all. */
1251 case SEEK_CUR
: pos
+= sampleoff
; break;
1252 case SEEK_SET
: pos
= sampleoff
; break;
1254 if(mh
->track_frames
> 0) pos
= SAMPLE_ADJUST(mh
,frame_outs(mh
, mh
->track_frames
)) - sampleoff
;
1256 else if(mh
->end_os
>= 0) pos
= SAMPLE_ADJUST(mh
,mh
->end_os
) - sampleoff
;
1260 mh
->err
= MPG123_NO_SEEK_FROM_END
;
1264 default: mh
->err
= MPG123_BAD_WHENCE
; return MPG123_ERR
;
1266 if(pos
< 0) pos
= 0;
1267 frame_set_seek(mh
, SAMPLE_UNADJUST(mh
,pos
));
1268 pos
= SEEKFRAME(mh
);
1269 mh
->buffer
.fill
= 0;
1271 /* Shortcuts without modifying input stream. */
1272 *input_offset
= mh
->rdat
.buffer
.fileoff
+ mh
->rdat
.buffer
.size
;
1273 if(mh
->num
< mh
->firstframe
) mh
->to_decode
= FALSE
;
1274 if(mh
->num
== pos
&& mh
->to_decode
) goto feedseekend
;
1275 if(mh
->num
== pos
-1) goto feedseekend
;
1277 *input_offset
= feed_set_pos(mh
, frame_index_find(mh
, SEEKFRAME(mh
), &pos
));
1278 mh
->num
= pos
-1; /* The next read frame will have num = pos. */
1279 if(*input_offset
< 0) return MPG123_ERR
;
1282 return mpg123_tell(mh
);
1284 mh
->err
= MPG123_MISSING_FEATURE
;
1289 off_t attribute_align_arg
mpg123_seek_frame(mpg123_handle
*mh
, off_t offset
, int whence
)
1294 if(mh
== NULL
) return MPG123_ERR
;
1295 if((b
=init_track(mh
)) < 0) return b
;
1297 /* Could play games here with to_decode... */
1301 case SEEK_CUR
: pos
+= offset
; break;
1302 case SEEK_SET
: pos
= offset
; break;
1304 if(mh
->track_frames
> 0) pos
= mh
->track_frames
- offset
;
1307 mh
->err
= MPG123_NO_SEEK_FROM_END
;
1312 mh
->err
= MPG123_BAD_WHENCE
;
1315 if(pos
< 0) pos
= 0;
1316 /* Not limiting the possible position on end for the chance that there might be more to the stream than announced via track_frames. */
1318 frame_set_frameseek(mh
, pos
);
1319 pos
= do_the_seek(mh
);
1320 if(pos
< 0) return pos
;
1322 return mpg123_tellframe(mh
);
1325 int attribute_align_arg
mpg123_set_filesize(mpg123_handle
*mh
, off_t size
)
1327 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
1329 mh
->rdat
.filelen
= size
;
1333 off_t attribute_align_arg
mpg123_framelength(mpg123_handle
*mh
)
1341 if(mh
->track_frames
> 0)
1342 return mh
->track_frames
;
1343 if(mh
->rdat
.filelen
> 0)
1344 { /* A bad estimate. Ignoring tags 'n stuff. */
1345 double bpf
= mh
->mean_framesize
> 0.
1346 ? mh
->mean_framesize
1348 return (off_t
)((double)(mh
->rdat
.filelen
)/bpf
+0.5);
1350 /* Last resort: No view of the future, can at least count the frames that
1351 were already parsed. */
1358 off_t attribute_align_arg
mpg123_length(mpg123_handle
*mh
)
1363 if(mh
== NULL
) return MPG123_ERR
;
1366 if(mh
->track_samples
> -1) length
= mh
->track_samples
;
1367 else if(mh
->track_frames
> 0) length
= mh
->track_frames
*mh
->spf
;
1368 else if(mh
->rdat
.filelen
> 0) /* Let the case of 0 length just fall through. */
1370 /* A bad estimate. Ignoring tags 'n stuff. */
1371 double bpf
= mh
->mean_framesize
? mh
->mean_framesize
: compute_bpf(mh
);
1372 length
= (off_t
)((double)(mh
->rdat
.filelen
)/bpf
*mh
->spf
);
1374 else if(mh
->rdat
.filelen
== 0) return mpg123_tell(mh
); /* we could be in feeder mode */
1375 else return MPG123_ERR
; /* No length info there! */
1377 debug1("mpg123_length: internal sample length: %"OFF_P
, (off_p
)length
);
1379 length
= frame_ins2outs(mh
, length
);
1380 debug1("mpg123_length: external sample length: %"OFF_P
, (off_p
)length
);
1381 length
= SAMPLE_ADJUST(mh
,length
);
1386 int attribute_align_arg
mpg123_scan(mpg123_handle
*mh
)
1390 off_t track_frames
= 0;
1391 off_t track_samples
= 0;
1393 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
1394 if(!(mh
->rdat
.flags
& READER_SEEKABLE
)){ mh
->err
= MPG123_NO_SEEK
; return MPG123_ERR
; }
1395 /* Scan through the _whole_ file, since the current position is no count but computed assuming constant samples per frame. */
1396 /* Also, we can just keep the current buffer and seek settings. Just operate on input frames here. */
1397 debug("issuing scan");
1398 b
= init_track(mh
); /* mh->num >= 0 !! */
1401 if(b
== MPG123_DONE
) return MPG123_OK
;
1402 else return MPG123_ERR
; /* Must be error here, NEED_MORE is not for seekable streams. */
1404 oldpos
= mpg123_tell(mh
);
1405 b
= mh
->rd
->seek_frame(mh
, 0);
1406 if(b
<0 || mh
->num
!= 0) return MPG123_ERR
;
1407 /* One frame must be there now. */
1409 track_samples
= mh
->spf
; /* Internal samples. */
1410 debug("TODO: We should disable gapless code when encountering inconsistent mh->spf!");
1411 debug(" ... at least unset MPG123_ACCURATE.");
1412 /* Do not increment mh->track_frames in the loop as tha would confuse Frankenstein detection. */
1413 while(read_frame(mh
) == 1)
1416 track_samples
+= mh
->spf
;
1418 mh
->track_frames
= track_frames
;
1419 mh
->track_samples
= track_samples
;
1420 debug2("Scanning yielded %"OFF_P
" track samples, %"OFF_P
" frames.", (off_p
)mh
->track_samples
, (off_p
)mh
->track_frames
);
1422 /* Also, think about usefulness of that extra value track_samples ... it could be used for consistency checking. */
1423 if(mh
->p
.flags
& MPG123_GAPLESS
) frame_gapless_update(mh
, mh
->track_samples
);
1425 return mpg123_seek(mh
, oldpos
, SEEK_SET
) >= 0 ? MPG123_OK
: MPG123_ERR
;
1428 int attribute_align_arg
mpg123_meta_check(mpg123_handle
*mh
)
1430 if(mh
!= NULL
) return mh
->metaflags
;
1434 void attribute_align_arg
mpg123_meta_free(mpg123_handle
*mh
)
1436 if(mh
== NULL
) return;
1439 reset_icy(&mh
->icy
);
1442 int attribute_align_arg
mpg123_id3(mpg123_handle
*mh
, mpg123_id3v1
**v1
, mpg123_id3v2
**v2
)
1444 if(v1
!= NULL
) *v1
= NULL
;
1445 if(v2
!= NULL
) *v2
= NULL
;
1446 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
1448 if(mh
->metaflags
& MPG123_ID3
)
1451 if(v1
!= NULL
&& mh
->rdat
.flags
& READER_ID3TAG
) *v1
= (mpg123_id3v1
*) mh
->id3buf
;
1459 mh
->metaflags
|= MPG123_ID3
;
1460 mh
->metaflags
&= ~MPG123_NEW_ID3
;
1465 int attribute_align_arg
mpg123_icy(mpg123_handle
*mh
, char **icy_meta
)
1467 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
1469 if(icy_meta
== NULL
)
1471 mh
->err
= MPG123_NULL_POINTER
;
1476 if(mh
->metaflags
& MPG123_ICY
)
1478 *icy_meta
= mh
->icy
.data
;
1479 mh
->metaflags
|= MPG123_ICY
;
1480 mh
->metaflags
&= ~MPG123_NEW_ICY
;
1484 mh
->err
= MPG123_MISSING_FEATURE
;
1489 char* attribute_align_arg
mpg123_icy2utf8(const char* icy_text
)
1492 return icy2utf8(icy_text
, 0);
1498 /* That one is always defined... it's not worth it to remove it for NO_ID3V2. */
1499 enum mpg123_text_encoding attribute_align_arg
mpg123_enc_from_id3(unsigned char id3_enc_byte
)
1501 switch(id3_enc_byte
)
1503 case mpg123_id3_latin1
: return mpg123_text_latin1
;
1504 case mpg123_id3_utf16bom
: return mpg123_text_utf16bom
; /* ID3v2.3 has UCS-2 with BOM here. */
1505 case mpg123_id3_utf16be
: return mpg123_text_utf16be
;
1506 case mpg123_id3_utf8
: return mpg123_text_utf8
;
1507 default: return mpg123_text_unknown
;
1512 int mpg123_store_utf8(mpg123_string
*sb
, enum mpg123_text_encoding enc
, const unsigned char *source
, size_t source_size
)
1517 /* The encodings we get from ID3v2 tags. */
1518 case mpg123_text_utf8
:
1519 id3_to_utf8(sb
, mpg123_id3_utf8
, source
, source_size
, 0);
1521 case mpg123_text_latin1
:
1522 id3_to_utf8(sb
, mpg123_id3_latin1
, source
, source_size
, 0);
1524 case mpg123_text_utf16bom
:
1525 case mpg123_text_utf16
:
1526 id3_to_utf8(sb
, mpg123_id3_utf16bom
, source
, source_size
, 0);
1528 /* Special because one cannot skip zero bytes here. */
1529 case mpg123_text_utf16be
:
1530 id3_to_utf8(sb
, mpg123_id3_utf16be
, source
, source_size
, 0);
1534 /* ICY encoding... */
1535 case mpg123_text_icy
:
1536 case mpg123_text_cp1252
:
1538 mpg123_free_string(sb
);
1539 /* Paranoia: Make sure that the string ends inside the buffer... */
1540 if(source
[source_size
-1] == 0)
1542 /* Convert from ICY encoding... with force applied or not. */
1543 char *tmpstring
= icy2utf8((const char*)source
, enc
== mpg123_text_cp1252
? 1 : 0);
1544 if(tmpstring
!= NULL
)
1546 mpg123_set_string(sb
, tmpstring
);
1554 mpg123_free_string(sb
);
1556 /* At least a trailing null of some form should be there... */
1557 return (sb
->fill
> 0) ? 1 : 0;
1561 int attribute_align_arg
mpg123_index(mpg123_handle
*mh
, off_t
**offsets
, off_t
*step
, size_t *fill
)
1563 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
1564 if(offsets
== NULL
|| step
== NULL
|| fill
== NULL
)
1566 mh
->err
= MPG123_BAD_INDEX_PAR
;
1570 *offsets
= mh
->index
.data
;
1571 *step
= mh
->index
.step
;
1572 *fill
= mh
->index
.fill
;
1581 int attribute_align_arg
mpg123_set_index(mpg123_handle
*mh
, off_t
*offsets
, off_t step
, size_t fill
)
1583 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
1587 mh
->err
= MPG123_BAD_INDEX_PAR
;
1590 if(fi_set(&mh
->index
, offsets
, step
, fill
) == -1)
1592 mh
->err
= MPG123_OUT_OF_MEM
;
1597 mh
->err
= MPG123_MISSING_FEATURE
;
1602 int attribute_align_arg
mpg123_close(mpg123_handle
*mh
)
1604 if(mh
== NULL
) return MPG123_BAD_HANDLE
;
1606 /* mh->rd is never NULL! */
1607 if(mh
->rd
->close
!= NULL
) mh
->rd
->close(mh
);
1611 debug("Hey, we are closing a track before the new format has been queried...");
1612 invalidate_format(&mh
->af
);
1615 /* Always reset the frame buffers on close, so we cannot forget it in funky opening routines (wrappers, even). */
1620 void attribute_align_arg
mpg123_delete(mpg123_handle
*mh
)
1625 frame_exit(mh
); /* free buffers in frame */
1626 free(mh
); /* free struct; cast? */
1630 static const char *mpg123_error
[] =
1632 "No error... (code 0)",
1633 "Unable to set up output format! (code 1)",
1634 "Invalid channel number specified. (code 2)",
1635 "Invalid sample rate specified. (code 3)",
1636 "Unable to allocate memory for 16 to 8 converter table! (code 4)",
1637 "Bad parameter id! (code 5)",
1638 "Bad buffer given -- invalid pointer or too small size. (code 6)",
1639 "Out of memory -- some malloc() failed. (code 7)",
1640 "You didn't initialize the library! (code 8)",
1641 "Invalid decoder choice. (code 9)",
1642 "Invalid mpg123 handle. (code 10)",
1643 "Unable to initialize frame buffers (out of memory?)! (code 11)",
1644 "Invalid RVA mode. (code 12)",
1645 "This build doesn't support gapless decoding. (code 13)",
1646 "Not enough buffer space. (code 14)",
1647 "Incompatible numeric data types. (code 15)",
1648 "Bad equalizer band. (code 16)",
1649 "Null pointer given where valid storage address needed. (code 17)",
1650 "Error reading the stream. (code 18)",
1651 "Cannot seek from end (end is not known). (code 19)",
1652 "Invalid 'whence' for seek function. (code 20)",
1653 "Build does not support stream timeouts. (code 21)",
1654 "File access error. (code 22)",
1655 "Seek not supported by stream. (code 23)",
1656 "No stream opened. (code 24)",
1657 "Bad parameter handle. (code 25)",
1658 "Invalid parameter addresses for index retrieval. (code 26)",
1659 "Lost track in the bytestream and did not attempt resync. (code 27)",
1660 "Failed to find valid MPEG data within limit on resync. (code 28)",
1661 "No 8bit encoding possible. (code 29)",
1662 "Stack alignment is not good. (code 30)",
1663 "You gave me a NULL buffer? (code 31)",
1664 "File position is screwed up, please do an absolute seek (code 32)",
1665 "Inappropriate NULL-pointer provided.",
1666 "Bad key value given.",
1667 "There is no frame index (disabled in this build).",
1668 "Frame index operation failed.",
1669 "Decoder setup failed (invalid combination of settings?)",
1670 "Feature not in this build."
1671 ,"Some bad value has been provided."
1672 ,"Low-level seeking has failed (call to lseek(), usually)."
1673 ,"Custom I/O obviously not prepared."
1674 ,"Overflow in LFS (large file support) conversion."
1675 ,"Overflow in integer conversion."
1678 const char* attribute_align_arg
mpg123_plain_strerror(int errcode
)
1680 if(errcode
>= 0 && errcode
< sizeof(mpg123_error
)/sizeof(char*))
1681 return mpg123_error
[errcode
];
1682 else switch(errcode
)
1685 return "A generic mpg123 error.";
1687 return "Message: I am done with this track.";
1688 case MPG123_NEED_MORE
:
1689 return "Message: Feed me more input data!";
1690 case MPG123_NEW_FORMAT
:
1691 return "Message: Prepare for a changed audio format (query the new one)!";
1693 return "I have no idea - an unknown error code!";
1697 int attribute_align_arg
mpg123_errcode(mpg123_handle
*mh
)
1699 if(mh
!= NULL
) return mh
->err
;
1700 return MPG123_BAD_HANDLE
;
1703 const char* attribute_align_arg
mpg123_strerror(mpg123_handle
*mh
)
1705 return mpg123_plain_strerror(mpg123_errcode(mh
));