-
Notifications
You must be signed in to change notification settings - Fork 68
refactor metadata fallback logic #1021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mollyxu
merged 14 commits into
meta-pytorch:main
from
mollyxu:refactor-metadata-fallback
Nov 10, 2025
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8bee8d7
refactor metadata fallback logic
08c348e
fix tests and move seekmode to metadata.h
868e23e
add checks
e58cf39
add cpp tests
47ff2db
Merge branch 'meta-pytorch:main' into refactor-metadata-fallback
mollyxu 35febfb
use streametadata methods in singlestreamdecoder
20a7227
address feedback
b677863
address feedback
1478117
modify docstrings
3494331
modified fallback logic
cdabcb0
fix fallthrough error
8bf490c
modify fallback logic and add torch_checks
e3b91ea
address feedback
71f4861
address feedback
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| // All rights reserved. | ||
| // | ||
| // This source code is licensed under the BSD-style license found in the | ||
| // LICENSE file in the root directory of this source tree. | ||
|
|
||
| #include "Metadata.h" | ||
|
|
||
| namespace facebook::torchcodec { | ||
|
|
||
| std::optional<double> StreamMetadata::getDurationSeconds( | ||
| SeekMode seekMode) const { | ||
| switch (seekMode) { | ||
| case SeekMode::custom_frame_mappings: | ||
| case SeekMode::exact: | ||
| // In exact mode, use the scanned content value | ||
| if (endStreamPtsSecondsFromContent.has_value() && | ||
| beginStreamPtsSecondsFromContent.has_value()) { | ||
| return endStreamPtsSecondsFromContent.value() - | ||
| beginStreamPtsSecondsFromContent.value(); | ||
| } | ||
| return std::nullopt; | ||
| case SeekMode::approximate: | ||
| if (durationSecondsFromHeader.has_value()) { | ||
| return durationSecondsFromHeader.value(); | ||
| } | ||
| if (numFramesFromHeader.has_value() && averageFpsFromHeader.has_value() && | ||
| averageFpsFromHeader.value() != 0.0) { | ||
| return static_cast<double>(numFramesFromHeader.value()) / | ||
| averageFpsFromHeader.value(); | ||
| } | ||
| return std::nullopt; | ||
| } | ||
| return std::nullopt; | ||
mollyxu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| double StreamMetadata::getBeginStreamSeconds(SeekMode seekMode) const { | ||
| switch (seekMode) { | ||
| case SeekMode::custom_frame_mappings: | ||
| case SeekMode::exact: | ||
| if (beginStreamPtsSecondsFromContent.has_value()) { | ||
| return beginStreamPtsSecondsFromContent.value(); | ||
| } | ||
mollyxu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return 0.0; | ||
| case SeekMode::approximate: | ||
| return 0.0; | ||
mollyxu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| return 0.0; | ||
| } | ||
|
|
||
| std::optional<double> StreamMetadata::getEndStreamSeconds( | ||
| SeekMode seekMode) const { | ||
| switch (seekMode) { | ||
| case SeekMode::custom_frame_mappings: | ||
| case SeekMode::exact: | ||
| if (endStreamPtsSecondsFromContent.has_value()) { | ||
| return endStreamPtsSecondsFromContent.value(); | ||
| } | ||
mollyxu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return getDurationSeconds(seekMode); | ||
| case SeekMode::approximate: | ||
| return getDurationSeconds(seekMode); | ||
| } | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| std::optional<int64_t> StreamMetadata::getNumFrames(SeekMode seekMode) const { | ||
| switch (seekMode) { | ||
| case SeekMode::custom_frame_mappings: | ||
| case SeekMode::exact: | ||
| if (numFramesFromContent.has_value()) { | ||
| return numFramesFromContent.value(); | ||
| } | ||
mollyxu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return std::nullopt; | ||
| case SeekMode::approximate: { | ||
| if (numFramesFromHeader.has_value()) { | ||
| return numFramesFromHeader.value(); | ||
| } | ||
| if (averageFpsFromHeader.has_value() && | ||
| durationSecondsFromHeader.has_value()) { | ||
| return static_cast<int64_t>( | ||
| averageFpsFromHeader.value() * durationSecondsFromHeader.value()); | ||
| } | ||
| return std::nullopt; | ||
| } | ||
| } | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| std::optional<double> StreamMetadata::getAverageFps(SeekMode seekMode) const { | ||
| switch (seekMode) { | ||
| case SeekMode::custom_frame_mappings: | ||
| case SeekMode::exact: | ||
| if (getNumFrames(seekMode).has_value() && | ||
|
||
| beginStreamPtsSecondsFromContent.has_value() && | ||
| endStreamPtsSecondsFromContent.has_value() && | ||
| (beginStreamPtsSecondsFromContent.value() != | ||
| endStreamPtsSecondsFromContent.value())) { | ||
| return static_cast<double>( | ||
| getNumFrames(seekMode).value() / | ||
| (endStreamPtsSecondsFromContent.value() - | ||
| beginStreamPtsSecondsFromContent.value())); | ||
| } | ||
| return averageFpsFromHeader; | ||
| case SeekMode::approximate: | ||
| return averageFpsFromHeader; | ||
| } | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| } // namespace facebook::torchcodec | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One of the benefits of using the seek mode is that we can know that a scan should have been performed, and the metadata from the scan should definitely be there. I think we should instead make this:
That is, we don't check if the value is present, and we don't return
nullopt. The unconditional call to.value()will throw an exception if we somehow get into a situation where we're in exact seek mode and there is no value. But, that is what we want. Such a situation should never happen, and we want an exception thrown.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you recommend modifying the logic such that
exactmode is accessed directly but we keep the checks forcustom_frame_mappings? Previously the Python logic didn't have knowledge of seek mode socustom_frame_mappingswas oftentimes grouped withexactbecause scans are performed.In
custom_frame_mappingsa scan only happens to specific streams uponadd_video_stream()is called (ie.test_get_metadataintest_metadata.py). Removing the checks would lead to cases in which not all streams in the container would have scanned metadata.Or should
custom_frame_mappingsthat's missing the scanned information behave the same asapproximatemode?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only allow one stream to be added and scanned via
add_video_stream, and I believe (but do correct me if I am mistaken!) when we access any stream metadata, its from the stream currently stored asactiveStreamIndex_. This stream index is updated by thecustom_frame_mappingsequivalent "scan" function.Is it possible to access the stream metadata of a stream other than
activeStreamIndex_? If not, it should be safe to assume that the metadata will be present, and treat it as exact mode.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in
get_container_metadata.pyin_metadata.pywe callget_stream_json_metadatafor each stream in the container. It is at this step that we are triggering the logic inMetadata.cppforcustom_frame_mappingsthat doesn't exist for streams that are at theactiveStreamIndex_torchcodec/src/torchcodec/_core/_metadata.py
Lines 259 to 260 in afd5aba
The error seems to come not from accessing a non-active stream but rather from how we store metadata.