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;
72 int chunk, total_out, status, c;
73 int input_avail, output_avail;
74 char *outfn, *infn, *ext;
80 strm.bits_per_sample = 8;
83 strm.flags = AEC_DATA_PREPROCESS;
86 while ((c = getopt (argc, argv, "d3Mscb:B:R:J:")) != -1)
96 strm.bits_per_sample = atoi(optarg);
99 strm.block_size = atoi(optarg);
102 strm.rsi = atoi(optarg);
108 strm.flags |= AEC_DATA_SIGNED;
111 strm.flags |= AEC_DATA_MSB;
114 strm.flags |= AEC_DATA_3BYTE;
118 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
119 else if (isprint (optopt))
120 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
123 "Unknown option character `\\x%x'.\n",
136 fprintf(stderr, "Usage: %s [ -c ] [ -b chunksize ] name\n", argv[0]);
140 if (strm.bits_per_sample > 16)
142 if (strm.bits_per_sample <= 24 && strm.flags & AEC_DATA_3BYTE)
147 else if (strm.bits_per_sample > 8)
152 out = (unsigned char *)malloc(chunk);
153 in = (unsigned char *)malloc(chunk);
156 if (in == NULL || out == NULL)
161 strm.avail_out = chunk;
167 if ((infp = fopen(infn, "r")) == NULL)
176 outfn = malloc(strlen(infn) + 4);
182 if ((ext = strstr(infn, ".aec")) == NULL)
184 fprintf(stderr, "Error: input file needs to end with .aec\n");
187 strncpy(outfn, infn, ext - infn);
191 sprintf(outfn, "%s.aec", infn);
194 if ((outfp = fopen(outfn, "w")) == NULL)
200 if (aec_decode_init(&strm) != AEC_OK)
205 if (aec_encode_init(&strm) != AEC_OK)
209 while(input_avail || output_avail)
211 if (strm.avail_in == 0 && input_avail)
213 strm.avail_in = fread(in, 1, chunk, infp);
214 if (strm.avail_in != chunk)
220 status = aec_decode(&strm, AEC_NO_FLUSH);
222 status = aec_encode(&strm, AEC_NO_FLUSH);
224 if (status != AEC_OK)
226 fprintf(stderr, "error is %i\n", status);
230 if (strm.total_out - total_out > 0)
232 fwrite(out, strm.total_out - total_out, 1, outfp);
233 total_out = strm.total_out;
236 strm.avail_out = chunk;
247 aec_decode_end(&strm);
251 if ((status = aec_encode(&strm, AEC_FLUSH)) != AEC_OK)
253 fprintf(stderr, "error is %i\n", status);
257 if (strm.total_out - total_out > 0)
259 fwrite(out, strm.total_out - total_out, 1, outfp);
262 aec_encode_end(&strm);