XiaoHongbo-Hope commented on code in PR #9461:
URL: https://github.com/apache/paimon/pull/9461#discussion_r3885991552


##########
paimon-python/pypaimon/multimodal/video.py:
##########
@@ -0,0 +1,194 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""PyTorch DataLoader helpers for descriptor-backed video frame rows."""
+
+import os
+from collections import OrderedDict
+from collections.abc import Mapping
+
+from pypaimon.table.row.blob import Blob, VideoFrameDescriptor
+
+
+class VideoFrameCollator:
+    """Decode frame rows in a DataLoader worker while reusing video sessions.
+
+    ``decoder_factory`` receives a seekable stream containing exactly one
+    descriptor-backed video. ``decode_fn`` receives the cached decoder, the
+    frame ordinal embedded in the descriptor, and one row dictionary. This
+    keeps Paimon independent of a particular video codec library while allowing
+    PyAV, TorchCodec, or an application decoder to be plugged in.
+
+    The cache is process-local and keyed by physical video payload identity.
+    ``collate_fn`` defaults to PyTorch's ``default_collate`` and may be 
replaced
+    for decoders that already return batched objects.
+    """
+
+    def __init__(
+            self,
+            table,
+            *,
+            video_column,
+            decoder_factory,
+            decode_fn,
+            output_column="frame",
+            max_open_videos=8,
+            collate_fn=None):
+        if not video_column:
+            raise ValueError("video_column is required.")
+        if not callable(decoder_factory):
+            raise ValueError("decoder_factory must be callable.")
+        if not callable(decode_fn):
+            raise ValueError("decode_fn must be callable.")
+        if (
+            isinstance(max_open_videos, bool)
+            or not isinstance(max_open_videos, int)
+            or max_open_videos <= 0
+        ):
+            raise ValueError("max_open_videos must be a positive int.")
+        if collate_fn is not None and not callable(collate_fn):
+            raise ValueError("collate_fn must be callable or None.")
+
+        raw_table = getattr(table, "raw_table", table)
+        file_io = getattr(raw_table, "file_io", None)
+        if file_io is None:
+            raise ValueError("table must provide raw_table.file_io or 
file_io.")
+
+        self.file_io = file_io
+        self.video_column = video_column
+        self.decoder_factory = decoder_factory
+        self.decode_fn = decode_fn
+        self.output_column = output_column
+        self.max_open_videos = max_open_videos
+        self.collate_fn = collate_fn
+        self._decoders = OrderedDict()
+        self._owner_pid = os.getpid()
+
+    def __call__(self, rows):
+        self._ensure_process_local_cache()
+        single_row = isinstance(rows, Mapping)
+        input_rows = [rows] if single_row else list(rows)
+        decoded_rows = [self._decode_row(row) for row in input_rows]

Review Comment:
   Nit: VideoFrameCollator adds ~4.6% sequential decode overhead on droid_100, 
while direct .video range decoding matches raw MP4. video? We can create. a 
batch fast path PR as follow-up.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to