Skip to content

Commit 1ea235a

Browse files
authored
Use std::move to reduce copies in VideoEncoder (#1056)
1 parent b0097cc commit 1ea235a

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

src/torchcodec/_core/Encoder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ void tryToValidateCodecOption(
607607
"] for this codec. For more details, run 'ffmpeg -h encoder=",
608608
avCodec.name,
609609
"'");
610-
} catch (const std::invalid_argument& e) {
610+
} catch (const std::invalid_argument&) {
611611
TORCH_CHECK(
612612
false,
613613
"Option ",

src/torchcodec/_core/custom_ops.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -613,14 +613,14 @@ void encode_video_to_file(
613613
const at::Tensor& frames,
614614
int64_t frame_rate,
615615
std::string_view file_name,
616-
std::optional<std::string> codec = std::nullopt,
616+
std::optional<std::string_view> codec = std::nullopt,
617617
std::optional<std::string_view> pixel_format = std::nullopt,
618618
std::optional<double> crf = std::nullopt,
619619
std::optional<std::string_view> preset = std::nullopt,
620620
std::optional<std::vector<std::string>> extra_options = std::nullopt) {
621621
VideoStreamOptions videoStreamOptions;
622-
videoStreamOptions.codec = codec;
623-
videoStreamOptions.pixelFormat = pixel_format;
622+
videoStreamOptions.codec = std::move(codec);
623+
videoStreamOptions.pixelFormat = std::move(pixel_format);
624624
videoStreamOptions.crf = crf;
625625
videoStreamOptions.preset = preset;
626626

@@ -641,15 +641,15 @@ at::Tensor encode_video_to_tensor(
641641
const at::Tensor& frames,
642642
int64_t frame_rate,
643643
std::string_view format,
644-
std::optional<std::string> codec = std::nullopt,
644+
std::optional<std::string_view> codec = std::nullopt,
645645
std::optional<std::string_view> pixel_format = std::nullopt,
646646
std::optional<double> crf = std::nullopt,
647647
std::optional<std::string_view> preset = std::nullopt,
648648
std::optional<std::vector<std::string>> extra_options = std::nullopt) {
649649
auto avioContextHolder = std::make_unique<AVIOToTensorContext>();
650650
VideoStreamOptions videoStreamOptions;
651-
videoStreamOptions.codec = codec;
652-
videoStreamOptions.pixelFormat = pixel_format;
651+
videoStreamOptions.codec = std::move(codec);
652+
videoStreamOptions.pixelFormat = std::move(pixel_format);
653653
videoStreamOptions.crf = crf;
654654
videoStreamOptions.preset = preset;
655655

@@ -672,7 +672,7 @@ void _encode_video_to_file_like(
672672
int64_t frame_rate,
673673
std::string_view format,
674674
int64_t file_like_context,
675-
std::optional<std::string> codec = std::nullopt,
675+
std::optional<std::string_view> codec = std::nullopt,
676676
std::optional<std::string_view> pixel_format = std::nullopt,
677677
std::optional<double> crf = std::nullopt,
678678
std::optional<std::string_view> preset = std::nullopt,
@@ -684,8 +684,8 @@ void _encode_video_to_file_like(
684684
std::unique_ptr<AVIOFileLikeContext> avioContextHolder(fileLikeContext);
685685

686686
VideoStreamOptions videoStreamOptions;
687-
videoStreamOptions.codec = codec;
688-
videoStreamOptions.pixelFormat = pixel_format;
687+
videoStreamOptions.codec = std::move(codec);
688+
videoStreamOptions.pixelFormat = std::move(pixel_format);
689689
videoStreamOptions.crf = crf;
690690
videoStreamOptions.preset = preset;
691691

src/torchcodec/_core/ops.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def encode_video_to_file_abstract(
331331
frames: torch.Tensor,
332332
frame_rate: int,
333333
filename: str,
334-
codec: Optional[str],
334+
codec: Optional[str] = None,
335335
pixel_format: Optional[str] = None,
336336
preset: Optional[str] = None,
337337
crf: Optional[Union[int, float]] = None,
@@ -345,7 +345,7 @@ def encode_video_to_tensor_abstract(
345345
frames: torch.Tensor,
346346
frame_rate: int,
347347
format: str,
348-
codec: Optional[str],
348+
codec: Optional[str] = None,
349349
pixel_format: Optional[str] = None,
350350
preset: Optional[str] = None,
351351
crf: Optional[Union[int, float]] = None,
@@ -360,7 +360,7 @@ def _encode_video_to_file_like_abstract(
360360
frame_rate: int,
361361
format: str,
362362
file_like_context: int,
363-
codec: Optional[str],
363+
codec: Optional[str] = None,
364364
pixel_format: Optional[str] = None,
365365
preset: Optional[str] = None,
366366
crf: Optional[Union[int, float]] = None,

src/torchcodec/encoders/_video_encoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ def __init__(self, frames: Tensor, *, frame_rate: int):
3535
def to_file(
3636
self,
3737
dest: Union[str, Path],
38-
extra_options: Optional[Dict[str, Any]] = None,
3938
*,
4039
codec: Optional[str] = None,
4140
pixel_format: Optional[str] = None,
4241
crf: Optional[Union[int, float]] = None,
4342
preset: Optional[Union[str, int]] = None,
43+
extra_options: Optional[Dict[str, Any]] = None,
4444
) -> None:
4545
"""Encode frames into a file.
4646

0 commit comments

Comments
 (0)