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
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
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;
87 while ((c = getopt (argc, argv, "3b:cdj:mn:pr:st")) != -1)
90 strm.flags |= AEC_DATA_3BYTE;
102 strm.block_size = atoi(optarg);
105 strm.flags |= AEC_DATA_MSB;
108 strm.bits_per_sample = atoi(optarg);
111 strm.flags |= AEC_PAD_RSI;
114 strm.rsi = atoi(optarg);
117 strm.flags |= AEC_DATA_SIGNED;
120 strm.flags |= AEC_RESTRICTED;
124 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
125 else if (isprint (optopt))
126 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
129 "Unknown option character `\\x%x'.\n",
139 fprintf(stderr, "Usage: %s [OPTION] SOURCE\n", argv[0]);
140 fprintf(stderr, "\nOPTIONS\n");
141 fprintf(stderr, "-3\n 24 bit samples are sored in 3 bytes\n");
142 fprintf(stderr, "-b size\n internal buffer sizein bytes\n");
143 fprintf(stderr, "-c\n write output on standard output\n");
144 fprintf(stderr, "-d\n decode SOURCE. If -d is not used: encode.\n");
145 fprintf(stderr, "-j samples\n block size in samples\n");
146 fprintf(stderr, "-m\n samples are MSB first. Default is LSB\n");
147 fprintf(stderr, "-n bits\n bits per sample\n");
148 fprintf(stderr, "-p\n pad RSI to byte boundary\n");
149 fprintf(stderr, "-r blocks\n reference sample interval in blocks\n");
150 fprintf(stderr, "-s\n samples are signed. Default is unsigned\n");
151 fprintf(stderr, "-t\n use restricted set of code options\n\n");
155 if (strm.bits_per_sample > 16) {
156 if (strm.bits_per_sample <= 24 && strm.flags & AEC_DATA_3BYTE)
160 } else if (strm.bits_per_sample > 8) {
164 out = (unsigned char *)malloc(chunk);
165 in = (unsigned char *)malloc(chunk);
168 if (in == NULL || out == NULL)
173 strm.avail_out = chunk;
179 if ((infp = fopen(infn, "r")) == NULL)
185 outfn = malloc(strlen(infn) + 4);
190 if ((ext = strstr(infn, ".rz")) == NULL) {
191 fprintf(stderr, "ERROR: input file needs to end with .rz\n");
194 strncpy(outfn, infn, ext - infn);
196 sprintf(outfn, "%s.rz", infn);
199 if ((outfp = fopen(outfn, "w")) == NULL)
204 if (aec_decode_init(&strm) != AEC_OK) {
205 fprintf(stderr, "ERROR: Initialization failed\n");
209 if (aec_encode_init(&strm) != AEC_OK) {
210 fprintf(stderr, "ERROR: Initialization failed\n");
215 while(input_avail || output_avail) {
216 if (strm.avail_in == 0 && input_avail) {
217 strm.avail_in = fread(in, 1, chunk, infp);
218 if (strm.avail_in != chunk)
224 status = aec_decode(&strm, AEC_NO_FLUSH);
226 status = aec_encode(&strm, AEC_NO_FLUSH);
228 if (status != AEC_OK) {
229 fprintf(stderr, "ERROR: %i\n", status);
233 if (strm.total_out - total_out > 0) {
234 fwrite(out, strm.total_out - total_out, 1, outfp);
235 total_out = strm.total_out;
238 strm.avail_out = chunk;
246 aec_decode_end(&strm);
248 if ((status = aec_encode(&strm, AEC_FLUSH)) != AEC_OK) {
249 fprintf(stderr, "ERROR: %i\n", status);
253 if (strm.total_out - total_out > 0)
254 fwrite(out, strm.total_out - total_out, 1, outfp);
256 aec_encode_end(&strm);