Add ASSERT_WITH_SECURITY_IMPLICATION to detect out of bounds access
[WebKit-https.git] / Source / WebCore / platform / audio / HRTFDatabase.cpp
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 "config.h"
30
31 #if ENABLE(WEB_AUDIO)
32
33 #include "HRTFDatabase.h"
34
35 #include "HRTFElevation.h"
36 #include "PlatformMemoryInstrumentation.h"
37 #include <wtf/MemoryInstrumentationVector.h>
38
39 using namespace std;
40
41 namespace WebCore {
42
43 const int HRTFDatabase::MinElevation = -45;
44 const int HRTFDatabase::MaxElevation = 90;
45 const unsigned HRTFDatabase::RawElevationAngleSpacing = 15;
46 const unsigned HRTFDatabase::NumberOfRawElevations = 10; // -45 -> +90 (each 15 degrees)
47 const unsigned HRTFDatabase::InterpolationFactor = 1;
48 const unsigned HRTFDatabase::NumberOfTotalElevations = NumberOfRawElevations * InterpolationFactor;
49
50 PassOwnPtr<HRTFDatabase> HRTFDatabase::create(float sampleRate)
51 {
52     OwnPtr<HRTFDatabase> hrtfDatabase = adoptPtr(new HRTFDatabase(sampleRate));
53     return hrtfDatabase.release();
54 }
55
56 HRTFDatabase::HRTFDatabase(float sampleRate)
57     : m_elevations(NumberOfTotalElevations)
58     , m_sampleRate(sampleRate)
59 {
60     unsigned elevationIndex = 0;
61     for (int elevation = MinElevation; elevation <= MaxElevation; elevation += RawElevationAngleSpacing) {
62         OwnPtr<HRTFElevation> hrtfElevation = HRTFElevation::createForSubject("Composite", elevation, sampleRate);
63         ASSERT(hrtfElevation.get());
64         if (!hrtfElevation.get())
65             return;
66         
67         m_elevations[elevationIndex] = hrtfElevation.release();
68         elevationIndex += InterpolationFactor;
69     }
70
71     // Now, go back and interpolate elevations.
72     if (InterpolationFactor > 1) {
73         for (unsigned i = 0; i < NumberOfTotalElevations; i += InterpolationFactor) {
74             unsigned j = (i + InterpolationFactor);
75             if (j >= NumberOfTotalElevations)
76                 j = i; // for last elevation interpolate with itself
77
78             // Create the interpolated convolution kernels and delays.
79             for (unsigned jj = 1; jj < InterpolationFactor; ++jj) {
80                 float x = static_cast<float>(jj) / static_cast<float>(InterpolationFactor);
81                 m_elevations[i + jj] = HRTFElevation::createByInterpolatingSlices(m_elevations[i].get(), m_elevations[j].get(), x, sampleRate);
82                 ASSERT(m_elevations[i + jj].get());
83             }
84         }
85     }
86 }
87
88 void HRTFDatabase::getKernelsFromAzimuthElevation(double azimuthBlend, unsigned azimuthIndex, double elevationAngle, HRTFKernel* &kernelL, HRTFKernel* &kernelR,
89                                                   double& frameDelayL, double& frameDelayR)
90 {
91     unsigned elevationIndex = indexFromElevationAngle(elevationAngle);
92     ASSERT_WITH_SECURITY_IMPLICATION(elevationIndex < m_elevations.size() && m_elevations.size() > 0);
93     
94     if (!m_elevations.size()) {
95         kernelL = 0;
96         kernelR = 0;
97         return;
98     }
99     
100     if (elevationIndex > m_elevations.size() - 1)
101         elevationIndex = m_elevations.size() - 1;    
102     
103     HRTFElevation* hrtfElevation = m_elevations[elevationIndex].get();
104     ASSERT(hrtfElevation);
105     if (!hrtfElevation) {
106         kernelL = 0;
107         kernelR = 0;
108         return;
109     }
110     
111     hrtfElevation->getKernelsFromAzimuth(azimuthBlend, azimuthIndex, kernelL, kernelR, frameDelayL, frameDelayR);
112 }                                                     
113
114 unsigned HRTFDatabase::indexFromElevationAngle(double elevationAngle)
115 {
116     // Clamp to allowed range.
117     elevationAngle = max(static_cast<double>(MinElevation), elevationAngle);
118     elevationAngle = min(static_cast<double>(MaxElevation), elevationAngle);
119
120     unsigned elevationIndex = static_cast<int>(InterpolationFactor * (elevationAngle - MinElevation) / RawElevationAngleSpacing);    
121     return elevationIndex;
122 }
123
124 void HRTFDatabase::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
125 {
126     MemoryClassInfo info(memoryObjectInfo, this, PlatformMemoryTypes::AudioSharedData);
127     info.addMember(m_elevations, "elevations");
128 }
129
130 } // namespace WebCore
131
132 #endif // ENABLE(WEB_AUDIO)