annotate Plugins/Input/flac/replaygain_synthesis.c @ 61:fa848bd484d8 trunk

[svn] Move plugins to Plugins/
author nenolod
date Fri, 28 Oct 2005 22:58:11 -0700
parents
children f12d7e208b43
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
61
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
1 /* replaygain_synthesis - Routines for applying ReplayGain to a signal
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
2 * Copyright (C) 2002,2003,2004,2005 Josh Coalson
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
3 *
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
5 * modify it under the terms of the GNU General Public License
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
6 * as published by the Free Software Foundation; either version 2
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
7 * of the License, or (at your option) any later version.
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
8 *
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
9 * This program is distributed in the hope that it will be useful,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
12 * GNU General Public License for more details.
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
13 *
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
14 * You should have received a copy of the GNU General Public License
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
15 * along with this program; if not, write to the Free Software
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
17 */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
18 /*
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
19 * This is an aggregation of pieces of code from John Edwards' WaveGain
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
20 * program. Mostly cosmetic changes were made; otherwise, the dithering
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
21 * code is almost untouched and the gain processing was converted from
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
22 * processing a whole file to processing chunks of samples.
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
23 *
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
24 * The original copyright notices for WaveGain's dither.c and wavegain.c
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
25 * appear below:
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
26 */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
27 /*
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
28 * (c) 2002 John Edwards
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
29 * mostly lifted from work by Frank Klemm
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
30 * random functions for dithering.
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
31 */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
32 /*
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
33 * Copyright (C) 2002 John Edwards
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
34 * Additional code by Magnus Holmgren and Gian-Carlo Pascutto
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
35 */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
36
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
37 #include <string.h> /* for memset() */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
38 #include <math.h>
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
39 #include "fast_float_math_hack.h"
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
40 #include "replaygain_synthesis.h"
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
41 #include "FLAC/assert.h"
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
42
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
43 #if defined _MSC_VER
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
44 #define FLAC__INLINE __inline
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
45 #else
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
46 #define FLAC__INLINE
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
47 #endif
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
48
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
49 /* adjust for compilers that can't understand using LL suffix for int64_t literals */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
50 #ifdef _MSC_VER
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
51 #define FLAC__I64L(x) x
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
52 #else
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
53 #define FLAC__I64L(x) x##LL
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
54 #endif
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
55
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
56
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
57 /*
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
58 * the following is based on parts of dither.c
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
59 */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
60
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
61
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
62 /*
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
63 * This is a simple random number generator with good quality for audio purposes.
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
64 * It consists of two polycounters with opposite rotation direction and different
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
65 * periods. The periods are coprime, so the total period is the product of both.
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
66 *
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
67 * -------------------------------------------------------------------------------------------------
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
68 * +-> |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|
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
69 * | -------------------------------------------------------------------------------------------------
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
70 * | | | | | | |
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
71 * | +--+--+--+-XOR-+--------+
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
72 * | |
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
73 * +--------------------------------------------------------------------------------------+
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
74 *
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
75 * -------------------------------------------------------------------------------------------------
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
76 * |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| <-+
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
77 * ------------------------------------------------------------------------------------------------- |
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
78 * | | | | |
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
79 * +--+----XOR----+--+ |
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
80 * | |
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
81 * +----------------------------------------------------------------------------------------+
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
82 *
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
83 *
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
84 * The first has an period of 3*5*17*257*65537, the second of 7*47*73*178481,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
85 * which gives a period of 18.410.713.077.675.721.215. The result is the
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
86 * XORed values of both generators.
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
87 */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
88
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
89 static unsigned int random_int_()
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
90 {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
91 static const unsigned char parity_[256] = {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
92 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,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
93 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,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
94 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,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
95 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,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
96 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,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
97 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,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
98 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,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
99 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
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
100 };
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
101 static unsigned int r1_ = 1;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
102 static unsigned int r2_ = 1;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
103
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
104 unsigned int t1, t2, t3, t4;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
105
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
106 /* Parity calculation is done via table lookup, this is also available
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
107 * on CPUs without parity, can be implemented in C and avoid unpredictable
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
108 * jumps and slow rotate through the carry flag operations.
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
109 */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
110 t3 = t1 = r1_; t4 = t2 = r2_;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
111 t1 &= 0xF5; t2 >>= 25;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
112 t1 = parity_[t1]; t2 &= 0x63;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
113 t1 <<= 31; t2 = parity_[t2];
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
114
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
115 return (r1_ = (t3 >> 1) | t1 ) ^ (r2_ = (t4 + t4) | t2 );
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
116 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
117
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
118 /* gives a equal distributed random number */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
119 /* between -2^31*mult and +2^31*mult */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
120 static double random_equi_(double mult)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
121 {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
122 return mult * (int) random_int_();
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
123 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
124
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
125 /* gives a triangular distributed random number */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
126 /* between -2^32*mult and +2^32*mult */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
127 static double random_triangular_(double mult)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
128 {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
129 return mult * ( (double) (int) random_int_() + (double) (int) random_int_() );
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
130 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
131
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
132
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
133 static const float F44_0 [16 + 32] = {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
134 (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
135 (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
136
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
137 (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
138 (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
139
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
140 (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
141 (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
142 };
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
143
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
144
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
145 static const float F44_1 [16 + 32] = { /* SNR(w) = 4.843163 dB, SNR = -3.192134 dB */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
146 (float) 0.85018292704024355931, (float) 0.29089597350995344721, (float)-0.05021866022121039450, (float)-0.23545456294599161833,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
147 (float)-0.58362726442227032096, (float)-0.67038978965193036429, (float)-0.38566861572833459221, (float)-0.15218663390367969967,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
148 (float)-0.02577543084864530676, (float) 0.14119295297688728127, (float) 0.22398848581628781612, (float) 0.15401727203382084116,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
149 (float) 0.05216161232906000929, (float)-0.00282237820999675451, (float)-0.03042794608323867363, (float)-0.03109780942998826024,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
150
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
151 (float) 0.85018292704024355931, (float) 0.29089597350995344721, (float)-0.05021866022121039450, (float)-0.23545456294599161833,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
152 (float)-0.58362726442227032096, (float)-0.67038978965193036429, (float)-0.38566861572833459221, (float)-0.15218663390367969967,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
153 (float)-0.02577543084864530676, (float) 0.14119295297688728127, (float) 0.22398848581628781612, (float) 0.15401727203382084116,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
154 (float) 0.05216161232906000929, (float)-0.00282237820999675451, (float)-0.03042794608323867363, (float)-0.03109780942998826024,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
155
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
156 (float) 0.85018292704024355931, (float) 0.29089597350995344721, (float)-0.05021866022121039450, (float)-0.23545456294599161833,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
157 (float)-0.58362726442227032096, (float)-0.67038978965193036429, (float)-0.38566861572833459221, (float)-0.15218663390367969967,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
158 (float)-0.02577543084864530676, (float) 0.14119295297688728127, (float) 0.22398848581628781612, (float) 0.15401727203382084116,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
159 (float) 0.05216161232906000929, (float)-0.00282237820999675451, (float)-0.03042794608323867363, (float)-0.03109780942998826024,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
160 };
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
161
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
162
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
163 static const float F44_2 [16 + 32] = { /* SNR(w) = 10.060213 dB, SNR = -12.766730 dB */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
164 (float) 1.78827593892108555290, (float) 0.95508210637394326553, (float)-0.18447626783899924429, (float)-0.44198126506275016437,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
165 (float)-0.88404052492547413497, (float)-1.42218907262407452967, (float)-1.02037566838362314995, (float)-0.34861755756425577264,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
166 (float)-0.11490230170431934434, (float) 0.12498899339968611803, (float) 0.38065885268563131927, (float) 0.31883491321310506562,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
167 (float) 0.10486838686563442765, (float)-0.03105361685110374845, (float)-0.06450524884075370758, (float)-0.02939198261121969816,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
168
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
169 (float) 1.78827593892108555290, (float) 0.95508210637394326553, (float)-0.18447626783899924429, (float)-0.44198126506275016437,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
170 (float)-0.88404052492547413497, (float)-1.42218907262407452967, (float)-1.02037566838362314995, (float)-0.34861755756425577264,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
171 (float)-0.11490230170431934434, (float) 0.12498899339968611803, (float) 0.38065885268563131927, (float) 0.31883491321310506562,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
172 (float) 0.10486838686563442765, (float)-0.03105361685110374845, (float)-0.06450524884075370758, (float)-0.02939198261121969816,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
173
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
174 (float) 1.78827593892108555290, (float) 0.95508210637394326553, (float)-0.18447626783899924429, (float)-0.44198126506275016437,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
175 (float)-0.88404052492547413497, (float)-1.42218907262407452967, (float)-1.02037566838362314995, (float)-0.34861755756425577264,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
176 (float)-0.11490230170431934434, (float) 0.12498899339968611803, (float) 0.38065885268563131927, (float) 0.31883491321310506562,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
177 (float) 0.10486838686563442765, (float)-0.03105361685110374845, (float)-0.06450524884075370758, (float)-0.02939198261121969816,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
178 };
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
179
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
180
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
181 static const float F44_3 [16 + 32] = { /* SNR(w) = 15.382598 dB, SNR = -29.402334 dB */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
182 (float) 2.89072132015058161445, (float) 2.68932810943698754106, (float) 0.21083359339410251227, (float)-0.98385073324997617515,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
183 (float)-1.11047823227097316719, (float)-2.18954076314139673147, (float)-2.36498032881953056225, (float)-0.95484132880101140785,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
184 (float)-0.23924057925542965158, (float)-0.13865235703915925642, (float) 0.43587843191057992846, (float) 0.65903257226026665927,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
185 (float) 0.24361815372443152787, (float)-0.00235974960154720097, (float) 0.01844166574603346289, (float) 0.01722945988740875099,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
186
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
187 (float) 2.89072132015058161445, (float) 2.68932810943698754106, (float) 0.21083359339410251227, (float)-0.98385073324997617515,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
188 (float)-1.11047823227097316719, (float)-2.18954076314139673147, (float)-2.36498032881953056225, (float)-0.95484132880101140785,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
189 (float)-0.23924057925542965158, (float)-0.13865235703915925642, (float) 0.43587843191057992846, (float) 0.65903257226026665927,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
190 (float) 0.24361815372443152787, (float)-0.00235974960154720097, (float) 0.01844166574603346289, (float) 0.01722945988740875099,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
191
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
192 (float) 2.89072132015058161445, (float) 2.68932810943698754106, (float) 0.21083359339410251227, (float)-0.98385073324997617515,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
193 (float)-1.11047823227097316719, (float)-2.18954076314139673147, (float)-2.36498032881953056225, (float)-0.95484132880101140785,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
194 (float)-0.23924057925542965158, (float)-0.13865235703915925642, (float) 0.43587843191057992846, (float) 0.65903257226026665927,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
195 (float) 0.24361815372443152787, (float)-0.00235974960154720097, (float) 0.01844166574603346289, (float) 0.01722945988740875099
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
196 };
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
197
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
198
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
199 static double scalar16_(const float* x, const float* y)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
200 {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
201 return
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
202 x[ 0]*y[ 0] + x[ 1]*y[ 1] + x[ 2]*y[ 2] + x[ 3]*y[ 3] +
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
203 x[ 4]*y[ 4] + x[ 5]*y[ 5] + x[ 6]*y[ 6] + x[ 7]*y[ 7] +
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
204 x[ 8]*y[ 8] + x[ 9]*y[ 9] + x[10]*y[10] + x[11]*y[11] +
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
205 x[12]*y[12] + x[13]*y[13] + x[14]*y[14] + x[15]*y[15];
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
206 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
207
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
208
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
209 void FLAC__replaygain_synthesis__init_dither_context(DitherContext *d, int bits, int shapingtype)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
210 {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
211 static unsigned char default_dither [] = { 92, 92, 88, 84, 81, 78, 74, 67, 0, 0 };
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
212 static const float* F [] = { F44_0, F44_1, F44_2, F44_3 };
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
213
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
214 int index;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
215
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
216 if (shapingtype < 0) shapingtype = 0;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
217 if (shapingtype > 3) shapingtype = 3;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
218 d->ShapingType = (NoiseShaping)shapingtype;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
219 index = bits - 11 - shapingtype;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
220 if (index < 0) index = 0;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
221 if (index > 9) index = 9;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
222
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
223 memset ( d->ErrorHistory , 0, sizeof (d->ErrorHistory ) );
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
224 memset ( d->DitherHistory, 0, sizeof (d->DitherHistory) );
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
225
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
226 d->FilterCoeff = F [shapingtype];
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
227 d->Mask = ((FLAC__uint64)-1) << (32 - bits);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
228 d->Add = 0.5 * ((1L << (32 - bits)) - 1);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
229 d->Dither = 0.01f*default_dither[index] / (((FLAC__int64)1) << bits);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
230 d->LastHistoryIndex = 0;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
231 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
232
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
233 /*
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
234 * the following is based on parts of wavegain.c
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
235 */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
236
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
237 static FLAC__INLINE FLAC__int64 dither_output_(DitherContext *d, FLAC__bool do_dithering, int shapingtype, int i, double Sum, int k)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
238 {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
239 union {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
240 double d;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
241 FLAC__int64 i;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
242 } doubletmp;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
243 double Sum2;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
244 FLAC__int64 val;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
245
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
246 #define ROUND64(x) ( doubletmp.d = (x) + d->Add + (FLAC__int64)FLAC__I64L(0x001FFFFD80000000), doubletmp.i - (FLAC__int64)FLAC__I64L(0x433FFFFD80000000) )
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
247
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
248 if(do_dithering) {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
249 if(shapingtype == 0) {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
250 double tmp = random_equi_(d->Dither);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
251 Sum2 = tmp - d->LastRandomNumber [k];
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
252 d->LastRandomNumber [k] = (int)tmp;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
253 Sum2 = Sum += Sum2;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
254 val = ROUND64(Sum2) & d->Mask;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
255 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
256 else {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
257 Sum2 = random_triangular_(d->Dither) - scalar16_(d->DitherHistory[k], d->FilterCoeff + i);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
258 Sum += d->DitherHistory [k] [(-1-i)&15] = (float)Sum2;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
259 Sum2 = Sum + scalar16_(d->ErrorHistory [k], d->FilterCoeff + i);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
260 val = ROUND64(Sum2) & d->Mask;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
261 d->ErrorHistory [k] [(-1-i)&15] = (float)(Sum - val);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
262 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
263 return val;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
264 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
265 else
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
266 return ROUND64(Sum);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
267
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
268 #undef ROUND64
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
269 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
270
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
271 #if 0
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
272 float peak = 0.f,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
273 new_peak,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
274 factor_clip
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
275 double scale,
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
276 dB;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
277
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
278 ...
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
279
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
280 peak is in the range -32768.0 .. 32767.0
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
281
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
282 /* calculate factors for ReplayGain and ClippingPrevention */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
283 *track_gain = GetTitleGain() + settings->man_gain;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
284 scale = (float) pow(10., *track_gain * 0.05);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
285 if(settings->clip_prev) {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
286 factor_clip = (float) (32767./( peak + 1));
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
287 if(scale < factor_clip)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
288 factor_clip = 1.f;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
289 else
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
290 factor_clip /= scale;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
291 scale *= factor_clip;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
292 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
293 new_peak = (float) peak * scale;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
294
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
295 dB = 20. * log10(scale);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
296 *track_gain = (float) dB;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
297
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
298 const double scale = pow(10., (double)gain * 0.05);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
299 #endif
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
300
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
301
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
302 size_t FLAC__replaygain_synthesis__apply_gain(FLAC__byte *data_out, FLAC__bool little_endian_data_out, FLAC__bool unsigned_data_out, const FLAC__int32 * const input[], unsigned wide_samples, unsigned channels, const unsigned source_bps, const unsigned target_bps, const double scale, const FLAC__bool hard_limit, FLAC__bool do_dithering, DitherContext *dither_context)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
303 {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
304 static const FLAC__int32 conv_factors_[33] = {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
305 -1, /* 0 bits-per-sample (not supported) */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
306 -1, /* 1 bits-per-sample (not supported) */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
307 -1, /* 2 bits-per-sample (not supported) */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
308 -1, /* 3 bits-per-sample (not supported) */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
309 268435456, /* 4 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
310 134217728, /* 5 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
311 67108864, /* 6 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
312 33554432, /* 7 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
313 16777216, /* 8 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
314 8388608, /* 9 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
315 4194304, /* 10 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
316 2097152, /* 11 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
317 1048576, /* 12 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
318 524288, /* 13 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
319 262144, /* 14 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
320 131072, /* 15 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
321 65536, /* 16 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
322 32768, /* 17 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
323 16384, /* 18 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
324 8192, /* 19 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
325 4096, /* 20 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
326 2048, /* 21 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
327 1024, /* 22 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
328 512, /* 23 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
329 256, /* 24 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
330 128, /* 25 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
331 64, /* 26 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
332 32, /* 27 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
333 16, /* 28 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
334 8, /* 29 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
335 4, /* 30 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
336 2, /* 31 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
337 1 /* 32 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
338 };
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
339 static const FLAC__int64 hard_clip_factors_[33] = {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
340 0, /* 0 bits-per-sample (not supported) */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
341 0, /* 1 bits-per-sample (not supported) */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
342 0, /* 2 bits-per-sample (not supported) */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
343 0, /* 3 bits-per-sample (not supported) */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
344 -8, /* 4 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
345 -16, /* 5 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
346 -32, /* 6 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
347 -64, /* 7 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
348 -128, /* 8 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
349 -256, /* 9 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
350 -512, /* 10 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
351 -1024, /* 11 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
352 -2048, /* 12 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
353 -4096, /* 13 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
354 -8192, /* 14 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
355 -16384, /* 15 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
356 -32768, /* 16 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
357 -65536, /* 17 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
358 -131072, /* 18 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
359 -262144, /* 19 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
360 -524288, /* 20 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
361 -1048576, /* 21 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
362 -2097152, /* 22 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
363 -4194304, /* 23 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
364 -8388608, /* 24 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
365 -16777216, /* 25 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
366 -33554432, /* 26 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
367 -67108864, /* 27 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
368 -134217728, /* 28 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
369 -268435456, /* 29 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
370 -536870912, /* 30 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
371 -1073741824, /* 31 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
372 (FLAC__int64)(-1073741824) * 2 /* 32 bits-per-sample */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
373 };
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
374 const FLAC__int32 conv_factor = conv_factors_[target_bps];
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
375 const FLAC__int64 hard_clip_factor = hard_clip_factors_[target_bps];
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
376 /*
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
377 * The integer input coming in has a varying range based on the
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
378 * source_bps. We want to normalize it to [-1.0, 1.0) so instead
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
379 * of doing two multiplies on each sample, we just multiple
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
380 * 'scale' by 1/(2^(source_bps-1))
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
381 */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
382 const double multi_scale = scale / (double)(1u << (source_bps-1));
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
383
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
384 FLAC__byte * const start = data_out;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
385 unsigned i, channel;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
386 const FLAC__int32 *input_;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
387 double sample;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
388 const unsigned bytes_per_sample = target_bps / 8;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
389 const unsigned last_history_index = dither_context->LastHistoryIndex;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
390 NoiseShaping noise_shaping = dither_context->ShapingType;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
391 FLAC__int64 val64;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
392 FLAC__int32 val32;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
393 FLAC__int32 uval32;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
394 const FLAC__uint32 twiggle = 1u << (target_bps - 1);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
395
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
396 FLAC__ASSERT(channels > 0 && channels <= FLAC_SHARE__MAX_SUPPORTED_CHANNELS);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
397 FLAC__ASSERT(source_bps >= 4);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
398 FLAC__ASSERT(target_bps >= 4);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
399 FLAC__ASSERT(source_bps <= 32);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
400 FLAC__ASSERT(target_bps < 32);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
401 FLAC__ASSERT((target_bps & 7) == 0);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
402
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
403 for(channel = 0; channel < channels; channel++) {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
404 const unsigned incr = bytes_per_sample * channels;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
405 data_out = start + bytes_per_sample * channel;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
406 input_ = input[channel];
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
407 for(i = 0; i < wide_samples; i++, data_out += incr) {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
408 sample = (double)input_[i] * multi_scale;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
409
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
410 if(hard_limit) {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
411 /* hard 6dB limiting */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
412 if(sample < -0.5)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
413 sample = tanh((sample + 0.5) / (1-0.5)) * (1-0.5) - 0.5;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
414 else if(sample > 0.5)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
415 sample = tanh((sample - 0.5) / (1-0.5)) * (1-0.5) + 0.5;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
416 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
417 sample *= 2147483647.f;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
418
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
419 val64 = dither_output_(dither_context, do_dithering, noise_shaping, (i + last_history_index) % 32, sample, channel) / conv_factor;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
420
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
421 val32 = (FLAC__int32)val64;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
422 if(val64 >= -hard_clip_factor)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
423 val32 = (FLAC__int32)(-(hard_clip_factor+1));
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
424 else if(val64 < hard_clip_factor)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
425 val32 = (FLAC__int32)hard_clip_factor;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
426
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
427 uval32 = (FLAC__uint32)val32;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
428 if (unsigned_data_out)
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
429 uval32 ^= twiggle;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
430
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
431 if (little_endian_data_out) {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
432 switch(target_bps) {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
433 case 24:
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
434 data_out[2] = (FLAC__byte)(uval32 >> 16);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
435 /* fall through */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
436 case 16:
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
437 data_out[1] = (FLAC__byte)(uval32 >> 8);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
438 /* fall through */
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
439 case 8:
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
440 data_out[0] = (FLAC__byte)uval32;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
441 break;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
442 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
443 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
444 else {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
445 switch(target_bps) {
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
446 case 24:
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
447 data_out[0] = (FLAC__byte)(uval32 >> 16);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
448 data_out[1] = (FLAC__byte)(uval32 >> 8);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
449 data_out[2] = (FLAC__byte)uval32;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
450 break;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
451 case 16:
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
452 data_out[0] = (FLAC__byte)(uval32 >> 8);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
453 data_out[1] = (FLAC__byte)uval32;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
454 break;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
455 case 8:
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
456 data_out[0] = (FLAC__byte)uval32;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
457 break;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
458 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
459 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
460 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
461 }
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
462 dither_context->LastHistoryIndex = (last_history_index + wide_samples) % 32;
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
463
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
464 return wide_samples * channels * (target_bps/8);
fa848bd484d8 [svn] Move plugins to Plugins/
nenolod
parents:
diff changeset
465 }