Skip to content
This repository
Browse code

Merge pull request #2879 from aballier/dvdaudioffmpeg_improvements2

Some improvements in the resampling code in DVDAudioCodecFFmpeg
  • Loading branch information...
commit 87ec33d6d6c80a5b6408905b93bada017f79b1a9 2 parents 377f2a7 + 3c6c0fc
Alexis Ballier authored July 02, 2013
38  xbmc/cores/dvdplayer/DVDCodecs/Audio/DVDAudioCodecFFmpeg.cpp
@@ -34,8 +34,8 @@ CDVDAudioCodecFFmpeg::CDVDAudioCodecFFmpeg() : CDVDAudioCodec()
34 34
 {
35 35
   m_iBufferSize1 = 0;
36 36
   m_iBufferSize2 = 0;
37  
-  m_pBuffer2     = (uint8_t*)_aligned_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE, 16);
38  
-  memset(m_pBuffer2, 0, AVCODEC_MAX_AUDIO_FRAME_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
  37
+  m_iBufferTotalSize2 = 0;
  38
+  m_pBuffer2     = NULL;
39 39
 
40 40
   m_iBuffered = 0;
41 41
   m_pCodecContext = NULL;
@@ -54,7 +54,6 @@ CDVDAudioCodecFFmpeg::CDVDAudioCodecFFmpeg() : CDVDAudioCodec()
54 54
 
55 55
 CDVDAudioCodecFFmpeg::~CDVDAudioCodecFFmpeg()
56 56
 {
57  
-  _aligned_free(m_pBuffer2);
58 57
   Dispose();
59 58
 }
60 59
 
@@ -130,6 +129,9 @@ void CDVDAudioCodecFFmpeg::Dispose()
130 129
   if (m_pConvert)
131 130
     m_dllSwResample.swr_free(&m_pConvert);
132 131
 
  132
+  if (m_pBuffer2)
  133
+    m_dllAvUtil.av_freep(&m_pBuffer2);
  134
+
133 135
   if (m_pCodecContext)
134 136
   {
135 137
     if (m_bOpenedCodec) m_dllAvCodec.avcodec_close(m_pCodecContext);
@@ -144,6 +146,7 @@ void CDVDAudioCodecFFmpeg::Dispose()
144 146
 
145 147
   m_iBufferSize1 = 0;
146 148
   m_iBufferSize2 = 0;
  149
+  m_iBufferTotalSize2 = 0;
147 150
   m_iBuffered = 0;
148 151
 }
149 152
 
@@ -152,7 +155,6 @@ int CDVDAudioCodecFFmpeg::Decode(uint8_t* pData, int iSize)
152 155
   int iBytesUsed, got_frame;
153 156
   if (!m_pCodecContext) return -1;
154 157
 
155  
-  m_iBufferSize1 = AVCODEC_MAX_AUDIO_FRAME_SIZE ;
156 158
   m_iBufferSize2 = 0;
157 159
 
158 160
   AVPacket avpkt;
@@ -212,8 +214,25 @@ void CDVDAudioCodecFFmpeg::ConvertToFloat()
212 214
       }
213 215
     }
214 216
 
215  
-    int len = m_iBufferSize1 / m_dllAvUtil.av_get_bytes_per_sample(m_pCodecContext->sample_fmt);
216  
-    if(m_dllSwResample.swr_convert(m_pConvert, &m_pBuffer2, len, (const uint8_t**)m_pFrame1->data, m_pFrame1->nb_samples) < 0)
  217
+    int needed_buf_size = m_dllAvUtil.av_samples_get_buffer_size(NULL, m_pCodecContext->channels, m_pFrame1->nb_samples, AV_SAMPLE_FMT_FLT, 0);
  218
+    if(m_iBufferTotalSize2 < needed_buf_size)
  219
+    {
  220
+        m_pBuffer2 = (uint8_t*)m_dllAvUtil.av_realloc(m_pBuffer2, needed_buf_size);
  221
+        if(!m_pBuffer2)
  222
+        {
  223
+            CLog::Log(LOGERROR, "CDVDAudioCodecFFmpeg::Decode - Unable to allocate a %i bytes buffer for resampling", needed_buf_size);
  224
+            m_iBufferSize1 = 0;
  225
+            m_iBufferSize2 = 0;
  226
+            m_iBufferTotalSize2 = 0;
  227
+            return;
  228
+        }
  229
+        m_iBufferTotalSize2 = needed_buf_size;
  230
+    }
  231
+
  232
+    int outsamples;
  233
+    outsamples = m_dllSwResample.swr_convert(m_pConvert, &m_pBuffer2, m_iBufferTotalSize2, (const uint8_t**)m_pFrame1->extended_data, m_pFrame1->nb_samples);
  234
+
  235
+    if(outsamples < 0)
217 236
     {
218 237
       CLog::Log(LOGERROR, "CDVDAudioCodecFFmpeg::Decode - Unable to convert %d to AV_SAMPLE_FMT_FLT", (int)m_pCodecContext->sample_fmt);
219 238
       m_iBufferSize1 = 0;
@@ -221,8 +240,13 @@ void CDVDAudioCodecFFmpeg::ConvertToFloat()
221 240
       return;
222 241
     }
223 242
 
  243
+    if(outsamples < m_pFrame1->nb_samples)
  244
+    {
  245
+      CLog::Log(LOGWARNING, "CDVDAudioCodecFFmpeg::Decode - Resampler produced less samples than what it was given");
  246
+    }
  247
+
224 248
     m_iBufferSize1 = 0;
225  
-    m_iBufferSize2 = len * m_dllAvUtil.av_get_bytes_per_sample(AV_SAMPLE_FMT_FLT);
  249
+    m_iBufferSize2 = m_dllAvUtil.av_samples_get_buffer_size(NULL, m_pCodecContext->channels, m_pFrame1->nb_samples, AV_SAMPLE_FMT_FLT, 0);
226 250
   }
227 251
 }
228 252
 
1  xbmc/cores/dvdplayer/DVDCodecs/Audio/DVDAudioCodecFFmpeg.h
@@ -57,6 +57,7 @@ class CDVDAudioCodecFFmpeg : public CDVDAudioCodec
57 57
   int      m_iBufferSize1;
58 58
   uint8_t*    m_pBuffer2;
59 59
   int      m_iBufferSize2;
  60
+  int      m_iBufferTotalSize2;
60 61
 
61 62
   bool m_bOpenedCodec;
62 63
   int m_iBuffered;

0 notes on commit 87ec33d

Please sign in to comment.
Something went wrong with that request. Please try again.