Skip to content

Instantly share code, notes, and snippets.

@tamarous
Created September 12, 2018 02:54
Show Gist options
  • Save tamarous/4f05095026959146af2693730d7a55e3 to your computer and use it in GitHub Desktop.
Save tamarous/4f05095026959146af2693730d7a55e3 to your computer and use it in GitHub Desktop.
Convert NV12 to BGR24 format.
void NV12ToBGR24(uint8_t* pYUV, uint8_t* pBGR24, int width, int height) {
if(width < 1 || height < 1 || pYUV == NULL || pBGR24 == NULL)
return;
const long len = width * height;
unsigned char* yData = pYUV;
unsigned char* vData = &yData[len];
unsigned char* uData = &vData[len >> 2];
int bgr[3];
int yIdx, uIdx, vIdx, idx;
for(int i = 0; i < height; i++) {
for(int j = 0; j < width; j++) {
yIdx = i * width + j;
vIdx = (i / 2) * (width / 2) + (j / 2);
uIdx = vIdx;
bgr[0] = (int)(yData[yIdx] + (uData[vIdx] - 128) + (((uData[vIdx] - 128) * 198) >> 8)); // b分量
bgr[1] = (int)(yData[yIdx] - (((uData[vIdx] - 128) * 88) >> 8) - (((vData[vIdx] - 128) * 183) >> 8)); // g分量
bgr[2] = (int)(yData[yIdx] + (vData[uIdx] - 128) + (((vData[uIdx] - 128) * 103) >> 8));
for(int k = 0; k < 3; k++) {
idx = (i * width + j) * 3 + k;
if(bgr[k] >= 0 && bgr[k] <= 255)
pBGR24[idx] = bgr[k];
else
pBGR24[idx] = (bgr[k] < 0) ? 0 : 255;
}
}
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment