Statistics
| Branch: | Revision:

ffmpeg / libavformat / mpc.c @ 9d4edf96

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