Sunday, February 22, 2015

Using FFMPEG library in Visual Studio

Ffmpeg is a very flexible and comprehensive library for decoding and playing media files. However, the code is written conformed to C99 while Visual Studio (version 2012 or earlier) is primarily C89 compliant. There are several ways to do it. One way is to use C99-to-C89 converter to convert the source code so it builds as explained in the ffmpeg website. It may now also be possible to build ffmepg with Visual Studio 2013 and Intel C++ Compiler (ICL).

But I am a little lazy and choose to use the pre-compiled binary. The only problem with those library is that they are built with MinGW instead of Visual Studio. The project I am working on is built with Visual Studio (because it uses some Windows specific features). So the question is figure out a way to link to the pre-built libraries so the pre-built DLLs can be loaded.

In general, binaries compiled with different compiler can not be linked together. There are different name mangling rules, and more importantly they are using different ABIs (Application Binary Interfaces). However, in this case it is actually doable. Using the shared DLLs are generally done by linking to a small "loading" library that actually loads the DLL binaries. As explained in a MinGW wiki page, the "loading" library in Visual Studio can be generated from the DEF file (which is a text file defines all the exposed functions) with the LIB tool provided in Visual Studio.

It turns out that even this is already done. The Zeranoe website with the pre-built ffmepg provides all the pieces that are needed. All it needs is the "shared" package (which contains the DLLs) and the "dev" package (which contains the include and the libraries that loads the DLLs). All the necessary .h, .lib and .dll files are included for building apps to using the ffmpeg DLLs.

One additional small step is that if the main project is in C++, the included headers of ffmpeg need to be enclosed in an extern "C" declaration.