aec.c 7.11 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 51
#include "config.h"
#include "libaec.h"
52 53 54
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
55

56
#define CHUNK 10485760
57

58
int get_param(unsigned int *param, int *iarg, char *argv[])
59 60 61 62 63 64 65 66 67 68 69 70 71
{
    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;
}

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

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

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

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

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

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

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

    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;

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

181 182 183 184 185 186 187 188
    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;
189 190
    }

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

199
        if (dflag)
200
            status = aec_decode(&strm, AEC_NO_FLUSH);
201
        else
202
            status = aec_encode(&strm, AEC_NO_FLUSH);
203

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

209
        if (strm.total_out - total_out > 0) {
210 211 212 213 214
            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;
215
        } else {
216 217 218 219 220
            output_avail = 0;
        }

    }

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

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

232
        aec_encode_end(&strm);
233 234 235 236 237 238 239
    }

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

FAIL:
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    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");
257
    return 1;
258
}