Rename var: val -> energy
[FFMpeg-mirror/DVCPRO-HD.git] / libavcodec / lcldec.c
blobe9add522cd95a7c61b70a1d7d2402ff11831f158
1 /*
2 * LCL (LossLess Codec Library) Codec
3 * Copyright (c) 2002-2004 Roberto Togni
5 * This file is part of FFmpeg.
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.
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.
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
22 /**
23 * @file lcl.c
24 * LCL (LossLess Codec Library) Video Codec
25 * Decoder for MSZH and ZLIB codecs
26 * Experimental encoder for ZLIB RGB24
28 * Fourcc: MSZH, ZLIB
30 * Original Win32 dll:
31 * Ver2.23 By Kenji Oshima 2000.09.20
32 * avimszh.dll, avizlib.dll
34 * A description of the decoding algorithm can be found here:
35 * http://www.pcisys.net/~melanson/codecs
37 * Supports: BGR24 (RGB 24bpp)
41 #include <stdio.h>
42 #include <stdlib.h>
44 #include "avcodec.h"
45 #include "bitstream.h"
46 #include "lcl.h"
48 #ifdef CONFIG_ZLIB
49 #include <zlib.h>
50 #endif
53 * Decoder context
55 typedef struct LclDecContext {
56 AVFrame pic;
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 #ifdef CONFIG_ZLIB
69 z_stream zstream;
70 #endif
71 } LclDecContext;
76 * Helper functions
79 static inline unsigned char fix (int pix14)
81 int tmp;
83 tmp = (pix14 + 0x80000) >> 20;
84 if (tmp < 0)
85 return 0;
86 if (tmp > 255)
87 return 255;
88 return tmp;
93 static inline unsigned char get_b (unsigned char yq, signed char bq)
95 return fix((yq << 20) + bq * 1858076);
100 static inline unsigned char get_g (unsigned char yq, signed char bq, signed char rq)
102 return fix((yq << 20) - bq * 360857 - rq * 748830);
107 static inline unsigned char get_r (unsigned char yq, signed char rq)
109 return fix((yq << 20) + rq * 1470103);
114 static unsigned int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
116 unsigned char *destptr_bak = destptr;
117 unsigned char *destptr_end = destptr + destsize;
118 unsigned char mask = 0;
119 unsigned char maskbit = 0;
120 unsigned int ofs, cnt;
122 while ((srclen > 0) && (destptr < destptr_end)) {
123 if (maskbit == 0) {
124 mask = *(srcptr++);
125 maskbit = 8;
126 srclen--;
127 continue;
129 if ((mask & (1 << (--maskbit))) == 0) {
130 if (destptr + 4 > destptr_end)
131 break;
132 *(int*)destptr = *(int*)srcptr;
133 srclen -= 4;
134 destptr += 4;
135 srcptr += 4;
136 } else {
137 ofs = *(srcptr++);
138 cnt = *(srcptr++);
139 ofs += cnt * 256;
140 cnt = ((cnt >> 3) & 0x1f) + 1;
141 ofs &= 0x7ff;
142 srclen -= 2;
143 cnt *= 4;
144 if (destptr + cnt > destptr_end) {
145 cnt = destptr_end - destptr;
147 for (; cnt > 0; cnt--) {
148 *(destptr) = *(destptr - ofs);
149 destptr++;
154 return (destptr - destptr_bak);
161 * Decode a frame
164 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size)
166 LclDecContext * const c = avctx->priv_data;
167 unsigned char *encoded = (unsigned char *)buf;
168 unsigned int pixel_ptr;
169 int row, col;
170 unsigned char *outptr;
171 unsigned int width = avctx->width; // Real image width
172 unsigned int height = avctx->height; // Real image height
173 unsigned int mszh_dlen;
174 unsigned char yq, y1q, uq, vq;
175 int uqvq;
176 unsigned int mthread_inlen, mthread_outlen;
177 #ifdef CONFIG_ZLIB
178 int zret; // Zlib return code
179 #endif
180 unsigned int len = buf_size;
182 if(c->pic.data[0])
183 avctx->release_buffer(avctx, &c->pic);
185 c->pic.reference = 0;
186 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
187 if(avctx->get_buffer(avctx, &c->pic) < 0){
188 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
189 return -1;
192 outptr = c->pic.data[0]; // Output image pointer
194 /* Decompress frame */
195 switch (avctx->codec_id) {
196 case CODEC_ID_MSZH:
197 switch (c->compression) {
198 case COMP_MSZH:
199 if (c->flags & FLAG_MULTITHREAD) {
200 mthread_inlen = *((unsigned int*)encoded);
201 mthread_outlen = *((unsigned int*)(encoded+4));
202 if (mthread_outlen > c->decomp_size) // this should not happen
203 mthread_outlen = c->decomp_size;
204 mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
205 if (mthread_outlen != mszh_dlen) {
206 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
207 mthread_outlen, mszh_dlen);
208 return -1;
210 mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - mthread_inlen,
211 c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
212 if (mthread_outlen != mszh_dlen) {
213 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
214 mthread_outlen, mszh_dlen);
215 return -1;
217 encoded = c->decomp_buf;
218 len = c->decomp_size;
219 } else {
220 mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
221 if (c->decomp_size != mszh_dlen) {
222 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
223 c->decomp_size, mszh_dlen);
224 return -1;
226 encoded = c->decomp_buf;
227 len = mszh_dlen;
229 break;
230 case COMP_MSZH_NOCOMP:
231 break;
232 default:
233 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
234 return -1;
236 break;
237 case CODEC_ID_ZLIB:
238 #ifdef CONFIG_ZLIB
239 /* Using the original dll with normal compression (-1) and RGB format
240 * gives a file with ZLIB fourcc, but frame is really uncompressed.
241 * To be sure that's true check also frame size */
242 if ((c->compression == COMP_ZLIB_NORMAL) && (c->imgtype == IMGTYPE_RGB24) &&
243 (len == width * height * 3))
244 break;
245 zret = inflateReset(&(c->zstream));
246 if (zret != Z_OK) {
247 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
248 return -1;
250 if (c->flags & FLAG_MULTITHREAD) {
251 mthread_inlen = *((unsigned int*)encoded);
252 mthread_outlen = *((unsigned int*)(encoded+4));
253 if (mthread_outlen > c->decomp_size)
254 mthread_outlen = c->decomp_size;
255 c->zstream.next_in = encoded + 8;
256 c->zstream.avail_in = mthread_inlen;
257 c->zstream.next_out = c->decomp_buf;
258 c->zstream.avail_out = c->decomp_size;
259 zret = inflate(&(c->zstream), Z_FINISH);
260 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
261 av_log(avctx, AV_LOG_ERROR, "Mthread1 inflate error: %d\n", zret);
262 return -1;
264 if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
265 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%u != %lu)\n",
266 mthread_outlen, c->zstream.total_out);
267 return -1;
269 zret = inflateReset(&(c->zstream));
270 if (zret != Z_OK) {
271 av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate reset error: %d\n", zret);
272 return -1;
274 c->zstream.next_in = encoded + 8 + mthread_inlen;
275 c->zstream.avail_in = len - mthread_inlen;
276 c->zstream.next_out = c->decomp_buf + mthread_outlen;
277 c->zstream.avail_out = c->decomp_size - mthread_outlen;
278 zret = inflate(&(c->zstream), Z_FINISH);
279 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
280 av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate error: %d\n", zret);
281 return -1;
283 if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
284 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %lu)\n",
285 mthread_outlen, c->zstream.total_out);
286 return -1;
288 } else {
289 c->zstream.next_in = encoded;
290 c->zstream.avail_in = len;
291 c->zstream.next_out = c->decomp_buf;
292 c->zstream.avail_out = c->decomp_size;
293 zret = inflate(&(c->zstream), Z_FINISH);
294 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
295 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
296 return -1;
298 if (c->decomp_size != (unsigned int)(c->zstream.total_out)) {
299 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
300 c->decomp_size, c->zstream.total_out);
301 return -1;
304 encoded = c->decomp_buf;
305 len = c->decomp_size;
306 #else
307 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
308 return -1;
309 #endif
310 break;
311 default:
312 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
313 return -1;
317 /* Apply PNG filter */
318 if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) {
319 switch (c->imgtype) {
320 case IMGTYPE_YUV111:
321 case IMGTYPE_RGB24:
322 for (row = 0; row < height; row++) {
323 pixel_ptr = row * width * 3;
324 yq = encoded[pixel_ptr++];
325 uqvq = AV_RL16(encoded+pixel_ptr);
326 pixel_ptr += 2;
327 for (col = 1; col < width; col++) {
328 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
329 uqvq -= AV_RL16(encoded+pixel_ptr+1);
330 AV_WL16(encoded+pixel_ptr+1, uqvq);
331 pixel_ptr += 3;
334 break;
335 case IMGTYPE_YUV422:
336 for (row = 0; row < height; row++) {
337 pixel_ptr = row * width * 2;
338 yq = uq = vq =0;
339 for (col = 0; col < width/4; col++) {
340 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
341 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
342 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
343 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
344 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
345 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
346 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
347 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
348 pixel_ptr += 8;
351 break;
352 case IMGTYPE_YUV411:
353 for (row = 0; row < height; row++) {
354 pixel_ptr = row * width / 2 * 3;
355 yq = uq = vq =0;
356 for (col = 0; col < width/4; col++) {
357 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
358 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
359 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
360 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
361 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
362 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
363 pixel_ptr += 6;
366 break;
367 case IMGTYPE_YUV211:
368 for (row = 0; row < height; row++) {
369 pixel_ptr = row * width * 2;
370 yq = uq = vq =0;
371 for (col = 0; col < width/2; col++) {
372 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
373 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
374 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
375 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
376 pixel_ptr += 4;
379 break;
380 case IMGTYPE_YUV420:
381 for (row = 0; row < height/2; row++) {
382 pixel_ptr = row * width * 3;
383 yq = y1q = uq = vq =0;
384 for (col = 0; col < width/2; col++) {
385 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
386 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
387 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
388 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
389 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
390 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
391 pixel_ptr += 6;
394 break;
395 default:
396 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
397 return -1;
401 /* Convert colorspace */
402 switch (c->imgtype) {
403 case IMGTYPE_YUV111:
404 for (row = height - 1; row >= 0; row--) {
405 pixel_ptr = row * c->pic.linesize[0];
406 for (col = 0; col < width; col++) {
407 outptr[pixel_ptr++] = get_b(encoded[0], encoded[1]);
408 outptr[pixel_ptr++] = get_g(encoded[0], encoded[1], encoded[2]);
409 outptr[pixel_ptr++] = get_r(encoded[0], encoded[2]);
410 encoded += 3;
413 break;
414 case IMGTYPE_YUV422:
415 for (row = height - 1; row >= 0; row--) {
416 pixel_ptr = row * c->pic.linesize[0];
417 for (col = 0; col < width/4; col++) {
418 outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
419 outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[6]);
420 outptr[pixel_ptr++] = get_r(encoded[0], encoded[6]);
421 outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
422 outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[6]);
423 outptr[pixel_ptr++] = get_r(encoded[1], encoded[6]);
424 outptr[pixel_ptr++] = get_b(encoded[2], encoded[5]);
425 outptr[pixel_ptr++] = get_g(encoded[2], encoded[5], encoded[7]);
426 outptr[pixel_ptr++] = get_r(encoded[2], encoded[7]);
427 outptr[pixel_ptr++] = get_b(encoded[3], encoded[5]);
428 outptr[pixel_ptr++] = get_g(encoded[3], encoded[5], encoded[7]);
429 outptr[pixel_ptr++] = get_r(encoded[3], encoded[7]);
430 encoded += 8;
433 break;
434 case IMGTYPE_RGB24:
435 for (row = height - 1; row >= 0; row--) {
436 pixel_ptr = row * c->pic.linesize[0];
437 for (col = 0; col < width; col++) {
438 outptr[pixel_ptr++] = encoded[0];
439 outptr[pixel_ptr++] = encoded[1];
440 outptr[pixel_ptr++] = encoded[2];
441 encoded += 3;
444 break;
445 case IMGTYPE_YUV411:
446 for (row = height - 1; row >= 0; row--) {
447 pixel_ptr = row * c->pic.linesize[0];
448 for (col = 0; col < width/4; col++) {
449 outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
450 outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[5]);
451 outptr[pixel_ptr++] = get_r(encoded[0], encoded[5]);
452 outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
453 outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[5]);
454 outptr[pixel_ptr++] = get_r(encoded[1], encoded[5]);
455 outptr[pixel_ptr++] = get_b(encoded[2], encoded[4]);
456 outptr[pixel_ptr++] = get_g(encoded[2], encoded[4], encoded[5]);
457 outptr[pixel_ptr++] = get_r(encoded[2], encoded[5]);
458 outptr[pixel_ptr++] = get_b(encoded[3], encoded[4]);
459 outptr[pixel_ptr++] = get_g(encoded[3], encoded[4], encoded[5]);
460 outptr[pixel_ptr++] = get_r(encoded[3], encoded[5]);
461 encoded += 6;
464 break;
465 case IMGTYPE_YUV211:
466 for (row = height - 1; row >= 0; row--) {
467 pixel_ptr = row * c->pic.linesize[0];
468 for (col = 0; col < width/2; col++) {
469 outptr[pixel_ptr++] = get_b(encoded[0], encoded[2]);
470 outptr[pixel_ptr++] = get_g(encoded[0], encoded[2], encoded[3]);
471 outptr[pixel_ptr++] = get_r(encoded[0], encoded[3]);
472 outptr[pixel_ptr++] = get_b(encoded[1], encoded[2]);
473 outptr[pixel_ptr++] = get_g(encoded[1], encoded[2], encoded[3]);
474 outptr[pixel_ptr++] = get_r(encoded[1], encoded[3]);
475 encoded += 4;
478 break;
479 case IMGTYPE_YUV420:
480 for (row = height / 2 - 1; row >= 0; row--) {
481 pixel_ptr = 2 * row * c->pic.linesize[0];
482 for (col = 0; col < width/2; col++) {
483 outptr[pixel_ptr] = get_b(encoded[0], encoded[4]);
484 outptr[pixel_ptr+1] = get_g(encoded[0], encoded[4], encoded[5]);
485 outptr[pixel_ptr+2] = get_r(encoded[0], encoded[5]);
486 outptr[pixel_ptr+3] = get_b(encoded[1], encoded[4]);
487 outptr[pixel_ptr+4] = get_g(encoded[1], encoded[4], encoded[5]);
488 outptr[pixel_ptr+5] = get_r(encoded[1], encoded[5]);
489 outptr[pixel_ptr-c->pic.linesize[0]] = get_b(encoded[2], encoded[4]);
490 outptr[pixel_ptr-c->pic.linesize[0]+1] = get_g(encoded[2], encoded[4], encoded[5]);
491 outptr[pixel_ptr-c->pic.linesize[0]+2] = get_r(encoded[2], encoded[5]);
492 outptr[pixel_ptr-c->pic.linesize[0]+3] = get_b(encoded[3], encoded[4]);
493 outptr[pixel_ptr-c->pic.linesize[0]+4] = get_g(encoded[3], encoded[4], encoded[5]);
494 outptr[pixel_ptr-c->pic.linesize[0]+5] = get_r(encoded[3], encoded[5]);
495 pixel_ptr += 6;
496 encoded += 6;
499 break;
500 default:
501 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
502 return -1;
505 *data_size = sizeof(AVFrame);
506 *(AVFrame*)data = c->pic;
508 /* always report that the buffer was completely consumed */
509 return buf_size;
514 * Init lcl decoder
517 static av_cold int decode_init(AVCodecContext *avctx)
519 LclDecContext * const c = avctx->priv_data;
520 unsigned int basesize = avctx->width * avctx->height;
521 unsigned int max_basesize = ((avctx->width + 3) & ~3) * ((avctx->height + 3) & ~3);
522 unsigned int max_decomp_size;
523 int zret; // Zlib return code
525 c->pic.data[0] = NULL;
527 #ifdef CONFIG_ZLIB
528 // Needed if zlib unused or init aborted before inflateInit
529 memset(&(c->zstream), 0, sizeof(z_stream));
530 #endif
532 if (avctx->extradata_size < 8) {
533 av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
534 return 1;
537 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
538 return 1;
541 /* Check codec type */
542 if (((avctx->codec_id == CODEC_ID_MSZH) && (*((char *)avctx->extradata + 7) != CODEC_MSZH)) ||
543 ((avctx->codec_id == CODEC_ID_ZLIB) && (*((char *)avctx->extradata + 7) != CODEC_ZLIB))) {
544 av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
547 /* Detect image type */
548 switch (c->imgtype = *((char *)avctx->extradata + 4)) {
549 case IMGTYPE_YUV111:
550 c->decomp_size = basesize * 3;
551 max_decomp_size = max_basesize * 3;
552 av_log(avctx, AV_LOG_INFO, "Image type is YUV 1:1:1.\n");
553 break;
554 case IMGTYPE_YUV422:
555 c->decomp_size = basesize * 2;
556 max_decomp_size = max_basesize * 2;
557 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:2.\n");
558 break;
559 case IMGTYPE_RGB24:
560 c->decomp_size = basesize * 3;
561 max_decomp_size = max_basesize * 3;
562 av_log(avctx, AV_LOG_INFO, "Image type is RGB 24.\n");
563 break;
564 case IMGTYPE_YUV411:
565 c->decomp_size = basesize / 2 * 3;
566 max_decomp_size = max_basesize / 2 * 3;
567 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:1:1.\n");
568 break;
569 case IMGTYPE_YUV211:
570 c->decomp_size = basesize * 2;
571 max_decomp_size = max_basesize * 2;
572 av_log(avctx, AV_LOG_INFO, "Image type is YUV 2:1:1.\n");
573 break;
574 case IMGTYPE_YUV420:
575 c->decomp_size = basesize / 2 * 3;
576 max_decomp_size = max_basesize / 2 * 3;
577 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:0.\n");
578 break;
579 default:
580 av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
581 return 1;
584 /* Detect compression method */
585 c->compression = *((char *)avctx->extradata + 5);
586 switch (avctx->codec_id) {
587 case CODEC_ID_MSZH:
588 switch (c->compression) {
589 case COMP_MSZH:
590 av_log(avctx, AV_LOG_INFO, "Compression enabled.\n");
591 break;
592 case COMP_MSZH_NOCOMP:
593 c->decomp_size = 0;
594 av_log(avctx, AV_LOG_INFO, "No compression.\n");
595 break;
596 default:
597 av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
598 return 1;
600 break;
601 case CODEC_ID_ZLIB:
602 #ifdef CONFIG_ZLIB
603 switch (c->compression) {
604 case COMP_ZLIB_HISPEED:
605 av_log(avctx, AV_LOG_INFO, "High speed compression.\n");
606 break;
607 case COMP_ZLIB_HICOMP:
608 av_log(avctx, AV_LOG_INFO, "High compression.\n");
609 break;
610 case COMP_ZLIB_NORMAL:
611 av_log(avctx, AV_LOG_INFO, "Normal compression.\n");
612 break;
613 default:
614 if ((c->compression < Z_NO_COMPRESSION) || (c->compression > Z_BEST_COMPRESSION)) {
615 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
616 return 1;
618 av_log(avctx, AV_LOG_INFO, "Compression level for ZLIB: (%d).\n", c->compression);
620 #else
621 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
622 return 1;
623 #endif
624 break;
625 default:
626 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
627 return 1;
630 /* Allocate decompression buffer */
631 if (c->decomp_size) {
632 if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
633 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
634 return 1;
638 /* Detect flags */
639 c->flags = *((char *)avctx->extradata + 6);
640 if (c->flags & FLAG_MULTITHREAD)
641 av_log(avctx, AV_LOG_INFO, "Multithread encoder flag set.\n");
642 if (c->flags & FLAG_NULLFRAME)
643 av_log(avctx, AV_LOG_INFO, "Nullframe insertion flag set.\n");
644 if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER))
645 av_log(avctx, AV_LOG_INFO, "PNG filter flag set.\n");
646 if (c->flags & FLAGMASK_UNUSED)
647 av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
649 /* If needed init zlib */
650 if (avctx->codec_id == CODEC_ID_ZLIB) {
651 #ifdef CONFIG_ZLIB
652 c->zstream.zalloc = Z_NULL;
653 c->zstream.zfree = Z_NULL;
654 c->zstream.opaque = Z_NULL;
655 zret = inflateInit(&(c->zstream));
656 if (zret != Z_OK) {
657 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
658 return 1;
660 #else
661 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
662 return 1;
663 #endif
666 avctx->pix_fmt = PIX_FMT_BGR24;
668 return 0;
673 * Uninit lcl decoder
676 static av_cold int decode_end(AVCodecContext *avctx)
678 LclDecContext * const c = avctx->priv_data;
680 if (c->pic.data[0])
681 avctx->release_buffer(avctx, &c->pic);
682 #ifdef CONFIG_ZLIB
683 inflateEnd(&(c->zstream));
684 #endif
686 return 0;
689 #ifdef CONFIG_MSZH_DECODER
690 AVCodec mszh_decoder = {
691 "mszh",
692 CODEC_TYPE_VIDEO,
693 CODEC_ID_MSZH,
694 sizeof(LclDecContext),
695 decode_init,
696 NULL,
697 decode_end,
698 decode_frame,
699 CODEC_CAP_DR1,
700 .long_name ="LCL (LossLess Codec Library) MSZH",
702 #endif
704 #ifdef CONFIG_ZLIB_DECODER
705 AVCodec zlib_decoder = {
706 "zlib",
707 CODEC_TYPE_VIDEO,
708 CODEC_ID_ZLIB,
709 sizeof(LclDecContext),
710 decode_init,
711 NULL,
712 decode_end,
713 decode_frame,
714 CODEC_CAP_DR1,
715 .long_name ="LCL (LossLess Codec Library) ZLIB",
717 #endif