libavformat/mpc8.c
Go to the documentation of this file.
00001 /*
00002  * Musepack SV8 demuxer
00003  * Copyright (c) 2007 Konstantin Shishkov
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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/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; //seems to be valid MPC but no header yet
00094         if (header_found) {
00095             if (size < 11 || size > 28)
00096                 return 0;
00097             if (!AV_RL32(bs)) //zero CRC is invalid
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     if (s->nb_streams == 0) {
00141         av_log(s, AV_LOG_ERROR, "No stream added before parsing seek table\n");
00142         return;
00143     }
00144 
00145     avio_seek(s->pb, off, SEEK_SET);
00146     mpc8_get_chunk_header(s->pb, &tag, &size);
00147     if(tag != TAG_SEEKTABLE){
00148         av_log(s, AV_LOG_ERROR, "No seek table at given position\n");
00149         return;
00150     }
00151     if (size < 0 || size >= INT_MAX / 2) {
00152         av_log(s, AV_LOG_ERROR, "Bad seek table size\n");
00153         return;
00154     }
00155     if(!(buf = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE)))
00156         return;
00157     avio_read(s->pb, buf, size);
00158     init_get_bits(&gb, buf, size * 8);
00159     size = gb_get_v(&gb);
00160     if(size > UINT_MAX/4 || size > c->samples/1152){
00161         av_log(s, AV_LOG_ERROR, "Seek table is too big\n");
00162         return;
00163     }
00164     seekd = get_bits(&gb, 4);
00165     for(i = 0; i < 2; i++){
00166         pos = gb_get_v(&gb) + c->header_pos;
00167         ppos[1 - i] = pos;
00168         av_add_index_entry(s->streams[0], pos, i, 0, 0, AVINDEX_KEYFRAME);
00169     }
00170     for(; i < size; i++){
00171         t = get_unary(&gb, 1, 33) << 12;
00172         t += get_bits(&gb, 12);
00173         if(t & 1)
00174             t = -(t & ~1);
00175         pos = (t >> 1) + ppos[0]*2 - ppos[1];
00176         av_add_index_entry(s->streams[0], pos, i << seekd, 0, 0, AVINDEX_KEYFRAME);
00177         ppos[1] = ppos[0];
00178         ppos[0] = pos;
00179     }
00180     av_free(buf);
00181 }
00182 
00183 static void mpc8_handle_chunk(AVFormatContext *s, int tag, int64_t chunk_pos, int64_t size)
00184 {
00185     AVIOContext *pb = s->pb;
00186     int64_t pos, off;
00187 
00188     switch(tag){
00189     case TAG_SEEKTBLOFF:
00190         pos = avio_tell(pb) + size;
00191         off = ffio_read_varlen(pb);
00192         mpc8_parse_seektable(s, chunk_pos + off);
00193         avio_seek(pb, pos, SEEK_SET);
00194         break;
00195     default:
00196         avio_skip(pb, size);
00197     }
00198 }
00199 
00200 static int mpc8_read_header(AVFormatContext *s, AVFormatParameters *ap)
00201 {
00202     MPCContext *c = s->priv_data;
00203     AVIOContext *pb = s->pb;
00204     AVStream *st;
00205     int tag = 0;
00206     int64_t size, pos;
00207 
00208     c->header_pos = avio_tell(pb);
00209     if(avio_rl32(pb) != TAG_MPCK){
00210         av_log(s, AV_LOG_ERROR, "Not a Musepack8 file\n");
00211         return -1;
00212     }
00213 
00214     while(!pb->eof_reached){
00215         pos = avio_tell(pb);
00216         mpc8_get_chunk_header(pb, &tag, &size);
00217         if(tag == TAG_STREAMHDR)
00218             break;
00219         mpc8_handle_chunk(s, tag, pos, size);
00220     }
00221     if(tag != TAG_STREAMHDR){
00222         av_log(s, AV_LOG_ERROR, "Stream header not found\n");
00223         return -1;
00224     }
00225     pos = avio_tell(pb);
00226     avio_skip(pb, 4); //CRC
00227     c->ver = avio_r8(pb);
00228     if(c->ver != 8){
00229         av_log(s, AV_LOG_ERROR, "Unknown stream version %d\n", c->ver);
00230         return -1;
00231     }
00232     c->samples = ffio_read_varlen(pb);
00233     ffio_read_varlen(pb); //silence samples at the beginning
00234 
00235     st = avformat_new_stream(s, NULL);
00236     if (!st)
00237         return AVERROR(ENOMEM);
00238     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00239     st->codec->codec_id = CODEC_ID_MUSEPACK8;
00240     st->codec->bits_per_coded_sample = 16;
00241 
00242     st->codec->extradata_size = 2;
00243     st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00244     avio_read(pb, st->codec->extradata, st->codec->extradata_size);
00245 
00246     st->codec->channels = (st->codec->extradata[1] >> 4) + 1;
00247     st->codec->sample_rate = mpc8_rate[st->codec->extradata[0] >> 5];
00248     avpriv_set_pts_info(st, 32, 1152  << (st->codec->extradata[1]&3)*2, st->codec->sample_rate);
00249     st->duration = c->samples / (1152 << (st->codec->extradata[1]&3)*2);
00250     size -= avio_tell(pb) - pos;
00251 
00252     return 0;
00253 }
00254 
00255 static int mpc8_read_packet(AVFormatContext *s, AVPacket *pkt)
00256 {
00257     MPCContext *c = s->priv_data;
00258     int tag;
00259     int64_t pos, size;
00260 
00261     while(!s->pb->eof_reached){
00262         pos = avio_tell(s->pb);
00263         mpc8_get_chunk_header(s->pb, &tag, &size);
00264         if (size < 0)
00265             return -1;
00266         if(tag == TAG_AUDIOPACKET){
00267             if(av_get_packet(s->pb, pkt, size) < 0)
00268                 return AVERROR(ENOMEM);
00269             pkt->stream_index = 0;
00270             pkt->pts = c->frame;
00271             return 0;
00272         }
00273         if(tag == TAG_STREAMEND)
00274             return AVERROR(EIO);
00275         mpc8_handle_chunk(s, tag, pos, size);
00276     }
00277     return AVERROR_EOF;
00278 }
00279 
00280 static int mpc8_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00281 {
00282     AVStream *st = s->streams[stream_index];
00283     MPCContext *c = s->priv_data;
00284     int index = av_index_search_timestamp(st, timestamp, flags);
00285 
00286     if(index < 0) return -1;
00287     avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
00288     c->frame = st->index_entries[index].timestamp;
00289     return 0;
00290 }
00291 
00292 
00293 AVInputFormat ff_mpc8_demuxer = {
00294     .name           = "mpc8",
00295     .long_name      = NULL_IF_CONFIG_SMALL("Musepack SV8"),
00296     .priv_data_size = sizeof(MPCContext),
00297     .read_probe     = mpc8_probe,
00298     .read_header    = mpc8_read_header,
00299     .read_packet    = mpc8_read_packet,
00300     .read_seek      = mpc8_read_seek,
00301 };