Skip to content

Commit

Permalink
Merge pull request #2879 from aballier/dvdaudioffmpeg_improvements2
Browse files Browse the repository at this point in the history
Some improvements in the resampling code in DVDAudioCodecFFmpeg
  • Loading branch information
aballier committed Jul 2, 2013
2 parents 377f2a7 + 3c6c0fc commit 87ec33d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
38 changes: 31 additions & 7 deletions xbmc/cores/dvdplayer/DVDCodecs/Audio/DVDAudioCodecFFmpeg.cpp
Expand Up @@ -34,8 +34,8 @@ CDVDAudioCodecFFmpeg::CDVDAudioCodecFFmpeg() : CDVDAudioCodec()
{ {
m_iBufferSize1 = 0; m_iBufferSize1 = 0;
m_iBufferSize2 = 0; m_iBufferSize2 = 0;
m_pBuffer2 = (uint8_t*)_aligned_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE, 16); m_iBufferTotalSize2 = 0;
memset(m_pBuffer2, 0, AVCODEC_MAX_AUDIO_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE); m_pBuffer2 = NULL;


m_iBuffered = 0; m_iBuffered = 0;
m_pCodecContext = NULL; m_pCodecContext = NULL;
Expand All @@ -54,7 +54,6 @@ CDVDAudioCodecFFmpeg::CDVDAudioCodecFFmpeg() : CDVDAudioCodec()


CDVDAudioCodecFFmpeg::~CDVDAudioCodecFFmpeg() CDVDAudioCodecFFmpeg::~CDVDAudioCodecFFmpeg()
{ {
_aligned_free(m_pBuffer2);
Dispose(); Dispose();
} }


Expand Down Expand Up @@ -130,6 +129,9 @@ void CDVDAudioCodecFFmpeg::Dispose()
if (m_pConvert) if (m_pConvert)
m_dllSwResample.swr_free(&m_pConvert); m_dllSwResample.swr_free(&m_pConvert);


if (m_pBuffer2)
m_dllAvUtil.av_freep(&m_pBuffer2);

if (m_pCodecContext) if (m_pCodecContext)
{ {
if (m_bOpenedCodec) m_dllAvCodec.avcodec_close(m_pCodecContext); if (m_bOpenedCodec) m_dllAvCodec.avcodec_close(m_pCodecContext);
Expand All @@ -144,6 +146,7 @@ void CDVDAudioCodecFFmpeg::Dispose()


m_iBufferSize1 = 0; m_iBufferSize1 = 0;
m_iBufferSize2 = 0; m_iBufferSize2 = 0;
m_iBufferTotalSize2 = 0;
m_iBuffered = 0; m_iBuffered = 0;
} }


Expand All @@ -152,7 +155,6 @@ int CDVDAudioCodecFFmpeg::Decode(uint8_t* pData, int iSize)
int iBytesUsed, got_frame; int iBytesUsed, got_frame;
if (!m_pCodecContext) return -1; if (!m_pCodecContext) return -1;


m_iBufferSize1 = AVCODEC_MAX_AUDIO_FRAME_SIZE ;
m_iBufferSize2 = 0; m_iBufferSize2 = 0;


AVPacket avpkt; AVPacket avpkt;
Expand Down Expand Up @@ -212,17 +214,39 @@ void CDVDAudioCodecFFmpeg::ConvertToFloat()
} }
} }


int len = m_iBufferSize1 / m_dllAvUtil.av_get_bytes_per_sample(m_pCodecContext->sample_fmt); int needed_buf_size = m_dllAvUtil.av_samples_get_buffer_size(NULL, m_pCodecContext->channels, m_pFrame1->nb_samples, AV_SAMPLE_FMT_FLT, 0);
if(m_dllSwResample.swr_convert(m_pConvert, &m_pBuffer2, len, (const uint8_t**)m_pFrame1->data, m_pFrame1->nb_samples) < 0) if(m_iBufferTotalSize2 < needed_buf_size)
{
m_pBuffer2 = (uint8_t*)m_dllAvUtil.av_realloc(m_pBuffer2, needed_buf_size);
if(!m_pBuffer2)
{
CLog::Log(LOGERROR, "CDVDAudioCodecFFmpeg::Decode - Unable to allocate a %i bytes buffer for resampling", needed_buf_size);
m_iBufferSize1 = 0;
m_iBufferSize2 = 0;
m_iBufferTotalSize2 = 0;
return;
}
m_iBufferTotalSize2 = needed_buf_size;
}

int outsamples;
outsamples = m_dllSwResample.swr_convert(m_pConvert, &m_pBuffer2, m_iBufferTotalSize2, (const uint8_t**)m_pFrame1->extended_data, m_pFrame1->nb_samples);

if(outsamples < 0)
{ {
CLog::Log(LOGERROR, "CDVDAudioCodecFFmpeg::Decode - Unable to convert %d to AV_SAMPLE_FMT_FLT", (int)m_pCodecContext->sample_fmt); CLog::Log(LOGERROR, "CDVDAudioCodecFFmpeg::Decode - Unable to convert %d to AV_SAMPLE_FMT_FLT", (int)m_pCodecContext->sample_fmt);
m_iBufferSize1 = 0; m_iBufferSize1 = 0;
m_iBufferSize2 = 0; m_iBufferSize2 = 0;
return; return;
} }


if(outsamples < m_pFrame1->nb_samples)
{
CLog::Log(LOGWARNING, "CDVDAudioCodecFFmpeg::Decode - Resampler produced less samples than what it was given");
}

m_iBufferSize1 = 0; m_iBufferSize1 = 0;
m_iBufferSize2 = len * m_dllAvUtil.av_get_bytes_per_sample(AV_SAMPLE_FMT_FLT); m_iBufferSize2 = m_dllAvUtil.av_samples_get_buffer_size(NULL, m_pCodecContext->channels, m_pFrame1->nb_samples, AV_SAMPLE_FMT_FLT, 0);
} }
} }


Expand Down
1 change: 1 addition & 0 deletions xbmc/cores/dvdplayer/DVDCodecs/Audio/DVDAudioCodecFFmpeg.h
Expand Up @@ -57,6 +57,7 @@ class CDVDAudioCodecFFmpeg : public CDVDAudioCodec
int m_iBufferSize1; int m_iBufferSize1;
uint8_t* m_pBuffer2; uint8_t* m_pBuffer2;
int m_iBufferSize2; int m_iBufferSize2;
int m_iBufferTotalSize2;


bool m_bOpenedCodec; bool m_bOpenedCodec;
int m_iBuffered; int m_iBuffered;
Expand Down

0 comments on commit 87ec33d

Please sign in to comment.