3 * Copyright (c) 2006 Reimar Doeffinger
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 int linelen
, height
, bpp
;
33 unsigned int decomp_size
;
34 unsigned char* decomp_buf
;
37 static void copy_frame_default(AVFrame
*f
, uint8_t *src
,
38 int linelen
, int height
) {
40 uint8_t *dst
= f
->data
[0];
41 dst
+= (height
- 1) * f
->linesize
[0];
42 for (i
= height
; i
; i
--) {
43 memcpy(dst
, src
, linelen
);
45 dst
-= f
->linesize
[0];
49 static void add_frame_default(AVFrame
*f
, uint8_t *src
,
50 int linelen
, int height
) {
52 uint8_t *dst
= f
->data
[0];
53 dst
+= (height
- 1) * f
->linesize
[0];
54 for (i
= height
; i
; i
--) {
55 for (j
= linelen
; j
; j
--)
57 dst
-= f
->linesize
[0] + linelen
;
61 #ifndef WORDS_BIGENDIAN
62 #define copy_frame_16 copy_frame_default
63 #define copy_frame_32 copy_frame_default
64 #define add_frame_16 add_frame_default
65 #define add_frame_32 add_frame_default
67 static void copy_frame_16(AVFrame
*f
, uint8_t *src
,
68 int linelen
, int height
) {
70 uint8_t *dst
= f
->data
[0];
71 dst
+= (height
- 1) * f
->linesize
[0];
72 for (i
= height
; i
; i
--) {
73 for (j
= linelen
/ 2; j
; j
--) {
79 dst
-= f
->linesize
[0] + linelen
;
83 static void copy_frame_32(AVFrame
*f
, uint8_t *src
,
84 int linelen
, int height
) {
86 uint8_t *dst
= f
->data
[0];
87 dst
+= (height
- 1) * f
->linesize
[0];
88 for (i
= height
; i
; i
--) {
89 for (j
= linelen
/ 4; j
; j
--) {
97 dst
-= f
->linesize
[0] + linelen
;
101 static void add_frame_16(AVFrame
*f
, uint8_t *src
,
102 int linelen
, int height
) {
104 uint8_t *dst
= f
->data
[0];
105 dst
+= (height
- 1) * f
->linesize
[0];
106 for (i
= height
; i
; i
--) {
107 for (j
= linelen
/ 2; j
; j
--) {
113 dst
-= f
->linesize
[0] + linelen
;
117 static void add_frame_32(AVFrame
*f
, uint8_t *src
,
118 int linelen
, int height
) {
120 uint8_t *dst
= f
->data
[0];
121 dst
+= (height
- 1) * f
->linesize
[0];
122 for (i
= height
; i
; i
--) {
123 for (j
= linelen
/ 4; j
; j
--) {
131 dst
-= f
->linesize
[0] + linelen
;
136 static int decode_frame(AVCodecContext
*avctx
, void *data
, int *data_size
,
137 uint8_t *buf
, int buf_size
) {
138 CamStudioContext
*c
= (CamStudioContext
*)avctx
->priv_data
;
139 AVFrame
*picture
= data
;
142 av_log(avctx
, AV_LOG_ERROR
, "coded frame too small\n");
147 avctx
->release_buffer(avctx
, &c
->pic
);
148 c
->pic
.reference
= 1;
149 c
->pic
.buffer_hints
= FF_BUFFER_HINTS_VALID
| FF_BUFFER_HINTS_READABLE
|
150 FF_BUFFER_HINTS_PRESERVE
| FF_BUFFER_HINTS_REUSABLE
;
151 if (avctx
->get_buffer(avctx
, &c
->pic
) < 0) {
152 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
157 switch ((buf
[0] >> 1) & 7) {
158 case 0: { // lzo compression
159 int outlen
= c
->decomp_size
, inlen
= buf_size
- 2;
160 if (lzo1x_decode(c
->decomp_buf
, &outlen
, &buf
[2], &inlen
))
161 av_log(avctx
, AV_LOG_ERROR
, "error during lzo decompression\n");
164 case 1: { // zlib compression
166 unsigned long dlen
= c
->decomp_size
;
167 if (uncompress(c
->decomp_buf
, &dlen
, &buf
[2], buf_size
- 2) != Z_OK
)
168 av_log(avctx
, AV_LOG_ERROR
, "error during zlib decompression\n");
171 av_log(avctx
, AV_LOG_ERROR
, "compiled without zlib support\n");
176 av_log(avctx
, AV_LOG_ERROR
, "unknown compression\n");
180 // flip upside down, add difference frame
181 if (buf
[0] & 1) { // keyframe
182 c
->pic
.pict_type
= FF_I_TYPE
;
183 c
->pic
.key_frame
= 1;
186 copy_frame_16(&c
->pic
, c
->decomp_buf
, c
->linelen
, c
->height
);
189 copy_frame_32(&c
->pic
, c
->decomp_buf
, c
->linelen
, c
->height
);
192 copy_frame_default(&c
->pic
, c
->decomp_buf
, c
->linelen
, c
->height
);
195 c
->pic
.pict_type
= FF_P_TYPE
;
196 c
->pic
.key_frame
= 0;
199 add_frame_16(&c
->pic
, c
->decomp_buf
, c
->linelen
, c
->height
);
202 add_frame_32(&c
->pic
, c
->decomp_buf
, c
->linelen
, c
->height
);
205 add_frame_default(&c
->pic
, c
->decomp_buf
, c
->linelen
, c
->height
);
210 *data_size
= sizeof(AVFrame
);
214 static int decode_init(AVCodecContext
*avctx
) {
215 CamStudioContext
*c
= (CamStudioContext
*)avctx
->priv_data
;
216 if (avcodec_check_dimensions(avctx
, avctx
->height
, avctx
->width
) < 0) {
219 avctx
->has_b_frames
= 0;
220 switch (avctx
->bits_per_sample
) {
221 case 16: avctx
->pix_fmt
= PIX_FMT_RGB565
; break;
222 case 24: avctx
->pix_fmt
= PIX_FMT_BGR24
; break;
223 case 32: avctx
->pix_fmt
= PIX_FMT_RGBA32
; break;
225 av_log(avctx
, AV_LOG_ERROR
,
226 "CamStudio codec error: unvalid depth %i bpp\n",
227 avctx
->bits_per_sample
);
230 c
->bpp
= avctx
->bits_per_sample
;
231 c
->pic
.data
[0] = NULL
;
232 c
->linelen
= avctx
->width
* avctx
->bits_per_sample
/ 8;
233 c
->height
= avctx
->height
;
234 c
->decomp_size
= c
->height
* c
->linelen
;
235 c
->decomp_buf
= av_malloc(c
->decomp_size
+ LZO_OUTPUT_PADDING
);
236 if (!c
->decomp_buf
) {
237 av_log(avctx
, AV_LOG_ERROR
, "Can't allocate decompression buffer.\n");
243 static int decode_end(AVCodecContext
*avctx
) {
244 CamStudioContext
*c
= (CamStudioContext
*)avctx
->priv_data
;
245 av_freep(&c
->decomp_buf
);
247 avctx
->release_buffer(avctx
, &c
->pic
);
251 AVCodec cscd_decoder
= {
255 sizeof(CamStudioContext
),