00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029
00030 #include "avcodec.h"
00031 #include "bytestream.h"
00032 #include "internal.h"
00033 #include "libavutil/common.h"
00034
00035 #define KMVC_KEYFRAME 0x80
00036 #define KMVC_PALETTE 0x40
00037 #define KMVC_METHOD 0x0F
00038 #define MAX_PALSIZE 256
00039
00040
00041
00042
00043 typedef struct KmvcContext {
00044 AVCodecContext *avctx;
00045 AVFrame pic;
00046
00047 int setpal;
00048 int palsize;
00049 uint32_t pal[MAX_PALSIZE];
00050 uint8_t *cur, *prev;
00051 uint8_t frm0[320 * 200], frm1[320 * 200];
00052 GetByteContext g;
00053 } KmvcContext;
00054
00055 typedef struct BitBuf {
00056 int bits;
00057 int bitbuf;
00058 } BitBuf;
00059
00060 #define BLK(data, x, y) data[av_clip((x) + (y) * 320, 0, 320 * 200 -1)]
00061
00062 #define kmvc_init_getbits(bb, g) bb.bits = 7; bb.bitbuf = bytestream2_get_byte(g);
00063
00064 #define kmvc_getbit(bb, g, res) {\
00065 res = 0; \
00066 if (bb.bitbuf & (1 << bb.bits)) res = 1; \
00067 bb.bits--; \
00068 if(bb.bits == -1) { \
00069 bb.bitbuf = bytestream2_get_byte(g); \
00070 bb.bits = 7; \
00071 } \
00072 }
00073
00074 static int kmvc_decode_intra_8x8(KmvcContext * ctx, int w, int h)
00075 {
00076 BitBuf bb;
00077 int res, val;
00078 int i, j;
00079 int bx, by;
00080 int l0x, l1x, l0y, l1y;
00081 int mx, my;
00082
00083 kmvc_init_getbits(bb, &ctx->g);
00084
00085 for (by = 0; by < h; by += 8)
00086 for (bx = 0; bx < w; bx += 8) {
00087 if (!bytestream2_get_bytes_left(&ctx->g)) {
00088 av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
00089 return AVERROR_INVALIDDATA;
00090 }
00091 kmvc_getbit(bb, &ctx->g, res);
00092 if (!res) {
00093 val = bytestream2_get_byte(&ctx->g);
00094 for (i = 0; i < 64; i++)
00095 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
00096 } else {
00097 for (i = 0; i < 4; i++) {
00098 l0x = bx + (i & 1) * 4;
00099 l0y = by + (i & 2) * 2;
00100 kmvc_getbit(bb, &ctx->g, res);
00101 if (!res) {
00102 kmvc_getbit(bb, &ctx->g, res);
00103 if (!res) {
00104 val = bytestream2_get_byte(&ctx->g);
00105 for (j = 0; j < 16; j++)
00106 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
00107 } else {
00108 val = bytestream2_get_byte(&ctx->g);
00109 mx = val & 0xF;
00110 my = val >> 4;
00111 for (j = 0; j < 16; j++)
00112 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
00113 BLK(ctx->cur, l0x + (j & 3) - mx, l0y + (j >> 2) - my);
00114 }
00115 } else {
00116 for (j = 0; j < 4; j++) {
00117 l1x = l0x + (j & 1) * 2;
00118 l1y = l0y + (j & 2);
00119 kmvc_getbit(bb, &ctx->g, res);
00120 if (!res) {
00121 kmvc_getbit(bb, &ctx->g, res);
00122 if (!res) {
00123 val = bytestream2_get_byte(&ctx->g);
00124 BLK(ctx->cur, l1x, l1y) = val;
00125 BLK(ctx->cur, l1x + 1, l1y) = val;
00126 BLK(ctx->cur, l1x, l1y + 1) = val;
00127 BLK(ctx->cur, l1x + 1, l1y + 1) = val;
00128 } else {
00129 val = bytestream2_get_byte(&ctx->g);
00130 mx = val & 0xF;
00131 my = val >> 4;
00132 BLK(ctx->cur, l1x, l1y) = BLK(ctx->cur, l1x - mx, l1y - my);
00133 BLK(ctx->cur, l1x + 1, l1y) =
00134 BLK(ctx->cur, l1x + 1 - mx, l1y - my);
00135 BLK(ctx->cur, l1x, l1y + 1) =
00136 BLK(ctx->cur, l1x - mx, l1y + 1 - my);
00137 BLK(ctx->cur, l1x + 1, l1y + 1) =
00138 BLK(ctx->cur, l1x + 1 - mx, l1y + 1 - my);
00139 }
00140 } else {
00141 BLK(ctx->cur, l1x, l1y) = bytestream2_get_byte(&ctx->g);
00142 BLK(ctx->cur, l1x + 1, l1y) = bytestream2_get_byte(&ctx->g);
00143 BLK(ctx->cur, l1x, l1y + 1) = bytestream2_get_byte(&ctx->g);
00144 BLK(ctx->cur, l1x + 1, l1y + 1) = bytestream2_get_byte(&ctx->g);
00145 }
00146 }
00147 }
00148 }
00149 }
00150 }
00151
00152 return 0;
00153 }
00154
00155 static int kmvc_decode_inter_8x8(KmvcContext * ctx, int w, int h)
00156 {
00157 BitBuf bb;
00158 int res, val;
00159 int i, j;
00160 int bx, by;
00161 int l0x, l1x, l0y, l1y;
00162 int mx, my;
00163
00164 kmvc_init_getbits(bb, &ctx->g);
00165
00166 for (by = 0; by < h; by += 8)
00167 for (bx = 0; bx < w; bx += 8) {
00168 kmvc_getbit(bb, &ctx->g, res);
00169 if (!res) {
00170 kmvc_getbit(bb, &ctx->g, res);
00171 if (!res) {
00172 if (!bytestream2_get_bytes_left(&ctx->g)) {
00173 av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
00174 return AVERROR_INVALIDDATA;
00175 }
00176 val = bytestream2_get_byte(&ctx->g);
00177 for (i = 0; i < 64; i++)
00178 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
00179 } else {
00180 for (i = 0; i < 64; i++)
00181 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) =
00182 BLK(ctx->prev, bx + (i & 0x7), by + (i >> 3));
00183 }
00184 } else {
00185 if (!bytestream2_get_bytes_left(&ctx->g)) {
00186 av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
00187 return AVERROR_INVALIDDATA;
00188 }
00189 for (i = 0; i < 4; i++) {
00190 l0x = bx + (i & 1) * 4;
00191 l0y = by + (i & 2) * 2;
00192 kmvc_getbit(bb, &ctx->g, res);
00193 if (!res) {
00194 kmvc_getbit(bb, &ctx->g, res);
00195 if (!res) {
00196 val = bytestream2_get_byte(&ctx->g);
00197 for (j = 0; j < 16; j++)
00198 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
00199 } else {
00200 val = bytestream2_get_byte(&ctx->g);
00201 mx = (val & 0xF) - 8;
00202 my = (val >> 4) - 8;
00203 for (j = 0; j < 16; j++)
00204 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
00205 BLK(ctx->prev, l0x + (j & 3) + mx, l0y + (j >> 2) + my);
00206 }
00207 } else {
00208 for (j = 0; j < 4; j++) {
00209 l1x = l0x + (j & 1) * 2;
00210 l1y = l0y + (j & 2);
00211 kmvc_getbit(bb, &ctx->g, res);
00212 if (!res) {
00213 kmvc_getbit(bb, &ctx->g, res);
00214 if (!res) {
00215 val = bytestream2_get_byte(&ctx->g);
00216 BLK(ctx->cur, l1x, l1y) = val;
00217 BLK(ctx->cur, l1x + 1, l1y) = val;
00218 BLK(ctx->cur, l1x, l1y + 1) = val;
00219 BLK(ctx->cur, l1x + 1, l1y + 1) = val;
00220 } else {
00221 val = bytestream2_get_byte(&ctx->g);
00222 mx = (val & 0xF) - 8;
00223 my = (val >> 4) - 8;
00224 BLK(ctx->cur, l1x, l1y) = BLK(ctx->prev, l1x + mx, l1y + my);
00225 BLK(ctx->cur, l1x + 1, l1y) =
00226 BLK(ctx->prev, l1x + 1 + mx, l1y + my);
00227 BLK(ctx->cur, l1x, l1y + 1) =
00228 BLK(ctx->prev, l1x + mx, l1y + 1 + my);
00229 BLK(ctx->cur, l1x + 1, l1y + 1) =
00230 BLK(ctx->prev, l1x + 1 + mx, l1y + 1 + my);
00231 }
00232 } else {
00233 BLK(ctx->cur, l1x, l1y) = bytestream2_get_byte(&ctx->g);
00234 BLK(ctx->cur, l1x + 1, l1y) = bytestream2_get_byte(&ctx->g);
00235 BLK(ctx->cur, l1x, l1y + 1) = bytestream2_get_byte(&ctx->g);
00236 BLK(ctx->cur, l1x + 1, l1y + 1) = bytestream2_get_byte(&ctx->g);
00237 }
00238 }
00239 }
00240 }
00241 }
00242 }
00243
00244 return 0;
00245 }
00246
00247 static int decode_frame(AVCodecContext * avctx, void *data, int *data_size, AVPacket *avpkt)
00248 {
00249 KmvcContext *const ctx = avctx->priv_data;
00250 uint8_t *out, *src;
00251 int i;
00252 int header;
00253 int blocksize;
00254 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
00255
00256 bytestream2_init(&ctx->g, avpkt->data, avpkt->size);
00257 if (ctx->pic.data[0])
00258 avctx->release_buffer(avctx, &ctx->pic);
00259
00260 ctx->pic.reference = 1;
00261 ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
00262 if (avctx->get_buffer(avctx, &ctx->pic) < 0) {
00263 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00264 return -1;
00265 }
00266
00267 header = bytestream2_get_byte(&ctx->g);
00268
00269
00270 if (bytestream2_peek_byte(&ctx->g) == 127) {
00271 bytestream2_skip(&ctx->g, 3);
00272 for (i = 0; i < 127; i++) {
00273 ctx->pal[i + (header & 0x81)] = bytestream2_get_be24(&ctx->g);
00274 bytestream2_skip(&ctx->g, 1);
00275 }
00276 bytestream2_seek(&ctx->g, -127 * 4 - 3, SEEK_CUR);
00277 }
00278
00279 if (header & KMVC_KEYFRAME) {
00280 ctx->pic.key_frame = 1;
00281 ctx->pic.pict_type = AV_PICTURE_TYPE_I;
00282 } else {
00283 ctx->pic.key_frame = 0;
00284 ctx->pic.pict_type = AV_PICTURE_TYPE_P;
00285 }
00286
00287 if (header & KMVC_PALETTE) {
00288 ctx->pic.palette_has_changed = 1;
00289
00290 for (i = 1; i <= ctx->palsize; i++) {
00291 ctx->pal[i] = bytestream2_get_be24(&ctx->g);
00292 }
00293 }
00294
00295 if (pal) {
00296 ctx->pic.palette_has_changed = 1;
00297 memcpy(ctx->pal, pal, AVPALETTE_SIZE);
00298 }
00299
00300 if (ctx->setpal) {
00301 ctx->setpal = 0;
00302 ctx->pic.palette_has_changed = 1;
00303 }
00304
00305
00306 memcpy(ctx->pic.data[1], ctx->pal, 1024);
00307
00308 blocksize = bytestream2_get_byte(&ctx->g);
00309
00310 if (blocksize != 8 && blocksize != 127) {
00311 av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize);
00312 return -1;
00313 }
00314 memset(ctx->cur, 0, 320 * 200);
00315 switch (header & KMVC_METHOD) {
00316 case 0:
00317 case 1:
00318 memcpy(ctx->cur, ctx->prev, 320 * 200);
00319 break;
00320 case 3:
00321 kmvc_decode_intra_8x8(ctx, avctx->width, avctx->height);
00322 break;
00323 case 4:
00324 kmvc_decode_inter_8x8(ctx, avctx->width, avctx->height);
00325 break;
00326 default:
00327 av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
00328 return -1;
00329 }
00330
00331 out = ctx->pic.data[0];
00332 src = ctx->cur;
00333 for (i = 0; i < avctx->height; i++) {
00334 memcpy(out, src, avctx->width);
00335 src += 320;
00336 out += ctx->pic.linesize[0];
00337 }
00338
00339
00340 if (ctx->cur == ctx->frm0) {
00341 ctx->cur = ctx->frm1;
00342 ctx->prev = ctx->frm0;
00343 } else {
00344 ctx->cur = ctx->frm0;
00345 ctx->prev = ctx->frm1;
00346 }
00347
00348 *data_size = sizeof(AVFrame);
00349 *(AVFrame *) data = ctx->pic;
00350
00351
00352 return avpkt->size;
00353 }
00354
00355
00356
00357
00358
00359
00360 static av_cold int decode_init(AVCodecContext * avctx)
00361 {
00362 KmvcContext *const c = avctx->priv_data;
00363 int i;
00364
00365 c->avctx = avctx;
00366
00367 if (avctx->width > 320 || avctx->height > 200) {
00368 av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n");
00369 return -1;
00370 }
00371
00372 c->cur = c->frm0;
00373 c->prev = c->frm1;
00374
00375 for (i = 0; i < 256; i++) {
00376 c->pal[i] = i * 0x10101;
00377 }
00378
00379 if (avctx->extradata_size < 12) {
00380 av_log(NULL, 0, "Extradata missing, decoding may not work properly...\n");
00381 c->palsize = 127;
00382 } else {
00383 c->palsize = AV_RL16(avctx->extradata + 10);
00384 if (c->palsize >= MAX_PALSIZE) {
00385 av_log(avctx, AV_LOG_ERROR, "KMVC palette too large\n");
00386 return AVERROR_INVALIDDATA;
00387 }
00388 }
00389
00390 if (avctx->extradata_size == 1036) {
00391 uint8_t *src = avctx->extradata + 12;
00392 for (i = 0; i < 256; i++) {
00393 c->pal[i] = AV_RL32(src);
00394 src += 4;
00395 }
00396 c->setpal = 1;
00397 }
00398
00399 avctx->pix_fmt = PIX_FMT_PAL8;
00400
00401 return 0;
00402 }
00403
00404 AVCodec ff_kmvc_decoder = {
00405 .name = "kmvc",
00406 .type = AVMEDIA_TYPE_VIDEO,
00407 .id = CODEC_ID_KMVC,
00408 .priv_data_size = sizeof(KmvcContext),
00409 .init = decode_init,
00410 .decode = decode_frame,
00411 .capabilities = CODEC_CAP_DR1,
00412 .long_name = NULL_IF_CONFIG_SMALL("Karl Morton's video codec"),
00413 };