FFmpeg  4.4.5
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  return destptr - destptr_bak;
120 }
121 
122 
123 #if CONFIG_ZLIB_DECODER
124 /**
125  * @brief decompress a zlib-compressed data block into decomp_buf
126  * @param src compressed input buffer
127  * @param src_len data length in input buffer
128  * @param offset offset in decomp_buf
129  * @param expected expected decompressed length
130  */
131 static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected)
132 {
133  LclDecContext *c = avctx->priv_data;
134  int zret = inflateReset(&c->zstream);
135  if (zret != Z_OK) {
136  av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
137  return AVERROR_UNKNOWN;
138  }
139  c->zstream.next_in = src;
140  c->zstream.avail_in = src_len;
141  c->zstream.next_out = c->decomp_buf + offset;
142  c->zstream.avail_out = c->decomp_size - offset;
143  zret = inflate(&c->zstream, Z_FINISH);
144  if (zret != Z_OK && zret != Z_STREAM_END) {
145  av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
146  return AVERROR_UNKNOWN;
147  }
148  if (expected != (unsigned int)c->zstream.total_out) {
149  av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
150  expected, c->zstream.total_out);
151  if (expected > (unsigned int)c->zstream.total_out)
152  return (unsigned int)c->zstream.total_out;
153  return AVERROR_UNKNOWN;
154  }
155  return c->zstream.total_out;
156 }
157 #endif
158 
159 
160 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
161 {
162  AVFrame *frame = data;
163  ThreadFrame tframe = { .f = data };
164  const uint8_t *buf = avpkt->data;
165  int buf_size = avpkt->size;
166  LclDecContext * const c = avctx->priv_data;
167  unsigned int pixel_ptr;
168  int row, col;
169  unsigned char *encoded = avpkt->data, *outptr;
170  uint8_t *y_out, *u_out, *v_out;
171  int width = avctx->width; // Real image width
172  int height = avctx->height; // Real image height
173  unsigned int mszh_dlen;
174  unsigned char yq, y1q, uq, vq;
175  int uqvq, ret;
176  unsigned int mthread_inlen, mthread_outlen;
177  unsigned int len = buf_size;
178  int linesize, offset;
179 
180  if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
181  return ret;
182 
183  outptr = frame->data[0]; // Output image pointer
184 
185  /* Decompress frame */
186  switch (avctx->codec_id) {
187  case AV_CODEC_ID_MSZH:
188  switch (c->compression) {
189  case COMP_MSZH:
190  if (c->imgtype == IMGTYPE_RGB24 && len == FFALIGN(width * 3, 4) * height ||
191  c->imgtype == IMGTYPE_YUV111 && len == width * height * 3) {
192  ;
193  } else if (c->flags & FLAG_MULTITHREAD) {
194  mthread_inlen = AV_RL32(buf);
195  if (len < 8 || len - 8 < mthread_inlen) {
196  av_log(avctx, AV_LOG_ERROR, "len %d is too small\n", len);
197  return AVERROR_INVALIDDATA;
198  }
199  mthread_outlen = AV_RL32(buf + 4);
200  mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
201  mszh_dlen = mszh_decomp(buf + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
202  if (mthread_outlen != mszh_dlen) {
203  av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
204  mthread_outlen, mszh_dlen);
205  return AVERROR_INVALIDDATA;
206  }
207  mszh_dlen = mszh_decomp(buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
208  c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
209  if (mthread_outlen != mszh_dlen) {
210  av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
211  mthread_outlen, mszh_dlen);
212  return AVERROR_INVALIDDATA;
213  }
214  encoded = c->decomp_buf;
215  len = c->decomp_size;
216  } else {
217  mszh_dlen = mszh_decomp(buf, len, c->decomp_buf, c->decomp_size);
218  if (c->decomp_size != mszh_dlen) {
219  av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
220  c->decomp_size, mszh_dlen);
221  return AVERROR_INVALIDDATA;
222  }
223  encoded = c->decomp_buf;
224  len = mszh_dlen;
225  }
226  break;
227  case COMP_MSZH_NOCOMP: {
228  int bppx2;
229  int aligned_width = width;
230  switch (c->imgtype) {
231  case IMGTYPE_YUV111:
232  case IMGTYPE_RGB24:
233  bppx2 = 6;
234  break;
235  case IMGTYPE_YUV422:
236  aligned_width &= ~3;
237  case IMGTYPE_YUV211:
238  bppx2 = 4;
239  break;
240  case IMGTYPE_YUV411:
241  aligned_width &= ~3;
242  case IMGTYPE_YUV420:
243  bppx2 = 3;
244  break;
245  default:
246  bppx2 = 0; // will error out below
247  break;
248  }
249  if (len < ((aligned_width * height * bppx2) >> 1))
250  return AVERROR_INVALIDDATA;
251  break;
252  }
253  default:
254  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
255  return AVERROR_INVALIDDATA;
256  }
257  break;
258 #if CONFIG_ZLIB_DECODER
259  case AV_CODEC_ID_ZLIB:
260  /* Using the original dll with normal compression (-1) and RGB format
261  * gives a file with ZLIB fourcc, but frame is really uncompressed.
262  * To be sure that's true check also frame size */
263  if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
264  len == width * height * 3) {
265  if (c->flags & FLAG_PNGFILTER) {
266  memcpy(c->decomp_buf, buf, len);
267  encoded = c->decomp_buf;
268  } else {
269  break;
270  }
271  } else if (c->flags & FLAG_MULTITHREAD) {
272  mthread_inlen = AV_RL32(buf);
273  mthread_inlen = FFMIN(mthread_inlen, len - 8);
274  mthread_outlen = AV_RL32(buf + 4);
275  mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
276  ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen);
277  if (ret < 0) return ret;
278  ret = zlib_decomp(avctx, buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
279  mthread_outlen, mthread_outlen);
280  if (ret < 0) return ret;
281  len = c->decomp_size;
282  } else {
283  int ret = zlib_decomp(avctx, buf, len, 0, c->decomp_size);
284  if (ret < 0) return ret;
285  len = ret;
286  }
287  encoded = c->decomp_buf;
288  break;
289 #endif
290  default:
291  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
292  return AVERROR_INVALIDDATA;
293  }
294 
295 
296  /* Apply PNG filter */
297  if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
298  switch (c->imgtype) {
299  case IMGTYPE_YUV111:
300  case IMGTYPE_RGB24:
301  for (row = 0; row < height; row++) {
302  pixel_ptr = row * width * 3;
303  yq = encoded[pixel_ptr++];
304  uqvq = AV_RL16(encoded+pixel_ptr);
305  pixel_ptr += 2;
306  for (col = 1; col < width; col++) {
307  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
308  uqvq -= AV_RL16(encoded+pixel_ptr+1);
309  AV_WL16(encoded+pixel_ptr+1, uqvq);
310  pixel_ptr += 3;
311  }
312  }
313  break;
314  case IMGTYPE_YUV422:
315  pixel_ptr = 0;
316  for (row = 0; row < height; row++) {
317  yq = uq = vq =0;
318  for (col = 0; col < width/4; col++) {
319  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
320  encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
321  encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
322  encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
323  encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
324  encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
325  encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
326  encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
327  pixel_ptr += 8;
328  }
329  }
330  break;
331  case IMGTYPE_YUV411:
332  pixel_ptr = 0;
333  for (row = 0; row < height; row++) {
334  yq = uq = vq =0;
335  for (col = 0; col < width/4; col++) {
336  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
337  encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
338  encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
339  encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
340  encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
341  encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
342  pixel_ptr += 6;
343  }
344  }
345  break;
346  case IMGTYPE_YUV211:
347  for (row = 0; row < height; row++) {
348  pixel_ptr = row * width * 2;
349  yq = uq = vq =0;
350  for (col = 0; col < width/2; col++) {
351  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
352  encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
353  encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
354  encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
355  pixel_ptr += 4;
356  }
357  }
358  break;
359  case IMGTYPE_YUV420:
360  for (row = 0; row < height/2; row++) {
361  pixel_ptr = row * width * 3;
362  yq = y1q = uq = vq =0;
363  for (col = 0; col < width/2; col++) {
364  encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
365  encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
366  encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
367  encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
368  encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
369  encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
370  pixel_ptr += 6;
371  }
372  }
373  break;
374  default:
375  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
376  return AVERROR_INVALIDDATA;
377  }
378  }
379 
380  /* Convert colorspace */
381  y_out = frame->data[0] + (height - 1) * frame->linesize[0];
382  offset = (height - 1) * frame->linesize[1];
383  u_out = FF_PTR_ADD(frame->data[1], offset);
384  offset = (height - 1) * frame->linesize[2];
385  v_out = FF_PTR_ADD(frame->data[2], offset);
386  switch (c->imgtype) {
387  case IMGTYPE_YUV111:
388  for (row = 0; row < height; row++) {
389  for (col = 0; col < width; col++) {
390  y_out[col] = *encoded++;
391  u_out[col] = *encoded++ + 128;
392  v_out[col] = *encoded++ + 128;
393  }
394  y_out -= frame->linesize[0];
395  u_out -= frame->linesize[1];
396  v_out -= frame->linesize[2];
397  }
398  break;
399  case IMGTYPE_YUV422:
400  for (row = 0; row < height; row++) {
401  for (col = 0; col < width - 3; col += 4) {
402  memcpy(y_out + col, encoded, 4);
403  encoded += 4;
404  u_out[ col >> 1 ] = *encoded++ + 128;
405  u_out[(col >> 1) + 1] = *encoded++ + 128;
406  v_out[ col >> 1 ] = *encoded++ + 128;
407  v_out[(col >> 1) + 1] = *encoded++ + 128;
408  }
409  if (col && col < width) {
410  u_out[ col >> 1 ] = u_out[(col>>1) - 1];
411  v_out[ col >> 1 ] = v_out[(col>>1) - 1];
412  }
413 
414  y_out -= frame->linesize[0];
415  u_out -= frame->linesize[1];
416  v_out -= frame->linesize[2];
417  }
418  break;
419  case IMGTYPE_RGB24:
420  linesize = len < FFALIGN(3 * width, 4) * height ? 3 * width : FFALIGN(3 * width, 4);
421  for (row = height - 1; row >= 0; row--) {
422  pixel_ptr = row * frame->linesize[0];
423  memcpy(outptr + pixel_ptr, encoded, 3 * width);
424  encoded += linesize;
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  if (col && col < width) {
436  u_out[col >> 2] = u_out[(col>>2) - 1];
437  v_out[col >> 2] = v_out[(col>>2) - 1];
438  }
439  y_out -= frame->linesize[0];
440  u_out -= frame->linesize[1];
441  v_out -= frame->linesize[2];
442  }
443  break;
444  case IMGTYPE_YUV211:
445  for (row = 0; row < height; row++) {
446  for (col = 0; col < width - 1; col += 2) {
447  memcpy(y_out + col, encoded, 2);
448  encoded += 2;
449  u_out[col >> 1] = *encoded++ + 128;
450  v_out[col >> 1] = *encoded++ + 128;
451  }
452  y_out -= frame->linesize[0];
453  u_out -= frame->linesize[1];
454  v_out -= frame->linesize[2];
455  }
456  break;
457  case IMGTYPE_YUV420:
458  u_out = frame->data[1] + ((height >> 1) - 1) * frame->linesize[1];
459  v_out = frame->data[2] + ((height >> 1) - 1) * frame->linesize[2];
460  for (row = 0; row < height - 1; row += 2) {
461  for (col = 0; col < width - 1; col += 2) {
462  memcpy(y_out + col, encoded, 2);
463  encoded += 2;
464  memcpy(y_out + col - frame->linesize[0], encoded, 2);
465  encoded += 2;
466  u_out[col >> 1] = *encoded++ + 128;
467  v_out[col >> 1] = *encoded++ + 128;
468  }
469  y_out -= frame->linesize[0] << 1;
470  u_out -= frame->linesize[1];
471  v_out -= frame->linesize[2];
472  }
473  break;
474  default:
475  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
476  return AVERROR_INVALIDDATA;
477  }
478 
479  frame->key_frame = 1;
481 
482  *got_frame = 1;
483 
484  /* always report that the buffer was completely consumed */
485  return buf_size;
486 }
487 
489 {
490  LclDecContext * const c = avctx->priv_data;
491  unsigned int basesize = avctx->width * avctx->height;
492  unsigned int max_basesize = FFALIGN(avctx->width, 4) *
493  FFALIGN(avctx->height, 4);
494  unsigned int max_decomp_size;
495  int subsample_h, subsample_v;
496  int partial_h_supported = 0;
497 
498  if (avctx->extradata_size < 8) {
499  av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
500  return AVERROR_INVALIDDATA;
501  }
502 
503  /* Check codec type */
504  if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
505  (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
506  av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
507  }
508 
509  /* Detect image type */
510  switch (c->imgtype = avctx->extradata[4]) {
511  case IMGTYPE_YUV111:
512  c->decomp_size = basesize * 3;
513  max_decomp_size = max_basesize * 3;
514  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
515  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
516  break;
517  case IMGTYPE_YUV422:
518  c->decomp_size = (avctx->width & ~3) * avctx->height * 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 4:2:2.\n");
522  partial_h_supported = 1;
523  break;
524  case IMGTYPE_RGB24:
525  c->decomp_size = FFALIGN(avctx->width*3, 4) * avctx->height;
526  max_decomp_size = max_basesize * 3;
527  avctx->pix_fmt = AV_PIX_FMT_BGR24;
528  av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
529  break;
530  case IMGTYPE_YUV411:
531  c->decomp_size = (avctx->width & ~3) * avctx->height / 2 * 3;
532  max_decomp_size = max_basesize / 2 * 3;
533  avctx->pix_fmt = AV_PIX_FMT_YUV411P;
534  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
535  partial_h_supported = 1;
536  break;
537  case IMGTYPE_YUV211:
538  c->decomp_size = basesize * 2;
539  max_decomp_size = max_basesize * 2;
540  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
541  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
542  break;
543  case IMGTYPE_YUV420:
544  c->decomp_size = basesize / 2 * 3;
545  max_decomp_size = max_basesize / 2 * 3;
546  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
547  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
548  break;
549  default:
550  av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
551  return AVERROR_INVALIDDATA;
552  }
553 
554  av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &subsample_h, &subsample_v);
555  if ((avctx->width % (1<<subsample_h) && !partial_h_supported) || avctx->height % (1<<subsample_v)) {
556  avpriv_request_sample(avctx, "Unsupported dimensions");
557  return AVERROR_INVALIDDATA;
558  }
559 
560  /* Detect compression method */
561  c->compression = (int8_t)avctx->extradata[5];
562  switch (avctx->codec_id) {
563  case AV_CODEC_ID_MSZH:
564  switch (c->compression) {
565  case COMP_MSZH:
566  av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
567  break;
568  case COMP_MSZH_NOCOMP:
569  c->decomp_size = 0;
570  av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
571  break;
572  default:
573  av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
574  return AVERROR_INVALIDDATA;
575  }
576  break;
577 #if CONFIG_ZLIB_DECODER
578  case AV_CODEC_ID_ZLIB:
579  switch (c->compression) {
580  case COMP_ZLIB_HISPEED:
581  av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
582  break;
583  case COMP_ZLIB_HICOMP:
584  av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
585  break;
586  case COMP_ZLIB_NORMAL:
587  av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
588  break;
589  default:
590  if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
591  av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
592  return AVERROR_INVALIDDATA;
593  }
594  av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
595  }
596  break;
597 #endif
598  default:
599  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
600  return AVERROR_INVALIDDATA;
601  }
602 
603  /* Allocate decompression buffer */
604  if (c->decomp_size) {
605  if (!(c->decomp_buf = av_malloc(max_decomp_size))) {
606  av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
607  return AVERROR(ENOMEM);
608  }
609  }
610 
611  /* Detect flags */
612  c->flags = avctx->extradata[6];
613  if (c->flags & FLAG_MULTITHREAD)
614  av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
615  if (c->flags & FLAG_NULLFRAME)
616  av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
617  if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
618  av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
619  if (c->flags & FLAGMASK_UNUSED)
620  av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
621 
622  /* If needed init zlib */
623 #if CONFIG_ZLIB_DECODER
624  if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
625  int zret;
626  c->zstream.zalloc = Z_NULL;
627  c->zstream.zfree = Z_NULL;
628  c->zstream.opaque = Z_NULL;
629  zret = inflateInit(&c->zstream);
630  if (zret != Z_OK) {
631  av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
632  av_freep(&c->decomp_buf);
633  return AVERROR_UNKNOWN;
634  }
635  }
636 #endif
637 
638  return 0;
639 }
640 
642 {
643  LclDecContext * const c = avctx->priv_data;
644 
645  av_freep(&c->decomp_buf);
646 #if CONFIG_ZLIB_DECODER
647  if (avctx->codec_id == AV_CODEC_ID_ZLIB)
648  inflateEnd(&c->zstream);
649 #endif
650 
651  return 0;
652 }
653 
654 #if CONFIG_MSZH_DECODER
656  .name = "mszh",
657  .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
658  .type = AVMEDIA_TYPE_VIDEO,
659  .id = AV_CODEC_ID_MSZH,
660  .priv_data_size = sizeof(LclDecContext),
661  .init = decode_init,
662  .close = decode_end,
663  .decode = decode_frame,
665  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
666 };
667 #endif
668 
669 #if CONFIG_ZLIB_DECODER
671  .name = "zlib",
672  .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
673  .type = AVMEDIA_TYPE_VIDEO,
674  .id = AV_CODEC_ID_ZLIB,
675  .priv_data_size = sizeof(LclDecContext),
676  .init = decode_init,
677  .close = decode_end,
678  .decode = decode_frame,
680  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
681 };
682 #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:488
static av_cold int decode_end(AVCodecContext *avctx)
Definition: lcldec.c:641
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: lcldec.c:160
#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:198
static const uint8_t offset[127][2]
Definition: vf_spp.c:107
int len
static double c[64]