This is an automated email from the ASF dual-hosted git repository.

JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new c0a7943a55 [python] Reduce unnecessary GOP prefetch for random video 
reads (#10818)
c0a7943a55 is described below

commit c0a7943a555b0f0dccf25de1a0ff9c147e886d41
Author: XiaoHongbo <[email protected]>
AuthorDate: Mon Nov 2 15:22:40 2026 +0800

    [python] Reduce unnecessary GOP prefetch for random video reads (#10818)
---
 .../pypaimon/multimodal/lerobot/dataset.py         |  7 ++-
 .../pypaimon/tests/multimodal_lerobot_test.py      | 72 ++++++++++++++++++++++
 2 files changed, 77 insertions(+), 2 deletions(-)

diff --git a/paimon-python/pypaimon/multimodal/lerobot/dataset.py 
b/paimon-python/pypaimon/multimodal/lerobot/dataset.py
index c13aaf1d1c..b54072bbd8 100644
--- a/paimon-python/pypaimon/multimodal/lerobot/dataset.py
+++ b/paimon-python/pypaimon/multimodal/lerobot/dataset.py
@@ -2140,13 +2140,16 @@ class _PyAVVideoDecoder:
                 raise IndexError(
                     "Video frame index %d is out of range." % index)
             groups.setdefault(preceding, []).append(index)
-            start = mapping.keyframes[max(0, preceding - 1)][2]
-            range_end = preceding + 2
+            # Include one PyAV input buffer beyond the GOP for delayed 
B-frames.
+            # Larger dependencies are still fetched on demand.
+            start = mapping.keyframes[preceding][2]
+            range_end = preceding + 1
             end = (
                 mapping.keyframes[range_end][2]
                 if range_end < len(mapping.keyframes)
                 else self._video_length()
             )
+            end = min(end + 32 * 1024, self._video_length())
             ranges.append((start, end - start))
         return groups, _merge_video_ranges(ranges)
 
diff --git a/paimon-python/pypaimon/tests/multimodal_lerobot_test.py 
b/paimon-python/pypaimon/tests/multimodal_lerobot_test.py
index 56966d5503..2a2fd90fde 100644
--- a/paimon-python/pypaimon/tests/multimodal_lerobot_test.py
+++ b/paimon-python/pypaimon/tests/multimodal_lerobot_test.py
@@ -664,6 +664,78 @@ class LeRobotValidationTest(unittest.TestCase):
                 finally:
                     decoder.close()
 
+    @unittest.skipUnless(av is not None, "PyAV is required")
+    def 
test_indexed_plan_prefetches_requested_gops_with_bounded_lookahead(self):
+        source = io.BytesIO(b"")
+        source.video_length = 1000000
+        index = VideoKeyframeIndex(
+            [(900000, 100000)],
+            [(0, 0, 100000), (10, 10, 300000),
+             (20, 20, 500000), (30, 30, 700000)])
+        decoder = _PyAVVideoDecoder(source, index)
+        try:
+            self.assertEqual(({1: [12]}, [(300000, 232768)]),
+                             decoder._indexed_plan([12]))
+            self.assertEqual(({1: [19], 2: [20]}, [(300000, 432768)]),
+                             decoder._indexed_plan([19, 20]))
+            self.assertEqual(({0: [0], 3: [39]}, [(100000, 232768), (700000, 
300000)]),
+                             decoder._indexed_plan([0, 39]))
+        finally:
+            decoder.close()
+
+    @unittest.skipUnless(av is not None, "PyAV is required")
+    def test_indexed_gop_boundary_avoids_extra_range_read(self):
+        output = io.BytesIO()
+        with av.open(output, mode="w", format="mp4") as container:
+            stream = container.add_stream("libx264", rate=30)
+            stream.width, stream.height = 160, 120
+            stream.pix_fmt = "yuv420p"
+            stream.options = {"x264-params":
+                              
"keyint=12:min-keyint=12:scenecut=0:bframes=2:threads=1"}
+            rng = np.random.RandomState(12)
+            for ordinal in range(60):
+                frame = av.VideoFrame.from_ndarray(
+                    rng.randint(0, 256, (120, 160, 3), dtype=np.uint8),
+                    format="rgb24")
+                frame.pts, frame.time_base = ordinal, Fraction(1, 30)
+                for packet in stream.encode(frame):
+                    container.mux(packet)
+            for packet in stream.encode():
+                container.mux(packet)
+        payload = output.getvalue()
+        index = VideoKeyframeIndex.inspect(io.BytesIO(payload), len(payload))
+        with av.open(io.BytesIO(payload)) as container:
+            expected = [frame.to_ndarray(format="rgb24")
+                        for frame in container.decode(video=0)]
+        for target in (23, 35):
+            counts = []
+            for lookahead in (False, True):
+                source = io.BytesIO(payload)
+                source.video_length = len(payload)
+                calls = []
+
+                def read_ranges(ranges):
+                    calls.extend(ranges)
+                    return [payload[start:start + size] for start, size in 
ranges]
+
+                source.video_read_ranges = read_ranges
+                decoder = _PyAVVideoDecoder(source, index)
+                if not lookahead:
+                    anchor = next(i for i, entry in enumerate(index.keyframes)
+                                  if entry[0] <= target < index.keyframes[i + 
1][0])
+                    start = index.keyframes[anchor][2]
+                    end = index.keyframes[anchor + 1][2]
+                    decoder._indexed_plan = lambda indices: (
+                        {anchor: indices}, [(start, end - start)])
+                try:
+                    actual = decoder.get_frames_at(indices=[target]).data
+                    np.testing.assert_array_equal(
+                        expected[target], actual[0].permute(1, 2, 0).numpy())
+                    counts.append(len(calls))
+                finally:
+                    decoder.close()
+            self.assertLess(counts[1], counts[0])
+
     def test_default_video_backend_falls_back_on_os_error(self):
         stream = Mock()
         decoder = object()

Reply via email to