avformat/mxfdec: Check edit unit for overflow in mxf_set_current_edit_unit()
[FFMpeg-mirror.git] / libavformat / wavdec.c
blob78e37b88d75205e006e5dcf4730120ebea3673a8
1 /*
2 * WAV demuxer
3 * Copyright (c) 2001, 2002 Fabrice Bellard
5 * Sony Wave64 demuxer
6 * RF64 demuxer
7 * Copyright (c) 2009 Daniel Verkamp
9 * BW64 demuxer
11 * This file is part of FFmpeg.
13 * FFmpeg is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * FFmpeg is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with FFmpeg; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <stdint.h>
30 #include "config_components.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/dict.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/log.h"
35 #include "libavutil/mathematics.h"
36 #include "libavutil/mem.h"
37 #include "libavutil/opt.h"
38 #include "libavcodec/internal.h"
39 #include "avformat.h"
40 #include "avio.h"
41 #include "avio_internal.h"
42 #include "demux.h"
43 #include "id3v2.h"
44 #include "internal.h"
45 #include "metadata.h"
46 #include "pcm.h"
47 #include "riff.h"
48 #include "w64.h"
49 #include "spdif.h"
51 typedef struct WAVDemuxContext {
52 const AVClass *class;
53 int64_t data_end;
54 int w64;
55 AVStream *vst;
56 int64_t smv_data_ofs;
57 int smv_block_size;
58 int smv_frames_per_jpeg;
59 int smv_block;
60 int smv_last_stream;
61 int smv_eof;
62 int audio_eof;
63 int ignore_length;
64 int max_size;
65 int spdif;
66 int smv_given_first;
67 int unaligned; // e.g. if an odd number of bytes ID3 tag was prepended
68 int rifx; // RIFX: integer byte order for parameters is big endian
69 } WAVDemuxContext;
71 #define OFFSET(x) offsetof(WAVDemuxContext, x)
72 #define DEC AV_OPT_FLAG_DECODING_PARAM
73 static const AVOption demux_options[] = {
74 #define W64_DEMUXER_OPTIONS_OFFSET (1 * CONFIG_WAV_DEMUXER)
75 #if CONFIG_WAV_DEMUXER
76 { "ignore_length", "Ignore length", OFFSET(ignore_length), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, DEC },
77 #endif
78 { "max_size", "max size of single packet", OFFSET(max_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1 << 22, DEC },
79 { NULL },
82 static void set_max_size(AVStream *st, WAVDemuxContext *wav)
84 if (wav->max_size <= 0) {
85 int max_size = ff_pcm_default_packet_size(st->codecpar);
86 wav->max_size = max_size < 0 ? 4096 : max_size;
90 static void set_spdif(AVFormatContext *s, WAVDemuxContext *wav)
92 if (CONFIG_SPDIF_DEMUXER && s->streams[0]->codecpar->codec_tag == 1) {
93 enum AVCodecID codec;
94 int len = 1<<16;
95 int ret = ffio_ensure_seekback(s->pb, len);
97 if (ret >= 0) {
98 uint8_t *buf = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE);
99 if (!buf) {
100 ret = AVERROR(ENOMEM);
101 } else {
102 int64_t pos = avio_tell(s->pb);
103 len = ret = avio_read(s->pb, buf, len);
104 if (len >= 0) {
105 ret = ff_spdif_probe(buf, len, &codec);
106 if (ret > AVPROBE_SCORE_EXTENSION) {
107 s->streams[0]->codecpar->codec_id = codec;
108 wav->spdif = 1;
111 avio_seek(s->pb, pos, SEEK_SET);
112 av_free(buf);
116 if (ret < 0)
117 av_log(s, AV_LOG_WARNING, "Cannot check for SPDIF\n");
121 #if CONFIG_WAV_DEMUXER
123 static int64_t next_tag(AVIOContext *pb, uint32_t *tag, int big_endian)
125 *tag = avio_rl32(pb);
126 if (!big_endian) {
127 return avio_rl32(pb);
128 } else {
129 return avio_rb32(pb);
133 /* RIFF chunks are always at even offsets relative to where they start. */
134 static int64_t wav_seek_tag(WAVDemuxContext * wav, AVIOContext *s, int64_t offset, int whence)
136 offset += offset < INT64_MAX && offset + wav->unaligned & 1;
138 return avio_seek(s, offset, whence);
141 /* return the size of the found tag */
142 static int64_t find_tag(WAVDemuxContext * wav, AVIOContext *pb, uint32_t tag1)
144 unsigned int tag;
145 int64_t size;
147 for (;;) {
148 if (avio_feof(pb))
149 return AVERROR_EOF;
150 size = next_tag(pb, &tag, wav->rifx);
151 if (tag == tag1)
152 break;
153 wav_seek_tag(wav, pb, size, SEEK_CUR);
155 return size;
158 static int wav_probe(const AVProbeData *p)
160 /* check file header */
161 if (p->buf_size <= 32)
162 return 0;
163 if (!memcmp(p->buf + 8, "WAVE", 4)) {
164 if (!memcmp(p->buf, "RIFF", 4) || !memcmp(p->buf, "RIFX", 4))
165 /* Since the ACT demuxer has a standard WAV header at the top of
166 * its own, the returned score is decreased to avoid a probe
167 * conflict between ACT and WAV. */
168 return AVPROBE_SCORE_MAX - 1;
169 else if ((!memcmp(p->buf, "RF64", 4) ||
170 !memcmp(p->buf, "BW64", 4)) &&
171 !memcmp(p->buf + 12, "ds64", 4))
172 return AVPROBE_SCORE_MAX;
174 return 0;
177 static void handle_stream_probing(AVStream *st)
179 if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE) {
180 FFStream *const sti = ffstream(st);
181 sti->request_probe = AVPROBE_SCORE_EXTENSION;
182 sti->probe_packets = FFMIN(sti->probe_packets, 32);
186 static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream *st)
188 AVIOContext *pb = s->pb;
189 WAVDemuxContext *wav = s->priv_data;
190 int ret;
192 /* parse fmt header */
193 ret = ff_get_wav_header(s, pb, st->codecpar, size, wav->rifx);
194 if (ret < 0)
195 return ret;
196 handle_stream_probing(st);
198 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
200 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
202 return 0;
205 static int wav_parse_xma2_tag(AVFormatContext *s, int64_t size, AVStream *st)
207 AVIOContext *pb = s->pb;
208 int version, num_streams, i, channels = 0, ret;
210 if (size < 36)
211 return AVERROR_INVALIDDATA;
213 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
214 st->codecpar->codec_id = AV_CODEC_ID_XMA2;
215 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
217 version = avio_r8(pb);
218 if (version != 3 && version != 4)
219 return AVERROR_INVALIDDATA;
220 num_streams = avio_r8(pb);
221 if (size != (32 + ((version==3)?0:8) + 4*num_streams))
222 return AVERROR_INVALIDDATA;
223 avio_skip(pb, 10);
224 st->codecpar->sample_rate = avio_rb32(pb);
225 if (version == 4)
226 avio_skip(pb, 8);
227 avio_skip(pb, 4);
228 st->duration = avio_rb32(pb);
229 avio_skip(pb, 8);
231 for (i = 0; i < num_streams; i++) {
232 channels += avio_r8(pb);
233 avio_skip(pb, 3);
235 av_channel_layout_uninit(&st->codecpar->ch_layout);
236 st->codecpar->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
237 st->codecpar->ch_layout.nb_channels = channels;
239 if (st->codecpar->ch_layout.nb_channels <= 0 || st->codecpar->sample_rate <= 0)
240 return AVERROR_INVALIDDATA;
242 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
244 avio_seek(pb, -size, SEEK_CUR);
245 if ((ret = ff_get_extradata(s, st->codecpar, pb, size)) < 0)
246 return ret;
248 return 0;
251 static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
252 int length)
254 char temp[257];
255 int ret;
257 av_assert0(length < sizeof(temp));
258 if ((ret = ffio_read_size(s->pb, temp, length)) < 0)
259 return ret;
261 temp[length] = 0;
263 if (strlen(temp))
264 return av_dict_set(&s->metadata, key, temp, 0);
266 return 0;
269 static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
271 char temp[131], *coding_history;
272 int ret, x;
273 uint64_t time_reference;
274 int64_t umid_parts[8], umid_mask = 0;
276 if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
277 (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
278 (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
279 (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
280 (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
281 return ret;
283 time_reference = avio_rl64(s->pb);
284 snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
285 if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
286 return ret;
288 /* check if version is >= 1, in which case an UMID may be present */
289 if (avio_rl16(s->pb) >= 1) {
290 for (x = 0; x < 8; x++)
291 umid_mask |= umid_parts[x] = avio_rb64(s->pb);
293 if (umid_mask) {
294 /* the string formatting below is per SMPTE 330M-2004 Annex C */
295 if (umid_parts[4] == 0 && umid_parts[5] == 0 &&
296 umid_parts[6] == 0 && umid_parts[7] == 0) {
297 /* basic UMID */
298 snprintf(temp, sizeof(temp),
299 "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
300 umid_parts[0], umid_parts[1],
301 umid_parts[2], umid_parts[3]);
302 } else {
303 /* extended UMID */
304 snprintf(temp, sizeof(temp),
305 "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
306 "%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
307 umid_parts[0], umid_parts[1],
308 umid_parts[2], umid_parts[3],
309 umid_parts[4], umid_parts[5],
310 umid_parts[6], umid_parts[7]);
313 if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
314 return ret;
317 avio_skip(s->pb, 190);
318 } else
319 avio_skip(s->pb, 254);
321 if (size > 602) {
322 /* CodingHistory present */
323 size -= 602;
325 if (!(coding_history = av_malloc(size + 1)))
326 return AVERROR(ENOMEM);
328 if ((ret = ffio_read_size(s->pb, coding_history, size)) < 0) {
329 av_free(coding_history);
330 return ret;
333 coding_history[size] = 0;
334 if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
335 AV_DICT_DONT_STRDUP_VAL)) < 0)
336 return ret;
339 return 0;
342 static const AVMetadataConv wav_metadata_conv[] = {
343 { "description", "comment" },
344 { "originator", "encoded_by" },
345 { "origination_date", "date" },
346 { "origination_time", "creation_time" },
347 { 0 },
350 /* wav input */
351 static int wav_read_header(AVFormatContext *s)
353 int64_t size, av_uninit(data_size);
354 int64_t sample_count = 0;
355 int rf64 = 0, bw64 = 0;
356 uint32_t tag;
357 AVIOContext *pb = s->pb;
358 AVStream *st = NULL;
359 WAVDemuxContext *wav = s->priv_data;
360 int ret, got_fmt = 0, got_xma2 = 0;
361 int64_t next_tag_ofs, data_ofs = -1;
363 wav->unaligned = avio_tell(s->pb) & 1;
365 wav->smv_data_ofs = -1;
367 /* read chunk ID */
368 tag = avio_rl32(pb);
369 switch (tag) {
370 case MKTAG('R', 'I', 'F', 'F'):
371 break;
372 case MKTAG('R', 'I', 'F', 'X'):
373 wav->rifx = 1;
374 break;
375 case MKTAG('R', 'F', '6', '4'):
376 rf64 = 1;
377 break;
378 case MKTAG('B', 'W', '6', '4'):
379 bw64 = 1;
380 break;
381 default:
382 av_log(s, AV_LOG_ERROR, "invalid start code %s in RIFF header\n",
383 av_fourcc2str(tag));
384 return AVERROR_INVALIDDATA;
387 /* read chunk size */
388 avio_rl32(pb);
390 /* read format */
391 if (avio_rl32(pb) != MKTAG('W', 'A', 'V', 'E')) {
392 av_log(s, AV_LOG_ERROR, "invalid format in RIFF header\n");
393 return AVERROR_INVALIDDATA;
396 if (rf64 || bw64) {
397 if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
398 return AVERROR_INVALIDDATA;
399 size = avio_rl32(pb);
400 if (size < 24)
401 return AVERROR_INVALIDDATA;
402 avio_rl64(pb); /* RIFF size */
404 data_size = avio_rl64(pb);
405 sample_count = avio_rl64(pb);
407 if (data_size < 0 || sample_count < 0) {
408 av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
409 "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
410 data_size, sample_count);
411 return AVERROR_INVALIDDATA;
413 avio_skip(pb, size - 24); /* skip rest of ds64 chunk */
417 /* Create the audio stream now so that its index is always zero */
418 st = avformat_new_stream(s, NULL);
419 if (!st)
420 return AVERROR(ENOMEM);
422 for (;;) {
423 AVStream *vst;
424 size = next_tag(pb, &tag, wav->rifx);
425 next_tag_ofs = avio_tell(pb) + size;
427 if (avio_feof(pb))
428 break;
430 switch (tag) {
431 case MKTAG('f', 'm', 't', ' '):
432 /* only parse the first 'fmt ' tag found */
433 if (!got_xma2 && !got_fmt && (ret = wav_parse_fmt_tag(s, size, st)) < 0) {
434 return ret;
435 } else if (got_fmt)
436 av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
438 got_fmt = 1;
439 break;
440 case MKTAG('X', 'M', 'A', '2'):
441 /* only parse the first 'XMA2' tag found */
442 if (!got_fmt && !got_xma2 && (ret = wav_parse_xma2_tag(s, size, st)) < 0) {
443 return ret;
444 } else if (got_xma2)
445 av_log(s, AV_LOG_WARNING, "found more than one 'XMA2' tag\n");
447 got_xma2 = 1;
448 break;
449 case MKTAG('d', 'a', 't', 'a'):
450 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) && !got_fmt && !got_xma2) {
451 av_log(s, AV_LOG_ERROR,
452 "found no 'fmt ' tag before the 'data' tag\n");
453 return AVERROR_INVALIDDATA;
456 if (rf64 || bw64) {
457 next_tag_ofs = wav->data_end = av_sat_add64(avio_tell(pb), data_size);
458 } else if (size != 0xFFFFFFFF) {
459 data_size = size;
460 next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
461 } else {
462 av_log(s, AV_LOG_WARNING, "Ignoring maximum wav data size, "
463 "file may be invalid\n");
464 data_size = 0;
465 next_tag_ofs = wav->data_end = INT64_MAX;
468 data_ofs = avio_tell(pb);
470 /* don't look for footer metadata if we can't seek or if we don't
471 * know where the data tag ends
473 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) || (!(rf64 && !bw64) && !size))
474 goto break_loop;
475 break;
476 case MKTAG('f', 'a', 'c', 't'):
477 if (!sample_count)
478 sample_count = (!wav->rifx ? avio_rl32(pb) : avio_rb32(pb));
479 break;
480 case MKTAG('b', 'e', 'x', 't'):
481 if ((ret = wav_parse_bext_tag(s, size)) < 0)
482 return ret;
483 break;
484 case MKTAG('S','M','V','0'):
485 if (!got_fmt) {
486 av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'SMV0' tag\n");
487 return AVERROR_INVALIDDATA;
489 // SMV file, a wav file with video appended.
490 if (size != MKTAG('0','2','0','0')) {
491 av_log(s, AV_LOG_ERROR, "Unknown SMV version found\n");
492 goto break_loop;
494 av_log(s, AV_LOG_DEBUG, "Found SMV data\n");
495 wav->smv_given_first = 0;
496 vst = avformat_new_stream(s, NULL);
497 if (!vst)
498 return AVERROR(ENOMEM);
499 wav->vst = vst;
500 avio_r8(pb);
501 vst->id = 1;
502 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
503 vst->codecpar->codec_id = AV_CODEC_ID_SMVJPEG;
504 vst->codecpar->width = avio_rl24(pb);
505 vst->codecpar->height = avio_rl24(pb);
506 if ((ret = ff_alloc_extradata(vst->codecpar, 4)) < 0) {
507 av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n");
508 return ret;
510 size = avio_rl24(pb);
511 wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
512 avio_rl24(pb);
513 wav->smv_block_size = avio_rl24(pb);
514 if (!wav->smv_block_size)
515 return AVERROR_INVALIDDATA;
516 avpriv_set_pts_info(vst, 32, 1, avio_rl24(pb));
517 vst->duration = avio_rl24(pb);
518 avio_rl24(pb);
519 avio_rl24(pb);
520 wav->smv_frames_per_jpeg = avio_rl24(pb);
521 if (wav->smv_frames_per_jpeg > 65536) {
522 av_log(s, AV_LOG_ERROR, "too many frames per jpeg\n");
523 return AVERROR_INVALIDDATA;
525 AV_WL32(vst->codecpar->extradata, wav->smv_frames_per_jpeg);
526 goto break_loop;
527 case MKTAG('L', 'I', 'S', 'T'):
528 case MKTAG('l', 'i', 's', 't'):
529 if (size < 4) {
530 av_log(s, AV_LOG_ERROR, "too short LIST tag\n");
531 return AVERROR_INVALIDDATA;
533 switch (avio_rl32(pb)) {
534 case MKTAG('I', 'N', 'F', 'O'):
535 ff_read_riff_info(s, size - 4);
536 break;
537 case MKTAG('a', 'd', 't', 'l'):
538 if (s->nb_chapters > 0) {
539 while (avio_tell(pb) < next_tag_ofs &&
540 !avio_feof(pb)) {
541 char cue_label[512];
542 unsigned id, sub_size;
544 if (avio_rl32(pb) != MKTAG('l', 'a', 'b', 'l'))
545 break;
547 sub_size = avio_rl32(pb);
548 if (sub_size < 5)
549 break;
550 id = avio_rl32(pb);
551 avio_get_str(pb, sub_size - 4, cue_label, sizeof(cue_label));
552 avio_skip(pb, avio_tell(pb) & 1);
554 for (int i = 0; i < s->nb_chapters; i++) {
555 if (s->chapters[i]->id == id) {
556 av_dict_set(&s->chapters[i]->metadata, "title", cue_label, 0);
557 break;
562 break;
564 break;
565 case MKTAG('I', 'D', '3', ' '):
566 case MKTAG('i', 'd', '3', ' '): {
567 ID3v2ExtraMeta *id3v2_extra_meta;
568 ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta, 0);
569 if (id3v2_extra_meta) {
570 ff_id3v2_parse_apic(s, id3v2_extra_meta);
571 ff_id3v2_parse_chapters(s, id3v2_extra_meta);
572 ff_id3v2_parse_priv(s, id3v2_extra_meta);
574 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
576 break;
577 case MKTAG('c', 'u', 'e', ' '):
578 if (size >= 4 && got_fmt && st->codecpar->sample_rate > 0) {
579 AVRational tb = {1, st->codecpar->sample_rate};
580 unsigned nb_cues = avio_rl32(pb);
582 if (size >= nb_cues * 24LL + 4LL) {
583 for (int i = 0; i < nb_cues; i++) {
584 unsigned offset, id = avio_rl32(pb);
586 if (avio_feof(pb))
587 return AVERROR_INVALIDDATA;
589 avio_skip(pb, 16);
590 offset = avio_rl32(pb);
592 if (!avpriv_new_chapter(s, id, tb, offset, AV_NOPTS_VALUE, NULL))
593 return AVERROR(ENOMEM);
597 break;
600 /* seek to next tag unless we know that we'll run into EOF */
601 if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
602 wav_seek_tag(wav, pb, next_tag_ofs, SEEK_SET) < 0) {
603 break;
607 break_loop:
608 if (!got_fmt && !got_xma2) {
609 av_log(s, AV_LOG_ERROR, "no 'fmt ' or 'XMA2' tag found\n");
610 return AVERROR_INVALIDDATA;
613 if (data_ofs < 0) {
614 av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
615 return AVERROR_INVALIDDATA;
618 avio_seek(pb, data_ofs, SEEK_SET);
620 if (data_size > (INT64_MAX>>3)) {
621 av_log(s, AV_LOG_WARNING, "Data size %"PRId64" is too large\n", data_size);
622 data_size = 0;
625 if ( st->codecpar->bit_rate > 0 && data_size > 0
626 && st->codecpar->sample_rate > 0
627 && sample_count > 0 && st->codecpar->ch_layout.nb_channels > 1
628 && sample_count % st->codecpar->ch_layout.nb_channels == 0) {
629 if (fabs(8.0 * data_size * st->codecpar->ch_layout.nb_channels * st->codecpar->sample_rate /
630 sample_count /st->codecpar->bit_rate - 1.0) < 0.3)
631 sample_count /= st->codecpar->ch_layout.nb_channels;
634 if (data_size > 0 && sample_count && st->codecpar->ch_layout.nb_channels &&
635 (data_size << 3) / sample_count / st->codecpar->ch_layout.nb_channels > st->codecpar->bits_per_coded_sample + 1) {
636 av_log(s, AV_LOG_WARNING, "ignoring wrong sample_count %"PRId64"\n", sample_count);
637 sample_count = 0;
640 /* G.729 hack (for Ticket4577)
641 * FIXME: Come up with cleaner, more general solution */
642 if (st->codecpar->codec_id == AV_CODEC_ID_G729 && sample_count && (data_size << 3) > sample_count) {
643 av_log(s, AV_LOG_WARNING, "ignoring wrong sample_count %"PRId64"\n", sample_count);
644 sample_count = 0;
647 if (!sample_count || av_get_exact_bits_per_sample(st->codecpar->codec_id) > 0)
648 if ( st->codecpar->ch_layout.nb_channels
649 && data_size
650 && av_get_bits_per_sample(st->codecpar->codec_id)
651 && wav->data_end <= avio_size(pb))
652 sample_count = (data_size << 3)
654 (st->codecpar->ch_layout.nb_channels * (uint64_t)av_get_bits_per_sample(st->codecpar->codec_id));
656 if (sample_count)
657 st->duration = sample_count;
659 if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S32LE &&
660 st->codecpar->block_align == st->codecpar->ch_layout.nb_channels * 4 &&
661 st->codecpar->bits_per_coded_sample == 32 &&
662 st->codecpar->extradata_size == 2 &&
663 AV_RL16(st->codecpar->extradata) == 1) {
664 st->codecpar->codec_id = AV_CODEC_ID_PCM_F16LE;
665 st->codecpar->bits_per_coded_sample = 16;
666 } else if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE &&
667 st->codecpar->block_align == st->codecpar->ch_layout.nb_channels * 4 &&
668 st->codecpar->bits_per_coded_sample == 24) {
669 st->codecpar->codec_id = AV_CODEC_ID_PCM_F24LE;
670 } else if (st->codecpar->codec_id == AV_CODEC_ID_XMA1 ||
671 st->codecpar->codec_id == AV_CODEC_ID_XMA2) {
672 st->codecpar->block_align = 2048;
673 } else if (st->codecpar->codec_id == AV_CODEC_ID_ADPCM_MS && st->codecpar->ch_layout.nb_channels > 2 &&
674 st->codecpar->block_align < INT_MAX / st->codecpar->ch_layout.nb_channels) {
675 st->codecpar->block_align *= st->codecpar->ch_layout.nb_channels;
678 ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
679 ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
681 set_spdif(s, wav);
682 set_max_size(st, wav);
684 return 0;
688 * Find chunk with w64 GUID by skipping over other chunks.
689 * @return the size of the found chunk
691 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
693 uint8_t guid[16];
694 int64_t size;
696 while (!avio_feof(pb)) {
697 avio_read(pb, guid, 16);
698 size = avio_rl64(pb);
699 if (size <= 24 || size > INT64_MAX - 8)
700 return AVERROR_INVALIDDATA;
701 if (!memcmp(guid, guid1, 16))
702 return size;
703 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
705 return AVERROR_EOF;
708 static int wav_read_packet(AVFormatContext *s, AVPacket *pkt)
710 int ret, size;
711 int64_t left;
712 WAVDemuxContext *wav = s->priv_data;
713 AVStream *st = s->streams[0];
715 if (CONFIG_SPDIF_DEMUXER && wav->spdif == 1)
716 return ff_spdif_read_packet(s, pkt);
718 if (wav->smv_data_ofs > 0) {
719 int64_t audio_dts, video_dts;
720 AVStream *vst = wav->vst;
721 smv_retry:
722 audio_dts = (int32_t)ffstream( st)->cur_dts;
723 video_dts = (int32_t)ffstream(vst)->cur_dts;
725 if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
726 /*We always return a video frame first to get the pixel format first*/
727 wav->smv_last_stream = wav->smv_given_first ?
728 av_compare_ts(video_dts, vst->time_base,
729 audio_dts, st->time_base) > 0 : 0;
730 wav->smv_given_first = 1;
732 wav->smv_last_stream = !wav->smv_last_stream;
733 wav->smv_last_stream |= wav->audio_eof;
734 wav->smv_last_stream &= !wav->smv_eof;
735 if (wav->smv_last_stream) {
736 uint64_t old_pos = avio_tell(s->pb);
737 uint64_t new_pos = wav->smv_data_ofs +
738 wav->smv_block * (int64_t)wav->smv_block_size;
739 if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
740 ret = AVERROR_EOF;
741 goto smv_out;
743 size = avio_rl24(s->pb);
744 if (size > wav->smv_block_size) {
745 ret = AVERROR_EOF;
746 goto smv_out;
748 ret = av_get_packet(s->pb, pkt, size);
749 if (ret < 0)
750 goto smv_out;
751 pkt->pos -= 3;
752 pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg;
753 pkt->duration = wav->smv_frames_per_jpeg;
754 wav->smv_block++;
756 pkt->stream_index = vst->index;
757 smv_out:
758 avio_seek(s->pb, old_pos, SEEK_SET);
759 if (ret == AVERROR_EOF) {
760 wav->smv_eof = 1;
761 goto smv_retry;
763 return ret;
767 left = wav->data_end - avio_tell(s->pb);
768 if (wav->ignore_length)
769 left = INT_MAX;
770 if (left <= 0) {
771 if (CONFIG_W64_DEMUXER && wav->w64)
772 left = find_guid(s->pb, ff_w64_guid_data) - 24;
773 else
774 left = find_tag(wav, s->pb, MKTAG('d', 'a', 't', 'a'));
775 if (left < 0) {
776 wav->audio_eof = 1;
777 if (wav->smv_data_ofs > 0 && !wav->smv_eof)
778 goto smv_retry;
779 return AVERROR_EOF;
781 if (INT64_MAX - left < avio_tell(s->pb))
782 return AVERROR_INVALIDDATA;
783 wav->data_end = avio_tell(s->pb) + left;
786 size = wav->max_size;
787 if (st->codecpar->block_align > 1) {
788 if (size < st->codecpar->block_align)
789 size = st->codecpar->block_align;
790 size = (size / st->codecpar->block_align) * st->codecpar->block_align;
792 size = FFMIN(size, left);
793 ret = av_get_packet(s->pb, pkt, size);
794 if (ret < 0)
795 return ret;
796 pkt->stream_index = 0;
798 return ret;
801 static int wav_read_seek(AVFormatContext *s,
802 int stream_index, int64_t timestamp, int flags)
804 WAVDemuxContext *wav = s->priv_data;
805 AVStream *ast = s->streams[0], *vst = wav->vst;
806 wav->smv_eof = 0;
807 wav->audio_eof = 0;
809 if (stream_index != 0 && (!vst || stream_index != vst->index))
810 return AVERROR(EINVAL);
811 if (wav->smv_data_ofs > 0) {
812 int64_t smv_timestamp = timestamp;
813 if (stream_index == 0)
814 smv_timestamp = av_rescale_q(timestamp, ast->time_base, vst->time_base);
815 else
816 timestamp = av_rescale_q(smv_timestamp, vst->time_base, ast->time_base);
817 if (wav->smv_frames_per_jpeg > 0) {
818 wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
822 switch (ast->codecpar->codec_id) {
823 case AV_CODEC_ID_MP2:
824 case AV_CODEC_ID_MP3:
825 case AV_CODEC_ID_AC3:
826 case AV_CODEC_ID_DTS:
827 case AV_CODEC_ID_XMA2:
828 /* use generic seeking with dynamically generated indexes */
829 return -1;
830 default:
831 break;
833 return ff_pcm_read_seek(s, 0, timestamp, flags);
836 static const AVClass wav_demuxer_class = {
837 .class_name = "WAV demuxer",
838 .item_name = av_default_item_name,
839 .option = demux_options,
840 .version = LIBAVUTIL_VERSION_INT,
842 const FFInputFormat ff_wav_demuxer = {
843 .p.name = "wav",
844 .p.long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
845 .p.flags = AVFMT_GENERIC_INDEX,
846 .p.codec_tag = ff_wav_codec_tags_list,
847 .p.priv_class = &wav_demuxer_class,
848 .priv_data_size = sizeof(WAVDemuxContext),
849 .read_probe = wav_probe,
850 .read_header = wav_read_header,
851 .read_packet = wav_read_packet,
852 .read_seek = wav_read_seek,
854 #endif /* CONFIG_WAV_DEMUXER */
856 #if CONFIG_W64_DEMUXER
857 static int w64_probe(const AVProbeData *p)
859 if (p->buf_size <= 40)
860 return 0;
861 if (!memcmp(p->buf, ff_w64_guid_riff, 16) &&
862 !memcmp(p->buf + 24, ff_w64_guid_wave, 16))
863 return AVPROBE_SCORE_MAX;
864 else
865 return 0;
868 static int w64_read_header(AVFormatContext *s)
870 int64_t size, data_ofs = 0;
871 AVIOContext *pb = s->pb;
872 WAVDemuxContext *wav = s->priv_data;
873 AVStream *st;
874 uint8_t guid[16];
875 int ret;
877 if (avio_read(pb, guid, 16) != 16 || memcmp(guid, ff_w64_guid_riff, 16))
878 return AVERROR_INVALIDDATA;
880 /* riff + wave + fmt + sizes */
881 if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8)
882 return AVERROR_INVALIDDATA;
884 avio_read(pb, guid, 16);
885 if (memcmp(guid, ff_w64_guid_wave, 16)) {
886 av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
887 return AVERROR_INVALIDDATA;
890 wav->w64 = 1;
892 st = avformat_new_stream(s, NULL);
893 if (!st)
894 return AVERROR(ENOMEM);
896 while (!avio_feof(pb)) {
897 if (avio_read(pb, guid, 16) != 16)
898 break;
899 size = avio_rl64(pb);
900 if (size <= 24 || INT64_MAX - size < avio_tell(pb)) {
901 if (data_ofs)
902 break;
903 return AVERROR_INVALIDDATA;
906 if (!memcmp(guid, ff_w64_guid_fmt, 16)) {
907 /* subtract chunk header size - normal wav file doesn't count it */
908 ret = ff_get_wav_header(s, pb, st->codecpar, size - 24, 0);
909 if (ret < 0)
910 return ret;
911 avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
912 if (st->codecpar->block_align &&
913 st->codecpar->ch_layout.nb_channels < FF_SANE_NB_CHANNELS &&
914 st->codecpar->bits_per_coded_sample < 128) {
915 int block_align = st->codecpar->block_align;
917 block_align = FFMAX(block_align,
918 ((st->codecpar->bits_per_coded_sample + 7) / 8) *
919 st->codecpar->ch_layout.nb_channels);
920 if (block_align > st->codecpar->block_align) {
921 av_log(s, AV_LOG_WARNING, "invalid block_align: %d, broken file.\n",
922 st->codecpar->block_align);
923 st->codecpar->block_align = block_align;
926 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
927 } else if (!memcmp(guid, ff_w64_guid_fact, 16)) {
928 int64_t samples;
930 samples = avio_rl64(pb);
931 if (samples > 0)
932 st->duration = samples;
933 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 32);
934 } else if (!memcmp(guid, ff_w64_guid_data, 16)) {
935 wav->data_end = avio_tell(pb) + size - 24;
937 data_ofs = avio_tell(pb);
938 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
939 break;
941 avio_skip(pb, size - 24);
942 } else if (!memcmp(guid, ff_w64_guid_summarylist, 16)) {
943 int64_t start, end, cur;
944 uint32_t count, chunk_size, i;
945 int64_t filesize = avio_size(s->pb);
947 start = avio_tell(pb);
948 end = start + FFALIGN(size, INT64_C(8)) - 24;
949 count = avio_rl32(pb);
951 for (i = 0; i < count; i++) {
952 char chunk_key[5], *value;
954 if (avio_feof(pb) || (cur = avio_tell(pb)) < 0 || cur > end - 8 /* = tag + size */)
955 break;
957 chunk_key[4] = 0;
958 avio_read(pb, chunk_key, 4);
959 chunk_size = avio_rl32(pb);
960 if (chunk_size == UINT32_MAX || (filesize >= 0 && chunk_size > filesize))
961 return AVERROR_INVALIDDATA;
963 value = av_malloc(chunk_size + 1);
964 if (!value)
965 return AVERROR(ENOMEM);
967 ret = avio_get_str16le(pb, chunk_size, value, chunk_size);
968 if (ret < 0) {
969 av_free(value);
970 return ret;
972 avio_skip(pb, chunk_size - ret);
974 av_dict_set(&s->metadata, chunk_key, value, AV_DICT_DONT_STRDUP_VAL);
977 avio_skip(pb, end - avio_tell(pb));
978 } else {
979 av_log(s, AV_LOG_DEBUG, "unknown guid: "FF_PRI_GUID"\n", FF_ARG_GUID(guid));
980 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
984 if (!data_ofs)
985 return AVERROR_EOF;
987 ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
988 ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
990 handle_stream_probing(st);
991 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
993 avio_seek(pb, data_ofs, SEEK_SET);
995 set_spdif(s, wav);
996 set_max_size(st, wav);
998 return 0;
1001 static const AVClass w64_demuxer_class = {
1002 .class_name = "W64 demuxer",
1003 .item_name = av_default_item_name,
1004 .option = &demux_options[W64_DEMUXER_OPTIONS_OFFSET],
1005 .version = LIBAVUTIL_VERSION_INT,
1008 const FFInputFormat ff_w64_demuxer = {
1009 .p.name = "w64",
1010 .p.long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
1011 .p.flags = AVFMT_GENERIC_INDEX,
1012 .p.codec_tag = ff_wav_codec_tags_list,
1013 .p.priv_class = &w64_demuxer_class,
1014 .priv_data_size = sizeof(WAVDemuxContext),
1015 .read_probe = w64_probe,
1016 .read_header = w64_read_header,
1017 .read_packet = wav_read_packet,
1018 .read_seek = wav_read_seek,
1020 #endif /* CONFIG_W64_DEMUXER */