use ffmpeg for sample packing

FFmpeg has a much more optimized algorithm than we do so may as well use it.
This commit is contained in:
itsmattkc
2021-09-25 13:22:13 -07:00
parent 4c9863a696
commit 845b4a7559
9 changed files with 191 additions and 29 deletions
+7 -5
View File
@@ -16,15 +16,17 @@
set(OLIVE_SOURCES
${OLIVE_SOURCES}
audio/audiomanager.h
audio/audiomanager.cpp
audio/audiovisualwaveform.h
audio/audiomanager.h
audio/audiovisualwaveform.cpp
audio/outputdeviceproxy.h
audio/audiovisualwaveform.h
audio/outputdeviceproxy.cpp
audio/outputmanager.h
audio/outputdeviceproxy.h
audio/outputmanager.cpp
audio/tempoprocessor.h
audio/outputmanager.h
audio/packedprocessor.cpp
audio/packedprocessor.h
audio/tempoprocessor.cpp
audio/tempoprocessor.h
PARENT_SCOPE
)
+106
View File
@@ -0,0 +1,106 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2021 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "packedprocessor.h"
#include "common/ffmpegutils.h"
namespace olive {
PackedProcessor::PackedProcessor() :
swr_ctx_(nullptr)
{
}
PackedProcessor::~PackedProcessor()
{
Close();
}
bool PackedProcessor::Open(const AudioParams &params)
{
if (IsOpen()) {
return true;
}
swr_ctx_ = swr_alloc_set_opts(nullptr,
params.channel_layout(),
FFmpegUtils::GetFFmpegSampleFormat(params.format(), false),
params.sample_rate(),
params.channel_layout(),
FFmpegUtils::GetFFmpegSampleFormat(params.format(), true),
params.sample_rate(),
0,
nullptr);
if (!swr_ctx_) {
qCritical() << "Failed to allocate resample context";
return false;
}
if (swr_init(swr_ctx_) < 0) {
qCritical() << "Failed to init resample context";
swr_free(&swr_ctx_);
return false;
}
return true;
}
QByteArray PackedProcessor::Convert(SampleBufferPtr planar)
{
if (!IsOpen()) {
qCritical() << "Tried to convert while closed";
return QByteArray();
}
int nb_samples = planar->sample_count();
if (nb_samples == 0) {
return QByteArray();
}
int nb_channels = planar->audio_params().channel_count();
QByteArray output(planar->audio_params().samples_to_bytes(nb_samples), Qt::Uninitialized);
uint8_t *output_data = reinterpret_cast<uint8_t*>(output.data());
QVector<const uint8_t*> input_arrays(nb_channels);
for (int i=0; i<nb_channels; i++) {
input_arrays[i] = reinterpret_cast<const uint8_t*>(planar->data(i));
}
int ret = swr_convert(swr_ctx_, &output_data, nb_samples, input_arrays.data(), nb_samples);
if (ret < 0) {
char buf[200];
av_strerror(ret, buf, 200);
qDebug() << "Packed processor failed with error:" << buf << ret;
}
return output;
}
void PackedProcessor::Close()
{
if (swr_ctx_) {
swr_free(&swr_ctx_);
}
}
}
+60
View File
@@ -0,0 +1,60 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2021 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef PACKEDPROCESSOR_H
#define PACKEDPROCESSOR_H
extern "C" {
#include <libswresample/swresample.h>
}
#include "codec/samplebuffer.h"
#include "render/audioparams.h"
namespace olive {
class PackedProcessor
{
public:
PackedProcessor();
~PackedProcessor();
DISABLE_COPY_MOVE(PackedProcessor)
bool Open(const AudioParams &params);
QByteArray Convert(SampleBufferPtr planar);
void Close();
bool IsOpen() const
{
return swr_ctx_;
}
private:
SwrContext *swr_ctx_;
};
}
#endif // PACKEDPROCESSOR_H
+4
View File
@@ -39,7 +39,11 @@ TempoProcessor::TempoProcessor() :
processed_frame_(nullptr),
open_(false)
{
}
TempoProcessor::~TempoProcessor()
{
Close();
}
bool TempoProcessor::IsOpen() const
+4
View File
@@ -42,6 +42,10 @@ class TempoProcessor
public:
TempoProcessor();
~TempoProcessor();
DISABLE_COPY_MOVE(TempoProcessor)
bool IsOpen() const;
const double& GetSpeed() const;
-19
View File
@@ -237,23 +237,4 @@ void SampleBuffer::set(int channel, const float *data, int sample_offset, int sa
memcpy(&data_[channel].data()[sample_offset], data, sizeof(float) * sample_length);
}
QByteArray SampleBuffer::toPackedData() const
{
QByteArray packed_data;
if (is_allocated()) {
packed_data.resize(audio_params_.samples_to_bytes(sample_count_per_channel_));
float* output_data = reinterpret_cast<float*>(packed_data.data());
for (int j=0;j<sample_count_per_channel_;j++) {
for (int i=0;i<audio_params_.channel_count();i++) {
output_data[i*j + i] = data_[i][j];
}
}
}
return packed_data;
}
}
-2
View File
@@ -86,8 +86,6 @@ public:
set(channel, data, 0, sample_length);
}
QByteArray toPackedData() const;
private:
AudioParams audio_params_;
+8 -3
View File
@@ -434,9 +434,13 @@ void ViewerWidget::ReceivedAudioBufferForPlayback()
if (watcher->HasResult()) {
SampleBufferPtr samples = watcher->Get().value<SampleBufferPtr>();
if (samples && audio_playback_device_) {
qint64 t = QDateTime::currentMSecsSinceEpoch();
QByteArray pack = samples->toPackedData();
qDebug() << "Packing took:" << (QDateTime::currentMSecsSinceEpoch() - t);
if (!packed_processor_.IsOpen()) {
packed_processor_.Open(samples->audio_params());
}
// Convert to packed data for audio output
QByteArray pack = packed_processor_.Convert(samples);
audio_playback_device_->Push(pack);
if (prequeuing_audio_) {
@@ -627,6 +631,7 @@ void ViewerWidget::PauseInternal()
audio_playback_device_.reset(nullptr);
qDeleteAll(audio_playback_queue_);
audio_playback_queue_.clear();
packed_processor_.Close();
UpdateTextureFromNode();
}
+2
View File
@@ -28,6 +28,7 @@
#include <QTimer>
#include <QWidget>
#include "audio/packedprocessor.h"
#include "audiowaveformview.h"
#include "common/rational.h"
#include "node/output/viewer/viewer.h"
@@ -257,6 +258,7 @@ private:
std::unique_ptr<PreviewAudioDevice> audio_playback_device_;
std::list<RenderTicketWatcher*> audio_playback_queue_;
rational audio_playback_queue_time_;
PackedProcessor packed_processor_;
static QVector<ViewerWidget*> instances_;