tif_zstd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. TIFFPredictorState predict;
  40. ZSTD_DStream* dstream;
  41. ZSTD_CStream* cstream;
  42. int compression_level; /* compression level */
  43. ZSTD_outBuffer out_buffer;
  44. int state; /* state flags */
  45. #define LSTATE_INIT_DECODE 0x01
  46. #define LSTATE_INIT_ENCODE 0x02
  47. TIFFVGetMethod vgetparent; /* super-class method */
  48. TIFFVSetMethod vsetparent; /* super-class method */
  49. } ZSTDState;
  50. #define LState(tif) ((ZSTDState*) (tif)->tif_data)
  51. #define DecoderState(tif) LState(tif)
  52. #define EncoderState(tif) LState(tif)
  53. static int ZSTDEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
  54. static int ZSTDDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
  55. static int
  56. ZSTDFixupTags(TIFF* tif)
  57. {
  58. (void) tif;
  59. return 1;
  60. }
  61. static int
  62. ZSTDSetupDecode(TIFF* tif)
  63. {
  64. ZSTDState* sp = DecoderState(tif);
  65. assert(sp != NULL);
  66. /* if we were last encoding, terminate this mode */
  67. if (sp->state & LSTATE_INIT_ENCODE) {
  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
  79. ZSTDPreDecode(TIFF* tif, uint16 s)
  80. {
  81. static const char module[] = "ZSTDPreDecode";
  82. ZSTDState* sp = DecoderState(tif);
  83. size_t zstd_ret;
  84. (void) s;
  85. assert(sp != NULL);
  86. if( (sp->state & LSTATE_INIT_DECODE) == 0 )
  87. tif->tif_setupdecode(tif);
  88. if( sp->dstream )
  89. {
  90. ZSTD_freeDStream(sp->dstream);
  91. sp->dstream = NULL;
  92. }
  93. sp->dstream = ZSTD_createDStream();
  94. if( sp->dstream == NULL ) {
  95. TIFFErrorExt(tif->tif_clientdata, module,
  96. "Cannot allocate decompression stream");
  97. return 0;
  98. }
  99. zstd_ret = ZSTD_initDStream(sp->dstream);
  100. if( ZSTD_isError(zstd_ret) ) {
  101. TIFFErrorExt(tif->tif_clientdata, module,
  102. "Error in ZSTD_initDStream(): %s",
  103. ZSTD_getErrorName(zstd_ret));
  104. return 0;
  105. }
  106. return 1;
  107. }
  108. static int
  109. ZSTDDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
  110. {
  111. static const char module[] = "ZSTDDecode";
  112. ZSTDState* sp = DecoderState(tif);
  113. ZSTD_inBuffer in_buffer;
  114. ZSTD_outBuffer out_buffer;
  115. size_t zstd_ret;
  116. (void) s;
  117. assert(sp != NULL);
  118. assert(sp->state == LSTATE_INIT_DECODE);
  119. in_buffer.src = tif->tif_rawcp;
  120. in_buffer.size = (size_t) tif->tif_rawcc;
  121. in_buffer.pos = 0;
  122. out_buffer.dst = op;
  123. out_buffer.size = (size_t) occ;
  124. out_buffer.pos = 0;
  125. do {
  126. zstd_ret = ZSTD_decompressStream(sp->dstream, &out_buffer,
  127. &in_buffer);
  128. if( ZSTD_isError(zstd_ret) ) {
  129. TIFFErrorExt(tif->tif_clientdata, module,
  130. "Error in ZSTD_decompressStream(): %s",
  131. ZSTD_getErrorName(zstd_ret));
  132. return 0;
  133. }
  134. } while( zstd_ret != 0 &&
  135. in_buffer.pos < in_buffer.size &&
  136. out_buffer.pos < out_buffer.size );
  137. if (out_buffer.pos < (size_t)occ) {
  138. TIFFErrorExt(tif->tif_clientdata, module,
  139. "Not enough data at scanline %lu (short %lu bytes)",
  140. (unsigned long) tif->tif_row,
  141. (unsigned long) (size_t)occ - out_buffer.pos);
  142. return 0;
  143. }
  144. tif->tif_rawcp += in_buffer.pos;
  145. tif->tif_rawcc -= in_buffer.pos;
  146. return 1;
  147. }
  148. static int
  149. ZSTDSetupEncode(TIFF* tif)
  150. {
  151. ZSTDState* sp = EncoderState(tif);
  152. assert(sp != NULL);
  153. if (sp->state & LSTATE_INIT_DECODE) {
  154. ZSTD_freeDStream(sp->dstream);
  155. sp->dstream = NULL;
  156. sp->state = 0;
  157. }
  158. sp->state |= LSTATE_INIT_ENCODE;
  159. return 1;
  160. }
  161. /*
  162. * Reset encoding state at the start of a strip.
  163. */
  164. static int
  165. ZSTDPreEncode(TIFF* tif, uint16 s)
  166. {
  167. static const char module[] = "ZSTDPreEncode";
  168. ZSTDState *sp = EncoderState(tif);
  169. size_t zstd_ret;
  170. (void) s;
  171. assert(sp != NULL);
  172. if( sp->state != LSTATE_INIT_ENCODE )
  173. tif->tif_setupencode(tif);
  174. if (sp->cstream) {
  175. ZSTD_freeCStream(sp->cstream);
  176. sp->cstream = NULL;
  177. }
  178. sp->cstream = ZSTD_createCStream();
  179. if( sp->cstream == NULL ) {
  180. TIFFErrorExt(tif->tif_clientdata, module,
  181. "Cannot allocate compression stream");
  182. return 0;
  183. }
  184. zstd_ret = ZSTD_initCStream(sp->cstream, sp->compression_level);
  185. if( ZSTD_isError(zstd_ret) ) {
  186. TIFFErrorExt(tif->tif_clientdata, module,
  187. "Error in ZSTD_initCStream(): %s",
  188. ZSTD_getErrorName(zstd_ret));
  189. return 0;
  190. }
  191. sp->out_buffer.dst = tif->tif_rawdata;
  192. sp->out_buffer.size = (size_t)tif->tif_rawdatasize;
  193. sp->out_buffer.pos = 0;
  194. return 1;
  195. }
  196. /*
  197. * Encode a chunk of pixels.
  198. */
  199. static int
  200. ZSTDEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  201. {
  202. static const char module[] = "ZSTDEncode";
  203. ZSTDState *sp = EncoderState(tif);
  204. ZSTD_inBuffer in_buffer;
  205. size_t zstd_ret;
  206. assert(sp != NULL);
  207. assert(sp->state == LSTATE_INIT_ENCODE);
  208. (void) s;
  209. in_buffer.src = bp;
  210. in_buffer.size = (size_t)cc;
  211. in_buffer.pos = 0;
  212. do {
  213. zstd_ret = ZSTD_compressStream(sp->cstream, &sp->out_buffer,
  214. &in_buffer);
  215. if( ZSTD_isError(zstd_ret) ) {
  216. TIFFErrorExt(tif->tif_clientdata, module,
  217. "Error in ZSTD_compressStream(): %s",
  218. ZSTD_getErrorName(zstd_ret));
  219. return 0;
  220. }
  221. if( sp->out_buffer.pos == sp->out_buffer.size ) {
  222. tif->tif_rawcc = tif->tif_rawdatasize;
  223. if (!TIFFFlushData1(tif))
  224. return 0;
  225. sp->out_buffer.dst = tif->tif_rawcp;
  226. sp->out_buffer.pos = 0;
  227. }
  228. } while( in_buffer.pos < in_buffer.size );
  229. return 1;
  230. }
  231. /*
  232. * Finish off an encoded strip by flushing it.
  233. */
  234. static int
  235. ZSTDPostEncode(TIFF* tif)
  236. {
  237. static const char module[] = "ZSTDPostEncode";
  238. ZSTDState *sp = EncoderState(tif);
  239. size_t zstd_ret;
  240. do {
  241. zstd_ret = ZSTD_endStream(sp->cstream, &sp->out_buffer);
  242. if( ZSTD_isError(zstd_ret) ) {
  243. TIFFErrorExt(tif->tif_clientdata, module,
  244. "Error in ZSTD_endStream(): %s",
  245. ZSTD_getErrorName(zstd_ret));
  246. return 0;
  247. }
  248. if( sp->out_buffer.pos > 0 ) {
  249. tif->tif_rawcc = sp->out_buffer.pos;
  250. if (!TIFFFlushData1(tif))
  251. return 0;
  252. sp->out_buffer.dst = tif->tif_rawcp;
  253. sp->out_buffer.pos = 0;
  254. }
  255. } while (zstd_ret != 0);
  256. return 1;
  257. }
  258. static void
  259. ZSTDCleanup(TIFF* tif)
  260. {
  261. ZSTDState* sp = LState(tif);
  262. assert(sp != 0);
  263. (void)TIFFPredictorCleanup(tif);
  264. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  265. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  266. if (sp->dstream) {
  267. ZSTD_freeDStream(sp->dstream);
  268. sp->dstream = NULL;
  269. }
  270. if (sp->cstream) {
  271. ZSTD_freeCStream(sp->cstream);
  272. sp->cstream = NULL;
  273. }
  274. _TIFFfree(sp);
  275. tif->tif_data = NULL;
  276. _TIFFSetDefaultCompressionState(tif);
  277. }
  278. static int
  279. ZSTDVSetField(TIFF* tif, uint32 tag, va_list ap)
  280. {
  281. static const char module[] = "ZSTDVSetField";
  282. ZSTDState* sp = LState(tif);
  283. switch (tag) {
  284. case TIFFTAG_ZSTD_LEVEL:
  285. sp->compression_level = (int) va_arg(ap, int);
  286. if( sp->compression_level <= 0 ||
  287. sp->compression_level > ZSTD_maxCLevel() )
  288. {
  289. TIFFWarningExt(tif->tif_clientdata, module,
  290. "ZSTD_LEVEL should be between 1 and %d",
  291. ZSTD_maxCLevel());
  292. }
  293. return 1;
  294. default:
  295. return (*sp->vsetparent)(tif, tag, ap);
  296. }
  297. /*NOTREACHED*/
  298. }
  299. static int
  300. ZSTDVGetField(TIFF* tif, uint32 tag, va_list ap)
  301. {
  302. ZSTDState* sp = LState(tif);
  303. switch (tag) {
  304. case TIFFTAG_ZSTD_LEVEL:
  305. *va_arg(ap, int*) = sp->compression_level;
  306. break;
  307. default:
  308. return (*sp->vgetparent)(tif, tag, ap);
  309. }
  310. return 1;
  311. }
  312. static const TIFFField ZSTDFields[] = {
  313. { TIFFTAG_ZSTD_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
  314. TIFF_SETGET_UNDEFINED,
  315. FIELD_PSEUDO, TRUE, FALSE, "ZSTD compression_level", NULL },
  316. };
  317. int
  318. TIFFInitZSTD(TIFF* tif, int scheme)
  319. {
  320. static const char module[] = "TIFFInitZSTD";
  321. ZSTDState* sp;
  322. assert( scheme == COMPRESSION_ZSTD );
  323. /*
  324. * Merge codec-specific tag information.
  325. */
  326. if (!_TIFFMergeFields(tif, ZSTDFields, TIFFArrayCount(ZSTDFields))) {
  327. TIFFErrorExt(tif->tif_clientdata, module,
  328. "Merging ZSTD codec-specific tags failed");
  329. return 0;
  330. }
  331. /*
  332. * Allocate state block so tag methods have storage to record values.
  333. */
  334. tif->tif_data = (uint8*) _TIFFmalloc(sizeof(ZSTDState));
  335. if (tif->tif_data == NULL)
  336. goto bad;
  337. sp = LState(tif);
  338. /*
  339. * Override parent get/set field methods.
  340. */
  341. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  342. tif->tif_tagmethods.vgetfield = ZSTDVGetField; /* hook for codec tags */
  343. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  344. tif->tif_tagmethods.vsetfield = ZSTDVSetField; /* hook for codec tags */
  345. /* Default values for codec-specific fields */
  346. sp->compression_level = 9; /* default comp. level */
  347. sp->state = 0;
  348. sp->dstream = 0;
  349. sp->cstream = 0;
  350. sp->out_buffer.dst = NULL;
  351. sp->out_buffer.size = 0;
  352. sp->out_buffer.pos = 0;
  353. /*
  354. * Install codec methods.
  355. */
  356. tif->tif_fixuptags = ZSTDFixupTags;
  357. tif->tif_setupdecode = ZSTDSetupDecode;
  358. tif->tif_predecode = ZSTDPreDecode;
  359. tif->tif_decoderow = ZSTDDecode;
  360. tif->tif_decodestrip = ZSTDDecode;
  361. tif->tif_decodetile = ZSTDDecode;
  362. tif->tif_setupencode = ZSTDSetupEncode;
  363. tif->tif_preencode = ZSTDPreEncode;
  364. tif->tif_postencode = ZSTDPostEncode;
  365. tif->tif_encoderow = ZSTDEncode;
  366. tif->tif_encodestrip = ZSTDEncode;
  367. tif->tif_encodetile = ZSTDEncode;
  368. tif->tif_cleanup = ZSTDCleanup;
  369. /*
  370. * Setup predictor setup.
  371. */
  372. (void) TIFFPredictorInit(tif);
  373. return 1;
  374. bad:
  375. TIFFErrorExt(tif->tif_clientdata, module,
  376. "No space for ZSTD state block");
  377. return 0;
  378. }
  379. #endif /* ZSTD_SUPPORT */
  380. /* vim: set ts=8 sts=8 sw=8 noet: */