tif_zstd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright (c) 2017, Planet Labs
  3. * Author: <even.rouault at spatialys.com>
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and
  6. * its documentation for any purpose is hereby granted without fee, provided
  7. * that (i) the above copyright notices and this permission notice appear in
  8. * all copies of the software and related documentation, and (ii) the names of
  9. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  10. * publicity relating to the software without the specific, prior written
  11. * permission of Sam Leffler and Silicon Graphics.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. #include "tiffiop.h"
  25. #ifdef ZSTD_SUPPORT
  26. /*
  27. * TIFF Library.
  28. *
  29. * ZSTD Compression Support
  30. *
  31. */
  32. #include "tif_predict.h"
  33. #include "zstd.h"
  34. #include <stdio.h>
  35. /*
  36. * State block for each open TIFF file using ZSTD compression/decompression.
  37. */
  38. typedef struct
  39. {
  40. TIFFPredictorState predict;
  41. ZSTD_DStream *dstream;
  42. ZSTD_CStream *cstream;
  43. int compression_level; /* compression level */
  44. ZSTD_outBuffer out_buffer;
  45. int state; /* state flags */
  46. #define LSTATE_INIT_DECODE 0x01
  47. #define LSTATE_INIT_ENCODE 0x02
  48. TIFFVGetMethod vgetparent; /* super-class method */
  49. TIFFVSetMethod vsetparent; /* super-class method */
  50. } ZSTDState;
  51. #define GetZSTDState(tif) ((ZSTDState *)(tif)->tif_data)
  52. #define ZSTDDecoderState(tif) GetZSTDState(tif)
  53. #define ZSTDEncoderState(tif) GetZSTDState(tif)
  54. static int ZSTDEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s);
  55. static int ZSTDDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s);
  56. static int ZSTDFixupTags(TIFF *tif)
  57. {
  58. (void)tif;
  59. return 1;
  60. }
  61. static int ZSTDSetupDecode(TIFF *tif)
  62. {
  63. ZSTDState *sp = ZSTDDecoderState(tif);
  64. assert(sp != NULL);
  65. /* if we were last encoding, terminate this mode */
  66. if (sp->state & LSTATE_INIT_ENCODE)
  67. {
  68. ZSTD_freeCStream(sp->cstream);
  69. sp->cstream = NULL;
  70. sp->state = 0;
  71. }
  72. sp->state |= LSTATE_INIT_DECODE;
  73. return 1;
  74. }
  75. /*
  76. * Setup state for decoding a strip.
  77. */
  78. static int ZSTDPreDecode(TIFF *tif, uint16_t s)
  79. {
  80. static const char module[] = "ZSTDPreDecode";
  81. ZSTDState *sp = ZSTDDecoderState(tif);
  82. size_t zstd_ret;
  83. (void)s;
  84. assert(sp != NULL);
  85. if ((sp->state & LSTATE_INIT_DECODE) == 0)
  86. tif->tif_setupdecode(tif);
  87. if (sp->dstream == NULL)
  88. {
  89. sp->dstream = ZSTD_createDStream();
  90. if (sp->dstream == NULL)
  91. {
  92. TIFFErrorExtR(tif, module, "Cannot allocate decompression stream");
  93. return 0;
  94. }
  95. }
  96. zstd_ret = ZSTD_initDStream(sp->dstream);
  97. if (ZSTD_isError(zstd_ret))
  98. {
  99. TIFFErrorExtR(tif, module, "Error in ZSTD_initDStream(): %s",
  100. ZSTD_getErrorName(zstd_ret));
  101. return 0;
  102. }
  103. return 1;
  104. }
  105. static int ZSTDDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
  106. {
  107. static const char module[] = "ZSTDDecode";
  108. ZSTDState *sp = ZSTDDecoderState(tif);
  109. ZSTD_inBuffer in_buffer;
  110. ZSTD_outBuffer out_buffer;
  111. size_t zstd_ret;
  112. (void)s;
  113. assert(sp != NULL);
  114. assert(sp->state == LSTATE_INIT_DECODE);
  115. in_buffer.src = tif->tif_rawcp;
  116. in_buffer.size = (size_t)tif->tif_rawcc;
  117. in_buffer.pos = 0;
  118. out_buffer.dst = op;
  119. out_buffer.size = (size_t)occ;
  120. out_buffer.pos = 0;
  121. do
  122. {
  123. zstd_ret = ZSTD_decompressStream(sp->dstream, &out_buffer, &in_buffer);
  124. if (ZSTD_isError(zstd_ret))
  125. {
  126. memset(op + out_buffer.pos, 0, out_buffer.size - out_buffer.pos);
  127. TIFFErrorExtR(tif, module, "Error in ZSTD_decompressStream(): %s",
  128. ZSTD_getErrorName(zstd_ret));
  129. return 0;
  130. }
  131. } while (zstd_ret != 0 && in_buffer.pos < in_buffer.size &&
  132. out_buffer.pos < out_buffer.size);
  133. if (out_buffer.pos < (size_t)occ)
  134. {
  135. memset(op + out_buffer.pos, 0, out_buffer.size - out_buffer.pos);
  136. TIFFErrorExtR(tif, module,
  137. "Not enough data at scanline %lu (short %lu bytes)",
  138. (unsigned long)tif->tif_row,
  139. (unsigned long)((size_t)occ - out_buffer.pos));
  140. return 0;
  141. }
  142. tif->tif_rawcp += in_buffer.pos;
  143. tif->tif_rawcc -= in_buffer.pos;
  144. return 1;
  145. }
  146. static int ZSTDSetupEncode(TIFF *tif)
  147. {
  148. ZSTDState *sp = ZSTDEncoderState(tif);
  149. assert(sp != NULL);
  150. if (sp->state & LSTATE_INIT_DECODE)
  151. {
  152. ZSTD_freeDStream(sp->dstream);
  153. sp->dstream = NULL;
  154. sp->state = 0;
  155. }
  156. sp->state |= LSTATE_INIT_ENCODE;
  157. return 1;
  158. }
  159. /*
  160. * Reset encoding state at the start of a strip.
  161. */
  162. static int ZSTDPreEncode(TIFF *tif, uint16_t s)
  163. {
  164. static const char module[] = "ZSTDPreEncode";
  165. ZSTDState *sp = ZSTDEncoderState(tif);
  166. size_t zstd_ret;
  167. (void)s;
  168. assert(sp != NULL);
  169. if (sp->state != LSTATE_INIT_ENCODE)
  170. tif->tif_setupencode(tif);
  171. if (sp->cstream == NULL)
  172. {
  173. sp->cstream = ZSTD_createCStream();
  174. if (sp->cstream == NULL)
  175. {
  176. TIFFErrorExtR(tif, module, "Cannot allocate compression stream");
  177. return 0;
  178. }
  179. }
  180. zstd_ret = ZSTD_initCStream(sp->cstream, sp->compression_level);
  181. if (ZSTD_isError(zstd_ret))
  182. {
  183. TIFFErrorExtR(tif, module, "Error in ZSTD_initCStream(): %s",
  184. ZSTD_getErrorName(zstd_ret));
  185. return 0;
  186. }
  187. sp->out_buffer.dst = tif->tif_rawdata;
  188. sp->out_buffer.size = (size_t)tif->tif_rawdatasize;
  189. sp->out_buffer.pos = 0;
  190. return 1;
  191. }
  192. /*
  193. * Encode a chunk of pixels.
  194. */
  195. static int ZSTDEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
  196. {
  197. static const char module[] = "ZSTDEncode";
  198. ZSTDState *sp = ZSTDEncoderState(tif);
  199. ZSTD_inBuffer in_buffer;
  200. size_t zstd_ret;
  201. assert(sp != NULL);
  202. assert(sp->state == LSTATE_INIT_ENCODE);
  203. (void)s;
  204. in_buffer.src = bp;
  205. in_buffer.size = (size_t)cc;
  206. in_buffer.pos = 0;
  207. do
  208. {
  209. zstd_ret =
  210. ZSTD_compressStream(sp->cstream, &sp->out_buffer, &in_buffer);
  211. if (ZSTD_isError(zstd_ret))
  212. {
  213. TIFFErrorExtR(tif, module, "Error in ZSTD_compressStream(): %s",
  214. ZSTD_getErrorName(zstd_ret));
  215. return 0;
  216. }
  217. if (sp->out_buffer.pos == sp->out_buffer.size)
  218. {
  219. tif->tif_rawcc = tif->tif_rawdatasize;
  220. if (!TIFFFlushData1(tif))
  221. return 0;
  222. sp->out_buffer.dst = tif->tif_rawcp;
  223. sp->out_buffer.pos = 0;
  224. }
  225. } while (in_buffer.pos < in_buffer.size);
  226. return 1;
  227. }
  228. /*
  229. * Finish off an encoded strip by flushing it.
  230. */
  231. static int ZSTDPostEncode(TIFF *tif)
  232. {
  233. static const char module[] = "ZSTDPostEncode";
  234. ZSTDState *sp = ZSTDEncoderState(tif);
  235. size_t zstd_ret;
  236. do
  237. {
  238. zstd_ret = ZSTD_endStream(sp->cstream, &sp->out_buffer);
  239. if (ZSTD_isError(zstd_ret))
  240. {
  241. TIFFErrorExtR(tif, module, "Error in ZSTD_endStream(): %s",
  242. ZSTD_getErrorName(zstd_ret));
  243. return 0;
  244. }
  245. if (sp->out_buffer.pos > 0)
  246. {
  247. tif->tif_rawcc = sp->out_buffer.pos;
  248. if (!TIFFFlushData1(tif))
  249. return 0;
  250. sp->out_buffer.dst = tif->tif_rawcp;
  251. sp->out_buffer.pos = 0;
  252. }
  253. } while (zstd_ret != 0);
  254. return 1;
  255. }
  256. static void ZSTDCleanup(TIFF *tif)
  257. {
  258. ZSTDState *sp = GetZSTDState(tif);
  259. assert(sp != 0);
  260. (void)TIFFPredictorCleanup(tif);
  261. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  262. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  263. if (sp->dstream)
  264. {
  265. ZSTD_freeDStream(sp->dstream);
  266. sp->dstream = NULL;
  267. }
  268. if (sp->cstream)
  269. {
  270. ZSTD_freeCStream(sp->cstream);
  271. sp->cstream = NULL;
  272. }
  273. _TIFFfreeExt(tif, sp);
  274. tif->tif_data = NULL;
  275. _TIFFSetDefaultCompressionState(tif);
  276. }
  277. static int ZSTDVSetField(TIFF *tif, uint32_t tag, va_list ap)
  278. {
  279. static const char module[] = "ZSTDVSetField";
  280. ZSTDState *sp = GetZSTDState(tif);
  281. switch (tag)
  282. {
  283. case TIFFTAG_ZSTD_LEVEL:
  284. sp->compression_level = (int)va_arg(ap, int);
  285. if (sp->compression_level <= 0 ||
  286. sp->compression_level > ZSTD_maxCLevel())
  287. {
  288. TIFFWarningExtR(tif, module,
  289. "ZSTD_LEVEL should be between 1 and %d",
  290. ZSTD_maxCLevel());
  291. }
  292. return 1;
  293. default:
  294. return (*sp->vsetparent)(tif, tag, ap);
  295. }
  296. /*NOTREACHED*/
  297. }
  298. static int ZSTDVGetField(TIFF *tif, uint32_t tag, va_list ap)
  299. {
  300. ZSTDState *sp = GetZSTDState(tif);
  301. switch (tag)
  302. {
  303. case TIFFTAG_ZSTD_LEVEL:
  304. *va_arg(ap, int *) = sp->compression_level;
  305. break;
  306. default:
  307. return (*sp->vgetparent)(tif, tag, ap);
  308. }
  309. return 1;
  310. }
  311. static const TIFFField ZSTDFields[] = {
  312. {TIFFTAG_ZSTD_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
  313. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "ZSTD compression_level",
  314. NULL},
  315. };
  316. int TIFFInitZSTD(TIFF *tif, int scheme)
  317. {
  318. static const char module[] = "TIFFInitZSTD";
  319. ZSTDState *sp;
  320. (void)scheme;
  321. assert(scheme == COMPRESSION_ZSTD);
  322. /*
  323. * Merge codec-specific tag information.
  324. */
  325. if (!_TIFFMergeFields(tif, ZSTDFields, TIFFArrayCount(ZSTDFields)))
  326. {
  327. TIFFErrorExtR(tif, module, "Merging ZSTD codec-specific tags failed");
  328. return 0;
  329. }
  330. /*
  331. * Allocate state block so tag methods have storage to record values.
  332. */
  333. tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(ZSTDState));
  334. if (tif->tif_data == NULL)
  335. goto bad;
  336. sp = GetZSTDState(tif);
  337. /*
  338. * Override parent get/set field methods.
  339. */
  340. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  341. tif->tif_tagmethods.vgetfield = ZSTDVGetField; /* hook for codec tags */
  342. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  343. tif->tif_tagmethods.vsetfield = ZSTDVSetField; /* hook for codec tags */
  344. /* Default values for codec-specific fields */
  345. sp->compression_level = 9; /* default comp. level */
  346. sp->state = 0;
  347. sp->dstream = 0;
  348. sp->cstream = 0;
  349. sp->out_buffer.dst = NULL;
  350. sp->out_buffer.size = 0;
  351. sp->out_buffer.pos = 0;
  352. /*
  353. * Install codec methods.
  354. */
  355. tif->tif_fixuptags = ZSTDFixupTags;
  356. tif->tif_setupdecode = ZSTDSetupDecode;
  357. tif->tif_predecode = ZSTDPreDecode;
  358. tif->tif_decoderow = ZSTDDecode;
  359. tif->tif_decodestrip = ZSTDDecode;
  360. tif->tif_decodetile = ZSTDDecode;
  361. tif->tif_setupencode = ZSTDSetupEncode;
  362. tif->tif_preencode = ZSTDPreEncode;
  363. tif->tif_postencode = ZSTDPostEncode;
  364. tif->tif_encoderow = ZSTDEncode;
  365. tif->tif_encodestrip = ZSTDEncode;
  366. tif->tif_encodetile = ZSTDEncode;
  367. tif->tif_cleanup = ZSTDCleanup;
  368. /*
  369. * Setup predictor setup.
  370. */
  371. (void)TIFFPredictorInit(tif);
  372. return 1;
  373. bad:
  374. TIFFErrorExtR(tif, module, "No space for ZSTD state block");
  375. return 0;
  376. }
  377. #endif /* ZSTD_SUPPORT */