-
Notifications
You must be signed in to change notification settings - Fork 71
Add comment and TODO to canWeSkipSeeking()
#1041
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
Open
NicolasHug
wants to merge
9
commits into
meta-pytorch:main
Choose a base branch
from
NicolasHug:seek_comment
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3e9ae6f
Avoid seeking checks when decoding frames sequentially
NicolasHug 86ec99e
Simplify seek skipping logic
NicolasHug e68d7ce
Merge branch 'main' of github.com:pytorch/torchcodec into avoid_seeki…
NicolasHug d98c78a
Merge branch 'simplify_skip' into avoid_seeking_checks
NicolasHug 42409a9
comment
NicolasHug 9acc148
Merge branch 'main' of github.com:pytorch/torchcodec into avoid_seeki…
NicolasHug 2295241
Fix
NicolasHug 4ace1d3
Comments
NicolasHug faf8e35
Merge branch 'main' of github.com:pytorch/torchcodec into seek_comment
NicolasHug 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1081,32 +1081,17 @@ void SingleStreamDecoder::setCursor(int64_t pts) { | |
| cursor_ = pts; | ||
| } | ||
|
|
||
| /* | ||
| Videos have I frames and non-I frames (P and B frames). Non-I frames need data | ||
| from the previous I frame to be decoded. | ||
|
|
||
| Imagine the cursor is at a random frame with PTS=lastDecodedAvFramePts (x for | ||
| brevity) and we wish to seek to a user-specified PTS=y. | ||
|
|
||
| If y < x, we don't have a choice but to seek backwards to the highest I frame | ||
| before y. | ||
|
|
||
| If y > x, we have two choices: | ||
|
|
||
| 1. We could keep decoding forward until we hit y. Illustrated below: | ||
|
|
||
| I P P P I P P P I P P I P P I P | ||
| x y | ||
|
|
||
| 2. We could try to jump to an I frame between x and y (indicated by j below). | ||
| And then start decoding until we encounter y. Illustrated below: | ||
|
|
||
| I P P P I P P P I P P I P P I P | ||
| x j y | ||
|
|
||
| (2) is more efficient than (1) if there is an I frame between x and y. | ||
| */ | ||
| bool SingleStreamDecoder::canWeAvoidSeeking() const { | ||
| // Returns true if we can avoid seeking in the AVFormatContext based on | ||
| // heuristics that rely on the target cursor_ and the last decoded frame. | ||
| // Seeking is expensive, so we try to avoid it when possible. | ||
| // Note that this function itself isn't always that cheap to call: in | ||
| // particular the calls to getKeyFrameIndexForPts below in approximate mode | ||
| // are sometimes slow. | ||
| // TODO we should understand why (is it because it reads the file?) and | ||
| // potentially optimize it. E.g. we may not want to ever seek, or even *check* | ||
| // if we need to seek in some cases, like if we're going to decode 80% of the | ||
| // frames anyway. | ||
| const StreamInfo& streamInfo = streamInfos_.at(activeStreamIndex_); | ||
| if (streamInfo.avMediaType == AVMEDIA_TYPE_AUDIO) { | ||
| // For audio, we only need to seek if a backwards seek was requested | ||
|
|
@@ -1129,13 +1114,34 @@ bool SingleStreamDecoder::canWeAvoidSeeking() const { | |
| // implement caching. | ||
| return false; | ||
| } | ||
| // We are seeking forwards. | ||
| // We can only skip a seek if both lastDecodedAvFramePts and | ||
| // cursor_ share the same keyframe. | ||
| int lastDecodedAvFrameIndex = getKeyFrameIndexForPts(lastDecodedAvFramePts_); | ||
| // We are seeking forwards. We can skip a seek if both the last decoded frame | ||
| // and cursor_ share the same keyframe: | ||
| // Videos have I frames and non-I frames (P and B frames). Non-I frames need | ||
| // data from the previous I frame to be decoded. | ||
| // | ||
| // Imagine the cursor is at a random frame with PTS=lastDecodedAvFramePts (x | ||
| // for brevity) and we wish to seek to a user-specified PTS=y. | ||
| // | ||
| // If y < x, we don't have a choice but to seek backwards to the highest I | ||
| // frame before y. | ||
| // | ||
| // If y > x, we have two choices: | ||
| // | ||
| // 1. We could keep decoding forward until we hit y. Illustrated below: | ||
| // | ||
| // I P P P I P P P I P P I P | ||
| // x y | ||
| // | ||
| // 2. We could try to jump to an I frame between x and y (indicated by j | ||
| // below). And then start decoding until we encounter y. Illustrated below: | ||
| // | ||
| // I P P P I P P P I P P I P | ||
| // x j y | ||
| // (2) is only more efficient than (1) if there is an I frame between x and y. | ||
| int lastKeyFrameIndex = getKeyFrameIndexForPts(lastDecodedAvFramePts_); | ||
| int targetKeyFrameIndex = getKeyFrameIndexForPts(cursor_); | ||
| return lastDecodedAvFrameIndex >= 0 && targetKeyFrameIndex >= 0 && | ||
| lastDecodedAvFrameIndex == targetKeyFrameIndex; | ||
| return lastKeyFrameIndex >= 0 && targetKeyFrameIndex >= 0 && | ||
| lastKeyFrameIndex == targetKeyFrameIndex; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Above: renamed to make it clearer that it's the index of a keyframe, not the index of the last decoded frame. |
||
| } | ||
|
|
||
| // This method looks at currentPts and desiredPts and seeks in the | ||
|
|
||
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.
Comment moved below, largely unchanged.