2005-12-17 18:14:38 +00:00
|
|
|
/*
|
2006-10-23 08:57:54 +00:00
|
|
|
* WAV muxer and demuxer
|
2002-10-28 00:55:25 +00:00
|
|
|
* Copyright (c) 2001, 2002 Fabrice Bellard.
|
2001-07-22 14:18:56 +00:00
|
|
|
*
|
2006-10-07 15:30:46 +00:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2002-05-25 22:34:32 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 15:30:46 +00:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2001-07-22 14:18:56 +00:00
|
|
|
*
|
2006-10-07 15:30:46 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2001-07-22 14:18:56 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2002-05-25 22:34:32 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-07-22 14:18:56 +00:00
|
|
|
*
|
2002-05-25 22:34:32 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 15:30:46 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-12 22:43:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2001-07-22 14:18:56 +00:00
|
|
|
*/
|
|
|
|
#include "avformat.h"
|
2007-09-11 09:27:46 +00:00
|
|
|
#include "raw.h"
|
2006-07-12 00:09:34 +00:00
|
|
|
#include "riff.h"
|
2001-09-24 23:26:46 +00:00
|
|
|
|
2001-07-22 14:18:56 +00:00
|
|
|
typedef struct {
|
|
|
|
offset_t data;
|
2006-06-28 10:51:56 +00:00
|
|
|
offset_t data_end;
|
2007-02-12 15:57:21 +00:00
|
|
|
int64_t minpts;
|
|
|
|
int64_t maxpts;
|
|
|
|
int last_duration;
|
2001-07-22 14:18:56 +00:00
|
|
|
} WAVContext;
|
|
|
|
|
2006-07-05 22:01:49 +00:00
|
|
|
#ifdef CONFIG_MUXERS
|
2001-07-22 14:18:56 +00:00
|
|
|
static int wav_write_header(AVFormatContext *s)
|
|
|
|
{
|
2002-05-20 16:31:13 +00:00
|
|
|
WAVContext *wav = s->priv_data;
|
2007-11-21 07:41:00 +00:00
|
|
|
ByteIOContext *pb = s->pb;
|
2007-02-12 15:57:21 +00:00
|
|
|
offset_t fmt, fact;
|
2001-07-22 14:18:56 +00:00
|
|
|
|
|
|
|
put_tag(pb, "RIFF");
|
|
|
|
put_le32(pb, 0); /* file length */
|
|
|
|
put_tag(pb, "WAVE");
|
|
|
|
|
|
|
|
/* format header */
|
|
|
|
fmt = start_tag(pb, "fmt ");
|
2005-07-17 22:24:36 +00:00
|
|
|
if (put_wav_header(pb, s->streams[0]->codec) < 0) {
|
2002-05-18 23:11:09 +00:00
|
|
|
av_free(wav);
|
2001-09-24 23:26:46 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2001-07-22 14:18:56 +00:00
|
|
|
end_tag(pb, fmt);
|
|
|
|
|
2007-02-12 15:57:21 +00:00
|
|
|
if(s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
|
2007-11-21 07:41:00 +00:00
|
|
|
&& !url_is_streamed(s->pb)) {
|
2007-02-12 15:57:21 +00:00
|
|
|
fact = start_tag(pb, "fact");
|
|
|
|
put_le32(pb, 0);
|
|
|
|
end_tag(pb, fact);
|
|
|
|
}
|
|
|
|
|
2005-07-17 22:24:36 +00:00
|
|
|
av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
|
2007-02-12 15:57:21 +00:00
|
|
|
wav->maxpts = wav->last_duration = 0;
|
|
|
|
wav->minpts = INT64_MAX;
|
2005-01-12 18:42:21 +00:00
|
|
|
|
2001-07-22 14:18:56 +00:00
|
|
|
/* data header */
|
|
|
|
wav->data = start_tag(pb, "data");
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2001-07-22 14:18:56 +00:00
|
|
|
put_flush_packet(pb);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-29 02:06:32 +00:00
|
|
|
static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
|
2001-07-22 14:18:56 +00:00
|
|
|
{
|
2007-11-21 07:41:00 +00:00
|
|
|
ByteIOContext *pb = s->pb;
|
2007-02-12 15:57:21 +00:00
|
|
|
WAVContext *wav = s->priv_data;
|
2004-05-29 02:06:32 +00:00
|
|
|
put_buffer(pb, pkt->data, pkt->size);
|
2007-02-12 15:57:21 +00:00
|
|
|
if(pkt->pts != AV_NOPTS_VALUE) {
|
|
|
|
wav->minpts = FFMIN(wav->minpts, pkt->pts);
|
|
|
|
wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
|
|
|
|
wav->last_duration = pkt->duration;
|
|
|
|
} else
|
|
|
|
av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
|
2001-07-22 14:18:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wav_write_trailer(AVFormatContext *s)
|
|
|
|
{
|
2007-11-21 07:41:00 +00:00
|
|
|
ByteIOContext *pb = s->pb;
|
2001-07-22 14:18:56 +00:00
|
|
|
WAVContext *wav = s->priv_data;
|
|
|
|
offset_t file_size;
|
|
|
|
|
2007-11-21 07:41:00 +00:00
|
|
|
if (!url_is_streamed(s->pb)) {
|
2001-07-22 14:18:56 +00:00
|
|
|
end_tag(pb, wav->data);
|
|
|
|
|
|
|
|
/* update file size */
|
|
|
|
file_size = url_ftell(pb);
|
|
|
|
url_fseek(pb, 4, SEEK_SET);
|
2003-02-11 16:35:48 +00:00
|
|
|
put_le32(pb, (uint32_t)(file_size - 8));
|
2001-07-22 14:18:56 +00:00
|
|
|
url_fseek(pb, file_size, SEEK_SET);
|
|
|
|
|
|
|
|
put_flush_packet(pb);
|
2007-02-12 15:57:21 +00:00
|
|
|
|
|
|
|
if(s->streams[0]->codec->codec_tag != 0x01) {
|
|
|
|
/* Update num_samps in fact chunk */
|
|
|
|
int number_of_samples;
|
|
|
|
number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
|
|
|
|
s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
|
|
|
|
s->streams[0]->time_base.den);
|
|
|
|
url_fseek(pb, wav->data-12, SEEK_SET);
|
|
|
|
put_le32(pb, number_of_samples);
|
|
|
|
url_fseek(pb, file_size, SEEK_SET);
|
|
|
|
put_flush_packet(pb);
|
|
|
|
}
|
2001-07-22 14:18:56 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2005-09-23 00:25:41 +00:00
|
|
|
#endif //CONFIG_MUXERS
|
2001-07-22 14:18:56 +00:00
|
|
|
|
|
|
|
/* return the size of the found tag */
|
|
|
|
/* XXX: > 2GB ? */
|
2003-02-11 16:35:48 +00:00
|
|
|
static int find_tag(ByteIOContext *pb, uint32_t tag1)
|
2001-07-22 14:18:56 +00:00
|
|
|
{
|
|
|
|
unsigned int tag;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
if (url_feof(pb))
|
|
|
|
return -1;
|
|
|
|
tag = get_le32(pb);
|
|
|
|
size = get_le32(pb);
|
|
|
|
if (tag == tag1)
|
|
|
|
break;
|
|
|
|
url_fseek(pb, size, SEEK_CUR);
|
|
|
|
}
|
|
|
|
if (size < 0)
|
|
|
|
size = 0x7fffffff;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2002-05-20 16:31:13 +00:00
|
|
|
static int wav_probe(AVProbeData *p)
|
|
|
|
{
|
|
|
|
/* check file header */
|
|
|
|
if (p->buf_size <= 32)
|
|
|
|
return 0;
|
|
|
|
if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
|
|
|
|
p->buf[2] == 'F' && p->buf[3] == 'F' &&
|
|
|
|
p->buf[8] == 'W' && p->buf[9] == 'A' &&
|
|
|
|
p->buf[10] == 'V' && p->buf[11] == 'E')
|
2008-04-19 14:52:19 +00:00
|
|
|
/*
|
|
|
|
Since ACT demuxer has standard WAV header at top of it's own,
|
|
|
|
returning score is decreased to avoid probe conflict
|
|
|
|
between ACT and WAV.
|
|
|
|
*/
|
|
|
|
return AVPROBE_SCORE_MAX - 1;
|
2002-05-20 16:31:13 +00:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-07-22 14:18:56 +00:00
|
|
|
/* wav input */
|
|
|
|
static int wav_read_header(AVFormatContext *s,
|
|
|
|
AVFormatParameters *ap)
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
unsigned int tag;
|
2007-11-21 07:41:00 +00:00
|
|
|
ByteIOContext *pb = s->pb;
|
2001-07-22 14:18:56 +00:00
|
|
|
AVStream *st;
|
2006-06-28 10:51:56 +00:00
|
|
|
WAVContext *wav = s->priv_data;
|
2001-07-22 14:18:56 +00:00
|
|
|
|
|
|
|
/* check RIFF header */
|
|
|
|
tag = get_le32(pb);
|
|
|
|
|
|
|
|
if (tag != MKTAG('R', 'I', 'F', 'F'))
|
|
|
|
return -1;
|
|
|
|
get_le32(pb); /* file size */
|
|
|
|
tag = get_le32(pb);
|
|
|
|
if (tag != MKTAG('W', 'A', 'V', 'E'))
|
|
|
|
return -1;
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2001-07-22 14:18:56 +00:00
|
|
|
/* parse fmt header */
|
|
|
|
size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
|
|
|
|
if (size < 0)
|
|
|
|
return -1;
|
2002-05-20 16:31:13 +00:00
|
|
|
st = av_new_stream(s, 0);
|
2001-07-22 14:18:56 +00:00
|
|
|
if (!st)
|
2007-07-19 15:21:30 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2001-07-22 14:18:56 +00:00
|
|
|
|
2005-07-17 22:24:36 +00:00
|
|
|
get_wav_header(pb, st->codec, size);
|
2007-04-15 13:51:57 +00:00
|
|
|
st->need_parsing = AVSTREAM_PARSE_FULL;
|
2004-10-18 09:43:39 +00:00
|
|
|
|
2005-07-17 22:24:36 +00:00
|
|
|
av_set_pts_info(st, 64, 1, st->codec->sample_rate);
|
2004-10-18 09:43:39 +00:00
|
|
|
|
2002-10-28 00:55:25 +00:00
|
|
|
size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
|
|
|
|
if (size < 0)
|
|
|
|
return -1;
|
2006-06-28 10:51:56 +00:00
|
|
|
wav->data_end= url_ftell(pb) + size;
|
2001-07-22 14:18:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_SIZE 4096
|
|
|
|
|
|
|
|
static int wav_read_packet(AVFormatContext *s,
|
|
|
|
AVPacket *pkt)
|
|
|
|
{
|
2006-06-28 10:51:56 +00:00
|
|
|
int ret, size, left;
|
2003-11-10 18:44:27 +00:00
|
|
|
AVStream *st;
|
2006-06-28 10:51:56 +00:00
|
|
|
WAVContext *wav = s->priv_data;
|
2001-07-22 14:18:56 +00:00
|
|
|
|
2007-11-21 07:41:00 +00:00
|
|
|
if (url_feof(s->pb))
|
2007-07-19 15:23:32 +00:00
|
|
|
return AVERROR(EIO);
|
2003-11-10 18:44:27 +00:00
|
|
|
st = s->streams[0];
|
|
|
|
|
2007-11-21 07:41:00 +00:00
|
|
|
left= wav->data_end - url_ftell(s->pb);
|
2006-06-28 10:51:56 +00:00
|
|
|
if(left <= 0){
|
2007-11-21 07:41:00 +00:00
|
|
|
left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
|
2006-06-28 10:51:56 +00:00
|
|
|
if (left < 0) {
|
2007-07-19 15:23:32 +00:00
|
|
|
return AVERROR(EIO);
|
2006-06-28 10:51:56 +00:00
|
|
|
}
|
2007-11-21 07:41:00 +00:00
|
|
|
wav->data_end= url_ftell(s->pb) + left;
|
2006-06-28 10:51:56 +00:00
|
|
|
}
|
|
|
|
|
2003-11-10 18:44:27 +00:00
|
|
|
size = MAX_SIZE;
|
2005-07-17 22:24:36 +00:00
|
|
|
if (st->codec->block_align > 1) {
|
|
|
|
if (size < st->codec->block_align)
|
|
|
|
size = st->codec->block_align;
|
|
|
|
size = (size / st->codec->block_align) * st->codec->block_align;
|
2003-11-10 18:44:27 +00:00
|
|
|
}
|
2006-06-28 10:51:56 +00:00
|
|
|
size= FFMIN(size, left);
|
2007-11-21 07:41:00 +00:00
|
|
|
ret= av_get_packet(s->pb, pkt, size);
|
2007-02-05 23:04:48 +00:00
|
|
|
if (ret <= 0)
|
2007-07-19 15:23:32 +00:00
|
|
|
return AVERROR(EIO);
|
2001-07-22 14:18:56 +00:00
|
|
|
pkt->stream_index = 0;
|
|
|
|
|
2001-09-23 17:19:49 +00:00
|
|
|
/* note: we need to modify the packet size here to handle the last
|
|
|
|
packet */
|
|
|
|
pkt->size = ret;
|
2001-07-22 14:18:56 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-12-17 18:14:38 +00:00
|
|
|
static int wav_read_seek(AVFormatContext *s,
|
2004-10-11 23:53:53 +00:00
|
|
|
int stream_index, int64_t timestamp, int flags)
|
2003-11-10 18:44:27 +00:00
|
|
|
{
|
|
|
|
AVStream *st;
|
|
|
|
|
|
|
|
st = s->streams[0];
|
2005-07-17 22:24:36 +00:00
|
|
|
switch(st->codec->codec_id) {
|
2003-11-10 18:44:27 +00:00
|
|
|
case CODEC_ID_MP2:
|
|
|
|
case CODEC_ID_MP3:
|
|
|
|
case CODEC_ID_AC3:
|
2004-07-14 01:32:14 +00:00
|
|
|
case CODEC_ID_DTS:
|
2003-11-10 18:44:27 +00:00
|
|
|
/* use generic seeking with dynamically generated indexes */
|
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2004-10-11 23:53:53 +00:00
|
|
|
return pcm_read_seek(s, stream_index, timestamp, flags);
|
2003-11-10 18:44:27 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 21:14:37 +00:00
|
|
|
#ifdef CONFIG_WAV_DEMUXER
|
|
|
|
AVInputFormat wav_demuxer = {
|
2002-05-20 16:31:13 +00:00
|
|
|
"wav",
|
2008-06-03 16:20:54 +00:00
|
|
|
NULL_IF_CONFIG_SMALL("WAV format"),
|
2006-06-28 10:51:56 +00:00
|
|
|
sizeof(WAVContext),
|
2002-05-20 16:31:13 +00:00
|
|
|
wav_probe,
|
|
|
|
wav_read_header,
|
|
|
|
wav_read_packet,
|
2008-06-19 23:25:04 +00:00
|
|
|
NULL,
|
2003-11-10 18:44:27 +00:00
|
|
|
wav_read_seek,
|
2007-02-05 23:04:48 +00:00
|
|
|
.flags= AVFMT_GENERIC_INDEX,
|
2007-01-21 12:08:31 +00:00
|
|
|
.codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0},
|
2002-05-20 16:31:13 +00:00
|
|
|
};
|
2006-07-10 21:14:37 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_WAV_MUXER
|
|
|
|
AVOutputFormat wav_muxer = {
|
2001-07-22 14:18:56 +00:00
|
|
|
"wav",
|
2008-06-03 16:20:54 +00:00
|
|
|
NULL_IF_CONFIG_SMALL("WAV format"),
|
2001-07-22 14:18:56 +00:00
|
|
|
"audio/x-wav",
|
|
|
|
"wav",
|
2002-05-20 16:31:13 +00:00
|
|
|
sizeof(WAVContext),
|
2001-09-23 17:19:49 +00:00
|
|
|
CODEC_ID_PCM_S16LE,
|
2001-07-22 14:18:56 +00:00
|
|
|
CODEC_ID_NONE,
|
|
|
|
wav_write_header,
|
|
|
|
wav_write_packet,
|
|
|
|
wav_write_trailer,
|
2007-01-21 12:08:31 +00:00
|
|
|
.codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0},
|
2001-07-22 14:18:56 +00:00
|
|
|
};
|
2006-07-10 21:14:37 +00:00
|
|
|
#endif
|