OSDN Git Service

Clean up background audio processing code
authorJames Dong <jdong@google.com>
Wed, 4 May 2011 06:31:23 +0000 (23:31 -0700)
committerJames Dong <jdong@google.com>
Wed, 4 May 2011 06:50:00 +0000 (23:50 -0700)
Change-Id: I6184e71cb0ccfdb1fc5f250bd7ecd4555efad482

libvideoeditor/lvpp/VideoEditorBGAudioProcessing.cpp
libvideoeditor/lvpp/VideoEditorBGAudioProcessing.h

index 9368d54..bcdc3de 100755 (executable)
 \r
 namespace android {\r
 \r
-VideoEditorBGAudioProcessing ::VideoEditorBGAudioProcessing() {\r
+VideoEditorBGAudioProcessing::VideoEditorBGAudioProcessing() {\r
 \r
     LOGV("VideoEditorBGAudioProcessing:: Construct  VideoEditorBGAudioProcessing ");\r
 \r
-    VideoEditorBGAudioProcessing::mAudVolArrIndex = 0;\r
-    VideoEditorBGAudioProcessing::mDoDucking = 0;\r
-    VideoEditorBGAudioProcessing::mDucking_enable = 0;\r
-    VideoEditorBGAudioProcessing::mDucking_lowVolume = 0;\r
-    VideoEditorBGAudioProcessing::mDucking_threshold = 0;\r
-    VideoEditorBGAudioProcessing::mDuckingFactor = 0;\r
+    mAudVolArrIndex = 0;\r
+    mDoDucking = 0;\r
+    mDucking_enable = 0;\r
+    mDucking_lowVolume = 0;\r
+    mDucking_threshold = 0;\r
+    mDuckingFactor = 0;\r
 \r
-    VideoEditorBGAudioProcessing::mBTVolLevel = 0;\r
-    VideoEditorBGAudioProcessing::mPTVolLevel = 0;\r
+    mBTVolLevel = 0;\r
+    mPTVolLevel = 0;\r
 \r
-    VideoEditorBGAudioProcessing::mIsSSRCneeded = 0;\r
-    VideoEditorBGAudioProcessing::mChannelConversion = 0;\r
+    mIsSSRCneeded = 0;\r
+    mChannelConversion = 0;\r
 \r
-    VideoEditorBGAudioProcessing::mBTFormat = MONO_16_BIT;\r
+    mBTFormat = MONO_16_BIT;\r
 \r
-    VideoEditorBGAudioProcessing::mInSampleRate = 8000;\r
-    VideoEditorBGAudioProcessing::mOutSampleRate = 16000;\r
-    VideoEditorBGAudioProcessing::mPTChannelCount = 2;\r
-    VideoEditorBGAudioProcessing::mBTChannelCount = 1;\r
+    mInSampleRate = 8000;\r
+    mOutSampleRate = 16000;\r
+    mPTChannelCount = 2;\r
+    mBTChannelCount = 1;\r
 }\r
 \r
 M4OSA_Int32 VideoEditorBGAudioProcessing::veProcessAudioMixNDuck(\r
@@ -55,8 +55,8 @@ M4OSA_Int32 VideoEditorBGAudioProcessing::veProcessAudioMixNDuck(
     M4AM_Buffer16* pMixedOutBuffer  = (M4AM_Buffer16*)pOutBuffer;\r
 \r
     LOGV("VideoEditorBGAudioProcessing::lvProcessAudioMixNDuck \\r
-     pPTBuffer 0x%x pBTBuffer 0x%x pOutBuffer 0x%x", pPTBuffer,\r
-      pBTBuffer, pOutBuffer);\r
+        pPTBuffer 0x%x pBTBuffer 0x%x pOutBuffer 0x%x", pPTBuffer,\r
+        pBTBuffer, pOutBuffer);\r
 \r
     M4OSA_ERR result = M4NO_ERROR;\r
     M4OSA_Int16 *pBTMdata1;\r
@@ -75,7 +75,7 @@ M4OSA_Int32 VideoEditorBGAudioProcessing::veProcessAudioMixNDuck(
 \r
     // Before mixing, we need to have only PT as out buffer\r
     memcpy((void *)pMixedOutBuffer->m_dataAddress,\r
-     (void *)pPrimaryTrack->m_dataAddress, pMixedOutBuffer->m_bufferSize);\r
+        (void *)pPrimaryTrack->m_dataAddress, pMixedOutBuffer->m_bufferSize);\r
 \r
     // Initially contains the input primary track\r
     pPTMdata2 = (M4OSA_Int16*)pMixedOutBuffer->m_dataAddress;\r
@@ -85,7 +85,7 @@ M4OSA_Int32 VideoEditorBGAudioProcessing::veProcessAudioMixNDuck(
     // Since we need to give sample count and not buffer size\r
     uiPCMsize = pMixedOutBuffer->m_bufferSize/2 ;\r
 \r
-    if((this->mDucking_enable) && (this->mPTVolLevel != 0.0)) {\r
+    if ((mDucking_enable) && (mPTVolLevel != 0.0)) {\r
         // LOGI("VideoEditorBGAudioProcessing:: In Ducking analysis ");\r
         loopIndex = 0;\r
         peakDbValue = 0;\r
@@ -93,35 +93,32 @@ M4OSA_Int32 VideoEditorBGAudioProcessing::veProcessAudioMixNDuck(
 \r
         pPCM16Sample = (M4OSA_Int16*)pPrimaryTrack->m_dataAddress;\r
 \r
-        while( loopIndex < pPrimaryTrack->m_bufferSize/sizeof(M4OSA_Int16))\r
-        {\r
-            if (pPCM16Sample[loopIndex] >= 0){\r
+        while (loopIndex < pPrimaryTrack->m_bufferSize/sizeof(M4OSA_Int16)) {\r
+            if (pPCM16Sample[loopIndex] >= 0) {\r
                 peakDbValue = previousDbValue > pPCM16Sample[loopIndex] ?\r
-                 previousDbValue : pPCM16Sample[loopIndex];\r
+                        previousDbValue : pPCM16Sample[loopIndex];\r
                 previousDbValue = peakDbValue;\r
-            }else{\r
+            } else {\r
                 peakDbValue = previousDbValue > -pPCM16Sample[loopIndex] ?\r
-                 previousDbValue: -pPCM16Sample[loopIndex];\r
+                        previousDbValue: -pPCM16Sample[loopIndex];\r
                 previousDbValue = peakDbValue;\r
             }\r
             loopIndex++;\r
         }\r
 \r
-        this->mAudioVolumeArray[this->mAudVolArrIndex] =\r
-         getDecibelSound(peakDbValue);\r
+        mAudioVolumeArray[mAudVolArrIndex] = getDecibelSound(peakDbValue);\r
 \r
         LOGV("VideoEditorBGAudioProcessing:: getDecibelSound %d",\r
-         this->mAudioVolumeArray[this->mAudVolArrIndex]);\r
+            mAudioVolumeArray[mAudVolArrIndex]);\r
 \r
         // WINDOW_SIZE is 10 by default\r
         // Check for threshold is done after 10 cycles\r
-        if ( this->mAudVolArrIndex >= WINDOW_SIZE -1) {\r
-            this->mDoDucking = isThresholdBreached(this->mAudioVolumeArray,\r
-             this->mAudVolArrIndex,this->mDucking_threshold );\r
-\r
-            this->mAudVolArrIndex = 0;\r
+        if (mAudVolArrIndex >= WINDOW_SIZE - 1) {\r
+            mDoDucking = isThresholdBreached(mAudioVolumeArray,\r
+            mAudVolArrIndex,mDucking_threshold );\r
+            mAudVolArrIndex = 0;\r
         } else {\r
-            this->mAudVolArrIndex++;\r
+            mAudVolArrIndex++;\r
         }\r
 \r
         //\r
@@ -135,25 +132,23 @@ M4OSA_Int32 VideoEditorBGAudioProcessing::veProcessAudioMixNDuck(
         // (500 ms (window under analysis) / 20 ms (sample duration))\r
         //\r
 \r
-        if(this->mDoDucking){\r
-            if ( this->mDuckingFactor > this->mDucking_lowVolume) {\r
+        if (mDoDucking) {\r
+            if (mDuckingFactor > mDucking_lowVolume) {\r
                 // FADE OUT BG Track\r
                 // Increment ducking factor in total steps in factor\r
                 // of low volume steps to reach low volume level\r
-                this->mDuckingFactor -= (this->mDucking_lowVolume);\r
-            }\r
-            else {\r
-                this->mDuckingFactor = this->mDucking_lowVolume;\r
+                mDuckingFactor -= (mDucking_lowVolume);\r
+            } else {\r
+                mDuckingFactor = mDucking_lowVolume;\r
             }\r
         } else {\r
-            if ( this->mDuckingFactor < 1.0 ){\r
+            if (mDuckingFactor < 1.0 ) {\r
                 // FADE IN BG Track\r
                 // Increment ducking factor in total steps of\r
                 // low volume factor to reach orig.volume level\r
-                this->mDuckingFactor += (this->mDucking_lowVolume);\r
-            }\r
-            else{\r
-                this->mDuckingFactor = 1.0;\r
+                mDuckingFactor += (mDucking_lowVolume);\r
+            } else {\r
+                mDuckingFactor = 1.0;\r
             }\r
         }\r
     } // end if - mDucking_enable\r
@@ -162,46 +157,42 @@ M4OSA_Int32 VideoEditorBGAudioProcessing::veProcessAudioMixNDuck(
     // Mixing Logic\r
 \r
     LOGV("VideoEditorBGAudioProcessing:: Out of Ducking analysis uiPCMsize\\r
-     %d %f %f", this->mDoDucking, this->mDuckingFactor,this->mBTVolLevel);\r
+        %d %f %f", mDoDucking, mDuckingFactor,mBTVolLevel);\r
 \r
-    while(uiPCMsize-->0) {\r
+    while (uiPCMsize-- > 0) {\r
 \r
         M4OSA_Int32 temp;\r
         // Set vol factor for BT and PT\r
-        *pBTMdata1 = (M4OSA_Int16)(*pBTMdata1*this->mBTVolLevel);\r
-        *pPTMdata2 = (M4OSA_Int16)(*pPTMdata2*this->mPTVolLevel);\r
+        *pBTMdata1 = (M4OSA_Int16)(*pBTMdata1*mBTVolLevel);\r
+        *pPTMdata2 = (M4OSA_Int16)(*pPTMdata2*mPTVolLevel);\r
 \r
         // Mix the two samples\r
-        if ( this->mDoDucking) {\r
+        if (mDoDucking) {\r
 \r
             // Duck the BG track to ducking factor value before mixing\r
-            *pBTMdata1 = (M4OSA_Int16)((*pBTMdata1)*(this->mDuckingFactor));\r
+            *pBTMdata1 = (M4OSA_Int16)((*pBTMdata1)*(mDuckingFactor));\r
 \r
             // mix as normal case\r
             *pBTMdata1 = (M4OSA_Int16)(*pBTMdata1 /2 + *pPTMdata2 /2);\r
-        }\r
-        else {\r
+        } else {\r
 \r
-            *pBTMdata1 = (M4OSA_Int16)((*pBTMdata1)*(this->mDuckingFactor));\r
+            *pBTMdata1 = (M4OSA_Int16)((*pBTMdata1)*(mDuckingFactor));\r
             *pBTMdata1 = (M4OSA_Int16)(*pBTMdata1 /2 + *pPTMdata2 /2);\r
         }\r
 \r
-        if ( *pBTMdata1 < 0) {\r
+        if (*pBTMdata1 < 0) {\r
             temp = -(*pBTMdata1) * 2; // bring to original Amplitude level\r
 \r
-            if ( temp > 32767) {\r
+            if (temp > 32767) {\r
                 *pBTMdata1 = -32766; // less then max allowed value\r
-            }\r
-            else{\r
+            } else {\r
                 *pBTMdata1 = (M4OSA_Int16)(-temp);\r
             }\r
-        }\r
-        else {\r
+        } else {\r
             temp = (*pBTMdata1) * 2; // bring to original Amplitude level\r
             if ( temp > 32768) {\r
                 *pBTMdata1 = 32767; // less than max allowed value\r
-            }\r
-            else {\r
+            } else {\r
                 *pBTMdata1 = (M4OSA_Int16)temp;\r
             }\r
         }\r
@@ -211,140 +202,115 @@ M4OSA_Int32 VideoEditorBGAudioProcessing::veProcessAudioMixNDuck(
     }\r
     //LOGV("VideoEditorBGAudioProcessing:: Copy final out ");\r
     memcpy((void *)pMixedOutBuffer->m_dataAddress,\r
-     (void *)pBackgroundTrack->m_dataAddress,\r
-      pBackgroundTrack->m_bufferSize);\r
+        (void *)pBackgroundTrack->m_dataAddress,\r
+        pBackgroundTrack->m_bufferSize);\r
 \r
     LOGV("VideoEditorBGAudioProcessing::lvProcessAudioMixNDuck EXIT");\r
     return result;\r
 }\r
 \r
-VideoEditorBGAudioProcessing:: ~VideoEditorBGAudioProcessing() {\r
+VideoEditorBGAudioProcessing::~VideoEditorBGAudioProcessing() {\r
 \r
- //free(VideoEditorBGAudioProcessing:: pTempBuffer);\r
 }\r
 \r
 M4OSA_Int32 VideoEditorBGAudioProcessing::calculateOutResampleBufSize() {\r
 \r
-    M4OSA_Int32 bufSize =0;\r
-\r
     // This already takes care of channel count in mBTBuffer.m_bufferSize\r
-    bufSize = (this->mOutSampleRate/this->mInSampleRate)*this->mBTBuffer.m_bufferSize;\r
-\r
-    return bufSize;\r
+    return (mOutSampleRate / mInSampleRate) * mBTBuffer.m_bufferSize;\r
 }\r
 \r
 void VideoEditorBGAudioProcessing ::veSetAudioProcessingParams(\r
-        veAudMixSettings gInputParams) {\r
+        const veAudMixSettings& gInputParams) {\r
 \r
     LOGV("VideoEditorBGAudioProcessing:: ENTER lvSetAudioProcessingParams ");\r
-    this->mDucking_enable       = gInputParams.lvInDucking_enable;\r
-    this->mDucking_lowVolume    = gInputParams.lvInDucking_lowVolume;\r
-    this->mDucking_threshold    = gInputParams.lvInDucking_threshold;\r
+    mDucking_enable       = gInputParams.lvInDucking_enable;\r
+    mDucking_lowVolume    = gInputParams.lvInDucking_lowVolume;\r
+    mDucking_threshold    = gInputParams.lvInDucking_threshold;\r
 \r
-    this->mPTVolLevel           = gInputParams.lvPTVolLevel;\r
-    this->mBTVolLevel           = gInputParams.lvBTVolLevel ;\r
+    mPTVolLevel           = gInputParams.lvPTVolLevel;\r
+    mBTVolLevel           = gInputParams.lvBTVolLevel ;\r
 \r
-    this->mBTChannelCount       = gInputParams.lvBTChannelCount;\r
-    this->mPTChannelCount       = gInputParams.lvPTChannelCount;\r
+    mBTChannelCount       = gInputParams.lvBTChannelCount;\r
+    mPTChannelCount       = gInputParams.lvPTChannelCount;\r
 \r
-    this->mBTFormat             = gInputParams.lvBTFormat;\r
+    mBTFormat             = gInputParams.lvBTFormat;\r
 \r
-    this->mInSampleRate         = gInputParams.lvInSampleRate;\r
-    this->mOutSampleRate        = gInputParams.lvOutSampleRate;\r
+    mInSampleRate         = gInputParams.lvInSampleRate;\r
+    mOutSampleRate        = gInputParams.lvOutSampleRate;\r
 \r
-    this->mAudVolArrIndex       = 0;\r
-    this->mDoDucking            = 0;\r
-    this->mDuckingFactor        = 1.0; // default\r
+    mAudVolArrIndex       = 0;\r
+    mDoDucking            = 0;\r
+    mDuckingFactor        = 1.0; // default\r
 \r
     LOGV("VideoEditorBGAudioProcessing::  ducking_enable 0x%x \\r
-     ducking_lowVolume %f  ducking_threshold %d  fPTVolLevel %f BTVolLevel %f",\r
-     this->mDucking_enable, this->mDucking_lowVolume, this->mDucking_threshold,\r
-     this->mPTVolLevel, this->mPTVolLevel);\r
+        ducking_lowVolume %f  ducking_threshold %d  fPTVolLevel %f BTVolLevel %f",\r
+        mDucking_enable, mDucking_lowVolume, mDucking_threshold,\r
+        mPTVolLevel, mPTVolLevel);\r
 \r
     // Following logc decides if SSRC support is needed for this mixing\r
-    if ( gInputParams.lvInSampleRate != gInputParams.lvOutSampleRate){\r
-        this->mIsSSRCneeded      = 1;\r
-    }else{\r
-        this->mIsSSRCneeded      = 0;\r
-    }\r
-    if( gInputParams.lvBTChannelCount != gInputParams.lvPTChannelCount){\r
+    mIsSSRCneeded = (gInputParams.lvInSampleRate != gInputParams.lvOutSampleRate);\r
+    if (gInputParams.lvBTChannelCount != gInputParams.lvPTChannelCount){\r
         if (gInputParams.lvBTChannelCount == 2){\r
-            this->mChannelConversion   = 1; // convert to MONO\r
-        }else{\r
-            this->mChannelConversion   = 2; // Convert to STEREO\r
+            mChannelConversion   = 1; // convert to MONO\r
+        } else {\r
+            mChannelConversion   = 2; // Convert to STEREO\r
         }\r
-    }else{\r
-        this->mChannelConversion   = 0;\r
+    } else {\r
+        mChannelConversion   = 0;\r
     }\r
     LOGV("VideoEditorBGAudioProcessing:: EXIT veSetAudioProcessingParams ");\r
 }\r
 \r
 \r
-M4OSA_Int32 VideoEditorBGAudioProcessing:: getDecibelSound(M4OSA_UInt32 value) {\r
-\r
-    int dbSound = 1;\r
-\r
-    if (value == 0) return 0;\r
-\r
-    if (value > 0x4000 && value <= 0x8000) // 32768\r
-        dbSound = 90;\r
-    else if (value > 0x2000 && value <= 0x4000) // 16384\r
-        dbSound = 84;\r
-    else if (value > 0x1000 && value <= 0x2000) // 8192\r
-        dbSound = 78;\r
-    else if (value > 0x0800 && value <= 0x1000) // 4028\r
-        dbSound = 72;\r
-    else if (value > 0x0400 && value <= 0x0800) // 2048\r
-        dbSound = 66;\r
-    else if (value > 0x0200 && value <= 0x0400) // 1024\r
-        dbSound = 60;\r
-    else if (value > 0x0100 && value <= 0x0200) // 512\r
-        dbSound = 54;\r
-    else if (value > 0x0080 && value <= 0x0100) // 256\r
-        dbSound = 48;\r
-    else if (value > 0x0040 && value <= 0x0080) // 128\r
-        dbSound = 42;\r
-    else if (value > 0x0020 && value <= 0x0040) // 64\r
-        dbSound = 36;\r
-    else if (value > 0x0010 && value <= 0x0020) // 32\r
-        dbSound = 30;\r
-    else if (value > 0x0008 && value <= 0x0010) //16\r
-        dbSound = 24;\r
-    else if (value > 0x0007 && value <= 0x0008) //8\r
-        dbSound = 24;\r
-    else if (value > 0x0003 && value <= 0x0007) // 4\r
-        dbSound = 18;\r
-    else if (value > 0x0001 && value <= 0x0003) //2\r
-        dbSound = 12;\r
-    else if (value > 0x000 && value <= 0x0001) // 1\r
-        dbSound = 6;\r
-    else\r
-        dbSound = 0;\r
-\r
-    return dbSound;\r
+// Fast way to compute 10 * log(value)\r
+M4OSA_Int32 VideoEditorBGAudioProcessing::getDecibelSound(M4OSA_UInt32 value) {\r
+    if (value <= 0 || value > 0x8000) {\r
+        return 0;\r
+    } else if (value > 0x4000) { // 32768\r
+        return 90;\r
+    } else if (value > 0x2000) { // 16384\r
+        return 84;\r
+    } else if (value > 0x1000) { // 8192\r
+        return 78;\r
+    } else if (value > 0x0800) { // 4028\r
+        return 72;\r
+    } else if (value > 0x0400) { // 2048\r
+        return 66;\r
+    } else if (value > 0x0200) { // 1024\r
+        return 60;\r
+    } else if (value > 0x0100) { // 512\r
+        return 54;\r
+    } else if (value > 0x0080) { // 256\r
+        return 48;\r
+    } else if (value > 0x0040) { // 128\r
+        return 42;\r
+    } else if (value > 0x0020) { // 64\r
+        return 36;\r
+    } else if (value > 0x0010) { // 32\r
+        return 30;\r
+    } else if (value > 0x0008) { // 16\r
+        return 24;\r
+    } else if (value > 0x0007) { // 8\r
+        return 24;\r
+    } else if (value > 0x0003) { // 4\r
+        return 18;\r
+    } else if (value > 0x0001) { // 2\r
+        return 12;\r
+    } else  { // 1\r
+        return 6;\r
+    }\r
 }\r
 \r
-M4OSA_Bool VideoEditorBGAudioProcessing:: isThresholdBreached(\r
-        M4OSA_Int32* averageValue, M4OSA_Int32 storeCount,\r
-         M4OSA_Int32 thresholdValue) {\r
-\r
-    M4OSA_Bool result = 0;\r
-    int i;\r
-    int finalValue = 0;\r
-\r
-    for (i=0; i< storeCount;i++)\r
-        finalValue += averageValue[i];\r
+M4OSA_Bool VideoEditorBGAudioProcessing::isThresholdBreached(\r
+        M4OSA_Int32* averageValue,\r
+        M4OSA_Int32 storeCount,\r
+        M4OSA_Int32 thresholdValue) {\r
 \r
-    finalValue = finalValue/storeCount;\r
-\r
-    //printf ("<%d > \t  ", finalValue);\r
-\r
-    if (finalValue > thresholdValue)\r
-        result = M4OSA_TRUE;\r
-    else\r
-        result = M4OSA_FALSE;\r
-\r
-    return result;\r
+    int totalValue = 0;\r
+    for (int i = 0; i < storeCount; ++i) {\r
+        totalValue += averageValue[i];\r
+    }\r
+    return (totalValue / storeCount > thresholdValue);\r
 }\r
 \r
 }//namespace android\r
index 24586a7..1015721 100755 (executable)
@@ -53,8 +53,12 @@ typedef struct {
 class VideoEditorBGAudioProcessing {\r
 public:\r
     VideoEditorBGAudioProcessing();\r
-    void veSetAudioProcessingParams(veAudMixSettings mixParams);\r
-    M4OSA_Int32 veProcessAudioMixNDuck(void* , void *, void *);\r
+    void veSetAudioProcessingParams(const veAudMixSettings& mixParams);\r
+\r
+    M4OSA_Int32 veProcessAudioMixNDuck(\r
+                    void* primaryTrackBuffer,\r
+                    void* backgroundTrackBuffer,\r
+                    void* mixedOutputBuffer);\r
 \r
     ~VideoEditorBGAudioProcessing();\r
 \r
@@ -78,15 +82,11 @@ private:
     M4OSA_Float mPTVolLevel;\r
     M4OSA_Float mBTVolLevel;\r
 \r
-    M4AM_Buffer16 mPTBuffer;\r
     M4AM_Buffer16 mBTBuffer;\r
-    M4AM_Buffer16 mOutMixBuffer;\r
-    M4OSA_Int16 *mTempBuffer;\r
-    M4OSA_Int32 mTempFrameCount;\r
 \r
     M4OSA_Int32 getDecibelSound(M4OSA_UInt32 value);\r
     M4OSA_Bool  isThresholdBreached(M4OSA_Int32* averageValue,\r
-     M4OSA_Int32 storeCount, M4OSA_Int32 thresholdValue);\r
+                    M4OSA_Int32 storeCount, M4OSA_Int32 thresholdValue);\r
 \r
     // This returns the size of buffer which needs to allocated\r
     // before resampling is called\r