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)
44 #include "libavutil/mem.h"
45 #include "libavutil/pixdesc.h"
47 #include "bytestream.h"
51 #if CONFIG_ZLIB_DECODER
58 typedef struct LclDecContext {
65 // Decompressed data size
66 unsigned int decomp_size;
67 // Decompression buffer
68 unsigned char* decomp_buf;
69 #if CONFIG_ZLIB_DECODER
76 * @param srcptr compressed source buffer, must be padded with at least 5 extra bytes
77 * @param destptr must be padded sufficiently for av_memcpy_backptr
79 static unsigned int mszh_decomp(const unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
81 unsigned char *destptr_bak = destptr;
82 unsigned char *destptr_end = destptr + destsize;
83 const unsigned char *srcptr_end = srcptr + srclen;
84 unsigned mask = *srcptr++;
85 unsigned maskbit = 0x80;
87 while (srcptr < srcptr_end && destptr < destptr_end) {
88 if (!(mask & maskbit)) {
89 memcpy(destptr, srcptr, 4);
93 unsigned ofs = bytestream_get_le16(&srcptr);
94 unsigned cnt = (ofs >> 11) + 1;
96 ofs = FFMIN(ofs, destptr - destptr_bak);
98 cnt = FFMIN(cnt, destptr_end - destptr);
100 av_memcpy_backptr(destptr, ofs, cnt);
102 // Not known what the correct behaviour is, but
103 // this at least avoids uninitialized data.
104 memset(destptr, 0, cnt);
112 if (destptr_end - destptr < 32 || srcptr_end - srcptr < 32) break;
113 memcpy(destptr, srcptr, 32);
122 return destptr - destptr_bak;
126 #if CONFIG_ZLIB_DECODER
128 * @brief decompress a zlib-compressed data block into decomp_buf
129 * @param src compressed input buffer
130 * @param src_len data length in input buffer
131 * @param offset offset in decomp_buf
132 * @param expected expected decompressed length
134 static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected)
136 LclDecContext *c = avctx->priv_data;
137 int zret = inflateReset(&c->zstream);
139 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
140 return AVERROR_UNKNOWN;
142 c->zstream.next_in = (uint8_t *)src;
143 c->zstream.avail_in = src_len;
144 c->zstream.next_out = c->decomp_buf + offset;
145 c->zstream.avail_out = c->decomp_size - offset;
146 zret = inflate(&c->zstream, Z_FINISH);
147 if (zret != Z_OK && zret != Z_STREAM_END) {
148 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
149 return AVERROR_UNKNOWN;
151 if (expected != (unsigned int)c->zstream.total_out) {
152 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
153 expected, c->zstream.total_out);
154 return AVERROR_UNKNOWN;
156 return c->zstream.total_out;
166 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
168 AVFrame *frame = data;
169 const uint8_t *buf = avpkt->data;
170 int buf_size = avpkt->size;
171 LclDecContext * const c = avctx->priv_data;
172 unsigned int pixel_ptr;
174 unsigned char *encoded, *outptr;
175 uint8_t *y_out, *u_out, *v_out;
176 unsigned int width = avctx->width; // Real image width
177 unsigned int height = avctx->height; // Real image height
178 unsigned int mszh_dlen;
179 unsigned char yq, y1q, uq, vq;
181 unsigned int mthread_inlen, mthread_outlen;
182 unsigned int len = buf_size;
184 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
187 outptr = frame->data[0]; // Output image pointer
189 /* Decompress frame */
190 switch (avctx->codec_id) {
191 case AV_CODEC_ID_MSZH:
192 switch (c->compression) {
194 if (c->imgtype == IMGTYPE_RGB24 && len == width * height * 3) {
196 } else if (c->flags & FLAG_MULTITHREAD) {
197 mthread_inlen = AV_RL32(buf);
199 av_log(avctx, AV_LOG_ERROR, "len %d is too small\n", len);
200 return AVERROR_INVALIDDATA;
202 mthread_inlen = FFMIN(mthread_inlen, len - 8);
203 mthread_outlen = AV_RL32(buf + 4);
204 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
205 mszh_dlen = mszh_decomp(buf + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
206 if (mthread_outlen != mszh_dlen) {
207 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
208 mthread_outlen, mszh_dlen);
209 return AVERROR_INVALIDDATA;
211 mszh_dlen = mszh_decomp(buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
212 c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
213 if (mthread_outlen != mszh_dlen) {
214 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
215 mthread_outlen, mszh_dlen);
216 return AVERROR_INVALIDDATA;
218 encoded = c->decomp_buf;
219 len = c->decomp_size;
221 mszh_dlen = mszh_decomp(buf, len, c->decomp_buf, c->decomp_size);
222 if (c->decomp_size != mszh_dlen) {
223 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
224 c->decomp_size, mszh_dlen);
225 return AVERROR_INVALIDDATA;
227 encoded = c->decomp_buf;
231 case COMP_MSZH_NOCOMP: {
233 switch (c->imgtype) {
247 bppx2 = 0; // will error out below
250 if (len < ((width * height * bppx2) >> 1))
251 return AVERROR_INVALIDDATA;
255 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
256 return AVERROR_INVALIDDATA;
259 #if CONFIG_ZLIB_DECODER
260 case AV_CODEC_ID_ZLIB:
261 /* Using the original dll with normal compression (-1) and RGB format
262 * gives a file with ZLIB fourcc, but frame is really uncompressed.
263 * To be sure that's true check also frame size */
264 if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
265 len == width * height * 3) {
266 if (c->flags & FLAG_PNGFILTER) {
267 memcpy(c->decomp_buf, buf, len);
268 encoded = c->decomp_buf;
272 } else if (c->flags & FLAG_MULTITHREAD) {
273 mthread_inlen = AV_RL32(buf);
274 mthread_inlen = FFMIN(mthread_inlen, len - 8);
275 mthread_outlen = AV_RL32(buf + 4);
276 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
277 ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen);
278 if (ret < 0) return ret;
279 ret = zlib_decomp(avctx, buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
280 mthread_outlen, mthread_outlen);
281 if (ret < 0) return ret;
283 int ret = zlib_decomp(avctx, buf, len, 0, c->decomp_size);
284 if (ret < 0) return ret;
286 encoded = c->decomp_buf;
287 len = c->decomp_size;
291 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
292 return AVERROR_INVALIDDATA;
296 /* Apply PNG filter */
297 if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
298 switch (c->imgtype) {
301 for (row = 0; row < height; row++) {
302 pixel_ptr = row * width * 3;
303 yq = encoded[pixel_ptr++];
304 uqvq = AV_RL16(encoded+pixel_ptr);
306 for (col = 1; col < width; col++) {
307 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
308 uqvq -= AV_RL16(encoded+pixel_ptr+1);
309 AV_WL16(encoded+pixel_ptr+1, uqvq);
315 for (row = 0; row < height; row++) {
316 pixel_ptr = row * width * 2;
318 for (col = 0; col < width/4; col++) {
319 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
320 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
321 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
322 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
323 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
324 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
325 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
326 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
332 for (row = 0; row < height; row++) {
333 pixel_ptr = row * width / 2 * 3;
335 for (col = 0; col < width/4; col++) {
336 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
337 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
338 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
339 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
340 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
341 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
347 for (row = 0; row < height; row++) {
348 pixel_ptr = row * width * 2;
350 for (col = 0; col < width/2; col++) {
351 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
352 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
353 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
354 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
360 for (row = 0; row < height/2; row++) {
361 pixel_ptr = row * width * 3;
362 yq = y1q = uq = vq =0;
363 for (col = 0; col < width/2; col++) {
364 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
365 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
366 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
367 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
368 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
369 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
375 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
376 return AVERROR_INVALIDDATA;
380 /* Convert colorspace */
381 y_out = frame->data[0] + (height - 1) * frame->linesize[0];
382 u_out = frame->data[1] + (height - 1) * frame->linesize[1];
383 v_out = frame->data[2] + (height - 1) * frame->linesize[2];
384 switch (c->imgtype) {
386 for (row = 0; row < height; row++) {
387 for (col = 0; col < width; col++) {
388 y_out[col] = *encoded++;
389 u_out[col] = *encoded++ + 128;
390 v_out[col] = *encoded++ + 128;
392 y_out -= frame->linesize[0];
393 u_out -= frame->linesize[1];
394 v_out -= frame->linesize[2];
398 for (row = 0; row < height; row++) {
399 for (col = 0; col < width - 3; col += 4) {
400 memcpy(y_out + col, encoded, 4);
402 u_out[ col >> 1 ] = *encoded++ + 128;
403 u_out[(col >> 1) + 1] = *encoded++ + 128;
404 v_out[ col >> 1 ] = *encoded++ + 128;
405 v_out[(col >> 1) + 1] = *encoded++ + 128;
407 y_out -= frame->linesize[0];
408 u_out -= frame->linesize[1];
409 v_out -= frame->linesize[2];
413 for (row = height - 1; row >= 0; row--) {
414 pixel_ptr = row * frame->linesize[0];
415 memcpy(outptr + pixel_ptr, encoded, 3 * width);
416 encoded += 3 * width;
420 for (row = 0; row < height; row++) {
421 for (col = 0; col < width - 3; col += 4) {
422 memcpy(y_out + col, encoded, 4);
424 u_out[col >> 2] = *encoded++ + 128;
425 v_out[col >> 2] = *encoded++ + 128;
427 y_out -= frame->linesize[0];
428 u_out -= frame->linesize[1];
429 v_out -= frame->linesize[2];
433 for (row = 0; row < height; row++) {
434 for (col = 0; col < width - 1; col += 2) {
435 memcpy(y_out + col, encoded, 2);
437 u_out[col >> 1] = *encoded++ + 128;
438 v_out[col >> 1] = *encoded++ + 128;
440 y_out -= frame->linesize[0];
441 u_out -= frame->linesize[1];
442 v_out -= frame->linesize[2];
446 u_out = frame->data[1] + ((height >> 1) - 1) * frame->linesize[1];
447 v_out = frame->data[2] + ((height >> 1) - 1) * frame->linesize[2];
448 for (row = 0; row < height - 1; row += 2) {
449 for (col = 0; col < width - 1; col += 2) {
450 memcpy(y_out + col, encoded, 2);
452 memcpy(y_out + col - frame->linesize[0], encoded, 2);
454 u_out[col >> 1] = *encoded++ + 128;
455 v_out[col >> 1] = *encoded++ + 128;
457 y_out -= frame->linesize[0] << 1;
458 u_out -= frame->linesize[1];
459 v_out -= frame->linesize[2];
463 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
464 return AVERROR_INVALIDDATA;
469 /* always report that the buffer was completely consumed */
478 static av_cold int decode_init(AVCodecContext *avctx)
480 LclDecContext * const c = avctx->priv_data;
481 unsigned int basesize = avctx->width * avctx->height;
482 unsigned int max_basesize = FFALIGN(avctx->width, 4) *
483 FFALIGN(avctx->height, 4);
484 unsigned int max_decomp_size;
485 int subsample_h, subsample_v;
487 if (avctx->extradata_size < 8) {
488 av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
489 return AVERROR_INVALIDDATA;
492 /* Check codec type */
493 if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
494 (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
495 av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
498 /* Detect image type */
499 switch (c->imgtype = avctx->extradata[4]) {
501 c->decomp_size = basesize * 3;
502 max_decomp_size = max_basesize * 3;
503 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
504 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
507 c->decomp_size = basesize * 2;
508 max_decomp_size = max_basesize * 2;
509 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
510 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
511 if (avctx->width % 4) {
512 avpriv_request_sample(avctx, "Unsupported dimensions\n");
513 return AVERROR_INVALIDDATA;
517 c->decomp_size = basesize * 3;
518 max_decomp_size = max_basesize * 3;
519 avctx->pix_fmt = AV_PIX_FMT_BGR24;
520 av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
523 c->decomp_size = basesize / 2 * 3;
524 max_decomp_size = max_basesize / 2 * 3;
525 avctx->pix_fmt = AV_PIX_FMT_YUV411P;
526 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
529 c->decomp_size = basesize * 2;
530 max_decomp_size = max_basesize * 2;
531 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
532 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
535 c->decomp_size = basesize / 2 * 3;
536 max_decomp_size = max_basesize / 2 * 3;
537 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
538 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
541 av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
542 return AVERROR_INVALIDDATA;
545 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &subsample_h, &subsample_v);
546 if (avctx->width % (1<<subsample_h) || avctx->height % (1<<subsample_v)) {
547 avpriv_request_sample(avctx, "Unsupported dimensions\n");
548 return AVERROR_INVALIDDATA;
551 /* Detect compression method */
552 c->compression = (int8_t)avctx->extradata[5];
553 switch (avctx->codec_id) {
554 case AV_CODEC_ID_MSZH:
555 switch (c->compression) {
557 av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
559 case COMP_MSZH_NOCOMP:
561 av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
564 av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
565 return AVERROR_INVALIDDATA;
568 #if CONFIG_ZLIB_DECODER
569 case AV_CODEC_ID_ZLIB:
570 switch (c->compression) {
571 case COMP_ZLIB_HISPEED:
572 av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
574 case COMP_ZLIB_HICOMP:
575 av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
577 case COMP_ZLIB_NORMAL:
578 av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
581 if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
582 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
583 return AVERROR_INVALIDDATA;
585 av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
590 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
591 return AVERROR_INVALIDDATA;
594 /* Allocate decompression buffer */
595 if (c->decomp_size) {
596 if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
597 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
598 return AVERROR(ENOMEM);
603 c->flags = avctx->extradata[6];
604 if (c->flags & FLAG_MULTITHREAD)
605 av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
606 if (c->flags & FLAG_NULLFRAME)
607 av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
608 if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
609 av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
610 if (c->flags & FLAGMASK_UNUSED)
611 av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
613 /* If needed init zlib */
614 #if CONFIG_ZLIB_DECODER
615 if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
617 c->zstream.zalloc = Z_NULL;
618 c->zstream.zfree = Z_NULL;
619 c->zstream.opaque = Z_NULL;
620 zret = inflateInit(&c->zstream);
622 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
623 av_freep(&c->decomp_buf);
624 return AVERROR_UNKNOWN;
637 static av_cold int decode_end(AVCodecContext *avctx)
639 LclDecContext * const c = avctx->priv_data;
641 av_freep(&c->decomp_buf);
642 #if CONFIG_ZLIB_DECODER
643 if (avctx->codec_id == AV_CODEC_ID_ZLIB)
644 inflateEnd(&c->zstream);
650 #if CONFIG_MSZH_DECODER
651 AVCodec ff_mszh_decoder = {
653 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
654 .type = AVMEDIA_TYPE_VIDEO,
655 .id = AV_CODEC_ID_MSZH,
656 .priv_data_size = sizeof(LclDecContext),
659 .decode = decode_frame,
660 .capabilities = CODEC_CAP_DR1,
664 #if CONFIG_ZLIB_DECODER
665 AVCodec ff_zlib_decoder = {
667 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
668 .type = AVMEDIA_TYPE_VIDEO,
669 .id = AV_CODEC_ID_ZLIB,
670 .priv_data_size = sizeof(LclDecContext),
673 .decode = decode_frame,
674 .capabilities = CODEC_CAP_DR1,