vocdec.c 6.69 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
    if (~version + 0x1234 != check)
        return 10;

    return AVPROBE_SCORE_MAX;
}

41
static int voc_read_header(AVFormatContext *s)
42
{
43
    VocDecContext *voc = s->priv_data;
44
    AVIOContext *pb = s->pb;
45 46 47
    int header_size;
    AVStream *st;

48
    avio_skip(pb, 20);
49
    header_size = avio_rl16(pb) - 22;
50
    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
    avio_skip(pb, header_size);
55
    st = avformat_new_stream(s, NULL);
56
    if (!st)
57
        return AVERROR(ENOMEM);
58
    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
59 60 61 62 63 64

    voc->remaining_size = 0;
    return 0;
}

int
65
ff_voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
66
{
67
    VocDecContext *voc = s->priv_data;
68
    AVCodecContext *dec = st->codec;
69
    AVIOContext *pb = s->pb;
70
    VocType type;
71
    int size, tmp_codec=-1;
72 73
    int sample_rate = 0;
    int channels = 1;
74 75 76 77 78 79 80 81 82
    int64_t duration;
    int ret;

    av_add_index_entry(st,
                       avio_tell(pb),
                       voc->pts,
                       voc->remaining_size,
                       0,
                       AVINDEX_KEYFRAME);
83 84

    while (!voc->remaining_size) {
85
        type = avio_r8(pb);
86
        if (type == VOC_TYPE_EOF)
87
            return AVERROR_EOF;
88
        voc->remaining_size = avio_rl24(pb);
89
        if (!voc->remaining_size) {
90
            if (!s->pb->seekable)
91
                return AVERROR(EIO);
92
            voc->remaining_size = avio_size(pb) - avio_tell(pb);
93
        }
94 95 96 97
        max_size -= 4;

        switch (type) {
        case VOC_TYPE_VOICE_DATA:
98 99 100 101 102
            if (!dec->sample_rate) {
                dec->sample_rate = 1000000 / (256 - avio_r8(pb));
                if (sample_rate)
                    dec->sample_rate = sample_rate;
                avpriv_set_pts_info(st, 64, 1, dec->sample_rate);
103 104
                dec->channels = channels;
                dec->bits_per_coded_sample = av_get_bits_per_sample(dec->codec_id);
105 106
            } else
                avio_skip(pb, 1);
107
            tmp_codec = avio_r8(pb);
108 109 110 111 112 113 114 115 116
            voc->remaining_size -= 2;
            max_size -= 2;
            channels = 1;
            break;

        case VOC_TYPE_VOICE_DATA_CONT:
            break;

        case VOC_TYPE_EXTENDED:
117 118 119
            sample_rate = avio_rl16(pb);
            avio_r8(pb);
            channels = avio_r8(pb) + 1;
120 121 122 123 124 125
            sample_rate = 256000000 / (channels * (65536 - sample_rate));
            voc->remaining_size = 0;
            max_size -= 4;
            break;

        case VOC_TYPE_NEW_VOICE_DATA:
126 127 128
            if (!dec->sample_rate) {
                dec->sample_rate = avio_rl32(pb);
                avpriv_set_pts_info(st, 64, 1, dec->sample_rate);
129 130
                dec->bits_per_coded_sample = avio_r8(pb);
                dec->channels = avio_r8(pb);
131
            } else
132
                avio_skip(pb, 6);
133
            tmp_codec = avio_rl16(pb);
134
            avio_skip(pb, 4);
135 136 137 138 139
            voc->remaining_size -= 12;
            max_size -= 12;
            break;

        default:
140
            avio_skip(pb, voc->remaining_size);
141 142 143 144
            max_size -= voc->remaining_size;
            voc->remaining_size = 0;
            break;
        }
145 146 147 148
    }

    if (tmp_codec >= 0) {
        tmp_codec = ff_codec_get_id(ff_voc_codec_tags, tmp_codec);
149
        if (dec->codec_id == AV_CODEC_ID_NONE)
150 151 152
            dec->codec_id = tmp_codec;
        else if (dec->codec_id != tmp_codec)
            av_log(s, AV_LOG_WARNING, "Ignoring mid-stream change in audio codec\n");
153 154
        if (dec->codec_id == AV_CODEC_ID_NONE) {
            if (s->audio_codec_id == AV_CODEC_ID_NONE) {
155 156 157 158
                av_log(s, AV_LOG_ERROR, "unknown codec tag\n");
                return AVERROR(EINVAL);
            }
            av_log(s, AV_LOG_WARNING, "unknown codec tag\n");
159
        }
160 161
    }

162
    dec->bit_rate = dec->sample_rate * dec->channels * dec->bits_per_coded_sample;
163 164

    if (max_size <= 0)
Alex Beregszaszi's avatar
Alex Beregszaszi committed
165
        max_size = 2048;
166 167
    size = FFMIN(voc->remaining_size, max_size);
    voc->remaining_size -= size;
168 169 170 171 172 173 174 175 176 177 178

    ret = av_get_packet(pb, pkt, size);
    pkt->dts = pkt->pts = voc->pts;

    duration = av_get_audio_frame_duration(st->codec, size);
    if (duration > 0 && voc->pts != AV_NOPTS_VALUE)
        voc->pts += duration;
    else
        voc->pts = AV_NOPTS_VALUE;

    return ret;
179 180 181 182
}

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

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
static int voc_read_seek(AVFormatContext *s, int stream_index,
                         int64_t timestamp, int flags)
{
    VocDecContext *voc = s->priv_data;
    AVStream *st = s->streams[stream_index];
    int index = av_index_search_timestamp(st, timestamp, flags);

    if (index >= 0 && index < st->nb_index_entries - 1) {
        AVIndexEntry *e = &st->index_entries[index];
        avio_seek(s->pb, e->pos, SEEK_SET);
        voc->pts = e->timestamp;
        voc->remaining_size = e->size;
        return 0;
    } else if (st->nb_index_entries && st->index_entries[0].timestamp <= timestamp) {
        AVIndexEntry *e = &st->index_entries[st->nb_index_entries - 1];
        // prepare context for seek_frame_generic()
        voc->pts = e->timestamp;
        voc->remaining_size = e->size;
    }
    return -1;
}

208
AVInputFormat ff_voc_demuxer = {
209
    .name           = "voc",
210
    .long_name      = NULL_IF_CONFIG_SMALL("Creative Voice"),
211 212 213 214
    .priv_data_size = sizeof(VocDecContext),
    .read_probe     = voc_probe,
    .read_header    = voc_read_header,
    .read_packet    = voc_read_packet,
215
    .read_seek      = voc_read_seek,
216
    .codec_tag      = (const AVCodecTag* const []){ ff_voc_codec_tags, 0 },
217
};