2008-04-18 11:43:11 +03:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2008-04-18 11:43:11 +03:00
|
|
|
* @brief Brute Force & Ignorance (.bfi) video decoder
|
|
|
|
* @author Sisir Koppaka ( sisir.koppaka at gmail dot com )
|
2011-07-15 01:45:23 +03:00
|
|
|
* @see http://wiki.multimedia.cx/index.php?title=BFI
|
2008-04-18 11:43:11 +03:00
|
|
|
*/
|
|
|
|
|
2008-05-09 14:56:36 +03:00
|
|
|
#include "libavutil/common.h"
|
2008-04-18 11:43:11 +03:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "bytestream.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2022-08-24 21:28:16 +02:00
|
|
|
#include "decode.h"
|
2008-04-18 11:43:11 +03:00
|
|
|
|
|
|
|
typedef struct BFIContext {
|
|
|
|
AVCodecContext *avctx;
|
|
|
|
uint8_t *dst;
|
2011-04-25 21:26:15 +03:00
|
|
|
uint32_t pal[256];
|
2008-04-18 11:43:11 +03:00
|
|
|
} BFIContext;
|
|
|
|
|
2012-01-01 19:37:54 +03:00
|
|
|
static av_cold int bfi_decode_init(AVCodecContext *avctx)
|
2008-04-18 11:43:11 +03:00
|
|
|
{
|
|
|
|
BFIContext *bfi = avctx->priv_data;
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_PAL8;
|
2012-04-01 11:47:39 +03:00
|
|
|
bfi->dst = av_mallocz(avctx->width * avctx->height);
|
2013-09-14 14:54:10 +03:00
|
|
|
if (!bfi->dst)
|
|
|
|
return AVERROR(ENOMEM);
|
2008-04-18 11:43:11 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-30 21:33:24 +02:00
|
|
|
static int bfi_decode_frame(AVCodecContext *avctx, AVFrame *frame,
|
2012-11-13 21:35:22 +03:00
|
|
|
int *got_frame, AVPacket *avpkt)
|
2008-04-18 11:43:11 +03:00
|
|
|
{
|
2012-01-08 22:57:58 +03:00
|
|
|
GetByteContext g;
|
2012-04-01 11:47:39 +03:00
|
|
|
int buf_size = avpkt->size;
|
2008-04-18 11:43:11 +03:00
|
|
|
BFIContext *bfi = avctx->priv_data;
|
2012-04-01 11:47:39 +03:00
|
|
|
uint8_t *dst = bfi->dst;
|
2008-04-18 11:43:11 +03:00
|
|
|
uint8_t *src, *dst_offset, colour1, colour2;
|
|
|
|
uint8_t *frame_end = bfi->dst + avctx->width * avctx->height;
|
|
|
|
uint32_t *pal;
|
2012-11-14 18:09:39 +03:00
|
|
|
int i, j, ret, height = avctx->height;
|
2008-04-18 11:43:11 +03:00
|
|
|
|
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 10:41:53 +03:00
|
|
|
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
|
2012-11-14 18:09:39 +03:00
|
|
|
return ret;
|
2008-04-18 11:43:11 +03:00
|
|
|
|
2012-01-08 22:57:58 +03:00
|
|
|
bytestream2_init(&g, avpkt->data, buf_size);
|
|
|
|
|
2008-04-18 11:43:11 +03:00
|
|
|
/* Set frame parameters and palette, if necessary */
|
2023-01-24 01:35:54 +02:00
|
|
|
if (!avctx->frame_num) {
|
2012-11-21 23:34:46 +03:00
|
|
|
frame->pict_type = AV_PICTURE_TYPE_I;
|
2023-04-12 18:58:54 +02:00
|
|
|
frame->flags |= AV_FRAME_FLAG_KEY;
|
2008-04-18 11:43:11 +03:00
|
|
|
/* Setting the palette */
|
2012-01-01 19:37:54 +03:00
|
|
|
if (avctx->extradata_size > 768) {
|
2018-12-31 22:32:37 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Palette is too large.\n");
|
2012-11-14 18:09:39 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2008-04-18 11:43:11 +03:00
|
|
|
}
|
2012-11-21 23:34:46 +03:00
|
|
|
pal = (uint32_t *)frame->data[1];
|
2008-04-18 11:43:11 +03:00
|
|
|
for (i = 0; i < avctx->extradata_size / 3; i++) {
|
|
|
|
int shift = 16;
|
2012-10-14 04:17:13 +03:00
|
|
|
*pal = 0xFFU << 24;
|
2008-04-18 11:43:11 +03:00
|
|
|
for (j = 0; j < 3; j++, shift -= 8)
|
2012-04-01 11:47:39 +03:00
|
|
|
*pal += ((avctx->extradata[i * 3 + j] << 2) |
|
|
|
|
(avctx->extradata[i * 3 + j] >> 4)) << shift;
|
2008-04-18 11:43:11 +03:00
|
|
|
pal++;
|
|
|
|
}
|
2013-03-12 05:20:18 +03:00
|
|
|
memcpy(bfi->pal, frame->data[1], sizeof(bfi->pal));
|
2023-05-18 03:39:57 +02:00
|
|
|
#if FF_API_PALETTE_HAS_CHANGED
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2012-11-21 23:34:46 +03:00
|
|
|
frame->palette_has_changed = 1;
|
2023-05-18 03:39:57 +02:00
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
2008-04-18 11:43:11 +03:00
|
|
|
} else {
|
2012-11-21 23:34:46 +03:00
|
|
|
frame->pict_type = AV_PICTURE_TYPE_P;
|
2023-04-12 18:58:54 +02:00
|
|
|
frame->flags &= ~AV_FRAME_FLAG_KEY;
|
2023-05-18 03:39:57 +02:00
|
|
|
#if FF_API_PALETTE_HAS_CHANGED
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2013-03-12 05:20:18 +03:00
|
|
|
frame->palette_has_changed = 0;
|
2023-05-18 03:39:57 +02:00
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
2013-03-12 05:20:18 +03:00
|
|
|
memcpy(frame->data[1], bfi->pal, sizeof(bfi->pal));
|
2008-04-18 11:43:11 +03:00
|
|
|
}
|
|
|
|
|
2012-01-08 22:57:58 +03:00
|
|
|
bytestream2_skip(&g, 4); // Unpacked size, not required.
|
2008-04-18 11:43:11 +03:00
|
|
|
|
|
|
|
while (dst != frame_end) {
|
2012-01-01 19:37:54 +03:00
|
|
|
static const uint8_t lentab[4] = { 0, 2, 0, 1 };
|
2012-01-08 22:57:58 +03:00
|
|
|
unsigned int byte = bytestream2_get_byte(&g), av_uninit(offset);
|
2012-01-01 19:37:54 +03:00
|
|
|
unsigned int code = byte >> 6;
|
2008-04-18 11:43:11 +03:00
|
|
|
unsigned int length = byte & ~0xC0;
|
|
|
|
|
2012-01-08 22:57:58 +03:00
|
|
|
if (!bytestream2_get_bytes_left(&g)) {
|
2012-01-01 19:37:54 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Input resolution larger than actual frame.\n");
|
2012-11-14 18:09:39 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-01-07 01:14:27 +02:00
|
|
|
}
|
|
|
|
|
2012-04-01 11:47:39 +03:00
|
|
|
/* Get length and offset (if required) */
|
2008-04-18 11:43:11 +03:00
|
|
|
if (length == 0) {
|
|
|
|
if (code == 1) {
|
2012-01-08 22:57:58 +03:00
|
|
|
length = bytestream2_get_byte(&g);
|
|
|
|
offset = bytestream2_get_le16(&g);
|
2008-04-18 11:43:11 +03:00
|
|
|
} else {
|
2012-01-08 22:57:58 +03:00
|
|
|
length = bytestream2_get_le16(&g);
|
2008-04-18 11:43:11 +03:00
|
|
|
if (code == 2 && length == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (code == 1)
|
2012-01-08 22:57:58 +03:00
|
|
|
offset = bytestream2_get_byte(&g);
|
2008-04-18 11:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Do boundary check */
|
2012-01-01 19:37:54 +03:00
|
|
|
if (dst + (length << lentab[code]) > frame_end)
|
2008-04-18 11:43:11 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
switch (code) {
|
2012-04-01 11:47:39 +03:00
|
|
|
case 0: // normal chain
|
2012-01-08 22:57:58 +03:00
|
|
|
if (length >= bytestream2_get_bytes_left(&g)) {
|
2011-01-07 01:14:27 +02:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Frame larger than buffer.\n");
|
2012-11-14 18:09:39 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2011-01-07 01:14:27 +02:00
|
|
|
}
|
2012-01-08 22:57:58 +03:00
|
|
|
bytestream2_get_buffer(&g, dst, length);
|
2008-04-18 11:43:11 +03:00
|
|
|
dst += length;
|
|
|
|
break;
|
2012-04-01 11:47:39 +03:00
|
|
|
case 1: // back chain
|
2008-04-18 11:43:11 +03:00
|
|
|
dst_offset = dst - offset;
|
2012-04-01 11:47:39 +03:00
|
|
|
length *= 4; // Convert dwords to bytes.
|
2008-04-18 11:43:11 +03:00
|
|
|
if (dst_offset < bfi->dst)
|
|
|
|
break;
|
|
|
|
while (length--)
|
|
|
|
*dst++ = *dst_offset++;
|
|
|
|
break;
|
2012-04-01 11:47:39 +03:00
|
|
|
case 2: // skip chain
|
2008-04-18 11:43:11 +03:00
|
|
|
dst += length;
|
|
|
|
break;
|
2012-04-01 11:47:39 +03:00
|
|
|
case 3: // fill chain
|
2012-01-08 22:57:58 +03:00
|
|
|
colour1 = bytestream2_get_byte(&g);
|
|
|
|
colour2 = bytestream2_get_byte(&g);
|
2008-04-18 11:43:11 +03:00
|
|
|
while (length--) {
|
|
|
|
*dst++ = colour1;
|
|
|
|
*dst++ = colour2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
src = bfi->dst;
|
2012-11-21 23:34:46 +03:00
|
|
|
dst = frame->data[0];
|
2008-04-18 11:43:11 +03:00
|
|
|
while (height--) {
|
|
|
|
memcpy(dst, src, avctx->width);
|
|
|
|
src += avctx->width;
|
2012-11-21 23:34:46 +03:00
|
|
|
dst += frame->linesize[0];
|
2008-04-18 11:43:11 +03:00
|
|
|
}
|
2012-11-13 21:35:22 +03:00
|
|
|
*got_frame = 1;
|
2012-11-21 23:34:46 +03:00
|
|
|
|
2008-04-18 11:43:11 +03:00
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
|
2012-04-01 11:47:39 +03:00
|
|
|
static av_cold int bfi_decode_close(AVCodecContext *avctx)
|
2008-04-18 11:43:11 +03:00
|
|
|
{
|
|
|
|
BFIContext *bfi = avctx->priv_data;
|
2014-10-08 17:08:28 +03:00
|
|
|
av_freep(&bfi->dst);
|
2008-04-18 11:43:11 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_bfi_decoder = {
|
|
|
|
.p.name = "bfi",
|
2022-08-29 13:38:02 +02:00
|
|
|
CODEC_LONG_NAME("Brute Force & Ignorance"),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_BFI,
|
2008-04-18 11:43:11 +03:00
|
|
|
.priv_data_size = sizeof(BFIContext),
|
2012-04-06 19:19:39 +03:00
|
|
|
.init = bfi_decode_init,
|
|
|
|
.close = bfi_decode_close,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_DECODE_CB(bfi_decode_frame),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1,
|
2008-04-18 11:43:11 +03:00
|
|
|
};
|