2007-04-07 22:51:58 +02:00
|
|
|
/*
|
|
|
|
* Bethesda VID video decoder
|
|
|
|
* Copyright (C) 2007 Nicholas Tung
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 16:45:34 +02:00
|
|
|
* @file
|
2007-04-07 22:51:58 +02:00
|
|
|
* @brief Bethesda Softworks VID Video Decoder
|
|
|
|
* @author Nicholas Tung [ntung (at. ntung com] (2007-03)
|
2011-07-15 00:45:23 +02:00
|
|
|
* @see http://wiki.multimedia.cx/index.php?title=Bethsoft_VID
|
|
|
|
* @see http://www.svatopluk.com/andux/docs/dfvid.html
|
2007-04-07 22:51:58 +02:00
|
|
|
*/
|
|
|
|
|
2008-05-09 13:56:36 +02:00
|
|
|
#include "libavutil/common.h"
|
2013-02-23 20:10:38 +01:00
|
|
|
#include "avcodec.h"
|
2007-04-07 22:51:58 +02:00
|
|
|
#include "bethsoftvideo.h"
|
|
|
|
#include "bytestream.h"
|
2012-11-21 21:34:46 +01:00
|
|
|
#include "internal.h"
|
2007-04-07 22:51:58 +02:00
|
|
|
|
|
|
|
typedef struct BethsoftvidContext {
|
|
|
|
AVFrame frame;
|
2012-01-10 19:08:03 +01:00
|
|
|
GetByteContext g;
|
2007-04-07 22:51:58 +02:00
|
|
|
} BethsoftvidContext;
|
|
|
|
|
2008-03-21 04:11:20 +01:00
|
|
|
static av_cold int bethsoftvid_decode_init(AVCodecContext *avctx)
|
2007-04-07 22:51:58 +02:00
|
|
|
{
|
|
|
|
BethsoftvidContext *vid = avctx->priv_data;
|
2011-05-02 02:15:15 +02:00
|
|
|
avcodec_get_frame_defaults(&vid->frame);
|
2012-10-06 12:10:34 +02:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
2007-04-07 22:51:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-10 19:08:03 +01:00
|
|
|
static int set_palette(BethsoftvidContext *ctx)
|
2007-04-07 22:51:58 +02:00
|
|
|
{
|
2012-01-10 19:08:03 +01:00
|
|
|
uint32_t *palette = (uint32_t *)ctx->frame.data[1];
|
2007-04-07 22:51:58 +02:00
|
|
|
int a;
|
2011-11-21 21:22:44 +01:00
|
|
|
|
2012-01-10 19:08:03 +01:00
|
|
|
if (bytestream2_get_bytes_left(&ctx->g) < 256*3)
|
2011-11-21 21:22:44 +01:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2007-04-08 01:16:05 +02:00
|
|
|
for(a = 0; a < 256; a++){
|
2012-01-11 02:23:55 +01:00
|
|
|
palette[a] = 0xFFU << 24 | bytestream2_get_be24u(&ctx->g) * 4;
|
2011-11-13 14:40:32 +01:00
|
|
|
palette[a] |= palette[a] >> 6 & 0x30303;
|
2007-04-07 22:51:58 +02:00
|
|
|
}
|
2012-01-10 19:08:03 +01:00
|
|
|
ctx->frame.palette_has_changed = 1;
|
2012-01-18 19:09:43 +01:00
|
|
|
return 0;
|
2007-04-07 22:51:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int bethsoftvid_decode_frame(AVCodecContext *avctx,
|
2012-11-13 19:35:22 +01:00
|
|
|
void *data, int *got_frame,
|
2009-04-07 17:59:50 +02:00
|
|
|
AVPacket *avpkt)
|
2007-04-07 22:51:58 +02:00
|
|
|
{
|
|
|
|
BethsoftvidContext * vid = avctx->priv_data;
|
|
|
|
char block_type;
|
2007-04-08 01:30:47 +02:00
|
|
|
uint8_t * dst;
|
2007-04-07 22:51:58 +02:00
|
|
|
uint8_t * frame_end;
|
2007-04-08 01:30:47 +02:00
|
|
|
int remaining = avctx->width; // number of bytes remaining on a line
|
2012-01-18 21:45:47 +01:00
|
|
|
int wrap_to_next_line;
|
|
|
|
int code, ret;
|
2007-04-07 22:51:58 +02:00
|
|
|
int yoffset;
|
|
|
|
|
lavc: factorize ff_{thread_,re,}get_buffer error messages.
Coccinelle profile used:
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_get_buffer(ctx, f, flags)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str;
@@
-if ((r = ff_reget_buffer(ctx, f)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_reget_buffer(ctx, f)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0)
+ return r;
...along with some manual patches for the remaining ones.
2013-03-12 08:41:53 +01:00
|
|
|
if ((ret = ff_reget_buffer(avctx, &vid->frame)) < 0)
|
2012-10-13 10:05:42 +02:00
|
|
|
return ret;
|
2012-01-18 21:45:47 +01: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 19:08:03 +01:00
|
|
|
|
|
|
|
bytestream2_init(&vid->g, avpkt->data, avpkt->size);
|
2007-04-08 01:30:47 +02:00
|
|
|
dst = vid->frame.data[0];
|
2007-04-07 22:51:58 +02:00
|
|
|
frame_end = vid->frame.data[0] + vid->frame.linesize[0] * avctx->height;
|
|
|
|
|
2012-01-10 19:08:03 +01:00
|
|
|
switch(block_type = bytestream2_get_byte(&vid->g)){
|
|
|
|
case PALETTE_BLOCK: {
|
2012-11-13 19:35:22 +01:00
|
|
|
*got_frame = 0;
|
2012-01-18 19:09:43 +01: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 19:08:03 +01:00
|
|
|
}
|
2007-04-08 01:10:22 +02:00
|
|
|
case VIDEO_YOFF_P_FRAME:
|
2012-01-10 19:08:03 +01:00
|
|
|
yoffset = bytestream2_get_le16(&vid->g);
|
2007-04-08 01:16:05 +02:00
|
|
|
if(yoffset >= avctx->height)
|
2012-10-13 10:05:42 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2007-04-08 01:30:47 +02:00
|
|
|
dst += vid->frame.linesize[0] * yoffset;
|
2007-04-07 22:51:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// main code
|
2012-01-10 19:08:03 +01:00
|
|
|
while((code = bytestream2_get_byte(&vid->g))){
|
2007-04-08 01:30:47 +02:00
|
|
|
int length = code & 0x7f;
|
2007-04-07 22:51:58 +02:00
|
|
|
|
|
|
|
// copy any bytes starting at the current position, and ending at the frame width
|
2007-04-08 01:30:47 +02:00
|
|
|
while(length > remaining){
|
|
|
|
if(code < 0x80)
|
2012-01-10 19:08:03 +01:00
|
|
|
bytestream2_get_buffer(&vid->g, dst, remaining);
|
2007-04-08 01:16:05 +02:00
|
|
|
else if(block_type == VIDEO_I_FRAME)
|
2012-01-10 19:08:03 +01:00
|
|
|
memset(dst, bytestream2_peek_byte(&vid->g), remaining);
|
2007-04-08 01:30:47 +02: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 01:16:05 +02:00
|
|
|
goto end;
|
2007-04-07 22:51:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy any remaining bytes after / if line overflows
|
2007-04-08 01:30:47 +02:00
|
|
|
if(code < 0x80)
|
2012-01-10 19:08:03 +01:00
|
|
|
bytestream2_get_buffer(&vid->g, dst, length);
|
2007-04-08 01:16:05 +02:00
|
|
|
else if(block_type == VIDEO_I_FRAME)
|
2012-01-10 19:08:03 +01:00
|
|
|
memset(dst, bytestream2_get_byte(&vid->g), length);
|
2007-04-08 01:30:47 +02:00
|
|
|
remaining -= length;
|
|
|
|
dst += length;
|
2007-04-07 22:51:58 +02:00
|
|
|
}
|
|
|
|
end:
|
|
|
|
|
2012-11-21 21:34:46 +01:00
|
|
|
if ((ret = av_frame_ref(data, &vid->frame)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2012-11-13 19:35:22 +01:00
|
|
|
*got_frame = 1;
|
2007-04-07 22:51:58 +02:00
|
|
|
|
2012-01-10 19:08:03 +01:00
|
|
|
return avpkt->size;
|
2007-04-07 22:51:58 +02:00
|
|
|
}
|
|
|
|
|
2008-03-21 04:11:20 +01:00
|
|
|
static av_cold int bethsoftvid_decode_end(AVCodecContext *avctx)
|
2007-04-07 22:51:58 +02:00
|
|
|
{
|
|
|
|
BethsoftvidContext * vid = avctx->priv_data;
|
2012-11-21 21:34:46 +01:00
|
|
|
av_frame_unref(&vid->frame);
|
2007-04-07 22:51:58 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:40:11 +01:00
|
|
|
AVCodec ff_bethsoftvid_decoder = {
|
2012-04-06 18:19:39 +02:00
|
|
|
.name = "bethsoftvid",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 11:11:04 +02:00
|
|
|
.id = AV_CODEC_ID_BETHSOFTVID,
|
2007-04-07 22:51:58 +02:00
|
|
|
.priv_data_size = sizeof(BethsoftvidContext),
|
2012-04-06 18:19:39 +02: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 22:51:58 +02:00
|
|
|
};
|