libavformat/wav.c
Go to the documentation of this file.
00001 /*
00002  * WAV muxer and demuxer
00003  * Copyright (c) 2001, 2002 Fabrice Bellard
00004  *
00005  * Sony Wave64 demuxer
00006  * RF64 demuxer
00007  * Copyright (c) 2009 Daniel Verkamp
00008  *
00009  * This file is part of Libav.
00010  *
00011  * Libav is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU Lesser General Public
00013  * License as published by the Free Software Foundation; either
00014  * version 2.1 of the License, or (at your option) any later version.
00015  *
00016  * Libav is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with Libav; if not, write to the Free Software
00023  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00024  */
00025 
00026 #include "libavutil/avassert.h"
00027 #include "libavutil/dict.h"
00028 #include "libavutil/log.h"
00029 #include "libavutil/mathematics.h"
00030 #include "libavutil/opt.h"
00031 #include "avformat.h"
00032 #include "internal.h"
00033 #include "avio_internal.h"
00034 #include "pcm.h"
00035 #include "riff.h"
00036 #include "avio.h"
00037 #include "metadata.h"
00038 
00039 typedef struct {
00040     const AVClass *class;
00041     int64_t data;
00042     int64_t data_end;
00043     int64_t minpts;
00044     int64_t maxpts;
00045     int last_duration;
00046     int w64;
00047     int write_bext;
00048 } WAVContext;
00049 
00050 #if CONFIG_WAV_MUXER
00051 static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
00052 {
00053     AVDictionaryEntry *tag;
00054     int len = 0;
00055 
00056     if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
00057         len = strlen(tag->value);
00058         len = FFMIN(len, maxlen);
00059         avio_write(s->pb, tag->value, len);
00060     }
00061 
00062     ffio_fill(s->pb, 0, maxlen - len);
00063 }
00064 
00065 static void bwf_write_bext_chunk(AVFormatContext *s)
00066 {
00067     AVDictionaryEntry *tmp_tag;
00068     uint64_t time_reference = 0;
00069     int64_t bext = ff_start_tag(s->pb, "bext");
00070 
00071     bwf_write_bext_string(s, "description", 256);
00072     bwf_write_bext_string(s, "originator", 32);
00073     bwf_write_bext_string(s, "originator_reference", 32);
00074     bwf_write_bext_string(s, "origination_date", 10);
00075     bwf_write_bext_string(s, "origination_time", 8);
00076 
00077     if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
00078         time_reference = strtoll(tmp_tag->value, NULL, 10);
00079     avio_wl64(s->pb, time_reference);
00080     avio_wl16(s->pb, 1);  // set version to 1
00081 
00082     if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
00083         unsigned char umidpart_str[17] = {0};
00084         int i;
00085         uint64_t umidpart;
00086         int len = strlen(tmp_tag->value+2);
00087 
00088         for (i = 0; i < len/16; i++) {
00089             memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
00090             umidpart = strtoll(umidpart_str, NULL, 16);
00091             avio_wb64(s->pb, umidpart);
00092         }
00093         ffio_fill(s->pb, 0, 64 - i*8);
00094     } else
00095         ffio_fill(s->pb, 0, 64); // zero UMID
00096 
00097     ffio_fill(s->pb, 0, 190); // Reserved
00098 
00099     if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
00100         avio_put_str(s->pb, tmp_tag->value);
00101 
00102     ff_end_tag(s->pb, bext);
00103 }
00104 
00105 static int wav_write_header(AVFormatContext *s)
00106 {
00107     WAVContext *wav = s->priv_data;
00108     AVIOContext *pb = s->pb;
00109     int64_t fmt, fact;
00110 
00111     ffio_wfourcc(pb, "RIFF");
00112     avio_wl32(pb, 0); /* file length */
00113     ffio_wfourcc(pb, "WAVE");
00114 
00115     /* format header */
00116     fmt = ff_start_tag(pb, "fmt ");
00117     if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
00118         av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
00119                s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
00120         return -1;
00121     }
00122     ff_end_tag(pb, fmt);
00123 
00124     if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
00125         && s->pb->seekable) {
00126         fact = ff_start_tag(pb, "fact");
00127         avio_wl32(pb, 0);
00128         ff_end_tag(pb, fact);
00129     }
00130 
00131     if (wav->write_bext)
00132         bwf_write_bext_chunk(s);
00133 
00134     avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
00135     wav->maxpts = wav->last_duration = 0;
00136     wav->minpts = INT64_MAX;
00137 
00138     /* data header */
00139     wav->data = ff_start_tag(pb, "data");
00140 
00141     avio_flush(pb);
00142 
00143     return 0;
00144 }
00145 
00146 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
00147 {
00148     AVIOContext *pb  = s->pb;
00149     WAVContext    *wav = s->priv_data;
00150     avio_write(pb, pkt->data, pkt->size);
00151     if(pkt->pts != AV_NOPTS_VALUE) {
00152         wav->minpts        = FFMIN(wav->minpts, pkt->pts);
00153         wav->maxpts        = FFMAX(wav->maxpts, pkt->pts);
00154         wav->last_duration = pkt->duration;
00155     } else
00156         av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
00157     return 0;
00158 }
00159 
00160 static int wav_write_trailer(AVFormatContext *s)
00161 {
00162     AVIOContext *pb  = s->pb;
00163     WAVContext    *wav = s->priv_data;
00164     int64_t file_size;
00165 
00166     avio_flush(pb);
00167 
00168     if (s->pb->seekable) {
00169         ff_end_tag(pb, wav->data);
00170 
00171         /* update file size */
00172         file_size = avio_tell(pb);
00173         avio_seek(pb, 4, SEEK_SET);
00174         avio_wl32(pb, (uint32_t)(file_size - 8));
00175         avio_seek(pb, file_size, SEEK_SET);
00176 
00177         avio_flush(pb);
00178 
00179         if(s->streams[0]->codec->codec_tag != 0x01) {
00180             /* Update num_samps in fact chunk */
00181             int number_of_samples;
00182             number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
00183                                            s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
00184                                            s->streams[0]->time_base.den);
00185             avio_seek(pb, wav->data-12, SEEK_SET);
00186             avio_wl32(pb, number_of_samples);
00187             avio_seek(pb, file_size, SEEK_SET);
00188             avio_flush(pb);
00189         }
00190     }
00191     return 0;
00192 }
00193 
00194 #define OFFSET(x) offsetof(WAVContext, x)
00195 #define ENC AV_OPT_FLAG_ENCODING_PARAM
00196 static const AVOption options[] = {
00197     { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { 0 }, 0, 1, ENC },
00198     { NULL },
00199 };
00200 
00201 static const AVClass wav_muxer_class = {
00202     .class_name = "WAV muxer",
00203     .item_name  = av_default_item_name,
00204     .option     = options,
00205     .version    = LIBAVUTIL_VERSION_INT,
00206 };
00207 
00208 AVOutputFormat ff_wav_muxer = {
00209     .name              = "wav",
00210     .long_name         = NULL_IF_CONFIG_SMALL("WAV format"),
00211     .mime_type         = "audio/x-wav",
00212     .extensions        = "wav",
00213     .priv_data_size    = sizeof(WAVContext),
00214     .audio_codec       = CODEC_ID_PCM_S16LE,
00215     .video_codec       = CODEC_ID_NONE,
00216     .write_header      = wav_write_header,
00217     .write_packet      = wav_write_packet,
00218     .write_trailer     = wav_write_trailer,
00219     .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
00220     .priv_class = &wav_muxer_class,
00221 };
00222 #endif /* CONFIG_WAV_MUXER */
00223 
00224 
00225 #if CONFIG_WAV_DEMUXER
00226 
00227 static int64_t next_tag(AVIOContext *pb, uint32_t *tag)
00228 {
00229     *tag = avio_rl32(pb);
00230     return avio_rl32(pb);
00231 }
00232 
00233 /* return the size of the found tag */
00234 static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
00235 {
00236     unsigned int tag;
00237     int64_t size;
00238 
00239     for (;;) {
00240         if (pb->eof_reached)
00241             return -1;
00242         size = next_tag(pb, &tag);
00243         if (tag == tag1)
00244             break;
00245         avio_skip(pb, size);
00246     }
00247     return size;
00248 }
00249 
00250 static int wav_probe(AVProbeData *p)
00251 {
00252     /* check file header */
00253     if (p->buf_size <= 32)
00254         return 0;
00255     if (!memcmp(p->buf + 8, "WAVE", 4)) {
00256         if (!memcmp(p->buf, "RIFF", 4))
00257             /*
00258               Since ACT demuxer has standard WAV header at top of it's own,
00259               returning score is decreased to avoid probe conflict
00260               between ACT and WAV.
00261             */
00262             return AVPROBE_SCORE_MAX - 1;
00263         else if (!memcmp(p->buf,      "RF64", 4) &&
00264                  !memcmp(p->buf + 12, "ds64", 4))
00265             return AVPROBE_SCORE_MAX;
00266     }
00267     return 0;
00268 }
00269 
00270 static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
00271 {
00272     AVIOContext *pb = s->pb;
00273     int ret;
00274 
00275     /* parse fmt header */
00276     *st = avformat_new_stream(s, NULL);
00277     if (!*st)
00278         return AVERROR(ENOMEM);
00279 
00280     ret = ff_get_wav_header(pb, (*st)->codec, size);
00281     if (ret < 0)
00282         return ret;
00283     (*st)->need_parsing = AVSTREAM_PARSE_FULL;
00284 
00285     avpriv_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
00286 
00287     return 0;
00288 }
00289 
00290 static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
00291                                         int length)
00292 {
00293     char temp[257];
00294     int ret;
00295 
00296     av_assert0(length <= sizeof(temp));
00297     if ((ret = avio_read(s->pb, temp, length)) < 0)
00298         return ret;
00299 
00300     temp[length] = 0;
00301 
00302     if (strlen(temp))
00303         return av_dict_set(&s->metadata, key, temp, 0);
00304 
00305     return 0;
00306 }
00307 
00308 static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
00309 {
00310     char temp[131], *coding_history;
00311     int ret, x;
00312     uint64_t time_reference;
00313     int64_t umid_parts[8], umid_mask = 0;
00314 
00315     if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
00316         (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
00317         (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
00318         (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
00319         (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
00320         return ret;
00321 
00322     time_reference = avio_rl64(s->pb);
00323     snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
00324     if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
00325         return ret;
00326 
00327     /* check if version is >= 1, in which case an UMID may be present */
00328     if (avio_rl16(s->pb) >= 1) {
00329         for (x = 0; x < 8; x++)
00330             umid_mask |= umid_parts[x] = avio_rb64(s->pb);
00331 
00332         if (umid_mask) {
00333             /* the string formatting below is per SMPTE 330M-2004 Annex C */
00334             if (umid_parts[4] == 0 && umid_parts[5] == 0 && umid_parts[6] == 0 && umid_parts[7] == 0) {
00335                 /* basic UMID */
00336                 snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
00337                          umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3]);
00338             } else {
00339                 /* extended UMID */
00340                 snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
00341                                              "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
00342                          umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3],
00343                          umid_parts[4], umid_parts[5], umid_parts[6], umid_parts[7]);
00344             }
00345 
00346             if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
00347                 return ret;
00348         }
00349 
00350         avio_skip(s->pb, 190);
00351     } else
00352         avio_skip(s->pb, 254);
00353 
00354     if (size > 602) {
00355         /* CodingHistory present */
00356         size -= 602;
00357 
00358         if (!(coding_history = av_malloc(size+1)))
00359             return AVERROR(ENOMEM);
00360 
00361         if ((ret = avio_read(s->pb, coding_history, size)) < 0)
00362             return ret;
00363 
00364         coding_history[size] = 0;
00365         if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
00366                                AV_DICT_DONT_STRDUP_VAL)) < 0)
00367             return ret;
00368     }
00369 
00370     return 0;
00371 }
00372 
00373 static const AVMetadataConv wav_metadata_conv[] = {
00374     {"description",      "comment"      },
00375     {"originator",       "encoded_by"   },
00376     {"origination_date", "date"         },
00377     {"origination_time", "creation_time"},
00378     {0},
00379 };
00380 
00381 /* wav input */
00382 static int wav_read_header(AVFormatContext *s,
00383                            AVFormatParameters *ap)
00384 {
00385     int64_t size, av_uninit(data_size);
00386     int64_t sample_count=0;
00387     int rf64;
00388     uint32_t tag, list_type;
00389     AVIOContext *pb = s->pb;
00390     AVStream *st;
00391     WAVContext *wav = s->priv_data;
00392     int ret, got_fmt = 0;
00393     int64_t next_tag_ofs, data_ofs = -1;
00394 
00395     /* check RIFF header */
00396     tag = avio_rl32(pb);
00397 
00398     rf64 = tag == MKTAG('R', 'F', '6', '4');
00399     if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
00400         return -1;
00401     avio_rl32(pb); /* file size */
00402     tag = avio_rl32(pb);
00403     if (tag != MKTAG('W', 'A', 'V', 'E'))
00404         return -1;
00405 
00406     if (rf64) {
00407         if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
00408             return -1;
00409         size = avio_rl32(pb);
00410         if (size < 16)
00411             return -1;
00412         avio_rl64(pb); /* RIFF size */
00413         data_size = avio_rl64(pb);
00414         sample_count = avio_rl64(pb);
00415         if (data_size < 0 || sample_count < 0) {
00416             av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
00417                    "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
00418                    data_size, sample_count);
00419             return AVERROR_INVALIDDATA;
00420         }
00421         avio_skip(pb, size - 16); /* skip rest of ds64 chunk */
00422     }
00423 
00424     for (;;) {
00425         size = next_tag(pb, &tag);
00426         next_tag_ofs = avio_tell(pb) + size;
00427 
00428         if (pb->eof_reached)
00429             break;
00430 
00431         switch (tag) {
00432         case MKTAG('f', 'm', 't', ' '):
00433             /* only parse the first 'fmt ' tag found */
00434             if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st) < 0)) {
00435                 return ret;
00436             } else if (got_fmt)
00437                 av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
00438 
00439             got_fmt = 1;
00440             break;
00441         case MKTAG('d', 'a', 't', 'a'):
00442             if (!got_fmt) {
00443                 av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
00444                 return AVERROR_INVALIDDATA;
00445             }
00446 
00447             if (rf64) {
00448                 next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
00449             } else {
00450                 data_size = size;
00451                 next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
00452             }
00453 
00454             data_ofs = avio_tell(pb);
00455 
00456             /* don't look for footer metadata if we can't seek or if we don't
00457              * know where the data tag ends
00458              */
00459             if (!pb->seekable || (!rf64 && !size))
00460                 goto break_loop;
00461             break;
00462         case MKTAG('f','a','c','t'):
00463             if (!sample_count)
00464                 sample_count = avio_rl32(pb);
00465             break;
00466         case MKTAG('b','e','x','t'):
00467             if ((ret = wav_parse_bext_tag(s, size)) < 0)
00468                 return ret;
00469             break;
00470         case MKTAG('L', 'I', 'S', 'T'):
00471             list_type = avio_rl32(pb);
00472             if (size < 4) {
00473                 av_log(s, AV_LOG_ERROR, "too short LIST");
00474                 return AVERROR_INVALIDDATA;
00475             }
00476             switch (list_type) {
00477             case MKTAG('I', 'N', 'F', 'O'):
00478                 if ((ret = ff_read_riff_info(s, size - 4)) < 0)
00479                     return ret;
00480             }
00481             break;
00482         }
00483 
00484         /* seek to next tag unless we know that we'll run into EOF */
00485         if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
00486             avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) {
00487             break;
00488         }
00489     }
00490 break_loop:
00491     if (data_ofs < 0) {
00492         av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
00493         return AVERROR_INVALIDDATA;
00494     }
00495 
00496     avio_seek(pb, data_ofs, SEEK_SET);
00497 
00498     if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
00499         sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
00500     if (sample_count)
00501         st->duration = sample_count;
00502 
00503     ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
00504     ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
00505 
00506     return 0;
00507 }
00508 
00512 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
00513 {
00514     uint8_t guid[16];
00515     int64_t size;
00516 
00517     while (!pb->eof_reached) {
00518         avio_read(pb, guid, 16);
00519         size = avio_rl64(pb);
00520         if (size <= 24)
00521             return -1;
00522         if (!memcmp(guid, guid1, 16))
00523             return size;
00524         avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
00525     }
00526     return -1;
00527 }
00528 
00529 static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
00530     0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
00531 
00532 #define MAX_SIZE 4096
00533 
00534 static int wav_read_packet(AVFormatContext *s,
00535                            AVPacket *pkt)
00536 {
00537     int ret, size;
00538     int64_t left;
00539     AVStream *st;
00540     WAVContext *wav = s->priv_data;
00541 
00542     st = s->streams[0];
00543 
00544     left = wav->data_end - avio_tell(s->pb);
00545     if (left <= 0){
00546         if (CONFIG_W64_DEMUXER && wav->w64)
00547             left = find_guid(s->pb, guid_data) - 24;
00548         else
00549             left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
00550         if (left < 0)
00551             return AVERROR_EOF;
00552         wav->data_end= avio_tell(s->pb) + left;
00553     }
00554 
00555     size = MAX_SIZE;
00556     if (st->codec->block_align > 1) {
00557         if (size < st->codec->block_align)
00558             size = st->codec->block_align;
00559         size = (size / st->codec->block_align) * st->codec->block_align;
00560     }
00561     size = FFMIN(size, left);
00562     ret  = av_get_packet(s->pb, pkt, size);
00563     if (ret < 0)
00564         return ret;
00565     pkt->stream_index = 0;
00566 
00567     return ret;
00568 }
00569 
00570 static int wav_read_seek(AVFormatContext *s,
00571                          int stream_index, int64_t timestamp, int flags)
00572 {
00573     AVStream *st;
00574 
00575     st = s->streams[0];
00576     switch (st->codec->codec_id) {
00577     case CODEC_ID_MP2:
00578     case CODEC_ID_MP3:
00579     case CODEC_ID_AC3:
00580     case CODEC_ID_DTS:
00581         /* use generic seeking with dynamically generated indexes */
00582         return -1;
00583     default:
00584         break;
00585     }
00586     return pcm_read_seek(s, stream_index, timestamp, flags);
00587 }
00588 
00589 AVInputFormat ff_wav_demuxer = {
00590     .name           = "wav",
00591     .long_name      = NULL_IF_CONFIG_SMALL("WAV format"),
00592     .priv_data_size = sizeof(WAVContext),
00593     .read_probe     = wav_probe,
00594     .read_header    = wav_read_header,
00595     .read_packet    = wav_read_packet,
00596     .read_seek      = wav_read_seek,
00597     .flags= AVFMT_GENERIC_INDEX,
00598     .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
00599 };
00600 #endif /* CONFIG_WAV_DEMUXER */
00601 
00602 
00603 #if CONFIG_W64_DEMUXER
00604 static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
00605     0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
00606 
00607 static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
00608     0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
00609 
00610 static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
00611     0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
00612 
00613 static int w64_probe(AVProbeData *p)
00614 {
00615     if (p->buf_size <= 40)
00616         return 0;
00617     if (!memcmp(p->buf,      guid_riff, 16) &&
00618         !memcmp(p->buf + 24, guid_wave, 16))
00619         return AVPROBE_SCORE_MAX;
00620     else
00621         return 0;
00622 }
00623 
00624 static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
00625 {
00626     int64_t size;
00627     AVIOContext *pb  = s->pb;
00628     WAVContext    *wav = s->priv_data;
00629     AVStream *st;
00630     uint8_t guid[16];
00631     int ret;
00632 
00633     avio_read(pb, guid, 16);
00634     if (memcmp(guid, guid_riff, 16))
00635         return -1;
00636 
00637     if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
00638         return -1;
00639 
00640     avio_read(pb, guid, 16);
00641     if (memcmp(guid, guid_wave, 16)) {
00642         av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
00643         return -1;
00644     }
00645 
00646     size = find_guid(pb, guid_fmt);
00647     if (size < 0) {
00648         av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
00649         return -1;
00650     }
00651 
00652     st = avformat_new_stream(s, NULL);
00653     if (!st)
00654         return AVERROR(ENOMEM);
00655 
00656     /* subtract chunk header size - normal wav file doesn't count it */
00657     ret = ff_get_wav_header(pb, st->codec, size - 24);
00658     if (ret < 0)
00659         return ret;
00660     avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
00661 
00662     st->need_parsing = AVSTREAM_PARSE_FULL;
00663 
00664     avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00665 
00666     size = find_guid(pb, guid_data);
00667     if (size < 0) {
00668         av_log(s, AV_LOG_ERROR, "could not find data guid\n");
00669         return -1;
00670     }
00671     wav->data_end = avio_tell(pb) + size - 24;
00672     wav->w64      = 1;
00673 
00674     return 0;
00675 }
00676 
00677 AVInputFormat ff_w64_demuxer = {
00678     .name           = "w64",
00679     .long_name      = NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
00680     .priv_data_size = sizeof(WAVContext),
00681     .read_probe     = w64_probe,
00682     .read_header    = w64_read_header,
00683     .read_packet    = wav_read_packet,
00684     .read_seek      = wav_read_seek,
00685     .flags = AVFMT_GENERIC_INDEX,
00686     .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
00687 };
00688 #endif /* CONFIG_W64_DEMUXER */