0
我正在用x264編寫一個視頻會議應用程序作爲編碼器。我從網絡攝像頭接收YUY2原始幀,然後根據x264的要求將它們轉換爲I420。問題是 - 我自己寫了轉換算法,並沒有那麼快(佔整個CPU時間的20%,由profiler提供的數據)。我怎樣才能讓它更快?這裏是我的代碼:優化YUY2到I420的轉換
int YUY2ToI420(BYTE *in, BYTE *out){
long pixels = _width * _height;
long macropixels = pixels/2; // macropixel count
// new size will be w * h * 3/2 -> 12 bits per pixel 4:2:0
long mpx_per_row = info.biWidth/2;
// for each macropixel
for (int i = 0, ci = 0; i < macropixels; i++){ // ci is chroma index
// get macropixel address, order is Y0 U0 Y1 V0
BYTE *mpAddress = in + i * 4;
// copy luma data
out[i * 2] = mpAddress[0];
out[i * 2 + 1] = mpAddress[2];
// copy chroma data - we skip odd rows because of 4:2:0 sampling
long row_number = i/mpx_per_row;
if (row_number % 2 == 0) {
out[pixels + ci] = mpAddress[1]; // shift by Y vector
out[pixels + pixels/4 + ci] = mpAddress[3]; // shift by Y and U vector
ci++;
}
}
return pixels * 12/8; // I420
}