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