e0f80bd7 |
/*
* CamStudio decoder
* Copyright (c) 2006 Reimar Doeffinger
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
e0f80bd7 |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
e0f80bd7 |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
e0f80bd7 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
e0f80bd7 |
*/
#include <stdio.h>
#include <stdlib.h>
#include "avcodec.h" |
594d4d5d |
#include "internal.h" |
1d9c2dc8 |
#include "libavutil/common.h" |
e0f80bd7 |
|
b250f9c6 |
#if CONFIG_ZLIB |
e0f80bd7 |
#include <zlib.h>
#endif |
245976da |
#include "libavutil/lzo.h" |
e0f80bd7 |
|
7f9f771e |
typedef struct CamStudioContext { |
80e9e63c |
AVFrame *pic; |
e0f80bd7 |
int linelen, height, bpp;
unsigned int decomp_size;
unsigned char* decomp_buf;
} CamStudioContext;
|
3dafde0b |
static void copy_frame_default(AVFrame *f, const uint8_t *src, |
e0f80bd7 |
int linelen, int height) { |
3dafde0b |
int i, src_stride = FFALIGN(linelen, 4); |
e0f80bd7 |
uint8_t *dst = f->data[0];
dst += (height - 1) * f->linesize[0];
for (i = height; i; i--) {
memcpy(dst, src, linelen); |
936cfbc5 |
src += src_stride; |
e0f80bd7 |
dst -= f->linesize[0];
}
}
|
3dafde0b |
static void add_frame_default(AVFrame *f, const uint8_t *src, |
e0f80bd7 |
int linelen, int height) { |
3dafde0b |
int i, j, src_stride = FFALIGN(linelen, 4); |
e0f80bd7 |
uint8_t *dst = f->data[0];
dst += (height - 1) * f->linesize[0];
for (i = height; i; i--) {
for (j = linelen; j; j--)
*dst++ += *src++; |
936cfbc5 |
src += src_stride - linelen; |
e0f80bd7 |
dst -= f->linesize[0] + linelen;
}
}
|
df9b9567 |
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, |
7a00bbad |
AVPacket *avpkt) {
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size; |
e4141433 |
CamStudioContext *c = avctx->priv_data; |
8fd4d1f9 |
int ret; |
e0f80bd7 |
if (buf_size < 2) {
av_log(avctx, AV_LOG_ERROR, "coded frame too small\n"); |
8fd4d1f9 |
return AVERROR_INVALIDDATA; |
e0f80bd7 |
}
|
1ec94b0f |
if ((ret = ff_reget_buffer(avctx, c->pic)) < 0) |
8fd4d1f9 |
return ret; |
e0f80bd7 |
// decompress data
switch ((buf[0] >> 1) & 7) {
case 0: { // lzo compression |
517840c6 |
int outlen = c->decomp_size, inlen = buf_size - 2; |
35c365f6 |
if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen) || outlen) { |
e0f80bd7 |
av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n"); |
f4e01b87 |
return AVERROR_INVALIDDATA;
} |
e0f80bd7 |
break;
}
case 1: { // zlib compression |
b250f9c6 |
#if CONFIG_ZLIB |
e0f80bd7 |
unsigned long dlen = c->decomp_size; |
f4e01b87 |
if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK) { |
e0f80bd7 |
av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n"); |
f4e01b87 |
return AVERROR_INVALIDDATA;
} |
e0f80bd7 |
break;
#else
av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n"); |
8fd4d1f9 |
return AVERROR(ENOSYS); |
e0f80bd7 |
#endif
}
default:
av_log(avctx, AV_LOG_ERROR, "unknown compression\n"); |
8fd4d1f9 |
return AVERROR_INVALIDDATA; |
e0f80bd7 |
}
// flip upside down, add difference frame
if (buf[0] & 1) { // keyframe |
80e9e63c |
c->pic->pict_type = AV_PICTURE_TYPE_I;
c->pic->key_frame = 1;
copy_frame_default(c->pic, c->decomp_buf, |
936cfbc5 |
c->linelen, c->height); |
e0f80bd7 |
} else { |
80e9e63c |
c->pic->pict_type = AV_PICTURE_TYPE_P;
c->pic->key_frame = 0;
add_frame_default(c->pic, c->decomp_buf, |
936cfbc5 |
c->linelen, c->height); |
e0f80bd7 |
}
|
df9b9567 |
*got_frame = 1; |
80e9e63c |
if ((ret = av_frame_ref(data, c->pic)) < 0)
return ret;
|
e0f80bd7 |
return buf_size;
}
|
98a6fff9 |
static av_cold int decode_init(AVCodecContext *avctx) { |
e4141433 |
CamStudioContext *c = avctx->priv_data; |
936cfbc5 |
int stride; |
dd1c8f3e |
switch (avctx->bits_per_coded_sample) { |
ac627b3d |
case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555LE; break; |
716d413c |
case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break; |
67fe1a2b |
case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break; |
e0f80bd7 |
default:
av_log(avctx, AV_LOG_ERROR, |
2c04fc1f |
"CamStudio codec error: invalid depth %i bpp\n", |
dd1c8f3e |
avctx->bits_per_coded_sample); |
8a9faf33 |
return AVERROR_INVALIDDATA; |
e0f80bd7 |
} |
dd1c8f3e |
c->bpp = avctx->bits_per_coded_sample;
c->linelen = avctx->width * avctx->bits_per_coded_sample / 8; |
e0f80bd7 |
c->height = avctx->height; |
3dafde0b |
stride = FFALIGN(c->linelen, 4); |
936cfbc5 |
c->decomp_size = c->height * stride; |
0b178e56 |
c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING); |
e0f80bd7 |
if (!c->decomp_buf) {
av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n"); |
8a9faf33 |
return AVERROR(ENOMEM); |
e0f80bd7 |
} |
80e9e63c |
c->pic = av_frame_alloc();
if (!c->pic)
return AVERROR(ENOMEM); |
e0f80bd7 |
return 0;
}
|
98a6fff9 |
static av_cold int decode_end(AVCodecContext *avctx) { |
e4141433 |
CamStudioContext *c = avctx->priv_data; |
e0f80bd7 |
av_freep(&c->decomp_buf); |
80e9e63c |
av_frame_free(&c->pic); |
e0f80bd7 |
return 0;
}
|
e7e2df27 |
AVCodec ff_cscd_decoder = { |
ec6402b7 |
.name = "camstudio", |
b2bed932 |
.long_name = NULL_IF_CONFIG_SMALL("CamStudio"), |
ec6402b7 |
.type = AVMEDIA_TYPE_VIDEO, |
36ef5369 |
.id = AV_CODEC_ID_CSCD, |
ec6402b7 |
.priv_data_size = sizeof(CamStudioContext),
.init = decode_init,
.close = decode_end,
.decode = decode_frame, |
def97856 |
.capabilities = AV_CODEC_CAP_DR1, |
e0f80bd7 |
}; |