前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >NV21、NV12、YV12、RGB565、YUV等颜色编码格式区别和接口设计探讨

NV21、NV12、YV12、RGB565、YUV等颜色编码格式区别和接口设计探讨

原创
作者头像
音视频牛哥
发布2023-09-01 00:00:16
7760
发布2023-09-01 00:00:16
举报

?NV21、NV12、YV12、RGB565、YUV扫盲

NV21、NV12、YV12、RGB565、YUV分别是不同的颜色编码格式,这些颜色编码格式各有特点,适用于不同的应用场景。选择合适的颜色编码格式取决于具体的需求和环境:

  1. NV21:NV21是一种用于Android系统的图像颜色编码格式。它使用YUV 4:2:0的采样方式,即垂直方向上每两个像素采样一次,水平方向上每个像素采样两次。NV21的Y分量是亮度信息,V和U分量是色度信息(分别代表饱和度和色调)。这种格式主要应用于前置摄像头和Android的Camera API。
  2. NV12:NV12是一种用于视频编解码的颜色编码格式,同样采用了YUV 4:2:0的采样方式。NV12的Y分量是亮度信息,V和U分量也是色度信息。不同的是,与NV21不同的是,NV12的Y、V、U三个分量分别采用了不同的采样率,即垂直方向上每两个像素采样一次,水平方向上每隔一个像素采样一次。
  3. YV12:YV12是一种用于视频编解码的颜色编码格式。它同样采用了YUV 4:2:0的采样方式。YV12的Y分量是亮度信息,V和U分量也是色度信息。与NV12不同的是,YV12的V和U分量交换了位置。这种格式主要应用于软件编解码器,如FFmpeg。
  4. RGB565:RGB565是一种颜色编码格式,它有3个通道,分别是红色、绿色和蓝色,由这三个通道的强度值共同决定一个颜色。在RGB565中,每个通道的精度为5位(红色)、6位(绿色)和5位(蓝色)。因此,RGB565能够表示的颜色数量有限。
  5. YUV:YUV是一种将亮度信息和色度信息分开的颜色编码格式。在YUV格式中,Y是亮度分量,而UV是色度分量。UV分量又进一步分为U和V,分别代表饱和度和色调。这种格式主要用于优化彩色视频信号的传输,因为可以对亮度信息进行更高效的压缩。

如何对接上述颜色编码格式

大牛直播SDK在做Android平台RTMP推送、轻量级RTSP服务和GB28181设备接入模块的时候,对接过上述的颜色编码格式,下面分别探讨下不同格式设计的数据接口。

YV12的数据接口

YV12的数据接口,主要是用于第三方的设备对接居多,这个接口的u_stride, v_stride分别是(width+1)/2,如果出来的数据需要旋转,通过rotation_degree来控制旋转角度即可。

代码语言:java
复制
    /**
     * 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数据接口

支持标准的I420数据接口对接,不再赘述:

代码语言:java
复制
  /**
	 * 投递层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转I420并旋转接口

这个接口也是主要用于特定的数据类型对接,NV21的数据,直接转I420后,对接即可,接口参数比较简单,不再赘述。

代码语言:java
复制
  /**
	 * 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);

支持RGBA数据接入

RGBA的主要用于屏幕共享场景下。

代码语言:java
复制
   /**
    * 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);

支持RGB565数据接入(主要用于同屏场景)

RGB565数据类型也主要用于屏幕采集这块。

代码语言:java
复制
    /**
     * 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格式

代码语言:java
复制
  /**
	 * 投递层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接口

代码语言:java
复制
  /**
	 * 投递层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);

技术总结

上面大概介绍了颜色编码格式常用的类型区别和接口设计,基本上涵盖了可能用到的所有类型,如果是编码后的H.264、H.265数据,我们也做了相关的设计,不管是自带的数据类型还是第三方外部数据对接(如Unity采集的数据),都可以很容易对接进来。

?

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ?NV21、NV12、YV12、RGB565、YUV扫盲
  • 如何对接上述颜色编码格式
    • YV12的数据接口
      • YUV数据接口
        • NV21转I420并旋转接口
          • 支持RGBA数据接入
            • 支持RGB565数据接入(主要用于同屏场景)
              • NV12、NV21格式
                • RGBA8888、RGBX8888接口
                • 技术总结
                相关产品与服务
                快直播
                快直播(Live Event Broadcasting,LEB)(超低延时直播)是标准直播在超低延时播放场景下的延伸,比传统直播协议延迟更低,为观众提供毫秒级的极致直播观看体验。 快直播能够满足对延时性能要求更高的场景需求,例如电商直播、赛事直播、在线教育、在线答题等。
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
                http://www.vxiaotou.com