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)
54 #define IMGTYPE_YUV111 0
55 #define IMGTYPE_YUV422 1
56 #define IMGTYPE_RGB24 2
57 #define IMGTYPE_YUV411 3
58 #define IMGTYPE_YUV211 4
59 #define IMGTYPE_YUV420 5
62 #define COMP_MSZH_NOCOMP 1
63 #define COMP_ZLIB_HISPEED 1
64 #define COMP_ZLIB_HICOMP 9
65 #define COMP_ZLIB_NORMAL -1
67 #define FLAG_MULTITHREAD 1
68 #define FLAG_NULLFRAME 2
69 #define FLAG_PNGFILTER 4
70 #define FLAGMASK_UNUSED 0xf8
75 #define FOURCC_MSZH mmioFOURCC('M','S','Z','H')
76 #define FOURCC_ZLIB mmioFOURCC('Z','L','I','B')
81 typedef struct LclContext {
83 AVCodecContext *avctx;
93 // Decompressed data size
94 unsigned int decomp_size;
95 // Decompression buffer
96 unsigned char* decomp_buf;
97 // Maximum compressed data size
98 unsigned int max_comp_size;
100 unsigned char* comp_buf;
112 static inline unsigned char fix (int pix14)
116 tmp = (pix14 + 0x80000) >> 20;
126 static inline unsigned char get_b (unsigned char yq, signed char bq)
128 return fix((yq << 20) + bq * 1858076);
133 static inline unsigned char get_g (unsigned char yq, signed char bq, signed char rq)
135 return fix((yq << 20) - bq * 360857 - rq * 748830);
140 static inline unsigned char get_r (unsigned char yq, signed char rq)
142 return fix((yq << 20) + rq * 1470103);
147 static int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr)
149 unsigned char *destptr_bak = destptr;
150 unsigned char mask = 0;
151 unsigned char maskbit = 0;
152 unsigned int ofs, cnt;
161 if ((mask & (1 << (--maskbit))) == 0) {
162 *(int*)destptr = *(int*)srcptr;
170 cnt = ((cnt >> 3) & 0x1f) + 1;
174 for (; cnt > 0; cnt--) {
175 *(destptr) = *(destptr - ofs);
181 return (destptr - destptr_bak);
192 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
194 LclContext * const c = (LclContext *)avctx->priv_data;
195 unsigned char *encoded = (unsigned char *)buf;
198 unsigned char *outptr;
199 unsigned int width = avctx->width; // Real image width
200 unsigned int height = avctx->height; // Real image height
201 unsigned int mszh_dlen;
202 unsigned char yq, y1q, uq, vq;
204 unsigned int mthread_inlen, mthread_outlen;
206 int zret; // Zlib return code
210 /* no supplementary picture */
215 avctx->release_buffer(avctx, &c->pic);
217 c->pic.reference = 0;
218 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
219 if(avctx->get_buffer(avctx, &c->pic) < 0){
220 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
224 outptr = c->pic.data[0]; // Output image pointer
226 /* Decompress frame */
227 switch (avctx->codec_id) {
229 switch (c->compression) {
231 if (c->flags & FLAG_MULTITHREAD) {
232 mthread_inlen = *((unsigned int*)encoded);
233 mthread_outlen = *((unsigned int*)(encoded+4));
234 mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf);
235 if (mthread_outlen != mszh_dlen) {
236 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
237 mthread_outlen, mszh_dlen);
239 mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - mthread_inlen,
240 c->decomp_buf + mthread_outlen);
241 if ((c->decomp_size - mthread_outlen) != mszh_dlen) {
242 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
243 c->decomp_size - mthread_outlen, mszh_dlen);
245 encoded = c->decomp_buf;
246 len = c->decomp_size;
248 mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf);
249 if (c->decomp_size != mszh_dlen) {
250 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
251 c->decomp_size, mszh_dlen);
253 encoded = c->decomp_buf;
257 case COMP_MSZH_NOCOMP:
260 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
266 /* Using the original dll with normal compression (-1) and RGB format
267 * gives a file with ZLIB fourcc, but frame is really uncompressed.
268 * To be sure that's true check also frame size */
269 if ((c->compression == COMP_ZLIB_NORMAL) && (c->imgtype == IMGTYPE_RGB24) &&
270 (len == width * height * 3))
272 zret = inflateReset(&(c->zstream));
274 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
277 if (c->flags & FLAG_MULTITHREAD) {
278 mthread_inlen = *((unsigned int*)encoded);
279 mthread_outlen = *((unsigned int*)(encoded+4));
280 c->zstream.next_in = encoded + 8;
281 c->zstream.avail_in = mthread_inlen;
282 c->zstream.next_out = c->decomp_buf;
283 c->zstream.avail_out = mthread_outlen;
284 zret = inflate(&(c->zstream), Z_FINISH);
285 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
286 av_log(avctx, AV_LOG_ERROR, "Mthread1 inflate error: %d\n", zret);
289 if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
290 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%u != %lu)\n",
291 mthread_outlen, c->zstream.total_out);
293 zret = inflateReset(&(c->zstream));
295 av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate reset error: %d\n", zret);
298 c->zstream.next_in = encoded + 8 + mthread_inlen;
299 c->zstream.avail_in = len - mthread_inlen;
300 c->zstream.next_out = c->decomp_buf + mthread_outlen;
301 c->zstream.avail_out = mthread_outlen;
302 zret = inflate(&(c->zstream), Z_FINISH);
303 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
304 av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate error: %d\n", zret);
307 if ((c->decomp_size - mthread_outlen) != (unsigned int)(c->zstream.total_out)) {
308 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %lu)\n",
309 c->decomp_size - mthread_outlen, c->zstream.total_out);
312 c->zstream.next_in = encoded;
313 c->zstream.avail_in = len;
314 c->zstream.next_out = c->decomp_buf;
315 c->zstream.avail_out = c->decomp_size;
316 zret = inflate(&(c->zstream), Z_FINISH);
317 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
318 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
321 if (c->decomp_size != (unsigned int)(c->zstream.total_out)) {
322 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
323 c->decomp_size, c->zstream.total_out);
326 encoded = c->decomp_buf;
327 len = c->decomp_size;;
329 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
334 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
339 /* Apply PNG filter */
340 if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) {
341 switch (c->imgtype) {
344 for (row = 0; row < height; row++) {
345 pixel_ptr = row * width * 3;
346 yq = encoded[pixel_ptr++];
347 uqvq = encoded[pixel_ptr++];
348 uqvq+=(encoded[pixel_ptr++] << 8);
349 for (col = 1; col < width; col++) {
350 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
351 uqvq -= (encoded[pixel_ptr+1] | (encoded[pixel_ptr+2]<<8));
352 encoded[pixel_ptr+1] = (uqvq) & 0xff;
353 encoded[pixel_ptr+2] = ((uqvq)>>8) & 0xff;
359 for (row = 0; row < height; row++) {
360 pixel_ptr = row * width * 2;
362 for (col = 0; col < width/4; col++) {
363 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
364 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
365 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
366 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
367 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
368 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
369 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
370 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
376 for (row = 0; row < height; row++) {
377 pixel_ptr = row * width / 2 * 3;
379 for (col = 0; col < width/4; col++) {
380 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
381 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
382 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
383 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
384 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
385 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
391 for (row = 0; row < height; row++) {
392 pixel_ptr = row * width * 2;
394 for (col = 0; col < width/2; col++) {
395 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
396 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
397 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
398 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
404 for (row = 0; row < height/2; row++) {
405 pixel_ptr = row * width * 3;
406 yq = y1q = uq = vq =0;
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] = y1q -= encoded[pixel_ptr+2];
411 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
412 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
413 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
419 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
424 /* Convert colorspace */
425 switch (c->imgtype) {
427 for (row = height - 1; row >= 0; row--) {
428 pixel_ptr = row * c->pic.linesize[0];
429 for (col = 0; col < width; col++) {
430 outptr[pixel_ptr++] = get_b(encoded[0], encoded[1]);
431 outptr[pixel_ptr++] = get_g(encoded[0], encoded[1], encoded[2]);
432 outptr[pixel_ptr++] = get_r(encoded[0], encoded[2]);
438 for (row = height - 1; row >= 0; row--) {
439 pixel_ptr = row * c->pic.linesize[0];
440 for (col = 0; col < width/4; col++) {
441 outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
442 outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[6]);
443 outptr[pixel_ptr++] = get_r(encoded[0], encoded[6]);
444 outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
445 outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[6]);
446 outptr[pixel_ptr++] = get_r(encoded[1], encoded[6]);
447 outptr[pixel_ptr++] = get_b(encoded[2], encoded[5]);
448 outptr[pixel_ptr++] = get_g(encoded[2], encoded[5], encoded[7]);
449 outptr[pixel_ptr++] = get_r(encoded[2], encoded[7]);
450 outptr[pixel_ptr++] = get_b(encoded[3], encoded[5]);
451 outptr[pixel_ptr++] = get_g(encoded[3], encoded[5], encoded[7]);
452 outptr[pixel_ptr++] = get_r(encoded[3], encoded[7]);
458 for (row = height - 1; row >= 0; row--) {
459 pixel_ptr = row * c->pic.linesize[0];
460 for (col = 0; col < width; col++) {
461 outptr[pixel_ptr++] = encoded[0];
462 outptr[pixel_ptr++] = encoded[1];
463 outptr[pixel_ptr++] = encoded[2];
469 for (row = height - 1; row >= 0; row--) {
470 pixel_ptr = row * c->pic.linesize[0];
471 for (col = 0; col < width/4; col++) {
472 outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
473 outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[5]);
474 outptr[pixel_ptr++] = get_r(encoded[0], encoded[5]);
475 outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
476 outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[5]);
477 outptr[pixel_ptr++] = get_r(encoded[1], encoded[5]);
478 outptr[pixel_ptr++] = get_b(encoded[2], encoded[4]);
479 outptr[pixel_ptr++] = get_g(encoded[2], encoded[4], encoded[5]);
480 outptr[pixel_ptr++] = get_r(encoded[2], encoded[5]);
481 outptr[pixel_ptr++] = get_b(encoded[3], encoded[4]);
482 outptr[pixel_ptr++] = get_g(encoded[3], encoded[4], encoded[5]);
483 outptr[pixel_ptr++] = get_r(encoded[3], encoded[5]);
489 for (row = height - 1; row >= 0; row--) {
490 pixel_ptr = row * c->pic.linesize[0];
491 for (col = 0; col < width/2; col++) {
492 outptr[pixel_ptr++] = get_b(encoded[0], encoded[2]);
493 outptr[pixel_ptr++] = get_g(encoded[0], encoded[2], encoded[3]);
494 outptr[pixel_ptr++] = get_r(encoded[0], encoded[3]);
495 outptr[pixel_ptr++] = get_b(encoded[1], encoded[2]);
496 outptr[pixel_ptr++] = get_g(encoded[1], encoded[2], encoded[3]);
497 outptr[pixel_ptr++] = get_r(encoded[1], encoded[3]);
503 for (row = height / 2 - 1; row >= 0; row--) {
504 pixel_ptr = 2 * row * c->pic.linesize[0];
505 for (col = 0; col < width/2; col++) {
506 outptr[pixel_ptr] = get_b(encoded[0], encoded[4]);
507 outptr[pixel_ptr+1] = get_g(encoded[0], encoded[4], encoded[5]);
508 outptr[pixel_ptr+2] = get_r(encoded[0], encoded[5]);
509 outptr[pixel_ptr+3] = get_b(encoded[1], encoded[4]);
510 outptr[pixel_ptr+4] = get_g(encoded[1], encoded[4], encoded[5]);
511 outptr[pixel_ptr+5] = get_r(encoded[1], encoded[5]);
512 outptr[pixel_ptr-c->pic.linesize[0]] = get_b(encoded[2], encoded[4]);
513 outptr[pixel_ptr-c->pic.linesize[0]+1] = get_g(encoded[2], encoded[4], encoded[5]);
514 outptr[pixel_ptr-c->pic.linesize[0]+2] = get_r(encoded[2], encoded[5]);
515 outptr[pixel_ptr-c->pic.linesize[0]+3] = get_b(encoded[3], encoded[4]);
516 outptr[pixel_ptr-c->pic.linesize[0]+4] = get_g(encoded[3], encoded[4], encoded[5]);
517 outptr[pixel_ptr-c->pic.linesize[0]+5] = get_r(encoded[3], encoded[5]);
524 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
528 *data_size = sizeof(AVFrame);
529 *(AVFrame*)data = c->pic;
531 /* always report that the buffer was completely consumed */
542 static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
543 LclContext *c = avctx->priv_data;
544 AVFrame *pict = data;
545 AVFrame * const p = &c->pic;
547 int zret; // Zlib return code
550 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled in.\n");
554 init_put_bits(&c->pb, buf, buf_size);
557 p->pict_type= FF_I_TYPE;
560 if(avctx->pix_fmt != PIX_FMT_BGR24){
561 av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
565 zret = deflateReset(&(c->zstream));
567 av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
570 c->zstream.next_in = p->data[0];
571 c->zstream.avail_in = c->decomp_size;
572 c->zstream.next_out = c->comp_buf;
573 c->zstream.avail_out = c->max_comp_size;
575 zret = deflate(&(c->zstream), Z_FINISH);
576 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
577 av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
581 for (i = 0; i < c->zstream.total_out; i++)
582 put_bits(&c->pb, 8, c->comp_buf[i]);
583 flush_put_bits(&c->pb);
585 return c->zstream.total_out;
596 static int decode_init(AVCodecContext *avctx)
598 LclContext * const c = (LclContext *)avctx->priv_data;
599 int basesize = avctx->width * avctx->height;
600 int zret; // Zlib return code
603 avctx->has_b_frames = 0;
605 c->pic.data[0] = NULL;
608 // Needed if zlib unused or init aborted before inflateInit
609 memset(&(c->zstream), 0, sizeof(z_stream));
612 if (avctx->extradata_size < 8) {
613 av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
617 /* Check codec type */
618 if (((avctx->codec_id == CODEC_ID_MSZH) && (*((char *)avctx->extradata + 7) != CODEC_MSZH)) ||
619 ((avctx->codec_id == CODEC_ID_ZLIB) && (*((char *)avctx->extradata + 7) != CODEC_ZLIB))) {
620 av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
623 /* Detect image type */
624 switch (c->imgtype = *((char *)avctx->extradata + 4)) {
626 c->decomp_size = basesize * 3;
627 av_log(avctx, AV_LOG_INFO, "Image type is YUV 1:1:1.\n");
630 c->decomp_size = basesize * 2;
631 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:2.\n");
634 c->decomp_size = basesize * 3;
635 av_log(avctx, AV_LOG_INFO, "Image type is RGB 24.\n");
638 c->decomp_size = basesize / 2 * 3;
639 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:1:1.\n");
642 c->decomp_size = basesize * 2;
643 av_log(avctx, AV_LOG_INFO, "Image type is YUV 2:1:1.\n");
646 c->decomp_size = basesize / 2 * 3;
647 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:0.\n");
650 av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
654 /* Detect compression method */
655 c->compression = *((char *)avctx->extradata + 5);
656 switch (avctx->codec_id) {
658 switch (c->compression) {
660 av_log(avctx, AV_LOG_INFO, "Compression enabled.\n");
662 case COMP_MSZH_NOCOMP:
664 av_log(avctx, AV_LOG_INFO, "No compression.\n");
667 av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
673 switch (c->compression) {
674 case COMP_ZLIB_HISPEED:
675 av_log(avctx, AV_LOG_INFO, "High speed compression.\n");
677 case COMP_ZLIB_HICOMP:
678 av_log(avctx, AV_LOG_INFO, "High compression.\n");
680 case COMP_ZLIB_NORMAL:
681 av_log(avctx, AV_LOG_INFO, "Normal compression.\n");
684 if ((c->compression < Z_NO_COMPRESSION) || (c->compression > Z_BEST_COMPRESSION)) {
685 av_log(avctx, AV_LOG_ERROR, "Unusupported compression level for ZLIB: (%d).\n", c->compression);
688 av_log(avctx, AV_LOG_INFO, "Compression level for ZLIB: (%d).\n", c->compression);
691 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
696 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
700 /* Allocate decompression buffer */
701 /* 4*8 max overflow space for mszh decomp algorithm */
702 if (c->decomp_size) {
703 if ((c->decomp_buf = av_malloc(c->decomp_size+4*8)) == NULL) {
704 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
710 c->flags = *((char *)avctx->extradata + 6);
711 if (c->flags & FLAG_MULTITHREAD)
712 av_log(avctx, AV_LOG_INFO, "Multithread encoder flag set.\n");
713 if (c->flags & FLAG_NULLFRAME)
714 av_log(avctx, AV_LOG_INFO, "Nullframe insertion flag set.\n");
715 if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER))
716 av_log(avctx, AV_LOG_INFO, "PNG filter flag set.\n");
717 if (c->flags & FLAGMASK_UNUSED)
718 av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
720 /* If needed init zlib */
721 if (avctx->codec_id == CODEC_ID_ZLIB) {
723 c->zstream.zalloc = Z_NULL;
724 c->zstream.zfree = Z_NULL;
725 c->zstream.opaque = Z_NULL;
726 zret = inflateInit(&(c->zstream));
728 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
732 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
737 avctx->pix_fmt = PIX_FMT_BGR24;
749 static int encode_init(AVCodecContext *avctx)
751 LclContext *c = avctx->priv_data;
752 int zret; // Zlib return code
755 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
761 assert(avctx->width && avctx->height);
763 avctx->extradata= av_mallocz(8);
764 avctx->coded_frame= &c->pic;
766 // Will be user settable someday
770 switch(avctx->pix_fmt){
772 c->imgtype = IMGTYPE_RGB24;
773 c->decomp_size = avctx->width * avctx->height * 3;
774 avctx->bits_per_sample= 24;
777 av_log(avctx, AV_LOG_ERROR, "Format %d not supported\n", avctx->pix_fmt);
781 ((uint8_t*)avctx->extradata)[0]= 4;
782 ((uint8_t*)avctx->extradata)[1]= 0;
783 ((uint8_t*)avctx->extradata)[2]= 0;
784 ((uint8_t*)avctx->extradata)[3]= 0;
785 ((uint8_t*)avctx->extradata)[4]= c->imgtype;
786 ((uint8_t*)avctx->extradata)[5]= c->compression;
787 ((uint8_t*)avctx->extradata)[6]= c->flags;
788 ((uint8_t*)avctx->extradata)[7]= 0;
789 c->avctx->extradata_size= 8;
791 c->zstream.zalloc = Z_NULL;
792 c->zstream.zfree = Z_NULL;
793 c->zstream.opaque = Z_NULL;
794 zret = deflateInit(&(c->zstream), c->compression);
796 av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret);
800 /* Conservative upper bound taken from zlib v1.2.1 source */
801 c->max_comp_size = c->decomp_size + ((c->decomp_size + 7) >> 3) +
802 ((c->decomp_size + 63) >> 6) + 11;
803 if ((c->comp_buf = av_malloc(c->max_comp_size)) == NULL) {
804 av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
821 static int decode_end(AVCodecContext *avctx)
823 LclContext * const c = (LclContext *)avctx->priv_data;
826 avctx->release_buffer(avctx, &c->pic);
828 inflateEnd(&(c->zstream));
841 static int encode_end(AVCodecContext *avctx)
843 LclContext *c = avctx->priv_data;
845 av_freep(&avctx->extradata);
846 av_freep(c->comp_buf);
848 deflateEnd(&(c->zstream));
854 AVCodec mszh_decoder = {
867 AVCodec zlib_decoder = {
879 #ifdef CONFIG_ENCODERS
881 AVCodec zlib_encoder = {
889 // .options = lcl_options,
892 #endif //CONFIG_ENCODERS