Statistics
| Branch: | Revision:

ffmpeg / libavformat / mpc.c @ bd51c013

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