[pitivi/1.0] previewers: Assume the thumbnail generation is accurate enough



commit 7ba86ccf22268d1eedd3adce400689f638bef68a
Author: Alexandru Băluț <alexandru balut gmail com>
Date:   Wed Jan 10 09:54:27 2018 +0100

    previewers: Assume the thumbnail generation is accurate enough
    
    It simplifies the logic. There is no good reason for keeping the
    complicated one.
    
    This change affects the most thumbnail placement at high zoom levels.
    But in this case if the difference between the actual pixbuf position
    and the expected position is so large as to be associated with a
    different thumbnail, we can assume that the format of the file is not
    video-editing friendly, and for these users we prefer to show all
    thumbnails rather than maybe have gaps in the thumbnails.
    
    Differential Revision: https://phabricator.freedesktop.org/D1940

 pitivi/timeline/previewers.py |   32 ++++++++++----------------------
 pitivi/utils/misc.py          |   23 -----------------------
 tests/test_misc.py            |   24 ------------------------
 3 files changed, 10 insertions(+), 69 deletions(-)
---
diff --git a/pitivi/timeline/previewers.py b/pitivi/timeline/previewers.py
index 9cadca6..3fdb63a 100644
--- a/pitivi/timeline/previewers.py
+++ b/pitivi/timeline/previewers.py
@@ -36,7 +36,6 @@ from pitivi.settings import get_dir
 from pitivi.settings import GlobalSettings
 from pitivi.settings import xdg_cache_home
 from pitivi.utils.loggable import Loggable
-from pitivi.utils.misc import binary_search
 from pitivi.utils.misc import hash_file
 from pitivi.utils.misc import path_from_uri
 from pitivi.utils.misc import quantize
@@ -431,6 +430,8 @@ class VideoPreviewer(Previewer, Zoomable, Loggable):
 
         # The thumbs to be generated.
         self.queue = []
+        # The position for which a thumbnail is currently being generated.
+        self.position = -1
         self._thumb_cb_id = None
 
         self.thumbs = {}
@@ -550,12 +551,12 @@ class VideoPreviewer(Previewer, Zoomable, Loggable):
         """Creates a missing thumbnail."""
         self._thumb_cb_id = None
 
-        position = self.queue.pop(0)
-        self.log("Creating thumb for `%s` at %s", path_from_uri(self.uri), position)
+        self.position = self.queue.pop(0)
+        self.log("Creating thumb for `%s` at %s", path_from_uri(self.uri), self.position)
         self.pipeline.seek(1.0,
                            Gst.Format.TIME,
                            Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
-                           Gst.SeekType.SET, position,
+                           Gst.SeekType.SET, self.position,
                            Gst.SeekType.NONE, -1)
 
         # Stop calling me.
@@ -619,23 +620,11 @@ class VideoPreviewer(Previewer, Zoomable, Loggable):
         if queue:
             self.become_controlled()
 
-    def _set_pixbuf(self, position, pixbuf):
-        """Sets the pixbuf for the thumbnail at the specified position."""
-        if position in self.thumbs:
-            thumb = self.thumbs[position]
-        else:
-            # The pixbufs we get from gdkpixbufsink are not always
-            # exactly the ones requested, the reported position can differ.
-            # Try to find the closest thumbnail for the specified position.
-            sorted_times = sorted(self.thumbs.keys())
-            index = binary_search(sorted_times, position)
-            position = sorted_times[index]
-            thumb = self.thumbs[position]
-
+    def _set_pixbuf(self, pixbuf):
+        """Sets the pixbuf for the thumbnail at the expected position."""
+        thumb = self.thumbs[self.position]
         thumb.set_from_pixbuf(pixbuf)
-        if position in self.queue:
-            self.queue.remove(position)
-        self.thumb_cache[position] = pixbuf
+        self.thumb_cache[self.position] = pixbuf
         self.queue_draw()
 
     def zoomChanged(self):
@@ -661,9 +650,8 @@ class VideoPreviewer(Previewer, Zoomable, Loggable):
             struct = message.get_structure()
             struct_name = struct.get_name()
             if struct_name == "preroll-pixbuf":
-                stream_time = struct.get_value("stream-time")
                 pixbuf = struct.get_value("pixbuf")
-                self._set_pixbuf(stream_time, pixbuf)
+                self._set_pixbuf(pixbuf)
         elif message.src == self.pipeline and \
                 message.type == Gst.MessageType.ASYNC_DONE:
             # The seek operation has been performed.
diff --git a/pitivi/utils/misc.py b/pitivi/utils/misc.py
index eaf5e31..88068ce 100644
--- a/pitivi/utils/misc.py
+++ b/pitivi/utils/misc.py
@@ -229,29 +229,6 @@ def quantize(input, interval):
     return (input // interval) * interval
 
 
-def binary_search(elements, value):
-    """Returns the index of the element closest to value.
-
-    Args:
-        elements (List): A sorted list.
-    """
-    if not elements:
-        return -1
-    closest_index = bisect.bisect_left(elements, value, 0, len(elements) - 1)
-    element = elements[closest_index]
-    closest_distance = abs(element - value)
-    if closest_distance == 0:
-        return closest_index
-    for index in (closest_index - 1,):
-        if index < 0:
-            continue
-        distance = abs(elements[index] - value)
-        if closest_distance > distance:
-            closest_index = index
-            closest_distance = distance
-    return closest_index
-
-
 def show_user_manual(page=None):
     """Displays the user manual.
 
diff --git a/tests/test_misc.py b/tests/test_misc.py
index 9627095..244b3cb 100644
--- a/tests/test_misc.py
+++ b/tests/test_misc.py
@@ -23,35 +23,11 @@ import unittest
 
 from gi.repository import Gst
 
-from pitivi.utils.misc import binary_search
 from pitivi.utils.misc import PathWalker
 from tests.common import create_main_loop
 from tests.common import get_sample_uri
 
 
-class BinarySearchTest(unittest.TestCase):
-    """Tests for the `binary_search` method."""
-
-    def test_empty_list(self):
-        """Checks the result when the list is empty."""
-        self.assertEqual(binary_search([], 10), -1)
-
-    def test_existing(self):
-        """Checks the result when the element is present."""
-        self.assertEqual(binary_search([10, 20, 30], 10), 0)
-        self.assertEqual(binary_search([10, 20, 30], 20), 1)
-        self.assertEqual(binary_search([10, 20, 30], 30), 2)
-
-    def test_missing(self):
-        """Checks the result when the element is missing."""
-        self.assertEqual(binary_search([10, 20, 30], 1), 0)
-        self.assertEqual(binary_search([10, 20, 30], 11), 0)
-        self.assertEqual(binary_search([10, 20, 30], 16), 1)
-        self.assertEqual(binary_search([10, 20, 30], 24), 1)
-        self.assertEqual(binary_search([10, 20, 30], 29), 2)
-        self.assertEqual(binary_search([10, 20, 30], 40), 2)
-
-
 class PathWalkerTest(unittest.TestCase):
     """Tests for the `PathWalker` class."""
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]