summaryrefslogtreecommitdiffstats
path: root/libvideoeditor/lvpp/VideoEditorBGAudioProcessing.cpp
blob: e24fcf43213a4ab1e8f7a5e58b9f3b3f2cb3d767 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "VideoEditorBGAudioProcessing"
#include <utils/Log.h>
#include "VideoEditorBGAudioProcessing.h"

namespace android {

VideoEditorBGAudioProcessing::VideoEditorBGAudioProcessing() {
    ALOGV("Constructor");

    mAudVolArrIndex = 0;
    mDoDucking = 0;
    mDucking_enable = 0;
    mDucking_lowVolume = 0;
    mDucking_threshold = 0;
    mDuckingFactor = 0;

    mBTVolLevel = 0;
    mPTVolLevel = 0;

    mIsSSRCneeded = 0;
    mChannelConversion = 0;

    mBTFormat = MONO_16_BIT;

    mInSampleRate = 8000;
    mOutSampleRate = 16000;
    mPTChannelCount = 2;
    mBTChannelCount = 1;
}

M4OSA_Int32 VideoEditorBGAudioProcessing::mixAndDuck(
        void *primaryTrackBuffer,
        void *backgroundTrackBuffer,
        void *outBuffer) {

    ALOGV("mixAndDuck: track buffers (primary: 0x%x and background: 0x%x) "
            "and out buffer 0x%x",
            primaryTrackBuffer, backgroundTrackBuffer, outBuffer);

    M4AM_Buffer16* pPrimaryTrack   = (M4AM_Buffer16*)primaryTrackBuffer;
    M4AM_Buffer16* pBackgroundTrack = (M4AM_Buffer16*)backgroundTrackBuffer;
    M4AM_Buffer16* pMixedOutBuffer  = (M4AM_Buffer16*)outBuffer;

    // Output size if same as PT size
    pMixedOutBuffer->m_bufferSize = pPrimaryTrack->m_bufferSize;

    // Before mixing, we need to have only PT as out buffer
    memcpy((void *)pMixedOutBuffer->m_dataAddress,
        (void *)pPrimaryTrack->m_dataAddress, pMixedOutBuffer->m_bufferSize);

    // Initialize ducking variables
    // Initially contains the input primary track
    M4OSA_Int16 *pPTMdata2 = (M4OSA_Int16*)pMixedOutBuffer->m_dataAddress;

    // Contains BG track processed data(like channel conversion etc..
    M4OSA_Int16 *pBTMdata1 = (M4OSA_Int16*) pBackgroundTrack->m_dataAddress;

    // Since we need to give sample count and not buffer size
    M4OSA_UInt32 uiPCMsize = pMixedOutBuffer->m_bufferSize / 2 ;

    if ((mDucking_enable) && (mPTVolLevel != 0.0)) {
        M4OSA_Int32 peakDbValue = 0;
        M4OSA_Int32 previousDbValue = 0;
        M4OSA_Int16 *pPCM16Sample = (M4OSA_Int16*)pPrimaryTrack->m_dataAddress;
        const size_t n = pPrimaryTrack->m_bufferSize / sizeof(M4OSA_Int16);

        for (size_t loopIndex = 0; loopIndex < n; ++loopIndex) {
            if (pPCM16Sample[loopIndex] >= 0) {
                peakDbValue = previousDbValue > pPCM16Sample[loopIndex] ?
                        previousDbValue : pPCM16Sample[loopIndex];
                previousDbValue = peakDbValue;
            } else {
                peakDbValue = previousDbValue > -pPCM16Sample[loopIndex] ?
                        previousDbValue: -pPCM16Sample[loopIndex];
                previousDbValue = peakDbValue;
            }
        }

        mAudioVolumeArray[mAudVolArrIndex] = getDecibelSound(peakDbValue);

        // Check for threshold is done after kProcessingWindowSize cycles
        if (mAudVolArrIndex >= kProcessingWindowSize - 1) {
            mDoDucking = isThresholdBreached(
                    mAudioVolumeArray, mAudVolArrIndex, mDucking_threshold);

            mAudVolArrIndex = 0;
        } else {
            mAudVolArrIndex++;
        }

        //
        // Below logic controls the mixing weightage
        // for Background and Primary Tracks
        // for the duration of window under analysis,
        // to give fade-out for Background and fade-in for primary
        // Current fading factor is distributed in equal range over
        // the defined window size.
        // For a window size = 25
        // (500 ms (window under analysis) / 20 ms (sample duration))
        //

        if (mDoDucking) {
            if (mDuckingFactor > mDucking_lowVolume) {
                // FADE OUT BG Track
                // Increment ducking factor in total steps in factor
                // of low volume steps to reach low volume level
                mDuckingFactor -= mDucking_lowVolume;
            } else {
                mDuckingFactor = mDucking_lowVolume;
            }
        } else {
            if (mDuckingFactor < 1.0 ) {
                // FADE IN BG Track
                // Increment ducking factor in total steps of
                // low volume factor to reach orig.volume level
                mDuckingFactor += mDucking_lowVolume;
            } else {
                mDuckingFactor = 1.0;
            }
        }
    } // end if - mDucking_enable


    // Mixing logic
    ALOGV("Out of Ducking analysis uiPCMsize %d %f %f",
            mDoDucking, mDuckingFactor, mBTVolLevel);
    while (uiPCMsize-- > 0) {

        // Set vol factor for BT and PT
        *pBTMdata1 = (M4OSA_Int16)(*pBTMdata1*mBTVolLevel);
        *pPTMdata2 = (M4OSA_Int16)(*pPTMdata2*mPTVolLevel);

        // Mix the two samples
        if (mDoDucking) {

            // Duck the BG track to ducking factor value before mixing
            *pBTMdata1 = (M4OSA_Int16)((*pBTMdata1)*(mDuckingFactor));

            // mix as normal case
            *pBTMdata1 = (M4OSA_Int16)(*pBTMdata1 /2 + *pPTMdata2 /2);
        } else {

            *pBTMdata1 = (M4OSA_Int16)((*pBTMdata1)*(mDuckingFactor));
            *pBTMdata1 = (M4OSA_Int16)(*pBTMdata1 /2 + *pPTMdata2 /2);
        }

        M4OSA_Int32 temp;
        if (*pBTMdata1 < 0) {
            temp = -(*pBTMdata1) * 2; // bring to original Amplitude level

            if (temp > 32767) {
                *pBTMdata1 = -32766; // less then max allowed value
            } else {
                *pBTMdata1 = (M4OSA_Int16)(-temp);
            }
        } else {
            temp = (*pBTMdata1) * 2; // bring to original Amplitude level
            if ( temp > 32768) {
                *pBTMdata1 = 32767; // less than max allowed value
            } else {
                *pBTMdata1 = (M4OSA_Int16)temp;
            }
        }

        pBTMdata1++;
        pPTMdata2++;
    }

    memcpy((void *)pMixedOutBuffer->m_dataAddress,
        (void *)pBackgroundTrack->m_dataAddress,
        pBackgroundTrack->m_bufferSize);

    ALOGV("mixAndDuck: X");
    return M4NO_ERROR;
}

M4OSA_Int32 VideoEditorBGAudioProcessing::calculateOutResampleBufSize() {

    // This already takes care of channel count in mBTBuffer.m_bufferSize
    return (mOutSampleRate / mInSampleRate) * mBTBuffer.m_bufferSize;
}

void VideoEditorBGAudioProcessing::setMixParams(
        const AudioMixSettings& setting) {
    ALOGV("setMixParams");

    mDucking_enable       = setting.lvInDucking_enable;
    mDucking_lowVolume    = setting.lvInDucking_lowVolume;
    mDucking_threshold    = setting.lvInDucking_threshold;
    mPTVolLevel           = setting.lvPTVolLevel;
    mBTVolLevel           = setting.lvBTVolLevel ;
    mBTChannelCount       = setting.lvBTChannelCount;
    mPTChannelCount       = setting.lvPTChannelCount;
    mBTFormat             = setting.lvBTFormat;
    mInSampleRate         = setting.lvInSampleRate;
    mOutSampleRate        = setting.lvOutSampleRate;

    // Reset the following params to default values
    mAudVolArrIndex       = 0;
    mDoDucking            = 0;
    mDuckingFactor        = 1.0;

    ALOGV("ducking enable 0x%x lowVolume %f threshold %d "
            "fPTVolLevel %f BTVolLevel %f",
            mDucking_enable, mDucking_lowVolume, mDucking_threshold,
            mPTVolLevel, mPTVolLevel);

    // Decides if SSRC support is needed for this mixing
    mIsSSRCneeded = (setting.lvInSampleRate != setting.lvOutSampleRate);
    if (setting.lvBTChannelCount != setting.lvPTChannelCount){
        if (setting.lvBTChannelCount == 2){
            mChannelConversion = 1; // convert to MONO
        } else {
            mChannelConversion = 2; // Convert to STEREO
        }
    } else {
        mChannelConversion = 0;
    }
}

// Fast way to compute 10 * log(value)
M4OSA_Int32 VideoEditorBGAudioProcessing::getDecibelSound(M4OSA_UInt32 value) {
    ALOGV("getDecibelSound: %ld", value);

    if (value <= 0 || value > 0x8000) {
        return 0;
    } else if (value > 0x4000) { // 32768
        return 90;
    } else if (value > 0x2000) { // 16384
        return 84;
    } else if (value > 0x1000) { // 8192
        return 78;
    } else if (value > 0x0800) { // 4028
        return 72;
    } else if (value > 0x0400) { // 2048
        return 66;
    } else if (value > 0x0200) { // 1024
        return 60;
    } else if (value > 0x0100) { // 512
        return 54;
    } else if (value > 0x0080) { // 256
        return 48;
    } else if (value > 0x0040) { // 128
        return 42;
    } else if (value > 0x0020) { // 64
        return 36;
    } else if (value > 0x0010) { // 32
        return 30;
    } else if (value > 0x0008) { // 16
        return 24;
    } else if (value > 0x0007) { // 8
        return 24;
    } else if (value > 0x0003) { // 4
        return 18;
    } else if (value > 0x0001) { // 2
        return 12;
    } else  { // 1
        return 6;
    }
}

M4OSA_Bool VideoEditorBGAudioProcessing::isThresholdBreached(
        M4OSA_Int32* averageValue,
        M4OSA_Int32 storeCount,
        M4OSA_Int32 thresholdValue) {

    ALOGV("isThresholdBreached");

    int totalValue = 0;
    for (int i = 0; i < storeCount; ++i) {
        totalValue += averageValue[i];
    }
    return (totalValue / storeCount > thresholdValue);
}

}//namespace android