More options for encode/decode to aid in testing
[platform/upstream/libaec.git] / src / encode.c
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <inttypes.h>
6 #include <string.h>
7 #include "libae.h"
8
9 #define CHUNK 1024
10
11 int main(int argc, char *argv[])
12 {
13     ae_stream strm;
14     uint8_t *in;
15     uint8_t *out;
16     int chunk, total_out, status, c;
17     int input_avail, output_avail;
18     char *outfn, *infn;
19     FILE *infp, *outfp;
20     int cflag = 0;
21
22     chunk = CHUNK;
23     strm.bit_per_sample = 8;
24     strm.block_size = 8;
25     strm.segment_size = 2;
26     strm.flags = AE_DATA_MSB | AE_DATA_PREPROCESS;
27     opterr = 0;
28
29     while ((c = getopt (argc, argv, "cb:B:S:")) != -1)
30         switch (c)
31         {
32         case 'b':
33             chunk = 2 * atoi(optarg);
34             break;
35         case 'B':
36             strm.bit_per_sample = atoi(optarg);
37             break;
38         case 'S':
39             strm.segment_size = atoi(optarg);
40             break;
41         case 'c':
42             cflag = 1;
43             break;
44         case '?':
45             if (optopt == 'b')
46                 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
47             else if (isprint (optopt))
48                 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
49             else
50                 fprintf (stderr,
51                          "Unknown option character `\\x%x'.\n",
52                          optopt);
53             return 1;
54         default:
55             abort ();
56         }
57
58     if (optind < argc)
59     {
60         infn = argv[optind];
61     }
62     else
63     {
64         fprintf(stderr, "Usage: %s [ -c ] [ -b chunksize ] name\n", argv[0]);
65         exit(-1);
66     }
67
68     out = (uint8_t *)malloc(chunk);
69     in = (uint8_t *)malloc(chunk);
70
71
72     if (in == NULL || out == NULL)
73         exit(-1);
74
75     if (ae_encode_init(&strm) != AE_OK)
76         return 1;
77
78     total_out = 0;
79     strm.avail_in = 0;
80     strm.avail_out = chunk;
81     strm.next_out = out;
82
83     input_avail = 1;
84     output_avail = 1;
85
86     if ((infp = fopen(infn, "r")) == NULL)
87         exit(-1);
88
89     if (cflag)
90     {
91         outfp = stdout;
92     }
93     else
94     {
95         outfn = malloc(strlen(infn) + 4);
96         if (outfn == NULL)
97             exit(-1);
98
99         sprintf(outfn, "%s.aee", infn);
100
101         if ((outfp = fopen(outfn, "w")) == NULL)
102             exit(-1);
103     }
104
105     while(input_avail || output_avail)
106     {
107         if (strm.avail_in == 0 && input_avail)
108         {
109             strm.avail_in = fread(in, 1, chunk, infp);
110             if (strm.avail_in != chunk)
111                 input_avail = 0;
112             strm.next_in = (uint8_t *)in;
113         }
114
115         if ((status = ae_encode(&strm, AE_NO_FLUSH)) != AE_OK)
116         {
117             fprintf(stderr, "error is %i\n", status);
118             return 1;
119         }
120
121         if (strm.total_out - total_out > 0)
122         {
123             fwrite(out, strm.total_out - total_out, 1, outfp);
124             total_out = strm.total_out;
125             output_avail = 1;
126             strm.next_out = out;
127             strm.avail_out = chunk;
128         }
129         else
130         {
131             output_avail = 0;
132         }
133
134     }
135
136     if ((status = ae_encode(&strm, AE_FLUSH)) != AE_OK)
137     {
138         fprintf(stderr, "error is %i\n", status);
139         return 1;
140     }
141
142     if (strm.total_out - total_out > 0)
143     {
144         fwrite(out, strm.total_out - total_out, 1, outfp);
145     }
146
147     ae_encode_end(&strm);
148     fclose(infp);
149     fclose(outfp);
150     free(in);
151     free(out);
152     if (!cflag)
153     {
154         unlink(infn);
155         free(outfn);
156     }
157     return 0;
158 }