Browse code

Beam Software SIFF demuxer and video decoder

Originally committed as revision 10833 to svn://svn.ffmpeg.org/ffmpeg/trunk

Kostya Shishkov authored on 2007/10/22 15:01:38
Showing 11 changed files
... ...
@@ -100,6 +100,7 @@ version <next>
100 100
 - DNxHD encoder
101 101
 - H.264 PAFF decoding
102 102
 - Nellymoser ASAO decoder
103
+- Beam Software SIFF demuxer and decoder
103 104
 
104 105
 version 0.4.9-pre1:
105 106
 
... ...
@@ -177,6 +177,7 @@ Codecs:
177 177
   tscc.c                                Kostya Shishkov
178 178
   txd.c                                 Ivo van Poorten
179 179
   ulti*                                 Kostya Shishkov
180
+  vb.c                                  Kostya Shishkov
180 181
   vc1*                                  Kostya Shishkov
181 182
   vcr1.c                                Michael Niedermayer
182 183
   vmnc.c                                Kostya Shishkov
... ...
@@ -245,6 +246,7 @@ Muxers/Demuxers:
245 245
   rtsp.c                                Luca Barbato
246 246
   sdp.c                                 Luca Abeni
247 247
   segafilm.c                            Mike Melanson
248
+  siff.c                                Kostya Shishkov
248 249
   swf.c                                 Baptiste Coudurier
249 250
   txd.c                                 Ivo van Poorten
250 251
   v4l2.c                                Luca Abeni
... ...
@@ -117,6 +117,8 @@ different game cutscenes repacked for use with ScummVM.
117 117
 @item CRYO APC @tab    @tab X
118 118
 @tab Audio format used in some games by CRYO Interactive Entertainment.
119 119
 @item Monkey's Audio @tab    @tab X
120
+@item SIFF @tab    @tab X
121
+@tab Audio and video format used in some games by Beam Software
120 122
 @end multitable
121 123
 
122 124
 @code{X} means that encoding (resp. decoding) is supported.
... ...
@@ -186,6 +186,7 @@ OBJS-$(CONFIG_TSCC_DECODER)            += tscc.o
186 186
 OBJS-$(CONFIG_TTA_DECODER)             += tta.o
187 187
 OBJS-$(CONFIG_TXD_DECODER)             += txd.o s3tc.o
188 188
 OBJS-$(CONFIG_ULTI_DECODER)            += ulti.o
189
+OBJS-$(CONFIG_VB_DECODER)              += vb.o
189 190
 OBJS-$(CONFIG_VC1_DECODER)             += vc1.o vc1data.o vc1dsp.o msmpeg4data.o
190 191
 OBJS-$(CONFIG_VCR1_DECODER)            += vcr1.o
191 192
 OBJS-$(CONFIG_VCR1_ENCODER)            += vcr1.o
... ...
@@ -150,6 +150,7 @@ void avcodec_register_all(void)
150 150
     REGISTER_DECODER (TSCC, tscc);
151 151
     REGISTER_DECODER (TXD, txd);
152 152
     REGISTER_DECODER (ULTI, ulti);
153
+    REGISTER_DECODER (VB, vb);
153 154
     REGISTER_DECODER (VC1, vc1);
154 155
     REGISTER_DECODER (VCR1, vcr1);
155 156
     REGISTER_DECODER (VMDVIDEO, vmdvideo);
... ...
@@ -33,8 +33,8 @@
33 33
 #define AV_STRINGIFY(s)         AV_TOSTRING(s)
34 34
 #define AV_TOSTRING(s) #s
35 35
 
36
-#define LIBAVCODEC_VERSION_INT  ((51<<16)+(46<<8)+1)
37
-#define LIBAVCODEC_VERSION      51.46.1
36
+#define LIBAVCODEC_VERSION_INT  ((51<<16)+(47<<8)+0)
37
+#define LIBAVCODEC_VERSION      51.47.0
38 38
 #define LIBAVCODEC_BUILD        LIBAVCODEC_VERSION_INT
39 39
 
40 40
 #define LIBAVCODEC_IDENT        "Lavc" AV_STRINGIFY(LIBAVCODEC_VERSION)
