tscc.c 5.37 KB
Newer Older
1 2 3 4
/*
 * TechSmith Camtasia decoder
 * Copyright (c) 2004 Konstantin Shishkov
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * Libav is distributed in the hope that it will be useful,
13 14 15 16 17
 * 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
18
 * License along with Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 21 22
 */

/**
23
 * @file
24 25 26 27 28 29 30 31 32 33
 * 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
 *
Mike Melanson's avatar
Mike Melanson committed
34
 * Supports: BGR8,BGR555,BGR24 - only BGR8 and BGR555 tested
35 36 37 38 39 40
 */

#include <stdio.h>
#include <stdlib.h>

#include "avcodec.h"
41
#include "internal.h"
42
#include "msrledec.h"
43 44 45 46 47 48 49 50 51 52 53 54 55

#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;
56
    GetByteContext gb;
57 58
    int height;
    z_stream zstream;
59 60

    uint32_t pal[256];
61 62
} CamtasiaContext;

63 64
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
                        AVPacket *avpkt)
65
{
66 67
    const uint8_t *buf = avpkt->data;
    int buf_size = avpkt->size;
68
    CamtasiaContext * const c = avctx->priv_data;
69
    AVFrame *frame = data;
70
    int ret;
71

72
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0){
73
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
74
        return ret;
75 76
    }

77 78 79
    ret = inflateReset(&c->zstream);
    if (ret != Z_OK) {
        av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
80
        return AVERROR_UNKNOWN;
81
    }
82 83
    c->zstream.next_in   = buf;
    c->zstream.avail_in  = buf_size;
84 85
    c->zstream.next_out = c->decomp_buf;
    c->zstream.avail_out = c->decomp_size;
86
    ret = inflate(&c->zstream, Z_FINISH);
87
    // Z_DATA_ERROR means empty picture
88 89
    if ((ret != Z_OK) && (ret != Z_STREAM_END) && (ret != Z_DATA_ERROR)) {
        av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
90
        return AVERROR_UNKNOWN;
91
    }
92 93


94
    if (ret != Z_DATA_ERROR) {
95 96
        bytestream2_init(&c->gb, c->decomp_buf,
                         c->decomp_size - c->zstream.avail_out);
97
        ff_msrle_decode(avctx, frame, c->bpp, &c->gb);
98
    }
99

Mike Melanson's avatar
Mike Melanson committed
100
    /* make the palette available on the way out */
101
    if (c->avctx->pix_fmt == AV_PIX_FMT_PAL8) {
102 103 104
        const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);

        if (pal) {
105
            frame->palette_has_changed = 1;
106
            memcpy(c->pal, pal, AVPALETTE_SIZE);
Mike Melanson's avatar
Mike Melanson committed
107
        }
108
        memcpy(frame->data[1], c->pal, AVPALETTE_SIZE);
Mike Melanson's avatar
Mike Melanson committed
109 110
    }

111
    *got_frame      = 1;
112 113 114 115 116

    /* always report that the buffer was completely consumed */
    return buf_size;
}

117
static av_cold int decode_init(AVCodecContext *avctx)
118
{
119
    CamtasiaContext * const c = avctx->priv_data;
120 121 122 123 124 125 126
    int zret; // Zlib return code

    c->avctx = avctx;

    c->height = avctx->height;

    // Needed if zlib unused or init aborted before inflateInit
127
    memset(&c->zstream, 0, sizeof(z_stream));
128
    switch(avctx->bits_per_coded_sample){
129 130
    case  8: avctx->pix_fmt = AV_PIX_FMT_PAL8; break;
    case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
Roberto Togni's avatar
Roberto Togni committed
131
    case 24:
132
             avctx->pix_fmt = AV_PIX_FMT_BGR24;
133
             break;
134
    case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break;
135
    default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
136
             return AVERROR_INVALIDDATA;
137
    }
138
    c->bpp = avctx->bits_per_coded_sample;
139
    // buffer size for RLE 'best' case when 2-byte code precedes each pixel and there may be padding after it too
140
    c->decomp_size = (((avctx->width * c->bpp + 7) >> 3) + 3 * avctx->width + 2) * avctx->height + 2;
141 142 143

    /* Allocate decompression buffer */
    if (c->decomp_size) {
144
        if (!(c->decomp_buf = av_malloc(c->decomp_size))) {
145
            av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
146
            return AVERROR(ENOMEM);
147 148
        }
    }
149

150 151 152
    c->zstream.zalloc = Z_NULL;
    c->zstream.zfree = Z_NULL;
    c->zstream.opaque = Z_NULL;
153
    zret = inflateInit(&c->zstream);
154 155
    if (zret != Z_OK) {
        av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
156
        return AVERROR_UNKNOWN;
157 158 159 160 161
    }

    return 0;
}

162
static av_cold int decode_end(AVCodecContext *avctx)
163
{
164
    CamtasiaContext * const c = avctx->priv_data;
165

Michael Niedermayer's avatar
Michael Niedermayer committed
166 167
    av_freep(&c->decomp_buf);

168
    inflateEnd(&c->zstream);
169 170 171 172

    return 0;
}

173
AVCodec ff_tscc_decoder = {
174
    .name           = "camtasia",
175
    .long_name      = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
176
    .type           = AVMEDIA_TYPE_VIDEO,
177
    .id             = AV_CODEC_ID_TSCC,
178 179 180 181
    .priv_data_size = sizeof(CamtasiaContext),
    .init           = decode_init,
    .close          = decode_end,
    .decode         = decode_frame,
182
    .capabilities   = AV_CODEC_CAP_DR1,
183
};