[MPlayer-dev-eng] [PATCH] Preliminary musepack support

Reimar Döffinger Reimar.Doeffinger at stud.uni-karlsruhe.de
Tue May 10 11:02:27 CEST 2005


Hi,
On Wed, Mar 02, 2005 at 12:07:11PM +0100, Reimar D?ffinger wrote:
> Another update - unfortunately there still is an naming clash between
> the random_int functions in libfaad2 and libmusepack - so MPlayer won't
> compile with gcc 3.3.x when you use both - with gcc 3.4.x it compiles
> but crashes when you use libfaad2 :-(

The naming clash seems to be fixed in the latest version of the lib.
The attached patch uses it. Even though seeking is still completely
broken, I would like to apply it soon (since I don't intend to fix it
any time soon, like this others can improve on it more easily).
Is that okay with you? Aynthing you dislike about this patch?

Greetings,
Reimar Döffinger
-------------- next part --------------
Index: Makefile
===================================================================
RCS file: /cvsroot/mplayer/main/Makefile,v
retrieving revision 1.322
diff -u -r1.322 Makefile
--- Makefile	7 May 2005 14:50:14 -0000	1.322
+++ Makefile	10 May 2005 08:46:48 -0000
@@ -32,7 +32,7 @@
 
 VO_LIBS = $(AA_LIB) $(X_LIB) $(SDL_LIB) $(GGI_LIB) $(MP1E_LIB) $(MLIB_LIB) $(SVGA_LIB) $(DIRECTFB_LIB) $(CACA_LIB)
 AO_LIBS = $(ARTS_LIB) $(ESD_LIB) $(JACK_LIB) $(NAS_LIB) $(SGIAUDIO_LIB) $(POLYP_LIB)
-CODEC_LIBS = $(AV_LIB) $(FAME_LIB) $(MAD_LIB) $(VORBIS_LIB) $(THEORA_LIB) $(FAAD_LIB) $(LIBLZO_LIB) $(DECORE_LIB) $(XVID_LIB) $(DTS_LIB) $(PNG_LIB) $(Z_LIB) $(JPEG_LIB) $(ALSA_LIB) $(XMMS_LIB) $(X264_LIB)
+CODEC_LIBS = $(AV_LIB) $(FAME_LIB) $(MAD_LIB) $(VORBIS_LIB) $(THEORA_LIB) $(FAAD_LIB) $(LIBLZO_LIB) $(DECORE_LIB) $(XVID_LIB) $(DTS_LIB) $(PNG_LIB) $(Z_LIB) $(JPEG_LIB) $(ALSA_LIB) $(XMMS_LIB) $(X264_LIB) $(MUSEPACK_LIB)
 COMMON_LIBS = libmpcodecs/libmpcodecs.a $(W32_LIB) $(DS_LIB) libaf/libaf.a libmpdemux/libmpdemux.a input/libinput.a postproc/libswscale.a osdep/libosdep.a $(DVDREAD_LIB) $(CODEC_LIBS) $(FREETYPE_LIB) $(TERMCAP_LIB) $(CDPARANOIA_LIB) $(MPLAYER_NETWORK_LIB) $(WIN32_LIB) $(GIF_LIB) $(MACOSX_FRAMEWORKS) $(SMBSUPPORT_LIB) $(FRIBIDI_LIB) $(FONTCONFIG_LIB) $(ENCA_LIB)
 
 CFLAGS = $(OPTFLAGS) -I. $(FREETYPE_INC) $(EXTRA_INC) $(CDPARANOIA_INC) $(SDL_INC) $(X11_INC) $(FRIBIDI_INC) $(DVB_INC) $(XVID_INC) $(FONTCONFIG_INC) $(CACA_INC) # -Wall
Index: configure
===================================================================
RCS file: /cvsroot/mplayer/main/configure,v
retrieving revision 1.1000
diff -u -r1.1000 configure
--- configure	10 May 2005 00:57:43 -0000	1.1000
+++ configure	10 May 2005 08:46:48 -0000
@@ -232,6 +232,7 @@
   --disable-liba52       disable builtin liba52 [enabled]
   --enable-libdts        enable libdts support [autodetect]
   --disable-libmpeg2     disable builtin libmpeg2 [enabled]
+  --disable-musepack     disable musepack support [autodetect]
   --disable-amr_nb       disable amr narrowband, floating point [autodetect]
   --disable-amr_nb-fixed disable amr narrowband, fixed point [autodetect]
   --disable-amr_wb       disable amr wideband, floating point [autodetect]
@@ -1423,6 +1424,7 @@
 _inet6=auto
 _gethostbyname2=auto
 _ftp=yes
+_musepack=auto
 _vstream=auto
 for ac_option do
   case "$ac_option" in
@@ -1539,6 +1541,8 @@
   --disable-libdts)	_libdts=no      ;;
   --enable-libmpeg2)	_libmpeg2=yes	;;
   --disable-libmpeg2)	_libmpeg2=no	;;
