shithub: opus

Download patch

ref: 913a1742b960b8cb705132d1ebfcf0718bb994e7
parent: c97b258c628ae6bd0e8e6229a8f398c980dfae77
author: Jean-Marc Valin <[email protected]>
date: Sat Jan 29 05:00:20 EST 2011

Adding resampling support

We use the MDCT as low-pass filter.

--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -63,6 +63,32 @@
 #define COMBFILTER_MAXPERIOD 1024
 #define COMBFILTER_MINPERIOD 15
 
+static int resampling_factor(celt_int32 rate)
+{
+   int ret;
+   switch (rate)
+   {
+   case 48000:
+      ret = 1;
+      break;
+   case 24000:
+      ret = 2;
+      break;
+   case 16000:
+      ret = 3;
+      break;
+   case 12000:
+      ret = 4;
+      break;
+   case 8000:
+      ret = 6;
+      break;
+   default:
+      ret = 0;
+   }
+   return ret;
+}
+
 /** Encoder state 
  @brief Encoder state
  */
@@ -73,6 +99,7 @@
    
    int force_intra;
    int complexity;
+   int upsample;
    int start, end;
 
    celt_int32 vbr_rate_norm; /* Target number of 8th bits per frame */
@@ -127,12 +154,13 @@
    return size;
 }
 
-CELTEncoder *celt_encoder_create(int channels, int *error)
+CELTEncoder *celt_encoder_create(int sampling_rate, int channels, int *error)
 {
    CELTMode *mode = celt_mode_create(48000, 960, NULL);
-   return celt_encoder_init(
+   CELTEncoder *st = celt_encoder_init(
          (CELTEncoder *)celt_alloc(celt_encoder_get_size(mode, channels)),
-         channels, error);
+         sampling_rate, channels, error);
+   return st;
 }
 
 CELTEncoder *celt_encoder_create_custom(const CELTMode *mode, int channels, int *error)
@@ -142,9 +170,17 @@
          mode, channels, error);
 }
 
-CELTEncoder *celt_encoder_init(CELTEncoder *st, int channels, int *error)
+CELTEncoder *celt_encoder_init(CELTEncoder *st, int sampling_rate, int channels, int *error)
 {
-   return celt_encoder_init_custom(st, celt_mode_create(48000, 960, NULL), channels, error);
+   celt_encoder_init_custom(st, celt_mode_create(48000, 960, NULL), channels, error);
+   st->upsample = resampling_factor(sampling_rate);
+   if (st->upsample==0)
+   {
+      if (error)
+         *error = CELT_BAD_ARG;
+      return NULL;
+   }
+   return st;
 }
 
 CELTEncoder *celt_encoder_init_custom(CELTEncoder *st, const CELTMode *mode, int channels, int *error)
@@ -169,6 +205,7 @@
    st->overlap = mode->overlap;
    st->channels = channels;
 
+   st->upsample = 1;
    st->start = 0;
    st->end = st->mode->effEBands;
    st->constrained_vbr = 1;
@@ -385,10 +422,11 @@
    } while (++c<C);
 }
 
-static void deemphasis(celt_sig *in[], celt_word16 *pcm, int N, int _C, const celt_word16 *coef, celt_sig *mem)
+static void deemphasis(celt_sig *in[], celt_word16 *pcm, int N, int _C, int downsample, const celt_word16 *coef, celt_sig *mem)
 {
    const int C = CHANNELS(_C);
    int c;
+   int count=0;
    c=0; do {
       int j;
       celt_sig * restrict x;
@@ -402,9 +440,15 @@
          m = MULT16_32_Q15(coef[0], tmp)
            - MULT16_32_Q15(coef[1], *x);
          tmp = SHL32(MULT16_32_Q15(coef[3], tmp), 2);
-         *y = SCALEOUT(SIG2WORD16(tmp));
          x++;
-         y+=C;
+         /* Technically the store could be moved outside of the if because
+            the stores we don't want will just be overwritten */
+         if (++count==downsample)
+         {
+            *y = SCALEOUT(SIG2WORD16(tmp));
+            y+=C;
+            count=0;
+         }
       }
       mem[c] = m;
    } while (++c<C);
