1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/webaudio/blink/DirectConvolver.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,347 @@ 1.4 +/* 1.5 + * Copyright (C) 2012 Intel Inc. All rights reserved. 1.6 + * 1.7 + * Redistribution and use in source and binary forms, with or without 1.8 + * modification, are permitted provided that the following conditions 1.9 + * are met: 1.10 + * 1.11 + * 1. Redistributions of source code must retain the above copyright 1.12 + * notice, this list of conditions and the following disclaimer. 1.13 + * 2. Redistributions in binary form must reproduce the above copyright 1.14 + * notice, this list of conditions and the following disclaimer in the 1.15 + * documentation and/or other materials provided with the distribution. 1.16 + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 1.17 + * its contributors may be used to endorse or promote products derived 1.18 + * from this software without specific prior written permission. 1.19 + * 1.20 + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 1.21 + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 1.22 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 1.23 + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 1.24 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 1.25 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 1.26 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 1.27 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.28 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 1.29 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.30 + */ 1.31 + 1.32 +#include "DirectConvolver.h" 1.33 +#include "mozilla/PodOperations.h" 1.34 + 1.35 +using namespace mozilla; 1.36 + 1.37 +namespace WebCore { 1.38 + 1.39 +DirectConvolver::DirectConvolver(size_t inputBlockSize) 1.40 + : m_inputBlockSize(inputBlockSize) 1.41 +{ 1.42 + m_buffer.SetLength(inputBlockSize * 2); 1.43 + PodZero(m_buffer.Elements(), inputBlockSize * 2); 1.44 +} 1.45 + 1.46 +void DirectConvolver::process(const nsTArray<float>* convolutionKernel, const float* sourceP, float* destP, size_t framesToProcess) 1.47 +{ 1.48 + MOZ_ASSERT(framesToProcess == m_inputBlockSize); 1.49 + if (framesToProcess != m_inputBlockSize) 1.50 + return; 1.51 + 1.52 + // Only support kernelSize <= m_inputBlockSize 1.53 + size_t kernelSize = convolutionKernel->Length(); 1.54 + MOZ_ASSERT(kernelSize <= m_inputBlockSize); 1.55 + if (kernelSize > m_inputBlockSize) 1.56 + return; 1.57 + 1.58 + const float* kernelP = convolutionKernel->Elements(); 1.59 + 1.60 + // Sanity check 1.61 + bool isCopyGood = kernelP && sourceP && destP && m_buffer.Elements(); 1.62 + MOZ_ASSERT(isCopyGood); 1.63 + if (!isCopyGood) 1.64 + return; 1.65 + 1.66 + float* inputP = m_buffer.Elements() + m_inputBlockSize; 1.67 + 1.68 + // Copy samples to 2nd half of input buffer. 1.69 + memcpy(inputP, sourceP, sizeof(float) * framesToProcess); 1.70 + 1.71 + // FIXME: The macro can be further optimized to avoid pipeline stalls. One possibility is to maintain 4 separate sums and change the macro to CONVOLVE_FOUR_SAMPLES. 1.72 +#define CONVOLVE_ONE_SAMPLE \ 1.73 + sum += inputP[i - j] * kernelP[j]; \ 1.74 + j++; 1.75 + 1.76 + size_t i = 0; 1.77 + while (i < framesToProcess) { 1.78 + size_t j = 0; 1.79 + float sum = 0; 1.80 + 1.81 + // FIXME: SSE optimization may be applied here. 1.82 + if (kernelSize == 32) { 1.83 + CONVOLVE_ONE_SAMPLE // 1 1.84 + CONVOLVE_ONE_SAMPLE // 2 1.85 + CONVOLVE_ONE_SAMPLE // 3 1.86 + CONVOLVE_ONE_SAMPLE // 4 1.87 + CONVOLVE_ONE_SAMPLE // 5 1.88 + CONVOLVE_ONE_SAMPLE // 6 1.89 + CONVOLVE_ONE_SAMPLE // 7 1.90 + CONVOLVE_ONE_SAMPLE // 8 1.91 + CONVOLVE_ONE_SAMPLE // 9 1.92 + CONVOLVE_ONE_SAMPLE // 10 1.93 + 1.94 + CONVOLVE_ONE_SAMPLE // 11 1.95 + CONVOLVE_ONE_SAMPLE // 12 1.96 + CONVOLVE_ONE_SAMPLE // 13 1.97 + CONVOLVE_ONE_SAMPLE // 14 1.98 + CONVOLVE_ONE_SAMPLE // 15 1.99 + CONVOLVE_ONE_SAMPLE // 16 1.100 + CONVOLVE_ONE_SAMPLE // 17 1.101 + CONVOLVE_ONE_SAMPLE // 18 1.102 + CONVOLVE_ONE_SAMPLE // 19 1.103 + CONVOLVE_ONE_SAMPLE // 20 1.104 + 1.105 + CONVOLVE_ONE_SAMPLE // 21 1.106 + CONVOLVE_ONE_SAMPLE // 22 1.107 + CONVOLVE_ONE_SAMPLE // 23 1.108 + CONVOLVE_ONE_SAMPLE // 24 1.109 + CONVOLVE_ONE_SAMPLE // 25 1.110 + CONVOLVE_ONE_SAMPLE // 26 1.111 + CONVOLVE_ONE_SAMPLE // 27 1.112 + CONVOLVE_ONE_SAMPLE // 28 1.113 + CONVOLVE_ONE_SAMPLE // 29 1.114 + CONVOLVE_ONE_SAMPLE // 30 1.115 + 1.116 + CONVOLVE_ONE_SAMPLE // 31 1.117 + CONVOLVE_ONE_SAMPLE // 32 1.118 + 1.119 + } else if (kernelSize == 64) { 1.120 + CONVOLVE_ONE_SAMPLE // 1 1.121 + CONVOLVE_ONE_SAMPLE // 2 1.122 + CONVOLVE_ONE_SAMPLE // 3 1.123 + CONVOLVE_ONE_SAMPLE // 4 1.124 + CONVOLVE_ONE_SAMPLE // 5 1.125 + CONVOLVE_ONE_SAMPLE // 6 1.126 + CONVOLVE_ONE_SAMPLE // 7 1.127 + CONVOLVE_ONE_SAMPLE // 8 1.128 + CONVOLVE_ONE_SAMPLE // 9 1.129 + CONVOLVE_ONE_SAMPLE // 10 1.130 + 1.131 + CONVOLVE_ONE_SAMPLE // 11 1.132 + CONVOLVE_ONE_SAMPLE // 12 1.133 + CONVOLVE_ONE_SAMPLE // 13 1.134 + CONVOLVE_ONE_SAMPLE // 14 1.135 + CONVOLVE_ONE_SAMPLE // 15 1.136 + CONVOLVE_ONE_SAMPLE // 16 1.137 + CONVOLVE_ONE_SAMPLE // 17 1.138 + CONVOLVE_ONE_SAMPLE // 18 1.139 + CONVOLVE_ONE_SAMPLE // 19 1.140 + CONVOLVE_ONE_SAMPLE // 20 1.141 + 1.142 + CONVOLVE_ONE_SAMPLE // 21 1.143 + CONVOLVE_ONE_SAMPLE // 22 1.144 + CONVOLVE_ONE_SAMPLE // 23 1.145 + CONVOLVE_ONE_SAMPLE // 24 1.146 + CONVOLVE_ONE_SAMPLE // 25 1.147 + CONVOLVE_ONE_SAMPLE // 26 1.148 + CONVOLVE_ONE_SAMPLE // 27 1.149 + CONVOLVE_ONE_SAMPLE // 28 1.150 + CONVOLVE_ONE_SAMPLE // 29 1.151 + CONVOLVE_ONE_SAMPLE // 30 1.152 + 1.153 + CONVOLVE_ONE_SAMPLE // 31 1.154 + CONVOLVE_ONE_SAMPLE // 32 1.155 + CONVOLVE_ONE_SAMPLE // 33 1.156 + CONVOLVE_ONE_SAMPLE // 34 1.157 + CONVOLVE_ONE_SAMPLE // 35 1.158 + CONVOLVE_ONE_SAMPLE // 36 1.159 + CONVOLVE_ONE_SAMPLE // 37 1.160 + CONVOLVE_ONE_SAMPLE // 38 1.161 + CONVOLVE_ONE_SAMPLE // 39 1.162 + CONVOLVE_ONE_SAMPLE // 40 1.163 + 1.164 + CONVOLVE_ONE_SAMPLE // 41 1.165 + CONVOLVE_ONE_SAMPLE // 42 1.166 + CONVOLVE_ONE_SAMPLE // 43 1.167 + CONVOLVE_ONE_SAMPLE // 44 1.168 + CONVOLVE_ONE_SAMPLE // 45 1.169 + CONVOLVE_ONE_SAMPLE // 46 1.170 + CONVOLVE_ONE_SAMPLE // 47 1.171 + CONVOLVE_ONE_SAMPLE // 48 1.172 + CONVOLVE_ONE_SAMPLE // 49 1.173 + CONVOLVE_ONE_SAMPLE // 50 1.174 + 1.175 + CONVOLVE_ONE_SAMPLE // 51 1.176 + CONVOLVE_ONE_SAMPLE // 52 1.177 + CONVOLVE_ONE_SAMPLE // 53 1.178 + CONVOLVE_ONE_SAMPLE // 54 1.179 + CONVOLVE_ONE_SAMPLE // 55 1.180 + CONVOLVE_ONE_SAMPLE // 56 1.181 + CONVOLVE_ONE_SAMPLE // 57 1.182 + CONVOLVE_ONE_SAMPLE // 58 1.183 + CONVOLVE_ONE_SAMPLE // 59 1.184 + CONVOLVE_ONE_SAMPLE // 60 1.185 + 1.186 + CONVOLVE_ONE_SAMPLE // 61 1.187 + CONVOLVE_ONE_SAMPLE // 62 1.188 + CONVOLVE_ONE_SAMPLE // 63 1.189 + CONVOLVE_ONE_SAMPLE // 64 1.190 + 1.191 + } else if (kernelSize == 128) { 1.192 + CONVOLVE_ONE_SAMPLE // 1 1.193 + CONVOLVE_ONE_SAMPLE // 2 1.194 + CONVOLVE_ONE_SAMPLE // 3 1.195 + CONVOLVE_ONE_SAMPLE // 4 1.196 + CONVOLVE_ONE_SAMPLE // 5 1.197 + CONVOLVE_ONE_SAMPLE // 6 1.198 + CONVOLVE_ONE_SAMPLE // 7 1.199 + CONVOLVE_ONE_SAMPLE // 8 1.200 + CONVOLVE_ONE_SAMPLE // 9 1.201 + CONVOLVE_ONE_SAMPLE // 10 1.202 + 1.203 + CONVOLVE_ONE_SAMPLE // 11 1.204 + CONVOLVE_ONE_SAMPLE // 12 1.205 + CONVOLVE_ONE_SAMPLE // 13 1.206 + CONVOLVE_ONE_SAMPLE // 14 1.207 + CONVOLVE_ONE_SAMPLE // 15 1.208 + CONVOLVE_ONE_SAMPLE // 16 1.209 + CONVOLVE_ONE_SAMPLE // 17 1.210 + CONVOLVE_ONE_SAMPLE // 18 1.211 + CONVOLVE_ONE_SAMPLE // 19 1.212 + CONVOLVE_ONE_SAMPLE // 20 1.213 + 1.214 + CONVOLVE_ONE_SAMPLE // 21 1.215 + CONVOLVE_ONE_SAMPLE // 22 1.216 + CONVOLVE_ONE_SAMPLE // 23 1.217 + CONVOLVE_ONE_SAMPLE // 24 1.218 + CONVOLVE_ONE_SAMPLE // 25 1.219 + CONVOLVE_ONE_SAMPLE // 26 1.220 + CONVOLVE_ONE_SAMPLE // 27 1.221 + CONVOLVE_ONE_SAMPLE // 28 1.222 + CONVOLVE_ONE_SAMPLE // 29 1.223 + CONVOLVE_ONE_SAMPLE // 30 1.224 + 1.225 + CONVOLVE_ONE_SAMPLE // 31 1.226 + CONVOLVE_ONE_SAMPLE // 32 1.227 + CONVOLVE_ONE_SAMPLE // 33 1.228 + CONVOLVE_ONE_SAMPLE // 34 1.229 + CONVOLVE_ONE_SAMPLE // 35 1.230 + CONVOLVE_ONE_SAMPLE // 36 1.231 + CONVOLVE_ONE_SAMPLE // 37 1.232 + CONVOLVE_ONE_SAMPLE // 38 1.233 + CONVOLVE_ONE_SAMPLE // 39 1.234 + CONVOLVE_ONE_SAMPLE // 40 1.235 + 1.236 + CONVOLVE_ONE_SAMPLE // 41 1.237 + CONVOLVE_ONE_SAMPLE // 42 1.238 + CONVOLVE_ONE_SAMPLE // 43 1.239 + CONVOLVE_ONE_SAMPLE // 44 1.240 + CONVOLVE_ONE_SAMPLE // 45 1.241 + CONVOLVE_ONE_SAMPLE // 46 1.242 + CONVOLVE_ONE_SAMPLE // 47 1.243 + CONVOLVE_ONE_SAMPLE // 48 1.244 + CONVOLVE_ONE_SAMPLE // 49 1.245 + CONVOLVE_ONE_SAMPLE // 50 1.246 + 1.247 + CONVOLVE_ONE_SAMPLE // 51 1.248 + CONVOLVE_ONE_SAMPLE // 52 1.249 + CONVOLVE_ONE_SAMPLE // 53 1.250 + CONVOLVE_ONE_SAMPLE // 54 1.251 + CONVOLVE_ONE_SAMPLE // 55 1.252 + CONVOLVE_ONE_SAMPLE // 56 1.253 + CONVOLVE_ONE_SAMPLE // 57 1.254 + CONVOLVE_ONE_SAMPLE // 58 1.255 + CONVOLVE_ONE_SAMPLE // 59 1.256 + CONVOLVE_ONE_SAMPLE // 60 1.257 + 1.258 + CONVOLVE_ONE_SAMPLE // 61 1.259 + CONVOLVE_ONE_SAMPLE // 62 1.260 + CONVOLVE_ONE_SAMPLE // 63 1.261 + CONVOLVE_ONE_SAMPLE // 64 1.262 + CONVOLVE_ONE_SAMPLE // 65 1.263 + CONVOLVE_ONE_SAMPLE // 66 1.264 + CONVOLVE_ONE_SAMPLE // 67 1.265 + CONVOLVE_ONE_SAMPLE // 68 1.266 + CONVOLVE_ONE_SAMPLE // 69 1.267 + CONVOLVE_ONE_SAMPLE // 70 1.268 + 1.269 + CONVOLVE_ONE_SAMPLE // 71 1.270 + CONVOLVE_ONE_SAMPLE // 72 1.271 + CONVOLVE_ONE_SAMPLE // 73 1.272 + CONVOLVE_ONE_SAMPLE // 74 1.273 + CONVOLVE_ONE_SAMPLE // 75 1.274 + CONVOLVE_ONE_SAMPLE // 76 1.275 + CONVOLVE_ONE_SAMPLE // 77 1.276 + CONVOLVE_ONE_SAMPLE // 78 1.277 + CONVOLVE_ONE_SAMPLE // 79 1.278 + CONVOLVE_ONE_SAMPLE // 80 1.279 + 1.280 + CONVOLVE_ONE_SAMPLE // 81 1.281 + CONVOLVE_ONE_SAMPLE // 82 1.282 + CONVOLVE_ONE_SAMPLE // 83 1.283 + CONVOLVE_ONE_SAMPLE // 84 1.284 + CONVOLVE_ONE_SAMPLE // 85 1.285 + CONVOLVE_ONE_SAMPLE // 86 1.286 + CONVOLVE_ONE_SAMPLE // 87 1.287 + CONVOLVE_ONE_SAMPLE // 88 1.288 + CONVOLVE_ONE_SAMPLE // 89 1.289 + CONVOLVE_ONE_SAMPLE // 90 1.290 + 1.291 + CONVOLVE_ONE_SAMPLE // 91 1.292 + CONVOLVE_ONE_SAMPLE // 92 1.293 + CONVOLVE_ONE_SAMPLE // 93 1.294 + CONVOLVE_ONE_SAMPLE // 94 1.295 + CONVOLVE_ONE_SAMPLE // 95 1.296 + CONVOLVE_ONE_SAMPLE // 96 1.297 + CONVOLVE_ONE_SAMPLE // 97 1.298 + CONVOLVE_ONE_SAMPLE // 98 1.299 + CONVOLVE_ONE_SAMPLE // 99 1.300 + CONVOLVE_ONE_SAMPLE // 100 1.301 + 1.302 + CONVOLVE_ONE_SAMPLE // 101 1.303 + CONVOLVE_ONE_SAMPLE // 102 1.304 + CONVOLVE_ONE_SAMPLE // 103 1.305 + CONVOLVE_ONE_SAMPLE // 104 1.306 + CONVOLVE_ONE_SAMPLE // 105 1.307 + CONVOLVE_ONE_SAMPLE // 106 1.308 + CONVOLVE_ONE_SAMPLE // 107 1.309 + CONVOLVE_ONE_SAMPLE // 108 1.310 + CONVOLVE_ONE_SAMPLE // 109 1.311 + CONVOLVE_ONE_SAMPLE // 110 1.312 + 1.313 + CONVOLVE_ONE_SAMPLE // 111 1.314 + CONVOLVE_ONE_SAMPLE // 112 1.315 + CONVOLVE_ONE_SAMPLE // 113 1.316 + CONVOLVE_ONE_SAMPLE // 114 1.317 + CONVOLVE_ONE_SAMPLE // 115 1.318 + CONVOLVE_ONE_SAMPLE // 116 1.319 + CONVOLVE_ONE_SAMPLE // 117 1.320 + CONVOLVE_ONE_SAMPLE // 118 1.321 + CONVOLVE_ONE_SAMPLE // 119 1.322 + CONVOLVE_ONE_SAMPLE // 120 1.323 + 1.324 + CONVOLVE_ONE_SAMPLE // 121 1.325 + CONVOLVE_ONE_SAMPLE // 122 1.326 + CONVOLVE_ONE_SAMPLE // 123 1.327 + CONVOLVE_ONE_SAMPLE // 124 1.328 + CONVOLVE_ONE_SAMPLE // 125 1.329 + CONVOLVE_ONE_SAMPLE // 126 1.330 + CONVOLVE_ONE_SAMPLE // 127 1.331 + CONVOLVE_ONE_SAMPLE // 128 1.332 + } else { 1.333 + while (j < kernelSize) { 1.334 + // Non-optimized using actual while loop. 1.335 + CONVOLVE_ONE_SAMPLE 1.336 + } 1.337 + } 1.338 + destP[i++] = sum; 1.339 + } 1.340 + 1.341 + // Copy 2nd half of input buffer to 1st half. 1.342 + memcpy(m_buffer.Elements(), inputP, sizeof(float) * framesToProcess); 1.343 +} 1.344 + 1.345 +void DirectConvolver::reset() 1.346 +{ 1.347 + PodZero(m_buffer.Elements(), m_buffer.Length()); 1.348 +} 1.349 + 1.350 +} // namespace WebCore