2 * LCL (LossLess Codec Library) Codec
3 * Copyright (c) 2002-2004 Roberto Togni
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * LCL (LossLess Codec Library) Video Codec
24 * Decoder for MSZH and ZLIB codecs
25 * Experimental encoder for ZLIB RGB24
30 * Ver2.23 By Kenji Oshima 2000.09.20
31 * avimszh.dll, avizlib.dll
33 * A description of the decoding algorithm can be found here:
34 * http://www.pcisys.net/~melanson/codecs
36 * Supports: BGR24 (RGB 24bpp)
44 #include "bitstream.h"
55 #define IMGTYPE_YUV111 0
56 #define IMGTYPE_YUV422 1
57 #define IMGTYPE_RGB24 2
58 #define IMGTYPE_YUV411 3
59 #define IMGTYPE_YUV211 4
60 #define IMGTYPE_YUV420 5
63 #define COMP_MSZH_NOCOMP 1
64 #define COMP_ZLIB_HISPEED 1
65 #define COMP_ZLIB_HICOMP 9
66 #define COMP_ZLIB_NORMAL -1
68 #define FLAG_MULTITHREAD 1
69 #define FLAG_NULLFRAME 2
70 #define FLAG_PNGFILTER 4
71 #define FLAGMASK_UNUSED 0xf8
76 #define FOURCC_MSZH mmioFOURCC('M','S','Z','H')
77 #define FOURCC_ZLIB mmioFOURCC('Z','L','I','B')
82 typedef struct LclContext {
84 AVCodecContext *avctx;
94 // Decompressed data size
95 unsigned int decomp_size;
96 // Decompression buffer
97 unsigned char* decomp_buf;
98 // Maximum compressed data size
99 unsigned int max_comp_size;
100 // Compression buffer
101 unsigned char* comp_buf;
113 static inline unsigned char fix (int pix14)
117 tmp = (pix14 + 0x80000) >> 20;
127 static inline unsigned char get_b (unsigned char yq, signed char bq)
129 return fix((yq << 20) + bq * 1858076);
134 static inline unsigned char get_g (unsigned char yq, signed char bq, signed char rq)
136 return fix((yq << 20) - bq * 360857 - rq * 748830);
141 static inline unsigned char get_r (unsigned char yq, signed char rq)
143 return fix((yq << 20) + rq * 1470103);
148 static unsigned int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
150 unsigned char *destptr_bak = destptr;
151 unsigned char *destptr_end = destptr + destsize;
152 unsigned char mask = 0;
153 unsigned char maskbit = 0;
154 unsigned int ofs, cnt;
156 while ((srclen > 0) && (destptr < destptr_end)) {
163 if ((mask & (1 << (--maskbit))) == 0) {
164 if (destptr + 4 > destptr_end)
166 *(int*)destptr = *(int*)srcptr;
174 cnt = ((cnt >> 3) & 0x1f) + 1;
178 if (destptr + cnt > destptr_end) {
179 cnt = destptr_end - destptr;
181 for (; cnt > 0; cnt--) {
182 *(destptr) = *(destptr - ofs);
188 return (destptr - destptr_bak);
199 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
201 LclContext * const c = (LclContext *)avctx->priv_data;
202 unsigned char *encoded = (unsigned char *)buf;
203 unsigned int pixel_ptr;
205 unsigned char *outptr;
206 unsigned int width = avctx->width; // Real image width
207 unsigned int height = avctx->height; // Real image height
208 unsigned int mszh_dlen;
209 unsigned char yq, y1q, uq, vq;
211 unsigned int mthread_inlen, mthread_outlen;
213 int zret; // Zlib return code
215 unsigned int len = buf_size;
218 avctx->release_buffer(avctx, &c->pic);
220 c->pic.reference = 0;
221 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
222 if(avctx->get_buffer(avctx, &c->pic) < 0){
223 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
227 outptr = c->pic.data[0]; // Output image pointer
229 /* Decompress frame */
230 switch (avctx->codec_id) {
232 switch (c->compression) {
234 if (c->flags & FLAG_MULTITHREAD) {
235 mthread_inlen = *((unsigned int*)encoded);
236 mthread_outlen = *((unsigned int*)(encoded+4));
237 if (mthread_outlen > c->decomp_size) // this should not happen
238 mthread_outlen = c->decomp_size;
239 mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
240 if (mthread_outlen != mszh_dlen) {
241 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
242 mthread_outlen, mszh_dlen);
245 mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - mthread_inlen,
246 c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
247 if (mthread_outlen != mszh_dlen) {
248 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
249 mthread_outlen, mszh_dlen);
252 encoded = c->decomp_buf;
253 len = c->decomp_size;
255 mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
256 if (c->decomp_size != mszh_dlen) {
257 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
258 c->decomp_size, mszh_dlen);
261 encoded = c->decomp_buf;
265 case COMP_MSZH_NOCOMP:
268 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
274 /* Using the original dll with normal compression (-1) and RGB format
275 * gives a file with ZLIB fourcc, but frame is really uncompressed.
276 * To be sure that's true check also frame size */
277 if ((c->compression == COMP_ZLIB_NORMAL) && (c->imgtype == IMGTYPE_RGB24) &&
278 (len == width * height * 3))
280 zret = inflateReset(&(c->zstream));
282 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
285 if (c->flags & FLAG_MULTITHREAD) {
286 mthread_inlen = *((unsigned int*)encoded);
287 mthread_outlen = *((unsigned int*)(encoded+4));
288 if (mthread_outlen > c->decomp_size)
289 mthread_outlen = c->decomp_size;
290 c->zstream.next_in = encoded + 8;
291 c->zstream.avail_in = mthread_inlen;
292 c->zstream.next_out = c->decomp_buf;
293 c->zstream.avail_out = c->decomp_size;
294 zret = inflate(&(c->zstream), Z_FINISH);
295 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
296 av_log(avctx, AV_LOG_ERROR, "Mthread1 inflate error: %d\n", zret);
299 if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
300 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%u != %lu)\n",
301 mthread_outlen, c->zstream.total_out);
304 zret = inflateReset(&(c->zstream));
306 av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate reset error: %d\n", zret);
309 c->zstream.next_in = encoded + 8 + mthread_inlen;
310 c->zstream.avail_in = len - mthread_inlen;
311 c->zstream.next_out = c->decomp_buf + mthread_outlen;
312 c->zstream.avail_out = c->decomp_size - mthread_outlen;
313 zret = inflate(&(c->zstream), Z_FINISH);
314 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
315 av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate error: %d\n", zret);
318 if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
319 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %lu)\n",
320 mthread_outlen, c->zstream.total_out);
324 c->zstream.next_in = encoded;
325 c->zstream.avail_in = len;
326 c->zstream.next_out = c->decomp_buf;
327 c->zstream.avail_out = c->decomp_size;
328 zret = inflate(&(c->zstream), Z_FINISH);
329 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
330 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
333 if (c->decomp_size != (unsigned int)(c->zstream.total_out)) {
334 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
335 c->decomp_size, c->zstream.total_out);
339 encoded = c->decomp_buf;
340 len = c->decomp_size;;
342 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
347 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
352 /* Apply PNG filter */
353 if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) {
354 switch (c->imgtype) {
357 for (row = 0; row < height; row++) {
358 pixel_ptr = row * width * 3;
359 yq = encoded[pixel_ptr++];
360 uqvq = encoded[pixel_ptr++];
361 uqvq+=(encoded[pixel_ptr++] << 8);
362 for (col = 1; col < width; col++) {
363 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
364 uqvq -= (encoded[pixel_ptr+1] | (encoded[pixel_ptr+2]<<8));
365 encoded[pixel_ptr+1] = (uqvq) & 0xff;
366 encoded[pixel_ptr+2] = ((uqvq)>>8) & 0xff;
372 for (row = 0; row < height; row++) {
373 pixel_ptr = row * width * 2;
375 for (col = 0; col < width/4; col++) {
376 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
377 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
378 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
379 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
380 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
381 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
382 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
383 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
389 for (row = 0; row < height; row++) {
390 pixel_ptr = row * width / 2 * 3;
392 for (col = 0; col < width/4; col++) {
393 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
394 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
395 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
396 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
397 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
398 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
404 for (row = 0; row < height; row++) {
405 pixel_ptr = row * width * 2;
407 for (col = 0; col < width/2; col++) {
408 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
409 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
410 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
411 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
417 for (row = 0; row < height/2; row++) {
418 pixel_ptr = row * width * 3;
419 yq = y1q = uq = vq =0;
420 for (col = 0; col < width/2; col++) {
421 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
422 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
423 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
424 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
425 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
426 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
432 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
437 /* Convert colorspace */
438 switch (c->imgtype) {
440 for (row = height - 1; row >= 0; row--) {
441 pixel_ptr = row * c->pic.linesize[0];
442 for (col = 0; col < width; col++) {
443 outptr[pixel_ptr++] = get_b(encoded[0], encoded[1]);
444 outptr[pixel_ptr++] = get_g(encoded[0], encoded[1], encoded[2]);
445 outptr[pixel_ptr++] = get_r(encoded[0], encoded[2]);
451 for (row = height - 1; row >= 0; row--) {
452 pixel_ptr = row * c->pic.linesize[0];
453 for (col = 0; col < width/4; col++) {
454 outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
455 outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[6]);
456 outptr[pixel_ptr++] = get_r(encoded[0], encoded[6]);
457 outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
458 outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[6]);
459 outptr[pixel_ptr++] = get_r(encoded[1], encoded[6]);
460 outptr[pixel_ptr++] = get_b(encoded[2], encoded[5]);
461 outptr[pixel_ptr++] = get_g(encoded[2], encoded[5], encoded[7]);
462 outptr[pixel_ptr++] = get_r(encoded[2], encoded[7]);
463 outptr[pixel_ptr++] = get_b(encoded[3], encoded[5]);
464 outptr[pixel_ptr++] = get_g(encoded[3], encoded[5], encoded[7]);
465 outptr[pixel_ptr++] = get_r(encoded[3], encoded[7]);
471 for (row = height - 1; row >= 0; row--) {
472 pixel_ptr = row * c->pic.linesize[0];
473 for (col = 0; col < width; col++) {
474 outptr[pixel_ptr++] = encoded[0];
475 outptr[pixel_ptr++] = encoded[1];
476 outptr[pixel_ptr++] = encoded[2];
482 for (row = height - 1; row >= 0; row--) {
483 pixel_ptr = row * c->pic.linesize[0];
484 for (col = 0; col < width/4; col++) {
485 outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
486 outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[5]);
487 outptr[pixel_ptr++] = get_r(encoded[0], encoded[5]);
488 outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
489 outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[5]);
490 outptr[pixel_ptr++] = get_r(encoded[1], encoded[5]);
491 outptr[pixel_ptr++] = get_b(encoded[2], encoded[4]);
492 outptr[pixel_ptr++] = get_g(encoded[2], encoded[4], encoded[5]);
493 outptr[pixel_ptr++] = get_r(encoded[2], encoded[5]);
494 outptr[pixel_ptr++] = get_b(encoded[3], encoded[4]);
495 outptr[pixel_ptr++] = get_g(encoded[3], encoded[4], encoded[5]);
496 outptr[pixel_ptr++] = get_r(encoded[3], encoded[5]);
502 for (row = height - 1; row >= 0; row--) {
503 pixel_ptr = row * c->pic.linesize[0];
504 for (col = 0; col < width/2; col++) {
505 outptr[pixel_ptr++] = get_b(encoded[0], encoded[2]);
506 outptr[pixel_ptr++] = get_g(encoded[0], encoded[2], encoded[3]);
507 outptr[pixel_ptr++] = get_r(encoded[0], encoded[3]);
508 outptr[pixel_ptr++] = get_b(encoded[1], encoded[2]);
509 outptr[pixel_ptr++] = get_g(encoded[1], encoded[2], encoded[3]);
510 outptr[pixel_ptr++] = get_r(encoded[1], encoded[3]);
516 for (row = height / 2 - 1; row >= 0; row--) {
517 pixel_ptr = 2 * row * c->pic.linesize[0];
518 for (col = 0; col < width/2; col++) {
519 outptr[pixel_ptr] = get_b(encoded[0], encoded[4]);
520 outptr[pixel_ptr+1] = get_g(encoded[0], encoded[4], encoded[5]);
521 outptr[pixel_ptr+2] = get_r(encoded[0], encoded[5]);
522 outptr[pixel_ptr+3] = get_b(encoded[1], encoded[4]);
523 outptr[pixel_ptr+4] = get_g(encoded[1], encoded[4], encoded[5]);
524 outptr[pixel_ptr+5] = get_r(encoded[1], encoded[5]);
525 outptr[pixel_ptr-c->pic.linesize[0]] = get_b(encoded[2], encoded[4]);
526 outptr[pixel_ptr-c->pic.linesize[0]+1] = get_g(encoded[2], encoded[4], encoded[5]);
527 outptr[pixel_ptr-c->pic.linesize[0]+2] = get_r(encoded[2], encoded[5]);
528 outptr[pixel_ptr-c->pic.linesize[0]+3] = get_b(encoded[3], encoded[4]);
529 outptr[pixel_ptr-c->pic.linesize[0]+4] = get_g(encoded[3], encoded[4], encoded[5]);
530 outptr[pixel_ptr-c->pic.linesize[0]+5] = get_r(encoded[3], encoded[5]);
537 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
541 *data_size = sizeof(AVFrame);
542 *(AVFrame*)data = c->pic;
544 /* always report that the buffer was completely consumed */
555 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
556 LclContext *c = avctx->priv_data;
557 AVFrame *pict = data;
558 AVFrame * const p = &c->pic;
560 int zret; // Zlib return code
563 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled in.\n");
567 init_put_bits(&c->pb, buf, buf_size);
570 p->pict_type= FF_I_TYPE;
573 if(avctx->pix_fmt != PIX_FMT_BGR24){
574 av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
578 zret = deflateReset(&(c->zstream));
580 av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
583 c->zstream.next_out = c->comp_buf;
584 c->zstream.avail_out = c->max_comp_size;
586 for(i = avctx->height - 1; i >= 0; i--) {
587 c->zstream.next_in = p->data[0]+p->linesize[0]*i;
588 c->zstream.avail_in = avctx->width*3;
589 zret = deflate(&(c->zstream), Z_NO_FLUSH);
591 av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
595 zret = deflate(&(c->zstream), Z_FINISH);
596 if (zret != Z_STREAM_END) {
597 av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
601 for (i = 0; i < c->zstream.total_out; i++)
602 put_bits(&c->pb, 8, c->comp_buf[i]);
603 flush_put_bits(&c->pb);
605 return c->zstream.total_out;
616 static int decode_init(AVCodecContext *avctx)
618 LclContext * const c = (LclContext *)avctx->priv_data;
619 unsigned int basesize = avctx->width * avctx->height;
620 unsigned int max_basesize = ((avctx->width + 3) & ~3) * ((avctx->height + 3) & ~3);
621 unsigned int max_decomp_size;
622 int zret; // Zlib return code
625 avctx->has_b_frames = 0;
627 c->pic.data[0] = NULL;
630 // Needed if zlib unused or init aborted before inflateInit
631 memset(&(c->zstream), 0, sizeof(z_stream));
634 if (avctx->extradata_size < 8) {
635 av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
639 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
643 /* Check codec type */
644 if (((avctx->codec_id == CODEC_ID_MSZH) && (*((char *)avctx->extradata + 7) != CODEC_MSZH)) ||
645 ((avctx->codec_id == CODEC_ID_ZLIB) && (*((char *)avctx->extradata + 7) != CODEC_ZLIB))) {
646 av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
649 /* Detect image type */
650 switch (c->imgtype = *((char *)avctx->extradata + 4)) {
652 c->decomp_size = basesize * 3;
653 max_decomp_size = max_basesize * 3;
654 av_log(avctx, AV_LOG_INFO, "Image type is YUV 1:1:1.\n");
657 c->decomp_size = basesize * 2;
658 max_decomp_size = max_basesize * 2;
659 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:2.\n");
662 c->decomp_size = basesize * 3;
663 max_decomp_size = max_basesize * 3;
664 av_log(avctx, AV_LOG_INFO, "Image type is RGB 24.\n");
667 c->decomp_size = basesize / 2 * 3;
668 max_decomp_size = max_basesize / 2 * 3;
669 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:1:1.\n");
672 c->decomp_size = basesize * 2;
673 max_decomp_size = max_basesize * 2;
674 av_log(avctx, AV_LOG_INFO, "Image type is YUV 2:1:1.\n");
677 c->decomp_size = basesize / 2 * 3;
678 max_decomp_size = max_basesize / 2 * 3;
679 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:0.\n");
682 av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
686 /* Detect compression method */
687 c->compression = *((char *)avctx->extradata + 5);
688 switch (avctx->codec_id) {
690 switch (c->compression) {
692 av_log(avctx, AV_LOG_INFO, "Compression enabled.\n");
694 case COMP_MSZH_NOCOMP:
696 av_log(avctx, AV_LOG_INFO, "No compression.\n");
699 av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
705 switch (c->compression) {
706 case COMP_ZLIB_HISPEED:
707 av_log(avctx, AV_LOG_INFO, "High speed compression.\n");
709 case COMP_ZLIB_HICOMP:
710 av_log(avctx, AV_LOG_INFO, "High compression.\n");
712 case COMP_ZLIB_NORMAL:
713 av_log(avctx, AV_LOG_INFO, "Normal compression.\n");
716 if ((c->compression < Z_NO_COMPRESSION) || (c->compression > Z_BEST_COMPRESSION)) {
717 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
720 av_log(avctx, AV_LOG_INFO, "Compression level for ZLIB: (%d).\n", c->compression);
723 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
728 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
732 /* Allocate decompression buffer */
733 if (c->decomp_size) {
734 if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
735 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
741 c->flags = *((char *)avctx->extradata + 6);
742 if (c->flags & FLAG_MULTITHREAD)
743 av_log(avctx, AV_LOG_INFO, "Multithread encoder flag set.\n");
744 if (c->flags & FLAG_NULLFRAME)
745 av_log(avctx, AV_LOG_INFO, "Nullframe insertion flag set.\n");
746 if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER))
747 av_log(avctx, AV_LOG_INFO, "PNG filter flag set.\n");
748 if (c->flags & FLAGMASK_UNUSED)
749 av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
751 /* If needed init zlib */
752 if (avctx->codec_id == CODEC_ID_ZLIB) {
754 c->zstream.zalloc = Z_NULL;
755 c->zstream.zfree = Z_NULL;
756 c->zstream.opaque = Z_NULL;
757 zret = inflateInit(&(c->zstream));
759 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
763 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
768 avctx->pix_fmt = PIX_FMT_BGR24;
780 static int encode_init(AVCodecContext *avctx)
782 LclContext *c = avctx->priv_data;
783 int zret; // Zlib return code
786 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
792 assert(avctx->width && avctx->height);
794 avctx->extradata= av_mallocz(8);
795 avctx->coded_frame= &c->pic;
797 // Will be user settable someday
801 switch(avctx->pix_fmt){
803 c->imgtype = IMGTYPE_RGB24;
804 c->decomp_size = avctx->width * avctx->height * 3;
805 avctx->bits_per_sample= 24;
808 av_log(avctx, AV_LOG_ERROR, "Format %d not supported\n", avctx->pix_fmt);
812 ((uint8_t*)avctx->extradata)[0]= 4;
813 ((uint8_t*)avctx->extradata)[1]= 0;
814 ((uint8_t*)avctx->extradata)[2]= 0;
815 ((uint8_t*)avctx->extradata)[3]= 0;
816 ((uint8_t*)avctx->extradata)[4]= c->imgtype;
817 ((uint8_t*)avctx->extradata)[5]= c->compression;
818 ((uint8_t*)avctx->extradata)[6]= c->flags;
819 ((uint8_t*)avctx->extradata)[7]= CODEC_ZLIB;
820 c->avctx->extradata_size= 8;
822 c->zstream.zalloc = Z_NULL;
823 c->zstream.zfree = Z_NULL;
824 c->zstream.opaque = Z_NULL;
825 zret = deflateInit(&(c->zstream), c->compression);
827 av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret);
831 /* Conservative upper bound taken from zlib v1.2.1 source */
832 c->max_comp_size = c->decomp_size + ((c->decomp_size + 7) >> 3) +
833 ((c->decomp_size + 63) >> 6) + 11;
834 if ((c->comp_buf = av_malloc(c->max_comp_size)) == NULL) {
835 av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
852 static int decode_end(AVCodecContext *avctx)
854 LclContext * const c = (LclContext *)avctx->priv_data;
857 avctx->release_buffer(avctx, &c->pic);
859 inflateEnd(&(c->zstream));
872 static int encode_end(AVCodecContext *avctx)
874 LclContext *c = avctx->priv_data;
876 av_freep(&avctx->extradata);
877 av_freep(&c->comp_buf);
879 deflateEnd(&(c->zstream));
885 AVCodec mszh_decoder = {
898 AVCodec zlib_decoder = {
910 #ifdef CONFIG_ENCODERS
912 AVCodec zlib_encoder = {
922 #endif //CONFIG_ENCODERS