aec.c 7.09 KB
Newer Older
1 2 3 4
/**
 * @file aec.c
 *
 * @section LICENSE
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
5
 * Copyright 2012 - 2019
6
 *
7 8 9 10
 * Mathis Rosenhauer, Moritz Hanke, Joerg Behrens
 * Deutsches Klimarechenzentrum GmbH
 * Bundesstr. 45a
 * 20146 Hamburg Germany
11
 *
12 13 14 15 16
 * Luis Kornblueh
 * Max-Planck-Institut fuer Meteorologie
 * Bundesstr. 53
 * 20146 Hamburg
 * Germany
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
 *
 * 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
 *
 * CLI frontend for Adaptive Entropy Coding library
 *
 */

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
55
#define CHUNK 10485760
56

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
57
int get_param(unsigned int *param, int *iarg, char *argv[])
58 59 60 61 62 63 64 65 66 67 68 69 70
{
    if (strlen(argv[*iarg]) == 2) {
        (*iarg)++;
        if (argv[*iarg][0] == '-')
            return 1;
        else
            *param = atoi(argv[*iarg]);
    } else {
        *param = atoi(&argv[*iarg][2]);
    }
    return 0;
}

71 72
int main(int argc, char *argv[])
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
73
    struct aec_stream strm;
74 75
    unsigned char *in;
    unsigned char *out;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
76
    size_t total_out;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
77 78
    unsigned int chunk;
    int status;
79
    int input_avail, output_avail;
80
    char *infn, *outfn;
81
    FILE *infp, *outfp;
82 83 84
    int dflag;
    char *opt;
    int iarg;
85 86

    chunk = CHUNK;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
87
    strm.bits_per_sample = 8;
88
    strm.block_size = 8;
89
    strm.rsi = 2;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
90
    strm.flags = AEC_DATA_PREPROCESS;
91 92 93
    dflag = 0;
    iarg = 1;

94
    while (iarg < argc - 2) {
95 96 97 98
        opt = argv[iarg];
        if (opt[0] != '-')
            goto FAIL;
        switch (opt[1]) {
99 100
        case '3':
            strm.flags |= AEC_DATA_3BYTE;
101
            break;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
102 103 104
        case 'N':
            strm.flags &= ~AEC_DATA_PREPROCESS;
            break;
105
        case 'b':
106 107
            if (get_param(&chunk, &iarg, argv))
                goto FAIL;
108 109 110
            break;
        case 'd':
            dflag = 1;
111
            break;
112
        case 'j':
113 114
            if (get_param(&strm.block_size, &iarg, argv))
                goto FAIL;
115
            break;
116 117 118 119
        case 'm':
            strm.flags |= AEC_DATA_MSB;
            break;
        case 'n':
120 121
            if (get_param(&strm.bits_per_sample, &iarg, argv))
                goto FAIL;
122
            break;
123 124 125
        case 'p':
            strm.flags |= AEC_PAD_RSI;
            break;
126
        case 'r':
127 128
            if (get_param(&strm.rsi, &iarg, argv))
                goto FAIL;
129
            break;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
130
        case 's':
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
131
            strm.flags |= AEC_DATA_SIGNED;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
132
            break;
133
        case 't':
134
            strm.flags |= AEC_RESTRICTED;
135
            break;
136
        default:
137
            goto FAIL;
138
        }
139
        iarg++;
140 141
    }

142 143 144
    if (argc - iarg < 2)
        goto FAIL;

145
    infn = argv[iarg];
146
    outfn = argv[iarg + 1];
147

148
    if (strm.bits_per_sample > 16) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
149
        if (strm.bits_per_sample <= 24 && strm.flags & AEC_DATA_3BYTE)
150 151 152
            chunk *= 3;
        else
            chunk *= 4;
153
    } else if (strm.bits_per_sample > 8) {
154 155 156
        chunk *= 2;
    }

157 158
    out = (unsigned char *)malloc(chunk);
    in = (unsigned char *)malloc(chunk);
