Skip to content

Commit 88a87c4

Browse files
committed
Merge branch 'migrate_encoding_test' into encoding_sample_rate_lezzzgo
2 parents c40deef + 952af0f commit 88a87c4

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/torchcodec/_core/Encoder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ void AudioEncoder::initializeEncoder(
183183

184184
numChannelsOutput_ = static_cast<int>(numChannels.value_or(wf_.sizes()[0]));
185185
validateNumChannels(*avCodec, numChannelsOutput_);
186+
// The avCodecContext layout defines the layout of the encoded output, it's
187+
// not related to the input sampes.
186188
setDefaultChannelLayout(avCodecContext_, numChannelsOutput_);
187189

188190
sampleRateOutput_ =
@@ -239,6 +241,8 @@ void AudioEncoder::encode() {
239241
avFrame->format = AV_SAMPLE_FMT_FLTP;
240242
avFrame->sample_rate = sampleRateInput_;
241243
avFrame->pts = 0;
244+
// We set the channel layout of the frame to the default layout corresponding
245+
// to the input samples' number of channels
242246
setDefaultChannelLayout(avFrame, static_cast<int>(wf_.sizes()[0]));
243247

244248
auto status = av_frame_get_buffer(avFrame.get(), 0);

src/torchcodec/_core/FFMPEGCommon.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,17 @@ void validateNumChannels(const AVCodec& avCodec, int numChannels) {
107107
// eventually raise.
108108
return;
109109
}
110-
for (auto i = 0; avCodec.ch_layouts[i].order != AV_CHANNEL_ORDER_UNSPEC;
111-
++i) {
110+
// FFmpeg doc indicate that the ch_layouts array is terminated by a zeroed
111+
// layout, so checking for nb_channels == 0 should indicate its end.
112+
for (auto i = 0; avCodec.ch_layouts[i].nb_channels != 0; ++i) {
112113
if (numChannels == avCodec.ch_layouts[i].nb_channels) {
113114
return;
114115
}
115116
}
117+
// At this point it seems that the encoder doesn't support the requested
118+
// number of channels, so we error out.
116119
std::stringstream supportedNumChannels;
117-
for (auto i = 0; avCodec.ch_layouts[i].order != AV_CHANNEL_ORDER_UNSPEC;
118-
++i) {
120+
for (auto i = 0; avCodec.ch_layouts[i].nb_channels != 0; ++i) {
119121
if (i > 0) {
120122
supportedNumChannels << ", ";
121123
}
@@ -132,6 +134,8 @@ void validateNumChannels(const AVCodec& avCodec, int numChannels) {
132134
return;
133135
}
134136
}
137+
// At this point it seems that the encoder doesn't support the requested
138+
// number of channels, so we error out.
135139
std::stringstream supportedNumChannels;
136140
for (auto i = 0; avCodec.channel_layouts[i] != 0; ++i) {
137141
if (i > 0) {

0 commit comments

Comments
 (0)