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.

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