wvdec.c 10.2 KB
Newer Older
1 2
/*
 * WavPack demuxer
3
 * Copyright (c) 2006,2011 Konstantin Shishkov
4
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * 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 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 20 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22
#include "libavutil/channel_layout.h"
23
#include "libavutil/intreadwrite.h"
24
#include "libavutil/dict.h"
25
#include "avformat.h"
26
#include "internal.h"
27 28
#include "apetag.h"
#include "id3v1.h"
29
#include "wv.h"
30

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

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

51
typedef struct WVContext {
52
    uint8_t block_header[WV_HEADER_SIZE];
53
    WvHeader header;
54
    int rate, chan, bpp;
55 56
    uint32_t chmask;
    int multichannel;
57
    int block_parsed;
58
    int64_t pos;
59 60

    int64_t apetag_start;
61
} WVContext;
62

63
static int wv_probe(const AVProbeData *p)
64 65 66 67
{
    /* check file header */
    if (p->buf_size <= 32)
        return 0;
68 69 70 71 72
    if (AV_RL32(&p->buf[0]) == MKTAG('w', 'v', 'p', 'k') &&
        AV_RL32(&p->buf[4]) >= 24 &&
        AV_RL32(&p->buf[4]) <= WV_BLOCK_LIMIT &&
        AV_RL16(&p->buf[8]) >= 0x402 &&
        AV_RL16(&p->buf[8]) <= 0x410)
73 74 75 76 77
        return AVPROBE_SCORE_MAX;
    else
        return 0;
}

78
static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
79 80
{
    WVContext *wc = ctx->priv_data;
81
    int ret;
82
    int rate, bpp, chan;
83
    uint32_t chmask, flags;
84

85
    wc->pos = avio_tell(pb);
86 87 88 89 90

    /* don't return bogus packets with the ape tag data */
    if (wc->apetag_start && wc->pos >= wc->apetag_start)
        return AVERROR_EOF;

91 92 93 94
    ret = avio_read(pb, wc->block_header, WV_HEADER_SIZE);
    if (ret != WV_HEADER_SIZE)
        return (ret < 0) ? ret : AVERROR_EOF;

95 96 97 98
    ret = ff_wv_parse_header(&wc->header, wc->block_header);
    if (ret < 0) {
        av_log(ctx, AV_LOG_ERROR, "Invalid block header.\n");
        return ret;
99
    }
100

101 102 103 104 105
    if (wc->header.flags & WV_DSD) {
        avpriv_report_missing_feature(ctx, "WV DSD");
        return AVERROR_PATCHWELCOME;
    }

106
    if (wc->header.version < 0x402 || wc->header.version > 0x410) {
107 108
        avpriv_report_missing_feature(ctx, "WV version 0x%03X",
                                      wc->header.version);
109
        return AVERROR_PATCHWELCOME;
110
    }
111

112 113
    /* Blocks with zero samples don't contain actual audio information
     * and should be ignored */
114
    if (!wc->header.samples)
115
        return 0;
116
    // parse flags
117 118 119 120 121 122
    flags  = wc->header.flags;
    bpp    = ((flags & 3) + 1) << 3;
    chan   = 1 + !(flags & WV_MONO);
    chmask = flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
    rate   = wv_rates[(flags >> 23) & 0xF];
    wc->multichannel = !(wc->header.initial && wc->header.final);
123 124
    if (wc->multichannel) {
        chan   = wc->chan;
125
        chmask = wc->chmask;
126
    }
127
    if ((rate == -1 || !chan) && !wc->block_parsed) {
128
        int64_t block_end = avio_tell(pb) + wc->header.blocksize;
129
        if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
130 131
            av_log(ctx, AV_LOG_ERROR,
                   "Cannot determine additional parameters\n");
132
            return AVERROR_INVALIDDATA;
133
        }
134
        while (avio_tell(pb) < block_end && !avio_feof(pb)) {
135
            int id, size;
136
            id   = avio_r8(pb);
137
            size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
138
            size <<= 1;
139
            if (id & 0x40)
140
                size--;
141
            switch (id & 0x3F) {
142
            case 0xD:
143 144 145
                if (size <= 1) {
                    av_log(ctx, AV_LOG_ERROR,
                           "Insufficient channel information\n");
146
                    return AVERROR_INVALIDDATA;
147
                }
148
                chan = avio_r8(pb);
149
                switch (size - 2) {
150
                case 0:
151
                    chmask = avio_r8(pb);
152 153
                    break;
                case 1:
154
                    chmask = avio_rl16(pb);
155 156
                    break;
                case 2:
157
                    chmask = avio_rl24(pb);
158 159
                    break;
                case 3:
160
                    chmask = avio_rl32(pb);
161
                    break;
162
                case 4:
163
                    avio_skip(pb, 1);
164
                    chan  |= (avio_r8(pb) & 0xF) << 8;
165
                    chan  += 1;
166
                    chmask = avio_rl24(pb);
167
                    break;
168 169 170 171 172 173
                case 5:
                    avio_skip(pb, 1);
                    chan  |= (avio_r8(pb) & 0xF) << 8;
                    chan  += 1;
                    chmask = avio_rl32(pb);
                    break;
174
                default:
175 176
                    av_log(ctx, AV_LOG_ERROR,
                           "Invalid channel info size %d\n", size);
177
                    return AVERROR_INVALIDDATA;
178 179 180
                }
                break;
            case 0x27:
181
                rate = avio_rl24(pb);
182
                break;
183
            default:
184
                avio_skip(pb, size);
185
            }
186
            if (id & 0x40)
187
                avio_skip(pb, 1);
188
        }
189 190 191
        if (rate == -1) {
            av_log(ctx, AV_LOG_ERROR,
                   "Cannot determine custom sampling rate\n");
192
            return AVERROR_INVALIDDATA;
193
        }
194
        avio_seek(pb, block_end - wc->header.blocksize, SEEK_SET);
195
    }
