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

libavcodec/lclenc.c

Go to the documentation of this file.
00001 /*
00002  * LCL (LossLess Codec Library) Codec
00003  * Copyright (c) 2002-2004 Roberto Togni
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 
00041 #include <stdio.h>
00042 #include <stdlib.h>
00043 
00044 #include "avcodec.h"
00045 #include "bitstream.h"
00046 #include "lcl.h"
00047 
00048 #if CONFIG_ZLIB
00049 #include <zlib.h>
00050 #endif
00051 
00052 /*
00053  * Decoder context
00054  */
00055 typedef struct LclEncContext {
00056 
00057         AVCodecContext *avctx;
00058         AVFrame pic;
00059     PutBitContext pb;
00060 
00061     // Image type
00062     int imgtype;
00063     // Compression type
00064     int compression;
00065     // Flags
00066     int flags;
00067     // Decompressed data size
00068     unsigned int decomp_size;
00069     // Maximum compressed data size
00070     unsigned int max_comp_size;
00071     // Compression buffer
00072     unsigned char* comp_buf;
00073 #if CONFIG_ZLIB
00074     z_stream zstream;
00075 #endif
00076 } LclEncContext;
00077 
00078 /*
00079  *
00080  * Encode a frame
00081  *
00082  */
00083 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
00084     LclEncContext *c = avctx->priv_data;
00085     AVFrame *pict = data;
00086     AVFrame * const p = &c->pic;
00087     int i;
00088     int zret; // Zlib return code
00089 
00090 #if !CONFIG_ZLIB
00091     av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled in.\n");
00092     return -1;
00093 #else
00094 
00095     init_put_bits(&c->pb, buf, buf_size);
00096 
00097     *p = *pict;
00098     p->pict_type= FF_I_TYPE;
00099     p->key_frame= 1;
00100 
00101     if(avctx->pix_fmt != PIX_FMT_BGR24){
00102         av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
00103         return -1;
00104     }
00105 
00106     zret = deflateReset(&(c->zstream));
00107     if (zret != Z_OK) {
00108         av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
00109         return -1;
00110     }
00111     c->zstream.next_out = c->comp_buf;
00112     c->zstream.avail_out = c->max_comp_size;
00113 
00114     for(i = avctx->height - 1; i >= 0; i--) {
00115         c->zstream.next_in = p->data[0]+p->linesize[0]*i;
00116         c->zstream.avail_in = avctx->width*3;
00117         zret = deflate(&(c->zstream), Z_NO_FLUSH);
00118         if (zret != Z_OK) {
00119             av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
00120             return -1;
00121         }
00122     }
00123     zret = deflate(&(c->zstream), Z_FINISH);
00124     if (zret != Z_STREAM_END) {
00125         av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
00126         return -1;
00127     }
00128 
00129     for (i = 0; i < c->zstream.total_out; i++)
00130         put_bits(&c->pb, 8, c->comp_buf[i]);
00131     flush_put_bits(&c->pb);
00132 
00133     return c->zstream.total_out;
00134 #endif
00135 }
00136 
00137 /*
00138  *
00139  * Init lcl encoder
00140  *
00141  */
00142 static av_cold int encode_init(AVCodecContext *avctx)
00143 {
00144     LclEncContext *c = avctx->priv_data;
00145     int zret; // Zlib return code
00146 
00147 #if !CONFIG_ZLIB
00148     av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
00149     return 1;
00150 #else
00151 
00152     c->avctx= avctx;
00153 
00154     assert(avctx->width && avctx->height);
00155 
00156     avctx->extradata= av_mallocz(8);
00157     avctx->coded_frame= &c->pic;
00158 
00159     // Will be user settable someday
00160     c->compression = 6;
00161     c->flags = 0;
00162 
00163     switch(avctx->pix_fmt){
00164         case PIX_FMT_BGR24:
00165             c->imgtype = IMGTYPE_RGB24;
00166             c->decomp_size = avctx->width * avctx->height * 3;
00167             avctx->bits_per_coded_sample= 24;
00168             break;
00169         default:
00170             av_log(avctx, AV_LOG_ERROR, "Input pixel format %s not supported\n", avcodec_get_pix_fmt_name(avctx->pix_fmt));
00171             return -1;
00172     }
00173 
00174     ((uint8_t*)avctx->extradata)[0]= 4;
00175     ((uint8_t*)avctx->extradata)[1]= 0;
00176     ((uint8_t*)avctx->extradata)[2]= 0;
00177     ((uint8_t*)avctx->extradata)[3]= 0;
00178     ((uint8_t*)avctx->extradata)[4]= c->imgtype;
00179     ((uint8_t*)avctx->extradata)[5]= c->compression;
00180     ((uint8_t*)avctx->extradata)[6]= c->flags;
00181     ((uint8_t*)avctx->extradata)[7]= CODEC_ZLIB;
00182     c->avctx->extradata_size= 8;
00183 
00184     c->zstream.zalloc = Z_NULL;
00185     c->zstream.zfree = Z_NULL;
00186     c->zstream.opaque = Z_NULL;
00187     zret = deflateInit(&(c->zstream), c->compression);
00188     if (zret != Z_OK) {
00189         av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret);
00190         return 1;
00191     }
00192 
00193         /* Conservative upper bound taken from zlib v1.2.1 source */
00194         c->max_comp_size = c->decomp_size + ((c->decomp_size + 7) >> 3) +
00195                            ((c->decomp_size + 63) >> 6) + 11;
00196     if ((c->comp_buf = av_malloc(c->max_comp_size)) == NULL) {
00197         av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
00198         return 1;
00199     }
00200 
00201     return 0;
00202 #endif
00203 }
00204 
00205 /*
00206  *
00207  * Uninit lcl encoder
00208  *
00209  */
00210 static av_cold int encode_end(AVCodecContext *avctx)
00211 {
00212     LclEncContext *c = avctx->priv_data;
00213 
00214     av_freep(&avctx->extradata);
00215     av_freep(&c->comp_buf);
00216 #if CONFIG_ZLIB
00217     deflateEnd(&(c->zstream));
00218 #endif
00219 
00220     return 0;
00221 }
00222 
00223 AVCodec zlib_encoder = {
00224     "zlib",
00225     CODEC_TYPE_VIDEO,
00226     CODEC_ID_ZLIB,
00227     sizeof(LclEncContext),
00228     encode_init,
00229     encode_frame,
00230     encode_end,
00231     .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
00232 };

Generated on Tue Nov 4 2014 12:59:22 for ffmpeg by  doxygen 1.7.1