sz_compat.c 8.86 KB
Newer Older
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
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
/**
 * @file sz_compat.c
 *
 * @section LICENSE
 * Copyright 2012 - 2018
 *
 * Mathis Rosenhauer, Moritz Hanke, Joerg Behrens
 * Deutsches Klimarechenzentrum GmbH
 * Bundesstr. 45a
 * 20146 Hamburg Germany
 *
 * Luis Kornblueh
 * Max-Planck-Institut fuer Meteorologie
 * Bundesstr. 53
 * 20146 Hamburg
 * Germany
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. 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.
 *
 * 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 HOLDER 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.
 *
 * @section DESCRIPTION
 *
 * Adaptive Entropy Coding library
 *
 */

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
50 51
#include "config.h"
#include "szlib.h"
52
#include <stdio.h>
53
#include <stdlib.h>
54
#include <string.h>
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
55

56
#define NOPTS 129
57
#define MIN(a, b) (((a) < (b))? (a): (b))
58 59 60 61 62 63 64 65 66 67

static int convert_options(int sz_opts)
{
    int co[NOPTS];
    int opts = 0;

    memset(co, 0, sizeof(int) * NOPTS);
    co[SZ_MSB_OPTION_MASK] = AEC_DATA_MSB;
    co[SZ_NN_OPTION_MASK] = AEC_DATA_PREPROCESS;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
68
    for (int i = 1; i < NOPTS; i <<= 1)
69 70
        if (sz_opts & i)
            opts |= co[i];
71 72 73 74

    return opts;
}

75 76 77 78 79 80 81 82 83 84
static int bits_to_bytes(int bit_length)
{
    if (bit_length > 16)
        return 4;
    else if (bit_length > 8)
        return 2;
    else
        return 1;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
85
static void interleave_buffer(void *dest, const void *src,
86 87
                              size_t n, int wordsize)
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
88 89
    const unsigned char *src8 = (unsigned char *)src;
    unsigned char *dest8 = (unsigned char *)dest;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
90

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
91 92
    for (size_t i = 0; i < n / wordsize; i++)
        for (size_t j = 0; j < wordsize; j++)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
93
            dest8[j * (n / wordsize) + i] = src8[i * wordsize + j];
94 95
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
96 97
static void deinterleave_buffer(void *dest, const void *src,
                                size_t n, int wordsize)
98
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
99 100
    const unsigned char *src8 = (unsigned char *)src;
    unsigned char *dest8 = (unsigned char *)dest;
101

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
102 103
    for (size_t i = 0; i < n / wordsize; i++)
        for (size_t j = 0; j < wordsize; j++)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
104
            dest8[i * wordsize + j] = src8[j * (n / wordsize) + i];
105 106
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
107
static void add_padding(void *dest, const void *src, size_t src_length,
108 109
                        size_t line_size, size_t padding_size,
                        int pixel_size, int pp)
110
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
111
    const char zero_pixel[] = {0, 0, 0, 0};
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
112

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
113 114 115
    const char *pixel = zero_pixel;
    size_t j = 0;
    size_t i = 0;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
116
    while (i < src_length) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
117 118
        size_t ps;
        size_t ls = MIN(src_length - i, line_size);
119 120 121
        memcpy((char *)dest + j, (char *)src + i, ls);
        j += ls;
        i += ls;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
122
        if (pp)
123 124
            pixel = (char *)src + i - pixel_size;
        ps = line_size + padding_size - ls;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
125
        for (size_t k = 0; k < ps; k += pixel_size)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
126
            memcpy((char *)dest + j + k, pixel, pixel_size);
127
        j += ps;
128 129 130
    }
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
131
static void remove_padding(void *buf, size_t buf_length,
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
132 133
                           size_t line_size, size_t padding_size,
                           int pixel_size)
134
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
135
    size_t padded_line_size = line_size + padding_size;
136

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
137 138
    size_t i = line_size;
    for (size_t j = padded_line_size; j < buf_length; j += padded_line_size) {
139
        memmove((char *)buf + i, (char *)buf + j, line_size);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
140
        i += line_size;
141 142 143
    }
}

144 145 146
int SZ_BufftoBuffCompress(void *dest, size_t *destLen,
                          const void *source, size_t sourceLen,
                          SZ_com_t *param)
147
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
148
    struct aec_stream strm;
149 150
    void *buf = 0;
    void *padbuf = 0;
151
    int status;
152 153 154 155 156 157
    int interleave;
    int pixel_size;
    int aec_status;
    size_t scanlines;
    size_t padbuf_size;
    size_t padding_size;
158 159 160 161

    strm.block_size = param->pixels_per_block;
    strm.rsi = (param->pixels_per_scanline + param->pixels_per_block - 1)
        / param->pixels_per_block;
162
    strm.flags = AEC_NOT_ENFORCE | convert_options(param->options_mask);
163 164
    strm.avail_out = *destLen;
    strm.next_out = dest;
165

166
    interleave = param->bits_per_pixel == 32 || param->bits_per_pixel == 64;
167
    if (interleave) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
168
        strm.bits_per_sample = 8;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
