CS-FreeStyle / 10000-How-To-Do-in-CS

1 stars 0 forks source link

build ffmpeg with qsv, nvenc support #100

Open liuty10 opened 4 years ago

liuty10 commented 4 years ago

Thanks to this link: https://gist.github.com/Brainiarc7/4f831867f8e55d35cbcb527e15f9f116 https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu

Build FFmpeg with QSV support: Build platform: Ubuntu 18.04LTS. Ubuntu 16.04LTS is not recommended for some third-party tools' version mismatch. But it's not a big problem.

  1. Ensure the platform is up to date: sudo apt update && sudo apt -y upgrade && sudo apt -y dist-upgrade

  2. Install baseline dependencies first (inclusive of OpenCL headers+) sudo apt-get -y install autoconf automake build-essential libass-dev libtool pkg-config texinfo zlib1g-dev libva-dev cmake mercurial libdrm-dev libvorbis-dev libogg-dev git libx11-dev libperl-dev libpciaccess-dev libpciaccess0 xorg-dev intel-gpu-tools opencl-headers libwayland-dev xutils-dev ocl-icd-* libssl-dev libnuma-dev

  3. Then add the Oibaf PPA, needed to install the latest development headers for libva:

    sudo add-apt-repository ppa:oibaf/graphics-drivers
    sudo apt-get update && sudo apt-get -y upgrade && sudo apt-get -y dist-upgrade
  4. Build the latest libva and all drivers from source: Setup build environment:

    mkdir -p ~/FFMPEG/vaapi
    mkdir -p ~/FFMPEG/ffmpeg_build
    mkdir -p ~/FFMPEG/ffmpeg_sources
    mkdir -p ~/FFMPEG/bin

    Build the dependency chain as shown, starting with installing the latest build of libdrm. This is needed to enable the cl_intel_va_api_media_sharing extension, needed when deriving OpenCL device initialization interop in FFmpeg, as illustrated later on in the documentation:

    cd vaapi
    git clone https://anongit.freedesktop.org/git/mesa/drm.git libdrm
    cd libdrm
    # apt install meson
    # meson --prefix=/usr -Dudev=true
    # meson --prefix=/usr -Dudev=true && ninja
    # ninja install

    if if doesn't work, you can also use sudo apt install libdrm. (1)Libva Libva is an implementation for VA-API (Video Acceleration API)

VA-API is an open-source library and API specification, which provides access to graphics hardware acceleration capabilities for video processing. It consists of a main library and driver-specific acceleration backends for each supported hardware vendor. It is a prerequisite for building the VAAPI driver components below.

cd vaapi
git clone https://github.com/01org/libva
cd libva
./autogen.sh --prefix=/usr --libdir=/usr/lib/x86_64-linux-gnu
time make -j$(nproc) VERBOSE=1
sudo make -j$(nproc) install
sudo ldconfig -vvvv

(2) Gmmlib: The Intel(R) Graphics Memory Management Library provides device specific and buffer management for the Intel(R) Graphics Compute Runtime for OpenCL(TM) and the Intel(R) Media Driver for VAAPI.

The component is a prerequisite to the Intel Media driver build step below.

To build this, create a workspace directory within the vaapi sub directory and run the build:

mkdir -p ~/FFMPEG/vaapi/workspace
cd ~/FFMPEG/vaapi/workspace
git clone https://github.com/intel/gmmlib
mkdir -p build
cd build
cmake -DCMAKE_BUILD_TYPE= Release ../gmmlib
make -j$(nproc)
sudo make -j$(nproc) install 

(3) Intel Media driver: The Intel(R) Media Driver for VAAPI is a new VA-API (Video Acceleration API) user mode driver supporting hardware accelerated decoding, encoding, and video post processing for GEN based graphics hardware, released under the MIT license.

cd ~/FFMPEG/vaapi/workspace
git clone https://github.com/intel/media-driver
cd media-driver
git submodule init
git pull
mkdir -p ~/FFMPEG/vaapi/workspace/build_media
cd ~/FFMPEG/vaapi/workspace/build_media

