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