| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * | 7 * |
| 8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 | 36 |
| 37 extern "C" { | 37 extern "C" { |
| 38 #include <libavcodec/avfft.h> | 38 #include <libavcodec/avfft.h> |
| 39 } | 39 } |
| 40 | 40 |
| 41 #include "wtf/MathExtras.h" | 41 #include "wtf/MathExtras.h" |
| 42 | 42 |
| 43 namespace blink { | 43 namespace blink { |
| 44 | 44 |
| 45 #if ENABLE(ASSERT) | 45 #if ENABLE(ASSERT) |
| 46 // Max FFT size for FFMPEG. WebAudio currently only uses FFTs up to size 15 (2^
15 points). | 46 // Max FFT size for FFMPEG. WebAudio currently only uses FFTs up to size 15 |
| 47 // (2^15 points). |
| 47 const int kMaxFFTPow2Size = 16; | 48 const int kMaxFFTPow2Size = 16; |
| 48 #endif | 49 #endif |
| 49 | 50 |
| 50 // Normal constructor: allocates for a given fftSize. | 51 // Normal constructor: allocates for a given fftSize. |
| 51 FFTFrame::FFTFrame(unsigned fftSize) | 52 FFTFrame::FFTFrame(unsigned fftSize) |
| 52 : m_FFTSize(fftSize), | 53 : m_FFTSize(fftSize), |
| 53 m_log2FFTSize(static_cast<unsigned>(log2(fftSize))), | 54 m_log2FFTSize(static_cast<unsigned>(log2(fftSize))), |
| 54 m_realData(fftSize / 2), | 55 m_realData(fftSize / 2), |
| 55 m_imagData(fftSize / 2), | 56 m_imagData(fftSize / 2), |
| 56 m_forwardContext(nullptr), | 57 m_forwardContext(nullptr), |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 // Compute Forward transform. | 106 // Compute Forward transform. |
| 106 av_rdft_calc(m_forwardContext, p); | 107 av_rdft_calc(m_forwardContext, p); |
| 107 | 108 |
| 108 // De-interleave to separate real and complex arrays. | 109 // De-interleave to separate real and complex arrays. |
| 109 int len = m_FFTSize / 2; | 110 int len = m_FFTSize / 2; |
| 110 | 111 |
| 111 float* real = m_realData.data(); | 112 float* real = m_realData.data(); |
| 112 float* imag = m_imagData.data(); | 113 float* imag = m_imagData.data(); |
| 113 for (int i = 0; i < len; ++i) { | 114 for (int i = 0; i < len; ++i) { |
| 114 int baseComplexIndex = 2 * i; | 115 int baseComplexIndex = 2 * i; |
| 115 // m_realData[0] is the DC component and m_imagData[0] is the nyquist compon
ent | 116 // m_realData[0] is the DC component and m_imagData[0] is the nyquist |
| 116 // since the interleaved complex data is packed. | 117 // component since the interleaved complex data is packed. |
| 117 real[i] = p[baseComplexIndex]; | 118 real[i] = p[baseComplexIndex]; |
| 118 imag[i] = p[baseComplexIndex + 1]; | 119 imag[i] = p[baseComplexIndex + 1]; |
| 119 } | 120 } |
| 120 } | 121 } |
| 121 | 122 |
| 122 void FFTFrame::doInverseFFT(float* data) { | 123 void FFTFrame::doInverseFFT(float* data) { |
| 123 // Prepare interleaved data. | 124 // Prepare interleaved data. |
| 124 float* interleavedData = getUpToDateComplexData(); | 125 float* interleavedData = getUpToDateComplexData(); |
| 125 | 126 |
| 126 // Compute inverse transform. | 127 // Compute inverse transform. |
| 127 av_rdft_calc(m_inverseContext, interleavedData); | 128 av_rdft_calc(m_inverseContext, interleavedData); |
| 128 | 129 |
| 129 // Scale so that a forward then inverse FFT yields exactly the original data.
For some reason | 130 // Scale so that a forward then inverse FFT yields exactly the original data. |
| 130 // av_rdft_calc above returns values that are half of what I expect. Hence mak
e the scale factor | 131 // For some reason av_rdft_calc above returns values that are half of what I |
| 132 // expect. Hence make the scale factor |
| 131 // twice as large to compensate for that. | 133 // twice as large to compensate for that. |
| 132 const float scale = 2.0 / m_FFTSize; | 134 const float scale = 2.0 / m_FFTSize; |
| 133 VectorMath::vsmul(interleavedData, 1, &scale, data, 1, m_FFTSize); | 135 VectorMath::vsmul(interleavedData, 1, &scale, data, 1, m_FFTSize); |
| 134 } | 136 } |
| 135 | 137 |
| 136 float* FFTFrame::getUpToDateComplexData() { | 138 float* FFTFrame::getUpToDateComplexData() { |
| 137 // FIXME: if we can't completely get rid of this method, SSE | 139 // FIXME: if we can't completely get rid of this method, SSE |
| 138 // optimization could be considered if it shows up hot on profiles. | 140 // optimization could be considered if it shows up hot on profiles. |
| 139 int len = m_FFTSize / 2; | 141 int len = m_FFTSize / 2; |
| 140 const float* real = m_realData.data(); | 142 const float* real = m_realData.data(); |
| 141 const float* imag = m_imagData.data(); | 143 const float* imag = m_imagData.data(); |
| 142 float* c = m_complexData.data(); | 144 float* c = m_complexData.data(); |
| 143 for (int i = 0; i < len; ++i) { | 145 for (int i = 0; i < len; ++i) { |
| 144 int baseComplexIndex = 2 * i; | 146 int baseComplexIndex = 2 * i; |
| 145 c[baseComplexIndex] = real[i]; | 147 c[baseComplexIndex] = real[i]; |
| 146 c[baseComplexIndex + 1] = imag[i]; | 148 c[baseComplexIndex + 1] = imag[i]; |
| 147 } | 149 } |
| 148 return const_cast<float*>(m_complexData.data()); | 150 return const_cast<float*>(m_complexData.data()); |
| 149 } | 151 } |
| 150 | 152 |
| 151 RDFTContext* FFTFrame::contextForSize(unsigned fftSize, int trans) { | 153 RDFTContext* FFTFrame::contextForSize(unsigned fftSize, int trans) { |
| 152 // FIXME: This is non-optimal. Ideally, we'd like to share the contexts for FF
TFrames of the same size. | 154 // FIXME: This is non-optimal. Ideally, we'd like to share the contexts for |
| 153 // But FFmpeg's RDFT uses a scratch buffer inside the context and so they are
not thread-safe. | 155 // FFTFrames of the same size. But FFmpeg's RDFT uses a scratch buffer |
| 154 // We could improve this by sharing the FFTFrames on a per-thread basis. | 156 // inside the context and so they are not thread-safe. We could improve this |
| 157 // by sharing the FFTFrames on a per-thread basis. |
| 155 ASSERT(fftSize); | 158 ASSERT(fftSize); |
| 156 int pow2size = static_cast<int>(log2(fftSize)); | 159 int pow2size = static_cast<int>(log2(fftSize)); |
| 157 ASSERT(pow2size < kMaxFFTPow2Size); | 160 ASSERT(pow2size < kMaxFFTPow2Size); |
| 158 | 161 |
| 159 RDFTContext* context = av_rdft_init(pow2size, (RDFTransformType)trans); | 162 RDFTContext* context = av_rdft_init(pow2size, (RDFTransformType)trans); |
| 160 return context; | 163 return context; |
| 161 } | 164 } |
| 162 | 165 |
| 163 } // namespace blink | 166 } // namespace blink |
| 164 | 167 |
| 165 #endif // USE(WEBAUDIO_FFMPEG) | 168 #endif // USE(WEBAUDIO_FFMPEG) |
| OLD | NEW |