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