]> git.sesse.net Git - ffmpeg/blob - libavcodec/lcldec.c
vc1: use use_ic instead of shuffling mv_mode around
[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 "internal.h"
48 #include "lcl.h"
49
50 #if CONFIG_ZLIB_DECODER
51 #include <zlib.h>
52 #endif
53
54 /*
55  * Decoder context
56  */
57 typedef struct LclDecContext {
58     // Image type
59     int imgtype;
60     // Compression type
61     int compression;
62     // Flags
63     int flags;
64     // Decompressed data size
65     unsigned int decomp_size;
66     // Decompression buffer
67     unsigned char* decomp_buf;
68 #if CONFIG_ZLIB_DECODER
69     z_stream zstream;
70 #endif
71 } LclDecContext;
72
73
74 /**
75  * @param srcptr compressed source buffer, must be padded with at least 5 extra bytes
76  * @param destptr must be padded sufficiently for av_memcpy_backptr
77  */
78 static unsigned int mszh_decomp(const unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
79 {
80     unsigned char *destptr_bak = destptr;
81     unsigned char *destptr_end = destptr + destsize;
82     const unsigned char *srcptr_end = srcptr + srclen;
83     unsigned mask = *srcptr++;
84     unsigned maskbit = 0x80;
85
86     while (srcptr < srcptr_end && destptr < destptr_end) {
87         if (!(mask & maskbit)) {
88             memcpy(destptr, srcptr, 4);
89             destptr += 4;
90             srcptr += 4;
91         } else {
92             unsigned ofs = bytestream_get_le16(&srcptr);
93             unsigned cnt = (ofs >> 11) + 1;
94             ofs &= 0x7ff;
95             ofs = FFMIN(ofs, destptr - destptr_bak);
96             cnt *= 4;
97             cnt = FFMIN(cnt, destptr_end - destptr);
98             if (ofs) {
99                 av_memcpy_backptr(destptr, ofs, cnt);
100             } else {
101                 // Not known what the correct behaviour is, but
102                 // this at least avoids uninitialized data.
103                 memset(destptr, 0, cnt);
104             }
105             destptr += cnt;
106         }
107         maskbit >>= 1;
108         if (!maskbit) {
109             mask = *srcptr++;
110             while (!mask) {
111                 if (destptr_end - destptr < 32 || srcptr_end - srcptr < 32) break;
112                 memcpy(destptr, srcptr, 32);
113                 destptr += 32;
114                 srcptr += 32;
115                 mask = *srcptr++;
116             }
117             maskbit = 0x80;
118         }
119     }
120
121     return destptr - destptr_bak;
122 }
123
124
125 #if CONFIG_ZLIB_DECODER
126 /**
127  * @brief decompress a zlib-compressed data block into decomp_buf
128  * @param src compressed input buffer
129  * @param src_len data length in input buffer
130  * @param offset offset in decomp_buf
131  * @param expected expected decompressed length
132  */
133 static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected)
134 {
135     LclDecContext *c = avctx->priv_data;
136     int zret = inflateReset(&c->zstream);
137     if (zret != Z_OK) {
138         av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
139         return AVERROR_UNKNOWN;
140     }
141     c->zstream.next_in = (uint8_t *)src;
142     c->zstream.avail_in = src_len;
143     c->zstream.next_out = c->decomp_buf + offset;
144     c->zstream.avail_out = c->decomp_size - offset;
145     zret = inflate(&c->zstream, Z_FINISH);
146     if (zret != Z_OK && zret != Z_STREAM_END) {
147         av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
148         return AVERROR_UNKNOWN;
149     }
150     if (expected != (unsigned int)c->zstream.total_out) {
151         av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
152                expected, c->zstream.total_out);
153         return AVERROR_UNKNOWN;
154     }
155     return c->zstream.total_out;
156 }
157 #endif
158
159
160 /*
161  *
162  * Decode a frame
163  *
164  */
165 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
166 {
167     AVFrame *frame = data;
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, 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(encoded);
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(encoded+4);
204                 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
205                 mszh_dlen = mszh_decomp(encoded + 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(encoded + 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(encoded, 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, encoded, len);
268                 encoded = c->decomp_buf;
269             } else {
270                 break;
271             }
272         } else if (c->flags & FLAG_MULTITHREAD) {
273             mthread_inlen = AV_RL32(encoded);
274             mthread_inlen = FFMIN(mthread_inlen, len - 8);
275             mthread_outlen = AV_RL32(encoded+4);
276             mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
277             ret = zlib_decomp(avctx, encoded + 8, mthread_inlen, 0, mthread_outlen);
278             if (ret < 0) return ret;
279             ret = zlib_decomp(avctx, encoded + 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, encoded, 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
486     if (avctx->extradata_size < 8) {
487         av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
488         return AVERROR_INVALIDDATA;
489     }
490
491     /* Check codec type */
492     if ((avctx->codec_id == AV_CODEC_ID_MSZH  && avctx->extradata[7] != CODEC_MSZH) ||
493         (avctx->codec_id == AV_CODEC_ID_ZLIB  && avctx->extradata[7] != CODEC_ZLIB)) {
494         av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
495     }
496
497     /* Detect image type */
498     switch (c->imgtype = avctx->extradata[4]) {
499     case IMGTYPE_YUV111:
500         c->decomp_size = basesize * 3;
501         max_decomp_size = max_basesize * 3;
502         avctx->pix_fmt = AV_PIX_FMT_YUV444P;
503         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
504         break;
505     case IMGTYPE_YUV422:
506         c->decomp_size = basesize * 2;
507         max_decomp_size = max_basesize * 2;
508         avctx->pix_fmt = AV_PIX_FMT_YUV422P;
509         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
510         break;
511     case IMGTYPE_RGB24:
512         c->decomp_size = basesize * 3;
513         max_decomp_size = max_basesize * 3;
514         avctx->pix_fmt = AV_PIX_FMT_BGR24;
515         av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
516         break;
517     case IMGTYPE_YUV411:
518         c->decomp_size = basesize / 2 * 3;
519         max_decomp_size = max_basesize / 2 * 3;
520         avctx->pix_fmt = AV_PIX_FMT_YUV411P;
521         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
522         break;
523     case IMGTYPE_YUV211:
524         c->decomp_size = basesize * 2;
525         max_decomp_size = max_basesize * 2;
526         avctx->pix_fmt = AV_PIX_FMT_YUV422P;
527         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
528         break;
529     case IMGTYPE_YUV420:
530         c->decomp_size = basesize / 2 * 3;
531         max_decomp_size = max_basesize / 2 * 3;
532         avctx->pix_fmt = AV_PIX_FMT_YUV420P;
533         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
534         break;
535     default:
536         av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
537         return AVERROR_INVALIDDATA;
538     }
539
540     /* Detect compression method */
541     c->compression = (int8_t)avctx->extradata[5];
542     switch (avctx->codec_id) {
543     case AV_CODEC_ID_MSZH:
544         switch (c->compression) {
545         case COMP_MSZH:
546             av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
547             break;
548         case COMP_MSZH_NOCOMP:
549             c->decomp_size = 0;
550             av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
551             break;
552         default:
553             av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
554             return AVERROR_INVALIDDATA;
555         }
556         break;
557 #if CONFIG_ZLIB_DECODER
558     case AV_CODEC_ID_ZLIB:
559         switch (c->compression) {
560         case COMP_ZLIB_HISPEED:
561             av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
562             break;
563         case COMP_ZLIB_HICOMP:
564             av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
565             break;
566         case COMP_ZLIB_NORMAL:
567             av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
568             break;
569         default:
570             if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
571                 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
572                 return AVERROR_INVALIDDATA;
573             }
574             av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
575         }
576         break;
577 #endif
578     default:
579         av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
580         return AVERROR_INVALIDDATA;
581     }
582
583     /* Allocate decompression buffer */
584     if (c->decomp_size) {
585         if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
586             av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
587             return AVERROR(ENOMEM);
588         }
589     }
590
591     /* Detect flags */
592     c->flags = avctx->extradata[6];
593     if (c->flags & FLAG_MULTITHREAD)
594         av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
595     if (c->flags & FLAG_NULLFRAME)
596         av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
597     if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
598         av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
599     if (c->flags & FLAGMASK_UNUSED)
600         av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
601
602     /* If needed init zlib */
603 #if CONFIG_ZLIB_DECODER
604     if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
605         int zret;
606         c->zstream.zalloc = Z_NULL;
607         c->zstream.zfree = Z_NULL;
608         c->zstream.opaque = Z_NULL;
609         zret = inflateInit(&c->zstream);
610         if (zret != Z_OK) {
611             av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
612             av_freep(&c->decomp_buf);
613             return AVERROR_UNKNOWN;
614         }
615     }
616 #endif
617
618     return 0;
619 }
620
621 /*
622  *
623  * Uninit lcl decoder
624  *
625  */
626 static av_cold int decode_end(AVCodecContext *avctx)
627 {
628     LclDecContext * const c = avctx->priv_data;
629
630     av_freep(&c->decomp_buf);
631 #if CONFIG_ZLIB_DECODER
632     if (avctx->codec_id == AV_CODEC_ID_ZLIB)
633         inflateEnd(&c->zstream);
634 #endif
635
636     return 0;
637 }
638
639 #if CONFIG_MSZH_DECODER
640 AVCodec ff_mszh_decoder = {
641     .name           = "mszh",
642     .type           = AVMEDIA_TYPE_VIDEO,
643     .id             = AV_CODEC_ID_MSZH,
644     .priv_data_size = sizeof(LclDecContext),
645     .init           = decode_init,
646     .close          = decode_end,
647     .decode         = decode_frame,
648     .capabilities   = CODEC_CAP_DR1,
649     .long_name      = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
650 };
651 #endif
652
653 #if CONFIG_ZLIB_DECODER
654 AVCodec ff_zlib_decoder = {
655     .name           = "zlib",
656     .type           = AVMEDIA_TYPE_VIDEO,
657     .id             = AV_CODEC_ID_ZLIB,
658     .priv_data_size = sizeof(LclDecContext),
659     .init           = decode_init,
660     .close          = decode_end,
661     .decode         = decode_frame,
662     .capabilities   = CODEC_CAP_DR1,
663     .long_name      = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
664 };
665 #endif