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"
46 #include "bytestream.h"
50 #if CONFIG_ZLIB_DECODER
57 typedef struct LclDecContext {
64 // Decompressed data size
65 unsigned int decomp_size;
66 // Decompression buffer
67 unsigned char* decomp_buf;
68 #if CONFIG_ZLIB_DECODER
75 * @param srcptr compressed source buffer, must be padded with at least 5 extra bytes
76 * @param destptr must be padded sufficiently for av_memcpy_backptr
78 static unsigned int mszh_decomp(const unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
80 unsigned char *destptr_bak = destptr;
81 unsigned char *destptr_end = destptr + destsize;
82 const unsigned char *srcptr_end = srcptr + srclen;
83 unsigned mask = *srcptr++;
84 unsigned maskbit = 0x80;
86 while (srcptr < srcptr_end && destptr < destptr_end) {
87 if (!(mask & maskbit)) {
88 memcpy(destptr, srcptr, 4);
92 unsigned ofs = bytestream_get_le16(&srcptr);
93 unsigned cnt = (ofs >> 11) + 1;
95 ofs = FFMIN(ofs, destptr - destptr_bak);
97 cnt = FFMIN(cnt, destptr_end - destptr);
99 av_memcpy_backptr(destptr, ofs, cnt);
101 // Not known what the correct behaviour is, but
102 // this at least avoids uninitialized data.
103 memset(destptr, 0, cnt);
111 if (destptr_end - destptr < 32 || srcptr_end - srcptr < 32) break;
112 memcpy(destptr, srcptr, 32);
121 return destptr - destptr_bak;
125 #if CONFIG_ZLIB_DECODER
127 * @brief decompress a zlib-compressed data block into decomp_buf
128 * @param src compressed input buffer
129 * @param src_len data length in input buffer
130 * @param offset offset in decomp_buf
131 * @param expected expected decompressed length
133 static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected)
135 LclDecContext *c = avctx->priv_data;
136 int zret = inflateReset(&c->zstream);
138 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
139 return AVERROR_UNKNOWN;
141 c->zstream.next_in = (uint8_t *)src;
142 c->zstream.avail_in = src_len;
143 c->zstream.next_out = c->decomp_buf + offset;
144 c->zstream.avail_out = c->decomp_size - offset;
145 zret = inflate(&c->zstream, Z_FINISH);
146 if (zret != Z_OK && zret != Z_STREAM_END) {
147 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
148 return AVERROR_UNKNOWN;
150 if (expected != (unsigned int)c->zstream.total_out) {
151 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
152 expected, c->zstream.total_out);
153 return AVERROR_UNKNOWN;
155 return c->zstream.total_out;
165 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
167 AVFrame *frame = data;
168 const uint8_t *buf = avpkt->data;
169 int buf_size = avpkt->size;
170 LclDecContext * const c = avctx->priv_data;
171 unsigned char *encoded = (unsigned char *)buf;
172 unsigned int pixel_ptr;
174 unsigned char *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(encoded);
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(encoded+4);
204 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
205 mszh_dlen = mszh_decomp(encoded + 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(encoded + 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(encoded, 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, encoded, len);
268 encoded = c->decomp_buf;
272 } else if (c->flags & FLAG_MULTITHREAD) {
273 mthread_inlen = AV_RL32(encoded);
274 mthread_inlen = FFMIN(mthread_inlen, len - 8);
275 mthread_outlen = AV_RL32(encoded+4);
276 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
277 ret = zlib_decomp(avctx, encoded + 8, mthread_inlen, 0, mthread_outlen);
278 if (ret < 0) return ret;
279 ret = zlib_decomp(avctx, encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
280 mthread_outlen, mthread_outlen);
281 if (ret < 0) return ret;
283 int ret = zlib_decomp(avctx, encoded, 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;
486 if (avctx->extradata_size < 8) {
487 av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
488 return AVERROR_INVALIDDATA;
491 /* Check codec type */
492 if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
493 (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
494 av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
497 /* Detect image type */
498 switch (c->imgtype = avctx->extradata[4]) {
500 c->decomp_size = basesize * 3;
501 max_decomp_size = max_basesize * 3;
502 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
503 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
506 c->decomp_size = basesize * 2;
507 max_decomp_size = max_basesize * 2;
508 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
509 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
512 c->decomp_size = basesize * 3;
513 max_decomp_size = max_basesize * 3;
514 avctx->pix_fmt = AV_PIX_FMT_BGR24;
515 av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
518 c->decomp_size = basesize / 2 * 3;
519 max_decomp_size = max_basesize / 2 * 3;
520 avctx->pix_fmt = AV_PIX_FMT_YUV411P;
521 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
524 c->decomp_size = basesize * 2;
525 max_decomp_size = max_basesize * 2;
526 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
527 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
530 c->decomp_size = basesize / 2 * 3;
531 max_decomp_size = max_basesize / 2 * 3;
532 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
533 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
536 av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
537 return AVERROR_INVALIDDATA;
540 /* Detect compression method */
541 c->compression = (int8_t)avctx->extradata[5];
542 switch (avctx->codec_id) {
543 case AV_CODEC_ID_MSZH:
544 switch (c->compression) {
546 av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
548 case COMP_MSZH_NOCOMP:
550 av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
553 av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
554 return AVERROR_INVALIDDATA;
557 #if CONFIG_ZLIB_DECODER
558 case AV_CODEC_ID_ZLIB:
559 switch (c->compression) {
560 case COMP_ZLIB_HISPEED:
561 av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
563 case COMP_ZLIB_HICOMP:
564 av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
566 case COMP_ZLIB_NORMAL:
567 av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
570 if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
571 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
572 return AVERROR_INVALIDDATA;
574 av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
579 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
580 return AVERROR_INVALIDDATA;
583 /* Allocate decompression buffer */
584 if (c->decomp_size) {
585 if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
586 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
587 return AVERROR(ENOMEM);
592 c->flags = avctx->extradata[6];
593 if (c->flags & FLAG_MULTITHREAD)
594 av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
595 if (c->flags & FLAG_NULLFRAME)
596 av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
597 if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
598 av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
599 if (c->flags & FLAGMASK_UNUSED)
600 av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
602 /* If needed init zlib */
603 #if CONFIG_ZLIB_DECODER
604 if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
606 c->zstream.zalloc = Z_NULL;
607 c->zstream.zfree = Z_NULL;
608 c->zstream.opaque = Z_NULL;
609 zret = inflateInit(&c->zstream);
611 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
612 av_freep(&c->decomp_buf);
613 return AVERROR_UNKNOWN;
626 static av_cold int decode_end(AVCodecContext *avctx)
628 LclDecContext * const c = avctx->priv_data;
630 av_freep(&c->decomp_buf);
631 #if CONFIG_ZLIB_DECODER
632 if (avctx->codec_id == AV_CODEC_ID_ZLIB)
633 inflateEnd(&c->zstream);
639 #if CONFIG_MSZH_DECODER
640 AVCodec ff_mszh_decoder = {
642 .type = AVMEDIA_TYPE_VIDEO,
643 .id = AV_CODEC_ID_MSZH,
644 .priv_data_size = sizeof(LclDecContext),
647 .decode = decode_frame,
648 .capabilities = CODEC_CAP_DR1,
649 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
653 #if CONFIG_ZLIB_DECODER
654 AVCodec ff_zlib_decoder = {
656 .type = AVMEDIA_TYPE_VIDEO,
657 .id = AV_CODEC_ID_ZLIB,
658 .priv_data_size = sizeof(LclDecContext),
661 .decode = decode_frame,
662 .capabilities = CODEC_CAP_DR1,
663 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),