在 Android/NDK 下,我使用下一个算法将 RGB 转换为 YUV420sp。我开始收到一些用户的少量崩溃报告。因此,在他们向我发送日志后,在我发布评论时,下一个算法似乎都失败了,就在第二个算法decodeARGB888_YUV420sp,日志显示一个SIGILL错误,我发现它具有下一个定义:
非法指令 (ANSI) 通常表示可执行文件已损坏或使用了预期指向函数的数据。
这些报告都来自 SGSII-skyrocket 和 Rogers 变体,以及三星 Captivate Glide。并非所有人,因为使用这些设备的其他一些用户也没有报告任何问题。
这些设备硬件有什么特定的吗?或者任何已知的错误?或者我在我的代码中做错了什么?
JNIEXPORT void JNICALL Java_com_test_NatLib_decodeBitmapToYuv420sp(JNIEnv *env, jobject javaThis, jobject bitmap, jbyteArray yuv)
{
AndroidBitmapInfo bitmapInfo;
int code;
void *nativeRGBPixels;
if ((code = AndroidBitmap_getInfo(env, bitmap, &bitmapInfo)) < 0)
{
return;
}
if ((code = AndroidBitmap_lockPixels(env, bitmap, (void**)&nativeRGBPixels)) < 0)
{
return;
}
char *nativeYuvPixels = (char*)env->GetByteArrayElements(yuv, 0);
decodeARGB888_YUV420sp(nativeRGBPixels, nativeYuvPixels, bitmapInfo.width, bitmapInfo.height);
env->ReleaseByteArrayElements(yuv, (jbyte*)nativeYuvPixels , 0);
AndroidBitmap_unlockPixels(env, bitmap);
}
static void decodeARGB888_YUV420sp(const int *argb, char *yuv, const int width, const int height)
{
const int totalPixels = width * height;
int indexPixel = 0;
int indexY = 0;
int indexUV = totalPixels;
int R, G, B, Y, U, V;
int x, y;
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
{
//--------------------------------------------------------------------
// AT THIS POINT IS WHERE THE LAST LOG WITH SIGILL WAS CAPTURED
//--------------------------------------------------------------------
const int pixelValue = argb[indexPixel];
R = pixelValue & 0xff;
G = (pixelValue & 0xff00) >> 8;
B = (pixelValue & 0xff0000) >> 16;
// RGB to YUV algorithm for component Y
Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
// NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2 meaning for
// every 4 Y pixels there are 1 V and 1 U. Note the sampling is every other pixel AND every other scanline
yuv[indexY++] = (char)((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
if (y % 2 == 0 && indexPixel % 2 == 0)
{
// RGB to YUV algorithm for component U & V
U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
yuv[indexUV++] = (char)((V < 0) ? 0 : ((V > 255) ? 255 : V));
yuv[indexUV++] = (char)((U < 0) ? 0 : ((U > 255) ? 255 : U));
}
indexPixel++;
}
}
}