-
Notifications
You must be signed in to change notification settings - Fork 264
/
Copy pathH5Zblosc.c
380 lines (309 loc) · 12.3 KB
/
H5Zblosc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
Blosc - A blocking, shuffling and lossless compression library
Copyright (C) 2009-2015 Francesc Alted <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Blosc for HDF5 - An HDF5 filter that uses the Blosc compressor.
Copyright (C) 2009-2015 Francesc Alted <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Copyright Notice and Statement for the h5py Project
Copyright (c) 2008 Andrew Collette
http://h5py.alfven.org
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
a. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
b. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
c. Neither the name of the author nor the names of contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
Copyright (C) 2010-2016 Francesc Alted
http://blosc.org
License: MIT (see LICENSE.txt)
Filter program that allows the use of the Blosc filter in HDF5.
This is based on the LZF filter interface (http://h5py.alfven.org)
by Andrew Collette.
*/
/*
HDF5 Blosc filter takes 7 parameters as follows:
param[0] -- FILTER_BLOSC_VERSION (ignored)
param[1] -- BLOSC_VERSION_FORMAT (ignored)
param[2] -- typesize in bytes
param[3] -- chunksize in bytes (ignored)
param[4] -- compression level
param[5] -- shuffle enabled
param[6] -- compressor to use
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "netcdf_filter_build.h"
#include <netcdf_json.h>
#include "H5Zblosc.h"
#ifdef USE_HDF5
#include <H5Epubgen.h>
#endif
#undef BLOSC_DEBUG
#undef BLOSCCTX
static size_t blosc_filter(unsigned flags, size_t cd_nelmts,
const unsigned cd_values[], size_t nbytes,
size_t* buf_size, void** buf);
#ifndef USE_HDF5
#define blosc_set_local NULL
#else
static herr_t blosc_set_local(hid_t dcpl, hid_t type, hid_t space);
/* Filter setup. Records the following inside the DCPL:
1. If version information is not present, set slots 0 and 1 to the filter
revision and Blosc version, respectively.
2. Compute the type size in bytes and store it in slot 2.
3. Compute the chunk size in bytes and store it in slot 3.
*/
static
herr_t blosc_set_local(hid_t dcpl, hid_t type, hid_t space)
{
int i,ndims;
unsigned int typesize, basetypesize;
unsigned int bufsize;
unsigned int flags = 0;
size_t nelements = 7;
unsigned int values[] = {0, 0, 0, 0, 0, 0, 0};
hid_t super_type;
H5T_class_t classt;
hsize_t chunkdims[32];
assert(sizeof(hid_t) == 8);
if(H5Pget_filter_by_id1(dcpl, H5Z_FILTER_BLOSC, &flags, &nelements, values, 0, NULL) < 0) goto failed;
assert(nelements == 7);
#ifdef BLOSC_DEBUG
fprintf(stderr, "Blosc: initial parameters =(%u) [%u %u %u %u %u %u %u]\n",
(unsigned)nelements,values[0],values[1],values[2],values[3],values[4],values[5],values[6]); fflush(stderr);
#endif
/* Ignore first two slots */
values[0] = 0; /* FILTER_BLOSC_VERSION*/
values[1] = 0; /*BLOSC_VERSION_FORMAT*/
ndims = H5Pget_chunk(dcpl, 32, chunkdims);
if (ndims < 0) goto failed;
if (ndims > 32) {
fprintf(stderr,"Blosc Filter Error: blosc_set_local: Chunk rank exceeds limit\n");
goto failed;
}
typesize = H5Tget_size(type);
if (typesize == 0) goto failed;
/* Get the size of the base type, even for ARRAY types */
classt = H5Tget_class(type);
if (classt == H5T_ARRAY) {
/* Get the array base component */
super_type = H5Tget_super(type);
basetypesize = H5Tget_size(super_type);
/* Release resources */
H5Tclose(super_type);
} else {
basetypesize = typesize;
}
/* Limit large typesizes (they are pretty expensive to shuffle
and, in addition, Blosc does not handle typesizes larger than
256 bytes). */
if (basetypesize > BLOSC_MAX_TYPESIZE) basetypesize = 1;
values[2] = basetypesize;
{
/* Get the size of the chunk */
bufsize = typesize;
#ifdef BLOSC_DEBUG
fprintf(stderr,">>> set_local: |dims|=%d dims=(",(int)ndims);
for (i = 0; i < ndims; i++) {
fprintf(stderr,"%s%u",(i==0?"":","),(unsigned)chunkdims[i]);
}
fprintf(stderr,")\n");
#endif
for (i = 0; i < ndims; i++) {
bufsize *= chunkdims[i];
}
values[3] = bufsize;
}
#ifdef BLOSC_DEBUG
fprintf(stderr, "Blosc: working parameters =(%u) [%u %u %u %u %u %u %u]\n",
(unsigned)nelements,values[0],values[1],values[2],values[3],values[4],values[5],values[6]); fflush(stderr);
#endif
if(H5Pmodify_filter(dcpl, H5Z_FILTER_BLOSC, flags, nelements, values) < 0) goto failed;
return 1;
failed:
return -1;
}
#endif /*USE_HDF5*/
/* The filter function */
static
size_t blosc_filter(unsigned flags, size_t cd_nelmts,
const unsigned cd_values[], size_t nbytes,
size_t* buf_size, void** buf)
{
void* outbuf = NULL;
size_t typesize;
size_t outbuf_size;
int clevel = 5; /* Compression level default */
int doshuffle = 1; /* Shuffle default */
int compcode; /* Blosc compressor */
int code;
const char* compname = "blosclz"; /* The compressor by default */
int bloscsize = 0;
#ifdef BLOSC_DEBUG
fprintf(stderr, "Blosc: compressor =(%u) [%u %u %u %u %u %u %u]\n",
(unsigned)cd_nelmts,cd_values[0],cd_values[1],cd_values[2],cd_values[3],cd_values[4],cd_values[5],cd_values[6]); fflush(stderr);
#endif
/* Filter params that are always set */
typesize = cd_values[2]; /* The datatype size */
/* Optional params */
if (cd_nelmts >= 5) {
clevel = cd_values[4]; /* The compression level */
}
if (cd_nelmts >= 6) {
doshuffle = cd_values[5]; /* BLOSC_SHUFFLE, BLOSC_BITSHUFFLE */
}
if (cd_nelmts >= 7) {
compcode = cd_values[6]; /* The Blosc compressor used */
code = blosc_compcode_to_compname(compcode, &compname);
if (code == -1) {
fprintf(stderr,"Blosc Filter Error: this Blosc library does not have support for the '%s' compressor\n",compname);
goto failed;
}
}
/* We're compressing */
if (!(flags & H5Z_FLAG_REVERSE)) {
/* Allocate an output buffer exactly as long as the input data; if
the result is larger, we simply return 0. The filter is flagged
as optional, so HDF5 marks the chunk as uncompressed and
proceeds.
*/
outbuf_size = (*buf_size);
#ifdef BLOSC_DEBUG
fprintf(stderr, "Blosc: Compress %zd chunk w/chunksize %zd\n",
nbytes, outbuf_size);
#endif
outbuf = malloc(outbuf_size);
if (outbuf == NULL) {
fprintf(stderr,"Blosc Filter Error: blosc_filter: Can't allocate compression buffer\n");
goto failed;
}
#ifdef BLOSCCTX
bloscsize = blosc_compress_ctx(clevel, doshuffle, typesize, nbytes, *buf, outbut, nbytes,
compname, /*blocksize*/0, /*no. thredds*/0);
#else
blosc_set_compressor(compname);
bloscsize = blosc_compress(clevel, doshuffle, typesize, nbytes, *buf, outbuf, nbytes);
#endif
if(bloscsize == 0) {
fprintf(stderr,"Blosc_Filter Error: blosc_filter: Buffer is uncompressible.\n");
goto failed;
} else if(bloscsize < 0) {
fprintf(stderr,"Blosc Filter Error: blosc_filter: blosc compression error\n");
goto failed;
}
} else { /* We're decompressing */
/* declare dummy variables */
size_t cbytes, blocksize;
if(outbuf) {
free(outbuf);
outbuf = NULL;
}
/* Extract the exact outbuf_size from the buffer header.
*
* NOTE: the guess value got from "cd_values" corresponds to the
* uncompressed chunk size but it should not be used in a general
* cases since other filters in the pipeline can modify the buffere
* size.
*/
blosc_cbuffer_sizes(*buf, &outbuf_size, &cbytes, &blocksize);
#ifdef BLOSC_DEBUG
fprintf(stderr, "Blosc: Decompress %zd chunk w/buffer %zd\n", nbytes, outbuf_size);
#endif
outbuf = malloc(outbuf_size);
if (outbuf == NULL) {
fprintf(stderr,"Blosc Filter Error: blosc_filter: can't allocate decompression buffer\n");
goto failed;
}
#ifdef BLOSCCTX
bloscsize = blosc_decompress_ctx(*buf, outbuf, outbuf_size, /*no. thredds*/0);
#else
bloscsize = blosc_decompress(*buf, outbuf, outbuf_size);
#endif
if(bloscsize <= 0) { /* decompression failed */
fprintf(stderr,"Blosc Filter Error: blosc_filter: blosc decompression error\n");
goto failed;
} /* if !status */
} /* compressing vs decompressing */
{
free(*buf);
*buf = outbuf;
*buf_size = outbuf_size;
return bloscsize; /* Size of compressed/decompressed data */
}
failed:
free(outbuf);
*buf = NULL;
*buf_size = 0;
return 0;
} /* End filter function */
/* HDF5 Plugin Interface */
const H5Z_class2_t blosc_H5Filter[1] = {
{
H5Z_CLASS_T_VERS,
(H5Z_filter_t)(H5Z_FILTER_BLOSC),
1, /* encoder_present flag (set to true) */
1, /* decoder_present flag (set to true) */
"blosc",
/* Filter info */
NULL, /* The "can apply" callback */
(H5Z_set_local_func_t)(blosc_set_local), /* The "set local" callback */
(H5Z_func_t)(blosc_filter), /* The filter function */
}
};
H5PL_type_t H5PLget_plugin_type(void) { return H5PL_TYPE_FILTER; }
const void* H5PLget_plugin_info(void) { return blosc_H5Filter; }