I'm trying to write an YUV420P to RGB888 for when I have the entire thing as one giant buffer with Y
(of size width*height
) then Cr
(of size width*height/4
) then Cb
(of size width*height/4
). The output should be an RGB buffer with size width*height*3
.
I think my function below is very inefficient. For example, I use the ceiling function (shouldn't it return an int? In my case it's returning a double, why?) and I've never seen any color conversion function use this function. But this is the way I found to get the corresponding Cr
and Cb
to each Y
.
JNIEXPORT void JNICALL Java_com_example_mediacodecdecoderexample_YuvToRgb_YUVtoRBGA2(JNIEnv * env, jobject obj, jbyteArray yuv420sp, jint width, jint height, jbyteArray rgbOut)
{
//ITU-R BT.601 conversion
//
// R = 1.164*(Y-16)+1.596*(Cr-128)
// G = 1.164*(Y-16)-0.392*(Cb-128)-0.813*(Cr-128)
// B = 1.164*(Y-16)+2.017*(Cb-128)
//
int Y;
int Cr;
int Cb;
int R;
int G;
int B;
int size = width * height;
//After width*height luminance values we have the Cr values
size_t CrBase = size;
//After width*height luminance values + width*height/4 we have the Cb values
size_t CbBase = size + width*height/4;
jbyte *rgbData = (jbyte*) ((*env)->GetPrimitiveArrayCritical(env, rgbOut, 0));
jbyte* yuv = (jbyte*) (*env)->GetPrimitiveArrayCritical(env, yuv420sp, 0);
for (int i=0; i<size; i++) {
Y = rgbData[i] - 16;
Cr = rgbData[CrBase + ceil(i/4)] - 128;
Cb = rgbData[CbBase + ceil(i/4)] - 128;
R = 1.164*Y+1.596*Cr;
G = 1.164*Y-0.392*Cb-0.813*Cr;
B = 1.164*Y+2.017*Cb;
yuv[i*3] = R;
yuv[i*3+1] = G;
yuv[i*3+2] = B;
}
(*env)->ReleasePrimitiveArrayCritical(env, rgbOut, rgbData, 0);
(*env)->ReleasePrimitiveArrayCritical(env, yuv420sp, yuv, 0);
}
I'm doing this because I haven't found a function that does exactly this and I need one for a MediaCodec decoded buffer. But even if there's one, I'd like to know what can be done to improve my function, just to learn.
UPDATE:
I modified the code based on the answer below in order for it to work with ByteBuffer:
JNIEXPORT void JNICALL Java_com_lucaszanella_mediacodecdecoderexample_YuvToRgb_YUVtoRBGA2(JNIEnv * env, jobject obj, jobject yuv420sp, jint width, jint height, jobject rgbOut)
{
//ITU-R BT.601 conversion
//
// R = 1.164*(Y-16)+1.596*(Cr-128)
// G = 1.164*(Y-16)-0.392*(Cb-128)-0.813*(Cr-128)
// B = 1.164*(Y-16)+2.017*(Cb-128)
//
char *rgbData = (char*)(*env)->GetDirectBufferAddress(env, rgbOut);
char *yuv = (char*)(*env)->GetDirectBufferAddress(env, yuv420sp);
const int size = width * height;
//After width*height luminance values we have the Cr values
const size_t CrBase = size;
//After width*height luminance values + width*height/4 we have the Cb values
const size_t CbBase = size + width*height/4;
for (int i=0; i<size; i++) {
int Y = yuv[i] - 16;
int Cr = yuv[CrBase + i/4] - 128;
int Cb = yuv[CbBase + i/4] - 128;
double R = 1.164*Y+1.596*Cr;
double G = 1.164*Y-0.392*Cb-0.813*Cr;
double B = 1.164*Y+2.017*Cb;
rgbData[i*3] = (R > 255) ? 255 : ((R < 0) ? 0 : R);
rgbData[i*3+1] = (G > 255) ? 255 : ((G < 0) ? 0 : G);
rgbData[i*3+2] = (B > 255) ? 255 : ((B < 0) ? 0 : B);
}
}
however it's crashing. I don't see anything being written outside of boundary. Anyone have any idea?
UPDATE:
Code above works if we call it with a direct byte buffer. Won't work if the buffer is not direct.
Added
if (rgbData==NULL) {
__android_log_print(ANDROID_LOG_ERROR, "TRACKERS", "%s", "RGB data null");
}
if (yuv==NULL) {
__android_log_print(ANDROID_LOG_ERROR, "TRACKERS", "%s", "yuv data null");
}
if (rgbData==NULL || yuv==NULL) {
return;
}
for safety.
Anyways, color is not correct: