[FFmpeg-devel] [PATCH] SIFF demuxer and decoder

Kostya kostya.shishkov
Mon Oct 15 13:59:47 CEST 2007


Here is demuxer and video decoder for SIFF format
based on description sent to me by anonymous
contributor.

Samples are at:
ftp://upload.mplqyerhq.hu/incoming/siff_samples.tar.bz2
-------------- next part --------------
Index: Changelog
===================================================================
--- Changelog	(revision 10739)
+++ Changelog	(working copy)
@@ -99,6 +99,7 @@
 - AMV audio and video decoder
 - DNxHD encoder
 - H.264 PAFF decoding
+- Beam Software SIFF demuxer and decoder
 
 version 0.4.9-pre1:
 
Index: libavcodec/Makefile
===================================================================
--- libavcodec/Makefile	(revision 10739)
+++ libavcodec/Makefile	(working copy)
@@ -185,6 +186,7 @@
 OBJS-$(CONFIG_TTA_DECODER)             += tta.o
 OBJS-$(CONFIG_TXD_DECODER)             += txd.o s3tc.o
 OBJS-$(CONFIG_ULTI_DECODER)            += ulti.o
+OBJS-$(CONFIG_VB_DECODER)              += vb.o
 OBJS-$(CONFIG_VC1_DECODER)             += vc1.o vc1data.o vc1dsp.o msmpeg4data.o
 OBJS-$(CONFIG_VCR1_DECODER)            += vcr1.o
 OBJS-$(CONFIG_VCR1_ENCODER)            += vcr1.o
Index: libavcodec/allcodecs.c
===================================================================
--- libavcodec/allcodecs.c	(revision 10739)
+++ libavcodec/allcodecs.c	(working copy)
@@ -150,6 +152,7 @@
     REGISTER_DECODER (TSCC, tscc);
     REGISTER_DECODER (TXD, txd);
     REGISTER_DECODER (ULTI, ulti);
+    REGISTER_DECODER (VB, vb);
     REGISTER_DECODER (VC1, vc1);
     REGISTER_DECODER (VCR1, vcr1);
     REGISTER_DECODER (VMDVIDEO, vmdvideo);
Index: libavcodec/avcodec.h
===================================================================
--- libavcodec/avcodec.h	(revision 10739)
+++ libavcodec/avcodec.h	(working copy)
@@ -168,6 +168,7 @@
     CODEC_ID_TXD,
     CODEC_ID_VP6A,
     CODEC_ID_AMV,
+    CODEC_ID_VB,
 
     /* various PCM "codecs" */
     CODEC_ID_PCM_S16LE= 0x10000,
