3 * Copyright (c) 2006 Konstantin Shishkov
5 * This file is part of Libav.
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.
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.
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
24 * Karl Morton's Video Codec decoder
31 #include "bytestream.h"
34 #define KMVC_KEYFRAME 0x80
35 #define KMVC_PALETTE 0x40
36 #define KMVC_METHOD 0x0F
37 #define MAX_PALSIZE 256
42 typedef struct KmvcContext {
43 AVCodecContext *avctx;
47 uint32_t pal[MAX_PALSIZE];
53 typedef struct BitBuf {
58 #define BLK(data, x, y) data[(x) + (y) * 320]
60 #define kmvc_init_getbits(bb, g) bb.bits = 7; bb.bitbuf = bytestream2_get_byte(g);
62 #define kmvc_getbit(bb, g, res) {\
64 if (bb.bitbuf & (1 << bb.bits)) res = 1; \
67 bb.bitbuf = bytestream2_get_byte(g); \
72 static int kmvc_decode_intra_8x8(KmvcContext * ctx, int w, int h)
78 int l0x, l1x, l0y, l1y;
81 kmvc_init_getbits(bb, &ctx->g);
83 for (by = 0; by < h; by += 8)
84 for (bx = 0; bx < w; bx += 8) {
85 if (!bytestream2_get_bytes_left(&ctx->g)) {
86 av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
87 return AVERROR_INVALIDDATA;
89 kmvc_getbit(bb, &ctx->g, res);
90 if (!res) { // fill whole 8x8 block
91 val = bytestream2_get_byte(&ctx->g);
92 for (i = 0; i < 64; i++)
93 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
94 } else { // handle four 4x4 subblocks
95 for (i = 0; i < 4; i++) {
96 l0x = bx + (i & 1) * 4;
97 l0y = by + (i & 2) * 2;
98 kmvc_getbit(bb, &ctx->g, res);
100 kmvc_getbit(bb, &ctx->g, res);
101 if (!res) { // fill whole 4x4 block
102 val = bytestream2_get_byte(&ctx->g);
103 for (j = 0; j < 16; j++)
104 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
105 } else { // copy block from already decoded place
106 val = bytestream2_get_byte(&ctx->g);
109 for (j = 0; j < 16; j++)
110 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
111 BLK(ctx->cur, l0x + (j & 3) - mx, l0y + (j >> 2) - my);
113 } else { // descend to 2x2 sub-sub-blocks
114 for (j = 0; j < 4; j++) {
115 l1x = l0x + (j & 1) * 2;
117 kmvc_getbit(bb, &ctx->g, res);
119 kmvc_getbit(bb, &ctx->g, res);
120 if (!res) { // fill whole 2x2 block
121 val = bytestream2_get_byte(&ctx->g);
122 BLK(ctx->cur, l1x, l1y) = val;
123 BLK(ctx->cur, l1x + 1, l1y) = val;
124 BLK(ctx->cur, l1x, l1y + 1) = val;
125 BLK(ctx->cur, l1x + 1, l1y + 1) = val;
126 } else { // copy block from already decoded place
127 val = bytestream2_get_byte(&ctx->g);
130 BLK(ctx->cur, l1x, l1y) = BLK(ctx->cur, l1x - mx, l1y - my);
131 BLK(ctx->cur, l1x + 1, l1y) =
132 BLK(ctx->cur, l1x + 1 - mx, l1y - my);
133 BLK(ctx->cur, l1x, l1y + 1) =
134 BLK(ctx->cur, l1x - mx, l1y + 1 - my);
135 BLK(ctx->cur, l1x + 1, l1y + 1) =
136 BLK(ctx->cur, l1x + 1 - mx, l1y + 1 - my);
138 } else { // read values for block
139 BLK(ctx->cur, l1x, l1y) = bytestream2_get_byte(&ctx->g);
140 BLK(ctx->cur, l1x + 1, l1y) = bytestream2_get_byte(&ctx->g);
141 BLK(ctx->cur, l1x, l1y + 1) = bytestream2_get_byte(&ctx->g);
142 BLK(ctx->cur, l1x + 1, l1y + 1) = bytestream2_get_byte(&ctx->g);
153 static int kmvc_decode_inter_8x8(KmvcContext * ctx, int w, int h)
159 int l0x, l1x, l0y, l1y;
162 kmvc_init_getbits(bb, &ctx->g);
164 for (by = 0; by < h; by += 8)
165 for (bx = 0; bx < w; bx += 8) {
166 kmvc_getbit(bb, &ctx->g, res);
168 kmvc_getbit(bb, &ctx->g, res);
169 if (!res) { // fill whole 8x8 block
170 if (!bytestream2_get_bytes_left(&ctx->g)) {
171 av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
172 return AVERROR_INVALIDDATA;
174 val = bytestream2_get_byte(&ctx->g);
175 for (i = 0; i < 64; i++)
176 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
177 } else { // copy block from previous frame
178 for (i = 0; i < 64; i++)
179 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) =
180 BLK(ctx->prev, bx + (i & 0x7), by + (i >> 3));
182 } else { // handle four 4x4 subblocks
183 if (!bytestream2_get_bytes_left(&ctx->g)) {
184 av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
185 return AVERROR_INVALIDDATA;
187 for (i = 0; i < 4; i++) {
188 l0x = bx + (i & 1) * 4;
189 l0y = by + (i & 2) * 2;
190 kmvc_getbit(bb, &ctx->g, res);
192 kmvc_getbit(bb, &ctx->g, res);
193 if (!res) { // fill whole 4x4 block
194 val = bytestream2_get_byte(&ctx->g);
195 for (j = 0; j < 16; j++)
196 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
197 } else { // copy block
198 val = bytestream2_get_byte(&ctx->g);
199 mx = (val & 0xF) - 8;
201 for (j = 0; j < 16; j++)
202 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
203 BLK(ctx->prev, l0x + (j & 3) + mx, l0y + (j >> 2) + my);
205 } else { // descend to 2x2 sub-sub-blocks
206 for (j = 0; j < 4; j++) {
207 l1x = l0x + (j & 1) * 2;
209 kmvc_getbit(bb, &ctx->g, res);
211 kmvc_getbit(bb, &ctx->g, res);
212 if (!res) { // fill whole 2x2 block
213 val = bytestream2_get_byte(&ctx->g);
214 BLK(ctx->cur, l1x, l1y) = val;
215 BLK(ctx->cur, l1x + 1, l1y) = val;
216 BLK(ctx->cur, l1x, l1y + 1) = val;
217 BLK(ctx->cur, l1x + 1, l1y + 1) = val;
218 } else { // copy block
219 val = bytestream2_get_byte(&ctx->g);
220 mx = (val & 0xF) - 8;
222 BLK(ctx->cur, l1x, l1y) = BLK(ctx->prev, l1x + mx, l1y + my);
223 BLK(ctx->cur, l1x + 1, l1y) =
224 BLK(ctx->prev, l1x + 1 + mx, l1y + my);
225 BLK(ctx->cur, l1x, l1y + 1) =
226 BLK(ctx->prev, l1x + mx, l1y + 1 + my);
227 BLK(ctx->cur, l1x + 1, l1y + 1) =
228 BLK(ctx->prev, l1x + 1 + mx, l1y + 1 + my);
230 } else { // read values for block
231 BLK(ctx->cur, l1x, l1y) = bytestream2_get_byte(&ctx->g);
232 BLK(ctx->cur, l1x + 1, l1y) = bytestream2_get_byte(&ctx->g);
233 BLK(ctx->cur, l1x, l1y + 1) = bytestream2_get_byte(&ctx->g);
234 BLK(ctx->cur, l1x + 1, l1y + 1) = bytestream2_get_byte(&ctx->g);
245 static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame,
248 KmvcContext *const ctx = avctx->priv_data;
249 AVFrame *frame = data;
254 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
256 bytestream2_init(&ctx->g, avpkt->data, avpkt->size);
258 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
259 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
263 header = bytestream2_get_byte(&ctx->g);
265 /* blocksize 127 is really palette change event */
266 if (bytestream2_peek_byte(&ctx->g) == 127) {
267 bytestream2_skip(&ctx->g, 3);
268 for (i = 0; i < 127; i++) {
269 ctx->pal[i + (header & 0x81)] = bytestream2_get_be24(&ctx->g);
270 bytestream2_skip(&ctx->g, 1);
272 bytestream2_seek(&ctx->g, -127 * 4 - 3, SEEK_CUR);
275 if (header & KMVC_KEYFRAME) {
276 frame->key_frame = 1;
277 frame->pict_type = AV_PICTURE_TYPE_I;
279 frame->key_frame = 0;
280 frame->pict_type = AV_PICTURE_TYPE_P;
283 if (header & KMVC_PALETTE) {
284 frame->palette_has_changed = 1;
285 // palette starts from index 1 and has 127 entries
286 for (i = 1; i <= ctx->palsize; i++) {
287 ctx->pal[i] = bytestream2_get_be24(&ctx->g);
292 frame->palette_has_changed = 1;
293 memcpy(ctx->pal, pal, AVPALETTE_SIZE);
298 frame->palette_has_changed = 1;
301 /* make the palette available on the way out */
302 memcpy(frame->data[1], ctx->pal, 1024);
304 blocksize = bytestream2_get_byte(&ctx->g);
306 if (blocksize != 8 && blocksize != 127) {
307 av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize);
308 return AVERROR_INVALIDDATA;
310 memset(ctx->cur, 0, 320 * 200);
311 switch (header & KMVC_METHOD) {
313 case 1: // used in palette changed event
314 memcpy(ctx->cur, ctx->prev, 320 * 200);
317 kmvc_decode_intra_8x8(ctx, avctx->width, avctx->height);
320 kmvc_decode_inter_8x8(ctx, avctx->width, avctx->height);
323 av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
324 return AVERROR_INVALIDDATA;
327 out = frame->data[0];
329 for (i = 0; i < avctx->height; i++) {
330 memcpy(out, src, avctx->width);
332 out += frame->linesize[0];
336 if (ctx->cur == ctx->frm0) {
337 ctx->cur = ctx->frm1;
338 ctx->prev = ctx->frm0;
340 ctx->cur = ctx->frm0;
341 ctx->prev = ctx->frm1;
346 /* always report that the buffer was completely consumed */
355 static av_cold int decode_init(AVCodecContext * avctx)
357 KmvcContext *const c = avctx->priv_data;
362 if (avctx->width > 320 || avctx->height > 200) {
363 av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n");
364 return AVERROR(EINVAL);
367 c->frm0 = av_mallocz(320 * 200);
368 c->frm1 = av_mallocz(320 * 200);
372 for (i = 0; i < 256; i++) {
373 c->pal[i] = i * 0x10101;
376 if (avctx->extradata_size < 12) {
377 av_log(avctx, AV_LOG_WARNING,
378 "Extradata missing, decoding may not work properly...\n");
381 c->palsize = AV_RL16(avctx->extradata + 10);
382 if (c->palsize >= MAX_PALSIZE) {
383 av_log(avctx, AV_LOG_ERROR, "KMVC palette too large\n");
384 return AVERROR_INVALIDDATA;
388 if (avctx->extradata_size == 1036) { // palette in extradata
389 uint8_t *src = avctx->extradata + 12;
390 for (i = 0; i < 256; i++) {
391 c->pal[i] = AV_RL32(src);
397 avctx->pix_fmt = AV_PIX_FMT_PAL8;
405 * Uninit kmvc decoder
407 static av_cold int decode_end(AVCodecContext * avctx)
409 KmvcContext *const c = avctx->priv_data;
417 AVCodec ff_kmvc_decoder = {
419 .type = AVMEDIA_TYPE_VIDEO,
420 .id = AV_CODEC_ID_KMVC,
421 .priv_data_size = sizeof(KmvcContext),
424 .decode = decode_frame,
425 .capabilities = CODEC_CAP_DR1,
426 .long_name = NULL_IF_CONFIG_SMALL("Karl Morton's video codec"),