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.

926 lines
31KB

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