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