-
Notifications
You must be signed in to change notification settings - Fork 64
BETA CUDA interface: NVCUVID decoder implementation 1/N #910
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
Merged
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
78ab058
Let's just commit 3k loc in a single commit
NicolasHug b45decc
Fixes
NicolasHug 316f218
Merge branch 'main' of github.com:pytorch/torchcodec into aeaenjfjanef
NicolasHug d0192ec
GetCache -> getCache
NicolasHug 515deb5
Make UniqueCUvideodecoder a pointer on CUvideodecoder, not void
NicolasHug 13fad10
Make device and device_variant have a default instead of being std::o…
NicolasHug eb8de72
Remove old registerDeviceInterface
NicolasHug 4f7a4fb
Call std::memset
NicolasHug dcf3124
remove unnecessary cuda_runtime.h include, update cmake accordingly
NicolasHug 0ad7370
abstract frameBuffer_ into a FrameBuffer class
NicolasHug aad142e
Cleanup BSF logic
NicolasHug 2592888
Return int in callback instead of unsigned char
NicolasHug b5fe9bc
define width and height as unsigned int
NicolasHug 7494259
Merge branch 'main' of github.com:pytorch/torchcodec into aeaenjfjanef
NicolasHug 560b376
Fix cuda context initialization
NicolasHug d1e51b3
Merge branch 'main' of github.com:pytorch/torchcodec into aeaenjfjanef
NicolasHug f9c7297
Skip equality check on ffmepg 4
NicolasHug 7e4dd10
Define constant, add TODO for AVRational
NicolasHug f614846
Use uint32_t types
NicolasHug aa6e253
Create packet.reset() and add P0 TODO
NicolasHug 186eaa4
Add TODO
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,108 @@ | ||
// 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. | ||
|
||
// BETA CUDA device interface that provides direct control over NVDEC | ||
// while keeping FFmpeg for demuxing. A lot of the logic, particularly the use | ||
// of a cache for the decoders, is inspired by DALI's implementation which is | ||
// APACHE 2.0: | ||
// https://github.com/NVIDIA/DALI/blob/c7539676a24a8e9e99a6e8665e277363c5445259/dali/operators/video/frames_decoder_gpu.cc#L1 | ||
// | ||
// NVDEC / NVCUVID docs: | ||
// https://docs.nvidia.com/video-technologies/video-codec-sdk/13.0/nvdec-video-decoder-api-prog-guide/index.html#using-nvidia-video-decoder-nvdecode-api | ||
|
||
#pragma once | ||
|
||
#include "src/torchcodec/_core/Cache.h" | ||
#include "src/torchcodec/_core/DeviceInterface.h" | ||
#include "src/torchcodec/_core/FFMPEGCommon.h" | ||
#include "src/torchcodec/_core/NVDECCache.h" | ||
|
||
#include <map> | ||
#include <memory> | ||
#include <mutex> | ||
#include <queue> | ||
#include <unordered_map> | ||
#include <vector> | ||
|
||
#include "src/torchcodec/_core/nvcuvid_include/cuviddec.h" | ||
#include "src/torchcodec/_core/nvcuvid_include/nvcuvid.h" | ||
|
||
namespace facebook::torchcodec { | ||
|
||
class BetaCudaDeviceInterface : public DeviceInterface { | ||
public: | ||
explicit BetaCudaDeviceInterface(const torch::Device& device); | ||
virtual ~BetaCudaDeviceInterface(); | ||
|
||
void initializeInterface(AVStream* stream) override; | ||
|
||
void convertAVFrameToFrameOutput( | ||
const VideoStreamOptions& videoStreamOptions, | ||
const AVRational& timeBase, | ||
UniqueAVFrame& avFrame, | ||
FrameOutput& frameOutput, | ||
std::optional<torch::Tensor> preAllocatedOutputTensor = | ||
std::nullopt) override; | ||
|
||
bool canDecodePacketDirectly() const override { | ||
return true; | ||
} | ||
|
||
int sendPacket(ReferenceAVPacket& packet) override; | ||
int receiveFrame(UniqueAVFrame& avFrame, int64_t desiredPts) override; | ||
void flush() override; | ||
ReferenceAVPacket* applyBSF( | ||
ReferenceAVPacket& packet, | ||
AutoAVPacket& filteredAutoPacket, | ||
ReferenceAVPacket& filteredPacket) override; | ||
|
||
// NVDEC callback functions (must be public for C callbacks) | ||
unsigned char streamPropertyChange(CUVIDEOFORMAT* videoFormat); | ||
int frameReadyForDecoding(CUVIDPICPARAMS* pPicParams); | ||
|
||
private: | ||
UniqueAVFrame convertCudaFrameToAVFrame( | ||
CUdeviceptr framePtr, | ||
unsigned int pitch, | ||
const CUVIDPARSERDISPINFO& dispInfo); | ||
|
||
CUvideoparser videoParser_ = nullptr; | ||
UniqueCUvideodecoder decoder_; | ||
CUVIDEOFORMAT videoFormat_ = {}; | ||
|
||
struct FrameBufferSlot { | ||
CUVIDPARSERDISPINFO dispInfo; | ||
int64_t guessedPts; | ||
bool occupied = false; | ||
|
||
FrameBufferSlot() : guessedPts(-1), occupied(false) { | ||
memset(&dispInfo, 0, sizeof(dispInfo)); | ||
} | ||
}; | ||
|
||
std::vector<FrameBufferSlot> frameBuffer_; | ||
FrameBufferSlot* findEmptySlot(); | ||
FrameBufferSlot* findFrameWithExactPts(int64_t desiredPts); | ||
NicolasHug marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
std::queue<int64_t> packetsPtsQueue; | ||
|
||
bool eofSent_ = false; | ||
|
||
// Flush flag to prevent decode operations during flush (like DALI's | ||
// isFlushing_) | ||
bool isFlushing_ = false; | ||
|
||
AVRational timeBase_ = {0, 0}; | ||
|
||
UniqueAVBSFContext bitstreamFilter_; | ||
|
||
// Default CUDA interface for color conversion. | ||
// TODONVDEC P2: we shouldn't need to keep a separate instance of the default. | ||
// See other TODO there about how interfaces should be completely independent. | ||
std::unique_ptr<DeviceInterface> defaultCudaInterface_; | ||
}; | ||
|
||
} // 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.
Uh oh!
There was an error while loading. Please reload this page.