split voc.c into vocdec.c and vocenc.c
[libav.git] / libavformat / vocdec.c
CommitLineData
ea395e8c
AJ
1/*
2 * Creative Voice File demuxer.
3 * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
ea395e8c
AJ
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
ea395e8c 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
ea395e8c
AJ
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
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
ea395e8c
AJ
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "avformat.h"
9d9f4119 23#include "riff.h" /* for CodecTag */
ea395e8c
AJ
24#include "voc.h"
25
26
ea395e8c
AJ
27static const int voc_max_pkt_size = 2048;
28static const unsigned char voc_magic[] = "Creative Voice File\x1A";
29
30static const CodecTag voc_codec_tags[] = {
31 {CODEC_ID_PCM_U8, 0x00},
2433f24f
AJ
32 {CODEC_ID_ADPCM_SBPRO_4, 0x01},
33 {CODEC_ID_ADPCM_SBPRO_3, 0x02},
34 {CODEC_ID_ADPCM_SBPRO_2, 0x03},
ea395e8c
AJ
35 {CODEC_ID_PCM_S16LE, 0x04},
36 {CODEC_ID_PCM_ALAW, 0x06},
37 {CODEC_ID_PCM_MULAW, 0x07},
38 {CODEC_ID_ADPCM_CT, 0x0200},
39 {0, 0},
40};
41
42
ea395e8c
AJ
43static int voc_probe(AVProbeData *p)
44{
45 int version, check;
46
47 if (p->buf_size < 26)
48 return 0;
49 if (memcmp(p->buf, voc_magic, sizeof(voc_magic) - 1))
50 return 0;
51 version = p->buf[22] | (p->buf[23] << 8);
52 check = p->buf[24] | (p->buf[25] << 8);
53 if (~version + 0x1234 != check)
54 return 10;
55
56 return AVPROBE_SCORE_MAX;
57}
58
59static int voc_read_header(AVFormatContext *s, AVFormatParameters *ap)
60{
61 voc_dec_context_t *voc = s->priv_data;
62 ByteIOContext *pb = &s->pb;
63 int header_size;
64 AVStream *st;
65
66 url_fskip(pb, 20);
67 header_size = get_le16(pb) - 22;
68 if (header_size != 4) {
69 av_log(s, AV_LOG_ERROR, "unkown header size: %d\n", header_size);
70 return AVERROR_NOTSUPP;
71 }
72 url_fskip(pb, header_size);
73 st = av_new_stream(s, 0);
74 if (!st)
75 return AVERROR_NOMEM;
76 st->codec->codec_type = CODEC_TYPE_AUDIO;
77
78 voc->remaining_size = 0;
79 return 0;
80}
81
82int
83voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
84{
85 voc_dec_context_t *voc = s->priv_data;
86 AVCodecContext *dec = st->codec;
87 ByteIOContext *pb = &s->pb;
88 voc_type_t type;
89 int size;
90 int sample_rate = 0;
91 int channels = 1;
92
93 while (!voc->remaining_size) {
94 type = get_byte(pb);
95 if (type == VOC_TYPE_EOF)
96 return AVERROR_IO;
97 voc->remaining_size = get_le24(pb);
98 max_size -= 4;
99
100 switch (type) {
101 case VOC_TYPE_VOICE_DATA:
102 dec->sample_rate = 1000000 / (256 - get_byte(pb));
103 if (sample_rate)
104 dec->sample_rate = sample_rate;
105 dec->channels = channels;
106 dec->codec_id = codec_get_id(voc_codec_tags, get_byte(pb));
5da71469 107 dec->bits_per_sample = av_get_bits_per_sample(dec->codec_id);
ea395e8c
AJ
108 voc->remaining_size -= 2;
109 max_size -= 2;
110 channels = 1;
111 break;
112
113 case VOC_TYPE_VOICE_DATA_CONT:
114 break;
115
116 case VOC_TYPE_EXTENDED:
117 sample_rate = get_le16(pb);
118 get_byte(pb);
119 channels = get_byte(pb) + 1;
120 sample_rate = 256000000 / (channels * (65536 - sample_rate));
121 voc->remaining_size = 0;
122 max_size -= 4;
123 break;
124
125 case VOC_TYPE_NEW_VOICE_DATA:
126 dec->sample_rate = get_le32(pb);
127 dec->bits_per_sample = get_byte(pb);
128 dec->channels = get_byte(pb);
129 dec->codec_id = codec_get_id(voc_codec_tags, get_le16(pb));
130 url_fskip(pb, 4);
131 voc->remaining_size -= 12;
132 max_size -= 12;
133 break;
134
135 default:
136 url_fskip(pb, voc->remaining_size);
137 max_size -= voc->remaining_size;
138 voc->remaining_size = 0;
139 break;
140 }
141 }
142
143 dec->bit_rate = dec->sample_rate * dec->bits_per_sample;
144
145 if (max_size <= 0)
146 max_size = voc_max_pkt_size;
147 size = FFMIN(voc->remaining_size, max_size);
148 voc->remaining_size -= size;
149 return av_get_packet(pb, pkt, size);
150}
151
152static int voc_read_packet(AVFormatContext *s, AVPacket *pkt)
153{
154 return voc_get_packet(s, pkt, s->streams[0], 0);
155}
156
157static int voc_read_close(AVFormatContext *s)
158{
159 return 0;
160}
161
ff70e601 162AVInputFormat voc_demuxer = {
ea395e8c
AJ
163 "voc",
164 "Creative Voice File format",
165 sizeof(voc_dec_context_t),
166 voc_probe,
167 voc_read_header,
168 voc_read_packet,
169 voc_read_close,
170};