Skip to content

Commit a8aa138

Browse files
committed
rgbYuv simd
1 parent d8de7d8 commit a8aa138

File tree

11 files changed

+351
-490
lines changed

11 files changed

+351
-490
lines changed

Examples/CrossPlatformTest/Program.cs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ internal class Program
1414
*/
1515
static void Main(string[] args)
1616
{
17-
H264Encoder.EnableDebugPrints = true;
18-
H264Decoder.EnableDebugPrints = true;
19-
Converter.EnableNEON = false;
17+
18+
Converter.EnableNEON = true;
2019
Converter.NumThreads = 1;
2120

2221
H264Encoder encoder = new H264Encoder();
@@ -39,8 +38,13 @@ static void Main(string[] args)
3938

4039
YuvImage yuvImage = new YuvImage(w, h);
4140
RgbImage rgb = new RgbImage(w, h);
41+
42+
var ss1 = Stopwatch.StartNew();
4243
Converter.Rgbx2Yuv(data, yuvImage);
4344
Converter.Yuv2Rgb(yuvImage, rgb);
45+
ss1.Stop();
46+
47+
Console.WriteLine("Conv 1: " + ss1.ElapsedMilliseconds);
4448
byte[] dat = new byte[w * h * 3];
4549

4650
unsafe
@@ -49,11 +53,51 @@ static void Main(string[] args)
4953
Buffer.MemoryCopy((byte*)rgb.ImageBytes.ToPointer(), dataPtr, dat.Length, dat.Length);
5054
}
5155
File.WriteAllBytes("Output.bin", dat);
52-
5356

57+
Converter.Rgb2Yuv(rgb, yuvImage);
58+
Converter.Yuv2Rgb(yuvImage, rgb);
59+
unsafe
60+
{
61+
fixed (byte* dataPtr = dat)
62+
Buffer.MemoryCopy((byte*)rgb.ImageBytes.ToPointer(), dataPtr, dat.Length, dat.Length);
63+
}
64+
File.WriteAllBytes("Output1.bin", dat);
65+
66+
67+
var ss2 = Stopwatch.StartNew();
68+
69+
for (int i = 0; i < 50; i++)
70+
{
71+
bytes = File.ReadAllBytes("RawBgr.bin");
72+
data = new ImageData(ImageType.Bgra, 1920, 1080, 1920 * 4, bytes);
73+
74+
ss2.Restart();
75+
Converter.Rgbx2Yuv(data, yuvImage);
76+
ss2.Stop();
77+
Console.WriteLine("Conv1: " + ss2.ElapsedMilliseconds);
78+
Thread.Sleep(100);
79+
80+
ss2.Restart();
81+
Converter.Yuv2Rgb(yuvImage, rgb);
82+
ss2.Stop();
83+
Console.WriteLine("Conv2: " + ss2.ElapsedMilliseconds);
84+
Thread.Sleep(100);
85+
86+
87+
}
88+
89+
90+
var ss = Stopwatch.StartNew();
91+
for (int i = 0; i < 1000; i++)
92+
{
93+
Converter.Rgbx2Yuv(data, yuvImage);
94+
Converter.Yuv2Rgb(yuvImage, rgb);
95+
}
96+
ss.Stop();
97+
Console.WriteLine("Conv: " + ss.ElapsedMilliseconds);
5498

5599
RgbImage rgbb = new RgbImage(w, h);
56-
for (int j = 0; j < 1000; j++)
100+
for (int i = 0; i <= 200; i++)
57101
{
58102

59103
if (!encoder.Encode(data, out EncodedData[] ec))

Examples/H264SharpNativePInvoke/H264SharpNativePInvoke.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,5 @@
4545
<None Update="openh264-2.4.1-win64.dll">
4646
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4747
</None>
48-
<None Update="Output.bin">
49-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
50-
</None>
5148
</ItemGroup>
5249
</Project>

Examples/H264SharpNativePInvoke/Output.bin

Lines changed: 0 additions & 1 deletion
This file was deleted.

Examples/H264SharpNativePInvoke/Program.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,14 @@ static unsafe void Main(string[] args)
5555

5656
Bitmap bp = RawRgbToBitmap(bytes, 1920, 1080);
5757
bp.Save("CVR.bmp");
58-
59-
60-
58+
59+
var bytes1 = File.ReadAllBytes("Output1.bin");
60+
61+
Bitmap bp1 = RawRgbToBitmap(bytes1, 1920, 1080);
62+
bp1.Save("CVR1.bmp");
63+
64+
65+
6166
return;
6267

6368

H264Sharp/Converter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static void Rgb2Yuv(RgbImage from, YuvImage yuv)
7878
Width = from.Width,
7979
Height = from.Height,
8080
Stride = from.Stride,
81-
ImgType = ImageType.Rgb,
81+
ImgType = ImageType.Bgr,
8282
};
8383
var refe = yuv.ToYUVImagePointer();
8484
Defines.Native.RGBXtoYUV(ref ugi, ref refe);

H264SharpNative/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ add_library (CMakeProject1 SHARED
5656
"Yuv2RgbSSE.cpp"
5757
"Yuv2RgbNEON.cpp"
5858
"ThreadPool.cpp"
59+
"Rgb2YuvNEON.cpp"
5960
"Rgb2Yuv.cpp")
6061

6162
if (CMAKE_VERSION VERSION_GREATER 3.12)

