vd_ffmpeg: compare IMGFMT instead of PIX_FMT
[mplayer/glamo.git] / libmpcodecs / ad_mpg123.c
blob4a8507fe4dbcf2efe684521288ebdf65bc9afe13
1 /*
2 * MPEG 1.0/2.0/2.5 audio layer I, II, III decoding with libmpg123
4 * Copyright (C) 2010 Thomas Orgis <thomas@orgis.org>
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
25 #include "config.h"
27 #include "ad_internal.h"
29 static const ad_info_t info = {
30 "MPEG 1.0/2.0/2.5 layers I, II, III",
31 "mpg123",
32 "Thomas Orgis",
33 "mpg123.org",
34 "High-performance decoder using libmpg123."
37 LIBAD_EXTERN(mpg123)
39 #include "libvo/fastmemcpy.h"
41 /* We avoid any usage of mpg123 API that is sensitive to the large file
42 * support setting. This ensures compatibility with a wide range of libmpg123
43 * installs. This code is intended to work with version 1.0.0 of libmpg123.
45 * Though the chosen API subset is not affected by the choice of large file
46 * support, the mpg123 header (old versions of which) might include a check
47 * for matching _FILE_OFFSET_BITS. Since MPlayer does always define this one
48 * for large file support, we are safe for any default mpg123 install that
49 * either doesn't have such checks or defaults to the large value of
50 * _FILE_OFFSET_BITS .
51 * So, in short: There's no worry unless you have a non-default libmpg123
52 * with intentionally disabled large file support. */
53 /* You might need to #undef _FILE_OFFSET_BITS here on a 64 bit system
54 with released mpg123 1.12 when using callback API. SVN snapshots
55 should work fine. */
56 #include <mpg123.h>
58 /* Selection of mpg123 usage patterns:
59 * AD_MPG123_CALLBACK: Use callback API instead of feeding of memory buffers.
60 * That needs mpg123>=1.12, on x86-64 SVN snapshot because of
61 * _FILE_OFFSET_BITS being defined (see above).
62 * AD_MPG123_PACKET: Use packet-based input (including pts handling).
63 * AD_MPG123_SEEKBUFFER: Use internal mpg123 buffer to enhance stream parsing.
64 * Makes sense with callback API only.
65 * Any of those might affect I/O performance, might be significant compared
66 * to the excessively optimized decoding.
68 /* #define AD_MPG123_CALLBACK */
69 #define AD_MPG123_PACKET
70 /* #define AD_MPG123_SEEKBUFFER */
72 /* Switch for updating bitrate info of VBR files. Not essential. */
73 #define AD_MPG123_MEAN_BITRATE
75 /* Funny thing, that. I assume I shall use it for selecting mpg123 channels.
76 * Please correct me if I guessed wrong. */
77 extern int fakemono;
79 struct ad_mpg123_context {
80 mpg123_handle *handle;
81 #ifdef AD_MPG123_MEAN_BITRATE
82 /* Running mean for bit rate, stream length estimation. */
83 float mean_rate;
84 unsigned int mean_count;
85 /* Time delay for updates. */
86 short delay;
87 #endif
88 /* If the stream is actually VBR. */
89 char vbr;
90 #if (defined AD_MPG123_CALLBACK) && (defined AD_MPG123_PACKET)
91 unsigned char *packet;
92 int packleft;
93 #endif
96 static void context_reset(struct ad_mpg123_context *con)
98 #ifdef AD_MPG123_MEAN_BITRATE
99 con->mean_rate = 0.;
100 con->mean_count = 0;
101 con->delay = 1;
102 #endif
103 #if (defined AD_MPG123_CALLBACK) && (defined AD_MPG123_PACKET)
104 con->packet = NULL;
105 con->packleft = 0;
106 #endif
110 #ifdef AD_MPG123_CALLBACK
111 /* Mpg123 calls that for retrieving data.
112 * This wrapper is at least needed for the call frame (ssize_t vs. int). */
113 static ssize_t read_callback(void *ash, void *buf, size_t count)
115 sh_audio_t *sh = ash;
116 #ifdef AD_MPG123_PACKET
117 struct ad_mpg123_context *con = sh->context;
118 unsigned char *target = buf;
119 int need = count;
120 ssize_t got = 0;
121 while (need > 0) {
122 if (con->packleft > 0) {
123 int get = need > con->packleft ? con->packleft : need;
124 /* Any difference to normal memcpy? */
125 fast_memcpy(target, con->packet, get);
126 /* OK, that does look redundant. */
127 con->packet += get;
128 con->packleft -= get;
129 target += get;
130 need -= get;
131 got += get;
132 } else {
133 double pts;
134 /* Feed more input data. */
135 con->packleft = ds_get_packet_pts(sh->ds, &con->packet, &pts);
136 if (con->packleft <= 0)
137 break; /* Apparently that's it. EOF. */
139 /* Next bytes from that presentation time. */
140 if (pts != MP_NOPTS_VALUE) {
141 sh->pts = pts;
142 sh->pts_bytes = 0;
146 return got;
147 #else
148 /* It returns int... with the meaning of byte count. */
149 return (ssize_t) demux_read_data(sh->ds, buf, count);
150 #endif
153 /* Arbitrary input seeking is not supported with this MPlayer API(?).
154 That also means that we won't read any ID3v1 tags. */
155 static off_t seek_callback(void *sh, off_t pos, int whence)
157 return -1;
159 #endif
161 /* This initializes libmpg123 and prepares the handle, including funky
162 * parameters. */
163 static int preinit(sh_audio_t *sh)
165 int err, flag;
166 struct ad_mpg123_context *con;
167 /* Assumption: You always call preinit + init + uninit, on every file.
168 * But you stop at preinit in case it fails.
169 * If that is not true, one must ensure not to call mpg123_init / exit
170 * twice in a row. */
171 if (mpg123_init() != MPG123_OK)
172 return 0;
174 sh->context = malloc(sizeof(struct ad_mpg123_context));
175 con = sh->context;
176 context_reset(con);
178 /* Auto-choice of optimized decoder (first argument NULL). */
179 con->handle = mpg123_new(NULL, &err);
180 if (!con->handle)
181 goto bad_end;
183 #ifdef CONFIG_FAKE_MONO
184 /* Guessing here: Default value triggers forced upmix of mono to stereo. */
185 flag = fakemono == 0 ? MPG123_FORCE_STEREO :
186 fakemono == 1 ? MPG123_MONO_LEFT :
187 fakemono == 2 ? MPG123_MONO_RIGHT : 0;
188 if (mpg123_param(con->handle, MPG123_ADD_FLAGS, flag, 0.0) != MPG123_OK)
189 goto bad_end;
190 #endif
191 #ifdef AD_MPG123_CALLBACK
192 /* The I/O is handled via callbacks to MPlayer stream functions,
193 * actually only the reading, as general seeking does not seem to be available */
194 if (mpg123_replace_reader_handle(con->handle, read_callback,
195 seek_callback, NULL) != MPG123_OK) {
196 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 error: %s\n",
197 mpg123_strerror(con->handle));
198 mpg123_exit();
199 return 0;
201 #endif
203 /* Basic settings.
204 * Don't spill messages, enable better resync with non-seekable streams.
205 * Give both flags individually without error checking to keep going with
206 * old libmpg123. Generally, it is not fatal if the flags are not
207 * honored */
208 mpg123_param(con->handle, MPG123_ADD_FLAGS, MPG123_QUIET, 0.0);
209 /* Old headers don't know MPG123_SEEKBUFFER yet, so use the plain 0x100. */
210 #ifdef AD_MPG123_SEEKBUFFER
211 mpg123_param(con->handle, MPG123_ADD_FLAGS, 0x100, 0.0);
212 #endif
213 /* Do not bail out on malformed streams at all.
214 * MPlayer does not handle a decoder throwing the towel on crappy input. */
215 mpg123_param(con->handle, MPG123_RESYNC_LIMIT, -1, 0.0);
217 /* Open decisions: Configure libmpg123 to force encoding (or stay open about
218 * library builds that support only float or int32 output), (de)configure
219 * gapless decoding (won't work with seeking in MPlayer, though).
220 * Don't forget to eventually enable ReplayGain/RVA support, too.
221 * Let's try to run with the default for now. */
223 /* Example for RVA choice (available since libmpg123 1.0.0):
224 mpg123_param(con->handle, MPG123_RVA, MPG123_RVA_MIX, 0.0) */
226 return 1;
228 bad_end:
229 if (!con->handle)
230 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 preinit error: %s\n",
231 mpg123_plain_strerror(err));
232 else
233 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 preinit error: %s\n",
234 mpg123_strerror(con->handle));
236 if (con->handle)
237 mpg123_delete(con->handle);
238 mpg123_exit();
239 free(sh->context);
240 sh->context = NULL;
241 return 0;
244 /* Compute bitrate from frame size. */
245 static int compute_bitrate(struct mpg123_frameinfo *i)
247 static const int samples_per_frame[4][4] = {
248 {-1, 384, 1152, 1152}, /* MPEG 1 */
249 {-1, 384, 1152, 576}, /* MPEG 2 */
250 {-1, 384, 1152, 576}, /* MPEG 2.5 */
251 {-1, -1, -1, -1}, /* Unknown */
253 return (int) ((i->framesize + 4) * 8 * i->rate * 0.001 /
254 samples_per_frame[i->version][i->layer] + 0.5);
257 /* Opted against the header printout from old mp3lib, too much
258 * irrelevant info. This is modelled after the mpg123 app's
259 * standard output line.
260 * If more verbosity is demanded, one can add more detail and
261 * also throw in ID3v2 info which libmpg123 collects anyway. */
262 static void print_header_compact(struct mpg123_frameinfo *i)
264 static const char *smodes[5] = {
265 "stereo", "joint-stereo", "dual-channel", "mono", "invalid"
267 static const char *layers[4] = {
268 "Unknown", "I", "II", "III"
270 static const char *versions[4] = {
271 "1.0", "2.0", "2.5", "x.x"
274 mp_msg(MSGT_DECAUDIO, MSGL_V, "MPEG %s layer %s, ",
275 versions[i->version], layers[i->layer]);
276 switch (i->vbr) {
277 case MPG123_CBR:
278 if (i->bitrate)
279 mp_msg(MSGT_DECAUDIO, MSGL_V, "%d kbit/s", i->bitrate);
280 else
281 mp_msg(MSGT_DECAUDIO, MSGL_V, "%d kbit/s (free format)",
282 compute_bitrate(i));
283 break;
284 case MPG123_VBR:
285 mp_msg(MSGT_DECAUDIO, MSGL_V, "VBR");
286 break;
287 case MPG123_ABR:
288 mp_msg(MSGT_DECAUDIO, MSGL_V, "%d kbit/s ABR", i->abr_rate);
289 break;
290 default:
291 mp_msg(MSGT_DECAUDIO, MSGL_V, "???");
293 mp_msg(MSGT_DECAUDIO, MSGL_V, ", %ld Hz %s\n", i->rate,
294 smodes[i->mode]);
297 #ifndef AD_MPG123_CALLBACK
298 /* This tries to extract a requested amount of decoded data.
299 * Even when you request 0 bytes, it will feed enough input so that
300 * the decoder _could_ have delivered something.
301 * Returns byte count >= 0, -1 on error.
303 * Thoughts on exact pts keeping:
304 * We have to assume that MPEG frames are cut in pieces by packet boundaries.
305 * Also, it might be possible that the first packet does not contain enough
306 * data to ensure initial stream sync... or re-sync on erroneous streams.
307 * So we need something robust to relate the decoded byte count to the correct
308 * time stamp. This is tricky, though. From the outside, you cannot tell if,
309 * after having fed two packets until the first output arrives, one should
310 * start counting from the first packet's pts or the second packet's.
311 * So, let's just count from the last fed package's pts. If the packets are
312 * exactly cut to MPEG frames, this will cause one frame mismatch in the
313 * beginning (when mpg123 peeks ahead for the following header), but will
314 * be corrected with the third frame already. One might add special code to
315 * not increment the base pts past the first packet's after a resync before
316 * the first decoded bytes arrived. */
317 static int decode_a_bit(sh_audio_t *sh, unsigned char *buf, int count)
319 int ret = MPG123_OK;
320 int got = 0;
321 struct ad_mpg123_context *con = sh->context;
323 /* There will be one MPG123_NEW_FORMAT message on first open.
324 * This will be implicitly handled in reopen_stream(). */
325 do {
326 size_t got_now = 0;
327 ret = mpg123_decode(con->handle, NULL, 0, buf + got, count - got,
328 &got_now);
329 got += got_now;
330 #ifdef AD_MPG123_PACKET
331 sh->pts_bytes += got_now;
332 #endif
334 if (ret == MPG123_NEED_MORE) {
335 int incount;
336 #ifdef AD_MPG123_PACKET
337 double pts;
338 unsigned char *inbuf;
339 /* Feed more input data. */
340 incount = ds_get_packet_pts(sh->ds, &inbuf, &pts);
341 if (incount <= 0)
342 break; /* Apparently that's it. EOF. */
344 /* Next bytes from that presentation time. */
345 if (pts != MP_NOPTS_VALUE) {
346 sh->pts = pts;
347 sh->pts_bytes = 0;
349 #else
350 const int inbufsize = 4096;
351 unsigned char inbuf[inbufsize];
352 /* Feed more input data. */
353 incount = demux_read_data(((sh_audio_t *) sh)->ds,
354 inbuf, inbufsize);
355 if (incount == 0)
356 break; /* Apparently that's it. EOF. */
357 #endif
359 /* Do not use mpg123_feed(), added in later libmpg123 versions. */
360 ret = mpg123_decode(con->handle, inbuf, incount, NULL, 0, NULL);
361 /* Return value is checked in the loop condition.
362 * It could be MPG12_OK now, it could need more. */
364 /* Older mpg123 versions might indicate MPG123_DONE, so be prepared. */
365 else if (ret == MPG123_ERR || ret == MPG123_DONE)
366 break;
368 } while (ret == MPG123_NEED_MORE || got < count);
370 if (ret == MPG123_ERR) {
371 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 decoding failed: %s\n",
372 mpg123_strerror(con->handle));
373 mpg123_close(con->handle);
374 return -1;
377 return got;
379 #endif
381 /* Close, reopen stream. Feed data until we know the format of the stream.
382 * 1 on success, 0 on error */
383 static int reopen_stream(sh_audio_t *sh)
385 long rate;
386 int chan, enc;
387 struct ad_mpg123_context *con = (struct ad_mpg123_context*) sh->context;
389 mpg123_close(con->handle);
390 context_reset(con);
392 #ifdef AD_MPG123_CALLBACK
393 if (MPG123_OK == mpg123_open_handle(con->handle, sh) &&
394 #else
395 if (/* Open and make sure we have fed enough data to get stream properties. */
396 MPG123_OK == mpg123_open_feed(con->handle) &&
397 /* Feed data until mpg123 is ready (has found stream beginning). */
398 !decode_a_bit(sh, NULL, 0) &&
399 #endif
400 /* Not handing NULL pointers for compatibility with old libmpg123. */
401 MPG123_OK == mpg123_getformat(con->handle, &rate, &chan, &enc)) {
402 return 1;
403 } else {
404 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
405 "mpg123 failed to reopen stream: %s\n",
406 mpg123_strerror(con->handle));
407 mpg123_close(con->handle);
408 return 0;
412 /* Now we really start accessing some data and determining file format.
413 * Paranoia note: The mpg123_close() on errors is not really necessary,
414 * But it ensures that we don't accidentally continue decoding with a
415 * bad state (possibly interpreting the format badly or whatnot). */
416 static int init(sh_audio_t *sh)
418 long rate = 0;
419 int channels = 0;
420 int encoding = 0;
421 mpg123_id3v2 *v2;
422 struct mpg123_frameinfo finfo;
423 struct ad_mpg123_context *con = sh->context;
425 /* We're open about any output format that libmpg123 will suggest.
426 * Note that a standard build will always default to 16 bit signed and
427 * the native sample rate of the file. */
428 if (MPG123_OK == mpg123_format_all(con->handle) &&
429 reopen_stream(sh) &&
430 MPG123_OK == mpg123_getformat(con->handle, &rate, &channels, &encoding) &&
431 /* Forbid the format to change later on. */
432 MPG123_OK == mpg123_format_none(con->handle) &&
433 MPG123_OK == mpg123_format(con->handle, rate, channels, encoding) &&
434 /* Get MPEG header info. */
435 MPG123_OK == mpg123_info(con->handle, &finfo) &&
436 /* Since we queried format, mpg123 should have read past ID3v2 tags.
437 * We need to decide if printing of UTF-8 encoded text info is wanted. */
438 MPG123_OK == mpg123_id3(con->handle, NULL, &v2)) {
439 /* If we are here, we passed all hurdles. Yay! Extract the info. */
440 print_header_compact(&finfo);
441 /* Do we want to print out the UTF-8 Id3v2 info?
442 if (v2)
443 print_id3v2(v2); */
445 /* Have kb/s, want B/s
446 * For VBR, the first frame will be a bad estimate. */
447 sh->i_bps = (finfo.bitrate ? finfo.bitrate : compute_bitrate(&finfo))
448 * 1000 / 8;
449 context_reset(con);
450 con->vbr = (finfo.vbr != MPG123_CBR);
451 sh->channels = channels;
452 sh->samplerate = rate;
453 /* Without external force, mpg123 will always choose signed encoding,
454 * and non-16-bit only on builds that don't support it.
455 * Be reminded that it doesn't matter to the MPEG file what encoding
456 * is produced from it. */
457 switch (encoding) {
458 case MPG123_ENC_SIGNED_8:
459 sh->sample_format = AF_FORMAT_S8;
460 sh->samplesize = 1;
461 break;
462 case MPG123_ENC_SIGNED_16:
463 sh->sample_format = AF_FORMAT_S16_NE;
464 sh->samplesize = 2;
465 break;
466 /* To stay compatible with the oldest libmpg123 headers, do not rely
467 * on float and 32 bit encoding symbols being defined.
468 * Those formats came later */
469 case 0x1180: /* MPG123_ENC_SIGNED_32 */
470 sh->sample_format = AF_FORMAT_S32_NE;
471 sh->samplesize = 4;
472 break;
473 case 0x200: /* MPG123_ENC_FLOAT_32 */
474 sh->sample_format = AF_FORMAT_FLOAT_NE;
475 sh->samplesize = 4;
476 break;
477 default:
478 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
479 "Bad encoding from mpg123: %i.\n", encoding);
480 mpg123_close(con->handle);
481 return 0;
484 return 1;
485 } else {
486 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 init error: %s\n",
487 mpg123_strerror(con->handle));
488 mpg123_close(con->handle);
489 return 0;
493 static void uninit(sh_audio_t *sh)
495 struct ad_mpg123_context *con = (struct ad_mpg123_context*) sh->context;
497 mpg123_close(con->handle);
498 mpg123_delete(con->handle);
499 free(sh->context);
500 sh->context = NULL;
501 mpg123_exit();
504 #ifdef AD_MPG123_MEAN_BITRATE
505 /* Update mean bitrate. This could be dropped if accurate time display
506 * on audio file playback is not desired. */
507 static void update_info(sh_audio_t *sh)
509 struct ad_mpg123_context *con = sh->context;
510 if (con->vbr && --con->delay < 1) {
511 struct mpg123_frameinfo finfo;
512 if (MPG123_OK == mpg123_info(con->handle, &finfo)) {
513 if (++con->mean_count > ((unsigned int) -1) / 2)
514 con->mean_count = ((unsigned int) -1) / 4;
516 /* Might not be numerically optimal, but works fine enough. */
517 con->mean_rate = ((con->mean_count - 1) * con->mean_rate +
518 finfo.bitrate) / con->mean_count;
519 sh->i_bps = (int) (con->mean_rate * 1000 / 8);
521 con->delay = 10;
525 #endif
527 static int decode_audio(sh_audio_t *sh, unsigned char *buf, int minlen,
528 int maxlen)
530 int bytes;
532 #ifdef AD_MPG123_CALLBACK
533 struct ad_mpg123_context *con = sh->context;
534 size_t got_bytes = 0;
535 if (MPG123_ERR == mpg123_read(con->handle, buf, minlen, &got_bytes)) {
536 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Decoding error in mpg123: %s\n",
537 mpg123_strerror(con->handle));
538 return -1;
540 #ifdef AD_MPG123_PACKET
541 sh->pts_bytes += got_bytes;
542 #endif
543 bytes = got_bytes;
544 #else
545 bytes = decode_a_bit(sh, buf, minlen);
546 #endif
548 if (bytes == 0)
549 return -1; /* EOF */
551 #ifdef AD_MPG123_MEAN_BITRATE
552 update_info(sh);
553 #endif
555 return bytes;
558 static int control(sh_audio_t *sh, int cmd, void *arg, ...)
560 switch (cmd) {
561 case ADCTRL_RESYNC_STREAM:
562 /* Close/reopen the stream for mpg123 to make sure it doesn't
563 * think that it still knows the exact stream position.
564 * Otherwise, we would have funny effects from the gapless code.
565 * Oh, and it helps to minimize artifacts from jumping in the stream. */
566 if (reopen_stream(sh)) {
567 #ifdef AD_MPG123_MEAN_BITRATE
568 update_info(sh);
569 #endif
570 return CONTROL_TRUE;
571 } else {
572 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
573 "mpg123 cannot reopen stream for resync.\n");
574 return CONTROL_FALSE;
576 break;
578 return CONTROL_UNKNOWN;