/+junk/build-libffmpeg-for-chromium

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/build-libffmpeg-for-chromium

« back to all changes in this revision

Viewing changes to document.md

  • Committer: Gustav Hartvigsson
  • Date: 2021-06-07 17:31:30 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210607173130-ltls48c5mw8ypt6q
initial coode

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
---
 
2
title: How to build `libffmpeg.so`\linebreak
 
3
       for use in Chrome(ium) and derivatives.
 
4
author: Gustav (Gego of Xaren)
 
5
date: 2020-06-07
 
6
geometry: margin=2cm
 
7
papersize: a4
 
8
mainfont: DejaVu Serif
 
9
fontsize: 12pt
 
10
---
 
11
 
 
12
# Abstract
 
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.
 
16
 
 
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.
 
19
 
 
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.
 
23
 
 
24
The note will conclude with a table that the improvements can make a difference
 
25
in responsiveness.
 
26
 
 
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.
 
34
 
 
35
[^stadia]: Google's cloud gaming service.
 
36
 
 
37
[^hardware]: CPU: AMD FX-8350 @ 4.000GHz, GPU: AMD Radeon RX 570 (8GB).
 
38
 
 
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
 
42
document.
 
43
 
 
44
# How to build `libffmpeg.so`
 
45
 
 
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
 
48
be used.
 
49
 
 
50
```bash
 
51
# change directory to some place.
 
52
wget https://ffmpeg.org/releases/ffmpeg-snapshot.tar.bz2
 
53
tar -xf ffmpeg-snapshot.tar.bz2
 
54
cd ffmpeg
 
55
```
 
56
 
 
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).
 
62
 
 
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:
 
68
 
 
69
```bash
 
70
./configure --enable-nonfree --enable-gpl --enable-version3\
 
71
            --enable-hardcoded-tables\
 
72
            --enable-pic --enable-lto\
 
73
            --arch=amd64 --cpu=bdver4\
 
74
            --disable-doc
 
75
make -j8
 
76
```
 
77
 
 
78
In the code listing above the microarchitecture is provided to the `--cpu=`
 
79
flag.
 
80
 
 
81
Then testing to see if it works:
 
82
 
 
83
```bash
 
84
./ffplay "path to testfile.mp4"
 
85
# and
 
86
./ffplay "path to testfile.webm"
 
87
```
 
88
 
 
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.
 
93
 
 
94
```bash
 
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\
 
102
            --disable-doc
 
103
```
 
104
Note that it may seem that the command has frozen, but just give it a few
 
105
seconds.
 
106
 
 
107
Building of the final library is very easy:
 
108
 
 
109
```bash
 
110
gcc -shared -fPIC\
 
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\
 
119
    -o libffmpeg.so
 
120
```
 
121
 
 
122
# Testing and Results
 
123
TODO
 
124
 
 
125
# Refrences
 
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.
 
130
 
 
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.
 
134
 
 
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.
 
138