Skip to content

Commit cf06c28

Browse files
committed
linux compatibility support on native code
1 parent feaaa73 commit cf06c28

File tree

20 files changed

+567
-141
lines changed

20 files changed

+567
-141
lines changed

Examples/AVRecord/MainWindow.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public partial class MainWindow :System.Windows.Window
4141
private H264Decoder decoder;
4242
private Stream s;
4343
private AviWriter writer;
44-
const int w = 1920;
45-
const int h = 1080;
44+
const int w = 640;
45+
const int h = 480;
4646
object mtex = new object();
4747
int numThreads = 4;
4848
public MainWindow()
4.5 KB
Binary file not shown.

Examples/H264SharpNativePInvoke/Program.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,16 @@ static void Main(string[] args)
3939
Stopwatch sw = Stopwatch.StartNew();
4040
var data = BitmapToImageData(bmp);
4141

42+
//Converter converter = new Converter();
43+
//RgbImage to = new RgbImage(data.Width / 2, data.Height / 2);
44+
//converter.Downscale(data,to,2);
45+
//var bb = RgbToBitmap(to);
46+
//bb.Save("Dowmscaled.bmp");
47+
4248
RgbImage rgbb = new RgbImage(w, h);
4349
for (int j = 0; j < 1000; j++)
4450
{
45-
51+
4652
if(!encoder.Encode(data, out EncodedData[] ec))
4753
{
4854
Console.WriteLine("skipped");
@@ -104,7 +110,7 @@ private static Bitmap RgbToBitmap(RgbImage img)
104110
* On a little-endian machine, like yours and many others,
105111
* the little end is stored first, so the byte order is b g r a.
106112
*/
107-
private static H264Sharp.ImageData BitmapToImageData(Bitmap bmp)
113+
private static ImageData BitmapToImageData(Bitmap bmp)
108114
{
109115
int width = bmp.Width;
110116
int height = bmp.Height;

H264Sharp/Converter.cs

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace H264Sharp
77
{
88
public class Converter
99
{
10+
//todo
1011
[DllImport(Defines.WrapperDllName64bit, EntryPoint = "GetDecoder", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
1112
private static extern void RGBtoYUVx64(ref RGBImagePointer rgb, ref YUVImagePointer yuv, int numThreads);
1213

@@ -18,10 +19,96 @@ public class Converter
1819

1920
[DllImport(Defines.WrapperDllName32bit, EntryPoint = "DecodeAsRGB", CallingConvention = CallingConvention.Cdecl)]
2021
private static extern void YUV2RGBx86(ref YUVImagePointer rgb, ref RGBImagePointer yuv, int numThreads);
22+
//
23+
24+
[DllImport(Defines.WrapperDllName32bit, EntryPoint = "DownscaleImg", CallingConvention = CallingConvention.Cdecl)]
25+
private static extern void DownscaleImgx86(ref UnsafeGenericImage from, ref UnsafeGenericImage to, int mul);
26+
27+
[DllImport(Defines.WrapperDllName64bit, EntryPoint = "DownscaleImg", CallingConvention = CallingConvention.Cdecl)]
28+
private static extern void DownscaleImgx64(ref UnsafeGenericImage from, ref UnsafeGenericImage to, int mul);
29+
30+
31+
32+
private readonly bool x64 = Environment.Is64BitProcess;
33+
34+
public void Downscale(ImageData from, RgbImage to, int multiplier)
35+
{
36+
unsafe
37+
{
38+
if (from.isManaged)
39+
{
40+
fixed (byte* dp = &from.data[from.dataOffset])
41+
{
42+
var ugi = new UnsafeGenericImage()
43+
{
44+
ImageBytes = dp,
45+
Width = from.Width,
46+
Height = from.Height,
47+
Stride = from.Stride,
48+
ImgType = from.ImgType,
49+
};
50+
51+
var t = new UnsafeGenericImage();
52+
53+
t.ImageBytes = (byte*)to.ImageBytes;
54+
t.Width = from.Width;
55+
t.Height = from.Height;
56+
t.Stride = from.Stride;
57+
t.ImgType = ImageType.Rgb;
58+
59+
if (x64)
60+
{
61+
DownscaleImgx64(ref ugi, ref t, multiplier);
62+
}
63+
else
64+
{
65+
DownscaleImgx86(ref ugi, ref t, multiplier);
66+
}
67+
68+
}
69+
}
70+
else
71+
{
72+
73+
var ugi = new UnsafeGenericImage()
74+
{
75+
ImageBytes = (byte*)from.imageData.ToPointer(),
76+
Width = from.Width,
77+
Height = from.Height,
78+
Stride = from.Stride,
79+
ImgType = from.ImgType,
80+
};
81+
82+
var t = new UnsafeGenericImage();
83+
84+
t.ImageBytes = (byte*)to.ImageBytes;
85+
t.Width = from.Width;
86+
t.Height = from.Height;
87+
t.Stride = from.Stride;
88+
t.ImgType = ImageType.Rgb;
89+
90+
if (x64)
91+
{
92+
DownscaleImgx64(ref ugi, ref t, multiplier);
93+
}
94+
else
95+
{
96+
DownscaleImgx86(ref ugi, ref t, multiplier);
97+
}
98+
}
99+
100+
101+
102+
}
103+
104+
}
105+
106+
107+
21108

22109

23-
//todo
24110
}
111+
25112
}
26113

27114

H264Sharp/Data.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public ImageData(ImageType imgType, int width, int height, int stride, IntPtr im
110110
imageData= imageBytes;
111111
isManaged = false;
112112
}
113+
113114

114115
}
115116

@@ -223,6 +224,53 @@ public void Dispose()
223224
}
224225

225226

227+
}
228+
229+
/// <summary>
230+
/// Represents storable bgr image
231+
/// you can pool images and reuse them.
232+
/// </summary>
233+
public class BgrImage : IDisposable
234+
{
235+
public IntPtr ImageBytes;
236+
public int offset;
237+
public int Width;
238+
public int Height;
239+
public int Stride;
240+
private bool disposedValue;
241+
242+
public BgrImage(int width, int height)
243+
{
244+
245+
this.Width = width;
246+
this.Height = height;
247+
this.offset = 0;
248+
this.Stride = width * 3;
249+
this.ImageBytes = Marshal.AllocHGlobal(width * height * 3);
250+
}
251+
252+
protected virtual void Dispose(bool disposing)
253+
{
254+
if (!disposedValue)
255+
{
256+
257+
Marshal.FreeHGlobal(ImageBytes);
258+
disposedValue = true;
259+
}
260+
}
261+
262+
~BgrImage()
263+
{
264+
Dispose(disposing: false);
265+
}
266+
267+
public void Dispose()
268+
{
269+
Dispose(disposing: true);
270+
GC.SuppressFinalize(this);
271+
}
272+
273+
226274
}
227275

228276

H264Sharp/H264Encoder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ public class H264Encoder:IDisposable
119119

120120
[DllImport(Defines.WrapperDllName32bit, EntryPoint = "SetOptionEncoder", CallingConvention = CallingConvention.Cdecl)]
121121
private static extern int SetOptionEncoderx86(IntPtr encoder, ENCODER_OPTION option, IntPtr value);
122+
123+
122124
#endregion
123125

124126
private readonly IntPtr encoder;
@@ -430,7 +432,7 @@ public unsafe bool Encode(byte* YUV, out EncodedData[] ed)
430432
// return img;
431433
// }
432434
//}
433-
435+
434436
private unsafe EncodedData[] Convert(FrameContainer fc)
435437
{
436438
EncodedData[] data = new EncodedData[fc.FrameCount];

H264Sharp/H264Sharp.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
66
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
77
<Title>H264Sharp</Title>
8-
<Version>1.2.0</Version>
8+
<Version>1.2.1</Version>
99
<PackageProjectUrl>https://github.com/ReferenceType/H264Sharp</PackageProjectUrl>
1010
<Authors>ReferenceType</Authors>
1111
<RepositoryUrl>https://github.com/ReferenceType/H264Sharp</RepositoryUrl>
1212
<PackageTags>h264;openh264;transcoder;h264sharp</PackageTags>
1313
<PackageReleaseNotes>https://github.com/ReferenceType/H264Sharp/releases/</PackageReleaseNotes>
1414
<PackageId>H264Sharp</PackageId>
15-
<Description>Cisco's OpenH264 wrapper for .Net with fast YUV conversion support</Description>
15+
<Description>H264 encoding library well suited for streaming based on Cisco's OpenH264</Description>
1616
<Copyright></Copyright>
1717
<PackageLicenseFile>Licence.txt</PackageLicenseFile>
1818
</PropertyGroup>
4.5 KB
Binary file not shown.
3.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)