Blog

How to Cross-Compile OpenCV with FFmpeg (x264 and xVid) for AR Drone (ARM Processor)

Setting Up Environment Variables

Remember the setupCrossCompiler file that we created in the previous post, we need to edit this file again to specify the path were all the cross-compiled libraries will go. We want to keep the compiled libraries in the ARM_Install folder in the home directory.

Running the following command will export the environment variable ARMPREFIX that defines the install path ~/ARM_Install.

echo "export ARMPREFIX=/home/$USER/ARM_Install #path where the cross compiled programs will be installed">>~/ARM_Files/setupCrossCompiler

at this point you should open a new termial so that the new environment variable can effect.

Compiling xVideo

cd ~/ARM_Files
wget http://downloads.xvid.org/downloads/xvidcore-1.3.3.tar.gz
tar -zxvf xvidcore-1.3.3.tar.gz
cd xvidcore/build/generic/
./configure --prefix=${ARMPREFIX} --host=arm-none-linux-gnueabi --disable-assembly
make
make install

Compiling x264

cd ~/ARM_Files
git clone git://git.videolan.org/x264
cd x264
./configure --enable-shared --host=arm-none-linux --disable-asm --prefix=${ARMPREFIX} --cross-prefix=${CCPREFIX}
make
make install

Compiling FFmpeg

cd ~/ARM_Files
git clone git://source.ffmpeg.org/ffmpeg.git
cd ffmpeg
git checkout release/2.6
./configure --enable-cross-compile --cross-prefix=${CCPREFIX} --target-os=linux --arch=arm --enable-shared --disable-static --enable-gpl --enable-nonfree --enable-ffmpeg --disable-ffplay --enable-ffserver --enable-swscale --enable-pthreads --disable-yasm --disable-stripping --enable-libx264 --enable-libxvid --prefix=${ARMPREFIX} --extra-cflags="-I"${ARMPREFIX}"/include" --extra-ldflags="-L"${ARMPREFIX}"/lib"
make
make install

Compiling OpenCV

Download opencv from github and checkout version 2.4.10

cd ~/ARM_Files
git clone https://github.com/Itseez/opencv.git
cd opencv
git checkout 2.4.10

Create a new toolchain file my.toolchain.cmake in OpenCV directory, this file will tell cmake how to build the OpenCV.

gedit my.toolchain.cmake

Paste the following code in the file

Now generate the build files using toolchain file you just created.

mkdir build
cd build
cmake -DENABLE_PRECOMPILED_HEADERS=OFF -DWITH_FFMPEG=ON -DCMAKE_TOOLCHAIN_FILE=../my.toolchain.cmake ../

After this is done you can scroll up to confirm if OpenCV has found FFMPEG. You should see something like the following

--   Video I/O:
--     DC1394 1.x:                  NO
--     DC1394 2.x:                  NO
--     FFMPEG:                      YES
--       codec:                     YES (ver 56.35.101)
--       format:                    YES (ver 56.30.100)
--       util:                      YES (ver 54.23.101)
--       swscale:                   YES (ver 3.1.101)
Note: At this point you have the option to add/remove OpenCV components using cmake-gui by doing a cmake-gui . </br>Don't forget to "Configure" and "Generate" after making any changes.
make -j2

Make will take a while, so grab a coffee or something. If you get a compilation error regarding some of OpenCV component(s), you can try and disable that component if you don’t need it. At this point OpenCV has been compiled now all that is left to do is install it.

make install

This will copy the cross-compiled OpenCV to your /home/$USER/ARM_Install directory, the directory you defined in the first step.

You have successfully cross compiled OpenCV, you are now ready to build your first OpenCV program.

How to Cross Compile C/C++ application for AR Drone

Installing Prerequisites

First we need to download all the prerequisite packages that we will be needing in the series of these posts.

sudo apt-get update
sudo apt-get install build-essential linux-libc-dev wget bzip2 ncurses-dev git cmake cmake-curses-gui cmake-qt-gui config-manager wput

Downloading Compiler

Create a new folder named ‘ARM_Files’ in home folder. We will keep all toolchain and cross compiled binaries in this folder throughout the series of tutorials.

mkdir ~/ARM_Files
cd ~/ARM_Files
wget https://sourcery.mentor.com/public/gnu_toolchain/arm-none-linux-gnueabi/arm-2009q3-67-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2
tar jxf arm-2009q3-67-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2

This cross-compiler is meant for 32-bit systems, if you are running a 64-bit linux you will have to install the following additional packages to be able to use the compiler.

sudo apt-get install lib32z1 lib32ncurses5 lib32bz2-1.0

Configuring System Paths

Create a new bash script to set up the environment variables that will help the system find the compiler.

gedit ~/ARM_Files/setupCrossCompiler

Paste the following bash script into the file. All this script does is add the recently downloaded compiler to the system path so that we can use it latter and define a new variable called CCPREFIX.

echo "Setting up the Cross Compiler Environment"

# Path to bin directory of the compiler
export PATH="/home/$USER/ARM_Files/arm-2009q3/bin":$PATH

# prefix of all the tools in a toolchain
export CCPREFIX="/home/$USER/ARM_Files/arm-2009q3/bin/arm-none-linux-gnueabi-"

Now make the script executable

chmod +x ~/ARM_Files/setupCrossCompiler

Add the script to .bashrc so that the cross compilation environment is set up every time you open a terminal.

echo "source /home/$USER/ARM_Files/setupCrossCompiler" >> ~/.bashrc

Close and Reopen the terminal, if you see a message in the terminal saying “Setting up the Cross Compiler Environment” then your cross compilation environment is ready and you can proceed to the next step.

Writing a simple C Program

We test our cross compilation setup by compiling a simple C/C++ program. Add the following hello word code to a file hello.c.

#include <stdio.h>
int main(){
 printf("Hello Drone\n");
 return 0;
}

Compile and run the program on native system to test the code.

gcc hello.c -o PC_hello
./PC_hello

If this prints out Hello Drone on your terminal you can go ahead the compile the same code for AR Drone.

arm-none-linux-gnueabi-gcc hello.c -o AR_hello

Transfer the file to AR Drone

Now that you have the compiled AR Drone object file you can transfer the file to AR Drone via ftp and run it using telnet. Make sure you are in the directory of hello.c file and run the following commands. Just press enter when prompted for passwords.

Note: Make sure that you are connected to the AR Drone's Wifi.
ftp 192.168.1.1
put AR_hello
exit
telnet 192.168.1.1
cd /data/video
chmod +x AR_hello
./AR_hello

Congratulations! You just ran your first cross-compiled project on AR Drone.

For more advanced projects like cross compiling OpenCV libraries and cross compiling OpenCV Projects, see my other posts.