Statistics
| Branch: | Revision:

ffmpeg / libavformat / wv.c @ e356fc57

History | View | Annotate | Download (10.5 KB)

1 730581f3 Kostya Shishkov
/*
2
 * WavPack demuxer
3 3bdc886c Kostya
 * Copyright (c) 2006,2011 Konstantin Shishkov
4 730581f3 Kostya Shishkov
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 730581f3 Kostya Shishkov
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 730581f3 Kostya Shishkov
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 730581f3 Kostya Shishkov
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 730581f3 Kostya Shishkov
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22 737eb597 Reinhard Tartler
#include "libavutil/audioconvert.h"
23 6a5d31ac Diego Biurrun
#include "libavutil/intreadwrite.h"
24 730581f3 Kostya Shishkov
#include "avformat.h"
25 c4d438c2 Anton Khirnov
#include "apetag.h"
26
#include "id3v1.h"
27 730581f3 Kostya Shishkov
28
// specs say that maximum block size is 1Mb
29
#define WV_BLOCK_LIMIT 1047576
30
31
#define WV_EXTRA_SIZE 12
32
33 3bdc886c Kostya
#define WV_START_BLOCK  0x0800
34
#define WV_END_BLOCK    0x1000
35
#define WV_SINGLE_BLOCK (WV_START_BLOCK | WV_END_BLOCK)
36
37 730581f3 Kostya Shishkov
enum WV_FLAGS{
38
    WV_MONO   = 0x0004,
39
    WV_HYBRID = 0x0008,
40
    WV_JOINT  = 0x0010,
41
    WV_CROSSD = 0x0020,
42
    WV_HSHAPE = 0x0040,
43
    WV_FLOAT  = 0x0080,
44
    WV_INT32  = 0x0100,
45
    WV_HBR    = 0x0200,
46
    WV_HBAL   = 0x0400,
47
    WV_MCINIT = 0x0800,
48
    WV_MCEND  = 0x1000,
49
};
50
51
static const int wv_rates[16] = {
52
     6000,  8000,  9600, 11025, 12000, 16000, 22050, 24000,
53
    32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
54
};
55
56
typedef struct{
57
    uint32_t blksize, flags;
58
    int rate, chan, bpp;
59 3bdc886c Kostya
    uint32_t chmask;
60 49561f99 Kostya Shishkov
    uint32_t samples, soff;
61 3bdc886c Kostya
    int multichannel;
62 730581f3 Kostya Shishkov
    int block_parsed;
63
    uint8_t extra[WV_EXTRA_SIZE];
64 49561f99 Kostya Shishkov
    int64_t pos;
65 730581f3 Kostya Shishkov
}WVContext;
66
67
static int wv_probe(AVProbeData *p)
68
{
69
    /* check file header */
70
    if (p->buf_size <= 32)
71
        return 0;
72
    if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
73
        p->buf[2] == 'p' && p->buf[3] == 'k')
74
        return AVPROBE_SCORE_MAX;
75
    else
76
        return 0;
