You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
void FrameFormatConvert::RGBtoNV12(byte* nv12, byte* rgb, int width, int height)
{
if(CheckIsSupportNeon())
{
#ifndef WIN32
LOGD("start neon nv12");
//rgb888_2_nv12_neon(nv12, rgb, width, height, width >> 4);
rgb888_2_nv12_intrinsic(nv12, rgb, width, height);
return;
#endif
}
int frameSize = width * height;
int yIndex = 0;
int uvIndex = frameSize;
int R, G, B, Y, U, V;
int index = 0;
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
//a = (argb[index] & 0xff000000) >> 24; // a is not used obviously
//R = (argb[index] & 0xff0000) >> 16;
//G = (argb[index] & 0xff00) >> 8;
//B = (argb[index] & 0xff) >> 0;
R = rgb[index];
index++;
G = rgb[index];
index++;
B = rgb[index];
index++;
// well known RGB to YUV algorithm
Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
// NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
// meaning for every 4 Y pixels there are 1 V and 1 U. Note the sampling is every other
// pixel AND every other scanline.
// NV12 YYYYYYYY UVUV
// NV21 YYYYYYYY VUVU
nv12[yIndex++] = (byte)((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
if (j % 2 == 0 && index % 2 == 0)
{
nv12[uvIndex++] = (byte)((U < 0) ? 0 : ((U > 255) ? 255 : U));
nv12[uvIndex++] = (byte)((V < 0) ? 0 : ((V > 255) ? 255 : V));
}
}
}
}
最近做H264编解码的工作,使用了Android平台MediaCodec,进行硬编解码,以减少CPU的压力。
然后发现适用性最强的格式是YUV420P和YUV420SP,绝大部分Android设备都可以支持这两种格式的H264输入。
(如何查看Android设备所支持格式,见我的上一篇文章《Android平台MediaCodec避坑指北》)
但是Android API level 23. 开始不被推荐,推荐COLOR_FormatYUV420Flexible.
那我们采集到视频帧数据如果是RGB888或者UYUV422,那就需要进行格式转换。
如果刚好用的NV21,NV12,或者YV12,I420有可能不用转换,也有可能需要转换。见《Android平台MediaCodec避坑指北》坑点3
先说一下开发环境,使用的是,Windows10 + AndroidStudio3.0 + CMake
NEON相关的学习资料 ,可以看这个
https://www.jianshu.com/p/16d60ac56249
https://blog.csdn.net/chshplp_liaoping/article/details/12752749
简单小例子:
http://hilbert-space.de/?p=22
翻译:
https://zhuanlan.zhihu.com/p/24702989
Neon支持检测
https://developer.android.com/ndk/guides/cpu-arm-neon.html
0、Neon指令
Neon指令可以在arm官网下到文档NEON Programmer’s Guide Version: 1.0
里面有许多特色长宽指令,8_8 变为 16_8,收窄指令 16_8 变为 8_8 ,饱和指令等等,
要多看看文档才能了然于胸。
1、使用Intrinsic_C还是汇编
Intrinsic_C用起来简单,还能跨armv7,armv8,每一个API接口都对应着相应的一条指令。由于充满了sign/unsign,int、short、char,8x8,16x8 ,加 减 乘,长宽,收窄,饱和,位移等等,在arm_neon.h里有几百个Api,但是殊途同归,大同小异,每了解1个汇编指令,就能通吃10~20个Api。
这里有份帮助文档:https://blog.csdn.net/fengbingchun/article/details/38085781
汇编代码就不能跨armv7,armv8了,但是真正懂得每个汇编指令的意思,才能优化到极致。
懂了汇编,反过来写Intrinsic_C也大有帮助。
2、开启Neon之旅
在你的CMakeLists.txt配置文件里,加入这两句,使用mk文件也一样。
(如果不需要的话可以不加)
也可以加在build.gradle里,AndroidStudio3.0就是方便。
3、CMake开启ASM
enable_language(ASM)
这句不加,你就不能使用.S汇编文件。
4、使用IntrinsicC库
非常简单,只需#include <arm_neon.h>
5、Show me the code
相关转换函数,例子是rgb转yuv420sp nv12的,反过来也是一样道理。
先上C++版本:
然后是Intrinsic_C:
最后是汇编版:
附arm常用寄存器
neon寄存器,注意:这玩意是重叠的,Q0 = D0 + D1 = S0 + S1 + S2 +S3
具体看上文CSDN里博客写的,就不赘述了。
就酱
The text was updated successfully, but these errors were encountered: