1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "ReverbConvolver.h"
30 #include "ReverbConvolverStage.h"
31 
32 using namespace mozilla;
33 
34 namespace WebCore {
35 
36 const int InputBufferSize = 8 * 16384;
37 
38 // We only process the leading portion of the impulse response in the real-time
39 // thread.  We don't exceed this length. It turns out then, that the background
40 // thread has about 278msec of scheduling slop. Empirically, this has been found
41 // to be a good compromise between giving enough time for scheduling slop, while
42 // still minimizing the amount of processing done in the primary (high-priority)
43 // thread. This was found to be a good value on Mac OS X, and may work well on
44 // other platforms as well, assuming the very rough scheduling latencies are
45 // similar on these time-scales.  Of course, this code may need to be tuned for
46 // individual platforms if this assumption is found to be incorrect.
47 const size_t RealtimeFrameLimit = 8192 + 4096  // ~278msec @ 44.1KHz
48                                   - WEBAUDIO_BLOCK_SIZE;
49 // First stage will have size MinFFTSize - successive stages will double in
50 // size each time until we hit the maximum size.
51 const size_t MinFFTSize = 256;
52 // If we are using background threads then don't exceed this FFT size for the
53 // stages which run in the real-time thread.  This avoids having only one or
54 // two large stages (size 16384 or so) at the end which take a lot of time
55 // every several processing slices.  This way we amortize the cost over more
56 // processing slices.
57 const size_t MaxRealtimeFFTSize = 4096;
58 
ReverbConvolver(const float * impulseResponseData,size_t impulseResponseLength,size_t maxFFTSize,size_t convolverRenderPhase,bool useBackgroundThreads)59 ReverbConvolver::ReverbConvolver(const float* impulseResponseData,
60                                  size_t impulseResponseLength,
61                                  size_t maxFFTSize, size_t convolverRenderPhase,
62                                  bool useBackgroundThreads)
63     : m_impulseResponseLength(impulseResponseLength),
64       m_accumulationBuffer(impulseResponseLength + WEBAUDIO_BLOCK_SIZE),
65       m_inputBuffer(InputBufferSize),
66       m_backgroundThread("ConvolverWorker"),
67       m_backgroundThreadMonitor("ConvolverMonitor"),
68       m_useBackgroundThreads(useBackgroundThreads),
69       m_wantsToExit(false),
70       m_moreInputBuffered(false) {
71   // For the moment, a good way to know if we have real-time constraint is to
72   // check if we're using background threads. Otherwise, assume we're being run
73   // from a command-line tool.
74   bool hasRealtimeConstraint = useBackgroundThreads;
75 
76   const float* response = impulseResponseData;
77   size_t totalResponseLength = impulseResponseLength;
78 
79   // The total latency is zero because the first FFT stage is small enough
80   // to return output in the first block.
81   size_t reverbTotalLatency = 0;
82 
83   size_t stageOffset = 0;
84   size_t stagePhase = 0;
85   size_t fftSize = MinFFTSize;
86   while (stageOffset < totalResponseLength) {
87     size_t stageSize = fftSize / 2;
88 
89     // For the last stage, it's possible that stageOffset is such that we're
90     // straddling the end of the impulse response buffer (if we use stageSize),
91     // so reduce the last stage's length...
92     if (stageSize + stageOffset > totalResponseLength) {
93       stageSize = totalResponseLength - stageOffset;
94       // Use smallest FFT that is large enough to cover the last stage.
95       fftSize = MinFFTSize;
96       while (stageSize * 2 > fftSize) {
97         fftSize *= 2;
98       }
99     }
100 
101     // This "staggers" the time when each FFT happens so they don't all happen
102     // at the same time
103     int renderPhase = convolverRenderPhase + stagePhase;
104 
105     UniquePtr<ReverbConvolverStage> stage(new ReverbConvolverStage(
106         response, totalResponseLength, reverbTotalLatency, stageOffset,
107         stageSize, fftSize, renderPhase, &m_accumulationBuffer));
108 
109     bool isBackgroundStage = false;
110 
111     if (this->useBackgroundThreads() && stageOffset > RealtimeFrameLimit) {
112       m_backgroundStages.AppendElement(std::move(stage));
113       isBackgroundStage = true;
114     } else
115       m_stages.AppendElement(std::move(stage));
116 
117     // Figure out next FFT size
118     fftSize *= 2;
119 
120     stageOffset += stageSize;
121 
122     if (hasRealtimeConstraint && !isBackgroundStage &&
123         fftSize > MaxRealtimeFFTSize) {
124       fftSize = MaxRealtimeFFTSize;
125       // Custom phase positions for all but the first of the realtime
126       // stages of largest size.  These spread out the work of the
127       // larger realtime stages.  None of the FFTs of size 1024, 2048 or
128       // 4096 are performed when processing the same block.  The first
129       // MaxRealtimeFFTSize = 4096 stage, at the end of the doubling,
130       // performs its FFT at block 7.  The FFTs of size 2048 are
131       // performed in blocks 3 + 8 * n and size 1024 at 1 + 4 * n.
132       const uint32_t phaseLookup[] = {14, 0, 10, 4};
133       stagePhase = WEBAUDIO_BLOCK_SIZE *
134                    phaseLookup[m_stages.Length() % ArrayLength(phaseLookup)];
135     } else if (fftSize > maxFFTSize) {
136       fftSize = maxFFTSize;
137       // A prime offset spreads out FFTs in a way that all
138       // available phase positions will be used if there are sufficient
139       // stages.
140       stagePhase += 5 * WEBAUDIO_BLOCK_SIZE;
141     } else if (stageSize > WEBAUDIO_BLOCK_SIZE) {
142       // As the stages are doubling in size, the next FFT will occur
143       // mid-way between FFTs for this stage.
144       stagePhase = stageSize - WEBAUDIO_BLOCK_SIZE;
145     }
146   }
147 
148   // Start up background thread
149   // FIXME: would be better to up the thread priority here.  It doesn't need to
150   // be real-time, but higher than the default...
151   if (this->useBackgroundThreads() && m_backgroundStages.Length() > 0) {
152     if (!m_backgroundThread.Start()) {
153       NS_WARNING("Cannot start convolver thread.");
154       return;
155     }
156     m_backgroundThread.message_loop()->PostTask(NewNonOwningRunnableMethod(
157         "WebCore::ReverbConvolver::backgroundThreadEntry", this,
158         &ReverbConvolver::backgroundThreadEntry));
159   }
160 }
161 
~ReverbConvolver()162 ReverbConvolver::~ReverbConvolver() {
163   // Wait for background thread to stop
164   if (useBackgroundThreads() && m_backgroundThread.IsRunning()) {
165     m_wantsToExit = true;
166 
167     // Wake up thread so it can return
168     {
169       MonitorAutoLock locker(m_backgroundThreadMonitor);
170       m_moreInputBuffered = true;
171       m_backgroundThreadMonitor.Notify();
172     }
173 
174     m_backgroundThread.Stop();
175   }
176 }
177 
sizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const178 size_t ReverbConvolver::sizeOfIncludingThis(
179     mozilla::MallocSizeOf aMallocSizeOf) const {
180   size_t amount = aMallocSizeOf(this);
181   amount += m_stages.ShallowSizeOfExcludingThis(aMallocSizeOf);
182   for (size_t i = 0; i < m_stages.Length(); i++) {
183     if (m_stages[i]) {
184       amount += m_stages[i]->sizeOfIncludingThis(aMallocSizeOf);
185     }
186   }
187 
188   amount += m_backgroundStages.ShallowSizeOfExcludingThis(aMallocSizeOf);
189   for (size_t i = 0; i < m_backgroundStages.Length(); i++) {
190     if (m_backgroundStages[i]) {
191       amount += m_backgroundStages[i]->sizeOfIncludingThis(aMallocSizeOf);
192     }
193   }
194 
195   // NB: The buffer sizes are static, so even though they might be accessed
196   //     in another thread it's safe to measure them.
197   amount += m_accumulationBuffer.sizeOfExcludingThis(aMallocSizeOf);
198   amount += m_inputBuffer.sizeOfExcludingThis(aMallocSizeOf);
199 
200   // Possible future measurements:
201   // - m_backgroundThread
202   // - m_backgroundThreadMonitor
203   return amount;
204 }
205 
backgroundThreadEntry()206 void ReverbConvolver::backgroundThreadEntry() {
207   while (!m_wantsToExit) {
208     // Wait for realtime thread to give us more input
209     m_moreInputBuffered = false;
210     {
211       MonitorAutoLock locker(m_backgroundThreadMonitor);
212       while (!m_moreInputBuffered && !m_wantsToExit)
213         m_backgroundThreadMonitor.Wait();
214     }
215 
216     // Process all of the stages until their read indices reach the input
217     // buffer's write index
218     int writeIndex = m_inputBuffer.writeIndex();
219 
220     // Even though it doesn't seem like every stage needs to maintain its own
221     // version of readIndex we do this in case we want to run in more than one
222     // background thread.
223     int readIndex;
224 
225     while ((readIndex = m_backgroundStages[0]->inputReadIndex()) !=
226            writeIndex) {  // FIXME: do better to detect buffer overrun...
227       // Accumulate contributions from each stage
228       for (size_t i = 0; i < m_backgroundStages.Length(); ++i)
229         m_backgroundStages[i]->processInBackground(this);
230     }
231   }
232 }
233 
process(const float * sourceChannelData,float * destinationChannelData)234 void ReverbConvolver::process(const float* sourceChannelData,
235                               float* destinationChannelData) {
236   const float* source = sourceChannelData;
237   float* destination = destinationChannelData;
238   bool isDataSafe = source && destination;
239   MOZ_ASSERT(isDataSafe);
240   if (!isDataSafe) return;
241 
242   // Feed input buffer (read by all threads)
243   m_inputBuffer.write(source, WEBAUDIO_BLOCK_SIZE);
244 
245   // Accumulate contributions from each stage
246   for (size_t i = 0; i < m_stages.Length(); ++i) m_stages[i]->process(source);
247 
248   // Finally read from accumulation buffer
249   m_accumulationBuffer.readAndClear(destination, WEBAUDIO_BLOCK_SIZE);
250 
251   // Now that we've buffered more input, wake up our background thread.
252 
253   // Not using a MonitorAutoLock looks strange, but we use a TryLock() instead
254   // because this is run on the real-time thread where it is a disaster for the
255   // lock to be contended (causes audio glitching).  It's OK if we fail to
256   // signal from time to time, since we'll get to it the next time we're called.
257   // We're called repeatedly and frequently (around every 3ms).  The background
258   // thread is processing well into the future and has a considerable amount of
259   // leeway here...
260   if (m_backgroundThreadMonitor.TryLock()) {
261     m_moreInputBuffered = true;
262     m_backgroundThreadMonitor.Notify();
263     m_backgroundThreadMonitor.Unlock();
264   }
265 }
266 
267 }  // namespace WebCore
268