From 3a6ca0cde195ab755c8007a5572a0cd06037533a Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Sun, 29 Mar 2026 16:42:45 +0800 Subject: [PATCH 1/3] reduce narrow, see: paddlepaddle/paddle#77544 --- src/torchcodec/_core/AVIOTensorContext.cpp | 6 ++---- src/torchcodec/_core/SingleStreamDecoder.cpp | 7 ++----- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/torchcodec/_core/AVIOTensorContext.cpp b/src/torchcodec/_core/AVIOTensorContext.cpp index 263ce2228..238475761 100644 --- a/src/torchcodec/_core/AVIOTensorContext.cpp +++ b/src/torchcodec/_core/AVIOTensorContext.cpp @@ -123,10 +123,8 @@ AVIOToTensorContext::AVIOToTensorContext() } torch::Tensor AVIOToTensorContext::getOutputTensor() { - throw std::runtime_error( - "AVIOToTensorContext::getOutputTensor is not implemented yet."); - // return tensorContext_.data.narrow( - // /*dim=*/0, /*start=*/0, /*length=*/tensorContext_.max_pos); + return tensorContext_.data.narrow( + /*dim=*/0, /*start=*/0, /*length=*/tensorContext_.max_pos); } } // namespace facebook::torchcodec diff --git a/src/torchcodec/_core/SingleStreamDecoder.cpp b/src/torchcodec/_core/SingleStreamDecoder.cpp index bd87f12d3..e078c7435 100644 --- a/src/torchcodec/_core/SingleStreamDecoder.cpp +++ b/src/torchcodec/_core/SingleStreamDecoder.cpp @@ -1419,11 +1419,8 @@ std::optional SingleStreamDecoder::maybeFlushSwrBuffers() { auto actualNumRemainingSamples = swr_convert( swrContext_.get(), outputBuffers.data(), numRemainingSamples, nullptr, 0); - throw std::runtime_error( - "SingleStreamDecoder::maybeFlushSwrBuffers is not implemented yet."); - - // return lastSamples.narrow( - // /*dim=*/1, /*start=*/0, /*length=*/actualNumRemainingSamples); + return lastSamples.narrow( + /*dim=*/1, /*start=*/0, /*length=*/actualNumRemainingSamples); } // -------------------------------------------------------------------------- From 22b2c3e6124caf7d91c13371c310ac653d9ee9ed Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Sun, 5 Apr 2026 00:43:49 +0800 Subject: [PATCH 2/3] feat: support BackendSelect --- src/torchcodec/_core/custom_ops.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/torchcodec/_core/custom_ops.cpp b/src/torchcodec/_core/custom_ops.cpp index 13ad3be35..f7bfceec1 100644 --- a/src/torchcodec/_core/custom_ops.cpp +++ b/src/torchcodec/_core/custom_ops.cpp @@ -10,12 +10,12 @@ #include #include "c10/core/SymIntArrayRef.h" #include "c10/util/Exception.h" -#include "torch/library.h" #include "src/torchcodec/_core/AVIOFileLikeContext.h" #include "src/torchcodec/_core/AVIOTensorContext.h" #include "src/torchcodec/_core/Encoder.h" #include "src/torchcodec/_core/SingleStreamDecoder.h" #include "src/torchcodec/_core/ValidationUtils.h" +#include "torch/library.h" namespace facebook::torchcodec { @@ -118,7 +118,7 @@ OpsFrameOutput makeOpsFrameOutput(FrameOutput& frame) { // frame.data, // torch::tensor(frame.ptsSeconds, torch::dtype(torch::kFloat64)), // torch::tensor(frame.durationSeconds, torch::dtype(torch::kFloat64))); - return std::make_tuple( + return std::make_tuple( frame.data, torch::full({}, frame.ptsSeconds, torch::kFloat64), torch::full({}, frame.durationSeconds, torch::kFloat64)); @@ -920,15 +920,15 @@ void scan_all_streams_to_update_metadata(at::Tensor& decoder) { videoDecoder->scanFileAndUpdateMetadataAndIndex(); } -TORCH_LIBRARY_IMPL(torchcodec_ns, CPU, m) { +TORCH_LIBRARY_IMPL(torchcodec_ns, BackendSelect, m) { m.impl("create_from_file", &create_from_file); m.impl("create_from_tensor", &create_from_tensor); m.impl("_create_from_file_like", &_create_from_file_like); m.impl( "_get_json_ffmpeg_library_versions", &_get_json_ffmpeg_library_versions); -// } +} -// TORCH_LIBRARY_IMPL(torchcodec_ns, CPU, m) { +TORCH_LIBRARY_IMPL(torchcodec_ns, CPU, m) { m.impl("encode_audio_to_file", &encode_audio_to_file); m.impl("encode_audio_to_tensor", &encode_audio_to_tensor); m.impl("_encode_audio_to_file_like", &_encode_audio_to_file_like); From c348bb8e136a659c25882c7ed7b0dabddfb0ab1b Mon Sep 17 00:00:00 2001 From: gouzi <530971494@qq.com> Date: Sun, 5 Apr 2026 12:32:20 +0800 Subject: [PATCH 3/3] fix: enable audio frame decoding check in getFramesPlayedInRangeAudio --- src/torchcodec/_core/SingleStreamDecoder.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/torchcodec/_core/SingleStreamDecoder.cpp b/src/torchcodec/_core/SingleStreamDecoder.cpp index e078c7435..8d9e9f651 100644 --- a/src/torchcodec/_core/SingleStreamDecoder.cpp +++ b/src/torchcodec/_core/SingleStreamDecoder.cpp @@ -1030,15 +1030,15 @@ AudioFramesOutput SingleStreamDecoder::getFramesPlayedInRangeAudio( frames.push_back(*lastSamples); } - // TORCH_CHECK( - // frames.size() > 0 && firstFramePtsSeconds.has_value(), - // "No audio frames were decoded. ", - // "This is probably because start_seconds is too high(", - // startSeconds, - // "),", - // "or because stop_seconds(", - // stopSecondsOptional, - // ") is too low."); + TORCH_CHECK( + frames.size() > 0 && firstFramePtsSeconds.has_value(), + "No audio frames were decoded. ", + "This is probably because start_seconds is too high(", + startSeconds, + "),", + "or because stop_seconds(", + stopSecondsOptional, + ") is too low."); return AudioFramesOutput{torch::cat(frames, 1), *firstFramePtsSeconds}; }