1 /*
2 * KMVC decoder
3 * Copyright (c) 2006 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 /**
23 * @file
24 * Karl Morton's Video Codec decoder
25 */
26
27 #include <stdio.h>
28
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 */
45
53
58
59 #define BLK(data, x, y) data[av_clip((x) + (y) * 320, 0, 320 * 200 -1)]
60
61 #define kmvc_init_getbits(bb, g) bb.bits = 7; bb.bitbuf = bytestream2_get_byte(g);
62
63 #define kmvc_getbit(bb, g, res) {\
64 res = 0; \
65 if (bb.bitbuf & (1 << bb.bits)) res = 1; \
66 bb.bits--; \
67 if(bb.bits == -1) { \
68 bb.bitbuf = bytestream2_get_byte(g); \
69 bb.bits = 7; \
70 } \
71 }
72
74 {
78 int bx, by;
79 int l0x, l1x, l0y, l1y;
80 int mx, my;
81
83
84 for (by = 0; by <
h; by += 8)
85 for (bx = 0; bx <
w; bx += 8) {
89 }
91 if (!res) { // fill whole 8x8 block
92 val = bytestream2_get_byte(&
ctx->g);
93 for (
i = 0;
i < 64;
i++)
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;
100 if (!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) > 320*197 - 4) {
113 }
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);
117 }
118 } else { // descend to 2x2 sub-sub-blocks
119 for (j = 0; j < 4; j++) {
120 l1x = l0x + (j & 1) * 2;
121 l1y = l0y + (j & 2);
123 if (!res) {
125 if (!res) { // fill whole 2x2 block
126 val = bytestream2_get_byte(&
ctx->g);
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) > 320*199 - 2) {
138 }
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);
146 }
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);
152 }
153 }
154 }
155 }
156 }
157 }
158
159 return 0;
160 }
161
163 {
167 int bx, by;
168 int l0x, l1x, l0y, l1y;
169 int mx, my;
170
172
173 for (by = 0; by <
h; by += 8)
174 for (bx = 0; bx <
w; bx += 8) {
176 if (!res) {
178 if (!res) { // fill whole 8x8 block
182 }
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));
190 }
191 } else { // handle four 4x4 subblocks
195 }
196 for (
i = 0;
i < 4;
i++) {
197 l0x = bx + (
i & 1) * 4;
198 l0y = by + (
i & 2) * 2;
200 if (!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) > 320*197 - 4) {
213 }
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);
217 }
218 } else { // descend to 2x2 sub-sub-blocks
219 for (j = 0; j < 4; j++) {
220 l1x = l0x + (j & 1) * 2;
221 l1y = l0y + (j & 2);
223 if (!res) {
225 if (!res) { // fill whole 2x2 block
226 val = bytestream2_get_byte(&
ctx->g);
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) > 320*199 - 2) {
238 }
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);
246 }
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);
252 }
253 }
254 }
255 }
256 }
257 }
258
259 return 0;
260 }
261
264 {
269 int blocksize;
270
272
275
277
279
280 /* blocksize 127 is really palette change event */
281 if (bytestream2_peek_byte(&
ctx->g) == 127) {
283 for (
i = 0;
i < 127;
i++) {
284 ctx->pal[
i + (
header & 0x81)] = 0xFFU << 24 | bytestream2_get_be24(&
ctx->g);
286 }
288 }
289
291 frame->key_frame = 1;
293 } else {
294 frame->key_frame = 0;
296 }
297
299 frame->palette_has_changed = 1;
300 // palette starts from index 1 and has 127 entries
301 for (
i = 1;
i <=
ctx->palsize;
i++) {
302 ctx->pal[
i] = 0xFF
U << 24 | bytestream2_get_be24(&
ctx->g);
303 }
304 }
305
308 frame->palette_has_changed = 1;
309 }
310
311 /* make the palette available on the way out */
312 memcpy(
frame->data[1],
ctx->pal, 1024);
313
314 blocksize = bytestream2_get_byte(&
ctx->g);
315
316 if (blocksize != 8 && blocksize != 127) {
319 }
320 memset(
ctx->cur, 0, 320 * 200);
322 case 0:
323 case 1: // used in palette changed event
324 memcpy(
ctx->cur,
ctx->prev, 320 * 200);
325 break;
326 case 3:
328 break;
329 case 4:
331 break;
332 default:
335 }
336
343 }
344
345 /* flip buffers */
347
348 *got_frame = 1;
349
350 /* always report that the buffer was completely consumed */
352 }
353
354
355
356 /*
357 * Init kmvc decoder
358 */
360 {
363
365
369 }
370
373
374 for (
i = 0;
i < 256;
i++) {
375 c->pal[
i] = 0xFF
U << 24 |
i * 0x10101;
376 }
377
380 "Extradata missing, decoding may not work properly...\n");
382 } else {
388 }
389 }
390
393 for (
i = 0;
i < 256;
i++) {
396 }
398 }
399
401
402 return 0;
403 }
404
414 };