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