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