Statistics
| Branch: | Revision:

ffmpeg / libavcodec / tscc.c @ 650cec0c

History | View | Annotate | Download (8.07 KB)

1 9d53d58e Mike Melanson
/*
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 229daca7 Mike Melanson
 * Supports: BGR8,BGR555,BGR24 - only BGR8 and BGR555 tested
34 9d53d58e Mike Melanson
 *
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)
76
{
77
    unsigned char *src = c->decomp_buf;
78 229daca7 Mike Melanson
    unsigned char *output;
79 9d53d58e Mike Melanson
    int p1, p2, line=c->height, pos=0, i;
80 229daca7 Mike Melanson
    
81
    output = c->pic.data[0] + (c->height - 1) * c->pic.linesize[0];
82 9d53d58e Mike Melanson
    while(src < c->decomp_buf + c->decomp_size) {
83
        p1 = *src++;
84
        if(p1 == 0) { //Escape code
85
            p2 = *src++;
86
            if(p2 == 0) { //End-of-line
87
                output = c->pic.data[0] + (--line) * c->pic.linesize[0];
88
                pos = 0;
89
                continue;
90
            } else if(p2 == 1) { //End-of-picture
91
                return 0;
92
            } else if(p2 == 2) { //Skip
93
                p1 = *src++;
94
                p2 = *src++;
95
                line -= p2;
96
                pos += p1;
97
                output = c->pic.data[0] + line * c->pic.linesize[0] + pos * (c->bpp / 8);
98
                continue;
99
            }
100
            // Copy data
101
            for(i = 0; i < p2 * (c->bpp / 8); i++) {
102
                *output++ = *src++;
103
            }
104 229daca7 Mike Melanson
            // RLE8 copy is actually padded - and runs are not!
105
            if(c->bpp == 8 && (p2 & 1)) {
106
                src++;
107
            }
108 9d53d58e Mike Melanson
            pos += p2;
109
        } else { //Run of pixels
110
            int pix[3]; //original pixel
111
            switch(c->bpp){
112
            case  8: pix[0] = *src++;
113
                     break;
114
            case 16: pix[0] = *src++;
115
                     pix[1] = *src++;
116
                     break;
117
            case 24: pix[0] = *src++;
118
                     pix[1] = *src++;
119
                     pix[2] = *src++;
120
                     break;
121
            }
122
            for(i = 0; i < p1; i++) {
123
                switch(c->bpp){
124
                case  8: *output++ = pix[0];
125
                         break;
126
                case 16: *output++ = pix[0];
127
                         *output++ = pix[1];
128
                         break;
129
                case 24: *output++ = pix[0];
130
                         *output++ = pix[1];
131
                         *output++ = pix[2];
132
                         break;
133
                }
134
            }
135
            pos += p1;
136
        }
137
    }
138
    
139
    av_log(c->avctx, AV_LOG_ERROR, "Camtasia warning: no End-of-picture code\n");        
140
    return 1;
141
}
142
143
/*
144
 *
145
 * Decode a frame
146
 *
147
 */
148
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
149
{
150
    CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
151
    unsigned char *encoded = (unsigned char *)buf;
152
    unsigned char *outptr;
153
#ifdef CONFIG_ZLIB
154
    int zret; // Zlib return code
155
#endif
156
    int len = buf_size;
157
158
    /* no supplementary picture */
159
    if (buf_size == 0)
160
        return 0;
161
162
    if(c->pic.data[0])
163
            avctx->release_buffer(avctx, &c->pic);
164
165
    c->pic.reference = 1;
166
    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
167
    if(avctx->get_buffer(avctx, &c->pic) < 0){
168
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
169
        return -1;
170
    }
171
172
    outptr = c->pic.data[0]; // Output image pointer
173
174
#ifdef CONFIG_ZLIB
175
    zret = inflateReset(&(c->zstream));
176
    if (zret != Z_OK) {
177
        av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
178
        return -1;
179
    }
180
    c->zstream.next_in = encoded;
181
    c->zstream.avail_in = len;
182
    c->zstream.next_out = c->decomp_buf;
183
    c->zstream.avail_out = c->decomp_size;
184
    zret = inflate(&(c->zstream), Z_FINISH);
185
    // Z_DATA_ERROR means empty picture
186
    if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
187
        av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
188
        return -1;
189
    }
