Statistics
| Branch: | Revision:

ffmpeg / libavcodec / smc.c @ 11e29a41

History | View | Annotate | Download (17 KB)

1
/*
2
 * Quicktime Graphics (SMC) Video Decoder
3
 * Copyright (C) 2003 the ffmpeg project
4
 *
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.
9
 *
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.
14
 *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 *
19
 */
20

    
21
/**
22
 * @file smc.c
23
 * QT SMC Video Decoder by Mike Melanson (melanson@pcisys.net)
24
 * For more information about the SMC format, visit:
25
 *   http://www.pcisys.net/~melanson/codecs/
26
 *
27
 * The SMC decoder outputs PAL8 colorspace data.
28
 */
29

    
30
#include <stdio.h>
31
#include <stdlib.h>
32
#include <string.h>
33
#include <unistd.h>
34

    
35
#include "common.h"
36
#include "avcodec.h"
37
#include "dsputil.h"
38

    
39
#define CPAIR 2
40
#define CQUAD 4
41
#define COCTET 8
42

    
43
#define COLORS_PER_TABLE 256
44

    
45
typedef struct SmcContext {
46

    
47
    AVCodecContext *avctx;
48
    DSPContext dsp;
49
    AVFrame frame;
50
    AVFrame prev_frame;
51

    
52
    unsigned char *buf;
53
    int size;
54

    
55
    /* SMC color tables */
56
    unsigned char color_pairs[COLORS_PER_TABLE * CPAIR];
57
    unsigned char color_quads[COLORS_PER_TABLE * CQUAD];
58
    unsigned char color_octets[COLORS_PER_TABLE * COCTET];
59

    
60
} SmcContext;
61

    
62
#define BE_16(x)  ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
63
#define BE_32(x)  ((((uint8_t*)(x))[0] << 24) | \
64
                   (((uint8_t*)(x))[1] << 16) | \
65
                   (((uint8_t*)(x))[2] << 8) | \
66
                    ((uint8_t*)(x))[3])
67
#define GET_BLOCK_COUNT() \
68
  (opcode & 0x10) ? (1 + s->buf[stream_ptr++]) : 1 + (opcode & 0x0F);