+  --enable-musepack)	_musepack=yes	;;
+  --disable-musepack)	_musepack=no	;;
   --enable-internal-matroska)  _matroska_internal=yes ;;
   --disable-internal-matroska) _matroska_internal=no ;;
   --enable-internal-faad)	_faad_internal=yes	_faad_external=no	;;
@@ -5375,6 +5379,25 @@
 fi
 echores "$_libmpeg2"
 
+echocheck "musepack support"
+if test "$_musepack" = auto ; then
+  _musepack=no
+  cat > $TMPC << EOF
+#include <mpcdec/mpcdec.h>
+int main(void) { mpc_streaminfo info; mpc_streaminfo_init(&info); return 0; }
+EOF
+  cc_check -lmpcdec $_ld_lm && _musepack=yes
+fi
+if test "$_musepack" = yes ; then
+  _def_musepack='#define HAVE_MUSEPACK 1'
+  _ld_musepack='-lmpcdec'
+  _codecmodules="musepack $_codecmodules"
+else
+  _def_musepack='#undef HAVE_MUSEPACK'
+  _nocodecmodules="musepack $_nocodecmodules"
+fi
+echores "$_musepack"
+
 
 echocheck "Matroska support"
 if test "$_matroska_internal" = yes ; then
@@ -6826,6 +6860,8 @@
 TREMOR = $_tremor_internal
 TREMOR_FLAGS = $_tremor_flags
 
+MUSEPACK = $_musepack
+
 UNRARLIB = $_unrarlib
 HAVE_FFPOSTPROCESS = $_def_haveffpostprocess
 PNG = $_mkf_png
@@ -6946,6 +6982,7 @@
 TOOLAME_LIB=$_toolame_lib
 TWOLAME=$_twolame
 TWOLAME_LIB=$_twolame_lib
+MUSEPACK_LIB = $_ld_musepack
 FAAC=$_faac
 FAAC_LIB=$_ld_faac
 AMR_NB=$_amr_nb
@@ -7486,6 +7523,9 @@
 /* enable Tremor as vorbis decoder */
 $_def_tremor
 
+/* enable musepack support */
+$_def_musepack
+
 /* enable OggTheora support */
 $_def_theora
 
Index: etc/codecs.conf
===================================================================
RCS file: /cvsroot/mplayer/main/etc/codecs.conf,v
retrieving revision 1.411
diff -u -r1.411 codecs.conf
--- etc/codecs.conf	4 May 2005 20:35:21 -0000	1.411
+++ etc/codecs.conf	10 May 2005 08:46:49 -0000
@@ -2446,6 +2446,12 @@
   driver ffmpeg
   dll "dts"
 
+audiocodec musepack
+  info "MPC/MpegPlus audio codec"
+  status  working
+  fourcc  "MPC "
+  driver libmusepack
+
 audiocodec ffamrnb
   info "AMR Narrowband"
   status working
Index: libmpcodecs/Makefile
===================================================================
RCS file: /cvsroot/mplayer/main/libmpcodecs/Makefile,v
retrieving revision 1.141
diff -u -r1.141 Makefile
--- libmpcodecs/Makefile	7 May 2005 14:49:18 -0000	1.141
+++ libmpcodecs/Makefile	10 May 2005 08:46:49 -0000
@@ -56,6 +56,10 @@
 ENCODER_SRCS += ae_lavc.c
 endif
 
+ifeq ($(MUSEPACK),yes)
+AUDIO_SRCS += ad_mpc.c
+endif
+
 ifeq ($(FAAC),yes)
 ENCODER_SRCS += ae_faac.c
 endif
Index: libmpcodecs/ad.c
===================================================================
RCS file: /cvsroot/mplayer/main/libmpcodecs/ad.c,v
retrieving revision 1.20
diff -u -r1.20 ad.c
--- libmpcodecs/ad.c	29 Dec 2004 19:51:56 -0000	1.20
+++ libmpcodecs/ad.c	10 May 2005 08:46:49 -0000
@@ -39,6 +39,7 @@
 extern ad_functions_t mpcodecs_ad_qtaudio;
 extern ad_functions_t mpcodecs_ad_ra1428;
 extern ad_functions_t mpcodecs_ad_twin;
