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

Reimar Döffinger Reimar.Doeffinger at stud.uni-karlsruhe.de
Mon Feb 14 21:20:55 CET 2005


Hi,
On Wed, Feb 02, 2005 at 04:23:06PM +0100, Reimar Döffinger wrote:
> 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...)

Updated ad_mpc.c, should hopefully fix the case where some people got only noise.

Greetings,
Reimar Döffinger
-------------- 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 <musepack/musepack.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 BOOL 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 BOOL 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