69

    
70
#define ADVANCE_BLOCK() \
71
{ \
72
    pixel_ptr += 4; \
73
    if (pixel_ptr >= width) \
74
    { \
75
        pixel_ptr = 0; \
76
        row_ptr += stride * 4; \
77
    } \
78
    total_blocks--; \
79
    if (total_blocks < 0) \
80
    { \
81
        printf("warning: block counter just went negative (this should not happen)\n"); \
82
        return; \
83
    } \
84
}
85

    
86
static void smc_decode_stream(SmcContext *s)
87
{
88
    int width = s->avctx->width;
89
    int height = s->avctx->height;
90
    int stride = s->frame.linesize[0];
91
    int i;
92
    int stream_ptr = 0;
93
    int chunk_size;
94
    unsigned char opcode;
95
    int n_blocks;
96
    unsigned int color_flags;
97
    unsigned int color_flags_a;
98
    unsigned int color_flags_b;
99
    unsigned int flag_mask;
100

    
101
    unsigned char *pixels = s->frame.data[0];
102
    unsigned char *prev_pixels = s->prev_frame.data[0];
103

    
104
    int image_size = height * s->frame.linesize[0];
105
    int row_ptr = 0;
106
    int pixel_ptr = 0;
107
    int pixel_x, pixel_y;
108
    int row_inc = stride - 4;
109
    int block_ptr;
110
    int prev_block_ptr;
111
    int prev_block_ptr1, prev_block_ptr2;
112
    int prev_block_flag;
113
    int total_blocks;
114
    int color_table_index;  /* indexes to color pair, quad, or octet tables */
115
    int pixel;
116

    
117
    int color_pair_index = 0;
118
    int color_quad_index = 0;
119
    int color_octet_index = 0;
120

    
121
    /* make the palette available */
122
    memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
123
    if (s->avctx->palctrl->palette_changed) {
124
        s->frame.palette_has_changed = 1;
125
        s->avctx->palctrl->palette_changed = 0;
126
    }
127

    
128
    chunk_size = BE_32(&s->buf[stream_ptr]) & 0x00FFFFFF;
129
    stream_ptr += 4;
130
    if (chunk_size != s->size)
131
        printf("warning: MOV chunk size != encoded chunk size (%d != %d); using MOV chunk size\n",
132
            chunk_size, s->size);
133

    
134
    chunk_size = s->size;
135
    total_blocks = (s->avctx->width * s->avctx->height) / (4 * 4);
136

    
137
    /* traverse through the blocks */
138
    while (total_blocks) {
139
        /* sanity checks */
140
        /* make sure stream ptr hasn't gone out of bounds */
141
        if (stream_ptr > chunk_size) {
142
            printf("SMC decoder just went out of bounds (stream ptr = %d, chunk size = %d)\n",
143
                stream_ptr, chunk_size);
144
            return;
145
        }
146
        /* make sure the row pointer hasn't gone wild */
147
        if (row_ptr >= image_size) {
148
            printf("SMC decoder just went out of bounds (row ptr = %d, height = %d)\n",
149
                row_ptr, image_size);
150
            return;
151
        }
152

    
153
        opcode = s->buf[stream_ptr++];
154
        switch (opcode & 0xF0) {
155
        /* skip n blocks */
156
        case 0x00:
157
        case 0x10:
158
            n_blocks = GET_BLOCK_COUNT();
159
            while (n_blocks--) {
160
                block_ptr = row_ptr + pixel_ptr;
161
                for (pixel_y = 0; pixel_y < 4; pixel_y++) {
162
                    for (pixel_x = 0; pixel_x < 4; pixel_x++) {
163
                        pixels[block_ptr] = prev_pixels[block_ptr];
164
                        block_ptr++;
165
                    }
166
                    block_ptr += row_inc;
167
                }
168
                ADVANCE_BLOCK();
169
            }
170
            break;
171

    
172
        /* repeat last block n times */
173
        case 0x20:
174
        case 0x30:
175
            n_blocks = GET_BLOCK_COUNT();
176

    
177
            /* sanity check */
178
            if ((row_ptr == 0) && (pixel_ptr == 0)) {
179
                printf("encountered repeat block opcode (%02X) but no blocks rendered yet\n",
180
                    opcode & 0xF0);
181
                break;
182
            }
183

    
184
            /* figure out where the previous block started */
185
            if (pixel_ptr == 0)
186
                prev_block_ptr1 = 
187
                    (row_ptr - s->avctx->width * 4) + s->avctx->width - 4;
188
            else
189
                prev_block_ptr1 = row_ptr + pixel_ptr - 4;
190

    
191
            while (n_blocks--) {
192
                block_ptr = row_ptr + pixel_ptr;
193
                prev_block_ptr = prev_block_ptr1;
194
                for (pixel_y = 0; pixel_y < 4; pixel_y++) {
195
                    for (pixel_x = 0; pixel_x < 4; pixel_x++) {
196
                        pixels[block_ptr++] = pixels[prev_block_ptr++];
197
                    }
198
                    block_ptr += row_inc;
199
                    prev_block_ptr += row_inc;
200
                }
201
                ADVANCE_BLOCK();
202
            }
203
            break;
204

    
205
        /* repeat previous pair of blocks n times */
206
        case 0x40:
207
        case 0x50:
208
            n_blocks = GET_BLOCK_COUNT();
209
            n_blocks *= 2;
210

    
211
            /* sanity check */
212
            if ((row_ptr == 0) && (pixel_ptr < 2 * 4)) {
213
                printf("encountered repeat block opcode (%02X) but not enough blocks rendered yet\n",
214
                    opcode & 0xF0);
215
                break;
216
            }
217

    
218
            /* figure out where the previous 2 blocks started */
219
            if (pixel_ptr == 0)
220
                prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + 
221
                    s->avctx->width - 4 * 2;
222
            else if (pixel_ptr == 4)
223
                prev_block_ptr1 = (row_ptr - s->avctx->width * 4) + row_inc;
224
            else
225
                prev_block_ptr1 = row_ptr + pixel_ptr - 4 * 2;
226

    
227
            if (pixel_ptr == 0)
228
                prev_block_ptr2 = (row_ptr - s->avctx->width * 4) + row_inc;
229
            else
230
                prev_block_ptr2 = row_ptr + pixel_ptr - 4;
231

    
232
            prev_block_flag = 0;
233
            while (n_blocks--) {
234
                block_ptr = row_ptr + pixel_ptr;
235
                if (prev_block_flag)
236
                    prev_block_ptr = prev_block_ptr2;
237
                else
238
                    prev_block_ptr = prev_block_ptr1;
239
                prev_block_flag = !prev_block_flag;
240

    
241
                for (pixel_y = 0; pixel_y < 4; pixel_y++) {
242
                    for (pixel_x = 0; pixel_x < 4; pixel_x++) {
243
                        pixels[block_ptr++] = pixels[prev_block_ptr++];
244
                    }
245
                    block_ptr += row_inc;
246
                    prev_block_ptr += row_inc;
247
                }
248
                ADVANCE_BLOCK();
249
            }
250
            break;
251

    
252
        /* 1-color block encoding */
253
        case 0x60:
254
        case 0x70:
255
            n_blocks = GET_BLOCK_COUNT();
256
            pixel = s->buf[stream_ptr++];
257

    
258
            while (n_blocks--) {
259
                block_ptr = row_ptr + pixel_ptr;
260
                for (pixel_y = 0; pixel_y < 4; pixel_y++) {
261
                    for (pixel_x = 0; pixel_x < 4; pixel_x++) {
262
                        pixels[block_ptr++] = pixel;
263
                    }
264
                    block_ptr += row_inc;
265
                }
266
                ADVANCE_BLOCK();
267
            }
268
            break;
269

    
270
        /* 2-color block encoding */
271
        case 0x80:
272
        case 0x90:
273
            n_blocks = (opcode & 0x0F) + 1;
274

    
275
            /* figure out which color pair to use to paint the 2-color block */
276
            if ((opcode & 0xF0) == 0x80) {
277
                /* fetch the next 2 colors from bytestream and store in next
278
                 * available entry in the color pair table */
279
                for (i = 0; i < CPAIR; i++) {
280
                    pixel = s->buf[stream_ptr++];
281
                    color_table_index = CPAIR * color_pair_index + i;
282
                    s->color_pairs[color_table_index] = pixel;
283
                }
284
                /* this is the base index to use for this block */
285
                color_table_index = CPAIR * color_pair_index;
286
                color_pair_index++;
287
                /* wraparound */
288
                if (color_pair_index == COLORS_PER_TABLE)
289
                    color_pair_index = 0;
290
            } else
291
                color_table_index = CPAIR * s->buf[stream_ptr++];
292

    
293
            while (n_blocks--) {
294
                color_flags = BE_16(&s->buf[stream_ptr]);
295
                stream_ptr += 2;
296
                flag_mask = 0x8000;
297
                block_ptr = row_ptr + pixel_ptr;
298
                for (pixel_y = 0; pixel_y < 4; pixel_y++) {
299
                    for (pixel_x = 0; pixel_x < 4; pixel_x++) {
300
                        if (color_flags & flag_mask)
301
                            pixel = color_table_index + 1;
302
                        else
303
                            pixel = color_table_index;
304
                        flag_mask >>= 1;
305
                        pixels[block_ptr++] = s->color_pairs[pixel];
306
                    }
307
                    block_ptr += row_inc;
308
                }
309
                ADVANCE_BLOCK();
310
            }
311
            break;
312

    
313
        /* 4-color block encoding */
314
        case 0xA0:
315
        case 0xB0:
316
            n_blocks = (opcode & 0x0F) + 1;
317

    
318
            /* figure out which color quad to use to paint the 4-color block */
319
            if ((opcode & 0xF0) == 0xA0) {
320
                /* fetch the next 4 colors from bytestream and store in next
321
                 * available entry in the color quad table */
322
                for (i = 0; i < CQUAD; i++) {
323
                    pixel = s->buf[stream_ptr++];
324
                    color_table_index = CQUAD * color_quad_index + i;
325
                    s->color_quads[color_table_index] = pixel;
326
                }
327
                /* this is the base index to use for this block */
328
                color_table_index = CQUAD * color_quad_index;
329
                color_quad_index++;
330
                /* wraparound */
331
                if (color_quad_index == COLORS_PER_TABLE)
332
                    color_quad_index = 0;
333
            } else
334
                color_table_index = CQUAD * s->buf[stream_ptr++];
335

    
336
            while (n_blocks--) {
337
                color_flags = BE_32(&s->buf[stream_ptr]);
338
                stream_ptr += 4;
339
                /* flag mask actually acts as a bit shift count here */
340
                flag_mask = 30;
341
                block_ptr = row_ptr + pixel_ptr;
342
                for (pixel_y = 0; pixel_y < 4; pixel_y++) {
343
                    for (pixel_x = 0; pixel_x < 4; pixel_x++) {
344
                        pixel = color_table_index + 
345
                            ((color_flags >> flag_mask) & 0x03);
346
                        flag_mask -= 2;
347
                        pixels[block_ptr++] = s->color_quads[pixel];
348
                    }
349
                    block_ptr += row_inc;
350
                }
351
                ADVANCE_BLOCK();
352
            }
353
            break;
354

    
355
        /* 8-color block encoding */
356
        case 0xC0:
357
        case 0xD0:
358
            n_blocks = (opcode & 0x0F) + 1;
359

    
360
            /* figure out which color octet to use to paint the 8-color block */
361
            if ((opcode & 0xF0) == 0xC0) {
362
                /* fetch the next 8 colors from bytestream and store in next
363
                 * available entry in the color octet table */
364
                for (i = 0; i < COCTET; i++) {
365
                    pixel = s->buf[stream_ptr++];
366
                    color_table_index = COCTET * color_octet_index + i;
367
                    s->color_octets[color_table_index] = pixel;
368
                }
369
                /* this is the base index to use for this block */
370
                color_table_index = COCTET * color_octet_index;
371
                color_octet_index++;
372
                /* wraparound */
373
                if (color_octet_index == COLORS_PER_TABLE)
374
                    color_octet_index = 0;
375
            } else
376
                color_table_index = COCTET * s->buf[stream_ptr++];
377

    
378
            while (n_blocks--) {
379
                /*
380
                  For this input of 6 hex bytes:
381
                    01 23 45 67 89 AB
382
                  Mangle it to this output:
383
                    flags_a = xx012456, flags_b = xx89A37B
384
                */
385
                /* build the color flags */
386
                color_flags_a = color_flags_b = 0;
387
                color_flags_a =
388
                    (s->buf[stream_ptr + 0] << 16) |
389
                    ((s->buf[stream_ptr + 1] & 0xF0) << 8) |
390
                    ((s->buf[stream_ptr + 2] & 0xF0) << 4) |
391
                    ((s->buf[stream_ptr + 2] & 0x0F) << 4) |
392
                    ((s->buf[stream_ptr + 3] & 0xF0) >> 4);
393
                color_flags_b =
394
                    (s->buf[stream_ptr + 4] << 16) |
395
                    ((s->buf[stream_ptr + 5] & 0xF0) << 8) |
396
                    ((s->buf[stream_ptr + 1] & 0x0F) << 8) |
397
                    ((s->buf[stream_ptr + 3] & 0x0F) << 4) |
398
                    (s->buf[stream_ptr + 5] & 0x0F);
399
                stream_ptr += 6;
400

    
401
                color_flags = color_flags_a;
402
                /* flag mask actually acts as a bit shift count here */
403
                flag_mask = 21;
404
                block_ptr = row_ptr + pixel_ptr;
405
                for (pixel_y = 0; pixel_y < 4; pixel_y++) {
406
                    /* reload flags at third row (iteration pixel_y == 2) */
407
                    if (pixel_y == 2) {
408
                        color_flags = color_flags_b;
409
                        flag_mask = 21;
410
                    }
411
                    for (pixel_x = 0; pixel_x < 4; pixel_x++) {
412
                        pixel = color_table_index + 
413
                            ((color_flags >> flag_mask) & 0x07);
414
                        flag_mask -= 3;
415
                        pixels[block_ptr++] = s->color_octets[pixel];
416
                    }
417
                    block_ptr += row_inc;
418
                }
419
                ADVANCE_BLOCK();
420
            }
421
            break;
422

    
423
        /* 16-color block encoding (every pixel is a different color) */
424
        case 0xE0:
425
            n_blocks = (opcode & 0x0F) + 1;
426

    
427
            while (n_blocks--) {
428
                block_ptr = row_ptr + pixel_ptr;
429
                for (pixel_y = 0; pixel_y < 4; pixel_y++) {
430
                    for (pixel_x = 0; pixel_x < 4; pixel_x++) {
431
                        pixels[block_ptr++] = s->buf[stream_ptr++];
432
                    }
433
                    block_ptr += row_inc;
434
                }
435
                ADVANCE_BLOCK();
436
            }
437
            break;
438

    
439
        case 0xF0:
440
            printf("0xF0 opcode seen in SMC chunk (xine developers would like to know)\n");
441
            break;
442
        }
443
    }
444
}
445

    
446
static int smc_decode_init(AVCodecContext *avctx)
447
{
448
    SmcContext *s = (SmcContext *)avctx->priv_data;
449

    
450
    s->avctx = avctx;
451
    avctx->pix_fmt = PIX_FMT_PAL8;
452
    avctx->has_b_frames = 0;
453
    dsputil_init(&s->dsp, avctx);
454

    
455
    s->frame.data[0] = s->prev_frame.data[0] = NULL;
456

    
457
    return 0;
458
}
459

    
460
static int smc_decode_frame(AVCodecContext *avctx,
461
                             void *data, int *data_size,
462
                             uint8_t *buf, int buf_size)
463
{
464
    SmcContext *s = (SmcContext *)avctx->priv_data;
465

    
466
    s->buf = buf;
467
    s->size = buf_size;
468

    
469
    s->frame.reference = 1;
470
    if (avctx->get_buffer(avctx, &s->frame)) {
471
        printf ("  smc Video: get_buffer() failed\n");
472
        return -1;
473
    }
474

    
475
    smc_decode_stream(s);
476

    
477
    if (s->prev_frame.data[0])
478
        avctx->release_buffer(avctx, &s->prev_frame);
479

    
480
    /* shuffle frames */
481
    s->prev_frame = s->frame;
482

    
483
    *data_size = sizeof(AVFrame);
484
    *(AVFrame*)data = s->frame;
485

    
486
    /* always report that the buffer was completely consumed */
487
    return buf_size;
488
}
489

    
490
static int smc_decode_end(AVCodecContext *avctx)
491
{
492
    SmcContext *s = (SmcContext *)avctx->priv_data;
493

    
494
    if (s->prev_frame.data[0])
495
        avctx->release_buffer(avctx, &s->prev_frame);
496

    
497
    return 0;
498
}
499

    
500
AVCodec smc_decoder = {
501
    "smc",
502
    CODEC_TYPE_VIDEO,
503
    CODEC_ID_SMC,
504
    sizeof(SmcContext),
505
    smc_decode_init,
506
    NULL,
507
    smc_decode_end,
508
    smc_decode_frame,
509
    CODEC_CAP_DR1,
510
};