e38ef7e00c9940ef93315637b79374e2e2761622
[libav.git] / libavcodec / tscc.c
1 /*
2 * TechSmith Camtasia decoder
3 * Copyright (c) 2004 Konstantin Shishkov
4 *
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.
9 *
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.
14 *
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
18 *
19 */
20
21 /**
22 * @file tscc.c
23 * TechSmith Camtasia decoder
24 *
25 * Fourcc: TSCC
26 *
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
32 *
33 * Supports: BGR8,BGR555,BGR24 - only BGR8 and BGR555 tested
34 *
35 */
36
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 #include "common.h"
41 #include "avcodec.h"
42
43 #ifdef CONFIG_ZLIB
44 #include <zlib.h>
45 #endif
46
47
48 /*
49 * Decoder context
50 */
51 typedef struct TsccContext {
52
53 AVCodecContext *avctx;
54 AVFrame pic;
55
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;
67
68 /*
69 *
70 * Decode RLE - almost identical to Windows BMP RLE8
71 * and enhanced to bigger color depths
72 *
73 */
74
75 static int decode_rle(CamtasiaContext *c, unsigned int srcsize)
76 {
77 unsigned char *src = c->decomp_buf;
78 unsigned char *output, *output_end;
79 int p1, p2, line=c->height, pos=0, i;
80
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;
104 }
105 // Copy data
106 if (output + p2 * (c->bpp / 8) > output_end) {
107 src += p2 * (c->bpp / 8);
108 continue;
109 }
110 for(i = 0; i < p2 * (c->bpp / 8); i++) {
111 *output++ = *src++;
112 }
113 // RLE8 copy is actually padded - and runs are not!
114 if(c->bpp == 8 && (p2 & 1)) {
115 src++;
116 }
117 pos += p2;
118 } else { //Run of pixels
119 int pix[3]; //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 }
131 if (output + p1 * (c->bpp / 8) > output_end)
132 continue;
133 for(i = 0; i < p1; i++) {
134 switch(c->bpp){
135 case 8: *output++ = pix[0];
136 break;
137 case 16: *output++ = pix[0];
138 *output++ = pix[1];
139 break;
140 case 24: *output++ = pix[0];
141 *output++ = pix[1];
142 *output++ = pix[2];
143 break;
144 }
145 }
146 pos += p1;
147 }
148 }
149
150 av_log(c->avctx, AV_LOG_ERROR, "Camtasia warning: no End-of-picture code\n");
151 return 1;
152 }
153
154 /*
155 *
156 * Decode a frame
157 *
158 */
159 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
160 {
161 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
162 unsigned char *encoded = (unsigned char *)buf;
163 unsigned char *outptr;
164 #ifdef CONFIG_ZLIB
165 int zret; // Zlib return code
166 #endif
167 int len = buf_size;
168
169 if(c->pic.data[0])
170 avctx->release_buffer(avctx, &c->pic);
171
172 c->pic.reference = 1;
173 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
174 if(avctx->get_buffer(avctx, &c->pic) < 0){
175 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
176 return -1;
177 }
178
179 outptr = c->pic.data[0]; // Output image pointer
180
181 #ifdef CONFIG_ZLIB
182 zret = inflateReset(&(c->zstream));
183 if (zret != Z_OK) {
184 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
185 return -1;
186 }
187 c->zstream.next_in = encoded;
188 c->zstream.avail_in = len;
189 c->zstream.next_out = c->decomp_buf;
190 c->zstream.avail_out = c->decomp_size;
191 zret = inflate(&(c->zstream), Z_FINISH);
192 // Z_DATA_ERROR means empty picture
193 if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
194 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
195 return -1;
196 }
197
198
199 if(zret != Z_DATA_ERROR)
200 decode_rle(c, c->zstream.avail_out);
201
202 /* make the palette available on the way out */
203 if (c->avctx->pix_fmt == PIX_FMT_PAL8) {
204 memcpy(c->pic.data[1], c->avctx->palctrl->palette, AVPALETTE_SIZE);
205 if (c->avctx->palctrl->palette_changed) {
206 c->pic.palette_has_changed = 1;
207 c->avctx->palctrl->palette_changed = 0;
208 }
209 }
210
211 #else
212 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
213 return -1;
214 #endif
215
216 *data_size = sizeof(AVFrame);
217 *(AVFrame*)data = c->pic;
218
219 /* always report that the buffer was completely consumed */
220 return buf_size;
221 }
222
223
224
225 /*
226 *
227 * Init tscc decoder
228 *
229 */
230 static int decode_init(AVCodecContext *avctx)
231 {
232 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
233 int zret; // Zlib return code
234
235 c->avctx = avctx;
236 avctx->has_b_frames = 0;
237
238 c->pic.data[0] = NULL;
239 c->height = avctx->height;
240
241 if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
242 return 1;
243 }
244
245 #ifdef CONFIG_ZLIB
246 // Needed if zlib unused or init aborted before inflateInit
247 memset(&(c->zstream), 0, sizeof(z_stream));
248 #else
249 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
250 return 1;
251 #endif
252 switch(avctx->bits_per_sample){
253 case 8: avctx->pix_fmt = PIX_FMT_PAL8; break;
254 case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
255 case 24: av_log(avctx, AV_LOG_ERROR, "Camtasia warning: RGB24 is just guessed\n");
256 avctx->pix_fmt = PIX_FMT_BGR24;
257 break;
258 default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_sample);
259 return -1;
260 }
261 c->bpp = avctx->bits_per_sample;
262 c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;//RLE in the 'best' case
263
264 /* Allocate decompression buffer */
265 if (c->decomp_size) {
266 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
267 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
268 return 1;
269 }
270 }
271
272 #ifdef CONFIG_ZLIB
273 c->zstream.zalloc = Z_NULL;
274 c->zstream.zfree = Z_NULL;
275 c->zstream.opaque = Z_NULL;
276 zret = inflateInit(&(c->zstream));
277 if (zret != Z_OK) {
278 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
279 return 1;
280 }
281 #endif
282
283 return 0;
284 }
285
286
287
288 /*
289 *
290 * Uninit tscc decoder
291 *
292 */
293 static int decode_end(AVCodecContext *avctx)
294 {
295 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
296
297 if (c->pic.data[0])
298 avctx->release_buffer(avctx, &c->pic);
299 #ifdef CONFIG_ZLIB
300 inflateEnd(&(c->zstream));
301 #endif
302
303 return 0;
304 }
305
306 AVCodec tscc_decoder = {
307 "camtasia",
308 CODEC_TYPE_VIDEO,
309 CODEC_ID_TSCC,
310 sizeof(CamtasiaContext),
311 decode_init,
312 NULL,
313 decode_end,
314 decode_frame,
315 CODEC_CAP_DR1,
316 };
317