4 * @author Mathis Rosenhauer, Deutsches Klimarechenzentrum
5 * @author Moritz Hanke, Deutsches Klimarechenzentrum
6 * @author Joerg Behrens, Deutsches Klimarechenzentrum
7 * @author Luis Kornblueh, Max-Planck-Institut fuer Meteorologie
10 * Copyright 2012 - 2014
12 * Mathis Rosenhauer, Luis Kornblueh
16 * Deutsches Klimarechenzentrum GmbH Max-Planck-Institut fuer Meteorologie
17 * Bundesstr. 45a Bundesstr. 53
18 * 20146 Hamburg 20146 Hamburg
21 * All rights reserved.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above
30 * copyright notice, this list of conditions and the following
31 * disclaimer in the documentation and/or other materials provided
32 * with the distribution.
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
37 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
38 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
39 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
45 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * @section DESCRIPTION
49 * CLI frontend for Adaptive Entropy Coding library
65 #define CHUNK 10485760
67 int main(int argc, char *argv[])
69 struct aec_stream strm;
74 int input_avail, output_avail;
75 char *outfn, *infn, *ext;
81 strm.bits_per_sample = 8;
84 strm.flags = AEC_DATA_PREPROCESS;
88 while ((c = getopt (argc, argv, "3Nb:cdj:mn:pr:st")) != -1)
91 strm.flags |= AEC_DATA_3BYTE;
94 strm.flags &= ~AEC_DATA_PREPROCESS;
106 strm.block_size = atoi(optarg);
109 strm.flags |= AEC_DATA_MSB;
112 strm.bits_per_sample = atoi(optarg);
115 strm.flags |= AEC_PAD_RSI;
118 strm.rsi = atoi(optarg);
121 strm.flags |= AEC_DATA_SIGNED;
124 strm.flags |= AEC_RESTRICTED;
128 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
129 else if (isprint (optopt))
130 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
133 "Unknown option character `\\x%x'.\n",
143 fprintf(stderr, "Usage: %s [OPTION] SOURCE\n", argv[0]);
144 fprintf(stderr, "\nOPTIONS\n");
145 fprintf(stderr, "-3\n 24 bit samples are stored in 3 bytes\n");
146 fprintf(stderr, "-N\n disable pre/post processing\n");
147 fprintf(stderr, "-b size\n internal buffer size in bytes\n");
148 fprintf(stderr, "-c\n write output on standard output\n");
149 fprintf(stderr, "-d\n decode SOURCE. If -d is not used: encode.\n");
150 fprintf(stderr, "-j samples\n block size in samples\n");
151 fprintf(stderr, "-m\n samples are MSB first. Default is LSB\n");
152 fprintf(stderr, "-n bits\n bits per sample\n");
153 fprintf(stderr, "-p\n pad RSI to byte boundary\n");
154 fprintf(stderr, "-r blocks\n reference sample interval in blocks\n");
155 fprintf(stderr, "-s\n samples are signed. Default is unsigned\n");
156 fprintf(stderr, "-t\n use restricted set of code options\n\n");
160 if (strm.bits_per_sample > 16) {
161 if (strm.bits_per_sample <= 24 && strm.flags & AEC_DATA_3BYTE)
165 } else if (strm.bits_per_sample > 8) {
169 out = (unsigned char *)malloc(chunk);
170 in = (unsigned char *)malloc(chunk);
173 if (in == NULL || out == NULL)
178 strm.avail_out = chunk;
184 if ((infp = fopen(infn, "r")) == NULL)
190 outfn = malloc(strlen(infn) + 4);
195 if ((ext = strstr(infn, ".rz")) == NULL) {
196 fprintf(stderr, "ERROR: input file needs to end with .rz\n");
199 strncpy(outfn, infn, ext - infn);
201 sprintf(outfn, "%s.rz", infn);
204 if ((outfp = fopen(outfn, "w")) == NULL)
209 if (aec_decode_init(&strm) != AEC_OK) {
210 fprintf(stderr, "ERROR: Initialization failed\n");
214 if (aec_encode_init(&strm) != AEC_OK) {
215 fprintf(stderr, "ERROR: Initialization failed\n");
220 while(input_avail || output_avail) {
221 if (strm.avail_in == 0 && input_avail) {
222 strm.avail_in = fread(in, 1, chunk, infp);
223 if (strm.avail_in != chunk)
229 status = aec_decode(&strm, AEC_NO_FLUSH);
231 status = aec_encode(&strm, AEC_NO_FLUSH);
233 if (status != AEC_OK) {
234 fprintf(stderr, "ERROR: %i\n", status);
238 if (strm.total_out - total_out > 0) {
239 fwrite(out, strm.total_out - total_out, 1, outfp);
240 total_out = strm.total_out;
243 strm.avail_out = chunk;
251 aec_decode_end(&strm);
253 if ((status = aec_encode(&strm, AEC_FLUSH)) != AEC_OK) {
254 fprintf(stderr, "ERROR: %i\n", status);
258 if (strm.total_out - total_out > 0)
259 fwrite(out, strm.total_out - total_out, 1, outfp);
261 aec_encode_end(&strm);