@@ -8,16 +8,19 @@ namespace facebook::torchcodec {
8
8
9
9
namespace {
10
10
11
- torch::Tensor validateWf (torch::Tensor wf ) {
11
+ torch::Tensor validateSamples (torch::Tensor samples ) {
12
12
TORCH_CHECK (
13
- wf.dtype () == torch::kFloat32 ,
14
- " waveform must have float32 dtype, got " ,
15
- wf.dtype ());
16
- TORCH_CHECK (wf.dim () == 2 , " waveform must have 2 dimensions, got " , wf.dim ());
13
+ samples.dtype () == torch::kFloat32 ,
14
+ " samples must have float32 dtype, got " ,
15
+ samples.dtype ());
16
+ TORCH_CHECK (
17
+ samples.dim () == 2 ,
18
+ " samples must have 2 dimensions, got " ,
19
+ samples.dim ());
17
20
18
21
// We enforce this, but if we get user reports we should investigate whether
19
22
// that's actually needed.
20
- int numChannels = static_cast <int >(wf .sizes ()[0 ]);
23
+ int numChannels = static_cast <int >(samples .sizes ()[0 ]);
21
24
TORCH_CHECK (
22
25
numChannels <= AV_NUM_DATA_POINTERS,
23
26
" Trying to encode " ,
@@ -26,7 +29,7 @@ torch::Tensor validateWf(torch::Tensor wf) {
26
29
AV_NUM_DATA_POINTERS,
27
30
" channels per frame." );
28
31
29
- return wf .contiguous ();
32
+ return samples .contiguous ();
30
33
}
31
34
32
35
void validateSampleRate (const AVCodec& avCodec, int sampleRate) {
@@ -71,7 +74,7 @@ static const std::vector<AVSampleFormat> preferredFormatsOrder = {
71
74
72
75
AVSampleFormat findBestOutputSampleFormat (const AVCodec& avCodec) {
73
76
// Find a sample format that the encoder supports. We prefer using FLT[P],
74
- // since this is the format of the input waveform . If FLTP isn't supported
77
+ // since this is the format of the input samples . If FLTP isn't supported
75
78
// then we'll need to convert the AVFrame's format. Our heuristic is to encode
76
79
// into the format with the highest resolution.
77
80
if (avCodec.sample_fmts == nullptr ) {
@@ -98,11 +101,11 @@ AVSampleFormat findBestOutputSampleFormat(const AVCodec& avCodec) {
98
101
AudioEncoder::~AudioEncoder () {}
99
102
100
103
AudioEncoder::AudioEncoder (
101
- const torch::Tensor wf ,
104
+ const torch::Tensor samples ,
102
105
int sampleRate,
103
106
std::string_view fileName,
104
107
const AudioStreamOptions& audioStreamOptions)
105
- : wf_(validateWf(wf )) {
108
+ : samples_(validateSamples(samples )) {
106
109
setFFmpegLogLevel ();
107
110
AVFormatContext* avFormatContext = nullptr ;
108
111
int status = avformat_alloc_output_context2 (
@@ -129,12 +132,13 @@ AudioEncoder::AudioEncoder(
129
132
}
130
133
131
134
AudioEncoder::AudioEncoder (
132
- const torch::Tensor wf ,
135
+ const torch::Tensor samples ,
133
136
int sampleRate,
134
137
std::string_view formatName,
135
138
std::unique_ptr<AVIOToTensorContext> avioContextHolder,
136
139
const AudioStreamOptions& audioStreamOptions)
137
- : wf_(validateWf(wf)), avioContextHolder_(std::move(avioContextHolder)) {
140
+ : samples_(validateSamples(samples)),
141
+ avioContextHolder_ (std::move(avioContextHolder)) {
138
142
setFFmpegLogLevel ();
139
143
AVFormatContext* avFormatContext = nullptr ;
140
144
int status = avformat_alloc_output_context2 (
@@ -176,8 +180,8 @@ void AudioEncoder::initializeEncoder(
176
180
// well when "-b:a" isn't specified.
177
181
avCodecContext_->bit_rate = desiredBitRate.value_or (0 );
178
182
179
- outNumChannels_ =
180
- static_cast < int >( audioStreamOptions.numChannels .value_or (wf_ .sizes ()[0 ]));
183
+ outNumChannels_ = static_cast < int >(
184
+ audioStreamOptions.numChannels .value_or (samples_ .sizes ()[0 ]));
181
185
validateNumChannels (*avCodec, outNumChannels_);
182
186
// The avCodecContext layout defines the layout of the encoded output, it's
183
187
// not related to the input sampes.
@@ -186,9 +190,9 @@ void AudioEncoder::initializeEncoder(
186
190
validateSampleRate (*avCodec, sampleRate);
187
191
avCodecContext_->sample_rate = sampleRate;
188
192
189
- // Input waveform is expected to be FLTP. Not all encoders support FLTP, so we
190
- // may need to convert the wf into a supported output sample format, which is
191
- // what the `.sample_fmt` defines.
193
+ // Input samples are expected to be FLTP. Not all encoders support FLTP, so we
194
+ // may need to convert the samples into a supported output sample format,
195
+ // which is what the `.sample_fmt` defines.
192
196
avCodecContext_->sample_fmt = findBestOutputSampleFormat (*avCodec);
193
197
194
198
int status = avcodec_open2 (avCodecContext_.get (), avCodec, nullptr );
@@ -237,7 +241,7 @@ void AudioEncoder::encode() {
237
241
avFrame->pts = 0 ;
238
242
// We set the channel layout of the frame to the default layout corresponding
239
243
// to the input samples' number of channels
240
- setDefaultChannelLayout (avFrame, static_cast <int >(wf_ .sizes ()[0 ]));
244
+ setDefaultChannelLayout (avFrame, static_cast <int >(samples_ .sizes ()[0 ]));
241
245
242
246
auto status = av_frame_get_buffer (avFrame.get (), 0 );
243
247
TORCH_CHECK (
@@ -247,10 +251,10 @@ void AudioEncoder::encode() {
247
251
248
252
AutoAVPacket autoAVPacket;
249
253
250
- uint8_t * pwf = static_cast <uint8_t *>(wf_ .data_ptr ());
251
- int numSamples = static_cast <int >(wf_ .sizes ()[1 ]); // per channel
254
+ uint8_t * psamples = static_cast <uint8_t *>(samples_ .data_ptr ());
255
+ int numSamples = static_cast <int >(samples_ .sizes ()[1 ]); // per channel
252
256
int numEncodedSamples = 0 ; // per channel
253
- int numBytesPerSample = static_cast <int >(wf_ .element_size ());
257
+ int numBytesPerSample = static_cast <int >(samples_ .element_size ());
254
258
int numBytesPerChannel = numSamples * numBytesPerSample;
255
259
256
260
status = avformat_write_header (avFormatContext_.get (), nullptr );
@@ -270,11 +274,13 @@ void AudioEncoder::encode() {
270
274
std::min (numSamplesAllocatedPerFrame, numSamples - numEncodedSamples);
271
275
int numBytesToEncode = numSamplesToEncode * numBytesPerSample;
272
276
273
- for (int ch = 0 ; ch < wf_ .sizes ()[0 ]; ch++) {
277
+ for (int ch = 0 ; ch < samples_ .sizes ()[0 ]; ch++) {
274
278
std::memcpy (
275
- avFrame->data [ch], pwf + ch * numBytesPerChannel, numBytesToEncode);
279
+ avFrame->data [ch],
280
+ psamples + ch * numBytesPerChannel,
281
+ numBytesToEncode);
276
282
}
277
- pwf += numBytesToEncode;
283
+ psamples += numBytesToEncode;
278
284
279
285
// Above, we set the AVFrame's .nb_samples to AVCodecContext.frame_size so
280
286
// that the frame buffers are allocated to a big enough size. Here, we reset
0 commit comments