]> git.sesse.net Git - ffmpeg/blob - libavcodec/lcldec.c
f53d91e715baff662738c9bc933963ca1487aa88
[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 libavcodec/lcldec.c
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             cnt *= 4;
97             cnt = FFMIN(cnt, destptr_end - destptr);
98             av_memcpy_backptr(destptr, ofs, cnt);
99             destptr += cnt;
100         }
101         maskbit >>= 1;
102         if (!maskbit) {
103             mask = *srcptr++;
104             maskbit = 0x80;
105         }
106     }
107
108     return destptr - destptr_bak;
109 }
110
111
112 /**
113  * \brief decompress a zlib-compressed data block into decomp_buf
114  * \param src compressed input buffer
115  * \param src_len data length in input buffer
116  * \param offset offset in decomp_buf
117  * \param expected expected decompressed length
118  */
119 static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected)
120 {
121     LclDecContext *c = avctx->priv_data;
122     int zret = inflateReset(&c->zstream);
123     if (zret != Z_OK) {
124         av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
125         return -1;
126     }
127     c->zstream.next_in = src;
128     c->zstream.avail_in = src_len;
129     c->zstream.next_out = c->decomp_buf + offset;
130     c->zstream.avail_out = c->decomp_size - offset;
131     zret = inflate(&c->zstream, Z_FINISH);
132     if (zret != Z_OK && zret != Z_STREAM_END) {
133         av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
134         return -1;
135     }
136     if (expected != (unsigned int)c->zstream.total_out) {
137         av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
138                expected, c->zstream.total_out);
139         return -1;
140     }
141     return c->zstream.total_out;
142 }
143
144
145 /*
146  *
147  * Decode a frame
148  *
149  */
150 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
151 {
152     const uint8_t *buf = avpkt->data;
153     int buf_size = avpkt->size;
154     LclDecContext * const c = avctx->priv_data;
155     unsigned char *encoded = (unsigned char *)buf;
156     unsigned int pixel_ptr;
157     int row, col;
158     unsigned char *outptr;
159     uint8_t *y_out, *u_out, *v_out;
160     unsigned int width = avctx->width; // Real image width
161     unsigned int height = avctx->height; // Real image height
162     unsigned int mszh_dlen;
163     unsigned char yq, y1q, uq, vq;
164     int uqvq;
165     unsigned int mthread_inlen, mthread_outlen;
166     unsigned int len = buf_size;
167
168     if(c->pic.data[0])
169         avctx->release_buffer(avctx, &c->pic);
170
171     c->pic.reference = 0;
172     c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
173     if(avctx->get_buffer(avctx, &c->pic) < 0){
174         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
175         return -1;
176     }
177
178     outptr = c->pic.data[0]; // Output image pointer
179
180     /* Decompress frame */
181     switch (avctx->codec_id) {
182     case CODEC_ID_MSZH:
183         switch (c->compression) {
184         case COMP_MSZH:
185             if (c->flags & FLAG_MULTITHREAD) {
186                 mthread_inlen = AV_RL32(encoded);
187                 mthread_inlen = FFMIN(mthread_inlen, len - 8);
188                 mthread_outlen = AV_RL32(encoded+4);
189                 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
190                 mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
191                 if (mthread_outlen != mszh_dlen) {
192                     av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
193                            mthread_outlen, mszh_dlen);
194                     return -1;
195                 }
196                 mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
197                                         c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
198                 if (mthread_outlen != mszh_dlen) {
199                     av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
200                            mthread_outlen, mszh_dlen);
201                     return -1;
202                 }
203                 encoded = c->decomp_buf;
204                 len = c->decomp_size;
205             } else {
206                 mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
207                 if (c->decomp_size != mszh_dlen) {
208                     av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
209                            c->decomp_size, mszh_dlen);
210                     return -1;
211                 }
212                 encoded = c->decomp_buf;
213                 len = mszh_dlen;
214             }
215             break;
216         case COMP_MSZH_NOCOMP:
217             break;
218         default:
219             av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
220             return -1;
221         }
222         break;
223 #if CONFIG_ZLIB_DECODER
224     case CODEC_ID_ZLIB:
225         /* Using the original dll with normal compression (-1) and RGB format
226          * gives a file with ZLIB fourcc, but frame is really uncompressed.
227          * To be sure that's true check also frame size */
228         if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
229             len == width * height * 3)
230             break;
231         if (c->flags & FLAG_MULTITHREAD) {
232             int ret;
233             mthread_inlen = AV_RL32(encoded);
234             mthread_inlen = FFMIN(mthread_inlen, len - 8);
235             mthread_outlen = AV_RL32(encoded+4);
236             mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
237             ret = zlib_decomp(avctx, encoded + 8, mthread_inlen, 0, mthread_outlen);
238             if (ret < 0) return ret;
239             ret = zlib_decomp(avctx, encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
240                               mthread_outlen, mthread_outlen);
241             if (ret < 0) return ret;
242         } else {
243             int ret = zlib_decomp(avctx, encoded, len, 0, c->decomp_size);
244             if (ret < 0) return ret;
245         }
246         encoded = c->decomp_buf;
247         len = c->decomp_size;
248         break;
249 #endif
250     default:
251         av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
252         return -1;
253     }
254
255
256     /* Apply PNG filter */
257     if (avctx->codec_id == CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
258         switch (c->imgtype) {
259         case IMGTYPE_YUV111:
260         case IMGTYPE_RGB24:
261             for (row = 0; row < height; row++) {
262                 pixel_ptr = row * width * 3;
263                 yq = encoded[pixel_ptr++];
264                 uqvq = AV_RL16(encoded+pixel_ptr);
265                 pixel_ptr += 2;
266                 for (col = 1; col < width; col++) {
267                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
268                     uqvq -= AV_RL16(encoded+pixel_ptr+1);
269                     AV_WL16(encoded+pixel_ptr+1, uqvq);
270                     pixel_ptr += 3;
271                 }
272             }
273             break;
274         case IMGTYPE_YUV422:
275             for (row = 0; row < height; row++) {
276                 pixel_ptr = row * width * 2;
277                 yq = uq = vq =0;
278                 for (col = 0; col < width/4; col++) {
279                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
280                     encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
281                     encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
282                     encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
283                     encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
284                     encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
285                     encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
286                     encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
287                     pixel_ptr += 8;
288                 }
289             }
290             break;
291         case IMGTYPE_YUV411:
292             for (row = 0; row < height; row++) {
293                 pixel_ptr = row * width / 2 * 3;
294                 yq = uq = vq =0;
295                 for (col = 0; col < width/4; col++) {
296                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
297                     encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
298                     encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
299                     encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
300                     encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
301                     encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
302                     pixel_ptr += 6;
303                 }
304             }
305             break;
306         case IMGTYPE_YUV211:
307             for (row = 0; row < height; row++) {
308                 pixel_ptr = row * width * 2;
309                 yq = uq = vq =0;
310                 for (col = 0; col < width/2; col++) {
311                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
312                     encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
313                     encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
314                     encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
315                     pixel_ptr += 4;
316                 }
317             }
318             break;
319         case IMGTYPE_YUV420:
320             for (row = 0; row < height/2; row++) {
321                 pixel_ptr = row * width * 3;
322                 yq = y1q = uq = vq =0;
323                 for (col = 0; col < width/2; col++) {
324                     encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
325                     encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
326                     encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
327                     encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
328                     encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
329                     encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
330                     pixel_ptr += 6;
331                 }
332             }
333             break;
334         default:
335             av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
336             return -1;
337         }
338     }
339
340     /* Convert colorspace */
341     y_out = c->pic.data[0] + (height - 1) * c->pic.linesize[0];
342     u_out = c->pic.data[1] + (height - 1) * c->pic.linesize[1];
343     v_out = c->pic.data[2] + (height - 1) * c->pic.linesize[2];
344     switch (c->imgtype) {
345     case IMGTYPE_YUV111:
346         for (row = 0; row < height; row++) {
347             for (col = 0; col < width; col++) {
348                 y_out[col] = *encoded++;
349                 u_out[col] = *encoded++ + 128;
350                 v_out[col] = *encoded++ + 128;
351             }
352             y_out -= c->pic.linesize[0];
353             u_out -= c->pic.linesize[1];
354             v_out -= c->pic.linesize[2];
355         }
356         break;
357     case IMGTYPE_YUV422:
358         for (row = 0; row < height; row++) {
359             for (col = 0; col < width - 3; col += 4) {
360                 memcpy(y_out + col, encoded, 4);
361                 encoded += 4;
362                 u_out[ col >> 1     ] = *encoded++ + 128;
363                 u_out[(col >> 1) + 1] = *encoded++ + 128;
364                 v_out[ col >> 1     ] = *encoded++ + 128;
365                 v_out[(col >> 1) + 1] = *encoded++ + 128;
366             }
367             y_out -= c->pic.linesize[0];
368             u_out -= c->pic.linesize[1];
369             v_out -= c->pic.linesize[2];
370         }
371         break;
372     case IMGTYPE_RGB24:
373         for (row = height - 1; row >= 0; row--) {
374             pixel_ptr = row * c->pic.linesize[0];
375             memcpy(outptr + pixel_ptr, encoded, 3 * width);
376             encoded += 3 * width;
377         }
378         break;
379     case IMGTYPE_YUV411:
380         for (row = 0; row < height; row++) {
381             for (col = 0; col < width - 3; col += 4) {
382                 memcpy(y_out + col, encoded, 4);
383                 encoded += 4;
384                 u_out[col >> 2] = *encoded++ + 128;
385                 v_out[col >> 2] = *encoded++ + 128;
386             }
387             y_out -= c->pic.linesize[0];
388             u_out -= c->pic.linesize[1];
389             v_out -= c->pic.linesize[2];
390         }
391         break;
392     case IMGTYPE_YUV211:
393         for (row = 0; row < height; row++) {
394             for (col = 0; col < width - 1; col += 2) {
395                 memcpy(y_out + col, encoded, 2);
396                 encoded += 2;
397                 u_out[col >> 1] = *encoded++ + 128;
398                 v_out[col >> 1] = *encoded++ + 128;
399             }
400             y_out -= c->pic.linesize[0];
401             u_out -= c->pic.linesize[1];
402             v_out -= c->pic.linesize[2];
403         }
404         break;
405     case IMGTYPE_YUV420:
406         u_out = c->pic.data[1] + ((height >> 1) - 1) * c->pic.linesize[1];
407         v_out = c->pic.data[2] + ((height >> 1) - 1) * c->pic.linesize[2];
408         for (row = 0; row < height - 1; row += 2) {
409             for (col = 0; col < width - 1; col += 2) {
410                 memcpy(y_out + col, encoded, 2);
411                 encoded += 2;
412                 memcpy(y_out + col - c->pic.linesize[0], encoded, 2);
413                 encoded += 2;
414                 u_out[col >> 1] = *encoded++ + 128;
415                 v_out[col >> 1] = *encoded++ + 128;
416             }
417             y_out -= c->pic.linesize[0] << 1;
418             u_out -= c->pic.linesize[1];
419             v_out -= c->pic.linesize[2];
420         }
421         break;
422     default:
423         av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
424         return -1;
425     }
426
427     *data_size = sizeof(AVFrame);
428     *(AVFrame*)data = c->pic;
429
430     /* always report that the buffer was completely consumed */
431     return buf_size;
432 }
433
434 /*
435  *
436  * Init lcl decoder
437  *
438  */
439 static av_cold int decode_init(AVCodecContext *avctx)
440 {
441     LclDecContext * const c = avctx->priv_data;
442     unsigned int basesize = avctx->width * avctx->height;
443     unsigned int max_basesize = FFALIGN(avctx->width, 4) * FFALIGN(avctx->height, 4) + AV_LZO_OUTPUT_PADDING;
444     unsigned int max_decomp_size;
445
446     c->pic.data[0] = NULL;
447
448 #if CONFIG_ZLIB_DECODER
449     // Needed if zlib unused or init aborted before inflateInit
450     memset(&c->zstream, 0, sizeof(z_stream));
451 #endif
452
453     if (avctx->extradata_size < 8) {
454         av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
455         return 1;
456     }
457
458     if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
459         return 1;
460     }
461
462     /* Check codec type */
463     if ((avctx->codec_id == CODEC_ID_MSZH  && avctx->extradata[7] != CODEC_MSZH) ||
464         (avctx->codec_id == CODEC_ID_ZLIB  && avctx->extradata[7] != CODEC_ZLIB)) {
465         av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
466     }
467
468     /* Detect image type */
469     switch (c->imgtype = avctx->extradata[4]) {
470     case IMGTYPE_YUV111:
471         c->decomp_size = basesize * 3;
472         max_decomp_size = max_basesize * 3;
473         avctx->pix_fmt = PIX_FMT_YUV444P;
474         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
475         break;
476     case IMGTYPE_YUV422:
477         c->decomp_size = basesize * 2;
478         max_decomp_size = max_basesize * 2;
479         avctx->pix_fmt = PIX_FMT_YUV422P;
480         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
481         break;
482     case IMGTYPE_RGB24:
483         c->decomp_size = basesize * 3;
484         max_decomp_size = max_basesize * 3;
485         avctx->pix_fmt = PIX_FMT_BGR24;
486         av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
487         break;
488     case IMGTYPE_YUV411:
489         c->decomp_size = basesize / 2 * 3;
490         max_decomp_size = max_basesize / 2 * 3;
491         avctx->pix_fmt = PIX_FMT_YUV411P;
492         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
493         break;
494     case IMGTYPE_YUV211:
495         c->decomp_size = basesize * 2;
496         max_decomp_size = max_basesize * 2;
497         avctx->pix_fmt = PIX_FMT_YUV422P;
498         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
499         break;
500     case IMGTYPE_YUV420:
501         c->decomp_size = basesize / 2 * 3;
502         max_decomp_size = max_basesize / 2 * 3;
503         avctx->pix_fmt = PIX_FMT_YUV420P;
504         av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
505         break;
506     default:
507         av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
508         return 1;
509     }
510
511     /* Detect compression method */
512     c->compression = (int8_t)avctx->extradata[5];
513     switch (avctx->codec_id) {
514     case CODEC_ID_MSZH:
515         switch (c->compression) {
516         case COMP_MSZH:
517             av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
518             break;
519         case COMP_MSZH_NOCOMP:
520             c->decomp_size = 0;
521             av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
522             break;
523         default:
524             av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
525             return 1;
526         }
527         break;
528 #if CONFIG_ZLIB_DECODER
529     case CODEC_ID_ZLIB:
530         switch (c->compression) {
531         case COMP_ZLIB_HISPEED:
532             av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
533             break;
534         case COMP_ZLIB_HICOMP:
535             av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
536             break;
537         case COMP_ZLIB_NORMAL:
538             av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
539             break;
540         default:
541             if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
542                 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
543                 return 1;
544             }
545             av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
546         }
547         break;
548 #endif
549     default:
550         av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
551         return 1;
552     }
553
554     /* Allocate decompression buffer */
555     if (c->decomp_size) {
556         if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
557             av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
558             return 1;
559         }
560     }
561
562     /* Detect flags */
563     c->flags = avctx->extradata[6];
564     if (c->flags & FLAG_MULTITHREAD)
565         av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
566     if (c->flags & FLAG_NULLFRAME)
567         av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
568     if (avctx->codec_id == CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
569         av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
570     if (c->flags & FLAGMASK_UNUSED)
571         av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
572
573     /* If needed init zlib */
574 #if CONFIG_ZLIB_DECODER
575     if (avctx->codec_id == CODEC_ID_ZLIB) {
576         int zret;
577         c->zstream.zalloc = Z_NULL;
578         c->zstream.zfree = Z_NULL;
579         c->zstream.opaque = Z_NULL;
580         zret = inflateInit(&c->zstream);
581         if (zret != Z_OK) {
582             av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
583             return 1;
584         }
585     }
586 #endif
587
588     return 0;
589 }
590
591 /*
592  *
593  * Uninit lcl decoder
594  *
595  */
596 static av_cold int decode_end(AVCodecContext *avctx)
597 {
598     LclDecContext * const c = avctx->priv_data;
599
600     if (c->pic.data[0])
601         avctx->release_buffer(avctx, &c->pic);
602 #if CONFIG_ZLIB_DECODER
603     inflateEnd(&c->zstream);
604 #endif
605
606     return 0;
607 }
608
609 #if CONFIG_MSZH_DECODER
610 AVCodec mszh_decoder = {
611     "mszh",
612     CODEC_TYPE_VIDEO,
613     CODEC_ID_MSZH,
614     sizeof(LclDecContext),
615     decode_init,
616     NULL,
617     decode_end,
618     decode_frame,
619     CODEC_CAP_DR1,
620     .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
621 };
622 #endif
623
624 #if CONFIG_ZLIB_DECODER
625 AVCodec zlib_decoder = {
626     "zlib",
627     CODEC_TYPE_VIDEO,
628     CODEC_ID_ZLIB,
629     sizeof(LclDecContext),
630     decode_init,
631     NULL,
632     decode_end,
633     decode_frame,
634     CODEC_CAP_DR1,
635     .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
636 };
637 #endif