FFmpeg
Build and Tuning Guide
Oct, 2022
FFmpeg is a free and open-source complete cross-platform solution to record, convert, and stream audio and video. FFmpeg is comprised of a suite of libraries and programs for handling video, audio, and other multimedia files and streams and is widely used for video transcoding and video scaling. It is a critical part of the workflow of many software projects like YouTube and BiliBili, and its libraries are used by software media players like VLC.
This guide is written for Application Architects and Build Engineers who want to know how to build the latest version of FFmpeg, x264, x265, libvpx, and libaom to take advantage of the latest optimizations available for Ampere® Altra® processors.
This document describes how to build the latest version of FFmpeg and related video codecs for Ampere processors to take advantage of industry leading performance and power efficiency running video encoding on Ampere Altra and Ampere® Altra® Max processors. This document is based on the FFmpeg Compilation Guide available here.
Installing Prerequisites
Follow the instructions in the section titled “Get the Dependencies” in the FFmpeg Compilation Guide for your Linux OS. Versions exist for Ubuntu/Debian/Mint and CentOS/RHEL/Fedora. After verifying the prerequisites are correctly installed, continue to Setup.
Setup
The build instructions use the following directories for FFmpeg source files, build directories and binary output:
~/ffmpeg_sources – location for downloaded source files
~/ffmpeg_build – location of the build and output libraries
~/bin – location of binaries
Create the follow directories:
mkdir -p ~/ffmpeg_sources ~/ffmpeg_build ~/bin
The FFmpeg Compilation Guide describes how to either install these packages using the system package installer or downloading and building the latest version. Compared to the FFmpeg Compilation Guide, this document describes how to:
1. Download and build the latest versions. This is described as an option in the FFmpeg Compilation Guide. 2. How to add the recommended gcc compiler options. When running gcc on Arm architectures, it is recommended to use the -mcpu=native option. Refer to the blog on the Arm website “Compiler flags across architectures: -march, -mtune, and -mcpu” for details. 3. This guide shows how to build with a subset of the packages described in the FFmpeg Compilation Guide. If the full FFmpeg functionality is required for your usage, refer to that document noting the steps outlined below where we highlight our specific recommendations in the beginning of each section describing how to build a package.
libx264 – Download and Build
Note: We modify the default build instructions to add --extra-cflags="-mcpu=native" when running configure.
cd ~/ffmpeg_sources && \ git -C x264 pull 2> /dev/null || git clone --depth 1 https://code.videolan.org/videolan/x264.git && \ cd x264 && \ PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure --prefix="$HOME/ffmpeg_build" --bindir="$HOME/bin" --enable-static --enable-pic --extra-cflags="-mcpu=native" && \ PATH="$HOME/bin:$PATH" make -j $(nproc) && \ make install
libx265 – Download and Build
Notes: It is important to download x265 from bitbucket.org to get the latest optimizations. Also, we modify the default FFmpeg build instructions by setting the CMake environment flags CFLAGS and CXXFLAGS to use -mcpu=native. You can optionally run make with this command PATH="$HOME/bin:$PATH" make -j $(nproc) VERBOSE=1>& make.log to build in parallel, and with VERBOSE=1 verify gcc is using -mcpu=native.
export CFLAGS="-mcpu=native" && export CXXFLAGS="-mcpu=native" sudo apt-get install libnuma-dev && \ cd ~/ffmpeg_sources && \ wget -O x265.tar.bz2 https://bitbucket.org/multicoreware/x265_git/get/master.tar.bz2 && \ tar xjvf x265.tar.bz2 && \ cd multicoreware*/build/linux && \ PATH="$HOME/bin:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_SHARED=off ../../source && \ PATH="$HOME/bin:$PATH" make -j $(nproc) && \ make install
libvpx – Download and Build
Note: We modify the default FFmpeg build instructions to set extra_cflags and extra_cxxflags which are used by configure.
export extra_cflags="-mcpu=native" export extra_cxxflags="-mcpu=native" cd ~/ffmpeg_sources && \ git -C libvpx pull 2> /dev/null || git clone --depth 1 https://chromium.googlesource.com/webm/libvpx.git && \ cd libvpx && \ PATH="$HOME/bin:$PATH" ./configure --prefix="$HOME/ffmpeg_build" --disable-examples --disable-unit-tests --enable-vp9-highbitdepth --as=yasm && \ PATH="$HOME/bin:$PATH" make -j $(nproc) && \ make install
libaom – Download and Build
Note: We modify the default FFmpeg build instructions to set CFLAGS and CXXFLAGS which are used by CMake.
export CFLAGS="-mcpu=native" && export CXXFLAGS="-mcpu=native" cd ~/ffmpeg_sources && \ git -C aom pull 2> /dev/null || git clone --depth 1 https://aomedia.googlesource.com/aom && \ mkdir -p aom_build && \ cd aom_build && \ PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" PATH="$HOME/bin:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/ffmpeg_build" -DENABLE_TESTS=OFF -DENABLE_NASM=on ../aom && \ PATH="$HOME/bin:$PATH" make -j $(nproc) && \ make install
FFmpeg – Download and Build
cd ~/ffmpeg_sources && \ wget -O ffmpeg-snapshot.tar.bz2 https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2 && \ tar xjvf ffmpeg-snapshot.tar.bz2 && \ cd ffmpeg && \ PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure \ --prefix="$HOME/ffmpeg_build" \ --pkg-config-flags="--static" \ --extra-cflags="-I$HOME/ffmpeg_build/include -mcpu=native" \ --extra-cxxflags="-mcpu=native" \ --extra-ldflags="-L$HOME/ffmpeg_build/lib" \ --extra-libs="-lpthread -lm" \ --ld="g++" \ --bindir="$HOME/bin" \ --enable-gpl \ --enable-libaom \ --enable-libvpx \ --enable-libx264 \ --enable-libx265 \ --enable-nonfree && \ PATH="$HOME/bin:$PATH" make -j $(nproc) && \ make install
If the build was successful, you can run FFmpeg as shown below. Congratulations. Happy Benchmarking.
$ ls -1tr ~/bin x264 ffmpeg ffplay ffprobe
FFmpeg can be run via ~/bin/ffmpeg
$ ~/bin/ffmpeg
ffmpeg version XXX Copyright (c) 2000-2022 the FFmpeg developers
...