content/media/webaudio/blink/DirectConvolver.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /*
michael@0 2 * Copyright (C) 2012 Intel Inc. All rights reserved.
michael@0 3 *
michael@0 4 * Redistribution and use in source and binary forms, with or without
michael@0 5 * modification, are permitted provided that the following conditions
michael@0 6 * are met:
michael@0 7 *
michael@0 8 * 1. Redistributions of source code must retain the above copyright
michael@0 9 * notice, this list of conditions and the following disclaimer.
michael@0 10 * 2. Redistributions in binary form must reproduce the above copyright
michael@0 11 * notice, this list of conditions and the following disclaimer in the
michael@0 12 * documentation and/or other materials provided with the distribution.
michael@0 13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
michael@0 14 * its contributors may be used to endorse or promote products derived
michael@0 15 * from this software without specific prior written permission.
michael@0 16 *
michael@0 17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
michael@0 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
michael@0 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
michael@0 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
michael@0 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
michael@0 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
michael@0 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
michael@0 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
michael@0 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 27 */
michael@0 28
michael@0 29 #include "DirectConvolver.h"
michael@0 30 #include "mozilla/PodOperations.h"
michael@0 31
michael@0 32 using namespace mozilla;
michael@0 33
michael@0 34 namespace WebCore {
michael@0 35
michael@0 36 DirectConvolver::DirectConvolver(size_t inputBlockSize)
michael@0 37 : m_inputBlockSize(inputBlockSize)
michael@0 38 {
michael@0 39 m_buffer.SetLength(inputBlockSize * 2);
michael@0 40 PodZero(m_buffer.Elements(), inputBlockSize * 2);
michael@0 41 }
michael@0 42
michael@0 43 void DirectConvolver::process(const nsTArray<float>* convolutionKernel, const float* sourceP, float* destP, size_t framesToProcess)
michael@0 44 {
michael@0 45 MOZ_ASSERT(framesToProcess == m_inputBlockSize);
michael@0 46 if (framesToProcess != m_inputBlockSize)
michael@0 47 return;
michael@0 48
michael@0 49 // Only support kernelSize <= m_inputBlockSize
michael@0 50 size_t kernelSize = convolutionKernel->Length();
michael@0 51 MOZ_ASSERT(kernelSize <= m_inputBlockSize);
michael@0 52 if (kernelSize > m_inputBlockSize)
michael@0 53 return;
michael@0 54
michael@0 55 const float* kernelP = convolutionKernel->Elements();
michael@0 56
michael@0 57 // Sanity check
michael@0 58 bool isCopyGood = kernelP && sourceP && destP && m_buffer.Elements();
michael@0 59 MOZ_ASSERT(isCopyGood);
michael@0 60 if (!isCopyGood)
michael@0 61 return;
michael@0 62
michael@0 63 float* inputP = m_buffer.Elements() + m_inputBlockSize;
michael@0 64
michael@0 65 // Copy samples to 2nd half of input buffer.
michael@0 66 memcpy(inputP, sourceP, sizeof(float) * framesToProcess);
michael@0 67
michael@0 68 // 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.
michael@0 69 #define CONVOLVE_ONE_SAMPLE \
michael@0 70 sum += inputP[i - j] * kernelP[j]; \
michael@0 71 j++;
michael@0 72
michael@0 73 size_t i = 0;
michael@0 74 while (i < framesToProcess) {
michael@0 75 size_t j = 0;
michael@0 76 float sum = 0;
michael@0 77
michael@0 78 // FIXME: SSE optimization may be applied here.
michael@0 79 if (kernelSize == 32) {
michael@0 80 CONVOLVE_ONE_SAMPLE // 1
michael@0 81 CONVOLVE_ONE_SAMPLE // 2
michael@0 82 CONVOLVE_ONE_SAMPLE // 3
michael@0 83 CONVOLVE_ONE_SAMPLE // 4
michael@0 84 CONVOLVE_ONE_SAMPLE // 5
michael@0 85 CONVOLVE_ONE_SAMPLE // 6
michael@0 86 CONVOLVE_ONE_SAMPLE // 7
michael@0 87 CONVOLVE_ONE_SAMPLE // 8
michael@0 88 CONVOLVE_ONE_SAMPLE // 9
michael@0 89 CONVOLVE_ONE_SAMPLE // 10
michael@0 90
michael@0 91 CONVOLVE_ONE_SAMPLE // 11
michael@0 92 CONVOLVE_ONE_SAMPLE // 12
michael@0 93 CONVOLVE_ONE_SAMPLE // 13
michael@0 94 CONVOLVE_ONE_SAMPLE // 14
michael@0 95 CONVOLVE_ONE_SAMPLE // 15
michael@0 96 CONVOLVE_ONE_SAMPLE // 16
michael@0 97 CONVOLVE_ONE_SAMPLE // 17
michael@0 98 CONVOLVE_ONE_SAMPLE // 18
michael@0 99 CONVOLVE_ONE_SAMPLE // 19
michael@0 100 CONVOLVE_ONE_SAMPLE // 20
michael@0 101
michael@0 102 CONVOLVE_ONE_SAMPLE // 21
michael@0 103 CONVOLVE_ONE_SAMPLE // 22
michael@0 104 CONVOLVE_ONE_SAMPLE // 23
michael@0 105 CONVOLVE_ONE_SAMPLE // 24
michael@0 106 CONVOLVE_ONE_SAMPLE // 25
michael@0 107 CONVOLVE_ONE_SAMPLE // 26
michael@0 108 CONVOLVE_ONE_SAMPLE // 27
michael@0 109 CONVOLVE_ONE_SAMPLE // 28
michael@0 110 CONVOLVE_ONE_SAMPLE // 29
michael@0 111 CONVOLVE_ONE_SAMPLE // 30
michael@0 112
michael@0 113 CONVOLVE_ONE_SAMPLE // 31
michael@0 114 CONVOLVE_ONE_SAMPLE // 32
michael@0 115
michael@0 116 } else if (kernelSize == 64) {
michael@0 117 CONVOLVE_ONE_SAMPLE // 1
michael@0 118 CONVOLVE_ONE_SAMPLE // 2
michael@0 119 CONVOLVE_ONE_SAMPLE // 3
michael@0 120 CONVOLVE_ONE_SAMPLE // 4
michael@0 121 CONVOLVE_ONE_SAMPLE // 5
michael@0 122 CONVOLVE_ONE_SAMPLE // 6
michael@0 123 CONVOLVE_ONE_SAMPLE // 7
michael@0 124 CONVOLVE_ONE_SAMPLE // 8
michael@0 125 CONVOLVE_ONE_SAMPLE // 9
michael@0 126 CONVOLVE_ONE_SAMPLE // 10
michael@0 127
michael@0 128 CONVOLVE_ONE_SAMPLE // 11
michael@0 129 CONVOLVE_ONE_SAMPLE // 12
michael@0 130 CONVOLVE_ONE_SAMPLE // 13
michael@0 131 CONVOLVE_ONE_SAMPLE // 14
michael@0 132 CONVOLVE_ONE_SAMPLE // 15
michael@0 133 CONVOLVE_ONE_SAMPLE // 16
michael@0 134 CONVOLVE_ONE_SAMPLE // 17
michael@0 135 CONVOLVE_ONE_SAMPLE // 18
michael@0 136 CONVOLVE_ONE_SAMPLE // 19
michael@0 137 CONVOLVE_ONE_SAMPLE // 20
michael@0 138
michael@0 139 CONVOLVE_ONE_SAMPLE // 21
michael@0 140 CONVOLVE_ONE_SAMPLE // 22
michael@0 141 CONVOLVE_ONE_SAMPLE // 23
michael@0 142 CONVOLVE_ONE_SAMPLE // 24
michael@0 143 CONVOLVE_ONE_SAMPLE // 25
michael@0 144 CONVOLVE_ONE_SAMPLE // 26
michael@0 145 CONVOLVE_ONE_SAMPLE // 27
michael@0 146 CONVOLVE_ONE_SAMPLE // 28
michael@0 147 CONVOLVE_ONE_SAMPLE // 29
michael@0 148 CONVOLVE_ONE_SAMPLE // 30
michael@0 149
michael@0 150 CONVOLVE_ONE_SAMPLE // 31
michael@0 151 CONVOLVE_ONE_SAMPLE // 32
michael@0 152 CONVOLVE_ONE_SAMPLE // 33
michael@0 153 CONVOLVE_ONE_SAMPLE // 34
michael@0 154 CONVOLVE_ONE_SAMPLE // 35
michael@0 155 CONVOLVE_ONE_SAMPLE // 36
michael@0 156 CONVOLVE_ONE_SAMPLE // 37
michael@0 157 CONVOLVE_ONE_SAMPLE // 38
michael@0 158 CONVOLVE_ONE_SAMPLE // 39
michael@0 159 CONVOLVE_ONE_SAMPLE // 40
michael@0 160
michael@0 161 CONVOLVE_ONE_SAMPLE // 41
michael@0 162 CONVOLVE_ONE_SAMPLE // 42
michael@0 163 CONVOLVE_ONE_SAMPLE // 43
michael@0 164 CONVOLVE_ONE_SAMPLE // 44
michael@0 165 CONVOLVE_ONE_SAMPLE // 45
michael@0 166 CONVOLVE_ONE_SAMPLE // 46
michael@0 167 CONVOLVE_ONE_SAMPLE // 47
michael@0 168 CONVOLVE_ONE_SAMPLE // 48
michael@0 169 CONVOLVE_ONE_SAMPLE // 49
michael@0 170 CONVOLVE_ONE_SAMPLE // 50
michael@0 171
michael@0 172 CONVOLVE_ONE_SAMPLE // 51
michael@0 173 CONVOLVE_ONE_SAMPLE // 52
michael@0 174 CONVOLVE_ONE_SAMPLE // 53
michael@0 175 CONVOLVE_ONE_SAMPLE // 54
michael@0 176 CONVOLVE_ONE_SAMPLE // 55
michael@0 177 CONVOLVE_ONE_SAMPLE // 56
michael@0 178 CONVOLVE_ONE_SAMPLE // 57
michael@0 179 CONVOLVE_ONE_SAMPLE // 58
michael@0 180 CONVOLVE_ONE_SAMPLE // 59
michael@0 181 CONVOLVE_ONE_SAMPLE // 60
michael@0 182
michael@0 183 CONVOLVE_ONE_SAMPLE // 61
michael@0 184 CONVOLVE_ONE_SAMPLE // 62
michael@0 185 CONVOLVE_ONE_SAMPLE // 63
michael@0 186 CONVOLVE_ONE_SAMPLE // 64
michael@0 187
michael@0 188 } else if (kernelSize == 128) {
michael@0 189 CONVOLVE_ONE_SAMPLE // 1
michael@0 190 CONVOLVE_ONE_SAMPLE // 2
michael@0 191 CONVOLVE_ONE_SAMPLE // 3
michael@0 192 CONVOLVE_ONE_SAMPLE // 4
michael@0 193 CONVOLVE_ONE_SAMPLE // 5
michael@0 194 CONVOLVE_ONE_SAMPLE // 6
michael@0 195 CONVOLVE_ONE_SAMPLE // 7
michael@0 196 CONVOLVE_ONE_SAMPLE // 8
michael@0 197 CONVOLVE_ONE_SAMPLE // 9
michael@0 198 CONVOLVE_ONE_SAMPLE // 10
michael@0 199
michael@0 200 CONVOLVE_ONE_SAMPLE // 11
michael@0 201 CONVOLVE_ONE_SAMPLE // 12
michael@0 202 CONVOLVE_ONE_SAMPLE // 13
michael@0 203 CONVOLVE_ONE_SAMPLE // 14
michael@0 204 CONVOLVE_ONE_SAMPLE // 15
michael@0 205 CONVOLVE_ONE_SAMPLE // 16
michael@0 206 CONVOLVE_ONE_SAMPLE // 17
michael@0 207 CONVOLVE_ONE_SAMPLE // 18
michael@0 208 CONVOLVE_ONE_SAMPLE // 19
michael@0 209 CONVOLVE_ONE_SAMPLE // 20
michael@0 210
michael@0 211 CONVOLVE_ONE_SAMPLE // 21
michael@0 212 CONVOLVE_ONE_SAMPLE // 22
michael@0 213 CONVOLVE_ONE_SAMPLE // 23
michael@0 214 CONVOLVE_ONE_SAMPLE // 24
michael@0 215 CONVOLVE_ONE_SAMPLE // 25
michael@0 216 CONVOLVE_ONE_SAMPLE // 26
michael@0 217 CONVOLVE_ONE_SAMPLE // 27
michael@0 218 CONVOLVE_ONE_SAMPLE // 28
michael@0 219 CONVOLVE_ONE_SAMPLE // 29
michael@0 220 CONVOLVE_ONE_SAMPLE // 30
michael@0 221
michael@0 222 CONVOLVE_ONE_SAMPLE // 31
michael@0 223 CONVOLVE_ONE_SAMPLE // 32
michael@0 224 CONVOLVE_ONE_SAMPLE // 33
michael@0 225 CONVOLVE_ONE_SAMPLE // 34
michael@0 226 CONVOLVE_ONE_SAMPLE // 35
michael@0 227 CONVOLVE_ONE_SAMPLE // 36
michael@0 228 CONVOLVE_ONE_SAMPLE // 37
michael@0 229 CONVOLVE_ONE_SAMPLE // 38
michael@0 230 CONVOLVE_ONE_SAMPLE // 39
michael@0 231 CONVOLVE_ONE_SAMPLE // 40
michael@0 232
michael@0 233 CONVOLVE_ONE_SAMPLE // 41
michael@0 234 CONVOLVE_ONE_SAMPLE // 42
michael@0 235 CONVOLVE_ONE_SAMPLE // 43
michael@0 236 CONVOLVE_ONE_SAMPLE // 44
michael@0 237 CONVOLVE_ONE_SAMPLE // 45
michael@0 238 CONVOLVE_ONE_SAMPLE // 46
michael@0 239 CONVOLVE_ONE_SAMPLE // 47
michael@0 240 CONVOLVE_ONE_SAMPLE // 48
michael@0 241 CONVOLVE_ONE_SAMPLE // 49
michael@0 242 CONVOLVE_ONE_SAMPLE // 50
michael@0 243
michael@0 244 CONVOLVE_ONE_SAMPLE // 51
michael@0 245 CONVOLVE_ONE_SAMPLE // 52
michael@0 246 CONVOLVE_ONE_SAMPLE // 53
michael@0 247 CONVOLVE_ONE_SAMPLE // 54
michael@0 248 CONVOLVE_ONE_SAMPLE // 55
michael@0 249 CONVOLVE_ONE_SAMPLE // 56
michael@0 250 CONVOLVE_ONE_SAMPLE // 57
michael@0 251 CONVOLVE_ONE_SAMPLE // 58
michael@0 252 CONVOLVE_ONE_SAMPLE // 59
michael@0 253 CONVOLVE_ONE_SAMPLE // 60
michael@0 254
michael@0 255 CONVOLVE_ONE_SAMPLE // 61
michael@0 256 CONVOLVE_ONE_SAMPLE // 62
michael@0 257 CONVOLVE_ONE_SAMPLE // 63
michael@0 258 CONVOLVE_ONE_SAMPLE // 64
michael@0 259 CONVOLVE_ONE_SAMPLE // 65
michael@0 260 CONVOLVE_ONE_SAMPLE // 66
michael@0 261 CONVOLVE_ONE_SAMPLE // 67
michael@0 262 CONVOLVE_ONE_SAMPLE // 68
michael@0 263 CONVOLVE_ONE_SAMPLE // 69
michael@0 264 CONVOLVE_ONE_SAMPLE // 70
michael@0 265
michael@0 266 CONVOLVE_ONE_SAMPLE // 71
michael@0 267 CONVOLVE_ONE_SAMPLE // 72
michael@0 268 CONVOLVE_ONE_SAMPLE // 73
michael@0 269 CONVOLVE_ONE_SAMPLE // 74
michael@0 270 CONVOLVE_ONE_SAMPLE // 75
michael@0 271 CONVOLVE_ONE_SAMPLE // 76
michael@0 272 CONVOLVE_ONE_SAMPLE // 77
michael@0 273 CONVOLVE_ONE_SAMPLE // 78
michael@0 274 CONVOLVE_ONE_SAMPLE // 79
michael@0 275 CONVOLVE_ONE_SAMPLE // 80
michael@0 276
michael@0 277 CONVOLVE_ONE_SAMPLE // 81
michael@0 278 CONVOLVE_ONE_SAMPLE // 82
michael@0 279 CONVOLVE_ONE_SAMPLE // 83
michael@0 280 CONVOLVE_ONE_SAMPLE // 84
michael@0 281 CONVOLVE_ONE_SAMPLE // 85
michael@0 282 CONVOLVE_ONE_SAMPLE // 86
michael@0 283 CONVOLVE_ONE_SAMPLE // 87
michael@0 284 CONVOLVE_ONE_SAMPLE // 88
michael@0 285 CONVOLVE_ONE_SAMPLE // 89
michael@0 286 CONVOLVE_ONE_SAMPLE // 90
michael@0 287
michael@0 288 CONVOLVE_ONE_SAMPLE // 91
michael@0 289 CONVOLVE_ONE_SAMPLE // 92
michael@0 290 CONVOLVE_ONE_SAMPLE // 93
michael@0 291 CONVOLVE_ONE_SAMPLE // 94
michael@0 292 CONVOLVE_ONE_SAMPLE // 95
michael@0 293 CONVOLVE_ONE_SAMPLE // 96
michael@0 294 CONVOLVE_ONE_SAMPLE // 97
michael@0 295 CONVOLVE_ONE_SAMPLE // 98
michael@0 296 CONVOLVE_ONE_SAMPLE // 99
michael@0 297 CONVOLVE_ONE_SAMPLE // 100
michael@0 298
michael@0 299 CONVOLVE_ONE_SAMPLE // 101
michael@0 300 CONVOLVE_ONE_SAMPLE // 102
michael@0 301 CONVOLVE_ONE_SAMPLE // 103
michael@0 302 CONVOLVE_ONE_SAMPLE // 104
michael@0 303 CONVOLVE_ONE_SAMPLE // 105
michael@0 304 CONVOLVE_ONE_SAMPLE // 106
michael@0 305 CONVOLVE_ONE_SAMPLE // 107
michael@0 306 CONVOLVE_ONE_SAMPLE // 108
michael@0 307 CONVOLVE_ONE_SAMPLE // 109
michael@0 308 CONVOLVE_ONE_SAMPLE // 110
michael@0 309
michael@0 310 CONVOLVE_ONE_SAMPLE // 111
michael@0 311 CONVOLVE_ONE_SAMPLE // 112
michael@0 312 CONVOLVE_ONE_SAMPLE // 113
michael@0 313 CONVOLVE_ONE_SAMPLE // 114
michael@0 314 CONVOLVE_ONE_SAMPLE // 115
michael@0 315 CONVOLVE_ONE_SAMPLE // 116
michael@0 316 CONVOLVE_ONE_SAMPLE // 117
michael@0 317 CONVOLVE_ONE_SAMPLE // 118
michael@0 318 CONVOLVE_ONE_SAMPLE // 119
michael@0 319 CONVOLVE_ONE_SAMPLE // 120
michael@0 320
michael@0 321 CONVOLVE_ONE_SAMPLE // 121
michael@0 322 CONVOLVE_ONE_SAMPLE // 122
michael@0 323 CONVOLVE_ONE_SAMPLE // 123
michael@0 324 CONVOLVE_ONE_SAMPLE // 124
michael@0 325 CONVOLVE_ONE_SAMPLE // 125
michael@0 326 CONVOLVE_ONE_SAMPLE // 126
michael@0 327 CONVOLVE_ONE_SAMPLE // 127
michael@0 328 CONVOLVE_ONE_SAMPLE // 128
michael@0 329 } else {
michael@0 330 while (j < kernelSize) {
michael@0 331 // Non-optimized using actual while loop.
michael@0 332 CONVOLVE_ONE_SAMPLE
michael@0 333 }
michael@0 334 }
michael@0 335 destP[i++] = sum;
michael@0 336 }
michael@0 337
michael@0 338 // Copy 2nd half of input buffer to 1st half.
michael@0 339 memcpy(m_buffer.Elements(), inputP, sizeof(float) * framesToProcess);
michael@0 340 }
michael@0 341
michael@0 342 void DirectConvolver::reset()
michael@0 343 {
michael@0 344 PodZero(m_buffer.Elements(), m_buffer.Length());
michael@0 345 }
michael@0 346
michael@0 347 } // namespace WebCore

mercurial