Configure the project with cmake:

cmake ../media-driver \
-DBS_DIR_GMMLIB=$PWD/../gmmlib/Source/GmmLib/ \
-DBS_DIR_COMMON=$PWD/../gmmlib/Source/Common/ \
-DBS_DIR_INC=$PWD/../gmmlib/Source/inc/ \
-DBS_DIR_MEDIA=$PWD/../media-driver \
-DCMAKE_INSTALL_PREFIX=/usr \
-DCMAKE_INSTALL_LIBDIR=/usr/lib/x86_64-linux-gnu \
-DINSTALL_DRIVER_SYSCONF=OFF \
-DLIBVA_DRIVERS_PATH=/usr/lib/x86_64-linux-gnu/dri

Then build the media driver: time make -j$(nproc) VERBOSE=1 Then install the project: sudo make -j$(nproc) install VERBOSE=1 Add yourself to the video group: sudo usermod -a -G video $USER Now, export environment variables as shown below:

LIBVA_DRIVERS_PATH=/usr/lib/x86_64-linux-gnu/dri
LIBVA_DRIVER_NAME=iHD

Put that in /etc/environment.

(4) Before you proceed with the iMSDK: It is recommended that you install the Intel Neo OpenCL runtime: Justification: This will allow for Intel's MediaSDK OpenCL inter-op back-end to be built. Note: There's also a Personal Package Archive (PPA) for this that you can add, allowing you to skip the manual build step, as shown:

sudo add-apt-repository ppa:intel-opencl/intel-opencl
sudo apt-get update

Then install the packages: sudo apt install intel-*

Here, you will find some packages can't be installed:

The following packages have unmet dependencies:
 intel-cloc : Depends: intel-igc-opencl but it is not installable
 intel-ocloc : Depends: intel-igc-opencl but it is not installable
 intel-opencl : Depends: intel-igc-opencl but it is not installable
 intel-opencl-icd : Depends: libigdgmm11 (>= 20.1.1-1~) but it is not going to be installed
                    Breaks: intel-opencl but 19.36.14103-1~ppa1~bionic1 is to be installed
E: Unable to correct problems, you have held broken packages.

The main reason is that Neo packages and dependencies on launchpad were renamed, so please use: sudo apt-get install intel-opencl-icd

Install the dependencies for the OpenCL back-end:

Build dependencies: sudo apt-get install ccache flex bison cmake g++ git patch zlib1g-dev autoconf xutils-dev libtool pkg-config libpciaccess-dev libz-dev clinfo

Testing: Use clinfoand confirm that the ICD is detected.

Number of platforms                               1
  Platform Name                                   Intel(R) OpenCL HD Graphics
  Platform Vendor                                 Intel(R) Corporation
  Platform Version                                OpenCL 2.1 
  Platform Profile                                FULL_PROFILE

Optionally, run Luxmark and confirm that Intel Neo's OpenCL platform is detected and usable.

Be aware that Luxmark, among others, require freeglut, which you can install by running:

sudo apt install freeglut3*

  1. Build Intel's MSDK: This package provides an API to access hardware-accelerated video decode, encode and filtering on Intel® platforms with integrated graphics. It is supported on platforms that the intel-media-driver is targeted for.
cd ~/FFMPEG/vaapi
git clone https://github.com/Intel-Media-SDK/MediaSDK msdk
cd msdk
git submodule init
git pull
mkdir -p ~/FFMPEG/vaapi/build_msdk
cd ~/FFMPEG/vaapi/build_msdk
cmake -DCMAKE_BUILD_TYPE=Release -DENABLE_WAYLAND=ON -DENABLE_X11_DRI3=ON -DENABLE_OPENCL=ON  ../msdk
time make -j$(nproc) VERBOSE=1
sudo make install -j$(nproc) VERBOSE=1

CMake will automatically detect the platform you're on and enable the platform-specific hooks needed for a working build.

Create a library config file for the iMSDK: sudo nano /etc/ld.so.conf.d/imsdk.conf

Content:

/opt/intel/mediasdk/lib
/opt/intel/mediasdk/plugins

Then run:

sudo ldconfig -vvvv
sudo systemctl reboot
  1. Other preparations: (1) Build and deploy libx264 statically: This library provides a H.264 video encoder. See the H.264 Encoding Guide for more information and usage examples. This requires ffmpeg to be configured with --enable-gpl --enable-libx264.
    cd ~/FFMPEG/ffmpeg_sources
    git clone http://git.videolan.org/git/x264.git 
    cd x264/
    PATH="$HOME/FFMPEG/bin:$PATH" ./configure --prefix="$HOME/FFMPEG/ffmpeg_build" --enable-static --enable-pic --bit-depth=all
    PATH="$HOME/FFMPEG/bin:$PATH" make -j$(nproc) VERBOSE=1
    make -j$(nproc) install VERBOSE=1
    make -j$(nproc) distclean

    (2) Build and configure libx265: This library provides a H.265/HEVC video encoder. See the H.265 Encoding Guide for more information and usage examples.

    cd ~/FFMPEG/ffmpeg_sources
    hg clone https://bitbucket.org/multicoreware/x265
    cd ~/FFMPEG/ffmpeg_sources/x265/build/linux
    PATH="$HOME/FFMPEG/bin:$PATH" cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$HOME/FFMPEG/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source
    make -j$(nproc) VERBOSE=1
    make -j$(nproc) install VERBOSE=1
    make -j$(nproc) clean VERBOSE=1

    (3)Build and deploy the libfdk-aac library: This provides an AAC audio encoder. See the AAC Audio Encoding Guide for more information and usage examples. This requires ffmpeg to be configured with --enable-libfdk-aac (and --enable-nonfree if you also included --enable-gpl).

    cd ~/FFMPEG/ffmpeg_sources
    git clone https://github.com/mstorsjo/fdk-aac
    cd fdk-aac
    autoreconf -fiv
    ./configure --prefix="$HOME/FFMPEG/ffmpeg_build" --disable-shared --with-pic
    make -j$(nproc)
    make -j$(nproc) install
    make -j$(nproc) distclean

(4) Build and configure libvpx:

cd ~/FFMPEG/ffmpeg_sources
git clone https://github.com/webmproject/libvpx
cd libvpx
./configure --prefix="$HOME/FFMPEG/ffmpeg_build" --enable-runtime-cpu-detect --cpu=native --as=nasm --enable-vp8 --enable-vp9 \
--enable-postproc-visualizer --disable-examples --disable-unit-tests --enable-static --disable-shared \
--enable-multi-res-encoding --enable-postproc --enable-vp9-postproc \
--enable-vp9-highbitdepth --enable-pic --enable-webm-io --enable-libyuv 
time make -j$(nproc)
time make -j$(nproc) install
time make clean -j$(nproc)
time make distclean

(5) Build LibVorbis:

cd ~/FFMPEG/ffmpeg_sources
wget https://ftp.osuosl.org/pub/xiph/releases/vorbis/libvorbis-1.3.6.tar.gz
tar -xf libvorbis-1.3.6.tar.gz 
cd libvorbis-1.3.6/
autoreconf -ivf
./configure --enable-static --prefix="$HOME/FFMPEG/ffmpeg_build"
time make -j$(nproc)
time make -j$(nproc) install
time make clean -j$(nproc)
time make distclean

(6) sudo apt-get -y install libnuma-dev libmp3lame-dev libopus-dev

  1. Build FFMPEG
    cd ~/FFMPEG/ffmpeg_sources
    git clone https://github.com/FFmpeg/FFmpeg -b master
    cd FFmpeg

Then, build:

