bpdng / app / src / main / cpp / bpdng.cpp
bpdng.cpp
Raw

#include <jni.h>
#include <arm_neon.h>
#include <android/bitmap.h>
#include <cmath>
#include <cstring>

namespace {

    void applymap(uint16_t *raw_buffer, int layout, float *map) {
        const int bl = 64; // black level
        const int st = 4080; // stride

        for (int by = 0; by < 12; by++) {
            for (int mbx = 0; mbx < 8; mbx++) {
                // LensShadingMap grid point indices (Left, Middle, Right)
                int iL = (by * 17 + (mbx * 2)) * 4;
                int iM = iL + 4;
                int iR = iL + 8;

                for (int y = 0; y < 256; y += 2) {
                    float v = y / 256.0f;
                    uint16_t *row0 = &raw_buffer[(by * 256 + y) * st + (mbx * 510)];
                    uint16_t *row1 = row0 + st;

                    // 1. Vertical interpolation
                    float L_tmp[4], M_tmp[4], R_tmp[4];
                    for(int c = 0; c < 4; c++) {
                        L_tmp[c] = map[iL+c] + (map[iL+68+c] - map[iL+c]) * v;
                        M_tmp[c] = map[iM+c] + (map[iM+68+c] - map[iM+c]) * v;
                        R_tmp[c] = map[iR+c] + (map[iR+68+c] - map[iR+c]) * v;
                    }

                    // 2. Channel mapping: Maps Map-order [R, Ge, Go, B] to Buffer-order
                    int m[4];
                    if (layout == 1) {
                        // GRBG Normal
                        m[0] = 1; m[1] = 0; m[2] = 3; m[3] = 2;
                    } else {
                        // GBRG 180-degree rotated
                        m[0] = 2; m[1] = 3; m[2] = 0; m[3] = 1;
                    }

                    // 3. Horizontal interpolation setup
                    float L[4], M[4], step1[4], step2[4];
                    for(int c = 0; c < 4; c++) {
                        L[c] = L_tmp[m[c]];
                        M[c] = M_tmp[m[c]];
                        step1[c] = (M[c] - L[c]) / 255.0f;
                        step2[c] = (R_tmp[m[c]] - M[c]) / 255.0f;
                    }

                    // 4. Block 1: Pixels 0 to 253
                    for (int x = 0; x < 254; x += 2) {
                        row0[x]   = fmaxf((row0[x]   - bl) * L[0] + bl, 0);
                        row0[x+1] = fmaxf((row0[x+1] - bl) * L[1] + bl, 0);
                        row1[x]   = fmaxf((row1[x]   - bl) * L[2] + bl, 0);
                        row1[x+1] = fmaxf((row1[x+1] - bl) * L[3] + bl, 0);
                        for(int c = 0; c < 4; c++) L[c] += step1[c] * 2;
                    }

                    // 5. Bridge: Synchronize Bayer clusters with 255-pixel grid
                    row0[254] = fmaxf((row0[254] - bl) * L[0] + bl, 0);
                    row1[254] = fmaxf((row1[254] - bl) * L[2] + bl, 0);
                    row0[255] = fmaxf((row0[255] - bl) * M[1] + bl, 0);
                    row1[255] = fmaxf((row1[255] - bl) * M[3] + bl, 0);

                    // 6. Block 2: Pixels 256 to 510
                    for(int c = 0; c < 4; c++) M[c] += step2[c];
                    for (int x = 256; x < 510; x += 2) {
                        row0[x]   = fmaxf((row0[x]   - bl) * M[0] + bl, 0);
                        row0[x+1] = fmaxf((row0[x+1] - bl) * M[1] + bl, 0);
                        row1[x]   = fmaxf((row1[x]   - bl) * M[2] + bl, 0);
                        row1[x+1] = fmaxf((row1[x+1] - bl) * M[3] + bl, 0);
                        for(int c = 0; c < 4; c++) M[c] += step2[c] * 2;
                    }
                }
            }
        }
    }

    // for hand shake detection Brenner works better as Laplacian
    double calculatesharpness(
            const uint16_t *buffer,
            int width,
            int height,
            int iso)
    {
        const int roi_size = 1280;
        const int start_x = (width  - roi_size) / 2;
        const int start_y = (height - roi_size) / 2;

        // Noise floor (based on SENSOR_NOISE_PROFILE)
        uint16_t noise_floor;
        switch(iso) {
            case 100: noise_floor = 11; break;
            case 200: noise_floor = 13; break;
            case 400: noise_floor = 16; break;
            case 800: noise_floor = 20; break;
            default:  noise_floor = 16;
        }
        uint16x8_t noise = vdupq_n_u16(noise_floor);
        int64x2_t sumh = vdupq_n_s64(0);
        int64x2_t sumv = vdupq_n_s64(0);

        // processes same amount of pixels in H and V direction
        for (int y = start_y; y < start_y + roi_size - 2; y += 2) {
            // Row 0: G R G R (even)
            // Row 1: B G B G (odd)
            const uint16_t *row0 = buffer + y * width + start_x;
            const uint16_t *row2 = buffer + (y + 2) * width + start_x;
            uint16x8_t prev = vdupq_n_u16(*row0); // first column for H compare is discarded
            for (int x = 0; x < roi_size; x += 16) {
                // Load and isolate green
                uint16x8_t g00 = vld2q_u16(row0 + x).val[0];
                uint16x8_t g02 = vextq_u16(prev, g00, 7);
                uint16x8_t g20 = vld2q_u16(row2 + x).val[0];
                prev = g00;
                // Brenner diffs
                uint16x8_t h = vabdq_u16(g00, g02);
                uint16x8_t v = vabdq_u16(g00, g20);
                // Noise floor
                h = vqsubq_u16(h, noise);
                v = vqsubq_u16(v, noise);
                // Square and accumulate
                uint32x4_t t;
                t = vmull_u16(vget_low_u16(h), vget_low_u16(h));
                t = vmlal_u16(t, vget_high_u16(h), vget_high_u16(h));
                sumh = vpadalq_u32(sumh, t);
                t = vmull_u16(vget_low_u16(v), vget_low_u16(v));
                t = vmlal_u16(t, vget_high_u16(v), vget_high_u16(v));
                sumv = vpadalq_u32(sumv, t);
            }
        }

        double H = vgetq_lane_s64(sumh, 0) + vgetq_lane_s64(sumh, 1);
        double V = vgetq_lane_s64(sumv, 0) + vgetq_lane_s64(sumv, 1);
        return H * V;
    }

}

extern "C" {

void Java_com_example_bpdng_Native_applymap(
        JNIEnv *env, jclass clazz,
        jobject raw_short_buffer, jint layout,
        jfloatArray map) {
    auto raw_buffer = static_cast<uint16_t *>(env->GetDirectBufferAddress(raw_short_buffer));
    if (raw_buffer == nullptr) return;
    jfloat* map_ptr = env->GetFloatArrayElements(map, nullptr);
    if (map_ptr != nullptr) {
        applymap(raw_buffer, layout, map_ptr);
        env->ReleaseFloatArrayElements(map, map_ptr, JNI_ABORT);
    }
}

jdouble Java_com_example_bpdng_Native_calculatesharpness(
        JNIEnv *env, jclass clazz,
        jobject raw_short_buffer, jint width, jint height, jint iso) {
    auto raw_buffer = static_cast<uint16_t *>(env->GetDirectBufferAddress(raw_short_buffer));
    if (raw_buffer == nullptr) return 0;
    return calculatesharpness(raw_buffer, width, height, iso);
}

}  // extern "C"