Skip to content

Commit b90ba59

Browse files
author
Dogancan Ozturk
committed
changes SSE and threading options, fixes,cleaning.
1 parent 66b4220 commit b90ba59

File tree

21 files changed

+550
-493
lines changed

21 files changed

+550
-493
lines changed

Examples/AVRecord/MainWindow.xaml.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ public MainWindow()
100100
decParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_TYPE.VIDEO_BITSTREAM_SVC;
101101
decoder.Initialize(decParam);
102102

103-
encoder.ConverterNumberOfThreads = 0;
104-
decoder.ConverterNumberOfThreads = 0;
105-
decoder.EnableSSEYUVConversion= true;
103+
Converter.EnableSSE = true;
104+
Converter.NumThreads = 0;
105+
106106
InitializeComponent();
107107

108108
}
@@ -558,29 +558,27 @@ private void ParallelConverterChecked(object sender, RoutedEventArgs e)
558558
{
559559
if (encoder == null)
560560
return;
561-
encoder.ConverterNumberOfThreads = ((CheckBox)sender).IsChecked ?? false ? numThreads : 0 ;
562-
decoder.ConverterNumberOfThreads = ((CheckBox)sender).IsChecked??false ? numThreads : 0;
561+
Converter.NumThreads = ((CheckBox)sender).IsChecked ?? false ? numThreads : 0 ;
563562
}
564563
private void ParallelConverterUnChecked(object sender, RoutedEventArgs e)
565564
{
566565
if (encoder == null)
567566
return;
568-
encoder.ConverterNumberOfThreads = ((CheckBox)sender).IsChecked ?? false ? numThreads : 0;
569-
decoder.ConverterNumberOfThreads = ((CheckBox)sender).IsChecked ?? false ? numThreads : 0;
567+
Converter.NumThreads = ((CheckBox)sender).IsChecked ?? false ? numThreads : 0;
570568
}
571569

572570
private void SSEChecked(object sender, RoutedEventArgs e)
573571
{
574572
if (decoder == null)
575573
return;
576-
decoder.EnableSSEYUVConversion = ((CheckBox)sender).IsChecked ?? false;
574+
Converter.EnableSSE = ((CheckBox)sender).IsChecked ?? false;
577575

578576
}
579577
private void SSEUnChecked(object sender, RoutedEventArgs e)
580578
{
581579
if (decoder == null)
582580
return;
583-
decoder.EnableSSEYUVConversion = ((CheckBox)sender).IsChecked ?? false;
581+
Converter.EnableSSE = ((CheckBox)sender).IsChecked ?? false;
584582

585583
}
586584

36.5 KB
Binary file not shown.

Examples/H264SharpNativePInvoke/H264SharpNativePInvoke.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
</ItemGroup>
1414

1515
<ItemGroup>
16+
<ProjectReference Include="..\..\H264SharpBitmapExtentions\H264SharpBitmapExtentions.csproj" />
1617
<ProjectReference Include="..\..\H264Sharp\H264Sharp.csproj" />
1718
</ItemGroup>
1819

@@ -44,8 +45,5 @@
4445
<None Update="openh264-2.4.1-win64.dll">
4546
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
4647
</None>
47-
<None Update="openh264-2.5.0-win64.dll">
48-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
49-
</None>
5048
</ItemGroup>
5149
</Project>

