diff options
author | Stefano Sabatini <stefano.sabatini-lala@poste.it> | 2008-09-25 23:23:13 +0400 |
---|---|---|
committer | Stefano Sabatini <stefano.sabatini-lala@poste.it> | 2008-09-25 23:23:13 +0400 |
commit | 05b90fc0c59aaac40af484f3d5a617a0404b1b4e (patch) | |
tree | bee469192cb8d4dda0b9d626a3a92830a8298b86 /libavutil/rational.c | |
parent | 5f129a05bf69d5655ae6d6ea82ca1a97f0b1913f (diff) |
Implement av_nearer_q() and av_find_nearest_q_idx() functions.
Originally committed as revision 15415 to svn://svn.ffmpeg.org/ffmpeg/trunk
Diffstat (limited to 'libavutil/rational.c')
-rw-r--r-- | libavutil/rational.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/libavutil/rational.c b/libavutil/rational.c index 96a4c5462e..cac3ff5a99 100644 --- a/libavutil/rational.c +++ b/libavutil/rational.c @@ -101,3 +101,28 @@ AVRational av_d2q(double d, int max){ return a; } + +int av_nearer_q(AVRational q, AVRational q1, AVRational q2) +{ + /* n/d is q, a/b is the median between q1 and q2 */ + int64_t a = q1.num * (int64_t)q2.den + q2.num * (int64_t)q1.den; + int64_t b = 2 * (int64_t)q1.den * q2.den; + + /* rnd_up(a*d/b) > n => a*d/b > n */ + int64_t x_up = av_rescale_rnd(a, q.den, b, AV_ROUND_UP); + + /* rnd_down(a*d/b) < n => a*d/b < n */ + int64_t x_down = av_rescale_rnd(a, q.den, b, AV_ROUND_DOWN); + + return ((x_up > q.num) - (x_down < q.num)) * av_cmp_q(q2, q1); +} + +int av_find_nearest_q_idx(AVRational q, const AVRational* q_list) +{ + int i, nearest_q_idx = 0; + for(i=0; q_list[i].den; i++) + if (av_nearer_q(q, q_list[i], q_list[nearest_q_idx]) > 0) + nearest_q_idx = i; + + return nearest_q_idx; +} |