#include "FLAC/all.h"
#include "encode.h"
+#ifdef min
+#undef min
+#endif
+#define min(x,y) ((x)<(y)?(x):(y))
+
#define CHUNK_OF_SAMPLES 2048
typedef enum {
unsigned size; /* of each original[] in samples */
unsigned tail; /* in wide samples */
const byte *encoded_signal;
+ unsigned encoded_signal_capacity;
unsigned encoded_bytes;
bool into_frames;
verify_code result;
static bool is_big_endian_host;
-static unsigned char ucbuffer[CHUNK_OF_SAMPLES*FLAC__MAX_CHANNELS*(FLAC__MAX_BITS_PER_SAMPLE>>3)];
+static unsigned char ucbuffer[CHUNK_OF_SAMPLES*FLAC__MAX_CHANNELS*((FLAC__MAX_BITS_PER_SAMPLE+7)/8)];
static signed char *scbuffer = (signed char *)ucbuffer;
static uint16 *usbuffer = (uint16 *)ucbuffer;
static int16 *ssbuffer = (int16 *)ucbuffer;
/* local routines */
static bool init(encoder_wrapper_struct *encoder_wrapper);
-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);
+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, encoder_wrapper_struct *encoder_wrapper);
static void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, encoder_wrapper_struct *encoder_wrapper);
static FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
static void metadata_callback(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data);
static FLAC__StreamDecoderReadStatus verify_read_callback(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data);
-static FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data);
+static FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data);
static void verify_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
static void verify_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
static void print_stats(const encoder_wrapper_struct *encoder_wrapper);
static bool read_little_endian_uint16(FILE *f, uint16 *val, bool eof_ok);
static bool read_little_endian_uint32(FILE *f, uint32 *val, bool eof_ok);
-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)
+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)
{
encoder_wrapper_struct encoder_wrapper;
FILE *fin;
bytes_per_wide_sample = channels * (bps >> 3);
if(skip > 0) {
- if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_CUR)) {
- fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
- goto wav_abort_;
+ if(fin != stdin) {
+ if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_CUR)) {
+ fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
+ goto wav_abort_;
+ }
+ }
+ else {
+ int64 left;
+ unsigned need;
+ for(left = (int64)skip; left > 0; left -= CHUNK_OF_SAMPLES) {
+ need = min(left, CHUNK_OF_SAMPLES);
+ if(fread(ucbuffer, 1, bytes_per_wide_sample * need, fin) < need) {
+ fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
+ goto wav_abort_;
+ }
+ }
}
}
encoder_wrapper.total_samples_to_encode = data_bytes / bytes_per_wide_sample - skip;
encoder_wrapper.unencoded_size = encoder_wrapper.total_samples_to_encode * bytes_per_wide_sample + 44; /* 44 for the size of the WAV headers */
- 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))
+ 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, &encoder_wrapper))
goto wav_abort_;
encoder_wrapper.verify_fifo.into_frames = true;
return 1;
}
-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)
+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, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps, unsigned sample_rate)
{
encoder_wrapper_struct encoder_wrapper;
FILE *fin;
}
}
- if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_SET)) {
- fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
- goto raw_abort_;
+ if(skip > 0) {
+ if(fin != stdin) {
+ if(-1 == fseek(fin, bytes_per_wide_sample * (unsigned)skip, SEEK_SET)) {
+ fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
+ goto raw_abort_;
+ }
+ }
+ else {
+ int64 left;
+ unsigned need;
+ for(left = (int64)skip; left > 0; left -= CHUNK_OF_SAMPLES) {
+ need = min(left, CHUNK_OF_SAMPLES);
+ if(fread(ucbuffer, 1, bytes_per_wide_sample * need, fin) < need) {
+ fprintf(stderr, "ERROR seeking while skipping samples in input file %s\n", infile);
+ goto raw_abort_;
+ }
+ }
+ }
+ }
+ else {
+ fseek(fin, 0, SEEK_SET);
}
- 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))
+ 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, &encoder_wrapper))
goto raw_abort_;
encoder_wrapper.verify_fifo.into_frames = true;
return true;
}
-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)
+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, encoder_wrapper_struct *encoder_wrapper)
{
- if(channels != 2 || bps > 16)
- do_mid_side = false;
+ if(channels != 2 /*@@@ not necessary? || bps > 16*/)
+ do_mid_side = loose_mid_side = false;
if(encoder_wrapper->verify) {
unsigned i;
encoder_wrapper->encoder->qlp_coeff_precision = qlp_coeff_precision;
encoder_wrapper->encoder->max_lpc_order = max_lpc_order;
encoder_wrapper->encoder->do_mid_side_stereo = do_mid_side;
+ encoder_wrapper->encoder->loose_mid_side_stereo = loose_mid_side;
encoder_wrapper->encoder->do_exhaustive_model_search = do_exhaustive_model_search;
encoder_wrapper->encoder->do_qlp_coeff_prec_search = do_qlp_coeff_prec_search;
encoder_wrapper->encoder->rice_optimization_level = rice_optimization_level;
encoder_wrapper->encoder->total_samples_estimate = encoder_wrapper->total_samples_to_encode;
+ encoder_wrapper->encoder->padding = padding;
if(FLAC__encoder_init(encoder_wrapper->encoder, write_callback, metadata_callback, encoder_wrapper) != FLAC__ENCODER_OK) {
fprintf(stderr, "ERROR initializing encoder, state = %d\n", encoder_wrapper->encoder->state);
if(is_unsigned_samples) {
for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
for(channel = 0; channel < channels; channel++, sample++)
- input[channel][wide_sample] = (int32)ucbuffer[sample] - 128;
+ input[channel][wide_sample] = (int32)ucbuffer[sample] - 0x80;
}
else {
for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
input[channel][wide_sample] = (int32)scbuffer[sample];
}
}
- else {
+ else if(bps == 16) {
if(is_big_endian != is_big_endian_host) {
unsigned char tmp;
const unsigned bytes = wide_samples * channels * (bps >> 3);
if(is_unsigned_samples) {
for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
for(channel = 0; channel < channels; channel++, sample++)
- input[channel][wide_sample] = (int32)usbuffer[sample] - 32768;
+ input[channel][wide_sample] = (int32)usbuffer[sample] - 0x8000;
}
else {
for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
input[channel][wide_sample] = (int32)ssbuffer[sample];
}
}
+ else if(bps == 24) {
+ if(!is_big_endian) {
+ unsigned char tmp;
+ const unsigned bytes = wide_samples * channels * (bps >> 3);
+ for(byte = 0; byte < bytes; byte += 3) {
+ tmp = ucbuffer[byte];
+ ucbuffer[byte] = ucbuffer[byte+2];
+ ucbuffer[byte+2] = tmp;
+ }
+ }
+ if(is_unsigned_samples) {
+ for(byte = sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
+ for(channel = 0; channel < channels; channel++, sample++) {
+ input[channel][wide_sample] = ucbuffer[byte++]; input[channel][wide_sample] <<= 8;
+ input[channel][wide_sample] |= ucbuffer[byte++]; input[channel][wide_sample] <<= 8;
+ input[channel][wide_sample] |= ucbuffer[byte++];
+ input[channel][wide_sample] -= 0x800000;
+ }
+ }
+ else {
+ for(byte = sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
+ for(channel = 0; channel < channels; channel++, sample++) {
+ input[channel][wide_sample] = scbuffer[byte++]; input[channel][wide_sample] <<= 8;
+ input[channel][wide_sample] |= ucbuffer[byte++]; input[channel][wide_sample] <<= 8;
+ input[channel][wide_sample] |= ucbuffer[byte++];
+ }
+ }
+ }
+ else {
+ assert(0);
+ }
if(encoder_wrapper->verify) {
for(channel = 0; channel < channels; channel++)
encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
byte b;
FILE *f;
- const uint64 samples = metadata->data.encoding.total_samples;
- const unsigned min_framesize = metadata->data.encoding.min_framesize;
- const unsigned max_framesize = metadata->data.encoding.max_framesize;
+ const uint64 samples = metadata->data.stream_info.total_samples;
+ const unsigned min_framesize = metadata->data.stream_info.min_framesize;
+ const unsigned max_framesize = metadata->data.stream_info.max_framesize;
(void)encoder; /* silence compiler warning about unused parameter */
*/
if(-1 == fseek(f, 26, SEEK_SET)) goto samples_;
- fwrite(metadata->data.encoding.md5sum, 1, 16, f);
+ fwrite(metadata->data.stream_info.md5sum, 1, 16, f);
samples_:
if(-1 == fseek(f, 21, SEEK_SET)) goto framesize_;
b = (byte)(max_framesize & 0xFF);
if(fwrite(&b, 1, 1, f) != 1) goto end_;
end_:
- fclose(encoder_wrapper->fout);
+ fclose(f);
return;
}
FLAC__StreamDecoderReadStatus verify_read_callback(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data)
{
encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
+ const unsigned encoded_bytes = encoder_wrapper->verify_fifo.encoded_bytes;
(void)decoder;
- *bytes = encoder_wrapper->verify_fifo.encoded_bytes;
- memcpy(buffer, encoder_wrapper->verify_fifo.encoded_signal, *bytes);
+ if(encoded_bytes <= *bytes) {
+ *bytes = encoded_bytes;
+ memcpy(buffer, encoder_wrapper->verify_fifo.encoded_signal, *bytes);
+ }
+ else {
+ memcpy(buffer, encoder_wrapper->verify_fifo.encoded_signal, *bytes);
+ encoder_wrapper->verify_fifo.encoded_signal += *bytes;
+ encoder_wrapper->verify_fifo.encoded_bytes -= *bytes;
+ }
return FLAC__STREAM_DECODER_READ_CONTINUE;
}
-FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data)
+FLAC__StreamDecoderWriteStatus verify_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const int32 *buffer[], void *client_data)
{
encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
unsigned channel, l, r;
if(0 != memcmp(buffer[channel], encoder_wrapper->verify_fifo.original[channel], sizeof(int32) * decoder->blocksize)) {
fprintf(stderr, "\nERROR: mismatch in decoded data, verify FAILED!\n");
fprintf(stderr, " Please submit a bug report to http://sourceforge.net/bugs/?func=addbug&group_id=13478\n");
+for(l=0;l<decoder->blocksize;l++)
+if(buffer[channel][l]!=encoder_wrapper->verify_fifo.original[channel][l])break;
+fprintf(stderr,"@@@channel=%u, sample=%u, expected %08x, got %08x\n",channel,l,buffer[channel][l],encoder_wrapper->verify_fifo.original[channel][l]);
return FLAC__STREAM_DECODER_WRITE_ABORT;
}
}
/* dequeue the frame from the fifo */
for(channel = 0; channel < decoder->channels; channel++) {
- for(l = 0, r = header->blocksize; r < encoder_wrapper->verify_fifo.tail; l++, r++) {
+ for(l = 0, r = frame->header.blocksize; r < encoder_wrapper->verify_fifo.tail; l++, r++) {
encoder_wrapper->verify_fifo.original[channel][l] = encoder_wrapper->verify_fifo.original[channel][r];
}
}
- encoder_wrapper->verify_fifo.tail -= header->blocksize;
+ encoder_wrapper->verify_fifo.tail -= frame->header.blocksize;
return FLAC__STREAM_DECODER_WRITE_CONTINUE;
}