Commit bf1214a9 authored by moto's avatar moto Committed by Facebook GitHub Bot
Browse files

Set "experimental" automatically when using native opus/vorbis encoder (#3192)

Summary:
OPUS encoder and VORBIS encoders require "strict=experimental" flags. This commit enables it automatically.

The rational behind of it is typically we care if we can encode these formats at all and not how they are encoded. (This might be concern when these encoder becomes more mature on FFmpeg side and providing flags would result in weird behavior)

Also when writing high-level functions that uses StreamWriter, if we do not set these flags, then these high-level functions have to add new options that should be passed down to StreamWriter, which turned out to be very painful in https://github.com/pytorch/audio/issues/3163

Pull Request resolved: https://github.com/pytorch/audio/pull/3192

Reviewed By: nateanl

Differential Revision: D44275089

Pulled By: mthrok

fbshipit-source-id: 74a757b4b7fc8467c8c88ffcb54fbaf89d6e4384
parent aa590a1b
...@@ -155,21 +155,26 @@ class StreamWriterInterfaceTest(_MediaSourceMixin, TempDirMixin, TorchaudioTestC ...@@ -155,21 +155,26 @@ class StreamWriterInterfaceTest(_MediaSourceMixin, TempDirMixin, TorchaudioTestC
@parameterized.expand( @parameterized.expand(
[ [
("mp3", 8000, 1, "s32p", None), ("mp3", 8000, 1, None, "s32p", None),
("mp3", 16000, 2, "fltp", None), ("mp3", 16000, 2, None, "fltp", None),
("mp3", 44100, 1, "s16p", {"abr": "true"}), ("mp3", 44100, 1, None, "s16p", {"abr": "true"}),
("flac", 8000, 1, "s16", None), ("flac", 8000, 1, None, "s16", None),
("flac", 16000, 2, "s32", None), ("flac", 16000, 2, None, "s32", None),
("opus", 48000, 2, None, {"strict": "experimental"}), ("opus", 48000, 2, "opus", None, None),
("adts", 8000, 1, "fltp", None), # AAC format ("ogg", 48000, 2, "vorbis", None, None),
("adts", 8000, 1, None, "fltp", None), # AAC format
] ]
) )
def test_valid_audio_muxer_and_codecs(self, ext, sample_rate, num_channels, encoder_format, encoder_option): def test_valid_audio_muxer_and_codecs(
self, ext, sample_rate, num_channels, encoder, encoder_format, encoder_option
):
"""Tensor of various dtypes can be saved as given format.""" """Tensor of various dtypes can be saved as given format."""
path = self.get_dst(f"test.{ext}") path = self.get_dst(f"test.{ext}")
s = StreamWriter(path, format=ext) s = StreamWriter(path, format=ext)
s.set_metadata(metadata={"artist": "torchaudio", "title": self.id()}) s.set_metadata(metadata={"artist": "torchaudio", "title": self.id()})
s.add_audio_stream(sample_rate, num_channels, encoder_option=encoder_option, encoder_format=encoder_format) s.add_audio_stream(
sample_rate, num_channels, encoder=encoder, encoder_option=encoder_option, encoder_format=encoder_format
)
chunk = get_audio_chunk("flt", sample_rate, num_channels) chunk = get_audio_chunk("flt", sample_rate, num_channels)
with s.open(): with s.open():
......
...@@ -178,6 +178,37 @@ void open_codec( ...@@ -178,6 +178,37 @@ void open_codec(
AVCodecContextPtr& codec_ctx, AVCodecContextPtr& codec_ctx,
const c10::optional<OptionDict>& option) { const c10::optional<OptionDict>& option) {
AVDictionary* opt = get_option_dict(option); AVDictionary* opt = get_option_dict(option);
// Enable experimental feature if required
// Note:
// "vorbis" refers to FFmpeg's native encoder,
// https://ffmpeg.org/doxygen/4.1/vorbisenc_8c.html#a8c2e524b0f125f045fef39c747561450
// while "libvorbis" refers to the one depends on libvorbis,
// which is not experimental
// https://ffmpeg.org/doxygen/4.1/libvorbisenc_8c.html#a5dd5fc671e2df9c5b1f97b2ee53d4025
// similarly, "opus" refers to FFmpeg's native encoder
// https://ffmpeg.org/doxygen/4.1/opusenc_8c.html#a05b203d4a9a231cc1fd5a7ddeb68cebc
// while "libopus" refers to the one depends on libopusenc
// https://ffmpeg.org/doxygen/4.1/libopusenc_8c.html#aa1d649e48cd2ec00cfe181cf9d0f3251
if (std::strcmp(codec_ctx->codec->name, "vorbis") == 0) {
if (!av_dict_get(opt, "strict", nullptr, 0)) {
TORCH_WARN_ONCE(
"\"vorbis\" encoder is selected. Enabling '-strict experimental'. ",
"If this is not desired, please provide \"strict\" encoder option ",
"with desired value.");
av_dict_set(&opt, "strict", "experimental", 0);
}
}
if (std::strcmp(codec_ctx->codec->name, "opus") == 0) {
if (!av_dict_get(opt, "strict", nullptr, 0)) {
TORCH_WARN_ONCE(
"\"opus\" encoder is selected. Enabling '-strict experimental'. ",
"If this is not desired, please provide \"strict\" encoder option ",
"with desired value.");
av_dict_set(&opt, "strict", "experimental", 0);
}
}
int ret = avcodec_open2(codec_ctx, codec_ctx->codec, &opt); int ret = avcodec_open2(codec_ctx, codec_ctx->codec, &opt);
clean_up_dict(opt); clean_up_dict(opt);
TORCH_CHECK(ret >= 0, "Failed to open codec: (", av_err2string(ret), ")"); TORCH_CHECK(ret >= 0, "Failed to open codec: (", av_err2string(ret), ")");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment