wav.c

Go to the documentation of this file.
00001 /*
00002  * WAV muxer and demuxer
00003  * Copyright (c) 2001, 2002 Fabrice Bellard.
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 #include "avformat.h"
00022 #include "raw.h"
00023 #include "riff.h"
00024 
00025 typedef struct {
00026     offset_t data;
00027     offset_t data_end;
00028     int64_t minpts;
00029     int64_t maxpts;
00030     int last_duration;
00031 } WAVContext;
00032 
00033 #ifdef CONFIG_MUXERS
00034 static int wav_write_header(AVFormatContext *s)
00035 {
00036     WAVContext *wav = s->priv_data;
00037     ByteIOContext *pb = s->pb;
00038     offset_t fmt, fact;
00039 
00040     put_tag(pb, "RIFF");
00041     put_le32(pb, 0); /* file length */
00042     put_tag(pb, "WAVE");
00043 
00044     /* format header */
00045     fmt = start_tag(pb, "fmt ");
00046     if (put_wav_header(pb, s->streams[0]->codec) < 0) {
00047         av_free(wav);
00048         return -1;
00049     }
00050     end_tag(pb, fmt);
00051 
00052     if(s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
00053        && !url_is_streamed(s->pb)) {
00054         fact = start_tag(pb, "fact");
00055         put_le32(pb, 0);
00056         end_tag(pb, fact);
00057     }
00058 
00059     av_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
00060     wav->maxpts = wav->last_duration = 0;
00061     wav->minpts = INT64_MAX;
00062 
00063     /* data header */
00064     wav->data = start_tag(pb, "data");
00065 
00066     put_flush_packet(pb);
00067 
00068     return 0;
00069 }
00070 
00071 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
00072 {
00073     ByteIOContext *pb = s->pb;
00074     WAVContext *wav = s->priv_data;
00075     put_buffer(pb, pkt->data, pkt->size);
00076     if(pkt->pts != AV_NOPTS_VALUE) {
00077         wav->minpts = FFMIN(wav->minpts, pkt->pts);
00078         wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
00079         wav->last_duration = pkt->duration;
00080     } else
00081         av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
00082     return 0;
00083 }
00084 
00085 static int wav_write_trailer(AVFormatContext *s)
00086 {
00087     ByteIOContext *pb = s->pb;
00088     WAVContext *wav = s->priv_data;
00089     offset_t file_size;
00090 
00091     if (!url_is_streamed(s->pb)) {
00092         end_tag(pb, wav->data);
00093 
00094         /* update file size */
00095         file_size = url_ftell(pb);
00096         url_fseek(pb, 4, SEEK_SET);
00097         put_le32(pb, (uint32_t)(file_size - 8));
00098         url_fseek(pb, file_size, SEEK_SET);
00099 
00100         put_flush_packet(pb);
00101 
00102         if(s->streams[0]->codec->codec_tag != 0x01) {
00103             /* Update num_samps in fact chunk */
00104             int number_of_samples;
00105             number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
00106                                            s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
00107                                            s->streams[0]->time_base.den);
00108             url_fseek(pb, wav->data-12, SEEK_SET);
00109             put_le32(pb, number_of_samples);
00110             url_fseek(pb, file_size, SEEK_SET);
00111             put_flush_packet(pb);
00112         }
00113     }
00114     return 0;
00115 }
00116 #endif //CONFIG_MUXERS
00117 
00118 /* return the size of the found tag */
00119 /* XXX: > 2GB ? */
00120 static int find_tag(ByteIOContext *pb, uint32_t tag1)
00121 {
00122     unsigned int tag;
00123     int size;
00124 
00125     for(;;) {
00126         if (url_feof(pb))
00127             return -1;
00128         tag = get_le32(pb);
00129         size = get_le32(pb);
00130         if (tag == tag1)
00131             break;
00132         url_fseek(pb, size, SEEK_CUR);
00133     }
00134     if (size < 0)
00135         size = 0x7fffffff;
00136     return size;
00137 }
00138 
00139 static int wav_probe(AVProbeData *p)
00140 {
00141     /* check file header */
00142     if (p->buf_size <= 32)
00143         return 0;
00144     if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
00145         p->buf[2] == 'F' && p->buf[3] == 'F' &&
00146         p->buf[8] == 'W' && p->buf[9] == 'A' &&
00147         p->buf[10] == 'V' && p->buf[11] == 'E')
00148         /*
00149           Since ACT demuxer has standard WAV header at top of it's own,
00150           returning score is decreased to avoid probe conflict
00151           between ACT and WAV.
00152         */
00153         return AVPROBE_SCORE_MAX - 1;
00154     else
00155         return 0;
00156 }
00157 
00158 /* wav input */
00159 static int wav_read_header(AVFormatContext *s,
00160                            AVFormatParameters *ap)
00161 {
00162     int size;
00163     unsigned int tag;
00164     ByteIOContext *pb = s->pb;
00165     AVStream *st;
00166     WAVContext *wav = s->priv_data;
00167 
00168     /* check RIFF header */
00169     tag = get_le32(pb);
00170 
00171     if (tag != MKTAG('R', 'I', 'F', 'F'))
00172         return -1;
00173     get_le32(pb); /* file size */
00174     tag = get_le32(pb);
00175     if (tag != MKTAG('W', 'A', 'V', 'E'))
00176         return -1;
00177 
00178     /* parse fmt header */
00179     size = find_tag(pb, MKTAG('f', 'm', 't', ' '));
00180     if (size < 0)
00181         return -1;
00182     st = av_new_stream(s, 0);
00183     if (!st)
00184         return AVERROR(ENOMEM);
00185 
00186     get_wav_header(pb, st->codec, size);
00187     st->need_parsing = AVSTREAM_PARSE_FULL;
00188 
00189     av_set_pts_info(st, 64, 1, st->codec->sample_rate);
00190 
00191     size = find_tag(pb, MKTAG('d', 'a', 't', 'a'));
00192     if (size < 0)
00193         return -1;
00194     wav->data_end= url_ftell(pb) + size;
00195     return 0;
00196 }
00197 
00198 #define MAX_SIZE 4096
00199 
00200 static int wav_read_packet(AVFormatContext *s,
00201                            AVPacket *pkt)
00202 {
00203     int ret, size, left;
00204     AVStream *st;
00205     WAVContext *wav = s->priv_data;
00206 
00207     if (url_feof(s->pb))
00208         return AVERROR(EIO);
00209     st = s->streams[0];
00210 
00211     left= wav->data_end - url_ftell(s->pb);
00212     if(left <= 0){
00213         left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
00214         if (left < 0) {
00215             return AVERROR(EIO);
00216         }
00217         wav->data_end= url_ftell(s->pb) + left;
00218     }
00219 
00220     size = MAX_SIZE;
00221     if (st->codec->block_align > 1) {
00222         if (size < st->codec->block_align)
00223             size = st->codec->block_align;
00224         size = (size / st->codec->block_align) * st->codec->block_align;
00225     }
00226     size= FFMIN(size, left);
00227     ret= av_get_packet(s->pb, pkt, size);
00228     if (ret <= 0)
00229         return AVERROR(EIO);
00230     pkt->stream_index = 0;
00231 
00232     /* note: we need to modify the packet size here to handle the last
00233        packet */
00234     pkt->size = ret;
00235     return ret;
00236 }
00237 
00238 static int wav_read_seek(AVFormatContext *s,
00239                          int stream_index, int64_t timestamp, int flags)
00240 {
00241     AVStream *st;
00242 
00243     st = s->streams[0];
00244     switch(st->codec->codec_id) {
00245     case CODEC_ID_MP2:
00246     case CODEC_ID_MP3:
00247     case CODEC_ID_AC3:
00248     case CODEC_ID_DTS:
00249         /* use generic seeking with dynamically generated indexes */
00250         return -1;
00251     default:
00252         break;
00253     }
00254     return pcm_read_seek(s, stream_index, timestamp, flags);
00255 }
00256 
00257 #ifdef CONFIG_WAV_DEMUXER
00258 AVInputFormat wav_demuxer = {
00259     "wav",
00260     NULL_IF_CONFIG_SMALL("WAV format"),
00261     sizeof(WAVContext),
00262     wav_probe,
00263     wav_read_header,
00264     wav_read_packet,
00265     NULL,
00266     wav_read_seek,
00267     .flags= AVFMT_GENERIC_INDEX,
00268     .codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0},
00269 };
00270 #endif
00271 #ifdef CONFIG_WAV_MUXER
00272 AVOutputFormat wav_muxer = {
00273     "wav",
00274     NULL_IF_CONFIG_SMALL("WAV format"),
00275     "audio/x-wav",
00276     "wav",
00277     sizeof(WAVContext),
00278     CODEC_ID_PCM_S16LE,
00279     CODEC_ID_NONE,
00280     wav_write_header,
00281     wav_write_packet,
00282     wav_write_trailer,
00283     .codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0},
00284 };
00285 #endif

Generated on Thu Dec 4 11:45:43 2008 for libextractor by  doxygen 1.5.1