Home > Android Development > decodeYUV420SP using JNI = Nice performance boost

decodeYUV420SP using JNI = Nice performance boost


One of the major issues when trying to implement custom camera live preview,
is converting the byte[] data from the public void onPreviewFrame(byte[] data, Camera camera) function,
to RGB.

This has a huge impact on my app frame rate.

After implementing it using JNI and the NDK, I’ve noticed a huge performance difference.
I didn’t measure it though, so I can give the exact numbers.

I’ve still have some performance issues, that need to be taken care of.

In this stage the app doesn’t have the smooth performance I wan’t…

Advertisements
Categories: Android Development
  1. Park Jung Un
    July 12, 2010 at 8:46 pm

    Hello, I’am student who study the android.
    I’am from korean. I has been doing the android project.
    and I has a problem. it is converting the byte[] data from the public void onPreviewFrame(byte[] data, Camera camera) function
    So. would I get decodeYUV420SP function?
    … Bye.. Thank you

  2. July 12, 2010 at 11:28 pm

    Hi,
    Here is the Java code to convert YUV to RGB:

    static public void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width,
    int height) {
    final int frameSize = width * height;

    for (int j = 0, yp = 0; j > 1) * width, u = 0, v = 0;
    for (int i = 0; i < width; i++, yp++) {
    int y = (0xff & ((int) yuv420sp[yp])) – 16;
    if (y < 0)
    y = 0;
    if ((i & 1) == 0) {
    v = (0xff & yuv420sp[uvp++]) – 128;
    u = (0xff & yuv420sp[uvp++]) – 128;
    }

    int y1192 = 1192 * y;
    int r = (y1192 + 1634 * v);
    int g = (y1192 – 833 * v – 400 * u);
    int b = (y1192 + 2066 * u);

    if (r 262143)
    r = 262143;
    if (g 262143)
    g = 262143;
    if (b 262143)
    b = 262143;

    rgb[yp] = 0xff000000 | ((r <> 2) & 0xff00) | ((b >> 10) & 0xff);
    }
    }
    }

  3. Amber
    August 14, 2011 at 12:22 am

    Hi! Do u think it’s possible for you to share your converted yuvtorgb in jni format? I did a conversion but my results were distorted with major green patches and purple colour.

    Thanks!

    • August 14, 2011 at 1:50 am

      Hi,
      What resolution is your preview?
      Have you tried setting it to a lower res?
      I’ve found that 640×480 was causing problems.

  4. Amber
    August 14, 2011 at 8:10 am

    Hi I did 2 types of testing with the abv code, 1st type is that I decoded in java with 640×480 it wrks fine, but however when I added extra camera effect/filter , out of memory prob occurred. 2nd type is I tried linking them to jni to do the decoding with 320×240 preview size but all I got was the green patches n purple patches. Thanks for responding!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: