| 1 |
/* Copyright (C) 2002-2006 Jean-Marc Valin */ |
| 2 |
/** |
| 3 |
@file nb_celp.h |
| 4 |
@brief Narrowband CELP encoder/decoder |
| 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 NB_CELP_H |
| 37 |
#define NB_CELP_H |
| 38 |
|
| 39 |
#include "modes.h" |
| 40 |
#include "speex/speex_callbacks.h" |
| 41 |
#include "vbr.h" |
| 42 |
#include "filters.h" |
| 43 |
|
| 44 |
#ifdef VORBIS_PSYCHO |
| 45 |
#include "vorbis_psy.h" |
| 46 |
#endif |
| 47 |
|
| 48 |
#define NB_ORDER 10 |
| 49 |
#define NB_FRAME_SIZE 160 |
| 50 |
#define NB_SUBFRAME_SIZE 40 |
| 51 |
#define NB_NB_SUBFRAMES 4 |
| 52 |
#define NB_PITCH_START 17 |
| 53 |
#define NB_PITCH_END 144 |
| 54 |
|
| 55 |
#define NB_WINDOW_SIZE (NB_FRAME_SIZE+NB_SUBFRAME_SIZE) |
| 56 |
#define NB_EXCBUF (NB_FRAME_SIZE+NB_PITCH_END+2) |
| 57 |
#define NB_DEC_BUFFER (NB_FRAME_SIZE+2*NB_PITCH_END+NB_SUBFRAME_SIZE+12) |
| 58 |
/**Structure representing the full state of the narrowband encoder*/ |
| 59 |
typedef struct EncState { |
| 60 |
const SpeexMode *mode; /**< Mode corresponding to the state */ |
| 61 |
int first; /**< Is this the first frame? */ |
| 62 |
|
| 63 |
spx_word32_t cumul_gain; /**< Product of previously used pitch gains (Q10) */ |
| 64 |
int bounded_pitch; /**< Next frame should not rely on previous frames for pitch */ |
| 65 |
int ol_pitch; /**< Open-loop pitch */ |
| 66 |
int ol_voiced; /**< Open-loop voiced/non-voiced decision */ |
| 67 |
int pitch[NB_NB_SUBFRAMES]; |
| 68 |
|
| 69 |
#ifdef VORBIS_PSYCHO |
| 70 |
VorbisPsy *psy; |
| 71 |
float *psy_window; |
| 72 |
float *curve; |
| 73 |
float *old_curve; |
| 74 |
#endif |
| 75 |
|
| 76 |
spx_word16_t gamma1; /**< Perceptual filter: A(z/gamma1) */ |
| 77 |
spx_word16_t gamma2; /**< Perceptual filter: A(z/gamma2) */ |
| 78 |
spx_word16_t lpc_floor; /**< Noise floor multiplier for A[0] in LPC analysis*/ |
| 79 |
char *stack; /**< Pseudo-stack allocation for temporary memory */ |
| 80 |
spx_word16_t winBuf[NB_WINDOW_SIZE-NB_FRAME_SIZE]; /**< Input buffer (original signal) */ |
| 81 |
spx_word16_t excBuf[NB_EXCBUF]; /**< Excitation buffer */ |
| 82 |
spx_word16_t *exc; /**< Start of excitation frame */ |
| 83 |
spx_word16_t swBuf[NB_EXCBUF]; /**< Weighted signal buffer */ |
| 84 |
spx_word16_t *sw; /**< Start of weighted signal frame */ |
| 85 |
const spx_word16_t *window; /**< Temporary (Hanning) window */ |
| 86 |
const spx_word16_t *lagWindow; /**< Window applied to auto-correlation */ |
| 87 |
spx_lsp_t old_lsp[NB_ORDER]; /**< LSPs for previous frame */ |
| 88 |
spx_lsp_t old_qlsp[NB_ORDER]; /**< Quantized LSPs for previous frame */ |
| 89 |
spx_mem_t mem_sp[NB_ORDER]; /**< Filter memory for signal synthesis */ |
| 90 |
spx_mem_t mem_sw[NB_ORDER]; /**< Filter memory for perceptually-weighted signal */ |
| 91 |
spx_mem_t mem_sw_whole[NB_ORDER]; /**< Filter memory for perceptually-weighted signal (whole frame)*/ |
| 92 |
spx_mem_t mem_exc[NB_ORDER]; /**< Filter memory for excitation (whole frame) */ |
| 93 |
spx_mem_t mem_exc2[NB_ORDER]; /**< Filter memory for excitation (whole frame) */ |
| 94 |
spx_mem_t mem_hp[2]; /**< High-pass filter memory */ |
| 95 |
spx_word32_t pi_gain[NB_NB_SUBFRAMES]; /**< Gain of LPC filter at theta=pi (fe/2) */ |
| 96 |
spx_word16_t *innov_rms_save; /**< If non-NULL, innovation RMS is copied here */ |
| 97 |
|
| 98 |
#ifndef DISABLE_VBR |
| 99 |
VBRState vbr; /**< State of the VBR data */ |
| 100 |
float vbr_quality; /**< Quality setting for VBR encoding */ |
| 101 |
float relative_quality; /**< Relative quality that will be needed by VBR */ |
| 102 |
spx_int32_t vbr_enabled; /**< 1 for enabling VBR, 0 otherwise */ |
| 103 |
spx_int32_t vbr_max; /**< Max bit-rate allowed in VBR mode */ |
| 104 |
int vad_enabled; /**< 1 for enabling VAD, 0 otherwise */ |
| 105 |
int dtx_enabled; /**< 1 for enabling DTX, 0 otherwise */ |
| 106 |
int dtx_count; /**< Number of consecutive DTX frames */ |
| 107 |
spx_int32_t abr_enabled; /**< ABR setting (in bps), 0 if off */ |
| 108 |
float abr_drift; |
| 109 |
float abr_drift2; |
| 110 |
float abr_count; |
| 111 |
#endif /* #ifndef DISABLE_VBR */ |
| 112 |
|
| 113 |
int complexity; /**< Complexity setting (0-10 from least complex to most complex) */ |
| 114 |
spx_int32_t sampling_rate; |
| 115 |
int plc_tuning; |
| 116 |
int encode_submode; |
| 117 |
const SpeexSubmode * const *submodes; /**< Sub-mode data */ |
| 118 |
int submodeID; /**< Activated sub-mode */ |
| 119 |
int submodeSelect; /**< Mode chosen by the user (may differ from submodeID if VAD is on) */ |
| 120 |
int isWideband; /**< Is this used as part of the embedded wideband codec */ |
| 121 |
int highpass_enabled; /**< Is the input filter enabled */ |
| 122 |
} EncState; |
| 123 |
|
| 124 |
/**Structure representing the full state of the narrowband decoder*/ |
| 125 |
typedef struct DecState { |
| 126 |
const SpeexMode *mode; /**< Mode corresponding to the state */ |
| 127 |
int first; /**< Is this the first frame? */ |
| 128 |
int count_lost; /**< Was the last frame lost? */ |
| 129 |
spx_int32_t sampling_rate; |
| 130 |
|
| 131 |
spx_word16_t last_ol_gain; /**< Open-loop gain for previous frame */ |
| 132 |
|
| 133 |
char *stack; /**< Pseudo-stack allocation for temporary memory */ |
| 134 |
spx_word16_t excBuf[NB_DEC_BUFFER]; /**< Excitation buffer */ |
| 135 |
spx_word16_t *exc; /**< Start of excitation frame */ |
| 136 |
spx_lsp_t old_qlsp[NB_ORDER]; /**< Quantized LSPs for previous frame */ |
| 137 |
spx_coef_t interp_qlpc[NB_ORDER]; /**< Interpolated quantized LPCs */ |
| 138 |
spx_mem_t mem_sp[NB_ORDER]; /**< Filter memory for synthesis signal */ |
| 139 |
spx_mem_t mem_hp[2]; /**< High-pass filter memory */ |
| 140 |
spx_word32_t pi_gain[NB_NB_SUBFRAMES]; /**< Gain of LPC filter at theta=pi (fe/2) */ |
| 141 |
spx_word16_t *innov_save; /** If non-NULL, innovation is copied here */ |
| 142 |
|
| 143 |
spx_word16_t level; |
| 144 |
spx_word16_t max_level; |
| 145 |
spx_word16_t min_level; |
| 146 |
|
| 147 |
/* This is used in packet loss concealment */ |
| 148 |
int last_pitch; /**< Pitch of last correctly decoded frame */ |
| 149 |
spx_word16_t last_pitch_gain; /**< Pitch gain of last correctly decoded frame */ |
| 150 |
spx_word16_t pitch_gain_buf[3]; /**< Pitch gain of last decoded frames */ |
| 151 |
int pitch_gain_buf_idx; /**< Tail of the buffer */ |
| 152 |
spx_uint32_t seed; /** Seed used for random number generation */ |
| 153 |
|
| 154 |
int encode_submode; |
| 155 |
const SpeexSubmode * const *submodes; /**< Sub-mode data */ |
| 156 |
int submodeID; /**< Activated sub-mode */ |
| 157 |
int lpc_enh_enabled; /**< 1 when LPC enhancer is on, 0 otherwise */ |
| 158 |
SpeexCallback speex_callbacks[SPEEX_MAX_CALLBACKS]; |
| 159 |
|
| 160 |
SpeexCallback user_callback; |
| 161 |
|
| 162 |
/*Vocoder data*/ |
| 163 |
spx_word16_t voc_m1; |
| 164 |
spx_word32_t voc_m2; |
| 165 |
spx_word16_t voc_mean; |
| 166 |
int voc_offset; |
| 167 |
|
| 168 |
int dtx_enabled; |
| 169 |
int isWideband; /**< Is this used as part of the embedded wideband codec */ |
| 170 |
int highpass_enabled; /**< Is the input filter enabled */ |
| 171 |
} DecState; |
| 172 |
|
| 173 |
/** Initializes encoder state*/ |
| 174 |
void *nb_encoder_init(const SpeexMode *m); |
| 175 |
|
| 176 |
/** De-allocates encoder state resources*/ |
| 177 |
void nb_encoder_destroy(void *state); |
| 178 |
|
| 179 |
/** Encodes one frame*/ |
| 180 |
int nb_encode(void *state, void *in, SpeexBits *bits); |
| 181 |
|
| 182 |
|
| 183 |
/** Initializes decoder state*/ |
| 184 |
void *nb_decoder_init(const SpeexMode *m); |
| 185 |
|
| 186 |
/** De-allocates decoder state resources*/ |
| 187 |
void nb_decoder_destroy(void *state); |
| 188 |
|
| 189 |
/** Decodes one frame*/ |
| 190 |
int nb_decode(void *state, SpeexBits *bits, void *out); |
| 191 |
|
| 192 |
/** ioctl-like function for controlling a narrowband encoder */ |
| 193 |
int nb_encoder_ctl(void *state, int request, void *ptr); |
| 194 |
|
| 195 |
/** ioctl-like function for controlling a narrowband decoder */ |
| 196 |
int nb_decoder_ctl(void *state, int request, void *ptr); |
| 197 |
|
| 198 |
|
| 199 |
#endif |