comparison tscc.c @ 2170:51da590b31a3 libavcodec

TechSmith Camtasia (TSCC) video decoder, courtesy of Konstantin Shishkov
author melanson
date Sat, 14 Aug 2004 15:08:09 +0000
parents
children 3eae46c131a3
comparison
equal deleted inserted replaced
2169:db8baace74d8 2170:51da590b31a3
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 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)
76 {
77 unsigned char *src = c->decomp_buf;
78 unsigned char *output = c->pic.data[0];
79 int p1, p2, line=c->height, pos=0, i;
80 while(src < c->decomp_buf + c->decomp_size) {
81 p1 = *src++;
82 if(p1 == 0) { //Escape code
83 p2 = *src++;
84 if(p2 == 0) { //End-of-line
85 output = c->pic.data[0] + (--line) * c->pic.linesize[0];
86 pos = 0;
87 continue;
88 } else if(p2 == 1) { //End-of-picture
89 return 0;
90 } else if(p2 == 2) { //Skip
91 p1 = *src++;
92 p2 = *src++;
93 line -= p2;
94 pos += p1;
95 output = c->pic.data[0] + line * c->pic.linesize[0] + pos * (c->bpp / 8);
96 continue;
97 }
98 // Copy data
99 for(i = 0; i < p2 * (c->bpp / 8); i++) {
100 *output++ = *src++;
101 }
102 pos += p2;
103 } else { //Run of pixels
104 int pix[3]; //original pixel
105 switch(c->bpp){
106 case 8: pix[0] = *src++;
107 break;
108 case 16: pix[0] = *src++;
109 pix[1] = *src++;
110 break;
111 case 24: pix[0] = *src++;
112 pix[1] = *src++;
113 pix[2] = *src++;
114 break;
115 }
116 for(i = 0; i < p1; i++) {
117 switch(c->bpp){
118 case 8: *output++ = pix[0];
119 break;
120 case 16: *output++ = pix[0];
121 *output++ = pix[1];
122 break;
123 case 24: *output++ = pix[0];
124 *output++ = pix[1];
125 *output++ = pix[2];
126 break;
127 }
128 }
129 pos += p1;
130 }
131 }
132
133 av_log(c->avctx, AV_LOG_ERROR, "Camtasia warning: no End-of-picture code\n");
134 return 1;
135 }
136
137 /*
138 *
139 * Decode a frame
140 *
141 */
142 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
143 {
144 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
145 unsigned char *encoded = (unsigned char *)buf;
146 unsigned char *outptr;
147 #ifdef CONFIG_ZLIB
148 int zret; // Zlib return code
149 #endif
150 int len = buf_size;
151
152 /* no supplementary picture */
153 if (buf_size == 0)
154 return 0;
155
156 if(c->pic.data[0])
157 avctx->release_buffer(avctx, &c->pic);
158
159 c->pic.reference = 1;
160 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
161 if(avctx->get_buffer(avctx, &c->pic) < 0){
162 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
163 return -1;
164 }
165
166 outptr = c->pic.data[0]; // Output image pointer
167
168 #ifdef CONFIG_ZLIB
169 zret = inflateReset(&(c->zstream));
170 if (zret != Z_OK) {
171 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
172 return -1;
173 }
174 c->zstream.next_in = encoded;
175 c->zstream.avail_in = len;
176 c->zstream.next_out = c->decomp_buf;
177 c->zstream.avail_out = c->decomp_size;
178 zret = inflate(&(c->zstream), Z_FINISH);
179 // Z_DATA_ERROR means empty picture
180 if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
181 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
182 return -1;
183 }
184 encoded = c->decomp_buf;
185 len = c->decomp_size;
186 if(zret != Z_DATA_ERROR)
187 decode_rle(c);
188 #else
189 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
190 return -1;
191 #endif
192
193 *data_size = sizeof(AVFrame);
194 *(AVFrame*)data = c->pic;
195
196 /* always report that the buffer was completely consumed */
197 return buf_size;
198 }
199
200
201
202 /*
203 *
204 * Init tscc decoder
205 *
206 */
207 static int decode_init(AVCodecContext *avctx)
208 {
209 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
210 int zret; // Zlib return code
211
212 c->avctx = avctx;
213 avctx->has_b_frames = 0;
214
215 c->pic.data[0] = NULL;
216 c->height = avctx->height;
217
218 #ifdef CONFIG_ZLIB
219 // Needed if zlib unused or init aborted before inflateInit
220 memset(&(c->zstream), 0, sizeof(z_stream));
221 #else
222 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
223 return 1;
224 #endif
225 switch(avctx->bits_per_sample){
226 case 8: av_log(avctx, AV_LOG_ERROR, "Camtasia warning: RGB8 is just guessed\n");
227 avctx->pix_fmt = PIX_FMT_PAL8;
228 break;
229 case 16: avctx->pix_fmt = PIX_FMT_RGB555;break;
230 case 24: av_log(avctx, AV_LOG_ERROR, "Camtasia warning: RGB24 is just guessed\n");
231 avctx->pix_fmt = PIX_FMT_BGR24;
232 break;
233 default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_sample);
234 return -1;
235 }
236 c->bpp = avctx->bits_per_sample;
237 av_log(avctx, AV_LOG_INFO, "bpp=%i \n\n\n", c->bpp);
238 c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;//RLE in the best case
239
240 /* Allocate decompression buffer */
241 if (c->decomp_size) {
242 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
243 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
244 return 1;
245 }
246 }
247
248 #ifdef CONFIG_ZLIB
249 c->zstream.zalloc = Z_NULL;
250 c->zstream.zfree = Z_NULL;
251 c->zstream.opaque = Z_NULL;
252 zret = inflateInit(&(c->zstream));
253 if (zret != Z_OK) {
254 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
255 return 1;
256 }
257 #endif
258
259 return 0;
260 }
261
262
263
264 /*
265 *
266 * Uninit tscc decoder
267 *
268 */
269 static int decode_end(AVCodecContext *avctx)
270 {
271 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
272
273 if (c->pic.data[0])
274 avctx->release_buffer(avctx, &c->pic);
275 #ifdef CONFIG_ZLIB
276 inflateEnd(&(c->zstream));
277 #endif
278
279 return 0;
280 }
281
282 AVCodec tscc_decoder = {
283 "camtasia",
284 CODEC_TYPE_VIDEO,
285 CODEC_ID_TSCC,
286 sizeof(CamtasiaContext),
287 decode_init,
288 NULL,
289 decode_end,
290 decode_frame,
291 CODEC_CAP_DR1,
292 };
293