Static OpenCV 2.4.3 Libraries with (FFMPEG, x264, and CUDA) on OSX 10.8

Static OpenCV 2.4.3 Libraries with (FFMPEG, x264, and CUDA) on OSX 10.8

Ok, so this guide will show you how to compile OpenCV as a x86_64 (64 bit) Static Library with FFMPEG (x264, Xvid, Ogg, Vorbis, Lame, and FAAC) and CUDA enabled on Apple OSX 10.8 Mountain Lion. This is very useful for using the latest OpenCV with openFrameworks.

1. First you want to download and decompress all of the dependent tools and libraries into a designated folder. Using CUDA is optional, so don’t install it if you won’t be using it.

Dependencies

Tools:

  1. CMAKE  – http://www.cmake.org/cmake/resources/software.html
  2. pkg-config- http://pkg-config.freedesktop.org/releases/
  3. Yasm- http://yasm.tortall.net/Download.html

Libraries:

  1. FAAC – http://www.audiocoding.com/downloads.html
  2. libogg- http://xiph.org/downloads/
  3. libvorbis- http://xiph.org/downloads/
  4. LAME- http://sourceforge.net/projects/lame/files/lame/
  5. XvidCore- http://www.xvid.org/Downloads.43.0.html
  6. x264- http://www.videolan.org/developers/x264.html
  7. CUDA- http://developer.nvidia.com/cuda-downloads

Core:

  1. FFMPEG- http://ffmpeg.org/download.html
  2. OpenCV- http://opencv.org/downloads.html

2. Install CMAKE and CUDA if you will be using it.

3. Now create a folder where you have downloaded all of the tools and libraries called “opt”. This is where all of your header files and compiled libraries will be copied to.

4. Configure and compile all of the downloaded tools, libraries, and core software. I will be using [DOWNLOAD_FOLDER] as the folder location where all of the downloaded files were saved. You will notice that –prefix=[DOWNLOAD_FOLDER]/opt is added to almost all of the configure commands, this is to tell each one where to install the libraries after compiling is finished.

Configuration and Compiling

FAAC:

cd faac-*/
./configure --enable-static --enable-shared CFLAGS="-arch x86_64" LDFLAGS="-arch x86_64" --prefix=[DOWNLOAD_FOLDER]/opt
make -j4 && sudo make install

LAME:

cd lame-*/
./configure --disable-dependency-tracking --enable-static --enable-shared CFLAGS="-arch i386 -arch x86_64" LDFLAGS="-arch i386 -arch x86_64" --prefix=[DOWNLOAD_FOLDER]/opt
make -j4 && sudo make install

LIBOGG:

cd libogg-*/
./configure --disable-dependency-tracking --enable-static --enable-shared CFLAGS="-arch i386 -arch x86_64" LDFLAGS="-arch i386 -arch x86_64" --prefix=[DOWNLOAD_FOLDER]/opt
make -j4 && sudo make install

PKG-CONFIG:

cd pkg-config-*/
./configure
make -j4 && sudo make install

LIBVORBIS:

cd libvorbis-*/
./configure --disable-dependency-tracking --enable-static --enable-shared --with-pic CFLAGS="-arch i386 -arch x86_64" LDFLAGS="-arch i386 -arch x86_64" --prefix=[DOWNLOAD_FOLDER]/opt --with-ogg-libraries=[DOWNLOAD_FOLDER]/opt/libogg.a/ --with-ogg-includes=[DOWNLOAD_FOLDER]/opt/include/ogg --build=x86_64

YASM:

cd yasm-*/
./configure && make -j4 && sudo make install

X264:

cd x264-*/
./configure --enable-static --enable-shared --enable-pic CFLAGS="-arch i386 -arch x86_64" LDFLAGS="-arch i386 -arch x86_64" --prefix=[DOWNLOAD_FOLDER]/opt
make -j4 && sudo make install

XVID CORE:

cd xvidcore/build/generic
./configure --disable-assembly --enable-static --enable-shared --enable-pic CFLAGS="-arch i386 -arch x86_64" LDFLAGS="-arch i386 -arch x86_64" --prefix=[DOWNLOAD_FOLDER]/opt

FFMPEG:

cd ffmpeg-*/
./configure --disable-debug --disable-ffplay --disable-ffserver --enable-pthreads --enable-pic --enable-static --enable-libmp3lame --enable-libfaac --enable-libx264 --enable-libvorbis  --enable-nonfree --enable-gpl --enable-libxvid --arch=x86_64 --prefix=[DOWNLOAD_FOLDER]/opt --extra-ldflags="-L[DOWNLOAD_FOLDER]/opt/lib" --extra-cflags="-I[DOWNLOAD_FOLDER]/opt/include" --cc=clang
make -j4 && sudo make install

OPENCV:

Patch CUDA DETECT CMAKE File

Edit File: OpenCV-*/cmake/OpenCVDetectCUDA.cmake

Comment out lines 6-9…

#if (NOT MSVC AND NOT CMAKE_COMPILER_IS_GNUCXX OR MINGW)
#  message(STATUS "CUDA compilation was disabled (due to unsuppoted host compiler).")
#  return()
#endif()