Index: libavcodec/vb.c
===================================================================
--- libavcodec/vb.c	(revision 0)
+++ libavcodec/vb.c	(revision 0)
@@ -0,0 +1,274 @@
+/*
+ * Beam Software VB decoder
+ * Copyright (c) 2007 Konstantin Shishkov
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file vb.c
+ * VB Video decoder
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "avcodec.h"
+
+enum VBFlags{
+    VB_HAS_GMC     = 0x01,
+    VB_HAS_AUDIO   = 0x04,
+    VB_HAS_VIDEO   = 0x08,
+    VB_HAS_PALETTE = 0x10,
+    VB_HAS_LENGTH  = 0x20
+};
+
+typedef struct VBDecContext {
+    AVCodecContext *avctx;
+    AVFrame pic;
+
+    uint8_t *frame, *prev_frame;
+    uint32_t pal[256];
+} VBDecContext;
+
+static const uint16_t vb_patterns[64] = {
+    0x0660, 0xFF00, 0xCCCC, 0xF000, 0x8888, 0x000F, 0x1111, 0xFEC8,
+    0x8CEF, 0x137F, 0xF731, 0xC800, 0x008C, 0x0013, 0x3100, 0xCC00,
+    0x00CC, 0x0033, 0x3300, 0x0FF0, 0x6666, 0x00F0, 0x0F00, 0x2222,
+    0x4444, 0xF600, 0x8CC8, 0x006F, 0x1331, 0x318C, 0xC813, 0x33CC,
+    0x6600, 0x0CC0, 0x0066, 0x0330, 0xF900, 0xC88C, 0x009F, 0x3113,
+    0x6000, 0x0880, 0x0006, 0x0110, 0xCC88, 0xFC00, 0x00CF, 0x88CC,
+    0x003F, 0x1133, 0x3311, 0xF300, 0x6FF6, 0x0603, 0x08C6, 0x8C63,
+    0xC631, 0x6310, 0xC060, 0x0136, 0x136C, 0x36C8, 0x6C80, 0x324C
+};
+
+static void vb_decode_palette(VBDecContext *c, uint8_t *buf)
+{
+    int start, size, i;
+    int r, g, b;
+
+    start = *buf++;
+    size = ((*buf++) - 1) & 0xFF;
+    for(i = start; i <= start + size; i++){
+        r = *buf++;
+        g = *buf++;
+        b = *buf++;
+        c->pal[i] = (r << 16) | (g << 8) | b;
+    }
+}
+
+static int vb_decode_framedata(VBDecContext *c, uint8_t *buf, int offset)
+{
+    uint8_t *prev, *cur;
+    int blk, blocks, t, blk2;
+    int blocktypes = 0;
+    int x, y, a, b;
+    int pattype, pattern;
+    const int width = c->avctx->width;
+    
+    prev = c->prev_frame + offset;
+    cur = c->frame;
+    
+    blocks = (c->avctx->width >> 2) * (c->avctx->height >> 2);
+    blk2 = 0;
+    for(blk = 0; blk < blocks; blk++){
+        if(!(blk & 3))
+            blocktypes = *buf++;
+        switch(blocktypes & 0xC0){
+        case 0x00: //skip
+            for(y = 0; y < 4; y++)
+                memcpy(cur + y*width, prev + y*width, 4);
+            break;
+        case 0x40:
+            t = *buf++;
+            if(!t){ //raw block
+                for(y = 0; y < 4; y++)
+                    memcpy(cur + y*width, buf + y*4, 4);
+                buf += 16;
+            }else{ // motion compensation
+                x = ((t & 0xF)^8) - 8;
+                y = ((t >> 4) ^8) - 8;
+                t = x + y*width;
+                for(y = 0; y < 4; y++)
+                    memcpy(cur + y*width, prev + t + y*width, 4);
+            }
+            break;
+        case 0x80: // fill
+            t = *buf++;
+            for(y = 0; y < 4; y++)
+                memset(cur + y*width, t, 4);
+            break;
+        case 0xC0: // pattern fill
+            t = *buf++;
+            pattype = t >> 6;
+            pattern = vb_patterns[t & 0x3F];
+            switch(pattype){
+            case 0:
+                a = *buf++;
+                b = *buf++;
+                for(y = 0; y < 4; y++)
+                    for(x = 0; x < 4; x++, pattern >>= 1)
+                        cur[x + y*width] = (pattern & 1) ? b : a;
+                break;
+            case 1:
+                pattern = ~pattern;
+            case 2:
+                a = *buf++;
+                for(y = 0; y < 4; y++)
+                    for(x = 0; x < 4; x++, pattern >>= 1)
+                        cur[x + y*width] = (pattern & 1) ? prev[x + y*width] : a;
+                break;
+            case 3:
+                av_log(c->avctx, AV_LOG_ERROR, "Invalid opcode seen @%d\n",blk);
+                return -1;
+            }
+            break;
+        }
+        blocktypes <<= 2;
+        cur  += 4;
+        prev += 4;
+        blk2++;
+        if(blk2 == (width >> 2)){
+            blk2 = 0;
+            cur  += width * 3;
+            prev += width * 3;
+        }
+    }
+    return 0;
+}
+
+static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
+{
+    VBDecContext * const c = avctx->priv_data;
+    uint8_t *outptr, *srcptr;
+    int i, j;
+    int flags;
+    uint32_t size;
+    int rest = buf_size;
+    int offset = 0;
+    
+    flags = AV_RL16(buf);
+    buf += 2;
+    rest -= 2;
+    
+    if(c->pic.data[0])
+        avctx->release_buffer(avctx, &c->pic);
+
+    if(flags & VB_HAS_GMC){
+        i = (int16_t)AV_RL16(buf  );
+        j = (int16_t)AV_RL16(buf+2);
+        offset = i + j * avctx->width;
+        buf += 4;
+        rest -= 4;
+    }
+    if(flags & VB_HAS_AUDIO){
+        size = AV_RL32(buf);
+        if(size > rest){
+            av_log(avctx, AV_LOG_ERROR, "Sound size is too big\n");
+            return -1;
+        }
+        buf += size;
+        rest -= size;
+    }
+    if(flags & VB_HAS_VIDEO){
+        size = AV_RL32(buf);
+        if(size > rest){
+            av_log(avctx, AV_LOG_ERROR, "Frame size is too big\n");
+            return -1;
+        }
+        vb_decode_framedata(c, buf + 4, offset);
+        buf += size;
+        rest -= size;
+    }
+    if(flags & VB_HAS_PALETTE){
+        size = AV_RL32(buf);
+        if(size > rest){
+            av_log(avctx, AV_LOG_ERROR, "Palette size is too big\n");
+            return -1;
+        }
+        vb_decode_palette(c, buf + 4);
+        buf += size;
+        rest -= size;
+    }
+
+    c->pic.reference = 1;
+    if(avctx->get_buffer(avctx, &c->pic) < 0){
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
+        return -1;
+    }
+
+    memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
+    c->pic.palette_has_changed = flags & VB_HAS_PALETTE;
+
+    outptr = c->pic.data[0];
+    srcptr = c->frame;
+    
+    for(i = 0; i < avctx->height; i++){
+        memcpy(outptr, srcptr, avctx->width);
+        srcptr += avctx->width;
+        outptr += c->pic.linesize[0];
+    }
+
+    memcpy(c->prev_frame, c->frame, avctx->width * avctx->height);
+
+    *data_size = sizeof(AVFrame);
+    *(AVFrame*)data = c->pic;
+
+    /* always report that the buffer was completely consumed */
+    return buf_size;
+}
+
+static int decode_init(AVCodecContext *avctx)
+{
+    VBDecContext * const c = avctx->priv_data;
+
+    c->avctx = avctx;
+    avctx->pix_fmt = PIX_FMT_PAL8;
+
+    if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
+        return -1;
+    }
+
+    c->frame      = av_malloc( avctx->width * avctx->height);
+    c->prev_frame = av_malloc( avctx->width * avctx->height);
+
+    return 0;
+}
+
+static int decode_end(AVCodecContext *avctx)
+{
+    VBDecContext *c = avctx->priv_data;
+
+    av_freep(&c->frame);
+    av_freep(&c->prev_frame);
+    if(c->pic.data[0])
+        avctx->release_buffer(avctx, &c->pic);
+
+    return 0;
+}
+
+AVCodec vb_decoder = {
+    "vb",
+    CODEC_TYPE_VIDEO,
+    CODEC_ID_VB,
+    sizeof(VBDecContext),
+    decode_init,
+    NULL,
+    decode_end,
+    decode_frame
+};
+
Index: doc/general.texi
===================================================================
--- doc/general.texi	(revision 10739)
+++ doc/general.texi	(working copy)
@@ -117,6 +117,8 @@
 @item CRYO APC @tab    @tab X
 @tab Audio format used in some games by CRYO Interactive Entertainment.
 @item Monkey's Audio @tab    @tab X
+ at item SIFF @tab    @tab X
+ at tab Audio and video format used in some games by Beam Software
 @end multitable
 
 @code{X} means that encoding (resp. decoding) is supported.
Index: libavformat/siff.c
===================================================================
--- libavformat/siff.c	(revision 0)
+++ libavformat/siff.c	(revision 0)
@@ -0,0 +1,255 @@
+/*
+ * Beam Software SIFF demuxer
+ * Copyright (c) 2007 Konstantin Shishkov.
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "avformat.h"
+#include "riff.h"
+
+enum SIFFTags{
+    TAG_SIFF = MKTAG('S', 'I', 'F', 'F'),
+    TAG_BODY = MKTAG('B', 'O', 'D', 'Y'),
+    TAG_VBHD = MKTAG('V', 'B', 'H', 'D'),
+    TAG_SHDR = MKTAG('S', 'H', 'D', 'R'),
+    TAG_VBV1 = MKTAG('V', 'B', 'V', '1'),
+    TAG_SOUN = MKTAG('S', 'O', 'U', 'N'),
+};
+
+enum VBFlags{
+    VB_HAS_GMC     = 0x01,
+    VB_HAS_AUDIO   = 0x04,
+    VB_HAS_VIDEO   = 0x08,
+    VB_HAS_PALETTE = 0x10,
+    VB_HAS_LENGTH  = 0x20
+};
+
+typedef struct SIFFContext{
+    int frames;
+    int cur_frame;
+    int rate;
+    int block_align;
+    int has_video;
+    int has_audio;
+    int next_audio;
+    int curstrm;
+    uint8_t *blkdata;
+}SIFFContext;
+
+static int siff_probe(AVProbeData *p)
+{
+    /* check file header */
+    if (p->buf[0] == 'S' && p->buf[1] == 'I' &&
+        p->buf[2] == 'F' && p->buf[3] == 'F')
+        return AVPROBE_SCORE_MAX;
+    else
+        return 0;
+}
+
+static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, ByteIOContext *pb)
+{
+    AVStream *st, *ast;
+    uint32_t tag, size;
+    int width, height, bits;
+
+    tag = get_le32(pb);
+    if (tag != TAG_VBHD){
+        av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
+        return -1;
+    }
+    size = get_be32(pb);
+    if(size != 32){
+        av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
+        return -1;    
+    }
+    if(get_le16(pb) != 1){
+        av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
+        return -1;    
+    }
+    width = get_le16(pb);
+    height = get_le16(pb);
+    url_fskip(pb, 4);
+    c->frames = get_le16(pb);
+    if(!c->frames){
+        av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
+        return -1;
+    }
+    bits = get_le16(pb);
+    c->rate = get_le16(pb);
+    c->block_align = c->rate * (bits >> 3);
+
+    url_fskip(pb, 16); //zeroes
+
+    st = av_new_stream(s, 0);
+    if (!st)
+        return -1;
+    st->codec->codec_type = CODEC_TYPE_VIDEO;
+    st->codec->codec_id   = CODEC_ID_VB;
+    st->codec->codec_tag  = MKTAG('V', 'B', 'V', '1');
+    st->codec->width      = width;
+    st->codec->height     = height;
+    st->codec->pix_fmt    = PIX_FMT_PAL8;
+    av_set_pts_info(st, 33, 1, 12);
+
+    c->has_audio = !!c->rate;
+    if (c->has_audio){
+        ast = av_new_stream(s, 0);
+        if (!ast)
+            return -1;
+        ast->codec->codec_type      = CODEC_TYPE_AUDIO;
+        ast->codec->codec_id        = CODEC_ID_PCM_U8;
+        ast->codec->channels        = 1;
+        ast->codec->bits_per_sample = bits;
+        ast->codec->sample_rate     = c->rate;
+        ast->codec->frame_size      = c->block_align;
+        av_set_pts_info(ast, 33, 1, c->rate);
+    }
+    
+    c->cur_frame = 0;
+    c->has_video = 1;
+    return 0;
+}
+
+static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, ByteIOContext *pb)
+{
+    AVStream *st;
+    uint32_t tag, size;
+    int bits;
+
+    tag = get_le32(pb);
+    if (tag != TAG_SHDR){
+        av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
+        return -1;
+    }
+    size = get_be32(pb);
+    if(size != 8){
+        av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
+        return -1;    
+    }
+    url_fskip(pb, 4); //unknown value
+    c->rate = get_le16(pb);
+    bits = get_le16(pb);
+    c->block_align = c->rate * (bits >> 3);
+
+    st = av_new_stream(s, 0);
+    if (!st)
+        return -1;
+    st->codec->codec_type      = CODEC_TYPE_AUDIO;
+    st->codec->codec_id        = CODEC_ID_PCM_U8;
+    st->codec->channels        = 1;
+    st->codec->bits_per_sample = bits;
+    st->codec->sample_rate     = c->rate;
+    st->codec->frame_size      = c->block_align;
+    av_set_pts_info(st, 33, 1, c->rate);
+
+    return 0;
+}
+
+static int siff_read_header(AVFormatContext *s, AVFormatParameters *ap)
+{
+    ByteIOContext *pb = &s->pb;
+    SIFFContext *c = s->priv_data;
+    uint32_t tag, size;
+
+    tag = get_le32(pb);
+    if (tag != TAG_SIFF)
+        return -1;
+    size = get_be32(pb);
+    tag = get_le32(pb);
+    
+    if (tag != TAG_VBV1 && tag != TAG_SOUN){
+        av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
+        return -1;
+    }
+
+    if (tag == TAG_VBV1 && siff_parse_vbv1(s, c, pb) < 0)
+        return -1;
+    if (tag == TAG_SOUN && siff_parse_soun(s, c, pb) < 0)
+        return -1;
+    tag = get_le32(pb);
+    if (tag != MKTAG('B', 'O', 'D', 'Y')){
+        av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
+        return -1;
+    }
+    size = get_be32(pb);
+
+    return 0;
+}
+
+static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    SIFFContext *c = s->priv_data;
+    int size, flags;
+    uint8_t *snddata;
+
+    if (c->has_video){
+        if (c->cur_frame >= c->frames)
+            return AVERROR(EIO);
+        if (!c->curstrm){ //get block
+            size = get_le32(&s->pb) - 4;
+            if(av_get_packet(&s->pb, pkt, size) != size)
+                return AVERROR(EIO);
+            if(c->has_audio){
+                c->blkdata = av_realloc(c->blkdata, size);
+                memcpy(c->blkdata, pkt->data, pkt->size);
+            }
+            flags = pkt->data[0];
+            c->next_audio = !!(flags & VB_HAS_AUDIO);
+        }else{
+            //parse block
+            flags = c->blkdata[0];
+            snddata = c->blkdata + 2 + ((flags & VB_HAS_GMC) ? 4 : 0);
+            size = AV_RL32(snddata) - 4;
+            if (av_new_packet(pkt, size) < 0)
+                return AVERROR(ENOMEM);
+            memcpy(pkt->data, snddata + 4, size);
+        }
+        if(!c->cur_frame || c->curstrm)
+            pkt->flags |= PKT_FLAG_KEY;
+        pkt->stream_index = c->curstrm;
+        c->curstrm ^= c->next_audio;
+        if (!c->curstrm)
+            c->cur_frame++;
+    }else{
+        size = av_get_packet(&s->pb, pkt, c->block_align);
+        if(size <= 0)
+            return AVERROR(EIO);
+        pkt->stream_index = 0;
+        pkt->size = size;
+    }
+    return pkt->size;
+}
+
+static int siff_read_close(AVFormatContext *s)
+{
+    SIFFContext *c = s->priv_data;
+
+    av_freep(&c->blkdata);
+    return 0;
+}
+
+AVInputFormat siff_demuxer = {
+    "siff",
+    "Beam Software SIFF",
+    sizeof(SIFFContext),
+    siff_probe,
+    siff_read_header,
+    siff_read_packet,
+    siff_read_close,
+    .extensions = "vb,son"
+};
Index: libavformat/Makefile
===================================================================
--- libavformat/Makefile	(revision 10739)
+++ libavformat/Makefile	(working copy)
@@ -127,6 +127,7 @@
 OBJS-$(CONFIG_SDP_DEMUXER)               += rtsp.o
 OBJS-$(CONFIG_SEGAFILM_DEMUXER)          += segafilm.o
 OBJS-$(CONFIG_SHORTEN_DEMUXER)           += raw.o
