libavformat/mpc.c
185c7b6b
 /*
  * Musepack demuxer
  * Copyright (c) 2006 Konstantin Shishkov
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
e5a389a1
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
185c7b6b
  */
245976da
 
f6c6e5aa
 #include "libavutil/channel_layout.h"
9106a698
 #include "libavcodec/get_bits.h"
185c7b6b
 #include "avformat.h"
c3f9ebf7
 #include "internal.h"
311f96a8
 #include "apetag.h"
3694ac24
 #include "id3v1.h"
d2d67e42
 #include "libavutil/dict.h"
185c7b6b
 
 #define MPC_FRAMESIZE  1152
aac88b53
 #define DELAY_FRAMES   32
185c7b6b
 
 static const int mpc_rate[4] = { 44100, 48000, 37800, 32000 };
 typedef struct {
     int64_t pos;
     int size, skip;
 }MPCFrame;
 
 typedef struct {
     int ver;
     uint32_t curframe, lastframe;
     uint32_t fcount;
     MPCFrame *frames;
     int curbits;
     int frames_noted;
 } MPCContext;
 
 static int mpc_probe(AVProbeData *p)
 {
     const uint8_t *d = p->buf;
     if (d[0] == 'M' && d[1] == 'P' && d[2] == '+' && (d[3] == 0x17 || d[3] == 0x7))
         return AVPROBE_SCORE_MAX;
     return 0;
 }
 
6e9651d1
 static int mpc_read_header(AVFormatContext *s)
185c7b6b
 {
     MPCContext *c = s->priv_data;
     AVStream *st;
 
e63a3628
     if(avio_rl24(s->pb) != MKTAG('M', 'P', '+', 0)){
6612d8cf
         av_log(s, AV_LOG_ERROR, "Not a Musepack file\n");
263dbe9d
         return AVERROR_INVALIDDATA;
185c7b6b
     }
e63a3628
     c->ver = avio_r8(s->pb);
185c7b6b
     if(c->ver != 0x07 && c->ver != 0x17){
         av_log(s, AV_LOG_ERROR, "Can demux Musepack SV7, got version %02X\n", c->ver);
263dbe9d
         return AVERROR_INVALIDDATA;
185c7b6b
     }
e63a3628
     c->fcount = avio_rl32(s->pb);
185c7b6b
     if((int64_t)c->fcount * sizeof(MPCFrame) >= UINT_MAX){
         av_log(s, AV_LOG_ERROR, "Too many frames, seeking is not possible\n");
263dbe9d
         return AVERROR_INVALIDDATA;
185c7b6b
     }
0cdd1208
     if(c->fcount){
         c->frames = av_malloc(c->fcount * sizeof(MPCFrame));
         if(!c->frames){
             av_log(s, AV_LOG_ERROR, "Cannot allocate seektable\n");
             return AVERROR(ENOMEM);
         }
     }else{
         av_log(s, AV_LOG_WARNING, "Container reports no frames\n");
     }
185c7b6b
     c->curframe = 0;
     c->lastframe = -1;
     c->curbits = 8;
e3113632
     c->frames_noted = 0;
185c7b6b
 
3b3bbdd3
     st = avformat_new_stream(s, NULL);
185c7b6b
     if (!st)
769e10f0
         return AVERROR(ENOMEM);
72415b2a
     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
36ef5369
     st->codec->codec_id = AV_CODEC_ID_MUSEPACK7;
185c7b6b
     st->codec->channels = 2;
f6c6e5aa
     st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
dd1c8f3e
     st->codec->bits_per_coded_sample = 16;
185c7b6b
 
a1ce41c9
     if (ff_get_extradata(st->codec, s->pb, 16) < 0)
a807c682
         return AVERROR(ENOMEM);
185c7b6b
     st->codec->sample_rate = mpc_rate[st->codec->extradata[2] & 3];
c3f9ebf7
     avpriv_set_pts_info(st, 32, MPC_FRAMESIZE, st->codec->sample_rate);
185c7b6b
     /* scan for seekpoints */
0fc07ad9
     st->start_time = 0;
     st->duration = c->fcount;
185c7b6b
 
311f96a8
     /* try to read APE tags */
8978feda
     if (s->pb->seekable) {
384c9c2f
         int64_t pos = avio_tell(s->pb);
311f96a8
         ff_ape_parse_tag(s);
d2d67e42
         if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
3694ac24
             ff_id3v1_read(s);
f59d8ff8
         avio_seek(s->pb, pos, SEEK_SET);
311f96a8
     }
 
185c7b6b
     return 0;
 }
 
 static int mpc_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     MPCContext *c = s->priv_data;
     int ret, size, size2, curbits, cur = c->curframe;
d9ba767d
     unsigned tmp;
     int64_t pos;
