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 += ((avctx->extradata[i * 3 + j] << 2) |
00088 (avctx->extradata[i * 3 + j] >> 4)) << shift;
00089 pal++;
00090 }
00091 memcpy(bfi->pal, bfi->frame.data[1], sizeof(bfi->pal));
00092 bfi->frame.palette_has_changed = 1;
00093 } else {
00094 bfi->frame.pict_type = AV_PICTURE_TYPE_P;
00095 bfi->frame.key_frame = 0;
00096 bfi->frame.palette_has_changed = 0;
00097 memcpy(bfi->frame.data[1], bfi->pal, sizeof(bfi->pal));
00098 }
00099
00100 bytestream2_skip(&g, 4);
00101
00102 while (dst != frame_end) {
00103 static const uint8_t lentab[4] = { 0, 2, 0, 1 };
00104 unsigned int byte = bytestream2_get_byte(&g), av_uninit(offset);
00105 unsigned int code = byte >> 6;
00106 unsigned int length = byte & ~0xC0;
00107
00108 if (!bytestream2_get_bytes_left(&g)) {
00109 av_log(avctx, AV_LOG_ERROR,
00110 "Input resolution larger than actual frame.\n");
00111 return -1;
00112 }
00113
00114
00115 if (length == 0) {
00116 if (code == 1) {
00117 length = bytestream2_get_byte(&g);
00118 offset = bytestream2_get_le16(&g);
00119 } else {
00120 length = bytestream2_get_le16(&g);
00121 if (code == 2 && length == 0)
00122 break;
00123 }
00124 } else {
00125 if (code == 1)
00126 offset = bytestream2_get_byte(&g);
00127 }
00128
00129
00130 if (dst + (length << lentab[code]) > frame_end)
00131 break;
00132
00133 switch (code) {
00134 case 0:
00135 if (length >= bytestream2_get_bytes_left(&g)) {
00136 av_log(avctx, AV_LOG_ERROR, "Frame larger than buffer.\n");
00137 return -1;
00138 }
00139 bytestream2_get_buffer(&g, dst, length);
00140 dst += length;
00141 break;
00142 case 1:
00143 dst_offset = dst - offset;
00144 length *= 4;
00145 if (dst_offset < bfi->dst)
00146 break;
00147 while (length--)
00148 *dst++ = *dst_offset++;
00149 break;
00150 case 2:
00151 dst += length;
00152 break;
00153 case 3:
00154 colour1 = bytestream2_get_byte(&g);
00155 colour2 = bytestream2_get_byte(&g);
00156 while (length--) {
00157 *dst++ = colour1;
00158 *dst++ = colour2;
00159 }
00160 break;
00161 }
00162 }
00163
00164 src = bfi->dst;
00165 dst = bfi->frame.data[0];
00166 while (height--) {
00167 memcpy(dst, src, avctx->width);
00168 src += avctx->width;
00169 dst += bfi->frame.linesize[0];
00170 }
00171 *data_size = sizeof(AVFrame);
00172 *(AVFrame *)data = bfi->frame;
00173 return buf_size;
00174 }
00175
00176 static av_cold int bfi_decode_close(AVCodecContext *avctx)
00177 {
00178 BFIContext *bfi = avctx->priv_data;
00179 if (bfi->frame.data[0])
00180 avctx->release_buffer(avctx, &bfi->frame);
00181 av_free(bfi->dst);
00182 return 0;
00183 }
00184
00185 AVCodec ff_bfi_decoder = {
00186 .name = "bfi",
00187 .type = AVMEDIA_TYPE_VIDEO,
00188 .id = CODEC_ID_BFI,
00189 .priv_data_size = sizeof(BFIContext),
00190 .init = bfi_decode_init,
00191 .close = bfi_decode_close,
00192 .decode = bfi_decode_frame,
00193 .capabilities = CODEC_CAP_DR1,
00194 .long_name = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
00195 };