Video Compression

VideoNerd

Content

Prerequisites before Compilation AV1 Reference Codec on Ubuntu 16.04 or Higher

Prerequisites before Compilation AV1 Reference Codec on Windows

Getting and Compilation of  AV1 Sources

Compilation for Ubuntu

Compilation for Windows

Running AV1 Reference Encoder (aomenc)

Single-Pass VBR Encoding 

          I-Only Stream

Dual Pass Encoding

Constant QP Encoding

Screen Content Coding

        Disabling In-Loop Filters

Running AV1 Reference Decoder (aomdec)

Using AV1 Encoder by means of FFMPEG

Using AV1 Dav1d decoder by means of ffmpeg

 

Prerequisites before Compilation AV1 Reference Codec on Ubuntu 16.04 or Higher

  • Check that ‘git’ installed, otherwise apply ‘sudo apt-get install git’
  • Check that ‘cmake’ version is 3.5 or higher, otherwise ‘sudo apt-get install cmake’
  • Check that ‘perl’ installed, otherwise ‘sudo apt-get install perl’
  • Check that ‘yasm’ installed, otherwise ‘sudo apt-get install yasm’

 

Prerequisites before Compilation AV1 Reference Codec on Windows

 

Getting and Compilation of  AV1 Sources

      git clone  https://aomedia.googlesource.com/aom

As a result of the cloning the directory ‘aom’ is created

Compilation for Ubuntu

  • Apply ‘cmake’

My favorite way: go to the sub-folder ‘aom/build/cmake’ (remove CMakeCache.txt if exist) and apply the following command:  cmake ../.. 

          Generally, go to a specified folder and apply:

cmake  path-to-CMakeLists.txt 

Notice ‘CMakeLists.txt’ is located at ‘/aom/’, therefore in the first example we use ‘../..’

If the compilation fails then it’s worth looking at ‘Readme.md’ and check that all prerequisites are available.

  •  Apply ‘make’

As a result aomenc and aomdec are created in the current directory (in our case at the place  /aom/build/cmake).

 

Note:  For  compilation in debug-mode use

cmake  path-to-CMakeLists.txt  -DCMAKE_BUILD_TYPE=Debug

 

Compilation for Windows

You need create Visual Studio solution, go to aom/build directory and run for example “cmake .. -G “Visual Studio 16 2019”

Open the solution (AOM.sln) and build the project ‘aomenc’ .

The exe-files aomenc.exe and aomdec.exe are created at build\Release

 

Running AV1 Reference Encoder (aomenc)

Useful ‘aomenc’  command line parameters:

    •    -o <arg>                               Output filename (can be ‘/dev/null’ if profiling only needed and in case of dual pass)
    •  -q                                             Do not print progress status
    • -w <arg>                                   Frame width in pixels
    •  -h <arg>                                   Frame height in pixels
    • --end-usage=<arg>              Rate control mode: vbr, cbr, cq (constant quality), q (constant QP)
    • --target-bitrate=<arg>        Bitrate (in units of kbps)
    • --kf-min-dist=<arg>            Minimum keyframe interval (in frames)
    • --kf-max-dist=<arg>            Maximum keyframe interval (in frames)
    • --disable-kf                     Disable keyframe placement, no keyframe is generated except the very first frame, actually scene change detection is disabled.
    • --cpu-used=<arg>               CPU Used (0..8), ‘0’ corresponding to ‘veryslow’ preset and ‘8’ to ‘ultrafast’ preset. Generally speaking the preset  controls quality/encoding time tradeoff
    • --tile-columns=<arg>           Number of tile columns
    • --tile-rows=<arg>                 Number of tile rows
    • --tile-loopfilter-v=<arg>      Enable loop filter across vertical tile boundary, 0 or 1 (default 1)
    • --tile-loopfilter-h=<arg>      Enable loop filter across horizontal tile boundary, 0 or 1 (default 1)
    • --aq-mode=<arg>                Adaptive quantization mode (0: off (default), 1: variance, 2: complexity, 3: cyclic refresh)
    • --sb-size=<arg>                    Superblock size to use: dynamic, 64, 128
    • --disable-tempmv=<arg>     Disable temporal mv prediction (default is 0), recommended to set to enchance error resilience
    • --limit==<arg>                      Number of frames to encode
    • -t  <arg>                                  Number of threads
    • -p  <arg>                                 Number of passes
    • --lag-in-frames=0                   Disable look-ahead, relevant for low-latency applications
    • –enable-cdef =0                     Disable Constrained Directional Enhancement Filter, by default enabled
    • –enable-restoration=0 or 1    Enable the loop restoration filter (0: false (default in Realtime mode), 1: true (default in Non-realtime mode))

