demux_lmlm4: avoid printing error from check_file()
[mplayer.git] / libmpcodecs / ad_mpg123.c
blob21173a8a8f95edac0205e6091a7732f73ce2d224
1 /*
2 * MPEG 1.0/2.0/2.5 audio layer I, II, III decoding with libmpg123
4 * Copyright (C) 2010-2012 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 /* Reducing the ifdeffery to two main variants:
42 * 1. most compatible to any libmpg123 version
43 * 2. fastest variant with recent libmpg123 (>=1.14)
44 * Running variant 2 on older libmpg123 versions may work in
45 * principle, but is not supported.
46 * So, please leave the check for MPG123_API_VERSION there, m-kay?
48 #include <mpg123.h>
50 /* Enable faster mode of operation with newer libmpg123, avoiding
51 * unnecessary memcpy() calls. */
52 #if (defined MPG123_API_VERSION) && (MPG123_API_VERSION >= 33)
53 #define AD_MPG123_FRAMEWISE
54 #endif
56 /* Switch for updating bitrate info of VBR files. Not essential. */
57 #define AD_MPG123_MEAN_BITRATE
59 /* Funny thing, that. I assume I shall use it for selecting mpg123 channels.
60 * Please correct me if I guessed wrong. */
61 extern int fakemono;
63 struct ad_mpg123_context {
64 mpg123_handle *handle;
65 #ifdef AD_MPG123_MEAN_BITRATE
66 /* Running mean for bit rate, stream length estimation. */
67 float mean_rate;
68 unsigned int mean_count;
69 /* Time delay for updates. */
70 short delay;
71 #endif
72 /* If the stream is actually VBR. */
73 char vbr;
76 /* This initializes libmpg123 and prepares the handle, including funky
77 * parameters. */
78 static int preinit(sh_audio_t *sh)
80 int err, flag;
81 struct ad_mpg123_context *con;
82 /* Assumption: You always call preinit + init + uninit, on every file.
83 * But you stop at preinit in case it fails.
84 * If that is not true, one must ensure not to call mpg123_init / exit
85 * twice in a row. */
86 if (mpg123_init() != MPG123_OK)
87 return 0;
89 sh->context = malloc(sizeof(struct ad_mpg123_context));
90 con = sh->context;
91 /* Auto-choice of optimized decoder (first argument NULL). */
92 con->handle = mpg123_new(NULL, &err);
93 if (!con->handle)
94 goto bad_end;
96 #ifdef CONFIG_FAKE_MONO
97 /* Guessing here: Default value triggers forced upmix of mono to stereo. */
98 flag = fakemono == 0 ? MPG123_FORCE_STEREO :
99 fakemono == 1 ? MPG123_MONO_LEFT :
100 fakemono == 2 ? MPG123_MONO_RIGHT : 0;
101 if (mpg123_param(con->handle, MPG123_ADD_FLAGS, flag, 0.0) != MPG123_OK)
102 goto bad_end;
103 #endif
105 /* Basic settings.
106 * Don't spill messages, enable better resync with non-seekable streams.
107 * Give both flags individually without error checking to keep going with
108 * old libmpg123. Generally, it is not fatal if the flags are not
109 * honored */
110 mpg123_param(con->handle, MPG123_ADD_FLAGS, MPG123_QUIET, 0.0);
111 /* Do not bail out on malformed streams at all.
112 * MPlayer does not handle a decoder throwing the towel on crappy input. */
113 mpg123_param(con->handle, MPG123_RESYNC_LIMIT, -1, 0.0);
115 /* Open decisions: Configure libmpg123 to force encoding (or stay open about
116 * library builds that support only float or int32 output), (de)configure
117 * gapless decoding (won't work with seeking in MPlayer, though).
118 * Don't forget to eventually enable ReplayGain/RVA support, too.
119 * Let's try to run with the default for now. */
121 /* That would produce floating point output.
122 * You can get 32 and 24 bit ints, even 8 bit via format matrix. */
123 /* mpg123_param(con->handle, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT, 0.); */
125 /* Example for RVA choice (available since libmpg123 1.0.0):
126 mpg123_param(con->handle, MPG123_RVA, MPG123_RVA_MIX, 0.0) */
128 #ifdef AD_MPG123_FRAMEWISE
129 /* Prevent funky automatic resampling.
130 * This way, we can be sure that one frame will never produce
131 * more than 1152 stereo samples. */
132 mpg123_param(con->handle, MPG123_REMOVE_FLAGS, MPG123_AUTO_RESAMPLE, 0.);
133 #else
134 /* Older mpg123 is vulnerable to concatenated streams when gapless cutting
135 * is enabled (will only play the jingle of a badly constructed radio
136 * stream). The versions using framewise decoding are fine with that. */
137 mpg123_param(con->handle, MPG123_REMOVE_FLAGS, MPG123_GAPLESS, 0.);
138 #endif
140 return 1;
142 bad_end:
143 if (!con->handle)
144 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 preinit error: %s\n",
145 mpg123_plain_strerror(err));
146 else
147 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 preinit error: %s\n",
148 mpg123_strerror(con->handle));
150 if (con->handle)
151 mpg123_delete(con->handle);
152 mpg123_exit();
153 free(sh->context);
154 sh->context = NULL;
155 return 0;
158 /* Compute bitrate from frame size. */
159 static int compute_bitrate(struct mpg123_frameinfo *i)
161 static const int samples_per_frame[4][4] = {
162 {-1, 384, 1152, 1152}, /* MPEG 1 */
163 {-1, 384, 1152, 576}, /* MPEG 2 */
164 {-1, 384, 1152, 576}, /* MPEG 2.5 */
165 {-1, -1, -1, -1}, /* Unknown */
167 return (int) ((i->framesize + 4) * 8 * i->rate * 0.001 /
168 samples_per_frame[i->version][i->layer] + 0.5);
171 /* Opted against the header printout from old mp3lib, too much
172 * irrelevant info. This is modelled after the mpg123 app's
173 * standard output line.
174 * If more verbosity is demanded, one can add more detail and
175 * also throw in ID3v2 info which libmpg123 collects anyway. */
176 static void print_header_compact(struct mpg123_frameinfo *i)
178 static const char *smodes[5] = {
179 "stereo", "joint-stereo", "dual-channel", "mono", "invalid"
181 static const char *layers[4] = {
182 "Unknown", "I", "II", "III"
184 static const char *versions[4] = {
185 "1.0", "2.0", "2.5", "x.x"
188 mp_msg(MSGT_DECAUDIO, MSGL_V, "MPEG %s layer %s, ",
189 versions[i->version], layers[i->layer]);
190 switch (i->vbr) {
191 case MPG123_CBR:
192 if (i->bitrate)
193 mp_msg(MSGT_DECAUDIO, MSGL_V, "%d kbit/s", i->bitrate);
194 else
195 mp_msg(MSGT_DECAUDIO, MSGL_V, "%d kbit/s (free format)",
196 compute_bitrate(i));
197 break;
198 case MPG123_VBR:
199 mp_msg(MSGT_DECAUDIO, MSGL_V, "VBR");
200 break;
201 case MPG123_ABR:
202 mp_msg(MSGT_DECAUDIO, MSGL_V, "%d kbit/s ABR", i->abr_rate);
203 break;
204 default:
205 mp_msg(MSGT_DECAUDIO, MSGL_V, "???");
207 mp_msg(MSGT_DECAUDIO, MSGL_V, ", %ld Hz %s\n", i->rate,
208 smodes[i->mode]);
211 /* This tries to extract a requested amount of decoded data.
212 * Even when you request 0 bytes, it will feed enough input so that
213 * the decoder _could_ have delivered something.
214 * Returns byte count >= 0, -1 on error.
216 * Thoughts on exact pts keeping:
217 * We have to assume that MPEG frames are cut in pieces by packet boundaries.
218 * Also, it might be possible that the first packet does not contain enough
219 * data to ensure initial stream sync... or re-sync on erroneous streams.
220 * So we need something robust to relate the decoded byte count to the correct
221 * time stamp. This is tricky, though. From the outside, you cannot tell if,
222 * after having fed two packets until the first output arrives, one should
223 * start counting from the first packet's pts or the second packet's.
224 * So, let's just count from the last fed package's pts. If the packets are
225 * exactly cut to MPEG frames, this will cause one frame mismatch in the
226 * beginning (when mpg123 peeks ahead for the following header), but will
227 * be corrected with the third frame already. One might add special code to
228 * not increment the base pts past the first packet's after a resync before
229 * the first decoded bytes arrived. */
230 static int decode_a_bit(sh_audio_t *sh, unsigned char *buf, int count)
232 int ret = MPG123_OK;
233 int got = 0;
234 struct ad_mpg123_context *con = sh->context;
236 /* There will be one MPG123_NEW_FORMAT message on first open.
237 * This will be handled in init(). */
238 do {
239 size_t got_now = 0;
241 /* Feed the decoder. This will only fire from the second round on. */
242 if (ret == MPG123_NEED_MORE) {
243 int incount;
244 double pts;
245 unsigned char *inbuf;
246 /* Feed more input data. */
247 incount = ds_get_packet_pts(sh->ds, &inbuf, &pts);
248 if (incount <= 0)
249 break; /* Apparently that's it. EOF. */
251 /* Next bytes from that presentation time. */
252 if (pts != MP_NOPTS_VALUE) {
253 sh->pts = pts;
254 sh->pts_bytes = 0;
257 #ifdef AD_MPG123_FRAMEWISE
258 /* Have to use mpg123_feed() to avoid decoding here. */
259 ret = mpg123_feed(con->handle, inbuf, incount);
260 #else
261 /* Do not use mpg123_feed(), added in later libmpg123 versions. */
262 ret = mpg123_decode(con->handle, inbuf, incount, NULL, 0, NULL);
263 #endif
264 if (ret == MPG123_ERR)
265 break;
267 /* Theoretically, mpg123 could return MPG123_DONE, so be prepared.
268 * Should not happen in our usage, but it is a valid return code. */
269 else if (ret == MPG123_ERR || ret == MPG123_DONE)
270 break;
272 /* Try to decode a bit. This is the return value that counts
273 * for the loop condition. */
274 #ifdef AD_MPG123_FRAMEWISE
275 if (!buf) { /* fake call just for feeding to get format */
276 ret = mpg123_getformat(con->handle, NULL, NULL, NULL);
277 } else { /* This is the decoding. One frame at a time. */
278 ret = mpg123_replace_buffer(con->handle, buf, count);
279 if (ret == MPG123_OK)
280 ret = mpg123_decode_frame(con->handle, NULL, NULL, &got_now);
282 #else
283 ret = mpg123_decode(con->handle, NULL, 0, buf + got, count - got,
284 &got_now);
285 #endif
287 got += got_now;
288 sh->pts_bytes += got_now;
290 #ifdef AD_MPG123_FRAMEWISE
291 } while (ret == MPG123_NEED_MORE || (got == 0 && count != 0));
292 #else
293 } while (ret == MPG123_NEED_MORE || got < count);
294 #endif
296 if (ret == MPG123_ERR) {
297 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 decoding failed: %s\n",
298 mpg123_strerror(con->handle));
299 mpg123_close(con->handle);
300 return -1;
303 return got;
306 /* Close, reopen stream. Feed data until we know the format of the stream.
307 * 1 on success, 0 on error */
308 static int reopen_stream(sh_audio_t *sh)
310 struct ad_mpg123_context *con = (struct ad_mpg123_context*) sh->context;
312 mpg123_close(con->handle);
313 /* No resetting of the context:
314 * We do not want to loose the mean bitrate data. */
316 /* Open and make sure we have fed enough data to get stream properties. */
317 if (MPG123_OK == mpg123_open_feed(con->handle) &&
318 /* Feed data until mpg123 is ready (has found stream beginning). */
319 !decode_a_bit(sh, NULL, 0)) {
320 return 1;
321 } else {
322 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
323 "mpg123 failed to reopen stream: %s\n",
324 mpg123_strerror(con->handle));
325 mpg123_close(con->handle);
326 return 0;
330 /* Now we really start accessing some data and determining file format.
331 * Paranoia note: The mpg123_close() on errors is not really necessary,
332 * But it ensures that we don't accidentally continue decoding with a
333 * bad state (possibly interpreting the format badly or whatnot). */
334 static int init(sh_audio_t *sh)
336 long rate = 0;
337 int channels = 0;
338 int encoding = 0;
339 mpg123_id3v2 *v2;
340 struct mpg123_frameinfo finfo;
341 struct ad_mpg123_context *con = sh->context;
343 /* We're open about any output format that libmpg123 will suggest.
344 * Note that a standard build will always default to 16 bit signed and
345 * the native sample rate of the file. */
346 if (MPG123_OK == mpg123_format_all(con->handle) &&
347 reopen_stream(sh) &&
348 MPG123_OK == mpg123_getformat(con->handle, &rate, &channels, &encoding) &&
349 /* Forbid the format to change later on. */
350 MPG123_OK == mpg123_format_none(con->handle) &&
351 MPG123_OK == mpg123_format(con->handle, rate, channels, encoding) &&
352 /* Get MPEG header info. */
353 MPG123_OK == mpg123_info(con->handle, &finfo) &&
354 /* Since we queried format, mpg123 should have read past ID3v2 tags.
355 * We need to decide if printing of UTF-8 encoded text info is wanted. */
356 MPG123_OK == mpg123_id3(con->handle, NULL, &v2)) {
357 /* If we are here, we passed all hurdles. Yay! Extract the info. */
358 print_header_compact(&finfo);
359 /* Do we want to print out the UTF-8 Id3v2 info?
360 if (v2)
361 print_id3v2(v2); */
363 /* Have kb/s, want B/s
364 * For VBR, the first frame will be a bad estimate. */
365 sh->i_bps = (finfo.bitrate ? finfo.bitrate : compute_bitrate(&finfo))
366 * 1000 / 8;
367 #ifdef AD_MPG123_MEAN_BITRATE
368 con->delay = 1;
369 con->mean_rate = 0.;
370 con->mean_count = 0;
371 #endif
372 con->vbr = (finfo.vbr != MPG123_CBR);
373 sh->channels = channels;
374 sh->samplerate = rate;
375 /* Without external force, mpg123 will always choose signed encoding,
376 * and non-16-bit only on builds that don't support it.
377 * Be reminded that it doesn't matter to the MPEG file what encoding
378 * is produced from it. */
379 switch (encoding) {
380 case MPG123_ENC_SIGNED_8:
381 sh->sample_format = AF_FORMAT_S8;
382 sh->samplesize = 1;
383 break;
384 case MPG123_ENC_SIGNED_16:
385 sh->sample_format = AF_FORMAT_S16_NE;
386 sh->samplesize = 2;
387 break;
388 /* To stay compatible with the oldest libmpg123 headers, do not rely
389 * on float and 32 bit encoding symbols being defined.
390 * Those formats came later */
391 case 0x1180: /* MPG123_ENC_SIGNED_32 */
392 sh->sample_format = AF_FORMAT_S32_NE;
393 sh->samplesize = 4;
394 break;
395 case 0x200: /* MPG123_ENC_FLOAT_32 */
396 sh->sample_format = AF_FORMAT_FLOAT_NE;
397 sh->samplesize = 4;
398 break;
399 default:
400 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
401 "Bad encoding from mpg123: %i.\n", encoding);
402 mpg123_close(con->handle);
403 return 0;
405 #ifdef AD_MPG123_FRAMEWISE
406 /* Going to decode directly to MPlayer's memory. It is important
407 * to have MPG123_AUTO_RESAMPLE disabled for the buffer size
408 * being an all-time limit. */
409 sh->audio_out_minsize = 1152 * 2 * sh->samplesize;
410 #endif
412 return 1;
413 } else {
414 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 init error: %s\n",
415 mpg123_strerror(con->handle));
416 mpg123_close(con->handle);
417 return 0;
421 static void uninit(sh_audio_t *sh)
423 struct ad_mpg123_context *con = (struct ad_mpg123_context*) sh->context;
425 mpg123_close(con->handle);
426 mpg123_delete(con->handle);
427 free(sh->context);
428 sh->context = NULL;
429 mpg123_exit();
432 #ifdef AD_MPG123_MEAN_BITRATE
433 /* Update mean bitrate. This could be dropped if accurate time display
434 * on audio file playback is not desired. */
435 static void update_info(sh_audio_t *sh)
437 struct ad_mpg123_context *con = sh->context;
438 if (con->vbr && --con->delay < 1) {
439 struct mpg123_frameinfo finfo;
440 if (MPG123_OK == mpg123_info(con->handle, &finfo)) {
441 if (++con->mean_count > ((unsigned int) -1) / 2)
442 con->mean_count = ((unsigned int) -1) / 4;
444 /* Might not be numerically optimal, but works fine enough. */
445 con->mean_rate = ((con->mean_count - 1) * con->mean_rate +
446 finfo.bitrate) / con->mean_count;
447 sh->i_bps = (int) (con->mean_rate * 1000 / 8);
449 con->delay = 10;
453 #endif
455 static int decode_audio(sh_audio_t *sh, unsigned char *buf, int minlen,
456 int maxlen)
458 int bytes;
460 bytes = decode_a_bit(sh, buf, maxlen);
461 if (bytes == 0)
462 return -1; /* EOF */
464 #ifdef AD_MPG123_MEAN_BITRATE
465 update_info(sh);
466 #endif
467 return bytes;
470 static int control(sh_audio_t *sh, int cmd, void *arg, ...)
472 switch (cmd) {
473 case ADCTRL_RESYNC_STREAM:
474 /* Close/reopen the stream for mpg123 to make sure it doesn't
475 * think that it still knows the exact stream position.
476 * Otherwise, we would have funny effects from the gapless code.
477 * Oh, and it helps to minimize artifacts from jumping in the stream. */
478 if (reopen_stream(sh)) {
479 #ifdef AD_MPG123_MEAN_BITRATE
480 update_info(sh);
481 #endif
482 return CONTROL_TRUE;
483 } else {
484 /* MPlayer ignores this case! It just keeps on decoding.
485 * So we have to make sure resync never fails ... */
486 mp_msg(MSGT_DECAUDIO, MSGL_ERR,
487 "mpg123 cannot reopen stream for resync.\n");
488 return CONTROL_FALSE;
490 break;
492 return CONTROL_UNKNOWN;