[PATCH 04/13] Add a special case for av_cmp_q() when one of the values to be compared is 0/0, and add a test for it.

Stefano Sabatini stefano.sabatini-lala
Thu Sep 30 21:42:56 CEST 2010


---
 libavutil/rational.c |   39 +++++++++++++++++++++++++++++++++++++++
 libavutil/rational.h |    8 ++++++--
 2 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/libavutil/rational.c b/libavutil/rational.c
index a82bc84..e7cca8f 100644
--- a/libavutil/rational.c
+++ b/libavutil/rational.c
@@ -133,3 +133,42 @@ int av_find_nearest_q_idx(AVRational q, const AVRational* q_list)
 
     return nearest_q_idx;
 }
+
+#ifdef TEST
+
+#undef printf
+
+int main(void)
+{
+    int i;
+
+    printf("Testing av_cmp_q()\n");
+    {
+        AVRational rationals[] = {
+            {  1, 0 },  {  1, 1 },
+            { -1, 0 },  {  1, 1 },
+            {  1, 0 },  { -1, 1 },
+            {  1, 0 },  {  1, 0 },
+            {  2, 0 },  {  1, 0 },
+            {  1, 0 },  { -1, 0 },
+            { -1, 0 },  {  1, 0 },
+            { -1, 0 },  { -1, 0 },
+            {  0, 0 },  {  1, 1 },
+            {  0, 0 },  { -1, 1 },
+            {  0, 0 },  {  1, 0 },
+            {  1, 0 },  {  0, 0 },
+            {  0, 0 },  { -1, 0 },
+            {  0, 0 },  {  0, 0 },
+        };
+
+        for (i = 0; i < FF_ARRAY_ELEMS(rationals); i+=2) {
+            printf("%d/%d cmp %d/%d -> %d\n",
+                   rationals[i  ].num, rationals[i  ].den,
+                   rationals[i+1].num, rationals[i+1].den,
+                   av_cmp_q(rationals[i], rationals[i+1]));
+        }
+    }
+    return 0;
+}
+
+#endif /* TEST */
diff --git a/libavutil/rational.h b/libavutil/rational.h
index 21542a8..f70c1b5 100644
--- a/libavutil/rational.h
+++ b/libavutil/rational.h
@@ -28,6 +28,7 @@
 #ifndef AVUTIL_RATIONAL_H
 #define AVUTIL_RATIONAL_H
 
+#include <limits.h>
 #include <stdint.h>
 #include "attributes.h"
 
@@ -43,13 +44,16 @@ typedef struct AVRational{
  * Compare two rationals.
  * @param a first rational
  * @param b second rational
- * @return 0 if a==b, 1 if a>b and -1 if a<b
+ * @return 0 if a==b, 1 if a>b, -1 if a<b, and INT_MIN if one of the
+ * values is of the form 0/0
  */
 static inline int av_cmp_q(AVRational a, AVRational b){
     const int64_t tmp= a.num * (int64_t)b.den - b.num * (int64_t)a.den;
 
     if(tmp) return ((tmp ^ a.den ^ b.den)>>63)|1;
-    else    return 0;
+    else if (b.den && a.den) return 0;
+    else if (a.num && b.num) return (a.num>>31) - (b.num>>31);
+    else                     return INT_MIN;
 }
 
 /**
-- 
1.7.1


--XsQoSWH+UP9D9v3l--



More information about the ffmpeg-devel mailing list