Statistics
| Branch: | Revision:

ffmpeg / libavformat / vocdec.c @ e356fc57

History | View | Annotate | Download (4.97 KB)

1 ea395e8c Aurelien Jacobs
/*
2
 * Creative Voice File demuxer.
3
 * Copyright (c) 2006  Aurelien Jacobs <aurel@gnuage.org>
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 ea395e8c Aurelien Jacobs
 * 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 ea395e8c Aurelien Jacobs
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 ea395e8c Aurelien Jacobs
 * 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 e5a389a1 Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 ea395e8c Aurelien Jacobs
 */
21
22 6a5d31ac Diego Biurrun
#include "libavutil/intreadwrite.h"
23 ea395e8c Aurelien Jacobs
#include "voc.h"
24 80b39e1c Francesco Lavra
#include "internal.h"
25 ea395e8c Aurelien Jacobs
26
27
static int voc_probe(AVProbeData *p)
28
{
29
    int version, check;
30
31 090a4179 Aurelien Jacobs
    if (memcmp(p->buf, ff_voc_magic, sizeof(ff_voc_magic) - 1))
32 ea395e8c Aurelien Jacobs
        return 0;
33 6bca498d Aurelien Jacobs
    version = AV_RL16(p->buf + 22);
34
    check = AV_RL16(p->buf + 24);
35 ea395e8c Aurelien Jacobs
    if (~version + 0x1234 != check)
36
        return 10;
37
38
    return AVPROBE_SCORE_MAX;
39
}
40
41
static int voc_read_header(AVFormatContext *s, AVFormatParameters *ap)
42
{
43 e998ba4f Aurelien Jacobs
    VocDecContext *voc = s->priv_data;
44 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
45 ea395e8c Aurelien Jacobs
    int header_size;
46
    AVStream *st;
47
48 e356fc57 Anton Khirnov
    avio_seek(pb, 20, SEEK_CUR);
49 b7effd4e Anton Khirnov
    header_size = avio_rl16(pb) - 22;
50 ea395e8c Aurelien Jacobs
    if (header_size != 4) {
51 cbc09a7d Alex Beregszaszi
        av_log(s, AV_LOG_ERROR, "unknown header size: %d\n", header_size);
52 85565db0 Panagiotis Issaris
        return AVERROR(ENOSYS);
53 ea395e8c Aurelien Jacobs
    }
54 e356fc57 Anton Khirnov
    avio_seek(pb, header_size, SEEK_CUR);
55 ea395e8c Aurelien Jacobs
    st = av_new_stream(s, 0);
56
    if (!st)
57 769e10f0 Panagiotis Issaris
        return AVERROR(ENOMEM);
58 72415b2a Stefano Sabatini
    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
59 ea395e8c Aurelien Jacobs
60
    voc->remaining_size = 0;
61
    return 0;
62
}
63
64
int
65
voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
66
{
67 e998ba4f Aurelien Jacobs
    VocDecContext *voc = s->priv_data;
68 ea395e8c Aurelien Jacobs
    AVCodecContext *dec = st->codec;
69 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
70 e998ba4f Aurelien Jacobs
    VocType type;
71 f61cbc22 Justin Ruggles
    int size, tmp_codec=-1;
72 ea395e8c Aurelien Jacobs
    int sample_rate = 0;
73
    int channels = 1;
74
75
    while (!voc->remaining_size) {
76 b7effd4e Anton Khirnov
        type = avio_r8(pb);
77 ea395e8c Aurelien Jacobs
        if (type == VOC_TYPE_EOF)
78 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
79 b7effd4e Anton Khirnov
        voc->remaining_size = avio_rl24(pb);
80 c351524c Peter Ross
        if (!voc->remaining_size) {
81
            if (url_is_streamed(s->pb))
82
                return AVERROR(EIO);
83
            voc->remaining_size = url_fsize(pb) - url_ftell(pb);
84
        }
85 ea395e8c Aurelien Jacobs
        max_size -= 4;
86
87
        switch (type) {
88
        case VOC_TYPE_VOICE_DATA:
89 b7effd4e Anton Khirnov
            dec->sample_rate = 1000000 / (256 - avio_r8(pb));
90 ea395e8c Aurelien Jacobs
            if (sample_rate)
91
                dec->sample_rate = sample_rate;
92
            dec->channels = channels;
93 b7effd4e Anton Khirnov
            tmp_codec = avio_r8(pb);
94 dd1c8f3e Luca Abeni
            dec->bits_per_coded_sample = av_get_bits_per_sample(dec->codec_id);
95 ea395e8c Aurelien Jacobs
            voc->remaining_size -= 2;
96
            max_size -= 2;
97
            channels = 1;
98
            break;
99
100
        case VOC_TYPE_VOICE_DATA_CONT:
101
            break;
102
103
        case VOC_TYPE_EXTENDED:
104 b7effd4e Anton Khirnov
            sample_rate = avio_rl16(pb);
105
            avio_r8(pb);
106
            channels = avio_r8(pb) + 1;
107 ea395e8c Aurelien Jacobs
            sample_rate = 256000000 / (channels * (65536 - sample_rate));
108
            voc->remaining_size = 0;
109
            max_size -= 4;
110
            break;
111
112
        case VOC_TYPE_NEW_VOICE_DATA:
113 b7effd4e Anton Khirnov
            dec->sample_rate = avio_rl32(pb);
114
            dec->bits_per_coded_sample = avio_r8(pb);
115
            dec->channels = avio_r8(pb);
116
            tmp_codec = avio_rl16(pb);
117 e356fc57 Anton Khirnov
            avio_seek(pb, 4, SEEK_CUR);
118 ea395e8c Aurelien Jacobs
            voc->remaining_size -= 12;
119
            max_size -= 12;
120
            break;
121
122
        default:
123 e356fc57 Anton Khirnov
            avio_seek(pb, voc->remaining_size, SEEK_CUR);
124 ea395e8c Aurelien Jacobs
            max_size -= voc->remaining_size;
125
            voc->remaining_size = 0;
126
            break;
127
        }
128 f61cbc22 Justin Ruggles
    }
129
130
    if (tmp_codec >= 0) {
131
        tmp_codec = ff_codec_get_id(ff_voc_codec_tags, tmp_codec);
132
        if (dec->codec_id == CODEC_ID_NONE)
133
            dec->codec_id = tmp_codec;
134
        else if (dec->codec_id != tmp_codec)
135
            av_log(s, AV_LOG_WARNING, "Ignoring mid-stream change in audio codec\n");
136 e048a9ca Daniel Kang
        if (dec->codec_id == CODEC_ID_NONE) {
137 f61cbc22 Justin Ruggles
            if (s->audio_codec_id == CODEC_ID_NONE) {
138
                av_log(s, AV_LOG_ERROR, "unknown codec tag\n");
139
                return AVERROR(EINVAL);
140
            }
141
            av_log(s, AV_LOG_WARNING, "unknown codec tag\n");
142 e048a9ca Daniel Kang
        }
143 ea395e8c Aurelien Jacobs
    }
144
145 dd1c8f3e Luca Abeni
    dec->bit_rate = dec->sample_rate * dec->bits_per_coded_sample;
146 ea395e8c Aurelien Jacobs
147
    if (max_size <= 0)
148 fd9a71ac Alex Beregszaszi
        max_size = 2048;
149 ea395e8c Aurelien Jacobs
    size = FFMIN(voc->remaining_size, max_size);
150
    voc->remaining_size -= size;
151
    return av_get_packet(pb, pkt, size);
152
}
153
154
static int voc_read_packet(AVFormatContext *s, AVPacket *pkt)
155
{
156
    return voc_get_packet(s, pkt, s->streams[0], 0);
157
}
158
159 c6610a21 Diego Elio Pettenò
AVInputFormat ff_voc_demuxer = {
160 ea395e8c Aurelien Jacobs
    "voc",
161 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("Creative Voice file format"),
162 e998ba4f Aurelien Jacobs
    sizeof(VocDecContext),
163 ea395e8c Aurelien Jacobs
    voc_probe,
164
    voc_read_header,
165
    voc_read_packet,
166 c1854592 Reimar Döffinger
    .codec_tag=(const AVCodecTag* const []){ff_voc_codec_tags, 0},
167 ea395e8c Aurelien Jacobs
};