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];
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 *got_frame,
248  AVPacket *avpkt)
249 {
250  KmvcContext *const ctx = avctx->priv_data;
251  uint8_t *out, *src;
252  int i;
253  int header;
254  int blocksize;
256 
257  bytestream2_init(&ctx->g, avpkt->data, avpkt->size);
258  if (ctx->pic.data[0])
259  avctx->release_buffer(avctx, &ctx->pic);
260 
261  ctx->pic.reference = 1;
263  if (ff_get_buffer(avctx, &ctx->pic) < 0) {
264  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
265  return -1;
266  }
267 
268  header = bytestream2_get_byte(&ctx->g);
269 
270  /* blocksize 127 is really palette change event */
271  if (bytestream2_peek_byte(&ctx->g) == 127) {
272  bytestream2_skip(&ctx->g, 3);
273  for (i = 0; i < 127; i++) {
274  ctx->pal[i + (header & 0x81)] = bytestream2_get_be24(&ctx->g);
275  bytestream2_skip(&ctx->g, 1);
276  }
277  bytestream2_seek(&ctx->g, -127 * 4 - 3, SEEK_CUR);
278  }
279 
280  if (header & KMVC_KEYFRAME) {
281  ctx->pic.key_frame = 1;
283  } else {
284  ctx->pic.key_frame = 0;
286  }
287 
288  if (header & KMVC_PALETTE) {
289  ctx->pic.palette_has_changed = 1;
290  // palette starts from index 1 and has 127 entries
291  for (i = 1; i <= ctx->palsize; i++) {
292  ctx->pal[i] = bytestream2_get_be24(&ctx->g);
293  }
294  }
295 
296  if (pal) {
297  ctx->pic.palette_has_changed = 1;
298  memcpy(ctx->pal, pal, AVPALETTE_SIZE);
299  }
300 
301  if (ctx->setpal) {
302  ctx->setpal = 0;
303  ctx->pic.palette_has_changed = 1;
304  }
305 
306  /* make the palette available on the way out */
307  memcpy(ctx->pic.data[1], ctx->pal, 1024);
308 
309  blocksize = bytestream2_get_byte(&ctx->g);
310 
311  if (blocksize != 8 && blocksize != 127) {
312  av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize);
313  return -1;
314  }
315  memset(ctx->cur, 0, 320 * 200);
316  switch (header & KMVC_METHOD) {
317  case 0:
318  case 1: // used in palette changed event
319  memcpy(ctx->cur, ctx->prev, 320 * 200);
320  break;
321  case 3:
322  kmvc_decode_intra_8x8(ctx, avctx->width, avctx->height);
323  break;
324  case 4:
325  kmvc_decode_inter_8x8(ctx, avctx->width, avctx->height);
326  break;
327  default:
328  av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
329  return -1;
330  }
331 
332  out = ctx->pic.data[0];
333  src = ctx->cur;
334  for (i = 0; i < avctx->height; i++) {
335  memcpy(out, src, avctx->width);
336  src += 320;
337  out += ctx->pic.linesize[0];
338  }
339 
340  /* flip buffers */
341  if (ctx->cur == ctx->frm0) {
342  ctx->cur = ctx->frm1;
343  ctx->prev = ctx->frm0;
344  } else {
345  ctx->cur = ctx->frm0;
346  ctx->prev = ctx->frm1;
347  }
348 
349  *got_frame = 1;
350  *(AVFrame *) data = ctx->pic;
351 
352  /* always report that the buffer was completely consumed */
353  return avpkt->size;
354 }
355 
356 
357 
358 /*
359  * Init kmvc decoder
360  */
361 static av_cold int decode_init(AVCodecContext * avctx)
362 {
363  KmvcContext *const c = avctx->priv_data;
364  int i;
365 
366  c->avctx = avctx;
367 
368  if (avctx->width > 320 || avctx->height > 200) {
369  av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n");
370  return -1;
371  }
372 
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 
407  .name = "kmvc",
408  .type = AVMEDIA_TYPE_VIDEO,
409  .id = AV_CODEC_ID_KMVC,
410  .priv_data_size = sizeof(KmvcContext),
411  .init = decode_init,
412  .decode = decode_frame,
413  .capabilities = CODEC_CAP_DR1,
414  .long_name = NULL_IF_CONFIG_SMALL("Karl Morton's video codec"),
415 };