Based partially on implementation by Michael Niedermayer <michaelni@gmx.at> in libswresample in FFmpeg. See commits: 7f1ae79d38 24ab1abfb6
7f1ae79d38
24ab1abfb6