+extern ad_functions_t mpcodecs_ad_libmusepack;
 
 ad_functions_t* mpcodecs_ad_drivers[] =
 {
@@ -87,5 +88,8 @@
   &mpcodecs_ad_libdv,
 #endif
   &mpcodecs_ad_ra1428,
+#ifdef HAVE_MUSEPACK
+  &mpcodecs_ad_libmusepack,
+#endif
   NULL
 };
Index: libmpdemux/demux_audio.c
===================================================================
RCS file: /cvsroot/mplayer/main/libmpdemux/demux_audio.c,v
retrieving revision 1.35
diff -u -r1.35 demux_audio.c
--- libmpdemux/demux_audio.c	18 Apr 2005 20:51:34 -0000	1.35
+++ libmpdemux/demux_audio.c	10 May 2005 08:46:49 -0000
@@ -18,6 +18,7 @@
 #define MP3 1
 #define WAV 2
 #define fLaC 3
+#define MPC 4
 
 
 #define HDR_SIZE 4
@@ -157,6 +158,9 @@
       len = (hdr[0]<<21) | (hdr[1]<<14) | (hdr[2]<<7) | hdr[3];
       stream_skip(s,len);
       step = 4;
+    } else if( hdr[0] == 'M' && hdr[1] == 'P' && hdr[2] == '+') {
+      frmt = MPC;
+      break;
     } else if( hdr[0] == 'f' && hdr[1] == 'm' && hdr[2] == 't' && hdr[3] == ' ' ) {
       frmt = WAV;
       break;      
@@ -238,6 +242,20 @@
       }
     }
     break;
+  case MPC: {
+      char *wf = (char *)calloc(1, sizeof(WAVEFORMATEX) + 6 * 4);
+      char *header = &wf[sizeof(WAVEFORMATEX)];
+      sh_audio->format = mmioFOURCC('M', 'P', 'C', ' ');
+      memcpy(header, hdr, 4);
+      stream_read(s, &header[4], 5 * 4);
+      sh_audio->wf = (WAVEFORMATEX *)wf;
+      sh_audio->wf->wFormatTag = sh_audio->format;
+      sh_audio->wf->nAvgBytesPerSec = 100; // dummy to make mencoder not hang
+      sh_audio->wf->cbSize = 6 * 4;
+      demuxer->movi_start = stream_tell(s);
+      demuxer->movi_end = s->end_pos;
+    }
+    break;
   case WAV: {
     unsigned int chunk_type;
     unsigned int chunk_size;
@@ -411,6 +429,7 @@
     priv->last_pts = priv->last_pts < 0 ? 0 : priv->last_pts + l/(float)sh_audio->i_bps;
     break;
   }
