b=966636 rename resampling copy variables consistently and test remaining input better r=padenot a=abillings
authorKarl Tomlinson <karlt+@karlt.net>
Wed, 05 Feb 2014 14:42:18 +1300
changeset 176220 de63d702095ea4d865c0a61fc08d4c88e6fa65da
parent 176217 cd1bfc373cfb1fb7c8181dddab35c6a5eb7200e1
child 176221 b532c1989306a3fa41a0c84cb1945a69429dc8a4
push id445
push userffxbld
push dateMon, 10 Mar 2014 22:05:19 +0000
treeherdermozilla-release@dc38b741b04e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspadenot, abillings
bugs966636
milestone28.0
b=966636 rename resampling copy variables consistently and test remaining input better r=padenot a=abillings (transplanted from ba2c4b5ff7d878041c2ce1cc061145aaeecec233)
content/media/webaudio/AudioBufferSourceNode.cpp
--- a/content/media/webaudio/AudioBufferSourceNode.cpp
+++ b/content/media/webaudio/AudioBufferSourceNode.cpp
@@ -180,51 +180,52 @@ public:
   // Resamples input data to an output buffer, according to |mBufferSampleRate| and
   // the playbackRate.
   // The number of frames consumed/produced depends on the amount of space
   // remaining in both the input and output buffer, and the playback rate (that
   // is, the ratio between the output samplerate and the input samplerate).
   void CopyFromInputBufferWithResampling(AudioNodeStream* aStream,
                                          AudioChunk* aOutput,
                                          uint32_t aChannels,
-                                         uintptr_t aSourceOffset,
-                                         uintptr_t aBufferOffset,
-                                         uint32_t aAvailableInInputBuffer,
-                                         uint32_t& aFramesWritten) {
+                                         uint32_t aOffsetWithinBlock,
+                                         uint32_t& aFramesWritten,
+                                         uint32_t aBufferOffset,
+                                         uint32_t aBufferMax) {
     // TODO: adjust for mStop (see bug 913854 comment 9).
-    uint32_t availableInOutputBuffer = WEBAUDIO_BLOCK_SIZE - aBufferOffset;
+    uint32_t availableInOutputBuffer = WEBAUDIO_BLOCK_SIZE - aOffsetWithinBlock;
     SpeexResamplerState* resampler = Resampler(aStream, aChannels);
     MOZ_ASSERT(aChannels > 0);
 
-    if (aAvailableInInputBuffer) {
+    if (aBufferOffset < aBufferMax) {
+      uint32_t availableInInputBuffer = aBufferMax - aBufferOffset;
       // Limit the number of input samples copied and possibly
       // format-converted for resampling by estimating how many will be used.
       // This may be a little small when filling the resampler with initial
       // data, but we'll get called again and it will work out.
       uint32_t num, den;
       speex_resampler_get_ratio(resampler, &num, &den);
-      uint32_t inputLimit = std::min(aAvailableInInputBuffer,
+      uint32_t inputLimit = std::min(availableInInputBuffer,
                                      availableInOutputBuffer * den / num + 10);
       for (uint32_t i = 0; true; ) {
         uint32_t inSamples = inputLimit;
-        const float* inputData = mBuffer->GetData(i) + aSourceOffset;
+        const float* inputData = mBuffer->GetData(i) + aBufferOffset;
 
         uint32_t outSamples = availableInOutputBuffer;
         float* outputData =
           static_cast<float*>(const_cast<void*>(aOutput->mChannelData[i])) +
-          aBufferOffset;
+          aOffsetWithinBlock;
 
         WebAudioUtils::SpeexResamplerProcess(resampler, i,
                                              inputData, &inSamples,
                                              outputData, &outSamples);
         if (++i == aChannels) {
           mPosition += inSamples;
-          MOZ_ASSERT(mPosition <= mDuration);
+          MOZ_ASSERT(mPosition <= mDuration || mLoop);
           aFramesWritten = outSamples;
-          if (inSamples == aAvailableInInputBuffer && !mLoop) {
+          if (inSamples == availableInInputBuffer && !mLoop) {
             // If the available output space were unbounded then the input
             // latency would always be the correct amount of extra input to
             // provide in order to advance the output position to align with
             // the final point in the buffer.  However, when the output space
             // becomes full, the resampler may read all available input
             // without writing out the corresponding output.  Add one more
             // input sample, so that we know that enough output has been
             // written when the last input sample has been read.  This may
@@ -237,17 +238,17 @@ public:
         }
       }
     } else {
       for (uint32_t i = 0; true; ) {
         uint32_t inSamples = mRemainingResamplerTail;
         uint32_t outSamples = availableInOutputBuffer;
         float* outputData =
           static_cast<float*>(const_cast<void*>(aOutput->mChannelData[i])) +
-          aBufferOffset;
+          aOffsetWithinBlock;
 
         // AudioDataValue* for aIn selects the function that does not try to
         // copy and format-convert input data.
         WebAudioUtils::SpeexResamplerProcess(resampler, i,
                          static_cast<AudioDataValue*>(nullptr), &inSamples,
                          outputData, &outSamples);
         if (++i == aChannels) {
           mRemainingResamplerTail -= inSamples;
@@ -307,36 +308,35 @@ public:
                       uint32_t aBufferMax)
   {
     MOZ_ASSERT(*aCurrentPosition < mStop);
     uint32_t numFrames =
       std::min<TrackTicks>(std::min(WEBAUDIO_BLOCK_SIZE - *aOffsetWithinBlock,
                                     aBufferMax - aBufferOffset),
                            mStop - *aCurrentPosition);
     if (numFrames == WEBAUDIO_BLOCK_SIZE && !ShouldResample(aStream->SampleRate())) {
+      MOZ_ASSERT(aBufferOffset < aBufferMax);
       BorrowFromInputBuffer(aOutput, aChannels, aBufferOffset);
       *aOffsetWithinBlock += numFrames;
       *aCurrentPosition += numFrames;
       mPosition += numFrames;
     } else {
       if (aOutput->IsNull()) {
         MOZ_ASSERT(*aOffsetWithinBlock == 0);
         AllocateAudioBlock(aChannels, aOutput);
       }
       if (!ShouldResample(aStream->SampleRate())) {
+        MOZ_ASSERT(aBufferOffset < aBufferMax);
         CopyFromInputBuffer(aOutput, aChannels, aBufferOffset, *aOffsetWithinBlock, numFrames);
         *aOffsetWithinBlock += numFrames;
         *aCurrentPosition += numFrames;
         mPosition += numFrames;
       } else {
-        uint32_t framesWritten, availableInInputBuffer;
-
-        availableInInputBuffer = aBufferMax - aBufferOffset;
-
-        CopyFromInputBufferWithResampling(aStream, aOutput, aChannels, aBufferOffset, *aOffsetWithinBlock, availableInInputBuffer, framesWritten);
+        uint32_t framesWritten;
+        CopyFromInputBufferWithResampling(aStream, aOutput, aChannels, *aOffsetWithinBlock, framesWritten, aBufferOffset, aBufferMax);
         *aOffsetWithinBlock += framesWritten;
         *aCurrentPosition += framesWritten;
       }
     }
   }
 
   uint32_t ComputeFinalOutSampleRate(TrackRate aStreamSampleRate)
   {