From a3d8f14bf60caa1bee2d7f4931e8f05348d19385 Mon Sep 17 00:00:00 2001 From: Daniel Flores Date: Sat, 15 Nov 2025 12:07:11 -0500 Subject: [PATCH 1/8] std::move --- src/torchcodec/_core/custom_ops.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/torchcodec/_core/custom_ops.cpp b/src/torchcodec/_core/custom_ops.cpp index 3836e52da..75a8d1395 100644 --- a/src/torchcodec/_core/custom_ops.cpp +++ b/src/torchcodec/_core/custom_ops.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include "AVIOFileLikeContext.h" #include "AVIOTensorContext.h" #include "Encoder.h" @@ -620,7 +621,7 @@ void encode_video_to_file( std::optional> extra_options = std::nullopt) { VideoStreamOptions videoStreamOptions; videoStreamOptions.codec = codec; - videoStreamOptions.pixelFormat = pixel_format; + videoStreamOptions.pixelFormat = std::move(pixel_format); videoStreamOptions.crf = crf; videoStreamOptions.preset = preset; @@ -649,7 +650,7 @@ at::Tensor encode_video_to_tensor( auto avioContextHolder = std::make_unique(); VideoStreamOptions videoStreamOptions; videoStreamOptions.codec = codec; - videoStreamOptions.pixelFormat = pixel_format; + videoStreamOptions.pixelFormat = std::move(pixel_format); videoStreamOptions.crf = crf; videoStreamOptions.preset = preset; @@ -685,7 +686,7 @@ void _encode_video_to_file_like( VideoStreamOptions videoStreamOptions; videoStreamOptions.codec = codec; - videoStreamOptions.pixelFormat = pixel_format; + videoStreamOptions.pixelFormat = std::move(pixel_format); videoStreamOptions.crf = crf; videoStreamOptions.preset = preset; From d1c0b699936ada50f0b7b480e005a45c044581b0 Mon Sep 17 00:00:00 2001 From: Daniel Flores Date: Sat, 15 Nov 2025 12:54:17 -0500 Subject: [PATCH 2/8] add missing default None --- src/torchcodec/_core/ops.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/torchcodec/_core/ops.py b/src/torchcodec/_core/ops.py index 6823f4037..c3562f679 100644 --- a/src/torchcodec/_core/ops.py +++ b/src/torchcodec/_core/ops.py @@ -331,7 +331,7 @@ def encode_video_to_file_abstract( frames: torch.Tensor, frame_rate: int, filename: str, - codec: Optional[str], + codec: Optional[str] = None, pixel_format: Optional[str] = None, preset: Optional[str] = None, crf: Optional[Union[int, float]] = None, @@ -345,7 +345,7 @@ def encode_video_to_tensor_abstract( frames: torch.Tensor, frame_rate: int, format: str, - codec: Optional[str], + codec: Optional[str] = None, pixel_format: Optional[str] = None, preset: Optional[str] = None, crf: Optional[Union[int, float]] = None, @@ -360,7 +360,7 @@ def _encode_video_to_file_like_abstract( frame_rate: int, format: str, file_like_context: int, - codec: Optional[str], + codec: Optional[str] = None, pixel_format: Optional[str] = None, preset: Optional[str] = None, crf: Optional[Union[int, float]] = None, From 9cdde313bbc07fe3bfb529b1165b67bde17980a4 Mon Sep 17 00:00:00 2001 From: Daniel Flores Date: Mon, 17 Nov 2025 00:23:21 -0500 Subject: [PATCH 3/8] remove unused exception, move codec param --- src/torchcodec/_core/Encoder.cpp | 2 +- src/torchcodec/_core/custom_ops.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/torchcodec/_core/Encoder.cpp b/src/torchcodec/_core/Encoder.cpp index 3d052ab50..362a02a95 100644 --- a/src/torchcodec/_core/Encoder.cpp +++ b/src/torchcodec/_core/Encoder.cpp @@ -607,7 +607,7 @@ void tryToValidateCodecOption( "] for this codec. For more details, run 'ffmpeg -h encoder=", avCodec.name, "'"); - } catch (const std::invalid_argument& e) { + } catch (const std::invalid_argument&) { TORCH_CHECK( false, "Option ", diff --git a/src/torchcodec/_core/custom_ops.cpp b/src/torchcodec/_core/custom_ops.cpp index 75a8d1395..59bb626e3 100644 --- a/src/torchcodec/_core/custom_ops.cpp +++ b/src/torchcodec/_core/custom_ops.cpp @@ -620,7 +620,7 @@ void encode_video_to_file( std::optional preset = std::nullopt, std::optional> extra_options = std::nullopt) { VideoStreamOptions videoStreamOptions; - videoStreamOptions.codec = codec; + videoStreamOptions.codec = std::move(codec); videoStreamOptions.pixelFormat = std::move(pixel_format); videoStreamOptions.crf = crf; videoStreamOptions.preset = preset; @@ -649,7 +649,7 @@ at::Tensor encode_video_to_tensor( std::optional> extra_options = std::nullopt) { auto avioContextHolder = std::make_unique(); VideoStreamOptions videoStreamOptions; - videoStreamOptions.codec = codec; + videoStreamOptions.codec = std::move(codec); videoStreamOptions.pixelFormat = std::move(pixel_format); videoStreamOptions.crf = crf; videoStreamOptions.preset = preset; @@ -685,7 +685,7 @@ void _encode_video_to_file_like( std::unique_ptr avioContextHolder(fileLikeContext); VideoStreamOptions videoStreamOptions; - videoStreamOptions.codec = codec; + videoStreamOptions.codec = std::move(codec); videoStreamOptions.pixelFormat = std::move(pixel_format); videoStreamOptions.crf = crf; videoStreamOptions.preset = preset; From a7a25b765129fbd56c689644fad315f411907245 Mon Sep 17 00:00:00 2001 From: Daniel Flores Date: Mon, 17 Nov 2025 11:40:56 -0500 Subject: [PATCH 4/8] use string_view consistently --- src/torchcodec/_core/custom_ops.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/torchcodec/_core/custom_ops.cpp b/src/torchcodec/_core/custom_ops.cpp index 59bb626e3..27171e6b9 100644 --- a/src/torchcodec/_core/custom_ops.cpp +++ b/src/torchcodec/_core/custom_ops.cpp @@ -614,11 +614,11 @@ void encode_video_to_file( const at::Tensor& frames, int64_t frame_rate, std::string_view file_name, - std::optional codec = std::nullopt, + std::optional codec = std::nullopt, std::optional pixel_format = std::nullopt, std::optional crf = std::nullopt, std::optional preset = std::nullopt, - std::optional> extra_options = std::nullopt) { + std::optional> extra_options = std::nullopt) { VideoStreamOptions videoStreamOptions; videoStreamOptions.codec = std::move(codec); videoStreamOptions.pixelFormat = std::move(pixel_format); @@ -642,11 +642,11 @@ at::Tensor encode_video_to_tensor( const at::Tensor& frames, int64_t frame_rate, std::string_view format, - std::optional codec = std::nullopt, + std::optional codec = std::nullopt, std::optional pixel_format = std::nullopt, std::optional crf = std::nullopt, std::optional preset = std::nullopt, - std::optional> extra_options = std::nullopt) { + std::optional> extra_options = std::nullopt) { auto avioContextHolder = std::make_unique(); VideoStreamOptions videoStreamOptions; videoStreamOptions.codec = std::move(codec); @@ -673,11 +673,11 @@ void _encode_video_to_file_like( int64_t frame_rate, std::string_view format, int64_t file_like_context, - std::optional codec = std::nullopt, + std::optional codec = std::nullopt, std::optional pixel_format = std::nullopt, std::optional crf = std::nullopt, std::optional preset = std::nullopt, - std::optional> extra_options = std::nullopt) { + std::optional> extra_options = std::nullopt) { auto fileLikeContext = reinterpret_cast(file_like_context); TORCH_CHECK( From 6f199e8a3e4fc2e433d41aff1a993d5e33a3f6b8 Mon Sep 17 00:00:00 2001 From: Daniel Flores Date: Mon, 17 Nov 2025 11:59:23 -0500 Subject: [PATCH 5/8] make extra_options keyword only in to_file --- src/torchcodec/encoders/_video_encoder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/torchcodec/encoders/_video_encoder.py b/src/torchcodec/encoders/_video_encoder.py index 909cf73a9..0bb754025 100644 --- a/src/torchcodec/encoders/_video_encoder.py +++ b/src/torchcodec/encoders/_video_encoder.py @@ -35,12 +35,12 @@ def __init__(self, frames: Tensor, *, frame_rate: int): def to_file( self, dest: Union[str, Path], - extra_options: Optional[Dict[str, Any]] = None, *, codec: Optional[str] = None, pixel_format: Optional[str] = None, crf: Optional[Union[int, float]] = None, preset: Optional[Union[str, int]] = None, + extra_options: Optional[Dict[str, Any]] = None, ) -> None: """Encode frames into a file. From 5e7b0293443c108b8f1159d193152a0e6d5a2c25 Mon Sep 17 00:00:00 2001 From: Daniel Flores Date: Mon, 17 Nov 2025 13:22:13 -0500 Subject: [PATCH 6/8] fix unflattenExtraOptions arg type --- src/torchcodec/_core/custom_ops.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/torchcodec/_core/custom_ops.cpp b/src/torchcodec/_core/custom_ops.cpp index 27171e6b9..666b17718 100644 --- a/src/torchcodec/_core/custom_ops.cpp +++ b/src/torchcodec/_core/custom_ops.cpp @@ -161,10 +161,10 @@ std::string quoteValue(const std::string& value) { // Helper function to unflatten extra_options, alternating keys and values std::map unflattenExtraOptions( - const std::vector& opts) { + const std::vector& opts) { std::map optionsMap; for (size_t i = 0; i < opts.size(); i += 2) { - optionsMap[opts[i]] = opts[i + 1]; + optionsMap[opts[i].data()] = opts[i + 1].data(); } return optionsMap; } From effe99228b29ec41784af8b3f71ce7c6e4cabfe3 Mon Sep 17 00:00:00 2001 From: Daniel Flores Date: Mon, 17 Nov 2025 14:10:09 -0500 Subject: [PATCH 7/8] return to string for extra_options arg --- 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 666b17718..625913847 100644 --- a/src/torchcodec/_core/custom_ops.cpp +++ b/src/torchcodec/_core/custom_ops.cpp @@ -161,10 +161,10 @@ std::string quoteValue(const std::string& value) { // Helper function to unflatten extra_options, alternating keys and values std::map unflattenExtraOptions( - const std::vector& opts) { + const std::vector& opts) { std::map optionsMap; for (size_t i = 0; i < opts.size(); i += 2) { - optionsMap[opts[i].data()] = opts[i + 1].data(); + optionsMap[opts[i]] = opts[i + 1]; } return optionsMap; } @@ -618,7 +618,7 @@ void encode_video_to_file( std::optional pixel_format = std::nullopt, std::optional crf = std::nullopt, std::optional preset = std::nullopt, - std::optional> extra_options = std::nullopt) { + std::optional> extra_options = std::nullopt) { VideoStreamOptions videoStreamOptions; videoStreamOptions.codec = std::move(codec); videoStreamOptions.pixelFormat = std::move(pixel_format); @@ -646,7 +646,7 @@ at::Tensor encode_video_to_tensor( std::optional pixel_format = std::nullopt, std::optional crf = std::nullopt, std::optional preset = std::nullopt, - std::optional> extra_options = std::nullopt) { + std::optional> extra_options = std::nullopt) { auto avioContextHolder = std::make_unique(); VideoStreamOptions videoStreamOptions; videoStreamOptions.codec = std::move(codec); @@ -677,7 +677,7 @@ void _encode_video_to_file_like( std::optional pixel_format = std::nullopt, std::optional crf = std::nullopt, std::optional preset = std::nullopt, - std::optional> extra_options = std::nullopt) { + std::optional> extra_options = std::nullopt) { auto fileLikeContext = reinterpret_cast(file_like_context); TORCH_CHECK( From e41b5128ebdc5a317e688bde2d454cbfd77dd22a Mon Sep 17 00:00:00 2001 From: Daniel Flores Date: Tue, 18 Nov 2025 10:52:44 -0500 Subject: [PATCH 8/8] remove unneeded include --- src/torchcodec/_core/custom_ops.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/torchcodec/_core/custom_ops.cpp b/src/torchcodec/_core/custom_ops.cpp index 625913847..14ca48a7b 100644 --- a/src/torchcodec/_core/custom_ops.cpp +++ b/src/torchcodec/_core/custom_ops.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include "AVIOFileLikeContext.h" #include "AVIOTensorContext.h" #include "Encoder.h"