00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavcodec/get_bits.h"
00023 #include "libavcodec/unary.h"
00024 #include "apetag.h"
00025 #include "avformat.h"
00026 #include "internal.h"
00027 #include "avio_internal.h"
00028
00030 #define MKMPCTAG(a, b) (a | (b << 8))
00031
00032 #define TAG_MPCK MKTAG('M','P','C','K')
00033
00035 enum MPCPacketTags{
00036 TAG_STREAMHDR = MKMPCTAG('S','H'),
00037 TAG_STREAMEND = MKMPCTAG('S','E'),
00038
00039 TAG_AUDIOPACKET = MKMPCTAG('A','P'),
00040
00041 TAG_SEEKTBLOFF = MKMPCTAG('S','O'),
00042 TAG_SEEKTABLE = MKMPCTAG('S','T'),
00043
00044 TAG_REPLAYGAIN = MKMPCTAG('R','G'),
00045 TAG_ENCINFO = MKMPCTAG('E','I'),
00046 };
00047
00048 static const int mpc8_rate[8] = { 44100, 48000, 37800, 32000, -1, -1, -1, -1 };
00049
00050 typedef struct {
00051 int ver;
00052 int64_t header_pos;
00053 int64_t samples;
00054
00055 int64_t apetag_start;
00056 } MPCContext;
00057
00058 static inline int64_t bs_get_v(uint8_t **bs)
00059 {
00060 int64_t v = 0;
00061 int br = 0;
00062 int c;
00063
00064 do {
00065 c = **bs; (*bs)++;
00066 v <<= 7;
00067 v |= c & 0x7F;
00068 br++;
00069 if (br > 10)
00070 return -1;
00071 } while (c & 0x80);
00072
00073 return v - br;
00074 }
00075
00076 static int mpc8_probe(AVProbeData *p)
00077 {
00078 uint8_t *bs = p->buf + 4;
00079 uint8_t *bs_end = bs + p->buf_size;
00080 int64_t size;
00081
00082 if (p->buf_size < 16)
00083 return 0;
00084 if (AV_RL32(p->buf) != TAG_MPCK)
00085 return 0;
00086 while (bs < bs_end + 3) {
00087 int header_found = (bs[0] == 'S' && bs[1] == 'H');
00088 if (bs[0] < 'A' || bs[0] > 'Z' || bs[1] < 'A' || bs[1] > 'Z')
00089 return 0;
00090 bs += 2;
00091 size = bs_get_v(&bs);
00092 if (size < 2)
00093 return 0;
00094 if (bs + size - 2 >= bs_end)
00095 return AVPROBE_SCORE_MAX / 4 - 1;
00096 if (header_found) {
00097 if (size < 11 || size > 28)
00098 return 0;
00099 if (!AV_RL32(bs))
00100 return 0;
00101 return AVPROBE_SCORE_MAX;
00102 } else {
00103 bs += size - 2;
00104 }
00105 }
00106 return 0;
00107 }
00108
00109 static inline int64_t gb_get_v(GetBitContext *gb)
00110 {
00111 int64_t v = 0;
00112 int bits = 0;
00113 while(get_bits1(gb) && bits < 64-7){
00114 v <<= 7;
00115 v |= get_bits(gb, 7);
00116 bits += 7;
00117 }
00118 v <<= 7;
00119 v |= get_bits(gb, 7);
00120
00121 return v;
00122 }
00123
00124 static void mpc8_get_chunk_header(AVIOContext *pb, int *tag, int64_t *size)
00125 {
00126 int64_t pos;
00127 pos = avio_tell(pb);
00128 *tag = avio_rl16(pb);
00129 *size = ffio_read_varlen(pb);
00130 *size -= avio_tell(pb) - pos;
00131 }
00132
00133 static void mpc8_parse_seektable(AVFormatContext *s, int64_t off)
00134 {
00135 MPCContext *c = s->priv_data;
00136 int tag;
00137 int64_t size, pos, ppos[2];
00138 uint8_t *buf;
00139 int i, t, seekd;
00140 GetBitContext gb;
00141
00142 avio_seek(s->pb, off, SEEK_SET);
00143 mpc8_get_chunk_header(s->pb, &tag, &size);
00144 if(tag != TAG_SEEKTABLE){
00145 av_log(s, AV_LOG_ERROR, "No seek table at given position\n");
00146 return;
00147 }
00148 if(!(buf = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE)))
00149 return;
00150 avio_read(s->pb, buf, size);
00151 init_get_bits(&gb, buf, size * 8);
00152 size = gb_get_v(&gb);
00153 if(size > UINT_MAX/4 || size > c->samples/1152){
00154 av_log(s, AV_LOG_ERROR, "Seek table is too big\n");
00155 return;
00156 }
00157 seekd = get_bits(&gb, 4);
00158 for(i = 0; i < 2; i++){
00159 pos = gb_get_v(&gb) + c->header_pos;
00160 ppos[1 - i] = pos;
00161 av_add_index_entry(s->streams[0], pos, i, 0, 0, AVINDEX_KEYFRAME);
00162 }
00163 for(; i < size; i++){
00164 t = get_unary(&gb, 1, 33) << 12;
00165 t += get_bits(&gb, 12);
00166 if(t & 1)
00167 t = -(t & ~1);
00168 pos = (t >> 1) + ppos[0]*2 - ppos[1];
00169 av_add_index_entry(s->streams[0], pos, i << seekd, 0, 0, AVINDEX_KEYFRAME);
00170 ppos[1] = ppos[0];
00171 ppos[0] = pos;
00172 }
00173 av_free(buf);
00174 }
00175
00176 static void mpc8_handle_chunk(AVFormatContext *s, int tag, int64_t chunk_pos, int64_t size)
00177 {
00178 AVIOContext *pb = s->pb;
00179 int64_t pos, off;
00180
00181 switch(tag){
00182 case TAG_SEEKTBLOFF:
00183 pos = avio_tell(pb) + size;
00184 off = ffio_read_varlen(pb);
00185 mpc8_parse_seektable(s, chunk_pos + off);
00186 avio_seek(pb, pos, SEEK_SET);
00187 break;
00188 default:
00189 avio_skip(pb, size);
00190 }
00191 }
00192
00193 static int mpc8_read_header(AVFormatContext *s)
00194 {
00195 MPCContext *c = s->priv_data;
00196 AVIOContext *pb = s->pb;
00197 AVStream *st;
00198 int tag = 0;
00199 int64_t size, pos;
00200
00201 c->header_pos = avio_tell(pb);
00202 if(avio_rl32(pb) != TAG_MPCK){
00203 av_log(s, AV_LOG_ERROR, "Not a Musepack8 file\n");
00204 return AVERROR_INVALIDDATA;
00205 }
00206
00207 while(!url_feof(pb)){
00208 pos = avio_tell(pb);
00209 mpc8_get_chunk_header(pb, &tag, &size);
00210 if(tag == TAG_STREAMHDR)
00211 break;
00212 mpc8_handle_chunk(s, tag, pos, size);
00213 }
00214 if(tag != TAG_STREAMHDR){
00215 av_log(s, AV_LOG_ERROR, "Stream header not found\n");
00216 return AVERROR_INVALIDDATA;
00217 }
00218 pos = avio_tell(pb);
00219 avio_skip(pb, 4);
00220 c->ver = avio_r8(pb);
00221 if(c->ver != 8){
00222 av_log(s, AV_LOG_ERROR, "Unknown stream version %d\n", c->ver);
00223 return AVERROR_PATCHWELCOME;
00224 }
00225 c->samples = ffio_read_varlen(pb);
00226 ffio_read_varlen(pb);
00227
00228 st = avformat_new_stream(s, NULL);
00229 if (!st)
00230 return AVERROR(ENOMEM);
00231 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00232 st->codec->codec_id = AV_CODEC_ID_MUSEPACK8;
00233 st->codec->bits_per_coded_sample = 16;
00234
00235 st->codec->extradata_size = 2;
00236 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00237 avio_read(pb, st->codec->extradata, st->codec->extradata_size);
00238
00239 st->codec->channels = (st->codec->extradata[1] >> 4) + 1;
00240 st->codec->sample_rate = mpc8_rate[st->codec->extradata[0] >> 5];
00241 avpriv_set_pts_info(st, 32, 1152 << (st->codec->extradata[1]&3)*2, st->codec->sample_rate);
00242 st->start_time = 0;
00243 st->duration = c->samples / (1152 << (st->codec->extradata[1]&3)*2);
00244 size -= avio_tell(pb) - pos;
00245 if (size > 0)
00246 avio_skip(pb, size);
00247
00248 if (pb->seekable) {
00249 int64_t pos = avio_tell(s->pb);
00250 c->apetag_start = ff_ape_parse_tag(s);
00251 avio_seek(s->pb, pos, SEEK_SET);
00252 }
00253
00254 return 0;
00255 }
00256
00257 static int mpc8_read_packet(AVFormatContext *s, AVPacket *pkt)
00258 {
00259 MPCContext *c = s->priv_data;
00260 int tag;
00261 int64_t pos, size;
00262
00263 while(!url_feof(s->pb)){
00264 pos = avio_tell(s->pb);
00265
00266
00267 if (c->apetag_start && pos >= c->apetag_start)
00268 return AVERROR_EOF;
00269
00270 mpc8_get_chunk_header(s->pb, &tag, &size);
00271 if (size < 0)
00272 return -1;
00273 if(tag == TAG_AUDIOPACKET){
00274 if(av_get_packet(s->pb, pkt, size) < 0)
00275 return AVERROR(ENOMEM);
00276 pkt->stream_index = 0;
00277 pkt->duration = 1;
00278 return 0;
00279 }
00280 if(tag == TAG_STREAMEND)
00281 return AVERROR(EIO);
00282 mpc8_handle_chunk(s, tag, pos, size);
00283 }
00284 return AVERROR_EOF;
00285 }
00286
00287 static int mpc8_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00288 {
00289 AVStream *st = s->streams[stream_index];
00290 int index = av_index_search_timestamp(st, timestamp, flags);
00291
00292 if(index < 0) return -1;
00293 if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
00294 return -1;
00295 ff_update_cur_dts(s, st, st->index_entries[index].timestamp);
00296 return 0;
00297 }
00298
00299
00300 AVInputFormat ff_mpc8_demuxer = {
00301 .name = "mpc8",
00302 .long_name = NULL_IF_CONFIG_SMALL("Musepack SV8"),
00303 .priv_data_size = sizeof(MPCContext),
00304 .read_probe = mpc8_probe,
00305 .read_header = mpc8_read_header,
00306 .read_packet = mpc8_read_packet,
00307 .read_seek = mpc8_read_seek,
00308 };