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;
149 av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
150 return AVERROR_INVALIDDATA;
154 avctx->release_buffer(avctx, &c->pic);
155 c->pic.reference = 1;
156 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
157 FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
158 if ((ret = ff_get_buffer(avctx, &c->pic)) < 0) {
159 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
164 switch ((buf[0] >> 1) & 7) {
165 case 0: { // lzo compression
166 int outlen = c->decomp_size, inlen = buf_size - 2;
167 if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
168 av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
171 case 1: { // zlib compression
173 unsigned long dlen = c->decomp_size;
174 if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
175 av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
178 av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
179 return AVERROR(ENOSYS);
183 av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
184 return AVERROR_INVALIDDATA;
187 // flip upside down, add difference frame
188 if (buf[0] & 1) { // keyframe
189 c->pic.pict_type = AV_PICTURE_TYPE_I;
190 c->pic.key_frame = 1;
193 copy_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
196 copy_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
199 copy_frame_default(&c->pic, c->decomp_buf, FFALIGN(c->linelen, 4),
200 c->linelen, c->height);
203 c->pic.pict_type = AV_PICTURE_TYPE_P;
204 c->pic.key_frame = 0;
207 add_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
210 add_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
213 add_frame_default(&c->pic, c->decomp_buf, FFALIGN(c->linelen, 4),
214 c->linelen, c->height);
223 static av_cold int decode_init(AVCodecContext *avctx) {
224 CamStudioContext *c = avctx->priv_data;
226 switch (avctx->bits_per_coded_sample) {
227 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
228 case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
229 case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break;
231 av_log(avctx, AV_LOG_ERROR,
232 "CamStudio codec error: invalid depth %i bpp\n",
233 avctx->bits_per_coded_sample);
234 return AVERROR_INVALIDDATA;
236 c->bpp = avctx->bits_per_coded_sample;
237 c->pic.data[0] = NULL;
238 c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
239 c->height = avctx->height;
241 if (avctx->bits_per_coded_sample == 24)
242 stride = FFALIGN(stride, 4);
243 c->decomp_size = c->height * stride;
244 c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
245 if (!c->decomp_buf) {
246 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
247 return AVERROR(ENOMEM);
252 static av_cold int decode_end(AVCodecContext *avctx) {
253 CamStudioContext *c = avctx->priv_data;
254 av_freep(&c->decomp_buf);
256 avctx->release_buffer(avctx, &c->pic);
260 AVCodec ff_cscd_decoder = {
262 .type = AVMEDIA_TYPE_VIDEO,
263 .id = AV_CODEC_ID_CSCD,
264 .priv_data_size = sizeof(CamStudioContext),
267 .decode = decode_frame,
268 .capabilities = CODEC_CAP_DR1,
269 .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),