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