3 * Copyright (c) 2006 Reimar Doeffinger
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 int linelen, height, bpp;
35 unsigned int decomp_size;
36 unsigned char* decomp_buf;
39 static void copy_frame_default(AVFrame *f, uint8_t *src,
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, uint8_t *src,
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 dst -= f->linesize[0] + linelen;
63 #ifndef WORDS_BIGENDIAN
64 #define copy_frame_16 copy_frame_default
65 #define copy_frame_32 copy_frame_default
66 #define add_frame_16 add_frame_default
67 #define add_frame_32 add_frame_default
69 static void copy_frame_16(AVFrame *f, 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, 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, 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, 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,
139 uint8_t *buf, int buf_size) {
140 CamStudioContext *c = (CamStudioContext *)avctx->priv_data;
141 AVFrame *picture = data;
144 av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
149 avctx->release_buffer(avctx, &c->pic);
150 c->pic.reference = 1;
151 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
152 FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
153 if (avctx->get_buffer(avctx, &c->pic) < 0) {
154 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
159 switch ((buf[0] >> 1) & 7) {
160 case 0: { // lzo compression
162 unsigned int dlen = c->decomp_size;
163 if (lzo1x_decompress_safe(&buf[2], buf_size - 2,
164 c->decomp_buf, &dlen, NULL) != LZO_E_OK)
165 av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
168 av_log(avctx, AV_LOG_ERROR, "compiled without lzo (GPL) support\n");
172 case 1: { // zlib compression
174 unsigned long dlen = c->decomp_size;
175 if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
176 av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
179 av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
184 av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
188 // flip upside down, add difference frame
189 if (buf[0] & 1) { // keyframe
190 c->pic.pict_type = FF_I_TYPE;
191 c->pic.key_frame = 1;
194 copy_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
197 copy_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
200 copy_frame_default(&c->pic, c->decomp_buf, c->linelen, c->height);
203 c->pic.pict_type = FF_P_TYPE;
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, c->linelen, c->height);
218 *data_size = sizeof(AVFrame);
222 static int decode_init(AVCodecContext *avctx) {
223 CamStudioContext *c = (CamStudioContext *)avctx->priv_data;
224 if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
227 avctx->has_b_frames = 0;
228 switch (avctx->bits_per_sample) {
229 case 16: avctx->pix_fmt = PIX_FMT_RGB565; break;
230 case 24: avctx->pix_fmt = PIX_FMT_BGR24; break;
231 case 32: avctx->pix_fmt = PIX_FMT_RGBA32; break;
233 av_log(avctx, AV_LOG_ERROR,
234 "CamStudio codec error: unvalid depth %i bpp\n",
235 avctx->bits_per_sample);
238 c->bpp = avctx->bits_per_sample;
239 c->pic.data[0] = NULL;
240 c->linelen = avctx->width * avctx->bits_per_sample / 8;
241 c->height = avctx->height;
242 c->decomp_size = c->height * c->linelen;
243 c->decomp_buf = av_malloc(c->decomp_size);
244 if (!c->decomp_buf) {
245 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
251 static int decode_end(AVCodecContext *avctx) {
252 CamStudioContext *c = (CamStudioContext *)avctx->priv_data;
253 av_freep(&c->decomp_buf);
255 avctx->release_buffer(avctx, &c->pic);
259 AVCodec cscd_decoder = {
263 sizeof(CamStudioContext),