196 197 198 199 200 201 202 203
    if (!wc->bpp)
        wc->bpp    = bpp;
    if (!wc->chan)
        wc->chan   = chan;
    if (!wc->chmask)
        wc->chmask = chmask;
    if (!wc->rate)
        wc->rate   = rate;
204

205
    if (flags && bpp != wc->bpp) {
206 207 208
        av_log(ctx, AV_LOG_ERROR,
               "Bits per sample differ, this block: %i, header block: %i\n",
               bpp, wc->bpp);
209
        return AVERROR_INVALIDDATA;
210
    }
211
    if (flags && !wc->multichannel && chan != wc->chan) {
212 213 214
        av_log(ctx, AV_LOG_ERROR,
               "Channels differ, this block: %i, header block: %i\n",
               chan, wc->chan);
215
        return AVERROR_INVALIDDATA;
216
    }
217
    if (flags && rate != -1 && rate != wc->rate) {
218 219 220
        av_log(ctx, AV_LOG_ERROR,
               "Sampling rate differ, this block: %i, header block: %i\n",
               rate, wc->rate);
221
        return AVERROR_INVALIDDATA;
222 223 224 225
    }
    return 0;
}

226
static int wv_read_header(AVFormatContext *s)
227
{
228
    AVIOContext *pb = s->pb;
229 230
    WVContext *wc = s->priv_data;
    AVStream *st;
231
    int ret;
232

233
    wc->block_parsed = 0;
234
    for (;;) {
235
        if ((ret = wv_read_block_header(s, pb)) < 0)
236
            return ret;
237 238
        if (!wc->header.samples)
            avio_skip(pb, wc->header.blocksize);
239 240 241
        else
            break;
    }
242 243

    /* now we are ready: build format streams */
244
    st = avformat_new_stream(s, NULL);
245
    if (!st)
246
        return AVERROR(ENOMEM);
247 248 249 250 251 252
    st->codecpar->codec_type            = AVMEDIA_TYPE_AUDIO;
    st->codecpar->codec_id              = AV_CODEC_ID_WAVPACK;
    st->codecpar->channels              = wc->chan;
    st->codecpar->channel_layout        = wc->chmask;
    st->codecpar->sample_rate           = wc->rate;
    st->codecpar->bits_per_coded_sample = wc->bpp;
253
    avpriv_set_pts_info(st, 64, 1, wc->rate);
254
    st->start_time = 0;
255 256
    if (wc->header.total_samples != 0xFFFFFFFFu)
        st->duration = wc->header.total_samples;
257

258
    if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
259
        int64_t cur = avio_tell(s->pb);
260
        wc->apetag_start = ff_ape_parse_tag(s);
261
        if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
262
            ff_id3v1_read(s);
263
        avio_seek(s->pb, cur, SEEK_SET);
264 265
    }

266 267 268
    return 0;
}

269
static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
270 271
{
    WVContext *wc = s->priv_data;
Diego Biurrun's avatar
Diego Biurrun committed
272
    int ret;
273
    int off;
274
    int64_t pos;
275
    uint32_t block_samples;
276

277
    if (avio_feof(s->pb))
278
        return AVERROR_EOF;
279
    if (wc->block_parsed) {
280
        if ((ret = wv_read_block_header(s, s->pb)) < 0)
281
            return ret;
282 283
    }

284
    pos = wc->pos;
285
    if (av_new_packet(pkt, wc->header.blocksize + WV_HEADER_SIZE) < 0)
286
        return AVERROR(ENOMEM);
287
    memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE);
288 289
    ret = avio_read(s->pb, pkt->data + WV_HEADER_SIZE, wc->header.blocksize);
    if (ret != wc->header.blocksize) {
290
        av_packet_unref(pkt);
291
        return AVERROR(EIO);
292
    }
293
    while (!(wc->header.flags & WV_FLAG_FINAL_BLOCK)) {
294
        if ((ret = wv_read_block_header(s, s->pb)) < 0) {
295
            av_packet_unref(pkt);
296 297 298
            return ret;
        }

299
        off = pkt->size;
300
        if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->header.blocksize)) < 0) {
301
            av_packet_unref(pkt);
302
            return ret;
303
        }
304 305
        memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE);

306 307
        ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->header.blocksize);
        if (ret != wc->header.blocksize) {
308
            av_packet_unref(pkt);
309
            return (ret < 0) ? ret : AVERROR_EOF;
310 311
        }
    }
312
    pkt->stream_index = 0;
wm4's avatar
wm4 committed
313
    pkt->pos          = pos;
314
    wc->block_parsed  = 1;
315 316
    pkt->pts          = wc->header.block_idx;
    block_samples     = wc->header.samples;
317
    if (block_samples > INT32_MAX)
318 319
        av_log(s, AV_LOG_WARNING,
               "Too many samples in block: %"PRIu32"\n", block_samples);
320 321 322
    else
        pkt->duration = block_samples;

323 324 325
    return 0;
}

326
AVInputFormat ff_wv_demuxer = {
327 328 329 330 331 332
    .name           = "wv",
    .long_name      = NULL_IF_CONFIG_SMALL("WavPack"),
    .priv_data_size = sizeof(WVContext),
    .read_probe     = wv_probe,
    .read_header    = wv_read_header,
    .read_packet    = wv_read_packet,
wm4's avatar
wm4 committed
333
    .flags          = AVFMT_GENERIC_INDEX,
334
};