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 #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;
48 uint32_t pal[MAX_PALSIZE];
54 typedef struct BitBuf {
59 #define BLK(data, x, y) data[(x) + (y) * 320]
61 #define kmvc_init_getbits(bb, g) bb.bits = 7; bb.bitbuf = bytestream2_get_byte(g);
63 #define kmvc_getbit(bb, g, res) {\
65 if (bb.bitbuf & (1 << bb.bits)) res = 1; \
68 bb.bitbuf = bytestream2_get_byte(g); \
73 static int kmvc_decode_intra_8x8(KmvcContext * ctx, int w, int h)
79 int l0x, l1x, l0y, l1y;
82 kmvc_init_getbits(bb, &ctx->g);
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;
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);
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);
110 if ((l0x-mx) + 320*(l0y-my) < 0 || (l0x-mx) + 320*(l0y-my) > 316*196) {
111 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
112 return AVERROR_INVALIDDATA;
114 for (j = 0; j < 16; j++)
115 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
116 BLK(ctx->cur, l0x + (j & 3) - mx, l0y + (j >> 2) - my);
118 } else { // descend to 2x2 sub-sub-blocks
119 for (j = 0; j < 4; j++) {
120 l1x = l0x + (j & 1) * 2;
122 kmvc_getbit(bb, &ctx->g, res);
124 kmvc_getbit(bb, &ctx->g, res);
125 if (!res) { // fill whole 2x2 block
126 val = bytestream2_get_byte(&ctx->g);
127 BLK(ctx->cur, l1x, l1y) = val;
128 BLK(ctx->cur, l1x + 1, l1y) = val;
129 BLK(ctx->cur, l1x, l1y + 1) = val;
130 BLK(ctx->cur, l1x + 1, l1y + 1) = val;
131 } else { // copy block from already decoded place
132 val = bytestream2_get_byte(&ctx->g);
135 if ((l1x-mx) + 320*(l1y-my) < 0 || (l1x-mx) + 320*(l1y-my) > 318*198) {
136 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
137 return AVERROR_INVALIDDATA;
139 BLK(ctx->cur, l1x, l1y) = BLK(ctx->cur, l1x - mx, l1y - my);
140 BLK(ctx->cur, l1x + 1, l1y) =
141 BLK(ctx->cur, l1x + 1 - mx, l1y - my);
142 BLK(ctx->cur, l1x, l1y + 1) =
143 BLK(ctx->cur, l1x - mx, l1y + 1 - my);
144 BLK(ctx->cur, l1x + 1, l1y + 1) =
145 BLK(ctx->cur, l1x + 1 - mx, l1y + 1 - my);
147 } else { // read values for block
148 BLK(ctx->cur, l1x, l1y) = bytestream2_get_byte(&ctx->g);
149 BLK(ctx->cur, l1x + 1, l1y) = bytestream2_get_byte(&ctx->g);
150 BLK(ctx->cur, l1x, l1y + 1) = bytestream2_get_byte(&ctx->g);
151 BLK(ctx->cur, l1x + 1, l1y + 1) = bytestream2_get_byte(&ctx->g);
162 static int kmvc_decode_inter_8x8(KmvcContext * ctx, int w, int h)
168 int l0x, l1x, l0y, l1y;
171 kmvc_init_getbits(bb, &ctx->g);
173 for (by = 0; by < h; by += 8)
174 for (bx = 0; bx < w; bx += 8) {
175 kmvc_getbit(bb, &ctx->g, res);
177 kmvc_getbit(bb, &ctx->g, res);
178 if (!res) { // fill whole 8x8 block
179 if (!bytestream2_get_bytes_left(&ctx->g)) {
180 av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
181 return AVERROR_INVALIDDATA;
183 val = bytestream2_get_byte(&ctx->g);
184 for (i = 0; i < 64; i++)
185 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
186 } else { // copy block from previous frame
187 for (i = 0; i < 64; i++)
188 BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) =
189 BLK(ctx->prev, bx + (i & 0x7), by + (i >> 3));
191 } else { // handle four 4x4 subblocks
192 if (!bytestream2_get_bytes_left(&ctx->g)) {
193 av_log(ctx->avctx, AV_LOG_ERROR, "Data overrun\n");
194 return AVERROR_INVALIDDATA;
196 for (i = 0; i < 4; i++) {
197 l0x = bx + (i & 1) * 4;
198 l0y = by + (i & 2) * 2;
199 kmvc_getbit(bb, &ctx->g, res);
201 kmvc_getbit(bb, &ctx->g, res);
202 if (!res) { // fill whole 4x4 block
203 val = bytestream2_get_byte(&ctx->g);
204 for (j = 0; j < 16; j++)
205 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
206 } else { // copy block
207 val = bytestream2_get_byte(&ctx->g);
208 mx = (val & 0xF) - 8;
210 if ((l0x+mx) + 320*(l0y+my) < 0 || (l0x+mx) + 320*(l0y+my) > 318*198) {
211 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
212 return AVERROR_INVALIDDATA;
214 for (j = 0; j < 16; j++)
215 BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
216 BLK(ctx->prev, l0x + (j & 3) + mx, l0y + (j >> 2) + my);
218 } else { // descend to 2x2 sub-sub-blocks
219 for (j = 0; j < 4; j++) {
220 l1x = l0x + (j & 1) * 2;
222 kmvc_getbit(bb, &ctx->g, res);
224 kmvc_getbit(bb, &ctx->g, res);
225 if (!res) { // fill whole 2x2 block
226 val = bytestream2_get_byte(&ctx->g);
227 BLK(ctx->cur, l1x, l1y) = val;
228 BLK(ctx->cur, l1x + 1, l1y) = val;
229 BLK(ctx->cur, l1x, l1y + 1) = val;
230 BLK(ctx->cur, l1x + 1, l1y + 1) = val;
231 } else { // copy block
232 val = bytestream2_get_byte(&ctx->g);
233 mx = (val & 0xF) - 8;
235 if ((l1x+mx) + 320*(l1y+my) < 0 || (l1x+mx) + 320*(l1y+my) > 318*198) {
236 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid MV\n");
237 return AVERROR_INVALIDDATA;
239 BLK(ctx->cur, l1x, l1y) = BLK(ctx->prev, l1x + mx, l1y + my);
240 BLK(ctx->cur, l1x + 1, l1y) =
241 BLK(ctx->prev, l1x + 1 + mx, l1y + my);
242 BLK(ctx->cur, l1x, l1y + 1) =
243 BLK(ctx->prev, l1x + mx, l1y + 1 + my);
244 BLK(ctx->cur, l1x + 1, l1y + 1) =
245 BLK(ctx->prev, l1x + 1 + mx, l1y + 1 + my);
247 } else { // read values for block
248 BLK(ctx->cur, l1x, l1y) = bytestream2_get_byte(&ctx->g);
249 BLK(ctx->cur, l1x + 1, l1y) = bytestream2_get_byte(&ctx->g);
250 BLK(ctx->cur, l1x, l1y + 1) = bytestream2_get_byte(&ctx->g);
251 BLK(ctx->cur, l1x + 1, l1y + 1) = bytestream2_get_byte(&ctx->g);
262 static int decode_frame(AVCodecContext * avctx, void *data, int *got_frame,
265 KmvcContext *const ctx = avctx->priv_data;
270 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
272 bytestream2_init(&ctx->g, avpkt->data, avpkt->size);
273 if (ctx->pic.data[0])
274 avctx->release_buffer(avctx, &ctx->pic);
276 ctx->pic.reference = 3;
277 ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
278 if ((ret = ff_get_buffer(avctx, &ctx->pic)) < 0) {
279 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
283 header = bytestream2_get_byte(&ctx->g);
285 /* blocksize 127 is really palette change event */
286 if (bytestream2_peek_byte(&ctx->g) == 127) {
287 bytestream2_skip(&ctx->g, 3);
288 for (i = 0; i < 127; i++) {
289 ctx->pal[i + (header & 0x81)] = 0xFFU << 24 | bytestream2_get_be24(&ctx->g);
290 bytestream2_skip(&ctx->g, 1);
292 bytestream2_seek(&ctx->g, -127 * 4 - 3, SEEK_CUR);
295 if (header & KMVC_KEYFRAME) {
296 ctx->pic.key_frame = 1;
297 ctx->pic.pict_type = AV_PICTURE_TYPE_I;
299 ctx->pic.key_frame = 0;
300 ctx->pic.pict_type = AV_PICTURE_TYPE_P;
303 if (header & KMVC_PALETTE) {
304 ctx->pic.palette_has_changed = 1;
305 // palette starts from index 1 and has 127 entries
306 for (i = 1; i <= ctx->palsize; i++) {
307 ctx->pal[i] = 0xFFU << 24 | bytestream2_get_be24(&ctx->g);
312 ctx->pic.palette_has_changed = 1;
313 memcpy(ctx->pal, pal, AVPALETTE_SIZE);
318 ctx->pic.palette_has_changed = 1;
321 /* make the palette available on the way out */
322 memcpy(ctx->pic.data[1], ctx->pal, 1024);
324 blocksize = bytestream2_get_byte(&ctx->g);
326 if (blocksize != 8 && blocksize != 127) {
327 av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize);
328 return AVERROR_INVALIDDATA;
330 memset(ctx->cur, 0, 320 * 200);
331 switch (header & KMVC_METHOD) {
333 case 1: // used in palette changed event
334 memcpy(ctx->cur, ctx->prev, 320 * 200);
337 kmvc_decode_intra_8x8(ctx, avctx->width, avctx->height);
340 kmvc_decode_inter_8x8(ctx, avctx->width, avctx->height);
343 av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
344 return AVERROR_INVALIDDATA;
347 out = ctx->pic.data[0];
349 for (i = 0; i < avctx->height; i++) {
350 memcpy(out, src, avctx->width);
352 out += ctx->pic.linesize[0];
356 if (ctx->cur == ctx->frm0) {
357 ctx->cur = ctx->frm1;
358 ctx->prev = ctx->frm0;
360 ctx->cur = ctx->frm0;
361 ctx->prev = ctx->frm1;
365 *(AVFrame *) data = ctx->pic;
367 /* always report that the buffer was completely consumed */
376 static av_cold int decode_init(AVCodecContext * avctx)
378 KmvcContext *const c = avctx->priv_data;
383 if (avctx->width > 320 || avctx->height > 200) {
384 av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n");
385 return AVERROR(EINVAL);
388 c->frm0 = av_mallocz(320 * 200);
389 c->frm1 = av_mallocz(320 * 200);
393 for (i = 0; i < 256; i++) {
394 c->pal[i] = 0xFFU << 24 | i * 0x10101;
397 if (avctx->extradata_size < 12) {
398 av_log(avctx, AV_LOG_WARNING,
399 "Extradata missing, decoding may not work properly...\n");
402 c->palsize = AV_RL16(avctx->extradata + 10);
403 if (c->palsize >= (unsigned)MAX_PALSIZE) {
405 av_log(avctx, AV_LOG_ERROR, "KMVC palette too large\n");
406 return AVERROR_INVALIDDATA;
410 if (avctx->extradata_size == 1036) { // palette in extradata
411 uint8_t *src = avctx->extradata + 12;
412 for (i = 0; i < 256; i++) {
413 c->pal[i] = AV_RL32(src);
419 avcodec_get_frame_defaults(&c->pic);
420 avctx->pix_fmt = AV_PIX_FMT_PAL8;
428 * Uninit kmvc decoder
430 static av_cold int decode_end(AVCodecContext * avctx)
432 KmvcContext *const c = avctx->priv_data;
437 avctx->release_buffer(avctx, &c->pic);
442 AVCodec ff_kmvc_decoder = {
444 .type = AVMEDIA_TYPE_VIDEO,
445 .id = AV_CODEC_ID_KMVC,
446 .priv_data_size = sizeof(KmvcContext),
449 .decode = decode_frame,
450 .capabilities = CODEC_CAP_DR1,
451 .long_name = NULL_IF_CONFIG_SMALL("Karl Morton's video codec"),