Coder Social home page Coder Social logo

openh264lib.net's Introduction

OpenH264Lib.NET

Cisco's openh264 wrapper library for .NET Framework.
Written in C++/CLI to bridge other .NET Framework language like C#.  

How to use

// create encoder and decoder
var encoder = new OpenH264Lib.Encoder("openh264-2.1.1-win32.dll");
var decoder = new OpenH264Lib.Decoder("openh264-2.1.1-win32.dll");

// setup encoder
float fps = 10.0f;
int bps = 5000 * 1000;         // target bitrate. 5Mbps.
float keyFrameInterval = 2.0f; // insert key frame interval. unit is second.
encoder.Setup(640, 480, bps, fps, keyFrameInterval, (data, length, frameType) =>
{
    // called when each frame encoded.
    Console.WriteLine("Encord {0} bytes, frameType:{1}", length, frameType);
    
    // decode it to Bitmap again...
    var bmp = decoder.Decode(data, length);
    if (bmp != null) Console.WriteLine(bmp.Size);
});

// encode frame
foreach(var bmp in bitmaps)
{
    encoder.Encode(bmp);
}

See Example

  1. Open 'OpenH264Lib.sln' Visual Studio solution file.
  2. Build OpenH264Lib project from VisualStudio 'Build OpenH264Lib' menu. (Not 'Build Solution'), or in Solution Explorer, right click 'OpenH264Lib' project and build. Then created OpenH264Lib.dll.
  3. Build OpenH264Sample project from VisualStudio 'Build Solution' menu, or in Solution Explorer, right click 'OpenH264Sample' project and build. This is example C# project how to use OpenH264Lib.dll.
  4. Download 'openh264-2.1.1-win32.dll' from Cisco's openh264 Github repository, and copy it to OpenH264Sample/bin/Debug/ directory.
    • If you are going to use 'openh264-x.x.x-win64.dll', build dll and exe as x64 pratform in step (2) and (3).
    • Use Cisco's dll version 2.1.0 and later. (issue #19). If you are going to use dll version 2.0.0 and before, use old repository.
  5. Execute OpenH264Sample.exe. This program encode image files to H264 avi file, and decode avi to image.

Remarks

Since this is only a wrapper library, you have to know about original openh264 implementation when you add/modify features.

And...

You can make H264 video recorder by using OpenH264Lib.NET and UsbCamera and AviWriter(in MotionJPEGWriter).

int index = 0;
var camera = new Github.secile.Video.UsbCamera(index, new Size(640, 480));
camera.Start();

// create H264 encorder.
var path = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\test.avi";
var encoder = new OpenH264Lib.Encoder("openh264-2.1.1-win32.dll");

// write avi for every frame encorded.
var fps = 10.0f;
var writer = new Github.secile.Avi.AviWriter(System.IO.File.OpenWrite(path), "H264", camera.Size.Width, camera.Size.Height, fps);
OpenH264Lib.Encoder.OnEncodeCallback onEncode = (data, length, frameType) =>
{
    var keyFrame = (frameType == OpenH264Lib.Encoder.FrameType.IDR) || (frameType == OpenH264Lib.Encoder.FrameType.I);
    writer.AddImage(data, keyFrame);
    Console.WriteLine("Encord {0} bytes, KeyFrame:{1}", length, keyFrame);
};

// setup encorder.
int bps = 100000; // 100kbps
encoder.Setup(camera.Size.Width, camera.Size.Height, bps, fps, 2.0f, onEncode);

// encode image for every captured image.
var timer = new System.Timers.Timer(1000 / fps) { SynchronizingObject = this };
timer.Elapsed += (s, ev) =>
{
    var bmp = camera.GetBitmap();
    pbxImage.Image = bmp;
    encoder.Encode(bmp);
};
timer.Start();

// release resource when close.
this.FormClosing += (s, ev) =>
{
    camera.Release();
    timer.Stop();
    writer.Close();
    encoder.Dispose();
};

openh264lib.net's People

Contributors

digitalprojections avatar secile avatar zacuke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

openh264lib.net's Issues

Missing dll

エラー 1 'C:\Users\XXX\source\repos\OpenH264Lib.NET\Debug\OpenH264Lib.dll' メタデータが見つかりませんでした。
it seems like a circular dependency. I need OpenH264Lib.dll to make my app work, while the sample project complains about the same missing dll
What to do?

How to display frames(I,P,B)

I need to display android screen in WPF, the data in WPF will received byte[] frame.
But the byte[] frame will be (I,P,B),
I find the method YUV420PtoRGB() , but just worked about P frame.
How to display all frames? Is it difficult?

How to use by AnyCPU?

How to use x64 platform?
Please add AnyCPU support document.

error by OpenH264Lib.log
“codec_api.h”: No such file or directory

timestamp argument in Encode function

Hello, can you tell me what is the purpose of timestamp argument in Encode function? I think it is delay between two frames but it could also be time from the beginning. However, there are no changes on the video when i change that parameter. How would i inform encoder about time between two frames?

Repetition in Metadata

I am using the encoder with AviWriter. Somehow I got the repetition in the Metadata. There are three in the example below. But it can be more.

General
Complete name                  : C:\test.avi
Format                         : AVI
Format/Info                    : Audio Video Interleave
File size                      : 1.42 MiB
Duration                       : 6 s 0 ms
Overall bit rate               : 1 992 kb/s

Video
ID                             : 0
Format                         : AVC / AVC / AVC
Format/Info                    : Advanced Video Codec
Format profile                 : Baseline@L5 / Baseline@L5 / Baseline@L5
Format settings                : 1 Ref Frames / 1 Ref Frames / 1 Ref Frames
Format settings, CABAC         : No / No / No
Format settings, Reference fra : 1 frame / 1 frame / 1 frame
Codec ID                       : H264
Duration                       : 6 s 0 ms
Bit rate                       : 1 988 kb/s
Width                          : 1 920 pixels
Original width                 : 1 920 pixels / 1 920 pixels / 1 920 pixels
Height                         : 1 200 pixels
Original height                : 1 200 pixels / 1 200 pixels / 1 200 pixels
Display aspect ratio           : 16:10
Frame rate                     : 20.000 FPS
Color space                    : YUV
Chroma subsampling             : 4:2:0 / 4:2:0 / 4:2:0
Bit depth                      : 8 bits / 8 bits / 8 bits
Scan type                      : Progressive / Progressive / Progressive
Bits/(Pixel*Frame)             : 0.043
Stream size                    : 1.42 MiB (100%)

Decoder Samples

In the main readme.md there's a sample for encoding. Is there a sample available for decoding?

H264 Stream freezes when generating IDR Frames

