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