Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/torchcodec/_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function(make_torchcodec_libraries
CpuDeviceInterface.cpp
SingleStreamDecoder.cpp
Encoder.cpp
ValidationUtils.cpp
)

if(ENABLE_CUDA)
Expand Down
35 changes: 35 additions & 0 deletions src/torchcodec/_core/ValidationUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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.

#include "src/torchcodec/_core/ValidationUtils.h"
#include <limits>
#include "c10/util/Exception.h"

namespace facebook::torchcodec {

int validateInt64ToInt(int64_t value, const std::string& parameterName) {
TORCH_CHECK(
value >= std::numeric_limits<int>::min() &&
value <= std::numeric_limits<int>::max(),
parameterName,
"=",
value,
" is out of range for int type.");

return static_cast<int>(value);
}

std::optional<int> validateOptionalInt64ToInt(
const std::optional<int64_t>& value,
const std::string& parameterName) {
if (value.has_value()) {
return validateInt64ToInt(value.value(), parameterName);
} else {
return std::nullopt;
}
}

} // namespace facebook::torchcodec
21 changes: 21 additions & 0 deletions src/torchcodec/_core/ValidationUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.

#pragma once

#include <cstdint>
#include <optional>
#include <string>

namespace facebook::torchcodec {

int validateInt64ToInt(int64_t value, const std::string& parameterName);

std::optional<int> validateOptionalInt64ToInt(
const std::optional<int64_t>& value,
const std::string& parameterName);

} // namespace facebook::torchcodec
38 changes: 16 additions & 22 deletions src/torchcodec/_core/custom_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "src/torchcodec/_core/AVIOTensorContext.h"
#include "src/torchcodec/_core/Encoder.h"
#include "src/torchcodec/_core/SingleStreamDecoder.h"
#include "src/torchcodec/_core/ValidationUtils.h"

namespace facebook::torchcodec {

Expand Down Expand Up @@ -164,16 +165,6 @@ std::string mapToJson(const std::map<std::string, std::string>& metadataMap) {
return ss.str();
}

int validateSampleRate(int64_t sampleRate) {
TORCH_CHECK(
sampleRate <= std::numeric_limits<int>::max(),
"sample_rate=",
sampleRate,
" is too large to be cast to an int.");

return static_cast<int>(sampleRate);
}

} // namespace

// ==============================
Expand Down Expand Up @@ -413,14 +404,17 @@ void encode_audio_to_file(
std::optional<int64_t> bit_rate = std::nullopt,
std::optional<int64_t> num_channels = std::nullopt,
std::optional<int64_t> desired_sample_rate = std::nullopt) {
// TODO Fix implicit int conversion:
// https://github.com/pytorch/torchcodec/issues/679
AudioStreamOptions audioStreamOptions;
audioStreamOptions.bitRate = bit_rate;
audioStreamOptions.numChannels = num_channels;
audioStreamOptions.sampleRate = desired_sample_rate;
audioStreamOptions.bitRate = validateOptionalInt64ToInt(bit_rate, "bit_rate");
audioStreamOptions.numChannels =
validateOptionalInt64ToInt(num_channels, "num_channels");
audioStreamOptions.sampleRate =
validateOptionalInt64ToInt(desired_sample_rate, "desired_sample_rate");
AudioEncoder(
samples, validateSampleRate(sample_rate), file_name, audioStreamOptions)
samples,
validateInt64ToInt(sample_rate, "sample_rate"),
file_name,
audioStreamOptions)
.encode();
}

Expand All @@ -432,15 +426,15 @@ at::Tensor encode_audio_to_tensor(
std::optional<int64_t> num_channels = std::nullopt,
std::optional<int64_t> desired_sample_rate = std::nullopt) {
auto avioContextHolder = std::make_unique<AVIOToTensorContext>();
// TODO Fix implicit int conversion:
// https://github.com/pytorch/torchcodec/issues/679
AudioStreamOptions audioStreamOptions;
audioStreamOptions.bitRate = bit_rate;
audioStreamOptions.numChannels = num_channels;
audioStreamOptions.sampleRate = desired_sample_rate;
audioStreamOptions.bitRate = validateOptionalInt64ToInt(bit_rate, "bit_rate");
audioStreamOptions.numChannels =
validateOptionalInt64ToInt(num_channels, "num_channels");
audioStreamOptions.sampleRate =
validateOptionalInt64ToInt(desired_sample_rate, "desired_sample_rate");
return AudioEncoder(
samples,
validateSampleRate(sample_rate),
validateInt64ToInt(sample_rate, "sample_rate"),
format,
std::move(avioContextHolder),
audioStreamOptions)
Expand Down
14 changes: 7 additions & 7 deletions src/torchcodec/_core/pybind_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "src/torchcodec/_core/Encoder.h"
#include "src/torchcodec/_core/SingleStreamDecoder.h"
#include "src/torchcodec/_core/StreamOptions.h"
#include "src/torchcodec/_core/ValidationUtils.h"

namespace py = pybind11;

Expand Down Expand Up @@ -55,20 +56,19 @@ void encode_audio_to_file_like(
auto samples = torch::from_blob(
reinterpret_cast<void*>(data_ptr), shape, tensor_options);

// TODO Fix implicit int conversion:
// https://github.com/pytorch/torchcodec/issues/679
// same for sample_rate parameter below
AudioStreamOptions audioStreamOptions;
audioStreamOptions.bitRate = bit_rate;
audioStreamOptions.numChannels = num_channels;
audioStreamOptions.sampleRate = desired_sample_rate;
audioStreamOptions.bitRate = validateOptionalInt64ToInt(bit_rate, "bit_rate");
audioStreamOptions.numChannels =
validateOptionalInt64ToInt(num_channels, "num_channels");
audioStreamOptions.sampleRate =
validateOptionalInt64ToInt(desired_sample_rate, "desired_sample_rate");

auto avioContextHolder =
std::make_unique<AVIOFileLikeContext>(file_like, /*isForWriting=*/true);

AudioEncoder encoder(
samples,
static_cast<int>(sample_rate),
validateInt64ToInt(sample_rate, "sample_rate"),
format,
std::move(avioContextHolder),
audioStreamOptions);
Expand Down
Loading