2
title: How to build `libffmpeg.so`\linebreak
3
for use in Chrome(ium) and derivatives.
4
author: Gustav (Gego of Xaren)
13
When streaming games to a web browser, the decode lag[^decode-lag] can be very
14
high on older systems when using generic x86_64/AMD64. This can be mitigated
15
by compiling `libffmpeg.so` from source using a specific set of optimisations.
17
[^decode-lag]: The delay that occurs from when the browser gets the data to
18
decode and when the browser renders the image to the screen.
20
This note provides information on how to compiling `ffmpeg` and it's libraries
21
from source and how to produce `libffmpeg.so` on a GNU/Linux system, as the
22
standard `make script` does not produce it.
24
The note will conclude with a table that the improvements can make a difference
27
# Introduction and Motivation
28
The author of was playing games on Stadia[^stadia] and using a plug-in for
29
Chrome(ium) measured that the decode time was higher than optimal when playing
30
intensive games (in the order of 10+ ms) running on his older
31
hardware[^hardware] without hardware acceleration, and the quite high CPU usage.
32
The author wanted to see if using a specific microarchitecture (m-arch)
33
would lower the decode lag.
35
[^stadia]: Google's cloud gaming service.
37
[^hardware]: CPU: AMD FX-8350 @ 4.000GHz, GPU: AMD Radeon RX 570 (8GB).
39
There there is no real documentation on how to produce `libffmpeg.so` by hand,
40
only a patch submitted to ffmpeg that was rejected
41
[(Le Cuirot, 2017)](#ref:LeCuirot). Le Cuirot's patch became basis for this
44
# How to build `libffmpeg.so`
46
The fist step is to download the source code for ffmpeg. This can be done in
47
a number of ways, in this example the archive provided on ffmpeg's homepage will
51
# change directory to some place.
52
wget https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
53
tar -xf ffmpeg-snapshot.tar.bz2
57
In this example the target CPU is the FX-8350, which uses the
58
Piledriver microarchitecture [(Wikipedia, Piledriver)](#ref:WikPiledriver),
59
which is AMD's 15th microarchitecture family. This would make it one of the
60
`bdver` class microarchitectures
61
[GCC Manual (2021)](#ref:GccManual).
63
Configure the build system for ffmpeg to use the correct architecture. This
64
could be a bit of trail-and-error to figure out, when building: target the
65
highest microarchitecture that in the cpu family that may work, (Here other
66
flags are provided to include most things with our build.) and then build the
67
programs and libraries:
70
./configure --enable-nonfree --enable-gpl --enable-version3\
71
--enable-hardcoded-tables\
72
--enable-pic --enable-lto\
73
--arch=amd64 --cpu=bdver4\
78
In the code listing above the microarchitecture is provided to the `--cpu=`
81
Then testing to see if it works:
84
./ffplay "path to testfile.mp4"
86
./ffplay "path to testfile.webm"
89
In this case the test resulted in a segmentation fault, so using `--cpu=bdver3`
90
was tested instead, and worked. The configuration used can be seen below.
91
Additional added a some `-mtune` and `-O2` options to make sure that the
92
binaries are tuned to the architecture and optimised.
95
./configure --enable-nonfree --enable-gpl --enable-version3\
96
--enable-hardcoded-tables\
97
--enable-pic --enable-lto\
98
--extra-cflags='-mtune=bdver3 -O2'\
99
--extra-cxxflags='-mtune=bdver3 -O2'\
100
--extra-objcflags='-mtune=bdver3 -O2'\
101
--arch=amd64 --cpu=bdver3\
104
Note that it may seem that the command has frozen, but just give it a few
107
Building of the final library is very easy:
111
libavcodec/libavcodec.a\
112
libavdevice/libavdevice.a\
113
libavfilter/libavfilter.a\
114
libavformat/libavformat.a\
115
libavutil/libavutil.a\
116
libpostproc/libpostproc.a\
117
libswresample/libswresample.a\
118
libswscale/libswscale.a\
122
# Testing and Results
126
[](){#ref:LeCuirot} _Le Cuirot, J,_ 2017, [FFmpeg-devel] build: Allow libffmpeg
127
to be built for Chromium-based browsers, URL:
128
[https://patchwork.ffmpeg.org/project /ffmpeg/patch/20170728100716.8143-1-chewi@gentoo.org/](https://patchwork.ffmpeg.org/project/ffmpeg/patch/20170728100716.8143-1-chewi@gentoo.org/)
129
, date read: 2021-06-07.
131
[](){#ref:WikPiledriver} _Wikipedia,_ Piledriver (microarchitecture), URL:
132
[https://en.wikipedia.org/wiki/Piledriver_(microarchitecture)](https://en.wikipedia.org/wiki/Piledriver_(microarchitecture))
133
, date read: 2021-06-07.
135
[](){#ref:GccManual} _GCC Manual,_ 2021, version 11.1, pp. 142-152, URL:
136
[https://gcc.gnu.org/onlinedocs/gcc-11.1.0/gcc.pdf](https://gcc.gnu.org/onlinedocs/gcc-11.1.0/gcc.pdf)
137
, date read: 2021-06-07.