190
    encoded = c->decomp_buf;
191
    len = c->decomp_size;
192
    if(zret != Z_DATA_ERROR)
193
        decode_rle(c);
194 229daca7 Mike Melanson
    
195
    /* make the palette available on the way out */
196
    if (c->avctx->pix_fmt == PIX_FMT_PAL8) {
197
        memcpy(c->pic.data[1], c->avctx->palctrl->palette, AVPALETTE_SIZE);
198
        if (c->avctx->palctrl->palette_changed) {
199
            c->pic.palette_has_changed = 1;
200
            c->avctx->palctrl->palette_changed = 0;
201
        }
202
    }
203
204 9d53d58e Mike Melanson
#else
205
    av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
206
    return -1;
207
#endif
208
209
    *data_size = sizeof(AVFrame);
210
    *(AVFrame*)data = c->pic;
211
212
    /* always report that the buffer was completely consumed */
213
    return buf_size;
214
}
215
216
217
218
/*
219
 *
220
 * Init tscc decoder
221
 *
222
 */
223
static int decode_init(AVCodecContext *avctx)
224
{
225
    CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
226
    int zret; // Zlib return code
227
228
    c->avctx = avctx;
229
    avctx->has_b_frames = 0;
230
231
    c->pic.data[0] = NULL;
232
    c->height = avctx->height;
233
234
#ifdef CONFIG_ZLIB
235
    // Needed if zlib unused or init aborted before inflateInit
236
    memset(&(c->zstream), 0, sizeof(z_stream)); 
237
#else
238
    av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
239
    return 1;
240
#endif
241
    switch(avctx->bits_per_sample){
242 229daca7 Mike Melanson
    case  8: avctx->pix_fmt = PIX_FMT_PAL8; break;
243
    case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
244 9d53d58e Mike Melanson
    case 24: av_log(avctx, AV_LOG_ERROR, "Camtasia warning: RGB24 is just guessed\n");
245
             avctx->pix_fmt = PIX_FMT_BGR24;
246
             break;
247
    default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_sample);
248
             return -1;             
249
    }
250
    c->bpp = avctx->bits_per_sample;
251 229daca7 Mike Melanson
    c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;//RLE in the 'best' case
252 9d53d58e Mike Melanson
253
    /* Allocate decompression buffer */
254
    if (c->decomp_size) {
255
        if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
256
            av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
257
            return 1;
258
        }
259
    }
260
  
261
#ifdef CONFIG_ZLIB
262
    c->zstream.zalloc = Z_NULL;
263
    c->zstream.zfree = Z_NULL;
264
    c->zstream.opaque = Z_NULL;
265
    zret = inflateInit(&(c->zstream));
266
    if (zret != Z_OK) {
267
        av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
268
        return 1;
269
    }
270
#endif
271
272
    return 0;
273
}
274
275
276
277
/*
278
 *
279
 * Uninit tscc decoder
280
 *
281
 */
282
static int decode_end(AVCodecContext *avctx)
283
{
284
    CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
285
286
    if (c->pic.data[0])
287
        avctx->release_buffer(avctx, &c->pic);
288
#ifdef CONFIG_ZLIB
289
    inflateEnd(&(c->zstream));
290
#endif
291
292
    return 0;
293
}
294
295
AVCodec tscc_decoder = {
296
        "camtasia",
297
        CODEC_TYPE_VIDEO,
298
        CODEC_ID_TSCC,
299
        sizeof(CamtasiaContext),
300
        decode_init,
301
        NULL,
302
        decode_end,
303
        decode_frame,
304
        CODEC_CAP_DR1,
305
};