2
* WAV encoder and decoder
3
* Copyright (c) 2001 Gerard Lantau.
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
static int wav_write_header(AVFormatContext *s)
29
ByteIOContext *pb = &s->pb;
32
wav = malloc(sizeof(WAVContext));
35
memset(wav, 0, sizeof(WAVContext));
39
put_le32(pb, 0); /* file length */
43
fmt = start_tag(pb, "fmt ");
44
put_wav_header(pb, &s->streams[0]->codec);
48
wav->data = start_tag(pb, "data");
55
static int wav_write_packet(AVFormatContext *s, int stream_index_ptr,
58
ByteIOContext *pb = &s->pb;
59
put_buffer(pb, buf, size);
63
static int wav_write_trailer(AVFormatContext *s)
65
ByteIOContext *pb = &s->pb;
66
WAVContext *wav = s->priv_data;
69
if (!url_is_streamed(&s->pb)) {
70
end_tag(pb, wav->data);
72
/* update file size */
73
file_size = url_ftell(pb);
74
url_fseek(pb, 4, SEEK_SET);
75
put_le32(pb, (UINT32)(file_size - 8));
76
url_fseek(pb, file_size, SEEK_SET);
85
/* return the size of the found tag */
87
static int find_tag(ByteIOContext *pb, UINT32 tag1)
99
url_fseek(pb, size, SEEK_CUR);
107
static int wav_read_header(AVFormatContext *s,
108
AVFormatParameters *ap)
112
ByteIOContext *pb = &s->pb;
113
unsigned int id, channels, rate, bit_rate, extra_size;
116
/* check RIFF header */
119
if (tag != MKTAG('R', 'I', 'F', 'F'))
121
get_le32(pb); /* file size */
123
if (tag != MKTAG('W', 'A', 'V', 'E'))
126
/* parse fmt header */
127
size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
131
channels = get_le16(pb);
133
bit_rate = get_le32(pb) * 8;
134
get_le16(pb); /* block align */
135
get_le16(pb); /* bits per sample */
138
extra_size = get_le16(pb);
139
/* skip unused data */
140
url_fseek(pb, size - 18, SEEK_CUR);
143
size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
147
/* now we are ready: build format streams */
148
st = malloc(sizeof(AVStream));
156
st->codec.codec_type = CODEC_TYPE_AUDIO;
157
st->codec.codec_tag = id;
158
st->codec.codec_id = codec_get_id(codec_wav_tags, id);
159
st->codec.channels = channels;
160
st->codec.sample_rate = rate;
164
#define MAX_SIZE 4096
166
static int wav_read_packet(AVFormatContext *s,
169
int packet_size, n, ret;
171
if (url_feof(&s->pb))
173
packet_size = url_get_packet_size(&s->pb);
174
n = MAX_SIZE / packet_size;
177
if (av_new_packet(pkt, n * packet_size))
179
pkt->stream_index = 0;
181
ret = get_buffer(&s->pb, pkt->data, pkt->size);
187
static int wav_read_close(AVFormatContext *s)
192
AVFormat wav_format = {