PATH="$HOME/FFMPEG/bin:$PATH" PKG_CONFIG_PATH="$HOME/FFMPEG/ffmpeg_build/lib/pkgconfig:/opt/intel/mediasdk/lib/pkgconfig" ./configure \
  --pkg-config-flags="--static" \
  --enable-static --disable-shared \
  --prefix="$HOME/FFMPEG/ffmpeg_build" \
  --bindir="$HOME/FFMPEG/bin" \
  --extra-cflags="-I$HOME/FFMPEG/ffmpeg_build/include" \
  --extra-ldflags="-L$HOME/FFMPEG/ffmpeg_build/lib" \
  --extra-cflags="-I/opt/intel/mediasdk/include" \
  --extra-ldflags="-L/opt/intel/mediasdk/lib" \
  --extra-ldflags="-L/opt/intel/mediasdk/plugins" \
  --enable-libmfx \
  --enable-vaapi \
  --enable-opencl \
  --disable-debug \
  --enable-libvorbis \
  --enable-libvpx \
  --enable-libdrm \
  --enable-gpl \
  --enable-runtime-cpudetect \
  --enable-libfdk-aac \
  --enable-libx264 \
  --enable-libx265 \
  --enable-openssl \
  --enable-pic \
  --extra-libs="-lpthread -lm -lz -ldl" \
  --enable-nonfree 
PATH="$HOME/FFMPEG/bin:$PATH" make -j$(nproc) 
make -j$(nproc) install 
make -j$(nproc) distclean 
hash -r

testing QSV: https://trac.ffmpeg.org/wiki/Hardware/QuickSync

Now, QSV should work. Next, we will try to set the building environment for NVENC. Follow the steps here: https://devblogs.nvidia.com/nvidia-ffmpeg-transcoding-guide/

Download and install a compatible driver from the NVIDIA web site Download and install the CUDA toolkit Clone the nv-codec-headers repository and install using this repository as header-only: make install Configure FFmpeg using the following command (use correct CUDA library path): ./configure --enable-cuda --enable-cuvid --enable-nvenc --enable-nonfree --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64

liuty10 commented 4 years ago

So, the configuration should be like this.

PATH="$HOME/FFMPEG/bin:$PATH" PKG_CONFIG_PATH="$HOME/FFMPEG/ffmpeg_build/lib/pkgconfig:/opt/intel/mediasdk/lib/pkgconfig" ./configure \
  --pkg-config-flags="--static" \
  --enable-static --disable-shared \
  --prefix="$HOME/FFMPEG/ffmpeg_build" \
  --bindir="$HOME/FFMPEG/bin" \
  --extra-cflags="-I$HOME/FFMPEG/ffmpeg_build/include" \
  --extra-ldflags="-L$HOME/FFMPEG/ffmpeg_build/lib" \
  --extra-cflags="-I/opt/intel/mediasdk/include" \
  --extra-ldflags="-L/opt/intel/mediasdk/lib" \
  --extra-ldflags="-L/opt/intel/mediasdk/plugins" \
  --extra-cflags="-I/usr/local/cuda/include" \
  --extra-ldflags="-L/usr/local/cuda/lib64" \
  --enable-libmfx \
  --enable-vaapi \
  --enable-opencl \
  --disable-debug \
  --enable-libvorbis \
  --enable-libvpx \
  --enable-libdrm \
  --enable-gpl \
  --enable-runtime-cpudetect \
  --enable-libfdk-aac \
  --enable-libx264 \
  --enable-libx265 \
  --enable-openssl \
  --enable-pic \
  --extra-libs="-lpthread -lm -lz -ldl" \
  --enable-cuda \
  --enable-cuvid \
  --enable-nvenc \
  --enable-libnpp \
  --enable-nonfree
PATH="$HOME/FFMPEG/bin:$PATH" make -j$(nproc)
make -j$(nproc) install
make -j$(nproc) distclean
hash -r
liuty10 commented 3 years ago

install nvidia-driver: https://askubuntu.com/questions/1077061/how-do-i-install-nvidia-and-cuda-drivers-into-ubuntu

liuty10 commented 3 years ago

How to test:

