JingsongLi commented on code in PR #10905: URL: https://github.com/apache/paimon/pull/10905#discussion_r4513834420
########## paimon-python/pypaimon/multimodal/mp4.py: ########## @@ -0,0 +1,331 @@ +# 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. + +"""Bounded MP4 sample-table parsing for frame-addressed video reads.""" + +import struct +from fractions import Fraction + +import numpy as np + +_MAX_METADATA_BYTES = 16 * 1024 * 1024 +_MAX_SAMPLES = 1000000 +_MAX_TOP_LEVEL_BOXES = 1024 + + +def _boxes(data): + data = memoryview(data) + offset = 0 + box_count = 0 + while offset < len(data): + box_count += 1 + if box_count > 65536: + raise ValueError("Too many MP4 boxes.") + if len(data) - offset < 8: + raise ValueError("Truncated MP4 box.") + size, kind = struct.unpack_from(">I4s", data, offset) + header = 8 + if size == 1: + if len(data) - offset < 16: + raise ValueError("Truncated extended MP4 box.") + size = struct.unpack_from(">Q", data, offset + 8)[0] + header = 16 + elif size == 0: + size = len(data) - offset + if size < header or size > len(data) - offset: + raise ValueError("Invalid MP4 box size.") + yield kind, data[offset + header:offset + size] + offset += size + + +def _child(data, kind, required=True): + matches = [body for name, body in _boxes(data) if name == kind] + if len(matches) > 1 or (required and not matches): + raise ValueError("Missing or duplicate MP4 box: %r." % kind) + return matches[0] if matches else None + + +def _uint(data, offset): + if offset < 0 or offset + 4 > len(data): + raise ValueError("Truncated MP4 field.") + return struct.unpack_from(">I", data, offset)[0] + + +def _scale(data): + if not len(data) or data[0] not in (0, 1): + raise ValueError("Unsupported MP4 timing version.") + value = _uint(data, 20 if data[0] else 12) + if value == 0: + raise ValueError("Invalid MP4 time scale.") + return value + + +def _runs(data, composition=False): + count = _uint(data, 4) + if len(data) != 8 + count * 8 or count > _MAX_SAMPLES: + raise ValueError("Invalid MP4 timing run count.") + if data[0] not in ((0, 1) if composition else (0,)): + raise ValueError("Unsupported MP4 timing version.") + entries = np.frombuffer(data, dtype=">u4", offset=8).reshape(count, 2) + counts = entries[:, 0].astype(np.int64) + total = int(counts.sum()) + if total == 0 or total > _MAX_SAMPLES or np.any(counts == 0): + raise ValueError("MP4 sample count exceeds supported bounds.") + values = entries[:, 1] + if composition and data[0] == 1: + values = values.view(">i4") + return counts, values.astype(np.int64) + + +def _timeline(source, length, with_index=False): + hint = getattr(source, "video_moov_range", (0, 0)) + offset, size = hint + if size: + try: + if offset < 0 or size < 8 or offset > length or size > length - offset: + raise ValueError("Invalid moov hint.") + source.seek(offset) + header = source.read(min(16, size)) + actual, kind = struct.unpack_from(">I4s", header) + if actual == 1: + actual = struct.unpack_from(">Q", header, 8)[0] + elif actual == 0: + actual = length - offset + if kind != b"moov" or actual != size: + raise ValueError("Invalid moov hint.") + except (ValueError, struct.error): + offset = 0 + source.seek(offset) + moov = None + for unused in range(_MAX_TOP_LEVEL_BOXES): + offset = source.tell() + if offset >= length: + break + header = source.read(8) + if len(header) != 8: + raise ValueError("Truncated video header.") + size, kind = struct.unpack(">I4s", header) + header_size = 8 + if size == 1: + extended = source.read(8) + if len(extended) != 8: + raise ValueError("Truncated extended video header.") + size = struct.unpack(">Q", extended)[0] + header_size = 16 + elif size == 0: + size = length - offset + if size < header_size or size > length - offset: + raise ValueError("Invalid video box extent.") + if kind == b"moov": + if size > _MAX_METADATA_BYTES: + raise ValueError("MP4 metadata exceeds supported bounds.") + metadata_range = (offset, size) + moov = source.read(size - header_size) + if len(moov) != size - header_size: + raise ValueError("Truncated MP4 metadata.") + break + source.seek(offset + size) + if moov is None or _child(moov, b"mvex", required=False) is not None: + raise ValueError("A non-fragmented MP4 sample table is required.") + tracks = [body for kind, body in _boxes(moov) if kind == b"trak"] + selected = None + for stream_index, track in enumerate(tracks): + media = _child(track, b"mdia") + handler = _child(media, b"hdlr") + if len(handler) < 12: + raise ValueError("Truncated MP4 handler.") + if bytes(handler[8:12]) == b"vide": + if selected is not None: + raise ValueError("Multiple video tracks require exact mode.") Review Comment: **[P1] Preserve the video track when falling back.** LeRobot assigns frame ordinals from the first video stream. Rejecting a second video track here sends the default backend to TorchCodec exact mode without a `stream_index`, so TorchCodec can select a different stream. I reproduced this with a two-track MP4 whose second track is marked as default. Reading frame 0 through the `.video` writer and reader returned the first track's red frame on the base commit and the second track's blue frame on this PR. Both tracks have the same dimensions, so the shape check passes. Please keep the first video stream in the fallback path and add a regression test with a different default track. ########## paimon-python/pypaimon/execution/native/read/table_read.py: ########## @@ -38,7 +38,8 @@ CoreOptions.FILE_FORMAT_MOSAIC, }) _NATIVE_READ_FILE_SUFFIXES = tuple('.%s' % file_format for file_format in _NATIVE_READ_FILE_FORMATS) -_NATIVE_BLOB_FILE_SUFFIXES = ('.blob', '.video') +# Released native readers use the previous five-array layout. +_NATIVE_BLOB_FILE_SUFFIXES = ('.blob',) Review Comment: **[P2] Land Rust support for the new `.video` layout first.** This check sends an entire split to the Python reader whenever it contains a `.video` file. I reproduced a regression by reusing a full-column split from a Data Evolution table and projecting only `try_variant_get` on its Parquet `VARIANT` column. The base commit returned `1.5` with Rust 0.6.0. This PR raises `Variant extraction requires a compatible native reader` even though the query only needs the Parquet file. Please land apache/paimon-rust#1347 first and make the compatible Rust runtime available to PyPaimon. Then remove this split-wide fallback and keep `.video` on the native read path. -- 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]
