• 简单YUV数据转换


    YUV是一种亮度信号Y和色度信号U、V是分离的色彩空间,它主要用于优化彩色视频信号的传输,使其向后相容老式黑白电视。其中“Y”表示明亮度(Luminance或Luma),也就是灰阶值;而“U”和“V”表示的则是色度(Chrominance或Chroma),作用是描述影像色彩及饱和度,用于指定像素的颜色。 YUV格式分为两种类型:Packed类型和Planar类型。 

    1、Packed类型:是将YUV分量存在在同一个数组中,每个像素点的Y、U、V是连续交错存储的;

    2、Planar类型:是将YUV分量分别存放到三个独立的数组中,且先连续存储所有像素点的Y,紧接着存储所有像素点的U,最后是所有像素点的V。

    一、将NV21转Yuv420p

    1. // YYYYYYYY VUVU --> YYYYYYYY UVUV
    2. // 将NV21转换为Yuv420sp
    3. public static byte[] nv21ToYuv420sp(byte[] src, int width, int height) {
    4. int yLength = width * height;
    5. int uLength = yLength / 4;
    6. int vLength = yLength / 4;
    7. int frameSize = yLength + uLength + vLength;
    8. byte[] yuv420sp = new byte[frameSize];
    9. // Y分量
    10. System.arraycopy(src, 0, yuv420sp, 0, yLength);
    11. for (int i = 0; i < yLength/4; i++) {
    12. // U分量
    13. yuv420sp[yLength + 2 * i] = src[yLength + 2*i+1];
    14. // V分量
    15. yuv420sp[yLength + 2*i+1] = src[yLength + 2*i];
    16. }
    17. return yuv420sp;
    18. }

    二、将YUV420SemiPlanner转换为NV21

    1. // YYYYYYYY UVUV(yuv420sp)--> YYYYYYYY VUVU(nv21)
    2. // 将YUV420SemiPlanner转换为NV21
    3. public static byte[] yuv420spToNV21(byte[] src, int width, int height) {
    4. int yLength = width * height;
    5. int uLength = yLength / 4;
    6. int vLength = yLength / 4;
    7. int frameSize = yLength + uLength + vLength;
    8. byte[] nv21 = new byte[frameSize];
    9. // Y分量
    10. System.arraycopy(src, 0, nv21, 0, yLength);
    11. for (int i = 0; i < yLength/4; i++) {
    12. // U分量
    13. nv21[yLength + 2*i +1] = src[yLength+2*i];
    14. // V分量
    15. nv21[yLength + 2*i] = src[yLength + 2*i + 1];
    16. }
    17. return nv21;
    18. }

     三、将YUV420Planner(I420)转换为NV21

    1. // YYYYYYYY UU VV --> YYYYYYYY VUVU
    2. // 将YUV420Planner(I420)转换为NV21
    3. public static byte[] yuv420pToNV21(byte[] src, int width, int height) {
    4. int yLength = width * height;
    5. int uLength = yLength / 4;
    6. int vLength = yLength / 4;
    7. int frameSize = yLength + uLength + vLength;
    8. byte[] nv21 = new byte[frameSize];
    9. System.arraycopy(src, 0, nv21, 0, yLength); // Y分量
    10. for (int i = 0; i < yLength / 4; i++) {
    11. // U分量
    12. nv21[yLength + 2*i + 1] = src[yLength + i];
    13. // V分量
    14. nv21[yLength + 2*i] = src[yLength + uLength + i];
    15. }
    16. return nv21;
    17. }

    四、将nv21转换为yuv420p(I420)

    1. // YYYYYYYY VUVU ---> YYYYYYYY UU VV
    2. //
    3. public static byte[] nv21ToYuv420p(byte[] src, int width, int height) {
    4. int yLength = width * height;
    5. int uLength = yLength / 4;
    6. int vLength = yLength / 4;
    7. int frameSize = yLength + uLength + vLength;
    8. byte[] yuv420p = new byte[frameSize];
    9. // Y分量
    10. System.arraycopy(src, 0, yuv420p, 0, yLength);
    11. for (int i = 0; i < yLength/4; i++) {
    12. // U分量
    13. yuv420p[yLength + i] = src[yLength + 2*i + 1];
    14. // V分量
    15. yuv420p[yLength + uLength + i] = src[yLength + 2*i];
    16. }
    17. return yuv420p;
    18. }

     五、将YV12转换为NV21

    1. // YYYYYYYY VV UU --> YYYYYYYY VUVU
    2. // 将YV12转换为NV21
    3. public static byte[] yv12ToNV21(byte[] src, int width, int height) {
    4. int yLength = width * height;
    5. int uLength = yLength / 4;
    6. int vLength = yLength / 4;
    7. int frameSize = yLength + uLength + vLength;
    8. byte[] nv21 = new byte[frameSize];
    9. System.arraycopy(src, 0, nv21, 0, yLength); // Y分量
    10. for (int i = 0; i < yLength / 4; i++) {
    11. // U分量
    12. nv21[yLength + 2*i + 1] = src[yLength + vLength + i];
    13. // V分量
    14. nv21[yLength + 2*i] = src[yLength + i];
    15. }
    16. return nv21;
    17. }

    五种数据互相转换。

                                                                                         -END

  • 相关阅读:
    vue深入响应式原理
    Java 18 新特性:简单Web服务器 jwebserver
    Worthington酶促细胞收获&细胞粘附和收获
    MCE | 丙型肝炎病毒的终结之路
    液压系统中比较常用的密封件是什么?
    怎样学好java
    2022年金九银十Java面试手册宝典,横扫春招+校招+社招+秋招
    最新红盟云卡个人自动发卡开源系统源码+全开源无加密+虚拟商品在线售卖平台
    神经网络分类任务
    普通本科大学生活避坑指南
  • 原文地址:https://blog.csdn.net/generallizhong/article/details/132756200