1 问题描述
在Unity中或者OpenGL中抓取的帧缓冲区数据(Unity为Texture2D)即一张RGB图片数据使用ffmpeg做.h264编码后使用vlc播放出现了图像垂直颠倒的问题,如下图所示:
正常的图片:
.h264编码之后在vlc中播放出现了垂直翻转的问题:
2 解决方法
在sws_scale之前进行RGB数据的垂直翻转,可使用以下函数:
///
/// @brief 垂直翻转RGB数据
/// @param[in] width - RGB图像宽度
/// @param[in] height - RGB图像高度
/// @param[in] rgbData - RGB图像数据指针
/// @param[in] bitsPerPixel - 每一个像素的字节大小,通常为rgbData的长度/width*height
///
void h264Encoder::VerticalRotateRGBData(int width, int height, unsigned char* rgbData, int bitsPerPixel)
{
unsigned char* tempRgbData = new unsigned char[width*bitsPerPixel];
height--;
int index = (height + 1) / 2;
for (int y = 0; y < index; y++)
{
memcpy(tempRgbData, &rgbData[y*width*bitsPerPixel], width*bitsPerPixel);
memcpy(&rgbData[y*width*bitsPerPixel], &rgbData[(height - y)*width*bitsPerPixel], width*bitsPerPixel);
memcpy(&rgbData[(height - y)*width*bitsPerPixel], tempRgbData, width*bitsPerPixel);
}
delete[] tempRgbData;
}