9d53d58e |
/*
* TechSmith Camtasia decoder
* Copyright (c) 2004 Konstantin Shishkov
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
9d53d58e |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
9d53d58e |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
9d53d58e |
* 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 |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
9d53d58e |
*/
/** |
ba87f080 |
* @file |
9d53d58e |
* 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
* |
229daca7 |
* Supports: BGR8,BGR555,BGR24 - only BGR8 and BGR555 tested |
9d53d58e |
*/
#include <stdio.h>
#include <stdlib.h>
#include "avcodec.h" |
594d4d5d |
#include "internal.h" |
44aa9771 |
#include "msrledec.h" |
9d53d58e |
#include <zlib.h>
typedef struct TsccContext {
AVCodecContext *avctx; |
80e9e63c |
AVFrame *frame; |
9d53d58e |
// Bits per pixel
int bpp;
// Decompressed data size
unsigned int decomp_size;
// Decompression buffer
unsigned char* decomp_buf; |
992f71e9 |
GetByteContext gb; |
9d53d58e |
int height;
z_stream zstream; |
2d8591c2 |
uint32_t pal[256]; |
9d53d58e |
} CamtasiaContext;
|
df9b9567 |
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
AVPacket *avpkt) |
9d53d58e |
{ |
7a00bbad |
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size; |
e4141433 |
CamtasiaContext * const c = avctx->priv_data; |
80e9e63c |
AVFrame *frame = c->frame; |
4da8cdbb |
int ret; |
9d53d58e |
|
1ec94b0f |
if ((ret = ff_reget_buffer(avctx, frame)) < 0) |
1aeb87fa |
return ret; |
9d53d58e |
|
4da8cdbb |
ret = inflateReset(&c->zstream);
if (ret != Z_OK) {
av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret); |
ac1e93f5 |
return AVERROR_UNKNOWN; |
9d53d58e |
} |
4da8cdbb |
c->zstream.next_in = buf;
c->zstream.avail_in = buf_size; |
9d53d58e |
c->zstream.next_out = c->decomp_buf;
c->zstream.avail_out = c->decomp_size; |
4da8cdbb |
ret = inflate(&c->zstream, Z_FINISH); |
9d53d58e |
// Z_DATA_ERROR means empty picture |
4da8cdbb |
if ((ret != Z_OK) && (ret != Z_STREAM_END) && (ret != Z_DATA_ERROR)) {
av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret); |
ac1e93f5 |
return AVERROR_UNKNOWN; |
9d53d58e |
} |
cca1a426 |
|
4da8cdbb |
if (ret != Z_DATA_ERROR) { |
992f71e9 |
bytestream2_init(&c->gb, c->decomp_buf,
c->decomp_size - c->zstream.avail_out); |
3496cec4 |
ff_msrle_decode(avctx, frame, c->bpp, &c->gb); |
992f71e9 |
} |
115329f1 |
|
229daca7 |
/* make the palette available on the way out */ |
716d413c |
if (c->avctx->pix_fmt == AV_PIX_FMT_PAL8) { |
979bca51 |
int size;
const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, &size); |
2d8591c2 |
|
979bca51 |
if (pal && size == AVPALETTE_SIZE) { |
759001c5 |
frame->palette_has_changed = 1; |
2d8591c2 |
memcpy(c->pal, pal, AVPALETTE_SIZE); |
979bca51 |
} else if (pal) {
av_log(avctx, AV_LOG_ERROR, "Palette size %d is wrong\n", size); |
229daca7 |
} |
759001c5 |
memcpy(frame->data[1], c->pal, AVPALETTE_SIZE); |
229daca7 |
}
|
80e9e63c |
if ((ret = av_frame_ref(data, frame)) < 0)
return ret; |
df9b9567 |
*got_frame = 1; |
9d53d58e |
/* always report that the buffer was completely consumed */
return buf_size;
}
|
98a6fff9 |
static av_cold int decode_init(AVCodecContext *avctx) |
9d53d58e |
{ |
e4141433 |
CamtasiaContext * const c = avctx->priv_data; |
9d53d58e |
int zret; // Zlib return code
c->avctx = avctx;
c->height = avctx->height;
// Needed if zlib unused or init aborted before inflateInit |
3dc99a18 |
memset(&c->zstream, 0, sizeof(z_stream)); |
dd1c8f3e |
switch(avctx->bits_per_coded_sample){ |
716d413c |
case 8: avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break; |
b2eef2f0 |
case 24: |
716d413c |
avctx->pix_fmt = AV_PIX_FMT_BGR24; |
9d53d58e |
break; |
d45fadb6 |
case 32: avctx->pix_fmt = AV_PIX_FMT_0RGB32; break; |
dd1c8f3e |
default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample); |
1aeb87fa |
return AVERROR_PATCHWELCOME; |
9d53d58e |
} |
dd1c8f3e |
c->bpp = avctx->bits_per_coded_sample; |
da9cea77 |
// buffer size for RLE 'best' case when 2-byte code precedes each pixel and there may be padding after it too |
eebece46 |
c->decomp_size = (((avctx->width * c->bpp + 7) >> 3) + 3 * avctx->width + 2) * avctx->height + 2; |
9d53d58e |
/* Allocate decompression buffer */
if (c->decomp_size) { |
f929ab05 |
if (!(c->decomp_buf = av_malloc(c->decomp_size))) { |
9d53d58e |
av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n"); |
1aeb87fa |
return AVERROR(ENOMEM); |
9d53d58e |
}
} |
115329f1 |
|
9d53d58e |
c->zstream.zalloc = Z_NULL;
c->zstream.zfree = Z_NULL;
c->zstream.opaque = Z_NULL; |
3dc99a18 |
zret = inflateInit(&c->zstream); |
9d53d58e |
if (zret != Z_OK) {
av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret); |
ac1e93f5 |
return AVERROR_UNKNOWN; |
9d53d58e |
}
|
80e9e63c |
c->frame = av_frame_alloc();
|
9d53d58e |
return 0;
}
|
98a6fff9 |
static av_cold int decode_end(AVCodecContext *avctx) |
9d53d58e |
{ |
e4141433 |
CamtasiaContext * const c = avctx->priv_data; |
9d53d58e |
|
c2d57a34 |
av_freep(&c->decomp_buf); |
80e9e63c |
av_frame_free(&c->frame); |
c2d57a34 |
|
3dc99a18 |
inflateEnd(&c->zstream); |
9d53d58e |
return 0;
}
|
e7e2df27 |
AVCodec ff_tscc_decoder = { |
ec6402b7 |
.name = "camtasia", |
b2bed932 |
.long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"), |
ec6402b7 |
.type = AVMEDIA_TYPE_VIDEO, |
36ef5369 |
.id = AV_CODEC_ID_TSCC, |
ec6402b7 |
.priv_data_size = sizeof(CamtasiaContext),
.init = decode_init,
.close = decode_end,
.decode = decode_frame, |
def97856 |
.capabilities = AV_CODEC_CAP_DR1, |
9d53d58e |
}; |