1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003,2004,2005 Josh Coalson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef FLAC__STREAM_ENCODER_H
33 #define FLAC__STREAM_ENCODER_H
37 #include "stream_decoder.h"
44 /** \file include/FLAC/stream_encoder.h
47 * This module contains the functions which implement the stream
50 * See the detailed documentation in the
51 * \link flac_stream_encoder stream encoder \endlink module.
54 /** \defgroup flac_encoder FLAC/ *_encoder.h: encoder interfaces
58 * This module describes the two encoder layers provided by libFLAC.
60 * For encoding FLAC streams, libFLAC provides three layers of access. The
61 * lowest layer is non-seekable stream-level encoding, the next is seekable
62 * stream-level encoding, and the highest layer is file-level encoding. The
63 * interfaces are described in the \link flac_stream_encoder stream encoder
64 * \endlink, \link flac_seekable_stream_encoder seekable stream encoder
65 * \endlink, and \link flac_file_encoder file encoder \endlink modules
66 * respectively. Typically you will choose the highest layer that your input
67 * source will support.
68 * The stream encoder relies on callbacks for writing the data and
69 * metadata. The file encoder provides these callbacks internally and you
70 * need only supply the filename.
72 * The stream encoder relies on callbacks for writing the data and has no
73 * provisions for seeking the output. The seekable stream encoder wraps
74 * the stream encoder and also automaticallay handles the writing back of
75 * metadata discovered while encoding. However, you must provide extra
76 * callbacks for seek-related operations on your output, like seek and
77 * tell. The file encoder wraps the seekable stream encoder and supplies
78 * all of the callbacks internally, simplifying the processing of standard
79 * files. The only callback exposed is for progress reporting, and that
83 /** \defgroup flac_stream_encoder FLAC/stream_encoder.h: stream encoder interface
84 * \ingroup flac_encoder
87 * This module contains the functions which implement the stream
90 * The basic usage of this encoder is as follows:
91 * - The program creates an instance of an encoder using
92 * FLAC__stream_encoder_new().
93 * - The program overrides the default settings and sets callbacks using
94 * FLAC__stream_encoder_set_*() functions.
95 * - The program initializes the instance to validate the settings and
96 * prepare for encoding using FLAC__stream_encoder_init().
97 * - The program calls FLAC__stream_encoder_process() or
98 * FLAC__stream_encoder_process_interleaved() to encode data, which
99 * subsequently calls the callbacks when there is encoder data ready
101 * - The program finishes the encoding with FLAC__stream_encoder_finish(),
102 * which causes the encoder to encode any data still in its input pipe,
103 * call the metadata callback with the final encoding statistics, and
104 * finally reset the encoder to the uninitialized state.
105 * - The instance may be used again or deleted with
106 * FLAC__stream_encoder_delete().
108 * In more detail, the stream encoder functions similarly to the
109 * \link flac_stream_decoder stream decoder \endlink, but has fewer
110 * callbacks and more options. Typically the user will create a new
111 * instance by calling FLAC__stream_encoder_new(), then set the necessary
112 * parameters and callbacks with FLAC__stream_encoder_set_*(), and
113 * initialize it by calling FLAC__stream_encoder_init().
115 * Unlike the decoders, the stream encoder has many options that can
116 * affect the speed and compression ratio. When setting these parameters
117 * you should have some basic knowledge of the format (see the
118 * <A HREF="../documentation.html#format">user-level documentation</A>
119 * or the <A HREF="../format.html">formal description</A>). The
120 * FLAC__stream_encoder_set_*() functions themselves do not validate the
121 * values as many are interdependent. The FLAC__stream_encoder_init()
122 * function will do this, so make sure to pay attention to the state
123 * returned by FLAC__stream_encoder_init() to make sure that it is
124 * FLAC__STREAM_ENCODER_OK. Any parameters that are not set before
125 * FLAC__stream_encoder_init() will take on the defaults from the
128 * The user must provide function pointers for the following callbacks:
130 * - Write callback - This function is called by the encoder anytime there
131 * is raw encoded data to write. It may include metadata mixed with
132 * encoded audio frames and the data is not guaranteed to be aligned on
133 * frame or metadata block boundaries.
134 * - Metadata callback - This function is called once at the end of
135 * encoding with the populated STREAMINFO structure. This is so file
136 * encoders can seek back to the beginning of the file and write the
137 * STREAMINFO block with the correct statistics after encoding (like
138 * minimum/maximum frame size).
140 * The call to FLAC__stream_encoder_init() currently will also immediately
141 * call the write callback several times, once with the \c fLaC signature,
142 * and once for each encoded metadata block.
144 * After initializing the instance, the user may feed audio data to the
145 * encoder in one of two ways:
147 * - Channel separate, through FLAC__stream_encoder_process() - The user
148 * will pass an array of pointers to buffers, one for each channel, to
149 * the encoder, each of the same length. The samples need not be
151 * - Channel interleaved, through
152 * FLAC__stream_encoder_process_interleaved() - The user will pass a single
153 * pointer to data that is channel-interleaved (i.e. channel0_sample0,
154 * channel1_sample0, ... , channelN_sample0, channel0_sample1, ...).
155 * Again, the samples need not be block-aligned but they must be
156 * sample-aligned, i.e. the first value should be channel0_sample0 and
157 * the last value channelN_sampleM.
159 * When the user is finished encoding data, it calls
160 * FLAC__stream_encoder_finish(), which causes the encoder to encode any
161 * data still in its input pipe, and call the metadata callback with the
162 * final encoding statistics. Then the instance may be deleted with
163 * FLAC__stream_encoder_delete() or initialized again to encode another
166 * For programs that write their own metadata, but that do not know the
167 * actual metadata until after encoding, it is advantageous to instruct
168 * the encoder to write a PADDING block of the correct size, so that
169 * instead of rewriting the whole stream after encoding, the program can
170 * just overwrite the PADDING block. If only the maximum size of the
171 * metadata is known, the program can write a slightly larger padding
172 * block, then split it after encoding.
174 * Make sure you understand how lengths are calculated. All FLAC metadata
175 * blocks have a 4 byte header which contains the type and length. This
176 * length does not include the 4 bytes of the header. See the format page
177 * for the specification of metadata blocks and their lengths.
180 * The "set" functions may only be called when the encoder is in the
181 * state FLAC__STREAM_ENCODER_UNINITIALIZED, i.e. after
182 * FLAC__stream_encoder_new() or FLAC__stream_encoder_finish(), but
183 * before FLAC__stream_encoder_init(). If this is the case they will
184 * return \c true, otherwise \c false.
187 * FLAC__stream_encoder_finish() resets all settings to the constructor
188 * defaults, including the callbacks.
194 /** State values for a FLAC__StreamEncoder
196 * The encoder's state can be obtained by calling FLAC__stream_encoder_get_state().
200 FLAC__STREAM_ENCODER_OK = 0,
201 /**< The encoder is in the normal OK state. */
203 FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR,
204 /**< An error occurred in the underlying verify stream decoder;
205 * check FLAC__stream_encoder_get_verify_decoder_state().
208 FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA,
209 /**< The verify decoder detected a mismatch between the original
210 * audio signal and the decoded audio signal.
213 FLAC__STREAM_ENCODER_INVALID_CALLBACK,
214 /**< The encoder was initialized before setting all the required callbacks. */
216 FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS,
217 /**< The encoder has an invalid setting for number of channels. */
219 FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE,
220 /**< The encoder has an invalid setting for bits-per-sample.
221 * FLAC supports 4-32 bps but the reference encoder currently supports
225 FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE,
226 /**< The encoder has an invalid setting for the input sample rate. */
228 FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE,
229 /**< The encoder has an invalid setting for the block size. */
231 FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER,
232 /**< The encoder has an invalid setting for the maximum LPC order. */
234 FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION,
235 /**< The encoder has an invalid setting for the precision of the quantized linear predictor coefficients. */
237 FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH,
238 /**< Mid/side coding was specified but the number of channels is not equal to 2. */
240 FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH,
243 FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE,
244 /**< Loose mid/side coding was specified but mid/side coding was not. */
246 FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER,
247 /**< The specified block size is less than the maximum LPC order. */
249 FLAC__STREAM_ENCODER_NOT_STREAMABLE,
250 /**< The encoder is bound to the "streamable subset" but other settings violate it. */
252 FLAC__STREAM_ENCODER_FRAMING_ERROR,
253 /**< An error occurred while writing the stream; usually, the write_callback returned an error. */
255 FLAC__STREAM_ENCODER_INVALID_METADATA,
256 /**< The metadata input to the encoder is invalid, in one of the following ways:
257 * - FLAC__stream_encoder_set_metadata() was called with a null pointer but a block count > 0
258 * - One of the metadata blocks contains an undefined type
259 * - It contains an illegal CUESHEET as checked by FLAC__format_cuesheet_is_legal()
260 * - It contains an illegal SEEKTABLE as checked by FLAC__format_seektable_is_legal()
261 * - It contains more than one SEEKTABLE block or more than one VORBIS_COMMENT block
264 FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING,
265 /**< An error occurred while writing the stream; usually, the write_callback returned an error. */
267 FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING,
268 /**< The write_callback returned an error. */
270 FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR,
271 /**< Memory allocation failed. */
273 FLAC__STREAM_ENCODER_ALREADY_INITIALIZED,
274 /**< FLAC__stream_encoder_init() was called when the encoder was
275 * already initialized, usually because
276 * FLAC__stream_encoder_finish() was not called.
279 FLAC__STREAM_ENCODER_UNINITIALIZED
280 /**< The encoder is in the uninitialized state. */
282 } FLAC__StreamEncoderState;
284 /** Maps a FLAC__StreamEncoderState to a C string.
286 * Using a FLAC__StreamEncoderState as the index to this array
287 * will give the string equivalent. The contents should not be modified.
289 extern FLAC_API const char * const FLAC__StreamEncoderStateString[];
291 /** Return values for the FLAC__StreamEncoder write callback.
295 FLAC__STREAM_ENCODER_WRITE_STATUS_OK = 0,
296 /**< The write was OK and encoding can continue. */
298 FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR
299 /**< An unrecoverable error occurred. The encoder will return from the process call. */
301 } FLAC__StreamEncoderWriteStatus;
303 /** Maps a FLAC__StreamEncoderWriteStatus to a C string.
305 * Using a FLAC__StreamEncoderWriteStatus as the index to this array
306 * will give the string equivalent. The contents should not be modified.
308 extern FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[];
311 /***********************************************************************
313 * class FLAC__StreamEncoder
315 ***********************************************************************/
317 struct FLAC__StreamEncoderProtected;
318 struct FLAC__StreamEncoderPrivate;
319 /** The opaque structure definition for the stream encoder type.
320 * See the \link flac_stream_encoder stream encoder module \endlink
321 * for a detailed description.
324 struct FLAC__StreamEncoderProtected *protected_; /* avoid the C++ keyword 'protected' */
325 struct FLAC__StreamEncoderPrivate *private_; /* avoid the C++ keyword 'private' */
326 } FLAC__StreamEncoder;
328 /** Signature for the write callback.
329 * See FLAC__stream_encoder_set_write_callback() for more info.
331 * \param encoder The encoder instance calling the callback.
332 * \param buffer An array of encoded data of length \a bytes.
333 * \param bytes The byte length of \a buffer.
334 * \param samples The number of samples encoded by \a buffer.
335 * \c 0 has a special meaning; see
336 * FLAC__stream_encoder_set_write_callback().
337 * \param current_frame The number of the current frame being encoded.
338 * \param client_data The callee's client data set through
339 * FLAC__stream_encoder_set_client_data().
340 * \retval FLAC__StreamEncoderWriteStatus
341 * The callee's return status.
343 typedef FLAC__StreamEncoderWriteStatus (*FLAC__StreamEncoderWriteCallback)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
345 /** Signature for the metadata callback.
346 * See FLAC__stream_encoder_set_metadata_callback() for more info.
348 * \param encoder The encoder instance calling the callback.
349 * \param metadata The final populated STREAMINFO block.
350 * \param client_data The callee's client data set through
351 * FLAC__stream_encoder_set_client_data().
353 typedef void (*FLAC__StreamEncoderMetadataCallback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data);
356 /***********************************************************************
358 * Class constructor/destructor
360 ***********************************************************************/
362 /** Create a new stream encoder instance. The instance is created with
363 * default settings; see the individual FLAC__stream_encoder_set_*()
364 * functions for each setting's default.
366 * \retval FLAC__StreamEncoder*
367 * \c NULL if there was an error allocating memory, else the new instance.
369 FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new();
371 /** Free an encoder instance. Deletes the object pointed to by \a encoder.
373 * \param encoder A pointer to an existing encoder.
375 * \code encoder != NULL \endcode
377 FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder);
380 /***********************************************************************
382 * Public class method prototypes
384 ***********************************************************************/
386 /** Set the "verify" flag. If \c true, the encoder will verify it's own
387 * encoded output by feeding it through an internal decoder and comparing
388 * the original signal against the decoded signal. If a mismatch occurs,
389 * the process call will return \c false. Note that this will slow the
390 * encoding process by the extra time required for decoding and comparison.
393 * \param encoder An encoder instance to set.
394 * \param value Flag value (see above).
396 * \code encoder != NULL \endcode
398 * \c false if the encoder is already initialized, else \c true.
400 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value);
402 /** Set the "streamable subset" flag. If \c true, the encoder will comply
403 * with the subset (see the format specification) and will check the
404 * settings during FLAC__stream_encoder_init() to see if all settings
405 * comply. If \c false, the settings may take advantage of the full
406 * range that the format allows.
408 * Make sure you know what it entails before setting this to \c false.
411 * \param encoder An encoder instance to set.
412 * \param value Flag value (see above).
414 * \code encoder != NULL \endcode
416 * \c false if the encoder is already initialized, else \c true.
418 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value);
420 /** Set to \c true to enable mid-side encoding on stereo input. The
421 * number of channels must be 2. Set to \c false to use only
422 * independent channel coding.
425 * \param encoder An encoder instance to set.
426 * \param value Flag value (see above).
428 * \code encoder != NULL \endcode
430 * \c false if the encoder is already initialized, else \c true.
432 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value);
434 /** Set to \c true to enable adaptive switching between mid-side and
435 * left-right encoding on stereo input. The number of channels must
436 * be 2. Set to \c false to use exhaustive searching. In either
437 * case, the mid/side stereo setting must be \c true.
440 * \param encoder An encoder instance to set.
441 * \param value Flag value (see above).
443 * \code encoder != NULL \endcode
445 * \c false if the encoder is already initialized, else \c true.
447 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value);
449 /** Set the number of channels to be encoded.
452 * \param encoder An encoder instance to set.
453 * \param value See above.
455 * \code encoder != NULL \endcode
457 * \c false if the encoder is already initialized, else \c true.
459 FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value);
461 /** Set the sample resolution of the input to be encoded.
464 * Do not feed the encoder data that is wider than the value you
465 * set here or you will generate an invalid stream.
468 * \param encoder An encoder instance to set.
469 * \param value See above.
471 * \code encoder != NULL \endcode
473 * \c false if the encoder is already initialized, else \c true.
475 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value);
477 /** Set the sample rate (in Hz) of the input to be encoded.
480 * \param encoder An encoder instance to set.
481 * \param value See above.
483 * \code encoder != NULL \endcode
485 * \c false if the encoder is already initialized, else \c true.
487 FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value);
489 /** Set the blocksize to use while encoding.
492 * \param encoder An encoder instance to set.
493 * \param value See above.
495 * \code encoder != NULL \endcode
497 * \c false if the encoder is already initialized, else \c true.
499 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value);
501 /** Sets the apodization function(s) the encoder will use when windowing
502 * audio data for LPC analysis.
504 * The \a specification is a plain ASCII string which specifies exactly
505 * which functions to use. There may be more than one (up to 32),
506 * separated by \c ';' characters. Some functions take one or more
507 * comma-separated arguments in parentheses.
509 * The available functions are \c bartlett, \c bartlett_hann,
510 * \c blackman, \c blackman_harris_4term_92db, \c connes, \c flattop,
511 * \c gauss(STDDEV), \c hamming, \c hann, \c kaiser_bessel, \c nuttall,
512 * \c rectangle, \c triangle, \c tukey(P), \c welch.
514 * For \c gauss(STDDEV), STDDEV specifies the standard deviation
517 * For \c tukey(P), P specifies the fraction of the window that is
518 * tapered (0<=P<=1). P=0 corresponds to \c rectangle and P=1
519 * corresponds to \c hann.
521 * Example specifications are \c "blackman" or
522 * \c "hann;triangle;tukey(0.5);tukey(0.25);tukey(0.125)"
524 * Any function that is specified erroneously is silently dropped. Up
525 * to 32 functions are kept, the rest are dropped. If the specification
526 * is empty the encoder defaults to \c "hann".
528 * When more than one function is specified, then for every subframe the
529 * encoder will try each of them separately and choose the window that
530 * results in the smallest compressed subframe.
532 * Note that each function specified causes the encoder to occupy a
533 * floating point array in which to store the window.
536 * \param encoder An encoder instance to set.
537 * \param specification See above.
539 * \code encoder != NULL \endcode
540 * \code specification != NULL \endcode
542 * \c false if the encoder is already initialized, else \c true.
544 /* @@@@add to unit tests*/
545 FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification);
547 /** Set the maximum LPC order, or \c 0 to use only the fixed predictors.
550 * \param encoder An encoder instance to set.
551 * \param value See above.
553 * \code encoder != NULL \endcode
555 * \c false if the encoder is already initialized, else \c true.
557 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value);
559 /** Set the precision, in bits, of the quantized linear predictor
560 * coefficients, or \c 0 to let the encoder select it based on the
564 * In the current implementation, qlp_coeff_precision + bits_per_sample must
568 * \param encoder An encoder instance to set.
569 * \param value See above.
571 * \code encoder != NULL \endcode
573 * \c false if the encoder is already initialized, else \c true.
575 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value);
577 /** Set to \c false to use only the specified quantized linear predictor
578 * coefficient precision, or \c true to search neighboring precision
579 * values and use the best one.
582 * \param encoder An encoder instance to set.
583 * \param value See above.
585 * \code encoder != NULL \endcode
587 * \c false if the encoder is already initialized, else \c true.
589 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value);
591 /** Deprecated. Setting this value has no effect.
594 * \param encoder An encoder instance to set.
595 * \param value See above.
597 * \code encoder != NULL \endcode
599 * \c false if the encoder is already initialized, else \c true.
601 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value);
603 /** Set to \c false to let the encoder estimate the best model order
604 * based on the residual signal energy, or \c true to force the
605 * encoder to evaluate all order models and select the best.
608 * \param encoder An encoder instance to set.
609 * \param value See above.
611 * \code encoder != NULL \endcode
613 * \c false if the encoder is already initialized, else \c true.
615 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value);
617 /** Set the minimum partition order to search when coding the residual.
618 * This is used in tandem with
619 * FLAC__stream_encoder_set_max_residual_partition_order().
621 * The partition order determines the context size in the residual.
622 * The context size will be approximately <tt>blocksize / (2 ^ order)</tt>.
624 * Set both min and max values to \c 0 to force a single context,
625 * whose Rice parameter is based on the residual signal variance.
626 * Otherwise, set a min and max order, and the encoder will search
627 * all orders, using the mean of each context for its Rice parameter,
631 * \param encoder An encoder instance to set.
632 * \param value See above.
634 * \code encoder != NULL \endcode
636 * \c false if the encoder is already initialized, else \c true.
638 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value);
640 /** Set the maximum partition order to search when coding the residual.
641 * This is used in tandem with
642 * FLAC__stream_encoder_set_min_residual_partition_order().
644 * The partition order determines the context size in the residual.
645 * The context size will be approximately <tt>blocksize / (2 ^ order)</tt>.
647 * Set both min and max values to \c 0 to force a single context,
648 * whose Rice parameter is based on the residual signal variance.
649 * Otherwise, set a min and max order, and the encoder will search
650 * all orders, using the mean of each context for its Rice parameter,
654 * \param encoder An encoder instance to set.
655 * \param value See above.
657 * \code encoder != NULL \endcode
659 * \c false if the encoder is already initialized, else \c true.
661 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value);
663 /** Deprecated. Setting this value has no effect.
666 * \param encoder An encoder instance to set.
667 * \param value See above.
669 * \code encoder != NULL \endcode
671 * \c false if the encoder is already initialized, else \c true.
673 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value);
675 /** Set an estimate of the total samples that will be encoded.
676 * This is merely an estimate and may be set to \c 0 if unknown.
677 * This value will be written to the STREAMINFO block before encoding,
678 * and can remove the need for the caller to rewrite the value later
679 * if the value is known before encoding.
682 * \param encoder An encoder instance to set.
683 * \param value See above.
685 * \code encoder != NULL \endcode
687 * \c false if the encoder is already initialized, else \c true.
689 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value);
691 /** Set the metadata blocks to be emitted to the stream before encoding.
692 * A value of \c NULL, \c 0 implies no metadata; otherwise, supply an
693 * array of pointers to metadata blocks. The array is non-const since
694 * the encoder may need to change the \a is_last flag inside them.
695 * Otherwise, the encoder will not modify or free the blocks. It is up
696 * to the caller to free the metadata blocks after encoding.
699 * The encoder stores only the \a metadata pointer; the passed-in array
700 * must survive at least until after FLAC__stream_encoder_init() returns.
701 * Do not modify the array or free the blocks until then.
704 * The STREAMINFO block is always written and no STREAMINFO block may
705 * occur in the supplied array.
708 * By default the encoder does not create a SEEKTABLE. If one is supplied
709 * in the \a metadata array it will be written verbatim. However by itself
710 * this is not very useful as the user will not know the stream offsets for
711 * the seekpoints ahead of time. You must use the seekable stream encoder
712 * to generate a legal seektable
713 * (see FLAC__seekable_stream_encoder_set_metadata())
716 * A VORBIS_COMMENT block may be supplied. The vendor string in it
717 * will be ignored. libFLAC will use it's own vendor string. libFLAC
718 * will not modify the passed-in VORBIS_COMMENT's vendor string, it
719 * will simply write it's own into the stream. If no VORBIS_COMMENT
720 * block is present in the \a metadata array, libFLAC will write an
721 * empty one, containing only the vendor string.
723 * \default \c NULL, 0
724 * \param encoder An encoder instance to set.
725 * \param metadata See above.
726 * \param num_blocks See above.
728 * \code encoder != NULL \endcode
730 * \c false if the encoder is already initialized, else \c true.
732 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks);
734 /** Set the write callback.
735 * The supplied function will be called by the encoder anytime there is raw
736 * encoded data ready to write. It may include metadata mixed with encoded
737 * audio frames and the data is not guaranteed to be aligned on frame or
738 * metadata block boundaries.
740 * The only duty of the callback is to write out the \a bytes worth of data
741 * in \a buffer to the current position in the output stream. The arguments
742 * \a samples and \a current_frame are purely informational. If \a samples
743 * is greater than \c 0, then \a current_frame will hold the current frame
744 * number that is being written; otherwise, the write callback is being called
748 * The callback is mandatory and must be set before initialization.
751 * \param encoder An encoder instance to set.
752 * \param value See above.
754 * \code encoder != NULL \endcode
755 * \code value != NULL \endcode
757 * \c false if the encoder is already initialized, else \c true.
759 FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value);
761 /** Set the metadata callback.
762 * The supplied function will be called once at the end of encoding with
763 * the populated STREAMINFO structure. This is so file encoders can seek
764 * back to the beginning of the file and write the STREAMINFO block with
765 * the correct statistics after encoding (like minimum/maximum frame size
766 * and total samples).
769 * The callback is mandatory and must be set before initialization.
772 * \param encoder An encoder instance to set.
773 * \param value See above.
775 * \code encoder != NULL \endcode
776 * \code value != NULL \endcode
778 * \c false if the encoder is already initialized, else \c true.
780 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value);
782 /** Set the client data to be passed back to callbacks.
783 * This value will be supplied to callbacks in their \a client_data
787 * \param encoder An encoder instance to set.
788 * \param value See above.
790 * \code encoder != NULL \endcode
792 * \c false if the encoder is already initialized, else \c true.
794 FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value);
796 /** Get the current encoder state.
798 * \param encoder An encoder instance to query.
800 * \code encoder != NULL \endcode
801 * \retval FLAC__StreamEncoderState
802 * The current encoder state.
804 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder);
806 /** Get the state of the verify stream decoder.
807 * Useful when the stream encoder state is
808 * \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR.
810 * \param encoder An encoder instance to query.
812 * \code encoder != NULL \endcode
813 * \retval FLAC__StreamDecoderState
814 * The verify stream decoder state.
816 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder);
818 /** Get the current encoder state as a C string.
819 * This version automatically resolves
820 * \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR by getting the
821 * verify decoder's state.
823 * \param encoder A encoder instance to query.
825 * \code encoder != NULL \endcode
826 * \retval const char *
827 * The encoder state as a C string. Do not modify the contents.
829 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder);
831 /** Get relevant values about the nature of a verify decoder error.
832 * Useful when the stream encoder state is
833 * \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR. The arguments should
834 * be addresses in which the stats will be returned, or NULL if value
837 * \param encoder An encoder instance to query.
838 * \param absolute_sample The absolute sample number of the mismatch.
839 * \param frame_number The number of the frame in which the mismatch occurred.
840 * \param channel The channel in which the mismatch occurred.
841 * \param sample The number of the sample (relative to the frame) in
842 * which the mismatch occurred.
843 * \param expected The expected value for the sample in question.
844 * \param got The actual value returned by the decoder.
846 * \code encoder != NULL \endcode
848 FLAC_API void FLAC__stream_encoder_get_verify_decoder_error_stats(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_sample, unsigned *frame_number, unsigned *channel, unsigned *sample, FLAC__int32 *expected, FLAC__int32 *got);
850 /** Get the "verify" flag.
852 * \param encoder An encoder instance to query.
854 * \code encoder != NULL \endcode
856 * See FLAC__stream_encoder_set_verify().
858 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder);
860 /** Get the "streamable subset" flag.
862 * \param encoder An encoder instance to query.
864 * \code encoder != NULL \endcode
866 * See FLAC__stream_encoder_set_streamable_subset().
868 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder);
870 /** Get the "mid/side stereo coding" flag.
872 * \param encoder An encoder instance to query.
874 * \code encoder != NULL \endcode
876 * See FLAC__stream_encoder_get_do_mid_side_stereo().
878 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder);
880 /** Get the "adaptive mid/side switching" flag.
882 * \param encoder An encoder instance to query.
884 * \code encoder != NULL \endcode
886 * See FLAC__stream_encoder_set_loose_mid_side_stereo().
888 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder);
890 /** Get the number of input channels being processed.
892 * \param encoder An encoder instance to query.
894 * \code encoder != NULL \endcode
896 * See FLAC__stream_encoder_set_channels().
898 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder);
900 /** Get the input sample resolution setting.
902 * \param encoder An encoder instance to query.
904 * \code encoder != NULL \endcode
906 * See FLAC__stream_encoder_set_bits_per_sample().
908 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder);
910 /** Get the input sample rate setting.
912 * \param encoder An encoder instance to query.
914 * \code encoder != NULL \endcode
916 * See FLAC__stream_encoder_set_sample_rate().
918 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder);
920 /** Get the blocksize setting.
922 * \param encoder An encoder instance to query.
924 * \code encoder != NULL \endcode
926 * See FLAC__stream_encoder_set_blocksize().
928 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder);
930 /** Get the maximum LPC order setting.
932 * \param encoder An encoder instance to query.
934 * \code encoder != NULL \endcode
936 * See FLAC__stream_encoder_set_max_lpc_order().
938 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder);
940 /** Get the quantized linear predictor coefficient precision setting.
942 * \param encoder An encoder instance to query.
944 * \code encoder != NULL \endcode
946 * See FLAC__stream_encoder_set_qlp_coeff_precision().
948 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder);
950 /** Get the qlp coefficient precision search flag.
952 * \param encoder An encoder instance to query.
954 * \code encoder != NULL \endcode
956 * See FLAC__stream_encoder_set_do_qlp_coeff_prec_search().
958 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder);
960 /** Get the "escape coding" flag.
962 * \param encoder An encoder instance to query.
964 * \code encoder != NULL \endcode
966 * See FLAC__stream_encoder_set_do_escape_coding().
968 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder);
970 /** Get the exhaustive model search flag.
972 * \param encoder An encoder instance to query.
974 * \code encoder != NULL \endcode
976 * See FLAC__stream_encoder_set_do_exhaustive_model_search().
978 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder);
980 /** Get the minimum residual partition order setting.
982 * \param encoder An encoder instance to query.
984 * \code encoder != NULL \endcode
986 * See FLAC__stream_encoder_set_min_residual_partition_order().
988 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder);
990 /** Get maximum residual partition order setting.
992 * \param encoder An encoder instance to query.
994 * \code encoder != NULL \endcode
996 * See FLAC__stream_encoder_set_max_residual_partition_order().
998 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder);
1000 /** Get the Rice parameter search distance setting.
1002 * \param encoder An encoder instance to query.
1004 * \code encoder != NULL \endcode
1006 * See FLAC__stream_encoder_set_rice_parameter_search_dist().
1008 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder);
1010 /** Get the previously set estimate of the total samples to be encoded.
1011 * The encoder merely mimics back the value given to
1012 * FLAC__stream_encoder_set_total_samples_estimate() since it has no
1013 * other way of knowing how many samples the user will encode.
1015 * \param encoder An encoder instance to set.
1017 * \code encoder != NULL \endcode
1018 * \retval FLAC__uint64
1019 * See FLAC__stream_encoder_get_total_samples_estimate().
1021 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder);
1023 /** Initialize the encoder instance.
1024 * Should be called after FLAC__stream_encoder_new() and
1025 * FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process()
1026 * or FLAC__stream_encoder_process_interleaved(). Will set and return
1027 * the encoder state, which will be FLAC__STREAM_ENCODER_OK if
1028 * initialization succeeded.
1030 * The call to FLAC__stream_encoder_init() currently will also immediately
1031 * call the write callback several times, once with the \c fLaC signature,
1032 * and once for each encoded metadata block.
1034 * \param encoder An uninitialized encoder instance.
1036 * \code encoder != NULL \endcode
1037 * \retval FLAC__StreamEncoderState
1038 * \c FLAC__STREAM_ENCODER_OK if initialization was successful; see
1039 * FLAC__StreamEncoderState for the meanings of other return values.
1041 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder);
1043 /** Finish the encoding process.
1044 * Flushes the encoding buffer, releases resources, resets the encoder
1045 * settings to their defaults, and returns the encoder state to
1046 * FLAC__STREAM_ENCODER_UNINITIALIZED. Note that this can generate
1047 * one or more write callbacks before returning, and will generate
1048 * a metadata callback.
1050 * In the event of a prematurely-terminated encode, it is not strictly
1051 * necessary to call this immediately before FLAC__stream_encoder_delete()
1052 * but it is good practice to match every FLAC__stream_encoder_init()
1053 * with a FLAC__stream_encoder_finish().
1055 * \param encoder An uninitialized encoder instance.
1057 * \code encoder != NULL \endcode
1059 FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder);
1061 /** Submit data for encoding.
1062 * This version allows you to supply the input data via an array of
1063 * pointers, each pointer pointing to an array of \a samples samples
1064 * representing one channel. The samples need not be block-aligned,
1065 * but each channel should have the same number of samples.
1067 * \param encoder An initialized encoder instance in the OK state.
1068 * \param buffer An array of pointers to each channel's signal.
1069 * \param samples The number of samples in one channel.
1071 * \code encoder != NULL \endcode
1072 * \code FLAC__stream_encoder_get_state(encoder) == FLAC__STREAM_ENCODER_OK \endcode
1073 * \retval FLAC__bool
1074 * \c true if successful, else \c false; in this case, check the
1075 * encoder state with FLAC__stream_encoder_get_state() to see what
1078 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples);
1080 /** Submit data for encoding.
1081 * This version allows you to supply the input data where the channels
1082 * are interleaved into a single array (i.e. channel0_sample0,
1083 * channel1_sample0, ... , channelN_sample0, channel0_sample1, ...).
1084 * The samples need not be block-aligned but they must be
1085 * sample-aligned, i.e. the first value should be channel0_sample0
1086 * and the last value channelN_sampleM.
1088 * \param encoder An initialized encoder instance in the OK state.
1089 * \param buffer An array of channel-interleaved data (see above).
1090 * \param samples The number of samples in one channel, the same as for
1091 * FLAC__stream_encoder_process(). For example, if
1092 * encoding two channels, \c 1000 \a samples corresponds
1093 * to a \a buffer of 2000 values.
1095 * \code encoder != NULL \endcode
1096 * \code FLAC__stream_encoder_get_state(encoder) == FLAC__STREAM_ENCODER_OK \endcode
1097 * \retval FLAC__bool
1098 * \c true if successful, else \c false; in this case, check the
1099 * encoder state with FLAC__stream_encoder_get_state() to see what
1102 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples);