从网上找的一团乱糟糟 索性自己写个吧 其实也很简单的
很多人对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];
- }
- }
- }