libavcodec/lcldec.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 "bytestream.h"
00046 #include "lcl.h"
00047 #include "libavutil/lzo.h"
00048 
00049 #if CONFIG_ZLIB_DECODER
00050 #include <zlib.h>
00051 #endif
00052 
00053 /*
00054  * Decoder context
00055  */
00056 typedef struct LclDecContext {
00057     AVFrame pic;
00058 
00059     // Image type
00060     int imgtype;
00061     // Compression type
00062     int compression;
00063     // Flags
00064     int flags;
00065     // Decompressed data size
00066     unsigned int decomp_size;
00067     // Decompression buffer
00068     unsigned char* decomp_buf;
00069 #if CONFIG_ZLIB_DECODER
00070     z_stream zstream;
00071 #endif
00072 } LclDecContext;
00073 
00074 
00079 static unsigned int mszh_decomp(const unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
00080 {
00081     unsigned char *destptr_bak = destptr;
00082     unsigned char *destptr_end = destptr + destsize;
00083     const unsigned char *srcptr_end = srcptr + srclen;
00084     unsigned mask = *srcptr++;
00085     unsigned maskbit = 0x80;
00086 
00087     while (srcptr < srcptr_end && destptr < destptr_end) {
00088         if (!(mask & maskbit)) {
00089             memcpy(destptr, srcptr, 4);
00090             destptr += 4;
00091             srcptr += 4;
00092         } else {
00093             unsigned ofs = bytestream_get_le16(&srcptr);
00094             unsigned cnt = (ofs >> 11) + 1;
00095             ofs &= 0x7ff;
00096             ofs = FFMIN(ofs, destptr - destptr_bak);
00097             cnt *= 4;
00098             cnt = FFMIN(cnt, destptr_end - destptr);
00099             if (ofs) {
00100                 av_memcpy_backptr(destptr, ofs, cnt);
00101             } else {
00102                 // Not known what the correct behaviour is, but
00103                 // this at least avoids uninitialized data.
00104                 memset(destptr, 0, cnt);
00105             }
00106             destptr += cnt;
00107         }
00108         maskbit >>= 1;
00109         if (!maskbit) {
00110             mask = *srcptr++;
00111             while (!mask) {
00112                 if (destptr_end - destptr < 32 || srcptr_end - srcptr < 32) break;
00113                 memcpy(destptr, srcptr, 32);
00114                 destptr += 32;
00115                 srcptr += 32;
00116                 mask = *srcptr++;
00117             }
00118             maskbit = 0x80;
00119         }
00120     }
00121 
00122     return destptr - destptr_bak;
00123 }
00124 
00125 
00126 #if CONFIG_ZLIB_DECODER
00127 
00134 static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected)
00135 {
00136     LclDecContext *c = avctx->priv_data;
00137     int zret = inflateReset(&c->zstream);
00138     if (zret != Z_OK) {
00139         av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
00140         return -1;
00141     }
00142     c->zstream.next_in = src;
00143     c->zstream.avail_in = src_len;
00144     c->zstream.next_out = c->decomp_buf + offset;
00145     c->zstream.avail_out = c->decomp_size - offset;
00146     zret = inflate(&c->zstream, Z_FINISH);
00147     if (zret != Z_OK && zret != Z_STREAM_END) {
00148         av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
00149         return -1;
00150     }
00151     if (expected != (unsigned int)c->zstream.total_out) {
00152         av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
00153                expected, c->zstream.total_out);
00154         return -1;
00155     }
00156     return c->zstream.total_out;
00157 }
00158 #endif
00159 
00160 
00161 /*
00162  *
00163  * Decode a frame
00164  *
00165  */
00166 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
00167 {
00168     const uint8_t *buf = avpkt->data;
00169     int buf_size = avpkt->size;
00170     LclDecContext * const c = avctx->priv_data;
00171     unsigned char *encoded = (unsigned char *)buf;
00172     unsigned int pixel_ptr;
00173     int row, col;
00174     unsigned char *outptr;
00175     uint8_t *y_out, *u_out, *v_out;
00176     unsigned int width = avctx->width; // Real image width
00177     unsigned int height = avctx->height; // Real image height
00178     unsigned int mszh_dlen;
00179     unsigned char yq, y1q, uq, vq;
00180     int uqvq;
00181     unsigned int mthread_inlen, mthread_outlen;
00182     unsigned int len = buf_size;
00183 
00184     if(c->pic.data[0])
00185         avctx->release_buffer(avctx, &c->pic);
00186 
00187     c->pic.reference = 0;
00188     c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
00189     if(avctx->get_buffer(avctx, &c->pic) < 0){
00190         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00191         return -1;
00192     }
00193 
00194     outptr = c->pic.data[0]; // Output image pointer
00195 
00196     /* Decompress frame */
00197     switch (avctx->codec_id) {
00198     case CODEC_ID_MSZH:
00199         switch (c->compression) {
00200         case COMP_MSZH:
00201             if (c->flags & FLAG_MULTITHREAD) {
00202                 mthread_inlen = AV_RL32(encoded);
00203                 mthread_inlen = FFMIN(mthread_inlen, len - 8);
00204                 mthread_outlen = AV_RL32(encoded+4);
00205                 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
00206                 mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
00207                 if (mthread_outlen != mszh_dlen) {
00208                     av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
00209                            mthread_outlen, mszh_dlen);
00210                     return -1;
00211                 }
00212                 mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
00213                                         c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
00214                 if (mthread_outlen != mszh_dlen) {
00215                     av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
00216                            mthread_outlen, mszh_dlen);
00217                     return -1;
00218                 }
00219                 encoded = c->decomp_buf;
00220                 len = c->decomp_size;
00221             } else {
00222                 mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
00223                 if (c->decomp_size != mszh_dlen) {
00224                     av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
00225                            c->decomp_size, mszh_dlen);
00226                     return -1;
00227                 }
00228                 encoded = c->decomp_buf;
00229                 len = mszh_dlen;
00230             }
00231             break;
00232         case COMP_MSZH_NOCOMP: {
00233             int bppx2;
00234             switch (c->imgtype) {
00235             case IMGTYPE_YUV111:
00236             case IMGTYPE_RGB24:
00237                 bppx2 = 6;
00238                 break;
00239             case IMGTYPE_YUV422:
00240             case IMGTYPE_YUV211:
00241                 bppx2 = 4;
00242                 break;
00243             case IMGTYPE_YUV411:
00244             case IMGTYPE_YUV420:
00245                 bppx2 = 3;
00246                 break;
00247             default:
00248                 bppx2 = 0; // will error out below
00249                 break;
00250             }
00251             if (len < ((width * height * bppx2) >> 1))
00252                 return AVERROR_INVALIDDATA;
00253             break;
00254         }
00255         default:
00256             av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
00257             return -1;
00258         }
00259         break;
00260 #if CONFIG_ZLIB_DECODER
00261     case CODEC_ID_ZLIB:
00262         /* Using the original dll with normal compression (-1) and RGB format
00263          * gives a file with ZLIB fourcc, but frame is really uncompressed.
00264          * To be sure that's true check also frame size */
00265         if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
00266             len == width * height * 3)
00267             break;
00268         if (c->flags & FLAG_MULTITHREAD) {
00269             int ret;
00270             mthread_inlen = AV_RL32(encoded);
00271             mthread_inlen = FFMIN(mthread_inlen, len - 8);
00272             mthread_outlen = AV_RL32(encoded+4);
00273             mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
00274             ret = zlib_decomp(avctx, encoded + 8, mthread_inlen, 0, mthread_outlen);
00275             if (ret < 0) return ret;
00276             ret = zlib_decomp(avctx, encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
00277                               mthread_outlen, mthread_outlen);
00278             if (ret < 0) return ret;
00279         } else {
00280             int ret = zlib_decomp(avctx, encoded, len, 0, c->decomp_size);
00281             if (ret < 0) return ret;
00282         }
00283         encoded = c->decomp_buf;
00284         len = c->decomp_size;
00285         break;
00286 #endif
00287     default:
00288         av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
00289         return -1;
00290     }
00291 
00292 
00293     /* Apply PNG filter */
00294     if (avctx->codec_id == CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
00295         switch (c->imgtype) {
00296         case IMGTYPE_YUV111:
00297         case IMGTYPE_RGB24:
00298             for (row = 0; row < height; row++) {
00299                 pixel_ptr = row * width * 3;
00300                 yq = encoded[pixel_ptr++];
00301                 uqvq = AV_RL16(encoded+pixel_ptr);
00302                 pixel_ptr += 2;
00303                 for (col = 1; col < width; col++) {
00304                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
00305                     uqvq -= AV_RL16(encoded+pixel_ptr+1);
00306                     AV_WL16(encoded+pixel_ptr+1, uqvq);
00307                     pixel_ptr += 3;
00308                 }
00309             }
00310             break;
00311         case IMGTYPE_YUV422:
00312             for (row = 0; row < height; row++) {
00313                 pixel_ptr = row * width * 2;
00314                 yq = uq = vq =0;
00315                 for (col = 0; col < width/4; col++) {
00316                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
00317                     encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
00318                     encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
00319                     encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
00320                     encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
00321                     encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
00322                     encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
00323                     encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
00324                     pixel_ptr += 8;
00325                 }
00326             }
00327             break;
00328         case IMGTYPE_YUV411:
00329             for (row = 0; row < height; row++) {
00330                 pixel_ptr = row * width / 2 * 3;
00331                 yq = uq = vq =0;
00332                 for (col = 0; col < width/4; col++) {
00333                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
00334                     encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
00335                     encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
00336                     encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
00337                     encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
00338                     encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
00339                     pixel_ptr += 6;
00340                 }
00341             }
00342             break;
00343         case IMGTYPE_YUV211:
00344             for (row = 0; row < height; row++) {
00345                 pixel_ptr = row * width * 2;
00346                 yq = uq = vq =0;
00347                 for (col = 0; col < width/2; col++) {
00348                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
00349                     encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
00350                     encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
00351                     encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
00352                     pixel_ptr += 4;
00353                 }
00354             }
00355             break;
00356         case IMGTYPE_YUV420:
00357             for (row = 0; row < height/2; row++) {
00358                 pixel_ptr = row * width * 3;
00359                 yq = y1q = uq = vq =0;
00360                 for (col = 0; col < width/2; col++) {
00361                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
00362                     encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
00363                     encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
00364                     encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
00365                     encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
00366                     encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
00367                     pixel_ptr += 6;
00368                 }
00369             }
00370             break;
00371         default:
00372             av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
00373             return -1;
00374         }
00375     }
00376 
00377     /* Convert colorspace */
00378     y_out = c->pic.data[0] + (height - 1) * c->pic.linesize[0];
00379     u_out = c->pic.data[1] + (height - 1) * c->pic.linesize[1];
00380     v_out = c->pic.data[2] + (height - 1) * c->pic.linesize[2];
00381     switch (c->imgtype) {
00382     case IMGTYPE_YUV111:
00383         for (row = 0; row < height; row++) {
00384             for (col = 0; col < width; col++) {
00385                 y_out[col] = *encoded++;
00386                 u_out[col] = *encoded++ + 128;
00387                 v_out[col] = *encoded++ + 128;
00388             }
00389             y_out -= c->pic.linesize[0];
00390             u_out -= c->pic.linesize[1];
00391             v_out -= c->pic.linesize[2];
00392         }
00393         break;
00394     case IMGTYPE_YUV422:
00395         for (row = 0; row < height; row++) {
00396             for (col = 0; col < width - 3; col += 4) {
00397                 memcpy(y_out + col, encoded, 4);
00398                 encoded += 4;
00399                 u_out[ col >> 1     ] = *encoded++ + 128;
00400                 u_out[(col >> 1) + 1] = *encoded++ + 128;
00401                 v_out[ col >> 1     ] = *encoded++ + 128;
00402                 v_out[(col >> 1) + 1] = *encoded++ + 128;
00403             }
00404             y_out -= c->pic.linesize[0];
00405             u_out -= c->pic.linesize[1];
00406             v_out -= c->pic.linesize[2];
00407         }
00408         break;
00409     case IMGTYPE_RGB24:
00410         for (row = height - 1; row >= 0; row--) {
00411             pixel_ptr = row * c->pic.linesize[0];
00412             memcpy(outptr + pixel_ptr, encoded, 3 * width);
00413             encoded += 3 * width;
00414         }
00415         break;
00416     case IMGTYPE_YUV411:
00417         for (row = 0; row < height; row++) {
00418             for (col = 0; col < width - 3; col += 4) {
00419                 memcpy(y_out + col, encoded, 4);
00420                 encoded += 4;
00421                 u_out[col >> 2] = *encoded++ + 128;
00422                 v_out[col >> 2] = *encoded++ + 128;
00423             }
00424             y_out -= c->pic.linesize[0];
00425             u_out -= c->pic.linesize[1];
00426             v_out -= c->pic.linesize[2];
00427         }
00428         break;
00429     case IMGTYPE_YUV211:
00430         for (row = 0; row < height; row++) {
00431             for (col = 0; col < width - 1; col += 2) {
00432                 memcpy(y_out + col, encoded, 2);
00433                 encoded += 2;
00434                 u_out[col >> 1] = *encoded++ + 128;
00435                 v_out[col >> 1] = *encoded++ + 128;
00436             }
00437             y_out -= c->pic.linesize[0];
00438             u_out -= c->pic.linesize[1];
00439             v_out -= c->pic.linesize[2];
00440         }
00441         break;
00442     case IMGTYPE_YUV420:
00443         u_out = c->pic.data[1] + ((height >> 1) - 1) * c->pic.linesize[1];
00444         v_out = c->pic.data[2] + ((height >> 1) - 1) * c->pic.linesize[2];
00445         for (row = 0; row < height - 1; row += 2) {
00446             for (col = 0; col < width - 1; col += 2) {
00447                 memcpy(y_out + col, encoded, 2);
00448                 encoded += 2;
00449                 memcpy(y_out + col - c->pic.linesize[0], encoded, 2);
00450                 encoded += 2;
00451                 u_out[col >> 1] = *encoded++ + 128;
00452                 v_out[col >> 1] = *encoded++ + 128;
00453             }
00454             y_out -= c->pic.linesize[0] << 1;
00455             u_out -= c->pic.linesize[1];
00456             v_out -= c->pic.linesize[2];
00457         }
00458         break;
00459     default:
00460         av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
00461         return -1;
00462     }
00463 
00464     *data_size = sizeof(AVFrame);
00465     *(AVFrame*)data = c->pic;
00466 
00467     /* always report that the buffer was completely consumed */
00468     return buf_size;
00469 }
00470 
00471 /*
00472  *
00473  * Init lcl decoder
00474  *
00475  */
00476 static av_cold int decode_init(AVCodecContext *avctx)
00477 {
00478     LclDecContext * const c = avctx->priv_data;
00479     unsigned int basesize = avctx->width * avctx->height;
00480     unsigned int max_basesize = FFALIGN(avctx->width, 4) * FFALIGN(avctx->height, 4) + AV_LZO_OUTPUT_PADDING;
00481     unsigned int max_decomp_size;
00482 
00483     avcodec_get_frame_defaults(&c->pic);
00484     if (avctx->extradata_size < 8) {
00485         av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
00486         return AVERROR_INVALIDDATA;
00487     }
00488 
00489     /* Check codec type */
00490     if ((avctx->codec_id == CODEC_ID_MSZH  && avctx->extradata[7] != CODEC_MSZH) ||
00491         (avctx->codec_id == CODEC_ID_ZLIB  && avctx->extradata[7] != CODEC_ZLIB)) {
00492         av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
00493     }
00494 
00495     /* Detect image type */
00496     switch (c->imgtype = avctx->extradata[4]) {
00497     case IMGTYPE_YUV111:
00498         c->decomp_size = basesize * 3;
00499         max_decomp_size = max_basesize * 3;
00500         avctx->pix_fmt = PIX_FMT_YUV444P;
00501         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
00502         break;
00503     case IMGTYPE_YUV422:
00504         c->decomp_size = basesize * 2;
00505         max_decomp_size = max_basesize * 2;
00506         avctx->pix_fmt = PIX_FMT_YUV422P;
00507         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
00508         break;
00509     case IMGTYPE_RGB24:
00510         c->decomp_size = basesize * 3;
00511         max_decomp_size = max_basesize * 3;
00512         avctx->pix_fmt = PIX_FMT_BGR24;
00513         av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
00514         break;
00515     case IMGTYPE_YUV411:
00516         c->decomp_size = basesize / 2 * 3;
00517         max_decomp_size = max_basesize / 2 * 3;
00518         avctx->pix_fmt = PIX_FMT_YUV411P;
00519         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
00520         break;
00521     case IMGTYPE_YUV211:
00522         c->decomp_size = basesize * 2;
00523         max_decomp_size = max_basesize * 2;
00524         avctx->pix_fmt = PIX_FMT_YUV422P;
00525         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
00526         break;
00527     case IMGTYPE_YUV420:
00528         c->decomp_size = basesize / 2 * 3;
00529         max_decomp_size = max_basesize / 2 * 3;
00530         avctx->pix_fmt = PIX_FMT_YUV420P;
00531         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
00532         break;
00533     default:
00534         av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
00535         return AVERROR_INVALIDDATA;
00536     }
00537 
00538     /* Detect compression method */
00539     c->compression = (int8_t)avctx->extradata[5];
00540     switch (avctx->codec_id) {
00541     case CODEC_ID_MSZH:
00542         switch (c->compression) {
00543         case COMP_MSZH:
00544             av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
00545             break;
00546         case COMP_MSZH_NOCOMP:
00547             c->decomp_size = 0;
00548             av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
00549             break;
00550         default:
00551             av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
00552             return AVERROR_INVALIDDATA;
00553         }
00554         break;
00555 #if CONFIG_ZLIB_DECODER
00556     case CODEC_ID_ZLIB:
00557         switch (c->compression) {
00558         case COMP_ZLIB_HISPEED:
00559             av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
00560             break;
00561         case COMP_ZLIB_HICOMP:
00562             av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
00563             break;
00564         case COMP_ZLIB_NORMAL:
00565             av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
00566             break;
00567         default:
00568             if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
00569                 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
00570                 return AVERROR_INVALIDDATA;
00571             }
00572             av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
00573         }
00574         break;
00575 #endif
00576     default:
00577         av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
00578         return AVERROR_INVALIDDATA;
00579     }
00580 
00581     /* Allocate decompression buffer */
00582     if (c->decomp_size) {
00583         if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
00584             av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
00585             return AVERROR(ENOMEM);
00586         }
00587     }
00588 
00589     /* Detect flags */
00590     c->flags = avctx->extradata[6];
00591     if (c->flags & FLAG_MULTITHREAD)
00592         av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
00593     if (c->flags & FLAG_NULLFRAME)
00594         av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
00595     if (avctx->codec_id == CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
00596         av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
00597     if (c->flags & FLAGMASK_UNUSED)
00598         av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
00599 
00600     /* If needed init zlib */
00601 #if CONFIG_ZLIB_DECODER
00602     if (avctx->codec_id == CODEC_ID_ZLIB) {
00603         int zret;
00604         c->zstream.zalloc = Z_NULL;
00605         c->zstream.zfree = Z_NULL;
00606         c->zstream.opaque = Z_NULL;
00607         zret = inflateInit(&c->zstream);
00608         if (zret != Z_OK) {
00609             av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
00610             av_freep(&c->decomp_buf);
00611             return AVERROR_UNKNOWN;
00612         }
00613     }
00614 #endif
00615 
00616     return 0;
00617 }
00618 
00619 /*
00620  *
00621  * Uninit lcl decoder
00622  *
00623  */
00624 static av_cold int decode_end(AVCodecContext *avctx)
00625 {
00626     LclDecContext * const c = avctx->priv_data;
00627 
00628     av_freep(&c->decomp_buf);
00629     if (c->pic.data[0])
00630         avctx->release_buffer(avctx, &c->pic);
00631 #if CONFIG_ZLIB_DECODER
00632     if (avctx->codec_id == CODEC_ID_ZLIB)
00633         inflateEnd(&c->zstream);
00634 #endif
00635 
00636     return 0;
00637 }
00638 
00639 #if CONFIG_MSZH_DECODER
00640 AVCodec ff_mszh_decoder = {
00641     .name           = "mszh",
00642     .type           = AVMEDIA_TYPE_VIDEO,
00643     .id             = CODEC_ID_MSZH,
00644     .priv_data_size = sizeof(LclDecContext),
00645     .init           = decode_init,
00646     .close          = decode_end,
00647     .decode         = decode_frame,
00648     .capabilities   = CODEC_CAP_DR1,
00649     .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
00650 };
00651 #endif
00652 
00653 #if CONFIG_ZLIB_DECODER
00654 AVCodec ff_zlib_decoder = {
00655     .name           = "zlib",
00656     .type           = AVMEDIA_TYPE_VIDEO,
00657     .id             = CODEC_ID_ZLIB,
00658     .priv_data_size = sizeof(LclDecContext),
00659     .init           = decode_init,
00660     .close          = decode_end,
00661     .decode         = decode_frame,
00662     .capabilities   = CODEC_CAP_DR1,
00663     .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
00664 };
00665 #endif