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