mirror of
https://github.com/Unidata/netcdf-c.git
synced 2024-12-21 08:39:46 +08:00
11fe00ea05
Filter support has three goals: 1. Use the existing HDF5 filter implementations, 2. Allow filter metadata to be stored in the NumCodecs metadata format used by Zarr, 3. Allow filters to be used even when HDF5 is disabled Detailed usage directions are define in docs/filters.md. For now, the existing filter API is left in place. So filters are defined using ''nc_def_var_filter'' using the HDF5 style where the id and parameters are unsigned integers. This is a big change since filters affect many parts of the code. In the following, the terms "compressor" and "filter" and "codec" are generally used synonomously. ### Filter-Related Changes: * In order to support dynamic loading of shared filter libraries, a new library was added in the libncpoco directory; it helps to isolate dynamic loading across multiple platforms. * Provide a json parsing library for use by plugins; this is created by merging libdispatch/ncjson.c with include/ncjson.h. * Add a new _Codecs attribute to allow clients to see what codecs are being used; let ncdump -s print it out. * Provide special headers to help support compilation of HDF5 filters when HDF5 is not enabled: netcdf_filter_hdf5_build.h and netcdf_filter_build.h. * Add a number of new test to test the new nczarr filters. * Let ncgen parse _Codecs attribute, although it is ignored. ### Plugin directory changes: * Add support for the Blosc compressor; this is essential because it is the most common compressor used in Zarr datasets. This also necessitated adding a CMake FindBlosc.cmake file * Add NCZarr support for the big-four filters provided by HDF5: shuffle, fletcher32, deflate (zlib), and szip * Add a Codec defaulter (see docs/filters.md) for the big four filters. * Make plugins work with windows by properly adding __declspec declaration. ### Misc. Non-Filter Changes * Replace most uses of USE_NETCDF4 (deprecated) with USE_HDF5. * Improve support for caching * More fixes for path conversion code * Fix misc. memory leaks * Add new utility -- ncdump/ncpathcvt -- that does more or less the same thing as cygpath. * Add a number of new test to test the non-filter fixes. * Update the parsers * Convert most instances of '#ifdef _MSC_VER' to '#ifdef _WIN32'
298 lines
8.1 KiB
C
298 lines
8.1 KiB
C
#include "config.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
#include <stdio.h>
|
|
#include <sys/types.h>
|
|
#include "netcdf_filter_build.h"
|
|
#include "h5misc.h"
|
|
|
|
/* WARNING:
|
|
Starting with HDF5 version 1.10.x, the plugin code MUST be
|
|
careful when using the standard *malloc()*, *realloc()*, and
|
|
*free()* function.
|
|
|
|
In the event that the code is allocating, reallocating, for
|
|
free'ing memory that either came from or will be exported to the
|
|
calling HDF5 library, then one MUST use the corresponding HDF5
|
|
functions *H5allocate_memory()*, *H5resize_memory()*,
|
|
*H5free_memory()* [5] to avoid memory failures.
|
|
|
|
Additionally, if your filter code leaks memory, then the HDF5 library
|
|
will generate an error.
|
|
|
|
*/
|
|
|
|
#undef DEBUG
|
|
|
|
/* The C standard apparently defines all floating point constants as double;
|
|
we rely on that in this code.
|
|
*/
|
|
#define DBLVAL 12345678.12345678
|
|
|
|
static htri_t H5Z_test_can_apply(hid_t dcpl_id, hid_t type_id, hid_t space_id);
|
|
static size_t H5Z_filter_test(unsigned int flags, size_t cd_nelmts,
|
|
const unsigned int cd_values[], size_t nbytes,
|
|
size_t *buf_size, void **buf);
|
|
|
|
static int paramcheck(size_t nparams, const unsigned int* params);
|
|
static void mismatch(size_t i, const char* which);
|
|
|
|
const H5Z_class2_t H5Z_TEST[1] = {{
|
|
H5Z_CLASS_T_VERS, /* H5Z_class_t version */
|
|
(H5Z_filter_t)(H5Z_FILTER_TEST), /* Filter id number */
|
|
1, /* encoder_present flag (set to true) */
|
|
1, /* decoder_present flag (set to true) */
|
|
"test", /* Filter name for debugging */
|
|
(H5Z_can_apply_func_t)H5Z_test_can_apply, /* The "can apply" callback */
|
|
NULL, /* The "set local" callback */
|
|
(H5Z_func_t)H5Z_filter_test, /* The actual filter function */
|
|
}};
|
|
|
|
/* External Discovery Functions */
|
|
DLLEXPORT
|
|
H5PL_type_t
|
|
H5PLget_plugin_type(void)
|
|
{
|
|
return H5PL_TYPE_FILTER;
|
|
}
|
|
|
|
DLLEXPORT
|
|
const void*
|
|
H5PLget_plugin_info(void)
|
|
{
|
|
return H5Z_TEST;
|
|
}
|
|
|
|
/* Make this explicit */
|
|
/*
|
|
* The "can_apply" callback returns positive a valid combination, zero for an
|
|
* invalid combination and negative for an error.
|
|
*/
|
|
static htri_t
|
|
H5Z_test_can_apply(hid_t dcpl_id, hid_t type_id, hid_t space_id)
|
|
{
|
|
return 1; /* Assume it can always apply */
|
|
}
|
|
|
|
/*
|
|
This filter does some verification
|
|
that the parameters passed to the filter
|
|
are correct. Specifically, that endian-ness
|
|
is correct. As a filter, it is the identify
|
|
function, passing input to output unchanged.
|
|
It also prints out the size of each chunk.
|
|
|
|
Test cases format:
|
|
1.The first param is the test index i.e. which test to execute.
|
|
2. The remaining parameters are those for the test chosen in #1
|
|
|
|
*/
|
|
|
|
static size_t
|
|
H5Z_filter_test(unsigned int flags, size_t cd_nelmts,
|
|
const unsigned int cd_values[], size_t nbytes,
|
|
size_t *buf_size, void **buf)
|
|
{
|
|
void* newbuf;
|
|
unsigned int testcase = 0;
|
|
size_t size = 1024 * sizeof(float) * 2;
|
|
|
|
if(cd_nelmts == 0)
|
|
goto fail;
|
|
|
|
testcase = cd_values[0];
|
|
|
|
switch (testcase) {
|
|
case TC_ENDIAN:
|
|
if(!paramcheck(cd_nelmts,cd_values))
|
|
goto fail;
|
|
break;
|
|
case TC_ODDSIZE:
|
|
/* Print out the chunk size */
|
|
fprintf(stderr,"nbytes = %lld chunk size = %lld\n",(long long)nbytes,(long long)*buf_size);
|
|
fflush(stderr);
|
|
break;
|
|
default: break;
|
|
}
|
|
|
|
if (flags & H5Z_FLAG_REVERSE) { /* Decompress */
|
|
|
|
if(testcase == TC_EXPANDED) {
|
|
int i;
|
|
float* b = (float*)*buf;
|
|
fprintf(stderr,"TC_EXPANDED: decompress: nbytes=%u buf_size=%u xdata[0..8]=|",(unsigned)nbytes,(unsigned)*buf_size);
|
|
for(i=0;i<8;i++) {
|
|
fprintf(stderr," %u",(int)(b[1024+i]));
|
|
}
|
|
fprintf(stderr,"|\n");
|
|
/* Replace buffer */
|
|
newbuf = H5allocate_memory(*buf_size,0);
|
|
if(newbuf == NULL) abort();
|
|
memcpy(newbuf,*buf,*buf_size);
|
|
|
|
} else {
|
|
/* Replace buffer */
|
|
newbuf = H5allocate_memory(*buf_size,0);
|
|
if(newbuf == NULL) abort();
|
|
memcpy(newbuf,*buf,*buf_size);
|
|
}
|
|
|
|
/* reclaim old buffer */
|
|
H5free_memory(*buf);
|
|
*buf = newbuf;
|
|
|
|
} else { /* Compress */
|
|
if(testcase == TC_EXPANDED) {
|
|
int i;
|
|
float* b;
|
|
#if 0
|
|
fprintf(stderr,"TC_EXPANDED: compress: nbytes=%u buf_size=%u size=%u\n",(unsigned)nbytes,(unsigned)*buf_size,(unsigned)size);
|
|
#endif
|
|
/* Replace buffer with one that is bigger than the chunk size */
|
|
newbuf = H5allocate_memory(size,0);
|
|
if(newbuf == NULL) abort();
|
|
b = (float*)newbuf;
|
|
for(i=0;i<1024*2;i++) {
|
|
b[i] = (float)(17+i);
|
|
}
|
|
memcpy(newbuf,*buf,*buf_size);
|
|
*buf_size = size;
|
|
} else {
|
|
/* Replace buffer */
|
|
newbuf = H5allocate_memory(*buf_size,0);
|
|
if(newbuf == NULL) abort();
|
|
memcpy(newbuf,*buf,*buf_size);
|
|
}
|
|
|
|
/* reclaim old buffer */
|
|
H5free_memory(*buf);
|
|
*buf = newbuf;
|
|
}
|
|
|
|
return *buf_size;
|
|
|
|
fail:
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
paramcheck(size_t nparams, const unsigned int* params)
|
|
{
|
|
size_t i;
|
|
unsigned char mem[8];
|
|
|
|
if(nparams != 14) {
|
|
fprintf(stderr,"Too few parameters: need=14 sent=%ld\n",(unsigned long)nparams);
|
|
goto fail;
|
|
}
|
|
|
|
for(i=0;i<nparams;i++) {
|
|
unsigned int ival;
|
|
unsigned long long lval;
|
|
float fval;
|
|
double dval;
|
|
switch (i) {
|
|
case 0: break; /* this is the testcase # */
|
|
case 1:
|
|
ival = (-17) & 0xff;
|
|
if(ival != (signed int)(params[i]))
|
|
{mismatch(i,"signed byte"); goto fail; };
|
|
break;
|
|
case 2:
|
|
ival = 23;
|
|
if(ival != (unsigned int)(params[i]))
|
|
{mismatch(i,"unsigned byte"); goto fail; };
|
|
break;
|
|
case 3:
|
|
ival = (-25) & 0xffff;
|
|
if(ival != (signed int)(params[i]))
|
|
{mismatch(i,"signed short"); goto fail; };
|
|
break;
|
|
case 4:
|
|
ival = 27;
|
|
if(ival != (unsigned int)(params[i]))
|
|
{mismatch(i,"unsigned short"); goto fail; };
|
|
break;
|
|
case 5:
|
|
ival = 77;
|
|
if(ival != (signed int)(params[i]))
|
|
{mismatch(i,"signed int"); goto fail; };
|
|
break;
|
|
case 6:
|
|
ival = 93u;
|
|
if(ival != (unsigned int)(params[i]))
|
|
{mismatch(i,"unsigned int"); goto fail; };
|
|
break;
|
|
case 7:
|
|
fval = 789.0f;
|
|
if(fval != *(float*)(¶ms[i]))
|
|
{mismatch(i,"float"); goto fail; };
|
|
break;
|
|
case 8: {/*double*/
|
|
double x;
|
|
memcpy(mem,¶ms[i],sizeof(mem));
|
|
NC_h5filterspec_fix8(mem,1); /* Fix up endianness */
|
|
x = *(double*)mem;
|
|
dval = DBLVAL;
|
|
i++; /* takes two parameters */
|
|
if(dval != x) {
|
|
mismatch(i,"double");
|
|
goto fail;
|
|
}
|
|
}; break;
|
|
case 10: {/*signed long long*/
|
|
signed long long x;
|
|
memcpy(mem,¶ms[i],sizeof(mem));
|
|
NC_h5filterspec_fix8(mem,1); /* Fix up endianness */
|
|
x = *(signed long long*)mem;
|
|
NC_h5filterspec_fix8(&x,1); /* Fix up endianness */
|
|
lval = -9223372036854775807L;
|
|
i++; /* takes two parameters */
|
|
if(lval != x) {
|
|
mismatch(i,"signed long long");
|
|
goto fail;
|
|
}
|
|
}; break;
|
|
case 12: {/*unsigned long long*/
|
|
unsigned long long x;
|
|
memcpy(mem,¶ms[i],sizeof(mem));
|
|
NC_h5filterspec_fix8(mem,1); /* Fix up endianness */
|
|
x = *(unsigned long long*)mem;
|
|
lval = 18446744073709551615UL;
|
|
i++; /* takes two parameters */
|
|
if(lval != x) {
|
|
mismatch(i,"unsigned long long");
|
|
goto fail;
|
|
}
|
|
}; break;
|
|
|
|
default:
|
|
mismatch(i,"unexpected parameter");
|
|
goto fail;
|
|
break;
|
|
}
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
{
|
|
size_t i;
|
|
fprintf(stderr,"bigendian=%d nparams=%d params=\n",bigendian,nparams);
|
|
for(i=0;i<nparams;i++) {
|
|
fprintf(stderr,"[%d] %ud %d %f\n", (unsigned int)i, params[i],(signed int)params[i],*(float*)¶ms[i]);
|
|
}
|
|
fflush(stderr);
|
|
}
|
|
#endif
|
|
return 1;
|
|
fail:
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
mismatch(size_t i, const char* which)
|
|
{
|
|
fprintf(stderr,"mismatch: [%ld] %s\n",(unsigned long)i,which);
|
|
fflush(stderr);
|
|
}
|