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.

666 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 -1;
  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 -1;
  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 -1;
  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;
  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(ff_get_buffer(avctx, &c->pic) < 0){
  169. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  170. return -1;
  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 -1;
  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 -1;
  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 -1;
  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 -1;
  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. int ret;
  252. mthread_inlen = AV_RL32(encoded);
  253. mthread_inlen = FFMIN(mthread_inlen, len - 8);
  254. mthread_outlen = AV_RL32(encoded+4);
  255. mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
  256. ret = zlib_decomp(avctx, encoded + 8, mthread_inlen, 0, mthread_outlen);
  257. if (ret < 0) return ret;
  258. ret = zlib_decomp(avctx, encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
  259. mthread_outlen, mthread_outlen);
  260. if (ret < 0) return ret;
  261. } else {
  262. int ret = zlib_decomp(avctx, encoded, len, 0, c->decomp_size);
  263. if (ret < 0) return ret;
  264. }
  265. encoded = c->decomp_buf;
  266. len = c->decomp_size;
  267. break;
  268. #endif
  269. default:
  270. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
  271. return -1;
  272. }
  273. /* Apply PNG filter */
  274. if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
  275. switch (c->imgtype) {
  276. case IMGTYPE_YUV111:
  277. case IMGTYPE_RGB24:
  278. for (row = 0; row < height; row++) {
  279. pixel_ptr = row * width * 3;
  280. yq = encoded[pixel_ptr++];
  281. uqvq = AV_RL16(encoded+pixel_ptr);
  282. pixel_ptr += 2;
  283. for (col = 1; col < width; col++) {
  284. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  285. uqvq -= AV_RL16(encoded+pixel_ptr+1);
  286. AV_WL16(encoded+pixel_ptr+1, uqvq);
  287. pixel_ptr += 3;
  288. }
  289. }
  290. break;
  291. case IMGTYPE_YUV422:
  292. for (row = 0; row < height; row++) {
  293. pixel_ptr = row * width * 2;
  294. yq = uq = vq =0;
  295. for (col = 0; col < width/4; col++) {
  296. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  297. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  298. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  299. encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
  300. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  301. encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
  302. encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
  303. encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
  304. pixel_ptr += 8;
  305. }
  306. }
  307. break;
  308. case IMGTYPE_YUV411:
  309. for (row = 0; row < height; row++) {
  310. pixel_ptr = row * width / 2 * 3;
  311. yq = uq = vq =0;
  312. for (col = 0; col < width/4; col++) {
  313. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  314. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  315. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  316. encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
  317. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  318. encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
  319. pixel_ptr += 6;
  320. }
  321. }
  322. break;
  323. case IMGTYPE_YUV211:
  324. for (row = 0; row < height; row++) {
  325. pixel_ptr = row * width * 2;
  326. yq = uq = vq =0;
  327. for (col = 0; col < width/2; col++) {
  328. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  329. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  330. encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
  331. encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
  332. pixel_ptr += 4;
  333. }
  334. }
  335. break;
  336. case IMGTYPE_YUV420:
  337. for (row = 0; row < height/2; row++) {
  338. pixel_ptr = row * width * 3;
  339. yq = y1q = uq = vq =0;
  340. for (col = 0; col < width/2; col++) {
  341. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  342. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  343. encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
  344. encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
  345. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  346. encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
  347. pixel_ptr += 6;
  348. }
  349. }
  350. break;
  351. default:
  352. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
  353. return -1;
  354. }
  355. }
  356. /* Convert colorspace */
  357. y_out = c->pic.data[0] + (height - 1) * c->pic.linesize[0];
  358. u_out = c->pic.data[1] + (height - 1) * c->pic.linesize[1];
  359. v_out = c->pic.data[2] + (height - 1) * c->pic.linesize[2];
  360. switch (c->imgtype) {
  361. case IMGTYPE_YUV111:
  362. for (row = 0; row < height; row++) {
  363. for (col = 0; col < width; col++) {
  364. y_out[col] = *encoded++;
  365. u_out[col] = *encoded++ + 128;
  366. v_out[col] = *encoded++ + 128;
  367. }
  368. y_out -= c->pic.linesize[0];
  369. u_out -= c->pic.linesize[1];
  370. v_out -= c->pic.linesize[2];
  371. }
  372. break;
  373. case IMGTYPE_YUV422:
  374. for (row = 0; row < height; row++) {
  375. for (col = 0; col < width - 3; col += 4) {
  376. memcpy(y_out + col, encoded, 4);
  377. encoded += 4;
  378. u_out[ col >> 1 ] = *encoded++ + 128;
  379. u_out[(col >> 1) + 1] = *encoded++ + 128;
  380. v_out[ col >> 1 ] = *encoded++ + 128;
  381. v_out[(col >> 1) + 1] = *encoded++ + 128;
  382. }
  383. y_out -= c->pic.linesize[0];
  384. u_out -= c->pic.linesize[1];
  385. v_out -= c->pic.linesize[2];
  386. }
  387. break;
  388. case IMGTYPE_RGB24:
  389. for (row = height - 1; row >= 0; row--) {
  390. pixel_ptr = row * c->pic.linesize[0];
  391. memcpy(outptr + pixel_ptr, encoded, 3 * width);
  392. encoded += 3 * width;
  393. }
  394. break;
  395. case IMGTYPE_YUV411:
  396. for (row = 0; row < height; row++) {
  397. for (col = 0; col < width - 3; col += 4) {
  398. memcpy(y_out + col, encoded, 4);
  399. encoded += 4;
  400. u_out[col >> 2] = *encoded++ + 128;
  401. v_out[col >> 2] = *encoded++ + 128;
  402. }
  403. y_out -= c->pic.linesize[0];
  404. u_out -= c->pic.linesize[1];
  405. v_out -= c->pic.linesize[2];
  406. }
  407. break;
  408. case IMGTYPE_YUV211:
  409. for (row = 0; row < height; row++) {
  410. for (col = 0; col < width - 1; col += 2) {
  411. memcpy(y_out + col, encoded, 2);
  412. encoded += 2;
  413. u_out[col >> 1] = *encoded++ + 128;
  414. v_out[col >> 1] = *encoded++ + 128;
  415. }
  416. y_out -= c->pic.linesize[0];
  417. u_out -= c->pic.linesize[1];
  418. v_out -= c->pic.linesize[2];
  419. }
  420. break;
  421. case IMGTYPE_YUV420:
  422. u_out = c->pic.data[1] + ((height >> 1) - 1) * c->pic.linesize[1];
  423. v_out = c->pic.data[2] + ((height >> 1) - 1) * c->pic.linesize[2];
  424. for (row = 0; row < height - 1; row += 2) {
  425. for (col = 0; col < width - 1; col += 2) {
  426. memcpy(y_out + col, encoded, 2);
  427. encoded += 2;
  428. memcpy(y_out + col - c->pic.linesize[0], encoded, 2);
  429. encoded += 2;
  430. u_out[col >> 1] = *encoded++ + 128;
  431. v_out[col >> 1] = *encoded++ + 128;
  432. }
  433. y_out -= c->pic.linesize[0] << 1;
  434. u_out -= c->pic.linesize[1];
  435. v_out -= c->pic.linesize[2];
  436. }
  437. break;
  438. default:
  439. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
  440. return -1;
  441. }
  442. *got_frame = 1;
  443. *(AVFrame*)data = c->pic;
  444. /* always report that the buffer was completely consumed */
  445. return buf_size;
  446. }
  447. /*
  448. *
  449. * Init lcl decoder
  450. *
  451. */
  452. static av_cold int decode_init(AVCodecContext *avctx)
  453. {
  454. LclDecContext * const c = avctx->priv_data;
  455. unsigned int basesize = avctx->width * avctx->height;
  456. unsigned int max_basesize = FFALIGN(avctx->width, 4) *
  457. FFALIGN(avctx->height, 4);
  458. unsigned int max_decomp_size;
  459. if (avctx->extradata_size < 8) {
  460. av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
  461. return AVERROR_INVALIDDATA;
  462. }
  463. /* Check codec type */
  464. if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
  465. (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
  466. av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
  467. }
  468. /* Detect image type */
  469. switch (c->imgtype = avctx->extradata[4]) {
  470. case IMGTYPE_YUV111:
  471. c->decomp_size = basesize * 3;
  472. max_decomp_size = max_basesize * 3;
  473. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  474. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
  475. break;
  476. case IMGTYPE_YUV422:
  477. c->decomp_size = basesize * 2;
  478. max_decomp_size = max_basesize * 2;
  479. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  480. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
  481. break;
  482. case IMGTYPE_RGB24:
  483. c->decomp_size = basesize * 3;
  484. max_decomp_size = max_basesize * 3;
  485. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  486. av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
  487. break;
  488. case IMGTYPE_YUV411:
  489. c->decomp_size = basesize / 2 * 3;
  490. max_decomp_size = max_basesize / 2 * 3;
  491. avctx->pix_fmt = AV_PIX_FMT_YUV411P;
  492. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
  493. break;
  494. case IMGTYPE_YUV211:
  495. c->decomp_size = basesize * 2;
  496. max_decomp_size = max_basesize * 2;
  497. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  498. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
  499. break;
  500. case IMGTYPE_YUV420:
  501. c->decomp_size = basesize / 2 * 3;
  502. max_decomp_size = max_basesize / 2 * 3;
  503. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  504. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
  505. break;
  506. default:
  507. av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
  508. return AVERROR_INVALIDDATA;
  509. }
  510. /* Detect compression method */
  511. c->compression = (int8_t)avctx->extradata[5];
  512. switch (avctx->codec_id) {
  513. case AV_CODEC_ID_MSZH:
  514. switch (c->compression) {
  515. case COMP_MSZH:
  516. av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
  517. break;
  518. case COMP_MSZH_NOCOMP:
  519. c->decomp_size = 0;
  520. av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
  521. break;
  522. default:
  523. av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
  524. return AVERROR_INVALIDDATA;
  525. }
  526. break;
  527. #if CONFIG_ZLIB_DECODER
  528. case AV_CODEC_ID_ZLIB:
  529. switch (c->compression) {
  530. case COMP_ZLIB_HISPEED:
  531. av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
  532. break;
  533. case COMP_ZLIB_HICOMP:
  534. av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
  535. break;
  536. case COMP_ZLIB_NORMAL:
  537. av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
  538. break;
  539. default:
  540. if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
  541. av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
  542. return AVERROR_INVALIDDATA;
  543. }
  544. av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
  545. }
  546. break;
  547. #endif
  548. default:
  549. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
  550. return AVERROR_INVALIDDATA;
  551. }
  552. /* Allocate decompression buffer */
  553. if (c->decomp_size) {
  554. if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
  555. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  556. return AVERROR(ENOMEM);
  557. }
  558. }
  559. /* Detect flags */
  560. c->flags = avctx->extradata[6];
  561. if (c->flags & FLAG_MULTITHREAD)
  562. av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
  563. if (c->flags & FLAG_NULLFRAME)
  564. av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
  565. if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
  566. av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
  567. if (c->flags & FLAGMASK_UNUSED)
  568. av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
  569. /* If needed init zlib */
  570. #if CONFIG_ZLIB_DECODER
  571. if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
  572. int zret;
  573. c->zstream.zalloc = Z_NULL;
  574. c->zstream.zfree = Z_NULL;
  575. c->zstream.opaque = Z_NULL;
  576. zret = inflateInit(&c->zstream);
  577. if (zret != Z_OK) {
  578. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  579. av_freep(&c->decomp_buf);
  580. return AVERROR_UNKNOWN;
  581. }
  582. }
  583. #endif
  584. return 0;
  585. }
  586. /*
  587. *
  588. * Uninit lcl decoder
  589. *
  590. */
  591. static av_cold int decode_end(AVCodecContext *avctx)
  592. {
  593. LclDecContext * const c = avctx->priv_data;
  594. av_freep(&c->decomp_buf);
  595. if (c->pic.data[0])
  596. avctx->release_buffer(avctx, &c->pic);
  597. #if CONFIG_ZLIB_DECODER
  598. if (avctx->codec_id == AV_CODEC_ID_ZLIB)
  599. inflateEnd(&c->zstream);
  600. #endif
  601. return 0;
  602. }
  603. #if CONFIG_MSZH_DECODER
  604. AVCodec ff_mszh_decoder = {
  605. .name = "mszh",
  606. .type = AVMEDIA_TYPE_VIDEO,
  607. .id = AV_CODEC_ID_MSZH,
  608. .priv_data_size = sizeof(LclDecContext),
  609. .init = decode_init,
  610. .close = decode_end,
  611. .decode = decode_frame,
  612. .capabilities = CODEC_CAP_DR1,
  613. .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
  614. };
  615. #endif
  616. #if CONFIG_ZLIB_DECODER
  617. AVCodec ff_zlib_decoder = {
  618. .name = "zlib",
  619. .type = AVMEDIA_TYPE_VIDEO,
  620. .id = AV_CODEC_ID_ZLIB,
  621. .priv_data_size = sizeof(LclDecContext),
  622. .init = decode_init,
  623. .close = decode_end,
  624. .decode = decode_frame,
  625. .capabilities = CODEC_CAP_DR1,
  626. .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
  627. };
  628. #endif