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.

901 lines
30KB

  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 int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr)
  120. {
  121. unsigned char *destptr_bak = destptr;
  122. unsigned char mask = 0;
  123. unsigned char maskbit = 0;
  124. unsigned int ofs, cnt;
  125. while (srclen > 0) {
  126. if (maskbit == 0) {
  127. mask = *(srcptr++);
  128. maskbit = 8;
  129. srclen--;
  130. continue;
  131. }
  132. if ((mask & (1 << (--maskbit))) == 0) {
  133. *(int*)destptr = *(int*)srcptr;
  134. srclen -= 4;
  135. destptr += 4;
  136. srcptr += 4;
  137. } else {
  138. ofs = *(srcptr++);
  139. cnt = *(srcptr++);
  140. ofs += cnt * 256;;
  141. cnt = ((cnt >> 3) & 0x1f) + 1;
  142. ofs &= 0x7ff;
  143. srclen -= 2;
  144. cnt *= 4;
  145. for (; cnt > 0; cnt--) {
  146. *(destptr) = *(destptr - ofs);
  147. destptr++;
  148. }
  149. }
  150. }
  151. return (destptr - destptr_bak);
  152. }
  153. /*
  154. *
  155. * Decode a frame
  156. *
  157. */
  158. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
  159. {
  160. LclContext * const c = (LclContext *)avctx->priv_data;
  161. unsigned char *encoded = (unsigned char *)buf;
  162. int pixel_ptr;
  163. int row, col;
  164. unsigned char *outptr;
  165. unsigned int width = avctx->width; // Real image width
  166. unsigned int height = avctx->height; // Real image height
  167. unsigned int mszh_dlen;
  168. unsigned char yq, y1q, uq, vq;
  169. int uqvq;
  170. unsigned int mthread_inlen, mthread_outlen;
  171. #ifdef CONFIG_ZLIB
  172. int zret; // Zlib return code
  173. #endif
  174. int len = buf_size;
  175. /* no supplementary picture */
  176. if (buf_size == 0)
  177. return 0;
  178. if(c->pic.data[0])
  179. avctx->release_buffer(avctx, &c->pic);
  180. c->pic.reference = 0;
  181. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  182. if(avctx->get_buffer(avctx, &c->pic) < 0){
  183. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  184. return -1;
  185. }
  186. outptr = c->pic.data[0]; // Output image pointer
  187. /* Decompress frame */
  188. switch (avctx->codec_id) {
  189. case CODEC_ID_MSZH:
  190. switch (c->compression) {
  191. case COMP_MSZH:
  192. if (c->flags & FLAG_MULTITHREAD) {
  193. mthread_inlen = *((unsigned int*)encoded);
  194. mthread_outlen = *((unsigned int*)(encoded+4));
  195. mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf);
  196. if (mthread_outlen != mszh_dlen) {
  197. av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
  198. mthread_outlen, mszh_dlen);
  199. }
  200. mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - mthread_inlen,
  201. c->decomp_buf + mthread_outlen);
  202. if ((c->decomp_size - mthread_outlen) != mszh_dlen) {
  203. av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
  204. c->decomp_size - mthread_outlen, mszh_dlen);
  205. }
  206. encoded = c->decomp_buf;
  207. len = c->decomp_size;
  208. } else {
  209. mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf);
  210. if (c->decomp_size != mszh_dlen) {
  211. av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
  212. c->decomp_size, mszh_dlen);
  213. }
  214. encoded = c->decomp_buf;
  215. len = mszh_dlen;
  216. }
  217. break;
  218. case COMP_MSZH_NOCOMP:
  219. break;
  220. default:
  221. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
  222. return -1;
  223. }
  224. break;
  225. case CODEC_ID_ZLIB:
  226. #ifdef CONFIG_ZLIB
  227. /* Using the original dll with normal compression (-1) and RGB format
  228. * gives a file with ZLIB fourcc, but frame is really uncompressed.
  229. * To be sure that's true check also frame size */
  230. if ((c->compression == COMP_ZLIB_NORMAL) && (c->imgtype == IMGTYPE_RGB24) &&
  231. (len == width * height * 3))
  232. break;
  233. zret = inflateReset(&(c->zstream));
  234. if (zret != Z_OK) {
  235. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  236. return -1;
  237. }
  238. if (c->flags & FLAG_MULTITHREAD) {
  239. mthread_inlen = *((unsigned int*)encoded);
  240. mthread_outlen = *((unsigned int*)(encoded+4));
  241. c->zstream.next_in = encoded + 8;
  242. c->zstream.avail_in = mthread_inlen;
  243. c->zstream.next_out = c->decomp_buf;
  244. c->zstream.avail_out = mthread_outlen;
  245. zret = inflate(&(c->zstream), Z_FINISH);
  246. if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
  247. av_log(avctx, AV_LOG_ERROR, "Mthread1 inflate error: %d\n", zret);
  248. return -1;
  249. }
  250. if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
  251. av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%u != %lu)\n",
  252. mthread_outlen, c->zstream.total_out);
  253. }
  254. zret = inflateReset(&(c->zstream));
  255. if (zret != Z_OK) {
  256. av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate reset error: %d\n", zret);
  257. return -1;
  258. }
  259. c->zstream.next_in = encoded + 8 + mthread_inlen;
  260. c->zstream.avail_in = len - mthread_inlen;
  261. c->zstream.next_out = c->decomp_buf + mthread_outlen;
  262. c->zstream.avail_out = mthread_outlen;
  263. zret = inflate(&(c->zstream), Z_FINISH);
  264. if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
  265. av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate error: %d\n", zret);
  266. return -1;
  267. }
  268. if ((c->decomp_size - mthread_outlen) != (unsigned int)(c->zstream.total_out)) {
  269. av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %lu)\n",
  270. c->decomp_size - mthread_outlen, c->zstream.total_out);
  271. }
  272. } else {
  273. c->zstream.next_in = encoded;
  274. c->zstream.avail_in = len;
  275. c->zstream.next_out = c->decomp_buf;
  276. c->zstream.avail_out = c->decomp_size;
  277. zret = inflate(&(c->zstream), Z_FINISH);
  278. if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
  279. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
  280. return -1;
  281. }
  282. if (c->decomp_size != (unsigned int)(c->zstream.total_out)) {
  283. av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
  284. c->decomp_size, c->zstream.total_out);
  285. }
  286. }
  287. encoded = c->decomp_buf;
  288. len = c->decomp_size;;
  289. #else
  290. av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
  291. return -1;
  292. #endif
  293. break;
  294. default:
  295. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
  296. return -1;
  297. }
  298. /* Apply PNG filter */
  299. if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) {
  300. switch (c->imgtype) {
  301. case IMGTYPE_YUV111:
  302. case IMGTYPE_RGB24:
  303. for (row = 0; row < height; row++) {
  304. pixel_ptr = row * width * 3;
  305. yq = encoded[pixel_ptr++];
  306. uqvq = encoded[pixel_ptr++];
  307. uqvq+=(encoded[pixel_ptr++] << 8);
  308. for (col = 1; col < width; col++) {
  309. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  310. uqvq -= (encoded[pixel_ptr+1] | (encoded[pixel_ptr+2]<<8));
  311. encoded[pixel_ptr+1] = (uqvq) & 0xff;
  312. encoded[pixel_ptr+2] = ((uqvq)>>8) & 0xff;
  313. pixel_ptr += 3;
  314. }
  315. }
  316. break;
  317. case IMGTYPE_YUV422:
  318. for (row = 0; row < height; row++) {
  319. pixel_ptr = row * width * 2;
  320. yq = uq = vq =0;
  321. for (col = 0; col < width/4; col++) {
  322. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  323. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  324. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  325. encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
  326. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  327. encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
  328. encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
  329. encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
  330. pixel_ptr += 8;
  331. }
  332. }
  333. break;
  334. case IMGTYPE_YUV411:
  335. for (row = 0; row < height; row++) {
  336. pixel_ptr = row * width / 2 * 3;
  337. yq = uq = vq =0;
  338. for (col = 0; col < width/4; col++) {
  339. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  340. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  341. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  342. encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
  343. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  344. encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
  345. pixel_ptr += 6;
  346. }
  347. }
  348. break;
  349. case IMGTYPE_YUV211:
  350. for (row = 0; row < height; row++) {
  351. pixel_ptr = row * width * 2;
  352. yq = 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] = uq -= encoded[pixel_ptr+2];
  357. encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
  358. pixel_ptr += 4;
  359. }
  360. }
  361. break;
  362. case IMGTYPE_YUV420:
  363. for (row = 0; row < height/2; row++) {
  364. pixel_ptr = row * width * 3;
  365. yq = y1q = uq = vq =0;
  366. for (col = 0; col < width/2; col++) {
  367. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  368. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  369. encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
  370. encoded[pixel_ptr+3] = y1q -= encoded[pixel_ptr+3];
  371. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  372. encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
  373. pixel_ptr += 6;
  374. }
  375. }
  376. break;
  377. default:
  378. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
  379. return -1;
  380. }
  381. }
  382. /* Convert colorspace */
  383. switch (c->imgtype) {
  384. case IMGTYPE_YUV111:
  385. for (row = height - 1; row >= 0; row--) {
  386. pixel_ptr = row * c->pic.linesize[0];
  387. for (col = 0; col < width; col++) {
  388. outptr[pixel_ptr++] = get_b(encoded[0], encoded[1]);
  389. outptr[pixel_ptr++] = get_g(encoded[0], encoded[1], encoded[2]);
  390. outptr[pixel_ptr++] = get_r(encoded[0], encoded[2]);
  391. encoded += 3;
  392. }
  393. }
  394. break;
  395. case IMGTYPE_YUV422:
  396. for (row = height - 1; row >= 0; row--) {
  397. pixel_ptr = row * c->pic.linesize[0];
  398. for (col = 0; col < width/4; col++) {
  399. outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
  400. outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[6]);
  401. outptr[pixel_ptr++] = get_r(encoded[0], encoded[6]);
  402. outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
  403. outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[6]);
  404. outptr[pixel_ptr++] = get_r(encoded[1], encoded[6]);
  405. outptr[pixel_ptr++] = get_b(encoded[2], encoded[5]);
  406. outptr[pixel_ptr++] = get_g(encoded[2], encoded[5], encoded[7]);
  407. outptr[pixel_ptr++] = get_r(encoded[2], encoded[7]);
  408. outptr[pixel_ptr++] = get_b(encoded[3], encoded[5]);
  409. outptr[pixel_ptr++] = get_g(encoded[3], encoded[5], encoded[7]);
  410. outptr[pixel_ptr++] = get_r(encoded[3], encoded[7]);
  411. encoded += 8;
  412. }
  413. }
  414. break;
  415. case IMGTYPE_RGB24:
  416. for (row = height - 1; row >= 0; row--) {
  417. pixel_ptr = row * c->pic.linesize[0];
  418. for (col = 0; col < width; col++) {
  419. outptr[pixel_ptr++] = encoded[0];
  420. outptr[pixel_ptr++] = encoded[1];
  421. outptr[pixel_ptr++] = encoded[2];
  422. encoded += 3;
  423. }
  424. }
  425. break;
  426. case IMGTYPE_YUV411:
  427. for (row = height - 1; row >= 0; row--) {
  428. pixel_ptr = row * c->pic.linesize[0];
  429. for (col = 0; col < width/4; col++) {
  430. outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
  431. outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[5]);
  432. outptr[pixel_ptr++] = get_r(encoded[0], encoded[5]);
  433. outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
  434. outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[5]);
  435. outptr[pixel_ptr++] = get_r(encoded[1], encoded[5]);
  436. outptr[pixel_ptr++] = get_b(encoded[2], encoded[4]);
  437. outptr[pixel_ptr++] = get_g(encoded[2], encoded[4], encoded[5]);
  438. outptr[pixel_ptr++] = get_r(encoded[2], encoded[5]);
  439. outptr[pixel_ptr++] = get_b(encoded[3], encoded[4]);
  440. outptr[pixel_ptr++] = get_g(encoded[3], encoded[4], encoded[5]);
  441. outptr[pixel_ptr++] = get_r(encoded[3], encoded[5]);
  442. encoded += 6;
  443. }
  444. }
  445. break;
  446. case IMGTYPE_YUV211:
  447. for (row = height - 1; row >= 0; row--) {
  448. pixel_ptr = row * c->pic.linesize[0];
  449. for (col = 0; col < width/2; col++) {
  450. outptr[pixel_ptr++] = get_b(encoded[0], encoded[2]);
  451. outptr[pixel_ptr++] = get_g(encoded[0], encoded[2], encoded[3]);
  452. outptr[pixel_ptr++] = get_r(encoded[0], encoded[3]);
  453. outptr[pixel_ptr++] = get_b(encoded[1], encoded[2]);
  454. outptr[pixel_ptr++] = get_g(encoded[1], encoded[2], encoded[3]);
  455. outptr[pixel_ptr++] = get_r(encoded[1], encoded[3]);
  456. encoded += 4;
  457. }
  458. }
  459. break;
  460. case IMGTYPE_YUV420:
  461. for (row = height / 2 - 1; row >= 0; row--) {
  462. pixel_ptr = 2 * row * c->pic.linesize[0];
  463. for (col = 0; col < width/2; col++) {
  464. outptr[pixel_ptr] = get_b(encoded[0], encoded[4]);
  465. outptr[pixel_ptr+1] = get_g(encoded[0], encoded[4], encoded[5]);
  466. outptr[pixel_ptr+2] = get_r(encoded[0], encoded[5]);
  467. outptr[pixel_ptr+3] = get_b(encoded[1], encoded[4]);
  468. outptr[pixel_ptr+4] = get_g(encoded[1], encoded[4], encoded[5]);
  469. outptr[pixel_ptr+5] = get_r(encoded[1], encoded[5]);
  470. outptr[pixel_ptr-c->pic.linesize[0]] = get_b(encoded[2], encoded[4]);
  471. outptr[pixel_ptr-c->pic.linesize[0]+1] = get_g(encoded[2], encoded[4], encoded[5]);
  472. outptr[pixel_ptr-c->pic.linesize[0]+2] = get_r(encoded[2], encoded[5]);
  473. outptr[pixel_ptr-c->pic.linesize[0]+3] = get_b(encoded[3], encoded[4]);
  474. outptr[pixel_ptr-c->pic.linesize[0]+4] = get_g(encoded[3], encoded[4], encoded[5]);
  475. outptr[pixel_ptr-c->pic.linesize[0]+5] = get_r(encoded[3], encoded[5]);
  476. pixel_ptr += 6;
  477. encoded += 6;
  478. }
  479. }
  480. break;
  481. default:
  482. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
  483. return -1;
  484. }
  485. *data_size = sizeof(AVFrame);
  486. *(AVFrame*)data = c->pic;
  487. /* always report that the buffer was completely consumed */
  488. return buf_size;
  489. }
  490. /*
  491. *
  492. * Encode a frame
  493. *
  494. */
  495. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  496. LclContext *c = avctx->priv_data;
  497. AVFrame *pict = data;
  498. AVFrame * const p = &c->pic;
  499. int i;
  500. int zret; // Zlib return code
  501. #ifndef CONFIG_ZLIB
  502. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled in.\n");
  503. return -1;
  504. #else
  505. init_put_bits(&c->pb, buf, buf_size);
  506. *p = *pict;
  507. p->pict_type= FF_I_TYPE;
  508. p->key_frame= 1;
  509. if(avctx->pix_fmt != PIX_FMT_BGR24){
  510. av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
  511. return -1;
  512. }
  513. zret = deflateReset(&(c->zstream));
  514. if (zret != Z_OK) {
  515. av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
  516. return -1;
  517. }
  518. c->zstream.next_out = c->comp_buf;
  519. c->zstream.avail_out = c->max_comp_size;
  520. for(i = avctx->height - 1; i >= 0; i--) {
  521. c->zstream.next_in = p->data[0]+p->linesize[0]*i;
  522. c->zstream.avail_in = avctx->width*3;
  523. zret = deflate(&(c->zstream), Z_NO_FLUSH);
  524. if (zret != Z_OK) {
  525. av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
  526. return -1;
  527. }
  528. }
  529. zret = deflate(&(c->zstream), Z_FINISH);
  530. if (zret != Z_STREAM_END) {
  531. av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
  532. return -1;
  533. }
  534. for (i = 0; i < c->zstream.total_out; i++)
  535. put_bits(&c->pb, 8, c->comp_buf[i]);
  536. flush_put_bits(&c->pb);
  537. return c->zstream.total_out;
  538. #endif
  539. }
  540. /*
  541. *
  542. * Init lcl decoder
  543. *
  544. */
  545. static int decode_init(AVCodecContext *avctx)
  546. {
  547. LclContext * const c = (LclContext *)avctx->priv_data;
  548. int basesize = avctx->width * avctx->height;
  549. int zret; // Zlib return code
  550. c->avctx = avctx;
  551. avctx->has_b_frames = 0;
  552. c->pic.data[0] = NULL;
  553. #ifdef CONFIG_ZLIB
  554. // Needed if zlib unused or init aborted before inflateInit
  555. memset(&(c->zstream), 0, sizeof(z_stream));
  556. #endif
  557. if (avctx->extradata_size < 8) {
  558. av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
  559. return 1;
  560. }
  561. /* Check codec type */
  562. if (((avctx->codec_id == CODEC_ID_MSZH) && (*((char *)avctx->extradata + 7) != CODEC_MSZH)) ||
  563. ((avctx->codec_id == CODEC_ID_ZLIB) && (*((char *)avctx->extradata + 7) != CODEC_ZLIB))) {
  564. av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
  565. }
  566. /* Detect image type */
  567. switch (c->imgtype = *((char *)avctx->extradata + 4)) {
  568. case IMGTYPE_YUV111:
  569. c->decomp_size = basesize * 3;
  570. av_log(avctx, AV_LOG_INFO, "Image type is YUV 1:1:1.\n");
  571. break;
  572. case IMGTYPE_YUV422:
  573. c->decomp_size = basesize * 2;
  574. av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:2.\n");
  575. break;
  576. case IMGTYPE_RGB24:
  577. c->decomp_size = basesize * 3;
  578. av_log(avctx, AV_LOG_INFO, "Image type is RGB 24.\n");
  579. break;
  580. case IMGTYPE_YUV411:
  581. c->decomp_size = basesize / 2 * 3;
  582. av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:1:1.\n");
  583. break;
  584. case IMGTYPE_YUV211:
  585. c->decomp_size = basesize * 2;
  586. av_log(avctx, AV_LOG_INFO, "Image type is YUV 2:1:1.\n");
  587. break;
  588. case IMGTYPE_YUV420:
  589. c->decomp_size = basesize / 2 * 3;
  590. av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:0.\n");
  591. break;
  592. default:
  593. av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
  594. return 1;
  595. }
  596. /* Detect compression method */
  597. c->compression = *((char *)avctx->extradata + 5);
  598. switch (avctx->codec_id) {
  599. case CODEC_ID_MSZH:
  600. switch (c->compression) {
  601. case COMP_MSZH:
  602. av_log(avctx, AV_LOG_INFO, "Compression enabled.\n");
  603. break;
  604. case COMP_MSZH_NOCOMP:
  605. c->decomp_size = 0;
  606. av_log(avctx, AV_LOG_INFO, "No compression.\n");
  607. break;
  608. default:
  609. av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
  610. return 1;
  611. }
  612. break;
  613. case CODEC_ID_ZLIB:
  614. #ifdef CONFIG_ZLIB
  615. switch (c->compression) {
  616. case COMP_ZLIB_HISPEED:
  617. av_log(avctx, AV_LOG_INFO, "High speed compression.\n");
  618. break;
  619. case COMP_ZLIB_HICOMP:
  620. av_log(avctx, AV_LOG_INFO, "High compression.\n");
  621. break;
  622. case COMP_ZLIB_NORMAL:
  623. av_log(avctx, AV_LOG_INFO, "Normal compression.\n");
  624. break;
  625. default:
  626. if ((c->compression < Z_NO_COMPRESSION) || (c->compression > Z_BEST_COMPRESSION)) {
  627. av_log(avctx, AV_LOG_ERROR, "Unusupported compression level for ZLIB: (%d).\n", c->compression);
  628. return 1;
  629. }
  630. av_log(avctx, AV_LOG_INFO, "Compression level for ZLIB: (%d).\n", c->compression);
  631. }
  632. #else
  633. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  634. return 1;
  635. #endif
  636. break;
  637. default:
  638. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
  639. return 1;
  640. }
  641. /* Allocate decompression buffer */
  642. /* 4*8 max overflow space for mszh decomp algorithm */
  643. if (c->decomp_size) {
  644. if ((c->decomp_buf = av_malloc(c->decomp_size+4*8)) == NULL) {
  645. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  646. return 1;
  647. }
  648. }
  649. /* Detect flags */
  650. c->flags = *((char *)avctx->extradata + 6);
  651. if (c->flags & FLAG_MULTITHREAD)
  652. av_log(avctx, AV_LOG_INFO, "Multithread encoder flag set.\n");
  653. if (c->flags & FLAG_NULLFRAME)
  654. av_log(avctx, AV_LOG_INFO, "Nullframe insertion flag set.\n");
  655. if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER))
  656. av_log(avctx, AV_LOG_INFO, "PNG filter flag set.\n");
  657. if (c->flags & FLAGMASK_UNUSED)
  658. av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
  659. /* If needed init zlib */
  660. if (avctx->codec_id == CODEC_ID_ZLIB) {
  661. #ifdef CONFIG_ZLIB
  662. c->zstream.zalloc = Z_NULL;
  663. c->zstream.zfree = Z_NULL;
  664. c->zstream.opaque = Z_NULL;
  665. zret = inflateInit(&(c->zstream));
  666. if (zret != Z_OK) {
  667. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  668. return 1;
  669. }
  670. #else
  671. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  672. return 1;
  673. #endif
  674. }
  675. avctx->pix_fmt = PIX_FMT_BGR24;
  676. return 0;
  677. }
  678. /*
  679. *
  680. * Init lcl encoder
  681. *
  682. */
  683. static int encode_init(AVCodecContext *avctx)
  684. {
  685. LclContext *c = avctx->priv_data;
  686. int zret; // Zlib return code
  687. #ifndef CONFIG_ZLIB
  688. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  689. return 1;
  690. #else
  691. c->avctx= avctx;
  692. assert(avctx->width && avctx->height);
  693. avctx->extradata= av_mallocz(8);
  694. avctx->coded_frame= &c->pic;
  695. // Will be user settable someday
  696. c->compression = 6;
  697. c->flags = 0;
  698. switch(avctx->pix_fmt){
  699. case PIX_FMT_BGR24:
  700. c->imgtype = IMGTYPE_RGB24;
  701. c->decomp_size = avctx->width * avctx->height * 3;
  702. avctx->bits_per_sample= 24;
  703. break;
  704. default:
  705. av_log(avctx, AV_LOG_ERROR, "Format %d not supported\n", avctx->pix_fmt);
  706. return -1;
  707. }
  708. ((uint8_t*)avctx->extradata)[0]= 4;
  709. ((uint8_t*)avctx->extradata)[1]= 0;
  710. ((uint8_t*)avctx->extradata)[2]= 0;
  711. ((uint8_t*)avctx->extradata)[3]= 0;
  712. ((uint8_t*)avctx->extradata)[4]= c->imgtype;
  713. ((uint8_t*)avctx->extradata)[5]= c->compression;
  714. ((uint8_t*)avctx->extradata)[6]= c->flags;
  715. ((uint8_t*)avctx->extradata)[7]= CODEC_ZLIB;
  716. c->avctx->extradata_size= 8;
  717. c->zstream.zalloc = Z_NULL;
  718. c->zstream.zfree = Z_NULL;
  719. c->zstream.opaque = Z_NULL;
  720. zret = deflateInit(&(c->zstream), c->compression);
  721. if (zret != Z_OK) {
  722. av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret);
  723. return 1;
  724. }
  725. /* Conservative upper bound taken from zlib v1.2.1 source */
  726. c->max_comp_size = c->decomp_size + ((c->decomp_size + 7) >> 3) +
  727. ((c->decomp_size + 63) >> 6) + 11;
  728. if ((c->comp_buf = av_malloc(c->max_comp_size)) == NULL) {
  729. av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
  730. return 1;
  731. }
  732. return 0;
  733. #endif
  734. }
  735. /*
  736. *
  737. * Uninit lcl decoder
  738. *
  739. */
  740. static int decode_end(AVCodecContext *avctx)
  741. {
  742. LclContext * const c = (LclContext *)avctx->priv_data;
  743. if (c->pic.data[0])
  744. avctx->release_buffer(avctx, &c->pic);
  745. #ifdef CONFIG_ZLIB
  746. inflateEnd(&(c->zstream));
  747. #endif
  748. return 0;
  749. }
  750. /*
  751. *
  752. * Uninit lcl encoder
  753. *
  754. */
  755. static int encode_end(AVCodecContext *avctx)
  756. {
  757. LclContext *c = avctx->priv_data;
  758. av_freep(&avctx->extradata);
  759. av_freep(&c->comp_buf);
  760. #ifdef CONFIG_ZLIB
  761. deflateEnd(&(c->zstream));
  762. #endif
  763. return 0;
  764. }
  765. AVCodec mszh_decoder = {
  766. "mszh",
  767. CODEC_TYPE_VIDEO,
  768. CODEC_ID_MSZH,
  769. sizeof(LclContext),
  770. decode_init,
  771. NULL,
  772. decode_end,
  773. decode_frame,
  774. CODEC_CAP_DR1,
  775. };
  776. AVCodec zlib_decoder = {
  777. "zlib",
  778. CODEC_TYPE_VIDEO,
  779. CODEC_ID_ZLIB,
  780. sizeof(LclContext),
  781. decode_init,
  782. NULL,
  783. decode_end,
  784. decode_frame,
  785. CODEC_CAP_DR1,
  786. };
  787. #ifdef CONFIG_ENCODERS
  788. AVCodec zlib_encoder = {
  789. "zlib",
  790. CODEC_TYPE_VIDEO,
  791. CODEC_ID_ZLIB,
  792. sizeof(LclContext),
  793. encode_init,
  794. encode_frame,
  795. encode_end,
  796. // .options = lcl_options,
  797. };
  798. #endif //CONFIG_ENCODERS