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