handle malloc failures in ff_get_wav_header
[libav.git] / 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 #include "avformat.h"
26 #include "avio_internal.h"
27 #include "pcm.h"
28 #include "riff.h"
29
30 typedef struct {
31 int64_t data;
32 int64_t data_end;
33 int64_t minpts;
34 int64_t maxpts;
35 int last_duration;
36 int w64;
37 } WAVContext;
38
39 #if CONFIG_WAV_MUXER
40 static int wav_write_header(AVFormatContext *s)
41 {
42 WAVContext *wav = s->priv_data;
43 AVIOContext *pb = s->pb;
44 int64_t fmt, fact;
45
46 ffio_wfourcc(pb, "RIFF");
47 avio_wl32(pb, 0); /* file length */
48 ffio_wfourcc(pb, "WAVE");
49
50 /* format header */
51 fmt = ff_start_tag(pb, "fmt ");
52 if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
53 av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
54 s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
55 av_free(wav);
56 return -1;
57 }
58 ff_end_tag(pb, fmt);
59
60 if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
61 && s->pb->seekable) {
62 fact = ff_start_tag(pb, "fact");
63 avio_wl32(pb, 0);
64 ff_end_tag(pb, fact);
65 }
66
67 av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
68 wav->maxpts = wav->last_duration = 0;
69 wav->minpts = INT64_MAX;
70
71 /* data header */
72 wav->data = ff_start_tag(pb, "data");
73
74 avio_flush(pb);
75
76 return 0;
77 }
78
79 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
80 {
81 AVIOContext *pb = s->pb;
82 WAVContext *wav = s->priv_data;
83 avio_write(pb, pkt->data, pkt->size);
84 if(pkt->pts != AV_NOPTS_VALUE) {
85 wav->minpts = FFMIN(wav->minpts, pkt->pts);
86 wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
87 wav->last_duration = pkt->duration;
88 } else
89 av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
90 return 0;
91 }
92
93 static int wav_write_trailer(AVFormatContext *s)
94 {
95 AVIOContext *pb = s->pb;
96 WAVContext *wav = s->priv_data;
97 int64_t file_size;
98
99 avio_flush(pb);
100
101 if (s->pb->seekable) {
102 ff_end_tag(pb, wav->data);
103
104 /* update file size */
105 file_size = avio_tell(pb);
106 avio_seek(pb, 4, SEEK_SET);
107 avio_wl32(pb, (uint32_t)(file_size - 8));
108 avio_seek(pb, file_size, SEEK_SET);
109
110 avio_flush(pb);
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);
118 avio_seek(pb, wav->data-12, SEEK_SET);
119 avio_wl32(pb, number_of_samples);
120 avio_seek(pb, file_size, SEEK_SET);
121 avio_flush(pb);
122 }
123 }
124 return 0;
125 }
126
127 AVOutputFormat ff_wav_muxer = {
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 };
140 #endif /* CONFIG_WAV_MUXER */
141
142
143 #if CONFIG_WAV_DEMUXER
144
145 static int64_t next_tag(AVIOContext *pb, unsigned int *tag)
146 {
147 *tag = avio_rl32(pb);
148 return avio_rl32(pb);
149 }
150
151 /* return the size of the found tag */
152 static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
153 {
154 unsigned int tag;
155 int64_t size;
156
157 for (;;) {
158 if (pb->eof_reached)
159 return -1;
160 size = next_tag(pb, &tag);
161 if (tag == tag1)
162 break;
163 avio_skip(pb, size);
164 }
165 return size;
166 }
167
168 static int wav_probe(AVProbeData *p)
169 {
170 /* check file header */
171 if (p->buf_size <= 32)
172 return 0;
173 if (!memcmp(p->buf + 8, "WAVE", 4)) {
174 if (!memcmp(p->buf, "RIFF", 4))
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;
181 else if (!memcmp(p->buf, "RF64", 4) &&
182 !memcmp(p->buf + 12, "ds64", 4))
183 return AVPROBE_SCORE_MAX;
184 }
185 return 0;
186 }
187
188 /* wav input */
189 static int wav_read_header(AVFormatContext *s,
190 AVFormatParameters *ap)
191 {
192 int64_t size, av_uninit(data_size);
193 int64_t sample_count=0;
194 int rf64;
195 unsigned int tag;
196 AVIOContext *pb = s->pb;
197 AVStream *st;
198 WAVContext *wav = s->priv_data;
199 int ret;
200
201 /* check RIFF header */
202 tag = avio_rl32(pb);
203
204 rf64 = tag == MKTAG('R', 'F', '6', '4');
205 if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
206 return -1;
207 avio_rl32(pb); /* file size */
208 tag = avio_rl32(pb);
209 if (tag != MKTAG('W', 'A', 'V', 'E'))
210 return -1;
211
212 if (rf64) {
213 if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
214 return -1;
215 size = avio_rl32(pb);
216 if (size < 16)
217 return -1;
218 avio_rl64(pb); /* RIFF size */
219 data_size = avio_rl64(pb);
220 sample_count = avio_rl64(pb);
221 avio_skip(pb, size - 16); /* skip rest of ds64 chunk */
222 }
223
224 /* parse fmt header */
225 size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
226 if (size < 0)
227 return -1;
228 st = av_new_stream(s, 0);
229 if (!st)
230 return AVERROR(ENOMEM);
231
232 ret = ff_get_wav_header(pb, st->codec, size);
233 if (ret < 0)
234 return ret;
235 st->need_parsing = AVSTREAM_PARSE_FULL;
236
237 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
238
239 for (;;) {
240 if (pb->eof_reached)
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){
246 sample_count = avio_rl32(pb);
247 size -= 4;
248 }
249 avio_skip(pb, size);
250 }
251 if (rf64)
252 size = data_size;
253 if (size < 0)
254 return -1;
255 if (!size) {
256 wav->data_end = INT64_MAX;
257 } else
258 wav->data_end= avio_tell(pb) + size;
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;
264 return 0;
265 }
266
267 /** Find chunk with w64 GUID by skipping over other chunks
268 * @return the size of the found chunk
269 */
270 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
271 {
272 uint8_t guid[16];
273 int64_t size;
274
275 while (!pb->eof_reached) {
276 avio_read(pb, guid, 16);
277 size = avio_rl64(pb);
278 if (size <= 24)
279 return -1;
280 if (!memcmp(guid, guid1, 16))
281 return size;
282 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
283 }
284 return -1;
285 }
286
287 static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
288 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
289
290 #define MAX_SIZE 4096
291
292 static int wav_read_packet(AVFormatContext *s,
293 AVPacket *pkt)
294 {
295 int ret, size;
296 int64_t left;
297 AVStream *st;
298 WAVContext *wav = s->priv_data;
299
300 st = s->streams[0];
301
302 left = wav->data_end - avio_tell(s->pb);
303 if (left <= 0){
304 if (CONFIG_W64_DEMUXER && wav->w64)
305 left = find_guid(s->pb, guid_data) - 24;
306 else
307 left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
308 if (left < 0)
309 return AVERROR_EOF;
310 wav->data_end= avio_tell(s->pb) + left;
311 }
312
313 size = MAX_SIZE;
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;
318 }
319 size = FFMIN(size, left);
320 ret = av_get_packet(s->pb, pkt, size);
321 if (ret < 0)
322 return ret;
323 pkt->stream_index = 0;
324
325 return ret;
326 }
327
328 static int wav_read_seek(AVFormatContext *s,
329 int stream_index, int64_t timestamp, int flags)
330 {
331 AVStream *st;
332
333 st = s->streams[0];
334 switch (st->codec->codec_id) {
335 case CODEC_ID_MP2:
336 case CODEC_ID_MP3:
337 case CODEC_ID_AC3:
338 case CODEC_ID_DTS:
339 /* use generic seeking with dynamically generated indexes */
340 return -1;
341 default:
342 break;
343 }
344 return pcm_read_seek(s, stream_index, timestamp, flags);
345 }
346
347 AVInputFormat ff_wav_demuxer = {
348 "wav",
349 NULL_IF_CONFIG_SMALL("WAV format"),
350 sizeof(WAVContext),
351 wav_probe,
352 wav_read_header,
353 wav_read_packet,
354 NULL,
355 wav_read_seek,
356 .flags= AVFMT_GENERIC_INDEX,
357 .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
358 };
359 #endif /* CONFIG_WAV_DEMUXER */
360
361
362 #if CONFIG_W64_DEMUXER
363 static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
364 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
365
366 static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
367 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
368
369 static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
370 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
371
372 static 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
383 static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
384 {
385 int64_t size;
386 AVIOContext *pb = s->pb;
387 WAVContext *wav = s->priv_data;
388 AVStream *st;
389 uint8_t guid[16];
390 int ret;
391
392 avio_read(pb, guid, 16);
393 if (memcmp(guid, guid_riff, 16))
394 return -1;
395
396 if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
397 return -1;
398
399 avio_read(pb, guid, 16);
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 */
416 ret = ff_get_wav_header(pb, st->codec, size - 24);
417 if (ret < 0)
418 return ret;
419 avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
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 }
430 wav->data_end = avio_tell(pb) + size - 24;
431 wav->w64 = 1;
432
433 return 0;
434 }
435
436 AVInputFormat ff_w64_demuxer = {
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 };
448 #endif /* CONFIG_W64_DEMUXER */