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

libavcodec/tscc.c

Go to the documentation of this file.
00001 /*
00002  * TechSmith Camtasia decoder
00003  * Copyright (c) 2004 Konstantin Shishkov
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 
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 
00041 #include "avcodec.h"
00042 #include "msrledec.h"
00043 
00044 #include <zlib.h>
00045 
00046 
00047 /*
00048  * Decoder context
00049  */
00050 typedef struct TsccContext {
00051 
00052     AVCodecContext *avctx;
00053     AVFrame pic;
00054 
00055     // Bits per pixel
00056     int bpp;
00057     // Decompressed data size
00058     unsigned int decomp_size;
00059     // Decompression buffer
00060     unsigned char* decomp_buf;
00061     int height;
00062     z_stream zstream;
00063 } CamtasiaContext;
00064 
00065 /*
00066  *
00067  * Decode a frame
00068  *
00069  */
00070 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
00071 {
00072     const uint8_t *buf = avpkt->data;
00073     int buf_size = avpkt->size;
00074     CamtasiaContext * const c = avctx->priv_data;
00075     const unsigned char *encoded = buf;
00076     unsigned char *outptr;
00077     int zret; // Zlib return code
00078     int len = buf_size;
00079 
00080     if(c->pic.data[0])
00081             avctx->release_buffer(avctx, &c->pic);
00082 
00083     c->pic.reference = 1;
00084     c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
00085     if(avctx->get_buffer(avctx, &c->pic) < 0){
00086         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00087         return -1;
00088     }
00089 
00090     outptr = c->pic.data[0]; // Output image pointer
00091 
00092     zret = inflateReset(&(c->zstream));
00093     if (zret != Z_OK) {
00094         av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
00095         return -1;
00096     }
00097     c->zstream.next_in = encoded;
00098     c->zstream.avail_in = len;
00099     c->zstream.next_out = c->decomp_buf;
00100     c->zstream.avail_out = c->decomp_size;
00101     zret = inflate(&(c->zstream), Z_FINISH);
00102     // Z_DATA_ERROR means empty picture
00103     if ((zret != Z_OK) && (zret != Z_STREAM_END) && (zret != Z_DATA_ERROR)) {
00104         av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
00105         return -1;
00106     }
00107 
00108 
00109     if(zret != Z_DATA_ERROR)
00110         ff_msrle_decode(avctx, (AVPicture*)&c->pic, c->bpp, c->decomp_buf, c->decomp_size - c->zstream.avail_out);
00111 
00112     /* make the palette available on the way out */
00113     if (c->avctx->pix_fmt == PIX_FMT_PAL8) {
00114         memcpy(c->pic.data[1], c->avctx->palctrl->palette, AVPALETTE_SIZE);
00115         if (c->avctx->palctrl->palette_changed) {
00116             c->pic.palette_has_changed = 1;
00117             c->avctx->palctrl->palette_changed = 0;
00118         }
00119     }
00120 
00121     *data_size = sizeof(AVFrame);
00122     *(AVFrame*)data = c->pic;
00123 
00124     /* always report that the buffer was completely consumed */
00125     return buf_size;
00126 }
00127 
00128 
00129 
00130 /*
00131  *
00132  * Init tscc decoder
00133  *
00134  */
00135 static av_cold int decode_init(AVCodecContext *avctx)
00136 {
00137     CamtasiaContext * const c = avctx->priv_data;
00138     int zret; // Zlib return code
00139 
00140     c->avctx = avctx;
00141 
00142     c->height = avctx->height;
00143 
00144     // Needed if zlib unused or init aborted before inflateInit
00145     memset(&(c->zstream), 0, sizeof(z_stream));
00146     switch(avctx->bits_per_coded_sample){
00147     case  8: avctx->pix_fmt = PIX_FMT_PAL8; break;
00148     case 16: avctx->pix_fmt = PIX_FMT_RGB555; break;
00149     case 24:
00150              avctx->pix_fmt = PIX_FMT_BGR24;
00151              break;
00152     case 32: avctx->pix_fmt = PIX_FMT_RGB32; break;
00153     default: av_log(avctx, AV_LOG_ERROR, "Camtasia error: unknown depth %i bpp\n", avctx->bits_per_coded_sample);
00154              return -1;
00155     }
00156     c->bpp = avctx->bits_per_coded_sample;
00157     // buffer size for RLE 'best' case when 2-byte code preceeds each pixel and there may be padding after it too
00158     c->decomp_size = (((avctx->width * c->bpp + 7) >> 3) + 3 * avctx->width + 2) * avctx->height + 2;
00159 
00160     /* Allocate decompression buffer */
00161     if (c->decomp_size) {
00162         if ((c->decomp_buf = av_malloc(c->decomp_size)) == NULL) {
00163             av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
00164             return 1;
00165         }
00166     }
00167 
00168     c->zstream.zalloc = Z_NULL;
00169     c->zstream.zfree = Z_NULL;
00170     c->zstream.opaque = Z_NULL;
00171     zret = inflateInit(&(c->zstream));
00172     if (zret != Z_OK) {
00173         av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
00174         return 1;
00175     }
00176 
00177     return 0;
00178 }
00179 
00180 
00181 
00182 /*
00183  *
00184  * Uninit tscc decoder
00185  *
00186  */
00187 static av_cold int decode_end(AVCodecContext *avctx)
00188 {
00189     CamtasiaContext * const c = avctx->priv_data;
00190 
00191     av_freep(&c->decomp_buf);
00192 
00193     if (c->pic.data[0])
00194         avctx->release_buffer(avctx, &c->pic);
00195     inflateEnd(&(c->zstream));
00196 
00197     return 0;
00198 }
00199 
00200 AVCodec tscc_decoder = {
00201         "camtasia",
00202         AVMEDIA_TYPE_VIDEO,
00203         CODEC_ID_TSCC,
00204         sizeof(CamtasiaContext),
00205         decode_init,
00206         NULL,
00207         decode_end,
00208         decode_frame,
00209         CODEC_CAP_DR1,
00210         .long_name = NULL_IF_CONFIG_SMALL("TechSmith Screen Capture Codec"),
00211 };
00212 

Generated on Fri Sep 16 2011 17:17:44 for FFmpeg by  doxygen 1.7.1