]> git.sesse.net Git - ffmpeg/blob - libavcodec/cscd.c
Separate library install into static and shared installation.
[ffmpeg] / libavcodec / cscd.c
1 /*
2  * CamStudio decoder
3  * Copyright (c) 2006 Reimar Doeffinger
4  *
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.
9  *
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.
14  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include "common.h"
23 #include "avcodec.h"
24
25 #ifdef CONFIG_ZLIB
26 #include <zlib.h>
27 #endif
28 #include "lzo.h"
29
30 typedef struct {
31     AVFrame pic;
32     int linelen, height, bpp;
33     unsigned int decomp_size;
34     unsigned char* decomp_buf;
35 } CamStudioContext;
36
37 static void copy_frame_default(AVFrame *f, uint8_t *src,
38                                int linelen, int height) {
39     int i;
40     uint8_t *dst = f->data[0];
41     dst += (height - 1) * f->linesize[0];
42     for (i = height; i; i--) {
43         memcpy(dst, src, linelen);
44         src += linelen;
45         dst -= f->linesize[0];
46     }
47 }
48
49 static void add_frame_default(AVFrame *f, uint8_t *src,
50                               int linelen, int height) {
51     int i, j;
52     uint8_t *dst = f->data[0];
53     dst += (height - 1) * f->linesize[0];
54     for (i = height; i; i--) {
55         for (j = linelen; j; j--)
56             *dst++ += *src++;
57         dst -= f->linesize[0] + linelen;
58     }
59 }
60
61 #ifndef WORDS_BIGENDIAN
62 #define copy_frame_16 copy_frame_default
63 #define copy_frame_32 copy_frame_default
64 #define add_frame_16 add_frame_default
65 #define add_frame_32 add_frame_default
66 #else
67 static void copy_frame_16(AVFrame *f, uint8_t *src,
68                           int linelen, int height) {
69     int i, j;
70     uint8_t *dst = f->data[0];
71     dst += (height - 1) * f->linesize[0];
72     for (i = height; i; i--) {
73         for (j = linelen / 2; j; j--) {
74           dst[0] = src[1];
75           dst[1] = src[0];
76           src += 2;
77           dst += 2;
78         }
79         dst -= f->linesize[0] + linelen;
80     }
81 }
82
83 static void copy_frame_32(AVFrame *f, uint8_t *src,
84                           int linelen, int height) {
85     int i, j;
86     uint8_t *dst = f->data[0];
87     dst += (height - 1) * f->linesize[0];
88     for (i = height; i; i--) {
89         for (j = linelen / 4; j; j--) {
90           dst[0] = src[3];
91           dst[1] = src[2];
92           dst[2] = src[1];
93           dst[3] = src[0];
94           src += 4;
95           dst += 4;
96         }
97         dst -= f->linesize[0] + linelen;
98     }
99 }
100
101 static void add_frame_16(AVFrame *f, uint8_t *src,
102                          int linelen, int height) {
103     int i, j;
104     uint8_t *dst = f->data[0];
105     dst += (height - 1) * f->linesize[0];
106     for (i = height; i; i--) {
107         for (j = linelen / 2; j; j--) {
108           dst[0] += src[1];
109           dst[1] += src[0];
110           src += 2;
111           dst += 2;
112         }
113         dst -= f->linesize[0] + linelen;
114     }
115 }
116
117 static void add_frame_32(AVFrame *f, uint8_t *src,
118                          int linelen, int height) {
119     int i, j;
120     uint8_t *dst = f->data[0];
121     dst += (height - 1) * f->linesize[0];
122     for (i = height; i; i--) {
123         for (j = linelen / 4; j; j--) {
124           dst[0] += src[3];
125           dst[1] += src[2];
126           dst[2] += src[1];
127           dst[3] += src[0];
128           src += 4;
129           dst += 4;
130         }
131         dst -= f->linesize[0] + linelen;
132     }
133 }
134 #endif
135
136 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
137                         uint8_t *buf, int buf_size) {
138     CamStudioContext *c = (CamStudioContext *)avctx->priv_data;
139     AVFrame *picture = data;
140
141     if (buf_size < 2) {
142         av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
143         return -1;
144     }
145
146     if (c->pic.data[0])
147         avctx->release_buffer(avctx, &c->pic);
148     c->pic.reference = 1;
149     c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
150                           FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
151     if (avctx->get_buffer(avctx, &c->pic) < 0) {
152         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
153         return -1;
154     }
155
156     // decompress data
157     switch ((buf[0] >> 1) & 7) {
158         case 0: { // lzo compression
159             int outlen = c->decomp_size, inlen = buf_size - 2;
160             if (lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
161                 av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
162             break;
163         }
164         case 1: { // zlib compression
165 #ifdef CONFIG_ZLIB
166             unsigned long dlen = c->decomp_size;
167             if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
168                 av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
169             break;
170 #else
171             av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
172             return -1;
173 #endif
174         }
175         default:
176             av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
177             return -1;
178     }
179
180     // flip upside down, add difference frame
181     if (buf[0] & 1) { // keyframe
182         c->pic.pict_type = FF_I_TYPE;
183         c->pic.key_frame = 1;
184         switch (c->bpp) {
185           case 16:
186               copy_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
187               break;
188           case 32:
189               copy_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
190               break;
191           default:
192               copy_frame_default(&c->pic, c->decomp_buf, c->linelen, c->height);
193         }
194     } else {
195         c->pic.pict_type = FF_P_TYPE;
196         c->pic.key_frame = 0;
197         switch (c->bpp) {
198           case 16:
199               add_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
200               break;
201           case 32:
202               add_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
203               break;
204           default:
205               add_frame_default(&c->pic, c->decomp_buf, c->linelen, c->height);
206         }
207     }
208
209     *picture = c->pic;
210     *data_size = sizeof(AVFrame);
211     return buf_size;
212 }
213
214 static int decode_init(AVCodecContext *avctx) {
215     CamStudioContext *c = (CamStudioContext *)avctx->priv_data;
216     if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
217         return 1;
218     }
219     avctx->has_b_frames = 0;
220     switch (avctx->bits_per_sample) {
221         case 16: avctx->pix_fmt = PIX_FMT_RGB565; break;
222         case 24: avctx->pix_fmt = PIX_FMT_BGR24; break;
223         case 32: avctx->pix_fmt = PIX_FMT_RGBA32; break;
224         default:
225             av_log(avctx, AV_LOG_ERROR,
226                    "CamStudio codec error: unvalid depth %i bpp\n",
227                    avctx->bits_per_sample);
228              return 1;
229     }
230     c->bpp = avctx->bits_per_sample;
231     c->pic.data[0] = NULL;
232     c->linelen = avctx->width * avctx->bits_per_sample / 8;
233     c->height = avctx->height;
234     c->decomp_size = c->height * c->linelen;
235     c->decomp_buf = av_malloc(c->decomp_size + LZO_OUTPUT_PADDING);
236     if (!c->decomp_buf) {
237         av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
238         return 1;
239     }
240     return 0;
241 }
242
243 static int decode_end(AVCodecContext *avctx) {
244     CamStudioContext *c = (CamStudioContext *)avctx->priv_data;
245     av_freep(&c->decomp_buf);
246     if (c->pic.data[0])
247         avctx->release_buffer(avctx, &c->pic);
248     return 0;
249 }
250
251 AVCodec cscd_decoder = {
252     "camstudio",
253     CODEC_TYPE_VIDEO,
254     CODEC_ID_CSCD,
255     sizeof(CamStudioContext),
256     decode_init,
257     NULL,
258     decode_end,
259     decode_frame,
260     CODEC_CAP_DR1,
261 };
262