FFmpeg  4.0
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 CamStudioContext {
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  return AVERROR_INVALIDDATA;
87  }
88  break;
89  }
90  case 1: { // zlib compression
91 #if CONFIG_ZLIB
92  unsigned long dlen = c->decomp_size;
93  if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK) {
94  av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
95  return AVERROR_INVALIDDATA;
96  }
97  break;
98 #else
99  av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
100  return AVERROR(ENOSYS);
101 #endif
102  }
103  default:
104  av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
105  return AVERROR_INVALIDDATA;
106  }
107 
108  // flip upside down, add difference frame
109  if (buf[0] & 1) { // keyframe
111  c->pic->key_frame = 1;
113  c->linelen, c->height);
114  } else {
116  c->pic->key_frame = 0;
118  c->linelen, c->height);
119  }
120 
121  *got_frame = 1;
122  if ((ret = av_frame_ref(data, c->pic)) < 0)
123  return ret;
124 
125  return buf_size;
126 }
127 
128 static av_cold int decode_init(AVCodecContext *avctx) {
129  CamStudioContext *c = avctx->priv_data;
130  int stride;
131  switch (avctx->bits_per_coded_sample) {
132  case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555LE; break;
133  case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
134  case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
135  default:
136  av_log(avctx, AV_LOG_ERROR,
137  "CamStudio codec error: invalid depth %i bpp\n",
138  avctx->bits_per_coded_sample);
139  return AVERROR_INVALIDDATA;
140  }
141  c->bpp = avctx->bits_per_coded_sample;
142  c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
143  c->height = avctx->height;
144  stride = FFALIGN(c->linelen, 4);
145  c->decomp_size = c->height * stride;
147  if (!c->decomp_buf) {
148  av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
149  return AVERROR(ENOMEM);
150  }
151  c->pic = av_frame_alloc();
152  if (!c->pic)
153  return AVERROR(ENOMEM);
154  return 0;
155 }
156 
157 static av_cold int decode_end(AVCodecContext *avctx) {
158  CamStudioContext *c = avctx->priv_data;
159  av_freep(&c->decomp_buf);
160  av_frame_free(&c->pic);
161  return 0;
162 }
163 
165  .name = "camstudio",
166  .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),
167  .type = AVMEDIA_TYPE_VIDEO,
168  .id = AV_CODEC_ID_CSCD,
169  .priv_data_size = sizeof(CamStudioContext),
170  .init = decode_init,
171  .close = decode_end,
172  .decode = decode_frame,
173  .capabilities = AV_CODEC_CAP_DR1,
174 };
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:218
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined ...
Definition: pixfmt.h:104
int size
Definition: avcodec.h:1431
int height
Definition: cscd.c:35
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1727
#define src
Definition: vp8dsp.c:254
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:3408
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
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...
Definition: decode.c:1938
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:189
static av_cold int decode_end(AVCodecContext *avctx)
Definition: cscd.c:157
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:441
#define AV_LZO_OUTPUT_PADDING
Definition: lzo.h:47
const char data[16]
Definition: mxf.c:90
uint8_t * data
Definition: avcodec.h:1430
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2734
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: cscd.c:65
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
unsigned char * decomp_buf
Definition: cscd.c:37
const char * name
Name of the codec implementation.
Definition: avcodec.h:3415
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:301
int width
picture width / height.
Definition: avcodec.h:1690
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:65
AVFrame * pic
Definition: cscd.c:34
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:249
main external API structure.
Definition: avcodec.h:1518
void * buf
Definition: avisynth_c.h:690
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:236
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:232
common internal api header.
common internal and external API header
static double c[64]
void * priv_data
Definition: avcodec.h:1545
static av_cold int decode_init(AVCodecContext *avctx)
Definition: cscd.c:128
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:296
int linelen
Definition: cscd.c:35
#define av_freep(p)
static void copy_frame_default(AVFrame *f, const uint8_t *src, int linelen, int height)
Definition: cscd.c:40
This structure stores compressed data.
Definition: avcodec.h:1407
AVCodec ff_cscd_decoder
Definition: cscd.c:164
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:959
for(j=16;j >0;--j)
unsigned int decomp_size
Definition: cscd.c:36
Predicted.
Definition: avutil.h:275