Libav
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 {
34  int linelen, height, bpp;
35  unsigned int decomp_size;
36  unsigned char* decomp_buf;
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 *got_frame,
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  int ret;
146 
147  if (buf_size < 2) {
148  av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
149  return AVERROR_INVALIDDATA;
150  }
151 
152  if ((ret = ff_get_buffer(avctx, picture, 0)) < 0) {
153  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
154  return ret;
155  }
156 
157  // decompress data
158  switch ((buf[0] >> 1) & 7) {
159  case 0: { // lzo compression
160  int outlen = c->decomp_size, inlen = buf_size - 2;
161  if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
162  av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
163  break;
164  }
165  case 1: { // zlib compression
166 #if CONFIG_ZLIB
167  unsigned long dlen = c->decomp_size;
168  if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
169  av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
170  break;
171 #else
172  av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
173  return AVERROR(ENOSYS);
174 #endif
175  }
176  default:
177  av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
178  return AVERROR_INVALIDDATA;
179  }
180 
181  // flip upside down, add difference frame
182  if (buf[0] & 1) { // keyframe
183  picture->pict_type = AV_PICTURE_TYPE_I;
184  picture->key_frame = 1;
185  switch (c->bpp) {
186  case 16:
187  copy_frame_16(picture, c->decomp_buf, c->linelen, c->height);
188  break;
189  case 32:
190  copy_frame_32(picture, c->decomp_buf, c->linelen, c->height);
191  break;
192  default:
193  copy_frame_default(picture, c->decomp_buf, FFALIGN(c->linelen, 4),
194  c->linelen, c->height);
195  }
196  } else {
197  picture->pict_type = AV_PICTURE_TYPE_P;
198  picture->key_frame = 0;
199  switch (c->bpp) {
200  case 16:
201  add_frame_16(picture, c->decomp_buf, c->linelen, c->height);
202  break;
203  case 32:
204  add_frame_32(picture, c->decomp_buf, c->linelen, c->height);
205  break;
206  default:
207  add_frame_default(picture, c->decomp_buf, FFALIGN(c->linelen, 4),
208  c->linelen, c->height);
209  }
210  }
211 
212  *got_frame = 1;
213  return buf_size;
214 }
215 
216 static av_cold int decode_init(AVCodecContext *avctx) {
217  CamStudioContext *c = avctx->priv_data;
218  int stride;
219  switch (avctx->bits_per_coded_sample) {
220  case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555; break;
221  case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
222  case 32: avctx->pix_fmt = AV_PIX_FMT_RGB32; break;
223  default:
224  av_log(avctx, AV_LOG_ERROR,
225  "CamStudio codec error: invalid depth %i bpp\n",
226  avctx->bits_per_coded_sample);
227  return AVERROR_INVALIDDATA;
228  }
229  c->bpp = avctx->bits_per_coded_sample;
230  c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
231  c->height = avctx->height;
232  stride = c->linelen;
233  if (avctx->bits_per_coded_sample == 24)
234  stride = FFALIGN(stride, 4);
235  c->decomp_size = c->height * stride;
237  if (!c->decomp_buf) {
238  av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
239  return AVERROR(ENOMEM);
240  }
241  return 0;
242 }
243 
244 static av_cold int decode_end(AVCodecContext *avctx) {
245  CamStudioContext *c = avctx->priv_data;
246  av_freep(&c->decomp_buf);
247  return 0;
248 }
249 
251  .name = "camstudio",
252  .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),
253  .type = AVMEDIA_TYPE_VIDEO,
254  .id = AV_CODEC_ID_CSCD,
255  .priv_data_size = sizeof(CamStudioContext),
256  .init = decode_init,
257  .close = decode_end,
258  .decode = decode_frame,
259  .capabilities = CODEC_CAP_DR1,
260 };
#define copy_frame_32(f, s, l, h)
Definition: cscd.c:66
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
int size
Definition: avcodec.h:974
int height
Definition: cscd.c:34
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1254
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2796
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
static void add_frame_default(AVFrame *f, const uint8_t *src, int src_stride, int linelen, int height)
Definition: cscd.c:51
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
uint8_t
static av_cold int decode_end(AVCodecContext *avctx)
Definition: cscd.c:244
#define AV_LZO_OUTPUT_PADDING
Definition: lzo.h:47
const char * name
Name of the codec implementation.
Definition: avcodec.h:2803
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
#define add_frame_16(f, s, l, h)
Definition: cscd.c:67
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2507
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: cscd.c:139
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
unsigned char * decomp_buf
Definition: cscd.c:36
#define copy_frame_16(f, s, l, h)
Definition: cscd.c:65
Libavcodec external API header.
#define add_frame_32(f, s, l, h)
Definition: cscd.c:68
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
static void copy_frame_default(AVFrame *f, const uint8_t *src, int src_stride, int linelen, int height)
Definition: cscd.c:39
int width
picture width / height.
Definition: avcodec.h:1224
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#define FFALIGN(x, a)
Definition: common.h:62
int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
Decodes LZO 1x compressed data.
Definition: lzo.c:134
#define av_cold
Definition: attributes.h:66
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:231
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
uint8_t * data
Definition: avcodec.h:973
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
void * priv_data
Definition: avcodec.h:1092
int height
Definition: gxfenc.c:72
common internal api header.
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
#define AV_PIX_FMT_RGB32
Definition: pixfmt.h:222
static av_cold int decode_init(AVCodecContext *avctx)
Definition: cscd.c:216
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
int linelen
Definition: cscd.c:34
This structure stores compressed data.
Definition: avcodec.h:950
AVCodec ff_cscd_decoder
Definition: cscd.c:250
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
for(j=16;j >0;--j)
unsigned int decomp_size
Definition: cscd.c:35
Predicted.
Definition: avutil.h:254