Skip to content

Commit

Permalink
[dvdplayer]: Use the SwResample API.
Browse files Browse the repository at this point in the history
This drops all usage of the private libavcodec::audiocovert API in xbmc.
  • Loading branch information
aballier committed Apr 14, 2012
1 parent 918b7a0 commit 9cf29df
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
25 changes: 11 additions & 14 deletions xbmc/cores/dvdplayer/DVDCodecs/Audio/DVDAudioCodecFFmpeg.cpp
Expand Up @@ -55,7 +55,7 @@ bool CDVDAudioCodecFFmpeg::Open(CDVDStreamInfo &hints, CDVDCodecOptions &options
AVCodec* pCodec;
m_bOpenedCodec = false;

if (!m_dllAvUtil.Load() || !m_dllAvCodec.Load())
if (!m_dllAvUtil.Load() || !m_dllAvCodec.Load() || !m_dllSwResample.Load())
return false;

m_dllAvCodec.avcodec_register_all();
Expand Down Expand Up @@ -112,8 +112,7 @@ void CDVDAudioCodecFFmpeg::Dispose()

if (m_pConvert)
{
m_dllAvCodec.av_audio_convert_free(m_pConvert);
m_pConvert = NULL;
m_dllSwResample.swr_free(&m_pConvert);
}

if (m_pCodecContext)
Expand Down Expand Up @@ -172,30 +171,28 @@ int CDVDAudioCodecFFmpeg::Decode(BYTE* pData, int iSize)
{
if(m_pConvert && m_pCodecContext->sample_fmt != m_iSampleFormat)
{
m_dllAvCodec.av_audio_convert_free(m_pConvert);
m_pConvert = NULL;
m_dllSwResample.swr_free(&m_pConvert);
}

if(!m_pConvert)
{
m_iSampleFormat = m_pCodecContext->sample_fmt;
m_pConvert = m_dllAvCodec.av_audio_convert_alloc(AV_SAMPLE_FMT_S16, 1, m_pCodecContext->sample_fmt, 1, NULL, 0);
m_pConvert = m_dllSwResample.swr_alloc_set_opts(NULL,
m_dllAvUtil.av_get_default_channel_layout(1), AV_SAMPLE_FMT_S16, m_pCodecContext->sample_rate,
m_dllAvUtil.av_get_default_channel_layout(1), m_pCodecContext->sample_fmt, m_pCodecContext->sample_rate,
0, NULL);
}

if(!m_pConvert)
if(!m_pConvert || m_dllSwResample.swr_init(m_pConvert))
{
CLog::Log(LOGERROR, "CDVDAudioCodecFFmpeg::Decode - Unable to convert %d to AV_SAMPLE_FMT_S16", m_pCodecContext->sample_fmt);
m_iBufferSize1 = 0;
m_iBufferSize2 = 0;
return iBytesUsed;
}

const void *ibuf[6] = { m_pFrame1->data[0] };
void *obuf[6] = { m_pBuffer2 };
int istr[6] = { m_dllAvUtil.av_get_bytes_per_sample(m_pCodecContext->sample_fmt) };
int ostr[6] = { 2 };
int len = m_iBufferSize1 / istr[0];
if(m_dllAvCodec.av_audio_convert(m_pConvert, obuf, ostr, ibuf, istr, len) < 0)
int len = m_iBufferSize1 / m_dllAvUtil.av_get_bytes_per_sample(m_pCodecContext->sample_fmt);
if(m_dllSwResample.swr_convert(m_pConvert, &m_pBuffer2, len, (const uint8_t**)m_pFrame1->data, m_pFrame1->nb_samples) < 0)
{
CLog::Log(LOGERROR, "CDVDAudioCodecFFmpeg::Decode - Unable to convert %d to AV_SAMPLE_FMT_S16", (int)m_pCodecContext->sample_fmt);
m_iBufferSize1 = 0;
Expand All @@ -204,7 +201,7 @@ int CDVDAudioCodecFFmpeg::Decode(BYTE* pData, int iSize)
}

m_iBufferSize1 = 0;
m_iBufferSize2 = len * ostr[0];
m_iBufferSize2 = len * 2;
}

return iBytesUsed;
Expand Down
4 changes: 3 additions & 1 deletion xbmc/cores/dvdplayer/DVDCodecs/Audio/DVDAudioCodecFFmpeg.h
Expand Up @@ -25,6 +25,7 @@
#include "DllAvCodec.h"
#include "DllAvFormat.h"
#include "DllAvUtil.h"
#include "DllSwResample.h"

class CDVDAudioCodecFFmpeg : public CDVDAudioCodec
{
Expand All @@ -46,7 +47,7 @@ class CDVDAudioCodecFFmpeg : public CDVDAudioCodec

protected:
AVCodecContext* m_pCodecContext;
AVAudioConvert* m_pConvert;;
struct SwrContext* m_pConvert;;
enum AVSampleFormat m_iSampleFormat;
enum PCMChannels m_channelMap[PCM_MAX_CH + 1];

Expand All @@ -64,6 +65,7 @@ class CDVDAudioCodecFFmpeg : public CDVDAudioCodec

DllAvCodec m_dllAvCodec;
DllAvUtil m_dllAvUtil;
DllSwResample m_dllSwResample;

void BuildChannelMap();
};
Expand Down
Expand Up @@ -42,10 +42,10 @@ CDVDAudioEncoderFFmpeg::~CDVDAudioEncoderFFmpeg()
m_dllAvUtil.av_freep(&m_CodecCtx);
}

