1e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat/* libFLAC - Free Lossless Audio Codec library 2e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * Copyright (C) 2000-2009 Josh Coalson 3e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * Copyright (C) 2011-2014 Xiph.Org Foundation 4e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * 5e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * Redistribution and use in source and binary forms, with or without 6e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * modification, are permitted provided that the following conditions 7e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * are met: 8e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * 9e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * - Redistributions of source code must retain the above copyright 10e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * notice, this list of conditions and the following disclaimer. 11e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * 12e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * - Redistributions in binary form must reproduce the above copyright 13e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * notice, this list of conditions and the following disclaimer in the 14e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * documentation and/or other materials provided with the distribution. 15e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * 16e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * - Neither the name of the Xiph.org Foundation nor the names of its 17e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * contributors may be used to endorse or promote products derived from 18e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * this software without specific prior written permission. 19e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * 20e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR 24e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat */ 32e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 33e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#ifdef HAVE_CONFIG_H 34e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat# include <config.h> 35e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#endif 36e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 37e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#include <math.h> 38e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 39e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#include "FLAC/assert.h" 40e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#include "FLAC/format.h" 41e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#include "share/compat.h" 42e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#include "private/bitmath.h" 43e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#include "private/lpc.h" 44e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#include "private/macros.h" 45e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#if defined DEBUG || defined FLAC__OVERFLOW_DETECT || defined FLAC__OVERFLOW_DETECT_VERBOSE 46e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#include <stdio.h> 47e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#endif 48e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 49e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat/* OPT: #undef'ing this may improve the speed on some architectures */ 50e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#define FLAC__LPC_UNROLLED_FILTER_LOOPS 51e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 52e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#ifndef FLAC__INTEGER_ONLY_LIBRARY 53e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 54e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#if !defined(HAVE_LROUND) 55e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#if defined(_MSC_VER) 56e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#include <float.h> 57e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#define copysign _copysign 58e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#elif defined(__GNUC__) 59e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#define copysign __builtin_copysign 60e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#endif 61e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehatstatic inline long int lround(double x) { 62e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat return (long)(x + copysign (0.5, x)); 63e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat} 64e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat/* If this fails, we are in the presence of a mid 90's compiler, move along... */ 65e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#endif 66e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 67e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehatvoid FLAC__lpc_window_data(const FLAC__int32 in[], const FLAC__real window[], FLAC__real out[], unsigned data_len) 68e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat{ 69e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat unsigned i; 70e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = 0; i < data_len; i++) 71e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat out[i] = in[i] * window[i]; 72e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat} 73e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 74e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehatvoid FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]) 75e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat{ 76e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat /* a readable, but slower, version */ 77e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#if 0 78e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__real d; 79e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat unsigned i; 80e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 81e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__ASSERT(lag > 0); 82e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__ASSERT(lag <= data_len); 83e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 84e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat /* 85e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * Technically we should subtract the mean first like so: 86e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * for(i = 0; i < data_len; i++) 87e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * data[i] -= mean; 88e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * but it appears not to make enough of a difference to matter, and 89e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * most signals are already closely centered around zero 90e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat */ 91e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat while(lag--) { 92e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = lag, d = 0.0; i < data_len; i++) 93e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat d += data[i] * data[i - lag]; 94e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat autoc[lag] = d; 95e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 96e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#endif 97e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 98e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat /* 99e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * this version tends to run faster because of better data locality 100e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * ('data_len' is usually much larger than 'lag') 101e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat */ 102e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__real d; 103e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat unsigned sample, coeff; 104e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat const unsigned limit = data_len - lag; 105e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 106e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__ASSERT(lag > 0); 107e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__ASSERT(lag <= data_len); 108e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 109e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(coeff = 0; coeff < lag; coeff++) 110e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat autoc[coeff] = 0.0; 111e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(sample = 0; sample <= limit; sample++) { 112e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat d = data[sample]; 113e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(coeff = 0; coeff < lag; coeff++) 114e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat autoc[coeff] += d * data[sample+coeff]; 115e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 116e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(; sample < data_len; sample++) { 117e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat d = data[sample]; 118e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(coeff = 0; coeff < data_len - sample; coeff++) 119e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat autoc[coeff] += d * data[sample+coeff]; 120e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 121e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat} 122e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 123e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehatvoid FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned *max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__double error[]) 124e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat{ 125e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat unsigned i, j; 126e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__double r, err, lpc[FLAC__MAX_LPC_ORDER]; 127e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 128e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__ASSERT(0 != max_order); 129e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__ASSERT(0 < *max_order); 130e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__ASSERT(*max_order <= FLAC__MAX_LPC_ORDER); 131e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__ASSERT(autoc[0] != 0.0); 132e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 133e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat err = autoc[0]; 134e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 135e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = 0; i < *max_order; i++) { 136e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat /* Sum up this iteration's reflection coefficient. */ 137e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat r = -autoc[i+1]; 138e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(j = 0; j < i; j++) 139e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat r -= lpc[j] * autoc[i-j]; 140e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat r /= err; 141e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 142e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat /* Update LPC coefficients and total error. */ 143e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat lpc[i]=r; 144e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(j = 0; j < (i>>1); j++) { 145e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__double tmp = lpc[j]; 146e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat lpc[j] += r * lpc[i-1-j]; 147e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat lpc[i-1-j] += r * tmp; 148e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 149e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(i & 1) 150e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat lpc[j] += lpc[j] * r; 151e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 152e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat err *= (1.0 - r * r); 153e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 154e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat /* save this order */ 155e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(j = 0; j <= i; j++) 156e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat lp_coeff[i][j] = (FLAC__real)(-lpc[j]); /* negate FIR filter coeff to get predictor coeff */ 157e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat error[i] = err; 158e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 159e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat /* see SF bug https://sourceforge.net/p/flac/bugs/234/ */ 160e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(err == 0.0) { 161e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat *max_order = i+1; 162e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat return; 163e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 164e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 165e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat} 166e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 167e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehatint FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, FLAC__int32 qlp_coeff[], int *shift) 168e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat{ 169e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat unsigned i; 170e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__double cmax; 171e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__int32 qmax, qmin; 172e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 173e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__ASSERT(precision > 0); 174e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__ASSERT(precision >= FLAC__MIN_QLP_COEFF_PRECISION); 175e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 176e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat /* drop one bit for the sign; from here on out we consider only |lp_coeff[i]| */ 177e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat precision--; 178e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat qmax = 1 << precision; 179e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat qmin = -qmax; 180e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat qmax--; 181e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 182e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat /* calc cmax = max( |lp_coeff[i]| ) */ 183e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat cmax = 0.0; 184e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = 0; i < order; i++) { 185e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat const FLAC__double d = fabs(lp_coeff[i]); 186e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(d > cmax) 187e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat cmax = d; 188e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 189e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 190e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(cmax <= 0.0) { 191e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat /* => coefficients are all 0, which means our constant-detect didn't work */ 192e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat return 2; 193e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 194e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat else { 195e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat const int max_shiftlimit = (1 << (FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN-1)) - 1; 196e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat const int min_shiftlimit = -max_shiftlimit - 1; 197e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat int log2cmax; 198e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 199e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat (void)frexp(cmax, &log2cmax); 200e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat log2cmax--; 201e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat *shift = (int)precision - log2cmax - 1; 202e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 203e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(*shift > max_shiftlimit) 204e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat *shift = max_shiftlimit; 205e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat else if(*shift < min_shiftlimit) 206e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat return 1; 207e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 208e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 209e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(*shift >= 0) { 210e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__double error = 0.0; 211e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__int32 q; 212e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = 0; i < order; i++) { 213e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat error += lp_coeff[i] * (1 << *shift); 214e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat q = lround(error); 215e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 216e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#ifdef FLAC__OVERFLOW_DETECT 217e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */ 218e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]); 219e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat else if(q < qmin) 220e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]); 221e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#endif 222e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(q > qmax) 223e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat q = qmax; 224e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat else if(q < qmin) 225e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat q = qmin; 226e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat error -= q; 227e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat qlp_coeff[i] = q; 228e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 229e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 230e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat /* negative shift is very rare but due to design flaw, negative shift is 231e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * a NOP in the decoder, so it must be handled specially by scaling down 232e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * coeffs 233e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat */ 234e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat else { 235e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat const int nshift = -(*shift); 236e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__double error = 0.0; 237e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__int32 q; 238e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#ifdef DEBUG 239e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat fprintf(stderr,"FLAC__lpc_quantize_coefficients: negative shift=%d order=%u cmax=%f\n", *shift, order, cmax); 240e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#endif 241e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = 0; i < order; i++) { 242e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat error += lp_coeff[i] / (1 << nshift); 243e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat q = lround(error); 244e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#ifdef FLAC__OVERFLOW_DETECT 245e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(q > qmax+1) /* we expect q==qmax+1 occasionally due to rounding */ 246e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q>qmax %d>%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmax,*shift,cmax,precision+1,i,lp_coeff[i]); 247e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat else if(q < qmin) 248e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat fprintf(stderr,"FLAC__lpc_quantize_coefficients: quantizer overflow: q<qmin %d<%d shift=%d cmax=%f precision=%u lpc[%u]=%f\n",q,qmin,*shift,cmax,precision+1,i,lp_coeff[i]); 249e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#endif 250e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(q > qmax) 251e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat q = qmax; 252e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat else if(q < qmin) 253e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat q = qmin; 254e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat error -= q; 255e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat qlp_coeff[i] = q; 256e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 257e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat *shift = 0; 258e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 259e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 260e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat return 0; 261e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat} 262e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 263e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#if defined(_MSC_VER) 264e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat// silence MSVC warnings about __restrict modifier 265e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#pragma warning ( disable : 4028 ) 266e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#endif 267e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 268e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehatvoid FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 * flac_restrict data, unsigned data_len, const FLAC__int32 * flac_restrict qlp_coeff, unsigned order, int lp_quantization, FLAC__int32 * flac_restrict residual) 269e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS) 270e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat{ 271e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__int64 sumo; 272e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat unsigned i, j; 273e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__int32 sum; 274e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat const FLAC__int32 *history; 275e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 276e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#ifdef FLAC__OVERFLOW_DETECT_VERBOSE 277e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization); 278e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i=0;i<order;i++) 279e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]); 280e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat fprintf(stderr,"\n"); 281e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#endif 282e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__ASSERT(order > 0); 283e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 284e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = 0; i < data_len; i++) { 285e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sumo = 0; 286e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum = 0; 287e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat history = data; 288e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(j = 0; j < order; j++) { 289e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[j] * (*(--history)); 290e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history); 291e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(sumo > 2147483647ll || sumo < -2147483648ll) 292e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%" PRId64 "\n",i,j,qlp_coeff[j],*history,sumo); 293e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 294e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat *(residual++) = *(data++) - (sum >> lp_quantization); 295e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 296e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 297e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat /* Here's a slower but clearer version: 298e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = 0; i < data_len; i++) { 299e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum = 0; 300e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(j = 0; j < order; j++) 301e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[j] * data[i-j-1]; 302e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat residual[i] = data[i] - (sum >> lp_quantization); 303e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 304e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat */ 305e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat} 306e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat#else /* fully unrolled version for normal use */ 307e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat{ 308e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat int i; 309e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__int32 sum; 310e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 311e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__ASSERT(order > 0); 312e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat FLAC__ASSERT(order <= 32); 313e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat 314e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat /* 315e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * We do unique versions up to 12th order since that's the subset limit. 316e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * Also they are roughly ordered to match frequency of occurrence to 317e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat * minimize branching. 318e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat */ 319e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(order <= 12) { 320e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(order > 8) { 321e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(order > 10) { 322e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(order == 12) { 323e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = 0; i < (int)data_len; i++) { 324e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum = 0; 325e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[11] * data[i-12]; 326e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[10] * data[i-11]; 327e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[9] * data[i-10]; 328e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[8] * data[i-9]; 329e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[7] * data[i-8]; 330e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[6] * data[i-7]; 331e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[5] * data[i-6]; 332e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[4] * data[i-5]; 333e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[3] * data[i-4]; 334e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[2] * data[i-3]; 335e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[1] * data[i-2]; 336e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[0] * data[i-1]; 337e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat residual[i] = data[i] - (sum >> lp_quantization); 338e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 339e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 340e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat else { /* order == 11 */ 341e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = 0; i < (int)data_len; i++) { 342e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum = 0; 343e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[10] * data[i-11]; 344e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[9] * data[i-10]; 345e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[8] * data[i-9]; 346e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[7] * data[i-8]; 347e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[6] * data[i-7]; 348e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[5] * data[i-6]; 349e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[4] * data[i-5]; 350e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[3] * data[i-4]; 351e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[2] * data[i-3]; 352e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[1] * data[i-2]; 353e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[0] * data[i-1]; 354e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat residual[i] = data[i] - (sum >> lp_quantization); 355e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 356e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 357e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 358e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat else { 359e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(order == 10) { 360e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = 0; i < (int)data_len; i++) { 361e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum = 0; 362e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[9] * data[i-10]; 363e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[8] * data[i-9]; 364e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[7] * data[i-8]; 365e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[6] * data[i-7]; 366e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[5] * data[i-6]; 367e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[4] * data[i-5]; 368e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[3] * data[i-4]; 369e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[2] * data[i-3]; 370e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[1] * data[i-2]; 371e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[0] * data[i-1]; 372e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat residual[i] = data[i] - (sum >> lp_quantization); 373e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 374e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 375e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat else { /* order == 9 */ 376e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = 0; i < (int)data_len; i++) { 377e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum = 0; 378e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[8] * data[i-9]; 379e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[7] * data[i-8]; 380e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[6] * data[i-7]; 381e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[5] * data[i-6]; 382e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[4] * data[i-5]; 383e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[3] * data[i-4]; 384e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[2] * data[i-3]; 385e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[1] * data[i-2]; 386e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[0] * data[i-1]; 387e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat residual[i] = data[i] - (sum >> lp_quantization); 388e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 389e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 390e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 391e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 392e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat else if(order > 4) { 393e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(order > 6) { 394e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(order == 8) { 395e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = 0; i < (int)data_len; i++) { 396e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum = 0; 397e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[7] * data[i-8]; 398e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[6] * data[i-7]; 399e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[5] * data[i-6]; 400e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[4] * data[i-5]; 401e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[3] * data[i-4]; 402e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[2] * data[i-3]; 403e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[1] * data[i-2]; 404e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[0] * data[i-1]; 405e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat residual[i] = data[i] - (sum >> lp_quantization); 406e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 407e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 408e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat else { /* order == 7 */ 409e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = 0; i < (int)data_len; i++) { 410e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum = 0; 411e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[6] * data[i-7]; 412e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[5] * data[i-6]; 413e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[4] * data[i-5]; 414e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[3] * data[i-4]; 415e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[2] * data[i-3]; 416e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[1] * data[i-2]; 417e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[0] * data[i-1]; 418e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat residual[i] = data[i] - (sum >> lp_quantization); 419e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 420e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 421e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 422e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat else { 423e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(order == 6) { 424e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = 0; i < (int)data_len; i++) { 425e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum = 0; 426e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[5] * data[i-6]; 427e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[4] * data[i-5]; 428e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[3] * data[i-4]; 429e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[2] * data[i-3]; 430e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[1] * data[i-2]; 431e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[0] * data[i-1]; 432e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat residual[i] = data[i] - (sum >> lp_quantization); 433e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 434e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 435e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat else { /* order == 5 */ 436e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = 0; i < (int)data_len; i++) { 437e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum = 0; 438e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[4] * data[i-5]; 439e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[3] * data[i-4]; 440e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[2] * data[i-3]; 441e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[1] * data[i-2]; 442e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[0] * data[i-1]; 443e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat residual[i] = data[i] - (sum >> lp_quantization); 444e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 445e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 446e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 447e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 448e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat else { 449e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(order > 2) { 450e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat if(order == 4) { 451e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = 0; i < (int)data_len; i++) { 452e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum = 0; 453e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[3] * data[i-4]; 454e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[2] * data[i-3]; 455e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[1] * data[i-2]; 456e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[0] * data[i-1]; 457e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat residual[i] = data[i] - (sum >> lp_quantization); 458e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 459e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat } 460e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat else { /* order == 3 */ 461e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat for(i = 0; i < (int)data_len; i++) { 462e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum = 0; 463e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[2] * data[i-3]; 464e20e1347b9914aa05e30548c15d7cd5e412cc0e2San Mehat sum += qlp_coeff[1] * data[i-2]; 465 sum += qlp_coeff[0] * data[i-1]; 466 residual[i] = data[i] - (sum >> lp_quantization); 467 } 468 } 469 } 470 else { 471 if(order == 2) { 472 for(i = 0; i < (int)data_len; i++) { 473 sum = 0; 474 sum += qlp_coeff[1] * data[i-2]; 475 sum += qlp_coeff[0] * data[i-1]; 476 residual[i] = data[i] - (sum >> lp_quantization); 477 } 478 } 479 else { /* order == 1 */ 480 for(i = 0; i < (int)data_len; i++) 481 residual[i] = data[i] - ((qlp_coeff[0] * data[i-1]) >> lp_quantization); 482 } 483 } 484 } 485 } 486 else { /* order > 12 */ 487 for(i = 0; i < (int)data_len; i++) { 488 sum = 0; 489 switch(order) { 490 case 32: sum += qlp_coeff[31] * data[i-32]; 491 case 31: sum += qlp_coeff[30] * data[i-31]; 492 case 30: sum += qlp_coeff[29] * data[i-30]; 493 case 29: sum += qlp_coeff[28] * data[i-29]; 494 case 28: sum += qlp_coeff[27] * data[i-28]; 495 case 27: sum += qlp_coeff[26] * data[i-27]; 496 case 26: sum += qlp_coeff[25] * data[i-26]; 497 case 25: sum += qlp_coeff[24] * data[i-25]; 498 case 24: sum += qlp_coeff[23] * data[i-24]; 499 case 23: sum += qlp_coeff[22] * data[i-23]; 500 case 22: sum += qlp_coeff[21] * data[i-22]; 501 case 21: sum += qlp_coeff[20] * data[i-21]; 502 case 20: sum += qlp_coeff[19] * data[i-20]; 503 case 19: sum += qlp_coeff[18] * data[i-19]; 504 case 18: sum += qlp_coeff[17] * data[i-18]; 505 case 17: sum += qlp_coeff[16] * data[i-17]; 506 case 16: sum += qlp_coeff[15] * data[i-16]; 507 case 15: sum += qlp_coeff[14] * data[i-15]; 508 case 14: sum += qlp_coeff[13] * data[i-14]; 509 case 13: sum += qlp_coeff[12] * data[i-13]; 510 sum += qlp_coeff[11] * data[i-12]; 511 sum += qlp_coeff[10] * data[i-11]; 512 sum += qlp_coeff[ 9] * data[i-10]; 513 sum += qlp_coeff[ 8] * data[i- 9]; 514 sum += qlp_coeff[ 7] * data[i- 8]; 515 sum += qlp_coeff[ 6] * data[i- 7]; 516 sum += qlp_coeff[ 5] * data[i- 6]; 517 sum += qlp_coeff[ 4] * data[i- 5]; 518 sum += qlp_coeff[ 3] * data[i- 4]; 519 sum += qlp_coeff[ 2] * data[i- 3]; 520 sum += qlp_coeff[ 1] * data[i- 2]; 521 sum += qlp_coeff[ 0] * data[i- 1]; 522 } 523 residual[i] = data[i] - (sum >> lp_quantization); 524 } 525 } 526} 527#endif 528 529void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 * flac_restrict data, unsigned data_len, const FLAC__int32 * flac_restrict qlp_coeff, unsigned order, int lp_quantization, FLAC__int32 * flac_restrict residual) 530#if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS) 531{ 532 unsigned i, j; 533 FLAC__int64 sum; 534 const FLAC__int32 *history; 535 536#ifdef FLAC__OVERFLOW_DETECT_VERBOSE 537 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization); 538 for(i=0;i<order;i++) 539 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]); 540 fprintf(stderr,"\n"); 541#endif 542 FLAC__ASSERT(order > 0); 543 544 for(i = 0; i < data_len; i++) { 545 sum = 0; 546 history = data; 547 for(j = 0; j < order; j++) 548 sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history)); 549 if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) { 550 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, sum=%" PRId64 "\n", i, (sum >> lp_quantization)); 551 break; 552 } 553 if(FLAC__bitmath_silog2_wide((FLAC__int64)(*data) - (sum >> lp_quantization)) > 32) { 554 fprintf(stderr,"FLAC__lpc_compute_residual_from_qlp_coefficients_wide: OVERFLOW, i=%u, data=%d, sum=%" PRId64 ", residual=%" PRId64 "\n", i, *data, (int64_t)(sum >> lp_quantization), ((FLAC__int64)(*data) - (sum >> lp_quantization))); 555 break; 556 } 557 *(residual++) = *(data++) - (FLAC__int32)(sum >> lp_quantization); 558 } 559} 560#else /* fully unrolled version for normal use */ 561{ 562 int i; 563 FLAC__int64 sum; 564 565 FLAC__ASSERT(order > 0); 566 FLAC__ASSERT(order <= 32); 567 568 /* 569 * We do unique versions up to 12th order since that's the subset limit. 570 * Also they are roughly ordered to match frequency of occurrence to 571 * minimize branching. 572 */ 573 if(order <= 12) { 574 if(order > 8) { 575 if(order > 10) { 576 if(order == 12) { 577 for(i = 0; i < (int)data_len; i++) { 578 sum = 0; 579 sum += qlp_coeff[11] * (FLAC__int64)data[i-12]; 580 sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; 581 sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; 582 sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; 583 sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; 584 sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; 585 sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; 586 sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; 587 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 588 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 589 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 590 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 591 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); 592 } 593 } 594 else { /* order == 11 */ 595 for(i = 0; i < (int)data_len; i++) { 596 sum = 0; 597 sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; 598 sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; 599 sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; 600 sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; 601 sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; 602 sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; 603 sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; 604 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 605 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 606 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 607 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 608 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); 609 } 610 } 611 } 612 else { 613 if(order == 10) { 614 for(i = 0; i < (int)data_len; i++) { 615 sum = 0; 616 sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; 617 sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; 618 sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; 619 sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; 620 sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; 621 sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; 622 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 623 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 624 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 625 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 626 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); 627 } 628 } 629 else { /* order == 9 */ 630 for(i = 0; i < (int)data_len; i++) { 631 sum = 0; 632 sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; 633 sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; 634 sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; 635 sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; 636 sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; 637 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 638 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 639 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 640 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 641 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); 642 } 643 } 644 } 645 } 646 else if(order > 4) { 647 if(order > 6) { 648 if(order == 8) { 649 for(i = 0; i < (int)data_len; i++) { 650 sum = 0; 651 sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; 652 sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; 653 sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; 654 sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; 655 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 656 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 657 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 658 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 659 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); 660 } 661 } 662 else { /* order == 7 */ 663 for(i = 0; i < (int)data_len; i++) { 664 sum = 0; 665 sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; 666 sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; 667 sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; 668 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 669 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 670 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 671 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 672 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); 673 } 674 } 675 } 676 else { 677 if(order == 6) { 678 for(i = 0; i < (int)data_len; i++) { 679 sum = 0; 680 sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; 681 sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; 682 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 683 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 684 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 685 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 686 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); 687 } 688 } 689 else { /* order == 5 */ 690 for(i = 0; i < (int)data_len; i++) { 691 sum = 0; 692 sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; 693 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 694 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 695 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 696 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 697 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); 698 } 699 } 700 } 701 } 702 else { 703 if(order > 2) { 704 if(order == 4) { 705 for(i = 0; i < (int)data_len; i++) { 706 sum = 0; 707 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 708 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 709 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 710 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 711 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); 712 } 713 } 714 else { /* order == 3 */ 715 for(i = 0; i < (int)data_len; i++) { 716 sum = 0; 717 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 718 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 719 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 720 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); 721 } 722 } 723 } 724 else { 725 if(order == 2) { 726 for(i = 0; i < (int)data_len; i++) { 727 sum = 0; 728 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 729 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 730 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); 731 } 732 } 733 else { /* order == 1 */ 734 for(i = 0; i < (int)data_len; i++) 735 residual[i] = data[i] - (FLAC__int32)((qlp_coeff[0] * (FLAC__int64)data[i-1]) >> lp_quantization); 736 } 737 } 738 } 739 } 740 else { /* order > 12 */ 741 for(i = 0; i < (int)data_len; i++) { 742 sum = 0; 743 switch(order) { 744 case 32: sum += qlp_coeff[31] * (FLAC__int64)data[i-32]; 745 case 31: sum += qlp_coeff[30] * (FLAC__int64)data[i-31]; 746 case 30: sum += qlp_coeff[29] * (FLAC__int64)data[i-30]; 747 case 29: sum += qlp_coeff[28] * (FLAC__int64)data[i-29]; 748 case 28: sum += qlp_coeff[27] * (FLAC__int64)data[i-28]; 749 case 27: sum += qlp_coeff[26] * (FLAC__int64)data[i-27]; 750 case 26: sum += qlp_coeff[25] * (FLAC__int64)data[i-26]; 751 case 25: sum += qlp_coeff[24] * (FLAC__int64)data[i-25]; 752 case 24: sum += qlp_coeff[23] * (FLAC__int64)data[i-24]; 753 case 23: sum += qlp_coeff[22] * (FLAC__int64)data[i-23]; 754 case 22: sum += qlp_coeff[21] * (FLAC__int64)data[i-22]; 755 case 21: sum += qlp_coeff[20] * (FLAC__int64)data[i-21]; 756 case 20: sum += qlp_coeff[19] * (FLAC__int64)data[i-20]; 757 case 19: sum += qlp_coeff[18] * (FLAC__int64)data[i-19]; 758 case 18: sum += qlp_coeff[17] * (FLAC__int64)data[i-18]; 759 case 17: sum += qlp_coeff[16] * (FLAC__int64)data[i-17]; 760 case 16: sum += qlp_coeff[15] * (FLAC__int64)data[i-16]; 761 case 15: sum += qlp_coeff[14] * (FLAC__int64)data[i-15]; 762 case 14: sum += qlp_coeff[13] * (FLAC__int64)data[i-14]; 763 case 13: sum += qlp_coeff[12] * (FLAC__int64)data[i-13]; 764 sum += qlp_coeff[11] * (FLAC__int64)data[i-12]; 765 sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; 766 sum += qlp_coeff[ 9] * (FLAC__int64)data[i-10]; 767 sum += qlp_coeff[ 8] * (FLAC__int64)data[i- 9]; 768 sum += qlp_coeff[ 7] * (FLAC__int64)data[i- 8]; 769 sum += qlp_coeff[ 6] * (FLAC__int64)data[i- 7]; 770 sum += qlp_coeff[ 5] * (FLAC__int64)data[i- 6]; 771 sum += qlp_coeff[ 4] * (FLAC__int64)data[i- 5]; 772 sum += qlp_coeff[ 3] * (FLAC__int64)data[i- 4]; 773 sum += qlp_coeff[ 2] * (FLAC__int64)data[i- 3]; 774 sum += qlp_coeff[ 1] * (FLAC__int64)data[i- 2]; 775 sum += qlp_coeff[ 0] * (FLAC__int64)data[i- 1]; 776 } 777 residual[i] = data[i] - (FLAC__int32)(sum >> lp_quantization); 778 } 779 } 780} 781#endif 782 783#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ 784 785void FLAC__lpc_restore_signal(const FLAC__int32 * flac_restrict residual, unsigned data_len, const FLAC__int32 * flac_restrict qlp_coeff, unsigned order, int lp_quantization, FLAC__int32 * flac_restrict data) 786#if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS) 787{ 788 FLAC__int64 sumo; 789 unsigned i, j; 790 FLAC__int32 sum; 791 const FLAC__int32 *r = residual, *history; 792 793#ifdef FLAC__OVERFLOW_DETECT_VERBOSE 794 fprintf(stderr,"FLAC__lpc_restore_signal: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization); 795 for(i=0;i<order;i++) 796 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]); 797 fprintf(stderr,"\n"); 798#endif 799 FLAC__ASSERT(order > 0); 800 801 for(i = 0; i < data_len; i++) { 802 sumo = 0; 803 sum = 0; 804 history = data; 805 for(j = 0; j < order; j++) { 806 sum += qlp_coeff[j] * (*(--history)); 807 sumo += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*history); 808 if(sumo > 2147483647ll || sumo < -2147483648ll) 809 fprintf(stderr,"FLAC__lpc_restore_signal: OVERFLOW, i=%u, j=%u, c=%d, d=%d, sumo=%" PRId64 "\n",i,j,qlp_coeff[j],*history,sumo); 810 } 811 *(data++) = *(r++) + (sum >> lp_quantization); 812 } 813 814 /* Here's a slower but clearer version: 815 for(i = 0; i < data_len; i++) { 816 sum = 0; 817 for(j = 0; j < order; j++) 818 sum += qlp_coeff[j] * data[i-j-1]; 819 data[i] = residual[i] + (sum >> lp_quantization); 820 } 821 */ 822} 823#else /* fully unrolled version for normal use */ 824{ 825 int i; 826 FLAC__int32 sum; 827 828 FLAC__ASSERT(order > 0); 829 FLAC__ASSERT(order <= 32); 830 831 /* 832 * We do unique versions up to 12th order since that's the subset limit. 833 * Also they are roughly ordered to match frequency of occurrence to 834 * minimize branching. 835 */ 836 if(order <= 12) { 837 if(order > 8) { 838 if(order > 10) { 839 if(order == 12) { 840 for(i = 0; i < (int)data_len; i++) { 841 sum = 0; 842 sum += qlp_coeff[11] * data[i-12]; 843 sum += qlp_coeff[10] * data[i-11]; 844 sum += qlp_coeff[9] * data[i-10]; 845 sum += qlp_coeff[8] * data[i-9]; 846 sum += qlp_coeff[7] * data[i-8]; 847 sum += qlp_coeff[6] * data[i-7]; 848 sum += qlp_coeff[5] * data[i-6]; 849 sum += qlp_coeff[4] * data[i-5]; 850 sum += qlp_coeff[3] * data[i-4]; 851 sum += qlp_coeff[2] * data[i-3]; 852 sum += qlp_coeff[1] * data[i-2]; 853 sum += qlp_coeff[0] * data[i-1]; 854 data[i] = residual[i] + (sum >> lp_quantization); 855 } 856 } 857 else { /* order == 11 */ 858 for(i = 0; i < (int)data_len; i++) { 859 sum = 0; 860 sum += qlp_coeff[10] * data[i-11]; 861 sum += qlp_coeff[9] * data[i-10]; 862 sum += qlp_coeff[8] * data[i-9]; 863 sum += qlp_coeff[7] * data[i-8]; 864 sum += qlp_coeff[6] * data[i-7]; 865 sum += qlp_coeff[5] * data[i-6]; 866 sum += qlp_coeff[4] * data[i-5]; 867 sum += qlp_coeff[3] * data[i-4]; 868 sum += qlp_coeff[2] * data[i-3]; 869 sum += qlp_coeff[1] * data[i-2]; 870 sum += qlp_coeff[0] * data[i-1]; 871 data[i] = residual[i] + (sum >> lp_quantization); 872 } 873 } 874 } 875 else { 876 if(order == 10) { 877 for(i = 0; i < (int)data_len; i++) { 878 sum = 0; 879 sum += qlp_coeff[9] * data[i-10]; 880 sum += qlp_coeff[8] * data[i-9]; 881 sum += qlp_coeff[7] * data[i-8]; 882 sum += qlp_coeff[6] * data[i-7]; 883 sum += qlp_coeff[5] * data[i-6]; 884 sum += qlp_coeff[4] * data[i-5]; 885 sum += qlp_coeff[3] * data[i-4]; 886 sum += qlp_coeff[2] * data[i-3]; 887 sum += qlp_coeff[1] * data[i-2]; 888 sum += qlp_coeff[0] * data[i-1]; 889 data[i] = residual[i] + (sum >> lp_quantization); 890 } 891 } 892 else { /* order == 9 */ 893 for(i = 0; i < (int)data_len; i++) { 894 sum = 0; 895 sum += qlp_coeff[8] * data[i-9]; 896 sum += qlp_coeff[7] * data[i-8]; 897 sum += qlp_coeff[6] * data[i-7]; 898 sum += qlp_coeff[5] * data[i-6]; 899 sum += qlp_coeff[4] * data[i-5]; 900 sum += qlp_coeff[3] * data[i-4]; 901 sum += qlp_coeff[2] * data[i-3]; 902 sum += qlp_coeff[1] * data[i-2]; 903 sum += qlp_coeff[0] * data[i-1]; 904 data[i] = residual[i] + (sum >> lp_quantization); 905 } 906 } 907 } 908 } 909 else if(order > 4) { 910 if(order > 6) { 911 if(order == 8) { 912 for(i = 0; i < (int)data_len; i++) { 913 sum = 0; 914 sum += qlp_coeff[7] * data[i-8]; 915 sum += qlp_coeff[6] * data[i-7]; 916 sum += qlp_coeff[5] * data[i-6]; 917 sum += qlp_coeff[4] * data[i-5]; 918 sum += qlp_coeff[3] * data[i-4]; 919 sum += qlp_coeff[2] * data[i-3]; 920 sum += qlp_coeff[1] * data[i-2]; 921 sum += qlp_coeff[0] * data[i-1]; 922 data[i] = residual[i] + (sum >> lp_quantization); 923 } 924 } 925 else { /* order == 7 */ 926 for(i = 0; i < (int)data_len; i++) { 927 sum = 0; 928 sum += qlp_coeff[6] * data[i-7]; 929 sum += qlp_coeff[5] * data[i-6]; 930 sum += qlp_coeff[4] * data[i-5]; 931 sum += qlp_coeff[3] * data[i-4]; 932 sum += qlp_coeff[2] * data[i-3]; 933 sum += qlp_coeff[1] * data[i-2]; 934 sum += qlp_coeff[0] * data[i-1]; 935 data[i] = residual[i] + (sum >> lp_quantization); 936 } 937 } 938 } 939 else { 940 if(order == 6) { 941 for(i = 0; i < (int)data_len; i++) { 942 sum = 0; 943 sum += qlp_coeff[5] * data[i-6]; 944 sum += qlp_coeff[4] * data[i-5]; 945 sum += qlp_coeff[3] * data[i-4]; 946 sum += qlp_coeff[2] * data[i-3]; 947 sum += qlp_coeff[1] * data[i-2]; 948 sum += qlp_coeff[0] * data[i-1]; 949 data[i] = residual[i] + (sum >> lp_quantization); 950 } 951 } 952 else { /* order == 5 */ 953 for(i = 0; i < (int)data_len; i++) { 954 sum = 0; 955 sum += qlp_coeff[4] * data[i-5]; 956 sum += qlp_coeff[3] * data[i-4]; 957 sum += qlp_coeff[2] * data[i-3]; 958 sum += qlp_coeff[1] * data[i-2]; 959 sum += qlp_coeff[0] * data[i-1]; 960 data[i] = residual[i] + (sum >> lp_quantization); 961 } 962 } 963 } 964 } 965 else { 966 if(order > 2) { 967 if(order == 4) { 968 for(i = 0; i < (int)data_len; i++) { 969 sum = 0; 970 sum += qlp_coeff[3] * data[i-4]; 971 sum += qlp_coeff[2] * data[i-3]; 972 sum += qlp_coeff[1] * data[i-2]; 973 sum += qlp_coeff[0] * data[i-1]; 974 data[i] = residual[i] + (sum >> lp_quantization); 975 } 976 } 977 else { /* order == 3 */ 978 for(i = 0; i < (int)data_len; i++) { 979 sum = 0; 980 sum += qlp_coeff[2] * data[i-3]; 981 sum += qlp_coeff[1] * data[i-2]; 982 sum += qlp_coeff[0] * data[i-1]; 983 data[i] = residual[i] + (sum >> lp_quantization); 984 } 985 } 986 } 987 else { 988 if(order == 2) { 989 for(i = 0; i < (int)data_len; i++) { 990 sum = 0; 991 sum += qlp_coeff[1] * data[i-2]; 992 sum += qlp_coeff[0] * data[i-1]; 993 data[i] = residual[i] + (sum >> lp_quantization); 994 } 995 } 996 else { /* order == 1 */ 997 for(i = 0; i < (int)data_len; i++) 998 data[i] = residual[i] + ((qlp_coeff[0] * data[i-1]) >> lp_quantization); 999 } 1000 } 1001 } 1002 } 1003 else { /* order > 12 */ 1004 for(i = 0; i < (int)data_len; i++) { 1005 sum = 0; 1006 switch(order) { 1007 case 32: sum += qlp_coeff[31] * data[i-32]; 1008 case 31: sum += qlp_coeff[30] * data[i-31]; 1009 case 30: sum += qlp_coeff[29] * data[i-30]; 1010 case 29: sum += qlp_coeff[28] * data[i-29]; 1011 case 28: sum += qlp_coeff[27] * data[i-28]; 1012 case 27: sum += qlp_coeff[26] * data[i-27]; 1013 case 26: sum += qlp_coeff[25] * data[i-26]; 1014 case 25: sum += qlp_coeff[24] * data[i-25]; 1015 case 24: sum += qlp_coeff[23] * data[i-24]; 1016 case 23: sum += qlp_coeff[22] * data[i-23]; 1017 case 22: sum += qlp_coeff[21] * data[i-22]; 1018 case 21: sum += qlp_coeff[20] * data[i-21]; 1019 case 20: sum += qlp_coeff[19] * data[i-20]; 1020 case 19: sum += qlp_coeff[18] * data[i-19]; 1021 case 18: sum += qlp_coeff[17] * data[i-18]; 1022 case 17: sum += qlp_coeff[16] * data[i-17]; 1023 case 16: sum += qlp_coeff[15] * data[i-16]; 1024 case 15: sum += qlp_coeff[14] * data[i-15]; 1025 case 14: sum += qlp_coeff[13] * data[i-14]; 1026 case 13: sum += qlp_coeff[12] * data[i-13]; 1027 sum += qlp_coeff[11] * data[i-12]; 1028 sum += qlp_coeff[10] * data[i-11]; 1029 sum += qlp_coeff[ 9] * data[i-10]; 1030 sum += qlp_coeff[ 8] * data[i- 9]; 1031 sum += qlp_coeff[ 7] * data[i- 8]; 1032 sum += qlp_coeff[ 6] * data[i- 7]; 1033 sum += qlp_coeff[ 5] * data[i- 6]; 1034 sum += qlp_coeff[ 4] * data[i- 5]; 1035 sum += qlp_coeff[ 3] * data[i- 4]; 1036 sum += qlp_coeff[ 2] * data[i- 3]; 1037 sum += qlp_coeff[ 1] * data[i- 2]; 1038 sum += qlp_coeff[ 0] * data[i- 1]; 1039 } 1040 data[i] = residual[i] + (sum >> lp_quantization); 1041 } 1042 } 1043} 1044#endif 1045 1046void FLAC__lpc_restore_signal_wide(const FLAC__int32 * flac_restrict residual, unsigned data_len, const FLAC__int32 * flac_restrict qlp_coeff, unsigned order, int lp_quantization, FLAC__int32 * flac_restrict data) 1047#if defined(FLAC__OVERFLOW_DETECT) || !defined(FLAC__LPC_UNROLLED_FILTER_LOOPS) 1048{ 1049 unsigned i, j; 1050 FLAC__int64 sum; 1051 const FLAC__int32 *r = residual, *history; 1052 1053#ifdef FLAC__OVERFLOW_DETECT_VERBOSE 1054 fprintf(stderr,"FLAC__lpc_restore_signal_wide: data_len=%d, order=%u, lpq=%d",data_len,order,lp_quantization); 1055 for(i=0;i<order;i++) 1056 fprintf(stderr,", q[%u]=%d",i,qlp_coeff[i]); 1057 fprintf(stderr,"\n"); 1058#endif 1059 FLAC__ASSERT(order > 0); 1060 1061 for(i = 0; i < data_len; i++) { 1062 sum = 0; 1063 history = data; 1064 for(j = 0; j < order; j++) 1065 sum += (FLAC__int64)qlp_coeff[j] * (FLAC__int64)(*(--history)); 1066 if(FLAC__bitmath_silog2_wide(sum >> lp_quantization) > 32) { 1067 fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, sum=%" PRId64 "\n", i, (sum >> lp_quantization)); 1068 break; 1069 } 1070 if(FLAC__bitmath_silog2_wide((FLAC__int64)(*r) + (sum >> lp_quantization)) > 32) { 1071 fprintf(stderr,"FLAC__lpc_restore_signal_wide: OVERFLOW, i=%u, residual=%d, sum=%" PRId64 ", data=%" PRId64 "\n", i, *r, (sum >> lp_quantization), ((FLAC__int64)(*r) + (sum >> lp_quantization))); 1072 break; 1073 } 1074 *(data++) = *(r++) + (FLAC__int32)(sum >> lp_quantization); 1075 } 1076} 1077#else /* fully unrolled version for normal use */ 1078{ 1079 int i; 1080 FLAC__int64 sum; 1081 1082 FLAC__ASSERT(order > 0); 1083 FLAC__ASSERT(order <= 32); 1084 1085 /* 1086 * We do unique versions up to 12th order since that's the subset limit. 1087 * Also they are roughly ordered to match frequency of occurrence to 1088 * minimize branching. 1089 */ 1090 if(order <= 12) { 1091 if(order > 8) { 1092 if(order > 10) { 1093 if(order == 12) { 1094 for(i = 0; i < (int)data_len; i++) { 1095 sum = 0; 1096 sum += qlp_coeff[11] * (FLAC__int64)data[i-12]; 1097 sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; 1098 sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; 1099 sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; 1100 sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; 1101 sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; 1102 sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; 1103 sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; 1104 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 1105 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 1106 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 1107 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 1108 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); 1109 } 1110 } 1111 else { /* order == 11 */ 1112 for(i = 0; i < (int)data_len; i++) { 1113 sum = 0; 1114 sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; 1115 sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; 1116 sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; 1117 sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; 1118 sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; 1119 sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; 1120 sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; 1121 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 1122 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 1123 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 1124 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 1125 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); 1126 } 1127 } 1128 } 1129 else { 1130 if(order == 10) { 1131 for(i = 0; i < (int)data_len; i++) { 1132 sum = 0; 1133 sum += qlp_coeff[9] * (FLAC__int64)data[i-10]; 1134 sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; 1135 sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; 1136 sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; 1137 sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; 1138 sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; 1139 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 1140 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 1141 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 1142 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 1143 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); 1144 } 1145 } 1146 else { /* order == 9 */ 1147 for(i = 0; i < (int)data_len; i++) { 1148 sum = 0; 1149 sum += qlp_coeff[8] * (FLAC__int64)data[i-9]; 1150 sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; 1151 sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; 1152 sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; 1153 sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; 1154 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 1155 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 1156 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 1157 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 1158 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); 1159 } 1160 } 1161 } 1162 } 1163 else if(order > 4) { 1164 if(order > 6) { 1165 if(order == 8) { 1166 for(i = 0; i < (int)data_len; i++) { 1167 sum = 0; 1168 sum += qlp_coeff[7] * (FLAC__int64)data[i-8]; 1169 sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; 1170 sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; 1171 sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; 1172 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 1173 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 1174 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 1175 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 1176 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); 1177 } 1178 } 1179 else { /* order == 7 */ 1180 for(i = 0; i < (int)data_len; i++) { 1181 sum = 0; 1182 sum += qlp_coeff[6] * (FLAC__int64)data[i-7]; 1183 sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; 1184 sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; 1185 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 1186 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 1187 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 1188 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 1189 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); 1190 } 1191 } 1192 } 1193 else { 1194 if(order == 6) { 1195 for(i = 0; i < (int)data_len; i++) { 1196 sum = 0; 1197 sum += qlp_coeff[5] * (FLAC__int64)data[i-6]; 1198 sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; 1199 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 1200 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 1201 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 1202 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 1203 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); 1204 } 1205 } 1206 else { /* order == 5 */ 1207 for(i = 0; i < (int)data_len; i++) { 1208 sum = 0; 1209 sum += qlp_coeff[4] * (FLAC__int64)data[i-5]; 1210 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 1211 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 1212 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 1213 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 1214 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); 1215 } 1216 } 1217 } 1218 } 1219 else { 1220 if(order > 2) { 1221 if(order == 4) { 1222 for(i = 0; i < (int)data_len; i++) { 1223 sum = 0; 1224 sum += qlp_coeff[3] * (FLAC__int64)data[i-4]; 1225 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 1226 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 1227 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 1228 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); 1229 } 1230 } 1231 else { /* order == 3 */ 1232 for(i = 0; i < (int)data_len; i++) { 1233 sum = 0; 1234 sum += qlp_coeff[2] * (FLAC__int64)data[i-3]; 1235 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 1236 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 1237 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); 1238 } 1239 } 1240 } 1241 else { 1242 if(order == 2) { 1243 for(i = 0; i < (int)data_len; i++) { 1244 sum = 0; 1245 sum += qlp_coeff[1] * (FLAC__int64)data[i-2]; 1246 sum += qlp_coeff[0] * (FLAC__int64)data[i-1]; 1247 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); 1248 } 1249 } 1250 else { /* order == 1 */ 1251 for(i = 0; i < (int)data_len; i++) 1252 data[i] = residual[i] + (FLAC__int32)((qlp_coeff[0] * (FLAC__int64)data[i-1]) >> lp_quantization); 1253 } 1254 } 1255 } 1256 } 1257 else { /* order > 12 */ 1258 for(i = 0; i < (int)data_len; i++) { 1259 sum = 0; 1260 switch(order) { 1261 case 32: sum += qlp_coeff[31] * (FLAC__int64)data[i-32]; 1262 case 31: sum += qlp_coeff[30] * (FLAC__int64)data[i-31]; 1263 case 30: sum += qlp_coeff[29] * (FLAC__int64)data[i-30]; 1264 case 29: sum += qlp_coeff[28] * (FLAC__int64)data[i-29]; 1265 case 28: sum += qlp_coeff[27] * (FLAC__int64)data[i-28]; 1266 case 27: sum += qlp_coeff[26] * (FLAC__int64)data[i-27]; 1267 case 26: sum += qlp_coeff[25] * (FLAC__int64)data[i-26]; 1268 case 25: sum += qlp_coeff[24] * (FLAC__int64)data[i-25]; 1269 case 24: sum += qlp_coeff[23] * (FLAC__int64)data[i-24]; 1270 case 23: sum += qlp_coeff[22] * (FLAC__int64)data[i-23]; 1271 case 22: sum += qlp_coeff[21] * (FLAC__int64)data[i-22]; 1272 case 21: sum += qlp_coeff[20] * (FLAC__int64)data[i-21]; 1273 case 20: sum += qlp_coeff[19] * (FLAC__int64)data[i-20]; 1274 case 19: sum += qlp_coeff[18] * (FLAC__int64)data[i-19]; 1275 case 18: sum += qlp_coeff[17] * (FLAC__int64)data[i-18]; 1276 case 17: sum += qlp_coeff[16] * (FLAC__int64)data[i-17]; 1277 case 16: sum += qlp_coeff[15] * (FLAC__int64)data[i-16]; 1278 case 15: sum += qlp_coeff[14] * (FLAC__int64)data[i-15]; 1279 case 14: sum += qlp_coeff[13] * (FLAC__int64)data[i-14]; 1280 case 13: sum += qlp_coeff[12] * (FLAC__int64)data[i-13]; 1281 sum += qlp_coeff[11] * (FLAC__int64)data[i-12]; 1282 sum += qlp_coeff[10] * (FLAC__int64)data[i-11]; 1283 sum += qlp_coeff[ 9] * (FLAC__int64)data[i-10]; 1284 sum += qlp_coeff[ 8] * (FLAC__int64)data[i- 9]; 1285 sum += qlp_coeff[ 7] * (FLAC__int64)data[i- 8]; 1286 sum += qlp_coeff[ 6] * (FLAC__int64)data[i- 7]; 1287 sum += qlp_coeff[ 5] * (FLAC__int64)data[i- 6]; 1288 sum += qlp_coeff[ 4] * (FLAC__int64)data[i- 5]; 1289 sum += qlp_coeff[ 3] * (FLAC__int64)data[i- 4]; 1290 sum += qlp_coeff[ 2] * (FLAC__int64)data[i- 3]; 1291 sum += qlp_coeff[ 1] * (FLAC__int64)data[i- 2]; 1292 sum += qlp_coeff[ 0] * (FLAC__int64)data[i- 1]; 1293 } 1294 data[i] = residual[i] + (FLAC__int32)(sum >> lp_quantization); 1295 } 1296 } 1297} 1298#endif 1299 1300#if defined(_MSC_VER) 1301#pragma warning ( default : 4028 ) 1302#endif 1303 1304#ifndef FLAC__INTEGER_ONLY_LIBRARY 1305 1306FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__double lpc_error, unsigned total_samples) 1307{ 1308 FLAC__double error_scale; 1309 1310 FLAC__ASSERT(total_samples > 0); 1311 1312 error_scale = 0.5 / (FLAC__double)total_samples; 1313 1314 return FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error, error_scale); 1315} 1316 1317FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__double lpc_error, FLAC__double error_scale) 1318{ 1319 if(lpc_error > 0.0) { 1320 FLAC__double bps = (FLAC__double)0.5 * log(error_scale * lpc_error) / M_LN2; 1321 if(bps >= 0.0) 1322 return bps; 1323 else 1324 return 0.0; 1325 } 1326 else if(lpc_error < 0.0) { /* error should not be negative but can happen due to inadequate floating-point resolution */ 1327 return 1e32; 1328 } 1329 else { 1330 return 0.0; 1331 } 1332} 1333 1334unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned overhead_bits_per_order) 1335{ 1336 unsigned order, indx, best_index; /* 'index' the index into lpc_error; index==order-1 since lpc_error[0] is for order==1, lpc_error[1] is for order==2, etc */ 1337 FLAC__double bits, best_bits, error_scale; 1338 1339 FLAC__ASSERT(max_order > 0); 1340 FLAC__ASSERT(total_samples > 0); 1341 1342 error_scale = 0.5 / (FLAC__double)total_samples; 1343 1344 best_index = 0; 1345 best_bits = (unsigned)(-1); 1346 1347 for(indx = 0, order = 1; indx < max_order; indx++, order++) { 1348 bits = FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(lpc_error[indx], error_scale) * (FLAC__double)(total_samples - order) + (FLAC__double)(order * overhead_bits_per_order); 1349 if(bits < best_bits) { 1350 best_index = indx; 1351 best_bits = bits; 1352 } 1353 } 1354 1355 return best_index+1; /* +1 since indx of lpc_error[] is order-1 */ 1356} 1357 1358#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ 1359