159 160 161 162 163 164 165 166 167 168 169 170

    if (in == NULL || out == NULL)
        exit(-1);

    total_out = 0;
    strm.avail_in = 0;
    strm.avail_out = chunk;
    strm.next_out = out;

    input_avail = 1;
    output_avail = 1;

171 172 173
    if ((infp = fopen(infn, "rb")) == NULL) {
        fprintf(stderr, "ERROR: cannot open input file %s\n", infn);
        return 1;
174
    }
175 176 177
    if ((outfp = fopen(outfn, "wb")) == NULL) {
        fprintf(stderr, "ERROR: cannot open output file %s\n", infn);
        return 1;
178 179
    }

180 181 182 183 184 185 186 187
    if (dflag)
        status = aec_decode_init(&strm);
    else
        status = aec_encode_init(&strm);

    if (status != AEC_OK) {
        fprintf(stderr, "ERROR: initialization failed (%d)\n", status);
        return 1;
188 189
    }

190 191
    while(input_avail || output_avail) {
        if (strm.avail_in == 0 && input_avail) {
192 193 194
            strm.avail_in = fread(in, 1, chunk, infp);
            if (strm.avail_in != chunk)
                input_avail = 0;
195
            strm.next_in = in;
196 197
        }

198
        if (dflag)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
199
            status = aec_decode(&strm, AEC_NO_FLUSH);
200
        else
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
201
            status = aec_encode(&strm, AEC_NO_FLUSH);
202

203
        if (status != AEC_OK) {
204
            fprintf(stderr, "ERROR: %i\n", status);
205 206 207
            return 1;
        }

208
        if (strm.total_out - total_out > 0) {
209 210 211 212 213
            fwrite(out, strm.total_out - total_out, 1, outfp);
            total_out = strm.total_out;
            output_avail = 1;
            strm.next_out = out;
            strm.avail_out = chunk;
214
        } else {
215 216 217 218 219
            output_avail = 0;
        }

    }

220
    if (dflag) {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
221
        aec_decode_end(&strm);
222 223
    } else {
        if ((status = aec_encode(&strm, AEC_FLUSH)) != AEC_OK) {
224
            fprintf(stderr, "ERROR: while flushing output (%i)\n", status);
225 226 227 228 229 230
            return 1;
        }

        if (strm.total_out - total_out > 0)
            fwrite(out, strm.total_out - total_out, 1, outfp);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
231
        aec_encode_end(&strm);
232 233 234 235 236 237 238
    }

    fclose(infp);
    fclose(outfp);
    free(in);
    free(out);
    return 0;
239 240

FAIL:
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    fprintf(stderr, "NAME\n\taec - encode or decode files ");
    fprintf(stderr, "with Adaptive Entropy Coding\n\n");
    fprintf(stderr, "SYNOPSIS\n\taec [OPTION]... SOURCE DEST\n");
    fprintf(stderr, "\nOPTIONS\n");
    fprintf(stderr, "\t-3\n\t\t24 bit samples are stored in 3 bytes\n");
    fprintf(stderr, "\t-N\n\t\tdisable pre/post processing\n");
    fprintf(stderr, "\t-b size\n\t\tinternal buffer size in bytes\n");
    fprintf(stderr, "\t-d\n\t\tdecode SOURCE. If -d is not used: encode.\n");
    fprintf(stderr, "\t-j samples\n\t\tblock size in samples\n");
    fprintf(stderr, "\t-m\n\t\tsamples are MSB first. Default is LSB\n");
    fprintf(stderr, "\t-n bits\n\t\tbits per sample\n");
    fprintf(stderr, "\t-p\n\t\tpad RSI to byte boundary\n");
    fprintf(stderr, "\t-r blocks\n\t\treference sample interval in blocks\n");
    fprintf(stderr, "\t-s\n\t\tsamples are signed. Default is unsigned\n");
    fprintf(stderr, "\t-t\n\t\tuse restricted set of code options\n\n");
256
    return 1;
257
}