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