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 "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,
41  int linelen, int height) {
42  int i, src_stride = FFALIGN(linelen, 4);
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,
53  int linelen, int height) {
54  int i, j, src_stride = FFALIGN(linelen, 4);
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 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
66  AVPacket *avpkt) {
67  const uint8_t *buf = avpkt->data;
68  int buf_size = avpkt->size;
69  CamStudioContext *c = avctx->priv_data;
70  int ret;
71 
72  if (buf_size < 2) {
73  av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
74  return AVERROR_INVALIDDATA;
75  }
76 
77  if ((ret = ff_reget_buffer(avctx, c->pic)) < 0)
78  return ret;
79 
80  // decompress data
81  switch ((buf[0] >> 1) & 7) {
82  case 0: { // lzo compression
83  int outlen = c->decomp_size, inlen = buf_size - 2;
84  if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
85  av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
86  break;
87  }
88  case 1: { // zlib compression
89 #if CONFIG_ZLIB
90  unsigned long dlen = c->decomp_size;
91  if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
92  av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
93  break;
94 #else
95  av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
96  return AVERROR(ENOSYS);
97 #endif
98  }
99  default:
100  av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
101  return AVERROR_INVALIDDATA;
102  }
103 
104  // flip upside down, add difference frame
105  if (buf[0] & 1) { // keyframe
107  c->pic->key_frame = 1;
109  c->linelen, c->height);
110  } else {
112  c->pic->key_frame = 0;
114  c->linelen, c->height);
115  }
116 
117  *got_frame = 1;
118  if ((ret = av_frame_ref(data, c->pic)) < 0)
119  return ret;
120 
121  return buf_size;
122 }
123 
124 static av_cold int decode_init(AVCodecContext *avctx) {
125  CamStudioContext *c = avctx->priv_data;
126  int stride;
127  switch (avctx->bits_per_coded_sample) {
128  case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555LE; break;
129  case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
130  case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA; break;
131  default:
132  av_log(avctx, AV_LOG_ERROR,
133  "CamStudio codec error: invalid depth %i bpp\n",
134  avctx->bits_per_coded_sample);
135  return AVERROR_INVALIDDATA;
136  }
137  c->bpp = avctx->bits_per_coded_sample;
138  c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
139  c->height = avctx->height;
140  stride = FFALIGN(c->linelen, 4);
141  c->decomp_size = c->height * stride;
143  if (!c->decomp_buf) {
144  av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
145  return AVERROR(ENOMEM);
146  }
147  c->pic = av_frame_alloc();
148  if (!c->pic)
149  return AVERROR(ENOMEM);
150  return 0;
151 }
152 
153 static av_cold int decode_end(AVCodecContext *avctx) {
154  CamStudioContext *c = avctx->priv_data;
155  av_freep(&c->decomp_buf);
156  av_frame_free(&c->pic);
157  return 0;
158 }
159 
161  .name = "camstudio",
162  .type = AVMEDIA_TYPE_VIDEO,
163  .id = AV_CODEC_ID_CSCD,
164  .priv_data_size = sizeof(CamStudioContext),
165  .init = decode_init,
166  .close = decode_end,
167  .decode = decode_frame,
168  .capabilities = CODEC_CAP_DR1,
169  .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),
170 };
static void add_frame_default(AVFrame *f, const uint8_t *src, int linelen, int height)
Definition: cscd.c:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), little-endian, most significant bit to 0 ...
Definition: pixfmt.h:117
Sinusoidal phase f
int height
Definition: cscd.c:35
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
int stride
Definition: mace.c:144
#define FFALIGN(x, a)
Definition: common.h:63
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:198
uint8_t
#define av_cold
Definition: attributes.h:78
static av_cold int decode_end(AVCodecContext *avctx)
Definition: cscd.c:153
#define AV_LZO_OUTPUT_PADDING
Definition: lzo.h:47
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
uint8_t * data
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: cscd.c:65
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
unsigned char * decomp_buf
Definition: cscd.c:37
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:99
Spectrum Plot time data
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
external API header
int ff_reget_buffer(AVCodecContext *avctx, AVFrame *frame)
Identical in function to av_frame_make_writable(), except it uses ff_get_buffer() to allocate the buf...
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:144
ret
Definition: avfilter.c:821
int width
picture width / height.
int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
Decodes LZO 1x compressed data.
Definition: lzo.c:126
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:71
AVFrame * pic
Definition: cscd.c:34
AVS_Value src
Definition: avisynth_c.h:523
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
BYTE int const BYTE int int int height
Definition: avisynth_c.h:713
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:73
synthesis window for stochastic i
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:95
int av_frame_ref(AVFrame *dst, AVFrame *src)
Setup a new reference to the data described by an given frame.
Definition: frame.c:228
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
common internal api header.
common internal and external API header
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
static double c[64]
static av_cold int decode_init(AVCodecContext *avctx)
Definition: cscd.c:124
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:139
int linelen
Definition: cscd.c:35
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
static void copy_frame_default(AVFrame *f, const uint8_t *src, int linelen, int height)
Definition: cscd.c:40
This structure stores compressed data.
AVCodec ff_cscd_decoder
Definition: cscd.c:160
for(j=16;j >0;--j)
unsigned int decomp_size
Definition: cscd.c:36
Predicted.
Definition: avutil.h:217