Skip to content

AudioDecoder: specify desired num_channels #678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 15, 2025

Conversation

NicolasHug
Copy link
Member

Closes #675

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Meta Open Source bot. label May 13, 2025
AudioStreamOptions audioStreamOptions;
audioStreamOptions.sampleRate = sample_rate;
audioStreamOptions.numChannels = num_channels;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implicitly converts an optional int64 to an optional int. The same implicit conversion already happens for sample_rate, and for the VideoStreamOptions' fields like height, width, and ffmpegThreadCount.

I'll open an issue to fix / check these all at once.

}
convertedAVFrame = convertAudioAVFrameSampleFormatAndSampleRate(
convertedAVFrame = convertAudioAVFrameSamples(
Copy link
Member Author

@NicolasHug NicolasHug May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed the function, because it converts too many things to list all of them now. @scotts I also remember that you wanted to align all the sourceStuff to srcStuff - which I'll do as a quick follow-up!

int desiredSampleRate) {
int desiredSampleRate,
const UniqueAVFrame& srcAVFrame,
int desiredNumChannels) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slightly changed the signature to take an AVFrame instead of the AVCodecContext. This is where we get the source num_channel and source channel layout. It's more accurate to get it from the AVFrame, although they should both always be the same.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I prefer for the primary FFmpeg data structures to appear first in a function. I think of these functions as if they are methods on those structures, and that gets reinforced by making them the first parameter. I think I understand the current order, which srcFrame is placed next to desiredNumChannels because you're getting the source number of channels from srcFrame. But given what we're doing, I think of this function as "Create a swrContext from this AVFrame with these desire parameters."

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that for functions that look like methods, we want the FFmpeg struct to be the first. The reason I removed the avCodecContext from the top and now use an AVFrame is precisely because this function shouldn't read as a method.

I think I understand the current order, which srcFrame is placed next to desiredNumChannels because you're getting the source number of channels from srcFrame

Yes, you're correct. Ideally, we shouldn't need to pass the AVFrame at all, we should just pass its AVChannelLayout as sourceChannelLayout. But unfortunately, AVChannelLayout only exists from FFmpeg 5. That's the main reason we're passing an AVFrame: it's so that we can call createSwrContext() uniformly across ffmpeg versions, and contain the preproc #ifdef directives into FFmpegCommon.cpp.

Does that make sense?

Basically: we're passing an AVFrame because of some FFmpeg version constraint, but really this just be "source channel layout", and thus this isn't a method on an AVFrame at all.

@NicolasHug NicolasHug marked this pull request as ready for review May 14, 2025 11:45
@NicolasHug NicolasHug merged commit 721c315 into pytorch:main May 15, 2025
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Meta Open Source bot.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow user to choose num_channels in AudioDecoder
3 participants