3 * Copyright (c) 2006 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
24 * Karl Morton's Video Codec decoder
31 #include "bytestream.h"
34 #include "libavutil/common.h"
36 #define KMVC_KEYFRAME 0x80
37 #define KMVC_PALETTE 0x40
38 #define KMVC_METHOD 0x0F
39 #define MAX_PALSIZE 256
44 typedef struct KmvcContext {
45 AVCodecContext *avctx;
51 uint32_t pal[MAX_PALSIZE];
52 uint8_t frm0[320 * 200], frm1[320 * 200];
55 typedef struct BitBuf {
60 #define BLK(data, x, y) data[av_clip((x) + (y) * 320, 0, 320 * 200 -1)]
62 #define kmvc_init_getbits(bb, g) bb.bits = 7; bb.bitbuf = bytestream2_get_byte(g);
64 #define kmvc_getbit(bb, g, res) {\
66 if (bb.bitbuf & (1 << bb.bits)) res = 1; \
69 bb.bitbuf = bytestream2_get_byte(g); \
74 static int kmvc_decode_intra_8x8(KmvcContext * ctx, int w, int h)
80 int l0x, l1x, l0y, l1y;
83 kmvc_init_getbits(bb, &ctx->g);
85 for (by = 0; by < h; by += 8)
86 for (bx = 0; bx < w; bx += 8) {
87 if (!bytestream2_get_bytes_left(&ctx->g)) {
88 av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
89 return AVERROR_INVALIDDATA;
91 kmvc_getbit(bb, &ctx->g, res);
92 if (!res) { // fill whole 8x8 block
93 val = bytestream2_get_byte(&ctx->g);
94 for (i = 0; i < 64; i++)
95 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
96 } else { // handle four 4x4 subblocks
97 for (i = 0; i < 4; i++) {
98 l0x = bx + (i & 1) * 4;
99 l0y = by + (i & 2) * 2;
100 kmvc_getbit(bb, &ctx->g, res);
102 kmvc_getbit(bb, &ctx->g, res);
103 if (!res) { // fill whole 4x4 block
104 val = bytestream2_get_byte(&ctx->g);
105 for (j = 0; j < 16; j++)
106 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
107 } else { // copy block from already decoded place
108 val = bytestream2_get_byte(&ctx->g);
111 if ((l0x-mx) + 320*(l0y-my) < 0 || (l0x-mx) + 320*(l0y-my) > 320*197 - 4) {
112 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
113 return AVERROR_INVALIDDATA;
115 for (j = 0; j < 16; j++)
116 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
117 BLK(ctx->cur, l0x + (j & 3) - mx, l0y + (j >> 2) - my);
119 } else { // descend to 2x2 sub-sub-blocks
120 for (j = 0; j < 4; j++) {
121 l1x = l0x + (j & 1) * 2;
123 kmvc_getbit(bb, &ctx->g, res);
125 kmvc_getbit(bb, &ctx->g, res);
126 if (!res) { // fill whole 2x2 block
127 val = bytestream2_get_byte(&ctx->g);
128 BLK(ctx->cur, l1x, l1y) = val;
129 BLK(ctx->cur, l1x + 1, l1y) = val;
130 BLK(ctx->cur, l1x, l1y + 1) = val;
131 BLK(ctx->cur, l1x + 1, l1y + 1) = val;
132 } else { // copy block from already decoded place
133 val = bytestream2_get_byte(&ctx->g);
136 if ((l1x-mx) + 320*(l1y-my) < 0 || (l1x-mx) + 320*(l1y-my) > 320*199 - 2) {
137 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
138 return AVERROR_INVALIDDATA;
140 BLK(ctx->cur, l1x, l1y) = BLK(ctx->cur, l1x - mx, l1y - my);
141 BLK(ctx->cur, l1x + 1, l1y) =
142 BLK(ctx->cur, l1x + 1 - mx, l1y - my);
143 BLK(ctx->cur, l1x, l1y + 1) =
144 BLK(ctx->cur, l1x - mx, l1y + 1 - my);
145 BLK(ctx->cur, l1x + 1, l1y + 1) =
146 BLK(ctx->cur, l1x + 1 - mx, l1y + 1 - my);
148 } else { // read values for block
149 BLK(ctx->cur, l1x, l1y) = bytestream2_get_byte(&ctx->g);
150 BLK(ctx->cur, l1x + 1, l1y) = bytestream2_get_byte(&ctx->g);
151 BLK(ctx->cur, l1x, l1y + 1) = bytestream2_get_byte(&ctx->g);
152 BLK(ctx->cur, l1x + 1, l1y + 1) = bytestream2_get_byte(&ctx->g);
163 static int kmvc_decode_inter_8x8(KmvcContext * ctx, int w, int h)
169 int l0x, l1x, l0y, l1y;
172 kmvc_init_getbits(bb, &ctx->g);
174 for (by = 0; by < h; by += 8)
175 for (bx = 0; bx < w; bx += 8) {
176 kmvc_getbit(bb, &ctx->g, res);
178 kmvc_getbit(bb, &ctx->g, res);
179 if (!res) { // fill whole 8x8 block
180 if (!bytestream2_get_bytes_left(&ctx->g)) {
181 av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
182 return AVERROR_INVALIDDATA;
184 val = bytestream2_get_byte(&ctx->g);
185 for (i = 0; i < 64; i++)
186 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
187 } else { // copy block from previous frame
188 for (i = 0; i < 64; i++)
189 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) =
190 BLK(ctx->prev, bx + (i & 0x7), by + (i >> 3));
192 } else { // handle four 4x4 subblocks
193 if (!bytestream2_get_bytes_left(&ctx->g)) {
194 av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
195 return AVERROR_INVALIDDATA;
197 for (i = 0; i < 4; i++) {
198 l0x = bx + (i & 1) * 4;
199 l0y = by + (i & 2) * 2;
200 kmvc_getbit(bb, &ctx->g, res);
202 kmvc_getbit(bb, &ctx->g, res);
203 if (!res) { // fill whole 4x4 block
204 val = bytestream2_get_byte(&ctx->g);
205 for (j = 0; j < 16; j++)
206 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
207 } else { // copy block
208 val = bytestream2_get_byte(&ctx->g);
209 mx = (val & 0xF) - 8;
211 if ((l0x+mx) + 320*(l0y+my) < 0 || (l0x+mx) + 320*(l0y+my) > 320*197 - 4) {
212 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
213 return AVERROR_INVALIDDATA;
215 for (j = 0; j < 16; j++)
216 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
217 BLK(ctx->prev, l0x + (j & 3) + mx, l0y + (j >> 2) + my);
219 } else { // descend to 2x2 sub-sub-blocks
220 for (j = 0; j < 4; j++) {
221 l1x = l0x + (j & 1) * 2;
223 kmvc_getbit(bb, &ctx->g, res);
225 kmvc_getbit(bb, &ctx->g, res);
226 if (!res) { // fill whole 2x2 block
227 val = bytestream2_get_byte(&ctx->g);
228 BLK(ctx->cur, l1x, l1y) = val;
229 BLK(ctx->cur, l1x + 1, l1y) = val;
230 BLK(ctx->cur, l1x, l1y + 1) = val;
231 BLK(ctx->cur, l1x + 1, l1y + 1) = val;
232 } else { // copy block
233 val = bytestream2_get_byte(&ctx->g);
234 mx = (val & 0xF) - 8;
236 if ((l1x+mx) + 320*(l1y+my) < 0 || (l1x+mx) + 320*(l1y+my) > 320*199 - 2) {
237 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
238 return AVERROR_INVALIDDATA;
240 BLK(ctx->cur, l1x, l1y) = BLK(ctx->prev, l1x + mx, l1y + my);
241 BLK(ctx->cur, l1x + 1, l1y) =
242 BLK(ctx->prev, l1x + 1 + mx, l1y + my);
243 BLK(ctx->cur, l1x, l1y + 1) =
244 BLK(ctx->prev, l1x + mx, l1y + 1 + my);
245 BLK(ctx->cur, l1x + 1, l1y + 1) =
246 BLK(ctx->prev, l1x + 1 + mx, l1y + 1 + my);
248 } else { // read values for block
249 BLK(ctx->cur, l1x, l1y) = bytestream2_get_byte(&ctx->g);
250 BLK(ctx->cur, l1x + 1, l1y) = bytestream2_get_byte(&ctx->g);
251 BLK(ctx->cur, l1x, l1y + 1) = bytestream2_get_byte(&ctx->g);
252 BLK(ctx->cur, l1x + 1, l1y + 1) = bytestream2_get_byte(&ctx->g);
263 static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame,
266 KmvcContext *const ctx = avctx->priv_data;
267 AVFrame *frame = data;
273 bytestream2_init(&ctx->g, avpkt->data, avpkt->size);
275 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
278 frame->palette_has_changed = ff_copy_palette(ctx->pal, avpkt, avctx);
280 header = bytestream2_get_byte(&ctx->g);
282 /* blocksize 127 is really palette change event */
283 if (bytestream2_peek_byte(&ctx->g) == 127) {
284 bytestream2_skip(&ctx->g, 3);
285 for (i = 0; i < 127; i++) {
286 ctx->pal[i + (header & 0x81)] = 0xFFU << 24 | bytestream2_get_be24(&ctx->g);
287 bytestream2_skip(&ctx->g, 1);
289 bytestream2_seek(&ctx->g, -127 * 4 - 3, SEEK_CUR);
292 if (header & KMVC_KEYFRAME) {
293 frame->key_frame = 1;
294 frame->pict_type = AV_PICTURE_TYPE_I;
296 frame->key_frame = 0;
297 frame->pict_type = AV_PICTURE_TYPE_P;
300 if (header & KMVC_PALETTE) {
301 frame->palette_has_changed = 1;
302 // palette starts from index 1 and has 127 entries
303 for (i = 1; i <= ctx->palsize; i++) {
304 ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24(&ctx->g);
310 frame->palette_has_changed = 1;
313 /* make the palette available on the way out */
314 memcpy(frame->data[1], ctx->pal, 1024);
316 blocksize = bytestream2_get_byte(&ctx->g);
318 if (blocksize != 8 && blocksize != 127) {
319 av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize);
320 return AVERROR_INVALIDDATA;
322 memset(ctx->cur, 0, 320 * 200);
323 switch (header & KMVC_METHOD) {
325 case 1: // used in palette changed event
326 memcpy(ctx->cur, ctx->prev, 320 * 200);
329 kmvc_decode_intra_8x8(ctx, avctx->width, avctx->height);
332 kmvc_decode_inter_8x8(ctx, avctx->width, avctx->height);
335 av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
336 return AVERROR_INVALIDDATA;
339 out = frame->data[0];
341 for (i = 0; i < avctx->height; i++) {
342 memcpy(out, src, avctx->width);
344 out += frame->linesize[0];
348 FFSWAP(uint8_t *, ctx->cur, ctx->prev);
352 /* always report that the buffer was completely consumed */
361 static av_cold int decode_init(AVCodecContext * avctx)
363 KmvcContext *const c = avctx->priv_data;
368 if (avctx->width > 320 || avctx->height > 200) {
369 av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n");
370 return AVERROR(EINVAL);
376 for (i = 0; i < 256; i++) {
377 c->pal[i] = 0xFFU << 24 | i * 0x10101;
380 if (avctx->extradata_size < 12) {
381 av_log(avctx, AV_LOG_WARNING,
382 "Extradata missing, decoding may not work properly...\n");
385 c->palsize = AV_RL16(avctx->extradata + 10);
386 if (c->palsize >= (unsigned)MAX_PALSIZE) {
388 av_log(avctx, AV_LOG_ERROR, "KMVC palette too large\n");
389 return AVERROR_INVALIDDATA;
393 if (avctx->extradata_size == 1036) { // palette in extradata
394 uint8_t *src = avctx->extradata + 12;
395 for (i = 0; i < 256; i++) {
396 c->pal[i] = AV_RL32(src);
402 avctx->pix_fmt = AV_PIX_FMT_PAL8;
407 const AVCodec ff_kmvc_decoder = {
409 .long_name = NULL_IF_CONFIG_SMALL("Karl Morton's video codec"),
410 .type = AVMEDIA_TYPE_VIDEO,
411 .id = AV_CODEC_ID_KMVC,
412 .priv_data_size = sizeof(KmvcContext),
414 .decode = decode_frame,
415 .capabilities = AV_CODEC_CAP_DR1,