shithub: opus

Download patch

ref: 46014ca49a91e345cd4d3a9d70a67a1cbe73c2ac
parent: 36d78e6f74a85cda823083d6b07235d474bda527
author: Jean-Marc Valin <[email protected]>
date: Fri Dec 14 08:47:04 EST 2007

Propagating perceptual weighting around (not used yet).

--- a/libcelt/bands.c
+++ b/libcelt/bands.c
@@ -214,7 +214,7 @@
       P[i] = 0;
 }
 
-void quant_bands(const CELTMode *m, float *X, float *P, ec_enc *enc)
+void quant_bands(const CELTMode *m, float *X, float *P, float *W, ec_enc *enc)
 {
    int i, j, B;
    const int *eBands = m->eBands;
@@ -227,7 +227,7 @@
       q = m->nbPulses[i];
       if (q>0) {
          float n = sqrt(B*(eBands[i+1]-eBands[i]));
-         alg_quant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, P+B*eBands[i], 0.7, enc);
+         alg_quant(X+B*eBands[i], W+B*eBands[i], B*(eBands[i+1]-eBands[i]), q, P+B*eBands[i], 0.7, enc);
          for (j=B*eBands[i];j<B*eBands[i+1];j++)
             norm[j] = X[j] * n;
          //printf ("%f ", log2(ncwrs64(B*(eBands[i+1]-eBands[i]), q))/(B*(eBands[i+1]-eBands[i])));
@@ -234,7 +234,7 @@
          //printf ("%f ", log2(ncwrs64(B*(eBands[i+1]-eBands[i]), q)));
       } else {
          float n = sqrt(B*(eBands[i+1]-eBands[i]));
-         copy_quant(X+B*eBands[i], B*(eBands[i+1]-eBands[i]), -q, norm, B, eBands[i], enc);
+         copy_quant(X+B*eBands[i], W+B*eBands[i], B*(eBands[i+1]-eBands[i]), -q, norm, B, eBands[i], enc);
          for (j=B*eBands[i];j<B*eBands[i+1];j++)
             norm[j] = X[j] * n;
          //printf ("%f ", (1+log2(eBands[i]-(eBands[i+1]-eBands[i]))+log2(ncwrs64(B*(eBands[i+1]-eBands[i]), -q)))/(B*(eBands[i+1]-eBands[i])));
--- a/libcelt/bands.h
+++ b/libcelt/bands.h
@@ -47,7 +47,7 @@
 
 void pitch_quant_bands(const CELTMode *m, float *X, float *P, float *gains);
 
-void quant_bands(const CELTMode *m, float *X, float *P, ec_enc *enc);
+void quant_bands(const CELTMode *m, float *X, float *P, float *W, ec_enc *enc);
 
 void unquant_bands(const CELTMode *m, float *X, float *P, ec_dec *dec);
 
--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -40,6 +40,7 @@
 #include "probenc.h"
 #include "quant_pitch.h"
 #include "quant_bands.h"
+#include "psy.h"
 
 #define MAX_PERIOD 1024
 
@@ -50,6 +51,7 @@
    int block_size;
    int nb_blocks;
    int channels;
+   int Fs;
    
    ec_byte_buffer buf;
    ec_enc         enc;
@@ -83,6 +85,7 @@
    st->frame_size = B*N;
    st->block_size = N;
    st->nb_blocks  = B;
+   st->Fs = 44100;
    
    ec_byte_writeinit(&st->buf);
    ec_enc_init(&st->enc,&st->buf);
@@ -207,6 +210,7 @@
    
    float X[B*C*N];         /**< Interleaved signal MDCTs */
    float P[B*C*N];         /**< Interleaved pitch MDCTs*/
+   float mask[B*C*N];      /**< Masking curve */
    float bandE[st->mode->nbEBands];
    float gains[st->mode->nbPBands];
    int pitch_index;
@@ -228,6 +232,11 @@
    /* Compute MDCTs */
    compute_mdcts(&st->mdct_lookup, st->window, in, X, N, B, C);
    
+   compute_masking(X, mask, B*C*N, st->Fs);
+   /* Invert and stretch the mask to length of X */
+   for (i=B*C*N-1;i>=0;i--)
+      mask[i] = 1/(1+mask[i>>1]);
+   
    /* Pitch analysis */
    for (c=0;c<C;c++)
    {
@@ -286,7 +295,7 @@
       sum += X[i]*X[i];
    printf ("%f\n", sum);*/
    /* Residual quantisation */
-   quant_bands(st->mode, X, P, &st->enc);
+   quant_bands(st->mode, X, P, mask, &st->enc);
    
    if (0) {//This is just for debugging
       ec_enc_done(&st->enc);
--- a/libcelt/testcelt.c
+++ b/libcelt/testcelt.c
@@ -52,8 +52,8 @@
    outFile = argv[2];
    fout = fopen(outFile, "wb+");
    
-   enc = celt_encoder_new(celt_mode2);
-   dec = celt_decoder_new(celt_mode2);
+   enc = celt_encoder_new(celt_mode1);
+   dec = celt_decoder_new(celt_mode1);
    
    while (!feof(fin))
    {
--- a/libcelt/vq.c
+++ b/libcelt/vq.c
@@ -38,7 +38,7 @@
 /* Improved algebraic pulse-base quantiser. The signal x is replaced by the sum of the pitch 
    a combination of pulses such that its norm is still equal to 1. The only difference with 
    the quantiser above is that the search is more complete. */
-void alg_quant(float *x, int N, int K, float *p, float alpha, ec_enc *enc)
+void alg_quant(float *x, float *W, int N, int K, float *p, float alpha, ec_enc *enc)
 {
    int L = 3;
    //float tata[200];
@@ -223,7 +223,7 @@
 static const float pg[5] = {1.f, .6f, .45f, 0.35f, 0.25f};
 
 /* Finds the right offset into Y and copy it */
-void copy_quant(float *x, int N, int K, float *Y, int B, int N0, ec_enc *enc)
+void copy_quant(float *x, float *W, int N, int K, float *Y, int B, int N0, ec_enc *enc)
 {
    int i,j;
    int best=0;
@@ -287,7 +287,7 @@
       E = .8/sqrt(E);
       for (j=0;j<N;j++)
          P[j] *= E;
-      alg_quant(x, N, K, P, 0, enc);
+      alg_quant(x, W, N, K, P, 0, enc);
    }
 }
 
--- a/libcelt/vq.h
+++ b/libcelt/vq.h
@@ -39,12 +39,12 @@
 /* Algebraic pulse-base quantiser. The signal x is replaced by the sum of the pitch 
    a combination of pulses such that its norm is still equal to 1. The only difference with 
    the quantiser above is that the search is more complete. */
-void alg_quant(float *x, int N, int K, float *p, float alpha, ec_enc *enc);
+void alg_quant(float *x, float *W, int N, int K, float *p, float alpha, ec_enc *enc);
 
 void alg_unquant(float *x, int N, int K, float *p, float alpha, ec_dec *dec);
 
 /* Finds the right offset into Y and copy it */
-void copy_quant(float *x, int N, int K, float *Y, int B, int N0, ec_enc *enc);
+void copy_quant(float *x, float *W, int N, int K, float *Y, int B, int N0, ec_enc *enc);
 
 void copy_unquant(float *x, int N, int K, float *Y, int B, int N0, ec_dec *dec);
 #endif /* VQ_H */