]> git.sesse.net Git - ffmpeg/blob - libavformat/wav.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / wav.c
1 /*
2  * WAV muxer and 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 "internal.h"
33 #include "avio_internal.h"
34 #include "pcm.h"
35 #include "riff.h"
36 #include "avio.h"
37 #include "metadata.h"
38
39 typedef struct {
40     const AVClass *class;
41     int64_t data;
42     int64_t data_end;
43     int64_t minpts;
44     int64_t maxpts;
45     int last_duration;
46     int w64;
47     int write_bext;
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 } WAVContext;
56
57 #if CONFIG_WAV_MUXER
58 static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
59 {
60     AVDictionaryEntry *tag;
61     int len = 0;
62
63     if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
64         len = strlen(tag->value);
65         len = FFMIN(len, maxlen);
66         avio_write(s->pb, tag->value, len);
67     }
68
69     ffio_fill(s->pb, 0, maxlen - len);
70 }
71
72 static void bwf_write_bext_chunk(AVFormatContext *s)
73 {
74     AVDictionaryEntry *tmp_tag;
75     uint64_t time_reference = 0;
76     int64_t bext = ff_start_tag(s->pb, "bext");
77
78     bwf_write_bext_string(s, "description", 256);
79     bwf_write_bext_string(s, "originator", 32);
80     bwf_write_bext_string(s, "originator_reference", 32);
81     bwf_write_bext_string(s, "origination_date", 10);
82     bwf_write_bext_string(s, "origination_time", 8);
83
84     if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
85         time_reference = strtoll(tmp_tag->value, NULL, 10);
86     avio_wl64(s->pb, time_reference);
87     avio_wl16(s->pb, 1);  // set version to 1
88
89     if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
90         unsigned char umidpart_str[17] = {0};
91         int i;
92         uint64_t umidpart;
93         int len = strlen(tmp_tag->value+2);
94
95         for (i = 0; i < len/16; i++) {
96             memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
97             umidpart = strtoll(umidpart_str, NULL, 16);
98             avio_wb64(s->pb, umidpart);
99         }
100         ffio_fill(s->pb, 0, 64 - i*8);
101     } else
102         ffio_fill(s->pb, 0, 64); // zero UMID
103
104     ffio_fill(s->pb, 0, 190); // Reserved
105
106     if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
107         avio_put_str(s->pb, tmp_tag->value);
108
109     ff_end_tag(s->pb, bext);
110 }
111
112 static int wav_write_header(AVFormatContext *s)
113 {
114     WAVContext *wav = s->priv_data;
115     AVIOContext *pb = s->pb;
116     int64_t fmt, fact;
117
118     ffio_wfourcc(pb, "RIFF");
119     avio_wl32(pb, 0); /* file length */
120     ffio_wfourcc(pb, "WAVE");
121
122     /* format header */
123     fmt = ff_start_tag(pb, "fmt ");
124     if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
125         av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
126                s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
127         return -1;
128     }
129     ff_end_tag(pb, fmt);
130
131     if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
132         && s->pb->seekable) {
133         fact = ff_start_tag(pb, "fact");
134         avio_wl32(pb, 0);
135         ff_end_tag(pb, fact);
136     }
137
138     if (wav->write_bext)
139         bwf_write_bext_chunk(s);
140
141     avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
142     wav->maxpts = wav->last_duration = 0;
143     wav->minpts = INT64_MAX;
144
145     /* data header */
146     wav->data = ff_start_tag(pb, "data");
147
148     avio_flush(pb);
149
150     return 0;
151 }
152
153 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
154 {
155     AVIOContext *pb  = s->pb;
156     WAVContext    *wav = s->priv_data;
157     avio_write(pb, pkt->data, pkt->size);
158     if(pkt->pts != AV_NOPTS_VALUE) {
159         wav->minpts        = FFMIN(wav->minpts, pkt->pts);
160         wav->maxpts        = FFMAX(wav->maxpts, pkt->pts);
161         wav->last_duration = pkt->duration;
162     } else
163         av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
164     return 0;
165 }
166
167 static int wav_write_trailer(AVFormatContext *s)
168 {
169     AVIOContext *pb  = s->pb;
170     WAVContext    *wav = s->priv_data;
171     int64_t file_size;
172
173     avio_flush(pb);
174
175     if (s->pb->seekable) {
176         ff_end_tag(pb, wav->data);
177
178         /* update file size */
179         file_size = avio_tell(pb);
180         avio_seek(pb, 4, SEEK_SET);
181         avio_wl32(pb, (uint32_t)(file_size - 8));
182         avio_seek(pb, file_size, SEEK_SET);
183
184         avio_flush(pb);
185
186         if(s->streams[0]->codec->codec_tag != 0x01) {
187             /* Update num_samps in fact chunk */
188             int number_of_samples;
189             number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
190                                            s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
191                                            s->streams[0]->time_base.den);
192             avio_seek(pb, wav->data-12, SEEK_SET);
193             avio_wl32(pb, number_of_samples);
194             avio_seek(pb, file_size, SEEK_SET);
195             avio_flush(pb);
196         }
197     }
198     return 0;
199 }
200
201 #define OFFSET(x) offsetof(WAVContext, x)
202 #define ENC AV_OPT_FLAG_ENCODING_PARAM
203 static const AVOption options[] = {
204     { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { 0 }, 0, 1, ENC },
205     { NULL },
206 };
207
208 static const AVClass wav_muxer_class = {
209     .class_name = "WAV muxer",
210     .item_name  = av_default_item_name,
211     .option     = options,
212     .version    = LIBAVUTIL_VERSION_INT,
213 };
214
215 AVOutputFormat ff_wav_muxer = {
216     .name              = "wav",
217     .long_name         = NULL_IF_CONFIG_SMALL("WAV format"),
218     .mime_type         = "audio/x-wav",
219     .extensions        = "wav",
220     .priv_data_size    = sizeof(WAVContext),
221     .audio_codec       = CODEC_ID_PCM_S16LE,
222     .video_codec       = CODEC_ID_NONE,
223     .write_header      = wav_write_header,
224     .write_packet      = wav_write_packet,
225     .write_trailer     = wav_write_trailer,
226     .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
227     .priv_class = &wav_muxer_class,
228 };
229 #endif /* CONFIG_WAV_MUXER */
230
231
232 #if CONFIG_WAV_DEMUXER
233
234 static int64_t next_tag(AVIOContext *pb, uint32_t *tag)
235 {
236     *tag = avio_rl32(pb);
237     return avio_rl32(pb);
238 }
239
240 /* return the size of the found tag */
241 static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
242 {
243     unsigned int tag;
244     int64_t size;
245
246     for (;;) {
247         if (url_feof(pb))
248             return -1;
249         size = next_tag(pb, &tag);
250         if (tag == tag1)
251             break;
252         avio_skip(pb, size);
253     }
254     return size;
255 }
256
257 static int wav_probe(AVProbeData *p)
258 {
259     /* check file header */
260     if (p->buf_size <= 32)
261         return 0;
262     if (!memcmp(p->buf + 8, "WAVE", 4)) {
263         if (!memcmp(p->buf, "RIFF", 4))
264             /*
265               Since ACT demuxer has standard WAV header at top of it's own,
266               returning score is decreased to avoid probe conflict
267               between ACT and WAV.
268             */
269             return AVPROBE_SCORE_MAX - 1;
270         else if (!memcmp(p->buf,      "RF64", 4) &&
271                  !memcmp(p->buf + 12, "ds64", 4))
272             return AVPROBE_SCORE_MAX;
273     }
274     return 0;
275 }
276
277 static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
278 {
279     AVIOContext *pb = s->pb;
280     int ret;
281
282     /* parse fmt header */
283     *st = avformat_new_stream(s, NULL);
284     if (!*st)
285         return AVERROR(ENOMEM);
286
287     ret = ff_get_wav_header(pb, (*st)->codec, size);
288     if (ret < 0)
289         return ret;
290     (*st)->need_parsing = AVSTREAM_PARSE_FULL;
291
292     avpriv_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
293
294     return 0;
295 }
296
297 static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
298                                         int length)
299 {
300     char temp[257];
301     int ret;
302
303     av_assert0(length <= sizeof(temp));
304     if ((ret = avio_read(s->pb, temp, length)) < 0)
305         return ret;
306
307     temp[length] = 0;
308
309     if (strlen(temp))
310         return av_dict_set(&s->metadata, key, temp, 0);
311
312     return 0;
313 }
314
315 static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
316 {
317     char temp[131], *coding_history;
318     int ret, x;
319     uint64_t time_reference;
320     int64_t umid_parts[8], umid_mask = 0;
321
322     if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
323         (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
324         (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
325         (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
326         (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
327         return ret;
328
329     time_reference = avio_rl64(s->pb);
330     snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
331     if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
332         return ret;
333
334     /* check if version is >= 1, in which case an UMID may be present */
335     if (avio_rl16(s->pb) >= 1) {
336         for (x = 0; x < 8; x++)
337             umid_mask |= umid_parts[x] = avio_rb64(s->pb);
338
339         if (umid_mask) {
340             /* the string formatting below is per SMPTE 330M-2004 Annex C */
341             if (umid_parts[4] == 0 && umid_parts[5] == 0 && umid_parts[6] == 0 && umid_parts[7] == 0) {
342                 /* basic UMID */
343                 snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
344                          umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3]);
345             } else {
346                 /* extended UMID */
347                 snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
348                                              "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
349                          umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3],
350                          umid_parts[4], umid_parts[5], umid_parts[6], umid_parts[7]);
351             }
352
353             if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
354                 return ret;
355         }
356
357         avio_skip(s->pb, 190);
358     } else
359         avio_skip(s->pb, 254);
360
361     if (size > 602) {
362         /* CodingHistory present */
363         size -= 602;
364
365         if (!(coding_history = av_malloc(size+1)))
366             return AVERROR(ENOMEM);
367
368         if ((ret = avio_read(s->pb, coding_history, size)) < 0)
369             return ret;
370
371         coding_history[size] = 0;
372         if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
373                                AV_DICT_DONT_STRDUP_VAL)) < 0)
374             return ret;
375     }
376
377     return 0;
378 }
379
380 static const AVMetadataConv wav_metadata_conv[] = {
381     {"description",      "comment"      },
382     {"originator",       "encoded_by"   },
383     {"origination_date", "date"         },
384     {"origination_time", "creation_time"},
385     {0},
386 };
387
388 /* wav input */
389 static int wav_read_header(AVFormatContext *s,
390                            AVFormatParameters *ap)
391 {
392     int64_t size, av_uninit(data_size);
393     int64_t sample_count=0;
394     int rf64;
395     uint32_t tag, list_type;
396     AVIOContext *pb = s->pb;
397     AVStream *st = NULL;
398     WAVContext *wav = s->priv_data;
399     int ret, got_fmt = 0;
400     int64_t next_tag_ofs, data_ofs = -1;
401
402     wav->smv_data_ofs = -1;
403
404     /* check RIFF header */
405     tag = avio_rl32(pb);
406
407     rf64 = tag == MKTAG('R', 'F', '6', '4');
408     if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
409         return -1;
410     avio_rl32(pb); /* file size */
411     tag = avio_rl32(pb);
412     if (tag != MKTAG('W', 'A', 'V', 'E'))
413         return -1;
414
415     if (rf64) {
416         if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
417             return -1;
418         size = avio_rl32(pb);
419         if (size < 24)
420             return -1;
421         avio_rl64(pb); /* RIFF size */
422         data_size = avio_rl64(pb);
423         sample_count = avio_rl64(pb);
424         if (data_size < 0 || sample_count < 0) {
425             av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
426                    "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
427                    data_size, sample_count);
428             return AVERROR_INVALIDDATA;
429         }
430         avio_skip(pb, size - 24); /* skip rest of ds64 chunk */
431
432     }
433
434     for (;;) {
435         AVStream *vst;
436         size = next_tag(pb, &tag);
437         next_tag_ofs = avio_tell(pb) + size;
438
439         if (url_feof(pb))
440             break;
441
442         switch (tag) {
443         case MKTAG('f', 'm', 't', ' '):
444             /* only parse the first 'fmt ' tag found */
445             if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st)) < 0) {
446                 return ret;
447             } else if (got_fmt)
448                 av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
449
450             got_fmt = 1;
451             break;
452         case MKTAG('d', 'a', 't', 'a'):
453             if (!got_fmt) {
454                 av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
455                 return AVERROR_INVALIDDATA;
456             }
457
458             if (rf64) {
459                 next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
460             } else {
461                 data_size = size;
462                 next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
463             }
464
465             data_ofs = avio_tell(pb);
466
467             /* don't look for footer metadata if we can't seek or if we don't
468              * know where the data tag ends
469              */
470             if (!pb->seekable || (!rf64 && !size))
471                 goto break_loop;
472             break;
473         case MKTAG('f','a','c','t'):
474             if (!sample_count)
475                 sample_count = avio_rl32(pb);
476             break;
477         case MKTAG('b','e','x','t'):
478             if ((ret = wav_parse_bext_tag(s, size)) < 0)
479                 return ret;
480             break;
481         case MKTAG('S','M','V','0'):
482             // SMV file, a wav file with video appended.
483             if (size != MKTAG('0','2','0','0')) {
484                 av_log(s, AV_LOG_ERROR, "Unknown SMV version found\n");
485                 goto break_loop;
486             }
487             av_log(s, AV_LOG_DEBUG, "Found SMV data\n");
488             vst = avformat_new_stream(s, NULL);
489             if (!vst)
490                 return AVERROR(ENOMEM);
491             avio_r8(pb);
492             vst->id = 1;
493             vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
494             vst->codec->codec_id = CODEC_ID_MJPEG;
495             vst->codec->width  = avio_rl24(pb);
496             vst->codec->height = avio_rl24(pb);
497             size = avio_rl24(pb);
498             wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
499             avio_rl24(pb);
500             wav->smv_block_size = avio_rl24(pb);
501             avpriv_set_pts_info(vst, 32, 1, avio_rl24(pb));
502             vst->duration = avio_rl24(pb);
503             avio_rl24(pb);
504             avio_rl24(pb);
505             wav->smv_frames_per_jpeg = avio_rl24(pb);
506             goto break_loop;
507         case MKTAG('L', 'I', 'S', 'T'):
508             list_type = avio_rl32(pb);
509             if (size <= 4) {
510                 av_log(s, AV_LOG_ERROR, "too short LIST");
511                 return AVERROR_INVALIDDATA;
512             }
513             switch (list_type) {
514             case MKTAG('I', 'N', 'F', 'O'):
515                 if ((ret = ff_read_riff_info(s, size - 4)) < 0)
516                     return ret;
517             }
518             break;
519         }
520
521         /* seek to next tag unless we know that we'll run into EOF */
522         if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
523             avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) {
524             break;
525         }
526     }
527 break_loop:
528     if (data_ofs < 0) {
529         av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
530         return AVERROR_INVALIDDATA;
531     }
532
533     avio_seek(pb, data_ofs, SEEK_SET);
534
535     if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
536         sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
537     if (sample_count)
538         st->duration = sample_count;
539
540     ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
541     ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
542
543     return 0;
544 }
545
546 /** Find chunk with w64 GUID by skipping over other chunks
547  * @return the size of the found chunk
548  */
549 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
550 {
551     uint8_t guid[16];
552     int64_t size;
553
554     while (!url_feof(pb)) {
555         avio_read(pb, guid, 16);
556         size = avio_rl64(pb);
557         if (size <= 24)
558             return -1;
559         if (!memcmp(guid, guid1, 16))
560             return size;
561         avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
562     }
563     return -1;
564 }
565
566 static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
567     0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
568
569 #define MAX_SIZE 4096
570
571 static int wav_read_packet(AVFormatContext *s,
572                            AVPacket *pkt)
573 {
574     int ret, size;
575     int64_t left;
576     AVStream *st;
577     WAVContext *wav = s->priv_data;
578
579     if (wav->smv_data_ofs > 0) {
580         int64_t audio_dts, video_dts;
581 smv_retry:
582         audio_dts = s->streams[0]->cur_dts;
583         video_dts = s->streams[1]->cur_dts;
584         if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
585             audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q);
586             video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q);
587             wav->smv_last_stream = video_dts >= audio_dts;
588         }
589         wav->smv_last_stream = !wav->smv_last_stream;
590         wav->smv_last_stream |= wav->audio_eof;
591         wav->smv_last_stream &= !wav->smv_eof;
592         if (wav->smv_last_stream) {
593             uint64_t old_pos = avio_tell(s->pb);
594             uint64_t new_pos = wav->smv_data_ofs +
595                 wav->smv_block * wav->smv_block_size;
596             if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
597                 ret = AVERROR_EOF;
598                 goto smv_out;
599             }
600             size = avio_rl24(s->pb);
601             ret  = av_get_packet(s->pb, pkt, size);
602             if (ret < 0)
603                 goto smv_out;
604             pkt->pos -= 3;
605             pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg;
606             wav->smv_block++;
607             pkt->stream_index = 1;
608 smv_out:
609             avio_seek(s->pb, old_pos, SEEK_SET);
610             if (ret == AVERROR_EOF) {
611                 wav->smv_eof = 1;
612                 goto smv_retry;
613             }
614             return ret;
615         }
616     }
617
618     st = s->streams[0];
619
620     left = wav->data_end - avio_tell(s->pb);
621     if (left <= 0){
622         if (CONFIG_W64_DEMUXER && wav->w64)
623             left = find_guid(s->pb, guid_data) - 24;
624         else
625             left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
626         if (left < 0) {
627             wav->audio_eof = 1;
628             if (wav->smv_data_ofs > 0 && !wav->smv_eof)
629                 goto smv_retry;
630             return AVERROR_EOF;
631         }
632         wav->data_end= avio_tell(s->pb) + left;
633     }
634
635     size = MAX_SIZE;
636     if (st->codec->block_align > 1) {
637         if (size < st->codec->block_align)
638             size = st->codec->block_align;
639         size = (size / st->codec->block_align) * st->codec->block_align;
640     }
641     size = FFMIN(size, left);
642     ret  = av_get_packet(s->pb, pkt, size);
643     if (ret < 0)
644         return ret;
645     pkt->stream_index = 0;
646
647     return ret;
648 }
649
650 static int wav_read_seek(AVFormatContext *s,
651                          int stream_index, int64_t timestamp, int flags)
652 {
653     WAVContext *wav = s->priv_data;
654     AVStream *st;
655     wav->smv_eof = 0;
656     wav->audio_eof = 0;
657     if (wav->smv_data_ofs > 0) {
658         int64_t smv_timestamp = timestamp;
659         if (stream_index == 0)
660             smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
661         else
662             timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
663         wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
664     }
665
666     st = s->streams[0];
667     switch (st->codec->codec_id) {
668     case CODEC_ID_MP2:
669     case CODEC_ID_MP3:
670     case CODEC_ID_AC3:
671     case CODEC_ID_DTS:
672         /* use generic seeking with dynamically generated indexes */
673         return -1;
674     default:
675         break;
676     }
677     return pcm_read_seek(s, stream_index, timestamp, flags);
678 }
679
680 AVInputFormat ff_wav_demuxer = {
681     .name           = "wav",
682     .long_name      = NULL_IF_CONFIG_SMALL("WAV format"),
683     .priv_data_size = sizeof(WAVContext),
684     .read_probe     = wav_probe,
685     .read_header    = wav_read_header,
686     .read_packet    = wav_read_packet,
687     .read_seek      = wav_read_seek,
688     .flags= AVFMT_GENERIC_INDEX,
689     .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
690 };
691 #endif /* CONFIG_WAV_DEMUXER */
692
693
694 #if CONFIG_W64_DEMUXER
695 static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
696     0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
697
698 static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
699     0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
700
701 static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
702     0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
703
704 static int w64_probe(AVProbeData *p)
705 {
706     if (p->buf_size <= 40)
707         return 0;
708     if (!memcmp(p->buf,      guid_riff, 16) &&
709         !memcmp(p->buf + 24, guid_wave, 16))
710         return AVPROBE_SCORE_MAX;
711     else
712         return 0;
713 }
714
715 static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
716 {
717     int64_t size;
718     AVIOContext *pb  = s->pb;
719     WAVContext    *wav = s->priv_data;
720     AVStream *st;
721     uint8_t guid[16];
722     int ret;
723
724     avio_read(pb, guid, 16);
725     if (memcmp(guid, guid_riff, 16))
726         return -1;
727
728     if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
729         return -1;
730
731     avio_read(pb, guid, 16);
732     if (memcmp(guid, guid_wave, 16)) {
733         av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
734         return -1;
735     }
736
737     size = find_guid(pb, guid_fmt);
738     if (size < 0) {
739         av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
740         return -1;
741     }
742
743     st = avformat_new_stream(s, NULL);
744     if (!st)
745         return AVERROR(ENOMEM);
746
747     /* subtract chunk header size - normal wav file doesn't count it */
748     ret = ff_get_wav_header(pb, st->codec, size - 24);
749     if (ret < 0)
750         return ret;
751     avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
752
753     st->need_parsing = AVSTREAM_PARSE_FULL;
754
755     avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
756
757     size = find_guid(pb, guid_data);
758     if (size < 0) {
759         av_log(s, AV_LOG_ERROR, "could not find data guid\n");
760         return -1;
761     }
762     wav->data_end = avio_tell(pb) + size - 24;
763     wav->w64      = 1;
764
765     return 0;
766 }
767
768 AVInputFormat ff_w64_demuxer = {
769     .name           = "w64",
770     .long_name      = NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
771     .priv_data_size = sizeof(WAVContext),
772     .read_probe     = w64_probe,
773     .read_header    = w64_read_header,
774     .read_packet    = wav_read_packet,
775     .read_seek      = wav_read_seek,
776     .flags = AVFMT_GENERIC_INDEX,
777     .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
778 };
779 #endif /* CONFIG_W64_DEMUXER */