051cd843a81a985be10c719c04d688b30e042841
[libav.git] / libavformat / wav.c
1 /*
2 * WAV muxer and demuxer
3 * Copyright (c) 2001, 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21 #include "avformat.h"
22 #include "raw.h"
23 #include "riff.h"
24
25 typedef struct {
26 int64_t data;
27 int64_t data_end;
28 int64_t minpts;
29 int64_t maxpts;
30 int last_duration;
31 } WAVContext;
32
33 #if CONFIG_WAV_MUXER
34 static int wav_write_header(AVFormatContext *s)
35 {
36 WAVContext *wav = s->priv_data;
37 ByteIOContext *pb = s->pb;
38 int64_t fmt, fact;
39
40 put_tag(pb, "RIFF");
41 put_le32(pb, 0); /* file length */
42 put_tag(pb, "WAVE");
43
44 /* format header */
45 fmt = ff_start_tag(pb, "fmt ");
46 if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
47 av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
48 s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
49 av_free(wav);
50 return -1;
51 }
52 ff_end_tag(pb, fmt);
53
54 if(s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
55 && !url_is_streamed(s->pb)) {
56 fact = ff_start_tag(pb, "fact");
57 put_le32(pb, 0);
58 ff_end_tag(pb, fact);
59 }
60
61 av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
62 wav->maxpts = wav->last_duration = 0;
63 wav->minpts = INT64_MAX;
64
65 /* data header */
66 wav->data = ff_start_tag(pb, "data");
67
68 put_flush_packet(pb);
69
70 return 0;
71 }
72
73 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
74 {
75 ByteIOContext *pb = s->pb;
76 WAVContext *wav = s->priv_data;
77 put_buffer(pb, pkt->data, pkt->size);
78 if(pkt->pts != AV_NOPTS_VALUE) {
79 wav->minpts = FFMIN(wav->minpts, pkt->pts);
80 wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
81 wav->last_duration = pkt->duration;
82 } else
83 av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
84 return 0;
85 }
86
87 static int wav_write_trailer(AVFormatContext *s)
88 {
89 ByteIOContext *pb = s->pb;
90 WAVContext *wav = s->priv_data;
91 int64_t file_size;
92
93 if (!url_is_streamed(s->pb)) {
94 ff_end_tag(pb, wav->data);
95
96 /* update file size */
97 file_size = url_ftell(pb);
98 url_fseek(pb, 4, SEEK_SET);
99 put_le32(pb, (uint32_t)(file_size - 8));
100 url_fseek(pb, file_size, SEEK_SET);
101
102 put_flush_packet(pb);
103
104 if(s->streams[0]->codec->codec_tag != 0x01) {
105 /* Update num_samps in fact chunk */
106 int number_of_samples;
107 number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
108 s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
109 s->streams[0]->time_base.den);
110 url_fseek(pb, wav->data-12, SEEK_SET);
111 put_le32(pb, number_of_samples);
112 url_fseek(pb, file_size, SEEK_SET);
113 put_flush_packet(pb);
114 }
115 }
116 return 0;
117 }
118 #endif /* CONFIG_WAV_MUXER */
119
120 /* return the size of the found tag */
121 static int64_t find_tag(ByteIOContext *pb, uint32_t tag1)
122 {
123 unsigned int tag;
124 int64_t size;
125
126 for(;;) {
127 if (url_feof(pb))
128 return -1;
129 tag = get_le32(pb);
130 size = get_le32(pb);
131 if (tag == tag1)
132 break;
133 url_fseek(pb, size, SEEK_CUR);
134 }
135 return size;
136 }
137
138 static int wav_probe(AVProbeData *p)
139 {
140 /* check file header */
141 if (p->buf_size <= 32)
142 return 0;
143 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
144 p->buf[2] == 'F' && p->buf[3] == 'F' &&
145 p->buf[8] == 'W' && p->buf[9] == 'A' &&
146 p->buf[10] == 'V' && p->buf[11] == 'E')
147 /*
148 Since ACT demuxer has standard WAV header at top of it's own,
149 returning score is decreased to avoid probe conflict
150 between ACT and WAV.
151 */
152 return AVPROBE_SCORE_MAX - 1;
153 else
154 return 0;
155 }
156
157 /* wav input */
158 static int wav_read_header(AVFormatContext *s,
159 AVFormatParameters *ap)
160 {
161 int64_t size;
162 unsigned int tag;
163 ByteIOContext *pb = s->pb;
164 AVStream *st;
165 WAVContext *wav = s->priv_data;
166
167 /* check RIFF header */
168 tag = get_le32(pb);
169
170 if (tag != MKTAG('R', 'I', 'F', 'F'))
171 return -1;
172 get_le32(pb); /* file size */
173 tag = get_le32(pb);
174 if (tag != MKTAG('W', 'A', 'V', 'E'))
175 return -1;
176
177 /* parse fmt header */
178 size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
179 if (size < 0)
180 return -1;
181 st = av_new_stream(s, 0);
182 if (!st)
183 return AVERROR(ENOMEM);
184
185 ff_get_wav_header(pb, st->codec, size);
186 st->need_parsing = AVSTREAM_PARSE_FULL;
187
188 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
189
190 size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
191 if (size < 0)
192 return -1;
193 wav->data_end= url_ftell(pb) + size;
194 return 0;
195 }
196
197 #define MAX_SIZE 4096
198
199 static int wav_read_packet(AVFormatContext *s,
200 AVPacket *pkt)
201 {
202 int ret, size, left;
203 AVStream *st;
204 WAVContext *wav = s->priv_data;
205
206 if (url_feof(s->pb))
207 return AVERROR(EIO);
208 st = s->streams[0];
209
210 left= wav->data_end - url_ftell(s->pb);
211 if(left <= 0){
212 left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
213 if (left < 0) {
214 return AVERROR(EIO);
215 }
216 wav->data_end= url_ftell(s->pb) + left;
217 }
218
219 size = MAX_SIZE;
220 if (st->codec->block_align > 1) {
221 if (size < st->codec->block_align)
222 size = st->codec->block_align;
223 size = (size / st->codec->block_align) * st->codec->block_align;
224 }
225 size= FFMIN(size, left);
226 ret= av_get_packet(s->pb, pkt, size);
227 if (ret <= 0)
228 return AVERROR(EIO);
229 pkt->stream_index = 0;
230
231 /* note: we need to modify the packet size here to handle the last
232 packet */
233 pkt->size = ret;
234 return ret;
235 }
236
237 static int wav_read_seek(AVFormatContext *s,
238 int stream_index, int64_t timestamp, int flags)
239 {
240 AVStream *st;
241
242 st = s->streams[0];
243 switch(st->codec->codec_id) {
244 case CODEC_ID_MP2:
245 case CODEC_ID_MP3:
246 case CODEC_ID_AC3:
247 case CODEC_ID_DTS:
248 /* use generic seeking with dynamically generated indexes */
249 return -1;
250 default:
251 break;
252 }
253 return pcm_read_seek(s, stream_index, timestamp, flags);
254 }
255
256 #if CONFIG_WAV_DEMUXER
257 AVInputFormat wav_demuxer = {
258 "wav",
259 NULL_IF_CONFIG_SMALL("WAV format"),
260 sizeof(WAVContext),
261 wav_probe,
262 wav_read_header,
263 wav_read_packet,
264 NULL,
265 wav_read_seek,
266 .flags= AVFMT_GENERIC_INDEX,
267 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
268 };
269 #endif
270 #if CONFIG_WAV_MUXER
271 AVOutputFormat wav_muxer = {
272 "wav",
273 NULL_IF_CONFIG_SMALL("WAV format"),
274 "audio/x-wav",
275 "wav",
276 sizeof(WAVContext),
277 CODEC_ID_PCM_S16LE,
278 CODEC_ID_NONE,
279 wav_write_header,
280 wav_write_packet,
281 wav_write_trailer,
282 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
283 };
284 #endif