Libav
|
00001 /* 00002 * KMVC decoder 00003 * Copyright (c) 2006 Konstantin Shishkov 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 00037 /* 00038 * Decoder context 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, res) {\ 00061 res = 0; \ 00062 if (bb.bitbuf & (1 << bb.bits)) res = 1; \ 00063 bb.bits--; \ 00064 if(bb.bits == -1) { \ 00065 bb.bitbuf = *src++; \ 00066 bb.bits = 7; \ 00067 } \ 00068 } 00069 00070 static void kmvc_decode_intra_8x8(KmvcContext * ctx, const uint8_t * src, int w, int h) 00071 { 00072 BitBuf bb; 00073 int res, val; 00074 int i, j; 00075 int bx, by; 00076 int l0x, l1x, l0y, l1y; 00077 int mx, my; 00078 00079 kmvc_init_getbits(bb, src); 00080 00081 for (by = 0; by < h; by += 8) 00082 for (bx = 0; bx < w; bx += 8) { 00083 kmvc_getbit(bb, src, res); 00084 if (!res) { // fill whole 8x8 block 00085 val = *src++; 00086 for (i = 0; i < 64; i++) 00087 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val; 00088 } else { // handle four 4x4 subblocks 00089 for (i = 0; i < 4; i++) { 00090 l0x = bx + (i & 1) * 4; 00091 l0y = by + (i & 2) * 2; 00092 kmvc_getbit(bb, src, res); 00093 if (!res) { 00094 kmvc_getbit(bb, src, res); 00095 if (!res) { // fill whole 4x4 block 00096 val = *src++; 00097 for (j = 0; j < 16; j++) 00098 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val; 00099 } else { // copy block from already decoded place 00100 val = *src++; 00101 mx = val & 0xF; 00102 my = val >> 4; 00103 for (j = 0; j < 16; j++) 00104 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = 00105 BLK(ctx->cur, l0x + (j & 3) - mx, l0y + (j >> 2) - my); 00106 } 00107 } else { // descend to 2x2 sub-sub-blocks 00108 for (j = 0; j < 4; j++) { 00109 l1x = l0x + (j & 1) * 2; 00110 l1y = l0y + (j & 2); 00111 kmvc_getbit(bb, src, res); 00112 if (!res) { 00113 kmvc_getbit(bb, src, res); 00114 if (!res) { // fill whole 2x2 block 00115 val = *src++; 00116 BLK(ctx->cur, l1x, l1y) = val; 00117 BLK(ctx->cur, l1x + 1, l1y) = val; 00118 BLK(ctx->cur, l1x, l1y + 1) = val; 00119 BLK(ctx->cur, l1x + 1, l1y + 1) = val; 00120 } else { // copy block from already decoded place 00121 val = *src++; 00122 mx = val & 0xF; 00123 my = val >> 4; 00124 BLK(ctx->cur, l1x, l1y) = BLK(ctx->cur, l1x - mx, l1y - my); 00125 BLK(ctx->cur, l1x + 1, l1y) = 00126 BLK(ctx->cur, l1x + 1 - mx, l1y - my); 00127 BLK(ctx->cur, l1x, l1y + 1) = 00128 BLK(ctx->cur, l1x - mx, l1y + 1 - my); 00129 BLK(ctx->cur, l1x + 1, l1y + 1) = 00130 BLK(ctx->cur, l1x + 1 - mx, l1y + 1 - my); 00131 } 00132 } else { // read values for block 00133 BLK(ctx->cur, l1x, l1y) = *src++; 00134 BLK(ctx->cur, l1x + 1, l1y) = *src++; 00135 BLK(ctx->cur, l1x, l1y + 1) = *src++; 00136 BLK(ctx->cur, l1x + 1, l1y + 1) = *src++; 00137 } 00138 } 00139 } 00140 } 00141 } 00142 } 00143 } 00144 00145 static void kmvc_decode_inter_8x8(KmvcContext * ctx, const uint8_t * src, int w, int h) 00146 { 00147 BitBuf bb; 00148 int res, val; 00149 int i, j; 00150 int bx, by; 00151 int l0x, l1x, l0y, l1y; 00152 int mx, my; 00153 00154 kmvc_init_getbits(bb, src); 00155 00156 for (by = 0; by < h; by += 8) 00157 for (bx = 0; bx < w; bx += 8) { 00158 kmvc_getbit(bb, src, res); 00159 if (!res) { 00160 kmvc_getbit(bb, src, res); 00161 if (!res) { // fill whole 8x8 block 00162 val = *src++; 00163 for (i = 0; i < 64; i++) 00164 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val; 00165 } else { // copy block from previous frame 00166 for (i = 0; i < 64; i++) 00167 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = 00168 BLK(ctx->prev, bx + (i & 0x7), by + (i >> 3)); 00169 } 00170 } else { // handle four 4x4 subblocks 00171 for (i = 0; i < 4; i++) { 00172 l0x = bx + (i & 1) * 4; 00173 l0y = by + (i & 2) * 2; 00174 kmvc_getbit(bb, src, res); 00175 if (!res) { 00176 kmvc_getbit(bb, src, res); 00177 if (!res) { // fill whole 4x4 block 00178 val = *src++; 00179 for (j = 0; j < 16; j++) 00180 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val; 00181 } else { // copy block 00182 val = *src++; 00183 mx = (val & 0xF) - 8; 00184 my = (val >> 4) - 8; 00185 for (j = 0; j < 16; j++) 00186 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = 00187 BLK(ctx->prev, l0x + (j & 3) + mx, l0y + (j >> 2) + my); 00188 } 00189 } else { // descend to 2x2 sub-sub-blocks 00190 for (j = 0; j < 4; j++) { 00191 l1x = l0x + (j & 1) * 2; 00192 l1y = l0y + (j & 2); 00193 kmvc_getbit(bb, src, res); 00194 if (!res) { 00195 kmvc_getbit(bb, src, res); 00196 if (!res) { // fill whole 2x2 block 00197 val = *src++; 00198 BLK(ctx->cur, l1x, l1y) = val; 00199 BLK(ctx->cur, l1x + 1, l1y) = val; 00200 BLK(ctx->cur, l1x, l1y + 1) = val; 00201 BLK(ctx->cur, l1x + 1, l1y + 1) = val; 00202 } else { // copy block 00203 val = *src++; 00204 mx = (val & 0xF) - 8; 00205 my = (val >> 4) - 8; 00206 BLK(ctx->cur, l1x, l1y) = BLK(ctx->prev, l1x + mx, l1y + my); 00207 BLK(ctx->cur, l1x + 1, l1y) = 00208 BLK(ctx->prev, l1x + 1 + mx, l1y + my); 00209 BLK(ctx->cur, l1x, l1y + 1) = 00210 BLK(ctx->prev, l1x + mx, l1y + 1 + my); 00211 BLK(ctx->cur, l1x + 1, l1y + 1) = 00212 BLK(ctx->prev, l1x + 1 + mx, l1y + 1 + my); 00213 } 00214 } else { // read values for block 00215 BLK(ctx->cur, l1x, l1y) = *src++; 00216 BLK(ctx->cur, l1x + 1, l1y) = *src++; 00217 BLK(ctx->cur, l1x, l1y + 1) = *src++; 00218 BLK(ctx->cur, l1x + 1, l1y + 1) = *src++; 00219 } 00220 } 00221 } 00222 } 00223 } 00224 } 00225 } 00226 00227 static int decode_frame(AVCodecContext * avctx, void *data, int *data_size, AVPacket *avpkt) 00228 { 00229 const uint8_t *buf = avpkt->data; 00230 int buf_size = avpkt->size; 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 /* blocksize 127 is really palette change event */ 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 /* if palette has been changed, copy it from palctrl */ 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 // palette starts from index 1 and has 127 entries 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 /* make the palette available on the way out */ 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: // used in palette changed event 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 /* flip buffers */ 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 /* always report that the buffer was completely consumed */ 00334 return buf_size; 00335 } 00336 00337 00338 00339 /* 00340 * Init kmvc decoder 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 if (avctx->width > 320 || avctx->height > 200) { 00350 av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n"); 00351 return -1; 00352 } 00353 00354 c->frm0 = av_mallocz(320 * 200); 00355 c->frm1 = av_mallocz(320 * 200); 00356 c->cur = c->frm0; 00357 c->prev = c->frm1; 00358 00359 for (i = 0; i < 256; i++) { 00360 c->pal[i] = i * 0x10101; 00361 } 00362 00363 if (avctx->extradata_size < 12) { 00364 av_log(NULL, 0, "Extradata missing, decoding may not work properly...\n"); 00365 c->palsize = 127; 00366 } else { 00367 c->palsize = AV_RL16(avctx->extradata + 10); 00368 } 00369 00370 if (avctx->extradata_size == 1036) { // palette in extradata 00371 uint8_t *src = avctx->extradata + 12; 00372 for (i = 0; i < 256; i++) { 00373 c->pal[i] = AV_RL32(src); 00374 src += 4; 00375 } 00376 c->setpal = 1; 00377 if (c->avctx->palctrl) { 00378 c->avctx->palctrl->palette_changed = 0; 00379 } 00380 } 00381 00382 avctx->pix_fmt = PIX_FMT_PAL8; 00383 00384 return 0; 00385 } 00386 00387 00388 00389 /* 00390 * Uninit kmvc decoder 00391 */ 00392 static av_cold int decode_end(AVCodecContext * avctx) 00393 { 00394 KmvcContext *const c = avctx->priv_data; 00395 00396 av_freep(&c->frm0); 00397 av_freep(&c->frm1); 00398 if (c->pic.data[0]) 00399 avctx->release_buffer(avctx, &c->pic); 00400 00401 return 0; 00402 } 00403 00404 AVCodec kmvc_decoder = { 00405 "kmvc", 00406 AVMEDIA_TYPE_VIDEO, 00407 CODEC_ID_KMVC, 00408 sizeof(KmvcContext), 00409 decode_init, 00410 NULL, 00411 decode_end, 00412 decode_frame, 00413 CODEC_CAP_DR1, 00414 .long_name = NULL_IF_CONFIG_SMALL("Karl Morton's video codec"), 00415 };