1 /* 2 libmpg123: MPEG Audio Decoder library (version 1.22.4) 3 4 copyright 1995-2010 by the mpg123 project - free software under the terms of the LGPL 2.1 5 see COPYING and AUTHORS files in distribution or http://mpg123.org 6 */ 7 8 #ifndef MPG123_LIB_H 9 #define MPG123_LIB_H 10 11 /** \file mpg123.h The header file for the libmpg123 MPEG Audio decoder */ 12 13 /* A macro to check at compile time which set of API functions to expect. 14 This should be incremented at least each time a new symbol is added to the header. */ 15 #define MPG123_API_VERSION 42 16 17 /* These aren't actually in use... seems to work without using libtool. */ 18 #ifdef BUILD_MPG123_DLL 19 /* The dll exports. */ 20 #define MPG123_EXPORT __declspec(dllexport) 21 #else 22 #ifdef LINK_MPG123_DLL 23 /* The exe imports. */ 24 #define MPG123_EXPORT __declspec(dllimport) 25 #else 26 /* Nothing on normal/UNIX builds */ 27 #define MPG123_EXPORT 28 #endif 29 #endif 30 31 /* This is for Visual Studio, so this header works as distributed in the binary downloads */ 32 #if defined(_MSC_VER) && !defined(MPG123_DEF_SSIZE_T) 33 #define MPG123_DEF_SSIZE_T 34 #include <stddef.h> 35 typedef ptrdiff_t ssize_t; 36 #endif 37 38 #ifndef MPG123_NO_CONFIGURE /* Enable use of this file without configure. */ 39 #include <stdlib.h> 40 #include <sys/types.h> 41 42 /* Simplified large file handling. 43 I used to have a check here that prevents building for a library with conflicting large file setup 44 (application that uses 32 bit offsets with library that uses 64 bits). 45 While that was perfectly fine in an environment where there is one incarnation of the library, 46 it hurt GNU/Linux and Solaris systems with multilib where the distribution fails to provide the 47 correct header matching the 32 bit library (where large files need explicit support) or 48 the 64 bit library (where there is no distinction). 49 50 New approach: When the app defines _FILE_OFFSET_BITS, it wants non-default large file support, 51 and thus functions with added suffix (mpg123_open_64). 52 Any mismatch will be caught at link time because of the _FILE_OFFSET_BITS setting used when 53 building libmpg123. Plus, there's dual mode large file support in mpg123 since 1.12 now. 54 Link failure is not the expected outcome of any half-sane usage anymore. 55 56 More complication: What about client code defining _LARGEFILE64_SOURCE? It might want direct access to the _64 functions, along with the ones without suffix. Well, that's possible now via defining MPG123_NO_LARGENAME and MPG123_LARGESUFFIX, respectively, for disabling or enforcing the suffix names. 57 */ 58 59 /* 60 Now, the renaming of large file aware functions. 61 By default, it appends underscore _FILE_OFFSET_BITS (so, mpg123_seek_64 for mpg123_seek), if _FILE_OFFSET_BITS is defined. You can force a different suffix via MPG123_LARGESUFFIX (that must include the underscore), or you can just disable the whole mess by defining MPG123_NO_LARGENAME. 62 */ 63 #if (!defined MPG123_NO_LARGENAME) && ((defined _FILE_OFFSET_BITS) || (defined MPG123_LARGESUFFIX)) 64 65 /* Need some trickery to concatenate the value(s) of the given macro(s). */ 66 #define MPG123_MACROCAT_REALLY(a, b) a ## b 67 #define MPG123_MACROCAT(a, b) MPG123_MACROCAT_REALLY(a, b) 68 #ifndef MPG123_LARGESUFFIX 69 #define MPG123_LARGESUFFIX MPG123_MACROCAT(_, _FILE_OFFSET_BITS) 70 #endif 71 #define MPG123_LARGENAME(func) MPG123_MACROCAT(func, MPG123_LARGESUFFIX) 72 73 #define mpg123_open MPG123_LARGENAME(mpg123_open) 74 #define mpg123_open_fd MPG123_LARGENAME(mpg123_open_fd) 75 #define mpg123_open_handle MPG123_LARGENAME(mpg123_open_handle) 76 #define mpg123_framebyframe_decode MPG123_LARGENAME(mpg123_framebyframe_decode) 77 #define mpg123_decode_frame MPG123_LARGENAME(mpg123_decode_frame) 78 #define mpg123_tell MPG123_LARGENAME(mpg123_tell) 79 #define mpg123_tellframe MPG123_LARGENAME(mpg123_tellframe) 80 #define mpg123_tell_stream MPG123_LARGENAME(mpg123_tell_stream) 81 #define mpg123_seek MPG123_LARGENAME(mpg123_seek) 82 #define mpg123_feedseek MPG123_LARGENAME(mpg123_feedseek) 83 #define mpg123_seek_frame MPG123_LARGENAME(mpg123_seek_frame) 84 #define mpg123_timeframe MPG123_LARGENAME(mpg123_timeframe) 85 #define mpg123_index MPG123_LARGENAME(mpg123_index) 86 #define mpg123_set_index MPG123_LARGENAME(mpg123_set_index) 87 #define mpg123_position MPG123_LARGENAME(mpg123_position) 88 #define mpg123_length MPG123_LARGENAME(mpg123_length) 89 #define mpg123_framelength MPG123_LARGENAME(mpg123_framelength) 90 #define mpg123_set_filesize MPG123_LARGENAME(mpg123_set_filesize) 91 #define mpg123_replace_reader MPG123_LARGENAME(mpg123_replace_reader) 92 #define mpg123_replace_reader_handle MPG123_LARGENAME(mpg123_replace_reader_handle) 93 #define mpg123_framepos MPG123_LARGENAME(mpg123_framepos) 94 95 #endif /* largefile hackery */ 96 97 #endif /* MPG123_NO_CONFIGURE */ 98 99 #ifdef __cplusplus 100 extern "C" { 101 #endif 102 103 /** \defgroup mpg123_init mpg123 library and handle setup 104 * 105 * Functions to initialise and shutdown the mpg123 library and handles. 106 * The parameters of handles have workable defaults, you only have to tune them when you want to tune something;-) 107 * Tip: Use a RVA setting... 108 * 109 * @{ 110 */ 111 112 /** Opaque structure for the libmpg123 decoder handle. */ 113 struct mpg123_handle_struct; 114 115 /** Opaque structure for the libmpg123 decoder handle. 116 * Most functions take a pointer to a mpg123_handle as first argument and operate on its data in an object-oriented manner. 117 */ 118 typedef struct mpg123_handle_struct mpg123_handle; 119 120 /** Function to initialise the mpg123 library. 121 * This function is not thread-safe. Call it exactly once per process, before any other (possibly threaded) work with the library. 122 * 123 * \return MPG123_OK if successful, otherwise an error number. 124 */ 125 MPG123_EXPORT int mpg123_init(void); 126 127 /** Function to close down the mpg123 library. 128 * This function is not thread-safe. Call it exactly once per process, before any other (possibly threaded) work with the library. */ 129 MPG123_EXPORT void mpg123_exit(void); 130 131 /** Create a handle with optional choice of decoder (named by a string, see mpg123_decoders() or mpg123_supported_decoders()). 132 * and optional retrieval of an error code to feed to mpg123_plain_strerror(). 133 * Optional means: Any of or both the parameters may be NULL. 134 * 135 * \return Non-NULL pointer when successful. 136 */ 137 MPG123_EXPORT mpg123_handle *mpg123_new(const char* decoder, int *error); 138 139 /** Delete handle, mh is either a valid mpg123 handle or NULL. */ 140 MPG123_EXPORT void mpg123_delete(mpg123_handle *mh); 141 142 /** Enumeration of the parameters types that it is possible to set/get. */ 143 enum mpg123_parms 144 { 145 MPG123_VERBOSE = 0, /**< set verbosity value for enabling messages to stderr, >= 0 makes sense (integer) */ 146 MPG123_FLAGS, /**< set all flags, p.ex val = MPG123_GAPLESS|MPG123_MONO_MIX (integer) */ 147 MPG123_ADD_FLAGS, /**< add some flags (integer) */ 148 MPG123_FORCE_RATE, /**< when value > 0, force output rate to that value (integer) */ 149 MPG123_DOWN_SAMPLE, /**< 0=native rate, 1=half rate, 2=quarter rate (integer) */ 150 MPG123_RVA, /**< one of the RVA choices above (integer) */ 151 MPG123_DOWNSPEED, /**< play a frame N times (integer) */ 152 MPG123_UPSPEED, /**< play every Nth frame (integer) */ 153 MPG123_START_FRAME, /**< start with this frame (skip frames before that, integer) */ 154 MPG123_DECODE_FRAMES, /**< decode only this number of frames (integer) */ 155 MPG123_ICY_INTERVAL, /**< stream contains ICY metadata with this interval (integer) */ 156 MPG123_OUTSCALE, /**< the scale for output samples (amplitude - integer or float according to mpg123 output format, normally integer) */ 157 MPG123_TIMEOUT, /**< timeout for reading from a stream (not supported on win32, integer) */ 158 MPG123_REMOVE_FLAGS, /**< remove some flags (inverse of MPG123_ADD_FLAGS, integer) */ 159 MPG123_RESYNC_LIMIT, /**< Try resync on frame parsing for that many bytes or until end of stream (<0 ... integer). This can enlarge the limit for skipping junk on beginning, too (but not reduce it). */ 160 MPG123_INDEX_SIZE /**< Set the frame index size (if supported). Values <0 mean that the index is allowed to grow dynamically in these steps (in positive direction, of course) -- Use this when you really want a full index with every individual frame. */ 161 ,MPG123_PREFRAMES /**< Decode/ignore that many frames in advance for layer 3. This is needed to fill bit reservoir after seeking, for example (but also at least one frame in advance is needed to have all "normal" data for layer 3). Give a positive integer value, please.*/ 162 ,MPG123_FEEDPOOL /**< For feeder mode, keep that many buffers in a pool to avoid frequent malloc/free. The pool is allocated on mpg123_open_feed(). If you change this parameter afterwards, you can trigger growth and shrinkage during decoding. The default value could change any time. If you care about this, then set it. (integer) */ 163 ,MPG123_FEEDBUFFER /**< Minimal size of one internal feeder buffer, again, the default value is subject to change. (integer) */ 164 }; 165 166 /** Flag bits for MPG123_FLAGS, use the usual binary or to combine. */ 167 enum mpg123_param_flags 168 { 169 MPG123_FORCE_MONO = 0x7 /**< 0111 Force some mono mode: This is a test bitmask for seeing if any mono forcing is active. */ 170 ,MPG123_MONO_LEFT = 0x1 /**< 0001 Force playback of left channel only. */ 171 ,MPG123_MONO_RIGHT = 0x2 /**< 0010 Force playback of right channel only. */ 172 ,MPG123_MONO_MIX = 0x4 /**< 0100 Force playback of mixed mono. */ 173 ,MPG123_FORCE_STEREO = 0x8 /**< 1000 Force stereo output. */ 174 ,MPG123_FORCE_8BIT = 0x10 /**< 00010000 Force 8bit formats. */ 175 ,MPG123_QUIET = 0x20 /**< 00100000 Suppress any printouts (overrules verbose). */ 176 ,MPG123_GAPLESS = 0x40 /**< 01000000 Enable gapless decoding (default on if libmpg123 has support). */ 177 ,MPG123_NO_RESYNC = 0x80 /**< 10000000 Disable resync stream after error. */ 178 ,MPG123_SEEKBUFFER = 0x100 /**< 000100000000 Enable small buffer on non-seekable streams to allow some peek-ahead (for better MPEG sync). */ 179 ,MPG123_FUZZY = 0x200 /**< 001000000000 Enable fuzzy seeks (guessing byte offsets or using approximate seek points from Xing TOC) */ 180 ,MPG123_FORCE_FLOAT = 0x400 /**< 010000000000 Force floating point output (32 or 64 bits depends on mpg123 internal precision). */ 181 ,MPG123_PLAIN_ID3TEXT = 0x800 /**< 100000000000 Do not translate ID3 text data to UTF-8. ID3 strings will contain the raw text data, with the first byte containing the ID3 encoding code. */ 182 ,MPG123_IGNORE_STREAMLENGTH = 0x1000 /**< 1000000000000 Ignore any stream length information contained in the stream, which can be contained in a 'TLEN' frame of an ID3v2 tag or a Xing tag */ 183 ,MPG123_SKIP_ID3V2 = 0x2000 /**< 10 0000 0000 0000 Do not parse ID3v2 tags, just skip them. */ 184 ,MPG123_IGNORE_INFOFRAME = 0x4000 /**< 100 0000 0000 0000 Do not parse the LAME/Xing info frame, treat it as normal MPEG data. */ 185 ,MPG123_AUTO_RESAMPLE = 0x8000 /**< 1000 0000 0000 0000 Allow automatic internal resampling of any kind (default on if supported). Especially when going lowlevel with replacing output buffer, you might want to unset this flag. Setting MPG123_DOWNSAMPLE or MPG123_FORCE_RATE will override this. */ 186 ,MPG123_PICTURE = 0x10000 /**< 17th bit: Enable storage of pictures from tags (ID3v2 APIC). */ 187 }; 188 189 /** choices for MPG123_RVA */ 190 enum mpg123_param_rva 191 { 192 MPG123_RVA_OFF = 0 /**< RVA disabled (default). */ 193 ,MPG123_RVA_MIX = 1 /**< Use mix/track/radio gain. */ 194 ,MPG123_RVA_ALBUM = 2 /**< Use album/audiophile gain */ 195 ,MPG123_RVA_MAX = MPG123_RVA_ALBUM /**< The maximum RVA code, may increase in future. */ 196 }; 197 198 /* TODO: Assess the possibilities and troubles of changing parameters during playback. */ 199 200 /** Set a specific parameter, for a specific mpg123_handle, using a parameter 201 * type key chosen from the mpg123_parms enumeration, to the specified value. 202 * \return MPG123_OK on success 203 */ 204 MPG123_EXPORT int mpg123_param(mpg123_handle *mh, enum mpg123_parms type, long value, double fvalue); 205 206 /** Get a specific parameter, for a specific mpg123_handle. 207 * See the mpg123_parms enumeration for a list of available parameters. 208 * \return MPG123_OK on success 209 */ 210 MPG123_EXPORT int mpg123_getparam(mpg123_handle *mh, enum mpg123_parms type, long *val, double *fval); 211 212 /** Feature set available for query with mpg123_feature. */ 213 enum mpg123_feature_set 214 { 215 MPG123_FEATURE_ABI_UTF8OPEN = 0 /**< mpg123 expects path names to be given in UTF-8 encoding instead of plain native. */ 216 ,MPG123_FEATURE_OUTPUT_8BIT /**< 8bit output */ 217 ,MPG123_FEATURE_OUTPUT_16BIT /**< 16bit output */ 218 ,MPG123_FEATURE_OUTPUT_32BIT /**< 32bit output */ 219 ,MPG123_FEATURE_INDEX /**< support for building a frame index for accurate seeking */ 220 ,MPG123_FEATURE_PARSE_ID3V2 /**< id3v2 parsing */ 221 ,MPG123_FEATURE_DECODE_LAYER1 /**< mpeg layer-1 decoder enabled */ 222 ,MPG123_FEATURE_DECODE_LAYER2 /**< mpeg layer-2 decoder enabled */ 223 ,MPG123_FEATURE_DECODE_LAYER3 /**< mpeg layer-3 decoder enabled */ 224 ,MPG123_FEATURE_DECODE_ACCURATE /**< accurate decoder rounding */ 225 ,MPG123_FEATURE_DECODE_DOWNSAMPLE /**< downsample (sample omit) */ 226 ,MPG123_FEATURE_DECODE_NTOM /**< flexible rate decoding */ 227 ,MPG123_FEATURE_PARSE_ICY /**< ICY support */ 228 ,MPG123_FEATURE_TIMEOUT_READ /**< Reader with timeout (network). */ 229 }; 230 231 /** Query libmpg123 feature, 1 for success, 0 for unimplemented functions. */ 232 MPG123_EXPORT int mpg123_feature(const enum mpg123_feature_set key); 233 234 /* @} */ 235 236 237 /** \defgroup mpg123_error mpg123 error handling 238 * 239 * Functions to get text version of the error numbers and an enumeration 240 * of the error codes returned by libmpg123. 241 * 242 * Most functions operating on a mpg123_handle simply return MPG123_OK (0) 243 * on success and MPG123_ERR (-1) on failure, setting the internal error 244 * variable of the handle to the specific error code. If there was not a valid 245 * (non-NULL) handle provided to a function operating on one, MPG123_BAD_HANDLE 246 * may be returned if this can not be confused with a valid positive return 247 * value. 248 * Meaning: A function expected to return positive integers on success will 249 * always indicate error or a special condition by returning a negative one. 250 * 251 * Decoding/seek functions may also return message codes MPG123_DONE, 252 * MPG123_NEW_FORMAT and MPG123_NEED_MORE (all negative, see below on how to 253 * react). Note that calls to those can be nested, so generally watch out 254 * for these codes after initial handle setup. 255 * Especially any function that needs information about the current stream 256 * to work will try to at least parse the beginning if that did not happen 257 * yet. 258 * 259 * On a function that is supposed to return MPG123_OK on success and 260 * MPG123_ERR on failure, make sure you check for != MPG123_OK, not 261 * == MPG123_ERR, as the error code could get more specific in future, 262 * or there is just a special message from a decoding routine as indicated 263 * above. 264 * 265 * @{ 266 */ 267 268 /** Enumeration of the message and error codes and returned by libmpg123 functions. */ 269 enum mpg123_errors 270 { 271 MPG123_DONE=-12, /**< Message: Track ended. Stop decoding. */ 272 MPG123_NEW_FORMAT=-11, /**< Message: Output format will be different on next call. Note that some libmpg123 versions between 1.4.3 and 1.8.0 insist on you calling mpg123_getformat() after getting this message code. Newer verisons behave like advertised: You have the chance to call mpg123_getformat(), but you can also just continue decoding and get your data. */ 273 MPG123_NEED_MORE=-10, /**< Message: For feed reader: "Feed me more!" (call mpg123_feed() or mpg123_decode() with some new input data). */ 274 MPG123_ERR=-1, /**< Generic Error */ 275 MPG123_OK=0, /**< Success */ 276 MPG123_BAD_OUTFORMAT, /**< Unable to set up output format! */ 277 MPG123_BAD_CHANNEL, /**< Invalid channel number specified. */ 278 MPG123_BAD_RATE, /**< Invalid sample rate specified. */ 279 MPG123_ERR_16TO8TABLE, /**< Unable to allocate memory for 16 to 8 converter table! */ 280 MPG123_BAD_PARAM, /**< Bad parameter id! */ 281 MPG123_BAD_BUFFER, /**< Bad buffer given -- invalid pointer or too small size. */ 282 MPG123_OUT_OF_MEM, /**< Out of memory -- some malloc() failed. */ 283 MPG123_NOT_INITIALIZED, /**< You didn't initialize the library! */ 284 MPG123_BAD_DECODER, /**< Invalid decoder choice. */ 285 MPG123_BAD_HANDLE, /**< Invalid mpg123 handle. */ 286 MPG123_NO_BUFFERS, /**< Unable to initialize frame buffers (out of memory?). */ 287 MPG123_BAD_RVA, /**< Invalid RVA mode. */ 288 MPG123_NO_GAPLESS, /**< This build doesn't support gapless decoding. */ 289 MPG123_NO_SPACE, /**< Not enough buffer space. */ 290 MPG123_BAD_TYPES, /**< Incompatible numeric data types. */ 291 MPG123_BAD_BAND, /**< Bad equalizer band. */ 292 MPG123_ERR_NULL, /**< Null pointer given where valid storage address needed. */ 293 MPG123_ERR_READER, /**< Error reading the stream. */ 294 MPG123_NO_SEEK_FROM_END,/**< Cannot seek from end (end is not known). */ 295 MPG123_BAD_WHENCE, /**< Invalid 'whence' for seek function.*/ 296 MPG123_NO_TIMEOUT, /**< Build does not support stream timeouts. */ 297 MPG123_BAD_FILE, /**< File access error. */ 298 MPG123_NO_SEEK, /**< Seek not supported by stream. */ 299 MPG123_NO_READER, /**< No stream opened. */ 300 MPG123_BAD_PARS, /**< Bad parameter handle. */ 301 MPG123_BAD_INDEX_PAR, /**< Bad parameters to mpg123_index() and mpg123_set_index() */ 302 MPG123_OUT_OF_SYNC, /**< Lost track in bytestream and did not try to resync. */ 303 MPG123_RESYNC_FAIL, /**< Resync failed to find valid MPEG data. */ 304 MPG123_NO_8BIT, /**< No 8bit encoding possible. */ 305 MPG123_BAD_ALIGN, /**< Stack aligmnent error */ 306 MPG123_NULL_BUFFER, /**< NULL input buffer with non-zero size... */ 307 MPG123_NO_RELSEEK, /**< Relative seek not possible (screwed up file offset) */ 308 MPG123_NULL_POINTER, /**< You gave a null pointer somewhere where you shouldn't have. */ 309 MPG123_BAD_KEY, /**< Bad key value given. */ 310 MPG123_NO_INDEX, /**< No frame index in this build. */ 311 MPG123_INDEX_FAIL, /**< Something with frame index went wrong. */ 312 MPG123_BAD_DECODER_SETUP, /**< Something prevents a proper decoder setup */ 313 MPG123_MISSING_FEATURE /**< This feature has not been built into libmpg123. */ 314 ,MPG123_BAD_VALUE /**< A bad value has been given, somewhere. */ 315 ,MPG123_LSEEK_FAILED /**< Low-level seek failed. */ 316 ,MPG123_BAD_CUSTOM_IO /**< Custom I/O not prepared. */ 317 ,MPG123_LFS_OVERFLOW /**< Offset value overflow during translation of large file API calls -- your client program cannot handle that large file. */ 318 ,MPG123_INT_OVERFLOW /**< Some integer overflow. */ 319 }; 320 321 /** Return a string describing that error errcode means. */ 322 MPG123_EXPORT const char* mpg123_plain_strerror(int errcode); 323 324 /** Give string describing what error has occured in the context of handle mh. 325 * When a function operating on an mpg123 handle returns MPG123_ERR, you should check for the actual reason via 326 * char *errmsg = mpg123_strerror(mh) 327 * This function will catch mh == NULL and return the message for MPG123_BAD_HANDLE. */ 328 MPG123_EXPORT const char* mpg123_strerror(mpg123_handle *mh); 329 330 /** Return the plain errcode intead of a string. 331 * \return error code recorded in handle or MPG123_BAD_HANDLE 332 */ 333 MPG123_EXPORT int mpg123_errcode(mpg123_handle *mh); 334 335 /*@}*/ 336 337 338 /** \defgroup mpg123_decoder mpg123 decoder selection 339 * 340 * Functions to list and select the available decoders. 341 * Perhaps the most prominent feature of mpg123: You have several (optimized) decoders to choose from (on x86 and PPC (MacOS) systems, that is). 342 * 343 * @{ 344 */ 345 346 /** Return a NULL-terminated array of generally available decoder names (plain 8bit ASCII). */ 347 MPG123_EXPORT const char **mpg123_decoders(void); 348 349 /** Return a NULL-terminated array of the decoders supported by the CPU (plain 8bit ASCII). */ 350 MPG123_EXPORT const char **mpg123_supported_decoders(void); 351 352 /** Set the chosen decoder to 'decoder_name' 353 * \return MPG123_OK on success 354 */ 355 MPG123_EXPORT int mpg123_decoder(mpg123_handle *mh, const char* decoder_name); 356 357 /** Get the currently active decoder engine name. 358 The active decoder engine can vary depening on output constraints, 359 mostly non-resampling, integer output is accelerated via 3DNow & Co. but for other modes a fallback engine kicks in. 360 Note that this can return a decoder that is ony active in the hidden and not available as decoder choice from the outside. 361 \return The decoder name or NULL on error. */ 362 MPG123_EXPORT const char* mpg123_current_decoder(mpg123_handle *mh); 363 364 /*@}*/ 365 366 367 /** \defgroup mpg123_output mpg123 output audio format 368 * 369 * Functions to get and select the format of the decoded audio. 370 * 371 * Before you dive in, please be warned that you might get confused by this. This seems to happen a lot, therefore I am trying to explain in advance. 372 * 373 * The mpg123 library decides what output format to use when encountering the first frame in a stream, or actually any frame that is still valid but differs from the frames before in the prompted output format. At such a deciding point, an internal table of allowed encodings, sampling rates and channel setups is consulted. According to this table, an output format is chosen and the decoding engine set up accordingly (including ptimized routines for different output formats). This might seem unusual but it just follows from the non-existence of "MPEG audio files" with defined overall properties. There are streams, streams are concatenations of (semi) independent frames. We store streams on disk and call them "MPEG audio files", but that does not change their nature as the decoder is concerned (the LAME/Xing header for gapless decoding makes things interesting again). 374 * 375 * To get to the point: What you do with mpg123_format() and friends is to fill the internal table of allowed formats before it is used. That includes removing support for some formats or adding your forced sample rate (see MPG123_FORCE_RATE) that will be used with the crude internal resampler. Also keep in mind that the sample encoding is just a question of choice -- the MPEG frames do only indicate their native sampling rate and channel count. If you want to decode to integer or float samples, 8 or 16 bit ... that is your decision. In a "clean" world, libmpg123 would always decode to 32 bit float and let you handle any sample conversion. But there are optimized routines that work faster by directly decoding to the desired encoding / accuracy. We prefer efficiency over conceptual tidyness. 376 * 377 * People often start out thinking that mpg123_format() should change the actual decoding format on the fly. That is wrong. It only has effect on the next natural change of output format, when libmpg123 will consult its format table again. To make life easier, you might want to call mpg123_format_none() before any thing else and then just allow one desired encoding and a limited set of sample rates / channel choices that you actually intend to deal with. You can force libmpg123 to decode everything to 44100 KHz, stereo, 16 bit integer ... it will duplicate mono channels and even do resampling if needed (unless that feature is disabled in the build, same with some encodings). But I have to stress that the resampling of libmpg123 is very crude and doesn't even contain any kind of "proper" interpolation. 378 * 379 * In any case, watch out for MPG123_NEW_FORMAT as return message from decoding routines and call mpg123_getformat() to get the currently active output format. 380 * 381 * @{ 382 */ 383 384 /** An enum over all sample types possibly known to mpg123. 385 * The values are designed as bit flags to allow bitmasking for encoding families. 386 * 387 * Note that (your build of) libmpg123 does not necessarily support all these. 388 * Usually, you can expect the 8bit encodings and signed 16 bit. 389 * Also 32bit float will be usual beginning with mpg123-1.7.0 . 390 * What you should bear in mind is that (SSE, etc) optimized routines may be absent 391 * for some formats. We do have SSE for 16, 32 bit and float, though. 392 * 24 bit integer is done via postprocessing of 32 bit output -- just cutting 393 * the last byte, no rounding, even. If you want better, do it yourself. 394 * 395 * All formats are in native byte order. If you need different endinaness, you 396 * can simply postprocess the output buffers (libmpg123 wouldn't do anything else). 397 * mpg123_encsize() can be helpful there. 398 */ 399 enum mpg123_enc_enum 400 { 401 MPG123_ENC_8 = 0x00f /**< 0000 0000 1111 Some 8 bit integer encoding. */ 402 ,MPG123_ENC_16 = 0x040 /**< 0000 0100 0000 Some 16 bit integer encoding. */ 403 ,MPG123_ENC_24 = 0x4000 /**< 0100 0000 0000 0000 Some 24 bit integer encoding. */ 404 ,MPG123_ENC_32 = 0x100 /**< 0001 0000 0000 Some 32 bit integer encoding. */ 405 ,MPG123_ENC_SIGNED = 0x080 /**< 0000 1000 0000 Some signed integer encoding. */ 406 ,MPG123_ENC_FLOAT = 0xe00 /**< 1110 0000 0000 Some float encoding. */ 407 ,MPG123_ENC_SIGNED_16 = (MPG123_ENC_16|MPG123_ENC_SIGNED|0x10) /**< 1101 0000 signed 16 bit */ 408 ,MPG123_ENC_UNSIGNED_16 = (MPG123_ENC_16|0x20) /**< 0110 0000 unsigned 16 bit */ 409 ,MPG123_ENC_UNSIGNED_8 = 0x01 /**< 0000 0001 unsigned 8 bit */ 410 ,MPG123_ENC_SIGNED_8 = (MPG123_ENC_SIGNED|0x02) /**< 1000 0010 signed 8 bit */ 411 ,MPG123_ENC_ULAW_8 = 0x04 /**< 0000 0100 ulaw 8 bit */ 412 ,MPG123_ENC_ALAW_8 = 0x08 /**< 0000 1000 alaw 8 bit */ 413 ,MPG123_ENC_SIGNED_32 = MPG123_ENC_32|MPG123_ENC_SIGNED|0x1000 /**< 0001 0001 1000 0000 signed 32 bit */ 414 ,MPG123_ENC_UNSIGNED_32 = MPG123_ENC_32|0x2000 /**< 0010 0001 0000 0000 unsigned 32 bit */ 415 ,MPG123_ENC_SIGNED_24 = MPG123_ENC_24|MPG123_ENC_SIGNED|0x1000 /**< 0101 0000 1000 0000 signed 24 bit */ 416 ,MPG123_ENC_UNSIGNED_24 = MPG123_ENC_24|0x2000 /**< 0110 0000 0000 0000 unsigned 24 bit */ 417 ,MPG123_ENC_FLOAT_32 = 0x200 /**< 0010 0000 0000 32bit float */ 418 ,MPG123_ENC_FLOAT_64 = 0x400 /**< 0100 0000 0000 64bit float */ 419 ,MPG123_ENC_ANY = ( MPG123_ENC_SIGNED_16 | MPG123_ENC_UNSIGNED_16 | MPG123_ENC_UNSIGNED_8 420 | MPG123_ENC_SIGNED_8 | MPG123_ENC_ULAW_8 | MPG123_ENC_ALAW_8 421 | MPG123_ENC_SIGNED_32 | MPG123_ENC_UNSIGNED_32 422 | MPG123_ENC_SIGNED_24 | MPG123_ENC_UNSIGNED_24 423 | MPG123_ENC_FLOAT_32 | MPG123_ENC_FLOAT_64 ) /**< Any encoding on the list. */ 424 }; 425 426 /** They can be combined into one number (3) to indicate mono and stereo... */ 427 enum mpg123_channelcount 428 { 429 MPG123_MONO = 1 430 ,MPG123_STEREO = 2 431 }; 432 433 /** An array of supported standard sample rates 434 * These are possible native sample rates of MPEG audio files. 435 * You can still force mpg123 to resample to a different one, but by default you will only get audio in one of these samplings. 436 * \param list Store a pointer to the sample rates array there. 437 * \param number Store the number of sample rates there. */ 438 MPG123_EXPORT void mpg123_rates(const long **list, size_t *number); 439 440 /** An array of supported audio encodings. 441 * An audio encoding is one of the fully qualified members of mpg123_enc_enum (MPG123_ENC_SIGNED_16, not MPG123_SIGNED). 442 * \param list Store a pointer to the encodings array there. 443 * \param number Store the number of encodings there. */ 444 MPG123_EXPORT void mpg123_encodings(const int **list, size_t *number); 445 446 /** Return the size (in bytes) of one mono sample of the named encoding. 447 * \param encoding The encoding value to analyze. 448 * \return positive size of encoding in bytes, 0 on invalid encoding. */ 449 MPG123_EXPORT int mpg123_encsize(int encoding); 450 451 /** Configure a mpg123 handle to accept no output format at all, 452 * use before specifying supported formats with mpg123_format 453 * \return MPG123_OK on success 454 */ 455 MPG123_EXPORT int mpg123_format_none(mpg123_handle *mh); 456 457 /** Configure mpg123 handle to accept all formats 458 * (also any custom rate you may set) -- this is default. 459 * \return MPG123_OK on success 460 */ 461 MPG123_EXPORT int mpg123_format_all(mpg123_handle *mh); 462 463 /** Set the audio format support of a mpg123_handle in detail: 464 * \param mh audio decoder handle 465 * \param rate The sample rate value (in Hertz). 466 * \param channels A combination of MPG123_STEREO and MPG123_MONO. 467 * \param encodings A combination of accepted encodings for rate and channels, p.ex MPG123_ENC_SIGNED16 | MPG123_ENC_ULAW_8 (or 0 for no support). Please note that some encodings may not be supported in the library build and thus will be ignored here. 468 * \return MPG123_OK on success, MPG123_ERR if there was an error. */ 469 MPG123_EXPORT int mpg123_format(mpg123_handle *mh, long rate, int channels, int encodings); 470 471 /** Check to see if a specific format at a specific rate is supported 472 * by mpg123_handle. 473 * \return 0 for no support (that includes invalid parameters), MPG123_STEREO, 474 * MPG123_MONO or MPG123_STEREO|MPG123_MONO. */ 475 MPG123_EXPORT int mpg123_format_support(mpg123_handle *mh, long rate, int encoding); 476 477 /** Get the current output format written to the addresses given. 478 * \return MPG123_OK on success 479 */ 480 MPG123_EXPORT int mpg123_getformat(mpg123_handle *mh, long *rate, int *channels, int *encoding); 481 482 /*@}*/ 483 484 485 /** \defgroup mpg123_input mpg123 file input and decoding 486 * 487 * Functions for input bitstream and decoding operations. 488 * Decoding/seek functions may also return message codes MPG123_DONE, MPG123_NEW_FORMAT and MPG123_NEED_MORE (please read up on these on how to react!). 489 * @{ 490 */ 491 492 /* reading samples / triggering decoding, possible return values: */ 493 /** Enumeration of the error codes returned by libmpg123 functions. */ 494 495 /** Open and prepare to decode the specified file by filesystem path. 496 * This does not open HTTP urls; libmpg123 contains no networking code. 497 * If you want to decode internet streams, use mpg123_open_fd() or mpg123_open_feed(). 498 * \param path filesystem path 499 * \return MPG123_OK on success 500 */ 501 MPG123_EXPORT int mpg123_open(mpg123_handle *mh, const char *path); 502 503 /** Use an already opened file descriptor as the bitstream input 504 * mpg123_close() will _not_ close the file descriptor. 505 */ 506 MPG123_EXPORT int mpg123_open_fd(mpg123_handle *mh, int fd); 507 508 /** Use an opaque handle as bitstream input. This works only with the 509 * replaced I/O from mpg123_replace_reader_handle()! 510 * mpg123_close() will call the cleanup callback for your handle (if you gave one). 511 * \return MPG123_OK on success 512 */ 513 MPG123_EXPORT int mpg123_open_handle(mpg123_handle *mh, void *iohandle); 514 515 /** Open a new bitstream and prepare for direct feeding 516 * This works together with mpg123_decode(); you are responsible for reading and feeding the input bitstream. 517 * \return MPG123_OK on success 518 */ 519 MPG123_EXPORT int mpg123_open_feed(mpg123_handle *mh); 520 521 /** Closes the source, if libmpg123 opened it. 522 * \return MPG123_OK on success 523 */ 524 MPG123_EXPORT int mpg123_close(mpg123_handle *mh); 525 526 /** Read from stream and decode up to outmemsize bytes. 527 * \param outmemory address of output buffer to write to 528 * \param outmemsize maximum number of bytes to write 529 * \param done address to store the number of actually decoded bytes to 530 * \return MPG123_OK or error/message code 531 */ 532 MPG123_EXPORT int mpg123_read(mpg123_handle *mh, unsigned char *outmemory, size_t outmemsize, size_t *done); 533 534 /** Feed data for a stream that has been opened with mpg123_open_feed(). 535 * It's give and take: You provide the bytestream, mpg123 gives you the decoded samples. 536 * \param in input buffer 537 * \param size number of input bytes 538 * \return MPG123_OK or error/message code. 539 */ 540 MPG123_EXPORT int mpg123_feed(mpg123_handle *mh, const unsigned char *in, size_t size); 541 542 /** Decode MPEG Audio from inmemory to outmemory. 543 * This is very close to a drop-in replacement for old mpglib. 544 * When you give zero-sized output buffer the input will be parsed until 545 * decoded data is available. This enables you to get MPG123_NEW_FORMAT (and query it) 546 * without taking decoded data. 547 * Think of this function being the union of mpg123_read() and mpg123_feed() (which it actually is, sort of;-). 548 * You can actually always decide if you want those specialized functions in separate steps or one call this one here. 549 * \param inmemory input buffer 550 * \param inmemsize number of input bytes 551 * \param outmemory output buffer 552 * \param outmemsize maximum number of output bytes 553 * \param done address to store the number of actually decoded bytes to 554 * \return error/message code (watch out especially for MPG123_NEED_MORE) 555 */ 556 MPG123_EXPORT int mpg123_decode(mpg123_handle *mh, const unsigned char *inmemory, size_t inmemsize, unsigned char *outmemory, size_t outmemsize, size_t *done); 557 558 /** Decode next MPEG frame to internal buffer 559 * or read a frame and return after setting a new format. 560 * \param num current frame offset gets stored there 561 * \param audio This pointer is set to the internal buffer to read the decoded audio from. 562 * \param bytes number of output bytes ready in the buffer 563 * \return MPG123_OK or error/message code 564 */ 565 MPG123_EXPORT int mpg123_decode_frame(mpg123_handle *mh, off_t *num, unsigned char **audio, size_t *bytes); 566 567 /** Decode current MPEG frame to internal buffer. 568 * Warning: This is experimental API that might change in future releases! 569 * Please watch mpg123 development closely when using it. 570 * \param num last frame offset gets stored there 571 * \param audio this pointer is set to the internal buffer to read the decoded audio from. 572 * \param bytes number of output bytes ready in the buffer 573 * \return MPG123_OK or error/message code 574 */ 575 MPG123_EXPORT int mpg123_framebyframe_decode(mpg123_handle *mh, off_t *num, unsigned char **audio, size_t *bytes); 576 577 /** Find, read and parse the next mp3 frame 578 * Warning: This is experimental API that might change in future releases! 579 * Please watch mpg123 development closely when using it. 580 * \return MPG123_OK or error/message code 581 */ 582 MPG123_EXPORT int mpg123_framebyframe_next(mpg123_handle *mh); 583 584 /** Get access to the raw input data for the last parsed frame. 585 * This gives you a direct look (and write access) to the frame body data. 586 * Together with the raw header, you can reconstruct the whole raw MPEG stream without junk and meta data, or play games by actually modifying the frame body data before decoding this frame (mpg123_framebyframe_decode()). 587 * A more sane use would be to use this for CRC checking (see mpg123_info() and MPG123_CRC), the first two bytes of the body make up the CRC16 checksum, if present. 588 * You can provide NULL for a parameter pointer when you are not interested in the value. 589 * 590 * \param header the 4-byte MPEG header 591 * \param bodydata pointer to the frame body stored in the handle (without the header) 592 * \param bodybytes size of frame body in bytes (without the header) 593 * \return MPG123_OK if there was a yet un-decoded frame to get the 594 * data from, MPG123_BAD_HANDLE or MPG123_ERR otherwise (without further 595 * explanation, the error state of the mpg123_handle is not modified by 596 * this function). 597 */ 598 MPG123_EXPORT int mpg123_framedata(mpg123_handle *mh, unsigned long *header, unsigned char **bodydata, size_t *bodybytes); 599 600 /** Get the input position (byte offset in stream) of the last parsed frame. 601 * This can be used for external seek index building, for example. 602 * It just returns the internally stored offset, regardless of validity -- you ensure that a valid frame has been parsed before! */ 603 MPG123_EXPORT off_t mpg123_framepos(mpg123_handle *mh); 604 605 /*@}*/ 606 607 608 /** \defgroup mpg123_seek mpg123 position and seeking 609 * 610 * Functions querying and manipulating position in the decoded audio bitstream. 611 * The position is measured in decoded audio samples, or MPEG frame offset for the specific functions. 612 * If gapless code is in effect, the positions are adjusted to compensate the skipped padding/delay - meaning, you should not care about that at all and just use the position defined for the samples you get out of the decoder;-) 613 * The general usage is modelled after stdlib's ftell() and fseek(). 614 * Especially, the whence parameter for the seek functions has the same meaning as the one for fseek() and needs the same constants from stdlib.h: 615 * - SEEK_SET: set position to (or near to) specified offset 616 * - SEEK_CUR: change position by offset from now 617 * - SEEK_END: set position to offset from end 618 * 619 * Note that sample-accurate seek only works when gapless support has been enabled at compile time; seek is frame-accurate otherwise. 620 * Also, really sample-accurate seeking (meaning that you get the identical sample value after seeking compared to plain decoding up to the position) is only guaranteed when you do not mess with the position code by using MPG123_UPSPEED, MPG123_DOWNSPEED or MPG123_START_FRAME. The first two mainly should cause trouble with NtoM resampling, but in any case with these options in effect, you have to keep in mind that the sample offset is not the same as counting the samples you get from decoding since mpg123 counts the skipped samples, too (or the samples played twice only once)! 621 * Short: When you care about the sample position, don't mess with those parameters;-) 622 * Also, seeking is not guaranteed to work for all streams (underlying stream may not support it). 623 * And yet another caveat: If the stream is concatenated out of differing pieces (Frankenstein stream), seeking may suffer, too. 624 * 625 * @{ 626 */ 627 628 /** Returns the current position in samples. 629 * On the next successful read, you'd get that sample. 630 * \return sample offset or MPG123_ERR (null handle) 631 */ 632 MPG123_EXPORT off_t mpg123_tell(mpg123_handle *mh); 633 634 /** Returns the frame number that the next read will give you data from. 635 * \return frame offset or MPG123_ERR (null handle) 636 */ 637 MPG123_EXPORT off_t mpg123_tellframe(mpg123_handle *mh); 638 639 /** Returns the current byte offset in the input stream. 640 * \return byte offset or MPG123_ERR (null handle) 641 */ 642 MPG123_EXPORT off_t mpg123_tell_stream(mpg123_handle *mh); 643 644 /** Seek to a desired sample offset. 645 * Set whence to SEEK_SET, SEEK_CUR or SEEK_END. 646 * \return The resulting offset >= 0 or error/message code */ 647 MPG123_EXPORT off_t mpg123_seek(mpg123_handle *mh, off_t sampleoff, int whence); 648 649 /** Seek to a desired sample offset in data feeding mode. 650 * This just prepares things to be right only if you ensure that the next chunk of input data will be from input_offset byte position. 651 * \param input_offset The position it expects to be at the 652 * next time data is fed to mpg123_decode(). 653 * \return The resulting offset >= 0 or error/message code */ 654 MPG123_EXPORT off_t mpg123_feedseek(mpg123_handle *mh, off_t sampleoff, int whence, off_t *input_offset); 655 656 /** Seek to a desired MPEG frame index. 657 * Set whence to SEEK_SET, SEEK_CUR or SEEK_END. 658 * \return The resulting offset >= 0 or error/message code */ 659 MPG123_EXPORT off_t mpg123_seek_frame(mpg123_handle *mh, off_t frameoff, int whence); 660 661 /** Return a MPEG frame offset corresponding to an offset in seconds. 662 * This assumes that the samples per frame do not change in the file/stream, which is a good assumption for any sane file/stream only. 663 * \return frame offset >= 0 or error/message code */ 664 MPG123_EXPORT off_t mpg123_timeframe(mpg123_handle *mh, double sec); 665 666 /** Give access to the frame index table that is managed for seeking. 667 * You are asked not to modify the values... Use mpg123_set_index to set the 668 * seek index 669 * \param offsets pointer to the index array 670 * \param step one index byte offset advances this many MPEG frames 671 * \param fill number of recorded index offsets; size of the array 672 * \return MPG123_OK on success 673 */ 674 MPG123_EXPORT int mpg123_index(mpg123_handle *mh, off_t **offsets, off_t *step, size_t *fill); 675 676 /** Set the frame index table 677 * Setting offsets to NULL and fill > 0 will allocate fill entries. Setting offsets 678 * to NULL and fill to 0 will clear the index and free the allocated memory used by the index. 679 * \param offsets pointer to the index array 680 * \param step one index byte offset advances this many MPEG frames 681 * \param fill number of recorded index offsets; size of the array 682 * \return MPG123_OK on success 683 */ 684 MPG123_EXPORT int mpg123_set_index(mpg123_handle *mh, off_t *offsets, off_t step, size_t fill); 685 686 /** Get information about current and remaining frames/seconds. 687 * WARNING: This function is there because of special usage by standalone mpg123 and may be removed in the final version of libmpg123! 688 * broken for various cases (p.ex. 24 bit output). Do never use. 689 * You provide an offset (in frames) from now and a number of output bytes 690 * served by libmpg123 but not yet played. You get the projected current frame 691 * and seconds, as well as the remaining frames/seconds. This does _not_ care 692 * about skipped samples due to gapless playback. */ 693 MPG123_EXPORT int mpg123_position( mpg123_handle *mh, off_t frame_offset, off_t buffered_bytes, off_t *current_frame, off_t *frames_left, double *current_seconds, double *seconds_left); 694 695 /*@}*/ 696 697 698 /** \defgroup mpg123_voleq mpg123 volume and equalizer 699 * 700 * @{ 701 */ 702 703 enum mpg123_channels 704 { 705 MPG123_LEFT=0x1 /**< The Left Channel. */ 706 ,MPG123_RIGHT=0x2 /**< The Right Channel. */ 707 ,MPG123_LR=0x3 /**< Both left and right channel; same as MPG123_LEFT|MPG123_RIGHT */ 708 }; 709 710 /** Set the 32 Band Audio Equalizer settings. 711 * \param channel Can be MPG123_LEFT, MPG123_RIGHT or MPG123_LEFT|MPG123_RIGHT for both. 712 * \param band The equaliser band to change (from 0 to 31) 713 * \param val The (linear) adjustment factor. 714 * \return MPG123_OK on success 715 */ 716 MPG123_EXPORT int mpg123_eq(mpg123_handle *mh, enum mpg123_channels channel, int band, double val); 717 718 /** Get the 32 Band Audio Equalizer settings. 719 * \param channel Can be MPG123_LEFT, MPG123_RIGHT or MPG123_LEFT|MPG123_RIGHT for (arithmetic mean of) both. 720 * \param band The equaliser band to change (from 0 to 31) 721 * \return The (linear) adjustment factor (zero for pad parameters) */ 722 MPG123_EXPORT double mpg123_geteq(mpg123_handle *mh, enum mpg123_channels channel, int band); 723 724 /** Reset the 32 Band Audio Equalizer settings to flat 725 * \return MPG123_OK on success 726 */ 727 MPG123_EXPORT int mpg123_reset_eq(mpg123_handle *mh); 728 729 /** Set the absolute output volume including the RVA setting, 730 * vol<0 just applies (a possibly changed) RVA setting. */ 731 MPG123_EXPORT int mpg123_volume(mpg123_handle *mh, double vol); 732 733 /** Adjust output volume including the RVA setting by chosen amount */ 734 MPG123_EXPORT int mpg123_volume_change(mpg123_handle *mh, double change); 735 736 /** Return current volume setting, the actual value due to RVA, and the RVA 737 * adjustment itself. It's all as double float value to abstract the sample 738 * format. The volume values are linear factors / amplitudes (not percent) 739 * and the RVA value is in decibels. */ 740 MPG123_EXPORT int mpg123_getvolume(mpg123_handle *mh, double *base, double *really, double *rva_db); 741 742 /* TODO: Set some preamp in addition / to replace internal RVA handling? */ 743 744 /*@}*/ 745 746 747 /** \defgroup mpg123_status mpg123 status and information 748 * 749 * @{ 750 */ 751 752 /** Enumeration of the mode types of Variable Bitrate */ 753 enum mpg123_vbr { 754 MPG123_CBR=0, /**< Constant Bitrate Mode (default) */ 755 MPG123_VBR, /**< Variable Bitrate Mode */ 756 MPG123_ABR /**< Average Bitrate Mode */ 757 }; 758 759 /** Enumeration of the MPEG Versions */ 760 enum mpg123_version { 761 MPG123_1_0=0, /**< MPEG Version 1.0 */ 762 MPG123_2_0, /**< MPEG Version 2.0 */ 763 MPG123_2_5 /**< MPEG Version 2.5 */ 764 }; 765 766 767 /** Enumeration of the MPEG Audio mode. 768 * Only the mono mode has 1 channel, the others have 2 channels. */ 769 enum mpg123_mode { 770 MPG123_M_STEREO=0, /**< Standard Stereo. */ 771 MPG123_M_JOINT, /**< Joint Stereo. */ 772 MPG123_M_DUAL, /**< Dual Channel. */ 773 MPG123_M_MONO /**< Single Channel. */ 774 }; 775 776 777 /** Enumeration of the MPEG Audio flag bits */ 778 enum mpg123_flags { 779 MPG123_CRC=0x1, /**< The bitstream is error protected using 16-bit CRC. */ 780 MPG123_COPYRIGHT=0x2, /**< The bitstream is copyrighted. */ 781 MPG123_PRIVATE=0x4, /**< The private bit has been set. */ 782 MPG123_ORIGINAL=0x8 /**< The bitstream is an original, not a copy. */ 783 }; 784 785 /** Data structure for storing information about a frame of MPEG Audio */ 786 struct mpg123_frameinfo 787 { 788 enum mpg123_version version; /**< The MPEG version (1.0/2.0/2.5). */ 789 int layer; /**< The MPEG Audio Layer (MP1/MP2/MP3). */ 790 long rate; /**< The sampling rate in Hz. */ 791 enum mpg123_mode mode; /**< The audio mode (Mono, Stereo, Joint-stero, Dual Channel). */ 792 int mode_ext; /**< The mode extension bit flag. */ 793 int framesize; /**< The size of the frame (in bytes, including header). */ 794 enum mpg123_flags flags; /**< MPEG Audio flag bits. Just now I realize that it should be declared as int, not enum. It's a bitwise combination of the enum values. */ 795 int emphasis; /**< The emphasis type. */ 796 int bitrate; /**< Bitrate of the frame (kbps). */ 797 int abr_rate; /**< The target average bitrate. */ 798 enum mpg123_vbr vbr; /**< The VBR mode. */ 799 }; 800 801 /** Get frame information about the MPEG audio bitstream and store it in a mpg123_frameinfo structure. 802 * \return MPG123_OK on success 803 */ 804 MPG123_EXPORT int mpg123_info(mpg123_handle *mh, struct mpg123_frameinfo *mi); 805 806 /** Get the safe output buffer size for all cases (when you want to replace the internal buffer) */ 807 MPG123_EXPORT size_t mpg123_safe_buffer(void); 808 809 /** Make a full parsing scan of each frame in the file. ID3 tags are found. An accurate length 810 * value is stored. Seek index will be filled. A seek back to current position 811 * is performed. At all, this function refuses work when stream is 812 * not seekable. 813 * \return MPG123_OK on success 814 */ 815 MPG123_EXPORT int mpg123_scan(mpg123_handle *mh); 816 817 /** Return, if possible, the full (expected) length of current track in frames. 818 * \return length >= 0 or MPG123_ERR if there is no length guess possible. */ 819 MPG123_EXPORT off_t mpg123_framelength(mpg123_handle *mh); 820 821 /** Return, if possible, the full (expected) length of current track in samples. 822 * \return length >= 0 or MPG123_ERR if there is no length guess possible. */ 823 MPG123_EXPORT off_t mpg123_length(mpg123_handle *mh); 824 825 /** Override the value for file size in bytes. 826 * Useful for getting sensible track length values in feed mode or for HTTP streams. 827 * \return MPG123_OK on success 828 */ 829 MPG123_EXPORT int mpg123_set_filesize(mpg123_handle *mh, off_t size); 830 831 /** Returns the time (seconds) per frame; <0 is error. */ 832 MPG123_EXPORT double mpg123_tpf(mpg123_handle *mh); 833 834 /** Returns the samples per frame for the most recently parsed frame; <0 is error. */ 835 MPG123_EXPORT int mpg123_spf(mpg123_handle *mh); 836 837 /** Get and reset the clip count. */ 838 MPG123_EXPORT long mpg123_clip(mpg123_handle *mh); 839 840 841 /** The key values for state information from mpg123_getstate(). */ 842 enum mpg123_state 843 { 844 MPG123_ACCURATE = 1 /**< Query if positons are currently accurate (integer value, 0 if false, 1 if true). */ 845 ,MPG123_BUFFERFILL /**< Get fill of internal (feed) input buffer as integer byte count returned as long and as double. An error is returned on integer overflow while converting to (signed) long, but the returned floating point value shold still be fine. */ 846 ,MPG123_FRANKENSTEIN /**< Stream consists of carelessly stitched together files. Seeking may yield unexpected results (also with MPG123_ACCURATE, it may be confused). */ 847 ,MPG123_FRESH_DECODER /**< Decoder structure has been updated, possibly indicating changed stream (integer value, 0 if false, 1 if true). Flag is cleared after retrieval. */ 848 }; 849 850 /** Get various current decoder/stream state information. 851 * \param key the key to identify the information to give. 852 * \param val the address to return (long) integer values to 853 * \param fval the address to return floating point values to 854 * \return MPG123_OK on success 855 */ 856 MPG123_EXPORT int mpg123_getstate(mpg123_handle *mh, enum mpg123_state key, long *val, double *fval); 857 858 /*@}*/ 859 860 861 /** \defgroup mpg123_metadata mpg123 metadata handling 862 * 863 * Functions to retrieve the metadata from MPEG Audio files and streams. 864 * Also includes string handling functions. 865 * 866 * @{ 867 */ 868 869 /** Data structure for storing strings in a safer way than a standard C-String. 870 * Can also hold a number of null-terminated strings. */ 871 typedef struct 872 { 873 char* p; /**< pointer to the string data */ 874 size_t size; /**< raw number of bytes allocated */ 875 size_t fill; /**< number of used bytes (including closing zero byte) */ 876 } mpg123_string; 877 878 /** Create and allocate memory for a new mpg123_string */ 879 MPG123_EXPORT void mpg123_init_string(mpg123_string* sb); 880 881 /** Free-up mempory for an existing mpg123_string */ 882 MPG123_EXPORT void mpg123_free_string(mpg123_string* sb); 883 884 /** Change the size of a mpg123_string 885 * \return 0 on error, 1 on success */ 886 MPG123_EXPORT int mpg123_resize_string(mpg123_string* sb, size_t news); 887 888 /** Increase size of a mpg123_string if necessary (it may stay larger). 889 * Note that the functions for adding and setting in current libmpg123 use this instead of mpg123_resize_string(). 890 * That way, you can preallocate memory and safely work afterwards with pieces. 891 * \return 0 on error, 1 on success */ 892 MPG123_EXPORT int mpg123_grow_string(mpg123_string* sb, size_t news); 893 894 /** Copy the contents of one mpg123_string string to another. 895 * \return 0 on error, 1 on success */ 896 MPG123_EXPORT int mpg123_copy_string(mpg123_string* from, mpg123_string* to); 897 898 /** Append a C-String to an mpg123_string 899 * \return 0 on error, 1 on success */ 900 MPG123_EXPORT int mpg123_add_string(mpg123_string* sb, const char* stuff); 901 902 /** Append a C-substring to an mpg123 string 903 * \return 0 on error, 1 on success 904 * \param from offset to copy from 905 * \param count number of characters to copy (a null-byte is always appended) */ 906 MPG123_EXPORT int mpg123_add_substring(mpg123_string *sb, const char *stuff, size_t from, size_t count); 907 908 /** Set the conents of a mpg123_string to a C-string 909 * \return 0 on error, 1 on success */ 910 MPG123_EXPORT int mpg123_set_string(mpg123_string* sb, const char* stuff); 911 912 /** Set the contents of a mpg123_string to a C-substring 913 * \return 0 on error, 1 on success 914 * \param from offset to copy from 915 * \param count number of characters to copy (a null-byte is always appended) */ 916 MPG123_EXPORT int mpg123_set_substring(mpg123_string *sb, const char *stuff, size_t from, size_t count); 917 918 /** Count characters in a mpg123 string (non-null bytes or UTF-8 characters). 919 * \return character count 920 * \param sb the string 921 * \param utf8 a flag to tell if the string is in utf8 encoding 922 * Even with the fill property, the character count is not obvious as there could be multiple trailing null bytes. 923 */ 924 MPG123_EXPORT size_t mpg123_strlen(mpg123_string *sb, int utf8); 925 926 /** Remove trailing \r and \n, if present. 927 * \return 0 on error, 1 on success 928 * \param sb the string 929 */ 930 MPG123_EXPORT int mpg123_chomp_string(mpg123_string *sb); 931 932 /** The mpg123 text encodings. This contains encodings we encounter in ID3 tags or ICY meta info. */ 933 enum mpg123_text_encoding 934 { 935 mpg123_text_unknown = 0 /**< Unkown encoding... mpg123_id3_encoding can return that on invalid codes. */ 936 ,mpg123_text_utf8 = 1 /**< UTF-8 */ 937 ,mpg123_text_latin1 = 2 /**< ISO-8859-1. Note that sometimes latin1 in ID3 is abused for totally different encodings. */ 938 ,mpg123_text_icy = 3 /**< ICY metadata encoding, usually CP-1252 but we take it as UTF-8 if it qualifies as such. */ 939 ,mpg123_text_cp1252 = 4 /**< Really CP-1252 without any guessing. */ 940 ,mpg123_text_utf16 = 5 /**< Some UTF-16 encoding. The last of a set of leading BOMs (byte order mark) rules. 941 * When there is no BOM, big endian ordering is used. Note that UCS-2 qualifies as UTF-8 when 942 * you don't mess with the reserved code points. If you want to decode little endian data 943 * without BOM you need to prepend 0xff 0xfe yourself. */ 944 ,mpg123_text_utf16bom = 6 /**< Just an alias for UTF-16, ID3v2 has this as distinct code. */ 945 ,mpg123_text_utf16be = 7 /**< Another alias for UTF16 from ID3v2. Note, that, because of the mess that is reality, 946 * BOMs are used if encountered. There really is not much distinction between the UTF16 types for mpg123 947 * One exception: Since this is seen in ID3v2 tags, leading null bytes are skipped for all other UTF16 948 * types (we expect a BOM before real data there), not so for utf16be!*/ 949 ,mpg123_text_max = 7 /**< Placeholder for the maximum encoding value. */ 950 }; 951 952 /** The encoding byte values from ID3v2. */ 953 enum mpg123_id3_enc 954 { 955 mpg123_id3_latin1 = 0 /**< Note: This sometimes can mean anything in practice... */ 956 ,mpg123_id3_utf16bom = 1 /**< UTF16, UCS-2 ... it's all the same for practical purposes. */ 957 ,mpg123_id3_utf16be = 2 /**< Big-endian UTF-16, BOM see note for mpg123_text_utf16be. */ 958 ,mpg123_id3_utf8 = 3 /**< Our lovely overly ASCII-compatible 8 byte encoding for the world. */ 959 ,mpg123_id3_enc_max = 3 /**< Placeholder to check valid range of encoding byte. */ 960 }; 961 962 /** Convert ID3 encoding byte to mpg123 encoding index. */ 963 MPG123_EXPORT enum mpg123_text_encoding mpg123_enc_from_id3(unsigned char id3_enc_byte); 964 965 /** Store text data in string, after converting to UTF-8 from indicated encoding 966 * \return 0 on error, 1 on success (on error, mpg123_free_string is called on sb) 967 * \param sb target string 968 * \param enc mpg123 text encoding value 969 * \param source source buffer with plain unsigned bytes (you might need to cast from char *) 970 * \param source_size number of bytes in the source buffer 971 * 972 * A prominent error can be that you provided an unknown encoding value, or this build of libmpg123 lacks support for certain encodings (ID3 or ICY stuff missing). 973 * Also, you might want to take a bit of care with preparing the data; for example, strip leading zeroes (I have seen that). 974 */ 975 MPG123_EXPORT int mpg123_store_utf8(mpg123_string *sb, enum mpg123_text_encoding enc, const unsigned char *source, size_t source_size); 976 977 /** Sub data structure for ID3v2, for storing various text fields (including comments). 978 * This is for ID3v2 COMM, TXXX and all the other text fields. 979 * Only COMM and TXXX have a description, only COMM and USLT have a language. 980 * You should consult the ID3v2 specification for the use of the various text fields ("frames" in ID3v2 documentation, I use "fields" here to separate from MPEG frames). */ 981 typedef struct 982 { 983 char lang[3]; /**< Three-letter language code (not terminated). */ 984 char id[4]; /**< The ID3v2 text field id, like TALB, TPE2, ... (4 characters, no string termination). */ 985 mpg123_string description; /**< Empty for the generic comment... */ 986 mpg123_string text; /**< ... */ 987 } mpg123_text; 988 989 /** The picture type values from ID3v2. */ 990 enum mpg123_id3_pic_type 991 { 992 mpg123_id3_pic_other = 0 993 ,mpg123_id3_pic_icon = 1 994 ,mpg123_id3_pic_other_icon = 2 995 ,mpg123_id3_pic_front_cover = 3 996 ,mpg123_id3_pic_back_cover = 4 997 ,mpg123_id3_pic_leaflet = 5 998 ,mpg123_id3_pic_media = 6 999 ,mpg123_id3_pic_lead = 7 1000 ,mpg123_id3_pic_artist = 8 1001 ,mpg123_id3_pic_conductor = 9 1002 ,mpg123_id3_pic_orchestra = 10 1003 ,mpg123_id3_pic_composer = 11 1004 ,mpg123_id3_pic_lyricist = 12 1005 ,mpg123_id3_pic_location = 13 1006 ,mpg123_id3_pic_recording = 14 1007 ,mpg123_id3_pic_performance = 15 1008 ,mpg123_id3_pic_video = 16 1009 ,mpg123_id3_pic_fish = 17 1010 ,mpg123_id3_pic_illustration = 18 1011 ,mpg123_id3_pic_artist_logo = 19 1012 ,mpg123_id3_pic_publisher_logo = 20 1013 }; 1014 1015 /** Sub data structure for ID3v2, for storing picture data including comment. 1016 * This is for the ID3v2 APIC field. You should consult the ID3v2 specification 1017 * for the use of the APIC field ("frames" in ID3v2 documentation, I use "fields" 1018 * here to separate from MPEG frames). */ 1019 typedef struct 1020 { 1021 char type; 1022 mpg123_string description; 1023 mpg123_string mime_type; 1024 size_t size; 1025 unsigned char* data; 1026 } mpg123_picture; 1027 1028 /** Data structure for storing IDV3v2 tags. 1029 * This structure is not a direct binary mapping with the file contents. 1030 * The ID3v2 text frames are allowed to contain multiple strings. 1031 * So check for null bytes until you reach the mpg123_string fill. 1032 * All text is encoded in UTF-8. */ 1033 typedef struct 1034 { 1035 unsigned char version; /**< 3 or 4 for ID3v2.3 or ID3v2.4. */ 1036 mpg123_string *title; /**< Title string (pointer into text_list). */ 1037 mpg123_string *artist; /**< Artist string (pointer into text_list). */ 1038 mpg123_string *album; /**< Album string (pointer into text_list). */ 1039 mpg123_string *year; /**< The year as a string (pointer into text_list). */ 1040 mpg123_string *genre; /**< Genre String (pointer into text_list). The genre string(s) may very well need postprocessing, esp. for ID3v2.3. */ 1041 mpg123_string *comment; /**< Pointer to last encountered comment text with empty description. */ 1042 /* Encountered ID3v2 fields are appended to these lists. 1043 There can be multiple occurences, the pointers above always point to the last encountered data. */ 1044 mpg123_text *comment_list; /**< Array of comments. */ 1045 size_t comments; /**< Number of comments. */ 1046 mpg123_text *text; /**< Array of ID3v2 text fields (including USLT) */ 1047 size_t texts; /**< Numer of text fields. */ 1048 mpg123_text *extra; /**< The array of extra (TXXX) fields. */ 1049 size_t extras; /**< Number of extra text (TXXX) fields. */ 1050 mpg123_picture *picture; /**< Array of ID3v2 pictures fields (APIC). */ 1051 size_t pictures; /**< Number of picture (APIC) fields. */ 1052 } mpg123_id3v2; 1053 1054 /** Data structure for ID3v1 tags (the last 128 bytes of a file). 1055 * Don't take anything for granted (like string termination)! 1056 * Also note the change ID3v1.1 did: comment[28] = 0; comment[29] = track_number 1057 * It is your task to support ID3v1 only or ID3v1.1 ...*/ 1058 typedef struct 1059 { 1060 char tag[3]; /**< Always the string "TAG", the classic intro. */ 1061 char title[30]; /**< Title string. */ 1062 char artist[30]; /**< Artist string. */ 1063 char album[30]; /**< Album string. */ 1064 char year[4]; /**< Year string. */ 1065 char comment[30]; /**< Comment string. */ 1066 unsigned char genre; /**< Genre index. */ 1067 } mpg123_id3v1; 1068 1069 #define MPG123_ID3 0x3 /**< 0011 There is some ID3 info. Also matches 0010 or NEW_ID3. */ 1070 #define MPG123_NEW_ID3 0x1 /**< 0001 There is ID3 info that changed since last call to mpg123_id3. */ 1071 #define MPG123_ICY 0xc /**< 1100 There is some ICY info. Also matches 0100 or NEW_ICY.*/ 1072 #define MPG123_NEW_ICY 0x4 /**< 0100 There is ICY info that changed since last call to mpg123_icy. */ 1073 1074 /** Query if there is (new) meta info, be it ID3 or ICY (or something new in future). 1075 The check function returns a combination of flags. */ 1076 MPG123_EXPORT int mpg123_meta_check(mpg123_handle *mh); /* On error (no valid handle) just 0 is returned. */ 1077 1078 /** Clean up meta data storage (ID3v2 and ICY), freeing memory. */ 1079 MPG123_EXPORT void mpg123_meta_free(mpg123_handle *mh); 1080 1081 /** Point v1 and v2 to existing data structures wich may change on any next read/decode function call. 1082 * v1 and/or v2 can be set to NULL when there is no corresponding data. 1083 * \return MPG123_OK on success 1084 */ 1085 MPG123_EXPORT int mpg123_id3(mpg123_handle *mh, mpg123_id3v1 **v1, mpg123_id3v2 **v2); 1086 1087 /** Point icy_meta to existing data structure wich may change on any next read/decode function call. 1088 * \return MPG123_OK on success 1089 */ 1090 MPG123_EXPORT int mpg123_icy(mpg123_handle *mh, char **icy_meta); /* same for ICY meta string */ 1091 1092 /** Decode from windows-1252 (the encoding ICY metainfo used) to UTF-8. 1093 * Note that this is very similar to mpg123_store_utf8(&sb, mpg123_text_icy, icy_text, strlen(icy_text+1)) . 1094 * \param icy_text The input data in ICY encoding 1095 * \return pointer to newly allocated buffer with UTF-8 data (You free() it!) */ 1096 MPG123_EXPORT char* mpg123_icy2utf8(const char* icy_text); 1097 1098 1099 /* @} */ 1100 1101 1102 /** \defgroup mpg123_advpar mpg123 advanced parameter API 1103 * 1104 * Direct access to a parameter set without full handle around it. 1105 * Possible uses: 1106 * - Influence behaviour of library _during_ initialization of handle (MPG123_VERBOSE). 1107 * - Use one set of parameters for multiple handles. 1108 * 1109 * The functions for handling mpg123_pars (mpg123_par() and mpg123_fmt() 1110 * family) directly return a fully qualified mpg123 error code, the ones 1111 * operating on full handles normally MPG123_OK or MPG123_ERR, storing the 1112 * specific error code itseld inside the handle. 1113 * 1114 * @{ 1115 */ 1116 1117 /** Opaque structure for the libmpg123 decoder parameters. */ 1118 struct mpg123_pars_struct; 1119 1120 /** Opaque structure for the libmpg123 decoder parameters. */ 1121 typedef struct mpg123_pars_struct mpg123_pars; 1122 1123 /** Create a handle with preset parameters. */ 1124 MPG123_EXPORT mpg123_handle *mpg123_parnew(mpg123_pars *mp, const char* decoder, int *error); 1125 1126 /** Allocate memory for and return a pointer to a new mpg123_pars */ 1127 MPG123_EXPORT mpg123_pars *mpg123_new_pars(int *error); 1128 1129 /** Delete and free up memory used by a mpg123_pars data structure */ 1130 MPG123_EXPORT void mpg123_delete_pars(mpg123_pars* mp); 1131 1132 /** Configure mpg123 parameters to accept no output format at all, 1133 * use before specifying supported formats with mpg123_format 1134 * \return MPG123_OK on success 1135 */ 1136 MPG123_EXPORT int mpg123_fmt_none(mpg123_pars *mp); 1137 1138 /** Configure mpg123 parameters to accept all formats 1139 * (also any custom rate you may set) -- this is default. 1140 * \return MPG123_OK on success 1141 */ 1142 MPG123_EXPORT int mpg123_fmt_all(mpg123_pars *mp); 1143 1144 /** Set the audio format support of a mpg123_pars in detail: 1145 \param rate The sample rate value (in Hertz). 1146 \param channels A combination of MPG123_STEREO and MPG123_MONO. 1147 \param encodings A combination of accepted encodings for rate and channels, p.ex MPG123_ENC_SIGNED16|MPG123_ENC_ULAW_8 (or 0 for no support). 1148 \return MPG123_OK on success 1149 */ 1150 MPG123_EXPORT int mpg123_fmt(mpg123_pars *mp, long rate, int channels, int encodings); /* 0 is good, -1 is error */ 1151 1152 /** Check to see if a specific format at a specific rate is supported 1153 * by mpg123_pars. 1154 * \return 0 for no support (that includes invalid parameters), MPG123_STEREO, 1155 * MPG123_MONO or MPG123_STEREO|MPG123_MONO. */ 1156 MPG123_EXPORT int mpg123_fmt_support(mpg123_pars *mp, long rate, int encoding); 1157 1158 /** Set a specific parameter, for a specific mpg123_pars, using a parameter 1159 * type key chosen from the mpg123_parms enumeration, to the specified value. */ 1160 MPG123_EXPORT int mpg123_par(mpg123_pars *mp, enum mpg123_parms type, long value, double fvalue); 1161 1162 /** Get a specific parameter, for a specific mpg123_pars. 1163 * See the mpg123_parms enumeration for a list of available parameters. */ 1164 MPG123_EXPORT int mpg123_getpar(mpg123_pars *mp, enum mpg123_parms type, long *val, double *fval); 1165 1166 /* @} */ 1167 1168 1169 /** \defgroup mpg123_lowio mpg123 low level I/O 1170 * You may want to do tricky stuff with I/O that does not work with mpg123's default file access or you want to make it decode into your own pocket... 1171 * 1172 * @{ */ 1173 1174 /** Replace default internal buffer with user-supplied buffer. 1175 * Instead of working on it's own private buffer, mpg123 will directly use the one you provide for storing decoded audio. 1176 * Note that the required buffer size could be bigger than expected from output 1177 * encoding if libmpg123 has to convert from primary decoder output (p.ex. 32 bit 1178 * storage for 24 bit output. 1179 * \param data pointer to user buffer 1180 * \param size of buffer in bytes 1181 * \return MPG123_OK on success 1182 */ 1183 MPG123_EXPORT int mpg123_replace_buffer(mpg123_handle *mh, unsigned char *data, size_t size); 1184 1185 /** The max size of one frame's decoded output with current settings. 1186 * Use that to determine an appropriate minimum buffer size for decoding one frame. */ 1187 MPG123_EXPORT size_t mpg123_outblock(mpg123_handle *mh); 1188 1189 /** Replace low-level stream access functions; read and lseek as known in POSIX. 1190 * You can use this to make any fancy file opening/closing yourself, 1191 * using mpg123_open_fd() to set the file descriptor for your read/lseek (doesn't need to be a "real" file descriptor...). 1192 * Setting a function to NULL means that the default internal read is 1193 * used (active from next mpg123_open call on). 1194 * Note: As it would be troublesome to mess with this while having a file open, 1195 * this implies mpg123_close(). */ 1196 MPG123_EXPORT int mpg123_replace_reader(mpg123_handle *mh, ssize_t (*r_read) (int, void *, size_t), off_t (*r_lseek)(int, off_t, int)); 1197 1198 /** Replace I/O functions with your own ones operating on some kind of handle instead of integer descriptors. 1199 * The handle is a void pointer, so you can pass any data you want... 1200 * mpg123_open_handle() is the call you make to use the I/O defined here. 1201 * There is no fallback to internal read/seek here. 1202 * Note: As it would be troublesome to mess with this while having a file open, 1203 * this mpg123_close() is implied here. 1204 * \param r_read The callback for reading (behaviour like posix read). 1205 * \param r_lseek The callback for seeking (like posix lseek). 1206 * \param cleanup A callback to clean up an I/O handle on mpg123_close, can be NULL for none (you take care of cleaning your handles). */ 1207 MPG123_EXPORT int 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*)); 1208 1209 /* @} */ 1210 1211 #ifdef __cplusplus 1212 } 1213 #endif 1214 1215 #endif 1216