1/* 2 * CamStudio decoder 3 * Copyright (c) 2006 Reimar Doeffinger 4 * 5 * This file is part of FFmpeg. 6 * 7 * FFmpeg is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * FFmpeg is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with FFmpeg; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 20 */ 21#include <stdio.h> 22#include <stdlib.h> 23 24#include "avcodec.h" 25#include "codec_internal.h" 26#include "internal.h" 27#include "libavutil/common.h" 28 29#if CONFIG_ZLIB 30#include <zlib.h> 31#endif 32#include "libavutil/lzo.h" 33 34typedef struct CamStudioContext { 35 AVFrame *pic; 36 int linelen, height, bpp; 37 unsigned int decomp_size; 38 unsigned char* decomp_buf; 39} CamStudioContext; 40 41static void copy_frame_default(AVFrame *f, const uint8_t *src, 42 int linelen, int height) 43{ 44 int i, src_stride = FFALIGN(linelen, 4); 45 uint8_t *dst = f->data[0]; 46 dst += (height - 1) * f->linesize[0]; 47 for (i = height; i; i--) { 48 memcpy(dst, src, linelen); 49 src += src_stride; 50 dst -= f->linesize[0]; 51 } 52} 53 54static void add_frame_default(AVFrame *f, const uint8_t *src, 55 int linelen, int height) 56{ 57 int i, j, src_stride = FFALIGN(linelen, 4); 58 uint8_t *dst = f->data[0]; 59 dst += (height - 1) * f->linesize[0]; 60 for (i = height; i; i--) { 61 for (j = linelen; j; j--) 62 *dst++ += *src++; 63 src += src_stride - linelen; 64 dst -= f->linesize[0] + linelen; 65 } 66} 67 68static int decode_frame(AVCodecContext *avctx, AVFrame *rframe, 69 int *got_frame, AVPacket *avpkt) 70{ 71 const uint8_t *buf = avpkt->data; 72 int buf_size = avpkt->size; 73 CamStudioContext *c = avctx->priv_data; 74 int ret; 75 int bpp = avctx->bits_per_coded_sample / 8; 76 int bugdelta = FFALIGN(avctx->width * bpp, 4) * avctx->height 77 - (avctx->width & ~3) * bpp * avctx->height; 78 79 if (buf_size < 2) { 80 av_log(avctx, AV_LOG_ERROR, "coded frame too small\n"); 81 return AVERROR_INVALIDDATA; 82 } 83 84 if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0) 85 return ret; 86 87 // decompress data 88 switch ((buf[0] >> 1) & 7) { 89 case 0: { // lzo compression 90 int outlen = c->decomp_size, inlen = buf_size - 2; 91 if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen) || (outlen && outlen != bugdelta)) { 92 av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n"); 93 return AVERROR_INVALIDDATA; 94 } 95 break; 96 } 97 case 1: { // zlib compression 98#if CONFIG_ZLIB 99 unsigned long dlen = c->decomp_size; 100 if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK || (dlen != c->decomp_size && dlen != c->decomp_size - bugdelta)) { 101 av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n"); 102 return AVERROR_INVALIDDATA; 103 } 104 break; 105#else 106 av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n"); 107 return AVERROR(ENOSYS); 108#endif 109 } 110 default: 111 av_log(avctx, AV_LOG_ERROR, "unknown compression\n"); 112 return AVERROR_INVALIDDATA; 113 } 114 115 // flip upside down, add difference frame 116 if (buf[0] & 1) { // keyframe 117 c->pic->pict_type = AV_PICTURE_TYPE_I; 118 c->pic->key_frame = 1; 119 copy_frame_default(c->pic, c->decomp_buf, 120 c->linelen, c->height); 121 } else { 122 c->pic->pict_type = AV_PICTURE_TYPE_P; 123 c->pic->key_frame = 0; 124 add_frame_default(c->pic, c->decomp_buf, 125 c->linelen, c->height); 126 } 127 128 *got_frame = 1; 129 if ((ret = av_frame_ref(rframe, c->pic)) < 0) 130 return ret; 131 132 return buf_size; 133} 134 135static av_cold int decode_init(AVCodecContext *avctx) 136{ 137 CamStudioContext *c = avctx->priv_data; 138 int stride; 139 switch (avctx->bits_per_coded_sample) { 140 case 16: avctx->pix_fmt = AV_PIX_FMT_RGB555LE; break; 141 case 24: avctx->pix_fmt = AV_PIX_FMT_BGR24; break; 142 case 32: avctx->pix_fmt = AV_PIX_FMT_BGR0; break; 143 default: 144 av_log(avctx, AV_LOG_ERROR, 145 "CamStudio codec error: invalid depth %i bpp\n", 146 avctx->bits_per_coded_sample); 147 return AVERROR_INVALIDDATA; 148 } 149 c->bpp = avctx->bits_per_coded_sample; 150 c->linelen = avctx->width * avctx->bits_per_coded_sample / 8; 151 c->height = avctx->height; 152 stride = FFALIGN(c->linelen, 4); 153 c->decomp_size = c->height * stride; 154 c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING); 155 if (!c->decomp_buf) { 156 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n"); 157 return AVERROR(ENOMEM); 158 } 159 c->pic = av_frame_alloc(); 160 if (!c->pic) 161 return AVERROR(ENOMEM); 162 return 0; 163} 164 165static av_cold int decode_end(AVCodecContext *avctx) 166{ 167 CamStudioContext *c = avctx->priv_data; 168 av_freep(&c->decomp_buf); 169 av_frame_free(&c->pic); 170 return 0; 171} 172 173const FFCodec ff_cscd_decoder = { 174 .p.name = "camstudio", 175 .p.long_name = NULL_IF_CONFIG_SMALL("CamStudio"), 176 .p.type = AVMEDIA_TYPE_VIDEO, 177 .p.id = AV_CODEC_ID_CSCD, 178 .priv_data_size = sizeof(CamStudioContext), 179 .init = decode_init, 180 .close = decode_end, 181 FF_CODEC_DECODE_CB(decode_frame), 182 .p.capabilities = AV_CODEC_CAP_DR1, 183 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP, 184}; 185