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