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 -1;
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 -1;
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 -1;
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;
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(ff_get_buffer(avctx, &c->pic) < 0){
191  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
192  return -1;
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  mthread_inlen = FFMIN(mthread_inlen, len - 8);
207  mthread_outlen = AV_RL32(encoded+4);
208  mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
209  mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
210  if (mthread_outlen != mszh_dlen) {
211  av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
212  mthread_outlen, mszh_dlen);
213  return -1;
214  }
215  mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
216  c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
217  if (mthread_outlen != mszh_dlen) {
218  av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
219  mthread_outlen, mszh_dlen);
220  return -1;
221  }
222  encoded = c->decomp_buf;
223  len = c->decomp_size;
224  } else {
225  mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
226  if (c->decomp_size != mszh_dlen) {
227  av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
228  c->decomp_size, mszh_dlen);
229  return -1;
230  }
231  encoded = c->decomp_buf;
232  len = mszh_dlen;
233  }
234  break;
235  case COMP_MSZH_NOCOMP: {
236  int bppx2;
237  switch (c->imgtype) {
238  case IMGTYPE_YUV111:
239  case IMGTYPE_RGB24:
240  bppx2 = 6;
241  break;
242  case IMGTYPE_YUV422:
243  case IMGTYPE_YUV211:
244  bppx2 = 4;
245  break;
246  case IMGTYPE_YUV411:
247  case IMGTYPE_YUV420:
248  bppx2 = 3;
249  break;
250  default:
251  bppx2 = 0; // will error out below
252  break;
253  }
254  if (len < ((width * height * bppx2) >> 1))
255  return AVERROR_INVALIDDATA;
256  break;
257  }
258  default:
259  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
260  return -1;
261  }
262  break;
263 #if CONFIG_ZLIB_DECODER
264  case AV_CODEC_ID_ZLIB:
265  /* Using the original dll with normal compression (-1) and RGB format
266  * gives a file with ZLIB fourcc, but frame is really uncompressed.
267  * To be sure that's true check also frame size */
268  if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
269  len == width * height * 3) {
270  if (c->flags & FLAG_PNGFILTER) {
271  memcpy(c->decomp_buf, encoded, len);
272  encoded = c->decomp_buf;
273  } else {
274  break;
275  }
276  } else if (c->flags & FLAG_MULTITHREAD) {
277  int ret;
278  mthread_inlen = AV_RL32(encoded);
279  mthread_inlen = FFMIN(mthread_inlen, len - 8);
280  mthread_outlen = AV_RL32(encoded+4);
281  mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
282  ret = zlib_decomp(avctx, encoded + 8, mthread_inlen, 0, mthread_outlen);
283  if (ret < 0) return ret;
284  ret = zlib_decomp(avctx, encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
285  mthread_outlen, mthread_outlen);
286  if (ret < 0) return ret;
287  } else {
288  int ret = zlib_decomp(avctx, encoded, len, 0, c->decomp_size);
289  if (ret < 0) return ret;
290  }
291  encoded = c->decomp_buf;
292  len = c->decomp_size;
293  break;
294 #endif
295  default:
296  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
297  return -1;
298  }
299 
300 
301  /* Apply PNG filter */
302  if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
303  switch (c->imgtype) {
304  case IMGTYPE_YUV111:
305  case IMGTYPE_RGB24:
306  for (row = 0; row < height; row++) {
307  pixel_ptr = row * width * 3;
308  yq = encoded[pixel_ptr++];
309  uqvq = AV_RL16(encoded+pixel_ptr);
310  pixel_ptr += 2;
311  for (col = 1; col < width; col++) {
312  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
313  uqvq -= AV_RL16(encoded+pixel_ptr+1);
314  AV_WL16(encoded+pixel_ptr+1, uqvq);
315  pixel_ptr += 3;
316  }
317  }
318  break;
319  case IMGTYPE_YUV422:
320  for (row = 0; row < height; row++) {
321  pixel_ptr = row * width * 2;
322  yq = uq = vq =0;
323  for (col = 0; col < width/4; col++) {
324  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
325  encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
326  encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
327  encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
328  encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
329  encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
330  encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
331  encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
332  pixel_ptr += 8;
333  }
334  }
335  break;
336  case IMGTYPE_YUV411:
337  for (row = 0; row < height; row++) {
338  pixel_ptr = row * width / 2 * 3;
339  yq = uq = vq =0;
340  for (col = 0; col < width/4; col++) {
341  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
342  encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
343  encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
344  encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
345  encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
346  encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
347  pixel_ptr += 6;
348  }
349  }
350  break;
351  case IMGTYPE_YUV211:
352  for (row = 0; row < height; row++) {
353  pixel_ptr = row * width * 2;
354  yq = uq = vq =0;
355  for (col = 0; col < width/2; col++) {
356  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
357  encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
358  encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
359  encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
360  pixel_ptr += 4;
361  }
362  }
363  break;
364  case IMGTYPE_YUV420:
365  for (row = 0; row < height/2; row++) {
366  pixel_ptr = row * width * 3;
367  yq = y1q = uq = vq =0;
368  for (col = 0; col < width/2; col++) {
369  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
370  encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
371  encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
372  encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
373  encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
374  encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
375  pixel_ptr += 6;
376  }
377  }
378  break;
379  default:
380  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
381  return -1;
382  }
383  }
384 
385  /* Convert colorspace */
386  y_out = c->pic.data[0] + (height - 1) * c->pic.linesize[0];
387  u_out = c->pic.data[1] + (height - 1) * c->pic.linesize[1];
388  v_out = c->pic.data[2] + (height - 1) * c->pic.linesize[2];
389  switch (c->imgtype) {
390  case IMGTYPE_YUV111:
391  for (row = 0; row < height; row++) {
392  for (col = 0; col < width; col++) {
393  y_out[col] = *encoded++;
394  u_out[col] = *encoded++ + 128;
395  v_out[col] = *encoded++ + 128;
396  }
397  y_out -= c->pic.linesize[0];
398  u_out -= c->pic.linesize[1];
399  v_out -= c->pic.linesize[2];
400  }
401  break;
402  case IMGTYPE_YUV422:
403  for (row = 0; row < height; row++) {
404  for (col = 0; col < width - 3; col += 4) {
405  memcpy(y_out + col, encoded, 4);
406  encoded += 4;
407  u_out[ col >> 1 ] = *encoded++ + 128;
408  u_out[(col >> 1) + 1] = *encoded++ + 128;
409  v_out[ col >> 1 ] = *encoded++ + 128;
410  v_out[(col >> 1) + 1] = *encoded++ + 128;
411  }
412  y_out -= c->pic.linesize[0];
413  u_out -= c->pic.linesize[1];
414  v_out -= c->pic.linesize[2];
415  }
416  break;
417  case IMGTYPE_RGB24:
418  for (row = height - 1; row >= 0; row--) {
419  pixel_ptr = row * c->pic.linesize[0];
420  memcpy(outptr + pixel_ptr, encoded, 3 * width);
421  encoded += 3 * width;
422  }
423  break;
424  case IMGTYPE_YUV411:
425  for (row = 0; row < height; row++) {
426  for (col = 0; col < width - 3; col += 4) {
427  memcpy(y_out + col, encoded, 4);
428  encoded += 4;
429  u_out[col >> 2] = *encoded++ + 128;
430  v_out[col >> 2] = *encoded++ + 128;
431  }
432  y_out -= c->pic.linesize[0];
433  u_out -= c->pic.linesize[1];
434  v_out -= c->pic.linesize[2];
435  }
436  break;
437  case IMGTYPE_YUV211:
438  for (row = 0; row < height; row++) {
439  for (col = 0; col < width - 1; col += 2) {
440  memcpy(y_out + col, encoded, 2);
441  encoded += 2;
442  u_out[col >> 1] = *encoded++ + 128;
443  v_out[col >> 1] = *encoded++ + 128;
444  }
445  y_out -= c->pic.linesize[0];
446  u_out -= c->pic.linesize[1];
447  v_out -= c->pic.linesize[2];
448  }
449  break;
450  case IMGTYPE_YUV420:
451  u_out = c->pic.data[1] + ((height >> 1) - 1) * c->pic.linesize[1];
452  v_out = c->pic.data[2] + ((height >> 1) - 1) * c->pic.linesize[2];
453  for (row = 0; row < height - 1; row += 2) {
454  for (col = 0; col < width - 1; col += 2) {
455  memcpy(y_out + col, encoded, 2);
456  encoded += 2;
457  memcpy(y_out + col - c->pic.linesize[0], encoded, 2);
458  encoded += 2;
459  u_out[col >> 1] = *encoded++ + 128;
460  v_out[col >> 1] = *encoded++ + 128;
461  }
462  y_out -= c->pic.linesize[0] << 1;
463  u_out -= c->pic.linesize[1];
464  v_out -= c->pic.linesize[2];
465  }
466  break;
467  default:
468  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
469  return -1;
470  }
471 
472  *got_frame = 1;
473  *(AVFrame*)data = c->pic;
474 
475  /* always report that the buffer was completely consumed */
476  return buf_size;
477 }
478 
479 /*
480  *
481  * Init lcl decoder
482  *
483  */
485 {
486  LclDecContext * const c = avctx->priv_data;
487  unsigned int basesize = avctx->width * avctx->height;
488  unsigned int max_basesize = FFALIGN(avctx->width, 4) *
489  FFALIGN(avctx->height, 4);
490  unsigned int max_decomp_size;
491 
493  if (avctx->extradata_size < 8) {
494  av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
495  return AVERROR_INVALIDDATA;
496  }
497 
498  /* Check codec type */
499  if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
500  (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
501  av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
502  }
503 
504  /* Detect image type */
505  switch (c->imgtype = avctx->extradata[4]) {
506  case IMGTYPE_YUV111:
507  c->decomp_size = basesize * 3;
508  max_decomp_size = max_basesize * 3;
509  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
510  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
511  break;
512  case IMGTYPE_YUV422:
513  c->decomp_size = basesize * 2;
514  max_decomp_size = max_basesize * 2;
515  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
516  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
517  break;
518  case IMGTYPE_RGB24:
519  c->decomp_size = basesize * 3;
520  max_decomp_size = max_basesize * 3;
521  avctx->pix_fmt = AV_PIX_FMT_BGR24;
522  av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
523  break;
524  case IMGTYPE_YUV411:
525  c->decomp_size = basesize / 2 * 3;
526  max_decomp_size = max_basesize / 2 * 3;
527  avctx->pix_fmt = AV_PIX_FMT_YUV411P;
528  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
529  break;
530  case IMGTYPE_YUV211:
531  c->decomp_size = basesize * 2;
532  max_decomp_size = max_basesize * 2;
533  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
534  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
535  break;
536  case IMGTYPE_YUV420:
537  c->decomp_size = basesize / 2 * 3;
538  max_decomp_size = max_basesize / 2 * 3;
539  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
540  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
541  break;
542  default:
543  av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
544  return AVERROR_INVALIDDATA;
545  }
546 
547  /* Detect compression method */
548  c->compression = (int8_t)avctx->extradata[5];
549  switch (avctx->codec_id) {
550  case AV_CODEC_ID_MSZH:
551  switch (c->compression) {
552  case COMP_MSZH:
553  av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
554  break;
555  case COMP_MSZH_NOCOMP:
556  c->decomp_size = 0;
557  av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
558  break;
559  default:
560  av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
561  return AVERROR_INVALIDDATA;
562  }
563  break;
564 #if CONFIG_ZLIB_DECODER
565  case AV_CODEC_ID_ZLIB:
566  switch (c->compression) {
567  case COMP_ZLIB_HISPEED:
568  av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
569  break;
570  case COMP_ZLIB_HICOMP:
571  av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
572  break;
573  case COMP_ZLIB_NORMAL:
574  av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
575  break;
576  default:
577  if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
578  av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
579  return AVERROR_INVALIDDATA;
580  }
581  av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
582  }
583  break;
584 #endif
585  default:
586  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
587  return AVERROR_INVALIDDATA;
588  }
589 
590  /* Allocate decompression buffer */
591  if (c->decomp_size) {
592  if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
593  av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
594  return AVERROR(ENOMEM);
595  }
596  }
597 
598  /* Detect flags */
599  c->flags = avctx->extradata[6];
600  if (c->flags & FLAG_MULTITHREAD)
601  av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
602  if (c->flags & FLAG_NULLFRAME)
603  av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
604  if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
605  av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
606  if (c->flags & FLAGMASK_UNUSED)
607  av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
608 
609  /* If needed init zlib */
610 #if CONFIG_ZLIB_DECODER
611  if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
612  int zret;
613  c->zstream.zalloc = Z_NULL;
614  c->zstream.zfree = Z_NULL;
615  c->zstream.opaque = Z_NULL;
616  zret = inflateInit(&c->zstream);
617  if (zret != Z_OK) {
618  av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
619  av_freep(&c->decomp_buf);
620  return AVERROR_UNKNOWN;
621  }
622  }
623 #endif
624 
625  return 0;
626 }
627 
628 /*
629  *
630  * Uninit lcl decoder
631  *
632  */
634 {
635  LclDecContext * const c = avctx->priv_data;
636 
637  av_freep(&c->decomp_buf);
638  if (c->pic.data[0])
639  avctx->release_buffer(avctx, &c->pic);
640 #if CONFIG_ZLIB_DECODER
641  if (avctx->codec_id == AV_CODEC_ID_ZLIB)
642  inflateEnd(&c->zstream);
643 #endif
644 
645  return 0;
646 }
647 
648 #if CONFIG_MSZH_DECODER
649 AVCodec ff_mszh_decoder = {
650  .name = "mszh",
651  .type = AVMEDIA_TYPE_VIDEO,
652  .id = AV_CODEC_ID_MSZH,
653  .priv_data_size = sizeof(LclDecContext),
654  .init = decode_init,
655  .close = decode_end,
656  .decode = decode_frame,
657  .capabilities = CODEC_CAP_DR1,
658  .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
659 };
660 #endif
661 
662 #if CONFIG_ZLIB_DECODER
663 AVCodec ff_zlib_decoder = {
664  .name = "zlib",
665  .type = AVMEDIA_TYPE_VIDEO,
666  .id = AV_CODEC_ID_ZLIB,
667  .priv_data_size = sizeof(LclDecContext),
668  .init = decode_init,
669  .close = decode_end,
670  .decode = decode_frame,
671  .capabilities = CODEC_CAP_DR1,
672  .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
673 };
674 #endif