FFmpeg
Loading...
Searching...
No Matches
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
46#include "libavutil/mem.h"
47#include "libavutil/pixdesc.h"
48#include "avcodec.h"
49#include "bytestream.h"
50#include "codec_internal.h"
51#include "lcl.h"
52#include "thread.h"
53
54#if CONFIG_ZLIB_DECODER
55#include "zlib_wrapper.h"
56#include <zlib.h>
57#endif
58
59typedef struct LclDecContext {
60 // Image type
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 FFZStream 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 */
80static 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 if (destptr < destptr_end)
124 memset(destptr, 0, destptr_end - destptr);
125
126 return destptr - destptr_bak;
127}
128
129
130#if CONFIG_ZLIB_DECODER
131/**
132 * @brief decompress a zlib-compressed data block into decomp_buf
133 * @param src compressed input buffer
134 * @param src_len data length in input buffer
135 * @param offset offset in decomp_buf
136 * @param expected expected decompressed length
137 */
138static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected)
139{
140 LclDecContext *c = avctx->priv_data;
141 z_stream *const zstream = &c->zstream.zstream;
142 int zret = inflateReset(zstream);
143 if (zret != Z_OK) {
144 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
145 return AVERROR_UNKNOWN;
146 }
147 zstream->next_in = src;
148 zstream->avail_in = src_len;
149 zstream->next_out = c->decomp_buf + offset;
150 zstream->avail_out = c->decomp_size - offset;
151 zret = inflate(zstream, Z_FINISH);
152 if (zret != Z_OK && zret != Z_STREAM_END) {
153 av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
154 return AVERROR_UNKNOWN;
155 }
156 if (expected != (unsigned int)zstream->total_out) {
157 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
158 expected, zstream->total_out);
159 if (expected > (unsigned int)zstream->total_out) {
160 memset(c->decomp_buf + offset + zstream->total_out, 0,
161 c->decomp_size - offset - zstream->total_out);
162 return (unsigned int)zstream->total_out;
163 }
164 return AVERROR_UNKNOWN;
165 }
166 return zstream->total_out;
167}
168#endif
169
170
172 int *got_frame, AVPacket *avpkt)
173{
174 const uint8_t *buf = avpkt->data;
175 int buf_size = avpkt->size;
176 LclDecContext * const c = avctx->priv_data;
177 ptrdiff_t pixel_ptr;
178 int row, col;
179 unsigned char *encoded = avpkt->data, *outptr;
180 uint8_t *y_out, *u_out, *v_out;
181 int width = avctx->width; // Real image width
182 int height = avctx->height; // Real image height
183 unsigned int mszh_dlen;
184 unsigned char yq, y1q, uq, vq;
185 int ret;
186 unsigned int mthread_inlen, mthread_outlen;
187 unsigned int len = buf_size;
188 int linesize, offset;
189
190 if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
191 return ret;
192
193 outptr = frame->data[0]; // Output image pointer
194
195 /* Decompress frame */
196 switch (avctx->codec_id) {
197 case AV_CODEC_ID_MSZH:
198 switch (c->compression) {
199 case COMP_MSZH:
200 if (c->imgtype == IMGTYPE_RGB24 && len == FFALIGN(width * 3, 4) * height ||
201 c->imgtype == IMGTYPE_YUV111 && len == width * height * 3) {
202 ;
203 } else if (c->flags & FLAG_MULTITHREAD) {
204 mthread_inlen = AV_RL32(buf);
205 if (len < 8 || len - 8 < mthread_inlen) {
206 av_log(avctx, AV_LOG_ERROR, "len %d is too small\n", len);
207 return AVERROR_INVALIDDATA;
208 }
209 mthread_outlen = AV_RL32(buf + 4);
210 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
211 mszh_dlen = mszh_decomp(buf + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
212 if (mthread_outlen != mszh_dlen) {
213 av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
214 mthread_outlen, mszh_dlen);
215 return AVERROR_INVALIDDATA;
216 }
217 mszh_dlen = mszh_decomp(buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
218 c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
219 if (mthread_outlen != mszh_dlen) {
220 av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
221 mthread_outlen, mszh_dlen);
222 return AVERROR_INVALIDDATA;
223 }
224 encoded = c->decomp_buf;
225 len = c->decomp_size;
226 } else {
227 mszh_dlen = mszh_decomp(buf, len, c->decomp_buf, c->decomp_size);
228 if (c->decomp_size != mszh_dlen) {
229 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
230 c->decomp_size, mszh_dlen);
231 if (c->decomp_size != mszh_dlen &&
232 c->decomp_size != mszh_dlen + 2) // YUV420 306x306 is missing 2 bytes
233 return AVERROR_INVALIDDATA;
234 }
235 encoded = c->decomp_buf;
236 len = mszh_dlen;
237 }
238 break;
239 case COMP_MSZH_NOCOMP: {
240 int bppx2;
241 int aligned_width = width;
242 switch (c->imgtype) {
243 case IMGTYPE_YUV111:
244 case IMGTYPE_RGB24:
245 bppx2 = 6;
246 break;
247 case IMGTYPE_YUV422:
248 aligned_width &= ~3;
250 case IMGTYPE_YUV211:
251 bppx2 = 4;
252 break;
253 case IMGTYPE_YUV411:
254 aligned_width &= ~3;
256 case IMGTYPE_YUV420:
257 bppx2 = 3;
258 break;
259 default:
260 bppx2 = 0; // will error out below
261 break;
262 }
263 if (len < ((aligned_width * height * bppx2) >> 1))
264 return AVERROR_INVALIDDATA;
265 break;
266 }
267 default:
268 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
269 return AVERROR_INVALIDDATA;
270 }
271 break;
272#if CONFIG_ZLIB_DECODER
273 case AV_CODEC_ID_ZLIB:
274 /* Using the original dll with normal compression (-1) and RGB format
275 * gives a file with ZLIB fourcc, but frame is really uncompressed.
276 * To be sure that's true check also frame size */
277 if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
278 len == width * height * 3) {
279 if (c->flags & FLAG_PNGFILTER) {
280 memcpy(c->decomp_buf, buf, len);
281 encoded = c->decomp_buf;
282 } else {
283 break;
284 }
285 } else if (c->flags & FLAG_MULTITHREAD) {
286 if (len < 8) {
287 av_log(avctx, AV_LOG_ERROR, "len %d is too small\n", len);
288 return AVERROR_INVALIDDATA;
289 }
290 mthread_inlen = AV_RL32(buf);
291 mthread_inlen = FFMIN(mthread_inlen, len - 8);
292 mthread_outlen = AV_RL32(buf + 4);
293 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
294 ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen);
295 if (ret < 0) return ret;
296 ret = zlib_decomp(avctx, buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
297 mthread_outlen, mthread_outlen);
298 if (ret < 0) return ret;
299 memset(c->decomp_buf + mthread_outlen + ret, 0,
300 c->decomp_size - mthread_outlen - ret);
301 len = c->decomp_size;
302 } else {
303 int ret = zlib_decomp(avctx, buf, len, 0, c->decomp_size);
304 if (ret < 0) return ret;
305 len = ret;
306 }
307 encoded = c->decomp_buf;
308 break;
309#endif
310 default:
311 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
312 return AVERROR_INVALIDDATA;
313 }
314
315
316 /* Apply PNG filter */
317 if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
318 switch (c->imgtype) {
319 case IMGTYPE_YUV111:
320 case IMGTYPE_RGB24:
321 for (row = 0; row < height; row++) {
322 pixel_ptr = row * width * 3;
323 yq = encoded[pixel_ptr++];
324 unsigned uqvq = AV_RL16(encoded+pixel_ptr);
325 pixel_ptr += 2;
326 for (col = 1; col < width; col++) {
327 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
328 uqvq -= AV_RL16(encoded+pixel_ptr+1);
329 AV_WL16(encoded+pixel_ptr+1, uqvq);
330 pixel_ptr += 3;
331 }
332 }
333 break;
334 case IMGTYPE_YUV422:
335 pixel_ptr = 0;
336 for (row = 0; row < height; row++) {
337 yq = uq = vq =0;
338 for (col = 0; col < width/4; col++) {
339 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
340 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
341 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
342 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
343 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
344 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
345 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
346 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
347 pixel_ptr += 8;
348 }
349 }
350 break;
351 case IMGTYPE_YUV411:
352 pixel_ptr = 0;
353 for (row = 0; row < height; row++) {
354 yq = uq = vq =0;
355 for (col = 0; col < width/4; col++) {
356 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
357 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
358 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
359 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
360 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
361 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
362 pixel_ptr += 6;
363 }
364 }
365 break;
366 case IMGTYPE_YUV211:
367 for (row = 0; row < height; row++) {
368 pixel_ptr = row * width * 2;
369 yq = uq = vq =0;
370 for (col = 0; col < width/2; col++) {
371 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
372 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
373 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
374 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
375 pixel_ptr += 4;
376 }
377 }
378 break;
379 case IMGTYPE_YUV420:
380 for (row = 0; row < height/2; row++) {
381 pixel_ptr = row * width * 3;
382 yq = y1q = uq = vq =0;
383 for (col = 0; col < width/2; col++) {
384 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
385 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
386 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
387 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
388 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
389 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
390 pixel_ptr += 6;
391 }
392 }
393 break;
394 default:
395 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
396 return AVERROR_INVALIDDATA;
397 }
398 }
399
400 /* Convert colorspace */
401 y_out = frame->data[0] + (height - 1) * frame->linesize[0];
402 offset = (height - 1) * frame->linesize[1];
403 u_out = FF_PTR_ADD(frame->data[1], offset);
404 offset = (height - 1) * frame->linesize[2];
405 v_out = FF_PTR_ADD(frame->data[2], offset);
406 switch (c->imgtype) {
407 case IMGTYPE_YUV111:
408 for (row = 0; row < height; row++) {
409 for (col = 0; col < width; col++) {
410 y_out[col] = *encoded++;
411 u_out[col] = *encoded++ + 128;
412 v_out[col] = *encoded++ + 128;
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_YUV422:
420 for (row = 0; row < height; row++) {
421 for (col = 0; col < width - 3; col += 4) {
422 memcpy(y_out + col, encoded, 4);
423 encoded += 4;
424 u_out[ col >> 1 ] = *encoded++ + 128;
425 u_out[(col >> 1) + 1] = *encoded++ + 128;
426 v_out[ col >> 1 ] = *encoded++ + 128;
427 v_out[(col >> 1) + 1] = *encoded++ + 128;
428 }
429 if (col && col < width) {
430 u_out[ col >> 1 ] = u_out[(col>>1) - 1];
431 v_out[ col >> 1 ] = v_out[(col>>1) - 1];
432 }
433
434 y_out -= frame->linesize[0];
435 u_out -= frame->linesize[1];
436 v_out -= frame->linesize[2];
437 }
438 break;
439 case IMGTYPE_RGB24:
440 linesize = len < FFALIGN(3 * width, 4) * height ? 3 * width : FFALIGN(3 * width, 4);
441 for (row = height - 1; row >= 0; row--) {
442 pixel_ptr = row * frame->linesize[0];
443 memcpy(outptr + pixel_ptr, encoded, 3 * width);
444 encoded += linesize;
445 }
446 break;
447 case IMGTYPE_YUV411:
448 for (row = 0; row < height; row++) {
449 for (col = 0; col < width - 3; col += 4) {
450 memcpy(y_out + col, encoded, 4);
451 encoded += 4;
452 u_out[col >> 2] = *encoded++ + 128;
453 v_out[col >> 2] = *encoded++ + 128;
454 }
455 if (col && col < width) {
456 u_out[col >> 2] = u_out[(col>>2) - 1];
457 v_out[col >> 2] = v_out[(col>>2) - 1];
458 }
459 y_out -= frame->linesize[0];
460 u_out -= frame->linesize[1];
461 v_out -= frame->linesize[2];
462 }
463 break;
464 case IMGTYPE_YUV211:
465 for (row = 0; row < height; row++) {
466 for (col = 0; col < width - 1; col += 2) {
467 memcpy(y_out + col, encoded, 2);
468 encoded += 2;
469 u_out[col >> 1] = *encoded++ + 128;
470 v_out[col >> 1] = *encoded++ + 128;
471 }
472 y_out -= frame->linesize[0];
473 u_out -= frame->linesize[1];
474 v_out -= frame->linesize[2];
475 }
476 break;
477 case IMGTYPE_YUV420:
478 u_out = frame->data[1] + ((height >> 1) - 1) * frame->linesize[1];
479 v_out = frame->data[2] + ((height >> 1) - 1) * frame->linesize[2];
480 for (row = 0; row < height - 1; row += 2) {
481 for (col = 0; col < width - 1; col += 2) {
482 memcpy(y_out + col, encoded, 2);
483 encoded += 2;
484 memcpy(y_out + col - frame->linesize[0], encoded, 2);
485 encoded += 2;
486 u_out[col >> 1] = *encoded++ + 128;
487 v_out[col >> 1] = *encoded++ + 128;
488 }
489 y_out -= frame->linesize[0] << 1;
490 u_out -= frame->linesize[1];
491 v_out -= frame->linesize[2];
492 }
493 break;
494 default:
495 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
496 return AVERROR_INVALIDDATA;
497 }
498
499 *got_frame = 1;
500
501 /* always report that the buffer was completely consumed */
502 return buf_size;
503}
504
506{
507 LclDecContext * const c = avctx->priv_data;
508 unsigned int basesize = avctx->width * avctx->height;
509 unsigned int max_basesize = FFALIGN(avctx->width, 4) *
510 FFALIGN(avctx->height, 4);
511 unsigned int max_decomp_size;
512 int subsample_h, subsample_v;
513 int partial_h_supported = 0;
514
515 if (avctx->extradata_size < 8) {
516 av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
517 return AVERROR_INVALIDDATA;
518 }
519
520 /* Check codec type */
521 if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
522 (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
523 av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
524 }
525
526 /* Detect image type */
527 switch (c->imgtype = avctx->extradata[4]) {
528 case IMGTYPE_YUV111:
529 c->decomp_size = basesize * 3;
530 max_decomp_size = max_basesize * 3;
532 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
533 break;
534 case IMGTYPE_YUV422:
535 c->decomp_size = (avctx->width & ~3) * avctx->height * 2;
536 max_decomp_size = max_basesize * 2;
538 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
539 partial_h_supported = 1;
540 break;
541 case IMGTYPE_RGB24:
542 c->decomp_size = FFALIGN(avctx->width*3, 4) * avctx->height;
543 max_decomp_size = max_basesize * 3;
544 avctx->pix_fmt = AV_PIX_FMT_BGR24;
545 av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
546 break;
547 case IMGTYPE_YUV411:
548 c->decomp_size = (avctx->width & ~3) * avctx->height / 2 * 3;
549 max_decomp_size = max_basesize / 2 * 3;
551 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
552 partial_h_supported = 1;
553 break;
554 case IMGTYPE_YUV211:
555 c->decomp_size = basesize * 2;
556 max_decomp_size = max_basesize * 2;
558 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
559 break;
560 case IMGTYPE_YUV420:
561 c->decomp_size = basesize / 2 * 3;
562 max_decomp_size = max_basesize / 2 * 3;
564 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
565 break;
566 default:
567 av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
568 return AVERROR_INVALIDDATA;
569 }
570
571 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &subsample_h, &subsample_v);
572 if ((avctx->width % (1<<subsample_h) && !partial_h_supported) || avctx->height % (1<<subsample_v)) {
573 avpriv_request_sample(avctx, "Unsupported dimensions");
574 return AVERROR_INVALIDDATA;
575 }
576
577 /* Detect compression method */
578 c->compression = (int8_t)avctx->extradata[5];
579 switch (avctx->codec_id) {
580 case AV_CODEC_ID_MSZH:
581 switch (c->compression) {
582 case COMP_MSZH:
583 av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
584 break;
585 case COMP_MSZH_NOCOMP:
586 c->decomp_size = 0;
587 av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
588 break;
589 default:
590 av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
591 return AVERROR_INVALIDDATA;
592 }
593 break;
594#if CONFIG_ZLIB_DECODER
595 case AV_CODEC_ID_ZLIB:
596 switch (c->compression) {
598 av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
599 break;
600 case COMP_ZLIB_HICOMP:
601 av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
602 break;
603 case COMP_ZLIB_NORMAL:
604 av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
605 break;
606 default:
607 if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
608 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
609 return AVERROR_INVALIDDATA;
610 }
611 av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
612 }
613 break;
614#endif
615 default:
616 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
617 return AVERROR_INVALIDDATA;
618 }
619
620 /* Allocate decompression buffer */
621 if (c->decomp_size) {
622 if (!(c->decomp_buf = av_malloc(max_decomp_size))) {
623 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
624 return AVERROR(ENOMEM);
625 }
626 }
627
628 /* Detect flags */
629 c->flags = avctx->extradata[6];
630 if (c->flags & FLAG_MULTITHREAD)
631 av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
632 if (c->flags & FLAG_NULLFRAME)
633 av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
634 if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
635 av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
636 if (c->flags & FLAGMASK_UNUSED)
637 av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
638
639 /* If needed init zlib */
640#if CONFIG_ZLIB_DECODER
641 if (avctx->codec_id == AV_CODEC_ID_ZLIB)
642 return ff_inflate_init(&c->zstream, avctx);
643#endif
644
645 return 0;
646}
647
649{
650 LclDecContext * const c = avctx->priv_data;
651
652 av_freep(&c->decomp_buf);
653#if CONFIG_ZLIB_DECODER
654 ff_inflate_end(&c->zstream);
655#endif
656
657 return 0;
658}
659
660#if CONFIG_MSZH_DECODER
661const FFCodec ff_mszh_decoder = {
662 .p.name = "mszh",
663 CODEC_LONG_NAME("LCL (LossLess Codec Library) MSZH"),
664 .p.type = AVMEDIA_TYPE_VIDEO,
665 .p.id = AV_CODEC_ID_MSZH,
667 .priv_data_size = sizeof(LclDecContext),
668 .init = decode_init,
669 .close = decode_end,
671 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
672};
673#endif
674
675#if CONFIG_ZLIB_DECODER
676const FFCodec ff_zlib_decoder = {
677 .p.name = "zlib",
678 CODEC_LONG_NAME("LCL (LossLess Codec Library) ZLIB"),
679 .p.type = AVMEDIA_TYPE_VIDEO,
680 .p.id = AV_CODEC_ID_ZLIB,
682 .priv_data_size = sizeof(LclDecContext),
683 .init = decode_init,
684 .close = decode_end,
686 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
687};
688#endif
const FFCodec ff_mszh_decoder
const FFCodec ff_zlib_decoder
Libavcodec external API header.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
int ret
Definition ffplay.c:391
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition codec.h:98
@ AV_CODEC_ID_ZLIB
Definition codec_id.h:104
@ AV_CODEC_ID_MSZH
Definition codec_id.h:103
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition error.h:73
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
Overlapping memcpy() implementation.
Definition mem.c:551
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_RL32(p)
#define AV_RL16(p)
#define AV_WL16(p, v)
#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 int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition lcldec.c:171
static unsigned int mszh_decomp(const unsigned char *srcptr, int srclen, unsigned char *destptr, unsigned int destsize)
Definition lcldec.c:80
static av_cold int decode_init(AVCodecContext *avctx)
Definition lcldec.c:505
static av_cold int decode_end(AVCodecContext *avctx)
Definition lcldec.c:648
unsigned offset
Definition libaomenc.c:763
static av_cold int decode_init(AVCodecContext *avctx)
Definition 4xm.c:998
static av_cold int decode_end(AVCodecContext *avctx)
Definition 4xm.c:980
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition 4xm.c:837
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition cbs.c:118
Multithreading API for decoders.
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
#define av_cold
Definition attributes.h:117
#define FF_PTR_ADD(ptr, off)
Definition internal.h:83
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMIN(a, b)
Definition macros.h:49
#define FFALIGN(x, a)
Definition macros.h:78
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
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:3512
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:644
int width
picture width / height.
Definition avcodec.h:605
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
enum AVCodecID codec_id
Definition avcodec.h:453
int extradata_size
Definition avcodec.h:527
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
uint8_t * data
Definition packet.h:610
unsigned int decomp_size
Definition lcldec.c:67
unsigned char * decomp_buf
Definition lcldec.c:69
int imgtype
Definition lcldec.c:61
int compression
Definition lcldec.c:63
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99
static void inflate(uint8_t *dst, const uint8_t *p1, int width, int threshold, const uint8_t *coordinates[], int coord, int maxc)
int len
static double c[64]
void ff_inflate_end(FFZStream *zstream)
Wrapper around inflateEnd().
int ff_inflate_init(FFZStream *zstream, void *logctx)
Wrapper around inflateInit().