-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.c
More file actions
291 lines (261 loc) · 9.88 KB
/
encoder.c
File metadata and controls
291 lines (261 loc) · 9.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <error.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <argp.h>
#include <unistd.h>
#include <math.h>
#include "bit_string.h"
#include "bit_string_writer.h"
#include "lz77.h"
#include "log.h"
#include "queue.h"
#include "block.h"
#include "delta.h"
#define DEFAULT_BLOCK_SIZE 32768
#define DEFAULT_WINDOW_SIZE 32768
#define DEFAULT_THREADS 8
/** Encoder version */
const char *argp_program_version = "eac_encode 0.2";
/** Email to send encoder bug reports */
const char *argp_program_bug_address = "<dmitry.zbarski@gmail.com>";
/** Program documentation. */
static char doc[] = "Entropy Adaptive Coding - encoder";
/** A description of the arguments we accept. */
static char args_doc[] = " -i INPUT_FILE -o OUTPUT_FILE";
/** The options encoder accpets */
static struct argp_option options[] = {
{"verbose", 'v', 0, OPTION_ARG_OPTIONAL, "Produce verbose output" },
{"debug", 'd', 0, OPTION_ARG_OPTIONAL, "Don't produce any output" },
{"eac", 'e', 0, OPTION_ARG_OPTIONAL, "Use Adaptive Entropy Coding" },
{"window-size", 'n', "FILE", 0, "LZ77 window size (ignored when --eac is used)" },
{"input", 'i', "FILE", 0, "Input file" },
{"output", 'o', "FILE", 0, "Output file" },
{"block-size", 'b', "BLOCK_SIZE", 0, "Block size"},
{"threads", 't', "THREADS", 0, "Number of concurrent threads"},
{ 0 }
};
/** \brief Encoder arguments structure */
struct enc_arguments {
int debug; /**< debug parameter */
int verbose; /**< verbose parmeter */
int eac; /**< eac parameter */
int block_size; /**< block size */
size_t window_size; /**< window size parameter */
char *input_file; /**< input file parameter */
char *output_file; /**< output file parameter */
int threads; /**< number of threads */
};
/** Parse a single option of encoder */
static error_t enc_parse_opt(int key, char *arg, struct argp_state *state)
{
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct enc_arguments *arguments = state->input;
switch (key)
{
case 'd':
arguments->verbose = 1;
arguments->debug = 1;
break;
case 'v':
arguments->verbose = 1;
break;
case 'i':
arguments->input_file = arg;
break;
case 'o':
arguments->output_file = arg;
break;
case 'n':
arguments->window_size = atoi(arg) * 8;
break;
case 'e':
arguments->eac = 1;
break;
case 'b':
arguments->block_size = atoi(arg);
break;
case 't':
arguments->threads = atoi(arg);
break;
case ARGP_KEY_ARG:
if(state->arg_num >= 1)
argp_usage (state);
break;
case ARGP_KEY_END:
if(arguments->input_file == 0 || arguments->output_file == 0)
argp_usage (state);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
/** Encoder argp parser */
static struct argp argp = { options, enc_parse_opt, args_doc, doc };
/**
* \brief Convert byte array into bit string
*
* Convert given byte array into newly allocated bit string.
*
* <b>NOTE:</b> It is user's responsibility to destroy bit string when it is
* not needed anymore.
* \param src source byte array
* \param size size of byte array
* \return newly allocated bit string
*/
bit_string_t *convert(uint8_t *src, size_t size)
{
bit_string_t *result = bit_string_init(size);
for(int i = 0; i < size; ++i) {
for(int j = 7; j >= 0; --j) {
bit_string_append_bit(result, src[i] >> j & 1);
}
}
return result;
}
int main(int argc, char *argv[])
{
struct enc_arguments arguments;
/* Default values. */
arguments.debug = 0;
arguments.verbose = 0;
arguments.eac = 0;
arguments.block_size = DEFAULT_BLOCK_SIZE;
arguments.input_file = 0;
arguments.output_file = 0;
arguments.window_size = DEFAULT_WINDOW_SIZE;
arguments.threads = DEFAULT_THREADS;
/* Parse our arguments; every option seen by enc_parse_opt will
be reflected in arguments. */
argp_parse(&argp, argc, argv, 0, 0, &arguments);
log_verbose = arguments.verbose;
log_debug = arguments.debug;
PRINT_DEBUG("INPUT_FILE = %s\nOUTPUT_FILE = %s\nWINDOW_SIZE = %zu bits\nBLOCK_SIZE = %d bytes\nEAC = %s\nVERBOSE = %s\nDEBUG = %s\n",
arguments.input_file,
arguments.output_file,
arguments.window_size,
arguments.block_size,
arguments.eac ? "yes" : "no",
arguments.verbose ? "yes" : "no",
arguments.debug ? "yes" : "no");
FILE *file = fopen(arguments.input_file,"r");
if(file == NULL) {
error(1, errno, "Could not open input file");
}
FILE *outfile = fopen(arguments.output_file,"w");
if(outfile == NULL) {
error(1, errno, "Could not open output file");
}
uint8_t *buffer;
bit_string_t *buffer_string;
int buffer_size;
long file_size = 0, compressed_size = 0;
size_t *longest_match, longest_match_size, max_longest_match = 0;
bit_string_writer_t *writer = bit_string_writer_init(outfile);
if(arguments.eac) {
queue_t *queue = queue_init();
block_t *first = NULL;
block_t *block;
block_t *prev_block = NULL;
int num = 0;
buffer = malloc(sizeof(uint8_t) * arguments.block_size);
longest_match_size = 0;
while(!feof(file)) {
buffer_size = fread(buffer,sizeof(uint8_t),arguments.block_size,file);
if(buffer_size == 0) {
continue;
}
buffer_string = convert(buffer,buffer_size);
block = block_init(buffer_string,prev_block,num++);
if(first == NULL)
first = block;
for(int i = MIN_NW; i <= MAX_NW && i <= arguments.block_size * 8; i *= 2 ) {
queue_add_job(queue,block,i);
}
prev_block = block;
longest_match_size++;
}
longest_match = malloc(sizeof(size_t) * longest_match_size);
if(longest_match == NULL) {
printf("Failed allocating memory for longest_match\n");
return EXIT_FAILURE;
}
queue_run(queue,arguments.threads);
block = first;
while(block != NULL) {
longest_match[block->num_block] = block->longest_match;
if(max_longest_match < block->longest_match) {
max_longest_match = block->longest_match;
}
if(block->prev_block != NULL) {
uint8_t delta = nw_change_encode(block->prev_block->best_window_size,block->best_window_size);
bit_string_writer_write_byte(writer,delta,NW_BITS);
compressed_size += NW_BITS;
}
PRINT_VERBOSE("Block %d - best window size %zu compressed size %zu ratio %f\n", block->num_block, block->best_window_size, block->result->offset, (float)block->block->offset / block->result->offset);
bit_string_writer_write(writer,block->result);
file_size += block->block->offset;
compressed_size += block->result->offset;
block = block->next_block;
}
block = first;
while(block != NULL) {
block_t *tmp = block->next_block;
block_destroy(block);
block = tmp;
}
queue_destroy(queue);
} else {
bit_string_t *lz77_string;
longest_match_size = 1;
longest_match = malloc(sizeof(size_t));
if(longest_match == NULL) {
printf("Failed allocating memory for longest_match\n");
return EXIT_FAILURE;
}
fseek(file,0L,SEEK_END);
file_size = ftell(file);
fseek(file,0L,SEEK_SET);
buffer = malloc(sizeof(uint8_t) * file_size);
buffer_size = fread(buffer,sizeof(uint8_t),file_size,file);
buffer_string = convert(buffer,buffer_size);
lz77_string = lz77_encode(buffer_string,arguments.window_size,NULL,longest_match);
max_longest_match = *longest_match;
PRINT_VERBOSE("Compressed using window size %zu compressed size %zu ratio %f\n", arguments.window_size, lz77_string->offset, (float)buffer_string->offset / lz77_string->offset);
compressed_size += lz77_string->offset;
file_size = buffer_string->offset;
bit_string_writer_write(writer,lz77_string);
bit_string_destroy(lz77_string);
}
free(buffer);
if(compressed_size == 0) {
fclose(file);
fclose(outfile);
unlink(arguments.output_file);
printf("File too small to be compressed\n");
return EXIT_FAILURE;
}
long avg_longest_match = 0;
for(int i=0; i < longest_match_size; ++i) {
avg_longest_match += longest_match[i];
}
avg_longest_match /= longest_match_size;
long std_dev_longest_match = 0;
for(int i=0; i < longest_match_size; ++i) {
std_dev_longest_match += (avg_longest_match - longest_match[i]) * (avg_longest_match - longest_match[i]);
}
std_dev_longest_match /= longest_match_size;
std_dev_longest_match = sqrt(std_dev_longest_match);
compressed_size += bit_string_writer_flush(writer);
printf("%ld;%ld;%f;%zu;%ld;%ld;",file_size,compressed_size, (double)file_size / compressed_size, max_longest_match,avg_longest_match,std_dev_longest_match);
for(int i=0; i < longest_match_size; ++i) {
avg_longest_match += longest_match[i];
printf("%zu ",longest_match[i]);
}
bit_string_writer_destroy(writer);
fclose(file);
fclose(outfile);
return EXIT_SUCCESS;
}