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