Skip to content

Commit e46ebea

Browse files
author
Dogancan Ozturk
committed
resutructure native code, Converter interop
1 parent 8b04259 commit e46ebea

33 files changed

+2303
-1882
lines changed

Examples/AVRecord/AVRecord.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
</ItemGroup>
2525

2626
<ItemGroup>
27+
<None Update="H264SharpNative-win64.dll">
28+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
29+
</None>
2730
<None Update="openh264-2.4.1-win64.dll">
2831
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2932
</None>
119 KB
Binary file not shown.

Examples/CrossPlatformTest/CrossPlatformTest.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
<!--<RuntimeIdentifier>win-x86</RuntimeIdentifier>-->
1010

1111
</PropertyGroup>
12-
13-
<ItemGroup>
14-
<PackageReference Include="H264Sharp" Version="1.4.2" />
15-
</ItemGroup>
1612

1713
<ItemGroup>
1814
<None Update="H264SharpNative-linux64.so">
119 KB
Binary file not shown.
20 KB
Binary file not shown.

Examples/H264SharpNativePInvoke/Program.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,23 @@ static void Main(string[] args)
3535
Stopwatch sw = Stopwatch.StartNew();
3636
var data = BitmapToImageData(bmp);
3737

38+
YuvImage yuvImage = new YuvImage(w, h);
39+
Converter.Rgbx2Yuv(data, yuvImage, 4);
40+
41+
RgbImage rgb = new RgbImage(w, h);
42+
Converter.Yuv2Rgb(yuvImage, rgb, 4);
43+
44+
Bitmap result2 = RgbToBitmap(rgb);
45+
result2.Save("converted.bmp");
46+
3847
//Converter converter = new Converter();
3948
//RgbImage to = new RgbImage(data.Width / 2, data.Height / 2);
4049
//converter.Downscale(data,to,2);
4150
//var bb = RgbToBitmap(to);
4251
//bb.Save("Dowmscaled.bmp");
4352

4453
RgbImage rgbb = new RgbImage(w, h);
45-
for (int j = 0; j < 1000; j++)
54+
for (int j = 0; j < 1; j++)
4655
{
4756

4857
if (!encoder.Encode(data, out EncodedData[] ec))
@@ -61,12 +70,20 @@ static void Main(string[] args)
6170
//encoded.GetBytes();
6271
//encoded.CopyTo(buffer,offset);
6372

64-
if (decoder.Decode(encoded, noDelay: true, out DecodingState ds, ref rgbb))
73+
if (decoder.Decode(encoded, noDelay: true, out DecodingState ds, out YUVImagePointer yv))
6574
{
66-
//Console.WriteLine($"F:{encoded.FrameType} size: {encoded.Length}");
67-
//Bitmap result = RgbToBitmap(rgbb);
68-
//result.Save("Ok1.bmp");
75+
RgbImage rgba = new RgbImage(w, h);
76+
Converter.Yuv2Rgb(yv, rgba, 4);
77+
Bitmap result22 = RgbToBitmap(rgba);
78+
result22.Save("converted2.bmp");
6979
}
80+
//if (decoder.Decode(encoded, noDelay: true, out DecodingState ds, ref rgbb))
81+
//{
82+
// //Console.WriteLine($"F:{encoded.FrameType} size: {encoded.Length}");
83+
// Bitmap result = RgbToBitmap(rgbb);
84+
// result.Save("Ok1.bmp");
85+
86+
//}
7087

7188
}
7289
}

H264Sharp/Converter.cs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,70 @@ namespace H264Sharp
66
public class Converter
77
{
88

9-
NativeBindings native = new NativeBindings();
9+
static NativeBindings native = new NativeBindings();
10+
11+
public static void Rgbx2Yuv(ImageData from, YuvImage yuv, int numchunks)
12+
{
13+
unsafe
14+
{
15+
if (from.isManaged)
16+
{
17+
fixed (byte* dp = &from.data[from.dataOffset])
18+
{
19+
20+
var ugi = new UnsafeGenericImage()
21+
{
22+
ImageBytes = dp,
23+
Width = from.Width,
24+
Height = from.Height,
25+
Stride = from.Stride,
26+
ImgType = from.ImgType,
27+
};
28+
var refe = yuv.GetRef();
29+
native.RGBXtoYUV(ref ugi, ref refe, numchunks);
30+
}
31+
}
32+
else
33+
{
34+
var ugi = new UnsafeGenericImage()
35+
{
36+
ImageBytes = (byte*)from.imageData.ToPointer(),
37+
Width = from.Width,
38+
Height = from.Height,
39+
Stride = from.Stride,
40+
ImgType = from.ImgType,
41+
};
42+
var refe = yuv.GetRef();
43+
native.RGBXtoYUV(ref ugi, ref refe, numchunks);
44+
}
45+
46+
47+
}
48+
49+
}
50+
51+
public static void Yuv2Rgb( YuvImage yuv,RgbImage image, int numchunks)
52+
{
53+
var rgb = new RGBImagePointer(image.Width, image.Height,image.Stride,image.ImageBytes);
54+
55+
var yuvref = yuv.GetRef();
56+
native.YUV2RGB(ref yuvref, ref rgb, numchunks);
57+
}
58+
public static void Yuv2Rgb(YUVImagePointer yuv, RgbImage image, int numchunks)
59+
{
60+
var rgb = new RGBImagePointer(image.Width, image.Height, image.Stride, image.ImageBytes);
61+
62+
native.YUV2RGB(ref yuv, ref rgb, numchunks);
63+
}
64+
1065
/// <summary>
1166
/// Downslales image by given factor efficiently
1267
/// i.e multiplier 2 gives w/2,h/2.
1368
/// </summary>
1469
/// <param name="from"></param>
1570
/// <param name="to"></param>
1671
/// <param name="multiplier"></param>
17-
public void Downscale(ImageData from, RgbImage to, int multiplier)
72+
public static void Downscale(ImageData from, RgbImage to, int multiplier)
1873
{
1974
unsafe
2075
{
@@ -67,6 +122,8 @@ public void Downscale(ImageData from, RgbImage to, int multiplier)
67122
}
68123
}
69124
}
125+
126+
70127
}
71128
}
72129

