kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | /* The global function contained in this file initializes SPL function |
| 12 | * pointers, currently only for ARM platforms. |
| 13 | * |
| 14 | * Some code came from common/rtcd.c in the WebM project. |
| 15 | */ |
| 16 | |
pbos@webrtc.org | aa30bb7 | 2013-05-27 09:49:58 +0000 | [diff] [blame] | 17 | #include "webrtc/common_audio/signal_processing/include/real_fft.h" |
| 18 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
| 19 | #include "webrtc/system_wrappers/interface/cpu_features_wrapper.h" |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 20 | |
| 21 | /* Declare function pointers. */ |
| 22 | MaxAbsValueW16 WebRtcSpl_MaxAbsValueW16; |
| 23 | MaxAbsValueW32 WebRtcSpl_MaxAbsValueW32; |
| 24 | MaxValueW16 WebRtcSpl_MaxValueW16; |
| 25 | MaxValueW32 WebRtcSpl_MaxValueW32; |
| 26 | MinValueW16 WebRtcSpl_MinValueW16; |
| 27 | MinValueW32 WebRtcSpl_MinValueW32; |
| 28 | CrossCorrelation WebRtcSpl_CrossCorrelation; |
| 29 | DownsampleFast WebRtcSpl_DownsampleFast; |
| 30 | ScaleAndAddVectorsWithRound WebRtcSpl_ScaleAndAddVectorsWithRound; |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 31 | CreateRealFFT WebRtcSpl_CreateRealFFT; |
| 32 | FreeRealFFT WebRtcSpl_FreeRealFFT; |
kma@webrtc.org | f9e6cc2 | 2012-09-21 18:51:12 +0000 | [diff] [blame] | 33 | RealForwardFFT WebRtcSpl_RealForwardFFT; |
| 34 | RealInverseFFT WebRtcSpl_RealInverseFFT; |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 35 | |
andrew@webrtc.org | 5140e24 | 2013-02-21 20:12:21 +0000 | [diff] [blame] | 36 | #if (defined(WEBRTC_DETECT_ARM_NEON) || !defined(WEBRTC_ARCH_ARM_NEON)) && \ |
| 37 | !defined(MIPS32_LE) |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 38 | /* Initialize function pointers to the generic C version. */ |
| 39 | static void InitPointersToC() { |
| 40 | WebRtcSpl_MaxAbsValueW16 = WebRtcSpl_MaxAbsValueW16C; |
| 41 | WebRtcSpl_MaxAbsValueW32 = WebRtcSpl_MaxAbsValueW32C; |
| 42 | WebRtcSpl_MaxValueW16 = WebRtcSpl_MaxValueW16C; |
| 43 | WebRtcSpl_MaxValueW32 = WebRtcSpl_MaxValueW32C; |
| 44 | WebRtcSpl_MinValueW16 = WebRtcSpl_MinValueW16C; |
| 45 | WebRtcSpl_MinValueW32 = WebRtcSpl_MinValueW32C; |
| 46 | WebRtcSpl_CrossCorrelation = WebRtcSpl_CrossCorrelationC; |
| 47 | WebRtcSpl_DownsampleFast = WebRtcSpl_DownsampleFastC; |
| 48 | WebRtcSpl_ScaleAndAddVectorsWithRound = |
| 49 | WebRtcSpl_ScaleAndAddVectorsWithRoundC; |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 50 | WebRtcSpl_CreateRealFFT = WebRtcSpl_CreateRealFFTC; |
| 51 | WebRtcSpl_FreeRealFFT = WebRtcSpl_FreeRealFFTC; |
kma@webrtc.org | f9e6cc2 | 2012-09-21 18:51:12 +0000 | [diff] [blame] | 52 | WebRtcSpl_RealForwardFFT = WebRtcSpl_RealForwardFFTC; |
| 53 | WebRtcSpl_RealInverseFFT = WebRtcSpl_RealInverseFFTC; |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 54 | } |
wu@webrtc.org | 7aa1189 | 2012-10-10 20:59:35 +0000 | [diff] [blame] | 55 | #endif |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 56 | |
| 57 | #if defined(WEBRTC_DETECT_ARM_NEON) || defined(WEBRTC_ARCH_ARM_NEON) |
| 58 | /* Initialize function pointers to the Neon version. */ |
| 59 | static void InitPointersToNeon() { |
| 60 | WebRtcSpl_MaxAbsValueW16 = WebRtcSpl_MaxAbsValueW16Neon; |
| 61 | WebRtcSpl_MaxAbsValueW32 = WebRtcSpl_MaxAbsValueW32Neon; |
| 62 | WebRtcSpl_MaxValueW16 = WebRtcSpl_MaxValueW16Neon; |
| 63 | WebRtcSpl_MaxValueW32 = WebRtcSpl_MaxValueW32Neon; |
| 64 | WebRtcSpl_MinValueW16 = WebRtcSpl_MinValueW16Neon; |
| 65 | WebRtcSpl_MinValueW32 = WebRtcSpl_MinValueW32Neon; |
| 66 | WebRtcSpl_CrossCorrelation = WebRtcSpl_CrossCorrelationNeon; |
| 67 | WebRtcSpl_DownsampleFast = WebRtcSpl_DownsampleFastNeon; |
| 68 | WebRtcSpl_ScaleAndAddVectorsWithRound = |
| 69 | WebRtcSpl_ScaleAndAddVectorsWithRoundNeon; |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 70 | WebRtcSpl_CreateRealFFT = WebRtcSpl_CreateRealFFTNeon; |
| 71 | WebRtcSpl_FreeRealFFT = WebRtcSpl_FreeRealFFTNeon; |
kma@webrtc.org | f9e6cc2 | 2012-09-21 18:51:12 +0000 | [diff] [blame] | 72 | WebRtcSpl_RealForwardFFT = WebRtcSpl_RealForwardFFTNeon; |
| 73 | WebRtcSpl_RealInverseFFT = WebRtcSpl_RealInverseFFTNeon; |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 74 | } |
| 75 | #endif |
| 76 | |
andrew@webrtc.org | 5140e24 | 2013-02-21 20:12:21 +0000 | [diff] [blame] | 77 | #if defined(MIPS32_LE) |
| 78 | /* Initialize function pointers to the MIPS version. */ |
| 79 | static void InitPointersToMIPS() { |
| 80 | WebRtcSpl_MaxAbsValueW16 = WebRtcSpl_MaxAbsValueW16_mips; |
| 81 | WebRtcSpl_MaxValueW16 = WebRtcSpl_MaxValueW16_mips; |
| 82 | WebRtcSpl_MaxValueW32 = WebRtcSpl_MaxValueW32_mips; |
| 83 | WebRtcSpl_MinValueW16 = WebRtcSpl_MinValueW16_mips; |
| 84 | WebRtcSpl_MinValueW32 = WebRtcSpl_MinValueW32_mips; |
| 85 | WebRtcSpl_CrossCorrelation = WebRtcSpl_CrossCorrelationC; |
andrew@webrtc.org | eed919d | 2013-05-30 16:38:36 +0000 | [diff] [blame] | 86 | WebRtcSpl_DownsampleFast = WebRtcSpl_DownsampleFast_mips; |
andrew@webrtc.org | 5140e24 | 2013-02-21 20:12:21 +0000 | [diff] [blame] | 87 | WebRtcSpl_ScaleAndAddVectorsWithRound = |
| 88 | WebRtcSpl_ScaleAndAddVectorsWithRoundC; |
kma@webrtc.org | fc8aaf0 | 2013-07-24 17:38:23 +0000 | [diff] [blame] | 89 | WebRtcSpl_CreateRealFFT = WebRtcSpl_CreateRealFFTC; |
| 90 | WebRtcSpl_FreeRealFFT = WebRtcSpl_FreeRealFFTC; |
andrew@webrtc.org | 5140e24 | 2013-02-21 20:12:21 +0000 | [diff] [blame] | 91 | WebRtcSpl_RealForwardFFT = WebRtcSpl_RealForwardFFTC; |
| 92 | WebRtcSpl_RealInverseFFT = WebRtcSpl_RealInverseFFTC; |
| 93 | #if defined(MIPS_DSP_R1_LE) |
| 94 | WebRtcSpl_MaxAbsValueW32 = WebRtcSpl_MaxAbsValueW32_mips; |
| 95 | #else |
| 96 | WebRtcSpl_MaxAbsValueW32 = WebRtcSpl_MaxAbsValueW32C; |
| 97 | #endif |
| 98 | } |
| 99 | #endif |
| 100 | |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 101 | static void InitFunctionPointers(void) { |
| 102 | #if defined(WEBRTC_DETECT_ARM_NEON) |
| 103 | if ((WebRtc_GetCPUFeaturesARM() & kCPUFeatureNEON) != 0) { |
| 104 | InitPointersToNeon(); |
| 105 | } else { |
| 106 | InitPointersToC(); |
| 107 | } |
| 108 | #elif defined(WEBRTC_ARCH_ARM_NEON) |
| 109 | InitPointersToNeon(); |
andrew@webrtc.org | 5140e24 | 2013-02-21 20:12:21 +0000 | [diff] [blame] | 110 | #elif defined(MIPS32_LE) |
| 111 | InitPointersToMIPS(); |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 112 | #else |
| 113 | InitPointersToC(); |
| 114 | #endif /* WEBRTC_DETECT_ARM_NEON */ |
| 115 | } |
| 116 | |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 117 | #if defined(WEBRTC_POSIX) |
| 118 | #include <pthread.h> |
| 119 | |
| 120 | static void once(void (*func)(void)) { |
| 121 | static pthread_once_t lock = PTHREAD_ONCE_INIT; |
| 122 | pthread_once(&lock, func); |
| 123 | } |
| 124 | |
| 125 | #elif defined(_WIN32) |
| 126 | #include <windows.h> |
| 127 | |
| 128 | static void once(void (*func)(void)) { |
| 129 | /* Didn't use InitializeCriticalSection() since there's no race-free context |
| 130 | * in which to execute it. |
| 131 | * |
| 132 | * TODO(kma): Change to different implementation (e.g. |
| 133 | * InterlockedCompareExchangePointer) to avoid issues similar to |
| 134 | * http://code.google.com/p/webm/issues/detail?id=467. |
| 135 | */ |
andrew@webrtc.org | 63e0964 | 2013-01-29 06:45:22 +0000 | [diff] [blame] | 136 | static CRITICAL_SECTION lock = {(void *)((size_t)-1), -1, 0, 0, 0, 0}; |
kma@webrtc.org | 0221b78 | 2012-09-08 00:09:26 +0000 | [diff] [blame] | 137 | static int done = 0; |
| 138 | |
| 139 | EnterCriticalSection(&lock); |
| 140 | if (!done) { |
| 141 | func(); |
| 142 | done = 1; |
| 143 | } |
| 144 | LeaveCriticalSection(&lock); |
| 145 | } |
| 146 | |
| 147 | /* There's no fallback version as an #else block here to ensure thread safety. |
| 148 | * In case of neither pthread for WEBRTC_POSIX nor _WIN32 is present, build |
| 149 | * system should pick it up. |
| 150 | */ |
| 151 | #endif /* WEBRTC_POSIX */ |
| 152 | |
| 153 | void WebRtcSpl_Init() { |
| 154 | once(InitFunctionPointers); |
| 155 | } |