cscd.c
Go to the documentation of this file.
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 "internal.h"
26 #include "libavutil/common.h"
27 
28 #if CONFIG_ZLIB
29 #include <zlib.h>
30 #endif
31 #include "libavutil/lzo.h"
32 
33 typedef struct {
35  int linelen, height, bpp;
36  unsigned int decomp_size;
37  unsigned char* decomp_buf;
39 
40 static void copy_frame_default(AVFrame *f, const uint8_t *src, int src_stride,
41  int linelen, int height) {
42  int i;
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);
47  src += src_stride;
48  dst -= f->linesize[0];
49  }
50 }
51 
52 static void add_frame_default(AVFrame *f, const uint8_t *src, int src_stride,
53  int linelen, int height) {
54  int i, j;
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--)
59  *dst++ += *src++;
60  src += src_stride - linelen;
61  dst -= f->linesize[0] + linelen;
62  }
63 }
64 
65 #if !HAVE_BIGENDIAN
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)
70 #else
71 static void copy_frame_16(AVFrame *f, const uint8_t *src,
72  int linelen, int height) {
73  int i, j;
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--) {
78  dst[0] = src[1];
79  dst[1] = src[0];
80  src += 2;
81  dst += 2;
82  }
83  dst -= f->linesize[0] + linelen;
84  }
85 }
86 
87 static void copy_frame_32(AVFrame *f, const uint8_t *src,
88  int linelen, int height) {
89  int i, j;
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--) {
94  dst[0] = src[3];
95  dst[1] = src[2];
96  dst[2] = src[1];
97  dst[3] = src[0];
98  src += 4;
99  dst += 4;
100  }
101  dst -= f->linesize[0] + linelen;
102  }
103 }
104 
105 static void add_frame_16(AVFrame *f, const uint8_t *src,
106  int linelen, int height) {
107  int i, j;
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--) {
112  dst[0] += src[1];
113  dst[1] += src[0];
114  src += 2;
115  dst += 2;
116  }
117  dst -= f->linesize[0] + linelen;
118  }
119 }
120 
121 static void add_frame_32(AVFrame *f, const uint8_t *src,
122  int linelen, int height) {
123  int i, j;
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--) {
128  dst[0] += src[3];
129  dst[1] += src[2];
130  dst[2] += src[1];
131  dst[3] += src[0];
132  src += 4;
133  dst += 4;
134  }
135  dst -= f->linesize[0] + linelen;
136  }
137 }
138 #endif
139 
140 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
141  AVPacket *avpkt) {
142  const uint8_t *buf = avpkt->data;
143  int buf_size = avpkt->size;
144  CamStudioContext *c = avctx->priv_data;
145  AVFrame *picture = data;
146 
147  if (buf_size < 2) {
148  av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
149  return -1;
150  }
151 
152  if (c->pic.data[0])
153  avctx->release_buffer(avctx, &c->pic);
154  c->pic.reference = 1;
157  if (ff_get_buffer(avctx, &c->pic) < 0) {
158  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
159  return -1;
160  }
161 
162  // decompress data
163  switch ((buf[0] >> 1) & 7) {
164  case 0: { // lzo compression
165  int outlen = c->decomp_size, inlen = buf_size - 2;
166  if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
167  av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
168  break;
169  }
170  case 1: { // zlib compression
171 #if CONFIG_ZLIB
172  unsigned long dlen = c->decomp_size;
173  if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
174  av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
175  break;
176 #else
177  av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
178  return -1;
179 #endif
180  }
181  default:
182  av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
183  return -1;
184  }
185 
186  // flip upside down, add difference frame
187  if (buf[0] & 1) { // keyframe
189  c->pic.key_frame = 1;
190  switch (c->bpp) {
191  case 16:
192  copy_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
193  break;
194  case 32:
195  copy_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
196  break;
197  default:
199  c->linelen, c->height);
200  }
201  } else {
203  c->pic.key_frame = 0;
204  switch (c->bpp) {
205  case 16:
206  add_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
207  break;
208  case 32:
209  add_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
210  break;
211  default:
213  c->linelen, c->height);
214  }
215  }
216 
217  *picture = c->pic;
218  *got_frame = 1;
219  return buf_size;
220 }
221 
222 static av_cold int decode_init(AVCodecContext *avctx) {
223  CamStudioContext *c = avctx->priv_data;
224  int stride;
225  switch (avctx->bits_per_coded_sample) {
226  case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
227  case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
228  case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break;
229  default:
230  av_log(avctx, AV_LOG_ERROR,
231  "CamStudio codec error: invalid depth %i bpp\n",
232  avctx->bits_per_coded_sample);
233  return AVERROR_INVALIDDATA;
234  }
235  c->bpp = avctx->bits_per_coded_sample;
236  c->pic.data[0] = NULL;
237  c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
238  c->height = avctx->height;
239  stride = c->linelen;
240  if (avctx->bits_per_coded_sample == 24)
241  stride = FFALIGN(stride, 4);
242  c->decomp_size = c->height * stride;
244  if (!c->decomp_buf) {
245  av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
246  return AVERROR(ENOMEM);
247  }
248  return 0;
249 }
250 
251 static av_cold int decode_end(AVCodecContext *avctx) {
252  CamStudioContext *c = avctx->priv_data;
253  av_freep(&c->decomp_buf);
254  if (c->pic.data[0])
255  avctx->release_buffer(avctx, &c->pic);
256  return 0;
257 }
258 
260  .name = "camstudio",
261  .type = AVMEDIA_TYPE_VIDEO,
262  .id = AV_CODEC_ID_CSCD,
263  .priv_data_size = sizeof(CamStudioContext),
264  .init = decode_init,
265  .close = decode_end,
266  .decode = decode_frame,
267  .capabilities = CODEC_CAP_DR1,
268  .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),
269 };