Statistics
| Branch: | Revision:

ffmpeg / libavcodec / tscc.c @ 2029f312

History | View | Annotate | Download (9.35 KB)

1
/*
2
 * TechSmith Camtasia decoder
3
 * Copyright (c) 2004 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
/**
24
 * @file tscc.c
25
 * TechSmith Camtasia decoder
26
 *
27
 * Fourcc: TSCC
28
 *
29
 * Codec is very simple:
30
 *  it codes picture (picture difference, really)
31
 *  with algorithm almost identical to Windows RLE8,
32
 *  only without padding and with greater pixel sizes,
33
 *  then this coded picture is packed with ZLib
34
 *
35
 * Supports: BGR8,BGR555,BGR24 - only BGR8 and BGR555 tested
36
 *
37
 */
38

    
39
#include <stdio.h>
40
#include <stdlib.h>
41

    
42
#include "avcodec.h"
43

    
44
#ifdef CONFIG_ZLIB
45
#include <zlib.h>
46
#endif
47

    
48

    
49
/*
50
 * Decoder context
51
 */
52
typedef struct TsccContext {
53

    
54
    AVCodecContext *avctx;
55
    AVFrame pic;
56

    
57
    // Bits per pixel
58
    int bpp;
59
    // Decompressed data size
60
    unsigned int decomp_size;
61
    // Decompression buffer
62
    unsigned char* decomp_buf;
63
    int height;
64
#ifdef CONFIG_ZLIB
65
    z_stream zstream;
66
#endif
67
} CamtasiaContext;
68

    
69
/*
70
 *
71
 * Decode RLE - almost identical to Windows BMP RLE8
72
 *              and enhanced to bigger color depths
73
 *
74
 */
75

    
76
static int decode_rle(CamtasiaContext *c, unsigned int srcsize)
77
{
78
    unsigned char *src = c->decomp_buf;
79
    unsigned char *output, *output_end;
80
    int p1, p2, line=c->height, pos=0, i;
81
    uint16_t pix16;
82
    uint32_t pix32;
83

    
84
    output = c->pic.data[0] + (c->height - 1) * c->pic.linesize[0];
85
    output_end = c->pic.data[0] + (c->height) * c->pic.linesize[0];
86
    while(src < c->decomp_buf + srcsize) {
87
        p1 = *src++;
88
        if(p1 == 0) { //Escape code
89
            p2 = *src++;
90
            if(p2 == 0) { //End-of-line
91
                output = c->pic.data[0] + (--line) * c->pic.linesize[0];
92
                if (line < 0)
93
                    return -1;
94
                pos = 0;
95
                continue;
96
            } else if(p2 == 1) { //End-of-picture
97
                return 0;
98
            } else if(p2 == 2) { //Skip
99
                p1 = *src++;
100
                p2 = *src++;
101
                line -= p2;
102
                if (line < 0)
103
                    return -1;
104
                pos += p1;
105
                output = c->pic.data[0] + line * c->pic.linesize[0] + pos * (c->bpp / 8);
106
                continue;
107
            }
108
            // Copy data
109
            if (output + p2 * (c->bpp / 8) > output_end) {
110
                src += p2 * (c->bpp / 8);
111
                continue;
112
            }
113
            if ((c->bpp == 8) || (c->bpp == 24)) {
114
                for(i = 0; i < p2 * (c->bpp / 8); i++) {
115
                    *output++ = *src++;
116
                }
117
                // RLE8 copy is actually padded - and runs are not!
118
                if(c->bpp == 8 && (p2 & 1)) {
119
                    src++;
120
                }
121
            } else if (c->bpp == 16) {
122
                for(i = 0; i < p2; i++) {
123
                    pix16 = AV_RL16(src);
124
                    src += 2;
125
                    *(uint16_t*)output = pix16;
126
                    output += 2;
127
                }
128
            } else if (c->bpp == 32) {
129
                for(i = 0; i < p2; i++) {
130
                    pix32 = AV_RL32(src);
131
                    src += 4;
132
                    *(uint32_t*)output = pix32;
133
                    output += 4;
134
                }
135
            }
136
            pos += p2;
137
        } else { //Run of pixels
138
            int pix[4]; //original pixel
139
            switch(c->bpp){
140
            case  8: pix[0] = *src++;
141
                     break;
142
            case 16: pix16 = AV_RL16(src);
143
                     src += 2;
144
                     *(uint16_t*)pix = pix16;
145
                     break;
146
            case 24: pix[0] = *src++;
147
                     pix[1] = *src++;
148
                     pix[2] = *src++;
149
                     break;
150
            case 32: pix32 = AV_RL32(src);
151
                     src += 4;
152
                     *(uint32_t*)pix = pix32;
153
                     break;
154
            }
155
            if (output + p1 * (c->bpp / 8) > output_end)
156
                continue;
157
            for(i = 0; i < p1; i++) {
158
                switch(c->bpp){
159
                case  8: *output++ = pix[0];
160
                         break;
161
                case 16: *(uint16_t*)output = pix16;
162
                         output += 2;
163
                         break;
164
                case 24: *output++ = pix[0];
165
                         *output++ = pix[1];
166
                         *output++ = pix[2];
167
                         break;
168
                case 32: *(uint32_t*)output = pix32;
169
                         output += 4;
170
                         break;
171
                }
172
            }
173
            pos += p1;
174
        }
175
    }
176

    
177
    av_log(c->avctx, AV_LOG_ERROR, "Camtasia warning: no End-of-picture code\n");
178
    return 1;
179
}
180

    
181
/*
182
 *
183
 * Decode a frame
184
 *
185
 */
