fix demuxing of Video006.3gp
[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
5509bffa 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
9d53d58e
MM
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 */
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;
115329f1 80
229daca7 81 output = c->pic.data[0] + (c->height - 1) * c->pic.linesize[0];
cca1a426
RT
82 output_end = c->pic.data[0] + (c->height) * c->pic.linesize[0];
83 while(src < c->decomp_buf + srcsize) {
9d53d58e
MM
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];
cca1a426
RT
89 if (line < 0)
90 return -1;
9d53d58e
MM
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;
cca1a426
RT
99 if (line < 0)
100 return -1;
9d53d58e
MM
101 pos += p1;
102 output = c->pic.data[0] + line * c->pic.linesize[0] + pos * (c->bpp / 8);
103 continue;
104 }
105 // Copy data
cca1a426
RT
106 if (output + p2 * (c->bpp / 8) > output_end) {
107 src += p2 * (c->bpp / 8);
108 continue;
109 }
9d53d58e
MM
110 for(i = 0; i < p2 * (c->bpp / 8); i++) {
111 *output++ = *src++;
112 }
bb270c08
DB
113 // RLE8 copy is actually padded - and runs are not!
114 if(c->bpp == 8 && (p2 & 1)) {
115 src++;
116 }
9d53d58e
MM
117 pos += p2;
118 } else { //Run of pixels
b2eef2f0 119 int pix[4]; //original pixel
9d53d58e
MM
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;
b2eef2f0
RT
130 case 32: pix[0] = *src++;
131 pix[1] = *src++;
132 pix[2] = *src++;
133 pix[3] = *src++;
134 break;
9d53d58e 135 }
cca1a426
RT
136 if (output + p1 * (c->bpp / 8) > output_end)
137 continue;
9d53d58e
MM
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;
b2eef2f0
RT
149 case 32: *output++ = pix[0];
150 *output++ = pix[1];
151 *output++ = pix[2];
152 *output++ = pix[3];
153 break;
9d53d58e
MM
154 }
155 }
156 pos += p1;
157 }
158 }
115329f1
DB
159
160 av_log(c->avctx, AV_LOG_ERROR, "Camtasia warning: no End-of-picture code\n");
9d53d58e
MM
161 return 1;
162}
163
164/*
165 *
166 * Decode a frame
167 *
168 */
169static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
170{
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;
178
9d53d58e
MM
179 if(c->pic.data[0])
180 avctx->release_buffer(avctx, &c->pic);
181
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;
187 }
188
189 outptr = c->pic.data[0]; // Output image pointer
190
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;
196 }
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;
206 }
cca1a426
RT
207
208
9d53d58e 209 if(zret != Z_DATA_ERROR)
cca1a426 210 decode_rle(c, c->zstream.avail_out);
115329f1 211
229daca7
MM
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;
218 }
219 }
220
9d53d58e
MM
221#else
222 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
223 return -1;
224#endif
225
226 *data_size = sizeof(AVFrame);
227 *(AVFrame*)data = c->pic;
228
229 /* always report that the buffer was completely consumed */
230 return buf_size;
231}
232
233
234
235/*
236 *
237 * Init tscc decoder
238 *
239 */
240static int decode_init(AVCodecContext *avctx)
241{
242 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
243 int zret; // Zlib return code
244
245 c->avctx = avctx;
246 avctx->has_b_frames = 0;
247
248 c->pic.data[0] = NULL;
249 c->height = avctx->height;
250
cca1a426
RT
251 if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
252 return 1;
253 }
254
9d53d58e
MM
255#ifdef CONFIG_ZLIB
256 // Needed if zlib unused or init aborted before inflateInit
115329f1 257 memset(&(c->zstream), 0, sizeof(z_stream));
9d53d58e
MM
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){
229daca7
MM
263 case 8: avctx->pix_fmt = PIX_FMT_PAL8; break;
264 case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
b2eef2f0 265 case 24:
9d53d58e
MM
266 avctx->pix_fmt = PIX_FMT_BGR24;
267 break;
b2eef2f0 268 case 32: avctx->pix_fmt = PIX_FMT_RGBA32; break;
9d53d58e 269 default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_sample);
115329f1 270 return -1;
9d53d58e
MM
271 }
272 c->bpp = avctx->bits_per_sample;
229daca7 273 c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;//RLE in the 'best' case
9d53d58e
MM
274
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;
280 }
281 }
115329f1 282
9d53d58e
MM
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;
291 }
292#endif
293
294 return 0;
295}
296
297
298
299/*
300 *
301 * Uninit tscc decoder
302 *
303 */
304static int decode_end(AVCodecContext *avctx)
305{
306 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
307
c2d57a34
MN
308 av_freep(&c->decomp_buf);
309
9d53d58e
MM
310 if (c->pic.data[0])
311 avctx->release_buffer(avctx, &c->pic);
312#ifdef CONFIG_ZLIB
313 inflateEnd(&(c->zstream));
314#endif
315
316 return 0;
317}
318
319AVCodec 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,
329};
330