change license verbiage to Xiph's
[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  * Public class method prototypes
371  *
372  ***********************************************************************/
373
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.
379  *
380  * \default \c false
381  * \param  encoder  An encoder instance to set.
382  * \param  value    Flag value (see above).
383  * \assert
384  *    \code encoder != NULL \endcode
385  * \retval FLAC__bool
386  *    \c false if the encoder is already initialized, else \c true.
387  */
388 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value);
389
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.
395  *
396  *  Make sure you know what it entails before setting this to \c false.
397  *
398  * \default \c true
399  * \param  encoder  An encoder instance to set.
400  * \param  value    Flag value (see above).
401  * \assert
402  *    \code encoder != NULL \endcode
403  * \retval FLAC__bool
404  *    \c false if the encoder is already initialized, else \c true.
405  */
406 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value);
407
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.
411  *
412  * \default \c false
413  * \param  encoder  An encoder instance to set.
414  * \param  value    Flag value (see above).
415  * \assert
416  *    \code encoder != NULL \endcode
417  * \retval FLAC__bool
418  *    \c false if the encoder is already initialized, else \c true.
419  */
420 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value);
421
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.
426  *
427  * \default \c false
428  * \param  encoder  An encoder instance to set.
429  * \param  value    Flag value (see above).
430  * \assert
431  *    \code encoder != NULL \endcode
432  * \retval FLAC__bool
433  *    \c false if the encoder is already initialized, else \c true.
434  */
435 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value);
436
437 /** Set the number of channels to be encoded.
438  *
439  * \default \c 2
440  * \param  encoder  An encoder instance to set.
441  * \param  value    See above.
442  * \assert
443  *    \code encoder != NULL \endcode
444  * \retval FLAC__bool
445  *    \c false if the encoder is already initialized, else \c true.
446  */
447 FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value);
448
449 /** Set the sample resolution of the input to be encoded.
450  *
451  * \warning
452  * Do not feed the encoder data that is wider than the value you
453  * set here or you will generate an invalid stream.
454  *
455  * \default \c 16
456  * \param  encoder  An encoder instance to set.
457  * \param  value    See above.
458  * \assert
459  *    \code encoder != NULL \endcode
460  * \retval FLAC__bool
461  *    \c false if the encoder is already initialized, else \c true.
462  */
463 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value);
464
465 /** Set the sample rate (in Hz) of the input to be encoded.
466  *
467  * \default \c 44100
468  * \param  encoder  An encoder instance to set.
469  * \param  value    See above.
470  * \assert
471  *    \code encoder != NULL \endcode
472  * \retval FLAC__bool
473  *    \c false if the encoder is already initialized, else \c true.
474  */
475 FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value);
476
477 /** Set the blocksize to use while encoding.
478  *
479  * \default \c 1152
480  * \param  encoder  An encoder instance to set.
481  * \param  value    See above.
482  * \assert
483  *    \code encoder != NULL \endcode
484  * \retval FLAC__bool
485  *    \c false if the encoder is already initialized, else \c true.
486  */
487 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value);
488
489 /** Set the maximum LPC order, or \c 0 to use only the fixed predictors.
490  *
491  * \default \c 0
492  * \param  encoder  An encoder instance to set.
493  * \param  value    See above.
494  * \assert
495  *    \code encoder != NULL \endcode
496  * \retval FLAC__bool
497  *    \c false if the encoder is already initialized, else \c true.
498  */
499 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value);
500
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
503  *  blocksize.
504  *
505  * \note
506  * In the current implementation, qlp_coeff_precision + bits_per_sample must
507  * be less than 32.
508  *
509  * \default \c 0
510  * \param  encoder  An encoder instance to set.
511  * \param  value    See above.
512  * \assert
513  *    \code encoder != NULL \endcode
514  * \retval FLAC__bool
515  *    \c false if the encoder is already initialized, else \c true.
516  */
517 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value);
518
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.
522  *
523  * \default \c false
524  * \param  encoder  An encoder instance to set.
525  * \param  value    See above.
526  * \assert
527  *    \code encoder != NULL \endcode
528  * \retval FLAC__bool
529  *    \c false if the encoder is already initialized, else \c true.
530  */
531 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value);
532
533 /** Deprecated.  Setting this value has no effect.
534  *
535  * \default \c false
536  * \param  encoder  An encoder instance to set.
537  * \param  value    See above.
538  * \assert
539  *    \code encoder != NULL \endcode
540  * \retval FLAC__bool
541  *    \c false if the encoder is already initialized, else \c true.
542  */
543 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value);
544
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.
548  *
549  * \default \c false
550  * \param  encoder  An encoder instance to set.
551  * \param  value    See above.
552  * \assert
553  *    \code encoder != NULL \endcode
554  * \retval FLAC__bool
555  *    \c false if the encoder is already initialized, else \c true.
556  */
557 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value);
558
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().
562  *
563  *  The partition order determines the context size in the residual.
564  *  The context size will be approximately <tt>blocksize / (2 ^ order)</tt>.
565  *
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,
570  *  and use the best.
571  *
572  * \default \c 0
573  * \param  encoder  An encoder instance to set.
574  * \param  value    See above.
575  * \assert
576  *    \code encoder != NULL \endcode
577  * \retval FLAC__bool
578  *    \c false if the encoder is already initialized, else \c true.
579  */
580 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value);
581
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().
585  *
586  *  The partition order determines the context size in the residual.
587  *  The context size will be approximately <tt>blocksize / (2 ^ order)</tt>.
588  *
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,
593  *  and use the best.
594  *
595  * \default \c 0
596  * \param  encoder  An encoder instance to set.
597  * \param  value    See above.
598  * \assert
599  *    \code encoder != NULL \endcode
600  * \retval FLAC__bool
601  *    \c false if the encoder is already initialized, else \c true.
602  */
603 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value);
604
605 /** Deprecated.  Setting this value has no effect.
606  *
607  * \default \c 0
608  * \param  encoder  An encoder instance to set.
609  * \param  value    See above.
610  * \assert
611  *    \code encoder != NULL \endcode
612  * \retval FLAC__bool
613  *    \c false if the encoder is already initialized, else \c true.
614  */
615 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value);
616
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.
622  *
623  * \default \c 0
624  * \param  encoder  An encoder instance to set.
625  * \param  value    See above.
626  * \assert
627  *    \code encoder != NULL \endcode
628  * \retval FLAC__bool
629  *    \c false if the encoder is already initialized, else \c true.
630  */
631 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value);
632
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.
639  *
640  * \note
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.
644  *
645  * \note
646  * The STREAMINFO block is always written and no STREAMINFO block may
647  * occur in the supplied array.
648  *
649  * \note
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())
656  *
657  * \note
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.
664  *
665  * \default \c NULL, 0
666  * \param  encoder     An encoder instance to set.
667  * \param  metadata    See above.
668  * \param  num_blocks  See above.
669  * \assert
670  *    \code encoder != NULL \endcode
671  * \retval FLAC__bool
672  *    \c false if the encoder is already initialized, else \c true.
673  */
674 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks);
675
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.
681  *
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
687  *  to write metadata.
688  *
689  * \note
690  * The callback is mandatory and must be set before initialization.
691  *
692  * \default \c NULL
693  * \param  encoder  An encoder instance to set.
694  * \param  value    See above.
695  * \assert
696  *    \code encoder != NULL \endcode
697  *    \code value != NULL \endcode
698  * \retval FLAC__bool
699  *    \c false if the encoder is already initialized, else \c true.
700  */
701 FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value);
702
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).
709  *
710  * \note
711  * The callback is mandatory and must be set before initialization.
712  *
713  * \default \c NULL
714  * \param  encoder  An encoder instance to set.
715  * \param  value    See above.
716  * \assert
717  *    \code encoder != NULL \endcode
718  *    \code value != NULL \endcode
719  * \retval FLAC__bool
720  *    \c false if the encoder is already initialized, else \c true.
721  */
722 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value);
723
724 /** Set the client data to be passed back to callbacks.
725  *  This value will be supplied to callbacks in their \a client_data
726  *  argument.
727  *
728  * \default \c NULL
729  * \param  encoder  An encoder instance to set.
730  * \param  value    See above.
731  * \assert
732  *    \code encoder != NULL \endcode
733  * \retval FLAC__bool
734  *    \c false if the encoder is already initialized, else \c true.
735  */
736 FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value);
737
738 /** Get the current encoder state.
739  *
740  * \param  encoder  An encoder instance to query.
741  * \assert
742  *    \code encoder != NULL \endcode
743  * \retval FLAC__StreamEncoderState
744  *    The current encoder state.
745  */
746 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder);
747
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.
751  *
752  * \param  encoder  An encoder instance to query.
753  * \assert
754  *    \code encoder != NULL \endcode
755  * \retval FLAC__StreamDecoderState
756  *    The verify stream decoder state.
757  */
758 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder);
759
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.
764  *
765  * \param  encoder  A encoder instance to query.
766  * \assert
767  *    \code encoder != NULL \endcode
768  * \retval const char *
769  *    The encoder state as a C string.  Do not modify the contents.
770  */
771 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder);
772
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
777  *  is not desired.
778  *
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.
787  * \assert
788  *    \code encoder != NULL \endcode
789  */
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);
791
792 /** Get the "verify" flag.
793  *
794  * \param  encoder  An encoder instance to query.
795  * \assert
796  *    \code encoder != NULL \endcode
797  * \retval FLAC__bool
798  *    See FLAC__stream_encoder_set_verify().
799  */
800 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder);
801
802 /** Get the "streamable subset" flag.
803  *
804  * \param  encoder  An encoder instance to query.
805  * \assert
806  *    \code encoder != NULL \endcode
807  * \retval FLAC__bool
808  *    See FLAC__stream_encoder_set_streamable_subset().
809  */
810 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder);
811
812 /** Get the "mid/side stereo coding" flag.
813  *
814  * \param  encoder  An encoder instance to query.
815  * \assert
816  *    \code encoder != NULL \endcode
817  * \retval FLAC__bool
818  *    See FLAC__stream_encoder_get_do_mid_side_stereo().
819  */
820 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder);
821
822 /** Get the "adaptive mid/side switching" flag.
823  *
824  * \param  encoder  An encoder instance to query.
825  * \assert
826  *    \code encoder != NULL \endcode
827  * \retval FLAC__bool
828  *    See FLAC__stream_encoder_set_loose_mid_side_stereo().
829  */
830 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder);
831
832 /** Get the number of input channels being processed.
833  *
834  * \param  encoder  An encoder instance to query.
835  * \assert
836  *    \code encoder != NULL \endcode
837  * \retval unsigned
838  *    See FLAC__stream_encoder_set_channels().
839  */
840 FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder);
841
842 /** Get the input sample resolution setting.
843  *
844  * \param  encoder  An encoder instance to query.
845  * \assert
846  *    \code encoder != NULL \endcode
847  * \retval unsigned
848  *    See FLAC__stream_encoder_set_bits_per_sample().
849  */
850 FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder);
851
852 /** Get the input sample rate setting.
853  *
854  * \param  encoder  An encoder instance to query.
855  * \assert
856  *    \code encoder != NULL \endcode
857  * \retval unsigned
858  *    See FLAC__stream_encoder_set_sample_rate().
859  */
860 FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder);
861
862 /** Get the blocksize setting.
863  *
864  * \param  encoder  An encoder instance to query.
865  * \assert
866  *    \code encoder != NULL \endcode
867  * \retval unsigned
868  *    See FLAC__stream_encoder_set_blocksize().
869  */
870 FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder);
871
872 /** Get the maximum LPC order setting.
873  *
874  * \param  encoder  An encoder instance to query.
875  * \assert
876  *    \code encoder != NULL \endcode
877  * \retval unsigned
878  *    See FLAC__stream_encoder_set_max_lpc_order().
879  */
880 FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder);
881
882 /** Get the quantized linear predictor coefficient precision setting.
883  *
884  * \param  encoder  An encoder instance to query.
885  * \assert
886  *    \code encoder != NULL \endcode
887  * \retval unsigned
888  *    See FLAC__stream_encoder_set_qlp_coeff_precision().
889  */
890 FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder);
891
892 /** Get the qlp coefficient precision search flag.
893  *
894  * \param  encoder  An encoder instance to query.
895  * \assert
896  *    \code encoder != NULL \endcode
897  * \retval FLAC__bool
898  *    See FLAC__stream_encoder_set_do_qlp_coeff_prec_search().
899  */
900 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder);
901
902 /** Get the "escape coding" flag.
903  *
904  * \param  encoder  An encoder instance to query.
905  * \assert
906  *    \code encoder != NULL \endcode
907  * \retval FLAC__bool
908  *    See FLAC__stream_encoder_set_do_escape_coding().
909  */
910 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder);
911
912 /** Get the exhaustive model search flag.
913  *
914  * \param  encoder  An encoder instance to query.
915  * \assert
916  *    \code encoder != NULL \endcode
917  * \retval FLAC__bool
918  *    See FLAC__stream_encoder_set_do_exhaustive_model_search().
919  */
920 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder);
921
922 /** Get the minimum residual partition order setting.
923  *
924  * \param  encoder  An encoder instance to query.
925  * \assert
926  *    \code encoder != NULL \endcode
927  * \retval unsigned
928  *    See FLAC__stream_encoder_set_min_residual_partition_order().
929  */
930 FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder);
931
932 /** Get maximum residual partition order setting.
933  *
934  * \param  encoder  An encoder instance to query.
935  * \assert
936  *    \code encoder != NULL \endcode
937  * \retval unsigned
938  *    See FLAC__stream_encoder_set_max_residual_partition_order().
939  */
940 FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder);
941
942 /** Get the Rice parameter search distance setting.
943  *
944  * \param  encoder  An encoder instance to query.
945  * \assert
946  *    \code encoder != NULL \endcode
947  * \retval unsigned
948  *    See FLAC__stream_encoder_set_rice_parameter_search_dist().
949  */
950 FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder);
951
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.
956  *
957  * \param  encoder  An encoder instance to set.
958  * \assert
959  *    \code encoder != NULL \endcode
960  * \retval FLAC__uint64
961  *    See FLAC__stream_encoder_get_total_samples_estimate().
962  */
963 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder);
964
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.
971  *
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.
975  *
976  * \param  encoder  An uninitialized encoder instance.
977  * \assert
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.
982  */
983 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder);
984
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.
991  *
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().
996  *
997  * \param  encoder  An uninitialized encoder instance.
998  * \assert
999  *    \code encoder != NULL \endcode
1000  */
1001 FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder);
1002
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.
1008  *
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.
1012  * \assert
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
1018  *    went wrong.
1019  */
1020 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples);
1021
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.
1029  *
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.
1036  * \assert
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
1042  *    went wrong.
1043  */
1044 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples);
1045
1046 /* \} */
1047
1048 #ifdef __cplusplus
1049 }
1050 #endif
1051
1052 #endif