Skip to content

Commit 1ab438c

Browse files
committed
Support for mp3 decoding using MPG123 instead of MAD
1 parent cfc3ccc commit 1ab438c

File tree

4 files changed

+395
-19
lines changed

4 files changed

+395
-19
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ optional(HAVE_SNDIO sndio.h sndio sio_open sndio)
124124
optional(HAVE_AO ao/ao.h ao ao_play ao)
125125
optional(HAVE_FLAC FLAC/all.h FLAC FLAC__stream_encoder_new flac)
126126
optional(HAVE_MAD_H mad.h mad mad_stream_buffer mp3)
127+
optional(HAVE_MPG123_H mpg123.h mpg123 mpg123_init mp3)
127128
optional(HAVE_LAME_LAME_H lame/lame.h mp3lame lame_get_lametag_frame mp3)
128129
if (NOT HAVE_LAME_LAME_H)
129130
optional(HAVE_LAME_LAME_H lame.h mp3lame lame_get_lametag_frame mp3)
@@ -162,9 +163,9 @@ optional4(HAVE_OGG_VORBIS vorbis/codec.h ogg ogg_stream_flush vorbis vorbis_anal
162163
optional3(HAVE_OPUS opusfile.h ogg ogg_stream_flush opus opus_encoder_create opusfile op_open_callbacks opus)
163164
optional(HAVE_WAVPACK wavpack/wavpack.h wavpack WavpackGetSampleRate wavpack)
164165

165-
if (HAVE_LAME_LAME_H OR HAVE_MAD_H)
166+
if (HAVE_LAME_LAME_H OR HAVE_MAD_H OR HAVE_MPG123_H)
166167
set(HAVE_MP3 1)
167-
endif (HAVE_LAME_LAME_H OR HAVE_MAD_H)
168+
endif (HAVE_LAME_LAME_H OR HAVE_MAD_H OR HAVE_MPG123_H)
168169

169170
set(CMAKE_REQUIRED_LIBRARIES mp3lame m)
170171
check_function_exists("lame_set_VBR_quality" HAVE_LAME_SET_VBR_QUALITY)

configure.ac

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,32 @@ AM_CONDITIONAL(HAVE_ID3TAG, test x$using_id3tag = xyes)
394394

395395

396396

397+
dnl Check for mpg123 libraries
398+
AC_ARG_WITH(mpg123,
399+
AS_HELP_STRING([--with-mpg123],
400+
[Try to use mpg123 (LGPL MP3 Audio Decoder)]))
401+
using_mpg123=no
402+
if test "$with_mpg123" == "yes"; then
403+
using_mpg123=yes
404+
AC_CHECK_HEADERS(mpg123.h,, using_mpg123=no)
405+
AC_MSG_CHECKING([whether to dlopen mpg123])
406+
AC_ARG_ENABLE(dl_mpg123,
407+
AS_HELP_STRING([--enable-dl-mpg123], [Dlopen mpg123 instead of linking in.]),
408+
enable_dl_mpg123=$enableval, enable_dl_mpg123=no)
409+
AC_MSG_RESULT($enable_dl_mpg123)
410+
if test "x$using_libltdl" = "xyes" -a "x$enable_dl_mpg123" = "xyes"; then
411+
AC_DEFINE(DL_MPG123, 1, [Define to dlopen() mpg123.])
412+
else
413+
enable_dl_mpg123="no"
414+
AC_CHECK_LIB(mpg123, mpg123_new, MP3_LIBS="$MP3_LIBS -lmpg123",using_mpg123=no)
415+
if test "$with_mpg123" = "yes" -a "$using_mpg123" = "no"; then
416+
AC_MSG_FAILURE([cannot find libmpg123])
417+
fi
418+
fi
419+
fi
420+
421+
422+
397423
dnl Check for LAME library.
398424
AC_ARG_WITH(lame,
399425
AS_HELP_STRING([--without-lame],
@@ -623,9 +649,9 @@ AC_OPTIONAL_FORMAT(sunaudio, SUN_AUDIO, [AC_CHECK_HEADERS(sys/audioio.h,,
623649

624650

625651

626-
# MP2/MP3 format depends on libmad || LAME || twolame
652+
# MP2/MP3 format depends on libmad || libmpg123 || LAME || twolame
627653
AC_OPTIONAL_FORMAT(mp3, MP3, [
628-
if test "$using_mad" != yes -a "$using_lame" != yes -a "$using_twolame" != yes; then
654+
if test "$using_mad" != yes -a "$using_mpg123" != yes -a "$using_lame" != yes -a "$using_twolame" != yes; then
629655
using_mp3=no
630656
fi])
631657

@@ -740,6 +766,10 @@ echo " mad.......................$using_mad"
740766
if test "x$using_mad" = "xyes"; then
741767
echo " dlopen mad................$enable_dl_mad"
742768
fi
769+
echo " mpg123....................$using_mpg123"
770+
if test "x$using_mpg123" = "xyes"; then
771+
echo " dlopen mpg123.............$enable_dl_mpg123"
772+
fi
743773
echo " twolame...................$using_twolame"
744774
if test "x$using_twolame" = "xyes"; then
745775
echo " dlopen twolame............$enable_dl_twolame"

src/mp3-util.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,39 @@ static size_t mp3_duration_ms(sox_format_t * ft)
348348
}
349349

350350
#endif /* HAVE_MAD_H */
351+
352+
#ifdef HAVE_MPG123_H
353+
354+
static size_t mp3_duration(sox_format_t * ft)
355+
{
356+
priv_t * p = (priv_t *) ft->priv;
357+
FILE * fp = ft->fp;
358+
if (!fp || !ft->seekable) {
359+
lsx_fail_errno(ft, SOX_EOF, "File pointer is undefined or not seekable in mp3_duration_ms");
360+
return SOX_UNSPEC;
361+
}
362+
int error;
363+
mpg123_handle * handle = p->mpg123_new(NULL, &error);
364+
if (!handle) {
365+
lsx_fail_errno(ft, SOX_EOF, "Could not get mpg123 handle: %s", mpg123_plain_strerror(error));
366+
return SOX_UNSPEC;
367+
}
368+
369+
p->mpg123_open_fd(handle, fileno(fp));
370+
p->mpg123_scan(handle);
371+
off_t samples = p->mpg123_length(handle);
372+
int channels = 0;
373+
int encoding = 0;
374+
long sample_rate = 0;
375+
mpg123_getformat(handle, &sample_rate, &channels, &encoding);
376+
p->mpg123_close(handle);
377+
p->mpg123_delete(handle);
378+
379+
lsx_rewind(ft);
380+
381+
// fprintf(stderr, "File length in samples, according to mpg123 (after scan): %zu / %zu\n", samples, samples * channels);
382+
383+
return (size_t) (samples * channels);
384+
}
385+
386+
#endif /* HAVE_MPG123_H */

0 commit comments

Comments
 (0)