Statistics
| Branch: | Revision:

ffmpeg / libavformat / wv.c @ dacbcd17

History | View | Annotate | Download (10.5 KB)

1
/*
2
 * WavPack demuxer
3
 * Copyright (c) 2006,2011 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

    
22
#include "libavutil/intreadwrite.h"
23
#include "avformat.h"
24
#include "apetag.h"
25
#include "id3v1.h"
26

    
27
// specs say that maximum block size is 1Mb
28
#define WV_BLOCK_LIMIT 1047576
29

    
30
#define WV_EXTRA_SIZE 12
31

    
32
#define WV_START_BLOCK  0x0800
33
#define WV_END_BLOCK    0x1000
34
#define WV_SINGLE_BLOCK (WV_START_BLOCK | WV_END_BLOCK)
35

    
36
enum WV_FLAGS{
37
    WV_MONO   = 0x0004,
38
    WV_HYBRID = 0x0008,
39
    WV_JOINT  = 0x0010,
40
    WV_CROSSD = 0x0020,
41
    WV_HSHAPE = 0x0040,
42
    WV_FLOAT  = 0x0080,
43
    WV_INT32  = 0x0100,
44
    WV_HBR    = 0x0200,
45
    WV_HBAL   = 0x0400,
46
    WV_MCINIT = 0x0800,
47
    WV_MCEND  = 0x1000,
48
};
49

    
50
static const int wv_rates[16] = {
51
     6000,  8000,  9600, 11025, 12000, 16000, 22050, 24000,
52
    32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
53
};
54

    
55
typedef struct{
56
    uint32_t blksize, flags;
57
    int rate, chan, bpp;
58
    uint32_t chmask;
59
    uint32_t samples, soff;
60
    int multichannel;
61
    int block_parsed;
62
    uint8_t extra[WV_EXTRA_SIZE];
63
    int64_t pos;
64
}WVContext;
65

    
66
static int wv_probe(AVProbeData *p)
67
{
68
    /* check file header */
69
    if (p->buf_size <= 32)
70
        return 0;
71
    if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
72
        p->buf[2] == 'p' && p->buf[3] == 'k')
73
        return AVPROBE_SCORE_MAX;
74
    else
75
        return 0;
