00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039
00040 #include "common.h"
00041 #include "avcodec.h"
00042
00043 #ifdef CONFIG_ZLIB
00044 #include <zlib.h>
00045 #endif
00046
00047
00048
00049
00050
00051 typedef struct TsccContext {
00052
00053 AVCodecContext *avctx;
00054 AVFrame pic;
00055
00056
00057 int bpp;
00058
00059 unsigned int decomp_size;
00060
00061 unsigned char* decomp_buf;
00062 int height;
00063 #ifdef CONFIG_ZLIB
00064 z_stream zstream;
00065 #endif
00066 } CamtasiaContext;
00067
00068
00069
00070
00071
00072
00073
00074
00075 static int decode_rle(CamtasiaContext *c, unsigned int srcsize)
00076 {
00077 unsigned char *src = c->decomp_buf;
00078 unsigned char *output, *output_end;
00079 int p1, p2, line=c->height, pos=0, i;
00080
00081 output = c->pic.data[0] + (c->height - 1) * c->pic.linesize[0];
00082 output_end = c->pic.data[0] + (c->height) * c->pic.linesize[0];
00083 while(src < c->decomp_buf + srcsize) {
00084 p1 = *src++;
00085 if(p1 == 0) {
00086 p2 = *src++;
00087 if(p2 == 0) {
00088 output = c->pic.data[0] + (--line) * c->pic.linesize[0];
00089 if (line < 0)
00090 return -1;
00091 pos = 0;
00092 continue;
00093 } else if(p2 == 1) {
00094 return 0;
00095 } else if(p2 == 2) {
00096 p1 = *src++;
00097 p2 = *src++;
00098 line -= p2;
00099 if (line < 0)
00100 return -1;
00101 pos += p1;
00102 output = c->pic.data[0] + line * c->pic.linesize[0] + pos * (c->bpp / 8);
00103 continue;
00104 }
00105
00106 if (output + p2 * (c->bpp / 8) > output_end) {
00107 src += p2 * (c->bpp / 8);
00108 continue;
00109 }
00110 for(i = 0; i < p2 * (c->bpp / 8); i++) {
00111 *output++ = *src++;
00112 }
00113
00114 if(c->bpp == 8 && (p2 & 1)) {
00115 src++;
00116 }
00117 pos += p2;
00118 } else {
00119 int pix[4];
00120 switch(c->bpp){
00121 case 8: pix[0] = *src++;
00122 break;
00123 case 16: pix[0] = *src++;
00124 pix[1] = *src++;
00125 break;
00126 case 24: pix[0] = *src++;
00127 pix[1] = *src++;
00128 pix[2] = *src++;
00129 break;
00130 case 32: pix[0] = *src++;
00131 pix[1] = *src++;
00132 pix[2] = *src++;
00133 pix[3] = *src++;
00134 break;
00135 }
00136 if (output + p1 * (c->bpp / 8) > output_end)
00137 continue;
00138 for(i = 0; i < p1; i++) {
00139 switch(c->bpp){
00140 case 8: *output++ = pix[0];
00141 break;
00142 case 16: *output++ = pix[0];
00143 *output++ = pix[1];
00144 break;
00145 case 24: *output++ = pix[0];
00146 *output++ = pix[1];
00147 *output++ = pix[2];
00148 break;
00149 case 32: *output++ = pix[0];
00150 *output++ = pix[1];
00151 *output++ = pix[2];
00152 *output++ = pix[3];
00153 break;
00154 }
00155 }
00156 pos += p1;
00157 }
00158 }
00159
00160 av_log(c->avctx, AV_LOG_ERROR, "Camtasia warning: no End-of-picture code\n");
00161 return 1;
00162 }
00163
00164
00165
00166
00167
00168
00169 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
00170 {
00171 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
00172 unsigned char *encoded = (unsigned char *)buf;
00173 unsigned char *outptr;
00174 #ifdef CONFIG_ZLIB
00175 int zret;
00176 #endif
00177 int len = buf_size;
00178
00179 if(c->pic.data[0])
00180 avctx->release_buffer(avctx, &c->pic);
00181
00182 c->pic.reference = 1;
00183 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
00184 if(avctx->get_buffer(avctx, &c->pic) < 0){
00185 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00186 return -1;
00187 }
00188
00189 outptr = c->pic.data[0];
00190
00191 #ifdef CONFIG_ZLIB
00192 zret = inflateReset(&(c->zstream));
00193 if (zret != Z_OK) {
00194 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
00195 return -1;
00196 }
00197 c->zstream.next_in = encoded;
00198 c->zstream.avail_in = len;
00199 c->zstream.next_out = c->decomp_buf;
00200 c->zstream.avail_out = c->decomp_size;
00201 zret = inflate(&(c->zstream), Z_FINISH);
00202
00203 if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
00204 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
00205 return -1;
00206 }
00207
00208
00209 if(zret != Z_DATA_ERROR)
00210 decode_rle(c, c->zstream.avail_out);
00211
00212
00213 if (c->avctx->pix_fmt == PIX_FMT_PAL8) {
00214 memcpy(c->pic.data[1], c->avctx->palctrl->palette, AVPALETTE_SIZE);
00215 if (c->avctx->palctrl->palette_changed) {
00216 c->pic.palette_has_changed = 1;
00217 c->avctx->palctrl->palette_changed = 0;
00218 }
00219 }
00220
00221 #else
00222 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
00223 return -1;
00224 #endif
00225
00226 *data_size = sizeof(AVFrame);
00227 *(AVFrame*)data = c->pic;
00228
00229
00230 return buf_size;
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240 static int decode_init(AVCodecContext *avctx)
00241 {
00242 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
00243 int zret;
00244
00245 c->avctx = avctx;
00246 avctx->has_b_frames = 0;
00247
00248 c->pic.data[0] = NULL;
00249 c->height = avctx->height;
00250
00251 if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
00252 return 1;
00253 }
00254
00255 #ifdef CONFIG_ZLIB
00256
00257 memset(&(c->zstream), 0, sizeof(z_stream));
00258 #else
00259 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
00260 return 1;
00261 #endif
00262 switch(avctx->bits_per_sample){
00263 case 8: avctx->pix_fmt = PIX_FMT_PAL8; break;
00264 case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
00265 case 24:
00266 avctx->pix_fmt = PIX_FMT_BGR24;
00267 break;
00268 case 32: avctx->pix_fmt = PIX_FMT_RGBA32; break;
00269 default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_sample);
00270 return -1;
00271 }
00272 c->bpp = avctx->bits_per_sample;
00273 c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;
00274
00275
00276 if (c->decomp_size) {
00277 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
00278 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
00279 return 1;
00280 }
00281 }
00282
00283 #ifdef CONFIG_ZLIB
00284 c->zstream.zalloc = Z_NULL;
00285 c->zstream.zfree = Z_NULL;
00286 c->zstream.opaque = Z_NULL;
00287 zret = inflateInit(&(c->zstream));
00288 if (zret != Z_OK) {
00289 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
00290 return 1;
00291 }
00292 #endif
00293
00294 return 0;
00295 }
00296
00297
00298
00299
00300
00301
00302
00303
00304 static int decode_end(AVCodecContext *avctx)
00305 {
00306 CamtasiaContext * const c = (CamtasiaContext *)avctx->priv_data;
00307
00308 av_freep(&c->decomp_buf);
00309
00310 if (c->pic.data[0])
00311 avctx->release_buffer(avctx, &c->pic);
00312 #ifdef CONFIG_ZLIB
00313 inflateEnd(&(c->zstream));
00314 #endif
00315
00316 return 0;
00317 }
00318
00319 AVCodec tscc_decoder = {
00320 "camtasia",
00321 CODEC_TYPE_VIDEO,
00322 CODEC_ID_TSCC,
00323 sizeof(CamtasiaContext),
00324 decode_init,
00325 NULL,
00326 decode_end,
00327 decode_frame,
00328 CODEC_CAP_DR1,
00329 };
00330