2004-08-14 18:08:09 +03:00
|
|
|
/*
|
|
|
|
* TechSmith Camtasia decoder
|
|
|
|
* Copyright (c) 2004 Konstantin Shishkov
|
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2004-08-14 18:08:09 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2004-08-14 18:08:09 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2004-08-14 18:08:09 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2004-08-14 18:08:09 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2004-08-14 18:08:09 +03:00
|
|
|
* TechSmith Camtasia decoder
|
|
|
|
*
|
|
|
|
* Fourcc: TSCC
|
|
|
|
*
|
|
|
|
* Codec is very simple:
|
|
|
|
* it codes picture (picture difference, really)
|
|
|
|
* with algorithm almost identical to Windows RLE8,
|
|
|
|
* only without padding and with greater pixel sizes,
|
|
|
|
* then this coded picture is packed with ZLib
|
|
|
|
*
|
2004-08-28 15:28:53 +03:00
|
|
|
* Supports: BGR8,BGR555,BGR24 - only BGR8 and BGR555 tested
|
2004-08-14 18:08:09 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
2012-11-10 15:22:56 +03:00
|
|
|
#include "internal.h"
|
2008-09-18 08:20:54 +03:00
|
|
|
#include "msrledec.h"
|
2004-08-14 18:08:09 +03:00
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decoder context
|
|
|
|
*/
|
|
|
|
typedef struct TsccContext {
|
|
|
|
|
|
|
|
AVCodecContext *avctx;
|
|
|
|
AVFrame pic;
|
|
|
|
|
|
|
|
// Bits per pixel
|
|
|
|
int bpp;
|
|
|
|
// Decompressed data size
|
|
|
|
unsigned int decomp_size;
|
|
|
|
// Decompression buffer
|
|
|
|
unsigned char* decomp_buf;
|
2012-03-31 20:10:54 +03:00
|
|
|
GetByteContext gb;
|
2004-08-14 18:08:09 +03:00
|
|
|
int height;
|
|
|
|
z_stream zstream;
|
2011-04-09 16:49:51 +03:00
|
|
|
|
|
|
|
uint32_t pal[256];
|
2004-08-14 18:08:09 +03:00
|
|
|
} CamtasiaContext;
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Decode a frame
|
|
|
|
*
|
|
|
|
*/
|
2012-11-13 21:35:22 +03:00
|
|
|
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
|
|
|
|
AVPacket *avpkt)
|
2004-08-14 18:08:09 +03:00
|
|
|
{
|
2009-04-07 18:59:50 +03:00
|
|
|
const uint8_t *buf = avpkt->data;
|
|
|
|
int buf_size = avpkt->size;
|
2007-04-08 23:24:16 +03:00
|
|
|
CamtasiaContext * const c = avctx->priv_data;
|
2008-02-01 17:58:29 +02:00
|
|
|
const unsigned char *encoded = buf;
|
2004-08-14 18:08:09 +03:00
|
|
|
int zret; // Zlib return code
|
2012-07-21 07:16:26 +03:00
|
|
|
int ret, len = buf_size;
|
2004-08-14 18:08:09 +03:00
|
|
|
|
2011-11-05 21:48:39 +03:00
|
|
|
c->pic.reference = 3;
|
2004-08-14 18:08:09 +03:00
|
|
|
c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
|
2012-08-17 17:43:50 +03:00
|
|
|
if((ret = avctx->reget_buffer(avctx, &c->pic)) < 0){
|
2004-08-14 18:08:09 +03:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
2012-07-21 07:16:26 +03:00
|
|
|
return ret;
|
2004-08-14 18:08:09 +03:00
|
|
|
}
|
|
|
|
|
2012-01-07 21:07:42 +03:00
|
|
|
zret = inflateReset(&c->zstream);
|
2004-08-14 18:08:09 +03:00
|
|
|
if (zret != Z_OK) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
|
2012-07-21 07:16:26 +03:00
|
|
|
return AVERROR(EINVAL);
|
2004-08-14 18:08:09 +03:00
|
|
|
}
|
2012-10-08 04:11:02 +03:00
|
|
|
c->zstream.next_in = (uint8_t*)encoded;
|
2004-08-14 18:08:09 +03:00
|
|
|
c->zstream.avail_in = len;
|
|
|
|
c->zstream.next_out = c->decomp_buf;
|
|
|
|
c->zstream.avail_out = c->decomp_size;
|
2012-01-07 21:07:42 +03:00
|
|
|
zret = inflate(&c->zstream, Z_FINISH);
|
2004-08-14 18:08:09 +03:00
|
|
|
// Z_DATA_ERROR means empty picture
|
|
|
|
if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
|
2012-07-21 07:16:26 +03:00
|
|
|
return AVERROR(EINVAL);
|
2004-08-14 18:08:09 +03:00
|
|
|
}
|
2005-01-23 23:36:24 +02:00
|
|
|
|
|
|
|
|
2012-03-31 20:10:54 +03:00
|
|
|
if (zret != Z_DATA_ERROR) {
|
|
|
|
bytestream2_init(&c->gb, c->decomp_buf,
|
|
|
|
c->decomp_size - c->zstream.avail_out);
|
|
|
|
ff_msrle_decode(avctx, (AVPicture*)&c->pic, c->bpp, &c->gb);
|
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2004-08-28 15:28:53 +03:00
|
|
|
/* make the palette available on the way out */
|
2012-10-06 13:10:34 +03:00
|
|
|
if (c->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
|
2011-04-09 16:49:51 +03:00
|
|
|
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
|
|
|
|
|
|
|
|
if (pal) {
|
2004-08-28 15:28:53 +03:00
|
|
|
c->pic.palette_has_changed = 1;
|
2011-04-09 16:49:51 +03:00
|
|
|
memcpy(c->pal, pal, AVPALETTE_SIZE);
|
2004-08-28 15:28:53 +03:00
|
|
|
}
|
2011-04-09 16:49:51 +03:00
|
|
|
memcpy(c->pic.data[1], c->pal, AVPALETTE_SIZE);
|
2004-08-28 15:28:53 +03:00
|
|
|
}
|
|
|
|
|
2012-11-13 21:35:22 +03:00
|
|
|
*got_frame = 1;
|
2004-08-14 18:08:09 +03:00
|
|
|
*(AVFrame*)data = c->pic;
|
|
|
|
|
|
|
|
/* always report that the buffer was completely consumed */
|
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Init tscc decoder
|
|
|
|
*
|
|
|
|
*/
|
2008-03-21 05:11:20 +02:00
|
|
|
static av_cold int decode_init(AVCodecContext *avctx)
|
2004-08-14 18:08:09 +03:00
|
|
|
{
|
2007-04-08 23:24:16 +03:00
|
|
|
CamtasiaContext * const c = avctx->priv_data;
|
2004-08-14 18:08:09 +03:00
|
|
|
int zret; // Zlib return code
|
|
|
|
|
|
|
|
c->avctx = avctx;
|
|
|
|
|
|
|
|
c->height = avctx->height;
|
|
|
|
|
2011-05-02 03:15:15 +03:00
|
|
|
avcodec_get_frame_defaults(&c->pic);
|
2004-08-14 18:08:09 +03:00
|
|
|
// Needed if zlib unused or init aborted before inflateInit
|
2012-01-07 21:07:42 +03:00
|
|
|
memset(&c->zstream, 0, sizeof(z_stream));
|
2008-09-08 17:24:59 +03:00
|
|
|
switch(avctx->bits_per_coded_sample){
|
2012-10-06 13:10:34 +03:00
|
|
|
case 8: avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
|
|
|
|
case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
|
2005-02-01 00:34:02 +02:00
|
|
|
case 24:
|
2012-10-06 13:10:34 +03:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_BGR24;
|
2004-08-14 18:08:09 +03:00
|
|
|
break;
|
2012-10-06 13:10:34 +03:00
|
|
|
case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break;
|
2008-09-08 17:24:59 +03:00
|
|
|
default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
|
2012-07-21 07:16:26 +03:00
|
|
|
return AVERROR_PATCHWELCOME;
|
2004-08-14 18:08:09 +03:00
|
|
|
}
|
2008-09-08 17:24:59 +03:00
|
|
|
c->bpp = avctx->bits_per_coded_sample;
|
2011-10-05 15:12:42 +03:00
|
|
|
// buffer size for RLE 'best' case when 2-byte code precedes each pixel and there may be padding after it too
|
2010-01-11 16:21:53 +02:00
|
|
|
c->decomp_size = (((avctx->width * c->bpp + 7) >> 3) + 3 * avctx->width + 2) * avctx->height + 2;
|
2004-08-14 18:08:09 +03:00
|
|
|
|
|
|
|
/* Allocate decompression buffer */
|
|
|
|
if (c->decomp_size) {
|
|
|
|
if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
|
2012-07-21 07:16:26 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2004-08-14 18:08:09 +03:00
|
|
|
}
|
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2004-08-14 18:08:09 +03:00
|
|
|
c->zstream.zalloc = Z_NULL;
|
|
|
|
c->zstream.zfree = Z_NULL;
|
|
|
|
c->zstream.opaque = Z_NULL;
|
2012-01-07 21:07:42 +03:00
|
|
|
zret = inflateInit(&c->zstream);
|
2004-08-14 18:08:09 +03:00
|
|
|
if (zret != Z_OK) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
|
2012-07-21 07:16:26 +03:00
|
|
|
return AVERROR(ENOMEM);
|
2004-08-14 18:08:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Uninit tscc decoder
|
|
|
|
*
|
|
|
|
*/
|
2008-03-21 05:11:20 +02:00
|
|
|
static av_cold int decode_end(AVCodecContext *avctx)
|
2004-08-14 18:08:09 +03:00
|
|
|
{
|
2007-04-08 23:24:16 +03:00
|
|
|
CamtasiaContext * const c = avctx->priv_data;
|
2004-08-14 18:08:09 +03:00
|
|
|
|
2005-02-17 02:00:20 +02:00
|
|
|
av_freep(&c->decomp_buf);
|
|
|
|
|
2004-08-14 18:08:09 +03:00
|
|
|
if (c->pic.data[0])
|
|
|
|
avctx->release_buffer(avctx, &c->pic);
|
2012-01-07 21:07:42 +03:00
|
|
|
inflateEnd(&c->zstream);
|
2004-08-14 18:08:09 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 23:40:11 +02:00
|
|
|
AVCodec ff_tscc_decoder = {
|
2011-07-17 13:54:31 +03:00
|
|
|
.name = "camtasia",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 12:11:04 +03:00
|
|
|
.id = AV_CODEC_ID_TSCC,
|
2011-07-17 13:54:31 +03:00
|
|
|
.priv_data_size = sizeof(CamtasiaContext),
|
|
|
|
.init = decode_init,
|
|
|
|
.close = decode_end,
|
|
|
|
.decode = decode_frame,
|
|
|
|
.capabilities = CODEC_CAP_DR1,
|
2011-08-04 00:27:50 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
|
2004-08-14 18:08:09 +03:00
|
|
|
};
|