add --sector-align option
authorJosh Coalson <jcoalson@users.sourceforce.net>
Wed, 27 Jun 2001 06:48:34 +0000 (06:48 +0000)
committerJosh Coalson <jcoalson@users.sourceforce.net>
Wed, 27 Jun 2001 06:48:34 +0000 (06:48 +0000)
doc/documentation.html
man/flac.sgml
src/flac/encode.c
src/flac/encode.h
src/flac/main.c

index 5baa07b..b23d76f 100644 (file)
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               --a-rtext
+                               <TT>--a-rtext</TT>
                        </TD>
                        <TD>
                                Includes the residual signal in the analysis file.  This will make the file <B>very</B> big, much larger than even the decoded file.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               --a-rgp
+                               <TT>--a-rgp</TT>
                        </TD>
                        <TD>
                                Generates a gnuplot file for every subframe; each file will contain the residual distribution of the subframe.  This will create a <B>lot</B> of files.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               --lax
+                               <TT>--lax</TT>
                        </TD>
                        <TD>
                                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.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -S { # | X | #x }
+                               <TT>--sector-align</TT>
+                       </TD>
+                       <TD>
+                               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 <TT>--sector-align</TT>, 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.<P>
+                               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).  <TT><B>flac</B></TT> can only align a set of files given in one invocation of <TT><B>flac</B></TT>.<P>
+                               <B>WARNING:</B> The ordering of files is important!  If you give a command like '<TT>flac --sector-align *.wav</TT>' the shell may not expand the wildcard to the order you expect.  To be safe you should '<TT>echo *.wav</TT>' first to confirm the order, or be explicit like '<TT>flac --sector-align 8.wav 9.wav 10.wav</TT>'.
+                       </TD>
+               </TR>
+               <TR>
+                       <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
+                               <TT>-S { # | X | #x }</TT>
                        </TD>
                        <TD>
                                Include a point or points in a SEEKTABLE:<BR>
                                </UL>
                                You may use many -S options; the resulting SEEKTABLE will be the unique-ified union of all such values.<BR>
                                With no -S options, flac defaults to '-S 100x'.  Use -S- for no SEEKTABLE.<BR>
-                               NOTE: -S #x will not work if the encoder can't determine the input size before starting.<BR>
-                               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).<BR>
+                               <B>NOTE:</B> -S #x will not work if the encoder can't determine the input size before starting.<BR>
+                               <B>NOTE:</B> 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).<BR>
                        </TD>
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -P #
+                               <TT>-P #</TT>
                        </TD>
                        <TD>
                                  Tell the encoder to write a <TT>PADDING</TT> metadata block of the given length (in bytes) after the <TT>STREAMINFO</TT> block.  <TT>-P 0</TT> implies no <TT>PADDING</TT> block, which is the default.  This is useful if you plan to tag the file later with an <TT>APPLICATION</TT> block; instead of having to rewrite the entire file later just to insert your block, you can write directly over the <TT>PADDING</TT> block.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -b #
+                               <TT>-b #</TT>
                        </TD>
                        <TD>
                                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.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -m
+                               <TT>-m</TT>
                        </TD>
                        <TD>
                                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.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -M
+                               <TT>-M</TT>
                        </TD>
                        <TD>
                                Enable loose mid-side coding (only for stereo streams).  Like <TT>-m</TT> but the encoder adaptively switches between independent and mid-side coding, which is faster but yields less compression than <TT>-m</TT> (which does an exhaustive search).
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -0 .. -9
+                               <TT>-0 .. -9</TT>
                        </TD>
                        <TD>
                                Fastest compression .. highest compression.  The default is <TT>-5</TT>.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -0
+                               <TT>-0</TT>
                        </TD>
                        <TD>
                                Synonymous with -l 0 -b 1152 -r 2,2
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -1
+                               <TT>-1</TT>
                        </TD>
                        <TD>
                                Synonymous with -l 0 -b 1152 -M -r 2,2
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -2
+                               <TT>-2</TT>
                        </TD>
                        <TD>
                                Synonymous with -l 0 -b 1152 -m -r 3
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -3
+                               <TT>-3</TT>
                        </TD>
                        <TD>
                                Synonymous with -l 6 -b 4608 -r 3,3
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -4
+                               <TT>-4</TT>
                        </TD>
                        <TD>
                                Synonymous with -l 8 -b 4608 -M -r 3,3
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -5
+                               <TT>-5</TT>
                        </TD>
                        <TD>
                                Synonymous with -l 8 -b 4608 -m -r 3,3
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -6
+                               <TT>-6</TT>
                        </TD>
                        <TD>
                                Synonymous with -l 8 -b 4608 -m -r 4
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -7
+                               <TT>-7</TT>
                        </TD>
                        <TD>
                                Synonymous with -l 8 -b 4608 -m -e -r 6
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -8
+                               <TT>-8</TT>
                        </TD>
                        <TD>
                                Synonymous with -l 12 -b 4608 -m -e -r 6
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -9
+                               <TT>-9</TT>
                        </TD>
                        <TD>
                                Synonymous with -l 32 -b 4608 -m -e -r 16 -p.  This is painfully slow but gives you the maximum compression <B><TT>flac</TT></B> can do for the given block size.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -e
