FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/wavdec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 359 572 62.8%
Functions: 16 17 94.1%
Branches: 208 444 46.8%

Line Branch Exec Source
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/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"
50
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;
70
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 },
80 };
81
82 549 static void set_max_size(AVStream *st, WAVDemuxContext *wav)
83 {
84
2/2
✓ Branch 0 taken 542 times.
✓ Branch 1 taken 7 times.
549 if (wav->max_size <= 0) {
85 542 int max_size = ff_pcm_default_packet_size(st->codecpar);
86
1/2
✓ Branch 0 taken 542 times.
✗ Branch 1 not taken.
542 wav->max_size = max_size < 0 ? 4096 : max_size;
87 }
88 549 }
89
90 549 static void set_spdif(AVFormatContext *s, WAVDemuxContext *wav)
91 {
92
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 37 times.
549 if (CONFIG_SPDIF_DEMUXER && s->streams[0]->codecpar->codec_tag == 1) {
93 enum AVCodecID codec;
94 512 int len = 1<<16;
95 512 int ret = ffio_ensure_seekback(s->pb, len);
96
97
1/2
✓ Branch 0 taken 512 times.
✗ Branch 1 not taken.
512 if (ret >= 0) {
98 512 uint8_t *buf = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE);
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 512 times.
512 if (!buf) {
100 ret = AVERROR(ENOMEM);
101 } else {
102 512 int64_t pos = avio_tell(s->pb);
103 512 len = ret = avio_read(s->pb, buf, len);
104
1/2
✓ Branch 0 taken 512 times.
✗ Branch 1 not taken.
512 if (len >= 0) {
105 512 ret = ff_spdif_probe(buf, len, &codec);
106
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 511 times.
512 if (ret > AVPROBE_SCORE_EXTENSION) {
107 1 s->streams[0]->codecpar->codec_id = codec;
108 1 wav->spdif = 1;
109 }
110 }
111 512 avio_seek(s->pb, pos, SEEK_SET);
112 512 av_free(buf);
113 }
114 }
115
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 512 times.
512 if (ret < 0)
117 av_log(s, AV_LOG_WARNING, "Cannot check for SPDIF\n");
118 }
119 549 }
120
121 #if CONFIG_WAV_DEMUXER
122
123 1366 static int64_t next_tag(AVIOContext *pb, uint32_t *tag, int big_endian)
124 {
125 1366 *tag = avio_rl32(pb);
126
1/2
✓ Branch 0 taken 1366 times.
✗ Branch 1 not taken.
1366 if (!big_endian) {
127 1366 return avio_rl32(pb);
128 } else {
129 return avio_rb32(pb);
130 }
131 }
132
133 /* RIFF chunks are always at even offsets relative to where they start. */
134 819 static int64_t wav_seek_tag(WAVDemuxContext * wav, AVIOContext *s, int64_t offset, int whence)
135 {
136
3/4
✓ Branch 0 taken 819 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 817 times.
819 offset += offset < INT64_MAX && offset + wav->unaligned & 1;
137
138 819 return avio_seek(s, offset, whence);
139 }
140
141 /* return the size of the found tag */
142 217 static int64_t find_tag(WAVDemuxContext * wav, AVIOContext *pb, uint32_t tag1)
143 {
144 unsigned int tag;
145 int64_t size;
146
147 for (;;) {
148
2/2
✓ Branch 1 taken 217 times.
✓ Branch 2 taken 206 times.
423 if (avio_feof(pb))
149 217 return AVERROR_EOF;
150 206 size = next_tag(pb, &tag, wav->rifx);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 206 times.
206 if (tag == tag1)
152 break;
153 206 wav_seek_tag(wav, pb, size, SEEK_CUR);
154 }
155 return size;
156 }
157
158 7186 static int wav_probe(const AVProbeData *p)
159 {
160 /* check file header */
161
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7185 times.
7186 if (p->buf_size <= 32)
162 1 return 0;
163
2/2
✓ Branch 0 taken 473 times.
✓ Branch 1 taken 6712 times.
7185 if (!memcmp(p->buf + 8, "WAVE", 4)) {
164
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 473 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
473 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 473 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;
173 }
174 6712 return 0;
175 }
176
177 549 static void handle_stream_probing(AVStream *st)
178 {
179
2/2
✓ Branch 0 taken 496 times.
✓ Branch 1 taken 53 times.
549 if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE) {
180 496 FFStream *const sti = ffstream(st);
181 496 sti->request_probe = AVPROBE_SCORE_EXTENSION;
182 496 sti->probe_packets = FFMIN(sti->probe_packets, 32);
183 }
184 549 }
185
186 547 static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream *st)
187 {
188 547 AVIOContext *pb = s->pb;
189 547 WAVDemuxContext *wav = s->priv_data;
190 int ret;
191
192 /* parse fmt header */
193 547 ret = ff_get_wav_header(s, pb, st->codecpar, size, wav->rifx);
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
547 if (ret < 0)
195 return ret;
196 547 handle_stream_probing(st);
197
198 547 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
199
200 547 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
201
202 547 return 0;
203 }
204
205 static int wav_parse_xma2_tag(AVFormatContext *s, int64_t size, AVStream *st)
206 {
207 AVIOContext *pb = s->pb;
208 int version, num_streams, i, channels = 0, ret;
209
210 if (size < 36)
211 return AVERROR_INVALIDDATA;
212
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;
216
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);
230
231 for (i = 0; i < num_streams; i++) {
232 channels += avio_r8(pb);
233 avio_skip(pb, 3);
234 }
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;
238
239 if (st->codecpar->ch_layout.nb_channels <= 0 || st->codecpar->sample_rate <= 0)
240 return AVERROR_INVALIDDATA;
241
242 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
243
244 avio_seek(pb, -size, SEEK_CUR);
245 if ((ret = ff_get_extradata(s, st->codecpar, pb, size)) < 0)
246 return ret;
247
248 return 0;
249 }
250
251 15 static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
252 int length)
253 {
254 char temp[257];
255 int ret;
256
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 av_assert0(length < sizeof(temp));
258
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if ((ret = ffio_read_size(s->pb, temp, length)) < 0)
259 return ret;
260
261 15 temp[length] = 0;
262
263
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 6 times.
15 if (strlen(temp))
264 9 return av_dict_set(&s->metadata, key, temp, 0);
265
266 6 return 0;
267 }
268
269 3 static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
270 {
271 char temp[131], *coding_history;
272 int ret, x;
273 uint64_t time_reference;
274 3 int64_t umid_parts[8], umid_mask = 0;
275
276
2/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
6 if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
277
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
278
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
6 (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
279
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
6 (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
280 3 (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
281 return ret;
282
283 3 time_reference = avio_rl64(s->pb);
284 3 snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
285
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
286 return ret;
287
288 /* check if version is >= 1, in which case an UMID may be present */
289
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (avio_rl16(s->pb) >= 1) {
290 for (x = 0; x < 8; x++)
291 umid_mask |= umid_parts[x] = avio_rb64(s->pb);
292
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]);
311 }
312
313 if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
314 return ret;
315 }
316
317 avio_skip(s->pb, 190);
318 } else
319 3 avio_skip(s->pb, 254);
320
321
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (size > 602) {
322 /* CodingHistory present */
323 3 size -= 602;
324
325
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!(coding_history = av_malloc(size + 1)))
326 return AVERROR(ENOMEM);
327
328
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = ffio_read_size(s->pb, coding_history, size)) < 0) {
329 av_free(coding_history);
330 return ret;
331 }
332
333 3 coding_history[size] = 0;
334
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
335 AV_DICT_DONT_STRDUP_VAL)) < 0)
336 return ret;
337 }
338
339 3 return 0;
340 }
341
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 },
348 };
349
350 /* wav input */
351 547 static int wav_read_header(AVFormatContext *s)
352 {
353 547 int64_t size, av_uninit(data_size);
354 547 int64_t sample_count = 0;
355 547 int rf64 = 0, bw64 = 0;
356 uint32_t tag;
357 547 AVIOContext *pb = s->pb;
358 547 AVStream *st = NULL;
359 547 WAVDemuxContext *wav = s->priv_data;
360 547 int ret, got_fmt = 0, got_xma2 = 0;
361 547 int64_t next_tag_ofs, data_ofs = -1;
362
363 547 wav->unaligned = avio_tell(s->pb) & 1;
364
365 547 wav->smv_data_ofs = -1;
366
367 /* read chunk ID */
368 547 tag = avio_rl32(pb);
369
1/5
✓ Branch 0 taken 547 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
547 switch (tag) {
370 547 case MKTAG('R', 'I', 'F', 'F'):
371 547 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;
385 }
386
387 /* read chunk size */
388 547 avio_rl32(pb);
389
390 /* read format */
391
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 547 times.
547 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;
394 }
395
396
2/4
✓ Branch 0 taken 547 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 547 times.
547 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 */
403
404 data_size = avio_rl64(pb);
405 sample_count = avio_rl64(pb);
406
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;
412 }
413 avio_skip(pb, size - 24); /* skip rest of ds64 chunk */
414
415 }
416
417 /* Create the audio stream now so that its index is always zero */
418 547 st = avformat_new_stream(s, NULL);
419
1/2
✓ Branch 0 taken 547 times.
✗ Branch 1 not taken.
547 if (!st)
420 return AVERROR(ENOMEM);
421
422 613 for (;;) {
423 AVStream *vst;
424 1160 size = next_tag(pb, &tag, wav->rifx);
425 1160 next_tag_ofs = avio_tell(pb) + size;
426
427
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1158 times.
1160 if (avio_feof(pb))
428 2 break;
429
430
8/10
✓ Branch 0 taken 547 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 547 times.
✓ Branch 3 taken 39 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 17 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 1 times.
1158 switch (tag) {
431 547 case MKTAG('f', 'm', 't', ' '):
432 /* only parse the first 'fmt ' tag found */
433
3/6
✓ Branch 0 taken 547 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 547 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 547 times.
547 if (!got_xma2 && !got_fmt && (ret = wav_parse_fmt_tag(s, size, st)) < 0) {
434 return ret;
435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
547 } else if (got_fmt)
436 av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
437
438 547 got_fmt = 1;
439 547 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");
446
447 got_xma2 = 1;
448 break;
449 547 case MKTAG('d', 'a', 't', 'a'):
450
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
547 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;
454 }
455
456
2/4
✓ Branch 0 taken 547 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 547 times.
547 if (rf64 || bw64) {
457 next_tag_ofs = wav->data_end = av_sat_add64(avio_tell(pb), data_size);
458
1/2
✓ Branch 0 taken 547 times.
✗ Branch 1 not taken.
547 } else if (size != 0xFFFFFFFF) {
459 547 data_size = size;
460
1/2
✓ Branch 0 taken 547 times.
✗ Branch 1 not taken.
547 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;
466 }
467
468 547 data_ofs = avio_tell(pb);
469
470 /* don't look for footer metadata if we can't seek or if we don't
471 * know where the data tag ends
472 */
473
3/8
✓ Branch 0 taken 547 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 547 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 547 times.
547 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL) || (!(rf64 && !bw64) && !size))
474 goto break_loop;
475 547 break;
476 39 case MKTAG('f', 'a', 'c', 't'):
477
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 if (!sample_count)
478
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 sample_count = (!wav->rifx ? avio_rl32(pb) : avio_rb32(pb));
479 39 break;
480 3 case MKTAG('b', 'e', 'x', 't'):
481
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = wav_parse_bext_tag(s, size)) < 0)
482 return ret;
483 3 break;
484 1 case MKTAG('S','M','V','0'):
485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!got_fmt) {
486 av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'SMV0' tag\n");
487 return AVERROR_INVALIDDATA;
488 }
489 // SMV file, a wav file with video appended.
490
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (size != MKTAG('0','2','0','0')) {
491 av_log(s, AV_LOG_ERROR, "Unknown SMV version found\n");
492 goto break_loop;
493 }
494 1 av_log(s, AV_LOG_DEBUG, "Found SMV data\n");
495 1 wav->smv_given_first = 0;
496 1 vst = avformat_new_stream(s, NULL);
497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!vst)
498 return AVERROR(ENOMEM);
499 1 wav->vst = vst;
500 1 avio_r8(pb);
501 1 vst->id = 1;
502 1 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
503 1 vst->codecpar->codec_id = AV_CODEC_ID_SMVJPEG;
504 1 vst->codecpar->width = avio_rl24(pb);
505 1 vst->codecpar->height = avio_rl24(pb);
506
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = ff_alloc_extradata(vst->codecpar, 4)) < 0) {
507 av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n");
508 return ret;
509 }
510 1 size = avio_rl24(pb);
511 1 wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
512 1 avio_rl24(pb);
513 1 wav->smv_block_size = avio_rl24(pb);
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!wav->smv_block_size)
515 return AVERROR_INVALIDDATA;
516 1 avpriv_set_pts_info(vst, 32, 1, avio_rl24(pb));
517 1 vst->duration = avio_rl24(pb);
518 1 avio_rl24(pb);
519 1 avio_rl24(pb);
520 1 wav->smv_frames_per_jpeg = avio_rl24(pb);
521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (wav->smv_frames_per_jpeg > 65536) {
522 av_log(s, AV_LOG_ERROR, "too many frames per jpeg\n");
523 return AVERROR_INVALIDDATA;
524 }
525 1 AV_WL32(vst->codecpar->extradata, wav->smv_frames_per_jpeg);
526 1 goto break_loop;
527 17 case MKTAG('L', 'I', 'S', 'T'):
528 case MKTAG('l', 'i', 's', 't'):
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (size < 4) {
530 av_log(s, AV_LOG_ERROR, "too short LIST tag\n");
531 return AVERROR_INVALIDDATA;
532 }
533
2/3
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
17 switch (avio_rl32(pb)) {
534 14 case MKTAG('I', 'N', 'F', 'O'):
535 14 ff_read_riff_info(s, size - 4);
536 14 break;
537 3 case MKTAG('a', 'd', 't', 'l'):
538
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (s->nb_chapters > 0) {
539
3/4
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
33 while (avio_tell(pb) < next_tag_ofs &&
540 15 !avio_feof(pb)) {
541 char cue_label[512];
542 unsigned id, sub_size;
543
544
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (avio_rl32(pb) != MKTAG('l', 'a', 'b', 'l'))
545 break;
546
547 15 sub_size = avio_rl32(pb);
548
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (sub_size < 5)
549 break;
550 15 id = avio_rl32(pb);
551 15 avio_get_str(pb, sub_size - 4, cue_label, sizeof(cue_label));
552 15 avio_skip(pb, avio_tell(pb) & 1);
553
554
1/2
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
45 for (int i = 0; i < s->nb_chapters; i++) {
555
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 30 times.
45 if (s->chapters[i]->id == id) {
556 15 av_dict_set(&s->chapters[i]->metadata, "title", cue_label, 0);
557 15 break;
558 }
559 }
560 }
561 }
562 3 break;
563 }
564 17 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);
573 }
574 ff_id3v2_free_extra_meta(&id3v2_extra_meta);
575 }
576 break;
577 3 case MKTAG('c', 'u', 'e', ' '):
578
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 if (size >= 4 && got_fmt && st->codecpar->sample_rate > 0) {
579 3 AVRational tb = {1, st->codecpar->sample_rate};
580 3 unsigned nb_cues = avio_rl32(pb);
581
582
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (size >= nb_cues * 24LL + 4LL) {
583
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3 times.
18 for (int i = 0; i < nb_cues; i++) {
584 15 unsigned offset, id = avio_rl32(pb);
585
586
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (avio_feof(pb))
587 return AVERROR_INVALIDDATA;
588
589 15 avio_skip(pb, 16);
590 15 offset = avio_rl32(pb);
591
592
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (!avpriv_new_chapter(s, id, tb, offset, AV_NOPTS_VALUE, NULL))
593 return AVERROR(ENOMEM);
594 }
595 }
596 }
597 3 break;
598 }
599
600 /* seek to next tag unless we know that we'll run into EOF */
601
4/6
✓ Branch 1 taken 1157 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 613 times.
✓ Branch 5 taken 544 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 613 times.
1770 if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
602 613 wav_seek_tag(wav, pb, next_tag_ofs, SEEK_SET) < 0) {
603 break;
604 }
605 }
606
607 544 break_loop:
608
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
547 if (!got_fmt && !got_xma2) {
609 av_log(s, AV_LOG_ERROR, "no 'fmt ' or 'XMA2' tag found\n");
610 return AVERROR_INVALIDDATA;
611 }
612
613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
547 if (data_ofs < 0) {
614 av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
615 return AVERROR_INVALIDDATA;
616 }
617
618 547 avio_seek(pb, data_ofs, SEEK_SET);
619
620
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
547 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;
623 }
624
625
2/4
✓ Branch 0 taken 547 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 547 times.
✗ Branch 3 not taken.
547 if ( st->codecpar->bit_rate > 0 && data_size > 0
626
1/2
✓ Branch 0 taken 547 times.
✗ Branch 1 not taken.
547 && st->codecpar->sample_rate > 0
627
4/4
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 509 times.
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 9 times.
547 && sample_count > 0 && st->codecpar->ch_layout.nb_channels > 1
628
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 && sample_count % st->codecpar->ch_layout.nb_channels == 0) {
629 29 if (fabs(8.0 * data_size * st->codecpar->ch_layout.nb_channels * st->codecpar->sample_rate /
630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 sample_count /st->codecpar->bit_rate - 1.0) < 0.3)
631 sample_count /= st->codecpar->ch_layout.nb_channels;
632 }
633
634
4/6
✓ Branch 0 taken 547 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 509 times.
✓ Branch 4 taken 38 times.
✗ Branch 5 not taken.
547 if (data_size > 0 && sample_count && st->codecpar->ch_layout.nb_channels &&
635
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 (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;
638 }
639
640 /* G.729 hack (for Ticket4577)
641 * FIXME: Come up with cleaner, more general solution */
642
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
547 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;
645 }
646
647
4/4
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 509 times.
✓ Branch 3 taken 22 times.
✓ Branch 4 taken 16 times.
547 if (!sample_count || av_get_exact_bits_per_sample(st->codecpar->codec_id) > 0)
648
1/2
✓ Branch 0 taken 531 times.
✗ Branch 1 not taken.
531 if ( st->codecpar->ch_layout.nb_channels
649
1/2
✓ Branch 0 taken 531 times.
✗ Branch 1 not taken.
531 && data_size
650
2/2
✓ Branch 1 taken 525 times.
✓ Branch 2 taken 6 times.
531 && av_get_bits_per_sample(st->codecpar->codec_id)
651
2/2
✓ Branch 1 taken 523 times.
✓ Branch 2 taken 2 times.
525 && wav->data_end <= avio_size(pb))
652 1046 sample_count = (data_size << 3)
653 523 /
654 523 (st->codecpar->ch_layout.nb_channels * (uint64_t)av_get_bits_per_sample(st->codecpar->codec_id));
655
656
2/2
✓ Branch 0 taken 539 times.
✓ Branch 1 taken 8 times.
547 if (sample_count)
657 539 st->duration = sample_count;
658
659
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 544 times.
547 if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S32LE &&
660
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 st->codecpar->block_align == st->codecpar->ch_layout.nb_channels * 4 &&
661
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 st->codecpar->bits_per_coded_sample == 32 &&
662
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 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
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 537 times.
547 } else if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE &&
667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 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
1/2
✓ Branch 0 taken 547 times.
✗ Branch 1 not taken.
547 } else if (st->codecpar->codec_id == AV_CODEC_ID_XMA1 ||
671
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
547 st->codecpar->codec_id == AV_CODEC_ID_XMA2) {
672 st->codecpar->block_align = 2048;
673
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 543 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
547 } 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;
676 }
677
678 547 ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
679 547 ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
680
681 547 set_spdif(s, wav);
682 547 set_max_size(st, wav);
683
684 547 return 0;
685 }
686
687 /**
688 * Find chunk with w64 GUID by skipping over other chunks.
689 * @return the size of the found chunk
690 */
691 6 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
692 {
693 uint8_t guid[16];
694 int64_t size;
695
696
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
6 while (!avio_feof(pb)) {
697 2 avio_read(pb, guid, 16);
698 2 size = avio_rl64(pb);
699
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (size <= 24 || size > INT64_MAX - 8)
700 2 return AVERROR_INVALIDDATA;
701 if (!memcmp(guid, guid1, 16))
702 return size;
703 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
704 }
705 4 return AVERROR_EOF;
706 }
707
708 34425 static int wav_read_packet(AVFormatContext *s, AVPacket *pkt)
709 {
710 int ret, size;
711 int64_t left;
712 34425 WAVDemuxContext *wav = s->priv_data;
713 34425 AVStream *st = s->streams[0];
714
715
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 34380 times.
34425 if (CONFIG_SPDIF_DEMUXER && wav->spdif == 1)
716 45 return ff_spdif_read_packet(s, pkt);
717
718
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 34312 times.
34380 if (wav->smv_data_ofs > 0) {
719 int64_t audio_dts, video_dts;
720 68 AVStream *vst = wav->vst;
721 68 smv_retry:
722 69 audio_dts = (int32_t)ffstream( st)->cur_dts;
723 69 video_dts = (int32_t)ffstream(vst)->cur_dts;
724
725
2/4
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 69 times.
✗ Branch 3 not taken.
69 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 138 wav->smv_last_stream = wav->smv_given_first ?
728 68 av_compare_ts(video_dts, vst->time_base,
729
4/4
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 65 times.
✓ Branch 3 taken 3 times.
137 audio_dts, st->time_base) > 0 : 0;
730 69 wav->smv_given_first = 1;
731 }
732 69 wav->smv_last_stream = !wav->smv_last_stream;
733 69 wav->smv_last_stream |= wav->audio_eof;
734 69 wav->smv_last_stream &= !wav->smv_eof;
735
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 66 times.
69 if (wav->smv_last_stream) {
736 3 uint64_t old_pos = avio_tell(s->pb);
737 3 uint64_t new_pos = wav->smv_data_ofs +
738 3 wav->smv_block * (int64_t)wav->smv_block_size;
739
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
740 ret = AVERROR_EOF;
741 goto smv_out;
742 }
743 3 size = avio_rl24(s->pb);
744
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (size > wav->smv_block_size) {
745 ret = AVERROR_EOF;
746 goto smv_out;
747 }
748 3 ret = av_get_packet(s->pb, pkt, size);
749
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (ret < 0)
750 1 goto smv_out;
751 2 pkt->pos -= 3;
752 2 pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg;
753 2 pkt->duration = wav->smv_frames_per_jpeg;
754 2 wav->smv_block++;
755
756 2 pkt->stream_index = vst->index;
757 3 smv_out:
758 3 avio_seek(s->pb, old_pos, SEEK_SET);
759
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (ret == AVERROR_EOF) {
760 1 wav->smv_eof = 1;
761 1 goto smv_retry;
762 }
763 2 return ret;
764 }
765 }
766
767 34378 left = wav->data_end - avio_tell(s->pb);
768
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34378 times.
34378 if (wav->ignore_length)
769 left = INT_MAX;
770
2/2
✓ Branch 0 taken 223 times.
✓ Branch 1 taken 34155 times.
34378 if (left <= 0) {
771
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 217 times.
223 if (CONFIG_W64_DEMUXER && wav->w64)
772 6 left = find_guid(s->pb, ff_w64_guid_data) - 24;
773 else
774 217 left = find_tag(wav, s->pb, MKTAG('d', 'a', 't', 'a'));
775
1/2
✓ Branch 0 taken 223 times.
✗ Branch 1 not taken.
223 if (left < 0) {
776 223 wav->audio_eof = 1;
777
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 222 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
223 if (wav->smv_data_ofs > 0 && !wav->smv_eof)
778 goto smv_retry;
779 223 return AVERROR_EOF;
780 }
781 if (INT64_MAX - left < avio_tell(s->pb))
782 return AVERROR_INVALIDDATA;
783 wav->data_end = avio_tell(s->pb) + left;
784 }
785
786 34155 size = wav->max_size;
787
2/2
✓ Branch 0 taken 33671 times.
✓ Branch 1 taken 484 times.
34155 if (st->codecpar->block_align > 1) {
788
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33671 times.
33671 if (size < st->codecpar->block_align)
789 size = st->codecpar->block_align;
790 33671 size = (size / st->codecpar->block_align) * st->codecpar->block_align;
791 }
792 34155 size = FFMIN(size, left);
793 34155 ret = av_get_packet(s->pb, pkt, size);
794
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 34146 times.
34155 if (ret < 0)
795 9 return ret;
796 34146 pkt->stream_index = 0;
797
798 34146 return ret;
799 }
800
801 404 static int wav_read_seek(AVFormatContext *s,
802 int stream_index, int64_t timestamp, int flags)
803 {
804 404 WAVDemuxContext *wav = s->priv_data;
805 404 AVStream *ast = s->streams[0], *vst = wav->vst;
806 404 wav->smv_eof = 0;
807 404 wav->audio_eof = 0;
808
809
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 404 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
404 if (stream_index != 0 && (!vst || stream_index != vst->index))
810 return AVERROR(EINVAL);
811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 404 times.
404 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;
819 }
820 }
821
822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 404 times.
404 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 404 default:
831 404 break;
832 }
833 404 return ff_pcm_read_seek(s, 0, timestamp, flags);
834 }
835
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,
841 };
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,
853 };
854 #endif /* CONFIG_WAV_DEMUXER */
855
856 #if CONFIG_W64_DEMUXER
857 7186 static int w64_probe(const AVProbeData *p)
858 {
859
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7184 times.
7186 if (p->buf_size <= 40)
860 2 return 0;
861
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7182 times.
7184 if (!memcmp(p->buf, ff_w64_guid_riff, 16) &&
862
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 !memcmp(p->buf + 24, ff_w64_guid_wave, 16))
863 2 return AVPROBE_SCORE_MAX;
864 else
865 7182 return 0;
866 }
867
868 2 static int w64_read_header(AVFormatContext *s)
869 {
870 2 int64_t size, data_ofs = 0;
871 2 AVIOContext *pb = s->pb;
872 2 WAVDemuxContext *wav = s->priv_data;
873 AVStream *st;
874 uint8_t guid[16];
875 int ret;
876
877
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 if (avio_read(pb, guid, 16) != 16 || memcmp(guid, ff_w64_guid_riff, 16))
878 return AVERROR_INVALIDDATA;
879
880 /* riff + wave + fmt + sizes */
881
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8)
882 return AVERROR_INVALIDDATA;
883
884 2 avio_read(pb, guid, 16);
885
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 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;
888 }
889
890 2 wav->w64 = 1;
891
892 2 st = avformat_new_stream(s, NULL);
893
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!st)
894 return AVERROR(ENOMEM);
895
896
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 while (!avio_feof(pb)) {
897
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
6 if (avio_read(pb, guid, 16) != 16)
898 2 break;
899 4 size = avio_rl64(pb);
900
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
4 if (size <= 24 || INT64_MAX - size < avio_tell(pb)) {
901 if (data_ofs)
902 break;
903 return AVERROR_INVALIDDATA;
904 }
905
906
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (!memcmp(guid, ff_w64_guid_fmt, 16)) {
907 /* subtract chunk header size - normal wav file doesn't count it */
908 2 ret = ff_get_wav_header(s, pb, st->codecpar, size - 24, 0);
909
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
910 return ret;
911 2 avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
912
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (st->codecpar->block_align &&
913
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 st->codecpar->ch_layout.nb_channels < FF_SANE_NB_CHANNELS &&
914
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 st->codecpar->bits_per_coded_sample < 128) {
915 2 int block_align = st->codecpar->block_align;
916
917 2 block_align = FFMAX(block_align,
918 ((st->codecpar->bits_per_coded_sample + 7) / 8) *
919 st->codecpar->ch_layout.nb_channels);
920
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 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;
924 }
925 }
926 2 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
927
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } else if (!memcmp(guid, ff_w64_guid_fact, 16)) {
928 int64_t samples;
929
930 samples = avio_rl64(pb);
931 if (samples > 0)
932 st->duration = samples;
933 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 32);
934
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (!memcmp(guid, ff_w64_guid_data, 16)) {
935 2 wav->data_end = avio_tell(pb) + size - 24;
936
937 2 data_ofs = avio_tell(pb);
938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL))
939 break;
940
941 2 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);
946
947 start = avio_tell(pb);
948 end = start + FFALIGN(size, INT64_C(8)) - 24;
949 count = avio_rl32(pb);
950
951 for (i = 0; i < count; i++) {
952 char chunk_key[5], *value;
953
954 if (avio_feof(pb) || (cur = avio_tell(pb)) < 0 || cur > end - 8 /* = tag + size */)
955 break;
956
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;
962
963 value = av_malloc(chunk_size + 1);
964 if (!value)
965 return AVERROR(ENOMEM);
966
967 ret = avio_get_str16le(pb, chunk_size, value, chunk_size);
968 if (ret < 0) {
969 av_free(value);
970 return ret;
971 }
972 avio_skip(pb, chunk_size - ret);
973
974 av_dict_set(&s->metadata, chunk_key, value, AV_DICT_DONT_STRDUP_VAL);
975 }
976
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);
981 }
982 }
983
984
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!data_ofs)
985 return AVERROR_EOF;
986
987 2 ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
988 2 ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
989
990 2 handle_stream_probing(st);
991 2 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
992
993 2 avio_seek(pb, data_ofs, SEEK_SET);
994
995 2 set_spdif(s, wav);
996 2 set_max_size(st, wav);
997
998 2 return 0;
999 }
1000
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,
1006 };
1007
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,
1019 };
1020 #endif /* CONFIG_W64_DEMUXER */
1021