Statistics
| Branch: | Revision:

ffmpeg / libavformat / mpc.c @ 5f8e0227

History | View | Annotate | Download (6.56 KB)

1 185c7b6b Kostya Shishkov
/*
2
 * Musepack demuxer
3
 * Copyright (c) 2006 Konstantin Shishkov
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19 e5a389a1 Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 185c7b6b Kostya Shishkov
 */
21 245976da Diego Biurrun
22
#include "libavcodec/bitstream.h"
23 185c7b6b Kostya Shishkov
#include "avformat.h"
24
25
#define MPC_FRAMESIZE  1152
26 aac88b53 Kostya Shishkov
#define DELAY_FRAMES   32
27 185c7b6b Kostya Shishkov
28
static const int mpc_rate[4] = { 44100, 48000, 37800, 32000 };
29
typedef struct {
30
    int64_t pos;
31
    int size, skip;
32
}MPCFrame;
33
34
typedef struct {
35
    int ver;
36
    uint32_t curframe, lastframe;
37
    uint32_t fcount;
38
    MPCFrame *frames;
39
    int curbits;
40
    int frames_noted;
41
} MPCContext;
42
43
static int mpc_probe(AVProbeData *p)
44
{
45
    const uint8_t *d = p->buf;
46
    if (d[0] == 'M' && d[1] == 'P' && d[2] == '+' && (d[3] == 0x17 || d[3] == 0x7))
47
        return AVPROBE_SCORE_MAX;
48 0587b0ca Kostya Shishkov
    if (d[0] == 'I' && d[1] == 'D' && d[2] == '3')
49
        return AVPROBE_SCORE_MAX / 2;
50 185c7b6b Kostya Shishkov
    return 0;
51
}
52
53
static int mpc_read_header(AVFormatContext *s, AVFormatParameters *ap)
54
{
55
    MPCContext *c = s->priv_data;
56
    AVStream *st;
57 0587b0ca Kostya Shishkov
    int t;
58 185c7b6b Kostya Shishkov
59 899681cd Björn Axelsson
    t = get_le24(s->pb);
60 0587b0ca Kostya Shishkov
    if(t != MKTAG('M', 'P', '+', 0)){
61
        if(t != MKTAG('I', 'D', '3', 0)){
62
            av_log(s, AV_LOG_ERROR, "Not a Musepack file\n");
63
            return -1;
64
        }
65
        /* skip ID3 tags and try again */
66 899681cd Björn Axelsson
        url_fskip(s->pb, 3);
67
        t  = get_byte(s->pb) << 21;
68
        t |= get_byte(s->pb) << 14;
69
        t |= get_byte(s->pb) <<  7;
70
        t |= get_byte(s->pb);
71 0587b0ca Kostya Shishkov
        av_log(s, AV_LOG_DEBUG, "Skipping %d(%X) bytes of ID3 data\n", t, t);
72 899681cd Björn Axelsson
        url_fskip(s->pb, t);
73
        if(get_le24(s->pb) != MKTAG('M', 'P', '+', 0)){
74 0587b0ca Kostya Shishkov
            av_log(s, AV_LOG_ERROR, "Not a Musepack file\n");
75
            return -1;
76
        }
77 185c7b6b Kostya Shishkov
    }
78 899681cd Björn Axelsson
    c->ver = get_byte(s->pb);
79 185c7b6b Kostya Shishkov
    if(c->ver != 0x07 && c->ver != 0x17){
80
        av_log(s, AV_LOG_ERROR, "Can demux Musepack SV7, got version %02X\n", c->ver);
81
        return -1;
82
    }
83 899681cd Björn Axelsson
    c->fcount = get_le32(s->pb);
84 185c7b6b Kostya Shishkov
    if((int64_t)c->fcount * sizeof(MPCFrame) >= UINT_MAX){
85
        av_log(s, AV_LOG_ERROR, "Too many frames, seeking is not possible\n");
86
        return -1;
87
    }
88
    c->frames = av_malloc(c->fcount * sizeof(MPCFrame));
89
    c->curframe = 0;
90
    c->lastframe = -1;
91
    c->curbits = 8;
92 e3113632 Kostya Shishkov
    c->frames_noted = 0;
93 185c7b6b Kostya Shishkov
94
    st = av_new_stream(s, 0);
95
    if (!st)
96 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
97 185c7b6b Kostya Shishkov
    st->codec->codec_type = CODEC_TYPE_AUDIO;
98
    st->codec->codec_id = CODEC_ID_MUSEPACK7;
99
    st->codec->channels = 2;
100
    st->codec->bits_per_sample = 16;
101
102
    st->codec->extradata_size = 16;
103
    st->codec->extradata = av_mallocz(st->codec->extradata_size+FF_INPUT_BUFFER_PADDING_SIZE);
104 899681cd Björn Axelsson
    get_buffer(s->pb, st->codec->extradata, 16);
105 185c7b6b Kostya Shishkov
    st->codec->sample_rate = mpc_rate[st->codec->extradata[2] & 3];
106
    av_set_pts_info(st, 32, MPC_FRAMESIZE, st->codec->sample_rate);
107
    /* scan for seekpoints */
108
    s->start_time = 0;
109
    s->duration = (int64_t)c->fcount * MPC_FRAMESIZE * AV_TIME_BASE / st->codec->sample_rate;
110
111
    return 0;
112
}
113
114
static int mpc_read_packet(AVFormatContext *s, AVPacket *pkt)
115
{
116
    MPCContext *c = s->priv_data;
117
    int ret, size, size2, curbits, cur = c->curframe;
118
    int64_t tmp, pos;
119
120 d1886912 Reimar Döffinger
    if (c->curframe >= c->fcount)
121 185c7b6b Kostya Shishkov
        return -1;
122
123
    if(c->curframe != c->lastframe + 1){
124 899681cd Björn Axelsson
        url_fseek(s->pb, c->frames[c->curframe].pos, SEEK_SET);
125 185c7b6b Kostya Shishkov
        c->curbits = c->frames[c->curframe].skip;
126
    }
127
    c->lastframe = c->curframe;
128
    c->curframe++;
129
    curbits = c->curbits;
130 899681cd Björn Axelsson
    pos = url_ftell(s->pb);
131
    tmp = get_le32(s->pb);
132 185c7b6b Kostya Shishkov
    if(curbits <= 12){
133
        size2 = (tmp >> (12 - curbits)) & 0xFFFFF;
134
    }else{
135 899681cd Björn Axelsson
        tmp = (tmp << 32) | get_le32(s->pb);
136 185c7b6b Kostya Shishkov
        size2 = (tmp >> (44 - curbits)) & 0xFFFFF;
137
    }
138
    curbits += 20;
139 899681cd Björn Axelsson
    url_fseek(s->pb, pos, SEEK_SET);
140 185c7b6b Kostya Shishkov
141
    size = ((size2 + curbits + 31) & ~31) >> 3;
142
    if(cur == c->frames_noted){
143
        c->frames[cur].pos = pos;
144
        c->frames[cur].size = size;
145
        c->frames[cur].skip = curbits - 20;
146
        av_add_index_entry(s->streams[0], cur, cur, size, 0, AVINDEX_KEYFRAME);
147
        c->frames_noted++;
148
    }
149
    c->curbits = (curbits + size2) & 0x1F;
150
151
    if (av_new_packet(pkt, size) < 0)
152 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
153 185c7b6b Kostya Shishkov
154
    pkt->data[0] = curbits;
155
    pkt->data[1] = (c->curframe > c->fcount);
156
157
    pkt->stream_index = 0;
158 aac88b53 Kostya Shishkov
    pkt->pts = cur;
159 899681cd Björn Axelsson
    ret = get_buffer(s->pb, pkt->data + 4, size);
160 185c7b6b Kostya Shishkov
    if(c->curbits)
161 899681cd Björn Axelsson
        url_fseek(s->pb, -4, SEEK_CUR);
162 185c7b6b Kostya Shishkov
    if(ret < size){
163
        av_free_packet(pkt);
164 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
165 185c7b6b Kostya Shishkov
    }
166
    pkt->size = ret + 4;
167
168
    return 0;
169
}
170
171
static int mpc_read_close(AVFormatContext *s)
172
{
173
    MPCContext *c = s->priv_data;
174
175
    av_freep(&c->frames);
176
    return 0;
177
}
178
179 e3113632 Kostya Shishkov
/**
180
 * Seek to the given position
181
 * If position is unknown but is within the limits of file
182
 * then packets are skipped unless desired position is reached
183
 *
184
 * Also this function makes use of the fact that timestamp == frameno
185
 */
