Statistics
| Branch: | Revision:

ffmpeg / libavcodec / tscc.c @ b2eef2f0

History | View | Annotate | Download (8.86 KB)

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

    
37
#include <stdio.h>
38
#include <stdlib.h>
39

    
40
#include "common.h"
41
#include "avcodec.h"
42

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

    
47

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

    
53
    AVCodecContext *avctx;
54
    AVFrame pic;
55

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

    
68
/*
69
 *
70
 * Decode RLE - almost identical to Windows BMP RLE8
71
 *              and enhanced to bigger color depths
72
 *
73
 */
74
 
75
static int decode_rle(CamtasiaContext *c, unsigned int srcsize)
76
{
77
    unsigned char *src = c->decomp_buf;
78
    unsigned char *output, *output_end;
79
    int p1, p2, line=c->height, pos=0, i;
80
    
81
    output = c->pic.data[0] + (c->height - 1) * c->pic.linesize[0];
82
    output_end = c->pic.data[0] + (c->height) * c->pic.linesize[0];
83
    while(src < c->decomp_buf + srcsize) {
84
        p1 = *src++;
85
        if(p1 == 0) { //Escape code
86
            p2 = *src++;
87
            if(p2 == 0) { //End-of-line
88
                output = c->pic.data[0] + (--line) * c->pic.linesize[0];
89
                if (line < 0)
90
                    return -1;
91
                pos = 0;
92
                continue;
93
            } else if(p2 == 1) { //End-of-picture
94
                return 0;
95
            } else if(p2 == 2) { //Skip
96
                p1 = *src++;
97
                p2 = *src++;
98
                line -= p2;
99
                if (line < 0)
100
                    return -1;
101
                pos += p1;
102
                output = c->pic.data[0] + line * c->pic.linesize[0] + pos * (c->bpp / 8);
103
                continue;
104
            }
105
            // Copy data
106
            if (output + p2 * (c->bpp / 8) > output_end) {
107
                src += p2 * (c->bpp / 8);
108
                continue;
109
            }
110
            for(i = 0; i < p2 * (c->bpp / 8); i++) {
111
                *output++ = *src++;
112
            }
113
            // RLE8 copy is actually padded - and runs are not!
114
            if(c->bpp == 8 && (p2 & 1)) {
115
                src++;
116
            }
117
            pos += p2;
118
        } else { //Run of pixels
119
            int pix[4]; //original pixel
120
            switch(c->bpp){
121
            case  8: pix[0] = *src++;
122
                     break;
123
            case 16: pix[0] = *src++;
124
                     pix[1] = *src++;
125
                     break;
126
            case 24: pix[0] = *src++;
127
                     pix[1] = *src++;
128
                     pix[2] = *src++;
129
                     break;
130
            case 32: pix[0] = *src++;
131
                     pix[1] = *src++;
132
                     pix[2] = *src++;
133
                     pix[3] = *src++;
134
                     break;
135
            }
136
            if (output + p1 * (c->bpp / 8) > output_end)
137
                continue;
138
            for(i = 0; i < p1; i++) {
139
                switch(c->bpp){
140
                case  8: *output++ = pix[0];
141
                         break;
142
                case 16: *output++ = pix[0];
143
                         *output++ = pix[1];
144
                         break;
145
                case 24: *output++ = pix[0];
146
                         *output++ = pix[1];
147
                         *output++ = pix[2];
148
                         break;
149
                case 32: *output++ = pix[0];
150
                         *output++ = pix[1];
151
                         *output++ = pix[2];
152
                         *output++ = pix[3];
153
                         break;
154
                }
155
            }
156
            pos += p1;
157
        }
158
    }
159
    
160
    av_log(c->avctx, AV_LOG_ERROR, "Camtasia warning: no End-of-picture code\n");        
161
    return 1;
162
}
163

    
164
/*
165
 *
166
 * Decode a frame
167
 *
168
 */
