1 /* Copyright (C) 2002-2006 Jean-Marc Valin */
2 /**
3    @file sb_celp.h
4    @brief Sub-band CELP mode used for wideband encoding
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 SB_CELP_H
37 #define SB_CELP_H
38 
39 #include "modes.h"
40 #include <speex/speex_bits.h>
41 #include "nb_celp.h"
42 
43 /**Structure representing the full state of the sub-band encoder*/
44 typedef struct SBEncState {
45    const SpeexMode *mode;         /**< Pointer to the mode (containing for vtable info) */
46    void *st_low;                  /**< State of the low-band (narrowband) encoder */
47    int    full_frame_size;        /**< Length of full-band frames*/
48    int    frame_size;             /**< Length of high-band frames*/
49    int    subframeSize;           /**< Length of high-band sub-frames*/
50    int    nbSubframes;            /**< Number of high-band sub-frames*/
51    int    windowSize;             /**< Length of high-band LPC window*/
52    int    lpcSize;                /**< Order of high-band LPC analysis */
53    int    first;                  /**< First frame? */
54    spx_word16_t  lpc_floor;       /**< Controls LPC analysis noise floor */
55    spx_word16_t  gamma1;          /**< Perceptual weighting coef 1 */
56    spx_word16_t  gamma2;          /**< Perceptual weighting coef 2 */
57 
58    char  *stack;                  /**< Temporary allocation stack */
59    spx_word16_t *high;               /**< High-band signal (buffer) */
60    spx_word16_t *h0_mem, *h1_mem;
61 
62    const spx_word16_t *window;    /**< LPC analysis window */
63    const spx_word16_t *lagWindow;       /**< Auto-correlation window */
64    spx_lsp_t *old_lsp;            /**< LSPs of previous frame */
65    spx_lsp_t *old_qlsp;           /**< Quantized LSPs of previous frame */
66    spx_coef_t *interp_qlpc;       /**< Interpolated quantized LPCs for current sub-frame */
67 
68    spx_mem_t *mem_sp;             /**< Synthesis signal memory */
69    spx_mem_t *mem_sp2;
70    spx_mem_t *mem_sw;             /**< Perceptual signal memory */
71    spx_word32_t *pi_gain;
72    spx_word16_t *exc_rms;
73    spx_word16_t *innov_rms_save;         /**< If non-NULL, innovation is copied here */
74 
75 #ifndef DISABLE_VBR
76    float  vbr_quality;            /**< Quality setting for VBR encoding */
77    int    vbr_enabled;            /**< 1 for enabling VBR, 0 otherwise */
78    spx_int32_t vbr_max;           /**< Max bit-rate allowed in VBR mode (total) */
79    spx_int32_t vbr_max_high;      /**< Max bit-rate allowed in VBR mode for the high-band */
80    spx_int32_t abr_enabled;       /**< ABR setting (in bps), 0 if off */
81    float  abr_drift;
82    float  abr_drift2;
83    float  abr_count;
84    int    vad_enabled;            /**< 1 for enabling VAD, 0 otherwise */
85    float  relative_quality;
86 #endif /* #ifndef DISABLE_VBR */
87 
88    int    encode_submode;
89    const SpeexSubmode * const *submodes;
90    int    submodeID;
91    int    submodeSelect;
92    int    complexity;
93    spx_int32_t sampling_rate;
94 
95 } SBEncState;
96 
97 
98 /**Structure representing the full state of the sub-band decoder*/
99 typedef struct SBDecState {
100    const SpeexMode *mode;            /**< Pointer to the mode (containing for vtable info) */
101    void *st_low;               /**< State of the low-band (narrowband) encoder */
102    int    full_frame_size;
103    int    frame_size;
104    int    subframeSize;
105    int    nbSubframes;
106    int    lpcSize;
107    int    first;
108    spx_int32_t sampling_rate;
109    int    lpc_enh_enabled;
110 
111    char  *stack;
112    spx_word16_t *g0_mem, *g1_mem;
113 
114    spx_word16_t *excBuf;
115    spx_lsp_t *old_qlsp;
116    spx_coef_t *interp_qlpc;
117 
118    spx_mem_t *mem_sp;
119    spx_word32_t *pi_gain;
120    spx_word16_t *exc_rms;
121    spx_word16_t *innov_save;      /** If non-NULL, innovation is copied here */
122 
123    spx_word16_t last_ener;
124    spx_int32_t seed;
125 
126    int    encode_submode;
127    const SpeexSubmode * const *submodes;
128    int    submodeID;
129 } SBDecState;
130 
131 
132 /**Initializes encoder state*/
133 void *sb_encoder_init(const SpeexMode *m);
134 
135 /**De-allocates encoder state resources*/
136 void sb_encoder_destroy(void *state);
137 
138 /**Encodes one frame*/
139 int sb_encode(void *state, void *in, SpeexBits *bits);
140 
141 
142 /**Initializes decoder state*/
143 void *sb_decoder_init(const SpeexMode *m);
144 
145 /**De-allocates decoder state resources*/
146 void sb_decoder_destroy(void *state);
147 
148 /**Decodes one frame*/
149 int sb_decode(void *state, SpeexBits *bits, void *out);
150 
151 int sb_encoder_ctl(void *state, int request, void *ptr);
152 
153 int sb_decoder_ctl(void *state, int request, void *ptr);
154 
155 #endif
156