You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

638 lines
23KB

  1. /*
  2. * LCL (LossLess Codec Library) Codec
  3. * Copyright (c) 2002-2004 Roberto Togni
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * LCL (LossLess Codec Library) Video Codec
  24. * Decoder for MSZH and ZLIB codecs
  25. * Experimental encoder for ZLIB RGB24
  26. *
  27. * Fourcc: MSZH, ZLIB
  28. *
  29. * Original Win32 dll:
  30. * Ver2.23 By Kenji Oshima 2000.09.20
  31. * avimszh.dll, avizlib.dll
  32. *
  33. * A description of the decoding algorithm can be found here:
  34. * http://www.pcisys.net/~melanson/codecs
  35. *
  36. * Supports: BGR24 (RGB 24bpp)
  37. */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include "libavutil/mem.h"
  41. #include "avcodec.h"
  42. #include "bytestream.h"
  43. #include "internal.h"
  44. #include "lcl.h"
  45. #if CONFIG_ZLIB_DECODER
  46. #include <zlib.h>
  47. #endif
  48. typedef struct LclDecContext {
  49. // Image type
  50. int imgtype;
  51. // Compression type
  52. int compression;
  53. // Flags
  54. int flags;
  55. // Decompressed data size
  56. unsigned int decomp_size;
  57. // Decompression buffer
  58. unsigned char* decomp_buf;
  59. #if CONFIG_ZLIB_DECODER
  60. z_stream zstream;
  61. #endif
  62. } LclDecContext;
  63. /**
  64. * @param srcptr compressed source buffer, must be padded with at least 5 extra bytes
  65. * @param destptr must be padded sufficiently for av_memcpy_backptr
  66. */
  67. static unsigned int mszh_decomp(const unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
  68. {
  69. unsigned char *destptr_bak = destptr;
  70. unsigned char *destptr_end = destptr + destsize;
  71. const unsigned char *srcptr_end = srcptr + srclen;
  72. unsigned mask = *srcptr++;
  73. unsigned maskbit = 0x80;
  74. while (srcptr < srcptr_end && destptr < destptr_end) {
  75. if (!(mask & maskbit)) {
  76. memcpy(destptr, srcptr, 4);
  77. destptr += 4;
  78. srcptr += 4;
  79. } else {
  80. unsigned ofs = bytestream_get_le16(&srcptr);
  81. unsigned cnt = (ofs >> 11) + 1;
  82. ofs &= 0x7ff;
  83. ofs = FFMIN(ofs, destptr - destptr_bak);
  84. cnt *= 4;
  85. cnt = FFMIN(cnt, destptr_end - destptr);
  86. av_memcpy_backptr(destptr, ofs, cnt);
  87. destptr += cnt;
  88. }
  89. maskbit >>= 1;
  90. if (!maskbit) {
  91. mask = *srcptr++;
  92. while (!mask) {
  93. if (destptr_end - destptr < 32 || srcptr_end - srcptr < 32) break;
  94. memcpy(destptr, srcptr, 32);
  95. destptr += 32;
  96. srcptr += 32;
  97. mask = *srcptr++;
  98. }
  99. maskbit = 0x80;
  100. }
  101. }
  102. return destptr - destptr_bak;
  103. }
  104. #if CONFIG_ZLIB_DECODER
  105. /**
  106. * @brief decompress a zlib-compressed data block into decomp_buf
  107. * @param src compressed input buffer
  108. * @param src_len data length in input buffer
  109. * @param offset offset in decomp_buf
  110. * @param expected expected decompressed length
  111. */
  112. static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected)
  113. {
  114. LclDecContext *c = avctx->priv_data;
  115. int zret = inflateReset(&c->zstream);
  116. if (zret != Z_OK) {
  117. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  118. return AVERROR_UNKNOWN;
  119. }
  120. c->zstream.next_in = src;
  121. c->zstream.avail_in = src_len;
  122. c->zstream.next_out = c->decomp_buf + offset;
  123. c->zstream.avail_out = c->decomp_size - offset;
  124. zret = inflate(&c->zstream, Z_FINISH);
  125. if (zret != Z_OK && zret != Z_STREAM_END) {
  126. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
  127. return AVERROR_UNKNOWN;
  128. }
  129. if (expected != (unsigned int)c->zstream.total_out) {
  130. av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
  131. expected, c->zstream.total_out);
  132. return AVERROR_UNKNOWN;
  133. }
  134. return c->zstream.total_out;
  135. }
  136. #endif
  137. static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
  138. {
  139. AVFrame *frame = data;
  140. const uint8_t *buf = avpkt->data;
  141. int buf_size = avpkt->size;
  142. LclDecContext * const c = avctx->priv_data;
  143. unsigned int pixel_ptr;
  144. int row, col;
  145. unsigned char *encoded, *outptr;
  146. uint8_t *y_out, *u_out, *v_out;
  147. unsigned int width = avctx->width; // Real image width
  148. unsigned int height = avctx->height; // Real image height
  149. unsigned int mszh_dlen;
  150. unsigned char yq, y1q, uq, vq;
  151. int uqvq, ret;
  152. unsigned int mthread_inlen, mthread_outlen;
  153. unsigned int len = buf_size;
  154. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  155. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  156. return ret;
  157. }
  158. outptr = frame->data[0]; // Output image pointer
  159. /* Decompress frame */
  160. switch (avctx->codec_id) {
  161. case AV_CODEC_ID_MSZH:
  162. switch (c->compression) {
  163. case COMP_MSZH:
  164. if (c->flags & FLAG_MULTITHREAD) {
  165. mthread_inlen = AV_RL32(buf);
  166. mthread_inlen = FFMIN(mthread_inlen, len - 8);
  167. mthread_outlen = AV_RL32(buf + 4);
  168. mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
  169. mszh_dlen = mszh_decomp(buf + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
  170. if (mthread_outlen != mszh_dlen) {
  171. av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
  172. mthread_outlen, mszh_dlen);
  173. return AVERROR_INVALIDDATA;
  174. }
  175. mszh_dlen = mszh_decomp(buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
  176. c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
  177. if (mthread_outlen != mszh_dlen) {
  178. av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
  179. mthread_outlen, mszh_dlen);
  180. return AVERROR_INVALIDDATA;
  181. }
  182. encoded = c->decomp_buf;
  183. len = c->decomp_size;
  184. } else {
  185. mszh_dlen = mszh_decomp(buf, len, c->decomp_buf, c->decomp_size);
  186. if (c->decomp_size != mszh_dlen) {
  187. av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
  188. c->decomp_size, mszh_dlen);
  189. return AVERROR_INVALIDDATA;
  190. }
  191. encoded = c->decomp_buf;
  192. len = mszh_dlen;
  193. }
  194. break;
  195. case COMP_MSZH_NOCOMP: {
  196. int bppx2;
  197. switch (c->imgtype) {
  198. case IMGTYPE_YUV111:
  199. case IMGTYPE_RGB24:
  200. bppx2 = 6;
  201. break;
  202. case IMGTYPE_YUV422:
  203. case IMGTYPE_YUV211:
  204. bppx2 = 4;
  205. break;
  206. case IMGTYPE_YUV411:
  207. case IMGTYPE_YUV420:
  208. bppx2 = 3;
  209. break;
  210. default:
  211. bppx2 = 0; // will error out below
  212. break;
  213. }
  214. if (len < ((width * height * bppx2) >> 1))
  215. return AVERROR_INVALIDDATA;
  216. break;
  217. }
  218. default:
  219. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
  220. return AVERROR_INVALIDDATA;
  221. }
  222. break;
  223. #if CONFIG_ZLIB_DECODER
  224. case AV_CODEC_ID_ZLIB:
  225. /* Using the original dll with normal compression (-1) and RGB format
  226. * gives a file with ZLIB fourcc, but frame is really uncompressed.
  227. * To be sure that's true check also frame size */
  228. if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
  229. len == width * height * 3) {
  230. if (c->flags & FLAG_PNGFILTER) {
  231. memcpy(c->decomp_buf, buf, len);
  232. encoded = c->decomp_buf;
  233. } else {
  234. break;
  235. }
  236. } else if (c->flags & FLAG_MULTITHREAD) {
  237. mthread_inlen = AV_RL32(buf);
  238. mthread_inlen = FFMIN(mthread_inlen, len - 8);
  239. mthread_outlen = AV_RL32(buf + 4);
  240. mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
  241. ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen);
  242. if (ret < 0) return ret;
  243. ret = zlib_decomp(avctx, buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
  244. mthread_outlen, mthread_outlen);
  245. if (ret < 0) return ret;
  246. } else {
  247. int ret = zlib_decomp(avctx, buf, len, 0, c->decomp_size);
  248. if (ret < 0) return ret;
  249. }
  250. encoded = c->decomp_buf;
  251. len = c->decomp_size;
  252. break;
  253. #endif
  254. default:
  255. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
  256. return AVERROR_INVALIDDATA;
  257. }
  258. /* Apply PNG filter */
  259. if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
  260. switch (c->imgtype) {
  261. case IMGTYPE_YUV111:
  262. case IMGTYPE_RGB24:
  263. for (row = 0; row < height; row++) {
  264. pixel_ptr = row * width * 3;
  265. yq = encoded[pixel_ptr++];
  266. uqvq = AV_RL16(encoded+pixel_ptr);
  267. pixel_ptr += 2;
  268. for (col = 1; col < width; col++) {
  269. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  270. uqvq -= AV_RL16(encoded+pixel_ptr+1);
  271. AV_WL16(encoded+pixel_ptr+1, uqvq);
  272. pixel_ptr += 3;
  273. }
  274. }
  275. break;
  276. case IMGTYPE_YUV422:
  277. for (row = 0; row < height; row++) {
  278. pixel_ptr = row * width * 2;
  279. yq = uq = vq =0;
  280. for (col = 0; col < width/4; col++) {
  281. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  282. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  283. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  284. encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
  285. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  286. encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
  287. encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
  288. encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
  289. pixel_ptr += 8;
  290. }
  291. }
  292. break;
  293. case IMGTYPE_YUV411:
  294. for (row = 0; row < height; row++) {
  295. pixel_ptr = row * width / 2 * 3;
  296. yq = uq = vq =0;
  297. for (col = 0; col < width/4; col++) {
  298. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  299. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  300. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  301. encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
  302. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  303. encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
  304. pixel_ptr += 6;
  305. }
  306. }
  307. break;
  308. case IMGTYPE_YUV211:
  309. for (row = 0; row < height; row++) {
  310. pixel_ptr = row * width * 2;
  311. yq = uq = vq =0;
  312. for (col = 0; col < width/2; col++) {
  313. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  314. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  315. encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
  316. encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
  317. pixel_ptr += 4;
  318. }
  319. }
  320. break;
  321. case IMGTYPE_YUV420:
  322. for (row = 0; row < height/2; row++) {
  323. pixel_ptr = row * width * 3;
  324. yq = y1q = uq = vq =0;
  325. for (col = 0; col < width/2; col++) {
  326. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  327. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  328. encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
  329. encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
  330. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  331. encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
  332. pixel_ptr += 6;
  333. }
  334. }
  335. break;
  336. default:
  337. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
  338. return AVERROR_INVALIDDATA;
  339. }
  340. }
  341. /* Convert colorspace */
  342. y_out = frame->data[0] + (height - 1) * frame->linesize[0];
  343. u_out = frame->data[1] + (height - 1) * frame->linesize[1];
  344. v_out = frame->data[2] + (height - 1) * frame->linesize[2];
  345. switch (c->imgtype) {
  346. case IMGTYPE_YUV111:
  347. for (row = 0; row < height; row++) {
  348. for (col = 0; col < width; col++) {
  349. y_out[col] = *encoded++;
  350. u_out[col] = *encoded++ + 128;
  351. v_out[col] = *encoded++ + 128;
  352. }
  353. y_out -= frame->linesize[0];
  354. u_out -= frame->linesize[1];
  355. v_out -= frame->linesize[2];
  356. }
  357. break;
  358. case IMGTYPE_YUV422:
  359. for (row = 0; row < height; row++) {
  360. for (col = 0; col < width - 3; col += 4) {
  361. memcpy(y_out + col, encoded, 4);
  362. encoded += 4;
  363. u_out[ col >> 1 ] = *encoded++ + 128;
  364. u_out[(col >> 1) + 1] = *encoded++ + 128;
  365. v_out[ col >> 1 ] = *encoded++ + 128;
  366. v_out[(col >> 1) + 1] = *encoded++ + 128;
  367. }
  368. y_out -= frame->linesize[0];
  369. u_out -= frame->linesize[1];
  370. v_out -= frame->linesize[2];
  371. }
  372. break;
  373. case IMGTYPE_RGB24:
  374. for (row = height - 1; row >= 0; row--) {
  375. pixel_ptr = row * frame->linesize[0];
  376. memcpy(outptr + pixel_ptr, encoded, 3 * width);
  377. encoded += 3 * width;
  378. }
  379. break;
  380. case IMGTYPE_YUV411:
  381. for (row = 0; row < height; row++) {
  382. for (col = 0; col < width - 3; col += 4) {
  383. memcpy(y_out + col, encoded, 4);
  384. encoded += 4;
  385. u_out[col >> 2] = *encoded++ + 128;
  386. v_out[col >> 2] = *encoded++ + 128;
  387. }
  388. y_out -= frame->linesize[0];
  389. u_out -= frame->linesize[1];
  390. v_out -= frame->linesize[2];
  391. }
  392. break;
  393. case IMGTYPE_YUV211:
  394. for (row = 0; row < height; row++) {
  395. for (col = 0; col < width - 1; col += 2) {
  396. memcpy(y_out + col, encoded, 2);
  397. encoded += 2;
  398. u_out[col >> 1] = *encoded++ + 128;
  399. v_out[col >> 1] = *encoded++ + 128;
  400. }
  401. y_out -= frame->linesize[0];
  402. u_out -= frame->linesize[1];
  403. v_out -= frame->linesize[2];
  404. }
  405. break;
  406. case IMGTYPE_YUV420:
  407. u_out = frame->data[1] + ((height >> 1) - 1) * frame->linesize[1];
  408. v_out = frame->data[2] + ((height >> 1) - 1) * frame->linesize[2];
  409. for (row = 0; row < height - 1; row += 2) {
  410. for (col = 0; col < width - 1; col += 2) {
  411. memcpy(y_out + col, encoded, 2);
  412. encoded += 2;
  413. memcpy(y_out + col - frame->linesize[0], encoded, 2);
  414. encoded += 2;
  415. u_out[col >> 1] = *encoded++ + 128;
  416. v_out[col >> 1] = *encoded++ + 128;
  417. }
  418. y_out -= frame->linesize[0] << 1;
  419. u_out -= frame->linesize[1];
  420. v_out -= frame->linesize[2];
  421. }
  422. break;
  423. default:
  424. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
  425. return AVERROR_INVALIDDATA;
  426. }
  427. *got_frame = 1;
  428. /* always report that the buffer was completely consumed */
  429. return buf_size;
  430. }
  431. static av_cold int decode_init(AVCodecContext *avctx)
  432. {
  433. LclDecContext * const c = avctx->priv_data;
  434. unsigned int basesize = avctx->width * avctx->height;
  435. unsigned int max_basesize = FFALIGN(avctx->width, 4) *
  436. FFALIGN(avctx->height, 4);
  437. unsigned int max_decomp_size;
  438. if (avctx->extradata_size < 8) {
  439. av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
  440. return AVERROR_INVALIDDATA;
  441. }
  442. /* Check codec type */
  443. if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
  444. (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
  445. av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
  446. }
  447. /* Detect image type */
  448. switch (c->imgtype = avctx->extradata[4]) {
  449. case IMGTYPE_YUV111:
  450. c->decomp_size = basesize * 3;
  451. max_decomp_size = max_basesize * 3;
  452. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  453. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
  454. break;
  455. case IMGTYPE_YUV422:
  456. c->decomp_size = basesize * 2;
  457. max_decomp_size = max_basesize * 2;
  458. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  459. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
  460. break;
  461. case IMGTYPE_RGB24:
  462. c->decomp_size = basesize * 3;
  463. max_decomp_size = max_basesize * 3;
  464. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  465. av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
  466. break;
  467. case IMGTYPE_YUV411:
  468. c->decomp_size = basesize / 2 * 3;
  469. max_decomp_size = max_basesize / 2 * 3;
  470. avctx->pix_fmt = AV_PIX_FMT_YUV411P;
  471. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
  472. break;
  473. case IMGTYPE_YUV211:
  474. c->decomp_size = basesize * 2;
  475. max_decomp_size = max_basesize * 2;
  476. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  477. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
  478. break;
  479. case IMGTYPE_YUV420:
  480. c->decomp_size = basesize / 2 * 3;
  481. max_decomp_size = max_basesize / 2 * 3;
  482. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  483. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
  484. break;
  485. default:
  486. av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
  487. return AVERROR_INVALIDDATA;
  488. }
  489. /* Detect compression method */
  490. c->compression = (int8_t)avctx->extradata[5];
  491. switch (avctx->codec_id) {
  492. case AV_CODEC_ID_MSZH:
  493. switch (c->compression) {
  494. case COMP_MSZH:
  495. av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
  496. break;
  497. case COMP_MSZH_NOCOMP:
  498. c->decomp_size = 0;
  499. av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
  500. break;
  501. default:
  502. av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
  503. return AVERROR_INVALIDDATA;
  504. }
  505. break;
  506. #if CONFIG_ZLIB_DECODER
  507. case AV_CODEC_ID_ZLIB:
  508. switch (c->compression) {
  509. case COMP_ZLIB_HISPEED:
  510. av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
  511. break;
  512. case COMP_ZLIB_HICOMP:
  513. av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
  514. break;
  515. case COMP_ZLIB_NORMAL:
  516. av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
  517. break;
  518. default:
  519. if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
  520. av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
  521. return AVERROR_INVALIDDATA;
  522. }
  523. av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
  524. }
  525. break;
  526. #endif
  527. default:
  528. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
  529. return AVERROR_INVALIDDATA;
  530. }
  531. /* Allocate decompression buffer */
  532. if (c->decomp_size) {
  533. if (!(c->decomp_buf = av_malloc(max_decomp_size))) {
  534. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  535. return AVERROR(ENOMEM);
  536. }
  537. }
  538. /* Detect flags */
  539. c->flags = avctx->extradata[6];
  540. if (c->flags & FLAG_MULTITHREAD)
  541. av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
  542. if (c->flags & FLAG_NULLFRAME)
  543. av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
  544. if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
  545. av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
  546. if (c->flags & FLAGMASK_UNUSED)
  547. av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
  548. /* If needed init zlib */
  549. #if CONFIG_ZLIB_DECODER
  550. if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
  551. int zret;
  552. c->zstream.zalloc = Z_NULL;
  553. c->zstream.zfree = Z_NULL;
  554. c->zstream.opaque = Z_NULL;
  555. zret = inflateInit(&c->zstream);
  556. if (zret != Z_OK) {
  557. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  558. av_freep(&c->decomp_buf);
  559. return AVERROR_UNKNOWN;
  560. }
  561. }
  562. #endif
  563. return 0;
  564. }
  565. static av_cold int decode_end(AVCodecContext *avctx)
  566. {
  567. LclDecContext * const c = avctx->priv_data;
  568. av_freep(&c->decomp_buf);
  569. #if CONFIG_ZLIB_DECODER
  570. if (avctx->codec_id == AV_CODEC_ID_ZLIB)
  571. inflateEnd(&c->zstream);
  572. #endif
  573. return 0;
  574. }
  575. #if CONFIG_MSZH_DECODER
  576. AVCodec ff_mszh_decoder = {
  577. .name = "mszh",
  578. .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
  579. .type = AVMEDIA_TYPE_VIDEO,
  580. .id = AV_CODEC_ID_MSZH,
  581. .priv_data_size = sizeof(LclDecContext),
  582. .init = decode_init,
  583. .close = decode_end,
  584. .decode = decode_frame,
  585. .capabilities = AV_CODEC_CAP_DR1,
  586. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  587. };
  588. #endif
  589. #if CONFIG_ZLIB_DECODER
  590. AVCodec ff_zlib_decoder = {
  591. .name = "zlib",
  592. .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
  593. .type = AVMEDIA_TYPE_VIDEO,
  594. .id = AV_CODEC_ID_ZLIB,
  595. .priv_data_size = sizeof(LclDecContext),
  596. .init = decode_init,
  597. .close = decode_end,
  598. .decode = decode_frame,
  599. .capabilities = AV_CODEC_CAP_DR1,
  600. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  601. };
  602. #endif