H264Sharp/Data.cs

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ public readonly ref struct RGBImagePointer
142142
public readonly int Stride;
143143
public readonly IntPtr ImageBytes;
144144

145+
internal RGBImagePointer(int width, int height, int stride, IntPtr imageBytes)
146+
{
147+
Width = width;
148+
Height = height;
149+
Stride = stride;
150+
ImageBytes = imageBytes;
151+
}
152+
145153
public byte[] GetBytes()
146154
{
147155
int Length = Stride * Height;
@@ -179,6 +187,8 @@ public void CopyTo(byte[] buffer, int startIndex)
179187
Marshal.Copy(ImageBytes, buffer, startIndex, Length);
180188

181189
}
190+
191+
182192
};
183193

184194
/// <summary>
@@ -193,11 +203,69 @@ public unsafe readonly ref struct YUVImagePointer
193203
public readonly int width;
194204
public readonly int height;
195205
public readonly int strideY;
196-
public readonly int strideU;
197-
public readonly int strideV;
206+
public readonly int strideUV;
198207

208+
public YUVImagePointer(byte* y, byte* u, byte* v, int width, int height, int strideY, int strideUV)
209+
{
210+
Y = y;
211+
U = u;
212+
V = v;
213+
this.width = width;
214+
this.height = height;
215+
this.strideY = strideY;
216+
this.strideUV = strideUV;
217+
}
199218
};
200219

220+
public class YuvImage
221+
{
222+
public int Width;
223+
public int Height;
224+
public readonly int strideY;
225+
public readonly int strideUV;
226+
internal IntPtr ImageBytes;
227+
private bool disposedValue;
228+
229+
public YuvImage(int width, int height)
230+
{
231+
Width = width;
232+
Height = height;
233+
strideY = width;
234+
strideUV = width/2;
235+
this.ImageBytes = Marshal.AllocHGlobal(width * height * 3);
236+
}
237+
238+
internal unsafe YUVImagePointer GetRef()
239+
{
240+
return new YUVImagePointer(
241+
(byte*)ImageBytes.ToPointer(),
242+
(byte*)IntPtr.Add(ImageBytes, Width * Height),
243+
(byte*)IntPtr.Add(ImageBytes, Width * Height + (Width*Height/2)),
244+
Width, Height, strideY, strideUV);
245+
246+
}
247+
248+
protected virtual void Dispose(bool disposing)
249+
{
250+
if (!disposedValue)
251+
{
252+
Marshal.FreeHGlobal(ImageBytes);
253+
disposedValue = true;
254+
}
255+
}
256+
257+
~YuvImage()
258+
{
259+
Dispose(disposing: false);
260+
}
261+
262+
public void Dispose()
263+
{
264+
Dispose(disposing: true);
265+
GC.SuppressFinalize(this);
266+
}
267+
}
268+
201269
/// <summary>
202270
/// Represents storable and reusable rgb image container
203271
/// </summary>

H264Sharp/H264Sharp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<IncludeContentInPack>true</IncludeContentInPack>
2424
<SignAssembly>True</SignAssembly>
2525
<DelaySign>False</DelaySign>
26-
<AssemblyOriginatorKeyFile>bin\Release\netstandard2.0\MyKey.snk</AssemblyOriginatorKeyFile>
26+
<!--<AssemblyOriginatorKeyFile>bin\Release\netstandard2.0\MyKey.snk</AssemblyOriginatorKeyFile>-->
2727

2828
</PropertyGroup>
2929

0 commit comments

Comments
 (0)