FFmpeg裏面的sws_scale庫可以在一個函數裏面同時實現:1.圖像色彩空間轉換;2.分辨率縮放;3.前後圖像濾波處理。
其核心函數主要有三個:
// 初始化sws_scale
struct SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
int dstW, int dstH, enum AVPixelFormat dstFormat,
int flags,
SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param);
參數int srcW, int srcH, enum AVPixelFormat srcFormat定義輸入圖像信息(寬、高、顏色空間)
參數int dstW, int dstH, enum AVPixelFormat dstFormat定義輸出圖像信息。
參數int flags選擇縮放算法(只有當輸入輸出圖像大小不同時有效)
參數SwsFilter *srcFilter, SwsFilter *dstFilter分別定義輸入/輸出圖像濾波器信息,如果不做前後圖像濾波,輸入NULL
參數const double *param定義特定縮放算法需要的參數(?),默認爲NULL
函數返回SwsContext結構體,定義了基本變換信息。
如果是對一個序列的所有幀做相同的處理,函數sws_getContext只需要調用一次就可以了。
sws_getContext(w, h, YV12, w, h, NV12, 0, NULL, NULL, NULL); // YV12->NV12 色彩空間轉換
sws_getContext(w, h, YV12, w/2, h/2, YV12, 0, NULL, NULL, NULL); // YV12圖像縮小到原圖1/4
sws_getContext(w, h, YV12, 2w, 2h, YN12, 0, NULL, NULL, NULL); // YV12圖像放大到原圖4倍,並轉換爲NV12結構
// 做轉換
int sws_scale(struct SwsContext *c,
const uint8_t *const srcSlice[], const int srcStride[],
int srcSliceY, int srcSliceH,
uint8_t *const dst[], const int dstStride[]);
參數struct SwsContext *c,爲上面sws_getContext函數返回值;
參數const uint8_t *const srcSlice[], const int srcStride[]定義輸入圖像信息(當前處理區域的每個通道數據指針,每個通道行字節數)
stride定義下一行的起始位置。stride和width不一定相同,這是因爲:
1.由於數據幀存儲的對齊,有可能會向每行後面增加一些填充字節這樣 stride = width + N;
2.packet色彩空間下,每個像素幾個通道數據混合在一起,例如RGB24,每個像素3字節連續存放,因此下一行的位置需要跳過3*width字節。
srcSlice和srcStride的維數相同,由srcFormat值來。
csp 維數 寬width 跨度stride 高
YUV420 3 w, w/2, w/2 s, s/2, s/2 h, h/2, h/2
YUYV 1 w, w/2, w/2 2s, 0, 0 h, h, h
NV12 2 w, w/2, w/2 s, s, 0 h, h/2
RGB24 1 w, w, w 3s, 0, 0 h, 0, 0
參數int srcSliceY, int srcSliceH,定義在輸入圖像上處理區域,srcSliceY是起始位置,srcSliceH是處理多少行。如果srcSliceY=0,srcSliceH=height,表示一次性處理完整個圖像。
這種設置是爲了多線程並行,例如可以創建兩個線程,第一個線程處理 [0, h/2-1]行,第二個線程處理 [h/2, h-1]行。並行處理加快速度。
參數uint8_t *const dst[], const int dstStride[]定義輸出圖像信息(輸出的每個通道數據指針,每個通道行字節數)
// 釋放sws_scale
void sws_freeContext(struct SwsContext *swsContext);