Libav 0.7.1
|
00001 /* 00002 * KMVC decoder 00003 * Copyright (c) 2006 Konstantin Shishkov 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 * Decoder context 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) { // fill whole 8x8 block 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 { // handle four 4x4 subblocks 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) { // fill whole 4x4 block 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 { // copy block from already decoded place 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 { // descend to 2x2 sub-sub-blocks 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) { // fill whole 2x2 block 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 { // copy block from already decoded place 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 { // read values for block 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) { // fill whole 8x8 block 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 { // copy block from previous frame 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 { // handle four 4x4 subblocks 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) { // fill whole 4x4 block 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 { // copy block 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 { // descend to 2x2 sub-sub-blocks 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) { // fill whole 2x2 block 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 { // copy block 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 { // read values for block 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 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL); 00288 00289 if (ctx->pic.data[0]) 00290 avctx->release_buffer(avctx, &ctx->pic); 00291 00292 ctx->pic.reference = 1; 00293 ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID; 00294 if (avctx->get_buffer(avctx, &ctx->pic) < 0) { 00295 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 00296 return -1; 00297 } 00298 00299 header = *buf++; 00300 00301 /* blocksize 127 is really palette change event */ 00302 if (buf[0] == 127) { 00303 buf += 3; 00304 for (i = 0; i < 127; i++) { 00305 ctx->pal[i + (header & 0x81)] = AV_RB24(buf); 00306 buf += 4; 00307 } 00308 buf -= 127 * 4 + 3; 00309 } 00310 00311 if (header & KMVC_KEYFRAME) { 00312 ctx->pic.key_frame = 1; 00313 ctx->pic.pict_type = AV_PICTURE_TYPE_I; 00314 } else { 00315 ctx->pic.key_frame = 0; 00316 ctx->pic.pict_type = AV_PICTURE_TYPE_P; 00317 } 00318 00319 if (header & KMVC_PALETTE) { 00320 ctx->pic.palette_has_changed = 1; 00321 // palette starts from index 1 and has 127 entries 00322 for (i = 1; i <= ctx->palsize; i++) { 00323 ctx->pal[i] = bytestream_get_be24(&buf); 00324 } 00325 } 00326 00327 if (pal) { 00328 ctx->pic.palette_has_changed = 1; 00329 memcpy(ctx->pal, pal, AVPALETTE_SIZE); 00330 } 00331 00332 if (ctx->setpal) { 00333 ctx->setpal = 0; 00334 ctx->pic.palette_has_changed = 1; 00335 } 00336 00337 /* make the palette available on the way out */ 00338 memcpy(ctx->pic.data[1], ctx->pal, 1024); 00339 00340 blocksize = *buf++; 00341 00342 if (blocksize != 8 && blocksize != 127) { 00343 av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize); 00344 return -1; 00345 } 00346 memset(ctx->cur, 0, 320 * 200); 00347 switch (header & KMVC_METHOD) { 00348 case 0: 00349 case 1: // used in palette changed event 00350 memcpy(ctx->cur, ctx->prev, 320 * 200); 00351 break; 00352 case 3: 00353 kmvc_decode_intra_8x8(ctx, buf, buf_size, avctx->width, avctx->height); 00354 break; 00355 case 4: 00356 kmvc_decode_inter_8x8(ctx, buf, buf_size, avctx->width, avctx->height); 00357 break; 00358 default: 00359 av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD); 00360 return -1; 00361 } 00362 00363 out = ctx->pic.data[0]; 00364 src = ctx->cur; 00365 for (i = 0; i < avctx->height; i++) { 00366 memcpy(out, src, avctx->width); 00367 src += 320; 00368 out += ctx->pic.linesize[0]; 00369 } 00370 00371 /* flip buffers */ 00372 if (ctx->cur == ctx->frm0) { 00373 ctx->cur = ctx->frm1; 00374 ctx->prev = ctx->frm0; 00375 } else { 00376 ctx->cur = ctx->frm0; 00377 ctx->prev = ctx->frm1; 00378 } 00379 00380 *data_size = sizeof(AVFrame); 00381 *(AVFrame *) data = ctx->pic; 00382 00383 /* always report that the buffer was completely consumed */ 00384 return buf_size; 00385 } 00386 00387 00388 00389 /* 00390 * Init kmvc decoder 00391 */ 00392 static av_cold int decode_init(AVCodecContext * avctx) 00393 { 00394 KmvcContext *const c = avctx->priv_data; 00395 int i; 00396 00397 c->avctx = avctx; 00398 00399 if (avctx->width > 320 || avctx->height > 200) { 00400 av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n"); 00401 return -1; 00402 } 00403 00404 c->frm0 = av_mallocz(320 * 200); 00405 c->frm1 = av_mallocz(320 * 200); 00406 c->cur = c->frm0; 00407 c->prev = c->frm1; 00408 00409 for (i = 0; i < 256; i++) { 00410 c->pal[i] = i * 0x10101; 00411 } 00412 00413 if (avctx->extradata_size < 12) { 00414 av_log(NULL, 0, "Extradata missing, decoding may not work properly...\n"); 00415 c->palsize = 127; 00416 } else { 00417 c->palsize = AV_RL16(avctx->extradata + 10); 00418 if (c->palsize >= MAX_PALSIZE) { 00419 av_log(avctx, AV_LOG_ERROR, "KMVC palette too large\n"); 00420 return AVERROR_INVALIDDATA; 00421 } 00422 } 00423 00424 if (avctx->extradata_size == 1036) { // palette in extradata 00425 uint8_t *src = avctx->extradata + 12; 00426 for (i = 0; i < 256; i++) { 00427 c->pal[i] = AV_RL32(src); 00428 src += 4; 00429 } 00430 c->setpal = 1; 00431 } 00432 00433 avctx->pix_fmt = PIX_FMT_PAL8; 00434 00435 return 0; 00436 } 00437 00438 00439 00440 /* 00441 * Uninit kmvc decoder 00442 */ 00443 static av_cold int decode_end(AVCodecContext * avctx) 00444 { 00445 KmvcContext *const c = avctx->priv_data; 00446 00447 av_freep(&c->frm0); 00448 av_freep(&c->frm1); 00449 if (c->pic.data[0]) 00450 avctx->release_buffer(avctx, &c->pic); 00451 00452 return 0; 00453 } 00454 00455 AVCodec ff_kmvc_decoder = { 00456 "kmvc", 00457 AVMEDIA_TYPE_VIDEO, 00458 CODEC_ID_KMVC, 00459 sizeof(KmvcContext), 00460 decode_init, 00461 NULL, 00462 decode_end, 00463 decode_frame, 00464 CODEC_CAP_DR1, 00465 .long_name = NULL_IF_CONFIG_SMALL("Karl Morton's video codec"), 00466 };