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