FFmpeg
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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "codec_internal.h"
26 #include "internal.h"
27 #include "libavutil/common.h"
28 
29 #if CONFIG_ZLIB
30 #include <zlib.h>
31 #endif
32 #include "libavutil/lzo.h"
33 
34 typedef struct CamStudioContext {
37  unsigned int decomp_size;
38  unsigned char* decomp_buf;
40 
41 static void copy_frame_default(AVFrame *f, const uint8_t *src,
42  int linelen, int height)
43 {
44  int i, src_stride = FFALIGN(linelen, 4);
45  uint8_t *dst = f->data[0];
46  dst += (height - 1) * f->linesize[0];
47  for (i = height; i; i--) {
48  memcpy(dst, src, linelen);
49  src += src_stride;
50  dst -= f->linesize[0];
51  }
52 }
53 
54 static void add_frame_default(AVFrame *f, const uint8_t *src,
55  int linelen, int height)
56 {
57  int i, j, src_stride = FFALIGN(linelen, 4);
58  uint8_t *dst = f->data[0];
59  dst += (height - 1) * f->linesize[0];
60  for (i = height; i; i--) {
61  for (j = linelen; j; j--)
62  *dst++ += *src++;
63  src += src_stride - linelen;
64  dst -= f->linesize[0] + linelen;
65  }
66 }
67 
68 static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
69  int *got_frame, AVPacket *avpkt)
70 {
71  const uint8_t *buf = avpkt->data;
72  int buf_size = avpkt->size;
73  CamStudioContext *c = avctx->priv_data;
74  int ret;
75 
76  if (buf_size < 2) {
77  av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
78  return AVERROR_INVALIDDATA;
79  }
80 
81  if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
82  return ret;
83 
84  // decompress data
85  switch ((buf[0] >> 1) & 7) {
86  case 0: { // lzo compression
87  int outlen = c->decomp_size, inlen = buf_size - 2;
88  if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen) || outlen) {
89  av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
90  return AVERROR_INVALIDDATA;
91  }
92  break;
93  }
94  case 1: { // zlib compression
95 #if CONFIG_ZLIB
96  unsigned long dlen = c->decomp_size;
97  if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK || dlen != c->decomp_size) {
98  av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
99  return AVERROR_INVALIDDATA;
100  }
101  break;
102 #else
103  av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
104  return AVERROR(ENOSYS);
105 #endif
106  }
107  default:
108  av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
109  return AVERROR_INVALIDDATA;
110  }
111 
112  // flip upside down, add difference frame
113  if (buf[0] & 1) { // keyframe
114  c->pic->pict_type = AV_PICTURE_TYPE_I;
115  c->pic->key_frame = 1;
116  copy_frame_default(c->pic, c->decomp_buf,
117  c->linelen, c->height);
118  } else {
119  c->pic->pict_type = AV_PICTURE_TYPE_P;
120  c->pic->key_frame = 0;
121  add_frame_default(c->pic, c->decomp_buf,
122  c->linelen, c->height);
123  }
124 
125  *got_frame = 1;
126  if ((ret = av_frame_ref(rframe, c->pic)) < 0)
127  return ret;
128 
129  return buf_size;
130 }
131 
133 {
134  CamStudioContext *c = avctx->priv_data;
135  int stride;
136  switch (avctx->bits_per_coded_sample) {
137  case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555LE; break;
138  case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
139  case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
140  default:
141  av_log(avctx, AV_LOG_ERROR,
142  "CamStudio codec error: invalid depth %i bpp\n",
143  avctx->bits_per_coded_sample);
144  return AVERROR_INVALIDDATA;
145  }
146  c->bpp = avctx->bits_per_coded_sample;
147  c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
148  c->height = avctx->height;
149  stride = FFALIGN(c->linelen, 4);
150  c->decomp_size = c->height * stride;
151  c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
152  if (!c->decomp_buf) {
153  av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
154  return AVERROR(ENOMEM);
155  }
156  c->pic = av_frame_alloc();
157  if (!c->pic)
158  return AVERROR(ENOMEM);
159  return 0;
160 }
161 
163 {
164  CamStudioContext *c = avctx->priv_data;
165  av_freep(&c->decomp_buf);
166  av_frame_free(&c->pic);
167  return 0;
168 }
169 
171  .p.name = "camstudio",
172  .p.long_name = NULL_IF_CONFIG_SMALL("CamStudio"),
173  .p.type = AVMEDIA_TYPE_VIDEO,
174  .p.id = AV_CODEC_ID_CSCD,
175  .priv_data_size = sizeof(CamStudioContext),
176  .init = decode_init,
177  .close = decode_end,
179  .p.capabilities = AV_CODEC_CAP_DR1,
181 };
decode_end
static av_cold int decode_end(AVCodecContext *avctx)
Definition: cscd.c:162
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:39
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:111
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
FFCodec
Definition: codec_internal.h:112
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
init
static int init
Definition: av_tx.c:47
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: cscd.c:132
CamStudioContext::height
int height
Definition: cscd.c:36
ff_cscd_decoder
const FFCodec ff_cscd_decoder
Definition: cscd.c:170
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:99
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
copy_frame_default
static void copy_frame_default(AVFrame *f, const uint8_t *src, int linelen, int height)
Definition: cscd.c:41
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:254
CamStudioContext::decomp_buf
unsigned char * decomp_buf
Definition: cscd.c:38
add_frame_default
static void add_frame_default(AVFrame *f, const uint8_t *src, int linelen, int height)
Definition: cscd.c:54
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
AV_PIX_FMT_BGR0
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:230
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
f
f
Definition: af_crystalizer.c:122
av_lzo1x_decode
int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
Decodes LZO 1x compressed data.
Definition: lzo.c:136
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
av_frame_ref
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:343
codec_internal.h
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
CamStudioContext
Definition: cscd.c:34
CamStudioContext::bpp
int bpp
Definition: cscd.c:36
height
#define height
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1441
AV_PIX_FMT_RGB555LE
@ AV_PIX_FMT_RGB555LE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined
Definition: pixfmt.h:108
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AV_LZO_OUTPUT_PADDING
#define AV_LZO_OUTPUT_PADDING
Definition: lzo.h:47
common.h
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: codec_internal.h:31
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
AVCodecContext::height
int height
Definition: avcodec.h:562
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:599
avcodec.h
stride
#define stride
Definition: h264pred_template.c:537
ff_reget_buffer
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Identical in function to ff_get_buffer(), except it reuses the existing buffer if available.
Definition: decode.c:1521
ret
ret
Definition: filter_design.txt:187
AV_CODEC_ID_CSCD
@ AV_CODEC_ID_CSCD
Definition: codec_id.h:129
CamStudioContext::pic
AVFrame * pic
Definition: cscd.c:35
lzo.h
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition: cscd.c:68
AVCodecContext
main external API structure.
Definition: avcodec.h:389
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
CamStudioContext::linelen
int linelen
Definition: cscd.c:36
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:416
AVPacket
This structure stores compressed data.
Definition: packet.h:351
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:562
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
CamStudioContext::decomp_size
unsigned int decomp_size
Definition: cscd.c:37