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

libavformat/wv.c

Go to the documentation of this file.
00001 /*
00002  * WavPack demuxer
00003  * Copyright (c) 2006 Konstantin Shishkov
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 
00022 #include "libavutil/intreadwrite.h"
00023 #include "avformat.h"
00024 
00025 // specs say that maximum block size is 1Mb
00026 #define WV_BLOCK_LIMIT 1047576
00027 
00028 #define WV_EXTRA_SIZE 12
00029 
00030 enum WV_FLAGS{
00031     WV_MONO   = 0x0004,
00032     WV_HYBRID = 0x0008,
00033     WV_JOINT  = 0x0010,
00034     WV_CROSSD = 0x0020,
00035     WV_HSHAPE = 0x0040,
00036     WV_FLOAT  = 0x0080,
00037     WV_INT32  = 0x0100,
00038     WV_HBR    = 0x0200,
00039     WV_HBAL   = 0x0400,
00040     WV_MCINIT = 0x0800,
00041     WV_MCEND  = 0x1000,
00042 };
00043 
00044 static const int wv_rates[16] = {
00045      6000,  8000,  9600, 11025, 12000, 16000, 22050, 24000,
00046     32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
00047 };
00048 
00049 typedef struct{
00050     uint32_t blksize, flags;
00051     int rate, chan, bpp;
00052     uint32_t samples, soff;
00053     int block_parsed;
00054     uint8_t extra[WV_EXTRA_SIZE];
00055     int64_t pos;
00056 }WVContext;
00057 
00058 static int wv_probe(AVProbeData *p)
00059 {
00060     /* check file header */
00061     if (p->buf_size <= 32)
00062         return 0;
00063     if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
00064         p->buf[2] == 'p' && p->buf[3] == 'k')
00065         return AVPROBE_SCORE_MAX;
00066     else
00067         return 0;
00068 }
00069 
00070 static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb)
00071 {
00072     WVContext *wc = ctx->priv_data;
00073     uint32_t tag, ver;
00074     int size;
00075     int rate, bpp, chan;
00076 
00077     wc->pos = url_ftell(pb);
00078     tag = get_le32(pb);
00079     if (tag != MKTAG('w', 'v', 'p', 'k'))
00080         return -1;
00081     size = get_le32(pb);
00082     if(size < 24 || size > WV_BLOCK_LIMIT){
00083         av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
00084         return -1;
00085     }
00086     wc->blksize = size;
00087     ver = get_le16(pb);
00088     if(ver < 0x402 || ver > 0x410){
00089         av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
00090         return -1;
00091     }
00092     get_byte(pb); // track no
00093     get_byte(pb); // track sub index
00094     wc->samples = get_le32(pb); // total samples in file
00095     wc->soff = get_le32(pb); // offset in samples of current block
00096     get_buffer(pb, wc->extra, WV_EXTRA_SIZE);
00097     wc->flags = AV_RL32(wc->extra + 4);
00098     //parse flags
00099     if(wc->flags & WV_FLOAT){
00100         av_log(ctx, AV_LOG_ERROR, "Floating point data is not supported\n");
00101         return -1;
00102     }
00103 
00104     bpp = ((wc->flags & 3) + 1) << 3;
00105     chan = 1 + !(wc->flags & WV_MONO);
00106     rate = wv_rates[(wc->flags >> 23) & 0xF];
00107     if(rate == -1){
00108         av_log(ctx, AV_LOG_ERROR, "Unknown sampling rate\n");
00109         return -1;
00110     }
00111     if(!wc->bpp) wc->bpp = bpp;
00112     if(!wc->chan) wc->chan = chan;
00113     if(!wc->rate) wc->rate = rate;
00114 
00115     if(wc->flags && bpp != wc->bpp){
00116         av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
00117         return -1;
00118     }
00119     if(wc->flags && chan != wc->chan){
00120         av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
00121         return -1;
00122     }
00123     if(wc->flags && rate != wc->rate){
00124         av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
00125         return -1;
00126     }
00127     wc->blksize = size - 24;
00128     return 0;
00129 }
00130 
00131 static int wv_read_header(AVFormatContext *s,
00132                           AVFormatParameters *ap)
00133 {
00134     ByteIOContext *pb = s->pb;
00135     WVContext *wc = s->priv_data;
00136     AVStream *st;
00137 
00138     if(wv_read_block_header(s, pb) < 0)
00139         return -1;
00140 
00141     wc->block_parsed = 0;
00142     /* now we are ready: build format streams */
00143     st = av_new_stream(s, 0);
00144     if (!st)
00145         return -1;
00146     st->codec->codec_type = CODEC_TYPE_AUDIO;
00147     st->codec->codec_id = CODEC_ID_WAVPACK;
00148     st->codec->channels = wc->chan;
00149     st->codec->sample_rate = wc->rate;
00150     st->codec->bits_per_coded_sample = wc->bpp;
00151     av_set_pts_info(st, 64, 1, wc->rate);
00152     s->start_time = 0;
00153     s->duration = (int64_t)wc->samples * AV_TIME_BASE / st->codec->sample_rate;
00154     return 0;
00155 }
00156 
00157 static int wv_read_packet(AVFormatContext *s,
00158                           AVPacket *pkt)
00159 {
00160     WVContext *wc = s->priv_data;
00161     int ret;
00162 
00163     if (url_feof(s->pb))
00164         return AVERROR(EIO);
00165     if(wc->block_parsed){
00166         if(wv_read_block_header(s, s->pb) < 0)
00167             return -1;
00168     }
00169 
00170     if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE) < 0)
00171         return AVERROR(ENOMEM);
00172     memcpy(pkt->data, wc->extra, WV_EXTRA_SIZE);
00173     ret = get_buffer(s->pb, pkt->data + WV_EXTRA_SIZE, wc->blksize);
00174     if(ret != wc->blksize){
00175         av_free_packet(pkt);
00176         return AVERROR(EIO);
00177     }
00178     pkt->stream_index = 0;
00179     wc->block_parsed = 1;
00180     pkt->size = ret + WV_EXTRA_SIZE;
00181     pkt->pts = wc->soff;
00182     av_add_index_entry(s->streams[0], wc->pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
00183     return 0;
00184 }
00185 
00186 static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00187 {
00188     AVStream *st = s->streams[stream_index];
00189     WVContext *wc = s->priv_data;
00190     AVPacket pkt1, *pkt = &pkt1;
00191     int ret;
00192     int index = av_index_search_timestamp(st, timestamp, flags);
00193     int64_t pos, pts;
00194 
00195     /* if found, seek there */
00196     if (index >= 0){
00197         wc->block_parsed = 1;
00198         url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
00199         return 0;
00200     }
00201     /* if timestamp is out of bounds, return error */
00202     if(timestamp < 0 || timestamp >= s->duration)
00203         return -1;
00204 
00205     pos = url_ftell(s->pb);
00206     do{
00207         ret = av_read_frame(s, pkt);
00208         if (ret < 0){
00209             url_fseek(s->pb, pos, SEEK_SET);
00210             return -1;
00211         }
00212         pts = pkt->pts;
00213         av_free_packet(pkt);
00214     }while(pts < timestamp);
00215     return 0;
00216 }
00217 
00218 AVInputFormat wv_demuxer = {
00219     "wv",
00220     NULL_IF_CONFIG_SMALL("WavPack"),
00221     sizeof(WVContext),
00222     wv_probe,
00223     wv_read_header,
00224     wv_read_packet,
00225     NULL,
00226     wv_read_seek,
00227 };

Generated on Tue Nov 4 2014 12:59:24 for ffmpeg by  doxygen 1.7.1