yuv420转yuv422使用neno加速的示例代码

2024-05-29 18:36

本文主要是介绍yuv420转yuv422使用neno加速的示例代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、使用none实现yuv420快速转换为yuv422

#include <arm_neon.h>

static vs_int32_t yuv420_yuv422_conver(vs_uint8_t *p_yuv420_y, vs_uint8_t *p_yuv420_uv, vs_uint32_t yuv420_y_stride, vs_uint32_t yuv420_uv_stride, vs_int32_t width, vs_int32_t height, vs_uint8_t* p_yuv422_virt_addr)
{
    vs_int32_t w = 0, h = 0, remain = 0;
    vs_uint8_t* p_yuv422_out = NULL;
    uint8x8_t u8rslt;
    uint8x8x2_t u8yuvdata;
    uint8x8x2_t u8trans_idx;
    vs_int32_t p_yuv422_stride = (width << 1);
    const vs_uint8_t data_idx[2][8] = { { 0, 8, 1, 9, 2, 10, 3, 11 },
                                         { 4, 12, 5, 13, 6, 14, 7, 15 } };

    //width must be even
    if (width % 2 != 0) {
        VS_UVC_LOG_E("width[%u] must be even!\n", width);
        return VS_FAILED;
    }

    p_yuv422_out = p_yuv422_virt_addr;
    u8trans_idx.val[0] = vld1_u8(data_idx[0]);
    u8trans_idx.val[1] = vld1_u8(data_idx[1]);

    h = height;
    for (; h > 0; h--) {

        w = (width >> 3);
        remain = width - (w << 3);
        for (; w > 0; w--) {

            u8yuvdata.val[0] = vld1_u8(p_yuv420_y);
            u8yuvdata.val[1] = vld1_u8(p_yuv420_uv);
            u8rslt = vtbl2_u8(u8yuvdata, u8trans_idx.val[0]);
            vst1_u8(p_yuv422_out, u8rslt);

            u8rslt = vtbl2_u8(u8yuvdata, u8trans_idx.val[1]);

            vst1_u8(p_yuv422_out + 8, u8rslt);
            p_yuv420_y += 8;
            p_yuv420_uv += 8;
            p_yuv422_out += 16;
        }

        for (; remain > 0; remain -= 2) {

            p_yuv422_out[0] = p_yuv420_y[0];
            p_yuv422_out[1] = p_yuv420_uv[1];
            p_yuv422_out[2] = p_yuv420_y[1];
            p_yuv422_out[3] = p_yuv420_uv[0];
            p_yuv420_y += 2;
            p_yuv420_uv += 2;
            p_yuv422_out += 4;
        }

        if (h & 1) {
            p_yuv420_uv += (yuv420_uv_stride - width);
        } else {
            p_yuv420_uv -= width;
        }
        p_yuv420_y += (yuv420_y_stride - width);
        p_yuv422_out += (p_yuv422_stride - (width << 1));
    }

    return VS_SUCCESS;
}

static vs_int32_t frame_yuv420_yuv422_conver(vs_video_frame_s* p_yuv420_frame, vs_uint8_t* p_yuv422_virt_addr, vs_int32_t yuv422_w, vs_int32_t yuv422_h, FILE *p_yuv420_file)
{
    vs_int32_t ret = VS_SUCCESS;
    vs_uint32_t yuv420_frame_size = 0;
    vs_uint8_t *p_yuv420_virt_addr = NULL, *p_yuv420_y = NULL, *p_yuv420_uv = NULL;


    if(p_yuv420_frame->width != yuv422_w || p_yuv420_frame->height != yuv422_h) {
        VS_UVC_LOG_E("p_yuv420_frame->width != yuv422_w\n");
        return VS_FAILED;
    }

    yuv420_frame_size = p_yuv420_frame->stride[0] * p_yuv420_frame->height * 3 / 2;
    VS_UVC_LOG_I("===========p_yuv420_frame->stride[0] = %d =============\n", p_yuv420_frame->stride[0]);
    VS_UVC_LOG_I("===========p_yuv420_frame->stride[1] = %d =============\n", p_yuv420_frame->stride[1]);
    p_yuv420_virt_addr = (vs_uint8_t*)vs_mal_sys_mmap_cached(p_yuv420_frame->phys_addr[0], yuv420_frame_size);
    if (!p_yuv420_virt_addr) {
        VS_UVC_LOG_E("vs_mal_sys_mmap_cached failed\n");
        return VS_FAILED;
    }


    p_yuv420_y = p_yuv420_virt_addr;
    p_yuv420_uv = p_yuv420_y + p_yuv420_frame->stride[0] * p_yuv420_frame->height;

    ret = yuv420_yuv422_conver(p_yuv420_y, p_yuv420_uv, p_yuv420_frame->stride[0], p_yuv420_frame->stride[1], yuv422_w, yuv422_h, p_yuv422_virt_addr);
    if (ret != VS_SUCCESS) {
        VS_UVC_LOG_E("yuv420_2_yuv422_conver failed\n");
        vs_mal_sys_unmap(p_yuv420_virt_addr, yuv420_frame_size);
        return ret;
    }

    fwrite(p_yuv420_virt_addr, 1, yuv420_frame_size, p_yuv420_file);
    fflush(p_yuv420_file);

    vs_mal_sys_unmap(p_yuv420_virt_addr, yuv420_frame_size);

    return VS_SUCCESS;
}

static vs_int32_t frame_yuv420_witre_2_yuv422file(vs_video_frame_s* p_yuv420_frame)
{
    vs_int32_t ret = VS_SUCCESS;
    static vs_int32_t savefile_index = 0;
    vs_uint32_t yuv422_frame_size = 0;
    vs_uint8_t* p_yuv422_virt_addr = VS_NULL;
    FILE *p_yuv420_file = VS_NULL, *p_yuv422_file = VS_NULL;
    vs_char_t yuv420_file_name[64], yuv422_file_name[64];

    //yuv420_frame_size = p_yuv420_frame->stride[0] * p_yuv420_frame->height * 3 / 2;
    yuv422_frame_size = p_yuv420_frame->stride[0] * p_yuv420_frame->height * 2;
    p_yuv422_virt_addr = malloc(yuv422_frame_size);
    if (p_yuv422_virt_addr == VS_NULL) {
        VS_UVC_LOG_E("malloc size[%u] failed!\n", yuv422_frame_size);
        return VS_FAILED;
    }

    memset(yuv420_file_name, 0, sizeof(yuv420_file_name));
    memset(yuv422_file_name, 0, sizeof(yuv422_file_name));
    sprintf((char *)yuv420_file_name, "yuv420_idx%d.yuv", savefile_index);
    sprintf((char *)yuv422_file_name, "yuv422_idx%d.yuv", savefile_index);
    savefile_index++;
    p_yuv420_file = fopen((char *)yuv420_file_name, "wb");
    if (VS_NULL == p_yuv420_file) {
        VS_UVC_LOG_E("fopen file[%s] failed\n", yuv420_file_name);
        free(p_yuv422_virt_addr);
        return VS_FAILED;
    }
    p_yuv422_file = fopen((char *)yuv422_file_name, "wb");
    if (VS_NULL == p_yuv422_file) {
        VS_UVC_LOG_E("fopen file[%s] failed\n", yuv422_file_name);
        free(p_yuv422_virt_addr);
        fclose(p_yuv420_file);
        return VS_FAILED;
    }

    ret = frame_yuv420_yuv422_conver(p_yuv420_frame, p_yuv422_virt_addr, p_yuv420_frame->width, p_yuv420_frame->height, p_yuv420_file);
    if (ret != VS_SUCCESS) {
        VS_UVC_LOG_E("yuv420_2_yuv422_conver failed\n");
        free(p_yuv422_virt_addr);
        fclose(p_yuv420_file);
        fclose(p_yuv422_file);
        return ret;
    }

    fwrite(p_yuv422_virt_addr, 1, yuv422_frame_size, p_yuv422_file);
    fflush(p_yuv422_file);

    free(p_yuv422_virt_addr);
    fclose(p_yuv420_file);
    fclose(p_yuv422_file);

    return VS_SUCCESS;
}

