• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/cscd.c

Go to the documentation of this file.
00001 /*
00002  * CamStudio decoder
00003  * Copyright (c) 2006 Reimar Doeffinger
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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;
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 += linelen;
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;
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         dst -= f->linesize[0] + linelen;
00059     }
00060 }
00061 
00062 #ifndef WORDS_BIGENDIAN
00063 #define copy_frame_16 copy_frame_default
00064 #define copy_frame_32 copy_frame_default
00065 #define add_frame_16 add_frame_default
00066 #define add_frame_32 add_frame_default
00067 #else
00068 static void copy_frame_16(AVFrame *f, const uint8_t *src,
00069                           int linelen, int height) {
00070     int i, j;
00071     uint8_t *dst = f->data[0];
00072     dst += (height - 1) * f->linesize[0];
00073     for (i = height; i; i--) {
00074         for (j = linelen / 2; j; j--) {
00075           dst[0] = src[1];
00076           dst[1] = src[0];
00077           src += 2;
00078           dst += 2;
00079         }
00080         dst -= f->linesize[0] + linelen;
00081     }
00082 }
00083 
00084 static void copy_frame_32(AVFrame *f, const uint8_t *src,
00085                           int linelen, int height) {
00086     int i, j;
00087     uint8_t *dst = f->data[0];
00088     dst += (height - 1) * f->linesize[0];
00089     for (i = height; i; i--) {
00090         for (j = linelen / 4; j; j--) {
00091           dst[0] = src[3];
00092           dst[1] = src[2];
00093           dst[2] = src[1];
00094           dst[3] = src[0];
00095           src += 4;
00096           dst += 4;
00097         }
00098         dst -= f->linesize[0] + linelen;
00099     }
00100 }
00101 
00102 static void add_frame_16(AVFrame *f, const uint8_t *src,
00103                          int linelen, int height) {
00104     int i, j;
00105     uint8_t *dst = f->data[0];
00106     dst += (height - 1) * f->linesize[0];
00107     for (i = height; i; i--) {
00108         for (j = linelen / 2; j; j--) {
00109           dst[0] += src[1];
00110           dst[1] += src[0];
00111           src += 2;
00112           dst += 2;
00113         }
00114         dst -= f->linesize[0] + linelen;
00115     }
00116 }
00117 
00118 static void add_frame_32(AVFrame *f, const uint8_t *src,
00119                          int linelen, int height) {
00120     int i, j;
00121     uint8_t *dst = f->data[0];
00122     dst += (height - 1) * f->linesize[0];
00123     for (i = height; i; i--) {
00124         for (j = linelen / 4; j; j--) {
00125           dst[0] += src[3];
00126           dst[1] += src[2];
00127           dst[2] += src[1];
00128           dst[3] += src[0];
00129           src += 4;
00130           dst += 4;
00131         }
00132         dst -= f->linesize[0] + linelen;
00133     }
00134 }
00135 #endif
00136 
00137 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00138                         const uint8_t *buf, int buf_size) {
00139     CamStudioContext *c = avctx->priv_data;
00140     AVFrame *picture = data;
00141 
00142     if (buf_size < 2) {
00143         av_log(avctx, AV_LOG_ERROR, "coded frame too small\n");
00144         return -1;
00145     }
00146 
00147     if (c->pic.data[0])
00148         avctx->release_buffer(avctx, &c->pic);
00149     c->pic.reference = 1;
00150     c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE |
00151                           FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00152     if (avctx->get_buffer(avctx, &c->pic) < 0) {
00153         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00154         return -1;
00155     }
00156 
00157     // decompress data
00158     switch ((buf[0] >> 1) & 7) {
00159         case 0: { // lzo compression
00160             int outlen = c->decomp_size, inlen = buf_size - 2;
00161             if (av_lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen))
00162                 av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n");
00163             break;
00164         }
00165         case 1: { // zlib compression
00166 #if CONFIG_ZLIB
00167             unsigned long dlen = c->decomp_size;
00168             if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK)
00169                 av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n");
00170             break;
00171 #else
00172             av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n");
00173             return -1;
00174 #endif
00175         }
00176         default:
00177             av_log(avctx, AV_LOG_ERROR, "unknown compression\n");
00178             return -1;
00179     }
00180 
00181     // flip upside down, add difference frame
00182     if (buf[0] & 1) { // keyframe
00183         c->pic.pict_type = FF_I_TYPE;
00184         c->pic.key_frame = 1;
00185         switch (c->bpp) {
00186           case 16:
00187               copy_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
00188               break;
00189           case 32:
00190               copy_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
00191               break;
00192           default:
00193               copy_frame_default(&c->pic, c->decomp_buf, c->linelen, c->height);
00194         }
00195     } else {
00196         c->pic.pict_type = FF_P_TYPE;
00197         c->pic.key_frame = 0;
00198         switch (c->bpp) {
00199           case 16:
00200               add_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height);
00201               break;
00202           case 32:
00203               add_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height);
00204               break;
00205           default:
00206               add_frame_default(&c->pic, c->decomp_buf, c->linelen, c->height);
00207         }
00208     }
00209 
00210     *picture = c->pic;
00211     *data_size = sizeof(AVFrame);
00212     return buf_size;
00213 }
00214 
00215 static av_cold int decode_init(AVCodecContext *avctx) {
00216     CamStudioContext *c = avctx->priv_data;
00217     if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
00218         return 1;
00219     }
00220     switch (avctx->bits_per_coded_sample) {
00221         case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
00222         case 24: avctx->pix_fmt = PIX_FMT_BGR24; break;
00223         case 32: avctx->pix_fmt = PIX_FMT_RGB32; break;
00224         default:
00225             av_log(avctx, AV_LOG_ERROR,
00226                    "CamStudio codec error: invalid depth %i bpp\n",
00227                    avctx->bits_per_coded_sample);
00228              return 1;
00229     }
00230     c->bpp = avctx->bits_per_coded_sample;
00231     c->pic.data[0] = NULL;
00232     c->linelen = avctx->width * avctx->bits_per_coded_sample / 8;
00233     c->height = avctx->height;
00234     c->decomp_size = c->height * c->linelen;
00235     c->decomp_buf = av_malloc(c->decomp_size + AV_LZO_OUTPUT_PADDING);
00236     if (!c->decomp_buf) {
00237         av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
00238         return 1;
00239     }
00240     return 0;
00241 }
00242 
00243 static av_cold int decode_end(AVCodecContext *avctx) {
00244     CamStudioContext *c = avctx->priv_data;
00245     av_freep(&c->decomp_buf);
00246     if (c->pic.data[0])
00247         avctx->release_buffer(avctx, &c->pic);
00248     return 0;
00249 }
00250 
00251 AVCodec cscd_decoder = {
00252     "camstudio",
00253     CODEC_TYPE_VIDEO,
00254     CODEC_ID_CSCD,
00255     sizeof(CamStudioContext),
00256     decode_init,
00257     NULL,
00258     decode_end,
00259     decode_frame,
00260     CODEC_CAP_DR1,
00261     .long_name = NULL_IF_CONFIG_SMALL("CamStudio"),
00262 };
00263 

Generated on Sat Feb 16 2013 09:23:11 for ffmpeg by  doxygen 1.7.1