在.NET中有一组适用于FFMpeg,libavutil,libavformat和libavcodec的P / Invoke声明? [英] Is there a set of working P/Invoke declarations for FFMpeg, libavutil, libavformat and libavcodec in .NET?

查看:199
本文介绍了在.NET中有一组适用于FFMpeg,libavutil,libavformat和libavcodec的P / Invoke声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在寻找从.NET访问libavutil,libavformat和libavcodec(FFMpeg的所有部分)。



目前,我正在获得共享FFMpeg包的自动构建的库每天晚上为Windows 32位执行。



我还使用 ffmpeg-sharp项目。在那个项目中,我已经删除了一些没有编译的类(它们是包装类而不是P / Invoke声明)。



代码编译很好,但是我遇到了几个问题。



首先,av * .dll的构建似乎使用 cdecl调用约定,因为在尝试调用 PInvokeStackImbalanceException http://cekirdek.uludag.org.tr/~ismail/ffmpeg-docs/avformat_8h.html#9baec525f6e67ca07ce289a55cc60589 =noreferrer> av_open_input_file 。这很容易改变,让它正常工作。 AVFormatContext 结构填充。



之后,我想调用 av_find_stream_info 以获取有关文件中流的信息。但是,通过从 av_open_input_file 的调用中检索到的 AVFormatContext 来调用它, AccessViolationException 被抛出,表示我正在尝试有没有人使用P / Invoke来通过P / Invoke访问libavutil,libavformat和libavcodec dll库,并且已经得到它的工作吗?





我应该提到使用命令行版本的FFMpeg,而解决方案在这种情况下不是一个可行的解决方案,访问需要通过库进行。这样做的原因是我不得不为了做我所需要做的事情(我必须对一些非常高清晰度的视频进行逐帧分析)而浪费磁盘,而且我想避免磁盘

解决方案

这就是我想出来的 - 也就是说,大量的P / Invoke声明在ffmpeg-sharp项目不正确。在声明中使用结构的地方数量很多,这些地方已经被封送回来,但随后必须在以后传递给解除分配例程。



由于指针已经作为编组的一部分丢失,所以导致 AccessViolationException 在尝试将该结构传递给其他接受方法的方法时抛出有效的指针(如Windows中的句柄)。而不是将它们视为不透明(就像Windows API那样),他们会将结构编组回来并丢失该进程中的指针。



解决方案是更改获取/返回 <$的API声明c $ c> IntPtr ,并根据需要执行结构的封送处理,不要将它们包含在P / Invoke声明中。


I'm currently looking to access libavutil, libavformat and libavcodec (all part of FFMpeg) from .NET.

Currently, I'm getting the libraries from the automated builds of the shared FFMpeg package performed every night for Windows 32-bit.

I am also using the code from the ffmpeg-sharp project. In that project, I have removed a number of classes that were not compiling (they are wrapper classes not the P/Invoke declarations).

The code compiles fine, but I am running into a few issues.

First, it appears that the build of av*.dll uses the cdecl calling convention, as I was receiving a number of PInvokeStackImbalanceException when trying to call av_open_input_file. This was easy enough to change to get it to work right. The AVFormatContext structure is populated.

After that, I want to call av_find_stream_info to get information about the streams in the file. However, when calling that with the AVFormatContext retrieved from the call to av_open_input_file, an AccessViolationException is thrown indicating that I am trying to read or write from protected memory.

Has anyone used P/Invoke to access the libavutil, libavformat and libavcodec dll libraries through P/Invoke and have gotten it to work?

I should mention that working with the command-line version of FFMpeg, while a solution, is not a viable solution in this case, access needs to occur through the libraries. The reason for this is that I'd have to thrash the disk way too much to do what I need to do (I have to do a frame-by-frame analysis of some very high definition video) and I want to avoid the disk as much as possible.

解决方案

This is what I figured out - namely, a good amount of the P/Invoke declarations in the ffmpeg-sharp project are incorrect. There are a good number of places where they use structures in the declaration which are marshaled back, but subsequently, have to be passed to deallocation routines later.

Because the pointer has been lost as part of the marshaling, this is what was causing the AccessViolationException to be thrown when trying to pass that stucture to other methods that are accepting a valid pointer (like a handle in Windows). Instead of treating them as opaque (as they should, like Windows APIs do) they marshal the structures back and lose the pointer in the process.

The solution is to change their API declarations to take/return an IntPtr and perform marshaling of the structures as needed, not to include them in the P/Invoke declarations.

这篇关于在.NET中有一组适用于FFMpeg,libavutil,libavformat和libavcodec的P / Invoke声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