@@ -841,6 +885,7 @@
    if (nbCompressedBytes<2 || pcm==NULL)
      return CELT_BAD_ARG;
 
+   frame_size *= st->upsample;
    for (LM=0;LM<4;LM++)
       if (st->mode->shortMdctSize<<LM==frame_size)
          break;
@@ -919,19 +964,29 @@
 
       silence = 1;
       c=0; do {
+         int count = 0;
          const celt_word16 * restrict pcmp = pcm+c;
          celt_sig * restrict inp = in+c*(N+st->overlap)+st->overlap;
 
          for (i=0;i<N;i++)
          {
+            celt_sig x, tmp;
+
+            x = SCALEIN(*pcmp);
+            if (++count==st->upsample)
+            {
+               count=0;
+               pcmp+=C;
+            } else {
+               x = 0;
+            }
             /* Apply pre-emphasis */
-            celt_sig tmp = MULT16_16(st->mode->preemph[2], SCALEIN(*pcmp));
+            tmp = MULT16_16(st->mode->preemph[2], x);
             *inp = tmp + st->preemph_memE[c];
             st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp)
                                    - MULT16_32_Q15(st->mode->preemph[0], tmp);
             silence = silence && *inp == 0;
             inp++;
-            pcmp+=C;
          }
          CELT_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
          CELT_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N);
@@ -1082,6 +1137,17 @@
    /* Compute MDCTs */
    compute_mdcts(st->mode, shortBlocks, in, freq, C, LM);
 
+   if (st->upsample != 1)
+   {
+      c=0; do
+      {
+         int bound = N/st->upsample;
+         for (i=0;i<bound;i++)
+            freq[c*N+i] *= st->upsample;
+         for (;i<N;i++)
+            freq[c*N+i] = 0;
+      } while (++c<C);
+   }
    ALLOC(X, C*N, celt_norm);         /**< Interleaved normalised MDCTs */
 
    compute_band_energies(st->mode, freq, bandE, effEnd, C, M);
@@ -1412,7 +1478,7 @@
       } while (++c<C);
 #endif /* ENABLE_POSTFILTER */
 
-      deemphasis(out_mem, (celt_word16*)pcm, N, C, st->mode->preemph, st->preemph_memD);
+      deemphasis(out_mem, (celt_word16*)pcm, N, C, st->upsample, st->mode->preemph, st->preemph_memD);
       st->prefilter_period_old = st->prefilter_period;
       st->prefilter_gain_old = st->prefilter_gain;
       st->prefilter_tapset_old = st->prefilter_tapset;
@@ -1651,6 +1717,7 @@
    int overlap;
    int channels;
 
+   int downsample;
    int start, end;
 
    /* Everything beyond this point gets cleared on a reset */
@@ -1685,12 +1752,12 @@
    return size;
 }
 
-CELTDecoder *celt_decoder_create(int channels, int *error)
+CELTDecoder *celt_decoder_create(int sampling_rate, int channels, int *error)
 {
    const CELTMode *mode = celt_mode_create(48000, 960, NULL);
    return celt_decoder_init(
          (CELTDecoder *)celt_alloc(celt_decoder_get_size(mode, channels)),
-         channels, error);
+         sampling_rate, channels, error);
 }
 
 CELTDecoder *celt_decoder_create_custom(const CELTMode *mode, int channels, int *error)
@@ -1700,9 +1767,17 @@
          mode, channels, error);
 }
 
-CELTDecoder *celt_decoder_init(CELTDecoder *st, int channels, int *error)
+CELTDecoder *celt_decoder_init(CELTDecoder *st, int sampling_rate, int channels, int *error)
 {
-   return celt_decoder_init_custom(st, celt_mode_create(48000, 960, NULL), channels, error);
+   celt_decoder_init_custom(st, celt_mode_create(48000, 960, NULL), channels, error);
+   st->downsample = resampling_factor(sampling_rate);
+   if (st->downsample==0)
+   {
+      if (error)
+         *error = CELT_BAD_ARG;
+      return NULL;
+   }
+   return st;
 }
 
 CELTDecoder *celt_decoder_init_custom(CELTDecoder *st, const CELTMode *mode, int channels, int *error)
