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.

677 lines
24KB

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