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.

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