Mercurial > libavcodec.hg
diff common.c @ 880:1c32039e7215 libavcodec
aspect ratio cleanup
author | michaelni |
---|---|
date | Wed, 20 Nov 2002 13:08:04 +0000 |
parents | 058194d7ade6 |
children | 1f9afd8b9131 |
line wrap: on
line diff
--- a/common.c Wed Nov 20 07:32:18 2002 +0000 +++ b/common.c Wed Nov 20 13:08:04 2002 +0000 @@ -326,3 +326,29 @@ if(b) return ff_gcd(b, a%b); else return a; } + +void ff_float2fraction(int *nom_arg, int *denom_arg, double f, int max){ + double best_diff=1E10, diff; + int best_denom=1, best_nom=1; + int nom, denom, gcd; + + //brute force here, perhaps we should try continued fractions if we need large max ... + for(denom=1; denom<=max; denom++){ + nom= (int)(f*denom + 0.5); + if(nom<=0 || nom>max) continue; + + diff= ABS( f - (double)nom / (double)denom ); + if(diff < best_diff){ + best_diff= diff; + best_nom= nom; + best_denom= denom; + } + } + + gcd= ff_gcd(best_nom, best_denom); + best_nom /= gcd; + best_denom /= gcd; + + *nom_arg= best_nom; + *denom_arg= best_denom; +}