lcldec.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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 libavcodec/lcldec.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. #include "lcl.h"
  44. #if CONFIG_ZLIB
  45. #include <zlib.h>
  46. #endif
  47. /*
  48. * Decoder context
  49. */
  50. typedef struct LclDecContext {
  51. AVFrame pic;
  52. // Image type
  53. int imgtype;
  54. // Compression type
  55. int compression;
  56. // Flags
  57. int flags;
  58. // Decompressed data size
  59. unsigned int decomp_size;
  60. // Decompression buffer
  61. unsigned char* decomp_buf;
  62. #if CONFIG_ZLIB
  63. z_stream zstream;
  64. #endif
  65. } LclDecContext;
  66. /*
  67. *
  68. * Helper functions
  69. *
  70. */
  71. static inline unsigned char fix (int pix14)
  72. {
  73. int tmp;
  74. tmp = (pix14 + 0x80000) >> 20;
  75. if (tmp < 0)
  76. return 0;
  77. if (tmp > 255)
  78. return 255;
  79. return tmp;
  80. }
  81. static inline unsigned char get_b (unsigned char yq, signed char bq)
  82. {
  83. return fix((yq << 20) + bq * 1858076);
  84. }
  85. static inline unsigned char get_g (unsigned char yq, signed char bq, signed char rq)
  86. {
  87. return fix((yq << 20) - bq * 360857 - rq * 748830);
  88. }
  89. static inline unsigned char get_r (unsigned char yq, signed char rq)
  90. {
  91. return fix((yq << 20) + rq * 1470103);
  92. }
  93. static unsigned int mszh_decomp(unsigned char * srcptr, int srclen, unsigned char * destptr, unsigned int destsize)
  94. {
  95. unsigned char *destptr_bak = destptr;
  96. unsigned char *destptr_end = destptr + destsize;
  97. unsigned char mask = 0;
  98. unsigned char maskbit = 0;
  99. unsigned int ofs, cnt;
  100. while ((srclen > 0) && (destptr < destptr_end)) {
  101. if (maskbit == 0) {
  102. mask = *(srcptr++);
  103. maskbit = 8;
  104. srclen--;
  105. continue;
  106. }
  107. if ((mask & (1 << (--maskbit))) == 0) {
  108. if (destptr + 4 > destptr_end)
  109. break;
  110. *(int*)destptr = *(int*)srcptr;
  111. srclen -= 4;
  112. destptr += 4;
  113. srcptr += 4;
  114. } else {
  115. ofs = *(srcptr++);
  116. cnt = *(srcptr++);
  117. ofs += cnt * 256;
  118. cnt = ((cnt >> 3) & 0x1f) + 1;
  119. ofs &= 0x7ff;
  120. srclen -= 2;
  121. cnt *= 4;
  122. if (destptr + cnt > destptr_end) {
  123. cnt = destptr_end - destptr;
  124. }
  125. for (; cnt > 0; cnt--) {
  126. *(destptr) = *(destptr - ofs);
  127. destptr++;
  128. }
  129. }
  130. }
  131. return (destptr - destptr_bak);
  132. }
  133. /*
  134. *
  135. * Decode a frame
  136. *
  137. */
  138. static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size)
  139. {
  140. LclDecContext * const c = avctx->priv_data;
  141. unsigned char *encoded = (unsigned char *)buf;
  142. unsigned int pixel_ptr;
  143. int row, col;
  144. unsigned char *outptr;
  145. unsigned int width = avctx->width; // Real image width
  146. unsigned int height = avctx->height; // Real image height
  147. unsigned int mszh_dlen;
  148. unsigned char yq, y1q, uq, vq;
  149. int uqvq;
  150. unsigned int mthread_inlen, mthread_outlen;
  151. #if CONFIG_ZLIB
  152. int zret; // Zlib return code
  153. #endif
  154. unsigned int len = buf_size;
  155. if(c->pic.data[0])
  156. avctx->release_buffer(avctx, &c->pic);
  157. c->pic.reference = 0;
  158. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
  159. if(avctx->get_buffer(avctx, &c->pic) < 0){
  160. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  161. return -1;
  162. }
  163. outptr = c->pic.data[0]; // Output image pointer
  164. /* Decompress frame */
  165. switch (avctx->codec_id) {
  166. case CODEC_ID_MSZH:
  167. switch (c->compression) {
  168. case COMP_MSZH:
  169. if (c->flags & FLAG_MULTITHREAD) {
  170. mthread_inlen = *((unsigned int*)encoded);
  171. mthread_outlen = *((unsigned int*)(encoded+4));
  172. if (mthread_outlen > c->decomp_size) // this should not happen
  173. mthread_outlen = c->decomp_size;
  174. mszh_dlen = mszh_decomp(encoded + 8, mthread_inlen, c->decomp_buf, c->decomp_size);
  175. if (mthread_outlen != mszh_dlen) {
  176. av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%d != %d)\n",
  177. mthread_outlen, mszh_dlen);
  178. return -1;
  179. }
  180. mszh_dlen = mszh_decomp(encoded + 8 + mthread_inlen, len - mthread_inlen,
  181. c->decomp_buf + mthread_outlen, c->decomp_size - mthread_outlen);
  182. if (mthread_outlen != mszh_dlen) {
  183. av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %d)\n",
  184. mthread_outlen, mszh_dlen);
  185. return -1;
  186. }
  187. encoded = c->decomp_buf;
  188. len = c->decomp_size;
  189. } else {
  190. mszh_dlen = mszh_decomp(encoded, len, c->decomp_buf, c->decomp_size);
  191. if (c->decomp_size != mszh_dlen) {
  192. av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %d)\n",
  193. c->decomp_size, mszh_dlen);
  194. return -1;
  195. }
  196. encoded = c->decomp_buf;
  197. len = mszh_dlen;
  198. }
  199. break;
  200. case COMP_MSZH_NOCOMP:
  201. break;
  202. default:
  203. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown MSZH compression in frame decoder.\n");
  204. return -1;
  205. }
  206. break;
  207. case CODEC_ID_ZLIB:
  208. #if CONFIG_ZLIB
  209. /* Using the original dll with normal compression (-1) and RGB format
  210. * gives a file with ZLIB fourcc, but frame is really uncompressed.
  211. * To be sure that's true check also frame size */
  212. if ((c->compression == COMP_ZLIB_NORMAL) && (c->imgtype == IMGTYPE_RGB24) &&
  213. (len == width * height * 3))
  214. break;
  215. zret = inflateReset(&(c->zstream));
  216. if (zret != Z_OK) {
  217. av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
  218. return -1;
  219. }
  220. if (c->flags & FLAG_MULTITHREAD) {
  221. mthread_inlen = *((unsigned int*)encoded);
  222. mthread_outlen = *((unsigned int*)(encoded+4));
  223. if (mthread_outlen > c->decomp_size)
  224. mthread_outlen = c->decomp_size;
  225. c->zstream.next_in = encoded + 8;
  226. c->zstream.avail_in = mthread_inlen;
  227. c->zstream.next_out = c->decomp_buf;
  228. c->zstream.avail_out = c->decomp_size;
  229. zret = inflate(&(c->zstream), Z_FINISH);
  230. if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
  231. av_log(avctx, AV_LOG_ERROR, "Mthread1 inflate error: %d\n", zret);
  232. return -1;
  233. }
  234. if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
  235. av_log(avctx, AV_LOG_ERROR, "Mthread1 decoded size differs (%u != %lu)\n",
  236. mthread_outlen, c->zstream.total_out);
  237. return -1;
  238. }
  239. zret = inflateReset(&(c->zstream));
  240. if (zret != Z_OK) {
  241. av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate reset error: %d\n", zret);
  242. return -1;
  243. }
  244. c->zstream.next_in = encoded + 8 + mthread_inlen;
  245. c->zstream.avail_in = len - mthread_inlen;
  246. c->zstream.next_out = c->decomp_buf + mthread_outlen;
  247. c->zstream.avail_out = c->decomp_size - mthread_outlen;
  248. zret = inflate(&(c->zstream), Z_FINISH);
  249. if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
  250. av_log(avctx, AV_LOG_ERROR, "Mthread2 inflate error: %d\n", zret);
  251. return -1;
  252. }
  253. if (mthread_outlen != (unsigned int)(c->zstream.total_out)) {
  254. av_log(avctx, AV_LOG_ERROR, "Mthread2 decoded size differs (%d != %lu)\n",
  255. mthread_outlen, c->zstream.total_out);
  256. return -1;
  257. }
  258. } else {
  259. c->zstream.next_in = encoded;
  260. c->zstream.avail_in = len;
  261. c->zstream.next_out = c->decomp_buf;
  262. c->zstream.avail_out = c->decomp_size;
  263. zret = inflate(&(c->zstream), Z_FINISH);
  264. if ((zret != Z_OK) && (zret != Z_STREAM_END)) {
  265. av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
  266. return -1;
  267. }
  268. if (c->decomp_size != (unsigned int)(c->zstream.total_out)) {
  269. av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
  270. c->decomp_size, c->zstream.total_out);
  271. return -1;
  272. }
  273. }
  274. encoded = c->decomp_buf;
  275. len = c->decomp_size;
  276. #else
  277. av_log(avctx, AV_LOG_ERROR, "BUG! Zlib support not compiled in frame decoder.\n");
  278. return -1;
  279. #endif
  280. break;
  281. default:
  282. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in frame decoder compression switch.\n");
  283. return -1;
  284. }
  285. /* Apply PNG filter */
  286. if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER)) {
  287. switch (c->imgtype) {
  288. case IMGTYPE_YUV111:
  289. case IMGTYPE_RGB24:
  290. for (row = 0; row < height; row++) {
  291. pixel_ptr = row * width * 3;
  292. yq = encoded[pixel_ptr++];
  293. uqvq = AV_RL16(encoded+pixel_ptr);
  294. pixel_ptr += 2;
  295. for (col = 1; col < width; col++) {
  296. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  297. uqvq -= AV_RL16(encoded+pixel_ptr+1);
  298. AV_WL16(encoded+pixel_ptr+1, uqvq);
  299. pixel_ptr += 3;
  300. }
  301. }
  302. break;
  303. case IMGTYPE_YUV422:
  304. for (row = 0; row < height; row++) {
  305. pixel_ptr = row * width * 2;
  306. yq = uq = vq =0;
  307. for (col = 0; col < width/4; col++) {
  308. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  309. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  310. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  311. encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
  312. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  313. encoded[pixel_ptr+5] = uq -= encoded[pixel_ptr+5];
  314. encoded[pixel_ptr+6] = vq -= encoded[pixel_ptr+6];
  315. encoded[pixel_ptr+7] = vq -= encoded[pixel_ptr+7];
  316. pixel_ptr += 8;
  317. }
  318. }
  319. break;
  320. case IMGTYPE_YUV411:
  321. for (row = 0; row < height; row++) {
  322. pixel_ptr = row * width / 2 * 3;
  323. yq = uq = vq =0;
  324. for (col = 0; col < width/4; col++) {
  325. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  326. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  327. encoded[pixel_ptr+2] = yq -= encoded[pixel_ptr+2];
  328. encoded[pixel_ptr+3] = yq -= encoded[pixel_ptr+3];
  329. encoded[pixel_ptr+4] = uq -= encoded[pixel_ptr+4];
  330. encoded[pixel_ptr+5] = vq -= encoded[pixel_ptr+5];
  331. pixel_ptr += 6;
  332. }
  333. }
  334. break;
  335. case IMGTYPE_YUV211:
  336. for (row = 0; row < height; row++) {
  337. pixel_ptr = row * width * 2;
  338. yq = uq = vq =0;
  339. for (col = 0; col < width/2; col++) {
  340. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  341. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  342. encoded[pixel_ptr+2] = uq -= encoded[pixel_ptr+2];
  343. encoded[pixel_ptr+3] = vq -= encoded[pixel_ptr+3];
  344. pixel_ptr += 4;
  345. }
  346. }
  347. break;
  348. case IMGTYPE_YUV420:
  349. for (row = 0; row < height/2; row++) {
  350. pixel_ptr = row * width * 3;
  351. yq = y1q = uq = vq =0;
  352. for (col = 0; col < width/2; col++) {
  353. encoded[pixel_ptr] = yq -= encoded[pixel_ptr];
  354. encoded[pixel_ptr+1] = yq -= encoded[pixel_ptr+1];
  355. encoded[pixel_ptr+2] = y1q -= encoded[pixel_ptr+2];
  356. encoded[pixel_ptr+3] = y1q -= 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. default:
  364. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in pngfilter switch.\n");
  365. return -1;
  366. }
  367. }
  368. /* Convert colorspace */
  369. switch (c->imgtype) {
  370. case IMGTYPE_YUV111:
  371. for (row = height - 1; row >= 0; row--) {
  372. pixel_ptr = row * c->pic.linesize[0];
  373. for (col = 0; col < width; col++) {
  374. outptr[pixel_ptr++] = get_b(encoded[0], encoded[1]);
  375. outptr[pixel_ptr++] = get_g(encoded[0], encoded[1], encoded[2]);
  376. outptr[pixel_ptr++] = get_r(encoded[0], encoded[2]);
  377. encoded += 3;
  378. }
  379. }
  380. break;
  381. case IMGTYPE_YUV422:
  382. for (row = height - 1; row >= 0; row--) {
  383. pixel_ptr = row * c->pic.linesize[0];
  384. for (col = 0; col < width/4; col++) {
  385. outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
  386. outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[6]);
  387. outptr[pixel_ptr++] = get_r(encoded[0], encoded[6]);
  388. outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
  389. outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[6]);
  390. outptr[pixel_ptr++] = get_r(encoded[1], encoded[6]);
  391. outptr[pixel_ptr++] = get_b(encoded[2], encoded[5]);
  392. outptr[pixel_ptr++] = get_g(encoded[2], encoded[5], encoded[7]);
  393. outptr[pixel_ptr++] = get_r(encoded[2], encoded[7]);
  394. outptr[pixel_ptr++] = get_b(encoded[3], encoded[5]);
  395. outptr[pixel_ptr++] = get_g(encoded[3], encoded[5], encoded[7]);
  396. outptr[pixel_ptr++] = get_r(encoded[3], encoded[7]);
  397. encoded += 8;
  398. }
  399. }
  400. break;
  401. case IMGTYPE_RGB24:
  402. for (row = height - 1; row >= 0; row--) {
  403. pixel_ptr = row * c->pic.linesize[0];
  404. for (col = 0; col < width; col++) {
  405. outptr[pixel_ptr++] = encoded[0];
  406. outptr[pixel_ptr++] = encoded[1];
  407. outptr[pixel_ptr++] = encoded[2];
  408. encoded += 3;
  409. }
  410. }
  411. break;
  412. case IMGTYPE_YUV411:
  413. for (row = height - 1; row >= 0; row--) {
  414. pixel_ptr = row * c->pic.linesize[0];
  415. for (col = 0; col < width/4; col++) {
  416. outptr[pixel_ptr++] = get_b(encoded[0], encoded[4]);
  417. outptr[pixel_ptr++] = get_g(encoded[0], encoded[4], encoded[5]);
  418. outptr[pixel_ptr++] = get_r(encoded[0], encoded[5]);
  419. outptr[pixel_ptr++] = get_b(encoded[1], encoded[4]);
  420. outptr[pixel_ptr++] = get_g(encoded[1], encoded[4], encoded[5]);
  421. outptr[pixel_ptr++] = get_r(encoded[1], encoded[5]);
  422. outptr[pixel_ptr++] = get_b(encoded[2], encoded[4]);
  423. outptr[pixel_ptr++] = get_g(encoded[2], encoded[4], encoded[5]);
  424. outptr[pixel_ptr++] = get_r(encoded[2], encoded[5]);
  425. outptr[pixel_ptr++] = get_b(encoded[3], encoded[4]);
  426. outptr[pixel_ptr++] = get_g(encoded[3], encoded[4], encoded[5]);
  427. outptr[pixel_ptr++] = get_r(encoded[3], encoded[5]);
  428. encoded += 6;
  429. }
  430. }
  431. break;
  432. case IMGTYPE_YUV211:
  433. for (row = height - 1; row >= 0; row--) {
  434. pixel_ptr = row * c->pic.linesize[0];
  435. for (col = 0; col < width/2; col++) {
  436. outptr[pixel_ptr++] = get_b(encoded[0], encoded[2]);
  437. outptr[pixel_ptr++] = get_g(encoded[0], encoded[2], encoded[3]);
  438. outptr[pixel_ptr++] = get_r(encoded[0], encoded[3]);
  439. outptr[pixel_ptr++] = get_b(encoded[1], encoded[2]);
  440. outptr[pixel_ptr++] = get_g(encoded[1], encoded[2], encoded[3]);
  441. outptr[pixel_ptr++] = get_r(encoded[1], encoded[3]);
  442. encoded += 4;
  443. }
  444. }
  445. break;
  446. case IMGTYPE_YUV420:
  447. for (row = height / 2 - 1; row >= 0; row--) {
  448. pixel_ptr = 2 * row * c->pic.linesize[0];
  449. for (col = 0; col < width/2; col++) {
  450. outptr[pixel_ptr] = get_b(encoded[0], encoded[4]);
  451. outptr[pixel_ptr+1] = get_g(encoded[0], encoded[4], encoded[5]);
  452. outptr[pixel_ptr+2] = get_r(encoded[0], encoded[5]);
  453. outptr[pixel_ptr+3] = get_b(encoded[1], encoded[4]);
  454. outptr[pixel_ptr+4] = get_g(encoded[1], encoded[4], encoded[5]);
  455. outptr[pixel_ptr+5] = get_r(encoded[1], encoded[5]);
  456. outptr[pixel_ptr-c->pic.linesize[0]] = get_b(encoded[2], encoded[4]);
  457. outptr[pixel_ptr-c->pic.linesize[0]+1] = get_g(encoded[2], encoded[4], encoded[5]);
  458. outptr[pixel_ptr-c->pic.linesize[0]+2] = get_r(encoded[2], encoded[5]);
  459. outptr[pixel_ptr-c->pic.linesize[0]+3] = get_b(encoded[3], encoded[4]);
  460. outptr[pixel_ptr-c->pic.linesize[0]+4] = get_g(encoded[3], encoded[4], encoded[5]);
  461. outptr[pixel_ptr-c->pic.linesize[0]+5] = get_r(encoded[3], encoded[5]);
  462. pixel_ptr += 6;
  463. encoded += 6;
  464. }
  465. }
  466. break;
  467. default:
  468. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown imagetype in image decoder.\n");
  469. return -1;
  470. }
  471. *data_size = sizeof(AVFrame);
  472. *(AVFrame*)data = c->pic;
  473. /* always report that the buffer was completely consumed */
  474. return buf_size;
  475. }
  476. /*
  477. *
  478. * Init lcl decoder
  479. *
  480. */
  481. static av_cold int decode_init(AVCodecContext *avctx)
  482. {
  483. LclDecContext * const c = avctx->priv_data;
  484. unsigned int basesize = avctx->width * avctx->height;
  485. unsigned int max_basesize = ((avctx->width + 3) & ~3) * ((avctx->height + 3) & ~3);
  486. unsigned int max_decomp_size;
  487. int zret; // Zlib return code
  488. c->pic.data[0] = NULL;
  489. #if CONFIG_ZLIB
  490. // Needed if zlib unused or init aborted before inflateInit
  491. memset(&(c->zstream), 0, sizeof(z_stream));
  492. #endif
  493. if (avctx->extradata_size < 8) {
  494. av_log(avctx, AV_LOG_ERROR, "Extradata size too small.\n");
  495. return 1;
  496. }
  497. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
  498. return 1;
  499. }
  500. /* Check codec type */
  501. if (((avctx->codec_id == CODEC_ID_MSZH) && (*((char *)avctx->extradata + 7) != CODEC_MSZH)) ||
  502. ((avctx->codec_id == CODEC_ID_ZLIB) && (*((char *)avctx->extradata + 7) != CODEC_ZLIB))) {
  503. av_log(avctx, AV_LOG_ERROR, "Codec id and codec type mismatch. This should not happen.\n");
  504. }
  505. /* Detect image type */
  506. switch (c->imgtype = *((char *)avctx->extradata + 4)) {
  507. case IMGTYPE_YUV111:
  508. c->decomp_size = basesize * 3;
  509. max_decomp_size = max_basesize * 3;
  510. av_log(avctx, AV_LOG_INFO, "Image type is YUV 1:1:1.\n");
  511. break;
  512. case IMGTYPE_YUV422:
  513. c->decomp_size = basesize * 2;
  514. max_decomp_size = max_basesize * 2;
  515. av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:2.\n");
  516. break;
  517. case IMGTYPE_RGB24:
  518. c->decomp_size = basesize * 3;
  519. max_decomp_size = max_basesize * 3;
  520. av_log(avctx, AV_LOG_INFO, "Image type is RGB 24.\n");
  521. break;
  522. case IMGTYPE_YUV411:
  523. c->decomp_size = basesize / 2 * 3;
  524. max_decomp_size = max_basesize / 2 * 3;
  525. av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:1:1.\n");
  526. break;
  527. case IMGTYPE_YUV211:
  528. c->decomp_size = basesize * 2;
  529. max_decomp_size = max_basesize * 2;
  530. av_log(avctx, AV_LOG_INFO, "Image type is YUV 2:1:1.\n");
  531. break;
  532. case IMGTYPE_YUV420:
  533. c->decomp_size = basesize / 2 * 3;
  534. max_decomp_size = max_basesize / 2 * 3;
  535. av_log(avctx, AV_LOG_INFO, "Image type is YUV 4:2:0.\n");
  536. break;
  537. default:
  538. av_log(avctx, AV_LOG_ERROR, "Unsupported image format %d.\n", c->imgtype);
  539. return 1;
  540. }
  541. /* Detect compression method */
  542. c->compression = *((char *)avctx->extradata + 5);
  543. switch (avctx->codec_id) {
  544. case CODEC_ID_MSZH:
  545. switch (c->compression) {
  546. case COMP_MSZH:
  547. av_log(avctx, AV_LOG_INFO, "Compression enabled.\n");
  548. break;
  549. case COMP_MSZH_NOCOMP:
  550. c->decomp_size = 0;
  551. av_log(avctx, AV_LOG_INFO, "No compression.\n");
  552. break;
  553. default:
  554. av_log(avctx, AV_LOG_ERROR, "Unsupported compression format for MSZH (%d).\n", c->compression);
  555. return 1;
  556. }
  557. break;
  558. case CODEC_ID_ZLIB:
  559. #if CONFIG_ZLIB
  560. switch (c->compression) {
  561. case COMP_ZLIB_HISPEED:
  562. av_log(avctx, AV_LOG_INFO, "High speed compression.\n");
  563. break;
  564. case COMP_ZLIB_HICOMP:
  565. av_log(avctx, AV_LOG_INFO, "High compression.\n");
  566. break;
  567. case COMP_ZLIB_NORMAL:
  568. av_log(avctx, AV_LOG_INFO, "Normal compression.\n");
  569. break;
  570. default:
  571. if ((c->compression < Z_NO_COMPRESSION) || (c->compression > Z_BEST_COMPRESSION)) {
  572. av_log(avctx, AV_LOG_ERROR, "Unsupported compression level for ZLIB: (%d).\n", c->compression);
  573. return 1;
  574. }
  575. av_log(avctx, AV_LOG_INFO, "Compression level for ZLIB: (%d).\n", c->compression);
  576. }
  577. #else
  578. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  579. return 1;
  580. #endif
  581. break;
  582. default:
  583. av_log(avctx, AV_LOG_ERROR, "BUG! Unknown codec in compression switch.\n");
  584. return 1;
  585. }
  586. /* Allocate decompression buffer */
  587. if (c->decomp_size) {
  588. if ((c->decomp_buf = av_malloc(max_decomp_size)) == NULL) {
  589. av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
  590. return 1;
  591. }
  592. }
  593. /* Detect flags */
  594. c->flags = *((char *)avctx->extradata + 6);
  595. if (c->flags & FLAG_MULTITHREAD)
  596. av_log(avctx, AV_LOG_INFO, "Multithread encoder flag set.\n");
  597. if (c->flags & FLAG_NULLFRAME)
  598. av_log(avctx, AV_LOG_INFO, "Nullframe insertion flag set.\n");
  599. if ((avctx->codec_id == CODEC_ID_ZLIB) && (c->flags & FLAG_PNGFILTER))
  600. av_log(avctx, AV_LOG_INFO, "PNG filter flag set.\n");
  601. if (c->flags & FLAGMASK_UNUSED)
  602. av_log(avctx, AV_LOG_ERROR, "Unknown flag set (%d).\n", c->flags);
  603. /* If needed init zlib */
  604. if (avctx->codec_id == CODEC_ID_ZLIB) {
  605. #if CONFIG_ZLIB
  606. c->zstream.zalloc = Z_NULL;
  607. c->zstream.zfree = Z_NULL;
  608. c->zstream.opaque = Z_NULL;
  609. zret = inflateInit(&(c->zstream));
  610. if (zret != Z_OK) {
  611. av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
  612. return 1;
  613. }
  614. #else
  615. av_log(avctx, AV_LOG_ERROR, "Zlib support not compiled.\n");
  616. return 1;
  617. #endif
  618. }
  619. avctx->pix_fmt = PIX_FMT_BGR24;
  620. return 0;
  621. }
  622. /*
  623. *
  624. * Uninit lcl decoder
  625. *
  626. */
  627. static av_cold int decode_end(AVCodecContext *avctx)
  628. {
  629. LclDecContext * const c = avctx->priv_data;
  630. if (c->pic.data[0])
  631. avctx->release_buffer(avctx, &c->pic);
  632. #if CONFIG_ZLIB
  633. inflateEnd(&(c->zstream));
  634. #endif
  635. return 0;
  636. }
  637. #if CONFIG_MSZH_DECODER
  638. AVCodec mszh_decoder = {
  639. "mszh",
  640. CODEC_TYPE_VIDEO,
  641. CODEC_ID_MSZH,
  642. sizeof(LclDecContext),
  643. decode_init,
  644. NULL,
  645. decode_end,
  646. decode_frame,
  647. CODEC_CAP_DR1,
  648. .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) MSZH"),
  649. };
  650. #endif
  651. #if CONFIG_ZLIB_DECODER
  652. AVCodec zlib_decoder = {
  653. "zlib",
  654. CODEC_TYPE_VIDEO,
  655. CODEC_ID_ZLIB,
  656. sizeof(LclDecContext),
  657. decode_init,
  658. NULL,
  659. decode_end,
  660. decode_frame,
  661. CODEC_CAP_DR1,
  662. .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
  663. };
  664. #endif