videoeditorengine/audioeditorengine/resampler/src/resampler_sinc_conv_three_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_three_to_one_int16.h"
       
    21 #include "resampler_clip.h"
       
    22 #include "resampler_sinc_conv_three_to_one_tables_standard.h"
       
    23 #include "resampler_sinc_conv_filter_three_to_one_int16.h"
       
    24 
       
    25 
       
    26 #include <string.h>
       
    27 
       
    28 /*
       
    29  * The amount of zero crossings in positive or negative
       
    30  * side of the sinc function. Because of filter symmetry
       
    31  * the amount of filter taps used in convolution is
       
    32  * zero crossings * 2.
       
    33  */
       
    34 static const int LC_MAX_COEFF_COUNT = RESAMPLER_THREE_TO_ONE_COEFF_COUNT_STANDARD;
       
    35 
       
    36 static const int LC_BUFFER_ACCESS_OUTPUT = LC_MAX_COEFF_COUNT - 1;
       
    37 
       
    38 
       
    39 RESAMPLER_SincConvThreeToOneInt16::RESAMPLER_SincConvThreeToOneInt16(int channelCount) :
       
    40 m_memBuffers(0),
       
    41 m_scratchBuffer(0),
       
    42 m_channelCount(channelCount),
       
    43 m_blockSize(0),
       
    44 m_channelEnabled(0),
       
    45 m_state(0),
       
    46 m_coeffCount(RESAMPLER_THREE_TO_ONE_COEFF_COUNT_STANDARD),
       
    47 m_filterMatrix(RESAMPLER_THREE_TO_ONE_FILTERS_STANDARD)
       
    48 {
       
    49 }
       
    50 
       
    51 
       
    52 RESAMPLER_SincConvThreeToOneInt16::~RESAMPLER_SincConvThreeToOneInt16()
       
    53 {
       
    54     DeInit();
       
    55 }
       
    56 
       
    57 bool RESAMPLER_SincConvThreeToOneInt16::InitInputDriven()
       
    58 {
       
    59     return Init();
       
    60 }
       
    61 
       
    62 bool RESAMPLER_SincConvThreeToOneInt16::InitOutputDriven()
       
    63 {
       
    64     return Init();
       
    65 }
       
    66 
       
    67 bool 
       
    68 RESAMPLER_SincConvThreeToOneInt16::Init()
       
    69 {
       
    70     int i(0);
       
    71 
       
    72     m_memBuffers = new int16 *[m_channelCount];
       
    73     if (!m_memBuffers)
       
    74     {
       
    75         return false;
       
    76     }
       
    77 
       
    78     for (i = 0; i < m_channelCount; i++) 
       
    79     {
       
    80         m_memBuffers[i] = 0;
       
    81     }
       
    82 
       
    83     m_channelEnabled = new bool[m_channelCount];
       
    84     if (!m_channelEnabled)
       
    85     {
       
    86         DeInit();
       
    87         return false;
       
    88     }
       
    89     for (i = 0; i < m_channelCount; i++) 
       
    90     {
       
    91         m_channelEnabled[i] = true;
       
    92     }
       
    93 
       
    94     for (i = 0; i < m_channelCount; i++) 
       
    95     {
       
    96         m_memBuffers[i] = new int16[LC_MAX_COEFF_COUNT * 2 - 1];
       
    97         if (!m_memBuffers[i])
       
    98         {
       
    99             DeInit();
       
   100             return false;
       
   101         }
       
   102         memset(m_memBuffers[i], 0, sizeof(int16) * (LC_MAX_COEFF_COUNT * 2 - 1));
       
   103     }
       
   104 
       
   105     return true;
       
   106 }
       
   107 
       
   108 void 
       
   109 RESAMPLER_SincConvThreeToOneInt16::DeInit()
       
   110 {
       
   111     if (m_channelCount)
       
   112     {
       
   113         for (int i = 0; i < m_channelCount; i++)
       
   114         {
       
   115             delete [] m_memBuffers[i];
       
   116         }
       
   117         delete [] m_memBuffers;
       
   118         delete [] m_channelEnabled;
       
   119     }
       
   120 }
       
   121 
       
   122 
       
   123 void 
       
   124 RESAMPLER_SincConvThreeToOneInt16::EnableChannel(int channel)
       
   125 {
       
   126     m_channelEnabled[channel] = true;
       
   127 }
       
   128 
       
   129 void 
       
   130 RESAMPLER_SincConvThreeToOneInt16::DisableChannel(int channel)
       
   131 {
       
   132     m_channelEnabled[channel] = false;
       
   133 }
       
   134 
       
   135 size_t 
       
   136 RESAMPLER_SincConvThreeToOneInt16::ScratchMemoryNeedInputDriven(int maxInputBlockSize) const
       
   137 {
       
   138     return sizeof(int16) * ((LC_MAX_COEFF_COUNT * 2 - 1) + maxInputBlockSize);
       
   139 }
       
   140 
       
   141 void 
       
   142 RESAMPLER_SincConvThreeToOneInt16::SetScratchBufferInputDriven(char *buffer)
       
   143 {
       
   144     m_scratchBuffer = (int16 *)buffer;
       
   145 }
       
   146 
       
   147 size_t 
       
   148 RESAMPLER_SincConvThreeToOneInt16::ScratchMemoryNeedOutputDriven(int maxOutputBlockSize) const
       
   149 {
       
   150     int blockSize = 3 * maxOutputBlockSize; 
       
   151     
       
   152     return ScratchMemoryNeedInputDriven(blockSize);
       
   153 }
       
   154 
       
   155 void 
       
   156 RESAMPLER_SincConvThreeToOneInt16::SetScratchBufferOutputDriven(char *buffer)
       
   157 {
       
   158     m_scratchBuffer = (int16 *)buffer;
       
   159 }
       
   160 
       
   161 
       
   162 int 
       
   163 RESAMPLER_SincConvThreeToOneInt16::MaxOutputSampleCount(int inSamples) const 
       
   164 { 
       
   165     return ( inSamples + 2 ) / 3; 
       
   166 }
       
   167 
       
   168 int 
       
   169 RESAMPLER_SincConvThreeToOneInt16::MaxInputSampleCount(int outSamples) const
       
   170 { 
       
   171     return 3 * outSamples; 
       
   172 }
       
   173 
       
   174 
       
   175 int 
       
   176 RESAMPLER_SincConvThreeToOneInt16::InSamplesNeeded(int outSamples)
       
   177 {
       
   178     return 3 * outSamples;
       
   179 }
       
   180 
       
   181 
       
   182 int 
       
   183 RESAMPLER_SincConvThreeToOneInt16::ProcessToOutput(int16 *outputBuffers[], 
       
   184                                              int16 *inputBuffers[], 
       
   185                                              int outSamples)
       
   186 {
       
   187     int inSamples( 3 * outSamples );
       
   188     
       
   189     return ProcessFromInput(outputBuffers, inputBuffers, inSamples);
       
   190 }
       
   191 
       
   192 int 
       
   193 RESAMPLER_SincConvThreeToOneInt16::ProcessFromInput(int16 *outputBuffers[], 
       
   194                                               int16 *inputBuffers[], 
       
   195                                               int inSamples)
       
   196 {
       
   197     static const int FILTER_LENGTH = LC_MAX_COEFF_COUNT * 2 - 1;
       
   198 	int i, j;
       
   199     int state = m_state;
       
   200 	int outSamples = (inSamples + state) / 3;
       
   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_SincConvFilterThreeToOneInt16(tempBuf,
       
   228 									              m_filterMatrix,
       
   229 												  m_coeffCount);
       
   230             tempBuf += 3;
       
   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) % 3;
       
   243 
       
   244 	return outSamples;
       
   245 }
       
   246