00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023
00024 #include "avcodec.h"
00025 #include "internal.h"
00026 #include "libavutil/common.h"
00027
00028 #if CONFIG_ZLIB
00029 #include <zlib.h>
00030 #endif
00031 #include "libavutil/lzo.h"
00032
00033 typedef struct {
00034 AVFrame pic;
00035 int linelen, height, bpp;
00036 unsigned int decomp_size;
00037 unsigned char* decomp_buf;
00038 } CamStudioContext;
00039
00040 static void copy_frame_default(AVFrame *f, const uint8_t *src,
00041 int linelen, int height) {
00042 int i, src_stride = FFALIGN(linelen, 4);
00043 uint8_t *dst = f->data[0];
00044 dst += (height - 1) * f->linesize[0];
00045 for (i = height; i; i--) {
00046 memcpy(dst, src, linelen);
00047 src += src_stride;
00048 dst -= f->linesize[0];
00049 }
00050 }
00051
00052 static void add_frame_default(AVFrame *f, const uint8_t *src,
00053 int linelen, int height) {
00054 int i, j, src_stride = FFALIGN(linelen, 4);
00055 uint8_t *dst = f->data[0];
00056 dst += (height - 1) * f->linesize[0];
00057 for (i = height; i; i--) {
00058 for (j = linelen; j; j--)
00059 *dst++ += *src++;
00060 src += src_stride - linelen;
00061 dst -= f->linesize[0] + linelen;
00062 }
00063 }
00064
00065 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
00066 AVPacket *avpkt) {
00067 const uint8_t *buf = avpkt->data;
00068 int buf_size = avpkt->size;
00069 CamStudioContext *c = avctx->priv_data;
00070 AVFrame *picture = data;
00071
00072 if (buf_size < 2) {
00073 av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
00074 return -1;
00075 }
00076
00077 c->pic.reference = 3;
00078 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
00079 FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00080 if (avctx->reget_buffer(avctx, &c->pic) < 0) {
00081 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00082 return -1;
00083 }
00084
00085
00086 switch ((buf[0] >> 1) & 7) {
00087 case 0: {
00088 int outlen = c->decomp_size, inlen = buf_size - 2;
00089 if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
00090 av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
00091 break;
00092 }
00093 case 1: {
00094 #if CONFIG_ZLIB
00095 unsigned long dlen = c->decomp_size;
00096 if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
00097 av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
00098 break;
00099 #else
00100 av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
00101 return -1;
00102 #endif
00103 }
00104 default:
00105 av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
00106 return -1;
00107 }
00108
00109
00110 if (buf[0] & 1) {
00111 c->pic.pict_type = AV_PICTURE_TYPE_I;
00112 c->pic.key_frame = 1;
00113 copy_frame_default(&c->pic, c->decomp_buf,
00114 c->linelen, c->height);
00115 } else {
00116 c->pic.pict_type = AV_PICTURE_TYPE_P;
00117 c->pic.key_frame = 0;
00118 add_frame_default(&c->pic, c->decomp_buf,
00119 c->linelen, c->height);
00120 }
00121
00122 *picture = c->pic;
00123 *got_frame = 1;
00124 return buf_size;
00125 }
00126
00127 static av_cold int decode_init(AVCodecContext *avctx) {
00128 CamStudioContext *c = avctx->priv_data;
00129 int stride;
00130 switch (avctx->bits_per_coded_sample) {
00131 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555LE; break;
00132 case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break;
00133 case 32: avctx->pix_fmt = AV_PIX_FMT_BGRA; break;
00134 default:
00135 av_log(avctx, AV_LOG_ERROR,
00136 "CamStudio codec error: invalid depth %i bpp\n",
00137 avctx->bits_per_coded_sample);
00138 return AVERROR_INVALIDDATA;
00139 }
00140 c->bpp = avctx->bits_per_coded_sample;
00141 avcodec_get_frame_defaults(&c->pic);
00142 c->pic.data[0] = NULL;
00143 c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
00144 c->height = avctx->height;
00145 stride = FFALIGN(c->linelen, 4);
00146 c->decomp_size = c->height * stride;
00147 c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
00148 if (!c->decomp_buf) {
00149 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
00150 return AVERROR(ENOMEM);
00151 }
00152 return 0;
00153 }
00154
00155 static av_cold int decode_end(AVCodecContext *avctx) {
00156 CamStudioContext *c = avctx->priv_data;
00157 av_freep(&c->decomp_buf);
00158 if (c->pic.data[0])
00159 avctx->release_buffer(avctx, &c->pic);
00160 return 0;
00161 }
00162
00163 AVCodec ff_cscd_decoder = {
00164 .name = "camstudio",
00165 .type = AVMEDIA_TYPE_VIDEO,
00166 .id = AV_CODEC_ID_CSCD,
00167 .priv_data_size = sizeof(CamStudioContext),
00168 .init = decode_init,
00169 .close = decode_end,
00170 .decode = decode_frame,
00171 .capabilities = CODEC_CAP_DR1,
00172 .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),
00173 };