# yuv 图像里的stride和plane的解释

stride可以翻译为：跨距

stride指在内存中每行像素所占的空间。如下图所示，为了实现内存对齐（或者其它的什么原因），每行像素在内存中所占的空间并不是图像的宽度。

plane一般是以luma plane、chroma plane的形式出现，其实就是luma层和chroma层，就像RGB，要用三个plane来存。

写这段记录的目的就是想说这个stride的问题，所以在进行yuv420p，yuv420sp，等视频帧转换时一定要注意跨距stride这个参数。

When a video image is stored in memory, the memory buffer might contain extra padding bytes after each row of pixels. The padding bytes affect how the image is stored in memory, but do not affect how the image is displayed.

The stride is the number of bytes from one row of pixels in memory to the next row of pixels in memory. Stride is also called pitch. If padding bytes are present, the stride is wider than the width of the image, as shown in the following illustration.

Two buffers that contain video frames with equal dimensions can have two different strides. If you process a video image, you must take the stride into account.

In addition, there are two ways that an image can be arranged in memory. In a top-down image, the top row of pixels in the image appears first in memory. In a bottom-up image, the last row of pixels appears first in memory. The following illustration shows the difference between a top-down image and a bottom-up image.

A bottom-up image has a negative stride, because stride is defined as the number of bytes need to move down a row of pixels, relative to the displayed image. YUV images should always be top-down, and any image that is contained in a Direct3D surface must be top-down. RGB images in system memory are usually bottom-up.

Video transforms in particular need to handle buffers with mismatched strides, because the input buffer might not match the output buffer. For example, suppose that you want to convert a source image and write the result to a destination image. Assume that both images have the same width and height, but might not have the same pixel format or the same image stride.

The following example code shows a generalized approach for writing this kind of function. This is not a complete working example, because it abstracts many of the specific details.

void rv_csp_i420_uyvy(
uint8_t *y_plane,   // Y plane of I420
uint8_t *u_plane,   // U plane of I420
uint8_t *v_plane,   // V plane of I420
int y_stride,       // Y stride of I420, in pixel
int uv_stride,      // U and V stride of I420, in pixel
uint8_t *image,     // output UYVY image
int width,          // image width
int height)         // image height
{
int row;
int col;
uint8_t *pImg = image;

for (row = 0; row < height; row = row+1)     {
for (col = 0; col < width; col = col+2)         {
pImg[0] = u_plane[row/2 * uv_stride + col/2];
pImg[1] = y_plane[row * y_stride + col];
pImg[2] = v_plane[row/2 * uv_stride + col/2];
pImg[3] = y_plane[row * y_stride + col + 1];
pImg += 4;
}
}
}

posted @ 2015-11-05 15:11  微信公众号--共鸣圈  阅读(6828)  评论(0编辑  收藏