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.

680 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 "libavutil/pixdesc.h"
  43. #include "avcodec.h"
  44. #include "bytestream.h"
  45. #include "internal.h"
  46. #include "lcl.h"
  47. #if CONFIG_ZLIB_DECODER
  48. #include <zlib.h>
  49. #endif
  50. /*
  51. * Decoder context
  52. */
  53. typedef struct LclDecContext {
  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. AVFrame *frame = data;
  156. const uint8_t *buf = avpkt->data;
  157. int buf_size = avpkt->size;
  158. LclDecContext * const c = avctx->priv_data;
  159. unsigned int pixel_ptr;
  160. int row, col;
  161. unsigned char *encoded = avpkt->data, *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. int linesize;
  171. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  172. return ret;
  173. outptr = frame->data[0]; // Output image pointer
  174. /* Decompress frame */
  175. switch (avctx->codec_id) {
  176. case AV_CODEC_ID_MSZH:
  177. switch (c->compression) {
  178. case COMP_MSZH:
  179. if (c->imgtype == IMGTYPE_RGB24 && len == FFALIGN(width * 3, 4) * height ||
  180. c->imgtype == IMGTYPE_YUV111 && len == width * height * 3) {
  181. ;
  182. } else if (c->flags & FLAG_MULTITHREAD) {
  183. mthread_inlen = AV_RL32(buf);
  184. if (len < 8) {
  185. av_log(avctx, AV_LOG_ERROR, "len %d is too small\n", len);
  186. return AVERROR_INVALIDDATA;
  187. }
  188. mthread_inlen = FFMIN(mthread_inlen, len - 8);
  189. mthread_outlen = AV_RL32(buf + 4);
  190. mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
  191. mszh_dlen = mszh_decomp(buf + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
  192. if (mthread_outlen != mszh_dlen) {
  193. av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
  194. mthread_outlen, mszh_dlen);
  195. return AVERROR_INVALIDDATA;
  196. }
  197. mszh_dlen = mszh_decomp(buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
  198. c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
  199. if (mthread_outlen != mszh_dlen) {
  200. av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
  201. mthread_outlen, mszh_dlen);
  202. return AVERROR_INVALIDDATA;
  203. }
  204. encoded = c->decomp_buf;
  205. len = c->decomp_size;
  206. } else {
  207. mszh_dlen = mszh_decomp(buf, len, c->decomp_buf, c->decomp_size);
  208. if (c->decomp_size != mszh_dlen) {
  209. av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
  210. c->decomp_size, mszh_dlen);
  211. return AVERROR_INVALIDDATA;
  212. }
  213. encoded = c->decomp_buf;
  214. len = mszh_dlen;
  215. }
  216. break;
  217. case COMP_MSZH_NOCOMP: {
  218. int bppx2;
  219. switch (c->imgtype) {
  220. case IMGTYPE_YUV111:
  221. case IMGTYPE_RGB24:
  222. bppx2 = 6;
  223. break;
  224. case IMGTYPE_YUV422:
  225. case IMGTYPE_YUV211:
  226. bppx2 = 4;
  227. break;
  228. case IMGTYPE_YUV411:
  229. case IMGTYPE_YUV420:
  230. bppx2 = 3;
  231. break;
  232. default:
  233. bppx2 = 0; // will error out below
  234. break;
  235. }
  236. if (len < ((width * height * bppx2) >> 1))
  237. return AVERROR_INVALIDDATA;
  238. break;
  239. }
  240. default:
  241. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
  242. return AVERROR_INVALIDDATA;
  243. }
  244. break;
  245. #if CONFIG_ZLIB_DECODER
  246. case AV_CODEC_ID_ZLIB:
  247. /* Using the original dll with normal compression (-1) and RGB format
  248. * gives a file with ZLIB fourcc, but frame is really uncompressed.
  249. * To be sure that's true check also frame size */
  250. if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
  251. len == width * height * 3) {
  252. if (c->flags & FLAG_PNGFILTER) {
  253. memcpy(c->decomp_buf, buf, len);
  254. encoded = c->decomp_buf;
  255. } else {
  256. break;
  257. }
  258. } else if (c->flags & FLAG_MULTITHREAD) {
  259. mthread_inlen = AV_RL32(buf);
  260. mthread_inlen = FFMIN(mthread_inlen, len - 8);
  261. mthread_outlen = AV_RL32(buf + 4);
  262. mthread_outlen = FFMIN(mthread_outlen, c->decomp_size);
  263. ret = zlib_decomp(avctx, buf + 8, mthread_inlen, 0, mthread_outlen);
  264. if (ret < 0) return ret;
  265. ret = zlib_decomp(avctx, buf + 8 + mthread_inlen, len - 8 - mthread_inlen,
  266. mthread_outlen, mthread_outlen);
  267. if (ret < 0) return ret;
  268. } else {
  269. int ret = zlib_decomp(avctx, buf, len, 0, c->decomp_size);
  270. if (ret < 0) return ret;
  271. }
  272. encoded = c->decomp_buf;
  273. len = c->decomp_size;
  274. break;
  275. #endif
  276. default:
  277. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
  278. return AVERROR_INVALIDDATA;
  279. }
  280. /* Apply PNG filter */
  281. if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER)) {
  282. switch (c->imgtype) {
  283. case IMGTYPE_YUV111:
  284. case IMGTYPE_RGB24:
  285. for (row = 0; row < height; row++) {
  286. pixel_ptr = row * width * 3;
  287. yq = encoded[pixel_ptr++];
  288. uqvq = AV_RL16(encoded+pixel_ptr);
  289. pixel_ptr += 2;
  290. for (col = 1; col < width; col++) {
  291. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  292. uqvq -= AV_RL16(encoded+pixel_ptr+1);
  293. AV_WL16(encoded+pixel_ptr+1, uqvq);
  294. pixel_ptr += 3;
  295. }
  296. }
  297. break;
  298. case IMGTYPE_YUV422:
  299. for (row = 0; row < height; row++) {
  300. pixel_ptr = row * width * 2;
  301. yq = uq = vq =0;
  302. for (col = 0; col < width/4; col++) {
  303. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  304. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  305. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  306. encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
  307. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  308. encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
  309. encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
  310. encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
  311. pixel_ptr += 8;
  312. }
  313. }
  314. break;
  315. case IMGTYPE_YUV411:
  316. for (row = 0; row < height; row++) {
  317. pixel_ptr = row * width / 2 * 3;
  318. yq = uq = vq =0;
  319. for (col = 0; col < width/4; col++) {
  320. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  321. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  322. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  323. encoded[pixel_ptr+3] = yq -= 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. case IMGTYPE_YUV211:
  331. for (row = 0; row < height; row++) {
  332. pixel_ptr = row * width * 2;
  333. yq = uq = vq =0;
  334. for (col = 0; col < width/2; col++) {
  335. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  336. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  337. encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
  338. encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
  339. pixel_ptr += 4;
  340. }
  341. }
  342. break;
  343. case IMGTYPE_YUV420:
  344. for (row = 0; row < height/2; row++) {
  345. pixel_ptr = row * width * 3;
  346. yq = y1q = uq = vq =0;
  347. for (col = 0; col < width/2; col++) {
  348. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  349. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  350. encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
  351. encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
  352. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  353. encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
  354. pixel_ptr += 6;
  355. }
  356. }
  357. break;
  358. default:
  359. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
  360. return AVERROR_INVALIDDATA;
  361. }
  362. }
  363. /* Convert colorspace */
  364. y_out = frame->data[0] + (height - 1) * frame->linesize[0];
  365. u_out = frame->data[1] + (height - 1) * frame->linesize[1];
  366. v_out = frame->data[2] + (height - 1) * frame->linesize[2];
  367. switch (c->imgtype) {
  368. case IMGTYPE_YUV111:
  369. for (row = 0; row < height; row++) {
  370. for (col = 0; col < width; col++) {
  371. y_out[col] = *encoded++;
  372. u_out[col] = *encoded++ + 128;
  373. v_out[col] = *encoded++ + 128;
  374. }
  375. y_out -= frame->linesize[0];
  376. u_out -= frame->linesize[1];
  377. v_out -= frame->linesize[2];
  378. }
  379. break;
  380. case IMGTYPE_YUV422:
  381. for (row = 0; row < height; row++) {
  382. for (col = 0; col < width - 3; col += 4) {
  383. memcpy(y_out + col, encoded, 4);
  384. encoded += 4;
  385. u_out[ col >> 1 ] = *encoded++ + 128;
  386. u_out[(col >> 1) + 1] = *encoded++ + 128;
  387. v_out[ col >> 1 ] = *encoded++ + 128;
  388. v_out[(col >> 1) + 1] = *encoded++ + 128;
  389. }
  390. y_out -= frame->linesize[0];
  391. u_out -= frame->linesize[1];
  392. v_out -= frame->linesize[2];
  393. }
  394. break;
  395. case IMGTYPE_RGB24:
  396. linesize = len < FFALIGN(3 * width, 4) * height ? 3 * width : FFALIGN(3 * width, 4);
  397. for (row = height - 1; row >= 0; row--) {
  398. pixel_ptr = row * frame->linesize[0];
  399. memcpy(outptr + pixel_ptr, encoded, 3 * width);
  400. encoded += linesize;
  401. }
  402. break;
  403. case IMGTYPE_YUV411:
  404. for (row = 0; row < height; row++) {
  405. for (col = 0; col < width - 3; col += 4) {
  406. memcpy(y_out + col, encoded, 4);
  407. encoded += 4;
  408. u_out[col >> 2] = *encoded++ + 128;
  409. v_out[col >> 2] = *encoded++ + 128;
  410. }
  411. y_out -= frame->linesize[0];
  412. u_out -= frame->linesize[1];
  413. v_out -= frame->linesize[2];
  414. }
  415. break;
  416. case IMGTYPE_YUV211:
  417. for (row = 0; row < height; row++) {
  418. for (col = 0; col < width - 1; col += 2) {
  419. memcpy(y_out + col, encoded, 2);
  420. encoded += 2;
  421. u_out[col >> 1] = *encoded++ + 128;
  422. v_out[col >> 1] = *encoded++ + 128;
  423. }
  424. y_out -= frame->linesize[0];
  425. u_out -= frame->linesize[1];
  426. v_out -= frame->linesize[2];
  427. }
  428. break;
  429. case IMGTYPE_YUV420:
  430. u_out = frame->data[1] + ((height >> 1) - 1) * frame->linesize[1];
  431. v_out = frame->data[2] + ((height >> 1) - 1) * frame->linesize[2];
  432. for (row = 0; row < height - 1; row += 2) {
  433. for (col = 0; col < width - 1; col += 2) {
  434. memcpy(y_out + col, encoded, 2);
  435. encoded += 2;
  436. memcpy(y_out + col - frame->linesize[0], encoded, 2);
  437. encoded += 2;
  438. u_out[col >> 1] = *encoded++ + 128;
  439. v_out[col >> 1] = *encoded++ + 128;
  440. }
  441. y_out -= frame->linesize[0] << 1;
  442. u_out -= frame->linesize[1];
  443. v_out -= frame->linesize[2];
  444. }
  445. break;
  446. default:
  447. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
  448. return AVERROR_INVALIDDATA;
  449. }
  450. *got_frame = 1;
  451. /* always report that the buffer was completely consumed */
  452. return buf_size;
  453. }
  454. /*
  455. *
  456. * Init lcl decoder
  457. *
  458. */
  459. static av_cold int decode_init(AVCodecContext *avctx)
  460. {
  461. LclDecContext * const c = avctx->priv_data;
  462. unsigned int basesize = avctx->width * avctx->height;
  463. unsigned int max_basesize = FFALIGN(avctx->width, 4) *
  464. FFALIGN(avctx->height, 4);
  465. unsigned int max_decomp_size;
  466. int subsample_h, subsample_v;
  467. if (avctx->extradata_size < 8) {
  468. av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
  469. return AVERROR_INVALIDDATA;
  470. }
  471. /* Check codec type */
  472. if ((avctx->codec_id == AV_CODEC_ID_MSZH && avctx->extradata[7] != CODEC_MSZH) ||
  473. (avctx->codec_id == AV_CODEC_ID_ZLIB && avctx->extradata[7] != CODEC_ZLIB)) {
  474. av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
  475. }
  476. /* Detect image type */
  477. switch (c->imgtype = avctx->extradata[4]) {
  478. case IMGTYPE_YUV111:
  479. c->decomp_size = basesize * 3;
  480. max_decomp_size = max_basesize * 3;
  481. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  482. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 1:1:1.\n");
  483. break;
  484. case IMGTYPE_YUV422:
  485. c->decomp_size = basesize * 2;
  486. max_decomp_size = max_basesize * 2;
  487. avctx->pix_fmt = AV_PIX_FMT_YUV422P;
  488. av_log(avctx, AV_LOG_DEBUG, "Image type is YUV 4:2:2.\n");
  489. if (avctx->width % 4) {
  490. avpriv_request_sample(avctx, "Unsupported dimensions");
  491. return AVERROR_INVALIDDATA;
  492. }
  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. av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &subsample_h, &subsample_v);
  523. if (avctx->width % (1<<subsample_h) || avctx->height % (1<<subsample_v)) {
  524. avpriv_request_sample(avctx, "Unsupported dimensions");
  525. return AVERROR_INVALIDDATA;
  526. }
  527. /* Detect compression method */
  528. c->compression = (int8_t)avctx->extradata[5];
  529. switch (avctx->codec_id) {
  530. case AV_CODEC_ID_MSZH:
  531. switch (c->compression) {
  532. case COMP_MSZH:
  533. av_log(avctx, AV_LOG_DEBUG, "Compression enabled.\n");
  534. break;
  535. case COMP_MSZH_NOCOMP:
  536. c->decomp_size = 0;
  537. av_log(avctx, AV_LOG_DEBUG, "No compression.\n");
  538. break;
  539. default:
  540. av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
  541. return AVERROR_INVALIDDATA;
  542. }
  543. break;
  544. #if CONFIG_ZLIB_DECODER
  545. case AV_CODEC_ID_ZLIB:
  546. switch (c->compression) {
  547. case COMP_ZLIB_HISPEED:
  548. av_log(avctx, AV_LOG_DEBUG, "High speed compression.\n");
  549. break;
  550. case COMP_ZLIB_HICOMP:
  551. av_log(avctx, AV_LOG_DEBUG, "High compression.\n");
  552. break;
  553. case COMP_ZLIB_NORMAL:
  554. av_log(avctx, AV_LOG_DEBUG, "Normal compression.\n");
  555. break;
  556. default:
  557. if (c->compression < Z_NO_COMPRESSION || c->compression > Z_BEST_COMPRESSION) {
  558. av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
  559. return AVERROR_INVALIDDATA;
  560. }
  561. av_log(avctx, AV_LOG_DEBUG, "Compression level for ZLIB: (%d).\n", c->compression);
  562. }
  563. break;
  564. #endif
  565. default:
  566. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
  567. return AVERROR_INVALIDDATA;
  568. }
  569. /* Allocate decompression buffer */
  570. if (c->decomp_size) {
  571. if (!(c->decomp_buf = av_malloc(max_decomp_size))) {
  572. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  573. return AVERROR(ENOMEM);
  574. }
  575. }
  576. /* Detect flags */
  577. c->flags = avctx->extradata[6];
  578. if (c->flags & FLAG_MULTITHREAD)
  579. av_log(avctx, AV_LOG_DEBUG, "Multithread encoder flag set.\n");
  580. if (c->flags & FLAG_NULLFRAME)
  581. av_log(avctx, AV_LOG_DEBUG, "Nullframe insertion flag set.\n");
  582. if (avctx->codec_id == AV_CODEC_ID_ZLIB && (c->flags & FLAG_PNGFILTER))
  583. av_log(avctx, AV_LOG_DEBUG, "PNG filter flag set.\n");
  584. if (c->flags & FLAGMASK_UNUSED)
  585. av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
  586. /* If needed init zlib */
  587. #if CONFIG_ZLIB_DECODER
  588. if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
  589. int zret;
  590. c->zstream.zalloc = Z_NULL;
  591. c->zstream.zfree = Z_NULL;
  592. c->zstream.opaque = Z_NULL;
  593. zret = inflateInit(&c->zstream);
  594. if (zret != Z_OK) {
  595. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  596. av_freep(&c->decomp_buf);
  597. return AVERROR_UNKNOWN;
  598. }
  599. }
  600. #endif
  601. return 0;
  602. }
  603. /*
  604. *
  605. * Uninit lcl decoder
  606. *
  607. */
  608. static av_cold int decode_end(AVCodecContext *avctx)
  609. {
  610. LclDecContext * const c = avctx->priv_data;
  611. av_freep(&c->decomp_buf);
  612. #if CONFIG_ZLIB_DECODER
  613. if (avctx->codec_id == AV_CODEC_ID_ZLIB)
  614. inflateEnd(&c->zstream);
  615. #endif
  616. return 0;
  617. }
  618. #if CONFIG_MSZH_DECODER
  619. AVCodec ff_mszh_decoder = {
  620. .name = "mszh",
  621. .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
  622. .type = AVMEDIA_TYPE_VIDEO,
  623. .id = AV_CODEC_ID_MSZH,
  624. .priv_data_size = sizeof(LclDecContext),
  625. .init = decode_init,
  626. .close = decode_end,
  627. .decode = decode_frame,
  628. .capabilities = AV_CODEC_CAP_DR1,
  629. };
  630. #endif
  631. #if CONFIG_ZLIB_DECODER
  632. AVCodec ff_zlib_decoder = {
  633. .name = "zlib",
  634. .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
  635. .type = AVMEDIA_TYPE_VIDEO,
  636. .id = AV_CODEC_ID_ZLIB,
  637. .priv_data_size = sizeof(LclDecContext),
  638. .init = decode_init,
  639. .close = decode_end,
  640. .decode = decode_frame,
  641. .capabilities = AV_CODEC_CAP_DR1,
  642. };
  643. #endif