minor bug fix
[flac.git] / src / flac / encode.c
1 /* flac - Command-line FLAC encoder/decoder
2  * Copyright (C) 2000,2001  Josh Coalson
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18
19 /*@@@ need to "_finish()" the verify decoder */
20
21 #include <assert.h>
22 #if defined _WIN32 && !defined __CYGWIN__
23 /* where MSVC puts unlink() */
24 # include <io.h>
25 #else
26 # include <unistd.h>
27 #endif
28 #include <stdio.h> /* for FILE et al. */
29 #include <stdlib.h> /* for malloc */
30 #include <string.h> /* for strcmp() */
31 #include "FLAC/all.h"
32 #include "encode.h"
33
34 #ifdef min
35 #undef min
36 #endif
37 #define min(x,y) ((x)<(y)?(x):(y))
38
39 #define CHUNK_OF_SAMPLES 2048
40
41 typedef enum {
42         FLAC__VERIFY_OK,
43         FLAC__VERIFY_FAILED_IN_FRAME,
44         FLAC__VERIFY_FAILED_IN_METADATA
45 } verify_code;
46
47 static const char *verify_code_string[] = {
48         "FLAC__VERIFY_OK",
49         "FLAC__VERIFY_FAILED_IN_FRAME",
50         "FLAC__VERIFY_FAILED_IN_METADATA"
51 };
52
53 typedef struct {
54         int32 *original[FLAC__MAX_CHANNELS];
55         unsigned size; /* of each original[] in samples */
56         unsigned tail; /* in wide samples */
57         const byte *encoded_signal;
58         unsigned encoded_signal_capacity;
59         unsigned encoded_bytes;
60         bool into_frames;
61         verify_code result;
62         FLAC__StreamDecoder *decoder;
63 } verify_fifo_struct;
64
65 typedef struct {
66         FILE *fout;
67         const char *outfile;
68         FLAC__Encoder *encoder;
69         bool verify;
70         bool verbose;
71         uint64 unencoded_size;
72         uint64 total_samples_to_encode;
73         uint64 bytes_written;
74         uint64 samples_written;
75         uint64 stream_offset; /* i.e. number of bytes before the first byte of the the first frame's header */
76         unsigned current_frame;
77         verify_fifo_struct verify_fifo;
78         FLAC__StreamMetaData_SeekTable seek_table;
79         unsigned first_seek_point_to_check;
80 } encoder_wrapper_struct;
81
82 static bool is_big_endian_host;
83
84 static unsigned char ucbuffer[CHUNK_OF_SAMPLES*FLAC__MAX_CHANNELS*((FLAC__MAX_BITS_PER_SAMPLE+7)/8)];
85 static signed char *scbuffer = (signed char *)ucbuffer;
86 static uint16 *usbuffer = (uint16 *)ucbuffer;
87 static int16 *ssbuffer = (int16 *)ucbuffer;
88
89 static int32 in[FLAC__MAX_CHANNELS][CHUNK_OF_SAMPLES];
90 static int32 *input[FLAC__MAX_CHANNELS];
91
92 /* local routines */
93 static bool init(encoder_wrapper_struct *encoder_wrapper);
94 static bool init_encoder(bool lax, bool do_mid_side, bool loose_mid_side, bool do_exhaustive_model_search, bool do_qlp_coeff_prec_search, unsigned rice_optimization_level, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned channels, unsigned bps, unsigned sample_rate, unsigned padding, char *requested_seek_points, int num_requested_seek_points, encoder_wrapper_struct *encoder_wrapper);
95 static bool convert_to_seek_table(char *requested_seek_points, int num_requested_seek_points, uint64 stream_samples, unsigned blocksize, FLAC__StreamMetaData_SeekTable *seek_table);
96 static void append_point_to_seek_table(FLAC__StreamMetaData_SeekTable *seek_table, uint64 sample, uint64 stream_samples, uint64 blocksize);
97 static int seekpoint_compare(const FLAC__StreamMetaData_SeekPoint *l, const FLAC__StreamMetaData_SeekPoint *r);
98 static void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, encoder_wrapper_struct *encoder_wrapper);
99 static FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
100 static void metadata_callback(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
101 static FLAC__StreamDecoderReadStatus verify_read_callback(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data);
102 static FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
103 static void verify_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
104 static void verify_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
105 static void print_stats(const encoder_wrapper_struct *encoder_wrapper);
106 static bool read_little_endian_uint16(FILE *f, uint16 *val, bool eof_ok);
107 static bool read_little_endian_uint32(FILE *f, uint32 *val, bool eof_ok);
108 static bool write_big_endian_uint16(FILE *f, uint16 val);
109 static bool write_big_endian_uint64(FILE *f, uint64 val);
110
111 int encode_wav(const char *infile, const char *outfile, bool verbose, uint64 skip, bool verify, bool lax, bool do_mid_side, bool loose_mid_side, bool do_exhaustive_model_search, bool do_qlp_coeff_prec_search, unsigned rice_optimization_level, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned padding, char *requested_seek_points, int num_requested_seek_points)
112 {
113         encoder_wrapper_struct encoder_wrapper;
114         FILE *fin;
115         bool is_unsigned_samples;
116         unsigned channels, bps, sample_rate, data_bytes;
117         size_t bytes_per_wide_sample, bytes_read;
118         uint16 x;
119         uint32 xx;
120
121         encoder_wrapper.encoder = 0;
122         encoder_wrapper.verify = verify;
123         encoder_wrapper.verbose = verbose;
124         encoder_wrapper.bytes_written = 0;
125         encoder_wrapper.samples_written = 0;
126         encoder_wrapper.outfile = outfile;
127         encoder_wrapper.seek_table.points = 0;
128         encoder_wrapper.first_seek_point_to_check = 0;
129
130         if(0 == strcmp(infile, "-")) {
131                 fin = stdin;
132         }
133         else {
134                 if(0 == (fin = fopen(infile, "rb"))) {
135                         fprintf(stderr, "ERROR: can't open input file %s\n", infile);
136                         return false;
137                 }
138         }
139         if(0 == strcmp(outfile, "-")) {
140                 encoder_wrapper.fout = stdout;
141         }
142         else {
143                 if(0 == (encoder_wrapper.fout = fopen(outfile, "wb"))) {
144                         fprintf(stderr, "ERROR: can't open output file %s\n", outfile);
145                         fclose(fin);
146                         return false;
147                 }
148         }
149
150         if(!init(&encoder_wrapper))
151                 goto wav_abort_;
152
153         /*
154          * check the RIFF chunk
155          */
156         if(!read_little_endian_uint32(fin, &xx, false))
157                 goto wav_abort_;
158         if(xx != 0x46464952) { /* "RIFF" */
159                 fprintf(stderr, "ERROR: no RIFF header\n");
160                 goto wav_abort_;
161         }
162         if(!read_little_endian_uint32(fin, &xx, false))
163                 goto wav_abort_;
164
165         /*
166          * now process the WAVE chunk
167          */
168         if(!read_little_endian_uint32(fin, &xx, true))
169                 goto wav_end_;
170         if(xx != 0x45564157) { /* "WAVE" */
171                 fprintf(stderr, "ERROR: no WAVE header\n");
172                 goto wav_abort_;
173         }
174
175         /* do the format sub-chunk */
176         if(!read_little_endian_uint32(fin, &xx, false))
177                 goto wav_abort_;
178         if(xx != 0x20746d66) { /* "fmt " */
179                 fprintf(stderr, "ERROR: no format sub-chunk\n");
180                 goto wav_abort_;
181         }
182         /* fmt chunk size */
183         if(!read_little_endian_uint32(fin, &xx, false))
184                 goto wav_abort_;
185         if(xx != 16) {
186                 fprintf(stderr, "ERROR: unsupported chunk\n");
187                 goto wav_abort_;
188         }
189         /* compression code */
190         if(!read_little_endian_uint16(fin, &x, false))
191                 goto wav_abort_;
192         if(x != 1) {
193                 fprintf(stderr, "ERROR: unsupported compression type %u\n", (unsigned)x);
194                 goto wav_abort_;
195         }
196         /* number of channels */
197         if(!read_little_endian_uint16(fin, &x, false))
198                 goto wav_abort_;
199         if(x == 0 || x > FLAC__MAX_CHANNELS) {
200                 fprintf(stderr, "ERROR: unsupported number channels %u\n", (unsigned)x);
201                 goto wav_abort_;
202         }
203         channels = x;
204         /* sample rate */
205         if(!read_little_endian_uint32(fin, &xx, false))
206                 goto wav_abort_;
207         if(xx == 0 || xx > FLAC__MAX_SAMPLE_RATE) {
208                 fprintf(stderr, "ERROR: unsupported sample rate %u\n", (unsigned)xx);
209                 goto wav_abort_;
210         }
211         sample_rate = xx;
212         /* avg bytes per second (ignored) */
213         if(!read_little_endian_uint32(fin, &xx, false))
214                 goto wav_abort_;
215         /* block align (ignored) */
216         if(!read_little_endian_uint16(fin, &x, false))
217                 goto wav_abort_;
218         /* bits per sample */
219         if(!read_little_endian_uint16(fin, &x, false))
220                 goto wav_abort_;
221         if(x != 8 && x != 16) {
222                 fprintf(stderr, "ERROR: unsupported bits per sample %u\n", (unsigned)x);
223                 goto wav_abort_;
224         }
225         bps = x;
226         is_unsigned_samples = (x == 8);
227
228         /* do the data sub-chunk */
229         if(!read_little_endian_uint32(fin, &xx, false))
230                 goto wav_abort_;
231         if(xx != 0x61746164) { /* "data" */
232                 fprintf(stderr, "ERROR: no data sub-chunk\n");
233                 goto wav_abort_;
234         }
235         /* data size */
236         if(!read_little_endian_uint32(fin, &xx, false))
237                 goto wav_abort_;
238         data_bytes = xx;
239
240         bytes_per_wide_sample = channels * (bps >> 3);
241
242         if(skip > 0) {
243                 if(fin != stdin) {
244                         if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_CUR)) {
245                                 fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
246                                 goto wav_abort_;
247                         }
248                 }
249                 else {
250                         int64 left;
251                         unsigned need;
252                         for(left = (int64)skip; left > 0; left -= CHUNK_OF_SAMPLES) {
253                                 need = min(left, CHUNK_OF_SAMPLES);
254                                 if(fread(ucbuffer, 1, bytes_per_wide_sample * need, fin) < need) {
255                                         fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
256                                         goto wav_abort_;
257                                 }
258                         }
259                 }
260         }
261
262         encoder_wrapper.total_samples_to_encode = data_bytes / bytes_per_wide_sample - skip;
263         encoder_wrapper.unencoded_size = encoder_wrapper.total_samples_to_encode * bytes_per_wide_sample + 44; /* 44 for the size of the WAV headers */
264
265         if(!init_encoder(lax, do_mid_side, loose_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, blocksize, qlp_coeff_precision, channels, bps, sample_rate, padding, requested_seek_points, num_requested_seek_points, &encoder_wrapper))
266                 goto wav_abort_;
267
268         encoder_wrapper.verify_fifo.into_frames = true;
269
270         while(data_bytes > 0) {
271                 bytes_read = fread(ucbuffer, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample, fin);
272                 if(bytes_read == 0) {
273                         if(ferror(fin)) {
274                                 fprintf(stderr, "ERROR reading from %s\n", infile);
275                                 goto wav_abort_;
276                         }
277                         else if(feof(fin))
278                                 break;
279                 }
280                 else {
281                         if(bytes_read > data_bytes)
282                                 bytes_read = data_bytes; /* chop off anything after the end of the data chunk */
283                         if(bytes_read % bytes_per_wide_sample != 0) {
284                                 fprintf(stderr, "ERROR, got partial sample from input file %s\n", infile);
285                                 goto wav_abort_;
286                         }
287                         else {
288                                 unsigned wide_samples = bytes_read / bytes_per_wide_sample;
289                                 format_input(wide_samples, false, is_unsigned_samples, channels, bps, &encoder_wrapper);
290                                 if(!FLAC__encoder_process(encoder_wrapper.encoder, input, wide_samples)) {
291                                         fprintf(stderr, "ERROR during encoding, state = %d:%s\n", encoder_wrapper.encoder->state, FLAC__EncoderStateString[encoder_wrapper.encoder->state]);
292                                         goto wav_abort_;
293                                 }
294                                 data_bytes -= bytes_read;
295                         }
296                 }
297         }
298
299 wav_end_:
300         if(encoder_wrapper.encoder) {
301                 if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
302                         FLAC__encoder_finish(encoder_wrapper.encoder);
303                 FLAC__encoder_free_instance(encoder_wrapper.encoder);
304         }
305         if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0) {
306                 print_stats(&encoder_wrapper);
307                 printf("\n");
308         }
309         if(0 != encoder_wrapper.seek_table.points)
310                 free(encoder_wrapper.seek_table.points);
311         if(verify) {
312                 if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
313                         printf("Verify FAILED! (%s)  Do not use %s\n", verify_code_string[encoder_wrapper.verify_fifo.result], outfile);
314                         return 1;
315                 }
316                 else {
317                         printf("Verify succeeded\n");
318                 }
319         }
320         fclose(fin);
321         return 0;
322 wav_abort_:
323         if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0)
324                 printf("\n");
325         if(encoder_wrapper.encoder) {
326                 if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
327                         FLAC__encoder_finish(encoder_wrapper.encoder);
328                 FLAC__encoder_free_instance(encoder_wrapper.encoder);
329         }
330         if(0 != encoder_wrapper.seek_table.points)
331                 free(encoder_wrapper.seek_table.points);
332         if(verify) {
333                 if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
334                         printf("Verify FAILED! (%s)  Do not use %s\n", verify_code_string[encoder_wrapper.verify_fifo.result], outfile);
335                         return 1;
336                 }
337                 else {
338                         printf("Verify succeeded\n");
339                 }
340         }
341         fclose(fin);
342         unlink(outfile);
343         return 1;
344 }
345
346 int encode_raw(const char *infile, const char *outfile, bool verbose, uint64 skip, bool verify, bool lax, bool do_mid_side, bool loose_mid_side, bool do_exhaustive_model_search, bool do_qlp_coeff_prec_search, unsigned rice_optimization_level, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned padding, char *requested_seek_points, int num_requested_seek_points, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, unsigned sample_rate)
347 {
348         encoder_wrapper_struct encoder_wrapper;
349         FILE *fin;
350         size_t bytes_read;
351         const size_t bytes_per_wide_sample = channels * (bps >> 3);
352
353         encoder_wrapper.encoder = 0;
354         encoder_wrapper.verify = verify;
355         encoder_wrapper.verbose = verbose;
356         encoder_wrapper.bytes_written = 0;
357         encoder_wrapper.samples_written = 0;
358         encoder_wrapper.outfile = outfile;
359         encoder_wrapper.seek_table.points = 0;
360         encoder_wrapper.first_seek_point_to_check = 0;
361
362         if(0 == strcmp(infile, "-")) {
363                 fin = stdin;
364         }
365         else {
366                 if(0 == (fin = fopen(infile, "rb"))) {
367                         fprintf(stderr, "ERROR: can't open input file %s\n", infile);
368                         return false;
369                 }
370         }
371         if(0 == strcmp(outfile, "-")) {
372                 encoder_wrapper.fout = stdout;
373         }
374         else {
375                 if(0 == (encoder_wrapper.fout = fopen(outfile, "wb"))) {
376                         fprintf(stderr, "ERROR: can't open output file %s\n", outfile);
377                         fclose(fin);
378                         return false;
379                 }
380         }
381
382         if(!init(&encoder_wrapper))
383                 goto raw_abort_;
384
385         /* get the file length */
386         if(0 != fseek(fin, 0, SEEK_END)) {
387                 encoder_wrapper.total_samples_to_encode = encoder_wrapper.unencoded_size = 0;
388         }
389         else {
390                 long filesize;
391                 fflush(fin);
392                 if(-1 == (filesize = ftell(fin))) {
393                         encoder_wrapper.total_samples_to_encode = encoder_wrapper.unencoded_size = 0;
394                 }
395                 else {
396                         encoder_wrapper.unencoded_size = filesize - skip * bytes_per_wide_sample;
397                         encoder_wrapper.total_samples_to_encode = filesize / bytes_per_wide_sample - skip;
398                 }
399         }
400
401         if(skip > 0) {
402                 if(fin != stdin) {
403                         if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_SET)) {
404                                 fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
405                                 goto raw_abort_;
406                         }
407                 }
408                 else {
409                         int64 left;
410                         unsigned need;
411                         for(left = (int64)skip; left > 0; left -= CHUNK_OF_SAMPLES) {
412                                 need = min(left, CHUNK_OF_SAMPLES);
413                                 if(fread(ucbuffer, 1, bytes_per_wide_sample * need, fin) < need) {
414                                         fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
415                                         goto raw_abort_;
416                                 }
417                         }
418                 }
419         }
420         else {
421                 fseek(fin, 0, SEEK_SET);
422         }
423
424         if(!init_encoder(lax, do_mid_side, loose_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, rice_optimization_level, max_lpc_order, blocksize, qlp_coeff_precision, channels, bps, sample_rate, padding, requested_seek_points, num_requested_seek_points, &encoder_wrapper))
425                 goto raw_abort_;
426
427         encoder_wrapper.verify_fifo.into_frames = true;
428
429         while(!feof(fin)) {
430                 bytes_read = fread(ucbuffer, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample, fin);
431                 if(bytes_read == 0) {
432                         if(ferror(fin)) {
433                                 fprintf(stderr, "ERROR reading from %s\n", infile);
434                                 goto raw_abort_;
435                         }
436                 }
437                 else if(bytes_read % bytes_per_wide_sample != 0) {
438                         fprintf(stderr, "ERROR, got partial sample from input file %s\n", infile);
439                         goto raw_abort_;
440                 }
441                 else {
442                         unsigned wide_samples = bytes_read / bytes_per_wide_sample;
443                         format_input(wide_samples, is_big_endian, is_unsigned_samples, channels, bps, &encoder_wrapper);
444                         if(!FLAC__encoder_process(encoder_wrapper.encoder, input, wide_samples)) {
445                                 fprintf(stderr, "ERROR during encoding, state = %d:%s\n", encoder_wrapper.encoder->state, FLAC__EncoderStateString[encoder_wrapper.encoder->state]);
446                                 goto raw_abort_;
447                         }
448                 }
449         }
450
451         if(encoder_wrapper.encoder) {
452                 if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
453                         FLAC__encoder_finish(encoder_wrapper.encoder);
454                 FLAC__encoder_free_instance(encoder_wrapper.encoder);
455         }
456         if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0) {
457                 print_stats(&encoder_wrapper);
458                 printf("\n");
459         }
460         if(0 != encoder_wrapper.seek_table.points)
461                 free(encoder_wrapper.seek_table.points);
462         if(verify) {
463                 if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
464                         printf("Verify FAILED! (%s)  Do not use %s\n", verify_code_string[encoder_wrapper.verify_fifo.result], outfile);
465                         return 1;
466                 }
467                 else {
468                         printf("Verify succeeded\n");
469                 }
470         }
471         fclose(fin);
472         return 0;
473 raw_abort_:
474         if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0)
475                 printf("\n");
476         if(encoder_wrapper.encoder) {
477                 if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
478                         FLAC__encoder_finish(encoder_wrapper.encoder);
479                 FLAC__encoder_free_instance(encoder_wrapper.encoder);
480         }
481         if(0 != encoder_wrapper.seek_table.points)
482                 free(encoder_wrapper.seek_table.points);
483         if(verify) {
484                 if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
485                         printf("Verify FAILED! (%s)  Do not use %s\n", verify_code_string[encoder_wrapper.verify_fifo.result], outfile);
486                         return 1;
487                 }
488                 else {
489                         printf("Verify succeeded\n");
490                 }
491         }
492         fclose(fin);
493         unlink(outfile);
494         return 1;
495 }
496
497 bool init(encoder_wrapper_struct *encoder_wrapper)
498 {
499         unsigned i;
500         uint32 test = 1;
501
502         is_big_endian_host = (*((byte*)(&test)))? false : true;
503
504         for(i = 0; i < FLAC__MAX_CHANNELS; i++)
505                 input[i] = &(in[i][0]);
506
507         encoder_wrapper->encoder = FLAC__encoder_get_new_instance();
508         if(0 == encoder_wrapper->encoder) {
509                 fprintf(stderr, "ERROR creating the encoder instance\n");
510                 return false;
511         }
512
513         return true;
514 }
515
516 bool init_encoder(bool lax, bool do_mid_side, bool loose_mid_side, bool do_exhaustive_model_search, bool do_qlp_coeff_prec_search, unsigned rice_optimization_level, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned channels, unsigned bps, unsigned sample_rate, unsigned padding, char *requested_seek_points, int num_requested_seek_points, encoder_wrapper_struct *encoder_wrapper)
517 {
518         unsigned i;
519
520         if(channels != 2)
521                 do_mid_side = loose_mid_side = false;
522
523         if(encoder_wrapper->verify) {
524                 /* set up the fifo which will hold the original signal to compare against */
525                 encoder_wrapper->verify_fifo.size = blocksize + CHUNK_OF_SAMPLES;
526                 for(i = 0; i < channels; i++) {
527                         if(0 == (encoder_wrapper->verify_fifo.original[i] = (int32*)malloc(sizeof(int32) * encoder_wrapper->verify_fifo.size))) {
528                                 fprintf(stderr, "ERROR allocating verify buffers\n");
529                                 return false;
530                         }
531                 }
532                 encoder_wrapper->verify_fifo.tail = 0;
533                 encoder_wrapper->verify_fifo.into_frames = false;
534                 encoder_wrapper->verify_fifo.result = FLAC__VERIFY_OK;
535
536                 /* set up a stream decoder for verification */
537                 encoder_wrapper->verify_fifo.decoder = FLAC__stream_decoder_get_new_instance();
538                 if(0 == encoder_wrapper->verify_fifo.decoder) {
539                         fprintf(stderr, "ERROR creating the verify decoder instance\n");
540                         return false;
541                 }
542                 if(FLAC__stream_decoder_init(encoder_wrapper->verify_fifo.decoder, verify_read_callback, verify_write_callback, verify_metadata_callback, verify_error_callback, encoder_wrapper) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA) {
543                         fprintf(stderr, "ERROR initializing decoder, state = %d:%s\n", encoder_wrapper->verify_fifo.decoder->state, FLAC__StreamDecoderStateString[encoder_wrapper->verify_fifo.decoder->state]);
544                         return false;
545                 }
546         }
547
548         if(!convert_to_seek_table(requested_seek_points, num_requested_seek_points, encoder_wrapper->total_samples_to_encode, blocksize, &encoder_wrapper->seek_table)) {
549                 fprintf(stderr, "ERROR allocating seek table\n");
550                 return false;
551         }
552
553         if(encoder_wrapper->verbose && encoder_wrapper->seek_table.num_points > 0) {
554                 if(encoder_wrapper->total_samples_to_encode > 0)
555                         printf("seek points (%llu total samples):", encoder_wrapper->total_samples_to_encode);
556                 else
557                         printf("seek points (total samples unknown):");
558                 for(i = 0; i < encoder_wrapper->seek_table.num_points; i++) {
559                         if(encoder_wrapper->seek_table.points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER)
560                                 printf(" %llu", encoder_wrapper->seek_table.points[i].sample_number);
561                         else
562                                 printf(" X");
563                 }
564                 printf("\n");
565         }
566
567         encoder_wrapper->encoder->streamable_subset = !lax;
568         encoder_wrapper->encoder->channels = channels;
569         encoder_wrapper->encoder->bits_per_sample = bps;
570         encoder_wrapper->encoder->sample_rate = sample_rate;
571         encoder_wrapper->encoder->blocksize = blocksize;
572         encoder_wrapper->encoder->qlp_coeff_precision = qlp_coeff_precision;
573         encoder_wrapper->encoder->max_lpc_order = max_lpc_order;
574         encoder_wrapper->encoder->do_mid_side_stereo = do_mid_side;
575         encoder_wrapper->encoder->loose_mid_side_stereo = loose_mid_side;
576         encoder_wrapper->encoder->do_exhaustive_model_search = do_exhaustive_model_search;
577         encoder_wrapper->encoder->do_qlp_coeff_prec_search = do_qlp_coeff_prec_search;
578         encoder_wrapper->encoder->rice_optimization_level = rice_optimization_level;
579         encoder_wrapper->encoder->total_samples_estimate = encoder_wrapper->total_samples_to_encode;
580         encoder_wrapper->encoder->seek_table = (encoder_wrapper->seek_table.num_points > 0)? &encoder_wrapper->seek_table : 0;
581         encoder_wrapper->encoder->padding = padding;
582
583         if(FLAC__encoder_init(encoder_wrapper->encoder, write_callback, metadata_callback, encoder_wrapper) != FLAC__ENCODER_OK) {
584                 fprintf(stderr, "ERROR initializing encoder, state = %d:%s\n", encoder_wrapper->encoder->state, FLAC__EncoderStateString[encoder_wrapper->encoder->state]);
585                 return false;
586         }
587
588         /* the above call write all the metadata, so we save the stream offset now */
589         encoder_wrapper->stream_offset = encoder_wrapper->bytes_written;
590
591         return true;
592 }
593
594 bool convert_to_seek_table(char *requested_seek_points, int num_requested_seek_points, uint64 stream_samples, unsigned blocksize, FLAC__StreamMetaData_SeekTable *seek_table)
595 {
596         unsigned i, j, real_points, placeholders;
597         char *pt = requested_seek_points, *q;
598         bool first;
599
600         seek_table->num_points = 0;
601
602         if(num_requested_seek_points == 0)
603                 return true;
604
605         if(num_requested_seek_points < 0) {
606                 strcpy(requested_seek_points, ",100x");
607                 num_requested_seek_points = 100;
608         }
609
610         /* first count how many individual seek point we may need */
611         real_points = placeholders = 0;
612         for(i = 0; i < (unsigned)num_requested_seek_points; i++) {
613                 q = strchr(pt, '<');
614                 assert(0 != q);
615                 *q = '\0';
616
617                 if(0 == strcmp(pt, "X")) { /* -S X */
618                         placeholders++;
619                 }
620                 else if(pt[strlen(pt)-1] == 'x') { /* -S #x */
621                         if(stream_samples > 0) /* we can only do these if we know the number of samples to encode up front */
622                                 real_points += (unsigned)atoi(pt);
623                 }
624                 else { /* -S # */
625                         real_points++;
626                 }
627                 *q++ = '<';
628
629                 pt = q;
630         }
631         pt = requested_seek_points;
632
633         /* make some space */
634         if(0 == (seek_table->points = (FLAC__StreamMetaData_SeekPoint*)malloc(sizeof(FLAC__StreamMetaData_SeekPoint) * (real_points+placeholders))))
635                 return false;
636
637         /* initialize the seek_table.  we set frame_samples to zero to signify the points have not yet been hit by a frame write yet. */
638         for(i = 0; i < real_points+placeholders; i++) {
639                 seek_table->points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
640                 seek_table->points[i].stream_offset = 0;
641                 seek_table->points[i].frame_samples = 0;
642         }
643
644         for(i = 0; i < (unsigned)num_requested_seek_points; i++) {
645                 q = strchr(pt, '<');
646                 assert(0 != q);
647                 *q++ = '\0';
648
649                 if(0 == strcmp(pt, "X")) { /* -S X */
650                         placeholders++;
651                 }
652                 else if(pt[strlen(pt)-1] == 'x') { /* -S #x */
653                         if(stream_samples > 0) { /* we can only do these if we know the number of samples to encode up front */
654                                 unsigned j, n;
655                                 n = (unsigned)atoi(pt);
656                                 for(j = 0; j < n; j++)
657                                         append_point_to_seek_table(seek_table, stream_samples * (uint64)j / (uint64)n, stream_samples, blocksize);
658                         }
659                 }
660                 else { /* -S # */
661                         append_point_to_seek_table(seek_table, (uint64)atoi(pt), stream_samples, blocksize);
662                 }
663
664                 pt = q;
665         }
666
667         /* sort the seekpoints */
668         qsort(seek_table->points, seek_table->num_points, sizeof(FLAC__StreamMetaData_SeekPoint), (int (*)(const void *, const void *))seekpoint_compare);
669
670         /* uniqify the seekpoints */
671         first = false;
672         for(i = j = 0; i < seek_table->num_points; i++) {
673                 if(!first) {
674                         if(seek_table->points[i].sample_number == seek_table->points[j-1].sample_number)
675                                 continue;
676                 }
677                 first = false;
678                 seek_table->points[j++] = seek_table->points[i];
679         }
680         seek_table->num_points = j;
681
682         /* append placeholders */
683         for(i = 0, j = seek_table->num_points; i < placeholders; i++, j++)
684                 seek_table->points[j].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
685         seek_table->num_points += placeholders;
686
687         return true;
688 }
689
690 void append_point_to_seek_table(FLAC__StreamMetaData_SeekTable *seek_table, uint64 sample, uint64 stream_samples, uint64 blocksize)
691 {
692         const uint64 target_sample = (sample / blocksize) * blocksize;
693
694         if(stream_samples == 0 || target_sample < stream_samples)
695                 seek_table->points[seek_table->num_points++].sample_number = target_sample;
696 }
697
698 int seekpoint_compare(const FLAC__StreamMetaData_SeekPoint *l, const FLAC__StreamMetaData_SeekPoint *r)
699 {
700         /* we don't just 'return l->sample_number - r->sample_number' since the result (int64) might overflow an 'int' */
701         if(l->sample_number == r->sample_number)
702                 return 0;
703         else if(l->sample_number < r->sample_number)
704                 return -1;
705         else
706                 return 1;
707 }
708
709 void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, encoder_wrapper_struct *encoder_wrapper)
710 {
711         unsigned wide_sample, sample, channel, byte;
712
713         if(bps == 8) {
714                 if(is_unsigned_samples) {
715                         for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
716                                 for(channel = 0; channel < channels; channel++, sample++)
717                                         input[channel][wide_sample] = (int32)ucbuffer[sample] - 0x80;
718                 }
719                 else {
720                         for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
721                                 for(channel = 0; channel < channels; channel++, sample++)
722                                         input[channel][wide_sample] = (int32)scbuffer[sample];
723                 }
724         }
725         else if(bps == 16) {
726                 if(is_big_endian != is_big_endian_host) {
727                         unsigned char tmp;
728                         const unsigned bytes = wide_samples * channels * (bps >> 3);
729                         for(byte = 0; byte < bytes; byte += 2) {
730                                 tmp = ucbuffer[byte];
731                                 ucbuffer[byte] = ucbuffer[byte+1];
732                                 ucbuffer[byte+1] = tmp;
733                         }
734                 }
735                 if(is_unsigned_samples) {
736                         for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
737                                 for(channel = 0; channel < channels; channel++, sample++)
738                                         input[channel][wide_sample] = (int32)usbuffer[sample] - 0x8000;
739                 }
740                 else {
741                         for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
742                                 for(channel = 0; channel < channels; channel++, sample++)
743                                         input[channel][wide_sample] = (int32)ssbuffer[sample];
744                 }
745         }
746         else if(bps == 24) {
747                 if(!is_big_endian) {
748                         unsigned char tmp;
749                         const unsigned bytes = wide_samples * channels * (bps >> 3);
750                         for(byte = 0; byte < bytes; byte += 3) {
751                                 tmp = ucbuffer[byte];
752                                 ucbuffer[byte] = ucbuffer[byte+2];
753                                 ucbuffer[byte+2] = tmp;
754                         }
755                 }
756                 if(is_unsigned_samples) {
757                         for(byte = sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
758                                 for(channel = 0; channel < channels; channel++, sample++) {
759                                         input[channel][wide_sample]  = ucbuffer[byte++]; input[channel][wide_sample] <<= 8;
760                                         input[channel][wide_sample] |= ucbuffer[byte++]; input[channel][wide_sample] <<= 8;
761                                         input[channel][wide_sample] |= ucbuffer[byte++];
762                                         input[channel][wide_sample] -= 0x800000;
763                                 }
764                 }
765                 else {
766                         for(byte = sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
767                                 for(channel = 0; channel < channels; channel++, sample++) {
768                                         input[channel][wide_sample]  = scbuffer[byte++]; input[channel][wide_sample] <<= 8;
769                                         input[channel][wide_sample] |= ucbuffer[byte++]; input[channel][wide_sample] <<= 8;
770                                         input[channel][wide_sample] |= ucbuffer[byte++];
771                                 }
772                 }
773         }
774         else {
775                 assert(0);
776         }
777
778         if(encoder_wrapper->verify) {
779                 for(channel = 0; channel < channels; channel++)
780                         memcpy(&encoder_wrapper->verify_fifo.original[channel][encoder_wrapper->verify_fifo.tail], &input[channel][0], sizeof(int32) * wide_samples);
781                 encoder_wrapper->verify_fifo.tail += wide_samples;
782                 assert(encoder_wrapper->verify_fifo.tail <= encoder_wrapper->verify_fifo.size);
783         }
784 }
785
786 FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
787 {
788         encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
789         unsigned mask = (encoder->do_exhaustive_model_search || encoder->do_qlp_coeff_prec_search)? 0x07 : 0x1f;
790
791         /* mark the current seek point if hit */
792         if(encoder_wrapper->seek_table.num_points > 0) {
793                 uint64 current_sample = (uint64)current_frame * (uint64)encoder->blocksize, test_sample;
794                 unsigned i;
795                 for(i = encoder_wrapper->first_seek_point_to_check; i < encoder_wrapper->seek_table.num_points; i++) {
796                         test_sample = encoder_wrapper->seek_table.points[i].sample_number;
797                         if(test_sample > current_sample) {
798                                 break;
799                         }
800                         else if(test_sample == current_sample) {
801                                 encoder_wrapper->seek_table.points[i].stream_offset = encoder_wrapper->bytes_written - encoder_wrapper->stream_offset;
802                                 encoder_wrapper->seek_table.points[i].frame_samples = encoder->blocksize;
803                                 encoder_wrapper->first_seek_point_to_check++;
804 fprintf(stderr,"\nHIT seek point %llu at frame %u, stream_offset = %llu, first_frame_offset = %llu\n",encoder_wrapper->seek_table.points[i].sample_number,current_frame,encoder_wrapper->seek_table.points[i].stream_offset,encoder_wrapper->stream_offset);
805                                 break;
806                         }
807                         else {
808                                 encoder_wrapper->first_seek_point_to_check++;
809                         }
810                 }
811         }
812
813         encoder_wrapper->bytes_written += bytes;
814         encoder_wrapper->samples_written += samples;
815         encoder_wrapper->current_frame = current_frame;
816
817         if(samples && encoder_wrapper->verbose && encoder_wrapper->total_samples_to_encode > 0 && !(current_frame & mask))
818                 print_stats(encoder_wrapper);
819
820         if(encoder_wrapper->verify) {
821                 encoder_wrapper->verify_fifo.encoded_signal = buffer;
822                 encoder_wrapper->verify_fifo.encoded_bytes = bytes;
823                 if(encoder_wrapper->verify_fifo.into_frames) {
824                         if(!FLAC__stream_decoder_process_one_frame(encoder_wrapper->verify_fifo.decoder)) {
825                                 encoder_wrapper->verify_fifo.result = FLAC__VERIFY_FAILED_IN_FRAME;
826                                 return FLAC__ENCODER_WRITE_FATAL_ERROR;
827                         }
828                 }
829                 else {
830                         if(!FLAC__stream_decoder_process_metadata(encoder_wrapper->verify_fifo.decoder)) {
831                                 encoder_wrapper->verify_fifo.result = FLAC__VERIFY_FAILED_IN_METADATA;
832                                 return FLAC__ENCODER_WRITE_FATAL_ERROR;
833                         }
834                 }
835         }
836
837         if(fwrite(buffer, sizeof(byte), bytes, encoder_wrapper->fout) == bytes)
838                 return FLAC__ENCODER_WRITE_OK;
839         else
840                 return FLAC__ENCODER_WRITE_FATAL_ERROR;
841 }
842
843 void metadata_callback(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data)
844 {
845         encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
846         byte b;
847         FILE *f;
848         const uint64 samples = metadata->data.stream_info.total_samples;
849         const unsigned min_framesize = metadata->data.stream_info.min_framesize;
850         const unsigned max_framesize = metadata->data.stream_info.max_framesize;
851
852         /*
853          * we get called by the encoder when the encoding process has
854          * finished so that we can update the STREAMINFO and SEEKTABLE
855          * blocks.
856          */
857
858         (void)encoder; /* silence compiler warning about unused parameter */
859
860         if(encoder_wrapper->fout == stdout)
861                 return;
862
863         fclose(encoder_wrapper->fout);
864         if(0 == (f = fopen(encoder_wrapper->outfile, "r+b")))
865                 return;
866
867         /* all this is based on intimate knowledge of the stream header
868          * layout, but a change to the header format that would break this
869          * would also break all streams encoded in the previous format.
870          */
871
872         if(-1 == fseek(f, 26, SEEK_SET)) goto samples_;
873         fwrite(metadata->data.stream_info.md5sum, 1, 16, f);
874
875 samples_:
876         if(-1 == fseek(f, 21, SEEK_SET)) goto framesize_;
877         if(fread(&b, 1, 1, f) != 1) goto framesize_;
878         if(-1 == fseek(f, 21, SEEK_SET)) goto framesize_;
879         b = (b & 0xf0) | (byte)((samples >> 32) & 0x0F);
880         if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
881         b = (byte)((samples >> 24) & 0xFF);
882         if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
883         b = (byte)((samples >> 16) & 0xFF);
884         if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
885         b = (byte)((samples >> 8) & 0xFF);
886         if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
887         b = (byte)(samples & 0xFF);
888         if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
889
890 framesize_:
891         if(-1 == fseek(f, 12, SEEK_SET)) goto seektable_;
892         b = (byte)((min_framesize >> 16) & 0xFF);
893         if(fwrite(&b, 1, 1, f) != 1) goto seektable_;
894         b = (byte)((min_framesize >> 8) & 0xFF);
895         if(fwrite(&b, 1, 1, f) != 1) goto seektable_;
896         b = (byte)(min_framesize & 0xFF);
897         if(fwrite(&b, 1, 1, f) != 1) goto seektable_;
898         b = (byte)((max_framesize >> 16) & 0xFF);
899         if(fwrite(&b, 1, 1, f) != 1) goto seektable_;
900         b = (byte)((max_framesize >> 8) & 0xFF);
901         if(fwrite(&b, 1, 1, f) != 1) goto seektable_;
902         b = (byte)(max_framesize & 0xFF);
903         if(fwrite(&b, 1, 1, f) != 1) goto seektable_;
904
905 seektable_:
906         if(encoder_wrapper->seek_table.num_points > 0) {
907                 long pos;
908                 unsigned i;
909
910                 /* convert any unused seek points to placeholders */
911                 for(i = 0; i < encoder_wrapper->seek_table.num_points; i++) {
912                         if(encoder_wrapper->seek_table.points[i].sample_number == FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER)
913                                 break;
914                         else if(encoder_wrapper->seek_table.points[i].frame_samples == 0)
915                                 encoder_wrapper->seek_table.points[i].sample_number = FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER;
916                 }
917
918                 pos = (FLAC__STREAM_SYNC_LEN + FLAC__STREAM_METADATA_IS_LAST_LEN + FLAC__STREAM_METADATA_TYPE_LEN + FLAC__STREAM_METADATA_LENGTH_LEN) / 8;
919                 pos += metadata->length;
920                 pos = (FLAC__STREAM_METADATA_IS_LAST_LEN + FLAC__STREAM_METADATA_TYPE_LEN + FLAC__STREAM_METADATA_LENGTH_LEN) / 8;
921                 if(-1 == fseek(f, pos, SEEK_SET)) goto end_;
922                 for(i = 0; i < encoder_wrapper->seek_table.num_points; i++) {
923                         if(!write_big_endian_uint64(f, encoder_wrapper->seek_table.points[i].sample_number)) goto end_;
924                         if(!write_big_endian_uint64(f, encoder_wrapper->seek_table.points[i].stream_offset)) goto end_;
925                         if(!write_big_endian_uint16(f, encoder_wrapper->seek_table.points[i].frame_samples)) goto end_;
926                 }
927         }
928
929 end_:
930         fclose(f);
931         return;
932 }
933
934 FLAC__StreamDecoderReadStatus verify_read_callback(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data)
935 {
936         encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
937         const unsigned encoded_bytes = encoder_wrapper->verify_fifo.encoded_bytes;
938         (void)decoder;
939
940         if(encoded_bytes <= *bytes) {
941                 *bytes = encoded_bytes;
942                 memcpy(buffer, encoder_wrapper->verify_fifo.encoded_signal, *bytes);
943         }
944         else {
945                 memcpy(buffer, encoder_wrapper->verify_fifo.encoded_signal, *bytes);
946                 encoder_wrapper->verify_fifo.encoded_signal += *bytes;
947                 encoder_wrapper->verify_fifo.encoded_bytes -= *bytes;
948         }
949
950         return FLAC__STREAM_DECODER_READ_CONTINUE;
951 }
952
953 FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data)
954 {
955         encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
956         unsigned channel, l, r;
957
958         for(channel = 0; channel < decoder->channels; channel++) {
959                 if(0 != memcmp(buffer[channel], encoder_wrapper->verify_fifo.original[channel], sizeof(int32) * decoder->blocksize)) {
960                         fprintf(stderr, "\nERROR: mismatch in decoded data, verify FAILED!\n");
961                         fprintf(stderr, "       Please submit a bug report to http://sourceforge.net/bugs/?func=addbug&group_id=13478\n");
962                         return FLAC__STREAM_DECODER_WRITE_ABORT;
963                 }
964         }
965         /* dequeue the frame from the fifo */
966         for(channel = 0; channel < decoder->channels; channel++) {
967                 for(l = 0, r = frame->header.blocksize; r < encoder_wrapper->verify_fifo.tail; l++, r++) {
968                         encoder_wrapper->verify_fifo.original[channel][l] = encoder_wrapper->verify_fifo.original[channel][r];
969                 }
970         }
971         encoder_wrapper->verify_fifo.tail -= frame->header.blocksize;
972         return FLAC__STREAM_DECODER_WRITE_CONTINUE;
973 }
974
975 void verify_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
976 {
977         (void)decoder;
978         (void)metadata;
979         (void)client_data;
980 }
981
982 void verify_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
983 {
984         (void)decoder;
985         (void)client_data;
986         fprintf(stderr, "\nERROR: verification decoder returned error %d:%s\n", status, FLAC__StreamDecoderErrorStatusString[status]);
987 }
988
989 void print_stats(const encoder_wrapper_struct *encoder_wrapper)
990 {
991 #ifdef _MSC_VER
992         /* with VC++ you have to spoon feed it the casting */
993         double progress = (double)(int64)encoder_wrapper->samples_written / (double)(int64)encoder_wrapper->total_samples_to_encode;
994 #else
995         double progress = (double)encoder_wrapper->samples_written / (double)encoder_wrapper->total_samples_to_encode;
996 #endif
997         printf("\r%0.2f%% complete: frame %u, wrote %u bytes, %u of %u samples, ratio = %5.3f",
998                 progress * 100.0, encoder_wrapper->current_frame,
999                 (unsigned)encoder_wrapper->bytes_written, (unsigned)encoder_wrapper->samples_written, (unsigned)encoder_wrapper->total_samples_to_encode,
1000 #ifdef _MSC_VER
1001                 /* with VC++ you have to spoon feed it the casting */
1002                 (double)(int64)encoder_wrapper->bytes_written / ((double)(int64)encoder_wrapper->unencoded_size * progress)
1003 #else
1004                 (double)encoder_wrapper->bytes_written / ((double)encoder_wrapper->unencoded_size * progress)
1005 #endif
1006         );
1007         fflush(stdout);
1008 }
1009
1010 bool read_little_endian_uint16(FILE *f, uint16 *val, bool eof_ok)
1011 {
1012         size_t bytes_read = fread(val, 1, 2, f);
1013
1014         if(bytes_read == 0) {
1015                 if(!eof_ok) {
1016                         fprintf(stderr, "ERROR: unexpected EOF\n");
1017                         return false;
1018                 }
1019                 else
1020                         return true;
1021         }
1022         else if(bytes_read < 2) {
1023                 fprintf(stderr, "ERROR: unexpected EOF\n");
1024                 return false;
1025         }
1026         else {
1027                 if(is_big_endian_host) {
1028                         byte tmp, *b = (byte*)val;
1029                         tmp = b[1]; b[1] = b[0]; b[0] = tmp;
1030                 }
1031                 return true;
1032         }
1033 }
1034
1035 bool read_little_endian_uint32(FILE *f, uint32 *val, bool eof_ok)
1036 {
1037         size_t bytes_read = fread(val, 1, 4, f);
1038
1039         if(bytes_read == 0) {
1040                 if(!eof_ok) {
1041                         fprintf(stderr, "ERROR: unexpected EOF\n");
1042                         return false;
1043                 }
1044                 else
1045                         return true;
1046         }
1047         else if(bytes_read < 4) {
1048                 fprintf(stderr, "ERROR: unexpected EOF\n");
1049                 return false;
1050         }
1051         else {
1052                 if(is_big_endian_host) {
1053                         byte tmp, *b = (byte*)val;
1054                         tmp = b[3]; b[3] = b[0]; b[0] = tmp;
1055                         tmp = b[2]; b[2] = b[1]; b[1] = tmp;
1056                 }
1057                 return true;
1058         }
1059 }
1060
1061 bool write_big_endian_uint16(FILE *f, uint16 val)
1062 {
1063         if(!is_big_endian_host) {
1064                 byte *b = (byte *)&val, tmp;
1065                 tmp = b[0]; b[0] = b[1]; b[1] = tmp;
1066         }
1067         return fwrite(&val, 1, 2, f) == 2;
1068 }
1069
1070 bool write_big_endian_uint64(FILE *f, uint64 val)
1071 {
1072         if(!is_big_endian_host) {
1073                 byte *b = (byte *)&val, tmp;
1074                 tmp = b[0]; b[0] = b[7]; b[7] = tmp;
1075                 tmp = b[1]; b[1] = b[6]; b[6] = tmp;
1076                 tmp = b[2]; b[2] = b[5]; b[3] = tmp;
1077                 tmp = b[3]; b[3] = b[4]; b[4] = tmp;
1078         }
1079         return fwrite(&val, 1, 8, f) == 8;
1080 }