Single-Pass VBR Encoding 

Encode first 10 frames, VBR mode, single pass, target bitrate 200kbps, gop length = 20 frames, frame rate 24 fps, output container – ‘webm’:

./aomenc -p 1 --webm -o test.webm -w 352 -h 288 --limit=10 --fps=24000/1000 --end-usage=vbr --aq-mode=1 --kf-min-dist=20 --kf-max-dist=20 --target-bitrate=200 container_cif.yuv  

  • To speed-up encoding use ‘cpu-used’ = 4:

./aomenc -p 1 --webm -o test.webm -w 352 -h 288 --limit=30 --fps=24000/1000 --cpu-used=4 --end-usage=vbr       --aq-mode=1 --kf-min-dist=20 --kf-max-dist=20 --target-bitrate=200  container_cif.yuv

  • If increase number of threads to 8 and use tiling then encoding process is parallelized:

                 -t 8 --cpu-used=2 --tile-columns=1 --tile-rows=0

Notes:

  • --tile-columns and --tile-rows denote  log2 of tile columns and rows
  • To get better convergence to a target bitrate use ‘--deltaq-mode=1’ to enable varying of quantization index within a frame.

 

  • To measure processing time in Windows use PowerShell command prompt:

Measure-Command {.\aomenc.exe -p 1 --webm -o test.webm -w 352 -h 288 --limit=30 --fps=24000/1000 --cpu-used=4 --end-usage=vbr --aq-mode=1 --kf-min-dist=20 --kf-max-dist=20 --target-bitrate=200 C:\Tools\yuv\akiyo_cif.y4m }
Pass 1/1 frame 30/11 12179B 1874232 us 16.01 fps [ETA 0:00:05] 67F 5F 303F 5F 123F 24F Pass 1/1 frame 30/30 26976B 7193b/f 172632b/s 5068669 us (5.92 fps)

Days : 0
Hours : 0
Minutes : 0
Seconds : 5
Milliseconds : 149
Ticks : 51495198
TotalDays : 5.96009236111111E-05
TotalHours : 0.00143042216666667
TotalMinutes : 0.08582533
TotalSeconds : 5.1495198
TotalMilliseconds : 5149.5198

 

For low latency i recommend to reduce look-ahead buffer to single frame ( –lag-in-frames=1)

Measure-Command {.\aomenc.exe -p 1 --webm -o test.webm -w 352 -h 288 --limit=30 --fps=24000/1000 --cpu-used=4 --end-usage=vbr --lag-in-frames=1 --aq-mode=1 --kf-min-dist=20 --kf-max-dist=20 --target-bitrate=200 C:\Tools\yuv\akiyo_cif.y4m }
Pass 1/1 frame 30/30 12374B 3299b/f 79176b/s 2059658 us (14.57 fps)

Days : 0
Hours : 0
Minutes : 0
Seconds : 2
Milliseconds : 119
Ticks : 21195625
TotalDays : 2.45319733796296E-05
TotalHours : 0.000588767361111111
TotalMinutes : 0.0353260416666667
TotalSeconds : 2.1195625
TotalMilliseconds : 2119.5625

 

I-Only Stream

To generate I-only stream use ‘–kf-min-dist=1 –kf-max-dist=1

aomenc -p 1 –webm -o test.webm -w 352 -h 288 –limit=100 –cpu-used=4 –fps=24000/1000 –end-usage=vbr –aq-mode=1 –kf-min-dist=1 –kf-max-dist=1 –target-bitrate=200 –enable-cdef=0 akiyo_cif.yuv

 

For verification use ffprobe:

ffprobe -hide_banner -v panic -select_streams v:0 -show_frames test.webm | findstr pict_type

 

Note:

Because AV1 supports 56 directional intra prediction modes, plus several non-directional ones, there is a requirement to use heuristics to restrict RDO search (early termination).
In the paper “A Fast Intra Mode Decision Based on Accuracy of Rate Distortion Model for AV1 Intra Encoding”, Jinwoo Jeong et al. 2019; the authors use the variation and Welford’s online method coupled with the RDO to choose near best intra modes.
The authors reported a gain in encoding times of I-only sequences above 8% with a negligible penalty in BD-Rate.

 

Dual Pass Encoding

