FFmpeg  4.4.9
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 #include <stdio.h>
41 #include <stdlib.h>
42 
43 #include "libavutil/mem.h"
44 #include "libavutil/pixdesc.h"
45 #include "avcodec.h"
46 #include "bytestream.h"
47 #include "internal.h"
48 #include "lcl.h"
49 #include "thread.h"
50 
51 #if CONFIG_ZLIB_DECODER
52 #include <zlib.h>
53 #endif
54 
55 typedef struct LclDecContext {
56  // Image type
57  int imgtype;
58  // Compression type
60  // Flags
61  int flags;
62  // Decompressed data size
63  unsigned int decomp_size;
64  // Decompression buffer
65  unsigned char* decomp_buf;
66 #if CONFIG_ZLIB_DECODER
67  z_stream zstream;
68 #endif
70 
71 
72 /**
73  * @param srcptr compressed source buffer, must be padded with at least 5 extra bytes
74  * @param destptr must be padded sufficiently for av_memcpy_backptr
75  */
76 static unsigned int mszh_decomp(const unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
77 {
78  unsigned char *destptr_bak = destptr;
79  unsigned char *destptr_end = destptr + destsize;
80  const unsigned char *srcptr_end = srcptr + srclen;
81  unsigned mask = *srcptr++;
82  unsigned maskbit = 0x80;
83 
84  while (srcptr < srcptr_end && destptr < destptr_end) {
85  if (!(mask & maskbit)) {
86  memcpy(destptr, srcptr, 4);
87  destptr += 4;
88  srcptr += 4;
89  } else {
90  unsigned ofs = bytestream_get_le16(&srcptr);
91  unsigned cnt = (ofs >> 11) + 1;
92  ofs &= 0x7ff;
93  ofs = FFMIN(ofs, destptr - destptr_bak);
94  cnt *= 4;
95  cnt = FFMIN(cnt, destptr_end - destptr);
96  if (ofs) {
97  av_memcpy_backptr(destptr, ofs, cnt);
98  } else {
99  // Not known what the correct behaviour is, but
100  // this at least avoids uninitialized data.
101  memset(destptr, 0, cnt);
102  }
103  destptr += cnt;
104  }
105  maskbit >>= 1;
106  if (!maskbit) {
107  mask = *srcptr++;
108  while (!mask) {
109  if (destptr_end - destptr < 32 || srcptr_end - srcptr < 32) break;
110  memcpy(destptr, srcptr, 32);
111  destptr += 32;
112  srcptr += 32;
113  mask = *srcptr++;
114  }
115  maskbit = 0x80;
116  }
117  }
118 
119  if (destptr < destptr_end)
120  memset(destptr, 0, destptr_end - destptr);
121 
122  return destptr - destptr_bak;
123 }
124 
125 
126 #if CONFIG_ZLIB_DECODER
127 /**
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
133  */
134 static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected)
135 {
136  LclDecContext *c = avctx->priv_data;
137  int zret = inflateReset(&c->zstream);
138  if (zret != Z_OK) {
139  av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
140  return AVERROR_UNKNOWN;
141  }
142  c->zstream.next_in = 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;
150  }
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  if (expected > (unsigned int)c->zstream.total_out) {
155  memset(c->decomp_buf + offset + c->zstream.total_out, 0,
156  c->decomp_size - offset - c->zstream.total_out);
157  return (unsigned int)c->zstream.total_out;
158  }
159  return AVERROR_UNKNOWN;
160  }
161  return c->zstream.total_out;
162 }
163 #endif
164 
165 
166 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
167 {
168  AVFrame *frame = data;
169  ThreadFrame tframe = { .f = data };
170  const uint8_t *buf = avpkt->data;
171  int buf_size = avpkt->size;
172  LclDecContext * const c = avctx->priv_data;
173  ptrdiff_t pixel_ptr;
174  int row, col;
175  unsigned char *encoded = avpkt->data, *outptr;
176  uint8_t *y_out, *u_out, *v_out;
177  int width = avctx->width; // Real image width
178  int height = avctx->height; // Real image height
179  unsigned int mszh_dlen;
180  unsigned char yq, y1q, uq, vq;
181  int ret;
182  unsigned int mthread_inlen, mthread_outlen;
183  unsigned int len = buf_size;
184  int linesize, offset;
185 
186  if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
187  return ret;
188 
189  outptr = frame->data[0]; // Output image pointer
190 
191  /* Decompress frame */
192  switch (avctx->codec_id) {
193  case AV_CODEC_ID_MSZH:
194  switch (c->compression) {
195  case COMP_MSZH:
196  if (c->imgtype == IMGTYPE_RGB24 && len == FFALIGN(width * 3, 4) * height ||
197  c->imgtype == IMGTYPE_YUV111 && len == width * height * 3) {
198  ;
199  } else if (c->flags & FLAG_MULTITHREAD) {
200  mthread_inlen = AV_RL32(buf);
201  if (len < 8 || len - 8 < mthread_inlen) {
202  av_log(avctx, AV_LOG_ERROR, "len %d is too small\n", len);
203  return AVERROR_INVALIDDATA;
204  }
205  mthread_outlen = AV_RL32(buf + 4);
206  mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
207  mszh_dlen = mszh_decomp(buf + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
208  if (mthread_outlen != mszh_dlen) {
209  av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
210  mthread_outlen, mszh_dlen);
211  return AVERROR_INVALIDDATA;
212  }
213  mszh_dlen = mszh_decomp(buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
214  c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
215  if (mthread_outlen != mszh_dlen) {
216  av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
217  mthread_outlen, mszh_dlen);
218  return AVERROR_INVALIDDATA;
219  }
220  encoded = c->decomp_buf;
221  len = c->decomp_size;
222  } else {
223  mszh_dlen = mszh_decomp(buf, len, c->decomp_buf, c->decomp_size);
224  if (c->decomp_size != mszh_dlen) {
225  av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
226  c->decomp_size, mszh_dlen);
227  return AVERROR_INVALIDDATA;
228  }
229  encoded = c->decomp_buf;
230  len = mszh_dlen;
231  }
232  break;
233  case COMP_MSZH_NOCOMP: {
234  int bppx2;
235  int aligned_width = width;
236  switch (c->imgtype) {
237  case IMGTYPE_YUV111:
238  case IMGTYPE_RGB24:
239  bppx2 = 6;
240  break;
241  case IMGTYPE_YUV422:
242  aligned_width &= ~3;
243  case IMGTYPE_YUV211:
244  bppx2 = 4;
245  break;
246  case IMGTYPE_YUV411:
247  aligned_width &= ~3;
248  case IMGTYPE_YUV420:
249  bppx2 = 3;
250  break;
251  default:
252  bppx2 = 0; // will error out below
253  break;
254  }
255  if (len < ((aligned_width * height * bppx2) >> 1))
256  return AVERROR_INVALIDDATA;
257  break;
258  }
259  default:
260  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
261  return AVERROR_INVALIDDATA;
262  }
263  break;
264 #if CONFIG_ZLIB_DECODER
265  case AV_CODEC_ID_ZLIB:
266  /* Using the original dll with normal compression (-1) and RGB format
267  * gives a file with ZLIB fourcc, but frame is really uncompressed.
268  * To be sure that's true check also frame size */
269  if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
270  len == width * height * 3) {
271  if (c->flags & FLAG_PNGFILTER) {
272  memcpy(c->decomp_buf, buf, len);
273  encoded = c->decomp_buf;
274  } else {
275  break;
276  }
277  } else if (c->flags & FLAG_MULTITHREAD) {
278  if (len < 8) {
279  av_log(avctx, AV_LOG_ERROR, "len %d is too small\n", len);
280  return AVERROR_INVALIDDATA;
281  }
282  mthread_inlen = AV_RL32(buf);
283  mthread_inlen = FFMIN(mthread_inlen, len - 8);
284  mthread_outlen = AV_RL32(buf + 4);
285  mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
286  ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen);
287  if (ret < 0) return ret;
288  ret = zlib_decomp(avctx, buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
289  mthread_outlen, mthread_outlen);
290  if (ret < 0) return ret;
291  memset(c->decomp_buf + mthread_outlen + ret, 0,
292  c->decomp_size - mthread_outlen - ret);
293  len = c->decomp_size;
294  } else {
295  int ret = zlib_decomp(avctx, buf, len, 0, c->decomp_size);
296  if (ret < 0) return ret;
297  len = ret;
298  }
299  encoded = c->decomp_buf;
300  break;
301 #endif
302  default:
303  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
304  return AVERROR_INVALIDDATA;
305  }
306 
307 
308  /* Apply PNG filter */
309  if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
310  switch (c->imgtype) {
311  case IMGTYPE_YUV111:
312  case IMGTYPE_RGB24:
313  for (row = 0; row < height; row++) {
314  pixel_ptr = row * width * 3;
315  yq = encoded[pixel_ptr++];
316  unsigned uqvq = AV_RL16(encoded+pixel_ptr);
317  pixel_ptr += 2;
318  for (col = 1; col < width; col++) {
319  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
320  uqvq -= AV_RL16(encoded+pixel_ptr+1);
321  AV_WL16(encoded+pixel_ptr+1, uqvq);
322  pixel_ptr += 3;
323  }
324  }
325  break;
326  case IMGTYPE_YUV422:
327  pixel_ptr = 0;
328  for (row = 0; row < height; row++) {
329  yq = uq = vq =0;
330  for (col = 0; col < width/4; col++) {
331  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
332  encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
333  encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
334  encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
335  encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
336  encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
337  encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
338  encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
339  pixel_ptr += 8;
340  }
341  }
342  break;
343  case IMGTYPE_YUV411:
344  pixel_ptr = 0;
345  for (row = 0; row < height; row++) {
346  yq = uq = vq =0;
347  for (col = 0; col < width/4; col++) {
348  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
349  encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
350  encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
351  encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
352  encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
353  encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
354  pixel_ptr += 6;
355  }
356  }
357  break;
358  case IMGTYPE_YUV211:
359  for (row = 0; row < height; row++) {
360  pixel_ptr = row * width * 2;
361  yq = uq = vq =0;
362  for (col = 0; col < width/2; col++) {
363  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
364  encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
365  encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
366  encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
367  pixel_ptr += 4;
368  }
369  }
370  break;
371  case IMGTYPE_YUV420:
372  for (row = 0; row < height/2; row++) {
373  pixel_ptr = row * width * 3;
374  yq = y1q = uq = vq =0;
375  for (col = 0; col < width/2; col++) {
376  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
377  encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
378  encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
379  encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
380  encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
381  encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
382  pixel_ptr += 6;
383  }
384  }
385  break;
386  default:
387  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
388  return AVERROR_INVALIDDATA;
389  }
390  }
391 
392  /* Convert colorspace */
393  y_out = frame->data[0] + (height - 1) * frame->linesize[0];
394  offset = (height - 1) * frame->linesize[1];
395  u_out = FF_PTR_ADD(frame->data[1], offset);
396  offset = (height - 1) * frame->linesize[2];
397  v_out = FF_PTR_ADD(frame->data[2], offset);
398  switch (c->imgtype) {
399  case IMGTYPE_YUV111:
400  for (row = 0; row < height; row++) {
401  for (col = 0; col < width; col++) {
402  y_out[col] = *encoded++;
403  u_out[col] = *encoded++ + 128;
404  v_out[col] = *encoded++ + 128;
405  }
406  y_out -= frame->linesize[0];
407  u_out -= frame->linesize[1];
408  v_out -= frame->linesize[2];
409  }
410  break;
411  case IMGTYPE_YUV422:
412  for (row = 0; row < height; row++) {
413  for (col = 0; col < width - 3; col += 4) {
414  memcpy(y_out + col, encoded, 4);
415  encoded += 4;
416  u_out[ col >> 1 ] = *encoded++ + 128;
417  u_out[(col >> 1) + 1] = *encoded++ + 128;
418  v_out[ col >> 1 ] = *encoded++ + 128;
419  v_out[(col >> 1) + 1] = *encoded++ + 128;
420  }
421  if (col && col < width) {
422  u_out[ col >> 1 ] = u_out[(col>>1) - 1];
423  v_out[ col >> 1 ] = v_out[(col>>1) - 1];
424  }
425 
426  y_out -= frame->linesize[0];
427  u_out -= frame->linesize[1];
428  v_out -= frame->linesize[2];
429  }
430  break;
431  case IMGTYPE_RGB24:
432  linesize = len < FFALIGN(3 * width, 4) * height ? 3 * width : FFALIGN(3 * width, 4);
433  for (row = height - 1; row >= 0; row--) {
434  pixel_ptr = row * frame->linesize[0];
435  memcpy(outptr + pixel_ptr, encoded, 3 * width);
436  encoded += linesize;
437  }
438  break;
439  case IMGTYPE_YUV411:
440  for (row = 0; row < height; row++) {
441  for (col = 0; col < width - 3; col += 4) {
442  memcpy(y_out + col, encoded, 4);
443  encoded += 4;
444  u_out[col >> 2] = *encoded++ + 128;
445  v_out[col >> 2] = *encoded++ + 128;
446  }
447  if (col && col < width) {
448  u_out[col >> 2] = u_out[(col>>2) - 1];
449  v_out[col >> 2] = v_out[(col>>2) - 1];
450  }
451  y_out -= frame->linesize[0];
452  u_out -= frame->linesize[1];
453  v_out -= frame->linesize[2];
454  }
455  break;
456  case IMGTYPE_YUV211:
457  for (row = 0; row < height; row++) {
458  for (col = 0; col < width - 1; col += 2) {
459  memcpy(y_out + col, encoded, 2);
460  encoded += 2;
461  u_out[col >> 1] = *encoded++ + 128;
462  v_out[col >> 1] = *encoded++ + 128;
463  }
464  y_out -= frame->linesize[0];
465  u_out -= frame->linesize[1];
466  v_out -= frame->linesize[2];
467  }
468  break;
469  case IMGTYPE_YUV420:
470  u_out = frame->data[1] + ((height >> 1) - 1) * frame->linesize[1];
471  v_out = frame->data[2] + ((height >> 1) - 1) * frame->linesize[2];
472  for (row = 0; row < height - 1; row += 2) {
473  for (col = 0; col < width - 1; col += 2) {
474  memcpy(y_out + col, encoded, 2);
475  encoded += 2;
476  memcpy(y_out + col - frame->linesize[0], encoded, 2);
477  encoded += 2;
478  u_out[col >> 1] = *encoded++ + 128;
479  v_out[col >> 1] = *encoded++ + 128;
480  }
481  y_out -= frame->linesize[0] << 1;
482  u_out -= frame->linesize[1];
483  v_out -= frame->linesize[2];
484  }
485  break;
486  default:
487  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
488  return AVERROR_INVALIDDATA;
489  }
490 
491  frame->key_frame = 1;
493 
494  *got_frame = 1;
495 
496  /* always report that the buffer was completely consumed */
497  return buf_size;
498 }
499 
501 {
502  LclDecContext * const c = avctx->priv_data;
503  unsigned int basesize = avctx->width * avctx->height;
504  unsigned int max_basesize = FFALIGN(avctx->width, 4) *
505  FFALIGN(avctx->height, 4);
506  unsigned int max_decomp_size;
507  int subsample_h, subsample_v;
508  int partial_h_supported = 0;
509 
510  if (avctx->extradata_size < 8) {
511  av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
512  return AVERROR_INVALIDDATA;
513  }
514 
515  /* Check codec type */
516  if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
517  (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
518  av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
519  }
520 
521  /* Detect image type */
522  switch (c->imgtype = avctx->extradata[4]) {
523  case IMGTYPE_YUV111:
524  c->decomp_size = basesize * 3;
525  max_decomp_size = max_basesize * 3;
526  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
527  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
528  break;
529  case IMGTYPE_YUV422:
530  c->decomp_size = (avctx->width & ~3) * avctx->height * 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 4:2:2.\n");
534  partial_h_supported = 1;
535  break;
536  case IMGTYPE_RGB24:
537  c->decomp_size = FFALIGN(avctx->width*3, 4) * avctx->height;
538  max_decomp_size = max_basesize * 3;
539  avctx->pix_fmt = AV_PIX_FMT_BGR24;
540  av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
541  break;
542  case IMGTYPE_YUV411:
543  c->decomp_size = (avctx->width & ~3) * avctx->height / 2 * 3;
544  max_decomp_size = max_basesize / 2 * 3;
545  avctx->pix_fmt = AV_PIX_FMT_YUV411P;
546  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
547  partial_h_supported = 1;
548  break;
549  case IMGTYPE_YUV211:
550  c->decomp_size = basesize * 2;
551  max_decomp_size = max_basesize * 2;
552  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
553  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
554  break;
555  case IMGTYPE_YUV420:
556  c->decomp_size = basesize / 2 * 3;
557  max_decomp_size = max_basesize / 2 * 3;
558  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
559  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
560  break;
561  default:
562  av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
563  return AVERROR_INVALIDDATA;
564  }
565 
566  av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &subsample_h, &subsample_v);
567  if ((avctx->width % (1<<subsample_h) && !partial_h_supported) || avctx->height % (1<<subsample_v)) {
568  avpriv_request_sample(avctx, "Unsupported dimensions");
569  return AVERROR_INVALIDDATA;
570  }
571 
572  /* Detect compression method */
573  c->compression = (int8_t)avctx->extradata[5];
574  switch (avctx->codec_id) {
575  case AV_CODEC_ID_MSZH:
576  switch (c->compression) {
577  case COMP_MSZH:
578  av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
579  break;
580  case COMP_MSZH_NOCOMP:
581  c->decomp_size = 0;
582  av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
583  break;
584  default:
585  av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
586  return AVERROR_INVALIDDATA;
587  }
588  break;
589 #if CONFIG_ZLIB_DECODER
590  case AV_CODEC_ID_ZLIB:
591  switch (c->compression) {
592  case COMP_ZLIB_HISPEED:
593  av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
594  break;
595  case COMP_ZLIB_HICOMP:
596  av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
597  break;
598  case COMP_ZLIB_NORMAL:
599  av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
600  break;
601  default:
602  if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
603  av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
604  return AVERROR_INVALIDDATA;
605  }
606  av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
607  }
608  break;
609 #endif
610  default:
611  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
612  return AVERROR_INVALIDDATA;
613  }
614 
615  /* Allocate decompression buffer */
616  if (c->decomp_size) {
617  if (!(c->decomp_buf = av_malloc(max_decomp_size))) {
618  av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
619  return AVERROR(ENOMEM);
620  }
621  }
622 
623  /* Detect flags */
624  c->flags = avctx->extradata[6];
625  if (c->flags & FLAG_MULTITHREAD)
626  av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
627  if (c->flags & FLAG_NULLFRAME)
628  av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
629  if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
630  av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
631  if (c->flags & FLAGMASK_UNUSED)
632  av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
633 
634  /* If needed init zlib */
635 #if CONFIG_ZLIB_DECODER
636  if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
637  int zret;
638  c->zstream.zalloc = Z_NULL;
639  c->zstream.zfree = Z_NULL;
640  c->zstream.opaque = Z_NULL;
641  zret = inflateInit(&c->zstream);
642  if (zret != Z_OK) {
643  av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
644  av_freep(&c->decomp_buf);
645  return AVERROR_UNKNOWN;
646  }
647  }
648 #endif
649 
650  return 0;
651 }
652 
654 {
655  LclDecContext * const c = avctx->priv_data;
656 
657  av_freep(&c->decomp_buf);
658 #if CONFIG_ZLIB_DECODER
659  if (avctx->codec_id == AV_CODEC_ID_ZLIB)
660  inflateEnd(&c->zstream);
661 #endif
662 
663  return 0;
664 }
665 
666 #if CONFIG_MSZH_DECODER
668  .name = "mszh",
669  .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
670  .type = AVMEDIA_TYPE_VIDEO,
671  .id = AV_CODEC_ID_MSZH,
672  .priv_data_size = sizeof(LclDecContext),
673  .init = decode_init,
674  .close = decode_end,
675  .decode = decode_frame,
677  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
678 };
679 #endif
680 
681 #if CONFIG_ZLIB_DECODER
683  .name = "zlib",
684  .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
685  .type = AVMEDIA_TYPE_VIDEO,
686  .id = AV_CODEC_ID_ZLIB,
687  .priv_data_size = sizeof(LclDecContext),
688  .init = decode_init,
689  .close = decode_end,
690  .decode = decode_frame,
692  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
693 };
694 #endif
AVCodec ff_zlib_decoder
AVCodec ff_mszh_decoder
#define av_cold
Definition: attributes.h:88
uint8_t
Libavcodec external API header.
#define AV_RL16
Definition: intreadwrite.h:42
#define AV_RL32
Definition: intreadwrite.h:146
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define FFMIN(a, b)
Definition: common.h:105
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static AVFrame * frame
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:108
@ AV_CODEC_ID_ZLIB
Definition: codec_id.h:103
@ AV_CODEC_ID_MSZH
Definition: codec_id.h:102
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
Definition: mem.c:428
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
#define FLAG_NULLFRAME
Definition: lcl.h:42
#define CODEC_ZLIB
Definition: lcl.h:47
#define COMP_ZLIB_HICOMP
Definition: lcl.h:38
#define IMGTYPE_YUV420
Definition: lcl.h:33
#define FLAG_PNGFILTER
Definition: lcl.h:43
#define COMP_ZLIB_NORMAL
Definition: lcl.h:39
#define FLAG_MULTITHREAD
Definition: lcl.h:41
#define COMP_MSZH_NOCOMP
Definition: lcl.h:36
#define IMGTYPE_YUV422
Definition: lcl.h:29
#define FLAGMASK_UNUSED
Definition: lcl.h:44
#define IMGTYPE_RGB24
Definition: lcl.h:30
#define COMP_ZLIB_HISPEED
Definition: lcl.h:37
#define IMGTYPE_YUV411
Definition: lcl.h:31
#define CODEC_MSZH
Definition: lcl.h:46
#define IMGTYPE_YUV111
Definition: lcl.h:28
#define IMGTYPE_YUV211
Definition: lcl.h:32
#define COMP_MSZH
Definition: lcl.h:35
static unsigned int mszh_decomp(const unsigned char *srcptr, int srclen, unsigned char *destptr, unsigned int destsize)
Definition: lcldec.c:76
static av_cold int decode_init(AVCodecContext *avctx)
Definition: lcldec.c:500
static av_cold int decode_end(AVCodecContext *avctx)
Definition: lcldec.c:653
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: lcldec.c:166
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define FF_PTR_ADD(ptr, off)
Definition: internal.h:105
static const uint16_t mask[17]
Definition: lzw.c:38
#define FFALIGN(x, a)
Definition: macros.h:48
Memory handling functions.
const char data[16]
Definition: mxf.c:142
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2601
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:73
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
FF_ENABLE_DEPRECATION_WARNINGS int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:637
enum AVCodecID codec_id
Definition: avcodec.h:546
int extradata_size
Definition: avcodec.h:638
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:396
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
int flags
Definition: lcldec.c:61
unsigned int decomp_size
Definition: lcldec.c:63
unsigned char * decomp_buf
Definition: lcldec.c:65
int imgtype
Definition: lcldec.c:57
int compression
Definition: lcldec.c:59
AVFrame * f
Definition: thread.h:35
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
#define src
Definition: vp8dsp.c:255
#define height
#define width
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
Definition: vf_neighbor.c:199
static const uint8_t offset[127][2]
Definition: vf_spp.c:107
int len
static double c[64]