开发者

How to wrap a C library parameter?(Creating x264 .Net wrapper)

so in dll we have x264_param_t structure\object and a function for its setting up x264_param_apply_profile. in C we use such code to set it up

x264_param_t param;
x264_param_default_preset(&param, "veryfast", "zerolatency");
param.i_threads = 1;
param.i_width = width;
param.i_height = height;
param.i_fps_num = fps;
param.i_fps_den = 1;
// Intra refres:
param.i_keyint_max = fps;
param.b_intra_refresh = 1;
//Rate control:
param.rc.i_rc_method = X264_RC_CRF;
param.rc.f_rf_c开发者_StackOverflow中文版onstant = 25;
param.rc.f_rf_constant_max = 35;
//For streaming:
param.b_repeat_headers = 1;
param.b_annexb = 1;
x264_param_apply_profile(&param, "baseline");

I want to create wrapper for such.. thing. so I have libx264.dll and visual studio 2010 pro.

How can I create .Net C# wrapper for it?

I am a beginner in P\Invoke stuff so I do not get a lot of it...

what I want to achive is is frame by frame level of working with x264... By now I need only encoding parts... And all needed sample code for doing it in C is in How does one encode a series of images into H264 using the x264 C API? . So I need to write a wrapper only for stuff mentioned there... So I am asking - how to create a wrapper on Parameter and on Function that sets up thap param. And I would love to see how to call that wrapper back from c#. So if you could provide any code in support I'd be glad to see it.


Instead of trying this (and the zillions other codes) yourself, may I suggest you try the excellent MeGUI? It is developed in C# and supports all of X264, ready to be used in your C# or other .NET project. No wrappers needed, they did that all for you already.

While it won't be terribly hard to call a C library function using C#, if you do wish to do it all yourself, I'd like to suggest you have a look at http://pinvoke.net, which gives you numerous examples how to do this yourself.


Surprisingly, in all these years, it seems nobody has built and shared even the simplest .NET wrapper for libx264. So I recently built one which I call "x264net" and put it on github, here: https://github.com/bp2008/x264net

The wrapper is very simplistic at this time, making it a good example of how to access C libraries from .NET languages. I did not use P/Invoke, because I find it difficult to work with. Instead, I used a language called "C++/CLI" to create an interoperability layer. C++/CLI is a language based on C++ which can access native C/C++ libraries, while itself being accessible by .NET code. So it bridges the gap between managed and native code much like P/Invoke does.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