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

libavformat/mpc.c

Go to the documentation of this file.
00001 /*
00002  * Musepack 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 "libavcodec/bitstream.h"
00023 #include "avformat.h"
00024 #include "id3v2.h"
00025 
00026 #define MPC_FRAMESIZE  1152
00027 #define DELAY_FRAMES   32
00028 
00029 static const int mpc_rate[4] = { 44100, 48000, 37800, 32000 };
00030 typedef struct {
00031     int64_t pos;
00032     int size, skip;
00033 }MPCFrame;
00034 
00035 typedef struct {
00036     int ver;
00037     uint32_t curframe, lastframe;
00038     uint32_t fcount;
00039     MPCFrame *frames;
00040     int curbits;
00041     int frames_noted;
00042 } MPCContext;
00043 
00044 static int mpc_probe(AVProbeData *p)
00045 {
00046     const uint8_t *d = p->buf;
00047     if (ff_id3v2_match(d)) {
00048         d += ff_id3v2_tag_len(d);
00049     }
00050     if (d+3 < p->buf+p->buf_size)
00051     if (d[0] == 'M' && d[1] == 'P' && d[2] == '+' && (d[3] == 0x17 || d[3] == 0x7))
00052         return AVPROBE_SCORE_MAX;
00053     return 0;
00054 }
00055 
00056 static int mpc_read_header(AVFormatContext *s, AVFormatParameters *ap)
00057 {
00058     MPCContext *c = s->priv_data;
00059     AVStream *st;
00060     int t;
00061 
00062     t = get_le24(s->pb);
00063     if(t != MKTAG('M', 'P', '+', 0)){
00064         if(t != MKTAG('I', 'D', '3', 0)){
00065             av_log(s, AV_LOG_ERROR, "Not a Musepack file\n");
00066             return -1;
00067         }
00068         /* skip ID3 tags and try again */
00069         url_fskip(s->pb, 3);
00070         t  = get_byte(s->pb) << 21;
00071         t |= get_byte(s->pb) << 14;
00072         t |= get_byte(s->pb) <<  7;
00073         t |= get_byte(s->pb);
00074         av_log(s, AV_LOG_DEBUG, "Skipping %d(%X) bytes of ID3 data\n", t, t);
00075         url_fskip(s->pb, t);
00076         if(get_le24(s->pb) != MKTAG('M', 'P', '+', 0)){
00077             av_log(s, AV_LOG_ERROR, "Not a Musepack file\n");
00078             return -1;
00079         }
00080     }
00081     c->ver = get_byte(s->pb);
00082     if(c->ver != 0x07 && c->ver != 0x17){
00083         av_log(s, AV_LOG_ERROR, "Can demux Musepack SV7, got version %02X\n", c->ver);
00084         return -1;
00085     }
00086     c->fcount = get_le32(s->pb);
00087     if((int64_t)c->fcount * sizeof(MPCFrame) >= UINT_MAX){
00088         av_log(s, AV_LOG_ERROR, "Too many frames, seeking is not possible\n");
00089         return -1;
00090     }
00091     c->frames = av_malloc(c->fcount * sizeof(MPCFrame));
00092     c->curframe = 0;
00093     c->lastframe = -1;
00094     c->curbits = 8;
00095     c->frames_noted = 0;
00096 
00097     st = av_new_stream(s, 0);
00098     if (!st)
00099         return AVERROR(ENOMEM);
00100     st->codec->codec_type = CODEC_TYPE_AUDIO;
00101     st->codec->codec_id = CODEC_ID_MUSEPACK7;
00102     st->codec->channels = 2;
00103     st->codec->bits_per_coded_sample = 16;
00104 
00105     st->codec->extradata_size = 16;
00106     st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
00107     get_buffer(s->pb, st->codec->extradata, 16);
00108     st->codec->sample_rate = mpc_rate[st->codec->extradata[2] & 3];
00109     av_set_pts_info(st, 32, MPC_FRAMESIZE, st->codec->sample_rate);
00110     /* scan for seekpoints */
00111     s->start_time = 0;
00112     s->duration = (int64_t)c->fcount * MPC_FRAMESIZE * AV_TIME_BASE / st->codec->sample_rate;
00113 
00114     return 0;
00115 }
00116 
00117 static int mpc_read_packet(AVFormatContext *s, AVPacket *pkt)
00118 {
00119     MPCContext *c = s->priv_data;
00120     int ret, size, size2, curbits, cur = c->curframe;
00121     int64_t tmp, pos;
00122 
00123     if (c->curframe >= c->fcount)
00124         return -1;
00125 
00126     if(c->curframe != c->lastframe + 1){
00127         url_fseek(s->pb, c->frames[c->curframe].pos, SEEK_SET);
00128         c->curbits = c->frames[c->curframe].skip;
00129     }
00130     c->lastframe = c->curframe;
00131     c->curframe++;
00132     curbits = c->curbits;
00133     pos = url_ftell(s->pb);
00134     tmp = get_le32(s->pb);
00135     if(curbits <= 12){
00136         size2 = (tmp >> (12 - curbits)) & 0xFFFFF;
00137     }else{
00138         tmp = (tmp << 32) | get_le32(s->pb);
00139         size2 = (tmp >> (44 - curbits)) & 0xFFFFF;
00140     }
00141     curbits += 20;
00142     url_fseek(s->pb, pos, SEEK_SET);
00143 
00144     size = ((size2 + curbits + 31) & ~31) >> 3;
00145     if(cur == c->frames_noted){
00146         c->frames[cur].pos = pos;
00147         c->frames[cur].size = size;
00148         c->frames[cur].skip = curbits - 20;
00149         av_add_index_entry(s->streams[0], cur, cur, size, 0, AVINDEX_KEYFRAME);
00150         c->frames_noted++;
00151     }
00152     c->curbits = (curbits + size2) & 0x1F;
00153 
00154     if (av_new_packet(pkt, size) < 0)
00155         return AVERROR(EIO);
00156 
00157     pkt->data[0] = curbits;
00158     pkt->data[1] = (c->curframe > c->fcount);
00159 
00160     pkt->stream_index = 0;
00161     pkt->pts = cur;
00162     ret = get_buffer(s->pb, pkt->data + 4, size);
00163     if(c->curbits)
00164         url_fseek(s->pb, -4, SEEK_CUR);
00165     if(ret < size){
00166         av_free_packet(pkt);
00167         return AVERROR(EIO);
00168     }
00169     pkt->size = ret + 4;
00170 
00171     return 0;
00172 }
00173 
00174 static int mpc_read_close(AVFormatContext *s)
00175 {
00176     MPCContext *c = s->priv_data;
00177 
00178     av_freep(&c->frames);
00179     return 0;
00180 }
00181 
00189 static int mpc_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00190 {
00191     AVStream *st = s->streams[stream_index];
00192     MPCContext *c = s->priv_data;
00193     AVPacket pkt1, *pkt = &pkt1;
00194     int ret;
00195     int index = av_index_search_timestamp(st, timestamp - DELAY_FRAMES, flags);
00196     uint32_t lastframe;
00197 
00198     /* if found, seek there */
00199     if (index >= 0){
00200         c->curframe = st->index_entries[index].pos;
00201         return 0;
00202     }
00203     /* if timestamp is out of bounds, return error */
00204     if(timestamp < 0 || timestamp >= c->fcount)
00205         return -1;
00206     timestamp -= DELAY_FRAMES;
00207     /* seek to the furthest known position and read packets until
00208        we reach desired position */
00209     lastframe = c->curframe;
00210     if(c->frames_noted) c->curframe = c->frames_noted - 1;
00211     while(c->curframe < timestamp){
00212         ret = av_read_frame(s, pkt);
00213         if (ret < 0){
00214             c->curframe = lastframe;
00215             return -1;
00216         }
00217         av_free_packet(pkt);
00218     }
00219     return 0;
00220 }
00221 
00222 
00223 AVInputFormat mpc_demuxer = {
00224     "mpc",
00225     NULL_IF_CONFIG_SMALL("Musepack"),
00226     sizeof(MPCContext),
00227     mpc_probe,
00228     mpc_read_header,
00229     mpc_read_packet,
00230     mpc_read_close,
00231     mpc_read_seek,
00232     .extensions = "mpc",
00233 };

Generated on Sat Feb 16 2013 09:23:12 for ffmpeg by  doxygen 1.7.1