Patch MODULES CMAKE File

Edit File: OpenCV-*/modules/highgui/CMakeLists.txt

Add the following to the “CMakeLists.txt” file on line ~152, above “if(HAVE_FFMPEG)”…

if(APPLE)
list(APPEND HIGHGUI_LIBRARIES ${BZIP2_LIBRARIES} -L[DOWNLOAD_FOLDER]/opt/lib -lmp3lame -logg -lvorbis -lvorbisenc -lfaac -lx264 -lxvidcore -lbz2)
endif(APPLE)

Compile OpenCV

cd OpenCV-*/
mkdir release
cd release
cmake -DCMAKE_INSTALL_PREFIX=[DOWNLOAD_FOLDER]/opt -DFFMPEG_INCLUDE_DIR=[DOWNLOAD_FOLDER]/opt/include -DFFMPEG_LIB_DIR=[DOWNLOAD_FOLDER]/opt/lib -DBUILD_SHARED_LIBS=NO -DCMAKE_EXE_LINKER_FLAGS="-L[DOWNLOAD_FOLDER]/opt/lib -lmp3lame -lvorbis -lfaac -lx264 -lxvidcore"-DCMAKE_SHARED_LINKER_FLAGS="-L[DOWNLOAD_FOLDER]/opt/lib -lmp3lame -lvorbis -lfaac -lx264 -lxvidcore" ..
make -j4 && sudo make install

5. Copy 3rd party libraries from OpenCV to your library folder.

release/3rdparty/lib  =>  [DOWNLOAD_FOLDER]/opt/lib

6. Compile a program with the OpenCV libraries. By this point you should have a whole bunch of files in the “opt” folder.

To Compile a Static Binary

WITHOUT CUDA:

g++ -o freak freak_demo.cpp -I../opt/include/ -L../opt/lib/ -lopencv_core -lopencv_highgui -lopencv_features2d -lopencv_imgproc -lopencv_legacy -lopencv_flann -lopencv_nonfree -lopencv_gpu ../opt/lib/libpng.a ../opt/lib/libjpeg.a ../opt/lib/libtiff.a ../opt/lib/libjasper.a -lzlib -lIlmImf -framework Cocoa
g++ -o retina retinaDemo.cpp -I../opt/include/ -L../opt/lib/ -lopencv_core -lopencv_highgui -lopencv_features2d -lopencv_imgproc -lopencv_legacy -lopencv_flann -lopencv_nonfree -lopencv_gpu ../opt/lib/libpng.a ../opt/lib/libjpeg.a ../opt/lib/libtiff.a ../opt/lib/libjasper.a -lzlib -lIlmImf -framework Cocoa ../opt/lib/libswscale.a ../opt/lib/libavformat.a ../opt/lib/libavcodec.a ../opt/lib/libx264.a ../opt/lib/libxvidcore.a ../opt/lib/libvorbis.a ../opt/lib/libvorbisenc.a ../opt/lib/libogg.a ../opt/lib/libmp3lame.a ../opt/lib/libfaac.a -framework QuartzCore -framework VideoDecodeAcceleration ../opt/lib/libIlmImf.a ../opt/lib/libavfilter.a ../opt/lib/libavutil.a ../opt/lib/libavfilter.a -framework QTKit -lopencv_contrib -lbz2

WITH CUDA:

LDFLAGS=-static g++ -o retina retinaDemo.cpp -I../opt/include/ -L../opt/lib/ -lopencv_core -lopencv_highgui -lopencv_features2d -lopencv_imgproc -lopencv_legacy -lopencv_flann -lopencv_nonfree -lopencv_gpu ../opt/lib/libpng.a ../opt/lib/libjpeg.a ../opt/lib/libtiff.a ../opt/lib/libjasper.a -lzlib -lIlmImf -framework Cocoa ../opt/lib/libswscale.a ../opt/lib/libavformat.a ../opt/lib/libavcodec.a ../opt/lib/libx264.a ../opt/lib/libxvidcore.a ../opt/lib/libvorbis.a ../opt/lib/libvorbisenc.a ../opt/lib/libogg.a ../opt/lib/libmp3lame.a ../opt/lib/libfaac.a -framework QuartzCore -framework VideoDecodeAcceleration ../opt/lib/libIlmImf.a ../opt/lib/libavfilter.a ../opt/lib/libavutil.a ../opt/lib/libavfilter.a -framework QTKit -lopencv_contrib -lbz2 -L/usr/local/cuda/lib -lcuda -lcublas -lcudart -lcufft -lcurand -lnpp

Install Script

The following install script is based on the script from Hunter Ford – http://hunterford.me/compiling-ffmpeg-on-mac-os-x/ and information from http://tech.enekochan.com/2012/07/27/install-opencv-2-4-2-with-ffmpeg-support-in-mac-os-x-10-8/. This will download and install all of the necessary libraries  to a folder “/opt” within the directory that you execute the script from.

InstallOpenCV_2.4.3

For reference there is also another script that looks useful at https://github.com/enekochan/installation-scripts/blob/master/installOpenCV-2.4.2-ffmpeg-OSX-ML.sh

 

Leave a Reply

Your email address will not be published. Required fields are marked *