+  case MPC:
   case fLaC: {
     l = 65535;
     dp = new_demux_packet(l);
@@ -462,6 +481,9 @@
   s = demuxer->stream;
   priv = demuxer->priv;
 
+  if (priv->frmt == MPC)
+    return;
+
   if(priv->frmt == MP3 && hr_mp3_seek && !(flags & 2)) {
     len = (flags & 1) ? rel_seek_secs - priv->last_pts : rel_seek_secs;
     if(len < 0) {
-------------- next part --------------
/**
 * Musepack audio files decoder for MPlayer
 * by Reza Jelveh <reza.jelveh at tuhh.de> and
 * Reimar D?ffinger <Reimar.Doeffinger at stud.uni-karlsruhe.de>
 * License: GPL
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "config.h"
#include "ad_internal.h"
#include "../libaf/af_format.h"
#include "../libvo/fastmemcpy.h"

static ad_info_t info = 
{
	"MPC/MPEGPlus audio decoder",
	"libmusepack",
	"Reza Jelveh and Reimar D?ffinger",
	"",
	""
};

LIBAD_EXTERN(libmusepack)

#include <mpcdec/mpcdec.h>

typedef struct codecdata_s {
  char *header;
  int header_len;
  sh_audio_t *sh;
  uint32_t pos;
  mpc_decoder decoder;
} codecdata_t;

/**
 * \brief mpc_reader callback function for reading the header
 */
static mpc_int32_t cb_read(void *data, void *buf, mpc_int32_t size) {
  codecdata_t *d = (codecdata_t *)data;
  char *p = (char *)buf;
  int s = size;
  if (d->pos < d->header_len) {
    if (s > d->header_len - d->pos)
      s = d->header_len - d->pos;
    memcpy(p, &d->header[d->pos], s);
  } else
    s = 0;
  memset(&p[s], 0, size - s);
  d->pos += size;
  return size;
}

/**
 * \brief dummy mpc_reader callback function for seeking
 */
static mpc_bool_t cb_seek(void *data, mpc_int32_t offset ) {
  codecdata_t *d = (codecdata_t *)data;
  d->pos = offset;
  return 1;
}

/**
 * \brief dummy mpc_reader callback function for getting stream position
 */
static mpc_int32_t cb_tell(void *data) {
  codecdata_t *d = (codecdata_t *)data;
  return d->pos;
}

/**
 * \brief dummy mpc_reader callback function for getting stream length
 */
static mpc_int32_t cb_get_size(void *data) {
  return 1 << 30;
}

/**
 * \brief mpc_reader callback function, we cannot seek.
 */
static mpc_bool_t cb_canseek(void *data) {
  return 0;
}


/**
 * \brief mpc_reader callback function for reading from the stream.
 */
static mpc_int32_t cb_read2(void *data, void *ptr, mpc_int32_t size) {
  codecdata_t *d = (codecdata_t *)data;
  demux_read_data(d->sh->ds, ptr, size);
  d->pos += size;
  return size;
}

mpc_reader header_reader = {
  .read = cb_read, .seek = cb_seek, .tell = cb_tell,
  .get_size = cb_get_size, .canseek = cb_canseek
};

mpc_reader demux_reader = {
  .read = cb_read2, .seek = cb_seek, .tell = cb_tell,
  .get_size = cb_get_size, .canseek = cb_canseek
};

static int preinit(sh_audio_t *sh) {
  sh->audio_out_minsize = MPC_DECODER_BUFFER_LENGTH;
  return 1;
}

static void uninit(sh_audio_t *sh) {
  if (sh->codecdata)
    free(sh->codecdata);
}

static int init(sh_audio_t *sh) {
  mpc_streaminfo info;
  codecdata_t *cd = malloc(sizeof(codecdata_t));

  if (!sh->wf || (sh->wf->cbSize < 6 * 4)) {
    mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Missing extradata!\n");
    return 0;
  }
  cd->header = (char *)sh->wf;
  cd->header = &cd->header[sizeof(WAVEFORMATEX)];
  cd->header_len = sh->wf->cbSize;
  cd->sh = sh;
  cd->pos = 0;
  sh->codecdata = (char *)cd;

  /* read file's streaminfo data */
  mpc_streaminfo_init(&info);
  header_reader.data = cd;
  if (mpc_streaminfo_read(&info, &header_reader) != ERROR_CODE_OK) {
    mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Not a valid musepack file.\n");
    return 0;
  }
  sh->i_bps = info.average_bitrate / 8;
  sh->channels = info.channels;
  sh->samplerate = info.sample_freq;
  sh->samplesize = 4;
  sh->sample_format =
#if MPC_SAMPLE_FORMAT == float
             AF_FORMAT_FLOAT_NE;
#elif  MPC_SAMPLE_FORMAT == mpc_int32_t
             AF_FORMAT_S32_NE;
#else
  #error musepack lib must use either float or mpc_int32_t sample format
#endif

  demux_reader.data = cd;
  mpc_decoder_setup(&cd->decoder, &demux_reader);
  if (!mpc_decoder_initialize(&cd->decoder, &info))  {
    mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Error initializing decoder.\n");
    return 0;
  }
  return 1;
}

static int decode_audio(sh_audio_t *sh, unsigned char *buf,
                        int minlen, int maxlen) {
  int status;
  MPC_SAMPLE_FORMAT *sample_buffer = (MPC_SAMPLE_FORMAT *)buf;
  codecdata_t *cd = (codecdata_t *) sh->codecdata;
  if (maxlen < MPC_DECODER_BUFFER_LENGTH) {
    mp_msg(MSGT_DECAUDIO, MSGL_ERR, "maxlen too small in decode_audio\n");
    return -1;
  }
  demux_reader.data = cd; // just to be sure
  status = mpc_decoder_decode(&cd->decoder, sample_buffer, 0, 0);
  if (status == -1) //decode error
    mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Error decoding file.\n");
  if (status <= 0) // error or EOF
    return -1;

  // status > 0 (status == MPC_FRAME_LENGTH)
  status *= sh->channels; // one sample per channel
#if MPC_SAMPLE_FORMAT == float || MPC_SAMPLE_FORMAT == mpc_int32_t
  status *= 4;
#else
  // should not happen
  status *= 2;
#endif
  return status;
}

static int control(sh_audio_t *sh, int cmd, void* arg, ...) {
  return CONTROL_UNKNOWN;
}



More information about the MPlayer-dev-eng mailing list