Examples/H264SharpNativePInvoke/Program.cs

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using H264Sharp;
2+
using H264SharpBitmapExtentions;
23
using System.Diagnostics;
34
using System.Drawing;
45
using System.Drawing.Imaging;
@@ -11,6 +12,9 @@ internal class Program
1112
{
1213
static unsafe void Main(string[] args)
1314
{
15+
Converter.EnableSSE = true;
16+
Converter.NumThreads = 4;
17+
Defines.UseCustomThreadPool = true;
1418
//BencmarkConverter();
1519
//return;
1620
// You can change version or specify the path for cisco dll.
@@ -21,27 +25,22 @@ static unsafe void Main(string[] args)
2125
H264Encoder encoder = new H264Encoder();
2226
H264Decoder decoder = new H264Decoder();
2327

24-
encoder.ConverterNumberOfThreads = 4;
25-
decoder.ConverterNumberOfThreads = 4;
26-
decoder.EnableSSEYUVConversion = false;
28+
2729
decoder.Initialize();
30+
2831
var img = System.Drawing.Image.FromFile("ocean 1920x1080.jpg");
2932
//var img = System.Drawing.Image.FromFile("ocean 3840x2160.jpg");
33+
3034
int w = img.Width;
3135
int h = img.Height;
3236
var bmp = new Bitmap(img);
3337
Console.WriteLine($"{w}x{h}");
38+
3439
encoder.Initialize(w, h, 200_000_000, 30, ConfigType.CameraBasic);
3540
Console.WriteLine("Initialised Encoder");
3641

3742
Stopwatch sw = Stopwatch.StartNew();
38-
var data = BitmapToImageData(bmp);
39-
40-
41-
//RgbImage to = new RgbImage(data.Width / 2, data.Height / 2);
42-
//Converter.Downscale(data, to, 2);
43-
//var bb = RgbToBitmap(to);
44-
//bb.Save("Dowmscaled.bmp");
43+
var data = bmp.ToImageData();
4544

4645
RgbImage rgbb = new RgbImage(w, h);
4746
for (int j = 0; j < 1000; j++)
@@ -67,7 +66,7 @@ static unsafe void Main(string[] args)
6766
if (decoder.Decode(encoded, noDelay: true, out DecodingState ds, ref rgbb))
6867
{
6968
//Console.WriteLine($"F:{encoded.FrameType} size: {encoded.Length}");
70-
//Bitmap result = RgbToBitmap(rgbb);
69+
//var result = rgbb.ToBitmap();
7170
//result.Save("Ok1.bmp");
7271

7372
}
@@ -86,7 +85,6 @@ private static void BencmarkConverter()
8685
//var img = System.Drawing.Image.FromFile("ocean 3840x2160.jpg");
8786
var img = System.Drawing.Image.FromFile("ocean 1920x1080.jpg");
8887

89-
int th = 32;
9088
int w = img.Width;
9189
int h = img.Height;
9290
var bmp = new Bitmap(img);
@@ -97,41 +95,21 @@ private static void BencmarkConverter()
9795

9896
var data = BitmapToImageData(bmp);
9997

100-
Converter.Rgbx2Yuv(data, yuvImage, 1);
101-
Converter.Yuv2Rgb(yuvImage, rgb, 4);
102-
RgbToBitmap(rgb).Save("converted.bmp");
98+
Converter.Rgbx2Yuv(data, yuvImage);
99+
Converter.Yuv2Rgb(yuvImage, rgb);
100+
rgb.ToBitmap().Save("converted.bmp");
103101

104102
Stopwatch sw = Stopwatch.StartNew();
105103
for (int i = 0; i < 10000; i++)
106104
{
107-
Converter.Yuv2Rgb(yuvImage, rgb, th);
105+
Converter.Yuv2Rgb(yuvImage, rgb);
108106

109-
Converter.Rgb2Yuv(rgb, yuvImage, th);
107+
Converter.Rgb2Yuv(rgb, yuvImage);
110108
}
111109
Console.WriteLine(sw.ElapsedMilliseconds);
112110

113111
}
114112

115-
private static Bitmap RgbToBitmap(RGBImagePointer img)
116-
{
117-
Bitmap bmp = new Bitmap(img.Width,
118-
img.Height,
119-
img.Width * 3,
120-
PixelFormat.Format24bppRgb,
121-
img.ImageBytes);
122-
return bmp;
123-
}
124-
125-
private static Bitmap RgbToBitmap(RgbImage img)
126-
{
127-
Bitmap bmp = new Bitmap(img.Width,
128-
img.Height,
129-
img.Width * 3,
130-
PixelFormat.Format24bppRgb,
131-
img.ImageBytes);
132-
return bmp;
133-
}
134-
135113
/*
136114
* Pixel data is ARGB, 1 byte for alpha, 1 for red, 1 for green, 1 for blue.
137115
* Alpha is the most significant byte, blue is the least significant.
-964 KB
Binary file not shown.

H264Sharp/Converter.cs

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@
33

44
namespace H264Sharp
55
{
6+
/// <summary>
7+
/// Image format converter
8+
/// </summary>
69
public class Converter
710
{
8-
11+
12+
public static int NumThreads { set => Defines.Native.ConverterSetNumThreads(value); }
13+
public static bool EnableSSE { set => Defines.Native.EnableSSE(value?1:0); }
14+
public static bool EnableNEON { set => Defines.Native.EnableNEON(value?1:0); }
915
/// <summary>
1016
/// Converts RGB,BGR,RGBA,BGRA to YUV420P
1117
/// </summary>
1218
/// <param name="from"></param>
1319
/// <param name="yuv"></param>
14-
/// <param name="numchunks"></param>
15-
public static void Rgbx2Yuv(ImageData from, YuvImage yuv, int numchunks)
20+
/// <param name="numchunks">each chunk represents a parallel work</param>
21+
public static void Rgbx2Yuv(ImageData from, YuvImage yuv)
1622
{
1723
unsafe
1824
{
@@ -29,8 +35,8 @@ public static void Rgbx2Yuv(ImageData from, YuvImage yuv, int numchunks)
2935
Stride = from.Stride,
3036
ImgType = from.ImgType,
3137
};
32-
var refe = yuv.GetImagePointer();
33-
Defines.Native.RGBXtoYUV(ref ugi, ref refe, numchunks);
38+
var refe = yuv.ToYUVImagePointer();
39+
Defines.Native.RGBXtoYUV(ref ugi, ref refe);
3440
}
3541
}
3642
else
@@ -43,8 +49,8 @@ public static void Rgbx2Yuv(ImageData from, YuvImage yuv, int numchunks)
4349
Stride = from.Stride,
4450
ImgType = from.ImgType,
4551
};
46-
var refe = yuv.GetImagePointer();
47-
Defines.Native.RGBXtoYUV(ref ugi, ref refe, numchunks);
52+
var refe = yuv.ToYUVImagePointer();
53+
Defines.Native.RGBXtoYUV(ref ugi, ref refe);
4854
}
4955

5056

@@ -56,8 +62,8 @@ public static void Rgbx2Yuv(ImageData from, YuvImage yuv, int numchunks)
5662
/// </summary>
5763
/// <param name="from"></param>
5864
/// <param name="yuv"></param>
59-
/// <param name="numchunks"></param>
60-
public static void Rgb2Yuv(RgbImage from, YuvImage yuv, int numchunks)
65+
/// <param name="numchunks">each chunk represents a parallel work</param>
66+
public static void Rgb2Yuv(RgbImage from, YuvImage yuv)
6167
{
6268
unsafe
6369
{
@@ -69,23 +75,33 @@ public static void Rgb2Yuv(RgbImage from, YuvImage yuv, int numchunks)
6975
Stride = from.Stride,
7076
ImgType = ImageType.Rgb,
7177
};
72-
var refe = yuv.GetImagePointer();
73-
Defines.Native.RGBXtoYUV(ref ugi, ref refe, numchunks);
78+
var refe = yuv.ToYUVImagePointer();
79+
Defines.Native.RGBXtoYUV(ref ugi, ref refe);
7480
}
7581

7682
}
7783

78-
79-
public static void Yuv2Rgb(YuvImage yuv,RgbImage image, int numchunks)
84+
/// <summary>
85+
/// Converts YUV420p image to RGB format
86+
/// </summary>
87+
/// <param name="yuv"></param>
88+
/// <param name="image"></param>
89+
/// <param name="numchunks"> each chunk represents a parallel work</param>
90+
public static void Yuv2Rgb(YuvImage yuv,RgbImage image)
8091
{
81-
var rgb = new RGBImagePointer(image.Width, image.Height,image.Stride,image.ImageBytes);
82-
var yuvref = yuv.GetImagePointer();
83-
Defines.Native.YUV2RGB(ref yuvref, ref rgb, numchunks);
92+
Yuv2Rgb(yuv.ToYUVImagePointer(), image);
8493
}
85-
public static void Yuv2Rgb(YUVImagePointer yuv, RgbImage image, int numchunks)
94+
95+
/// <summary>
96+
/// Converts YUV420p image to RGB format
97+
/// </summary>
98+
/// <param name="yuv"></param>
99+
/// <param name="image"></param>
100+
/// <param name="numchunks">each chunk represents a parallel work</param>
101+
public static void Yuv2Rgb(YUVImagePointer yuv, RgbImage image)
86102
{
87103
var rgb = new RGBImagePointer(image.Width, image.Height, image.Stride, image.ImageBytes);
88-
Defines.Native.YUV2RGB(ref yuv, ref rgb, numchunks);
104+
Defines.Native.YUV2RGB(ref yuv, ref rgb);
89105
}
90106

91107
/// <summary>

H264Sharp/Data.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,18 @@ public YUVImagePointer(byte* y, byte* u, byte* v, int width, int height, int str
216216
this.strideUV = strideUV;
217217
}
218218
};
219+
219220
/// <summary>
220221
/// Represent YUV420 Planar image.
221222
/// This class owns unmanaged raw image bytes upon creation
222223
/// </summary>
223224
public class YuvImage
224225
{
225-
public int Width;
226-
public int Height;
226+
public readonly int Width;
227+
public readonly int Height;
227228
public readonly int strideY;
228229
public readonly int strideUV;
229-
public IntPtr ImageBytes;
230+
public readonly IntPtr ImageBytes;
230231
private bool disposedValue;
231232

232233
public YuvImage(int width, int height)
@@ -235,10 +236,10 @@ public YuvImage(int width, int height)
235236
Height = height;
236237
strideY = width;
237238
strideUV = width/2;
238-
this.ImageBytes = Marshal.AllocHGlobal((width * height)*2);
239+
this.ImageBytes = Marshal.AllocHGlobal((width * height)+(width*height)/2);
239240
}
240241

241-
internal unsafe YUVImagePointer GetImagePointer()
242+
internal unsafe YUVImagePointer ToYUVImagePointer()
242243
{
243244
return new YUVImagePointer(
244245
(byte*)ImageBytes.ToPointer(),
@@ -248,9 +249,6 @@ internal unsafe YUVImagePointer GetImagePointer()
248249

249250
}
250251

251-
/// <summary>
252-
/// Disposes the thnstance and clears unmanaged memory
253-
/// </summary>
254252
protected virtual void Dispose(bool disposing)
255253
{
256254
if (!disposedValue)
@@ -265,9 +263,6 @@ protected virtual void Dispose(bool disposing)
265263
Dispose(disposing: false);
266264
}
267265

268-
/// <summary>
269-
/// Disposes the thnstance and clears unmanaged memory
270-
/// </summary>
271266
public void Dispose()
272267
{
273268
Dispose(disposing: true);

H264Sharp/Defines.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ namespace H264Sharp
77
{
88
public class Defines
99
{
10-
public static NativeBindings Native = new NativeBindings();
10+
internal readonly static NativeBindings Native = new NativeBindings();
11+
public static bool UseCustomThreadPool { set => Native.EnableCustomPool(value ? 1 : 0); }
1112

1213
static Defines()
1314
{

0 commit comments

Comments
 (0)