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.

712 lines
25KB

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