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