H264SharpNative/Converter.cpp

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,27 @@ namespace H264Sharp {
3939
numThreads);
4040
return;
4141
#elif defined(__aarch64__)
42-
if(Converter::EnableNEON>0)
43-
Yuv2Rgb::ConvertYUVToRGB_NEON(
44-
y_ptr,
45-
u_ptr,
46-
v_ptr,
47-
dst_ptr,
48-
width,
49-
height);
50-
else
42+
if (Converter::EnableNEON > 0) {
43+
if(numThreads>1)
44+
Yuv2Rgb::ConvertYUVToRGB_NEON_Parallel(
45+
y_ptr,
46+
u_ptr,
47+
v_ptr,
48+
dst_ptr,
49+
width,
50+
height, numThreads);
51+
else
52+
Yuv2Rgb::ConvertYUVToRGB_NEON(
53+
y_ptr,
54+
u_ptr,
55+
v_ptr,
56+
dst_ptr,
57+
width,
58+
height);
59+
}
60+
61+
else
62+
{
5163
Yuv2Rgb::Yuv420P2RGBDefault(dst_ptr,
5264
y_ptr,
5365
u_ptr,
@@ -58,6 +70,8 @@ namespace H264Sharp {
5870
uv_span,
5971
dst_span,
6072
numThreads);
73+
}
74+
6175
#else
6276
Yuv2Rgb::Yuv420P2RGBDefault(dst_ptr,
6377
y_ptr,
@@ -121,6 +135,11 @@ namespace H264Sharp {
121135
void Converter::BGRAtoYUV420Planar(const unsigned char* bgra, unsigned char* dst, const int width, const int height, const int stride)
122136
{
123137
int numThreads = Converter::NumThreads;
138+
139+
#if defined(__aarch64__)
140+
Rgb2Yuv::BGRAtoYUV420PlanarNeon(bgra, dst, width, height, stride, numThreads);
141+
142+
#else
124143
if (width * height > minSize)
125144
{
126145
Rgb2Yuv::BGRAtoYUV420Planar(bgra, dst, width, height, stride, numThreads);
@@ -129,11 +148,17 @@ namespace H264Sharp {
129148
{
130149
Rgb2Yuv::BGRAtoYUV420Planar(bgra, dst, width, height, stride, 1);
131150
}
151+
#endif
132152
}
133153

134154
void Converter::RGBAtoYUV420Planar(unsigned char* bgra, unsigned char* dst, int width, int height, int stride)
135155
{
136156
int numThreads = Converter::NumThreads;
157+
158+
#if defined(__aarch64__)
159+
Rgb2Yuv::RGBAtoYUV420PlanarNeon(bgra, dst, width, height, stride, numThreads);
160+
161+
#else
137162
if (width * height > minSize)
138163
{
139164
Rgb2Yuv::RGBAtoYUV420Planar(bgra, dst, width, height, stride, numThreads);
@@ -142,12 +167,19 @@ namespace H264Sharp {
142167
{
143168
Rgb2Yuv::RGBAtoYUV420Planar(bgra, dst, width, height, stride, 1);
144169
}
170+
#endif
171+
145172

146173
}
147174

148175
void Converter::BGRtoYUV420Planar(unsigned char* bgra, unsigned char* dst, int width, int height, int stride)
149176
{
150177
int numThreads = Converter::NumThreads;
178+
179+
#if defined(__aarch64__)
180+
Rgb2Yuv::BGRtoYUV420PlanarNeon(bgra, dst, width, height, stride, numThreads);
181+
182+
#else
151183
if (width * height > minSize)
152184
{
153185
Rgb2Yuv::BGRtoYUV420Planar(bgra, dst, width, height, stride, numThreads);
@@ -156,11 +188,18 @@ namespace H264Sharp {
156188
{
157189
Rgb2Yuv::BGRtoYUV420Planar(bgra, dst, width, height, stride, 1);
158190
}
191+
#endif
192+
159193
}
160194

161195
void Converter::RGBtoYUV420Planar(unsigned char* bgra, unsigned char* dst, int width, int height, int stride)
162196
{
163197
int numThreads = Converter::NumThreads;
198+
199+
#if defined(__aarch64__)
200+
Rgb2Yuv::RGBtoYUV420PlanarNeon(bgra, dst, width, height, stride, numThreads);
201+
202+
#else
164203
if (width * height > minSize)
165204
{
166205
Rgb2Yuv::RGBtoYUV420Planar(bgra, dst, width, height, stride, numThreads);
@@ -169,6 +208,7 @@ namespace H264Sharp {
169208
{
170209
Rgb2Yuv::RGBtoYUV420Planar(bgra, dst, width, height, stride, 1);
171210
}
211+
#endif
172212

173213
}
174214

H264SharpNative/Rgb2Yuv.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ namespace H264Sharp
1010
static void RGBAtoYUV420Planar(unsigned char* bgr, unsigned char* dst, int width, int height, int stride, int threadCount);
1111
static void RGBtoYUV420Planar(unsigned char* bgr, unsigned char* dst, int width, int height, int stride, int threadCount);
1212

13+
#if defined(__aarch64__)
14+
1315
static void BGRAtoYUV420PlanarNeon(const unsigned char* bgra, unsigned char* dst, int width, int height, int stride, int threadCount);
1416
static void BGRtoYUV420PlanarNeon(unsigned char* bgr, unsigned char* dst, int width, int height, int stride, int threadCount);
1517
static void RGBAtoYUV420PlanarNeon(unsigned char* bgr, unsigned char* dst, int width, int height, int stride, int threadCount);
1618
static void RGBtoYUV420PlanarNeon(unsigned char* bgr, unsigned char* dst, int width, int height, int stride, int threadCount);
19+
#endif
1720
};
1821

1922
}

0 commit comments

Comments
 (0)