1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2000,2001 Josh Coalson
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.
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.
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.
19 /*@@@ need to "_finish()" the verify decoder */
22 #if defined _WIN32 && !defined __CYGWIN__
23 /* where MSVC puts unlink() */
28 #include <stdio.h> /* for FILE et al. */
29 #include <stdlib.h> /* for malloc */
30 #include <string.h> /* for strcmp() */
34 #define CHUNK_OF_SAMPLES 2048
38 FLAC__VERIFY_FAILED_IN_FRAME,
39 FLAC__VERIFY_FAILED_IN_METADATA
43 int32 *original[FLAC__MAX_CHANNELS];
44 unsigned size; /* of each original[] in samples */
45 unsigned tail; /* in wide samples */
46 const byte *encoded_signal;
47 unsigned encoded_bytes;
50 FLAC__StreamDecoder *decoder;
56 FLAC__Encoder *encoder;
59 uint64 unencoded_size;
60 uint64 total_samples_to_encode;
62 uint64 samples_written;
63 unsigned current_frame;
64 verify_fifo_struct verify_fifo;
65 } encoder_wrapper_struct;
67 static bool is_big_endian_host;
69 static unsigned char ucbuffer[CHUNK_OF_SAMPLES*FLAC__MAX_CHANNELS*(FLAC__MAX_BITS_PER_SAMPLE>>3)];
70 static signed char *scbuffer = (signed char *)ucbuffer;
71 static uint16 *usbuffer = (uint16 *)ucbuffer;
72 static int16 *ssbuffer = (int16 *)ucbuffer;
74 static int32 in[FLAC__MAX_CHANNELS][CHUNK_OF_SAMPLES];
75 static int32 *input[FLAC__MAX_CHANNELS];
78 static bool init(encoder_wrapper_struct *encoder_wrapper);
79 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);
80 static void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, encoder_wrapper_struct *encoder_wrapper);
81 static FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
82 static void metadata_callback(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
83 static FLAC__StreamDecoderReadStatus verify_read_callback(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data);
84 static FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data);
85 static void verify_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
86 static void verify_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
87 static void print_stats(const encoder_wrapper_struct *encoder_wrapper);
88 static bool read_little_endian_uint16(FILE *f, uint16 *val, bool eof_ok);
89 static bool read_little_endian_uint32(FILE *f, uint32 *val, bool eof_ok);
91 int encode_wav(const char *infile, const char *outfile, bool verbose, uint64 skip, bool verify, 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)
93 encoder_wrapper_struct encoder_wrapper;
95 bool is_unsigned_samples;
96 unsigned channels, bps, sample_rate, data_bytes;
97 size_t bytes_per_wide_sample, bytes_read;
101 encoder_wrapper.encoder = 0;
102 encoder_wrapper.verify = verify;
103 encoder_wrapper.verbose = verbose;
104 encoder_wrapper.bytes_written = 0;
105 encoder_wrapper.samples_written = 0;
106 encoder_wrapper.outfile = outfile;
108 if(0 == strcmp(infile, "-")) {
112 if(0 == (fin = fopen(infile, "rb"))) {
113 fprintf(stderr, "ERROR: can't open input file %s\n", infile);
117 if(0 == strcmp(outfile, "-")) {
118 encoder_wrapper.fout = stdout;
121 if(0 == (encoder_wrapper.fout = fopen(outfile, "wb"))) {
122 fprintf(stderr, "ERROR: can't open output file %s\n", outfile);
128 if(!init(&encoder_wrapper))
132 * check the RIFF chunk
134 if(!read_little_endian_uint32(fin, &xx, false))
136 if(xx != 0x46464952) { /* "RIFF" */
137 fprintf(stderr, "ERROR: no RIFF header\n");
140 if(!read_little_endian_uint32(fin, &xx, false))
144 * now process the WAVE chunk
146 if(!read_little_endian_uint32(fin, &xx, true))
148 if(xx != 0x45564157) { /* "WAVE" */
149 fprintf(stderr, "ERROR: no WAVE header\n");
153 /* do the format sub-chunk */
154 if(!read_little_endian_uint32(fin, &xx, false))
156 if(xx != 0x20746d66) { /* "fmt " */
157 fprintf(stderr, "ERROR: no format sub-chunk\n");
161 if(!read_little_endian_uint32(fin, &xx, false))
164 fprintf(stderr, "ERROR: unsupported chunk\n");
167 /* compression code */
168 if(!read_little_endian_uint16(fin, &x, false))
171 fprintf(stderr, "ERROR: unsupported compression type %u\n", (unsigned)x);
174 /* number of channels */
175 if(!read_little_endian_uint16(fin, &x, false))
177 if(x == 0 || x > FLAC__MAX_CHANNELS) {
178 fprintf(stderr, "ERROR: unsupported number channels %u\n", (unsigned)x);
183 if(!read_little_endian_uint32(fin, &xx, false))
185 if(xx == 0 || xx > FLAC__MAX_SAMPLE_RATE) {
186 fprintf(stderr, "ERROR: unsupported sample rate %u\n", (unsigned)xx);
190 /* avg bytes per second (ignored) */
191 if(!read_little_endian_uint32(fin, &xx, false))
193 /* block align (ignored) */
194 if(!read_little_endian_uint16(fin, &x, false))
196 /* bits per sample */
197 if(!read_little_endian_uint16(fin, &x, false))
199 if(x != 8 && x != 16) {
200 fprintf(stderr, "ERROR: unsupported bits per sample %u\n", (unsigned)x);
204 is_unsigned_samples = (x == 8);
206 /* do the data sub-chunk */
207 if(!read_little_endian_uint32(fin, &xx, false))
209 if(xx != 0x61746164) { /* "data" */
210 fprintf(stderr, "ERROR: no data sub-chunk\n");
214 if(!read_little_endian_uint32(fin, &xx, false))
218 bytes_per_wide_sample = channels * (bps >> 3);
221 if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_CUR)) {
222 fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
227 encoder_wrapper.total_samples_to_encode = data_bytes / bytes_per_wide_sample - skip;
228 encoder_wrapper.unencoded_size = encoder_wrapper.total_samples_to_encode * bytes_per_wide_sample + 44; /* 44 for the size of the WAV headers */
230 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))
233 encoder_wrapper.verify_fifo.into_frames = true;
235 while(data_bytes > 0) {
236 bytes_read = fread(ucbuffer, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample, fin);
237 if(bytes_read == 0) {
239 fprintf(stderr, "ERROR reading from %s\n", infile);
246 if(bytes_read > data_bytes)
247 bytes_read = data_bytes; /* chop off anything after the end of the data chunk */
248 if(bytes_read % bytes_per_wide_sample != 0) {
249 fprintf(stderr, "ERROR, got partial sample from input file %s\n", infile);
253 unsigned wide_samples = bytes_read / bytes_per_wide_sample;
254 format_input(wide_samples, false, is_unsigned_samples, channels, bps, &encoder_wrapper);
255 if(!FLAC__encoder_process(encoder_wrapper.encoder, input, wide_samples)) {
256 fprintf(stderr, "ERROR during encoding, state = %d:%s\n", encoder_wrapper.encoder->state, FLAC__EncoderStateString[encoder_wrapper.encoder->state]);
259 data_bytes -= bytes_read;
265 if(encoder_wrapper.encoder) {
266 if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
267 FLAC__encoder_finish(encoder_wrapper.encoder);
268 FLAC__encoder_free_instance(encoder_wrapper.encoder);
270 if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0) {
271 print_stats(&encoder_wrapper);
275 if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
276 printf("Verify FAILED! Do not use %s\n", outfile);
280 printf("Verify succeeded\n");
286 if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0)
288 if(encoder_wrapper.encoder) {
289 if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
290 FLAC__encoder_finish(encoder_wrapper.encoder);
291 FLAC__encoder_free_instance(encoder_wrapper.encoder);
294 if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
295 printf("Verify FAILED! Do not use %s\n", outfile);
299 printf("Verify succeeded\n");
307 int encode_raw(const char *infile, const char *outfile, bool verbose, uint64 skip, bool verify, 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)
309 encoder_wrapper_struct encoder_wrapper;
312 const size_t bytes_per_wide_sample = channels * (bps >> 3);
314 encoder_wrapper.encoder = 0;
315 encoder_wrapper.verify = verify;
316 encoder_wrapper.verbose = verbose;
317 encoder_wrapper.bytes_written = 0;
318 encoder_wrapper.samples_written = 0;
319 encoder_wrapper.outfile = outfile;
321 if(0 == strcmp(infile, "-")) {
325 if(0 == (fin = fopen(infile, "rb"))) {
326 fprintf(stderr, "ERROR: can't open input file %s\n", infile);
330 if(0 == strcmp(outfile, "-")) {
331 encoder_wrapper.fout = stdout;
334 if(0 == (encoder_wrapper.fout = fopen(outfile, "wb"))) {
335 fprintf(stderr, "ERROR: can't open output file %s\n", outfile);
341 if(!init(&encoder_wrapper))
344 /* get the file length */
345 if(0 != fseek(fin, 0, SEEK_END)) {
346 encoder_wrapper.total_samples_to_encode = encoder_wrapper.unencoded_size = 0;
351 if(-1 == (filesize = ftell(fin))) {
352 encoder_wrapper.total_samples_to_encode = encoder_wrapper.unencoded_size = 0;
355 encoder_wrapper.unencoded_size = filesize - skip * bytes_per_wide_sample;
356 encoder_wrapper.total_samples_to_encode = filesize / bytes_per_wide_sample - skip;
360 if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_SET)) {
361 fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
365 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))
368 encoder_wrapper.verify_fifo.into_frames = true;
371 bytes_read = fread(ucbuffer, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample, fin);
372 if(bytes_read == 0) {
374 fprintf(stderr, "ERROR reading from %s\n", infile);
378 else if(bytes_read % bytes_per_wide_sample != 0) {
379 fprintf(stderr, "ERROR, got partial sample from input file %s\n", infile);
383 unsigned wide_samples = bytes_read / bytes_per_wide_sample;
384 format_input(wide_samples, is_big_endian, is_unsigned_samples, channels, bps, &encoder_wrapper);
385 if(!FLAC__encoder_process(encoder_wrapper.encoder, input, wide_samples)) {
386 fprintf(stderr, "ERROR during encoding, state = %d:%s\n", encoder_wrapper.encoder->state, FLAC__EncoderStateString[encoder_wrapper.encoder->state]);
392 if(encoder_wrapper.encoder) {
393 if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
394 FLAC__encoder_finish(encoder_wrapper.encoder);
395 FLAC__encoder_free_instance(encoder_wrapper.encoder);
397 if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0) {
398 print_stats(&encoder_wrapper);
402 if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
403 printf("Verify FAILED! Do not use %s\n", outfile);
407 printf("Verify succeeded\n");
413 if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0)
415 if(encoder_wrapper.encoder) {
416 if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
417 FLAC__encoder_finish(encoder_wrapper.encoder);
418 FLAC__encoder_free_instance(encoder_wrapper.encoder);
421 if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
422 printf("Verify FAILED! Do not use %s\n", outfile);
426 printf("Verify succeeded\n");
434 bool init(encoder_wrapper_struct *encoder_wrapper)
439 is_big_endian_host = (*((byte*)(&test)))? false : true;
441 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
442 input[i] = &(in[i][0]);
444 encoder_wrapper->encoder = FLAC__encoder_get_new_instance();
445 if(0 == encoder_wrapper->encoder) {
446 fprintf(stderr, "ERROR creating the encoder instance\n");
453 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)
455 if(channels != 2 || bps > 16)
458 if(encoder_wrapper->verify) {
461 /* set up the fifo which will hold the original signal to compare against */
462 encoder_wrapper->verify_fifo.size = blocksize + CHUNK_OF_SAMPLES;
463 for(i = 0; i < channels; i++) {
464 if(0 == (encoder_wrapper->verify_fifo.original[i] = (int32*)malloc(sizeof(int32) * encoder_wrapper->verify_fifo.size))) {
465 fprintf(stderr, "ERROR allocating verify buffers\n");
469 encoder_wrapper->verify_fifo.tail = 0;
470 encoder_wrapper->verify_fifo.into_frames = false;
471 encoder_wrapper->verify_fifo.result = FLAC__VERIFY_OK;
473 /* set up a stream decoder for verification */
474 encoder_wrapper->verify_fifo.decoder = FLAC__stream_decoder_get_new_instance();
475 if(0 == encoder_wrapper->verify_fifo.decoder) {
476 fprintf(stderr, "ERROR creating the verify decoder instance\n");
479 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) {
480 fprintf(stderr, "ERROR initializing decoder, state = %d:%s\n", encoder_wrapper->verify_fifo.decoder->state, FLAC__StreamDecoderStateString[encoder_wrapper->verify_fifo.decoder->state]);
485 encoder_wrapper->encoder->streamable_subset = !lax;
486 encoder_wrapper->encoder->channels = channels;
487 encoder_wrapper->encoder->bits_per_sample = bps;
488 encoder_wrapper->encoder->sample_rate = sample_rate;
489 encoder_wrapper->encoder->blocksize = blocksize;
490 encoder_wrapper->encoder->qlp_coeff_precision = qlp_coeff_precision;
491 encoder_wrapper->encoder->max_lpc_order = max_lpc_order;
492 encoder_wrapper->encoder->do_mid_side_stereo = do_mid_side;
493 encoder_wrapper->encoder->do_exhaustive_model_search = do_exhaustive_model_search;
494 encoder_wrapper->encoder->do_qlp_coeff_prec_search = do_qlp_coeff_prec_search;
495 encoder_wrapper->encoder->rice_optimization_level = rice_optimization_level;
496 encoder_wrapper->encoder->total_samples_estimate = encoder_wrapper->total_samples_to_encode;
498 if(FLAC__encoder_init(encoder_wrapper->encoder, write_callback, metadata_callback, encoder_wrapper) != FLAC__ENCODER_OK) {
499 fprintf(stderr, "ERROR initializing encoder, state = %d\n", encoder_wrapper->encoder->state);
506 void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, encoder_wrapper_struct *encoder_wrapper)
508 unsigned wide_sample, sample, channel, byte;
511 if(is_unsigned_samples) {
512 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
513 for(channel = 0; channel < channels; channel++, sample++)
514 input[channel][wide_sample] = (int32)ucbuffer[sample] - 128;
517 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
518 for(channel = 0; channel < channels; channel++, sample++)
519 input[channel][wide_sample] = (int32)scbuffer[sample];
523 if(is_big_endian != is_big_endian_host) {
525 const unsigned bytes = wide_samples * channels * (bps >> 3);
526 for(byte = 0; byte < bytes; byte += 2) {
527 tmp = ucbuffer[byte];
528 ucbuffer[byte] = ucbuffer[byte+1];
529 ucbuffer[byte+1] = tmp;
532 if(is_unsigned_samples) {
533 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
534 for(channel = 0; channel < channels; channel++, sample++)
535 input[channel][wide_sample] = (int32)usbuffer[sample] - 32768;
538 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
539 for(channel = 0; channel < channels; channel++, sample++)
540 input[channel][wide_sample] = (int32)ssbuffer[sample];
544 if(encoder_wrapper->verify) {
545 for(channel = 0; channel < channels; channel++)
546 memcpy(&encoder_wrapper->verify_fifo.original[channel][encoder_wrapper->verify_fifo.tail], &input[channel][0], sizeof(int32) * wide_samples);
547 encoder_wrapper->verify_fifo.tail += wide_samples;
548 assert(encoder_wrapper->verify_fifo.tail <= encoder_wrapper->verify_fifo.size);
552 FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
554 encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
555 unsigned mask = (encoder->do_exhaustive_model_search || encoder->do_qlp_coeff_prec_search)? 0x07 : 0x1f;
557 encoder_wrapper->bytes_written += bytes;
558 encoder_wrapper->samples_written += samples;
559 encoder_wrapper->current_frame = current_frame;
561 if(samples && encoder_wrapper->verbose && encoder_wrapper->total_samples_to_encode > 0 && !(current_frame & mask))
562 print_stats(encoder_wrapper);
564 if(encoder_wrapper->verify) {
565 encoder_wrapper->verify_fifo.encoded_signal = buffer;
566 encoder_wrapper->verify_fifo.encoded_bytes = bytes;
567 if(encoder_wrapper->verify_fifo.into_frames) {
568 if(!FLAC__stream_decoder_process_one_frame(encoder_wrapper->verify_fifo.decoder)) {
569 encoder_wrapper->verify_fifo.result = FLAC__VERIFY_FAILED_IN_FRAME;
570 return FLAC__ENCODER_WRITE_FATAL_ERROR;
574 if(!FLAC__stream_decoder_process_metadata(encoder_wrapper->verify_fifo.decoder)) {
575 encoder_wrapper->verify_fifo.result = FLAC__VERIFY_FAILED_IN_METADATA;
576 return FLAC__ENCODER_WRITE_FATAL_ERROR;
581 if(fwrite(buffer, sizeof(byte), bytes, encoder_wrapper->fout) == bytes)
582 return FLAC__ENCODER_WRITE_OK;
584 return FLAC__ENCODER_WRITE_FATAL_ERROR;
587 void metadata_callback(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data)
589 encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
592 const uint64 samples = metadata->data.encoding.total_samples;
593 const unsigned min_framesize = metadata->data.encoding.min_framesize;
594 const unsigned max_framesize = metadata->data.encoding.max_framesize;
596 (void)encoder; /* silence compiler warning about unused parameter */
598 if(encoder_wrapper->fout == stdout)
601 fclose(encoder_wrapper->fout);
602 if(0 == (f = fopen(encoder_wrapper->outfile, "r+b")))
605 /* all this is based on intimate knowledge of the stream header
606 * layout, but a change to the header format that would break this
607 * would also break all streams encoded in the previous format.
610 if(-1 == fseek(f, 26, SEEK_SET)) goto samples_;
611 fwrite(metadata->data.encoding.md5sum, 1, 16, f);
614 if(-1 == fseek(f, 21, SEEK_SET)) goto framesize_;
615 if(fread(&b, 1, 1, f) != 1) goto framesize_;
616 if(-1 == fseek(f, 21, SEEK_SET)) goto framesize_;
617 b = (b & 0xf0) | (byte)((samples >> 32) & 0x0F);
618 if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
619 b = (byte)((samples >> 24) & 0xFF);
620 if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
621 b = (byte)((samples >> 16) & 0xFF);
622 if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
623 b = (byte)((samples >> 8) & 0xFF);
624 if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
625 b = (byte)(samples & 0xFF);
626 if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
629 if(-1 == fseek(f, 12, SEEK_SET)) goto end_;
630 b = (byte)((min_framesize >> 16) & 0xFF);
631 if(fwrite(&b, 1, 1, f) != 1) goto end_;
632 b = (byte)((min_framesize >> 8) & 0xFF);
633 if(fwrite(&b, 1, 1, f) != 1) goto end_;
634 b = (byte)(min_framesize & 0xFF);
635 if(fwrite(&b, 1, 1, f) != 1) goto end_;
636 b = (byte)((max_framesize >> 16) & 0xFF);
637 if(fwrite(&b, 1, 1, f) != 1) goto end_;
638 b = (byte)((max_framesize >> 8) & 0xFF);
639 if(fwrite(&b, 1, 1, f) != 1) goto end_;
640 b = (byte)(max_framesize & 0xFF);
641 if(fwrite(&b, 1, 1, f) != 1) goto end_;
643 fclose(encoder_wrapper->fout);
647 FLAC__StreamDecoderReadStatus verify_read_callback(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data)
649 encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
652 *bytes = encoder_wrapper->verify_fifo.encoded_bytes;
653 memcpy(buffer, encoder_wrapper->verify_fifo.encoded_signal, *bytes);
655 return FLAC__STREAM_DECODER_READ_CONTINUE;
658 FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data)
660 encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
661 unsigned channel, l, r;
663 for(channel = 0; channel < decoder->channels; channel++) {
664 if(0 != memcmp(buffer[channel], encoder_wrapper->verify_fifo.original[channel], sizeof(int32) * decoder->blocksize)) {
665 fprintf(stderr, "\nERROR: mismatch in decoded data, verify FAILED!\n");
666 fprintf(stderr, " Please submit a bug report to http://sourceforge.net/bugs/?func=addbug&group_id=13478\n");
667 return FLAC__STREAM_DECODER_WRITE_ABORT;
670 /* dequeue the frame from the fifo */
671 for(channel = 0; channel < decoder->channels; channel++) {
672 for(l = 0, r = header->blocksize; r < encoder_wrapper->verify_fifo.tail; l++, r++) {
673 encoder_wrapper->verify_fifo.original[channel][l] = encoder_wrapper->verify_fifo.original[channel][r];
676 encoder_wrapper->verify_fifo.tail -= header->blocksize;
677 return FLAC__STREAM_DECODER_WRITE_CONTINUE;
680 void verify_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
687 void verify_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
691 fprintf(stderr, "\nERROR: verification decoder returned error %d:%s\n", status, FLAC__StreamDecoderErrorStatusString[status]);
694 void print_stats(const encoder_wrapper_struct *encoder_wrapper)
697 /* with VC++ you have to spoon feed it the casting */
698 double progress = (double)(int64)encoder_wrapper->samples_written / (double)(int64)encoder_wrapper->total_samples_to_encode;
700 double progress = (double)encoder_wrapper->samples_written / (double)encoder_wrapper->total_samples_to_encode;
702 printf("\r%0.2f%% complete: frame %u, wrote %u bytes, %u of %u samples, ratio = %5.3f",
703 progress * 100.0, encoder_wrapper->current_frame,
704 (unsigned)encoder_wrapper->bytes_written, (unsigned)encoder_wrapper->samples_written, (unsigned)encoder_wrapper->total_samples_to_encode,
706 /* with VC++ you have to spoon feed it the casting */
707 (double)(int64)encoder_wrapper->bytes_written / ((double)(int64)encoder_wrapper->unencoded_size * progress)
709 (double)encoder_wrapper->bytes_written / ((double)encoder_wrapper->unencoded_size * progress)
715 bool read_little_endian_uint16(FILE *f, uint16 *val, bool eof_ok)
717 size_t bytes_read = fread(val, 1, 2, f);
719 if(bytes_read == 0) {
721 fprintf(stderr, "ERROR: unexpected EOF\n");
727 else if(bytes_read < 2) {
728 fprintf(stderr, "ERROR: unexpected EOF\n");
732 if(is_big_endian_host) {
733 byte tmp, *b = (byte*)val;
734 tmp = b[1]; b[1] = b[0]; b[0] = tmp;
740 bool read_little_endian_uint32(FILE *f, uint32 *val, bool eof_ok)
742 size_t bytes_read = fread(val, 1, 4, f);
744 if(bytes_read == 0) {
746 fprintf(stderr, "ERROR: unexpected EOF\n");
752 else if(bytes_read < 4) {
753 fprintf(stderr, "ERROR: unexpected EOF\n");
757 if(is_big_endian_host) {
758 byte tmp, *b = (byte*)val;
759 tmp = b[3]; b[3] = b[0]; b[0] = tmp;
760 tmp = b[2]; b[2] = b[1]; b[1] = tmp;