ffmpeg -i USVA6833.MP4 -c:v libx264 -global_quality 25 libx264.mp4
ffmpeg -i USVA6833.MP4 -c:v libx265 -global_quality 25 libx265.mp4
ffmpeg -i USVA6833.MP4 -c:v h264_qsv -global_quality 25 h264_qsv.mp4
ffmpeg -i USVA6833.MP4 -c:v hevc_qsv -global_quality 25 hevc_qsv.mp4
ffmpeg -i USVA6833.MP4 -c:v h264_nvenc -global_quality 25 h264_nvenc.mp4
ffmpeg -i USVA6833.MP4 -c:v hevc_nvenc -global_quality 25 hevc_nvenc.mp4
liuty10 commented 3 years ago

https://tecadmin.net/install-ffmpeg-on-linux/

liuty10 commented 3 years ago

Server:

cd ./ffmpeg-3.4.6
export PATH="$HOME/FFMPEG/bin:$PATH"
export PKG_CONFIG_PATH="$HOME/FFMPEG/ffmpeg_build/lib/pkgconfig:/opt/intel/mediasdk/lib/pkgconfig"

./configure --prefix="$HOME/FFMPEG/ffmpeg_build" \
            --bindir="$HOME/FFMPEG/bin" \
            --pkg-config-flags="--static" \
            --extra-cflags="-I$HOME/FFMPEG/ffmpeg_build/include" \
            --extra-ldflags="-L$HOME/FFMPEG/ffmpeg_build/lib" \
            --extra-cflags="-I/opt/intel/mediasdk/include" \
            --extra-ldflags="-L/opt/intel/mediasdk/lib" \
            --extra-ldflags="-L/opt/intel/mediasdk/plugins" \
            --extra-libs="-lpthread -lm -lz -ldl" \
            --enable-libmfx \
            --enable-vaapi \
            --enable-libx264 \
            --enable-libx265 \
            --enable-nonfree \
            --enable-gpl \
            --enable-libass \
            --enable-libfdk-aac \
            --enable-libfreetype \
            --enable-libmp3lame \
            --enable-libopus \
            --enable-libvorbis \
            --enable-libvpx \
            --enable-runtime-cpudetect \
            --enable-libdrm \
            --enable-pic \
            --enable-shared \
            --enable-nonfree

make
make install
hash -r

Client:

PATH="$HOME/FFMPEG/bin:$PATH" PKG_CONFIG_PATH="$HOME/FFMPEG/ffmpeg_build/lib/pkgconfig" ./configure \
  --pkg-config-flags="--static" \
  --prefix="$HOME/FFMPEG/ffmpeg_build" \
  --bindir="$HOME/FFMPEG/bin" \
  --extra-cflags="-I$HOME/FFMPEG/ffmpeg_build/include" \
  --extra-ldflags="-L$HOME/FFMPEG/ffmpeg_build/lib" \
  --enable-libdrm \
  --enable-gpl \
  --enable-runtime-cpudetect \
  --enable-libx264 \
  --enable-libx265 \
  --enable-pic \
  --extra-libs="-lpthread -lm -lz -ldl" \
  --enable-shared \
  --enable-nonfree
PATH="$HOME/FFMPEG/bin:$PATH" make -j$(nproc)
make -j$(nproc) install
make -j$(nproc) distclean
hash -r
liuty10 commented 3 years ago

Server:

export PATH="$HOME/FFMPEG/bin:$PATH:/usr/local/cuda/bin"
export PKG_CONFIG_PATH="$HOME/FFMPEG/ffmpeg_build/lib/pkgconfig:/opt/intel/mediasdk/lib/pkgconfig"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:$HOME/FFMPEG/ffmpeg_build/lib:/opt/VES/lib64"

Client

export PATH="$HOME/FFMPEG/bin:$PATH"
export PKG_CONFIG_PATH="$HOME/FFMPEG/ffmpeg_build/lib/pkgconfig"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$HOME/FFMPEG/ffmpeg_build/lib:/opt/VES/lib64"