1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002,2003 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 two layers of access. The
61 * lowest layer is stream-level encoding, and the highest is file-level
62 * encoding. The interfaces are described in the \link flac_stream_encoder
63 * stream encoder \endlink and \link flac_file_encoder file encoder \endlink
64 * modules respectively. Typically you will choose the highest layer that
65 * your output source will support.
67 * The stream encoder relies on callbacks for writing the data and
68 * metadata. The file encoder provides these callbacks internally and you
69 * need only supply the filename.
72 /** \defgroup flac_stream_encoder FLAC/stream_encoder.h: stream encoder interface
73 * \ingroup flac_encoder
76 * This module contains the functions which implement the stream
79 * The basic usage of this encoder is as follows:
80 * - The program creates an instance of an encoder using
81 * FLAC__stream_encoder_new().
82 * - The program overrides the default settings and sets callbacks using
83 * FLAC__stream_encoder_set_*() functions.
84 * - The program initializes the instance to validate the settings and
85 * prepare for encoding using FLAC__stream_encoder_init().
86 * - The program calls FLAC__stream_encoder_process() or
87 * FLAC__stream_encoder_process_interleaved() to encode data, which
88 * subsequently calls the callbacks when there is encoder data ready
90 * - The program finishes the encoding with FLAC__stream_encoder_finish(),
91 * which causes the encoder to encode any data still in its input pipe,
92 * call the metadata callback with the final encoding statistics, and
93 * finally reset the encoder to the uninitialized state.
94 * - The instance may be used again or deleted with
95 * FLAC__stream_encoder_delete().
97 * In more detail, the stream encoder functions similarly to the
98 * \link flac_stream_decoder stream decoder \endlink, but has fewer
99 * callbacks and more options. Typically the user will create a new
100 * instance by calling FLAC__stream_encoder_new(), then set the necessary
101 * parameters and callbacks with FLAC__stream_encoder_set_*(), and
102 * initialize it by calling FLAC__stream_encoder_init().
104 * Unlike the decoders, the stream encoder has many options that can
105 * affect the speed and compression ratio. When setting these parameters
106 * you should have some basic knowledge of the format (see the
107 * <A HREF="../documentation.html#format">user-level documentation</A>
108 * or the <A HREF="../format.html">formal description</A>). The
109 * FLAC__stream_encoder_set_*() functions themselves do not validate the
110 * values as many are interdependent. The FLAC__stream_encoder_init()
111 * function will do this, so make sure to pay attention to the state
112 * returned by FLAC__stream_encoder_init() to make sure that it is
113 * FLAC__STREAM_ENCODER_OK. Any parameters that are not set before
114 * FLAC__stream_encoder_init() will take on the defaults from the
117 * The user must provide function pointers for the following callbacks:
119 * - Write callback - This function is called by the encoder anytime there
120 * is raw encoded data to write. It may include metadata mixed with
121 * encoded audio frames and the data is not guaranteed to be aligned on
122 * frame or metadata block boundaries.
123 * - Metadata callback - This function is called once at the end of
124 * encoding with the populated STREAMINFO structure. This is so file
125 * encoders can seek back to the beginning of the file and write the
126 * STREAMINFO block with the correct statistics after encoding (like
127 * minimum/maximum frame size).
129 * The call to FLAC__stream_encoder_init() currently will also immediately
130 * call the write callback several times, once with the \c fLaC signature,
131 * and once for each encoded metadata block.
133 * After initializing the instance, the user may feed audio data to the
134 * encoder in one of two ways:
136 * - Channel separate, through FLAC__stream_encoder_process() - The user
137 * will pass an array of pointers to buffers, one for each channel, to
138 * the encoder, each of the same length. The samples need not be
140 * - Channel interleaved, through
141 * FLAC__stream_encoder_process_interleaved() - The user will pass a single
142 * pointer to data that is channel-interleaved (i.e. channel0_sample0,
143 * channel1_sample0, ... , channelN_sample0, channel0_sample1, ...).
144 * Again, the samples need not be block-aligned but they must be
145 * sample-aligned, i.e. the first value should be channel0_sample0 and
146 * the last value channelN_sampleM.
148 * When the user is finished encoding data, it calls
149 * FLAC__stream_encoder_finish(), which causes the encoder to encode any
150 * data still in its input pipe, and call the metadata callback with the
151 * final encoding statistics. Then the instance may be deleted with
152 * FLAC__stream_encoder_delete() or initialized again to encode another
155 * For programs that write their own metadata, but that do not know the
156 * actual metadata until after encoding, it is advantageous to instruct
157 * the encoder to write a PADDING block of the correct size, so that
158 * instead of rewriting the whole stream after encoding, the program can
159 * just overwrite the PADDING block. If only the maximum size of the
160 * metadata is known, the program can write a slightly larger padding
161 * block, then split it after encoding.
163 * Make sure you understand how lengths are calculated. All FLAC metadata
164 * blocks have a 4 byte header which contains the type and length. This
165 * length does not include the 4 bytes of the header. See the format page
166 * for the specification of metadata blocks and their lengths.
169 * The "set" functions may only be called when the encoder is in the
170 * state FLAC__STREAM_ENCODER_UNINITIALIZED, i.e. after
171 * FLAC__stream_encoder_new() or FLAC__stream_encoder_finish(), but
172 * before FLAC__stream_encoder_init(). If this is the case they will
173 * return \c true, otherwise \c false.
176 * FLAC__stream_encoder_finish() resets all settings to the constructor
177 * defaults, including the callbacks.
183 /** State values for a FLAC__StreamEncoder
185 * The encoder's state can be obtained by calling FLAC__stream_encoder_get_state().
189 FLAC__STREAM_ENCODER_OK = 0,
190 /**< The encoder is in the normal OK state. */
192 FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR,
193 /**< An error occurred in the underlying verify stream decoder;
194 * check FLAC__stream_encoder_get_verify_decoder_state().
197 FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA,
198 /**< The verify decoder detected a mismatch between the original
199 * audio signal and the decoded audio signal.
202 FLAC__STREAM_ENCODER_INVALID_CALLBACK,
203 /**< The encoder was initialized before setting all the required callbacks. */
205 FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS,
206 /**< The encoder has an invalid setting for number of channels. */
208 FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE,
209 /**< The encoder has an invalid setting for bits-per-sample.
210 * FLAC supports 4-32 bps but the reference encoder currently supports
214 FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE,
215 /**< The encoder has an invalid setting for the input sample rate. */
217 FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE,
218 /**< The encoder has an invalid setting for the block size. */
220 FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER,
221 /**< The encoder has an invalid setting for the maximum LPC order. */
223 FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION,
224 /**< The encoder has an invalid setting for the precision of the quantized linear predictor coefficients. */
226 FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH,
227 /**< Mid/side coding was specified but the number of channels is not equal to 2. */
229 FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH,
232 FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE,
233 /**< Loose mid/side coding was specified but mid/side coding was not. */
235 FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER,
236 /**< The specified block size is less than the maximum LPC order. */
238 FLAC__STREAM_ENCODER_NOT_STREAMABLE,
239 /**< The encoder is bound to the "streamable subset" but other settings violate it. */
241 FLAC__STREAM_ENCODER_FRAMING_ERROR,
242 /**< An error occurred while writing the stream; usually, the write_callback returned an error. */
244 FLAC__STREAM_ENCODER_INVALID_METADATA,
245 /**< The metadata input to the encoder is invalid, in one of the following ways:
246 * - FLAC__stream_encoder_set_metadata() was called with a null pointer but a block count > 0
247 * - One of the metadata blocks contains an undefined type
248 * - It contains an illegal CUESHEET as checked by FLAC__format_cuesheet_is_legal()
249 * - It contains an illegal SEEKTABLE as checked by FLAC__format_seektable_is_legal()
250 * - It contains more than one SEEKTABLE block or more than one VORBIS_COMMENT block
253 FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING,
254 /**< An error occurred while writing the stream; usually, the write_callback returned an error. */
256 FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING,
257 /**< The write_callback returned an error. */
259 FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR,
260 /**< Memory allocation failed. */
262 FLAC__STREAM_ENCODER_ALREADY_INITIALIZED,
263 /**< FLAC__stream_encoder_init() was called when the encoder was
264 * already initialized, usually because
265 * FLAC__stream_encoder_finish() was not called.
268 FLAC__STREAM_ENCODER_UNINITIALIZED
269 /**< The encoder is in the uninitialized state. */
271 } FLAC__StreamEncoderState;
273 /** Maps a FLAC__StreamEncoderState to a C string.
275 * Using a FLAC__StreamEncoderState as the index to this array
276 * will give the string equivalent. The contents should not be modified.
278 extern FLAC_API const char * const FLAC__StreamEncoderStateString[];
280 /** Return values for the FLAC__StreamEncoder write callback.
284 FLAC__STREAM_ENCODER_WRITE_STATUS_OK = 0,
285 /**< The write was OK and encoding can continue. */
287 FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR
288 /**< An unrecoverable error occurred. The encoder will return from the process call. */
290 } FLAC__StreamEncoderWriteStatus;
292 /** Maps a FLAC__StreamEncoderWriteStatus to a C string.
294 * Using a FLAC__StreamEncoderWriteStatus as the index to this array
295 * will give the string equivalent. The contents should not be modified.
297 extern FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[];
300 /***********************************************************************
302 * class FLAC__StreamEncoder
304 ***********************************************************************/
306 struct FLAC__StreamEncoderProtected;
307 struct FLAC__StreamEncoderPrivate;
308 /** The opaque structure definition for the stream encoder type.
309 * See the \link flac_stream_encoder stream encoder module \endlink
310 * for a detailed description.
313 struct FLAC__StreamEncoderProtected *protected_; /* avoid the C++ keyword 'protected' */
314 struct FLAC__StreamEncoderPrivate *private_; /* avoid the C++ keyword 'private' */
315 } FLAC__StreamEncoder;
317 /** Signature for the write callback.
318 * See FLAC__stream_encoder_set_write_callback() for more info.
320 * \param encoder The encoder instance calling the callback.
321 * \param buffer An array of encoded data of length \a bytes.
322 * \param bytes The byte length of \a buffer.
323 * \param samples The number of samples encoded by \a buffer.
324 * \c 0 has a special meaning; see
325 * FLAC__stream_encoder_set_write_callback().
326 * \param current_frame The number of the current frame being encoded.
327 * \param client_data The callee's client data set through
328 * FLAC__stream_encoder_set_client_data().
329 * \retval FLAC__StreamDecoderWriteStatus
330 * The callee's return status.
332 typedef FLAC__StreamEncoderWriteStatus (*FLAC__StreamEncoderWriteCallback)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
334 /** Signature for the metadata callback.
335 * See FLAC__stream_encoder_set_metadata_callback() for more info.
337 * \param encoder The encoder instance calling the callback.
338 * \param metadata The final populated STREAMINFO block.
339 * \param client_data The callee's client data set through
340 * FLAC__stream_encoder_set_client_data().
342 typedef void (*FLAC__StreamEncoderMetadataCallback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data);
345 /***********************************************************************
347 * Class constructor/destructor
349 ***********************************************************************/
351 /** Create a new stream encoder instance. The instance is created with
352 * default settings; see the individual FLAC__stream_encoder_set_*()
353 * functions for each setting's default.
355 * \retval FLAC__StreamEncoder*
356 * \c NULL if there was an error allocating memory, else the new instance.
358 FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new();
360 /** Free an encoder instance. Deletes the object pointed to by \a encoder.
362 * \param encoder A pointer to an existing encoder.
364 * \code encoder != NULL \endcode
366 FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder);
368 /***********************************************************************
370 * Public class method prototypes
372 ***********************************************************************/
374 /** Set the "verify" flag. If \c true, the encoder will verify it's own
375 * encoded output by feeding it through an internal decoder and comparing
376 * the original signal against the decoded signal. If a mismatch occurs,
377 * the process call will return \c false. Note that this will slow the
378 * encoding process by the extra time required for decoding and comparison.
381 * \param encoder An encoder instance to set.
382 * \param value Flag value (see above).
384 * \code encoder != NULL \endcode
386 * \c false if the encoder is already initialized, else \c true.
388 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value);
390 /** Set the "streamable subset" flag. If \c true, the encoder will comply
391 * with the subset (see the format specification) and will check the
392 * settings during FLAC__stream_encoder_init() to see if all settings
393 * comply. If \c false, the settings may take advantage of the full
394 * range that the format allows.
396 * Make sure you know what it entails before setting this to \c false.
399 * \param encoder An encoder instance to set.
400 * \param value Flag value (see above).
402 * \code encoder != NULL \endcode
404 * \c false if the encoder is already initialized, else \c true.
406 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value);
408 /** Set to \c true to enable mid-side encoding on stereo input. The
409 * number of channels must be 2. Set to \c false to use only
410 * independent channel coding.
413 * \param encoder An encoder instance to set.
414 * \param value Flag value (see above).
416 * \code encoder != NULL \endcode
418 * \c false if the encoder is already initialized, else \c true.
420 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value);
422 /** Set to \c true to enable adaptive switching between mid-side and
423 * left-right encoding on stereo input. The number of channels must
424 * be 2. Set to \c false to use exhaustive searching. In either
425 * case, the mid/side stereo setting must be \c true.
428 * \param encoder An encoder instance to set.
429 * \param value Flag value (see above).
431 * \code encoder != NULL \endcode
433 * \c false if the encoder is already initialized, else \c true.
435 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value);
437 /** Set the number of channels to be encoded.
440 * \param encoder An encoder instance to set.
441 * \param 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_channels(FLAC__StreamEncoder *encoder, unsigned value);
449 /** Set the sample resolution of the input to be encoded.
452 * Do not feed the encoder data that is wider than the value you
453 * set here or you will generate an invalid stream.
456 * \param encoder An encoder instance to set.
457 * \param value See above.
459 * \code encoder != NULL \endcode
461 * \c false if the encoder is already initialized, else \c true.
463 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value);
465 /** Set the sample rate (in Hz) of the input to be encoded.
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_sample_rate(FLAC__StreamEncoder *encoder, unsigned value);
477 /** Set the blocksize to use while encoding.
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_blocksize(FLAC__StreamEncoder *encoder, unsigned value);
489 /** Set the maximum LPC order, or \c 0 to use only the fixed predictors.
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_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value);
501 /** Set the precision, in bits, of the quantized linear predictor
502 * coefficients, or \c 0 to let the encoder select it based on the
506 * In the current implementation, qlp_coeff_precision + bits_per_sample must
510 * \param encoder An encoder instance to set.
511 * \param value See above.
513 * \code encoder != NULL \endcode
515 * \c false if the encoder is already initialized, else \c true.
517 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value);
519 /** Set to \c false to use only the specified quantized linear predictor
520 * coefficient precision, or \c true to search neighboring precision
521 * values and use the best one.
524 * \param encoder An encoder instance to set.
525 * \param value See above.
527 * \code encoder != NULL \endcode
529 * \c false if the encoder is already initialized, else \c true.
531 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value);
533 /** Deprecated. Setting this value has no effect.
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_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value);
545 /** Set to \c false to let the encoder estimate the best model order
546 * based on the residual signal energy, or \c true to force the
547 * encoder to evaluate all order models and select the best.
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_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value);
559 /** Set the minimum partition order to search when coding the residual.
560 * This is used in tandem with
561 * FLAC__stream_encoder_set_max_residual_partition_order().
563 * The partition order determines the context size in the residual.
564 * The context size will be approximately <tt>blocksize / (2 ^ order)</tt>.
566 * Set both min and max values to \c 0 to force a single context,
567 * whose Rice parameter is based on the residual signal variance.
568 * Otherwise, set a min and max order, and the encoder will search
569 * all orders, using the mean of each context for its Rice parameter,
573 * \param encoder An encoder instance to set.
574 * \param value See above.
576 * \code encoder != NULL \endcode
578 * \c false if the encoder is already initialized, else \c true.
580 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value);
582 /** Set the maximum partition order to search when coding the residual.
583 * This is used in tandem with
584 * FLAC__stream_encoder_set_min_residual_partition_order().
586 * The partition order determines the context size in the residual.
587 * The context size will be approximately <tt>blocksize / (2 ^ order)</tt>.
589 * Set both min and max values to \c 0 to force a single context,
590 * whose Rice parameter is based on the residual signal variance.
591 * Otherwise, set a min and max order, and the encoder will search
592 * all orders, using the mean of each context for its Rice parameter,
596 * \param encoder An encoder instance to set.
597 * \param value See above.
599 * \code encoder != NULL \endcode
601 * \c false if the encoder is already initialized, else \c true.
603 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value);
605 /** Deprecated. Setting this value has no effect.
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_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value);
617 /** Set an estimate of the total samples that will be encoded.
618 * This is merely an estimate and may be set to \c 0 if unknown.
619 * This value will be written to the STREAMINFO block before encoding,
620 * and can remove the need for the caller to rewrite the value later
621 * if the value is known before encoding.
624 * \param encoder An encoder instance to set.
625 * \param value See above.
627 * \code encoder != NULL \endcode
629 * \c false if the encoder is already initialized, else \c true.
631 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value);
633 /** Set the metadata blocks to be emitted to the stream before encoding.
634 * A value of \c NULL, \c 0 implies no metadata; otherwise, supply an
635 * array of pointers to metadata blocks. The array is non-const since
636 * the encoder may need to change the \a is_last flag inside them.
637 * Otherwise, the encoder will not modify or free the blocks. It is up
638 * to the caller to free the metadata blocks after encoding.
641 * The encoder stores only the \a metadata pointer; the passed-in array
642 * must survive at least until after FLAC__stream_encoder_init() returns.
643 * Do not modify the array or free the blocks until then.
646 * The STREAMINFO block is always written and no STREAMINFO block may
647 * occur in the supplied array.
650 * By default the encoder does not create a SEEKTABLE. If one is supplied
651 * in the \a metadata array it will be written verbatim. However by itself
652 * this is not very useful as the user will not know the stream offsets for
653 * the seekpoints ahead of time. You must use the seekable stream encoder
654 * to generate a legal seektable
655 * (see FLAC__seekable_stream_encoder_set_metadata())
658 * A VORBIS_COMMENT block may be supplied. The vendor string in it
659 * will be ignored. libFLAC will use it's own vendor string. libFLAC
660 * will not modify the passed-in VORBIS_COMMENT's vendor string, it
661 * will simply write it's own into the stream. If no VORBIS_COMMENT
662 * block is present in the \a metadata array, libFLAC will write an
663 * empty one, containing only the vendor string.
665 * \default \c NULL, 0
666 * \param encoder An encoder instance to set.
667 * \param metadata See above.
668 * \param num_blocks See above.
670 * \code encoder != NULL \endcode
672 * \c false if the encoder is already initialized, else \c true.
674 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks);
676 /** Set the write callback.
677 * The supplied function will be called by the encoder anytime there is raw
678 * encoded data ready to write. It may include metadata mixed with encoded
679 * audio frames and the data is not guaranteed to be aligned on frame or
680 * metadata block boundaries.
682 * The only duty of the callback is to write out the \a bytes worth of data
683 * in \a buffer to the current position in the output stream. The arguments
684 * \a samples and \a current_frame are purely informational. If \a samples
685 * is greater than \c 0, then \a current_frame will hold the current frame
686 * number that is being written; otherwise, the write callback is being called
690 * The callback is mandatory and must be set before initialization.
693 * \param encoder An encoder instance to set.
694 * \param value See above.
696 * \code encoder != NULL \endcode
697 * \code value != NULL \endcode
699 * \c false if the encoder is already initialized, else \c true.
701 FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value);
703 /** Set the metadata callback.
704 * The supplied function will be called once at the end of encoding with
705 * the populated STREAMINFO structure. This is so file encoders can seek
706 * back to the beginning of the file and write the STREAMINFO block with
707 * the correct statistics after encoding (like minimum/maximum frame size
708 * and total samples).
711 * The callback is mandatory and must be set before initialization.
714 * \param encoder An encoder instance to set.
715 * \param value See above.
717 * \code encoder != NULL \endcode
718 * \code value != NULL \endcode
720 * \c false if the encoder is already initialized, else \c true.
722 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value);
724 /** Set the client data to be passed back to callbacks.
725 * This value will be supplied to callbacks in their \a client_data
729 * \param encoder An encoder instance to set.
730 * \param value See above.
732 * \code encoder != NULL \endcode
734 * \c false if the encoder is already initialized, else \c true.
736 FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value);
738 /** Get the current encoder state.
740 * \param encoder An encoder instance to query.
742 * \code encoder != NULL \endcode
743 * \retval FLAC__StreamEncoderState
744 * The current encoder state.
746 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder);
748 /** Get the state of the verify stream decoder.
749 * Useful when the stream encoder state is
750 * \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR.
752 * \param encoder An encoder instance to query.
754 * \code encoder != NULL \endcode
755 * \retval FLAC__StreamDecoderState
756 * The verify stream decoder state.
758 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder);
760 /** Get the current encoder state as a C string.
761 * This version automatically resolves
762 * \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR by getting the
763 * verify decoder's state.
765 * \param encoder A encoder instance to query.
767 * \code encoder != NULL \endcode
768 * \retval const char *
769 * The encoder state as a C string. Do not modify the contents.
771 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder);
773 /** Get relevant values about the nature of a verify decoder error.
774 * Useful when the stream encoder state is
775 * \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR. The arguments should
776 * be addresses in which the stats will be returned, or NULL if value
779 * \param encoder An encoder instance to query.
780 * \param absolute_sample The absolute sample number of the mismatch.
781 * \param frame_number The number of the frame in which the mismatch occurred.
782 * \param channel The channel in which the mismatch occurred.
783 * \param sample The number of the sample (relative to the frame) in
784 * which the mismatch occurred.
785 * \param expected The expected value for the sample in question.
786 * \param got The actual value returned by the decoder.
788 * \code encoder != NULL \endcode
790 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);
792 /** Get the "verify" flag.
794 * \param encoder An encoder instance to query.
796 * \code encoder != NULL \endcode
798 * See FLAC__stream_encoder_set_verify().
800 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder);
802 /** Get the "streamable subset" flag.
804 * \param encoder An encoder instance to query.
806 * \code encoder != NULL \endcode
808 * See FLAC__stream_encoder_set_streamable_subset().
810 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder);
812 /** Get the "mid/side stereo coding" flag.
814 * \param encoder An encoder instance to query.
816 * \code encoder != NULL \endcode
818 * See FLAC__stream_encoder_get_do_mid_side_stereo().
820 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder);
822 /** Get the "adaptive mid/side switching" flag.
824 * \param encoder An encoder instance to query.
826 * \code encoder != NULL \endcode
828 * See FLAC__stream_encoder_set_loose_mid_side_stereo().
830 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder);
832 /** Get the number of input channels being processed.
834 * \param encoder An encoder instance to query.
836 * \code encoder != NULL \endcode
838 * See FLAC__stream_encoder_set_channels().
840 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder);
842 /** Get the input sample resolution setting.
844 * \param encoder An encoder instance to query.
846 * \code encoder != NULL \endcode
848 * See FLAC__stream_encoder_set_bits_per_sample().
850 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder);
852 /** Get the input sample rate setting.
854 * \param encoder An encoder instance to query.
856 * \code encoder != NULL \endcode
858 * See FLAC__stream_encoder_set_sample_rate().
860 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder);
862 /** Get the blocksize setting.
864 * \param encoder An encoder instance to query.
866 * \code encoder != NULL \endcode
868 * See FLAC__stream_encoder_set_blocksize().
870 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder);
872 /** Get the maximum LPC order setting.
874 * \param encoder An encoder instance to query.
876 * \code encoder != NULL \endcode
878 * See FLAC__stream_encoder_set_max_lpc_order().
880 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder);
882 /** Get the quantized linear predictor coefficient precision setting.
884 * \param encoder An encoder instance to query.
886 * \code encoder != NULL \endcode
888 * See FLAC__stream_encoder_set_qlp_coeff_precision().
890 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder);
892 /** Get the qlp coefficient precision search flag.
894 * \param encoder An encoder instance to query.
896 * \code encoder != NULL \endcode
898 * See FLAC__stream_encoder_set_do_qlp_coeff_prec_search().
900 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder);
902 /** Get the "escape coding" flag.
904 * \param encoder An encoder instance to query.
906 * \code encoder != NULL \endcode
908 * See FLAC__stream_encoder_set_do_escape_coding().
910 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder);
912 /** Get the exhaustive model search flag.
914 * \param encoder An encoder instance to query.
916 * \code encoder != NULL \endcode
918 * See FLAC__stream_encoder_set_do_exhaustive_model_search().
920 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder);
922 /** Get the minimum residual partition order setting.
924 * \param encoder An encoder instance to query.
926 * \code encoder != NULL \endcode
928 * See FLAC__stream_encoder_set_min_residual_partition_order().
930 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder);
932 /** Get maximum residual partition order setting.
934 * \param encoder An encoder instance to query.
936 * \code encoder != NULL \endcode
938 * See FLAC__stream_encoder_set_max_residual_partition_order().
940 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder);
942 /** Get the Rice parameter search distance setting.
944 * \param encoder An encoder instance to query.
946 * \code encoder != NULL \endcode
948 * See FLAC__stream_encoder_set_rice_parameter_search_dist().
950 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder);
952 /** Get the previously set estimate of the total samples to be encoded.
953 * The encoder merely mimics back the value given to
954 * FLAC__stream_encoder_set_total_samples_estimate() since it has no
955 * other way of knowing how many samples the user will encode.
957 * \param encoder An encoder instance to set.
959 * \code encoder != NULL \endcode
960 * \retval FLAC__uint64
961 * See FLAC__stream_encoder_get_total_samples_estimate().
963 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder);
965 /** Initialize the encoder instance.
966 * Should be called after FLAC__stream_encoder_new() and
967 * FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process()
968 * or FLAC__stream_encoder_process_interleaved(). Will set and return
969 * the encoder state, which will be FLAC__STREAM_ENCODER_OK if
970 * initialization succeeded.
972 * The call to FLAC__stream_encoder_init() currently will also immediately
973 * call the write callback several times, once with the \c fLaC signature,
974 * and once for each encoded metadata block.
976 * \param encoder An uninitialized encoder instance.
978 * \code encoder != NULL \endcode
979 * \retval FLAC__StreamEncoderState
980 * \c FLAC__STREAM_ENCODER_OK if initialization was successful; see
981 * FLAC__StreamEncoderState for the meanings of other return values.
983 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder);
985 /** Finish the encoding process.
986 * Flushes the encoding buffer, releases resources, resets the encoder
987 * settings to their defaults, and returns the encoder state to
988 * FLAC__STREAM_ENCODER_UNINITIALIZED. Note that this can generate
989 * one or more write callbacks before returning, and will generate
990 * a metadata callback.
992 * In the event of a prematurely-terminated encode, it is not strictly
993 * necessary to call this immediately before FLAC__stream_encoder_delete()
994 * but it is good practice to match every FLAC__stream_encoder_init()
995 * with a FLAC__stream_encoder_finish().
997 * \param encoder An uninitialized encoder instance.
999 * \code encoder != NULL \endcode
1001 FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder);
1003 /** Submit data for encoding.
1004 * This version allows you to supply the input data via an array of
1005 * pointers, each pointer pointing to an array of \a samples samples
1006 * representing one channel. The samples need not be block-aligned,
1007 * but each channel should have the same number of samples.
1009 * \param encoder An initialized encoder instance in the OK state.
1010 * \param buffer An array of pointers to each channel's signal.
1011 * \param samples The number of samples in one channel.
1013 * \code encoder != NULL \endcode
1014 * \code FLAC__stream_encoder_get_state(encoder) == FLAC__STREAM_ENCODER_OK \endcode
1015 * \retval FLAC__bool
1016 * \c true if successful, else \c false; in this case, check the
1017 * encoder state with FLAC__stream_encoder_get_state() to see what
1020 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples);
1022 /** Submit data for encoding.
1023 * This version allows you to supply the input data where the channels
1024 * are interleaved into a single array (i.e. channel0_sample0,
1025 * channel1_sample0, ... , channelN_sample0, channel0_sample1, ...).
1026 * The samples need not be block-aligned but they must be
1027 * sample-aligned, i.e. the first value should be channel0_sample0
1028 * and the last value channelN_sampleM.
1030 * \param encoder An initialized encoder instance in the OK state.
1031 * \param buffer An array of channel-interleaved data (see above).
1032 * \param samples The number of samples in one channel, the same as for
1033 * FLAC__stream_encoder_process(). For example, if
1034 * encoding two channels, \c 1000 \a samples corresponds
1035 * to a \a buffer of 2000 values.
1037 * \code encoder != NULL \endcode
1038 * \code FLAC__stream_encoder_get_state(encoder) == FLAC__STREAM_ENCODER_OK \endcode
1039 * \retval FLAC__bool
1040 * \c true if successful, else \c false; in this case, check the
1041 * encoder state with FLAC__stream_encoder_get_state() to see what
1044 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples);