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 char *encoded = (unsigned char *)buf;
173 unsigned int pixel_ptr;
175 unsigned char *outptr;
176 uint8_t *y_out, *u_out, *v_out;
177 unsigned int width = avctx->width; // Real image width
178 unsigned int height = avctx->height; // Real image height
179 unsigned int mszh_dlen;
180 unsigned char yq, y1q, uq, vq;
182 unsigned int mthread_inlen, mthread_outlen;
183 unsigned int len = buf_size;
185 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
188 outptr = frame->data[0]; // Output image pointer
190 /* Decompress frame */
191 switch (avctx->codec_id) {
192 case AV_CODEC_ID_MSZH:
193 switch (c->compression) {
195 if (c->imgtype == IMGTYPE_RGB24 && len == width * height * 3) {
197 } else if (c->flags & FLAG_MULTITHREAD) {
198 mthread_inlen = AV_RL32(encoded);
200 av_log(avctx, AV_LOG_ERROR, "len %d is too small\n", len);
201 return AVERROR_INVALIDDATA;
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);
210 return AVERROR_INVALIDDATA;
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);
217 return AVERROR_INVALIDDATA;
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);
226 return AVERROR_INVALIDDATA;
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");
257 return AVERROR_INVALIDDATA;
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) {
274 mthread_inlen = AV_RL32(encoded);
275 mthread_inlen = FFMIN(mthread_inlen, len - 8);
276 mthread_outlen = AV_RL32(encoded+4);
277 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
278 ret = zlib_decomp(avctx, encoded + 8, mthread_inlen, 0, mthread_outlen);
279 if (ret < 0) return ret;
280 ret = zlib_decomp(avctx, encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
281 mthread_outlen, mthread_outlen);
282 if (ret < 0) return ret;
284 int ret = zlib_decomp(avctx, encoded, len, 0, c->decomp_size);
285 if (ret < 0) return ret;
287 encoded = c->decomp_buf;
288 len = c->decomp_size;
292 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
293 return AVERROR_INVALIDDATA;
297 /* Apply PNG filter */
298 if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
299 switch (c->imgtype) {
302 for (row = 0; row < height; row++) {
303 pixel_ptr = row * width * 3;
304 yq = encoded[pixel_ptr++];
305 uqvq = AV_RL16(encoded+pixel_ptr);
307 for (col = 1; col < width; col++) {
308 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
309 uqvq -= AV_RL16(encoded+pixel_ptr+1);
310 AV_WL16(encoded+pixel_ptr+1, uqvq);
316 for (row = 0; row < height; row++) {
317 pixel_ptr = row * width * 2;
319 for (col = 0; col < width/4; col++) {
320 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
321 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
322 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
323 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
324 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
325 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
326 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
327 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
333 for (row = 0; row < height; row++) {
334 pixel_ptr = row * width / 2 * 3;
336 for (col = 0; col < width/4; col++) {
337 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
338 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
339 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
340 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
341 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
342 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
348 for (row = 0; row < height; row++) {
349 pixel_ptr = row * width * 2;
351 for (col = 0; col < width/2; col++) {
352 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
353 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
354 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
355 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
361 for (row = 0; row < height/2; row++) {
362 pixel_ptr = row * width * 3;
363 yq = y1q = uq = vq =0;
364 for (col = 0; col < width/2; col++) {
365 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
366 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
367 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
368 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
369 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
370 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
376 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
377 return AVERROR_INVALIDDATA;
381 /* Convert colorspace */
382 y_out = frame->data[0] + (height - 1) * frame->linesize[0];
383 u_out = frame->data[1] + (height - 1) * frame->linesize[1];
384 v_out = frame->data[2] + (height - 1) * frame->linesize[2];
385 switch (c->imgtype) {
387 for (row = 0; row < height; row++) {
388 for (col = 0; col < width; col++) {
389 y_out[col] = *encoded++;
390 u_out[col] = *encoded++ + 128;
391 v_out[col] = *encoded++ + 128;
393 y_out -= frame->linesize[0];
394 u_out -= frame->linesize[1];
395 v_out -= frame->linesize[2];
399 for (row = 0; row < height; row++) {
400 for (col = 0; col < width - 3; col += 4) {
401 memcpy(y_out + col, encoded, 4);
403 u_out[ col >> 1 ] = *encoded++ + 128;
404 u_out[(col >> 1) + 1] = *encoded++ + 128;
405 v_out[ col >> 1 ] = *encoded++ + 128;
406 v_out[(col >> 1) + 1] = *encoded++ + 128;
408 y_out -= frame->linesize[0];
409 u_out -= frame->linesize[1];
410 v_out -= frame->linesize[2];
414 for (row = height - 1; row >= 0; row--) {
415 pixel_ptr = row * frame->linesize[0];
416 memcpy(outptr + pixel_ptr, encoded, 3 * width);
417 encoded += 3 * width;
421 for (row = 0; row < height; row++) {
422 for (col = 0; col < width - 3; col += 4) {
423 memcpy(y_out + col, encoded, 4);
425 u_out[col >> 2] = *encoded++ + 128;
426 v_out[col >> 2] = *encoded++ + 128;
428 y_out -= frame->linesize[0];
429 u_out -= frame->linesize[1];
430 v_out -= frame->linesize[2];
434 for (row = 0; row < height; row++) {
435 for (col = 0; col < width - 1; col += 2) {
436 memcpy(y_out + col, encoded, 2);
438 u_out[col >> 1] = *encoded++ + 128;
439 v_out[col >> 1] = *encoded++ + 128;
441 y_out -= frame->linesize[0];
442 u_out -= frame->linesize[1];
443 v_out -= frame->linesize[2];
447 u_out = frame->data[1] + ((height >> 1) - 1) * frame->linesize[1];
448 v_out = frame->data[2] + ((height >> 1) - 1) * frame->linesize[2];
449 for (row = 0; row < height - 1; row += 2) {
450 for (col = 0; col < width - 1; col += 2) {
451 memcpy(y_out + col, encoded, 2);
453 memcpy(y_out + col - frame->linesize[0], encoded, 2);
455 u_out[col >> 1] = *encoded++ + 128;
456 v_out[col >> 1] = *encoded++ + 128;
458 y_out -= frame->linesize[0] << 1;
459 u_out -= frame->linesize[1];
460 v_out -= frame->linesize[2];
464 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
465 return AVERROR_INVALIDDATA;
470 /* always report that the buffer was completely consumed */
479 static av_cold int decode_init(AVCodecContext *avctx)
481 LclDecContext * const c = avctx->priv_data;
482 unsigned int basesize = avctx->width * avctx->height;
483 unsigned int max_basesize = FFALIGN(avctx->width, 4) *
484 FFALIGN(avctx->height, 4);
485 unsigned int max_decomp_size;
486 int subsample_h, subsample_v;
488 if (avctx->extradata_size < 8) {
489 av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
490 return AVERROR_INVALIDDATA;
493 /* Check codec type */
494 if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
495 (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
496 av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
499 /* Detect image type */
500 switch (c->imgtype = avctx->extradata[4]) {
502 c->decomp_size = basesize * 3;
503 max_decomp_size = max_basesize * 3;
504 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
505 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
508 c->decomp_size = basesize * 2;
509 max_decomp_size = max_basesize * 2;
510 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
511 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
512 if (avctx->width % 4) {
513 avpriv_request_sample(avctx, "Unsupported dimensions\n");
514 return AVERROR_INVALIDDATA;
518 c->decomp_size = basesize * 3;
519 max_decomp_size = max_basesize * 3;
520 avctx->pix_fmt = AV_PIX_FMT_BGR24;
521 av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
524 c->decomp_size = basesize / 2 * 3;
525 max_decomp_size = max_basesize / 2 * 3;
526 avctx->pix_fmt = AV_PIX_FMT_YUV411P;
527 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
530 c->decomp_size = basesize * 2;
531 max_decomp_size = max_basesize * 2;
532 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
533 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
536 c->decomp_size = basesize / 2 * 3;
537 max_decomp_size = max_basesize / 2 * 3;
538 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
539 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
542 av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
543 return AVERROR_INVALIDDATA;
546 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &subsample_h, &subsample_v);
547 if (avctx->width % (1<<subsample_h) || avctx->height % (1<<subsample_v)) {
548 avpriv_request_sample(avctx, "Unsupported dimensions\n");
549 return AVERROR_INVALIDDATA;
552 /* Detect compression method */
553 c->compression = (int8_t)avctx->extradata[5];
554 switch (avctx->codec_id) {
555 case AV_CODEC_ID_MSZH:
556 switch (c->compression) {
558 av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
560 case COMP_MSZH_NOCOMP:
562 av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
565 av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
566 return AVERROR_INVALIDDATA;
569 #if CONFIG_ZLIB_DECODER
570 case AV_CODEC_ID_ZLIB:
571 switch (c->compression) {
572 case COMP_ZLIB_HISPEED:
573 av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
575 case COMP_ZLIB_HICOMP:
576 av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
578 case COMP_ZLIB_NORMAL:
579 av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
582 if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
583 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
584 return AVERROR_INVALIDDATA;
586 av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
591 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
592 return AVERROR_INVALIDDATA;
595 /* Allocate decompression buffer */
596 if (c->decomp_size) {
597 if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
598 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
599 return AVERROR(ENOMEM);
604 c->flags = avctx->extradata[6];
605 if (c->flags & FLAG_MULTITHREAD)
606 av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
607 if (c->flags & FLAG_NULLFRAME)
608 av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
609 if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
610 av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
611 if (c->flags & FLAGMASK_UNUSED)
612 av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
614 /* If needed init zlib */
615 #if CONFIG_ZLIB_DECODER
616 if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
618 c->zstream.zalloc = Z_NULL;
619 c->zstream.zfree = Z_NULL;
620 c->zstream.opaque = Z_NULL;
621 zret = inflateInit(&c->zstream);
623 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
624 av_freep(&c->decomp_buf);
625 return AVERROR_UNKNOWN;
638 static av_cold int decode_end(AVCodecContext *avctx)
640 LclDecContext * const c = avctx->priv_data;
642 av_freep(&c->decomp_buf);
643 #if CONFIG_ZLIB_DECODER
644 if (avctx->codec_id == AV_CODEC_ID_ZLIB)
645 inflateEnd(&c->zstream);
651 #if CONFIG_MSZH_DECODER
652 AVCodec ff_mszh_decoder = {
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,
661 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
665 #if CONFIG_ZLIB_DECODER
666 AVCodec ff_zlib_decoder = {
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,
675 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),