stream_encoder.c
资源名称:tcpmp.rar [点击查看]
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:130k
源码类别:
Windows CE
开发平台:
C/C++
- /* libFLAC - Free Lossless Audio Codec library
- * Copyright (C) 2000,2001,2002,2003,2004,2005 Josh Coalson
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of the Xiph.org Foundation nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <limits.h>
- #include <stdio.h>
- #include <stdlib.h> /* for malloc() */
- #include <string.h> /* for memcpy() */
- #include "FLAC/assert.h"
- #include "FLAC/stream_decoder.h"
- #include "protected/stream_encoder.h"
- #include "private/bitbuffer.h"
- #include "private/bitmath.h"
- #include "private/crc.h"
- #include "private/cpu.h"
- #include "private/fixed.h"
- #include "private/format.h"
- #include "private/lpc.h"
- #include "private/md5.h"
- #include "private/memory.h"
- #include "private/stream_encoder_framing.h"
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #ifdef min
- #undef min
- #endif
- #define min(x,y) ((x)<(y)?(x):(y))
- #ifdef max
- #undef max
- #endif
- #define max(x,y) ((x)>(y)?(x):(y))
- typedef struct {
- FLAC__int32 *data[FLAC__MAX_CHANNELS];
- unsigned size; /* of each data[] in samples */
- unsigned tail;
- } verify_input_fifo;
- typedef struct {
- const FLAC__byte *data;
- unsigned capacity;
- unsigned bytes;
- } verify_output;
- typedef enum {
- ENCODER_IN_MAGIC = 0,
- ENCODER_IN_METADATA = 1,
- ENCODER_IN_AUDIO = 2
- } EncoderStateHint;
- /***********************************************************************
- *
- * Private class method prototypes
- *
- ***********************************************************************/
- static void set_defaults_(FLAC__StreamEncoder *encoder);
- static void free_(FLAC__StreamEncoder *encoder);
- static FLAC__bool resize_buffers_(FLAC__StreamEncoder *encoder, unsigned new_size);
- static FLAC__bool write_bitbuffer_(FLAC__StreamEncoder *encoder, unsigned samples);
- static FLAC__bool process_frame_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
- static FLAC__bool process_subframes_(FLAC__StreamEncoder *encoder, FLAC__bool is_last_frame);
- static FLAC__bool process_subframe_(
- FLAC__StreamEncoder *encoder,
- unsigned min_partition_order,
- unsigned max_partition_order,
- FLAC__bool precompute_partition_sums,
- const FLAC__FrameHeader *frame_header,
- unsigned subframe_bps,
- const FLAC__int32 integer_signal[],
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- const FLAC__real real_signal[],
- #endif
- FLAC__Subframe *subframe[2],
- FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents[2],
- FLAC__int32 *residual[2],
- unsigned *best_subframe,
- unsigned *best_bits
- );
- static FLAC__bool add_subframe_(
- FLAC__StreamEncoder *encoder,
- const FLAC__FrameHeader *frame_header,
- unsigned subframe_bps,
- const FLAC__Subframe *subframe,
- FLAC__BitBuffer *frame
- );
- static unsigned evaluate_constant_subframe_(
- const FLAC__int32 signal,
- unsigned subframe_bps,
- FLAC__Subframe *subframe
- );
- static unsigned evaluate_fixed_subframe_(
- FLAC__StreamEncoder *encoder,
- const FLAC__int32 signal[],
- FLAC__int32 residual[],
- FLAC__uint32 abs_residual[],
- FLAC__uint64 abs_residual_partition_sums[],
- unsigned raw_bits_per_partition[],
- unsigned blocksize,
- unsigned subframe_bps,
- unsigned order,
- unsigned rice_parameter,
- unsigned min_partition_order,
- unsigned max_partition_order,
- FLAC__bool precompute_partition_sums,
- FLAC__bool do_escape_coding,
- unsigned rice_parameter_search_dist,
- FLAC__Subframe *subframe,
- FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
- );
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- static unsigned evaluate_lpc_subframe_(
- FLAC__StreamEncoder *encoder,
- const FLAC__int32 signal[],
- FLAC__int32 residual[],
- FLAC__uint32 abs_residual[],
- FLAC__uint64 abs_residual_partition_sums[],
- unsigned raw_bits_per_partition[],
- const FLAC__real lp_coeff[],
- unsigned blocksize,
- unsigned subframe_bps,
- unsigned order,
- unsigned qlp_coeff_precision,
- unsigned rice_parameter,
- unsigned min_partition_order,
- unsigned max_partition_order,
- FLAC__bool precompute_partition_sums,
- FLAC__bool do_escape_coding,
- unsigned rice_parameter_search_dist,
- FLAC__Subframe *subframe,
- FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents
- );
- #endif
- static unsigned evaluate_verbatim_subframe_(
- const FLAC__int32 signal[],
- unsigned blocksize,
- unsigned subframe_bps,
- FLAC__Subframe *subframe
- );
- static unsigned find_best_partition_order_(
- struct FLAC__StreamEncoderPrivate *private_,
- const FLAC__int32 residual[],
- FLAC__uint32 abs_residual[],
- FLAC__uint64 abs_residual_partition_sums[],
- unsigned raw_bits_per_partition[],
- unsigned residual_samples,
- unsigned predictor_order,
- unsigned rice_parameter,
- unsigned min_partition_order,
- unsigned max_partition_order,
- FLAC__bool precompute_partition_sums,
- FLAC__bool do_escape_coding,
- unsigned rice_parameter_search_dist,
- FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
- );
- static void precompute_partition_info_sums_(
- const FLAC__uint32 abs_residual[],
- FLAC__uint64 abs_residual_partition_sums[],
- unsigned residual_samples,
- unsigned predictor_order,
- unsigned min_partition_order,
- unsigned max_partition_order
- );
- static void precompute_partition_info_escapes_(
- const FLAC__int32 residual[],
- unsigned raw_bits_per_partition[],
- unsigned residual_samples,
- unsigned predictor_order,
- unsigned min_partition_order,
- unsigned max_partition_order
- );
- #ifdef DONT_ESTIMATE_RICE_BITS
- static FLAC__bool set_partitioned_rice_(
- const FLAC__uint32 abs_residual[],
- const FLAC__int32 residual[],
- const unsigned residual_samples,
- const unsigned predictor_order,
- const unsigned suggested_rice_parameter,
- const unsigned rice_parameter_search_dist,
- const unsigned partition_order,
- FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
- unsigned *bits
- );
- static FLAC__bool set_partitioned_rice_with_precompute_(
- const FLAC__int32 residual[],
- const FLAC__uint64 abs_residual_partition_sums[],
- const unsigned raw_bits_per_partition[],
- const unsigned residual_samples,
- const unsigned predictor_order,
- const unsigned suggested_rice_parameter,
- const unsigned rice_parameter_search_dist,
- const unsigned partition_order,
- const FLAC__bool search_for_escapes,
- FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
- unsigned *bits
- );
- #else
- static FLAC__bool set_partitioned_rice_(
- const FLAC__uint32 abs_residual[],
- const unsigned residual_samples,
- const unsigned predictor_order,
- const unsigned suggested_rice_parameter,
- const unsigned rice_parameter_search_dist,
- const unsigned partition_order,
- FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
- unsigned *bits
- );
- static FLAC__bool set_partitioned_rice_with_precompute_(
- const FLAC__uint32 abs_residual[],
- const FLAC__uint64 abs_residual_partition_sums[],
- const unsigned raw_bits_per_partition[],
- const unsigned residual_samples,
- const unsigned predictor_order,
- const unsigned suggested_rice_parameter,
- const unsigned rice_parameter_search_dist,
- const unsigned partition_order,
- const FLAC__bool search_for_escapes,
- FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents,
- unsigned *bits
- );
- #endif
- static unsigned get_wasted_bits_(FLAC__int32 signal[], unsigned samples);
- /* verify-related routines: */
- static void append_to_verify_fifo_(
- verify_input_fifo *fifo,
- const FLAC__int32 * const input[],
- unsigned input_offset,
- unsigned channels,
- unsigned wide_samples
- );
- static void append_to_verify_fifo_interleaved_(
- verify_input_fifo *fifo,
- const FLAC__int32 input[],
- unsigned input_offset,
- unsigned channels,
- unsigned wide_samples
- );
- static FLAC__StreamDecoderReadStatus verify_read_callback_(
- const FLAC__StreamDecoder *decoder,
- FLAC__byte buffer[],
- unsigned *bytes,
- void *client_data
- );
- static FLAC__StreamDecoderWriteStatus verify_write_callback_(
- const FLAC__StreamDecoder *decoder,
- const FLAC__Frame *frame,
- const FLAC__int32 * const buffer[],
- void *client_data
- );
- static void verify_metadata_callback_(
- const FLAC__StreamDecoder *decoder,
- const FLAC__StreamMetadata *metadata,
- void *client_data
- );
- static void verify_error_callback_(
- const FLAC__StreamDecoder *decoder,
- FLAC__StreamDecoderErrorStatus status,
- void *client_data
- );
- /***********************************************************************
- *
- * Private class data
- *
- ***********************************************************************/
- typedef struct FLAC__StreamEncoderPrivate {
- unsigned input_capacity; /* current size (in samples) of the signal and residual buffers */
- FLAC__int32 *integer_signal[FLAC__MAX_CHANNELS]; /* the integer version of the input signal */
- FLAC__int32 *integer_signal_mid_side[2]; /* the integer version of the mid-side input signal (stereo only) */
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* the floating-point version of the input signal */
- FLAC__real *real_signal_mid_side[2]; /* the floating-point version of the mid-side input signal (stereo only) */
- #endif
- unsigned subframe_bps[FLAC__MAX_CHANNELS]; /* the effective bits per sample of the input signal (stream bps - wasted bits) */
- unsigned subframe_bps_mid_side[2]; /* the effective bits per sample of the mid-side input signal (stream bps - wasted bits + 0/1) */
- FLAC__int32 *residual_workspace[FLAC__MAX_CHANNELS][2]; /* each channel has a candidate and best workspace where the subframe residual signals will be stored */
- FLAC__int32 *residual_workspace_mid_side[2][2];
- FLAC__Subframe subframe_workspace[FLAC__MAX_CHANNELS][2];
- FLAC__Subframe subframe_workspace_mid_side[2][2];
- FLAC__Subframe *subframe_workspace_ptr[FLAC__MAX_CHANNELS][2];
- FLAC__Subframe *subframe_workspace_ptr_mid_side[2][2];
- FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace[FLAC__MAX_CHANNELS][2];
- FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_workspace_mid_side[FLAC__MAX_CHANNELS][2];
- FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr[FLAC__MAX_CHANNELS][2];
- FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents_workspace_ptr_mid_side[FLAC__MAX_CHANNELS][2];
- unsigned best_subframe[FLAC__MAX_CHANNELS]; /* index into the above workspaces */
- unsigned best_subframe_mid_side[2];
- unsigned best_subframe_bits[FLAC__MAX_CHANNELS]; /* size in bits of the best subframe for each channel */
- unsigned best_subframe_bits_mid_side[2];
- FLAC__uint32 *abs_residual; /* workspace where abs(candidate residual) is stored */
- FLAC__uint64 *abs_residual_partition_sums; /* workspace where the sum of abs(candidate residual) for each partition is stored */
- unsigned *raw_bits_per_partition; /* workspace where the sum of silog2(candidate residual) for each partition is stored */
- FLAC__BitBuffer *frame; /* the current frame being worked on */
- unsigned loose_mid_side_stereo_frames; /* rounded number of frames the encoder will use before trying both independent and mid/side frames again */
- unsigned loose_mid_side_stereo_frame_count; /* number of frames using the current channel assignment */
- FLAC__ChannelAssignment last_channel_assignment;
- FLAC__StreamMetadata metadata;
- unsigned current_sample_number;
- unsigned current_frame_number;
- struct FLAC__MD5Context md5context;
- FLAC__CPUInfo cpuinfo;
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__float residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
- #else
- unsigned (*local_fixed_compute_best_predictor)(const FLAC__int32 data[], unsigned data_len, FLAC__fixedpoint residual_bits_per_sample[FLAC__MAX_FIXED_ORDER+1]);
- #endif
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- void (*local_lpc_compute_autocorrelation)(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
- void (*local_lpc_compute_residual_from_qlp_coefficients)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
- void (*local_lpc_compute_residual_from_qlp_coefficients_64bit)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
- void (*local_lpc_compute_residual_from_qlp_coefficients_16bit)(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
- #endif
- FLAC__bool use_wide_by_block; /* use slow 64-bit versions of some functions because of the block size */
- FLAC__bool use_wide_by_partition; /* use slow 64-bit versions of some functions because of the min partition order and blocksize */
- FLAC__bool use_wide_by_order; /* use slow 64-bit versions of some functions because of the lpc order */
- FLAC__bool precompute_partition_sums; /* our initial guess as to whether precomputing the partitions sums will be a speed improvement */
- FLAC__bool disable_constant_subframes;
- FLAC__bool disable_fixed_subframes;
- FLAC__bool disable_verbatim_subframes;
- FLAC__StreamEncoderWriteCallback write_callback;
- FLAC__StreamEncoderMetadataCallback metadata_callback;
- void *client_data;
- /* unaligned (original) pointers to allocated data */
- FLAC__int32 *integer_signal_unaligned[FLAC__MAX_CHANNELS];
- FLAC__int32 *integer_signal_mid_side_unaligned[2];
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS];
- FLAC__real *real_signal_mid_side_unaligned[2];
- #endif
- FLAC__int32 *residual_workspace_unaligned[FLAC__MAX_CHANNELS][2];
- FLAC__int32 *residual_workspace_mid_side_unaligned[2][2];
- FLAC__uint32 *abs_residual_unaligned;
- FLAC__uint64 *abs_residual_partition_sums_unaligned;
- unsigned *raw_bits_per_partition_unaligned;
- /*
- * These fields have been moved here from private function local
- * declarations merely to save stack space during encoding.
- */
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- FLAC__real lp_coeff[FLAC__MAX_LPC_ORDER][FLAC__MAX_LPC_ORDER]; /* from process_subframe_() */
- #endif
- FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents_extra[2]; /* from find_best_partition_order_() */
- /*
- * The data for the verify section
- */
- struct {
- FLAC__StreamDecoder *decoder;
- EncoderStateHint state_hint;
- FLAC__bool needs_magic_hack;
- verify_input_fifo input_fifo;
- verify_output output;
- struct {
- FLAC__uint64 absolute_sample;
- unsigned frame_number;
- unsigned channel;
- unsigned sample;
- FLAC__int32 expected;
- FLAC__int32 got;
- } error_stats;
- } verify;
- FLAC__bool is_being_deleted; /* if true, call to ..._finish() from ..._delete() will not call the callbacks */
- } FLAC__StreamEncoderPrivate;
- /***********************************************************************
- *
- * Public static class data
- *
- ***********************************************************************/
- FLAC_API const char * const FLAC__StreamEncoderStateString[] = {
- "FLAC__STREAM_ENCODER_OK",
- "FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR",
- "FLAC__STREAM_ENCODER_VERIFY_MISMATCH_IN_AUDIO_DATA",
- "FLAC__STREAM_ENCODER_INVALID_CALLBACK",
- "FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS",
- "FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE",
- "FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE",
- "FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE",
- "FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER",
- "FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION",
- "FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH",
- "FLAC__STREAM_ENCODER_MID_SIDE_SAMPLE_SIZE_MISMATCH",
- "FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE",
- "FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER",
- "FLAC__STREAM_ENCODER_NOT_STREAMABLE",
- "FLAC__STREAM_ENCODER_FRAMING_ERROR",
- "FLAC__STREAM_ENCODER_INVALID_METADATA",
- "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_ENCODING",
- "FLAC__STREAM_ENCODER_FATAL_ERROR_WHILE_WRITING",
- "FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR",
- "FLAC__STREAM_ENCODER_ALREADY_INITIALIZED",
- "FLAC__STREAM_ENCODER_UNINITIALIZED"
- };
- FLAC_API const char * const FLAC__StreamEncoderWriteStatusString[] = {
- "FLAC__STREAM_ENCODER_WRITE_STATUS_OK",
- "FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR"
- };
- /***********************************************************************
- *
- * Class constructor/destructor
- *
- */
- FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
- {
- FLAC__StreamEncoder *encoder;
- unsigned i;
- FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
- encoder = (FLAC__StreamEncoder*)calloc(1, sizeof(FLAC__StreamEncoder));
- if(encoder == 0) {
- return 0;
- }
- encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
- if(encoder->protected_ == 0) {
- free(encoder);
- return 0;
- }
- encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
- if(encoder->private_ == 0) {
- free(encoder->protected_);
- free(encoder);
- return 0;
- }
- encoder->private_->frame = FLAC__bitbuffer_new();
- if(encoder->private_->frame == 0) {
- free(encoder->private_);
- free(encoder->protected_);
- free(encoder);
- return 0;
- }
- set_defaults_(encoder);
- encoder->private_->is_being_deleted = false;
- for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
- encoder->private_->subframe_workspace_ptr[i][0] = &encoder->private_->subframe_workspace[i][0];
- encoder->private_->subframe_workspace_ptr[i][1] = &encoder->private_->subframe_workspace[i][1];
- }
- for(i = 0; i < 2; i++) {
- encoder->private_->subframe_workspace_ptr_mid_side[i][0] = &encoder->private_->subframe_workspace_mid_side[i][0];
- encoder->private_->subframe_workspace_ptr_mid_side[i][1] = &encoder->private_->subframe_workspace_mid_side[i][1];
- }
- for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
- encoder->private_->partitioned_rice_contents_workspace_ptr[i][0] = &encoder->private_->partitioned_rice_contents_workspace[i][0];
- encoder->private_->partitioned_rice_contents_workspace_ptr[i][1] = &encoder->private_->partitioned_rice_contents_workspace[i][1];
- }
- for(i = 0; i < 2; i++) {
- encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][0] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0];
- encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[i][1] = &encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1];
- }
- for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
- FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
- FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
- }
- for(i = 0; i < 2; i++) {
- FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
- FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
- }
- for(i = 0; i < 2; i++)
- FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&encoder->private_->partitioned_rice_contents_extra[i]);
- encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
- return encoder;
- }
- FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
- {
- unsigned i;
- FLAC__ASSERT(0 != encoder);
- FLAC__ASSERT(0 != encoder->protected_);
- FLAC__ASSERT(0 != encoder->private_);
- FLAC__ASSERT(0 != encoder->private_->frame);
- encoder->private_->is_being_deleted = true;
- FLAC__stream_encoder_finish(encoder);
- if(0 != encoder->private_->verify.decoder)
- FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
- for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
- FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][0]);
- FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace[i][1]);
- }
- for(i = 0; i < 2; i++) {
- FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][0]);
- FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_workspace_mid_side[i][1]);
- }
- for(i = 0; i < 2; i++)
- FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&encoder->private_->partitioned_rice_contents_extra[i]);
- FLAC__bitbuffer_delete(encoder->private_->frame);
- free(encoder->private_);
- free(encoder->protected_);
- free(encoder);
- }
- /***********************************************************************
- *
- * Public class methods
- *
- ***********************************************************************/
- FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_init(FLAC__StreamEncoder *encoder)
- {
- unsigned i;
- FLAC__bool metadata_has_seektable, metadata_has_vorbis_comment;
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return encoder->protected_->state = FLAC__STREAM_ENCODER_ALREADY_INITIALIZED;
- encoder->protected_->state = FLAC__STREAM_ENCODER_OK;
- if(0 == encoder->private_->write_callback || 0 == encoder->private_->metadata_callback)
- return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_CALLBACK;
- if(encoder->protected_->channels == 0 || encoder->protected_->channels > FLAC__MAX_CHANNELS)
- return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_NUMBER_OF_CHANNELS;
- if(encoder->protected_->do_mid_side_stereo && encoder->protected_->channels != 2)
- return encoder->protected_->state = FLAC__STREAM_ENCODER_MID_SIDE_CHANNELS_MISMATCH;
- if(encoder->protected_->loose_mid_side_stereo && !encoder->protected_->do_mid_side_stereo)
- return encoder->protected_->state = FLAC__STREAM_ENCODER_ILLEGAL_MID_SIDE_FORCE;
- if(encoder->protected_->bits_per_sample >= 32)
- encoder->protected_->do_mid_side_stereo = false; /* since we do 32-bit math, the side channel would have 33 bps and overflow */
- if(encoder->protected_->bits_per_sample < FLAC__MIN_BITS_PER_SAMPLE || encoder->protected_->bits_per_sample > FLAC__REFERENCE_CODEC_MAX_BITS_PER_SAMPLE)
- return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BITS_PER_SAMPLE;
- if(!FLAC__format_sample_rate_is_valid(encoder->protected_->sample_rate))
- return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_SAMPLE_RATE;
- if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
- return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_BLOCK_SIZE;
- if(encoder->protected_->max_lpc_order > FLAC__MAX_LPC_ORDER)
- return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_MAX_LPC_ORDER;
- if(encoder->protected_->blocksize < encoder->protected_->max_lpc_order)
- return encoder->protected_->state = FLAC__STREAM_ENCODER_BLOCK_SIZE_TOO_SMALL_FOR_LPC_ORDER;
- if(encoder->protected_->qlp_coeff_precision == 0) {
- if(encoder->protected_->bits_per_sample < 16) {
- /* @@@ need some data about how to set this here w.r.t. blocksize and sample rate */
- /* @@@ until then we'll make a guess */
- encoder->protected_->qlp_coeff_precision = max(FLAC__MIN_QLP_COEFF_PRECISION, 2 + encoder->protected_->bits_per_sample / 2);
- }
- else if(encoder->protected_->bits_per_sample == 16) {
- if(encoder->protected_->blocksize <= 192)
- encoder->protected_->qlp_coeff_precision = 7;
- else if(encoder->protected_->blocksize <= 384)
- encoder->protected_->qlp_coeff_precision = 8;
- else if(encoder->protected_->blocksize <= 576)
- encoder->protected_->qlp_coeff_precision = 9;
- else if(encoder->protected_->blocksize <= 1152)
- encoder->protected_->qlp_coeff_precision = 10;
- else if(encoder->protected_->blocksize <= 2304)
- encoder->protected_->qlp_coeff_precision = 11;
- else if(encoder->protected_->blocksize <= 4608)
- encoder->protected_->qlp_coeff_precision = 12;
- else
- encoder->protected_->qlp_coeff_precision = 13;
- }
- else {
- if(encoder->protected_->blocksize <= 384)
- encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-2;
- else if(encoder->protected_->blocksize <= 1152)
- encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION-1;
- else
- encoder->protected_->qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
- }
- FLAC__ASSERT(encoder->protected_->qlp_coeff_precision <= FLAC__MAX_QLP_COEFF_PRECISION);
- }
- else if(encoder->protected_->qlp_coeff_precision < FLAC__MIN_QLP_COEFF_PRECISION || encoder->protected_->qlp_coeff_precision > FLAC__MAX_QLP_COEFF_PRECISION)
- return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_QLP_COEFF_PRECISION;
- if(encoder->protected_->streamable_subset) {
- if(
- encoder->protected_->blocksize != 192 &&
- encoder->protected_->blocksize != 576 &&
- encoder->protected_->blocksize != 1152 &&
- encoder->protected_->blocksize != 2304 &&
- encoder->protected_->blocksize != 4608 &&
- encoder->protected_->blocksize != 256 &&
- encoder->protected_->blocksize != 512 &&
- encoder->protected_->blocksize != 1024 &&
- encoder->protected_->blocksize != 2048 &&
- encoder->protected_->blocksize != 4096 &&
- encoder->protected_->blocksize != 8192 &&
- encoder->protected_->blocksize != 16384
- )
- return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
- if(
- encoder->protected_->sample_rate != 8000 &&
- encoder->protected_->sample_rate != 16000 &&
- encoder->protected_->sample_rate != 22050 &&
- encoder->protected_->sample_rate != 24000 &&
- encoder->protected_->sample_rate != 32000 &&
- encoder->protected_->sample_rate != 44100 &&
- encoder->protected_->sample_rate != 48000 &&
- encoder->protected_->sample_rate != 96000
- )
- return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
- if(
- encoder->protected_->bits_per_sample != 8 &&
- encoder->protected_->bits_per_sample != 12 &&
- encoder->protected_->bits_per_sample != 16 &&
- encoder->protected_->bits_per_sample != 20 &&
- encoder->protected_->bits_per_sample != 24
- )
- return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
- if(encoder->protected_->max_residual_partition_order > FLAC__SUBSET_MAX_RICE_PARTITION_ORDER)
- return encoder->protected_->state = FLAC__STREAM_ENCODER_NOT_STREAMABLE;
- }
- if(encoder->protected_->max_residual_partition_order >= (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
- encoder->protected_->max_residual_partition_order = (1u << FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN) - 1;
- if(encoder->protected_->min_residual_partition_order >= encoder->protected_->max_residual_partition_order)
- encoder->protected_->min_residual_partition_order = encoder->protected_->max_residual_partition_order;
- /* validate metadata */
- if(0 == encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0)
- return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
- metadata_has_seektable = false;
- metadata_has_vorbis_comment = false;
- for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
- if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_STREAMINFO)
- return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
- else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
- if(metadata_has_seektable) /* only one is allowed */
- return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
- metadata_has_seektable = true;
- if(!FLAC__format_seektable_is_legal(&encoder->protected_->metadata[i]->data.seek_table))
- return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
- }
- else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
- if(metadata_has_vorbis_comment) /* only one is allowed */
- return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
- metadata_has_vorbis_comment = true;
- }
- else if(encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_CUESHEET) {
- if(!FLAC__format_cuesheet_is_legal(&encoder->protected_->metadata[i]->data.cue_sheet, encoder->protected_->metadata[i]->data.cue_sheet.is_cd, /*violation=*/0))
- return encoder->protected_->state = FLAC__STREAM_ENCODER_INVALID_METADATA;
- }
- }
- encoder->private_->input_capacity = 0;
- for(i = 0; i < encoder->protected_->channels; i++) {
- encoder->private_->integer_signal_unaligned[i] = encoder->private_->integer_signal[i] = 0;
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->real_signal_unaligned[i] = encoder->private_->real_signal[i] = 0;
- #endif
- }
- for(i = 0; i < 2; i++) {
- encoder->private_->integer_signal_mid_side_unaligned[i] = encoder->private_->integer_signal_mid_side[i] = 0;
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->real_signal_mid_side_unaligned[i] = encoder->private_->real_signal_mid_side[i] = 0;
- #endif
- }
- for(i = 0; i < encoder->protected_->channels; i++) {
- encoder->private_->residual_workspace_unaligned[i][0] = encoder->private_->residual_workspace[i][0] = 0;
- encoder->private_->residual_workspace_unaligned[i][1] = encoder->private_->residual_workspace[i][1] = 0;
- encoder->private_->best_subframe[i] = 0;
- }
- for(i = 0; i < 2; i++) {
- encoder->private_->residual_workspace_mid_side_unaligned[i][0] = encoder->private_->residual_workspace_mid_side[i][0] = 0;
- encoder->private_->residual_workspace_mid_side_unaligned[i][1] = encoder->private_->residual_workspace_mid_side[i][1] = 0;
- encoder->private_->best_subframe_mid_side[i] = 0;
- }
- encoder->private_->abs_residual_unaligned = encoder->private_->abs_residual = 0;
- encoder->private_->abs_residual_partition_sums_unaligned = encoder->private_->abs_residual_partition_sums = 0;
- encoder->private_->raw_bits_per_partition_unaligned = encoder->private_->raw_bits_per_partition = 0;
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->loose_mid_side_stereo_frames = (unsigned)((FLAC__double)encoder->protected_->sample_rate * 0.4 / (FLAC__double)encoder->protected_->blocksize + 0.5);
- #else
- /* 26214 is the approximate fixed-point equivalent to 0.4 (0.4 * 2^16) */
- /* sample rate can be up to 655350 Hz, and thus use 20 bits, so we do the multiply÷ by hand */
- FLAC__ASSERT(FLAC__MAX_SAMPLE_RATE <= 655350);
- FLAC__ASSERT(FLAC__MAX_BLOCK_SIZE <= 65535);
- FLAC__ASSERT(encoder->protected_->sample_rate <= 655350);
- FLAC__ASSERT(encoder->protected_->blocksize <= 65535);
- encoder->private_->loose_mid_side_stereo_frames = (unsigned)FLAC__fixedpoint_trunc((((FLAC__uint64)(encoder->protected_->sample_rate) * (FLAC__uint64)(26214)) << 16) / (encoder->protected_->blocksize<<16) + FLAC__FP_ONE_HALF);
- #endif
- if(encoder->private_->loose_mid_side_stereo_frames == 0)
- encoder->private_->loose_mid_side_stereo_frames = 1;
- encoder->private_->loose_mid_side_stereo_frame_count = 0;
- encoder->private_->current_sample_number = 0;
- encoder->private_->current_frame_number = 0;
- encoder->private_->use_wide_by_block = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(encoder->protected_->blocksize)+1 > 30);
- encoder->private_->use_wide_by_order = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(max(encoder->protected_->max_lpc_order, FLAC__MAX_FIXED_ORDER))+1 > 30); /*@@@ need to use this? */
- encoder->private_->use_wide_by_partition = (false); /*@@@ need to set this */
- /*
- * get the CPU info and set the function pointers
- */
- FLAC__cpu_info(&encoder->private_->cpuinfo);
- /* first default to the non-asm routines */
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation;
- #endif
- encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor;
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients;
- encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_64bit = FLAC__lpc_compute_residual_from_qlp_coefficients_wide;
- encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients;
- #endif
- /* now override with asm where appropriate */
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- # ifndef FLAC__NO_ASM
- if(encoder->private_->cpuinfo.use_asm) {
- # ifdef FLAC__CPU_IA32
- FLAC__ASSERT(encoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
- # ifdef FLAC__HAS_NASM
- # ifdef FLAC__SSE_OS
- if(encoder->private_->cpuinfo.data.ia32.sse) {
- if(encoder->protected_->max_lpc_order < 4)
- encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4;
- else if(encoder->protected_->max_lpc_order < 8)
- encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8;
- else if(encoder->protected_->max_lpc_order < 12)
- encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12;
- else
- encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
- }
- else
- # endif /* FLAC__SSE_OS */
- if(encoder->private_->cpuinfo.data.ia32._3dnow)
- encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow;
- else
- encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
- if(encoder->private_->cpuinfo.data.ia32.mmx) {
- encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
- encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx;
- }
- else {
- encoder->private_->local_lpc_compute_residual_from_qlp_coefficients = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
- encoder->private_->local_lpc_compute_residual_from_qlp_coefficients_16bit = FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32;
- }
- if(encoder->private_->cpuinfo.data.ia32.mmx && encoder->private_->cpuinfo.data.ia32.cmov)
- encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_asm_ia32_mmx_cmov;
- # endif /* FLAC__HAS_NASM */
- # endif /* FLAC__CPU_IA32 */
- }
- # endif /* !FLAC__NO_ASM */
- #endif /* !FLAC__INTEGER_ONLY_LIBRARY */
- /* finally override based on wide-ness if necessary */
- if(encoder->private_->use_wide_by_block) {
- encoder->private_->local_fixed_compute_best_predictor = FLAC__fixed_compute_best_predictor_wide;
- }
- /* we require precompute_partition_sums if do_escape_coding because of their intertwined nature */
- encoder->private_->precompute_partition_sums = (encoder->protected_->max_residual_partition_order > encoder->protected_->min_residual_partition_order) || encoder->protected_->do_escape_coding;
- if(!resize_buffers_(encoder, encoder->protected_->blocksize)) {
- /* the above function sets the state for us in case of an error */
- return encoder->protected_->state;
- }
- if(!FLAC__bitbuffer_init(encoder->private_->frame))
- return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
- /*
- * Set up the verify stuff if necessary
- */
- if(encoder->protected_->verify) {
- /*
- * First, set up the fifo which will hold the
- * original signal to compare against
- */
- encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize;
- for(i = 0; i < encoder->protected_->channels; i++) {
- if(0 == (encoder->private_->verify.input_fifo.data[i] = (FLAC__int32*)malloc(sizeof(FLAC__int32) * encoder->private_->verify.input_fifo.size)))
- return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
- }
- encoder->private_->verify.input_fifo.tail = 0;
- /*
- * Now set up a stream decoder for verification
- */
- encoder->private_->verify.decoder = FLAC__stream_decoder_new();
- if(0 == encoder->private_->verify.decoder)
- return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
- FLAC__stream_decoder_set_read_callback(encoder->private_->verify.decoder, verify_read_callback_);
- FLAC__stream_decoder_set_write_callback(encoder->private_->verify.decoder, verify_write_callback_);
- FLAC__stream_decoder_set_metadata_callback(encoder->private_->verify.decoder, verify_metadata_callback_);
- FLAC__stream_decoder_set_error_callback(encoder->private_->verify.decoder, verify_error_callback_);
- FLAC__stream_decoder_set_client_data(encoder->private_->verify.decoder, encoder);
- if(FLAC__stream_decoder_init(encoder->private_->verify.decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
- return encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
- }
- encoder->private_->verify.error_stats.absolute_sample = 0;
- encoder->private_->verify.error_stats.frame_number = 0;
- encoder->private_->verify.error_stats.channel = 0;
- encoder->private_->verify.error_stats.sample = 0;
- encoder->private_->verify.error_stats.expected = 0;
- encoder->private_->verify.error_stats.got = 0;
- /*
- * write the stream header
- */
- if(encoder->protected_->verify)
- encoder->private_->verify.state_hint = ENCODER_IN_MAGIC;
- if(!FLAC__bitbuffer_write_raw_uint32(encoder->private_->frame, FLAC__STREAM_SYNC, FLAC__STREAM_SYNC_LEN))
- return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
- if(!write_bitbuffer_(encoder, 0)) {
- /* the above function sets the state for us in case of an error */
- return encoder->protected_->state;
- }
- /*
- * write the STREAMINFO metadata block
- */
- if(encoder->protected_->verify)
- encoder->private_->verify.state_hint = ENCODER_IN_METADATA;
- encoder->private_->metadata.type = FLAC__METADATA_TYPE_STREAMINFO;
- encoder->private_->metadata.is_last = false; /* we will have at a minimum a VORBIS_COMMENT afterwards */
- encoder->private_->metadata.length = FLAC__STREAM_METADATA_STREAMINFO_LENGTH;
- encoder->private_->metadata.data.stream_info.min_blocksize = encoder->protected_->blocksize; /* this encoder uses the same blocksize for the whole stream */
- encoder->private_->metadata.data.stream_info.max_blocksize = encoder->protected_->blocksize;
- encoder->private_->metadata.data.stream_info.min_framesize = 0; /* we don't know this yet; have to fill it in later */
- encoder->private_->metadata.data.stream_info.max_framesize = 0; /* we don't know this yet; have to fill it in later */
- encoder->private_->metadata.data.stream_info.sample_rate = encoder->protected_->sample_rate;
- encoder->private_->metadata.data.stream_info.channels = encoder->protected_->channels;
- encoder->private_->metadata.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
- encoder->private_->metadata.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
- memset(encoder->private_->metadata.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
- FLAC__MD5Init(&encoder->private_->md5context);
- if(!FLAC__bitbuffer_clear(encoder->private_->frame))
- return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
- if(!FLAC__add_metadata_block(&encoder->private_->metadata, encoder->private_->frame))
- return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
- if(!write_bitbuffer_(encoder, 0)) {
- /* the above function sets the state for us in case of an error */
- return encoder->protected_->state;
- }
- /*
- * Now that the STREAMINFO block is written, we can init this to an
- * absurdly-high value...
- */
- encoder->private_->metadata.data.stream_info.min_framesize = (1u << FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN) - 1;
- /* ... and clear this to 0 */
- encoder->private_->metadata.data.stream_info.total_samples = 0;
- /*
- * Check to see if the supplied metadata contains a VORBIS_COMMENT;
- * if not, we will write an empty one (FLAC__add_metadata_block()
- * automatically supplies the vendor string).
- *
- * WATCHOUT: libOggFLAC depends on us to write this block after the
- * STREAMINFO since that's what the mapping requires. (In the case
- * that metadata_has_vorbis_comment is true it will have already
- * insured that the metadata list is properly ordered.)
- */
- if(!metadata_has_vorbis_comment) {
- FLAC__StreamMetadata vorbis_comment;
- vorbis_comment.type = FLAC__METADATA_TYPE_VORBIS_COMMENT;
- vorbis_comment.is_last = (encoder->protected_->num_metadata_blocks == 0);
- vorbis_comment.length = 4 + 4; /* MAGIC NUMBER */
- vorbis_comment.data.vorbis_comment.vendor_string.length = 0;
- vorbis_comment.data.vorbis_comment.vendor_string.entry = 0;
- vorbis_comment.data.vorbis_comment.num_comments = 0;
- vorbis_comment.data.vorbis_comment.comments = 0;
- if(!FLAC__bitbuffer_clear(encoder->private_->frame))
- return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
- if(!FLAC__add_metadata_block(&vorbis_comment, encoder->private_->frame))
- return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
- if(!write_bitbuffer_(encoder, 0)) {
- /* the above function sets the state for us in case of an error */
- return encoder->protected_->state;
- }
- }
- /*
- * write the user's metadata blocks
- */
- for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
- encoder->protected_->metadata[i]->is_last = (i == encoder->protected_->num_metadata_blocks - 1);
- if(!FLAC__bitbuffer_clear(encoder->private_->frame))
- return encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
- if(!FLAC__add_metadata_block(encoder->protected_->metadata[i], encoder->private_->frame))
- return encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
- if(!write_bitbuffer_(encoder, 0)) {
- /* the above function sets the state for us in case of an error */
- return encoder->protected_->state;
- }
- }
- if(encoder->protected_->verify)
- encoder->private_->verify.state_hint = ENCODER_IN_AUDIO;
- return encoder->protected_->state;
- }
- FLAC_API void FLAC__stream_encoder_finish(FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state == FLAC__STREAM_ENCODER_UNINITIALIZED)
- return;
- if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
- if(encoder->private_->current_sample_number != 0) {
- encoder->protected_->blocksize = encoder->private_->current_sample_number;
- process_frame_(encoder, true); /* true => is last frame */
- }
- }
- FLAC__MD5Final(encoder->private_->metadata.data.stream_info.md5sum, &encoder->private_->md5context);
- if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK && !encoder->private_->is_being_deleted) {
- encoder->private_->metadata_callback(encoder, &encoder->private_->metadata, encoder->private_->client_data);
- }
- if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder)
- FLAC__stream_decoder_finish(encoder->private_->verify.decoder);
- free_(encoder);
- set_defaults_(encoder);
- encoder->protected_->state = FLAC__STREAM_ENCODER_UNINITIALIZED;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_verify(FLAC__StreamEncoder *encoder, FLAC__bool value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- #ifndef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
- encoder->protected_->verify = value;
- #endif
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_streamable_subset(FLAC__StreamEncoder *encoder, FLAC__bool value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->protected_->streamable_subset = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_do_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->protected_->do_mid_side_stereo = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_loose_mid_side_stereo(FLAC__StreamEncoder *encoder, FLAC__bool value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->protected_->loose_mid_side_stereo = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->protected_->channels = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_bits_per_sample(FLAC__StreamEncoder *encoder, unsigned value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->protected_->bits_per_sample = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_sample_rate(FLAC__StreamEncoder *encoder, unsigned value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->protected_->sample_rate = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_blocksize(FLAC__StreamEncoder *encoder, unsigned value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->protected_->blocksize = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_max_lpc_order(FLAC__StreamEncoder *encoder, unsigned value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->protected_->max_lpc_order = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_qlp_coeff_precision(FLAC__StreamEncoder *encoder, unsigned value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->protected_->qlp_coeff_precision = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_do_qlp_coeff_prec_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->protected_->do_qlp_coeff_prec_search = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_do_escape_coding(FLAC__StreamEncoder *encoder, FLAC__bool value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- #if 0
- /*@@@ deprecated: */
- encoder->protected_->do_escape_coding = value;
- #else
- (void)value;
- #endif
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_do_exhaustive_model_search(FLAC__StreamEncoder *encoder, FLAC__bool value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->protected_->do_exhaustive_model_search = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_min_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->protected_->min_residual_partition_order = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_max_residual_partition_order(FLAC__StreamEncoder *encoder, unsigned value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->protected_->max_residual_partition_order = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_rice_parameter_search_dist(FLAC__StreamEncoder *encoder, unsigned value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- #if 0
- /*@@@ deprecated: */
- encoder->protected_->rice_parameter_search_dist = value;
- #else
- (void)value;
- #endif
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_total_samples_estimate(FLAC__StreamEncoder *encoder, FLAC__uint64 value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->protected_->total_samples_estimate = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata(FLAC__StreamEncoder *encoder, FLAC__StreamMetadata **metadata, unsigned num_blocks)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->protected_->metadata = metadata;
- encoder->protected_->num_metadata_blocks = num_blocks;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_write_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderWriteCallback value)
- {
- FLAC__ASSERT(0 != encoder);
- FLAC__ASSERT(0 != value);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->private_->write_callback = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_metadata_callback(FLAC__StreamEncoder *encoder, FLAC__StreamEncoderMetadataCallback value)
- {
- FLAC__ASSERT(0 != encoder);
- FLAC__ASSERT(0 != value);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->private_->metadata_callback = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_set_client_data(FLAC__StreamEncoder *encoder, void *value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->private_->client_data = value;
- return true;
- }
- /*
- * These three functions are not static, but not publically exposed in
- * include/FLAC/ either. They are used by the test suite.
- */
- FLAC_API FLAC__bool FLAC__stream_encoder_disable_constant_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->private_->disable_constant_subframes = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_disable_fixed_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->private_->disable_fixed_subframes = value;
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_disable_verbatim_subframes(FLAC__StreamEncoder *encoder, FLAC__bool value)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
- return false;
- encoder->private_->disable_verbatim_subframes = value;
- return true;
- }
- FLAC_API FLAC__StreamEncoderState FLAC__stream_encoder_get_state(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->state;
- }
- FLAC_API FLAC__StreamDecoderState FLAC__stream_encoder_get_verify_decoder_state(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- if(encoder->protected_->verify)
- return FLAC__stream_decoder_get_state(encoder->private_->verify.decoder);
- else
- return FLAC__STREAM_DECODER_UNINITIALIZED;
- }
- FLAC_API const char *FLAC__stream_encoder_get_resolved_state_string(const FLAC__StreamEncoder *encoder)
- {
- if(encoder->protected_->state != FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR)
- return FLAC__StreamEncoderStateString[encoder->protected_->state];
- else
- return FLAC__stream_decoder_get_resolved_state_string(encoder->private_->verify.decoder);
- }
- 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)
- {
- FLAC__ASSERT(0 != encoder);
- if(0 != absolute_sample)
- *absolute_sample = encoder->private_->verify.error_stats.absolute_sample;
- if(0 != frame_number)
- *frame_number = encoder->private_->verify.error_stats.frame_number;
- if(0 != channel)
- *channel = encoder->private_->verify.error_stats.channel;
- if(0 != sample)
- *sample = encoder->private_->verify.error_stats.sample;
- if(0 != expected)
- *expected = encoder->private_->verify.error_stats.expected;
- if(0 != got)
- *got = encoder->private_->verify.error_stats.got;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_get_verify(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->verify;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_get_streamable_subset(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->streamable_subset;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_get_do_mid_side_stereo(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->do_mid_side_stereo;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_get_loose_mid_side_stereo(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->loose_mid_side_stereo;
- }
- FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->channels;
- }
- FLAC_API unsigned FLAC__stream_encoder_get_bits_per_sample(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->bits_per_sample;
- }
- FLAC_API unsigned FLAC__stream_encoder_get_sample_rate(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->sample_rate;
- }
- FLAC_API unsigned FLAC__stream_encoder_get_blocksize(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->blocksize;
- }
- FLAC_API unsigned FLAC__stream_encoder_get_max_lpc_order(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->max_lpc_order;
- }
- FLAC_API unsigned FLAC__stream_encoder_get_qlp_coeff_precision(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->qlp_coeff_precision;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_get_do_qlp_coeff_prec_search(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->do_qlp_coeff_prec_search;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_get_do_escape_coding(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->do_escape_coding;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_get_do_exhaustive_model_search(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->do_exhaustive_model_search;
- }
- FLAC_API unsigned FLAC__stream_encoder_get_min_residual_partition_order(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->min_residual_partition_order;
- }
- FLAC_API unsigned FLAC__stream_encoder_get_max_residual_partition_order(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->max_residual_partition_order;
- }
- FLAC_API unsigned FLAC__stream_encoder_get_rice_parameter_search_dist(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->rice_parameter_search_dist;
- }
- FLAC_API FLAC__uint64 FLAC__stream_encoder_get_total_samples_estimate(const FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- return encoder->protected_->total_samples_estimate;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_process(FLAC__StreamEncoder *encoder, const FLAC__int32 * const buffer[], unsigned samples)
- {
- unsigned i, j, channel;
- FLAC__int32 x, mid, side;
- const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
- FLAC__ASSERT(0 != encoder);
- FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
- j = 0;
- /*
- * we have several flavors of the same basic loop, optimized for
- * different conditions:
- */
- if(encoder->protected_->max_lpc_order > 0) {
- if(encoder->protected_->do_mid_side_stereo && channels == 2) {
- /*
- * stereo coding: unroll channel loop
- * with LPC: calculate floating point version of signal
- */
- do {
- if(encoder->protected_->verify)
- append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
- for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
- x = mid = side = buffer[0][j];
- encoder->private_->integer_signal[0][i] = x;
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->real_signal[0][i] = (FLAC__real)x;
- #endif
- x = buffer[1][j];
- encoder->private_->integer_signal[1][i] = x;
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->real_signal[1][i] = (FLAC__real)x;
- #endif
- mid += x;
- side -= x;
- mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
- encoder->private_->integer_signal_mid_side[1][i] = side;
- encoder->private_->integer_signal_mid_side[0][i] = mid;
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
- encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
- #endif
- encoder->private_->current_sample_number++;
- }
- if(i == blocksize) {
- if(!process_frame_(encoder, false)) /* false => not last frame */
- return false;
- }
- } while(j < samples);
- }
- else {
- /*
- * independent channel coding: buffer each channel in inner loop
- * with LPC: calculate floating point version of signal
- */
- do {
- if(encoder->protected_->verify)
- append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
- for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
- for(channel = 0; channel < channels; channel++) {
- x = buffer[channel][j];
- encoder->private_->integer_signal[channel][i] = x;
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->real_signal[channel][i] = (FLAC__real)x;
- #endif
- }
- encoder->private_->current_sample_number++;
- }
- if(i == blocksize) {
- if(!process_frame_(encoder, false)) /* false => not last frame */
- return false;
- }
- } while(j < samples);
- }
- }
- else {
- if(encoder->protected_->do_mid_side_stereo && channels == 2) {
- /*
- * stereo coding: unroll channel loop
- * without LPC: no need to calculate floating point version of signal
- */
- do {
- if(encoder->protected_->verify)
- append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
- for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
- encoder->private_->integer_signal[0][i] = mid = side = buffer[0][j];
- x = buffer[1][j];
- encoder->private_->integer_signal[1][i] = x;
- mid += x;
- side -= x;
- mid >>= 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
- encoder->private_->integer_signal_mid_side[1][i] = side;
- encoder->private_->integer_signal_mid_side[0][i] = mid;
- encoder->private_->current_sample_number++;
- }
- if(i == blocksize) {
- if(!process_frame_(encoder, false)) /* false => not last frame */
- return false;
- }
- } while(j < samples);
- }
- else {
- /*
- * independent channel coding: buffer each channel in inner loop
- * without LPC: no need to calculate floating point version of signal
- */
- do {
- if(encoder->protected_->verify)
- append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
- for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
- for(channel = 0; channel < channels; channel++)
- encoder->private_->integer_signal[channel][i] = buffer[channel][j];
- encoder->private_->current_sample_number++;
- }
- if(i == blocksize) {
- if(!process_frame_(encoder, false)) /* false => not last frame */
- return false;
- }
- } while(j < samples);
- }
- }
- return true;
- }
- FLAC_API FLAC__bool FLAC__stream_encoder_process_interleaved(FLAC__StreamEncoder *encoder, const FLAC__int32 buffer[], unsigned samples)
- {
- unsigned i, j, k, channel;
- FLAC__int32 x, mid, side;
- const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
- FLAC__ASSERT(0 != encoder);
- FLAC__ASSERT(encoder->protected_->state == FLAC__STREAM_ENCODER_OK);
- j = k = 0;
- /*
- * we have several flavors of the same basic loop, optimized for
- * different conditions:
- */
- if(encoder->protected_->max_lpc_order > 0) {
- if(encoder->protected_->do_mid_side_stereo && channels == 2) {
- /*
- * stereo coding: unroll channel loop
- * with LPC: calculate floating point version of signal
- */
- do {
- if(encoder->protected_->verify)
- append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
- for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
- x = mid = side = buffer[k++];
- encoder->private_->integer_signal[0][i] = x;
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->real_signal[0][i] = (FLAC__real)x;
- #endif
- x = buffer[k++];
- encoder->private_->integer_signal[1][i] = x;
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->real_signal[1][i] = (FLAC__real)x;
- #endif
- mid += x;
- side -= x;
- mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
- encoder->private_->integer_signal_mid_side[1][i] = side;
- encoder->private_->integer_signal_mid_side[0][i] = mid;
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->real_signal_mid_side[1][i] = (FLAC__real)side;
- encoder->private_->real_signal_mid_side[0][i] = (FLAC__real)mid;
- #endif
- encoder->private_->current_sample_number++;
- }
- if(i == blocksize) {
- if(!process_frame_(encoder, false)) /* false => not last frame */
- return false;
- }
- } while(j < samples);
- }
- else {
- /*
- * independent channel coding: buffer each channel in inner loop
- * with LPC: calculate floating point version of signal
- */
- do {
- if(encoder->protected_->verify)
- append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
- for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
- for(channel = 0; channel < channels; channel++) {
- x = buffer[k++];
- encoder->private_->integer_signal[channel][i] = x;
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->real_signal[channel][i] = (FLAC__real)x;
- #endif
- }
- encoder->private_->current_sample_number++;
- }
- if(i == blocksize) {
- if(!process_frame_(encoder, false)) /* false => not last frame */
- return false;
- }
- } while(j < samples);
- }
- }
- else {
- if(encoder->protected_->do_mid_side_stereo && channels == 2) {
- /*
- * stereo coding: unroll channel loop
- * without LPC: no need to calculate floating point version of signal
- */
- do {
- if(encoder->protected_->verify)
- append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
- for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
- encoder->private_->integer_signal[0][i] = mid = side = buffer[k++];
- x = buffer[k++];
- encoder->private_->integer_signal[1][i] = x;
- mid += x;
- side -= x;
- mid >>= 1; /* NOTE: not the same as 'mid = (left + right) / 2' ! */
- encoder->private_->integer_signal_mid_side[1][i] = side;
- encoder->private_->integer_signal_mid_side[0][i] = mid;
- encoder->private_->current_sample_number++;
- }
- if(i == blocksize) {
- if(!process_frame_(encoder, false)) /* false => not last frame */
- return false;
- }
- } while(j < samples);
- }
- else {
- /*
- * independent channel coding: buffer each channel in inner loop
- * without LPC: no need to calculate floating point version of signal
- */
- do {
- if(encoder->protected_->verify)
- append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, min(blocksize-encoder->private_->current_sample_number, samples-j));
- for(i = encoder->private_->current_sample_number; i < blocksize && j < samples; i++, j++) {
- for(channel = 0; channel < channels; channel++)
- encoder->private_->integer_signal[channel][i] = buffer[k++];
- encoder->private_->current_sample_number++;
- }
- if(i == blocksize) {
- if(!process_frame_(encoder, false)) /* false => not last frame */
- return false;
- }
- } while(j < samples);
- }
- }
- return true;
- }
- /***********************************************************************
- *
- * Private class methods
- *
- ***********************************************************************/
- void set_defaults_(FLAC__StreamEncoder *encoder)
- {
- FLAC__ASSERT(0 != encoder);
- #ifdef FLAC__MANDATORY_VERIFY_WHILE_ENCODING
- encoder->protected_->verify = true;
- #else
- encoder->protected_->verify = false;
- #endif
- encoder->protected_->streamable_subset = true;
- encoder->protected_->do_mid_side_stereo = false;
- encoder->protected_->loose_mid_side_stereo = false;
- encoder->protected_->channels = 2;
- encoder->protected_->bits_per_sample = 16;
- encoder->protected_->sample_rate = 44100;
- encoder->protected_->blocksize = 1152;
- encoder->protected_->max_lpc_order = 0;
- encoder->protected_->qlp_coeff_precision = 0;
- encoder->protected_->do_qlp_coeff_prec_search = false;
- encoder->protected_->do_exhaustive_model_search = false;
- encoder->protected_->do_escape_coding = false;
- encoder->protected_->min_residual_partition_order = 0;
- encoder->protected_->max_residual_partition_order = 0;
- encoder->protected_->rice_parameter_search_dist = 0;
- encoder->protected_->total_samples_estimate = 0;
- encoder->protected_->metadata = 0;
- encoder->protected_->num_metadata_blocks = 0;
- encoder->private_->disable_constant_subframes = false;
- encoder->private_->disable_fixed_subframes = false;
- encoder->private_->disable_verbatim_subframes = false;
- encoder->private_->write_callback = 0;
- encoder->private_->metadata_callback = 0;
- encoder->private_->client_data = 0;
- }
- void free_(FLAC__StreamEncoder *encoder)
- {
- unsigned i, channel;
- FLAC__ASSERT(0 != encoder);
- for(i = 0; i < encoder->protected_->channels; i++) {
- if(0 != encoder->private_->integer_signal_unaligned[i]) {
- free(encoder->private_->integer_signal_unaligned[i]);
- encoder->private_->integer_signal_unaligned[i] = 0;
- }
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- if(0 != encoder->private_->real_signal_unaligned[i]) {
- free(encoder->private_->real_signal_unaligned[i]);
- encoder->private_->real_signal_unaligned[i] = 0;
- }
- #endif
- }
- for(i = 0; i < 2; i++) {
- if(0 != encoder->private_->integer_signal_mid_side_unaligned[i]) {
- free(encoder->private_->integer_signal_mid_side_unaligned[i]);
- encoder->private_->integer_signal_mid_side_unaligned[i] = 0;
- }
- #ifndef FLAC__INTEGER_ONLY_LIBRARY
- if(0 != encoder->private_->real_signal_mid_side_unaligned[i]) {
- free(encoder->private_->real_signal_mid_side_unaligned[i]);
- encoder->private_->real_signal_mid_side_unaligned[i] = 0;
- }
- #endif