二、使用none实现yuv420快速转换为nv12

static vs_int32_t yuv420_nv12_conver(vs_uint8_t* p_yuv420_virt_addr, vs_uint32_t yuv420_y_stride, vs_uint32_t yuv420_uv_stride, vs_int32_t width, vs_int32_t height, vs_uint8_t* p_nv12_virt_addr)
{
    vs_int32_t w, h;
    vs_uint8_t* p_yuv420_in = p_yuv420_virt_addr, *p_nv12_out = p_nv12_virt_addr;
    vs_int32_t remain = 0;
    uint8x16_t u8data;

    //width must be even
    if (width % 2 != 0) {
        VS_UVC_LOG_E("width[%u] must be even!\n", width);
        return VS_FAILED;
    }

    // for y
    h = height;
    for (; h > 0; h--) {
        w = (width >> 4);
        remain = width % 16;
        for (; w > 0; w--) {
            u8data = vld1q_u8(p_yuv420_in);
            vst1q_u8(p_nv12_out, u8data);
            p_nv12_out += 16;
            p_yuv420_in += 16;
        }
        for (; remain > 0; remain--) {
            *p_nv12_out = *p_yuv420_in;
            p_nv12_out++;
            p_yuv420_in++;
        }
        p_yuv420_in += (yuv420_y_stride - width);
    }

    // for yv
    h = (height >> 1);
    for (; h > 0; h--) {
        w = (width >> 4);
        remain = width % 16;
        for (; w > 0; w--) {
            u8data = vld1q_u8(p_yuv420_in);
            //u8data = vrev16q_u8(u8data);
            vst1q_u8(p_nv12_out, u8data);
            p_nv12_out += 16;
            p_yuv420_in += 16;
        }

        for (; remain > 0; remain -= 2) {
            p_nv12_out[0] = p_yuv420_in[0];
            p_nv12_out[1] = p_yuv420_in[1];
            p_nv12_out += 2;
            p_yuv420_in += 2;
        }
        p_yuv420_in += (yuv420_uv_stride - width);
    }

    return  VS_SUCCESS;
}

static vs_int32_t frame_yuv420_nv12_conver(vs_video_frame_s* p_yuv420_frame, vs_uint8_t* p_nv12_virt_addr,vs_int32_t nv12_w,vs_int32_t nv12_h, FILE *p_yuv420_file)
{
    vs_int32_t ret = VS_SUCCESS;
    vs_uint32_t yuv420_frame_size = 0;
    vs_uint8_t *p_yuv420_virt_addr = NULL;

    if (p_yuv420_frame->width != nv12_w || p_yuv420_frame->height != nv12_h) {
        VS_UVC_LOG_E("p_yuv420_frame->width != nv12_w\n");
        return 0;
    }

    yuv420_frame_size = p_yuv420_frame->stride[0] * p_yuv420_frame->height * 3 / 2;
    VS_UVC_LOG_I("===========p_yuv420_frame->stride[0] = %d =============\n", p_yuv420_frame->stride[0]);
    VS_UVC_LOG_I("===========p_yuv420_frame->stride[1] = %d =============\n", p_yuv420_frame->stride[1]);
    p_yuv420_virt_addr = (vs_uint8_t*)vs_mal_sys_mmap_cached(p_yuv420_frame->phys_addr[0], yuv420_frame_size);
    if (!p_yuv420_virt_addr) {
        VS_UVC_LOG_E(" vs_mal_sys_mmap_cached failed\n");
        return 0;
    }

    ret = yuv420_nv12_conver(p_yuv420_virt_addr, p_yuv420_frame->stride[0], p_yuv420_frame->stride[1], nv12_w, nv12_h, p_nv12_virt_addr);
    if (ret != VS_SUCCESS) {
        VS_UVC_LOG_E("yuv420_2_yuv422_conver failed\n");
        vs_mal_sys_unmap(p_yuv420_virt_addr, yuv420_frame_size);
        return ret;
    }

    fwrite(p_yuv420_virt_addr, 1, yuv420_frame_size, p_yuv420_file);
    fflush(p_yuv420_file);

    vs_mal_sys_unmap(p_yuv420_virt_addr, yuv420_frame_size);

    return VS_SUCCESS;
}

