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

Reimar Döffinger Reimar.Doeffinger at stud.uni-karlsruhe.de
Wed Feb 2 16:23:06 CET 2005


Hi,
the attached patch add muspack support (needs libmusepack 1.1). Seeking
does not work at all, mpc-in-avi is half-working (if not to say quite
broken, though it plays somehow - I guess that mencoder is to blame for
that brokenness...)

Greetings,
Reimar Döffinger
-------------- next part --------------
Index: Makefile
===================================================================
RCS file: /cvsroot/mplayer/main/Makefile,v
retrieving revision 1.314
diff -u -r1.314 Makefile
--- Makefile	31 Dec 2004 11:11:24 -0000	1.314
+++ Makefile	2 Feb 2005 15:15:00 -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.958
diff -u -r1.958 configure
--- configure	1 Feb 2005 11:54:50 -0000	1.958
+++ configure	2 Feb 2005 15:15:31 -0000
@@ -222,6 +222,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]
@@ -1402,6 +1403,7 @@
 _inet6=auto
 _gethostbyname2=auto
 _ftp=yes
+_musepack=auto
 for ac_option do
   case "$ac_option" in
   # Skip 1st pass
@@ -1516,6 +1518,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	;;
@@ -5249,6 +5258,25 @@
 fi
 echores "$_libmpeg2"
 
+echocheck "musepack support"
+if test "$_musepack" = auto ; then
+  _musepack=no
+  cat > $TMPC << EOF
+#include <musepack/musepack.h>
+int main(void) { mpc_streaminfo info; mpc_streaminfo_init(&info); return 0; }
+EOF
+  cc_check -lmusepack $_ld_lm && _musepack=yes
+fi
+if test "$_musepack" = yes ; then
+  _def_musepack='#define HAVE_MUSEPACK 1'
+  _ld_musepack='-lmusepack'
+  _codecmodules="musepack $_codecmodules"
+else
+  _def_musepack='#undef HAVE_MUSEPACK'
+  _nocodecmodules="musepack $_codecmodules"
+fi
+echores "$_musepack"
+
 
 echocheck "Matroska support"
 if test "$_matroska_internal" = yes ; then
@@ -6672,6 +6700,8 @@
 TREMOR = $_tremor_internal
 TREMOR_FLAGS = $_tremor_flags
 
+MUSEPACK = $_musepack
+
 UNRARLIB = $_unrarlib
 HAVE_FFPOSTPROCESS = $_def_haveffpostprocess
 PNG = $_mkf_png
@@ -6787,6 +6817,7 @@
 TOOLAME=$_toolame
 TOOLAME_EXTRAFLAGS=$_toolame_extraflags
 TOOLAME_LIB=$_toolame_lib
+MUSEPACK_LIB = $_ld_musepack
 AMR_NB=$_amr_nb
 AMR_NB_FIXED=$_amr_nb_fixed
 AMR_WB=$_amr_wb
@@ -7312,6 +7343,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.389
diff -u -r1.389 codecs.conf
--- etc/codecs.conf	31 Jan 2005 22:36:57 -0000	1.389
+++ etc/codecs.conf	2 Feb 2005 15:16:06 -0000
@@ -2371,6 +2371,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.138
diff -u -r1.138 Makefile
--- libmpcodecs/Makefile	29 Dec 2004 19:51:56 -0000	1.138
+++ libmpcodecs/Makefile	2 Feb 2005 15:16:12 -0000
@@ -44,6 +44,10 @@
 EXTRA_INC += $(TOOLAME_EXTRAFLAGS)
 endif
 
+ifeq ($(MUSEPACK),yes)
+AUDIO_SRCS += ad_mpc.c
+endif
+
 SRCS=$(AUDIO_SRCS) $(VIDEO_SRCS) $(VFILTER_SRCS) $(NATIVE_SRCS) img_format.c
 OBJS=$(SRCS:.c=.o)
 
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	2 Feb 2005 15:16:13 -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.30
diff -u -r1.30 demux_audio.c
--- libmpdemux/demux_audio.c	29 Jan 2005 12:55:56 -0000	1.30
+++ libmpdemux/demux_audio.c	2 Feb 2005 15:16:22 -0000
@@ -18,6 +18,7 @@
 #define MP3 1
 #define WAV 2
 #define fLaC 3
+#define MPC 4
 
 
 #define HDR_SIZE 4
@@ -154,6 +155,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;      
@@ -235,6 +239,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;
@@ -406,6 +424,7 @@
     l = stream_read(s,dp->buffer,l);
     break;
   }
+  case MPC:
   case fLaC: {
     l = 65535;
     dp = new_demux_packet(l);
@@ -460,6 +479,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 "demuxer/decoder"
 * for MPlayer
 * by Reza Jelveh <reza.jelveh at tuhh.de> and
 * Reimar Döffinger <Reimar.Doeffinger at stud.uni-karlsruhe.de>
 * Licence: 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 <musepack/musepack.h>

typedef struct readerdata_s {
  char *d;
  uint32_t pos;
  uint32_t end_pos;
} readerdata_t;

static readerdata_t reader_data;
static mpc_decoder decoder_data;

/*
 *   Our implementations of the mpc_reader callback functions.
 *   */
static mpc_int32_t cb_read(void *data, void *buf, mpc_int32_t size) {
  readerdata_t *d = (readerdata_t *)data;
  char *p = (char *)buf;
  int s = size;
  if (d->pos < d->end_pos) {
    if (s > d->end_pos - d->pos)
      s = d->end_pos - d->pos;
    memcpy(p, &d->d[d->pos], s);
  } else
    s = 0;
  memset(&p[s], 0, size - s);
  d->pos += size;
  return size;
}

static BOOL cb_seek(void *data, mpc_int32_t offset ) {
  readerdata_t *d = (readerdata_t *)data;
  d->pos = offset;
  return 1;
}

static mpc_int32_t cb_tell(void *data) {
  readerdata_t *d = (readerdata_t *)data;
  return d->pos;
}

static mpc_int32_t cb_get_size(void *data) {
  return 1 << 30;
}

static BOOL cb_canseek(void *data) {
  return 0;
}

static mpc_int32_t cb_read2(void *data, void *ptr, mpc_int32_t size) {
  readerdata_t *d = (readerdata_t *)data;
  sh_audio_t *sh = (sh_audio_t*)d->d;
  demux_read_data(sh->ds, ptr, size);
  d->pos += size;
  return size;
}

mpc_reader codecdata_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) {
}

static int init(sh_audio_t *sh) {
  mpc_streaminfo info;
  char *header;
  int header_len;

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

  reader_data.pos = 0;
  reader_data.d = header;
  reader_data.end_pos = header_len;
  codecdata_reader.data = &reader_data;

  /* read file's streaminfo data */
  mpc_streaminfo_init(&info);
  if (mpc_streaminfo_read(&info, &codecdata_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->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

  reader_data.d = (char*)sh;
  demux_reader.data = &reader_data;

  mpc_decoder_setup(&decoder_data, &demux_reader);
  if (!mpc_decoder_initialize(&decoder_data, &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;
  if (maxlen < MPC_DECODER_BUFFER_LENGTH) {
    mp_msg(MSGT_DECAUDIO, MSGL_ERR, "maxlen too small in decode_audio\n");
    return -1;
  }
  status = mpc_decoder_decode(&decoder_data, 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