• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

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 FFmpeg.
00010  *
00011  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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     int64_t smv_data_ofs;
00049     int smv_block_size;
00050     int smv_frames_per_jpeg;
00051     int smv_block;
00052     int smv_last_stream;
00053     int smv_eof;
00054     int audio_eof;
00055     int ignore_length;
00056 } WAVContext;
00057 
00058 #if CONFIG_WAV_MUXER
00059 static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
00060 {
00061     AVDictionaryEntry *tag;
00062     int len = 0;
00063 
00064     if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
00065         len = strlen(tag->value);
00066         len = FFMIN(len, maxlen);
00067         avio_write(s->pb, tag->value, len);
00068     }
00069 
00070     ffio_fill(s->pb, 0, maxlen - len);
00071 }
00072 
00073 static void bwf_write_bext_chunk(AVFormatContext *s)
00074 {
00075     AVDictionaryEntry *tmp_tag;
00076     uint64_t time_reference = 0;
00077     int64_t bext = ff_start_tag(s->pb, "bext");
00078 
00079     bwf_write_bext_string(s, "description", 256);
00080     bwf_write_bext_string(s, "originator", 32);
00081     bwf_write_bext_string(s, "originator_reference", 32);
00082     bwf_write_bext_string(s, "origination_date", 10);
00083     bwf_write_bext_string(s, "origination_time", 8);
00084 
00085     if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
00086         time_reference = strtoll(tmp_tag->value, NULL, 10);
00087     avio_wl64(s->pb, time_reference);
00088     avio_wl16(s->pb, 1);  // set version to 1
00089 
00090     if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
00091         unsigned char umidpart_str[17] = {0};
00092         int i;
00093         uint64_t umidpart;
00094         int len = strlen(tmp_tag->value+2);
00095 
00096         for (i = 0; i < len/16; i++) {
00097             memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
00098             umidpart = strtoll(umidpart_str, NULL, 16);
00099             avio_wb64(s->pb, umidpart);
00100         }
00101         ffio_fill(s->pb, 0, 64 - i*8);
00102     } else
00103         ffio_fill(s->pb, 0, 64); // zero UMID
00104 
00105     ffio_fill(s->pb, 0, 190); // Reserved
00106 
00107     if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
00108         avio_put_str(s->pb, tmp_tag->value);
00109 
00110     ff_end_tag(s->pb, bext);
00111 }
00112 
00113 static int wav_write_header(AVFormatContext *s)
00114 {
00115     WAVContext *wav = s->priv_data;
00116     AVIOContext *pb = s->pb;
00117     int64_t fmt, fact;
00118 
00119     ffio_wfourcc(pb, "RIFF");
00120     avio_wl32(pb, 0); /* file length */
00121     ffio_wfourcc(pb, "WAVE");
00122 
00123     /* format header */
00124     fmt = ff_start_tag(pb, "fmt ");
00125     if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
00126         av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
00127                s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
00128         return -1;
00129     }
00130     ff_end_tag(pb, fmt);
00131 
00132     if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
00133         && s->pb->seekable) {
00134         fact = ff_start_tag(pb, "fact");
00135         avio_wl32(pb, 0);
00136         ff_end_tag(pb, fact);
00137     }
00138 
00139     if (wav->write_bext)
00140         bwf_write_bext_chunk(s);
00141 
00142     avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
00143     wav->maxpts = wav->last_duration = 0;
00144     wav->minpts = INT64_MAX;
00145 
00146     /* data header */
00147     wav->data = ff_start_tag(pb, "data");
00148 
00149     avio_flush(pb);
00150 
00151     return 0;
00152 }
00153 
00154 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
00155 {
00156     AVIOContext *pb  = s->pb;
00157     WAVContext    *wav = s->priv_data;
00158     avio_write(pb, pkt->data, pkt->size);
00159     if(pkt->pts != AV_NOPTS_VALUE) {
00160         wav->minpts        = FFMIN(wav->minpts, pkt->pts);
00161         wav->maxpts        = FFMAX(wav->maxpts, pkt->pts);
00162         wav->last_duration = pkt->duration;
00163     } else
00164         av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
00165     return 0;
00166 }
00167 
00168 static int wav_write_trailer(AVFormatContext *s)
00169 {
00170     AVIOContext *pb  = s->pb;
00171     WAVContext    *wav = s->priv_data;
00172     int64_t file_size;
00173 
00174     avio_flush(pb);
00175 
00176     if (s->pb->seekable) {
00177         ff_end_tag(pb, wav->data);
00178 
00179         /* update file size */
00180         file_size = avio_tell(pb);
00181         avio_seek(pb, 4, SEEK_SET);
00182         avio_wl32(pb, (uint32_t)(file_size - 8));
00183         avio_seek(pb, file_size, SEEK_SET);
00184 
00185         avio_flush(pb);
00186 
00187         if(s->streams[0]->codec->codec_tag != 0x01) {
00188             /* Update num_samps in fact chunk */
00189             int number_of_samples;
00190             number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
00191                                            s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
00192                                            s->streams[0]->time_base.den);
00193             avio_seek(pb, wav->data-12, SEEK_SET);
00194             avio_wl32(pb, number_of_samples);
00195             avio_seek(pb, file_size, SEEK_SET);
00196             avio_flush(pb);
00197         }
00198     }
00199     return 0;
00200 }
00201 
00202 #define OFFSET(x) offsetof(WAVContext, x)
00203 #define ENC AV_OPT_FLAG_ENCODING_PARAM
00204 static const AVOption options[] = {
00205     { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { 0 }, 0, 1, ENC },
00206     { NULL },
00207 };
00208 
00209 static const AVClass wav_muxer_class = {
00210     .class_name = "WAV muxer",
00211     .item_name  = av_default_item_name,
00212     .option     = options,
00213     .version    = LIBAVUTIL_VERSION_INT,
00214 };
00215 
00216 AVOutputFormat ff_wav_muxer = {
00217     .name              = "wav",
00218     .long_name         = NULL_IF_CONFIG_SMALL("WAV format"),
00219     .mime_type         = "audio/x-wav",
00220     .extensions        = "wav",
00221     .priv_data_size    = sizeof(WAVContext),
00222     .audio_codec       = CODEC_ID_PCM_S16LE,
00223     .video_codec       = CODEC_ID_NONE,
00224     .write_header      = wav_write_header,
00225     .write_packet      = wav_write_packet,
00226     .write_trailer     = wav_write_trailer,
00227     .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
00228     .priv_class = &wav_muxer_class,
00229 };
00230 #endif /* CONFIG_WAV_MUXER */
00231 
00232 
00233 #if CONFIG_WAV_DEMUXER
00234 
00235 static int64_t next_tag(AVIOContext *pb, uint32_t *tag)
00236 {
00237     *tag = avio_rl32(pb);
00238     return avio_rl32(pb);
00239 }
00240 
00241 /* return the size of the found tag */
00242 static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
00243 {
00244     unsigned int tag;
00245     int64_t size;
00246 
00247     for (;;) {
00248         if (url_feof(pb))
00249             return -1;
00250         size = next_tag(pb, &tag);
00251         if (tag == tag1)
00252             break;
00253         avio_skip(pb, size);
00254     }
00255     return size;
00256 }
00257 
00258 static int wav_probe(AVProbeData *p)
00259 {
00260     /* check file header */
00261     if (p->buf_size <= 32)
00262         return 0;
00263     if (!memcmp(p->buf + 8, "WAVE", 4)) {
00264         if (!memcmp(p->buf, "RIFF", 4))
00265             /*
00266               Since ACT demuxer has standard WAV header at top of it's own,
00267               returning score is decreased to avoid probe conflict
00268               between ACT and WAV.
00269             */
00270             return AVPROBE_SCORE_MAX - 1;
00271         else if (!memcmp(p->buf,      "RF64", 4) &&
00272                  !memcmp(p->buf + 12, "ds64", 4))
00273             return AVPROBE_SCORE_MAX;
00274     }
00275     return 0;
00276 }
00277 
00278 static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
00279 {
00280     AVIOContext *pb = s->pb;
00281     int ret;
00282 
00283     /* parse fmt header */
00284     *st = avformat_new_stream(s, NULL);
00285     if (!*st)
00286         return AVERROR(ENOMEM);
00287 
00288     ret = ff_get_wav_header(pb, (*st)->codec, size);
00289     if (ret < 0)
00290         return ret;
00291     (*st)->need_parsing = AVSTREAM_PARSE_FULL;
00292 
00293     avpriv_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
00294 
00295     return 0;
00296 }
00297 
00298 static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
00299                                         int length)
00300 {
00301     char temp[257];
00302     int ret;
00303 
00304     av_assert0(length <= sizeof(temp));
00305     if ((ret = avio_read(s->pb, temp, length)) < 0)
00306         return ret;
00307 
00308     temp[length] = 0;
00309 
00310     if (strlen(temp))
00311         return av_dict_set(&s->metadata, key, temp, 0);
00312 
00313     return 0;
00314 }
00315 
00316 static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
00317 {
00318     char temp[131], *coding_history;
00319     int ret, x;
00320     uint64_t time_reference;
00321     int64_t umid_parts[8], umid_mask = 0;
00322 
00323     if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
00324         (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
00325         (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
00326         (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
00327         (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
00328         return ret;
00329 
00330     time_reference = avio_rl64(s->pb);
00331     snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
00332     if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
00333         return ret;
00334 
00335     /* check if version is >= 1, in which case an UMID may be present */
00336     if (avio_rl16(s->pb) >= 1) {
00337         for (x = 0; x < 8; x++)
00338             umid_mask |= umid_parts[x] = avio_rb64(s->pb);
00339 
00340         if (umid_mask) {
00341             /* the string formatting below is per SMPTE 330M-2004 Annex C */
00342             if (umid_parts[4] == 0 && umid_parts[5] == 0 && umid_parts[6] == 0 && umid_parts[7] == 0) {
00343                 /* basic UMID */
00344                 snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
00345                          umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3]);
00346             } else {
00347                 /* extended UMID */
00348                 snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
00349                                                "%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
00350                          umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3],
00351                          umid_parts[4], umid_parts[5], umid_parts[6], umid_parts[7]);
00352             }
00353 
00354             if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
00355                 return ret;
00356         }
00357 
00358         avio_skip(s->pb, 190);
00359     } else
00360         avio_skip(s->pb, 254);
00361 
00362     if (size > 602) {
00363         /* CodingHistory present */
00364         size -= 602;
00365 
00366         if (!(coding_history = av_malloc(size+1)))
00367             return AVERROR(ENOMEM);
00368 
00369         if ((ret = avio_read(s->pb, coding_history, size)) < 0)
00370             return ret;
00371 
00372         coding_history[size] = 0;
00373         if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
00374                                AV_DICT_DONT_STRDUP_VAL)) < 0)
00375             return ret;
00376     }
00377 
00378     return 0;
00379 }
00380 
00381 static const AVMetadataConv wav_metadata_conv[] = {
00382     {"description",      "comment"      },
00383     {"originator",       "encoded_by"   },
00384     {"origination_date", "date"         },
00385     {"origination_time", "creation_time"},
00386     {0},
00387 };
00388 
00389 /* wav input */
00390 static int wav_read_header(AVFormatContext *s,
00391                            AVFormatParameters *ap)
00392 {
00393     int64_t size, av_uninit(data_size);
00394     int64_t sample_count=0;
00395     int rf64;
00396     uint32_t tag, list_type;
00397     AVIOContext *pb = s->pb;
00398     AVStream *st = NULL;
00399     WAVContext *wav = s->priv_data;
00400     int ret, got_fmt = 0;
00401     int64_t next_tag_ofs, data_ofs = -1;
00402 
00403     wav->smv_data_ofs = -1;
00404 
00405     /* check RIFF header */
00406     tag = avio_rl32(pb);
00407 
00408     rf64 = tag == MKTAG('R', 'F', '6', '4');
00409     if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
00410         return -1;
00411     avio_rl32(pb); /* file size */
00412     tag = avio_rl32(pb);
00413     if (tag != MKTAG('W', 'A', 'V', 'E'))
00414         return -1;
00415 
00416     if (rf64) {
00417         if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
00418             return -1;
00419         size = avio_rl32(pb);
00420         if (size < 24)
00421             return -1;
00422         avio_rl64(pb); /* RIFF size */
00423         data_size = avio_rl64(pb);
00424         sample_count = avio_rl64(pb);
00425         if (data_size < 0 || sample_count < 0) {
00426             av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
00427                    "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
00428                    data_size, sample_count);
00429             return AVERROR_INVALIDDATA;
00430         }
00431         avio_skip(pb, size - 24); /* skip rest of ds64 chunk */
00432 
00433     }
00434 
00435     for (;;) {
00436         AVStream *vst;
00437         size = next_tag(pb, &tag);
00438         next_tag_ofs = avio_tell(pb) + size;
00439 
00440         if (url_feof(pb))
00441             break;
00442 
00443         switch (tag) {
00444         case MKTAG('f', 'm', 't', ' '):
00445             /* only parse the first 'fmt ' tag found */
00446             if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st)) < 0) {
00447                 return ret;
00448             } else if (got_fmt)
00449                 av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
00450 
00451             got_fmt = 1;
00452             break;
00453         case MKTAG('d', 'a', 't', 'a'):
00454             if (!got_fmt) {
00455                 av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
00456                 return AVERROR_INVALIDDATA;
00457             }
00458 
00459             if (rf64) {
00460                 next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
00461             } else {
00462                 data_size = size;
00463                 next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
00464             }
00465 
00466             data_ofs = avio_tell(pb);
00467 
00468             /* don't look for footer metadata if we can't seek or if we don't
00469              * know where the data tag ends
00470              */
00471             if (!pb->seekable || (!rf64 && !size))
00472                 goto break_loop;
00473             break;
00474         case MKTAG('f','a','c','t'):
00475             if (!sample_count)
00476                 sample_count = avio_rl32(pb);
00477             break;
00478         case MKTAG('b','e','x','t'):
00479             if ((ret = wav_parse_bext_tag(s, size)) < 0)
00480                 return ret;
00481             break;
00482         case MKTAG('S','M','V','0'):
00483             // SMV file, a wav file with video appended.
00484             if (size != MKTAG('0','2','0','0')) {
00485                 av_log(s, AV_LOG_ERROR, "Unknown SMV version found\n");
00486                 goto break_loop;
00487             }
00488             av_log(s, AV_LOG_DEBUG, "Found SMV data\n");
00489             vst = avformat_new_stream(s, NULL);
00490             if (!vst)
00491                 return AVERROR(ENOMEM);
00492             avio_r8(pb);
00493             vst->id = 1;
00494             vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00495             vst->codec->codec_id = CODEC_ID_MJPEG;
00496             vst->codec->width  = avio_rl24(pb);
00497             vst->codec->height = avio_rl24(pb);
00498             size = avio_rl24(pb);
00499             wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
00500             avio_rl24(pb);
00501             wav->smv_block_size = avio_rl24(pb);
00502             avpriv_set_pts_info(vst, 32, 1, avio_rl24(pb));
00503             vst->duration = avio_rl24(pb);
00504             avio_rl24(pb);
00505             avio_rl24(pb);
00506             wav->smv_frames_per_jpeg = avio_rl24(pb);
00507             goto break_loop;
00508         case MKTAG('L', 'I', 'S', 'T'):
00509             list_type = avio_rl32(pb);
00510             if (size < 4) {
00511                 av_log(s, AV_LOG_ERROR, "too short LIST");
00512                 return AVERROR_INVALIDDATA;
00513             }
00514             switch (list_type) {
00515             case MKTAG('I', 'N', 'F', 'O'):
00516                 if ((ret = ff_read_riff_info(s, size - 4)) < 0)
00517                     return ret;
00518             }
00519             break;
00520         }
00521 
00522         /* seek to next tag unless we know that we'll run into EOF */
00523         if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
00524             avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) {
00525             break;
00526         }
00527     }
00528 break_loop:
00529     if (data_ofs < 0) {
00530         av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
00531         return AVERROR_INVALIDDATA;
00532     }
00533 
00534     avio_seek(pb, data_ofs, SEEK_SET);
00535 
00536     if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
00537         sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
00538     if (sample_count)
00539         st->duration = sample_count;
00540 
00541     ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
00542     ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
00543 
00544     return 0;
00545 }
00546 
00550 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
00551 {
00552     uint8_t guid[16];
00553     int64_t size;
00554 
00555     while (!url_feof(pb)) {
00556         avio_read(pb, guid, 16);
00557         size = avio_rl64(pb);
00558         if (size <= 24)
00559             return -1;
00560         if (!memcmp(guid, guid1, 16))
00561             return size;
00562         avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
00563     }
00564     return -1;
00565 }
00566 
00567 static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
00568     0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
00569 
00570 #define MAX_SIZE 4096
00571 
00572 static int wav_read_packet(AVFormatContext *s,
00573                            AVPacket *pkt)
00574 {
00575     int ret, size;
00576     int64_t left;
00577     AVStream *st;
00578     WAVContext *wav = s->priv_data;
00579 
00580     if (wav->smv_data_ofs > 0) {
00581         int64_t audio_dts, video_dts;
00582 smv_retry:
00583         audio_dts = s->streams[0]->cur_dts;
00584         video_dts = s->streams[1]->cur_dts;
00585         if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
00586             audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q);
00587             video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q);
00588             wav->smv_last_stream = video_dts >= audio_dts;
00589         }
00590         wav->smv_last_stream = !wav->smv_last_stream;
00591         wav->smv_last_stream |= wav->audio_eof;
00592         wav->smv_last_stream &= !wav->smv_eof;
00593         if (wav->smv_last_stream) {
00594             uint64_t old_pos = avio_tell(s->pb);
00595             uint64_t new_pos = wav->smv_data_ofs +
00596                 wav->smv_block * wav->smv_block_size;
00597             if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
00598                 ret = AVERROR_EOF;
00599                 goto smv_out;
00600             }
00601             size = avio_rl24(s->pb);
00602             ret  = av_get_packet(s->pb, pkt, size);
00603             if (ret < 0)
00604                 goto smv_out;
00605             pkt->pos -= 3;
00606             pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg;
00607             wav->smv_block++;
00608             pkt->stream_index = 1;
00609 smv_out:
00610             avio_seek(s->pb, old_pos, SEEK_SET);
00611             if (ret == AVERROR_EOF) {
00612                 wav->smv_eof = 1;
00613                 goto smv_retry;
00614             }
00615             return ret;
00616         }
00617     }
00618 
00619     st = s->streams[0];
00620 
00621     left = wav->data_end - avio_tell(s->pb);
00622     if (wav->ignore_length)
00623         left= INT_MAX;
00624     if (left <= 0){
00625         if (CONFIG_W64_DEMUXER && wav->w64)
00626             left = find_guid(s->pb, guid_data) - 24;
00627         else
00628             left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
00629         if (left < 0) {
00630             wav->audio_eof = 1;
00631             if (wav->smv_data_ofs > 0 && !wav->smv_eof)
00632                 goto smv_retry;
00633             return AVERROR_EOF;
00634         }
00635         wav->data_end= avio_tell(s->pb) + left;
00636     }
00637 
00638     size = MAX_SIZE;
00639     if (st->codec->block_align > 1) {
00640         if (size < st->codec->block_align)
00641             size = st->codec->block_align;
00642         size = (size / st->codec->block_align) * st->codec->block_align;
00643     }
00644     size = FFMIN(size, left);
00645     ret  = av_get_packet(s->pb, pkt, size);
00646     if (ret < 0)
00647         return ret;
00648     pkt->stream_index = 0;
00649 
00650     return ret;
00651 }
00652 
00653 static int wav_read_seek(AVFormatContext *s,
00654                          int stream_index, int64_t timestamp, int flags)
00655 {
00656     WAVContext *wav = s->priv_data;
00657     AVStream *st;
00658     wav->smv_eof = 0;
00659     wav->audio_eof = 0;
00660     if (wav->smv_data_ofs > 0) {
00661         int64_t smv_timestamp = timestamp;
00662         if (stream_index == 0)
00663             smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
00664         else
00665             timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
00666         wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
00667     }
00668 
00669     st = s->streams[0];
00670     switch (st->codec->codec_id) {
00671     case CODEC_ID_MP2:
00672     case CODEC_ID_MP3:
00673     case CODEC_ID_AC3:
00674     case CODEC_ID_DTS:
00675         /* use generic seeking with dynamically generated indexes */
00676         return -1;
00677     default:
00678         break;
00679     }
00680     return pcm_read_seek(s, stream_index, timestamp, flags);
00681 }
00682 
00683 #define OFFSET(x) offsetof(WAVContext, x)
00684 #define DEC AV_OPT_FLAG_DECODING_PARAM
00685 static const AVOption demux_options[] = {
00686     { "ignore_length", "Ignore length", OFFSET(ignore_length), AV_OPT_TYPE_INT, { 0 }, 0, 1, DEC },
00687     { NULL },
00688 };
00689 
00690 static const AVClass wav_demuxer_class = {
00691     .class_name = "WAV demuxer",
00692     .item_name  = av_default_item_name,
00693     .option     = demux_options,
00694     .version    = LIBAVUTIL_VERSION_INT,
00695 };
00696 AVInputFormat ff_wav_demuxer = {
00697     .name           = "wav",
00698     .long_name      = NULL_IF_CONFIG_SMALL("WAV format"),
00699     .priv_data_size = sizeof(WAVContext),
00700     .read_probe     = wav_probe,
00701     .read_header    = wav_read_header,
00702     .read_packet    = wav_read_packet,
00703     .read_seek      = wav_read_seek,
00704     .flags= AVFMT_GENERIC_INDEX,
00705     .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
00706     .priv_class     = &wav_demuxer_class,
00707 };
00708 #endif /* CONFIG_WAV_DEMUXER */
00709 
00710 
00711 #if CONFIG_W64_DEMUXER
00712 static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
00713     0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
00714 
00715 static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
00716     0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
00717 
00718 static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
00719     0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
00720 
00721 static int w64_probe(AVProbeData *p)
00722 {
00723     if (p->buf_size <= 40)
00724         return 0;
00725     if (!memcmp(p->buf,      guid_riff, 16) &&
00726         !memcmp(p->buf + 24, guid_wave, 16))
00727         return AVPROBE_SCORE_MAX;
00728     else
00729         return 0;
00730 }
00731 
00732 static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
00733 {
00734     int64_t size;
00735     AVIOContext *pb  = s->pb;
00736     WAVContext    *wav = s->priv_data;
00737     AVStream *st;
00738     uint8_t guid[16];
00739     int ret;
00740 
00741     avio_read(pb, guid, 16);
00742     if (memcmp(guid, guid_riff, 16))
00743         return -1;
00744 
00745     if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
00746         return -1;
00747 
00748     avio_read(pb, guid, 16);
00749     if (memcmp(guid, guid_wave, 16)) {
00750         av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
00751         return -1;
00752     }
00753 
00754     size = find_guid(pb, guid_fmt);
00755     if (size < 0) {
00756         av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
00757         return -1;
00758     }
00759 
00760     st = avformat_new_stream(s, NULL);
00761     if (!st)
00762         return AVERROR(ENOMEM);
00763 
00764     /* subtract chunk header size - normal wav file doesn't count it */
00765     ret = ff_get_wav_header(pb, st->codec, size - 24);
00766     if (ret < 0)
00767         return ret;
00768     avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
00769 
00770     st->need_parsing = AVSTREAM_PARSE_FULL;
00771 
00772     avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00773 
00774     size = find_guid(pb, guid_data);
00775     if (size < 0) {
00776         av_log(s, AV_LOG_ERROR, "could not find data guid\n");
00777         return -1;
00778     }
00779     wav->data_end = avio_tell(pb) + size - 24;
00780     wav->w64      = 1;
00781 
00782     return 0;
00783 }
00784 
00785 AVInputFormat ff_w64_demuxer = {
00786     .name           = "w64",
00787     .long_name      = NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
00788     .priv_data_size = sizeof(WAVContext),
00789     .read_probe     = w64_probe,
00790     .read_header    = w64_read_header,
00791     .read_packet    = wav_read_packet,
00792     .read_seek      = wav_read_seek,
00793     .flags = AVFMT_GENERIC_INDEX,
00794     .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
00795 };
00796 #endif /* CONFIG_W64_DEMUXER */
Generated on Fri Feb 1 2013 14:34:55 for FFmpeg by doxygen 1.7.1