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