videoeditorengine/audioeditorengine/resampler/src/resampler_sinc_conv_two_to_three_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_three_int16.h"
       
    21 #include "resampler_clip.h"
       
    22 #include "resampler_sinc_conv_two_to_three_tables_standard.h"
       
    23 
       
    24 #include "resampler_sinc_conv_two_to_three_int16.inl"
       
    25 
       
    26 
       
    27 #include <string.h>
       
    28 
       
    29 
       
    30 static const int LC_ZERO_CROSSINGS = RESAMPLER_TWO_TO_THREE_ZERO_CROSSINGS_STANDARD;
       
    31 static const int LC_MOD3[6] = { 0, 1, 2, 0, 1, 2 };
       
    32 
       
    33 
       
    34 RESAMPLER_SincConvTwoToThreeInt16::RESAMPLER_SincConvTwoToThreeInt16(int channelCount) :
       
    35 m_memBuffers(0),
       
    36 m_scratchBuffer(0),
       
    37 m_filter1(RESAMPLER_TWO_TO_THREE_FILTER1_STANDARD),
       
    38 m_filter2(RESAMPLER_TWO_TO_THREE_FILTER2_STANDARD),
       
    39 m_channelCount(channelCount),
       
    40 m_blockSize(0),
       
    41 m_channelEnabled(0),
       
    42 m_inputSamples(0),
       
    43 m_state(2)
       
    44 {
       
    45 }
       
    46 
       
    47 
       
    48 RESAMPLER_SincConvTwoToThreeInt16::~RESAMPLER_SincConvTwoToThreeInt16()
       
    49 {
       
    50     DeInit();
       
    51 }
       
    52 
       
    53 
       
    54 bool RESAMPLER_SincConvTwoToThreeInt16::InitInputDriven()
       
    55 {
       
    56     return Init();
       
    57 }
       
    58 
       
    59 
       
    60 bool RESAMPLER_SincConvTwoToThreeInt16::InitOutputDriven()
       
    61 {
       
    62     return Init();
       
    63 }
       
    64 
       
    65 
       
    66 /*
       
    67 *   This function must be called before using the converter.
       
    68 *   This function reserves memory for ring buffers and 
       
    69 *   prepares the ring buffer indexing.
       
    70 */
       
    71 bool RESAMPLER_SincConvTwoToThreeInt16::Init()
       
    72 {
       
    73     int i = 0;
       
    74 
       
    75     m_memBuffers = new int16 *[m_channelCount];
       
    76     if (!m_memBuffers)
       
    77     {
       
    78         return false;
       
    79     }
       
    80 
       
    81     for (i = 0; i < m_channelCount; i++) 
       
    82     {
       
    83         m_memBuffers[i] = 0;
       
    84     }
       
    85 
       
    86     m_channelCount = m_channelCount;
       
    87 
       
    88     m_channelEnabled = new bool[m_channelCount];
       
    89     if (!m_channelEnabled)
       
    90     {
       
    91         DeInit();
       
    92         return false;
       
    93     }
       
    94     for (i = 0; i < m_channelCount; i++) 
       
    95     {
       
    96         m_channelEnabled[i] = true;
       
    97     }
       
    98 
       
    99     for (i = 0; i < m_channelCount; i++) 
       
   100     {
       
   101         m_memBuffers[i] = new int16[LC_ZERO_CROSSINGS * 2];
       
   102         if (!m_memBuffers[i])
       
   103         {
       
   104             DeInit();
       
   105             return false;
       
   106         }
       
   107         memset(m_memBuffers[i], 0, sizeof(int16) * (LC_ZERO_CROSSINGS * 2));
       
   108     }
       
   109 
       
   110     return true;
       
   111 }
       
   112 
       
   113 
       
   114 void RESAMPLER_SincConvTwoToThreeInt16::DeInit()
       
   115 {
       
   116     if (m_channelCount)
       
   117     {
       
   118         for (int i = 0; i < m_channelCount; i++)
       
   119         {
       
   120             delete [] m_memBuffers[i];
       
   121         }
       
   122         delete [] m_memBuffers;
       
   123         delete [] m_channelEnabled;
       
   124     }
       
   125 }
       
   126 
       
   127 
       
   128 void RESAMPLER_SincConvTwoToThreeInt16::EnableChannel(int channel)
       
   129 {
       
   130     m_channelEnabled[channel] = true;
       
   131 }
       
   132 
       
   133 
       
   134 void RESAMPLER_SincConvTwoToThreeInt16::DisableChannel(int channel)
       
   135 {
       
   136     m_channelEnabled[channel] = false;
       
   137 }
       
   138 
       
   139 
       
   140 int RESAMPLER_SincConvTwoToThreeInt16::MaxOutputSampleCount(int inSamples) const 
       
   141 { 
       
   142     return 3 * inSamples / 2 + 1; 
       
   143 }
       
   144 
       
   145 
       
   146 int RESAMPLER_SincConvTwoToThreeInt16::ProcessFromInput(int16 *outputBuffers[], 
       
   147                                                   int16 *inputBuffers[], 
       
   148                                                   int inSamples)
       
   149 {
       
   150     int outSamples( (3*inSamples + 2 - m_state) / 2 );
       
   151     m_inputSamples = inSamples;
       
   152 
       
   153     return ProcessToOutput(outputBuffers, inputBuffers, outSamples);
       
   154 }
       
   155 
       
   156 
       
   157 size_t 
       
   158 RESAMPLER_SincConvTwoToThreeInt16::ScratchMemoryNeedOutputDriven(int maxOutputBlockSize) const
       
   159 {
       
   160     return ScratchMemoryNeedInputDriven((2*maxOutputBlockSize + 2) / 3);
       
   161 }
       
   162 
       
   163 
       
   164 void 
       
   165 RESAMPLER_SincConvTwoToThreeInt16::SetScratchBufferOutputDriven(char *buffer)
       
   166 {
       
   167     m_scratchBuffer = (int16 *)buffer;
       
   168 }
       
   169 
       
   170 size_t 
       
   171 RESAMPLER_SincConvTwoToThreeInt16::ScratchMemoryNeedInputDriven(int maxInputBlockSize) const
       
   172 {
       
   173     return sizeof(int16) * (LC_ZERO_CROSSINGS * 2 + maxInputBlockSize);
       
   174 }
       
   175 
       
   176 
       
   177 void 
       
   178 RESAMPLER_SincConvTwoToThreeInt16::SetScratchBufferInputDriven(char *buffer)
       
   179 {
       
   180     m_scratchBuffer = (int16 *)buffer;
       
   181 }
       
   182 
       
   183 int RESAMPLER_SincConvTwoToThreeInt16::MaxInputSampleCount(int outSamples) const 
       
   184 { 
       
   185     return (2*outSamples+2)/3; 
       
   186 }
       
   187 
       
   188     
       
   189 /*
       
   190 *   This function returns the value, which is given as
       
   191 *   a parameter to the nextSamplesOut() function. This
       
   192 *   function must be called every time before calling
       
   193 *   nextSamplesOut.
       
   194 */
       
   195 int RESAMPLER_SincConvTwoToThreeInt16::InSamplesNeeded(int outSamples)
       
   196 {
       
   197     m_inputSamples = (2 * outSamples + m_state) / 3;
       
   198     return m_inputSamples;
       
   199 }
       
   200 
       
   201 
       
   202 int RESAMPLER_SincConvTwoToThreeInt16::ProcessToOutput(int16 *outputBuffers[], 
       
   203                                                  int16 *inputBuffers[], 
       
   204                                                  int outSamples)
       
   205 {
       
   206     static const int FILTER_LENGTH = 2 * LC_ZERO_CROSSINGS; 
       
   207     int i, j, k;
       
   208     
       
   209     int bufReadHead1 = LC_ZERO_CROSSINGS + m_state;
       
   210     int bufReadHead2 = LC_ZERO_CROSSINGS + (m_state == 1 ? 1 : 0);
       
   211     int bufReadHead3 = LC_ZERO_CROSSINGS + (m_state != 1 ? 1 : 0);
       
   212 
       
   213     for (i = 0; i < m_channelCount; i++) 
       
   214     {
       
   215         if (!m_channelEnabled[i])
       
   216         {
       
   217             break;
       
   218         }
       
   219 
       
   220         int16 *tempBuf = m_scratchBuffer;
       
   221         int16 *outBuf  = outputBuffers[i];
       
   222 
       
   223         memcpy(m_scratchBuffer, m_memBuffers[i], FILTER_LENGTH * sizeof(int16));
       
   224 
       
   225         memcpy(tempBuf + FILTER_LENGTH, 
       
   226                inputBuffers[i], 
       
   227                m_inputSamples * sizeof(int16));
       
   228 
       
   229         // copy every second sample into every third index of output buffer.
       
   230         tempBuf = m_scratchBuffer + bufReadHead1;
       
   231         for (j = 0, k = m_state; k < outSamples; j+=2, k+=3)
       
   232         {
       
   233             outBuf[k] = tempBuf[j]; 
       
   234         }
       
   235         
       
   236         // Do band limited interpolation and set the result into 
       
   237         // every third index in the output buffer. 
       
   238         tempBuf = m_scratchBuffer + bufReadHead2;
       
   239         for (j = 0, k = LC_MOD3[m_state+1]; k < outSamples; j += 2, k += 3) 
       
   240         {
       
   241              // Note that the filters are reversed
       
   242  
       
   243             int32 newSample = RESAMPLER_SincConvTwoToThreeFilterInt16(tempBuf + j, 
       
   244                                                                 m_filter2,
       
   245                                                                 m_filter1,
       
   246                                                                 LC_ZERO_CROSSINGS);
       
   247 
       
   248             // round and shift down 
       
   249             outBuf[k] = (int16)RESAMPLER_Clip16((newSample + 16384) >> 15);
       
   250         }
       
   251        
       
   252         // Do band limited interpolation and set the result into 
       
   253         // every third index in the output buffer. 
       
   254         tempBuf = m_scratchBuffer + bufReadHead3;
       
   255         for (j = 0, k = LC_MOD3[m_state+2]; k < outSamples; j += 2, k += 3) 
       
   256         {
       
   257             int32 newSample = RESAMPLER_SincConvTwoToThreeFilterInt16(tempBuf + j, 
       
   258                                                                 m_filter1,
       
   259                                                                 m_filter2,
       
   260                                                                 LC_ZERO_CROSSINGS);
       
   261 
       
   262             // round and shift down 
       
   263             outBuf[k] = (int16)RESAMPLER_Clip16((newSample + 16384) >> 15);
       
   264         }
       
   265 
       
   266         // Copy the newest samples to the beginning of the buffer
       
   267         memcpy(m_memBuffers[i], 
       
   268                m_scratchBuffer + m_inputSamples, 
       
   269                FILTER_LENGTH * sizeof(int16));       
       
   270     }    
       
   271     
       
   272     // Update state according to amount of output samples.
       
   273     m_state = LC_MOD3[m_state + (3 - (outSamples % 3))];
       
   274 
       
   275     return outSamples;
       
   276 }
       
   277 
       
   278