169
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
170
{
171
    CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
172
    unsigned char *encoded = (unsigned char *)buf;
173
    unsigned char *outptr;
174
#ifdef CONFIG_ZLIB
175
    int zret; // Zlib return code
176
#endif
177
    int len = buf_size;
178

    
179
    if(c->pic.data[0])
180
            avctx->release_buffer(avctx, &c->pic);
181

    
182
    c->pic.reference = 1;
183
    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
184
    if(avctx->get_buffer(avctx, &c->pic) < 0){
185
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
186
        return -1;
187
    }
188

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

    
191
#ifdef CONFIG_ZLIB
192
    zret = inflateReset(&(c->zstream));
193
    if (zret != Z_OK) {
194
        av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
195
        return -1;
196
    }
197
    c->zstream.next_in = encoded;
198
    c->zstream.avail_in = len;
199
    c->zstream.next_out = c->decomp_buf;
200
    c->zstream.avail_out = c->decomp_size;
201
    zret = inflate(&(c->zstream), Z_FINISH);
202
    // Z_DATA_ERROR means empty picture
203
    if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
204
        av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
205
        return -1;
206
    }
207

    
208

    
209
    if(zret != Z_DATA_ERROR)
210
        decode_rle(c, c->zstream.avail_out);
211
    
212
    /* make the palette available on the way out */
213
    if (c->avctx->pix_fmt == PIX_FMT_PAL8) {
214
        memcpy(c->pic.data[1], c->avctx->palctrl->palette, AVPALETTE_SIZE);
215
        if (c->avctx->palctrl->palette_changed) {
216
            c->pic.palette_has_changed = 1;
217
            c->avctx->palctrl->palette_changed = 0;
218
        }
219
    }
220

    
221
#else
222
    av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
223
    return -1;
224
#endif
225

    
226
    *data_size = sizeof(AVFrame);
227
    *(AVFrame*)data = c->pic;
228

    
229
    /* always report that the buffer was completely consumed */
230
    return buf_size;
231
}
232

    
233

    
234

    
235
/*
236
 *
237
 * Init tscc decoder
238
 *
239
 */
240
static int decode_init(AVCodecContext *avctx)
241
{
242
    CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
243
    int zret; // Zlib return code
244

    
245
    c->avctx = avctx;
246
    avctx->has_b_frames = 0;
247

    
248
    c->pic.data[0] = NULL;
249
    c->height = avctx->height;
250

    
251
    if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
252
        return 1;
253
    }
254

    
255
#ifdef CONFIG_ZLIB
256
    // Needed if zlib unused or init aborted before inflateInit
257
    memset(&(c->zstream), 0, sizeof(z_stream)); 
258
#else
259
    av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
260
    return 1;
261
#endif
262
    switch(avctx->bits_per_sample){
263
    case  8: avctx->pix_fmt = PIX_FMT_PAL8; break;
264
    case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
265
    case 24:
266
             avctx->pix_fmt = PIX_FMT_BGR24;
267
             break;
268
    case 32: avctx->pix_fmt = PIX_FMT_RGBA32; break;
269
    default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_sample);
270
             return -1;             
271
    }
272
    c->bpp = avctx->bits_per_sample;
273
    c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;//RLE in the 'best' case
274

    
275
    /* Allocate decompression buffer */
276
    if (c->decomp_size) {
277
        if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
278
            av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
279
            return 1;
280
        }
281
    }
282
  
283
#ifdef CONFIG_ZLIB
284
    c->zstream.zalloc = Z_NULL;
285
    c->zstream.zfree = Z_NULL;
286
    c->zstream.opaque = Z_NULL;
287
    zret = inflateInit(&(c->zstream));
288
    if (zret != Z_OK) {
289
        av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
290
        return 1;
291
    }
292
#endif
293

    
294
    return 0;
295
}
296

    
297

    
298

    
299
/*
300
 *
301
 * Uninit tscc decoder
302
 *
303
 */
304
static int decode_end(AVCodecContext *avctx)
305
{
306
    CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
307

    
308
    if (c->pic.data[0])
309
        avctx->release_buffer(avctx, &c->pic);
310
#ifdef CONFIG_ZLIB
311
    inflateEnd(&(c->zstream));
312
#endif
313

    
314
    return 0;
315
}
316

    
317
AVCodec tscc_decoder = {
318
        "camtasia",
319
        CODEC_TYPE_VIDEO,
320
        CODEC_ID_TSCC,
321
        sizeof(CamtasiaContext),
322
        decode_init,
323
        NULL,
324
        decode_end,
325
        decode_frame,
326
        CODEC_CAP_DR1,
327
};
328