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