lcl.c 32 KB

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