The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define LOG_TAG "ResampleInputStream" |
| 19 | #include "utils/Log.h" |
| 20 | |
| 21 | #include <media/mediarecorder.h> |
| 22 | #include <stdio.h> |
| 23 | #include <assert.h> |
| 24 | #include <limits.h> |
| 25 | #include <unistd.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <utils/threads.h> |
| 28 | |
| 29 | #include "jni.h" |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 30 | #include <nativehelper/JNIHelp.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | #include "android_runtime/AndroidRuntime.h" |
| 32 | |
| 33 | |
| 34 | // ---------------------------------------------------------------------------- |
| 35 | |
| 36 | using namespace android; |
| 37 | |
| 38 | |
Chih-Hung Hsieh | 0ca16ef | 2016-05-19 15:14:54 -0700 | [diff] [blame] | 39 | #define FIR_COEF(coef) (short)(0x10000 * (coef)) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | static const short fir21[] = { |
| 41 | FIR_COEF(-0.006965742326), |
| 42 | FIR_COEF(-0.008428945737), |
| 43 | FIR_COEF( 0.004241280174), |
| 44 | FIR_COEF( 0.022141096893), |
| 45 | FIR_COEF( 0.018765669437), |
| 46 | FIR_COEF(-0.009871891152), |
| 47 | FIR_COEF(-0.024842433247), |
| 48 | FIR_COEF( 0.006121772058), |
| 49 | FIR_COEF( 0.045890841611), |
| 50 | FIR_COEF( 0.021573503509), |
| 51 | FIR_COEF(-0.059681984668), |
| 52 | FIR_COEF(-0.076036275138), |
| 53 | FIR_COEF( 0.072405390275), |
| 54 | FIR_COEF( 0.308255674582), |
| 55 | FIR_COEF( 0.424321210495), |
| 56 | FIR_COEF( 0.308255674582), |
| 57 | FIR_COEF( 0.072405390275), |
| 58 | FIR_COEF(-0.076036275138), |
| 59 | FIR_COEF(-0.059681984668), |
| 60 | FIR_COEF( 0.021573503509), |
| 61 | FIR_COEF( 0.045890841611), |
| 62 | FIR_COEF( 0.006121772058), |
| 63 | FIR_COEF(-0.024842433247), |
| 64 | FIR_COEF(-0.009871891152), |
| 65 | FIR_COEF( 0.018765669437), |
| 66 | FIR_COEF( 0.022141096893), |
| 67 | FIR_COEF( 0.004241280174), |
| 68 | FIR_COEF(-0.008428945737), |
| 69 | FIR_COEF(-0.006965742326) |
| 70 | }; |
| 71 | static const int nFir21 = sizeof(fir21) / sizeof(fir21[0]); |
| 72 | |
| 73 | static const int BUF_SIZE = 2048; |
| 74 | |
| 75 | |
Andreas Gampe | 5a15d0d | 2014-11-10 18:19:40 -0800 | [diff] [blame] | 76 | static void android_media_ResampleInputStream_fir21(JNIEnv *env, jclass /* clazz */, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | jbyteArray jIn, jint jInOffset, |
| 78 | jbyteArray jOut, jint jOutOffset, |
| 79 | jint jNpoints) { |
Elliott Hughes | 15dd15f | 2011-04-08 17:42:34 -0700 | [diff] [blame] | 80 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | // safety first! |
Gloria Wang | 196863d | 2010-02-22 15:56:21 -0800 | [diff] [blame] | 82 | if (nFir21 + jNpoints * 2 > BUF_SIZE) { |
Elliott Hughes | 15dd15f | 2011-04-08 17:42:34 -0700 | [diff] [blame] | 83 | jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | "FIR+data too long %d", nFir21 + jNpoints); |
| 85 | return; |
| 86 | } |
Elliott Hughes | 15dd15f | 2011-04-08 17:42:34 -0700 | [diff] [blame] | 87 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | // get input data |
| 89 | short in[BUF_SIZE]; |
| 90 | env->GetByteArrayRegion(jIn, jInOffset, (jNpoints * 2 + nFir21 - 1) * 2, (jbyte*)in); |
Elliott Hughes | 15dd15f | 2011-04-08 17:42:34 -0700 | [diff] [blame] | 91 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | // compute filter |
| 93 | short out[BUF_SIZE]; |
| 94 | for (int i = 0; i < jNpoints; i++) { |
| 95 | long sum = 0; |
| 96 | const short* firp = &fir21[0]; |
| 97 | const short* inp = &in[i * 2]; |
| 98 | for (int n = nFir21; --n >= 0; ) { |
| 99 | sum += ((long)*firp++) * ((long)*inp++); |
| 100 | } |
| 101 | out[i] = (short)(sum >> 16); |
| 102 | } |
| 103 | |
| 104 | // save new values |
| 105 | env->SetByteArrayRegion(jOut, jOutOffset, jNpoints * 2, (jbyte*)out); |
| 106 | } |
| 107 | |
| 108 | // ---------------------------------------------------------------------------- |
| 109 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 110 | static const JNINativeMethod gMethods[] = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | {"fir21", "([BI[BII)V", (void*)android_media_ResampleInputStream_fir21}, |
| 112 | }; |
| 113 | |
| 114 | |
| 115 | int register_android_media_ResampleInputStream(JNIEnv *env) |
| 116 | { |
| 117 | const char* const kClassPathName = "android/media/ResampleInputStream"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | |
| 119 | return AndroidRuntime::registerNativeMethods(env, |
| 120 | kClassPathName, gMethods, NELEM(gMethods)); |
| 121 | } |