@@ -1727,6 +1802,7 @@
    st->overlap = mode->overlap;
    st->channels = channels;
 
+   st->downsample = 1;
    st->start = 0;
    st->end = st->mode->effEBands;
 
@@ -1955,7 +2031,7 @@
          out_mem[c][MAX_PERIOD+i] = e[i];
    } while (++c<C);
 
-   deemphasis(out_syn, pcm, N, C, st->mode->preemph, st->preemph_memD);
+   deemphasis(out_syn, pcm, N, C, st->downsample, st->mode->preemph, st->preemph_memD);
    
    st->loss_count++;
 
@@ -2015,6 +2091,7 @@
    if (pcm==NULL)
       return CELT_BAD_ARG;
 
+   frame_size *= st->downsample;
    for (LM=0;LM<4;LM++)
       if (st->mode->shortMdctSize<<LM==frame_size)
          break;
@@ -2220,10 +2297,13 @@
       for (i=0;i<M*st->mode->eBands[st->start];i++)
          freq[c*N+i] = 0;
    while (++c<C);
-   c=0; do
+   c=0; do {
+      int bound = M*st->mode->eBands[effEnd];
+      if (st->downsample!=1)
+         bound = IMIN(bound, N/st->downsample);
       for (i=M*st->mode->eBands[effEnd];i<N;i++)
          freq[c*N+i] = 0;
-   while (++c<C);
+   } while (++c<C);
 
    out_syn[0] = out_mem[0]+MAX_PERIOD-N;
    if (C==2)
@@ -2280,7 +2360,7 @@
    }
    st->rng = dec->rng;
 
-   deemphasis(out_syn, pcm, N, C, st->mode->preemph, st->preemph_memD);
+   deemphasis(out_syn, pcm, N, C, st->downsample, st->mode->preemph, st->preemph_memD);
    st->loss_count = 0;
    RESTORE_STACK;
    if (ec_dec_tell(dec,0) > 8*len || ec_dec_get_error(dec))
--- a/libcelt/celt.h
+++ b/libcelt/celt.h
@@ -167,7 +167,7 @@
  @param error Returns an error code
  @return Newly created encoder state.
 */
-EXPORT CELTEncoder *celt_encoder_create(int channels, int *error);
+EXPORT CELTEncoder *celt_encoder_create(int sampling_rate, int channels, int *error);
 
 /** Creates a new encoder state. Each stream needs its own encoder
     state (can't be shared across simultaneous streams).
@@ -180,7 +180,7 @@
 */
 EXPORT CELTEncoder *celt_encoder_create_custom(const CELTMode *mode, int channels, int *error);
 
-EXPORT CELTEncoder *celt_encoder_init(CELTEncoder *st, int channels, int *error);
+EXPORT CELTEncoder *celt_encoder_init(CELTEncoder *st, int sampling_rate, int channels, int *error);
 
 EXPORT CELTEncoder *celt_encoder_init_custom(CELTEncoder *st, const CELTMode *mode, int channels, int *error);
 
@@ -245,7 +245,7 @@
  @param error Returns an error code
  @return Newly created decoder state.
  */
-EXPORT CELTDecoder *celt_decoder_create(int channels, int *error);
+EXPORT CELTDecoder *celt_decoder_create(int sampling_rate, int channels, int *error);
 
 /** Creates a new decoder state. Each stream needs its own decoder state (can't
     be shared across simultaneous streams).
@@ -257,7 +257,7 @@
  */
 EXPORT CELTDecoder *celt_decoder_create_custom(const CELTMode *mode, int channels, int *error);
 
-EXPORT CELTDecoder *celt_decoder_init(CELTDecoder *st, int channels, int *error);
+EXPORT CELTDecoder *celt_decoder_init(CELTDecoder *st, int sampling_rate, int channels, int *error);
 
 EXPORT CELTDecoder *celt_decoder_init_custom(CELTDecoder *st, const CELTMode *mode, int channels, int *error);