Skip to content

Commit

Permalink
Extract resampling of decoded frame into function
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Sep 8, 2019
1 parent d8c78d7 commit 5a85b9a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
81 changes: 44 additions & 37 deletions src/sources/soundsourceffmpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,45 @@ bool SoundSourceFFmpeg::consumeNextPacket(
return true;
}

const CSAMPLE* SoundSourceFFmpeg::resampleDecodedFrame() {
if (m_pSwrContext) {
// Decoded frame must be resampled before reading
m_pavResampledFrame->channel_layout = m_avResampledChannelLayout;
m_pavResampledFrame->sample_rate = sampleRate();
m_pavResampledFrame->format = kavSampleFormat;
if (m_pavDecodedFrame->channel_layout == kavChannelLayoutUndefined) {
// Sometimes the channel layout is undefined.
m_pavDecodedFrame->channel_layout = m_avStreamChannelLayout;
}
#if ENABLE_TRACING
avTrace("Resampling decoded frame", *m_pavDecodedFrame);
#endif
const auto swr_convert_frame_result =
swr_convert_frame(
m_pSwrContext,
m_pavResampledFrame,
m_pavDecodedFrame);
if (swr_convert_frame_result != 0) {
kLogger.warning()
<< "swr_convert_frame() failed:"
<< formatErrorMessage(swr_convert_frame_result).toLocal8Bit().constData();
// Discard decoded frame and abort after unrecoverable error
av_frame_unref(m_pavDecodedFrame);
return nullptr;
}
#if ENABLE_TRACING
avTrace("Received resampled frame", *m_pavResampledFrame);
#endif
DEBUG_ASSERT(m_pavDecodedFrame->pts = m_pavResampledFrame->pts);
DEBUG_ASSERT(m_pavDecodedFrame->nb_samples = m_pavResampledFrame->nb_samples);
return reinterpret_cast<const CSAMPLE*>(
m_pavResampledFrame->extended_data[0]);
} else {
return reinterpret_cast<const CSAMPLE*>(
m_pavDecodedFrame->extended_data[0]);
}
}

ReadableSampleFrames SoundSourceFFmpeg::readSampleFramesClamped(
WritableSampleFrames writableSampleFrames) {
const SINT readableStartIndex = writableSampleFrames.frameIndexRange().start();
Expand Down Expand Up @@ -1000,7 +1039,6 @@ ReadableSampleFrames SoundSourceFFmpeg::readSampleFramesClamped(
DEBUG_ASSERT(writableRange.empty() || m_sampleBuffer.empty());

SINT missingFrameCount = 0;
const CSAMPLE* pDecodedSampleData = nullptr;

// Decode next frame
IndexRange decodedFrameRange;
Expand Down Expand Up @@ -1148,42 +1186,11 @@ ReadableSampleFrames SoundSourceFFmpeg::readSampleFramesClamped(
// This is expected behavior and will be compensated during 'preskip'
// (see below).

if (m_pSwrContext) {
// Decoded frame must be resampled before reading
m_pavResampledFrame->channel_layout = m_avResampledChannelLayout;
m_pavResampledFrame->sample_rate = sampleRate();
m_pavResampledFrame->format = kavSampleFormat;
if (m_pavDecodedFrame->channel_layout == kavChannelLayoutUndefined) {
// Sometimes the channel layout is undefined.
m_pavDecodedFrame->channel_layout = m_avStreamChannelLayout;
}
#if ENABLE_TRACING
avTrace("Resampling decoded frame", *m_pavDecodedFrame);
#endif
const auto swr_convert_frame_result =
swr_convert_frame(
m_pSwrContext,
m_pavResampledFrame,
m_pavDecodedFrame);
if (swr_convert_frame_result != 0) {
kLogger.warning()
<< "swr_convert_frame() failed:"
<< formatErrorMessage(swr_convert_frame_result).toLocal8Bit().constData();
// Abort reading
av_frame_unref(m_pavDecodedFrame);
m_curFrameIndex = kFrameIndexInvalid;
break;
}
#if ENABLE_TRACING
avTrace("Received resampled frame", *m_pavResampledFrame);
#endif
DEBUG_ASSERT(m_pavDecodedFrame->pts = m_pavResampledFrame->pts);
DEBUG_ASSERT(m_pavDecodedFrame->nb_samples = m_pavResampledFrame->nb_samples);
pDecodedSampleData = reinterpret_cast<const CSAMPLE*>(
m_pavResampledFrame->extended_data[0]);
} else {
pDecodedSampleData = reinterpret_cast<const CSAMPLE*>(
m_pavDecodedFrame->extended_data[0]);
const CSAMPLE* pDecodedSampleData = resampleDecodedFrame();
if (!pDecodedSampleData) {
// Invalidate current position and abort reading after unrecoverable error
m_curFrameIndex = kFrameIndexInvalid;
break;
}

// readFrameIndex
Expand Down
1 change: 1 addition & 0 deletions src/sources/soundsourceffmpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class SoundSourceFFmpeg : public SoundSource {
bool initResampling(
ChannelCount* pResampledChannelCount,
SampleRate* pResampledSampleRate);
const CSAMPLE* resampleDecodedFrame();

// Consume as many buffered sample frames as possible and return
// the remaining range that could not be filled from the buffer.
Expand Down

0 comments on commit 5a85b9a

Please sign in to comment.