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