videoeditorengine/audioeditorengine/resampler/src/resampler_sinc_conv_one_to_three_int16.cpp
changeset 0 951a5db380a0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/videoeditorengine/audioeditorengine/resampler/src/resampler_sinc_conv_one_to_three_int16.cpp	Fri Jan 29 14:08:33 2010 +0200
@@ -0,0 +1,275 @@
+/*
+* Copyright (c) 2010 Ixonos Plc.
+* All rights reserved.
+* This component and the accompanying materials are made available
+* under the terms of the "Eclipse Public License v1.0"
+* which accompanies this distribution, and is available
+* at the URL "http://www.eclipse.org/legal/epl-v10.html".
+*
+* Initial Contributors:
+* Nokia Corporation - Initial contribution
+*
+* Contributors:
+* Ixonos Plc
+*
+* Description:
+*
+*/
+
+
+#include "resampler_sinc_conv_one_to_three_int16.h"
+#include "resampler_clip.h"
+#include "resampler_sinc_conv_one_to_three_tables_standard.h"
+
+#include <string.h>
+
+#include "resampler_sinc_conv_one_to_three_int16.inl"
+
+static const int LC_ZERO_CROSSINGS = RESAMPLER_ONE_TO_THREE_ZERO_CROSSINGS_STANDARD;
+static const int LC_MOD3[6] = { 0, 1, 2, 0, 1, 2 };
+
+
+RESAMPLER_SincConvOneToThreeInt16::RESAMPLER_SincConvOneToThreeInt16(int channelCount) :
+m_memBuffers(0),
+m_scratchBuffer(0),
+m_filter1(RESAMPLER_ONE_TO_THREE_FILTER1_STANDARD),
+m_filter2(RESAMPLER_ONE_TO_THREE_FILTER2_STANDARD),
+m_channelCount(channelCount),
+m_blockSize(0),
+m_channelEnabled(0),
+m_inputSamples(0),
+m_state(2)
+{
+}
+
+
+RESAMPLER_SincConvOneToThreeInt16::~RESAMPLER_SincConvOneToThreeInt16()
+{
+    DeInit();
+}
+
+
+bool RESAMPLER_SincConvOneToThreeInt16::InitInputDriven()
+{
+    return Init();
+}
+
+
+bool RESAMPLER_SincConvOneToThreeInt16::InitOutputDriven()
+{
+    return Init();
+}
+
+
+/*
+*   This function must be called before using the converter.
+*   This function reserves memory for ring buffers and 
+*   prepares the ring buffer indexing.
+*/
+bool RESAMPLER_SincConvOneToThreeInt16::Init()
+{
+    int i(0);
+
+    m_memBuffers = new int16 *[m_channelCount];
+    if (!m_memBuffers)
+    {
+        return false;
+    }
+
+    for (i = 0; i < m_channelCount; i++) 
+    {
+        m_memBuffers[i] = 0;
+    }
+
+    m_channelEnabled = new bool[m_channelCount];
+    if (!m_channelEnabled)
+    {
+        DeInit();
+        return false;
+    }
+    for (i = 0; i < m_channelCount; i++) 
+    {
+        m_channelEnabled[i] = true;
+    }
+
+    for (i = 0; i < m_channelCount; i++) 
+    {
+        m_memBuffers[i] = new int16[LC_ZERO_CROSSINGS * 2];
+        if (!m_memBuffers[i])
+        {
+            DeInit();
+            return false;
+        }
+        memset(m_memBuffers[i], 0, sizeof(int16) * (LC_ZERO_CROSSINGS * 2));
+    }
+
+    return true;
+}
+
+
+void RESAMPLER_SincConvOneToThreeInt16::DeInit()
+{
+    if (m_channelCount)
+    {
+        for (int i = 0; i < m_channelCount; i++)
+        {
+            delete [] m_memBuffers[i];
+        }
+        delete [] m_memBuffers;
+        delete [] m_channelEnabled;
+    }
+}
+
+
+void RESAMPLER_SincConvOneToThreeInt16::EnableChannel(int channel)
+{
+    m_channelEnabled[channel] = true;
+}
+
+
+void RESAMPLER_SincConvOneToThreeInt16::DisableChannel(int channel)
+{
+    m_channelEnabled[channel] = false;
+}
+
+
+int RESAMPLER_SincConvOneToThreeInt16::MaxOutputSampleCount(int inSamples) const
+{ 
+    return 3*inSamples; 
+}
+
+
+int RESAMPLER_SincConvOneToThreeInt16::ProcessFromInput(int16 *outputBuffers[], 
+                                                  int16 *inputBuffers[], 
+                                                  int inSamples)
+{
+    int outSamples(3 * inSamples);
+    m_inputSamples = inSamples;
+    return ProcessToOutput(outputBuffers, inputBuffers, outSamples);
+}
+
+
+size_t 
+RESAMPLER_SincConvOneToThreeInt16::ScratchMemoryNeedOutputDriven(int maxOutputBlockSize) const
+{
+    return ScratchMemoryNeedInputDriven((maxOutputBlockSize + 2) / 3);
+}
+
+
+void 
+RESAMPLER_SincConvOneToThreeInt16::SetScratchBufferOutputDriven(char *buffer)
+{
+    m_scratchBuffer = (int16 *)buffer;
+}
+
+size_t 
+RESAMPLER_SincConvOneToThreeInt16::ScratchMemoryNeedInputDriven(int maxInputBlockSize) const
+{
+    return sizeof(int16) * (LC_ZERO_CROSSINGS * 2 + maxInputBlockSize);
+}
+
+
+void 
+RESAMPLER_SincConvOneToThreeInt16::SetScratchBufferInputDriven(char *buffer)
+{
+    m_scratchBuffer = (int16 *)buffer;
+}
+
+
+int RESAMPLER_SincConvOneToThreeInt16::MaxInputSampleCount(int outSamples) const
+{ 
+    return (outSamples + 2) / 3; 
+}
+
+
+/*
+*   This function returns the value, which is given as
+*   a parameter to the nextSamplesOut() function. This
+*   function must be called every time before calling
+*   nextSamplesOut.
+*/
+int RESAMPLER_SincConvOneToThreeInt16::InSamplesNeeded(int outSamples)
+{
+    m_inputSamples = (outSamples + LC_MOD3[4 - m_state]) / 3;
+
+    return m_inputSamples;      
+}
+
+
+int RESAMPLER_SincConvOneToThreeInt16::ProcessToOutput(int16 *outputBuffers[], 
+                                                 int16 *inputBuffers[], 
+                                                 int outSamples)
+{
+    static const int FILTER_LENGTH = 2 * LC_ZERO_CROSSINGS;
+    int i, j, k;
+    int bufReadHead1 = LC_ZERO_CROSSINGS + 1 - (m_state != 2 ? 1 : 0);
+    int bufReadHead2 = LC_ZERO_CROSSINGS;
+    int bufReadHead3 = LC_ZERO_CROSSINGS - (m_state == 1 ? 1 : 0);
+
+    for (i = 0; i < m_channelCount; i++) 
+    {
+        if (!m_channelEnabled[i])
+        {
+            break;
+        }
+
+        int16 *tempBuf = m_scratchBuffer;
+        int16 *outBuf  = outputBuffers[i];
+
+        memcpy(m_scratchBuffer, m_memBuffers[i], FILTER_LENGTH * sizeof(int16));
+
+        memcpy(tempBuf + FILTER_LENGTH, 
+               inputBuffers[i], 
+               m_inputSamples * sizeof(int16));
+
+        // copy samples into every third index of output buffer.
+        tempBuf = m_scratchBuffer + bufReadHead1;
+        for (j = 0, k = m_state; k < outSamples; j++, k+=3)
+        {
+            outBuf[k] = tempBuf[j];
+        }
+        
+        // Do band limited interpolation and set the result into 
+        // every third index in the output buffer. 
+        tempBuf = m_scratchBuffer + bufReadHead2;
+        for (j = 0, k = LC_MOD3[m_state+1]; k < outSamples; j++, k += 3) 
+        {
+            int32 newSample(0);
+             // Note that the filters are reversed
+            newSample = 
+                RESAMPLER_SincConvOneToThreeFilterInt16(tempBuf + j + 1,
+                                                  m_filter2,
+                                                  m_filter1,
+                                                  LC_ZERO_CROSSINGS);
+            // round and shift down 
+            outBuf[k] = (int16)RESAMPLER_Clip16((newSample + 16384) >> 15);
+        }
+       
+        // Do band limited interpolation and set the result into 
+        // every third index in the output buffer. 
+        tempBuf = m_scratchBuffer + bufReadHead3;
+        for (j = 0, k = LC_MOD3[m_state+2]; k < outSamples; j++, k += 3) 
+        {
+            int32 newSample(0);
+            newSample = 
+                RESAMPLER_SincConvOneToThreeFilterInt16(tempBuf + j + 1,
+                                                  m_filter1,
+                                                  m_filter2,
+                                                  LC_ZERO_CROSSINGS);
+            // round and shift down 
+            outBuf[k] = (int16)RESAMPLER_Clip16((newSample + 16384) >> 15);
+        }
+
+        // Copy the newest samples to the beginning of the buffer
+        memcpy(m_memBuffers[i], 
+               m_scratchBuffer + m_inputSamples, 
+               FILTER_LENGTH * sizeof(int16));       
+    }    
+
+    // Update state according to amount of output samples.
+    m_state = LC_MOD3[m_state + (3 - (outSamples % 3))];
+
+    return outSamples;
+}
+
+