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