FFmpeg
Loading...
Searching...
No Matches
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
22#include "avcodec.h"
23#include "codec_internal.h"
24#include "decode.h"
25#include "libavutil/mem.h"
26
27#if CONFIG_ZLIB
28#include <zlib.h>
29#endif
30#include "libavutil/lzo.h"
31
32typedef struct CamStudioContext {
35 unsigned int decomp_size;
36 unsigned char* decomp_buf;
38
39static void copy_frame_default(AVFrame *f, const uint8_t *src,
40 int linelen, int height)
41{
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
52static void add_frame_default(AVFrame *f, const uint8_t *src,
53 int linelen, int height)
54{
55 int i, j, src_stride = FFALIGN(linelen, 4);
56 uint8_t *dst = f->data[0];
57 dst += (height - 1) * f->linesize[0];
58 for (i = height; i; i--) {
59 for (j = linelen; j; j--)
60 *dst++ += *src++;
61 src += src_stride - linelen;
62 dst -= f->linesize[0] + linelen;
63 }
64}
65
66static int decode_frame(AVCodecContext *avctx, AVFrame *rframe,
67 int *got_frame, AVPacket *avpkt)
68{
69 const uint8_t *buf = avpkt->data;
70 int buf_size = avpkt->size;
72 int ret;
73 int bpp = avctx->bits_per_coded_sample / 8;
74 int bugdelta = FFALIGN(avctx->width * bpp, 4) * avctx->height
75 - (avctx->width & ~3) * bpp * avctx->height;
76
77 if (buf_size < 2) {
78 av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
80 }
81
82 if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
83 return ret;
84
85 // decompress data
86 switch ((buf[0] >> 1) & 7) {
87 case 0: { // lzo compression
88 int outlen = c->decomp_size, inlen = buf_size - 2;
89 if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen) || (outlen && outlen != bugdelta)) {
90 av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
92 }
93 break;
94 }
95 case 1: { // zlib compression
96#if CONFIG_ZLIB
97 unsigned long dlen = c->decomp_size;
98 if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK || (dlen != c->decomp_size && dlen != c->decomp_size - bugdelta)) {
99 av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
100 return AVERROR_INVALIDDATA;
101 }
102 break;
103#else
104 av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
105 return AVERROR(ENOSYS);
106#endif
107 }
108 default:
109 av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
110 return AVERROR_INVALIDDATA;
111 }
112
113 // flip upside down, add difference frame
114 if (buf[0] & 1) { // keyframe
115 c->pic->pict_type = AV_PICTURE_TYPE_I;
116 c->pic->flags |= AV_FRAME_FLAG_KEY;
117 copy_frame_default(c->pic, c->decomp_buf,
118 c->linelen, c->height);
119 } else {
120 c->pic->pict_type = AV_PICTURE_TYPE_P;
121 c->pic->flags &= ~AV_FRAME_FLAG_KEY;
122 add_frame_default(c->pic, c->decomp_buf,
123 c->linelen, c->height);
124 }
125
126 *got_frame = 1;
127 if ((ret = av_frame_ref(rframe, c->pic)) < 0)
128 return ret;
129
130 return buf_size;
131}
132
134{
135 CamStudioContext *c = avctx->priv_data;
136 int stride;
137 switch (avctx->bits_per_coded_sample) {
138 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555LE; break;
139 case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
140 case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break;
141 default:
142 av_log(avctx, AV_LOG_ERROR,
143 "CamStudio codec error: invalid depth %i bpp\n",
144 avctx->bits_per_coded_sample);
145 return AVERROR_INVALIDDATA;
146 }
147 c->bpp = avctx->bits_per_coded_sample;
148 c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
149 c->height = avctx->height;
150 stride = FFALIGN(c->linelen, 4);
151 c->decomp_size = c->height * stride;
152 c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
153 if (!c->decomp_buf) {
154 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
155 return AVERROR(ENOMEM);
156 }
157 c->pic = av_frame_alloc();
158 if (!c->pic)
159 return AVERROR(ENOMEM);
160 return 0;
161}
162
164{
165 CamStudioContext *c = avctx->priv_data;
166 av_freep(&c->decomp_buf);
167 av_frame_free(&c->pic);
168 return 0;
169}
170
172 .p.name = "camstudio",
173 CODEC_LONG_NAME("CamStudio"),
174 .p.type = AVMEDIA_TYPE_VIDEO,
175 .p.id = AV_CODEC_ID_CSCD,
176 .priv_data_size = sizeof(CamStudioContext),
177 .init = decode_init,
178 .close = decode_end,
180 .p.capabilities = AV_CODEC_CAP_DR1,
181 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
182};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFCodec ff_cscd_decoder
Definition cscd.c:171
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition cscd.c:66
static void copy_frame_default(AVFrame *f, const uint8_t *src, int linelen, int height)
Definition cscd.c:39
static av_cold int decode_init(AVCodecContext *avctx)
Definition cscd.c:133
static void add_frame_default(AVFrame *f, const uint8_t *src, int linelen, int height)
Definition cscd.c:52
static av_cold int decode_end(AVCodecContext *avctx)
Definition cscd.c:163
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:1906
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_CSCD
Definition codec_id.h:129
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:687
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:278
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int av_lzo1x_decode(void *out, int *outlen, const void *in, int *inlen)
Decodes LZO 1x compressed data.
Definition lzo.c:136
#define AV_LZO_OUTPUT_PADDING
Definition lzo.h:47
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
static av_cold int decode_init(AVCodecContext *avctx)
Definition 4xm.c:998
static av_cold int decode_end(AVCodecContext *avctx)
Definition 4xm.c:980
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition 4xm.c:837
#define av_cold
Definition attributes.h:117
#define FFALIGN(x, a)
Definition macros.h:78
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition pixfmt.h:265
@ AV_PIX_FMT_RGB555LE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined
Definition pixfmt.h:115
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition avcodec.h:1564
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
AVFrame * pic
Definition cscd.c:33
unsigned char * decomp_buf
Definition cscd.c:36
unsigned int decomp_size
Definition cscd.c:35
#define stride
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:89
static double c[64]