/* Copyright (c) 2005, The Musepack Development Team All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the The Musepack Development Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /// \file synth_filter.c /// Synthesis functions. /// \todo document me #include "musepack.h" #include "internal.h" /* C O N S T A N T S */ #undef _ #if defined(MPC_FIXED_POINT) #if defined(OPTIMIZE_FOR_SPEED) // round at compile time to +/- 2^14 as a pre-shift before 32=32x32-multiply #define D(value) (MPC_SHR_RND(value, 3)) // round at runtime to +/- 2^17 as a pre-shift before 32=32x32-multiply // samples are 18.14 fixed point. 30.2 after this shift, whereas the // 15.2 bits are significant (not including sign) #define MPC_V_PRESHIFT(X) MPC_SHR_RND(X, 12) // in this configuration a post-shift by >>1 is needed after synthesis #else #if defined(CPU_ARM) // do not up-scale D-values to achieve higher speed in smull/mlal // operations. saves ~14/8 = 1.75 cycles per multiplication #define D(value) (value) // in this configuration a post-shift by >>16 is needed after synthesis #else // saturate to +/- 2^31 (= value << (31-17)), D-values are +/- 2^17 #define D(value) (value << (14)) #endif // do not perform pre-shift #define MPC_V_PRESHIFT(X) (X) #endif #else // IMPORTANT: internal scaling is somehow strange for floating point, therefore we scale the coefficients Di_opt // by the correct amount to have proper scaled output #define D(value) MAKE_MPC_SAMPLE((double)value*(double)(0x1000)) // do not perform pre-shift #define MPC_V_PRESHIFT(X) (X) #endif // Di_opt coefficients are +/- 2^17 (pre-shifted by <<16) static const MPC_SAMPLE_FORMAT Di_opt [512] ICONST_ATTR = { /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ /* 0 */ D( 0), -D( 29), D(213), -D( 459), D(2037), -D(5153), D( 6574), -D(37489), D(75038), D(37489), D(6574), D(5153), D(2037), D(459), D(213), D(29), /* 1 */ -D( 1), -D( 31), D(218), -D( 519), D(2000), -D(5517), D( 5959), -D(39336), D(74992), D(35640), D(7134), D(4788), D(2063), D(401), D(208), D(26), /* 2 */ -D( 1), -D( 35), D(222), -D( 581), D(1952), -D(5879), D( 5288), -D(41176), D(74856), D(33791), D(7640), D(4425), D(2080), D(347), D(202), D(24), /* 3 */ -D( 1), -D( 38), D(225), -D( 645), D(1893), -D(6237), D( 4561), -D(43006), D(74630), D(31947), D(8092), D(4063), D(2087), D(294), D(196), D(21), /* 4 */ -D( 1), -D( 41), D(227), -D( 711), D(1822), -D(6589), D( 3776), -D(44821), D(74313), D(30112), D(8492), D(3705), D(2085), D(244), D(190), D(19), /* 5 */ -D( 1), -D( 45), D(228), -D( 779), D(1739), -D(6935), D( 2935), -D(46617), D(73908), D(28289), D(8840), D(3351), D(2075), D(197), D(183), D(17), /* 6 */ -D( 1), -D( 49), D(228), -D( 848), D(1644), -D(7271), D( 2037), -D(48390), D(73415), D(26482), D(9139), D(3004), D(2057), D(153), D(176), D(16), /* 7 */ -D( 2), -D( 53), D(227), -D( 919), D(1535), -D(7597), D( 1082), -D(50137), D(72835), D(24694), D(9389), D(2663), D(2032), D(111), D(169), D(14), /* 8 */ -D( 2), -D( 58), D(224), -D( 991), D(1414), -D(7910), D( 70), -D(51853), D(72169), D(22929), D(9592), D(2330), D(2001), D( 72), D(161), D(13), /* 9 */ -D( 2), -D( 63), D(221), -D(1064), D(1280), -D(8209), -D( 998), -D(53534), D(71420), D(21189), D(9750), D(2006), D(1962), D( 36), D(154), D(11), /* 10 */ -D( 2), -D( 68), D(215), -D(1137), D(1131), -D(8491), -D( 2122), -D(55178), D(70590), D(19478), D(9863), D(1692), D(1919), D( 2), D(147), D(10), /* 11 */ -D( 3), -D( 73), D(208), -D(1210), D( 970), -D(8755), -D( 3300), -D(56778), D(69679), D(17799), D(9935), D(1388), D(1870), -D( 29), D(139), D( 9), /* 12 */ -D( 3), -D( 79), D(200), -D(1283), D( 794), -D(8998), -D( 4533), -D(58333), D(68692), D(16155), D(9966), D(1095), D(1817), -D( 57), D(132), D( 8), /* 13 */ -D( 4), -D( 85), D(189), -D(1356), D( 605), -D(9219), -D( 5818), -D(59838), D(67629), D(14548), D(9959), D( 814), D(1759), -D( 83), D(125), D( 7), /* 14 */ -D( 4), -D( 91), D(177), -D(1428), D( 402), -D(9416), -D( 7154), -D(61289), D(66494), D(12980), D(9916), D( 545), D(1698), -D(106), D(117), D( 7), /* 15 */ -D( 5), -D( 97), D(163), -D(1498), D( 185), -D(9585), -D( 8540), -D(62684), D(65290), D(11455), D(9838), D( 288), D(1634), -D(127), D(111), D( 6), /* 16 */ -D( 5), -D(104), D(146), -D(1567), -D( 45), -D(9727), -D( 9975), -D(64019), D(64019), D( 9975), D(9727), D( 45), D(1567), -D(146), D(104), D( 5), /* 17 */ -D( 6), -D(111), D(127), -D(1634), -D( 288), -D(9838), -D(11455), -D(65290), D(62684), D( 8540), D(9585), -D( 185), D(1498), -D(163), D( 97), D( 5), /* 18 */ -D( 7), -D(117), D(106), -D(1698), -D( 545), -D(9916), -D(12980), -D(66494), D(61289), D( 7154), D(9416), -D( 402), D(1428), -D(177), D( 91), D( 4), /* 19 */ -D( 7), -D(125), D( 83), -D(1759), -D( 814), -D(9959), -D(14548), -D(67629), D(59838), D( 5818), D(9219), -D( 605), D(1356), -D(189), D( 85), D( 4), /* 20 */ -D( 8), -D(132), D( 57), -D(1817), -D(1095), -D(9966), -D(16155), -D(68692), D(58333), D( 4533), D(8998), -D( 794), D(1283), -D(200), D( 79), D( 3), /* 21 */ -D( 9), -D(139), D( 29), -D(1870), -D(1388), -D(9935), -D(17799), -D(69679), D(56778), D( 3300), D(8755), -D( 970), D(1210), -D(208), D( 73), D( 3), /* 22 */ -D(10), -D(147), -D( 2), -D(1919), -D(1692), -D(9863), -D(19478), -D(70590), D(55178), D( 2122), D(8491), -D(1131), D(1137), -D(215), D( 68), D( 2), /* 23 */ -D(11), -D(154), -D( 36), -D(1962), -D(2006), -D(9750), -D(21189), -D(71420), D(53534), D( 998), D(8209), -D(1280), D(1064), -D(221), D( 63), D( 2), /* 24 */ -D(13), -D(161), -D( 72), -D(2001), -D(2330), -D(9592), -D(22929), -D(72169), D(51853), -D( 70), D(7910), -D(1414), D( 991), -D(224), D( 58), D( 2), /* 25 */ -D(14), -D(169), -D(111), -D(2032), -D(2663), -D(9389), -D(24694), -D(72835), D(50137), -D( 1082), D(7597), -D(1535), D( 919), -D(227), D( 53), D( 2), /* 26 */ -D(16), -D(176), -D(153), -D(2057), -D(3004), -D(9139), -D(26482), -D(73415), D(48390), -D( 2037), D(7271), -D(1644), D( 848), -D(228), D( 49), D( 1), /* 27 */ -D(17), -D(183), -D(197), -D(2075), -D(3351), -D(8840), -D(28289), -D(73908), D(46617), -D( 2935), D(6935), -D(1739), D( 779), -D(228), D( 45), D( 1), /* 28 */ -D(19), -D(190), -D(244), -D(2085), -D(3705), -D(8492), -D(30112), -D(74313), D(44821), -D( 3776), D(6589), -D(1822), D( 711), -D(227), D( 41), D( 1), /* 29 */ -D(21), -D(196), -D(294), -D(2087), -D(4063), -D(8092), -D(31947), -D(74630), D(43006), -D( 4561), D(6237), -D(1893), D( 645), -D(225), D( 38), D( 1), /* 30 */ -D(24), -D(202), -D(347), -D(2080), -D(4425), -D(7640), -D(33791), -D(74856), D(41176), -D( 5288), D(5879), -D(1952), D( 581), -D(222), D( 35), D( 1), /* 31 */ -D(26), -D(208), -D(401), -D(2063), -D(4788), -D(7134), -D(35640), -D(74992), D(39336), -D( 5959), D(5517), -D(2000), D( 519), -D(218), D( 31), D( 1) }; #undef D // needed to prevent from internal overflow in calculate_V (see below) #define OVERFLOW_FIX 2 // V-coefficients were expanded (<<) by V_COEFFICIENT_EXPAND #define V_COEFFICIENT_EXPAND 27 #if defined(MPC_FIXED_POINT) #if defined(OPTIMIZE_FOR_SPEED) // define 32=32x32-multiplication for DCT-coefficients with samples, vcoef will be pre-shifted on creation // samples are rounded to +/- 2^19 as pre-shift before 32=32x32-multiply #define MPC_MULTIPLY_V(sample, vcoef) ( MPC_SHR_RND(sample, 12) * vcoef ) // pre- and postscale are used to avoid internal overflow in synthesis calculation // samples are s15.0, v-coefs are 4.12 -> internal format is s19.12 #define MPC_MULTIPLY_V_PRESCALE(sample, vcoef) ( MPC_SHR_RND(sample, (12+OVERFLOW_FIX)) * vcoef ) #define MPC_MULTIPLY_V_POSTSCALE(sample, vcoef) ( MPC_SHR_RND(sample, (12-OVERFLOW_FIX)) * vcoef ) #define MPC_V_POSTSCALE(sample) (sample<>32 after multiplication, as V-coef were expanded by V_COEFFICIENT_EXPAND we'll correct this on the result. // Will loose 5bit accuracy on result in fract part without effect on final audio result #define MPC_MULTIPLY_V(sample, vcoef) ( (MPC_MULTIPLY_FRACT(sample, vcoef)) << (32-V_COEFFICIENT_EXPAND) ) // pre- and postscale are used to avoid internal overflow in synthesis calculation // samples are s15.14, v-coefs are 4.27 -> internal format is s19.12 #define MPC_MULTIPLY_V_PRESCALE(sample, vcoef) ( (MPC_MULTIPLY_FRACT(sample, vcoef)) << (32-V_COEFFICIENT_EXPAND-OVERFLOW_FIX) ) #define MPC_MULTIPLY_V_POSTSCALE(sample, vcoef) ( (MPC_MULTIPLY_FRACT(sample, vcoef)) << (32-V_COEFFICIENT_EXPAND+OVERFLOW_FIX) ) #define MPC_V_POSTSCALE(sample) (sample<>= 1; // post shift to compensate for pre-shifting Data += 1; // total: 32 * (16 muls, 15 adds) } #else #if defined(CPU_COLDFIRE) // 64=32x32-multiply assembler for Coldfire for ( k = 0; k < 32; k++, D += 16, V++ ) { asm volatile ( "movem.l (%[D]), %%d0-%%d3 \n\t" "move.l (%[V]), %%a5 \n\t" "mac.l %%d0, %%a5, (96*4, %[V]), %%a5, %%acc0 \n\t" "mac.l %%d1, %%a5, (128*4, %[V]), %%a5, %%acc0\n\t" "mac.l %%d2, %%a5, (224*4, %[V]), %%a5, %%acc0\n\t" "mac.l %%d3, %%a5, (256*4, %[V]), %%a5, %%acc0\n\t" "movem.l (4*4, %[D]), %%d0-%%d3 \n\t" "mac.l %%d0, %%a5, (352*4, %[V]), %%a5, %%acc0\n\t" "mac.l %%d1, %%a5, (384*4, %[V]), %%a5, %%acc0\n\t" "mac.l %%d2, %%a5, (480*4, %[V]), %%a5, %%acc0\n\t" "mac.l %%d3, %%a5, (512*4, %[V]), %%a5, %%acc0\n\t" "movem.l (8*4, %[D]), %%d0-%%d3 \n\t" "mac.l %%d0, %%a5, (608*4, %[V]), %%a5, %%acc0\n\t" "mac.l %%d1, %%a5, (640*4, %[V]), %%a5, %%acc0\n\t" "mac.l %%d2, %%a5, (736*4, %[V]), %%a5, %%acc0\n\t" "mac.l %%d3, %%a5, (768*4, %[V]), %%a5, %%acc0\n\t" "movem.l (12*4, %[D]), %%d0-%%d3 \n\t" "mac.l %%d0, %%a5, (864*4, %[V]), %%a5, %%acc0\n\t" "mac.l %%d1, %%a5, (896*4, %[V]), %%a5, %%acc0\n\t" "mac.l %%d2, %%a5, (992*4, %[V]), %%a5, %%acc0\n\t" "mac.l %%d3, %%a5, %%acc0 \n\t" "movclr.l %%acc0, %%d0 \n\t" "lsl.l #1, %%d0 \n\t" "move.l %%d0, (%[Data])+ \n" : [Data] "+a" (Data) : [V] "a" (V), [D] "a" (D) : "d0", "d1", "d2", "d3", "a5"); } #else // 64=64x64-multiply (FIXED_POINT) or float=float*float (!FIXED_POINT) in C for ( k = 0; k < 32; k++, D += 16, V++ ) { *Data = MPC_MULTIPLY_EX(V[ 0],D[ 0],30) + MPC_MULTIPLY_EX(V[ 96],D[ 1],30) + MPC_MULTIPLY_EX(V[128],D[ 2],30) + MPC_MULTIPLY_EX(V[224],D[ 3],30) + MPC_MULTIPLY_EX(V[256],D[ 4],30) + MPC_MULTIPLY_EX(V[352],D[ 5],30) + MPC_MULTIPLY_EX(V[384],D[ 6],30) + MPC_MULTIPLY_EX(V[480],D[ 7],30) + MPC_MULTIPLY_EX(V[512],D[ 8],30) + MPC_MULTIPLY_EX(V[608],D[ 9],30) + MPC_MULTIPLY_EX(V[640],D[10],30) + MPC_MULTIPLY_EX(V[736],D[11],30) + MPC_MULTIPLY_EX(V[768],D[12],30) + MPC_MULTIPLY_EX(V[864],D[13],30) + MPC_MULTIPLY_EX(V[896],D[14],30) + MPC_MULTIPLY_EX(V[992],D[15],30); Data += 1; // total: 16 muls, 15 adds, 16 shifts } #endif #endif } #endif /* CPU_ARM */ static void mpc_full_synthesis_filter(MPC_SAMPLE_FORMAT *OutData, MPC_SAMPLE_FORMAT *V, const MPC_SAMPLE_FORMAT *Y) { mpc_uint32_t n; if (NULL != OutData) { for ( n = 0; n < 36; n++, Y += 32, OutData += 32 ) { V -= 64; mpc_calculate_new_V ( Y, V ); mpc_decoder_windowing_D( OutData, V, Di_opt ); } } } void mpc_decoder_synthese_filter_float(mpc_decoder *d, MPC_SAMPLE_FORMAT *OutData) { /********* left channel ********/ memmove(d->V_L + MPC_V_MEM, d->V_L, 960 * sizeof(MPC_SAMPLE_FORMAT) ); mpc_full_synthesis_filter( OutData, (MPC_SAMPLE_FORMAT *)(d->V_L + MPC_V_MEM), (MPC_SAMPLE_FORMAT *)(d->Y_L [0])); /******** right channel ********/ memmove(d->V_R + MPC_V_MEM, d->V_R, 960 * sizeof(MPC_SAMPLE_FORMAT) ); mpc_full_synthesis_filter( (OutData == NULL ? NULL : OutData + MPC_FRAME_LENGTH), (MPC_SAMPLE_FORMAT *)(d->V_R + MPC_V_MEM), (MPC_SAMPLE_FORMAT *)(d->Y_R [0])); } /*******************************************/ /* */ /* dithered synthesis */ /* */ /*******************************************/ static const unsigned char Parity [256] ICONST_ATTR = { // parity 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0, 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0, 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0, 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0 }; /* * This is a simple random number generator with good quality for audio purposes. * It consists of two polycounters with opposite rotation direction and different * periods. The periods are coprime, so the total period is the product of both. * * ------------------------------------------------------------------------------------------------- * +-> |31:30:29:28:27:26:25:24:23:22:21:20:19:18:17:16:15:14:13:12:11:10: 9: 8: 7: 6: 5: 4: 3: 2: 1: 0| * | ------------------------------------------------------------------------------------------------- * | | | | | | | * | +--+--+--+-XOR-+--------+ * | | * +--------------------------------------------------------------------------------------+ * * ------------------------------------------------------------------------------------------------- * |31:30:29:28:27:26:25:24:23:22:21:20:19:18:17:16:15:14:13:12:11:10: 9: 8: 7: 6: 5: 4: 3: 2: 1: 0| <-+ * ------------------------------------------------------------------------------------------------- | * | | | | | * +--+----XOR----+--+ | * | | * +----------------------------------------------------------------------------------------+ * * * The first has an period of 3*5*17*257*65537, the second of 7*47*73*178481, * which gives a period of 18.410.713.077.675.721.215. The result is the * XORed values of both generators. */ mpc_uint32_t mpc_random_int(mpc_decoder *d) { #if 1 mpc_uint32_t t1, t2, t3, t4; t3 = t1 = d->__r1; t4 = t2 = d->__r2; // Parity calculation is done via table lookup, this is also available t1 &= 0xF5; t2 >>= 25; // on CPUs without parity, can be implemented in C and avoid unpredictable t1 = Parity [t1]; t2 &= 0x63; // jumps and slow rotate through the carry flag operations. t1 <<= 31; t2 = Parity [t2]; return (d->__r1 = (t3 >> 1) | t1 ) ^ (d->__r2 = (t4 + t4) | t2 ); #else return (d->__r1 = (d->__r1 >> 1) | ((mpc_uint32_t)Parity [d->__r1 & 0xF5] << 31) ) ^ (d->__r2 = (d->__r2 << 1) | (mpc_uint32_t)Parity [(d->__r2 >> 25) & 0x63] ); #endif }