2 * WAV muxer and demuxer
3 * Copyright (c) 2001, 2002 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "allformats.h"
31 static int wav_write_header(AVFormatContext *s)
33 WAVContext *wav = s->priv_data;
34 ByteIOContext *pb = &s->pb;
38 put_le32(pb, 0); /* file length */
42 fmt = start_tag(pb, "fmt ");
43 if (put_wav_header(pb, s->streams[0]->codec) < 0) {
49 av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
52 wav->data = start_tag(pb, "data");
59 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
61 ByteIOContext *pb = &s->pb;
62 put_buffer(pb, pkt->data, pkt->size);
66 static int wav_write_trailer(AVFormatContext *s)
68 ByteIOContext *pb = &s->pb;
69 WAVContext *wav = s->priv_data;
72 if (!url_is_streamed(&s->pb)) {
73 end_tag(pb, wav->data);
75 /* update file size */
76 file_size = url_ftell(pb);
77 url_fseek(pb, 4, SEEK_SET);
78 put_le32(pb, (uint32_t)(file_size - 8));
79 url_fseek(pb, file_size, SEEK_SET);
85 #endif //CONFIG_MUXERS
87 /* return the size of the found tag */
89 static int find_tag(ByteIOContext *pb, uint32_t tag1)
101 url_fseek(pb, size, SEEK_CUR);
108 static int wav_probe(AVProbeData *p)
110 /* check file header */
111 if (p->buf_size <= 32)
113 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
114 p->buf[2] == 'F' && p->buf[3] == 'F' &&
115 p->buf[8] == 'W' && p->buf[9] == 'A' &&
116 p->buf[10] == 'V' && p->buf[11] == 'E')
117 return AVPROBE_SCORE_MAX;
123 static int wav_read_header(AVFormatContext *s,
124 AVFormatParameters *ap)
128 ByteIOContext *pb = &s->pb;
130 WAVContext *wav = s->priv_data;
132 /* check RIFF header */
135 if (tag != MKTAG('R', 'I', 'F', 'F'))
137 get_le32(pb); /* file size */
139 if (tag != MKTAG('W', 'A', 'V', 'E'))
142 /* parse fmt header */
143 size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
146 st = av_new_stream(s, 0);
148 return AVERROR_NOMEM;
150 get_wav_header(pb, st->codec, size);
151 st->need_parsing = 1;
153 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
155 size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
158 wav->data_end= url_ftell(pb) + size;
162 #define MAX_SIZE 4096
164 static int wav_read_packet(AVFormatContext *s,
169 WAVContext *wav = s->priv_data;
171 if (url_feof(&s->pb))
175 left= wav->data_end - url_ftell(&s->pb);
177 left = find_tag(&(s->pb), MKTAG('d', 'a', 't', 'a'));
181 wav->data_end= url_ftell(&s->pb) + left;
185 if (st->codec->block_align > 1) {
186 if (size < st->codec->block_align)
187 size = st->codec->block_align;
188 size = (size / st->codec->block_align) * st->codec->block_align;
190 size= FFMIN(size, left);
191 if (av_new_packet(pkt, size))
193 pkt->stream_index = 0;
195 ret = get_buffer(&s->pb, pkt->data, pkt->size);
198 /* note: we need to modify the packet size here to handle the last
204 static int wav_read_close(AVFormatContext *s)
209 static int wav_read_seek(AVFormatContext *s,
210 int stream_index, int64_t timestamp, int flags)
215 switch(st->codec->codec_id) {
220 /* use generic seeking with dynamically generated indexes */
225 return pcm_read_seek(s, stream_index, timestamp, flags);
228 #ifdef CONFIG_WAV_DEMUXER
229 AVInputFormat wav_demuxer = {
240 #ifdef CONFIG_WAV_MUXER
241 AVOutputFormat wav_muxer = {