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 /** Set the maximum LPC order, or \c 0 to use only the fixed predictors.
504 * \param encoder An encoder instance to set.
505 * \param value See above.
507 * \code encoder != NULL \endcode
509 * \c false if the encoder is already initialized, else \c true.
511 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value);
513 /** Set the precision, in bits, of the quantized linear predictor
514 * coefficients, or \c 0 to let the encoder select it based on the
518 * In the current implementation, qlp_coeff_precision + bits_per_sample must
522 * \param encoder An encoder instance to set.
523 * \param value See above.
525 * \code encoder != NULL \endcode
527 * \c false if the encoder is already initialized, else \c true.
529 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value);
531 /** Set to \c false to use only the specified quantized linear predictor
532 * coefficient precision, or \c true to search neighboring precision
533 * values and use the best one.
536 * \param encoder An encoder instance to set.
537 * \param value See above.
539 * \code encoder != NULL \endcode
541 * \c false if the encoder is already initialized, else \c true.
543 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value);
545 /** Deprecated. Setting this value has no effect.
548 * \param encoder An encoder instance to set.
549 * \param value See above.
551 * \code encoder != NULL \endcode
553 * \c false if the encoder is already initialized, else \c true.
555 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value);
557 /** Set to \c false to let the encoder estimate the best model order
558 * based on the residual signal energy, or \c true to force the
559 * encoder to evaluate all order models and select the best.
562 * \param encoder An encoder instance to set.
563 * \param value See above.
565 * \code encoder != NULL \endcode
567 * \c false if the encoder is already initialized, else \c true.
569 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value);
571 /** Set the minimum partition order to search when coding the residual.
572 * This is used in tandem with
573 * FLAC__stream_encoder_set_max_residual_partition_order().
575 * The partition order determines the context size in the residual.
576 * The context size will be approximately <tt>blocksize / (2 ^ order)</tt>.
578 * Set both min and max values to \c 0 to force a single context,
579 * whose Rice parameter is based on the residual signal variance.
580 * Otherwise, set a min and max order, and the encoder will search
581 * all orders, using the mean of each context for its Rice parameter,
585 * \param encoder An encoder instance to set.
586 * \param value See above.
588 * \code encoder != NULL \endcode
590 * \c false if the encoder is already initialized, else \c true.
592 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value);
594 /** Set the maximum partition order to search when coding the residual.
595 * This is used in tandem with
596 * FLAC__stream_encoder_set_min_residual_partition_order().
598 * The partition order determines the context size in the residual.
599 * The context size will be approximately <tt>blocksize / (2 ^ order)</tt>.
601 * Set both min and max values to \c 0 to force a single context,
602 * whose Rice parameter is based on the residual signal variance.
603 * Otherwise, set a min and max order, and the encoder will search
604 * all orders, using the mean of each context for its Rice parameter,
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_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value);
617 /** Deprecated. Setting this value has no effect.
620 * \param encoder An encoder instance to set.
621 * \param value See above.
623 * \code encoder != NULL \endcode
625 * \c false if the encoder is already initialized, else \c true.
627 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value);
629 /** Set an estimate of the total samples that will be encoded.
630 * This is merely an estimate and may be set to \c 0 if unknown.
631 * This value will be written to the STREAMINFO block before encoding,
632 * and can remove the need for the caller to rewrite the value later
633 * if the value is known before encoding.
636 * \param encoder An encoder instance to set.
637 * \param value See above.
639 * \code encoder != NULL \endcode
641 * \c false if the encoder is already initialized, else \c true.
643 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value);
645 /** Set the metadata blocks to be emitted to the stream before encoding.
646 * A value of \c NULL, \c 0 implies no metadata; otherwise, supply an
647 * array of pointers to metadata blocks. The array is non-const since
648 * the encoder may need to change the \a is_last flag inside them.
649 * Otherwise, the encoder will not modify or free the blocks. It is up
650 * to the caller to free the metadata blocks after encoding.
653 * The encoder stores only the \a metadata pointer; the passed-in array
654 * must survive at least until after FLAC__stream_encoder_init() returns.
655 * Do not modify the array or free the blocks until then.
658 * The STREAMINFO block is always written and no STREAMINFO block may
659 * occur in the supplied array.
662 * By default the encoder does not create a SEEKTABLE. If one is supplied
663 * in the \a metadata array it will be written verbatim. However by itself
664 * this is not very useful as the user will not know the stream offsets for
665 * the seekpoints ahead of time. You must use the seekable stream encoder
666 * to generate a legal seektable
667 * (see FLAC__seekable_stream_encoder_set_metadata())
670 * A VORBIS_COMMENT block may be supplied. The vendor string in it
671 * will be ignored. libFLAC will use it's own vendor string. libFLAC
672 * will not modify the passed-in VORBIS_COMMENT's vendor string, it
673 * will simply write it's own into the stream. If no VORBIS_COMMENT
674 * block is present in the \a metadata array, libFLAC will write an
675 * empty one, containing only the vendor string.
677 * \default \c NULL, 0
678 * \param encoder An encoder instance to set.
679 * \param metadata See above.
680 * \param num_blocks See above.
682 * \code encoder != NULL \endcode
684 * \c false if the encoder is already initialized, else \c true.
686 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks);
688 /** Set the write callback.
689 * The supplied function will be called by the encoder anytime there is raw
690 * encoded data ready to write. It may include metadata mixed with encoded
691 * audio frames and the data is not guaranteed to be aligned on frame or
692 * metadata block boundaries.
694 * The only duty of the callback is to write out the \a bytes worth of data
695 * in \a buffer to the current position in the output stream. The arguments
696 * \a samples and \a current_frame are purely informational. If \a samples
697 * is greater than \c 0, then \a current_frame will hold the current frame
698 * number that is being written; otherwise, the write callback is being called
702 * The callback is mandatory and must be set before initialization.
705 * \param encoder An encoder instance to set.
706 * \param value See above.
708 * \code encoder != NULL \endcode
709 * \code value != NULL \endcode
711 * \c false if the encoder is already initialized, else \c true.
713 FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value);
715 /** Set the metadata callback.
716 * The supplied function will be called once at the end of encoding with
717 * the populated STREAMINFO structure. This is so file encoders can seek
718 * back to the beginning of the file and write the STREAMINFO block with
719 * the correct statistics after encoding (like minimum/maximum frame size
720 * and total samples).
723 * The callback is mandatory and must be set before initialization.
726 * \param encoder An encoder instance to set.
727 * \param value See above.
729 * \code encoder != NULL \endcode
730 * \code value != NULL \endcode
732 * \c false if the encoder is already initialized, else \c true.
734 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value);
736 /** Set the client data to be passed back to callbacks.
737 * This value will be supplied to callbacks in their \a client_data
741 * \param encoder An encoder instance to set.
742 * \param value See above.
744 * \code encoder != NULL \endcode
746 * \c false if the encoder is already initialized, else \c true.
748 FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value);
750 /** Get the current encoder state.
752 * \param encoder An encoder instance to query.
754 * \code encoder != NULL \endcode
755 * \retval FLAC__StreamEncoderState
756 * The current encoder state.
758 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder);
760 /** Get the state of the verify stream decoder.
761 * Useful when the stream encoder state is
762 * \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR.
764 * \param encoder An encoder instance to query.
766 * \code encoder != NULL \endcode
767 * \retval FLAC__StreamDecoderState
768 * The verify stream decoder state.
770 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder);
772 /** Get the current encoder state as a C string.
773 * This version automatically resolves
774 * \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR by getting the
775 * verify decoder's state.
777 * \param encoder A encoder instance to query.
779 * \code encoder != NULL \endcode
780 * \retval const char *
781 * The encoder state as a C string. Do not modify the contents.
783 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder);
785 /** Get relevant values about the nature of a verify decoder error.
786 * Useful when the stream encoder state is
787 * \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR. The arguments should
788 * be addresses in which the stats will be returned, or NULL if value
791 * \param encoder An encoder instance to query.
792 * \param absolute_sample The absolute sample number of the mismatch.
793 * \param frame_number The number of the frame in which the mismatch occurred.
794 * \param channel The channel in which the mismatch occurred.
795 * \param sample The number of the sample (relative to the frame) in
796 * which the mismatch occurred.
797 * \param expected The expected value for the sample in question.
798 * \param got The actual value returned by the decoder.
800 * \code encoder != NULL \endcode
802 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);
804 /** Get the "verify" flag.
806 * \param encoder An encoder instance to query.
808 * \code encoder != NULL \endcode
810 * See FLAC__stream_encoder_set_verify().
812 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder);
814 /** Get the "streamable subset" flag.
816 * \param encoder An encoder instance to query.
818 * \code encoder != NULL \endcode
820 * See FLAC__stream_encoder_set_streamable_subset().
822 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder);
824 /** Get the "mid/side stereo coding" flag.
826 * \param encoder An encoder instance to query.
828 * \code encoder != NULL \endcode
830 * See FLAC__stream_encoder_get_do_mid_side_stereo().
832 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder);
834 /** Get the "adaptive mid/side switching" flag.
836 * \param encoder An encoder instance to query.
838 * \code encoder != NULL \endcode
840 * See FLAC__stream_encoder_set_loose_mid_side_stereo().
842 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder);
844 /** Get the number of input channels being processed.
846 * \param encoder An encoder instance to query.
848 * \code encoder != NULL \endcode
850 * See FLAC__stream_encoder_set_channels().
852 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder);
854 /** Get the input sample resolution setting.
856 * \param encoder An encoder instance to query.
858 * \code encoder != NULL \endcode
860 * See FLAC__stream_encoder_set_bits_per_sample().
862 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder);
864 /** Get the input sample rate setting.
866 * \param encoder An encoder instance to query.
868 * \code encoder != NULL \endcode
870 * See FLAC__stream_encoder_set_sample_rate().
872 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder);
874 /** Get the blocksize setting.
876 * \param encoder An encoder instance to query.
878 * \code encoder != NULL \endcode
880 * See FLAC__stream_encoder_set_blocksize().
882 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder);
884 /** Get the maximum LPC order setting.
886 * \param encoder An encoder instance to query.
888 * \code encoder != NULL \endcode
890 * See FLAC__stream_encoder_set_max_lpc_order().
892 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder);
894 /** Get the quantized linear predictor coefficient precision setting.
896 * \param encoder An encoder instance to query.
898 * \code encoder != NULL \endcode
900 * See FLAC__stream_encoder_set_qlp_coeff_precision().
902 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder);
904 /** Get the qlp coefficient precision search flag.
906 * \param encoder An encoder instance to query.
908 * \code encoder != NULL \endcode
910 * See FLAC__stream_encoder_set_do_qlp_coeff_prec_search().
912 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder);
914 /** Get the "escape coding" flag.
916 * \param encoder An encoder instance to query.
918 * \code encoder != NULL \endcode
920 * See FLAC__stream_encoder_set_do_escape_coding().
922 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder);
924 /** Get the exhaustive model search flag.
926 * \param encoder An encoder instance to query.
928 * \code encoder != NULL \endcode
930 * See FLAC__stream_encoder_set_do_exhaustive_model_search().
932 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder);
934 /** Get the minimum residual partition order setting.
936 * \param encoder An encoder instance to query.
938 * \code encoder != NULL \endcode
940 * See FLAC__stream_encoder_set_min_residual_partition_order().
942 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder);
944 /** Get maximum residual partition order setting.
946 * \param encoder An encoder instance to query.
948 * \code encoder != NULL \endcode
950 * See FLAC__stream_encoder_set_max_residual_partition_order().
952 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder);
954 /** Get the Rice parameter search distance setting.
956 * \param encoder An encoder instance to query.
958 * \code encoder != NULL \endcode
960 * See FLAC__stream_encoder_set_rice_parameter_search_dist().
962 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder);
964 /** Get the previously set estimate of the total samples to be encoded.
965 * The encoder merely mimics back the value given to
966 * FLAC__stream_encoder_set_total_samples_estimate() since it has no
967 * other way of knowing how many samples the user will encode.
969 * \param encoder An encoder instance to set.
971 * \code encoder != NULL \endcode
972 * \retval FLAC__uint64
973 * See FLAC__stream_encoder_get_total_samples_estimate().
975 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder);
977 /** Initialize the encoder instance.
978 * Should be called after FLAC__stream_encoder_new() and
979 * FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process()
980 * or FLAC__stream_encoder_process_interleaved(). Will set and return
981 * the encoder state, which will be FLAC__STREAM_ENCODER_OK if
982 * initialization succeeded.
984 * The call to FLAC__stream_encoder_init() currently will also immediately
985 * call the write callback several times, once with the \c fLaC signature,
986 * and once for each encoded metadata block.
988 * \param encoder An uninitialized encoder instance.
990 * \code encoder != NULL \endcode
991 * \retval FLAC__StreamEncoderState
992 * \c FLAC__STREAM_ENCODER_OK if initialization was successful; see
993 * FLAC__StreamEncoderState for the meanings of other return values.
995 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder);
997 /** Finish the encoding process.
998 * Flushes the encoding buffer, releases resources, resets the encoder
999 * settings to their defaults, and returns the encoder state to
1000 * FLAC__STREAM_ENCODER_UNINITIALIZED. Note that this can generate
1001 * one or more write callbacks before returning, and will generate
1002 * a metadata callback.
1004 * In the event of a prematurely-terminated encode, it is not strictly
1005 * necessary to call this immediately before FLAC__stream_encoder_delete()
1006 * but it is good practice to match every FLAC__stream_encoder_init()
1007 * with a FLAC__stream_encoder_finish().
1009 * \param encoder An uninitialized encoder instance.
1011 * \code encoder != NULL \endcode
1013 FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder);
1015 /** Submit data for encoding.
1016 * This version allows you to supply the input data via an array of
1017 * pointers, each pointer pointing to an array of \a samples samples
1018 * representing one channel. The samples need not be block-aligned,
1019 * but each channel should have the same number of samples.
1021 * \param encoder An initialized encoder instance in the OK state.
1022 * \param buffer An array of pointers to each channel's signal.
1023 * \param samples The number of samples in one channel.
1025 * \code encoder != NULL \endcode
1026 * \code FLAC__stream_encoder_get_state(encoder) == FLAC__STREAM_ENCODER_OK \endcode
1027 * \retval FLAC__bool
1028 * \c true if successful, else \c false; in this case, check the
1029 * encoder state with FLAC__stream_encoder_get_state() to see what
1032 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples);
1034 /** Submit data for encoding.
1035 * This version allows you to supply the input data where the channels
1036 * are interleaved into a single array (i.e. channel0_sample0,
1037 * channel1_sample0, ... , channelN_sample0, channel0_sample1, ...).
1038 * The samples need not be block-aligned but they must be
1039 * sample-aligned, i.e. the first value should be channel0_sample0
1040 * and the last value channelN_sampleM.
1042 * \param encoder An initialized encoder instance in the OK state.
1043 * \param buffer An array of channel-interleaved data (see above).
1044 * \param samples The number of samples in one channel, the same as for
1045 * FLAC__stream_encoder_process(). For example, if
1046 * encoding two channels, \c 1000 \a samples corresponds
1047 * to a \a buffer of 2000 values.
1049 * \code encoder != NULL \endcode
1050 * \code FLAC__stream_encoder_get_state(encoder) == FLAC__STREAM_ENCODER_OK \endcode
1051 * \retval FLAC__bool
1052 * \c true if successful, else \c false; in this case, check the
1053 * encoder state with FLAC__stream_encoder_get_state() to see what
1056 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples);