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