1 /*
2 * WAV demuxer
3 * Copyright (c) 2001, 2002 Fabrice Bellard
4 *
5 * Sony Wave64 demuxer
6 * RF64 demuxer
7 * Copyright (c) 2009 Daniel Verkamp
8 *
9 * BW64 demuxer
10 *
11 * This file is part of FFmpeg.
12 *
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.
17 *
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.
22 *
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
26 */
27
28 #include <stdint.h>
29
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/opt.h"
37 #include "avformat.h"
38 #include "avio.h"
39 #include "avio_internal.h"
40 #include "demux.h"
41 #include "id3v2.h"
42 #include "internal.h"
43 #include "metadata.h"
44 #include "pcm.h"
45 #include "riff.h"
46 #include "w64.h"
47 #include "spdif.h"
48
49 typedef struct WAVDemuxContext {
50 const AVClass *class;
51 int64_t data_end;
52 int w64;
53 AVStream *vst;
54 int64_t smv_data_ofs;
55 int smv_block_size;
56 int smv_frames_per_jpeg;
57 int smv_block;
58 int smv_last_stream;
59 int smv_eof;
60 int audio_eof;
61 int ignore_length;
62 int max_size;
63 int spdif;
64 int smv_given_first;
65 int unaligned; // e.g. if an odd number of bytes ID3 tag was prepended
66 int rifx; // RIFX: integer byte order for parameters is big endian
67 } WAVDemuxContext;
68
69 #define OFFSET(x) offsetof(WAVDemuxContext, x)
70 #define DEC AV_OPT_FLAG_DECODING_PARAM
71 static const AVOption demux_options[] = {
72 #define W64_DEMUXER_OPTIONS_OFFSET (1 * CONFIG_WAV_DEMUXER)
73 #if CONFIG_WAV_DEMUXER
74 { "ignore_length", "Ignore length", OFFSET(ignore_length), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, DEC },
75 #endif
76 { "max_size", "max size of single packet", OFFSET(max_size), AV_OPT_TYPE_INT, { .i64 = 4096 }, 1024, 1 << 22, DEC },
77 { NULL },
78 };
79
set_spdif(AVFormatContext *s, WAVDemuxContext *wav)80 static void set_spdif(AVFormatContext *s, WAVDemuxContext *wav)
81 {
82 if (CONFIG_SPDIF_DEMUXER && s->streams[0]->codecpar->codec_tag == 1) {
83 enum AVCodecID codec;
84 int len = 1<<16;
85 int ret = ffio_ensure_seekback(s->pb, len);
86
87 if (ret >= 0) {
88 uint8_t *buf = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE);
89 if (!buf) {
90 ret = AVERROR(ENOMEM);
91 } else {
92 int64_t pos = avio_tell(s->pb);
93 len = ret = avio_read(s->pb, buf, len);
94 if (len >= 0) {
95 ret = ff_spdif_probe(buf, len, &codec);
96 if (ret > AVPROBE_SCORE_EXTENSION) {
97 s->streams[0]->codecpar->codec_id = codec;
98 wav->spdif = 1;
99 }
100 }
101 avio_seek(s->pb, pos, SEEK_SET);
102 av_free(buf);
103 }
104 }
105
106 if (ret < 0)
107 av_log(s, AV_LOG_WARNING, "Cannot check for SPDIF\n");
108 }
109 }
110
111 #if CONFIG_WAV_DEMUXER
112
next_tag(AVIOContext *pb, uint32_t *tag, int big_endian)113 static int64_t next_tag(AVIOContext *pb, uint32_t *tag, int big_endian)
114 {
115 *tag = avio_rl32(pb);
116 if (!big_endian) {
117 return avio_rl32(pb);
118 } else {
119 return avio_rb32(pb);
120 }
121 }
122
123 /* RIFF chunks are always at even offsets relative to where they start. */
wav_seek_tag(WAVDemuxContext * wav, AVIOContext *s, int64_t offset, int whence)124 static int64_t wav_seek_tag(WAVDemuxContext * wav, AVIOContext *s, int64_t offset, int whence)
125 {
126 offset += offset < INT64_MAX && offset + wav->unaligned & 1;
127
128 return avio_seek(s, offset, whence);
129 }
130
131 /* return the size of the found tag */
find_tag(WAVDemuxContext * wav, AVIOContext *pb, uint32_t tag1)132 static int64_t find_tag(WAVDemuxContext * wav, AVIOContext *pb, uint32_t tag1)
133 {
134 unsigned int tag;
135 int64_t size;
136
137 for (;;) {
138 if (avio_feof(pb))
139 return AVERROR_EOF;
140 size = next_tag(pb, &tag, wav->rifx);
141 if (tag == tag1)
142 break;
143 wav_seek_tag(wav, pb, size, SEEK_CUR);
144 }
145 return size;
146 }
147
wav_probe(const AVProbeData *p)148 static int wav_probe(const AVProbeData *p)
149 {
150 /* check file header */
151 if (p->buf_size <= 32)
152 return 0;
153 if (!memcmp(p->buf + 8, "WAVE", 4)) {
154 if (!memcmp(p->buf, "RIFF", 4) || !memcmp(p->buf, "RIFX", 4))
155 /* Since the ACT demuxer has a standard WAV header at the top of
156 * its own, the returned score is decreased to avoid a probe
157 * conflict between ACT and WAV. */
158 return AVPROBE_SCORE_MAX - 1;
159 else if ((!memcmp(p->buf, "RF64", 4) ||
160 !memcmp(p->buf, "BW64", 4)) &&
161 !memcmp(p->buf + 12, "ds64", 4))
162 return AVPROBE_SCORE_MAX;
163 }
164 return 0;
165 }
166
handle_stream_probing(AVStream *st)167 static void handle_stream_probing(AVStream *st)
168 {
169 if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE) {
170 FFStream *const sti = ffstream(st);
171 sti->request_probe = AVPROBE_SCORE_EXTENSION;
172 sti->probe_packets = FFMIN(sti->probe_packets, 32);
173 }
174 }
175
wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream *st)176 static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream *st)
177 {
178 AVIOContext *pb = s->pb;
179 WAVDemuxContext *wav = s->priv_data;
180 int ret;
181
182 /* parse fmt header */
183 ret = ff_get_wav_header(s, pb, st->codecpar, size, wav->rifx);
184 if (ret < 0)
185 return ret;
186 handle_stream_probing(st);
187
188 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
189
190 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
191
192 return 0;
193 }
194
wav_parse_xma2_tag(AVFormatContext *s, int64_t size, AVStream *st)195 static int wav_parse_xma2_tag(AVFormatContext *s, int64_t size, AVStream *st)
196 {
197 AVIOContext *pb = s->pb;
198 int version, num_streams, i, channels = 0, ret;
199
200 if (size < 36)
201 return AVERROR_INVALIDDATA;
202
203 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
204 st->codecpar->codec_id = AV_CODEC_ID_XMA2;
205 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
206
207 version = avio_r8(pb);
208 if (version != 3 && version != 4)
209 return AVERROR_INVALIDDATA;
210 num_streams = avio_r8(pb);
211 if (size != (32 + ((version==3)?0:8) + 4*num_streams))
212 return AVERROR_INVALIDDATA;
213 avio_skip(pb, 10);
214 st->codecpar->sample_rate = avio_rb32(pb);
215 if (version == 4)
216 avio_skip(pb, 8);
217 avio_skip(pb, 4);
218 st->duration = avio_rb32(pb);
219 avio_skip(pb, 8);
220
221 for (i = 0; i < num_streams; i++) {
222 channels += avio_r8(pb);
223 avio_skip(pb, 3);
224 }
225 av_channel_layout_uninit(&st->codecpar->ch_layout);
226 st->codecpar->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
227 st->codecpar->ch_layout.nb_channels = channels;
228
229 if (st->codecpar->ch_layout.nb_channels <= 0 || st->codecpar->sample_rate <= 0)
230 return AVERROR_INVALIDDATA;
231
232 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
233
234 avio_seek(pb, -size, SEEK_CUR);
235 if ((ret = ff_get_extradata(s, st->codecpar, pb, size)) < 0)
236 return ret;
237
238 return 0;
239 }
240
wav_parse_bext_string(AVFormatContext *s, const char *key, int length)241 static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
242 int length)
243 {
244 char temp[257];
245 int ret;
246
247 av_assert0(length < sizeof(temp));
248 if ((ret = ffio_read_size(s->pb, temp, length)) < 0)
249 return ret;
250
251 temp[length] = 0;
252
253 if (strlen(temp))
254 return av_dict_set(&s->metadata, key, temp, 0);
255
256 return 0;
257 }
258
wav_parse_bext_tag(AVFormatContext *s, int64_t size)259 static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
260 {
261 char temp[131], *coding_history;
262 int ret, x;
263 uint64_t time_reference;
264 int64_t umid_parts[8], umid_mask = 0;
265
266 if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
267 (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
268 (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
269 (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
270 (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
271 return ret;
272
273 time_reference = avio_rl64(s->pb);
274 snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
275 if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
276 return ret;
277
278 /* check if version is >= 1, in which case an UMID may be present */
279 if (avio_rl16(s->pb) >= 1) {
280 for (x = 0; x < 8; x++)
281 umid_mask |= umid_parts[x] = avio_rb64(s->pb);
282
283 if (umid_mask) {
284 /* the string formatting below is per SMPTE 330M-2004 Annex C */
285 if (umid_parts[4] == 0 && umid_parts[5] == 0 &&
286 umid_parts[6] == 0 && umid_parts[7] == 0) {
287 /* basic UMID */
288 snprintf(temp, sizeof(temp),
289 "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
290 umid_parts[0], umid_parts[1],
291 umid_parts[2], umid_parts[3]);
292 } else {
293 /* extended UMID */
294 snprintf(temp, sizeof(temp),
295 "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
296 "%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
297 umid_parts[0], umid_parts[1],
298 umid_parts[2], umid_parts[3],
299 umid_parts[4], umid_parts[5],
300 umid_parts[6], umid_parts[7]);
301 }
302
303 if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
304 return ret;
305 }
306
307 avio_skip(s->pb, 190);
308 } else
309 avio_skip(s->pb, 254);
310
311 if (size > 602) {
312 /* CodingHistory present */
313 size -= 602;
314
315 if (!(coding_history = av_malloc(size + 1)))
316 return AVERROR(ENOMEM);
317
318 if ((ret = ffio_read_size(s->pb, coding_history, size)) < 0) {
319 av_free(coding_history);
320 return ret;
321 }
322
323 coding_history[size] = 0;
324 if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
325 AV_DICT_DONT_STRDUP_VAL)) < 0)
326 return ret;
327 }
328
329 return 0;
330 }
331
332 static const AVMetadataConv wav_metadata_conv[] = {
333 { "description", "comment" },
334 { "originator", "encoded_by" },
335 { "origination_date", "date" },
336 { "origination_time", "creation_time" },
337 { 0 },
338 };
339
340 /* wav input */
wav_read_header(AVFormatContext *s)341 static int wav_read_header(AVFormatContext *s)
342 {
343 int64_t size, av_uninit(data_size);
344 int64_t sample_count = 0;
345 int rf64 = 0, bw64 = 0;
346 uint32_t tag;
347 AVIOContext *pb = s->pb;
348 AVStream *st = NULL;
349 WAVDemuxContext *wav = s->priv_data;
350 int ret, got_fmt = 0, got_xma2 = 0;
351 int64_t next_tag_ofs, data_ofs = -1;
352
353 wav->unaligned = avio_tell(s->pb) & 1;
354
355 wav->smv_data_ofs = -1;
356
357 /* read chunk ID */
358 tag = avio_rl32(pb);
359 switch (tag) {
360 case MKTAG('R', 'I', 'F', 'F'):
361 break;
362 case MKTAG('R', 'I', 'F', 'X'):
363 wav->rifx = 1;
364 break;
365 case MKTAG('R', 'F', '6', '4'):
366 rf64 = 1;
367 break;
368 case MKTAG('B', 'W', '6', '4'):
369 bw64 = 1;
370 break;
371 default:
372 av_log(s, AV_LOG_ERROR, "invalid start code %s in RIFF header\n",
373 av_fourcc2str(tag));
374 return AVERROR_INVALIDDATA;
375 }
376
377 /* read chunk size */
378 avio_rl32(pb);
379
380 /* read format */
381 if (avio_rl32(pb) != MKTAG('W', 'A', 'V', 'E')) {
382 av_log(s, AV_LOG_ERROR, "invalid format in RIFF header\n");
383 return AVERROR_INVALIDDATA;
384 }
385
386 if (rf64 || bw64) {
387 if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
388 return AVERROR_INVALIDDATA;
389 size = avio_rl32(pb);
390 if (size < 24)
391 return AVERROR_INVALIDDATA;
392 avio_rl64(pb); /* RIFF size */
393
394 data_size = avio_rl64(pb);
395 sample_count = avio_rl64(pb);
396
397 if (data_size < 0 || sample_count < 0) {
398 av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
399 "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
400 data_size, sample_count);
401 return AVERROR_INVALIDDATA;
402 }
403 avio_skip(pb, size - 24); /* skip rest of ds64 chunk */
404
405 }
406
407 /* Create the audio stream now so that its index is always zero */
408 st = avformat_new_stream(s, NULL);
409 if (!st)
410 return AVERROR(ENOMEM);
411
412 for (;;) {
413 AVStream *vst;
414 size = next_tag(pb, &tag, wav->rifx);
415 next_tag_ofs = avio_tell(pb) + size;
416
417 if (avio_feof(pb))
418 break;
419
420 switch (tag) {
421 case MKTAG('f', 'm', 't', ' '):
422 /* only parse the first 'fmt ' tag found */
423 if (!got_xma2 && !got_fmt && (ret = wav_parse_fmt_tag(s, size, st)) < 0) {
424 return ret;
425 } else if (got_fmt)
426 av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
427
428 got_fmt = 1;
429 break;
430 case MKTAG('X', 'M', 'A', '2'):
431 /* only parse the first 'XMA2' tag found */
432 if (!got_fmt && !got_xma2 && (ret = wav_parse_xma2_tag(s, size, st)) < 0) {
433 return ret;
434 } else if (got_xma2)
435 av_log(s, AV_LOG_WARNING, "found more than one 'XMA2' tag\n");
436
437 got_xma2 = 1;
438 break;
439 case MKTAG('d', 'a', 't', 'a'):
440 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) && !got_fmt && !got_xma2) {
441 av_log(s, AV_LOG_ERROR,
442 "found no 'fmt ' tag before the 'data' tag\n");
443 return AVERROR_INVALIDDATA;
444 }
445
446 if (rf64 || bw64) {
447 next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
448 } else if (size != 0xFFFFFFFF) {
449 data_size = size;
450 next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
451 } else {
452 av_log(s, AV_LOG_WARNING, "Ignoring maximum wav data size, "
453 "file may be invalid\n");
454 data_size = 0;
455 next_tag_ofs = wav->data_end = INT64_MAX;
456 }
457
458 data_ofs = avio_tell(pb);
459
460 /* don't look for footer metadata if we can't seek or if we don't
461 * know where the data tag ends
462 */
463 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) || (!(rf64 && !bw64) && !size))
464 goto break_loop;
465 break;
466 case MKTAG('f', 'a', 'c', 't'):
467 if (!sample_count)
468 sample_count = (!wav->rifx ? avio_rl32(pb) : avio_rb32(pb));
469 break;
470 case MKTAG('b', 'e', 'x', 't'):
471 if ((ret = wav_parse_bext_tag(s, size)) < 0)
472 return ret;
473 break;
474 case MKTAG('S','M','V','0'):
475 if (!got_fmt) {
476 av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'SMV0' tag\n");
477 return AVERROR_INVALIDDATA;
478 }
479 // SMV file, a wav file with video appended.
480 if (size != MKTAG('0','2','0','0')) {
481 av_log(s, AV_LOG_ERROR, "Unknown SMV version found\n");
482 goto break_loop;
483 }
484 av_log(s, AV_LOG_DEBUG, "Found SMV data\n");
485 wav->smv_given_first = 0;
486 vst = avformat_new_stream(s, NULL);
487 if (!vst)
488 return AVERROR(ENOMEM);
489 wav->vst = vst;
490 avio_r8(pb);
491 vst->id = 1;
492 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
493 vst->codecpar->codec_id = AV_CODEC_ID_SMVJPEG;
494 vst->codecpar->width = avio_rl24(pb);
495 vst->codecpar->height = avio_rl24(pb);
496 if ((ret = ff_alloc_extradata(vst->codecpar, 4)) < 0) {
497 av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n");
498 return ret;
499 }
500 size = avio_rl24(pb);
501 wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
502 avio_rl24(pb);
503 wav->smv_block_size = avio_rl24(pb);
504 if (!wav->smv_block_size)
505 return AVERROR_INVALIDDATA;
506 avpriv_set_pts_info(vst, 32, 1, avio_rl24(pb));
507 vst->duration = avio_rl24(pb);
508 avio_rl24(pb);
509 avio_rl24(pb);
510 wav->smv_frames_per_jpeg = avio_rl24(pb);
511 if (wav->smv_frames_per_jpeg > 65536) {
512 av_log(s, AV_LOG_ERROR, "too many frames per jpeg\n");
513 return AVERROR_INVALIDDATA;
514 }
515 AV_WL32(vst->codecpar->extradata, wav->smv_frames_per_jpeg);
516 goto break_loop;
517 case MKTAG('L', 'I', 'S', 'T'):
518 case MKTAG('l', 'i', 's', 't'):
519 if (size < 4) {
520 av_log(s, AV_LOG_ERROR, "too short LIST tag\n");
521 return AVERROR_INVALIDDATA;
522 }
523 switch (avio_rl32(pb)) {
524 case MKTAG('I', 'N', 'F', 'O'):
525 ff_read_riff_info(s, size - 4);
526 break;
527 case MKTAG('a', 'd', 't', 'l'):
528 if (s->nb_chapters > 0) {
529 while (avio_tell(pb) < next_tag_ofs &&
530 !avio_feof(pb)) {
531 char cue_label[512];
532 unsigned id, sub_size;
533
534 if (avio_rl32(pb) != MKTAG('l', 'a', 'b', 'l'))
535 break;
536
537 sub_size = avio_rl32(pb);
538 if (sub_size < 5)
539 break;
540 id = avio_rl32(pb);
541 avio_get_str(pb, sub_size - 4, cue_label, sizeof(cue_label));
542 avio_skip(pb, avio_tell(pb) & 1);
543
544 for (int i = 0; i < s->nb_chapters; i++) {
545 if (s->chapters[i]->id == id) {
546 av_dict_set(&s->chapters[i]->metadata, "title", cue_label, 0);
547 break;
548 }
549 }
550 }
551 }
552 break;
553 }
554 break;
555 case MKTAG('I', 'D', '3', ' '):
556 case MKTAG('i', 'd', '3', ' '): {
557 ID3v2ExtraMeta *id3v2_extra_meta;
558 ff_id3v2_read_dict(pb, &ffformatcontext(s)->id3v2_meta, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
559 if (id3v2_extra_meta) {
560 ff_id3v2_parse_apic(s, id3v2_extra_meta);
561 ff_id3v2_parse_chapters(s, id3v2_extra_meta);
562 ff_id3v2_parse_priv(s, id3v2_extra_meta);
563 }
564 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
565 }
566 break;
567 case MKTAG('c', 'u', 'e', ' '):
568 if (size >= 4 && got_fmt && st->codecpar->sample_rate > 0) {
569 AVRational tb = {1, st->codecpar->sample_rate};
570 unsigned nb_cues = avio_rl32(pb);
571
572 if (size >= nb_cues * 24LL + 4LL) {
573 for (int i = 0; i < nb_cues; i++) {
574 unsigned offset, id = avio_rl32(pb);
575
576 if (avio_feof(pb))
577 return AVERROR_INVALIDDATA;
578
579 avio_skip(pb, 16);
580 offset = avio_rl32(pb);
581
582 if (!avpriv_new_chapter(s, id, tb, offset, AV_NOPTS_VALUE, NULL))
583 return AVERROR(ENOMEM);
584 }
585 }
586 }
587 break;
588 }
589
590 /* seek to next tag unless we know that we'll run into EOF */
591 if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
592 wav_seek_tag(wav, pb, next_tag_ofs, SEEK_SET) < 0) {
593 break;
594 }
595 }
596
597 break_loop:
598 if (!got_fmt && !got_xma2) {
599 av_log(s, AV_LOG_ERROR, "no 'fmt ' or 'XMA2' tag found\n");
600 return AVERROR_INVALIDDATA;
601 }
602
603 if (data_ofs < 0) {
604 av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
605 return AVERROR_INVALIDDATA;
606 }
607
608 avio_seek(pb, data_ofs, SEEK_SET);
609
610 if (data_size > (INT64_MAX>>3)) {
611 av_log(s, AV_LOG_WARNING, "Data size %"PRId64" is too large\n", data_size);
612 data_size = 0;
613 }
614
615 if ( st->codecpar->bit_rate > 0 && data_size > 0
616 && st->codecpar->sample_rate > 0
617 && sample_count > 0 && st->codecpar->ch_layout.nb_channels > 1
618 && sample_count % st->codecpar->ch_layout.nb_channels == 0) {
619 if (fabs(8.0 * data_size * st->codecpar->ch_layout.nb_channels * st->codecpar->sample_rate /
620 sample_count /st->codecpar->bit_rate - 1.0) < 0.3)
621 sample_count /= st->codecpar->ch_layout.nb_channels;
622 }
623
624 if (data_size > 0 && sample_count && st->codecpar->ch_layout.nb_channels &&
625 (data_size << 3) / sample_count / st->codecpar->ch_layout.nb_channels > st->codecpar->bits_per_coded_sample + 1) {
626 av_log(s, AV_LOG_WARNING, "ignoring wrong sample_count %"PRId64"\n", sample_count);
627 sample_count = 0;
628 }
629
630 /* G.729 hack (for Ticket4577)
631 * FIXME: Come up with cleaner, more general solution */
632 if (st->codecpar->codec_id == AV_CODEC_ID_G729 && sample_count && (data_size << 3) > sample_count) {
633 av_log(s, AV_LOG_WARNING, "ignoring wrong sample_count %"PRId64"\n", sample_count);
634 sample_count = 0;
635 }
636
637 if (!sample_count || av_get_exact_bits_per_sample(st->codecpar->codec_id) > 0)
638 if ( st->codecpar->ch_layout.nb_channels
639 && data_size
640 && av_get_bits_per_sample(st->codecpar->codec_id)
641 && wav->data_end <= avio_size(pb))
642 sample_count = (data_size << 3)
643 /
644 (st->codecpar->ch_layout.nb_channels * (uint64_t)av_get_bits_per_sample(st->codecpar->codec_id));
645
646 if (sample_count)
647 st->duration = sample_count;
648
649 if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S32LE &&
650 st->codecpar->block_align == st->codecpar->ch_layout.nb_channels * 4 &&
651 st->codecpar->bits_per_coded_sample == 32 &&
652 st->codecpar->extradata_size == 2 &&
653 AV_RL16(st->codecpar->extradata) == 1) {
654 st->codecpar->codec_id = AV_CODEC_ID_PCM_F16LE;
655 st->codecpar->bits_per_coded_sample = 16;
656 } else if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE &&
657 st->codecpar->block_align == st->codecpar->ch_layout.nb_channels * 4 &&
658 st->codecpar->bits_per_coded_sample == 24) {
659 st->codecpar->codec_id = AV_CODEC_ID_PCM_F24LE;
660 } else if (st->codecpar->codec_id == AV_CODEC_ID_XMA1 ||
661 st->codecpar->codec_id == AV_CODEC_ID_XMA2) {
662 st->codecpar->block_align = 2048;
663 } else if (st->codecpar->codec_id == AV_CODEC_ID_ADPCM_MS && st->codecpar->ch_layout.nb_channels > 2 &&
664 st->codecpar->block_align < INT_MAX / st->codecpar->ch_layout.nb_channels) {
665 st->codecpar->block_align *= st->codecpar->ch_layout.nb_channels;
666 }
667
668 ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
669 ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
670
671 set_spdif(s, wav);
672
673 #ifdef OHOS_OPT_COMPAT
674 if (ffformatcontext(s)->id3v2_meta && s->metadata) {
675 av_log(s, AV_LOG_WARNING, "Discarding fmt metadata because ID3 tag is found.\n");
676 av_dict_free(&s->metadata);
677 s->metadata = NULL;
678 }
679 #endif
680
681 return 0;
682 }
683
684 /**
685 * Find chunk with w64 GUID by skipping over other chunks.
686 * @return the size of the found chunk
687 */
find_guid(AVIOContext *pb, const uint8_t guid1[16])688 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
689 {
690 uint8_t guid[16];
691 int64_t size;
692
693 while (!avio_feof(pb)) {
694 avio_read(pb, guid, 16);
695 size = avio_rl64(pb);
696 if (size <= 24 || size > INT64_MAX - 8)
697 return AVERROR_INVALIDDATA;
698 if (!memcmp(guid, guid1, 16))
699 return size;
700 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
701 }
702 return AVERROR_EOF;
703 }
704
wav_read_packet(AVFormatContext *s, AVPacket *pkt)705 static int wav_read_packet(AVFormatContext *s, AVPacket *pkt)
706 {
707 int ret, size;
708 int64_t left;
709 WAVDemuxContext *wav = s->priv_data;
710 AVStream *st = s->streams[0];
711
712 if (CONFIG_SPDIF_DEMUXER && wav->spdif == 1)
713 return ff_spdif_read_packet(s, pkt);
714
715 if (wav->smv_data_ofs > 0) {
716 int64_t audio_dts, video_dts;
717 AVStream *vst = wav->vst;
718 smv_retry:
719 audio_dts = (int32_t)ffstream( st)->cur_dts;
720 video_dts = (int32_t)ffstream(vst)->cur_dts;
721
722 if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
723 /*We always return a video frame first to get the pixel format first*/
724 wav->smv_last_stream = wav->smv_given_first ?
725 av_compare_ts(video_dts, vst->time_base,
726 audio_dts, st->time_base) > 0 : 0;
727 wav->smv_given_first = 1;
728 }
729 wav->smv_last_stream = !wav->smv_last_stream;
730 wav->smv_last_stream |= wav->audio_eof;
731 wav->smv_last_stream &= !wav->smv_eof;
732 if (wav->smv_last_stream) {
733 uint64_t old_pos = avio_tell(s->pb);
734 uint64_t new_pos = wav->smv_data_ofs +
735 wav->smv_block * (int64_t)wav->smv_block_size;
736 if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
737 ret = AVERROR_EOF;
738 goto smv_out;
739 }
740 size = avio_rl24(s->pb);
741 if (size > wav->smv_block_size) {
742 ret = AVERROR_EOF;
743 goto smv_out;
744 }
745 ret = av_get_packet(s->pb, pkt, size);
746 if (ret < 0)
747 goto smv_out;
748 pkt->pos -= 3;
749 pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg;
750 pkt->duration = wav->smv_frames_per_jpeg;
751 wav->smv_block++;
752
753 pkt->stream_index = vst->index;
754 smv_out:
755 avio_seek(s->pb, old_pos, SEEK_SET);
756 if (ret == AVERROR_EOF) {
757 wav->smv_eof = 1;
758 goto smv_retry;
759 }
760 return ret;
761 }
762 }
763
764 left = wav->data_end - avio_tell(s->pb);
765 if (wav->ignore_length)
766 left = INT_MAX;
767 if (left <= 0) {
768 if (CONFIG_W64_DEMUXER && wav->w64)
769 left = find_guid(s->pb, ff_w64_guid_data) - 24;
770 else
771 left = find_tag(wav, s->pb, MKTAG('d', 'a', 't', 'a'));
772 if (left < 0) {
773 wav->audio_eof = 1;
774 if (wav->smv_data_ofs > 0 && !wav->smv_eof)
775 goto smv_retry;
776 return AVERROR_EOF;
777 }
778 if (INT64_MAX - left < avio_tell(s->pb))
779 return AVERROR_INVALIDDATA;
780 wav->data_end = avio_tell(s->pb) + left;
781 }
782
783 size = wav->max_size;
784 if (st->codecpar->block_align > 1) {
785 if (size < st->codecpar->block_align)
786 size = st->codecpar->block_align;
787 size = (size / st->codecpar->block_align) * st->codecpar->block_align;
788 }
789 size = FFMIN(size, left);
790 ret = av_get_packet(s->pb, pkt, size);
791 if (ret < 0)
792 return ret;
793 pkt->stream_index = 0;
794
795 return ret;
796 }
797
wav_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)798 static int wav_read_seek(AVFormatContext *s,
799 int stream_index, int64_t timestamp, int flags)
800 {
801 WAVDemuxContext *wav = s->priv_data;
802 AVStream *ast = s->streams[0], *vst = wav->vst;
803 wav->smv_eof = 0;
804 wav->audio_eof = 0;
805
806 if (stream_index != 0 && (!vst || stream_index != vst->index))
807 return AVERROR(EINVAL);
808 if (wav->smv_data_ofs > 0) {
809 int64_t smv_timestamp = timestamp;
810 if (stream_index == 0)
811 smv_timestamp = av_rescale_q(timestamp, ast->time_base, vst->time_base);
812 else
813 timestamp = av_rescale_q(smv_timestamp, vst->time_base, ast->time_base);
814 if (wav->smv_frames_per_jpeg > 0) {
815 wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
816 }
817 }
818
819 switch (ast->codecpar->codec_id) {
820 case AV_CODEC_ID_MP2:
821 case AV_CODEC_ID_MP3:
822 case AV_CODEC_ID_AC3:
823 case AV_CODEC_ID_DTS:
824 case AV_CODEC_ID_XMA2:
825 /* use generic seeking with dynamically generated indexes */
826 return -1;
827 default:
828 break;
829 }
830 return ff_pcm_read_seek(s, 0, timestamp, flags);
831 }
832
833 static const AVClass wav_demuxer_class = {
834 .class_name = "WAV demuxer",
835 .item_name = av_default_item_name,
836 .option = demux_options,
837 .version = LIBAVUTIL_VERSION_INT,
838 };
839 const AVInputFormat ff_wav_demuxer = {
840 .name = "wav",
841 .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
842 .priv_data_size = sizeof(WAVDemuxContext),
843 .read_probe = wav_probe,
844 .read_header = wav_read_header,
845 .read_packet = wav_read_packet,
846 .read_seek = wav_read_seek,
847 .flags = AVFMT_GENERIC_INDEX,
848 .codec_tag = ff_wav_codec_tags_list,
849 .priv_class = &wav_demuxer_class,
850 };
851 #endif /* CONFIG_WAV_DEMUXER */
852
853 #if CONFIG_W64_DEMUXER
w64_probe(const AVProbeData *p)854 static int w64_probe(const AVProbeData *p)
855 {
856 if (p->buf_size <= 40)
857 return 0;
858 if (!memcmp(p->buf, ff_w64_guid_riff, 16) &&
859 !memcmp(p->buf + 24, ff_w64_guid_wave, 16))
860 return AVPROBE_SCORE_MAX;
861 else
862 return 0;
863 }
864
w64_read_header(AVFormatContext *s)865 static int w64_read_header(AVFormatContext *s)
866 {
867 int64_t size, data_ofs = 0;
868 AVIOContext *pb = s->pb;
869 WAVDemuxContext *wav = s->priv_data;
870 AVStream *st;
871 uint8_t guid[16];
872 int ret;
873
874 if (avio_read(pb, guid, 16) != 16 || memcmp(guid, ff_w64_guid_riff, 16))
875 return AVERROR_INVALIDDATA;
876
877 /* riff + wave + fmt + sizes */
878 if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8)
879 return AVERROR_INVALIDDATA;
880
881 avio_read(pb, guid, 16);
882 if (memcmp(guid, ff_w64_guid_wave, 16)) {
883 av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
884 return AVERROR_INVALIDDATA;
885 }
886
887 wav->w64 = 1;
888
889 st = avformat_new_stream(s, NULL);
890 if (!st)
891 return AVERROR(ENOMEM);
892
893 while (!avio_feof(pb)) {
894 if (avio_read(pb, guid, 16) != 16)
895 break;
896 size = avio_rl64(pb);
897 if (size <= 24 || INT64_MAX - size < avio_tell(pb))
898 return AVERROR_INVALIDDATA;
899
900 if (!memcmp(guid, ff_w64_guid_fmt, 16)) {
901 /* subtract chunk header size - normal wav file doesn't count it */
902 ret = ff_get_wav_header(s, pb, st->codecpar, size - 24, 0);
903 if (ret < 0)
904 return ret;
905 avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
906
907 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
908 } else if (!memcmp(guid, ff_w64_guid_fact, 16)) {
909 int64_t samples;
910
911 samples = avio_rl64(pb);
912 if (samples > 0)
913 st->duration = samples;
914 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 32);
915 } else if (!memcmp(guid, ff_w64_guid_data, 16)) {
916 wav->data_end = avio_tell(pb) + size - 24;
917
918 data_ofs = avio_tell(pb);
919 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
920 break;
921
922 avio_skip(pb, size - 24);
923 } else if (!memcmp(guid, ff_w64_guid_summarylist, 16)) {
924 int64_t start, end, cur;
925 uint32_t count, chunk_size, i;
926 int64_t filesize = avio_size(s->pb);
927
928 start = avio_tell(pb);
929 end = start + FFALIGN(size, INT64_C(8)) - 24;
930 count = avio_rl32(pb);
931
932 for (i = 0; i < count; i++) {
933 char chunk_key[5], *value;
934
935 if (avio_feof(pb) || (cur = avio_tell(pb)) < 0 || cur > end - 8 /* = tag + size */)
936 break;
937
938 chunk_key[4] = 0;
939 avio_read(pb, chunk_key, 4);
940 chunk_size = avio_rl32(pb);
941 if (chunk_size == UINT32_MAX || (filesize >= 0 && chunk_size > filesize))
942 return AVERROR_INVALIDDATA;
943
944 value = av_malloc(chunk_size + 1);
945 if (!value)
946 return AVERROR(ENOMEM);
947
948 ret = avio_get_str16le(pb, chunk_size, value, chunk_size);
949 if (ret < 0) {
950 av_free(value);
951 return ret;
952 }
953 avio_skip(pb, chunk_size - ret);
954
955 av_dict_set(&s->metadata, chunk_key, value, AV_DICT_DONT_STRDUP_VAL);
956 }
957
958 avio_skip(pb, end - avio_tell(pb));
959 } else {
960 av_log(s, AV_LOG_DEBUG, "unknown guid: "FF_PRI_GUID"\n", FF_ARG_GUID(guid));
961 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
962 }
963 }
964
965 if (!data_ofs)
966 return AVERROR_EOF;
967
968 ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
969 ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
970
971 handle_stream_probing(st);
972 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
973
974 avio_seek(pb, data_ofs, SEEK_SET);
975
976 set_spdif(s, wav);
977
978 return 0;
979 }
980
981 static const AVClass w64_demuxer_class = {
982 .class_name = "W64 demuxer",
983 .item_name = av_default_item_name,
984 .option = &demux_options[W64_DEMUXER_OPTIONS_OFFSET],
985 .version = LIBAVUTIL_VERSION_INT,
986 };
987
988 const AVInputFormat ff_w64_demuxer = {
989 .name = "w64",
990 .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
991 .priv_data_size = sizeof(WAVDemuxContext),
992 .read_probe = w64_probe,
993 .read_header = w64_read_header,
994 .read_packet = wav_read_packet,
995 .read_seek = wav_read_seek,
996 .flags = AVFMT_GENERIC_INDEX,
997 .codec_tag = ff_wav_codec_tags_list,
998 .priv_class = &w64_demuxer_class,
999 };
1000 #endif /* CONFIG_W64_DEMUXER */
1001