Statistics
| Branch: | Revision:

ffmpeg / libavformat / wv.c @ d2062ff6

History | View | Annotate | Download (6.5 KB)

1 730581f3 Kostya Shishkov
/*
2
 * WavPack demuxer
3
 * Copyright (c) 2006 Konstantin Shishkov.
4
 *
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
#include "avformat.h"
23
#include "allformats.h"
24
#include "bswap.h"
25
26
// specs say that maximum block size is 1Mb
27
#define WV_BLOCK_LIMIT 1047576
28
29
#define WV_EXTRA_SIZE 12
30
31
enum WV_FLAGS{
32
    WV_MONO   = 0x0004,
33
    WV_HYBRID = 0x0008,
34
    WV_JOINT  = 0x0010,
35
    WV_CROSSD = 0x0020,
36
    WV_HSHAPE = 0x0040,
37
    WV_FLOAT  = 0x0080,
38
    WV_INT32  = 0x0100,
39
    WV_HBR    = 0x0200,
40
    WV_HBAL   = 0x0400,
41
    WV_MCINIT = 0x0800,
42
    WV_MCEND  = 0x1000,
43
};
44
45
static const int wv_rates[16] = {
46
     6000,  8000,  9600, 11025, 12000, 16000, 22050, 24000,
47
    32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
48
};
49
50
typedef struct{
51
    uint32_t blksize, flags;
52
    int rate, chan, bpp;
53 49561f99 Kostya Shishkov
    uint32_t samples, soff;
54 730581f3 Kostya Shishkov
    int block_parsed;
55
    uint8_t extra[WV_EXTRA_SIZE];
56 49561f99 Kostya Shishkov
    int64_t pos;
57 730581f3 Kostya Shishkov
}WVContext;
58
59
static int wv_probe(AVProbeData *p)
60
{
61
    /* check file header */
62
    if (p->buf_size <= 32)
63
        return 0;
64
    if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
65
        p->buf[2] == 'p' && p->buf[3] == 'k')
66
        return AVPROBE_SCORE_MAX;
67
    else
68
        return 0;
69
}
70
71
static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb)
72
{
73
    WVContext *wc = ctx->priv_data;
74
    uint32_t tag, ver;
75
    int size;
76
    int rate, bpp, chan;
77
78 49561f99 Kostya Shishkov
    wc->pos = url_ftell(pb);
79 730581f3 Kostya Shishkov
    tag = get_le32(pb);
80
    if (tag != MKTAG('w', 'v', 'p', 'k'))
81
        return -1;
82
    size = get_le32(pb);
83
    if(size < 24 || size > WV_BLOCK_LIMIT){
84
        av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
85
        return -1;
86
    }
87
    wc->blksize = size;
88
    ver = get_le16(pb);
89
    if(ver < 0x402 || ver > 0x40F){
90
        av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
91
        return -1;
92
    }
93
    get_byte(pb); // track no
94
    get_byte(pb); // track sub index
95 49561f99 Kostya Shishkov
    wc->samples = get_le32(pb); // total samples in file
96
    wc->soff = get_le32(pb); // offset in samples of current block
97 730581f3 Kostya Shishkov
    get_buffer(pb, wc->extra, WV_EXTRA_SIZE);
98 fead30d4 Alex Beregszaszi
    wc->flags = AV_RL32(wc->extra + 4);
99 730581f3 Kostya Shishkov
    //parse flags
100
    if(wc->flags & WV_FLOAT){
101
        av_log(ctx, AV_LOG_ERROR, "Floating point data is not supported\n");
102
        return -1;
103
    }
104
    if(wc->flags & WV_HYBRID){
105
        av_log(ctx, AV_LOG_ERROR, "Hybrid coding mode is not supported\n");
106
        return -1;
107
    }
108
109
    bpp = ((wc->flags & 3) + 1) << 3;
110
    chan = 1 + !(wc->flags & WV_MONO);
111
    rate = wv_rates[(wc->flags >> 23) & 0xF];
112
    if(rate == -1){
113
        av_log(ctx, AV_LOG_ERROR, "Unknown sampling rate\n");
114
        return -1;
115
    }
116
    if(!wc->bpp) wc->bpp = bpp;
117
    if(!wc->chan) wc->chan = chan;
118
    if(!wc->rate) wc->rate = rate;
119
120 a8789714 Kostya Shishkov
    if(wc->flags && bpp != wc->bpp){
121 730581f3 Kostya Shishkov
        av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
122
        return -1;
123
    }
124 a8789714 Kostya Shishkov
    if(wc->flags && chan != wc->chan){
125 730581f3 Kostya Shishkov
        av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
126
        return -1;
127
    }
128 a8789714 Kostya Shishkov
    if(wc->flags && rate != wc->rate){
129 730581f3 Kostya Shishkov
        av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
130
        return -1;
131
    }
132
    wc->blksize = size - 24;
133
    return 0;
134
}
135
136
static int wv_read_header(AVFormatContext *s,
137
                          AVFormatParameters *ap)
