134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)/* Copyright (C) 2002 Jean-Marc Valin */
234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)/**
334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   @file ltp.h
434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   @brief Long-Term Prediction functions
534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)*/
634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)/*
734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   Redistribution and use in source and binary forms, with or without
834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   modification, are permitted provided that the following conditions
934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   are met:
1034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
1134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   - Redistributions of source code must retain the above copyright
1234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   notice, this list of conditions and the following disclaimer.
1334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
1434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   - Redistributions in binary form must reproduce the above copyright
1534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   notice, this list of conditions and the following disclaimer in the
1634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   documentation and/or other materials provided with the distribution.
1734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
1834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   - Neither the name of the Xiph.org Foundation nor the names of its
1934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   contributors may be used to endorse or promote products derived from
2034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   this software without specific prior written permission.
2134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
2234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
2634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)*/
3434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
3534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#include <speex/speex_bits.h>
3634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#include "arch.h"
3734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
3834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)/** LTP parameters. */
3934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)typedef struct {
4034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   const signed char *gain_cdbk;
4134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   int     gain_bits;
4234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)   int     pitch_bits;
4334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)} ltp_params;
4434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
4534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#ifdef FIXED_POINT
4634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#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)))
4734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#else
4834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#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]))
4934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)#endif
5034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
5134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word32_t inner_prod(const spx_word16_t *x, const spx_word16_t *y, int len);
5234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)void pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack);
5334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
5434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)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);
5534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
5634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
5734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)/** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */
5834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int pitch_search_3tap(
5934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t target[],                 /* Target vector */
6034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t *sw,
6134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_coef_t ak[],                     /* LPCs for this subframe */
6234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_coef_t awk1[],                   /* Weighted LPCs #1 for this subframe */
6334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_coef_t awk2[],                   /* Weighted LPCs #2 for this subframe */
6434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_sig_t exc[],                    /* Overlapping codebook */
6534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)const void *par,
6634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int   start,                    /* Smallest pitch value allowed */
6734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int   end,                      /* Largest pitch value allowed */
6834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t pitch_coef,               /* Voicing (pitch) coefficient */
6934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int   p,                        /* Number of LPC coeffs */
7034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int   nsf,                      /* Number of samples in subframe */
7134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)SpeexBits *bits,
7234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)char *stack,
7334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t *exc2,
7434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t *r,
7534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int   complexity,
7634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int   cdbk_offset,
7734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int plc_tuning,
7834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word32_t *cumul_gain
7934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles));
8034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
8134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)/*Unquantize adaptive codebook and update pitch contribution*/
8234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)void pitch_unquant_3tap(
8334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t exc[],             /* Input excitation */
8434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word32_t exc_out[],         /* Output excitation */
8534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int   start,                    /* Smallest pitch value allowed */
8634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int   end,                      /* Largest pitch value allowed */
8734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t pitch_coef,        /* Voicing (pitch) coefficient */
8834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)const void *par,
8934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int   nsf,                      /* Number of samples in subframe */
9034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int *pitch_val,
9134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t *gain_val,
9234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)SpeexBits *bits,
9334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)char *stack,
9434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int lost,
9534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int subframe_offset,
9634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t last_pitch_gain,
9734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int cdbk_offset
9834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles));
9934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
10034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)/** Forced pitch delay and gain */
10134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int forced_pitch_quant(
10234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t target[],                 /* Target vector */
10334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t *sw,
10434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_coef_t ak[],                     /* LPCs for this subframe */
10534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_coef_t awk1[],                   /* Weighted LPCs #1 for this subframe */
10634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_coef_t awk2[],                   /* Weighted LPCs #2 for this subframe */
10734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_sig_t exc[],                    /* Excitation */
10834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)const void *par,
10934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int   start,                    /* Smallest pitch value allowed */
11034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int   end,                      /* Largest pitch value allowed */
11134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t pitch_coef,               /* Voicing (pitch) coefficient */
11234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int   p,                        /* Number of LPC coeffs */
11334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int   nsf,                      /* Number of samples in subframe */
11434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)SpeexBits *bits,
11534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)char *stack,
11634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t *exc2,
11734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t *r,
11834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int complexity,
11934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int cdbk_offset,
12034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int plc_tuning,
12134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word32_t *cumul_gain
12234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles));
12334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)
12434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)/** Unquantize forced pitch delay and gain */
12534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)void forced_pitch_unquant(
12634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t exc[],             /* Input excitation */
12734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word32_t exc_out[],         /* Output excitation */
12834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int   start,                    /* Smallest pitch value allowed */
12934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int   end,                      /* Largest pitch value allowed */
13034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t pitch_coef,        /* Voicing (pitch) coefficient */
13134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)const void *par,
13234680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int   nsf,                      /* Number of samples in subframe */
13334680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int *pitch_val,
13434680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t *gain_val,
13534680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)SpeexBits *bits,
13634680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)char *stack,
13734680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int lost,
13834680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int subframe_offset,
13934680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)spx_word16_t last_pitch_gain,
14034680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles)int cdbk_offset
14134680572440d7894ef8dafce81d8039ed80726a2Torne (Richard Coles));
142