1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2000,2001,2002 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 #if defined _WIN32 && !defined __CYGWIN__
20 /* where MSVC puts unlink() */
25 #include <limits.h> /* for LONG_MAX */
26 #include <math.h> /* for floor() */
28 #include <stdio.h> /* for FILE etc. */
29 #include <stdlib.h> /* for malloc */
30 #include <string.h> /* for strcmp() */
35 #include "OggFLAC/stream_encoder.h"
41 #define min(x,y) ((x)<(y)?(x):(y))
43 /* this MUST be >= 588 so that sector aligning can take place with one read */
44 #define CHUNK_OF_SAMPLES 2048
53 const char *inbasefilename;
54 const char *outfilename;
56 FLAC__uint64 unencoded_size;
57 FLAC__uint64 total_samples_to_encode;
58 FLAC__uint64 bytes_written;
59 FLAC__uint64 samples_written;
64 * We use flac.stream for encoding native FLAC to stdout
65 * We use flac.file for encoding native FLAC to a regular file
66 * We use ogg.stream for encoding Ogg FLAC to either stdout or a regular file
70 FLAC__StreamEncoder *stream;
71 FLAC__FileEncoder *file;
75 OggFLAC__StreamEncoder *stream;
82 FLAC__StreamMetadata *seek_table_template;
86 static FLAC__bool is_big_endian_host_;
88 static unsigned char ucbuffer_[CHUNK_OF_SAMPLES*FLAC__MAX_CHANNELS*((FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE+7)/8)];
89 static signed char *scbuffer_ = (signed char *)ucbuffer_;
90 static FLAC__uint16 *usbuffer_ = (FLAC__uint16 *)ucbuffer_;
91 static FLAC__int16 *ssbuffer_ = (FLAC__int16 *)ucbuffer_;
93 static FLAC__int32 in_[FLAC__MAX_CHANNELS][CHUNK_OF_SAMPLES];
94 static FLAC__int32 *input_[FLAC__MAX_CHANNELS];
98 * unpublished debug routines from the FLAC libs
100 extern FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value);
101 extern FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value);
102 extern FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value);
103 extern FLAC__bool FLAC__file_encoder_disable_constant_subframes(FLAC__FileEncoder *encoder, FLAC__bool value);
104 extern FLAC__bool FLAC__file_encoder_disable_fixed_subframes(FLAC__FileEncoder *encoder, FLAC__bool value);
105 extern FLAC__bool FLAC__file_encoder_disable_verbatim_subframes(FLAC__FileEncoder *encoder, FLAC__bool value);
106 extern FLAC__bool OggFLAC__stream_encoder_disable_constant_subframes(OggFLAC__StreamEncoder *encoder, FLAC__bool value);
107 extern FLAC__bool OggFLAC__stream_encoder_disable_fixed_subframes(OggFLAC__StreamEncoder *encoder, FLAC__bool value);
108 extern FLAC__bool OggFLAC__stream_encoder_disable_verbatim_subframes(OggFLAC__StreamEncoder *encoder, FLAC__bool value);
113 static FLAC__bool EncoderSession_construct(EncoderSession *e, FLAC__bool use_ogg, FLAC__bool verify, FLAC__bool verbose, FILE *infile, const char *infilename, const char *outfilename);
114 static void EncoderSession_destroy(EncoderSession *e);
115 static int EncoderSession_finish_ok(EncoderSession *e, int info_align_carry, int info_align_zero);
116 static int EncoderSession_finish_error(EncoderSession *e);
117 static FLAC__bool EncoderSession_init_encoder(EncoderSession *e, encode_options_t options, unsigned channels, unsigned bps, unsigned sample_rate);
118 static FLAC__bool EncoderSession_process(EncoderSession *e, const FLAC__int32 * const buffer[], unsigned samples);
119 static FLAC__bool convert_to_seek_table_template(char *requested_seek_points, int num_requested_seek_points, EncoderSession *e);
120 static void format_input(FLAC__int32 *dest[], unsigned wide_samples, FLAC__bool is_big_endian, FLAC__bool is_unsigned_samples, unsigned channels, unsigned bps);
122 static FLAC__StreamEncoderWriteStatus ogg_stream_encoder_write_callback(const OggFLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
124 static FLAC__StreamEncoderWriteStatus flac_stream_encoder_write_callback(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
125 static void flac_stream_encoder_metadata_callback(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data);
126 static void flac_file_encoder_progress_callback(const FLAC__FileEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data);
127 static void print_stats(const EncoderSession *encoder_session);
128 static void print_error_with_state(const EncoderSession *e, const char *message);
129 static void print_verify_error(EncoderSession *e);
130 static FLAC__bool read_little_endian_uint16(FILE *f, FLAC__uint16 *val, FLAC__bool eof_ok, const char *fn);
131 static FLAC__bool read_little_endian_uint32(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn);
132 static FLAC__bool read_big_endian_uint16(FILE *f, FLAC__uint16 *val, FLAC__bool eof_ok, const char *fn);
133 static FLAC__bool read_big_endian_uint32(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn);
134 static FLAC__bool read_sane_extended(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn);
140 flac__encode_aif(FILE *infile, long infilesize, const char *infilename, const char *outfilename,
141 const FLAC__byte *lookahead, unsigned lookahead_length, wav_encode_options_t options)
143 EncoderSession encoder_session;
146 unsigned int channels= 0U, bps= 0U, sample_rate= 0U, sample_frames= 0U;
147 FLAC__bool got_comm_chunk= false, got_ssnd_chunk= false;
148 int info_align_carry= -1, info_align_zero= -1;
150 FLAC__ASSERT(!options.common.sector_align || options.common.skip == 0);
152 (void)infilesize; /* silence compiler warning about unused parameter */
153 (void)lookahead; /* silence compiler warning about unused parameter */
154 (void)lookahead_length; /* silence compiler warning about unused parameter */
157 EncoderSession_construct(
160 options.common.use_ogg,
164 options.common.verify,
165 options.common.verbose,
173 /* lookahead[] already has "FORMxxxxAIFF", do sub-chunks */
179 /* chunk identifier; really conservative about behavior of fread() and feof() */
180 if(feof(infile) || ((c= fread(chunk_id, 1U, 4U, infile)), c==0U && feof(infile)))
182 else if(c<4U || feof(infile)) {
183 fprintf(stderr, "%s: ERROR: incomplete chunk identifier\n", encoder_session.inbasefilename);
184 return EncoderSession_finish_error(&encoder_session);
187 if(got_comm_chunk==false && !strncmp(chunk_id, "COMM", 4)) { /* common chunk */
190 /* COMM chunk size */
191 if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
192 return EncoderSession_finish_error(&encoder_session);
194 fprintf(stderr, "%s: ERROR: non-standard 'COMM' chunk has length = %u\n", encoder_session.inbasefilename, (unsigned int)xx);
195 return EncoderSession_finish_error(&encoder_session);
198 fprintf(stderr, "%s: WARNING: non-standard 'COMM' chunk has length = %u\n", encoder_session.inbasefilename, (unsigned int)xx);
199 skip= (xx-18U)+(xx & 1U);
201 /* number of channels */
202 if(!read_big_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
203 return EncoderSession_finish_error(&encoder_session);
204 else if(x==0U || x>FLAC__MAX_CHANNELS) {
205 fprintf(stderr, "%s: ERROR: unsupported number channels %u\n", encoder_session.inbasefilename, (unsigned int)x);
206 return EncoderSession_finish_error(&encoder_session);
208 else if(options.common.sector_align && x!=2U) {
209 fprintf(stderr, "%s: ERROR: file has %u channels, must be 2 for --sector-align\n", encoder_session.inbasefilename, (unsigned int)x);
210 return EncoderSession_finish_error(&encoder_session);
214 /* number of sample frames */
215 if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
216 return EncoderSession_finish_error(&encoder_session);
219 /* bits per sample */
220 if(!read_big_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
221 return EncoderSession_finish_error(&encoder_session);
222 else if(x!=8U && x!=16U && x!=24U) {
223 fprintf(stderr, "%s: ERROR: unsupported bits per sample %u\n", encoder_session.inbasefilename, (unsigned int)x);
224 return EncoderSession_finish_error(&encoder_session);
226 else if(options.common.sector_align && x!=16U) {
227 fprintf(stderr, "%s: ERROR: file has %u bits per sample, must be 16 for --sector-align\n", encoder_session.inbasefilename, (unsigned int)x);
228 return EncoderSession_finish_error(&encoder_session);
233 if(!read_sane_extended(infile, &xx, false, encoder_session.inbasefilename))
234 return EncoderSession_finish_error(&encoder_session);
235 else if(!FLAC__format_sample_rate_is_valid(xx)) {
236 fprintf(stderr, "%s: ERROR: unsupported sample rate %u\n", encoder_session.inbasefilename, (unsigned int)xx);
237 return EncoderSession_finish_error(&encoder_session);
239 else if(options.common.sector_align && xx!=44100U) {
240 fprintf(stderr, "%s: ERROR: file's sample rate is %u, must be 44100 for --sector-align\n", encoder_session.inbasefilename, (unsigned int)xx);
241 return EncoderSession_finish_error(&encoder_session);
245 /* skip any extra data in the COMM chunk */
246 FLAC__ASSERT(skip<=LONG_MAX);
247 while(skip>0U && fseek(infile, skip, SEEK_CUR)<0) {
248 unsigned int need= min(skip, sizeof ucbuffer_);
249 if(fread(ucbuffer_, 1U, need, infile)<need) {
250 fprintf(stderr, "%s: ERROR during read while skipping extra COMM data\n", encoder_session.inbasefilename);
251 return EncoderSession_finish_error(&encoder_session);
256 got_comm_chunk= true;
258 else if(got_ssnd_chunk==false && !strncmp(chunk_id, "SSND", 4)) { /* sound data chunk */
259 unsigned int offset= 0U, block_size= 0U, align_remainder= 0U, data_bytes;
260 size_t bytes_per_frame= channels*(bps>>3);
261 FLAC__bool pad= false;
263 if(got_comm_chunk==false) {
264 fprintf(stderr, "%s: ERROR: got 'SSND' chunk before 'COMM' chunk\n", encoder_session.inbasefilename);
265 return EncoderSession_finish_error(&encoder_session);
268 /* SSND chunk size */
269 if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
270 return EncoderSession_finish_error(&encoder_session);
271 else if(xx!=(sample_frames*bytes_per_frame + 8U)) {
272 fprintf(stderr, "%s: ERROR: SSND chunk size inconsistent with sample frame count\n", encoder_session.inbasefilename);
273 return EncoderSession_finish_error(&encoder_session);
276 pad= (data_bytes & 1U) ? true : false;
279 if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
280 return EncoderSession_finish_error(&encoder_session);
282 fprintf(stderr, "%s: ERROR: offset is %u; must be 0\n", encoder_session.inbasefilename, (unsigned int)xx);
283 return EncoderSession_finish_error(&encoder_session);
288 if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
289 return EncoderSession_finish_error(&encoder_session);
291 fprintf(stderr, "%s: ERROR: block size is %u; must be 0\n", encoder_session.inbasefilename, (unsigned int)xx);
292 return EncoderSession_finish_error(&encoder_session);
296 if(options.common.skip>0U) {
297 FLAC__uint64 remaining= options.common.skip*bytes_per_frame;
299 /* do 1<<30 bytes at a time, since 1<<30 is a nice round number, and */
300 /* is guaranteed to be less than LONG_MAX */
301 for(; remaining>0U; remaining-= remaining>(1U<<30) ? remaining : (1U<<30))
303 unsigned long skip= (unsigned long)(remaining % (1U<<30));
305 FLAC__ASSERT(skip<=LONG_MAX);
306 while(skip>0 && fseek(infile, skip, SEEK_CUR)<0) {
307 unsigned int need= min(skip, sizeof ucbuffer_);
308 if(fread(ucbuffer_, 1U, need, infile)<need) {
309 fprintf(stderr, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
310 return EncoderSession_finish_error(&encoder_session);
317 data_bytes-= (8U + (unsigned int)options.common.skip*bytes_per_frame); /*@@@ WATCHOUT: 4GB limit */
318 encoder_session.total_samples_to_encode= data_bytes/bytes_per_frame + *options.common.align_reservoir_samples;
319 if(options.common.sector_align) {
320 align_remainder= (unsigned int)(encoder_session.total_samples_to_encode % 588U);
321 if(options.common.is_last_file)
322 encoder_session.total_samples_to_encode+= (588U-align_remainder); /* will pad with zeroes */
324 encoder_session.total_samples_to_encode-= align_remainder; /* will stop short and carry over to next file */
327 /* +54 for the size of the AIFF headers; this is just an estimate for the progress indicator and doesn't need to be exact */
328 encoder_session.unencoded_size= encoder_session.total_samples_to_encode*bytes_per_frame+54;
330 if(!EncoderSession_init_encoder(&encoder_session, options.common, channels, bps, sample_rate))
331 return EncoderSession_finish_error(&encoder_session);
333 /* first do any samples in the reservoir */
334 if(options.common.sector_align && *options.common.align_reservoir_samples>0U) {
336 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 *const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) {
337 print_error_with_state(&encoder_session, "ERROR during encoding");
338 return EncoderSession_finish_error(&encoder_session);
342 /* decrement the data_bytes counter if we need to align the file */
343 if(options.common.sector_align) {
344 if(options.common.is_last_file)
345 *options.common.align_reservoir_samples= 0U;
347 *options.common.align_reservoir_samples= align_remainder;
348 data_bytes-= (*options.common.align_reservoir_samples)*bytes_per_frame;
352 /* now do from the file */
353 while(data_bytes>0) {
354 size_t bytes_read= fread(ucbuffer_, 1U, min(data_bytes, CHUNK_OF_SAMPLES*bytes_per_frame), infile);
358 fprintf(stderr, "%s: ERROR during read\n", encoder_session.inbasefilename);
359 return EncoderSession_finish_error(&encoder_session);
361 else if(feof(infile)) {
362 fprintf(stderr, "%s: WARNING: unexpected EOF; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned int)encoder_session.total_samples_to_encode, (unsigned int)encoder_session.samples_written);
367 if(bytes_read % bytes_per_frame != 0U) {
368 fprintf(stderr, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
369 return EncoderSession_finish_error(&encoder_session);
372 unsigned int frames= bytes_read/bytes_per_frame;
373 format_input(input_, frames, true, false, channels, bps);
375 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 *const *)input_, frames)) {
376 print_error_with_state(&encoder_session, "ERROR during encoding");
377 return EncoderSession_finish_error(&encoder_session);
380 data_bytes-= bytes_read;
385 /* now read unaligned samples into reservoir or pad with zeroes if necessary */
386 if(options.common.sector_align) {
387 if(options.common.is_last_file) {
388 unsigned int pad_frames= 588U-align_remainder;
390 if(pad_frames<588U) {
393 info_align_zero= pad_frames;
394 for(i= 0U; i<channels; ++i)
395 memset(input_[i], 0, pad_frames*(bps>>3));
397 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 *const *)input_, pad_frames)) {
398 print_error_with_state(&encoder_session, "ERROR during encoding");
399 return EncoderSession_finish_error(&encoder_session);
404 if(*options.common.align_reservoir_samples > 0) {
405 size_t bytes_read= fread(ucbuffer_, 1U, (*options.common.align_reservoir_samples)*bytes_per_frame, infile);
407 FLAC__ASSERT(CHUNK_OF_SAMPLES>=588U);
408 if(bytes_read==0U && ferror(infile)) {
409 fprintf(stderr, "%s: ERROR during read\n", encoder_session.inbasefilename);
410 return EncoderSession_finish_error(&encoder_session);
412 else if(bytes_read != (*options.common.align_reservoir_samples) * bytes_per_frame)
413 fprintf(stderr, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned int)bytes_read, (unsigned int)encoder_session.total_samples_to_encode, (unsigned int)encoder_session.samples_written);
415 info_align_carry= *options.common.align_reservoir_samples;
416 format_input(options.common.align_reservoir, *options.common.align_reservoir_samples, true, false, channels, bps);
425 if(fread(&tmp, 1U, 1U, infile)<1U) {
426 fprintf(stderr, "%s: ERROR during read of SSND pad byte\n", encoder_session.inbasefilename);
427 return EncoderSession_finish_error(&encoder_session);
431 got_ssnd_chunk= true;
433 else { /* other chunk */
434 if(!strncmp(chunk_id, "COMM", 4))
435 fprintf(stderr, "%s: WARNING: skipping extra 'COMM' chunk\n", encoder_session.inbasefilename);
436 else if(!strncmp(chunk_id, "SSND", 4))
437 fprintf(stderr, "%s: WARNING: skipping extra 'SSND' chunk\n", encoder_session.inbasefilename);
439 fprintf(stderr, "%s: WARNING: skipping unknown chunk '%s'\n", encoder_session.inbasefilename, chunk_id);
442 if(!read_big_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
443 return EncoderSession_finish_error(&encoder_session);
445 unsigned long skip= xx+(xx & 1U);
447 FLAC__ASSERT(skip<=LONG_MAX);
448 while(skip>0U && fseek(infile, skip, SEEK_CUR)<0) {
449 unsigned int need= min(skip, sizeof ucbuffer_);
450 if(fread(ucbuffer_, 1U, need, infile)<need) {
451 fprintf(stderr, "%s: ERROR during read while skipping unknown chunk\n", encoder_session.inbasefilename);
452 return EncoderSession_finish_error(&encoder_session);
460 if(got_ssnd_chunk==false && sample_frames!=0U) {
461 fprintf(stderr, "%s: ERROR: missing SSND chunk\n", encoder_session.inbasefilename);
462 return EncoderSession_finish_error(&encoder_session);
465 return EncoderSession_finish_ok(&encoder_session, info_align_carry, info_align_zero);
468 int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, wav_encode_options_t options)
470 EncoderSession encoder_session;
471 FLAC__bool is_unsigned_samples = false;
472 unsigned channels = 0, bps = 0, sample_rate = 0, data_bytes;
473 size_t bytes_per_wide_sample, bytes_read;
476 FLAC__bool got_fmt_chunk = false, got_data_chunk = false;
477 unsigned align_remainder = 0;
478 int info_align_carry = -1, info_align_zero = -1;
480 FLAC__ASSERT(!options.common.sector_align || options.common.skip == 0);
484 (void)lookahead_length;
487 EncoderSession_construct(
490 options.common.use_ogg,
494 options.common.verify,
495 options.common.verbose,
504 * lookahead[] already has "RIFFxxxxWAVE", do sub-chunks
506 while(!feof(infile)) {
507 if(!read_little_endian_uint32(infile, &xx, true, encoder_session.inbasefilename))
508 return EncoderSession_finish_error(&encoder_session);
511 if(xx == 0x20746d66 && !got_fmt_chunk) { /* "fmt " */
512 /* fmt sub-chunk size */
513 if(!read_little_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
514 return EncoderSession_finish_error(&encoder_session);
516 fprintf(stderr, "%s: ERROR: found non-standard 'fmt ' sub-chunk which has length = %u\n", encoder_session.inbasefilename, (unsigned)xx);
517 return EncoderSession_finish_error(&encoder_session);
519 else if(xx != 16 && xx != 18) {
520 fprintf(stderr, "%s: WARNING: found non-standard 'fmt ' sub-chunk which has length = %u\n", encoder_session.inbasefilename, (unsigned)xx);
523 /* compression code */
524 if(!read_little_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
525 return EncoderSession_finish_error(&encoder_session);
527 fprintf(stderr, "%s: ERROR: unsupported compression type %u\n", encoder_session.inbasefilename, (unsigned)x);
528 return EncoderSession_finish_error(&encoder_session);
530 /* number of channels */
531 if(!read_little_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
532 return EncoderSession_finish_error(&encoder_session);
533 if(x == 0 || x > FLAC__MAX_CHANNELS) {
534 fprintf(stderr, "%s: ERROR: unsupported number channels %u\n", encoder_session.inbasefilename, (unsigned)x);
535 return EncoderSession_finish_error(&encoder_session);
537 else if(options.common.sector_align && x != 2) {
538 fprintf(stderr, "%s: ERROR: file has %u channels, must be 2 for --sector-align\n", encoder_session.inbasefilename, (unsigned)x);
539 return EncoderSession_finish_error(&encoder_session);
543 if(!read_little_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
544 return EncoderSession_finish_error(&encoder_session);
545 if(!FLAC__format_sample_rate_is_valid(xx)) {
546 fprintf(stderr, "%s: ERROR: unsupported sample rate %u\n", encoder_session.inbasefilename, (unsigned)xx);
547 return EncoderSession_finish_error(&encoder_session);
549 else if(options.common.sector_align && xx != 44100) {
550 fprintf(stderr, "%s: ERROR: file's sample rate is %u, must be 44100 for --sector-align\n", encoder_session.inbasefilename, (unsigned)xx);
551 return EncoderSession_finish_error(&encoder_session);
554 /* avg bytes per second (ignored) */
555 if(!read_little_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
556 return EncoderSession_finish_error(&encoder_session);
557 /* block align (ignored) */
558 if(!read_little_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
559 return EncoderSession_finish_error(&encoder_session);
560 /* bits per sample */
561 if(!read_little_endian_uint16(infile, &x, false, encoder_session.inbasefilename))
562 return EncoderSession_finish_error(&encoder_session);
563 if(x != 8 && x != 16 && x != 24) {
564 fprintf(stderr, "%s: ERROR: unsupported bits per sample %u\n", encoder_session.inbasefilename, (unsigned)x);
565 return EncoderSession_finish_error(&encoder_session);
567 else if(options.common.sector_align && x != 16) {
568 fprintf(stderr, "%s: ERROR: file has %u bits per sample, must be 16 for --sector-align\n", encoder_session.inbasefilename, (unsigned)x);
569 return EncoderSession_finish_error(&encoder_session);
572 is_unsigned_samples = (x == 8);
574 /* skip any extra data in the fmt sub-chunk */
578 for(left = data_bytes; left > 0; ) {
579 need = min(left, CHUNK_OF_SAMPLES);
580 if(fread(ucbuffer_, 1U, need, infile) < need) {
581 fprintf(stderr, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
582 return EncoderSession_finish_error(&encoder_session);
588 got_fmt_chunk = true;
590 else if(xx == 0x61746164 && !got_data_chunk && got_fmt_chunk) { /* "data" */
592 if(!read_little_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
593 return EncoderSession_finish_error(&encoder_session);
596 bytes_per_wide_sample = channels * (bps >> 3);
598 if(options.common.skip > 0) {
599 if(fseek(infile, bytes_per_wide_sample * (unsigned)options.common.skip, SEEK_CUR) < 0) {
600 /* can't seek input, read ahead manually... */
602 for(left = (unsigned)options.common.skip; left > 0; ) { /*@@@ WATCHOUT: 4GB limit */
603 need = min(left, CHUNK_OF_SAMPLES);
604 if(fread(ucbuffer_, bytes_per_wide_sample, need, infile) < need) {
605 fprintf(stderr, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
606 return EncoderSession_finish_error(&encoder_session);
613 data_bytes -= (unsigned)options.common.skip * bytes_per_wide_sample; /*@@@ WATCHOUT: 4GB limit */
614 encoder_session.total_samples_to_encode = data_bytes / bytes_per_wide_sample + *options.common.align_reservoir_samples;
615 if(options.common.sector_align) {
616 align_remainder = (unsigned)(encoder_session.total_samples_to_encode % 588);
617 if(options.common.is_last_file)
618 encoder_session.total_samples_to_encode += (588-align_remainder); /* will pad with zeroes */
620 encoder_session.total_samples_to_encode -= align_remainder; /* will stop short and carry over to next file */
623 /* +44 for the size of the WAV headers; this is just an estimate for the progress indicator and doesn't need to be exact */
624 encoder_session.unencoded_size = encoder_session.total_samples_to_encode * bytes_per_wide_sample + 44;
626 if(!EncoderSession_init_encoder(&encoder_session, options.common, channels, bps, sample_rate))
627 return EncoderSession_finish_error(&encoder_session);
630 * first do any samples in the reservoir
632 if(options.common.sector_align && *options.common.align_reservoir_samples > 0) {
633 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) {
634 print_error_with_state(&encoder_session, "ERROR during encoding");
635 return EncoderSession_finish_error(&encoder_session);
640 * decrement the data_bytes counter if we need to align the file
642 if(options.common.sector_align) {
643 if(options.common.is_last_file) {
644 *options.common.align_reservoir_samples = 0;
647 *options.common.align_reservoir_samples = align_remainder;
648 data_bytes -= (*options.common.align_reservoir_samples) * bytes_per_wide_sample;
653 * now do from the file
655 while(data_bytes > 0) {
656 bytes_read = fread(ucbuffer_, sizeof(unsigned char), min(data_bytes, CHUNK_OF_SAMPLES * bytes_per_wide_sample), infile);
657 if(bytes_read == 0) {
659 fprintf(stderr, "%s: ERROR during read\n", encoder_session.inbasefilename);
660 return EncoderSession_finish_error(&encoder_session);
662 else if(feof(infile)) {
663 fprintf(stderr, "%s: WARNING: unexpected EOF; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
668 if(bytes_read % bytes_per_wide_sample != 0) {
669 fprintf(stderr, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
670 return EncoderSession_finish_error(&encoder_session);
673 unsigned wide_samples = bytes_read / bytes_per_wide_sample;
674 format_input(input_, wide_samples, false, is_unsigned_samples, channels, bps);
676 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
677 print_error_with_state(&encoder_session, "ERROR during encoding");
678 return EncoderSession_finish_error(&encoder_session);
680 data_bytes -= bytes_read;
686 * now read unaligned samples into reservoir or pad with zeroes if necessary
688 if(options.common.sector_align) {
689 if(options.common.is_last_file) {
690 unsigned wide_samples = 588 - align_remainder;
691 if(wide_samples < 588) {
694 info_align_zero = wide_samples;
695 data_bytes = wide_samples * (bps >> 3);
696 for(channel = 0; channel < channels; channel++)
697 memset(input_[channel], 0, data_bytes);
699 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
700 print_error_with_state(&encoder_session, "ERROR during encoding");
701 return EncoderSession_finish_error(&encoder_session);
706 if(*options.common.align_reservoir_samples > 0) {
707 FLAC__ASSERT(CHUNK_OF_SAMPLES >= 588);
708 bytes_read = fread(ucbuffer_, sizeof(unsigned char), (*options.common.align_reservoir_samples) * bytes_per_wide_sample, infile);
709 if(bytes_read == 0 && ferror(infile)) {
710 fprintf(stderr, "%s: ERROR during read\n", encoder_session.inbasefilename);
711 return EncoderSession_finish_error(&encoder_session);
713 else if(bytes_read != (*options.common.align_reservoir_samples) * bytes_per_wide_sample) {
714 fprintf(stderr, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned)bytes_read, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
718 info_align_carry = *options.common.align_reservoir_samples;
719 format_input(options.common.align_reservoir, *options.common.align_reservoir_samples, false, is_unsigned_samples, channels, bps);
725 got_data_chunk = true;
728 if(xx == 0x20746d66 && got_fmt_chunk) { /* "fmt " */
729 fprintf(stderr, "%s: WARNING: skipping extra 'fmt ' sub-chunk\n", encoder_session.inbasefilename);
731 else if(xx == 0x61746164) { /* "data" */
733 fprintf(stderr, "%s: WARNING: skipping extra 'data' sub-chunk\n", encoder_session.inbasefilename);
735 else if(!got_fmt_chunk) {
736 fprintf(stderr, "%s: ERROR: got 'data' sub-chunk before 'fmt' sub-chunk\n", encoder_session.inbasefilename);
737 return EncoderSession_finish_error(&encoder_session);
744 fprintf(stderr, "%s: WARNING: skipping unknown sub-chunk '%c%c%c%c'\n", encoder_session.inbasefilename, (char)(xx&255), (char)((xx>>8)&255), (char)((xx>>16)&255), (char)(xx>>24));
747 if(!read_little_endian_uint32(infile, &xx, false, encoder_session.inbasefilename))
748 return EncoderSession_finish_error(&encoder_session);
749 if(fseek(infile, xx, SEEK_CUR) < 0) {
750 /* can't seek input, read ahead manually... */
752 const unsigned chunk = sizeof(ucbuffer_);
753 for(left = xx; left > 0; ) {
754 need = min(left, chunk);
755 if(fread(ucbuffer_, 1, need, infile) < need) {
756 fprintf(stderr, "%s: ERROR during read while skipping unsupported sub-chunk\n", encoder_session.inbasefilename);
757 return EncoderSession_finish_error(&encoder_session);
765 return EncoderSession_finish_ok(&encoder_session, info_align_carry, info_align_zero);
768 int flac__encode_raw(FILE *infile, long infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, raw_encode_options_t options)
770 EncoderSession encoder_session;
772 const size_t bytes_per_wide_sample = options.channels * (options.bps >> 3);
773 unsigned align_remainder = 0;
774 int info_align_carry = -1, info_align_zero = -1;
776 FLAC__ASSERT(!options.common.sector_align || options.common.skip == 0);
777 FLAC__ASSERT(!options.common.sector_align || options.channels == 2);
778 FLAC__ASSERT(!options.common.sector_align || options.bps == 16);
779 FLAC__ASSERT(!options.common.sector_align || options.sample_rate == 44100);
780 FLAC__ASSERT(!options.common.sector_align || infilesize >= 0);
783 EncoderSession_construct(
786 options.common.use_ogg,
790 options.common.verify,
791 options.common.verbose,
799 /* get the file length */
801 encoder_session.total_samples_to_encode = encoder_session.unencoded_size = 0;
804 if(options.common.sector_align) {
805 FLAC__ASSERT(options.common.skip == 0);
806 encoder_session.total_samples_to_encode = (unsigned)infilesize / bytes_per_wide_sample + *options.common.align_reservoir_samples;
807 align_remainder = (unsigned)(encoder_session.total_samples_to_encode % 588);
808 if(options.common.is_last_file)
809 encoder_session.total_samples_to_encode += (588-align_remainder); /* will pad with zeroes */
811 encoder_session.total_samples_to_encode -= align_remainder; /* will stop short and carry over to next file */
814 encoder_session.total_samples_to_encode = (unsigned)infilesize / bytes_per_wide_sample - options.common.skip;
817 encoder_session.unencoded_size = encoder_session.total_samples_to_encode * bytes_per_wide_sample;
820 if(encoder_session.verbose && encoder_session.total_samples_to_encode <= 0)
821 fprintf(stderr, "(No runtime statistics possible; please wait for encoding to finish...)\n");
823 if(options.common.skip > 0) {
824 unsigned skip_bytes = bytes_per_wide_sample * (unsigned)options.common.skip;
825 if(skip_bytes > lookahead_length) {
826 skip_bytes -= lookahead_length;
827 lookahead_length = 0;
828 if(fseek(infile, (long)skip_bytes, SEEK_CUR) < 0) {
829 /* can't seek input, read ahead manually... */
831 const unsigned chunk = sizeof(ucbuffer_);
832 for(left = skip_bytes; left > 0; ) {
833 need = min(left, chunk);
834 if(fread(ucbuffer_, 1, need, infile) < need) {
835 fprintf(stderr, "%s: ERROR during read while skipping samples\n", encoder_session.inbasefilename);
836 return EncoderSession_finish_error(&encoder_session);
843 lookahead += skip_bytes;
844 lookahead_length -= skip_bytes;
848 if(!EncoderSession_init_encoder(&encoder_session, options.common, options.channels, options.bps, options.sample_rate))
849 return EncoderSession_finish_error(&encoder_session);
852 * first do any samples in the reservoir
854 if(options.common.sector_align && *options.common.align_reservoir_samples > 0) {
855 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)options.common.align_reservoir, *options.common.align_reservoir_samples)) {
856 print_error_with_state(&encoder_session, "ERROR during encoding");
857 return EncoderSession_finish_error(&encoder_session);
862 * decrement infilesize if we need to align the file
864 if(options.common.sector_align) {
865 FLAC__ASSERT(infilesize >= 0);
866 if(options.common.is_last_file) {
867 *options.common.align_reservoir_samples = 0;
870 *options.common.align_reservoir_samples = align_remainder;
871 infilesize -= (long)((*options.common.align_reservoir_samples) * bytes_per_wide_sample);
876 * now do from the file
878 while(!feof(infile)) {
879 if(lookahead_length > 0) {
880 FLAC__ASSERT(lookahead_length < CHUNK_OF_SAMPLES * bytes_per_wide_sample);
881 memcpy(ucbuffer_, lookahead, lookahead_length);
882 bytes_read = fread(ucbuffer_+lookahead_length, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample - lookahead_length, infile) + lookahead_length;
884 fprintf(stderr, "%s: ERROR during read\n", encoder_session.inbasefilename);
885 return EncoderSession_finish_error(&encoder_session);
887 lookahead_length = 0;
890 bytes_read = fread(ucbuffer_, sizeof(unsigned char), CHUNK_OF_SAMPLES * bytes_per_wide_sample, infile);
892 if(bytes_read == 0) {
894 fprintf(stderr, "%s: ERROR during read\n", encoder_session.inbasefilename);
895 return EncoderSession_finish_error(&encoder_session);
898 else if(bytes_read % bytes_per_wide_sample != 0) {
899 fprintf(stderr, "%s: ERROR: got partial sample\n", encoder_session.inbasefilename);
900 return EncoderSession_finish_error(&encoder_session);
903 unsigned wide_samples = bytes_read / bytes_per_wide_sample;
904 format_input(input_, wide_samples, options.is_big_endian, options.is_unsigned_samples, options.channels, options.bps);
906 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
907 print_error_with_state(&encoder_session, "ERROR during encoding");
908 return EncoderSession_finish_error(&encoder_session);
914 * now read unaligned samples into reservoir or pad with zeroes if necessary
916 if(options.common.sector_align) {
917 if(options.common.is_last_file) {
918 unsigned wide_samples = 588 - align_remainder;
919 if(wide_samples < 588) {
920 unsigned channel, data_bytes;
922 info_align_zero = wide_samples;
923 data_bytes = wide_samples * (options.bps >> 3);
924 for(channel = 0; channel < options.channels; channel++)
925 memset(input_[channel], 0, data_bytes);
927 if(!EncoderSession_process(&encoder_session, (const FLAC__int32 * const *)input_, wide_samples)) {
928 print_error_with_state(&encoder_session, "ERROR during encoding");
929 return EncoderSession_finish_error(&encoder_session);
934 if(*options.common.align_reservoir_samples > 0) {
935 FLAC__ASSERT(CHUNK_OF_SAMPLES >= 588);
936 bytes_read = fread(ucbuffer_, sizeof(unsigned char), (*options.common.align_reservoir_samples) * bytes_per_wide_sample, infile);
937 if(bytes_read == 0 && ferror(infile)) {
938 fprintf(stderr, "%s: ERROR during read\n", encoder_session.inbasefilename);
939 return EncoderSession_finish_error(&encoder_session);
941 else if(bytes_read != (*options.common.align_reservoir_samples) * bytes_per_wide_sample) {
942 fprintf(stderr, "%s: WARNING: unexpected EOF; read %u bytes; expected %u samples, got %u samples\n", encoder_session.inbasefilename, (unsigned)bytes_read, (unsigned)encoder_session.total_samples_to_encode, (unsigned)encoder_session.samples_written);
945 info_align_carry = *options.common.align_reservoir_samples;
946 format_input(options.common.align_reservoir, *options.common.align_reservoir_samples, false, options.is_unsigned_samples, options.channels, options.bps);
952 return EncoderSession_finish_ok(&encoder_session, info_align_carry, info_align_zero);
955 FLAC__bool EncoderSession_construct(EncoderSession *e, FLAC__bool use_ogg, FLAC__bool verify, FLAC__bool verbose, FILE *infile, const char *infilename, const char *outfilename)
958 FLAC__uint32 test = 1;
964 is_big_endian_host_ = (*((FLAC__byte*)(&test)))? false : true;
966 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
967 input_[i] = &(in_[i][0]);
971 * initialize instance
975 e->use_ogg = use_ogg;
980 e->verbose = verbose;
982 e->is_stdout = (0 == strcmp(outfilename, "-"));
984 e->inbasefilename = flac__file_get_basename(infilename);
985 e->outfilename = outfilename;
987 e->unencoded_size = 0;
988 e->total_samples_to_encode = 0;
989 e->bytes_written = 0;
990 e->samples_written = 0;
994 e->encoder.flac.stream = 0;
995 e->encoder.flac.file = 0;
997 e->encoder.ogg.stream = 0;
1002 e->seek_table_template = 0;
1005 e->fout = file__get_binary_stdout();
1007 #ifdef FLAC__HAS_OGG
1010 if(0 == (e->fout = fopen(outfilename, "wb"))) {
1011 fprintf(stderr, "%s: ERROR: can't open output file %s\n", e->inbasefilename, outfilename);
1012 EncoderSession_destroy(e);
1019 if(0 == (e->seek_table_template = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE))) {
1020 fprintf(stderr, "%s: ERROR allocating memory for seek table\n", e->inbasefilename);
1024 #ifdef FLAC__HAS_OGG
1026 e->encoder.ogg.stream = OggFLAC__stream_encoder_new();
1027 if(0 == e->encoder.ogg.stream) {
1028 fprintf(stderr, "%s: ERROR creating the encoder instance\n", e->inbasefilename);
1029 EncoderSession_destroy(e);
1036 e->encoder.flac.stream = FLAC__stream_encoder_new();
1037 if(0 == e->encoder.flac.stream) {
1038 fprintf(stderr, "%s: ERROR creating the encoder instance\n", e->inbasefilename);
1039 EncoderSession_destroy(e);
1044 e->encoder.flac.file = FLAC__file_encoder_new();
1045 if(0 == e->encoder.flac.file) {
1046 fprintf(stderr, "%s: ERROR creating the encoder instance\n", e->inbasefilename);
1047 EncoderSession_destroy(e);
1055 void EncoderSession_destroy(EncoderSession *e)
1060 #ifdef FLAC__HAS_OGG
1062 if(0 != e->encoder.ogg.stream) {
1063 OggFLAC__stream_encoder_delete(e->encoder.ogg.stream);
1064 e->encoder.ogg.stream = 0;
1070 if(0 != e->encoder.flac.stream) {
1071 FLAC__stream_encoder_delete(e->encoder.flac.stream);
1072 e->encoder.flac.stream = 0;
1076 if(0 != e->encoder.flac.file) {
1077 FLAC__file_encoder_delete(e->encoder.flac.file);
1078 e->encoder.flac.file = 0;
1082 if(0 != e->seek_table_template) {
1083 FLAC__metadata_object_delete(e->seek_table_template);
1084 e->seek_table_template = 0;
1088 int EncoderSession_finish_ok(EncoderSession *e, int info_align_carry, int info_align_zero)
1090 FLAC__StreamEncoderState fse_state = FLAC__STREAM_ENCODER_OK;
1093 #ifdef FLAC__HAS_OGG
1095 if(e->encoder.ogg.stream) {
1096 fse_state = OggFLAC__stream_encoder_get_FLAC_stream_encoder_state(e->encoder.ogg.stream);
1097 OggFLAC__stream_encoder_finish(e->encoder.ogg.stream);
1103 if(e->encoder.flac.stream) {
1104 fse_state = FLAC__stream_encoder_get_state(e->encoder.flac.stream);
1105 FLAC__stream_encoder_finish(e->encoder.flac.stream);
1109 if(e->encoder.flac.file) {
1110 fse_state = FLAC__file_encoder_get_stream_encoder_state(e->encoder.flac.file);
1111 FLAC__file_encoder_finish(e->encoder.flac.file);
1115 if(e->verbose && e->total_samples_to_encode > 0) {
1117 fprintf(stderr, "\n");
1120 if(fse_state == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA) {
1121 print_verify_error(e);
1125 if(info_align_carry >= 0)
1126 fprintf(stderr, "%s: INFO: sector alignment causing %d samples to be carried over\n", e->inbasefilename, info_align_carry);
1127 if(info_align_zero >= 0)
1128 fprintf(stderr, "%s: INFO: sector alignment causing %d zero samples to be appended\n", e->inbasefilename, info_align_zero);
1131 EncoderSession_destroy(e);
1136 int EncoderSession_finish_error(EncoderSession *e)
1138 FLAC__StreamEncoderState fse_state;
1140 if(e->verbose && e->total_samples_to_encode > 0)
1141 fprintf(stderr, "\n");
1143 #ifdef FLAC__HAS_OGG
1145 fse_state = OggFLAC__stream_encoder_get_FLAC_stream_encoder_state(e->encoder.ogg.stream);
1150 fse_state = FLAC__stream_encoder_get_state(e->encoder.flac.stream);
1153 fse_state = FLAC__file_encoder_get_stream_encoder_state(e->encoder.flac.file);
1156 if(fse_state == FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA)
1157 print_verify_error(e);
1159 unlink(e->outfilename);
1161 EncoderSession_destroy(e);
1166 FLAC__bool EncoderSession_init_encoder(EncoderSession *e, encode_options_t options, unsigned channels, unsigned bps, unsigned sample_rate)
1168 unsigned num_metadata;
1169 FLAC__StreamMetadata padding;
1170 FLAC__StreamMetadata *metadata[3];
1173 options.do_mid_side = options.loose_mid_side = false;
1175 if(!convert_to_seek_table_template(options.requested_seek_points, options.num_requested_seek_points, e)) {
1176 fprintf(stderr, "%s: ERROR allocating memory for seek table\n", e->inbasefilename);
1181 metadata[num_metadata++] = options.vorbis_comment;
1182 if(e->seek_table_template->data.seek_table.num_points > 0) {
1183 e->seek_table_template->is_last = false; /* the encoder will set this for us */
1184 metadata[num_metadata++] = e->seek_table_template;
1186 if(options.padding > 0) {
1187 padding.is_last = false; /* the encoder will set this for us */
1188 padding.type = FLAC__METADATA_TYPE_PADDING;
1189 padding.length = (unsigned)options.padding;
1190 metadata[num_metadata++] = &padding;
1193 e->blocksize = options.blocksize;
1194 e->stats_mask = (options.do_exhaustive_model_search || options.do_qlp_coeff_prec_search)? 0x0f : 0x3f;
1196 #ifdef FLAC__HAS_OGG
1198 if(options.has_serial_number)
1199 OggFLAC__stream_encoder_set_serial_number(e->encoder.ogg.stream, options.serial_number);
1200 OggFLAC__stream_encoder_set_verify(e->encoder.ogg.stream, options.verify);
1201 OggFLAC__stream_encoder_set_streamable_subset(e->encoder.ogg.stream, !options.lax);
1202 OggFLAC__stream_encoder_set_do_mid_side_stereo(e->encoder.ogg.stream, options.do_mid_side);
1203 OggFLAC__stream_encoder_set_loose_mid_side_stereo(e->encoder.ogg.stream, options.loose_mid_side);
1204 OggFLAC__stream_encoder_set_channels(e->encoder.ogg.stream, channels);
1205 OggFLAC__stream_encoder_set_bits_per_sample(e->encoder.ogg.stream, bps);
1206 OggFLAC__stream_encoder_set_sample_rate(e->encoder.ogg.stream, sample_rate);
1207 OggFLAC__stream_encoder_set_blocksize(e->encoder.ogg.stream, options.blocksize);
1208 OggFLAC__stream_encoder_set_max_lpc_order(e->encoder.ogg.stream, options.max_lpc_order);
1209 OggFLAC__stream_encoder_set_qlp_coeff_precision(e->encoder.ogg.stream, options.qlp_coeff_precision);
1210 OggFLAC__stream_encoder_set_do_qlp_coeff_prec_search(e->encoder.ogg.stream, options.do_qlp_coeff_prec_search);
1211 OggFLAC__stream_encoder_set_do_escape_coding(e->encoder.ogg.stream, options.do_escape_coding);
1212 OggFLAC__stream_encoder_set_do_exhaustive_model_search(e->encoder.ogg.stream, options.do_exhaustive_model_search);
1213 OggFLAC__stream_encoder_set_min_residual_partition_order(e->encoder.ogg.stream, options.min_residual_partition_order);
1214 OggFLAC__stream_encoder_set_max_residual_partition_order(e->encoder.ogg.stream, options.max_residual_partition_order);
1215 OggFLAC__stream_encoder_set_rice_parameter_search_dist(e->encoder.ogg.stream, options.rice_parameter_search_dist);
1216 OggFLAC__stream_encoder_set_total_samples_estimate(e->encoder.ogg.stream, e->total_samples_to_encode);
1217 OggFLAC__stream_encoder_set_metadata(e->encoder.ogg.stream, (num_metadata > 0)? metadata : 0, num_metadata);
1218 OggFLAC__stream_encoder_set_write_callback(e->encoder.ogg.stream, ogg_stream_encoder_write_callback);
1219 OggFLAC__stream_encoder_set_client_data(e->encoder.ogg.stream, e);
1221 OggFLAC__stream_encoder_disable_constant_subframes(e->encoder.ogg.stream, options.debug.disable_constant_subframes);
1222 OggFLAC__stream_encoder_disable_fixed_subframes(e->encoder.ogg.stream, options.debug.disable_fixed_subframes);
1223 OggFLAC__stream_encoder_disable_verbatim_subframes(e->encoder.ogg.stream, options.debug.disable_verbatim_subframes);
1225 if(OggFLAC__stream_encoder_init(e->encoder.ogg.stream) != FLAC__STREAM_ENCODER_OK) {
1226 print_error_with_state(e, "ERROR initializing encoder");
1233 FLAC__stream_encoder_set_verify(e->encoder.flac.stream, options.verify);
1234 FLAC__stream_encoder_set_streamable_subset(e->encoder.flac.stream, !options.lax);
1235 FLAC__stream_encoder_set_do_mid_side_stereo(e->encoder.flac.stream, options.do_mid_side);
1236 FLAC__stream_encoder_set_loose_mid_side_stereo(e->encoder.flac.stream, options.loose_mid_side);
1237 FLAC__stream_encoder_set_channels(e->encoder.flac.stream, channels);
1238 FLAC__stream_encoder_set_bits_per_sample(e->encoder.flac.stream, bps);
1239 FLAC__stream_encoder_set_sample_rate(e->encoder.flac.stream, sample_rate);
1240 FLAC__stream_encoder_set_blocksize(e->encoder.flac.stream, options.blocksize);
1241 FLAC__stream_encoder_set_max_lpc_order(e->encoder.flac.stream, options.max_lpc_order);
1242 FLAC__stream_encoder_set_qlp_coeff_precision(e->encoder.flac.stream, options.qlp_coeff_precision);
1243 FLAC__stream_encoder_set_do_qlp_coeff_prec_search(e->encoder.flac.stream, options.do_qlp_coeff_prec_search);
1244 FLAC__stream_encoder_set_do_escape_coding(e->encoder.flac.stream, options.do_escape_coding);
1245 FLAC__stream_encoder_set_do_exhaustive_model_search(e->encoder.flac.stream, options.do_exhaustive_model_search);
1246 FLAC__stream_encoder_set_min_residual_partition_order(e->encoder.flac.stream, options.min_residual_partition_order);
1247 FLAC__stream_encoder_set_max_residual_partition_order(e->encoder.flac.stream, options.max_residual_partition_order);
1248 FLAC__stream_encoder_set_rice_parameter_search_dist(e->encoder.flac.stream, options.rice_parameter_search_dist);
1249 FLAC__stream_encoder_set_total_samples_estimate(e->encoder.flac.stream, e->total_samples_to_encode);
1250 FLAC__stream_encoder_set_metadata(e->encoder.flac.stream, (num_metadata > 0)? metadata : 0, num_metadata);
1251 FLAC__stream_encoder_set_write_callback(e->encoder.flac.stream, flac_stream_encoder_write_callback);
1252 FLAC__stream_encoder_set_metadata_callback(e->encoder.flac.stream, flac_stream_encoder_metadata_callback);
1253 FLAC__stream_encoder_set_client_data(e->encoder.flac.stream, e);
1255 FLAC__stream_encoder_disable_constant_subframes(e->encoder.flac.stream, options.debug.disable_constant_subframes);
1256 FLAC__stream_encoder_disable_fixed_subframes(e->encoder.flac.stream, options.debug.disable_fixed_subframes);
1257 FLAC__stream_encoder_disable_verbatim_subframes(e->encoder.flac.stream, options.debug.disable_verbatim_subframes);
1259 if(FLAC__stream_encoder_init(e->encoder.flac.stream) != FLAC__STREAM_ENCODER_OK) {
1260 print_error_with_state(e, "ERROR initializing encoder");
1265 FLAC__file_encoder_set_filename(e->encoder.flac.file, e->outfilename);
1266 FLAC__file_encoder_set_verify(e->encoder.flac.file, options.verify);
1267 FLAC__file_encoder_set_streamable_subset(e->encoder.flac.file, !options.lax);
1268 FLAC__file_encoder_set_do_mid_side_stereo(e->encoder.flac.file, options.do_mid_side);
1269 FLAC__file_encoder_set_loose_mid_side_stereo(e->encoder.flac.file, options.loose_mid_side);
1270 FLAC__file_encoder_set_channels(e->encoder.flac.file, channels);
1271 FLAC__file_encoder_set_bits_per_sample(e->encoder.flac.file, bps);
1272 FLAC__file_encoder_set_sample_rate(e->encoder.flac.file, sample_rate);
1273 FLAC__file_encoder_set_blocksize(e->encoder.flac.file, options.blocksize);
1274 FLAC__file_encoder_set_max_lpc_order(e->encoder.flac.file, options.max_lpc_order);
1275 FLAC__file_encoder_set_qlp_coeff_precision(e->encoder.flac.file, options.qlp_coeff_precision);
1276 FLAC__file_encoder_set_do_qlp_coeff_prec_search(e->encoder.flac.file, options.do_qlp_coeff_prec_search);
1277 FLAC__file_encoder_set_do_escape_coding(e->encoder.flac.file, options.do_escape_coding);
1278 FLAC__file_encoder_set_do_exhaustive_model_search(e->encoder.flac.file, options.do_exhaustive_model_search);
1279 FLAC__file_encoder_set_min_residual_partition_order(e->encoder.flac.file, options.min_residual_partition_order);
1280 FLAC__file_encoder_set_max_residual_partition_order(e->encoder.flac.file, options.max_residual_partition_order);
1281 FLAC__file_encoder_set_rice_parameter_search_dist(e->encoder.flac.file, options.rice_parameter_search_dist);
1282 FLAC__file_encoder_set_total_samples_estimate(e->encoder.flac.file, e->total_samples_to_encode);
1283 FLAC__file_encoder_set_metadata(e->encoder.flac.file, (num_metadata > 0)? metadata : 0, num_metadata);
1284 FLAC__file_encoder_set_progress_callback(e->encoder.flac.file, flac_file_encoder_progress_callback);
1285 FLAC__file_encoder_set_client_data(e->encoder.flac.file, e);
1287 FLAC__file_encoder_disable_constant_subframes(e->encoder.flac.file, options.debug.disable_constant_subframes);
1288 FLAC__file_encoder_disable_fixed_subframes(e->encoder.flac.file, options.debug.disable_fixed_subframes);
1289 FLAC__file_encoder_disable_verbatim_subframes(e->encoder.flac.file, options.debug.disable_verbatim_subframes);
1291 if(FLAC__file_encoder_init(e->encoder.flac.file) != FLAC__FILE_ENCODER_OK) {
1292 print_error_with_state(e, "ERROR initializing encoder");
1300 FLAC__bool EncoderSession_process(EncoderSession *e, const FLAC__int32 * const buffer[], unsigned samples)
1302 #ifdef FLAC__HAS_OGG
1304 return OggFLAC__stream_encoder_process(e->encoder.ogg.stream, buffer, samples);
1309 return FLAC__stream_encoder_process(e->encoder.flac.stream, buffer, samples);
1312 return FLAC__file_encoder_process(e->encoder.flac.file, buffer, samples);
1316 FLAC__bool convert_to_seek_table_template(char *requested_seek_points, int num_requested_seek_points, EncoderSession *e)
1319 char *pt = requested_seek_points, *q;
1320 FLAC__bool only_placeholders = false;
1321 FLAC__bool needs_warning = false;
1323 if(num_requested_seek_points == 0)
1326 if(num_requested_seek_points < 0) {
1327 strcpy(requested_seek_points, "100x<");
1328 num_requested_seek_points = 1;
1332 only_placeholders = true;
1335 else if(e->is_ogg) {
1336 only_placeholders = true;
1340 for(i = 0; i < (unsigned)num_requested_seek_points; i++) {
1341 q = strchr(pt, '<');
1342 FLAC__ASSERT(0 != q);
1345 if(0 == strcmp(pt, "X")) { /* -S X */
1346 if(!FLAC__metadata_object_seektable_template_append_placeholders(e->seek_table_template, 1))
1349 else if(!only_placeholders) {
1350 if(pt[strlen(pt)-1] == 'x') { /* -S #x */
1351 if(e->total_samples_to_encode > 0) { /* we can only do these if we know the number of samples to encode up front */
1352 if(!FLAC__metadata_object_seektable_template_append_spaced_points(e->seek_table_template, atoi(pt), e->total_samples_to_encode))
1357 FLAC__uint64 n = (unsigned)atoi(pt);
1358 if(!FLAC__metadata_object_seektable_template_append_point(e->seek_table_template, n))
1363 needs_warning = true;
1368 if(!FLAC__metadata_object_seektable_template_sort(e->seek_table_template, /*compact=*/true))
1373 fprintf(stderr, "%s: WARNING, cannot write back seektable when encoding to stdout\n", e->inbasefilename);
1376 else if(e->is_ogg) {
1377 fprintf(stderr, "%s: WARNING, cannot write back seektable when encoding to Ogg\n", e->inbasefilename);
1388 void format_input(FLAC__int32 *dest[], unsigned wide_samples, FLAC__bool is_big_endian, FLAC__bool is_unsigned_samples, unsigned channels, unsigned bps)
1390 unsigned wide_sample, sample, channel, byte;
1393 if(is_unsigned_samples) {
1394 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
1395 for(channel = 0; channel < channels; channel++, sample++)
1396 dest[channel][wide_sample] = (FLAC__int32)ucbuffer_[sample] - 0x80;
1399 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
1400 for(channel = 0; channel < channels; channel++, sample++)
1401 dest[channel][wide_sample] = (FLAC__int32)scbuffer_[sample];
1404 else if(bps == 16) {
1405 if(is_big_endian != is_big_endian_host_) {
1407 const unsigned bytes = wide_samples * channels * (bps >> 3);
1408 for(byte = 0; byte < bytes; byte += 2) {
1409 tmp = ucbuffer_[byte];
1410 ucbuffer_[byte] = ucbuffer_[byte+1];
1411 ucbuffer_[byte+1] = tmp;
1414 if(is_unsigned_samples) {
1415 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
1416 for(channel = 0; channel < channels; channel++, sample++)
1417 dest[channel][wide_sample] = (FLAC__int32)usbuffer_[sample] - 0x8000;
1420 for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
1421 for(channel = 0; channel < channels; channel++, sample++)
1422 dest[channel][wide_sample] = (FLAC__int32)ssbuffer_[sample];
1425 else if(bps == 24) {
1426 if(!is_big_endian) {
1428 const unsigned bytes = wide_samples * channels * (bps >> 3);
1429 for(byte = 0; byte < bytes; byte += 3) {
1430 tmp = ucbuffer_[byte];
1431 ucbuffer_[byte] = ucbuffer_[byte+2];
1432 ucbuffer_[byte+2] = tmp;
1435 if(is_unsigned_samples) {
1436 for(byte = sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
1437 for(channel = 0; channel < channels; channel++, sample++) {
1438 dest[channel][wide_sample] = ucbuffer_[byte++]; dest[channel][wide_sample] <<= 8;
1439 dest[channel][wide_sample] |= ucbuffer_[byte++]; dest[channel][wide_sample] <<= 8;
1440 dest[channel][wide_sample] |= ucbuffer_[byte++];
1441 dest[channel][wide_sample] -= 0x800000;
1445 for(byte = sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++)
1446 for(channel = 0; channel < channels; channel++, sample++) {
1447 dest[channel][wide_sample] = scbuffer_[byte++]; dest[channel][wide_sample] <<= 8;
1448 dest[channel][wide_sample] |= ucbuffer_[byte++]; dest[channel][wide_sample] <<= 8;
1449 dest[channel][wide_sample] |= ucbuffer_[byte++];
1458 #ifdef FLAC__HAS_OGG
1459 FLAC__StreamEncoderWriteStatus ogg_stream_encoder_write_callback(const OggFLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
1461 EncoderSession *encoder_session = (EncoderSession*)client_data;
1465 encoder_session->bytes_written += bytes;
1467 * With Ogg FLAC we don't get one write callback per frame and
1468 * we don't have good number for 'samples', so we estimate based
1469 * on the frame number and the knowledge that all blocks (except
1470 * the last) are the same size.
1473 encoder_session->samples_written = (current_frame+1) * encoder_session->blocksize;
1475 if(encoder_session->verbose && encoder_session->total_samples_to_encode > 0 && !(current_frame & encoder_session->stats_mask))
1476 print_stats(encoder_session);
1478 if(fwrite(buffer, sizeof(FLAC__byte), bytes, encoder_session->fout) == bytes)
1479 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
1481 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
1485 FLAC__StreamEncoderWriteStatus flac_stream_encoder_write_callback(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
1487 EncoderSession *encoder_session = (EncoderSession*)client_data;
1491 encoder_session->bytes_written += bytes;
1492 encoder_session->samples_written += samples;
1494 if(samples && encoder_session->verbose && encoder_session->total_samples_to_encode > 0 && !(current_frame & encoder_session->stats_mask))
1495 print_stats(encoder_session);
1497 if(fwrite(buffer, sizeof(FLAC__byte), bytes, encoder_session->fout) == bytes)
1498 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
1500 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
1503 void flac_stream_encoder_metadata_callback(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
1506 * Nothing to do; if we get here, we're decoding to stdout, in
1507 * which case we can't seek backwards to write new metadata.
1509 (void)encoder, (void)metadata, (void)client_data;
1512 void flac_file_encoder_progress_callback(const FLAC__FileEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, unsigned frames_written, unsigned total_frames_estimate, void *client_data)
1514 EncoderSession *encoder_session = (EncoderSession*)client_data;
1516 (void)encoder, (void)total_frames_estimate;
1518 encoder_session->bytes_written = bytes_written;
1519 encoder_session->samples_written = samples_written;
1521 if(encoder_session->verbose && encoder_session->total_samples_to_encode > 0 && !((frames_written-1) & encoder_session->stats_mask))
1522 print_stats(encoder_session);
1525 void print_stats(const EncoderSession *encoder_session)
1527 const FLAC__uint64 samples_written = min(encoder_session->total_samples_to_encode, encoder_session->samples_written);
1528 #if defined _MSC_VER || defined __MINGW32__
1529 /* with VC++ you have to spoon feed it the casting */
1530 const double progress = (double)(FLAC__int64)samples_written / (double)(FLAC__int64)encoder_session->total_samples_to_encode;
1531 const double ratio = (double)(FLAC__int64)encoder_session->bytes_written / ((double)(FLAC__int64)encoder_session->unencoded_size * progress);
1533 const double progress = (double)samples_written / (double)encoder_session->total_samples_to_encode;
1534 const double ratio = (double)encoder_session->bytes_written / ((double)encoder_session->unencoded_size * progress);
1537 if(samples_written == encoder_session->total_samples_to_encode) {
1538 fprintf(stderr, "\r%s:%s wrote %u bytes, ratio=%0.3f",
1539 encoder_session->inbasefilename,
1540 encoder_session->verify? " Verify OK," : "",
1541 (unsigned)encoder_session->bytes_written,
1546 fprintf(stderr, "\r%s: %u%% complete, ratio=%0.3f", encoder_session->inbasefilename, (unsigned)floor(progress * 100.0 + 0.5), ratio);
1550 void print_error_with_state(const EncoderSession *e, const char *message)
1552 const int ilen = strlen(e->inbasefilename) + 1;
1554 fprintf(stderr, "\n%s: %s\n", e->inbasefilename, message);
1556 #ifdef FLAC__HAS_OGG
1558 const OggFLAC__StreamEncoderState ose_state = OggFLAC__stream_encoder_get_state(e->encoder.ogg.stream);
1559 if(ose_state != OggFLAC__STREAM_ENCODER_FLAC_STREAM_ENCODER_ERROR) {
1560 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)ose_state, OggFLAC__StreamEncoderStateString[ose_state]);
1563 const FLAC__StreamEncoderState fse_state = OggFLAC__stream_encoder_get_FLAC_stream_encoder_state(e->encoder.ogg.stream);
1564 if(fse_state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
1565 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)fse_state, FLAC__StreamEncoderStateString[fse_state]);
1568 const FLAC__StreamDecoderState fsd_state = OggFLAC__stream_encoder_get_verify_decoder_state(e->encoder.ogg.stream);
1569 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)fsd_state, FLAC__StreamDecoderStateString[fsd_state]);
1576 const FLAC__StreamEncoderState fse_state = FLAC__stream_encoder_get_state(e->encoder.flac.stream);
1577 if(fse_state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
1578 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)fse_state, FLAC__StreamEncoderStateString[fse_state]);
1581 const FLAC__StreamDecoderState fsd_state = FLAC__stream_encoder_get_verify_decoder_state(e->encoder.flac.stream);
1582 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)fsd_state, FLAC__StreamDecoderStateString[fsd_state]);
1586 const FLAC__FileEncoderState ffe_state = FLAC__file_encoder_get_state(e->encoder.flac.file);
1587 if(ffe_state != FLAC__FILE_ENCODER_SEEKABLE_STREAM_ENCODER_ERROR) {
1588 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)ffe_state, FLAC__FileEncoderStateString[ffe_state]);
1591 const FLAC__SeekableStreamEncoderState fsse_state = FLAC__file_encoder_get_seekable_stream_encoder_state(e->encoder.flac.file);
1592 if(fsse_state != FLAC__SEEKABLE_STREAM_ENCODER_STREAM_ENCODER_ERROR) {
1593 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)fsse_state, FLAC__SeekableStreamEncoderStateString[fsse_state]);
1596 const FLAC__StreamEncoderState fse_state = FLAC__file_encoder_get_stream_encoder_state(e->encoder.flac.file);
1597 if(fse_state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR) {
1598 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)fse_state, FLAC__StreamEncoderStateString[fse_state]);
1601 const FLAC__StreamDecoderState fsd_state = FLAC__file_encoder_get_verify_decoder_state(e->encoder.flac.file);
1602 fprintf(stderr, "%*s state = %d:%s\n", ilen, "", (int)fsd_state, FLAC__StreamDecoderStateString[fsd_state]);
1609 void print_verify_error(EncoderSession *e)
1611 FLAC__uint64 absolute_sample;
1612 unsigned frame_number;
1615 FLAC__int32 expected;
1618 #ifdef FLAC__HAS_OGG
1620 OggFLAC__stream_encoder_get_verify_decoder_error_stats(e->encoder.ogg.stream, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
1625 FLAC__stream_encoder_get_verify_decoder_error_stats(e->encoder.flac.stream, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
1628 FLAC__file_encoder_get_verify_decoder_error_stats(e->encoder.flac.file, &absolute_sample, &frame_number, &channel, &sample, &expected, &got);
1631 fprintf(stderr, "%s: ERROR: mismatch in decoded data, verify FAILED!\n", e->inbasefilename);
1632 fprintf(stderr, " Absolute sample=%u, frame=%u, channel=%u, sample=%u, expected %d, got %d\n", (unsigned)absolute_sample, frame_number, channel, sample, expected, got);
1633 fprintf(stderr, " Please submit a bug report to\n");
1634 fprintf(stderr, " http://sourceforge.net/bugs/?func=addbug&group_id=13478\n");
1635 fprintf(stderr, " Make sure to include an email contact in the comment and/or use the\n");
1636 fprintf(stderr, " \"Monitor\" feature to monitor the bug status.\n");
1637 fprintf(stderr, "Verify FAILED! Do not trust %s\n", e->outfilename);
1640 FLAC__bool read_little_endian_uint16(FILE *f, FLAC__uint16 *val, FLAC__bool eof_ok, const char *fn)
1642 size_t bytes_read = fread(val, 1, 2, f);
1644 if(bytes_read == 0) {
1646 fprintf(stderr, "%s: ERROR: unexpected EOF\n", fn);
1652 else if(bytes_read < 2) {
1653 fprintf(stderr, "%s: ERROR: unexpected EOF\n", fn);
1657 if(is_big_endian_host_) {
1658 FLAC__byte tmp, *b = (FLAC__byte*)val;
1659 tmp = b[1]; b[1] = b[0]; b[0] = tmp;
1665 FLAC__bool read_little_endian_uint32(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn)
1667 size_t bytes_read = fread(val, 1, 4, f);
1669 if(bytes_read == 0) {
1671 fprintf(stderr, "%s: ERROR: unexpected EOF\n", fn);
1677 else if(bytes_read < 4) {
1678 fprintf(stderr, "%s: ERROR: unexpected EOF\n", fn);
1682 if(is_big_endian_host_) {
1683 FLAC__byte tmp, *b = (FLAC__byte*)val;
1684 tmp = b[3]; b[3] = b[0]; b[0] = tmp;
1685 tmp = b[2]; b[2] = b[1]; b[1] = tmp;
1692 read_big_endian_uint16(FILE *f, FLAC__uint16 *val, FLAC__bool eof_ok, const char *fn)
1694 unsigned char buf[4];
1695 size_t bytes_read= fread(buf, 1, 2, f);
1697 if(bytes_read==0U && eof_ok)
1699 else if(bytes_read<2U) {
1700 fprintf(stderr, "%s: ERROR: unexpected EOF\n", fn);
1704 /* this is independent of host endianness */
1705 *val= (FLAC__uint16)(buf[0])<<8 | buf[1];
1711 read_big_endian_uint32(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn)
1713 unsigned char buf[4];
1714 size_t bytes_read= fread(buf, 1, 4, f);
1716 if(bytes_read==0U && eof_ok)
1718 else if(bytes_read<4U) {
1719 fprintf(stderr, "%s: ERROR: unexpected EOF\n", fn);
1723 /* this is independent of host endianness */
1724 *val= (FLAC__uint32)(buf[0])<<24 | (FLAC__uint32)(buf[1])<<16 |
1725 (FLAC__uint32)(buf[2])<<8 | buf[3];
1731 read_sane_extended(FILE *f, FLAC__uint32 *val, FLAC__bool eof_ok, const char *fn)
1732 /* Read an IEEE 754 80-bit (aka SANE) extended floating point value from 'f',
1733 * convert it into an integral value and store in 'val'. Return false if only
1734 * between 1 and 9 bytes remain in 'f', if 0 bytes remain in 'f' and 'eof_ok' is
1735 * false, or if the value is negative, between zero and one, or too large to be
1736 * represented by 'val'; return true otherwise.
1740 unsigned char buf[10];
1741 size_t bytes_read= fread(buf, 1U, 10U, f);
1742 FLAC__int16 e= ((FLAC__uint16)(buf[0])<<8 | (FLAC__uint16)(buf[1]))-0x3FFF;
1743 FLAC__int16 shift= 63-e;
1746 if(bytes_read==0U && eof_ok)
1748 else if(bytes_read<10U) {
1749 fprintf(stderr, "%s: ERROR: unexpected EOF\n", fn);
1752 else if((buf[0]>>7)==1U || e<0 || e>63) {
1753 fprintf(stderr, "%s: ERROR: invalid floating-point value\n", fn);
1757 for(i= 0U; i<8U; ++i)
1758 p|= (FLAC__uint64)(buf[i+2])<<(56U-i*8);
1759 *val= (FLAC__uint32)((p>>shift)+(p>>(shift-1) & 0x1));