Skip to content
This repository
Browse code

Merge pull request #3240 from FernetMenta/aefixes

ActiveAE: fix setting resample ratio
  • Loading branch information...
commit 429ce9bc69cdee21d3f3be23c1f7147f25b42db6 2 parents 1b4a9d6 + 6f5642e
Rainer Hochecker authored September 13, 2013
4  lib/DllSwResample.h
@@ -57,6 +57,7 @@ class DllSwResampleInterface
57 57
   virtual int64_t swr_get_delay(struct SwrContext *s, int64_t base) = 0;
58 58
   virtual int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map) = 0;
59 59
   virtual int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride) = 0;
  60
+  virtual int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance) = 0;
60 61
 };
61 62
 
62 63
 #if (defined USE_EXTERNAL_FFMPEG) || (defined TARGET_DARWIN) 
@@ -83,6 +84,7 @@ class DllSwResample : public DllDynamic, DllSwResampleInterface
83 84
   virtual int64_t swr_get_delay(struct SwrContext *s, int64_t base) { return ::swr_get_delay(s, base); }
84 85
   virtual int swr_set_channel_mapping (struct SwrContext *s, const int *channel_map) { return ::swr_set_channel_mapping(s, channel_map); }
85 86
   virtual int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride) { return ::swr_set_matrix(s, matrix, stride); }
  87
+  virtual int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance) { return ::int swr_set_compensation(s, sample_delta, compensation_distance); }
86 88
 };
87 89
 
88 90
 #else
@@ -100,6 +102,7 @@ class DllSwResample : public DllDynamic, DllSwResampleInterface
100 102
   DEFINE_METHOD2(int64_t, swr_get_delay, (struct SwrContext *p1, int64_t p2))
101 103
   DEFINE_METHOD2(int, swr_set_channel_mapping, (struct SwrContext *p1, const int *p2))
102 104
   DEFINE_METHOD3(int, swr_set_matrix, (struct SwrContext *p1, const double *p2, int p3))
  105
+  DEFINE_METHOD3(int, swr_set_compensation, (struct SwrContext *p1, int p2, int p3))
103 106
 
104 107
   BEGIN_METHOD_RESOLVE()
105 108
     RESOLVE_METHOD(swr_alloc_set_opts)
@@ -109,6 +112,7 @@ class DllSwResample : public DllDynamic, DllSwResampleInterface
109 112
     RESOLVE_METHOD(swr_get_delay)
110 113
     RESOLVE_METHOD(swr_set_channel_mapping)
111 114
     RESOLVE_METHOD(swr_set_matrix)
  115
+    RESOLVE_METHOD(swr_set_compensation)
112 116
   END_METHOD_RESOLVE()
113 117
 
114 118
   /* dependencies of libavformat */
2  lib/xbmc-libav-hacks/libav_hacks.h
@@ -75,6 +75,8 @@ int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map);
75 75
 
76 76
 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride);
77 77
 
  78
+int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance);
  79
+
78 80
 // libavfilter
79 81
 
80 82
 #define LIBAVFILTER_AVFRAME_BASED
5  lib/xbmc-libav-hacks/swresample.c
@@ -73,3 +73,8 @@ int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
73 73
 {
74 74
     return avresample_set_matrix(s, matrix, stride);
75 75
 }
  76
+
  77
+int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance)
  78
+{
  79
+    return avresample_set_compensation(s, sample_delta, compensation_distance);
  80
+}
10  xbmc/cores/AudioEngine/Engines/ActiveAE/ActiveAE.cpp
@@ -458,12 +458,7 @@ void CActiveAE::StateMachine(int signal, Protocol *port, Message *msg)
458 458
           par = (MsgStreamParameter*)msg->data;
459 459
           if (par->stream->m_resampleBuffers)
460 460
           {
461  
-            if ((unsigned int)(par->stream->m_resampleBuffers->m_format.m_sampleRate * par->parameter.double_par) != par->stream->m_resampleBuffers->m_outSampleRate)
462  
-            {
463  
-              par->stream->m_resampleBuffers->m_resampleRatio = par->parameter.double_par;
464  
-              par->stream->m_resampleBuffers->m_resampleQuality = AE_QUALITY_LOW;
465  
-              par->stream->m_resampleBuffers->m_changeResampler = true;
466  
-            }
  461
+            par->stream->m_resampleBuffers->m_resampleRatio = par->parameter.double_par;
467 462
           }
468 463
           return;
469 464
         case CActiveAEControlProtocol::STREAMFADE:
@@ -2333,7 +2328,8 @@ bool CActiveAE::ResampleSound(CActiveAESound *sound)
2333 2328
   }
2334 2329
   int samples = resampler->Resample(dst_buffer, dst_samples,
2335 2330
                                     sound->GetSound(true)->data,
2336  
-                                    sound->GetSound(true)->nb_samples);
  2331
+                                    sound->GetSound(true)->nb_samples,
  2332
+                                    1.0);
2337 2333
 
2338 2334
   sound->GetSound(false)->nb_samples = samples;
2339 2335
 
12  xbmc/cores/AudioEngine/Engines/ActiveAE/ActiveAEBuffer.cpp
@@ -177,9 +177,6 @@ bool CActiveAEBufferPoolResample::Create(unsigned int totaltime, bool remap, boo
177 177
                                 m_resampleQuality);
