近期中视频流处理,需实现视频在QT中显示,视频源利用Gstreamer采集,得到一帧YUV视频数据帧;
bool yuv420pToRgb24(char* ch_YuvData,uint i_width,uint i_height,uchar* ch_RgbData)
{
int temp = 0;
int numOfPixel = i_width * i_height;
int positionOfV = numOfPixel;
int positionOfU = numOfPixel/4 + numOfPixel;
for(int i=0; i<i_height; i++)
{
int startY = i*i_width;
int step = (i/2)*(i_width/2);
int startV = positionOfV + step;
int startU = positionOfU + step;
for(int j = 0; j < i_width; j++)
{
int Y = startY + j;
int V = startV + j/2;
int U = startU + j/2;
int index = Y*3;
temp = ((ch_YuvData[Y]&0xff) + 1.4075 * ((ch_YuvData[V]&0xff)-128));
ch_RgbData[index+2] = temp<0 ? 0 : (temp > 255 ? 255 : temp);
temp = ((ch_YuvData[Y]&0xff) - 0.3455* ((ch_YuvData[U]&0xff)-128) - 0.7169*((ch_YuvData[V]&0xff)-128));
ch_RgbData[index+1] = temp<0 ? 0 : (temp > 255 ? 255 : temp);
temp = ((ch_YuvData[Y]&0xff) + 1.779* ((ch_YuvData[U]&0xff)-128));
ch_RgbData[index+0] =temp<0 ? 0 : (temp > 255 ? 255 : temp);
}
}
return true;
}
char* ch_YuvData---原始视频帧YUV
uint i_width ---视频宽度
uint i_height ---视频高度
uchar* ch_RgbData ---转换后RGB24
该转换方式亲测可用视频YUV为I420格式,具体格式排列自行百度;由于后期需移植嵌入式设备,该方法CPU占用率略高,且转换效率低,双循环,640*480就要执行307200次,这里我把数据都转int尽量避免浮点运算。网上也有大神写了优化方案,自行百度;
最后,建议还是使用库转换吧(^_^)
后期附上最终解决方案
如有疑问加群:676977101(新群)