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 (size > INT_MAX/10 || size<=0) {
00149 av_log(s, AV_LOG_ERROR, "Seek table size is invalid\n");
00150 return;
00151 }
00152 if(!(buf = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE)))
00153 return;
00154 avio_read(s->pb, buf, size);
00155 init_get_bits(&gb, buf, size * 8);
00156 size = gb_get_v(&gb);
00157 if(size > UINT_MAX/4 || size > c->samples/1152){
00158 av_log(s, AV_LOG_ERROR, "Seek table is too big\n");
00159 return;
00160 }
00161 seekd = get_bits(&gb, 4);
00162 for(i = 0; i < 2; i++){
00163 pos = gb_get_v(&gb) + c->header_pos;
00164 ppos[1 - i] = pos;
00165 av_add_index_entry(s->streams[0], pos, i, 0, 0, AVINDEX_KEYFRAME);
00166 }
00167 for(; i < size; i++){
00168 t = get_unary(&gb, 1, 33) << 12;
00169 t += get_bits(&gb, 12);
00170 if(t & 1)
00171 t = -(t & ~1);
00172 pos = (t >> 1) + ppos[0]*2 - ppos[1];
00173 av_add_index_entry(s->streams[0], pos, i << seekd, 0, 0, AVINDEX_KEYFRAME);
00174 ppos[1] = ppos[0];
00175 ppos[0] = pos;
00176 }
00177 av_free(buf);
00178 }
00179
00180 static void mpc8_handle_chunk(AVFormatContext *s, int tag, int64_t chunk_pos, int64_t size)
00181 {
00182 AVIOContext *pb = s->pb;
00183 int64_t pos, off;
00184
00185 switch(tag){
00186 case TAG_SEEKTBLOFF:
00187 pos = avio_tell(pb) + size;
00188 off = ffio_read_varlen(pb);
00189 mpc8_parse_seektable(s, chunk_pos + off);
00190 avio_seek(pb, pos, SEEK_SET);
00191 break;
00192 default:
00193 avio_skip(pb, size);
00194 }
00195 }
00196
00197 static int mpc8_read_header(AVFormatContext *s)
00198 {
00199 MPCContext *c = s->priv_data;
00200 AVIOContext *pb = s->pb;
00201 AVStream *st;
00202 int tag = 0;
00203 int64_t size, pos;
00204
00205 c->header_pos = avio_tell(pb);
00206 if(avio_rl32(pb) != TAG_MPCK){
00207 av_log(s, AV_LOG_ERROR, "Not a Musepack8 file\n");
00208 return AVERROR_INVALIDDATA;
00209 }
00210
00211 while(!url_feof(pb)){
00212 pos = avio_tell(pb);
00213 mpc8_get_chunk_header(pb, &tag, &size);
00214 if(tag == TAG_STREAMHDR)
00215 break;
00216 mpc8_handle_chunk(s, tag, pos, size);
00217 }
00218 if(tag != TAG_STREAMHDR){
00219 av_log(s, AV_LOG_ERROR, "Stream header not found\n");
00220 return AVERROR_INVALIDDATA;
00221 }
00222 pos = avio_tell(pb);
00223 avio_skip(pb, 4);
00224 c->ver = avio_r8(pb);
00225 if(c->ver != 8){
00226 av_log(s, AV_LOG_ERROR, "Unknown stream version %d\n", c->ver);
00227 return AVERROR_PATCHWELCOME;
00228 }
00229 c->samples = ffio_read_varlen(pb);
00230 ffio_read_varlen(pb);
00231
00232 st = avformat_new_stream(s, NULL);
00233 if (!st)
00234 return AVERROR(ENOMEM);
00235 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00236 st->codec->codec_id = AV_CODEC_ID_MUSEPACK8;
00237 st->codec->bits_per_coded_sample = 16;
00238
00239 st->codec->extradata_size = 2;
00240 st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00241 avio_read(pb, st->codec->extradata, st->codec->extradata_size);
00242
00243 st->codec->channels = (st->codec->extradata[1] >> 4) + 1;
00244 st->codec->sample_rate = mpc8_rate[st->codec->extradata[0] >> 5];
00245 avpriv_set_pts_info(st, 32, 1152 << (st->codec->extradata[1]&3)*2, st->codec->sample_rate);
00246 st->start_time = 0;
00247 st->duration = c->samples / (1152 << (st->codec->extradata[1]&3)*2);
00248 size -= avio_tell(pb) - pos;
00249 if (size > 0)
00250 avio_skip(pb, size);
00251
00252 if (pb->seekable) {
00253 int64_t pos = avio_tell(s->pb);
00254 c->apetag_start = ff_ape_parse_tag(s);
00255 avio_seek(s->pb, pos, SEEK_SET);
00256 }
00257
00258 return 0;
00259 }
00260
00261 static int mpc8_read_packet(AVFormatContext *s, AVPacket *pkt)
00262 {
00263 MPCContext *c = s->priv_data;
00264 int tag;
00265 int64_t pos, size;
00266
00267 while(!url_feof(s->pb)){
00268 pos = avio_tell(s->pb);
00269
00270
00271 if (c->apetag_start && pos >= c->apetag_start)
00272 return AVERROR_EOF;
00273
00274 mpc8_get_chunk_header(s->pb, &tag, &size);
00275 if (size < 0)
00276 return -1;
00277 if(tag == TAG_AUDIOPACKET){
00278 if(av_get_packet(s->pb, pkt, size) < 0)
00279 return AVERROR(ENOMEM);
00280 pkt->stream_index = 0;
00281 pkt->duration = 1;
00282 return 0;
00283 }
00284 if(tag == TAG_STREAMEND)
00285 return AVERROR(EIO);
00286 mpc8_handle_chunk(s, tag, pos, size);
00287 }
00288 return AVERROR_EOF;
00289 }
00290
00291 static int mpc8_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00292 {
00293 AVStream *st = s->streams[stream_index];
00294 int index = av_index_search_timestamp(st, timestamp, flags);
00295
00296 if(index < 0) return -1;
00297 if (avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET) < 0)
00298 return -1;
00299 ff_update_cur_dts(s, st, st->index_entries[index].timestamp);
00300 return 0;
00301 }
00302
00303
00304 AVInputFormat ff_mpc8_demuxer = {
00305 .name = "mpc8",
00306 .long_name = NULL_IF_CONFIG_SMALL("Musepack SV8"),
00307 .priv_data_size = sizeof(MPCContext),
00308 .read_probe = mpc8_probe,
00309 .read_header = mpc8_read_header,
00310 .read_packet = mpc8_read_packet,
00311 .read_seek = mpc8_read_seek,
00312 };