FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
wavdec.c
Go to the documentation of this file.
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;
51  int smv_block;
53  int smv_eof;
54  int audio_eof;
56  int spdif;
59  int unaligned; // e.g. if an odd number of bytes ID3 tag was prepended
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) {
117  st->probe_packets = FFMIN(st->probe_packets, 4);
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,
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;
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  }
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 (!sample_count || av_get_exact_bits_per_sample(st->codec->codec_id) > 0)
406  if ( st->codec->channels
407  && data_size
409  && wav->data_end <= avio_size(pb))
410  sample_count = (data_size << 3)
411  /
412  (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
413 
414  if (sample_count)
415  st->duration = sample_count;
416 
417  ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
419 
420  return 0;
421 }
422 
423 /**
424  * Find chunk with w64 GUID by skipping over other chunks.
425  * @return the size of the found chunk
426  */
427 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
428 {
429  uint8_t guid[16];
430  int64_t size;
431 
432  while (!url_feof(pb)) {
433  avio_read(pb, guid, 16);
434  size = avio_rl64(pb);
435  if (size <= 24)
436  return AVERROR_INVALIDDATA;
437  if (!memcmp(guid, guid1, 16))
438  return size;
439  avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
440  }
441  return AVERROR_EOF;
442 }
443 
444 #define MAX_SIZE 4096
445 
446 static int wav_read_packet(AVFormatContext *s, AVPacket *pkt)
447 {
448  int ret, size;
449  int64_t left;
450  AVStream *st;
451  WAVDemuxContext *wav = s->priv_data;
452 
453  if (CONFIG_SPDIF_DEMUXER && wav->spdif == 0 &&
454  s->streams[0]->codec->codec_tag == 1) {
455  enum AVCodecID codec;
456  ret = ff_spdif_probe(s->pb->buffer, s->pb->buf_end - s->pb->buffer,
457  &codec);
458  if (ret > AVPROBE_SCORE_EXTENSION) {
459  s->streams[0]->codec->codec_id = codec;
460  wav->spdif = 1;
461  } else {
462  wav->spdif = -1;
463  }
464  }
465  if (CONFIG_SPDIF_DEMUXER && wav->spdif == 1)
466  return ff_spdif_read_packet(s, pkt);
467 
468  if (wav->smv_data_ofs > 0) {
469  int64_t audio_dts, video_dts;
470 smv_retry:
471  audio_dts = (int32_t)s->streams[0]->cur_dts;
472  video_dts = (int32_t)s->streams[1]->cur_dts;
473 
474  if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
475  /*We always return a video frame first to get the pixel format first*/
476  wav->smv_last_stream = wav->smv_given_first ?
477  av_compare_ts(video_dts, s->streams[1]->time_base,
478  audio_dts, s->streams[0]->time_base) > 0 : 0;
479  wav->smv_given_first = 1;
480  }
481  wav->smv_last_stream = !wav->smv_last_stream;
482  wav->smv_last_stream |= wav->audio_eof;
483  wav->smv_last_stream &= !wav->smv_eof;
484  if (wav->smv_last_stream) {
485  uint64_t old_pos = avio_tell(s->pb);
486  uint64_t new_pos = wav->smv_data_ofs +
487  wav->smv_block * wav->smv_block_size;
488  if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
489  ret = AVERROR_EOF;
490  goto smv_out;
491  }
492  size = avio_rl24(s->pb);
493  ret = av_get_packet(s->pb, pkt, size);
494  if (ret < 0)
495  goto smv_out;
496  pkt->pos -= 3;
497  pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg + wav->smv_cur_pt;
498  wav->smv_cur_pt++;
499  if (wav->smv_frames_per_jpeg > 0)
500  wav->smv_cur_pt %= wav->smv_frames_per_jpeg;
501  if (!wav->smv_cur_pt)
502  wav->smv_block++;
503 
504  pkt->stream_index = 1;
505 smv_out:
506  avio_seek(s->pb, old_pos, SEEK_SET);
507  if (ret == AVERROR_EOF) {
508  wav->smv_eof = 1;
509  goto smv_retry;
510  }
511  return ret;
512  }
513  }
514 
515  st = s->streams[0];
516 
517  left = wav->data_end - avio_tell(s->pb);
518  if (wav->ignore_length)
519  left = INT_MAX;
520  if (left <= 0) {
521  if (CONFIG_W64_DEMUXER && wav->w64)
522  left = find_guid(s->pb, ff_w64_guid_data) - 24;
523  else
524  left = find_tag(wav, s->pb, MKTAG('d', 'a', 't', 'a'));
525  if (left < 0) {
526  wav->audio_eof = 1;
527  if (wav->smv_data_ofs > 0 && !wav->smv_eof)
528  goto smv_retry;
529  return AVERROR_EOF;
530  }
531  wav->data_end = avio_tell(s->pb) + left;
532  }
533 
534  size = MAX_SIZE;
535  if (st->codec->block_align > 1) {
536  if (size < st->codec->block_align)
537  size = st->codec->block_align;
538  size = (size / st->codec->block_align) * st->codec->block_align;
539  }
540  size = FFMIN(size, left);
541  ret = av_get_packet(s->pb, pkt, size);
542  if (ret < 0)
543  return ret;
544  pkt->stream_index = 0;
545 
546  return ret;
547 }
548 
549 static int wav_read_seek(AVFormatContext *s,
550  int stream_index, int64_t timestamp, int flags)
551 {
552  WAVDemuxContext *wav = s->priv_data;
553  AVStream *st;
554  wav->smv_eof = 0;
555  wav->audio_eof = 0;
556  if (wav->smv_data_ofs > 0) {
557  int64_t smv_timestamp = timestamp;
558  if (stream_index == 0)
559  smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
560  else
561  timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
562  if (wav->smv_frames_per_jpeg > 0) {
563  wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
564  wav->smv_cur_pt = smv_timestamp % wav->smv_frames_per_jpeg;
565  }
566  }
567 
568  st = s->streams[0];
569  switch (st->codec->codec_id) {
570  case AV_CODEC_ID_MP2:
571  case AV_CODEC_ID_MP3:
572  case AV_CODEC_ID_AC3:
573  case AV_CODEC_ID_DTS:
574  /* use generic seeking with dynamically generated indexes */
575  return -1;
576  default:
577  break;
578  }
579  return ff_pcm_read_seek(s, stream_index, timestamp, flags);
580 }
581 
582 #define OFFSET(x) offsetof(WAVDemuxContext, x)
583 #define DEC AV_OPT_FLAG_DECODING_PARAM
584 static const AVOption demux_options[] = {
585  { "ignore_length", "Ignore length", OFFSET(ignore_length), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
586  { NULL },
587 };
588 
589 static const AVClass wav_demuxer_class = {
590  .class_name = "WAV demuxer",
591  .item_name = av_default_item_name,
592  .option = demux_options,
593  .version = LIBAVUTIL_VERSION_INT,
594 };
595 AVInputFormat ff_wav_demuxer = {
596  .name = "wav",
597  .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
598  .priv_data_size = sizeof(WAVDemuxContext),
599  .read_probe = wav_probe,
600  .read_header = wav_read_header,
601  .read_packet = wav_read_packet,
602  .read_seek = wav_read_seek,
603  .flags = AVFMT_GENERIC_INDEX,
604  .codec_tag = (const AVCodecTag * const []) { ff_codec_wav_tags, 0 },
605  .priv_class = &wav_demuxer_class,
606 };
607 #endif /* CONFIG_WAV_DEMUXER */
608 
609 #if CONFIG_W64_DEMUXER
610 static int w64_probe(AVProbeData *p)
611 {
612  if (p->buf_size <= 40)
613  return 0;
614  if (!memcmp(p->buf, ff_w64_guid_riff, 16) &&
615  !memcmp(p->buf + 24, ff_w64_guid_wave, 16))
616  return AVPROBE_SCORE_MAX;
617  else
618  return 0;
619 }
620 
621 static int w64_read_header(AVFormatContext *s)
622 {
623  int64_t size, data_ofs = 0;
624  AVIOContext *pb = s->pb;
625  WAVDemuxContext *wav = s->priv_data;
626  AVStream *st;
627  uint8_t guid[16];
628  int ret;
629 
630  avio_read(pb, guid, 16);
631  if (memcmp(guid, ff_w64_guid_riff, 16))
632  return AVERROR_INVALIDDATA;
633 
634  /* riff + wave + fmt + sizes */
635  if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8)
636  return AVERROR_INVALIDDATA;
637 
638  avio_read(pb, guid, 16);
639  if (memcmp(guid, ff_w64_guid_wave, 16)) {
640  av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
641  return AVERROR_INVALIDDATA;
642  }
643 
644  wav->w64 = 1;
645 
646  st = avformat_new_stream(s, NULL);
647  if (!st)
648  return AVERROR(ENOMEM);
649 
650  while (!url_feof(pb)) {
651  if (avio_read(pb, guid, 16) != 16)
652  break;
653  size = avio_rl64(pb);
654  if (size <= 24 || INT64_MAX - size < avio_tell(pb))
655  return AVERROR_INVALIDDATA;
656 
657  if (!memcmp(guid, ff_w64_guid_fmt, 16)) {
658  /* subtract chunk header size - normal wav file doesn't count it */
659  ret = ff_get_wav_header(pb, st->codec, size - 24);
660  if (ret < 0)
661  return ret;
662  avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
663 
664  avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
665  } else if (!memcmp(guid, ff_w64_guid_fact, 16)) {
666  int64_t samples;
667 
668  samples = avio_rl64(pb);
669  if (samples > 0)
670  st->duration = samples;
671  } else if (!memcmp(guid, ff_w64_guid_data, 16)) {
672  wav->data_end = avio_tell(pb) + size - 24;
673 
674  data_ofs = avio_tell(pb);
675  if (!pb->seekable)
676  break;
677 
678  avio_skip(pb, size - 24);
679  } else if (!memcmp(guid, ff_w64_guid_summarylist, 16)) {
680  int64_t start, end, cur;
681  uint32_t count, chunk_size, i;
682 
683  start = avio_tell(pb);
684  end = start + FFALIGN(size, INT64_C(8)) - 24;
685  count = avio_rl32(pb);
686 
687  for (i = 0; i < count; i++) {
688  char chunk_key[5], *value;
689 
690  if (url_feof(pb) || (cur = avio_tell(pb)) < 0 || cur > end - 8 /* = tag + size */)
691  break;
692 
693  chunk_key[4] = 0;
694  avio_read(pb, chunk_key, 4);
695  chunk_size = avio_rl32(pb);
696 
697  value = av_mallocz(chunk_size + 1);
698  if (!value)
699  return AVERROR(ENOMEM);
700 
701  ret = avio_get_str16le(pb, chunk_size, value, chunk_size);
702  avio_skip(pb, chunk_size - ret);
703 
704  av_dict_set(&s->metadata, chunk_key, value, AV_DICT_DONT_STRDUP_VAL);
705  }
706 
707  avio_skip(pb, end - avio_tell(pb));
708  } else {
709  av_log(s, AV_LOG_DEBUG, "unknown guid: "FF_PRI_GUID"\n", FF_ARG_GUID(guid));
710  avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
711  }
712  }
713 
714  if (!data_ofs)
715  return AVERROR_EOF;
716 
717  ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
719 
720  handle_stream_probing(st);
722 
723  avio_seek(pb, data_ofs, SEEK_SET);
724 
725  return 0;
726 }
727 
728 AVInputFormat ff_w64_demuxer = {
729  .name = "w64",
730  .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
731  .priv_data_size = sizeof(WAVDemuxContext),
732  .read_probe = w64_probe,
733  .read_header = w64_read_header,
734  .read_packet = wav_read_packet,
735  .read_seek = wav_read_seek,
736  .flags = AVFMT_GENERIC_INDEX,
737  .codec_tag = (const AVCodecTag * const []) { ff_codec_wav_tags, 0 },
738 };
739 #endif /* CONFIG_W64_DEMUXER */