1 /* Copyright (C) 2002 Jean-Marc Valin */
4 @brief Long-Term Prediction functions
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
11 - Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
14 - Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
18 - Neither the name of the Xiph.org Foundation nor the names of its
19 contributors may be used to endorse or promote products derived from
20 this software without specific prior written permission.
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
26 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <speex/speex_bits.h>
38 /** LTP parameters. */
40 const signed char *gain_cdbk
;
46 #define gain_3tap_to_1tap(g) (ABS(g[1]) + (g[0]>0 ? g[0] : -SHR16(g[0],1)) + (g[2]>0 ? g[2] : -SHR16(g[2],1)))
48 #define gain_3tap_to_1tap(g) (ABS(g[1]) + (g[0]>0 ? g[0] : -.5*g[0]) + (g[2]>0 ? g[2] : -.5*g[2]))
51 spx_word32_t
inner_prod(const spx_word16_t
*x
, const spx_word16_t
*y
, int len
);
52 void pitch_xcorr(const spx_word16_t
*_x
, const spx_word16_t
*_y
, spx_word32_t
*corr
, int len
, int nb_pitch
, char *stack
);
54 void open_loop_nbest_pitch(spx_word16_t
*sw
, int start
, int end
, int len
, int *pitch
, spx_word16_t
*gain
, int N
, char *stack
);
57 /** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */
58 int pitch_search_3tap(
59 spx_word16_t target
[], /* Target vector */
61 spx_coef_t ak
[], /* LPCs for this subframe */
62 spx_coef_t awk1
[], /* Weighted LPCs #1 for this subframe */
63 spx_coef_t awk2
[], /* Weighted LPCs #2 for this subframe */
64 spx_sig_t exc
[], /* Overlapping codebook */
66 int start
, /* Smallest pitch value allowed */
67 int end
, /* Largest pitch value allowed */
68 spx_word16_t pitch_coef
, /* Voicing (pitch) coefficient */
69 int p
, /* Number of LPC coeffs */
70 int nsf
, /* Number of samples in subframe */
78 spx_word32_t
*cumul_gain
81 /*Unquantize adaptive codebook and update pitch contribution*/
82 void pitch_unquant_3tap(
83 spx_word16_t exc
[], /* Input excitation */
84 spx_word32_t exc_out
[], /* Output excitation */
85 int start
, /* Smallest pitch value allowed */
86 int end
, /* Largest pitch value allowed */
87 spx_word16_t pitch_coef
, /* Voicing (pitch) coefficient */
89 int nsf
, /* Number of samples in subframe */
91 spx_word16_t
*gain_val
,
96 spx_word16_t last_pitch_gain
,
100 /** Forced pitch delay and gain */
101 int forced_pitch_quant(
102 spx_word16_t target
[], /* Target vector */
104 spx_coef_t ak
[], /* LPCs for this subframe */
105 spx_coef_t awk1
[], /* Weighted LPCs #1 for this subframe */
106 spx_coef_t awk2
[], /* Weighted LPCs #2 for this subframe */
107 spx_sig_t exc
[], /* Excitation */
109 int start
, /* Smallest pitch value allowed */
110 int end
, /* Largest pitch value allowed */
111 spx_word16_t pitch_coef
, /* Voicing (pitch) coefficient */
112 int p
, /* Number of LPC coeffs */
113 int nsf
, /* Number of samples in subframe */
121 spx_word32_t
*cumul_gain
124 /** Unquantize forced pitch delay and gain */
125 void forced_pitch_unquant(
126 spx_word16_t exc
[], /* Input excitation */
127 spx_word32_t exc_out
[], /* Output excitation */
128 int start
, /* Smallest pitch value allowed */
129 int end
, /* Largest pitch value allowed */
130 spx_word16_t pitch_coef
, /* Voicing (pitch) coefficient */
132 int nsf
, /* Number of samples in subframe */
134 spx_word16_t
*gain_val
,
139 spx_word16_t last_pitch_gain
,