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.
27 #include "ad_internal.h"
29 static const ad_info_t info
= {
30 "MPEG 1.0/2.0/2.5 layers I, II, III",
34 "High-performance decoder using libmpg123."
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
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
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. */
79 struct ad_mpg123_context
{
80 mpg123_handle
*handle
;
81 #ifdef AD_MPG123_MEAN_BITRATE
82 /* Running mean for bit rate, stream length estimation. */
84 unsigned int mean_count
;
85 /* Time delay for updates. */
88 /* If the stream is actually VBR. */
90 #if (defined AD_MPG123_CALLBACK) && (defined AD_MPG123_PACKET)
91 unsigned char *packet
;
96 static void context_reset(struct ad_mpg123_context
*con
)
98 #ifdef AD_MPG123_MEAN_BITRATE
103 #if (defined AD_MPG123_CALLBACK) && (defined AD_MPG123_PACKET)
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
;
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. */
128 con
->packleft
-= get
;
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
) {
148 /* It returns int... with the meaning of byte count. */
149 return (ssize_t
) demux_read_data(sh
->ds
, buf
, count
);
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
)
161 /* This initializes libmpg123 and prepares the handle, including funky
163 static int preinit(sh_audio_t
*sh
)
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
171 if (mpg123_init() != MPG123_OK
)
174 sh
->context
= malloc(sizeof(struct ad_mpg123_context
));
178 /* Auto-choice of optimized decoder (first argument NULL). */
179 con
->handle
= mpg123_new(NULL
, &err
);
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
)
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
));
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
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);
214 /* Open decisions: Configure libmpg123 to force encoding (or stay open about
215 * library builds that support only float or int32 output), (de)configure
216 * gapless decoding (won't work with seeking in MPlayer, though).
217 * Don't forget to eventually enable ReplayGain/RVA support, too.
218 * Let's try to run with the default for now. */
220 /* Example for RVA choice (available since libmpg123 1.0.0):
221 mpg123_param(con->handle, MPG123_RVA, MPG123_RVA_MIX, 0.0) */
227 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "mpg123 preinit error: %s\n",
228 mpg123_plain_strerror(err
));
230 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "mpg123 preinit error: %s\n",
231 mpg123_strerror(con
->handle
));
234 mpg123_delete(con
->handle
);
241 /* Compute bitrate from frame size. */
242 static int compute_bitrate(struct mpg123_frameinfo
*i
)
244 static const int samples_per_frame
[4][4] = {
245 {-1, 384, 1152, 1152}, /* MPEG 1 */
246 {-1, 384, 1152, 576}, /* MPEG 2 */
247 {-1, 384, 1152, 576}, /* MPEG 2.5 */
248 {-1, -1, -1, -1}, /* Unknown */
250 return (int) ((i
->framesize
+ 4) * 8 * i
->rate
* 0.001 /
251 samples_per_frame
[i
->version
][i
->layer
] + 0.5);
254 /* Opted against the header printout from old mp3lib, too much
255 * irrelevant info. This is modelled after the mpg123 app's
256 * standard output line.
257 * If more verbosity is demanded, one can add more detail and
258 * also throw in ID3v2 info which libmpg123 collects anyway. */
259 static void print_header_compact(struct mpg123_frameinfo
*i
)
261 static const char *smodes
[5] = {
262 "stereo", "joint-stereo", "dual-channel", "mono", "invalid"
264 static const char *layers
[4] = {
265 "Unknown", "I", "II", "III"
267 static const char *versions
[4] = {
268 "1.0", "2.0", "2.5", "x.x"
271 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "MPEG %s layer %s, ",
272 versions
[i
->version
], layers
[i
->layer
]);
276 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "%d kbit/s", i
->bitrate
);
278 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "%d kbit/s (free format)",
282 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "VBR");
285 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "%d kbit/s ABR", i
->abr_rate
);
288 mp_msg(MSGT_DECAUDIO
, MSGL_V
, "???");
290 mp_msg(MSGT_DECAUDIO
, MSGL_V
, ", %ld Hz %s\n", i
->rate
,
294 #ifndef AD_MPG123_CALLBACK
295 /* This tries to extract a requested amount of decoded data.
296 * Even when you request 0 bytes, it will feed enough input so that
297 * the decoder _could_ have delivered something.
298 * Returns byte count >= 0, -1 on error.
300 * Thoughts on exact pts keeping:
301 * We have to assume that MPEG frames are cut in pieces by packet boundaries.
302 * Also, it might be possible that the first packet does not contain enough
303 * data to ensure initial stream sync... or re-sync on erroneous streams.
304 * So we need something robust to relate the decoded byte count to the correct
305 * time stamp. This is tricky, though. From the outside, you cannot tell if,
306 * after having fed two packets until the first output arrives, one should
307 * start counting from the first packet's pts or the second packet's.
308 * So, let's just count from the last fed package's pts. If the packets are
309 * exactly cut to MPEG frames, this will cause one frame mismatch in the
310 * beginning (when mpg123 peeks ahead for the following header), but will
311 * be corrected with the third frame already. One might add special code to
312 * not increment the base pts past the first packet's after a resync before
313 * the first decoded bytes arrived. */
314 static int decode_a_bit(sh_audio_t
*sh
, unsigned char *buf
, int count
)
318 struct ad_mpg123_context
*con
= sh
->context
;
320 /* There will be one MPG123_NEW_FORMAT message on first open.
321 * This will be implicitly handled in reopen_stream(). */
324 ret
= mpg123_decode(con
->handle
, NULL
, 0, buf
+ got
, count
- got
,
327 #ifdef AD_MPG123_PACKET
328 sh
->pts_bytes
+= got_now
;
331 if (ret
== MPG123_NEED_MORE
) {
333 #ifdef AD_MPG123_PACKET
335 unsigned char *inbuf
;
336 /* Feed more input data. */
337 incount
= ds_get_packet_pts(sh
->ds
, &inbuf
, &pts
);
339 break; /* Apparently that's it. EOF. */
341 /* Next bytes from that presentation time. */
342 if (pts
!= MP_NOPTS_VALUE
) {
347 const int inbufsize
= 4096;
348 unsigned char inbuf
[inbufsize
];
349 /* Feed more input data. */
350 incount
= demux_read_data(((sh_audio_t
*) sh
)->ds
,
353 break; /* Apparently that's it. EOF. */
356 /* Do not use mpg123_feed(), added in later libmpg123 versions. */
357 ret
= mpg123_decode(con
->handle
, inbuf
, incount
, NULL
, 0, NULL
);
358 /* Return value is checked in the loop condition.
359 * It could be MPG12_OK now, it could need more. */
361 /* Older mpg123 versions might indicate MPG123_DONE, so be prepared. */
362 else if (ret
== MPG123_ERR
|| ret
== MPG123_DONE
)
365 } while (ret
== MPG123_NEED_MORE
|| got
< count
);
367 if (ret
== MPG123_ERR
) {
368 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "mpg123 decoding failed: %s\n",
369 mpg123_strerror(con
->handle
));
370 mpg123_close(con
->handle
);
378 /* Close, reopen stream. Feed data until we know the format of the stream.
379 * 1 on success, 0 on error */
380 static int reopen_stream(sh_audio_t
*sh
)
384 struct ad_mpg123_context
*con
= (struct ad_mpg123_context
*) sh
->context
;
386 mpg123_close(con
->handle
);
389 #ifdef AD_MPG123_CALLBACK
390 if (MPG123_OK
== mpg123_open_handle(con
->handle
, sh
) &&
392 if (/* Open and make sure we have fed enough data to get stream properties. */
393 MPG123_OK
== mpg123_open_feed(con
->handle
) &&
394 /* Feed data until mpg123 is ready (has found stream beginning). */
395 !decode_a_bit(sh
, NULL
, 0) &&
397 /* Not handing NULL pointers for compatibility with old libmpg123. */
398 MPG123_OK
== mpg123_getformat(con
->handle
, &rate
, &chan
, &enc
)) {
401 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
,
402 "mpg123 failed to reopen stream: %s\n",
403 mpg123_strerror(con
->handle
));
404 mpg123_close(con
->handle
);
409 /* Now we really start accessing some data and determining file format.
410 * Paranoia note: The mpg123_close() on errors is not really necessary,
411 * But it ensures that we don't accidentally continue decoding with a
412 * bad state (possibly interpreting the format badly or whatnot). */
413 static int init(sh_audio_t
*sh
)
419 struct mpg123_frameinfo finfo
;
420 struct ad_mpg123_context
*con
= sh
->context
;
422 /* We're open about any output format that libmpg123 will suggest.
423 * Note that a standard build will always default to 16 bit signed and
424 * the native sample rate of the file. */
425 if (MPG123_OK
== mpg123_format_all(con
->handle
) &&
427 MPG123_OK
== mpg123_getformat(con
->handle
, &rate
, &channels
, &encoding
) &&
428 /* Forbid the format to change later on. */
429 MPG123_OK
== mpg123_format_none(con
->handle
) &&
430 MPG123_OK
== mpg123_format(con
->handle
, rate
, channels
, encoding
) &&
431 /* Get MPEG header info. */
432 MPG123_OK
== mpg123_info(con
->handle
, &finfo
) &&
433 /* Since we queried format, mpg123 should have read past ID3v2 tags.
434 * We need to decide if printing of UTF-8 encoded text info is wanted. */
435 MPG123_OK
== mpg123_id3(con
->handle
, NULL
, &v2
)) {
436 /* If we are here, we passed all hurdles. Yay! Extract the info. */
437 print_header_compact(&finfo
);
438 /* Do we want to print out the UTF-8 Id3v2 info?
442 /* Have kb/s, want B/s
443 * For VBR, the first frame will be a bad estimate. */
444 sh
->i_bps
= (finfo
.bitrate
? finfo
.bitrate
: compute_bitrate(&finfo
))
447 con
->vbr
= (finfo
.vbr
!= MPG123_CBR
);
448 sh
->channels
= channels
;
449 sh
->samplerate
= rate
;
450 /* Without external force, mpg123 will always choose signed encoding,
451 * and non-16-bit only on builds that don't support it.
452 * Be reminded that it doesn't matter to the MPEG file what encoding
453 * is produced from it. */
455 case MPG123_ENC_SIGNED_8
:
456 sh
->sample_format
= AF_FORMAT_S8
;
459 case MPG123_ENC_SIGNED_16
:
460 sh
->sample_format
= AF_FORMAT_S16_NE
;
463 /* To stay compatible with the oldest libmpg123 headers, do not rely
464 * on float and 32 bit encoding symbols being defined.
465 * Those formats came later */
466 case 0x1180: /* MPG123_ENC_SIGNED_32 */
467 sh
->sample_format
= AF_FORMAT_S32_NE
;
470 case 0x200: /* MPG123_ENC_FLOAT_32 */
471 sh
->sample_format
= AF_FORMAT_FLOAT_NE
;
475 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
,
476 "Bad encoding from mpg123: %i.\n", encoding
);
477 mpg123_close(con
->handle
);
483 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "mpg123 init error: %s\n",
484 mpg123_strerror(con
->handle
));
485 mpg123_close(con
->handle
);
490 static void uninit(sh_audio_t
*sh
)
492 struct ad_mpg123_context
*con
= (struct ad_mpg123_context
*) sh
->context
;
494 mpg123_close(con
->handle
);
495 mpg123_delete(con
->handle
);
501 #ifdef AD_MPG123_MEAN_BITRATE
502 /* Update mean bitrate. This could be dropped if accurate time display
503 * on audio file playback is not desired. */
504 static void update_info(sh_audio_t
*sh
)
506 struct ad_mpg123_context
*con
= sh
->context
;
507 if (con
->vbr
&& --con
->delay
< 1) {
508 struct mpg123_frameinfo finfo
;
509 if (MPG123_OK
== mpg123_info(con
->handle
, &finfo
)) {
510 if (++con
->mean_count
> ((unsigned int) -1) / 2)
511 con
->mean_count
= ((unsigned int) -1) / 4;
513 /* Might not be numerically optimal, but works fine enough. */
514 con
->mean_rate
= ((con
->mean_count
- 1) * con
->mean_rate
+
515 finfo
.bitrate
) / con
->mean_count
;
516 sh
->i_bps
= (int) (con
->mean_rate
* 1000 / 8);
524 static int decode_audio(sh_audio_t
*sh
, unsigned char *buf
, int minlen
,
529 #ifdef AD_MPG123_CALLBACK
530 struct ad_mpg123_context
*con
= sh
->context
;
531 size_t got_bytes
= 0;
532 if (MPG123_ERR
== mpg123_read(con
->handle
, buf
, minlen
, &got_bytes
)) {
533 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
, "Decoding error in mpg123: %s\n",
534 mpg123_strerror(con
->handle
));
537 #ifdef AD_MPG123_PACKET
538 sh
->pts_bytes
+= got_bytes
;
542 bytes
= decode_a_bit(sh
, buf
, minlen
);
548 #ifdef AD_MPG123_MEAN_BITRATE
555 static int control(sh_audio_t
*sh
, int cmd
, void *arg
, ...)
558 case ADCTRL_RESYNC_STREAM
:
559 /* Close/reopen the stream for mpg123 to make sure it doesn't
560 * think that it still knows the exact stream position.
561 * Otherwise, we would have funny effects from the gapless code.
562 * Oh, and it helps to minimize artifacts from jumping in the stream. */
563 if (reopen_stream(sh
)) {
564 #ifdef AD_MPG123_MEAN_BITRATE
569 mp_msg(MSGT_DECAUDIO
, MSGL_ERR
,
570 "mpg123 cannot reopen stream for resync.\n");
571 return CONTROL_FALSE
;
575 return CONTROL_UNKNOWN
;