Discussion on the differences and interface design of color coding formats such as NV21, NV12, YV12, RGB565, YUV, etc.

NV21, NV12, YV12, RGB565, YUV literacy

NV21, NV12, YV12, RGB565, and YUV are different color encoding formats. These color encoding formats have their own characteristics and are suitable for different application scenarios. Choosing the appropriate color coding format depends on your specific needs and environment:

  1. NV21: NV21 is an image color encoding format for Android systems. It uses the YUV 4:2:0 sampling method, that is, every two pixels are sampled in the vertical direction and every pixel is sampled twice in the horizontal direction. The Y component of NV21 is brightness information, and the V and U components are chrominance information (representing saturation and hue respectively). This format is mainly used for front-facing cameras and Android's Camera API.
  2. NV12: NV12 is a color encoding format used for video encoding and decoding. It also uses the YUV 4:2:0 sampling method. The Y component of NV12 is brightness information, and the V and U components are also chrominance information. The difference is that, unlike NV21, the Y, V, and U components of NV12 adopt different sampling rates, that is, every two pixels are sampled in the vertical direction and every other pixel is sampled in the horizontal direction.
  3. YV12: YV12 is a color encoding format used for video codecs. It also uses YUV 4:2:0 sampling method. The Y component of YV12 is brightness information, and the V and U components are also chrominance information. Unlike NV12, the V and U components of YV12 swap places. This format is mainly used in software codecs such as FFmpeg.
  4. RGB565: RGB565 is a color encoding format. It has 3 channels, namely red, green and blue. The intensity values ​​of these three channels jointly determine a color. In RGB565, the precision of each channel is 5 bits (red), 6 bits (green) and 5 bits (blue). Therefore, RGB565 has a limited number of colors that can be represented.
  5. YUV: YUV is a color encoding format that separates luminance and chrominance information. In YUV format, Y is the luminance component and UV is the chrominance component. The UV component is further divided into U and V, which represent saturation and hue respectively. This format is primarily used to optimize the transmission of color video signals because luminance information can be compressed more efficiently.

How to interface with the above color coding format

When Daniu Live SDK implements the Android platform RTMP push, lightweight RTSP service and GB28181 device access module, it interfaces with the above color-coding format. The data interfaces designed in different formats are discussed below.

Data interface of YV12

The data interface of YV12 is mainly used for docking with third-party devices. The u_stride and v_stride of this interface are (width+1)/2 respectively. If the outgoing data needs to be rotated, the rotation angle can be controlled through rotation_degree.

    /**
     * YV12数据接口
     *
     * @param data: YV12 data
     *
     * @param width: 图像宽
     *
     * @param height: 图像高
     *
     * @param y_stride:  y面步长
     *
     * @param v_stride: v面步长
     *
     * @param u_stride: u面步长
     *
     * rotation_degree: 顺时针旋转, 必须是0, 90, 180, 270
     *
     * @return {0} if successful
     */
    public native int SmartPublisherOnYV12Data(long handle, byte[] data, int width, int height, int y_stride,  int v_stride, int u_stride, int rotation_degree);

YUV data interface

