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 #include "internal.h"
00033
00034 typedef struct BFIContext {
00035 AVCodecContext *avctx;
00036 AVFrame frame;
00037 uint8_t *dst;
00038 uint32_t pal[256];
00039 } BFIContext;
00040
00041 static av_cold int bfi_decode_init(AVCodecContext *avctx)
00042 {
00043 BFIContext *bfi = avctx->priv_data;
00044 avctx->pix_fmt = AV_PIX_FMT_PAL8;
00045 avcodec_get_frame_defaults(&bfi->frame);
00046 bfi->dst = av_mallocz(avctx->width * avctx->height);
00047 return 0;
00048 }
00049
00050 static int bfi_decode_frame(AVCodecContext *avctx, void *data,
00051 int *got_frame, AVPacket *avpkt)
00052 {
00053 GetByteContext g;
00054 int buf_size = avpkt->size;
00055 BFIContext *bfi = avctx->priv_data;
00056 uint8_t *dst = bfi->dst;
00057 uint8_t *src, *dst_offset, colour1, colour2;
00058 uint8_t *frame_end = bfi->dst + avctx->width * avctx->height;
00059 uint32_t *pal;
00060 int i, j, height = avctx->height;
00061
00062 if (bfi->frame.data[0])
00063 avctx->release_buffer(avctx, &bfi->frame);
00064
00065 bfi->frame.reference = 3;
00066
00067 if (ff_get_buffer(avctx, &bfi->frame) < 0) {
00068 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00069 return -1;
00070 }
00071
00072 bytestream2_init(&g, avpkt->data, buf_size);
00073
00074
00075 if (!avctx->frame_number) {
00076 bfi->frame.pict_type = AV_PICTURE_TYPE_I;
00077 bfi->frame.key_frame = 1;
00078
00079 if (avctx->extradata_size > 768) {
00080 av_log(NULL, AV_LOG_ERROR, "Palette is too large.\n");
00081 return -1;
00082 }
00083 pal = (uint32_t *)bfi->frame.data[1];
00084 for (i = 0; i < avctx->extradata_size / 3; i++) {
00085 int shift = 16;
00086 *pal = 0xFFU << 24;
00087 for (j = 0; j < 3; j++, shift -= 8)
00088 *pal += ((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 case 0:
00136 if (length >= bytestream2_get_bytes_left(&g)) {
00137 av_log(avctx, AV_LOG_ERROR, "Frame larger than buffer.\n");
00138 return -1;
00139 }
00140 bytestream2_get_buffer(&g, dst, length);
00141 dst += length;
00142 break;
00143 case 1:
00144 dst_offset = dst - offset;
00145 length *= 4;
00146 if (dst_offset < bfi->dst)
00147 break;
00148 while (length--)
00149 *dst++ = *dst_offset++;
00150 break;
00151 case 2:
00152 dst += length;
00153 break;
00154 case 3:
00155 colour1 = bytestream2_get_byte(&g);
00156 colour2 = bytestream2_get_byte(&g);
00157 while (length--) {
00158 *dst++ = colour1;
00159 *dst++ = colour2;
00160 }
00161 break;
00162 }
00163 }
00164
00165 src = bfi->dst;
00166 dst = bfi->frame.data[0];
00167 while (height--) {
00168 memcpy(dst, src, avctx->width);
00169 src += avctx->width;
00170 dst += bfi->frame.linesize[0];
00171 }
00172 *got_frame = 1;
00173 *(AVFrame *)data = bfi->frame;
00174 return buf_size;
00175 }
00176
00177 static av_cold int bfi_decode_close(AVCodecContext *avctx)
00178 {
00179 BFIContext *bfi = avctx->priv_data;
00180 if (bfi->frame.data[0])
00181 avctx->release_buffer(avctx, &bfi->frame);
00182 av_free(bfi->dst);
00183 return 0;
00184 }
00185
00186 AVCodec ff_bfi_decoder = {
00187 .name = "bfi",
00188 .type = AVMEDIA_TYPE_VIDEO,
00189 .id = AV_CODEC_ID_BFI,
00190 .priv_data_size = sizeof(BFIContext),
00191 .init = bfi_decode_init,
00192 .close = bfi_decode_close,
00193 .decode = bfi_decode_frame,
00194 .capabilities = CODEC_CAP_DR1,
00195 .long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
00196 };