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