Supports standard I420 data interface docking, no need to go into details:

  /**
	 * 投递层I420图像
	 *
	 * @param index: 层索引, 必须大于等于0
	 *
	 * @param left: 层叠加的左上角坐标, 对于第0层的话传0
	 *
	 * @param top: 层叠加的左上角坐标, 对于第0层的话传0
	 *
	 * @param y_plane: y平面图像数据
	 *
	 * @param y_offset: 图像偏移, 这个主要目的是用来做clip的,一般传0
	 *
	 * @param y_row_stride: stride information
	 *
	 * @param u_plane: u平面图像数据
	 *
	 * @param u_offset: 图像偏移, 这个主要目的是用来做clip的,一般传0
	 *
	 * @param u_row_stride: stride information
	 *                    *
	 * @param v_plane: v平面图像数据
	 *
	 * @param v_offset: 图像偏移, 这个主要目的是用来做clip的,一般传0
	 *
	 * @param v_row_stride: stride information
	 *
	 * @param width: width, 必须大于1, 且必须是偶数
	 *
	 * @param height: height, 必须大于1, 且必须是偶数
	 *
	 * @param  is_vertical_flip: 是否垂直翻转, 0不翻转, 1翻转
	 *
	 * @param  is_horizontal_flip:是否水平翻转, 0不翻转, 1翻转
	 *
	 * @param  scale_width: 缩放宽,必须是偶数, 0或负数不缩放
	 *
	 * @param  scale_height: 缩放高, 必须是偶数, 0或负数不缩放
	 *
	 * @param  scale_filter_mode: 缩放质量, 传0使用默认速度,可选等级范围是:[1,3],值越大缩放质量越好, 但速度越慢
	 *
	 * @param  rotation_degree: 顺时针旋转, 必须是0, 90, 180, 270, 注意:旋转是在缩放, 垂直/水品反转之后再做, 请留意顺序
	 *
	 * @return {0} if successful
	 */
	public native int PostLayerImageI420ByteBuffer(long handle, int index, int left, int top,
												   ByteBuffer y_plane, int y_offset, int y_row_stride,
												   ByteBuffer u_plane, int u_offset, int u_row_stride,
												   ByteBuffer v_plane, int v_offset, int v_row_stride,
												   int width, int height, int is_vertical_flip,  int is_horizontal_flip,
												   int scale_width,  int scale_height, int scale_filter_mode,
												   int rotation_degree);

NV21 to I420 and rotate the interface

This interface is also mainly used for docking specific data types. NV21 data can be directly transferred to I420 and then docked. The interface parameters are relatively simple and will not be described in detail.

  /**
	 * NV21转换到I420并旋转
	 *
	 * @param src: nv21 data
	 *
	 * @param dst: 输出I420 data
	 *
	 * @param width: 图像宽
	 *
	 * @param height: 图像高
	 *
	 * rotation_degree: 顺时针旋转, 必须是0, 90, 180, 270
	 *
	 * @return {0} if successful
	 */
	public native int SmartPublisherNV21ToI420Rotate(long handle, byte[] src, int src_y_stride, int src_uv_stride, byte[] dst,
													 int dst_y_stride, int dst_u_stride, int dst_v_stride,
													 int width, int height,
													 int rotation_degree);

Support RGBA data access

RGBA is mainly used in screen sharing scenarios.

   /**
    * Set live video data(no encoded data).
    *
    * @param data: RGBA data
    * 
    * @param rowStride: stride information
    * 
    * @param width: width
    * 
    * @param height: height
    *
    * @return {0} if successful
    */
    public native int SmartPublisherOnCaptureVideoRGBAData(long handle,  ByteBuffer data, int rowStride, int width, int height);

    /**
     * 投递裁剪过的RGBA数据
     *
     * @param data: RGBA data
     *
     * @param rowStride: stride information
     *
     * @param width: width
     *
     * @param height: height
     *
     * @param clipedLeft: 左;  clipedTop: 上; clipedwidth: 裁剪后的宽; clipedHeight: 裁剪后的高; 确保传下去裁剪后的宽、高均为偶数
     *
     * @return {0} if successful
     */
    public native int SmartPublisherOnCaptureVideoClipedRGBAData(long handle,  ByteBuffer data, int rowStride, int width, int height, int clipedLeft, int clipedTop, int clipedWidth, int clipedHeight);

    /**
     * Set live video data(no encoded data).
     *
     * @param data: ABGR flip vertical(垂直翻转) data
     *
     * @param rowStride: stride information
     *
     * @param width: width
     *
     * @param height: height
     *
     * @return {0} if successful
     */
    public native int SmartPublisherOnCaptureVideoABGRFlipVerticalData(long handle,  ByteBuffer data, int rowStride, int width, int height);

Support RGB565 data access (mainly used in same-screen scenarios)

