diff --git a/src/media/media_decoder.cpp b/src/media/media_decoder.cpp index d73f77f242cd9dc90da154f23190c631f034f7ba..670f204371bd544b485d2300c20864a964097468 100644 --- a/src/media/media_decoder.cpp +++ b/src/media/media_decoder.cpp @@ -470,33 +470,33 @@ void MediaDecoder::writeToRingBuffer(const AudioFrame& decodedFrame, RingBuffer& rb, const AudioFormat outFormat) { - const auto libav_frame = decodedFrame.pointer(); - decBuff_.setFormat(AudioFormat{ - (unsigned) libav_frame->sample_rate, - (unsigned) decoderCtx_->channels - }); - decBuff_.resize(libav_frame->nb_samples); - - if ( decoderCtx_->sample_fmt == AV_SAMPLE_FMT_FLTP ) { - decBuff_.convertFloatPlanarToSigned16(libav_frame->extended_data, - libav_frame->nb_samples, - decoderCtx_->channels); - } else if ( decoderCtx_->sample_fmt == AV_SAMPLE_FMT_S16 ) { - decBuff_.deinterleave(reinterpret_cast<const AudioSample*>(libav_frame->data[0]), - libav_frame->nb_samples, decoderCtx_->channels); - } - if ((unsigned)libav_frame->sample_rate != outFormat.sample_rate) { + const auto frame = decodedFrame.pointer(); + const auto inFormat = AudioFormat((unsigned)frame->sample_rate, + (unsigned)frame->channels, + (AVSampleFormat)frame->format); + + AudioFrame output; + if (inFormat != outFormat) { if (!resampler_) { RING_DBG("Creating audio resampler"); resampler_.reset(new Resampler); } - resamplingBuff_.setFormat({(unsigned) outFormat.sample_rate, (unsigned) decoderCtx_->channels}); - resamplingBuff_.resize(libav_frame->nb_samples); - resampler_->resample(decBuff_, resamplingBuff_); - rb.put(resamplingBuff_); + auto out = output.pointer(); + out->format = (int)outFormat.sampleFormat; + out->channel_layout = av_get_default_channel_layout((int)outFormat.nb_channels); + out->channels = (int)outFormat.nb_channels; + out->sample_rate = (int)outFormat.sample_rate; + if (resampler_->resample(frame, out) < 0) { + RING_ERR() << "Failed to resample audio"; + return; + } } else { - rb.put(decBuff_); + output.copyFrom(decodedFrame); } + // let buf resize itself + auto buf = AudioBuffer(0, outFormat); + buf.append(output); + rb.put(buf); } int