FFmpeg - RGB圖像編碼爲h264出現垂直旋轉的問題

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章