1
1
# H264Sharp
2
+ Cisco's OpenH264 Native wrapper for .Net with optimised image format conversion support. It is very suitable for realtime streaming over network.
3
+ - Plug&Play
4
+ - Tested on .NetFramework and Net(up to 8).
5
+ - Compatible with OpenCV.(i.e. OpenCVsharp)
6
+ - Tested on WPF application with camera and screen capture (P2P Videocall).
7
+ - No memory leaks or GC pressure with bitmaps.
8
+ - Simple console application example is provided as an example.
9
+
10
+ Library consist of native dll which acts as OpenH264 wrapper and image format converter (YUV420p <-> RGB,BGR,RGBA,BGRA)
11
+ <br />Converters are vectorised(AVX2) for high performance.
12
+
13
+ C# library is .Net standard wrapper library for this dll and performs PInvoke to handle transcoding.
14
+ ## Nuget
15
+ Install the nuget package and its ready to go. All native dependencies are automatically installed and will apepear on your executable directory.
16
+
17
+ [ ![ NuGet] ( https://img.shields.io/nuget/v/H264Sharp )] ( https://www.nuget.org/packages/H264Sharp/1.0.4 )
18
+
19
+ For usage in Unity, You have to specify the absolute path for openh264 dll. (i.e. StreamingAssets)
20
+ ``` c#
21
+ Defines .CiscoDllName64bit = " {YourPath}/openh264-2.4.0-win64.dll" ;
22
+ ```
23
+
24
+ ## Example
25
+ Examples can be found on examples directroy.
26
+
27
+ Following code shows encoder and decoder in action, commented lines are for hints.
28
+ ``` c#
29
+ static void Main (string [] args )
30
+ {
31
+ Encoder encoder = new Encoder ();
32
+ Decoder decoder = new Decoder ();
33
+
34
+ var img = System .Drawing .Image .FromFile (" ocean.jpg" );
35
+ int w = img .Width ;
36
+ int h = img .Height ;
37
+ var bmp = new Bitmap (img );
38
+
39
+ encoder .Initialize (w , h , bps :20_ 000_ 000 , fps :30 , ConfigType .CameraBasic );
40
+
41
+ for (int j = 0 ; j < 100 ; j ++ )
42
+ {
43
+ var data = BitmapToGenericImage (bmp );
44
+ encoder .Encode (data , out EncodedData [] ec );
45
+
46
+ // encoder.ForceIntraFrame();
47
+ // encoder.SetMaxBitrate(2000000);
48
+ // encoder.SetTargetFps(16.9f);
49
+
50
+ foreach (var encoded in ec )
51
+ {
52
+ // encoded.GetBytes();
53
+ // encoded.CopyTo(buffer,offset,count);
54
+
55
+ if (decoder .Decode (encoded , noDelay : true , out DecodingState ds , out RGBImage rgb ))
56
+ {
57
+ Bitmap result = RgbToBitmap (rgb );
58
+ // result.Save("Ok.bmp");
59
+ }
60
+ }
61
+ }
62
+ ```
63
+ Bitmaps are not included on library to keep it cross platform .
64
+ < br / > For the bitmaps and other image container types i will provide extention libraries .
65
+ ``` c #
66
+ private static Bitmap RgbToBitmap (RGBImage img )
67
+ {
68
+ Bitmap bmp = new Bitmap (img .Width ,
69
+ img .Height ,
70
+ img .Width * 3 ,
71
+ PixelFormat .Format24bppRgb ,
72
+ img .ImageBytes );
73
+ return bmp ;
74
+ }
75
+ ```
76
+ And to extract bitmap data :
77
+ ```c #
78
+ /*
79
+ * Pixel data is ARGB, 1 byte for alpha, 1 for red, 1 for green, 1 for blue.
80
+ * Alpha is the most significant byte, blue is the least significant.
81
+ * On a little-endian machine, like yours and many others,
82
+ * the little end is stored first, so the byte order is b g r a.
83
+ */
84
+ private static GenericImage BitmapToGenericImage (Bitmap bmp )
85
+ {
86
+ int width = bmp .Width ;
87
+ int height = bmp .Height ;
88
+ BitmapData bmpData = bmp .LockBits (new Rectangle (0 , 0 , width , height ),
89
+ ImageLockMode .ReadOnly ,
90
+ PixelFormat .Format32bppArgb );
91
+ var bmpScan = bmpData .Scan0 ;
92
+
93
+ // PixelFormat.Format32bppArgb is default
94
+ var img = new GenericImage ();
95
+ switch (bmp .PixelFormat )
96
+ {
97
+ case PixelFormat .Format32bppArgb :
98
+ img .ImgType = ImageType .Bgra ; // endianness
99
+ break ;
100
+ case PixelFormat .Format32bppRgb :
101
+ img .ImgType = ImageType .Bgra ;
102
+ break ;
103
+ case PixelFormat .Format24bppRgb :
104
+ img .ImgType = ImageType .Bgr ;
105
+ break ;
106
+ default :
107
+ throw new NotSupportedException ($" Format {bmp .PixelFormat } is not supported" );
108
+
109
+ }
110
+
111
+ img .Width = width ;
112
+ img .Height = height ;
113
+ img .Stride = bmpData .Stride ;
114
+ img .ImageBytes = bmpScan ;
115
+
116
+ bmp .UnlockBits (bmpData );
117
+ return img ;
118
+ }
119
+ ```
120
+
121
+
122
+ # Legacy C++/CLI(deprecated)
123
+ ### C++Cli wrapper is deprecated due to platform limitations and other issues. Plase use native Pinvoke version which is also distrbuted with Nuget.
2
124
Cisco 's OpenH264 C++/CLI wrapper with optimised image format conversion support. It is very suitable for realtime streaming over network.
3
125
- Offers managed and unmanaged API .
4
126
- Tested on .NetFramework and NetCore (up to 8 ).
@@ -7,7 +129,7 @@ Cisco's OpenH264 C++/CLI wrapper with optimised image format conversion support.
7
129
- No memory leaks or GC pressure with bitmaps .
8
130
- Simple console application example is provided as an example .
9
131
10
- ### Setup
132
+ ### Setup(deprecated)
11
133
- Default Constructor will look for `openh264 -2.3.1-win32 .dll ` or `openh264 -2.3.1-win64 .dll ` automatically on executable directory depending on process type (64 / 32 bit ).
12
134
- You can setup with a different dll name , constructor is overloaded .
13
135
``` c #
@@ -21,7 +143,7 @@ Cisco's OpenH264 C++/CLI wrapper with optimised image format conversion support.
21
143
H264Sharp .Encoder .ConfigType .CameraBasic );
22
144
```
23
145
24
- ### Encode
146
+ ### Encode (deprecated)
25
147
- You can encode from rgb / rgba / bgr / bgra / yuv_i420 on as raw data format , or System .Drawing .Bitmaps .
26
148
- Raw data is compatible with OpenCV Mats or any other standard image container .
27
149
- EncodedFrame represents h264 encoded bytes (NALs etc ).
@@ -42,7 +164,7 @@ Cisco's OpenH264 C++/CLI wrapper with optimised image format conversion support.
42
164
```
43
165
44
166
45
- ### Decode
167
+ ### Decode(deprecated)
46
168
- You can decode with pointers or with managed byte array as input .
47
169
- You can decode into System .Drawing .Bitmaps or raw data format images (they are compatible with OpenCV Mats and any other standard image containers .).
48
170
```C #
@@ -60,22 +182,22 @@ Cisco's OpenH264 C++/CLI wrapper with optimised image format conversion support.
60
182
.. .
61
183
```
62
184
63
- # Converter dll
185
+ ## Converter dll(deprecated)
64
186
A separate dll is provided for RGB <-> YUV conversions . Its compiled with clang LLVM with AVX2 intrinsics .
65
187
</br >You can optionally include it on your executable path just like Openh264 dll .
66
188
</br >
67
189
</br >If wrapper cannot find the Converter32 /64 dll or if your machine does not support AVX2 it will fall back to use default C ++/Cli versions .
68
190
</br >External dll 2x + faster than C ++/Cli versions .
69
191
70
- # TLDR how to install
192
+ ## TLDR how to install(deprecated)
71
193
- Go to my releases find latest version .
72
194
- Reference H264Sharp dll on your C # project .
73
195
- Add `openh264 -2.3.1-win32 .dll ` or `openh264 -2.3.1-win64 .dll ` or both to your executable directory (Or include on your project and ckeck copy to output -> copy if newer ).
74
196
- Keep the original names if you want to use default constructors .
75
197
- Optionally Add Converter64 /32 dlls to your executable directory same way as openh264 dll .
76
198
- Enjoy
77
199
78
- # Remarks
200
+ ## Remarks(deprecated)
79
201
- Decode callbacks with raw image formats use cached back buffer , if you wont consume them immediately , make a copy or sync your system .
80
202
- Encoder output " EncodedFrame" uses cached back buffer , if you wont consume them immediately , make a copy or sync your system .
81
203
- .Net Core and .Net Framework releases are provided .
0 commit comments