1 /* Copyright (C) 2002-2006 Jean-Marc Valin */
2 /**
3    @file modes.h
4    @brief Describes the different modes of the codec
5 */
6 /*
7    Redistribution and use in source and binary forms, with or without
8    modification, are permitted provided that the following conditions
9    are met:
10 
11    - Redistributions of source code must retain the above copyright
12    notice, this list of conditions and the following disclaimer.
13 
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.
17 
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.
21 
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.
33 
34 */
35 
36 #ifndef MODES_H
37 #define MODES_H
38 
39 #include <speex/speex.h>
40 #include <speex/speex_bits.h>
41 #include "arch.h"
42 
43 #define NB_SUBMODES 16
44 #define NB_SUBMODE_BITS 4
45 
46 #define SB_SUBMODES 8
47 #define SB_SUBMODE_BITS 3
48 
49 /* Used internally, NOT TO BE USED in applications */
50 /** Used internally*/
51 #define SPEEX_GET_PI_GAIN 100
52 /** Used internally*/
53 #define SPEEX_GET_EXC     101
54 /** Used internally*/
55 #define SPEEX_GET_INNOV   102
56 /** Used internally*/
57 #define SPEEX_GET_DTX_STATUS   103
58 /** Used internally*/
59 #define SPEEX_SET_INNOVATION_SAVE   104
60 /** Used internally*/
61 #define SPEEX_SET_WIDEBAND   105
62 
63 /** Used internally*/
64 #define SPEEX_GET_STACK   106
65 
66 
67 /** Quantizes LSPs */
68 typedef void (*lsp_quant_func)(spx_lsp_t *, spx_lsp_t *, int, SpeexBits *);
69 
70 /** Decodes quantized LSPs */
71 typedef void (*lsp_unquant_func)(spx_lsp_t *, int, SpeexBits *);
72 
73 
74 /** Long-term predictor quantization */
75 typedef int (*ltp_quant_func)(spx_word16_t *, spx_word16_t *, spx_coef_t *, spx_coef_t *,
76                               spx_coef_t *, spx_sig_t *, const void *, int, int, spx_word16_t,
77                               int, int, SpeexBits*, char *, spx_word16_t *, spx_word16_t *, int, int, int, spx_word32_t *);
78 
79 /** Long-term un-quantize */
80 typedef void (*ltp_unquant_func)(spx_word16_t *, spx_word32_t *, int, int, spx_word16_t, const void *, int, int *,
81                                  spx_word16_t *, SpeexBits*, char*, int, int, spx_word16_t, int);
82 
83 
84 /** Innovation quantization function */
85 typedef void (*innovation_quant_func)(spx_word16_t *, spx_coef_t *, spx_coef_t *, spx_coef_t *, const void *, int, int,
86                                       spx_sig_t *, spx_word16_t *, SpeexBits *, char *, int, int);
87 
88 /** Innovation unquantization function */
89 typedef void (*innovation_unquant_func)(spx_sig_t *, const void *, int, SpeexBits*, char *, spx_int32_t *);
90 
91 /** Description of a Speex sub-mode (wither narrowband or wideband */
92 typedef struct SpeexSubmode {
93    int     lbr_pitch;          /**< Set to -1 for "normal" modes, otherwise encode pitch using a global pitch and allowing a +- lbr_pitch variation (for low not-rates)*/
94    int     forced_pitch_gain;  /**< Use the same (forced) pitch gain for all sub-frames */
95    int     have_subframe_gain; /**< Number of bits to use as sub-frame innovation gain */
96    int     double_codebook;    /**< Apply innovation quantization twice for higher quality (and higher bit-rate)*/
97    /*LSP functions*/
98    lsp_quant_func    lsp_quant; /**< LSP quantization function */
99    lsp_unquant_func  lsp_unquant; /**< LSP unquantization function */
100 
101    /*Long-term predictor functions*/
102    ltp_quant_func    ltp_quant; /**< Long-term predictor (pitch) quantizer */
103    ltp_unquant_func  ltp_unquant; /**< Long-term predictor (pitch) un-quantizer */
104    const void       *ltp_params; /**< Pitch parameters (options) */
105 
106    /*Quantization of innovation*/
107    innovation_quant_func innovation_quant; /**< Innovation quantization */
108    innovation_unquant_func innovation_unquant; /**< Innovation un-quantization */
109    const void             *innovation_params; /**< Innovation quantization parameters*/
110 
111    spx_word16_t      comb_gain;  /**< Gain of enhancer comb filter */
112 
113    int               bits_per_frame; /**< Number of bits per frame after encoding*/
114 } SpeexSubmode;
115 
116 /** Struct defining the encoding/decoding mode*/
117 typedef struct SpeexNBMode {
118    int     frameSize;      /**< Size of frames used for encoding */
119    int     subframeSize;   /**< Size of sub-frames used for encoding */
120    int     lpcSize;        /**< Order of LPC filter */
121    int     pitchStart;     /**< Smallest pitch value allowed */
122    int     pitchEnd;       /**< Largest pitch value allowed */
123 
124    spx_word16_t gamma1;    /**< Perceptual filter parameter #1 */
125    spx_word16_t gamma2;    /**< Perceptual filter parameter #2 */
126    spx_word16_t   lpc_floor;      /**< Noise floor for LPC analysis */
127 
128    const SpeexSubmode *submodes[NB_SUBMODES]; /**< Sub-mode data for the mode */
129    int     defaultSubmode; /**< Default sub-mode to use when encoding */
130    int     quality_map[11]; /**< Mode corresponding to each quality setting */
131 } SpeexNBMode;
132 
133 
134 /** Struct defining the encoding/decoding mode for SB-CELP (wideband) */
135 typedef struct SpeexSBMode {
136    const SpeexMode *nb_mode;    /**< Embedded narrowband mode */
137    int     frameSize;     /**< Size of frames used for encoding */
138    int     subframeSize;  /**< Size of sub-frames used for encoding */
139    int     lpcSize;       /**< Order of LPC filter */
140    spx_word16_t gamma1;   /**< Perceptual filter parameter #1 */
141    spx_word16_t gamma2;   /**< Perceptual filter parameter #1 */
142    spx_word16_t   lpc_floor;     /**< Noise floor for LPC analysis */
143    spx_word16_t   folding_gain;
144 
145    const SpeexSubmode *submodes[SB_SUBMODES]; /**< Sub-mode data for the mode */
146    int     defaultSubmode; /**< Default sub-mode to use when encoding */
147    int     low_quality_map[11]; /**< Mode corresponding to each quality setting */
148    int     quality_map[11]; /**< Mode corresponding to each quality setting */
149 #ifndef DISABLE_VBR
150    const float (*vbr_thresh)[11];
151 #endif
152    int     nb_modes;
153 } SpeexSBMode;
154 
155 int speex_encode_native(void *state, spx_word16_t *in, SpeexBits *bits);
156 int speex_decode_native(void *state, SpeexBits *bits, spx_word16_t *out);
157 
158 int nb_mode_query(const void *mode, int request, void *ptr);
159 int wb_mode_query(const void *mode, int request, void *ptr);
160 
161 #endif
162