+OBJS-$(CONFIG_SIFF_DEMUXER)              += siff.o
 OBJS-$(CONFIG_SMACKER_DEMUXER)           += smacker.o
 OBJS-$(CONFIG_SOL_DEMUXER)               += sol.o raw.o
 OBJS-$(CONFIG_STR_DEMUXER)               += psxstr.o
Index: libavformat/allformats.c
===================================================================
--- libavformat/allformats.c	(revision 10739)
+++ libavformat/allformats.c	(working copy)
@@ -148,6 +148,7 @@
 #endif
     REGISTER_DEMUXER  (SEGAFILM, segafilm);
     REGISTER_DEMUXER  (SHORTEN, shorten);
+    REGISTER_DEMUXER  (SIFF, siff);
     REGISTER_DEMUXER  (SMACKER, smacker);
     REGISTER_DEMUXER  (SOL, sol);
     REGISTER_DEMUXER  (STR, str);
Index: MAINTAINERS
===================================================================
--- MAINTAINERS	(revision 10739)
+++ MAINTAINERS	(working copy)
@@ -176,6 +176,7 @@
   tscc.c                                Kostya Shishkov
   txd.c                                 Ivo van Poorten
   ulti*                                 Kostya Shishkov
+  vb.c                                  Kostya Shishkov
   vc1*                                  Kostya Shishkov
   vcr1.c                                Michael Niedermayer
   vmnc.c                                Kostya Shishkov
@@ -244,6 +245,7 @@
   rtsp.c                                Luca Barbato
   sdp.c                                 Luca Abeni
   segafilm.c                            Mike Melanson
+  siff.c                                Kostya Shishkov
   swf.c                                 Baptiste Coudurier
   txd.c                                 Ivo van Poorten
   v4l2.c                                Luca Abeni



More information about the ffmpeg-devel mailing list