]> git.sesse.net Git - ffmpeg/blob - libavformat/wav.c
391461044b3b11449cc63ee13726170f9c20161b
[ffmpeg] / libavformat / wav.c
1 /*
2  * WAV muxer and demuxer
3  * Copyright (c) 2001, 2002 Fabrice Bellard
4  *
5  * Sony Wave64 demuxer
6  * RF64 demuxer
7  * Copyright (c) 2009 Daniel Verkamp
8  *
9  * This file is part of Libav.
10  *
11  * Libav is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * Libav is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with Libav; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25
26 #include "libavutil/mathematics.h"
27 #include "avformat.h"
28 #include "avio_internal.h"
29 #include "pcm.h"
30 #include "riff.h"
31
32 typedef struct {
33     int64_t data;
34     int64_t data_end;
35     int64_t minpts;
36     int64_t maxpts;
37     int last_duration;
38     int w64;
39 } WAVContext;
40
41 #if CONFIG_WAV_MUXER
42 static int wav_write_header(AVFormatContext *s)
43 {
44     WAVContext *wav = s->priv_data;
45     AVIOContext *pb = s->pb;
46     int64_t fmt, fact;
47
48     ffio_wfourcc(pb, "RIFF");
49     avio_wl32(pb, 0); /* file length */
50     ffio_wfourcc(pb, "WAVE");
51
52     /* format header */
53     fmt = ff_start_tag(pb, "fmt ");
54     if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
55         av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
56                s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
57         return -1;
58     }
59     ff_end_tag(pb, fmt);
60
61     if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
62         && s->pb->seekable) {
63         fact = ff_start_tag(pb, "fact");
64         avio_wl32(pb, 0);
65         ff_end_tag(pb, fact);
66     }
67
68     av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
69     wav->maxpts = wav->last_duration = 0;
70     wav->minpts = INT64_MAX;
71
72     /* data header */
73     wav->data = ff_start_tag(pb, "data");
74
75     avio_flush(pb);
76
77     return 0;
78 }
79
80 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
81 {
82     AVIOContext *pb  = s->pb;
83     WAVContext    *wav = s->priv_data;
84     avio_write(pb, pkt->data, pkt->size);
85     if(pkt->pts != AV_NOPTS_VALUE) {
86         wav->minpts        = FFMIN(wav->minpts, pkt->pts);
87         wav->maxpts        = FFMAX(wav->maxpts, pkt->pts);
88         wav->last_duration = pkt->duration;
89     } else
90         av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
91     return 0;
92 }
93
94 static int wav_write_trailer(AVFormatContext *s)
95 {
96     AVIOContext *pb  = s->pb;
97     WAVContext    *wav = s->priv_data;
98     int64_t file_size;
99
100     avio_flush(pb);
101
102     if (s->pb->seekable) {
103         ff_end_tag(pb, wav->data);
104
105         /* update file size */
106         file_size = avio_tell(pb);
107         avio_seek(pb, 4, SEEK_SET);
108         avio_wl32(pb, (uint32_t)(file_size - 8));
109         avio_seek(pb, file_size, SEEK_SET);
110
111         avio_flush(pb);
112
113         if(s->streams[0]->codec->codec_tag != 0x01) {
114             /* Update num_samps in fact chunk */
115             int number_of_samples;
116             number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
117                                            s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
118                                            s->streams[0]->time_base.den);
119             avio_seek(pb, wav->data-12, SEEK_SET);
120             avio_wl32(pb, number_of_samples);
121             avio_seek(pb, file_size, SEEK_SET);
122             avio_flush(pb);
123         }
124     }
125     return 0;
126 }
127
128 AVOutputFormat ff_wav_muxer = {
129     "wav",
130     NULL_IF_CONFIG_SMALL("WAV format"),
131     "audio/x-wav",
132     "wav",
133     sizeof(WAVContext),
134     CODEC_ID_PCM_S16LE,
135     CODEC_ID_NONE,
136     wav_write_header,
137     wav_write_packet,
138     wav_write_trailer,
139     .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
140 };
141 #endif /* CONFIG_WAV_MUXER */
142
143
144 #if CONFIG_WAV_DEMUXER
145
146 static int64_t next_tag(AVIOContext *pb, unsigned int *tag)
147 {
148     *tag = avio_rl32(pb);
149     return avio_rl32(pb);
150 }
151
152 /* return the size of the found tag */
153 static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
154 {
155     unsigned int tag;
156     int64_t size;
157
158     for (;;) {
159         if (pb->eof_reached)
160             return -1;
161         size = next_tag(pb, &tag);
162         if (tag == tag1)
163             break;
164         avio_skip(pb, size);
165     }
166     return size;
167 }
168
169 static int wav_probe(AVProbeData *p)
170 {
171     /* check file header */
172     if (p->buf_size <= 32)
173         return 0;
174     if (!memcmp(p->buf + 8, "WAVE", 4)) {
175         if (!memcmp(p->buf, "RIFF", 4))
176             /*
177               Since ACT demuxer has standard WAV header at top of it's own,
178               returning score is decreased to avoid probe conflict
179               between ACT and WAV.
180             */
181             return AVPROBE_SCORE_MAX - 1;
182         else if (!memcmp(p->buf,      "RF64", 4) &&
183                  !memcmp(p->buf + 12, "ds64", 4))
184             return AVPROBE_SCORE_MAX;
185     }
186     return 0;
187 }
188
189 /* wav input */
190 static int wav_read_header(AVFormatContext *s,
191                            AVFormatParameters *ap)
192 {
193     int64_t size, av_uninit(data_size);
194     int64_t sample_count=0;
195     int rf64;
196     unsigned int tag;
197     AVIOContext *pb = s->pb;
198     AVStream *st;
199     WAVContext *wav = s->priv_data;
200     int ret;
201
202     /* check RIFF header */
203     tag = avio_rl32(pb);
204
205     rf64 = tag == MKTAG('R', 'F', '6', '4');
206     if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
207         return -1;
208     avio_rl32(pb); /* file size */
209     tag = avio_rl32(pb);
210     if (tag != MKTAG('W', 'A', 'V', 'E'))
211         return -1;
212
213     if (rf64) {
214         if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
215             return -1;
216         size = avio_rl32(pb);
217         if (size < 16)
218             return -1;
219         avio_rl64(pb); /* RIFF size */
220         data_size = avio_rl64(pb);
221         sample_count = avio_rl64(pb);
222         avio_skip(pb, size - 16); /* skip rest of ds64 chunk */
223     }
224
225     /* parse fmt header */
226     size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
227     if (size < 0)
228         return -1;
229     st = av_new_stream(s, 0);
230     if (!st)
231         return AVERROR(ENOMEM);
232
233     ret = ff_get_wav_header(pb, st->codec, size);
234     if (ret < 0)
235         return ret;
236     st->need_parsing = AVSTREAM_PARSE_FULL;
237
238     av_set_pts_info(st, 64, 1, st->codec->sample_rate);
239
240     for (;;) {
241         if (pb->eof_reached)
242             return -1;
243         size = next_tag(pb, &tag);
244         if (tag == MKTAG('d', 'a', 't', 'a')){
245             break;
246         }else if (tag == MKTAG('f','a','c','t') && !sample_count){
247             sample_count = avio_rl32(pb);
248             size -= 4;
249         }
250         avio_skip(pb, size);
251     }
252     if (rf64)
253         size = data_size;
254     if (size < 0)
255         return -1;
256     if (!size) {
257         wav->data_end = INT64_MAX;
258     } else
259         wav->data_end= avio_tell(pb) + size;
260
261     if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
262         sample_count = (size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
263     if (sample_count)
264         st->duration = sample_count;
265     return 0;
266 }
267
268 /** Find chunk with w64 GUID by skipping over other chunks
269  * @return the size of the found chunk
270  */
271 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
272 {
273     uint8_t guid[16];
274     int64_t size;
275
276     while (!pb->eof_reached) {
277         avio_read(pb, guid, 16);
278         size = avio_rl64(pb);
279         if (size <= 24)
280             return -1;
281         if (!memcmp(guid, guid1, 16))
282             return size;
283         avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
284     }
285     return -1;
286 }
287
288 static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
289     0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
290
291 #define MAX_SIZE 4096
292
293 static int wav_read_packet(AVFormatContext *s,
294                            AVPacket *pkt)
295 {
296     int ret, size;
297     int64_t left;
298     AVStream *st;
299     WAVContext *wav = s->priv_data;
300
301     st = s->streams[0];
302
303     left = wav->data_end - avio_tell(s->pb);
304     if (left <= 0){
305         if (CONFIG_W64_DEMUXER && wav->w64)
306             left = find_guid(s->pb, guid_data) - 24;
307         else
308             left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
309         if (left < 0)
310             return AVERROR_EOF;
311         wav->data_end= avio_tell(s->pb) + left;
312     }
313
314     size = MAX_SIZE;
315     if (st->codec->block_align > 1) {
316         if (size < st->codec->block_align)
317             size = st->codec->block_align;
318         size = (size / st->codec->block_align) * st->codec->block_align;
319     }
320     size = FFMIN(size, left);
321     ret  = av_get_packet(s->pb, pkt, size);
322     if (ret < 0)
323         return ret;
324     pkt->stream_index = 0;
325
326     return ret;
327 }
328
329 static int wav_read_seek(AVFormatContext *s,
330                          int stream_index, int64_t timestamp, int flags)
331 {
332     AVStream *st;
333
334     st = s->streams[0];
335     switch (st->codec->codec_id) {
336     case CODEC_ID_MP2:
337     case CODEC_ID_MP3:
338     case CODEC_ID_AC3:
339     case CODEC_ID_DTS:
340         /* use generic seeking with dynamically generated indexes */
341         return -1;
342     default:
343         break;
344     }
345     return pcm_read_seek(s, stream_index, timestamp, flags);
346 }
347
348 AVInputFormat ff_wav_demuxer = {
349     "wav",
350     NULL_IF_CONFIG_SMALL("WAV format"),
351     sizeof(WAVContext),
352     wav_probe,
353     wav_read_header,
354     wav_read_packet,
355     NULL,
356     wav_read_seek,
357     .flags= AVFMT_GENERIC_INDEX,
358     .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
359 };
360 #endif /* CONFIG_WAV_DEMUXER */
361
362
363 #if CONFIG_W64_DEMUXER
364 static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
365     0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
366
367 static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
368     0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
369
370 static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
371     0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
372
373 static int w64_probe(AVProbeData *p)
374 {
375     if (p->buf_size <= 40)
376         return 0;
377     if (!memcmp(p->buf,      guid_riff, 16) &&
378         !memcmp(p->buf + 24, guid_wave, 16))
379         return AVPROBE_SCORE_MAX;
380     else
381         return 0;
382 }
383
384 static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
385 {
386     int64_t size;
387     AVIOContext *pb  = s->pb;
388     WAVContext    *wav = s->priv_data;
389     AVStream *st;
390     uint8_t guid[16];
391     int ret;
392
393     avio_read(pb, guid, 16);
394     if (memcmp(guid, guid_riff, 16))
395         return -1;
396
397     if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
398         return -1;
399
400     avio_read(pb, guid, 16);
401     if (memcmp(guid, guid_wave, 16)) {
402         av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
403         return -1;
404     }
405
406     size = find_guid(pb, guid_fmt);
407     if (size < 0) {
408         av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
409         return -1;
410     }
411
412     st = av_new_stream(s, 0);
413     if (!st)
414         return AVERROR(ENOMEM);
415
416     /* subtract chunk header size - normal wav file doesn't count it */
417     ret = ff_get_wav_header(pb, st->codec, size - 24);
418     if (ret < 0)
419         return ret;
420     avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
421
422     st->need_parsing = AVSTREAM_PARSE_FULL;
423
424     av_set_pts_info(st, 64, 1, st->codec->sample_rate);
425
426     size = find_guid(pb, guid_data);
427     if (size < 0) {
428         av_log(s, AV_LOG_ERROR, "could not find data guid\n");
429         return -1;
430     }
431     wav->data_end = avio_tell(pb) + size - 24;
432     wav->w64      = 1;
433
434     return 0;
435 }
436
437 AVInputFormat ff_w64_demuxer = {
438     "w64",
439     NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
440     sizeof(WAVContext),
441     w64_probe,
442     w64_read_header,
443     wav_read_packet,
444     NULL,
445     wav_read_seek,
446     .flags = AVFMT_GENERIC_INDEX,
447     .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
448 };
449 #endif /* CONFIG_W64_DEMUXER */