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