[vlc-commits] ytdl: improve format selection

Rémi Denis-Courmont git at videolan.org
Tue Sep 29 18:16:29 CEST 2020


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Tue Sep 29 19:15:46 2020 +0300| [5e9ceb894f8124282383e1d48d798143225acada] | committer: Rémi Denis-Courmont

ytdl: improve format selection

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=5e9ceb894f8124282383e1d48d798143225acada
---

 modules/demux/ytdl.c | 61 +++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 49 insertions(+), 12 deletions(-)

diff --git a/modules/demux/ytdl.c b/modules/demux/ytdl.c
index a6dce10213..8b21b067ee 100644
--- a/modules/demux/ytdl.c
+++ b/modules/demux/ytdl.c
@@ -79,6 +79,47 @@ struct ytdl_playlist {
     stream_t *source;
 };
 
+
+static int CompareFormats(const struct json_object *f_a,
+                          const struct json_object *f_b, double pref_height)
+{
+    double h_a = json_get_num(f_a, "height");
+    double abr_a = json_get_num(f_a, "abr");
+    double h_b = json_get_num(f_b, "height");
+    double abr_b = json_get_num(f_b, "abr");
+
+    /* Prefer non-mute formats */
+    if (!isnan(abr_a) != !isnan(abr_b))
+        return isnan(abr_a) ? -1 : +1;
+
+    /* Prefer non-blind formats */
+    if (!isnan(h_a) != !isnan(h_b))
+        return isnan(h_a) ? -1 : +1;
+
+    if (islessequal(h_a, pref_height)) {
+        if (!islessequal(h_b, pref_height))
+            return +1;
+        if (h_a > h_b)
+            return -1;
+        if (h_a < h_b)
+            return +1;
+    } else {
+        if (islessequal(h_b, pref_height))
+            return -1;
+        if (isless(h_a, h_b))
+            return -1;
+        if (isgreater(h_a, h_b))
+            return +1;
+    }
+
+    if (isgreater(abr_a, abr_b))
+        return +1;
+    if (isgreater(abr_b, abr_a))
+        return -1;
+
+    return 0;
+}
+
 static const struct json_object *PickFormat(stream_t *s,
                                             const struct json_object *entry)
 {
@@ -91,8 +132,9 @@ static const struct json_object *PickFormat(stream_t *s,
 
      const struct json_object *best_fmt = NULL;
      double pref_height = var_InheritInteger(s, "preferred-resolution");
-     double best_height = -1.;
-     double best_abr = -1.;
+
+     if (isless(pref_height, 0.))
+         pref_height = NAN;
 
      for (size_t i = 0; i < fmts->array.size; i++) {
          const struct json_value *v = &fmts->array.entries[i];
@@ -101,19 +143,14 @@ static const struct json_object *PickFormat(stream_t *s,
              continue;
 
          const struct json_object *fmt = &v->object;
-         double height = json_get_num(fmt, "height");
-         double abr = json_get_num(fmt, "abr");
 
-         if (!isgreaterequal(height, best_height)
-          || (best_height < pref_height && pref_height < height))
-             continue;
-
-         if (!isgreaterequal(abr, best_abr))
+         if (best_fmt == NULL) {
+             best_fmt = fmt;
              continue;
+         }
 
-         best_fmt = fmt;
-         best_height = height;
-         best_abr = abr;
+         if (CompareFormats(fmt, best_fmt, pref_height) > 0)
+              best_fmt = fmt;
      }
 
      return best_fmt;



More information about the vlc-commits mailing list