Based on code by John Stebbins <jstebbins.hb@gmail.com> Signed-off-by: Michael Niedermayer <michaelni@gmx.at>