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.

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