2 * LCL (LossLess Codec Library) Codec
3 * Copyright (c) 2002-2004 Roberto Togni
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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);
98 av_memcpy_backptr(destptr, ofs, cnt);
105 if (destptr_end - destptr < 32 || srcptr_end - srcptr < 32) break;
106 memcpy(destptr, srcptr, 32);
115 return destptr - destptr_bak;
119 #if CONFIG_ZLIB_DECODER
121 * @brief decompress a zlib-compressed data block into decomp_buf
122 * @param src compressed input buffer
123 * @param src_len data length in input buffer
124 * @param offset offset in decomp_buf
125 * @param expected expected decompressed length
127 static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected)
129 LclDecContext *c = avctx->priv_data;
130 int zret = inflateReset(&c->zstream);
132 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
133 return AVERROR_UNKNOWN;
135 c->zstream.next_in = src;
136 c->zstream.avail_in = src_len;
137 c->zstream.next_out = c->decomp_buf + offset;
138 c->zstream.avail_out = c->decomp_size - offset;
139 zret = inflate(&c->zstream, Z_FINISH);
140 if (zret != Z_OK && zret != Z_STREAM_END) {
141 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
142 return AVERROR_UNKNOWN;
144 if (expected != (unsigned int)c->zstream.total_out) {
145 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
146 expected, c->zstream.total_out);
147 return AVERROR_UNKNOWN;
149 return c->zstream.total_out;
159 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
161 AVFrame *frame = data;
162 const uint8_t *buf = avpkt->data;
163 int buf_size = avpkt->size;
164 LclDecContext * const c = avctx->priv_data;
165 unsigned int pixel_ptr;
167 unsigned char *encoded, *outptr;
168 uint8_t *y_out, *u_out, *v_out;
169 unsigned int width = avctx->width; // Real image width
170 unsigned int height = avctx->height; // Real image height
171 unsigned int mszh_dlen;
172 unsigned char yq, y1q, uq, vq;
174 unsigned int mthread_inlen, mthread_outlen;
175 unsigned int len = buf_size;
177 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
178 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
182 outptr = frame->data[0]; // Output image pointer
184 /* Decompress frame */
185 switch (avctx->codec_id) {
186 case AV_CODEC_ID_MSZH:
187 switch (c->compression) {
189 if (c->flags & FLAG_MULTITHREAD) {
190 mthread_inlen = AV_RL32(buf);
191 mthread_inlen = FFMIN(mthread_inlen, len - 8);
192 mthread_outlen = AV_RL32(buf + 4);
193 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
194 mszh_dlen = mszh_decomp(buf + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
195 if (mthread_outlen != mszh_dlen) {
196 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
197 mthread_outlen, mszh_dlen);
198 return AVERROR_INVALIDDATA;
200 mszh_dlen = mszh_decomp(buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
201 c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
202 if (mthread_outlen != mszh_dlen) {
203 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
204 mthread_outlen, mszh_dlen);
205 return AVERROR_INVALIDDATA;
207 encoded = c->decomp_buf;
208 len = c->decomp_size;
210 mszh_dlen = mszh_decomp(buf, len, c->decomp_buf, c->decomp_size);
211 if (c->decomp_size != mszh_dlen) {
212 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
213 c->decomp_size, mszh_dlen);
214 return AVERROR_INVALIDDATA;
216 encoded = c->decomp_buf;
220 case COMP_MSZH_NOCOMP: {
222 switch (c->imgtype) {
236 bppx2 = 0; // will error out below
239 if (len < ((width * height * bppx2) >> 1))
240 return AVERROR_INVALIDDATA;
244 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
245 return AVERROR_INVALIDDATA;
248 #if CONFIG_ZLIB_DECODER
249 case AV_CODEC_ID_ZLIB:
250 /* Using the original dll with normal compression (-1) and RGB format
251 * gives a file with ZLIB fourcc, but frame is really uncompressed.
252 * To be sure that's true check also frame size */
253 if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
254 len == width * height * 3) {
255 if (c->flags & FLAG_PNGFILTER) {
256 memcpy(c->decomp_buf, buf, len);
257 encoded = c->decomp_buf;
261 } else if (c->flags & FLAG_MULTITHREAD) {
262 mthread_inlen = AV_RL32(buf);
263 mthread_inlen = FFMIN(mthread_inlen, len - 8);
264 mthread_outlen = AV_RL32(buf + 4);
265 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
266 ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen);
267 if (ret < 0) return ret;
268 ret = zlib_decomp(avctx, buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
269 mthread_outlen, mthread_outlen);
270 if (ret < 0) return ret;
272 int ret = zlib_decomp(avctx, buf, len, 0, c->decomp_size);
273 if (ret < 0) return ret;
275 encoded = c->decomp_buf;
276 len = c->decomp_size;
280 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
281 return AVERROR_INVALIDDATA;
285 /* Apply PNG filter */
286 if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
287 switch (c->imgtype) {
290 for (row = 0; row < height; row++) {
291 pixel_ptr = row * width * 3;
292 yq = encoded[pixel_ptr++];
293 uqvq = AV_RL16(encoded+pixel_ptr);
295 for (col = 1; col < width; col++) {
296 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
297 uqvq -= AV_RL16(encoded+pixel_ptr+1);
298 AV_WL16(encoded+pixel_ptr+1, uqvq);
304 for (row = 0; row < height; row++) {
305 pixel_ptr = row * width * 2;
307 for (col = 0; col < width/4; col++) {
308 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
309 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
310 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
311 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
312 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
313 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
314 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
315 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
321 for (row = 0; row < height; row++) {
322 pixel_ptr = row * width / 2 * 3;
324 for (col = 0; col < width/4; col++) {
325 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
326 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
327 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
328 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
329 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
330 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
336 for (row = 0; row < height; row++) {
337 pixel_ptr = row * width * 2;
339 for (col = 0; col < width/2; col++) {
340 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
341 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
342 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
343 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
349 for (row = 0; row < height/2; row++) {
350 pixel_ptr = row * width * 3;
351 yq = y1q = uq = vq =0;
352 for (col = 0; col < width/2; col++) {
353 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
354 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
355 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
356 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
357 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
358 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
364 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
365 return AVERROR_INVALIDDATA;
369 /* Convert colorspace */
370 y_out = frame->data[0] + (height - 1) * frame->linesize[0];
371 u_out = frame->data[1] + (height - 1) * frame->linesize[1];
372 v_out = frame->data[2] + (height - 1) * frame->linesize[2];
373 switch (c->imgtype) {
375 for (row = 0; row < height; row++) {
376 for (col = 0; col < width; col++) {
377 y_out[col] = *encoded++;
378 u_out[col] = *encoded++ + 128;
379 v_out[col] = *encoded++ + 128;
381 y_out -= frame->linesize[0];
382 u_out -= frame->linesize[1];
383 v_out -= frame->linesize[2];
387 for (row = 0; row < height; row++) {
388 for (col = 0; col < width - 3; col += 4) {
389 memcpy(y_out + col, encoded, 4);
391 u_out[ col >> 1 ] = *encoded++ + 128;
392 u_out[(col >> 1) + 1] = *encoded++ + 128;
393 v_out[ col >> 1 ] = *encoded++ + 128;
394 v_out[(col >> 1) + 1] = *encoded++ + 128;
396 y_out -= frame->linesize[0];
397 u_out -= frame->linesize[1];
398 v_out -= frame->linesize[2];
402 for (row = height - 1; row >= 0; row--) {
403 pixel_ptr = row * frame->linesize[0];
404 memcpy(outptr + pixel_ptr, encoded, 3 * width);
405 encoded += 3 * width;
409 for (row = 0; row < height; row++) {
410 for (col = 0; col < width - 3; col += 4) {
411 memcpy(y_out + col, encoded, 4);
413 u_out[col >> 2] = *encoded++ + 128;
414 v_out[col >> 2] = *encoded++ + 128;
416 y_out -= frame->linesize[0];
417 u_out -= frame->linesize[1];
418 v_out -= frame->linesize[2];
422 for (row = 0; row < height; row++) {
423 for (col = 0; col < width - 1; col += 2) {
424 memcpy(y_out + col, encoded, 2);
426 u_out[col >> 1] = *encoded++ + 128;
427 v_out[col >> 1] = *encoded++ + 128;
429 y_out -= frame->linesize[0];
430 u_out -= frame->linesize[1];
431 v_out -= frame->linesize[2];
435 u_out = frame->data[1] + ((height >> 1) - 1) * frame->linesize[1];
436 v_out = frame->data[2] + ((height >> 1) - 1) * frame->linesize[2];
437 for (row = 0; row < height - 1; row += 2) {
438 for (col = 0; col < width - 1; col += 2) {
439 memcpy(y_out + col, encoded, 2);
441 memcpy(y_out + col - frame->linesize[0], encoded, 2);
443 u_out[col >> 1] = *encoded++ + 128;
444 v_out[col >> 1] = *encoded++ + 128;
446 y_out -= frame->linesize[0] << 1;
447 u_out -= frame->linesize[1];
448 v_out -= frame->linesize[2];
452 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
453 return AVERROR_INVALIDDATA;
458 /* always report that the buffer was completely consumed */
467 static av_cold int decode_init(AVCodecContext *avctx)
469 LclDecContext * const c = avctx->priv_data;
470 unsigned int basesize = avctx->width * avctx->height;
471 unsigned int max_basesize = FFALIGN(avctx->width, 4) *
472 FFALIGN(avctx->height, 4);
473 unsigned int max_decomp_size;
475 if (avctx->extradata_size < 8) {
476 av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
477 return AVERROR_INVALIDDATA;
480 /* Check codec type */
481 if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
482 (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
483 av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
486 /* Detect image type */
487 switch (c->imgtype = avctx->extradata[4]) {
489 c->decomp_size = basesize * 3;
490 max_decomp_size = max_basesize * 3;
491 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
492 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
495 c->decomp_size = basesize * 2;
496 max_decomp_size = max_basesize * 2;
497 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
498 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
501 c->decomp_size = basesize * 3;
502 max_decomp_size = max_basesize * 3;
503 avctx->pix_fmt = AV_PIX_FMT_BGR24;
504 av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
507 c->decomp_size = basesize / 2 * 3;
508 max_decomp_size = max_basesize / 2 * 3;
509 avctx->pix_fmt = AV_PIX_FMT_YUV411P;
510 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
513 c->decomp_size = basesize * 2;
514 max_decomp_size = max_basesize * 2;
515 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
516 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
519 c->decomp_size = basesize / 2 * 3;
520 max_decomp_size = max_basesize / 2 * 3;
521 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
522 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
525 av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
526 return AVERROR_INVALIDDATA;
529 /* Detect compression method */
530 c->compression = (int8_t)avctx->extradata[5];
531 switch (avctx->codec_id) {
532 case AV_CODEC_ID_MSZH:
533 switch (c->compression) {
535 av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
537 case COMP_MSZH_NOCOMP:
539 av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
542 av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
543 return AVERROR_INVALIDDATA;
546 #if CONFIG_ZLIB_DECODER
547 case AV_CODEC_ID_ZLIB:
548 switch (c->compression) {
549 case COMP_ZLIB_HISPEED:
550 av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
552 case COMP_ZLIB_HICOMP:
553 av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
555 case COMP_ZLIB_NORMAL:
556 av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
559 if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
560 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
561 return AVERROR_INVALIDDATA;
563 av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
568 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
569 return AVERROR_INVALIDDATA;
572 /* Allocate decompression buffer */
573 if (c->decomp_size) {
574 if (!(c->decomp_buf = av_malloc(max_decomp_size))) {
575 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
576 return AVERROR(ENOMEM);
581 c->flags = avctx->extradata[6];
582 if (c->flags & FLAG_MULTITHREAD)
583 av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
584 if (c->flags & FLAG_NULLFRAME)
585 av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
586 if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
587 av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
588 if (c->flags & FLAGMASK_UNUSED)
589 av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
591 /* If needed init zlib */
592 #if CONFIG_ZLIB_DECODER
593 if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
595 c->zstream.zalloc = Z_NULL;
596 c->zstream.zfree = Z_NULL;
597 c->zstream.opaque = Z_NULL;
598 zret = inflateInit(&c->zstream);
600 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
601 av_freep(&c->decomp_buf);
602 return AVERROR_UNKNOWN;
615 static av_cold int decode_end(AVCodecContext *avctx)
617 LclDecContext * const c = avctx->priv_data;
619 av_freep(&c->decomp_buf);
620 #if CONFIG_ZLIB_DECODER
621 if (avctx->codec_id == AV_CODEC_ID_ZLIB)
622 inflateEnd(&c->zstream);
628 #if CONFIG_MSZH_DECODER
629 AVCodec ff_mszh_decoder = {
631 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
632 .type = AVMEDIA_TYPE_VIDEO,
633 .id = AV_CODEC_ID_MSZH,
634 .priv_data_size = sizeof(LclDecContext),
637 .decode = decode_frame,
638 .capabilities = CODEC_CAP_DR1,
642 #if CONFIG_ZLIB_DECODER
643 AVCodec ff_zlib_decoder = {
645 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
646 .type = AVMEDIA_TYPE_VIDEO,
647 .id = AV_CODEC_ID_ZLIB,
648 .priv_data_size = sizeof(LclDecContext),
651 .decode = decode_frame,
652 .capabilities = CODEC_CAP_DR1,