... ...
@@ -168,6 +168,7 @@ enum CodecID {
168 168
     CODEC_ID_TXD,
169 169
     CODEC_ID_VP6A,
170 170
     CODEC_ID_AMV,
171
+    CODEC_ID_VB,
171 172
 
172 173
     /* various PCM "codecs" */
173 174
     CODEC_ID_PCM_S16LE= 0x10000,
174 175
new file mode 100644
... ...
@@ -0,0 +1,282 @@
0
+/*
1
+ * Beam Software VB decoder
2
+ * Copyright (c) 2007 Konstantin Shishkov
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file vb.c
23
+ * VB Video decoder
24
+ */
25
+
26
+#include <stdio.h>
27
+#include <stdlib.h>
28
+
29
+#include "avcodec.h"
30
+#include "bytestream.h"
31
+
32
+enum VBFlags{
33
+    VB_HAS_GMC     = 0x01,
34
+    VB_HAS_AUDIO   = 0x04,
35
+    VB_HAS_VIDEO   = 0x08,
36
+    VB_HAS_PALETTE = 0x10,
37
+    VB_HAS_LENGTH  = 0x20
38
+};
39
+
40
+typedef struct VBDecContext {
41
+    AVCodecContext *avctx;
42
+    AVFrame pic;
43
+
44
+    uint8_t *frame, *prev_frame;
45
+    uint32_t pal[256];
46
+    uint8_t *stream;
47
+} VBDecContext;
48
+
49
+static const uint16_t vb_patterns[64] = {
50
+    0x0660, 0xFF00, 0xCCCC, 0xF000, 0x8888, 0x000F, 0x1111, 0xFEC8,
51
+    0x8CEF, 0x137F, 0xF731, 0xC800, 0x008C, 0x0013, 0x3100, 0xCC00,
52
+    0x00CC, 0x0033, 0x3300, 0x0FF0, 0x6666, 0x00F0, 0x0F00, 0x2222,
53
+    0x4444, 0xF600, 0x8CC8, 0x006F, 0x1331, 0x318C, 0xC813, 0x33CC,
54
+    0x6600, 0x0CC0, 0x0066, 0x0330, 0xF900, 0xC88C, 0x009F, 0x3113,
55
+    0x6000, 0x0880, 0x0006, 0x0110, 0xCC88, 0xFC00, 0x00CF, 0x88CC,
56
+    0x003F, 0x1133, 0x3311, 0xF300, 0x6FF6, 0x0603, 0x08C6, 0x8C63,
57
+    0xC631, 0x6310, 0xC060, 0x0136, 0x136C, 0x36C8, 0x6C80, 0x324C
58
+};
59
+
60
+static void vb_decode_palette(VBDecContext *c)
61
+{
62
+    int start, size, i;
63
+
64
+    start = bytestream_get_byte(&c->stream);
65
+    size = (bytestream_get_byte(&c->stream) - 1) & 0xFF;
66
+    if(start + size > 255){
67
+        av_log(c->avctx, AV_LOG_ERROR, "Palette change runs beyond entry 256\n");
68
+        return;
69
+    }
70
+    for(i = start; i <= start + size; i++)
71
+        c->pal[i] = bytestream_get_be24(&c->stream);
72
+}
73
+
74
+static inline int check_pixel(uint8_t *buf, uint8_t *start, uint8_t *end)
75
+{
76
+    return buf >= start && buf < end;
77
+}
78
+
79
+static inline int check_line(uint8_t *buf, uint8_t *start, uint8_t *end)
80
+{
81
+    return buf >= start && (buf + 4) <= end;
82
+}
83
+
84
+static int vb_decode_framedata(VBDecContext *c, uint8_t *buf, int offset)
85
+{
86
+    uint8_t *prev, *cur;
87
+    int blk, blocks, t, blk2;
88
+    int blocktypes = 0;
89
+    int x, y, a, b;
90
+    int pattype, pattern;
91
+    const int width = c->avctx->width;
92
+    uint8_t *pstart = c->prev_frame;
93
+    uint8_t *pend = c->prev_frame + width*c->avctx->height;
94
+
95
+    prev = c->prev_frame + offset;
96
+    cur = c->frame;
97
+
98
+    blocks = (c->avctx->width >> 2) * (c->avctx->height >> 2);
99
+    blk2 = 0;
100
+    for(blk = 0; blk < blocks; blk++){
101
+        if(!(blk & 3))
102
+            blocktypes = bytestream_get_byte(&buf);
103
+        switch(blocktypes & 0xC0){
104
+        case 0x00: //skip
105
+            for(y = 0; y < 4; y++)
106
+                if(check_line(prev + y*width, pstart, pend))
107
+                    memcpy(cur + y*width, prev + y*width, 4);
108
+                else
109
+                    memset(cur + y*width, 0, 4);
110
+            break;
111
+        case 0x40:
112
+            t = bytestream_get_byte(&buf);
113
+            if(!t){ //raw block
114
+                for(y = 0; y < 4; y++)
115
+                    memcpy(cur + y*width, buf + y*4, 4);
116
+                buf += 16;
117
+            }else{ // motion compensation
118
+                x = ((t & 0xF)^8) - 8;
119
+                y = ((t >> 4) ^8) - 8;
120
+                t = x + y*width;
121
+                for(y = 0; y < 4; y++)
122
+                    if(check_line(prev + t + y*width, pstart, pend))
123
+                        memcpy(cur + y*width, prev + t + y*width, 4);
124
+                    else
125
+                        memset(cur + y*width, 0, 4);
126
+            }
127
+            break;
128
+        case 0x80: // fill
129
+            t = bytestream_get_byte(&buf);
130
+            for(y = 0; y < 4; y++)
131
+                memset(cur + y*width, t, 4);
132
+            break;
133
+        case 0xC0: // pattern fill
134
+            t = bytestream_get_byte(&buf);
135
+            pattype = t >> 6;
136
+            pattern = vb_patterns[t & 0x3F];
137
+            switch(pattype){
138
+            case 0:
139
+                a = bytestream_get_byte(&buf);
140
+                b = bytestream_get_byte(&buf);
141
+                for(y = 0; y < 4; y++)
142
+                    for(x = 0; x < 4; x++, pattern >>= 1)
143
+                        cur[x + y*width] = (pattern & 1) ? b : a;
144
+                break;
145
+            case 1:
146
+                pattern = ~pattern;
147
+            case 2:
148
+                a = bytestream_get_byte(&buf);
149
+                for(y = 0; y < 4; y++)
150
+                    for(x = 0; x < 4; x++, pattern >>= 1)
151
+                        if(pattern & 1 && check_pixel(prev + x + y*width, pstart, pend))
152
+                            cur[x + y*width] = prev[x + y*width];
153
+                        else
154
+                            cur[x + y*width] = a;
155
+                break;
156
+            case 3:
157
+                av_log(c->avctx, AV_LOG_ERROR, "Invalid opcode seen @%d\n",blk);
158
+                return -1;
159
+            }
160
+            break;
161
+        }
162
+        blocktypes <<= 2;
163
+        cur  += 4;
164
+        prev += 4;
165
+        blk2++;
166
+        if(blk2 == (width >> 2)){
167
+            blk2 = 0;
168
+            cur  += width * 3;
169
+            prev += width * 3;
170
+        }
171
+    }
172
+    return 0;
173
+}
174
+
175
+static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
176
+{
177
+    VBDecContext * const c = avctx->priv_data;
178
+    uint8_t *outptr, *srcptr;
179
+    int i, j;
180
+    int flags;
181
+    uint32_t size;
182
+    int rest = buf_size;
183
+    int offset = 0;
184
+
185
+    c->stream = buf;
186
+    flags = bytestream_get_le16(&c->stream);
187
+    rest -= 2;
188
+
189
+    if(flags & VB_HAS_GMC){
190
+        i = (int16_t)bytestream_get_le16(&c->stream);
191
+        j = (int16_t)bytestream_get_le16(&c->stream);
192
+        offset = i + j * avctx->width;
193
+        rest -= 4;
194
+    }
195
+    if(flags & VB_HAS_VIDEO){
196
+        size = bytestream_get_le32(&c->stream);
197
+        if(size > rest){
198
+            av_log(avctx, AV_LOG_ERROR, "Frame size is too big\n");
199
+            return -1;
200
+        }
201
+        vb_decode_framedata(c, c->stream, offset);
202
+        c->stream += size - 4;
203
+        rest -= size;
204
+    }
205
+    if(flags & VB_HAS_PALETTE){
206
+        size = bytestream_get_le32(&c->stream);
207
+        if(size > rest){
208
+            av_log(avctx, AV_LOG_ERROR, "Palette size is too big\n");
209
+            return -1;
210
+        }
211
+        vb_decode_palette(c);
212
+        rest -= size;
213
+    }
214
+
215
+    memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
216
+    c->pic.palette_has_changed = flags & VB_HAS_PALETTE;
217
+
218
+    outptr = c->pic.data[0];
219
+    srcptr = c->frame;
220
+
221
+    for(i = 0; i < avctx->height; i++){
222
+        memcpy(outptr, srcptr, avctx->width);
223
+        srcptr += avctx->width;
224
+        outptr += c->pic.linesize[0];
225
+    }
226
+
227
+    FFSWAP(uint8_t*, c->frame, c->prev_frame);
228
+
229
+    *data_size = sizeof(AVFrame);
230
+    *(AVFrame*)data = c->pic;
231
+
232
+    /* always report that the buffer was completely consumed */
233
+    return buf_size;
234
+}
235
+
236
+static int decode_init(AVCodecContext *avctx)
237
+{
238
+    VBDecContext * const c = avctx->priv_data;
239
+
240
+    c->avctx = avctx;
241
+    avctx->pix_fmt = PIX_FMT_PAL8;
242
+
243
+    if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
244
+        return -1;
245
+    }
246
+
247
+    c->pic.reference = 1;
248
+    if(avctx->get_buffer(avctx, &c->pic) < 0){
249
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
250
+        return -1;
251
+    }
252
+
253
+    c->frame      = av_malloc( avctx->width * avctx->height);
254
+    c->prev_frame = av_malloc( avctx->width * avctx->height);
255
+
256
+    return 0;
257
+}
258
+
259
+static int decode_end(AVCodecContext *avctx)
260
+{
261
+    VBDecContext *c = avctx->priv_data;
262
+
263
+    av_freep(&c->frame);
264
+    av_freep(&c->prev_frame);
265
+    if(c->pic.data[0])
266
+        avctx->release_buffer(avctx, &c->pic);
267
+
268
+    return 0;
269
+}
270
+
271
+AVCodec vb_decoder = {
272
+    "vb",
273
+    CODEC_TYPE_VIDEO,
274
+    CODEC_ID_VB,
275
+    sizeof(VBDecContext),
276
+    decode_init,
277
+    NULL,
278
+    decode_end,
279
+    decode_frame
280
+};
281
+
... ...
@@ -127,6 +127,7 @@ OBJS-$(CONFIG_RTSP_DEMUXER)              += rtsp.o
127 127
 OBJS-$(CONFIG_SDP_DEMUXER)               += rtsp.o