138
{
139
    ByteIOContext *pb = &s->pb;
140
    WVContext *wc = s->priv_data;
141
    AVStream *st;
142
143
    if(wv_read_block_header(s, pb) < 0)
144
        return -1;
145
146
    wc->block_parsed = 0;
147
    /* now we are ready: build format streams */
148
    st = av_new_stream(s, 0);
149
    if (!st)
150
        return -1;
151
    st->codec->codec_type = CODEC_TYPE_AUDIO;
152
    st->codec->codec_id = CODEC_ID_WAVPACK;
153
    st->codec->channels = wc->chan;
154
    st->codec->sample_rate = wc->rate;
155
    st->codec->bits_per_sample = wc->bpp;
156
    av_set_pts_info(st, 64, 1, wc->rate);
157 49561f99 Kostya Shishkov
    s->start_time = 0;
158
    s->duration = (int64_t)wc->samples * AV_TIME_BASE / st->codec->sample_rate;
159 730581f3 Kostya Shishkov
    return 0;
160
}
161
162
static int wv_read_packet(AVFormatContext *s,
163
                          AVPacket *pkt)
164
{
165
    WVContext *wc = s->priv_data;
166 827f0561 Diego Biurrun
    int ret;
167 730581f3 Kostya Shishkov
168
    if (url_feof(&s->pb))
169 8fa36ae0 Fran├žois Revol
        return AVERROR(EIO);
170 730581f3 Kostya Shishkov
    if(wc->block_parsed){
171
        if(wv_read_block_header(s, &s->pb) < 0)
172
            return -1;
173
    }
174
175
    if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE) < 0)
176 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
177 730581f3 Kostya Shishkov
    memcpy(pkt->data, wc->extra, WV_EXTRA_SIZE);
178
    ret = get_buffer(&s->pb, pkt->data + WV_EXTRA_SIZE, wc->blksize);
179
    if(ret != wc->blksize){
180
        av_free_packet(pkt);
181 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
182 730581f3 Kostya Shishkov
    }
183
    pkt->stream_index = 0;
184
    wc->block_parsed = 1;
185
    pkt->size = ret + WV_EXTRA_SIZE;
186 49561f99 Kostya Shishkov
    pkt->pts = wc->soff;
187
    av_add_index_entry(s->streams[0], wc->pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
188 730581f3 Kostya Shishkov
    return 0;
189
}
190
191
static int wv_read_close(AVFormatContext *s)
192
{
193
    return 0;
194
}
195
196 49561f99 Kostya Shishkov
static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
197
{
198
    AVStream *st = s->streams[stream_index];
199
    WVContext *wc = s->priv_data;
200
    AVPacket pkt1, *pkt = &pkt1;
201
    int ret;
202
    int index = av_index_search_timestamp(st, timestamp, flags);
203
    int64_t pos, pts;
204
205
    /* if found, seek there */
206
    if (index >= 0){
207
        wc->block_parsed = 1;
208
        url_fseek(&s->pb, st->index_entries[index].pos, SEEK_SET);
209
        return 0;
210
    }
211
    /* if timestamp is out of bounds, return error */
212
    if(timestamp < 0 || timestamp >= s->duration)
213
        return -1;
214
215
    pos = url_ftell(&s->pb);
216
    do{
217
        ret = av_read_frame(s, pkt);
218
        if (ret < 0){
219
            url_fseek(&s->pb, pos, SEEK_SET);
220
            return -1;
221
        }
222
        pts = pkt->pts;
223
        av_free_packet(pkt);
224
    }while(pts < timestamp);
225
    return 0;
226
}
227
228 730581f3 Kostya Shishkov
AVInputFormat wv_demuxer = {
229
    "wv",
230
    "WavPack",
231
    sizeof(WVContext),
232
    wv_probe,
233
    wv_read_header,
234
    wv_read_packet,
235
    wv_read_close,
236 49561f99 Kostya Shishkov
    wv_read_seek,
237 730581f3 Kostya Shishkov
};