00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040
00041 #include "avcodec.h"
00042 #include "msrledec.h"
00043
00044 #include <zlib.h>
00045
00046
00047
00048
00049
00050 typedef struct TsccContext {
00051
00052 AVCodecContext *avctx;
00053 AVFrame pic;
00054
00055
00056 int bpp;
00057
00058 unsigned int decomp_size;
00059
00060 unsigned char* decomp_buf;
00061 int height;
00062 z_stream zstream;
00063 } CamtasiaContext;
00064
00065
00066
00067
00068
00069
00070 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size)
00071 {
00072 CamtasiaContext * const c = avctx->priv_data;
00073 const unsigned char *encoded = buf;
00074 unsigned char *outptr;
00075 int zret;
00076 int len = buf_size;
00077
00078 if(c->pic.data[0])
00079 avctx->release_buffer(avctx, &c->pic);
00080
00081 c->pic.reference = 1;
00082 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
00083 if(avctx->get_buffer(avctx, &c->pic) < 0){
00084 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00085 return -1;
00086 }
00087
00088 outptr = c->pic.data[0];
00089
00090 zret = inflateReset(&(c->zstream));
00091 if (zret != Z_OK) {
00092 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
00093 return -1;
00094 }
00095 c->zstream.next_in = encoded;
00096 c->zstream.avail_in = len;
00097 c->zstream.next_out = c->decomp_buf;
00098 c->zstream.avail_out = c->decomp_size;
00099 zret = inflate(&(c->zstream), Z_FINISH);
00100
00101 if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
00102 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
00103 return -1;
00104 }
00105
00106
00107 if(zret != Z_DATA_ERROR)
00108 ff_msrle_decode(avctx, (AVPicture*)&c->pic, c->bpp, c->decomp_buf, c->zstream.avail_out);
00109
00110
00111 if (c->avctx->pix_fmt == PIX_FMT_PAL8) {
00112 memcpy(c->pic.data[1], c->avctx->palctrl->palette, AVPALETTE_SIZE);
00113 if (c->avctx->palctrl->palette_changed) {
00114 c->pic.palette_has_changed = 1;
00115 c->avctx->palctrl->palette_changed = 0;
00116 }
00117 }
00118
00119 *data_size = sizeof(AVFrame);
00120 *(AVFrame*)data = c->pic;
00121
00122
00123 return buf_size;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133 static av_cold int decode_init(AVCodecContext *avctx)
00134 {
00135 CamtasiaContext * const c = avctx->priv_data;
00136 int zret;
00137
00138 c->avctx = avctx;
00139
00140 c->pic.data[0] = NULL;
00141 c->height = avctx->height;
00142
00143 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
00144 return 1;
00145 }
00146
00147
00148 memset(&(c->zstream), 0, sizeof(z_stream));
00149 switch(avctx->bits_per_coded_sample){
00150 case 8: avctx->pix_fmt = PIX_FMT_PAL8; break;
00151 case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
00152 case 24:
00153 avctx->pix_fmt = PIX_FMT_BGR24;
00154 break;
00155 case 32: avctx->pix_fmt = PIX_FMT_RGB32; break;
00156 default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
00157 return -1;
00158 }
00159 c->bpp = avctx->bits_per_coded_sample;
00160 c->decomp_size = (avctx->width * c->bpp + (avctx->width + 254) / 255 + 2) * avctx->height + 2;
00161
00162
00163 if (c->decomp_size) {
00164 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
00165 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
00166 return 1;
00167 }
00168 }
00169
00170 c->zstream.zalloc = Z_NULL;
00171 c->zstream.zfree = Z_NULL;
00172 c->zstream.opaque = Z_NULL;
00173 zret = inflateInit(&(c->zstream));
00174 if (zret != Z_OK) {
00175 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
00176 return 1;
00177 }
00178
00179 return 0;
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189 static av_cold int decode_end(AVCodecContext *avctx)
00190 {
00191 CamtasiaContext * const c = avctx->priv_data;
00192
00193 av_freep(&c->decomp_buf);
00194
00195 if (c->pic.data[0])
00196 avctx->release_buffer(avctx, &c->pic);
00197 inflateEnd(&(c->zstream));
00198
00199 return 0;
00200 }
00201
00202 AVCodec tscc_decoder = {
00203 "camtasia",
00204 CODEC_TYPE_VIDEO,
00205 CODEC_ID_TSCC,
00206 sizeof(CamtasiaContext),
00207 decode_init,
00208 NULL,
00209 decode_end,
00210 decode_frame,
00211 CODEC_CAP_DR1,
00212 .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
00213 };
00214