Skip to content

Commit 8b04259

Browse files
Merge pull request #11 from ReferenceType/develop
dynamic native library resolution
2 parents 19c05cf + 2e6a702 commit 8b04259

25 files changed

+1184
-493
lines changed

Examples/CrossPlatformTest/CrossPlatformTest.csproj

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
2+
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
<PublishAot>False</PublishAot>
9-
</PropertyGroup>
9+
<!--<RuntimeIdentifier>win-x86</RuntimeIdentifier>-->
1010

11+
</PropertyGroup>
12+
1113
<ItemGroup>
12-
<Reference Include="H264Sharp">
13-
<HintPath>H264Sharp.dll</HintPath>
14-
</Reference>
14+
<PackageReference Include="H264Sharp" Version="1.4.2" />
1515
</ItemGroup>
1616

1717
<ItemGroup>
18-
<None Update="H264Sharp.dll">
19-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
20-
</None>
2118
<None Update="H264SharpNative-linux64.so">
2219
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2320
</None>
-27.5 KB
Binary file not shown.
-113 KB
Binary file not shown.

Examples/CrossPlatformTest/Program.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal class Program
99
//TODO test on linux with the opt none removal
1010

1111
/*
12-
* Loads a raw rgba and encodes -> decodes.
12+
* Loads a raw rgba and encodes -> decodes.
1313
* I publish this for linux and add ncessary .so files on out dir.
1414
*/
1515
static void Main(string[] args)
@@ -25,14 +25,16 @@ static void Main(string[] args)
2525

2626
var w = 1920;
2727
var h = 1080;
28-
encoder.Initialize(w, h, 200_000_000, 30, ConfigType.CameraBasic);
28+
encoder.Initialize(w, h, 200_000_000, 30, ConfigType.CameraCaptureAdvanced);
2929
Console.WriteLine("Initialised Encoder");
3030

3131
Stopwatch sw = Stopwatch.StartNew();
3232
var bytes = File.ReadAllBytes("RawBgr.bin");
3333
var data = new ImageData(ImageType.Bgra, 1920, 1080, 1920*4, bytes);
3434

3535

36+
37+
3638

3739
RgbImage rgbb = new RgbImage(w, h);
3840
for (int j = 0; j < 1000; j++)
-7 KB
Binary file not shown.

