Is there a set of working P/Invoke declarations for FFMpeg, libavutil, libavformat and libavcodec in .NET?

后端 未结 4 1231
旧时难觅i
旧时难觅i 2021-01-02 14:25

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 build

相关标签:
4条回答
  • 2021-01-02 14:42

    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.

    0 讨论(0)
  • 2021-01-02 14:44

    SharpFFmpeg imports c++ libraries. C++ code is an unmanaged code. It needs pointers to the unmanaged memory. "Marshal" class provides some method to allocate unmanaged memory. For example:

        IntPtr buffer = Marshal.AllocHGlobal(buf.Length + FFmpeg.FF_INPUT_BUFFER_PADDING_SIZE); //buf is a byte array
    

    Also, if you want to send a managed variable (any C# variable) to the function, you have to marshal (copy) this variable to the unmanaged memory.

        for (int i = 0; i < buf.Length; i++)
            Marshal.StructureToPtr(buf[i], buffer + i, true);
    

    Now you can send a pointer to the function.

        FFmpeg.avcodec_decode_video(codecContextUnmanaged, frame, ref success, buffer, buf.Length);
    

    It's possible that you'll need to modify some unmanaged structures. To do this you have to copy structure to a managed memory (Marshal.PtrToStructure method), then modify it, and copy it to an unmanaged memory again.
    I was tormented by the same problem a lot. I solved it, but I can't decode video anyway)) I hope my solution will help anybody.

    0 讨论(0)
  • 2021-01-02 14:50

    See Auto Generated FFmpeg wrapper for C#/.NET and Mono.

    0 讨论(0)
  • 2021-01-02 14:57

    I've steered clear from any of those libraries/projects. All info I found at the time pointed to those breaking too easily with new versions and/or just being too out of date.

    What I did is was run the ffmpeg process directly, as I mentioned on this answer, by modifying a sample in a blog post I link there. To this date we've not had trouble with it :)

    If the above doesn't work for your scenario, good luck.

    0 讨论(0)
提交回复
热议问题