77
}
78
79 ae628ec1 Anton Khirnov
static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb, int append)
80 730581f3 Kostya Shishkov
{
81
    WVContext *wc = ctx->priv_data;
82
    uint32_t tag, ver;
83
    int size;
84
    int rate, bpp, chan;
85 3bdc886c Kostya
    uint32_t chmask;
86 730581f3 Kostya Shishkov
87 49561f99 Kostya Shishkov
    wc->pos = url_ftell(pb);
88 3bdc886c Kostya
    if(!append){
89 b7effd4e Anton Khirnov
        tag = avio_rl32(pb);
90 dacbcd17 Kostya
        if (tag != MKTAG('w', 'v', 'p', 'k'))
91
            return -1;
92 b7effd4e Anton Khirnov
        size = avio_rl32(pb);
93 dacbcd17 Kostya
        if(size < 24 || size > WV_BLOCK_LIMIT){
94
            av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
95
            return -1;
96
        }
97
        wc->blksize = size;
98 b7effd4e Anton Khirnov
        ver = avio_rl16(pb);
99 dacbcd17 Kostya
        if(ver < 0x402 || ver > 0x410){
100
            av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
101
            return -1;
102
        }
103 b7effd4e Anton Khirnov
        avio_r8(pb); // track no
104
        avio_r8(pb); // track sub index
105
        wc->samples = avio_rl32(pb); // total samples in file
106
        wc->soff = avio_rl32(pb); // offset in samples of current block
107
        avio_read(pb, wc->extra, WV_EXTRA_SIZE);
108 3bdc886c Kostya
    }else{
109
        size = wc->blksize;
110
    }
111 fead30d4 Alex Beregszaszi
    wc->flags = AV_RL32(wc->extra + 4);
112 730581f3 Kostya Shishkov
    //parse flags
113
    bpp = ((wc->flags & 3) + 1) << 3;
114
    chan = 1 + !(wc->flags & WV_MONO);
115 c2fcd0a7 Anton Khirnov
    chmask = wc->flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
116 730581f3 Kostya Shishkov
    rate = wv_rates[(wc->flags >> 23) & 0xF];
117 3bdc886c Kostya
    wc->multichannel = !!((wc->flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK);
118
    if(wc->multichannel){
119
        chan = wc->chan;
120
        chmask = wc->chmask;
121 614e139a Kostya Shishkov
    }
122 3bdc886c Kostya
    if((rate == -1 || !chan) && !wc->block_parsed){
123 7aa2d42d Kostya Shishkov
        int64_t block_end = url_ftell(pb) + wc->blksize - 24;
124
        if(url_is_streamed(pb)){
125 3bdc886c Kostya
            av_log(ctx, AV_LOG_ERROR, "Cannot determine additional parameters\n");
126 7aa2d42d Kostya Shishkov
            return -1;
127
        }
128
        while(url_ftell(pb) < block_end){
129
            int id, size;
130 b7effd4e Anton Khirnov
            id = avio_r8(pb);
131
            size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
132 7aa2d42d Kostya Shishkov
            size <<= 1;
133
            if(id&0x40)
134
                size--;
135 3bdc886c Kostya
            switch(id&0x3F){
136
            case 0xD:
137
                if(size <= 1){
138
                    av_log(ctx, AV_LOG_ERROR, "Insufficient channel information\n");
139
                    return -1;
140
                }
141 b7effd4e Anton Khirnov
                chan = avio_r8(pb);
142 3bdc886c Kostya
                switch(size - 2){
143
                case 0:
144 b7effd4e Anton Khirnov
                    chmask = avio_r8(pb);
145 3bdc886c Kostya
                    break;
146
                case 1:
147 b7effd4e Anton Khirnov
                    chmask = avio_rl16(pb);
148 3bdc886c Kostya
                    break;
149
                case 2:
150 b7effd4e Anton Khirnov
                    chmask = avio_rl24(pb);
151 3bdc886c Kostya
                    break;
152
                case 3:
153 b7effd4e Anton Khirnov
                    chmask = avio_rl32(pb);
154 3bdc886c Kostya
                    break;
155
                case 5:
156 e356fc57 Anton Khirnov
                    avio_seek(pb, 1, SEEK_CUR);
157 b7effd4e Anton Khirnov
                    chan |= (avio_r8(pb) & 0xF) << 8;
158
                    chmask = avio_rl24(pb);
159 3bdc886c Kostya
                    break;
160
                default:
161
                    av_log(ctx, AV_LOG_ERROR, "Invalid channel info size %d\n", size);
162
                    return -1;
163
                }
164
                break;
165
            case 0x27:
166 b7effd4e Anton Khirnov
                rate = avio_rl24(pb);
167 7aa2d42d Kostya Shishkov
                break;
168 3bdc886c Kostya
            default:
169 e356fc57 Anton Khirnov
                avio_seek(pb, size, SEEK_CUR);
170 7aa2d42d Kostya Shishkov
            }
171 3bdc886c Kostya
            if(id&0x40)
172 e356fc57 Anton Khirnov
                avio_seek(pb, 1, SEEK_CUR);
173 7aa2d42d Kostya Shishkov
        }
174
        if(rate == -1){
175
            av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
176
            return -1;
177
        }
178 6b4aa5da Anton Khirnov
        avio_seek(pb, block_end - wc->blksize + 24, SEEK_SET);
179 730581f3 Kostya Shishkov
    }
180
    if(!wc->bpp) wc->bpp = bpp;
181
    if(!wc->chan) wc->chan = chan;
182 3bdc886c Kostya
    if(!wc->chmask) wc->chmask = chmask;
183 730581f3 Kostya Shishkov
    if(!wc->rate) wc->rate = rate;
184
185 a8789714 Kostya Shishkov
    if(wc->flags && bpp != wc->bpp){
186 730581f3 Kostya Shishkov
        av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
187
        return -1;
188
    }
189 3bdc886c Kostya
    if(wc->flags && !wc->multichannel && chan != wc->chan){
190 730581f3 Kostya Shishkov
        av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
191
        return -1;
192
    }
193 7aa2d42d Kostya Shishkov
    if(wc->flags && rate != -1 && rate != wc->rate){
194 730581f3 Kostya Shishkov
        av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
195
        return -1;
196
    }
197
    wc->blksize = size - 24;
198
    return 0;
199
}
200
201
static int wv_read_header(AVFormatContext *s,
202
                          AVFormatParameters *ap)
203
{
204 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
205 730581f3 Kostya Shishkov
    WVContext *wc = s->priv_data;
206
    AVStream *st;
207
208 aa926a48 Kostya Shishkov
    wc->block_parsed = 0;
209 3bdc886c Kostya
    if(wv_read_block_header(s, pb, 0) < 0)
210 730581f3 Kostya Shishkov
        return -1;
211
212
    /* now we are ready: build format streams */
213
    st = av_new_stream(s, 0);
214
    if (!st)
215
        return -1;
216 72415b2a Stefano Sabatini
    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
217 730581f3 Kostya Shishkov
    st->codec->codec_id = CODEC_ID_WAVPACK;
218
    st->codec->channels = wc->chan;
219 3bdc886c Kostya
    st->codec->channel_layout = wc->chmask;
220 730581f3 Kostya Shishkov
    st->codec->sample_rate = wc->rate;
221 dd1c8f3e Luca Abeni
    st->codec->bits_per_coded_sample = wc->bpp;
222 730581f3 Kostya Shishkov
    av_set_pts_info(st, 64, 1, wc->rate);
223 0fc07ad9 David Conrad
    st->start_time = 0;
224
    st->duration = wc->samples;
225 c4d438c2 Anton Khirnov
226
    if(!url_is_streamed(s->pb)) {
227
        int64_t cur = url_ftell(s->pb);
228
        ff_ape_parse_tag(s);
229
        if(!av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
230
            ff_id3v1_read(s);
231 6b4aa5da Anton Khirnov
        avio_seek(s->pb, cur, SEEK_SET);
232 c4d438c2 Anton Khirnov
    }
233
234 730581f3 Kostya Shishkov
    return 0;
235
}
236
237
static int wv_read_packet(AVFormatContext *s,
238
                          AVPacket *pkt)
239
{
240
    WVContext *wc = s->priv_data;
241 827f0561 Diego Biurrun
    int ret;
242 3bdc886c Kostya
    int size, ver, off;
243 730581f3 Kostya Shishkov
244 899681cd Björn Axelsson
    if (url_feof(s->pb))
245 8fa36ae0 François Revol
        return AVERROR(EIO);
246 730581f3 Kostya Shishkov
    if(wc->block_parsed){
247 3bdc886c Kostya
        if(wv_read_block_header(s, s->pb, 0) < 0)
248 730581f3 Kostya Shishkov
            return -1;
249
    }
250
251 3bdc886c Kostya
    off = wc->multichannel ? 4 : 0;
252
    if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE + off) < 0)
253 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
254 3bdc886c Kostya
    if(wc->multichannel)
255
        AV_WL32(pkt->data, wc->blksize + WV_EXTRA_SIZE + 12);
256
    memcpy(pkt->data + off, wc->extra, WV_EXTRA_SIZE);
257 b7effd4e Anton Khirnov
    ret = avio_read(s->pb, pkt->data + WV_EXTRA_SIZE + off, wc->blksize);
258 730581f3 Kostya Shishkov
    if(ret != wc->blksize){
259
        av_free_packet(pkt);
260 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
261 730581f3 Kostya Shishkov
    }
262 3bdc886c Kostya
    while(!(wc->flags & WV_END_BLOCK)){
263 b7effd4e Anton Khirnov
        if(avio_rl32(s->pb) != MKTAG('w', 'v', 'p', 'k')){
264 3bdc886c Kostya
            av_free_packet(pkt);
265
            return -1;
266
        }
267
        if((ret = av_append_packet(s->pb, pkt, 4)) < 0){
268
            av_free_packet(pkt);
269
            return ret;
270
        }
271
        size = AV_RL32(pkt->data + pkt->size - 4);
272
        if(size < 24 || size > WV_BLOCK_LIMIT){
273
            av_free_packet(pkt);
274
            av_log(s, AV_LOG_ERROR, "Incorrect block size %d\n", size);
275
            return -1;
276
        }
277
        wc->blksize = size;
278 b7effd4e Anton Khirnov
        ver = avio_rl16(s->pb);
279 3bdc886c Kostya
        if(ver < 0x402 || ver > 0x410){
280
            av_free_packet(pkt);
281
            av_log(s, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
282
            return -1;
283
        }
284 b7effd4e Anton Khirnov
        avio_r8(s->pb); // track no
285
        avio_r8(s->pb); // track sub index
286
        wc->samples = avio_rl32(s->pb); // total samples in file
287
        wc->soff = avio_rl32(s->pb); // offset in samples of current block
288 3bdc886c Kostya
        if((ret = av_append_packet(s->pb, pkt, WV_EXTRA_SIZE)) < 0){
289
            av_free_packet(pkt);
290
            return ret;
291
        }
292
        memcpy(wc->extra, pkt->data + pkt->size - WV_EXTRA_SIZE, WV_EXTRA_SIZE);
293
294
        if(wv_read_block_header(s, s->pb, 1) < 0){
295
            av_free_packet(pkt);
296
            return -1;
297
        }
298
        ret = av_append_packet(s->pb, pkt, wc->blksize);
299
        if(ret < 0){
300
            av_free_packet(pkt);
301
            return ret;
302
        }
303
    }
304 730581f3 Kostya Shishkov
    pkt->stream_index = 0;
305
    wc->block_parsed = 1;
306 49561f99 Kostya Shishkov
    pkt->pts = wc->soff;
307
    av_add_index_entry(s->streams[0], wc->pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
308 730581f3 Kostya Shishkov
    return 0;
309
}
310
311 49561f99 Kostya Shishkov
static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
312
{
313
    AVStream *st = s->streams[stream_index];
314
    WVContext *wc = s->priv_data;
315
    AVPacket pkt1, *pkt = &pkt1;
316
    int ret;
317
    int index = av_index_search_timestamp(st, timestamp, flags);
318
    int64_t pos, pts;
319
320
    /* if found, seek there */
321
    if (index >= 0){
322
        wc->block_parsed = 1;
323 6b4aa5da Anton Khirnov
        avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
324 49561f99 Kostya Shishkov
        return 0;
325
    }
326
    /* if timestamp is out of bounds, return error */
327
    if(timestamp < 0 || timestamp >= s->duration)
328
        return -1;
329
330 899681cd Björn Axelsson
    pos = url_ftell(s->pb);
331 49561f99 Kostya Shishkov
    do{
332
        ret = av_read_frame(s, pkt);
333
        if (ret < 0){
334 6b4aa5da Anton Khirnov
            avio_seek(s->pb, pos, SEEK_SET);
335 49561f99 Kostya Shishkov
            return -1;
336
        }
337
        pts = pkt->pts;
338
        av_free_packet(pkt);
339
    }while(pts < timestamp);
340
    return 0;
341
}
342
343 c6610a21 Diego Elio Pettenò
AVInputFormat ff_wv_demuxer = {
344 730581f3 Kostya Shishkov
    "wv",
345 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("WavPack"),
346 730581f3 Kostya Shishkov
    sizeof(WVContext),
347
    wv_probe,
348
    wv_read_header,
349
    wv_read_packet,
350 6a862b49 Baptiste Coudurier
    NULL,
351 49561f99 Kostya Shishkov
    wv_read_seek,
352 730581f3 Kostya Shishkov
};