ex

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Committer:
TMBOY
Date:
Tue Jul 18 16:54:45 2017 +0800
Revision:
47:9e361da97763
?

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TMBOY 47:9e361da97763 1 /* Copyright (C) 2002-2006 Jean-Marc Valin */
TMBOY 47:9e361da97763 2 /**
TMBOY 47:9e361da97763 3 @file nb_celp.h
TMBOY 47:9e361da97763 4 @brief Narrowband CELP encoder/decoder
TMBOY 47:9e361da97763 5 */
TMBOY 47:9e361da97763 6 /*
TMBOY 47:9e361da97763 7 Redistribution and use in source and binary forms, with or without
TMBOY 47:9e361da97763 8 modification, are permitted provided that the following conditions
TMBOY 47:9e361da97763 9 are met:
TMBOY 47:9e361da97763 10
TMBOY 47:9e361da97763 11 - Redistributions of source code must retain the above copyright
TMBOY 47:9e361da97763 12 notice, this list of conditions and the following disclaimer.
TMBOY 47:9e361da97763 13
TMBOY 47:9e361da97763 14 - Redistributions in binary form must reproduce the above copyright
TMBOY 47:9e361da97763 15 notice, this list of conditions and the following disclaimer in the
TMBOY 47:9e361da97763 16 documentation and/or other materials provided with the distribution.
TMBOY 47:9e361da97763 17
TMBOY 47:9e361da97763 18 - Neither the name of the Xiph.org Foundation nor the names of its
TMBOY 47:9e361da97763 19 contributors may be used to endorse or promote products derived from
TMBOY 47:9e361da97763 20 this software without specific prior written permission.
TMBOY 47:9e361da97763 21
TMBOY 47:9e361da97763 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
TMBOY 47:9e361da97763 23 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
TMBOY 47:9e361da97763 24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
TMBOY 47:9e361da97763 25 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
TMBOY 47:9e361da97763 26 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
TMBOY 47:9e361da97763 27 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
TMBOY 47:9e361da97763 28 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
TMBOY 47:9e361da97763 29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
TMBOY 47:9e361da97763 30 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
TMBOY 47:9e361da97763 31 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
TMBOY 47:9e361da97763 32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
TMBOY 47:9e361da97763 33
TMBOY 47:9e361da97763 34 */
TMBOY 47:9e361da97763 35
TMBOY 47:9e361da97763 36 #ifndef NB_CELP_H
TMBOY 47:9e361da97763 37 #define NB_CELP_H
TMBOY 47:9e361da97763 38
TMBOY 47:9e361da97763 39 #include "modes.h"
TMBOY 47:9e361da97763 40 #include <speex/speex_bits.h>
TMBOY 47:9e361da97763 41 #include <speex/speex_callbacks.h>
TMBOY 47:9e361da97763 42 #include "vbr.h"
TMBOY 47:9e361da97763 43 #include "filters.h"
TMBOY 47:9e361da97763 44
TMBOY 47:9e361da97763 45 #ifdef VORBIS_PSYCHO
TMBOY 47:9e361da97763 46 #include "vorbis_psy.h"
TMBOY 47:9e361da97763 47 #endif
TMBOY 47:9e361da97763 48
TMBOY 47:9e361da97763 49 /**Structure representing the full state of the narrowband encoder*/
TMBOY 47:9e361da97763 50 typedef struct EncState {
TMBOY 47:9e361da97763 51 const SpeexMode *mode; /**< Mode corresponding to the state */
TMBOY 47:9e361da97763 52 int first; /**< Is this the first frame? */
TMBOY 47:9e361da97763 53 int frameSize; /**< Size of frames */
TMBOY 47:9e361da97763 54 int subframeSize; /**< Size of sub-frames */
TMBOY 47:9e361da97763 55 int nbSubframes; /**< Number of sub-frames */
TMBOY 47:9e361da97763 56 int windowSize; /**< Analysis (LPC) window length */
TMBOY 47:9e361da97763 57 int lpcSize; /**< LPC order */
TMBOY 47:9e361da97763 58 int min_pitch; /**< Minimum pitch value allowed */
TMBOY 47:9e361da97763 59 int max_pitch; /**< Maximum pitch value allowed */
TMBOY 47:9e361da97763 60
TMBOY 47:9e361da97763 61 spx_word32_t cumul_gain; /**< Product of previously used pitch gains (Q10) */
TMBOY 47:9e361da97763 62 int bounded_pitch; /**< Next frame should not rely on previous frames for pitch */
TMBOY 47:9e361da97763 63 int ol_pitch; /**< Open-loop pitch */
TMBOY 47:9e361da97763 64 int ol_voiced; /**< Open-loop voiced/non-voiced decision */
TMBOY 47:9e361da97763 65 int *pitch;
TMBOY 47:9e361da97763 66
TMBOY 47:9e361da97763 67 #ifdef EPIC_48K
TMBOY 47:9e361da97763 68 int lbr_48k;
TMBOY 47:9e361da97763 69 #endif
TMBOY 47:9e361da97763 70
TMBOY 47:9e361da97763 71 #ifdef VORBIS_PSYCHO
TMBOY 47:9e361da97763 72 VorbisPsy *psy;
TMBOY 47:9e361da97763 73 float *psy_window;
TMBOY 47:9e361da97763 74 float *curve;
TMBOY 47:9e361da97763 75 float *old_curve;
TMBOY 47:9e361da97763 76 #endif
TMBOY 47:9e361da97763 77
TMBOY 47:9e361da97763 78 spx_word16_t gamma1; /**< Perceptual filter: A(z/gamma1) */
TMBOY 47:9e361da97763 79 spx_word16_t gamma2; /**< Perceptual filter: A(z/gamma2) */
TMBOY 47:9e361da97763 80 float lag_factor; /**< Lag windowing Gaussian width */
TMBOY 47:9e361da97763 81 spx_word16_t lpc_floor; /**< Noise floor multiplier for A[0] in LPC analysis*/
TMBOY 47:9e361da97763 82 char *stack; /**< Pseudo-stack allocation for temporary memory */
TMBOY 47:9e361da97763 83 spx_word16_t *winBuf; /**< Input buffer (original signal) */
TMBOY 47:9e361da97763 84 spx_word16_t *excBuf; /**< Excitation buffer */
TMBOY 47:9e361da97763 85 spx_word16_t *exc; /**< Start of excitation frame */
TMBOY 47:9e361da97763 86 spx_word16_t *swBuf; /**< Weighted signal buffer */
TMBOY 47:9e361da97763 87 spx_word16_t *sw; /**< Start of weighted signal frame */
TMBOY 47:9e361da97763 88 const spx_word16_t *window; /**< Temporary (Hanning) window */
TMBOY 47:9e361da97763 89 spx_word16_t *lagWindow; /**< Window applied to auto-correlation */
TMBOY 47:9e361da97763 90 spx_lsp_t *old_lsp; /**< LSPs for previous frame */
TMBOY 47:9e361da97763 91 spx_lsp_t *old_qlsp; /**< Quantized LSPs for previous frame */
TMBOY 47:9e361da97763 92 spx_mem_t *mem_sp; /**< Filter memory for signal synthesis */
TMBOY 47:9e361da97763 93 spx_mem_t *mem_sw; /**< Filter memory for perceptually-weighted signal */
TMBOY 47:9e361da97763 94 spx_mem_t *mem_sw_whole; /**< Filter memory for perceptually-weighted signal (whole frame)*/
TMBOY 47:9e361da97763 95 spx_mem_t *mem_exc; /**< Filter memory for excitation (whole frame) */
TMBOY 47:9e361da97763 96 spx_mem_t *mem_exc2; /**< Filter memory for excitation (whole frame) */
TMBOY 47:9e361da97763 97 spx_mem_t mem_hp[2]; /**< High-pass filter memory */
TMBOY 47:9e361da97763 98 spx_word32_t *pi_gain; /**< Gain of LPC filter at theta=pi (fe/2) */
TMBOY 47:9e361da97763 99 spx_word16_t *innov_rms_save; /**< If non-NULL, innovation RMS is copied here */
TMBOY 47:9e361da97763 100
TMBOY 47:9e361da97763 101 VBRState *vbr; /**< State of the VBR data */
TMBOY 47:9e361da97763 102 float vbr_quality; /**< Quality setting for VBR encoding */
TMBOY 47:9e361da97763 103 float relative_quality; /**< Relative quality that will be needed by VBR */
TMBOY 47:9e361da97763 104 spx_int32_t vbr_enabled; /**< 1 for enabling VBR, 0 otherwise */
TMBOY 47:9e361da97763 105 spx_int32_t vbr_max; /**< Max bit-rate allowed in VBR mode */
TMBOY 47:9e361da97763 106 int vad_enabled; /**< 1 for enabling VAD, 0 otherwise */
TMBOY 47:9e361da97763 107 int dtx_enabled; /**< 1 for enabling DTX, 0 otherwise */
TMBOY 47:9e361da97763 108 int dtx_count; /**< Number of consecutive DTX frames */
TMBOY 47:9e361da97763 109 spx_int32_t abr_enabled; /**< ABR setting (in bps), 0 if off */
TMBOY 47:9e361da97763 110 float abr_drift;
TMBOY 47:9e361da97763 111 float abr_drift2;
TMBOY 47:9e361da97763 112 float abr_count;
TMBOY 47:9e361da97763 113 int complexity; /**< Complexity setting (0-10 from least complex to most complex) */
TMBOY 47:9e361da97763 114 spx_int32_t sampling_rate;
TMBOY 47:9e361da97763 115 int plc_tuning;
TMBOY 47:9e361da97763 116 int encode_submode;
TMBOY 47:9e361da97763 117 const SpeexSubmode * const *submodes; /**< Sub-mode data */
TMBOY 47:9e361da97763 118 int submodeID; /**< Activated sub-mode */
TMBOY 47:9e361da97763 119 int submodeSelect; /**< Mode chosen by the user (may differ from submodeID if VAD is on) */
TMBOY 47:9e361da97763 120 int isWideband; /**< Is this used as part of the embedded wideband codec */
TMBOY 47:9e361da97763 121 int highpass_enabled; /**< Is the input filter enabled */
TMBOY 47:9e361da97763 122 } EncState;
TMBOY 47:9e361da97763 123
TMBOY 47:9e361da97763 124 /**Structure representing the full state of the narrowband decoder*/
TMBOY 47:9e361da97763 125 typedef struct DecState {
TMBOY 47:9e361da97763 126 const SpeexMode *mode; /**< Mode corresponding to the state */
TMBOY 47:9e361da97763 127 int first; /**< Is this the first frame? */
TMBOY 47:9e361da97763 128 int count_lost; /**< Was the last frame lost? */
TMBOY 47:9e361da97763 129 int frameSize; /**< Size of frames */
TMBOY 47:9e361da97763 130 int subframeSize; /**< Size of sub-frames */
TMBOY 47:9e361da97763 131 int nbSubframes; /**< Number of sub-frames */
TMBOY 47:9e361da97763 132 int lpcSize; /**< LPC order */
TMBOY 47:9e361da97763 133 int min_pitch; /**< Minimum pitch value allowed */
TMBOY 47:9e361da97763 134 int max_pitch; /**< Maximum pitch value allowed */
TMBOY 47:9e361da97763 135 spx_int32_t sampling_rate;
TMBOY 47:9e361da97763 136
TMBOY 47:9e361da97763 137 #ifdef EPIC_48K
TMBOY 47:9e361da97763 138 int lbr_48k;
TMBOY 47:9e361da97763 139 #endif
TMBOY 47:9e361da97763 140
TMBOY 47:9e361da97763 141 spx_word16_t last_ol_gain; /**< Open-loop gain for previous frame */
TMBOY 47:9e361da97763 142
TMBOY 47:9e361da97763 143 char *stack; /**< Pseudo-stack allocation for temporary memory */
TMBOY 47:9e361da97763 144 spx_word16_t *excBuf; /**< Excitation buffer */
TMBOY 47:9e361da97763 145 spx_word16_t *exc; /**< Start of excitation frame */
TMBOY 47:9e361da97763 146 spx_lsp_t *old_qlsp; /**< Quantized LSPs for previous frame */
TMBOY 47:9e361da97763 147 spx_coef_t *interp_qlpc; /**< Interpolated quantized LPCs */
TMBOY 47:9e361da97763 148 spx_mem_t *mem_sp; /**< Filter memory for synthesis signal */
TMBOY 47:9e361da97763 149 spx_mem_t mem_hp[2]; /**< High-pass filter memory */
TMBOY 47:9e361da97763 150 spx_word32_t *pi_gain; /**< Gain of LPC filter at theta=pi (fe/2) */
TMBOY 47:9e361da97763 151 spx_word16_t *innov_save; /** If non-NULL, innovation is copied here */
TMBOY 47:9e361da97763 152
TMBOY 47:9e361da97763 153 spx_word16_t level;
TMBOY 47:9e361da97763 154 spx_word16_t max_level;
TMBOY 47:9e361da97763 155 spx_word16_t min_level;
TMBOY 47:9e361da97763 156
TMBOY 47:9e361da97763 157 /* This is used in packet loss concealment */
TMBOY 47:9e361da97763 158 int last_pitch; /**< Pitch of last correctly decoded frame */
TMBOY 47:9e361da97763 159 spx_word16_t last_pitch_gain; /**< Pitch gain of last correctly decoded frame */
TMBOY 47:9e361da97763 160 spx_word16_t pitch_gain_buf[3]; /**< Pitch gain of last decoded frames */
TMBOY 47:9e361da97763 161 int pitch_gain_buf_idx; /**< Tail of the buffer */
TMBOY 47:9e361da97763 162 spx_int32_t seed; /** Seed used for random number generation */
TMBOY 47:9e361da97763 163
TMBOY 47:9e361da97763 164 int encode_submode;
TMBOY 47:9e361da97763 165 const SpeexSubmode * const *submodes; /**< Sub-mode data */
TMBOY 47:9e361da97763 166 int submodeID; /**< Activated sub-mode */
TMBOY 47:9e361da97763 167 int lpc_enh_enabled; /**< 1 when LPC enhancer is on, 0 otherwise */
TMBOY 47:9e361da97763 168 SpeexCallback speex_callbacks[SPEEX_MAX_CALLBACKS];
TMBOY 47:9e361da97763 169
TMBOY 47:9e361da97763 170 SpeexCallback user_callback;
TMBOY 47:9e361da97763 171
TMBOY 47:9e361da97763 172 /*Vocoder data*/
TMBOY 47:9e361da97763 173 spx_word16_t voc_m1;
TMBOY 47:9e361da97763 174 spx_word32_t voc_m2;
TMBOY 47:9e361da97763 175 float voc_mean;
TMBOY 47:9e361da97763 176 int voc_offset;
TMBOY 47:9e361da97763 177
TMBOY 47:9e361da97763 178 int dtx_enabled;
TMBOY 47:9e361da97763 179 int isWideband; /**< Is this used as part of the embedded wideband codec */
TMBOY 47:9e361da97763 180 int highpass_enabled; /**< Is the input filter enabled */
TMBOY 47:9e361da97763 181 } DecState;
TMBOY 47:9e361da97763 182
TMBOY 47:9e361da97763 183 /** Initializes encoder state*/
TMBOY 47:9e361da97763 184 void *nb_encoder_init(const SpeexMode *m);
TMBOY 47:9e361da97763 185
TMBOY 47:9e361da97763 186 /** De-allocates encoder state resources*/
TMBOY 47:9e361da97763 187 void nb_encoder_destroy(void *state);
TMBOY 47:9e361da97763 188
TMBOY 47:9e361da97763 189 /** Encodes one frame*/
TMBOY 47:9e361da97763 190 int nb_encode(void *state, void *in, SpeexBits *bits);
TMBOY 47:9e361da97763 191
TMBOY 47:9e361da97763 192
TMBOY 47:9e361da97763 193 /** Initializes decoder state*/
TMBOY 47:9e361da97763 194 void *nb_decoder_init(const SpeexMode *m);
TMBOY 47:9e361da97763 195
TMBOY 47:9e361da97763 196 /** De-allocates decoder state resources*/
TMBOY 47:9e361da97763 197 void nb_decoder_destroy(void *state);
TMBOY 47:9e361da97763 198
TMBOY 47:9e361da97763 199 /** Decodes one frame*/
TMBOY 47:9e361da97763 200 int nb_decode(void *state, SpeexBits *bits, void *out);
TMBOY 47:9e361da97763 201
TMBOY 47:9e361da97763 202 /** ioctl-like function for controlling a narrowband encoder */
TMBOY 47:9e361da97763 203 int nb_encoder_ctl(void *state, int request, void *ptr);
TMBOY 47:9e361da97763 204
TMBOY 47:9e361da97763 205 /** ioctl-like function for controlling a narrowband decoder */
TMBOY 47:9e361da97763 206 int nb_decoder_ctl(void *state, int request, void *ptr);
TMBOY 47:9e361da97763 207
TMBOY 47:9e361da97763 208
TMBOY 47:9e361da97763 209 #endif