178 178
   }
179 179
 
180  
-  // store output sampling rate, needed when ratio gets changed
181  
-  m_outSampleRate = m_format.m_sampleRate;
182  
-
183 180
   m_stereoUpmix = upmix;
184 181
 
185 182
   return true;
@@ -187,14 +184,12 @@ bool CActiveAEBufferPoolResample::Create(unsigned int totaltime, bool remap, boo
187 184
 
188 185
 void CActiveAEBufferPoolResample::ChangeResampler()
189 186
 {
190  
-  m_outSampleRate = m_format.m_sampleRate * m_resampleRatio;
191  
-
192 187
   delete m_resampler;
193 188
 
194 189
   m_resampler = new CActiveAEResample();
195 190
   m_resampler->Init(CActiveAEResample::GetAVChannelLayout(m_format.m_channelLayout),
196 191
                                 m_format.m_channelLayout.Count(),
197  
-                                m_outSampleRate,
  192
+                                m_format.m_sampleRate,
198 193
                                 CActiveAEResample::GetAVSampleFormat(m_format.m_dataFormat),
199 194
                                 CAEUtil::DataFormatToUsedBits(m_format.m_dataFormat),
200 195
                                 CActiveAEResample::GetAVChannelLayout(m_inputFormat.m_channelLayout),
@@ -275,7 +270,8 @@ bool CActiveAEBufferPoolResample::ResampleBuffers(unsigned int timestamp)
275 270
       out_samples = m_resampler->Resample(m_planes,
276 271
                                           m_procSample->pkt->max_nb_samples - m_procSample->pkt->nb_samples,
277 272
                                           in ? in->pkt->data : NULL,
278  
-                                          in ? in->pkt->nb_samples : 0);
  273
+                                          in ? in->pkt->nb_samples : 0,
  274
+                                          m_resampleRatio);
279 275
       m_procSample->pkt->nb_samples += out_samples;
280 276
       busy = true;
281 277
       m_empty = (out_samples == 0);
@@ -345,7 +341,7 @@ float CActiveAEBufferPoolResample::GetDelay()
345 341
   if (m_resampler)
346 342
   {
347 343
     int samples = m_resampler->GetBufferedSamples();
348  
-    delay += (float)samples / m_outSampleRate;
  344
+    delay += (float)samples / m_format.m_sampleRate;
349 345
   }
350 346
 
351 347
   return delay;
1  xbmc/cores/AudioEngine/Engines/ActiveAE/ActiveAEBuffer.h
@@ -107,7 +107,6 @@ class CActiveAEBufferPoolResample : public CActiveAEBufferPool
107 107
   bool m_changeResampler;
108 108
   double m_resampleRatio;
109 109
   AEQuality m_resampleQuality;
110  
-  unsigned int m_outSampleRate;
111 110
   bool m_stereoUpmix;
112 111
 };
113 112
 
13  xbmc/cores/AudioEngine/Engines/ActiveAE/ActiveAEResample.cpp
@@ -159,8 +159,19 @@ bool CActiveAEResample::Init(uint64_t dst_chan_layout, int dst_channels, int dst
159 159
   return true;
160 160
 }
161 161
 
162  
-int CActiveAEResample::Resample(uint8_t **dst_buffer, int dst_samples, uint8_t **src_buffer, int src_samples)
  162
+int CActiveAEResample::Resample(uint8_t **dst_buffer, int dst_samples, uint8_t **src_buffer, int src_samples, double ratio)
163 163
 {
  164
+  if (ratio != 1.0)
  165
+  {
  166
+    if (m_dllSwResample.swr_set_compensation(m_pContext,
  167
+                                            (dst_samples*ratio-dst_samples)*m_dst_rate/m_src_rate,
  168
+                                             dst_samples*m_dst_rate/m_src_rate) < 0)
  169
+    {
  170
+      CLog::Log(LOGERROR, "CActiveAEResample::Resample - set compensation failed");
  171
+      return 0;
  172
+    }
  173
+  }
  174
+
164 175
   int ret = m_dllSwResample.swr_convert(m_pContext, dst_buffer, dst_samples, (const uint8_t**)src_buffer, src_samples);
165 176
   if (ret < 0)
166 177
   {
2  xbmc/cores/AudioEngine/Engines/ActiveAE/ActiveAEResample.h
@@ -35,7 +35,7 @@ class CActiveAEResample
35 35
   CActiveAEResample();
36 36
   virtual ~CActiveAEResample();
37 37
   bool Init(uint64_t dst_chan_layout, int dst_channels, int dst_rate, AVSampleFormat dst_fmt, int dst_bits, uint64_t src_chan_layout, int src_channels, int src_rate, AVSampleFormat src_fmt, int src_bits, bool upmix, CAEChannelInfo *remapLayout, AEQuality quality);
38  
-  int Resample(uint8_t **dst_buffer, int dst_samples, uint8_t **src_buffer, int src_samples);
  38
+  int Resample(uint8_t **dst_buffer, int dst_samples, uint8_t **src_buffer, int src_samples, double ratio);
39 39
   int64_t GetDelay(int64_t base);
40 40
   int GetBufferedSamples();
41 41
   int CalcDstSampleCount(int src_samples, int dst_rate, int src_rate);

0 notes on commit 429ce9b

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