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"
49 #if CONFIG_ZLIB_DECODER
56 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);
142 c->zstream.next_in = 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);
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);
156 return c->zstream.total_out;
166 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
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;
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) {
198 case AV_CODEC_ID_MSZH:
199 switch (c->compression) {
201 if (c->flags & FLAG_MULTITHREAD) {
202 mthread_inlen = AV_RL32(encoded);
203 mthread_inlen = FFMIN(mthread_inlen, len - 8);
204 mthread_outlen = AV_RL32(encoded+4);
205 mthread_outlen = FFMIN(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 - 8 - 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: {
234 switch (c->imgtype) {
248 bppx2 = 0; // will error out below
251 if (len < ((width * height * bppx2) >> 1))
252 return AVERROR_INVALIDDATA;
256 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
260 #if CONFIG_ZLIB_DECODER
261 case AV_CODEC_ID_ZLIB:
262 /* Using the original dll with normal compression (-1) and RGB format
263 * gives a file with ZLIB fourcc, but frame is really uncompressed.
264 * To be sure that's true check also frame size */
265 if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
266 len == width * height * 3) {
267 if (c->flags & FLAG_PNGFILTER) {
268 memcpy(c->decomp_buf, encoded, len);
269 encoded = c->decomp_buf;
273 } else if (c->flags & FLAG_MULTITHREAD) {
275 mthread_inlen = AV_RL32(encoded);
276 mthread_inlen = FFMIN(mthread_inlen, len - 8);
277 mthread_outlen = AV_RL32(encoded+4);
278 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
279 ret = zlib_decomp(avctx, encoded + 8, mthread_inlen, 0, mthread_outlen);
280 if (ret < 0) return ret;
281 ret = zlib_decomp(avctx, encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
282 mthread_outlen, mthread_outlen);
283 if (ret < 0) return ret;
285 int ret = zlib_decomp(avctx, encoded, len, 0, c->decomp_size);
286 if (ret < 0) return ret;
288 encoded = c->decomp_buf;
289 len = c->decomp_size;
293 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
298 /* Apply PNG filter */
299 if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
300 switch (c->imgtype) {
303 for (row = 0; row < height; row++) {
304 pixel_ptr = row * width * 3;
305 yq = encoded[pixel_ptr++];
306 uqvq = AV_RL16(encoded+pixel_ptr);
308 for (col = 1; col < width; col++) {
309 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
310 uqvq -= AV_RL16(encoded+pixel_ptr+1);
311 AV_WL16(encoded+pixel_ptr+1, uqvq);
317 for (row = 0; row < height; row++) {
318 pixel_ptr = row * width * 2;
320 for (col = 0; col < width/4; col++) {
321 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
322 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
323 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
324 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
325 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
326 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
327 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
328 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
334 for (row = 0; row < height; row++) {
335 pixel_ptr = row * width / 2 * 3;
337 for (col = 0; col < width/4; col++) {
338 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
339 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
340 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
341 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
342 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
343 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
349 for (row = 0; row < height; row++) {
350 pixel_ptr = row * width * 2;
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] = uq -= encoded[pixel_ptr+2];
356 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
362 for (row = 0; row < height/2; row++) {
363 pixel_ptr = row * width * 3;
364 yq = y1q = uq = vq =0;
365 for (col = 0; col < width/2; col++) {
366 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
367 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
368 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
369 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
370 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
371 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
377 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
382 /* Convert colorspace */
383 y_out = c->pic.data[0] + (height - 1) * c->pic.linesize[0];
384 u_out = c->pic.data[1] + (height - 1) * c->pic.linesize[1];
385 v_out = c->pic.data[2] + (height - 1) * c->pic.linesize[2];
386 switch (c->imgtype) {
388 for (row = 0; row < height; row++) {
389 for (col = 0; col < width; col++) {
390 y_out[col] = *encoded++;
391 u_out[col] = *encoded++ + 128;
392 v_out[col] = *encoded++ + 128;
394 y_out -= c->pic.linesize[0];
395 u_out -= c->pic.linesize[1];
396 v_out -= c->pic.linesize[2];
400 for (row = 0; row < height; row++) {
401 for (col = 0; col < width - 3; col += 4) {
402 memcpy(y_out + col, encoded, 4);
404 u_out[ col >> 1 ] = *encoded++ + 128;
405 u_out[(col >> 1) + 1] = *encoded++ + 128;
406 v_out[ col >> 1 ] = *encoded++ + 128;
407 v_out[(col >> 1) + 1] = *encoded++ + 128;
409 y_out -= c->pic.linesize[0];
410 u_out -= c->pic.linesize[1];
411 v_out -= c->pic.linesize[2];
415 for (row = height - 1; row >= 0; row--) {
416 pixel_ptr = row * c->pic.linesize[0];
417 memcpy(outptr + pixel_ptr, encoded, 3 * width);
418 encoded += 3 * width;
422 for (row = 0; row < height; row++) {
423 for (col = 0; col < width - 3; col += 4) {
424 memcpy(y_out + col, encoded, 4);
426 u_out[col >> 2] = *encoded++ + 128;
427 v_out[col >> 2] = *encoded++ + 128;
429 y_out -= c->pic.linesize[0];
430 u_out -= c->pic.linesize[1];
431 v_out -= c->pic.linesize[2];
435 for (row = 0; row < height; row++) {
436 for (col = 0; col < width - 1; col += 2) {
437 memcpy(y_out + col, encoded, 2);
439 u_out[col >> 1] = *encoded++ + 128;
440 v_out[col >> 1] = *encoded++ + 128;
442 y_out -= c->pic.linesize[0];
443 u_out -= c->pic.linesize[1];
444 v_out -= c->pic.linesize[2];
448 u_out = c->pic.data[1] + ((height >> 1) - 1) * c->pic.linesize[1];
449 v_out = c->pic.data[2] + ((height >> 1) - 1) * c->pic.linesize[2];
450 for (row = 0; row < height - 1; row += 2) {
451 for (col = 0; col < width - 1; col += 2) {
452 memcpy(y_out + col, encoded, 2);
454 memcpy(y_out + col - c->pic.linesize[0], encoded, 2);
456 u_out[col >> 1] = *encoded++ + 128;
457 v_out[col >> 1] = *encoded++ + 128;
459 y_out -= c->pic.linesize[0] << 1;
460 u_out -= c->pic.linesize[1];
461 v_out -= c->pic.linesize[2];
465 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
469 *data_size = sizeof(AVFrame);
470 *(AVFrame*)data = c->pic;
472 /* always report that the buffer was completely consumed */
481 static av_cold int decode_init(AVCodecContext *avctx)
483 LclDecContext * const c = avctx->priv_data;
484 unsigned int basesize = avctx->width * avctx->height;
485 unsigned int max_basesize = FFALIGN(avctx->width, 4) *
486 FFALIGN(avctx->height, 4);
487 unsigned int max_decomp_size;
489 avcodec_get_frame_defaults(&c->pic);
490 if (avctx->extradata_size < 8) {
491 av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
492 return AVERROR_INVALIDDATA;
495 /* Check codec type */
496 if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
497 (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
498 av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
501 /* Detect image type */
502 switch (c->imgtype = avctx->extradata[4]) {
504 c->decomp_size = basesize * 3;
505 max_decomp_size = max_basesize * 3;
506 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
507 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
510 c->decomp_size = basesize * 2;
511 max_decomp_size = max_basesize * 2;
512 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
513 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
516 c->decomp_size = basesize * 3;
517 max_decomp_size = max_basesize * 3;
518 avctx->pix_fmt = AV_PIX_FMT_BGR24;
519 av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
522 c->decomp_size = basesize / 2 * 3;
523 max_decomp_size = max_basesize / 2 * 3;
524 avctx->pix_fmt = AV_PIX_FMT_YUV411P;
525 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
528 c->decomp_size = basesize * 2;
529 max_decomp_size = max_basesize * 2;
530 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
531 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
534 c->decomp_size = basesize / 2 * 3;
535 max_decomp_size = max_basesize / 2 * 3;
536 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
537 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
540 av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
541 return AVERROR_INVALIDDATA;
544 /* Detect compression method */
545 c->compression = (int8_t)avctx->extradata[5];
546 switch (avctx->codec_id) {
547 case AV_CODEC_ID_MSZH:
548 switch (c->compression) {
550 av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
552 case COMP_MSZH_NOCOMP:
554 av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
557 av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
558 return AVERROR_INVALIDDATA;
561 #if CONFIG_ZLIB_DECODER
562 case AV_CODEC_ID_ZLIB:
563 switch (c->compression) {
564 case COMP_ZLIB_HISPEED:
565 av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
567 case COMP_ZLIB_HICOMP:
568 av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
570 case COMP_ZLIB_NORMAL:
571 av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
574 if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
575 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
576 return AVERROR_INVALIDDATA;
578 av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
583 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
584 return AVERROR_INVALIDDATA;
587 /* Allocate decompression buffer */
588 if (c->decomp_size) {
589 if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
590 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
591 return AVERROR(ENOMEM);
596 c->flags = avctx->extradata[6];
597 if (c->flags & FLAG_MULTITHREAD)
598 av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
599 if (c->flags & FLAG_NULLFRAME)
600 av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
601 if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
602 av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
603 if (c->flags & FLAGMASK_UNUSED)
604 av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
606 /* If needed init zlib */
607 #if CONFIG_ZLIB_DECODER
608 if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
610 c->zstream.zalloc = Z_NULL;
611 c->zstream.zfree = Z_NULL;
612 c->zstream.opaque = Z_NULL;
613 zret = inflateInit(&c->zstream);
615 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
616 av_freep(&c->decomp_buf);
617 return AVERROR_UNKNOWN;
630 static av_cold int decode_end(AVCodecContext *avctx)
632 LclDecContext * const c = avctx->priv_data;
634 av_freep(&c->decomp_buf);
636 avctx->release_buffer(avctx, &c->pic);
637 #if CONFIG_ZLIB_DECODER
638 if (avctx->codec_id == AV_CODEC_ID_ZLIB)
639 inflateEnd(&c->zstream);
645 #if CONFIG_MSZH_DECODER
646 AVCodec ff_mszh_decoder = {
648 .type = AVMEDIA_TYPE_VIDEO,
649 .id = AV_CODEC_ID_MSZH,
650 .priv_data_size = sizeof(LclDecContext),
653 .decode = decode_frame,
654 .capabilities = CODEC_CAP_DR1,
655 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
659 #if CONFIG_ZLIB_DECODER
660 AVCodec ff_zlib_decoder = {
662 .type = AVMEDIA_TYPE_VIDEO,
663 .id = AV_CODEC_ID_ZLIB,
664 .priv_data_size = sizeof(LclDecContext),
667 .decode = decode_frame,
668 .capabilities = CODEC_CAP_DR1,
669 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),