186
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
187
{
188
    CamtasiaContext * const c = avctx->priv_data;
189
    unsigned char *encoded = (unsigned char *)buf;
190
    unsigned char *outptr;
191
#ifdef CONFIG_ZLIB
192
    int zret; // Zlib return code
193
#endif
194
    int len = buf_size;
195

    
196
    if(c->pic.data[0])
197
            avctx->release_buffer(avctx, &c->pic);
198

    
199
    c->pic.reference = 1;
200
    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
201
    if(avctx->get_buffer(avctx, &c->pic) < 0){
202
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
203
        return -1;
204
    }
205

    
206
    outptr = c->pic.data[0]; // Output image pointer
207

    
208
#ifdef CONFIG_ZLIB
209
    zret = inflateReset(&(c->zstream));
210
    if (zret != Z_OK) {
211
        av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
212
        return -1;
213
    }
214
    c->zstream.next_in = encoded;
215
    c->zstream.avail_in = len;
216
    c->zstream.next_out = c->decomp_buf;
217
    c->zstream.avail_out = c->decomp_size;
218
    zret = inflate(&(c->zstream), Z_FINISH);
219
    // Z_DATA_ERROR means empty picture
220
    if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
221
        av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
222
        return -1;
223
    }
224

    
225

    
226
    if(zret != Z_DATA_ERROR)
227
        decode_rle(c, c->zstream.avail_out);
228

    
229
    /* make the palette available on the way out */
230
    if (c->avctx->pix_fmt == PIX_FMT_PAL8) {
231
        memcpy(c->pic.data[1], c->avctx->palctrl->palette, AVPALETTE_SIZE);
232
        if (c->avctx->palctrl->palette_changed) {
233
            c->pic.palette_has_changed = 1;
234
            c->avctx->palctrl->palette_changed = 0;
235
        }
236
    }
237

    
238
#else
239
    av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
240
    return -1;
241
#endif
242

    
243
    *data_size = sizeof(AVFrame);
244
    *(AVFrame*)data = c->pic;
245

    
246
    /* always report that the buffer was completely consumed */
247
    return buf_size;
248
}
249

    
250

    
251

    
252
/*
253
 *
254
 * Init tscc decoder
255
 *
256
 */
257
static int decode_init(AVCodecContext *avctx)
258
{
259
    CamtasiaContext * const c = avctx->priv_data;
260
    int zret; // Zlib return code
261

    
262
    c->avctx = avctx;
263

    
264
    c->pic.data[0] = NULL;
265
    c->height = avctx->height;
266

    
267
    if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
268
        return 1;
269
    }
270

    
271
#ifdef CONFIG_ZLIB
272
    // Needed if zlib unused or init aborted before inflateInit
273
    memset(&(c->zstream), 0, sizeof(z_stream));
274
#else
275
    av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
276
    return 1;
277
#endif
278
    switch(avctx->bits_per_sample){
279
    case  8: avctx->pix_fmt = PIX_FMT_PAL8; break;
280
    case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
281
    case 24:
282
             avctx->pix_fmt = PIX_FMT_BGR24;
283
             break;
284
    case 32: avctx->pix_fmt = PIX_FMT_RGB32; break;
285
    default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_sample);
286
             return -1;
287
    }
288
    c->bpp = avctx->bits_per_sample;
289
    c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;//RLE in the 'best' case
290

    
291
    /* Allocate decompression buffer */
292
    if (c->decomp_size) {
293
        if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
294
            av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
295
            return 1;
296
        }
297
    }
298

    
299
#ifdef CONFIG_ZLIB
300
    c->zstream.zalloc = Z_NULL;
301
    c->zstream.zfree = Z_NULL;
302
    c->zstream.opaque = Z_NULL;
303
    zret = inflateInit(&(c->zstream));
304
    if (zret != Z_OK) {
305
        av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
306
        return 1;
307
    }
308
#endif
309

    
310
    return 0;
311
}
312

    
313

    
314

    
315
/*
316
 *
317
 * Uninit tscc decoder
318
 *
319
 */
320
static int decode_end(AVCodecContext *avctx)
321
{
322
    CamtasiaContext * const c = avctx->priv_data;
323

    
324
    av_freep(&c->decomp_buf);
325

    
326
    if (c->pic.data[0])
327
        avctx->release_buffer(avctx, &c->pic);
328
#ifdef CONFIG_ZLIB
329
    inflateEnd(&(c->zstream));
330
#endif
331

    
332
    return 0;
333
}
334

    
335
AVCodec tscc_decoder = {
336
        "camtasia",
337
        CODEC_TYPE_VIDEO,
338
        CODEC_ID_TSCC,
339
        sizeof(CamtasiaContext),
340
        decode_init,
341
        NULL,
342
        decode_end,
343
        decode_frame,
344
        CODEC_CAP_DR1,
345
};
346