videoeditorengine/audioeditorengine/resampler/src/resampler_sinc_conv_two_to_one_int16.cpp
changeset 0 951a5db380a0
equal deleted inserted replaced
-1:000000000000 0:951a5db380a0
       
     1 /*
       
     2 * Copyright (c) 2010 Ixonos Plc.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - Initial contribution
       
    11 *
       
    12 * Contributors:
       
    13 * Ixonos Plc
       
    14 *
       
    15 * Description:
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "resampler_sinc_conv_two_to_one_int16.h"
       
    21 #include "resampler_clip.h"
       
    22 #include "resampler_sinc_conv_two_to_one_tables_standard.h"
       
    23 #include "resampler_sinc_conv_filter_two_to_one_int16.h"
       
    24 
       
    25 #include <string.h>
       
    26 
       
    27 /*
       
    28  * The amount of zero crossings in positive or negative
       
    29  * side of the sinc function. Because of filter symmetry
       
    30  * the amount of filter taps used in convolution is
       
    31  * zero crossings * 2.
       
    32  */
       
    33 static const int LC_MAX_ZERO_CROSSINGS = RESAMPLER_TWO_TO_ONE_ZERO_CROSSINGS_STANDARD;
       
    34 
       
    35 static const int LC_BUFFER_ACCESS_OUTPUT = 2 * LC_MAX_ZERO_CROSSINGS;
       
    36 
       
    37 
       
    38 RESAMPLER_SincConvTwoToOneInt16::RESAMPLER_SincConvTwoToOneInt16(int channelCount) :
       
    39 m_memBuffers(0),
       
    40 m_scratchBuffer(0),
       
    41 m_channelCount(channelCount),
       
    42 m_blockSize(0),
       
    43 m_channelEnabled(0),
       
    44 m_state(0),
       
    45 m_zeroCrossings(RESAMPLER_TWO_TO_ONE_ZERO_CROSSINGS_STANDARD),
       
    46 m_filterMatrix(RESAMPLER_TWO_TO_ONE_FILTERS_STANDARD)
       
    47 {
       
    48 }
       
    49 
       
    50 
       
    51 RESAMPLER_SincConvTwoToOneInt16::~RESAMPLER_SincConvTwoToOneInt16()
       
    52 {
       
    53     DeInit();
       
    54 }
       
    55 
       
    56 bool RESAMPLER_SincConvTwoToOneInt16::InitInputDriven()
       
    57 {
       
    58     return Init();
       
    59 }
       
    60 
       
    61 bool RESAMPLER_SincConvTwoToOneInt16::InitOutputDriven()
       
    62 {
       
    63     return Init();
       
    64 }
       
    65 
       
    66 bool 
       
    67 RESAMPLER_SincConvTwoToOneInt16::Init()
       
    68 {
       
    69     int i(0);
       
    70 
       
    71     m_memBuffers = new int16 *[m_channelCount];
       
    72     if (!m_memBuffers)
       
    73     {
       
    74         return false;
       
    75     }
       
    76 
       
    77     for (i = 0; i < m_channelCount; i++) 
       
    78     {
       
    79         m_memBuffers[i] = 0;
       
    80     }
       
    81 
       
    82     m_channelEnabled = new bool[m_channelCount];
       
    83     if (!m_channelEnabled)
       
    84     {
       
    85         DeInit();
       
    86         return false;
       
    87     }
       
    88     for (i = 0; i < m_channelCount; i++) 
       
    89     {
       
    90         m_channelEnabled[i] = true;
       
    91     }
       
    92 
       
    93     for (i = 0; i < m_channelCount; i++) 
       
    94     {
       
    95         m_memBuffers[i] = new int16[LC_MAX_ZERO_CROSSINGS * 4];
       
    96         if (!m_memBuffers[i])
       
    97         {
       
    98             DeInit();
       
    99             return false;
       
   100         }
       
   101         memset(m_memBuffers[i], 0, sizeof(int16) * (LC_MAX_ZERO_CROSSINGS * 4));
       
   102     }
       
   103 
       
   104     return true;
       
   105 }
       
   106 
       
   107 void 
       
   108 RESAMPLER_SincConvTwoToOneInt16::DeInit()
       
   109 {
       
   110     if (m_channelCount)
       
   111     {
       
   112         for (int i = 0; i < m_channelCount; i++)
       
   113         {
       
   114             delete [] m_memBuffers[i];
       
   115         }
       
   116         delete [] m_memBuffers;
       
   117         delete [] m_channelEnabled;
       
   118     }
       
   119 }
       
   120 
       
   121 
       
   122 void 
       
   123 RESAMPLER_SincConvTwoToOneInt16::EnableChannel(int channel)
       
   124 {
       
   125     m_channelEnabled[channel] = true;
       
   126 }
       
   127 
       
   128 void 
       
   129 RESAMPLER_SincConvTwoToOneInt16::DisableChannel(int channel)
       
   130 {
       
   131     m_channelEnabled[channel] = false;
       
   132 }
       
   133 
       
   134 size_t 
       
   135 RESAMPLER_SincConvTwoToOneInt16::ScratchMemoryNeedInputDriven(int maxInputBlockSize) const
       
   136 {
       
   137     return sizeof(int16) * (LC_MAX_ZERO_CROSSINGS * 4 + maxInputBlockSize);
       
   138 }
       
   139 
       
   140 void 
       
   141 RESAMPLER_SincConvTwoToOneInt16::SetScratchBufferInputDriven(char *buffer)
       
   142 {
       
   143     m_scratchBuffer = (int16 *)buffer;
       
   144 }
       
   145 
       
   146 size_t 
       
   147 RESAMPLER_SincConvTwoToOneInt16::ScratchMemoryNeedOutputDriven(int maxOutputBlockSize) const
       
   148 {
       
   149     int blockSize = 2 * maxOutputBlockSize; 
       
   150     
       
   151     return ScratchMemoryNeedInputDriven(blockSize);
       
   152 }
       
   153 
       
   154 void 
       
   155 RESAMPLER_SincConvTwoToOneInt16::SetScratchBufferOutputDriven(char *buffer)
       
   156 {
       
   157     m_scratchBuffer = (int16 *)buffer;
       
   158 }
       
   159 
       
   160 
       
   161 int 
       
   162 RESAMPLER_SincConvTwoToOneInt16::MaxOutputSampleCount(int inSamples) const 
       
   163 { 
       
   164     return ( inSamples + 1 ) / 2; 
       
   165 }
       
   166 
       
   167 int 
       
   168 RESAMPLER_SincConvTwoToOneInt16::MaxInputSampleCount(int outSamples) const
       
   169 { 
       
   170     return 2 * outSamples; 
       
   171 }
       
   172 
       
   173 
       
   174 int 
       
   175 RESAMPLER_SincConvTwoToOneInt16::InSamplesNeeded(int outSamples)
       
   176 {
       
   177     return 2 * outSamples;
       
   178 }
       
   179 
       
   180 
       
   181 int 
       
   182 RESAMPLER_SincConvTwoToOneInt16::ProcessToOutput(int16 *outputBuffers[], 
       
   183                                            int16 *inputBuffers[], 
       
   184                                            int outSamples)
       
   185 {
       
   186     int inSamples( 2 * outSamples );
       
   187     
       
   188     return ProcessFromInput(outputBuffers, inputBuffers, inSamples);
       
   189 }
       
   190 
       
   191 
       
   192 int 
       
   193 RESAMPLER_SincConvTwoToOneInt16::ProcessFromInput(int16 *outputBuffers[], 
       
   194                                             int16 *inputBuffers[], 
       
   195                                             int inSamples)
       
   196 {
       
   197     static const int FILTER_LENGTH = 4 * LC_MAX_ZERO_CROSSINGS;
       
   198 	int i, j;
       
   199     int state = m_state;
       
   200 	int outSamples = (inSamples + state) >> 1;
       
   201 
       
   202     for (i = 0; i < m_channelCount; i++) 
       
   203     {
       
   204         if (!m_channelEnabled[i])
       
   205         {
       
   206             break;
       
   207         }
       
   208 
       
   209         int16 *tempBuf = m_scratchBuffer;
       
   210         int16 *outBuf  = outputBuffers[i];
       
   211         state = m_state;
       
   212         
       
   213         memcpy(m_scratchBuffer, 
       
   214                m_memBuffers[i], 
       
   215                FILTER_LENGTH * sizeof(int16));
       
   216 
       
   217         // Read samples into memory
       
   218         memcpy(tempBuf + FILTER_LENGTH, 
       
   219                inputBuffers[i], 
       
   220                inSamples * sizeof(int16));
       
   221 
       
   222         tempBuf += LC_BUFFER_ACCESS_OUTPUT - state;
       
   223 
       
   224         for (j = 0; j < outSamples; j++) 
       
   225         {
       
   226             int32 newSample = 
       
   227                 RESAMPLER_SincConvFilterTwoToOneInt16(tempBuf,
       
   228 												m_filterMatrix,
       
   229 												m_zeroCrossings);
       
   230             tempBuf += 2;
       
   231 
       
   232             // round and shift down
       
   233 	        outBuf[j] = (int16)RESAMPLER_Clip16((newSample + 16384 ) >> 15);
       
   234 		}
       
   235         
       
   236         // Copy the newest samples to the beginning of the buffer
       
   237         memcpy(m_memBuffers[i], 
       
   238                m_scratchBuffer + inSamples, 
       
   239                FILTER_LENGTH * sizeof(int16));       
       
   240     }
       
   241 
       
   242     m_state = (state + inSamples) & 1;
       
   243 
       
   244 	return outSamples;
       
   245 }
       
   246