1 /*
2 * Beam Software SIFF demuxer
3 * Copyright (c) 2007 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * 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 * 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 * 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/channel_layout.h"
23 #include "libavutil/intreadwrite.h"
24
25 #include "avformat.h"
26 #include "internal.h"
27 #include "avio_internal.h"
28
29 enum SIFFTags {
30 TAG_SIFF = MKTAG('S', 'I', 'F', 'F'),
31 TAG_BODY = MKTAG('B', 'O', 'D', 'Y'),
32 TAG_VBHD = MKTAG('V', 'B', 'H', 'D'),
33 TAG_SHDR = MKTAG('S', 'H', 'D', 'R'),
34 TAG_VBV1 = MKTAG('V', 'B', 'V', '1'),
35 TAG_SOUN = MKTAG('S', 'O', 'U', 'N'),
36 };
37
38 enum VBFlags {
39 VB_HAS_GMC = 0x01,
40 VB_HAS_AUDIO = 0x04,
41 VB_HAS_VIDEO = 0x08,
42 VB_HAS_PALETTE = 0x10,
43 VB_HAS_LENGTH = 0x20
44 };
45
46 typedef struct SIFFContext {
47 int frames;
48 int cur_frame;
49 int rate;
50 int bits;
51 int block_align;
52
53 int has_video;
54 int has_audio;
55
56 int curstrm;
57 unsigned int pktsize;
58 int gmcsize;
59 unsigned int sndsize;
60
61 unsigned int flags;
62 uint8_t gmc[4];
63 } SIFFContext;
64
siff_probe(const AVProbeData *p)65 static int siff_probe(const AVProbeData *p)
66 {
67 uint32_t tag = AV_RL32(p->buf + 8);
68 /* check file header */
69 if (AV_RL32(p->buf) != TAG_SIFF ||
70 (tag != TAG_VBV1 && tag != TAG_SOUN))
71 return 0;
72 return AVPROBE_SCORE_MAX;
73 }
74
create_audio_stream(AVFormatContext *s, SIFFContext *c)75 static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
76 {
77 AVStream *ast;
78 ast = avformat_new_stream(s, NULL);
79 if (!ast)
80 return AVERROR(ENOMEM);
81 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
82 ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
83 ast->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
84 ast->codecpar->bits_per_coded_sample = 8;
85 ast->codecpar->sample_rate = c->rate;
86 avpriv_set_pts_info(ast, 16, 1, c->rate);
87 ast->start_time = 0;
88 return 0;
89 }
90
siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)91 static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
92 {
93 AVStream *st;
94 int width, height;
95
96 if (avio_rl32(pb) != TAG_VBHD) {
97 av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
98 return AVERROR_INVALIDDATA;
99 }
100 if (avio_rb32(pb) != 32) {
101 av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
102 return AVERROR_INVALIDDATA;
103 }
104 if (avio_rl16(pb) != 1) {
105 av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
106 return AVERROR_INVALIDDATA;
107 }
108 width = avio_rl16(pb);
109 height = avio_rl16(pb);
110 avio_skip(pb, 4);
111 c->frames = avio_rl16(pb);
112 if (!c->frames) {
113 av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
114 return AVERROR_INVALIDDATA;
115 }
116 c->bits = avio_rl16(pb);
117 c->rate = avio_rl16(pb);
118 c->block_align = c->rate * (c->bits >> 3);
119
120 avio_skip(pb, 16); // zeroes
121
122 st = avformat_new_stream(s, NULL);
123 if (!st)
124 return AVERROR(ENOMEM);
125 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
126 st->codecpar->codec_id = AV_CODEC_ID_VB;
127 st->codecpar->codec_tag = MKTAG('V', 'B', 'V', '1');
128 st->codecpar->width = width;
129 st->codecpar->height = height;
130 st->codecpar->format = AV_PIX_FMT_PAL8;
131 st->nb_frames =
132 st->duration = c->frames;
133 avpriv_set_pts_info(st, 16, 1, 12);
134
135 c->cur_frame = 0;
136 c->has_video = 1;
137 c->has_audio = !!c->rate;
138 c->curstrm = -1;
139 if (c->has_audio)
140 return create_audio_stream(s, c);
141 return 0;
142 }
143
siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)144 static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
145 {
146 if (avio_rl32(pb) != TAG_SHDR) {
147 av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
148 return AVERROR_INVALIDDATA;
149 }
150 if (avio_rb32(pb) != 8) {
151 av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
152 return AVERROR_INVALIDDATA;
153 }
154 avio_skip(pb, 4); // unknown value
155 c->rate = avio_rl16(pb);
156 c->bits = avio_rl16(pb);
157 c->block_align = c->rate * (c->bits >> 3);
158 return create_audio_stream(s, c);
159 }
160
siff_read_header(AVFormatContext *s)161 static int siff_read_header(AVFormatContext *s)
162 {
163 AVIOContext *pb = s->pb;
164 SIFFContext *c = s->priv_data;
165 uint32_t tag;
166 int ret;
167
168 if (avio_rl32(pb) != TAG_SIFF)
169 return AVERROR_INVALIDDATA;
170 avio_skip(pb, 4); // ignore size
171 tag = avio_rl32(pb);
172
173 if (tag != TAG_VBV1 && tag != TAG_SOUN) {
174 av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
175 return AVERROR_INVALIDDATA;
176 }
177
178 if (tag == TAG_VBV1 && (ret = siff_parse_vbv1(s, c, pb)) < 0)
179 return ret;
180 if (tag == TAG_SOUN && (ret = siff_parse_soun(s, c, pb)) < 0)
181 return ret;
182 if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')) {
183 av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
184 return AVERROR_INVALIDDATA;
185 }
186 avio_skip(pb, 4); // ignore size
187
188 return 0;
189 }
190
siff_read_packet(AVFormatContext *s, AVPacket *pkt)191 static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
192 {
193 SIFFContext *c = s->priv_data;
194 int ret;
195
196 if (c->has_video) {
197 unsigned int size;
198 if (c->cur_frame >= c->frames)
199 return AVERROR_EOF;
200 if (c->curstrm == -1) {
201 c->pktsize = avio_rl32(s->pb) - 4;
202 c->flags = avio_rl16(s->pb);
203 if (c->flags & VB_HAS_AUDIO && !c->has_audio)
204 return AVERROR_INVALIDDATA;
205 c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
206 if (c->gmcsize)
207 avio_read(s->pb, c->gmc, c->gmcsize);
208 c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb) : 0;
209 c->curstrm = !!(c->flags & VB_HAS_AUDIO);
210 }
211
212 if (!c->curstrm) {
213 if (c->pktsize < 2LL + c->sndsize + c->gmcsize)
214 return AVERROR_INVALIDDATA;
215
216 size = c->pktsize - c->sndsize - c->gmcsize - 2;
217 size = ffio_limit(s->pb, size);
218 if ((ret = av_new_packet(pkt, size + c->gmcsize + 2)) < 0)
219 return ret;
220 AV_WL16(pkt->data, c->flags);
221 if (c->gmcsize)
222 memcpy(pkt->data + 2, c->gmc, c->gmcsize);
223 if (avio_read(s->pb, pkt->data + 2 + c->gmcsize, size) != size) {
224 return AVERROR_INVALIDDATA;
225 }
226 pkt->stream_index = 0;
227 c->curstrm = -1;
228 } else {
229 int pktsize = av_get_packet(s->pb, pkt, c->sndsize - 4);
230 if (pktsize < 0)
231 return AVERROR(EIO);
232 pkt->stream_index = 1;
233 pkt->duration = pktsize;
234 c->curstrm = 0;
235 }
236 if (!c->cur_frame || c->curstrm)
237 pkt->flags |= AV_PKT_FLAG_KEY;
238 if (c->curstrm == -1)
239 c->cur_frame++;
240 } else {
241 int pktsize = av_get_packet(s->pb, pkt, c->block_align);
242 if (!pktsize)
243 return AVERROR_EOF;
244 if (pktsize <= 0)
245 return AVERROR(EIO);
246 pkt->duration = pktsize;
247 }
248 return pkt->size;
249 }
250
251 const AVInputFormat ff_siff_demuxer = {
252 .name = "siff",
253 .long_name = NULL_IF_CONFIG_SMALL("Beam Software SIFF"),
254 .priv_data_size = sizeof(SIFFContext),
255 .read_probe = siff_probe,
256 .read_header = siff_read_header,
257 .read_packet = siff_read_packet,
258 .extensions = "vb,son",
259 };
260