Hi, this is a follow up of the following issue (sipsorcery-org/sipsorcery#317)
I noticed some huge freezes using your wrapper to send H264 frames to a WebRTC stream,
the behavior is described in the issue mentionned before.

I initialize the stream like so
this.H264Encoder = new Encoder("openh264-2.1.1-win64.dll");
this.H264Encoder.Setup(width, height, width * height * 3 / 2 * 8, fps, 2.0F, onEncode);

I changed wrapper and used X264.net and the issue disappeared. Do you have any idea where that freeze could come from ?
Thanks

Decode method returns null image

I followed the readme instructions and I can encode an image to an AVI file using the sample program. This avi file plays ok in media player.

When I use the sample program to decode the avi file, decoder.Decode(...) always returns a null bitmap. This is on Windows 10.

Exception thrown: 'System.IO.FileNotFoundException'

Exception thrown: 'System.IO.FileNotFoundException'
The very not found file is this:
OpenH264Lib.dll
Needless to say, it is included both in the folder and as embedded resource. Not working.
It is working perfectly on the development machine, but failing with the above mentioned error while remote debugging.
The target machine specs are very low:

[OS] Windows Embedded Standard 7
[CPU] Intel(R) ATOM(TM) CPU E3825 @1.33GHz
[メモリ] 2.00GB

I did all kind of research, but stuck with this issue now.

"Could not load file or assembly 'OpenH264Lib.dll' or one of it's dependencies" message on Win 10 vbox/vmware

Hey, first of all, thanks for great wrapper! Working perfectly on host side, but when i try to run my app (that's using this wrapper) on vbox/vmware virtual machine, i get "Could not load file or assembly 'OpenH264Lib.dll' or one of it's dependencies" message.. Which is a bit weird because both OS's are same. I'm pretty sure that problem is on my side but since i'm not experienced in c# i'm not sure where to look/how to resolved this... So any help would be greatly appreciated! :)

RGB to YUV420 mistake

When converting from RGB to YUV420 the U and V equations are flipped which results in a non-standard file where U and V are flipped. I discovered this when debugging and using a third party YUV to RGB converter.

You can tell that they are flipped because V is supposed to be the red component and U the blue component but reading the equations you can see that they are not.

This error is hidden on line 58 of Decoder.cpp when the U and V are flipped again.
byte* rgb = YUV420PtoRGB(y_plane, v_plane, u_plane, width, height, stride);
The u_plane and v_plane arguments are in the reverse order from the function header
byte* Decoder::YUV420PtoRGB(byte* yplane, byte* uplane, byte* vplane, int width, int height, int stride)

The planes are also flipped in the diagram in the documentation of Decoder::YUV420PtoRGB.

Lib can work with .h264 file or .h265 (hevc) ?

i read frames from stream rtsp camera, frames encode in H264 / H264+ / H265 / H265+ (by my setting). i want decode frame and send to web client to play video.
i can use this this lib to decode?
this is profile my frame:

[STREAM]
index=0
codec_name=h264
codec_long_name=H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10
profile=Main
codec_type=video
codec_time_base=1/50
codec_tag_string=[0][0][0][0]
codec_tag=0x0000
width=1920
height=1080
coded_width=1920
coded_height=1088
has_b_frames=0
sample_aspect_ratio=1:1
display_aspect_ratio=16:9
pix_fmt=yuvj420p
level=40
color_range=pc
color_space=bt709
color_transfer=bt709
color_primaries=bt709
chroma_location=left
field_order=progressive
timecode=N/A
refs=1
is_avc=false
nal_length_size=0
id=N/A
r_frame_rate=25/1
avg_frame_rate=25/1
time_base=1/1200000
...
thanks for reading!

Encoded video play speed(frame interval) seems depended on fps.

Hi,
Thank you for your project.

I encoded avi file using public int Encode(Bitmap bmp);
and played this video on mediaplayer(windows).
but, video files encoded with different fps have different play speeds.
It seems to vary according to fps.
I want to keep the playing speed constant even if the fps are different.
Can you help me?

I used the timer to encode the screen capture in real time as follows.

_timer.Interval = 1000 / fps; _timer.Tick += (sender, e) => { try { Bitmap screen = _provider.Capture(); _encoder.Encode(screen); } catch { } };

Unable to play H264 Stream from Ambarella Camera

I have a number of H.264 cameras that output raw h264 data frames. I have made a player based on the OpenH264.NET example, decoding one frame at a time and displaying. This works for all but one of the cameras. There doesn’t seem to be anything unusual about the camera and VLC is able to play it fine from a file or UDP stream. I also built OpenH264 from source on Linux and ran the h264dec utility against the data and it seemed to have no issue decoding. Any ideas as to why there might be an issue with this one camera with OpenH264.NET?

    "streams": [
        {
            "index": 0,
            "codec_name": "h264",
            "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
            "profile": "Main",
            "codec_type": "video",
            "codec_time_base": "1001/60000",
            "codec_tag_string": "[0][0][0][0]",
            "codec_tag": "0x0000",
            "width": 432,
            "height": 240,
            "coded_width": 432,
            "coded_height": 240,
            "closed_captions": 0,
            "has_b_frames": 0,
            "sample_aspect_ratio": "1:1",
            "display_aspect_ratio": "9:5",
            "pix_fmt": "yuvj420p",
            "level": 21,
            "color_range": "pc",
            "chroma_location": "left",
            "field_order": "progressive",
            "refs": 1,
            "is_avc": "false",
            "nal_length_size": "0",
            "r_frame_rate": "30000/1001",
            "avg_frame_rate": "30000/1001",
            "time_base": "1/1200000",
            "bits_per_raw_sample": "8",
            "disposition": {
                "default": 0,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0,
                "timed_thumbnails": 0
            }
        }
    ]

UPDATE: I can see that DecodeFrame2 is returning dsNoParamSets (16)
UPDATE2: It actually seems to alternate between frames, first dsNoParamSets (16) and then dsBitstreamError (4) the next frame...and so on

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.