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() */
37 #define min(x,y) ((x)<(y)?(x):(y))
39 #define CHUNK_OF_SAMPLES 2048
43 FLAC__VERIFY_FAILED_IN_FRAME,
44 FLAC__VERIFY_FAILED_IN_METADATA
48 int32 *original[FLAC__MAX_CHANNELS];
49 unsigned size; /* of each original[] in samples */
50 unsigned tail; /* in wide samples */
51 const byte *encoded_signal;
52 unsigned encoded_signal_capacity;
53 unsigned encoded_bytes;
56 FLAC__StreamDecoder *decoder;
62 FLAC__Encoder *encoder;
65 uint64 unencoded_size;
66 uint64 total_samples_to_encode;
68 uint64 samples_written;
69 unsigned current_frame;
70 verify_fifo_struct verify_fifo;
71 } encoder_wrapper_struct;
73 static bool is_big_endian_host;
75 static unsigned char ucbuffer[CHUNK_OF_SAMPLES*FLAC__MAX_CHANNELS*(FLAC__MAX_BITS_PER_SAMPLE>>3)];
76 static signed char *scbuffer = (signed char *)ucbuffer;
77 static uint16 *usbuffer = (uint16 *)ucbuffer;
78 static int16 *ssbuffer = (int16 *)ucbuffer;
80 static int32 in[FLAC__MAX_CHANNELS][CHUNK_OF_SAMPLES];
81 static int32 *input[FLAC__MAX_CHANNELS];
84 static bool init(encoder_wrapper_struct *encoder_wrapper);
85 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, encoder_wrapper_struct *encoder_wrapper);
86 static void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, encoder_wrapper_struct *encoder_wrapper);
87 static FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
88 static void metadata_callback(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
89 static FLAC__StreamDecoderReadStatus verify_read_callback(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data);
90 static FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
91 static void verify_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
92 static void verify_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
93 static void print_stats(const encoder_wrapper_struct *encoder_wrapper);
94 static bool read_little_endian_uint16(FILE *f, uint16 *val, bool eof_ok);
95 static bool read_little_endian_uint32(FILE *f, uint32 *val, bool eof_ok);
97 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)
99 encoder_wrapper_struct encoder_wrapper;
101 bool is_unsigned_samples;
102 unsigned channels, bps, sample_rate, data_bytes;
103 size_t bytes_per_wide_sample, bytes_read;
107 encoder_wrapper.encoder = 0;
108 encoder_wrapper.verify = verify;
109 encoder_wrapper.verbose = verbose;
110 encoder_wrapper.bytes_written = 0;
111 encoder_wrapper.samples_written = 0;
112 encoder_wrapper.outfile = outfile;
114 if(0 == strcmp(infile, "-")) {
118 if(0 == (fin = fopen(infile, "rb"))) {
119 fprintf(stderr, "ERROR: can't open input file %s\n", infile);
123 if(0 == strcmp(outfile, "-")) {
124 encoder_wrapper.fout = stdout;
127 if(0 == (encoder_wrapper.fout = fopen(outfile, "wb"))) {
128 fprintf(stderr, "ERROR: can't open output file %s\n", outfile);
134 if(!init(&encoder_wrapper))
138 * check the RIFF chunk
140 if(!read_little_endian_uint32(fin, &xx, false))
142 if(xx != 0x46464952) { /* "RIFF" */
143 fprintf(stderr, "ERROR: no RIFF header\n");
146 if(!read_little_endian_uint32(fin, &xx, false))
150 * now process the WAVE chunk
152 if(!read_little_endian_uint32(fin, &xx, true))
154 if(xx != 0x45564157) { /* "WAVE" */
155 fprintf(stderr, "ERROR: no WAVE header\n");
159 /* do the format sub-chunk */
160 if(!read_little_endian_uint32(fin, &xx, false))
162 if(xx != 0x20746d66) { /* "fmt " */
163 fprintf(stderr, "ERROR: no format sub-chunk\n");
167 if(!read_little_endian_uint32(fin, &xx, false))
170 fprintf(stderr, "ERROR: unsupported chunk\n");
173 /* compression code */
174 if(!read_little_endian_uint16(fin, &x, false))
177 fprintf(stderr, "ERROR: unsupported compression type %u\n", (unsigned)x);
180 /* number of channels */
181 if(!read_little_endian_uint16(fin, &x, false))
183 if(x == 0 || x > FLAC__MAX_CHANNELS) {
184 fprintf(stderr, "ERROR: unsupported number channels %u\n", (unsigned)x);
189 if(!read_little_endian_uint32(fin, &xx, false))
191 if(xx == 0 || xx > FLAC__MAX_SAMPLE_RATE) {
192 fprintf(stderr, "ERROR: unsupported sample rate %u\n", (unsigned)xx);
196 /* avg bytes per second (ignored) */
197 if(!read_little_endian_uint32(fin, &xx, false))
199 /* block align (ignored) */
200 if(!read_little_endian_uint16(fin, &x, false))
202 /* bits per sample */
203 if(!read_little_endian_uint16(fin, &x, false))
205 if(x != 8 && x != 16) {
206 fprintf(stderr, "ERROR: unsupported bits per sample %u\n", (unsigned)x);
210 is_unsigned_samples = (x == 8);
212 /* do the data sub-chunk */
213 if(!read_little_endian_uint32(fin, &xx, false))
215 if(xx != 0x61746164) { /* "data" */
216 fprintf(stderr, "ERROR: no data sub-chunk\n");
220 if(!read_little_endian_uint32(fin, &xx, false))
224 bytes_per_wide_sample = channels * (bps >> 3);
228 if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_CUR)) {
229 fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
236 for(left = (int64)skip; left > 0; left -= CHUNK_OF_SAMPLES) {
237 need = min(left, CHUNK_OF_SAMPLES);
238 if(fread(ucbuffer, 1, bytes_per_wide_sample * need, fin) < need) {
239 fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
246 encoder_wrapper.total_samples_to_encode = data_bytes / bytes_per_wide_sample - skip;
247 encoder_wrapper.unencoded_size = encoder_wrapper.total_samples_to_encode * bytes_per_wide_sample + 44; /* 44 for the size of the WAV headers */
249 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, &encoder_wrapper))
252 encoder_wrapper.verify_fifo.into_frames = true;
254 while(data_bytes > 0) {
255 bytes_read = fread(ucbuffer, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample, fin);
256 if(bytes_read == 0) {
258 fprintf(stderr, "ERROR reading from %s\n", infile);
265 if(bytes_read > data_bytes)
266 bytes_read = data_bytes; /* chop off anything after the end of the data chunk */
267 if(bytes_read % bytes_per_wide_sample != 0) {
268 fprintf(stderr, "ERROR, got partial sample from input file %s\n", infile);
272 unsigned wide_samples = bytes_read / bytes_per_wide_sample;
273 format_input(wide_samples, false, is_unsigned_samples, channels, bps, &encoder_wrapper);
274 if(!FLAC__encoder_process(encoder_wrapper.encoder, input, wide_samples)) {
275 fprintf(stderr, "ERROR during encoding, state = %d:%s\n", encoder_wrapper.encoder->state, FLAC__EncoderStateString[encoder_wrapper.encoder->state]);
278 data_bytes -= bytes_read;
284 if(encoder_wrapper.encoder) {
285 if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
286 FLAC__encoder_finish(encoder_wrapper.encoder);
287 FLAC__encoder_free_instance(encoder_wrapper.encoder);
289 if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0) {
290 print_stats(&encoder_wrapper);
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");
305 if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0)
307 if(encoder_wrapper.encoder) {
308 if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
309 FLAC__encoder_finish(encoder_wrapper.encoder);
310 FLAC__encoder_free_instance(encoder_wrapper.encoder);
313 if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
314 printf("Verify FAILED! Do not use %s\n", outfile);
318 printf("Verify succeeded\n");
326 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, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, unsigned sample_rate)
328 encoder_wrapper_struct encoder_wrapper;
331 const size_t bytes_per_wide_sample = channels * (bps >> 3);
333 encoder_wrapper.encoder = 0;
334 encoder_wrapper.verify = verify;
335 encoder_wrapper.verbose = verbose;
336 encoder_wrapper.bytes_written = 0;
337 encoder_wrapper.samples_written = 0;
338 encoder_wrapper.outfile = outfile;
340 if(0 == strcmp(infile, "-")) {
344 if(0 == (fin = fopen(infile, "rb"))) {
345 fprintf(stderr, "ERROR: can't open input file %s\n", infile);
349 if(0 == strcmp(outfile, "-")) {
350 encoder_wrapper.fout = stdout;
353 if(0 == (encoder_wrapper.fout = fopen(outfile, "wb"))) {
354 fprintf(stderr, "ERROR: can't open output file %s\n", outfile);
360 if(!init(&encoder_wrapper))
363 /* get the file length */
364 if(0 != fseek(fin, 0, SEEK_END)) {
365 encoder_wrapper.total_samples_to_encode = encoder_wrapper.unencoded_size = 0;
370 if(-1 == (filesize = ftell(fin))) {
371 encoder_wrapper.total_samples_to_encode = encoder_wrapper.unencoded_size = 0;
374 encoder_wrapper.unencoded_size = filesize - skip * bytes_per_wide_sample;
375 encoder_wrapper.total_samples_to_encode = filesize / bytes_per_wide_sample - skip;
381 if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_SET)) {
382 fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
389 for(left = (int64)skip; left > 0; left -= CHUNK_OF_SAMPLES) {
390 need = min(left, CHUNK_OF_SAMPLES);
391 if(fread(ucbuffer, 1, bytes_per_wide_sample * need, fin) < need) {
392 fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
399 fseek(fin, 0, SEEK_SET);
402 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, &encoder_wrapper))
405 encoder_wrapper.verify_fifo.into_frames = true;
408 bytes_read = fread(ucbuffer, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample, fin);
409 if(bytes_read == 0) {
411 fprintf(stderr, "ERROR reading from %s\n", infile);
415 else if(bytes_read % bytes_per_wide_sample != 0) {
416 fprintf(stderr, "ERROR, got partial sample from input file %s\n", infile);
420 unsigned wide_samples = bytes_read / bytes_per_wide_sample;
421 format_input(wide_samples, is_big_endian, is_unsigned_samples, channels, bps, &encoder_wrapper);
422 if(!FLAC__encoder_process(encoder_wrapper.encoder, input, wide_samples)) {
423 fprintf(stderr, "ERROR during encoding, state = %d:%s\n", encoder_wrapper.encoder->state, FLAC__EncoderStateString[encoder_wrapper.encoder->state]);
429 if(encoder_wrapper.encoder) {
430 if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
431 FLAC__encoder_finish(encoder_wrapper.encoder);
432 FLAC__encoder_free_instance(encoder_wrapper.encoder);
434 if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0) {
435 print_stats(&encoder_wrapper);
439 if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
440 printf("Verify FAILED! Do not use %s\n", outfile);
444 printf("Verify succeeded\n");
450 if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0)
452 if(encoder_wrapper.encoder) {
453 if(encoder_wrapper.encoder->state == FLAC__ENCODER_OK)
454 FLAC__encoder_finish(encoder_wrapper.encoder);
455 FLAC__encoder_free_instance(encoder_wrapper.encoder);
458 if(encoder_wrapper.verify_fifo.result != FLAC__VERIFY_OK) {
459 printf("Verify FAILED! Do not use %s\n", outfile);
463 printf("Verify succeeded\n");
471 bool init(encoder_wrapper_struct *encoder_wrapper)
476 is_big_endian_host = (*((byte*)(&test)))? false : true;
478 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
479 input[i] = &(in[i][0]);
481 encoder_wrapper->encoder = FLAC__encoder_get_new_instance();
482 if(0 == encoder_wrapper->encoder) {
483 fprintf(stderr, "ERROR creating the encoder instance\n");
490 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, encoder_wrapper_struct *encoder_wrapper)
492 if(channels != 2 || bps > 16)
493 do_mid_side = loose_mid_side = false;
495 if(encoder_wrapper->verify) {
498 /* set up the fifo which will hold the original signal to compare against */
499 encoder_wrapper->verify_fifo.size = blocksize + CHUNK_OF_SAMPLES;
500 for(i = 0; i < channels; i++) {
501 if(0 == (encoder_wrapper->verify_fifo.original[i] = (int32*)malloc(sizeof(int32) * encoder_wrapper->verify_fifo.size))) {
502 fprintf(stderr, "ERROR allocating verify buffers\n");
506 encoder_wrapper->verify_fifo.tail = 0;
507 encoder_wrapper->verify_fifo.into_frames = false;
508 encoder_wrapper->verify_fifo.result = FLAC__VERIFY_OK;
510 /* set up a stream decoder for verification */
511 encoder_wrapper->verify_fifo.decoder = FLAC__stream_decoder_get_new_instance();
512 if(0 == encoder_wrapper->verify_fifo.decoder) {
513 fprintf(stderr, "ERROR creating the verify decoder instance\n");
516 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) {
517 fprintf(stderr, "ERROR initializing decoder, state = %d:%s\n", encoder_wrapper->verify_fifo.decoder->state, FLAC__StreamDecoderStateString[encoder_wrapper->verify_fifo.decoder->state]);
522 encoder_wrapper->encoder->streamable_subset = !lax;
523 encoder_wrapper->encoder->channels = channels;
524 encoder_wrapper->encoder->bits_per_sample = bps;
525 encoder_wrapper->encoder->sample_rate = sample_rate;
526 encoder_wrapper->encoder->blocksize = blocksize;
527 encoder_wrapper->encoder->qlp_coeff_precision = qlp_coeff_precision;
528 encoder_wrapper->encoder->max_lpc_order = max_lpc_order;
529 encoder_wrapper->encoder->do_mid_side_stereo = do_mid_side;
530 encoder_wrapper->encoder->loose_mid_side_stereo = loose_mid_side;
531 encoder_wrapper->encoder->do_exhaustive_model_search = do_exhaustive_model_search;
532 encoder_wrapper->encoder->do_qlp_coeff_prec_search = do_qlp_coeff_prec_search;
533 encoder_wrapper->encoder->rice_optimization_level = rice_optimization_level;
534 encoder_wrapper->encoder->total_samples_estimate = encoder_wrapper->total_samples_to_encode;
536 if(FLAC__encoder_init(encoder_wrapper->encoder, write_callback, metadata_callback, encoder_wrapper) != FLAC__ENCODER_OK) {
537 fprintf(stderr, "ERROR initializing encoder, state = %d\n", encoder_wrapper->encoder->state);
544 void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, encoder_wrapper_struct *encoder_wrapper)
546 unsigned wide_sample, sample, channel, byte;
549 if(is_unsigned_samples) {
550 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
551 for(channel = 0; channel < channels; channel++, sample++)
552 input[channel][wide_sample] = (int32)ucbuffer[sample] - 128;
555 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
556 for(channel = 0; channel < channels; channel++, sample++)
557 input[channel][wide_sample] = (int32)scbuffer[sample];
561 if(is_big_endian != is_big_endian_host) {
563 const unsigned bytes = wide_samples * channels * (bps >> 3);
564 for(byte = 0; byte < bytes; byte += 2) {
565 tmp = ucbuffer[byte];
566 ucbuffer[byte] = ucbuffer[byte+1];
567 ucbuffer[byte+1] = tmp;
570 if(is_unsigned_samples) {
571 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
572 for(channel = 0; channel < channels; channel++, sample++)
573 input[channel][wide_sample] = (int32)usbuffer[sample] - 32768;
576 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
577 for(channel = 0; channel < channels; channel++, sample++)
578 input[channel][wide_sample] = (int32)ssbuffer[sample];
582 if(encoder_wrapper->verify) {
583 for(channel = 0; channel < channels; channel++)
584 memcpy(&encoder_wrapper->verify_fifo.original[channel][encoder_wrapper->verify_fifo.tail], &input[channel][0], sizeof(int32) * wide_samples);
585 encoder_wrapper->verify_fifo.tail += wide_samples;
586 assert(encoder_wrapper->verify_fifo.tail <= encoder_wrapper->verify_fifo.size);
590 FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
592 encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
593 unsigned mask = (encoder->do_exhaustive_model_search || encoder->do_qlp_coeff_prec_search)? 0x07 : 0x1f;
595 encoder_wrapper->bytes_written += bytes;
596 encoder_wrapper->samples_written += samples;
597 encoder_wrapper->current_frame = current_frame;
599 if(samples && encoder_wrapper->verbose && encoder_wrapper->total_samples_to_encode > 0 && !(current_frame & mask))
600 print_stats(encoder_wrapper);
602 if(encoder_wrapper->verify) {
603 encoder_wrapper->verify_fifo.encoded_signal = buffer;
604 encoder_wrapper->verify_fifo.encoded_bytes = bytes;
605 if(encoder_wrapper->verify_fifo.into_frames) {
606 if(!FLAC__stream_decoder_process_one_frame(encoder_wrapper->verify_fifo.decoder)) {
607 encoder_wrapper->verify_fifo.result = FLAC__VERIFY_FAILED_IN_FRAME;
608 return FLAC__ENCODER_WRITE_FATAL_ERROR;
612 if(!FLAC__stream_decoder_process_metadata(encoder_wrapper->verify_fifo.decoder)) {
613 encoder_wrapper->verify_fifo.result = FLAC__VERIFY_FAILED_IN_METADATA;
614 return FLAC__ENCODER_WRITE_FATAL_ERROR;
619 if(fwrite(buffer, sizeof(byte), bytes, encoder_wrapper->fout) == bytes)
620 return FLAC__ENCODER_WRITE_OK;
622 return FLAC__ENCODER_WRITE_FATAL_ERROR;
625 void metadata_callback(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data)
627 encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
630 const uint64 samples = metadata->data.encoding.total_samples;
631 const unsigned min_framesize = metadata->data.encoding.min_framesize;
632 const unsigned max_framesize = metadata->data.encoding.max_framesize;
634 (void)encoder; /* silence compiler warning about unused parameter */
636 if(encoder_wrapper->fout == stdout)
639 fclose(encoder_wrapper->fout);
640 if(0 == (f = fopen(encoder_wrapper->outfile, "r+b")))
643 /* all this is based on intimate knowledge of the stream header
644 * layout, but a change to the header format that would break this
645 * would also break all streams encoded in the previous format.
648 if(-1 == fseek(f, 26, SEEK_SET)) goto samples_;
649 fwrite(metadata->data.encoding.md5sum, 1, 16, f);
652 if(-1 == fseek(f, 21, SEEK_SET)) goto framesize_;
653 if(fread(&b, 1, 1, f) != 1) goto framesize_;
654 if(-1 == fseek(f, 21, SEEK_SET)) goto framesize_;
655 b = (b & 0xf0) | (byte)((samples >> 32) & 0x0F);
656 if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
657 b = (byte)((samples >> 24) & 0xFF);
658 if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
659 b = (byte)((samples >> 16) & 0xFF);
660 if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
661 b = (byte)((samples >> 8) & 0xFF);
662 if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
663 b = (byte)(samples & 0xFF);
664 if(fwrite(&b, 1, 1, f) != 1) goto framesize_;
667 if(-1 == fseek(f, 12, SEEK_SET)) goto end_;
668 b = (byte)((min_framesize >> 16) & 0xFF);
669 if(fwrite(&b, 1, 1, f) != 1) goto end_;
670 b = (byte)((min_framesize >> 8) & 0xFF);
671 if(fwrite(&b, 1, 1, f) != 1) goto end_;
672 b = (byte)(min_framesize & 0xFF);
673 if(fwrite(&b, 1, 1, f) != 1) goto end_;
674 b = (byte)((max_framesize >> 16) & 0xFF);
675 if(fwrite(&b, 1, 1, f) != 1) goto end_;
676 b = (byte)((max_framesize >> 8) & 0xFF);
677 if(fwrite(&b, 1, 1, f) != 1) goto end_;
678 b = (byte)(max_framesize & 0xFF);
679 if(fwrite(&b, 1, 1, f) != 1) goto end_;
685 FLAC__StreamDecoderReadStatus verify_read_callback(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data)
687 encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
688 const unsigned encoded_bytes = encoder_wrapper->verify_fifo.encoded_bytes;
691 if(encoded_bytes <= *bytes) {
692 *bytes = encoded_bytes;
693 memcpy(buffer, encoder_wrapper->verify_fifo.encoded_signal, *bytes);
696 fprintf(stderr,"@@@ verify_read_callback: encoded_bytes=%u, *bytes=%u\n",encoded_bytes,*bytes);
697 memcpy(buffer, encoder_wrapper->verify_fifo.encoded_signal, *bytes);
698 encoder_wrapper->verify_fifo.encoded_signal += *bytes;
699 encoder_wrapper->verify_fifo.encoded_bytes -= *bytes;
702 return FLAC__STREAM_DECODER_READ_CONTINUE;
705 FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data)
707 encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
708 unsigned channel, l, r;
710 for(channel = 0; channel < decoder->channels; channel++) {
711 if(0 != memcmp(buffer[channel], encoder_wrapper->verify_fifo.original[channel], sizeof(int32) * decoder->blocksize)) {
712 fprintf(stderr, "\nERROR: mismatch in decoded data, verify FAILED!\n");
713 fprintf(stderr, " Please submit a bug report to http://sourceforge.net/bugs/?func=addbug&group_id=13478\n");
714 return FLAC__STREAM_DECODER_WRITE_ABORT;
717 /* dequeue the frame from the fifo */
718 for(channel = 0; channel < decoder->channels; channel++) {
719 for(l = 0, r = frame->header.blocksize; r < encoder_wrapper->verify_fifo.tail; l++, r++) {
720 encoder_wrapper->verify_fifo.original[channel][l] = encoder_wrapper->verify_fifo.original[channel][r];
723 encoder_wrapper->verify_fifo.tail -= frame->header.blocksize;
724 return FLAC__STREAM_DECODER_WRITE_CONTINUE;
727 void verify_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
734 void verify_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
738 fprintf(stderr, "\nERROR: verification decoder returned error %d:%s\n", status, FLAC__StreamDecoderErrorStatusString[status]);
741 void print_stats(const encoder_wrapper_struct *encoder_wrapper)
744 /* with VC++ you have to spoon feed it the casting */
745 double progress = (double)(int64)encoder_wrapper->samples_written / (double)(int64)encoder_wrapper->total_samples_to_encode;
747 double progress = (double)encoder_wrapper->samples_written / (double)encoder_wrapper->total_samples_to_encode;
749 printf("\r%0.2f%% complete: frame %u, wrote %u bytes, %u of %u samples, ratio = %5.3f",
750 progress * 100.0, encoder_wrapper->current_frame,
751 (unsigned)encoder_wrapper->bytes_written, (unsigned)encoder_wrapper->samples_written, (unsigned)encoder_wrapper->total_samples_to_encode,
753 /* with VC++ you have to spoon feed it the casting */
754 (double)(int64)encoder_wrapper->bytes_written / ((double)(int64)encoder_wrapper->unencoded_size * progress)
756 (double)encoder_wrapper->bytes_written / ((double)encoder_wrapper->unencoded_size * progress)
762 bool read_little_endian_uint16(FILE *f, uint16 *val, bool eof_ok)
764 size_t bytes_read = fread(val, 1, 2, f);
766 if(bytes_read == 0) {
768 fprintf(stderr, "ERROR: unexpected EOF\n");
774 else if(bytes_read < 2) {
775 fprintf(stderr, "ERROR: unexpected EOF\n");
779 if(is_big_endian_host) {
780 byte tmp, *b = (byte*)val;
781 tmp = b[1]; b[1] = b[0]; b[0] = tmp;
787 bool read_little_endian_uint32(FILE *f, uint32 *val, bool eof_ok)
789 size_t bytes_read = fread(val, 1, 4, f);
791 if(bytes_read == 0) {
793 fprintf(stderr, "ERROR: unexpected EOF\n");
799 else if(bytes_read < 4) {
800 fprintf(stderr, "ERROR: unexpected EOF\n");
804 if(is_big_endian_host) {
805 byte tmp, *b = (byte*)val;
806 tmp = b[3]; b[3] = b[0]; b[0] = tmp;
807 tmp = b[2]; b[2] = b[1]; b[1] = tmp;