128 128
 OBJS-$(CONFIG_SEGAFILM_DEMUXER)          += segafilm.o
129 129
 OBJS-$(CONFIG_SHORTEN_DEMUXER)           += raw.o
130
+OBJS-$(CONFIG_SIFF_DEMUXER)              += siff.o
130 131
 OBJS-$(CONFIG_SMACKER_DEMUXER)           += smacker.o
131 132
 OBJS-$(CONFIG_SOL_DEMUXER)               += sol.o raw.o
132 133
 OBJS-$(CONFIG_STR_DEMUXER)               += psxstr.o
... ...
@@ -147,6 +147,7 @@ void av_register_all(void)
147 147
 #endif
148 148
     REGISTER_DEMUXER  (SEGAFILM, segafilm);
149 149
     REGISTER_DEMUXER  (SHORTEN, shorten);
150
+    REGISTER_DEMUXER  (SIFF, siff);
150 151
     REGISTER_DEMUXER  (SMACKER, smacker);
151 152
     REGISTER_DEMUXER  (SOL, sol);
152 153
     REGISTER_DEMUXER  (STR, str);
... ...
@@ -21,8 +21,8 @@
21 21
 #ifndef FFMPEG_AVFORMAT_H
22 22
 #define FFMPEG_AVFORMAT_H
