]> git.sesse.net Git - ffmpeg/blob - libavcodec/cscd.c
Don't include common.h from avutil.h
[ffmpeg] / libavcodec / cscd.c
1 /*
2  * CamStudio decoder
3  * Copyright (c) 2006 Reimar Doeffinger
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "avcodec.h"
25 #include "libavutil/common.h"
26
27 #if CONFIG_ZLIB
28 #include <zlib.h>
29 #endif
30 #include "libavutil/lzo.h"
31
32 typedef struct {
33     AVFrame pic;
34     int linelen, height, bpp;
35     unsigned int decomp_size;
36     unsigned char* decomp_buf;
37 } CamStudioContext;
38
39 static void copy_frame_default(AVFrame *f, const uint8_t *src, int src_stride,
40                                int linelen, int height) {
41     int i;
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);
46         src += src_stride;
47         dst -= f->linesize[0];
48     }
49 }
50
51 static void add_frame_default(AVFrame *f, const uint8_t *src, int src_stride,
52                               int linelen, int height) {
53     int i, j;
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--)
58             *dst++ += *src++;
59         src += src_stride - linelen;
60         dst -= f->linesize[0] + linelen;
61     }
62 }
63
64 #if !HAVE_BIGENDIAN
65 #define copy_frame_16(f, s, l, h) copy_frame_default(f, s, l, l, h)
66 #define copy_frame_32(f, s, l, h) copy_frame_default(f, s, l, l, h)
67 #define add_frame_16(f, s, l, h) add_frame_default(f, s, l, l, h)
68 #define add_frame_32(f, s, l, h) add_frame_default(f, s, l, l, h)
69 #else
70 static void copy_frame_16(AVFrame *f, const uint8_t *src,
71                           int linelen, int height) {
72     int i, j;
73     uint8_t *dst = f->data[0];
74     dst += (height - 1) * f->linesize[0];
75     for (i = height; i; i--) {
76         for (j = linelen / 2; j; j--) {
77           dst[0] = src[1];
78           dst[1] = src[0];
79           src += 2;
80           dst += 2;
81         }
82         dst -= f->linesize[0] + linelen;
83     }
84 }
85
86 static void copy_frame_32(AVFrame *f, const uint8_t *src,
87                           int linelen, int height) {
88     int i, j;
89     uint8_t *dst = f->data[0];
90     dst += (height - 1) * f->linesize[0];
91     for (i = height; i; i--) {
92         for (j = linelen / 4; j; j--) {
93           dst[0] = src[3];
94           dst[1] = src[2];
95           dst[2] = src[1];
96           dst[3] = src[0];
97           src += 4;
98           dst += 4;
99         }
100         dst -= f->linesize[0] + linelen;
101     }
102 }
103
104 static void add_frame_16(AVFrame *f, const uint8_t *src,
105                          int linelen, int height) {
106     int i, j;
107     uint8_t *dst = f->data[0];
108     dst += (height - 1) * f->linesize[0];
109     for (i = height; i; i--) {
110         for (j = linelen / 2; j; j--) {
111           dst[0] += src[1];
112           dst[1] += src[0];
113           src += 2;
114           dst += 2;
115         }
116         dst -= f->linesize[0] + linelen;
117     }
118 }
119
120 static void add_frame_32(AVFrame *f, const uint8_t *src,
121                          int linelen, int height) {
122     int i, j;
123     uint8_t *dst = f->data[0];
124     dst += (height - 1) * f->linesize[0];
125     for (i = height; i; i--) {
126         for (j = linelen / 4; j; j--) {
127           dst[0] += src[3];
128           dst[1] += src[2];
129           dst[2] += src[1];
130           dst[3] += src[0];
131           src += 4;
132           dst += 4;
133         }
134         dst -= f->linesize[0] + linelen;
135     }
136 }
137 #endif
138
139 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
140                         AVPacket *avpkt) {
141     const uint8_t *buf = avpkt->data;
142     int buf_size = avpkt->size;
143     CamStudioContext *c = avctx->priv_data;
144     AVFrame *picture = data;
145
146     if (buf_size < 2) {
147         av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
148         return -1;
149     }
150
151     if (c->pic.data[0])
152         avctx->release_buffer(avctx, &c->pic);
153     c->pic.reference = 1;
154     c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
155                           FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
156     if (avctx->get_buffer(avctx, &c->pic) < 0) {
157         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
158         return -1;
159     }
160
161     // decompress data
162     switch ((buf[0] >> 1) & 7) {
163         case 0: { // lzo compression
164             int outlen = c->decomp_size, inlen = buf_size - 2;
165             if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
166                 av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
167             break;
168         }
169         case 1: { // zlib compression
170 #if CONFIG_ZLIB
171             unsigned long dlen = c->decomp_size;
172             if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
173                 av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
174             break;
175 #else
176             av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
177             return -1;
178 #endif
179         }
180         default:
181             av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
182             return -1;
183     }
184
185     // flip upside down, add difference frame
186     if (buf[0] & 1) { // keyframe
187         c->pic.pict_type = AV_PICTURE_TYPE_I;
188         c->pic.key_frame = 1;
189         switch (c->bpp) {
190           case 16:
191               copy_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
192               break;
193           case 32:
194               copy_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
195               break;
196           default:
197               copy_frame_default(&c->pic, c->decomp_buf, FFALIGN(c->linelen, 4),
198                                  c->linelen, c->height);
199         }
200     } else {
201         c->pic.pict_type = AV_PICTURE_TYPE_P;
202         c->pic.key_frame = 0;
203         switch (c->bpp) {
204           case 16:
205               add_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
206               break;
207           case 32:
208               add_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
209               break;
210           default:
211               add_frame_default(&c->pic, c->decomp_buf, FFALIGN(c->linelen, 4),
212                                 c->linelen, c->height);
213         }
214     }
215
216     *picture = c->pic;
217     *data_size = sizeof(AVFrame);
218     return buf_size;
219 }
220
221 static av_cold int decode_init(AVCodecContext *avctx) {
222     CamStudioContext *c = avctx->priv_data;
223     int stride;
224     switch (avctx->bits_per_coded_sample) {
225         case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
226         case 24: avctx->pix_fmt = PIX_FMT_BGR24; break;
227         case 32: avctx->pix_fmt = PIX_FMT_RGB32; break;
228         default:
229             av_log(avctx, AV_LOG_ERROR,
230                    "CamStudio codec error: invalid depth %i bpp\n",
231                    avctx->bits_per_coded_sample);
232             return AVERROR_INVALIDDATA;
233     }
234     c->bpp = avctx->bits_per_coded_sample;
235     c->pic.data[0] = NULL;
236     c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
237     c->height = avctx->height;
238     stride = c->linelen;
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");
245         return AVERROR(ENOMEM);
246     }
247     return 0;
248 }
249
250 static av_cold int decode_end(AVCodecContext *avctx) {
251     CamStudioContext *c = avctx->priv_data;
252     av_freep(&c->decomp_buf);
253     if (c->pic.data[0])
254         avctx->release_buffer(avctx, &c->pic);
255     return 0;
256 }
257
258 AVCodec ff_cscd_decoder = {
259     .name           = "camstudio",
260     .type           = AVMEDIA_TYPE_VIDEO,
261     .id             = AV_CODEC_ID_CSCD,
262     .priv_data_size = sizeof(CamStudioContext),
263     .init           = decode_init,
264     .close          = decode_end,
265     .decode         = decode_frame,
266     .capabilities   = CODEC_CAP_DR1,
267     .long_name      = NULL_IF_CONFIG_SMALL("CamStudio"),
268 };