H264Sharp/Converter.cs

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,8 @@ namespace H264Sharp
55
{
66
public class Converter
77
{
8-
//todo
9-
[DllImport(Defines.WrapperDllName64bit, EntryPoint = "GetDecoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
10-
private static extern void RGBtoYUVx64(ref RGBImagePointer rgb, ref YUVImagePointer yuv, int numThreads);
11-
12-
[DllImport(Defines.WrapperDllName32bit, EntryPoint = "DecodeAsRGB", CallingConvention = CallingConvention.Cdecl)]
13-
private static extern void RGBtoYUVx86( ref RGBImagePointer rgb, ref YUVImagePointer yuv,int numThreads);
14-
15-
[DllImport(Defines.WrapperDllName64bit, EntryPoint = "GetDecoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
16-
private static extern void YUV2RGBx64(ref YUVImagePointer rgb, ref RGBImagePointer yuv, int numThreads);
17-
18-
[DllImport(Defines.WrapperDllName32bit, EntryPoint = "DecodeAsRGB", CallingConvention = CallingConvention.Cdecl)]
19-
private static extern void YUV2RGBx86(ref YUVImagePointer rgb, ref RGBImagePointer yuv, int numThreads);
20-
//
21-
22-
[DllImport(Defines.WrapperDllName32bit, EntryPoint = "DownscaleImg", CallingConvention = CallingConvention.Cdecl)]
23-
private static extern void DownscaleImgx86(ref UnsafeGenericImage from, ref UnsafeGenericImage to, int mul);
24-
25-
[DllImport(Defines.WrapperDllName64bit, EntryPoint = "DownscaleImg", CallingConvention = CallingConvention.Cdecl)]
26-
private static extern void DownscaleImgx64(ref UnsafeGenericImage from, ref UnsafeGenericImage to, int mul);
27-
28-
29-
30-
private readonly bool x64 = Environment.Is64BitProcess;
31-
8+
9+
NativeBindings native = new NativeBindings();
3210
/// <summary>
3311
/// Downslales image by given factor efficiently
3412
/// i.e multiplier 2 gives w/2,h/2.
@@ -61,15 +39,7 @@ public void Downscale(ImageData from, RgbImage to, int multiplier)
6139
t.Stride = from.Stride;
6240
t.ImgType = ImageType.Rgb;
6341

64-
if (x64)
65-
{
66-
DownscaleImgx64(ref ugi, ref t, multiplier);
67-
}
68-
else
69-
{
70-
DownscaleImgx86(ref ugi, ref t, multiplier);
71-
}
72-
42+
native.DownscaleImg(ref ugi, ref t, multiplier);
7343
}
7444
}
7545
else
@@ -92,14 +62,8 @@ public void Downscale(ImageData from, RgbImage to, int multiplier)
9262
t.Stride = from.Stride;
9363
t.ImgType = ImageType.Rgb;
9464

95-
if (x64)
96-
{
97-
DownscaleImgx64(ref ugi, ref t, multiplier);
98-
}
99-
else
100-
{
101-
DownscaleImgx86(ref ugi, ref t, multiplier);
102-
}
65+
native.DownscaleImg(ref ugi, ref t, multiplier);
66+
10367
}
10468
}
10569
}

H264Sharp/Defines.cs

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,59 @@
1-
//#define OS_LINUX
1+
//#define OS_LINUX_ARM
22
//#undef OS_WINDOWS
3+
using System.Runtime.InteropServices;
4+
using System;
5+
36
namespace H264Sharp
47
{
58
public class Defines
6-
{
7-
#if OS_WINDOWS
8-
public static string CiscoDllName64bit = "openh264-2.4.1-win64.dll";
9-
public static string CiscoDllName32bit = "openh264-2.4.1-win32.dll";
10-
11-
public const string WrapperDllName64bit = "H264SharpNative-win64.dll";
12-
public const string WrapperDllName32bit = "H264SharpNative-win32.dll";
13-
#elif OS_LINUX
14-
public static string CiscoDllName64bit = "./libopenh264-2.4.1-linux64.7.so";
15-
public static string CiscoDllName32bit = "./libopenh264-2.4.1-linux32.7.so";
16-
17-
public const string WrapperDllName64bit = "H264SharpNative-linux64.so";
18-
public const string WrapperDllName32bit = "H264SharpNative-linux32.so";
19-
#elif OS_FREEBSD
20-
// FreeBSD-specific dll
21-
#elif OS_MAC
22-
// Mac-specific dll
23-
#endif
24-
25-
26-
}
9+
{
10+
static Defines()
11+
{
12+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
13+
{
14+
switch (RuntimeInformation.ProcessArchitecture)
15+
{
16+
case Architecture.X86:
17+
CiscoDllName = "openh264-2.4.1-win32.dll";
18+
break;
19+
case Architecture.X64:
20+
CiscoDllName = "openh264-2.4.1-win64.dll";
21+
break;
22+
}
23+
24+
}
25+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
26+
{
27+
switch (RuntimeInformation.ProcessArchitecture)
28+
{
29+
case Architecture.X86:
30+
CiscoDllName = "./libopenh264-2.4.1-linux64.7.so";
31+
break;
32+
case Architecture.X64:
33+
CiscoDllName = "./libopenh264-2.4.1-linux32.7.so";
34+
break;
35+
case Architecture.Arm:
36+
CiscoDllName = "./libopenh264-2.4.1-linux-arm.7.so";
37+
break;
38+
case Architecture.Arm64:
39+
CiscoDllName = "./libopenh264-2.4.1-linux-arm64.7.so";
40+
break;
41+
}
42+
}
43+
44+
}
45+
46+
public static string CiscoDllName;
47+
48+
public const string WrapperDllWinx86 = "H264SharpNative-win32.dll";
49+
public const string WrapperDllWinx64 = "H264SharpNative-win64.dll";
50+
51+
public const string WrapperDllLinuxx64 = "H264SharpNative-linux64.so";
52+
public const string WrapperDllLinuxx86 = "H264SharpNative-linux32.so";
53+
54+
public const string WrapperDllLinuxArm64 = "H264SharpNative-linux-arm64.so";
55+
public const string WrapperDllLinuxArm32 = "H264SharpNative-linux-arm32.so";
56+
57+
}
58+
2759
}

0 commit comments

Comments
 (0)