static vs_int32_t frame_yuv420_witre_2_nv12file(vs_video_frame_s* p_yuv420_frame)
{
    vs_int32_t ret = VS_SUCCESS;
    static vs_int32_t savefile_index = 0;
    vs_uint32_t nv12_frame_size = 0;
    vs_uint8_t* p_yuv422_virt_addr = VS_NULL;
    FILE *p_yuv420_file = VS_NULL, *p_nv12_file = VS_NULL;
    vs_char_t yuv420_file_name[64], nv12_file_name[64];

    //yuv420_frame_size = p_yuv420_frame->stride[0] * p_yuv420_frame->height * 3 / 2;
    nv12_frame_size = p_yuv420_frame->stride[0] * p_yuv420_frame->height * 3 / 2;
    p_yuv422_virt_addr = malloc(nv12_frame_size);
    if (p_yuv422_virt_addr == VS_NULL) {
        VS_UVC_LOG_E("malloc size[%u] failed!\n", nv12_frame_size);
        return VS_FAILED;
    }

    memset(yuv420_file_name, 0, sizeof(yuv420_file_name));
    memset(nv12_file_name, 0, sizeof(nv12_file_name));
    sprintf((char *)yuv420_file_name, "yuv420_idx%d.yuv", savefile_index);
    sprintf((char *)nv12_file_name, "nv12_idx%d.yuv", savefile_index);
    savefile_index++;
    p_yuv420_file = fopen((char *)yuv420_file_name, "wb");
    if (VS_NULL == p_yuv420_file) {
        VS_UVC_LOG_E("fopen file[%s] failed\n", yuv420_file_name);
        free(p_yuv422_virt_addr);
        return VS_FAILED;
    }
    p_nv12_file = fopen((char *)nv12_file_name, "wb");
    if (VS_NULL == p_nv12_file) {
        VS_UVC_LOG_E("fopen file[%s] failed\n", nv12_file_name);
        free(p_yuv422_virt_addr);
        fclose(p_yuv420_file);
        return VS_FAILED;
    }

    ret = frame_yuv420_nv12_conver(p_yuv420_frame, p_yuv422_virt_addr, p_yuv420_frame->width, p_yuv420_frame->height, p_yuv420_file);
    if (ret != VS_SUCCESS) {
        VS_UVC_LOG_E("yuv420_2_yuv422_conver failed\n");
        free(p_yuv422_virt_addr);
        fclose(p_yuv420_file);
        fclose(p_nv12_file);
        return ret;
    }

    fwrite(p_yuv422_virt_addr, 1, nv12_frame_size, p_nv12_file);
    fflush(p_nv12_file);

    free(p_yuv422_virt_addr);
    fclose(p_yuv420_file);
    fclose(p_nv12_file);

    return VS_SUCCESS;
}
 

这篇关于yuv420转yuv422使用neno加速的示例代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1014316

相关文章

java图像识别工具类(ImageRecognitionUtils)使用实例详解

