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