more doxygen comments
[platform/upstream/flac.git] / include / FLAC / stream_encoder.h
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002  Josh Coalson
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA  02111-1307, USA.
18  */
19
20 #ifndef FLAC__STREAM_ENCODER_H
21 #define FLAC__STREAM_ENCODER_H
22
23 #include "format.h"
24 #include "stream_decoder.h"
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30
31 /** \file include/FLAC/stream_encoder.h
32  *
33  *  \brief
34  *  This module contains the functions which implement the stream
35  *  encoder.
36  *
37  *  See the detailed documentation in the
38  *  \link flac_stream_encoder stream encoder \endlink module.
39  */
40
41 /** \defgroup flac_encoder FLAC/ *_encoder.h: encoder interfaces
42  *  \ingroup flac
43  *
44  *  \brief
45  *  This module describes the two encoder layers provided by libFLAC.
46  *
47  * For encoding FLAC streams, libFLAC provides two layers of access.  The
48  * lowest layer is stream-level encoding, and the highest is file-level
49  * encoding.  The interfaces are described in the \link flac_stream_encoder
50  * stream encoder \endlink and \link flac_file_encoder file encoder \endlink
51  * modules respectively.  Typically you will choose the highest layer that
52  * your output source will support.
53  *
54  * The stream encoder relies on callbacks for writing the data and
55  * metadata. The file encoder provides these callbacks internally and you
56  * need only supply the filename.
57  */
58
59 /** \defgroup flac_stream_encoder FLAC/stream_encoder.h: stream encoder interface
60  *  \ingroup flac_encoder
61  *
62  *  \brief
63  *  This module contains the functions which implement the stream
64  *  encoder.
65  *
66  * The basic usage of this encoder is as follows:
67  * - The program creates an instance of an encoder using
68  *   FLAC__stream_encoder_new().
69  * - The program overrides the default settings and sets callbacks for
70  *   writing and metadata reporting using
71  *   FLAC__stream_encoder_set_*() functions.
72  * - The program initializes the instance to validate the settings and
73  *   prepare for encoding using FLAC__stream_encoder_init().
74  * - The program calls FLAC__stream_encoder_process() or
75  *   FLAC__stream_encoder_process_interleaved() to encode data, which
76  *   subsequently calls the callbacks when there is encoder data ready
77  *   to be written.
78  * - The program finishes the encoding with FLAC__stream_encoder_finish(),
79  *   which causes the encoder to encode any data still in its input pipe,
80  *   call the metadata callback with the final encoding statistics, and
81  *   finally reset the encoder to the uninitialized state.
82  * - The instance may be used again or deleted with
83  *   FLAC__stream_encoder_delete().
84  *
85  * In more detail, the stream encoder functions similarly to the
86  * \link flac_stream_decoder stream decoder \endlink, but has fewer
87  * callbacks and more options.  Typically the user will create a new
88  * instance by calling FLAC__stream_encoder_new(), then set the necessary
89  * parameters and callbacks with FLAC__stream_encoder_set_*(), and
90  * initialize it by calling FLAC__stream_encoder_init().
91  *
92  * Unlike the decoders, the stream encoder has many options that can
93  * affect the speed and compression ratio.  When setting these parameters
94  * you should have some basic knowledge of the format (see the
95  * <A HREF="../documentation.html#format">user-level documentation</A>
96  * or the <A HREF="../format.html">formal description</A>).  The
97  * FLAC__stream_encoder_set_*() functions themselves do not validate the
98  * values as many are interdependent.  The FLAC__stream_encoder_init()
99  * function will do this, so make sure to pay attention to the state
100  * returned by FLAC__stream_encoder_init() to make sure that it is
101  * FLAC__STREAM_ENCODER_OK.  Any parameters that are not set before
102  * FLAC__stream_encoder_init() will take on the defaults from the
103  * constructor.
104  *
105  * The user must provide function pointers for the following callbacks:
106  *
107  * - Write callback - This function is called by the encoder anytime there
108  *   is raw encoded data to write.  It may include metadata mixed with
109  *   encoded audio frames and the data is not guaranteed to be aligned on
110  *   frame or metadata block boundaries.
111  * - Metadata callback - This function is called once at the end of
112  *   encoding with the populated STREAMINFO structure.  This is so file
113  *   encoders can seek back to the beginning of the file and write the
114  *   STREAMINFO block with the correct statistics after encoding (like
115  *   minimum/maximum frame size).
116  *
117  * The call to FLAC__stream_encoder_init() currently will also immediately
118  * call the write callback several times, once with the \c fLaC signature,
119  * and once for each encoded metadata block.
120  *
121  * After initializing the instance, the user may feed audio data to the
122  * encoder in one of two ways:
123  *
124  * - Channel separate, through FLAC__stream_encoder_process() - The user
125  *   will pass an array of pointers to buffers, one for each channel, to
126  *   the encoder, each of the same length.  The samples need not be
127  *   block-aligned.
128  * - Channel interleaved, through
129  *   FLAC__stream_encoder_process_interleaved() - The user will pass a single
130  *   pointer to data that is channel-interleaved (i.e. channel0_sample0,
131  *   channel1_sample0, ... , channelN_sample0, channel0_sample1, ...).
132  *   Again, the samples need not be block-aligned but they must be
133  *   sample-aligned, i.e. the first value should be channel0_sample0 and
134  *   the last value channelN_sampleM.
135  *
136  * When the user is finished encoding data, it calls
137  * FLAC__stream_encoder_finish(), which causes the encoder to encode any
138  * data still in its input pipe, and call the metadata callback with the
139  * final encoding statistics.  Then the instance may be deleted with
140  * FLAC__stream_encoder_delete() or initialized again to encode another
141  * stream.
142  *
143  * For programs that write their own metadata, but that do not know the
144  * actual metadata until after encoding, it is advantageous to instruct
145  * the encoder to write a PADDING block of the correct size, so that
146  * instead of rewriting the whole stream after encoding, the program can
147  * just overwrite the PADDING block.  If only the maximum size of the
148  * metadata is known, the program can write a slightly larger padding
149  * block, then split it after encoding.
150  *
151  * Make sure you understand how lengths are calculated.  All FLAC metadata
152  * blocks have a 4 byte header which contains the type and length.  This
153  * length does not include the 4 bytes of the header.  See the format page
154  * for the specification of metadata blocks and their lengths.
155  *
156  * \note
157  * The "set" functions may only be called when the encoder is in the
158  * state FLAC__STREAM_ENCODER_UNINITIALIZED, i.e. after
159  * FLAC__stream_encoder_new() or FLAC__stream_encoder_finish(), but
160  * before FLAC__stream_encoder_init().  If this is the case they will
161  * return \c true, otherwise \c false.
162  *
163  * \note
164  * FLAC__stream_encoder_finish() resets all settings to the constructor
165  * defaults, including the callbacks.
166  *
167  * \{
168  */
169
170
171 /** State values for a FLAC__StreamEncoder
172  *
173  *  The encoder's state can be obtained by calling FLAC__stream_encoder_get_state().
174  */
175 typedef enum {
176
177         FLAC__STREAM_ENCODER_OK = 0,
178         /**< The encoder is in the normal OK state. */
179
180         FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR,
181         /**< An error occurred in the underlying verify stream decoder;
182          * check FLAC__stream_encoder_get_verify_decoder_state().
183          */
184
185         FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA,
186         /**< The verify decoder detected a mismatch between the original
187          * audio signal and the decoded audio signal.
188          */
189
190         FLAC__STREAM_ENCODER_INVALID_CALLBACK,
191         /**< The encoder was initialized before setting all the required callbacks. */
192
193         FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS,
194         /**< The encoder has an invalid setting for number of channels. */
195
196         FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE,
197         /**< The encoder has an invalid setting for bits-per-sample.
198          * FLAC supports 4-32 bps but the reference encoder currently supports
199          * only up to 24 bps.
200          */
201
202         FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE,
203         /**< The encoder has an invalid setting for the input sample rate. */
204
205         FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE,
206         /**< The encoder has an invalid setting for the block size. */
207
208         FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION,
209         /**< The encoder has an invalid setting for the precision of the quantized linear predictor coefficients. */
210
211         FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH,
212         /**< Mid/side coding was specified but the number of channels is not equal to 2. */
213
214         FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH,
215         /**< Deprecated. */
216
217         FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE,
218         /**< Loose mid/side coding was specified but mid/side coding was not. */
219
220         FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER,
221         /**< The specified block size is less than the maximum LPC order. */
222
223         FLAC__STREAM_ENCODER_NOT_STREAMABLE,
224         /**< The encoder is bound to the "streamable subset" but other settings violate it. */
225
226         FLAC__STREAM_ENCODER_FRAMING_ERROR,
227         /**< An error occurred while writing the stream; usually, the write_callback returned an error. */
228
229         FLAC__STREAM_ENCODER_INVALID_METADATA,
230         /**< The metadata input to the encoder is invalid. */
231
232         FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING,
233         /**< An error occurred while writing the stream; usually, the write_callback returned an error. */
234
235         FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING,
236         /**< The write_callback returned an error. */
237
238         FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR,
239         /**< Memory allocation failed. */
240
241         FLAC__STREAM_ENCODER_ALREADY_INITIALIZED,
242         /**< FLAC__stream_encoder_init() was called when the encoder was
243          * already initialized, usually because
244          * FLAC__stream_encoder_finish() was not called.
245          */
246
247         FLAC__STREAM_ENCODER_UNINITIALIZED
248         /**< The encoder is in the uninitialized state. */
249
250 } FLAC__StreamEncoderState;
251
252 /** Maps a FLAC__StreamEncoderState to a C string.
253  *
254  *  Using a FLAC__StreamEncoderState as the index to this array
255  *  will give the string equivalent.  The contents should not be modified.
256  */
257 extern const char * const FLAC__StreamEncoderStateString[];
258
259 /** Return values for the FLAC__StreamEncoder write callback.
260  */
261 typedef enum {
262
263         FLAC__STREAM_ENCODER_WRITE_STATUS_OK = 0,
264         /**< The write was OK and encoding can continue. */
265
266         FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR
267         /**< An unrecoverable error occurred.  The encoder will return from the process call. */
268
269 } FLAC__StreamEncoderWriteStatus;
270
271 /** Maps a FLAC__StreamEncoderWriteStatus to a C string.
272  *
273  *  Using a FLAC__StreamEncoderWriteStatus as the index to this array
274  *  will give the string equivalent.  The contents should not be modified.
275  */
276 extern const char * const FLAC__StreamEncoderWriteStatusString[];
277
278
279 /***********************************************************************
280  *
281  * class FLAC__StreamEncoder
282  *
283  ***********************************************************************/
284
285 struct FLAC__StreamEncoderProtected;
286 struct FLAC__StreamEncoderPrivate;
287 /** The opaque structure definition for the stream encoder type.
288  *  See the \link flac_stream_encoder stream encoder module \endlink
289  *  for a detailed description.
290  */
291 typedef struct {
292         struct FLAC__StreamEncoderProtected *protected_; /* avoid the C++ keyword 'protected' */
293         struct FLAC__StreamEncoderPrivate *private_; /* avoid the C++ keyword 'private' */
294 } FLAC__StreamEncoder;
295
296 /** Signature for the write callback.
297  *  See FLAC__stream_encoder_set_write_callback() for more info.
298  *
299  * \param  encoder  The encoder instance calling the callback.
300  * \param  buffer   An array of encoded data of length \a bytes.
301  * \param  bytes    The byte length of \a buffer.
302  * \param  samples  The number of samples encoded by \a buffer.
303  *                  \c 0 has a special meaning; see
304  *                  FLAC__stream_encoder_set_write_callback().
305  * \param  current_frame  The number of the current frame being encoded.
306  * \param  client_data  The callee's client data set through
307  *                      FLAC__stream_encoder_set_client_data().
308  * \retval FLAC__StreamDecoderWriteStatus
309  *    The callee's return status.
310  */
311 typedef FLAC__StreamEncoderWriteStatus (*FLAC__StreamEncoderWriteCallback)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data);
312
313 /** Signature for the metadata callback.
314  *  See FLAC__stream_encoder_set_metadata_callback() for more info.
315  *
316  * \param  encoder      The encoder instance calling the callback.
317  * \param  metadata     The final populated STREAMINFO block.
318  * \param  client_data  The callee's client data set through
319  *                      FLAC__stream_encoder_set_client_data().
320  */
321 typedef void (*FLAC__StreamEncoderMetadataCallback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data);
322
323
324 /***********************************************************************
325  *
326  * Class constructor/destructor
327  *
328  ***********************************************************************/
329
330 /** Create a new stream encoder instance.  The instance is created with
331  *  default settings; see the individual FLAC__stream_encoder_set_*()
332  *  functions for each setting's default.
333  *
334  * \retval FLAC__StreamEncoder*
335  *    \c NULL if there was an error allocating memory, else the new instance.
336  */
337 FLAC__StreamEncoder *FLAC__stream_encoder_new();
338
339 /** Free an encoder instance.  Deletes the object pointed to by \a encoder.
340  *
341  * \param encoder  A pointer to an existing encoder.
342  * \assert
343  *    \code encoder != NULL \endcode
344  */
345 void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder);
346
347 /***********************************************************************
348  *
349  * Public class method prototypes
350  *
351  ***********************************************************************/
352
353 /** Set the "verify" flag.  If \c true, the encoder will verify it's own
354  *  encoded output by feeding it through an internal decoder and comparing
355  *  the original signal against the decoded signal.  If a mismatch occurs,
356  *  the process call will return \c false.  Note that this will slow the
357  *  encoding process by the extra time required for decoding and comparison.
358  *
359  * \default \c false
360  * \param  encoder  An encoder instance to set.
361  * \param  value    Flag value (see above).
362  * \assert
363  *    \code encoder != NULL \endcode
364  * \retval FLAC__bool
365  *    \c false if the encoder is already initialized, else \c true.
366  */
367 FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value);
368
369 /** Set the "streamable subset" flag.  If \c true, the encoder will comply
370  *  with the subset (see the format specification) and will check the
371  *  settings during FLAC__stream_encoder_init() to see if all settings
372  *  comply.  If \c false, the settings may take advantage of the full
373  *  range that the format allows.
374  *
375  *  Make sure you know what it entails before setting this to \c false.
376  *
377  * \default \c true
378  * \param  encoder  An encoder instance to set.
379  * \param  value    Flag value (see above).
380  * \assert
381  *    \code encoder != NULL \endcode
382  * \retval FLAC__bool
383  *    \c false if the encoder is already initialized, else \c true.
384  */
385 FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value);
386
387 /** Set to \c true to enable mid-side encoding on stereo input.  The
388  *  number of channels must be 2.  Set to \c false to use only
389  *  independent channel coding.
390  *
391  * \default \c false
392  * \param  encoder  An encoder instance to set.
393  * \param  value    Flag value (see above).
394  * \assert
395  *    \code encoder != NULL \endcode
396  * \retval FLAC__bool
397  *    \c false if the encoder is already initialized, else \c true.
398  */
399 FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value);
400
401 /** Set to \c true to enable adaptive switching between mid-side and
402  *  left-right encoding on stereo input.  The number of channels must
403  *  be 2.  Set to \c false to use exhaustive searching.  In either
404  *  case, the mid/side stereo setting must be \c true.
405  *
406  * \default \c false
407  * \param  encoder  An encoder instance to set.
408  * \param  value    Flag value (see above).
409  * \assert
410  *    \code encoder != NULL \endcode
411  * \retval FLAC__bool
412  *    \c false if the encoder is already initialized, else \c true.
413  */
414 FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value);
415
416 /** Set the number of channels to be encoded.
417  *
418  * \default \c 2
419  * \param  encoder  An encoder instance to set.
420  * \param  value    See above.
421  * \assert
422  *    \code encoder != NULL \endcode
423  * \retval FLAC__bool
424  *    \c false if the encoder is already initialized, else \c true.
425  */
426 FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value);
427
428 /** Set the sample resolution of the input to be encoded.
429  *
430  * \warning
431  * Do not feed the encoder data that is wider than the value you
432  * set here or you will generate an invalid stream.
433  *
434  * \default \c 16
435  * \param  encoder  An encoder instance to set.
436  * \param  value    See above.
437  * \assert
438  *    \code encoder != NULL \endcode
439  * \retval FLAC__bool
440  *    \c false if the encoder is already initialized, else \c true.
441  */
442 FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value);
443
444 /** Set the sample rate (in Hz) of the input to be encoded.
445  *
446  * \default \c 44100
447  * \param  encoder  An encoder instance to set.
448  * \param  value    See above.
449  * \assert
450  *    \code encoder != NULL \endcode
451  * \retval FLAC__bool
452  *    \c false if the encoder is already initialized, else \c true.
453  */
454 FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value);
455
456 /** Set the blocksize to use while encoding.
457  *
458  * \default \c 1152
459  * \param  encoder  An encoder instance to set.
460  * \param  value    See above.
461  * \assert
462  *    \code encoder != NULL \endcode
463  * \retval FLAC__bool
464  *    \c false if the encoder is already initialized, else \c true.
465  */
466 FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value);
467
468 /** Set the maximum LPC order, or \c 0 to use only the fixed predictors.
469  *
470  * \default \c 0
471  * \param  encoder  An encoder instance to set.
472  * \param  value    See above.
473  * \assert
474  *    \code encoder != NULL \endcode
475  * \retval FLAC__bool
476  *    \c false if the encoder is already initialized, else \c true.
477  */
478 FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value);
479
480 /** Set the precision, in bits, of the quantized linear predictor
481  *  coefficients, or \c 0 to let the encoder select it based on the
482  *  blocksize.
483  *
484  * \note
485  * In the current implementation, qlp_coeff_precision + bits_per_sample must
486  * be less than 32.
487  *
488  * \default \c 0
489  * \param  encoder  An encoder instance to set.
490  * \param  value    See above.
491  * \assert
492  *    \code encoder != NULL \endcode
493  * \retval FLAC__bool
494  *    \c false if the encoder is already initialized, else \c true.
495  */
496 FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value);
497
498 /** Set to \c false to use only the specified quantized linear predictor
499  *  coefficient precision, or \c true to search neighboring precision
500  *  values and use the best one.
501  *
502  * \default \c false
503  * \param  encoder  An encoder instance to set.
504  * \param  value    See above.
505  * \assert
506  *    \code encoder != NULL \endcode
507  * \retval FLAC__bool
508  *    \c false if the encoder is already initialized, else \c true.
509  */
510 FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value);
511
512 /** Deprecated.  Setting this value has no effect.
513  *
514  * \default \c false
515  * \param  encoder  An encoder instance to set.
516  * \param  value    See above.
517  * \assert
518  *    \code encoder != NULL \endcode
519  * \retval FLAC__bool
520  *    \c false if the encoder is already initialized, else \c true.
521  */
522 FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value);
523
524 /** Set to \c false to let the encoder estimate the best model order
525  *  based on the residual signal energy, or \c true to force the
526  *  encoder to evaluate all order models and select the best.
527  *
528  * \default \c false
529  * \param  encoder  An encoder instance to set.
530  * \param  value    See above.
531  * \assert
532  *    \code encoder != NULL \endcode
533  * \retval FLAC__bool
534  *    \c false if the encoder is already initialized, else \c true.
535  */
536 FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value);
537
538 /** Set the minimum partition order to search when coding the residual.
539  *  This is used in tandem with
540  *  FLAC__stream_encoder_set_max_residual_partition_order().
541  *
542  *  The partition order determines the context size in the residual.
543  *  The context size will be approximately <tt>blocksize / (2 ^ order)</tt>.
544  *
545  *  Set both min and max values to \c 0 to force a single context,
546  *  whose Rice parameter is based on the residual signal variance.
547  *  Otherwise, set a min and max order, and the encoder will search
548  *  all orders, using the mean of each context for its Rice parameter,
549  *  and use the best.
550  *
551  * \default \c 0
552  * \param  encoder  An encoder instance to set.
553  * \param  value    See above.
554  * \assert
555  *    \code encoder != NULL \endcode
556  * \retval FLAC__bool
557  *    \c false if the encoder is already initialized, else \c true.
558  */
559 FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value);
560
561 /** Set the minimum partition order to search when coding the residual.
562  *  This is used in tandem with
563  *  FLAC__stream_encoder_set_min_residual_partition_order().
564  *
565  *  The partition order determines the context size in the residual.
566  *  The context size will be approximately <tt>blocksize / (2 ^ order)</tt>.
567  *
568  *  Set both min and max values to \c 0 to force a single context,
569  *  whose Rice parameter is based on the residual signal variance.
570  *  Otherwise, set a min and max order, and the encoder will search
571  *  all orders, using the mean of each context for its Rice parameter,
572  *  and use the best.
573  *
574  * \default \c 0
575  * \param  encoder  An encoder instance to set.
576  * \param  value    See above.
577  * \assert
578  *    \code encoder != NULL \endcode
579  * \retval FLAC__bool
580  *    \c false if the encoder is already initialized, else \c true.
581  */
582 FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value);
583
584 /** Deprecated.  Setting this value has no effect.
585  *
586  * \default \c 0
587  * \param  encoder  An encoder instance to set.
588  * \param  value    See above.
589  * \assert
590  *    \code encoder != NULL \endcode
591  * \retval FLAC__bool
592  *    \c false if the encoder is already initialized, else \c true.
593  */
594 FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value);
595
596 /** Set an estimate of the total samples that will be encoded.
597  *  This is merely an estimate and may be set to \c 0 if unknown.
598  *  This value will be written to the STREAMINFO block before encoding,
599  *  and can remove the need for the caller to rewrite the value later
600  *  if the value is known before encoding.
601  *
602  * \default \c 0
603  * \param  encoder  An encoder instance to set.
604  * \param  value    See above.
605  * \assert
606  *    \code encoder != NULL \endcode
607  * \retval FLAC__bool
608  *    \c false if the encoder is already initialized, else \c true.
609  */
610 FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value);
611
612 /** Set the metadata blocks to be emitted to the stream before encoding.
613  *  A value of \c NULL, \c 0 implies no metadata; otherwise, supply an
614  *  array of pointers to metadata blocks.  The array is non-const since
615  *  the encoder may need to change the \a is_last flag inside them.
616  *  Otherwise, the encoder will not modify or free the blocks.  It is up
617  *  to the caller to free the metadata blocks after encoding.
618  *
619  *  The STREAMINFO block is always written and no STREAMINFO block may
620  *  occur in the supplied array.
621  *
622  * \default \c NULL, 0
623  * \param  encoder     An encoder instance to set.
624  * \param  metadata    See above.
625  * \param  num_blocks  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__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks);
632
633 /** Set the write callback.
634  *  The supplied function will be called by the encoder anytime there is raw
635  *  encoded data ready to write.  It may include metadata mixed with encoded
636  *  audio frames and the data is not guaranteed to be aligned on frame or
637  *  metadata block boundaries.
638  *
639  *  The only duty of the callback is to write out the \a bytes worth of data
640  *  in \a buffer to the current position in the output stream.  The arguments
641  *  \a samples and \a current_frame are purely informational.  If \a samples
642  *  is greater than \c 0, then \a current_frame will hold the current frame
643  *  number that is being written; otherwise, the write callback is being called
644  *  to write metadata.
645  *
646  * \note
647  * The callback is mandatory and must be set before initialization.
648  *
649  * \default \c NULL
650  * \param  encoder  An encoder instance to set.
651  * \param  value    See above.
652  * \assert
653  *    \code encoder != NULL \endcode
654  *    \code value != NULL \endcode
655  * \retval FLAC__bool
656  *    \c false if the encoder is already initialized, else \c true.
657  */
658 FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value);
659
660 /** Set the metadata callback.
661  *  The supplied function will be called once at the end of encoding with
662  *  the populated STREAMINFO structure.  This is so file encoders can seek
663  *  back to the beginning of the file and write the STREAMINFO block with
664  *  the correct statistics after encoding (like minimum/maximum frame size
665  *  and total samples).
666  *
667  * \note
668  * The callback is mandatory and must be set before initialization.
669  *
670  * \default \c NULL
671  * \param  encoder  An encoder instance to set.
672  * \param  value    See above.
673  * \assert
674  *    \code encoder != NULL \endcode
675  *    \code value != NULL \endcode
676  * \retval FLAC__bool
677  *    \c false if the encoder is already initialized, else \c true.
678  */
679 FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value);
680
681 /** Set the client data to be passed back to callbacks.
682  *  This value will be supplied to callbacks in their \a client_data
683  *  argument.
684  *
685  * \default \c NULL
686  * \param  encoder  An encoder instance to set.
687  * \param  value    See above.
688  * \assert
689  *    \code encoder != NULL \endcode
690  * \retval FLAC__bool
691  *    \c false if the encoder is already initialized, else \c true.
692  */
693 FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value);
694
695 /** Get the current encoder state.
696  *
697  * \param  encoder  An encoder instance to query.
698  * \assert
699  *    \code encoder != NULL \endcode
700  * \retval FLAC__StreamEncoderState
701  *    The current encoder state.
702  */
703 FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder);
704
705 /** Get the state of the verify stream decoder.
706  *  Useful when the stream encoder state is
707  *  \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR.
708  *
709  * \param  encoder  An encoder instance to query.
710  * \assert
711  *    \code encoder != NULL \endcode
712  * \retval FLAC__StreamDecoderState
713  *    The verify stream decoder state.
714  */
715 FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder);
716
717 /** Get relevant values about the nature of a verify decoder error.
718  *  Useful when the stream encoder state is
719  *  \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR.  The arguments should
720  *  be addresses in which the stats will be returned, or NULL if value
721  *  is not desired.
722  *
723  * \param  encoder  An encoder instance to query.
724  * \param  absolute_sample  The absolute sample number of the mismatch.
725  * \param  frame_number  The number of the frame in which the mismatch occurred.
726  * \param  channel       The channel in which the mismatch occurred.
727  * \param  sample        The number of the sample (relative to the frame) in
728  *                       which the mismatch occurred.
729  * \param  expected      The expected value for the sample in question.
730  * \param  got           The actual value returned by the decoder.
731  * \assert
732  *    \code encoder != NULL \endcode
733  */
734 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);
735
736 /** Get the "verify" flag.
737  *
738  * \param  encoder  An encoder instance to query.
739  * \assert
740  *    \code encoder != NULL \endcode
741  * \retval FLAC__bool
742  *    See FLAC__stream_encoder_set_verify().
743  */
744 FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder);
745
746 /** Get the "streamable subset" flag.
747  *
748  * \param  encoder  An encoder instance to query.
749  * \assert
750  *    \code encoder != NULL \endcode
751  * \retval FLAC__bool
752  *    See FLAC__stream_encoder_set_streamable_subset().
753  */
754 FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder);
755
756 /** Get the "mid/side stereo coding" flag.
757  *
758  * \param  encoder  An encoder instance to query.
759  * \assert
760  *    \code encoder != NULL \endcode
761  * \retval FLAC__bool
762  *    See FLAC__stream_encoder_get_do_mid_side_stereo().
763  */
764 FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder);
765
766 /** Get the "adaptive mid/side switching" flag.
767  *
768  * \param  encoder  An encoder instance to query.
769  * \assert
770  *    \code encoder != NULL \endcode
771  * \retval FLAC__bool
772  *    See FLAC__stream_encoder_set_loose_mid_side_stereo().
773  */
774 FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder);
775
776 /** Get the number of input channels being processed.
777  *
778  * \param  encoder  An encoder instance to query.
779  * \assert
780  *    \code encoder != NULL \endcode
781  * \retval unsigned
782  *    See FLAC__stream_encoder_set_channels().
783  */
784 unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder);
785
786 /** Get the input sample resolution setting.
787  *
788  * \param  encoder  An encoder instance to query.
789  * \assert
790  *    \code encoder != NULL \endcode
791  * \retval unsigned
792  *    See FLAC__stream_encoder_set_bits_per_sample().
793  */
794 unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder);
795
796 /** Get the input sample rate setting.
797  *
798  * \param  encoder  An encoder instance to query.
799  * \assert
800  *    \code encoder != NULL \endcode
801  * \retval unsigned
802  *    See FLAC__stream_encoder_set_sample_rate().
803  */
804 unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder);
805
806 /** Get the blocksize setting.
807  *
808  * \param  encoder  An encoder instance to query.
809  * \assert
810  *    \code encoder != NULL \endcode
811  * \retval unsigned
812  *    See FLAC__stream_encoder_set_blocksize().
813  */
814 unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder);
815
816 /** Get the maximum LPC order setting.
817  *
818  * \param  encoder  An encoder instance to query.
819  * \assert
820  *    \code encoder != NULL \endcode
821  * \retval unsigned
822  *    See FLAC__stream_encoder_set_max_lpc_order().
823  */
824 unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder);
825
826 /** Get the quantized linear predictor coefficient precision setting.
827  *
828  * \param  encoder  An encoder instance to query.
829  * \assert
830  *    \code encoder != NULL \endcode
831  * \retval unsigned
832  *    See FLAC__stream_encoder_set_qlp_coeff_precision().
833  */
834 unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder);
835
836 /** Get the qlp coefficient precision search flag.
837  *
838  * \param  encoder  An encoder instance to query.
839  * \assert
840  *    \code encoder != NULL \endcode
841  * \retval FLAC__bool
842  *    See FLAC__stream_encoder_set_do_qlp_coeff_prec_search().
843  */
844 FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder);
845
846 /** Get the "escape coding" flag.
847  *
848  * \param  encoder  An encoder instance to query.
849  * \assert
850  *    \code encoder != NULL \endcode
851  * \retval FLAC__bool
852  *    See FLAC__stream_encoder_set_do_escape_coding().
853  */
854 FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder);
855
856 /** Get the exhaustive model search flag.
857  *
858  * \param  encoder  An encoder instance to query.
859  * \assert
860  *    \code encoder != NULL \endcode
861  * \retval FLAC__bool
862  *    See FLAC__stream_encoder_set_do_exhaustive_model_search().
863  */
864 FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder);
865
866 /** Get the minimum residual partition order setting.
867  *
868  * \param  encoder  An encoder instance to query.
869  * \assert
870  *    \code encoder != NULL \endcode
871  * \retval unsigned
872  *    See FLAC__stream_encoder_set_min_residual_partition_order().
873  */
874 unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder);
875
876 /** Get maximum residual partition order setting.
877  *
878  * \param  encoder  An encoder instance to query.
879  * \assert
880  *    \code encoder != NULL \endcode
881  * \retval unsigned
882  *    See FLAC__stream_encoder_set_max_residual_partition_order().
883  */
884 unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder);
885
886 /** Get the Rice parameter search distance setting.
887  *
888  * \param  encoder  An encoder instance to query.
889  * \assert
890  *    \code encoder != NULL \endcode
891  * \retval unsigned
892  *    See FLAC__stream_encoder_set_rice_parameter_search_dist().
893  */
894 unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder);
895
896 /** Get the previously set estimate of the total samples to be encoded.
897  *  The encoder merely mimics back the value given to
898  *  FLAC__stream_encoder_set_total_samples_estimate() since it has no
899  *  other way of knowing how many samples the user will encode.
900  *
901  * \param  encoder  An encoder instance to set.
902  * \assert
903  *    \code encoder != NULL \endcode
904  * \retval FLAC__uint64
905  *    See FLAC__stream_encoder_get_total_samples_estimate().
906  */
907 FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder);
908
909 /** Initialize the encoder instance.
910  *  Should be called after FLAC__stream_encoder_new() and
911  *  FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process()
912  *  or FLAC__stream_encoder_process_interleaved().  Will set and return
913  *  the encoder state, which will be FLAC__STREAM_ENCODER_OK if
914  *  initialization succeeded.
915  *
916  *  The call to FLAC__stream_encoder_init() currently will also immediately
917  *  call the write callback several times, once with the \c fLaC signature,
918  *  and once for each encoded metadata block.
919  *
920  * \param  encoder  An uninitialized encoder instance.
921  * \assert
922  *    \code encoder != NULL \endcode
923  * \retval FLAC__StreamEncoderState
924  *    \c FLAC__STREAM_ENCODER_OK if initialization was successful; see
925  *    FLAC__StreamEncoderState for the meanings of other return values.
926  */
927 FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder);
928
929 /** Finish the encoding process.
930  *  Flushes the encoding buffer, releases resources, resets the encoder
931  *  settings to their defaults, and returns the encoder state to
932  *  FLAC__STREAM_ENCODER_UNINITIALIZED.  Note that this can generate
933  *  one or more write callbacks before returning, and will generate
934  *  a metadata callback.
935  *
936  *  In the event of a prematurely-terminated encode, it is not strictly
937  *  necessary to call this immediately before FLAC__stream_encoder_delete()
938  *  but it is good practice to match every FLAC__stream_encoder_init()
939  *  with a FLAC__stream_encoder_finish().
940  *
941  * \param  encoder  An uninitialized encoder instance.
942  * \assert
943  *    \code encoder != NULL \endcode
944  */
945 void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder);
946
947 /** Submit data for encoding.
948  *  This version allows you to supply the input data via an array of
949  *  pointers, each pointer pointing to an array of \a samples samples
950  *  representing one channel.  The samples need not be block-aligned,
951  *  but each channel should have the same number of samples.
952  *
953  * \param  encoder  An initialized encoder instance in the OK state.
954  * \param  buffer   An array of pointers to each channel's signal.
955  * \param  samples  The number of samples in one channel.
956  * \assert
957  *    \code encoder != NULL \endcode
958  *    \code FLAC__stream_encoder_get_state(encoder) == FLAC__STREAM_ENCODER_OK \endcode
959  * \retval FLAC__bool
960  *    \c true if successful, else \c false; in this case, check the
961  *    encoder state with FLAC__stream_encoder_get_state() to see what
962  *    went wrong.
963  */
964 FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples);
965
966 /** Submit data for encoding.
967  *  This version allows you to supply the input data where the channels
968  *  are interleaved into a single array (i.e. channel0_sample0,
969  *  channel1_sample0, ... , channelN_sample0, channel0_sample1, ...).
970  *  The samples need not be block-aligned but they must be
971  *  sample-aligned, i.e. the first value should be channel0_sample0
972  *  and the last value channelN_sampleM.
973  *
974  * \param  encoder  An initialized encoder instance in the OK state.
975  * \param  buffer   An array of channel-interleaved data (see above).
976  * \param  samples  The number of samples in one channel, the same as for
977  *                  FLAC__stream_encoder_process().  For example, if
978  *                  encoding two channels, \c 1000 \a samples corresponds
979  *                  to a \a buffer of 2000 values.
980  * \assert
981  *    \code encoder != NULL \endcode
982  *    \code FLAC__stream_encoder_get_state(encoder) == FLAC__STREAM_ENCODER_OK \endcode
983  * \retval FLAC__bool
984  *    \c true if successful, else \c false; in this case, check the
985  *    encoder state with FLAC__stream_encoder_get_state() to see what
986  *    went wrong.
987  */
988 FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples);
989
990 /* \} */
991
992 #ifdef __cplusplus
993 }
994 #endif
995
996 #endif