Skip to content

Commit

Permalink
ao: select a best sample sample format in setAudioFormat()
Browse files Browse the repository at this point in the history
less computation in resampler if packed format of requested format is
supported, e.g. fltp=>flt
  • Loading branch information
wang-bin committed Jul 27, 2016
1 parent ef2130b commit b61894f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/AudioFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ AudioFormat::SampleFormat AudioFormat::packedSampleFormat(SampleFormat fmt)
return fmt;
}

AudioFormat::SampleFormat AudioFormat::make(int bytesPerSample, bool isFloat, bool isUnsigned, bool isPlanar)
{
int f = bytesPerSample;
if (isFloat)
f |= kFloat;
if (isUnsigned)
f |= kUnsigned;
if (isPlanar)
f |= kPlanar;
return SampleFormat(f);
}

AudioFormat::SampleFormat AudioFormat::planarSampleFormat(SampleFormat fmt)
{
if (isPlanar(fmt))
Expand Down
2 changes: 1 addition & 1 deletion src/QtAV/AudioFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Q_AV_EXPORT AudioFormat
static bool isPlanar(SampleFormat format);
static SampleFormat planarSampleFormat(SampleFormat fmt);
static SampleFormat packedSampleFormat(SampleFormat fmt);

static SampleFormat make(int bytesPerSample, bool isFloat, bool isUnsigned, bool isPlanar);
AudioFormat();
AudioFormat(const AudioFormat &other);
~AudioFormat();
Expand Down
31 changes: 27 additions & 4 deletions src/output/audio/AudioOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ bool AudioOutput::open()
QMutexLocker lock(&d.mutex);
Q_UNUSED(lock);
d.available = false;
d.paused = false;
d.resetStatus();
if (!d.backend)
return false;
Expand All @@ -379,6 +380,7 @@ bool AudioOutput::close()
QMutexLocker lock(&d.mutex);
Q_UNUSED(lock);
d.available = false;
d.paused = false;
d.resetStatus();
if (!d.backend)
return false;
Expand Down Expand Up @@ -417,7 +419,7 @@ bool AudioOutput::isPaused() const
bool AudioOutput::receiveData(const QByteArray &data, qreal pts)
{
DPTR_D(AudioOutput);
if (d.paused)
if (isPaused())
return false;
d.data = data;
if (isMute() && d.sw_mute) {
Expand Down Expand Up @@ -458,11 +460,32 @@ AudioFormat AudioOutput::setAudioFormat(const AudioFormat& format)
AudioFormat af(format);
if (d.backend) {
if (!d.backend->isSupported(format)) {
if (!d.backend->isSupported(format.sampleFormat())) {
af.setSampleFormat(d.backend->preferredSampleFormat());
int ss = af.bytesPerSample();
while (!d.backend->isSupported(af.sampleFormat())) {
if (af.bytesPerSample() < 1) {
if (ss > 1) {
qWarning("No sample format found");
break;
}
af.setSampleFormat(AudioFormat::SampleFormat_Float);
ss = af.bytesPerSample();
continue;
}
if (af.isPlanar()) {
af.setSampleFormat(AudioFormat::packedSampleFormat(af.sampleFormat()));
continue;
}
if (af.isFloat()) {
if (af.bytesPerSample() == 8)
af.setSampleFormat(AudioFormat::SampleFormat_Float);
else
af.setSampleFormat(AudioFormat::SampleFormat_Signed32);
} else {
af.setSampleFormat(AudioFormat::make(af.bytesPerSample()/2, false, af.bytesPerSample() == 2 | af.isUnsigned() /* U8, no S8 */, false));
}
}
if (!d.backend->isSupported(format.channelLayout())) {
af.setChannelLayout(d.backend->preferredChannelLayout());
af.setChannelLayout(AudioFormat::ChannelLayout_Stereo); // assume stereo is supported
}
}
}
Expand Down

0 comments on commit b61894f

Please sign in to comment.