if(m_dllAvCodec.IsLoaded())
if(m_dllSwResample.IsLoaded())
{
if (m_AudioConvert)
m_dllAvCodec.av_audio_convert_free(m_AudioConvert);
m_dllSwResample.swr_free(&m_AudioConvert);
}
delete[] m_Buffer;
delete[] m_TmpBuffer;
Expand All @@ -55,7 +55,7 @@ CDVDAudioEncoderFFmpeg::~CDVDAudioEncoderFFmpeg()
bool CDVDAudioEncoderFFmpeg::Initialize(unsigned int channels, enum PCMChannels *channelMap, unsigned int bitsPerSample, unsigned int sampleRate)
{
Reset();
if (!channelMap || !m_dllAvUtil.Load() || !m_dllAvCodec.Load())
if (!channelMap || !m_dllAvUtil.Load() || !m_dllAvCodec.Load() || !m_dllSwResample.Load())
return false;

m_dllAvCodec.avcodec_register_all();
Expand Down Expand Up @@ -93,9 +93,11 @@ bool CDVDAudioEncoderFFmpeg::Initialize(unsigned int channels, enum PCMChannels
/* the input format was not supported, initiate conversion to the first
* supported format */
CLog::Log(LOGDEBUG, "CDVDAudioEncoderFFmpeg: Initializing audio conversion for encoding");
m_AudioConvert = m_dllAvCodec.av_audio_convert_alloc(codec->sample_fmts[0], 1,
m_CodecCtx->sample_fmt, 1, NULL, 0);
if (!m_AudioConvert)
m_AudioConvert = m_dllSwResample.swr_alloc_set_opts(NULL,
m_dllAvUtil.av_get_default_channel_layout(1), codec->sample_fmts[0], codec->supported_samplerates ? codec->supported_samplerates[0] : m_CodecCtx->sample_rate,
m_dllAvUtil.av_get_default_channel_layout(1), m_CodecCtx->sample_fmt, m_CodecCtx->sample_rate,
0, NULL);
if (!m_AudioConvert || m_dllSwResample.swr_init(m_AudioConvert))
{
m_dllAvUtil.av_freep(&m_CodecCtx);
return false;
Expand Down Expand Up @@ -183,12 +185,9 @@ int CDVDAudioEncoderFFmpeg::Encode(uint8_t *data, int size)
m_Remap.Remap(data, m_TmpBuffer, m_NeededFrames);

if (m_AudioConvert) {
void *convInBuf[] = { m_TmpBuffer };
int convInStr[] = { m_BitsPerSample / 8 };
void *convOutBuf[] = { m_TmpBuffer2 };
int convOutStr[] = { m_dllAvUtil.av_get_bytes_per_sample(m_CodecCtx->sample_fmt) };
if (m_dllAvCodec.av_audio_convert(m_AudioConvert, convOutBuf, convOutStr,
convInBuf, convInStr, m_NeededFrames * m_CodecCtx->channels) < 0) {
if (m_dllSwResample.swr_convert(m_AudioConvert,
&m_TmpBuffer2, m_NeededFrames * m_CodecCtx->channels,
(const uint8_t**)&m_TmpBuffer, m_NeededFrames * m_CodecCtx->channels) < 0) {
CLog::Log(LOGERROR, "CDVDAudioEncoderFFmpeg: Audio conversion failed");
m_BufferSize = 0;
return m_NeededBytes;
Expand Down
Expand Up @@ -24,6 +24,7 @@
#include "DllAvCodec.h"
#include "DllAvFormat.h"
#include "DllAvUtil.h"
#include "DllSwResample.h"

class CDVDAudioEncoderFFmpeg: public IDVDAudioEncoder
{
Expand All @@ -44,9 +45,10 @@ class CDVDAudioEncoderFFmpeg: public IDVDAudioEncoder
private:
DllAvCodec m_dllAvCodec;
DllAvUtil m_dllAvUtil;
DllSwResample m_dllSwResample;

AVCodecContext *m_CodecCtx;
AVAudioConvert *m_AudioConvert;
SwrContext *m_AudioConvert;
enum PCMChannels m_ChannelMap[PCM_MAX_CH];
CPCMRemap m_Remap;
uint8_t *m_Buffer;
Expand Down

0 comments on commit 9cf29df

Please sign in to comment.