Skip to content

Commit 597fef3

Browse files
danielflores3Dan-Flores
authored andcommitted
Rename 'wf' to 'samples' in AudioEncoder
1 parent f655357 commit 597fef3

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/torchcodec/_core/Encoder.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ torch::Tensor validateSamples(torch::Tensor samples) {
1313
samples.dtype() == torch::kFloat32,
1414
"samples must have float32 dtype, got ",
1515
samples.dtype());
16-
TORCH_CHECK(samples.dim() == 2, "samples must have 2 dimensions, got ", samples.dim());
16+
TORCH_CHECK(
17+
samples.dim() == 2,
18+
"samples must have 2 dimensions, got ",
19+
samples.dim());
1720

1821
// We enforce this, but if we get user reports we should investigate whether
1922
// that's actually needed.
@@ -102,7 +105,7 @@ AudioEncoder::AudioEncoder(
102105
int sampleRate,
103106
std::string_view fileName,
104107
const AudioStreamOptions& audioStreamOptions)
105-
: samples_(validateSamples(samples)) {
108+
: samples_(validateSamples(samples)) {
106109
setFFmpegLogLevel();
107110
AVFormatContext* avFormatContext = nullptr;
108111
int status = avformat_alloc_output_context2(
@@ -134,7 +137,8 @@ AudioEncoder::AudioEncoder(
134137
std::string_view formatName,
135138
std::unique_ptr<AVIOToTensorContext> avioContextHolder,
136139
const AudioStreamOptions& audioStreamOptions)
137-
: samples_(validateSamples(samples)), avioContextHolder_(std::move(avioContextHolder)) {
140+
: samples_(validateSamples(samples)),
141+
avioContextHolder_(std::move(avioContextHolder)) {
138142
setFFmpegLogLevel();
139143
AVFormatContext* avFormatContext = nullptr;
140144
int status = avformat_alloc_output_context2(
@@ -175,8 +179,9 @@ void AudioEncoder::initializeEncoder(
175179
// bit_rate=None defaults to 0, which is what the FFmpeg CLI seems to use as
176180
// well when "-b:a" isn't specified.
177181
avCodecContext_->bit_rate = desiredBitRate.value_or(0);
178-
outNumChannels_ =
179-
static_cast<int>(audioStreamOptions.numChannels.value_or(samples_.sizes()[0]));
182+
183+
outNumChannels_ = static_cast<int>(
184+
audioStreamOptions.numChannels.value_or(samples_.sizes()[0]));
180185
validateNumChannels(*avCodec, outNumChannels_);
181186
// The avCodecContext layout defines the layout of the encoded output, it's
182187
// not related to the input sampes.
@@ -186,11 +191,12 @@ void AudioEncoder::initializeEncoder(
186191
avCodecContext_->sample_rate = sampleRate;
187192

188193
// Input samples are expected to be FLTP. Not all encoders support FLTP, so we
189-
// may need to convert the samples into a supported output sample format, which is
190-
// what the `.sample_fmt` defines.
194+
// may need to convert the samples into a supported output sample format,
195+
// which is what the `.sample_fmt` defines.
191196
avCodecContext_->sample_fmt = findBestOutputSampleFormat(*avCodec);
192197

193-
setDefaultChannelLayout(avCodecContext_, static_cast<int>(samples_.sizes()[0]));
198+
setDefaultChannelLayout(
199+
avCodecContext_, static_cast<int>(samples_.sizes()[0]));
194200

195201
int status = avcodec_open2(avCodecContext_.get(), avCodec, nullptr);
196202
TORCH_CHECK(
@@ -273,7 +279,9 @@ void AudioEncoder::encode() {
273279

274280
for (int ch = 0; ch < samples_.sizes()[0]; ch++) {
275281
std::memcpy(
276-
avFrame->data[ch], psamples + ch * numBytesPerChannel, numBytesToEncode);
282+
avFrame->data[ch],
283+
psamples + ch * numBytesPerChannel,
284+
numBytesToEncode);
277285
}
278286
psamples += numBytesToEncode;
279287

src/torchcodec/_core/Encoder.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,16 @@ class AudioEncoder {
4747
UniqueAVCodecContext avCodecContext_;
4848
int streamIndex_;
4949
UniqueSwrContext swrContext_;
50+
5051
AudioStreamOptions audioStreamOptions;
5152

5253
int outNumChannels_ = -1;
5354

55+
// TODO-ENCODING: desiredNumChannels should just be part of an options struct,
56+
// see other TODO above.
57+
int desiredNumChannels_ = -1;
58+
int outNumChannels_ = -1;
59+
5460
const torch::Tensor samples_;
5561

5662
// Stores the AVIOContext for the output tensor buffer.

0 commit comments

Comments
 (0)