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