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
24 * TechSmith Camtasia decoder
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
34 * Supports: BGR8,BGR555,BGR24 - only BGR8 and BGR555 tested
50 typedef struct TsccContext {
52 AVCodecContext *avctx;
57 // Decompressed data size
58 unsigned int decomp_size;
59 // Decompression buffer
60 unsigned char* decomp_buf;
72 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
74 const uint8_t *buf = avpkt->data;
75 int buf_size = avpkt->size;
76 CamtasiaContext * const c = avctx->priv_data;
77 const unsigned char *encoded = buf;
78 int zret; // Zlib return code
82 avctx->release_buffer(avctx, &c->pic);
85 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
86 if(avctx->get_buffer(avctx, &c->pic) < 0){
87 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
91 zret = inflateReset(&(c->zstream));
93 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
96 c->zstream.next_in = encoded;
97 c->zstream.avail_in = len;
98 c->zstream.next_out = c->decomp_buf;
99 c->zstream.avail_out = c->decomp_size;
100 zret = inflate(&(c->zstream), Z_FINISH);
101 // Z_DATA_ERROR means empty picture
102 if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
103 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
108 if(zret != Z_DATA_ERROR)
109 ff_msrle_decode(avctx, (AVPicture*)&c->pic, c->bpp, c->decomp_buf, c->decomp_size - c->zstream.avail_out);
111 /* make the palette available on the way out */
112 if (c->avctx->pix_fmt == PIX_FMT_PAL8) {
113 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
116 c->pic.palette_has_changed = 1;
117 memcpy(c->pal, pal, AVPALETTE_SIZE);
119 memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
122 *data_size = sizeof(AVFrame);
123 *(AVFrame*)data = c->pic;
125 /* always report that the buffer was completely consumed */
136 static av_cold int decode_init(AVCodecContext *avctx)
138 CamtasiaContext * const c = avctx->priv_data;
139 int zret; // Zlib return code
143 c->height = avctx->height;
145 avcodec_get_frame_defaults(&c->pic);
146 // Needed if zlib unused or init aborted before inflateInit
147 memset(&(c->zstream), 0, sizeof(z_stream));
148 switch(avctx->bits_per_coded_sample){
149 case 8: avctx->pix_fmt = PIX_FMT_PAL8; break;
150 case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
152 avctx->pix_fmt = PIX_FMT_BGR24;
154 case 32: avctx->pix_fmt = PIX_FMT_RGB32; break;
155 default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
158 c->bpp = avctx->bits_per_coded_sample;
159 // buffer size for RLE 'best' case when 2-byte code preceeds each pixel and there may be padding after it too
160 c->decomp_size = (((avctx->width * c->bpp + 7) >> 3) + 3 * avctx->width + 2) * avctx->height + 2;
162 /* Allocate decompression buffer */
163 if (c->decomp_size) {
164 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
165 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
170 c->zstream.zalloc = Z_NULL;
171 c->zstream.zfree = Z_NULL;
172 c->zstream.opaque = Z_NULL;
173 zret = inflateInit(&(c->zstream));
175 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
186 * Uninit tscc decoder
189 static av_cold int decode_end(AVCodecContext *avctx)
191 CamtasiaContext * const c = avctx->priv_data;
193 av_freep(&c->decomp_buf);
196 avctx->release_buffer(avctx, &c->pic);
197 inflateEnd(&(c->zstream));
202 AVCodec ff_tscc_decoder = {
204 .type = AVMEDIA_TYPE_VIDEO,
206 .priv_data_size = sizeof(CamtasiaContext),
209 .decode = decode_frame,
210 .capabilities = CODEC_CAP_DR1,
211 .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),