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.

678 lines
24KB

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