• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

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 FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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/bitstream.h"
00023 #include "libavcodec/unary.h"
00024 #include "avformat.h"
00025 
00027 #define MKMPCTAG(a, b) (a | (b << 8))
00028 
00029 #define TAG_MPCK MKTAG('M','P','C','K')
00030 
00032 enum MPCPacketTags{
00033     TAG_STREAMHDR   = MKMPCTAG('S','H'),
00034     TAG_STREAMEND   = MKMPCTAG('S','E'),
00035 
00036     TAG_AUDIOPACKET = MKMPCTAG('A','P'),
00037 
00038     TAG_SEEKTBLOFF  = MKMPCTAG('S','O'),
00039     TAG_SEEKTABLE   = MKMPCTAG('S','T'),
00040 
00041     TAG_REPLAYGAIN  = MKMPCTAG('R','G'),
00042     TAG_ENCINFO     = MKMPCTAG('E','I'),
00043 };
00044 
00045 static const int mpc8_rate[8] = { 44100, 48000, 37800, 32000, -1, -1, -1, -1 };
00046 
00047 typedef struct {
00048     int ver;
00049     int frame;
00050     int64_t header_pos;
00051     int64_t samples;
00052 } MPCContext;
00053 
00054 static int mpc8_probe(AVProbeData *p)
00055 {
00056     if (AV_RL32(p->buf) == TAG_MPCK)
00057         return AVPROBE_SCORE_MAX;
00058     return 0;
00059 }
00060 
00061 static inline int64_t gb_get_v(GetBitContext *gb)
00062 {
00063     int64_t v = 0;
00064     int bits = 0;
00065     while(get_bits1(gb) && bits < 64-7){
00066         v <<= 7;
00067         v |= get_bits(gb, 7);
00068         bits += 7;
00069     }
00070     v <<= 7;
00071     v |= get_bits(gb, 7);
00072 
00073     return v;
00074 }
00075 
00076 static void mpc8_get_chunk_header(ByteIOContext *pb, int *tag, int64_t *size)
00077 {
00078     int64_t pos;
00079     pos = url_ftell(pb);
00080     *tag = get_le16(pb);
00081     *size = ff_get_v(pb);
00082     *size -= url_ftell(pb) - pos;
00083 }
00084 
00085 static void mpc8_parse_seektable(AVFormatContext *s, int64_t off)
00086 {
00087     MPCContext *c = s->priv_data;
00088     int tag;
00089     int64_t size, pos, ppos[2];
00090     uint8_t *buf;
00091     int i, t, seekd;
00092     GetBitContext gb;
00093 
00094     url_fseek(s->pb, off, SEEK_SET);
00095     mpc8_get_chunk_header(s->pb, &tag, &size);
00096     if(tag != TAG_SEEKTABLE){
00097         av_log(s, AV_LOG_ERROR, "No seek table at given position\n");
00098         return;
00099     }
00100     if(!(buf = av_malloc(size)))
00101         return;
00102     get_buffer(s->pb, buf, size);
00103     init_get_bits(&gb, buf, size * 8);
00104     size = gb_get_v(&gb);
00105     if(size > UINT_MAX/4 || size > c->samples/1152){
00106         av_log(s, AV_LOG_ERROR, "Seek table is too big\n");
00107         return;
00108     }
00109     seekd = get_bits(&gb, 4);
00110     for(i = 0; i < 2; i++){
00111         pos = gb_get_v(&gb) + c->header_pos;
00112         ppos[1 - i] = pos;
00113         av_add_index_entry(s->streams[0], pos, i, 0, 0, AVINDEX_KEYFRAME);
00114     }
00115     for(; i < size; i++){
00116         t = get_unary(&gb, 1, 33) << 12;
00117         t += get_bits(&gb, 12);
00118         if(t & 1)
00119             t = -(t & ~1);
00120         pos = (t >> 1) + ppos[0]*2 - ppos[1];
00121         av_add_index_entry(s->streams[0], pos, i << seekd, 0, 0, AVINDEX_KEYFRAME);
00122         ppos[1] = ppos[0];
00123         ppos[0] = pos;
00124     }
00125     av_free(buf);
00126 }
00127 
00128 static void mpc8_handle_chunk(AVFormatContext *s, int tag, int64_t chunk_pos, int64_t size)
00129 {
00130     ByteIOContext *pb = s->pb;
00131     int64_t pos, off;
00132 
00133     switch(tag){
00134     case TAG_SEEKTBLOFF:
00135         pos = url_ftell(pb) + size;
00136         off = ff_get_v(pb);
00137         mpc8_parse_seektable(s, chunk_pos + off);
00138         url_fseek(pb, pos, SEEK_SET);
00139         break;
00140     default:
00141         url_fskip(pb, size);
00142     }
00143 }
00144 
00145 static int mpc8_read_header(AVFormatContext *s, AVFormatParameters *ap)
00146 {
00147     MPCContext *c = s->priv_data;
00148     ByteIOContext *pb = s->pb;
00149     AVStream *st;
00150     int tag = 0;
00151     int64_t size, pos;
00152 
00153     c->header_pos = url_ftell(pb);
00154     if(get_le32(pb) != TAG_MPCK){
00155         av_log(s, AV_LOG_ERROR, "Not a Musepack8 file\n");
00156         return -1;
00157     }
00158 
00159     while(!url_feof(pb)){
00160         pos = url_ftell(pb);
00161         mpc8_get_chunk_header(pb, &tag, &size);
00162         if(tag == TAG_STREAMHDR)
00163             break;
00164         mpc8_handle_chunk(s, tag, pos, size);
00165     }
00166     if(tag != TAG_STREAMHDR){
00167         av_log(s, AV_LOG_ERROR, "Stream header not found\n");
00168         return -1;
00169     }
00170     pos = url_ftell(pb);
00171     url_fskip(pb, 4); //CRC
00172     c->ver = get_byte(pb);
00173     if(c->ver != 8){
00174         av_log(s, AV_LOG_ERROR, "Unknown stream version %d\n", c->ver);
00175         return -1;
00176     }
00177     c->samples = ff_get_v(pb);
00178     ff_get_v(pb); //silence samples at the beginning
00179 
00180     st = av_new_stream(s, 0);
00181     if (!st)
00182         return AVERROR(ENOMEM);
00183     st->codec->codec_type = CODEC_TYPE_AUDIO;
00184     st->codec->codec_id = CODEC_ID_MUSEPACK8;
00185     st->codec->bits_per_coded_sample = 16;
00186 
00187     st->codec->extradata_size = 2;
00188     st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00189     get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
00190 
00191     st->codec->channels = (st->codec->extradata[1] >> 4) + 1;
00192     st->codec->sample_rate = mpc8_rate[st->codec->extradata[0] >> 5];
00193     av_set_pts_info(st, 32, 1152  << (st->codec->extradata[1]&3)*2, st->codec->sample_rate);
00194     st->duration = c->samples / (1152 << (st->codec->extradata[1]&3)*2);
00195     size -= url_ftell(pb) - pos;
00196 
00197     return 0;
00198 }
00199 
00200 static int mpc8_read_packet(AVFormatContext *s, AVPacket *pkt)
00201 {
00202     MPCContext *c = s->priv_data;
00203     int tag;
00204     int64_t pos, size;
00205 
00206     while(!url_feof(s->pb)){
00207         pos = url_ftell(s->pb);
00208         mpc8_get_chunk_header(s->pb, &tag, &size);
00209         if(tag == TAG_AUDIOPACKET){
00210             if(av_get_packet(s->pb, pkt, size) < 0)
00211                 return AVERROR(ENOMEM);
00212             pkt->stream_index = 0;
00213             pkt->pts = c->frame;
00214             return 0;
00215         }
00216         if(tag == TAG_STREAMEND)
00217             return AVERROR(EIO);
00218         mpc8_handle_chunk(s, tag, pos, size);
00219     }
00220     return 0;
00221 }
00222 
00223 static int mpc8_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00224 {
00225     AVStream *st = s->streams[stream_index];
00226     MPCContext *c = s->priv_data;
00227     int index = av_index_search_timestamp(st, timestamp, flags);
00228 
00229     if(index < 0) return -1;
00230     url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
00231     c->frame = st->index_entries[index].timestamp;
00232     return 0;
00233 }
00234 
00235 
00236 AVInputFormat mpc8_demuxer = {
00237     "mpc8",
00238     NULL_IF_CONFIG_SMALL("Musepack SV8"),
00239     sizeof(MPCContext),
00240     mpc8_probe,
00241     mpc8_read_header,
00242     mpc8_read_packet,
00243     NULL,
00244     mpc8_read_seek,
00245 };

Generated on Sat Feb 16 2013 09:23:13 for ffmpeg by  doxygen 1.7.1