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.

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