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.

716 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. for (col = 0; col < width; col++) {
  403. outptr[pixel_ptr++] = encoded[0];
  404. outptr[pixel_ptr++] = encoded[1];
  405. outptr[pixel_ptr++] = encoded[2];
  406. encoded += 3;
  407. }
  408. }
  409. break;
  410. case IMGTYPE_YUV411:
  411. for (row = height - 1; row >= 0; row--) {
  412. pixel_ptr = row * c->pic.linesize[0];
  413. for (col = 0; col < width/4; col++) {
  414. outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
  415. outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[5]);
  416. outptr[pixel_ptr++] = get_r(encoded[0], encoded[5]);
  417. outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
  418. outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[5]);
  419. outptr[pixel_ptr++] = get_r(encoded[1], encoded[5]);
  420. outptr[pixel_ptr++] = get_b(encoded[2], encoded[4]);
  421. outptr[pixel_ptr++] = get_g(encoded[2], encoded[4], encoded[5]);
  422. outptr[pixel_ptr++] = get_r(encoded[2], encoded[5]);
  423. outptr[pixel_ptr++] = get_b(encoded[3], encoded[4]);
  424. outptr[pixel_ptr++] = get_g(encoded[3], encoded[4], encoded[5]);
  425. outptr[pixel_ptr++] = get_r(encoded[3], encoded[5]);
  426. encoded += 6;
  427. }
  428. }
  429. break;
  430. case IMGTYPE_YUV211:
  431. for (row = height - 1; row >= 0; row--) {
  432. pixel_ptr = row * c->pic.linesize[0];
  433. for (col = 0; col < width/2; col++) {
  434. outptr[pixel_ptr++] = get_b(encoded[0], encoded[2]);
  435. outptr[pixel_ptr++] = get_g(encoded[0], encoded[2], encoded[3]);
  436. outptr[pixel_ptr++] = get_r(encoded[0], encoded[3]);
  437. outptr[pixel_ptr++] = get_b(encoded[1], encoded[2]);
  438. outptr[pixel_ptr++] = get_g(encoded[1], encoded[2], encoded[3]);
  439. outptr[pixel_ptr++] = get_r(encoded[1], encoded[3]);
  440. encoded += 4;
  441. }
  442. }
  443. break;
  444. case IMGTYPE_YUV420:
  445. for (row = height / 2 - 1; row >= 0; row--) {
  446. pixel_ptr = 2 * row * c->pic.linesize[0];
  447. for (col = 0; col < width/2; col++) {
  448. outptr[pixel_ptr] = get_b(encoded[0], encoded[4]);
  449. outptr[pixel_ptr+1] = get_g(encoded[0], encoded[4], encoded[5]);
  450. outptr[pixel_ptr+2] = get_r(encoded[0], encoded[5]);
  451. outptr[pixel_ptr+3] = get_b(encoded[1], encoded[4]);
  452. outptr[pixel_ptr+4] = get_g(encoded[1], encoded[4], encoded[5]);
  453. outptr[pixel_ptr+5] = get_r(encoded[1], encoded[5]);
  454. outptr[pixel_ptr-c->pic.linesize[0]] = get_b(encoded[2], encoded[4]);
  455. outptr[pixel_ptr-c->pic.linesize[0]+1] = get_g(encoded[2], encoded[4], encoded[5]);
  456. outptr[pixel_ptr-c->pic.linesize[0]+2] = get_r(encoded[2], encoded[5]);
  457. outptr[pixel_ptr-c->pic.linesize[0]+3] = get_b(encoded[3], encoded[4]);
  458. outptr[pixel_ptr-c->pic.linesize[0]+4] = get_g(encoded[3], encoded[4], encoded[5]);
  459. outptr[pixel_ptr-c->pic.linesize[0]+5] = get_r(encoded[3], encoded[5]);
  460. pixel_ptr += 6;
  461. encoded += 6;
  462. }
  463. }
  464. break;
  465. default:
  466. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
  467. return -1;
  468. }
  469. *data_size = sizeof(AVFrame);
  470. *(AVFrame*)data = c->pic;
  471. /* always report that the buffer was completely consumed */
  472. return buf_size;
  473. }
  474. /*
  475. *
  476. * Init lcl decoder
  477. *
  478. */
  479. static av_cold int decode_init(AVCodecContext *avctx)
  480. {
  481. LclDecContext * const c = avctx->priv_data;
  482. unsigned int basesize = avctx->width * avctx->height;
  483. unsigned int max_basesize = ((avctx->width + 3) & ~3) * ((avctx->height + 3) & ~3);
  484. unsigned int max_decomp_size;
  485. int zret; // Zlib return code
  486. c->pic.data[0] = NULL;
  487. #if CONFIG_ZLIB
  488. // Needed if zlib unused or init aborted before inflateInit
  489. memset(&(c->zstream), 0, sizeof(z_stream));
  490. #endif
  491. if (avctx->extradata_size < 8) {
  492. av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
  493. return 1;
  494. }
  495. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
  496. return 1;
  497. }
  498. /* Check codec type */
  499. if (((avctx->codec_id == CODEC_ID_MSZH) && (*((char *)avctx->extradata + 7) != CODEC_MSZH)) ||
  500. ((avctx->codec_id == CODEC_ID_ZLIB) && (*((char *)avctx->extradata + 7) != CODEC_ZLIB))) {
  501. av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
  502. }
  503. /* Detect image type */
  504. switch (c->imgtype = *((char *)avctx->extradata + 4)) {
  505. case IMGTYPE_YUV111:
  506. c->decomp_size = basesize * 3;
  507. max_decomp_size = max_basesize * 3;
  508. av_log(avctx, AV_LOG_INFO, "Image type is YUV 1:1:1.\n");
  509. break;
  510. case IMGTYPE_YUV422:
  511. c->decomp_size = basesize * 2;
  512. max_decomp_size = max_basesize * 2;
  513. av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:2.\n");
  514. break;
  515. case IMGTYPE_RGB24:
  516. c->decomp_size = basesize * 3;
  517. max_decomp_size = max_basesize * 3;
  518. av_log(avctx, AV_LOG_INFO, "Image type is RGB 24.\n");
  519. break;
  520. case IMGTYPE_YUV411:
  521. c->decomp_size = basesize / 2 * 3;
  522. max_decomp_size = max_basesize / 2 * 3;
  523. av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:1:1.\n");
  524. break;
  525. case IMGTYPE_YUV211:
  526. c->decomp_size = basesize * 2;
  527. max_decomp_size = max_basesize * 2;
  528. av_log(avctx, AV_LOG_INFO, "Image type is YUV 2:1:1.\n");
  529. break;
  530. case IMGTYPE_YUV420:
  531. c->decomp_size = basesize / 2 * 3;
  532. max_decomp_size = max_basesize / 2 * 3;
  533. av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:0.\n");
  534. break;
  535. default:
  536. av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
  537. return 1;
  538. }
  539. /* Detect compression method */
  540. c->compression = *((char *)avctx->extradata + 5);
  541. switch (avctx->codec_id) {
  542. case CODEC_ID_MSZH:
  543. switch (c->compression) {
  544. case COMP_MSZH:
  545. av_log(avctx, AV_LOG_INFO, "Compression enabled.\n");
  546. break;
  547. case COMP_MSZH_NOCOMP:
  548. c->decomp_size = 0;
  549. av_log(avctx, AV_LOG_INFO, "No compression.\n");
  550. break;
  551. default:
  552. av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
  553. return 1;
  554. }
  555. break;
  556. case CODEC_ID_ZLIB:
  557. #if CONFIG_ZLIB
  558. switch (c->compression) {
  559. case COMP_ZLIB_HISPEED:
  560. av_log(avctx, AV_LOG_INFO, "High speed compression.\n");
  561. break;
  562. case COMP_ZLIB_HICOMP:
  563. av_log(avctx, AV_LOG_INFO, "High compression.\n");
  564. break;
  565. case COMP_ZLIB_NORMAL:
  566. av_log(avctx, AV_LOG_INFO, "Normal compression.\n");
  567. break;
  568. default:
  569. if ((c->compression < Z_NO_COMPRESSION) || (c->compression > Z_BEST_COMPRESSION)) {
  570. av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
  571. return 1;
  572. }
  573. av_log(avctx, AV_LOG_INFO, "Compression level for ZLIB: (%d).\n", c->compression);
  574. }
  575. #else
  576. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  577. return 1;
  578. #endif
  579. break;
  580. default:
  581. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
  582. return 1;
  583. }
  584. /* Allocate decompression buffer */
  585. if (c->decomp_size) {
  586. if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
  587. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  588. return 1;
  589. }
  590. }
  591. /* Detect flags */
  592. c->flags = *((char *)avctx->extradata + 6);
  593. if (c->flags & FLAG_MULTITHREAD)
  594. av_log(avctx, AV_LOG_INFO, "Multithread encoder flag set.\n");
  595. if (c->flags & FLAG_NULLFRAME)
  596. av_log(avctx, AV_LOG_INFO, "Nullframe insertion flag set.\n");
  597. if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER))
  598. av_log(avctx, AV_LOG_INFO, "PNG filter flag set.\n");
  599. if (c->flags & FLAGMASK_UNUSED)
  600. av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
  601. /* If needed init zlib */
  602. if (avctx->codec_id == CODEC_ID_ZLIB) {
  603. #if CONFIG_ZLIB
  604. c->zstream.zalloc = Z_NULL;
  605. c->zstream.zfree = Z_NULL;
  606. c->zstream.opaque = Z_NULL;
  607. zret = inflateInit(&(c->zstream));
  608. if (zret != Z_OK) {
  609. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  610. return 1;
  611. }
  612. #else
  613. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  614. return 1;
  615. #endif
  616. }
  617. avctx->pix_fmt = PIX_FMT_BGR24;
  618. return 0;
  619. }
  620. /*
  621. *
  622. * Uninit lcl decoder
  623. *
  624. */
  625. static av_cold int decode_end(AVCodecContext *avctx)
  626. {
  627. LclDecContext * const c = avctx->priv_data;
  628. if (c->pic.data[0])
  629. avctx->release_buffer(avctx, &c->pic);
  630. #if CONFIG_ZLIB
  631. inflateEnd(&(c->zstream));
  632. #endif
  633. return 0;
  634. }
  635. #if CONFIG_MSZH_DECODER
  636. AVCodec mszh_decoder = {
  637. "mszh",
  638. CODEC_TYPE_VIDEO,
  639. CODEC_ID_MSZH,
  640. sizeof(LclDecContext),
  641. decode_init,
  642. NULL,
  643. decode_end,
  644. decode_frame,
  645. CODEC_CAP_DR1,
  646. .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
  647. };
  648. #endif
  649. #if CONFIG_ZLIB_DECODER
  650. AVCodec zlib_decoder = {
  651. "zlib",
  652. CODEC_TYPE_VIDEO,
  653. CODEC_ID_ZLIB,
  654. sizeof(LclDecContext),
  655. decode_init,
  656. NULL,
  657. decode_end,
  658. decode_frame,
  659. CODEC_CAP_DR1,
  660. .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
  661. };
  662. #endif