]> git.sesse.net Git - ffmpeg/blob - libavcodec/lcldec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / lcldec.c
1 /*
2  * LCL (LossLess Codec Library) Codec
3  * Copyright (c) 2002-2004 Roberto Togni
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
22 /**
23  * @file
24  * LCL (LossLess Codec Library) Video Codec
25  * Decoder for MSZH and ZLIB codecs
26  * Experimental encoder for ZLIB RGB24
27  *
28  * Fourcc: MSZH, ZLIB
29  *
30  * Original Win32 dll:
31  * Ver2.23 By Kenji Oshima 2000.09.20
32  * avimszh.dll, avizlib.dll
33  *
34  * A description of the decoding algorithm can be found here:
35  *   http://www.pcisys.net/~melanson/codecs
36  *
37  * Supports: BGR24 (RGB 24bpp)
38  *
39  */
40
41 #include <stdio.h>
42 #include <stdlib.h>
43
44 #include "libavutil/mem.h"
45 #include "avcodec.h"
46 #include "bytestream.h"
47 #include "lcl.h"
48
49 #if CONFIG_ZLIB_DECODER
50 #include <zlib.h>
51 #endif
52
53 /*
54  * Decoder context
55  */
56 typedef struct LclDecContext {
57     AVFrame pic;
58
59     // Image type
60     int imgtype;
61     // Compression type
62     int compression;
63     // Flags
64     int flags;
65     // Decompressed data size
66     unsigned int decomp_size;
67     // Decompression buffer
68     unsigned char* decomp_buf;
69 #if CONFIG_ZLIB_DECODER
70     z_stream zstream;
71 #endif
72 } LclDecContext;
73
74
75 /**
76  * @param srcptr compressed source buffer, must be padded with at least 5 extra bytes
77  * @param destptr must be padded sufficiently for av_memcpy_backptr
78  */
79 static unsigned int mszh_decomp(const unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
80 {
81     unsigned char *destptr_bak = destptr;
82     unsigned char *destptr_end = destptr + destsize;
83     const unsigned char *srcptr_end = srcptr + srclen;
84     unsigned mask = *srcptr++;
85     unsigned maskbit = 0x80;
86
87     while (srcptr < srcptr_end && destptr < destptr_end) {
88         if (!(mask & maskbit)) {
89             memcpy(destptr, srcptr, 4);
90             destptr += 4;
91             srcptr += 4;
92         } else {
93             unsigned ofs = bytestream_get_le16(&srcptr);
94             unsigned cnt = (ofs >> 11) + 1;
95             ofs &= 0x7ff;
96             ofs = FFMIN(ofs, destptr - destptr_bak);
97             cnt *= 4;
98             cnt = FFMIN(cnt, destptr_end - destptr);
99             if (ofs) {
100                 av_memcpy_backptr(destptr, ofs, cnt);
101             } else {
102                 // Not known what the correct behaviour is, but
103                 // this at least avoids uninitialized data.
104                 memset(destptr, 0, cnt);
105             }
106             destptr += cnt;
107         }
108         maskbit >>= 1;
109         if (!maskbit) {
110             mask = *srcptr++;
111             while (!mask) {
112                 if (destptr_end - destptr < 32 || srcptr_end - srcptr < 32) break;
113                 memcpy(destptr, srcptr, 32);
114                 destptr += 32;
115                 srcptr += 32;
116                 mask = *srcptr++;
117             }
118             maskbit = 0x80;
119         }
120     }
121
122     return destptr - destptr_bak;
123 }
124
125
126 #if CONFIG_ZLIB_DECODER
127 /**
128  * @brief decompress a zlib-compressed data block into decomp_buf
129  * @param src compressed input buffer
130  * @param src_len data length in input buffer
131  * @param offset offset in decomp_buf
132  * @param expected expected decompressed length
133  */
134 static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected)
135 {
136     LclDecContext *c = avctx->priv_data;
137     int zret = inflateReset(&c->zstream);
138     if (zret != Z_OK) {
139         av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
140         return -1;
141     }
142     c->zstream.next_in = src;
143     c->zstream.avail_in = src_len;
144     c->zstream.next_out = c->decomp_buf + offset;
145     c->zstream.avail_out = c->decomp_size - offset;
146     zret = inflate(&c->zstream, Z_FINISH);
147     if (zret != Z_OK && zret != Z_STREAM_END) {
148         av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
149         return -1;
150     }
151     if (expected != (unsigned int)c->zstream.total_out) {
152         av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
153                expected, c->zstream.total_out);
154         return -1;
155     }
156     return c->zstream.total_out;
157 }
158 #endif
159
160
161 /*
162  *
163  * Decode a frame
164  *
165  */
166 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
167 {
168     const uint8_t *buf = avpkt->data;
169     int buf_size = avpkt->size;
170     LclDecContext * const c = avctx->priv_data;
171     unsigned char *encoded = (unsigned char *)buf;
172     unsigned int pixel_ptr;
173     int row, col;
174     unsigned char *outptr;
175     uint8_t *y_out, *u_out, *v_out;
176     unsigned int width = avctx->width; // Real image width
177     unsigned int height = avctx->height; // Real image height
178     unsigned int mszh_dlen;
179     unsigned char yq, y1q, uq, vq;
180     int uqvq;
181     unsigned int mthread_inlen, mthread_outlen;
182     unsigned int len = buf_size;
183
184     if(c->pic.data[0])
185         avctx->release_buffer(avctx, &c->pic);
186
187     c->pic.reference = 0;
188     c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
189     if(avctx->get_buffer(avctx, &c->pic) < 0){
190         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
191         return -1;
192     }
193
194     outptr = c->pic.data[0]; // Output image pointer
195
196     /* Decompress frame */
197     switch (avctx->codec_id) {
198     case AV_CODEC_ID_MSZH:
199         switch (c->compression) {
200         case COMP_MSZH:
201             if (c->imgtype == IMGTYPE_RGB24 && len == width * height * 3) {
202                 ;
203             } else if (c->flags & FLAG_MULTITHREAD) {
204                 mthread_inlen = AV_RL32(encoded);
205                 mthread_inlen = FFMIN(mthread_inlen, len - 8);
206                 mthread_outlen = AV_RL32(encoded+4);
207                 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
208                 mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
209                 if (mthread_outlen != mszh_dlen) {
210                     av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
211                            mthread_outlen, mszh_dlen);
212                     return -1;
213                 }
214                 mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
215                                         c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
216                 if (mthread_outlen != mszh_dlen) {
217                     av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
218                            mthread_outlen, mszh_dlen);
219                     return -1;
220                 }
221                 encoded = c->decomp_buf;
222                 len = c->decomp_size;
223             } else {
224                 mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
225                 if (c->decomp_size != mszh_dlen) {
226                     av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
227                            c->decomp_size, mszh_dlen);
228                     return -1;
229                 }
230                 encoded = c->decomp_buf;
231                 len = mszh_dlen;
232             }
233             break;
234         case COMP_MSZH_NOCOMP: {
235             int bppx2;
236             switch (c->imgtype) {
237             case IMGTYPE_YUV111:
238             case IMGTYPE_RGB24:
239                 bppx2 = 6;
240                 break;
241             case IMGTYPE_YUV422:
242             case IMGTYPE_YUV211:
243                 bppx2 = 4;
244                 break;
245             case IMGTYPE_YUV411:
246             case IMGTYPE_YUV420:
247                 bppx2 = 3;
248                 break;
249             default:
250                 bppx2 = 0; // will error out below
251                 break;
252             }
253             if (len < ((width * height * bppx2) >> 1))
254                 return AVERROR_INVALIDDATA;
255             break;
256         }
257         default:
258             av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
259             return -1;
260         }
261         break;
262 #if CONFIG_ZLIB_DECODER
263     case AV_CODEC_ID_ZLIB:
264         /* Using the original dll with normal compression (-1) and RGB format
265          * gives a file with ZLIB fourcc, but frame is really uncompressed.
266          * To be sure that's true check also frame size */
267         if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
268             len == width * height * 3) {
269             if (c->flags & FLAG_PNGFILTER) {
270                 memcpy(c->decomp_buf, encoded, len);
271                 encoded = c->decomp_buf;
272             } else {
273                 break;
274             }
275         } else if (c->flags & FLAG_MULTITHREAD) {
276             int ret;
277             mthread_inlen = AV_RL32(encoded);
278             mthread_inlen = FFMIN(mthread_inlen, len - 8);
279             mthread_outlen = AV_RL32(encoded+4);
280             mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
281             ret = zlib_decomp(avctx, encoded + 8, mthread_inlen, 0, mthread_outlen);
282             if (ret < 0) return ret;
283             ret = zlib_decomp(avctx, encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
284                               mthread_outlen, mthread_outlen);
285             if (ret < 0) return ret;
286         } else {
287             int ret = zlib_decomp(avctx, encoded, len, 0, c->decomp_size);
288             if (ret < 0) return ret;
289         }
290         encoded = c->decomp_buf;
291         len = c->decomp_size;
292         break;
293 #endif
294     default:
295         av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
296         return -1;
297     }
298
299
300     /* Apply PNG filter */
301     if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
302         switch (c->imgtype) {
303         case IMGTYPE_YUV111:
304         case IMGTYPE_RGB24:
305             for (row = 0; row < height; row++) {
306                 pixel_ptr = row * width * 3;
307                 yq = encoded[pixel_ptr++];
308                 uqvq = AV_RL16(encoded+pixel_ptr);
309                 pixel_ptr += 2;
310                 for (col = 1; col < width; col++) {
311                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
312                     uqvq -= AV_RL16(encoded+pixel_ptr+1);
313                     AV_WL16(encoded+pixel_ptr+1, uqvq);
314                     pixel_ptr += 3;
315                 }
316             }
317             break;
318         case IMGTYPE_YUV422:
319             for (row = 0; row < height; row++) {
320                 pixel_ptr = row * width * 2;
321                 yq = uq = vq =0;
322                 for (col = 0; col < width/4; col++) {
323                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
324                     encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
325                     encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
326                     encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
327                     encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
328                     encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
329                     encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
330                     encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
331                     pixel_ptr += 8;
332                 }
333             }
334             break;
335         case IMGTYPE_YUV411:
336             for (row = 0; row < height; row++) {
337                 pixel_ptr = row * width / 2 * 3;
338                 yq = uq = vq =0;
339                 for (col = 0; col < width/4; col++) {
340                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
341                     encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
342                     encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
343                     encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
344                     encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
345                     encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
346                     pixel_ptr += 6;
347                 }
348             }
349             break;
350         case IMGTYPE_YUV211:
351             for (row = 0; row < height; row++) {
352                 pixel_ptr = row * width * 2;
353                 yq = uq = vq =0;
354                 for (col = 0; col < width/2; col++) {
355                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
356                     encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
357                     encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
358                     encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
359                     pixel_ptr += 4;
360                 }
361             }
362             break;
363         case IMGTYPE_YUV420:
364             for (row = 0; row < height/2; row++) {
365                 pixel_ptr = row * width * 3;
366                 yq = y1q = uq = vq =0;
367                 for (col = 0; col < width/2; col++) {
368                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
369                     encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
370                     encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
371                     encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
372                     encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
373                     encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
374                     pixel_ptr += 6;
375                 }
376             }
377             break;
378         default:
379             av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
380             return -1;
381         }
382     }
383
384     /* Convert colorspace */
385     y_out = c->pic.data[0] + (height - 1) * c->pic.linesize[0];
386     u_out = c->pic.data[1] + (height - 1) * c->pic.linesize[1];
387     v_out = c->pic.data[2] + (height - 1) * c->pic.linesize[2];
388     switch (c->imgtype) {
389     case IMGTYPE_YUV111:
390         for (row = 0; row < height; row++) {
391             for (col = 0; col < width; col++) {
392                 y_out[col] = *encoded++;
393                 u_out[col] = *encoded++ + 128;
394                 v_out[col] = *encoded++ + 128;
395             }
396             y_out -= c->pic.linesize[0];
397             u_out -= c->pic.linesize[1];
398             v_out -= c->pic.linesize[2];
399         }
400         break;
401     case IMGTYPE_YUV422:
402         for (row = 0; row < height; row++) {
403             for (col = 0; col < width - 3; col += 4) {
404                 memcpy(y_out + col, encoded, 4);
405                 encoded += 4;
406                 u_out[ col >> 1     ] = *encoded++ + 128;
407                 u_out[(col >> 1) + 1] = *encoded++ + 128;
408                 v_out[ col >> 1     ] = *encoded++ + 128;
409                 v_out[(col >> 1) + 1] = *encoded++ + 128;
410             }
411             y_out -= c->pic.linesize[0];
412             u_out -= c->pic.linesize[1];
413             v_out -= c->pic.linesize[2];
414         }
415         break;
416     case IMGTYPE_RGB24:
417         for (row = height - 1; row >= 0; row--) {
418             pixel_ptr = row * c->pic.linesize[0];
419             memcpy(outptr + pixel_ptr, encoded, 3 * width);
420             encoded += 3 * width;
421         }
422         break;
423     case IMGTYPE_YUV411:
424         for (row = 0; row < height; row++) {
425             for (col = 0; col < width - 3; col += 4) {
426                 memcpy(y_out + col, encoded, 4);
427                 encoded += 4;
428                 u_out[col >> 2] = *encoded++ + 128;
429                 v_out[col >> 2] = *encoded++ + 128;
430             }
431             y_out -= c->pic.linesize[0];
432             u_out -= c->pic.linesize[1];
433             v_out -= c->pic.linesize[2];
434         }
435         break;
436     case IMGTYPE_YUV211:
437         for (row = 0; row < height; row++) {
438             for (col = 0; col < width - 1; col += 2) {
439                 memcpy(y_out + col, encoded, 2);
440                 encoded += 2;
441                 u_out[col >> 1] = *encoded++ + 128;
442                 v_out[col >> 1] = *encoded++ + 128;
443             }
444             y_out -= c->pic.linesize[0];
445             u_out -= c->pic.linesize[1];
446             v_out -= c->pic.linesize[2];
447         }
448         break;
449     case IMGTYPE_YUV420:
450         u_out = c->pic.data[1] + ((height >> 1) - 1) * c->pic.linesize[1];
451         v_out = c->pic.data[2] + ((height >> 1) - 1) * c->pic.linesize[2];
452         for (row = 0; row < height - 1; row += 2) {
453             for (col = 0; col < width - 1; col += 2) {
454                 memcpy(y_out + col, encoded, 2);
455                 encoded += 2;
456                 memcpy(y_out + col - c->pic.linesize[0], encoded, 2);
457                 encoded += 2;
458                 u_out[col >> 1] = *encoded++ + 128;
459                 v_out[col >> 1] = *encoded++ + 128;
460             }
461             y_out -= c->pic.linesize[0] << 1;
462             u_out -= c->pic.linesize[1];
463             v_out -= c->pic.linesize[2];
464         }
465         break;
466     default:
467         av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
468         return -1;
469     }
470
471     *data_size = sizeof(AVFrame);
472     *(AVFrame*)data = c->pic;
473
474     /* always report that the buffer was completely consumed */
475     return buf_size;
476 }
477
478 /*
479  *
480  * Init lcl decoder
481  *
482  */
483 static av_cold int decode_init(AVCodecContext *avctx)
484 {
485     LclDecContext * const c = avctx->priv_data;
486     unsigned int basesize = avctx->width * avctx->height;
487     unsigned int max_basesize = FFALIGN(avctx->width,  4) *
488                                 FFALIGN(avctx->height, 4);
489     unsigned int max_decomp_size;
490
491     avcodec_get_frame_defaults(&c->pic);
492     if (avctx->extradata_size < 8) {
493         av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
494         return AVERROR_INVALIDDATA;
495     }
496
497     /* Check codec type */
498     if ((avctx->codec_id == AV_CODEC_ID_MSZH  && avctx->extradata[7] != CODEC_MSZH) ||
499         (avctx->codec_id == AV_CODEC_ID_ZLIB  && avctx->extradata[7] != CODEC_ZLIB)) {
500         av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
501     }
502
503     /* Detect image type */
504     switch (c->imgtype = avctx->extradata[4]) {
505     case IMGTYPE_YUV111:
506         c->decomp_size = basesize * 3;
507         max_decomp_size = max_basesize * 3;
508         avctx->pix_fmt = AV_PIX_FMT_YUV444P;
509         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
510         break;
511     case IMGTYPE_YUV422:
512         c->decomp_size = basesize * 2;
513         max_decomp_size = max_basesize * 2;
514         avctx->pix_fmt = AV_PIX_FMT_YUV422P;
515         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
516         break;
517     case IMGTYPE_RGB24:
518         c->decomp_size = basesize * 3;
519         max_decomp_size = max_basesize * 3;
520         avctx->pix_fmt = AV_PIX_FMT_BGR24;
521         av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
522         break;
523     case IMGTYPE_YUV411:
524         c->decomp_size = basesize / 2 * 3;
525         max_decomp_size = max_basesize / 2 * 3;
526         avctx->pix_fmt = AV_PIX_FMT_YUV411P;
527         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
528         break;
529     case IMGTYPE_YUV211:
530         c->decomp_size = basesize * 2;
531         max_decomp_size = max_basesize * 2;
532         avctx->pix_fmt = AV_PIX_FMT_YUV422P;
533         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
534         break;
535     case IMGTYPE_YUV420:
536         c->decomp_size = basesize / 2 * 3;
537         max_decomp_size = max_basesize / 2 * 3;
538         avctx->pix_fmt = AV_PIX_FMT_YUV420P;
539         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
540         break;
541     default:
542         av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
543         return AVERROR_INVALIDDATA;
544     }
545
546     /* Detect compression method */
547     c->compression = (int8_t)avctx->extradata[5];
548     switch (avctx->codec_id) {
549     case AV_CODEC_ID_MSZH:
550         switch (c->compression) {
551         case COMP_MSZH:
552             av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
553             break;
554         case COMP_MSZH_NOCOMP:
555             c->decomp_size = 0;
556             av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
557             break;
558         default:
559             av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
560             return AVERROR_INVALIDDATA;
561         }
562         break;
563 #if CONFIG_ZLIB_DECODER
564     case AV_CODEC_ID_ZLIB:
565         switch (c->compression) {
566         case COMP_ZLIB_HISPEED:
567             av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
568             break;
569         case COMP_ZLIB_HICOMP:
570             av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
571             break;
572         case COMP_ZLIB_NORMAL:
573             av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
574             break;
575         default:
576             if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
577                 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
578                 return AVERROR_INVALIDDATA;
579             }
580             av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
581         }
582         break;
583 #endif
584     default:
585         av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
586         return AVERROR_INVALIDDATA;
587     }
588
589     /* Allocate decompression buffer */
590     if (c->decomp_size) {
591         if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
592             av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
593             return AVERROR(ENOMEM);
594         }
595     }
596
597     /* Detect flags */
598     c->flags = avctx->extradata[6];
599     if (c->flags & FLAG_MULTITHREAD)
600         av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
601     if (c->flags & FLAG_NULLFRAME)
602         av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
603     if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
604         av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
605     if (c->flags & FLAGMASK_UNUSED)
606         av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
607
608     /* If needed init zlib */
609 #if CONFIG_ZLIB_DECODER
610     if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
611         int zret;
612         c->zstream.zalloc = Z_NULL;
613         c->zstream.zfree = Z_NULL;
614         c->zstream.opaque = Z_NULL;
615         zret = inflateInit(&c->zstream);
616         if (zret != Z_OK) {
617             av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
618             av_freep(&c->decomp_buf);
619             return AVERROR_UNKNOWN;
620         }
621     }
622 #endif
623
624     return 0;
625 }
626
627 /*
628  *
629  * Uninit lcl decoder
630  *
631  */
632 static av_cold int decode_end(AVCodecContext *avctx)
633 {
634     LclDecContext * const c = avctx->priv_data;
635
636     av_freep(&c->decomp_buf);
637     if (c->pic.data[0])
638         avctx->release_buffer(avctx, &c->pic);
639 #if CONFIG_ZLIB_DECODER
640     if (avctx->codec_id == AV_CODEC_ID_ZLIB)
641         inflateEnd(&c->zstream);
642 #endif
643
644     return 0;
645 }
646
647 #if CONFIG_MSZH_DECODER
648 AVCodec ff_mszh_decoder = {
649     .name           = "mszh",
650     .type           = AVMEDIA_TYPE_VIDEO,
651     .id             = AV_CODEC_ID_MSZH,
652     .priv_data_size = sizeof(LclDecContext),
653     .init           = decode_init,
654     .close          = decode_end,
655     .decode         = decode_frame,
656     .capabilities   = CODEC_CAP_DR1,
657     .long_name      = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
658 };
659 #endif
660
661 #if CONFIG_ZLIB_DECODER
662 AVCodec ff_zlib_decoder = {
663     .name           = "zlib",
664     .type           = AVMEDIA_TYPE_VIDEO,
665     .id             = AV_CODEC_ID_ZLIB,
666     .priv_data_size = sizeof(LclDecContext),
667     .init           = decode_init,
668     .close          = decode_end,
669     .decode         = decode_frame,
670     .capabilities   = CODEC_CAP_DR1,
671     .long_name      = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
672 };
673 #endif