23 23
 
24
-#define LIBAVFORMAT_VERSION_INT ((51<<16)+(16<<8)+1)
25
-#define LIBAVFORMAT_VERSION     51.16.1
24
+#define LIBAVFORMAT_VERSION_INT ((51<<16)+(17<<8)+0)
25
+#define LIBAVFORMAT_VERSION     51.17.0
26 26
 #define LIBAVFORMAT_BUILD       LIBAVFORMAT_VERSION_INT
27 27
 
28 28
 #define LIBAVFORMAT_IDENT       "Lavf" AV_STRINGIFY(LIBAVFORMAT_VERSION)
29 29
new file mode 100644
... ...
@@ -0,0 +1,238 @@
0
+/*
1
+ * Beam Software SIFF demuxer
2
+ * Copyright (c) 2007 Konstantin Shishkov.
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "avformat.h"
22
+#include "riff.h"
23
+
24
+enum SIFFTags{
25
+    TAG_SIFF = MKTAG('S', 'I', 'F', 'F'),
26
+    TAG_BODY = MKTAG('B', 'O', 'D', 'Y'),
27
+    TAG_VBHD = MKTAG('V', 'B', 'H', 'D'),
28
+    TAG_SHDR = MKTAG('S', 'H', 'D', 'R'),
29
+    TAG_VBV1 = MKTAG('V', 'B', 'V', '1'),
30
+    TAG_SOUN = MKTAG('S', 'O', 'U', 'N'),
31
+};
32
+
33
+enum VBFlags{
34
+    VB_HAS_GMC     = 0x01,
35
+    VB_HAS_AUDIO   = 0x04,
36
+    VB_HAS_VIDEO   = 0x08,
37
+    VB_HAS_PALETTE = 0x10,
38
+    VB_HAS_LENGTH  = 0x20
39
+};
40
+
41
+typedef struct SIFFContext{
42
+    int frames;
43
+    int cur_frame;
44
+    int rate;
45
+    int bits;
46
+    int block_align;
47
+
48
+    int has_video;
49
+    int has_audio;
50
+
51
+    int curstrm;
52
+    int pktsize;
53
+    int gmcsize;
54
+    int sndsize;
55
+
56
+    int flags;
57
+    uint8_t gmc[4];
58
+}SIFFContext;
59
+
60
+static int siff_probe(AVProbeData *p)
61
+{
62
+    /* check file header */
63
+    if (AV_RL32(p->buf) == TAG_SIFF)
64
+        return AVPROBE_SCORE_MAX;
65
+    else
66
+        return 0;
67
+}
68
+
69
+static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
70
+{
71
+    AVStream *ast;
72
+    ast = av_new_stream(s, 0);
73
+    if (!ast)
74
+        return -1;
75
+    ast->codec->codec_type      = CODEC_TYPE_AUDIO;
76
+    ast->codec->codec_id        = CODEC_ID_PCM_U8;
77
+    ast->codec->channels        = 1;
78
+    ast->codec->bits_per_sample = c->bits;
79
+    ast->codec->sample_rate     = c->rate;
80
+    ast->codec->frame_size      = c->block_align;
81
+    av_set_pts_info(ast, 16, 1, c->rate);
82
+    return 0;
83
+}
84
+
85
+static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, ByteIOContext *pb)
86
+{
87
+    AVStream *st;
88
+    int width, height;
89
+
90
+    if (get_le32(pb) != TAG_VBHD){
91
+        av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
92
+        return -1;
93
+    }
94
+    if(get_be32(pb) != 32){
95
+        av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
96
+        return -1;
97
+    }
98
+    if(get_le16(pb) != 1){
99
+        av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
100
+        return -1;
101
+    }
102
+    width = get_le16(pb);
103
+    height = get_le16(pb);
104
+    url_fskip(pb, 4);
105
+    c->frames = get_le16(pb);
106
+    if(!c->frames){
107
+        av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
108
+        return -1;
109
+    }
110
+    c->bits = get_le16(pb);
111
+    c->rate = get_le16(pb);
112
+    c->block_align = c->rate * (c->bits >> 3);
113
+
114
+    url_fskip(pb, 16); //zeroes
115
+
116
+    st = av_new_stream(s, 0);
117
+    if (!st)
118
+        return -1;
119
+    st->codec->codec_type = CODEC_TYPE_VIDEO;
120
+    st->codec->codec_id   = CODEC_ID_VB;
121
+    st->codec->codec_tag  = MKTAG('V', 'B', 'V', '1');
122
+    st->codec->width      = width;
123
+    st->codec->height     = height;
124
+    st->codec->pix_fmt    = PIX_FMT_PAL8;
125
+    av_set_pts_info(st, 16, 1, 12);
126
+
127
+    c->cur_frame = 0;
128
+    c->has_video = 1;
129
+    c->has_audio = !!c->rate;
130
+    c->curstrm = -1;
131
+    if (c->has_audio && create_audio_stream(s, c) < 0)
132
+        return -1;
133
+    return 0;
134
+}
135
+
136
+static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, ByteIOContext *pb)
137
+{
138
+    if (get_le32(pb) != TAG_SHDR){
139
+        av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
140
+        return -1;
141
+    }
142
+    if(get_be32(pb) != 8){
143
+        av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
144
+        return -1;
145
+    }
146
+    url_fskip(pb, 4); //unknown value
147
+    c->rate = get_le16(pb);
148
+    c->bits = get_le16(pb);
149
+    c->block_align = c->rate * (c->bits >> 3);
150
+    return create_audio_stream(s, c);
151
+}
152
+
153
+static int siff_read_header(AVFormatContext *s, AVFormatParameters *ap)
154
+{
155
+    ByteIOContext *pb = &s->pb;
156
+    SIFFContext *c = s->priv_data;
157
+    uint32_t tag;
158
+
159
+    if (get_le32(pb) != TAG_SIFF)
160
+        return -1;
161
+    url_fskip(pb, 4); //ignore size
162
+    tag = get_le32(pb);
163
+
164
+    if (tag != TAG_VBV1 && tag != TAG_SOUN){
165
+        av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
166
+        return -1;
167
+    }
168
+
169
+    if (tag == TAG_VBV1 && siff_parse_vbv1(s, c, pb) < 0)
170
+        return -1;
171
+    if (tag == TAG_SOUN && siff_parse_soun(s, c, pb) < 0)
172
+        return -1;
173
+    if (get_le32(pb) != MKTAG('B', 'O', 'D', 'Y')){
174
+        av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
175
+        return -1;
176
+    }
177
+    url_fskip(pb, 4); //ignore size
178
+
179
+    return 0;
180
+}
181
+
182
+static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
183
+{
184
+    SIFFContext *c = s->priv_data;
185
+    int size, size2;
186
+    uint8_t *snddata;
187
+
188
+    if (c->has_video){
189
+        if (c->cur_frame >= c->frames)
190
+            return AVERROR(EIO);
191
+        if (c->curstrm == -1){
192
+            c->pktsize = get_le32(&s->pb) - 4;
193
+            c->flags = get_le16(&s->pb);
194
+            c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
195
+            if (c->gmcsize)
196
+                get_buffer(&s->pb, c->gmc, c->gmcsize);
197
+            c->sndsize = (c->flags & VB_HAS_AUDIO) ? get_le32(&s->pb): 0;
198
+            c->curstrm = !!(c->flags & VB_HAS_AUDIO);
199
+        }
200
+
201
+        if (!c->curstrm){
202
+            size = c->pktsize - c->sndsize;
203
+            if (av_new_packet(pkt, size) < 0)
204
+                return AVERROR(ENOMEM);
205
+            AV_WL16(pkt->data, c->flags);
206
+            if (c->gmcsize)
207
+                memcpy(pkt->data + 2, c->gmc, c->gmcsize);
208
+            get_buffer(&s->pb, pkt->data + 2 + c->gmcsize, size - c->gmcsize - 2);
209
+            pkt->stream_index = 0;
210
+            c->curstrm = -1;
211
+        }else{
212
+            if (av_get_packet(&s->pb, pkt, c->sndsize - 4) < 0)
213
+                return AVERROR(EIO);
214
+            pkt->stream_index = 1;
215
+            c->curstrm = 0;
216
+        }
217
+        if(!c->cur_frame || c->curstrm)
218
+            pkt->flags |= PKT_FLAG_KEY;
219
+        if (c->curstrm == -1)
220
+            c->cur_frame++;
221
+    }else{
222
+        size = av_get_packet(&s->pb, pkt, c->block_align);
223
+        if(size <= 0)
224
+            return AVERROR(EIO);
225
+    }
226
+    return pkt->size;
227
+}
228
+
229
+AVInputFormat siff_demuxer = {
230
+    "siff",
231
+    "Beam Software SIFF",
232
+    sizeof(SIFFContext),
233
+    siff_probe,
234
+    siff_read_header,
235
+    siff_read_packet,
236
+    .extensions = "vb,son"
237
+};