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.

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