From ba03ad120b5023b61f11056c7f9d7b620f8c4c7a Mon Sep 17 00:00:00 2001 From: Josh Coalson Date: Wed, 27 Jun 2001 06:48:34 +0000 Subject: [PATCH] add --sector-align option --- doc/documentation.html | 84 +++++++++++++++------------ man/flac.sgml | 18 ++++-- src/flac/encode.c | 152 ++++++++++++++++++++++++++++++++++++++++++------- src/flac/encode.h | 4 +- src/flac/main.c | 46 +++++++++++---- 5 files changed, 229 insertions(+), 75 deletions(-) diff --git a/doc/documentation.html b/doc/documentation.html index 5baa07b..b23d76f 100644 --- a/doc/documentation.html +++ b/doc/documentation.html @@ -337,7 +337,7 @@ - --a-rtext + --a-rtext Includes the residual signal in the analysis file. This will make the file very big, much larger than even the decoded file. @@ -345,7 +345,7 @@ - --a-rgp + --a-rgp Generates a gnuplot file for every subframe; each file will contain the residual distribution of the subframe. This will create a lot of files. @@ -364,7 +364,7 @@ - --lax + --lax Allow encoder to generate non-Subset files. The resulting FLAC file may not be streamable, so you should only use this option in combination with custom encoding options meant for archival. File decoders will still be able play (and seek in) such files. @@ -372,7 +372,17 @@ - -S { # | X | #x } + --sector-align + + + Align encoding of multiple CD format WAVE files on sector boundaries. This option is only allowed when encoding WAVE files, all of which have a 44.1kHz sample rate and 2 channels. With --sector-align, the encoder will align the resulting .flac streams so that their lengths are even multiples of a CD sector (1/75th of a second, or 588 samples). It does this by carrying over any partial sector at the end of each WAVE file to the next stream. The last stream will be padded to alignment with zeroes.

+ This option will have no effect if the files are already aligned (as is the normally the case with WAVE files ripped from a CD). flac can only align a set of files given in one invocation of flac.

+ WARNING: The ordering of files is important! If you give a command like 'flac --sector-align *.wav' the shell may not expand the wildcard to the order you expect. To be safe you should 'echo *.wav' first to confirm the order, or be explicit like 'flac --sector-align 8.wav 9.wav 10.wav'. + + + + + -S { # | X | #x } Include a point or points in a SEEKTABLE:
@@ -389,13 +399,13 @@ You may use many -S options; the resulting SEEKTABLE will be the unique-ified union of all such values.
With no -S options, flac defaults to '-S 100x'. Use -S- for no SEEKTABLE.
- NOTE: -S #x will not work if the encoder can't determine the input size before starting.
- NOTE: if you use -S # and # is >= samples in the input, there will be either no seek point entered (if the input size is determinable before encoding starts) or a placeholder point (if input size is not determinable).
+ NOTE: -S #x will not work if the encoder can't determine the input size before starting.
+ NOTE: if you use -S # and # is >= samples in the input, there will be either no seek point entered (if the input size is determinable before encoding starts) or a placeholder point (if input size is not determinable).
- -P # + -P # Tell the encoder to write a PADDING metadata block of the given length (in bytes) after the STREAMINFO block. -P 0 implies no PADDING block, which is the default. This is useful if you plan to tag the file later with an APPLICATION block; instead of having to rewrite the entire file later just to insert your block, you can write directly over the PADDING block. @@ -403,7 +413,7 @@ - -b # + -b # Specify the block size in samples. The default is 1152 for -l 0, otherwise 4608. Subset streams must use one of 192/576/1152/2304/4608/256/512/1024/2048/4096/8192/16384/32768. The reference encoder uses the same block size for the entire stream. @@ -411,7 +421,7 @@ - -m + -m Enable mid-side coding (only for stereo streams). Tends to increase compression by a few percent on average. For each block both the stereo pair and mid-side versions of the block will be encoded, and smallest resulting frame will be stored. Currently mid-side encoding is only available when bits-per-sample <= 16. @@ -419,7 +429,7 @@ - -M + -M Enable loose mid-side coding (only for stereo streams). Like -m but the encoder adaptively switches between independent and mid-side coding, which is faster but yields less compression than -m (which does an exhaustive search). @@ -427,7 +437,7 @@ - -0 .. -9 + -0 .. -9 Fastest compression .. highest compression. The default is -5. @@ -435,7 +445,7 @@ - -0 + -0 Synonymous with -l 0 -b 1152 -r 2,2 @@ -443,7 +453,7 @@ - -1 + -1 Synonymous with -l 0 -b 1152 -M -r 2,2 @@ -451,7 +461,7 @@ - -2 + -2 Synonymous with -l 0 -b 1152 -m -r 3 @@ -459,7 +469,7 @@ - -3 + -3 Synonymous with -l 6 -b 4608 -r 3,3 @@ -467,7 +477,7 @@ - -4 + -4 Synonymous with -l 8 -b 4608 -M -r 3,3 @@ -475,7 +485,7 @@ - -5 + -5 Synonymous with -l 8 -b 4608 -m -r 3,3 @@ -483,7 +493,7 @@ - -6 + -6 Synonymous with -l 8 -b 4608 -m -r 4 @@ -491,7 +501,7 @@ - -7 + -7 Synonymous with -l 8 -b 4608 -m -e -r 6 @@ -499,7 +509,7 @@ - -8 + -8 Synonymous with -l 12 -b 4608 -m -e -r 6 @@ -507,7 +517,7 @@ - -9 + -9 Synonymous with -l 32 -b 4608 -m -e -r 16 -p. This is painfully slow but gives you the maximum compression flac can do for the given block size. @@ -515,7 +525,7 @@ - -e + -e Exhaustive model search (expensive!). Normally the encoder estimates the best model to use and encodes once based on the estimate. With an exhaustive model search, the encoder will generate subframes for every order and use the smallest. If the max LPC order is high this can significantly increase the encode time but can shave off another 0.5%. @@ -523,7 +533,7 @@ - -l # + -l # Specifies the maximum LPC order. This number must be <= 32. If 0, the encoder will not attempt generic linear prediction, and use only fixed predictors. Using fixed predictors is faster but usually results in files being 5-10% larger. @@ -531,7 +541,7 @@ - -q # + -q # Specifies the precision of the quantized LP coefficients, in bits. The default is -q 0, which means let the encoder decide based on the signal. Unless you really know your input file it's best to leave this up to the encoder. @@ -539,7 +549,7 @@ - -p + -p Do exhaustive LP coefficient quantization optimization. This option overrides any -q option. It is expensive and typically will only improve the compression a tiny fraction of a percent. -q has no effect when -l 0 is used. @@ -547,16 +557,16 @@ - -r [#,]# + -r [#,]# - Set the [min,]max residual partition order. The min value defaults to 0 if unspecified.
+ Set the [min,]max residual partition order. The min value defaults to 0 if unspecified.

By default the encoder uses a single Rice parameter for the subframe's entire residual. With this option, the residual is iteratively partitioned into 2^min# .. 2^max# pieces, each with its own Rice parameter. Higher values of max# yield diminishing returns. The most bang for the buck is usually with -r 2,2 (more for higher block sizes). This usually shaves off about 1.5%. The technique tends to peak out about when blocksize/(2^n)=128. Use -r 0,16 to force the highest degree of optimization. - -R # + -R # Set the Rice parameter search distance. Defaults to 0. The residual coder will search for the best Rice parameter +/- this number for each residual partition. This option is expensive (run time for -R n will typically be (2n)*30% over that of -R 0) and doesn't give much of a gain. As a matter of fact, none of the -0..-9 options currently use it since -R > 1 is not consistently better like it should be. @@ -564,7 +574,7 @@ - -V + -V Verify the encoding process. With this option, flac will create a parallel decoder that decodes the output of the encoder and compares the result against the original. It will abort immediately with an error if a mismatch occurs. -V increases the total encoding time but is guaranteed to catch any unforseen bug in the encoding process. @@ -574,7 +584,7 @@ - -S-, -m-, -e-, -p-, -V-, --delete-input-file-, --lax- can all be used to turn off a particular option. + -S-, -m-, -e-, -p-, -V-, --delete-input-file-, --lax-, --sector-align- can all be used to turn off a particular option. @@ -590,7 +600,7 @@ - -fb | -fl + -fb | -fl Specify big-endian | little-endian byte order in the raw file. @@ -598,7 +608,7 @@ - -fc # + -fc # Specify the number of channels in the raw file. @@ -606,7 +616,7 @@ - -fp # + -fp # Specify the number of bits per sample in the raw file. @@ -614,7 +624,7 @@ - -fs # + -fs # Specify the sample rate of the raw file. @@ -622,7 +632,7 @@ - -fu + -fu Specify that the samples in the raw file are unsigned (the default is signed). @@ -630,7 +640,7 @@ - -fr + -fr Treat the input file (or output file if decoding) as a raw file, regardless of the extension. @@ -638,7 +648,7 @@ - -fw + -fw Treat the input file (or output file if decoding) as a RIFF WAVE file, regardless of the extension. diff --git a/man/flac.sgml b/man/flac.sgml index e120f09..a029389 100644 --- a/man/flac.sgml +++ b/man/flac.sgml @@ -4,7 +4,7 @@ Matt"> Zimmerman"> - February 23, 2001"> + June 26, 2001"> 1"> @@ -27,7 +27,7 @@ &dhsurname; - 2000 + 2001 &dhusername; &dhdate; @@ -176,7 +176,17 @@ Allow encoder to generate non-Subset - files + files. + + + + + + + + Align encoding of multiple CD format WAVE + files on sector boundaries. See the HTML + documentation for more information. @@ -199,7 +209,7 @@ samples in the input, there will be either no seek point entered (if the input size is determinable before encoding starts) or a placeholder point (if - input size is not determinable) + input size is not determinable). diff --git a/src/flac/encode.c b/src/flac/encode.c index 1f3dda1..cacfbbd 100644 --- a/src/flac/encode.c +++ b/src/flac/encode.c @@ -35,6 +35,7 @@ #endif #define min(x,y) ((x)<(y)?(x):(y)) +/* this MUST be >= 588 so that sector aligning can take place with one read */ #define CHUNK_OF_SAMPLES 2048 typedef enum { @@ -95,7 +96,8 @@ static FLAC__bool init_encoder(FLAC__bool lax, FLAC__bool do_mid_side, FLAC__boo static FLAC__bool convert_to_seek_table(char *requested_seek_points, int num_requested_seek_points, FLAC__uint64 stream_samples, unsigned blocksize, FLAC__StreamMetaData_SeekTable *seek_table); static void append_point_to_seek_table(FLAC__StreamMetaData_SeekTable *seek_table, FLAC__uint64 sample, FLAC__uint64 stream_samples, FLAC__uint64 blocksize); static int seekpoint_compare(const FLAC__StreamMetaData_SeekPoint *l, const FLAC__StreamMetaData_SeekPoint *r); -static void format_input(unsigned wide_samples, FLAC__bool is_big_endian, FLAC__bool is_unsigned_samples, unsigned channels, unsigned bps, encoder_wrapper_struct *encoder_wrapper); +static void format_input(FLAC__int32 *dest[], unsigned wide_samples, FLAC__bool is_big_endian, FLAC__bool is_unsigned_samples, unsigned channels, unsigned bps, encoder_wrapper_struct *encoder_wrapper); +static void append_to_verify_fifo(encoder_wrapper_struct *encoder_wrapper, const FLAC__int32 *input[], unsigned channels, unsigned wide_samples); static FLAC__StreamEncoderWriteStatus write_callback(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data); static void metadata_callback(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetaData *metadata, void *client_data); static FLAC__StreamDecoderReadStatus verify_read_callback(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data); @@ -108,7 +110,7 @@ static FLAC__bool read_little_endian_uint32(FILE *f, FLAC__uint32 *val, FLAC__bo static FLAC__bool write_big_endian_uint16(FILE *f, FLAC__uint16 val); static FLAC__bool write_big_endian_uint64(FILE *f, FLAC__uint64 val); -int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, FLAC__bool verbose, FLAC__uint64 skip, FLAC__bool verify, FLAC__bool lax, FLAC__bool do_mid_side, FLAC__bool loose_mid_side, FLAC__bool do_exhaustive_model_search, FLAC__bool do_qlp_coeff_prec_search, unsigned min_residual_partition_order, unsigned max_residual_partition_order, unsigned rice_parameter_search_dist, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned padding, char *requested_seek_points, int num_requested_seek_points) +int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, FLAC__int32 *align_reservoir[], unsigned *align_reservoir_samples, FLAC__bool sector_align, FLAC__bool is_last_file, FLAC__bool verbose, FLAC__uint64 skip, FLAC__bool verify, FLAC__bool lax, FLAC__bool do_mid_side, FLAC__bool loose_mid_side, FLAC__bool do_exhaustive_model_search, FLAC__bool do_qlp_coeff_prec_search, unsigned min_residual_partition_order, unsigned max_residual_partition_order, unsigned rice_parameter_search_dist, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned padding, char *requested_seek_points, int num_requested_seek_points) { encoder_wrapper_struct encoder_wrapper; FLAC__bool is_unsigned_samples = false; @@ -117,6 +119,10 @@ int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, cons FLAC__uint16 x; FLAC__uint32 xx; FLAC__bool got_fmt_chunk = false, got_data_chunk = false; + unsigned align_remainder = 0; + int info_align_carry = -1, info_align_zero = -1; + + FLAC__ASSERT(!sector_align || skip == 0); encoder_wrapper.encoder = 0; encoder_wrapper.verify = verify; @@ -180,6 +186,10 @@ int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, cons fprintf(stderr, "%s: ERROR: unsupported number channels %u\n", encoder_wrapper.inbasefilename, (unsigned)x); goto wav_abort_; } + else if(sector_align && x != 2) { + fprintf(stderr, "%s: ERROR: file has %u channels, must be 2 for --sector-align\n", encoder_wrapper.inbasefilename, (unsigned)x); + goto wav_abort_; + } channels = x; /* sample rate */ if(!read_little_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename)) @@ -188,6 +198,10 @@ int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, cons fprintf(stderr, "%s: ERROR: unsupported sample rate %u\n", encoder_wrapper.inbasefilename, (unsigned)xx); goto wav_abort_; } + else if(sector_align && xx != 44100) { + fprintf(stderr, "%s: ERROR: file's sample rate is %u, must be 44100 for --sector-align\n", encoder_wrapper.inbasefilename, (unsigned)xx); + goto wav_abort_; + } sample_rate = xx; /* avg bytes per second (ignored) */ if(!read_little_endian_uint32(infile, &xx, false, encoder_wrapper.inbasefilename)) @@ -245,14 +259,53 @@ int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, cons } data_bytes -= skip * bytes_per_wide_sample; - encoder_wrapper.total_samples_to_encode = data_bytes / bytes_per_wide_sample; - encoder_wrapper.unencoded_size = encoder_wrapper.total_samples_to_encode * bytes_per_wide_sample + 44; /* 44 for the size of the WAV headers */ + encoder_wrapper.total_samples_to_encode = data_bytes / bytes_per_wide_sample + *align_reservoir_samples; + if(sector_align) { + align_remainder = encoder_wrapper.total_samples_to_encode % 588; + if(is_last_file) + encoder_wrapper.total_samples_to_encode += (588-align_remainder); /* will pad with zeroes */ + else + encoder_wrapper.total_samples_to_encode -= align_remainder; /* will stop short and carry over to next file */ + } + + /* +44 for the size of the WAV headers; this is just an estimate for the progress indicator and doesn't need to be exact */ + encoder_wrapper.unencoded_size = encoder_wrapper.total_samples_to_encode * bytes_per_wide_sample + 44; if(!init_encoder(lax, do_mid_side, loose_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, min_residual_partition_order, max_residual_partition_order, rice_parameter_search_dist, max_lpc_order, blocksize, qlp_coeff_precision, channels, bps, sample_rate, padding, requested_seek_points, num_requested_seek_points, &encoder_wrapper)) goto wav_abort_; encoder_wrapper.verify_fifo.into_frames = true; + /* + * first do any samples in the reservoir + */ + if(sector_align && *align_reservoir_samples > 0) { + /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 2 here: */ + append_to_verify_fifo(&encoder_wrapper, align_reservoir, channels, *align_reservoir_samples); + + /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 2 here: */ + if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, align_reservoir, *align_reservoir_samples)) { + fprintf(stderr, "%s: ERROR during encoding, state = %d:%s\n", encoder_wrapper.inbasefilename, FLAC__stream_encoder_get_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder_wrapper.encoder)]); + goto wav_abort_; + } + } + + /* + * decrement the data_bytes counter if we need to align the file + */ + if(sector_align) { + if(is_last_file) { + *align_reservoir_samples = 0; + } + else { + *align_reservoir_samples = align_remainder; + data_bytes -= (*align_reservoir_samples) * bytes_per_wide_sample; + } + } + + /* + * now do from the file + */ while(data_bytes > 0) { bytes_read = fread(ucbuffer, sizeof(unsigned char), min(data_bytes, CHUNK_OF_SAMPLES * bytes_per_wide_sample), infile); if(bytes_read == 0) { @@ -272,7 +325,7 @@ int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, cons } else { unsigned wide_samples = bytes_read / bytes_per_wide_sample; - format_input(wide_samples, false, is_unsigned_samples, channels, bps, &encoder_wrapper); + format_input(input, wide_samples, false, is_unsigned_samples, channels, bps, &encoder_wrapper); /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 2 here: */ if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, input, wide_samples)) { @@ -283,6 +336,50 @@ int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, cons } } } + + /* + * now read unaligned samples into reservoir or pad with zeroes if necessary + */ + if(sector_align) { + if(is_last_file) { + unsigned wide_samples = 588 - align_remainder; + if(wide_samples < 588) { + unsigned channel; + + info_align_zero = wide_samples; + data_bytes = wide_samples * bytes_per_wide_sample; + for(channel = 0; channel < channels; channel++) + memset(input[channel], 0, data_bytes); + /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 2 here: */ + append_to_verify_fifo(&encoder_wrapper, input, channels, wide_samples); + + /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 2 here: */ + if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, input, wide_samples)) { + fprintf(stderr, "%s: ERROR during encoding, state = %d:%s\n", encoder_wrapper.inbasefilename, FLAC__stream_encoder_get_state(encoder_wrapper.encoder), FLAC__StreamEncoderStateString[FLAC__stream_encoder_get_state(encoder_wrapper.encoder)]); + goto wav_abort_; + } + } + } + else { + if(*align_reservoir_samples > 0) { + FLAC__ASSERT(CHUNK_OF_SAMPLES >= 588); + bytes_read = fread(ucbuffer, sizeof(unsigned char), (*align_reservoir_samples) * bytes_per_wide_sample, infile); + if(bytes_read == 0 && ferror(infile)) { + fprintf(stderr, "%s: ERROR during read\n", encoder_wrapper.inbasefilename); + goto wav_abort_; + } + else if(bytes_read != (*align_reservoir_samples) * bytes_per_wide_sample) { + fprintf(stderr, "%s: WARNING: unexpected EOF; expected %u samples, got %u samples\n", encoder_wrapper.inbasefilename, (unsigned)encoder_wrapper.total_samples_to_encode, (unsigned)encoder_wrapper.samples_written); + data_bytes = 0; + } + else { + info_align_carry = *align_reservoir_samples; + format_input(align_reservoir, *align_reservoir_samples, false, is_unsigned_samples, channels, bps, &encoder_wrapper); + } + } + } + } + got_data_chunk = true; } } @@ -331,6 +428,10 @@ int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, cons return 1; } } + if(info_align_carry >= 0) + fprintf(stderr, "%s: INFO: sector alignment causing %d samples to be carried over\n", encoder_wrapper.inbasefilename, info_align_carry); + if(info_align_zero >= 0) + fprintf(stderr, "%s: INFO: sector alignment causing %d zero samples to be appended\n", encoder_wrapper.inbasefilename, info_align_zero); if(infile != stdin) fclose(infile); return 0; @@ -358,12 +459,14 @@ wav_abort_: return 1; } -int flac__encode_raw(FILE *infile, long infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, FLAC__bool verbose, FLAC__uint64 skip, FLAC__bool verify, FLAC__bool lax, FLAC__bool do_mid_side, FLAC__bool loose_mid_side, FLAC__bool do_exhaustive_model_search, FLAC__bool do_qlp_coeff_prec_search, unsigned min_residual_partition_order, unsigned max_residual_partition_order, unsigned rice_parameter_search_dist, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned padding, char *requested_seek_points, int num_requested_seek_points, FLAC__bool is_big_endian, FLAC__bool is_unsigned_samples, unsigned channels, unsigned bps, unsigned sample_rate) +int flac__encode_raw(FILE *infile, long infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, FLAC__bool is_last_file, FLAC__bool verbose, FLAC__uint64 skip, FLAC__bool verify, FLAC__bool lax, FLAC__bool do_mid_side, FLAC__bool loose_mid_side, FLAC__bool do_exhaustive_model_search, FLAC__bool do_qlp_coeff_prec_search, unsigned min_residual_partition_order, unsigned max_residual_partition_order, unsigned rice_parameter_search_dist, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned padding, char *requested_seek_points, int num_requested_seek_points, FLAC__bool is_big_endian, FLAC__bool is_unsigned_samples, unsigned channels, unsigned bps, unsigned sample_rate) { encoder_wrapper_struct encoder_wrapper; size_t bytes_read; const size_t bytes_per_wide_sample = channels * (bps >> 3); + (void)is_last_file; + encoder_wrapper.encoder = 0; encoder_wrapper.verify = verify; encoder_wrapper.verbose = verbose; @@ -394,8 +497,8 @@ int flac__encode_raw(FILE *infile, long infilesize, const char *infilename, cons encoder_wrapper.total_samples_to_encode = encoder_wrapper.unencoded_size = 0; } else { - encoder_wrapper.unencoded_size = (unsigned)infilesize - skip * bytes_per_wide_sample; encoder_wrapper.total_samples_to_encode = (unsigned)infilesize / bytes_per_wide_sample - skip; + encoder_wrapper.unencoded_size = encoder_wrapper.total_samples_to_encode * bytes_per_wide_sample; } if(encoder_wrapper.verbose && encoder_wrapper.total_samples_to_encode <= 0) @@ -465,7 +568,7 @@ int flac__encode_raw(FILE *infile, long infilesize, const char *infilename, cons } else { unsigned wide_samples = bytes_read / bytes_per_wide_sample; - format_input(wide_samples, is_big_endian, is_unsigned_samples, channels, bps, &encoder_wrapper); + format_input(input, wide_samples, is_big_endian, is_unsigned_samples, channels, bps, &encoder_wrapper); /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 2 here: */ if(!FLAC__stream_encoder_process(encoder_wrapper.encoder, input, wide_samples)) { @@ -730,7 +833,7 @@ int seekpoint_compare(const FLAC__StreamMetaData_SeekPoint *l, const FLAC__Strea return 1; } -void format_input(unsigned wide_samples, FLAC__bool is_big_endian, FLAC__bool is_unsigned_samples, unsigned channels, unsigned bps, encoder_wrapper_struct *encoder_wrapper) +void format_input(FLAC__int32 *dest[], unsigned wide_samples, FLAC__bool is_big_endian, FLAC__bool is_unsigned_samples, unsigned channels, unsigned bps, encoder_wrapper_struct *encoder_wrapper) { unsigned wide_sample, sample, channel, byte; @@ -738,12 +841,12 @@ void format_input(unsigned wide_samples, FLAC__bool is_big_endian, FLAC__bool is 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] = (FLAC__int32)ucbuffer[sample] - 0x80; + dest[channel][wide_sample] = (FLAC__int32)ucbuffer[sample] - 0x80; } else { for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++) for(channel = 0; channel < channels; channel++, sample++) - input[channel][wide_sample] = (FLAC__int32)scbuffer[sample]; + dest[channel][wide_sample] = (FLAC__int32)scbuffer[sample]; } } else if(bps == 16) { @@ -759,12 +862,12 @@ void format_input(unsigned wide_samples, FLAC__bool is_big_endian, FLAC__bool is 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] = (FLAC__int32)usbuffer[sample] - 0x8000; + dest[channel][wide_sample] = (FLAC__int32)usbuffer[sample] - 0x8000; } else { for(sample = wide_sample = 0; wide_sample < wide_samples; wide_sample++) for(channel = 0; channel < channels; channel++, sample++) - input[channel][wide_sample] = (FLAC__int32)ssbuffer[sample]; + dest[channel][wide_sample] = (FLAC__int32)ssbuffer[sample]; } } else if(bps == 24) { @@ -780,18 +883,18 @@ void format_input(unsigned wide_samples, FLAC__bool is_big_endian, FLAC__bool is 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; + dest[channel][wide_sample] = ucbuffer[byte++]; dest[channel][wide_sample] <<= 8; + dest[channel][wide_sample] |= ucbuffer[byte++]; dest[channel][wide_sample] <<= 8; + dest[channel][wide_sample] |= ucbuffer[byte++]; + dest[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++]; + dest[channel][wide_sample] = scbuffer[byte++]; dest[channel][wide_sample] <<= 8; + dest[channel][wide_sample] |= ucbuffer[byte++]; dest[channel][wide_sample] <<= 8; + dest[channel][wide_sample] |= ucbuffer[byte++]; } } } @@ -799,9 +902,16 @@ void format_input(unsigned wide_samples, FLAC__bool is_big_endian, FLAC__bool is FLAC__ASSERT(0); } + /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 2 here: */ + append_to_verify_fifo(encoder_wrapper, dest, channels, wide_samples); +} + +void append_to_verify_fifo(encoder_wrapper_struct *encoder_wrapper, const FLAC__int32 *src[], unsigned channels, unsigned wide_samples) +{ if(encoder_wrapper->verify) { + unsigned channel; for(channel = 0; channel < channels; channel++) - memcpy(&encoder_wrapper->verify_fifo.original[channel][encoder_wrapper->verify_fifo.tail], &input[channel][0], sizeof(FLAC__int32) * wide_samples); + memcpy(&encoder_wrapper->verify_fifo.original[channel][encoder_wrapper->verify_fifo.tail], src[channel], sizeof(FLAC__int32) * wide_samples); encoder_wrapper->verify_fifo.tail += wide_samples; FLAC__ASSERT(encoder_wrapper->verify_fifo.tail <= encoder_wrapper->verify_fifo.size); } diff --git a/src/flac/encode.h b/src/flac/encode.h index d2de921..e3ec233 100644 --- a/src/flac/encode.h +++ b/src/flac/encode.h @@ -21,7 +21,7 @@ #include "FLAC/ordinals.h" -int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, FLAC__bool verbose, FLAC__uint64 skip, FLAC__bool verify, FLAC__bool lax, FLAC__bool do_mid_side, FLAC__bool loose_mid_side, FLAC__bool do_exhaustive_model_search, FLAC__bool do_qlp_coeff_prec_search, unsigned min_residual_partition_order, unsigned max_residual_partition_order, unsigned rice_parameter_search_dist, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned padding, char *requested_seek_points, int num_requested_seek_points); -int flac__encode_raw(FILE *infile, long infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, FLAC__bool verbose, FLAC__uint64 skip, FLAC__bool verify, FLAC__bool lax, FLAC__bool do_mid_side, FLAC__bool loose_mid_side, FLAC__bool do_exhaustive_model_search, FLAC__bool do_qlp_coeff_prec_search, unsigned min_residual_partition_order, unsigned max_residual_partition_order, unsigned rice_parameter_search_dist, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned padding, char *requested_seek_points, int num_requested_seek_points, FLAC__bool is_big_endian, FLAC__bool is_unsigned_samples, unsigned channels, unsigned bps, unsigned sample_rate); +int flac__encode_wav(FILE *infile, long infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, FLAC__int32 *align_reservoir[], unsigned *align_reservoir_samples, FLAC__bool sector_align, FLAC__bool is_last_file, FLAC__bool verbose, FLAC__uint64 skip, FLAC__bool verify, FLAC__bool lax, FLAC__bool do_mid_side, FLAC__bool loose_mid_side, FLAC__bool do_exhaustive_model_search, FLAC__bool do_qlp_coeff_prec_search, unsigned min_residual_partition_order, unsigned max_residual_partition_order, unsigned rice_parameter_search_dist, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned padding, char *requested_seek_points, int num_requested_seek_points); +int flac__encode_raw(FILE *infile, long infilesize, const char *infilename, const char *outfilename, const FLAC__byte *lookahead, unsigned lookahead_length, FLAC__bool is_last_file, FLAC__bool verbose, FLAC__uint64 skip, FLAC__bool verify, FLAC__bool lax, FLAC__bool do_mid_side, FLAC__bool loose_mid_side, FLAC__bool do_exhaustive_model_search, FLAC__bool do_qlp_coeff_prec_search, unsigned min_residual_partition_order, unsigned max_residual_partition_order, unsigned rice_parameter_search_dist, unsigned max_lpc_order, unsigned blocksize, unsigned qlp_coeff_precision, unsigned padding, char *requested_seek_points, int num_requested_seek_points, FLAC__bool is_big_endian, FLAC__bool is_unsigned_samples, unsigned channels, unsigned bps, unsigned sample_rate); #endif diff --git a/src/flac/main.c b/src/flac/main.c index 4bd64ff..f0b3a08 100644 --- a/src/flac/main.c +++ b/src/flac/main.c @@ -32,12 +32,12 @@ #include "file.h" static int usage(const char *message, ...); -static int encode_file(const char *infilename, const char *forced_outfilename); +static int encode_file(const char *infilename, const char *forced_outfilename, FLAC__bool is_last_file); static int decode_file(const char *infilename, const char *forced_outfilename); FLAC__bool verify = false, verbose = true, lax = false, test_only = false, analyze = false; FLAC__bool do_mid_side = true, loose_mid_side = false, do_exhaustive_model_search = false, do_qlp_coeff_prec_search = false; -FLAC__bool force_to_stdout = false, delete_input = false; +FLAC__bool force_to_stdout = false, delete_input = false, sector_align = false; const char *cmdline_forced_outfilename = 0; analysis_options aopts = { false, false }; unsigned padding = 0; @@ -49,6 +49,9 @@ int format_channels = -1, format_bps = -1, format_sample_rate = -1; int blocksize = -1, min_residual_partition_order = -1, max_residual_partition_order = -1, rice_parameter_search_dist = -1; char requested_seek_points[50000]; /* @@@ bad MAGIC NUMBER */ int num_requested_seek_points = -1; /* -1 => no -S options were given, 0 => -S- was given */ +FLAC__int32 align_reservoir_0[588], align_reservoir_1[588]; /* for carrying over samples from --sector-align */ +FLAC__int32 *align_reservoir[2] = { align_reservoir_0, align_reservoir_1 }; +unsigned align_reservoir_samples = 0; /* 0 .. 587 */ int main(int argc, char *argv[]) { @@ -93,6 +96,10 @@ int main(int argc, char *argv[]) delete_input = true; else if(0 == strcmp(argv[i], "--delete-input-file-")) delete_input = false; + else if(0 == strcmp(argv[i], "--sector-align")) + sector_align = true; + else if(0 == strcmp(argv[i], "--sector-align-")) + sector_align = false; else if(0 == strcmp(argv[i], "--skip")) skip = (FLAC__uint64)atoi(argv[++i]); /* @@@ takes a pretty damn big file to overflow atoi() here, but it could happen */ else if(0 == strcmp(argv[i], "--lax")) @@ -322,6 +329,17 @@ int main(int argc, char *argv[]) return usage("ERROR: invalid value for -q '%u', must be 0 or >= %u\n", qlp_coeff_precision, FLAC__MIN_QLP_COEFF_PRECISION); } + if(sector_align) { + if(mode_decode) + return usage("ERROR: --sector-align only allowed for encoding\n"); + else if(skip > 0) + return usage("ERROR: --sector-align not allowed with --skip\n"); + else if(format_channels >= 0 && format_channels != 2) + return usage("ERROR: --sector-align can only be done with stereo input\n"); + else if(format_sample_rate >= 0 && format_sample_rate != 2) + return usage("ERROR: --sector-align can only be done with sample rate of 44100\n"); + } + if(verbose) { fprintf(stderr, "\n"); fprintf(stderr, "flac %s, Copyright (C) 2000,2001 Josh Coalson\n", FLAC__VERSION_STRING); @@ -329,8 +347,8 @@ int main(int argc, char *argv[]) fprintf(stderr, "welcome to redistribute it under certain conditions. Type `flac' for details.\n\n"); if(!mode_decode) { - fprintf(stderr, "options:%s%s -P %u -b %u%s -l %u%s%s -q %u -r %u,%u -R %u%s\n", - delete_input?" --delete-input-file":"", lax?" --lax":"", + fprintf(stderr, "options:%s%s%s -P %u -b %u%s -l %u%s%s -q %u -r %u,%u -R %u%s\n", + delete_input?" --delete-input-file":"", sector_align?" --sector-align":"", lax?" --lax":"", padding, (unsigned)blocksize, loose_mid_side?" -M":do_mid_side?" -m":"", max_lpc_order, do_exhaustive_model_search?" -e":"", do_qlp_coeff_prec_search?" -p":"", qlp_coeff_precision, @@ -365,7 +383,7 @@ int main(int argc, char *argv[]) int save_format; if(i == argc) { - retval = encode_file("-", 0); + retval = encode_file("-", 0, true); } else { if(i + 1 != argc) @@ -374,7 +392,7 @@ int main(int argc, char *argv[]) if(0 == strcmp(argv[i], "-") && !first) continue; save_format = format_is_wave; - retval = encode_file(argv[i], 0); + retval = encode_file(argv[i], 0, i == (argc-1)); format_is_wave = save_format; first = false; } @@ -451,6 +469,7 @@ int usage(const char *message, ...) fprintf(stderr, " --a-rgp : generate gnuplot files of residual distribution of each subframe\n"); fprintf(stderr, "encoding options:\n"); fprintf(stderr, " --lax : allow encoder to generate non-Subset files\n"); + fprintf(stderr, " --sector-align : align encoding of multiple files on sector boundaries\n"); fprintf(stderr, " -S { # | X | #x } : include a point or points in a SEEKTABLE\n"); fprintf(stderr, " # : a specific sample number for a seek point\n"); fprintf(stderr, " X : a placeholder point (always goes at the end of the SEEKTABLE)\n"); @@ -494,8 +513,8 @@ int usage(const char *message, ...) fprintf(stderr, " -R # : Rice parameter search distance (# is 0..32; above 2 doesn't help much)\n"); fprintf(stderr, " -V : verify a correct encoding by decoding the output in parallel and\n"); fprintf(stderr, " comparing to the original\n"); - fprintf(stderr, " -S-, -m-, -M-, -e-, -p-, -V-, --delete-input-file-, --lax- can all be used to\n"); - fprintf(stderr, " turn off a particular option\n"); + fprintf(stderr, " -S-, -m-, -M-, -e-, -p-, -V-, --delete-input-file-, --lax-, --sector-align-\n"); + fprintf(stderr, " can all be used to turn off a particular option\n"); fprintf(stderr, "format options:\n"); fprintf(stderr, " -fb | -fl : big-endian | little-endian byte order\n"); fprintf(stderr, " -fc channels\n"); @@ -508,7 +527,7 @@ int usage(const char *message, ...) return message? 1 : 0; } -int encode_file(const char *infilename, const char *forced_outfilename) +int encode_file(const char *infilename, const char *forced_outfilename, FLAC__bool is_last_file) { FILE *encode_infile; char outfilename[4096]; /* @@@ bad MAGIC NUMBER */ @@ -530,6 +549,11 @@ int encode_file(const char *infilename, const char *forced_outfilename) } } + if(sector_align && !format_is_wave && infilesize < 0) { + fprintf(stderr, "ERROR: can't --sector-align when the input size is unknown\n"); + return 1; + } + if(format_is_wave < 0) { /* first set format based on name */ if(0 == strcasecmp(infilename+(strlen(infilename)-4), ".wav")) @@ -578,9 +602,9 @@ int encode_file(const char *infilename, const char *forced_outfilename) forced_outfilename = cmdline_forced_outfilename; if(format_is_wave) - retval = flac__encode_wav(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, verbose, skip, verify, lax, do_mid_side, loose_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, min_residual_partition_order, max_residual_partition_order, rice_parameter_search_dist, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision, padding, requested_seek_points, num_requested_seek_points); + retval = flac__encode_wav(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, align_reservoir, &align_reservoir_samples, sector_align, is_last_file, verbose, skip, verify, lax, do_mid_side, loose_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, min_residual_partition_order, max_residual_partition_order, rice_parameter_search_dist, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision, padding, requested_seek_points, num_requested_seek_points); else - retval = flac__encode_raw(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, verbose, skip, verify, lax, do_mid_side, loose_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, min_residual_partition_order, max_residual_partition_order, rice_parameter_search_dist, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision, padding, requested_seek_points, num_requested_seek_points, format_is_big_endian, format_is_unsigned_samples, format_channels, format_bps, format_sample_rate); + retval = flac__encode_raw(encode_infile, infilesize, infilename, forced_outfilename, lookahead, lookahead_length, is_last_file, verbose, skip, verify, lax, do_mid_side, loose_mid_side, do_exhaustive_model_search, do_qlp_coeff_prec_search, min_residual_partition_order, max_residual_partition_order, rice_parameter_search_dist, max_lpc_order, (unsigned)blocksize, qlp_coeff_precision, padding, requested_seek_points, num_requested_seek_points, format_is_big_endian, format_is_unsigned_samples, format_channels, format_bps, format_sample_rate); if(retval == 0 && strcmp(infilename, "-")) { if(strcmp(forced_outfilename, "-")) -- 2.7.4