cscd.c 5.61 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 34 35 36 37 38 39

typedef struct {
    AVFrame pic;
    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
                               int linelen, int height) {
42
    int i, src_stride = FFALIGN(linelen, 4);
43 44 45 46
    uint8_t *dst = f->data[0];
    dst += (height - 1) * f->linesize[0];
    for (i = height; i; i--) {
        memcpy(dst, src, linelen);
47
        src += src_stride;
48 49 50 51
        dst -= f->linesize[0];
    }
}

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

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

    if (buf_size < 2) {
        av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
        return -1;
    }

77
    c->pic.reference = 3;
78 79
    c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
                          FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
80
    if (avctx->reget_buffer(avctx, &c->pic) < 0) {
81 82 83 84 85 86 87
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
        return -1;
    }

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

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

    *picture = c->pic;
123
    *got_frame = 1;
124 125 126
    return buf_size;
}

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

155
static av_cold int decode_end(AVCodecContext *avctx) {
156
    CamStudioContext *c = avctx->priv_data;
157 158 159 160 161 162
    av_freep(&c->decomp_buf);
    if (c->pic.data[0])
        avctx->release_buffer(avctx, &c->pic);
    return 0;
}

163
AVCodec ff_cscd_decoder = {
164 165
    .name           = "camstudio",
    .type           = AVMEDIA_TYPE_VIDEO,
166
    .id             = AV_CODEC_ID_CSCD,
167 168 169 170 171
    .priv_data_size = sizeof(CamStudioContext),
    .init           = decode_init,
    .close          = decode_end,
    .decode         = decode_frame,
    .capabilities   = CODEC_CAP_DR1,
172
    .long_name      = NULL_IF_CONFIG_SMALL("CamStudio"),
173
};