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

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

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设备接入模块的时候,对接过上述的颜色编码格式,下面分别探讨下不同格式设计的数据接口。

4899d1dd3d5946acaf3d9c13fd054b7f.jpg

YV12的数据接口

YV12的数据接口,主要是用于第三方的设备对接居多,这个接口的u_stride, v_stride分别是(width+1)/2,如果出来的数据需要旋转,通过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数据接口

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

  /**
   * 投递层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后,对接即可,接口参数比较简单,不再赘述。

  /**
   * 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的主要用于屏幕共享场景下。

   /**
    * 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数据类型也主要用于屏幕采集这块。

    /**
     * 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格式

  /**
   * 投递层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接口

  /**
   * 投递层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采集的数据),都可以很容易对接进来。

相关文章
|
7月前
|
存储 编解码 Android开发
NV21、NV12、YV12、RGB、YUV、RGBA、RGBX8888等图像色彩编码格式区别
NV21、NV12、YV12、RGB、YUV、RGBA、RGBX8888都是常见的图像颜色编码格式,它们之间的主要区别在于色彩空间和数据排列方式。
135 0
|
3月前
|
存储 传感器 编解码
图像格式:常见图像格式RAW, RGB, YUV&&图像格式的解析、格式转换和看图软件
图像格式:常见图像格式RAW, RGB, YUV&&图像格式的解析、格式转换和看图软件
287 1
|
4月前
|
存储 编解码
解码失败会显示绿屏,及yuv和rgb简单理解
解码失败会显示绿屏,及yuv和rgb简单理解
54 0
FreeSwitch中,RGB颜色转换为YUV
FreeSwitch中,RGB颜色转换为YUV
71 0
YUV转RGB查表方式的代码
YUV转RGB查表方式的代码
97 0
|
存储 算法 知识图谱
RGB与YUV相互转换
本文介绍YUV跟RGB互转的各种公式,以及推导原理
258 0
RGB转换为NV12的代码
RGB转换为NV12的代码
184 0
|
编解码
理解 RGB & YUV 颜色编码
理解 RGB & YUV 颜色编码
335 0
理解 RGB & YUV 颜色编码
YUV 与 RGB的转换
RGB 转换成 YUV Y = (0.257 * R) + (0.504 * G) + (0.
4760 0
使用FFMPEG的sws_scale函数实现各种原始颜色格式互转(YUV\RGB\)
使用FFMPEG的sws_scale函数实现各种原始颜色格式互转(YUV\RGB\)
728 0