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.

655 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 int pixel_ptr;
  153. int row, col;
  154. unsigned char *encoded, *outptr;
  155. uint8_t *y_out, *u_out, *v_out;
  156. unsigned int width = avctx->width; // Real image width
  157. unsigned int height = avctx->height; // Real image height
  158. unsigned int mszh_dlen;
  159. unsigned char yq, y1q, uq, vq;
  160. int uqvq, ret;
  161. unsigned int mthread_inlen, mthread_outlen;
  162. unsigned int len = buf_size;
  163. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  164. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  165. return ret;
  166. }
  167. outptr = frame->data[0]; // Output image pointer
  168. /* Decompress frame */
  169. switch (avctx->codec_id) {
  170. case AV_CODEC_ID_MSZH:
  171. switch (c->compression) {
  172. case COMP_MSZH:
  173. if (c->flags & FLAG_MULTITHREAD) {
  174. mthread_inlen = AV_RL32(buf);
  175. mthread_inlen = FFMIN(mthread_inlen, len - 8);
  176. mthread_outlen = AV_RL32(buf + 4);
  177. mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
  178. mszh_dlen = mszh_decomp(buf + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
  179. if (mthread_outlen != mszh_dlen) {
  180. av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
  181. mthread_outlen, mszh_dlen);
  182. return AVERROR_INVALIDDATA;
  183. }
  184. mszh_dlen = mszh_decomp(buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
  185. c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
  186. if (mthread_outlen != mszh_dlen) {
  187. av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
  188. mthread_outlen, mszh_dlen);
  189. return AVERROR_INVALIDDATA;
  190. }
  191. encoded = c->decomp_buf;
  192. len = c->decomp_size;
  193. } else {
  194. mszh_dlen = mszh_decomp(buf, len, c->decomp_buf, c->decomp_size);
  195. if (c->decomp_size != mszh_dlen) {
  196. av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
  197. c->decomp_size, mszh_dlen);
  198. return AVERROR_INVALIDDATA;
  199. }
  200. encoded = c->decomp_buf;
  201. len = mszh_dlen;
  202. }
  203. break;
  204. case COMP_MSZH_NOCOMP: {
  205. int bppx2;
  206. switch (c->imgtype) {
  207. case IMGTYPE_YUV111:
  208. case IMGTYPE_RGB24:
  209. bppx2 = 6;
  210. break;
  211. case IMGTYPE_YUV422:
  212. case IMGTYPE_YUV211:
  213. bppx2 = 4;
  214. break;
  215. case IMGTYPE_YUV411:
  216. case IMGTYPE_YUV420:
  217. bppx2 = 3;
  218. break;
  219. default:
  220. bppx2 = 0; // will error out below
  221. break;
  222. }
  223. if (len < ((width * height * bppx2) >> 1))
  224. return AVERROR_INVALIDDATA;
  225. break;
  226. }
  227. default:
  228. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
  229. return AVERROR_INVALIDDATA;
  230. }
  231. break;
  232. #if CONFIG_ZLIB_DECODER
  233. case AV_CODEC_ID_ZLIB:
  234. /* Using the original dll with normal compression (-1) and RGB format
  235. * gives a file with ZLIB fourcc, but frame is really uncompressed.
  236. * To be sure that's true check also frame size */
  237. if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
  238. len == width * height * 3) {
  239. if (c->flags & FLAG_PNGFILTER) {
  240. memcpy(c->decomp_buf, buf, len);
  241. encoded = c->decomp_buf;
  242. } else {
  243. break;
  244. }
  245. } else if (c->flags & FLAG_MULTITHREAD) {
  246. mthread_inlen = AV_RL32(buf);
  247. mthread_inlen = FFMIN(mthread_inlen, len - 8);
  248. mthread_outlen = AV_RL32(buf + 4);
  249. mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
  250. ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen);
  251. if (ret < 0) return ret;
  252. ret = zlib_decomp(avctx, buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
  253. mthread_outlen, mthread_outlen);
  254. if (ret < 0) return ret;
  255. } else {
  256. int ret = zlib_decomp(avctx, buf, len, 0, c->decomp_size);
  257. if (ret < 0) return ret;
  258. }
  259. encoded = c->decomp_buf;
  260. len = c->decomp_size;
  261. break;
  262. #endif
  263. default:
  264. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
  265. return AVERROR_INVALIDDATA;
  266. }
  267. /* Apply PNG filter */
  268. if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
  269. switch (c->imgtype) {
  270. case IMGTYPE_YUV111:
  271. case IMGTYPE_RGB24:
  272. for (row = 0; row < height; row++) {
  273. pixel_ptr = row * width * 3;
  274. yq = encoded[pixel_ptr++];
  275. uqvq = AV_RL16(encoded+pixel_ptr);
  276. pixel_ptr += 2;
  277. for (col = 1; col < width; col++) {
  278. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  279. uqvq -= AV_RL16(encoded+pixel_ptr+1);
  280. AV_WL16(encoded+pixel_ptr+1, uqvq);
  281. pixel_ptr += 3;
  282. }
  283. }
  284. break;
  285. case IMGTYPE_YUV422:
  286. for (row = 0; row < height; row++) {
  287. pixel_ptr = row * width * 2;
  288. yq = uq = vq =0;
  289. for (col = 0; col < width/4; col++) {
  290. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  291. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  292. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  293. encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
  294. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  295. encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
  296. encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
  297. encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
  298. pixel_ptr += 8;
  299. }
  300. }
  301. break;
  302. case IMGTYPE_YUV411:
  303. for (row = 0; row < height; row++) {
  304. pixel_ptr = row * width / 2 * 3;
  305. yq = uq = vq =0;
  306. for (col = 0; col < width/4; col++) {
  307. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  308. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  309. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  310. encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
  311. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  312. encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
  313. pixel_ptr += 6;
  314. }
  315. }
  316. break;
  317. case IMGTYPE_YUV211:
  318. for (row = 0; row < height; row++) {
  319. pixel_ptr = row * width * 2;
  320. yq = uq = vq =0;
  321. for (col = 0; col < width/2; col++) {
  322. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  323. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  324. encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
  325. encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
  326. pixel_ptr += 4;
  327. }
  328. }
  329. break;
  330. case IMGTYPE_YUV420:
  331. for (row = 0; row < height/2; row++) {
  332. pixel_ptr = row * width * 3;
  333. yq = y1q = uq = vq =0;
  334. for (col = 0; col < width/2; col++) {
  335. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  336. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  337. encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
  338. encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
  339. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  340. encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
  341. pixel_ptr += 6;
  342. }
  343. }
  344. break;
  345. default:
  346. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
  347. return AVERROR_INVALIDDATA;
  348. }
  349. }
  350. /* Convert colorspace */
  351. y_out = frame->data[0] + (height - 1) * frame->linesize[0];
  352. u_out = frame->data[1] + (height - 1) * frame->linesize[1];
  353. v_out = frame->data[2] + (height - 1) * frame->linesize[2];
  354. switch (c->imgtype) {
  355. case IMGTYPE_YUV111:
  356. for (row = 0; row < height; row++) {
  357. for (col = 0; col < width; col++) {
  358. y_out[col] = *encoded++;
  359. u_out[col] = *encoded++ + 128;
  360. v_out[col] = *encoded++ + 128;
  361. }
  362. y_out -= frame->linesize[0];
  363. u_out -= frame->linesize[1];
  364. v_out -= frame->linesize[2];
  365. }
  366. break;
  367. case IMGTYPE_YUV422:
  368. for (row = 0; row < height; row++) {
  369. for (col = 0; col < width - 3; col += 4) {
  370. memcpy(y_out + col, encoded, 4);
  371. encoded += 4;
  372. u_out[ col >> 1 ] = *encoded++ + 128;
  373. u_out[(col >> 1) + 1] = *encoded++ + 128;
  374. v_out[ col >> 1 ] = *encoded++ + 128;
  375. v_out[(col >> 1) + 1] = *encoded++ + 128;
  376. }
  377. y_out -= frame->linesize[0];
  378. u_out -= frame->linesize[1];
  379. v_out -= frame->linesize[2];
  380. }
  381. break;
  382. case IMGTYPE_RGB24:
  383. for (row = height - 1; row >= 0; row--) {
  384. pixel_ptr = row * frame->linesize[0];
  385. memcpy(outptr + pixel_ptr, encoded, 3 * width);
  386. encoded += 3 * width;
  387. }
  388. break;
  389. case IMGTYPE_YUV411:
  390. for (row = 0; row < height; row++) {
  391. for (col = 0; col < width - 3; col += 4) {
  392. memcpy(y_out + col, encoded, 4);
  393. encoded += 4;
  394. u_out[col >> 2] = *encoded++ + 128;
  395. v_out[col >> 2] = *encoded++ + 128;
  396. }
  397. y_out -= frame->linesize[0];
  398. u_out -= frame->linesize[1];
  399. v_out -= frame->linesize[2];
  400. }
  401. break;
  402. case IMGTYPE_YUV211:
  403. for (row = 0; row < height; row++) {
  404. for (col = 0; col < width - 1; col += 2) {
  405. memcpy(y_out + col, encoded, 2);
  406. encoded += 2;
  407. u_out[col >> 1] = *encoded++ + 128;
  408. v_out[col >> 1] = *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_YUV420:
  416. u_out = frame->data[1] + ((height >> 1) - 1) * frame->linesize[1];
  417. v_out = frame->data[2] + ((height >> 1) - 1) * frame->linesize[2];
  418. for (row = 0; row < height - 1; row += 2) {
  419. for (col = 0; col < width - 1; col += 2) {
  420. memcpy(y_out + col, encoded, 2);
  421. encoded += 2;
  422. memcpy(y_out + col - frame->linesize[0], encoded, 2);
  423. encoded += 2;
  424. u_out[col >> 1] = *encoded++ + 128;
  425. v_out[col >> 1] = *encoded++ + 128;
  426. }
  427. y_out -= frame->linesize[0] << 1;
  428. u_out -= frame->linesize[1];
  429. v_out -= frame->linesize[2];
  430. }
  431. break;
  432. default:
  433. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
  434. return AVERROR_INVALIDDATA;
  435. }
  436. *got_frame = 1;
  437. /* always report that the buffer was completely consumed */
  438. return buf_size;
  439. }
  440. /*
  441. *
  442. * Init lcl decoder
  443. *
  444. */
  445. static av_cold int decode_init(AVCodecContext *avctx)
  446. {
  447. LclDecContext * const c = avctx->priv_data;
  448. unsigned int basesize = avctx->width * avctx->height;
  449. unsigned int max_basesize = FFALIGN(avctx->width, 4) *
  450. FFALIGN(avctx->height, 4);
  451. unsigned int max_decomp_size;
  452. if (avctx->extradata_size < 8) {
  453. av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
  454. return AVERROR_INVALIDDATA;
  455. }
  456. /* Check codec type */
  457. if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
  458. (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
  459. av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
  460. }
  461. /* Detect image type */
  462. switch (c->imgtype = avctx->extradata[4]) {
  463. case IMGTYPE_YUV111:
  464. c->decomp_size = basesize * 3;
  465. max_decomp_size = max_basesize * 3;
  466. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  467. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
  468. break;
  469. case IMGTYPE_YUV422:
  470. c->decomp_size = basesize * 2;
  471. max_decomp_size = max_basesize * 2;
  472. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  473. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
  474. break;
  475. case IMGTYPE_RGB24:
  476. c->decomp_size = basesize * 3;
  477. max_decomp_size = max_basesize * 3;
  478. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  479. av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
  480. break;
  481. case IMGTYPE_YUV411:
  482. c->decomp_size = basesize / 2 * 3;
  483. max_decomp_size = max_basesize / 2 * 3;
  484. avctx->pix_fmt = AV_PIX_FMT_YUV411P;
  485. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
  486. break;
  487. case IMGTYPE_YUV211:
  488. c->decomp_size = basesize * 2;
  489. max_decomp_size = max_basesize * 2;
  490. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  491. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
  492. break;
  493. case IMGTYPE_YUV420:
  494. c->decomp_size = basesize / 2 * 3;
  495. max_decomp_size = max_basesize / 2 * 3;
  496. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  497. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
  498. break;
  499. default:
  500. av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
  501. return AVERROR_INVALIDDATA;
  502. }
  503. /* Detect compression method */
  504. c->compression = (int8_t)avctx->extradata[5];
  505. switch (avctx->codec_id) {
  506. case AV_CODEC_ID_MSZH:
  507. switch (c->compression) {
  508. case COMP_MSZH:
  509. av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
  510. break;
  511. case COMP_MSZH_NOCOMP:
  512. c->decomp_size = 0;
  513. av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
  514. break;
  515. default:
  516. av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
  517. return AVERROR_INVALIDDATA;
  518. }
  519. break;
  520. #if CONFIG_ZLIB_DECODER
  521. case AV_CODEC_ID_ZLIB:
  522. switch (c->compression) {
  523. case COMP_ZLIB_HISPEED:
  524. av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
  525. break;
  526. case COMP_ZLIB_HICOMP:
  527. av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
  528. break;
  529. case COMP_ZLIB_NORMAL:
  530. av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
  531. break;
  532. default:
  533. if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
  534. av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
  535. return AVERROR_INVALIDDATA;
  536. }
  537. av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
  538. }
  539. break;
  540. #endif
  541. default:
  542. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
  543. return AVERROR_INVALIDDATA;
  544. }
  545. /* Allocate decompression buffer */
  546. if (c->decomp_size) {
  547. if (!(c->decomp_buf = av_malloc(max_decomp_size))) {
  548. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  549. return AVERROR(ENOMEM);
  550. }
  551. }
  552. /* Detect flags */
  553. c->flags = avctx->extradata[6];
  554. if (c->flags & FLAG_MULTITHREAD)
  555. av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
  556. if (c->flags & FLAG_NULLFRAME)
  557. av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
  558. if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
  559. av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
  560. if (c->flags & FLAGMASK_UNUSED)
  561. av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
  562. /* If needed init zlib */
  563. #if CONFIG_ZLIB_DECODER
  564. if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
  565. int zret;
  566. c->zstream.zalloc = Z_NULL;
  567. c->zstream.zfree = Z_NULL;
  568. c->zstream.opaque = Z_NULL;
  569. zret = inflateInit(&c->zstream);
  570. if (zret != Z_OK) {
  571. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  572. av_freep(&c->decomp_buf);
  573. return AVERROR_UNKNOWN;
  574. }
  575. }
  576. #endif
  577. return 0;
  578. }
  579. /*
  580. *
  581. * Uninit lcl decoder
  582. *
  583. */
  584. static av_cold int decode_end(AVCodecContext *avctx)
  585. {
  586. LclDecContext * const c = avctx->priv_data;
  587. av_freep(&c->decomp_buf);
  588. #if CONFIG_ZLIB_DECODER
  589. if (avctx->codec_id == AV_CODEC_ID_ZLIB)
  590. inflateEnd(&c->zstream);
  591. #endif
  592. return 0;
  593. }
  594. #if CONFIG_MSZH_DECODER
  595. AVCodec ff_mszh_decoder = {
  596. .name = "mszh",
  597. .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
  598. .type = AVMEDIA_TYPE_VIDEO,
  599. .id = AV_CODEC_ID_MSZH,
  600. .priv_data_size = sizeof(LclDecContext),
  601. .init = decode_init,
  602. .close = decode_end,
  603. .decode = decode_frame,
  604. .capabilities = CODEC_CAP_DR1,
  605. };
  606. #endif
  607. #if CONFIG_ZLIB_DECODER
  608. AVCodec ff_zlib_decoder = {
  609. .name = "zlib",
  610. .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
  611. .type = AVMEDIA_TYPE_VIDEO,
  612. .id = AV_CODEC_ID_ZLIB,
  613. .priv_data_size = sizeof(LclDecContext),
  614. .init = decode_init,
  615. .close = decode_end,
  616. .decode = decode_frame,
  617. .capabilities = CODEC_CAP_DR1,
  618. };
  619. #endif