3 * Copyright (c) 2006 Konstantin Shishkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Karl Morton's Video Codec decoder
32 #define KMVC_KEYFRAME 0x80
33 #define KMVC_PALETTE 0x40
34 #define KMVC_METHOD 0x0F
39 typedef struct KmvcContext
{
40 AVCodecContext
*avctx
;
50 typedef struct BitBuf
{
55 #define BLK(data, x, y) data[(x) + (y) * 320]
57 #define kmvc_init_getbits(bb, src) bb.bits = 7; bb.bitbuf = *src++;
59 #define kmvc_getbit(bb, src, res) {\
61 if (bb.bitbuf & (1 << bb.bits)) res = 1; \
69 static void kmvc_decode_intra_8x8(KmvcContext
* ctx
, uint8_t * src
, int w
, int h
)
75 int l0x
, l1x
, l0y
, l1y
;
78 kmvc_init_getbits(bb
, src
);
80 for (by
= 0; by
< h
; by
+= 8)
81 for (bx
= 0; bx
< w
; bx
+= 8) {
82 kmvc_getbit(bb
, src
, res
);
83 if (!res
) { // fill whole 8x8 block
85 for (i
= 0; i
< 64; i
++)
86 BLK(ctx
->cur
, bx
+ (i
& 0x7), by
+ (i
>> 3)) = val
;
87 } else { // handle four 4x4 subblocks
88 for (i
= 0; i
< 4; i
++) {
89 l0x
= bx
+ (i
& 1) * 4;
90 l0y
= by
+ (i
& 2) * 2;
91 kmvc_getbit(bb
, src
, res
);
93 kmvc_getbit(bb
, src
, res
);
94 if (!res
) { // fill whole 4x4 block
96 for (j
= 0; j
< 16; j
++)
97 BLK(ctx
->cur
, l0x
+ (j
& 3), l0y
+ (j
>> 2)) = val
;
98 } else { // copy block from already decoded place
102 for (j
= 0; j
< 16; j
++)
103 BLK(ctx
->cur
, l0x
+ (j
& 3), l0y
+ (j
>> 2)) =
104 BLK(ctx
->cur
, l0x
+ (j
& 3) - mx
, l0y
+ (j
>> 2) - my
);
106 } else { // descend to 2x2 sub-sub-blocks
107 for (j
= 0; j
< 4; j
++) {
108 l1x
= l0x
+ (j
& 1) * 2;
110 kmvc_getbit(bb
, src
, res
);
112 kmvc_getbit(bb
, src
, res
);
113 if (!res
) { // fill whole 2x2 block
115 BLK(ctx
->cur
, l1x
, l1y
) = val
;
116 BLK(ctx
->cur
, l1x
+ 1, l1y
) = val
;
117 BLK(ctx
->cur
, l1x
, l1y
+ 1) = val
;
118 BLK(ctx
->cur
, l1x
+ 1, l1y
+ 1) = val
;
119 } else { // copy block from already decoded place
123 BLK(ctx
->cur
, l1x
, l1y
) = BLK(ctx
->cur
, l1x
- mx
, l1y
- my
);
124 BLK(ctx
->cur
, l1x
+ 1, l1y
) =
125 BLK(ctx
->cur
, l1x
+ 1 - mx
, l1y
- my
);
126 BLK(ctx
->cur
, l1x
, l1y
+ 1) =
127 BLK(ctx
->cur
, l1x
- mx
, l1y
+ 1 - my
);
128 BLK(ctx
->cur
, l1x
+ 1, l1y
+ 1) =
129 BLK(ctx
->cur
, l1x
+ 1 - mx
, l1y
+ 1 - my
);
131 } else { // read values for block
132 BLK(ctx
->cur
, l1x
, l1y
) = *src
++;
133 BLK(ctx
->cur
, l1x
+ 1, l1y
) = *src
++;
134 BLK(ctx
->cur
, l1x
, l1y
+ 1) = *src
++;
135 BLK(ctx
->cur
, l1x
+ 1, l1y
+ 1) = *src
++;
144 static void kmvc_decode_inter_8x8(KmvcContext
* ctx
, uint8_t * src
, int w
, int h
)
150 int l0x
, l1x
, l0y
, l1y
;
153 kmvc_init_getbits(bb
, src
);
155 for (by
= 0; by
< h
; by
+= 8)
156 for (bx
= 0; bx
< w
; bx
+= 8) {
157 kmvc_getbit(bb
, src
, res
);
159 kmvc_getbit(bb
, src
, res
);
160 if (!res
) { // fill whole 8x8 block
162 for (i
= 0; i
< 64; i
++)
163 BLK(ctx
->cur
, bx
+ (i
& 0x7), by
+ (i
>> 3)) = val
;
164 } else { // copy block from previous frame
165 for (i
= 0; i
< 64; i
++)
166 BLK(ctx
->cur
, bx
+ (i
& 0x7), by
+ (i
>> 3)) =
167 BLK(ctx
->prev
, bx
+ (i
& 0x7), by
+ (i
>> 3));
169 } else { // handle four 4x4 subblocks
170 for (i
= 0; i
< 4; i
++) {
171 l0x
= bx
+ (i
& 1) * 4;
172 l0y
= by
+ (i
& 2) * 2;
173 kmvc_getbit(bb
, src
, res
);
175 kmvc_getbit(bb
, src
, res
);
176 if (!res
) { // fill whole 4x4 block
178 for (j
= 0; j
< 16; j
++)
179 BLK(ctx
->cur
, l0x
+ (j
& 3), l0y
+ (j
>> 2)) = val
;
180 } else { // copy block
182 mx
= (val
& 0xF) - 8;
184 for (j
= 0; j
< 16; j
++)
185 BLK(ctx
->cur
, l0x
+ (j
& 3), l0y
+ (j
>> 2)) =
186 BLK(ctx
->prev
, l0x
+ (j
& 3) + mx
, l0y
+ (j
>> 2) + my
);
188 } else { // descend to 2x2 sub-sub-blocks
189 for (j
= 0; j
< 4; j
++) {
190 l1x
= l0x
+ (j
& 1) * 2;
192 kmvc_getbit(bb
, src
, res
);
194 kmvc_getbit(bb
, src
, res
);
195 if (!res
) { // fill whole 2x2 block
197 BLK(ctx
->cur
, l1x
, l1y
) = val
;
198 BLK(ctx
->cur
, l1x
+ 1, l1y
) = val
;
199 BLK(ctx
->cur
, l1x
, l1y
+ 1) = val
;
200 BLK(ctx
->cur
, l1x
+ 1, l1y
+ 1) = val
;
201 } else { // copy block
203 mx
= (val
& 0xF) - 8;
205 BLK(ctx
->cur
, l1x
, l1y
) = BLK(ctx
->prev
, l1x
+ mx
, l1y
+ my
);
206 BLK(ctx
->cur
, l1x
+ 1, l1y
) =
207 BLK(ctx
->prev
, l1x
+ 1 + mx
, l1y
+ my
);
208 BLK(ctx
->cur
, l1x
, l1y
+ 1) =
209 BLK(ctx
->prev
, l1x
+ mx
, l1y
+ 1 + my
);
210 BLK(ctx
->cur
, l1x
+ 1, l1y
+ 1) =
211 BLK(ctx
->prev
, l1x
+ 1 + mx
, l1y
+ 1 + my
);
213 } else { // read values for block
214 BLK(ctx
->cur
, l1x
, l1y
) = *src
++;
215 BLK(ctx
->cur
, l1x
+ 1, l1y
) = *src
++;
216 BLK(ctx
->cur
, l1x
, l1y
+ 1) = *src
++;
217 BLK(ctx
->cur
, l1x
+ 1, l1y
+ 1) = *src
++;
226 static int decode_frame(AVCodecContext
* avctx
, void *data
, int *data_size
, uint8_t * buf
,
229 KmvcContext
*const ctx
= (KmvcContext
*) avctx
->priv_data
;
235 if (ctx
->pic
.data
[0])
236 avctx
->release_buffer(avctx
, &ctx
->pic
);
238 ctx
->pic
.reference
= 1;
239 ctx
->pic
.buffer_hints
= FF_BUFFER_HINTS_VALID
;
240 if (avctx
->get_buffer(avctx
, &ctx
->pic
) < 0) {
241 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
247 /* blocksize 127 is really palette change event */
250 for (i
= 0; i
< 127; i
++) {
251 ctx
->pal
[i
+ (header
& 0x81)] = (buf
[0] << 16) | (buf
[1] << 8) | buf
[2];
257 if (header
& KMVC_KEYFRAME
) {
258 ctx
->pic
.key_frame
= 1;
259 ctx
->pic
.pict_type
= FF_I_TYPE
;
261 ctx
->pic
.key_frame
= 0;
262 ctx
->pic
.pict_type
= FF_P_TYPE
;
265 /* if palette has been changed, copy it from palctrl */
266 if (ctx
->avctx
->palctrl
&& ctx
->avctx
->palctrl
->palette_changed
) {
267 memcpy(ctx
->pal
, ctx
->avctx
->palctrl
->palette
, AVPALETTE_SIZE
);
269 ctx
->avctx
->palctrl
->palette_changed
= 0;
272 if (header
& KMVC_PALETTE
) {
273 ctx
->pic
.palette_has_changed
= 1;
274 // palette starts from index 1 and has 127 entries
275 for (i
= 1; i
<= ctx
->palsize
; i
++) {
276 ctx
->pal
[i
] = (buf
[0] << 16) | (buf
[1] << 8) | buf
[2];
283 ctx
->pic
.palette_has_changed
= 1;
286 /* make the palette available on the way out */
287 memcpy(ctx
->pic
.data
[1], ctx
->pal
, 1024);
291 if (blocksize
!= 8 && blocksize
!= 127) {
292 av_log(avctx
, AV_LOG_ERROR
, "Block size = %i\n", blocksize
);
295 memset(ctx
->cur
, 0, 320 * 200);
296 switch (header
& KMVC_METHOD
) {
298 case 1: // used in palette changed event
299 memcpy(ctx
->cur
, ctx
->prev
, 320 * 200);
302 kmvc_decode_intra_8x8(ctx
, buf
, avctx
->width
, avctx
->height
);
305 kmvc_decode_inter_8x8(ctx
, buf
, avctx
->width
, avctx
->height
);
308 av_log(avctx
, AV_LOG_ERROR
, "Unknown compression method %i\n", header
& KMVC_METHOD
);
312 out
= ctx
->pic
.data
[0];
314 for (i
= 0; i
< avctx
->height
; i
++) {
315 memcpy(out
, src
, avctx
->width
);
317 out
+= ctx
->pic
.linesize
[0];
321 if (ctx
->cur
== ctx
->frm0
) {
322 ctx
->cur
= ctx
->frm1
;
323 ctx
->prev
= ctx
->frm0
;
325 ctx
->cur
= ctx
->frm0
;
326 ctx
->prev
= ctx
->frm1
;
329 *data_size
= sizeof(AVFrame
);
330 *(AVFrame
*) data
= ctx
->pic
;
332 /* always report that the buffer was completely consumed */
341 static int decode_init(AVCodecContext
* avctx
)
343 KmvcContext
*const c
= (KmvcContext
*) avctx
->priv_data
;
347 avctx
->has_b_frames
= 0;
349 c
->pic
.data
[0] = NULL
;
351 if (avctx
->width
> 320 || avctx
->height
> 200) {
352 av_log(avctx
, AV_LOG_ERROR
, "KMVC supports frames <= 320x200\n");
356 c
->frm0
= av_mallocz(320 * 200);
357 c
->frm1
= av_mallocz(320 * 200);
361 for (i
= 0; i
< 256; i
++) {
362 c
->pal
[i
] = i
* 0x10101;
365 if (avctx
->extradata_size
< 12) {
366 av_log(NULL
, 0, "Extradata missing, decoding may not work properly...\n");
369 c
->palsize
= LE_16(avctx
->extradata
+ 10);
372 if (avctx
->extradata_size
== 1036) { // palette in extradata
373 uint8_t *src
= avctx
->extradata
+ 12;
374 for (i
= 0; i
< 256; i
++) {
375 c
->pal
[i
] = LE_32(src
);
379 if (c
->avctx
->palctrl
) {
380 c
->avctx
->palctrl
->palette_changed
= 0;
384 avctx
->pix_fmt
= PIX_FMT_PAL8
;
392 * Uninit kmvc decoder
394 static int decode_end(AVCodecContext
* avctx
)
396 KmvcContext
*const c
= (KmvcContext
*) avctx
->priv_data
;
403 avctx
->release_buffer(avctx
, &c
->pic
);
408 AVCodec kmvc_decoder
= {