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.

671 lines
23KB

  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 AV_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. int bppx2;
  216. switch (c->imgtype) {
  217. case IMGTYPE_YUV111:
  218. case IMGTYPE_RGB24:
  219. bppx2 = 6;
  220. break;
  221. case IMGTYPE_YUV422:
  222. case IMGTYPE_YUV211:
  223. bppx2 = 4;
  224. break;
  225. case IMGTYPE_YUV411:
  226. case IMGTYPE_YUV420:
  227. bppx2 = 3;
  228. break;
  229. default:
  230. bppx2 = 0; // will error out below
  231. break;
  232. }
  233. if (len < ((width * height * bppx2) >> 1))
  234. return AVERROR_INVALIDDATA;
  235. break;
  236. }
  237. default:
  238. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
  239. return -1;
  240. }
  241. break;
  242. #if CONFIG_ZLIB_DECODER
  243. case AV_CODEC_ID_ZLIB:
  244. /* Using the original dll with normal compression (-1) and RGB format
  245. * gives a file with ZLIB fourcc, but frame is really uncompressed.
  246. * To be sure that's true check also frame size */
  247. if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
  248. len == width * height * 3) {
  249. if (c->flags & FLAG_PNGFILTER) {
  250. memcpy(c->decomp_buf, encoded, len);
  251. encoded = c->decomp_buf;
  252. } else {
  253. break;
  254. }
  255. } else if (c->flags & FLAG_MULTITHREAD) {
  256. int ret;
  257. mthread_inlen = AV_RL32(encoded);
  258. mthread_inlen = FFMIN(mthread_inlen, len - 8);
  259. mthread_outlen = AV_RL32(encoded+4);
  260. mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
  261. ret = zlib_decomp(avctx, encoded + 8, mthread_inlen, 0, mthread_outlen);
  262. if (ret < 0) return ret;
  263. ret = zlib_decomp(avctx, encoded + 8 + mthread_inlen, len - 8 - mthread_inlen,
  264. mthread_outlen, mthread_outlen);
  265. if (ret < 0) return ret;
  266. } else {
  267. int ret = zlib_decomp(avctx, encoded, len, 0, c->decomp_size);
  268. if (ret < 0) return ret;
  269. }
  270. encoded = c->decomp_buf;
  271. len = c->decomp_size;
  272. break;
  273. #endif
  274. default:
  275. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
  276. return -1;
  277. }
  278. /* Apply PNG filter */
  279. if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
  280. switch (c->imgtype) {
  281. case IMGTYPE_YUV111:
  282. case IMGTYPE_RGB24:
  283. for (row = 0; row < height; row++) {
  284. pixel_ptr = row * width * 3;
  285. yq = encoded[pixel_ptr++];
  286. uqvq = AV_RL16(encoded+pixel_ptr);
  287. pixel_ptr += 2;
  288. for (col = 1; col < width; col++) {
  289. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  290. uqvq -= AV_RL16(encoded+pixel_ptr+1);
  291. AV_WL16(encoded+pixel_ptr+1, uqvq);
  292. pixel_ptr += 3;
  293. }
  294. }
  295. break;
  296. case IMGTYPE_YUV422:
  297. for (row = 0; row < height; row++) {
  298. pixel_ptr = row * width * 2;
  299. yq = uq = vq =0;
  300. for (col = 0; col < width/4; col++) {
  301. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  302. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  303. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  304. encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
  305. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  306. encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
  307. encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
  308. encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
  309. pixel_ptr += 8;
  310. }
  311. }
  312. break;
  313. case IMGTYPE_YUV411:
  314. for (row = 0; row < height; row++) {
  315. pixel_ptr = row * width / 2 * 3;
  316. yq = uq = vq =0;
  317. for (col = 0; col < width/4; col++) {
  318. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  319. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  320. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  321. encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
  322. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  323. encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
  324. pixel_ptr += 6;
  325. }
  326. }
  327. break;
  328. case IMGTYPE_YUV211:
  329. for (row = 0; row < height; row++) {
  330. pixel_ptr = row * width * 2;
  331. yq = uq = vq =0;
  332. for (col = 0; col < width/2; col++) {
  333. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  334. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  335. encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
  336. encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
  337. pixel_ptr += 4;
  338. }
  339. }
  340. break;
  341. case IMGTYPE_YUV420:
  342. for (row = 0; row < height/2; row++) {
  343. pixel_ptr = row * width * 3;
  344. yq = y1q = uq = vq =0;
  345. for (col = 0; col < width/2; col++) {
  346. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  347. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  348. encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
  349. encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
  350. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  351. encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
  352. pixel_ptr += 6;
  353. }
  354. }
  355. break;
  356. default:
  357. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
  358. return -1;
  359. }
  360. }
  361. /* Convert colorspace */
  362. y_out = c->pic.data[0] + (height - 1) * c->pic.linesize[0];
  363. u_out = c->pic.data[1] + (height - 1) * c->pic.linesize[1];
  364. v_out = c->pic.data[2] + (height - 1) * c->pic.linesize[2];
  365. switch (c->imgtype) {
  366. case IMGTYPE_YUV111:
  367. for (row = 0; row < height; row++) {
  368. for (col = 0; col < width; col++) {
  369. y_out[col] = *encoded++;
  370. u_out[col] = *encoded++ + 128;
  371. v_out[col] = *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_YUV422:
  379. for (row = 0; row < height; row++) {
  380. for (col = 0; col < width - 3; col += 4) {
  381. memcpy(y_out + col, encoded, 4);
  382. encoded += 4;
  383. u_out[ col >> 1 ] = *encoded++ + 128;
  384. u_out[(col >> 1) + 1] = *encoded++ + 128;
  385. v_out[ col >> 1 ] = *encoded++ + 128;
  386. v_out[(col >> 1) + 1] = *encoded++ + 128;
  387. }
  388. y_out -= c->pic.linesize[0];
  389. u_out -= c->pic.linesize[1];
  390. v_out -= c->pic.linesize[2];
  391. }
  392. break;
  393. case IMGTYPE_RGB24:
  394. for (row = height - 1; row >= 0; row--) {
  395. pixel_ptr = row * c->pic.linesize[0];
  396. memcpy(outptr + pixel_ptr, encoded, 3 * width);
  397. encoded += 3 * width;
  398. }
  399. break;
  400. case IMGTYPE_YUV411:
  401. for (row = 0; row < height; row++) {
  402. for (col = 0; col < width - 3; col += 4) {
  403. memcpy(y_out + col, encoded, 4);
  404. encoded += 4;
  405. u_out[col >> 2] = *encoded++ + 128;
  406. v_out[col >> 2] = *encoded++ + 128;
  407. }
  408. y_out -= c->pic.linesize[0];
  409. u_out -= c->pic.linesize[1];
  410. v_out -= c->pic.linesize[2];
  411. }
  412. break;
  413. case IMGTYPE_YUV211:
  414. for (row = 0; row < height; row++) {
  415. for (col = 0; col < width - 1; col += 2) {
  416. memcpy(y_out + col, encoded, 2);
  417. encoded += 2;
  418. u_out[col >> 1] = *encoded++ + 128;
  419. v_out[col >> 1] = *encoded++ + 128;
  420. }
  421. y_out -= c->pic.linesize[0];
  422. u_out -= c->pic.linesize[1];
  423. v_out -= c->pic.linesize[2];
  424. }
  425. break;
  426. case IMGTYPE_YUV420:
  427. u_out = c->pic.data[1] + ((height >> 1) - 1) * c->pic.linesize[1];
  428. v_out = c->pic.data[2] + ((height >> 1) - 1) * c->pic.linesize[2];
  429. for (row = 0; row < height - 1; row += 2) {
  430. for (col = 0; col < width - 1; col += 2) {
  431. memcpy(y_out + col, encoded, 2);
  432. encoded += 2;
  433. memcpy(y_out + col - c->pic.linesize[0], encoded, 2);
  434. encoded += 2;
  435. u_out[col >> 1] = *encoded++ + 128;
  436. v_out[col >> 1] = *encoded++ + 128;
  437. }
  438. y_out -= c->pic.linesize[0] << 1;
  439. u_out -= c->pic.linesize[1];
  440. v_out -= c->pic.linesize[2];
  441. }
  442. break;
  443. default:
  444. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
  445. return -1;
  446. }
  447. *data_size = sizeof(AVFrame);
  448. *(AVFrame*)data = c->pic;
  449. /* always report that the buffer was completely consumed */
  450. return buf_size;
  451. }
  452. /*
  453. *
  454. * Init lcl decoder
  455. *
  456. */
  457. static av_cold int decode_init(AVCodecContext *avctx)
  458. {
  459. LclDecContext * const c = avctx->priv_data;
  460. unsigned int basesize = avctx->width * avctx->height;
  461. unsigned int max_basesize = FFALIGN(avctx->width, 4) * FFALIGN(avctx->height, 4) + AV_LZO_OUTPUT_PADDING;
  462. unsigned int max_decomp_size;
  463. avcodec_get_frame_defaults(&c->pic);
  464. if (avctx->extradata_size < 8) {
  465. av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
  466. return AVERROR_INVALIDDATA;
  467. }
  468. /* Check codec type */
  469. if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
  470. (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
  471. av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
  472. }
  473. /* Detect image type */
  474. switch (c->imgtype = avctx->extradata[4]) {
  475. case IMGTYPE_YUV111:
  476. c->decomp_size = basesize * 3;
  477. max_decomp_size = max_basesize * 3;
  478. avctx->pix_fmt = PIX_FMT_YUV444P;
  479. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
  480. break;
  481. case IMGTYPE_YUV422:
  482. c->decomp_size = basesize * 2;
  483. max_decomp_size = max_basesize * 2;
  484. avctx->pix_fmt = PIX_FMT_YUV422P;
  485. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
  486. break;
  487. case IMGTYPE_RGB24:
  488. c->decomp_size = basesize * 3;
  489. max_decomp_size = max_basesize * 3;
  490. avctx->pix_fmt = PIX_FMT_BGR24;
  491. av_log(avctx, AV_LOG_DEBUG, "Image type is RGB 24.\n");
  492. break;
  493. case IMGTYPE_YUV411:
  494. c->decomp_size = basesize / 2 * 3;
  495. max_decomp_size = max_basesize / 2 * 3;
  496. avctx->pix_fmt = PIX_FMT_YUV411P;
  497. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:1:1.\n");
  498. break;
  499. case IMGTYPE_YUV211:
  500. c->decomp_size = basesize * 2;
  501. max_decomp_size = max_basesize * 2;
  502. avctx->pix_fmt = PIX_FMT_YUV422P;
  503. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 2:1:1.\n");
  504. break;
  505. case IMGTYPE_YUV420:
  506. c->decomp_size = basesize / 2 * 3;
  507. max_decomp_size = max_basesize / 2 * 3;
  508. avctx->pix_fmt = PIX_FMT_YUV420P;
  509. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:0.\n");
  510. break;
  511. default:
  512. av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
  513. return AVERROR_INVALIDDATA;
  514. }
  515. /* Detect compression method */
  516. c->compression = (int8_t)avctx->extradata[5];
  517. switch (avctx->codec_id) {
  518. case AV_CODEC_ID_MSZH:
  519. switch (c->compression) {
  520. case COMP_MSZH:
  521. av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
  522. break;
  523. case COMP_MSZH_NOCOMP:
  524. c->decomp_size = 0;
  525. av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
  526. break;
  527. default:
  528. av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
  529. return AVERROR_INVALIDDATA;
  530. }
  531. break;
  532. #if CONFIG_ZLIB_DECODER
  533. case AV_CODEC_ID_ZLIB:
  534. switch (c->compression) {
  535. case COMP_ZLIB_HISPEED:
  536. av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
  537. break;
  538. case COMP_ZLIB_HICOMP:
  539. av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
  540. break;
  541. case COMP_ZLIB_NORMAL:
  542. av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
  543. break;
  544. default:
  545. if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
  546. av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
  547. return AVERROR_INVALIDDATA;
  548. }
  549. av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
  550. }
  551. break;
  552. #endif
  553. default:
  554. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
  555. return AVERROR_INVALIDDATA;
  556. }
  557. /* Allocate decompression buffer */
  558. if (c->decomp_size) {
  559. if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
  560. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  561. return AVERROR(ENOMEM);
  562. }
  563. }
  564. /* Detect flags */
  565. c->flags = avctx->extradata[6];
  566. if (c->flags & FLAG_MULTITHREAD)
  567. av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
  568. if (c->flags & FLAG_NULLFRAME)
  569. av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
  570. if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
  571. av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
  572. if (c->flags & FLAGMASK_UNUSED)
  573. av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
  574. /* If needed init zlib */
  575. #if CONFIG_ZLIB_DECODER
  576. if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
  577. int zret;
  578. c->zstream.zalloc = Z_NULL;
  579. c->zstream.zfree = Z_NULL;
  580. c->zstream.opaque = Z_NULL;
  581. zret = inflateInit(&c->zstream);
  582. if (zret != Z_OK) {
  583. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  584. av_freep(&c->decomp_buf);
  585. return AVERROR_UNKNOWN;
  586. }
  587. }
  588. #endif
  589. return 0;
  590. }
  591. /*
  592. *
  593. * Uninit lcl decoder
  594. *
  595. */
  596. static av_cold int decode_end(AVCodecContext *avctx)
  597. {
  598. LclDecContext * const c = avctx->priv_data;
  599. av_freep(&c->decomp_buf);
  600. if (c->pic.data[0])
  601. avctx->release_buffer(avctx, &c->pic);
  602. #if CONFIG_ZLIB_DECODER
  603. if (avctx->codec_id == AV_CODEC_ID_ZLIB)
  604. inflateEnd(&c->zstream);
  605. #endif
  606. return 0;
  607. }
  608. #if CONFIG_MSZH_DECODER
  609. AVCodec ff_mszh_decoder = {
  610. .name = "mszh",
  611. .type = AVMEDIA_TYPE_VIDEO,
  612. .id = AV_CODEC_ID_MSZH,
  613. .priv_data_size = sizeof(LclDecContext),
  614. .init = decode_init,
  615. .close = decode_end,
  616. .decode = decode_frame,
  617. .capabilities = CODEC_CAP_DR1,
  618. .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
  619. };
  620. #endif
  621. #if CONFIG_ZLIB_DECODER
  622. AVCodec ff_zlib_decoder = {
  623. .name = "zlib",
  624. .type = AVMEDIA_TYPE_VIDEO,
  625. .id = AV_CODEC_ID_ZLIB,
  626. .priv_data_size = sizeof(LclDecContext),
  627. .init = decode_init,
  628. .close = decode_end,
  629. .decode = decode_frame,
  630. .capabilities = CODEC_CAP_DR1,
  631. .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
  632. };
  633. #endif