The RGB565 data type is also mainly used for screen capture.

    /**
     * Set live video data(no encoded data).
     *
     * @param data: RGB565 data
     *
     * @param row_stride: stride information
     *
     * @param width: width
     *
     * @param height: height
     *
     * @return {0} if successful
     */
    public native int SmartPublisherOnCaptureVideoRGB565Data(long handle,ByteBuffer data, int row_stride, int width, int height);

NV12, NV21 format

  /**
	 * 投递层NV21图像
	 *
	 * @param index: 层索引, 必须大于等于0
	 *
	 * @param left: 层叠加的左上角坐标, 对于第0层的话传0
	 *
	 * @param top: 层叠加的左上角坐标, 对于第0层的话传0
	 *
	 * @param y_plane: y平面图像数据
	 *
	 * @param y_offset: 图像偏移, 这个主要目的是用来做clip的,一般传0
	 *
	 * @param y_row_stride: stride information
	 *
	 * @param uv_plane: uv平面图像数据
	 *
	 * @param uv_offset: 图像偏移, 这个主要目的是用来做clip的,一般传0
	 *
	 * @param uv_row_stride: stride information
	 *
	 * @param width: width, 必须大于1, 且必须是偶数
	 *
	 * @param height: height, 必须大于1, 且必须是偶数
	 *
	 * @param  is_vertical_flip: 是否垂直翻转, 0不翻转, 1翻转
	 *
	 * @param  is_horizontal_flip:是否水平翻转, 0不翻转, 1翻转
	 *
	 * @param  scale_width: 缩放宽,必须是偶数, 0或负数不缩放
	 *
	 * @param  scale_height: 缩放高, 必须是偶数, 0或负数不缩放
	 *
	 * @param  scale_filter_mode: 缩放质量, 传0使用默认速度,可选等级范围是:[1,3],值越大缩放质量越好, 但速度越慢
	 *
	 * @param  rotation_degree: 顺时针旋转, 必须是0, 90, 180, 270, 注意:旋转是在缩放, 垂直/水品反转之后再做, 请留意顺序
	 *
	 * @return {0} if successful
	 */
	public native int PostLayerImageNV21ByteBuffer(long handle, int index, int left, int top,
													   ByteBuffer y_plane, int y_offset, int y_row_stride,
												       ByteBuffer uv_plane, int uv_offset, int uv_row_stride,
												       int width, int height, int is_vertical_flip,  int is_horizontal_flip,
													   int scale_width,  int scale_height, int scale_filter_mode,
													   int rotation_degree);


	/**
	 * 投递层NV21图像, 详细说明请参考PostLayerImageNV21ByteBuffer
	 *
	 * @return {0} if successful
	 */
	public native int PostLayerImageNV21ByteArray(long handle, int index, int left, int top,
												   byte[] y_plane, int y_offset, int y_row_stride,
												   byte[] uv_plane, int uv_offset, int uv_row_stride,
												   int width, int height, int is_vertical_flip,  int is_horizontal_flip,
												   int scale_width,  int scale_height, int scale_filter_mode,
												   int rotation_degree);


	/**
	 * 投递层NV12图像, 详细说明请参考PostLayerImageNV21ByteBuffer
	 *
	 * @return {0} if successful
	 */
	public native int PostLayerImageNV12ByteBuffer(long handle, int index, int left, int top,
												   ByteBuffer y_plane, int y_offset, int y_row_stride,
												   ByteBuffer uv_plane, int uv_offset, int uv_row_stride,
												   int width, int height, int is_vertical_flip,  int is_horizontal_flip,
												   int scale_width,  int scale_height, int scale_filter_mode,
												   int rotation_degree);


	/**
	 * 投递层NV12图像, 详细说明请参考PostLayerImageNV21ByteBuffer
	 *
	 * @return {0} if successful
	 */
	public native int PostLayerImageNV12ByteArray(long handle, int index, int left, int top,
												  byte[] y_plane, int y_offset, int y_row_stride,
												  byte[] uv_plane, int uv_offset, int uv_row_stride,
												  int width, int height, int is_vertical_flip,  int is_horizontal_flip,
												  int scale_width,  int scale_height, int scale_filter_mode,
												  int rotation_degree);

