1 /* libFLAC - Free Lossless Audio Codec library 2 * Copyright (C) 2000-2009 Josh Coalson 3 * Copyright (C) 2011-2022 Xiph.Org Foundation 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * - Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * - Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * - Neither the name of the Xiph.org Foundation nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #ifndef FLAC__STREAM_ENCODER_H 34 #define FLAC__STREAM_ENCODER_H 35 36 #include <stdio.h> /* for FILE */ 37 #include "export.h" 38 #include "format.h" 39 #include "stream_decoder.h" 40 41 #ifdef __cplusplus 42 extern "C" { 43 #endif 44 45 46 /** \file include/FLAC/stream_encoder.h 47 * 48 * \brief 49 * This module contains the functions which implement the stream 50 * encoder. 51 * 52 * See the detailed documentation in the 53 * \link flac_stream_encoder stream encoder \endlink module. 54 */ 55 56 /** \defgroup flac_encoder FLAC/ \*_encoder.h: encoder interfaces 57 * \ingroup flac 58 * 59 * \brief 60 * This module describes the encoder layers provided by libFLAC. 61 * 62 * The stream encoder can be used to encode complete streams either to the 63 * client via callbacks, or directly to a file, depending on how it is 64 * initialized. When encoding via callbacks, the client provides a write 65 * callback which will be called whenever FLAC data is ready to be written. 66 * If the client also supplies a seek callback, the encoder will also 67 * automatically handle the writing back of metadata discovered while 68 * encoding, like stream info, seek points offsets, etc. When encoding to 69 * a file, the client needs only supply a filename or open \c FILE* and an 70 * optional progress callback for periodic notification of progress; the 71 * write and seek callbacks are supplied internally. For more info see the 72 * \link flac_stream_encoder stream encoder \endlink module. 73 */ 74 75 /** \defgroup flac_stream_encoder FLAC/stream_encoder.h: stream encoder interface 76 * \ingroup flac_encoder 77 * 78 * \brief 79 * This module contains the functions which implement the stream 80 * encoder. 81 * 82 * The stream encoder can encode to native FLAC, and optionally Ogg FLAC 83 * (check FLAC_API_SUPPORTS_OGG_FLAC) streams and files. 84 * 85 * The basic usage of this encoder is as follows: 86 * - The program creates an instance of an encoder using 87 * FLAC__stream_encoder_new(). 88 * - The program overrides the default settings using 89 * FLAC__stream_encoder_set_*() functions. At a minimum, the following 90 * functions should be called: 91 * - FLAC__stream_encoder_set_channels() 92 * - FLAC__stream_encoder_set_bits_per_sample() 93 * - FLAC__stream_encoder_set_sample_rate() 94 * - FLAC__stream_encoder_set_ogg_serial_number() (if encoding to Ogg FLAC) 95 * - FLAC__stream_encoder_set_total_samples_estimate() (if known) 96 * - If the application wants to control the compression level or set its own 97 * metadata, then the following should also be called: 98 * - FLAC__stream_encoder_set_compression_level() 99 * - FLAC__stream_encoder_set_verify() 100 * - FLAC__stream_encoder_set_metadata() 101 * - The rest of the set functions should only be called if the client needs 102 * exact control over how the audio is compressed; thorough understanding 103 * of the FLAC format is necessary to achieve good results. 104 * - The program initializes the instance to validate the settings and 105 * prepare for encoding using 106 * - FLAC__stream_encoder_init_stream() or FLAC__stream_encoder_init_FILE() 107 * or FLAC__stream_encoder_init_file() for native FLAC 108 * - FLAC__stream_encoder_init_ogg_stream() or FLAC__stream_encoder_init_ogg_FILE() 109 * or FLAC__stream_encoder_init_ogg_file() for Ogg FLAC 110 * - The program calls FLAC__stream_encoder_process() or 111 * FLAC__stream_encoder_process_interleaved() to encode data, which 112 * subsequently calls the callbacks when there is encoder data ready 113 * to be written. 114 * - The program finishes the encoding with FLAC__stream_encoder_finish(), 115 * which causes the encoder to encode any data still in its input pipe, 116 * update the metadata with the final encoding statistics if output 117 * seeking is possible, and finally reset the encoder to the 118 * uninitialized state. 119 * - The instance may be used again or deleted with 120 * FLAC__stream_encoder_delete(). 121 * 122 * In more detail, the stream encoder functions similarly to the 123 * \link flac_stream_decoder stream decoder \endlink, but has fewer 124 * callbacks and more options. Typically the client will create a new 125 * instance by calling FLAC__stream_encoder_new(), then set the necessary 126 * parameters with FLAC__stream_encoder_set_*(), and initialize it by 127 * calling one of the FLAC__stream_encoder_init_*() functions. 128 * 129 * Unlike the decoders, the stream encoder has many options that can 130 * affect the speed and compression ratio. When setting these parameters 131 * you should have some basic knowledge of the format (see the 132 * <A HREF="https://xiph.org/flac/documentation_format_overview.html">user-level documentation</A> 133 * or the <A HREF="https://xiph.org/flac/format.html">formal description</A>). The 134 * FLAC__stream_encoder_set_*() functions themselves do not validate the 135 * values as many are interdependent. The FLAC__stream_encoder_init_*() 136 * functions will do this, so make sure to pay attention to the state 137 * returned by FLAC__stream_encoder_init_*() to make sure that it is 138 * FLAC__STREAM_ENCODER_INIT_STATUS_OK. Any parameters that are not set 139 * before FLAC__stream_encoder_init_*() will take on the defaults from 140 * the constructor. 141 * 142 * There are three initialization functions for native FLAC, one for 143 * setting up the encoder to encode FLAC data to the client via 144 * callbacks, and two for encoding directly to a file. 145 * 146 * For encoding via callbacks, use FLAC__stream_encoder_init_stream(). 147 * You must also supply a write callback which will be called anytime 148 * there is raw encoded data to write. If the client can seek the output 149 * it is best to also supply seek and tell callbacks, as this allows the 150 * encoder to go back after encoding is finished to write back 151 * information that was collected while encoding, like seek point offsets, 152 * frame sizes, etc. 153 * 154 * For encoding directly to a file, use FLAC__stream_encoder_init_FILE() 155 * or FLAC__stream_encoder_init_file(). Then you must only supply a 156 * filename or open \c FILE*; the encoder will handle all the callbacks 157 * internally. You may also supply a progress callback for periodic 158 * notification of the encoding progress. 159 * 160 * There are three similarly-named init functions for encoding to Ogg 161 * FLAC streams. Check \c FLAC_API_SUPPORTS_OGG_FLAC to find out if the 162 * library has been built with Ogg support. 163 * 164 * The call to FLAC__stream_encoder_init_*() currently will also immediately 165 * call the write callback several times, once with the \c fLaC signature, 166 * and once for each encoded metadata block. Note that for Ogg FLAC 167 * encoding you will usually get at least twice the number of callbacks than 168 * with native FLAC, one for the Ogg page header and one for the page body. 169 * 170 * After initializing the instance, the client may feed audio data to the 171 * encoder in one of two ways: 172 * 173 * - Channel separate, through FLAC__stream_encoder_process() - The client 174 * will pass an array of pointers to buffers, one for each channel, to 175 * the encoder, each of the same length. The samples need not be 176 * block-aligned, but each channel should have the same number of samples. 177 * - Channel interleaved, through 178 * FLAC__stream_encoder_process_interleaved() - The client will pass a single 179 * pointer to data that is channel-interleaved (i.e. channel0_sample0, 180 * channel1_sample0, ... , channelN_sample0, channel0_sample1, ...). 181 * Again, the samples need not be block-aligned but they must be 182 * sample-aligned, i.e. the first value should be channel0_sample0 and 183 * the last value channelN_sampleM. 184 * 185 * Note that for either process call, each sample in the buffers should be a 186 * signed integer, right-justified to the resolution set by 187 * FLAC__stream_encoder_set_bits_per_sample(). For example, if the resolution 188 * is 16 bits per sample, the samples should all be in the range [-32768,32767]. 189 * 190 * When the client is finished encoding data, it calls 191 * FLAC__stream_encoder_finish(), which causes the encoder to encode any 192 * data still in its input pipe, and call the metadata callback with the 193 * final encoding statistics. Then the instance may be deleted with 194 * FLAC__stream_encoder_delete() or initialized again to encode another 195 * stream. 196 * 197 * For programs that write their own metadata, but that do not know the 198 * actual metadata until after encoding, it is advantageous to instruct 199 * the encoder to write a PADDING block of the correct size, so that 200 * instead of rewriting the whole stream after encoding, the program can 201 * just overwrite the PADDING block. If only the maximum size of the 202 * metadata is known, the program can write a slightly larger padding 203 * block, then split it after encoding. 204 * 205 * Make sure you understand how lengths are calculated. All FLAC metadata 206 * blocks have a 4 byte header which contains the type and length. This 207 * length does not include the 4 bytes of the header. See the format page 208 * for the specification of metadata blocks and their lengths. 209 * 210 * \note 211 * If you are writing the FLAC data to a file via callbacks, make sure it 212 * is open for update (e.g. mode "w+" for stdio streams). This is because 213 * after the first encoding pass, the encoder will try to seek back to the 214 * beginning of the stream, to the STREAMINFO block, to write some data 215 * there. (If using FLAC__stream_encoder_init*_file() or 216 * FLAC__stream_encoder_init*_FILE(), the file is managed internally.) 217 * 218 * \note 219 * The "set" functions may only be called when the encoder is in the 220 * state FLAC__STREAM_ENCODER_UNINITIALIZED, i.e. after 221 * FLAC__stream_encoder_new() or FLAC__stream_encoder_finish(), but 222 * before FLAC__stream_encoder_init_*(). If this is the case they will 223 * return \c true, otherwise \c false. 224 * 225 * \note 226 * FLAC__stream_encoder_finish() resets all settings to the constructor 227 * defaults. 228 * 229 * \{ 230 */ 231 232 233 /** State values for a FLAC__StreamEncoder. 234 * 235 * The encoder's state can be obtained by calling FLAC__stream_encoder_get_state(). 236 * 237 * If the encoder gets into any other state besides \c FLAC__STREAM_ENCODER_OK 238 * or \c FLAC__STREAM_ENCODER_UNINITIALIZED, it becomes invalid for encoding and 239 * must be deleted with FLAC__stream_encoder_delete(). 240 */ 241 typedef enum { 242 243 FLAC__STREAM_ENCODER_OK = 0, 244 /**< The encoder is in the normal OK state and samples can be processed. */ 245 246 FLAC__STREAM_ENCODER_UNINITIALIZED, 247 /**< The encoder is in the uninitialized state; one of the 248 * FLAC__stream_encoder_init_*() functions must be called before samples 249 * can be processed. 250 */ 251 252 FLAC__STREAM_ENCODER_OGG_ERROR, 253 /**< An error occurred in the underlying Ogg layer. */ 254 255 FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR, 256 /**< An error occurred in the underlying verify stream decoder; 257 * check FLAC__stream_encoder_get_verify_decoder_state(). 258 */ 259 260 FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA, 261 /**< The verify decoder detected a mismatch between the original 262 * audio signal and the decoded audio signal. 263 */ 264 265 FLAC__STREAM_ENCODER_CLIENT_ERROR, 266 /**< One of the callbacks returned a fatal error. */ 267 268 FLAC__STREAM_ENCODER_IO_ERROR, 269 /**< An I/O error occurred while opening/reading/writing a file. 270 * Check \c errno. 271 */ 272 273 FLAC__STREAM_ENCODER_FRAMING_ERROR, 274 /**< An error occurred while writing the stream; usually, the 275 * write_callback returned an error. 276 */ 277 278 FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR 279 /**< Memory allocation failed. */ 280 281 } FLAC__StreamEncoderState; 282 283 /** Maps a FLAC__StreamEncoderState to a C string. 284 * 285 * Using a FLAC__StreamEncoderState as the index to this array 286 * will give the string equivalent. The contents should not be modified. 287 */ 288 extern FLAC_API const char * const FLAC__StreamEncoderStateString[]; 289 290 291 /** Possible return values for the FLAC__stream_encoder_init_*() functions. 292 */ 293 typedef enum { 294 295 FLAC__STREAM_ENCODER_INIT_STATUS_OK = 0, 296 /**< Initialization was successful. */ 297 298 FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR, 299 /**< General failure to set up encoder; call FLAC__stream_encoder_get_state() for cause. */ 300 301 FLAC__STREAM_ENCODER_INIT_STATUS_UNSUPPORTED_CONTAINER, 302 /**< The library was not compiled with support for the given container 303 * format. 304 */ 305 306 FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_CALLBACKS, 307 /**< A required callback was not supplied. */ 308 309 FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_NUMBER_OF_CHANNELS, 310 /**< The encoder has an invalid setting for number of channels. */ 311 312 FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BITS_PER_SAMPLE, 313 /**< The encoder has an invalid setting for bits-per-sample. 314 * FLAC supports 4-32 bps. 315 */ 316 317 FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_SAMPLE_RATE, 318 /**< The encoder has an invalid setting for the input sample rate. */ 319 320 FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_BLOCK_SIZE, 321 /**< The encoder has an invalid setting for the block size. */ 322 323 FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_MAX_LPC_ORDER, 324 /**< The encoder has an invalid setting for the maximum LPC order. */ 325 326 FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_QLP_COEFF_PRECISION, 327 /**< The encoder has an invalid setting for the precision of the quantized linear predictor coefficients. */ 328 329 FLAC__STREAM_ENCODER_INIT_STATUS_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER, 330 /**< The specified block size is less than the maximum LPC order. */ 331 332 FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE, 333 /**< The encoder is bound to the <A HREF="https://xiph.org/flac/format.html#subset">Subset</A> but other settings violate it. */ 334 335 FLAC__STREAM_ENCODER_INIT_STATUS_INVALID_METADATA, 336 /**< The metadata input to the encoder is invalid, in one of the following ways: 337 * - FLAC__stream_encoder_set_metadata() was called with a null pointer but a block count > 0 338 * - One of the metadata blocks contains an undefined type 339 * - It contains an illegal CUESHEET as checked by FLAC__format_cuesheet_is_legal() 340 * - It contains an illegal SEEKTABLE as checked by FLAC__format_seektable_is_legal() 341 * - It contains more than one SEEKTABLE block or more than one VORBIS_COMMENT block 342 */ 343 344 FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED 345 /**< FLAC__stream_encoder_init_*() was called when the encoder was 346 * already initialized, usually because 347 * FLAC__stream_encoder_finish() was not called. 348 */ 349 350 } FLAC__StreamEncoderInitStatus; 351 352 /** Maps a FLAC__StreamEncoderInitStatus to a C string. 353 * 354 * Using a FLAC__StreamEncoderInitStatus as the index to this array 355 * will give the string equivalent. The contents should not be modified. 356 */ 357 extern FLAC_API const char * const FLAC__StreamEncoderInitStatusString[]; 358 359 360 /** Return values for the FLAC__StreamEncoder read callback. 361 */ 362 typedef enum { 363 364 FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE, 365 /**< The read was OK and decoding can continue. */ 366 367 FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM, 368 /**< The read was attempted at the end of the stream. */ 369 370 FLAC__STREAM_ENCODER_READ_STATUS_ABORT, 371 /**< An unrecoverable error occurred. */ 372 373 FLAC__STREAM_ENCODER_READ_STATUS_UNSUPPORTED 374 /**< Client does not support reading back from the output. */ 375 376 } FLAC__StreamEncoderReadStatus; 377 378 /** Maps a FLAC__StreamEncoderReadStatus to a C string. 379 * 380 * Using a FLAC__StreamEncoderReadStatus as the index to this array 381 * will give the string equivalent. The contents should not be modified. 382 */ 383 extern FLAC_API const char * const FLAC__StreamEncoderReadStatusString[]; 384 385 386 /** Return values for the FLAC__StreamEncoder write callback. 387 */ 388 typedef enum { 389 390 FLAC__STREAM_ENCODER_WRITE_STATUS_OK = 0, 391 /**< The write was OK and encoding can continue. */ 392 393 FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR 394 /**< An unrecoverable error occurred. The encoder will return from the process call. */ 395 396 } FLAC__StreamEncoderWriteStatus; 397 398 /** Maps a FLAC__StreamEncoderWriteStatus to a C string. 399 * 400 * Using a FLAC__StreamEncoderWriteStatus as the index to this array 401 * will give the string equivalent. The contents should not be modified. 402 */ 403 extern FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[]; 404 405 406 /** Return values for the FLAC__StreamEncoder seek callback. 407 */ 408 typedef enum { 409 410 FLAC__STREAM_ENCODER_SEEK_STATUS_OK, 411 /**< The seek was OK and encoding can continue. */ 412 413 FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR, 414 /**< An unrecoverable error occurred. */ 415 416 FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED 417 /**< Client does not support seeking. */ 418 419 } FLAC__StreamEncoderSeekStatus; 420 421 /** Maps a FLAC__StreamEncoderSeekStatus to a C string. 422 * 423 * Using a FLAC__StreamEncoderSeekStatus as the index to this array 424 * will give the string equivalent. The contents should not be modified. 425 */ 426 extern FLAC_API const char * const FLAC__StreamEncoderSeekStatusString[]; 427 428 429 /** Return values for the FLAC__StreamEncoder tell callback. 430 */ 431 typedef enum { 432 433 FLAC__STREAM_ENCODER_TELL_STATUS_OK, 434 /**< The tell was OK and encoding can continue. */ 435 436 FLAC__STREAM_ENCODER_TELL_STATUS_ERROR, 437 /**< An unrecoverable error occurred. */ 438 439 FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED 440 /**< Client does not support seeking. */ 441 442 } FLAC__StreamEncoderTellStatus; 443 444 /** Maps a FLAC__StreamEncoderTellStatus to a C string. 445 * 446 * Using a FLAC__StreamEncoderTellStatus as the index to this array 447 * will give the string equivalent. The contents should not be modified. 448 */ 449 extern FLAC_API const char * const FLAC__StreamEncoderTellStatusString[]; 450 451 452 /*********************************************************************** 453 * 454 * class FLAC__StreamEncoder 455 * 456 ***********************************************************************/ 457 458 struct FLAC__StreamEncoderProtected; 459 struct FLAC__StreamEncoderPrivate; 460 /** The opaque structure definition for the stream encoder type. 461 * See the \link flac_stream_encoder stream encoder module \endlink 462 * for a detailed description. 463 */ 464 typedef struct { 465 struct FLAC__StreamEncoderProtected *protected_; /* avoid the C++ keyword 'protected' */ 466 struct FLAC__StreamEncoderPrivate *private_; /* avoid the C++ keyword 'private' */ 467 } FLAC__StreamEncoder; 468 469 /** Signature for the read callback. 470 * 471 * A function pointer matching this signature must be passed to 472 * FLAC__stream_encoder_init_ogg_stream() if seeking is supported. 473 * The supplied function will be called when the encoder needs to read back 474 * encoded data. This happens during the metadata callback, when the encoder 475 * has to read, modify, and rewrite the metadata (e.g. seekpoints) gathered 476 * while encoding. The address of the buffer to be filled is supplied, along 477 * with the number of bytes the buffer can hold. The callback may choose to 478 * supply less data and modify the byte count but must be careful not to 479 * overflow the buffer. The callback then returns a status code chosen from 480 * FLAC__StreamEncoderReadStatus. 481 * 482 * Here is an example of a read callback for stdio streams: 483 * \code 484 * FLAC__StreamEncoderReadStatus read_cb(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data) 485 * { 486 * FILE *file = ((MyClientData*)client_data)->file; 487 * if(*bytes > 0) { 488 * *bytes = fread(buffer, sizeof(FLAC__byte), *bytes, file); 489 * if(ferror(file)) 490 * return FLAC__STREAM_ENCODER_READ_STATUS_ABORT; 491 * else if(*bytes == 0) 492 * return FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM; 493 * else 494 * return FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE; 495 * } 496 * else 497 * return FLAC__STREAM_ENCODER_READ_STATUS_ABORT; 498 * } 499 * \endcode 500 * 501 * \note In general, FLAC__StreamEncoder functions which change the 502 * state should not be called on the \a encoder while in the callback. 503 * 504 * \param encoder The encoder instance calling the callback. 505 * \param buffer A pointer to a location for the callee to store 506 * data to be encoded. 507 * \param bytes A pointer to the size of the buffer. On entry 508 * to the callback, it contains the maximum number 509 * of bytes that may be stored in \a buffer. The 510 * callee must set it to the actual number of bytes 511 * stored (0 in case of error or end-of-stream) before 512 * returning. 513 * \param client_data The callee's client data set through 514 * FLAC__stream_encoder_set_client_data(). 515 * \retval FLAC__StreamEncoderReadStatus 516 * The callee's return status. 517 */ 518 typedef FLAC__StreamEncoderReadStatus (*FLAC__StreamEncoderReadCallback)(const FLAC__StreamEncoder *encoder, FLAC__byte buffer[], size_t *bytes, void *client_data); 519 520 /** Signature for the write callback. 521 * 522 * A function pointer matching this signature must be passed to 523 * FLAC__stream_encoder_init*_stream(). The supplied function will be called 524 * by the encoder anytime there is raw encoded data ready to write. It may 525 * include metadata mixed with encoded audio frames and the data is not 526 * guaranteed to be aligned on frame or metadata block boundaries. 527 * 528 * The only duty of the callback is to write out the \a bytes worth of data 529 * in \a buffer to the current position in the output stream. The arguments 530 * \a samples and \a current_frame are purely informational. If \a samples 531 * is greater than \c 0, then \a current_frame will hold the current frame 532 * number that is being written; otherwise it indicates that the write 533 * callback is being called to write metadata. 534 * 535 * \note 536 * Unlike when writing to native FLAC, when writing to Ogg FLAC the 537 * write callback will be called twice when writing each audio 538 * frame; once for the page header, and once for the page body. 539 * When writing the page header, the \a samples argument to the 540 * write callback will be \c 0. 541 * 542 * \note In general, FLAC__StreamEncoder functions which change the 543 * state should not be called on the \a encoder while in the callback. 544 * 545 * \param encoder The encoder instance calling the callback. 546 * \param buffer An array of encoded data of length \a bytes. 547 * \param bytes The byte length of \a buffer. 548 * \param samples The number of samples encoded by \a buffer. 549 * \c 0 has a special meaning; see above. 550 * \param current_frame The number of the current frame being encoded. 551 * \param client_data The callee's client data set through 552 * FLAC__stream_encoder_init_*(). 553 * \retval FLAC__StreamEncoderWriteStatus 554 * The callee's return status. 555 */ 556 typedef FLAC__StreamEncoderWriteStatus (*FLAC__StreamEncoderWriteCallback)(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, uint32_t samples, uint32_t current_frame, void *client_data); 557 558 /** Signature for the seek callback. 559 * 560 * A function pointer matching this signature may be passed to 561 * FLAC__stream_encoder_init*_stream(). The supplied function will be called 562 * when the encoder needs to seek the output stream. The encoder will pass 563 * the absolute byte offset to seek to, 0 meaning the beginning of the stream. 564 * 565 * Here is an example of a seek callback for stdio streams: 566 * \code 567 * FLAC__StreamEncoderSeekStatus seek_cb(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data) 568 * { 569 * FILE *file = ((MyClientData*)client_data)->file; 570 * if(file == stdin) 571 * return FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED; 572 * else if(fseeko(file, (off_t)absolute_byte_offset, SEEK_SET) < 0) 573 * return FLAC__STREAM_ENCODER_SEEK_STATUS_ERROR; 574 * else 575 * return FLAC__STREAM_ENCODER_SEEK_STATUS_OK; 576 * } 577 * \endcode 578 * 579 * \note In general, FLAC__StreamEncoder functions which change the 580 * state should not be called on the \a encoder while in the callback. 581 * 582 * \param encoder The encoder instance calling the callback. 583 * \param absolute_byte_offset The offset from the beginning of the stream 584 * to seek to. 585 * \param client_data The callee's client data set through 586 * FLAC__stream_encoder_init_*(). 587 * \retval FLAC__StreamEncoderSeekStatus 588 * The callee's return status. 589 */ 590 typedef FLAC__StreamEncoderSeekStatus (*FLAC__StreamEncoderSeekCallback)(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data); 591 592 /** Signature for the tell callback. 593 * 594 * A function pointer matching this signature may be passed to 595 * FLAC__stream_encoder_init*_stream(). The supplied function will be called 596 * when the encoder needs to know the current position of the output stream. 597 * 598 * \warning 599 * The callback must return the true current byte offset of the output to 600 * which the encoder is writing. If you are buffering the output, make 601 * sure and take this into account. If you are writing directly to a 602 * FILE* from your write callback, ftell() is sufficient. If you are 603 * writing directly to a file descriptor from your write callback, you 604 * can use lseek(fd, SEEK_CUR, 0). The encoder may later seek back to 605 * these points to rewrite metadata after encoding. 606 * 607 * Here is an example of a tell callback for stdio streams: 608 * \code 609 * FLAC__StreamEncoderTellStatus tell_cb(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data) 610 * { 611 * FILE *file = ((MyClientData*)client_data)->file; 612 * off_t pos; 613 * if(file == stdin) 614 * return FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED; 615 * else if((pos = ftello(file)) < 0) 616 * return FLAC__STREAM_ENCODER_TELL_STATUS_ERROR; 617 * else { 618 * *absolute_byte_offset = (FLAC__uint64)pos; 619 * return FLAC__STREAM_ENCODER_TELL_STATUS_OK; 620 * } 621 * } 622 * \endcode 623 * 624 * \note In general, FLAC__StreamEncoder functions which change the 625 * state should not be called on the \a encoder while in the callback. 626 * 627 * \param encoder The encoder instance calling the callback. 628 * \param absolute_byte_offset The address at which to store the current 629 * position of the output. 630 * \param client_data The callee's client data set through 631 * FLAC__stream_encoder_init_*(). 632 * \retval FLAC__StreamEncoderTellStatus 633 * The callee's return status. 634 */ 635 typedef FLAC__StreamEncoderTellStatus (*FLAC__StreamEncoderTellCallback)(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data); 636 637 /** Signature for the metadata callback. 638 * 639 * A function pointer matching this signature may be passed to 640 * FLAC__stream_encoder_init*_stream(). The supplied function will be called 641 * once at the end of encoding with the populated STREAMINFO structure. This 642 * is so the client can seek back to the beginning of the file and write the 643 * STREAMINFO block with the correct statistics after encoding (like 644 * minimum/maximum frame size and total samples). 645 * 646 * \note In general, FLAC__StreamEncoder functions which change the 647 * state should not be called on the \a encoder while in the callback. 648 * 649 * \param encoder The encoder instance calling the callback. 650 * \param metadata The final populated STREAMINFO block. 651 * \param client_data The callee's client data set through 652 * FLAC__stream_encoder_init_*(). 653 */ 654 typedef void (*FLAC__StreamEncoderMetadataCallback)(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data); 655 656 /** Signature for the progress callback. 657 * 658 * A function pointer matching this signature may be passed to 659 * FLAC__stream_encoder_init*_file() or FLAC__stream_encoder_init*_FILE(). 660 * The supplied function will be called when the encoder has finished 661 * writing a frame. The \c total_frames_estimate argument to the 662 * callback will be based on the value from 663 * FLAC__stream_encoder_set_total_samples_estimate(). 664 * 665 * \note In general, FLAC__StreamEncoder functions which change the 666 * state should not be called on the \a encoder while in the callback. 667 * 668 * \param encoder The encoder instance calling the callback. 669 * \param bytes_written Bytes written so far. 670 * \param samples_written Samples written so far. 671 * \param frames_written Frames written so far. 672 * \param total_frames_estimate The estimate of the total number of 673 * frames to be written. 674 * \param client_data The callee's client data set through 675 * FLAC__stream_encoder_init_*(). 676 */ 677 typedef void (*FLAC__StreamEncoderProgressCallback)(const FLAC__StreamEncoder *encoder, FLAC__uint64 bytes_written, FLAC__uint64 samples_written, uint32_t frames_written, uint32_t total_frames_estimate, void *client_data); 678 679 680 /*********************************************************************** 681 * 682 * Class constructor/destructor 683 * 684 ***********************************************************************/ 685 686 /** Create a new stream encoder instance. The instance is created with 687 * default settings; see the individual FLAC__stream_encoder_set_*() 688 * functions for each setting's default. 689 * 690 * \retval FLAC__StreamEncoder* 691 * \c NULL if there was an error allocating memory, else the new instance. 692 */ 693 FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new(void); 694 695 /** Free an encoder instance. Deletes the object pointed to by \a encoder. 696 * 697 * \param encoder A pointer to an existing encoder. 698 * \assert 699 * \code encoder != NULL \endcode 700 */ 701 FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder); 702 703 704 /*********************************************************************** 705 * 706 * Public class method prototypes 707 * 708 ***********************************************************************/ 709 710 /** Set the serial number for the FLAC stream to use in the Ogg container. 711 * 712 * \note 713 * This does not need to be set for native FLAC encoding. 714 * 715 * \note 716 * It is recommended to set a serial number explicitly as the default of '0' 717 * may collide with other streams. 718 * 719 * \default \c 0 720 * \param encoder An encoder instance to set. 721 * \param serial_number See above. 722 * \assert 723 * \code encoder != NULL \endcode 724 * \retval FLAC__bool 725 * \c false if the encoder is already initialized, else \c true. 726 */ 727 FLAC_API FLAC__bool FLAC__stream_encoder_set_ogg_serial_number(FLAC__StreamEncoder *encoder, long serial_number); 728 729 /** Set the "verify" flag. If \c true, the encoder will verify it's own 730 * encoded output by feeding it through an internal decoder and comparing 731 * the original signal against the decoded signal. If a mismatch occurs, 732 * the process call will return \c false. Note that this will slow the 733 * encoding process by the extra time required for decoding and comparison. 734 * 735 * \default \c false 736 * \param encoder An encoder instance to set. 737 * \param value Flag value (see above). 738 * \assert 739 * \code encoder != NULL \endcode 740 * \retval FLAC__bool 741 * \c false if the encoder is already initialized, else \c true. 742 */ 743 FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value); 744 745 /** Set the <A HREF="https://xiph.org/flac/format.html#subset">Subset</A> flag. If \c true, 746 * the encoder will comply with the Subset and will check the 747 * settings during FLAC__stream_encoder_init_*() to see if all settings 748 * comply. If \c false, the settings may take advantage of the full 749 * range that the format allows. 750 * 751 * Make sure you know what it entails before setting this to \c false. 752 * 753 * \default \c true 754 * \param encoder An encoder instance to set. 755 * \param value Flag value (see above). 756 * \assert 757 * \code encoder != NULL \endcode 758 * \retval FLAC__bool 759 * \c false if the encoder is already initialized, else \c true. 760 */ 761 FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value); 762 763 /** Set the number of channels to be encoded. 764 * 765 * \default \c 2 766 * \param encoder An encoder instance to set. 767 * \param value See above. 768 * \assert 769 * \code encoder != NULL \endcode 770 * \retval FLAC__bool 771 * \c false if the encoder is already initialized, else \c true. 772 */ 773 FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, uint32_t value); 774 775 /** Set the sample resolution of the input to be encoded. 776 * 777 * \warning 778 * Do not feed the encoder data that is wider than the value you 779 * set here or you will generate an invalid stream. 780 * 781 * \default \c 16 782 * \param encoder An encoder instance to set. 783 * \param value See above. 784 * \assert 785 * \code encoder != NULL \endcode 786 * \retval FLAC__bool 787 * \c false if the encoder is already initialized, else \c true. 788 */ 789 FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, uint32_t value); 790 791 /** Set the sample rate (in Hz) of the input to be encoded. 792 * 793 * \default \c 44100 794 * \param encoder An encoder instance to set. 795 * \param value See above. 796 * \assert 797 * \code encoder != NULL \endcode 798 * \retval FLAC__bool 799 * \c false if the encoder is already initialized, else \c true. 800 */ 801 FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, uint32_t value); 802 803 /** Set the compression level 804 * 805 * The compression level is roughly proportional to the amount of effort 806 * the encoder expends to compress the file. A higher level usually 807 * means more computation but higher compression. The default level is 808 * suitable for most applications. 809 * 810 * Currently the levels range from \c 0 (fastest, least compression) to 811 * \c 8 (slowest, most compression). A value larger than \c 8 will be 812 * treated as \c 8. 813 * 814 * This function automatically calls the following other \c _set_ 815 * functions with appropriate values, so the client does not need to 816 * unless it specifically wants to override them: 817 * - FLAC__stream_encoder_set_do_mid_side_stereo() 818 * - FLAC__stream_encoder_set_loose_mid_side_stereo() 819 * - FLAC__stream_encoder_set_apodization() 820 * - FLAC__stream_encoder_set_max_lpc_order() 821 * - FLAC__stream_encoder_set_qlp_coeff_precision() 822 * - FLAC__stream_encoder_set_do_qlp_coeff_prec_search() 823 * - FLAC__stream_encoder_set_do_escape_coding() 824 * - FLAC__stream_encoder_set_do_exhaustive_model_search() 825 * - FLAC__stream_encoder_set_min_residual_partition_order() 826 * - FLAC__stream_encoder_set_max_residual_partition_order() 827 * - FLAC__stream_encoder_set_rice_parameter_search_dist() 828 * 829 * The actual values set for each level are: 830 * <table> 831 * <tr> 832 * <td><b>level</b></td> 833 * <td>do mid-side stereo</td> 834 * <td>loose mid-side stereo</td> 835 * <td>apodization</td> 836 * <td>max lpc order</td> 837 * <td>qlp coeff precision</td> 838 * <td>qlp coeff prec search</td> 839 * <td>escape coding</td> 840 * <td>exhaustive model search</td> 841 * <td>min residual partition order</td> 842 * <td>max residual partition order</td> 843 * <td>rice parameter search dist</td> 844 * </tr> 845 * <tr> <td><b>0</b></td> <td>false</td> <td>false</td> <td>tukey(0.5)</td> <td>0</td> <td>0</td> <td>false</td> <td>false</td> <td>false</td> <td>0</td> <td>3</td> <td>0</td> </tr> 846 * <tr> <td><b>1</b></td> <td>true</td> <td>true</td> <td>tukey(0.5)</td> <td>0</td> <td>0</td> <td>false</td> <td>false</td> <td>false</td> <td>0</td> <td>3</td> <td>0</td> </tr> 847 * <tr> <td><b>2</b></td> <td>true</td> <td>false</td> <td>tukey(0.5)</td> <td>0</td> <td>0</td> <td>false</td> <td>false</td> <td>false</td> <td>0</td> <td>3</td> <td>0</td> </tr> 848 * <tr> <td><b>3</b></td> <td>false</td> <td>false</td> <td>tukey(0.5)</td> <td>6</td> <td>0</td> <td>false</td> <td>false</td> <td>false</td> <td>0</td> <td>4</td> <td>0</td> </tr> 849 * <tr> <td><b>4</b></td> <td>true</td> <td>true</td> <td>tukey(0.5)</td> <td>8</td> <td>0</td> <td>false</td> <td>false</td> <td>false</td> <td>0</td> <td>4</td> <td>0</td> </tr> 850 * <tr> <td><b>5</b></td> <td>true</td> <td>false</td> <td>tukey(0.5)</td> <td>8</td> <td>0</td> <td>false</td> <td>false</td> <td>false</td> <td>0</td> <td>5</td> <td>0</td> </tr> 851 * <tr> <td><b>6</b></td> <td>true</td> <td>false</td> <td>subdivide_tukey(2)</td> <td>8</td> <td>0</td> <td>false</td> <td>false</td> <td>false</td> <td>0</td> <td>6</td> <td>0</td> </tr> 852 * <tr> <td><b>7</b></td> <td>true</td> <td>false</td> <td>subdivide_tukey(2)</td> <td>12</td> <td>0</td> <td>false</td> <td>false</td> <td>false</td> <td>0</td> <td>6</td> <td>0</td> </tr> 853 * <tr> <td><b>8</b></td> <td>true</td> <td>false</td> <td>subdivide_tukey(2)</td> <td>12</td> <td>0</td> <td>false</td> <td>false</td> <td>false</td> <td>0</td> <td>6</td> <td>0</td> </tr> 854 * </table> 855 * 856 * \default \c 5 857 * \param encoder An encoder instance to set. 858 * \param value See above. 859 * \assert 860 * \code encoder != NULL \endcode 861 * \retval FLAC__bool 862 * \c false if the encoder is already initialized, else \c true. 863 */ 864 FLAC_API FLAC__bool FLAC__stream_encoder_set_compression_level(FLAC__StreamEncoder *encoder, uint32_t value); 865 866 /** Set the blocksize to use while encoding. 867 * 868 * The number of samples to use per frame. Use \c 0 to let the encoder 869 * estimate a blocksize; this is usually best. 870 * 871 * \default \c 0 872 * \param encoder An encoder instance to set. 873 * \param value See above. 874 * \assert 875 * \code encoder != NULL \endcode 876 * \retval FLAC__bool 877 * \c false if the encoder is already initialized, else \c true. 878 */ 879 FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, uint32_t value); 880 881 /** Set to \c true to enable mid-side encoding on stereo input. The 882 * number of channels must be 2 for this to have any effect. Set to 883 * \c false to use only independent channel coding. 884 * 885 * \default \c true 886 * \param encoder An encoder instance to set. 887 * \param value Flag value (see above). 888 * \assert 889 * \code encoder != NULL \endcode 890 * \retval FLAC__bool 891 * \c false if the encoder is already initialized, else \c true. 892 */ 893 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value); 894 895 /** Set to \c true to enable adaptive switching between mid-side and 896 * left-right encoding on stereo input. Set to \c false to use 897 * exhaustive searching. Setting this to \c true requires 898 * FLAC__stream_encoder_set_do_mid_side_stereo() to also be set to 899 * \c true in order to have any effect. 900 * 901 * \default \c false 902 * \param encoder An encoder instance to set. 903 * \param value Flag value (see above). 904 * \assert 905 * \code encoder != NULL \endcode 906 * \retval FLAC__bool 907 * \c false if the encoder is already initialized, else \c true. 908 */ 909 FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value); 910 911 /** Sets the apodization function(s) the encoder will use when windowing 912 * audio data for LPC analysis. 913 * 914 * The \a specification is a plain ASCII string which specifies exactly 915 * which functions to use. There may be more than one (up to 32), 916 * separated by \c ';' characters. Some functions take one or more 917 * comma-separated arguments in parentheses. 918 * 919 * The available functions are \c bartlett, \c bartlett_hann, 920 * \c blackman, \c blackman_harris_4term_92db, \c connes, \c flattop, 921 * \c gauss(STDDEV), \c hamming, \c hann, \c kaiser_bessel, \c nuttall, 922 * \c rectangle, \c triangle, \c tukey(P), \c partial_tukey(n[/ov[/P]]), 923 * \c punchout_tukey(n[/ov[/P]]), \c subdivide_tukey(n[/P]), \c welch. 924 * 925 * For \c gauss(STDDEV), STDDEV specifies the standard deviation 926 * (0<STDDEV<=0.5). 927 * 928 * For \c tukey(P), P specifies the fraction of the window that is 929 * tapered (0<=P<=1). P=0 corresponds to \c rectangle and P=1 930 * corresponds to \c hann. 931 * 932 * Specifying \c partial_tukey or \c punchout_tukey works a little 933 * different. These do not specify a single apodization function, but 934 * a series of them with some overlap. partial_tukey specifies a series 935 * of small windows (all treated separately) while punchout_tukey 936 * specifies a series of windows that have a hole in them. In this way, 937 * the predictor is constructed with only a part of the block, which 938 * helps in case a block consists of dissimilar parts. 939 * 940 * The three parameters that can be specified for the functions are 941 * n, ov and P. n is the number of functions to add, ov is the overlap 942 * of the windows in case of partial_tukey and the overlap in the gaps 943 * in case of punchout_tukey. P is the fraction of the window that is 944 * tapered, like with a regular tukey window. The function can be 945 * specified with only a number, a number and an overlap, or a number 946 * an overlap and a P, for example, partial_tukey(3), partial_tukey(3/0.3) 947 * and partial_tukey(3/0.3/0.5) are all valid. ov should be smaller than 1 948 * and can be negative. 949 * 950 * subdivide_tukey(n) is a more efficient reimplementation of 951 * partial_tukey and punchout_tukey taken together, recycling as much data 952 * as possible. It combines all possible non-redundant partial_tukey(n) 953 * and punchout_tukey(n) up to the n specified. Specifying 954 * subdivide_tukey(3) is equivalent to specifying tukey, partial_tukey(2), 955 * partial_tukey(3) and punchout_tukey(3), specifying subdivide_tukey(5) 956 * equivalently adds partial_tukey(4), punchout_tukey(4), partial_tukey(5) 957 * and punchout_tukey(5). To be able to reuse data as much as possible, 958 * the tukey taper is taken equal for all windows, and the P specified is 959 * applied for the smallest used window. In other words, 960 * subdivide_tukey(2/0.5) results in a taper equal to that of tukey(0.25) 961 * and subdivide_tukey(5) in a taper equal to that of tukey(0.1). The 962 * default P for subdivide_tukey when none is specified is 0.5. 963 * 964 * Example specifications are \c "blackman" or 965 * \c "hann;triangle;tukey(0.5);tukey(0.25);tukey(0.125)" 966 * 967 * Any function that is specified erroneously is silently dropped. Up 968 * to 32 functions are kept, the rest are dropped. If the specification 969 * is empty the encoder defaults to \c "tukey(0.5)". 970 * 971 * When more than one function is specified, then for every subframe the 972 * encoder will try each of them separately and choose the window that 973 * results in the smallest compressed subframe. 974 * 975 * Note that each function specified causes the encoder to occupy a 976 * floating point array in which to store the window. Also note that the 977 * values of P, STDDEV and ov are locale-specific, so if the comma 978 * separator specified by the locale is a comma, a comma should be used. 979 * A locale-independent way is to specify using scientific notation, 980 * e.g. 5e-1 instad of 0.5 or 0,5. 981 * 982 * \default \c "tukey(0.5)" 983 * \param encoder An encoder instance to set. 984 * \param specification See above. 985 * \assert 986 * \code encoder != NULL \endcode 987 * \code specification != NULL \endcode 988 * \retval FLAC__bool 989 * \c false if the encoder is already initialized, else \c true. 990 */ 991 FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification); 992 993 /** Set the maximum LPC order, or \c 0 to use only the fixed predictors. 994 * 995 * \default \c 8 996 * \param encoder An encoder instance to set. 997 * \param value See above. 998 * \assert 999 * \code encoder != NULL \endcode 1000 * \retval FLAC__bool 1001 * \c false if the encoder is already initialized, else \c true. 1002 */ 1003 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, uint32_t value); 1004 1005 /** Set the precision, in bits, of the quantized linear predictor 1006 * coefficients, or \c 0 to let the encoder select it based on the 1007 * blocksize. 1008 * 1009 * \default \c 0 1010 * \param encoder An encoder instance to set. 1011 * \param value See above. 1012 * \assert 1013 * \code encoder != NULL \endcode 1014 * \retval FLAC__bool 1015 * \c false if the encoder is already initialized, else \c true. 1016 */ 1017 FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, uint32_t value); 1018 1019 /** Set to \c false to use only the specified quantized linear predictor 1020 * coefficient precision, or \c true to search neighboring precision 1021 * values and use the best one. 1022 * 1023 * \default \c false 1024 * \param encoder An encoder instance to set. 1025 * \param value See above. 1026 * \assert 1027 * \code encoder != NULL \endcode 1028 * \retval FLAC__bool 1029 * \c false if the encoder is already initialized, else \c true. 1030 */ 1031 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value); 1032 1033 /** Deprecated. Setting this value has no effect. 1034 * 1035 * \default \c false 1036 * \param encoder An encoder instance to set. 1037 * \param value See above. 1038 * \assert 1039 * \code encoder != NULL \endcode 1040 * \retval FLAC__bool 1041 * \c false if the encoder is already initialized, else \c true. 1042 */ 1043 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value); 1044 1045 /** Set to \c false to let the encoder estimate the best model order 1046 * based on the residual signal energy, or \c true to force the 1047 * encoder to evaluate all order models and select the best. 1048 * 1049 * \default \c false 1050 * \param encoder An encoder instance to set. 1051 * \param value See above. 1052 * \assert 1053 * \code encoder != NULL \endcode 1054 * \retval FLAC__bool 1055 * \c false if the encoder is already initialized, else \c true. 1056 */ 1057 FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value); 1058 1059 /** Set the minimum partition order to search when coding the residual. 1060 * This is used in tandem with 1061 * FLAC__stream_encoder_set_max_residual_partition_order(). 1062 * 1063 * The partition order determines the context size in the residual. 1064 * The context size will be approximately <tt>blocksize / (2 ^ order)</tt>. 1065 * 1066 * Set both min and max values to \c 0 to force a single context, 1067 * whose Rice parameter is based on the residual signal variance. 1068 * Otherwise, set a min and max order, and the encoder will search 1069 * all orders, using the mean of each context for its Rice parameter, 1070 * and use the best. 1071 * 1072 * \default \c 0 1073 * \param encoder An encoder instance to set. 1074 * \param value See above. 1075 * \assert 1076 * \code encoder != NULL \endcode 1077 * \retval FLAC__bool 1078 * \c false if the encoder is already initialized, else \c true. 1079 */ 1080 FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, uint32_t value); 1081 1082 /** Set the maximum partition order to search when coding the residual. 1083 * This is used in tandem with 1084 * FLAC__stream_encoder_set_min_residual_partition_order(). 1085 * 1086 * The partition order determines the context size in the residual. 1087 * The context size will be approximately <tt>blocksize / (2 ^ order)</tt>. 1088 * 1089 * Set both min and max values to \c 0 to force a single context, 1090 * whose Rice parameter is based on the residual signal variance. 1091 * Otherwise, set a min and max order, and the encoder will search 1092 * all orders, using the mean of each context for its Rice parameter, 1093 * and use the best. 1094 * 1095 * \default \c 5 1096 * \param encoder An encoder instance to set. 1097 * \param value See above. 1098 * \assert 1099 * \code encoder != NULL \endcode 1100 * \retval FLAC__bool 1101 * \c false if the encoder is already initialized, else \c true. 1102 */ 1103 FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, uint32_t value); 1104 1105 /** Deprecated. Setting this value has no effect. 1106 * 1107 * \default \c 0 1108 * \param encoder An encoder instance to set. 1109 * \param value See above. 1110 * \assert 1111 * \code encoder != NULL \endcode 1112 * \retval FLAC__bool 1113 * \c false if the encoder is already initialized, else \c true. 1114 */ 1115 FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, uint32_t value); 1116 1117 /** Set an estimate of the total samples that will be encoded. 1118 * This is merely an estimate and may be set to \c 0 if unknown. 1119 * This value will be written to the STREAMINFO block before encoding, 1120 * and can remove the need for the caller to rewrite the value later 1121 * if the value is known before encoding. 1122 * 1123 * \default \c 0 1124 * \param encoder An encoder instance to set. 1125 * \param value See above. 1126 * \assert 1127 * \code encoder != NULL \endcode 1128 * \retval FLAC__bool 1129 * \c false if the encoder is already initialized, else \c true. 1130 */ 1131 FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value); 1132 1133 /** Set the metadata blocks to be emitted to the stream before encoding. 1134 * A value of \c NULL, \c 0 implies no metadata; otherwise, supply an 1135 * array of pointers to metadata blocks. The array is non-const since 1136 * the encoder may need to change the \a is_last flag inside them, and 1137 * in some cases update seek point offsets. Otherwise, the encoder will 1138 * not modify or free the blocks. It is up to the caller to free the 1139 * metadata blocks after encoding finishes. 1140 * 1141 * \note 1142 * The encoder stores only copies of the pointers in the \a metadata array; 1143 * the metadata blocks themselves must survive at least until after 1144 * FLAC__stream_encoder_finish() returns. Do not free the blocks until then. 1145 * 1146 * \note 1147 * The STREAMINFO block is always written and no STREAMINFO block may 1148 * occur in the supplied array. 1149 * 1150 * \note 1151 * By default the encoder does not create a SEEKTABLE. If one is supplied 1152 * in the \a metadata array, but the client has specified that it does not 1153 * support seeking, then the SEEKTABLE will be written verbatim. However 1154 * by itself this is not very useful as the client will not know the stream 1155 * offsets for the seekpoints ahead of time. In order to get a proper 1156 * seektable the client must support seeking. See next note. 1157 * 1158 * \note 1159 * SEEKTABLE blocks are handled specially. Since you will not know 1160 * the values for the seek point stream offsets, you should pass in 1161 * a SEEKTABLE 'template', that is, a SEEKTABLE object with the 1162 * required sample numbers (or placeholder points), with \c 0 for the 1163 * \a frame_samples and \a stream_offset fields for each point. If the 1164 * client has specified that it supports seeking by providing a seek 1165 * callback to FLAC__stream_encoder_init_stream() or both seek AND read 1166 * callback to FLAC__stream_encoder_init_ogg_stream() (or by using 1167 * FLAC__stream_encoder_init*_file() or FLAC__stream_encoder_init*_FILE()), 1168 * then while it is encoding the encoder will fill the stream offsets in 1169 * for you and when encoding is finished, it will seek back and write the 1170 * real values into the SEEKTABLE block in the stream. There are helper 1171 * routines for manipulating seektable template blocks; see metadata.h: 1172 * FLAC__metadata_object_seektable_template_*(). If the client does 1173 * not support seeking, the SEEKTABLE will have inaccurate offsets which 1174 * will slow down or remove the ability to seek in the FLAC stream. 1175 * 1176 * \note 1177 * The encoder instance \b will modify the first \c SEEKTABLE block 1178 * as it transforms the template to a valid seektable while encoding, 1179 * but it is still up to the caller to free all metadata blocks after 1180 * encoding. 1181 * 1182 * \note 1183 * A VORBIS_COMMENT block may be supplied. The vendor string in it 1184 * will be ignored. libFLAC will use it's own vendor string. libFLAC 1185 * will not modify the passed-in VORBIS_COMMENT's vendor string, it 1186 * will simply write it's own into the stream. If no VORBIS_COMMENT 1187 * block is present in the \a metadata array, libFLAC will write an 1188 * empty one, containing only the vendor string. 1189 * 1190 * \note The Ogg FLAC mapping requires that the VORBIS_COMMENT block be 1191 * the second metadata block of the stream. The encoder already supplies 1192 * the STREAMINFO block automatically. If \a metadata does not contain a 1193 * VORBIS_COMMENT block, the encoder will supply that too. Otherwise, if 1194 * \a metadata does contain a VORBIS_COMMENT block and it is not the 1195 * first, the init function will reorder \a metadata by moving the 1196 * VORBIS_COMMENT block to the front; the relative ordering of the other 1197 * blocks will remain as they were. 1198 * 1199 * \note The Ogg FLAC mapping limits the number of metadata blocks per 1200 * stream to \c 65535. If \a num_blocks exceeds this the function will 1201 * return \c false. 1202 * 1203 * \default \c NULL, 0 1204 * \param encoder An encoder instance to set. 1205 * \param metadata See above. 1206 * \param num_blocks See above. 1207 * \assert 1208 * \code encoder != NULL \endcode 1209 * \retval FLAC__bool 1210 * \c false if the encoder is already initialized, else \c true. 1211 * \c false if the encoder is already initialized, or if 1212 * \a num_blocks > 65535 if encoding to Ogg FLAC, else \c true. 1213 */ 1214 FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, uint32_t num_blocks); 1215 1216 /** Set to \c true to make the encoder not output frames which contain 1217 * only constant subframes. This is beneficial for streaming 1218 * applications: very small frames can cause problems with buffering 1219 * as bitrates can drop as low 1kbit/s for CDDA audio encoded within 1220 * subset. The minimum bitrate for a FLAC file encoded with this 1221 * function used is raised to 1bit/sample (i.e. 48kbit/s for 48kHz 1222 * material). 1223 * 1224 * \default \c false 1225 * \param encoder An encoder instance to set. 1226 * \param value Flag value (see above). 1227 * \assert 1228 * \code encoder != NULL \endcode 1229 * \retval FLAC__bool 1230 * \c false if the encoder is already initialized, else \c true. 1231 */ 1232 FLAC_API FLAC__bool FLAC__stream_encoder_set_limit_min_bitrate(FLAC__StreamEncoder *encoder, FLAC__bool value); 1233 1234 /** Get the current encoder state. 1235 * 1236 * \param encoder An encoder instance to query. 1237 * \assert 1238 * \code encoder != NULL \endcode 1239 * \retval FLAC__StreamEncoderState 1240 * The current encoder state. 1241 */ 1242 FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder); 1243 1244 /** Get the state of the verify stream decoder. 1245 * Useful when the stream encoder state is 1246 * \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR. 1247 * 1248 * \param encoder An encoder instance to query. 1249 * \assert 1250 * \code encoder != NULL \endcode 1251 * \retval FLAC__StreamDecoderState 1252 * The verify stream decoder state. 1253 */ 1254 FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder); 1255 1256 /** Get the current encoder state as a C string. 1257 * This version automatically resolves 1258 * \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR by getting the 1259 * verify decoder's state. 1260 * 1261 * \param encoder A encoder instance to query. 1262 * \assert 1263 * \code encoder != NULL \endcode 1264 * \retval const char * 1265 * The encoder state as a C string. Do not modify the contents. 1266 */ 1267 FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder); 1268 1269 /** Get relevant values about the nature of a verify decoder error. 1270 * Useful when the stream encoder state is 1271 * \c FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR. The arguments should 1272 * be addresses in which the stats will be returned, or NULL if value 1273 * is not desired. 1274 * 1275 * \param encoder An encoder instance to query. 1276 * \param absolute_sample The absolute sample number of the mismatch. 1277 * \param frame_number The number of the frame in which the mismatch occurred. 1278 * \param channel The channel in which the mismatch occurred. 1279 * \param sample The number of the sample (relative to the frame) in 1280 * which the mismatch occurred. 1281 * \param expected The expected value for the sample in question. 1282 * \param got The actual value returned by the decoder. 1283 * \assert 1284 * \code encoder != NULL \endcode 1285 */ 1286 FLAC_API void FLAC__stream_encoder_get_verify_decoder_error_stats(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_sample, uint32_t *frame_number, uint32_t *channel, uint32_t *sample, FLAC__int32 *expected, FLAC__int32 *got); 1287 1288 /** Get the "verify" flag. 1289 * 1290 * \param encoder An encoder instance to query. 1291 * \assert 1292 * \code encoder != NULL \endcode 1293 * \retval FLAC__bool 1294 * See FLAC__stream_encoder_set_verify(). 1295 */ 1296 FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder); 1297 1298 /** Get the <A HREF="https://xiph.org/flac/format.html#subset">Subset</A> flag. 1299 * 1300 * \param encoder An encoder instance to query. 1301 * \assert 1302 * \code encoder != NULL \endcode 1303 * \retval FLAC__bool 1304 * See FLAC__stream_encoder_set_streamable_subset(). 1305 */ 1306 FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder); 1307 1308 /** Get the number of input channels being processed. 1309 * 1310 * \param encoder An encoder instance to query. 1311 * \assert 1312 * \code encoder != NULL \endcode 1313 * \retval uint32_t 1314 * See FLAC__stream_encoder_set_channels(). 1315 */ 1316 FLAC_API uint32_t FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder); 1317 1318 /** Get the input sample resolution setting. 1319 * 1320 * \param encoder An encoder instance to query. 1321 * \assert 1322 * \code encoder != NULL \endcode 1323 * \retval uint32_t 1324 * See FLAC__stream_encoder_set_bits_per_sample(). 1325 */ 1326 FLAC_API uint32_t FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder); 1327 1328 /** Get the input sample rate setting. 1329 * 1330 * \param encoder An encoder instance to query. 1331 * \assert 1332 * \code encoder != NULL \endcode 1333 * \retval uint32_t 1334 * See FLAC__stream_encoder_set_sample_rate(). 1335 */ 1336 FLAC_API uint32_t FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder); 1337 1338 /** Get the blocksize setting. 1339 * 1340 * \param encoder An encoder instance to query. 1341 * \assert 1342 * \code encoder != NULL \endcode 1343 * \retval uint32_t 1344 * See FLAC__stream_encoder_set_blocksize(). 1345 */ 1346 FLAC_API uint32_t FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder); 1347 1348 /** Get the "mid/side stereo coding" flag. 1349 * 1350 * \param encoder An encoder instance to query. 1351 * \assert 1352 * \code encoder != NULL \endcode 1353 * \retval FLAC__bool 1354 * See FLAC__stream_encoder_get_do_mid_side_stereo(). 1355 */ 1356 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder); 1357 1358 /** Get the "adaptive mid/side switching" flag. 1359 * 1360 * \param encoder An encoder instance to query. 1361 * \assert 1362 * \code encoder != NULL \endcode 1363 * \retval FLAC__bool 1364 * See FLAC__stream_encoder_set_loose_mid_side_stereo(). 1365 */ 1366 FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder); 1367 1368 /** Get the maximum LPC order setting. 1369 * 1370 * \param encoder An encoder instance to query. 1371 * \assert 1372 * \code encoder != NULL \endcode 1373 * \retval uint32_t 1374 * See FLAC__stream_encoder_set_max_lpc_order(). 1375 */ 1376 FLAC_API uint32_t FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder); 1377 1378 /** Get the quantized linear predictor coefficient precision setting. 1379 * 1380 * \param encoder An encoder instance to query. 1381 * \assert 1382 * \code encoder != NULL \endcode 1383 * \retval uint32_t 1384 * See FLAC__stream_encoder_set_qlp_coeff_precision(). 1385 */ 1386 FLAC_API uint32_t FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder); 1387 1388 /** Get the qlp coefficient precision search flag. 1389 * 1390 * \param encoder An encoder instance to query. 1391 * \assert 1392 * \code encoder != NULL \endcode 1393 * \retval FLAC__bool 1394 * See FLAC__stream_encoder_set_do_qlp_coeff_prec_search(). 1395 */ 1396 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder); 1397 1398 /** Get the "escape coding" flag. 1399 * 1400 * \param encoder An encoder instance to query. 1401 * \assert 1402 * \code encoder != NULL \endcode 1403 * \retval FLAC__bool 1404 * See FLAC__stream_encoder_set_do_escape_coding(). 1405 */ 1406 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder); 1407 1408 /** Get the exhaustive model search flag. 1409 * 1410 * \param encoder An encoder instance to query. 1411 * \assert 1412 * \code encoder != NULL \endcode 1413 * \retval FLAC__bool 1414 * See FLAC__stream_encoder_set_do_exhaustive_model_search(). 1415 */ 1416 FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder); 1417 1418 /** Get the minimum residual partition order setting. 1419 * 1420 * \param encoder An encoder instance to query. 1421 * \assert 1422 * \code encoder != NULL \endcode 1423 * \retval uint32_t 1424 * See FLAC__stream_encoder_set_min_residual_partition_order(). 1425 */ 1426 FLAC_API uint32_t FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder); 1427 1428 /** Get maximum residual partition order setting. 1429 * 1430 * \param encoder An encoder instance to query. 1431 * \assert 1432 * \code encoder != NULL \endcode 1433 * \retval uint32_t 1434 * See FLAC__stream_encoder_set_max_residual_partition_order(). 1435 */ 1436 FLAC_API uint32_t FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder); 1437 1438 /** Get the Rice parameter search distance setting. 1439 * 1440 * \param encoder An encoder instance to query. 1441 * \assert 1442 * \code encoder != NULL \endcode 1443 * \retval uint32_t 1444 * See FLAC__stream_encoder_set_rice_parameter_search_dist(). 1445 */ 1446 FLAC_API uint32_t FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder); 1447 1448 /** Get the previously set estimate of the total samples to be encoded. 1449 * The encoder merely mimics back the value given to 1450 * FLAC__stream_encoder_set_total_samples_estimate() since it has no 1451 * other way of knowing how many samples the client will encode. 1452 * 1453 * \param encoder An encoder instance to set. 1454 * \assert 1455 * \code encoder != NULL \endcode 1456 * \retval FLAC__uint64 1457 * See FLAC__stream_encoder_get_total_samples_estimate(). 1458 */ 1459 FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder); 1460 1461 /** Get the "limit_min_bitrate" flag. 1462 * 1463 * \param encoder An encoder instance to query. 1464 * \assert 1465 * \code encoder != NULL \endcode 1466 * \retval FLAC__bool 1467 * See FLAC__stream_encoder_set_limit_min_bitrate(). 1468 */ 1469 FLAC_API FLAC__bool FLAC__stream_encoder_get_limit_min_bitrate(const FLAC__StreamEncoder *encoder); 1470 1471 /** Initialize the encoder instance to encode native FLAC streams. 1472 * 1473 * This flavor of initialization sets up the encoder to encode to a 1474 * native FLAC stream. I/O is performed via callbacks to the client. 1475 * For encoding to a plain file via filename or open \c FILE*, 1476 * FLAC__stream_encoder_init_file() and FLAC__stream_encoder_init_FILE() 1477 * provide a simpler interface. 1478 * 1479 * This function should be called after FLAC__stream_encoder_new() and 1480 * FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process() 1481 * or FLAC__stream_encoder_process_interleaved(). 1482 * initialization succeeded. 1483 * 1484 * The call to FLAC__stream_encoder_init_stream() currently will also 1485 * immediately call the write callback several times, once with the \c fLaC 1486 * signature, and once for each encoded metadata block. 1487 * 1488 * \param encoder An uninitialized encoder instance. 1489 * \param write_callback See FLAC__StreamEncoderWriteCallback. This 1490 * pointer must not be \c NULL. 1491 * \param seek_callback See FLAC__StreamEncoderSeekCallback. This 1492 * pointer may be \c NULL if seeking is not 1493 * supported. The encoder uses seeking to go back 1494 * and write some some stream statistics to the 1495 * STREAMINFO block; this is recommended but not 1496 * necessary to create a valid FLAC stream. If 1497 * \a seek_callback is not \c NULL then a 1498 * \a tell_callback must also be supplied. 1499 * Alternatively, a dummy seek callback that just 1500 * returns \c FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED 1501 * may also be supplied, all though this is slightly 1502 * less efficient for the encoder. 1503 * \param tell_callback See FLAC__StreamEncoderTellCallback. This 1504 * pointer may be \c NULL if seeking is not 1505 * supported. If \a seek_callback is \c NULL then 1506 * this argument will be ignored. If 1507 * \a seek_callback is not \c NULL then a 1508 * \a tell_callback must also be supplied. 1509 * Alternatively, a dummy tell callback that just 1510 * returns \c FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED 1511 * may also be supplied, all though this is slightly 1512 * less efficient for the encoder. 1513 * \param metadata_callback See FLAC__StreamEncoderMetadataCallback. This 1514 * pointer may be \c NULL if the callback is not 1515 * desired. If the client provides a seek callback, 1516 * this function is not necessary as the encoder 1517 * will automatically seek back and update the 1518 * STREAMINFO block. It may also be \c NULL if the 1519 * client does not support seeking, since it will 1520 * have no way of going back to update the 1521 * STREAMINFO. However the client can still supply 1522 * a callback if it would like to know the details 1523 * from the STREAMINFO. 1524 * \param client_data This value will be supplied to callbacks in their 1525 * \a client_data argument. 1526 * \assert 1527 * \code encoder != NULL \endcode 1528 * \retval FLAC__StreamEncoderInitStatus 1529 * \c FLAC__STREAM_ENCODER_INIT_STATUS_OK if initialization was successful; 1530 * see FLAC__StreamEncoderInitStatus for the meanings of other return values. 1531 */ 1532 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_stream(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback write_callback, FLAC__StreamEncoderSeekCallback seek_callback, FLAC__StreamEncoderTellCallback tell_callback, FLAC__StreamEncoderMetadataCallback metadata_callback, void *client_data); 1533 1534 /** Initialize the encoder instance to encode Ogg FLAC streams. 1535 * 1536 * This flavor of initialization sets up the encoder to encode to a FLAC 1537 * stream in an Ogg container. I/O is performed via callbacks to the 1538 * client. For encoding to a plain file via filename or open \c FILE*, 1539 * FLAC__stream_encoder_init_ogg_file() and FLAC__stream_encoder_init_ogg_FILE() 1540 * provide a simpler interface. 1541 * 1542 * This function should be called after FLAC__stream_encoder_new() and 1543 * FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process() 1544 * or FLAC__stream_encoder_process_interleaved(). 1545 * initialization succeeded. 1546 * 1547 * The call to FLAC__stream_encoder_init_ogg_stream() currently will also 1548 * immediately call the write callback several times to write the metadata 1549 * packets. 1550 * 1551 * \param encoder An uninitialized encoder instance. 1552 * \param read_callback See FLAC__StreamEncoderReadCallback. This 1553 * pointer must not be \c NULL if \a seek_callback 1554 * is non-NULL since they are both needed to be 1555 * able to write data back to the Ogg FLAC stream 1556 * in the post-encode phase. 1557 * \param write_callback See FLAC__StreamEncoderWriteCallback. This 1558 * pointer must not be \c NULL. 1559 * \param seek_callback See FLAC__StreamEncoderSeekCallback. This 1560 * pointer may be \c NULL if seeking is not 1561 * supported. The encoder uses seeking to go back 1562 * and write some some stream statistics to the 1563 * STREAMINFO block; this is recommended but not 1564 * necessary to create a valid FLAC stream. If 1565 * \a seek_callback is not \c NULL then a 1566 * \a tell_callback must also be supplied. 1567 * Alternatively, a dummy seek callback that just 1568 * returns \c FLAC__STREAM_ENCODER_SEEK_STATUS_UNSUPPORTED 1569 * may also be supplied, all though this is slightly 1570 * less efficient for the encoder. 1571 * \param tell_callback See FLAC__StreamEncoderTellCallback. This 1572 * pointer may be \c NULL if seeking is not 1573 * supported. If \a seek_callback is \c NULL then 1574 * this argument will be ignored. If 1575 * \a seek_callback is not \c NULL then a 1576 * \a tell_callback must also be supplied. 1577 * Alternatively, a dummy tell callback that just 1578 * returns \c FLAC__STREAM_ENCODER_TELL_STATUS_UNSUPPORTED 1579 * may also be supplied, all though this is slightly 1580 * less efficient for the encoder. 1581 * \param metadata_callback See FLAC__StreamEncoderMetadataCallback. This 1582 * pointer may be \c NULL if the callback is not 1583 * desired. If the client provides a seek callback, 1584 * this function is not necessary as the encoder 1585 * will automatically seek back and update the 1586 * STREAMINFO block. It may also be \c NULL if the 1587 * client does not support seeking, since it will 1588 * have no way of going back to update the 1589 * STREAMINFO. However the client can still supply 1590 * a callback if it would like to know the details 1591 * from the STREAMINFO. 1592 * \param client_data This value will be supplied to callbacks in their 1593 * \a client_data argument. 1594 * \assert 1595 * \code encoder != NULL \endcode 1596 * \retval FLAC__StreamEncoderInitStatus 1597 * \c FLAC__STREAM_ENCODER_INIT_STATUS_OK if initialization was successful; 1598 * see FLAC__StreamEncoderInitStatus for the meanings of other return values. 1599 */ 1600 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_stream(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderReadCallback read_callback, FLAC__StreamEncoderWriteCallback write_callback, FLAC__StreamEncoderSeekCallback seek_callback, FLAC__StreamEncoderTellCallback tell_callback, FLAC__StreamEncoderMetadataCallback metadata_callback, void *client_data); 1601 1602 /** Initialize the encoder instance to encode native FLAC files. 1603 * 1604 * This flavor of initialization sets up the encoder to encode to a 1605 * plain native FLAC file. For non-stdio streams, you must use 1606 * FLAC__stream_encoder_init_stream() and provide callbacks for the I/O. 1607 * 1608 * This function should be called after FLAC__stream_encoder_new() and 1609 * FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process() 1610 * or FLAC__stream_encoder_process_interleaved(). 1611 * initialization succeeded. 1612 * 1613 * \param encoder An uninitialized encoder instance. 1614 * \param file An open file. The file should have been opened 1615 * with mode \c "w+b" and rewound. The file 1616 * becomes owned by the encoder and should not be 1617 * manipulated by the client while encoding. 1618 * Unless \a file is \c stdout, it will be closed 1619 * when FLAC__stream_encoder_finish() is called. 1620 * Note however that a proper SEEKTABLE cannot be 1621 * created when encoding to \c stdout since it is 1622 * not seekable. 1623 * \param progress_callback See FLAC__StreamEncoderProgressCallback. This 1624 * pointer may be \c NULL if the callback is not 1625 * desired. 1626 * \param client_data This value will be supplied to callbacks in their 1627 * \a client_data argument. 1628 * \assert 1629 * \code encoder != NULL \endcode 1630 * \code file != NULL \endcode 1631 * \retval FLAC__StreamEncoderInitStatus 1632 * \c FLAC__STREAM_ENCODER_INIT_STATUS_OK if initialization was successful; 1633 * see FLAC__StreamEncoderInitStatus for the meanings of other return values. 1634 */ 1635 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(FLAC__StreamEncoder *encoder, FILE *file, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data); 1636 1637 /** Initialize the encoder instance to encode Ogg FLAC files. 1638 * 1639 * This flavor of initialization sets up the encoder to encode to a 1640 * plain Ogg FLAC file. For non-stdio streams, you must use 1641 * FLAC__stream_encoder_init_ogg_stream() and provide callbacks for the I/O. 1642 * 1643 * This function should be called after FLAC__stream_encoder_new() and 1644 * FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process() 1645 * or FLAC__stream_encoder_process_interleaved(). 1646 * initialization succeeded. 1647 * 1648 * \param encoder An uninitialized encoder instance. 1649 * \param file An open file. The file should have been opened 1650 * with mode \c "w+b" and rewound. The file 1651 * becomes owned by the encoder and should not be 1652 * manipulated by the client while encoding. 1653 * Unless \a file is \c stdout, it will be closed 1654 * when FLAC__stream_encoder_finish() is called. 1655 * Note however that a proper SEEKTABLE cannot be 1656 * created when encoding to \c stdout since it is 1657 * not seekable. 1658 * \param progress_callback See FLAC__StreamEncoderProgressCallback. This 1659 * pointer may be \c NULL if the callback is not 1660 * desired. 1661 * \param client_data This value will be supplied to callbacks in their 1662 * \a client_data argument. 1663 * \assert 1664 * \code encoder != NULL \endcode 1665 * \code file != NULL \endcode 1666 * \retval FLAC__StreamEncoderInitStatus 1667 * \c FLAC__STREAM_ENCODER_INIT_STATUS_OK if initialization was successful; 1668 * see FLAC__StreamEncoderInitStatus for the meanings of other return values. 1669 */ 1670 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(FLAC__StreamEncoder *encoder, FILE *file, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data); 1671 1672 /** Initialize the encoder instance to encode native FLAC files. 1673 * 1674 * This flavor of initialization sets up the encoder to encode to a plain 1675 * FLAC file. If POSIX fopen() semantics are not sufficient you must use 1676 * FLAC__stream_encoder_init_FILE(), or FLAC__stream_encoder_init_stream() 1677 * and provide callbacks for the I/O. 1678 * 1679 * On Windows, filename must be a UTF-8 encoded filename, which libFLAC 1680 * internally translates to an appropriate representation to use with 1681 * _wfopen. On all other systems, filename is passed to fopen without 1682 * any translation. 1683 * 1684 * This function should be called after FLAC__stream_encoder_new() and 1685 * FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process() 1686 * or FLAC__stream_encoder_process_interleaved(). 1687 * initialization succeeded. 1688 * 1689 * \param encoder An uninitialized encoder instance. 1690 * \param filename The name of the file to encode to. The file will 1691 * be opened with fopen(). Use \c NULL to encode to 1692 * \c stdout. Note however that a proper SEEKTABLE 1693 * cannot be created when encoding to \c stdout since 1694 * it is not seekable. 1695 * \param progress_callback See FLAC__StreamEncoderProgressCallback. This 1696 * pointer may be \c NULL if the callback is not 1697 * desired. 1698 * \param client_data This value will be supplied to callbacks in their 1699 * \a client_data argument. 1700 * \assert 1701 * \code encoder != NULL \endcode 1702 * \retval FLAC__StreamEncoderInitStatus 1703 * \c FLAC__STREAM_ENCODER_INIT_STATUS_OK if initialization was successful; 1704 * see FLAC__StreamEncoderInitStatus for the meanings of other return values. 1705 */ 1706 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_file(FLAC__StreamEncoder *encoder, const char *filename, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data); 1707 1708 /** Initialize the encoder instance to encode Ogg FLAC files. 1709 * 1710 * This flavor of initialization sets up the encoder to encode to a plain 1711 * Ogg FLAC file. If POSIX fopen() semantics are not sufficient, you must use 1712 * FLAC__stream_encoder_init_ogg_FILE(), or FLAC__stream_encoder_init_ogg_stream() 1713 * and provide callbacks for the I/O. 1714 * 1715 * On Windows, filename must be a UTF-8 encoded filename, which libFLAC 1716 * internally translates to an appropriate representation to use with 1717 * _wfopen. On all other systems, filename is passed to fopen without 1718 * any translation. 1719 * 1720 * This function should be called after FLAC__stream_encoder_new() and 1721 * FLAC__stream_encoder_set_*() but before FLAC__stream_encoder_process() 1722 * or FLAC__stream_encoder_process_interleaved(). 1723 * initialization succeeded. 1724 * 1725 * \param encoder An uninitialized encoder instance. 1726 * \param filename The name of the file to encode to. The file will 1727 * be opened with fopen(). Use \c NULL to encode to 1728 * \c stdout. Note however that a proper SEEKTABLE 1729 * cannot be created when encoding to \c stdout since 1730 * it is not seekable. 1731 * \param progress_callback See FLAC__StreamEncoderProgressCallback. This 1732 * pointer may be \c NULL if the callback is not 1733 * desired. 1734 * \param client_data This value will be supplied to callbacks in their 1735 * \a client_data argument. 1736 * \assert 1737 * \code encoder != NULL \endcode 1738 * \retval FLAC__StreamEncoderInitStatus 1739 * \c FLAC__STREAM_ENCODER_INIT_STATUS_OK if initialization was successful; 1740 * see FLAC__StreamEncoderInitStatus for the meanings of other return values. 1741 */ 1742 FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_file(FLAC__StreamEncoder *encoder, const char *filename, FLAC__StreamEncoderProgressCallback progress_callback, void *client_data); 1743 1744 /** Finish the encoding process. 1745 * Flushes the encoding buffer, releases resources, resets the encoder 1746 * settings to their defaults, and returns the encoder state to 1747 * FLAC__STREAM_ENCODER_UNINITIALIZED. Note that this can generate 1748 * one or more write callbacks before returning, and will generate 1749 * a metadata callback. 1750 * 1751 * Note that in the course of processing the last frame, errors can 1752 * occur, so the caller should be sure to check the return value to 1753 * ensure the file was encoded properly. 1754 * 1755 * In the event of a prematurely-terminated encode, it is not strictly 1756 * necessary to call this immediately before FLAC__stream_encoder_delete() 1757 * but it is good practice to match every FLAC__stream_encoder_init_*() 1758 * with a FLAC__stream_encoder_finish(). 1759 * 1760 * \param encoder An uninitialized encoder instance. 1761 * \assert 1762 * \code encoder != NULL \endcode 1763 * \retval FLAC__bool 1764 * \c false if an error occurred processing the last frame; or if verify 1765 * mode is set (see FLAC__stream_encoder_set_verify()), there was a 1766 * verify mismatch; else \c true. If \c false, caller should check the 1767 * state with FLAC__stream_encoder_get_state() for more information 1768 * about the error. 1769 */ 1770 FLAC_API FLAC__bool FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder); 1771 1772 /** Submit data for encoding. 1773 * This version allows you to supply the input data via an array of 1774 * pointers, each pointer pointing to an array of \a samples samples 1775 * representing one channel. The samples need not be block-aligned, 1776 * but each channel should have the same number of samples. Each sample 1777 * should be a signed integer, right-justified to the resolution set by 1778 * FLAC__stream_encoder_set_bits_per_sample(). For example, if the 1779 * resolution is 16 bits per sample, the samples should all be in the 1780 * range [-32768,32767]. 1781 * 1782 * For applications where channel order is important, channels must 1783 * follow the order as described in the 1784 * <A HREF="https://xiph.org/flac/format.html#frame_header">frame header</A>. 1785 * 1786 * \param encoder An initialized encoder instance in the OK state. 1787 * \param buffer An array of pointers to each channel's signal. 1788 * \param samples The number of samples in one channel. 1789 * \assert 1790 * \code encoder != NULL \endcode 1791 * \code FLAC__stream_encoder_get_state(encoder) == FLAC__STREAM_ENCODER_OK \endcode 1792 * \retval FLAC__bool 1793 * \c true if successful, else \c false; in this case, check the 1794 * encoder state with FLAC__stream_encoder_get_state() to see what 1795 * went wrong. 1796 */ 1797 FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], uint32_t samples); 1798 1799 /** Submit data for encoding. 1800 * This version allows you to supply the input data where the channels 1801 * are interleaved into a single array (i.e. channel0_sample0, 1802 * channel1_sample0, ... , channelN_sample0, channel0_sample1, ...). 1803 * The samples need not be block-aligned but they must be 1804 * sample-aligned, i.e. the first value should be channel0_sample0 1805 * and the last value channelN_sampleM. Each sample should be a signed 1806 * integer, right-justified to the resolution set by 1807 * FLAC__stream_encoder_set_bits_per_sample(). For example, if the 1808 * resolution is 16 bits per sample, the samples should all be in the 1809 * range [-32768,32767]. 1810 * 1811 * For applications where channel order is important, channels must 1812 * follow the order as described in the 1813 * <A HREF="https://xiph.org/flac/format.html#frame_header">frame header</A>. 1814 * 1815 * \param encoder An initialized encoder instance in the OK state. 1816 * \param buffer An array of channel-interleaved data (see above). 1817 * \param samples The number of samples in one channel, the same as for 1818 * FLAC__stream_encoder_process(). For example, if 1819 * encoding two channels, \c 1000 \a samples corresponds 1820 * to a \a buffer of 2000 values. 1821 * \assert 1822 * \code encoder != NULL \endcode 1823 * \code FLAC__stream_encoder_get_state(encoder) == FLAC__STREAM_ENCODER_OK \endcode 1824 * \retval FLAC__bool 1825 * \c true if successful, else \c false; in this case, check the 1826 * encoder state with FLAC__stream_encoder_get_state() to see what 1827 * went wrong. 1828 */ 1829 FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], uint32_t samples); 1830 1831 /* \} */ 1832 1833 #ifdef __cplusplus 1834 } 1835 #endif 1836 1837 #endif 1838