./aomenc -v --pass=1 --passes=2 --fpf="container.stats" -o /dev/null -w 352 -h 288  --limit=100 --fps=24000/1000 --cpu-used=2 --end-usage=vbr --aq-mode=1 --kf-min-dist=20 --kf-max-dist=20 --psnr   --end-usage=vbr --target-bitrate=200   container_cif.yuv


./aomenc -v --pass=2 --passes=2 --fpf="container.stats" -o test.webm -w 352 -h 288  --limit=100 --fps=24000/1000 --cpu-used=2 --end-usage=vbr --aq-mode=1 --kf-min-dist=20 --kf-max-dist=20 --psnr   --end-usage=vbr --target-bitrate=200  container_cif.yuv

 

Constant QP Encoding

--end-usage=q   --deltaq-mode=0    --cq-level=x

Example: To encode with constant QP=20 use:

aomenc -p 1 --webm -o test_qp20.webm -w 352 -h 288 --limit=200 --fps=30000/1000 --end-usage=q --deltaq-mode=0 --cq-level=20 --kf-min-dist=20 --kf-max-dist=20   --lag-in-frames=0 -t 8 --cpu-used=2 --tile-columns=1 --tile-rows=0   akiyo_cif.y4m

 

Screen Content Coding

AV1 has advanced tools for screen content:  IntraBC and Palette, you need enable these two tools to exploit screen coding more efficiently:

--tune-content=screen  --enable-intrabc=1 --enable-palette=1

For example, for the following screen content activation of IntraBC and Palette modes (--enable-intrabc=1 --enable-palette=1) , reduces file size by ~10% given constant QP=20, 30

 

Disabling In-Loop Filters

AV1 uses three in-loop filters:

  • Deblocking Filter – to reduce blocking artifacts
  • Constrained Directional Enhancement Filter (CDEF) – to reduce ringing while preserving details
  • Loop Restoration Filter – to reduce blurring artifacts

By default CDEF is enabled, to disable CDEF use ‘–enable-cdef=0‘ . According to my measures  disabling CDEF improves encoding times by 4-5%

aomenc -p 1 –webm -o test.webm -w 352 -h 288 –limit=100 –cpu-used=4 –fps=24000/1000 –end-usage=vbr –aq-mode=1 –kf-min-dist=20 –kf-max-dist=20 –target-bitrate=200 –enable-cdef=0 akiyo_cif.yuv

 

To disable Loop Restoration use ‘–enable-restoration=0

aomenc -p 1 –webm -o test.webm -w 352 -h 288 –limit=100 –cpu-used=4 –fps=24000/1000 –end-usage=vbr –aq-mode=1 –kf-min-dist=20 –kf-max-dist=20 –target-bitrate=200 –enable-restoration=0 akiyo_cif.yuv

 

 

 

Running AV1 Reference Decoder (aomdec)

Output file in the format yuv420p (–rawvideo)

aomdec.exe --rawvideo -o test_352x288.yuv   test_qp20.webm

 

Using AV1 Encoder by means of FFMPEG

Check that your version of ffmpeg supports libaom-av1

ffmpeg -codecs | findstr av1

DEV.L. av1 Alliance for Open Media AV1 (decoders: libdav1d libaom-av1 ) (encoders: libaom-av1 )

 

Note:  Relationship between the preset, the quality and encoding speed is elaborated in Jan Ozer‘s paper “Choosing Preset”

 

Example [ very slow running, bitrate 5Mbps, peak-bitrate 6Mbps, GOP size 60 frames ]

ffmpeg -y -s 1920x1080 -i .\yuv\crowdrun1080p50fps.yuv -c:v libaom-av1 -strict -2 -r 50 -b:v 5M -maxrate 6M -bufsize 1M -g 60 -keyint_min 60 -sc_threshold 0 -row-mt 1 -tile-columns 1 -tile-rows 0 -threads 2 -lag-in-frames 1 -cpu-used 8 crowdrun.mkv

 

Notes:

-row-mt 1 enables row based multi-threading, according to Jan Ozer’s analysis if -row-mt 1 is set then encoding times are reduced by 25%-30% with non-significant penalty on video quality.

-lag-in-frames number of frames which the encoder keeps in flight at any one time for lookahead purposes. For low-latency applications it’s recommended to set -lag-in-frames=0 . Otherwise, according to Jan Ozer’s analysis -lag-in-frames=20 is sufficient, setting more than 20 frames for look-ahead increases encoding times and don’t provide a significant gain in quality.

 

 

