avresample wrapper fix

loadfix
scrawl 10 years ago
parent a9be8628b9
commit 2a3627b5a8

@ -41,6 +41,22 @@ namespace
namespace Video namespace Video
{ {
// Moved to implementation file, so that HAVE_SWRESAMPLE is used at library compile time only
struct AudioResampler
{
AudioResampler()
: mSwr(NULL)
{
}
~AudioResampler()
{
swr_free(&mSwr);
}
SwrContext* mSwr;
};
MovieAudioDecoder::MovieAudioDecoder(VideoState* videoState) MovieAudioDecoder::MovieAudioDecoder(VideoState* videoState)
: mVideoState(videoState) : mVideoState(videoState)
, mAVStream(*videoState->audio_st) , mAVStream(*videoState->audio_st)
@ -53,7 +69,6 @@ MovieAudioDecoder::MovieAudioDecoder(VideoState* videoState)
/* Correct audio only if larger error than this */ /* Correct audio only if larger error than this */
, mAudioDiffThreshold(2.0 * 0.050/* 50 ms */) , mAudioDiffThreshold(2.0 * 0.050/* 50 ms */)
, mAudioDiffAvgCount(0) , mAudioDiffAvgCount(0)
, mSwr(0)
, mOutputSampleFormat(AV_SAMPLE_FMT_NONE) , mOutputSampleFormat(AV_SAMPLE_FMT_NONE)
, mOutputSampleRate(0) , mOutputSampleRate(0)
, mOutputChannelLayout(0) , mOutputChannelLayout(0)
@ -61,18 +76,18 @@ MovieAudioDecoder::MovieAudioDecoder(VideoState* videoState)
, mFrameData(NULL) , mFrameData(NULL)
, mDataBufLen(0) , mDataBufLen(0)
{ {
mAudioResampler.reset(new AudioResampler());
} }
MovieAudioDecoder::~MovieAudioDecoder() MovieAudioDecoder::~MovieAudioDecoder()
{ {
av_freep(&mFrame); av_freep(&mFrame);
swr_free(&mSwr);
av_freep(&mDataBuf); av_freep(&mDataBuf);
} }
void MovieAudioDecoder::setupFormat() void MovieAudioDecoder::setupFormat()
{ {
if (mSwr) if (mAudioResampler->mSwr)
return; // already set up return; // already set up
AVSampleFormat inputSampleFormat = mAVStream->codec->sample_fmt; AVSampleFormat inputSampleFormat = mAVStream->codec->sample_fmt;
@ -104,7 +119,7 @@ void MovieAudioDecoder::setupFormat()
|| inputChannelLayout != mOutputChannelLayout || inputChannelLayout != mOutputChannelLayout
|| inputSampleRate != mOutputSampleRate) || inputSampleRate != mOutputSampleRate)
{ {
mSwr = swr_alloc_set_opts(mSwr, mAudioResampler->mSwr = swr_alloc_set_opts(mAudioResampler->mSwr,
mOutputChannelLayout, mOutputChannelLayout,
mOutputSampleFormat, mOutputSampleFormat,
mOutputSampleRate, mOutputSampleRate,
@ -113,9 +128,9 @@ void MovieAudioDecoder::setupFormat()
inputSampleRate, inputSampleRate,
0, // logging level offset 0, // logging level offset
NULL); // log context NULL); // log context
if(!mSwr) if(!mAudioResampler->mSwr)
fail(std::string("Couldn't allocate SwrContext")); fail(std::string("Couldn't allocate SwrContext"));
if(swr_init(mSwr) < 0) if(swr_init(mAudioResampler->mSwr) < 0)
fail(std::string("Couldn't initialize SwrContext")); fail(std::string("Couldn't initialize SwrContext"));
} }
} }
@ -171,7 +186,7 @@ int MovieAudioDecoder::audio_decode_frame(AVFrame *frame)
if(!got_frame || frame->nb_samples <= 0) if(!got_frame || frame->nb_samples <= 0)
continue; continue;
if(mSwr) if(mAudioResampler->mSwr)
{ {
if(!mDataBuf || mDataBufLen < frame->nb_samples) if(!mDataBuf || mDataBufLen < frame->nb_samples)
{ {
@ -183,7 +198,7 @@ int MovieAudioDecoder::audio_decode_frame(AVFrame *frame)
mDataBufLen = frame->nb_samples; mDataBufLen = frame->nb_samples;
} }
if(swr_convert(mSwr, (uint8_t**)&mDataBuf, frame->nb_samples, if(swr_convert(mAudioResampler->mSwr, (uint8_t**)&mDataBuf, frame->nb_samples,
(const uint8_t**)frame->extended_data, frame->nb_samples) < 0) (const uint8_t**)frame->extended_data, frame->nb_samples) < 0)
{ {
break; break;

@ -7,6 +7,7 @@
#include <stdint.h> #include <stdint.h>
#include <new> #include <new>
#include <memory>
extern "C" extern "C"
{ {
@ -18,8 +19,6 @@ extern "C"
LIBAVUTIL_VERSION_MINOR, LIBAVUTIL_VERSION_MICRO) LIBAVUTIL_VERSION_MINOR, LIBAVUTIL_VERSION_MICRO)
#include <libavutil/channel_layout.h> #include <libavutil/channel_layout.h>
#endif #endif
struct SwrContext;
} }
#ifdef _WIN32 #ifdef _WIN32
@ -31,6 +30,8 @@ typedef SSIZE_T ssize_t;
namespace Video namespace Video
{ {
struct AudioResampler;
struct VideoState; struct VideoState;
class MovieAudioDecoder class MovieAudioDecoder
@ -57,7 +58,7 @@ private:
}; };
SwrContext *mSwr; std::auto_ptr<AudioResampler> mAudioResampler;
uint8_t *mDataBuf; uint8_t *mDataBuf;
uint8_t **mFrameData; uint8_t **mFrameData;

Loading…
Cancel
Save