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"
35 int linelen, height, bpp;
36 unsigned int decomp_size;
37 unsigned char* decomp_buf;
40 static void copy_frame_default(AVFrame *f, const uint8_t *src, int src_stride,
41 int linelen, int height) {
43 uint8_t *dst = f->data[0];
44 dst += (height - 1) * f->linesize[0];
45 for (i = height; i; i--) {
46 memcpy(dst, src, linelen);
48 dst -= f->linesize[0];
52 static void add_frame_default(AVFrame *f, const uint8_t *src, int src_stride,
53 int linelen, int height) {
55 uint8_t *dst = f->data[0];
56 dst += (height - 1) * f->linesize[0];
57 for (i = height; i; i--) {
58 for (j = linelen; j; j--)
60 src += src_stride - linelen;
61 dst -= f->linesize[0] + linelen;
66 #define copy_frame_16(f, s, l, h) copy_frame_default(f, s, l, l, h)
67 #define copy_frame_32(f, s, l, h) copy_frame_default(f, s, l, l, h)
68 #define add_frame_16(f, s, l, h) add_frame_default(f, s, l, l, h)
69 #define add_frame_32(f, s, l, h) add_frame_default(f, s, l, l, h)
71 static void copy_frame_16(AVFrame *f, const uint8_t *src,
72 int linelen, int height) {
74 uint8_t *dst = f->data[0];
75 dst += (height - 1) * f->linesize[0];
76 for (i = height; i; i--) {
77 for (j = linelen / 2; j; j--) {
83 dst -= f->linesize[0] + linelen;
87 static void copy_frame_32(AVFrame *f, const uint8_t *src,
88 int linelen, int height) {
90 uint8_t *dst = f->data[0];
91 dst += (height - 1) * f->linesize[0];
92 for (i = height; i; i--) {
93 for (j = linelen / 4; j; j--) {
101 dst -= f->linesize[0] + linelen;
105 static void add_frame_16(AVFrame *f, const uint8_t *src,
106 int linelen, int height) {
108 uint8_t *dst = f->data[0];
109 dst += (height - 1) * f->linesize[0];
110 for (i = height; i; i--) {
111 for (j = linelen / 2; j; j--) {
117 dst -= f->linesize[0] + linelen;
121 static void add_frame_32(AVFrame *f, const uint8_t *src,
122 int linelen, int height) {
124 uint8_t *dst = f->data[0];
125 dst += (height - 1) * f->linesize[0];
126 for (i = height; i; i--) {
127 for (j = linelen / 4; j; j--) {
135 dst -= f->linesize[0] + linelen;
140 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
142 const uint8_t *buf = avpkt->data;
143 int buf_size = avpkt->size;
144 CamStudioContext *c = avctx->priv_data;
145 AVFrame *picture = data;
148 av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
153 avctx->release_buffer(avctx, &c->pic);
154 c->pic.reference = 1;
155 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
156 FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
157 if (ff_get_buffer(avctx, &c->pic) < 0) {
158 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
163 switch ((buf[0] >> 1) & 7) {
164 case 0: { // lzo compression
165 int outlen = c->decomp_size, inlen = buf_size - 2;
166 if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
167 av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
170 case 1: { // zlib compression
172 unsigned long dlen = c->decomp_size;
173 if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
174 av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
177 av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
182 av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
186 // flip upside down, add difference frame
187 if (buf[0] & 1) { // keyframe
188 c->pic.pict_type = AV_PICTURE_TYPE_I;
189 c->pic.key_frame = 1;
192 copy_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
195 copy_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
198 copy_frame_default(&c->pic, c->decomp_buf, FFALIGN(c->linelen, 4),
199 c->linelen, c->height);
202 c->pic.pict_type = AV_PICTURE_TYPE_P;
203 c->pic.key_frame = 0;
206 add_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
209 add_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
212 add_frame_default(&c->pic, c->decomp_buf, FFALIGN(c->linelen, 4),
213 c->linelen, c->height);
222 static av_cold int decode_init(AVCodecContext *avctx) {
223 CamStudioContext *c = avctx->priv_data;
225 switch (avctx->bits_per_coded_sample) {
226 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
227 case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
228 case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break;
230 av_log(avctx, AV_LOG_ERROR,
231 "CamStudio codec error: invalid depth %i bpp\n",
232 avctx->bits_per_coded_sample);
233 return AVERROR_INVALIDDATA;
235 c->bpp = avctx->bits_per_coded_sample;
236 c->pic.data[0] = NULL;
237 c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
238 c->height = avctx->height;
240 if (avctx->bits_per_coded_sample == 24)
241 stride = FFALIGN(stride, 4);
242 c->decomp_size = c->height * stride;
243 c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
244 if (!c->decomp_buf) {
245 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
246 return AVERROR(ENOMEM);
251 static av_cold int decode_end(AVCodecContext *avctx) {
252 CamStudioContext *c = avctx->priv_data;
253 av_freep(&c->decomp_buf);
255 avctx->release_buffer(avctx, &c->pic);
259 AVCodec ff_cscd_decoder = {
261 .type = AVMEDIA_TYPE_VIDEO,
262 .id = AV_CODEC_ID_CSCD,
263 .priv_data_size = sizeof(CamStudioContext),
266 .decode = decode_frame,
267 .capabilities = CODEC_CAP_DR1,
268 .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),