Statistics
| Branch: | Revision:

ffmpeg / libavcodec / tscc.c @ 44aa9771

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

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

    
41
#include "avcodec.h"
42
#include "msrledec.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 a frame
72
 *
73
 */
74
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size)
75
{
76
    CamtasiaContext * const c = avctx->priv_data;
77
    const unsigned char *encoded = buf;
78
    unsigned char *outptr;
79
#ifdef CONFIG_ZLIB
80
    int zret; // Zlib return code
81
#endif
82
    int len = buf_size;
83

    
84
    if(c->pic.data[0])
85
            avctx->release_buffer(avctx, &c->pic);
86

    
87
    c->pic.reference = 1;
88
    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
89
    if(avctx->get_buffer(avctx, &c->pic) < 0){
90
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
91
        return -1;
92
    }
93

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

    
96
#ifdef CONFIG_ZLIB
97
    zret = inflateReset(&(c->zstream));
98
    if (zret != Z_OK) {
99
        av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
100
        return -1;
101
    }
102
    c->zstream.next_in = encoded;
103
    c->zstream.avail_in = len;
104
    c->zstream.next_out = c->decomp_buf;
105
    c->zstream.avail_out = c->decomp_size;
106
    zret = inflate(&(c->zstream), Z_FINISH);
107
    // Z_DATA_ERROR means empty picture
108
    if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
109
        av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
110
        return -1;
111
    }
112

    
113

    
114
    if(zret != Z_DATA_ERROR)
115
        ff_msrle_decode(avctx, &c->pic, c->bpp, c->decomp_buf, c->zstream.avail_out);
116

    
117
    /* make the palette available on the way out */
118
    if (c->avctx->pix_fmt == PIX_FMT_PAL8) {
119
        memcpy(c->pic.data[1], c->avctx->palctrl->palette, AVPALETTE_SIZE);
120
        if (c->avctx->palctrl->palette_changed) {
121
            c->pic.palette_has_changed = 1;
122
            c->avctx->palctrl->palette_changed = 0;
123
        }
124
    }
125

    
126
#else
127
    av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
128
    return -1;
129
#endif
130

    
131
    *data_size = sizeof(AVFrame);
132
    *(AVFrame*)data = c->pic;
133

    
134
    /* always report that the buffer was completely consumed */
135
    return buf_size;
136
}
137

    
138

    
139

    
140
/*
141
 *
142
 * Init tscc decoder
143
 *
144
 */
145
static av_cold int decode_init(AVCodecContext *avctx)
146
{
147
    CamtasiaContext * const c = avctx->priv_data;
148
    int zret; // Zlib return code
149

    
150
    c->avctx = avctx;
151

    
152
    c->pic.data[0] = NULL;
153
    c->height = avctx->height;
154

    
155
    if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
156
        return 1;
157
    }
158

    
159
#ifdef CONFIG_ZLIB
160
    // Needed if zlib unused or init aborted before inflateInit
161
    memset(&(c->zstream), 0, sizeof(z_stream));
162
#else
163
    av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
164
    return 1;
165
#endif
166
    switch(avctx->bits_per_coded_sample){
167
    case  8: avctx->pix_fmt = PIX_FMT_PAL8; break;
168
    case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
169
    case 24:
170
             avctx->pix_fmt = PIX_FMT_BGR24;
171
             break;
172
    case 32: avctx->pix_fmt = PIX_FMT_RGB32; break;
173
    default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
174
             return -1;
175
    }
176
    c->bpp = avctx->bits_per_coded_sample;
177
    c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;//RLE in the 'best' case
178

    
179
    /* Allocate decompression buffer */
180
    if (c->decomp_size) {
181
        if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
182
            av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
183
            return 1;
184
        }
185
    }
186

    
187
#ifdef CONFIG_ZLIB
188
    c->zstream.zalloc = Z_NULL;
189
    c->zstream.zfree = Z_NULL;
190
    c->zstream.opaque = Z_NULL;
191
    zret = inflateInit(&(c->zstream));
192
    if (zret != Z_OK) {
193
        av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
194
        return 1;
195
    }
196
#endif
197

    
198
    return 0;
199
}
200

    
201

    
202

    
203
/*
204
 *
205
 * Uninit tscc decoder
206
 *
207
 */
208
static av_cold int decode_end(AVCodecContext *avctx)
209
{
210
    CamtasiaContext * const c = avctx->priv_data;
211

    
212
    av_freep(&c->decomp_buf);
213

    
214
    if (c->pic.data[0])
215
        avctx->release_buffer(avctx, &c->pic);
216
#ifdef CONFIG_ZLIB
217
    inflateEnd(&(c->zstream));
218
#endif
219

    
220
    return 0;
221
}
222

    
223
AVCodec tscc_decoder = {
224
        "camtasia",
225
        CODEC_TYPE_VIDEO,
226
        CODEC_ID_TSCC,
227
        sizeof(CamtasiaContext),
228
        decode_init,
229
        NULL,
230
        decode_end,
231
        decode_frame,
232
        CODEC_CAP_DR1,
233
        .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
234
};
235