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 };
#define copy_frame_32(f, s, l, h)
Definition: cscd.c:67
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
int buffer_hints
codec suggestion on buffer type if != 0
Definition: avcodec.h:1253
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
int size
Definition: avcodec.h:916
int height
Definition: cscd.c:35
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2960
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
static void add_frame_default(AVFrame *f, const uint8_t *src, int src_stride, int linelen, int height)
Definition: cscd.c:52
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
uint8_t
static av_cold int decode_end(AVCodecContext *avctx)
Definition: cscd.c:251
#define AV_LZO_OUTPUT_PADDING
Definition: lzo.h:47
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
#define add_frame_16(f, s, l, h)
Definition: cscd.c:68
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2704
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: cscd.c:140
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1132
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
unsigned char * decomp_buf
Definition: cscd.c:37
#define copy_frame_16(f, s, l, h)
Definition: cscd.c:66
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
#define add_frame_32(f, s, l, h)
Definition: cscd.c:69
static AVFrame * picture
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
static void copy_frame_default(AVFrame *f, const uint8_t *src, int src_stride, int linelen, int height)
Definition: cscd.c:40
int width
picture width / height.
Definition: avcodec.h:1508
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
Decodes LZO 1x compressed data.
Definition: lzo.c:134
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
NULL
Definition: eval.c:52
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int height
Definition: gxfenc.c:72
AVFrame pic
Definition: cscd.c:34
common internal api header.
common internal and external API header
void * priv_data
Definition: avcodec.h:1382
static av_cold int decode_init(AVCodecContext *avctx)
Definition: cscd.c:222
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
int linelen
Definition: cscd.c:35
This structure stores compressed data.
Definition: avcodec.h:898
AVCodec ff_cscd_decoder
Definition: cscd.c:259
for(j=16;j >0;--j)
unsigned int decomp_size
Definition: cscd.c:36
Predicted.
Definition: avutil.h:246