本文主要是介绍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加速的示例代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!