2002-02-14 13:04:35 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "mp_msg.h"
|
2006-04-22 05:12:10 +00:00
|
|
|
#include "help_mp.h"
|
2002-02-14 13:04:35 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2007-03-15 18:36:36 +00:00
|
|
|
#include "stream/stream.h"
|
2002-02-14 13:04:35 +00:00
|
|
|
#include "demuxer.h"
|
|
|
|
#include "stheader.h"
|
|
|
|
#include "genres.h"
|
2002-07-21 14:36:33 +00:00
|
|
|
#include "mp3_hdr.h"
|
2007-06-24 13:20:17 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2002-02-14 13:04:35 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define MP3 1
|
|
|
|
#define WAV 2
|
2003-10-04 22:00:25 +00:00
|
|
|
#define fLaC 3
|
2002-02-14 13:04:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define HDR_SIZE 4
|
|
|
|
|
|
|
|
typedef struct da_priv {
|
|
|
|
int frmt;
|
2007-09-24 21:49:56 +00:00
|
|
|
double next_pts;
|
2002-02-14 13:04:35 +00:00
|
|
|
} da_priv_t;
|
|
|
|
|
2006-04-11 10:43:20 +00:00
|
|
|
//! rather arbitrary value for maximum length of wav-format headers
|
|
|
|
#define MAX_WAVHDR_LEN (1 * 1024 * 1024)
|
|
|
|
|
2006-12-18 15:15:21 +00:00
|
|
|
//! how many valid frames in a row we need before accepting as valid MP3
|
2006-12-18 15:14:23 +00:00
|
|
|
#define MIN_MP3_HDRS 12
|
2004-09-28 17:05:44 +00:00
|
|
|
|
|
|
|
//! Used to describe a potential (chain of) MP3 headers we found
|
|
|
|
typedef struct mp3_hdr {
|
|
|
|
off_t frame_pos; // start of first frame in this "chain" of headers
|
|
|
|
off_t next_frame_pos; // here we expect the next header with same parameters
|
|
|
|
int mp3_chans;
|
|
|
|
int mp3_freq;
|
2005-08-01 18:36:50 +00:00
|
|
|
int mpa_spf;
|
|
|
|
int mpa_layer;
|
|
|
|
int mpa_br;
|
2004-09-28 17:05:44 +00:00
|
|
|
int cons_hdrs; // if this reaches MIN_MP3_HDRS we accept as MP3 file
|
|
|
|
struct mp3_hdr *next;
|
|
|
|
} mp3_hdr_t;
|
|
|
|
|
2006-03-27 17:25:41 +00:00
|
|
|
extern void print_wave_header(WAVEFORMATEX *h, int verbose_level);
|
2002-02-14 13:04:35 +00:00
|
|
|
|
2002-10-23 17:21:01 +00:00
|
|
|
int hr_mp3_seek = 0;
|
2002-02-14 13:04:35 +00:00
|
|
|
|
2004-09-28 17:05:44 +00:00
|
|
|
/**
|
|
|
|
* \brief free a list of MP3 header descriptions
|
|
|
|
* \param list pointer to the head-of-list pointer
|
|
|
|
*/
|
|
|
|
static void free_mp3_hdrs(mp3_hdr_t **list) {
|
|
|
|
mp3_hdr_t *tmp;
|
|
|
|
while (*list) {
|
|
|
|
tmp = (*list)->next;
|
|
|
|
free(*list);
|
|
|
|
*list = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief add another potential MP3 header to our list
|
|
|
|
* If it fits into an existing chain this one is expanded otherwise
|
|
|
|
* a new one is created.
|
|
|
|
* All entries that expected a MP3 header before the current position
|
|
|
|
* are discarded.
|
|
|
|
* The list is expected to be and will be kept sorted by next_frame_pos
|
|
|
|
* and when those are equal by frame_pos.
|
|
|
|
* \param list pointer to the head-of-list pointer
|
|
|
|
* \param st_pos stream position where the described header starts
|
2005-08-01 18:36:50 +00:00
|
|
|
* \param mp3_chans number of channels as specified by the header (*)
|
|
|
|
* \param mp3_freq sampling frequency as specified by the header (*)
|
|
|
|
* \param mpa_spf frame size as specified by the header
|
|
|
|
* \param mpa_layer layer type ("version") as specified by the header (*)
|
|
|
|
* \param mpa_br bitrate as specified by the header
|
2004-09-28 17:05:44 +00:00
|
|
|
* \param mp3_flen length of the frame as specified by the header
|
|
|
|
* \return If non-null the current file is accepted as MP3 and the
|
|
|
|
* mp3_hdr struct describing the valid chain is returned. Must be
|
|
|
|
* freed independent of the list.
|
2005-08-01 18:36:50 +00:00
|
|
|
*
|
|
|
|
* parameters marked by (*) must be the same for all headers in the same chain
|
2004-09-28 17:05:44 +00:00
|
|
|
*/
|
|
|
|
static mp3_hdr_t *add_mp3_hdr(mp3_hdr_t **list, off_t st_pos,
|
2005-08-01 18:36:50 +00:00
|
|
|
int mp3_chans, int mp3_freq, int mpa_spf,
|
|
|
|
int mpa_layer, int mpa_br, int mp3_flen) {
|
2004-09-28 17:05:44 +00:00
|
|
|
mp3_hdr_t *tmp;
|
|
|
|
int in_list = 0;
|
|
|
|
while (*list && (*list)->next_frame_pos <= st_pos) {
|
|
|
|
if (((*list)->next_frame_pos < st_pos) || ((*list)->mp3_chans != mp3_chans)
|
2005-08-01 18:36:50 +00:00
|
|
|
|| ((*list)->mp3_freq != mp3_freq) || ((*list)->mpa_layer != mpa_layer) ) {
|
|
|
|
// wasn't valid!
|
2004-09-28 17:05:44 +00:00
|
|
|
tmp = (*list)->next;
|
|
|
|
free(*list);
|
|
|
|
*list = tmp;
|
|
|
|
} else {
|
|
|
|
(*list)->cons_hdrs++;
|
|
|
|
(*list)->next_frame_pos = st_pos + mp3_flen;
|
2005-08-01 18:36:50 +00:00
|
|
|
(*list)->mpa_spf = mpa_spf;
|
|
|
|
(*list)->mpa_br = mpa_br;
|
2004-09-28 17:05:44 +00:00
|
|
|
if ((*list)->cons_hdrs >= MIN_MP3_HDRS) {
|
|
|
|
// copy the valid entry, so that the list can be easily freed
|
|
|
|
tmp = malloc(sizeof(mp3_hdr_t));
|
|
|
|
memcpy(tmp, *list, sizeof(mp3_hdr_t));
|
|
|
|
tmp->next = NULL;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
in_list = 1;
|
|
|
|
list = &((*list)->next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!in_list) { // does not belong into an existing chain, insert
|
2005-04-18 20:51:34 +00:00
|
|
|
// find right position to insert to keep sorting
|
|
|
|
while (*list && (*list)->next_frame_pos <= st_pos + mp3_flen)
|
|
|
|
list = &((*list)->next);
|
2004-09-28 17:05:44 +00:00
|
|
|
tmp = malloc(sizeof(mp3_hdr_t));
|
|
|
|
tmp->frame_pos = st_pos;
|
|
|
|
tmp->next_frame_pos = st_pos + mp3_flen;
|
|
|
|
tmp->mp3_chans = mp3_chans;
|
|
|
|
tmp->mp3_freq = mp3_freq;
|
2005-08-01 18:36:50 +00:00
|
|
|
tmp->mpa_spf = mpa_spf;
|
|
|
|
tmp->mpa_layer = mpa_layer;
|
|
|
|
tmp->mpa_br = mpa_br;
|
2004-09-28 17:05:44 +00:00
|
|
|
tmp->cons_hdrs = 1;
|
|
|
|
tmp->next = *list;
|
|
|
|
*list = tmp;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-01-29 22:44:55 +00:00
|
|
|
#if 0 /* this code is a mess, clean it up before reenabling */
|
2006-03-20 20:06:04 +00:00
|
|
|
#define FLAC_SIGNATURE_SIZE 4
|
|
|
|
#define FLAC_STREAMINFO_SIZE 34
|
|
|
|
#define FLAC_SEEKPOINT_SIZE 18
|
|
|
|
|
|
|
|
enum {
|
|
|
|
FLAC_STREAMINFO = 0,
|
|
|
|
FLAC_PADDING,
|
|
|
|
FLAC_APPLICATION,
|
|
|
|
FLAC_SEEKTABLE,
|
|
|
|
FLAC_VORBIS_COMMENT,
|
|
|
|
FLAC_CUESHEET
|
|
|
|
} flac_preamble_t;
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_flac_metadata (demuxer_t* demuxer)
|
|
|
|
{
|
|
|
|
uint8_t preamble[4];
|
|
|
|
unsigned int blk_len;
|
2008-04-12 15:11:03 +00:00
|
|
|
stream_t *s = demuxer->stream;
|
2006-03-20 20:06:04 +00:00
|
|
|
|
|
|
|
/* file is qualified; skip over the signature bytes in the stream */
|
|
|
|
stream_seek (s, 4);
|
|
|
|
|
|
|
|
/* loop through the metadata blocks; use a do-while construct since there
|
|
|
|
* will always be 1 metadata block */
|
|
|
|
do {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = stream_read (s, (char *) preamble, FLAC_SIGNATURE_SIZE);
|
|
|
|
if (r != FLAC_SIGNATURE_SIZE)
|
|
|
|
return;
|
|
|
|
|
2008-04-12 15:14:02 +00:00
|
|
|
blk_len = AV_RB24(preamble + 1);
|
2006-03-20 20:06:04 +00:00
|
|
|
|
|
|
|
switch (preamble[0] & 0x7F)
|
|
|
|
{
|
|
|
|
case FLAC_VORBIS_COMMENT:
|
|
|
|
{
|
|
|
|
/* For a description of the format please have a look at */
|
|
|
|
/* http://www.xiph.org/vorbis/doc/v-comment.html */
|
|
|
|
|
|
|
|
uint32_t length, comment_list_len;
|
|
|
|
char comments[blk_len];
|
2007-06-24 13:21:50 +00:00
|
|
|
uint8_t *ptr = comments;
|
2006-03-20 20:06:04 +00:00
|
|
|
char *comment;
|
|
|
|
int cn;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if (stream_read (s, comments, blk_len) == blk_len)
|
|
|
|
{
|
2007-06-24 13:21:50 +00:00
|
|
|
length = AV_RL32(ptr);
|
2006-03-20 20:06:04 +00:00
|
|
|
ptr += 4 + length;
|
|
|
|
|
2007-06-24 13:21:50 +00:00
|
|
|
comment_list_len = AV_RL32(ptr);
|
2006-03-20 20:06:04 +00:00
|
|
|
ptr += 4;
|
|
|
|
|
|
|
|
cn = 0;
|
|
|
|
for (; cn < comment_list_len; cn++)
|
|
|
|
{
|
2007-06-24 13:21:50 +00:00
|
|
|
length = AV_RL32(ptr);
|
2006-03-20 20:06:04 +00:00
|
|
|
ptr += 4;
|
|
|
|
|
2007-06-24 13:21:50 +00:00
|
|
|
comment = ptr;
|
2008-01-29 22:00:58 +00:00
|
|
|
if (&comment[length] < comments || &comment[length] >= &comments[blk_len])
|
|
|
|
return;
|
2006-03-20 20:06:04 +00:00
|
|
|
c = comment[length];
|
|
|
|
comment[length] = 0;
|
|
|
|
|
|
|
|
if (!strncasecmp ("TITLE=", comment, 6) && (length - 6 > 0))
|
|
|
|
demux_info_add (demuxer, "Title", comment + 6);
|
|
|
|
else if (!strncasecmp ("ARTIST=", comment, 7) && (length - 7 > 0))
|
|
|
|
demux_info_add (demuxer, "Artist", comment + 7);
|
|
|
|
else if (!strncasecmp ("ALBUM=", comment, 6) && (length - 6 > 0))
|
|
|
|
demux_info_add (demuxer, "Album", comment + 6);
|
|
|
|
else if (!strncasecmp ("DATE=", comment, 5) && (length - 5 > 0))
|
|
|
|
demux_info_add (demuxer, "Year", comment + 5);
|
|
|
|
else if (!strncasecmp ("GENRE=", comment, 6) && (length - 6 > 0))
|
|
|
|
demux_info_add (demuxer, "Genre", comment + 6);
|
|
|
|
else if (!strncasecmp ("Comment=", comment, 8) && (length - 8 > 0))
|
|
|
|
demux_info_add (demuxer, "Comment", comment + 8);
|
|
|
|
else if (!strncasecmp ("TRACKNUMBER=", comment, 12)
|
|
|
|
&& (length - 12 > 0))
|
|
|
|
{
|
|
|
|
char buf[31];
|
|
|
|
buf[30] = '\0';
|
|
|
|
sprintf (buf, "%d", atoi (comment + 12));
|
|
|
|
demux_info_add(demuxer, "Track", buf);
|
|
|
|
}
|
|
|
|
comment[length] = c;
|
|
|
|
|
|
|
|
ptr += length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-04-12 15:16:37 +00:00
|
|
|
case FLAC_STREAMINFO:
|
2008-04-12 15:09:40 +00:00
|
|
|
case FLAC_PADDING:
|
|
|
|
case FLAC_APPLICATION:
|
2008-04-12 15:16:37 +00:00
|
|
|
case FLAC_SEEKTABLE:
|
2006-03-20 20:06:04 +00:00
|
|
|
case FLAC_CUESHEET:
|
|
|
|
default:
|
|
|
|
/* 6-127 are presently reserved */
|
|
|
|
stream_skip (s, blk_len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while ((preamble[0] & 0x80) == 0);
|
|
|
|
}
|
2008-01-29 22:04:46 +00:00
|
|
|
#endif
|
2006-03-20 20:06:04 +00:00
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
static int demux_audio_open(demuxer_t* demuxer) {
|
2002-02-14 13:04:35 +00:00
|
|
|
stream_t *s;
|
|
|
|
sh_audio_t* sh_audio;
|
|
|
|
uint8_t hdr[HDR_SIZE];
|
2005-08-01 18:36:50 +00:00
|
|
|
int frmt = 0, n = 0, step;
|
2004-09-28 17:05:44 +00:00
|
|
|
off_t st_pos = 0, next_frame_pos = 0;
|
|
|
|
// mp3_hdrs list is sorted first by next_frame_pos and then by frame_pos
|
|
|
|
mp3_hdr_t *mp3_hdrs = NULL, *mp3_found = NULL;
|
2002-02-14 13:04:35 +00:00
|
|
|
da_priv_t* priv;
|
|
|
|
|
|
|
|
s = demuxer->stream;
|
|
|
|
|
2004-09-28 17:05:44 +00:00
|
|
|
stream_read(s, hdr, HDR_SIZE);
|
|
|
|
while(n < 30000 && !s->eof) {
|
2005-08-01 18:36:50 +00:00
|
|
|
int mp3_freq, mp3_chans, mp3_flen, mpa_layer, mpa_spf, mpa_br;
|
2004-09-28 17:05:44 +00:00
|
|
|
st_pos = stream_tell(s) - HDR_SIZE;
|
2002-02-14 13:04:35 +00:00
|
|
|
step = 1;
|
|
|
|
|
|
|
|
if( hdr[0] == 'R' && hdr[1] == 'I' && hdr[2] == 'F' && hdr[3] == 'F' ) {
|
|
|
|
stream_skip(s,4);
|
|
|
|
if(s->eof)
|
|
|
|
break;
|
|
|
|
stream_read(s,hdr,4);
|
|
|
|
if(s->eof)
|
|
|
|
break;
|
|
|
|
if(hdr[0] != 'W' || hdr[1] != 'A' || hdr[2] != 'V' || hdr[3] != 'E' )
|
|
|
|
stream_skip(s,-8);
|
|
|
|
else
|
|
|
|
// We found wav header. Now we can have 'fmt ' or a mp3 header
|
|
|
|
// empty the buffer
|
|
|
|
step = 4;
|
2002-11-05 23:28:52 +00:00
|
|
|
} else if( hdr[0] == 'I' && hdr[1] == 'D' && hdr[2] == '3' && (hdr[3] >= 2)) {
|
|
|
|
int len;
|
|
|
|
stream_skip(s,2);
|
|
|
|
stream_read(s,hdr,4);
|
|
|
|
len = (hdr[0]<<21) | (hdr[1]<<14) | (hdr[2]<<7) | hdr[3];
|
|
|
|
stream_skip(s,len);
|
|
|
|
step = 4;
|
2002-02-14 13:04:35 +00:00
|
|
|
} else if( hdr[0] == 'f' && hdr[1] == 'm' && hdr[2] == 't' && hdr[3] == ' ' ) {
|
|
|
|
frmt = WAV;
|
|
|
|
break;
|
2005-08-01 18:36:50 +00:00
|
|
|
} else if((mp3_flen = mp_get_mp3_header(hdr, &mp3_chans, &mp3_freq,
|
|
|
|
&mpa_spf, &mpa_layer, &mpa_br)) > 0) {
|
|
|
|
mp3_found = add_mp3_hdr(&mp3_hdrs, st_pos, mp3_chans, mp3_freq,
|
|
|
|
mpa_spf, mpa_layer, mpa_br, mp3_flen);
|
2004-09-28 17:05:44 +00:00
|
|
|
if (mp3_found) {
|
|
|
|
frmt = MP3;
|
|
|
|
break;
|
|
|
|
}
|
2003-10-04 22:00:25 +00:00
|
|
|
} else if( hdr[0] == 'f' && hdr[1] == 'L' && hdr[2] == 'a' && hdr[3] == 'C' ) {
|
|
|
|
frmt = fLaC;
|
|
|
|
break;
|
2002-02-14 13:04:35 +00:00
|
|
|
}
|
|
|
|
// Add here some other audio format detection
|
|
|
|
if(step < HDR_SIZE)
|
|
|
|
memmove(hdr,&hdr[step],HDR_SIZE-step);
|
2004-09-28 17:05:44 +00:00
|
|
|
stream_read(s, &hdr[HDR_SIZE - step], step);
|
|
|
|
n++;
|
2002-02-14 13:04:35 +00:00
|
|
|
}
|
|
|
|
|
2004-09-28 17:05:44 +00:00
|
|
|
free_mp3_hdrs(&mp3_hdrs);
|
|
|
|
|
2002-02-14 13:04:35 +00:00
|
|
|
if(!frmt)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
sh_audio = new_sh_audio(demuxer,0);
|
|
|
|
|
|
|
|
switch(frmt) {
|
|
|
|
case MP3:
|
2005-08-01 18:36:50 +00:00
|
|
|
sh_audio->format = (mp3_found->mpa_layer < 3 ? 0x50 : 0x55);
|
2004-09-28 17:05:44 +00:00
|
|
|
demuxer->movi_start = mp3_found->frame_pos;
|
|
|
|
next_frame_pos = mp3_found->next_frame_pos;
|
2002-07-21 14:36:33 +00:00
|
|
|
sh_audio->audio.dwSampleSize= 0;
|
2005-08-01 18:36:50 +00:00
|
|
|
sh_audio->audio.dwScale = mp3_found->mpa_spf;
|
2004-09-28 17:05:44 +00:00
|
|
|
sh_audio->audio.dwRate = mp3_found->mp3_freq;
|
2002-07-21 14:36:33 +00:00
|
|
|
sh_audio->wf = malloc(sizeof(WAVEFORMATEX));
|
|
|
|
sh_audio->wf->wFormatTag = sh_audio->format;
|
2004-09-28 17:05:44 +00:00
|
|
|
sh_audio->wf->nChannels = mp3_found->mp3_chans;
|
|
|
|
sh_audio->wf->nSamplesPerSec = mp3_found->mp3_freq;
|
2005-08-01 18:36:50 +00:00
|
|
|
sh_audio->wf->nAvgBytesPerSec = mp3_found->mpa_br * (1000 / 8);
|
|
|
|
sh_audio->wf->nBlockAlign = mp3_found->mpa_spf;
|
2002-07-21 14:36:33 +00:00
|
|
|
sh_audio->wf->wBitsPerSample = 16;
|
|
|
|
sh_audio->wf->cbSize = 0;
|
2005-08-01 18:36:50 +00:00
|
|
|
sh_audio->i_bps = sh_audio->wf->nAvgBytesPerSec;
|
2004-09-28 17:05:44 +00:00
|
|
|
free(mp3_found);
|
|
|
|
mp3_found = NULL;
|
2002-02-14 13:04:35 +00:00
|
|
|
if(s->end_pos) {
|
|
|
|
char tag[4];
|
|
|
|
stream_seek(s,s->end_pos-128);
|
|
|
|
stream_read(s,tag,3);
|
|
|
|
tag[3] = '\0';
|
|
|
|
if(strcmp(tag,"TAG"))
|
|
|
|
demuxer->movi_end = s->end_pos;
|
|
|
|
else {
|
|
|
|
char buf[31];
|
|
|
|
uint8_t g;
|
|
|
|
demuxer->movi_end = stream_tell(s)-3;
|
|
|
|
stream_read(s,buf,30);
|
|
|
|
buf[30] = '\0';
|
|
|
|
demux_info_add(demuxer,"Title",buf);
|
|
|
|
stream_read(s,buf,30);
|
|
|
|
buf[30] = '\0';
|
|
|
|
demux_info_add(demuxer,"Artist",buf);
|
|
|
|
stream_read(s,buf,30);
|
|
|
|
buf[30] = '\0';
|
|
|
|
demux_info_add(demuxer,"Album",buf);
|
|
|
|
stream_read(s,buf,4);
|
2002-02-20 16:55:11 +00:00
|
|
|
buf[4] = '\0';
|
2002-02-14 13:04:35 +00:00
|
|
|
demux_info_add(demuxer,"Year",buf);
|
|
|
|
stream_read(s,buf,30);
|
|
|
|
buf[30] = '\0';
|
|
|
|
demux_info_add(demuxer,"Comment",buf);
|
|
|
|
if(buf[28] == 0 && buf[29] != 0) {
|
|
|
|
uint8_t trk = (uint8_t)buf[29];
|
|
|
|
sprintf(buf,"%d",trk);
|
|
|
|
demux_info_add(demuxer,"Track",buf);
|
|
|
|
}
|
|
|
|
g = stream_read_char(s);
|
|
|
|
demux_info_add(demuxer,"Genre",genres[g]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case WAV: {
|
2002-02-16 05:45:16 +00:00
|
|
|
unsigned int chunk_type;
|
|
|
|
unsigned int chunk_size;
|
2002-02-14 13:04:35 +00:00
|
|
|
WAVEFORMATEX* w;
|
|
|
|
int l;
|
|
|
|
l = stream_read_dword_le(s);
|
2003-03-15 19:40:52 +00:00
|
|
|
if(l < 16) {
|
2003-03-15 14:12:22 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_ERR,"[demux_audio] Bad wav header length: too short (%d)!!!\n",l);
|
2006-10-21 19:58:49 +00:00
|
|
|
l = 16;
|
2002-02-14 13:04:35 +00:00
|
|
|
}
|
2006-04-11 10:43:20 +00:00
|
|
|
if(l > MAX_WAVHDR_LEN) {
|
|
|
|
mp_msg(MSGT_DEMUX,MSGL_ERR,"[demux_audio] Bad wav header length: too long (%d)!!!\n",l);
|
2006-10-21 19:58:49 +00:00
|
|
|
l = 16;
|
2006-04-11 10:43:20 +00:00
|
|
|
}
|
2006-07-13 16:41:13 +00:00
|
|
|
sh_audio->wf = w = malloc(l > sizeof(WAVEFORMATEX) ? l : sizeof(WAVEFORMATEX));
|
2002-02-14 13:04:35 +00:00
|
|
|
w->wFormatTag = sh_audio->format = stream_read_word_le(s);
|
|
|
|
w->nChannels = sh_audio->channels = stream_read_word_le(s);
|
|
|
|
w->nSamplesPerSec = sh_audio->samplerate = stream_read_dword_le(s);
|
|
|
|
w->nAvgBytesPerSec = stream_read_dword_le(s);
|
|
|
|
w->nBlockAlign = stream_read_word_le(s);
|
2007-03-04 12:43:55 +00:00
|
|
|
w->wBitsPerSample = stream_read_word_le(s);
|
|
|
|
sh_audio->samplesize = (w->wBitsPerSample + 7) / 8;
|
2003-03-17 20:38:23 +00:00
|
|
|
w->cbSize = 0;
|
2005-08-01 18:36:50 +00:00
|
|
|
sh_audio->i_bps = sh_audio->wf->nAvgBytesPerSec;
|
2003-03-15 19:40:52 +00:00
|
|
|
l -= 16;
|
|
|
|
if (l > 0) {
|
2003-03-15 14:12:22 +00:00
|
|
|
w->cbSize = stream_read_word_le(s);
|
2003-03-15 19:40:52 +00:00
|
|
|
l -= 2;
|
2003-03-22 20:15:03 +00:00
|
|
|
if (w->cbSize > 0) {
|
2003-03-15 14:12:22 +00:00
|
|
|
if (l < w->cbSize) {
|
|
|
|
mp_msg(MSGT_DEMUX,MSGL_ERR,"[demux_audio] truncated extradata (%d < %d)\n",
|
|
|
|
l,w->cbSize);
|
|
|
|
stream_read(s,(char*)((char*)(w)+sizeof(WAVEFORMATEX)),l);
|
|
|
|
l = 0;
|
|
|
|
} else {
|
|
|
|
stream_read(s,(char*)((char*)(w)+sizeof(WAVEFORMATEX)),w->cbSize);
|
|
|
|
l -= w->cbSize;
|
|
|
|
}
|
2003-03-22 20:15:03 +00:00
|
|
|
}
|
2003-03-15 19:40:52 +00:00
|
|
|
}
|
2003-03-15 14:12:22 +00:00
|
|
|
|
2006-03-27 17:25:41 +00:00
|
|
|
if( mp_msg_test(MSGT_DEMUX,MSGL_V) ) print_wave_header(w, MSGL_V);
|
2002-02-14 13:04:35 +00:00
|
|
|
if(l)
|
|
|
|
stream_skip(s,l);
|
2002-02-16 05:45:16 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
chunk_type = stream_read_fourcc(demuxer->stream);
|
|
|
|
chunk_size = stream_read_dword_le(demuxer->stream);
|
|
|
|
if (chunk_type != mmioFOURCC('d', 'a', 't', 'a'))
|
|
|
|
stream_skip(demuxer->stream, chunk_size);
|
2007-03-15 10:38:51 +00:00
|
|
|
} while (!s->eof && chunk_type != mmioFOURCC('d', 'a', 't', 'a'));
|
2002-02-14 13:04:35 +00:00
|
|
|
demuxer->movi_start = stream_tell(s);
|
2007-02-04 09:33:57 +00:00
|
|
|
demuxer->movi_end = chunk_size ? demuxer->movi_start + chunk_size : s->end_pos;
|
2002-10-22 16:02:21 +00:00
|
|
|
// printf("wav: %X .. %X\n",(int)demuxer->movi_start,(int)demuxer->movi_end);
|
2004-08-12 12:36:08 +00:00
|
|
|
// Check if it contains dts audio
|
|
|
|
if((w->wFormatTag == 0x01) && (w->nChannels == 2) && (w->nSamplesPerSec == 44100)) {
|
|
|
|
unsigned char buf[16384]; // vlc uses 16384*4 (4 dts frames)
|
|
|
|
unsigned int i;
|
|
|
|
stream_read(s, buf, sizeof(buf));
|
2006-03-08 13:07:36 +00:00
|
|
|
for (i = 0; i < sizeof(buf) - 5; i += 2) {
|
2004-08-12 12:36:08 +00:00
|
|
|
// DTS, 14 bit, LE
|
|
|
|
if((buf[i] == 0xff) && (buf[i+1] == 0x1f) && (buf[i+2] == 0x00) &&
|
|
|
|
(buf[i+3] == 0xe8) && ((buf[i+4] & 0xfe) == 0xf0) && (buf[i+5] == 0x07)) {
|
|
|
|
sh_audio->format = 0x2001;
|
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"[demux_audio] DTS audio in wav, 14 bit, LE\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// DTS, 14 bit, BE
|
|
|
|
if((buf[i] == 0x1f) && (buf[i+1] == 0xff) && (buf[i+2] == 0xe8) &&
|
|
|
|
(buf[i+3] == 0x00) && (buf[i+4] == 0x07) && ((buf[i+5] & 0xfe) == 0xf0)) {
|
|
|
|
sh_audio->format = 0x2001;
|
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"[demux_audio] DTS audio in wav, 14 bit, BE\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// DTS, 16 bit, BE
|
|
|
|
if((buf[i] == 0x7f) && (buf[i+1] == 0xfe) && (buf[i+2] == 0x80) &&
|
|
|
|
(buf[i+3] == 0x01)) {
|
|
|
|
sh_audio->format = 0x2001;
|
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"[demux_audio] DTS audio in wav, 16 bit, BE\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// DTS, 16 bit, LE
|
|
|
|
if((buf[i] == 0xfe) && (buf[i+1] == 0x7f) && (buf[i+2] == 0x01) &&
|
|
|
|
(buf[i+3] == 0x80)) {
|
|
|
|
sh_audio->format = 0x2001;
|
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"[demux_audio] DTS audio in wav, 16 bit, LE\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sh_audio->format == 0x2001)
|
|
|
|
mp_msg(MSGT_DEMUX,MSGL_DBG2,"[demux_audio] DTS sync offset = %u\n", i);
|
|
|
|
|
|
|
|
}
|
|
|
|
stream_seek(s,demuxer->movi_start);
|
2002-02-14 13:04:35 +00:00
|
|
|
} break;
|
2003-10-04 22:00:25 +00:00
|
|
|
case fLaC:
|
|
|
|
sh_audio->format = mmioFOURCC('f', 'L', 'a', 'C');
|
2006-04-11 10:46:52 +00:00
|
|
|
demuxer->movi_start = stream_tell(s) - 4;
|
2003-10-04 22:00:25 +00:00
|
|
|
demuxer->movi_end = s->end_pos;
|
2006-04-11 10:46:52 +00:00
|
|
|
if (demuxer->movi_end > demuxer->movi_start) {
|
|
|
|
// try to find out approx. bitrate
|
|
|
|
int64_t size = demuxer->movi_end - demuxer->movi_start;
|
|
|
|
int64_t num_samples = 0;
|
|
|
|
int32_t srate = 0;
|
|
|
|
stream_skip(s, 14);
|
|
|
|
stream_read(s, (char *)&srate, 3);
|
|
|
|
srate = be2me_32(srate) >> 12;
|
|
|
|
stream_read(s, (char *)&num_samples, 5);
|
2006-04-16 13:45:21 +00:00
|
|
|
num_samples = (be2me_64(num_samples) >> 24) & 0xfffffffffULL;
|
2006-04-11 10:46:52 +00:00
|
|
|
if (num_samples && srate)
|
|
|
|
sh_audio->i_bps = size * srate / num_samples;
|
|
|
|
}
|
|
|
|
if (sh_audio->i_bps < 1) // guess value to prevent crash
|
|
|
|
sh_audio->i_bps = 64 * 1024;
|
2008-01-29 22:04:46 +00:00
|
|
|
// get_flac_metadata (demuxer);
|
2003-10-04 22:00:25 +00:00
|
|
|
break;
|
2002-02-14 13:04:35 +00:00
|
|
|
}
|
|
|
|
|
2006-07-13 16:41:13 +00:00
|
|
|
priv = malloc(sizeof(da_priv_t));
|
2002-02-14 13:04:35 +00:00
|
|
|
priv->frmt = frmt;
|
2007-09-24 21:49:56 +00:00
|
|
|
priv->next_pts = 0;
|
2002-02-14 13:04:35 +00:00
|
|
|
demuxer->priv = priv;
|
|
|
|
demuxer->audio->id = 0;
|
|
|
|
demuxer->audio->sh = sh_audio;
|
|
|
|
sh_audio->ds = demuxer->audio;
|
2003-06-04 10:52:58 +00:00
|
|
|
sh_audio->samplerate = sh_audio->audio.dwRate;
|
2002-02-14 13:04:35 +00:00
|
|
|
|
|
|
|
if(stream_tell(s) != demuxer->movi_start)
|
2004-09-28 17:05:44 +00:00
|
|
|
{
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "demux_audio: seeking from 0x%X to start pos 0x%X\n",
|
|
|
|
(int)stream_tell(s), (int)demuxer->movi_start);
|
2002-02-14 13:04:35 +00:00
|
|
|
stream_seek(s,demuxer->movi_start);
|
2004-09-28 17:05:44 +00:00
|
|
|
if (stream_tell(s) != demuxer->movi_start) {
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "demux_audio: seeking failed, now at 0x%X!\n",
|
|
|
|
(int)stream_tell(s));
|
|
|
|
if (next_frame_pos) {
|
|
|
|
mp_msg(MSGT_DEMUX, MSGL_V, "demux_audio: seeking to 0x%X instead\n",
|
|
|
|
(int)next_frame_pos);
|
|
|
|
stream_seek(s, next_frame_pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-02-14 13:04:35 +00:00
|
|
|
|
2004-09-28 17:05:44 +00:00
|
|
|
mp_msg(MSGT_DEMUX,MSGL_V,"demux_audio: audio data 0x%X - 0x%X \n",(int)demuxer->movi_start,(int)demuxer->movi_end);
|
2002-02-15 00:16:21 +00:00
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
return DEMUXER_TYPE_AUDIO;
|
2002-02-14 13:04:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
static int demux_audio_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds) {
|
2005-01-29 12:55:56 +00:00
|
|
|
int l;
|
|
|
|
demux_packet_t* dp;
|
2007-09-24 22:21:14 +00:00
|
|
|
sh_audio_t* sh_audio = ds->sh;
|
|
|
|
demuxer_t* demux = ds->demuxer;
|
|
|
|
da_priv_t* priv = demux->priv;
|
|
|
|
double this_pts = priv->next_pts;
|
|
|
|
stream_t* s = demux->stream;
|
2002-02-14 13:04:35 +00:00
|
|
|
|
2005-09-09 20:05:42 +00:00
|
|
|
if(s->eof)
|
2002-02-14 13:04:35 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch(priv->frmt) {
|
|
|
|
case MP3 :
|
2005-01-29 12:55:56 +00:00
|
|
|
while(1) {
|
2002-02-14 13:04:35 +00:00
|
|
|
uint8_t hdr[4];
|
|
|
|
stream_read(s,hdr,4);
|
2005-09-09 20:05:42 +00:00
|
|
|
if (s->eof)
|
2005-01-29 12:55:56 +00:00
|
|
|
return 0;
|
|
|
|
l = mp_decode_mp3_header(hdr);
|
|
|
|
if(l < 0) {
|
2005-09-09 20:05:42 +00:00
|
|
|
if (demux->movi_end && stream_tell(s) >= demux->movi_end)
|
|
|
|
return 0; // might be ID3 tag, i.e. EOF
|
2002-02-14 13:04:35 +00:00
|
|
|
stream_skip(s,-3);
|
|
|
|
} else {
|
2005-01-29 12:55:56 +00:00
|
|
|
dp = new_demux_packet(l);
|
2002-02-14 13:04:35 +00:00
|
|
|
memcpy(dp->buffer,hdr,4);
|
2005-09-09 20:05:42 +00:00
|
|
|
if (stream_read(s,dp->buffer + 4,l-4) != l-4)
|
2006-03-09 22:14:34 +00:00
|
|
|
{
|
|
|
|
free_demux_packet(dp);
|
2005-09-09 20:05:42 +00:00
|
|
|
return 0;
|
2006-03-09 22:14:34 +00:00
|
|
|
}
|
2007-09-24 21:49:56 +00:00
|
|
|
priv->next_pts += sh_audio->audio.dwScale/(double)sh_audio->samplerate;
|
2005-01-29 12:55:56 +00:00
|
|
|
break;
|
2002-02-14 13:04:35 +00:00
|
|
|
}
|
2002-02-14 18:37:33 +00:00
|
|
|
} break;
|
2002-02-14 13:04:35 +00:00
|
|
|
case WAV : {
|
2006-02-07 22:12:34 +00:00
|
|
|
unsigned align = sh_audio->wf->nBlockAlign;
|
2005-01-29 12:55:56 +00:00
|
|
|
l = sh_audio->wf->nAvgBytesPerSec;
|
2008-01-19 14:43:58 +00:00
|
|
|
if (l <= 0) l = 65536;
|
2007-02-04 09:33:57 +00:00
|
|
|
if (demux->movi_end && l > demux->movi_end - stream_tell(s)) {
|
|
|
|
// do not read beyond end, there might be junk after data chunk
|
|
|
|
l = demux->movi_end - stream_tell(s);
|
|
|
|
if (l <= 0) return 0;
|
|
|
|
}
|
2006-02-07 22:12:34 +00:00
|
|
|
if (align)
|
|
|
|
l = (l + align - 1) / align * align;
|
2005-01-29 12:55:56 +00:00
|
|
|
dp = new_demux_packet(l);
|
2003-09-20 12:50:25 +00:00
|
|
|
l = stream_read(s,dp->buffer,l);
|
2007-09-24 21:49:56 +00:00
|
|
|
priv->next_pts += l/(double)sh_audio->i_bps;
|
2005-01-29 12:55:56 +00:00
|
|
|
break;
|
2002-02-14 13:04:35 +00:00
|
|
|
}
|
2003-10-04 22:00:25 +00:00
|
|
|
case fLaC: {
|
2005-01-29 12:55:56 +00:00
|
|
|
l = 65535;
|
|
|
|
dp = new_demux_packet(l);
|
2003-10-04 22:00:25 +00:00
|
|
|
l = stream_read(s,dp->buffer,l);
|
2007-09-24 21:49:56 +00:00
|
|
|
/* FLAC is not a constant-bitrate codec. These values will be wrong. */
|
|
|
|
priv->next_pts += l/(double)sh_audio->i_bps;
|
2005-01-29 12:55:56 +00:00
|
|
|
break;
|
2003-10-04 22:00:25 +00:00
|
|
|
}
|
2002-02-14 13:04:35 +00:00
|
|
|
default:
|
2006-04-22 05:12:10 +00:00
|
|
|
mp_msg(MSGT_DEMUXER,MSGL_WARN,MSGTR_MPDEMUX_AUDIO_UnknownFormat,priv->frmt);
|
2005-01-29 12:55:56 +00:00
|
|
|
return 0;
|
2002-02-14 13:04:35 +00:00
|
|
|
}
|
|
|
|
|
2005-01-29 12:55:56 +00:00
|
|
|
resize_demux_packet(dp, l);
|
2007-09-24 21:49:56 +00:00
|
|
|
dp->pts = this_pts;
|
2005-01-29 12:55:56 +00:00
|
|
|
ds_add_packet(ds, dp);
|
|
|
|
return 1;
|
2002-02-14 13:04:35 +00:00
|
|
|
}
|
|
|
|
|
2002-02-20 16:55:11 +00:00
|
|
|
static void high_res_mp3_seek(demuxer_t *demuxer,float time) {
|
|
|
|
uint8_t hdr[4];
|
|
|
|
int len,nf;
|
|
|
|
da_priv_t* priv = demuxer->priv;
|
|
|
|
sh_audio_t* sh = (sh_audio_t*)demuxer->audio->sh;
|
|
|
|
|
2005-04-17 09:42:51 +00:00
|
|
|
nf = time*sh->samplerate/sh->audio.dwScale;
|
2002-02-20 16:55:11 +00:00
|
|
|
while(nf > 0) {
|
|
|
|
stream_read(demuxer->stream,hdr,4);
|
|
|
|
len = mp_decode_mp3_header(hdr);
|
|
|
|
if(len < 0) {
|
|
|
|
stream_skip(demuxer->stream,-3);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
stream_skip(demuxer->stream,len-4);
|
2007-09-24 21:49:56 +00:00
|
|
|
priv->next_pts += sh->audio.dwScale/(double)sh->samplerate;
|
2002-02-20 16:55:11 +00:00
|
|
|
nf--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-17 01:57:41 +00:00
|
|
|
static void demux_audio_seek(demuxer_t *demuxer,float rel_seek_secs,float audio_delay,int flags){
|
2002-02-14 13:04:35 +00:00
|
|
|
sh_audio_t* sh_audio;
|
|
|
|
stream_t* s;
|
|
|
|
int base,pos;
|
|
|
|
float len;
|
|
|
|
da_priv_t* priv;
|
|
|
|
|
2002-02-20 16:55:11 +00:00
|
|
|
if(!(sh_audio = demuxer->audio->sh))
|
|
|
|
return;
|
2002-02-14 13:04:35 +00:00
|
|
|
s = demuxer->stream;
|
|
|
|
priv = demuxer->priv;
|
|
|
|
|
2008-01-29 15:11:38 +00:00
|
|
|
if(priv->frmt == MP3 && hr_mp3_seek && !(flags & SEEK_FACTOR)) {
|
|
|
|
len = (flags & SEEK_ABSOLUTE) ? rel_seek_secs - priv->next_pts : rel_seek_secs;
|
2002-02-20 16:55:11 +00:00
|
|
|
if(len < 0) {
|
|
|
|
stream_seek(s,demuxer->movi_start);
|
2007-09-24 21:49:56 +00:00
|
|
|
len = priv->next_pts + len;
|
|
|
|
priv->next_pts = 0;
|
2002-02-20 16:55:11 +00:00
|
|
|
}
|
|
|
|
if(len > 0)
|
|
|
|
high_res_mp3_seek(demuxer,len);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-29 15:11:38 +00:00
|
|
|
base = flags&SEEK_ABSOLUTE ? demuxer->movi_start : stream_tell(s);
|
|
|
|
if(flags&SEEK_FACTOR)
|
2002-04-23 13:27:46 +00:00
|
|
|
pos = base + ((demuxer->movi_end - demuxer->movi_start)*rel_seek_secs);
|
|
|
|
else
|
|
|
|
pos = base + (rel_seek_secs*sh_audio->i_bps);
|
2002-02-14 13:04:35 +00:00
|
|
|
|
|
|
|
if(demuxer->movi_end && pos >= demuxer->movi_end) {
|
2002-12-04 23:51:44 +00:00
|
|
|
pos = demuxer->movi_end;
|
2002-02-14 13:04:35 +00:00
|
|
|
} else if(pos < demuxer->movi_start)
|
|
|
|
pos = demuxer->movi_start;
|
|
|
|
|
2007-09-24 21:49:56 +00:00
|
|
|
priv->next_pts = (pos-demuxer->movi_start)/(double)sh_audio->i_bps;
|
2002-02-20 16:55:11 +00:00
|
|
|
|
2002-02-14 13:04:35 +00:00
|
|
|
switch(priv->frmt) {
|
|
|
|
case WAV:
|
2005-10-20 15:15:53 +00:00
|
|
|
pos -= (pos - demuxer->movi_start) %
|
|
|
|
(sh_audio->wf->nBlockAlign ? sh_audio->wf->nBlockAlign :
|
|
|
|
(sh_audio->channels * sh_audio->samplesize));
|
2002-02-14 13:04:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream_seek(s,pos);
|
|
|
|
}
|
2002-02-20 16:55:11 +00:00
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
static void demux_close_audio(demuxer_t* demuxer) {
|
2002-04-24 17:28:23 +00:00
|
|
|
da_priv_t* priv = demuxer->priv;
|
|
|
|
|
|
|
|
if(!priv)
|
|
|
|
return;
|
|
|
|
free(priv);
|
|
|
|
}
|
|
|
|
|
2005-08-05 19:57:47 +00:00
|
|
|
static int demux_audio_control(demuxer_t *demuxer,int cmd, void *arg){
|
2003-09-19 19:40:46 +00:00
|
|
|
sh_audio_t *sh_audio=demuxer->audio->sh;
|
2008-01-19 14:38:24 +00:00
|
|
|
int audio_length = sh_audio->i_bps ? demuxer->movi_end / sh_audio->i_bps : 0;
|
2003-09-19 19:40:46 +00:00
|
|
|
da_priv_t* priv = demuxer->priv;
|
|
|
|
|
|
|
|
switch(cmd) {
|
|
|
|
case DEMUXER_CTRL_GET_TIME_LENGTH:
|
|
|
|
if (audio_length<=0) return DEMUXER_CTRL_DONTKNOW;
|
2005-09-02 08:32:32 +00:00
|
|
|
*((double *)arg)=(double)audio_length;
|
2003-09-19 19:40:46 +00:00
|
|
|
return DEMUXER_CTRL_GUESS;
|
|
|
|
|
|
|
|
case DEMUXER_CTRL_GET_PERCENT_POS:
|
|
|
|
if (audio_length<=0)
|
|
|
|
return DEMUXER_CTRL_DONTKNOW;
|
2007-09-24 21:49:56 +00:00
|
|
|
*((int *)arg)=(int)( (priv->next_pts*100) / audio_length);
|
2003-09-19 19:40:46 +00:00
|
|
|
return DEMUXER_CTRL_OK;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return DEMUXER_CTRL_NOTIMPL;
|
|
|
|
}
|
|
|
|
}
|
2005-08-05 19:57:47 +00:00
|
|
|
|
|
|
|
|
2008-01-13 16:00:39 +00:00
|
|
|
const demuxer_desc_t demuxer_desc_audio = {
|
2005-08-05 19:57:47 +00:00
|
|
|
"Audio demuxer",
|
|
|
|
"audio",
|
|
|
|
"Audio file",
|
|
|
|
"?",
|
|
|
|
"Audio only files",
|
|
|
|
DEMUXER_TYPE_AUDIO,
|
|
|
|
0, // unsafe autodetect
|
|
|
|
demux_audio_open,
|
|
|
|
demux_audio_fill_buffer,
|
|
|
|
NULL,
|
|
|
|
demux_close_audio,
|
|
|
|
demux_audio_seek,
|
|
|
|
demux_audio_control
|
|
|
|
};
|