-
Notifications
You must be signed in to change notification settings - Fork 1
/
compile-ffmpeg-patch.sh
332 lines (288 loc) · 11.1 KB
/
compile-ffmpeg-patch.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/bin/sh -e
#This script will compile and install a static ffmpeg build with support for nvenc un ubuntu.
#See the prefix path and compile options if edits are needed to suit your needs.
# Based on: https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu
# Based on: https://gist.github.com/Brainiarc7/3f7695ac2a0905b05c5b
# Based on: https://github.com/ilyaevseev/ffmpeg-build-static/
# Globals
NASM_VERSION="2.14rc15"
YASM_VERSION="1.3.0"
LAME_VERSION="3.100"
OPUS_VERSION="1.2.1"
CUDA_VERSION="10.0.130-1"
CUDA_REPO_KEY="http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub"
CUDA_DIR="/usr/local/cuda"
WORK_DIR="$HOME/ffmpeg-build-static-sources"
DEST_DIR="$HOME/ffmpeg-build-static-binaries"
# Nvidia Patch Globals
backup_path="/opt/nvidia/libnvidia-encode-backup"
drv_ver=`/usr/bin/nvidia-smi --query-gpu=driver_version --format=csv,noheader,nounits | head -n 1 | sed 's/\..*//'`
driver_dir='/usr/lib/nvidia-$drv_ver'
mkdir -p "$WORK_DIR" "$DEST_DIR" "$DEST_DIR/bin"
export PATH="$DEST_DIR/bin:$PATH"
MYDIR="$(cd "$(dirname "$0")" && pwd)" #"
#### Routines ################################################
Wget() { wget -cN "$@"; }
PKGS="autoconf automake libtool patch make cmake bzip2 unzip wget git mercurial"
installAptLibs() {
sudo apt-get update
sudo apt-get -y --force-yes install $PKGS \
build-essential pkg-config texi2html software-properties-common libssl-dev \
libass-dev libfreetype6-dev libgpac-dev libsdl1.2-dev libtheora-dev libva-dev \
libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev libxcb-xfixes0-dev zlib1g-dev
}
installLibs() {
echo "Installing prerequisites"
. /etc/os-release
case "$ID" in
ubuntu | linuxmint ) installAptLibs ;;
* ) echo "ERROR: only Ubuntu 16.04 or 18.04 are supported now."; exit 1;;
esac
}
installCUDASDKdeb() {
UBUNTU_VERSION="$1"
local CUDA_REPO_URL="https://developer.download.nvidia.com/compute/cuda/repos/ubuntu${UBUNTU_VERSION}/x86_64/cuda-repo-ubuntu${UBUNTU_VERSION}_${CUDA_VERSION}_amd64.deb"
Wget "$CUDA_REPO_URL"
sudo dpkg -i "$(basename "$CUDA_REPO_URL")"
sudo apt-key adv --fetch-keys "$CUDA_REPO_KEY"
sudo apt-get -y update
sudo apt-get -y install cuda --allow-unauthenticated
sudo env LC_ALL=C.UTF-8 add-apt-repository -y ppa:graphics-drivers/ppa
sudo apt-get -y update
sudo apt-get -y upgrade
}
installCUDASDK() {
echo "Installing CUDA and the latest driver repositories from repositories"
cd "$WORK_DIR/"
. /etc/os-release
case "$ID-$VERSION_ID" in
ubuntu-16.04 ) installCUDASDKdeb 1604 ;;
ubuntu-18.04 ) installCUDASDKdeb 1804 ;;
linuxmint-19.1)installCUDASDKdeb 1804 ;;
* ) echo "ERROR: only Ubuntu 16.04 or 18.04 are supported now."; exit 1;;
esac
}
installNvidiaSDK() {
echo "Installing the nVidia NVENC SDK."
cd "$WORK_DIR/"
test -d nv-codec-headers || git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers
git pull
make
make install PREFIX="$DEST_DIR"
patch --force -d "$DEST_DIR" -p1 < "$MYDIR/dynlink_cuda.h.patch" || :
}
compileNasm() {
echo "Compiling nasm"
cd "$WORK_DIR/"
Wget "http://www.nasm.us/pub/nasm/releasebuilds/$NASM_VERSION/nasm-$NASM_VERSION.tar.gz"
tar xzvf "nasm-$NASM_VERSION.tar.gz"
cd "nasm-$NASM_VERSION"
./configure --prefix="$DEST_DIR" --bindir="$DEST_DIR/bin"
make -j$(nproc)
make install distclean
}
compileLibX264() {
echo "Compiling libx264"
cd "$WORK_DIR/"
Wget http://download.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
rm -rf x264-snapshot*/ || :
tar xjvf last_x264.tar.bz2
cd x264-snapshot*
./configure --prefix="$DEST_DIR" --bindir="$DEST_DIR/bin" --enable-static --enable-pic
make -j$(nproc)
make install distclean
}
compileLibX265() {
if cd "$WORK_DIR/x265/"; then
hg pull
hg update
else
cd "$WORK_DIR/"
hg clone https://bitbucket.org/multicoreware/x265
fi
cd "$WORK_DIR/x265/build/linux/"
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$DEST_DIR" -DENABLE_SHARED:bool=off ../../source
make -j$(nproc)
make install
# forward declaration should not be used without struct keyword!
sed -i.orig -e 's,^ *x265_param\* zoneParam,struct x265_param* zoneParam,' "$DEST_DIR/include/x265.h"
}
compileLibAom() {
cd "$WORK_DIR/"
test -d aom/.git || git clone --depth 1 https://aomedia.googlesource.com/aom
cd aom
git pull
mkdir ../aom_build
cd ../aom_build
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$DEST_DIR" -DENABLE_SHARED=off -DENABLE_NASM=on ../aom
make -j$(nproc)
make install
}
compileLibfdkaac() {
echo "Compiling libfdk-aac"
cd "$WORK_DIR/"
Wget -O fdk-aac.zip https://github.com/mstorsjo/fdk-aac/zipball/master
unzip -o fdk-aac.zip
cd mstorsjo-fdk-aac*
autoreconf -fiv
./configure --prefix="$DEST_DIR" --disable-shared
make -j$(nproc)
make install distclean
}
compileLibMP3Lame() {
echo "Compiling libmp3lame"
cd "$WORK_DIR/"
Wget "http://downloads.sourceforge.net/project/lame/lame/$LAME_VERSION/lame-$LAME_VERSION.tar.gz"
tar xzvf "lame-$LAME_VERSION.tar.gz"
cd "lame-$LAME_VERSION"
./configure --prefix="$DEST_DIR" --enable-nasm --disable-shared
make -j$(nproc)
make install distclean
}
compileLibOpus() {
echo "Compiling libopus"
cd "$WORK_DIR/"
Wget "http://downloads.xiph.org/releases/opus/opus-$OPUS_VERSION.tar.gz"
tar xzvf "opus-$OPUS_VERSION.tar.gz"
cd "opus-$OPUS_VERSION"
#./autogen.sh
./configure --prefix="$DEST_DIR" --disable-shared
make -j$(nproc)
make install distclean
}
compileLibVpx() {
echo "Compiling libvpx"
cd "$WORK_DIR/"
test -d libvpx || git clone https://chromium.googlesource.com/webm/libvpx
cd libvpx
git pull
./configure --prefix="$DEST_DIR" --disable-examples --enable-runtime-cpu-detect --enable-vp9 --enable-vp8 \
--enable-postproc --enable-vp9-postproc --enable-multi-res-encoding --enable-webm-io --enable-better-hw-compatibility \
--enable-vp9-highbitdepth --enable-onthefly-bitpacking --enable-realtime-only \
--cpu=native --as=nasm
make -j$(nproc)
make install clean
}
compileFfmpeg(){
echo "Compiling ffmpeg"
cd "$WORK_DIR/"
test -d FFmpeg || git clone https://github.com/FFmpeg/FFmpeg -b master
cd FFmpeg
git pull
export PATH="$CUDA_DIR/bin:$PATH" # ..path to nvcc
PKG_CONFIG_PATH="$DEST_DIR/lib/pkgconfig" \
./configure \
--pkg-config-flags="--static" \
--prefix="$DEST_DIR" \
--bindir="$DEST_DIR/bin" \
--extra-cflags="-I $DEST_DIR/include -I $CUDA_DIR/include/" \
--extra-ldflags="-L $DEST_DIR/lib -L $CUDA_DIR/lib64/" \
--extra-libs="-lpthread" \
--enable-openssl \
--enable-cuda-sdk \
--enable-cuvid \
--enable-libnpp \
--enable-gpl \
--enable-libass \
--enable-libfdk-aac \
--enable-vaapi \
--enable-libfreetype \
--enable-libtheora \
--enable-libvorbis \
--enable-libvpx \
--enable-libx264 \
--enable-libx265 \
--enable-libmp3lame \
--enable-libopus \
--enable-nonfree \
--enable-libaom \
--enable-nvenc
make -j$(nproc)
make install distclean
hash -r
}
patchNvidia() {
echo "Patching Nvidia Drivers"
set -euo pipefail
test -d "$driver_dir" || driver_dir="/usr/lib/x86_64-linux-gnu" # ..Compatible Linux
test -d "$driver_dir" || { echo "ERROR: cannot detect driver directory"; exit 1; }
declare -A patch_list=(
["375.39"]='s/\x85\xC0\x89\xC5\x75\x18/\x29\xC0\x89\xC5\x90\x90/g'
["390.77"]='s/\x85\xC0\x89\xC5\x75\x18/\x29\xC0\x89\xC5\x90\x90/g'
["390.87"]='s/\x85\xC0\x89\xC5\x75\x18/\x29\xC0\x89\xC5\x90\x90/g'
["396.24"]='s/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
["396.26"]='s/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
["396.37"]='s/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
["396.54"]='s/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
["410.48"]='s/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
["410.57"]='s/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
["410.73"]='s/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
["410.78"]='s/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
["410.79"]='s/\x85\xC0\x89\xC5\x0F\x85\x96\x00\x00\x00/\x29\xC0\x89\xC5\x90\x90\x90\x90\x90\x90/g'
["415.18"]='s/\x00\x00\x00\x84\xc0\x0f\x84\x40\xfd\xff\xff/\x00\x00\x00\x84\xc0\x90\x90\x90\x90\x90\x90/g'
["415.25"]='s/\x00\x00\x00\x84\xc0\x0f\x84\x40\xfd\xff\xff/\x00\x00\x00\x84\xc0\x90\x90\x90\x90\x90\x90/g'
["415.27"]='s/\x00\x00\x00\x84\xc0\x0f\x84\x40\xfd\xff\xff/\x00\x00\x00\x84\xc0\x90\x90\x90\x90\x90\x90/g'
)
declare -A object_list=(
["375.39"]='libnvidia-encode.so'
["390.77"]='libnvidia-encode.so'
["390.87"]='libnvidia-encode.so'
["396.24"]='libnvidia-encode.so'
["396.26"]='libnvidia-encode.so'
["396.37"]='libnvidia-encode.so'
["396.54"]='libnvidia-encode.so'
["410.48"]='libnvidia-encode.so'
["410.57"]='libnvidia-encode.so'
["410.73"]='libnvidia-encode.so'
["410.78"]='libnvidia-encode.so'
["410.79"]='libnvidia-encode.so'
["415.18"]='libnvcuvid.so'
["415.25"]='libnvcuvid.so'
["415.27"]='libnvcuvid.so'
)
NVIDIA_SMI="$(which nvidia-smi)"
if ! driver_version=$("$NVIDIA_SMI" --query-gpu=driver_version --format=csv,noheader,nounits | head -n 1) ; then
echo 'Something went wrong. Check nvidia driver'
exit 1;
fi
echo "Detected nvidia driver version: $driver_version"
if [[ ! "${patch_list[$driver_version]+isset}" || ! "${object_list[$driver_version]+isset}" ]]; then
echo "Patch for this ($driver_version) nvidia driver not found." 1>&2
echo "Available patches for: " 1>&2
for drv in "${!patch_list[@]}"; do
echo "$drv" 1>&2
done
exit 1;
fi
patch="${patch_list[$driver_version]}"
object="${object_list[$driver_version]}"
if [[ ! -f "$backup_path/$object.$driver_version" ]]; then
echo "Attention! Backup not found. Copy current $object to backup."
mkdir -p "$backup_path"
cp -p "$driver_dir/$object.$driver_version" \
"$backup_path/$object.$driver_version"
fi
sha1sum "$backup_path/$object.$driver_version"
sed "$patch" "$backup_path/$object.$driver_version" > \
"$driver_dir/$object.$driver_version"
sha1sum "$driver_dir/$object.$driver_version"
ldconfig
echo "Patched!"
fi
}
installLibs
installCUDASDK
installNvidiaSDK
compileNasm
compileLibX264
compileLibX265
compileLibAom
compileLibVpx
compileLibfdkaac
compileLibMP3Lame
compileLibOpus
compileFfmpeg
# TODO: patchNvidia
echo "Complete!"
## END ##