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.

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