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 "internal.h"
00043 #include "msrledec.h"
00044
00045 #include <zlib.h>
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 GetByteContext gb;
00063 int height;
00064 z_stream zstream;
00065
00066 uint32_t pal[256];
00067 } CamtasiaContext;
00068
00069
00070
00071
00072
00073
00074 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
00075 AVPacket *avpkt)
00076 {
00077 const uint8_t *buf = avpkt->data;
00078 int buf_size = avpkt->size;
00079 CamtasiaContext * const c = avctx->priv_data;
00080 const unsigned char *encoded = buf;
00081 int zret;
00082 int ret, len = buf_size;
00083
00084 c->pic.reference = 3;
00085 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
00086 if((ret = avctx->reget_buffer(avctx, &c->pic)) < 0){
00087 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00088 return ret;
00089 }
00090
00091 zret = inflateReset(&c->zstream);
00092 if (zret != Z_OK) {
00093 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
00094 return AVERROR(EINVAL);
00095 }
00096 c->zstream.next_in = (uint8_t*)encoded;
00097 c->zstream.avail_in = len;
00098 c->zstream.next_out = c->decomp_buf;
00099 c->zstream.avail_out = c->decomp_size;
00100 zret = inflate(&c->zstream, Z_FINISH);
00101
00102 if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
00103 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
00104 return AVERROR(EINVAL);
00105 }
00106
00107
00108 if (zret != Z_DATA_ERROR) {
00109 bytestream2_init(&c->gb, c->decomp_buf,
00110 c->decomp_size - c->zstream.avail_out);
00111 ff_msrle_decode(avctx, (AVPicture*)&c->pic, c->bpp, &c->gb);
00112 }
00113
00114
00115 if (c->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
00116 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
00117
00118 if (pal) {
00119 c->pic.palette_has_changed = 1;
00120 memcpy(c->pal, pal, AVPALETTE_SIZE);
00121 }
00122 memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
00123 }
00124
00125 *got_frame = 1;
00126 *(AVFrame*)data = c->pic;
00127
00128
00129 return buf_size;
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139 static av_cold int decode_init(AVCodecContext *avctx)
00140 {
00141 CamtasiaContext * const c = avctx->priv_data;
00142 int zret;
00143
00144 c->avctx = avctx;
00145
00146 c->height = avctx->height;
00147
00148 avcodec_get_frame_defaults(&c->pic);
00149
00150 memset(&c->zstream, 0, sizeof(z_stream));
00151 switch(avctx->bits_per_coded_sample){
00152 case 8: avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
00153 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
00154 case 24:
00155 avctx->pix_fmt = AV_PIX_FMT_BGR24;
00156 break;
00157 case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break;
00158 default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
00159 return AVERROR_PATCHWELCOME;
00160 }
00161 c->bpp = avctx->bits_per_coded_sample;
00162
00163 c->decomp_size = (((avctx->width * c->bpp + 7) >> 3) + 3 * avctx->width + 2) * avctx->height + 2;
00164
00165
00166 if (c->decomp_size) {
00167 if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
00168 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
00169 return AVERROR(ENOMEM);
00170 }
00171 }
00172
00173 c->zstream.zalloc = Z_NULL;
00174 c->zstream.zfree = Z_NULL;
00175 c->zstream.opaque = Z_NULL;
00176 zret = inflateInit(&c->zstream);
00177 if (zret != Z_OK) {
00178 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
00179 return AVERROR(ENOMEM);
00180 }
00181
00182 return 0;
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192 static av_cold int decode_end(AVCodecContext *avctx)
00193 {
00194 CamtasiaContext * const c = avctx->priv_data;
00195
00196 av_freep(&c->decomp_buf);
00197
00198 if (c->pic.data[0])
00199 avctx->release_buffer(avctx, &c->pic);
00200 inflateEnd(&c->zstream);
00201
00202 return 0;
00203 }
00204
00205 AVCodec ff_tscc_decoder = {
00206 .name = "camtasia",
00207 .type = AVMEDIA_TYPE_VIDEO,
00208 .id = AV_CODEC_ID_TSCC,
00209 .priv_data_size = sizeof(CamtasiaContext),
00210 .init = decode_init,
00211 .close = decode_end,
00212 .decode = decode_frame,
00213 .capabilities = CODEC_CAP_DR1,
00214 .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
00215 };