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 
26 #if CONFIG_ZLIB
27 #include <zlib.h>
28 #endif
29 #include "libavutil/lzo.h"
30 
31 typedef struct {
33  int linelen, height, bpp;
34  unsigned int decomp_size;
35  unsigned char* decomp_buf;
37 
38 static void copy_frame_default(AVFrame *f, const uint8_t *src, int src_stride,
39  int linelen, int height) {
40  int i;
41  uint8_t *dst = f->data[0];
42  dst += (height - 1) * f->linesize[0];
43  for (i = height; i; i--) {
44  memcpy(dst, src, linelen);
45  src += src_stride;
46  dst -= f->linesize[0];
47  }
48 }
49 
50 static void add_frame_default(AVFrame *f, const uint8_t *src, int src_stride,
51  int linelen, int height) {
52  int i, j;
53  uint8_t *dst = f->data[0];
54  dst += (height - 1) * f->linesize[0];
55  for (i = height; i; i--) {
56  for (j = linelen; j; j--)
57  *dst++ += *src++;
58  src += src_stride - linelen;
59  dst -= f->linesize[0] + linelen;
60  }
61 }
62 
63 #if !HAVE_BIGENDIAN
64 #define copy_frame_16(f, s, l, h) copy_frame_default(f, s, l, l, h)
65 #define copy_frame_32(f, s, l, h) copy_frame_default(f, s, l, l, h)
66 #define add_frame_16(f, s, l, h) add_frame_default(f, s, l, l, h)
67 #define add_frame_32(f, s, l, h) add_frame_default(f, s, l, l, h)
68 #else
69 static void copy_frame_16(AVFrame *f, const uint8_t *src,
70  int linelen, int height) {
71  int i, j;
72  uint8_t *dst = f->data[0];
73  dst += (height - 1) * f->linesize[0];
74  for (i = height; i; i--) {
75  for (j = linelen / 2; j; j--) {
76  dst[0] = src[1];
77  dst[1] = src[0];
78  src += 2;
79  dst += 2;
80  }
81  dst -= f->linesize[0] + linelen;
82  }
83 }
84 
85 static void copy_frame_32(AVFrame *f, const uint8_t *src,
86  int linelen, int height) {
87  int i, j;
88  uint8_t *dst = f->data[0];
89  dst += (height - 1) * f->linesize[0];
90  for (i = height; i; i--) {
91  for (j = linelen / 4; j; j--) {
92  dst[0] = src[3];
93  dst[1] = src[2];
94  dst[2] = src[1];
95  dst[3] = src[0];
96  src += 4;
97  dst += 4;
98  }
99  dst -= f->linesize[0] + linelen;
100  }
101 }
102 
103 static void add_frame_16(AVFrame *f, const uint8_t *src,
104  int linelen, int height) {
105  int i, j;
106  uint8_t *dst = f->data[0];
107  dst += (height - 1) * f->linesize[0];
108  for (i = height; i; i--) {
109  for (j = linelen / 2; j; j--) {
110  dst[0] += src[1];
111  dst[1] += src[0];
112  src += 2;
113  dst += 2;
114  }
115  dst -= f->linesize[0] + linelen;
116  }
117 }
118 
119 static void add_frame_32(AVFrame *f, const uint8_t *src,
120  int linelen, int height) {
121  int i, j;
122  uint8_t *dst = f->data[0];
123  dst += (height - 1) * f->linesize[0];
124  for (i = height; i; i--) {
125  for (j = linelen / 4; j; j--) {
126  dst[0] += src[3];
127  dst[1] += src[2];
128  dst[2] += src[1];
129  dst[3] += src[0];
130  src += 4;
131  dst += 4;
132  }
133  dst -= f->linesize[0] + linelen;
134  }
135 }
136 #endif
137 
138 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
139  AVPacket *avpkt) {
140  const uint8_t *buf = avpkt->data;
141  int buf_size = avpkt->size;
142  CamStudioContext *c = avctx->priv_data;
143  AVFrame *picture = data;
144 
145  if (buf_size < 2) {
146  av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
147  return -1;
148  }
149 
150  if (c->pic.data[0])
151  avctx->release_buffer(avctx, &c->pic);
152  c->pic.reference = 1;
155  if (avctx->get_buffer(avctx, &c->pic) < 0) {
156  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
157  return -1;
158  }
159 
160  // decompress data
161  switch ((buf[0] >> 1) & 7) {
162  case 0: { // lzo compression
163  int outlen = c->decomp_size, inlen = buf_size - 2;
164  if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
165  av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
166  break;
167  }
168  case 1: { // zlib compression
169 #if CONFIG_ZLIB
170  unsigned long dlen = c->decomp_size;
171  if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
172  av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
173  break;
174 #else
175  av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
176  return -1;
177 #endif
178  }
179  default:
180  av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
181  return -1;
182  }
183 
184  // flip upside down, add difference frame
185  if (buf[0] & 1) { // keyframe
187  c->pic.key_frame = 1;
188  switch (c->bpp) {
189  case 16:
190  copy_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
191  break;
192  case 32:
193  copy_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
194  break;
195  default:
197  c->linelen, c->height);
198  }
199  } else {
201  c->pic.key_frame = 0;
202  switch (c->bpp) {
203  case 16:
204  add_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
205  break;
206  case 32:
207  add_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
208  break;
209  default:
211  c->linelen, c->height);
212  }
213  }
214 
215  *picture = c->pic;
216  *data_size = sizeof(AVFrame);
217  return buf_size;
218 }
219 
220 static av_cold int decode_init(AVCodecContext *avctx) {
221  CamStudioContext *c = avctx->priv_data;
222  int stride;
223  switch (avctx->bits_per_coded_sample) {
224  case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
225  case 24: avctx->pix_fmt = PIX_FMT_BGR24; break;
226  case 32: avctx->pix_fmt = PIX_FMT_RGB32; break;
227  default:
228  av_log(avctx, AV_LOG_ERROR,
229  "CamStudio codec error: invalid depth %i bpp\n",
230  avctx->bits_per_coded_sample);
231  return AVERROR_INVALIDDATA;
232  }
233  c->bpp = avctx->bits_per_coded_sample;
234  c->pic.data[0] = NULL;
235  c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
236  c->height = avctx->height;
237  stride = c->linelen;
238  if (avctx->bits_per_coded_sample == 24)
239  stride = FFALIGN(stride, 4);
240  c->decomp_size = c->height * stride;
242  if (!c->decomp_buf) {
243  av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
244  return AVERROR(ENOMEM);
245  }
246  return 0;
247 }
248 
249 static av_cold int decode_end(AVCodecContext *avctx) {
250  CamStudioContext *c = avctx->priv_data;
251  av_freep(&c->decomp_buf);
252  if (c->pic.data[0])
253  avctx->release_buffer(avctx, &c->pic);
254  return 0;
255 }
256 
258  .name = "camstudio",
259  .type = AVMEDIA_TYPE_VIDEO,
260  .id = CODEC_ID_CSCD,
261  .priv_data_size = sizeof(CamStudioContext),
262  .init = decode_init,
263  .close = decode_end,
264  .decode = decode_frame,
265  .capabilities = CODEC_CAP_DR1,
266  .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),
267 };
268 
#define copy_frame_32(f, s, l, h)
Definition: cscd.c:65
int(* get_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of each frame to get a buffer for it.
Definition: avcodec.h:1726
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
Definition: avcodec.h:1426
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
#define FF_BUFFER_HINTS_VALID
Definition: avcodec.h:880
Audio Video Frame.
Definition: avcodec.h:985
int buffer_hints
codec suggestion on buffer type if != 0
Definition: avcodec.h:1195
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:1737
int size
Definition: avcodec.h:909
int height
Definition: cscd.c:33
int stride
Definition: mace.c:143
AVCodec.
Definition: avcodec.h:3189
#define FFALIGN(x, a)
Definition: common.h:60
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:147
static void add_frame_default(AVFrame *f, const uint8_t *src, int src_stride, int linelen, int height)
Definition: cscd.c:50
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
#define av_cold
Definition: attributes.h:71
static av_cold int decode_end(AVCodecContext *avctx)
Definition: cscd.c:249
#define PIX_FMT_RGB555
Definition: pixfmt.h:177
#define AV_LZO_OUTPUT_PADDING
Definition: lzo.h:47
const char data[16]
Definition: mxf.c:60
uint8_t * data
Definition: avcodec.h:908
#define add_frame_16(f, s, l, h)
Definition: cscd.c:66
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1974
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:336
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:719
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:1074
#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:191
unsigned char * decomp_buf
Definition: cscd.c:35
#define copy_frame_16(f, s, l, h)
Definition: cscd.c:64
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
#define add_frame_32(f, s, l, h)
Definition: cscd.c:67
static AVFrame * picture
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1029
#define f(n)
Definition: regs.h:33
static void copy_frame_default(AVFrame *f, const uint8_t *src, int src_stride, int linelen, int height)
Definition: cscd.c:38
int width
picture width / height.
Definition: avcodec.h:1408
struct AVFrame AVFrame
Audio Video Frame.
int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
Decodes LZO 1x compressed data.
Definition: lzo.c:181
#define FF_BUFFER_HINTS_REUSABLE
Definition: avcodec.h:883
NULL
Definition: eval.c:50
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:1329
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:327
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
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:64
#define FF_BUFFER_HINTS_PRESERVE
Definition: avcodec.h:882
#define FF_BUFFER_HINTS_READABLE
Definition: avcodec.h:881
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:67
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int height
Definition: gxfenc.c:73
AVFrame pic
Definition: cscd.c:32
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
Definition: cscd.c:138
void * priv_data
Definition: avcodec.h:1531
static av_cold int decode_init(AVCodecContext *avctx)
Definition: cscd.c:220
#define PIX_FMT_RGB32
Definition: pixfmt.h:169
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1022
int linelen
Definition: cscd.c:33
AVCodec ff_cscd_decoder
Definition: cscd.c:257
for(j=16;j >0;--j)
unsigned int decomp_size
Definition: cscd.c:34
Predicted.
Definition: avutil.h:297