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