+                               <TT>-e</TT>
                        </TD>
                        <TD>
                                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%.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -l #
+                               <TT>-l #</TT>
                        </TD>
                        <TD>
                                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.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -q #
+                               <TT>-q #</TT>
                        </TD>
                        <TD>
                                Specifies the precision of the quantized LP coefficients, in bits.  The default is <B><TT>-q 0</TT></B>, 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.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -p
+                               <TT>-p</TT>
                        </TD>
                        <TD>
                                Do exhaustive LP coefficient quantization optimization.  This option overrides any <B><TT>-q</TT></B> option.  It is expensive and typically will only improve the compression a tiny fraction of a percent.  <B><TT>-q</TT></B> has no effect when <B><TT>-l 0</TT></B> is used.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -r [#,]#
+                               <TT>-r [#,]#</TT>
                        </TD>
                        <TD>
-                               Set the [min,]max residual partition order.  The min value defaults to 0 if unspecified.<BR>
+                               Set the [min,]max residual partition order.  The min value defaults to 0 if unspecified.<P>
                                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 <B><TT>-r 2,2</TT></B> (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 <B><TT>-r 0,16</TT></B> to force the highest degree of optimization.
                        </TD>
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -R #
+                               <TT>-R #</TT>
                        </TD>
                        <TD>
                                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.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -V
+                               <TT>-V</TT>
                        </TD>
                        <TD>
                                Verify the encoding process.  With this option, <B><TT>flac</TT></B> 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.  <B><TT>-V</TT></B> increases the total encoding time but is guaranteed to catch any unforseen bug in the encoding process.
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
                        </TD>
                        <TD>
-                               <TT>-S-</TT>, <TT>-m-</TT>, <TT>-e-</TT>, <TT>-p-</TT>, <TT>-V-</TT>, <TT>--delete-input-file-</TT>, <TT>--lax-</TT> can all be used to turn off a particular option.
+                               <TT>-S-</TT>, <TT>-m-</TT>, <TT>-e-</TT>, <TT>-p-</TT>, <TT>-V-</TT>, <TT>--delete-input-file-</TT>, <TT>--lax-</TT>, <TT>--sector-align-</TT> can all be used to turn off a particular option.
                        </TD>
                </TR>
        </TABLE>
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -fb | -fl
+                               <TT>-fb | -fl</TT>
                        </TD>
                        <TD>
                                Specify big-endian | little-endian byte order in the raw file.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -fc #
+                               <TT>-fc #</TT>
                        </TD>
                        <TD>
                                Specify the number of channels in the raw file.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -fp #
+                               <TT>-fp #</TT>
                        </TD>
                        <TD>
                                Specify the number of bits per sample in the raw file.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -fs #
+                               <TT>-fs #</TT>
                        </TD>
                        <TD>
                                Specify the sample rate of the raw file.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -fu
+                               <TT>-fu</TT>
                        </TD>
                        <TD>
                                Specify that the samples in the raw file are unsigned (the default is signed).
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -fr
+                               <TT>-fr</TT>
                        </TD>
                        <TD>
                                Treat the input file (or output file if decoding) as a raw file, regardless of the extension.
                </TR>
                <TR>
                        <TD NOWRAP ALIGN="RIGHT" VALIGN="TOP" BGCOLOR="#F4F4CC">
-                               -fw
+                               <TT>-fw</TT>
                        </TD>
                        <TD>
                                Treat the input file (or output file if decoding) as a RIFF WAVE file, regardless of the extension.
index e120f09..a029389 100644 (file)
@@ -4,7 +4,7 @@
   <!ENTITY dhfirstname "<firstname>Matt</firstname>">
   <!ENTITY dhsurname   "<surname>Zimmerman</surname>">
   <!-- Please adjust the date whenever revising the manpage. -->
-  <!ENTITY dhdate      "<date>February 23, 2001</date>">
+  <!ENTITY dhdate      "<date>June 26, 2001</date>">
   <!-- SECTION should be 1-8, maybe w/ subsection other parameters are
        allowed: see man(7), man(1). -->
   <!ENTITY dhsection   "<manvolnum>1</manvolnum>">
@@ -27,7 +27,7 @@
        &dhsurname;
       </author>
        <copyright>
-                   <year>2000</year>
+                   <year>2001</year>
                    <holder>&dhusername;</holder>
        </copyright>
            &dhdate;
 
                <listitem>
                  <para>Allow encoder to generate non-Subset
-                   files</para>
+                   files.</para>
+               </listitem>
+             </varlistentry>
+
+             <varlistentry>
+               <term><option>--sector-align</option></term>
+
+               <listitem>
+                 <para>Align encoding of multiple CD format WAVE
+                   files on sector boundaries.  See the HTML
+                   documentation for more information.</para>
                </listitem>
              </varlistentry>
 
                    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)</para>
+                   input size is not determinable).</para>
                </listitem>
              </varlistentry>
 
index 1f3dda1..cacfbbd 100644 (file)
@@ -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);
        }
index d2de921..e3ec233 100644 (file)
@@ -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
index 4bd64ff..f0b3a08 100644 (file)
 #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, "-"))