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