siff.c 6.82 KB
Newer Older
1 2
/*
 * Beam Software SIFF demuxer
3
 * Copyright (c) 2007 Konstantin Shishkov
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9 10 11
 * 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.
 *
12
 * Libav is distributed in the hope that it will be useful,
13 14 15 16 17
 * 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
18
 * License along with Libav; if not, write to the Free Software
19 20 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22
#include "libavutil/channel_layout.h"
23
#include "libavutil/intreadwrite.h"
24
#include "avformat.h"
25
#include "internal.h"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

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 bits;
    int block_align;

    int has_video;
    int has_audio;

    int curstrm;
    int pktsize;
    int gmcsize;
    int sndsize;

    int flags;
    uint8_t gmc[4];
}SIFFContext;

static int siff_probe(AVProbeData *p)
{
65
    uint32_t tag = AV_RL32(p->buf + 8);
66
    /* check file header */
67 68
    if (AV_RL32(p->buf) != TAG_SIFF ||
        (tag != TAG_VBV1 && tag != TAG_SOUN))
69
        return 0;
70
    return AVPROBE_SCORE_MAX;
71 72 73 74 75
}

static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
{
    AVStream *ast;
76
    ast = avformat_new_stream(s, NULL);
77 78
    if (!ast)
        return -1;
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);
86
    ast->start_time = 0;
87 88 89
    return 0;
}

90
static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
91 92 93 94
{
    AVStream *st;
    int width, height;

95
    if (avio_rl32(pb) != TAG_VBHD){
96 97 98
        av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
        return -1;
    }
99
    if(avio_rb32(pb) != 32){
100 101 102
        av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
        return -1;
    }
103
    if(avio_rl16(pb) != 1){
104 105 106
        av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
        return -1;
    }
107 108
    width = avio_rl16(pb);
    height = avio_rl16(pb);
109
    avio_skip(pb, 4);
110
    c->frames = avio_rl16(pb);
111 112 113 114
    if(!c->frames){
        av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
        return -1;
    }
115 116
    c->bits = avio_rl16(pb);
    c->rate = avio_rl16(pb);
117 118
    c->block_align = c->rate * (c->bits >> 3);

119
    avio_skip(pb, 16); //zeroes
120

121
    st = avformat_new_stream(s, NULL);
122 123
    if (!st)
        return -1;
124
    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
125
    st->codec->codec_id   = AV_CODEC_ID_VB;
126 127 128
    st->codec->codec_tag  = MKTAG('V', 'B', 'V', '1');
    st->codec->width      = width;
    st->codec->height     = height;
129
    st->codec->pix_fmt    = AV_PIX_FMT_PAL8;
130
    avpriv_set_pts_info(st, 16, 1, 12);
131 132 133 134 135 136 137 138 139 140

    c->cur_frame = 0;
    c->has_video = 1;
    c->has_audio = !!c->rate;
    c->curstrm = -1;
    if (c->has_audio && create_audio_stream(s, c) < 0)
        return -1;
    return 0;
}

141
static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
142
{
143
    if (avio_rl32(pb) != TAG_SHDR){
144 145 146
        av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
        return -1;
    }
147
    if(avio_rb32(pb) != 8){
148 149 150
        av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
        return -1;
    }
151
    avio_skip(pb, 4); //unknown value
152 153
    c->rate = avio_rl16(pb);
    c->bits = avio_rl16(pb);
154 155 156 157
    c->block_align = c->rate * (c->bits >> 3);
    return create_audio_stream(s, c);
}

158
static int siff_read_header(AVFormatContext *s)
159
{
160
    AVIOContext *pb = s->pb;
161 162 163
    SIFFContext *c = s->priv_data;
    uint32_t tag;

164
    if (avio_rl32(pb) != TAG_SIFF)
165
        return -1;
166
    avio_skip(pb, 4); //ignore size
167
    tag = avio_rl32(pb);
168 169 170 171 172 173 174 175 176 177

    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;
178
    if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')){
179 180 181
        av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
        return -1;
    }
182
    avio_skip(pb, 4); //ignore size
183 184 185 186 187 188 189

    return 0;
}

static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    SIFFContext *c = s->priv_data;
190
    int size;
191 192 193 194 195

    if (c->has_video){
        if (c->cur_frame >= c->frames)
            return AVERROR(EIO);
        if (c->curstrm == -1){
196 197
            c->pktsize = avio_rl32(s->pb) - 4;
            c->flags = avio_rl16(s->pb);
198 199
            c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
            if (c->gmcsize)
200 201
                avio_read(s->pb, c->gmc, c->gmcsize);
            c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb): 0;
202 203 204 205 206 207 208 209 210 211
            c->curstrm = !!(c->flags & VB_HAS_AUDIO);
        }

        if (!c->curstrm){
            size = c->pktsize - c->sndsize;
            if (av_new_packet(pkt, size) < 0)
                return AVERROR(ENOMEM);
            AV_WL16(pkt->data, c->flags);
            if (c->gmcsize)
                memcpy(pkt->data + 2, c->gmc, c->gmcsize);
212
            avio_read(s->pb, pkt->data + 2 + c->gmcsize, size - c->gmcsize - 2);
213 214 215
            pkt->stream_index = 0;
            c->curstrm = -1;
        }else{
216
            if ((size = av_get_packet(s->pb, pkt, c->sndsize - 4)) < 0)
217 218
                return AVERROR(EIO);
            pkt->stream_index = 1;
219
            pkt->duration     = size;
220 221 222
            c->curstrm = 0;
        }
        if(!c->cur_frame || c->curstrm)
223
            pkt->flags |= AV_PKT_FLAG_KEY;
224 225 226
        if (c->curstrm == -1)
            c->cur_frame++;
    }else{
227
        size = av_get_packet(s->pb, pkt, c->block_align);
228 229
        if(size <= 0)
            return AVERROR(EIO);
230
        pkt->duration = size;
231 232 233 234
    }
    return pkt->size;
}

235
AVInputFormat ff_siff_demuxer = {
236 237 238 239 240 241
    .name           = "siff",
    .long_name      = NULL_IF_CONFIG_SMALL("Beam Software SIFF"),
    .priv_data_size = sizeof(SIFFContext),
    .read_probe     = siff_probe,
    .read_header    = siff_read_header,
    .read_packet    = siff_read_packet,
242
    .extensions     = "vb,son",
243
};