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.

925 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. * @file lcl.c
  23. * LCL (LossLess Codec Library) Video Codec
  24. * Decoder for MSZH and ZLIB codecs
  25. * Experimental encoder for ZLIB RGB24
  26. *
  27. * Fourcc: MSZH, ZLIB
  28. *
  29. * Original Win32 dll:
  30. * Ver2.23 By Kenji Oshima 2000.09.20
  31. * avimszh.dll, avizlib.dll
  32. *
  33. * A description of the decoding algorithm can be found here:
  34. * http://www.pcisys.net/~melanson/codecs
  35. *
  36. * Supports: BGR24 (RGB 24bpp)
  37. *
  38. */
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include "avcodec.h"
  42. #include "bitstream.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. #ifdef CONFIG_DECODERS
  160. /*
  161. *
  162. * Decode a frame
  163. *
  164. */
  165. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
  166. {
  167. LclContext * const c = avctx->priv_data;
  168. unsigned char *encoded = (unsigned char *)buf;
  169. unsigned int pixel_ptr;
  170. int row, col;
  171. unsigned char *outptr;
  172. unsigned int width = avctx->width; // Real image width
  173. unsigned int height = avctx->height; // Real image height
  174. unsigned int mszh_dlen;
  175. unsigned char yq, y1q, uq, vq;
  176. int uqvq;
  177. unsigned int mthread_inlen, mthread_outlen;
  178. #ifdef CONFIG_ZLIB
  179. int zret; // Zlib return code
  180. #endif
  181. unsigned int len = buf_size;
  182. if(c->pic.data[0])
  183. avctx->release_buffer(avctx, &c->pic);
  184. c->pic.reference = 0;
  185. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  186. if(avctx->get_buffer(avctx, &c->pic) < 0){
  187. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  188. return -1;
  189. }
  190. outptr = c->pic.data[0]; // Output image pointer
  191. /* Decompress frame */
  192. switch (avctx->codec_id) {
  193. case CODEC_ID_MSZH:
  194. switch (c->compression) {
  195. case COMP_MSZH:
  196. if (c->flags & FLAG_MULTITHREAD) {
  197. mthread_inlen = *((unsigned int*)encoded);
  198. mthread_outlen = *((unsigned int*)(encoded+4));
  199. if (mthread_outlen > c->decomp_size) // this should not happen
  200. mthread_outlen = c->decomp_size;
  201. mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
  202. if (mthread_outlen != mszh_dlen) {
  203. av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
  204. mthread_outlen, mszh_dlen);
  205. return -1;
  206. }
  207. mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - mthread_inlen,
  208. c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
  209. if (mthread_outlen != mszh_dlen) {
  210. av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
  211. mthread_outlen, mszh_dlen);
  212. return -1;
  213. }
  214. encoded = c->decomp_buf;
  215. len = c->decomp_size;
  216. } else {
  217. mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
  218. if (c->decomp_size != mszh_dlen) {
  219. av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
  220. c->decomp_size, mszh_dlen);
  221. return -1;
  222. }
  223. encoded = c->decomp_buf;
  224. len = mszh_dlen;
  225. }
  226. break;
  227. case COMP_MSZH_NOCOMP:
  228. break;
  229. default:
  230. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
  231. return -1;
  232. }
  233. break;
  234. case CODEC_ID_ZLIB:
  235. #ifdef CONFIG_ZLIB
  236. /* Using the original dll with normal compression (-1) and RGB format
  237. * gives a file with ZLIB fourcc, but frame is really uncompressed.
  238. * To be sure that's true check also frame size */
  239. if ((c->compression == COMP_ZLIB_NORMAL) && (c->imgtype == IMGTYPE_RGB24) &&
  240. (len == width * height * 3))
  241. break;
  242. zret = inflateReset(&(c->zstream));
  243. if (zret != Z_OK) {
  244. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  245. return -1;
  246. }
  247. if (c->flags & FLAG_MULTITHREAD) {
  248. mthread_inlen = *((unsigned int*)encoded);
  249. mthread_outlen = *((unsigned int*)(encoded+4));
  250. if (mthread_outlen > c->decomp_size)
  251. mthread_outlen = c->decomp_size;
  252. c->zstream.next_in = encoded + 8;
  253. c->zstream.avail_in = mthread_inlen;
  254. c->zstream.next_out = c->decomp_buf;
  255. c->zstream.avail_out = c->decomp_size;
  256. zret = inflate(&(c->zstream), Z_FINISH);
  257. if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
  258. av_log(avctx, AV_LOG_ERROR, "Mthread1 inflate error: %d\n", zret);
  259. return -1;
  260. }
  261. if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
  262. av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%u != %lu)\n",
  263. mthread_outlen, c->zstream.total_out);
  264. return -1;
  265. }
  266. zret = inflateReset(&(c->zstream));
  267. if (zret != Z_OK) {
  268. av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate reset error: %d\n", zret);
  269. return -1;
  270. }
  271. c->zstream.next_in = encoded + 8 + mthread_inlen;
  272. c->zstream.avail_in = len - mthread_inlen;
  273. c->zstream.next_out = c->decomp_buf + mthread_outlen;
  274. c->zstream.avail_out = c->decomp_size - mthread_outlen;
  275. zret = inflate(&(c->zstream), Z_FINISH);
  276. if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
  277. av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate error: %d\n", zret);
  278. return -1;
  279. }
  280. if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
  281. av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %lu)\n",
  282. mthread_outlen, c->zstream.total_out);
  283. return -1;
  284. }
  285. } else {
  286. c->zstream.next_in = encoded;
  287. c->zstream.avail_in = len;
  288. c->zstream.next_out = c->decomp_buf;
  289. c->zstream.avail_out = c->decomp_size;
  290. zret = inflate(&(c->zstream), Z_FINISH);
  291. if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
  292. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
  293. return -1;
  294. }
  295. if (c->decomp_size != (unsigned int)(c->zstream.total_out)) {
  296. av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
  297. c->decomp_size, c->zstream.total_out);
  298. return -1;
  299. }
  300. }
  301. encoded = c->decomp_buf;
  302. len = c->decomp_size;;
  303. #else
  304. av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
  305. return -1;
  306. #endif
  307. break;
  308. default:
  309. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
  310. return -1;
  311. }
  312. /* Apply PNG filter */
  313. if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) {
  314. switch (c->imgtype) {
  315. case IMGTYPE_YUV111:
  316. case IMGTYPE_RGB24:
  317. for (row = 0; row < height; row++) {
  318. pixel_ptr = row * width * 3;
  319. yq = encoded[pixel_ptr++];
  320. uqvq = AV_RL16(encoded+pixel_ptr);
  321. pixel_ptr += 2;
  322. for (col = 1; col < width; col++) {
  323. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  324. uqvq -= AV_RL16(encoded+pixel_ptr+1);
  325. AV_WL16(encoded+pixel_ptr+1, uqvq);
  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. #endif
  504. #ifdef CONFIG_ENCODERS
  505. /*
  506. *
  507. * Encode a frame
  508. *
  509. */
  510. static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
  511. LclContext *c = avctx->priv_data;
  512. AVFrame *pict = data;
  513. AVFrame * const p = &c->pic;
  514. int i;
  515. int zret; // Zlib return code
  516. #ifndef CONFIG_ZLIB
  517. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled in.\n");
  518. return -1;
  519. #else
  520. init_put_bits(&c->pb, buf, buf_size);
  521. *p = *pict;
  522. p->pict_type= FF_I_TYPE;
  523. p->key_frame= 1;
  524. if(avctx->pix_fmt != PIX_FMT_BGR24){
  525. av_log(avctx, AV_LOG_ERROR, "Format not supported!\n");
  526. return -1;
  527. }
  528. zret = deflateReset(&(c->zstream));
  529. if (zret != Z_OK) {
  530. av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
  531. return -1;
  532. }
  533. c->zstream.next_out = c->comp_buf;
  534. c->zstream.avail_out = c->max_comp_size;
  535. for(i = avctx->height - 1; i >= 0; i--) {
  536. c->zstream.next_in = p->data[0]+p->linesize[0]*i;
  537. c->zstream.avail_in = avctx->width*3;
  538. zret = deflate(&(c->zstream), Z_NO_FLUSH);
  539. if (zret != Z_OK) {
  540. av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
  541. return -1;
  542. }
  543. }
  544. zret = deflate(&(c->zstream), Z_FINISH);
  545. if (zret != Z_STREAM_END) {
  546. av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
  547. return -1;
  548. }
  549. for (i = 0; i < c->zstream.total_out; i++)
  550. put_bits(&c->pb, 8, c->comp_buf[i]);
  551. flush_put_bits(&c->pb);
  552. return c->zstream.total_out;
  553. #endif
  554. }
  555. #endif /* CONFIG_ENCODERS */
  556. #ifdef CONFIG_DECODERS
  557. /*
  558. *
  559. * Init lcl decoder
  560. *
  561. */
  562. static int decode_init(AVCodecContext *avctx)
  563. {
  564. LclContext * const c = avctx->priv_data;
  565. unsigned int basesize = avctx->width * avctx->height;
  566. unsigned int max_basesize = ((avctx->width + 3) & ~3) * ((avctx->height + 3) & ~3);
  567. unsigned int max_decomp_size;
  568. int zret; // Zlib return code
  569. c->avctx = avctx;
  570. c->pic.data[0] = NULL;
  571. #ifdef CONFIG_ZLIB
  572. // Needed if zlib unused or init aborted before inflateInit
  573. memset(&(c->zstream), 0, sizeof(z_stream));
  574. #endif
  575. if (avctx->extradata_size < 8) {
  576. av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
  577. return 1;
  578. }
  579. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
  580. return 1;
  581. }
  582. /* Check codec type */
  583. if (((avctx->codec_id == CODEC_ID_MSZH) && (*((char *)avctx->extradata + 7) != CODEC_MSZH)) ||
  584. ((avctx->codec_id == CODEC_ID_ZLIB) && (*((char *)avctx->extradata + 7) != CODEC_ZLIB))) {
  585. av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
  586. }
  587. /* Detect image type */
  588. switch (c->imgtype = *((char *)avctx->extradata + 4)) {
  589. case IMGTYPE_YUV111:
  590. c->decomp_size = basesize * 3;
  591. max_decomp_size = max_basesize * 3;
  592. av_log(avctx, AV_LOG_INFO, "Image type is YUV 1:1:1.\n");
  593. break;
  594. case IMGTYPE_YUV422:
  595. c->decomp_size = basesize * 2;
  596. max_decomp_size = max_basesize * 2;
  597. av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:2.\n");
  598. break;
  599. case IMGTYPE_RGB24:
  600. c->decomp_size = basesize * 3;
  601. max_decomp_size = max_basesize * 3;
  602. av_log(avctx, AV_LOG_INFO, "Image type is RGB 24.\n");
  603. break;
  604. case IMGTYPE_YUV411:
  605. c->decomp_size = basesize / 2 * 3;
  606. max_decomp_size = max_basesize / 2 * 3;
  607. av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:1:1.\n");
  608. break;
  609. case IMGTYPE_YUV211:
  610. c->decomp_size = basesize * 2;
  611. max_decomp_size = max_basesize * 2;
  612. av_log(avctx, AV_LOG_INFO, "Image type is YUV 2:1:1.\n");
  613. break;
  614. case IMGTYPE_YUV420:
  615. c->decomp_size = basesize / 2 * 3;
  616. max_decomp_size = max_basesize / 2 * 3;
  617. av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:0.\n");
  618. break;
  619. default:
  620. av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
  621. return 1;
  622. }
  623. /* Detect compression method */
  624. c->compression = *((char *)avctx->extradata + 5);
  625. switch (avctx->codec_id) {
  626. case CODEC_ID_MSZH:
  627. switch (c->compression) {
  628. case COMP_MSZH:
  629. av_log(avctx, AV_LOG_INFO, "Compression enabled.\n");
  630. break;
  631. case COMP_MSZH_NOCOMP:
  632. c->decomp_size = 0;
  633. av_log(avctx, AV_LOG_INFO, "No compression.\n");
  634. break;
  635. default:
  636. av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
  637. return 1;
  638. }
  639. break;
  640. case CODEC_ID_ZLIB:
  641. #ifdef CONFIG_ZLIB
  642. switch (c->compression) {
  643. case COMP_ZLIB_HISPEED:
  644. av_log(avctx, AV_LOG_INFO, "High speed compression.\n");
  645. break;
  646. case COMP_ZLIB_HICOMP:
  647. av_log(avctx, AV_LOG_INFO, "High compression.\n");
  648. break;
  649. case COMP_ZLIB_NORMAL:
  650. av_log(avctx, AV_LOG_INFO, "Normal compression.\n");
  651. break;
  652. default:
  653. if ((c->compression < Z_NO_COMPRESSION) || (c->compression > Z_BEST_COMPRESSION)) {
  654. av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
  655. return 1;
  656. }
  657. av_log(avctx, AV_LOG_INFO, "Compression level for ZLIB: (%d).\n", c->compression);
  658. }
  659. #else
  660. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  661. return 1;
  662. #endif
  663. break;
  664. default:
  665. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
  666. return 1;
  667. }
  668. /* Allocate decompression buffer */
  669. if (c->decomp_size) {
  670. if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
  671. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  672. return 1;
  673. }
  674. }
  675. /* Detect flags */
  676. c->flags = *((char *)avctx->extradata + 6);
  677. if (c->flags & FLAG_MULTITHREAD)
  678. av_log(avctx, AV_LOG_INFO, "Multithread encoder flag set.\n");
  679. if (c->flags & FLAG_NULLFRAME)
  680. av_log(avctx, AV_LOG_INFO, "Nullframe insertion flag set.\n");
  681. if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER))
  682. av_log(avctx, AV_LOG_INFO, "PNG filter flag set.\n");
  683. if (c->flags & FLAGMASK_UNUSED)
  684. av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
  685. /* If needed init zlib */
  686. if (avctx->codec_id == CODEC_ID_ZLIB) {
  687. #ifdef CONFIG_ZLIB
  688. c->zstream.zalloc = Z_NULL;
  689. c->zstream.zfree = Z_NULL;
  690. c->zstream.opaque = Z_NULL;
  691. zret = inflateInit(&(c->zstream));
  692. if (zret != Z_OK) {
  693. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  694. return 1;
  695. }
  696. #else
  697. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  698. return 1;
  699. #endif
  700. }
  701. avctx->pix_fmt = PIX_FMT_BGR24;
  702. return 0;
  703. }
  704. #endif /* CONFIG_DECODERS */
  705. #ifdef CONFIG_ENCODERS
  706. /*
  707. *
  708. * Init lcl encoder
  709. *
  710. */
  711. static int encode_init(AVCodecContext *avctx)
  712. {
  713. LclContext *c = avctx->priv_data;
  714. int zret; // Zlib return code
  715. #ifndef CONFIG_ZLIB
  716. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  717. return 1;
  718. #else
  719. c->avctx= avctx;
  720. assert(avctx->width && avctx->height);
  721. avctx->extradata= av_mallocz(8);
  722. avctx->coded_frame= &c->pic;
  723. // Will be user settable someday
  724. c->compression = 6;
  725. c->flags = 0;
  726. switch(avctx->pix_fmt){
  727. case PIX_FMT_BGR24:
  728. c->imgtype = IMGTYPE_RGB24;
  729. c->decomp_size = avctx->width * avctx->height * 3;
  730. avctx->bits_per_sample= 24;
  731. break;
  732. default:
  733. av_log(avctx, AV_LOG_ERROR, "Format %d not supported\n", avctx->pix_fmt);
  734. return -1;
  735. }
  736. ((uint8_t*)avctx->extradata)[0]= 4;
  737. ((uint8_t*)avctx->extradata)[1]= 0;
  738. ((uint8_t*)avctx->extradata)[2]= 0;
  739. ((uint8_t*)avctx->extradata)[3]= 0;
  740. ((uint8_t*)avctx->extradata)[4]= c->imgtype;
  741. ((uint8_t*)avctx->extradata)[5]= c->compression;
  742. ((uint8_t*)avctx->extradata)[6]= c->flags;
  743. ((uint8_t*)avctx->extradata)[7]= CODEC_ZLIB;
  744. c->avctx->extradata_size= 8;
  745. c->zstream.zalloc = Z_NULL;
  746. c->zstream.zfree = Z_NULL;
  747. c->zstream.opaque = Z_NULL;
  748. zret = deflateInit(&(c->zstream), c->compression);
  749. if (zret != Z_OK) {
  750. av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret);
  751. return 1;
  752. }
  753. /* Conservative upper bound taken from zlib v1.2.1 source */
  754. c->max_comp_size = c->decomp_size + ((c->decomp_size + 7) >> 3) +
  755. ((c->decomp_size + 63) >> 6) + 11;
  756. if ((c->comp_buf = av_malloc(c->max_comp_size)) == NULL) {
  757. av_log(avctx, AV_LOG_ERROR, "Can't allocate compression buffer.\n");
  758. return 1;
  759. }
  760. return 0;
  761. #endif
  762. }
  763. #endif /* CONFIG_ENCODERS */
  764. #ifdef CONFIG_DECODERS
  765. /*
  766. *
  767. * Uninit lcl decoder
  768. *
  769. */
  770. static int decode_end(AVCodecContext *avctx)
  771. {
  772. LclContext * const c = avctx->priv_data;
  773. if (c->pic.data[0])
  774. avctx->release_buffer(avctx, &c->pic);
  775. #ifdef CONFIG_ZLIB
  776. inflateEnd(&(c->zstream));
  777. #endif
  778. return 0;
  779. }
  780. #endif
  781. #ifdef CONFIG_ENCODERS
  782. /*
  783. *
  784. * Uninit lcl encoder
  785. *
  786. */
  787. static int encode_end(AVCodecContext *avctx)
  788. {
  789. LclContext *c = avctx->priv_data;
  790. av_freep(&avctx->extradata);
  791. av_freep(&c->comp_buf);
  792. #ifdef CONFIG_ZLIB
  793. deflateEnd(&(c->zstream));
  794. #endif
  795. return 0;
  796. }
  797. #endif
  798. #ifdef CONFIG_MSZH_DECODER
  799. AVCodec mszh_decoder = {
  800. "mszh",
  801. CODEC_TYPE_VIDEO,
  802. CODEC_ID_MSZH,
  803. sizeof(LclContext),
  804. decode_init,
  805. NULL,
  806. decode_end,
  807. decode_frame,
  808. CODEC_CAP_DR1,
  809. };
  810. #endif
  811. #ifdef CONFIG_ZLIB_DECODER
  812. AVCodec zlib_decoder = {
  813. "zlib",
  814. CODEC_TYPE_VIDEO,
  815. CODEC_ID_ZLIB,
  816. sizeof(LclContext),
  817. decode_init,
  818. NULL,
  819. decode_end,
  820. decode_frame,
  821. CODEC_CAP_DR1,
  822. };
  823. #endif
  824. #ifdef CONFIG_ENCODERS
  825. AVCodec zlib_encoder = {
  826. "zlib",
  827. CODEC_TYPE_VIDEO,
  828. CODEC_ID_ZLIB,
  829. sizeof(LclContext),
  830. encode_init,
  831. encode_frame,
  832. encode_end,
  833. };
  834. #endif //CONFIG_ENCODERS