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