2007-10-22 06:01:38 +00:00
|
|
|
/*
|
|
|
|
* Beam Software SIFF demuxer
|
2009-01-19 15:46:40 +00:00
|
|
|
* Copyright (c) 2007 Konstantin Shishkov
|
2007-10-22 06:01:38 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* This file is part of Libav.
|
2007-10-22 06:01:38 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2007-10-22 06:01:38 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2007-10-22 06:01:38 +00:00
|
|
|
* 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
|
2011-03-18 17:35:10 +00:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2007-10-22 06:01:38 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2012-04-07 22:36:08 +00:00
|
|
|
#include "libavutil/channel_layout.h"
|
2009-01-11 22:19:48 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2007-10-22 06:01:38 +00:00
|
|
|
#include "avformat.h"
|
2011-11-29 18:28:15 +00:00
|
|
|
#include "internal.h"
|
2007-10-22 06:01:38 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2009-09-17 18:09:20 +00:00
|
|
|
uint32_t tag = AV_RL32(p->buf + 8);
|
2007-10-22 06:01:38 +00:00
|
|
|
/* check file header */
|
2009-09-17 18:09:20 +00:00
|
|
|
if (AV_RL32(p->buf) != TAG_SIFF ||
|
|
|
|
(tag != TAG_VBV1 && tag != TAG_SOUN))
|
2007-10-22 06:01:38 +00:00
|
|
|
return 0;
|
2009-09-17 18:09:20 +00:00
|
|
|
return AVPROBE_SCORE_MAX;
|
2007-10-22 06:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
|
|
|
|
{
|
|
|
|
AVStream *ast;
|
2011-06-18 09:43:24 +00:00
|
|
|
ast = avformat_new_stream(s, NULL);
|
2007-10-22 06:01:38 +00:00
|
|
|
if (!ast)
|
|
|
|
return -1;
|
2010-03-30 23:30:55 +00:00
|
|
|
ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
|
2012-08-05 09:11:04 +00:00
|
|
|
ast->codec->codec_id = AV_CODEC_ID_PCM_U8;
|
2007-10-22 06:01:38 +00:00
|
|
|
ast->codec->channels = 1;
|
2012-04-07 22:36:08 +00:00
|
|
|
ast->codec->channel_layout = AV_CH_LAYOUT_MONO;
|
2012-02-27 04:27:21 +00:00
|
|
|
ast->codec->bits_per_coded_sample = 8;
|
2007-10-22 06:01:38 +00:00
|
|
|
ast->codec->sample_rate = c->rate;
|
2011-11-29 18:28:15 +00:00
|
|
|
avpriv_set_pts_info(ast, 16, 1, c->rate);
|
2012-02-27 04:27:21 +00:00
|
|
|
ast->start_time = 0;
|
2007-10-22 06:01:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-20 10:04:12 +00:00
|
|
|
static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
|
2007-10-22 06:01:38 +00:00
|
|
|
{
|
|
|
|
AVStream *st;
|
|
|
|
int width, height;
|
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_rl32(pb) != TAG_VBHD){
|
2007-10-22 06:01:38 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-02-21 15:43:01 +00:00
|
|
|
if(avio_rb32(pb) != 32){
|
2007-10-22 06:01:38 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-02-21 15:43:01 +00:00
|
|
|
if(avio_rl16(pb) != 1){
|
2007-10-22 06:01:38 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "Incorrect header version\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-02-21 15:43:01 +00:00
|
|
|
width = avio_rl16(pb);
|
|
|
|
height = avio_rl16(pb);
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 4);
|
2011-02-21 15:43:01 +00:00
|
|
|
c->frames = avio_rl16(pb);
|
2007-10-22 06:01:38 +00:00
|
|
|
if(!c->frames){
|
|
|
|
av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-02-21 15:43:01 +00:00
|
|
|
c->bits = avio_rl16(pb);
|
|
|
|
c->rate = avio_rl16(pb);
|
2007-10-22 06:01:38 +00:00
|
|
|
c->block_align = c->rate * (c->bits >> 3);
|
|
|
|
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 16); //zeroes
|
2007-10-22 06:01:38 +00:00
|
|
|
|
2011-06-18 09:43:24 +00:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2007-10-22 06:01:38 +00:00
|
|
|
if (!st)
|
|
|
|
return -1;
|
2010-03-30 23:30:55 +00:00
|
|
|
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
|
2012-08-05 09:11:04 +00:00
|
|
|
st->codec->codec_id = AV_CODEC_ID_VB;
|
2007-10-22 06:01:38 +00:00
|
|
|
st->codec->codec_tag = MKTAG('V', 'B', 'V', '1');
|
|
|
|
st->codec->width = width;
|
|
|
|
st->codec->height = height;
|
2012-10-06 10:10:34 +00:00
|
|
|
st->codec->pix_fmt = AV_PIX_FMT_PAL8;
|
2011-11-29 18:28:15 +00:00
|
|
|
avpriv_set_pts_info(st, 16, 1, 12);
|
2007-10-22 06:01:38 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-02-20 10:04:12 +00:00
|
|
|
static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, AVIOContext *pb)
|
2007-10-22 06:01:38 +00:00
|
|
|
{
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_rl32(pb) != TAG_SHDR){
|
2007-10-22 06:01:38 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-02-21 15:43:01 +00:00
|
|
|
if(avio_rb32(pb) != 8){
|
2007-10-22 06:01:38 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "Header chunk size is incorrect\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 4); //unknown value
|
2011-02-21 15:43:01 +00:00
|
|
|
c->rate = avio_rl16(pb);
|
|
|
|
c->bits = avio_rl16(pb);
|
2007-10-22 06:01:38 +00:00
|
|
|
c->block_align = c->rate * (c->bits >> 3);
|
|
|
|
return create_audio_stream(s, c);
|
|
|
|
}
|
|
|
|
|
2012-01-12 12:20:36 +00:00
|
|
|
static int siff_read_header(AVFormatContext *s)
|
2007-10-22 06:01:38 +00:00
|
|
|
{
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2007-10-22 06:01:38 +00:00
|
|
|
SIFFContext *c = s->priv_data;
|
|
|
|
uint32_t tag;
|
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_rl32(pb) != TAG_SIFF)
|
2007-10-22 06:01:38 +00:00
|
|
|
return -1;
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 4); //ignore size
|
2011-02-21 15:43:01 +00:00
|
|
|
tag = avio_rl32(pb);
|
2007-10-22 06:01:38 +00:00
|
|
|
|
|
|
|
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;
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_rl32(pb) != MKTAG('B', 'O', 'D', 'Y')){
|
2007-10-22 06:01:38 +00:00
|
|
|
av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 4); //ignore size
|
2007-10-22 06:01:38 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
SIFFContext *c = s->priv_data;
|
2007-10-31 11:35:10 +00:00
|
|
|
int size;
|
2007-10-22 06:01:38 +00:00
|
|
|
|
|
|
|
if (c->has_video){
|
|
|
|
if (c->cur_frame >= c->frames)
|
|
|
|
return AVERROR(EIO);
|
|
|
|
if (c->curstrm == -1){
|
2011-02-21 15:43:01 +00:00
|
|
|
c->pktsize = avio_rl32(s->pb) - 4;
|
|
|
|
c->flags = avio_rl16(s->pb);
|
2007-10-22 06:01:38 +00:00
|
|
|
c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
|
|
|
|
if (c->gmcsize)
|
2011-02-21 15:43:01 +00:00
|
|
|
avio_read(s->pb, c->gmc, c->gmcsize);
|
|
|
|
c->sndsize = (c->flags & VB_HAS_AUDIO) ? avio_rl32(s->pb): 0;
|
2007-10-22 06:01:38 +00:00
|
|
|
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);
|
2011-02-21 15:43:01 +00:00
|
|
|
avio_read(s->pb, pkt->data + 2 + c->gmcsize, size - c->gmcsize - 2);
|
2007-10-22 06:01:38 +00:00
|
|
|
pkt->stream_index = 0;
|
|
|
|
c->curstrm = -1;
|
|
|
|
}else{
|
2012-02-27 04:27:21 +00:00
|
|
|
if ((size = av_get_packet(s->pb, pkt, c->sndsize - 4)) < 0)
|
2007-10-22 06:01:38 +00:00
|
|
|
return AVERROR(EIO);
|
|
|
|
pkt->stream_index = 1;
|
2012-02-27 04:27:21 +00:00
|
|
|
pkt->duration = size;
|
2007-10-22 06:01:38 +00:00
|
|
|
c->curstrm = 0;
|
|
|
|
}
|
|
|
|
if(!c->cur_frame || c->curstrm)
|
2010-03-31 12:29:58 +00:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2007-10-22 06:01:38 +00:00
|
|
|
if (c->curstrm == -1)
|
|
|
|
c->cur_frame++;
|
|
|
|
}else{
|
2007-11-21 07:41:00 +00:00
|
|
|
size = av_get_packet(s->pb, pkt, c->block_align);
|
2007-10-22 06:01:38 +00:00
|
|
|
if(size <= 0)
|
|
|
|
return AVERROR(EIO);
|
2012-02-27 04:27:21 +00:00
|
|
|
pkt->duration = size;
|
2007-10-22 06:01:38 +00:00
|
|
|
}
|
|
|
|
return pkt->size;
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:03:28 +00:00
|
|
|
AVInputFormat ff_siff_demuxer = {
|
2011-07-16 20:18:12 +00:00
|
|
|
.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,
|
2012-04-06 14:50:48 +00:00
|
|
|
.extensions = "vb,son",
|
2007-10-22 06:01:38 +00:00
|
|
|
};
|