FFmpeg  4.4.6
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 {
43  int i, src_stride = FFALIGN(linelen, 4);
44  uint8_t *dst = f->data[0];
45  dst += (height - 1) * f->linesize[0];
46  for (i = height; i; i--) {
47  memcpy(dst, src, linelen);
48  src += src_stride;
49  dst -= f->linesize[0];
50  }
51 }
52 
53 static void add_frame_default(AVFrame *f, const uint8_t *src,
54  int linelen, int height)
55 {
56  int i, j, src_stride = FFALIGN(linelen, 4);
57  uint8_t *dst = f->data[0];
58  dst += (height - 1) * f->linesize[0];
59  for (i = height; i; i--) {
60  for (j = linelen; j; j--)
61  *dst++ += *src++;
62  src += src_stride - linelen;
63  dst -= f->linesize[0] + linelen;
64  }
65 }
66 
67 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
68  AVPacket *avpkt)
69 {
70  const uint8_t *buf = avpkt->data;
71  int buf_size = avpkt->size;
72  CamStudioContext *c = avctx->priv_data;
73  int ret;
74  int bpp = avctx->bits_per_coded_sample / 8;
75  int bugdelta = FFALIGN(avctx->width * bpp, 4) * avctx->height
76  - (avctx->width & ~3) * bpp * avctx->height;
77 
78  if (buf_size < 2) {
79  av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
80  return AVERROR_INVALIDDATA;
81  }
82 
83  if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
84  return ret;
85 
86  // decompress data
87  switch ((buf[0] >> 1) & 7) {
88  case 0: { // lzo compression
89  int outlen = c->decomp_size, inlen = buf_size - 2;
90  if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen) || (outlen && outlen != bugdelta)) {
91  av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
92  return AVERROR_INVALIDDATA;
93  }
94  break;
95  }
96  case 1: { // zlib compression
97 #if CONFIG_ZLIB
98  unsigned long dlen = c->decomp_size;
99  if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK || (dlen != c->decomp_size && dlen != c->decomp_size - bugdelta)) {
100  av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
101  return AVERROR_INVALIDDATA;
102  }
103  break;
104 #else
105  av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
106  return AVERROR(ENOSYS);
107 #endif
108  }
109  default:
110  av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
111  return AVERROR_INVALIDDATA;
112  }
113 
114  // flip upside down, add difference frame
115  if (buf[0] & 1) { // keyframe
116  c->pic->pict_type = AV_PICTURE_TYPE_I;
117  c->pic->key_frame = 1;
118  copy_frame_default(c->pic, c->decomp_buf,
119  c->linelen, c->height);
120  } else {
121  c->pic->pict_type = AV_PICTURE_TYPE_P;
122  c->pic->key_frame = 0;
123  add_frame_default(c->pic, c->decomp_buf,
124  c->linelen, c->height);
125  }
126 
127  *got_frame = 1;
128  if ((ret = av_frame_ref(data, c->pic)) < 0)
129  return ret;
130 
131  return buf_size;
132 }
133 
135 {
136  CamStudioContext *c = avctx->priv_data;
137  int stride;
138  switch (avctx->bits_per_coded_sample) {
139  case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555LE; break;
140  case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
141  case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
142  default:
143  av_log(avctx, AV_LOG_ERROR,
144  "CamStudio codec error: invalid depth %i bpp\n",
145  avctx->bits_per_coded_sample);
146  return AVERROR_INVALIDDATA;
147  }
148  c->bpp = avctx->bits_per_coded_sample;
149  c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
150  c->height = avctx->height;
151  stride = FFALIGN(c->linelen, 4);
152  c->decomp_size = c->height * stride;
153  c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
154  if (!c->decomp_buf) {
155  av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
156  return AVERROR(ENOMEM);
157  }
158  c->pic = av_frame_alloc();
159  if (!c->pic)
160  return AVERROR(ENOMEM);
161  return 0;
162 }
163 
165 {
166  CamStudioContext *c = avctx->priv_data;
167  av_freep(&c->decomp_buf);
168  av_frame_free(&c->pic);
169  return 0;
170 }
171 
173  .name = "camstudio",
174  .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),
175  .type = AVMEDIA_TYPE_VIDEO,
176  .id = AV_CODEC_ID_CSCD,
177  .priv_data_size = sizeof(CamStudioContext),
178  .init = decode_init,
179  .close = decode_end,
180  .decode = decode_frame,
181  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
182  .capabilities = AV_CODEC_CAP_DR1,
183 };
#define av_cold
Definition: attributes.h:88
uint8_t
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define f(width, name)
Definition: cbs_vp9.c:255
common internal and external API header
static void copy_frame_default(AVFrame *f, const uint8_t *src, int linelen, int height)
Definition: cscd.c:40
static av_cold int decode_init(AVCodecContext *avctx)
Definition: cscd.c:134
static void add_frame_default(AVFrame *f, const uint8_t *src, int linelen, int height)
Definition: cscd.c:53
static av_cold int decode_end(AVCodecContext *avctx)
Definition: cscd.c:164
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: cscd.c:67
AVCodec ff_cscd_decoder
Definition: cscd.c:172
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:2007
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
@ AV_CODEC_ID_CSCD
Definition: codec_id.h:128
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
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:443
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
Decodes LZO 1x compressed data.
Definition: lzo.c:134
#define AV_LZO_OUTPUT_PADDING
Definition: lzo.h:47
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
for(j=16;j >0;--j)
int i
Definition: input.c:407
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:49
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
int stride
Definition: mace.c:144
#define FFALIGN(x, a)
Definition: macros.h:48
const char data[16]
Definition: mxf.c:142
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:240
@ 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
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1744
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
int height
Definition: cscd.c:35
AVFrame * pic
Definition: cscd.c:34
unsigned char * decomp_buf
Definition: cscd.c:37
int linelen
Definition: cscd.c:35
unsigned int decomp_size
Definition: cscd.c:36
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
#define height
if(ret< 0)
Definition: vf_mcdeint.c:282
static double c[64]