從網上找的一團亂糟糟 索性自己寫個吧 其實也很簡單的
很多人對YUV數據格式不清楚,以至於在做視頻的時候出現了一些不可預知的錯誤(比如說圖像帶有點、顏色不對等)。今晚是週末放假,我就抽點時間來給大家介紹一下。
提示: 讀下面的文字時,希望大家結合圖片看,這樣更易理解
在YUV420中,一個像素點對應一個Y,一個2X2的小方塊對應一個U和V。對於所有YUV420圖像,它們的Y值排列是完全相同的,因爲只有Y的圖像就是灰度圖像。YUV420sp與YUV420p的數據格式它們的UV排列在原理上是完全不同的。420p它是先把U存放完後,再存放V,也就是說UV它們是連續的。而420sp它是UV、UV這樣交替存放的。(見下圖)
有了上面的理論,我就可以準確的計算出一個YUV420在內存中存放的大小。
width * hight =Y(總和)
U = Y / 4
V = Y / 4
所以YUV420 數據在內存中的長度是 width * hight * 3 / 2,
假設一個分辨率爲8X4的YUV圖像,它們的格式如下圖:
YUV420sp格式如下圖 YUV420p數據格式如下圖
- class yuv420_rotate
- {
- public:
- yuv420_rotate(void);
- ~yuv420_rotate(void);
- public:
- //rotate clockwise
- static void yuv_rotate_90(BYTE *des,BYTE *src,int width,int height);
- static void yuv_rotate_180(BYTE *des,BYTE *src,int width,int height);
- static void yuv_rotate_270(BYTE *des,BYTE *src,int width,int height);
- //flip
- static void yuv_flip_horizontal(BYTE *des,BYTE *src,int width,int height);
- static void yuv_flip_vertical(BYTE *des,BYTE *src,int width,int height);
- };
- yuv420_rotate::yuv420_rotate(void)
- {
- }
- yuv420_rotate::~yuv420_rotate(void)
- {
- }
- void yuv420_rotate::yuv_rotate_90(BYTE *des,BYTE *src,int width,int height)
- {
- int n = 0;
- int hw = width / 2;
- int hh = height / 2;
- //copy y
- for(int j = 0; j < width;j++)
- {
- for(int i = height - 1; i >= 0; i--)
- {
- des[n++] = src[width * i + j];
- }
- }
- //copy u
- BYTE *ptemp = src + width * height;
- for(int j = 0;j < hw;j++)
- {
- for(int i = hh - 1;i >= 0;i--)
- {
- des[n++] = ptemp[ hw*i + j ];
- }
- }
- //copy v
- ptemp += width * height / 4;
- for(int j = 0; j < hw; j++)
- {
- for(int i = hh - 1;i >= 0;i--)
- {
- des[n++] = ptemp[hw*i + j];
- }
- }
- }
- void yuv420_rotate::yuv_rotate_180(BYTE *des,BYTE *src,int width,int height)
- {
- int n = 0;
- int hw = width / 2;
- int hh = height / 2;
- //copy y
- for(int j = height - 1; j >= 0; j--)
- {
- for(int i = width; i > 0; i--)
- {
- des[n++] = src[width*j + i];
- }
- }
- //copy u
- BYTE *ptemp = src + width * height;
- for(int j = hh - 1;j >= 0; j--)
- {
- for(int i = hw; i > 0; i--)
- {
- des[n++] = ptemp[hw * j + i];
- }
- }
- //copy v
- ptemp += width * height / 4;
- for(int j = hh - 1;j >= 0; j--)
- {
- for(int i = hw; i > 0; i--)
- {
- des[n++] = ptemp[hw * j + i];
- }
- }
- }
- void yuv420_rotate::yuv_rotate_270(BYTE *des,BYTE *src,int width,int height)
- {
- int n = 0;
- int hw = width / 2;
- int hh = height / 2;
- //copy y
- for(int j = width; j > 0; j--)
- {
- for(int i = 0; i < height;i++)
- {
- des[n++] = src[width*i + j];
- }
- }
- //copy u
- BYTE *ptemp = src + width * height;
- for(int j = hw; j > 0;j--)
- {
- for(int i = 0; i < hh;i++)
- {
- des[n++] = ptemp[hw * i + j];
- }
- }
- //copy v
- ptemp += width * height / 4;
- for(int j = hw; j > 0;j--)
- {
- for(int i = 0; i < hh;i++)
- {
- des[n++] = ptemp[hw * i + j];
- }
- }
- }
- void yuv420_rotate::yuv_flip_horizontal(BYTE *des,BYTE *src,int width,int height)
- {
- int n = 0;
- int hw = width / 2;
- int hh = height / 2;
- //copy y
- for(int j = 0; j < height; j++)
- {
- for(int i = width - 1;i >= 0;i--)
- {
- des[n++] = src[width * j + i];
- }
- }
- //copy u
- BYTE *ptemp = src + width * height;
- for(int j = 0; j < hh; j++)
- {
- for(int i = hw - 1;i >= 0;i--)
- {
- des[n++] = ptemp[hw * j + i];
- }
- }
- //copy v
- ptemp += width * height / 4;
- for(int j = 0; j < hh; j++)
- {
- for(int i = hw - 1;i >= 0;i--)
- {
- des[n++] = ptemp[hw * j + i];
- }
- }
- }
- void yuv420_rotate::yuv_flip_vertical(BYTE *des,BYTE *src,int width,int height)
- {
- int n = 0;
- int hw = width / 2;
- int hh = height / 2;
- //copy y
- for(int j = 0; j < width;j++)
- {
- for(int i = height - 1; i >= 0;i--)
- {
- des[n++] = src[width * i + j];
- }
- }
- //copy u
- BYTE *ptemp = src + width * height;
- for(int j = 0; j < hw; j++)
- {
- for(int i = hh - 1; i >= 0;i--)
- {
- des[n++] = ptemp[ hw * i + j];
- }
- }
- //copy v
- ptemp += width * height / 4;
- for(int j = 0; j < hw; j++)
- {
- for(int i = hh - 1; i >= 0; i--)
- {
- des[n++] = ptemp[ hw * i + j];
- }
- }
- }