]> git.sesse.net Git - ffmpeg/blob - libavcodec/lcldec.c
Merge commit 'd395895cdb2ac8c95bd488549e7f893bd4dcc248'
[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 "libavutil/pixdesc.h"
46 #include "avcodec.h"
47 #include "bytestream.h"
48 #include "internal.h"
49 #include "lcl.h"
50
51 #if CONFIG_ZLIB_DECODER
52 #include <zlib.h>
53 #endif
54
55 /*
56  * Decoder context
57  */
58 typedef struct LclDecContext {
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 AVERROR_UNKNOWN;
141     }
142     c->zstream.next_in = (uint8_t *)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 AVERROR_UNKNOWN;
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 AVERROR_UNKNOWN;
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 *got_frame, AVPacket *avpkt)
167 {
168     AVFrame *frame = data;
169     const uint8_t *buf = avpkt->data;
170     int buf_size = avpkt->size;
171     LclDecContext * const c = avctx->priv_data;
172     unsigned int pixel_ptr;
173     int row, col;
174     unsigned char *encoded, *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, ret;
181     unsigned int mthread_inlen, mthread_outlen;
182     unsigned int len = buf_size;
183
184     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
185         return ret;
186
187     outptr = frame->data[0]; // Output image pointer
188
189     /* Decompress frame */
190     switch (avctx->codec_id) {
191     case AV_CODEC_ID_MSZH:
192         switch (c->compression) {
193         case COMP_MSZH:
194             if (c->imgtype == IMGTYPE_RGB24 && len == width * height * 3) {
195                 ;
196             } else if (c->flags & FLAG_MULTITHREAD) {
197                 mthread_inlen = AV_RL32(buf);
198                 if (len < 8) {
199                     av_log(avctx, AV_LOG_ERROR, "len %d is too small\n", len);
200                     return AVERROR_INVALIDDATA;
201                 }
202                 mthread_inlen = FFMIN(mthread_inlen, len - 8);
203                 mthread_outlen = AV_RL32(buf + 4);
204                 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
205                 mszh_dlen = mszh_decomp(buf + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
206                 if (mthread_outlen != mszh_dlen) {
207                     av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
208                            mthread_outlen, mszh_dlen);
209                     return AVERROR_INVALIDDATA;
210                 }
211                 mszh_dlen = mszh_decomp(buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
212                                         c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
213                 if (mthread_outlen != mszh_dlen) {
214                     av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
215                            mthread_outlen, mszh_dlen);
216                     return AVERROR_INVALIDDATA;
217                 }
218                 encoded = c->decomp_buf;
219                 len = c->decomp_size;
220             } else {
221                 mszh_dlen = mszh_decomp(buf, len, c->decomp_buf, c->decomp_size);
222                 if (c->decomp_size != mszh_dlen) {
223                     av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
224                            c->decomp_size, mszh_dlen);
225                     return AVERROR_INVALIDDATA;
226                 }
227                 encoded = c->decomp_buf;
228                 len = mszh_dlen;
229             }
230             break;
231         case COMP_MSZH_NOCOMP: {
232             int bppx2;
233             switch (c->imgtype) {
234             case IMGTYPE_YUV111:
235             case IMGTYPE_RGB24:
236                 bppx2 = 6;
237                 break;
238             case IMGTYPE_YUV422:
239             case IMGTYPE_YUV211:
240                 bppx2 = 4;
241                 break;
242             case IMGTYPE_YUV411:
243             case IMGTYPE_YUV420:
244                 bppx2 = 3;
245                 break;
246             default:
247                 bppx2 = 0; // will error out below
248                 break;
249             }
250             if (len < ((width * height * bppx2) >> 1))
251                 return AVERROR_INVALIDDATA;
252             break;
253         }
254         default:
255             av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
256             return AVERROR_INVALIDDATA;
257         }
258         break;
259 #if CONFIG_ZLIB_DECODER
260     case AV_CODEC_ID_ZLIB:
261         /* Using the original dll with normal compression (-1) and RGB format
262          * gives a file with ZLIB fourcc, but frame is really uncompressed.
263          * To be sure that's true check also frame size */
264         if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
265             len == width * height * 3) {
266             if (c->flags & FLAG_PNGFILTER) {
267                 memcpy(c->decomp_buf, buf, len);
268                 encoded = c->decomp_buf;
269             } else {
270                 break;
271             }
272         } else if (c->flags & FLAG_MULTITHREAD) {
273             mthread_inlen = AV_RL32(buf);
274             mthread_inlen = FFMIN(mthread_inlen, len - 8);
275             mthread_outlen = AV_RL32(buf + 4);
276             mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
277             ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen);
278             if (ret < 0) return ret;
279             ret = zlib_decomp(avctx, buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
280                               mthread_outlen, mthread_outlen);
281             if (ret < 0) return ret;
282         } else {
283             int ret = zlib_decomp(avctx, buf, len, 0, c->decomp_size);
284             if (ret < 0) return ret;
285         }
286         encoded = c->decomp_buf;
287         len = c->decomp_size;
288         break;
289 #endif
290     default:
291         av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
292         return AVERROR_INVALIDDATA;
293     }
294
295
296     /* Apply PNG filter */
297     if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
298         switch (c->imgtype) {
299         case IMGTYPE_YUV111:
300         case IMGTYPE_RGB24:
301             for (row = 0; row < height; row++) {
302                 pixel_ptr = row * width * 3;
303                 yq = encoded[pixel_ptr++];
304                 uqvq = AV_RL16(encoded+pixel_ptr);
305                 pixel_ptr += 2;
306                 for (col = 1; col < width; col++) {
307                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
308                     uqvq -= AV_RL16(encoded+pixel_ptr+1);
309                     AV_WL16(encoded+pixel_ptr+1, uqvq);
310                     pixel_ptr += 3;
311                 }
312             }
313             break;
314         case IMGTYPE_YUV422:
315             for (row = 0; row < height; row++) {
316                 pixel_ptr = row * width * 2;
317                 yq = uq = vq =0;
318                 for (col = 0; col < width/4; col++) {
319                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
320                     encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
321                     encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
322                     encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
323                     encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
324                     encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
325                     encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
326                     encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
327                     pixel_ptr += 8;
328                 }
329             }
330             break;
331         case IMGTYPE_YUV411:
332             for (row = 0; row < height; row++) {
333                 pixel_ptr = row * width / 2 * 3;
334                 yq = uq = vq =0;
335                 for (col = 0; col < width/4; col++) {
336                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
337                     encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
338                     encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
339                     encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
340                     encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
341                     encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
342                     pixel_ptr += 6;
343                 }
344             }
345             break;
346         case IMGTYPE_YUV211:
347             for (row = 0; row < height; row++) {
348                 pixel_ptr = row * width * 2;
349                 yq = uq = vq =0;
350                 for (col = 0; col < width/2; col++) {
351                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
352                     encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
353                     encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
354                     encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
355                     pixel_ptr += 4;
356                 }
357             }
358             break;
359         case IMGTYPE_YUV420:
360             for (row = 0; row < height/2; row++) {
361                 pixel_ptr = row * width * 3;
362                 yq = y1q = uq = vq =0;
363                 for (col = 0; col < width/2; col++) {
364                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
365                     encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
366                     encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
367                     encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
368                     encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
369                     encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
370                     pixel_ptr += 6;
371                 }
372             }
373             break;
374         default:
375             av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
376             return AVERROR_INVALIDDATA;
377         }
378     }
379
380     /* Convert colorspace */
381     y_out = frame->data[0] + (height - 1) * frame->linesize[0];
382     u_out = frame->data[1] + (height - 1) * frame->linesize[1];
383     v_out = frame->data[2] + (height - 1) * frame->linesize[2];
384     switch (c->imgtype) {
385     case IMGTYPE_YUV111:
386         for (row = 0; row < height; row++) {
387             for (col = 0; col < width; col++) {
388                 y_out[col] = *encoded++;
389                 u_out[col] = *encoded++ + 128;
390                 v_out[col] = *encoded++ + 128;
391             }
392             y_out -= frame->linesize[0];
393             u_out -= frame->linesize[1];
394             v_out -= frame->linesize[2];
395         }
396         break;
397     case IMGTYPE_YUV422:
398         for (row = 0; row < height; row++) {
399             for (col = 0; col < width - 3; col += 4) {
400                 memcpy(y_out + col, encoded, 4);
401                 encoded += 4;
402                 u_out[ col >> 1     ] = *encoded++ + 128;
403                 u_out[(col >> 1) + 1] = *encoded++ + 128;
404                 v_out[ col >> 1     ] = *encoded++ + 128;
405                 v_out[(col >> 1) + 1] = *encoded++ + 128;
406             }
407             y_out -= frame->linesize[0];
408             u_out -= frame->linesize[1];
409             v_out -= frame->linesize[2];
410         }
411         break;
412     case IMGTYPE_RGB24:
413         for (row = height - 1; row >= 0; row--) {
414             pixel_ptr = row * frame->linesize[0];
415             memcpy(outptr + pixel_ptr, encoded, 3 * width);
416             encoded += 3 * width;
417         }
418         break;
419     case IMGTYPE_YUV411:
420         for (row = 0; row < height; row++) {
421             for (col = 0; col < width - 3; col += 4) {
422                 memcpy(y_out + col, encoded, 4);
423                 encoded += 4;
424                 u_out[col >> 2] = *encoded++ + 128;
425                 v_out[col >> 2] = *encoded++ + 128;
426             }
427             y_out -= frame->linesize[0];
428             u_out -= frame->linesize[1];
429             v_out -= frame->linesize[2];
430         }
431         break;
432     case IMGTYPE_YUV211:
433         for (row = 0; row < height; row++) {
434             for (col = 0; col < width - 1; col += 2) {
435                 memcpy(y_out + col, encoded, 2);
436                 encoded += 2;
437                 u_out[col >> 1] = *encoded++ + 128;
438                 v_out[col >> 1] = *encoded++ + 128;
439             }
440             y_out -= frame->linesize[0];
441             u_out -= frame->linesize[1];
442             v_out -= frame->linesize[2];
443         }
444         break;
445     case IMGTYPE_YUV420:
446         u_out = frame->data[1] + ((height >> 1) - 1) * frame->linesize[1];
447         v_out = frame->data[2] + ((height >> 1) - 1) * frame->linesize[2];
448         for (row = 0; row < height - 1; row += 2) {
449             for (col = 0; col < width - 1; col += 2) {
450                 memcpy(y_out + col, encoded, 2);
451                 encoded += 2;
452                 memcpy(y_out + col - frame->linesize[0], encoded, 2);
453                 encoded += 2;
454                 u_out[col >> 1] = *encoded++ + 128;
455                 v_out[col >> 1] = *encoded++ + 128;
456             }
457             y_out -= frame->linesize[0] << 1;
458             u_out -= frame->linesize[1];
459             v_out -= frame->linesize[2];
460         }
461         break;
462     default:
463         av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
464         return AVERROR_INVALIDDATA;
465     }
466
467     *got_frame = 1;
468
469     /* always report that the buffer was completely consumed */
470     return buf_size;
471 }
472
473 /*
474  *
475  * Init lcl decoder
476  *
477  */
478 static av_cold int decode_init(AVCodecContext *avctx)
479 {
480     LclDecContext * const c = avctx->priv_data;
481     unsigned int basesize = avctx->width * avctx->height;
482     unsigned int max_basesize = FFALIGN(avctx->width,  4) *
483                                 FFALIGN(avctx->height, 4);
484     unsigned int max_decomp_size;
485     int subsample_h, subsample_v;
486
487     if (avctx->extradata_size < 8) {
488         av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
489         return AVERROR_INVALIDDATA;
490     }
491
492     /* Check codec type */
493     if ((avctx->codec_id == AV_CODEC_ID_MSZH  && avctx->extradata[7] != CODEC_MSZH) ||
494         (avctx->codec_id == AV_CODEC_ID_ZLIB  && avctx->extradata[7] != CODEC_ZLIB)) {
495         av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
496     }
497
498     /* Detect image type */
499     switch (c->imgtype = avctx->extradata[4]) {
500     case IMGTYPE_YUV111:
501         c->decomp_size = basesize * 3;
502         max_decomp_size = max_basesize * 3;
503         avctx->pix_fmt = AV_PIX_FMT_YUV444P;
504         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
505         break;
506     case IMGTYPE_YUV422:
507         c->decomp_size = basesize * 2;
508         max_decomp_size = max_basesize * 2;
509         avctx->pix_fmt = AV_PIX_FMT_YUV422P;
510         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
511         if (avctx->width % 4) {
512             avpriv_request_sample(avctx, "Unsupported dimensions\n");
513             return AVERROR_INVALIDDATA;
514         }
515         break;
516     case IMGTYPE_RGB24:
517         c->decomp_size = basesize * 3;
518         max_decomp_size = max_basesize * 3;
519         avctx->pix_fmt = AV_PIX_FMT_BGR24;
520         av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
521         break;
522     case IMGTYPE_YUV411:
523         c->decomp_size = basesize / 2 * 3;
524         max_decomp_size = max_basesize / 2 * 3;
525         avctx->pix_fmt = AV_PIX_FMT_YUV411P;
526         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
527         break;
528     case IMGTYPE_YUV211:
529         c->decomp_size = basesize * 2;
530         max_decomp_size = max_basesize * 2;
531         avctx->pix_fmt = AV_PIX_FMT_YUV422P;
532         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
533         break;
534     case IMGTYPE_YUV420:
535         c->decomp_size = basesize / 2 * 3;
536         max_decomp_size = max_basesize / 2 * 3;
537         avctx->pix_fmt = AV_PIX_FMT_YUV420P;
538         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
539         break;
540     default:
541         av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
542         return AVERROR_INVALIDDATA;
543     }
544
545     av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &subsample_h, &subsample_v);
546     if (avctx->width % (1<<subsample_h) || avctx->height % (1<<subsample_v)) {
547         avpriv_request_sample(avctx, "Unsupported dimensions\n");
548         return AVERROR_INVALIDDATA;
549     }
550
551     /* Detect compression method */
552     c->compression = (int8_t)avctx->extradata[5];
553     switch (avctx->codec_id) {
554     case AV_CODEC_ID_MSZH:
555         switch (c->compression) {
556         case COMP_MSZH:
557             av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
558             break;
559         case COMP_MSZH_NOCOMP:
560             c->decomp_size = 0;
561             av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
562             break;
563         default:
564             av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
565             return AVERROR_INVALIDDATA;
566         }
567         break;
568 #if CONFIG_ZLIB_DECODER
569     case AV_CODEC_ID_ZLIB:
570         switch (c->compression) {
571         case COMP_ZLIB_HISPEED:
572             av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
573             break;
574         case COMP_ZLIB_HICOMP:
575             av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
576             break;
577         case COMP_ZLIB_NORMAL:
578             av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
579             break;
580         default:
581             if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
582                 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
583                 return AVERROR_INVALIDDATA;
584             }
585             av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
586         }
587         break;
588 #endif
589     default:
590         av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
591         return AVERROR_INVALIDDATA;
592     }
593
594     /* Allocate decompression buffer */
595     if (c->decomp_size) {
596         if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
597             av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
598             return AVERROR(ENOMEM);
599         }
600     }
601
602     /* Detect flags */
603     c->flags = avctx->extradata[6];
604     if (c->flags & FLAG_MULTITHREAD)
605         av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
606     if (c->flags & FLAG_NULLFRAME)
607         av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
608     if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
609         av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
610     if (c->flags & FLAGMASK_UNUSED)
611         av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
612
613     /* If needed init zlib */
614 #if CONFIG_ZLIB_DECODER
615     if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
616         int zret;
617         c->zstream.zalloc = Z_NULL;
618         c->zstream.zfree = Z_NULL;
619         c->zstream.opaque = Z_NULL;
620         zret = inflateInit(&c->zstream);
621         if (zret != Z_OK) {
622             av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
623             av_freep(&c->decomp_buf);
624             return AVERROR_UNKNOWN;
625         }
626     }
627 #endif
628
629     return 0;
630 }
631
632 /*
633  *
634  * Uninit lcl decoder
635  *
636  */
637 static av_cold int decode_end(AVCodecContext *avctx)
638 {
639     LclDecContext * const c = avctx->priv_data;
640
641     av_freep(&c->decomp_buf);
642 #if CONFIG_ZLIB_DECODER
643     if (avctx->codec_id == AV_CODEC_ID_ZLIB)
644         inflateEnd(&c->zstream);
645 #endif
646
647     return 0;
648 }
649
650 #if CONFIG_MSZH_DECODER
651 AVCodec ff_mszh_decoder = {
652     .name           = "mszh",
653     .long_name      = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
654     .type           = AVMEDIA_TYPE_VIDEO,
655     .id             = AV_CODEC_ID_MSZH,
656     .priv_data_size = sizeof(LclDecContext),
657     .init           = decode_init,
658     .close          = decode_end,
659     .decode         = decode_frame,
660     .capabilities   = CODEC_CAP_DR1,
661 };
662 #endif
663
664 #if CONFIG_ZLIB_DECODER
665 AVCodec ff_zlib_decoder = {
666     .name           = "zlib",
667     .long_name      = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
668     .type           = AVMEDIA_TYPE_VIDEO,
669     .id             = AV_CODEC_ID_ZLIB,
670     .priv_data_size = sizeof(LclDecContext),
671     .init           = decode_init,
672     .close          = decode_end,
673     .decode         = decode_frame,
674     .capabilities   = CODEC_CAP_DR1,
675 };
676 #endif