2 * TechSmith Camtasia decoder
3 * Copyright (c) 2004 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
25 * TechSmith Camtasia decoder
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
35 * Supports: BGR8,BGR555,BGR24 - only BGR8 and BGR555 tested
52 typedef struct TsccContext {
54 AVCodecContext *avctx;
59 // Decompressed data size
60 unsigned int decomp_size;
61 // Decompression buffer
62 unsigned char* decomp_buf;
71 * Decode RLE - almost identical to Windows BMP RLE8
72 * and enhanced to bigger color depths
76 static int decode_rle(CamtasiaContext *c, unsigned int srcsize)
78 unsigned char *src = c->decomp_buf;
79 unsigned char *output, *output_end;
80 int p1, p2, line=c->height, pos=0, i;
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) {
88 if(p1 == 0) { //Escape code
90 if(p2 == 0) { //End-of-line
91 output = c->pic.data[0] + (--line) * c->pic.linesize[0];
96 } else if(p2 == 1) { //End-of-picture
98 } else if(p2 == 2) { //Skip
105 output = c->pic.data[0] + line * c->pic.linesize[0] + pos * (c->bpp / 8);
109 if (output + p2 * (c->bpp / 8) > output_end) {
110 src += p2 * (c->bpp / 8);
113 if ((c->bpp == 8) || (c->bpp == 24)) {
114 for(i = 0; i < p2 * (c->bpp / 8); i++) {
117 // RLE8 copy is actually padded - and runs are not!
118 if(c->bpp == 8 && (p2 & 1)) {
121 } else if (c->bpp == 16) {
122 for(i = 0; i < p2; i++) {
123 pix16 = AV_RL16(src);
125 *(uint16_t*)output = pix16;
128 } else if (c->bpp == 32) {
129 for(i = 0; i < p2; i++) {
130 pix32 = AV_RL32(src);
132 *(uint32_t*)output = pix32;
137 } else { //Run of pixels
138 int pix[4]; //original pixel
140 case 8: pix[0] = *src++;
142 case 16: pix16 = AV_RL16(src);
144 *(uint16_t*)pix = pix16;
146 case 24: pix[0] = *src++;
150 case 32: pix32 = AV_RL32(src);
152 *(uint32_t*)pix = pix32;
155 if (output + p1 * (c->bpp / 8) > output_end)
157 for(i = 0; i < p1; i++) {
159 case 8: *output++ = pix[0];
161 case 16: *(uint16_t*)output = pix16;
164 case 24: *output++ = pix[0];
168 case 32: *(uint32_t*)output = pix32;
177 av_log(c->avctx, AV_LOG_ERROR, "Camtasia warning: no End-of-picture code\n");
186 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
188 CamtasiaContext * const c = avctx->priv_data;
189 unsigned char *encoded = (unsigned char *)buf;
190 unsigned char *outptr;
192 int zret; // Zlib return code
197 avctx->release_buffer(avctx, &c->pic);
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");
206 outptr = c->pic.data[0]; // Output image pointer
209 zret = inflateReset(&(c->zstream));
211 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
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);
226 if(zret != Z_DATA_ERROR)
227 decode_rle(c, c->zstream.avail_out);
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;
239 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
243 *data_size = sizeof(AVFrame);
244 *(AVFrame*)data = c->pic;
246 /* always report that the buffer was completely consumed */
257 static int decode_init(AVCodecContext *avctx)
259 CamtasiaContext * const c = avctx->priv_data;
260 int zret; // Zlib return code
264 c->pic.data[0] = NULL;
265 c->height = avctx->height;
267 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
272 // Needed if zlib unused or init aborted before inflateInit
273 memset(&(c->zstream), 0, sizeof(z_stream));
275 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
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;
282 avctx->pix_fmt = PIX_FMT_BGR24;
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);
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
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");
300 c->zstream.zalloc = Z_NULL;
301 c->zstream.zfree = Z_NULL;
302 c->zstream.opaque = Z_NULL;
303 zret = inflateInit(&(c->zstream));
305 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
317 * Uninit tscc decoder
320 static int decode_end(AVCodecContext *avctx)
322 CamtasiaContext * const c = avctx->priv_data;
324 av_freep(&c->decomp_buf);
327 avctx->release_buffer(avctx, &c->pic);
329 inflateEnd(&(c->zstream));
335 AVCodec tscc_decoder = {
339 sizeof(CamtasiaContext),