vocdec: do not create the stream in read_header()
[libav.git] / libavformat / vocdec.c
CommitLineData
ea395e8c
AJ
1/*
2 * Creative Voice File demuxer.
3 * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav 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 *
2912e87a 12 * Libav 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
2912e87a 18 * License along with Libav; if not, write to the Free Software
e5a389a1 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
ea395e8c
AJ
20 */
21
6a5d31ac 22#include "libavutil/intreadwrite.h"
ea395e8c 23#include "voc.h"
80b39e1c 24#include "internal.h"
ea395e8c 25
ea395e8c 26int
167f3b8d 27ff_voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
ea395e8c 28{
e998ba4f 29 VocDecContext *voc = s->priv_data;
ea395e8c 30 AVCodecContext *dec = st->codec;
ae628ec1 31 AVIOContext *pb = s->pb;
e998ba4f 32 VocType type;
f61cbc22 33 int size, tmp_codec=-1;
ea395e8c
AJ
34 int sample_rate = 0;
35 int channels = 1;
36
37 while (!voc->remaining_size) {
b7effd4e 38 type = avio_r8(pb);
ea395e8c 39 if (type == VOC_TYPE_EOF)
6f3e0b21 40 return AVERROR(EIO);
b7effd4e 41 voc->remaining_size = avio_rl24(pb);
c351524c 42 if (!voc->remaining_size) {
8978feda 43 if (!s->pb->seekable)
c351524c 44 return AVERROR(EIO);
76aa876e 45 voc->remaining_size = avio_size(pb) - avio_tell(pb);
c351524c 46 }
ea395e8c
AJ
47 max_size -= 4;
48
49 switch (type) {
50 case VOC_TYPE_VOICE_DATA:
0883109b
JR
51 if (!dec->sample_rate) {
52 dec->sample_rate = 1000000 / (256 - avio_r8(pb));
53 if (sample_rate)
54 dec->sample_rate = sample_rate;
55 avpriv_set_pts_info(st, 64, 1, dec->sample_rate);
5bbfe193
MS
56 dec->channels = channels;
57 dec->bits_per_coded_sample = av_get_bits_per_sample(dec->codec_id);
0883109b
JR
58 } else
59 avio_skip(pb, 1);
b7effd4e 60 tmp_codec = avio_r8(pb);
ea395e8c
AJ
61 voc->remaining_size -= 2;
62 max_size -= 2;
63 channels = 1;
64 break;
65
66 case VOC_TYPE_VOICE_DATA_CONT:
67 break;
68
69 case VOC_TYPE_EXTENDED:
b7effd4e
AK
70 sample_rate = avio_rl16(pb);
71 avio_r8(pb);
72 channels = avio_r8(pb) + 1;
ea395e8c
AJ
73 sample_rate = 256000000 / (channels * (65536 - sample_rate));
74 voc->remaining_size = 0;
75 max_size -= 4;
76 break;
77
78 case VOC_TYPE_NEW_VOICE_DATA:
0883109b
JR
79 if (!dec->sample_rate) {
80 dec->sample_rate = avio_rl32(pb);
81 avpriv_set_pts_info(st, 64, 1, dec->sample_rate);
5bbfe193
MS
82 dec->bits_per_coded_sample = avio_r8(pb);
83 dec->channels = avio_r8(pb);
0883109b 84 } else
5bbfe193 85 avio_skip(pb, 6);
b7effd4e 86 tmp_codec = avio_rl16(pb);
45a8a02a 87 avio_skip(pb, 4);
ea395e8c
AJ
88 voc->remaining_size -= 12;
89 max_size -= 12;
90 break;
91
92 default:
45a8a02a 93 avio_skip(pb, voc->remaining_size);
ea395e8c
AJ
94 max_size -= voc->remaining_size;
95 voc->remaining_size = 0;
96 break;
97 }
f61cbc22
JR
98 }
99
100 if (tmp_codec >= 0) {
101 tmp_codec = ff_codec_get_id(ff_voc_codec_tags, tmp_codec);
36ef5369 102 if (dec->codec_id == AV_CODEC_ID_NONE)
f61cbc22
JR
103 dec->codec_id = tmp_codec;
104 else if (dec->codec_id != tmp_codec)
105 av_log(s, AV_LOG_WARNING, "Ignoring mid-stream change in audio codec\n");
36ef5369
AK
106 if (dec->codec_id == AV_CODEC_ID_NONE) {
107 if (s->audio_codec_id == AV_CODEC_ID_NONE) {
f61cbc22
JR
108 av_log(s, AV_LOG_ERROR, "unknown codec tag\n");
109 return AVERROR(EINVAL);
110 }
111 av_log(s, AV_LOG_WARNING, "unknown codec tag\n");
e048a9ca 112 }
ea395e8c
AJ
113 }
114
dd1c8f3e 115 dec->bit_rate = dec->sample_rate * dec->bits_per_coded_sample;
ea395e8c
AJ
116
117 if (max_size <= 0)
fd9a71ac 118 max_size = 2048;
ea395e8c
AJ
119 size = FFMIN(voc->remaining_size, max_size);
120 voc->remaining_size -= size;
121 return av_get_packet(pb, pkt, size);
122}
123
2d0432d9
AK
124#if CONFIG_VOC_DEMUXER
125static int voc_probe(AVProbeData *p)
126{
127 int version, check;
128
129 if (memcmp(p->buf, ff_voc_magic, sizeof(ff_voc_magic) - 1))
130 return 0;
131 version = AV_RL16(p->buf + 22);
132 check = AV_RL16(p->buf + 24);
133 if (~version + 0x1234 != check)
134 return 10;
135
136 return AVPROBE_SCORE_MAX;
137}
138
139static int voc_read_header(AVFormatContext *s)
140{
141 VocDecContext *voc = s->priv_data;
142 AVIOContext *pb = s->pb;
143 int header_size;
2d0432d9
AK
144
145 avio_skip(pb, 20);
146 header_size = avio_rl16(pb) - 22;
147 if (header_size != 4) {
148 av_log(s, AV_LOG_ERROR, "unknown header size: %d\n", header_size);
149 return AVERROR(ENOSYS);
150 }
151 avio_skip(pb, header_size);
9f0b6e68
AK
152
153 s->ctx_flags |= AVFMTCTX_NOHEADER;
2d0432d9
AK
154
155 voc->remaining_size = 0;
156 return 0;
157}
158
ea395e8c
AJ
159static int voc_read_packet(AVFormatContext *s, AVPacket *pkt)
160{
9f0b6e68
AK
161 if (!s->nb_streams) {
162 AVStream *st = avformat_new_stream(s, NULL);
163 if (!st)
164 return AVERROR(ENOMEM);
165 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
166 }
167f3b8d 167 return ff_voc_get_packet(s, pkt, s->streams[0], 0);
ea395e8c
AJ
168}
169
c6610a21 170AVInputFormat ff_voc_demuxer = {
dfc2c4d9 171 .name = "voc",
6774247a 172 .long_name = NULL_IF_CONFIG_SMALL("Creative Voice"),
dfc2c4d9
AK
173 .priv_data_size = sizeof(VocDecContext),
174 .read_probe = voc_probe,
175 .read_header = voc_read_header,
176 .read_packet = voc_read_packet,
20234a4b 177 .codec_tag = (const AVCodecTag* const []){ ff_voc_codec_tags, 0 },
ea395e8c 178};
2d0432d9 179#endif /* CONFIG_VOC_DEMUXER */