处理AVFrame使存储的YUV旋转(YUV420P,旋转90的倍数)
程序员文章站
2022-07-01 23:30:40
...
1、原理介绍
以YUV420P为例,AVFrame结构体存储YUV为data[0]存储Y,data[1]存储U,data[2]存储V,按一行一行的顺序存储,知道如何存储后,要旋转90的倍数就简单了。
2、代码
旋转90:
for (int i = 0; i < m_srcW; i++)
{
for (int j = 0; j < m_srcH; j++)
{
m_FrameConverRotate->data[0][i * m_FrameConverRotate->linesize[0] + j] =
tmpFrameSrcYUV->data[0][(m_srcH - j - 1) * tmpFrameSrcYUV->linesize[0] + i];
}
}
//UV
for (int i = 0; i < m_srcW / 2; i++)
{
for (int j = 0; j < m_srcH / 2; j++)
{
m_FrameConverRotate->data[1][i * m_FrameConverRotate->linesize[1] + j] =
tmpFrameSrcYUV->data[1][(m_srcH / 2 - j - 1) * tmpFrameSrcYUV->linesize[1] + i];
m_FrameConverRotate->data[2][i * m_FrameConverRotate->linesize[2] + j] =
tmpFrameSrcYUV->data[2][(m_srcH / 2 - j - 1) * tmpFrameSrcYUV->linesize[2] + i];
}
}
旋转270:
for (int i = 0; i < m_srcW; i++)
{
for (int j = 0; j < m_srcH; j++)
{
m_FrameConverRotate->data[0][i * m_FrameConverRotate->linesize[0] + j] =
tmpFrameSrcYUV->data[0][j * tmpFrameSrcYUV->linesize[0] + (tmpFrameSrcYUV->linesize[0] - i - 1)];
}
}
for (int i = 0; i < m_srcW / 2; i++)
{
for (int j = 0; j < m_srcH / 2; j++)
{
m_FrameConverRotate->data[1][i * m_FrameConverRotate->linesize[1] + j] =
tmpFrameSrcYUV->data[1][j * tmpFrameSrcYUV->linesize[1] + (tmpFrameSrcYUV->linesize[1] - i - 1)];
m_FrameConverRotate->data[2][i * m_FrameConverRotate->linesize[2] + j] =
tmpFrameSrcYUV->data[2][j * tmpFrameSrcYUV->linesize[2] + (tmpFrameSrcYUV->linesize[2] - i - 1)];
}
}
180度上下颠倒即可,在此就不做示例了。