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)
77 unsigned char *src = c->decomp_buf;
78 unsigned char *output;
79 int p1, p2, line=c->height, pos=0, i;
81 output = c->pic.data[0] + (c->height - 1) * c->pic.linesize[0];
82 while(src < c->decomp_buf + c->decomp_size) {
84 if(p1 == 0) { //Escape code
86 if(p2 == 0) { //End-of-line
87 output = c->pic.data[0] + (--line) * c->pic.linesize[0];
90 } else if(p2 == 1) { //End-of-picture
92 } else if(p2 == 2) { //Skip
97 output = c->pic.data[0] + line * c->pic.linesize[0] + pos * (c->bpp / 8);
101 for(i = 0; i < p2 * (c->bpp / 8); i++) {
104 // RLE8 copy is actually padded - and runs are not!
105 if(c->bpp == 8 && (p2 & 1)) {
109 } else { //Run of pixels
110 int pix[3]; //original pixel
112 case 8: pix[0] = *src++;
114 case 16: pix[0] = *src++;
117 case 24: pix[0] = *src++;
122 for(i = 0; i < p1; i++) {
124 case 8: *output++ = pix[0];
126 case 16: *output++ = pix[0];
129 case 24: *output++ = pix[0];
139 av_log(c->avctx, AV_LOG_ERROR, "Camtasia warning: no End-of-picture code\n");
148 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
150 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
151 unsigned char *encoded = (unsigned char *)buf;
152 unsigned char *outptr;
154 int zret; // Zlib return code
159 avctx->release_buffer(avctx, &c->pic);
161 c->pic.reference = 1;
162 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
163 if(avctx->get_buffer(avctx, &c->pic) < 0){
164 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
168 outptr = c->pic.data[0]; // Output image pointer
171 zret = inflateReset(&(c->zstream));
173 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
176 c->zstream.next_in = encoded;
177 c->zstream.avail_in = len;
178 c->zstream.next_out = c->decomp_buf;
179 c->zstream.avail_out = c->decomp_size;
180 zret = inflate(&(c->zstream), Z_FINISH);
181 // Z_DATA_ERROR means empty picture
182 if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
183 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
186 encoded = c->decomp_buf;
187 len = c->decomp_size;
188 if(zret != Z_DATA_ERROR)
191 /* make the palette available on the way out */
192 if (c->avctx->pix_fmt == PIX_FMT_PAL8) {
193 memcpy(c->pic.data[1], c->avctx->palctrl->palette, AVPALETTE_SIZE);
194 if (c->avctx->palctrl->palette_changed) {
195 c->pic.palette_has_changed = 1;
196 c->avctx->palctrl->palette_changed = 0;
201 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
205 *data_size = sizeof(AVFrame);
206 *(AVFrame*)data = c->pic;
208 /* always report that the buffer was completely consumed */
219 static int decode_init(AVCodecContext *avctx)
221 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
222 int zret; // Zlib return code
225 avctx->has_b_frames = 0;
227 c->pic.data[0] = NULL;
228 c->height = avctx->height;
231 // Needed if zlib unused or init aborted before inflateInit
232 memset(&(c->zstream), 0, sizeof(z_stream));
234 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
237 switch(avctx->bits_per_sample){
238 case 8: avctx->pix_fmt = PIX_FMT_PAL8; break;
239 case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
240 case 24: av_log(avctx, AV_LOG_ERROR, "Camtasia warning: RGB24 is just guessed\n");
241 avctx->pix_fmt = PIX_FMT_BGR24;
243 default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_sample);
246 c->bpp = avctx->bits_per_sample;
247 c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;//RLE in the 'best' case
249 /* Allocate decompression buffer */
250 if (c->decomp_size) {
251 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
252 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
258 c->zstream.zalloc = Z_NULL;
259 c->zstream.zfree = Z_NULL;
260 c->zstream.opaque = Z_NULL;
261 zret = inflateInit(&(c->zstream));
263 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
275 * Uninit tscc decoder
278 static int decode_end(AVCodecContext *avctx)
280 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
283 avctx->release_buffer(avctx, &c->pic);
285 inflateEnd(&(c->zstream));
291 AVCodec tscc_decoder = {
295 sizeof(CamtasiaContext),