2 * TechSmith Camtasia decoder
3 * Copyright (c) 2004 Konstantin Shishkov
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.
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.
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
23 * TechSmith Camtasia decoder
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
33 * Supports: BGR8,BGR555,BGR24 - only BGR8 and BGR555 tested
51 typedef struct TsccContext {
53 AVCodecContext *avctx;
58 // Decompressed data size
59 unsigned int decomp_size;
60 // Decompression buffer
61 unsigned char* decomp_buf;
70 * Decode RLE - almost identical to Windows BMP RLE8
71 * and enhanced to bigger color depths
75 static int decode_rle(CamtasiaContext *c, unsigned int srcsize)
77 unsigned char *src = c->decomp_buf;
78 unsigned char *output, *output_end;
79 int p1, p2, line=c->height, pos=0, i;
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) {
85 if(p1 == 0) { //Escape code
87 if(p2 == 0) { //End-of-line
88 output = c->pic.data[0] + (--line) * c->pic.linesize[0];
93 } else if(p2 == 1) { //End-of-picture
95 } else if(p2 == 2) { //Skip
102 output = c->pic.data[0] + line * c->pic.linesize[0] + pos * (c->bpp / 8);
106 if (output + p2 * (c->bpp / 8) > output_end) {
107 src += p2 * (c->bpp / 8);
110 for(i = 0; i < p2 * (c->bpp / 8); i++) {
113 // RLE8 copy is actually padded - and runs are not!
114 if(c->bpp == 8 && (p2 & 1)) {
118 } else { //Run of pixels
119 int pix[4]; //original pixel
121 case 8: pix[0] = *src++;
123 case 16: pix[0] = *src++;
126 case 24: pix[0] = *src++;
130 case 32: pix[0] = *src++;
136 if (output + p1 * (c->bpp / 8) > output_end)
138 for(i = 0; i < p1; i++) {
140 case 8: *output++ = pix[0];
142 case 16: *output++ = pix[0];
145 case 24: *output++ = pix[0];
149 case 32: *output++ = pix[0];
160 av_log(c->avctx, AV_LOG_ERROR, "Camtasia warning: no End-of-picture code\n");
169 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
171 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
172 unsigned char *encoded = (unsigned char *)buf;
173 unsigned char *outptr;
175 int zret; // Zlib return code
180 avctx->release_buffer(avctx, &c->pic);
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");
189 outptr = c->pic.data[0]; // Output image pointer
192 zret = inflateReset(&(c->zstream));
194 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
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);
209 if(zret != Z_DATA_ERROR)
210 decode_rle(c, c->zstream.avail_out);
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;
222 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
226 *data_size = sizeof(AVFrame);
227 *(AVFrame*)data = c->pic;
229 /* always report that the buffer was completely consumed */
240 static int decode_init(AVCodecContext *avctx)
242 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
243 int zret; // Zlib return code
246 avctx->has_b_frames = 0;
248 c->pic.data[0] = NULL;
249 c->height = avctx->height;
251 if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
256 // Needed if zlib unused or init aborted before inflateInit
257 memset(&(c->zstream), 0, sizeof(z_stream));
259 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
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;
266 avctx->pix_fmt = PIX_FMT_BGR24;
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);
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
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");
284 c->zstream.zalloc = Z_NULL;
285 c->zstream.zfree = Z_NULL;
286 c->zstream.opaque = Z_NULL;
287 zret = inflateInit(&(c->zstream));
289 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
301 * Uninit tscc decoder
304 static int decode_end(AVCodecContext *avctx)
306 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
309 avctx->release_buffer(avctx, &c->pic);
311 inflateEnd(&(c->zstream));
317 AVCodec tscc_decoder = {
321 sizeof(CamtasiaContext),