《java图像识别工具类(ImageRecognitionUtils)使用实例详解》:本文主要介绍如何在Java中使用OpenCV进行图像识别,包括图像加载、预处理、分类、人脸检测和特征提取等步骤... 目录前言1. 图像识别的背景与作用2. 设计目标3. 项目依赖4. 设计与实现 ImageRecogni

python管理工具之conda安装部署及使用详解

《python管理工具之conda安装部署及使用详解》这篇文章详细介绍了如何安装和使用conda来管理Python环境,它涵盖了从安装部署、镜像源配置到具体的conda使用方法,包括创建、激活、安装包... 目录pytpshheraerUhon管理工具:conda部署+使用一、安装部署1、 下载2、 安装3

Mysql虚拟列的使用场景

《Mysql虚拟列的使用场景》MySQL虚拟列是一种在查询时动态生成的特殊列,它不占用存储空间,可以提高查询效率和数据处理便利性,本文给大家介绍Mysql虚拟列的相关知识,感兴趣的朋友一起看看吧... 目录1. 介绍mysql虚拟列1.1 定义和作用1.2 虚拟列与普通列的区别2. MySQL虚拟列的类型2

使用MongoDB进行数据存储的操作流程

《使用MongoDB进行数据存储的操作流程》在现代应用开发中,数据存储是一个至关重要的部分,随着数据量的增大和复杂性的增加,传统的关系型数据库有时难以应对高并发和大数据量的处理需求,MongoDB作为... 目录什么是MongoDB?MongoDB的优势使用MongoDB进行数据存储1. 安装MongoDB

关于@MapperScan和@ComponentScan的使用问题

《关于@MapperScan和@ComponentScan的使用问题》文章介绍了在使用`@MapperScan`和`@ComponentScan`时可能会遇到的包扫描冲突问题,并提供了解决方法,同时,... 目录@MapperScan和@ComponentScan的使用问题报错如下原因解决办法课外拓展总结@

mysql数据库分区的使用

《mysql数据库分区的使用》MySQL分区技术通过将大表分割成多个较小片段,提高查询性能、管理效率和数据存储效率,本文就来介绍一下mysql数据库分区的使用,感兴趣的可以了解一下... 目录【一】分区的基本概念【1】物理存储与逻辑分割【2】查询性能提升【3】数据管理与维护【4】扩展性与并行处理【二】分区的

使用Python实现在Word中添加或删除超链接

《使用Python实现在Word中添加或删除超链接》在Word文档中,超链接是一种将文本或图像连接到其他文档、网页或同一文档中不同部分的功能,本文将为大家介绍一下Python如何实现在Word中添加或... 在Word文档中,超链接是一种将文本或图像连接到其他文档、网页或同一文档中不同部分的功能。通过添加超

Linux使用fdisk进行磁盘的相关操作

《Linux使用fdisk进行磁盘的相关操作》fdisk命令是Linux中用于管理磁盘分区的强大文本实用程序,这篇文章主要为大家详细介绍了如何使用fdisk进行磁盘的相关操作,需要的可以了解下... 目录简介基本语法示例用法列出所有分区查看指定磁盘的区分管理指定的磁盘进入交互式模式创建一个新的分区删除一个存

C#使用HttpClient进行Post请求出现超时问题的解决及优化

《C#使用HttpClient进行Post请求出现超时问题的解决及优化》最近我的控制台程序发现有时候总是出现请求超时等问题,通常好几分钟最多只有3-4个请求,在使用apipost发现并发10个5分钟也... 目录优化结论单例HttpClient连接池耗尽和并发并发异步最终优化后优化结论我直接上优化结论吧,

SpringBoot使用Apache Tika检测敏感信息

《SpringBoot使用ApacheTika检测敏感信息》ApacheTika是一个功能强大的内容分析工具,它能够从多种文件格式中提取文本、元数据以及其他结构化信息,下面我们来看看如何使用Ap... 目录Tika 主要特性1. 多格式支持2. 自动文件类型检测3. 文本和元数据提取4. 支持 OCR(光学