Statistics
| Branch: | Revision:

ffmpeg / libavformat / wv.c @ 1ae7518d

History | View | Annotate | Download (5.44 KB)

1
/*
2
 * WavPack 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
 * 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
    int block_parsed;
54
    uint8_t extra[WV_EXTRA_SIZE];
55
}WVContext;
56

    
57
static int wv_probe(AVProbeData *p)
58
{
59
    /* check file header */
60
    if (p->buf_size <= 32)
61
        return 0;
62
    if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
63
        p->buf[2] == 'p' && p->buf[3] == 'k')
64
        return AVPROBE_SCORE_MAX;
65
    else
66
        return 0;
67
}
68

    
69
static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb)
70
{
71
    WVContext *wc = ctx->priv_data;
72
    uint32_t tag, ver;
73
    int size;
74
    int rate, bpp, chan;
75

    
76
    tag = get_le32(pb);
77
    if (tag != MKTAG('w', 'v', 'p', 'k'))
78
        return -1;
79
    size = get_le32(pb);
80
    if(size < 24 || size > WV_BLOCK_LIMIT){
81
        av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
82
        return -1;
83
    }
84
    wc->blksize = size;
85
    ver = get_le16(pb);
86
    if(ver < 0x402 || ver > 0x40F){
87
        av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
88
        return -1;
89
    }
90
    get_byte(pb); // track no
91
    get_byte(pb); // track sub index
92
    get_le32(pb); // total samples in file
93
    get_le32(pb); // offset in samples of current block
94
    get_buffer(pb, wc->extra, WV_EXTRA_SIZE);
95
    wc->flags = AV_RL32(wc->extra + 4);
96
    //parse flags
97
    if(wc->flags & WV_FLOAT){
98
        av_log(ctx, AV_LOG_ERROR, "Floating point data is not supported\n");
99
        return -1;
100
    }
101
    if(wc->flags & WV_HYBRID){
102
        av_log(ctx, AV_LOG_ERROR, "Hybrid coding mode is not supported\n");
103
        return -1;
104
    }
105
    if(wc->flags & WV_INT32){
106
        av_log(ctx, AV_LOG_ERROR, "Integer point data is not supported\n");
107
        return -1;
108
    }
109

    
110
    bpp = ((wc->flags & 3) + 1) << 3;
111
    chan = 1 + !(wc->flags & WV_MONO);
112
    rate = wv_rates[(wc->flags >> 23) & 0xF];
113
    if(rate == -1){
114
        av_log(ctx, AV_LOG_ERROR, "Unknown sampling rate\n");
115
        return -1;
116
    }
117
    if(!wc->bpp) wc->bpp = bpp;
118
    if(!wc->chan) wc->chan = chan;
119
    if(!wc->rate) wc->rate = rate;
120

    
121
    if(wc->flags && bpp != wc->bpp){
122
        av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
123
        return -1;
124
    }
125
    if(wc->flags && chan != wc->chan){
126
        av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
127
        return -1;
128
    }
129
    if(wc->flags && rate != wc->rate){
130
        av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
131
        return -1;
132
    }
133
    wc->blksize = size - 24;
134
    return 0;
135
}
136

    
137
static int wv_read_header(AVFormatContext *s,
138
                          AVFormatParameters *ap)
139
{
140
    ByteIOContext *pb = &s->pb;
141
    WVContext *wc = s->priv_data;
142
    AVStream *st;
143

    
144
    if(wv_read_block_header(s, pb) < 0)
145
        return -1;
146

    
147
    wc->block_parsed = 0;
148
    /* now we are ready: build format streams */
149
    st = av_new_stream(s, 0);
150
    if (!st)
151
        return -1;
152
    st->codec->codec_type = CODEC_TYPE_AUDIO;
153
    st->codec->codec_id = CODEC_ID_WAVPACK;
154
    st->codec->channels = wc->chan;
155
    st->codec->sample_rate = wc->rate;
156
    st->codec->bits_per_sample = wc->bpp;
157
    av_set_pts_info(st, 64, 1, wc->rate);
158
    return 0;
159
}
160

    
161
static int wv_read_packet(AVFormatContext *s,
162
                          AVPacket *pkt)
163
{
164
    WVContext *wc = s->priv_data;
165
    int ret;
166

    
167
    if (url_feof(&s->pb))
168
        return -EIO;
169
    if(wc->block_parsed){
170
        if(wv_read_block_header(s, &s->pb) < 0)
171
            return -1;
172
    }
173

    
174
    if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE) < 0)
175
        return AVERROR_NOMEM;
176
    memcpy(pkt->data, wc->extra, WV_EXTRA_SIZE);
177
    ret = get_buffer(&s->pb, pkt->data + WV_EXTRA_SIZE, wc->blksize);
178
    if(ret != wc->blksize){
179
        av_free_packet(pkt);
180
        return AVERROR_IO;
181
    }
182
    pkt->stream_index = 0;
183
    wc->block_parsed = 1;
184
    pkt->size = ret + WV_EXTRA_SIZE;
185

    
186
    return 0;
187
}
188

    
189
static int wv_read_close(AVFormatContext *s)
190
{
191
    return 0;
192
}
193

    
194
AVInputFormat wv_demuxer = {
195
    "wv",
196
    "WavPack",
197
    sizeof(WVContext),
198
    wv_probe,
199
    wv_read_header,
200
    wv_read_packet,
201
    wv_read_close,
202
};