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