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