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.

641 lines
22KB

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