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