Pitch now quantised at the band level, got rid of all the VQ code.

This commit is contained in:
Jean-Marc Valin 2009-01-13 23:04:12 -05:00
parent 77c80ce524
commit aca0be91f4
7 changed files with 37 additions and 314 deletions

View file

@ -16,7 +16,7 @@ lib_LTLIBRARIES = libcelt.la
# Sources for compilation in the library
libcelt_la_SOURCES = bands.c celt.c cwrs.c ecintrin.h entcode.c \
entdec.c entenc.c header.c kfft_single.c kiss_fft.c kiss_fftr.c laplace.c mdct.c \
modes.c pitch.c psy.c quant_bands.c quant_pitch.c rangedec.c rangeenc.c rate.c \
modes.c pitch.c psy.c quant_bands.c rangedec.c rangeenc.c rate.c \
vq.c
#noinst_HEADERS =
@ -26,8 +26,8 @@ libcelt_la_LDFLAGS = -version-info @CELT_LT_CURRENT@:@CELT_LT_REVISION@:@CELT_LT
noinst_HEADERS = _kiss_fft_guts.h arch.h bands.h fixed_c5x.h fixed_c6x.h \
cwrs.h ecintrin.h entcode.h entdec.h entenc.h fixed_generic.h float_cast.h \
kfft_double.h kfft_single.h kiss_fft.h kiss_fftr.h laplace.h mdct.h mfrngcod.h \
mathops.h modes.h os_support.h pgain_table.h pitch.h psy.h \
quant_bands.h quant_pitch.h rate.h stack_alloc.h vq.h
mathops.h modes.h os_support.h pitch.h psy.h \
quant_bands.h rate.h stack_alloc.h vq.h
noinst_PROGRAMS = testcelt dump_modes
testcelt_SOURCES = testcelt.c

View file

