📄 stream_encoder.c
字号:
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)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -