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