169
        buf = malloc(sourceLen);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
170 171 172 173
        if (buf == NULL) {
            status = SZ_MEM_ERROR;
            goto CLEANUP;
        }
174 175
        interleave_buffer(buf, source, sourceLen, param->bits_per_pixel / 8);
    } else {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
176
        strm.bits_per_sample = param->bits_per_pixel;
177
        buf = (void *)source;
178
    }
179

180
    pixel_size = bits_to_bytes(strm.bits_per_sample);
181

182
    scanlines = (sourceLen / pixel_size + param->pixels_per_scanline - 1)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
183
        / param->pixels_per_scanline;
184
    padbuf_size = strm.rsi * strm.block_size * pixel_size * scanlines;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
185 186 187 188 189
    padbuf = malloc(padbuf_size);
    if (padbuf == NULL) {
        status = SZ_MEM_ERROR;
        goto CLEANUP;
    }
190

191
    padding_size =
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
192 193
        (strm.rsi * strm.block_size - param->pixels_per_scanline)
        * pixel_size;
194

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
195 196 197 198 199 200
    add_padding(padbuf, buf, sourceLen,
                param->pixels_per_scanline * pixel_size,
                padding_size, pixel_size,
                strm.flags & AEC_DATA_PREPROCESS);
    strm.next_in = padbuf;
    strm.avail_in = padbuf_size;
201

202
    aec_status = aec_buffer_encode(&strm);
203 204 205 206
    if (aec_status == AEC_STREAM_ERROR)
        status = SZ_OUTBUFF_FULL;
    else
        status = aec_status;
207 208
    *destLen = strm.total_out;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
209
CLEANUP:
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
210
    if (padbuf)
211
        free(padbuf);
212
    if (interleave && buf)
213
        free(buf);
214
    return status;
215 216
}

217 218 219
int SZ_BufftoBuffDecompress(void *dest, size_t *destLen,
                            const void *source, size_t sourceLen,
                            SZ_com_t *param)
220
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
221
    struct aec_stream strm;
222
    void *buf = 0;
223
    int status;
224 225 226 227
    int pad_scanline;
    int deinterleave;
    int extra_buffer;
    int pixel_size;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
228
    size_t total_out;
229
    size_t scanlines;
230

231 232 233 234 235 236 237
    strm.block_size = param->pixels_per_block;
    strm.rsi = (param->pixels_per_scanline + param->pixels_per_block - 1)
        / param->pixels_per_block;
    strm.flags = convert_options(param->options_mask);
    strm.avail_in = sourceLen;
    strm.next_in = source;

238 239
    pad_scanline = param->pixels_per_scanline % param->pixels_per_block;
    deinterleave = (param->bits_per_pixel == 32
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
240
                        || param->bits_per_pixel == 64);
241
    extra_buffer = pad_scanline || deinterleave;
242

243
    if (deinterleave)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
244
        strm.bits_per_sample = 8;
245
    else
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
246 247
        strm.bits_per_sample = param->bits_per_pixel;
    pixel_size = bits_to_bytes(strm.bits_per_sample);
248

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
249

250
    if (extra_buffer) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
251
        size_t buf_size;
252 253 254
        if (pad_scanline) {
            scanlines = (*destLen / pixel_size + param->pixels_per_scanline - 1)
                / param->pixels_per_scanline;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
255
            buf_size = strm.rsi * strm.block_size * pixel_size * scanlines;
256 257 258 259
        } else {
            buf_size = *destLen;
        }
        buf = malloc(buf_size);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
260 261 262 263
        if (buf == NULL) {
            status = SZ_MEM_ERROR;
            goto CLEANUP;
        }
264
        strm.next_out = buf;
265
        strm.avail_out = buf_size;
266 267
    } else {
        strm.next_out = dest;
268
        strm.avail_out = *destLen;
269
    }
270

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
271
    status = aec_buffer_decode(&strm);
272
    if (status != AEC_OK)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
273
        goto CLEANUP;
274

275
    if (pad_scanline) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
276
        size_t padding_size =
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
277 278 279 280 281 282
            (strm.rsi * strm.block_size - param->pixels_per_scanline)
            * pixel_size;
        remove_padding(buf, strm.total_out,
                       param->pixels_per_scanline * pixel_size,
                       padding_size, pixel_size);
        total_out = scanlines * param->pixels_per_scanline * pixel_size;
283 284 285 286 287 288
    } else {
        total_out = strm.total_out;
    }

    if (total_out < *destLen)
        *destLen = total_out;
289

290
    if (deinterleave)
291
        deinterleave_buffer(dest, buf, *destLen, param->bits_per_pixel / 8);
292 293 294
    else if (pad_scanline)
        memcpy(dest, buf, *destLen);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
295
CLEANUP:
296
    if (extra_buffer && buf)
297 298
        free(buf);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
299
    return status;
300
}
301 302 303 304 305

int SZ_encoder_enabled(void)
{
    return 1;
}
306 307 308

/* netcdf searches for SZ_Compress in configure */
char SZ_Compress() { return SZ_OK; }