minor formatting
[platform/upstream/flac.git] / include / FLAC / stream_encoder.h
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002,2003  Josh Coalson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
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.
14  *
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.
18  *
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.
30  */
31
32 #ifndef FLAC__STREAM_ENCODER_H
33 #define FLAC__STREAM_ENCODER_H
34
35 #include "export.h"
36 #include "format.h"
37 #include "stream_decoder.h"
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43
44 /** \file include/FLAC/stream_encoder.h
45  *
46  *  \brief
47  *  This module contains the functions which implement the stream
48  *  encoder.
49  *
50  *  See the detailed documentation in the
51  *  \link flac_stream_encoder stream encoder \endlink module.
52  */
53
54 /** \defgroup flac_encoder FLAC/ *_encoder.h: encoder interfaces
55  *  \ingroup flac
56  *
57  *  \brief
58  *  This module describes the two encoder layers provided by libFLAC.
59  *
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.
66  *
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.
70  */
71
72 /** \defgroup flac_stream_encoder FLAC/stream_encoder.h: stream encoder interface
73  *  \ingroup flac_encoder
74  *
75  *  \brief
76  *  This module contains the functions which implement the stream
77  *  encoder.
78  *
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
89  *   to be written.
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().
96  *
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().
103  *
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
115  * constructor.
116  *
117  * The user must provide function pointers for the following callbacks:
118  *
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).
128  *
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.
132  *
133  * After initializing the instance, the user may feed audio data to the
134  * encoder in one of two ways:
135  *
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
139  *   block-aligned.
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.
147  *
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
153  * stream.
154  *
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.
162  *
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.
167  *
168  * \note
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.
174  *
175  * \note
176  * FLAC__stream_encoder_finish() resets all settings to the constructor
177  * defaults, including the callbacks.
178  *
179  * \{
180  */
181
182
183 /** State values for a FLAC__StreamEncoder
184  *
185  *  The encoder's state can be obtained by calling FLAC__stream_encoder_get_state().
186  */
187 typedef enum {
188
189         FLAC__STREAM_ENCODER_OK = 0,
190         /**< The encoder is in the normal OK state. */
191
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().
195          */
196
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.
200          */
201
202         FLAC__STREAM_ENCODER_INVALID_CALLBACK,
203         /**< The encoder was initialized before setting all the required callbacks. */
204
205         FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS,
206         /**< The encoder has an invalid setting for number of channels. */
207
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
211          * only up to 24 bps.
212          */
213
214         FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE,
215         /**< The encoder has an invalid setting for the input sample rate. */
216
217         FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE,
218         /**< The encoder has an invalid setting for the block size. */
219
220         FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER,
221         /**< The encoder has an invalid setting for the maximum LPC order. */
222
223         FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION,
224         /**< The encoder has an invalid setting for the precision of the quantized linear predictor coefficients. */
225
226         FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH,
227         /**< Mid/side coding was specified but the number of channels is not equal to 2. */
228
229         FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH,
230         /**< Deprecated. */
231
232         FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE,
233         /**< Loose mid/side coding was specified but mid/side coding was not. */
234
235         FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER,
236         /**< The specified block size is less than the maximum LPC order. */
237
238         FLAC__STREAM_ENCODER_NOT_STREAMABLE,
239         /**< The encoder is bound to the "streamable subset" but other settings violate it. */
240
241         FLAC__STREAM_ENCODER_FRAMING_ERROR,
242         /**< An error occurred while writing the stream; usually, the write_callback returned an error. */
243
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
251          */
252
253         FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING,
254         /**< An error occurred while writing the stream; usually, the write_callback returned an error. */
255
256         FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING,
257         /**< The write_callback returned an error. */
258
259         FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR,
260         /**< Memory allocation failed. */
261
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.
266          */
267
268         FLAC__STREAM_ENCODER_UNINITIALIZED
269         /**< The encoder is in the uninitialized state. */
270
271 } FLAC__StreamEncoderState;
272
273 /** Maps a FLAC__StreamEncoderState to a C string.
274  *
275  *  Using a FLAC__StreamEncoderState as the index to this array
276  *  will give the string equivalent.  The contents should not be modified.
277  */
278 extern FLAC_API const char * const FLAC__StreamEncoderStateString[];
279
280 /** Return values for the FLAC__StreamEncoder write callback.
281  */
282 typedef enum {
283
284         FLAC__STREAM_ENCODER_WRITE_STATUS_OK = 0,
285         /**< The write was OK and encoding can continue. */
286
287         FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR
288         /**< An unrecoverable error occurred.  The encoder will return from the process call. */
289
290 } FLAC__StreamEncoderWriteStatus;
291
292 /** Maps a FLAC__StreamEncoderWriteStatus to a C string.
293  *
294  *  Using a FLAC__StreamEncoderWriteStatus as the index to this array
295  *  will give the string equivalent.  The contents should not be modified.
296  */
297 extern FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[];
298
299
300 /***********************************************************************
301  *
302  * class FLAC__StreamEncoder
303  *
304  ***********************************************************************/
305
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.
311  */
312 typedef struct {
313         struct FLAC__StreamEncoderProtected *protected_; /* avoid the C++ keyword 'protected' */
314         struct FLAC__StreamEncoderPrivate *private_; /* avoid the C++ keyword 'private' */
315 } FLAC__StreamEncoder;
316
317 /** Signature for the write callback.
318  *  See FLAC__stream_encoder_set_write_callback() for more info.
319  *
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.
331  */
332 typedef FLAC__StreamEncoderWriteStatus (*FLAC__StreamEncoderWriteCallback)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
333
334 /** Signature for the metadata callback.
335  *  See FLAC__stream_encoder_set_metadata_callback() for more info.
336  *
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().
341  */
342 typedef void (*FLAC__StreamEncoderMetadataCallback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data);
343
344
345 /***********************************************************************
346  *
347  * Class constructor/destructor
348  *
349  ***********************************************************************/
350
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.
354  *
355  * \retval FLAC__StreamEncoder*
356  *    \c NULL if there was an error allocating memory, else the new instance.
357  */
358 FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new();
359
360 /** Free an encoder instance.  Deletes the object pointed to by \a encoder.
361  *
362  * \param encoder  A pointer to an existing encoder.
363  * \assert
364  *    \code encoder != NULL \endcode
365  */
366 FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder);
367
368
369 /***********************************************************************
370  *
371  * Public class method prototypes
372  *
373  ***********************************************************************/
374
375 /** Set the "verify" flag.  If \c true, the encoder will verify it's own
376  *  encoded output by feeding it through an internal decoder and comparing
377  *  the original signal against the decoded signal.  If a mismatch occurs,
378  *  the process call will return \c false.  Note that this will slow the
379  *  encoding process by the extra time required for decoding and comparison.
380  *
381  * \default \c false
382  * \param  encoder  An encoder instance to set.
383  * \param  value    Flag value (see above).
384  * \assert
385  *    \code encoder != NULL \endcode
386  * \retval FLAC__bool
387  *    \c false if the encoder is already initialized, else \c true.
388  */
389 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value);
390
391 /** Set the "streamable subset" flag.  If \c true, the encoder will comply
392  *  with the subset (see the format specification) and will check the
393  *  settings during FLAC__stream_encoder_init() to see if all settings
394  *  comply.  If \c false, the settings may take advantage of the full
395  *  range that the format allows.
396  *
397  *  Make sure you know what it entails before setting this to \c false.
398  *
399  * \default \c true
400  * \param  encoder  An encoder instance to set.
401  * \param  value    Flag value (see above).
402  * \assert
403  *    \code encoder != NULL \endcode
404  * \retval FLAC__bool
405  *    \c false if the encoder is already initialized, else \c true.
406  */
407 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value);
408
409 /** Set to \c true to enable mid-side encoding on stereo input.  The
410  *  number of channels must be 2.  Set to \c false to use only
411  *  independent channel coding.
412  *
413  * \default \c false
414  * \param  encoder  An encoder instance to set.
415  * \param  value    Flag value (see above).
416  * \assert
417  *    \code encoder != NULL \endcode
418  * \retval FLAC__bool
419  *    \c false if the encoder is already initialized, else \c true.
420  */
421 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value);
422
423 /** Set to \c true to enable adaptive switching between mid-side and
424  *  left-right encoding on stereo input.  The number of channels must
425  *  be 2.  Set to \c false to use exhaustive searching.  In either
426  *  case, the mid/side stereo setting must be \c true.
427  *
428  * \default \c false
429  * \param  encoder  An encoder instance to set.
430  * \param  value    Flag value (see above).
431  * \assert
432  *    \code encoder != NULL \endcode
433  * \retval FLAC__bool
434  *    \c false if the encoder is already initialized, else \c true.
435  */
436 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value);
437
438 /** Set the number of channels to be encoded.
439  *
440  * \default \c 2
441  * \param  encoder  An encoder instance to set.
442  * \param  value    See above.
443  * \assert
444  *    \code encoder != NULL \endcode
445  * \retval FLAC__bool
446  *    \c false if the encoder is already initialized, else \c true.
447  */
448 FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value);
449
450 /** Set the sample resolution of the input to be encoded.
451  *
452  * \warning
453  * Do not feed the encoder data that is wider than the value you
454  * set here or you will generate an invalid stream.
455  *
456  * \default \c 16
457  * \param  encoder  An encoder instance to set.
458  * \param  value    See above.
459  * \assert
460  *    \code encoder != NULL \endcode
461  * \retval FLAC__bool
462  *    \c false if the encoder is already initialized, else \c true.
463  */
464 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value);
465
466 /** Set the sample rate (in Hz) of the input to be encoded.
467  *
468  * \default \c 44100
469  * \param  encoder  An encoder instance to set.
470  * \param  value    See above.
471  * \assert
472  *    \code encoder != NULL \endcode
473  * \retval FLAC__bool
474  *    \c false if the encoder is already initialized, else \c true.
475  */
476 FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value);
477
478 /** Set the blocksize to use while encoding.
479  *
480  * \default \c 1152
481  * \param  encoder  An encoder instance to set.
482  * \param  value    See above.
483  * \assert
484  *    \code encoder != NULL \endcode
485  * \retval FLAC__bool
486  *    \c false if the encoder is already initialized, else \c true.
487  */
488 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value);
489
490 /** Set the maximum LPC order, or \c 0 to use only the fixed predictors.
491  *
492  * \default \c 0
493  * \param  encoder  An encoder instance to set.
494  * \param  value    See above.
495  * \assert
496  *    \code encoder != NULL \endcode
497  * \retval FLAC__bool
498  *    \c false if the encoder is already initialized, else \c true.
499  */
500 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value);
501
502 /** Set the precision, in bits, of the quantized linear predictor
503  *  coefficients, or \c 0 to let the encoder select it based on the
504  *  blocksize.
505  *
506  * \note
507  * In the current implementation, qlp_coeff_precision + bits_per_sample must
508  * be less than 32.
509  *
510  * \default \c 0
511  * \param  encoder  An encoder instance to set.
512  * \param  value    See above.
513  * \assert
514  *    \code encoder != NULL \endcode
515  * \retval FLAC__bool
516  *    \c false if the encoder is already initialized, else \c true.
517  */
518 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value);
519
520 /** Set to \c false to use only the specified quantized linear predictor
521  *  coefficient precision, or \c true to search neighboring precision
522  *  values and use the best one.
523  *
524  * \default \c false
525  * \param  encoder  An encoder instance to set.
526  * \param  value    See above.
527  * \assert
528  *    \code encoder != NULL \endcode
529  * \retval FLAC__bool
530  *    \c false if the encoder is already initialized, else \c true.
531  */
532 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value);
533
534 /** Deprecated.  Setting this value has no effect.
535  *
536  * \default \c false
537  * \param  encoder  An encoder instance to set.
538  * \param  value    See above.
539  * \assert
540  *    \code encoder != NULL \endcode
541  * \retval FLAC__bool
542  *    \c false if the encoder is already initialized, else \c true.
543  */
544 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value);
545
546 /** Set to \c false to let the encoder estimate the best model order
547  *  based on the residual signal energy, or \c true to force the
548  *  encoder to evaluate all order models and select the best.
549  *
550  * \default \c false
551  * \param  encoder  An encoder instance to set.
552  * \param  value    See above.
553  * \assert
554  *    \code encoder != NULL \endcode
555  * \retval FLAC__bool
556  *    \c false if the encoder is already initialized, else \c true.
557  */
558 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value);
559
560 /** Set the minimum partition order to search when coding the residual.
561  *  This is used in tandem with
562  *  FLAC__stream_encoder_set_max_residual_partition_order().
563  *
564  *  The partition order determines the context size in the residual.
565  *  The context size will be approximately <tt>blocksize / (2 ^ order)</tt>.
566  *
567  *  Set both min and max values to \c 0 to force a single context,
568  *  whose Rice parameter is based on the residual signal variance.
569  *  Otherwise, set a min and max order, and the encoder will search
570  *  all orders, using the mean of each context for its Rice parameter,
571  *  and use the best.
572  *
573  * \default \c 0
574  * \param  encoder  An encoder instance to set.
575  * \param  value    See above.
576  * \assert
577  *    \code encoder != NULL \endcode
578  * \retval FLAC__bool
579  *    \c false if the encoder is already initialized, else \c true.
580  */
581 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value);
582
583 /** Set the maximum partition order to search when coding the residual.
584  *  This is used in tandem with
585  *  FLAC__stream_encoder_set_min_residual_partition_order().
586  *
587  *  The partition order determines the context size in the residual.
588  *  The context size will be approximately <tt>blocksize / (2 ^ order)</tt>.
589  *
590  *  Set both min and max values to \c 0 to force a single context,
591  *  whose Rice parameter is based on the residual signal variance.
592  *  Otherwise, set a min and max order, and the encoder will search
593  *  all orders, using the mean of each context for its Rice parameter,
594  *  and use the best.
595  *
596  * \default \c 0
597  * \param  encoder  An encoder instance to set.
598  * \param  value    See above.
599  * \assert
600  *    \code encoder != NULL \endcode
601  * \retval FLAC__bool
602  *    \c false if the encoder is already initialized, else \c true.
603  */
604 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value);
605
606 /** Deprecated.  Setting this value has no effect.
607  *
608  * \default \c 0
609  * \param  encoder  An encoder instance to set.
610  * \param  value    See above.
611  * \assert
612  *    \code encoder != NULL \endcode
613  * \retval FLAC__bool
614  *    \c false if the encoder is already initialized, else \c true.
615  */
616 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value);
617
618 /** Set an estimate of the total samples that will be encoded.
619  *  This is merely an estimate and may be set to \c 0 if unknown.
620  *  This value will be written to the STREAMINFO block before encoding,
621  *  and can remove the need for the caller to rewrite the value later
622  *  if the value is known before encoding.
623  *
624  * \default \c 0
625  * \param  encoder  An encoder instance to set.
626  * \param  value    See above.
627  * \assert
628  *    \code encoder != NULL \endcode
629  * \retval FLAC__bool
630  *    \c false if the encoder is already initialized, else \c true.
631  */
632 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value);
633
634 /** Set the metadata blocks to be emitted to the stream before encoding.
635  *  A value of \c NULL, \c 0 implies no metadata; otherwise, supply an
636  *  array of pointers to metadata blocks.  The array is non-const since
637  *  the encoder may need to change the \a is_last flag inside them.
638  *  Otherwise, the encoder will not modify or free the blocks.  It is up
639  *  to the caller to free the metadata blocks after encoding.
640  *
641  * \note
642  * The encoder stores only the \a metadata pointer; the passed-in array
643  * must survive at least until after FLAC__stream_encoder_init() returns.
644  * Do not modify the array or free the blocks until then.
645  *
646  * \note
647  * The STREAMINFO block is always written and no STREAMINFO block may
648  * occur in the supplied array.
649  *
650  * \note
651  * By default the encoder does not create a SEEKTABLE.  If one is supplied
652  * in the \a metadata array it will be written verbatim.  However by itself
653  * this is not very useful as the user will not know the stream offsets for
654  * the seekpoints ahead of time.  You must use the seekable stream encoder
655  * to generate a legal seektable
656  * (see FLAC__seekable_stream_encoder_set_metadata())
657  *
658  * \note
659  * A VORBIS_COMMENT block may be supplied.  The vendor string in it
660  * will be ignored.  libFLAC will use it's own vendor string. libFLAC
661  * will not modify the passed-in VORBIS_COMMENT's vendor string, it
662  * will simply write it's own into the stream.  If no VORBIS_COMMENT
663  * block is present in the \a metadata array, libFLAC will write an
664  * empty one, containing only the vendor string.
665  *
666  * \default \c NULL, 0
667  * \param  encoder     An encoder instance to set.
668  * \param  metadata    See above.
669  * \param  num_blocks  See above.
670  * \assert
671  *    \code encoder != NULL \endcode
672  * \retval FLAC__bool
673  *    \c false if the encoder is already initialized, else \c true.
674  */
675 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks);
676
677 /** Set the write callback.
678  *  The supplied function will be called by the encoder anytime there is raw
679  *  encoded data ready to write.  It may include metadata mixed with encoded
680  *  audio frames and the data is not guaranteed to be aligned on frame or
681  *  metadata block boundaries.
682  *
683  *  The only duty of the callback is to write out the \a bytes worth of data
684  *  in \a buffer to the current position in the output stream.  The arguments
685  *  \a samples and \a current_frame are purely informational.  If \a samples
686  *  is greater than \c 0, then \a current_frame will hold the current frame
687  *  number that is being written; otherwise, the write callback is being called
688  *  to write metadata.
689  *
690  * \note
691  * The callback is mandatory and must be set before initialization.
692  *
693  * \default \c NULL
694  * \param  encoder  An encoder instance to set.
695  * \param  value    See above.
696  * \assert
697  *    \code encoder != NULL \endcode
698  *    \code value != NULL \endcode
699  * \retval FLAC__bool
700  *    \c false if the encoder is already initialized, else \c true.
701  */
702 FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value);
703
704 /** Set the metadata callback.
705  *  The supplied function will be called once at the end of encoding with
706  *  the populated STREAMINFO structure.  This is so file encoders can seek
707  *  back to the beginning of the file and write the STREAMINFO block with
708  *  the correct statistics after encoding (like minimum/maximum frame size
709  *  and total samples).
710  *
711  * \note
712  * The callback is mandatory and must be set before initialization.
713  *
714  * \default \c NULL
715  * \param  encoder  An encoder instance to set.
716  * \param  value    See above.
717  * \assert
718  *    \code encoder != NULL \endcode
719  *    \code value != NULL \endcode
720  * \retval FLAC__bool
721  *    \c false if the encoder is already initialized, else \c true.
722  */
723 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value);
724
725 /** Set the client data to be passed back to callbacks.
726  *  This value will be supplied to callbacks in their \a client_data
727  *  argument.
728  *
729  * \default \c NULL
730  * \param  encoder  An encoder instance to set.
731  * \param  value    See above.
732  * \assert
733  *    \code encoder != NULL \endcode
734  * \retval FLAC__bool
735  *    \c false if the encoder is already initialized, else \c true.
736  */
737 FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value);
738
739 /** Get the current encoder state.
740  *
741  * \param  encoder  An encoder instance to query.
742  * \assert
743  *    \code encoder != NULL \endcode
744  * \retval FLAC__StreamEncoderState
745  *    The current encoder state.
746  */
747 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder);
748
749 /** Get the state of the verify stream decoder.
750  *  Useful when the stream encoder state is
751  *  \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR.
752  *
753  * \param  encoder  An encoder instance to query.
754  * \assert
755  *    \code encoder != NULL \endcode
756  * \retval FLAC__StreamDecoderState
757  *    The verify stream decoder state.
758  */
759 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder);
760
761 /** Get the current encoder state as a C string.
762  *  This version automatically resolves
763  *  \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR by getting the
764  *  verify decoder's state.
765  *
766  * \param  encoder  A encoder instance to query.
767  * \assert
768  *    \code encoder != NULL \endcode
769  * \retval const char *
770  *    The encoder state as a C string.  Do not modify the contents.
771  */
772 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder);
773
774 /** Get relevant values about the nature of a verify decoder error.
775  *  Useful when the stream encoder state is
776  *  \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR.  The arguments should
777  *  be addresses in which the stats will be returned, or NULL if value
778  *  is not desired.
779  *
780  * \param  encoder  An encoder instance to query.
781  * \param  absolute_sample  The absolute sample number of the mismatch.
782  * \param  frame_number  The number of the frame in which the mismatch occurred.
783  * \param  channel       The channel in which the mismatch occurred.
784  * \param  sample        The number of the sample (relative to the frame) in
785  *                       which the mismatch occurred.
786  * \param  expected      The expected value for the sample in question.
787  * \param  got           The actual value returned by the decoder.
788  * \assert
789  *    \code encoder != NULL \endcode
790  */
791 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
793 /** Get the "verify" flag.
794  *
795  * \param  encoder  An encoder instance to query.
796  * \assert
797  *    \code encoder != NULL \endcode
798  * \retval FLAC__bool
799  *    See FLAC__stream_encoder_set_verify().
800  */
801 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder);
802
803 /** Get the "streamable subset" flag.
804  *
805  * \param  encoder  An encoder instance to query.
806  * \assert
807  *    \code encoder != NULL \endcode
808  * \retval FLAC__bool
809  *    See FLAC__stream_encoder_set_streamable_subset().
810  */
811 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder);
812
813 /** Get the "mid/side stereo coding" flag.
814  *
815  * \param  encoder  An encoder instance to query.
816  * \assert
817  *    \code encoder != NULL \endcode
818  * \retval FLAC__bool
819  *    See FLAC__stream_encoder_get_do_mid_side_stereo().
820  */
821 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder);
822
823 /** Get the "adaptive mid/side switching" flag.
824  *
825  * \param  encoder  An encoder instance to query.
826  * \assert
827  *    \code encoder != NULL \endcode
828  * \retval FLAC__bool
829  *    See FLAC__stream_encoder_set_loose_mid_side_stereo().
830  */
831 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder);
832
833 /** Get the number of input channels being processed.
834  *
835  * \param  encoder  An encoder instance to query.
836  * \assert
837  *    \code encoder != NULL \endcode
838  * \retval unsigned
839  *    See FLAC__stream_encoder_set_channels().
840  */
841 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder);
842
843 /** Get the input sample resolution setting.
844  *
845  * \param  encoder  An encoder instance to query.
846  * \assert
847  *    \code encoder != NULL \endcode
848  * \retval unsigned
849  *    See FLAC__stream_encoder_set_bits_per_sample().
850  */
851 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder);
852
853 /** Get the input sample rate setting.
854  *
855  * \param  encoder  An encoder instance to query.
856  * \assert
857  *    \code encoder != NULL \endcode
858  * \retval unsigned
859  *    See FLAC__stream_encoder_set_sample_rate().
860  */
861 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder);
862
863 /** Get the blocksize setting.
864  *
865  * \param  encoder  An encoder instance to query.
866  * \assert
867  *    \code encoder != NULL \endcode
868  * \retval unsigned
869  *    See FLAC__stream_encoder_set_blocksize().
870  */
871 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder);
872
873 /** Get the maximum LPC order setting.
874  *
875  * \param  encoder  An encoder instance to query.
876  * \assert
877  *    \code encoder != NULL \endcode
878  * \retval unsigned
879  *    See FLAC__stream_encoder_set_max_lpc_order().
880  */
881 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder);
882
883 /** Get the quantized linear predictor coefficient precision setting.
884  *
885  * \param  encoder  An encoder instance to query.
886  * \assert
887  *    \code encoder != NULL \endcode
888  * \retval unsigned
889  *    See FLAC__stream_encoder_set_qlp_coeff_precision().
890  */
891 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder);
892
893 /** Get the qlp coefficient precision search flag.
894  *
895  * \param  encoder  An encoder instance to query.
896  * \assert
897  *    \code encoder != NULL \endcode
898  * \retval FLAC__bool
899  *    See FLAC__stream_encoder_set_do_qlp_coeff_prec_search().
900  */
901 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder);
902
903 /** Get the "escape coding" flag.
904  *
905  * \param  encoder  An encoder instance to query.
906  * \assert
907  *    \code encoder != NULL \endcode
908  * \retval FLAC__bool
909  *    See FLAC__stream_encoder_set_do_escape_coding().
910  */
911 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder);
912
913 /** Get the exhaustive model search flag.
914  *
915  * \param  encoder  An encoder instance to query.
916  * \assert
917  *    \code encoder != NULL \endcode
918  * \retval FLAC__bool
919  *    See FLAC__stream_encoder_set_do_exhaustive_model_search().
920  */
921 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder);
922
923 /** Get the minimum residual partition order setting.
924  *
925  * \param  encoder  An encoder instance to query.
926  * \assert
927  *    \code encoder != NULL \endcode
928  * \retval unsigned
929  *    See FLAC__stream_encoder_set_min_residual_partition_order().
930  */
931 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder);
932
933 /** Get maximum residual partition order setting.
934  *
935  * \param  encoder  An encoder instance to query.
936  * \assert
937  *    \code encoder != NULL \endcode
938  * \retval unsigned
939  *    See FLAC__stream_encoder_set_max_residual_partition_order().
940  */
941 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder);
942
943 /** Get the Rice parameter search distance setting.
944  *
945  * \param  encoder  An encoder instance to query.
946  * \assert
947  *    \code encoder != NULL \endcode
948  * \retval unsigned
949  *    See FLAC__stream_encoder_set_rice_parameter_search_dist().
950  */
951 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder);
952
953 /** Get the previously set estimate of the total samples to be encoded.
954  *  The encoder merely mimics back the value given to
955  *  FLAC__stream_encoder_set_total_samples_estimate() since it has no
956  *  other way of knowing how many samples the user will encode.
957  *
958  * \param  encoder  An encoder instance to set.
959  * \assert
960  *    \code encoder != NULL \endcode
961  * \retval FLAC__uint64
962  *    See FLAC__stream_encoder_get_total_samples_estimate().
963  */
964 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder);
965
966 /** Initialize the encoder instance.
967  *  Should be called after FLAC__stream_encoder_new() and
968  *  FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process()
969  *  or FLAC__stream_encoder_process_interleaved().  Will set and return
970  *  the encoder state, which will be FLAC__STREAM_ENCODER_OK if
971  *  initialization succeeded.
972  *
973  *  The call to FLAC__stream_encoder_init() currently will also immediately
974  *  call the write callback several times, once with the \c fLaC signature,
975  *  and once for each encoded metadata block.
976  *
977  * \param  encoder  An uninitialized encoder instance.
978  * \assert
979  *    \code encoder != NULL \endcode
980  * \retval FLAC__StreamEncoderState
981  *    \c FLAC__STREAM_ENCODER_OK if initialization was successful; see
982  *    FLAC__StreamEncoderState for the meanings of other return values.
983  */
984 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder);
985
986 /** Finish the encoding process.
987  *  Flushes the encoding buffer, releases resources, resets the encoder
988  *  settings to their defaults, and returns the encoder state to
989  *  FLAC__STREAM_ENCODER_UNINITIALIZED.  Note that this can generate
990  *  one or more write callbacks before returning, and will generate
991  *  a metadata callback.
992  *
993  *  In the event of a prematurely-terminated encode, it is not strictly
994  *  necessary to call this immediately before FLAC__stream_encoder_delete()
995  *  but it is good practice to match every FLAC__stream_encoder_init()
996  *  with a FLAC__stream_encoder_finish().
997  *
998  * \param  encoder  An uninitialized encoder instance.
999  * \assert
1000  *    \code encoder != NULL \endcode
1001  */
1002 FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder);
1003
1004 /** Submit data for encoding.
1005  *  This version allows you to supply the input data via an array of
1006  *  pointers, each pointer pointing to an array of \a samples samples
1007  *  representing one channel.  The samples need not be block-aligned,
1008  *  but each channel should have the same number of samples.
1009  *
1010  * \param  encoder  An initialized encoder instance in the OK state.
1011  * \param  buffer   An array of pointers to each channel's signal.
1012  * \param  samples  The number of samples in one channel.
1013  * \assert
1014  *    \code encoder != NULL \endcode
1015  *    \code FLAC__stream_encoder_get_state(encoder) == FLAC__STREAM_ENCODER_OK \endcode
1016  * \retval FLAC__bool
1017  *    \c true if successful, else \c false; in this case, check the
1018  *    encoder state with FLAC__stream_encoder_get_state() to see what
1019  *    went wrong.
1020  */
1021 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples);
1022
1023 /** Submit data for encoding.
1024  *  This version allows you to supply the input data where the channels
1025  *  are interleaved into a single array (i.e. channel0_sample0,
1026  *  channel1_sample0, ... , channelN_sample0, channel0_sample1, ...).
1027  *  The samples need not be block-aligned but they must be
1028  *  sample-aligned, i.e. the first value should be channel0_sample0
1029  *  and the last value channelN_sampleM.
1030  *
1031  * \param  encoder  An initialized encoder instance in the OK state.
1032  * \param  buffer   An array of channel-interleaved data (see above).
1033  * \param  samples  The number of samples in one channel, the same as for
1034  *                  FLAC__stream_encoder_process().  For example, if
1035  *                  encoding two channels, \c 1000 \a samples corresponds
1036  *                  to a \a buffer of 2000 values.
1037  * \assert
1038  *    \code encoder != NULL \endcode
1039  *    \code FLAC__stream_encoder_get_state(encoder) == FLAC__STREAM_ENCODER_OK \endcode
1040  * \retval FLAC__bool
1041  *    \c true if successful, else \c false; in this case, check the
1042  *    encoder state with FLAC__stream_encoder_get_state() to see what
1043  *    went wrong.
1044  */
1045 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples);
1046
1047 /* \} */
1048
1049 #ifdef __cplusplus
1050 }
1051 #endif
1052
1053 #endif