diff options
author | Michael Niedermayer <[email protected]> | 2002-11-20 13:08:04 +0000 |
---|---|---|
committer | Michael Niedermayer <[email protected]> | 2002-11-20 13:08:04 +0000 |
commit | 5d3cea3a86a9b994591998fb05cacfc16ae4852d (patch) | |
tree | f9611b77616e2f574eb57c531cafa62d916c58a4 /libavcodec/common.c | |
parent | 67d06418daaee0ec43418f5e9a6288fc12203f54 (diff) |
aspect ratio cleanup
Originally committed as revision 1254 to svn://svn.ffmpeg.org/ffmpeg/trunk
Diffstat (limited to 'libavcodec/common.c')
-rw-r--r-- | libavcodec/common.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/libavcodec/common.c b/libavcodec/common.c index 2344dc6be3..40ba49811d 100644 --- a/libavcodec/common.c +++ b/libavcodec/common.c @@ -326,3 +326,29 @@ int ff_gcd(int a, int b){ 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; +} |