00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/channel_layout.h"
00023 #include "libavcodec/get_bits.h"
00024 #include "avformat.h"
00025 #include "internal.h"
00026 #include "apetag.h"
00027 #include "id3v1.h"
00028 #include "libavutil/dict.h"
00029
00030 #define MPC_FRAMESIZE 1152
00031 #define DELAY_FRAMES 32
00032
00033 static const int mpc_rate[4] = { 44100, 48000, 37800, 32000 };
00034 typedef struct {
00035 int64_t pos;
00036 int size, skip;
00037 }MPCFrame;
00038
00039 typedef struct {
00040 int ver;
00041 uint32_t curframe, lastframe;
00042 uint32_t fcount;
00043 MPCFrame *frames;
00044 int curbits;
00045 int frames_noted;
00046 } MPCContext;
00047
00048 static int mpc_probe(AVProbeData *p)
00049 {
00050 const uint8_t *d = p->buf;
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)
00057 {
00058 MPCContext *c = s->priv_data;
00059 AVStream *st;
00060
00061 if(avio_rl24(s->pb) != MKTAG('M', 'P', '+', 0)){
00062 av_log(s, AV_LOG_ERROR, "Not a Musepack file\n");
00063 return AVERROR_INVALIDDATA;
00064 }
00065 c->ver = avio_r8(s->pb);
00066 if(c->ver != 0x07 && c->ver != 0x17){
00067 av_log(s, AV_LOG_ERROR, "Can demux Musepack SV7, got version %02X\n", c->ver);
00068 return AVERROR_INVALIDDATA;
00069 }
00070 c->fcount = avio_rl32(s->pb);
00071 if((int64_t)c->fcount * sizeof(MPCFrame) >= UINT_MAX){
00072 av_log(s, AV_LOG_ERROR, "Too many frames, seeking is not possible\n");
00073 return AVERROR_INVALIDDATA;
00074 }
00075 if(c->fcount){
00076 c->frames = av_malloc(c->fcount * sizeof(MPCFrame));
00077 if(!c->frames){
00078 av_log(s, AV_LOG_ERROR, "Cannot allocate seektable\n");
00079 return AVERROR(ENOMEM);
00080 }
00081 }else{
00082 av_log(s, AV_LOG_WARNING, "Container reports no frames\n");
00083 }
00084 c->curframe = 0;
00085 c->lastframe = -1;
00086 c->curbits = 8;
00087 c->frames_noted = 0;
00088
00089 st = avformat_new_stream(s, NULL);
00090 if (!st)
00091 return AVERROR(ENOMEM);
00092 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00093 st->codec->codec_id = AV_CODEC_ID_MUSEPACK7;
00094 st->codec->channels = 2;
00095 st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
00096 st->codec->bits_per_coded_sample = 16;
00097
00098 st->codec->extradata_size = 16;
00099 st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
00100 avio_read(s->pb, st->codec->extradata, 16);
00101 st->codec->sample_rate = mpc_rate[st->codec->extradata[2] & 3];
00102 avpriv_set_pts_info(st, 32, MPC_FRAMESIZE, st->codec->sample_rate);
00103
00104 st->start_time = 0;
00105 st->duration = c->fcount;
00106
00107
00108 if (s->pb->seekable) {
00109 int64_t pos = avio_tell(s->pb);
00110 ff_ape_parse_tag(s);
00111 if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
00112 ff_id3v1_read(s);
00113 avio_seek(s->pb, pos, SEEK_SET);
00114 }
00115
00116 return 0;
00117 }
00118
00119 static int mpc_read_packet(AVFormatContext *s, AVPacket *pkt)
00120 {
00121 MPCContext *c = s->priv_data;
00122 int ret, size, size2, curbits, cur = c->curframe;
00123 unsigned tmp;
00124 int64_t pos;
00125
00126 if (c->curframe >= c->fcount && c->fcount)
00127 return AVERROR_EOF;
00128
00129 if(c->curframe != c->lastframe + 1){
00130 avio_seek(s->pb, c->frames[c->curframe].pos, SEEK_SET);
00131 c->curbits = c->frames[c->curframe].skip;
00132 }
00133 c->lastframe = c->curframe;
00134 c->curframe++;
00135 curbits = c->curbits;
00136 pos = avio_tell(s->pb);
00137 tmp = avio_rl32(s->pb);
00138 if(curbits <= 12){
00139 size2 = (tmp >> (12 - curbits)) & 0xFFFFF;
00140 }else{
00141 size2 = (tmp << (curbits - 12) | avio_rl32(s->pb) >> (44 - curbits)) & 0xFFFFF;
00142 }
00143 curbits += 20;
00144 avio_seek(s->pb, pos, SEEK_SET);
00145
00146 size = ((size2 + curbits + 31) & ~31) >> 3;
00147 if(cur == c->frames_noted && c->fcount){
00148 c->frames[cur].pos = pos;
00149 c->frames[cur].size = size;
00150 c->frames[cur].skip = curbits - 20;
00151 av_add_index_entry(s->streams[0], cur, cur, size, 0, AVINDEX_KEYFRAME);
00152 c->frames_noted++;
00153 }
00154 c->curbits = (curbits + size2) & 0x1F;
00155
00156 if ((ret = av_new_packet(pkt, size)) < 0)
00157 return ret;
00158
00159 pkt->data[0] = curbits;
00160 pkt->data[1] = (c->curframe > c->fcount) && c->fcount;
00161 pkt->data[2] = 0;
00162 pkt->data[3] = 0;
00163
00164 pkt->stream_index = 0;
00165 pkt->pts = cur;
00166 ret = avio_read(s->pb, pkt->data + 4, size);
00167 if(c->curbits)
00168 avio_seek(s->pb, -4, SEEK_CUR);
00169 if(ret < size){
00170 av_free_packet(pkt);
00171 return ret < 0 ? ret : AVERROR(EIO);
00172 }
00173 pkt->size = ret + 4;
00174
00175 return 0;
00176 }
00177
00178 static int mpc_read_close(AVFormatContext *s)
00179 {
00180 MPCContext *c = s->priv_data;
00181
00182 av_freep(&c->frames);
00183 return 0;
00184 }
00185
00193 static int mpc_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00194 {
00195 AVStream *st = s->streams[stream_index];
00196 MPCContext *c = s->priv_data;
00197 AVPacket pkt1, *pkt = &pkt1;
00198 int ret;
00199 int index = av_index_search_timestamp(st, FFMAX(timestamp - DELAY_FRAMES, 0), flags);
00200 uint32_t lastframe;
00201
00202
00203 if (index >= 0 && st->index_entries[st->nb_index_entries-1].timestamp >= timestamp - DELAY_FRAMES){
00204 c->curframe = st->index_entries[index].pos;
00205 return 0;
00206 }
00207
00208 if(timestamp < 0 || timestamp >= c->fcount)
00209 return -1;
00210 timestamp -= DELAY_FRAMES;
00211
00212
00213 lastframe = c->curframe;
00214 if(c->frames_noted) c->curframe = c->frames_noted - 1;
00215 while(c->curframe < timestamp){
00216 ret = av_read_frame(s, pkt);
00217 if (ret < 0){
00218 c->curframe = lastframe;
00219 return ret;
00220 }
00221 av_free_packet(pkt);
00222 }
00223 return 0;
00224 }
00225
00226
00227 AVInputFormat ff_mpc_demuxer = {
00228 .name = "mpc",
00229 .long_name = NULL_IF_CONFIG_SMALL("Musepack"),
00230 .priv_data_size = sizeof(MPCContext),
00231 .read_probe = mpc_probe,
00232 .read_header = mpc_read_header,
00233 .read_packet = mpc_read_packet,
00234 .read_close = mpc_read_close,
00235 .read_seek = mpc_read_seek,
00236 .extensions = "mpc",
00237 };