siff.c 7.68 KB
Newer Older
1 2
/*
 * Beam Software SIFF demuxer
3
 * Copyright (c) 2007 Konstantin Shishkov
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
 */

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 31 32 33 34 35 36 37
    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'),
};

38
enum VBFlags {
39 40 41 42 43 44 45
    VB_HAS_GMC     = 0x01,
    VB_HAS_AUDIO   = 0x04,
    VB_HAS_VIDEO   = 0x08,
    VB_HAS_PALETTE = 0x10,
    VB_HAS_LENGTH  = 0x20
};

46
typedef struct SIFFContext {
47 48 49 50 51 52 53 54 55 56
    int frames;
    int cur_frame;
    int rate;
    int bits;
    int block_align;

    int has_video;
    int has_audio;

    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 65 66

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

static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
{
    AVStream *ast;
78
    ast = avformat_new_stream(s, NULL);
79
    if (!ast)
80
        return AVERROR(ENOMEM);
81 82 83 84 85 86
    ast->codecpar->codec_type            = AVMEDIA_TYPE_AUDIO;
    ast->codecpar->codec_id              = AV_CODEC_ID_PCM_U8;
    ast->codecpar->channels              = 1;
    ast->codecpar->channel_layout        = AV_CH_LAYOUT_MONO;
    ast->codecpar->bits_per_coded_sample = 8;
    ast->codecpar->sample_rate           = c->rate;
87
    avpriv_set_pts_info(ast, 16, 1, c->rate);
88
    ast->start_time                   = 0;
89 90 91
    return 0;
}

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

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

121
    avio_skip(pb, 16); // zeroes
122

123
    st = avformat_new_stream(s, NULL);
124
    if (!st)
125
        return AVERROR(ENOMEM);
126 127 128 129 130 131
    st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
    st->codecpar->codec_id   = AV_CODEC_ID_VB;
    st->codecpar->codec_tag  = MKTAG('V', 'B', 'V', '1');
    st->codecpar->width      = width;
    st->codecpar->height     = height;
    st->codecpar->format     = AV_PIX_FMT_PAL8;
132 133
    st->nb_frames            =
    st->duration             = c->frames;
134
    avpriv_set_pts_info(st, 16, 1, 12);
135 136 137 138

    c->cur_frame = 0;
    c->has_video = 1;
    c->has_audio = !!c->rate;
139
    c->curstrm   = -1;
140 141
    if (c->has_audio)
        return create_audio_stream(s, c);
142 143 144
    return 0;
}

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

162
static int siff_read_header(AVFormatContext *s)
163
{
164
    AVIOContext *pb = s->pb;
165
    SIFFContext *c  = s->priv_data;
166
    uint32_t tag;
167
    int ret;
168

169
    if (avio_rl32(pb) != TAG_SIFF)
170
        return AVERROR_INVALIDDATA;
171
    avio_skip(pb, 4); // ignore size
172
    tag = avio_rl32(pb);
173

174
    if (tag != TAG_VBV1 && tag != TAG_SOUN) {
175
        av_log(s, AV_LOG_ERROR, "Not a VBV file\n");
176
        return AVERROR_INVALIDDATA;
177 178
    }

179 180 181 182
    if (tag == TAG_VBV1 && (ret = siff_parse_vbv1(s, c, pb)) < 0)
        return ret;
    if (tag == TAG_SOUN && (ret = siff_parse_soun(s, c, pb)) < 0)
        return ret;
183
    if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')) {
184
        av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
185
        return AVERROR_INVALIDDATA;
186
    }
187
    avio_skip(pb, 4); // ignore size
188 189 190 191 192 193 194 195

    return 0;
}

static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    SIFFContext *c = s->priv_data;

196
    if (c->has_video) {
197
        unsigned int size;
198
        if (c->cur_frame >= c->frames)
Piotr Bandurski's avatar
Piotr Bandurski committed
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 204
            c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
            if (c->gmcsize)
205
                avio_read(s->pb, c->gmc, c->gmcsize);
206
            c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb) : 0;
207 208 209
            c->curstrm = !!(c->flags & VB_HAS_AUDIO);
        }

210
        if (!c->curstrm) {
211 212 213
            if (c->pktsize < 2LL + c->sndsize + c->gmcsize)
                return AVERROR_INVALIDDATA;

214 215 216
            size = c->pktsize - c->sndsize - c->gmcsize - 2;
            size = ffio_limit(s->pb, size);
            if (av_new_packet(pkt, size + c->gmcsize + 2) < 0)
217 218 219 220
                return AVERROR(ENOMEM);
            AV_WL16(pkt->data, c->flags);
            if (c->gmcsize)
                memcpy(pkt->data + 2, c->gmc, c->gmcsize);
221
            if (avio_read(s->pb, pkt->data + 2 + c->gmcsize, size) != size) {
222
                av_packet_unref(pkt);
223 224
                return AVERROR_INVALIDDATA;
            }
225
            pkt->stream_index = 0;
226 227
            c->curstrm        = -1;
        } else {
228 229
            int pktsize = av_get_packet(s->pb, pkt, c->sndsize - 4);
            if (pktsize < 0)
230 231
                return AVERROR(EIO);
            pkt->stream_index = 1;
232
            pkt->duration     = pktsize;
233
            c->curstrm        = 0;
234
        }
235
        if (!c->cur_frame || c->curstrm)
236
            pkt->flags |= AV_PKT_FLAG_KEY;
237 238
        if (c->curstrm == -1)
            c->cur_frame++;
239
    } else {
240
        int pktsize = av_get_packet(s->pb, pkt, c->block_align);
241
        if (!pktsize)
Piotr Bandurski's avatar
Piotr Bandurski committed
242
            return AVERROR_EOF;
243
        if (pktsize <= 0)
244
            return AVERROR(EIO);
245
        pkt->duration = pktsize;
246 247 248 249
    }
    return pkt->size;
}

250
AVInputFormat ff_siff_demuxer = {
251 252 253 254 255 256
    .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,
257
    .extensions     = "vb,son",
258
};