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 "avcodec.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2021-03-17 23:43:32 +02:00
|
|
|
#include "decode.h"
|
2008-09-18 08:20:54 +03:00
|
|
|
#include "msrledec.h"
|
2022-03-15 12:48:14 +02:00
|
|
|
#include "zlib_wrapper.h"
|
2004-08-14 18:08:09 +03:00
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
typedef struct TsccContext {
|
|
|
|
|
|
|
|
AVCodecContext *avctx;
|
2013-03-12 05:20:18 +03:00
|
|
|
AVFrame *frame;
|
2004-08-14 18:08:09 +03:00
|
|
|
|
|
|
|
// 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;
|
2022-03-15 12:48:14 +02:00
|
|
|
FFZStream zstream;
|
2011-04-09 16:49:51 +03:00
|
|
|
|
|
|
|
uint32_t pal[256];
|
2004-08-14 18:08:09 +03:00
|
|
|
} CamtasiaContext;
|
|
|
|
|
2022-03-30 21:33:24 +02:00
|
|
|
static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
|
|
|
|
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;
|
2022-03-15 12:48:14 +02:00
|
|
|
z_stream *const zstream = &c->zstream.zstream;
|
2013-03-12 05:20:18 +03:00
|
|
|
AVFrame *frame = c->frame;
|
2014-07-29 19:57:48 +03:00
|
|
|
int ret;
|
2018-07-15 14:01:40 +02:00
|
|
|
int palette_has_changed = 0;
|
|
|
|
|
|
|
|
if (c->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
|
2021-03-17 23:43:32 +02:00
|
|
|
palette_has_changed = ff_copy_palette(c->pal, avpkt, avctx);
|
2018-07-15 14:01:40 +02:00
|
|
|
}
|
2004-08-14 18:08:09 +03:00
|
|
|
|
2022-03-15 12:48:14 +02:00
|
|
|
ret = inflateReset(zstream);
|
2014-07-29 19:57:48 +03:00
|
|
|
if (ret != Z_OK) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
|
2012-11-20 09:13:49 +03:00
|
|
|
return AVERROR_UNKNOWN;
|
2004-08-14 18:08:09 +03:00
|
|
|
}
|
2022-03-15 12:48:14 +02:00
|
|
|
zstream->next_in = buf;
|
|
|
|
zstream->avail_in = buf_size;
|
|
|
|
zstream->next_out = c->decomp_buf;
|
|
|
|
zstream->avail_out = c->decomp_size;
|
|
|
|
ret = inflate(zstream, Z_FINISH);
|
2004-08-14 18:08:09 +03:00
|
|
|
// Z_DATA_ERROR means empty picture
|
2018-07-15 14:04:05 +02:00
|
|
|
if (ret == Z_DATA_ERROR && !palette_has_changed) {
|
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
|
2014-07-29 19:57:48 +03: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 09:13:49 +03:00
|
|
|
return AVERROR_UNKNOWN;
|
2004-08-14 18:08:09 +03:00
|
|
|
}
|
2005-01-23 23:36:24 +02:00
|
|
|
|
2019-08-30 16:37:25 +02:00
|
|
|
if ((ret = ff_reget_buffer(avctx, frame, 0)) < 0)
|
2018-07-15 13:52:50 +02:00
|
|
|
return ret;
|
2005-01-23 23:36:24 +02:00
|
|
|
|
2014-07-29 19:57:48 +03:00
|
|
|
if (ret != Z_DATA_ERROR) {
|
2012-03-31 20:10:54 +03:00
|
|
|
bytestream2_init(&c->gb, c->decomp_buf,
|
2022-03-15 12:48:14 +02:00
|
|
|
c->decomp_size - zstream->avail_out);
|
2015-10-14 11:33:18 +02:00
|
|
|
ff_msrle_decode(avctx, frame, c->bpp, &c->gb);
|
2012-03-31 20:10:54 +03:00
|
|
|
}
|
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) {
|
2023-05-18 03:39:57 +02:00
|
|
|
#if FF_API_PALETTE_HAS_CHANGED
|
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
2018-07-15 14:01:40 +02:00
|
|
|
frame->palette_has_changed = palette_has_changed;
|
2023-05-18 03:39:57 +02:00
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
|
|
|
#endif
|
2012-11-21 23:34:46 +03:00
|
|
|
memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
|
2004-08-28 15:28:53 +03:00
|
|
|
}
|
|
|
|
|
2022-03-30 21:33:24 +02:00
|
|
|
if ((ret = av_frame_ref(rframe, frame)) < 0)
|
2013-03-12 05:20:18 +03:00
|
|
|
return ret;
|
2012-11-13 21:35:22 +03:00
|
|
|
*got_frame = 1;
|
2004-08-14 18:08:09 +03:00
|
|
|
|
|
|
|
/* always report that the buffer was completely consumed */
|
|
|
|
return buf_size;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
c->avctx = avctx;
|
|
|
|
|
|
|
|
c->height = avctx->height;
|
|
|
|
|
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;
|
2015-02-06 22:02:08 +02:00
|
|
|
case 32: avctx->pix_fmt = AV_PIX_FMT_0RGB32; 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) {
|
2014-08-14 23:31:24 +03:00
|
|
|
if (!(c->decomp_buf = av_malloc(c->decomp_size))) {
|
2004-08-14 18:08:09 +03:00
|
|
|
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
|
|
|
|
2013-03-12 05:20:18 +03:00
|
|
|
c->frame = av_frame_alloc();
|
2018-09-08 21:17:26 +02:00
|
|
|
if (!c->frame)
|
|
|
|
return AVERROR(ENOMEM);
|
2013-03-12 05:20:18 +03:00
|
|
|
|
2022-03-15 12:48:14 +02:00
|
|
|
return ff_inflate_init(&c->zstream, avctx);
|
2004-08-14 18:08:09 +03:00
|
|
|
}
|
|
|
|
|
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);
|
2013-03-12 05:20:18 +03:00
|
|
|
av_frame_free(&c->frame);
|
2022-03-15 12:48:14 +02:00
|
|
|
ff_inflate_end(&c->zstream);
|
2004-08-14 18:08:09 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_tscc_decoder = {
|
|
|
|
.p.name = "camtasia",
|
2022-08-29 13:38:02 +02:00
|
|
|
CODEC_LONG_NAME("TechSmith Screen Capture Codec"),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_TSCC,
|
2011-07-17 13:54:31 +03:00
|
|
|
.priv_data_size = sizeof(CamtasiaContext),
|
|
|
|
.init = decode_init,
|
|
|
|
.close = decode_end,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_DECODE_CB(decode_frame),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1,
|
2022-07-10 00:05:45 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
|
2004-08-14 18:08:09 +03:00
|
|
|
};
|