185c7b6b
 
0cdd1208
     if (c->curframe >= c->fcount && c->fcount)
263dbe9d
         return AVERROR_EOF;
185c7b6b
 
     if(c->curframe != c->lastframe + 1){
f59d8ff8
         avio_seek(s->pb, c->frames[c->curframe].pos, SEEK_SET);
185c7b6b
         c->curbits = c->frames[c->curframe].skip;
     }
     c->lastframe = c->curframe;
     c->curframe++;
     curbits = c->curbits;
384c9c2f
     pos = avio_tell(s->pb);
e63a3628
     tmp = avio_rl32(s->pb);
185c7b6b
     if(curbits <= 12){
         size2 = (tmp >> (12 - curbits)) & 0xFFFFF;
     }else{
d9ba767d
         size2 = (tmp << (curbits - 12) | avio_rl32(s->pb) >> (44 - curbits)) & 0xFFFFF;
185c7b6b
     }
     curbits += 20;
f59d8ff8
     avio_seek(s->pb, pos, SEEK_SET);
185c7b6b
 
     size = ((size2 + curbits + 31) & ~31) >> 3;
0cdd1208
     if(cur == c->frames_noted && c->fcount){
185c7b6b
         c->frames[cur].pos = pos;
         c->frames[cur].size = size;
         c->frames[cur].skip = curbits - 20;
         av_add_index_entry(s->streams[0], cur, cur, size, 0, AVINDEX_KEYFRAME);
         c->frames_noted++;
     }
     c->curbits = (curbits + size2) & 0x1F;
 
263dbe9d
     if ((ret = av_new_packet(pkt, size)) < 0)
         return ret;
185c7b6b
 
     pkt->data[0] = curbits;
0cdd1208
     pkt->data[1] = (c->curframe > c->fcount) && c->fcount;
2e519564
     pkt->data[2] = 0;
     pkt->data[3] = 0;
185c7b6b
 
     pkt->stream_index = 0;
aac88b53
     pkt->pts = cur;
e63a3628
     ret = avio_read(s->pb, pkt->data + 4, size);
185c7b6b
     if(c->curbits)
f59d8ff8
         avio_seek(s->pb, -4, SEEK_CUR);
185c7b6b
     if(ret < size){
         av_free_packet(pkt);
263dbe9d
         return ret < 0 ? ret : AVERROR(EIO);
185c7b6b
     }
     pkt->size = ret + 4;
 
     return 0;
 }
 
 static int mpc_read_close(AVFormatContext *s)
 {
     MPCContext *c = s->priv_data;
 
     av_freep(&c->frames);
     return 0;
 }
 
e3113632
 /**
  * Seek to the given position
  * If position is unknown but is within the limits of file
  * then packets are skipped unless desired position is reached
  *
  * Also this function makes use of the fact that timestamp == frameno
  */
185c7b6b
 static int mpc_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
 {
     AVStream *st = s->streams[stream_index];
     MPCContext *c = s->priv_data;
e3113632
     AVPacket pkt1, *pkt = &pkt1;
     int ret;
a503afb1
     int index = av_index_search_timestamp(st, FFMAX(timestamp - DELAY_FRAMES, 0), flags);
e3113632
     uint32_t lastframe;
185c7b6b
 
e3113632
     /* if found, seek there */
4e8b2060
     if (index >= 0 && st->index_entries[st->nb_index_entries-1].timestamp >= timestamp - DELAY_FRAMES){
e3113632
         c->curframe = st->index_entries[index].pos;
         return 0;
     }
     /* if timestamp is out of bounds, return error */
     if(timestamp < 0 || timestamp >= c->fcount)
         return -1;
aac88b53
     timestamp -= DELAY_FRAMES;
e3113632
     /* seek to the furthest known position and read packets until
        we reach desired position */
     lastframe = c->curframe;
     if(c->frames_noted) c->curframe = c->frames_noted - 1;
     while(c->curframe < timestamp){
         ret = av_read_frame(s, pkt);
         if (ret < 0){
             c->curframe = lastframe;
263dbe9d
             return ret;
e3113632
         }
         av_free_packet(pkt);
     }
185c7b6b
     return 0;
 }
 
 
66355be3
 AVInputFormat ff_mpc_demuxer = {
dfc2c4d9
     .name           = "mpc",
     .long_name      = NULL_IF_CONFIG_SMALL("Musepack"),
     .priv_data_size = sizeof(MPCContext),
     .read_probe     = mpc_probe,
     .read_header    = mpc_read_header,
     .read_packet    = mpc_read_packet,
     .read_close     = mpc_read_close,
     .read_seek      = mpc_read_seek,
20234a4b
     .extensions     = "mpc",
185c7b6b
 };