2 * Beam Software SIFF demuxer
3 * Copyright (c) 2007 Konstantin Shishkov
5 * This file is part of Libav.
7 * Libav 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.
12 * Libav 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/intreadwrite.h"
28 TAG_SIFF = MKTAG('S', 'I', 'F', 'F'),
29 TAG_BODY = MKTAG('B', 'O', 'D', 'Y'),
30 TAG_VBHD = MKTAG('V', 'B', 'H', 'D'),
31 TAG_SHDR = MKTAG('S', 'H', 'D', 'R'),
32 TAG_VBV1 = MKTAG('V', 'B', 'V', '1'),
33 TAG_SOUN = MKTAG('S', 'O', 'U', 'N'),
40 VB_HAS_PALETTE = 0x10,
44 typedef struct SIFFContext{
63 static int siff_probe(AVProbeData *p)
65 uint32_t tag = AV_RL32(p->buf + 8);
66 /* check file header */
67 if (AV_RL32(p->buf) != TAG_SIFF ||
68 (tag != TAG_VBV1 && tag != TAG_SOUN))
70 return AVPROBE_SCORE_MAX;
73 static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
76 ast = avformat_new_stream(s, NULL);
79 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
80 ast->codec->codec_id = AV_CODEC_ID_PCM_U8;
81 ast->codec->channels = 1;
82 ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
83 ast->codec->bits_per_coded_sample = 8;
84 ast->codec->sample_rate = c->rate;
85 avpriv_set_pts_info(ast, 16, 1, c->rate);
90 static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
95 if (avio_rl32(pb) != TAG_VBHD){
96 av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
99 if(avio_rb32(pb) != 32){
100 av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
103 if(avio_rl16(pb) != 1){
104 av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
107 width = avio_rl16(pb);
108 height = avio_rl16(pb);
110 c->frames = avio_rl16(pb);
112 av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
115 c->bits = avio_rl16(pb);
116 c->rate = avio_rl16(pb);
117 c->block_align = c->rate * (c->bits >> 3);
119 avio_skip(pb, 16); //zeroes
121 st = avformat_new_stream(s, NULL);
124 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
125 st->codec->codec_id = AV_CODEC_ID_VB;
126 st->codec->codec_tag = MKTAG('V', 'B', 'V', '1');
127 st->codec->width = width;
128 st->codec->height = height;
129 st->codec->pix_fmt = AV_PIX_FMT_PAL8;
130 avpriv_set_pts_info(st, 16, 1, 12);
134 c->has_audio = !!c->rate;
136 if (c->has_audio && create_audio_stream(s, c) < 0)
141 static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
143 if (avio_rl32(pb) != TAG_SHDR){
144 av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
147 if(avio_rb32(pb) != 8){
148 av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
151 avio_skip(pb, 4); //unknown value
152 c->rate = avio_rl16(pb);
153 c->bits = avio_rl16(pb);
154 c->block_align = c->rate * (c->bits >> 3);
155 return create_audio_stream(s, c);
158 static int siff_read_header(AVFormatContext *s)
160 AVIOContext *pb = s->pb;
161 SIFFContext *c = s->priv_data;
164 if (avio_rl32(pb) != TAG_SIFF)
166 avio_skip(pb, 4); //ignore size
169 if (tag != TAG_VBV1 && tag != TAG_SOUN){
170 av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
174 if (tag == TAG_VBV1 && siff_parse_vbv1(s, c, pb) < 0)
176 if (tag == TAG_SOUN && siff_parse_soun(s, c, pb) < 0)
178 if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')){
179 av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
182 avio_skip(pb, 4); //ignore size
187 static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
189 SIFFContext *c = s->priv_data;
193 if (c->cur_frame >= c->frames)
195 if (c->curstrm == -1){
196 c->pktsize = avio_rl32(s->pb) - 4;
197 c->flags = avio_rl16(s->pb);
198 c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
200 avio_read(s->pb, c->gmc, c->gmcsize);
201 c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb): 0;
202 c->curstrm = !!(c->flags & VB_HAS_AUDIO);
206 size = c->pktsize - c->sndsize;
207 if (av_new_packet(pkt, size) < 0)
208 return AVERROR(ENOMEM);
209 AV_WL16(pkt->data, c->flags);
211 memcpy(pkt->data + 2, c->gmc, c->gmcsize);
212 avio_read(s->pb, pkt->data + 2 + c->gmcsize, size - c->gmcsize - 2);
213 pkt->stream_index = 0;
216 if ((size = av_get_packet(s->pb, pkt, c->sndsize - 4)) < 0)
218 pkt->stream_index = 1;
219 pkt->duration = size;
222 if(!c->cur_frame || c->curstrm)
223 pkt->flags |= AV_PKT_FLAG_KEY;
224 if (c->curstrm == -1)
227 size = av_get_packet(s->pb, pkt, c->block_align);
230 pkt->duration = size;
235 AVInputFormat ff_siff_demuxer = {
237 .long_name = NULL_IF_CONFIG_SMALL("Beam Software SIFF"),
238 .priv_data_size = sizeof(SIFFContext),
239 .read_probe = siff_probe,
240 .read_header = siff_read_header,
241 .read_packet = siff_read_packet,
242 .extensions = "vb,son",