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.

634 lines
21KB

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