2004-08-14 15:08:09 +00:00
|
|
|
/*
|
|
|
|
* TechSmith Camtasia decoder
|
|
|
|
* Copyright (c) 2004 Konstantin Shishkov
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* This file is part of Libav.
|
2006-10-07 15:30:46 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2004-08-14 15:08:09 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 15:30:46 +00:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2004-08-14 15:08:09 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2004-08-14 15:08:09 +00: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
|
2011-03-18 17:35:10 +00:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2006-01-12 22:43:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2004-08-14 15:08:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 14:45:34 +00:00
|
|
|
* @file
|
2004-08-14 15:08:09 +00: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 12:28:53 +00:00
|
|
|
* Supports: BGR8,BGR555,BGR24 - only BGR8 and BGR555 tested
|
2004-08-14 15:08:09 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "avcodec.h"
|
2012-11-10 12:22:56 +00:00
|
|
|
#include "internal.h"
|
2008-09-18 05:20:54 +00:00
|
|
|
#include "msrledec.h"
|
2004-08-14 15:08:09 +00:00
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
typedef struct TsccContext {
|
|
|
|
|
|
|
|
AVCodecContext *avctx;
|
|
|
|
|
|
|
|
// Bits per pixel
|
|
|
|
int bpp;
|
|
|
|
// Decompressed data size
|
|
|
|
unsigned int decomp_size;
|
|
|
|
// Decompression buffer
|
|
|
|
unsigned char* decomp_buf;
|
2012-03-31 17:10:54 +00:00
|
|
|
GetByteContext gb;
|
2004-08-14 15:08:09 +00:00
|
|
|
int height;
|
|
|
|
z_stream zstream;
|
2011-04-09 13:49:51 +00:00
|
|
|
|
|
|
|
uint32_t pal[256];
|
2004-08-14 15:08:09 +00:00
|
|
|
} CamtasiaContext;
|
|
|
|
|
2012-11-13 18:35:22 +00:00
|
|
|
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
|
|
|
|
AVPacket *avpkt)
|
2004-08-14 15:08:09 +00:00
|
|
|
{
|
2009-04-07 15:59:50 +00:00
|
|
|
const uint8_t *buf = avpkt->data;
|
|
|
|
int buf_size = avpkt->size;
|
2007-04-08 20:24:16 +00:00
|
|
|
CamtasiaContext * const c = avctx->priv_data;
|
2012-11-21 20:34:46 +00:00
|
|
|
AVFrame *frame = data;
|
2014-07-29 16:57:48 +00:00
|
|
|
int ret;
|
2004-08-14 15:08:09 +00:00
|
|
|
|
2012-11-21 20:34:46 +00:00
|
|
|
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0){
|
2004-08-14 15:08:09 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
2012-11-20 06:13:49 +00:00
|
|
|
return ret;
|
2004-08-14 15:08:09 +00:00
|
|
|
}
|
|
|
|
|
2014-07-29 16:57:48 +00:00
|
|
|
ret = inflateReset(&c->zstream);
|
|
|
|
if (ret != Z_OK) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
|
2012-11-20 06:13:49 +00:00
|
|
|
return AVERROR_UNKNOWN;
|
2004-08-14 15:08:09 +00:00
|
|
|
}
|
2014-07-29 16:57:48 +00:00
|
|
|
c->zstream.next_in = buf;
|
|
|
|
c->zstream.avail_in = buf_size;
|
2004-08-14 15:08:09 +00:00
|
|
|
c->zstream.next_out = c->decomp_buf;
|
|
|
|
c->zstream.avail_out = c->decomp_size;
|
2014-07-29 16:57:48 +00:00
|
|
|
ret = inflate(&c->zstream, Z_FINISH);
|
2004-08-14 15:08:09 +00:00
|
|
|
// Z_DATA_ERROR means empty picture
|
2014-07-29 16:57:48 +00:00
|
|
|
if ((ret != Z_OK) && (ret != Z_STREAM_END) && (ret != Z_DATA_ERROR)) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
|
2012-11-20 06:13:49 +00:00
|
|
|
return AVERROR_UNKNOWN;
|
2004-08-14 15:08:09 +00:00
|
|
|
}
|
2005-01-23 21:36:24 +00:00
|
|
|
|
|
|
|
|
2014-07-29 16:57:48 +00:00
|
|
|
if (ret != Z_DATA_ERROR) {
|
2012-03-31 17:10:54 +00:00
|
|
|
bytestream2_init(&c->gb, c->decomp_buf,
|
|
|
|
c->decomp_size - c->zstream.avail_out);
|
2015-10-14 09:33:18 +00:00
|
|
|
ff_msrle_decode(avctx, frame, c->bpp, &c->gb);
|
2012-03-31 17:10:54 +00:00
|
|
|
}
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2004-08-28 12:28:53 +00:00
|
|
|
/* make the palette available on the way out */
|
2012-10-06 10:10:34 +00:00
|
|
|
if (c->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
|
2011-04-09 13:49:51 +00:00
|
|
|
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
|
|
|
|
|
|
|
|
if (pal) {
|
2012-11-21 20:34:46 +00:00
|
|
|
frame->palette_has_changed = 1;
|
2011-04-09 13:49:51 +00:00
|
|
|
memcpy(c->pal, pal, AVPALETTE_SIZE);
|
2004-08-28 12:28:53 +00:00
|
|
|
}
|
2012-11-21 20:34:46 +00:00
|
|
|
memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
|
2004-08-28 12:28:53 +00:00
|
|
|
}
|
|
|
|
|
2012-11-13 18:35:22 +00:00
|
|
|
*got_frame = 1;
|
2004-08-14 15:08:09 +00:00
|
|
|
|
|
|
|
/* always report that the buffer was completely consumed */
|
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
|
2008-03-21 03:11:20 +00:00
|
|
|
static av_cold int decode_init(AVCodecContext *avctx)
|
2004-08-14 15:08:09 +00:00
|
|
|
{
|
2007-04-08 20:24:16 +00:00
|
|
|
CamtasiaContext * const c = avctx->priv_data;
|
2004-08-14 15:08:09 +00:00
|
|
|
int zret; // Zlib return code
|
|
|
|
|
|
|
|
c->avctx = avctx;
|
|
|
|
|
|
|
|
c->height = avctx->height;
|
|
|
|
|
|
|
|
// Needed if zlib unused or init aborted before inflateInit
|
2012-01-07 18:07:42 +00:00
|
|
|
memset(&c->zstream, 0, sizeof(z_stream));
|
2008-09-08 14:24:59 +00:00
|
|
|
switch(avctx->bits_per_coded_sample){
|
2012-10-06 10:10:34 +00:00
|
|
|
case 8: avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
|
|
|
|
case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
|
2005-01-31 22:34:02 +00:00
|
|
|
case 24:
|
2012-10-06 10:10:34 +00:00
|
|
|
avctx->pix_fmt = AV_PIX_FMT_BGR24;
|
2004-08-14 15:08:09 +00:00
|
|
|
break;
|
2012-10-06 10:10:34 +00:00
|
|
|
case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break;
|
2008-09-08 14:24:59 +00:00
|
|
|
default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
|
2012-11-20 06:13:49 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2004-08-14 15:08:09 +00:00
|
|
|
}
|
2008-09-08 14:24:59 +00:00
|
|
|
c->bpp = avctx->bits_per_coded_sample;
|
2011-10-05 12:12:42 +00: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 14:21:53 +00:00
|
|
|
c->decomp_size = (((avctx->width * c->bpp + 7) >> 3) + 3 * avctx->width + 2) * avctx->height + 2;
|
2004-08-14 15:08:09 +00:00
|
|
|
|
|
|
|
/* Allocate decompression buffer */
|
|
|
|
if (c->decomp_size) {
|
2014-08-14 20:31:24 +00:00
|
|
|
if (!(c->decomp_buf = av_malloc(c->decomp_size))) {
|
2004-08-14 15:08:09 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
|
2012-11-20 06:13:49 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2004-08-14 15:08:09 +00:00
|
|
|
}
|
|
|
|
}
|
2005-12-17 18:14:38 +00:00
|
|
|
|
2004-08-14 15:08:09 +00:00
|
|
|
c->zstream.zalloc = Z_NULL;
|
|
|
|
c->zstream.zfree = Z_NULL;
|
|
|
|
c->zstream.opaque = Z_NULL;
|
2012-01-07 18:07:42 +00:00
|
|
|
zret = inflateInit(&c->zstream);
|
2004-08-14 15:08:09 +00:00
|
|
|
if (zret != Z_OK) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
|
2012-11-20 06:13:49 +00:00
|
|
|
return AVERROR_UNKNOWN;
|
2004-08-14 15:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-21 03:11:20 +00:00
|
|
|
static av_cold int decode_end(AVCodecContext *avctx)
|
2004-08-14 15:08:09 +00:00
|
|
|
{
|
2007-04-08 20:24:16 +00:00
|
|
|
CamtasiaContext * const c = avctx->priv_data;
|
2004-08-14 15:08:09 +00:00
|
|
|
|
2005-02-17 00:00:20 +00:00
|
|
|
av_freep(&c->decomp_buf);
|
|
|
|
|
2012-01-07 18:07:42 +00:00
|
|
|
inflateEnd(&c->zstream);
|
2004-08-14 15:08:09 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 21:40:11 +00:00
|
|
|
AVCodec ff_tscc_decoder = {
|
2011-07-17 10:54:31 +00:00
|
|
|
.name = "camtasia",
|
2013-10-03 20:57:53 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
|
2011-07-17 10:54:31 +00:00
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 09:11:04 +00:00
|
|
|
.id = AV_CODEC_ID_TSCC,
|
2011-07-17 10:54:31 +00:00
|
|
|
.priv_data_size = sizeof(CamtasiaContext),
|
|
|
|
.init = decode_init,
|
|
|
|
.close = decode_end,
|
|
|
|
.decode = decode_frame,
|
2015-07-07 00:41:27 +00:00
|
|
|
.capabilities = AV_CODEC_CAP_DR1,
|
2004-08-14 15:08:09 +00:00
|
|
|
};
|