3 * Copyright (c) 2006 Reimar Doeffinger
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * 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
26 #include "libavutil/common.h"
31 #include "libavutil/lzo.h"
33 typedef struct CamStudioContext {
34 int linelen, height, bpp;
35 unsigned int decomp_size;
36 unsigned char* decomp_buf;
39 static void copy_frame_default(AVFrame *f, const uint8_t *src, int src_stride,
40 int linelen, int height) {
42 uint8_t *dst = f->data[0];
43 dst += (height - 1) * f->linesize[0];
44 for (i = height; i; i--) {
45 memcpy(dst, src, linelen);
47 dst -= f->linesize[0];
51 static void add_frame_default(AVFrame *f, const uint8_t *src, int src_stride,
52 int linelen, int height) {
54 uint8_t *dst = f->data[0];
55 dst += (height - 1) * f->linesize[0];
56 for (i = height; i; i--) {
57 for (j = linelen; j; j--)
59 src += src_stride - linelen;
60 dst -= f->linesize[0] + linelen;
65 #define copy_frame_16(f, s, l, h) copy_frame_default(f, s, l, l, h)
66 #define copy_frame_32(f, s, l, h) copy_frame_default(f, s, l, l, h)
67 #define add_frame_16(f, s, l, h) add_frame_default(f, s, l, l, h)
68 #define add_frame_32(f, s, l, h) add_frame_default(f, s, l, l, h)
70 static void copy_frame_16(AVFrame *f, const uint8_t *src,
71 int linelen, int height) {
73 uint8_t *dst = f->data[0];
74 dst += (height - 1) * f->linesize[0];
75 for (i = height; i; i--) {
76 for (j = linelen / 2; j; j--) {
82 dst -= f->linesize[0] + linelen;
86 static void copy_frame_32(AVFrame *f, const uint8_t *src,
87 int linelen, int height) {
89 uint8_t *dst = f->data[0];
90 dst += (height - 1) * f->linesize[0];
91 for (i = height; i; i--) {
92 for (j = linelen / 4; j; j--) {
100 dst -= f->linesize[0] + linelen;
104 static void add_frame_16(AVFrame *f, const uint8_t *src,
105 int linelen, int height) {
107 uint8_t *dst = f->data[0];
108 dst += (height - 1) * f->linesize[0];
109 for (i = height; i; i--) {
110 for (j = linelen / 2; j; j--) {
116 dst -= f->linesize[0] + linelen;
120 static void add_frame_32(AVFrame *f, const uint8_t *src,
121 int linelen, int height) {
123 uint8_t *dst = f->data[0];
124 dst += (height - 1) * f->linesize[0];
125 for (i = height; i; i--) {
126 for (j = linelen / 4; j; j--) {
134 dst -= f->linesize[0] + linelen;
139 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
141 const uint8_t *buf = avpkt->data;
142 int buf_size = avpkt->size;
143 CamStudioContext *c = avctx->priv_data;
144 AVFrame *picture = data;
148 av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
149 return AVERROR_INVALIDDATA;
152 if ((ret = ff_get_buffer(avctx, picture, 0)) < 0) {
153 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
158 switch ((buf[0] >> 1) & 7) {
159 case 0: { // lzo compression
160 int outlen = c->decomp_size, inlen = buf_size - 2;
161 if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
162 av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
165 case 1: { // zlib compression
167 unsigned long dlen = c->decomp_size;
168 if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
169 av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
172 av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
173 return AVERROR(ENOSYS);
177 av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
178 return AVERROR_INVALIDDATA;
181 // flip upside down, add difference frame
182 if (buf[0] & 1) { // keyframe
183 picture->pict_type = AV_PICTURE_TYPE_I;
184 picture->key_frame = 1;
187 copy_frame_16(picture, c->decomp_buf, c->linelen, c->height);
190 copy_frame_32(picture, c->decomp_buf, c->linelen, c->height);
193 copy_frame_default(picture, c->decomp_buf, FFALIGN(c->linelen, 4),
194 c->linelen, c->height);
197 picture->pict_type = AV_PICTURE_TYPE_P;
198 picture->key_frame = 0;
201 add_frame_16(picture, c->decomp_buf, c->linelen, c->height);
204 add_frame_32(picture, c->decomp_buf, c->linelen, c->height);
207 add_frame_default(picture, c->decomp_buf, FFALIGN(c->linelen, 4),
208 c->linelen, c->height);
216 static av_cold int decode_init(AVCodecContext *avctx) {
217 CamStudioContext *c = avctx->priv_data;
219 switch (avctx->bits_per_coded_sample) {
220 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
221 case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
222 case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break;
224 av_log(avctx, AV_LOG_ERROR,
225 "CamStudio codec error: invalid depth %i bpp\n",
226 avctx->bits_per_coded_sample);
227 return AVERROR_INVALIDDATA;
229 c->bpp = avctx->bits_per_coded_sample;
230 c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
231 c->height = avctx->height;
233 if (avctx->bits_per_coded_sample == 24)
234 stride = FFALIGN(stride, 4);
235 c->decomp_size = c->height * stride;
236 c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
237 if (!c->decomp_buf) {
238 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
239 return AVERROR(ENOMEM);
244 static av_cold int decode_end(AVCodecContext *avctx) {
245 CamStudioContext *c = avctx->priv_data;
246 av_freep(&c->decomp_buf);
250 AVCodec ff_cscd_decoder = {
252 .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),
253 .type = AVMEDIA_TYPE_VIDEO,
254 .id = AV_CODEC_ID_CSCD,
255 .priv_data_size = sizeof(CamStudioContext),
258 .decode = decode_frame,
259 .capabilities = AV_CODEC_CAP_DR1,