KMVC (used in Worms games) decoder. Works fine with samples from MPHQ
[libav.git] / libavcodec / kmvc.c
1 /*
2 * KMVC decoder
3 * Copyright (c) 2006 Konstantin Shishkov
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21 /**
22 * @file kmvc.c
23 * Karl Morton's Video Codec decoder
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "common.h"
30 #include "avcodec.h"
31
32 #define KMVC_KEYFRAME 0x80
33 #define KMVC_PALETTE 0x40
34 #define KMVC_METHOD 0x0F
35
36 /*
37 * Decoder context
38 */
39 typedef struct KmvcContext {
40 AVCodecContext *avctx;
41 AVFrame pic;
42
43 int setpal;
44 int palsize;
45 uint32_t pal[256];
46 uint8_t *cur, *prev;
47 uint8_t *frm0, *frm1;
48 } KmvcContext;
49
50 typedef struct BitBuf {
51 int bits;
52 int bitbuf;
53 } BitBuf;
54
55 #define BLK(data, x, y) data[(x) + (y) * 320]
56
57 #define kmvc_init_getbits(bb, src) bb.bits = 7; bb.bitbuf = *src++;
58
59 #define kmvc_getbit(bb, src, res) {\
60 res = 0; \
61 if (bb.bitbuf & (1 << bb.bits)) res = 1; \
62 bb.bits--; \
63 if(bb.bits == -1) { \
64 bb.bitbuf = *src++; \
65 bb.bits = 7; \
66 } \
67 }
68
69 static void kmvc_decode_intra_8x8(KmvcContext * ctx, uint8_t * src, int w, int h)
70 {
71 BitBuf bb;
72 int res, val;
73 int i, j;
74 int bx, by;
75 int l0x, l1x, l0y, l1y;
76 int mx, my;
77
78 kmvc_init_getbits(bb, src);
79
80 for (by = 0; by < h; by += 8)
81 for (bx = 0; bx < w; bx += 8) {
82 kmvc_getbit(bb, src, res);
83 if (!res) { // fill whole 8x8 block
84 val = *src++;
85 for (i = 0; i < 64; i++)
86 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
87 } else { // handle four 4x4 subblocks
88 for (i = 0; i < 4; i++) {
89 l0x = bx + (i & 1) * 4;
90 l0y = by + (i & 2) * 2;
91 kmvc_getbit(bb, src, res);
92 if (!res) {
93 kmvc_getbit(bb, src, res);
94 if (!res) { // fill whole 4x4 block
95 val = *src++;
96 for (j = 0; j < 16; j++)
97 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
98 } else { // copy block from already decoded place
99 val = *src++;
100 mx = val & 0xF;
101 my = val >> 4;
102 for (j = 0; j < 16; j++)
103 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
104 BLK(ctx->cur, l0x + (j & 3) - mx, l0y + (j >> 2) - my);
105 }
106 } else { // descend to 2x2 sub-sub-blocks
107 for (j = 0; j < 4; j++) {
108 l1x = l0x + (j & 1) * 2;
109 l1y = l0y + (j & 2);
110 kmvc_getbit(bb, src, res);
111 if (!res) {
112 kmvc_getbit(bb, src, res);
113 if (!res) { // fill whole 2x2 block
114 val = *src++;
115 BLK(ctx->cur, l1x, l1y) = val;
116 BLK(ctx->cur, l1x + 1, l1y) = val;
117 BLK(ctx->cur, l1x, l1y + 1) = val;
118 BLK(ctx->cur, l1x + 1, l1y + 1) = val;
119 } else { // copy block from already decoded place
120 val = *src++;
121 mx = val & 0xF;
122 my = val >> 4;
123 BLK(ctx->cur, l1x, l1y) = BLK(ctx->cur, l1x - mx, l1y - my);
124 BLK(ctx->cur, l1x + 1, l1y) =
125 BLK(ctx->cur, l1x + 1 - mx, l1y - my);
126 BLK(ctx->cur, l1x, l1y + 1) =
127 BLK(ctx->cur, l1x - mx, l1y + 1 - my);
128 BLK(ctx->cur, l1x + 1, l1y + 1) =
129 BLK(ctx->cur, l1x + 1 - mx, l1y + 1 - my);
130 }
131 } else { // read values for block
132 BLK(ctx->cur, l1x, l1y) = *src++;
133 BLK(ctx->cur, l1x + 1, l1y) = *src++;
134 BLK(ctx->cur, l1x, l1y + 1) = *src++;
135 BLK(ctx->cur, l1x + 1, l1y + 1) = *src++;
136 }
137 }
138 }
139 }
140 }
141 }
142 }
143
144 static void kmvc_decode_inter_8x8(KmvcContext * ctx, uint8_t * src, int w, int h)
145 {
146 BitBuf bb;
147 int res, val;
148 int i, j;
149 int bx, by;
150 int l0x, l1x, l0y, l1y;
151 int mx, my;
152
153 kmvc_init_getbits(bb, src);
154
155 for (by = 0; by < h; by += 8)
156 for (bx = 0; bx < w; bx += 8) {
157 kmvc_getbit(bb, src, res);
158 if (!res) {
159 kmvc_getbit(bb, src, res);
160 if (!res) { // fill whole 8x8 block
161 val = *src++;
162 for (i = 0; i < 64; i++)
163 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
164 } else { // copy block from previous frame
165 for (i = 0; i < 64; i++)
166 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) =
167 BLK(ctx->prev, bx + (i & 0x7), by + (i >> 3));
168 }
169 } else { // handle four 4x4 subblocks
170 for (i = 0; i < 4; i++) {
171 l0x = bx + (i & 1) * 4;
172 l0y = by + (i & 2) * 2;
173 kmvc_getbit(bb, src, res);
174 if (!res) {
175 kmvc_getbit(bb, src, res);
176 if (!res) { // fill whole 4x4 block
177 val = *src++;
178 for (j = 0; j < 16; j++)
179 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
180 } else { // copy block
181 val = *src++;
182 mx = (val & 0xF) - 8;
183 my = (val >> 4) - 8;
184 for (j = 0; j < 16; j++)
185 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
186 BLK(ctx->prev, l0x + (j & 3) + mx, l0y + (j >> 2) + my);
187 }
188 } else { // descend to 2x2 sub-sub-blocks
189 for (j = 0; j < 4; j++) {
190 l1x = l0x + (j & 1) * 2;
191 l1y = l0y + (j & 2);
192 kmvc_getbit(bb, src, res);
193 if (!res) {
194 kmvc_getbit(bb, src, res);
195 if (!res) { // fill whole 2x2 block
196 val = *src++;
197 BLK(ctx->cur, l1x, l1y) = val;
198 BLK(ctx->cur, l1x + 1, l1y) = val;
199 BLK(ctx->cur, l1x, l1y + 1) = val;
200 BLK(ctx->cur, l1x + 1, l1y + 1) = val;
201 } else { // copy block
202 val = *src++;
203 mx = (val & 0xF) - 8;
204 my = (val >> 4) - 8;
205 BLK(ctx->cur, l1x, l1y) = BLK(ctx->prev, l1x + mx, l1y + my);
206 BLK(ctx->cur, l1x + 1, l1y) =
207 BLK(ctx->prev, l1x + 1 + mx, l1y + my);
208 BLK(ctx->cur, l1x, l1y + 1) =
209 BLK(ctx->prev, l1x + mx, l1y + 1 + my);
210 BLK(ctx->cur, l1x + 1, l1y + 1) =
211 BLK(ctx->prev, l1x + 1 + mx, l1y + 1 + my);
212 }
213 } else { // read values for block
214 BLK(ctx->cur, l1x, l1y) = *src++;
215 BLK(ctx->cur, l1x + 1, l1y) = *src++;
216 BLK(ctx->cur, l1x, l1y + 1) = *src++;
217 BLK(ctx->cur, l1x + 1, l1y + 1) = *src++;
218 }
219 }
220 }
221 }
222 }
223 }
224 }
225
226 static int decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t * buf,
227 int buf_size)
228 {
229 KmvcContext *const ctx = (KmvcContext *) avctx->priv_data;
230 uint8_t *out, *src;
231 int i;
232 int header;
233 int blocksize;
234
235 if (ctx->pic.data[0])
236 avctx->release_buffer(avctx, &ctx->pic);
237
238 ctx->pic.reference = 1;
239 ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
240 if (avctx->get_buffer(avctx, &ctx->pic) < 0) {
241 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
242 return -1;
243 }
244
245 header = buf[0];
246 buf++;
247
248 if (header & KMVC_KEYFRAME) {
249 ctx->pic.key_frame = 1;
250 ctx->pic.pict_type = FF_I_TYPE;
251 } else {
252 ctx->pic.key_frame = 0;
253 ctx->pic.pict_type = FF_P_TYPE;
254 }
255
256 if (header & KMVC_PALETTE) {
257 ctx->pic.palette_has_changed = 1;
258 // palette starts from index 1 and has 127 entries
259 for (i = 1; i <= ctx->palsize; i++) {
260 ctx->pal[i] = (buf[0] << 16) | (buf[1] << 8) | buf[2];
261 buf += 3;
262 }
263 }
264
265 if (ctx->setpal) {
266 ctx->setpal = 0;
267 ctx->pic.palette_has_changed = 1;
268 }
269
270 /* make the palette available on the way out */
271 memcpy(ctx->pic.data[1], ctx->pal, 1024);
272
273 blocksize = *buf++;
274
275 if (blocksize != 8) {
276 av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize);
277 return -1;
278 }
279 memset(ctx->cur, 0, 320 * 200);
280 switch (header & KMVC_METHOD) {
281 case 3:
282 kmvc_decode_intra_8x8(ctx, buf, avctx->width, avctx->height);
283 break;
284 case 4:
285 kmvc_decode_inter_8x8(ctx, buf, avctx->width, avctx->height);
286 break;
287 default:
288 av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
289 return -1;
290 }
291
292 out = ctx->pic.data[0];
293 src = ctx->cur;
294 for (i = 0; i < avctx->height; i++) {
295 memcpy(out, src, avctx->width);
296 src += 320;
297 out += ctx->pic.linesize[0];
298 }
299
300 /* flip buffers */
301 if (ctx->cur == ctx->frm0) {
302 ctx->cur = ctx->frm1;
303 ctx->prev = ctx->frm0;
304 } else {
305 ctx->cur = ctx->frm0;
306 ctx->prev = ctx->frm1;
307 }
308
309 *data_size = sizeof(AVFrame);
310 *(AVFrame *) data = ctx->pic;
311
312 /* always report that the buffer was completely consumed */
313 return buf_size;
314 }
315
316
317
318 /*
319 * Init kmvc decoder
320 */
321 static int decode_init(AVCodecContext * avctx)
322 {
323 KmvcContext *const c = (KmvcContext *) avctx->priv_data;
324 int i;
325
326 c->avctx = avctx;
327 avctx->has_b_frames = 0;
328
329 c->pic.data[0] = NULL;
330
331 if (avctx->width > 320 || avctx->height > 200) {
332 av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n");
333 return -1;
334 }
335
336 c->frm0 = av_mallocz(320 * 200);
337 c->frm1 = av_mallocz(320 * 200);
338 c->cur = c->frm0;
339 c->prev = c->frm1;
340
341 for (i = 0; i < 256; i++) {
342 c->pal[i] = i * 0x10101;
343 }
344
345 if (avctx->extradata_size < 12) {
346 av_log(NULL, 0, "Extradata missing, decoding may not work properly...\n");
347 c->palsize = 127;
348 } else {
349 c->palsize = LE_16(avctx->extradata + 10);
350 }
351
352 if (avctx->extradata_size == 1036) { // palette in extradata
353 uint8_t *src = avctx->extradata + 12;
354 for (i = 0; i < c->palsize; i++) {
355 c->pal[i] = LE_32(src);
356 src += 4;
357 }
358 c->setpal = 1;
359 }
360
361 avctx->pix_fmt = PIX_FMT_PAL8;
362
363 return 0;
364 }
365
366
367
368 /*
369 * Uninit kmvc decoder
370 */
371 static int decode_end(AVCodecContext * avctx)
372 {
373 KmvcContext *const c = (KmvcContext *) avctx->priv_data;
374
375 if (c->frm0)
376 av_free(c->frm0);
377 if (c->frm1)
378 av_free(c->frm1);
379 if (c->pic.data[0])
380 avctx->release_buffer(avctx, &c->pic);
381
382 return 0;
383 }
384
385 AVCodec kmvc_decoder = {
386 "kmvc",
387 CODEC_TYPE_VIDEO,
388 CODEC_ID_KMVC,
389 sizeof(KmvcContext),
390 decode_init,
391 NULL,
392 decode_end,
393 decode_frame
394 };