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