FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/lcldec.c
Date: 2026-08-14 15:11:37
Exec Total Coverage
Lines: 148 421 35.2%
Functions: 5 5 100.0%
Branches: 70 210 33.3%

Line Branch Exec Source
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/attributes.h"
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
59 typedef struct LclDecContext {
60 // Image type
61 int imgtype;
62 // Compression type
63 int compression;
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
73 } LclDecContext;
74
75
76 /**
77 * @param srcptr compressed source buffer, must be padded with at least 5 extra bytes
78 * @param destptr must be padded sufficiently for av_memcpy_backptr
79 */
80 1 static unsigned int mszh_decomp(const unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
81 {
82 1 unsigned char *destptr_bak = destptr;
83 1 unsigned char *destptr_end = destptr + destsize;
84 1 const unsigned char *srcptr_end = srcptr + srclen;
85 1 unsigned mask = *srcptr++;
86 1 unsigned maskbit = 0x80;
87
88
3/4
✓ Branch 0 taken 30272 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 30272 times.
✗ Branch 3 not taken.
30274 while (srcptr < srcptr_end && destptr < destptr_end) {
89
2/2
✓ Branch 0 taken 19751 times.
✓ Branch 1 taken 10521 times.
30272 if (!(mask & maskbit)) {
90 19751 memcpy(destptr, srcptr, 4);
91 19751 destptr += 4;
92 19751 srcptr += 4;
93 } else {
94 10521 unsigned ofs = bytestream_get_le16(&srcptr);
95 10521 unsigned cnt = (ofs >> 11) + 1;
96 10521 ofs &= 0x7ff;
97 10521 ofs = FFMIN(ofs, destptr - destptr_bak);
98 10521 cnt *= 4;
99 10521 cnt = FFMIN(cnt, destptr_end - destptr);
100
1/2
✓ Branch 0 taken 10521 times.
✗ Branch 1 not taken.
10521 if (ofs) {
101 10521 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 10521 destptr += cnt;
108 }
109 30272 maskbit >>= 1;
110
2/2
✓ Branch 0 taken 26488 times.
✓ Branch 1 taken 3784 times.
30272 if (!maskbit) {
111 3784 mask = *srcptr++;
112
2/2
✓ Branch 0 taken 2713 times.
✓ Branch 1 taken 3783 times.
6496 while (!mask) {
113
3/4
✓ Branch 0 taken 2712 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2712 times.
✗ Branch 3 not taken.
2713 if (destptr_end - destptr < 32 || srcptr_end - srcptr < 32) break;
114 2712 memcpy(destptr, srcptr, 32);
115 2712 destptr += 32;
116 2712 srcptr += 32;
117 2712 mask = *srcptr++;
118 }
119 3784 maskbit = 0x80;
120 }
121 }
122
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (destptr < destptr_end)
124 memset(destptr, 0, destptr_end - destptr);
125
126 1 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 */
138 201 static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected)
139 {
140 201 LclDecContext *c = avctx->priv_data;
141 201 z_stream *const zstream = &c->zstream.zstream;
142 201 int zret = inflateReset(zstream);
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 201 times.
201 if (zret != Z_OK) {
144 av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
145 return AVERROR_UNKNOWN;
146 }
147 201 zstream->next_in = src;
148 201 zstream->avail_in = src_len;
149 201 zstream->next_out = c->decomp_buf + offset;
150 201 zstream->avail_out = c->decomp_size - offset;
151 201 zret = inflate(zstream, Z_FINISH);
152
2/4
✓ Branch 0 taken 201 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 201 times.
201 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
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 151 times.
201 if (expected != (unsigned int)zstream->total_out) {
157 50 av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
158 expected, zstream->total_out);
159
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (expected > (unsigned int)zstream->total_out) {
160 50 memset(c->decomp_buf + offset + zstream->total_out, 0,
161 50 c->decomp_size - offset - zstream->total_out);
162 50 return (unsigned int)zstream->total_out;
163 }
164 return AVERROR_UNKNOWN;
165 }
166 151 return zstream->total_out;
167 }
168 #endif
169
170
171 202 static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
172 int *got_frame, AVPacket *avpkt)
173 {
174 202 const uint8_t *buf = avpkt->data;
175 202 int buf_size = avpkt->size;
176 202 LclDecContext * const c = avctx->priv_data;
177 ptrdiff_t pixel_ptr;
178 int row, col;
179 202 unsigned char *encoded = avpkt->data, *outptr;
180 uint8_t *y_out, *u_out, *v_out;
181 202 int width = avctx->width; // Real image width
182 202 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 202 unsigned int len = buf_size;
188 int linesize, offset;
189
190
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 202 times.
202 if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
191 return ret;
192
193 202 outptr = frame->data[0]; // Output image pointer
194
195 /* Decompress frame */
196
2/3
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 201 times.
✗ Branch 2 not taken.
202 switch (avctx->codec_id) {
197 1 case AV_CODEC_ID_MSZH:
198
1/3
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
1 switch (c->compression) {
199 1 case COMP_MSZH:
200
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (c->imgtype == IMGTYPE_RGB24 && len == FFALIGN(width * 3, 4) * height ||
201
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 c->imgtype == IMGTYPE_YUV111 && len == width * height * 3) {
202 ;
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } 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 1 mszh_dlen = mszh_decomp(buf, len, c->decomp_buf, c->decomp_size);
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 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 1 encoded = c->decomp_buf;
236 1 len = mszh_dlen;
237 }
238 1 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;
249 av_fallthrough;
250 case IMGTYPE_YUV211:
251 bppx2 = 4;
252 break;
253 case IMGTYPE_YUV411:
254 aligned_width &= ~3;
255 av_fallthrough;
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 1 break;
272 #if CONFIG_ZLIB_DECODER
273 201 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
3/4
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 200 times.
✗ Branch 3 not taken.
201 if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 201 times.
201 } else if (c->flags & FLAG_MULTITHREAD) {
286 mthread_inlen = AV_RL32(buf);
287 mthread_inlen = FFMIN(mthread_inlen, len - 8);
288 mthread_outlen = AV_RL32(buf + 4);
289 mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
290 ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen);
291 if (ret < 0) return ret;
292 ret = zlib_decomp(avctx, buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
293 mthread_outlen, mthread_outlen);
294 if (ret < 0) return ret;
295 memset(c->decomp_buf + mthread_outlen + ret, 0,
296 c->decomp_size - mthread_outlen - ret);
297 len = c->decomp_size;
298 } else {
299 201 int ret = zlib_decomp(avctx, buf, len, 0, c->decomp_size);
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 201 times.
201 if (ret < 0) return ret;
301 201 len = ret;
302 }
303 201 encoded = c->decomp_buf;
304 201 break;
305 #endif
306 default:
307 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
308 return AVERROR_INVALIDDATA;
309 }
310
311
312 /* Apply PNG filter */
313
3/4
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 201 times.
202 if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
314 switch (c->imgtype) {
315 case IMGTYPE_YUV111:
316 case IMGTYPE_RGB24:
317 for (row = 0; row < height; row++) {
318 pixel_ptr = row * width * 3;
319 yq = encoded[pixel_ptr++];
320 unsigned uqvq = AV_RL16(encoded+pixel_ptr);
321 pixel_ptr += 2;
322 for (col = 1; col < width; col++) {
323 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
324 uqvq -= AV_RL16(encoded+pixel_ptr+1);
325 AV_WL16(encoded+pixel_ptr+1, uqvq);
326 pixel_ptr += 3;
327 }
328 }
329 break;
330 case IMGTYPE_YUV422:
331 pixel_ptr = 0;
332 for (row = 0; row < height; row++) {
333 yq = uq = vq =0;
334 for (col = 0; col < width/4; col++) {
335 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
336 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
337 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
338 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
339 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
340 encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
341 encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
342 encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
343 pixel_ptr += 8;
344 }
345 }
346 break;
347 case IMGTYPE_YUV411:
348 pixel_ptr = 0;
349 for (row = 0; row < height; row++) {
350 yq = uq = vq =0;
351 for (col = 0; col < width/4; col++) {
352 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
353 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
354 encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
355 encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
356 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
357 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
358 pixel_ptr += 6;
359 }
360 }
361 break;
362 case IMGTYPE_YUV211:
363 for (row = 0; row < height; row++) {
364 pixel_ptr = row * width * 2;
365 yq = uq = vq =0;
366 for (col = 0; col < width/2; col++) {
367 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
368 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
369 encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
370 encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
371 pixel_ptr += 4;
372 }
373 }
374 break;
375 case IMGTYPE_YUV420:
376 for (row = 0; row < height/2; row++) {
377 pixel_ptr = row * width * 3;
378 yq = y1q = uq = vq =0;
379 for (col = 0; col < width/2; col++) {
380 encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
381 encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
382 encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
383 encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
384 encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
385 encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
386 pixel_ptr += 6;
387 }
388 }
389 break;
390 default:
391 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
392 return AVERROR_INVALIDDATA;
393 }
394 }
395
396 /* Convert colorspace */
397 202 y_out = frame->data[0] + (height - 1) * frame->linesize[0];
398 202 offset = (height - 1) * frame->linesize[1];
399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 202 times.
202 u_out = FF_PTR_ADD(frame->data[1], offset);
400 202 offset = (height - 1) * frame->linesize[2];
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 202 times.
202 v_out = FF_PTR_ADD(frame->data[2], offset);
402
1/7
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 202 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
202 switch (c->imgtype) {
403 case IMGTYPE_YUV111:
404 for (row = 0; row < height; row++) {
405 for (col = 0; col < width; col++) {
406 y_out[col] = *encoded++;
407 u_out[col] = *encoded++ + 128;
408 v_out[col] = *encoded++ + 128;
409 }
410 y_out -= frame->linesize[0];
411 u_out -= frame->linesize[1];
412 v_out -= frame->linesize[2];
413 }
414 break;
415 case IMGTYPE_YUV422:
416 for (row = 0; row < height; row++) {
417 for (col = 0; col < width - 3; col += 4) {
418 memcpy(y_out + col, encoded, 4);
419 encoded += 4;
420 u_out[ col >> 1 ] = *encoded++ + 128;
421 u_out[(col >> 1) + 1] = *encoded++ + 128;
422 v_out[ col >> 1 ] = *encoded++ + 128;
423 v_out[(col >> 1) + 1] = *encoded++ + 128;
424 }
425 if (col && col < width) {
426 u_out[ col >> 1 ] = u_out[(col>>1) - 1];
427 v_out[ col >> 1 ] = v_out[(col>>1) - 1];
428 }
429
430 y_out -= frame->linesize[0];
431 u_out -= frame->linesize[1];
432 v_out -= frame->linesize[2];
433 }
434 break;
435 202 case IMGTYPE_RGB24:
436
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 152 times.
202 linesize = len < FFALIGN(3 * width, 4) * height ? 3 * width : FFALIGN(3 * width, 4);
437
2/2
✓ Branch 0 taken 45380 times.
✓ Branch 1 taken 202 times.
45582 for (row = height - 1; row >= 0; row--) {
438 45380 pixel_ptr = row * frame->linesize[0];
439 45380 memcpy(outptr + pixel_ptr, encoded, 3 * width);
440 45380 encoded += linesize;
441 }
442 202 break;
443 case IMGTYPE_YUV411:
444 for (row = 0; row < height; row++) {
445 for (col = 0; col < width - 3; col += 4) {
446 memcpy(y_out + col, encoded, 4);
447 encoded += 4;
448 u_out[col >> 2] = *encoded++ + 128;
449 v_out[col >> 2] = *encoded++ + 128;
450 }
451 if (col && col < width) {
452 u_out[col >> 2] = u_out[(col>>2) - 1];
453 v_out[col >> 2] = v_out[(col>>2) - 1];
454 }
455 y_out -= frame->linesize[0];
456 u_out -= frame->linesize[1];
457 v_out -= frame->linesize[2];
458 }
459 break;
460 case IMGTYPE_YUV211:
461 for (row = 0; row < height; row++) {
462 for (col = 0; col < width - 1; col += 2) {
463 memcpy(y_out + col, encoded, 2);
464 encoded += 2;
465 u_out[col >> 1] = *encoded++ + 128;
466 v_out[col >> 1] = *encoded++ + 128;
467 }
468 y_out -= frame->linesize[0];
469 u_out -= frame->linesize[1];
470 v_out -= frame->linesize[2];
471 }
472 break;
473 case IMGTYPE_YUV420:
474 u_out = frame->data[1] + ((height >> 1) - 1) * frame->linesize[1];
475 v_out = frame->data[2] + ((height >> 1) - 1) * frame->linesize[2];
476 for (row = 0; row < height - 1; row += 2) {
477 for (col = 0; col < width - 1; col += 2) {
478 memcpy(y_out + col, encoded, 2);
479 encoded += 2;
480 memcpy(y_out + col - frame->linesize[0], encoded, 2);
481 encoded += 2;
482 u_out[col >> 1] = *encoded++ + 128;
483 v_out[col >> 1] = *encoded++ + 128;
484 }
485 y_out -= frame->linesize[0] << 1;
486 u_out -= frame->linesize[1];
487 v_out -= frame->linesize[2];
488 }
489 break;
490 default:
491 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
492 return AVERROR_INVALIDDATA;
493 }
494
495 202 *got_frame = 1;
496
497 /* always report that the buffer was completely consumed */
498 202 return buf_size;
499 }
500
501 12 static av_cold int decode_init(AVCodecContext *avctx)
502 {
503 12 LclDecContext * const c = avctx->priv_data;
504 12 unsigned int basesize = avctx->width * avctx->height;
505 12 unsigned int max_basesize = FFALIGN(avctx->width, 4) *
506 12 FFALIGN(avctx->height, 4);
507 unsigned int max_decomp_size;
508 int subsample_h, subsample_v;
509 12 int partial_h_supported = 0;
510
511
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (avctx->extradata_size < 8) {
512 av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
513 return AVERROR_INVALIDDATA;
514 }
515
516 /* Check codec type */
517
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
12 if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
518
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
12 (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
519 av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
520 }
521
522 /* Detect image type */
523
1/7
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
12 switch (c->imgtype = avctx->extradata[4]) {
524 case IMGTYPE_YUV111:
525 c->decomp_size = basesize * 3;
526 max_decomp_size = max_basesize * 3;
527 avctx->pix_fmt = AV_PIX_FMT_YUV444P;
528 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
529 break;
530 case IMGTYPE_YUV422:
531 c->decomp_size = (avctx->width & ~3) * avctx->height * 2;
532 max_decomp_size = max_basesize * 2;
533 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
534 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
535 partial_h_supported = 1;
536 break;
537 12 case IMGTYPE_RGB24:
538 12 c->decomp_size = FFALIGN(avctx->width*3, 4) * avctx->height;
539 12 max_decomp_size = max_basesize * 3;
540 12 avctx->pix_fmt = AV_PIX_FMT_BGR24;
541 12 av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
542 12 break;
543 case IMGTYPE_YUV411:
544 c->decomp_size = (avctx->width & ~3) * avctx->height / 2 * 3;
545 max_decomp_size = max_basesize / 2 * 3;
546 avctx->pix_fmt = AV_PIX_FMT_YUV411P;
547 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
548 partial_h_supported = 1;
549 break;
550 case IMGTYPE_YUV211:
551 c->decomp_size = basesize * 2;
552 max_decomp_size = max_basesize * 2;
553 avctx->pix_fmt = AV_PIX_FMT_YUV422P;
554 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
555 break;
556 case IMGTYPE_YUV420:
557 c->decomp_size = basesize / 2 * 3;
558 max_decomp_size = max_basesize / 2 * 3;
559 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
560 av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
561 break;
562 default:
563 av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
564 return AVERROR_INVALIDDATA;
565 }
566
567 12 av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &subsample_h, &subsample_v);
568
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
12 if ((avctx->width % (1<<subsample_h) && !partial_h_supported) || avctx->height % (1<<subsample_v)) {
569 avpriv_request_sample(avctx, "Unsupported dimensions");
570 return AVERROR_INVALIDDATA;
571 }
572
573 /* Detect compression method */
574 12 c->compression = (int8_t)avctx->extradata[5];
575
2/3
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
12 switch (avctx->codec_id) {
576 2 case AV_CODEC_ID_MSZH:
577
1/3
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
2 switch (c->compression) {
578 2 case COMP_MSZH:
579 2 av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
580 2 break;
581 case COMP_MSZH_NOCOMP:
582 c->decomp_size = 0;
583 av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
584 break;
585 default:
586 av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
587 return AVERROR_INVALIDDATA;
588 }
589 2 break;
590 #if CONFIG_ZLIB_DECODER
591 10 case AV_CODEC_ID_ZLIB:
592
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
10 switch (c->compression) {
593 case COMP_ZLIB_HISPEED:
594 av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
595 break;
596 2 case COMP_ZLIB_HICOMP:
597 2 av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
598 2 break;
599 8 case COMP_ZLIB_NORMAL:
600 8 av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
601 8 break;
602 default:
603 if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
604 av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
605 return AVERROR_INVALIDDATA;
606 }
607 av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
608 }
609 10 break;
610 #endif
611 default:
612 av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
613 return AVERROR_INVALIDDATA;
614 }
615
616 /* Allocate decompression buffer */
617
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (c->decomp_size) {
618
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if (!(c->decomp_buf = av_malloc(max_decomp_size))) {
619 av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
620 return AVERROR(ENOMEM);
621 }
622 }
623
624 /* Detect flags */
625 12 c->flags = avctx->extradata[6];
626
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (c->flags & FLAG_MULTITHREAD)
627 av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (c->flags & FLAG_NULLFRAME)
629 av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
630
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
12 if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
631 av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
632
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (c->flags & FLAGMASK_UNUSED)
633 av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
634
635 /* If needed init zlib */
636 #if CONFIG_ZLIB_DECODER
637
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
12 if (avctx->codec_id == AV_CODEC_ID_ZLIB)
638 10 return ff_inflate_init(&c->zstream, avctx);
639 #endif
640
641 2 return 0;
642 }
643
644 12 static av_cold int decode_end(AVCodecContext *avctx)
645 {
646 12 LclDecContext * const c = avctx->priv_data;
647
648 12 av_freep(&c->decomp_buf);
649 #if CONFIG_ZLIB_DECODER
650 12 ff_inflate_end(&c->zstream);
651 #endif
652
653 12 return 0;
654 }
655
656 #if CONFIG_MSZH_DECODER
657 const FFCodec ff_mszh_decoder = {
658 .p.name = "mszh",
659 CODEC_LONG_NAME("LCL (LossLess Codec Library) MSZH"),
660 .p.type = AVMEDIA_TYPE_VIDEO,
661 .p.id = AV_CODEC_ID_MSZH,
662 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
663 .priv_data_size = sizeof(LclDecContext),
664 .init = decode_init,
665 .close = decode_end,
666 FF_CODEC_DECODE_CB(decode_frame),
667 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
668 };
669 #endif
670
671 #if CONFIG_ZLIB_DECODER
672 const FFCodec ff_zlib_decoder = {
673 .p.name = "zlib",
674 CODEC_LONG_NAME("LCL (LossLess Codec Library) ZLIB"),
675 .p.type = AVMEDIA_TYPE_VIDEO,
676 .p.id = AV_CODEC_ID_ZLIB,
677 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
678 .priv_data_size = sizeof(LclDecContext),
679 .init = decode_init,
680 .close = decode_end,
681 FF_CODEC_DECODE_CB(decode_frame),
682 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
683 };
684 #endif
685