2007-04-07 23:51:58 +03:00
|
|
|
/*
|
|
|
|
* Bethesda VID video decoder
|
|
|
|
* Copyright (C) 2007 Nicholas Tung
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* This file is part of Libav.
|
2007-04-07 23:51:58 +03:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2007-04-07 23:51:58 +03: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 19:35:10 +02:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2007-04-07 23:51:58 +03: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 19:35:10 +02:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2007-04-07 23:51:58 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2007-04-07 23:51:58 +03:00
|
|
|
* @brief Bethesda Softworks VID Video Decoder
|
|
|
|
* @author Nicholas Tung [ntung (at. ntung com] (2007-03)
|
2011-07-15 01:45:23 +03:00
|
|
|
* @see http://wiki.multimedia.cx/index.php?title=Bethsoft_VID
|
|
|
|
* @see http://www.svatopluk.com/andux/docs/dfvid.html
|
2007-04-07 23:51:58 +03:00
|
|
|
*/
|
|
|
|
|
2008-05-09 14:56:36 +03:00
|
|
|
#include "libavutil/common.h"
|
2013-02-23 22:10:38 +03:00
|
|
|
#include "avcodec.h"
|
2007-04-07 23:51:58 +03:00
|
|
|
#include "bethsoftvideo.h"
|
|
|
|
#include "bytestream.h"
|
2012-11-21 23:34:46 +03:00
|
|
|
#include "internal.h"
|
2007-04-07 23:51:58 +03:00
|
|
|
|
|
|
|
typedef struct BethsoftvidContext {
|
|
|
|
AVFrame frame;
|
2012-01-10 21:08:03 +03:00
|
|
|
GetByteContext g;
|
2007-04-07 23:51:58 +03:00
|
|
|
} BethsoftvidContext;
|
|
|
|
|
2008-03-21 05:11:20 +02:00
|
|
|
static av_cold int bethsoftvid_decode_init(AVCodecContext *avctx)
|
2007-04-07 23:51:58 +03:00
|
|
|
{
|
|
|
|
BethsoftvidContext *vid = avctx->priv_data;
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
2013-02-13 10:50:04 +03:00
|
|
|
avcodec_get_frame_defaults(&vid->frame);
|
2007-04-07 23:51:58 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-10 21:08:03 +03:00
|
|
|
static int set_palette(BethsoftvidContext *ctx)
|
2007-04-07 23:51:58 +03:00
|
|
|
{
|
2012-01-10 21:08:03 +03:00
|
|
|
uint32_t *palette = (uint32_t *)ctx->frame.data[1];
|
2007-04-07 23:51:58 +03:00
|
|
|
int a;
|
2011-11-21 23:22:44 +03:00
|
|
|
|
2012-01-10 21:08:03 +03:00
|
|
|
if (bytestream2_get_bytes_left(&ctx->g) < 256*3)
|
2011-11-21 23:22:44 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2007-04-08 02:16:05 +03:00
|
|
|
for(a = 0; a < 256; a++){
|
2012-01-10 21:08:03 +03:00
|
|
|
palette[a] = bytestream2_get_be24u(&ctx->g) * 4;
|
2007-04-07 23:51:58 +03:00
|
|
|
}
|
2012-01-10 21:08:03 +03:00
|
|
|
ctx->frame.palette_has_changed = 1;
|
2012-01-18 21:09:43 +03:00
|
|
|
return 0;
|
2007-04-07 23:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int bethsoftvid_decode_frame(AVCodecContext *avctx,
|
2012-11-13 21:35:22 +03:00
|
|
|
void *data, int *got_frame,
|
2009-04-07 18:59:50 +03:00
|
|
|
AVPacket *avpkt)
|
2007-04-07 23:51:58 +03:00
|
|
|
{
|
|
|
|
BethsoftvidContext * vid = avctx->priv_data;
|
|
|
|
char block_type;
|
2007-04-08 02:30:47 +03:00
|
|
|
uint8_t * dst;
|
2007-04-07 23:51:58 +03:00
|
|
|
uint8_t * frame_end;
|
2007-04-08 02:30:47 +03:00
|
|
|
int remaining = avctx->width; // number of bytes remaining on a line
|
2012-01-18 23:45:47 +03:00
|
|
|
int wrap_to_next_line;
|
|
|
|
int code, ret;
|
2007-04-07 23:51:58 +03:00
|
|
|
int yoffset;
|
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
if ((ret = ff_reget_buffer(avctx, &vid->frame)) < 0) {
|
2007-04-07 23:51:58 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
|
2012-11-14 18:09:39 +03:00
|
|
|
return ret;
|
2007-04-07 23:51:58 +03:00
|
|
|
}
|
2012-01-18 23:45:47 +03:00
|
|
|
wrap_to_next_line = vid->frame.linesize[0] - avctx->width;
|
|
|
|
|
|
|
|
if (avpkt->side_data_elems > 0 &&
|
|
|
|
avpkt->side_data[0].type == AV_PKT_DATA_PALETTE) {
|
|
|
|
bytestream2_init(&vid->g, avpkt->side_data[0].data,
|
|
|
|
avpkt->side_data[0].size);
|
|
|
|
if ((ret = set_palette(vid)) < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2012-01-10 21:08:03 +03:00
|
|
|
|
|
|
|
bytestream2_init(&vid->g, avpkt->data, avpkt->size);
|
2007-04-08 02:30:47 +03:00
|
|
|
dst = vid->frame.data[0];
|
2007-04-07 23:51:58 +03:00
|
|
|
frame_end = vid->frame.data[0] + vid->frame.linesize[0] * avctx->height;
|
|
|
|
|
2012-01-10 21:08:03 +03:00
|
|
|
switch(block_type = bytestream2_get_byte(&vid->g)){
|
|
|
|
case PALETTE_BLOCK: {
|
2012-11-13 21:35:22 +03:00
|
|
|
*got_frame = 0;
|
2012-01-18 21:09:43 +03:00
|
|
|
if ((ret = set_palette(vid)) < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "error reading palette\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return bytestream2_tell(&vid->g);
|
2012-01-10 21:08:03 +03:00
|
|
|
}
|
2007-04-08 02:10:22 +03:00
|
|
|
case VIDEO_YOFF_P_FRAME:
|
2012-01-10 21:08:03 +03:00
|
|
|
yoffset = bytestream2_get_le16(&vid->g);
|
2007-04-08 02:16:05 +03:00
|
|
|
if(yoffset >= avctx->height)
|
2012-11-14 18:09:39 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-04-08 02:30:47 +03:00
|
|
|
dst += vid->frame.linesize[0] * yoffset;
|
2007-04-07 23:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// main code
|
2012-01-10 21:08:03 +03:00
|
|
|
while((code = bytestream2_get_byte(&vid->g))){
|
2007-04-08 02:30:47 +03:00
|
|
|
int length = code & 0x7f;
|
2007-04-07 23:51:58 +03:00
|
|
|
|
|
|
|
// copy any bytes starting at the current position, and ending at the frame width
|
2007-04-08 02:30:47 +03:00
|
|
|
while(length > remaining){
|
|
|
|
if(code < 0x80)
|
2012-01-10 21:08:03 +03:00
|
|
|
bytestream2_get_buffer(&vid->g, dst, remaining);
|
2007-04-08 02:16:05 +03:00
|
|
|
else if(block_type == VIDEO_I_FRAME)
|
2012-01-10 21:08:03 +03:00
|
|
|
memset(dst, bytestream2_peek_byte(&vid->g), remaining);
|
2007-04-08 02:30:47 +03:00
|
|
|
length -= remaining; // decrement the number of bytes to be copied
|
|
|
|
dst += remaining + wrap_to_next_line; // skip over extra bytes at end of frame
|
|
|
|
remaining = avctx->width;
|
|
|
|
if(dst == frame_end)
|
2007-04-08 02:16:05 +03:00
|
|
|
goto end;
|
2007-04-07 23:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy any remaining bytes after / if line overflows
|
2007-04-08 02:30:47 +03:00
|
|
|
if(code < 0x80)
|
2012-01-10 21:08:03 +03:00
|
|
|
bytestream2_get_buffer(&vid->g, dst, length);
|
2007-04-08 02:16:05 +03:00
|
|
|
else if(block_type == VIDEO_I_FRAME)
|
2012-01-10 21:08:03 +03:00
|
|
|
memset(dst, bytestream2_get_byte(&vid->g), length);
|
2007-04-08 02:30:47 +03:00
|
|
|
remaining -= length;
|
|
|
|
dst += length;
|
2007-04-07 23:51:58 +03:00
|
|
|
}
|
|
|
|
end:
|
|
|
|
|
2012-11-21 23:34:46 +03:00
|
|
|
if ((ret = av_frame_ref(data, &vid->frame)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2012-11-13 21:35:22 +03:00
|
|
|
*got_frame = 1;
|
2007-04-07 23:51:58 +03:00
|
|
|
|
2012-01-10 21:08:03 +03:00
|
|
|
return avpkt->size;
|
2007-04-07 23:51:58 +03:00
|
|
|
}
|
|
|
|
|
2008-03-21 05:11:20 +02:00
|
|
|
static av_cold int bethsoftvid_decode_end(AVCodecContext *avctx)
|
2007-04-07 23:51:58 +03:00
|
|
|
{
|
|
|
|
BethsoftvidContext * vid = avctx->priv_data;
|
2012-11-21 23:34:46 +03:00
|
|
|
av_frame_unref(&vid->frame);
|
2007-04-07 23:51:58 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 23:40:11 +02:00
|
|
|
AVCodec ff_bethsoftvid_decoder = {
|
2012-04-06 19:19:39 +03:00
|
|
|
.name = "bethsoftvid",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 12:11:04 +03:00
|
|
|
.id = AV_CODEC_ID_BETHSOFTVID,
|
2007-04-07 23:51:58 +03:00
|
|
|
.priv_data_size = sizeof(BethsoftvidContext),
|
2012-04-06 19:19:39 +03:00
|
|
|
.init = bethsoftvid_decode_init,
|
|
|
|
.close = bethsoftvid_decode_end,
|
|
|
|
.decode = bethsoftvid_decode_frame,
|
|
|
|
.capabilities = CODEC_CAP_DR1,
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Bethesda VID video"),
|
2007-04-07 23:51:58 +03:00
|
|
|
};
|