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