cscd.c 5.49 KB
Newer Older
1 2 3 4
/*
 * CamStudio decoder
 * Copyright (c) 2006 Reimar Doeffinger
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg 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
 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 21 22 23 24
 */
#include <stdio.h>
#include <stdlib.h>

#include "avcodec.h"
25
#include "internal.h"
26
#include "libavutil/common.h"
27

28
#if CONFIG_ZLIB
29 30
#include <zlib.h>
#endif
31
#include "libavutil/lzo.h"
32

33
typedef struct CamStudioContext {
34
    AVFrame *pic;
35 36 37 38 39
    int linelen, height, bpp;
    unsigned int decomp_size;
    unsigned char* decomp_buf;
} CamStudioContext;

40
static void copy_frame_default(AVFrame *f, const uint8_t *src,
41 42
                               int linelen, int height)
{
43
    int i, src_stride = FFALIGN(linelen, 4);
44 45 46 47
    uint8_t *dst = f->data[0];
    dst += (height - 1) * f->linesize[0];
    for (i = height; i; i--) {
        memcpy(dst, src, linelen);
48
        src += src_stride;
49 50 51 52
        dst -= f->linesize[0];
    }
}

53
static void add_frame_default(AVFrame *f, const uint8_t *src,
54 55
                              int linelen, int height)
{
56
    int i, j, src_stride = FFALIGN(linelen, 4);
57 58 59 60 61
    uint8_t *dst = f->data[0];
    dst += (height - 1) * f->linesize[0];
    for (i = height; i; i--) {
        for (j = linelen; j; j--)
            *dst++ += *src++;
62
        src += src_stride - linelen;
63 64 65 66
        dst -= f->linesize[0] + linelen;
    }
}

67
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
68 69
                        AVPacket *avpkt)
{
70 71
    const uint8_t *buf = avpkt->data;
    int buf_size = avpkt->size;
72
    CamStudioContext *c = avctx->priv_data;
73
    int ret;
74 75 76

    if (buf_size < 2) {
        av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
77
        return AVERROR_INVALIDDATA;
78 79
    }

80
    if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
81
        return ret;
82 83 84

    // decompress data
    switch ((buf[0] >> 1) & 7) {
85 86 87 88 89
    case 0: { // lzo compression
        int outlen = c->decomp_size, inlen = buf_size - 2;
        if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen) || outlen) {
            av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
            return AVERROR_INVALIDDATA;
90
        }
91 92 93
        break;
    }
    case 1: { // zlib compression
94
#if CONFIG_ZLIB
95 96 97 98 99 100
        unsigned long dlen = c->decomp_size;
        if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK) {
            av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
            return AVERROR_INVALIDDATA;
        }
        break;
101
#else
102 103
        av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
        return AVERROR(ENOSYS);
104
#endif
105 106 107 108
    }
    default:
        av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
        return AVERROR_INVALIDDATA;
109 110 111 112
    }

    // flip upside down, add difference frame
    if (buf[0] & 1) { // keyframe
113 114 115
        c->pic->pict_type = AV_PICTURE_TYPE_I;
        c->pic->key_frame = 1;
              copy_frame_default(c->pic, c->decomp_buf,
116
                                 c->linelen, c->height);
117
    } else {
118 119 120
        c->pic->pict_type = AV_PICTURE_TYPE_P;
        c->pic->key_frame = 0;
              add_frame_default(c->pic, c->decomp_buf,
121
                                c->linelen, c->height);
122 123
    }

124
    *got_frame = 1;
125 126 127
    if ((ret = av_frame_ref(data, c->pic)) < 0)
        return ret;

128 129 130
    return buf_size;
}

131 132
static av_cold int decode_init(AVCodecContext *avctx)
{
133
    CamStudioContext *c = avctx->priv_data;
134
    int stride;
135
    switch (avctx->bits_per_coded_sample) {
136 137 138 139 140 141 142 143
    case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555LE; break;
    case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
    case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
    default:
        av_log(avctx, AV_LOG_ERROR,
               "CamStudio codec error: invalid depth %i bpp\n",
               avctx->bits_per_coded_sample);
        return AVERROR_INVALIDDATA;
144
    }
145 146
    c->bpp = avctx->bits_per_coded_sample;
    c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
147
    c->height = avctx->height;
148
    stride = FFALIGN(c->linelen, 4);
149
    c->decomp_size = c->height * stride;
150
    c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
151 152
    if (!c->decomp_buf) {
        av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
153
        return AVERROR(ENOMEM);
154
    }
155 156 157
    c->pic = av_frame_alloc();
    if (!c->pic)
        return AVERROR(ENOMEM);
158 159 160
    return 0;
}

161 162
static av_cold int decode_end(AVCodecContext *avctx)
{
163
    CamStudioContext *c = avctx->priv_data;
164
    av_freep(&c->decomp_buf);
165
    av_frame_free(&c->pic);
166 167 168
    return 0;
}

169
AVCodec ff_cscd_decoder = {
170
    .name           = "camstudio",
171
    .long_name      = NULL_IF_CONFIG_SMALL("CamStudio"),
172
    .type           = AVMEDIA_TYPE_VIDEO,
173
    .id             = AV_CODEC_ID_CSCD,
174 175 176 177
    .priv_data_size = sizeof(CamStudioContext),
    .init           = decode_init,
    .close          = decode_end,
    .decode         = decode_frame,
178
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
179
    .capabilities   = AV_CODEC_CAP_DR1,
180
};