license header consistency cosmetics
[libav.git] / libavcodec / tscc.c
CommitLineData
9d53d58e
MM
1/*
2 * TechSmith Camtasia decoder
3 * Copyright (c) 2004 Konstantin Shishkov
4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
9d53d58e
MM
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
9d53d58e 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
9d53d58e
MM
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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
9d53d58e
MM
20 */
21
22/**
23 * @file tscc.c
24 * TechSmith Camtasia decoder
25 *
26 * Fourcc: TSCC
27 *
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
33 *
229daca7 34 * Supports: BGR8,BGR555,BGR24 - only BGR8 and BGR555 tested
9d53d58e
MM
35 *
36 */
37
38#include <stdio.h>
39#include <stdlib.h>
40
9d53d58e
MM
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 */
115329f1 74
cca1a426 75static int decode_rle(CamtasiaContext *c, unsigned int srcsize)
9d53d58e
MM
76{
77 unsigned char *src = c->decomp_buf;
cca1a426 78 unsigned char *output, *output_end;
9d53d58e 79 int p1, p2, line=c->height, pos=0, i;
1927cd41
MM
80 uint16_t pix16;
81 uint32_t pix32;
115329f1 82
229daca7 83 output = c->pic.data[0] + (c->height - 1) * c->pic.linesize[0];
cca1a426
RT
84 output_end = c->pic.data[0] + (c->height) * c->pic.linesize[0];
85 while(src < c->decomp_buf + srcsize) {
9d53d58e
MM
86 p1 = *src++;
87 if(p1 == 0) { //Escape code
88 p2 = *src++;
89 if(p2 == 0) { //End-of-line
90 output = c->pic.data[0] + (--line) * c->pic.linesize[0];
cca1a426
RT
91 if (line < 0)
92 return -1;
9d53d58e
MM
93 pos = 0;
94 continue;
95 } else if(p2 == 1) { //End-of-picture
96 return 0;
97 } else if(p2 == 2) { //Skip
98 p1 = *src++;
99 p2 = *src++;
100 line -= p2;
cca1a426
RT
101 if (line < 0)
102 return -1;
9d53d58e
MM
103 pos += p1;
104 output = c->pic.data[0] + line * c->pic.linesize[0] + pos * (c->bpp / 8);
105 continue;
106 }
107 // Copy data
cca1a426
RT
108 if (output + p2 * (c->bpp / 8) > output_end) {
109 src += p2 * (c->bpp / 8);
110 continue;
111 }
1927cd41
MM
112 if ((c->bpp == 8) || (c->bpp == 24)) {
113 for(i = 0; i < p2 * (c->bpp / 8); i++) {
114 *output++ = *src++;
115 }
116 // RLE8 copy is actually padded - and runs are not!
117 if(c->bpp == 8 && (p2 & 1)) {
118 src++;
119 }
120 } else if (c->bpp == 16) {
121 for(i = 0; i < p2; i++) {
fead30d4 122 pix16 = AV_RL16(src);
1927cd41
MM
123 src += 2;
124 *(uint16_t*)output = pix16;
125 output += 2;
126 }
127 } else if (c->bpp == 32) {
128 for(i = 0; i < p2; i++) {
fead30d4 129 pix32 = AV_RL32(src);
1927cd41
MM
130 src += 4;
131 *(uint32_t*)output = pix32;
132 output += 4;
133 }
bb270c08 134 }
9d53d58e
MM
135 pos += p2;
136 } else { //Run of pixels
b2eef2f0 137 int pix[4]; //original pixel
9d53d58e
MM
138 switch(c->bpp){
139 case 8: pix[0] = *src++;
140 break;
fead30d4 141 case 16: pix16 = AV_RL16(src);
1927cd41
MM
142 src += 2;
143 *(uint16_t*)pix = pix16;
9d53d58e
MM
144 break;
145 case 24: pix[0] = *src++;
146 pix[1] = *src++;
147 pix[2] = *src++;
148 break;
fead30d4 149 case 32: pix32 = AV_RL32(src);
1927cd41
MM
150 src += 4;
151 *(uint32_t*)pix = pix32;
b2eef2f0 152 break;
9d53d58e 153 }
cca1a426
RT
154 if (output + p1 * (c->bpp / 8) > output_end)
155 continue;
9d53d58e
MM
156 for(i = 0; i < p1; i++) {
157 switch(c->bpp){
158 case 8: *output++ = pix[0];
159 break;
1927cd41
MM
160 case 16: *(uint16_t*)output = pix16;
161 output += 2;
9d53d58e
MM
162 break;
163 case 24: *output++ = pix[0];
164 *output++ = pix[1];
165 *output++ = pix[2];
166 break;
1927cd41
MM
167 case 32: *(uint32_t*)output = pix32;
168 output += 4;
b2eef2f0 169 break;
9d53d58e
MM
170 }
171 }
172 pos += p1;
173 }
174 }
115329f1
DB
175
176 av_log(c->avctx, AV_LOG_ERROR, "Camtasia warning: no End-of-picture code\n");
9d53d58e
MM
177 return 1;
178}
179
180/*
181 *
182 * Decode a frame
183 *
184 */
185static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
186{
e4141433 187 CamtasiaContext * const c = avctx->priv_data;
9d53d58e
MM
188 unsigned char *encoded = (unsigned char *)buf;
189 unsigned char *outptr;
190#ifdef CONFIG_ZLIB
191 int zret; // Zlib return code
192#endif
193 int len = buf_size;
194
9d53d58e
MM
195 if(c->pic.data[0])
196 avctx->release_buffer(avctx, &c->pic);
197
198 c->pic.reference = 1;
199 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
200 if(avctx->get_buffer(avctx, &c->pic) < 0){
201 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
202 return -1;
203 }
204
205 outptr = c->pic.data[0]; // Output image pointer
206
207#ifdef CONFIG_ZLIB
208 zret = inflateReset(&(c->zstream));
209 if (zret != Z_OK) {
210 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
211 return -1;
212 }
213 c->zstream.next_in = encoded;
214 c->zstream.avail_in = len;
215 c->zstream.next_out = c->decomp_buf;
216 c->zstream.avail_out = c->decomp_size;
217 zret = inflate(&(c->zstream), Z_FINISH);
218 // Z_DATA_ERROR means empty picture
219 if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
220 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
221 return -1;
222 }
cca1a426
RT
223
224
9d53d58e 225 if(zret != Z_DATA_ERROR)
cca1a426 226 decode_rle(c, c->zstream.avail_out);
115329f1 227
229daca7
MM
228 /* make the palette available on the way out */
229 if (c->avctx->pix_fmt == PIX_FMT_PAL8) {
230 memcpy(c->pic.data[1], c->avctx->palctrl->palette, AVPALETTE_SIZE);
231 if (c->avctx->palctrl->palette_changed) {
232 c->pic.palette_has_changed = 1;
233 c->avctx->palctrl->palette_changed = 0;
234 }
235 }
236
9d53d58e
MM
237#else
238 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
239 return -1;
240#endif
241
242 *data_size = sizeof(AVFrame);
243 *(AVFrame*)data = c->pic;
244
245 /* always report that the buffer was completely consumed */
246 return buf_size;
247}
248
249
250
251/*
252 *
253 * Init tscc decoder
254 *
255 */
256static int decode_init(AVCodecContext *avctx)
257{
e4141433 258 CamtasiaContext * const c = avctx->priv_data;
9d53d58e
MM
259 int zret; // Zlib return code
260
261 c->avctx = avctx;
9d53d58e
MM
262
263 c->pic.data[0] = NULL;
264 c->height = avctx->height;
265
429914a3 266 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
cca1a426
RT
267 return 1;
268 }
269
9d53d58e
MM
270#ifdef CONFIG_ZLIB
271 // Needed if zlib unused or init aborted before inflateInit
115329f1 272 memset(&(c->zstream), 0, sizeof(z_stream));
9d53d58e
MM
273#else
274 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
275 return 1;
276#endif
277 switch(avctx->bits_per_sample){
229daca7
MM
278 case 8: avctx->pix_fmt = PIX_FMT_PAL8; break;
279 case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
b2eef2f0 280 case 24:
9d53d58e
MM
281 avctx->pix_fmt = PIX_FMT_BGR24;
282 break;
71e445fc 283 case 32: avctx->pix_fmt = PIX_FMT_RGB32; break;
9d53d58e 284 default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_sample);
115329f1 285 return -1;
9d53d58e
MM
286 }
287 c->bpp = avctx->bits_per_sample;
229daca7 288 c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;//RLE in the 'best' case
9d53d58e
MM
289
290 /* Allocate decompression buffer */
291 if (c->decomp_size) {
292 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
293 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
294 return 1;
295 }
296 }
115329f1 297
9d53d58e
MM
298#ifdef CONFIG_ZLIB
299 c->zstream.zalloc = Z_NULL;
300 c->zstream.zfree = Z_NULL;
301 c->zstream.opaque = Z_NULL;
302 zret = inflateInit(&(c->zstream));
303 if (zret != Z_OK) {
304 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
305 return 1;
306 }
307#endif
308
309 return 0;
310}
311
312
313
314/*
315 *
316 * Uninit tscc decoder
317 *
318 */
319static int decode_end(AVCodecContext *avctx)
320{
e4141433 321 CamtasiaContext * const c = avctx->priv_data;
9d53d58e 322
c2d57a34
MN
323 av_freep(&c->decomp_buf);
324
9d53d58e
MM
325 if (c->pic.data[0])
326 avctx->release_buffer(avctx, &c->pic);
327#ifdef CONFIG_ZLIB
328 inflateEnd(&(c->zstream));
329#endif
330
331 return 0;
332}
333
334AVCodec tscc_decoder = {
335 "camtasia",
336 CODEC_TYPE_VIDEO,
337 CODEC_ID_TSCC,
338 sizeof(CamtasiaContext),
339 decode_init,
340 NULL,
341 decode_end,
342 decode_frame,
343 CODEC_CAP_DR1,
344};
345