Home | History | Annotate | Download | only in source
      1 /*
      2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 /*
     12  * WebRtcIsacfix_kTransform.c
     13  *
     14  * Transform functions
     15  *
     16  */
     17 
     18 #include "webrtc/modules/audio_coding/codecs/isac/fix/source/codec.h"
     19 #include "webrtc/modules/audio_coding/codecs/isac/fix/source/fft.h"
     20 #include "webrtc/modules/audio_coding/codecs/isac/fix/source/settings.h"
     21 
     22 /* Tables are defined in transform_tables.c file or ARM assembly files. */
     23 /* Cosine table 1 in Q14 */
     24 extern const int16_t WebRtcIsacfix_kCosTab1[FRAMESAMPLES/2];
     25 /* Sine table 1 in Q14 */
     26 extern const int16_t WebRtcIsacfix_kSinTab1[FRAMESAMPLES/2];
     27 /* Sine table 2 in Q14 */
     28 extern const int16_t WebRtcIsacfix_kSinTab2[FRAMESAMPLES/4];
     29 
     30 void WebRtcIsacfix_Time2SpecC(int16_t *inre1Q9,
     31                               int16_t *inre2Q9,
     32                               int16_t *outreQ7,
     33                               int16_t *outimQ7)
     34 {
     35 
     36   int k;
     37   int32_t tmpreQ16[FRAMESAMPLES/2], tmpimQ16[FRAMESAMPLES/2];
     38   int16_t tmp1rQ14, tmp1iQ14;
     39   int32_t xrQ16, xiQ16, yrQ16, yiQ16;
     40   int32_t v1Q16, v2Q16;
     41   int16_t factQ19, sh;
     42 
     43   /* Multiply with complex exponentials and combine into one complex vector */
     44   factQ19 = 16921; // 0.5/sqrt(240) in Q19 is round(.5/sqrt(240)*(2^19)) = 16921
     45   for (k = 0; k < FRAMESAMPLES/2; k++) {
     46     tmp1rQ14 = WebRtcIsacfix_kCosTab1[k];
     47     tmp1iQ14 = WebRtcIsacfix_kSinTab1[k];
     48     xrQ16 = WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL_16_16(tmp1rQ14, inre1Q9[k]) + WEBRTC_SPL_MUL_16_16(tmp1iQ14, inre2Q9[k]), 7);
     49     xiQ16 = WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL_16_16(tmp1rQ14, inre2Q9[k]) - WEBRTC_SPL_MUL_16_16(tmp1iQ14, inre1Q9[k]), 7);
     50     tmpreQ16[k] = WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL_16_32_RSFT16(factQ19, xrQ16)+4, 3); // (Q16*Q19>>16)>>3 = Q16
     51     tmpimQ16[k] = WEBRTC_SPL_RSHIFT_W32(WEBRTC_SPL_MUL_16_32_RSFT16(factQ19, xiQ16)+4, 3); // (Q16*Q19>>16)>>3 = Q16
     52   }
     53 
     54 
     55   xrQ16  = WebRtcSpl_MaxAbsValueW32(tmpreQ16, FRAMESAMPLES/2);
     56   yrQ16 = WebRtcSpl_MaxAbsValueW32(tmpimQ16, FRAMESAMPLES/2);
     57   if (yrQ16>xrQ16) {
     58     xrQ16 = yrQ16;
     59   }
     60 
     61   sh = WebRtcSpl_NormW32(xrQ16);
     62   sh = sh-24; //if sh becomes >=0, then we should shift sh steps to the left, and the domain will become Q(16+sh)
     63   //if sh becomes <0, then we should shift -sh steps to the right, and the domain will become Q(16+sh)
     64 
     65   //"Fastest" vectors
     66   if (sh>=0) {
     67     for (k=0; k<FRAMESAMPLES/2; k++) {
     68       inre1Q9[k] = (int16_t) WEBRTC_SPL_LSHIFT_W32(tmpreQ16[k], sh); //Q(16+sh)
     69       inre2Q9[k] = (int16_t) WEBRTC_SPL_LSHIFT_W32(tmpimQ16[k], sh); //Q(16+sh)
     70     }
     71   } else {
     72     int32_t round = WEBRTC_SPL_LSHIFT_W32((int32_t)1, -sh-1);
     73     for (k=0; k<FRAMESAMPLES/2; k++) {
     74       inre1Q9[k] = (int16_t) WEBRTC_SPL_RSHIFT_W32(tmpreQ16[k]+round, -sh); //Q(16+sh)
     75       inre2Q9[k] = (int16_t) WEBRTC_SPL_RSHIFT_W32(tmpimQ16[k]+round, -sh); //Q(16+sh)
     76     }
     77   }
     78 
     79   /* Get DFT */
     80   WebRtcIsacfix_FftRadix16Fastest(inre1Q9, inre2Q9, -1); // real call
     81 
     82   //"Fastest" vectors
     83   if (sh>=0) {
     84     for (k=0; k<FRAMESAMPLES/2; k++) {
     85       tmpreQ16[k] = WEBRTC_SPL_RSHIFT_W32((int32_t)inre1Q9[k], sh); //Q(16+sh) -> Q16
     86       tmpimQ16[k] = WEBRTC_SPL_RSHIFT_W32((int32_t)inre2Q9[k], sh); //Q(16+sh) -> Q16
     87     }
     88   } else {
     89     for (k=0; k<FRAMESAMPLES/2; k++) {
     90       tmpreQ16[k] = WEBRTC_SPL_LSHIFT_W32((int32_t)inre1Q9[k], -sh); //Q(16+sh) -> Q16
     91       tmpimQ16[k] = WEBRTC_SPL_LSHIFT_W32((int32_t)inre2Q9[k], -sh); //Q(16+sh) -> Q16
     92     }
     93   }
     94 
     95 
     96   /* Use symmetry to separate into two complex vectors and center frames in time around zero */
     97   for (k = 0; k < FRAMESAMPLES/4; k++) {
     98     xrQ16 = tmpreQ16[k] + tmpreQ16[FRAMESAMPLES/2 - 1 - k];
     99     yiQ16 = -tmpreQ16[k] + tmpreQ16[FRAMESAMPLES/2 - 1 - k];
    100     xiQ16 = tmpimQ16[k] - tmpimQ16[FRAMESAMPLES/2 - 1 - k];
    101     yrQ16 = tmpimQ16[k] + tmpimQ16[FRAMESAMPLES/2 - 1 - k];
    102     tmp1rQ14 = -WebRtcIsacfix_kSinTab2[FRAMESAMPLES/4 - 1 - k];
    103     tmp1iQ14 = WebRtcIsacfix_kSinTab2[k];
    104     v1Q16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, xrQ16) - WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, xiQ16);
    105     v2Q16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, xrQ16) + WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, xiQ16);
    106     outreQ7[k] = (int16_t) WEBRTC_SPL_RSHIFT_W32(v1Q16, 9);
    107     outimQ7[k] = (int16_t) WEBRTC_SPL_RSHIFT_W32(v2Q16, 9);
    108     v1Q16 = -WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, yrQ16) - WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, yiQ16);
    109     v2Q16 = -WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, yrQ16) + WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, yiQ16);
    110     outreQ7[FRAMESAMPLES/2 - 1 - k] = (int16_t)WEBRTC_SPL_RSHIFT_W32(v1Q16, 9); //CalcLrIntQ(v1Q16, 9);
    111     outimQ7[FRAMESAMPLES/2 - 1 - k] = (int16_t)WEBRTC_SPL_RSHIFT_W32(v2Q16, 9); //CalcLrIntQ(v2Q16, 9);
    112 
    113   }
    114 }
    115 
    116 
    117 void WebRtcIsacfix_Spec2TimeC(int16_t *inreQ7, int16_t *inimQ7, int32_t *outre1Q16, int32_t *outre2Q16)
    118 {
    119 
    120   int k;
    121   int16_t tmp1rQ14, tmp1iQ14;
    122   int32_t xrQ16, xiQ16, yrQ16, yiQ16;
    123   int32_t tmpInRe, tmpInIm, tmpInRe2, tmpInIm2;
    124   int16_t factQ11;
    125   int16_t sh;
    126 
    127   for (k = 0; k < FRAMESAMPLES/4; k++) {
    128     /* Move zero in time to beginning of frames */
    129     tmp1rQ14 = -WebRtcIsacfix_kSinTab2[FRAMESAMPLES/4 - 1 - k];
    130     tmp1iQ14 = WebRtcIsacfix_kSinTab2[k];
    131 
    132     tmpInRe = WEBRTC_SPL_LSHIFT_W32((int32_t) inreQ7[k], 9);  // Q7 -> Q16
    133     tmpInIm = WEBRTC_SPL_LSHIFT_W32((int32_t) inimQ7[k], 9);  // Q7 -> Q16
    134     tmpInRe2 = WEBRTC_SPL_LSHIFT_W32((int32_t) inreQ7[FRAMESAMPLES/2 - 1 - k], 9);  // Q7 -> Q16
    135     tmpInIm2 = WEBRTC_SPL_LSHIFT_W32((int32_t) inimQ7[FRAMESAMPLES/2 - 1 - k], 9);  // Q7 -> Q16
    136 
    137     xrQ16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, tmpInRe) + WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, tmpInIm);
    138     xiQ16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, tmpInIm) - WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, tmpInRe);
    139     yrQ16 = -WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, tmpInIm2) - WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, tmpInRe2);
    140     yiQ16 = -WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, tmpInRe2) + WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, tmpInIm2);
    141 
    142     /* Combine into one vector,  z = x + j * y */
    143     outre1Q16[k] = xrQ16 - yiQ16;
    144     outre1Q16[FRAMESAMPLES/2 - 1 - k] = xrQ16 + yiQ16;
    145     outre2Q16[k] = xiQ16 + yrQ16;
    146     outre2Q16[FRAMESAMPLES/2 - 1 - k] = -xiQ16 + yrQ16;
    147   }
    148 
    149   /* Get IDFT */
    150   tmpInRe  = WebRtcSpl_MaxAbsValueW32(outre1Q16, 240);
    151   tmpInIm = WebRtcSpl_MaxAbsValueW32(outre2Q16, 240);
    152   if (tmpInIm>tmpInRe) {
    153     tmpInRe = tmpInIm;
    154   }
    155 
    156   sh = WebRtcSpl_NormW32(tmpInRe);
    157   sh = sh-24; //if sh becomes >=0, then we should shift sh steps to the left, and the domain will become Q(16+sh)
    158   //if sh becomes <0, then we should shift -sh steps to the right, and the domain will become Q(16+sh)
    159 
    160   //"Fastest" vectors
    161   if (sh>=0) {
    162     for (k=0; k<240; k++) {
    163       inreQ7[k] = (int16_t) WEBRTC_SPL_LSHIFT_W32(outre1Q16[k], sh); //Q(16+sh)
    164       inimQ7[k] = (int16_t) WEBRTC_SPL_LSHIFT_W32(outre2Q16[k], sh); //Q(16+sh)
    165     }
    166   } else {
    167     int32_t round = WEBRTC_SPL_LSHIFT_W32((int32_t)1, -sh-1);
    168     for (k=0; k<240; k++) {
    169       inreQ7[k] = (int16_t) WEBRTC_SPL_RSHIFT_W32(outre1Q16[k]+round, -sh); //Q(16+sh)
    170       inimQ7[k] = (int16_t) WEBRTC_SPL_RSHIFT_W32(outre2Q16[k]+round, -sh); //Q(16+sh)
    171     }
    172   }
    173 
    174   WebRtcIsacfix_FftRadix16Fastest(inreQ7, inimQ7, 1); // real call
    175 
    176   //"Fastest" vectors
    177   if (sh>=0) {
    178     for (k=0; k<240; k++) {
    179       outre1Q16[k] = WEBRTC_SPL_RSHIFT_W32((int32_t)inreQ7[k], sh); //Q(16+sh) -> Q16
    180       outre2Q16[k] = WEBRTC_SPL_RSHIFT_W32((int32_t)inimQ7[k], sh); //Q(16+sh) -> Q16
    181     }
    182   } else {
    183     for (k=0; k<240; k++) {
    184       outre1Q16[k] = WEBRTC_SPL_LSHIFT_W32((int32_t)inreQ7[k], -sh); //Q(16+sh) -> Q16
    185       outre2Q16[k] = WEBRTC_SPL_LSHIFT_W32((int32_t)inimQ7[k], -sh); //Q(16+sh) -> Q16
    186     }
    187   }
    188 
    189   /* Divide through by the normalizing constant: */
    190   /* scale all values with 1/240, i.e. with 273 in Q16 */
    191   /* 273/65536 ~= 0.0041656                            */
    192   /*     1/240 ~= 0.0041666                            */
    193   for (k=0; k<240; k++) {
    194     outre1Q16[k] = WEBRTC_SPL_MUL_16_32_RSFT16(273, outre1Q16[k]);
    195     outre2Q16[k] = WEBRTC_SPL_MUL_16_32_RSFT16(273, outre2Q16[k]);
    196   }
    197 
    198   /* Demodulate and separate */
    199   factQ11 = 31727; // sqrt(240) in Q11 is round(15.49193338482967*2048) = 31727
    200   for (k = 0; k < FRAMESAMPLES/2; k++) {
    201     tmp1rQ14 = WebRtcIsacfix_kCosTab1[k];
    202     tmp1iQ14 = WebRtcIsacfix_kSinTab1[k];
    203     xrQ16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, outre1Q16[k]) - WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, outre2Q16[k]);
    204     xiQ16 = WEBRTC_SPL_MUL_16_32_RSFT14(tmp1rQ14, outre2Q16[k]) + WEBRTC_SPL_MUL_16_32_RSFT14(tmp1iQ14, outre1Q16[k]);
    205     xrQ16 = WEBRTC_SPL_MUL_16_32_RSFT11(factQ11, xrQ16);
    206     xiQ16 = WEBRTC_SPL_MUL_16_32_RSFT11(factQ11, xiQ16);
    207     outre2Q16[k] = xiQ16;
    208     outre1Q16[k] = xrQ16;
    209   }
    210 }
    211