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)
43 #include "libavutil/mem.h"
44 #include "libavutil/pixdesc.h"
46 #include "bytestream.h"
50 #if CONFIG_ZLIB_DECODER
54 typedef struct LclDecContext {
61 // Decompressed data size
62 unsigned int decomp_size;
63 // Decompression buffer
64 unsigned char* decomp_buf;
65 #if CONFIG_ZLIB_DECODER
72 * @param srcptr compressed source buffer, must be padded with at least 5 extra bytes
73 * @param destptr must be padded sufficiently for av_memcpy_backptr
75 static unsigned int mszh_decomp(const unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
77 unsigned char *destptr_bak = destptr;
78 unsigned char *destptr_end = destptr + destsize;
79 const unsigned char *srcptr_end = srcptr + srclen;
80 unsigned mask = *srcptr++;
81 unsigned maskbit = 0x80;
83 while (srcptr < srcptr_end && destptr < destptr_end) {
84 if (!(mask & maskbit)) {
85 memcpy(destptr, srcptr, 4);
89 unsigned ofs = bytestream_get_le16(&srcptr);
90 unsigned cnt = (ofs >> 11) + 1;
92 ofs = FFMIN(ofs, destptr - destptr_bak);
94 cnt = FFMIN(cnt, destptr_end - destptr);
96 av_memcpy_backptr(destptr, ofs, cnt);
98 // Not known what the correct behaviour is, but
99 // this at least avoids uninitialized data.
100 memset(destptr, 0, cnt);
108 if (destptr_end - destptr < 32 || srcptr_end - srcptr < 32) break;
109 memcpy(destptr, srcptr, 32);
118 return destptr - destptr_bak;
122 #if CONFIG_ZLIB_DECODER
124 * @brief decompress a zlib-compressed data block into decomp_buf
125 * @param src compressed input buffer
126 * @param src_len data length in input buffer
127 * @param offset offset in decomp_buf
128 * @param expected expected decompressed length
130 static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected)
132 LclDecContext *c = avctx->priv_data;
133 int zret = inflateReset(&c->zstream);
135 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
136 return AVERROR_UNKNOWN;
138 c->zstream.next_in = (uint8_t *)src;
139 c->zstream.avail_in = src_len;
140 c->zstream.next_out = c->decomp_buf + offset;
141 c->zstream.avail_out = c->decomp_size - offset;
142 zret = inflate(&c->zstream, Z_FINISH);
143 if (zret != Z_OK && zret != Z_STREAM_END) {
144 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
145 return AVERROR_UNKNOWN;
147 if (expected != (unsigned int)c->zstream.total_out) {
148 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
149 expected, c->zstream.total_out);
150 return AVERROR_UNKNOWN;
152 return c->zstream.total_out;
157 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
159 AVFrame *frame = data;
160 const uint8_t *buf = avpkt->data;
161 int buf_size = avpkt->size;
162 LclDecContext * const c = avctx->priv_data;
163 unsigned int pixel_ptr;
165 unsigned char *encoded = avpkt->data, *outptr;
166 uint8_t *y_out, *u_out, *v_out;
167 unsigned int width = avctx->width; // Real image width
168 unsigned int height = avctx->height; // Real image height
169 unsigned int mszh_dlen;
170 unsigned char yq, y1q, uq, vq;
172 unsigned int mthread_inlen, mthread_outlen;
173 unsigned int len = buf_size;
176 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
179 outptr = frame->data[0]; // Output image pointer
181 /* Decompress frame */
182 switch (avctx->codec_id) {
183 case AV_CODEC_ID_MSZH:
184 switch (c->compression) {
186 if (c->imgtype == IMGTYPE_RGB24 && len == FFALIGN(width * 3, 4) * height ||
187 c->imgtype == IMGTYPE_YUV111 && len == width * height * 3) {
189 } else if (c->flags & FLAG_MULTITHREAD) {
190 mthread_inlen = AV_RL32(buf);
192 av_log(avctx, AV_LOG_ERROR, "len %d is too small\n", len);
193 return AVERROR_INVALIDDATA;
195 mthread_inlen = FFMIN(mthread_inlen, len - 8);
196 mthread_outlen = AV_RL32(buf + 4);
197 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
198 mszh_dlen = mszh_decomp(buf + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
199 if (mthread_outlen != mszh_dlen) {
200 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
201 mthread_outlen, mszh_dlen);
202 return AVERROR_INVALIDDATA;
204 mszh_dlen = mszh_decomp(buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
205 c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
206 if (mthread_outlen != mszh_dlen) {
207 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
208 mthread_outlen, mszh_dlen);
209 return AVERROR_INVALIDDATA;
211 encoded = c->decomp_buf;
212 len = c->decomp_size;
214 mszh_dlen = mszh_decomp(buf, len, c->decomp_buf, c->decomp_size);
215 if (c->decomp_size != mszh_dlen) {
216 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
217 c->decomp_size, mszh_dlen);
218 return AVERROR_INVALIDDATA;
220 encoded = c->decomp_buf;
224 case COMP_MSZH_NOCOMP: {
226 switch (c->imgtype) {
240 bppx2 = 0; // will error out below
243 if (len < ((width * height * bppx2) >> 1))
244 return AVERROR_INVALIDDATA;
248 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
249 return AVERROR_INVALIDDATA;
252 #if CONFIG_ZLIB_DECODER
253 case AV_CODEC_ID_ZLIB:
254 /* Using the original dll with normal compression (-1) and RGB format
255 * gives a file with ZLIB fourcc, but frame is really uncompressed.
256 * To be sure that's true check also frame size */
257 if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
258 len == width * height * 3) {
259 if (c->flags & FLAG_PNGFILTER) {
260 memcpy(c->decomp_buf, buf, len);
261 encoded = c->decomp_buf;
265 } else if (c->flags & FLAG_MULTITHREAD) {
266 mthread_inlen = AV_RL32(buf);
267 mthread_inlen = FFMIN(mthread_inlen, len - 8);
268 mthread_outlen = AV_RL32(buf + 4);
269 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
270 ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen);
271 if (ret < 0) return ret;
272 ret = zlib_decomp(avctx, buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
273 mthread_outlen, mthread_outlen);
274 if (ret < 0) return ret;
276 int ret = zlib_decomp(avctx, buf, len, 0, c->decomp_size);
277 if (ret < 0) return ret;
279 encoded = c->decomp_buf;
280 len = c->decomp_size;
284 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
285 return AVERROR_INVALIDDATA;
289 /* Apply PNG filter */
290 if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
291 switch (c->imgtype) {
294 for (row = 0; row < height; row++) {
295 pixel_ptr = row * width * 3;
296 yq = encoded[pixel_ptr++];
297 uqvq = AV_RL16(encoded+pixel_ptr);
299 for (col = 1; col < width; col++) {
300 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
301 uqvq -= AV_RL16(encoded+pixel_ptr+1);
302 AV_WL16(encoded+pixel_ptr+1, uqvq);
308 for (row = 0; row < height; row++) {
309 pixel_ptr = row * width * 2;
311 for (col = 0; col < width/4; col++) {
312 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
313 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
314 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
315 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
316 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
317 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
318 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
319 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
325 for (row = 0; row < height; row++) {
326 pixel_ptr = row * width / 2 * 3;
328 for (col = 0; col < width/4; col++) {
329 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
330 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
331 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
332 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
333 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
334 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
340 for (row = 0; row < height; row++) {
341 pixel_ptr = row * width * 2;
343 for (col = 0; col < width/2; col++) {
344 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
345 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
346 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
347 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
353 for (row = 0; row < height/2; row++) {
354 pixel_ptr = row * width * 3;
355 yq = y1q = uq = vq =0;
356 for (col = 0; col < width/2; col++) {
357 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
358 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
359 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
360 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
361 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
362 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
368 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
369 return AVERROR_INVALIDDATA;
373 /* Convert colorspace */
374 y_out = frame->data[0] + (height - 1) * frame->linesize[0];
375 u_out = frame->data[1] + (height - 1) * frame->linesize[1];
376 v_out = frame->data[2] + (height - 1) * frame->linesize[2];
377 switch (c->imgtype) {
379 for (row = 0; row < height; row++) {
380 for (col = 0; col < width; col++) {
381 y_out[col] = *encoded++;
382 u_out[col] = *encoded++ + 128;
383 v_out[col] = *encoded++ + 128;
385 y_out -= frame->linesize[0];
386 u_out -= frame->linesize[1];
387 v_out -= frame->linesize[2];
391 for (row = 0; row < height; row++) {
392 for (col = 0; col < width - 3; col += 4) {
393 memcpy(y_out + col, encoded, 4);
395 u_out[ col >> 1 ] = *encoded++ + 128;
396 u_out[(col >> 1) + 1] = *encoded++ + 128;
397 v_out[ col >> 1 ] = *encoded++ + 128;
398 v_out[(col >> 1) + 1] = *encoded++ + 128;
400 y_out -= frame->linesize[0];
401 u_out -= frame->linesize[1];
402 v_out -= frame->linesize[2];
406 linesize = len < FFALIGN(3 * width, 4) * height ? 3 * width : FFALIGN(3 * width, 4);
407 for (row = height - 1; row >= 0; row--) {
408 pixel_ptr = row * frame->linesize[0];
409 memcpy(outptr + pixel_ptr, encoded, 3 * width);
414 for (row = 0; row < height; row++) {
415 for (col = 0; col < width - 3; col += 4) {
416 memcpy(y_out + col, encoded, 4);
418 u_out[col >> 2] = *encoded++ + 128;
419 v_out[col >> 2] = *encoded++ + 128;
421 y_out -= frame->linesize[0];
422 u_out -= frame->linesize[1];
423 v_out -= frame->linesize[2];
427 for (row = 0; row < height; row++) {
428 for (col = 0; col < width - 1; col += 2) {
429 memcpy(y_out + col, encoded, 2);
431 u_out[col >> 1] = *encoded++ + 128;
432 v_out[col >> 1] = *encoded++ + 128;
434 y_out -= frame->linesize[0];
435 u_out -= frame->linesize[1];
436 v_out -= frame->linesize[2];
440 u_out = frame->data[1] + ((height >> 1) - 1) * frame->linesize[1];
441 v_out = frame->data[2] + ((height >> 1) - 1) * frame->linesize[2];
442 for (row = 0; row < height - 1; row += 2) {
443 for (col = 0; col < width - 1; col += 2) {
444 memcpy(y_out + col, encoded, 2);
446 memcpy(y_out + col - frame->linesize[0], encoded, 2);
448 u_out[col >> 1] = *encoded++ + 128;
449 v_out[col >> 1] = *encoded++ + 128;
451 y_out -= frame->linesize[0] << 1;
452 u_out -= frame->linesize[1];
453 v_out -= frame->linesize[2];
457 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
458 return AVERROR_INVALIDDATA;
463 /* 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;
474 int subsample_h, subsample_v;
476 if (avctx->extradata_size < 8) {
477 av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
478 return AVERROR_INVALIDDATA;
481 /* Check codec type */
482 if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
483 (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
484 av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
487 /* Detect image type */
488 switch (c->imgtype = avctx->extradata[4]) {
490 c->decomp_size = basesize * 3;
491 max_decomp_size = max_basesize * 3;
492 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
493 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
496 c->decomp_size = basesize * 2;
497 max_decomp_size = max_basesize * 2;
498 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
499 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
500 if (avctx->width % 4) {
501 avpriv_request_sample(avctx, "Unsupported dimensions");
502 return AVERROR_INVALIDDATA;
506 c->decomp_size = basesize * 3;
507 max_decomp_size = max_basesize * 3;
508 avctx->pix_fmt = AV_PIX_FMT_BGR24;
509 av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
512 c->decomp_size = basesize / 2 * 3;
513 max_decomp_size = max_basesize / 2 * 3;
514 avctx->pix_fmt = AV_PIX_FMT_YUV411P;
515 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
518 c->decomp_size = basesize * 2;
519 max_decomp_size = max_basesize * 2;
520 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
521 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
524 c->decomp_size = basesize / 2 * 3;
525 max_decomp_size = max_basesize / 2 * 3;
526 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
527 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
530 av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
531 return AVERROR_INVALIDDATA;
534 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &subsample_h, &subsample_v);
535 if (avctx->width % (1<<subsample_h) || avctx->height % (1<<subsample_v)) {
536 avpriv_request_sample(avctx, "Unsupported dimensions");
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))) {
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;
621 static av_cold int decode_end(AVCodecContext *avctx)
623 LclDecContext * const c = avctx->priv_data;
625 av_freep(&c->decomp_buf);
626 #if CONFIG_ZLIB_DECODER
627 if (avctx->codec_id == AV_CODEC_ID_ZLIB)
628 inflateEnd(&c->zstream);
634 #if CONFIG_MSZH_DECODER
635 AVCodec ff_mszh_decoder = {
637 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
638 .type = AVMEDIA_TYPE_VIDEO,
639 .id = AV_CODEC_ID_MSZH,
640 .priv_data_size = sizeof(LclDecContext),
643 .decode = decode_frame,
644 .capabilities = AV_CODEC_CAP_DR1,
645 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
649 #if CONFIG_ZLIB_DECODER
650 AVCodec ff_zlib_decoder = {
652 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
653 .type = AVMEDIA_TYPE_VIDEO,
654 .id = AV_CODEC_ID_ZLIB,
655 .priv_data_size = sizeof(LclDecContext),
658 .decode = decode_frame,
659 .capabilities = AV_CODEC_CAP_DR1,
660 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,