Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Waifu2x scaling up vs. Bilinear vs. Bicubic objective measurement
#!/bin/sh
## Christopher Kennedy Feb 2020
## Crunchyroll Waifu2x upscaling objective measurement
##
## Note you must install waifu2x and run this in the waifu2x directory,
## plus have an FFmpeg with VMAF enabled (must build from scratch)
##
## Dependencies:
## https://ffmpeg.org
## https://github.com/Netflix/vmaf
## https://github.com/nagadomi/waifu2x
# The goal is to compare Waifu2x upscale with an actual original at that new scale Waifu2x creates.
# To do so we use a 1080p mezz downscaled then upscaled again
#
# Note: These are done without compression,
# all raw through the process into VMAF calcs to avoid conflation of compression loss.
## We used Boruto Season 1 Episode 54 where Sasuke first appears
# 1. Choose a mezzanine that is 1080p HD high quality
mezz_1080p=borutoE54S1.mp4
## ADJUST FPS to VIDEO
fps=23.976
SCALE_NULL="null"
SCALE_720_BILINEAR="scale=h=720:w=-1:flags=bilinear"
SCALE_720_BICUBIC="scale=h=720:w=-1:flags=bicubic"
SCALE_360_BICUBIC="scale=h=360:w=-1:flags=bicubic"
# 2. scale down mezzanine to 360p SD size via Bicubic scaler into PNG raw images for Waifu2x to use
if [ ! -d "360p" ]; then
mkdir 360p
echo "Extracting $mezz_1080p video with fps $fps into PNG images"
ffmpeg -hide_banner -loglevel warning -i $mezz_1080p -vf $SCALE_360_BICUBIC 360p/%06d.png
echo
fi
# 3. scale mezzanine up to 720p HD size via either A. Waifu2x, B. Bicubic, C. Bilinear
# + run VMAF calculation comparing 1080p HD reference scaled down to 720p vs. each 720p HD upscale mezzanine
## A. waifu2x: create image index file mezzanine.txt, use for waifu2x 720p upscale...
find 360p/ -iname \*.png > mezzanine.txt
### run Waifu2x on 360p PNG images
if [ ! -d waifu2x_png ]; then
if [ -f waifu2x.lua ]; then
mkdir waifu2x_png
echo "Running Waifu2x"
th waifu2x.lua -m noise_scale -noise_level 1 -l mezzanine.txt \
-o waifu2x_png/%06d.png
echo
else
echo "WARNING: missing waifu2x.lua in the current directory!!!"
echo " not running waifu2x scaling"
echo
fi
fi
### run VMAF comparing upscale Waifu2x vs. orignal 1080p mezz downscaled to 720p bilinear
if [ -d waifu2x_png ]; then
echo "Measuring waifu2x scaling via VMAF"
ffmpeg -hide_banner -loglevel warning -f image2 -framerate $fps -i waifu2x_png/%06d.png -i $mezz_1080p \
-threads 0 -filter_complex "[0:v]$SCALE_NULL[enc]; [1:v]$SCALE_720_BICUBIC[ref]; \
[enc][ref]libvmaf=psnr=1:ms_ssim=1:log_fmt=json:log_path=vmaf_stats.log" \
-an -y -f 'null' /dev/null
echo
fi
## B. Bilinear upscale + VMAF calculation
### run VMAF comparing upscale Bilinear vs. orignal 1080p mezz downscaled to 720p bicubic
echo "Measuring Bilinear scaling via VMAF"
ffmpeg -hide_banner -loglevel warning -f image2 -framerate $fps -i 360p/%06d.png -i $mezz_1080p \
-threads 0 -filter_complex "[0:v]$SCALE_720_BILINEAR[enc]; [1:v]$SCALE_720_BICUBIC[ref]; \
[enc][ref]libvmaf=psnr=1:ms_ssim=1:log_fmt=json:log_path=vmaf_stats.log" \
-an -y -f 'null' /dev/null
echo
## C. Bicubic upscale + VMAF calculation
### run VMAF comparing upscale Bicubic vs. orignal 1080p mezz downscaled to 720p bicubic
echo "Measuring Bicubic scaling via VMAF"
ffmpeg -hide_banner -loglevel warning -f image2 -framerate $fps -i 360p/%06d.png -i $mezz_1080p \
-threads 0 -filter_complex "[0:v]$SCALE_720_BICUBIC[enc]; [1:v]$SCALE_720_BICUBIC[ref]; \
[enc][ref]libvmaf=psnr=1:ms_ssim=1:log_fmt=json:log_path=vmaf_stats.log" \
-an -y -f 'null' /dev/null
echo
echo "Finished with $mezz_1080p upscale quality loss comparisons"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment