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
23 * @file libavcodec/lcldec.c
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)
48 #if CONFIG_ZLIB_DECODER
55 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
74 static unsigned int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
76 unsigned char *destptr_bak = destptr;
77 unsigned char *destptr_end = destptr + destsize;
78 unsigned char mask = 0;
79 unsigned char maskbit = 0;
80 unsigned int ofs, cnt;
82 while (srclen > 0 && destptr < destptr_end) {
89 if ((mask & (1 << (--maskbit))) == 0) {
90 if (destptr + 4 > destptr_end)
92 AV_WN32(destptr, AV_RN32(srcptr));
100 cnt = ((cnt >> 3) & 0x1f) + 1;
104 if (destptr + cnt > destptr_end) {
105 cnt = destptr_end - destptr;
107 for (; cnt > 0; cnt--) {
108 *destptr = *(destptr - ofs);
114 return destptr - destptr_bak;
119 * \brief decompress a zlib-compressed data block into decomp_buf
120 * \param src compressed input buffer
121 * \param src_len data length in input buffer
122 * \param offset offset in decomp_buf
123 * \param expected expected decompressed length
125 static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected)
127 LclDecContext *c = avctx->priv_data;
128 int zret = inflateReset(&c->zstream);
130 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
133 c->zstream.next_in = src;
134 c->zstream.avail_in = src_len;
135 c->zstream.next_out = c->decomp_buf + offset;
136 c->zstream.avail_out = c->decomp_size - offset;
137 zret = inflate(&c->zstream, Z_FINISH);
138 if (zret != Z_OK && zret != Z_STREAM_END) {
139 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
142 if (expected != (unsigned int)c->zstream.total_out) {
143 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
144 expected, c->zstream.total_out);
147 return c->zstream.total_out;
156 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
158 const uint8_t *buf = avpkt->data;
159 int buf_size = avpkt->size;
160 LclDecContext * const c = avctx->priv_data;
161 unsigned char *encoded = (unsigned char *)buf;
162 unsigned int pixel_ptr;
164 unsigned char *outptr;
165 uint8_t *y_out, *u_out, *v_out;
166 unsigned int width = avctx->width; // Real image width
167 unsigned int height = avctx->height; // Real image height
168 unsigned int mszh_dlen;
169 unsigned char yq, y1q, uq, vq;
171 unsigned int mthread_inlen, mthread_outlen;
172 unsigned int len = buf_size;
175 avctx->release_buffer(avctx, &c->pic);
177 c->pic.reference = 0;
178 c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
179 if(avctx->get_buffer(avctx, &c->pic) < 0){
180 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
184 outptr = c->pic.data[0]; // Output image pointer
186 /* Decompress frame */
187 switch (avctx->codec_id) {
189 switch (c->compression) {
191 if (c->flags & FLAG_MULTITHREAD) {
192 mthread_inlen = *(unsigned int*)encoded;
193 mthread_inlen = FFMIN(mthread_inlen, len - 8);
194 mthread_outlen = *(unsigned int*)(encoded+4);
195 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
196 mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
197 if (mthread_outlen != mszh_dlen) {
198 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
199 mthread_outlen, mszh_dlen);
202 mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
203 c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
204 if (mthread_outlen != mszh_dlen) {
205 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
206 mthread_outlen, mszh_dlen);
209 encoded = c->decomp_buf;
210 len = c->decomp_size;
212 mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
213 if (c->decomp_size != mszh_dlen) {
214 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
215 c->decomp_size, mszh_dlen);
218 encoded = c->decomp_buf;
222 case COMP_MSZH_NOCOMP:
225 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
229 #if CONFIG_ZLIB_DECODER
231 /* Using the original dll with normal compression (-1) and RGB format
232 * gives a file with ZLIB fourcc, but frame is really uncompressed.
233 * To be sure that's true check also frame size */
234 if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
235 len == width * height * 3)
237 if (c->flags & FLAG_MULTITHREAD) {
239 mthread_inlen = *(unsigned int*)encoded;
240 mthread_inlen = FFMIN(mthread_inlen, len - 8);
241 mthread_outlen = *(unsigned int*)(encoded+4);
242 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
243 ret = zlib_decomp(avctx, encoded + 8, mthread_inlen, 0, mthread_outlen);
244 if (ret < 0) return ret;
245 ret = zlib_decomp(avctx, encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
246 mthread_outlen, mthread_outlen);
247 if (ret < 0) return ret;
249 int ret = zlib_decomp(avctx, encoded, len, 0, c->decomp_size);
250 if (ret < 0) return ret;
252 encoded = c->decomp_buf;
253 len = c->decomp_size;
257 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
262 /* Apply PNG filter */
263 if (avctx->codec_id == CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
264 switch (c->imgtype) {
267 for (row = 0; row < height; row++) {
268 pixel_ptr = row * width * 3;
269 yq = encoded[pixel_ptr++];
270 uqvq = AV_RL16(encoded+pixel_ptr);
272 for (col = 1; col < width; col++) {
273 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
274 uqvq -= AV_RL16(encoded+pixel_ptr+1);
275 AV_WL16(encoded+pixel_ptr+1, uqvq);
281 for (row = 0; row < height; row++) {
282 pixel_ptr = row * width * 2;
284 for (col = 0; col < width/4; col++) {
285 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
286 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
287 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
288 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
289 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
290 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
291 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
292 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
298 for (row = 0; row < height; row++) {
299 pixel_ptr = row * width / 2 * 3;
301 for (col = 0; col < width/4; col++) {
302 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
303 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
304 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
305 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
306 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
307 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
313 for (row = 0; row < height; row++) {
314 pixel_ptr = row * width * 2;
316 for (col = 0; col < width/2; col++) {
317 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
318 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
319 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
320 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
326 for (row = 0; row < height/2; row++) {
327 pixel_ptr = row * width * 3;
328 yq = y1q = uq = vq =0;
329 for (col = 0; col < width/2; col++) {
330 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
331 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
332 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
333 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
334 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
335 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
341 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
346 /* Convert colorspace */
347 y_out = c->pic.data[0] + (height - 1) * c->pic.linesize[0];
348 u_out = c->pic.data[1] + (height - 1) * c->pic.linesize[1];
349 v_out = c->pic.data[2] + (height - 1) * c->pic.linesize[2];
350 switch (c->imgtype) {
352 for (row = 0; row < height; row++) {
353 for (col = 0; col < width; col++) {
354 y_out[col] = *encoded++;
355 u_out[col] = *encoded++ + 128;
356 v_out[col] = *encoded++ + 128;
358 y_out -= c->pic.linesize[0];
359 u_out -= c->pic.linesize[1];
360 v_out -= c->pic.linesize[2];
364 for (row = 0; row < height; row++) {
365 for (col = 0; col < width - 3; col += 4) {
366 memcpy(y_out + col, encoded, 4);
368 u_out[ col >> 1 ] = *encoded++ + 128;
369 u_out[(col >> 1) + 1] = *encoded++ + 128;
370 v_out[ col >> 1 ] = *encoded++ + 128;
371 v_out[(col >> 1) + 1] = *encoded++ + 128;
373 y_out -= c->pic.linesize[0];
374 u_out -= c->pic.linesize[1];
375 v_out -= c->pic.linesize[2];
379 for (row = height - 1; row >= 0; row--) {
380 pixel_ptr = row * c->pic.linesize[0];
381 memcpy(outptr + pixel_ptr, encoded, 3 * width);
382 encoded += 3 * width;
386 for (row = 0; row < height; row++) {
387 for (col = 0; col < width - 3; col += 4) {
388 memcpy(y_out + col, encoded, 4);
390 u_out[col >> 2] = *encoded++ + 128;
391 v_out[col >> 2] = *encoded++ + 128;
393 y_out -= c->pic.linesize[0];
394 u_out -= c->pic.linesize[1];
395 v_out -= c->pic.linesize[2];
399 for (row = 0; row < height; row++) {
400 for (col = 0; col < width - 1; col += 2) {
401 memcpy(y_out + col, encoded, 2);
403 u_out[col >> 1] = *encoded++ + 128;
404 v_out[col >> 1] = *encoded++ + 128;
406 y_out -= c->pic.linesize[0];
407 u_out -= c->pic.linesize[1];
408 v_out -= c->pic.linesize[2];
412 u_out = c->pic.data[1] + ((height >> 1) - 1) * c->pic.linesize[1];
413 v_out = c->pic.data[2] + ((height >> 1) - 1) * c->pic.linesize[2];
414 for (row = 0; row < height - 1; row += 2) {
415 for (col = 0; col < width - 1; col += 2) {
416 memcpy(y_out + col, encoded, 2);
418 memcpy(y_out + col - c->pic.linesize[0], encoded, 2);
420 u_out[col >> 1] = *encoded++ + 128;
421 v_out[col >> 1] = *encoded++ + 128;
423 y_out -= c->pic.linesize[0] << 1;
424 u_out -= c->pic.linesize[1];
425 v_out -= c->pic.linesize[2];
429 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
433 *data_size = sizeof(AVFrame);
434 *(AVFrame*)data = c->pic;
436 /* always report that the buffer was completely consumed */
445 static av_cold int decode_init(AVCodecContext *avctx)
447 LclDecContext * const c = avctx->priv_data;
448 unsigned int basesize = avctx->width * avctx->height;
449 unsigned int max_basesize = FFALIGN(avctx->width, 4) * FFALIGN(avctx->height, 4);
450 unsigned int max_decomp_size;
452 c->pic.data[0] = NULL;
454 #if CONFIG_ZLIB_DECODER
455 // Needed if zlib unused or init aborted before inflateInit
456 memset(&c->zstream, 0, sizeof(z_stream));
459 if (avctx->extradata_size < 8) {
460 av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
464 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
468 /* Check codec type */
469 if ((avctx->codec_id == CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
470 (avctx->codec_id == CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
471 av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
474 /* Detect image type */
475 switch (c->imgtype = avctx->extradata[4]) {
477 c->decomp_size = basesize * 3;
478 max_decomp_size = max_basesize * 3;
479 avctx->pix_fmt = PIX_FMT_YUV444P;
480 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
483 c->decomp_size = basesize * 2;
484 max_decomp_size = max_basesize * 2;
485 avctx->pix_fmt = PIX_FMT_YUV422P;
486 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
489 c->decomp_size = basesize * 3;
490 max_decomp_size = max_basesize * 3;
491 avctx->pix_fmt = PIX_FMT_BGR24;
492 av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
495 c->decomp_size = basesize / 2 * 3;
496 max_decomp_size = max_basesize / 2 * 3;
497 avctx->pix_fmt = PIX_FMT_YUV411P;
498 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
501 c->decomp_size = basesize * 2;
502 max_decomp_size = max_basesize * 2;
503 avctx->pix_fmt = PIX_FMT_YUV422P;
504 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
507 c->decomp_size = basesize / 2 * 3;
508 max_decomp_size = max_basesize / 2 * 3;
509 avctx->pix_fmt = PIX_FMT_YUV420P;
510 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
513 av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
517 /* Detect compression method */
518 c->compression = avctx->extradata[5];
519 switch (avctx->codec_id) {
521 switch (c->compression) {
523 av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
525 case COMP_MSZH_NOCOMP:
527 av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
530 av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
534 #if CONFIG_ZLIB_DECODER
536 switch (c->compression) {
537 case COMP_ZLIB_HISPEED:
538 av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
540 case COMP_ZLIB_HICOMP:
541 av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
543 case COMP_ZLIB_NORMAL:
544 av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
547 if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
548 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
551 av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
556 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
560 /* Allocate decompression buffer */
561 if (c->decomp_size) {
562 if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
563 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
569 c->flags = avctx->extradata[6];
570 if (c->flags & FLAG_MULTITHREAD)
571 av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
572 if (c->flags & FLAG_NULLFRAME)
573 av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
574 if (avctx->codec_id == CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
575 av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
576 if (c->flags & FLAGMASK_UNUSED)
577 av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
579 /* If needed init zlib */
580 #if CONFIG_ZLIB_DECODER
581 if (avctx->codec_id == CODEC_ID_ZLIB) {
583 c->zstream.zalloc = Z_NULL;
584 c->zstream.zfree = Z_NULL;
585 c->zstream.opaque = Z_NULL;
586 zret = inflateInit(&c->zstream);
588 av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
602 static av_cold int decode_end(AVCodecContext *avctx)
604 LclDecContext * const c = avctx->priv_data;
607 avctx->release_buffer(avctx, &c->pic);
608 #if CONFIG_ZLIB_DECODER
609 inflateEnd(&c->zstream);
615 #if CONFIG_MSZH_DECODER
616 AVCodec mszh_decoder = {
620 sizeof(LclDecContext),
626 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
630 #if CONFIG_ZLIB_DECODER
631 AVCodec zlib_decoder = {
635 sizeof(LclDecContext),
641 .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),