FFmpeg
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 "config_components.h"
41 
42 #include <stdio.h>
43 #include <stdlib.h>
44 
45 #include "libavutil/mem.h"
46 #include "libavutil/pixdesc.h"
47 #include "avcodec.h"
48 #include "bytestream.h"
49 #include "codec_internal.h"
50 #include "lcl.h"
51 #include "thread.h"
52 
53 #if CONFIG_ZLIB_DECODER
54 #include "zlib_wrapper.h"
55 #include <zlib.h>
56 #endif
57 
58 typedef struct LclDecContext {
59  // Image type
60  int imgtype;
61  // Compression type
63  // Flags
64  int flags;
65  // Decompressed data size
66  unsigned int decomp_size;
67  // Decompression buffer
68  unsigned char* decomp_buf;
69 #if CONFIG_ZLIB_DECODER
70  FFZStream zstream;
71 #endif
73 
74 
75 /**
76  * @param srcptr compressed source buffer, must be padded with at least 5 extra bytes
77  * @param destptr must be padded sufficiently for av_memcpy_backptr
78  */
79 static unsigned int mszh_decomp(const unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
80 {
81  unsigned char *destptr_bak = destptr;
82  unsigned char *destptr_end = destptr + destsize;
83  const unsigned char *srcptr_end = srcptr + srclen;
84  unsigned mask = *srcptr++;
85  unsigned maskbit = 0x80;
86 
87  while (srcptr < srcptr_end && destptr < destptr_end) {
88  if (!(mask & maskbit)) {
89  memcpy(destptr, srcptr, 4);
90  destptr += 4;
91  srcptr += 4;
92  } else {
93  unsigned ofs = bytestream_get_le16(&srcptr);
94  unsigned cnt = (ofs >> 11) + 1;
95  ofs &= 0x7ff;
96  ofs = FFMIN(ofs, destptr - destptr_bak);
97  cnt *= 4;
98  cnt = FFMIN(cnt, destptr_end - destptr);
99  if (ofs) {
100  av_memcpy_backptr(destptr, ofs, cnt);
101  } else {
102  // Not known what the correct behaviour is, but
103  // this at least avoids uninitialized data.
104  memset(destptr, 0, cnt);
105  }
106  destptr += cnt;
107  }
108  maskbit >>= 1;
109  if (!maskbit) {
110  mask = *srcptr++;
111  while (!mask) {
112  if (destptr_end - destptr < 32 || srcptr_end - srcptr < 32) break;
113  memcpy(destptr, srcptr, 32);
114  destptr += 32;
115  srcptr += 32;
116  mask = *srcptr++;
117  }
118  maskbit = 0x80;
119  }
120  }
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  z_stream *const zstream = &c->zstream.zstream;
138  int zret = inflateReset(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  zstream->next_in = src;
144  zstream->avail_in = src_len;
145  zstream->next_out = c->decomp_buf + offset;
146  zstream->avail_out = c->decomp_size - offset;
147  zret = inflate(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)zstream->total_out) {
153  av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
154  expected, zstream->total_out);
155  if (expected > (unsigned int)zstream->total_out)
156  return (unsigned int)zstream->total_out;
157  return AVERROR_UNKNOWN;
158  }
159  return zstream->total_out;
160 }
161 #endif
162 
163 
165  int *got_frame, AVPacket *avpkt)
166 {
167  const uint8_t *buf = avpkt->data;
168  int buf_size = avpkt->size;
169  LclDecContext * const c = avctx->priv_data;
170  ptrdiff_t pixel_ptr;
171  int row, col;
172  unsigned char *encoded = avpkt->data, *outptr;
173  uint8_t *y_out, *u_out, *v_out;
174  int width = avctx->width; // Real image width
175  int height = avctx->height; // Real image height
176  unsigned int mszh_dlen;
177  unsigned char yq, y1q, uq, vq;
178  int uqvq, ret;
179  unsigned int mthread_inlen, mthread_outlen;
180  unsigned int len = buf_size;
181  int linesize, offset;
182 
183  if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
184  return ret;
185 
186  outptr = frame->data[0]; // Output image pointer
187 
188  /* Decompress frame */
189  switch (avctx->codec_id) {
190  case AV_CODEC_ID_MSZH:
191  switch (c->compression) {
192  case COMP_MSZH:
193  if (c->imgtype == IMGTYPE_RGB24 && len == FFALIGN(width * 3, 4) * height ||
194  c->imgtype == IMGTYPE_YUV111 && len == width * height * 3) {
195  ;
196  } else if (c->flags & FLAG_MULTITHREAD) {
197  mthread_inlen = AV_RL32(buf);
198  if (len < 8 || len - 8 < mthread_inlen) {
199  av_log(avctx, AV_LOG_ERROR, "len %d is too small\n", len);
200  return AVERROR_INVALIDDATA;
201  }
202  mthread_outlen = AV_RL32(buf + 4);
203  mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
204  mszh_dlen = mszh_decomp(buf + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
205  if (mthread_outlen != mszh_dlen) {
206  av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
207  mthread_outlen, mszh_dlen);
208  return AVERROR_INVALIDDATA;
209  }
210  mszh_dlen = mszh_decomp(buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
211  c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
212  if (mthread_outlen != mszh_dlen) {
213  av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
214  mthread_outlen, mszh_dlen);
215  return AVERROR_INVALIDDATA;
216  }
217  encoded = c->decomp_buf;
218  len = c->decomp_size;
219  } else {
220  mszh_dlen = mszh_decomp(buf, len, c->decomp_buf, c->decomp_size);
221  if (c->decomp_size != mszh_dlen) {
222  av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
223  c->decomp_size, mszh_dlen);
224  if (c->decomp_size != mszh_dlen &&
225  c->decomp_size != mszh_dlen + 2) // YUV420 306x306 is missing 2 bytes
226  return AVERROR_INVALIDDATA;
227  }
228  encoded = c->decomp_buf;
229  len = mszh_dlen;
230  }
231  break;
232  case COMP_MSZH_NOCOMP: {
233  int bppx2;
234  int aligned_width = width;
235  switch (c->imgtype) {
236  case IMGTYPE_YUV111:
237  case IMGTYPE_RGB24:
238  bppx2 = 6;
239  break;
240  case IMGTYPE_YUV422:
241  aligned_width &= ~3;
242  case IMGTYPE_YUV211:
243  bppx2 = 4;
244  break;
245  case IMGTYPE_YUV411:
246  aligned_width &= ~3;
247  case IMGTYPE_YUV420:
248  bppx2 = 3;
249  break;
250  default:
251  bppx2 = 0; // will error out below
252  break;
253  }
254  if (len < ((aligned_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 AVERROR_INVALIDDATA;
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, buf, len);
272  encoded = c->decomp_buf;
273  } else {
274  break;
275  }
276  } else if (c->flags & FLAG_MULTITHREAD) {
277  mthread_inlen = AV_RL32(buf);
278  mthread_inlen = FFMIN(mthread_inlen, len - 8);
279  mthread_outlen = AV_RL32(buf + 4);
280  mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
281  ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen);
282  if (ret < 0) return ret;
283  ret = zlib_decomp(avctx, buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
284  mthread_outlen, mthread_outlen);
285  if (ret < 0) return ret;
286  len = c->decomp_size;
287  } else {
288  int ret = zlib_decomp(avctx, buf, len, 0, c->decomp_size);
289  if (ret < 0) return ret;
290  len = ret;
291  }
292  encoded = c->decomp_buf;
293  break;
294 #endif
295  default:
296  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
297  return AVERROR_INVALIDDATA;
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  pixel_ptr = 0;
321  for (row = 0; row < height; row++) {
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  pixel_ptr = 0;
338  for (row = 0; row < height; row++) {
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 AVERROR_INVALIDDATA;
382  }
383  }
384 
385  /* Convert colorspace */
386  y_out = frame->data[0] + (height - 1) * frame->linesize[0];
387  offset = (height - 1) * frame->linesize[1];
388  u_out = FF_PTR_ADD(frame->data[1], offset);
389  offset = (height - 1) * frame->linesize[2];
390  v_out = FF_PTR_ADD(frame->data[2], offset);
391  switch (c->imgtype) {
392  case IMGTYPE_YUV111:
393  for (row = 0; row < height; row++) {
394  for (col = 0; col < width; col++) {
395  y_out[col] = *encoded++;
396  u_out[col] = *encoded++ + 128;
397  v_out[col] = *encoded++ + 128;
398  }
399  y_out -= frame->linesize[0];
400  u_out -= frame->linesize[1];
401  v_out -= frame->linesize[2];
402  }
403  break;
404  case IMGTYPE_YUV422:
405  for (row = 0; row < height; row++) {
406  for (col = 0; col < width - 3; col += 4) {
407  memcpy(y_out + col, encoded, 4);
408  encoded += 4;
409  u_out[ col >> 1 ] = *encoded++ + 128;
410  u_out[(col >> 1) + 1] = *encoded++ + 128;
411  v_out[ col >> 1 ] = *encoded++ + 128;
412  v_out[(col >> 1) + 1] = *encoded++ + 128;
413  }
414  if (col && col < width) {
415  u_out[ col >> 1 ] = u_out[(col>>1) - 1];
416  v_out[ col >> 1 ] = v_out[(col>>1) - 1];
417  }
418 
419  y_out -= frame->linesize[0];
420  u_out -= frame->linesize[1];
421  v_out -= frame->linesize[2];
422  }
423  break;
424  case IMGTYPE_RGB24:
425  linesize = len < FFALIGN(3 * width, 4) * height ? 3 * width : FFALIGN(3 * width, 4);
426  for (row = height - 1; row >= 0; row--) {
427  pixel_ptr = row * frame->linesize[0];
428  memcpy(outptr + pixel_ptr, encoded, 3 * width);
429  encoded += linesize;
430  }
431  break;
432  case IMGTYPE_YUV411:
433  for (row = 0; row < height; row++) {
434  for (col = 0; col < width - 3; col += 4) {
435  memcpy(y_out + col, encoded, 4);
436  encoded += 4;
437  u_out[col >> 2] = *encoded++ + 128;
438  v_out[col >> 2] = *encoded++ + 128;
439  }
440  if (col && col < width) {
441  u_out[col >> 2] = u_out[(col>>2) - 1];
442  v_out[col >> 2] = v_out[(col>>2) - 1];
443  }
444  y_out -= frame->linesize[0];
445  u_out -= frame->linesize[1];
446  v_out -= frame->linesize[2];
447  }
448  break;
449  case IMGTYPE_YUV211:
450  for (row = 0; row < height; row++) {
451  for (col = 0; col < width - 1; col += 2) {
452  memcpy(y_out + col, encoded, 2);
453  encoded += 2;
454  u_out[col >> 1] = *encoded++ + 128;
455  v_out[col >> 1] = *encoded++ + 128;
456  }
457  y_out -= frame->linesize[0];
458  u_out -= frame->linesize[1];
459  v_out -= frame->linesize[2];
460  }
461  break;
462  case IMGTYPE_YUV420:
463  u_out = frame->data[1] + ((height >> 1) - 1) * frame->linesize[1];
464  v_out = frame->data[2] + ((height >> 1) - 1) * frame->linesize[2];
465  for (row = 0; row < height - 1; row += 2) {
466  for (col = 0; col < width - 1; col += 2) {
467  memcpy(y_out + col, encoded, 2);
468  encoded += 2;
469  memcpy(y_out + col - frame->linesize[0], encoded, 2);
470  encoded += 2;
471  u_out[col >> 1] = *encoded++ + 128;
472  v_out[col >> 1] = *encoded++ + 128;
473  }
474  y_out -= frame->linesize[0] << 1;
475  u_out -= frame->linesize[1];
476  v_out -= frame->linesize[2];
477  }
478  break;
479  default:
480  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
481  return AVERROR_INVALIDDATA;
482  }
483 
484  frame->flags |= AV_FRAME_FLAG_KEY;
485  frame->pict_type = AV_PICTURE_TYPE_I;
486 
487  *got_frame = 1;
488 
489  /* always report that the buffer was completely consumed */
490  return buf_size;
491 }
492 
494 {
495  LclDecContext * const c = avctx->priv_data;
496  unsigned int basesize = avctx->width * avctx->height;
497  unsigned int max_basesize = FFALIGN(avctx->width, 4) *
498  FFALIGN(avctx->height, 4);
499  unsigned int max_decomp_size;
500  int subsample_h, subsample_v;
501  int partial_h_supported = 0;
502 
503  if (avctx->extradata_size < 8) {
504  av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
505  return AVERROR_INVALIDDATA;
506  }
507 
508  /* Check codec type */
509  if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
510  (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
511  av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
512  }
513 
514  /* Detect image type */
515  switch (c->imgtype = avctx->extradata[4]) {
516  case IMGTYPE_YUV111:
517  c->decomp_size = basesize * 3;
518  max_decomp_size = max_basesize * 3;
519  avctx->pix_fmt = AV_PIX_FMT_YUV444P;
520  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
521  break;
522  case IMGTYPE_YUV422:
523  c->decomp_size = (avctx->width & ~3) * avctx->height * 2;
524  max_decomp_size = max_basesize * 2;
525  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
526  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
527  partial_h_supported = 1;
528  break;
529  case IMGTYPE_RGB24:
530  c->decomp_size = FFALIGN(avctx->width*3, 4) * avctx->height;
531  max_decomp_size = max_basesize * 3;
532  avctx->pix_fmt = AV_PIX_FMT_BGR24;
533  av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
534  break;
535  case IMGTYPE_YUV411:
536  c->decomp_size = (avctx->width & ~3) * avctx->height / 2 * 3;
537  max_decomp_size = max_basesize / 2 * 3;
538  avctx->pix_fmt = AV_PIX_FMT_YUV411P;
539  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
540  partial_h_supported = 1;
541  break;
542  case IMGTYPE_YUV211:
543  c->decomp_size = basesize * 2;
544  max_decomp_size = max_basesize * 2;
545  avctx->pix_fmt = AV_PIX_FMT_YUV422P;
546  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
547  break;
548  case IMGTYPE_YUV420:
549  c->decomp_size = basesize / 2 * 3;
550  max_decomp_size = max_basesize / 2 * 3;
551  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
552  av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
553  break;
554  default:
555  av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
556  return AVERROR_INVALIDDATA;
557  }
558 
559  av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &subsample_h, &subsample_v);
560  if ((avctx->width % (1<<subsample_h) && !partial_h_supported) || avctx->height % (1<<subsample_v)) {
561  avpriv_request_sample(avctx, "Unsupported dimensions");
562  return AVERROR_INVALIDDATA;
563  }
564 
565  /* Detect compression method */
566  c->compression = (int8_t)avctx->extradata[5];
567  switch (avctx->codec_id) {
568  case AV_CODEC_ID_MSZH:
569  switch (c->compression) {
570  case COMP_MSZH:
571  av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
572  break;
573  case COMP_MSZH_NOCOMP:
574  c->decomp_size = 0;
575  av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
576  break;
577  default:
578  av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
579  return AVERROR_INVALIDDATA;
580  }
581  break;
582 #if CONFIG_ZLIB_DECODER
583  case AV_CODEC_ID_ZLIB:
584  switch (c->compression) {
585  case COMP_ZLIB_HISPEED:
586  av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
587  break;
588  case COMP_ZLIB_HICOMP:
589  av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
590  break;
591  case COMP_ZLIB_NORMAL:
592  av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
593  break;
594  default:
595  if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
596  av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
597  return AVERROR_INVALIDDATA;
598  }
599  av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
600  }
601  break;
602 #endif
603  default:
604  av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
605  return AVERROR_INVALIDDATA;
606  }
607 
608  /* Allocate decompression buffer */
609  if (c->decomp_size) {
610  if (!(c->decomp_buf = av_malloc(max_decomp_size))) {
611  av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
612  return AVERROR(ENOMEM);
613  }
614  }
615 
616  /* Detect flags */
617  c->flags = avctx->extradata[6];
618  if (c->flags & FLAG_MULTITHREAD)
619  av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
620  if (c->flags & FLAG_NULLFRAME)
621  av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
622  if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
623  av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
624  if (c->flags & FLAGMASK_UNUSED)
625  av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
626 
627  /* If needed init zlib */
628 #if CONFIG_ZLIB_DECODER
629  if (avctx->codec_id == AV_CODEC_ID_ZLIB)
630  return ff_inflate_init(&c->zstream, avctx);
631 #endif
632 
633  return 0;
634 }
635 
637 {
638  LclDecContext * const c = avctx->priv_data;
639 
640  av_freep(&c->decomp_buf);
641 #if CONFIG_ZLIB_DECODER
642  ff_inflate_end(&c->zstream);
643 #endif
644 
645  return 0;
646 }
647 
648 #if CONFIG_MSZH_DECODER
649 const FFCodec ff_mszh_decoder = {
650  .p.name = "mszh",
651  CODEC_LONG_NAME("LCL (LossLess Codec Library) MSZH"),
652  .p.type = AVMEDIA_TYPE_VIDEO,
653  .p.id = AV_CODEC_ID_MSZH,
654  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
655  .priv_data_size = sizeof(LclDecContext),
656  .init = decode_init,
657  .close = decode_end,
659  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
660 };
661 #endif
662 
663 #if CONFIG_ZLIB_DECODER
664 const FFCodec ff_zlib_decoder = {
665  .p.name = "zlib",
666  CODEC_LONG_NAME("LCL (LossLess Codec Library) ZLIB"),
667  .p.type = AVMEDIA_TYPE_VIDEO,
668  .p.id = AV_CODEC_ID_ZLIB,
669  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
670  .priv_data_size = sizeof(LclDecContext),
671  .init = decode_init,
672  .close = decode_end,
674  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
675 };
676 #endif
LclDecContext::decomp_size
unsigned int decomp_size
Definition: lcldec.c:66
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
IMGTYPE_YUV420
#define IMGTYPE_YUV420
Definition: lcl.h:33
CODEC_ZLIB
#define CODEC_ZLIB
Definition: lcl.h:47
mszh_decomp
static unsigned int mszh_decomp(const unsigned char *srcptr, int srclen, unsigned char *destptr, unsigned int destsize)
Definition: lcldec.c:79
IMGTYPE_YUV211
#define IMGTYPE_YUV211
Definition: lcl.h:32
COMP_MSZH_NOCOMP
#define COMP_MSZH_NOCOMP
Definition: lcl.h:36
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
pixdesc.h
AVPacket::data
uint8_t * data
Definition: packet.h:524
ff_zlib_decoder
const FFCodec ff_zlib_decoder
FLAG_MULTITHREAD
#define FLAG_MULTITHREAD
Definition: lcl.h:41
FFCodec
Definition: codec_internal.h:126
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:76
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
ff_mszh_decoder
const FFCodec ff_mszh_decoder
thread.h
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
lcl.h
LclDecContext::flags
int flags
Definition: lcldec.c:64
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:130
inflate
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:194
AV_CODEC_ID_MSZH
@ AV_CODEC_ID_MSZH
Definition: codec_id.h:105
av_pix_fmt_get_chroma_sub_sample
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:2993
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
zlib_wrapper.h
av_cold
#define av_cold
Definition: attributes.h:90
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:625
mask
static const uint16_t mask[17]
Definition: lzw.c:38
COMP_ZLIB_NORMAL
#define COMP_ZLIB_NORMAL
Definition: lcl.h:39
av_memcpy_backptr
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
Definition: mem.c:447
LclDecContext::imgtype
int imgtype
Definition: lcldec.c:60
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:524
width
#define width
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:286
ff_thread_get_buffer
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
Definition: pthread_frame.c:975
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: lcldec.c:493
LclDecContext::decomp_buf
unsigned char * decomp_buf
Definition: lcldec.c:68
IMGTYPE_RGB24
#define IMGTYPE_RGB24
Definition: lcl.h:30
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
IMGTYPE_YUV422
#define IMGTYPE_YUV422
Definition: lcl.h:29
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:271
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:455
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:110
FLAG_NULLFRAME
#define FLAG_NULLFRAME
Definition: lcl.h:42
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
AV_CODEC_ID_ZLIB
@ AV_CODEC_ID_ZLIB
Definition: codec_id.h:106
FF_PTR_ADD
#define FF_PTR_ADD(ptr, off)
Definition: internal.h:80
COMP_MSZH
#define COMP_MSZH
Definition: lcl.h:35
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
decode_end
static av_cold int decode_end(AVCodecContext *avctx)
Definition: lcldec.c:636
CODEC_MSZH
#define CODEC_MSZH
Definition: lcl.h:46
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:366
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:525
codec_internal.h
IMGTYPE_YUV411
#define IMGTYPE_YUV411
Definition: lcl.h:31
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:410
height
#define height
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
IMGTYPE_YUV111
#define IMGTYPE_YUV111
Definition: lcl.h:28
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:523
LclDecContext::compression
int compression
Definition: lcldec.c:62
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
COMP_ZLIB_HICOMP
#define COMP_ZLIB_HICOMP
Definition: lcl.h:38
COMP_ZLIB_HISPEED
#define COMP_ZLIB_HISPEED
Definition: lcl.h:37
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
len
int len
Definition: vorbis_enc_data.h:426
ff_inflate_end
void ff_inflate_end(FFZStream *zstream)
Wrapper around inflateEnd().
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: lcldec.c:164
LclDecContext
Definition: lcldec.c:58
AVCodecContext::height
int height
Definition: avcodec.h:618
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FFZStream
Definition: zlib_wrapper.h:27
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:77
mem.h
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVPacket
This structure stores compressed data.
Definition: packet.h:501
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
FLAG_PNGFILTER
#define FLAG_PNGFILTER
Definition: lcl.h:43
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AV_PIX_FMT_YUV411P
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:80
ff_inflate_init
int ff_inflate_init(FFZStream *zstream, void *logctx)
Wrapper around inflateInit().
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
bytestream.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
FLAGMASK_UNUSED
#define FLAGMASK_UNUSED
Definition: lcl.h:44
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61