#!/bin/bash
echo not yet automated
exit 1

wget https://ocv.me/dev/install-gentoo.sh
./install-gentoo.sh 
/c/gentoo/activate


# entered gentoo on networked machine (not necessarily destination arch)
source /etc/profile
env-update
emerge-webrsync
cat >>/etc/portage/make.conf <<EOF

MAKEOPTS="-j$(nproc)"
ACCEPT_LICENSE="*"
EMERGE_DEFAULT_OPTS="--color y --quiet-build y -j 1"
EOF

emerge -v cpuid2cpuflags vim strace
^D


# left gentoo
tar -cC/c gentoo | pixz -7tk > /c/zips/gentoo-01.tpxz7
/c/gentoo/activate


# entered gentoo on networked machine (not necessarily destination arch)
sed -ri 's/^(COMMON_FLAGS=").*/\1-O2 -pipe -march=native"/' /etc/portage/make.conf

# ffmpeg: encode fontconfig iconv libass lzma mp3 opus svg theora threads truetype vorbis wavpack x264 xvid zlib png jpeg gif
# pypy: bzip2 gdbm jit ncurses sqlite
# consider adding (ffmpeg): vaapi
# on ryzen: VIDEO_CARDS="amdgpu radeonsi"
cat >>/etc/portage/make.conf <<'EOF'
USE="pic encode fontconfig iconv libass lzma mp3 opus svg theora threads truetype vorbis wavpack x264 xvid zlib png jpeg gif bzip2 gdbm jit ncurses sqlite X"
VIDEO_CARDS="intel i965"
EOF

# consider dropping: bluray cdio
cat >/etc/portage/package.use/ffmpeg.conf <<'EOF'
media-video/ffmpeg bluray cdio dav1d fdk flite fribidi gpl gmp libtesseract libsoxr libxml2 openssl rubberband srt vidstab vpx x265 zimg zvbi
app-text/tessdata_fast en ja no
EOF

cat >>/etc/portage/package.accept_keywords <<'EOF'
media-video/ffmpeg ~amd64
media-libs/x264 ~amd64
media-libs/x265 ~amd64
media-libs/libvpx ~amd64
media-libs/dav1d ~amd64
app-text/tessdata_fast ~amd64
app-text/tesseract ~amd64
dev-python/pypy-exe ~amd64
dev-python/pypy3-exe ~amd64
virtual/pypy-exe ~amd64
EOF

# just to visualize changes; cancel this
# emerge -vfa ffmpeg

sed -ri '/^CPU_FLAGS_X86=/d' /etc/portage/make.conf
cpuid2cpuflags | sed -r 's/: /="/;s/$/"/' >> /etc/portage/make.conf
emerge -NuDvf @world vim ffmpeg pypy pypy3
^D


# left gentoo
tar -cC/c gentoo | pixz -7tk > /c/zips/gentoo-02.tpxz7
apk add linux-vanilla-dev
cp -pvR /usr/src/linux-headers-* /c/gentoo/usr/src/
ln -s /c/gentoo/usr/src/linux-headers-* /c/gentoo/usr/src/linux
/c/gentoo/activate


# entered gentoo on destination arch (not necessarily networked),
# let each make have two cores, use the rest on parallel packages
cores=$(nproc)
mj=2
pj=$((cores/mj))
sed -ri '/^CPU_FLAGS_X86=/d; s/(MAKEOPTS="-j *)[0-9]+/\1'$mj'/; s/(EMERGE_DEFAULT_OPTS=.* -j *)[0-9]+/\1'$pj/ /etc/portage/make.conf
cpuid2cpuflags | sed -r 's/: /="/;s/$/"/' >> /etc/portage/make.conf
emerge -NuDav @world ffmpeg pypy pypy3


# eof







# ffmpeg tests

ffmpeg -ss 69.9 -i tearsofsteel_4k.mov -vcodec libx264 -tune fastdecode -crf 24 -acodec copy tos-x264-fdec.mkv

time ffmpeg -hide_banner -nostdin -y -i tos-x264-fdec.mkv -map 0:v -t 60 -vcodec libx264 -crf 28 x264-crf28-$(date +%Y-%m%d-%H%M%S).mp4
gentoo  1m3.682s  5m47.237s  x264-crf28-2019-1024-2222xx.mp4  264 - core 157  MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
gentoo  1m2.703s  5m40.950s  x264-crf28-2019-1024-224602.mp4  264 - core 157  MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
alpine  1m3.592s  5m46.172s  x264-crf28-2019-1024-2226xx.mp4  264 - core 152  MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2

time ffmpeg -hide_banner -nostdin -y -i tos-x264-fdec.mkv -map 0:v -t 20 -vcodec libx265 -crf 28 x265-crf28-$(date +%Y-%m%d-%H%M%S).mp4
gentoo  0m47.959s  4m27.107s  x265-crf28-2019-1024-2224xx.mp4  HEVC 2.8  [GCC 8.3.0][64 bit] 8bit+10bit+12bit  MMX2 SSE2Fast LZCNT SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
gentoo  0m43.192s  3m21.613s  x265-crf28-2019-1024-224834.mp4  HEVC 3.2  [GCC 8.3.0][64 bit] 8bit+10bit+12bit  MMX2 SSE2Fast LZCNT SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
alpine  0m48.657s  4m30.332s  x265-crf28-2019-1024-2227xx.mp4  HEVC 3.0  [GCC 8.3.0][64 bit] 8bit              MMX2 SSE2Fast LZCNT SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2

time ffmpeg -hide_banner -nostdin -y -i tos-x264-fdec.mkv -map 0:v -t 30 -threads $(nproc) -vcodec libvpx -b:v 4M -crf 40 -cpu-used 4 vp8-crf40-$(date +%Y-%m%d-%H%M%S).webm
gentoo  0m34.762s  1m17.639s  vp8-crf40-2019-1024-225437.webm  v1.8.0
alpine  0m33.880s  1m18.364s  vp8-crf40-2019-1024-225550.webm  v1.8.0

time ffmpeg -hide_banner -nostdin -y -i tos-x264-fdec.mkv -map 0:v -t 30 -threads $(nproc) -vcodec libvpx-vp9 -row-mt 1 -tile-columns 6 -b:v 0 -crf 42 -cpu-used 5 vp9-crf42-$(date +%Y-%m%d-%H%M%S).webm
gentoo  1m14.704s  4m35.121s  vp9-crf42-2019-1024-230610.webm  vp9-crf42-2019-1024-231137.webm
alpine  1m11.782s  4m29.275s  vp9-crf42-2019-1024-230759.webm  vp9-crf42-2019-1024-231419.webm
