2005-12-17 20:14:38 +02:00
|
|
|
/*
|
2001-07-22 17:18:56 +03:00
|
|
|
* WAV encoder and decoder
|
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"
|
2006-06-30 10:41:25 +03:00
|
|
|
#include "allformats.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;
|
2001-07-22 17:18:56 +03:00
|
|
|
} WAVContext;
|
|
|
|
|
2006-07-06 01:01:49 +03:00
|
|
|
#ifdef CONFIG_MUXERS
|
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;
|
2001-07-22 17:18:56 +03:00
|
|
|
ByteIOContext *pb = &s->pb;
|
|
|
|
offset_t fmt;
|
|
|
|
|
|
|
|
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) {
|
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);
|
|
|
|
|
2005-07-18 01:24:36 +03:00
|
|
|
av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
|
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
|
|
|
{
|
|
|
|
ByteIOContext *pb = &s->pb;
|
2004-05-29 05:06:32 +03:00
|
|
|
put_buffer(pb, pkt->data, pkt->size);
|
2001-07-22 17:18:56 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wav_write_trailer(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
ByteIOContext *pb = &s->pb;
|
|
|
|
WAVContext *wav = s->priv_data;
|
|
|
|
offset_t file_size;
|
|
|
|
|
|
|
|
if (!url_is_streamed(&s->pb)) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2005-09-23 03:25:41 +03:00
|
|
|
#endif //CONFIG_MUXERS
|
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')
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
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;
|
|
|
|
ByteIOContext *pb = &s->pb;
|
|
|
|
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)
|
2002-05-20 19:31:13 +03:00
|
|
|
return AVERROR_NOMEM;
|
2001-07-22 17:18:56 +03:00
|
|
|
|
2005-07-18 01:24:36 +03:00
|
|
|
get_wav_header(pb, st->codec, size);
|
2003-11-10 20:44:27 +02:00
|
|
|
st->need_parsing = 1;
|
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
|
|
|
|
|
|
|
if (url_feof(&s->pb))
|
2004-06-19 06:59:34 +03:00
|
|
|
return AVERROR_IO;
|
2003-11-10 20:44:27 +02:00
|
|
|
st = s->streams[0];
|
|
|
|
|
2006-06-28 13:51:56 +03:00
|
|
|
left= wav->data_end - url_ftell(&s->pb);
|
|
|
|
if(left <= 0){
|
|
|
|
left = find_tag(&(s->pb), MKTAG('d', 'a', 't', 'a'));
|
|
|
|
if (left < 0) {
|
|
|
|
return AVERROR_IO;
|
|
|
|
}
|
|
|
|
wav->data_end= url_ftell(&s->pb) + left;
|
|
|
|
}
|
|
|
|
|
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);
|
2003-11-10 20:44:27 +02:00
|
|
|
if (av_new_packet(pkt, size))
|
2004-06-19 06:59:34 +03:00
|
|
|
return AVERROR_IO;
|
2001-07-22 17:18:56 +03:00
|
|
|
pkt->stream_index = 0;
|
|
|
|
|
|
|
|
ret = get_buffer(&s->pb, pkt->data, pkt->size);
|
|
|
|
if (ret < 0)
|
|
|
|
av_free_packet(pkt);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wav_read_close(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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",
|
|
|
|
"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,
|
|
|
|
wav_read_close,
|
2003-11-10 20:44:27 +02:00
|
|
|
wav_read_seek,
|
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",
|
|
|
|
"wav format",
|
|
|
|
"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,
|
|
|
|
};
|
2006-07-11 00:14:37 +03:00
|
|
|
#endif
|