/* libFLAC - Free Lossless Audio Codec library
- * Copyright (C) 2000,2001,2002,2003,2004,2005,2006 Josh Coalson
+ * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
# include <config.h>
#endif
-#if defined _MSC_VER || defined __MINGW32__
-#include <io.h> /* for _setmode() */
-#include <fcntl.h> /* for _O_BINARY */
-#endif
-#if defined __CYGWIN__ || defined __EMX__
-#include <io.h> /* for setmode(), O_BINARY */
-#include <fcntl.h> /* for _O_BINARY */
-#endif
#include <limits.h>
#include <stdio.h>
#include <stdlib.h> /* for malloc() */
#include <string.h> /* for memcpy() */
#include <sys/types.h> /* for off_t */
-#if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__
-#if _MSC_VER <= 1200 || defined __BORLANDC__ /* @@@ [2G limit] */
-#define fseeko fseek
-#define ftello ftell
-#endif
-#endif
#include "FLAC/assert.h"
#include "FLAC/stream_decoder.h"
#include "protected/stream_encoder.h"
#include "private/lpc.h"
#include "private/md5.h"
#include "private/memory.h"
+#include "private/macros.h"
#if FLAC__HAS_OGG
#include "private/ogg_helper.h"
#include "private/ogg_mapping.h"
#endif
#include "private/stream_encoder_framing.h"
#include "private/window.h"
+#include "share/alloc.h"
+#include "share/compat.h"
+#include "share/private.h"
-#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))
/* Exact Rice codeword length calculation is off by default. The simple
* (and fast) estimation (of how many bits a residual value will be
* parameter estimation in this encoder is very good, almost always
* yielding compression within 0.1% of the optimal parameters.
*/
-#undef ENABLE_RICE_PARAMETER_SEARCH
+#undef ENABLE_RICE_PARAMETER_SEARCH
+
typedef struct {
FLAC__int32 *data[FLAC__MAX_CHANNELS];
unsigned max_residual_partition_order;
unsigned rice_parameter_search_dist;
} compression_levels_[] = {
- { false, false, 0, 0, false, false, false, 2, 2, 0 },
- { true , true , 0, 0, false, false, false, 2, 2, 0 },
+ { false, false, 0, 0, false, false, false, 0, 3, 0 },
+ { true , true , 0, 0, false, false, false, 0, 3, 0 },
{ true , false, 0, 0, false, false, false, 0, 3, 0 },
- { false, false, 6, 0, false, false, false, 3, 3, 0 },
- { true , true , 8, 0, false, false, false, 3, 3, 0 },
- { true , false, 8, 0, false, false, false, 3, 3, 0 },
- { true , false, 8, 0, false, false, false, 0, 4, 0 },
+ { false, false, 6, 0, false, false, false, 0, 4, 0 },
+ { true , true , 8, 0, false, false, false, 0, 4, 0 },
+ { true , false, 8, 0, false, false, false, 0, 5, 0 },
+ { true , false, 8, 0, false, false, false, 0, 6, 0 },
{ true , false, 8, 0, false, false, true , 0, 6, 0 },
{ true , false, 12, 0, false, false, true , 0, 6, 0 }
};
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 subframe_bps,
unsigned order,
unsigned rice_parameter,
+ unsigned rice_parameter_limit,
unsigned min_partition_order,
unsigned max_partition_order,
FLAC__bool do_escape_coding,
unsigned order,
unsigned qlp_coeff_precision,
unsigned rice_parameter,
+ unsigned rice_parameter_limit,
unsigned min_partition_order,
unsigned max_partition_order,
FLAC__bool do_escape_coding,
#endif
static unsigned evaluate_verbatim_subframe_(
- FLAC__StreamEncoder *encoder,
+ FLAC__StreamEncoder *encoder,
const FLAC__int32 signal[],
unsigned blocksize,
unsigned subframe_bps,
unsigned residual_samples,
unsigned predictor_order,
unsigned rice_parameter,
+ unsigned rice_parameter_limit,
unsigned min_partition_order,
unsigned max_partition_order,
+ unsigned bps,
FLAC__bool do_escape_coding,
unsigned rice_parameter_search_dist,
- FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
+ FLAC__EntropyCodingMethod *best_ecm
);
static void precompute_partition_info_sums_(
unsigned residual_samples,
unsigned predictor_order,
unsigned min_partition_order,
- unsigned max_partition_order
+ unsigned max_partition_order,
+ unsigned bps
);
static void precompute_partition_info_escapes_(
const unsigned residual_samples,
const unsigned predictor_order,
const unsigned suggested_rice_parameter,
+ const unsigned rice_parameter_limit,
const unsigned rice_parameter_search_dist,
const unsigned partition_order,
const FLAC__bool search_for_escapes,
static FLAC__StreamEncoderSeekStatus file_seek_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 absolute_byte_offset, void *client_data);
static FLAC__StreamEncoderTellStatus file_tell_callback_(const FLAC__StreamEncoder *encoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
static FLAC__StreamEncoderWriteStatus file_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], size_t bytes, unsigned samples, unsigned current_frame, void *client_data);
-static FILE *get_binary_stdout_();
+static FILE *get_binary_stdout_(void);
/***********************************************************************
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) */
+ FLAC__real *real_signal[FLAC__MAX_CHANNELS]; /* (@@@ currently unused) the floating-point version of the input signal */
+ FLAC__real *real_signal_mid_side[2]; /* (@@@ currently unused) the floating-point version of the mid-side input signal (stereo only) */
FLAC__real *window[FLAC__MAX_APODIZATION_FUNCTIONS]; /* the pre-computed floating-point window for each apodization function */
- FLAC__real *windowed_signal; /* the real_signal[] * current window[] */
+ FLAC__real *windowed_signal; /* the integer_signal[] * current window[] */
#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__StreamMetadata_SeekTable *seek_table; /* pointer into encoder->protected_->metadata_ where the seek table is */
unsigned current_sample_number;
unsigned current_frame_number;
- struct FLAC__MD5Context md5context;
+ 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]);
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];
+ FLAC__real *real_signal_unaligned[FLAC__MAX_CHANNELS]; /* (@@@ currently unused) */
+ FLAC__real *real_signal_mid_side_unaligned[2]; /* (@@@ currently unused) */
FLAC__real *window_unaligned[FLAC__MAX_APODIZATION_FUNCTIONS];
FLAC__real *windowed_signal_unaligned;
#endif
"FLAC__STREAM_ENCODER_INIT_STATUS_ALREADY_INITIALIZED"
};
-FLAC_API const char * const FLAC__treamEncoderReadStatusString[] = {
+FLAC_API const char * const FLAC__StreamEncoderReadStatusString[] = {
"FLAC__STREAM_ENCODER_READ_STATUS_CONTINUE",
"FLAC__STREAM_ENCODER_READ_STATUS_END_OF_STREAM",
"FLAC__STREAM_ENCODER_READ_STATUS_ABORT",
* Class constructor/destructor
*
*/
-FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new()
+FLAC_API FLAC__StreamEncoder *FLAC__stream_encoder_new(void)
{
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));
+ encoder = calloc(1, sizeof(FLAC__StreamEncoder));
if(encoder == 0) {
return 0;
}
- encoder->protected_ = (FLAC__StreamEncoderProtected*)calloc(1, sizeof(FLAC__StreamEncoderProtected));
+ encoder->protected_ = calloc(1, sizeof(FLAC__StreamEncoderProtected));
if(encoder->protected_ == 0) {
free(encoder);
return 0;
}
- encoder->private_ = (FLAC__StreamEncoderPrivate*)calloc(1, sizeof(FLAC__StreamEncoderPrivate));
+ encoder->private_ = calloc(1, sizeof(FLAC__StreamEncoderPrivate));
if(encoder->private_ == 0) {
free(encoder->protected_);
free(encoder);
{
unsigned i;
- FLAC__ASSERT(0 != encoder);
+ if (encoder == NULL)
+ return ;
+
FLAC__ASSERT(0 != encoder->protected_);
FLAC__ASSERT(0 != encoder->private_);
FLAC__ASSERT(0 != encoder->private_->frame);
if(encoder->protected_->max_lpc_order == 0)
encoder->protected_->blocksize = 1152;
else
- encoder->protected_->blocksize = 4608;
+ encoder->protected_->blocksize = 4096;
}
if(encoder->protected_->blocksize < FLAC__MIN_BLOCK_SIZE || encoder->protected_->blocksize > FLAC__MAX_BLOCK_SIZE)
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);
+ encoder->protected_->qlp_coeff_precision = flac_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)
return FLAC__STREAM_ENCODER_INIT_STATUS_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
- )
+ if(!FLAC__format_blocksize_is_subset(encoder->protected_->blocksize, encoder->protected_->sample_rate))
return FLAC__STREAM_ENCODER_INIT_STATUS_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
- )
+ if(!FLAC__format_sample_rate_is_subset(encoder->protected_->sample_rate))
return FLAC__STREAM_ENCODER_INIT_STATUS_NOT_STREAMABLE;
if(
encoder->protected_->bits_per_sample != 8 &&
#if FLAC__HAS_OGG
/* reorder metadata if necessary to ensure that any VORBIS_COMMENT is the first, according to the mapping spec */
if(is_ogg && 0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 1) {
- unsigned i;
- for(i = 1; i < encoder->protected_->num_metadata_blocks; i++) {
- if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
- FLAC__StreamMetadata *vc = encoder->protected_->metadata[i];
- for( ; i > 0; i--)
- encoder->protected_->metadata[i] = encoder->protected_->metadata[i-1];
+ unsigned i1;
+ for(i1 = 1; i1 < encoder->protected_->num_metadata_blocks; i1++) {
+ if(0 != encoder->protected_->metadata[i1] && encoder->protected_->metadata[i1]->type == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
+ FLAC__StreamMetadata *vc = encoder->protected_->metadata[i1];
+ for( ; i1 > 0; i1--)
+ encoder->protected_->metadata[i1] = encoder->protected_->metadata[i1-1];
encoder->protected_->metadata[0] = vc;
break;
}
#endif
/* keep track of any SEEKTABLE block */
if(0 != encoder->protected_->metadata && encoder->protected_->num_metadata_blocks > 0) {
- unsigned i;
- for(i = 0; i < encoder->protected_->num_metadata_blocks; i++) {
- if(0 != encoder->protected_->metadata[i] && encoder->protected_->metadata[i]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
- encoder->private_->seek_table = &encoder->protected_->metadata[i]->data.seek_table;
+ unsigned i2;
+ for(i2 = 0; i2 < encoder->protected_->num_metadata_blocks; i2++) {
+ if(0 != encoder->protected_->metadata[i2] && encoder->protected_->metadata[i2]->type == FLAC__METADATA_TYPE_SEEKTABLE) {
+ encoder->private_->seek_table = &encoder->protected_->metadata[i2]->data.seek_table;
break; /* take only the first one */
}
}
metadata_picture_has_type1 = true;
/* standard icon must be 32x32 pixel PNG */
if(
- m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
+ m->data.picture.type == FLAC__STREAM_METADATA_PICTURE_TYPE_FILE_ICON_STANDARD &&
(
(strcmp(m->data.picture.mime_type, "image/png") && strcmp(m->data.picture.mime_type, "-->")) ||
m->data.picture.width != 32 ||
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_order = (encoder->protected_->bits_per_sample + FLAC__bitmath_ilog2(flac_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 */
/*
# 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
encoder->private_->local_lpc_compute_autocorrelation = FLAC__lpc_compute_autocorrelation_asm_ia32;
}
- else
-# endif /* FLAC__SSE_OS */
- if(encoder->private_->cpuinfo.data.ia32._3dnow)
+ else 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;
*/
encoder->private_->verify.input_fifo.size = encoder->protected_->blocksize+OVERREAD_;
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))) {
+ if(0 == (encoder->private_->verify.input_fifo.data[i] = safe_malloc_mul_2op_p(sizeof(FLAC__int32), /*times*/encoder->private_->verify.input_fifo.size))) {
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
}
/*
* Now set up a stream decoder for verification
*/
- encoder->private_->verify.decoder = FLAC__stream_decoder_new();
if(0 == encoder->private_->verify.decoder) {
- encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
- return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
+ encoder->private_->verify.decoder = FLAC__stream_decoder_new();
+ if(0 == encoder->private_->verify.decoder) {
+ encoder->protected_->state = FLAC__STREAM_ENCODER_VERIFY_DECODER_ERROR;
+ return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
+ }
}
if(FLAC__stream_decoder_init_stream(encoder->private_->verify.decoder, verify_read_callback_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, verify_write_callback_, verify_metadata_callback_, verify_error_callback_, /*client_data=*/encoder) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
encoder->private_->streaminfo.data.stream_info.bits_per_sample = encoder->protected_->bits_per_sample;
encoder->private_->streaminfo.data.stream_info.total_samples = encoder->protected_->total_samples_estimate; /* we will replace this later with the real total */
memset(encoder->private_->streaminfo.data.stream_info.md5sum, 0, 16); /* we don't know this yet; have to fill it in later */
- FLAC__MD5Init(&encoder->private_->md5context);
+ if(encoder->protected_->do_md5)
+ FLAC__MD5Init(&encoder->private_->md5context);
if(!FLAC__add_metadata_block(&encoder->private_->streaminfo, encoder->private_->frame)) {
encoder->protected_->state = FLAC__STREAM_ENCODER_FRAMING_ERROR;
return FLAC__STREAM_ENCODER_INIT_STATUS_ENCODER_ERROR;
/*is_ogg=*/true
);
}
-
+
static FLAC__StreamEncoderInitStatus init_FILE_internal_(
FLAC__StreamEncoder *encoder,
FILE *file,
return init_status;
}
-
+
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_FILE(
FLAC__StreamEncoder *encoder,
FILE *file,
{
return init_FILE_internal_(encoder, file, progress_callback, client_data, /*is_ogg=*/false);
}
-
+
FLAC_API FLAC__StreamEncoderInitStatus FLAC__stream_encoder_init_ogg_FILE(
FLAC__StreamEncoder *encoder,
FILE *file,
}
}
- FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
+ if(encoder->protected_->do_md5)
+ FLAC__MD5Final(encoder->private_->streaminfo.data.stream_info.md5sum, &encoder->private_->md5context);
if(!encoder->private_->is_being_deleted) {
if(encoder->protected_->state == FLAC__STREAM_ENCODER_OK) {
return true;
}
+FLAC_API FLAC__bool FLAC__stream_encoder_set_do_md5(FLAC__StreamEncoder *encoder, FLAC__bool value)
+{
+ FLAC__ASSERT(0 != encoder);
+ FLAC__ASSERT(0 != encoder->private_);
+ FLAC__ASSERT(0 != encoder->protected_);
+ if(encoder->protected_->state != FLAC__STREAM_ENCODER_UNINITIALIZED)
+ return false;
+ encoder->protected_->do_md5 = value;
+ return true;
+}
+
FLAC_API FLAC__bool FLAC__stream_encoder_set_channels(FLAC__StreamEncoder *encoder, unsigned value)
{
FLAC__ASSERT(0 != encoder);
value = sizeof(compression_levels_)/sizeof(compression_levels_[0]) - 1;
ok &= FLAC__stream_encoder_set_do_mid_side_stereo (encoder, compression_levels_[value].do_mid_side_stereo);
ok &= FLAC__stream_encoder_set_loose_mid_side_stereo (encoder, compression_levels_[value].loose_mid_side_stereo);
+#ifndef FLAC__INTEGER_ONLY_LIBRARY
#if 0
/* was: */
ok &= FLAC__stream_encoder_set_apodization (encoder, compression_levels_[value].apodization);
encoder->protected_->apodizations[0].type = FLAC__APODIZATION_TUKEY;
encoder->protected_->apodizations[0].parameters.tukey.p = 0.5;
#endif
+#endif
ok &= FLAC__stream_encoder_set_max_lpc_order (encoder, compression_levels_[value].max_lpc_order);
ok &= FLAC__stream_encoder_set_qlp_coeff_precision (encoder, compression_levels_[value].qlp_coeff_precision);
ok &= FLAC__stream_encoder_set_do_qlp_coeff_prec_search (encoder, compression_levels_[value].do_qlp_coeff_prec_search);
return true;
}
+/*@@@@add to tests*/
FLAC_API FLAC__bool FLAC__stream_encoder_set_apodization(FLAC__StreamEncoder *encoder, const char *specification)
{
FLAC__ASSERT(0 != encoder);
}
if(num_blocks) {
FLAC__StreamMetadata **m;
- if(0 == (m = (FLAC__StreamMetadata**)malloc(sizeof(m[0]) * num_blocks)))
+ if(0 == (m = safe_malloc_mul_2op_p(sizeof(m[0]), /*times*/num_blocks)))
return false;
memcpy(m, metadata, sizeof(m[0]) * num_blocks);
encoder->protected_->metadata = m;
return encoder->protected_->streamable_subset;
}
+FLAC_API FLAC__bool FLAC__stream_encoder_get_do_md5(const FLAC__StreamEncoder *encoder)
+{
+ FLAC__ASSERT(0 != encoder);
+ FLAC__ASSERT(0 != encoder->private_);
+ FLAC__ASSERT(0 != encoder->protected_);
+ return encoder->protected_->do_md5;
+}
+
FLAC_API unsigned FLAC__stream_encoder_get_channels(const FLAC__StreamEncoder *encoder)
{
FLAC__ASSERT(0 != encoder);
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;
+ unsigned i, j = 0, channel;
const unsigned channels = encoder->protected_->channels, blocksize = encoder->protected_->blocksize;
FLAC__ASSERT(0 != encoder);
FLAC__ASSERT(0 != encoder->protected_);
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+1-encoder->private_->current_sample_number, samples-j));
-
- /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
- 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++;
- }
- /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
- if(i > blocksize) {
- if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
- return false;
- /* move unprocessed overread samples to beginnings of arrays */
- FLAC__ASSERT(i == blocksize+OVERREAD_);
- FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
- i--;
- encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
- encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
- encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
- encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
-#ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->real_signal[0][0] = encoder->private_->real_signal[0][i];
- encoder->private_->real_signal[1][0] = encoder->private_->real_signal[1][i];
- encoder->private_->real_signal_mid_side[0][0] = encoder->private_->real_signal_mid_side[0][i];
- encoder->private_->real_signal_mid_side[1][0] = encoder->private_->real_signal_mid_side[1][i];
-#endif
- encoder->private_->current_sample_number = 1;
- }
- } 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+1-encoder->private_->current_sample_number, samples-j));
-
- /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
- 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++;
- }
- /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
- if(i > blocksize) {
- if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
- return false;
- /* move unprocessed overread samples to beginnings of arrays */
- FLAC__ASSERT(i == blocksize+OVERREAD_);
- FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
- i--;
- for(channel = 0; channel < channels; channel++) {
- encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
-#ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->real_signal[channel][0] = encoder->private_->real_signal[channel][i];
-#endif
- }
- encoder->private_->current_sample_number = 1;
- }
- } 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+1-encoder->private_->current_sample_number, samples-j));
-
- /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
- 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++;
- }
- /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
- if(i > blocksize) {
- if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
- return false;
- /* move unprocessed overread samples to beginnings of arrays */
- FLAC__ASSERT(i == blocksize+OVERREAD_);
- FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
- i--;
- encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
- encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
- encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
- encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
- encoder->private_->current_sample_number = 1;
- }
- } while(j < samples);
+ do {
+ const unsigned n = flac_min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j);
+
+ if(encoder->protected_->verify)
+ append_to_verify_fifo_(&encoder->private_->verify.input_fifo, buffer, j, channels, n);
+
+ for(channel = 0; channel < channels; channel++)
+ memcpy(&encoder->private_->integer_signal[channel][encoder->private_->current_sample_number], &buffer[channel][j], sizeof(buffer[channel][0]) * n);
+
+ if(encoder->protected_->do_mid_side_stereo) {
+ FLAC__ASSERT(channels == 2);
+ /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
+ for(i = encoder->private_->current_sample_number; i <= blocksize && j < samples; i++, j++) {
+ encoder->private_->integer_signal_mid_side[1][i] = buffer[0][j] - buffer[1][j];
+ encoder->private_->integer_signal_mid_side[0][i] = (buffer[0][j] + buffer[1][j]) >> 1; /* NOTE: not the same as 'mid = (buffer[0][j] + buffer[1][j]) / 2' ! */
+ }
}
- 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+1-encoder->private_->current_sample_number, samples-j));
-
- /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
- 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++;
- }
- /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
- if(i > blocksize) {
- if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
- return false;
- /* move unprocessed overread samples to beginnings of arrays */
- FLAC__ASSERT(i == blocksize+OVERREAD_);
- FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
- i--;
- for(channel = 0; channel < channels; channel++)
- encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
- encoder->private_->current_sample_number = 1;
- }
- } while(j < samples);
+ else
+ j += n;
+
+ encoder->private_->current_sample_number += n;
+
+ /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
+ if(encoder->private_->current_sample_number > blocksize) {
+ FLAC__ASSERT(encoder->private_->current_sample_number == blocksize+OVERREAD_);
+ FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
+ if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
+ return false;
+ /* move unprocessed overread samples to beginnings of arrays */
+ for(channel = 0; channel < channels; channel++)
+ encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][blocksize];
+ if(encoder->protected_->do_mid_side_stereo) {
+ encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][blocksize];
+ encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][blocksize];
+ }
+ encoder->private_->current_sample_number = 1;
}
- }
+ } while(j < samples);
return true;
}
* 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+1-encoder->private_->current_sample_number, samples-j));
-
- /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
- 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++;
- }
- /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
- if(i > blocksize) {
- if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
- return false;
- /* move unprocessed overread samples to beginnings of arrays */
- FLAC__ASSERT(i == blocksize+OVERREAD_);
- FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
- i--;
- encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
- encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
- encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
- encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
-#ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->real_signal[0][0] = encoder->private_->real_signal[0][i];
- encoder->private_->real_signal[1][0] = encoder->private_->real_signal[1][i];
- encoder->private_->real_signal_mid_side[0][0] = encoder->private_->real_signal_mid_side[0][i];
- encoder->private_->real_signal_mid_side[1][0] = encoder->private_->real_signal_mid_side[1][i];
-#endif
- encoder->private_->current_sample_number = 1;
- }
- } 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+1-encoder->private_->current_sample_number, samples-j));
-
- /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
- 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++;
- }
- /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
- if(i > blocksize) {
- if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
- return false;
- /* move unprocessed overread samples to beginnings of arrays */
- FLAC__ASSERT(i == blocksize+OVERREAD_);
- FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
- i--;
- for(channel = 0; channel < channels; channel++) {
- encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
-#ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->real_signal[channel][0] = encoder->private_->real_signal[channel][i];
-#endif
- }
- encoder->private_->current_sample_number = 1;
- }
- } while(j < samples);
- }
+ if(encoder->protected_->do_mid_side_stereo && channels == 2) {
+ /*
+ * stereo coding: unroll channel loop
+ */
+ do {
+ if(encoder->protected_->verify)
+ append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, flac_min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j));
+
+ /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
+ 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 = i;
+ /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
+ if(i > blocksize) {
+ if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
+ return false;
+ /* move unprocessed overread samples to beginnings of arrays */
+ FLAC__ASSERT(i == blocksize+OVERREAD_);
+ FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
+ encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][blocksize];
+ encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][blocksize];
+ encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][blocksize];
+ encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][blocksize];
+ encoder->private_->current_sample_number = 1;
+ }
+ } 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+1-encoder->private_->current_sample_number, samples-j));
-
- /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
- 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++;
- }
- /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
- if(i > blocksize) {
- if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
- return false;
- /* move unprocessed overread samples to beginnings of arrays */
- FLAC__ASSERT(i == blocksize+OVERREAD_);
- FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
- i--;
- encoder->private_->integer_signal[0][0] = encoder->private_->integer_signal[0][i];
- encoder->private_->integer_signal[1][0] = encoder->private_->integer_signal[1][i];
- encoder->private_->integer_signal_mid_side[0][0] = encoder->private_->integer_signal_mid_side[0][i];
- encoder->private_->integer_signal_mid_side[1][0] = encoder->private_->integer_signal_mid_side[1][i];
- encoder->private_->current_sample_number = 1;
- }
- } 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+1-encoder->private_->current_sample_number, samples-j));
-
- /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
- 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++;
- }
- /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
- if(i > blocksize) {
- if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
- return false;
- /* move unprocessed overread samples to beginnings of arrays */
- FLAC__ASSERT(i == blocksize+OVERREAD_);
- FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
- i--;
- for(channel = 0; channel < channels; channel++)
- encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][i];
- encoder->private_->current_sample_number = 1;
- }
- } while(j < samples);
- }
+ /*
+ * independent channel coding: buffer each channel in inner loop
+ */
+ do {
+ if(encoder->protected_->verify)
+ append_to_verify_fifo_interleaved_(&encoder->private_->verify.input_fifo, buffer, j, channels, flac_min(blocksize+OVERREAD_-encoder->private_->current_sample_number, samples-j));
+
+ /* "i <= blocksize" to overread 1 sample; see comment in OVERREAD_ decl */
+ 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 = i;
+ /* we only process if we have a full block + 1 extra sample; final block is always handled by FLAC__stream_encoder_finish() */
+ if(i > blocksize) {
+ if(!process_frame_(encoder, /*is_fractional_block=*/false, /*is_last_block=*/false))
+ return false;
+ /* move unprocessed overread samples to beginnings of arrays */
+ FLAC__ASSERT(i == blocksize+OVERREAD_);
+ FLAC__ASSERT(OVERREAD_ == 1); /* assert we only overread 1 sample which simplifies the rest of the code below */
+ for(channel = 0; channel < channels; channel++)
+ encoder->private_->integer_signal[channel][0] = encoder->private_->integer_signal[channel][blocksize];
+ encoder->private_->current_sample_number = 1;
+ }
+ } while(j < samples);
}
return true;
encoder->protected_->verify = false;
#endif
encoder->protected_->streamable_subset = true;
+ encoder->protected_->do_md5 = true;
encoder->protected_->do_mid_side_stereo = false;
encoder->protected_->loose_mid_side_stereo = false;
encoder->protected_->channels = 2;
#if FLAC__HAS_OGG
FLAC__ogg_encoder_aspect_set_defaults(&encoder->protected_->ogg_encoder_aspect);
#endif
+
+ FLAC__stream_encoder_set_compression_level(encoder, 5);
}
void free_(FLAC__StreamEncoder *encoder)
memset(encoder->private_->integer_signal[i], 0, sizeof(FLAC__int32)*4);
encoder->private_->integer_signal[i] += 4;
#ifndef FLAC__INTEGER_ONLY_LIBRARY
+#if 0 /* @@@ currently unused */
if(encoder->protected_->max_lpc_order > 0)
ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_unaligned[i], &encoder->private_->real_signal[i]);
#endif
+#endif
}
for(i = 0; ok && i < 2; i++) {
ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize+4+OVERREAD_, &encoder->private_->integer_signal_mid_side_unaligned[i], &encoder->private_->integer_signal_mid_side[i]);
memset(encoder->private_->integer_signal_mid_side[i], 0, sizeof(FLAC__int32)*4);
encoder->private_->integer_signal_mid_side[i] += 4;
#ifndef FLAC__INTEGER_ONLY_LIBRARY
+#if 0 /* @@@ currently unused */
if(encoder->protected_->max_lpc_order > 0)
ok = ok && FLAC__memory_alloc_aligned_real_array(new_blocksize+OVERREAD_, &encoder->private_->real_signal_mid_side_unaligned[i], &encoder->private_->real_signal_mid_side[i]);
#endif
+#endif
}
#ifndef FLAC__INTEGER_ONLY_LIBRARY
if(ok && encoder->protected_->max_lpc_order > 0) {
ok = ok && FLAC__memory_alloc_aligned_int32_array(new_blocksize, &encoder->private_->residual_workspace_mid_side_unaligned[channel][i], &encoder->private_->residual_workspace_mid_side[channel][i]);
}
}
- /* @@@@@@@@@ blocksize*2 is too pessimistic, but to fix, we need smarter logic because a smaller new_blocksize can actually increase the # of partitions */
+ /* the *2 is an approximation to the series 1 + 1/2 + 1/4 + ... that sums tree occupies in a flat array */
+ /*@@@ new_blocksize*2 is too pessimistic, but to fix, we need smarter logic because a smaller new_blocksize can actually increase the # of partitions; would require moving this out into a separate function, then checking its capacity against the need of the current blocksize&min/max_partition_order (and maybe predictor order) */
ok = ok && FLAC__memory_alloc_aligned_uint64_array(new_blocksize * 2, &encoder->private_->abs_residual_partition_sums_unaligned, &encoder->private_->abs_residual_partition_sums);
if(encoder->protected_->do_escape_coding)
ok = ok && FLAC__memory_alloc_aligned_unsigned_array(new_blocksize * 2, &encoder->private_->raw_bits_per_partition_unaligned, &encoder->private_->raw_bits_per_partition);
FLAC__bitwriter_clear(encoder->private_->frame);
if(samples > 0) {
- encoder->private_->streaminfo.data.stream_info.min_framesize = min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
- encoder->private_->streaminfo.data.stream_info.max_framesize = max(bytes, encoder->private_->streaminfo.data.stream_info.max_framesize);
+ encoder->private_->streaminfo.data.stream_info.min_framesize = flac_min(bytes, encoder->private_->streaminfo.data.stream_info.min_framesize);
+ encoder->private_->streaminfo.data.stream_info.max_framesize = flac_max(bytes, encoder->private_->streaminfo.data.stream_info.max_framesize);
}
return true;
* when the encoder goes back to write metadata, 'current_frame'
* will drop back to 0.
*/
- encoder->private_->frames_written = max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
+ encoder->private_->frames_written = flac_max(encoder->private_->frames_written, encoder->private_->current_frame_number+1);
}
else
encoder->protected_->state = FLAC__STREAM_ENCODER_CLIENT_ERROR;
/* Gets called when the encoding process has finished so that we can update the STREAMINFO and SEEKTABLE blocks. */
void update_metadata_(const FLAC__StreamEncoder *encoder)
{
- FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
+ FLAC__byte b[flac_max(6u, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
const unsigned min_framesize = metadata->data.stream_info.min_framesize;
FLAC__OGG_MAPPING_NUM_HEADERS_LENGTH +
FLAC__STREAM_SYNC_LENGTH
;
- FLAC__byte b[max(6, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
+ FLAC__byte b[flac_max(6u, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)];
const FLAC__StreamMetadata *metadata = &encoder->private_->streaminfo;
const FLAC__uint64 samples = metadata->data.stream_info.total_samples;
const unsigned min_framesize = metadata->data.stream_info.min_framesize;
/*
* Accumulate raw signal to the MD5 signature
*/
- if(!FLAC__MD5Accumulate(&encoder->private_->md5context, (const FLAC__int32 * const *)encoder->private_->integer_signal, encoder->protected_->channels, encoder->protected_->blocksize, (encoder->protected_->bits_per_sample+7) / 8)) {
+ if(encoder->protected_->do_md5 && !FLAC__MD5Accumulate(&encoder->private_->md5context, (const FLAC__int32 * const *)encoder->private_->integer_signal, encoder->protected_->channels, encoder->protected_->blocksize, (encoder->protected_->bits_per_sample+7) / 8)) {
encoder->protected_->state = FLAC__STREAM_ENCODER_MEMORY_ALLOCATION_ERROR;
return false;
}
}
else {
max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize(encoder->protected_->blocksize);
- max_partition_order = min(max_partition_order, encoder->protected_->max_residual_partition_order);
+ max_partition_order = flac_min(max_partition_order, encoder->protected_->max_residual_partition_order);
}
- min_partition_order = min(min_partition_order, max_partition_order);
+ min_partition_order = flac_min(min_partition_order, max_partition_order);
/*
* Setup the frame
&frame_header,
encoder->private_->subframe_bps[channel],
encoder->private_->integer_signal[channel],
-#ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->real_signal[channel],
-#endif
encoder->private_->subframe_workspace_ptr[channel],
encoder->private_->partitioned_rice_contents_workspace_ptr[channel],
encoder->private_->residual_workspace[channel],
&frame_header,
encoder->private_->subframe_bps_mid_side[channel],
encoder->private_->integer_signal_mid_side[channel],
-#ifndef FLAC__INTEGER_ONLY_LIBRARY
- encoder->private_->real_signal_mid_side[channel],
-#endif
encoder->private_->subframe_workspace_ptr_mid_side[channel],
encoder->private_->partitioned_rice_contents_workspace_ptr_mid_side[channel],
encoder->private_->residual_workspace_mid_side[channel],
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 rice_parameter;
unsigned _candidate_bits, _best_bits;
unsigned _best_subframe;
+ /* only use RICE2 partitions if stream bps > 16 */
+ const unsigned rice_parameter_limit = FLAC__stream_encoder_get_bits_per_sample(encoder) > 16? FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER : FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
FLAC__ASSERT(frame_header->blocksize > 0);
rice_parameter = (fixed_residual_bits_per_sample[fixed_order] > FLAC__FP_ZERO)? (unsigned)FLAC__fixedpoint_trunc(fixed_residual_bits_per_sample[fixed_order]+FLAC__FP_ONE_HALF) : 0; /* 0.5 is for rounding */
#endif
rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
- if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
+ if(rice_parameter >= rice_parameter_limit) {
#ifdef DEBUG_VERBOSE
- fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
+ fprintf(stderr, "clipping rice_parameter (%u -> %u) @0\n", rice_parameter, rice_parameter_limit - 1);
#endif
- rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
+ rice_parameter = rice_parameter_limit - 1;
}
_candidate_bits =
evaluate_fixed_subframe_(
subframe_bps,
fixed_order,
rice_parameter,
+ rice_parameter_limit,
min_partition_order,
max_partition_order,
encoder->protected_->do_escape_coding,
if(max_lpc_order > 0) {
unsigned a;
for (a = 0; a < encoder->protected_->num_apodizations; a++) {
- FLAC__lpc_window_data(real_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
+ FLAC__lpc_window_data(integer_signal, encoder->private_->window[a], encoder->private_->windowed_signal, frame_header->blocksize);
encoder->private_->local_lpc_compute_autocorrelation(encoder->private_->windowed_signal, frame_header->blocksize, max_lpc_order+1, autoc);
/* if autoc[0] == 0.0, the signal is constant and we usually won't get here, but it can happen */
if(autoc[0] != 0.0) {
continue; /* don't even try */
rice_parameter = (lpc_residual_bits_per_sample > 0.0)? (unsigned)(lpc_residual_bits_per_sample+0.5) : 0; /* 0.5 is for rounding */
rice_parameter++; /* to account for the signed->unsigned conversion during rice coding */
- if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
+ if(rice_parameter >= rice_parameter_limit) {
#ifdef DEBUG_VERBOSE
- fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
+ fprintf(stderr, "clipping rice_parameter (%u -> %u) @1\n", rice_parameter, rice_parameter_limit - 1);
#endif
- rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
+ rice_parameter = rice_parameter_limit - 1;
}
if(encoder->protected_->do_qlp_coeff_prec_search) {
min_qlp_coeff_precision = FLAC__MIN_QLP_COEFF_PRECISION;
/* try to ensure a 32-bit datapath throughout for 16bps(+1bps for side channel) or less */
if(subframe_bps <= 17) {
- max_qlp_coeff_precision = min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
- max_qlp_coeff_precision = max(max_qlp_coeff_precision, min_qlp_coeff_precision);
+ max_qlp_coeff_precision = flac_min(32 - subframe_bps - lpc_order, FLAC__MAX_QLP_COEFF_PRECISION);
+ max_qlp_coeff_precision = flac_max(max_qlp_coeff_precision, min_qlp_coeff_precision);
}
else
max_qlp_coeff_precision = FLAC__MAX_QLP_COEFF_PRECISION;
lpc_order,
qlp_coeff_precision,
rice_parameter,
+ rice_parameter_limit,
min_partition_order,
max_partition_order,
encoder->protected_->do_escape_coding,
return true;
}
-#define SPOTCHECK_ESTIMATE 0 //@@@@@@@@@
+#define SPOTCHECK_ESTIMATE 0
#if SPOTCHECK_ESTIMATE
static void spotcheck_subframe_estimate_(
FLAC__StreamEncoder *encoder,
unsigned subframe_bps,
unsigned order,
unsigned rice_parameter,
+ unsigned rice_parameter_limit,
unsigned min_partition_order,
unsigned max_partition_order,
FLAC__bool do_escape_coding,
residual_samples,
order,
rice_parameter,
+ rice_parameter_limit,
min_partition_order,
max_partition_order,
+ subframe_bps,
do_escape_coding,
rice_parameter_search_dist,
- &subframe->data.fixed.entropy_coding_method.data.partitioned_rice
+ &subframe->data.fixed.entropy_coding_method
);
subframe->data.fixed.order = order;
unsigned order,
unsigned qlp_coeff_precision,
unsigned rice_parameter,
+ unsigned rice_parameter_limit,
unsigned min_partition_order,
unsigned max_partition_order,
FLAC__bool do_escape_coding,
if(subframe_bps <= 16) {
FLAC__ASSERT(order > 0);
FLAC__ASSERT(order <= FLAC__MAX_LPC_ORDER);
- qlp_coeff_precision = min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
+ qlp_coeff_precision = flac_min(qlp_coeff_precision, 32 - subframe_bps - FLAC__bitmath_ilog2(order));
}
ret = FLAC__lpc_quantize_coefficients(lp_coeff, order, qlp_coeff_precision, qlp_coeff, &quantization);
residual_samples,
order,
rice_parameter,
+ rice_parameter_limit,
min_partition_order,
max_partition_order,
+ subframe_bps,
do_escape_coding,
rice_parameter_search_dist,
- &subframe->data.lpc.entropy_coding_method.data.partitioned_rice
+ &subframe->data.lpc.entropy_coding_method
);
subframe->data.lpc.order = order;
unsigned residual_samples,
unsigned predictor_order,
unsigned rice_parameter,
+ unsigned rice_parameter_limit,
unsigned min_partition_order,
unsigned max_partition_order,
+ unsigned bps,
FLAC__bool do_escape_coding,
unsigned rice_parameter_search_dist,
- FLAC__EntropyCodingMethod_PartitionedRice *best_partitioned_rice
+ FLAC__EntropyCodingMethod *best_ecm
)
{
unsigned residual_bits, best_residual_bits = 0;
unsigned best_parameters_index = 0;
+ unsigned best_partition_order = 0;
const unsigned blocksize = residual_samples + predictor_order;
max_partition_order = FLAC__format_get_max_rice_partition_order_from_blocksize_limited_max_and_predictor_order(max_partition_order, blocksize, predictor_order);
- min_partition_order = min(min_partition_order, max_partition_order);
+ min_partition_order = flac_min(min_partition_order, max_partition_order);
- precompute_partition_info_sums_(residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order);
+ precompute_partition_info_sums_(residual, abs_residual_partition_sums, residual_samples, predictor_order, min_partition_order, max_partition_order, bps);
if(do_escape_coding)
precompute_partition_info_escapes_(residual, raw_bits_per_partition, residual_samples, predictor_order, min_partition_order, max_partition_order);
residual_samples,
predictor_order,
rice_parameter,
+ rice_parameter_limit,
rice_parameter_search_dist,
(unsigned)partition_order,
do_escape_coding,
if(best_residual_bits == 0 || residual_bits < best_residual_bits) {
best_residual_bits = residual_bits;
best_parameters_index = !best_parameters_index;
- best_partitioned_rice->order = partition_order;
+ best_partition_order = partition_order;
}
}
}
- /*
- * We are allowed to de-const the pointer based on our special knowledge;
- * it is const to the outside world.
- */
+ best_ecm->data.partitioned_rice.order = best_partition_order;
+
{
- FLAC__EntropyCodingMethod_PartitionedRiceContents* best_partitioned_rice_contents = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_partitioned_rice->contents;
- FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(best_partitioned_rice_contents, max(6, best_partitioned_rice->order));
- memcpy(best_partitioned_rice_contents->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
- memcpy(best_partitioned_rice_contents->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partitioned_rice->order)));
+ /*
+ * We are allowed to de-const the pointer based on our special
+ * knowledge; it is const to the outside world.
+ */
+ FLAC__EntropyCodingMethod_PartitionedRiceContents* prc = (FLAC__EntropyCodingMethod_PartitionedRiceContents*)best_ecm->data.partitioned_rice.contents;
+ unsigned partition;
+
+ /* save best parameters and raw_bits */
+ FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(prc, flac_max(6u, best_partition_order));
+ memcpy(prc->parameters, private_->partitioned_rice_contents_extra[best_parameters_index].parameters, sizeof(unsigned)*(1<<(best_partition_order)));
+ if(do_escape_coding)
+ memcpy(prc->raw_bits, private_->partitioned_rice_contents_extra[best_parameters_index].raw_bits, sizeof(unsigned)*(1<<(best_partition_order)));
+ /*
+ * Now need to check if the type should be changed to
+ * FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2 based on the
+ * size of the rice parameters.
+ */
+ for(partition = 0; partition < (1u<<best_partition_order); partition++) {
+ if(prc->parameters[partition] >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
+ best_ecm->type = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2;
+ break;
+ }
+ }
}
return best_residual_bits;
}
+#if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM
+extern void precompute_partition_info_sums_32bit_asm_ia32_(
+ const FLAC__int32 residual[],
+ FLAC__uint64 abs_residual_partition_sums[],
+ unsigned blocksize,
+ unsigned predictor_order,
+ unsigned min_partition_order,
+ unsigned max_partition_order
+);
+#endif
+
void precompute_partition_info_sums_(
const FLAC__int32 residual[],
FLAC__uint64 abs_residual_partition_sums[],
unsigned residual_samples,
unsigned predictor_order,
unsigned min_partition_order,
- unsigned max_partition_order
+ unsigned max_partition_order,
+ unsigned bps
)
{
- int partition_order;
- unsigned from_partition, to_partition = 0;
- const unsigned blocksize = residual_samples + predictor_order;
+ const unsigned default_partition_samples = (residual_samples + predictor_order) >> max_partition_order;
+ unsigned partitions = 1u << max_partition_order;
- /* first do max_partition_order */
- for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
- FLAC__uint64 abs_residual_partition_sum; /* OPT: can reasonably be FLAC__uint32 for bps <= 17 and maybe higher */
- unsigned partition, partition_sample, partition_samples, residual_sample;
- const unsigned partitions = 1u << partition_order;
- const unsigned default_partition_samples = blocksize >> partition_order;
+ FLAC__ASSERT(default_partition_samples > predictor_order);
- FLAC__ASSERT(default_partition_samples > predictor_order);
-
- for(partition = residual_sample = 0; partition < partitions; partition++) {
- partition_samples = default_partition_samples;
- if(partition == 0)
- partition_samples -= predictor_order;
- abs_residual_partition_sum = 0;
- for(partition_sample = 0; partition_sample < partition_samples; partition_sample++, residual_sample++) {
-#if defined _MSC_VER && _MSC_VER <= 1200
- /* OPT: abs() may be faster for some compilers */
- abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */
-#else
- const FLAC__int32 r = residual[residual_sample];
- if(r < 0)
- abs_residual_partition_sum -= r;
- else
- abs_residual_partition_sum += r;
+#if defined(FLAC__CPU_IA32) && !defined FLAC__NO_ASM && defined FLAC__HAS_NASM
+ /* slightly pessimistic but still catches all common cases */
+ /* WATCHOUT: "+ bps" is an assumption that the average residual magnitude will not be more than "bps" bits */
+ if(FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) {
+ precompute_partition_info_sums_32bit_asm_ia32_(residual, abs_residual_partition_sums, residual_samples + predictor_order, predictor_order, min_partition_order, max_partition_order);
+ return;
+ }
#endif
+
+ /* first do max_partition_order */
+ {
+ unsigned partition, residual_sample, end = (unsigned)(-(int)predictor_order);
+ /* slightly pessimistic but still catches all common cases */
+ /* WATCHOUT: "+ bps" is an assumption that the average residual magnitude will not be more than "bps" bits */
+ if(FLAC__bitmath_ilog2(default_partition_samples) + bps < 32) {
+ FLAC__uint32 abs_residual_partition_sum;
+
+ for(partition = residual_sample = 0; partition < partitions; partition++) {
+ end += default_partition_samples;
+ abs_residual_partition_sum = 0;
+ for( ; residual_sample < end; residual_sample++)
+ abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */
+ abs_residual_partition_sums[partition] = abs_residual_partition_sum;
+ }
+ }
+ else { /* have to pessimistically use 64 bits for accumulator */
+ FLAC__uint64 abs_residual_partition_sum;
+
+ for(partition = residual_sample = 0; partition < partitions; partition++) {
+ end += default_partition_samples;
+ abs_residual_partition_sum = 0;
+ for( ; residual_sample < end; residual_sample++)
+ abs_residual_partition_sum += abs(residual[residual_sample]); /* abs(INT_MIN) is undefined, but if the residual is INT_MIN we have bigger problems */
+ abs_residual_partition_sums[partition] = abs_residual_partition_sum;
}
- abs_residual_partition_sums[partition] = abs_residual_partition_sum;
}
- to_partition = partitions;
- break;
}
/* now merge partitions for lower orders */
- for(from_partition = 0, --partition_order; partition_order >= (int)min_partition_order; partition_order--) {
- FLAC__uint64 s;
- unsigned i;
- const unsigned partitions = 1u << partition_order;
- for(i = 0; i < partitions; i++) {
- s = abs_residual_partition_sums[from_partition];
- from_partition++;
- abs_residual_partition_sums[to_partition] = s + abs_residual_partition_sums[from_partition];
- from_partition++;
- to_partition++;
+ {
+ unsigned from_partition = 0, to_partition = partitions;
+ int partition_order;
+ for(partition_order = (int)max_partition_order - 1; partition_order >= (int)min_partition_order; partition_order--) {
+ unsigned i;
+ partitions >>= 1;
+ for(i = 0; i < partitions; i++) {
+ abs_residual_partition_sums[to_partition++] =
+ abs_residual_partition_sums[from_partition ] +
+ abs_residual_partition_sums[from_partition+1];
+ from_partition += 2;
+ }
}
}
}
/* first do max_partition_order */
for(partition_order = (int)max_partition_order; partition_order >= 0; partition_order--) {
- FLAC__int32 r, residual_partition_min, residual_partition_max;
- unsigned silog2_min, silog2_max;
+ FLAC__int32 r;
+ FLAC__uint32 rmax;
unsigned partition, partition_sample, partition_samples, residual_sample;
const unsigned partitions = 1u << partition_order;
const unsigned default_partition_samples = blocksize >> partition_order;
partition_samples = default_partition_samples;
if(partition == 0)
partition_samples -= predictor_order;
- residual_partition_min = residual_partition_max = 0;
+ rmax = 0;
for(partition_sample = 0; partition_sample < partition_samples; partition_sample++) {
- r = residual[residual_sample];
- if(r < residual_partition_min)
- residual_partition_min = r;
- else if(r > residual_partition_max)
- residual_partition_max = r;
- residual_sample++;
+ r = residual[residual_sample++];
+ /* OPT: maybe faster: rmax |= r ^ (r>>31) */
+ if(r < 0)
+ rmax |= ~r;
+ else
+ rmax |= r;
}
- silog2_min = FLAC__bitmath_silog2(residual_partition_min);
- silog2_max = FLAC__bitmath_silog2(residual_partition_max);
- raw_bits_per_partition[partition] = max(silog2_min, silog2_max);
+ /* now we know all residual values are in the range [-rmax-1,rmax] */
+ raw_bits_per_partition[partition] = rmax? FLAC__bitmath_ilog2(rmax) + 2 : 1;
}
to_partition = partitions;
break; /*@@@ yuck, should remove the 'for' loop instead */
for(i = 0; i < partitions; i++) {
m = raw_bits_per_partition[from_partition];
from_partition++;
- raw_bits_per_partition[to_partition] = max(m, raw_bits_per_partition[from_partition]);
+ raw_bits_per_partition[to_partition] = flac_max(m, raw_bits_per_partition[from_partition]);
from_partition++;
to_partition++;
}
}
}
-/*@@@@@@ overflow is a possible problem here for hi-res samples */
#ifdef EXACT_RICE_BITS_CALCULATION
-static __inline unsigned count_rice_bits_in_partition_(
+static inline unsigned count_rice_bits_in_partition_(
const unsigned rice_parameter,
const unsigned partition_samples,
const FLAC__int32 *residual
)
{
unsigned i, partition_bits =
- FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN +
+ FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + /* actually could end up being FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN but err on side of 16bps */
(1+rice_parameter) * partition_samples /* 1 for unary stop bit + rice_parameter for the binary portion */
;
for(i = 0; i < partition_samples; i++)
return partition_bits;
}
#else
-static __inline unsigned count_rice_bits_in_partition_(
+static inline unsigned count_rice_bits_in_partition_(
const unsigned rice_parameter,
const unsigned partition_samples,
const FLAC__uint64 abs_residual_partition_sum
)
{
return
- FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN +
+ FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + /* actually could end up being FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN but err on side of 16bps */
(1+rice_parameter) * partition_samples + /* 1 for unary stop bit + rice_parameter for the binary portion */
(
rice_parameter?
)
- (partition_samples >> 1)
/* -(partition_samples>>1) to subtract out extra contributions to the abs_residual_partition_sum.
- * The actual number of bits used is closer to the sum for all i in the partition of abs(residual[i])>>(rice_parameter-1)
+ * The actual number of bits used is closer to the sum(for all i in the partition) of abs(residual[i])>>(rice_parameter-1)
* By using the abs_residual_partition sum, we also add in bits in the LSBs that would normally be shifted out.
* So the subtraction term tries to guess how many extra bits were contributed.
* If the LSBs are randomly distributed, this should average to 0.5 extra bits per sample.
const unsigned residual_samples,
const unsigned predictor_order,
const unsigned suggested_rice_parameter,
+ const unsigned rice_parameter_limit,
const unsigned rice_parameter_search_dist,
const unsigned partition_order,
const FLAC__bool search_for_escapes,
{
unsigned rice_parameter, partition_bits;
unsigned best_partition_bits, best_rice_parameter = 0;
- unsigned flat_bits;
unsigned bits_ = FLAC__ENTROPY_CODING_METHOD_TYPE_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN;
unsigned *parameters, *raw_bits;
#ifdef ENABLE_RICE_PARAMETER_SEARCH
(void)rice_parameter_search_dist;
#endif
- FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER);
+ FLAC__ASSERT(suggested_rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER);
+ FLAC__ASSERT(rice_parameter_limit <= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_ESCAPE_PARAMETER);
- FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order));
+ FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, flac_max(6u, partition_order));
parameters = partitioned_rice_contents->parameters;
raw_bits = partitioned_rice_contents->raw_bits;
if(partition_order == 0) {
- best_partition_bits = 0xffffffff;
+ best_partition_bits = (unsigned)(-1);
#ifdef ENABLE_RICE_PARAMETER_SEARCH
if(rice_parameter_search_dist) {
if(suggested_rice_parameter < rice_parameter_search_dist)
else
min_rice_parameter = suggested_rice_parameter - rice_parameter_search_dist;
max_rice_parameter = suggested_rice_parameter + rice_parameter_search_dist;
- if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
+ if(max_rice_parameter >= rice_parameter_limit) {
#ifdef DEBUG_VERBOSE
- fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
+ fprintf(stderr, "clipping rice_parameter (%u -> %u) @5\n", max_rice_parameter, rice_parameter_limit - 1);
#endif
- max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
+ max_rice_parameter = rice_parameter_limit - 1;
}
}
else
}
#endif
if(search_for_escapes) {
- flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples;
- if(flat_bits <= best_partition_bits) {
+ partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[0] * residual_samples;
+ if(partition_bits <= best_partition_bits) {
raw_bits[0] = raw_bits_per_partition[0];
- best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
- best_partition_bits = flat_bits;
+ best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */
+ best_partition_bits = partition_bits;
}
+ else
+ raw_bits[0] = 0;
}
parameters[0] = best_rice_parameter;
bits_ += best_partition_bits;
*/
for(rice_parameter = 0, k = partition_samples; k < mean; rice_parameter++, k <<= 1)
;
- if(rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
+ if(rice_parameter >= rice_parameter_limit) {
#ifdef DEBUG_VERBOSE
- fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
+ fprintf(stderr, "clipping rice_parameter (%u -> %u) @6\n", rice_parameter, rice_parameter_limit - 1);
#endif
- rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
+ rice_parameter = rice_parameter_limit - 1;
}
- best_partition_bits = 0xffffffff;
+ best_partition_bits = (unsigned)(-1);
#ifdef ENABLE_RICE_PARAMETER_SEARCH
if(rice_parameter_search_dist) {
if(rice_parameter < rice_parameter_search_dist)
else
min_rice_parameter = rice_parameter - rice_parameter_search_dist;
max_rice_parameter = rice_parameter + rice_parameter_search_dist;
- if(max_rice_parameter >= FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
+ if(max_rice_parameter >= rice_parameter_limit) {
#ifdef DEBUG_VERBOSE
- fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1);
+ fprintf(stderr, "clipping rice_parameter (%u -> %u) @7\n", max_rice_parameter, rice_parameter_limit - 1);
#endif
- max_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER - 1;
+ max_rice_parameter = rice_parameter_limit - 1;
}
}
else
}
#endif
if(search_for_escapes) {
- flat_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples;
- if(flat_bits <= best_partition_bits) {
+ partition_bits = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE2_PARAMETER_LEN + FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN + raw_bits_per_partition[partition] * partition_samples;
+ if(partition_bits <= best_partition_bits) {
raw_bits[partition] = raw_bits_per_partition[partition];
- best_rice_parameter = FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER;
- best_partition_bits = flat_bits;
+ best_rice_parameter = 0; /* will be converted to appropriate escape parameter later */
+ best_partition_bits = partition_bits;
}
+ else
+ raw_bits[partition] = 0;
}
parameters[partition] = best_rice_parameter;
bits_ += best_partition_bits;
/*
* This will forcibly set stdout to binary mode (for OSes that require it)
*/
-FILE *get_binary_stdout_()
+FILE *get_binary_stdout_(void)
{
/* if something breaks here it is probably due to the presence or
* absence of an underscore before the identifiers 'setmode',