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