Initial revision
[platform/upstream/flac.git] / src / flac / encode.c
1 /* flac - Command-line FLAC encoder/decoder
2  * Copyright (C) 2000  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 #include <assert.h>
20 #if defined _WIN32 && !defined __CYGWIN__
21 /* where MSVC puts unlink() */
22 # include <io.h>
23 #else
24 # include <unistd.h>
25 #endif
26 #include <stdio.h> /* for FILE */
27 #include <string.h> /* for strcmp() */
28 #include "FLAC/all.h"
29 #include "encode.h"
30
31 #define CHUNK_OF_SAMPLES 2048
32
33 typedef struct {
34         FILE *fout;
35         const char *outfile;
36         FLAC__Encoder *encoder;
37         bool verbose;
38         uint64 unencoded_size;
39         uint64 total_samples_to_encode;
40         uint64 bytes_written;
41         uint64 samples_written;
42         unsigned current_frame;
43 } encoder_wrapper_struct;
44
45 static bool is_big_endian_host;
46
47 static unsigned char ucbuffer[CHUNK_OF_SAMPLES*FLAC__MAX_CHANNELS*(FLAC__MAX_BITS_PER_SAMPLE>>3)];
48 static signed char *scbuffer = (signed char *)ucbuffer;
49 static uint16 *usbuffer = (uint16 *)ucbuffer;
50 static int16 *ssbuffer = (int16 *)ucbuffer;
51
52 static int32 in[FLAC__MAX_CHANNELS][CHUNK_OF_SAMPLES];
53 static int32 *input[FLAC__MAX_CHANNELS];
54
55 /* local routines */
56 static bool init(encoder_wrapper_struct *encoder_wrapper);
57 static bool init_encoder(bool lax, bool do_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, encoder_wrapper_struct *encoder_wrapper);
58 static void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps);
59 static FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
60 static void metadata_callback(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
61 static void print_stats(const encoder_wrapper_struct *encoder_wrapper);
62 static bool read_little_endian_uint16(FILE *f, uint16 *val, bool eof_ok);
63 static bool read_little_endian_uint32(FILE *f, uint32 *val, bool eof_ok);
64
65 int encode_wav(const char *infile, const char *outfile, bool verbose, uint64 skip, bool lax, bool do_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)
66 {
67         encoder_wrapper_struct encoder_wrapper;
68         FILE *fin;
69         bool is_unsigned_samples;
70         unsigned channels, bps, sample_rate, data_bytes;
71         size_t bytes_per_wide_sample, bytes_read;
72         uint16 x;
73         uint32 xx;
74
75         encoder_wrapper.encoder = 0;
76         encoder_wrapper.verbose = verbose;
77         encoder_wrapper.bytes_written = 0;
78         encoder_wrapper.samples_written = 0;
79         encoder_wrapper.outfile = outfile;
80
81         if(0 == strcmp(infile, "-")) {
82                 fin = stdin;
83         }
84         else {
85                 if(0 == (fin = fopen(infile, "rb"))) {
86                         fprintf(stderr, "ERROR: can't open input file %s\n", infile);
87                         return false;
88                 }
89         }
90         if(0 == strcmp(outfile, "-")) {
91                 encoder_wrapper.fout = stdout;
92         }
93         else {
94                 if(0 == (encoder_wrapper.fout = fopen(outfile, "wb"))) {
95                         fprintf(stderr, "ERROR: can't open output file %s\n", outfile);
96                         fclose(fin);
97                         return false;
98                 }
99         }
100
101         if(!init(&encoder_wrapper))
102                 goto wav_abort_;
103
104         /*
105          * check the RIFF chunk
106          */
107         if(!read_little_endian_uint32(fin, &xx, false))
108                 goto wav_abort_;
109         if(xx != 0x46464952) { /* "RIFF" */
110                 fprintf(stderr, "ERROR: no RIFF header\n");
111                 goto wav_abort_;
112         }
113         if(!read_little_endian_uint32(fin, &xx, false))
114                 goto wav_abort_;
115
116         /*
117          * now process the WAVE chunk
118          */
119         if(!read_little_endian_uint32(fin, &xx, true))
120                 goto wav_end_;
121         if(xx != 0x45564157) { /* "WAVE" */
122                 fprintf(stderr, "ERROR: no WAVE header\n");
123                 goto wav_abort_;
124         }
125
126         /* do the format sub-chunk */
127         if(!read_little_endian_uint32(fin, &xx, false))
128                 goto wav_abort_;
129         if(xx != 0x20746d66) { /* "fmt " */
130                 fprintf(stderr, "ERROR: no format sub-chunk\n");
131                 goto wav_abort_;
132         }
133         /* fmt chunk size */
134         if(!read_little_endian_uint32(fin, &xx, false))
135                 goto wav_abort_;
136         if(xx != 16) {
137                 fprintf(stderr, "ERROR: unsupported chunk\n");
138                 goto wav_abort_;
139         }
140         /* compression code */
141         if(!read_little_endian_uint16(fin, &x, false))
142                 goto wav_abort_;
143         if(x != 1) {
144                 fprintf(stderr, "ERROR: unsupported compression type %u\n", (unsigned)x);
145                 goto wav_abort_;
146         }
147         /* number of channels */
148         if(!read_little_endian_uint16(fin, &x, false))
149                 goto wav_abort_;
150         if(x == 0 || x > FLAC__MAX_CHANNELS) {
151                 fprintf(stderr, "ERROR: unsupported number channels %u\n", (unsigned)x);
152                 goto wav_abort_;
153         }
154         channels = x;
155         /* sample rate */
156         if(!read_little_endian_uint32(fin, &xx, false))
157                 goto wav_abort_;
158         if(xx == 0 || xx > FLAC__MAX_SAMPLE_RATE) {
159                 fprintf(stderr, "ERROR: unsupported sample rate %u\n", (unsigned)xx);
160                 goto wav_abort_;
161         }
162         sample_rate = xx;
163         /* avg bytes per second (ignored) */
164         if(!read_little_endian_uint32(fin, &xx, false))
165                 goto wav_abort_;
166         /* block align (ignored) */
167         if(!read_little_endian_uint16(fin, &x, false))
168                 goto wav_abort_;
169         /* bits per sample */
170         if(!read_little_endian_uint16(fin, &x, false))
171                 goto wav_abort_;
172         if(x != 8 && x != 16) {
173                 fprintf(stderr, "ERROR: unsupported bits per sample %u\n", (unsigned)x);
174                 goto wav_abort_;
175         }
176         bps = x;
177         is_unsigned_samples = (x == 8);
178
179         /* do the data sub-chunk */
180         if(!read_little_endian_uint32(fin, &xx, false))
181                 goto wav_abort_;
182         if(xx != 0x61746164) { /* "data" */
183                 fprintf(stderr, "ERROR: no data sub-chunk\n");
184                 goto wav_abort_;
185         }
186         /* data size */
187         if(!read_little_endian_uint32(fin, &xx, false))
188                 goto wav_abort_;
189         data_bytes = xx;
190
191         if(!init_encoder(lax, do_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, &encoder_wrapper))
192                 goto wav_abort_;
193
194         bytes_per_wide_sample = channels * (bps >> 3);
195
196         if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_CUR)) {
197                 fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
198                 goto wav_abort_;
199         }
200
201         encoder_wrapper.total_samples_to_encode = data_bytes / bytes_per_wide_sample - skip;
202         encoder_wrapper.unencoded_size = encoder_wrapper.total_samples_to_encode * bytes_per_wide_sample + 44; /* 44 for the size of the WAV headers */
203
204         while(data_bytes > 0) {
205                 bytes_read = fread(ucbuffer, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample, fin);
206                 if(bytes_read == 0) {
207                         if(ferror(fin)) {
208                                 fprintf(stderr, "ERROR reading from %s\n", infile);
209                                 goto wav_abort_;
210                         }
211                         else if(feof(fin))
212                                 break;
213                 }
214                 else if(bytes_read % bytes_per_wide_sample != 0) {
215                         fprintf(stderr, "ERROR, got partial sample from input file %s\n", infile);
216                         goto wav_abort_;
217                 }
218                 else {
219                         unsigned wide_samples = bytes_read / bytes_per_wide_sample;
220                         format_input(wide_samples, false, is_unsigned_samples, channels, bps);
221                         if(!FLAC__encoder_process(encoder_wrapper.encoder, input, wide_samples)) {
222                                 fprintf(stderr, "ERROR during encoding, state = %d\n", encoder_wrapper.encoder->state);
223                                 goto wav_abort_;
224                         }
225                         data_bytes -= bytes_read;
226                 }
227         }
228
229 wav_end_:
230         if(encoder_wrapper.encoder) {
231                 if(encoder_wrapper.encoder->state != FLAC__ENCODER_UNINITIALIZED)
232                         FLAC__encoder_finish(encoder_wrapper.encoder);
233                 FLAC__encoder_free_instance(encoder_wrapper.encoder);
234         }
235         if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0) {
236                 print_stats(&encoder_wrapper);
237                 printf("\n");
238         }
239         fclose(fin);
240         return 0;
241 wav_abort_:
242         if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0)
243                 printf("\n");
244         if(encoder_wrapper.encoder) {
245                 if(encoder_wrapper.encoder->state != FLAC__ENCODER_UNINITIALIZED)
246                         FLAC__encoder_finish(encoder_wrapper.encoder);
247                 FLAC__encoder_free_instance(encoder_wrapper.encoder);
248         }
249         fclose(fin);
250         unlink(outfile);
251         return 1;
252 }
253
254 int encode_raw(const char *infile, const char *outfile, bool verbose, uint64 skip, bool lax, bool do_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, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, unsigned sample_rate)
255 {
256         encoder_wrapper_struct encoder_wrapper;
257         FILE *fin;
258         size_t bytes_read;
259         const size_t bytes_per_wide_sample = channels * (bps >> 3);
260
261         encoder_wrapper.encoder = 0;
262         encoder_wrapper.verbose = verbose;
263         encoder_wrapper.bytes_written = 0;
264         encoder_wrapper.samples_written = 0;
265         encoder_wrapper.outfile = outfile;
266
267         if(0 == strcmp(infile, "-")) {
268                 fin = stdin;
269         }
270         else {
271                 if(0 == (fin = fopen(infile, "rb"))) {
272                         fprintf(stderr, "ERROR: can't open input file %s\n", infile);
273                         return false;
274                 }
275         }
276         if(0 == strcmp(outfile, "-")) {
277                 encoder_wrapper.fout = stdout;
278         }
279         else {
280                 if(0 == (encoder_wrapper.fout = fopen(outfile, "wb"))) {
281                         fprintf(stderr, "ERROR: can't open output file %s\n", outfile);
282                         fclose(fin);
283                         return false;
284                 }
285         }
286
287         if(!init(&encoder_wrapper))
288                 goto raw_abort_;
289
290         if(!init_encoder(lax, do_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, &encoder_wrapper))
291                 goto raw_abort_;
292
293         /* get the file length */
294         if(0 != fseek(fin, 0, SEEK_END)) {
295                 encoder_wrapper.total_samples_to_encode = encoder_wrapper.unencoded_size = 0;
296         }
297         else {
298                 long filesize;
299                 fflush(fin);
300                 if(-1 == (filesize = ftell(fin))) {
301                         encoder_wrapper.total_samples_to_encode = encoder_wrapper.unencoded_size = 0;
302                 }
303                 else {
304                         encoder_wrapper.unencoded_size = filesize - skip * bytes_per_wide_sample;
305                         encoder_wrapper.total_samples_to_encode = filesize / bytes_per_wide_sample - skip;
306                 }
307         }
308
309         if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_SET)) {
310                 fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
311                 goto raw_abort_;
312         }
313
314         while(!feof(fin)) {
315                 bytes_read = fread(ucbuffer, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample, fin);
316                 if(bytes_read == 0) {
317                         if(ferror(fin)) {
318                                 fprintf(stderr, "ERROR reading from %s\n", infile);
319                                 goto raw_abort_;
320                         }
321                 }
322                 else if(bytes_read % bytes_per_wide_sample != 0) {
323                         fprintf(stderr, "ERROR, got partial sample from input file %s\n", infile);
324                         goto raw_abort_;
325                 }
326                 else {
327                         unsigned wide_samples = bytes_read / bytes_per_wide_sample;
328                         format_input(wide_samples, is_big_endian, is_unsigned_samples, channels, bps);
329                         if(!FLAC__encoder_process(encoder_wrapper.encoder, input, wide_samples)) {
330                                 fprintf(stderr, "ERROR during encoding, state = %d\n", encoder_wrapper.encoder->state);
331                                 goto raw_abort_;
332                         }
333                 }
334         }
335
336         if(encoder_wrapper.encoder) {
337                 if(encoder_wrapper.encoder->state != FLAC__ENCODER_UNINITIALIZED)
338                         FLAC__encoder_finish(encoder_wrapper.encoder);
339                 FLAC__encoder_free_instance(encoder_wrapper.encoder);
340         }
341         if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0) {
342                 print_stats(&encoder_wrapper);
343                 printf("\n");
344         }
345         fclose(fin);
346         return 0;
347 raw_abort_:
348         if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0)
349                 printf("\n");
350         if(encoder_wrapper.encoder) {
351                 if(encoder_wrapper.encoder->state != FLAC__ENCODER_UNINITIALIZED)
352                         FLAC__encoder_finish(encoder_wrapper.encoder);
353                 FLAC__encoder_free_instance(encoder_wrapper.encoder);
354         }
355         fclose(fin);
356         unlink(outfile);
357         return 1;
358 }
359
360 bool init(encoder_wrapper_struct *encoder_wrapper)
361 {
362         unsigned i;
363         uint32 test = 1;
364
365         is_big_endian_host = (*((byte*)(&test)))? false : true;
366
367         for(i = 0; i < FLAC__MAX_CHANNELS; i++)
368                 input[i] = &(in[i][0]);
369
370         encoder_wrapper->encoder = FLAC__encoder_get_new_instance();
371         if(0 == encoder_wrapper->encoder) {
372                 fprintf(stderr, "ERROR creating the encoder instance\n");
373                 return false;
374         }
375
376         return true;
377 }
378
379 bool init_encoder(bool lax, bool do_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, encoder_wrapper_struct *encoder_wrapper)
380 {
381         if(channels != 2 || bps > 16)
382                 do_mid_side = false;
383
384         encoder_wrapper->encoder->streamable_subset = !lax;
385         encoder_wrapper->encoder->channels = channels;
386         encoder_wrapper->encoder->bits_per_sample = bps;
387         encoder_wrapper->encoder->sample_rate = sample_rate;
388         encoder_wrapper->encoder->blocksize = blocksize;
389         encoder_wrapper->encoder->qlp_coeff_precision = qlp_coeff_precision;
390         encoder_wrapper->encoder->max_lpc_order = max_lpc_order;
391         encoder_wrapper->encoder->do_mid_side_stereo = do_mid_side;
392         encoder_wrapper->encoder->do_exhaustive_model_search = do_exhaustive_model_search;
393         encoder_wrapper->encoder->do_qlp_coeff_prec_search = do_qlp_coeff_prec_search;
394         encoder_wrapper->encoder->rice_optimization_level = rice_optimization_level;
395
396         if(FLAC__encoder_init(encoder_wrapper->encoder, write_callback, metadata_callback, encoder_wrapper) != FLAC__ENCODER_OK) {
397                 fprintf(stderr, "ERROR initializing encoder, state = %d\n", encoder_wrapper->encoder->state);
398                 return false;
399         }
400
401         return true;
402 }
403
404 void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps)
405 {
406         unsigned wide_sample, sample, channel, byte;
407
408         if(bps == 8) {
409                 if(is_unsigned_samples) {
410                         for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
411                                 for(channel = 0; channel < channels; channel++, sample++)
412                                         input[channel][wide_sample] = (int32)ucbuffer[sample] - 128;
413                 }
414                 else {
415                         for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
416                                 for(channel = 0; channel < channels; channel++, sample++)
417                                         input[channel][wide_sample] = (int32)scbuffer[sample];
418                 }
419         }
420         else {
421                 if(is_big_endian != is_big_endian_host) {
422                         unsigned char tmp;
423                         const unsigned bytes = wide_samples * channels * (bps >> 3);
424                         for(byte = 0; byte < bytes; byte += 2) {
425                                 tmp = ucbuffer[byte];
426                                 ucbuffer[byte] = ucbuffer[byte+1];
427                                 ucbuffer[byte+1] = tmp;
428                         }
429                 }
430                 if(is_unsigned_samples) {
431                         for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
432                                 for(channel = 0; channel < channels; channel++, sample++)
433                                         input[channel][wide_sample] = (int32)usbuffer[sample] - 32768;
434                 }
435                 else {
436                         for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
437                                 for(channel = 0; channel < channels; channel++, sample++)
438                                         input[channel][wide_sample] = (int32)ssbuffer[sample];
439                 }
440         }
441 }
442
443 FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
444 {
445         encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
446         unsigned mask = (encoder->do_exhaustive_model_search || encoder->do_qlp_coeff_prec_search)? 0x07 : 0x1f;
447
448         encoder_wrapper->bytes_written += bytes;
449         encoder_wrapper->samples_written += samples;
450         encoder_wrapper->current_frame = current_frame;
451
452         if(samples && encoder_wrapper->verbose && encoder_wrapper->total_samples_to_encode > 0 && !(current_frame & mask))
453                 print_stats(encoder_wrapper);
454
455         if(fwrite(buffer, sizeof(byte), bytes, encoder_wrapper->fout) == bytes)
456                 return FLAC__ENCODER_WRITE_OK;
457         else
458                 return FLAC__ENCODER_WRITE_FATAL_ERROR;
459 }
460
461 void metadata_callback(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data)
462 {
463         encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
464         byte b;
465         FILE *f;
466         const uint64 samples = metadata->data.encoding.total_samples;
467         const unsigned min_framesize = metadata->data.encoding.min_framesize;
468         const unsigned max_framesize = metadata->data.encoding.max_framesize;
469
470         (void)encoder; /* silence compiler warning about unused parameter */
471
472         if(encoder_wrapper->fout == stdout)
473                 return;
474
475         fclose(encoder_wrapper->fout);
476         if(0 == (f = fopen(encoder_wrapper->outfile, "r+b")))
477                 return;
478
479         /* all this is based on intimate knowledge of the stream header
480          * layout, but a change to the header format that would break this
481          * would also break all streams encoded in the previous format.
482          */
483
484         if(-1 == fseek(f, 21, SEEK_SET)) goto framesize_;
485         if(fread(&b, 1, 1, f) != 1) goto framesize_;
486         if(-1 == fseek(f, 21, SEEK_SET)) goto framesize_;
487         b = (b & 0xf0) | (byte)((samples >> 32) & 0x0F);
488         if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
489         b = (byte)((samples >> 24) & 0xFF);
490         if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
491         b = (byte)((samples >> 16) & 0xFF);
492         if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
493         b = (byte)((samples >> 8) & 0xFF);
494         if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
495         b = (byte)(samples & 0xFF);
496         if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
497
498 framesize_:
499         if(-1 == fseek(f, 12, SEEK_SET)) goto end_;
500         b = (byte)((min_framesize >> 16) & 0xFF);
501         if(fwrite(&b, 1, 1, f) != 1) goto end_;
502         b = (byte)((min_framesize >> 8) & 0xFF);
503         if(fwrite(&b, 1, 1, f) != 1) goto end_;
504         b = (byte)(min_framesize & 0xFF);
505         if(fwrite(&b, 1, 1, f) != 1) goto end_;
506         b = (byte)((max_framesize >> 16) & 0xFF);
507         if(fwrite(&b, 1, 1, f) != 1) goto end_;
508         b = (byte)((max_framesize >> 8) & 0xFF);
509         if(fwrite(&b, 1, 1, f) != 1) goto end_;
510         b = (byte)(max_framesize & 0xFF);
511         if(fwrite(&b, 1, 1, f) != 1) goto end_;
512 end_:
513         fclose(encoder_wrapper->fout);
514         return;
515 }
516
517 void print_stats(const encoder_wrapper_struct *encoder_wrapper)
518 {
519 #ifdef _MSC_VER
520         /* with VC++ you have to spoon feed it the casting */
521         double progress = (double)(int64)encoder_wrapper->samples_written / (double)(int64)encoder_wrapper->total_samples_to_encode;
522 #else
523         double progress = (double)encoder_wrapper->samples_written / (double)encoder_wrapper->total_samples_to_encode;
524 #endif
525         printf("\r%0.2f%% complete: frame %u, wrote %u bytes, %u of %u samples, ratio = %5.3f",
526                 progress * 100.0, encoder_wrapper->current_frame,
527                 (unsigned)encoder_wrapper->bytes_written, (unsigned)encoder_wrapper->samples_written, (unsigned)encoder_wrapper->total_samples_to_encode,
528 #ifdef _MSC_VER
529                 /* with VC++ you have to spoon feed it the casting */
530                 (double)(int64)encoder_wrapper->bytes_written / ((double)(int64)encoder_wrapper->unencoded_size * progress)
531 #else
532                 (double)encoder_wrapper->bytes_written / ((double)encoder_wrapper->unencoded_size * progress)
533 #endif
534         );
535         fflush(stdout);
536 }
537
538 bool read_little_endian_uint16(FILE *f, uint16 *val, bool eof_ok)
539 {
540         size_t bytes_read = fread(val, 1, 2, f);
541
542         if(bytes_read == 0) {
543                 if(!eof_ok) {
544                         fprintf(stderr, "ERROR: unexpected EOF\n");
545                         return false;
546                 }
547                 else
548                         return true;
549         }
550         else if(bytes_read < 2) {
551                 fprintf(stderr, "ERROR: unexpected EOF\n");
552                 return false;
553         }
554         else {
555                 if(is_big_endian_host) {
556                         byte tmp, *b = (byte*)val;
557                         tmp = b[1]; b[1] = b[0]; b[0] = tmp;
558                 }
559                 return true;
560         }
561 }
562
563 bool read_little_endian_uint32(FILE *f, uint32 *val, bool eof_ok)
564 {
565         size_t bytes_read = fread(val, 1, 4, f);
566
567         if(bytes_read == 0) {
568                 if(!eof_ok) {
569                         fprintf(stderr, "ERROR: unexpected EOF\n");
570                         return false;
571                 }
572                 else
573                         return true;
574         }
575         else if(bytes_read < 4) {
576                 fprintf(stderr, "ERROR: unexpected EOF\n");
577                 return false;
578         }
579         else {
580                 if(is_big_endian_host) {
581                         byte tmp, *b = (byte*)val;
582                         tmp = b[3]; b[3] = b[0]; b[0] = tmp;
583                         tmp = b[2]; b[2] = b[1]; b[1] = tmp;
584                 }
585                 return true;
586         }
587 }