|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// This source code is licensed under the BSD-style license found in the |
| 5 | +// LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +#include "AudioConverter.h" |
| 8 | + |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +#include "AudioCommon.h" |
| 12 | + |
| 13 | +namespace facebook::torchcodec { |
| 14 | + |
| 15 | +AudioConverter::AudioConverter( |
| 16 | + std::optional<int> sample_rate, |
| 17 | + std::optional<int> num_channels) |
| 18 | + : requested_sample_rate_(sample_rate), |
| 19 | + requested_num_channels_(num_channels) { |
| 20 | + STD_TORCH_CHECK( |
| 21 | + !sample_rate.has_value() || *sample_rate > 0, |
| 22 | + "sample_rate must be > 0. Got: ", |
| 23 | + sample_rate.value_or(0)); |
| 24 | + STD_TORCH_CHECK( |
| 25 | + !num_channels.has_value() || *num_channels > 0, |
| 26 | + "num_channels must be > 0. Got: ", |
| 27 | + num_channels.value_or(0)); |
| 28 | +} |
| 29 | + |
| 30 | +void AudioConverter::reset() { |
| 31 | + swr_context_.reset(); |
| 32 | + src_sample_format_ = AV_SAMPLE_FMT_NONE; |
| 33 | + src_sample_rate_ = 0; |
| 34 | + src_num_channels_ = 0; |
| 35 | + out_sample_rate_ = 0; |
| 36 | + out_num_channels_ = 0; |
| 37 | +} |
| 38 | + |
| 39 | +torch::stable::Tensor AudioConverter::convert( |
| 40 | + const torch::stable::Tensor& samples, |
| 41 | + int sample_rate) { |
| 42 | + STD_TORCH_CHECK( |
| 43 | + samples.dim() == 2, |
| 44 | + "Expected a 2D [num_channels, num_samples] tensor, got a ", |
| 45 | + samples.dim(), |
| 46 | + "D one."); |
| 47 | + STD_TORCH_CHECK(samples.is_contiguous(), "The samples must be contiguous."); |
| 48 | + STD_TORCH_CHECK(sample_rate > 0, "sample_rate must be > 0."); |
| 49 | + |
| 50 | + AVSampleFormat src_sample_format = |
| 51 | + planar_sample_format(samples.scalar_type()); |
| 52 | + int num_channels = static_cast<int>(samples.sizes()[0]); |
| 53 | + int num_samples = static_cast<int>(samples.sizes()[1]); |
| 54 | + STD_TORCH_CHECK( |
| 55 | + num_channels > 0, "The samples must have at least 1 channel."); |
| 56 | + |
| 57 | + if (swr_context_ == nullptr) { |
| 58 | + src_sample_format_ = src_sample_format; |
| 59 | + src_sample_rate_ = sample_rate; |
| 60 | + src_num_channels_ = num_channels; |
| 61 | + out_sample_rate_ = requested_sample_rate_.value_or(sample_rate); |
| 62 | + out_num_channels_ = requested_num_channels_.value_or(num_channels); |
| 63 | + swr_context_.reset(create_swr_context( |
| 64 | + src_sample_format_, |
| 65 | + kAudioOutSampleFormat, |
| 66 | + src_sample_rate_, |
| 67 | + out_sample_rate_, |
| 68 | + src_num_channels_, |
| 69 | + out_num_channels_)); |
| 70 | + } else { |
| 71 | + // swresample is configured once, from the first samples we see, and its |
| 72 | + // buffered state is tied to that configuration. Rather than silently |
| 73 | + // reconfiguring - which would discard whatever it still holds - we make the |
| 74 | + // caller decide, by reset()ing. |
| 75 | + STD_TORCH_CHECK( |
| 76 | + src_sample_format == src_sample_format_ && |
| 77 | + sample_rate == src_sample_rate_ && |
| 78 | + num_channels == src_num_channels_, |
| 79 | + "This AudioConverter was set up for ", |
| 80 | + src_num_channels_, |
| 81 | + " channels of ", |
| 82 | + av_get_sample_fmt_name(src_sample_format_), |
| 83 | + " at ", |
| 84 | + src_sample_rate_, |
| 85 | + " Hz, but got ", |
| 86 | + num_channels, |
| 87 | + " channels of ", |
| 88 | + av_get_sample_fmt_name(src_sample_format), |
| 89 | + " at ", |
| 90 | + sample_rate, |
| 91 | + " Hz. Call reset() to convert a different stream."); |
| 92 | + } |
| 93 | + |
| 94 | + const auto* base = static_cast<const uint8_t*>(samples.const_data_ptr()); |
| 95 | + int64_t bytes_per_channel = |
| 96 | + num_samples * av_get_bytes_per_sample(src_sample_format); |
| 97 | + std::vector<const uint8_t*> src_planes(num_channels); |
| 98 | + for (int channel = 0; channel < num_channels; ++channel) { |
| 99 | + src_planes[channel] = base + channel * bytes_per_channel; |
| 100 | + } |
| 101 | + |
| 102 | + return swr_convert_to_tensor( |
| 103 | + swr_context_, |
| 104 | + src_planes.data(), |
| 105 | + num_samples, |
| 106 | + out_num_channels_, |
| 107 | + get_swr_output_num_samples_bound( |
| 108 | + swr_context_, num_samples, src_sample_rate_, out_sample_rate_)); |
| 109 | +} |
| 110 | + |
| 111 | +torch::stable::Tensor AudioConverter::drain() { |
| 112 | + STD_TORCH_CHECK( |
| 113 | + swr_context_ != nullptr, |
| 114 | + "This AudioConverter hasn't converted any samples, so there is nothing " |
| 115 | + "to drain and no way to know what shape the result should have."); |
| 116 | + // A null input is what tells swr_convert() to flush. Unlike the convert() |
| 117 | + // path we ask swresample how much it is holding rather than deriving a bound |
| 118 | + // from an input size, since here there is no input. |
| 119 | + return swr_convert_to_tensor( |
| 120 | + swr_context_, |
| 121 | + /*src_planes=*/nullptr, |
| 122 | + /*num_src_samples=*/0, |
| 123 | + out_num_channels_, |
| 124 | + swr_get_out_samples(swr_context_.get(), 0)); |
| 125 | +} |
| 126 | + |
| 127 | +} // namespace facebook::torchcodec |
0 commit comments