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.

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