Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / ffmpeg / libavformat / wavdec.c
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  * This file is part of FFmpeg.
10  *
11  * FFmpeg is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * FFmpeg is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with FFmpeg; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25
26 #include <stdint.h>
27
28 #include "libavutil/avassert.h"
29 #include "libavutil/dict.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/log.h"
32 #include "libavutil/mathematics.h"
33 #include "libavutil/opt.h"
34 #include "avformat.h"
35 #include "avio.h"
36 #include "avio_internal.h"
37 #include "internal.h"
38 #include "metadata.h"
39 #include "pcm.h"
40 #include "riff.h"
41 #include "w64.h"
42 #include "spdif.h"
43
44 typedef struct WAVDemuxContext {
45     const AVClass *class;
46     int64_t data_end;
47     int w64;
48     int64_t smv_data_ofs;
49     int smv_block_size;
50     int smv_frames_per_jpeg;
51     int smv_block;
52     int smv_last_stream;
53     int smv_eof;
54     int audio_eof;
55     int ignore_length;
56     int spdif;
57     int smv_cur_pt;
58     int smv_given_first;
59     int unaligned; // e.g. if an odd number of bytes ID3 tag was prepended
60 } WAVDemuxContext;
61
62 #if CONFIG_WAV_DEMUXER
63
64 static int64_t next_tag(AVIOContext *pb, uint32_t *tag)
65 {
66     *tag = avio_rl32(pb);
67     return avio_rl32(pb);
68 }
69
70 /* RIFF chunks are always at even offsets relative to where they start. */
71 static int64_t wav_seek_tag(WAVDemuxContext * wav, AVIOContext *s, int64_t offset, int whence)
72 {
73     offset += offset < INT64_MAX && offset + wav->unaligned & 1;
74
75     return avio_seek(s, offset, whence);
76 }
77
78 /* return the size of the found tag */
79 static int64_t find_tag(WAVDemuxContext * wav, AVIOContext *pb, uint32_t tag1)
80 {
81     unsigned int tag;
82     int64_t size;
83
84     for (;;) {
85         if (url_feof(pb))
86             return AVERROR_EOF;
87         size = next_tag(pb, &tag);
88         if (tag == tag1)
89             break;
90         wav_seek_tag(wav, pb, size, SEEK_CUR);
91     }
92     return size;
93 }
94
95 static int wav_probe(AVProbeData *p)
96 {
97     /* check file header */
98     if (p->buf_size <= 32)
99         return 0;
100     if (!memcmp(p->buf + 8, "WAVE", 4)) {
101         if (!memcmp(p->buf, "RIFF", 4))
102             /* Since the ACT demuxer has a standard WAV header at the top of
103              * its own, the returned score is decreased to avoid a probe
104              * conflict between ACT and WAV. */
105             return AVPROBE_SCORE_MAX - 1;
106         else if (!memcmp(p->buf,      "RF64", 4) &&
107                  !memcmp(p->buf + 12, "ds64", 4))
108             return AVPROBE_SCORE_MAX;
109     }
110     return 0;
111 }
112
113 static void handle_stream_probing(AVStream *st)
114 {
115     if (st->codec->codec_id == AV_CODEC_ID_PCM_S16LE) {
116         st->request_probe = AVPROBE_SCORE_EXTENSION;
117         st->probe_packets = FFMIN(st->probe_packets, 14);
118     }
119 }
120
121 static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
122 {
123     AVIOContext *pb = s->pb;
124     int ret;
125
126     /* parse fmt header */
127     *st = avformat_new_stream(s, NULL);
128     if (!*st)
129         return AVERROR(ENOMEM);
130
131     ret = ff_get_wav_header(pb, (*st)->codec, size);
132     if (ret < 0)
133         return ret;
134     handle_stream_probing(*st);
135
136     (*st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
137
138     avpriv_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
139
140     return 0;
141 }
142
143 static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
144                                         int length)
145 {
146     char temp[257];
147     int ret;
148
149     av_assert0(length <= sizeof(temp));
150     if ((ret = avio_read(s->pb, temp, length)) < 0)
151         return ret;
152
153     temp[length] = 0;
154
155     if (strlen(temp))
156         return av_dict_set(&s->metadata, key, temp, 0);
157
158     return 0;
159 }
160
161 static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
162 {
163     char temp[131], *coding_history;
164     int ret, x;
165     uint64_t time_reference;
166     int64_t umid_parts[8], umid_mask = 0;
167
168     if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
169         (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
170         (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
171         (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
172         (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
173         return ret;
174
175     time_reference = avio_rl64(s->pb);
176     snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
177     if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
178         return ret;
179
180     /* check if version is >= 1, in which case an UMID may be present */
181     if (avio_rl16(s->pb) >= 1) {
182         for (x = 0; x < 8; x++)
183             umid_mask |= umid_parts[x] = avio_rb64(s->pb);
184
185         if (umid_mask) {
186             /* the string formatting below is per SMPTE 330M-2004 Annex C */
187             if (umid_parts[4] == 0 && umid_parts[5] == 0 &&
188                 umid_parts[6] == 0 && umid_parts[7] == 0) {
189                 /* basic UMID */
190                 snprintf(temp, sizeof(temp),
191                          "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
192                          umid_parts[0], umid_parts[1],
193                          umid_parts[2], umid_parts[3]);
194             } else {
195                 /* extended UMID */
196                 snprintf(temp, sizeof(temp),
197                          "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
198                          "%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
199                          umid_parts[0], umid_parts[1],
200                          umid_parts[2], umid_parts[3],
201                          umid_parts[4], umid_parts[5],
202                          umid_parts[6], umid_parts[7]);
203             }
204
205             if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
206                 return ret;
207         }
208
209         avio_skip(s->pb, 190);
210     } else
211         avio_skip(s->pb, 254);
212
213     if (size > 602) {
214         /* CodingHistory present */
215         size -= 602;
216
217         if (!(coding_history = av_malloc(size + 1)))
218             return AVERROR(ENOMEM);
219
220         if ((ret = avio_read(s->pb, coding_history, size)) < 0)
221             return ret;
222
223         coding_history[size] = 0;
224         if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
225                                AV_DICT_DONT_STRDUP_VAL)) < 0)
226             return ret;
227     }
228
229     return 0;
230 }
231
232 static const AVMetadataConv wav_metadata_conv[] = {
233     { "description",      "comment"       },
234     { "originator",       "encoded_by"    },
235     { "origination_date", "date"          },
236     { "origination_time", "creation_time" },
237     { 0 },
238 };
239
240 /* wav input */
241 static int wav_read_header(AVFormatContext *s)
242 {
243     int64_t size, av_uninit(data_size);
244     int64_t sample_count = 0;
245     int rf64;
246     uint32_t tag;
247     AVIOContext *pb      = s->pb;
248     AVStream *st         = NULL;
249     WAVDemuxContext *wav = s->priv_data;
250     int ret, got_fmt = 0;
251     int64_t next_tag_ofs, data_ofs = -1;
252
253     wav->unaligned = avio_tell(s->pb) & 1;
254
255     wav->smv_data_ofs = -1;
256
257     /* check RIFF header */
258     tag = avio_rl32(pb);
259
260     rf64 = tag == MKTAG('R', 'F', '6', '4');
261     if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
262         return AVERROR_INVALIDDATA;
263     avio_rl32(pb); /* file size */
264     tag = avio_rl32(pb);
265     if (tag != MKTAG('W', 'A', 'V', 'E'))
266         return AVERROR_INVALIDDATA;
267
268     if (rf64) {
269         if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
270             return AVERROR_INVALIDDATA;
271         size = avio_rl32(pb);
272         if (size < 24)
273             return AVERROR_INVALIDDATA;
274         avio_rl64(pb); /* RIFF size */
275
276         data_size    = avio_rl64(pb);
277         sample_count = avio_rl64(pb);
278
279         if (data_size < 0 || sample_count < 0) {
280             av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
281                    "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
282                    data_size, sample_count);
283             return AVERROR_INVALIDDATA;
284         }
285         avio_skip(pb, size - 24); /* skip rest of ds64 chunk */
286
287     }
288
289     for (;;) {
290         AVStream *vst;
291         size         = next_tag(pb, &tag);
292         next_tag_ofs = avio_tell(pb) + size;
293
294         if (url_feof(pb))
295             break;
296
297         switch (tag) {
298         case MKTAG('f', 'm', 't', ' '):
299             /* only parse the first 'fmt ' tag found */
300             if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st)) < 0) {
301                 return ret;
302             } else if (got_fmt)
303                 av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
304
305             got_fmt = 1;
306             break;
307         case MKTAG('d', 'a', 't', 'a'):
308             if (!got_fmt) {
309                 av_log(s, AV_LOG_ERROR,
310                        "found no 'fmt ' tag before the 'data' tag\n");
311                 return AVERROR_INVALIDDATA;
312             }
313
314             if (rf64) {
315                 next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
316             } else {
317                 data_size    = size;
318                 next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
319             }
320
321             data_ofs = avio_tell(pb);
322
323             /* don't look for footer metadata if we can't seek or if we don't
324              * know where the data tag ends
325              */
326             if (!pb->seekable || (!rf64 && !size))
327                 goto break_loop;
328             break;
329         case MKTAG('f', 'a', 'c', 't'):
330             if (!sample_count)
331                 sample_count = avio_rl32(pb);
332             break;
333         case MKTAG('b', 'e', 'x', 't'):
334             if ((ret = wav_parse_bext_tag(s, size)) < 0)
335                 return ret;
336             break;
337         case MKTAG('S','M','V','0'):
338             if (!got_fmt) {
339                 av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'SMV0' tag\n");
340                 return AVERROR_INVALIDDATA;
341             }
342             // SMV file, a wav file with video appended.
343             if (size != MKTAG('0','2','0','0')) {
344                 av_log(s, AV_LOG_ERROR, "Unknown SMV version found\n");
345                 goto break_loop;
346             }
347             av_log(s, AV_LOG_DEBUG, "Found SMV data\n");
348             wav->smv_given_first = 0;
349             vst = avformat_new_stream(s, NULL);
350             if (!vst)
351                 return AVERROR(ENOMEM);
352             avio_r8(pb);
353             vst->id = 1;
354             vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
355             vst->codec->codec_id = AV_CODEC_ID_SMVJPEG;
356             vst->codec->width  = avio_rl24(pb);
357             vst->codec->height = avio_rl24(pb);
358             if (ff_alloc_extradata(vst->codec, 4)) {
359                 av_log(s, AV_LOG_ERROR, "Could not allocate extradata.\n");
360                 return AVERROR(ENOMEM);
361             }
362             size = avio_rl24(pb);
363             wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
364             avio_rl24(pb);
365             wav->smv_block_size = avio_rl24(pb);
366             avpriv_set_pts_info(vst, 32, 1, avio_rl24(pb));
367             vst->duration = avio_rl24(pb);
368             avio_rl24(pb);
369             avio_rl24(pb);
370             wav->smv_frames_per_jpeg = avio_rl24(pb);
371             if (wav->smv_frames_per_jpeg > 65536) {
372                 av_log(s, AV_LOG_ERROR, "too many frames per jpeg\n");
373                 return AVERROR_INVALIDDATA;
374             }
375             AV_WL32(vst->codec->extradata, wav->smv_frames_per_jpeg);
376             wav->smv_cur_pt = 0;
377             goto break_loop;
378         case MKTAG('L', 'I', 'S', 'T'):
379             if (size < 4) {
380                 av_log(s, AV_LOG_ERROR, "too short LIST tag\n");
381                 return AVERROR_INVALIDDATA;
382             }
383             switch (avio_rl32(pb)) {
384             case MKTAG('I', 'N', 'F', 'O'):
385                 ff_read_riff_info(s, size - 4);
386             }
387             break;
388         }
389
390         /* seek to next tag unless we know that we'll run into EOF */
391         if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
392             wav_seek_tag(wav, pb, next_tag_ofs, SEEK_SET) < 0) {
393             break;
394         }
395     }
396
397 break_loop:
398     if (data_ofs < 0) {
399         av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
400         return AVERROR_INVALIDDATA;
401     }
402
403     avio_seek(pb, data_ofs, SEEK_SET);
404
405     if (data_size > 0 && sample_count && data_size / sample_count / st->codec->channels > 8) {
406         av_log(s, AV_LOG_WARNING, "ignoring wrong sample_count %"PRId64"\n", sample_count);
407         sample_count = 0;
408     }
409
410     if (!sample_count || av_get_exact_bits_per_sample(st->codec->codec_id) > 0)
411         if (   st->codec->channels
412             && data_size
413             && av_get_bits_per_sample(st->codec->codec_id)
414             && wav->data_end <= avio_size(pb))
415             sample_count = (data_size << 3)
416                                   /
417                 (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
418
419     if (sample_count)
420         st->duration = sample_count;
421
422     ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
423     ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
424
425     return 0;
426 }
427
428 /**
429  * Find chunk with w64 GUID by skipping over other chunks.
430  * @return the size of the found chunk
431  */
432 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
433 {
434     uint8_t guid[16];
435     int64_t size;
436
437     while (!url_feof(pb)) {
438         avio_read(pb, guid, 16);
439         size = avio_rl64(pb);
440         if (size <= 24)
441             return AVERROR_INVALIDDATA;
442         if (!memcmp(guid, guid1, 16))
443             return size;
444         avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
445     }
446     return AVERROR_EOF;
447 }
448
449 #define MAX_SIZE 4096
450
451 static int wav_read_packet(AVFormatContext *s, AVPacket *pkt)
452 {
453     int ret, size;
454     int64_t left;
455     AVStream *st;
456     WAVDemuxContext *wav = s->priv_data;
457
458     if (CONFIG_SPDIF_DEMUXER && wav->spdif == 0 &&
459         s->streams[0]->codec->codec_tag == 1) {
460         enum AVCodecID codec;
461         ret = ff_spdif_probe(s->pb->buffer, s->pb->buf_end - s->pb->buffer,
462                              &codec);
463         if (ret > AVPROBE_SCORE_EXTENSION) {
464             s->streams[0]->codec->codec_id = codec;
465             wav->spdif = 1;
466         } else {
467             wav->spdif = -1;
468         }
469     }
470     if (CONFIG_SPDIF_DEMUXER && wav->spdif == 1)
471         return ff_spdif_read_packet(s, pkt);
472
473     if (wav->smv_data_ofs > 0) {
474         int64_t audio_dts, video_dts;
475 smv_retry:
476         audio_dts = (int32_t)s->streams[0]->cur_dts;
477         video_dts = (int32_t)s->streams[1]->cur_dts;
478
479         if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
480             /*We always return a video frame first to get the pixel format first*/
481             wav->smv_last_stream = wav->smv_given_first ?
482                 av_compare_ts(video_dts, s->streams[1]->time_base,
483                               audio_dts, s->streams[0]->time_base) > 0 : 0;
484             wav->smv_given_first = 1;
485         }
486         wav->smv_last_stream = !wav->smv_last_stream;
487         wav->smv_last_stream |= wav->audio_eof;
488         wav->smv_last_stream &= !wav->smv_eof;
489         if (wav->smv_last_stream) {
490             uint64_t old_pos = avio_tell(s->pb);
491             uint64_t new_pos = wav->smv_data_ofs +
492                 wav->smv_block * wav->smv_block_size;
493             if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
494                 ret = AVERROR_EOF;
495                 goto smv_out;
496             }
497             size = avio_rl24(s->pb);
498             ret  = av_get_packet(s->pb, pkt, size);
499             if (ret < 0)
500                 goto smv_out;
501             pkt->pos -= 3;
502             pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt;
503             wav->smv_cur_pt++;
504             if (wav->smv_frames_per_jpeg > 0)
505                 wav->smv_cur_pt %= wav->smv_frames_per_jpeg;
506             if (!wav->smv_cur_pt)
507                 wav->smv_block++;
508
509             pkt->stream_index = 1;
510 smv_out:
511             avio_seek(s->pb, old_pos, SEEK_SET);
512             if (ret == AVERROR_EOF) {
513                 wav->smv_eof = 1;
514                 goto smv_retry;
515             }
516             return ret;
517         }
518     }
519
520     st = s->streams[0];
521
522     left = wav->data_end - avio_tell(s->pb);
523     if (wav->ignore_length)
524         left = INT_MAX;
525     if (left <= 0) {
526         if (CONFIG_W64_DEMUXER && wav->w64)
527             left = find_guid(s->pb, ff_w64_guid_data) - 24;
528         else
529             left = find_tag(wav, s->pb, MKTAG('d', 'a', 't', 'a'));
530         if (left < 0) {
531             wav->audio_eof = 1;
532             if (wav->smv_data_ofs > 0 && !wav->smv_eof)
533                 goto smv_retry;
534             return AVERROR_EOF;
535         }
536         wav->data_end = avio_tell(s->pb) + left;
537     }
538
539     size = MAX_SIZE;
540     if (st->codec->block_align > 1) {
541         if (size < st->codec->block_align)
542             size = st->codec->block_align;
543         size = (size / st->codec->block_align) * st->codec->block_align;
544     }
545     size = FFMIN(size, left);
546     ret  = av_get_packet(s->pb, pkt, size);
547     if (ret < 0)
548         return ret;
549     pkt->stream_index = 0;
550
551     return ret;
552 }
553
554 static int wav_read_seek(AVFormatContext *s,
555                          int stream_index, int64_t timestamp, int flags)
556 {
557     WAVDemuxContext *wav = s->priv_data;
558     AVStream *st;
559     wav->smv_eof = 0;
560     wav->audio_eof = 0;
561     if (wav->smv_data_ofs > 0) {
562         int64_t smv_timestamp = timestamp;
563         if (stream_index == 0)
564             smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
565         else
566             timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
567         if (wav->smv_frames_per_jpeg > 0) {
568             wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
569             wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg;
570         }
571     }
572
573     st = s->streams[0];
574     switch (st->codec->codec_id) {
575     case AV_CODEC_ID_MP2:
576     case AV_CODEC_ID_MP3:
577     case AV_CODEC_ID_AC3:
578     case AV_CODEC_ID_DTS:
579         /* use generic seeking with dynamically generated indexes */
580         return -1;
581     default:
582         break;
583     }
584     return ff_pcm_read_seek(s, stream_index, timestamp, flags);
585 }
586
587 #define OFFSET(x) offsetof(WAVDemuxContext, x)
588 #define DEC AV_OPT_FLAG_DECODING_PARAM
589 static const AVOption demux_options[] = {
590     { "ignore_length", "Ignore length", OFFSET(ignore_length), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
591     { NULL },
592 };
593
594 static const AVClass wav_demuxer_class = {
595     .class_name = "WAV demuxer",
596     .item_name  = av_default_item_name,
597     .option     = demux_options,
598     .version    = LIBAVUTIL_VERSION_INT,
599 };
600 AVInputFormat ff_wav_demuxer = {
601     .name           = "wav",
602     .long_name      = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
603     .priv_data_size = sizeof(WAVDemuxContext),
604     .read_probe     = wav_probe,
605     .read_header    = wav_read_header,
606     .read_packet    = wav_read_packet,
607     .read_seek      = wav_read_seek,
608     .flags          = AVFMT_GENERIC_INDEX,
609     .codec_tag      = (const AVCodecTag * const []) { ff_codec_wav_tags,  0 },
610     .priv_class     = &wav_demuxer_class,
611 };
612 #endif /* CONFIG_WAV_DEMUXER */
613
614 #if CONFIG_W64_DEMUXER
615 static int w64_probe(AVProbeData *p)
616 {
617     if (p->buf_size <= 40)
618         return 0;
619     if (!memcmp(p->buf,      ff_w64_guid_riff, 16) &&
620         !memcmp(p->buf + 24, ff_w64_guid_wave, 16))
621         return AVPROBE_SCORE_MAX;
622     else
623         return 0;
624 }
625
626 static int w64_read_header(AVFormatContext *s)
627 {
628     int64_t size, data_ofs = 0;
629     AVIOContext *pb      = s->pb;
630     WAVDemuxContext *wav = s->priv_data;
631     AVStream *st;
632     uint8_t guid[16];
633     int ret;
634
635     avio_read(pb, guid, 16);
636     if (memcmp(guid, ff_w64_guid_riff, 16))
637         return AVERROR_INVALIDDATA;
638
639     /* riff + wave + fmt + sizes */
640     if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8)
641         return AVERROR_INVALIDDATA;
642
643     avio_read(pb, guid, 16);
644     if (memcmp(guid, ff_w64_guid_wave, 16)) {
645         av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
646         return AVERROR_INVALIDDATA;
647     }
648
649     wav->w64 = 1;
650
651     st = avformat_new_stream(s, NULL);
652     if (!st)
653         return AVERROR(ENOMEM);
654
655     while (!url_feof(pb)) {
656         if (avio_read(pb, guid, 16) != 16)
657             break;
658         size = avio_rl64(pb);
659         if (size <= 24 || INT64_MAX - size < avio_tell(pb))
660             return AVERROR_INVALIDDATA;
661
662         if (!memcmp(guid, ff_w64_guid_fmt, 16)) {
663             /* subtract chunk header size - normal wav file doesn't count it */
664             ret = ff_get_wav_header(pb, st->codec, size - 24);
665             if (ret < 0)
666                 return ret;
667             avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
668
669             avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
670         } else if (!memcmp(guid, ff_w64_guid_fact, 16)) {
671             int64_t samples;
672
673             samples = avio_rl64(pb);
674             if (samples > 0)
675                 st->duration = samples;
676         } else if (!memcmp(guid, ff_w64_guid_data, 16)) {
677             wav->data_end = avio_tell(pb) + size - 24;
678
679             data_ofs = avio_tell(pb);
680             if (!pb->seekable)
681                 break;
682
683             avio_skip(pb, size - 24);
684         } else if (!memcmp(guid, ff_w64_guid_summarylist, 16)) {
685             int64_t start, end, cur;
686             uint32_t count, chunk_size, i;
687
688             start = avio_tell(pb);
689             end = start + FFALIGN(size, INT64_C(8)) - 24;
690             count = avio_rl32(pb);
691
692             for (i = 0; i < count; i++) {
693                 char chunk_key[5], *value;
694
695                 if (url_feof(pb) || (cur = avio_tell(pb)) < 0 || cur > end - 8 /* = tag + size */)
696                     break;
697
698                 chunk_key[4] = 0;
699                 avio_read(pb, chunk_key, 4);
700                 chunk_size = avio_rl32(pb);
701
702                 value = av_mallocz(chunk_size + 1);
703                 if (!value)
704                     return AVERROR(ENOMEM);
705
706                 ret = avio_get_str16le(pb, chunk_size, value, chunk_size);
707                 avio_skip(pb, chunk_size - ret);
708
709                 av_dict_set(&s->metadata, chunk_key, value, AV_DICT_DONT_STRDUP_VAL);
710             }
711
712             avio_skip(pb, end - avio_tell(pb));
713         } else {
714             av_log(s, AV_LOG_DEBUG, "unknown guid: "FF_PRI_GUID"\n", FF_ARG_GUID(guid));
715             avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
716         }
717     }
718
719     if (!data_ofs)
720         return AVERROR_EOF;
721
722     ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
723     ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
724
725     handle_stream_probing(st);
726     st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
727
728     avio_seek(pb, data_ofs, SEEK_SET);
729
730     return 0;
731 }
732
733 AVInputFormat ff_w64_demuxer = {
734     .name           = "w64",
735     .long_name      = NULL_IF_CONFIG_SMALL("Sony Wave64"),
736     .priv_data_size = sizeof(WAVDemuxContext),
737     .read_probe     = w64_probe,
738     .read_header    = w64_read_header,
739     .read_packet    = wav_read_packet,
740     .read_seek      = wav_read_seek,
741     .flags          = AVFMT_GENERIC_INDEX,
742     .codec_tag      = (const AVCodecTag * const []) { ff_codec_wav_tags, 0 },
743 };
744 #endif /* CONFIG_W64_DEMUXER */