00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00029 #include "libavutil/common.h"
00030 #include "avcodec.h"
00031 #include "bytestream.h"
00032
00033 typedef struct BFIContext {
00034 AVCodecContext *avctx;
00035 AVFrame frame;
00036 uint8_t *dst;
00037 uint32_t pal[256];
00038 } BFIContext;
00039
00040 static av_cold int bfi_decode_init(AVCodecContext *avctx)
00041 {
00042 BFIContext *bfi = avctx->priv_data;
00043 avctx->pix_fmt = PIX_FMT_PAL8;
00044 avcodec_get_frame_defaults(&bfi->frame);
00045 bfi->dst = av_mallocz(avctx->width * avctx->height);
00046 return 0;
00047 }
00048
00049 static int bfi_decode_frame(AVCodecContext *avctx, void *data,
00050 int *data_size, AVPacket *avpkt)
00051 {
00052 GetByteContext g;
00053 int buf_size = avpkt->size;
00054 BFIContext *bfi = avctx->priv_data;
00055 uint8_t *dst = bfi->dst;
00056 uint8_t *src, *dst_offset, colour1, colour2;
00057 uint8_t *frame_end = bfi->dst + avctx->width * avctx->height;
00058 uint32_t *pal;
00059 int i, j, height = avctx->height;
00060
00061 if (bfi->frame.data[0])
00062 avctx->release_buffer(avctx, &bfi->frame);
00063
00064 bfi->frame.reference = 3;
00065
00066 if (avctx->get_buffer(avctx, &bfi->frame) < 0) {
00067 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00068 return -1;
00069 }
00070
00071 bytestream2_init(&g, avpkt->data, buf_size);
00072
00073
00074 if (!avctx->frame_number) {
00075 bfi->frame.pict_type = AV_PICTURE_TYPE_I;
00076 bfi->frame.key_frame = 1;
00077
00078 if (avctx->extradata_size > 768) {
00079 av_log(NULL, AV_LOG_ERROR, "Palette is too large.\n");
00080 return -1;
00081 }
00082 pal = (uint32_t *)bfi->frame.data[1];
00083 for (i = 0; i < avctx->extradata_size / 3; i++) {
00084 int shift = 16;
00085 *pal = 0xFF << 24;
00086 for (j = 0; j < 3; j++, shift -= 8)
00087 *pal +=
00088 ((avctx->extradata[i * 3 + j] << 2) |
00089 (avctx->extradata[i * 3 + j] >> 4)) << shift;
00090 pal++;
00091 }
00092 memcpy(bfi->pal, bfi->frame.data[1], sizeof(bfi->pal));
00093 bfi->frame.palette_has_changed = 1;
00094 } else {
00095 bfi->frame.pict_type = AV_PICTURE_TYPE_P;
00096 bfi->frame.key_frame = 0;
00097 bfi->frame.palette_has_changed = 0;
00098 memcpy(bfi->frame.data[1], bfi->pal, sizeof(bfi->pal));
00099 }
00100
00101 bytestream2_skip(&g, 4);
00102
00103 while (dst != frame_end) {
00104 static const uint8_t lentab[4] = { 0, 2, 0, 1 };
00105 unsigned int byte = bytestream2_get_byte(&g), av_uninit(offset);
00106 unsigned int code = byte >> 6;
00107 unsigned int length = byte & ~0xC0;
00108
00109 if (!bytestream2_get_bytes_left(&g)) {
00110 av_log(avctx, AV_LOG_ERROR,
00111 "Input resolution larger than actual frame.\n");
00112 return -1;
00113 }
00114
00115
00116 if (length == 0) {
00117 if (code == 1) {
00118 length = bytestream2_get_byte(&g);
00119 offset = bytestream2_get_le16(&g);
00120 } else {
00121 length = bytestream2_get_le16(&g);
00122 if (code == 2 && length == 0)
00123 break;
00124 }
00125 } else {
00126 if (code == 1)
00127 offset = bytestream2_get_byte(&g);
00128 }
00129
00130
00131 if (dst + (length << lentab[code]) > frame_end)
00132 break;
00133
00134 switch (code) {
00135
00136 case 0:
00137 if (length >= bytestream2_get_bytes_left(&g)) {
00138 av_log(avctx, AV_LOG_ERROR, "Frame larger than buffer.\n");
00139 return -1;
00140 }
00141 bytestream2_get_buffer(&g, dst, length);
00142 dst += length;
00143 break;
00144
00145 case 1:
00146 dst_offset = dst - offset;
00147 length *= 4;
00148 if (dst_offset < bfi->dst)
00149 break;
00150 while (length--)
00151 *dst++ = *dst_offset++;
00152 break;
00153
00154 case 2:
00155 dst += length;
00156 break;
00157
00158 case 3:
00159 colour1 = bytestream2_get_byte(&g);
00160 colour2 = bytestream2_get_byte(&g);
00161 while (length--) {
00162 *dst++ = colour1;
00163 *dst++ = colour2;
00164 }
00165 break;
00166
00167 }
00168 }
00169
00170 src = bfi->dst;
00171 dst = bfi->frame.data[0];
00172 while (height--) {
00173 memcpy(dst, src, avctx->width);
00174 src += avctx->width;
00175 dst += bfi->frame.linesize[0];
00176 }
00177 *data_size = sizeof(AVFrame);
00178 *(AVFrame *)data = bfi->frame;
00179 return buf_size;
00180 }
00181
00182 static av_cold int bfi_decode_close(AVCodecContext * avctx)
00183 {
00184 BFIContext *bfi = avctx->priv_data;
00185 if (bfi->frame.data[0])
00186 avctx->release_buffer(avctx, &bfi->frame);
00187 av_free(bfi->dst);
00188 return 0;
00189 }
00190
00191 AVCodec ff_bfi_decoder = {
00192 .name = "bfi",
00193 .type = AVMEDIA_TYPE_VIDEO,
00194 .id = CODEC_ID_BFI,
00195 .priv_data_size = sizeof(BFIContext),
00196 .init = bfi_decode_init,
00197 .close = bfi_decode_close,
00198 .decode = bfi_decode_frame,
00199 .capabilities = CODEC_CAP_DR1,
00200 .long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
00201 };