simplify
[libav.git] / libavcodec / tscc.c
CommitLineData
9d53d58e
MM
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 *
229daca7 33 * Supports: BGR8,BGR555,BGR24 - only BGR8 and BGR555 tested
9d53d58e
MM
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 */
51typedef 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
75static int decode_rle(CamtasiaContext *c)
76{
77 unsigned char *src = c->decomp_buf;
229daca7 78 unsigned char *output;
9d53d58e 79 int p1, p2, line=c->height, pos=0, i;
229daca7
MM
80
81 output = c->pic.data[0] + (c->height - 1) * c->pic.linesize[0];
9d53d58e
MM
82 while(src < c->decomp_buf + c->decomp_size) {
83 p1 = *src++;
84 if(p1 == 0) { //Escape code
85 p2 = *src++;
86 if(p2 == 0) { //End-of-line
87 output = c->pic.data[0] + (--line) * c->pic.linesize[0];
88 pos = 0;
89 continue;
90 } else if(p2 == 1) { //End-of-picture
91 return 0;
92 } else if(p2 == 2) { //Skip
93 p1 = *src++;
94 p2 = *src++;
95 line -= p2;
96 pos += p1;
97 output = c->pic.data[0] + line * c->pic.linesize[0] + pos * (c->bpp / 8);
98 continue;
99 }
100 // Copy data
101 for(i = 0; i < p2 * (c->bpp / 8); i++) {
102 *output++ = *src++;
103 }
229daca7
MM
104 // RLE8 copy is actually padded - and runs are not!
105 if(c->bpp == 8 && (p2 & 1)) {
106 src++;
107 }
9d53d58e
MM
108 pos += p2;
109 } else { //Run of pixels
110 int pix[3]; //original pixel
111 switch(c->bpp){
112 case 8: pix[0] = *src++;
113 break;
114 case 16: pix[0] = *src++;
115 pix[1] = *src++;
116 break;
117 case 24: pix[0] = *src++;
118 pix[1] = *src++;
119 pix[2] = *src++;
120 break;
121 }
122 for(i = 0; i < p1; i++) {
123 switch(c->bpp){
124 case 8: *output++ = pix[0];
125 break;
126 case 16: *output++ = pix[0];
127 *output++ = pix[1];
128 break;
129 case 24: *output++ = pix[0];
130 *output++ = pix[1];
131 *output++ = pix[2];
132 break;
133 }
134 }
135 pos += p1;
136 }
137 }
138
139 av_log(c->avctx, AV_LOG_ERROR, "Camtasia warning: no End-of-picture code\n");
140 return 1;
141}
142
143/*
144 *
145 * Decode a frame
146 *
147 */
148static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
149{
150 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
151 unsigned char *encoded = (unsigned char *)buf;
152 unsigned char *outptr;
153#ifdef CONFIG_ZLIB
154 int zret; // Zlib return code
155#endif
156 int len = buf_size;
157
9d53d58e
MM
158 if(c->pic.data[0])
159 avctx->release_buffer(avctx, &c->pic);
160
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");
165 return -1;
166 }
167
168 outptr = c->pic.data[0]; // Output image pointer
169
170#ifdef CONFIG_ZLIB
171 zret = inflateReset(&(c->zstream));
172 if (zret != Z_OK) {
173 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
174 return -1;
175 }
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);
184 return -1;
185 }
186 encoded = c->decomp_buf;
187 len = c->decomp_size;
188 if(zret != Z_DATA_ERROR)
189 decode_rle(c);
229daca7
MM
190
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;
197 }
198 }
199
9d53d58e
MM
200#else
201 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
202 return -1;
203#endif
204
205 *data_size = sizeof(AVFrame);
206 *(AVFrame*)data = c->pic;
207
208 /* always report that the buffer was completely consumed */
209 return buf_size;
210}
211
212
213
214/*
215 *
216 * Init tscc decoder
217 *
218 */
219static int decode_init(AVCodecContext *avctx)
220{
221 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
222 int zret; // Zlib return code
223
224 c->avctx = avctx;
225 avctx->has_b_frames = 0;
226
227 c->pic.data[0] = NULL;
228 c->height = avctx->height;
229
230#ifdef CONFIG_ZLIB
231 // Needed if zlib unused or init aborted before inflateInit
232 memset(&(c->zstream), 0, sizeof(z_stream));
233#else
234 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
235 return 1;
236#endif
237 switch(avctx->bits_per_sample){
229daca7
MM
238 case 8: avctx->pix_fmt = PIX_FMT_PAL8; break;
239 case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
9d53d58e
MM
240 case 24: av_log(avctx, AV_LOG_ERROR, "Camtasia warning: RGB24 is just guessed\n");
241 avctx->pix_fmt = PIX_FMT_BGR24;
242 break;
243 default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_sample);
244 return -1;
245 }
246 c->bpp = avctx->bits_per_sample;
229daca7 247 c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;//RLE in the 'best' case
9d53d58e
MM
248
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");
253 return 1;
254 }
255 }
256
257#ifdef CONFIG_ZLIB
258 c->zstream.zalloc = Z_NULL;
259 c->zstream.zfree = Z_NULL;
260 c->zstream.opaque = Z_NULL;
261 zret = inflateInit(&(c->zstream));
262 if (zret != Z_OK) {
263 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
264 return 1;
265 }
266#endif
267
268 return 0;
269}
270
271
272
273/*
274 *
275 * Uninit tscc decoder
276 *
277 */
278static int decode_end(AVCodecContext *avctx)
279{
280 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
281
282 if (c->pic.data[0])
283 avctx->release_buffer(avctx, &c->pic);
284#ifdef CONFIG_ZLIB
285 inflateEnd(&(c->zstream));
286#endif
287
288 return 0;
289}
290
291AVCodec tscc_decoder = {
292 "camtasia",
293 CODEC_TYPE_VIDEO,
294 CODEC_ID_TSCC,
295 sizeof(CamtasiaContext),
296 decode_init,
297 NULL,
298 decode_end,
299 decode_frame,
300 CODEC_CAP_DR1,
301};
302