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 /* RIFF chunks are always on a even offset. */
00234 static int64_t wav_seek_tag(AVIOContext *s, int64_t offset, int whence)
00235 {
00236     return avio_seek(s, offset + (offset & 1), whence);
00237 }
00238 
00239 /* return the size of the found tag */
00240 static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
00241 {
00242     unsigned int tag;
00243     int64_t size;
00244 
00245     for (;;) {
00246         if (pb->eof_reached)
00247             return -1;
00248         size = next_tag(pb, &tag);
00249         if (tag == tag1)
00250             break;
00251         wav_seek_tag(pb, size, SEEK_CUR);
00252     }
00253     return size;
00254 }
00255 
00256 static int wav_probe(AVProbeData *p)
00257 {
00258     /* check file header */
00259     if (p->buf_size <= 32)
00260         return 0;
00261     if (!memcmp(p->buf + 8, "WAVE", 4)) {
00262         if (!memcmp(p->buf, "RIFF", 4))
00263             /*
00264               Since ACT demuxer has standard WAV header at top of it's own,
00265               returning score is decreased to avoid probe conflict
00266               between ACT and WAV.
00267             */
00268             return AVPROBE_SCORE_MAX - 1;
00269         else if (!memcmp(p->buf,      "RF64", 4) &&
00270                  !memcmp(p->buf + 12, "ds64", 4))
00271             return AVPROBE_SCORE_MAX;
00272     }
00273     return 0;
00274 }
00275 
00276 static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
00277 {
00278     AVIOContext *pb = s->pb;
00279     int ret;
00280 
00281     /* parse fmt header */
00282     *st = avformat_new_stream(s, NULL);
00283     if (!*st)
00284         return AVERROR(ENOMEM);
00285 
00286     ret = ff_get_wav_header(pb, (*st)->codec, size);
00287     if (ret < 0)
00288         return ret;
00289     (*st)->need_parsing = AVSTREAM_PARSE_FULL;
00290 
00291     avpriv_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
00292 
00293     return 0;
00294 }
00295 
00296 static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
00297                                         int length)
00298 {
00299     char temp[257];
00300     int ret;
00301 
00302     av_assert0(length <= sizeof(temp));
00303     if ((ret = avio_read(s->pb, temp, length)) < 0)
00304         return ret;
00305 
00306     temp[length] = 0;
00307 
00308     if (strlen(temp))
00309         return av_dict_set(&s->metadata, key, temp, 0);
00310 
00311     return 0;
00312 }
00313 
00314 static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
00315 {
00316     char temp[131], *coding_history;
00317     int ret, x;
00318     uint64_t time_reference;
00319     int64_t umid_parts[8], umid_mask = 0;
00320 
00321     if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
00322         (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
00323         (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
00324         (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
00325         (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
00326         return ret;
00327 
00328     time_reference = avio_rl64(s->pb);
00329     snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
00330     if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
00331         return ret;
00332 
00333     /* check if version is >= 1, in which case an UMID may be present */
00334     if (avio_rl16(s->pb) >= 1) {
00335         for (x = 0; x < 8; x++)
00336             umid_mask |= umid_parts[x] = avio_rb64(s->pb);
00337 
00338         if (umid_mask) {
00339             /* the string formatting below is per SMPTE 330M-2004 Annex C */
00340             if (umid_parts[4] == 0 && umid_parts[5] == 0 && umid_parts[6] == 0 && umid_parts[7] == 0) {
00341                 /* basic UMID */
00342                 snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
00343                          umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3]);
00344             } else {
00345                 /* extended UMID */
00346                 snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
00347                                              "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
00348                          umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3],
00349                          umid_parts[4], umid_parts[5], umid_parts[6], umid_parts[7]);
00350             }
00351 
00352             if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
00353                 return ret;
00354         }
00355 
00356         avio_skip(s->pb, 190);
00357     } else
00358         avio_skip(s->pb, 254);
00359 
00360     if (size > 602) {
00361         /* CodingHistory present */
00362         size -= 602;
00363 
00364         if (!(coding_history = av_malloc(size+1)))
00365             return AVERROR(ENOMEM);
00366 
00367         if ((ret = avio_read(s->pb, coding_history, size)) < 0)
00368             return ret;
00369 
00370         coding_history[size] = 0;
00371         if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
00372                                AV_DICT_DONT_STRDUP_VAL)) < 0)
00373             return ret;
00374     }
00375 
00376     return 0;
00377 }
00378 
00379 static const AVMetadataConv wav_metadata_conv[] = {
00380     {"description",      "comment"      },
00381     {"originator",       "encoded_by"   },
00382     {"origination_date", "date"         },
00383     {"origination_time", "creation_time"},
00384     {0},
00385 };
00386 
00387 /* wav input */
00388 static int wav_read_header(AVFormatContext *s,
00389                            AVFormatParameters *ap)
00390 {
00391     int64_t size, av_uninit(data_size);
00392     int64_t sample_count=0;
00393     int rf64;
00394     uint32_t tag, list_type;
00395     AVIOContext *pb = s->pb;
00396     AVStream *st;
00397     WAVContext *wav = s->priv_data;
00398     int ret, got_fmt = 0;
00399     int64_t next_tag_ofs, data_ofs = -1;
00400 
00401     /* check RIFF header */
00402     tag = avio_rl32(pb);
00403 
00404     rf64 = tag == MKTAG('R', 'F', '6', '4');
00405     if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
00406         return -1;
00407     avio_rl32(pb); /* file size */
00408     tag = avio_rl32(pb);
00409     if (tag != MKTAG('W', 'A', 'V', 'E'))
00410         return -1;
00411 
00412     if (rf64) {
00413         if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
00414             return -1;
00415         size = avio_rl32(pb);
00416         if (size < 16)
00417             return -1;
00418         avio_rl64(pb); /* RIFF size */
00419         data_size = avio_rl64(pb);
00420         sample_count = avio_rl64(pb);
00421         if (data_size < 0 || sample_count < 0) {
00422             av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
00423                    "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
00424                    data_size, sample_count);
00425             return AVERROR_INVALIDDATA;
00426         }
00427         avio_skip(pb, size - 16); /* skip rest of ds64 chunk */
00428     }
00429 
00430     for (;;) {
00431         size = next_tag(pb, &tag);
00432         next_tag_ofs = avio_tell(pb) + size;
00433 
00434         if (pb->eof_reached)
00435             break;
00436 
00437         switch (tag) {
00438         case MKTAG('f', 'm', 't', ' '):
00439             /* only parse the first 'fmt ' tag found */
00440             if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st) < 0)) {
00441                 return ret;
00442             } else if (got_fmt)
00443                 av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
00444 
00445             got_fmt = 1;
00446             break;
00447         case MKTAG('d', 'a', 't', 'a'):
00448             if (!got_fmt) {
00449                 av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
00450                 return AVERROR_INVALIDDATA;
00451             }
00452 
00453             if (rf64) {
00454                 next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
00455             } else {
00456                 data_size = size;
00457                 next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
00458             }
00459 
00460             data_ofs = avio_tell(pb);
00461 
00462             /* don't look for footer metadata if we can't seek or if we don't
00463              * know where the data tag ends
00464              */
00465             if (!pb->seekable || (!rf64 && !size))
00466                 goto break_loop;
00467             break;
00468         case MKTAG('f','a','c','t'):
00469             if (!sample_count)
00470                 sample_count = avio_rl32(pb);
00471             break;
00472         case MKTAG('b','e','x','t'):
00473             if ((ret = wav_parse_bext_tag(s, size)) < 0)
00474                 return ret;
00475             break;
00476         case MKTAG('L', 'I', 'S', 'T'):
00477             list_type = avio_rl32(pb);
00478             if (size < 4) {
00479                 av_log(s, AV_LOG_ERROR, "too short LIST");
00480                 return AVERROR_INVALIDDATA;
00481             }
00482             switch (list_type) {
00483             case MKTAG('I', 'N', 'F', 'O'):
00484                 if ((ret = ff_read_riff_info(s, size - 4)) < 0)
00485                     return ret;
00486             }
00487             break;
00488         }
00489 
00490         /* seek to next tag unless we know that we'll run into EOF */
00491         if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
00492             wav_seek_tag(pb, next_tag_ofs, SEEK_SET) < 0) {
00493             break;
00494         }
00495     }
00496 break_loop:
00497     if (data_ofs < 0) {
00498         av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
00499         return AVERROR_INVALIDDATA;
00500     }
00501 
00502     avio_seek(pb, data_ofs, SEEK_SET);
00503 
00504     if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
00505         sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
00506     if (sample_count)
00507         st->duration = sample_count;
00508 
00509     ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
00510     ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
00511 
00512     return 0;
00513 }
00514 
00518 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
00519 {
00520     uint8_t guid[16];
00521     int64_t size;
00522 
00523     while (!pb->eof_reached) {
00524         avio_read(pb, guid, 16);
00525         size = avio_rl64(pb);
00526         if (size <= 24)
00527             return -1;
00528         if (!memcmp(guid, guid1, 16))
00529             return size;
00530         avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
00531     }
00532     return -1;
00533 }
00534 
00535 static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
00536     0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
00537 
00538 #define MAX_SIZE 4096
00539 
00540 static int wav_read_packet(AVFormatContext *s,
00541                            AVPacket *pkt)
00542 {
00543     int ret, size;
00544     int64_t left;
00545     AVStream *st;
00546     WAVContext *wav = s->priv_data;
00547 
00548     st = s->streams[0];
00549 
00550     left = wav->data_end - avio_tell(s->pb);
00551     if (left <= 0){
00552         if (CONFIG_W64_DEMUXER && wav->w64)
00553             left = find_guid(s->pb, guid_data) - 24;
00554         else
00555             left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
00556         if (left < 0)
00557             return AVERROR_EOF;
00558         wav->data_end= avio_tell(s->pb) + left;
00559     }
00560 
00561     size = MAX_SIZE;
00562     if (st->codec->block_align > 1) {
00563         if (size < st->codec->block_align)
00564             size = st->codec->block_align;
00565         size = (size / st->codec->block_align) * st->codec->block_align;
00566     }
00567     size = FFMIN(size, left);
00568     ret  = av_get_packet(s->pb, pkt, size);
00569     if (ret < 0)
00570         return ret;
00571     pkt->stream_index = 0;
00572 
00573     return ret;
00574 }
00575 
00576 static int wav_read_seek(AVFormatContext *s,
00577                          int stream_index, int64_t timestamp, int flags)
00578 {
00579     AVStream *st;
00580 
00581     st = s->streams[0];
00582     switch (st->codec->codec_id) {
00583     case CODEC_ID_MP2:
00584     case CODEC_ID_MP3:
00585     case CODEC_ID_AC3:
00586     case CODEC_ID_DTS:
00587         /* use generic seeking with dynamically generated indexes */
00588         return -1;
00589     default:
00590         break;
00591     }
00592     return pcm_read_seek(s, stream_index, timestamp, flags);
00593 }
00594 
00595 AVInputFormat ff_wav_demuxer = {
00596     .name           = "wav",
00597     .long_name      = NULL_IF_CONFIG_SMALL("WAV format"),
00598     .priv_data_size = sizeof(WAVContext),
00599     .read_probe     = wav_probe,
00600     .read_header    = wav_read_header,
00601     .read_packet    = wav_read_packet,
00602     .read_seek      = wav_read_seek,
00603     .flags= AVFMT_GENERIC_INDEX,
00604     .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
00605 };
00606 #endif /* CONFIG_WAV_DEMUXER */
00607 
00608 
00609 #if CONFIG_W64_DEMUXER
00610 static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
00611     0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
00612 
00613 static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
00614     0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
00615 
00616 static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
00617     0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
00618 
00619 static int w64_probe(AVProbeData *p)
00620 {
00621     if (p->buf_size <= 40)
00622         return 0;
00623     if (!memcmp(p->buf,      guid_riff, 16) &&
00624         !memcmp(p->buf + 24, guid_wave, 16))
00625         return AVPROBE_SCORE_MAX;
00626     else
00627         return 0;
00628 }
00629 
00630 static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
00631 {
00632     int64_t size;
00633     AVIOContext *pb  = s->pb;
00634     WAVContext    *wav = s->priv_data;
00635     AVStream *st;
00636     uint8_t guid[16];
00637     int ret;
00638 
00639     avio_read(pb, guid, 16);
00640     if (memcmp(guid, guid_riff, 16))
00641         return -1;
00642 
00643     if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
00644         return -1;
00645 
00646     avio_read(pb, guid, 16);
00647     if (memcmp(guid, guid_wave, 16)) {
00648         av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
00649         return -1;
00650     }
00651 
00652     size = find_guid(pb, guid_fmt);
00653     if (size < 0) {
00654         av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
00655         return -1;
00656     }
00657 
00658     st = avformat_new_stream(s, NULL);
00659     if (!st)
00660         return AVERROR(ENOMEM);
00661 
00662     /* subtract chunk header size - normal wav file doesn't count it */
00663     ret = ff_get_wav_header(pb, st->codec, size - 24);
00664     if (ret < 0)
00665         return ret;
00666     avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
00667 
00668     st->need_parsing = AVSTREAM_PARSE_FULL;
00669 
00670     avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00671 
00672     size = find_guid(pb, guid_data);
00673     if (size < 0) {
00674         av_log(s, AV_LOG_ERROR, "could not find data guid\n");
00675         return -1;
00676     }
00677     wav->data_end = avio_tell(pb) + size - 24;
00678     wav->w64      = 1;
00679 
00680     return 0;
00681 }
00682 
00683 AVInputFormat ff_w64_demuxer = {
00684     .name           = "w64",
00685     .long_name      = NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
00686     .priv_data_size = sizeof(WAVContext),
00687     .read_probe     = w64_probe,
00688     .read_header    = w64_read_header,
00689     .read_packet    = wav_read_packet,
00690     .read_seek      = wav_read_seek,
00691     .flags = AVFMT_GENERIC_INDEX,
00692     .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
00693 };
00694 #endif /* CONFIG_W64_DEMUXER */