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
27KB

  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 lcl.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. #ifdef CONFIG_ZLIB
  45. #include <zlib.h>
  46. #endif
  47. /*
  48. * Decoder context
  49. */
  50. typedef struct LclDecContext {
  51. AVCodecContext *avctx;
  52. AVFrame pic;
  53. // Image type
  54. int imgtype;
  55. // Compression type
  56. int compression;
  57. // Flags
  58. int flags;
  59. // Decompressed data size
  60. unsigned int decomp_size;
  61. // Decompression buffer
  62. unsigned char* decomp_buf;
  63. #ifdef CONFIG_ZLIB
  64. z_stream zstream;
  65. #endif
  66. } LclDecContext;
  67. /*
  68. *
  69. * Helper functions
  70. *
  71. */
  72. static inline unsigned char fix (int pix14)
  73. {
  74. int tmp;
  75. tmp = (pix14 + 0x80000) >> 20;
  76. if (tmp < 0)
  77. return 0;
  78. if (tmp > 255)
  79. return 255;
  80. return tmp;
  81. }
  82. static inline unsigned char get_b (unsigned char yq, signed char bq)
  83. {
  84. return fix((yq << 20) + bq * 1858076);
  85. }
  86. static inline unsigned char get_g (unsigned char yq, signed char bq, signed char rq)
  87. {
  88. return fix((yq << 20) - bq * 360857 - rq * 748830);
  89. }
  90. static inline unsigned char get_r (unsigned char yq, signed char rq)
  91. {
  92. return fix((yq << 20) + rq * 1470103);
  93. }
  94. static unsigned int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
  95. {
  96. unsigned char *destptr_bak = destptr;
  97. unsigned char *destptr_end = destptr + destsize;
  98. unsigned char mask = 0;
  99. unsigned char maskbit = 0;
  100. unsigned int ofs, cnt;
  101. while ((srclen > 0) && (destptr < destptr_end)) {
  102. if (maskbit == 0) {
  103. mask = *(srcptr++);
  104. maskbit = 8;
  105. srclen--;
  106. continue;
  107. }
  108. if ((mask & (1 << (--maskbit))) == 0) {
  109. if (destptr + 4 > destptr_end)
  110. break;
  111. *(int*)destptr = *(int*)srcptr;
  112. srclen -= 4;
  113. destptr += 4;
  114. srcptr += 4;
  115. } else {
  116. ofs = *(srcptr++);
  117. cnt = *(srcptr++);
  118. ofs += cnt * 256;;
  119. cnt = ((cnt >> 3) & 0x1f) + 1;
  120. ofs &= 0x7ff;
  121. srclen -= 2;
  122. cnt *= 4;
  123. if (destptr + cnt > destptr_end) {
  124. cnt = destptr_end - destptr;
  125. }
  126. for (; cnt > 0; cnt--) {
  127. *(destptr) = *(destptr - ofs);
  128. destptr++;
  129. }
  130. }
  131. }
  132. return (destptr - destptr_bak);
  133. }
  134. /*
  135. *
  136. * Decode a frame
  137. *
  138. */
  139. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
  140. {
  141. LclDecContext * const c = avctx->priv_data;
  142. unsigned char *encoded = (unsigned char *)buf;
  143. unsigned int pixel_ptr;
  144. int row, col;
  145. unsigned char *outptr;
  146. unsigned int width = avctx->width; // Real image width
  147. unsigned int height = avctx->height; // Real image height
  148. unsigned int mszh_dlen;
  149. unsigned char yq, y1q, uq, vq;
  150. int uqvq;
  151. unsigned int mthread_inlen, mthread_outlen;
  152. #ifdef CONFIG_ZLIB
  153. int zret; // Zlib return code
  154. #endif
  155. unsigned int len = buf_size;
  156. if(c->pic.data[0])
  157. avctx->release_buffer(avctx, &c->pic);
  158. c->pic.reference = 0;
  159. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  160. if(avctx->get_buffer(avctx, &c->pic) < 0){
  161. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  162. return -1;
  163. }
  164. outptr = c->pic.data[0]; // Output image pointer
  165. /* Decompress frame */
  166. switch (avctx->codec_id) {
  167. case CODEC_ID_MSZH:
  168. switch (c->compression) {
  169. case COMP_MSZH:
  170. if (c->flags & FLAG_MULTITHREAD) {
  171. mthread_inlen = *((unsigned int*)encoded);
  172. mthread_outlen = *((unsigned int*)(encoded+4));
  173. if (mthread_outlen > c->decomp_size) // this should not happen
  174. mthread_outlen = c->decomp_size;
  175. mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
  176. if (mthread_outlen != mszh_dlen) {
  177. av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
  178. mthread_outlen, mszh_dlen);
  179. return -1;
  180. }
  181. mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - mthread_inlen,
  182. c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
  183. if (mthread_outlen != mszh_dlen) {
  184. av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
  185. mthread_outlen, mszh_dlen);
  186. return -1;
  187. }
  188. encoded = c->decomp_buf;
  189. len = c->decomp_size;
  190. } else {
  191. mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
  192. if (c->decomp_size != mszh_dlen) {
  193. av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
  194. c->decomp_size, mszh_dlen);
  195. return -1;
  196. }
  197. encoded = c->decomp_buf;
  198. len = mszh_dlen;
  199. }
  200. break;
  201. case COMP_MSZH_NOCOMP:
  202. break;
  203. default:
  204. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
  205. return -1;
  206. }
  207. break;
  208. case CODEC_ID_ZLIB:
  209. #ifdef CONFIG_ZLIB
  210. /* Using the original dll with normal compression (-1) and RGB format
  211. * gives a file with ZLIB fourcc, but frame is really uncompressed.
  212. * To be sure that's true check also frame size */
  213. if ((c->compression == COMP_ZLIB_NORMAL) && (c->imgtype == IMGTYPE_RGB24) &&
  214. (len == width * height * 3))
  215. break;
  216. zret = inflateReset(&(c->zstream));
  217. if (zret != Z_OK) {
  218. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  219. return -1;
  220. }
  221. if (c->flags & FLAG_MULTITHREAD) {
  222. mthread_inlen = *((unsigned int*)encoded);
  223. mthread_outlen = *((unsigned int*)(encoded+4));
  224. if (mthread_outlen > c->decomp_size)
  225. mthread_outlen = c->decomp_size;
  226. c->zstream.next_in = encoded + 8;
  227. c->zstream.avail_in = mthread_inlen;
  228. c->zstream.next_out = c->decomp_buf;
  229. c->zstream.avail_out = c->decomp_size;
  230. zret = inflate(&(c->zstream), Z_FINISH);
  231. if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
  232. av_log(avctx, AV_LOG_ERROR, "Mthread1 inflate error: %d\n", zret);
  233. return -1;
  234. }
  235. if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
  236. av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%u != %lu)\n",
  237. mthread_outlen, c->zstream.total_out);
  238. return -1;
  239. }
  240. zret = inflateReset(&(c->zstream));
  241. if (zret != Z_OK) {
  242. av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate reset error: %d\n", zret);
  243. return -1;
  244. }
  245. c->zstream.next_in = encoded + 8 + mthread_inlen;
  246. c->zstream.avail_in = len - mthread_inlen;
  247. c->zstream.next_out = c->decomp_buf + mthread_outlen;
  248. c->zstream.avail_out = c->decomp_size - mthread_outlen;
  249. zret = inflate(&(c->zstream), Z_FINISH);
  250. if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
  251. av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate error: %d\n", zret);
  252. return -1;
  253. }
  254. if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
  255. av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %lu)\n",
  256. mthread_outlen, c->zstream.total_out);
  257. return -1;
  258. }
  259. } else {
  260. c->zstream.next_in = encoded;
  261. c->zstream.avail_in = len;
  262. c->zstream.next_out = c->decomp_buf;
  263. c->zstream.avail_out = c->decomp_size;
  264. zret = inflate(&(c->zstream), Z_FINISH);
  265. if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
  266. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
  267. return -1;
  268. }
  269. if (c->decomp_size != (unsigned int)(c->zstream.total_out)) {
  270. av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
  271. c->decomp_size, c->zstream.total_out);
  272. return -1;
  273. }
  274. }
  275. encoded = c->decomp_buf;
  276. len = c->decomp_size;;
  277. #else
  278. av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
  279. return -1;
  280. #endif
  281. break;
  282. default:
  283. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
  284. return -1;
  285. }
  286. /* Apply PNG filter */
  287. if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) {
  288. switch (c->imgtype) {
  289. case IMGTYPE_YUV111:
  290. case IMGTYPE_RGB24:
  291. for (row = 0; row < height; row++) {
  292. pixel_ptr = row * width * 3;
  293. yq = encoded[pixel_ptr++];
  294. uqvq = AV_RL16(encoded+pixel_ptr);
  295. pixel_ptr += 2;
  296. for (col = 1; col < width; col++) {
  297. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  298. uqvq -= AV_RL16(encoded+pixel_ptr+1);
  299. AV_WL16(encoded+pixel_ptr+1, uqvq);
  300. pixel_ptr += 3;
  301. }
  302. }
  303. break;
  304. case IMGTYPE_YUV422:
  305. for (row = 0; row < height; row++) {
  306. pixel_ptr = row * width * 2;
  307. yq = uq = vq =0;
  308. for (col = 0; col < width/4; col++) {
  309. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  310. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  311. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  312. encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
  313. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  314. encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
  315. encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
  316. encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
  317. pixel_ptr += 8;
  318. }
  319. }
  320. break;
  321. case IMGTYPE_YUV411:
  322. for (row = 0; row < height; row++) {
  323. pixel_ptr = row * width / 2 * 3;
  324. yq = uq = vq =0;
  325. for (col = 0; col < width/4; col++) {
  326. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  327. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  328. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  329. encoded[pixel_ptr+3] = yq -= 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. case IMGTYPE_YUV211:
  337. for (row = 0; row < height; row++) {
  338. pixel_ptr = row * width * 2;
  339. yq = uq = vq =0;
  340. for (col = 0; col < width/2; col++) {
  341. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  342. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  343. encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
  344. encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
  345. pixel_ptr += 4;
  346. }
  347. }
  348. break;
  349. case IMGTYPE_YUV420:
  350. for (row = 0; row < height/2; row++) {
  351. pixel_ptr = row * width * 3;
  352. yq = y1q = uq = vq =0;
  353. for (col = 0; col < width/2; col++) {
  354. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  355. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  356. encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
  357. encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
  358. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  359. encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
  360. pixel_ptr += 6;
  361. }
  362. }
  363. break;
  364. default:
  365. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
  366. return -1;
  367. }
  368. }
  369. /* Convert colorspace */
  370. switch (c->imgtype) {
  371. case IMGTYPE_YUV111:
  372. for (row = height - 1; row >= 0; row--) {
  373. pixel_ptr = row * c->pic.linesize[0];
  374. for (col = 0; col < width; col++) {
  375. outptr[pixel_ptr++] = get_b(encoded[0], encoded[1]);
  376. outptr[pixel_ptr++] = get_g(encoded[0], encoded[1], encoded[2]);
  377. outptr[pixel_ptr++] = get_r(encoded[0], encoded[2]);
  378. encoded += 3;
  379. }
  380. }
  381. break;
  382. case IMGTYPE_YUV422:
  383. for (row = height - 1; row >= 0; row--) {
  384. pixel_ptr = row * c->pic.linesize[0];
  385. for (col = 0; col < width/4; col++) {
  386. outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
  387. outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[6]);
  388. outptr[pixel_ptr++] = get_r(encoded[0], encoded[6]);
  389. outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
  390. outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[6]);
  391. outptr[pixel_ptr++] = get_r(encoded[1], encoded[6]);
  392. outptr[pixel_ptr++] = get_b(encoded[2], encoded[5]);
  393. outptr[pixel_ptr++] = get_g(encoded[2], encoded[5], encoded[7]);
  394. outptr[pixel_ptr++] = get_r(encoded[2], encoded[7]);
  395. outptr[pixel_ptr++] = get_b(encoded[3], encoded[5]);
  396. outptr[pixel_ptr++] = get_g(encoded[3], encoded[5], encoded[7]);
  397. outptr[pixel_ptr++] = get_r(encoded[3], encoded[7]);
  398. encoded += 8;
  399. }
  400. }
  401. break;
  402. case IMGTYPE_RGB24:
  403. for (row = height - 1; row >= 0; row--) {
  404. pixel_ptr = row * c->pic.linesize[0];
  405. for (col = 0; col < width; col++) {
  406. outptr[pixel_ptr++] = encoded[0];
  407. outptr[pixel_ptr++] = encoded[1];
  408. outptr[pixel_ptr++] = encoded[2];
  409. encoded += 3;
  410. }
  411. }
  412. break;
  413. case IMGTYPE_YUV411:
  414. for (row = height - 1; row >= 0; row--) {
  415. pixel_ptr = row * c->pic.linesize[0];
  416. for (col = 0; col < width/4; col++) {
  417. outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
  418. outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[5]);
  419. outptr[pixel_ptr++] = get_r(encoded[0], encoded[5]);
  420. outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
  421. outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[5]);
  422. outptr[pixel_ptr++] = get_r(encoded[1], encoded[5]);
  423. outptr[pixel_ptr++] = get_b(encoded[2], encoded[4]);
  424. outptr[pixel_ptr++] = get_g(encoded[2], encoded[4], encoded[5]);
  425. outptr[pixel_ptr++] = get_r(encoded[2], encoded[5]);
  426. outptr[pixel_ptr++] = get_b(encoded[3], encoded[4]);
  427. outptr[pixel_ptr++] = get_g(encoded[3], encoded[4], encoded[5]);
  428. outptr[pixel_ptr++] = get_r(encoded[3], encoded[5]);
  429. encoded += 6;
  430. }
  431. }
  432. break;
  433. case IMGTYPE_YUV211:
  434. for (row = height - 1; row >= 0; row--) {
  435. pixel_ptr = row * c->pic.linesize[0];
  436. for (col = 0; col < width/2; col++) {
  437. outptr[pixel_ptr++] = get_b(encoded[0], encoded[2]);
  438. outptr[pixel_ptr++] = get_g(encoded[0], encoded[2], encoded[3]);
  439. outptr[pixel_ptr++] = get_r(encoded[0], encoded[3]);
  440. outptr[pixel_ptr++] = get_b(encoded[1], encoded[2]);
  441. outptr[pixel_ptr++] = get_g(encoded[1], encoded[2], encoded[3]);
  442. outptr[pixel_ptr++] = get_r(encoded[1], encoded[3]);
  443. encoded += 4;
  444. }
  445. }
  446. break;
  447. case IMGTYPE_YUV420:
  448. for (row = height / 2 - 1; row >= 0; row--) {
  449. pixel_ptr = 2 * row * c->pic.linesize[0];
  450. for (col = 0; col < width/2; col++) {
  451. outptr[pixel_ptr] = get_b(encoded[0], encoded[4]);
  452. outptr[pixel_ptr+1] = get_g(encoded[0], encoded[4], encoded[5]);
  453. outptr[pixel_ptr+2] = get_r(encoded[0], encoded[5]);
  454. outptr[pixel_ptr+3] = get_b(encoded[1], encoded[4]);
  455. outptr[pixel_ptr+4] = get_g(encoded[1], encoded[4], encoded[5]);
  456. outptr[pixel_ptr+5] = get_r(encoded[1], encoded[5]);
  457. outptr[pixel_ptr-c->pic.linesize[0]] = get_b(encoded[2], encoded[4]);
  458. outptr[pixel_ptr-c->pic.linesize[0]+1] = get_g(encoded[2], encoded[4], encoded[5]);
  459. outptr[pixel_ptr-c->pic.linesize[0]+2] = get_r(encoded[2], encoded[5]);
  460. outptr[pixel_ptr-c->pic.linesize[0]+3] = get_b(encoded[3], encoded[4]);
  461. outptr[pixel_ptr-c->pic.linesize[0]+4] = get_g(encoded[3], encoded[4], encoded[5]);
  462. outptr[pixel_ptr-c->pic.linesize[0]+5] = get_r(encoded[3], encoded[5]);
  463. pixel_ptr += 6;
  464. encoded += 6;
  465. }
  466. }
  467. break;
  468. default:
  469. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
  470. return -1;
  471. }
  472. *data_size = sizeof(AVFrame);
  473. *(AVFrame*)data = c->pic;
  474. /* always report that the buffer was completely consumed */
  475. return buf_size;
  476. }
  477. /*
  478. *
  479. * Init lcl decoder
  480. *
  481. */
  482. static int decode_init(AVCodecContext *avctx)
  483. {
  484. LclDecContext * const c = avctx->priv_data;
  485. unsigned int basesize = avctx->width * avctx->height;
  486. unsigned int max_basesize = ((avctx->width + 3) & ~3) * ((avctx->height + 3) & ~3);
  487. unsigned int max_decomp_size;
  488. int zret; // Zlib return code
  489. c->avctx = avctx;
  490. c->pic.data[0] = NULL;
  491. #ifdef 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. #ifdef 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. #ifdef 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 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. #ifdef CONFIG_ZLIB
  635. inflateEnd(&(c->zstream));
  636. #endif
  637. return 0;
  638. }
  639. #ifdef 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. };
  651. #endif
  652. #ifdef CONFIG_ZLIB_DECODER
  653. AVCodec zlib_decoder = {
  654. "zlib",
  655. CODEC_TYPE_VIDEO,
  656. CODEC_ID_ZLIB,
  657. sizeof(LclDecContext),
  658. decode_init,
  659. NULL,
  660. decode_end,
  661. decode_frame,
  662. CODEC_CAP_DR1,
  663. };
  664. #endif