186 185c7b6b Kostya Shishkov
static int mpc_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
187
{
188
    AVStream *st = s->streams[stream_index];
189
    MPCContext *c = s->priv_data;
190 e3113632 Kostya Shishkov
    AVPacket pkt1, *pkt = &pkt1;
191
    int ret;
192 aac88b53 Kostya Shishkov
    int index = av_index_search_timestamp(st, timestamp - DELAY_FRAMES, flags);
193 e3113632 Kostya Shishkov
    uint32_t lastframe;
194 185c7b6b Kostya Shishkov
195 e3113632 Kostya Shishkov
    /* if found, seek there */
196
    if (index >= 0){
197
        c->curframe = st->index_entries[index].pos;
198
        return 0;
199
    }
200
    /* if timestamp is out of bounds, return error */
201
    if(timestamp < 0 || timestamp >= c->fcount)
202
        return -1;
203 aac88b53 Kostya Shishkov
    timestamp -= DELAY_FRAMES;
204 e3113632 Kostya Shishkov
    /* seek to the furthest known position and read packets until
205
       we reach desired position */
206
    lastframe = c->curframe;
207
    if(c->frames_noted) c->curframe = c->frames_noted - 1;
208
    while(c->curframe < timestamp){
209
        ret = av_read_frame(s, pkt);
210
        if (ret < 0){
211
            c->curframe = lastframe;
212
            return -1;
213
        }
214
        av_free_packet(pkt);
215
    }
216 185c7b6b Kostya Shishkov
    return 0;
217
}
218
219
220
AVInputFormat mpc_demuxer = {
221
    "mpc",
222 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("Musepack"),
223 185c7b6b Kostya Shishkov
    sizeof(MPCContext),
224
    mpc_probe,
225
    mpc_read_header,
226
    mpc_read_packet,
227
    mpc_read_close,
228
    mpc_read_seek,
229
    .extensions = "mpc",
230
};