Commit 66f7679c authored by Kostya Shishkov's avatar Kostya Shishkov

Beam Software SIFF demuxer and video decoder

Originally committed as revision 10833 to svn://svn.ffmpeg.org/ffmpeg/trunk
parent 4e745a3b
......@@ -100,6 +100,7 @@ version <next>
- DNxHD encoder
- H.264 PAFF decoding
- Nellymoser ASAO decoder
- Beam Software SIFF demuxer and decoder
version 0.4.9-pre1:
......
......@@ -177,6 +177,7 @@ Codecs:
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
......@@ -245,6 +246,7 @@ Muxers/Demuxers:
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
......
......@@ -117,6 +117,8 @@ different game cutscenes repacked for use with ScummVM.
@item CRYO APC @tab @tab X
@tab Audio format used in some games by CRYO Interactive Entertainment.
@item Monkey's Audio @tab @tab X
@item SIFF @tab @tab X
@tab Audio and video format used in some games by Beam Software
@end multitable
@code{X} means that encoding (resp. decoding) is supported.
......
......@@ -186,6 +186,7 @@ OBJS-$(CONFIG_TSCC_DECODER) += tscc.o
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
......
......@@ -150,6 +150,7 @@ void avcodec_register_all(void)
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);
......
......@@ -33,8 +33,8 @@
#define AV_STRINGIFY(s) AV_TOSTRING(s)
#define AV_TOSTRING(s) #s
#define LIBAVCODEC_VERSION_INT ((51<<16)+(46<<8)+1)
#define LIBAVCODEC_VERSION 51.46.1
#define LIBAVCODEC_VERSION_INT ((51<<16)+(47<<8)+0)
#define LIBAVCODEC_VERSION 51.47.0
#define LIBAVCODEC_BUILD LIBAVCODEC_VERSION_INT
#define LIBAVCODEC_IDENT "Lavc" AV_STRINGIFY(LIBAVCODEC_VERSION)
......@@ -168,6 +168,7 @@ enum CodecID {
CODEC_ID_TXD,
CODEC_ID_VP6A,
CODEC_ID_AMV,
CODEC_ID_VB,
/* various PCM "codecs" */
CODEC_ID_PCM_S16LE= 0x10000,
......
/*
* 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"
#include "bytestream.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];
uint8_t *stream;
} 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)
{
int start, size, i;
start = bytestream_get_byte(&c->stream);
size = (bytestream_get_byte(&c->stream) - 1) & 0xFF;
if(start + size > 255){
av_log(c->avctx, AV_LOG_ERROR, "Palette change runs beyond entry 256\n");
return;
}
for(i = start; i <= start + size; i++)
c->pal[i] = bytestream_get_be24(&c->stream);
}
static inline int check_pixel(uint8_t *buf, uint8_t *start, uint8_t *end)
{
return buf >= start && buf < end;
}
static inline int check_line(uint8_t *buf, uint8_t *start, uint8_t *end)
{
return buf >= start && (buf + 4) <= end;
}
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;
uint8_t *pstart = c->prev_frame;
uint8_t *pend = c->prev_frame + width*c->avctx->height;
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 = bytestream_get_byte(&buf);
switch(blocktypes & 0xC0){
case 0x00: //skip
for(y = 0; y < 4; y++)
if(check_line(prev + y*width, pstart, pend))
memcpy(cur + y*width, prev + y*width, 4);
else
memset(cur + y*width, 0, 4);
break;
case 0x40:
t = bytestream_get_byte(&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++)
if(check_line(prev + t + y*width, pstart, pend))
memcpy(cur + y*width, prev + t + y*width, 4);
else
memset(cur + y*width, 0, 4);
}
break;
case 0x80: // fill
t = bytestream_get_byte(&buf);
for(y = 0; y < 4; y++)
memset(cur + y*width, t, 4);
break;
case 0xC0: // pattern fill
t = bytestream_get_byte(&buf);
pattype = t >> 6;
pattern = vb_patterns[t & 0x3F];
switch(pattype){
case 0:
a = bytestream_get_byte(&buf);
b = bytestream_get_byte(&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 = bytestream_get_byte(&buf);
for(y = 0; y < 4; y++)
for(x = 0; x < 4; x++, pattern >>= 1)
if(pattern & 1 && check_pixel(prev + x + y*width, pstart, pend))
cur[x + y*width] = prev[x + y*width];
else
cur[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;
c->stream = buf;
flags = bytestream_get_le16(&c->stream);
rest -= 2;
if(flags & VB_HAS_GMC){
i = (int16_t)bytestream_get_le16(&c->stream);
j = (int16_t)bytestream_get_le16(&c->stream);
offset = i + j * avctx->width;
rest -= 4;
}
if(flags & VB_HAS_VIDEO){
size = bytestream_get_le32(&c->stream);
if(size > rest){
av_log(avctx, AV_LOG_ERROR, "Frame size is too big\n");
return -1;
}
vb_decode_framedata(c, c->stream, offset);
c->stream += size - 4;
rest -= size;
}
if(flags & VB_HAS_PALETTE){
size = bytestream_get_le32(&c->stream);
if(size > rest){
av_log(avctx, AV_LOG_ERROR, "Palette size is too big\n");
return -1;
}
vb_decode_palette(c);
rest -= size;
}
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];
}
FFSWAP(uint8_t*, c->frame, c->prev_frame);
*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->pic.reference = 1;
if(avctx->get_buffer(avctx, &c->pic) < 0){
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
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
};
......@@ -127,6 +127,7 @@ OBJS-$(CONFIG_RTSP_DEMUXER) += rtsp.o
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
......
......@@ -147,6 +147,7 @@ void av_register_all(void)
#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);
......
......@@ -21,8 +21,8 @@
#ifndef FFMPEG_AVFORMAT_H
#define FFMPEG_AVFORMAT_H
#define LIBAVFORMAT_VERSION_INT ((51<<16)+(16<<8)+1)
#define LIBAVFORMAT_VERSION 51.16.1
#define LIBAVFORMAT_VERSION_INT ((51<<16)+(17<<8)+0)
#define LIBAVFORMAT_VERSION 51.17.0
#define LIBAVFORMAT_BUILD LIBAVFORMAT_VERSION_INT
#define LIBAVFORMAT_IDENT "Lavf" AV_STRINGIFY(LIBAVFORMAT_VERSION)
......
/*
* 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 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)
{
/* check file header */
if (AV_RL32(p->buf) == TAG_SIFF)
return AVPROBE_SCORE_MAX;
else
return 0;
}
static int create_audio_stream(AVFormatContext *s, SIFFContext *c)
{
AVStream *ast;
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 = c->bits;
ast->codec->sample_rate = c->rate;
ast->codec->frame_size = c->block_align;
av_set_pts_info(ast, 16, 1, c->rate);
return 0;
}
static int siff_parse_vbv1(AVFormatContext *s, SIFFContext *c, ByteIOContext *pb)
{
AVStream *st;
int width, height;
if (get_le32(pb) != TAG_VBHD){
av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
return -1;
}
if(get_be32(pb) != 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;
}
c->bits = get_le16(pb);
c->rate = get_le16(pb);
c->block_align = c->rate * (c->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, 16, 1, 12);
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;
}
static int siff_parse_soun(AVFormatContext *s, SIFFContext *c, ByteIOContext *pb)
{
if (get_le32(pb) != TAG_SHDR){
av_log(s, AV_LOG_ERROR, "Header chunk is missing\n");
return -1;
}
if(get_be32(pb) != 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);
c->bits = get_le16(pb);
c->block_align = c->rate * (c->bits >> 3);
return create_audio_stream(s, c);
}
static int siff_read_header(AVFormatContext *s, AVFormatParameters *ap)
{
ByteIOContext *pb = &s->pb;
SIFFContext *c = s->priv_data;
uint32_t tag;
if (get_le32(pb) != TAG_SIFF)
return -1;
url_fskip(pb, 4); //ignore size
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;
if (get_le32(pb) != MKTAG('B', 'O', 'D', 'Y')){
av_log(s, AV_LOG_ERROR, "'BODY' chunk is missing\n");
return -1;
}
url_fskip(pb, 4); //ignore size
return 0;
}
static int siff_read_packet(AVFormatContext *s, AVPacket *pkt)
{
SIFFContext *c = s->priv_data;
int size, size2;
uint8_t *snddata;
if (c->has_video){
if (c->cur_frame >= c->frames)
return AVERROR(EIO);
if (c->curstrm == -1){
c->pktsize = get_le32(&s->pb) - 4;
c->flags = get_le16(&s->pb);
c->gmcsize = (c->flags & VB_HAS_GMC) ? 4 : 0;
if (c->gmcsize)
get_buffer(&s->pb, c->gmc, c->gmcsize);
c->sndsize = (c->flags & VB_HAS_AUDIO) ? get_le32(&s->pb): 0;
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);
get_buffer(&s->pb, pkt->data + 2 + c->gmcsize, size - c->gmcsize - 2);
pkt->stream_index = 0;
c->curstrm = -1;
}else{
if (av_get_packet(&s->pb, pkt, c->sndsize - 4) < 0)
return AVERROR(EIO);
pkt->stream_index = 1;
c->curstrm = 0;
}
if(!c->cur_frame || c->curstrm)
pkt->flags |= PKT_FLAG_KEY;
if (c->curstrm == -1)
c->cur_frame++;
}else{
size = av_get_packet(&s->pb, pkt, c->block_align);
if(size <= 0)
return AVERROR(EIO);
}
return pkt->size;
}
AVInputFormat siff_demuxer = {
"siff",
"Beam Software SIFF",
sizeof(SIFFContext),
siff_probe,
siff_read_header,
siff_read_packet,
.extensions = "vb,son"
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment