Statistics
| Branch: | Revision:

ffmpeg / libavcodec / kmvc.c @ ce5e49b0

History | View | Annotate | Download (13.4 KB)

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

    
37
/*
38
 * Decoder context
39
 */
40
typedef struct KmvcContext {
41
    AVCodecContext *avctx;
42
    AVFrame pic;
43

    
44
    int setpal;
45
    int palsize;
46
    uint32_t pal[256];
47
    uint8_t *cur, *prev;
48
    uint8_t *frm0, *frm1;
49
} KmvcContext;
50

    
51
typedef struct BitBuf {
52
    int bits;
53
    int bitbuf;
54
} BitBuf;
55

    
56
#define BLK(data, x, y)  data[(x) + (y) * 320]
57

    
58
#define kmvc_init_getbits(bb, src)  bb.bits = 7; bb.bitbuf = *src++;
59

    
60
#define kmvc_getbit(bb, src, res) {\
61
    res = 0; \
62
    if (bb.bitbuf & (1 << bb.bits)) res = 1; \
63
    bb.bits--; \
64
    if(bb.bits == -1) { \
65
        bb.bitbuf = *src++; \
66
        bb.bits = 7; \
67
    } \
68
}
69

    
70
static void kmvc_decode_intra_8x8(KmvcContext * ctx, const uint8_t * src, int w, int h)
71
{
72
    BitBuf bb;
73
    int res, val;
74
    int i, j;
75
    int bx, by;
76
    int l0x, l1x, l0y, l1y;
77
    int mx, my;
78

    
79
    kmvc_init_getbits(bb, src);
80

    
81
    for (by = 0; by < h; by += 8)
82
        for (bx = 0; bx < w; bx += 8) {
83
            kmvc_getbit(bb, src, res);
84
            if (!res) {         // fill whole 8x8 block
85
                val = *src++;
86
                for (i = 0; i < 64; i++)
87
                    BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
88
            } else {            // handle four 4x4 subblocks
89
                for (i = 0; i < 4; i++) {
90
                    l0x = bx + (i & 1) * 4;
91
                    l0y = by + (i & 2) * 2;
92
                    kmvc_getbit(bb, src, res);
93
                    if (!res) {
94
                        kmvc_getbit(bb, src, res);
95
                        if (!res) {     // fill whole 4x4 block
96
                            val = *src++;
97
                            for (j = 0; j < 16; j++)
98
                                BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
99
                        } else {        // copy block from already decoded place
100
                            val = *src++;
101
                            mx = val & 0xF;
102
                            my = val >> 4;
103
                            for (j = 0; j < 16; j++)
104
                                BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
105
                                    BLK(ctx->cur, l0x + (j & 3) - mx, l0y + (j >> 2) - my);
106
                        }
107
                    } else {    // descend to 2x2 sub-sub-blocks
108
                        for (j = 0; j < 4; j++) {
109
                            l1x = l0x + (j & 1) * 2;
110
                            l1y = l0y + (j & 2);
111
                            kmvc_getbit(bb, src, res);
112
                            if (!res) {
113
                                kmvc_getbit(bb, src, res);
114
                                if (!res) {     // fill whole 2x2 block
115
                                    val = *src++;
116
                                    BLK(ctx->cur, l1x, l1y) = val;
117
                                    BLK(ctx->cur, l1x + 1, l1y) = val;
118
                                    BLK(ctx->cur, l1x, l1y + 1) = val;
119
                                    BLK(ctx->cur, l1x + 1, l1y + 1) = val;
120
                                } else {        // copy block from already decoded place
121
                                    val = *src++;
122
                                    mx = val & 0xF;
123
                                    my = val >> 4;
124
                                    BLK(ctx->cur, l1x, l1y) = BLK(ctx->cur, l1x - mx, l1y - my);
125
                                    BLK(ctx->cur, l1x + 1, l1y) =
126
                                        BLK(ctx->cur, l1x + 1 - mx, l1y - my);
127
                                    BLK(ctx->cur, l1x, l1y + 1) =
128
                                        BLK(ctx->cur, l1x - mx, l1y + 1 - my);
129
                                    BLK(ctx->cur, l1x + 1, l1y + 1) =
130
                                        BLK(ctx->cur, l1x + 1 - mx, l1y + 1 - my);
131
                                }
132
                            } else {    // read values for block
133
                                BLK(ctx->cur, l1x, l1y) = *src++;
134
                                BLK(ctx->cur, l1x + 1, l1y) = *src++;
135
                                BLK(ctx->cur, l1x, l1y + 1) = *src++;
136
                                BLK(ctx->cur, l1x + 1, l1y + 1) = *src++;
137
                            }
138
                        }
139
                    }
140
                }
141
            }
142
        }
143
}
144

    
145
static void kmvc_decode_inter_8x8(KmvcContext * ctx, const uint8_t * src, int w, int h)
146
{
147
    BitBuf bb;
148
    int res, val;
149
    int i, j;
150
    int bx, by;
151
    int l0x, l1x, l0y, l1y;
152
    int mx, my;
153

    
154
    kmvc_init_getbits(bb, src);
155

    
156
    for (by = 0; by < h; by += 8)
157
        for (bx = 0; bx < w; bx += 8) {
158
            kmvc_getbit(bb, src, res);
159
            if (!res) {
160
                kmvc_getbit(bb, src, res);
161
                if (!res) {     // fill whole 8x8 block
162
                    val = *src++;
163
                    for (i = 0; i < 64; i++)
164
                        BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) = val;
165
                } else {        // copy block from previous frame
166
                    for (i = 0; i < 64; i++)
167
                        BLK(ctx->cur, bx + (i & 0x7), by + (i >> 3)) =
168
                            BLK(ctx->prev, bx + (i & 0x7), by + (i >> 3));
169
                }
170
            } else {            // handle four 4x4 subblocks
171
                for (i = 0; i < 4; i++) {
172
                    l0x = bx + (i & 1) * 4;
173
                    l0y = by + (i & 2) * 2;
174
                    kmvc_getbit(bb, src, res);
175
                    if (!res) {
176
                        kmvc_getbit(bb, src, res);
177
                        if (!res) {     // fill whole 4x4 block
178
                            val = *src++;
179
                            for (j = 0; j < 16; j++)
180
                                BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) = val;
181
                        } else {        // copy block
182
                            val = *src++;
183
                            mx = (val & 0xF) - 8;
184
                            my = (val >> 4) - 8;
185
                            for (j = 0; j < 16; j++)
186
                                BLK(ctx->cur, l0x + (j & 3), l0y + (j >> 2)) =
187
                                    BLK(ctx->prev, l0x + (j & 3) + mx, l0y + (j >> 2) + my);
188
                        }
189
                    } else {    // descend to 2x2 sub-sub-blocks
190
                        for (j = 0; j < 4; j++) {
191
                            l1x = l0x + (j & 1) * 2;
192
                            l1y = l0y + (j & 2);
193
                            kmvc_getbit(bb, src, res);
194
                            if (!res) {
195
                                kmvc_getbit(bb, src, res);
196
                                if (!res) {     // fill whole 2x2 block
197
                                    val = *src++;
198
                                    BLK(ctx->cur, l1x, l1y) = val;
199
                                    BLK(ctx->cur, l1x + 1, l1y) = val;
200
                                    BLK(ctx->cur, l1x, l1y + 1) = val;
201
                                    BLK(ctx->cur, l1x + 1, l1y + 1) = val;
202
                                } else {        // copy block
203
                                    val = *src++;
204
                                    mx = (val & 0xF) - 8;
205
                                    my = (val >> 4) - 8;
206
                                    BLK(ctx->cur, l1x, l1y) = BLK(ctx->prev, l1x + mx, l1y + my);
207
                                    BLK(ctx->cur, l1x + 1, l1y) =
208
                                        BLK(ctx->prev, l1x + 1 + mx, l1y + my);
209
                                    BLK(ctx->cur, l1x, l1y + 1) =
210
                                        BLK(ctx->prev, l1x + mx, l1y + 1 + my);
211
                                    BLK(ctx->cur, l1x + 1, l1y + 1) =
212
                                        BLK(ctx->prev, l1x + 1 + mx, l1y + 1 + my);
213
                                }
214
                            } else {    // read values for block
215
                                BLK(ctx->cur, l1x, l1y) = *src++;
216
                                BLK(ctx->cur, l1x + 1, l1y) = *src++;
217
                                BLK(ctx->cur, l1x, l1y + 1) = *src++;
218
                                BLK(ctx->cur, l1x + 1, l1y + 1) = *src++;
219
                            }
220
                        }
221
                    }
222
                }
223
            }
224
        }
225
}
226

    
227
static int decode_frame(AVCodecContext * avctx, void *data, int *data_size, AVPacket *avpkt)
228
{
229
    const uint8_t *buf = avpkt->data;
230
    int buf_size = avpkt->size;
231
    KmvcContext *const ctx = avctx->priv_data;
232
    uint8_t *out, *src;
233
    int i;
234
    int header;
235
    int blocksize;
236
    const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
237

    
238
    if (ctx->pic.data[0])
239
        avctx->release_buffer(avctx, &ctx->pic);
240

    
241
    ctx->pic.reference = 1;
242
    ctx->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
243
    if (avctx->get_buffer(avctx, &ctx->pic) < 0) {
244
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
245
        return -1;
246
    }
247

    
248
    header = *buf++;
249

    
250
    /* blocksize 127 is really palette change event */
251
    if (buf[0] == 127) {
252
        buf += 3;
253
        for (i = 0; i < 127; i++) {
254
            ctx->pal[i + (header & 0x81)] = AV_RB24(buf);
255
            buf += 4;
256
        }
257
        buf -= 127 * 4 + 3;
258
    }
259

    
260
    if (header & KMVC_KEYFRAME) {
261
        ctx->pic.key_frame = 1;
262
        ctx->pic.pict_type = AV_PICTURE_TYPE_I;
263
    } else {
264
        ctx->pic.key_frame = 0;
265
        ctx->pic.pict_type = AV_PICTURE_TYPE_P;
266
    }
267

    
268
    if (header & KMVC_PALETTE) {
269
        ctx->pic.palette_has_changed = 1;
270
        // palette starts from index 1 and has 127 entries
271
        for (i = 1; i <= ctx->palsize; i++) {
272
            ctx->pal[i] = bytestream_get_be24(&buf);
273
        }
274
    }
275

    
276
    if (pal) {
277
        ctx->pic.palette_has_changed = 1;
278
        memcpy(ctx->pal, pal, AVPALETTE_SIZE);
279
    }
280

    
281
    if (ctx->setpal) {
282
        ctx->setpal = 0;
283
        ctx->pic.palette_has_changed = 1;
284
    }
285

    
286
    /* make the palette available on the way out */
287
    memcpy(ctx->pic.data[1], ctx->pal, 1024);
288

    
289
    blocksize = *buf++;
290

    
291
    if (blocksize != 8 && blocksize != 127) {
292
        av_log(avctx, AV_LOG_ERROR, "Block size = %i\n", blocksize);
293
        return -1;
294
    }
295
    memset(ctx->cur, 0, 320 * 200);
296
    switch (header & KMVC_METHOD) {
297
    case 0:
298
    case 1: // used in palette changed event
299
        memcpy(ctx->cur, ctx->prev, 320 * 200);
300
        break;
301
    case 3:
302
        kmvc_decode_intra_8x8(ctx, buf, avctx->width, avctx->height);
303
        break;
304
    case 4:
305
        kmvc_decode_inter_8x8(ctx, buf, avctx->width, avctx->height);
306
        break;
307
    default:
308
        av_log(avctx, AV_LOG_ERROR, "Unknown compression method %i\n", header & KMVC_METHOD);
309
        return -1;
310
    }
311

    
312
    out = ctx->pic.data[0];
313
    src = ctx->cur;
314
    for (i = 0; i < avctx->height; i++) {
315
        memcpy(out, src, avctx->width);
316
        src += 320;
317
        out += ctx->pic.linesize[0];
318
    }
319

    
320
    /* flip buffers */
321
    if (ctx->cur == ctx->frm0) {
322
        ctx->cur = ctx->frm1;
323
        ctx->prev = ctx->frm0;
324
    } else {
325
        ctx->cur = ctx->frm0;
326
        ctx->prev = ctx->frm1;
327
    }
328

    
329
    *data_size = sizeof(AVFrame);
330
    *(AVFrame *) data = ctx->pic;
331

    
332
    /* always report that the buffer was completely consumed */
333
    return buf_size;
334
}
335

    
336

    
337

    
338
/*
339
 * Init kmvc decoder
340
 */
341
static av_cold int decode_init(AVCodecContext * avctx)
342
{
343
    KmvcContext *const c = avctx->priv_data;
344
    int i;
345

    
346
    c->avctx = avctx;
347

    
348
    if (avctx->width > 320 || avctx->height > 200) {
349
        av_log(avctx, AV_LOG_ERROR, "KMVC supports frames <= 320x200\n");
350
        return -1;
351
    }
352

    
353
    c->frm0 = av_mallocz(320 * 200);
354
    c->frm1 = av_mallocz(320 * 200);
355
    c->cur = c->frm0;
356
    c->prev = c->frm1;
357

    
358
    for (i = 0; i < 256; i++) {
359
        c->pal[i] = i * 0x10101;
360
    }
361

    
362
    if (avctx->extradata_size < 12) {
363
        av_log(NULL, 0, "Extradata missing, decoding may not work properly...\n");
364
        c->palsize = 127;
365
    } else {
366
        c->palsize = AV_RL16(avctx->extradata + 10);
367
    }
368

    
369
    if (avctx->extradata_size == 1036) {        // palette in extradata
370
        uint8_t *src = avctx->extradata + 12;
371
        for (i = 0; i < 256; i++) {
372
            c->pal[i] = AV_RL32(src);
373
            src += 4;
374
        }
375
        c->setpal = 1;
376
    }
377

    
378
    avctx->pix_fmt = PIX_FMT_PAL8;
379

    
380
    return 0;
381
}
382

    
383

    
384

    
385
/*
386
 * Uninit kmvc decoder
387
 */
388
static av_cold int decode_end(AVCodecContext * avctx)
389
{
390
    KmvcContext *const c = avctx->priv_data;
391

    
392
    av_freep(&c->frm0);
393
    av_freep(&c->frm1);
394
    if (c->pic.data[0])
395
        avctx->release_buffer(avctx, &c->pic);
396

    
397
    return 0;
398
}
399

    
400
AVCodec ff_kmvc_decoder = {
401
    "kmvc",
402
    AVMEDIA_TYPE_VIDEO,
403
    CODEC_ID_KMVC,
404
    sizeof(KmvcContext),
405
    decode_init,
406
    NULL,
407
    decode_end,
408
    decode_frame,
409
    CODEC_CAP_DR1,
410
    .long_name = NULL_IF_CONFIG_SMALL("Karl Morton's video codec"),
411
};