@ -210,9 +210,10 @@ void denormalise_bands(const CELTMode *m, const celt_norm_t * restrict X, celt_s
/* Compute the best gain for each "pitch band" */
void compute_pitch_gain(const CELTMode *m, const celt_norm_t *X, const celt_norm_t *P, celt_pgain_t *gains)
int compute_pitch_gain(const CELTMode *m, const celt_norm_t *X, const celt_norm_t *P, celt_pgain_t *gains)
{
int i;
int gain_sum = 0;
const celt_int16_t *pBands = m->pBands;
const int C = CHANNELS(m);
@ -234,9 +235,11 @@ void compute_pitch_gain(const CELTMode *m, const celt_norm_t *X, const celt_norm
Sxy = Sxx;
/* We need to be a bit conservative (multiply gain by 0.9), otherwise the
residual doesn't quantise well */
Sxy = MULT16_32_Q15(QCONST16(.9f, 15), Sxy);
Sxy = MULT16_32_Q15(QCONST16(.99f, 15), Sxy);
/* gain = Sxy/Sxx */
gains[i] = EXTRACT16(celt_div(Sxy,ADD32(SHR32(Sxx, PGAIN_SHIFT),EPSILON)));
if (gains[i]>QCONST16(.5,15))
gain_sum++;
/*printf ("%f ", 1-sqrt(1-gain*gain));*/
}
/*if(rand()%10==0)
@ -245,6 +248,7 @@ void compute_pitch_gain(const CELTMode *m, const celt_norm_t *X, const celt_norm
printf ("%f ", 1-sqrt(1-gains[i]*gains[i]));
printf ("\n");
}*/
return gain_sum > 5;
}
static void intensity_band(celt_norm_t * restrict X, int len)
@ -402,7 +406,17 @@ void quant_bands(const CELTMode *m, celt_norm_t * restrict X, celt_norm_t *P, ce
} else if (pitch_used && eBands[i] < m->pitchEnd)
{
if (eBands[i] == pBands[pband+1])
{
int enabled = 0;
pband++;
if (pgains[pband] > QCONST16(.5,15))
enabled = 1;
ec_enc_bits(enc, enabled, 1);
if (enabled)
pgains[pband] = QCONST16(.9,15);
else
pgains[pband] = 0;
}
for (j=C*eBands[i];j<C*eBands[i+1];j++)
P[j] = MULT16_16_Q15(pgains[pband], P[j]);
} else {
@ -496,7 +510,15 @@ void unquant_bands(const CELTMode *m, celt_norm_t * restrict X, celt_norm_t *P,
} else if (pitch_used && eBands[i] < m->pitchEnd)
{
if (eBands[i] == pBands[pband+1])
{
int enabled = 0;
pband++;
enabled = ec_dec_bits(dec, 1);
if (enabled)
pgains[pband] = QCONST16(.9,15);
else
pgains[pband] = 0;
}
for (j=C*eBands[i];j<C*eBands[i+1];j++)
P[j] = MULT16_16_Q15(pgains[pband], P[j]);
} else {

View file

@ -76,7 +76,7 @@ void denormalise_bands(const CELTMode *m, const celt_norm_t * restrict X, celt_s
* @param gains Gain computed for each pitch band (returned)
* @param bank Square root of the energy for each band
*/
void compute_pitch_gain(const CELTMode *m, const celt_norm_t *X, const celt_norm_t *P, celt_pgain_t *gains);
int compute_pitch_gain(const CELTMode *m, const celt_norm_t *X, const celt_norm_t *P, celt_pgain_t *gains);
/** Quantisation/encoding of the residual spectrum
* @param m Mode data

View file

@ -44,7 +44,6 @@
#include "bands.h"
#include "modes.h"
#include "entcode.h"
#include "quant_pitch.h"
#include "quant_bands.h"
#include "psy.h"
#include "rate.h"
@ -440,8 +439,8 @@ int celt_encode_float(CELTEncoder * restrict st, const celt_sig_t * pcm, celt_si
#ifndef FIXED_POINT
float gain_1;
#endif
ec_enc_bits(&enc, 0, 1); //Pitch off
ec_enc_bits(&enc, 1, 1); //Transient on
ec_enc_bits(&enc, 0, 1); /*Pitch off */
ec_enc_bits(&enc, 1, 1); /*Transient on */
ec_enc_bits(&enc, transient_shift, 2);
if (transient_shift)
ec_enc_uint(&enc, transient_time, N+st->overlap);
@ -557,23 +556,19 @@ int celt_encode_float(CELTEncoder * restrict st, const celt_sig_t * pcm, celt_si
pitch_power = bandEp[0]+bandEp[1]+bandEp[2];
/* Check if we can safely use the pitch (i.e. effective gain isn't too high) */
curr_power = bandE[0]+bandE[1]+bandE[2];
id=-1;
if ((MULT16_32_Q15(QCONST16(.1f, 15),curr_power) + QCONST32(10.f,ENER_SHIFT) < pitch_power))
{
/* Pitch prediction */
compute_pitch_gain(st->mode, X, P, gains);
id = quant_pitch(gains, st->mode->nbPBands);
}
if (id == -1)
has_pitch = compute_pitch_gain(st->mode, X, P, gains);
} else {
has_pitch = 0;
}
}
if (has_pitch)
{
unquant_pitch(id, gains, st->mode->nbPBands);
ec_enc_bits(&enc, has_pitch, 1); /* Pitch flag */
ec_enc_bits(&enc, has_fold, 1); /* Folding flag */
ec_enc_bits(&enc, id, 7);
ec_enc_uint(&enc, pitch_index, MAX_PERIOD-(2*N-2*N4));
} else {
if (!shortBlocks)
@ -618,6 +613,8 @@ int celt_encode_float(CELTEncoder * restrict st, const celt_sig_t * pcm, celt_si
for (i=0;i<st->mode->nbEBands;i++)
offsets[i] = 0;
bits = nbCompressedBytes*8 - ec_enc_tell(&enc, 0) - 1;
if (has_pitch)
bits -= st->mode->nbPBands;
#ifndef STDIN_TUNING
compute_allocation(st->mode, offsets, stereo_mode, bits, pulses, fine_quant);
#endif
@ -991,10 +988,6 @@ int celt_decode_float(CELTDecoder * restrict st, unsigned char *data, int len, c
if (has_pitch)
{
int id;
/* Get the pitch gains and index */
id = ec_dec_bits(&dec, 7);
unquant_pitch(id, gains, st->mode->nbPBands);
pitch_index = ec_dec_uint(&dec, MAX_PERIOD-(2*N-2*N4));
st->last_pitch_index = pitch_index;
} else {
@ -1016,6 +1009,8 @@ int celt_decode_float(CELTDecoder * restrict st, unsigned char *data, int len, c
offsets[i] = 0;
bits = len*8 - ec_dec_tell(&dec, 0) - 1;
if (has_pitch)
bits -= st->mode->nbPBands;
compute_allocation(st->mode, offsets, stereo_mode, bits, pulses, fine_quant);
/*bits = ec_dec_tell(&dec, 0);
compute_fine_allocation(st->mode, fine_quant, (20*C+len*8/5-(ec_dec_tell(&dec, 0)-bits))/C);*/

View file

@ -1,133 +0,0 @@
/* Each row contains 8 values for the pitch table. The lowest index value is
stored in the MSB (i.e. big endian ordering) */
celt_uint16_t pgain_table[512] = {
0x0000, 0x0000, 0x0000, 0x0000,
0x140d, 0x0908, 0x5b11, 0x0f07,
0x8380, 0x7608, 0x0807, 0x0705,
0x6078, 0x0605, 0x0706, 0x0504,
0x8378, 0x4118, 0x520f, 0x0d06,
0x4c06, 0x3105, 0x0706, 0x0704,
0x0f4c, 0x0606, 0x0b0b, 0x0b06,
0x837d, 0x4538, 0x0c09, 0x0906,
0x7c05, 0x0425, 0x0605, 0x0604,
0x8c87, 0x837f, 0x7064, 0x293a,
0x8c87, 0x827a, 0x715f, 0x1008,
0x4a05, 0x0404, 0x0504, 0x0704,
0x8322, 0x0403, 0x0504, 0x0504,
0x1968, 0x0b68, 0x130e, 0x0b05,
0x710d, 0x3d08, 0x3508, 0x0804,
0x7c46, 0x4446, 0x0d0a, 0x0705,
0x8a81, 0x7647, 0x203f, 0x0f09,
0x8155, 0x0637, 0x0706, 0x0504,
0x8b84, 0x7e7a, 0x460d, 0x0b07,
0x0a27, 0x0705, 0x0607, 0x0809,
0x7d06, 0x2504, 0x0505, 0x0604,
0x8980, 0x3c74, 0x684f, 0x170c,
0x7c32, 0x3406, 0x0606, 0x0604,
0x2905, 0x0504, 0x0505, 0x0606,
0x151e, 0x1a6e, 0x5713, 0x0e07,
0x6128, 0x0505, 0x0605, 0x0604,
0x8c88, 0x8685, 0x7a6f, 0x6715,
0x7b08, 0x4b05, 0x0507, 0x0504,
0x8277, 0x6612, 0x0e13, 0x470b,
0x6804, 0x0403, 0x0405, 0x0604,
0x8241, 0x0404, 0x0405, 0x0504,
0x7908, 0x064a, 0x0608, 0x0604,
0x4c72, 0x450a, 0x0a08, 0x0804,
0x4909, 0x0807, 0x3507, 0x0704,
0x857c, 0x4871, 0x0f0c, 0x0906,
0x7e6e, 0x0f60, 0x510f, 0x0a06,
0x8278, 0x231a, 0x4250, 0x5514,
0x7e28, 0x2370, 0x5944, 0x150a,
0x7a2f, 0x0631, 0x0806, 0x0704,
0x8981, 0x7b77, 0x1460, 0x5b14,
0x8680, 0x0504, 0x0505, 0x0604,
0x206d, 0x645b, 0x5c1a, 0x0e07,
0x877d, 0x7614, 0x6763, 0x6226,
0x8d88, 0x8482, 0x795d, 0x3b0c,
0x4c0a, 0x0631, 0x0707, 0x0704,
0x8481, 0x3c05, 0x0808, 0x0705,
0x0a0b, 0x0908, 0x0b37, 0x0f08,
0x8c86, 0x817c, 0x483f, 0x110a,
0x494e, 0x090a, 0x4109, 0x0803,
0x730e, 0x0909, 0x590b, 0x0a04,
0x7f30, 0x6b73, 0x1e46, 0x4518,
0x8a81, 0x7b4b, 0x6215, 0x0a07,
0x7f77, 0x0a6a, 0x0908, 0x0805,
0x7a60, 0x0d0a, 0x0a0d, 0x4107,
0x8377, 0x3166, 0x191f, 0x4e0e,
0x0f09, 0x3607, 0x0809, 0x0a08,
0x8983, 0x7b23, 0x6452, 0x150d,
0x4639, 0x3e07, 0x0909, 0x0704,
0x0e06, 0x0731, 0x0608, 0x0908,
0x480a, 0x6508, 0x0908, 0x0704,
0x1c13, 0x7165, 0x190f, 0x0906,
0x8170, 0x1c6a, 0x1c4d, 0x190d,
0x7a0f, 0x0708, 0x0932, 0x0a04,
0x7c40, 0x0806, 0x3107, 0x0604,
0x7809, 0x3636, 0x0807, 0x0804,
0x8c85, 0x817f, 0x7423, 0x0e0a,
0x440f, 0x0967, 0x0a09, 0x0805,
0x8442, 0x7574, 0x664f, 0x190d,
0x1716, 0x5511, 0x4c0e, 0x0b05,
0x4b5c, 0x0a41, 0x0908, 0x0704,
0x7c6d, 0x0c08, 0x0a3a, 0x0905,
0x773f, 0x0a6a, 0x0908, 0x0704,
0x897f, 0x7753, 0x4836, 0x3b0e,
0x720f, 0x4e70, 0x0c0c, 0x0705,
0x8430, 0x7276, 0x6b5c, 0x5c1c,
0x7740, 0x700a, 0x0808, 0x0704,
0x8c86, 0x8280, 0x495d, 0x4b0d,
0x8361, 0x0304, 0x0505, 0x0404,
0x0c08, 0x0708, 0x090b, 0x350b,
0x867d, 0x751d, 0x6623, 0x480e,
0x3a33, 0x0606, 0x0707, 0x0704,
0x8877, 0x4879, 0x6f64, 0x5e22,
0x2467, 0x625e, 0x0c0c, 0x0a06,
0x8b84, 0x807d, 0x6d3c, 0x5638,
0x8303, 0x0303, 0x0405, 0x0604,
0x887d, 0x4973, 0x5613, 0x0c08,
0x847c, 0x6b0c, 0x1143, 0x0f08,
0x7c08, 0x0506, 0x2b06, 0x0604,
0x7b09, 0x0872, 0x0908, 0x0704,
0x6f11, 0x0a50, 0x4409, 0x0904,
0x7f5c, 0x2805, 0x0606, 0x0504,
0x0e53, 0x3f0a, 0x0b0b, 0x0a05,
0x0d77, 0x0a0a, 0x0a0d, 0x0a05,
0x7e1d, 0x636b, 0x5915, 0x1208,
0x0c0b, 0x0806, 0x2d09, 0x0a08,
0x8a86, 0x7e79, 0x0f11, 0x0906,
0x7f76, 0x0809, 0x3f08, 0x0704,
0x0b0c, 0x0769, 0x0a0c, 0x0f08,
0x8676, 0x1475, 0x6050, 0x521b,
0x400f, 0x423c, 0x0b07, 0x0804,
0x7423, 0x6712, 0x5611, 0x0b05,
0x1062, 0x0f11, 0x4d0c, 0x0b05,
0x8983, 0x7e75, 0x1a1e, 0x4510,
0x815a, 0x5206, 0x0b08, 0x0705,
0x8880, 0x7925, 0x1958, 0x541a,
0x1340, 0x0b3d, 0x0b0b, 0x0a05,
0x0e0e, 0x6a0b, 0x090b, 0x0b07,
0x824e, 0x716c, 0x1811, 0x0a06,
0x867e, 0x7911, 0x490e, 0x0907,
0x8a84, 0x7f4e, 0x6f66, 0x5718,
0x2966, 0x696b, 0x565c, 0x4c14,
0x5e4e, 0x0505, 0x0507, 0x0504,
0x8b84, 0x7f7b, 0x6b22, 0x490b,
0x7719, 0x1663, 0x5856, 0x591a,
0x3869, 0x0706, 0x0808, 0x0704,
0x7b09, 0x7206, 0x0808, 0x0704,
0x8981, 0x7b79, 0x1753, 0x160d,
0x771b, 0x541c, 0x2d4c, 0x4215,
0x8d87, 0x8583, 0x7b75, 0x6d49,
0x1b67, 0x6f0a, 0x120c, 0x0b04,
0x8a82, 0x7e7a, 0x3667, 0x6044,
0x8882, 0x7a42, 0x120a, 0x0907,
0x460c, 0x0807, 0x0a2f, 0x0905,
0x8071, 0x1c1b, 0x5f4a, 0x130b,
0x160d, 0x0a3f, 0x3e0c, 0x0905,
0x750f, 0x6f44, 0x0c0b, 0x0805,
0x827c, 0x0733, 0x0806, 0x0704,
0x0e0f, 0x4259, 0x0c0b, 0x0b05,
};

View file

@ -1,117 +0,0 @@
/* (C) 2007-2008 Jean-Marc Valin, CSIRO
*/
/*
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of the Xiph.org Foundation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "quant_pitch.h"
#include <math.h>
#include "pgain_table.h"
#include "arch.h"
#include "mathops.h"
#ifdef FIXED_POINT
#define PGAIN_ODD(codebook, i) ((celt_word16_t)(((codebook)[(i)]&0x00ffU)<<7))
#define PGAIN_EVEN(codebook, i) ((celt_word16_t)(((codebook)[(i)]&0xff00U)>>1))
#define PGAIN_ODD14(codebook, i) ((celt_word16_t)(((codebook)[(i)]&0x00ffU)<<6))
#define PGAIN_EVEN14(codebook, i) ((celt_word16_t)(((codebook)[(i)]&0xff00U)>>2))
#else
#define PGAIN_ODD(codebook, i) ((1.f/32768.f)*(celt_word16_t)(((codebook)[(i)]&0x00ffU)<<7))
#define PGAIN_EVEN(codebook, i) ((1.f/32768.f)*(celt_word16_t)(((codebook)[(i)]&0xff00U)>>1) )
#define PGAIN_ODD14(codebook, i) PGAIN_ODD(codebook, i)
#define PGAIN_EVEN14(codebook, i) PGAIN_EVEN(codebook, i)
#endif
#define PGAIN(codebook, i) ((i)&1 ? PGAIN_ODD(codebook, (i)>>1) : PGAIN_EVEN(codebook, (i)>>1))
#define Q1515ONE MULT16_16(Q15ONE,Q15ONE)
/** Taken from Speex.Finds the index of the entry in a codebook that best matches the input*/
int vq_index(const celt_pgain_t *in, const celt_uint16_t *codebook, int len, int entries)
{
int i,j;
int ind = 0;
celt_word32_t min_dist=VERY_LARGE32;
int best_index=0;
for (i=0;i<entries;i++)
{
celt_word32_t dist=0;
const celt_pgain_t *inp = in;
j=0; do {
celt_pgain_t tmp1 = SUB16(*inp++,PGAIN_EVEN14(codebook, ind));
celt_pgain_t tmp2 = SUB16(*inp++,PGAIN_ODD14(codebook, ind));
ind++;
dist = MAC16_16(dist, tmp1, tmp1);
dist = MAC16_16(dist, tmp2, tmp2);
} while (++j<len>>1);
if (dist<min_dist)
{
min_dist=dist;
best_index=i;
}
}
return best_index;
}
int quant_pitch(celt_pgain_t *gains, int len)
{
int i, id;
celt_word32_t gain_sum = 0;
/*for (i=0;i<len;i++) printf ("%f ", gains[i]);printf ("\n");*/
/* Convert to a representation where the MSE criterion should be near-optimal */
for (i=0;i<len;i++)
{
gains[i] = SHR16(Q15ONE-celt_sqrt(Q1515ONE-MULT16_16(gains[i],gains[i])),1);
gain_sum = ADD32(gain_sum, EXTEND32(gains[i]));
}
/* Is it worth encoding the pitch? */
if (gain_sum > QCONST32(.3f,15))
{
id = vq_index(gains, pgain_table, len, 128);
/* FIXME: Remove when we're not waisting a transmitted index on 0 gains */
if (id==0)
id = -1;
} else {
id = -1;
}
return id;
}
/** Returns the pitch gain vector corresponding to a certain id */
void unquant_pitch(int id, celt_pgain_t *gains, int len)
{
int i;
for (i=0;i<len;i++)
gains[i] = celt_sqrt(Q1515ONE-MULT16_16(Q15ONE-PGAIN(pgain_table,id*len+i),Q15ONE-PGAIN(pgain_table,id*len+i)));
}

View file

@ -1,44 +0,0 @@
/* (C) 2007 Jean-Marc Valin, CSIRO
*/
/*
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of the Xiph.org Foundation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef QUANT_PITCH_H
#define QUANT_PITCH_H
#include "arch.h"
#include "entenc.h"
#include "entdec.h"
/** If this returns -1, the gain is zero (don't encode the pitch index) */
int quant_pitch(celt_pgain_t *gains, int len);
void unquant_pitch(int id, celt_pgain_t *gains, int len);
#endif /* QUANT_PITCH_H */