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.

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