3 * Copyright (c) 2006 Reimar Doeffinger
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "libavutil/lzo.h"
33 int linelen, height, bpp;
34 unsigned int decomp_size;
35 unsigned char* decomp_buf;
38 static void copy_frame_default(AVFrame *f, const uint8_t *src, int src_stride,
39 int linelen, int height) {
41 uint8_t *dst = f->data[0];
42 dst += (height - 1) * f->linesize[0];
43 for (i = height; i; i--) {
44 memcpy(dst, src, linelen);
46 dst -= f->linesize[0];
50 static void add_frame_default(AVFrame *f, const uint8_t *src, int src_stride,
51 int linelen, int height) {
53 uint8_t *dst = f->data[0];
54 dst += (height - 1) * f->linesize[0];
55 for (i = height; i; i--) {
56 for (j = linelen; j; j--)
58 src += src_stride - linelen;
59 dst -= f->linesize[0] + linelen;
64 #define copy_frame_16(f, s, l, h) copy_frame_default(f, s, l, l, h)
65 #define copy_frame_32(f, s, l, h) copy_frame_default(f, s, l, l, h)
66 #define add_frame_16(f, s, l, h) add_frame_default(f, s, l, l, h)
67 #define add_frame_32(f, s, l, h) add_frame_default(f, s, l, l, h)
69 static void copy_frame_16(AVFrame *f, const uint8_t *src,
70 int linelen, int height) {
72 uint8_t *dst = f->data[0];
73 dst += (height - 1) * f->linesize[0];
74 for (i = height; i; i--) {
75 for (j = linelen / 2; j; j--) {
81 dst -= f->linesize[0] + linelen;
85 static void copy_frame_32(AVFrame *f, const uint8_t *src,
86 int linelen, int height) {
88 uint8_t *dst = f->data[0];
89 dst += (height - 1) * f->linesize[0];
90 for (i = height; i; i--) {
91 for (j = linelen / 4; j; j--) {
99 dst -= f->linesize[0] + linelen;
103 static void add_frame_16(AVFrame *f, const uint8_t *src,
104 int linelen, int height) {
106 uint8_t *dst = f->data[0];
107 dst += (height - 1) * f->linesize[0];
108 for (i = height; i; i--) {
109 for (j = linelen / 2; j; j--) {
115 dst -= f->linesize[0] + linelen;
119 static void add_frame_32(AVFrame *f, const uint8_t *src,
120 int linelen, int height) {
122 uint8_t *dst = f->data[0];
123 dst += (height - 1) * f->linesize[0];
124 for (i = height; i; i--) {
125 for (j = linelen / 4; j; j--) {
133 dst -= f->linesize[0] + linelen;
138 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
140 const uint8_t *buf = avpkt->data;
141 int buf_size = avpkt->size;
142 CamStudioContext *c = avctx->priv_data;
143 AVFrame *picture = data;
146 av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
151 avctx->release_buffer(avctx, &c->pic);
152 c->pic.reference = 3;
153 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
154 FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
155 if (avctx->get_buffer(avctx, &c->pic) < 0) {
156 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
161 switch ((buf[0] >> 1) & 7) {
162 case 0: { // lzo compression
163 int outlen = c->decomp_size, inlen = buf_size - 2;
164 if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
165 av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
168 case 1: { // zlib compression
170 unsigned long dlen = c->decomp_size;
171 if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
172 av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
175 av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
180 av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
184 // flip upside down, add difference frame
185 if (buf[0] & 1) { // keyframe
186 c->pic.pict_type = AV_PICTURE_TYPE_I;
187 c->pic.key_frame = 1;
190 copy_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
193 copy_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
196 copy_frame_default(&c->pic, c->decomp_buf, FFALIGN(c->linelen, 4),
197 c->linelen, c->height);
200 c->pic.pict_type = AV_PICTURE_TYPE_P;
201 c->pic.key_frame = 0;
204 add_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
207 add_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
210 add_frame_default(&c->pic, c->decomp_buf, FFALIGN(c->linelen, 4),
211 c->linelen, c->height);
216 *data_size = sizeof(AVFrame);
220 static av_cold int decode_init(AVCodecContext *avctx) {
221 CamStudioContext *c = avctx->priv_data;
223 switch (avctx->bits_per_coded_sample) {
224 case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
225 case 24: avctx->pix_fmt = PIX_FMT_BGR24; break;
226 case 32: avctx->pix_fmt = PIX_FMT_RGB32; break;
228 av_log(avctx, AV_LOG_ERROR,
229 "CamStudio codec error: invalid depth %i bpp\n",
230 avctx->bits_per_coded_sample);
233 c->bpp = avctx->bits_per_coded_sample;
234 avcodec_get_frame_defaults(&c->pic);
235 c->pic.data[0] = NULL;
236 c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
237 c->height = avctx->height;
239 if (avctx->bits_per_coded_sample == 24)
240 stride = FFALIGN(stride, 4);
241 c->decomp_size = c->height * stride;
242 c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
243 if (!c->decomp_buf) {
244 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
250 static av_cold int decode_end(AVCodecContext *avctx) {
251 CamStudioContext *c = avctx->priv_data;
252 av_freep(&c->decomp_buf);
254 avctx->release_buffer(avctx, &c->pic);
258 AVCodec ff_cscd_decoder = {
260 .type = AVMEDIA_TYPE_VIDEO,
262 .priv_data_size = sizeof(CamStudioContext),
265 .decode = decode_frame,
266 .capabilities = CODEC_CAP_DR1,
267 .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),