RGBA8888, RGBX8888 interface

  /**
	 * 投递层RGBA8888图像,如果不需要Aplpha通道的话, 请使用RGBX8888接口, 效率高
	 *
	 * @param index: 层索引, 必须大于等于0, 注意:如果index是0的话,将忽略Alpha通道
	 *
	 * @param left: 层叠加的左上角坐标, 对于第0层的话传0
	 *
	 * @param top: 层叠加的左上角坐标, 对于第0层的话传0
	 *
	 * @param rgba_plane: rgba 图像数据
	 *
	 * @param offset: 图像偏移, 这个主要目的是用来做clip的, 一般传0
	 *
	 * @param row_stride: stride information
	 *
	 * @param width: width, 必须大于1, 如果是奇数, 将减1
	 *
	 * @param height: height, 必须大于1, 如果是奇数, 将减1
	 *
	 * @param  is_vertical_flip: 是否垂直翻转, 0不翻转, 1翻转
	 *
	 * @param  is_horizontal_flip:是否水平翻转, 0不翻转, 1翻转
	 *
	 * @param  scale_width: 缩放宽,必须是偶数, 0或负数不缩放
	 *
	 * @param  scale_height: 缩放高, 必须是偶数, 0或负数不缩放
	 *
	 * @param  scale_filter_mode: 缩放质量, 传0使用默认速度,可选等级范围是:[1,3],值越大缩放质量越好, 但速度越慢
	 *
	 * @param  rotation_degree: 顺时针旋转, 必须是0, 90, 180, 270, 注意:旋转是在缩放, 垂直/水品反转之后再做, 请留意顺序
	 *
	 * @return {0} if successful
	 */
	public native int PostLayerImageRGBA8888ByteBuffer(long handle, int index, int left, int top,
											 ByteBuffer rgba_plane, int offset, int row_stride, int width, int height,
											 int is_vertical_flip,  int is_horizontal_flip,
											 int scale_width,  int scale_height, int scale_filter_mode,
											 int rotation_degree);


	/**
	 * 投递层RGBA8888图像, 详细说明请参考PostLayerImageRGBA8888ByteBuffer
	 *
	 * @return {0} if successful
	 */
	public native int PostLayerImageRGBA8888ByteArray(long handle, int index, int left, int top,
													  byte[] rgba_plane, int offset, int row_stride, int width, int height,
													  int is_vertical_flip,  int is_horizontal_flip,
													  int scale_width,  int scale_height, int scale_filter_mode,
													  int rotation_degree);


	/**
	 * 投递层RGBA8888图像, 详细说明请参考PostLayerImageRGBA8888ByteBuffer
	 *
	 * @return {0} if successful
	 */
	public native int PostLayerImageRGBA8888Native(long handle, int index, int left, int top,
												   long rgba_plane, int offset, int row_stride, int width, int height,
												   int is_vertical_flip,  int is_horizontal_flip,
												   int scale_width,  int scale_height, int scale_filter_mode,
												   int rotation_degree);


	/**
	 * 投递层RGBX8888图像
	 *
	 * @param index: 层索引, 必须大于等于0
	 *
	 * @param left: 层叠加的左上角坐标, 对于第0层的话传0
	 *
	 * @param top: 层叠加的左上角坐标, 对于第0层的话传0
	 *
	 * @param rgbx_plane: rgbx 图像数据
	 *
	 * @param offset: 图像偏移, 这个主要目的是用来做clip的,一般传0
	 *
	 * @param row_stride: stride information
	 *
	 * @param width: width, 必须大于1, 如果是奇数, 将减1
	 *
	 * @param height: height, 必须大于1, 如果是奇数, 将减1
	 *
	 * @param  is_vertical_flip: 是否垂直翻转, 0不翻转, 1翻转
	 *
	 * @param  is_horizontal_flip:是否水平翻转, 0不翻转, 1翻转
	 *
	 * @param  scale_width: 缩放宽,必须是偶数, 0或负数不缩放
	 *
	 * @param  scale_height: 缩放高, 必须是偶数, 0或负数不缩放
	 *
	 * @param  scale_filter_mode: 缩放质量, 传0使用默认速度,可选等级范围是:[1,3],值越大缩放质量越好, 但速度越慢
	 *
	 * @param  rotation_degree: 顺时针旋转, 必须是0, 90, 180, 270, 注意:旋转是在缩放, 垂直/水品反转之后再做, 请留意顺序
	 *
	 * @return {0} if successful
	 */
	public native int PostLayerImageRGBX8888ByteBuffer(long handle, int index, int left, int top,
													   ByteBuffer rgbx_plane, int offset, int row_stride, int width, int height,
													   int is_vertical_flip,  int is_horizontal_flip,
													   int scale_width,  int scale_height, int scale_filter_mode,
													   int rotation_degree);


	/**
	 * 投递层RGBX8888图像, 详细说明请参考PostLayerImageRGBX8888ByteBuffer
	 *
	 * @return {0} if successful
	 */
	public native int PostLayerImageRGBX8888ByteArray(long handle, int index, int left, int top,
													  byte[] rgbx_plane, int offset, int row_stride, int width, int height,
													  int is_vertical_flip,  int is_horizontal_flip,
													  int scale_width,  int scale_height, int scale_filter_mode,
													  int rotation_degree);


	/**
	 * 投递层RGBX8888图像, 详细说明请参考PostLayerImageRGBX8888ByteBuffer
	 *
	 * @return {0} if successful
	 */
	public native int PostLayerImageRGBX8888Native(long handle, int index, int left, int top,
												   long rgbx_plane, int offset, int row_stride, int width, int height,
												   int is_vertical_flip,  int is_horizontal_flip,
												   int scale_width,  int scale_height, int scale_filter_mode,
												   int rotation_degree);


	/**
	 * 投递层RGB888图像
	 *
	 * @param index: 层索引, 必须大于等于0
	 *
	 * @param left: 层叠加的左上角坐标, 对于第0层的话传0
	 *
	 * @param top: 层叠加的左上角坐标, 对于第0层的话传0
	 *
	 * @param rgb_plane: rgb888 图像数据
	 *
	 * @param offset: 图像偏移, 这个主要目的是用来做clip的,一般传0
	 *
	 * @param row_stride: stride information
	 *
	 * @param width: width, 必须大于1, 如果是奇数, 将减1
	 *
	 * @param height: height, 必须大于1, 如果是奇数, 将减1
	 *
	 * @param  is_vertical_flip: 是否垂直翻转, 0不翻转, 1翻转
	 *
	 * @param  is_horizontal_flip:是否水平翻转, 0不翻转, 1翻转
	 *
	 * @param  scale_width: 缩放宽,必须是偶数, 0或负数不缩放
	 *
	 * @param  scale_height: 缩放高, 必须是偶数, 0或负数不缩放
	 *
	 * @param  scale_filter_mode: 缩放质量, 传0使用默认速度,可选等级范围是:[1,3],值越大缩放质量越好, 但速度越慢
	 *
	 * @param  rotation_degree: 顺时针旋转, 必须是0, 90, 180, 270, 注意:旋转是在缩放, 垂直/水品反转之后再做, 请留意顺序
	 *
	 * @return {0} if successful
	 */
	public native int PostLayerImageRGB888Native(long handle, int index, int left, int top,
													   long rgb_plane, int offset, int row_stride, int width, int height,
													   int is_vertical_flip,  int is_horizontal_flip,
													   int scale_width,  int scale_height, int scale_filter_mode,
													   int rotation_degree);

Technical summary

The above briefly introduces the commonly used type differences and interface design of color coding formats, which basically covers all types that may be used. If it is encoded H.264 and H.265 data, we have also made relevant designs, whether it is Both the built-in data types and third-party external data docking (such as data collected by Unity) can be easily connected.

Guess you like

Origin blog.csdn.net/renhui1112/article/details/132613476