Statistics
| Branch: | Revision:

ffmpeg / libavformat / mpc8.c @ f739b36d

History | View | Annotate | Download (6.53 KB)

1 12877faf Kostya Shishkov
/*
2
 * Musepack SV8 demuxer
3
 * Copyright (c) 2007 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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
#include "avformat.h"
22
#include "bitstream.h"
23
#include "unary.h"
24
25
/// Two-byte MPC tag
26
#define MKMPCTAG(a, b) (a | (b << 8))
27
28
#define TAG_MPCK MKTAG('M','P','C','K')
29
30
/// Reserved MPC tags
31
enum MPCPacketTags{
32
    TAG_STREAMHDR   = MKMPCTAG('S','H'),
33
    TAG_STREAMEND   = MKMPCTAG('S','E'),
34
35
    TAG_AUDIOPACKET = MKMPCTAG('A','P'),
36
37
    TAG_SEEKTBLOFF  = MKMPCTAG('S','O'),
38
    TAG_SEEKTABLE   = MKMPCTAG('S','T'),
39
40
    TAG_REPLAYGAIN  = MKMPCTAG('R','G'),
41
    TAG_ENCINFO     = MKMPCTAG('E','I'),
42
};
43
44
static const int mpc8_rate[8] = { 44100, 48000, 37800, 32000, -1, -1, -1, -1 };
45
46
typedef struct {
47
    int ver;
48
    int frame;
49
    int64_t header_pos;
50
    int64_t samples;
51
} MPCContext;
52
53
static int mpc8_probe(AVProbeData *p)
54
{
55
    if (AV_RL32(p->buf) == TAG_MPCK)
56
        return AVPROBE_SCORE_MAX;
57
    return 0;
58
}
59
60
static inline int64_t gb_get_v(GetBitContext *gb)
61
{
62
    int64_t v = 0;
63
    int bits = 0;
64
    while(get_bits1(gb) && bits < 64-7){
65
        v <<= 7;
66
        v |= get_bits(gb, 7);
67
        bits += 7;
68
    }
69
    v <<= 7;
70
    v |= get_bits(gb, 7);
71
72
    return v;
73
}
74
75
static void mpc8_get_chunk_header(ByteIOContext *pb, int *tag, int64_t *size)
76
{
77
    int64_t pos;
78
    pos = url_ftell(pb);
79
    *tag = get_le16(pb);
80
    *size = ff_get_v(pb);
81
    *size -= url_ftell(pb) - pos;
82
}
83
84
static void mpc8_parse_seektable(AVFormatContext *s, int64_t off)
85
{
86
    MPCContext *c = s->priv_data;
87
    int tag;
88
    int64_t size, pos, ppos[2];
89
    uint8_t *buf;
90
    int i, t, seekd;
91
    GetBitContext gb;
92
93 899681cd Björn Axelsson
    url_fseek(s->pb, off, SEEK_SET);
94
    mpc8_get_chunk_header(s->pb, &tag, &size);
95 12877faf Kostya Shishkov
    if(tag != TAG_SEEKTABLE){
96
        av_log(s, AV_LOG_ERROR, "No seek table at given position\n");
97
        return;
98
    }
99
    if(!(buf = av_malloc(size)))
100
        return;
101 899681cd Björn Axelsson
    get_buffer(s->pb, buf, size);
102 12877faf Kostya Shishkov
    init_get_bits(&gb, buf, size * 8);
103
    size = gb_get_v(&gb);
104
    if(size > UINT_MAX/4 || size > c->samples/1152){
105
        av_log(s, AV_LOG_ERROR, "Seek table is too big\n");
106
        return;
107
    }
108
    seekd = get_bits(&gb, 4);
109
    for(i = 0; i < 2; i++){
110
        pos = gb_get_v(&gb) + c->header_pos;
111
        ppos[1 - i] = pos;
112
        av_add_index_entry(s->streams[0], pos, i, 0, 0, AVINDEX_KEYFRAME);
113
    }
114
    for(; i < size; i++){
115
        t = get_unary(&gb, 1, 33) << 12;
116
        t += get_bits(&gb, 12);
117
        if(t & 1)
118
            t = -(t & ~1);
119
        pos = (t >> 1) + ppos[0]*2 - ppos[1];
120
        av_add_index_entry(s->streams[0], pos, i << seekd, 0, 0, AVINDEX_KEYFRAME);
121
        ppos[1] = ppos[0];
122
        ppos[0] = pos;
123
    }
124
    av_free(buf);
125
}
126
127
static void mpc8_handle_chunk(AVFormatContext *s, int tag, int64_t chunk_pos, int64_t size)
128
{
129 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
130 12877faf Kostya Shishkov
    int64_t pos, off;
131
132
    switch(tag){
133
    case TAG_SEEKTBLOFF:
134
        pos = url_ftell(pb) + size;
135
        off = ff_get_v(pb);
136
        mpc8_parse_seektable(s, chunk_pos + off);
137
        url_fseek(pb, pos, SEEK_SET);
138
        break;
139
    default:
140
        url_fskip(pb, size);
141
    }
142
}
143
144
static int mpc8_read_header(AVFormatContext *s, AVFormatParameters *ap)
145
{
146
    MPCContext *c = s->priv_data;
147 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
148 12877faf Kostya Shishkov
    AVStream *st;
149
    int tag = 0;
150
    int64_t size, pos;
151
152
    c->header_pos = url_ftell(pb);
153
    if(get_le32(pb) != TAG_MPCK){
154
        av_log(s, AV_LOG_ERROR, "Not a Musepack8 file\n");
155
        return -1;
156
    }
157
158
    while(!url_feof(pb)){
159
        pos = url_ftell(pb);
160
        mpc8_get_chunk_header(pb, &tag, &size);
161
        if(tag == TAG_STREAMHDR)
162
            break;
163
        mpc8_handle_chunk(s, tag, pos, size);
164
    }
165
    if(tag != TAG_STREAMHDR){
166
        av_log(s, AV_LOG_ERROR, "Stream header not found\n");
167
        return -1;
168
    }
169
    pos = url_ftell(pb);
170
    url_fskip(pb, 4); //CRC
171
    c->ver = get_byte(pb);
172
    if(c->ver != 8){
173
        av_log(s, AV_LOG_ERROR, "Unknown stream version %d\n", c->ver);
174
        return -1;
175
    }
176
    c->samples = ff_get_v(pb);
177
    ff_get_v(pb); //silence samples at the beginning
178
179
    st = av_new_stream(s, 0);
180
    if (!st)
181
        return AVERROR(ENOMEM);
182
    st->codec->codec_type = CODEC_TYPE_AUDIO;
183
    st->codec->codec_id = CODEC_ID_MUSEPACK8;
184
    st->codec->bits_per_sample = 16;
185
186
    st->codec->extradata_size = 2;
187
    st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
188
    get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
189
190
    st->codec->channels = (st->codec->extradata[1] >> 4) + 1;
191
    st->codec->sample_rate = mpc8_rate[st->codec->extradata[0] >> 5];
192
    av_set_pts_info(st, 32, 1152  << (st->codec->extradata[1]&3)*2, st->codec->sample_rate);
193
    st->duration = c->samples / (1152 << (st->codec->extradata[1]&3)*2);
194
    size -= url_ftell(pb) - pos;
195
196
    return 0;
197
}
198
199
static int mpc8_read_packet(AVFormatContext *s, AVPacket *pkt)
200
{
201
    MPCContext *c = s->priv_data;
202
    int tag;
203
    int64_t pos, size;
204
205 899681cd Björn Axelsson
    while(!url_feof(s->pb)){
206
        pos = url_ftell(s->pb);
207
        mpc8_get_chunk_header(s->pb, &tag, &size);
208 12877faf Kostya Shishkov
        if(tag == TAG_AUDIOPACKET){
209 899681cd Björn Axelsson
            if(av_get_packet(s->pb, pkt, size) < 0)
210 12877faf Kostya Shishkov
                return AVERROR(ENOMEM);
211
            pkt->stream_index = 0;
212
            pkt->pts = c->frame;
213
            return 0;
214
        }
215
        if(tag == TAG_STREAMEND)
216
            return AVERROR(EIO);
217
        mpc8_handle_chunk(s, tag, pos, size);
218
    }
219
    return 0;
220
}
221
222
static int mpc8_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
223
{
224
    AVStream *st = s->streams[stream_index];
225
    MPCContext *c = s->priv_data;
226
    int index = av_index_search_timestamp(st, timestamp, flags);
227
228
    if(index < 0) return -1;
229 899681cd Björn Axelsson
    url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
230 12877faf Kostya Shishkov
    c->frame = st->index_entries[index].timestamp;
231
    return 0;
232
}
233
234
235
AVInputFormat mpc8_demuxer = {
236
    "mpc8",
237
    "musepack8",
238
    sizeof(MPCContext),
239
    mpc8_probe,
240
    mpc8_read_header,
241
    mpc8_read_packet,
242
    NULL,
243
    mpc8_read_seek,
244
};