Monday, June 20, 2011

FFMPEG - Linux Build

This guild give you steps by step explanation on how to build ffmpeg from source for Linux. I am using CentOS 5.6 as platform

1. Additional repository. Get Epel so that you can yasm, git, etc... libraries

wget http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
rpm -Uvh epel-release-5*.rpm

2. Download FFMPEG source from http://www.ffmpeg.org/download.html

3. Download the following libraries from CentOS command line with wget

wget http://downloads.sourceforge.net/project/lame/lame/3.98.4/lame-3.98.4.tar.gz
wget http://downloads.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2
wget http://downloads.xvid.org/downloads/xvidcore-1.2.2.tar.gz
wget http://liba52.sourceforge.net/files/a52dec-0.7.4.tar.gz
wget http://downloads.sourceforge.net/faac/faad2-2.7.tar.gz
wget http://downloads.sourceforge.net/faac/faac-1.28.tar.gz
wget http://downloads.sourceforge.net/project/libraw1394/libraw1394/libraw1394-2.0.5.tar.gz
wget http://downloads.sourceforge.net/project/libdc1394/libdc1394-2/2.1.2/libdc1394-2.1.2.tar.gz
wget http://downloads.sourceforge.net/project/opencore-amr/opencore-amr/0.1.2/opencore-amr-0.1.2.tar.gz
wget http://www.mplayerhq.hu/MPlayer/releases/codecs/essential-20071007.tar.bz2
wget http://downloads.xiph.org/releases/theora/libtheora-1.1.1.tar.bz2
wget http://downloads.xiph.org/releases/ogg/libogg-1.1.4.tar.gz
wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.2.3.tar.gz
git clone git://git.videolan.org/x264.git

wget http://downloads.sourceforge.net/project/opencore-amr/vo-aacenc/vo-aacenc-0.1.1.tar.gz

4. Yum install the following development tools

yum install yasm
yum install nasm
yum install git
yum install libvpx*
yum install php-devel

5. Untar the download libraries

tar -zxf lame-3.98.4.tar.gz
tar -zxf xvidcore-1.2.2.tar.gz
tar -jxf ffmpeg-php-0.6.0.tbz2
tar -zxf a52dec-0.7.4.tar.gz
tar -zxf faad2-2.7.tar.gz
tar -zxf faac-1.28.tar.gz
tar -zxf libraw1394-2.0.5.tar.gz
tar -zxf libdc1394-2.1.2.tar.gz
tar -zxf opencore-amr-0.1.2.tar.gz
tar -jxf essential-20071007.tar.bz2
tar -jxf libtheora-1.1.1.tar.bz2
tar -zxf libogg-1.1.4.tar.gz
tar -zxf libvorbis-1.2.3.tar.gz

tar -zxf vo-aacenc-0.1.1.tar.gz


6. Make the following folders for x86

mkdir /usr/local/lib/codecs/
cp ./essential-20071007/* /usr/local/lib/codecs/
chmod -R 755 /usr/local/lib/codecs/

7. Make the following folders for x64 as well

mkdir /usr/local/lib64/codecs/
cp ./essential-20071007/* /usr/local/lib64/codecs/
chmod -R 755 /usr/local/lib64/codecs/

8. Perform the following to configure, make and make install the downloaded libraries

cd x264
./configure --enable-shared
make
make install
cd ..

cd xvidcore/build/generic
./configure
make
make install
cd ../../../

cd lame-3.98.4
./configure
make
make install
cd ..

cd a52dec-0.7.4
./configure --enable-shared=PKGS
make
make install
cd ..

cd faad2-2.7
autoreconf -vif
./configure
make
make install
cd ..

cd faac-1.28
./bootstrap
./configure
make
make install
cd ..

cd libraw1394-2.0.5
./configure
make
make install
cd ..

cd libdc1394-2.1.2
./configure
make
make install
cd ..

cd opencore-amr-0.1.2
./configure
make
make install
cd ..

cd libogg-1.1.4
./configure
make
make install
cd ..

cd libvorbis-1.2.3
./configure
make
make install
cd ..

cd libtheora-1.1.1
./configure
make
make install
cd ..

cd vo-aacenc-0.1.1
./configure
make
make install
cd ..

9. Link the built libraries

export LD_LIBRARY_PATH=/usr/local/lib/
echo /usr/local/lib > /etc/ld.so.conf.d/custom-libs.conf
ldconfig

10. Build the FFMPEG with the following configuration and ldconfig link ffmpeg

cd ffmpeg
./configure --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvpx --enable-libfaac --enable-libmp3lame --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libvo-aacenc --enable-libxvid --disable-ffplay --enable-shared --enable-gpl --enable-postproc --enable-nonfree --enable-avfilter --enable-pthreads --extra-cflags=-fPIC
make
make install
cd ..

ldconfig

11. Your FFMPEG is ready to use.

Tuesday, June 14, 2011

LInux - Recover from ldconfig error

If you accidentally ldconfig a library folder with libraries that are not suitable for your current Linux kernel and you restart your system, you may get yourself into a situation whereby you cannot log into the system anymore.

This is possible if you ldconfig a folder with libraries that requires glibc 2.5 during runtime. However, you system is using only glibc 2.3. At your next system startup, your system will try to load the shared object, but, it will fail and denied your log in.

To recover from this, you could do the following (assuming that you are using CentOS)

1. Boot up your system with your Linux installation disc
2. Go into Rescue mode. Simply following the on screen instruction given to load rescue mode.
3. When you are in Rescue mode, you are able to access your system. Usually, your original drive will be mounted in /mnt/sysimage/
4. Create a ld.so.conf file at /
5. Then, cd /mnt/sysimage/
6. Then, find your corrupted ld linkage. Using libtermcap.so as example, you can perform find /mnt/sysimage -name libtermcap.* . This command will find the located of this shared object.
7. Normally, system libraries are located at lib folder and I will use this as example.
8. Then, cd /mnt/sysimage/lib
9. Perform ldconfig
10. Please note that ldconfig kind of hardcode the ld.so.conf to / and that is the reason we create a ld.so.conf file at / in point 4
11. Go to / and perform cp ld* /mnt/sysimage/etc/
12. This will replace the corrupted ld.conf.so and ld.conf.cache.
13. Restart your system.

Tuesday, June 7, 2011

Critical Section - Windows vs Linux in C++

Just to provide a simple equivalent critical section code between Windows and Linux

For Windows, critical section code are located at WinBase.h. Normally, you do not need to include this header as it is pre-loading in VS compiler

For Linux, mutex code are located in pthread.h. You will have to include this header file in order to use mutex in Linux

Creating critical section variable

This variable will be the object to be acquired when you want to use any critical resource

On Windows

CRITICAL_SECTION cs;

On Linux

pthread_mutex_t m;


Initializing critical section

You have to initialize the critical section resource before using

On Windows

InitializeCriticalSection(&cs);

On Linux

//create mutex attribute variable
pthread_mutexattr_t mAttr;

// setup recursive mutex for mutex attribute
pthread_mutexattr_settype(&mAttr, PTHREAD_MUTEX_RECURSIVE_NP);

// Use the mutex attribute to create the mutex
pthread_mutex_init(&m, &mAttr);

// Mutex attribute can be destroy after initializing the mutex variable
pthread_mutexattr_destroy(&mAttr)


Enter and Leave critical section

They always use in a pair. Critical resource should be placed within the critical section

Windows

EnterCriticalSection(&cs);

LeaveCriticalSection(&cs);


Linux

pthread_mutex_lock (&m);


pthread_mutex_unlock (&m);

Destroy critical section

You have to release the critical section when it is no long required

On Windows

DeleteCriticalSection(&cs);

On Linux

pthread_mutex_destroy (&m);