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.

930 lines
31KB

  1. /*
  2. * LCL (LossLess Codec Library) Codec
  3. * Copyright (c) 2002-2004 Roberto Togni
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file lcl.c
  22. * LCL (LossLess Codec Library) Video Codec
  23. * Decoder for MSZH and ZLIB codecs
  24. * Experimental encoder for ZLIB RGB24
  25. *
  26. * Fourcc: MSZH, ZLIB
  27. *
  28. * Original Win32 dll:
  29. * Ver2.23 By Kenji Oshima 2000.09.20
  30. * avimszh.dll, avizlib.dll
  31. *
  32. * A description of the decoding algorithm can be found here:
  33. * http://www.pcisys.net/~melanson/codecs
  34. *
  35. * Supports: BGR24 (RGB 24bpp)
  36. *
  37. */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include "common.h"
  41. #include "bitstream.h"
  42. #include "avcodec.h"
  43. #ifdef CONFIG_ZLIB
  44. #include <zlib.h>
  45. #endif
  46. #define BMPTYPE_YUV 1
  47. #define BMPTYPE_RGB 2
  48. #define IMGTYPE_YUV111 0
  49. #define IMGTYPE_YUV422 1
  50. #define IMGTYPE_RGB24 2
  51. #define IMGTYPE_YUV411 3
  52. #define IMGTYPE_YUV211 4
  53. #define IMGTYPE_YUV420 5
  54. #define COMP_MSZH 0
  55. #define COMP_MSZH_NOCOMP 1
  56. #define COMP_ZLIB_HISPEED 1
  57. #define COMP_ZLIB_HICOMP 9
  58. #define COMP_ZLIB_NORMAL -1
  59. #define FLAG_MULTITHREAD 1
  60. #define FLAG_NULLFRAME 2
  61. #define FLAG_PNGFILTER 4
  62. #define FLAGMASK_UNUSED 0xf8
  63. #define CODEC_MSZH 1
  64. #define CODEC_ZLIB 3
  65. #define FOURCC_MSZH mmioFOURCC('M','S','Z','H')
  66. #define FOURCC_ZLIB mmioFOURCC('Z','L','I','B')
  67. /*
  68. * Decoder context
  69. */
  70. typedef struct LclContext {
  71. AVCodecContext *avctx;
  72. AVFrame pic;
  73. PutBitContext pb;
  74. // Image type
  75. int imgtype;
  76. // Compression type
  77. int compression;
  78. // Flags
  79. int flags;
  80. // Decompressed data size
  81. unsigned int decomp_size;
  82. // Decompression buffer
  83. unsigned char* decomp_buf;
  84. // Maximum compressed data size
  85. unsigned int max_comp_size;
  86. // Compression buffer
  87. unsigned char* comp_buf;
  88. #ifdef CONFIG_ZLIB
  89. z_stream zstream;
  90. #endif
  91. } LclContext;
  92. /*
  93. *
  94. * Helper functions
  95. *
  96. */
  97. static inline unsigned char fix (int pix14)
  98. {
  99. int tmp;
  100. tmp = (pix14 + 0x80000) >> 20;
  101. if (tmp < 0)
  102. return 0;
  103. if (tmp > 255)
  104. return 255;
  105. return tmp;
  106. }
  107. static inline unsigned char get_b (unsigned char yq, signed char bq)
  108. {
  109. return fix((yq << 20) + bq * 1858076);
  110. }
  111. static inline unsigned char get_g (unsigned char yq, signed char bq, signed char rq)
  112. {
  113. return fix((yq << 20) - bq * 360857 - rq * 748830);
  114. }
  115. static inline unsigned char get_r (unsigned char yq, signed char rq)
  116. {
  117. return fix((yq << 20) + rq * 1470103);
  118. }
  119. static unsigned int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
  120. {
  121. unsigned char *destptr_bak = destptr;
  122. unsigned char *destptr_end = destptr + destsize;
  123. unsigned char mask = 0;
  124. unsigned char maskbit = 0;
  125. unsigned int ofs, cnt;
  126. while ((srclen > 0) && (destptr < destptr_end)) {
  127. if (maskbit == 0) {
  128. mask = *(srcptr++);
  129. maskbit = 8;
  130. srclen--;
  131. continue;
  132. }
  133. if ((mask & (1 << (--maskbit))) == 0) {
  134. if (destptr + 4 > destptr_end)
  135. break;
  136. *(int*)destptr = *(int*)srcptr;
  137. srclen -= 4;
  138. destptr += 4;
  139. srcptr += 4;
  140. } else {
  141. ofs = *(srcptr++);
  142. cnt = *(srcptr++);
  143. ofs += cnt * 256;;
  144. cnt = ((cnt >> 3) & 0x1f) + 1;
  145. ofs &= 0x7ff;
  146. srclen -= 2;
  147. cnt *= 4;
  148. if (destptr + cnt > destptr_end) {
  149. cnt = destptr_end - destptr;
  150. }
  151. for (; cnt > 0; cnt--) {
  152. *(destptr) = *(destptr - ofs);
  153. destptr++;
  154. }
  155. }
  156. }
  157. return (destptr - destptr_bak);
  158. }
  159. /*
  160. *
  161. * Decode a frame
  162. *
  163. */
  164. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
  165. {
  166. LclContext * const c = (LclContext *)avctx->priv_data;
  167. unsigned char *encoded = (unsigned char *)buf;
  168. unsigned int pixel_ptr;
  169. int row, col;
  170. unsigned char *outptr;
  171. unsigned int width = avctx->width; // Real image width
  172. unsigned int height = avctx->height; // Real image height
  173. unsigned int mszh_dlen;
  174. unsigned char yq, y1q, uq, vq;
  175. int uqvq;
  176. unsigned int mthread_inlen, mthread_outlen;
  177. #ifdef CONFIG_ZLIB
  178. int zret; // Zlib return code
  179. #endif
  180. unsigned int len = buf_size;
  181. /* no supplementary picture */
  182. if (buf_size == 0)
  183. return 0;
  184. if(c->pic.data[0])
  185. avctx->release_buffer(avctx, &c->pic);
  186. c->pic.reference = 0;
  187. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  188. if(avctx->get_buffer(avctx, &c->pic) < 0){
  189. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  190. return -1;
  191. }
  192. outptr = c->pic.data[0]; // Output image pointer
  193. /* Decompress frame */
  194. switch (avctx->codec_id) {
  195. case CODEC_ID_MSZH:
  196. switch (c->compression) {
  197. case COMP_MSZH:
  198. if (c->flags & FLAG_MULTITHREAD) {
  199. mthread_inlen = *((unsigned int*)encoded);
  200. mthread_outlen = *((unsigned int*)(encoded+4));
  201. if (mthread_outlen > c->decomp_size) // this should not happen
  202. mthread_outlen = c->decomp_size;
  203. mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
  204. if (mthread_outlen != mszh_dlen) {
  205. av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
  206. mthread_outlen, mszh_dlen);
  207. return -1;
  208. }
  209. mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - mthread_inlen,
  210. c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
  211. if (mthread_outlen != mszh_dlen) {
  212. av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
  213. mthread_outlen, mszh_dlen);
  214. return -1;
  215. }
  216. encoded = c->decomp_buf;
  217. len = c->decomp_size;
  218. } else {
  219. mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
  220. if (c->decomp_size != mszh_dlen) {
  221. av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
  222. c->decomp_size, mszh_dlen);
  223. return -1;
  224. }
  225. encoded = c->decomp_buf;
  226. len = mszh_dlen;
  227. }
  228. break;
  229. case COMP_MSZH_NOCOMP:
  230. break;
  231. default:
  232. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
  233. return -1;
  234. }
  235. break;
  236. case CODEC_ID_ZLIB:
  237. #ifdef CONFIG_ZLIB
  238. /* Using the original dll with normal compression (-1) and RGB format
  239. * gives a file with ZLIB fourcc, but frame is really uncompressed.
  240. * To be sure that's true check also frame size */
  241. if ((c->compression == COMP_ZLIB_NORMAL) && (c->imgtype == IMGTYPE_RGB24) &&
  242. (len == width * height * 3))
  243. break;
  244. zret = inflateReset(&(c->zstream));
  245. if (zret != Z_OK) {
  246. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  247. return -1;
  248. }
  249. if (c->flags & FLAG_MULTITHREAD) {
  250. mthread_inlen = *((unsigned int*)encoded);
  251. mthread_outlen = *((unsigned int*)(encoded+4));
  252. if (mthread_outlen > c->decomp_size)
  253. mthread_outlen = c->decomp_size;
  254. c->zstream.next_in = encoded + 8;
  255. c->zstream.avail_in = mthread_inlen;
  256. c->zstream.next_out = c->decomp_buf;
  257. c->zstream.avail_out = c->decomp_size;
  258. zret = inflate(&(c->zstream), Z_FINISH);
  259. if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
  260. av_log(avctx, AV_LOG_ERROR, "Mthread1 inflate error: %d\n", zret);
  261. return -1;
  262. }
  263. if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
  264. av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%u != %lu)\n",
  265. mthread_outlen, c->zstream.total_out);
  266. return -1;
  267. }
  268. zret = inflateReset(&(c->zstream));
  269. if (zret != Z_OK) {
  270. av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate reset error: %d\n", zret);
  271. return -1;
  272. }
  273. c->zstream.next_in = encoded + 8 + mthread_inlen;
  274. c->zstream.avail_in = len - mthread_inlen;
  275. c->zstream.next_out = c->decomp_buf + mthread_outlen;
  276. c->zstream.avail_out = c->decomp_size - mthread_outlen;
  277. zret = inflate(&(c->zstream), Z_FINISH);
  278. if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
  279. av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate error: %d\n", zret);
  280. return -1;
  281. }
  282. if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
  283. av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %lu)\n",
  284. mthread_outlen, c->zstream.total_out);
  285. return -1;
  286. }
  287. } else {
  288. c->zstream.next_in = encoded;
  289. c->zstream.avail_in = len;
  290. c->zstream.next_out = c->decomp_buf;
  291. c->zstream.avail_out = c->decomp_size;
  292. zret = inflate(&(c->zstream), Z_FINISH);
  293. if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
  294. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
  295. return -1;
  296. }
  297. if (c->decomp_size != (unsigned int)(c->zstream.total_out)) {
  298. av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
  299. c->decomp_size, c->zstream.total_out);
  300. return -1;
  301. }
  302. }
  303. encoded = c->decomp_buf;
  304. len = c->decomp_size;;
  305. #else
  306. av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
  307. return -1;
  308. #endif
  309. break;
  310. default:
  311. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
  312. return -1;
  313. }
  314. /* Apply PNG filter */
  315. if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) {
  316. switch (c->imgtype) {
  317. case IMGTYPE_YUV111:
  318. case IMGTYPE_RGB24:
  319. for (row = 0; row < height; row++) {
  320. pixel_ptr = row * width * 3;
  321. yq = encoded[pixel_ptr++];
  322. uqvq = encoded[pixel_ptr++];
  323. uqvq+=(encoded[pixel_ptr++] << 8);
  324. for (col = 1; col < width; col++) {
  325. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  326. uqvq -= (encoded[pixel_ptr+1] | (encoded[pixel_ptr+2]<<8));
  327. encoded[pixel_ptr+1] = (uqvq) & 0xff;
  328. encoded[pixel_ptr+2] = ((uqvq)>>8) & 0xff;
  329. pixel_ptr += 3;
  330. }
  331. }
  332. break;
  333. case IMGTYPE_YUV422:
  334. for (row = 0; row < height; row++) {
  335. pixel_ptr = row * width * 2;
  336. yq = uq = vq =0;
  337. for (col = 0; col < width/4; col++) {
  338. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  339. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  340. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  341. encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
  342. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  343. encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
  344. encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
  345. encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
  346. pixel_ptr += 8;
  347. }
  348. }
  349. break;
  350. case IMGTYPE_YUV411:
  351. for (row = 0; row < height; row++) {
  352. pixel_ptr = row * width / 2 * 3;
  353. yq = uq = vq =0;
  354. for (col = 0; col < width/4; col++) {
  355. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  356. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  357. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  358. encoded[pixel_ptr+3] = yq -= 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. case IMGTYPE_YUV211:
  366. for (row = 0; row < height; row++) {
  367. pixel_ptr = row * width * 2;
  368. yq = uq = vq =0;
  369. for (col = 0; col < width/2; col++) {
  370. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  371. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  372. encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
  373. encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
  374. pixel_ptr += 4;
  375. }
  376. }
  377. break;
  378. case IMGTYPE_YUV420:
  379. for (row = 0; row < height/2; row++) {
  380. pixel_ptr = row * width * 3;
  381. yq = y1q = uq = vq =0;
  382. for (col = 0; col < width/2; col++) {
  383. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  384. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  385. encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
  386. encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
  387. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  388. encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
  389. pixel_ptr += 6;
  390. }
  391. }
  392. break;
  393. default:
  394. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
  395. return -1;
  396. }
  397. }
  398. /* Convert colorspace */
  399. switch (c->imgtype) {
  400. case IMGTYPE_YUV111:
  401. for (row = height - 1; row >= 0; row--) {
  402. pixel_ptr = row * c->pic.linesize[0];
  403. for (col = 0; col < width; col++) {
  404. outptr[pixel_ptr++] = get_b(encoded[0], encoded[1]);
  405. outptr[pixel_ptr++] = get_g(encoded[0], encoded[1], encoded[2]);
  406. outptr[pixel_ptr++] = get_r(encoded[0], encoded[2]);
  407. encoded += 3;
  408. }
  409. }
  410. break;
  411. case IMGTYPE_YUV422:
  412. for (row = height - 1; row >= 0; row--) {
  413. pixel_ptr = row * c->pic.linesize[0];
  414. for (col = 0; col < width/4; col++) {
  415. outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
  416. outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[6]);
  417. outptr[pixel_ptr++] = get_r(encoded[0], encoded[6]);
  418. outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
  419. outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[6]);
  420. outptr[pixel_ptr++] = get_r(encoded[1], encoded[6]);
  421. outptr[pixel_ptr++] = get_b(encoded[2], encoded[5]);
  422. outptr[pixel_ptr++] = get_g(encoded[2], encoded[5], encoded[7]);
  423. outptr[pixel_ptr++] = get_r(encoded[2], encoded[7]);
  424. outptr[pixel_ptr++] = get_b(encoded[3], encoded[5]);
  425. outptr[pixel_ptr++] = get_g(encoded[3], encoded[5], encoded[7]);
  426. outptr[pixel_ptr++] = get_r(encoded[3], encoded[7]);
  427. encoded += 8;
  428. }
  429. }
  430. break;
  431. case IMGTYPE_RGB24:
  432. for (row = height - 1; row >= 0; row--) {
  433. pixel_ptr = row * c->pic.linesize[0];
  434. for (col = 0; col < width; col++) {
  435. outptr[pixel_ptr++] = encoded[0];
  436. outptr[pixel_ptr++] = encoded[1];
  437. outptr[pixel_ptr++] = encoded[2];
  438. encoded += 3;
  439. }
  440. }
  441. break;
  442. case IMGTYPE_YUV411:
  443. for (row = height - 1; row >= 0; row--) {
  444. pixel_ptr = row * c->pic.linesize[0];
  445. for (col = 0; col < width/4; col++) {
  446. outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
  447. outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[5]);
  448. outptr[pixel_ptr++] = get_r(encoded[0], encoded[5]);
  449. outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
  450. outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[5]);
  451. outptr[pixel_ptr++] = get_r(encoded[1], encoded[5]);
  452. outptr[pixel_ptr++] = get_b(encoded[2], encoded[4]);
  453. outptr[pixel_ptr++] = get_g(encoded[2], encoded[4], encoded[5]);
  454. outptr[pixel_ptr++] = get_r(encoded[2], encoded[5]);
  455. outptr[pixel_ptr++] = get_b(encoded[3], encoded[4]);
  456. outptr[pixel_ptr++] = get_g(encoded[3], encoded[4], encoded[5]);
  457. outptr[pixel_ptr++] = get_r(encoded[3], encoded[5]);
  458. encoded += 6;
  459. }
  460. }
  461. break;
  462. case IMGTYPE_YUV211:
  463. for (row = height - 1; row >= 0; row--) {
  464. pixel_ptr = row * c->pic.linesize[0];
  465. for (col = 0; col < width/2; col++) {
  466. outptr[pixel_ptr++] = get_b(encoded[0], encoded[2]);
  467. outptr[pixel_ptr++] = get_g(encoded[0], encoded[2], encoded[3]);
  468. outptr[pixel_ptr++] = get_r(encoded[0], encoded[3]);
  469. outptr[pixel_ptr++] = get_b(encoded[1], encoded[2]);
  470. outptr[pixel_ptr++] = get_g(encoded[1], encoded[2], encoded[3]);
  471. outptr[pixel_ptr++] = get_r(encoded[1], encoded[3]);
  472. encoded += 4;
  473. }
  474. }
  475. break;
  476. case IMGTYPE_YUV420:
  477. for (row = height / 2 - 1; row >= 0; row--) {
  478. pixel_ptr = 2 * row * c->pic.linesize[0];
  479. for (col = 0; col < width/2; col++) {
  480. outptr[pixel_ptr] = get_b(encoded[0], encoded[4]);
  481. outptr[pixel_ptr+1] = get_g(encoded[0], encoded[4], encoded[5]);
  482. outptr[pixel_ptr+2] = get_r(encoded[0], encoded[5]);
  483. outptr[pixel_ptr+3] = get_b(encoded[1], encoded[4]);
  484. outptr[pixel_ptr+4] = get_g(encoded[1], encoded[4], encoded[5]);
  485. outptr[pixel_ptr+5] = get_r(encoded[1], encoded[5]);
  486. outptr[pixel_ptr-c->pic.linesize[0]] = get_b(encoded[2], encoded[4]);
  487. outptr[pixel_ptr-c->pic.linesize[0]+1] = get_g(encoded[2], encoded[4], encoded[5]);
  488. outptr[pixel_ptr-c->pic.linesize[0]+2] = get_r(encoded[2], encoded[5]);
  489. outptr[pixel_ptr-c->pic.linesize[0]+3] = get_b(encoded[3], encoded[4]);
  490. outptr[pixel_ptr-c->pic.linesize[0]+4] = get_g(encoded[3], encoded[4], encoded[5]);
  491. outptr[pixel_ptr-c->pic.linesize[0]+5] = get_r(encoded[3], encoded[5]);
  492. pixel_ptr += 6;
  493. encoded += 6;
  494. }
  495. }
  496. break;
  497. default:
  498. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
  499. return -1;
  500. }
  501. *data_size = sizeof(AVFrame);
  502. *(AVFrame*)data = c->pic;
  503. /* always report that the buffer was completely consumed */
  504. return buf_size;
  505. }
  506. /*
  507. *
  508. * Encode a frame
  509. *
  510. */
  511. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  512. LclContext *c = avctx->priv_data;
  513. AVFrame *pict = data;
  514. AVFrame * const p = &c->pic;
  515. int i;
  516. int zret; // Zlib return code
  517. #ifndef CONFIG_ZLIB
  518. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled in.\n");
  519. return -1;
  520. #else
  521. init_put_bits(&c->pb, buf, buf_size);
  522. *p = *pict;
  523. p->pict_type= FF_I_TYPE;
  524. p->key_frame= 1;
  525. if(avctx->pix_fmt != PIX_FMT_BGR24){
  526. av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
  527. return -1;
  528. }
  529. zret = deflateReset(&(c->zstream));
  530. if (zret != Z_OK) {
  531. av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
  532. return -1;
  533. }
  534. c->zstream.next_out = c->comp_buf;
  535. c->zstream.avail_out = c->max_comp_size;
  536. for(i = avctx->height - 1; i >= 0; i--) {
  537. c->zstream.next_in = p->data[0]+p->linesize[0]*i;
  538. c->zstream.avail_in = avctx->width*3;
  539. zret = deflate(&(c->zstream), Z_NO_FLUSH);
  540. if (zret != Z_OK) {
  541. av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
  542. return -1;
  543. }
  544. }
  545. zret = deflate(&(c->zstream), Z_FINISH);
  546. if (zret != Z_STREAM_END) {
  547. av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
  548. return -1;
  549. }
  550. for (i = 0; i < c->zstream.total_out; i++)
  551. put_bits(&c->pb, 8, c->comp_buf[i]);
  552. flush_put_bits(&c->pb);
  553. return c->zstream.total_out;
  554. #endif
  555. }
  556. /*
  557. *
  558. * Init lcl decoder
  559. *
  560. */
  561. static int decode_init(AVCodecContext *avctx)
  562. {
  563. LclContext * const c = (LclContext *)avctx->priv_data;
  564. unsigned int basesize = avctx->width * avctx->height;
  565. unsigned int max_basesize = ((avctx->width + 3) & ~3) * ((avctx->height + 3) & ~3);
  566. unsigned int max_decomp_size;
  567. int zret; // Zlib return code
  568. c->avctx = avctx;
  569. avctx->has_b_frames = 0;
  570. c->pic.data[0] = NULL;
  571. #ifdef CONFIG_ZLIB
  572. // Needed if zlib unused or init aborted before inflateInit
  573. memset(&(c->zstream), 0, sizeof(z_stream));
  574. #endif
  575. if (avctx->extradata_size < 8) {
  576. av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
  577. return 1;
  578. }
  579. // FIXME: find a better way to prevent integer overflow
  580. if (((unsigned int)avctx->width > 32000) || ((unsigned int)avctx->height > 32000)) {
  581. av_log(avctx, AV_LOG_ERROR, "Bad image size (w = %d, h = %d).\n", avctx->width, avctx->height);
  582. return 1;
  583. }
  584. /* Check codec type */
  585. if (((avctx->codec_id == CODEC_ID_MSZH) && (*((char *)avctx->extradata + 7) != CODEC_MSZH)) ||
  586. ((avctx->codec_id == CODEC_ID_ZLIB) && (*((char *)avctx->extradata + 7) != CODEC_ZLIB))) {
  587. av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
  588. }
  589. /* Detect image type */
  590. switch (c->imgtype = *((char *)avctx->extradata + 4)) {
  591. case IMGTYPE_YUV111:
  592. c->decomp_size = basesize * 3;
  593. max_decomp_size = max_basesize * 3;
  594. av_log(avctx, AV_LOG_INFO, "Image type is YUV 1:1:1.\n");
  595. break;
  596. case IMGTYPE_YUV422:
  597. c->decomp_size = basesize * 2;
  598. max_decomp_size = max_basesize * 2;
  599. av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:2.\n");
  600. break;
  601. case IMGTYPE_RGB24:
  602. c->decomp_size = basesize * 3;
  603. max_decomp_size = max_basesize * 3;
  604. av_log(avctx, AV_LOG_INFO, "Image type is RGB 24.\n");
  605. break;
  606. case IMGTYPE_YUV411:
  607. c->decomp_size = basesize / 2 * 3;
  608. max_decomp_size = max_basesize / 2 * 3;
  609. av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:1:1.\n");
  610. break;
  611. case IMGTYPE_YUV211:
  612. c->decomp_size = basesize * 2;
  613. max_decomp_size = max_basesize * 2;
  614. av_log(avctx, AV_LOG_INFO, "Image type is YUV 2:1:1.\n");
  615. break;
  616. case IMGTYPE_YUV420:
  617. c->decomp_size = basesize / 2 * 3;
  618. max_decomp_size = max_basesize / 2 * 3;
  619. av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:0.\n");
  620. break;
  621. default:
  622. av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
  623. return 1;
  624. }
  625. /* Detect compression method */
  626. c->compression = *((char *)avctx->extradata + 5);
  627. switch (avctx->codec_id) {
  628. case CODEC_ID_MSZH:
  629. switch (c->compression) {
  630. case COMP_MSZH:
  631. av_log(avctx, AV_LOG_INFO, "Compression enabled.\n");
  632. break;
  633. case COMP_MSZH_NOCOMP:
  634. c->decomp_size = 0;
  635. av_log(avctx, AV_LOG_INFO, "No compression.\n");
  636. break;
  637. default:
  638. av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
  639. return 1;
  640. }
  641. break;
  642. case CODEC_ID_ZLIB:
  643. #ifdef CONFIG_ZLIB
  644. switch (c->compression) {
  645. case COMP_ZLIB_HISPEED:
  646. av_log(avctx, AV_LOG_INFO, "High speed compression.\n");
  647. break;
  648. case COMP_ZLIB_HICOMP:
  649. av_log(avctx, AV_LOG_INFO, "High compression.\n");
  650. break;
  651. case COMP_ZLIB_NORMAL:
  652. av_log(avctx, AV_LOG_INFO, "Normal compression.\n");
  653. break;
  654. default:
  655. if ((c->compression < Z_NO_COMPRESSION) || (c->compression > Z_BEST_COMPRESSION)) {
  656. av_log(avctx, AV_LOG_ERROR, "Unusupported compression level for ZLIB: (%d).\n", c->compression);
  657. return 1;
  658. }
  659. av_log(avctx, AV_LOG_INFO, "Compression level for ZLIB: (%d).\n", c->compression);
  660. }
  661. #else
  662. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  663. return 1;
  664. #endif
  665. break;
  666. default:
  667. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
  668. return 1;
  669. }
  670. /* Allocate decompression buffer */
  671. if (c->decomp_size) {
  672. if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
  673. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  674. return 1;
  675. }
  676. }
  677. /* Detect flags */
  678. c->flags = *((char *)avctx->extradata + 6);
  679. if (c->flags & FLAG_MULTITHREAD)
  680. av_log(avctx, AV_LOG_INFO, "Multithread encoder flag set.\n");
  681. if (c->flags & FLAG_NULLFRAME)
  682. av_log(avctx, AV_LOG_INFO, "Nullframe insertion flag set.\n");
  683. if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER))
  684. av_log(avctx, AV_LOG_INFO, "PNG filter flag set.\n");
  685. if (c->flags & FLAGMASK_UNUSED)
  686. av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
  687. /* If needed init zlib */
  688. if (avctx->codec_id == CODEC_ID_ZLIB) {
  689. #ifdef CONFIG_ZLIB
  690. c->zstream.zalloc = Z_NULL;
  691. c->zstream.zfree = Z_NULL;
  692. c->zstream.opaque = Z_NULL;
  693. zret = inflateInit(&(c->zstream));
  694. if (zret != Z_OK) {
  695. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  696. return 1;
  697. }
  698. #else
  699. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  700. return 1;
  701. #endif
  702. }
  703. avctx->pix_fmt = PIX_FMT_BGR24;
  704. return 0;
  705. }
  706. /*
  707. *
  708. * Init lcl encoder
  709. *
  710. */
  711. static int encode_init(AVCodecContext *avctx)
  712. {
  713. LclContext *c = avctx->priv_data;
  714. int zret; // Zlib return code
  715. #ifndef CONFIG_ZLIB
  716. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  717. return 1;
  718. #else
  719. c->avctx= avctx;
  720. assert(avctx->width && avctx->height);
  721. avctx->extradata= av_mallocz(8);
  722. avctx->coded_frame= &c->pic;
  723. // Will be user settable someday
  724. c->compression = 6;
  725. c->flags = 0;
  726. switch(avctx->pix_fmt){
  727. case PIX_FMT_BGR24:
  728. c->imgtype = IMGTYPE_RGB24;
  729. c->decomp_size = avctx->width * avctx->height * 3;
  730. avctx->bits_per_sample= 24;
  731. break;
  732. default:
  733. av_log(avctx, AV_LOG_ERROR, "Format %d not supported\n", avctx->pix_fmt);
  734. return -1;
  735. }
  736. ((uint8_t*)avctx->extradata)[0]= 4;
  737. ((uint8_t*)avctx->extradata)[1]= 0;
  738. ((uint8_t*)avctx->extradata)[2]= 0;
  739. ((uint8_t*)avctx->extradata)[3]= 0;
  740. ((uint8_t*)avctx->extradata)[4]= c->imgtype;
  741. ((uint8_t*)avctx->extradata)[5]= c->compression;
  742. ((uint8_t*)avctx->extradata)[6]= c->flags;
  743. ((uint8_t*)avctx->extradata)[7]= CODEC_ZLIB;
  744. c->avctx->extradata_size= 8;
  745. c->zstream.zalloc = Z_NULL;
  746. c->zstream.zfree = Z_NULL;
  747. c->zstream.opaque = Z_NULL;
  748. zret = deflateInit(&(c->zstream), c->compression);
  749. if (zret != Z_OK) {
  750. av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret);
  751. return 1;
  752. }
  753. /* Conservative upper bound taken from zlib v1.2.1 source */
  754. c->max_comp_size = c->decomp_size + ((c->decomp_size + 7) >> 3) +
  755. ((c->decomp_size + 63) >> 6) + 11;
  756. if ((c->comp_buf = av_malloc(c->max_comp_size)) == NULL) {
  757. av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
  758. return 1;
  759. }
  760. return 0;
  761. #endif
  762. }
  763. /*
  764. *
  765. * Uninit lcl decoder
  766. *
  767. */
  768. static int decode_end(AVCodecContext *avctx)
  769. {
  770. LclContext * const c = (LclContext *)avctx->priv_data;
  771. if (c->pic.data[0])
  772. avctx->release_buffer(avctx, &c->pic);
  773. #ifdef CONFIG_ZLIB
  774. inflateEnd(&(c->zstream));
  775. #endif
  776. return 0;
  777. }
  778. /*
  779. *
  780. * Uninit lcl encoder
  781. *
  782. */
  783. static int encode_end(AVCodecContext *avctx)
  784. {
  785. LclContext *c = avctx->priv_data;
  786. av_freep(&avctx->extradata);
  787. av_freep(&c->comp_buf);
  788. #ifdef CONFIG_ZLIB
  789. deflateEnd(&(c->zstream));
  790. #endif
  791. return 0;
  792. }
  793. AVCodec mszh_decoder = {
  794. "mszh",
  795. CODEC_TYPE_VIDEO,
  796. CODEC_ID_MSZH,
  797. sizeof(LclContext),
  798. decode_init,
  799. NULL,
  800. decode_end,
  801. decode_frame,
  802. CODEC_CAP_DR1,
  803. };
  804. AVCodec zlib_decoder = {
  805. "zlib",
  806. CODEC_TYPE_VIDEO,
  807. CODEC_ID_ZLIB,
  808. sizeof(LclContext),
  809. decode_init,
  810. NULL,
  811. decode_end,
  812. decode_frame,
  813. CODEC_CAP_DR1,
  814. };
  815. #ifdef CONFIG_ENCODERS
  816. AVCodec zlib_encoder = {
  817. "zlib",
  818. CODEC_TYPE_VIDEO,
  819. CODEC_ID_ZLIB,
  820. sizeof(LclContext),
  821. encode_init,
  822. encode_frame,
  823. encode_end,
  824. // .options = lcl_options,
  825. };
  826. #endif //CONFIG_ENCODERS