1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2000 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.
20 #if defined _WIN32 && !defined __CYGWIN__
21 /* where MSVC puts unlink() */
26 #include <stdio.h> /* for FILE */
27 #include <string.h> /* for strcmp() */
31 #define CHUNK_OF_SAMPLES 2048
36 FLAC__Encoder *encoder;
38 uint64 unencoded_size;
39 uint64 total_samples_to_encode;
41 uint64 samples_written;
42 unsigned current_frame;
43 } encoder_wrapper_struct;
45 static bool is_big_endian_host;
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;
52 static int32 in[FLAC__MAX_CHANNELS][CHUNK_OF_SAMPLES];
53 static int32 *input[FLAC__MAX_CHANNELS];
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);
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)
67 encoder_wrapper_struct encoder_wrapper;
69 bool is_unsigned_samples;
70 unsigned channels, bps, sample_rate, data_bytes;
71 size_t bytes_per_wide_sample, bytes_read;
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;
81 if(0 == strcmp(infile, "-")) {
85 if(0 == (fin = fopen(infile, "rb"))) {
86 fprintf(stderr, "ERROR: can't open input file %s\n", infile);
90 if(0 == strcmp(outfile, "-")) {
91 encoder_wrapper.fout = stdout;
94 if(0 == (encoder_wrapper.fout = fopen(outfile, "wb"))) {
95 fprintf(stderr, "ERROR: can't open output file %s\n", outfile);
101 if(!init(&encoder_wrapper))
105 * check the RIFF chunk
107 if(!read_little_endian_uint32(fin, &xx, false))
109 if(xx != 0x46464952) { /* "RIFF" */
110 fprintf(stderr, "ERROR: no RIFF header\n");
113 if(!read_little_endian_uint32(fin, &xx, false))
117 * now process the WAVE chunk
119 if(!read_little_endian_uint32(fin, &xx, true))
121 if(xx != 0x45564157) { /* "WAVE" */
122 fprintf(stderr, "ERROR: no WAVE header\n");
126 /* do the format sub-chunk */
127 if(!read_little_endian_uint32(fin, &xx, false))
129 if(xx != 0x20746d66) { /* "fmt " */
130 fprintf(stderr, "ERROR: no format sub-chunk\n");
134 if(!read_little_endian_uint32(fin, &xx, false))
137 fprintf(stderr, "ERROR: unsupported chunk\n");
140 /* compression code */
141 if(!read_little_endian_uint16(fin, &x, false))
144 fprintf(stderr, "ERROR: unsupported compression type %u\n", (unsigned)x);
147 /* number of channels */
148 if(!read_little_endian_uint16(fin, &x, false))
150 if(x == 0 || x > FLAC__MAX_CHANNELS) {
151 fprintf(stderr, "ERROR: unsupported number channels %u\n", (unsigned)x);
156 if(!read_little_endian_uint32(fin, &xx, false))
158 if(xx == 0 || xx > FLAC__MAX_SAMPLE_RATE) {
159 fprintf(stderr, "ERROR: unsupported sample rate %u\n", (unsigned)xx);
163 /* avg bytes per second (ignored) */
164 if(!read_little_endian_uint32(fin, &xx, false))
166 /* block align (ignored) */
167 if(!read_little_endian_uint16(fin, &x, false))
169 /* bits per sample */
170 if(!read_little_endian_uint16(fin, &x, false))
172 if(x != 8 && x != 16) {
173 fprintf(stderr, "ERROR: unsupported bits per sample %u\n", (unsigned)x);
177 is_unsigned_samples = (x == 8);
179 /* do the data sub-chunk */
180 if(!read_little_endian_uint32(fin, &xx, false))
182 if(xx != 0x61746164) { /* "data" */
183 fprintf(stderr, "ERROR: no data sub-chunk\n");
187 if(!read_little_endian_uint32(fin, &xx, false))
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))
194 bytes_per_wide_sample = channels * (bps >> 3);
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);
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 */
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) {
208 fprintf(stderr, "ERROR reading from %s\n", infile);
214 else if(bytes_read % bytes_per_wide_sample != 0) {
215 fprintf(stderr, "ERROR, got partial sample from input file %s\n", infile);
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);
225 data_bytes -= bytes_read;
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);
235 if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0) {
236 print_stats(&encoder_wrapper);
242 if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0)
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);
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)
256 encoder_wrapper_struct encoder_wrapper;
259 const size_t bytes_per_wide_sample = channels * (bps >> 3);
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;
267 if(0 == strcmp(infile, "-")) {
271 if(0 == (fin = fopen(infile, "rb"))) {
272 fprintf(stderr, "ERROR: can't open input file %s\n", infile);
276 if(0 == strcmp(outfile, "-")) {
277 encoder_wrapper.fout = stdout;
280 if(0 == (encoder_wrapper.fout = fopen(outfile, "wb"))) {
281 fprintf(stderr, "ERROR: can't open output file %s\n", outfile);
287 if(!init(&encoder_wrapper))
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))
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;
300 if(-1 == (filesize = ftell(fin))) {
301 encoder_wrapper.total_samples_to_encode = encoder_wrapper.unencoded_size = 0;
304 encoder_wrapper.unencoded_size = filesize - skip * bytes_per_wide_sample;
305 encoder_wrapper.total_samples_to_encode = filesize / bytes_per_wide_sample - skip;
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);
315 bytes_read = fread(ucbuffer, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample, fin);
316 if(bytes_read == 0) {
318 fprintf(stderr, "ERROR reading from %s\n", infile);
322 else if(bytes_read % bytes_per_wide_sample != 0) {
323 fprintf(stderr, "ERROR, got partial sample from input file %s\n", infile);
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);
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);
341 if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0) {
342 print_stats(&encoder_wrapper);
348 if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode > 0)
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);
360 bool init(encoder_wrapper_struct *encoder_wrapper)
365 is_big_endian_host = (*((byte*)(&test)))? false : true;
367 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
368 input[i] = &(in[i][0]);
370 encoder_wrapper->encoder = FLAC__encoder_get_new_instance();
371 if(0 == encoder_wrapper->encoder) {
372 fprintf(stderr, "ERROR creating the encoder instance\n");
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)
381 if(channels != 2 || bps > 16)
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;
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);
404 void format_input(unsigned wide_samples, bool is_big_endian, bool is_unsigned_samples, unsigned channels, unsigned bps)
406 unsigned wide_sample, sample, channel, byte;
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;
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];
421 if(is_big_endian != is_big_endian_host) {
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;
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;
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];
443 FLAC__EncoderWriteStatus write_callback(const FLAC__Encoder *encoder, const byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
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;
448 encoder_wrapper->bytes_written += bytes;
449 encoder_wrapper->samples_written += samples;
450 encoder_wrapper->current_frame = current_frame;
452 if(samples && encoder_wrapper->verbose && encoder_wrapper->total_samples_to_encode > 0 && !(current_frame & mask))
453 print_stats(encoder_wrapper);
455 if(fwrite(buffer, sizeof(byte), bytes, encoder_wrapper->fout) == bytes)
456 return FLAC__ENCODER_WRITE_OK;
458 return FLAC__ENCODER_WRITE_FATAL_ERROR;
461 void metadata_callback(const FLAC__Encoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data)
463 encoder_wrapper_struct *encoder_wrapper = (encoder_wrapper_struct *)client_data;
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;
470 (void)encoder; /* silence compiler warning about unused parameter */
472 if(encoder_wrapper->fout == stdout)
475 fclose(encoder_wrapper->fout);
476 if(0 == (f = fopen(encoder_wrapper->outfile, "r+b")))
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.
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_;
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_;
513 fclose(encoder_wrapper->fout);
517 void print_stats(const encoder_wrapper_struct *encoder_wrapper)
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;
523 double progress = (double)encoder_wrapper->samples_written / (double)encoder_wrapper->total_samples_to_encode;
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,
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)
532 (double)encoder_wrapper->bytes_written / ((double)encoder_wrapper->unencoded_size * progress)
538 bool read_little_endian_uint16(FILE *f, uint16 *val, bool eof_ok)
540 size_t bytes_read = fread(val, 1, 2, f);
542 if(bytes_read == 0) {
544 fprintf(stderr, "ERROR: unexpected EOF\n");
550 else if(bytes_read < 2) {
551 fprintf(stderr, "ERROR: unexpected EOF\n");
555 if(is_big_endian_host) {
556 byte tmp, *b = (byte*)val;
557 tmp = b[1]; b[1] = b[0]; b[0] = tmp;
563 bool read_little_endian_uint32(FILE *f, uint32 *val, bool eof_ok)
565 size_t bytes_read = fread(val, 1, 4, f);
567 if(bytes_read == 0) {
569 fprintf(stderr, "ERROR: unexpected EOF\n");
575 else if(bytes_read < 4) {
576 fprintf(stderr, "ERROR: unexpected EOF\n");
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;