76
}
77

    
78
static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb, int append)
79
{
80
    WVContext *wc = ctx->priv_data;
81
    uint32_t tag, ver;
82
    int size;
83
    int rate, bpp, chan;
84
    uint32_t chmask;
85

    
86
    wc->pos = url_ftell(pb);
87
    if(!append){
88
        tag = get_le32(pb);
89
        if (tag != MKTAG('w', 'v', 'p', 'k'))
90
            return -1;
91
        size = get_le32(pb);
92
        if(size < 24 || size > WV_BLOCK_LIMIT){
93
            av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
94
            return -1;
95
        }
96
        wc->blksize = size;
97
        ver = get_le16(pb);
98
        if(ver < 0x402 || ver > 0x410){
99
            av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
100
            return -1;
101
        }
102
        get_byte(pb); // track no
103
        get_byte(pb); // track sub index
104
        wc->samples = get_le32(pb); // total samples in file
105
        wc->soff = get_le32(pb); // offset in samples of current block
106
        get_buffer(pb, wc->extra, WV_EXTRA_SIZE);
107
    }else{
108
        size = wc->blksize;
109
    }
110
    wc->flags = AV_RL32(wc->extra + 4);
111
    //parse flags
112
    bpp = ((wc->flags & 3) + 1) << 3;
113
    chan = 1 + !(wc->flags & WV_MONO);
114
    chmask = wc->flags & WV_MONO ? CH_LAYOUT_MONO : CH_LAYOUT_STEREO;
115
    rate = wv_rates[(wc->flags >> 23) & 0xF];
116
    wc->multichannel = !!((wc->flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK);
117
    if(wc->multichannel){
118
        chan = wc->chan;
119
        chmask = wc->chmask;
120
    }
121
    if((rate == -1 || !chan) && !wc->block_parsed){
122
        int64_t block_end = url_ftell(pb) + wc->blksize - 24;
123
        if(url_is_streamed(pb)){
124
            av_log(ctx, AV_LOG_ERROR, "Cannot determine additional parameters\n");
125
            return -1;
126
        }
127
        while(url_ftell(pb) < block_end){
128
            int id, size;
129
            id = get_byte(pb);
130
            size = (id & 0x80) ? get_le24(pb) : get_byte(pb);
131
            size <<= 1;
132
            if(id&0x40)
133
                size--;
134
            switch(id&0x3F){
135
            case 0xD:
136
                if(size <= 1){
137
                    av_log(ctx, AV_LOG_ERROR, "Insufficient channel information\n");
138
                    return -1;
139
                }
140
                chan = get_byte(pb);
141
                switch(size - 2){
142
                case 0:
143
                    chmask = get_byte(pb);
144
                    break;
145
                case 1:
146
                    chmask = get_le16(pb);
147
                    break;
148
                case 2:
149
                    chmask = get_le24(pb);
150
                    break;
151
                case 3:
152
                    chmask = get_le32(pb);
153
                    break;
154
                case 5:
155
                    url_fskip(pb, 1);
156
                    chan |= (get_byte(pb) & 0xF) << 8;
157
                    chmask = get_le24(pb);
158
                    break;
159
                default:
160
                    av_log(ctx, AV_LOG_ERROR, "Invalid channel info size %d\n", size);
161
                    return -1;
162
                }
163
                break;
164
            case 0x27:
165
                rate = get_le24(pb);
166
                break;
167
            default:
168
                url_fskip(pb, size);
169
            }
170
            if(id&0x40)
171
                url_fskip(pb, 1);
172
        }
173
        if(rate == -1){
174
            av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
175
            return -1;
176
        }
177
        url_fseek(pb, block_end - wc->blksize + 24, SEEK_SET);
178
    }
179
    if(!wc->bpp) wc->bpp = bpp;
180
    if(!wc->chan) wc->chan = chan;
181
    if(!wc->chmask) wc->chmask = chmask;
182
    if(!wc->rate) wc->rate = rate;
183

    
184
    if(wc->flags && bpp != wc->bpp){
185
        av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
186
        return -1;
187
    }
188
    if(wc->flags && !wc->multichannel && chan != wc->chan){
189
        av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
190
        return -1;
191
    }
192
    if(wc->flags && rate != -1 && rate != wc->rate){
193
        av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
194
        return -1;
195
    }
196
    wc->blksize = size - 24;
197
    return 0;
198
}
199

    
200
static int wv_read_header(AVFormatContext *s,
201
                          AVFormatParameters *ap)
202
{
203
    ByteIOContext *pb = s->pb;
204
    WVContext *wc = s->priv_data;
205
    AVStream *st;
206

    
207
    wc->block_parsed = 0;
208
    if(wv_read_block_header(s, pb, 0) < 0)
209
        return -1;
210

    
211
    /* now we are ready: build format streams */
212
    st = av_new_stream(s, 0);
213
    if (!st)
214
        return -1;
215
    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
216
    st->codec->codec_id = CODEC_ID_WAVPACK;
217
    st->codec->channels = wc->chan;
218
    st->codec->channel_layout = wc->chmask;
219
    st->codec->sample_rate = wc->rate;
220
    st->codec->bits_per_coded_sample = wc->bpp;
221
    av_set_pts_info(st, 64, 1, wc->rate);
222
    st->start_time = 0;
223
    st->duration = wc->samples;
224

    
225
    if(!url_is_streamed(s->pb)) {
226
        int64_t cur = url_ftell(s->pb);
227
        ff_ape_parse_tag(s);
228
        if(!av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
229
            ff_id3v1_read(s);
230
        url_fseek(s->pb, cur, SEEK_SET);
231
    }
232

    
233
    return 0;
234
}
235

    
236
static int wv_read_packet(AVFormatContext *s,
237
                          AVPacket *pkt)
238
{
239
    WVContext *wc = s->priv_data;
240
    int ret;
241
    int size, ver, off;
242

    
243
    if (url_feof(s->pb))
244
        return AVERROR(EIO);
245
    if(wc->block_parsed){
246
        if(wv_read_block_header(s, s->pb, 0) < 0)
247
            return -1;
248
    }
249

    
250
    off = wc->multichannel ? 4 : 0;
251
    if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE + off) < 0)
252
        return AVERROR(ENOMEM);
253
    if(wc->multichannel)
254
        AV_WL32(pkt->data, wc->blksize + WV_EXTRA_SIZE + 12);
255
    memcpy(pkt->data + off, wc->extra, WV_EXTRA_SIZE);
256
    ret = get_buffer(s->pb, pkt->data + WV_EXTRA_SIZE + off, wc->blksize);
257
    if(ret != wc->blksize){
258
        av_free_packet(pkt);
259
        return AVERROR(EIO);
260
    }
261
    while(!(wc->flags & WV_END_BLOCK)){
262
        if(get_le32(s->pb) != MKTAG('w', 'v', 'p', 'k')){
263
            av_free_packet(pkt);
264
            return -1;
265
        }
266
        if((ret = av_append_packet(s->pb, pkt, 4)) < 0){
267
            av_free_packet(pkt);
268
            return ret;
269
        }
270
        size = AV_RL32(pkt->data + pkt->size - 4);
271
        if(size < 24 || size > WV_BLOCK_LIMIT){
272
            av_free_packet(pkt);
273
            av_log(s, AV_LOG_ERROR, "Incorrect block size %d\n", size);
274
            return -1;
275
        }
276
        wc->blksize = size;
277
        ver = get_le16(s->pb);
278
        if(ver < 0x402 || ver > 0x410){
279
            av_free_packet(pkt);
280
            av_log(s, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
281
            return -1;
282
        }
283
        get_byte(s->pb); // track no
284
        get_byte(s->pb); // track sub index
285
        wc->samples = get_le32(s->pb); // total samples in file
286
        wc->soff = get_le32(s->pb); // offset in samples of current block
287
        if((ret = av_append_packet(s->pb, pkt, WV_EXTRA_SIZE)) < 0){
288
            av_free_packet(pkt);
289
            return ret;
290
        }
291
        memcpy(wc->extra, pkt->data + pkt->size - WV_EXTRA_SIZE, WV_EXTRA_SIZE);
292

    
293
        if(wv_read_block_header(s, s->pb, 1) < 0){
294
            av_free_packet(pkt);
295
            return -1;
296
        }
297
        ret = av_append_packet(s->pb, pkt, wc->blksize);
298
        if(ret < 0){
299
            av_free_packet(pkt);
300
            return ret;
301
        }
302
    }
303
    pkt->stream_index = 0;
304
    wc->block_parsed = 1;
305
    pkt->pts = wc->soff;
306
    av_add_index_entry(s->streams[0], wc->pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
307
    return 0;
308
}
309

    
310
static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
311
{
312
    AVStream *st = s->streams[stream_index];
313
    WVContext *wc = s->priv_data;
314
    AVPacket pkt1, *pkt = &pkt1;
315
    int ret;
316
    int index = av_index_search_timestamp(st, timestamp, flags);
317
    int64_t pos, pts;
318

    
319
    /* if found, seek there */
320
    if (index >= 0){
321
        wc->block_parsed = 1;
322
        url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
323
        return 0;
324
    }
325
    /* if timestamp is out of bounds, return error */
326
    if(timestamp < 0 || timestamp >= s->duration)
327
        return -1;
328

    
329
    pos = url_ftell(s->pb);
330
    do{
331
        ret = av_read_frame(s, pkt);
332
        if (ret < 0){
333
            url_fseek(s->pb, pos, SEEK_SET);
334
            return -1;
335
        }
336
        pts = pkt->pts;
337
        av_free_packet(pkt);
338
    }while(pts < timestamp);
339
    return 0;
340
}
341

    
342
AVInputFormat wv_demuxer = {
343
    "wv",
344
    NULL_IF_CONFIG_SMALL("WavPack"),
345
    sizeof(WVContext),
346
    wv_probe,
347
    wv_read_header,
348
    wv_read_packet,
349
    NULL,
350
    wv_read_seek,
351
};