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