Skip to content

Commit 2710cb6

Browse files
author
Molly Xu
committed
refactor ffmpegcommon
1 parent 4b78bf6 commit 2710cb6

File tree

3 files changed

+6
-9
lines changed

3 files changed

+6
-9
lines changed

src/torchcodec/_core/CudaDeviceInterface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ UniqueAVBufferRef getHardwareDeviceContext(const torch::Device& device) {
6262
AVBufferRef* hw_device_ctx_raw = nullptr;
6363
std::string deviceOrdinal = std::to_string(nonNegativeDeviceIndex);
6464

65-
int flags = getHardwareDeviceCreationFlags();
65+
int flags = (canReuseHardwareContext()) ? AV_CUDA_USE_CURRENT_CONTEXT : 0;
6666
int err = av_hwdevice_ctx_create(
6767
&hw_device_ctx_raw, type, deviceOrdinal.c_str(), nullptr, flags);
6868

src/torchcodec/_core/FFMPEGCommon.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,11 +585,11 @@ int64_t computeSafeDuration(
585585
}
586586
}
587587

588-
int64_t getHardwareDeviceCreationFlags() {
588+
int64_t canReuseHardwareContext() {
589589
// 58.26.100 introduced the concept of reusing the existing cuda context
590590
// which is much faster and lower memory than creating a new cuda context.
591591
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(58, 26, 100)
592-
return AV_CUDA_USE_CURRENT_CONTEXT;
592+
return 1;
593593
#else
594594
return 0;
595595
#endif

src/torchcodec/_core/FFMPEGCommon.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ extern "C" {
2222
#include <libavutil/dict.h>
2323
#include <libavutil/display.h>
2424
#include <libavutil/file.h>
25-
#include <libavutil/hwcontext.h>
26-
#include <libavutil/hwcontext_cuda.h>
2725
#include <libavutil/opt.h>
2826
#include <libavutil/pixfmt.h>
2927
#include <libavutil/version.h>
@@ -243,9 +241,8 @@ AVFilterContext* createBuffersinkFilter(
243241
AVFilterGraph* filterGraph,
244242
enum AVPixelFormat outputFormat);
245243

246-
// Returns the appropriate flags for av_hwdevice_ctx_create() based on FFmpeg
247-
// version. This abstracts FFmpeg version differences for hardware device
248-
// context creation.
249-
int64_t getHardwareDeviceCreationFlags();
244+
// Returns whether hardware context can be reused based on FFmpeg
245+
// version.
246+
int64_t canReuseHardwareContext();
250247

251248
} // namespace facebook::torchcodec

0 commit comments

Comments
 (0)