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.

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