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