2003d3da |
/*
* Brute Force & Ignorance (BFI) video decoder
* Copyright (c) 2008 Sisir Koppaka
*
* 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
*/
/** |
ba87f080 |
* @file |
2003d3da |
* @brief Brute Force & Ignorance (.bfi) video decoder
* @author Sisir Koppaka ( sisir.koppaka at gmail dot com ) |
bee6d2fd |
* @see http://wiki.multimedia.cx/index.php?title=BFI |
2003d3da |
*/
|
245976da |
#include "libavutil/common.h" |
2003d3da |
#include "avcodec.h"
#include "bytestream.h" |
594d4d5d |
#include "internal.h" |
2003d3da |
typedef struct BFIContext {
AVCodecContext *avctx;
uint8_t *dst; |
ff558d7b |
uint32_t pal[256]; |
2003d3da |
} BFIContext;
|
f7e30cc1 |
static av_cold int bfi_decode_init(AVCodecContext *avctx) |
2003d3da |
{
BFIContext *bfi = avctx->priv_data; |
716d413c |
avctx->pix_fmt = AV_PIX_FMT_PAL8; |
2ef15b46 |
bfi->dst = av_mallocz(avctx->width * avctx->height); |
1a3d142f |
if (!bfi->dst)
return AVERROR(ENOMEM); |
2003d3da |
return 0;
}
|
f7e30cc1 |
static int bfi_decode_frame(AVCodecContext *avctx, void *data, |
df9b9567 |
int *got_frame, AVPacket *avpkt) |
2003d3da |
{ |
759001c5 |
AVFrame *frame = data; |
ccc27e21 |
GetByteContext g; |
2ef15b46 |
int buf_size = avpkt->size; |
2003d3da |
BFIContext *bfi = avctx->priv_data; |
2ef15b46 |
uint8_t *dst = bfi->dst; |
2003d3da |
uint8_t *src, *dst_offset, colour1, colour2;
uint8_t *frame_end = bfi->dst + avctx->width * avctx->height;
uint32_t *pal; |
86e09b9e |
int i, j, ret, height = avctx->height; |
2003d3da |
|
1ec94b0f |
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
86e09b9e |
return ret; |
2003d3da |
|
ccc27e21 |
bytestream2_init(&g, avpkt->data, buf_size);
|
2003d3da |
/* Set frame parameters and palette, if necessary */
if (!avctx->frame_number) { |
759001c5 |
frame->pict_type = AV_PICTURE_TYPE_I;
frame->key_frame = 1; |
2003d3da |
/* Setting the palette */ |
f7e30cc1 |
if (avctx->extradata_size > 768) { |
2003d3da |
av_log(NULL, AV_LOG_ERROR, "Palette is too large.\n"); |
86e09b9e |
return AVERROR_INVALIDDATA; |
2003d3da |
} |
759001c5 |
pal = (uint32_t *)frame->data[1]; |
2003d3da |
for (i = 0; i < avctx->extradata_size / 3; i++) {
int shift = 16; |
b12d92ef |
*pal = 0xFFU << 24; |
2003d3da |
for (j = 0; j < 3; j++, shift -= 8) |
2ef15b46 |
*pal += ((avctx->extradata[i * 3 + j] << 2) |
(avctx->extradata[i * 3 + j] >> 4)) << shift; |
2003d3da |
pal++;
} |
80e9e63c |
memcpy(bfi->pal, frame->data[1], sizeof(bfi->pal)); |
759001c5 |
frame->palette_has_changed = 1; |
2003d3da |
} else { |
759001c5 |
frame->pict_type = AV_PICTURE_TYPE_P;
frame->key_frame = 0; |
80e9e63c |
frame->palette_has_changed = 0;
memcpy(frame->data[1], bfi->pal, sizeof(bfi->pal)); |
2003d3da |
}
|
ccc27e21 |
bytestream2_skip(&g, 4); // Unpacked size, not required. |
2003d3da |
while (dst != frame_end) { |
f7e30cc1 |
static const uint8_t lentab[4] = { 0, 2, 0, 1 }; |
ccc27e21 |
unsigned int byte = bytestream2_get_byte(&g), av_uninit(offset); |
f7e30cc1 |
unsigned int code = byte >> 6; |
2003d3da |
unsigned int length = byte & ~0xC0;
|
ccc27e21 |
if (!bytestream2_get_bytes_left(&g)) { |
f7e30cc1 |
av_log(avctx, AV_LOG_ERROR,
"Input resolution larger than actual frame.\n"); |
86e09b9e |
return AVERROR_INVALIDDATA; |
65cd45a8 |
}
|
2ef15b46 |
/* Get length and offset (if required) */ |
2003d3da |
if (length == 0) {
if (code == 1) { |
ccc27e21 |
length = bytestream2_get_byte(&g);
offset = bytestream2_get_le16(&g); |
2003d3da |
} else { |
ccc27e21 |
length = bytestream2_get_le16(&g); |
2003d3da |
if (code == 2 && length == 0)
break;
}
} else {
if (code == 1) |
ccc27e21 |
offset = bytestream2_get_byte(&g); |
2003d3da |
}
/* Do boundary check */ |
f7e30cc1 |
if (dst + (length << lentab[code]) > frame_end) |
2003d3da |
break;
switch (code) { |
2ef15b46 |
case 0: // normal chain |
ccc27e21 |
if (length >= bytestream2_get_bytes_left(&g)) { |
65cd45a8 |
av_log(avctx, AV_LOG_ERROR, "Frame larger than buffer.\n"); |
86e09b9e |
return AVERROR_INVALIDDATA; |
65cd45a8 |
} |
ccc27e21 |
bytestream2_get_buffer(&g, dst, length); |
2003d3da |
dst += length;
break; |
2ef15b46 |
case 1: // back chain |
2003d3da |
dst_offset = dst - offset; |
2ef15b46 |
length *= 4; // Convert dwords to bytes. |
2003d3da |
if (dst_offset < bfi->dst)
break;
while (length--)
*dst++ = *dst_offset++;
break; |
2ef15b46 |
case 2: // skip chain |
2003d3da |
dst += length;
break; |
2ef15b46 |
case 3: // fill chain |
ccc27e21 |
colour1 = bytestream2_get_byte(&g);
colour2 = bytestream2_get_byte(&g); |
2003d3da |
while (length--) {
*dst++ = colour1;
*dst++ = colour2;
}
break;
}
}
src = bfi->dst; |
759001c5 |
dst = frame->data[0]; |
2003d3da |
while (height--) {
memcpy(dst, src, avctx->width);
src += avctx->width; |
759001c5 |
dst += frame->linesize[0]; |
2003d3da |
} |
df9b9567 |
*got_frame = 1; |
759001c5 |
|
2003d3da |
return buf_size;
}
|
2ef15b46 |
static av_cold int bfi_decode_close(AVCodecContext *avctx) |
2003d3da |
{
BFIContext *bfi = avctx->priv_data;
av_free(bfi->dst);
return 0;
}
|
e7e2df27 |
AVCodec ff_bfi_decoder = { |
00c3b67b |
.name = "bfi", |
b2bed932 |
.long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"), |
00c3b67b |
.type = AVMEDIA_TYPE_VIDEO, |
36ef5369 |
.id = AV_CODEC_ID_BFI, |
2003d3da |
.priv_data_size = sizeof(BFIContext), |
00c3b67b |
.init = bfi_decode_init,
.close = bfi_decode_close,
.decode = bfi_decode_frame,
.capabilities = CODEC_CAP_DR1, |
2003d3da |
}; |