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
24 * LCL (LossLess Codec Library) Video Codec
25 * Decoder for MSZH and ZLIB codecs
26 * Experimental encoder for ZLIB RGB24
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)
45 #include "bitstream.h"
55 typedef struct LclDecContext {
57 AVCodecContext *avctx;
66 // Decompressed data size
67 unsigned int decomp_size;
68 // Decompression buffer
69 unsigned char* decomp_buf;
81 static inline unsigned char fix (int pix14)
85 tmp = (pix14 + 0x80000) >> 20;
95 static inline unsigned char get_b (unsigned char yq, signed char bq)
97 return fix((yq << 20) + bq * 1858076);
102 static inline unsigned char get_g (unsigned char yq, signed char bq, signed char rq)
104 return fix((yq << 20) - bq * 360857 - rq * 748830);
109 static inline unsigned char get_r (unsigned char yq, signed char rq)
111 return fix((yq << 20) + rq * 1470103);
116 static unsigned int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
118 unsigned char *destptr_bak = destptr;
119 unsigned char *destptr_end = destptr + destsize;
120 unsigned char mask = 0;
121 unsigned char maskbit = 0;
122 unsigned int ofs, cnt;
124 while ((srclen > 0) && (destptr < destptr_end)) {
131 if ((mask & (1 << (--maskbit))) == 0) {
132 if (destptr + 4 > destptr_end)
134 *(int*)destptr = *(int*)srcptr;
142 cnt = ((cnt >> 3) & 0x1f) + 1;
146 if (destptr + cnt > destptr_end) {
147 cnt = destptr_end - destptr;
149 for (; cnt > 0; cnt--) {
150 *(destptr) = *(destptr - ofs);
156 return (destptr - destptr_bak);
166 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
168 LclDecContext * const c = avctx->priv_data;
169 unsigned char *encoded = (unsigned char *)buf;
170 unsigned int pixel_ptr;
172 unsigned char *outptr;
173 unsigned int width = avctx->width; // Real image width
174 unsigned int height = avctx->height; // Real image height
175 unsigned int mszh_dlen;
176 unsigned char yq, y1q, uq, vq;
178 unsigned int mthread_inlen, mthread_outlen;
180 int zret; // Zlib return code
182 unsigned int len = buf_size;
185 avctx->release_buffer(avctx, &c->pic);
187 c->pic.reference = 0;
188 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
189 if(avctx->get_buffer(avctx, &c->pic) < 0){
190 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
194 outptr = c->pic.data[0]; // Output image pointer
196 /* Decompress frame */
197 switch (avctx->codec_id) {
199 switch (c->compression) {
201 if (c->flags & FLAG_MULTITHREAD) {
202 mthread_inlen = *((unsigned int*)encoded);
203 mthread_outlen = *((unsigned int*)(encoded+4));
204 if (mthread_outlen > c->decomp_size) // this should not happen
205 mthread_outlen = c->decomp_size;
206 mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
207 if (mthread_outlen != mszh_dlen) {
208 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
209 mthread_outlen, mszh_dlen);
212 mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - mthread_inlen,
213 c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
214 if (mthread_outlen != mszh_dlen) {
215 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
216 mthread_outlen, mszh_dlen);
219 encoded = c->decomp_buf;
220 len = c->decomp_size;
222 mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
223 if (c->decomp_size != mszh_dlen) {
224 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
225 c->decomp_size, mszh_dlen);
228 encoded = c->decomp_buf;
232 case COMP_MSZH_NOCOMP:
235 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
241 /* Using the original dll with normal compression (-1) and RGB format
242 * gives a file with ZLIB fourcc, but frame is really uncompressed.
243 * To be sure that's true check also frame size */
244 if ((c->compression == COMP_ZLIB_NORMAL) && (c->imgtype == IMGTYPE_RGB24) &&
245 (len == width * height * 3))
247 zret = inflateReset(&(c->zstream));
249 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
252 if (c->flags & FLAG_MULTITHREAD) {
253 mthread_inlen = *((unsigned int*)encoded);
254 mthread_outlen = *((unsigned int*)(encoded+4));
255 if (mthread_outlen > c->decomp_size)
256 mthread_outlen = c->decomp_size;
257 c->zstream.next_in = encoded + 8;
258 c->zstream.avail_in = mthread_inlen;
259 c->zstream.next_out = c->decomp_buf;
260 c->zstream.avail_out = c->decomp_size;
261 zret = inflate(&(c->zstream), Z_FINISH);
262 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
263 av_log(avctx, AV_LOG_ERROR, "Mthread1 inflate error: %d\n", zret);
266 if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
267 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%u != %lu)\n",
268 mthread_outlen, c->zstream.total_out);
271 zret = inflateReset(&(c->zstream));
273 av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate reset error: %d\n", zret);
276 c->zstream.next_in = encoded + 8 + mthread_inlen;
277 c->zstream.avail_in = len - mthread_inlen;
278 c->zstream.next_out = c->decomp_buf + mthread_outlen;
279 c->zstream.avail_out = c->decomp_size - mthread_outlen;
280 zret = inflate(&(c->zstream), Z_FINISH);
281 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
282 av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate error: %d\n", zret);
285 if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
286 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %lu)\n",
287 mthread_outlen, c->zstream.total_out);
291 c->zstream.next_in = encoded;
292 c->zstream.avail_in = len;
293 c->zstream.next_out = c->decomp_buf;
294 c->zstream.avail_out = c->decomp_size;
295 zret = inflate(&(c->zstream), Z_FINISH);
296 if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
297 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
300 if (c->decomp_size != (unsigned int)(c->zstream.total_out)) {
301 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
302 c->decomp_size, c->zstream.total_out);
306 encoded = c->decomp_buf;
307 len = c->decomp_size;;
309 av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
314 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
319 /* Apply PNG filter */
320 if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) {
321 switch (c->imgtype) {
324 for (row = 0; row < height; row++) {
325 pixel_ptr = row * width * 3;
326 yq = encoded[pixel_ptr++];
327 uqvq = AV_RL16(encoded+pixel_ptr);
329 for (col = 1; col < width; col++) {
330 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
331 uqvq -= AV_RL16(encoded+pixel_ptr+1);
332 AV_WL16(encoded+pixel_ptr+1, uqvq);
338 for (row = 0; row < height; row++) {
339 pixel_ptr = row * width * 2;
341 for (col = 0; col < width/4; col++) {
342 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
343 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
344 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
345 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
346 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
347 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
348 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
349 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
355 for (row = 0; row < height; row++) {
356 pixel_ptr = row * width / 2 * 3;
358 for (col = 0; col < width/4; col++) {
359 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
360 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
361 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
362 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
363 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
364 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
370 for (row = 0; row < height; row++) {
371 pixel_ptr = row * width * 2;
373 for (col = 0; col < width/2; col++) {
374 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
375 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
376 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
377 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
383 for (row = 0; row < height/2; row++) {
384 pixel_ptr = row * width * 3;
385 yq = y1q = uq = vq =0;
386 for (col = 0; col < width/2; col++) {
387 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
388 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
389 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
390 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
391 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
392 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
398 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
403 /* Convert colorspace */
404 switch (c->imgtype) {
406 for (row = height - 1; row >= 0; row--) {
407 pixel_ptr = row * c->pic.linesize[0];
408 for (col = 0; col < width; col++) {
409 outptr[pixel_ptr++] = get_b(encoded[0], encoded[1]);
410 outptr[pixel_ptr++] = get_g(encoded[0], encoded[1], encoded[2]);
411 outptr[pixel_ptr++] = get_r(encoded[0], encoded[2]);
417 for (row = height - 1; row >= 0; row--) {
418 pixel_ptr = row * c->pic.linesize[0];
419 for (col = 0; col < width/4; col++) {
420 outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
421 outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[6]);
422 outptr[pixel_ptr++] = get_r(encoded[0], encoded[6]);
423 outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
424 outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[6]);
425 outptr[pixel_ptr++] = get_r(encoded[1], encoded[6]);
426 outptr[pixel_ptr++] = get_b(encoded[2], encoded[5]);
427 outptr[pixel_ptr++] = get_g(encoded[2], encoded[5], encoded[7]);
428 outptr[pixel_ptr++] = get_r(encoded[2], encoded[7]);
429 outptr[pixel_ptr++] = get_b(encoded[3], encoded[5]);
430 outptr[pixel_ptr++] = get_g(encoded[3], encoded[5], encoded[7]);
431 outptr[pixel_ptr++] = get_r(encoded[3], encoded[7]);
437 for (row = height - 1; row >= 0; row--) {
438 pixel_ptr = row * c->pic.linesize[0];
439 for (col = 0; col < width; col++) {
440 outptr[pixel_ptr++] = encoded[0];
441 outptr[pixel_ptr++] = encoded[1];
442 outptr[pixel_ptr++] = encoded[2];
448 for (row = height - 1; row >= 0; row--) {
449 pixel_ptr = row * c->pic.linesize[0];
450 for (col = 0; col < width/4; col++) {
451 outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
452 outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[5]);
453 outptr[pixel_ptr++] = get_r(encoded[0], encoded[5]);
454 outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
455 outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[5]);
456 outptr[pixel_ptr++] = get_r(encoded[1], encoded[5]);
457 outptr[pixel_ptr++] = get_b(encoded[2], encoded[4]);
458 outptr[pixel_ptr++] = get_g(encoded[2], encoded[4], encoded[5]);
459 outptr[pixel_ptr++] = get_r(encoded[2], encoded[5]);
460 outptr[pixel_ptr++] = get_b(encoded[3], encoded[4]);
461 outptr[pixel_ptr++] = get_g(encoded[3], encoded[4], encoded[5]);
462 outptr[pixel_ptr++] = get_r(encoded[3], encoded[5]);
468 for (row = height - 1; row >= 0; row--) {
469 pixel_ptr = row * c->pic.linesize[0];
470 for (col = 0; col < width/2; col++) {
471 outptr[pixel_ptr++] = get_b(encoded[0], encoded[2]);
472 outptr[pixel_ptr++] = get_g(encoded[0], encoded[2], encoded[3]);
473 outptr[pixel_ptr++] = get_r(encoded[0], encoded[3]);
474 outptr[pixel_ptr++] = get_b(encoded[1], encoded[2]);
475 outptr[pixel_ptr++] = get_g(encoded[1], encoded[2], encoded[3]);
476 outptr[pixel_ptr++] = get_r(encoded[1], encoded[3]);
482 for (row = height / 2 - 1; row >= 0; row--) {
483 pixel_ptr = 2 * row * c->pic.linesize[0];
484 for (col = 0; col < width/2; col++) {
485 outptr[pixel_ptr] = get_b(encoded[0], encoded[4]);
486 outptr[pixel_ptr+1] = get_g(encoded[0], encoded[4], encoded[5]);
487 outptr[pixel_ptr+2] = get_r(encoded[0], encoded[5]);
488 outptr[pixel_ptr+3] = get_b(encoded[1], encoded[4]);
489 outptr[pixel_ptr+4] = get_g(encoded[1], encoded[4], encoded[5]);
490 outptr[pixel_ptr+5] = get_r(encoded[1], encoded[5]);
491 outptr[pixel_ptr-c->pic.linesize[0]] = get_b(encoded[2], encoded[4]);
492 outptr[pixel_ptr-c->pic.linesize[0]+1] = get_g(encoded[2], encoded[4], encoded[5]);
493 outptr[pixel_ptr-c->pic.linesize[0]+2] = get_r(encoded[2], encoded[5]);
494 outptr[pixel_ptr-c->pic.linesize[0]+3] = get_b(encoded[3], encoded[4]);
495 outptr[pixel_ptr-c->pic.linesize[0]+4] = get_g(encoded[3], encoded[4], encoded[5]);
496 outptr[pixel_ptr-c->pic.linesize[0]+5] = get_r(encoded[3], encoded[5]);
503 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
507 *data_size = sizeof(AVFrame);
508 *(AVFrame*)data = c->pic;
510 /* always report that the buffer was completely consumed */
519 static int decode_init(AVCodecContext *avctx)
521 LclDecContext * const c = avctx->priv_data;
522 unsigned int basesize = avctx->width * avctx->height;
523 unsigned int max_basesize = ((avctx->width + 3) & ~3) * ((avctx->height + 3) & ~3);
524 unsigned int max_decomp_size;
525 int zret; // Zlib return code
529 c->pic.data[0] = NULL;
532 // Needed if zlib unused or init aborted before inflateInit
533 memset(&(c->zstream), 0, sizeof(z_stream));
536 if (avctx->extradata_size < 8) {
537 av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
541 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
545 /* Check codec type */
546 if (((avctx->codec_id == CODEC_ID_MSZH) && (*((char *)avctx->extradata + 7) != CODEC_MSZH)) ||
547 ((avctx->codec_id == CODEC_ID_ZLIB) && (*((char *)avctx->extradata + 7) != CODEC_ZLIB))) {
548 av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
551 /* Detect image type */
552 switch (c->imgtype = *((char *)avctx->extradata + 4)) {
554 c->decomp_size = basesize * 3;
555 max_decomp_size = max_basesize * 3;
556 av_log(avctx, AV_LOG_INFO, "Image type is YUV 1:1:1.\n");
559 c->decomp_size = basesize * 2;
560 max_decomp_size = max_basesize * 2;
561 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:2.\n");
564 c->decomp_size = basesize * 3;
565 max_decomp_size = max_basesize * 3;
566 av_log(avctx, AV_LOG_INFO, "Image type is RGB 24.\n");
569 c->decomp_size = basesize / 2 * 3;
570 max_decomp_size = max_basesize / 2 * 3;
571 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:1:1.\n");
574 c->decomp_size = basesize * 2;
575 max_decomp_size = max_basesize * 2;
576 av_log(avctx, AV_LOG_INFO, "Image type is YUV 2:1:1.\n");
579 c->decomp_size = basesize / 2 * 3;
580 max_decomp_size = max_basesize / 2 * 3;
581 av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:0.\n");
584 av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
588 /* Detect compression method */
589 c->compression = *((char *)avctx->extradata + 5);
590 switch (avctx->codec_id) {
592 switch (c->compression) {
594 av_log(avctx, AV_LOG_INFO, "Compression enabled.\n");
596 case COMP_MSZH_NOCOMP:
598 av_log(avctx, AV_LOG_INFO, "No compression.\n");
601 av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
607 switch (c->compression) {
608 case COMP_ZLIB_HISPEED:
609 av_log(avctx, AV_LOG_INFO, "High speed compression.\n");
611 case COMP_ZLIB_HICOMP:
612 av_log(avctx, AV_LOG_INFO, "High compression.\n");
614 case COMP_ZLIB_NORMAL:
615 av_log(avctx, AV_LOG_INFO, "Normal compression.\n");
618 if ((c->compression < Z_NO_COMPRESSION) || (c->compression > Z_BEST_COMPRESSION)) {
619 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
622 av_log(avctx, AV_LOG_INFO, "Compression level for ZLIB: (%d).\n", c->compression);
625 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
630 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
634 /* Allocate decompression buffer */
635 if (c->decomp_size) {
636 if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
637 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
643 c->flags = *((char *)avctx->extradata + 6);
644 if (c->flags & FLAG_MULTITHREAD)
645 av_log(avctx, AV_LOG_INFO, "Multithread encoder flag set.\n");
646 if (c->flags & FLAG_NULLFRAME)
647 av_log(avctx, AV_LOG_INFO, "Nullframe insertion flag set.\n");
648 if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER))
649 av_log(avctx, AV_LOG_INFO, "PNG filter flag set.\n");
650 if (c->flags & FLAGMASK_UNUSED)
651 av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
653 /* If needed init zlib */
654 if (avctx->codec_id == CODEC_ID_ZLIB) {
656 c->zstream.zalloc = Z_NULL;
657 c->zstream.zfree = Z_NULL;
658 c->zstream.opaque = Z_NULL;
659 zret = inflateInit(&(c->zstream));
661 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
665 av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
670 avctx->pix_fmt = PIX_FMT_BGR24;
680 static int decode_end(AVCodecContext *avctx)
682 LclDecContext * const c = avctx->priv_data;
685 avctx->release_buffer(avctx, &c->pic);
687 inflateEnd(&(c->zstream));
693 #ifdef CONFIG_MSZH_DECODER
694 AVCodec mszh_decoder = {
698 sizeof(LclDecContext),
707 #ifdef CONFIG_ZLIB_DECODER
708 AVCodec zlib_decoder = {
712 sizeof(LclDecContext),