DJI-Mobile-SDK-Tutorials / Android-VideoStreamDecodingSample

This sample project demonstrates how to use FFmpeg for video frame parsing and to use MediaCodec for hardware decoding on DJI Products.
MIT License
173 stars 80 forks source link

What's the function of variables "nu" and "nv" in method saveYuvDataToJPEG? #50

Open mizhoux opened 5 years ago

mizhoux commented 5 years ago
byte[] y = new byte[width * height];
byte[] u = new byte[width * height / 4];
byte[] v = new byte[width * height / 4];
byte[] nu = new byte[width * height / 4]; //
byte[] nv = new byte[width * height / 4];

System.arraycopy(yuvFrame, 0, y, 0, y.length);
for (int i = 0; i < u.length; i++) {
      v[i] = yuvFrame[y.length + 2 * i];
      u[i] = yuvFrame[y.length + 2 * i + 1];
}

The code above seems that the YUV data received from the UVA is already encoded by NV21, the array u and array v represent the data U and V respectively. So I'm confused about the function of the following code due to I don't know what the variables "nu" and "nv" are for.

int uvWidth = width / 2;
int uvHeight = height / 2;
for (int j = 0; j < uvWidth / 2; j++) {
    for (int i = 0; i < uvHeight / 2; i++) {
        byte uSample1 = u[i * uvWidth + j];
        byte uSample2 = u[i * uvWidth + j + uvWidth / 2];
        byte vSample1 = v[(i + uvHeight / 2) * uvWidth + j];
        byte vSample2 = v[(i + uvHeight / 2) * uvWidth + j + uvWidth / 2];
        nu[2 * (i * uvWidth + j)] = uSample1;
        nu[2 * (i * uvWidth + j) + 1] = uSample1;
        nu[2 * (i * uvWidth + j) + uvWidth] = uSample2;
        nu[2 * (i * uvWidth + j) + 1 + uvWidth] = uSample2;
        nv[2 * (i * uvWidth + j)] = vSample1;
        nv[2 * (i * uvWidth + j) + 1] = vSample1;
        nv[2 * (i * uvWidth + j) + uvWidth] = vSample2;
        nv[2 * (i * uvWidth + j) + 1 + uvWidth] = vSample2;
    }
}

//nv21test
byte[] bytes = new byte[yuvFrame.length];
System.arraycopy(y, 0, bytes, 0, y.length);
for (int i = 0; i < u.length; i++) {
    bytes[y.length + (i * 2)] = nv[i];
    bytes[y.length + (i * 2) + 1] = nu[i];
}
neilyoung commented 5 years ago

did you ever get an answer somehow?