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