Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2010 Google 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 "ReverbConvolver.h" |
michael@0 | 30 | #include "ReverbConvolverStage.h" |
michael@0 | 31 | |
michael@0 | 32 | using namespace mozilla; |
michael@0 | 33 | |
michael@0 | 34 | template<> |
michael@0 | 35 | struct RunnableMethodTraits<WebCore::ReverbConvolver> |
michael@0 | 36 | { |
michael@0 | 37 | static void RetainCallee(WebCore::ReverbConvolver* obj) {} |
michael@0 | 38 | static void ReleaseCallee(WebCore::ReverbConvolver* obj) {} |
michael@0 | 39 | }; |
michael@0 | 40 | |
michael@0 | 41 | namespace WebCore { |
michael@0 | 42 | |
michael@0 | 43 | const int InputBufferSize = 8 * 16384; |
michael@0 | 44 | |
michael@0 | 45 | // We only process the leading portion of the impulse response in the real-time thread. We don't exceed this length. |
michael@0 | 46 | // It turns out then, that the background thread has about 278msec of scheduling slop. |
michael@0 | 47 | // Empirically, this has been found to be a good compromise between giving enough time for scheduling slop, |
michael@0 | 48 | // while still minimizing the amount of processing done in the primary (high-priority) thread. |
michael@0 | 49 | // This was found to be a good value on Mac OS X, and may work well on other platforms as well, assuming |
michael@0 | 50 | // the very rough scheduling latencies are similar on these time-scales. Of course, this code may need to be |
michael@0 | 51 | // tuned for individual platforms if this assumption is found to be incorrect. |
michael@0 | 52 | const size_t RealtimeFrameLimit = 8192 + 4096; // ~278msec @ 44.1KHz |
michael@0 | 53 | |
michael@0 | 54 | const size_t MinFFTSize = 128; |
michael@0 | 55 | const size_t MaxRealtimeFFTSize = 2048; |
michael@0 | 56 | |
michael@0 | 57 | ReverbConvolver::ReverbConvolver(const float* impulseResponseData, size_t impulseResponseLength, size_t renderSliceSize, size_t maxFFTSize, size_t convolverRenderPhase, bool useBackgroundThreads) |
michael@0 | 58 | : m_impulseResponseLength(impulseResponseLength) |
michael@0 | 59 | , m_accumulationBuffer(impulseResponseLength + renderSliceSize) |
michael@0 | 60 | , m_inputBuffer(InputBufferSize) |
michael@0 | 61 | , m_minFFTSize(MinFFTSize) // First stage will have this size - successive stages will double in size each time |
michael@0 | 62 | , m_maxFFTSize(maxFFTSize) // until we hit m_maxFFTSize |
michael@0 | 63 | , m_backgroundThread("ConvolverWorker") |
michael@0 | 64 | , m_backgroundThreadCondition(&m_backgroundThreadLock) |
michael@0 | 65 | , m_useBackgroundThreads(useBackgroundThreads) |
michael@0 | 66 | , m_wantsToExit(false) |
michael@0 | 67 | , m_moreInputBuffered(false) |
michael@0 | 68 | { |
michael@0 | 69 | // If we are using background threads then don't exceed this FFT size for the |
michael@0 | 70 | // stages which run in the real-time thread. This avoids having only one or two |
michael@0 | 71 | // large stages (size 16384 or so) at the end which take a lot of time every several |
michael@0 | 72 | // processing slices. This way we amortize the cost over more processing slices. |
michael@0 | 73 | m_maxRealtimeFFTSize = MaxRealtimeFFTSize; |
michael@0 | 74 | |
michael@0 | 75 | // For the moment, a good way to know if we have real-time constraint is to check if we're using background threads. |
michael@0 | 76 | // Otherwise, assume we're being run from a command-line tool. |
michael@0 | 77 | bool hasRealtimeConstraint = useBackgroundThreads; |
michael@0 | 78 | |
michael@0 | 79 | const float* response = impulseResponseData; |
michael@0 | 80 | size_t totalResponseLength = impulseResponseLength; |
michael@0 | 81 | |
michael@0 | 82 | // The total latency is zero because the direct-convolution is used in the leading portion. |
michael@0 | 83 | size_t reverbTotalLatency = 0; |
michael@0 | 84 | |
michael@0 | 85 | size_t stageOffset = 0; |
michael@0 | 86 | int i = 0; |
michael@0 | 87 | size_t fftSize = m_minFFTSize; |
michael@0 | 88 | while (stageOffset < totalResponseLength) { |
michael@0 | 89 | size_t stageSize = fftSize / 2; |
michael@0 | 90 | |
michael@0 | 91 | // For the last stage, it's possible that stageOffset is such that we're straddling the end |
michael@0 | 92 | // of the impulse response buffer (if we use stageSize), so reduce the last stage's length... |
michael@0 | 93 | if (stageSize + stageOffset > totalResponseLength) |
michael@0 | 94 | stageSize = totalResponseLength - stageOffset; |
michael@0 | 95 | |
michael@0 | 96 | // This "staggers" the time when each FFT happens so they don't all happen at the same time |
michael@0 | 97 | int renderPhase = convolverRenderPhase + i * renderSliceSize; |
michael@0 | 98 | |
michael@0 | 99 | bool useDirectConvolver = !stageOffset; |
michael@0 | 100 | |
michael@0 | 101 | nsAutoPtr<ReverbConvolverStage> stage(new ReverbConvolverStage(response, totalResponseLength, reverbTotalLatency, stageOffset, stageSize, fftSize, renderPhase, renderSliceSize, &m_accumulationBuffer, useDirectConvolver)); |
michael@0 | 102 | |
michael@0 | 103 | bool isBackgroundStage = false; |
michael@0 | 104 | |
michael@0 | 105 | if (this->useBackgroundThreads() && stageOffset > RealtimeFrameLimit) { |
michael@0 | 106 | m_backgroundStages.AppendElement(stage.forget()); |
michael@0 | 107 | isBackgroundStage = true; |
michael@0 | 108 | } else |
michael@0 | 109 | m_stages.AppendElement(stage.forget()); |
michael@0 | 110 | |
michael@0 | 111 | stageOffset += stageSize; |
michael@0 | 112 | ++i; |
michael@0 | 113 | |
michael@0 | 114 | if (!useDirectConvolver) { |
michael@0 | 115 | // Figure out next FFT size |
michael@0 | 116 | fftSize *= 2; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | if (hasRealtimeConstraint && !isBackgroundStage && fftSize > m_maxRealtimeFFTSize) |
michael@0 | 120 | fftSize = m_maxRealtimeFFTSize; |
michael@0 | 121 | if (fftSize > m_maxFFTSize) |
michael@0 | 122 | fftSize = m_maxFFTSize; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | // Start up background thread |
michael@0 | 126 | // FIXME: would be better to up the thread priority here. It doesn't need to be real-time, but higher than the default... |
michael@0 | 127 | if (this->useBackgroundThreads() && m_backgroundStages.Length() > 0) { |
michael@0 | 128 | if (!m_backgroundThread.Start()) { |
michael@0 | 129 | NS_WARNING("Cannot start convolver thread."); |
michael@0 | 130 | return; |
michael@0 | 131 | } |
michael@0 | 132 | CancelableTask* task = NewRunnableMethod(this, &ReverbConvolver::backgroundThreadEntry); |
michael@0 | 133 | m_backgroundThread.message_loop()->PostTask(FROM_HERE, task); |
michael@0 | 134 | } |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | ReverbConvolver::~ReverbConvolver() |
michael@0 | 138 | { |
michael@0 | 139 | // Wait for background thread to stop |
michael@0 | 140 | if (useBackgroundThreads() && m_backgroundThread.IsRunning()) { |
michael@0 | 141 | m_wantsToExit = true; |
michael@0 | 142 | |
michael@0 | 143 | // Wake up thread so it can return |
michael@0 | 144 | { |
michael@0 | 145 | AutoLock locker(m_backgroundThreadLock); |
michael@0 | 146 | m_moreInputBuffered = true; |
michael@0 | 147 | m_backgroundThreadCondition.Signal(); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | m_backgroundThread.Stop(); |
michael@0 | 151 | } |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | size_t ReverbConvolver::sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const |
michael@0 | 155 | { |
michael@0 | 156 | size_t amount = aMallocSizeOf(this); |
michael@0 | 157 | amount += m_stages.SizeOfExcludingThis(aMallocSizeOf); |
michael@0 | 158 | for (size_t i = 0; i < m_stages.Length(); i++) { |
michael@0 | 159 | if (m_stages[i]) { |
michael@0 | 160 | amount += m_stages[i]->sizeOfIncludingThis(aMallocSizeOf); |
michael@0 | 161 | } |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | amount += m_backgroundStages.SizeOfExcludingThis(aMallocSizeOf); |
michael@0 | 165 | for (size_t i = 0; i < m_backgroundStages.Length(); i++) { |
michael@0 | 166 | if (m_backgroundStages[i]) { |
michael@0 | 167 | amount += m_backgroundStages[i]->sizeOfIncludingThis(aMallocSizeOf); |
michael@0 | 168 | } |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | // NB: The buffer sizes are static, so even though they might be accessed |
michael@0 | 172 | // in another thread it's safe to measure them. |
michael@0 | 173 | amount += m_accumulationBuffer.sizeOfExcludingThis(aMallocSizeOf); |
michael@0 | 174 | amount += m_inputBuffer.sizeOfExcludingThis(aMallocSizeOf); |
michael@0 | 175 | |
michael@0 | 176 | // Possible future measurements: |
michael@0 | 177 | // - m_backgroundThread |
michael@0 | 178 | // - m_backgroundThreadLock |
michael@0 | 179 | // - m_backgroundThreadCondition |
michael@0 | 180 | return amount; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | void ReverbConvolver::backgroundThreadEntry() |
michael@0 | 184 | { |
michael@0 | 185 | while (!m_wantsToExit) { |
michael@0 | 186 | // Wait for realtime thread to give us more input |
michael@0 | 187 | m_moreInputBuffered = false; |
michael@0 | 188 | { |
michael@0 | 189 | AutoLock locker(m_backgroundThreadLock); |
michael@0 | 190 | while (!m_moreInputBuffered && !m_wantsToExit) |
michael@0 | 191 | m_backgroundThreadCondition.Wait(); |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | // Process all of the stages until their read indices reach the input buffer's write index |
michael@0 | 195 | int writeIndex = m_inputBuffer.writeIndex(); |
michael@0 | 196 | |
michael@0 | 197 | // Even though it doesn't seem like every stage needs to maintain its own version of readIndex |
michael@0 | 198 | // we do this in case we want to run in more than one background thread. |
michael@0 | 199 | int readIndex; |
michael@0 | 200 | |
michael@0 | 201 | while ((readIndex = m_backgroundStages[0]->inputReadIndex()) != writeIndex) { // FIXME: do better to detect buffer overrun... |
michael@0 | 202 | // The ReverbConvolverStages need to process in amounts which evenly divide half the FFT size |
michael@0 | 203 | const int SliceSize = MinFFTSize / 2; |
michael@0 | 204 | |
michael@0 | 205 | // Accumulate contributions from each stage |
michael@0 | 206 | for (size_t i = 0; i < m_backgroundStages.Length(); ++i) |
michael@0 | 207 | m_backgroundStages[i]->processInBackground(this, SliceSize); |
michael@0 | 208 | } |
michael@0 | 209 | } |
michael@0 | 210 | } |
michael@0 | 211 | |
michael@0 | 212 | void ReverbConvolver::process(const float* sourceChannelData, size_t sourceChannelLength, |
michael@0 | 213 | float* destinationChannelData, size_t destinationChannelLength, |
michael@0 | 214 | size_t framesToProcess) |
michael@0 | 215 | { |
michael@0 | 216 | bool isSafe = sourceChannelData && destinationChannelData && sourceChannelLength >= framesToProcess && destinationChannelLength >= framesToProcess; |
michael@0 | 217 | MOZ_ASSERT(isSafe); |
michael@0 | 218 | if (!isSafe) |
michael@0 | 219 | return; |
michael@0 | 220 | |
michael@0 | 221 | const float* source = sourceChannelData; |
michael@0 | 222 | float* destination = destinationChannelData; |
michael@0 | 223 | bool isDataSafe = source && destination; |
michael@0 | 224 | MOZ_ASSERT(isDataSafe); |
michael@0 | 225 | if (!isDataSafe) |
michael@0 | 226 | return; |
michael@0 | 227 | |
michael@0 | 228 | // Feed input buffer (read by all threads) |
michael@0 | 229 | m_inputBuffer.write(source, framesToProcess); |
michael@0 | 230 | |
michael@0 | 231 | // Accumulate contributions from each stage |
michael@0 | 232 | for (size_t i = 0; i < m_stages.Length(); ++i) |
michael@0 | 233 | m_stages[i]->process(source, framesToProcess); |
michael@0 | 234 | |
michael@0 | 235 | // Finally read from accumulation buffer |
michael@0 | 236 | m_accumulationBuffer.readAndClear(destination, framesToProcess); |
michael@0 | 237 | |
michael@0 | 238 | // Now that we've buffered more input, wake up our background thread. |
michael@0 | 239 | |
michael@0 | 240 | // Not using a MutexLocker looks strange, but we use a tryLock() instead because this is run on the real-time |
michael@0 | 241 | // thread where it is a disaster for the lock to be contended (causes audio glitching). It's OK if we fail to |
michael@0 | 242 | // signal from time to time, since we'll get to it the next time we're called. We're called repeatedly |
michael@0 | 243 | // and frequently (around every 3ms). The background thread is processing well into the future and has a considerable amount of |
michael@0 | 244 | // leeway here... |
michael@0 | 245 | if (m_backgroundThreadLock.Try()) { |
michael@0 | 246 | m_moreInputBuffered = true; |
michael@0 | 247 | m_backgroundThreadCondition.Signal(); |
michael@0 | 248 | m_backgroundThreadLock.Release(); |
michael@0 | 249 | } |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | void ReverbConvolver::reset() |
michael@0 | 253 | { |
michael@0 | 254 | for (size_t i = 0; i < m_stages.Length(); ++i) |
michael@0 | 255 | m_stages[i]->reset(); |
michael@0 | 256 | |
michael@0 | 257 | for (size_t i = 0; i < m_backgroundStages.Length(); ++i) |
michael@0 | 258 | m_backgroundStages[i]->reset(); |
michael@0 | 259 | |
michael@0 | 260 | m_accumulationBuffer.reset(); |
michael@0 | 261 | m_inputBuffer.reset(); |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | size_t ReverbConvolver::latencyFrames() const |
michael@0 | 265 | { |
michael@0 | 266 | return 0; |
michael@0 | 267 | } |
michael@0 | 268 | |
michael@0 | 269 | } // namespace WebCore |