tif_zstd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 LState(tif) ((ZSTDState *)(tif)->tif_data)
  52. #define DecoderState(tif) LState(tif)
  53. #define EncoderState(tif) LState(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 = DecoderState(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 = DecoderState(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 = DecoderState(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. TIFFErrorExtR(tif, module, "Error in ZSTD_decompressStream(): %s",
  127. ZSTD_getErrorName(zstd_ret));
  128. return 0;
  129. }
  130. } while (zstd_ret != 0 && in_buffer.pos < in_buffer.size &&
  131. out_buffer.pos < out_buffer.size);
  132. if (out_buffer.pos < (size_t)occ)
  133. {
  134. TIFFErrorExtR(tif, module,
  135. "Not enough data at scanline %lu (short %lu bytes)",
  136. (unsigned long)tif->tif_row,
  137. (unsigned long)((size_t)occ - out_buffer.pos));
  138. return 0;
  139. }
  140. tif->tif_rawcp += in_buffer.pos;
  141. tif->tif_rawcc -= in_buffer.pos;
  142. return 1;
  143. }
  144. static int ZSTDSetupEncode(TIFF *tif)
  145. {
  146. ZSTDState *sp = EncoderState(tif);
  147. assert(sp != NULL);
  148. if (sp->state & LSTATE_INIT_DECODE)
  149. {
  150. ZSTD_freeDStream(sp->dstream);
  151. sp->dstream = NULL;
  152. sp->state = 0;
  153. }
  154. sp->state |= LSTATE_INIT_ENCODE;
  155. return 1;
  156. }
  157. /*
  158. * Reset encoding state at the start of a strip.
  159. */
  160. static int ZSTDPreEncode(TIFF *tif, uint16_t s)
  161. {
  162. static const char module[] = "ZSTDPreEncode";
  163. ZSTDState *sp = EncoderState(tif);
  164. size_t zstd_ret;
  165. (void)s;
  166. assert(sp != NULL);
  167. if (sp->state != LSTATE_INIT_ENCODE)
  168. tif->tif_setupencode(tif);
  169. if (sp->cstream == NULL)
  170. {
  171. sp->cstream = ZSTD_createCStream();
  172. if (sp->cstream == NULL)
  173. {
  174. TIFFErrorExtR(tif, module, "Cannot allocate compression stream");
  175. return 0;
  176. }
  177. }
  178. zstd_ret = ZSTD_initCStream(sp->cstream, sp->compression_level);
  179. if (ZSTD_isError(zstd_ret))
  180. {
  181. TIFFErrorExtR(tif, module, "Error in ZSTD_initCStream(): %s",
  182. ZSTD_getErrorName(zstd_ret));
  183. return 0;
  184. }
  185. sp->out_buffer.dst = tif->tif_rawdata;
  186. sp->out_buffer.size = (size_t)tif->tif_rawdatasize;
  187. sp->out_buffer.pos = 0;
  188. return 1;
  189. }
  190. /*
  191. * Encode a chunk of pixels.
  192. */
  193. static int ZSTDEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
  194. {
  195. static const char module[] = "ZSTDEncode";
  196. ZSTDState *sp = EncoderState(tif);
  197. ZSTD_inBuffer in_buffer;
  198. size_t zstd_ret;
  199. assert(sp != NULL);
  200. assert(sp->state == LSTATE_INIT_ENCODE);
  201. (void)s;
  202. in_buffer.src = bp;
  203. in_buffer.size = (size_t)cc;
  204. in_buffer.pos = 0;
  205. do
  206. {
  207. zstd_ret =
  208. ZSTD_compressStream(sp->cstream, &sp->out_buffer, &in_buffer);
  209. if (ZSTD_isError(zstd_ret))
  210. {
  211. TIFFErrorExtR(tif, module, "Error in ZSTD_compressStream(): %s",
  212. ZSTD_getErrorName(zstd_ret));
  213. return 0;
  214. }
  215. if (sp->out_buffer.pos == sp->out_buffer.size)
  216. {
  217. tif->tif_rawcc = tif->tif_rawdatasize;
  218. if (!TIFFFlushData1(tif))
  219. return 0;
  220. sp->out_buffer.dst = tif->tif_rawcp;
  221. sp->out_buffer.pos = 0;
  222. }
  223. } while (in_buffer.pos < in_buffer.size);
  224. return 1;
  225. }
  226. /*
  227. * Finish off an encoded strip by flushing it.
  228. */
  229. static int ZSTDPostEncode(TIFF *tif)
  230. {
  231. static const char module[] = "ZSTDPostEncode";
  232. ZSTDState *sp = EncoderState(tif);
  233. size_t zstd_ret;
  234. do
  235. {
  236. zstd_ret = ZSTD_endStream(sp->cstream, &sp->out_buffer);
  237. if (ZSTD_isError(zstd_ret))
  238. {
  239. TIFFErrorExtR(tif, module, "Error in ZSTD_endStream(): %s",
  240. ZSTD_getErrorName(zstd_ret));
  241. return 0;
  242. }
  243. if (sp->out_buffer.pos > 0)
  244. {
  245. tif->tif_rawcc = sp->out_buffer.pos;
  246. if (!TIFFFlushData1(tif))
  247. return 0;
  248. sp->out_buffer.dst = tif->tif_rawcp;
  249. sp->out_buffer.pos = 0;
  250. }
  251. } while (zstd_ret != 0);
  252. return 1;
  253. }
  254. static void ZSTDCleanup(TIFF *tif)
  255. {
  256. ZSTDState *sp = LState(tif);
  257. assert(sp != 0);
  258. (void)TIFFPredictorCleanup(tif);
  259. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  260. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  261. if (sp->dstream)
  262. {
  263. ZSTD_freeDStream(sp->dstream);
  264. sp->dstream = NULL;
  265. }
  266. if (sp->cstream)
  267. {
  268. ZSTD_freeCStream(sp->cstream);
  269. sp->cstream = NULL;
  270. }
  271. _TIFFfreeExt(tif, sp);
  272. tif->tif_data = NULL;
  273. _TIFFSetDefaultCompressionState(tif);
  274. }
  275. static int ZSTDVSetField(TIFF *tif, uint32_t tag, va_list ap)
  276. {
  277. static const char module[] = "ZSTDVSetField";
  278. ZSTDState *sp = LState(tif);
  279. switch (tag)
  280. {
  281. case TIFFTAG_ZSTD_LEVEL:
  282. sp->compression_level = (int)va_arg(ap, int);
  283. if (sp->compression_level <= 0 ||
  284. sp->compression_level > ZSTD_maxCLevel())
  285. {
  286. TIFFWarningExtR(tif, module,
  287. "ZSTD_LEVEL should be between 1 and %d",
  288. ZSTD_maxCLevel());
  289. }
  290. return 1;
  291. default:
  292. return (*sp->vsetparent)(tif, tag, ap);
  293. }
  294. /*NOTREACHED*/
  295. }
  296. static int ZSTDVGetField(TIFF *tif, uint32_t tag, va_list ap)
  297. {
  298. ZSTDState *sp = LState(tif);
  299. switch (tag)
  300. {
  301. case TIFFTAG_ZSTD_LEVEL:
  302. *va_arg(ap, int *) = sp->compression_level;
  303. break;
  304. default:
  305. return (*sp->vgetparent)(tif, tag, ap);
  306. }
  307. return 1;
  308. }
  309. static const TIFFField ZSTDFields[] = {
  310. {TIFFTAG_ZSTD_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
  311. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "ZSTD compression_level",
  312. NULL},
  313. };
  314. int TIFFInitZSTD(TIFF *tif, int scheme)
  315. {
  316. static const char module[] = "TIFFInitZSTD";
  317. ZSTDState *sp;
  318. (void)scheme;
  319. assert(scheme == COMPRESSION_ZSTD);
  320. /*
  321. * Merge codec-specific tag information.
  322. */
  323. if (!_TIFFMergeFields(tif, ZSTDFields, TIFFArrayCount(ZSTDFields)))
  324. {
  325. TIFFErrorExtR(tif, module, "Merging ZSTD codec-specific tags failed");
  326. return 0;
  327. }
  328. /*
  329. * Allocate state block so tag methods have storage to record values.
  330. */
  331. tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(ZSTDState));
  332. if (tif->tif_data == NULL)
  333. goto bad;
  334. sp = LState(tif);
  335. /*
  336. * Override parent get/set field methods.
  337. */
  338. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  339. tif->tif_tagmethods.vgetfield = ZSTDVGetField; /* hook for codec tags */
  340. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  341. tif->tif_tagmethods.vsetfield = ZSTDVSetField; /* hook for codec tags */
  342. /* Default values for codec-specific fields */
  343. sp->compression_level = 9; /* default comp. level */
  344. sp->state = 0;
  345. sp->dstream = 0;
  346. sp->cstream = 0;
  347. sp->out_buffer.dst = NULL;
  348. sp->out_buffer.size = 0;
  349. sp->out_buffer.pos = 0;
  350. /*
  351. * Install codec methods.
  352. */
  353. tif->tif_fixuptags = ZSTDFixupTags;
  354. tif->tif_setupdecode = ZSTDSetupDecode;
  355. tif->tif_predecode = ZSTDPreDecode;
  356. tif->tif_decoderow = ZSTDDecode;
  357. tif->tif_decodestrip = ZSTDDecode;
  358. tif->tif_decodetile = ZSTDDecode;
  359. tif->tif_setupencode = ZSTDSetupEncode;
  360. tif->tif_preencode = ZSTDPreEncode;
  361. tif->tif_postencode = ZSTDPostEncode;
  362. tif->tif_encoderow = ZSTDEncode;
  363. tif->tif_encodestrip = ZSTDEncode;
  364. tif->tif_encodetile = ZSTDEncode;
  365. tif->tif_cleanup = ZSTDCleanup;
  366. /*
  367. * Setup predictor setup.
  368. */
  369. (void)TIFFPredictorInit(tif);
  370. return 1;
  371. bad:
  372. TIFFErrorExtR(tif, module, "No space for ZSTD state block");
  373. return 0;
  374. }
  375. #endif /* ZSTD_SUPPORT */