vocdec.c 4.41 KB
Newer Older
1 2 3 4
/*
 * Creative Voice File demuxer.
 * Copyright (c) 2006  Aurelien Jacobs <aurel@gnuage.org>
 *
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
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 21
 */

22
#include "libavutil/intreadwrite.h"
23
#include "voc.h"
24
#include "internal.h"
25 26 27 28 29 30


static int voc_probe(AVProbeData *p)
{
    int version, check;

31
    if (memcmp(p->buf, ff_voc_magic, sizeof(ff_voc_magic) - 1))
32
        return 0;
33 34
    version = AV_RL16(p->buf + 22);
    check = AV_RL16(p->buf + 24);
35 36 37 38 39 40 41 42
    if (~version + 0x1234 != check)
        return 10;

    return AVPROBE_SCORE_MAX;
}

static int voc_read_header(AVFormatContext *s, AVFormatParameters *ap)
{
43
    VocDecContext *voc = s->priv_data;
44
    ByteIOContext *pb = s->pb;
45 46 47 48 49 50
    int header_size;
    AVStream *st;

    url_fskip(pb, 20);
    header_size = get_le16(pb) - 22;
    if (header_size != 4) {
Alex Beregszaszi's avatar
Alex Beregszaszi committed
51
        av_log(s, AV_LOG_ERROR, "unknown header size: %d\n", header_size);
52
        return AVERROR(ENOSYS);
53 54 55 56
    }
    url_fskip(pb, header_size);
    st = av_new_stream(s, 0);
    if (!st)
57
        return AVERROR(ENOMEM);
58
    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
59 60 61 62 63 64 65 66

    voc->remaining_size = 0;
    return 0;
}

int
voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
{
67
    VocDecContext *voc = s->priv_data;
68
    AVCodecContext *dec = st->codec;
69
    ByteIOContext *pb = s->pb;
70
    VocType type;
71 72 73 74 75 76 77
    int size;
    int sample_rate = 0;
    int channels = 1;

    while (!voc->remaining_size) {
        type = get_byte(pb);
        if (type == VOC_TYPE_EOF)
78
            return AVERROR(EIO);
79
        voc->remaining_size = get_le24(pb);
80 81 82 83 84
        if (!voc->remaining_size) {
            if (url_is_streamed(s->pb))
                return AVERROR(EIO);
            voc->remaining_size = url_fsize(pb) - url_ftell(pb);
        }
85 86 87 88 89 90 91 92
        max_size -= 4;

        switch (type) {
        case VOC_TYPE_VOICE_DATA:
            dec->sample_rate = 1000000 / (256 - get_byte(pb));
            if (sample_rate)
                dec->sample_rate = sample_rate;
            dec->channels = channels;
93
            dec->codec_id = ff_codec_get_id(ff_voc_codec_tags, get_byte(pb));
94
            dec->bits_per_coded_sample = av_get_bits_per_sample(dec->codec_id);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
            voc->remaining_size -= 2;
            max_size -= 2;
            channels = 1;
            break;

        case VOC_TYPE_VOICE_DATA_CONT:
            break;

        case VOC_TYPE_EXTENDED:
            sample_rate = get_le16(pb);
            get_byte(pb);
            channels = get_byte(pb) + 1;
            sample_rate = 256000000 / (channels * (65536 - sample_rate));
            voc->remaining_size = 0;
            max_size -= 4;
            break;

        case VOC_TYPE_NEW_VOICE_DATA:
            dec->sample_rate = get_le32(pb);
114
            dec->bits_per_coded_sample = get_byte(pb);
115
            dec->channels = get_byte(pb);
116
            dec->codec_id = ff_codec_get_id(ff_voc_codec_tags, get_le16(pb));
117 118 119 120 121 122 123 124 125 126 127 128 129
            url_fskip(pb, 4);
            voc->remaining_size -= 12;
            max_size -= 12;
            break;

        default:
            url_fskip(pb, voc->remaining_size);
            max_size -= voc->remaining_size;
            voc->remaining_size = 0;
            break;
        }
    }

130
    dec->bit_rate = dec->sample_rate * dec->bits_per_coded_sample;
131 132

    if (max_size <= 0)
Alex Beregszaszi's avatar
Alex Beregszaszi committed
133
        max_size = 2048;
134 135 136 137 138 139 140 141 142 143
    size = FFMIN(voc->remaining_size, max_size);
    voc->remaining_size -= size;
    return av_get_packet(pb, pkt, size);
}

static int voc_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    return voc_get_packet(s, pkt, s->streams[0], 0);
}

144
AVInputFormat voc_demuxer = {
145
    "voc",
146
    NULL_IF_CONFIG_SMALL("Creative Voice file format"),
147
    sizeof(VocDecContext),
148 149 150
    voc_probe,
    voc_read_header,
    voc_read_packet,
151
    .codec_tag=(const AVCodecTag* const []){ff_voc_codec_tags, 0},
152
};