Example [activation automatic alt-reference frames]:

the parameter -auto-alt-ref enables selection of multiple frames for reference. According to Jan Ozer research (the paper “Decision: Auto-Alt-Ref”) if auto-alt-ref is disabled the encoding speed increases by ~30%, but visual quality drops 5-7% in VMAf scores

ffmpeg -s 1920x1080 -i Fifa17.yuv -r 60 -c:v libaom-av1 -b:v 5M -g 60 -keyint_min 60 -cpu-used 8 -auto-alt-ref 1 -y test.webm

 

 

Example [ choose preset with cpu-used] 

Generally speaking the preset controls quality/encoding time tradeoff.  In ffmpeg libaom encoder the preset is specified by ‘cpu-used’ , where cpu-used=0 corresponds to slowest coding and best quality respectively, and cpu-used=8 corresponds to fastest coding and worst quality. 

Medium preset is cpu-used=4

ffmpeg -y -s 1920x1080 -i .\yuv\test1080p.yuv -r 60 -c:v libaom-av1 -b:v 5M -g 60 -keyint_min 60 -cpu-used 4 -auto-alt-ref 0  test.webm

 

 

Example [ using 2×2 tiling]

According to Gan Ozer’s analysis the 2×2 uniform tiling of  video provides ~30% gain in encoding times and 0.5 points of penalty in VMAF scores. More dense tiling (finer 2×2) would cause more significant gain in encoding times up to 50%, but penalty in video quality is also significant. The rule of thumb – use 2×2 tiling (-tile-columns 1 -tile-rows 1) .

Frankly speaking i could not reproduce any encoding gain with 2×2 tiling versus non-tiling on my laptop.

 

ffmpeg -y -s 1920x1080 -i .\yuv\crowdrun1080p50fps.yuv -r 50 -c:v libaom-av1 -b:v 5M -g 50 -keyint_min 50 -threads 4 -cpu-used 4 -auto-alt-ref 0 -tile-columns 1 -tile-rows 1 -lag-in-frames 0 -vframes 100 test-crowd.webm

 

 

Using AV1 Dav1d decoder by means of ffmpeg

The sw-decoder Dav1d is already a part of ffmpeg (libdav1d):

 

ffmpeg -hide_banner -codecs | findstr dav1

DEV.L. av1    Alliance for Open Media AV1 (decoders: libdav1d libaom-av1 av1 av1_cuvid av1_qsv ) (encoders: libaom-av1 librav1e libsvtav1 )

 

To apply Dav1d decoder i use the following command:

 

ffmpeg -c:v libdav1d -i akiyo.ivf -f rawvideo -pix_fmt yuv420p -vsync 0 test_cif.yuv
...
[libdav1d @ 000001662f2f4500] libdav1d 1.0.0-84-g5b07b42
...

Verification:


ffplay -video_size 352x288 -i test_cif.yuv

24 Responses

  1. Very good written information. It will be useful to anyone who employess it, including me. Keep doing what you are doing – looking forward to more posts.

  2. Thank you so much for providing individuals with an exceptionally marvellous possiblity to read critical reviews from this website. It can be very kind and as well , jam-packed with a lot of fun for me personally and my office co-workers to search your site at a minimum 3 times a week to study the latest issues you have got. Not to mention, we are at all times fulfilled concerning the superb tricks served by you. Selected 3 facts in this article are clearly the simplest I’ve had.

  3. you are really a good webmaster. The web site loading speed is amazing. It seems that you’re doing any unique trick. Also, The contents are masterwork. you’ve done a fantastic job on this topic!

  4. Along with almost everything that appears to be building inside this subject material, many of your opinions tend to be very exciting. However, I beg your pardon, because I can not give credence to your whole plan, all be it stimulating none the less. It looks to everybody that your comments are not completely rationalized and in reality you are your self not fully convinced of your argument. In any event I did appreciate reading it.

  5. I’m extremely impressed with your writing skills and also with the layout on your weblog. Is this a paid theme or did you modify it yourself? Anyway keep up the nice quality writing, it’s rare to see a great blog like this one today..

  6. Thank you for the sensible critique. Me & my neighbor were just preparing to do a little research about this. We got a grab a book from our local library but I think I learned more clear from this post. I am very glad to see such excellent info being shared freely out there.

  7. I was wondering if you ever thought of changing the layout of your site? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having 1 or two pictures. Maybe you could space it out better?

Leave a Reply

Your email address will not be published. Required fields are marked *