2005-07-23 18:07:08 +00:00
|
|
|
/**
|
|
|
|
* Demuxer for Musepack v7 bitstream
|
2007-03-12 15:52:34 +00:00
|
|
|
* by Reimar Doeffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>
|
2005-07-23 18:07:08 +00:00
|
|
|
* This code may be be relicensed under the terms of the GNU LGPL when it
|
2005-07-26 00:01:44 +00:00
|
|
|
* becomes part of the FFmpeg project (ffmpeg.org)
|
2005-07-23 18:07:08 +00:00
|
|
|
*/
|
2005-07-10 17:14:12 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "mp_msg.h"
|
2006-11-29 15:02:45 +00:00
|
|
|
#include "libavutil/common.h"
|
2006-12-07 11:58:07 +00:00
|
|
|
#include "mpbswap.h"
|
2007-03-15 18:36:36 +00:00
|
|
|
#include "stream/stream.h"
|
2005-07-10 17:14:12 +00:00
|
|
|
#include "demuxer.h"
|
|
|
|
#include "stheader.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define HDR_SIZE (6 * 4)
|
|
|
|
|
|
|
|
typedef struct da_priv {
|
|
|
|
float last_pts;
|
2005-10-12 21:55:55 +00:00
|
|
|
float pts_per_packet;
|
2005-07-10 17:14:12 +00:00
|
|
|
uint32_t dword;
|
|
|
|
int pos;
|
2005-09-27 19:04:10 +00:00
|
|
|
float length;
|
2005-07-10 17:14:12 +00:00
|
|
|
} da_priv_t;
|
|
|
|
|
|
|
|
static uint32_t get_bits(da_priv_t* priv, stream_t* s, int bits) {
|
|
|
|
uint32_t out = priv->dword;
|
|
|
|
uint32_t mask = (1 << bits) - 1;
|
|
|
|
priv->pos += bits;
|
|
|
|
if (priv->pos < 32) {
|
|
|
|
out >>= (32 - priv->pos);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stream_read(s, (void *)&priv->dword, 4);
|
2005-07-29 15:52:06 +00:00
|
|
|
priv->dword = le2me_32(priv->dword);
|
2005-07-10 17:14:12 +00:00
|
|
|
priv->pos -= 32;
|
|
|
|
if (priv->pos) {
|
|
|
|
out <<= priv->pos;
|
|
|
|
out |= priv->dword >> (32 - priv->pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out & mask;
|
|
|
|
}
|
|
|
|
|
2005-09-06 19:40:19 +00:00
|
|
|
static int demux_mpc_check(demuxer_t* demuxer) {
|
2005-07-10 17:14:12 +00:00
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
uint8_t hdr[HDR_SIZE];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (stream_read(s, hdr, HDR_SIZE) != HDR_SIZE)
|
|
|
|
return 0;
|
|
|
|
for (i = 0; i < 30000 && !s->eof; i++) {
|
|
|
|
if (hdr[0] == 'M' && hdr[1] == 'P' && hdr[2] == '+')
|
|
|
|
break;
|
|
|
|
memmove(hdr, &hdr[1], HDR_SIZE - 1);
|
|
|
|
stream_read(s, &hdr[HDR_SIZE - 1], 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hdr[0] != 'M' || hdr[1] != 'P' || hdr[2] != '+')
|
|
|
|
return 0;
|
2005-09-06 20:35:14 +00:00
|
|
|
demuxer->movi_start = stream_tell(s) - HDR_SIZE;
|
|
|
|
demuxer->movi_end = s->end_pos;
|
2005-09-06 19:40:19 +00:00
|
|
|
demuxer->priv = malloc(HDR_SIZE);
|
|
|
|
memcpy(demuxer->priv, hdr, HDR_SIZE);
|
|
|
|
return DEMUXER_TYPE_MPC;
|
|
|
|
}
|
|
|
|
|
|
|
|
static demuxer_t *demux_mpc_open(demuxer_t* demuxer) {
|
2005-09-27 19:04:10 +00:00
|
|
|
float seconds = 0;
|
2005-09-06 19:40:19 +00:00
|
|
|
stream_t *s = demuxer->stream;
|
|
|
|
sh_audio_t* sh_audio;
|
|
|
|
da_priv_t* priv = demuxer->priv;
|
2005-07-10 17:14:12 +00:00
|
|
|
|
|
|
|
sh_audio = new_sh_audio(demuxer,0);
|
|
|
|
|
|
|
|
{
|
2006-07-03 13:21:54 +00:00
|
|
|
char *wf = calloc(1, sizeof(WAVEFORMATEX) + HDR_SIZE);
|
2005-07-10 17:14:12 +00:00
|
|
|
char *header = &wf[sizeof(WAVEFORMATEX)];
|
|
|
|
const int freqs[4] = {44100, 48000, 37800, 32000};
|
2005-09-06 20:35:14 +00:00
|
|
|
int frames;
|
2005-07-10 17:14:12 +00:00
|
|
|
sh_audio->format = mmioFOURCC('M', 'P', 'C', ' ');
|
2005-09-06 19:40:19 +00:00
|
|
|
memcpy(header, priv, HDR_SIZE);
|
|
|
|
free(priv);
|
2005-09-06 20:35:14 +00:00
|
|
|
frames = header[4] | header[5] << 8 | header[6] << 16 | header[7] << 24;
|
2005-07-10 17:14:12 +00:00
|
|
|
sh_audio->wf = (WAVEFORMATEX *)wf;
|
|
|
|
sh_audio->wf->wFormatTag = sh_audio->format;
|
|
|
|
sh_audio->wf->nChannels = 2;
|
|
|
|
sh_audio->wf->nSamplesPerSec = freqs[header[10] & 3];
|
|
|
|
sh_audio->wf->nBlockAlign = 32 * 36;
|
|
|
|
sh_audio->wf->wBitsPerSample = 16;
|
2005-09-27 19:04:10 +00:00
|
|
|
seconds = 1152 * frames / (float)sh_audio->wf->nSamplesPerSec;
|
|
|
|
if (demuxer->movi_end > demuxer->movi_start && seconds > 1)
|
2005-09-06 20:35:14 +00:00
|
|
|
sh_audio->wf->nAvgBytesPerSec = (demuxer->movi_end - demuxer->movi_start) / seconds;
|
|
|
|
else
|
|
|
|
sh_audio->wf->nAvgBytesPerSec = 32 * 1024; // dummy to make mencoder not hang
|
2005-07-10 17:14:12 +00:00
|
|
|
sh_audio->wf->cbSize = HDR_SIZE;
|
|
|
|
demuxer->movi_start = stream_tell(s);
|
|
|
|
demuxer->movi_end = s->end_pos;
|
|
|
|
}
|
|
|
|
|
2006-07-03 13:21:54 +00:00
|
|
|
priv = malloc(sizeof(da_priv_t));
|
2005-07-10 17:14:12 +00:00
|
|
|
priv->last_pts = -1;
|
2005-10-12 21:55:55 +00:00
|
|
|
priv->pts_per_packet = (32 * 36) / (float)sh_audio->wf->nSamplesPerSec;
|
2005-09-27 19:04:10 +00:00
|
|
|
priv->length = seconds;
|
2005-07-10 17:14:12 +00:00
|
|
|
priv->dword = 0;
|
2005-10-12 20:45:52 +00:00
|
|
|
priv->pos = 32; // empty bit buffer
|
|
|
|
get_bits(priv, s, 8); // discard first 8 bits
|
2005-07-10 17:14:12 +00:00
|
|
|
demuxer->priv = priv;
|
|
|
|
demuxer->audio->id = 0;
|
|
|
|
demuxer->audio->sh = sh_audio;
|
|
|
|
sh_audio->ds = demuxer->audio;
|
|
|
|
sh_audio->samplerate = sh_audio->wf->nSamplesPerSec;
|
|
|
|
sh_audio->i_bps = sh_audio->wf->nAvgBytesPerSec;
|
|
|
|
sh_audio->audio.dwSampleSize = 0;
|
|
|
|
sh_audio->audio.dwScale = 32 * 36;
|
|
|
|
sh_audio->audio.dwRate = sh_audio->samplerate;
|
|
|
|
|
2005-09-06 19:40:19 +00:00
|
|
|
return demuxer;
|
2005-07-10 17:14:12 +00:00
|
|
|
}
|
|
|
|
|
2005-09-06 19:40:19 +00:00
|
|
|
static int demux_mpc_fill_buffer(demuxer_t *demux, demux_stream_t *ds) {
|
2005-07-10 17:14:12 +00:00
|
|
|
int l;
|
|
|
|
int bit_len;
|
|
|
|
demux_packet_t* dp;
|
|
|
|
sh_audio_t* sh_audio = ds->sh;
|
|
|
|
da_priv_t* priv = demux->priv;
|
|
|
|
stream_t* s = demux->stream;
|
|
|
|
sh_audio = ds->sh;
|
|
|
|
|
|
|
|
if (s->eof)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bit_len = get_bits(priv, s, 20);
|
|
|
|
dp = new_demux_packet((bit_len + 7) / 8);
|
|
|
|
for (l = 0; l < (bit_len / 8); l++)
|
|
|
|
dp->buffer[l] = get_bits(priv, s, 8);
|
|
|
|
bit_len %= 8;
|
|
|
|
if (bit_len)
|
|
|
|
dp->buffer[l] = get_bits(priv, s, bit_len) << (8 - bit_len);
|
|
|
|
if (priv->last_pts < 0)
|
|
|
|
priv->last_pts = 0;
|
|
|
|
else
|
2005-10-12 21:55:55 +00:00
|
|
|
priv->last_pts += priv->pts_per_packet;
|
2006-06-14 19:48:54 +00:00
|
|
|
dp->pts = priv->last_pts;
|
2005-07-10 17:14:12 +00:00
|
|
|
ds_add_packet(ds, dp);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-02-17 01:57:41 +00:00
|
|
|
static void demux_mpc_seek(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){
|
2005-10-12 21:55:55 +00:00
|
|
|
sh_audio_t* sh_audio = demuxer->audio->sh;
|
|
|
|
da_priv_t* priv = demuxer->priv;
|
|
|
|
stream_t* s = demuxer->stream;
|
|
|
|
float target = rel_seek_secs;
|
|
|
|
if (flags & 2)
|
|
|
|
target *= priv->length;
|
|
|
|
if (!(flags & 1))
|
|
|
|
target += priv->last_pts;
|
|
|
|
if (target < priv->last_pts) {
|
|
|
|
stream_seek(s, demuxer->movi_start);
|
|
|
|
priv->pos = 32; // empty bit buffer
|
|
|
|
get_bits(priv, s, 8); // discard first 8 bits
|
|
|
|
priv->last_pts = 0;
|
|
|
|
}
|
|
|
|
while (target > priv->last_pts) {
|
|
|
|
int bit_len = get_bits(priv, s, 20);
|
|
|
|
if (bit_len > 32) {
|
|
|
|
stream_skip(s, bit_len / 32 * 4 - 4);
|
|
|
|
get_bits(priv, s, 32); // make sure dword is reloaded
|
|
|
|
}
|
|
|
|
get_bits(priv, s, bit_len % 32);
|
|
|
|
priv->last_pts += priv->pts_per_packet;
|
|
|
|
if (s->eof) break;
|
|
|
|
}
|
|
|
|
if (!sh_audio) return;
|
2005-07-10 17:14:12 +00:00
|
|
|
}
|
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
static void demux_close_mpc(demuxer_t* demuxer) {
|
2005-07-10 17:14:12 +00:00
|
|
|
da_priv_t* priv = demuxer->priv;
|
|
|
|
|
|
|
|
if(!priv)
|
|
|
|
return;
|
|
|
|
free(priv);
|
|
|
|
}
|
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
static int demux_mpc_control(demuxer_t *demuxer,int cmd, void *arg){
|
2005-09-27 19:04:10 +00:00
|
|
|
da_priv_t* priv = demuxer->priv;
|
|
|
|
switch (cmd) {
|
|
|
|
case DEMUXER_CTRL_GET_TIME_LENGTH:
|
|
|
|
if (priv->length < 1) return DEMUXER_CTRL_DONTKNOW;
|
|
|
|
*((double *)arg) = priv->length;
|
|
|
|
return DEMUXER_CTRL_OK;
|
|
|
|
case DEMUXER_CTRL_GET_PERCENT_POS:
|
|
|
|
if (priv->length < 1) return DEMUXER_CTRL_DONTKNOW;
|
|
|
|
*((int *)arg) = priv->last_pts * 100 / priv->length;
|
|
|
|
return DEMUXER_CTRL_OK;
|
|
|
|
}
|
2005-07-10 17:14:12 +00:00
|
|
|
return DEMUXER_CTRL_NOTIMPL;
|
|
|
|
}
|
2005-08-05 19:57:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
demuxer_desc_t demuxer_desc_mpc = {
|
|
|
|
"Musepack demuxer",
|
|
|
|
"mpc",
|
|
|
|
"MPC",
|
|
|
|
"Reza Jelveh, Reimar Doeffinger",
|
|
|
|
"supports v7 bitstream only",
|
|
|
|
DEMUXER_TYPE_MPC,
|
|
|
|
0, // unsafe autodetect
|
2005-09-06 19:40:19 +00:00
|
|
|
demux_mpc_check,
|
2005-08-05 19:57:47 +00:00
|
|
|
demux_mpc_fill_buffer,
|
2005-09-06 19:40:19 +00:00
|
|
|
demux_mpc_open,
|
2005-08-05 19:57:47 +00:00
|
|
|
demux_close_mpc,
|
|
|
|
demux_mpc_seek,
|
|
|
|
demux_mpc_control
|
|
|
|
};
|