Commit c79897cc authored by Philippe Gorley's avatar Philippe Gorley Committed by Adrien Béraud

resampler: optimize initialization

Also fixes compile-time warnings.

Change-Id: I03e7949bfc9abcda4ed33a7f4479bcf1c41496c4
parent a687eb2d
......@@ -44,17 +44,17 @@ Resampler::~Resampler()
}
void
Resampler::reinit(const AudioFormat& in, const AudioFormat& out)
Resampler::reinit(const AVFrame* in, const AVFrame* out)
{
av_opt_set_int(swrCtx_, "ich", in.nb_channels, 0);
av_opt_set_int(swrCtx_, "icl", av_get_default_channel_layout(in.nb_channels), 0);
av_opt_set_int(swrCtx_, "isr", in.sample_rate, 0);
av_opt_set_sample_fmt(swrCtx_, "isf", in.sampleFormat, 0);
av_opt_set_int(swrCtx_, "ich", in->channels, 0);
av_opt_set_int(swrCtx_, "icl", av_get_default_channel_layout(in->channels), 0);
av_opt_set_int(swrCtx_, "isr", in->sample_rate, 0);
av_opt_set_sample_fmt(swrCtx_, "isf", static_cast<AVSampleFormat>(in->format), 0);
av_opt_set_int(swrCtx_, "och", out.nb_channels, 0);
av_opt_set_int(swrCtx_, "ocl", av_get_default_channel_layout(out.nb_channels), 0);
av_opt_set_int(swrCtx_, "osr", out.sample_rate, 0);
av_opt_set_sample_fmt(swrCtx_, "osf", out.sampleFormat, 0);
av_opt_set_int(swrCtx_, "och", out->channels, 0);
av_opt_set_int(swrCtx_, "ocl", av_get_default_channel_layout(out->channels), 0);
av_opt_set_int(swrCtx_, "osr", out->sample_rate, 0);
av_opt_set_sample_fmt(swrCtx_, "osf", static_cast<AVSampleFormat>(out->format), 0);
swr_init(swrCtx_);
initialized_ = true;
......@@ -63,14 +63,12 @@ Resampler::reinit(const AudioFormat& in, const AudioFormat& out)
int
Resampler::resample(const AVFrame* input, AVFrame* output)
{
const auto inFmt = AudioFormat((unsigned)input->sample_rate, (unsigned)input->channels, (AVSampleFormat)input->format);
const auto outFmt = AudioFormat((unsigned)output->sample_rate, (unsigned)output->channels, (AVSampleFormat)output->format);
if (!initialized_)
reinit(inFmt, outFmt);
reinit(input, output);
int ret = swr_convert_frame(swrCtx_, output, input);
if (ret & AVERROR_INPUT_CHANGED || ret & AVERROR_OUTPUT_CHANGED) {
reinit(inFmt, outFmt);
reinit(input, output);
return resample(input, output);
} else if (ret < 0) {
RING_ERR() << "Failed to resample frame";
......@@ -103,8 +101,8 @@ Resampler::resample(const AudioBuffer& dataIn, AudioBuffer& dataOut)
std::unique_ptr<AudioFrame>
Resampler::resample(std::unique_ptr<AudioFrame>&& in, const AudioFormat& format)
{
if (in->pointer()->sample_rate == format.sample_rate &&
in->pointer()->channels == format.nb_channels &&
if (in->pointer()->sample_rate == (int)format.sample_rate &&
in->pointer()->channels == (int)format.nb_channels &&
(AVSampleFormat)in->pointer()->format == format.sampleFormat)
{
return std::move(in);
......@@ -117,8 +115,8 @@ Resampler::resample(std::unique_ptr<AudioFrame>&& in, const AudioFormat& format)
std::shared_ptr<AudioFrame>
Resampler::resample(std::shared_ptr<AudioFrame>&& in, const AudioFormat& format)
{
if (in->pointer()->sample_rate == format.sample_rate &&
in->pointer()->channels == format.nb_channels &&
if (in->pointer()->sample_rate == (int)format.sample_rate &&
in->pointer()->channels == (int)format.nb_channels &&
(AVSampleFormat)in->pointer()->format == format.sampleFormat)
{
return std::move(in);
......
......@@ -62,7 +62,7 @@ class Resampler {
* output buffers always have the same formats, will never be called, as the first
* initialization is done in swr_convert_frame.
*/
void reinit(const AudioFormat& in, const AudioFormat& out);
void reinit(const AVFrame* in, const AVFrame* out);
SwrContext* swrCtx_; // incomplete type, cannot be a unique_ptr
bool initialized_;
......
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