aec.c 6.68 KB
Newer Older
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
/**
 * @file aec.c
 *
 * @author Mathis Rosenhauer, Deutsches Klimarechenzentrum
 * @author Moritz Hanke, Deutsches Klimarechenzentrum
 * @author Joerg Behrens, Deutsches Klimarechenzentrum
 * @author Luis Kornblueh, Max-Planck-Institut fuer Meteorologie
 *
 * @section LICENSE
 * Copyright 2012
 *
 * Mathis Rosenhauer,                 Luis Kornblueh
 * Moritz Hanke,
 * Joerg Behrens
 *
 * Deutsches Klimarechenzentrum GmbH  Max-Planck-Institut fuer Meteorologie
 * Bundesstr. 45a                     Bundesstr. 53
 * 20146 Hamburg                      20146 Hamburg
 * Germany                            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
 *
 * CLI frontend for Adaptive Entropy Coding library
 *
 */

53 54 55 56 57
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
58 59

#ifndef _AIX
60
#include <getopt.h>
61 62
#endif

63
#include <libaec.h>
64 65 66 67 68

#define CHUNK 1024

int main(int argc, char *argv[])
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
69
    struct aec_stream strm;
70 71
    unsigned char *in;
    unsigned char *out;
72 73
    int chunk, total_out, status, c;
    int input_avail, output_avail;
74
    char *outfn, *infn, *ext;
75 76
    FILE *infp, *outfp;
    int cflag = 0;
77
    int dflag = 0;
78 79

    chunk = CHUNK;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
80
    strm.bits_per_sample = 8;
81
    strm.block_size = 8;
82
    strm.rsi = 2;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
83
    strm.flags = AEC_DATA_PREPROCESS;
84 85
    opterr = 0;

86
    while ((c = getopt (argc, argv, "d3Mscb:B:R:J:")) != -1)
87 88
        switch (c)
        {
89 90 91
        case 'd':
            dflag = 1;
            break;
92
        case 'b':
93
            chunk = atoi(optarg);
94
            break;
95
        case 'B':
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
96
            strm.bits_per_sample = atoi(optarg);
97
            break;
98 99 100
        case 'J':
            strm.block_size = atoi(optarg);
            break;
101 102
        case 'R':
            strm.rsi = atoi(optarg);
103
            break;
104 105 106
        case 'c':
            cflag = 1;
            break;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
107
        case 's':
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
108
            strm.flags |= AEC_DATA_SIGNED;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
109
            break;
110
        case 'M':
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
111
            strm.flags |= AEC_DATA_MSB;
112
            break;
113
        case '3':
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
114
            strm.flags |= AEC_DATA_3BYTE;
115
            break;
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        case '?':
            if (optopt == 'b')
                fprintf (stderr, "Option -%c requires an argument.\n", optopt);
            else if (isprint (optopt))
                fprintf (stderr, "Unknown option `-%c'.\n", optopt);
            else
                fprintf (stderr,
                         "Unknown option character `\\x%x'.\n",
                         optopt);
            return 1;
        default:
            abort ();
        }

    if (optind < argc)
    {
        infn = argv[optind];
    }
    else
    {
        fprintf(stderr, "Usage: %s [ -c ] [ -b chunksize ] name\n", argv[0]);
        exit(-1);
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
140
    if (strm.bits_per_sample > 16)
141
    {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
142
        if (strm.bits_per_sample <= 24 && strm.flags & AEC_DATA_3BYTE)
143 144 145 146
            chunk *= 3;
        else
            chunk *= 4;
    }
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
147
    else if (strm.bits_per_sample > 8)
148 149 150 151
    {
        chunk *= 2;
    }

152 153
    out = (unsigned char *)malloc(chunk);
    in = (unsigned char *)malloc(chunk);
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


    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;

    if ((infp = fopen(infn, "r")) == NULL)
        exit(-1);

    if (cflag)
    {
        outfp = stdout;
    }
    else
    {
        outfn = malloc(strlen(infn) + 4);
        if (outfn == NULL)
            exit(-1);

180 181
        if (dflag)
        {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
182
            if ((ext = strstr(infn, ".aec")) == NULL)
183
            {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
184
                fprintf(stderr, "Error: input file needs to end with .aec\n");
185 186 187 188 189 190
                exit(-1);
            }
            strncpy(outfn, infn, ext - infn);
        }
        else
        {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
191
            sprintf(outfn, "%s.aec", infn);
192
        }
193 194 195 196 197

        if ((outfp = fopen(outfn, "w")) == NULL)
            exit(-1);
    }

198 199
    if (dflag)
    {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
200
        if (aec_decode_init(&strm) != AEC_OK)
201 202 203 204
            return 1;
    }
    else
    {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
205
        if (aec_encode_init(&strm) != AEC_OK)
206 207 208
            return 1;
    }

209 210 211 212 213 214 215
    while(input_avail || output_avail)
    {
        if (strm.avail_in == 0 && input_avail)
        {
            strm.avail_in = fread(in, 1, chunk, infp);
            if (strm.avail_in != chunk)
                input_avail = 0;
216
            strm.next_in = in;
217 218
        }

219
        if (dflag)
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
220
            status = aec_decode(&strm, AEC_NO_FLUSH);
221
        else
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
222
            status = aec_encode(&strm, AEC_NO_FLUSH);
223

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
224
        if (status != AEC_OK)
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
        {
            fprintf(stderr, "error is %i\n", status);
            return 1;
        }

        if (strm.total_out - total_out > 0)
        {
            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;
        }
        else
        {
            output_avail = 0;
        }

    }

245
    if (dflag)
246
    {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
247
        aec_decode_end(&strm);
248
    }
249
    else
250
    {
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
251
        if ((status = aec_encode(&strm, AEC_FLUSH)) != AEC_OK)
252 253 254 255 256 257 258 259 260 261
        {
            fprintf(stderr, "error is %i\n", status);
            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
262
        aec_encode_end(&strm);
263 264 265 266 267 268 269 270 271 272 273 274 275
    }

    fclose(infp);
    fclose(outfp);
    free(in);
    free(out);
    if (!cflag)
    {
        unlink(infn);
        free(outfn);
    }
    return 0;
}