1 /*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <assert.h>
18 #include <filesystem>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23
24 #include <utils/misc.h>
25 #include <utils/String8.h>
26 #include <utils/Log.h>
27
28 #include <android/bitmap.h>
29
30 #include "jni.h"
31 #include <nativehelper/JNIHelp.h>
32
33 using namespace android;
34
35 extern "C"
36 {
37 #include <fd_emb_sdk.h>
38 }
39
40 struct FaceData
41 {
42 float confidence;
43 float midpointx;
44 float midpointy;
45 float eyedist;
46 };
47
48 struct FaceOffsets
49 {
50 jfieldID confidence;
51 jfieldID midpointx;
52 jfieldID midpointy;
53 jfieldID eyedist;
54 jfieldID eulerx;
55 jfieldID eulery;
56 jfieldID eulerz;
57 } gFaceOffsets;
58
59 struct FaceDetectorOffsets
60 {
61 jfieldID fd;
62 jfieldID sdk;
63 jfieldID dcr;
64 jfieldID width;
65 jfieldID height;
66 jfieldID maxFaces;
67 jfieldID bwbuffer;
68 } gFaceDetectorOffsets;
69
70 // ---------------------------------------------------------------------------
71
getFaceData(btk_HDCR hdcr,FaceData * fdata)72 static void getFaceData(btk_HDCR hdcr, FaceData* fdata)
73 {
74 btk_Node leftEye, rightEye;
75
76 btk_DCR_getNode(hdcr, 0, &leftEye);
77 btk_DCR_getNode(hdcr, 1, &rightEye);
78
79 fdata->eyedist = (float)(rightEye.x - leftEye.x) / (1 << 16);
80 fdata->midpointx = (float)(rightEye.x + leftEye.x) / (1 << 17);
81 fdata->midpointy = (float)(rightEye.y + leftEye.y) / (1 << 17);
82 fdata->confidence = (float)btk_DCR_confidence(hdcr) / (1 << 24);
83 }
84
85 // ---------------------------------------------------------------------------
86
doThrow(JNIEnv * env,const char * exc,const char * msg=NULL)87 static void doThrow(JNIEnv* env, const char* exc, const char* msg = NULL)
88 {
89 jclass npeClazz = env->FindClass(exc);
90 env->ThrowNew(npeClazz, msg);
91 }
92
93 static void
nativeClassInit(JNIEnv * _env,jclass _this)94 nativeClassInit
95 (JNIEnv *_env, jclass _this)
96 {
97 gFaceDetectorOffsets.fd = _env->GetFieldID(_this, "mFD", "J");
98 gFaceDetectorOffsets.sdk = _env->GetFieldID(_this, "mSDK", "J");
99 gFaceDetectorOffsets.dcr = _env->GetFieldID(_this, "mDCR", "J");
100 gFaceDetectorOffsets.width = _env->GetFieldID(_this, "mWidth", "I");
101 gFaceDetectorOffsets.height = _env->GetFieldID(_this, "mHeight", "I");
102 gFaceDetectorOffsets.maxFaces = _env->GetFieldID(_this, "mMaxFaces", "I");
103 gFaceDetectorOffsets.bwbuffer = _env->GetFieldID(_this, "mBWBuffer", "[B");
104
105 jclass faceClass = _env->FindClass("android/media/FaceDetector$Face");
106 gFaceOffsets.confidence = _env->GetFieldID(faceClass, "mConfidence", "F");
107 gFaceOffsets.midpointx = _env->GetFieldID(faceClass, "mMidPointX", "F");
108 gFaceOffsets.midpointy = _env->GetFieldID(faceClass, "mMidPointY", "F");
109 gFaceOffsets.eyedist = _env->GetFieldID(faceClass, "mEyesDist", "F");
110 gFaceOffsets.eulerx = _env->GetFieldID(faceClass, "mPoseEulerX", "F");
111 gFaceOffsets.eulery = _env->GetFieldID(faceClass, "mPoseEulerY", "F");
112 gFaceOffsets.eulerz = _env->GetFieldID(faceClass, "mPoseEulerZ", "F");
113 }
114
115 // ---------------------------------------------------------------------------
116
117 static jint
initialize(JNIEnv * _env,jobject _this,jint w,jint h,jint maxFaces)118 initialize(JNIEnv *_env, jobject _this,
119 jint w, jint h, jint maxFaces)
120 {
121 // load the configuration file
122 const char* root = getenv("ANDROID_ROOT");
123 std::filesystem::path path(root);
124 path /= "usr/share/bmd/RFFstd_501.bmd";
125 // path /= "usr/share/bmd/RFFspeed_501.bmd";
126
127 const int MAX_FILE_SIZE = 65536;
128 void* initData = malloc( MAX_FILE_SIZE ); /* enough to fit entire file */
129 int filedesc = open(path.c_str(), O_RDONLY);
130 int initDataSize = read(filedesc, initData, MAX_FILE_SIZE);
131 close(filedesc);
132
133 // --------------------------------------------------------------------
134 btk_HSDK sdk = NULL;
135 btk_SDKCreateParam sdkParam = btk_SDK_defaultParam();
136 sdkParam.fpMalloc = malloc;
137 sdkParam.fpFree = free;
138 sdkParam.maxImageWidth = w;
139 sdkParam.maxImageHeight = h;
140
141 btk_Status status = btk_SDK_create(&sdkParam, &sdk);
142 // make sure everything went well
143 if (status != btk_STATUS_OK) {
144 // XXX: be more precise about what went wrong
145 doThrow(_env, "java/lang/OutOfMemoryError", NULL);
146 return 0;
147 }
148
149 btk_HDCR dcr = NULL;
150 btk_DCRCreateParam dcrParam = btk_DCR_defaultParam();
151 btk_DCR_create( sdk, &dcrParam, &dcr );
152
153 btk_HFaceFinder fd = NULL;
154 btk_FaceFinderCreateParam fdParam = btk_FaceFinder_defaultParam();
155 fdParam.pModuleParam = initData;
156 fdParam.moduleParamSize = initDataSize;
157 fdParam.maxDetectableFaces = maxFaces;
158 status = btk_FaceFinder_create( sdk, &fdParam, &fd );
159 btk_FaceFinder_setRange(fd, 20, w/2); /* set eye distance range */
160
161 // make sure everything went well
162 if (status != btk_STATUS_OK) {
163 // XXX: be more precise about what went wrong
164 doThrow(_env, "java/lang/OutOfMemoryError", NULL);
165 return 0;
166 }
167
168 // free the configuration file
169 free(initData);
170
171 // initialize the java object
172 _env->SetLongField(_this, gFaceDetectorOffsets.fd, (jlong)fd);
173 _env->SetLongField(_this, gFaceDetectorOffsets.sdk, (jlong)sdk);
174 _env->SetLongField(_this, gFaceDetectorOffsets.dcr, (jlong)dcr);
175
176 return 1;
177 }
178
179 static void
destroy(JNIEnv * _env,jobject _this)180 destroy(JNIEnv *_env, jobject _this)
181 {
182 btk_HFaceFinder hfd =
183 (btk_HFaceFinder)(_env->GetLongField(_this, gFaceDetectorOffsets.fd));
184 btk_FaceFinder_close( hfd );
185
186 btk_HDCR hdcr = (btk_HDCR)(_env->GetLongField(_this, gFaceDetectorOffsets.dcr));
187 btk_DCR_close( hdcr );
188
189 btk_HSDK hsdk = (btk_HSDK)(_env->GetLongField(_this, gFaceDetectorOffsets.sdk));
190 btk_SDK_close( hsdk );
191 }
192
193 static jint
detect(JNIEnv * _env,jobject _this,jobject bitmap)194 detect(JNIEnv *_env, jobject _this,
195 jobject bitmap)
196 {
197 // get the fields we need
198 btk_HDCR hdcr = (btk_HDCR)(_env->GetLongField(_this, gFaceDetectorOffsets.dcr));
199 btk_HFaceFinder hfd =
200 (btk_HFaceFinder)(_env->GetLongField(_this, gFaceDetectorOffsets.fd));
201 u32 width = _env->GetIntField(_this, gFaceDetectorOffsets.width);
202 u32 height = _env->GetIntField(_this, gFaceDetectorOffsets.height);
203
204 jbyteArray bwbufferObject = (jbyteArray)
205 _env->GetObjectField(_this, gFaceDetectorOffsets.bwbuffer);
206
207 // get to our BW temporary buffer
208 jbyte* bwbuffer = _env->GetByteArrayElements(bwbufferObject, 0);
209
210 // convert the image to B/W
211 uint8_t* dst = (uint8_t*)bwbuffer;
212
213 uint16_t const* src;
214 AndroidBitmapInfo bitmapInfo;
215 AndroidBitmap_getInfo(_env, bitmap, &bitmapInfo);
216 AndroidBitmap_lockPixels(_env, bitmap, (void**) &src);
217
218 int wpr = bitmapInfo.stride / 2;
219 for (u32 y=0 ; y<height; y++) {
220 for (u32 x=0 ; x<width ; x++) {
221 uint16_t rgb = src[x];
222 int r = rgb >> 11;
223 int g2 = (rgb >> 5) & 0x3F;
224 int b = rgb & 0x1F;
225 // L coefficients 0.299 0.587 0.11
226 int L = (r<<1) + (g2<<1) + (g2>>1) + b;
227 *dst++ = L;
228 }
229 src += wpr;
230 }
231
232 // run detection
233 btk_DCR_assignGrayByteImage(hdcr, bwbuffer, width, height);
234
235 int numberOfFaces = 0;
236 if (btk_FaceFinder_putDCR(hfd, hdcr) == btk_STATUS_OK) {
237 numberOfFaces = btk_FaceFinder_faces(hfd);
238 } else {
239 ALOGE("ERROR: Return 0 faces because error exists in btk_FaceFinder_putDCR.\n");
240 }
241
242 // release the arrays we're using
243 AndroidBitmap_unlockPixels(_env, bitmap);
244 _env->ReleaseByteArrayElements(bwbufferObject, bwbuffer, 0);
245 return numberOfFaces;
246 }
247
248 static void
get_face(JNIEnv * _env,jobject _this,jobject face,jint)249 get_face(JNIEnv *_env, jobject _this,
250 jobject face, jint)
251 {
252 btk_HDCR hdcr = (btk_HDCR)(_env->GetLongField(_this, gFaceDetectorOffsets.dcr));
253 btk_HFaceFinder hfd =
254 (btk_HFaceFinder)(_env->GetLongField(_this, gFaceDetectorOffsets.fd));
255
256 FaceData faceData;
257 btk_FaceFinder_getDCR(hfd, hdcr);
258 getFaceData(hdcr, &faceData);
259
260 _env->SetFloatField(face, gFaceOffsets.confidence, faceData.confidence);
261 _env->SetFloatField(face, gFaceOffsets.midpointx, faceData.midpointx);
262 _env->SetFloatField(face, gFaceOffsets.midpointy, faceData.midpointy);
263 _env->SetFloatField(face, gFaceOffsets.eyedist, faceData.eyedist);
264 _env->SetFloatField(face, gFaceOffsets.eulerx, 0);
265 _env->SetFloatField(face, gFaceOffsets.eulery, 0);
266 _env->SetFloatField(face, gFaceOffsets.eulerz, 0);
267 }
268
269 // ---------------------------------------------------------------------------
270
271 static const char *classPathName = "android/media/FaceDetector";
272
273 static JNINativeMethod methods[] = {
274 {"nativeClassInit", "()V", (void*)nativeClassInit },
275 {"fft_initialize", "(III)I", (void*)initialize },
276 {"fft_detect", "(Landroid/graphics/Bitmap;)I", (void*)detect },
277 {"fft_get_face", "(Landroid/media/FaceDetector$Face;I)V",(void*)get_face },
278 {"fft_destroy", "()V", (void*)destroy },
279 };
280
register_android_media_FaceDetector(JNIEnv * _env)281 int register_android_media_FaceDetector(JNIEnv *_env)
282 {
283 return jniRegisterNativeMethods(_env, classPathName, methods, NELEM(methods));
284 }
285
286 // ---------------------------------------------------------------------------
287
JNI_OnLoad(JavaVM * vm,void *)288 jint JNI_OnLoad(JavaVM* vm, void*)
289 {
290 JNIEnv* env = NULL;
291 jint result = -1;
292
293 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
294 ALOGE("ERROR: GetEnv failed\n");
295 goto bail;
296 }
297 assert(env != NULL);
298
299 if (register_android_media_FaceDetector(env) < 0) {
300 ALOGE("ERROR: MediaPlayer native registration failed\n");
301 goto bail;
302 }
303
304 /* success -- return valid version number */
305 result = JNI_VERSION_1_4;
306
307 bail:
308 return result;
309 }
310