FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lcldec.c
Go to the documentation of this file.
1 /*
2  * LCL (LossLess Codec Library) Codec
3  * Copyright (c) 2002-2004 Roberto Togni
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 /**
23  * @file
24  * LCL (LossLess Codec Library) Video Codec
25  * Decoder for MSZH and ZLIB codecs
26  * Experimental encoder for ZLIB RGB24
27  *
28  * Fourcc: MSZH, ZLIB
29  *
30  * Original Win32 dll:
31  * Ver2.23 By Kenji Oshima 2000.09.20
32  * avimszh.dll, avizlib.dll
33  *
34  * A description of the decoding algorithm can be found here:
35  * http://www.pcisys.net/~melanson/codecs
36  *
37  * Supports: BGR24 (RGB 24bpp)
38  *
39  */
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 
44 #include "libavutil/mem.h"
45 #include "avcodec.h"
46 #include "bytestream.h"
47 #include "internal.h"
48 #include "lcl.h"
49 
50 #if CONFIG_ZLIB_DECODER
51 #include <zlib.h>
52 #endif
53 
54 /*
55  * Decoder context
56  */
57 typedef struct LclDecContext {
59 
60  // Image type
61  int imgtype;
62  // Compression type
64  // Flags
65  int flags;
66  // Decompressed data size
67  unsigned int decomp_size;
68  // Decompression buffer
69  unsigned char* decomp_buf;
70 #if CONFIG_ZLIB_DECODER
71  z_stream zstream;
72 #endif
74 
75 
76 /**
77  * @param srcptr compressed source buffer, must be padded with at least 5 extra bytes
78  * @param destptr must be padded sufficiently for av_memcpy_backptr
79  */
80 static unsigned int mszh_decomp(const unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
81 {
82  unsigned char *destptr_bak = destptr;
83  unsigned char *destptr_end = destptr + destsize;
84  const unsigned char *srcptr_end = srcptr + srclen;
85  unsigned mask = *srcptr++;
86  unsigned maskbit = 0x80;
87 
88  while (srcptr < srcptr_end && destptr < destptr_end) {
89  if (!(mask & maskbit)) {
90  memcpy(destptr, srcptr, 4);
91  destptr += 4;
92  srcptr += 4;
93  } else {
94  unsigned ofs = bytestream_get_le16(&srcptr);
95  unsigned cnt = (ofs >> 11) + 1;
96  ofs &= 0x7ff;
97  ofs = FFMIN(ofs, destptr - destptr_bak);
98  cnt *= 4;
99  cnt = FFMIN(cnt, destptr_end - destptr);
100  if (ofs) {
101  av_memcpy_backptr(destptr, ofs, cnt);
102  } else {
103  // Not known what the correct behaviour is, but
104  // this at least avoids uninitialized data.
105  memset(destptr, 0, cnt);
106  }
107  destptr += cnt;
108  }
109  maskbit >>= 1;
110  if (!maskbit) {
111  mask = *srcptr++;
112  while (!mask) {
113  if (destptr_end - destptr < 32 || srcptr_end - srcptr < 32) break;
114  memcpy(destptr, srcptr, 32);
115  destptr += 32;
116  srcptr += 32;
117  mask = *srcptr++;
118  }
119  maskbit = 0x80;
120  }
121  }
122 
123  return destptr - destptr_bak;
124 }
125 
126 
127 #if CONFIG_ZLIB_DECODER
128 /**
129  * @brief decompress a zlib-compressed data block into decomp_buf
130  * @param src compressed input buffer
131  * @param src_len data length in input buffer
132  * @param offset offset in decomp_buf
133  * @param expected expected decompressed length
134  */
135 static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected)
136 {
137  LclDecContext *c = avctx->priv_data;
138  int zret = inflateReset(&c->zstream);
139  if (zret != Z_OK) {
140  av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
141  return AVERROR_UNKNOWN;
142  }
143  c->zstream.next_in = (uint8_t *)src;
144  c->zstream.avail_in = src_len;
145  c->zstream.next_out = c->decomp_buf + offset;
146  c->zstream.avail_out = c->decomp_size - offset;
147  zret = inflate(&c->zstream, Z_FINISH);
148  if (zret != Z_OK && zret != Z_STREAM_END) {
149  av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
150  return AVERROR_UNKNOWN;
151  }
152  if (expected != (unsigned int)c->zstream.total_out) {
153  av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
154  expected, c->zstream.total_out);
155  return AVERROR_UNKNOWN;
156  }
157  return c->zstream.total_out;
158 }
159 #endif
160 
161 
162 /*
163  *
164  * Decode a frame
165  *
166  */
167 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
168 {
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;
174  int row, col;
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;
181  int uqvq, ret;
182  unsigned int mthread_inlen, mthread_outlen;
183  unsigned int len = buf_size;
184 
185  if(c->pic.data[0])
186  avctx->release_buffer(avctx, &c->pic);
187 
188  c->pic.reference = 0;
190  if ((ret = ff_get_buffer(avctx, &c->pic)) < 0) {
191  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
192  return ret;
193  }
194 
195  outptr = c->pic.data[0]; // Output image pointer
196 
197  /* Decompress frame */
198  switch (avctx->codec_id) {
199  case AV_CODEC_ID_MSZH:
200  switch (c->compression) {
201  case COMP_MSZH:
202  if (c->imgtype == IMGTYPE_RGB24 && len == width * height * 3) {
203  ;
204  } else if (c->flags & FLAG_MULTITHREAD) {
205  mthread_inlen = AV_RL32(encoded);
206  if (len < 8) {
207  av_log(avctx, AV_LOG_ERROR, "len %d is too small\n", len);
208  return AVERROR_INVALIDDATA;
209  }
210  mthread_inlen = FFMIN(mthread_inlen, len - 8);
211  mthread_outlen = AV_RL32(encoded+4);
212  mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
213  mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
214  if (mthread_outlen != mszh_dlen) {
215  av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
216  mthread_outlen, mszh_dlen);
217  return AVERROR_INVALIDDATA;
218  }
219  mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
220  c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
221  if (mthread_outlen != mszh_dlen) {
222  av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
223  mthread_outlen, mszh_dlen);
224  return AVERROR_INVALIDDATA;
225  }
226  encoded = c->decomp_buf;
227  len = c->decomp_size;
228  } else {
229  mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
230  if (c->decomp_size != mszh_dlen) {
231  av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
232  c->decomp_size, mszh_dlen);
233  return AVERROR_INVALIDDATA;
234  }
235  encoded = c->decomp_buf;
236  len = mszh_dlen;
237  }
238  break;
239  case COMP_MSZH_NOCOMP: {
240  int bppx2;
241  switch (c->imgtype) {
242  case IMGTYPE_YUV111:
243  case IMGTYPE_RGB24:
244  bppx2 = 6;
245  break;
246  case IMGTYPE_YUV422:
247  case IMGTYPE_YUV211:
248  bppx2 = 4;
249  break;
250  case IMGTYPE_YUV411:
251  case IMGTYPE_YUV420:
252  bppx2 = 3;
253  break;
254  default:
255  bppx2 = 0; // will error out below
256  break;
257  }
258  if (len < ((width * height * bppx2) >> 1))
259  return AVERROR_INVALIDDATA;
260  break;
261  }
262  default:
263  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
264  return AVERROR_INVALIDDATA;
265  }
266  break;
267 #if CONFIG_ZLIB_DECODER
268  case AV_CODEC_ID_ZLIB:
269  /* Using the original dll with normal compression (-1) and RGB format
270  * gives a file with ZLIB fourcc, but frame is really uncompressed.
271  * To be sure that's true check also frame size */
272  if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
273  len == width * height * 3) {
274  if (c->flags & FLAG_PNGFILTER) {
275  memcpy(c->decomp_buf, encoded, len);
276  encoded = c->decomp_buf;
277  } else {
278  break;
279  }
280  } else if (c->flags & FLAG_MULTITHREAD) {
281  mthread_inlen = AV_RL32(encoded);
282  mthread_inlen = FFMIN(mthread_inlen, len - 8);
283  mthread_outlen = AV_RL32(encoded+4);
284  mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
285  ret = zlib_decomp(avctx, encoded + 8, mthread_inlen, 0, mthread_outlen);
286  if (ret < 0) return ret;
287  ret = zlib_decomp(avctx, encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
288  mthread_outlen, mthread_outlen);
289  if (ret < 0) return ret;
290  } else {
291  int ret = zlib_decomp(avctx, encoded, len, 0, c->decomp_size);
292  if (ret < 0) return ret;
293  }
294  encoded = c->decomp_buf;
295  len = c->decomp_size;
296  break;
297 #endif
298  default:
299  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
300  return AVERROR_INVALIDDATA;
301  }
302 
303 
304  /* Apply PNG filter */
305  if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
306  switch (c->imgtype) {
307  case IMGTYPE_YUV111:
308  case IMGTYPE_RGB24:
309  for (row = 0; row < height; row++) {
310  pixel_ptr = row * width * 3;
311  yq = encoded[pixel_ptr++];
312  uqvq = AV_RL16(encoded+pixel_ptr);
313  pixel_ptr += 2;
314  for (col = 1; col < width; col++) {
315  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
316  uqvq -= AV_RL16(encoded+pixel_ptr+1);
317  AV_WL16(encoded+pixel_ptr+1, uqvq);
318  pixel_ptr += 3;
319  }
320  }
321  break;
322  case IMGTYPE_YUV422:
323  for (row = 0; row < height; row++) {
324  pixel_ptr = row * width * 2;
325  yq = uq = vq =0;
326  for (col = 0; col < width/4; col++) {
327  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
328  encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
329  encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
330  encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
331  encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
332  encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
333  encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
334  encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
335  pixel_ptr += 8;
336  }
337  }
338  break;
339  case IMGTYPE_YUV411:
340  for (row = 0; row < height; row++) {
341  pixel_ptr = row * width / 2 * 3;
342  yq = uq = vq =0;
343  for (col = 0; col < width/4; col++) {
344  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
345  encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
346  encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
347  encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
348  encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
349  encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
350  pixel_ptr += 6;
351  }
352  }
353  break;
354  case IMGTYPE_YUV211:
355  for (row = 0; row < height; row++) {
356  pixel_ptr = row * width * 2;
357  yq = uq = vq =0;
358  for (col = 0; col < width/2; col++) {
359  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
360  encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
361  encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
362  encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
363  pixel_ptr += 4;
364  }
365  }
366  break;
367  case IMGTYPE_YUV420:
368  for (row = 0; row < height/2; row++) {
369  pixel_ptr = row * width * 3;
370  yq = y1q = uq = vq =0;
371  for (col = 0; col < width/2; col++) {
372  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
373  encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
374  encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
375  encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
376  encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
377  encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
378  pixel_ptr += 6;
379  }
380  }
381  break;
382  default:
383  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
384  return AVERROR_INVALIDDATA;
385  }
386  }
387 
388  /* Convert colorspace */
389  y_out = c->pic.data[0] + (height - 1) * c->pic.linesize[0];
390  u_out = c->pic.data[1] + (height - 1) * c->pic.linesize[1];
391  v_out = c->pic.data[2] + (height - 1) * c->pic.linesize[2];
392  switch (c->imgtype) {
393  case IMGTYPE_YUV111:
394  for (row = 0; row < height; row++) {
395  for (col = 0; col < width; col++) {
396  y_out[col] = *encoded++;
397  u_out[col] = *encoded++ + 128;
398  v_out[col] = *encoded++ + 128;
399  }
400  y_out -= c->pic.linesize[0];
401  u_out -= c->pic.linesize[1];
402  v_out -= c->pic.linesize[2];
403  }
404  break;
405  case IMGTYPE_YUV422:
406  for (row = 0; row < height; row++) {
407  for (col = 0; col < width - 3; col += 4) {
408  memcpy(y_out + col, encoded, 4);
409  encoded += 4;
410  u_out[ col >> 1 ] = *encoded++ + 128;
411  u_out[(col >> 1) + 1] = *encoded++ + 128;
412  v_out[ col >> 1 ] = *encoded++ + 128;
413  v_out[(col >> 1) + 1] = *encoded++ + 128;
414  }
415  y_out -= c->pic.linesize[0];
416  u_out -= c->pic.linesize[1];
417  v_out -= c->pic.linesize[2];
418  }
419  break;
420  case IMGTYPE_RGB24:
421  for (row = height - 1; row >= 0; row--) {
422  pixel_ptr = row * c->pic.linesize[0];
423  memcpy(outptr + pixel_ptr, encoded, 3 * width);
424  encoded += 3 * width;
425  }
426  break;
427  case IMGTYPE_YUV411:
428  for (row = 0; row < height; row++) {
429  for (col = 0; col < width - 3; col += 4) {
430  memcpy(y_out + col, encoded, 4);
431  encoded += 4;
432  u_out[col >> 2] = *encoded++ + 128;
433  v_out[col >> 2] = *encoded++ + 128;
434  }
435  y_out -= c->pic.linesize[0];
436  u_out -= c->pic.linesize[1];
437  v_out -= c->pic.linesize[2];
438  }
439  break;
440  case IMGTYPE_YUV211:
441  for (row = 0; row < height; row++) {
442  for (col = 0; col < width - 1; col += 2) {
443  memcpy(y_out + col, encoded, 2);
444  encoded += 2;
445  u_out[col >> 1] = *encoded++ + 128;
446  v_out[col >> 1] = *encoded++ + 128;
447  }
448  y_out -= c->pic.linesize[0];
449  u_out -= c->pic.linesize[1];
450  v_out -= c->pic.linesize[2];
451  }
452  break;
453  case IMGTYPE_YUV420:
454  u_out = c->pic.data[1] + ((height >> 1) - 1) * c->pic.linesize[1];
455  v_out = c->pic.data[2] + ((height >> 1) - 1) * c->pic.linesize[2];
456  for (row = 0; row < height - 1; row += 2) {
457  for (col = 0; col < width - 1; col += 2) {
458  memcpy(y_out + col, encoded, 2);
459  encoded += 2;
460  memcpy(y_out + col - c->pic.linesize[0], encoded, 2);
461  encoded += 2;
462  u_out[col >> 1] = *encoded++ + 128;
463  v_out[col >> 1] = *encoded++ + 128;
464  }
465  y_out -= c->pic.linesize[0] << 1;
466  u_out -= c->pic.linesize[1];
467  v_out -= c->pic.linesize[2];
468  }
469  break;
470  default:
471  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
472  return AVERROR_INVALIDDATA;
473  }
474 
475  *got_frame = 1;
476  *(AVFrame*)data = c->pic;
477 
478  /* always report that the buffer was completely consumed */
479  return buf_size;
480 }
481 
482 /*
483  *
484  * Init lcl decoder
485  *
486  */
488 {
489  LclDecContext * const c = avctx->priv_data;
490  unsigned int basesize = avctx->width * avctx->height;
491  unsigned int max_basesize = FFALIGN(avctx->width, 4) *
492  FFALIGN(avctx->height, 4);
493  unsigned int max_decomp_size;
494 
496  if (avctx->extradata_size < 8) {
497  av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
498  return AVERROR_INVALIDDATA;
499  }
500 
501  /* Check codec type */
502  if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
503  (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
504  av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
505  }
506 
507  /* Detect image type */
508  switch (c->imgtype = avctx->extradata[4]) {
509  case IMGTYPE_YUV111:
510  c->decomp_size = basesize * 3;
511  max_decomp_size = max_basesize * 3;
512  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
513  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
514  break;
515  case IMGTYPE_YUV422:
516  c->decomp_size = basesize * 2;
517  max_decomp_size = max_basesize * 2;
518  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
519  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
520  break;
521  case IMGTYPE_RGB24:
522  c->decomp_size = basesize * 3;
523  max_decomp_size = max_basesize * 3;
524  avctx->pix_fmt = AV_PIX_FMT_BGR24;
525  av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
526  break;
527  case IMGTYPE_YUV411:
528  c->decomp_size = basesize / 2 * 3;
529  max_decomp_size = max_basesize / 2 * 3;
530  avctx->pix_fmt = AV_PIX_FMT_YUV411P;
531  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
532  break;
533  case IMGTYPE_YUV211:
534  c->decomp_size = basesize * 2;
535  max_decomp_size = max_basesize * 2;
536  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
537  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
538  break;
539  case IMGTYPE_YUV420:
540  c->decomp_size = basesize / 2 * 3;
541  max_decomp_size = max_basesize / 2 * 3;
542  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
543  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
544  break;
545  default:
546  av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
547  return AVERROR_INVALIDDATA;
548  }
549 
550  /* Detect compression method */
551  c->compression = (int8_t)avctx->extradata[5];
552  switch (avctx->codec_id) {
553  case AV_CODEC_ID_MSZH:
554  switch (c->compression) {
555  case COMP_MSZH:
556  av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
557  break;
558  case COMP_MSZH_NOCOMP:
559  c->decomp_size = 0;
560  av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
561  break;
562  default:
563  av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
564  return AVERROR_INVALIDDATA;
565  }
566  break;
567 #if CONFIG_ZLIB_DECODER
568  case AV_CODEC_ID_ZLIB:
569  switch (c->compression) {
570  case COMP_ZLIB_HISPEED:
571  av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
572  break;
573  case COMP_ZLIB_HICOMP:
574  av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
575  break;
576  case COMP_ZLIB_NORMAL:
577  av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
578  break;
579  default:
580  if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
581  av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
582  return AVERROR_INVALIDDATA;
583  }
584  av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
585  }
586  break;
587 #endif
588  default:
589  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
590  return AVERROR_INVALIDDATA;
591  }
592 
593  /* Allocate decompression buffer */
594  if (c->decomp_size) {
595  if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
596  av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
597  return AVERROR(ENOMEM);
598  }
599  }
600 
601  /* Detect flags */
602  c->flags = avctx->extradata[6];
603  if (c->flags & FLAG_MULTITHREAD)
604  av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
605  if (c->flags & FLAG_NULLFRAME)
606  av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
607  if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
608  av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
609  if (c->flags & FLAGMASK_UNUSED)
610  av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
611 
612  /* If needed init zlib */
613 #if CONFIG_ZLIB_DECODER
614  if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
615  int zret;
616  c->zstream.zalloc = Z_NULL;
617  c->zstream.zfree = Z_NULL;
618  c->zstream.opaque = Z_NULL;
619  zret = inflateInit(&c->zstream);
620  if (zret != Z_OK) {
621  av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
622  av_freep(&c->decomp_buf);
623  return AVERROR_UNKNOWN;
624  }
625  }
626 #endif
627 
628  return 0;
629 }
630 
631 /*
632  *
633  * Uninit lcl decoder
634  *
635  */
637 {
638  LclDecContext * const c = avctx->priv_data;
639 
640  av_freep(&c->decomp_buf);
641  if (c->pic.data[0])
642  avctx->release_buffer(avctx, &c->pic);
643 #if CONFIG_ZLIB_DECODER
644  if (avctx->codec_id == AV_CODEC_ID_ZLIB)
645  inflateEnd(&c->zstream);
646 #endif
647 
648  return 0;
649 }
650 
651 #if CONFIG_MSZH_DECODER
652 AVCodec ff_mszh_decoder = {
653  .name = "mszh",
654  .type = AVMEDIA_TYPE_VIDEO,
655  .id = AV_CODEC_ID_MSZH,
656  .priv_data_size = sizeof(LclDecContext),
657  .init = decode_init,
658  .close = decode_end,
659  .decode = decode_frame,
660  .capabilities = CODEC_CAP_DR1,
661  .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
662 };
663 #endif
664 
665 #if CONFIG_ZLIB_DECODER
666 AVCodec ff_zlib_decoder = {
667  .name = "zlib",
668  .type = AVMEDIA_TYPE_VIDEO,
669  .id = AV_CODEC_ID_ZLIB,
670  .priv_data_size = sizeof(LclDecContext),
671  .init = decode_init,
672  .close = decode_end,
673  .decode = decode_frame,
674  .capabilities = CODEC_CAP_DR1,
675  .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
676 };
677 #endif