r1009: Move the dependencies to newer package names
[cinelerra_cv/mob.git] / quicktime / ffmpeg / libavcodec / tscc.c
blob10940440470984db51d50c6e410d5e92c28d75f2
1 /*
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
21 /**
22 * @file tscc.c
23 * TechSmith Camtasia decoder
25 * Fourcc: TSCC
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
37 #include <stdio.h>
38 #include <stdlib.h>
40 #include "common.h"
41 #include "avcodec.h"
43 #ifdef CONFIG_ZLIB
44 #include <zlib.h>
45 #endif
49 * Decoder context
51 typedef struct TsccContext {
53 AVCodecContext *avctx;
54 AVFrame pic;
56 // Bits per pixel
57 int bpp;
58 // Decompressed data size
59 unsigned int decomp_size;
60 // Decompression buffer
61 unsigned char* decomp_buf;
62 int height;
63 #ifdef CONFIG_ZLIB
64 z_stream zstream;
65 #endif
66 } CamtasiaContext;
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) {
84 p1 = *src++;
85 if(p1 == 0) { //Escape code
86 p2 = *src++;
87 if(p2 == 0) { //End-of-line
88 output = c->pic.data[0] + (--line) * c->pic.linesize[0];
89 if (line < 0)
90 return -1;
91 pos = 0;
92 continue;
93 } else if(p2 == 1) { //End-of-picture
94 return 0;
95 } else if(p2 == 2) { //Skip
96 p1 = *src++;
97 p2 = *src++;
98 line -= p2;
99 if (line < 0)
100 return -1;
101 pos += p1;
102 output = c->pic.data[0] + line * c->pic.linesize[0] + pos * (c->bpp / 8);
103 continue;
105 // Copy data
106 if (output + p2 * (c->bpp / 8) > output_end) {
107 src += p2 * (c->bpp / 8);
108 continue;
110 for(i = 0; i < p2 * (c->bpp / 8); i++) {
111 *output++ = *src++;
113 // RLE8 copy is actually padded - and runs are not!
114 if(c->bpp == 8 && (p2 & 1)) {
115 src++;
117 pos += p2;
118 } else { //Run of pixels
119 int pix[4]; //original pixel
120 switch(c->bpp){
121 case 8: pix[0] = *src++;
122 break;
123 case 16: pix[0] = *src++;
124 pix[1] = *src++;
125 break;
126 case 24: pix[0] = *src++;
127 pix[1] = *src++;
128 pix[2] = *src++;
129 break;
130 case 32: pix[0] = *src++;
131 pix[1] = *src++;
132 pix[2] = *src++;
133 pix[3] = *src++;
134 break;
136 if (output + p1 * (c->bpp / 8) > output_end)
137 continue;
138 for(i = 0; i < p1; i++) {
139 switch(c->bpp){
140 case 8: *output++ = pix[0];
141 break;
142 case 16: *output++ = pix[0];
143 *output++ = pix[1];
144 break;
145 case 24: *output++ = pix[0];
146 *output++ = pix[1];
147 *output++ = pix[2];
148 break;
149 case 32: *output++ = pix[0];
150 *output++ = pix[1];
151 *output++ = pix[2];
152 *output++ = pix[3];
153 break;
156 pos += p1;
160 av_log(c->avctx, AV_LOG_ERROR, "Camtasia warning: no End-of-picture code\n");
161 return 1;
166 * Decode a frame
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;
174 #ifdef CONFIG_ZLIB
175 int zret; // Zlib return code
176 #endif
177 int len = buf_size;
179 if(c->pic.data[0])
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");
186 return -1;
189 outptr = c->pic.data[0]; // Output image pointer
191 #ifdef CONFIG_ZLIB
192 zret = inflateReset(&(c->zstream));
193 if (zret != Z_OK) {
194 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
195 return -1;
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);
205 return -1;
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;
221 #else
222 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
223 return -1;
224 #endif
226 *data_size = sizeof(AVFrame);
227 *(AVFrame*)data = c->pic;
229 /* always report that the buffer was completely consumed */
230 return buf_size;
237 * Init tscc decoder
240 static int decode_init(AVCodecContext *avctx)
242 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
243 int zret; // Zlib return code
245 c->avctx = avctx;
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) {
252 return 1;
255 #ifdef CONFIG_ZLIB
256 // Needed if zlib unused or init aborted before inflateInit
257 memset(&(c->zstream), 0, sizeof(z_stream));
258 #else
259 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
260 return 1;
261 #endif
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;
265 case 24:
266 avctx->pix_fmt = PIX_FMT_BGR24;
267 break;
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);
270 return -1;
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");
279 return 1;
283 #ifdef CONFIG_ZLIB
284 c->zstream.zalloc = Z_NULL;
285 c->zstream.zfree = Z_NULL;
286 c->zstream.opaque = Z_NULL;
287 zret = inflateInit(&(c->zstream));
288 if (zret != Z_OK) {
289 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
290 return 1;
292 #endif
294 return 0;
301 * Uninit tscc decoder
304 static int decode_end(AVCodecContext *avctx)
306 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
308 av_freep(&c->decomp_buf);
310 if (c->pic.data[0])
311 avctx->release_buffer(avctx, &c->pic);
312 #ifdef CONFIG_ZLIB
313 inflateEnd(&(c->zstream));
314 #endif
316 return 0;
319 AVCodec tscc_decoder = {
320 "camtasia",
321 CODEC_TYPE_VIDEO,
322 CODEC_ID_TSCC,
323 sizeof(CamtasiaContext),
324 decode_init,
325 NULL,
326 decode_end,
327 decode_frame,
328 CODEC_CAP_DR1,