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.

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