|
1 | 1 | # H264Sharp |
| 2 | + |
| 3 | +# Native Pinvoike |
| 4 | +Cisco's OpenH264 C++CLI wrapper with optimised image format conversion support. It is very suitable for realtime streaming over network. |
| 5 | +- Tested on .NetFramework and Net(up to 8). |
| 6 | +- Compatible with OpenCV.(i.e. OpenCVsharp) |
| 7 | +- Tested on WPF application with camera and screen capture (P2P Videocall). |
| 8 | +- No memory leaks or GC pressure with bitmaps. |
| 9 | +- Simple console application example is provided as an example. |
| 10 | + |
| 11 | +Library consist of native dll which acts as openH264 wrapper and image format converter (Yuv <-> rgb,bgr,rgba,bgra) |
| 12 | +<br/>Converters are vectorised(AVX2) for high performance. |
| 13 | + |
| 14 | +C# library is .Net standard wrapper library for this dll and performs PInvoke to handle transcoding. |
| 15 | +## Example |
| 16 | +Examples can be found on examples directroy. |
| 17 | + |
| 18 | +Following code shows encoder and decoder in action, commented lines are for hints. |
| 19 | +``` c# |
| 20 | +static void Main(string[] args) |
| 21 | +{ |
| 22 | + Encoder encoder = new Encoder(); |
| 23 | + Decoder decoder = new Decoder(); |
| 24 | + |
| 25 | + var img = System.Drawing.Image.FromFile("ocean.jpg"); |
| 26 | + int w = img.Width; |
| 27 | + int h = img.Height; |
| 28 | + var bmp = new Bitmap(img); |
| 29 | + |
| 30 | + encoder.Initialize(w, h, bps:20_000_000, fps:30, ConfigType.CameraBasic); |
| 31 | + |
| 32 | + for (int j = 0; j < 100; j++) |
| 33 | + { |
| 34 | + var data = BitmapToGenericImage(bmp); |
| 35 | + encoder.Encode(data, out EncodedData[] ec); |
| 36 | + |
| 37 | + //encoder.ForceIntraFrame(); |
| 38 | + //encoder.SetMaxBitrate(2000000); |
| 39 | + //encoder.SetTargetFps(16.9f); |
| 40 | +
|
| 41 | + foreach (var encoded in ec) |
| 42 | + { |
| 43 | + //encoded.GetBytes(); |
| 44 | + //encoded.CopyTo(buffer,offset,count); |
| 45 | +
|
| 46 | + if (decoder.Decode(encoded, noDelay: true, out DecodingState ds, out RGBImage rgb)) |
| 47 | + { |
| 48 | + Bitmap result = RgbToBitmap(rgb); |
| 49 | + //result.Save("Ok.bmp"); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +``` |
| 54 | +Bitmaps are not included on library to keep it cross platform. |
| 55 | +<br/>For the bitmaps and other image container types i will provide extention libraries. |
| 56 | +``` c# |
| 57 | + private static Bitmap RgbToBitmap(RGBImage img) |
| 58 | + { |
| 59 | + Bitmap bmp = new Bitmap(img.Width, |
| 60 | + img.Height, |
| 61 | + img.Width * 3, |
| 62 | + PixelFormat.Format24bppRgb, |
| 63 | + img.ImageBytes); |
| 64 | + return bmp; |
| 65 | + } |
| 66 | +``` |
| 67 | +And to extract bitmap data: |
| 68 | +```c# |
| 69 | +/* |
| 70 | + * Pixel data is ARGB, 1 byte for alpha, 1 for red, 1 for green, 1 for blue. |
| 71 | + * Alpha is the most significant byte, blue is the least significant. |
| 72 | + * On a little-endian machine, like yours and many others, |
| 73 | + * the little end is stored first, so the byte order is b g r a. |
| 74 | + */ |
| 75 | +private static GenericImage BitmapToGenericImage(Bitmap bmp) |
| 76 | +{ |
| 77 | + int width = bmp.Width; |
| 78 | + int height = bmp.Height; |
| 79 | + BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), |
| 80 | + ImageLockMode.ReadOnly, |
| 81 | + PixelFormat.Format32bppArgb); |
| 82 | + var bmpScan = bmpData.Scan0; |
| 83 | + |
| 84 | + //PixelFormat.Format32bppArgb is default |
| 85 | + var img = new GenericImage(); |
| 86 | + switch (bmp.PixelFormat) |
| 87 | + { |
| 88 | + case PixelFormat.Format32bppArgb: |
| 89 | + img.ImgType = ImageType.Bgra; //endianness |
| 90 | + break; |
| 91 | + case PixelFormat.Format32bppRgb: |
| 92 | + img.ImgType = ImageType.Bgra; |
| 93 | + break; |
| 94 | + case PixelFormat.Format24bppRgb: |
| 95 | + img.ImgType = ImageType.Bgr; |
| 96 | + break; |
| 97 | + default: |
| 98 | + throw new NotSupportedException($"Format {bmp.PixelFormat} is not supported"); |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + img.Width = width; |
| 103 | + img.Height = height; |
| 104 | + img.Stride = bmpData.Stride; |
| 105 | + img.ImageBytes = bmpScan; |
| 106 | + |
| 107 | + bmp.UnlockBits(bmpData); |
| 108 | + return img; |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | + |
| 113 | +# Legacy C++/CLI(deprecated) |
| 114 | +### C++Cli wrapper is deprecated due to platform limitations and other issues. Plase use native Pinvoke version which is also distrbuted with Nuget. |
2 | 115 | Cisco's OpenH264 C++/CLI wrapper with optimised image format conversion support. It is very suitable for realtime streaming over network. |
3 | 116 | - Offers managed and unmanaged API. |
4 | 117 | - Tested on .NetFramework and NetCore(up to 8). |
|
0 commit comments