1/* 2 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische 3 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for 4 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE. 5 */ 6 7#ifndef PRIVATE_H 8#define PRIVATE_H 9 10#include <stdint.h> 11 12/* Added by Erik de Castro Lopo */ 13#define USE_FLOAT_MUL 14#define FAST 15#define WAV49 16 17#ifdef __cplusplus 18#error "This code is not designed to be compiled with a C++ compiler." 19#endif 20/* Added by Erik de Castro Lopo */ 21 22struct gsm_state 23{ int16_t dp0 [280] ; 24 25 int16_t z1 ; /* preprocessing.c, Offset_com. */ 26 int32_t L_z2 ; /* Offset_com. */ 27 int mp ; /* Preemphasis */ 28 29 int16_t u [8] ; /* short_term_aly_filter.c */ 30 int16_t LARpp [2][8] ; /* */ 31 int16_t j ; /* */ 32 33 int16_t ltp_cut ; /* long_term.c, LTP crosscorr. */ 34 int16_t nrp ; /* 40 */ /* long_term.c, synthesis */ 35 int16_t v [9] ; /* short_term.c, synthesis */ 36 int16_t msr ; /* decoder.c, Postprocessing */ 37 38 char verbose ; /* only used if !NDEBUG */ 39 char fast ; /* only used if FAST */ 40 41 char wav_fmt ; /* only used if WAV49 defined */ 42 unsigned char frame_index ; /* odd/even chaining */ 43 unsigned char frame_chain ; /* half-byte to carry forward */ 44 45 /* Moved here from code.c where it was defined as static */ 46 int16_t e [50] ; 47} ; 48 49typedef struct gsm_state GSM_STATE ; 50 51#define MIN_WORD (-32767 - 1) 52#define MAX_WORD 32767 53 54#define MIN_LONGWORD (-2147483647 - 1) 55#define MAX_LONGWORD 2147483647 56 57/* Signed arithmetic shift right. */ 58static inline int16_t 59SASR_W (int16_t x, int16_t by) 60{ if (x >= 0) 61 return x >> by ; 62 return ~ ((~x) >> by) ; 63} /* SASR_W */ 64 65static inline int32_t 66SASR_L (int32_t x, int16_t by) 67{ if (x >= 0) 68 return x >> by ; 69 return ~ ((~x) >> by) ; 70} /* SASR_L */ 71 72/* Signed arithmetic shift left. */ 73static inline int16_t 74SASL_W (int16_t x, int16_t by) 75{ if (x >= 0) 76 return x << by ; 77 return - ((-x) << by) ; 78} /* SASR_W */ 79 80static inline int32_t 81SASL_L (int32_t x, int16_t by) 82{ if (x >= 0) 83 return x << by ; 84 return - ((-x) << by) ; 85} /* SASR_L */ 86 87/* 88 * Prototypes from add.c 89 */ 90int16_t gsm_mult (int16_t a, int16_t b) ; 91int32_t gsm_L_mult (int16_t a, int16_t b) ; 92int16_t gsm_mult_r (int16_t a, int16_t b) ; 93 94int16_t gsm_div (int16_t num, int16_t denum) ; 95 96int16_t gsm_add (int16_t a, int16_t b) ; 97int32_t gsm_L_add (int32_t a, int32_t b) ; 98 99int16_t gsm_sub (int16_t a, int16_t b) ; 100int32_t gsm_L_sub (int32_t a, int32_t b) ; 101 102int16_t gsm_abs (int16_t a) ; 103 104int16_t gsm_norm (int32_t a) ; 105 106int32_t gsm_L_asl (int32_t a, int n) ; 107int16_t gsm_asl (int16_t a, int n) ; 108 109int32_t gsm_L_asr (int32_t a, int n) ; 110int16_t gsm_asr (int16_t a, int n) ; 111 112/* 113 * Inlined functions from add.h 114 */ 115 116static inline int32_t 117GSM_MULT_R (int16_t a, int16_t b) 118{ return (((int32_t) (a)) * ((int32_t) (b)) + 16384) >> 15 ; 119} /* GSM_MULT_R */ 120 121static inline int32_t 122GSM_MULT (int16_t a, int16_t b) 123{ return (((int32_t) (a)) * ((int32_t) (b))) >> 15 ; 124} /* GSM_MULT */ 125 126static inline int32_t 127GSM_L_MULT (int16_t a, int16_t b) 128{ return ((int32_t) (a)) * ((int32_t) (b)) << 1 ; 129} /* GSM_L_MULT */ 130 131static inline int32_t 132GSM_L_ADD (int32_t a, int32_t b) 133{ uint32_t utmp ; 134 135 if (a < 0 && b < 0) 136 { utmp = (uint32_t) - ((a) + 1) + (uint32_t) - ((b) + 1) ; 137 return (utmp >= (uint32_t) MAX_LONGWORD) ? MIN_LONGWORD : - (int32_t) utmp - 2 ; 138 } ; 139 140 if (a > 0 && b > 0) 141 { utmp = (uint32_t) a + (uint32_t) b ; 142 return (utmp >= (uint32_t) MAX_LONGWORD) ? MAX_LONGWORD : utmp ; 143 } ; 144 145 return a + b ; 146} /* GSM_L_ADD */ 147 148static inline int32_t 149GSM_ADD (int16_t a, int16_t b) 150{ int32_t ltmp ; 151 152 ltmp = ((int32_t) a) + ((int32_t) b) ; 153 154 if (ltmp >= MAX_WORD) 155 return MAX_WORD ; 156 if (ltmp <= MIN_WORD) 157 return MIN_WORD ; 158 159 return ltmp ; 160} /* GSM_ADD */ 161 162static inline int32_t 163GSM_SUB (int16_t a, int16_t b) 164{ int32_t ltmp ; 165 166 ltmp = ((int32_t) a) - ((int32_t) b) ; 167 168 if (ltmp >= MAX_WORD) 169 ltmp = MAX_WORD ; 170 else if (ltmp <= MIN_WORD) 171 ltmp = MIN_WORD ; 172 173 return ltmp ; 174} /* GSM_SUB */ 175 176static inline int16_t 177GSM_ABS (int16_t a) 178{ 179 if (a > 0) 180 return a ; 181 if (a == MIN_WORD) 182 return MAX_WORD ; 183 return -a ; 184} /* GSM_ADD */ 185 186 187/* 188 * More prototypes from implementations.. 189 */ 190void Gsm_Coder ( 191 struct gsm_state * S, 192 int16_t * s, /* [0..159] samples IN */ 193 int16_t * LARc, /* [0..7] LAR coefficients OUT */ 194 int16_t * Nc, /* [0..3] LTP lag OUT */ 195 int16_t * bc, /* [0..3] coded LTP gain OUT */ 196 int16_t * Mc, /* [0..3] RPE grid selection OUT */ 197 int16_t * xmaxc, /* [0..3] Coded maximum amplitude OUT */ 198 int16_t * xMc) ; /* [13*4] normalized RPE samples OUT */ 199 200void Gsm_Long_Term_Predictor ( /* 4x for 160 samples */ 201 struct gsm_state * S, 202 int16_t * d, /* [0..39] residual signal IN */ 203 int16_t * dp, /* [-120..-1] d' IN */ 204 int16_t * e, /* [0..40] OUT */ 205 int16_t * dpp, /* [0..40] OUT */ 206 int16_t * Nc, /* correlation lag OUT */ 207 int16_t * bc) ; /* gain factor OUT */ 208 209void Gsm_LPC_Analysis ( 210 struct gsm_state * S, 211 int16_t * s, /* 0..159 signals IN/OUT */ 212 int16_t * LARc) ; /* 0..7 LARc's OUT */ 213 214void Gsm_Preprocess ( 215 struct gsm_state * S, 216 int16_t * s, int16_t * so) ; 217 218void Gsm_Encoding ( 219 struct gsm_state * S, 220 int16_t * e, 221 int16_t * ep, 222 int16_t * xmaxc, 223 int16_t * Mc, 224 int16_t * xMc) ; 225 226void Gsm_Short_Term_Analysis_Filter ( 227 struct gsm_state * S, 228 int16_t * LARc, /* coded log area ratio [0..7] IN */ 229 int16_t * d) ; /* st res. signal [0..159] IN/OUT */ 230 231void Gsm_Decoder ( 232 struct gsm_state * S, 233 int16_t * LARcr, /* [0..7] IN */ 234 int16_t * Ncr, /* [0..3] IN */ 235 int16_t * bcr, /* [0..3] IN */ 236 int16_t * Mcr, /* [0..3] IN */ 237 int16_t * xmaxcr, /* [0..3] IN */ 238 int16_t * xMcr, /* [0..13*4] IN */ 239 int16_t * s) ; /* [0..159] OUT */ 240 241void Gsm_Decoding ( 242 struct gsm_state * S, 243 int16_t xmaxcr, 244 int16_t Mcr, 245 int16_t * xMcr, /* [0..12] IN */ 246 int16_t * erp) ; /* [0..39] OUT */ 247 248void Gsm_Long_Term_Synthesis_Filtering ( 249 struct gsm_state* S, 250 int16_t Ncr, 251 int16_t bcr, 252 int16_t * erp, /* [0..39] IN */ 253 int16_t * drp) ; /* [-120..-1] IN, [0..40] OUT */ 254 255void Gsm_RPE_Decoding ( 256 /*-struct gsm_state *S,-*/ 257 int16_t xmaxcr, 258 int16_t Mcr, 259 int16_t * xMcr, /* [0..12], 3 bits IN */ 260 int16_t * erp) ; /* [0..39] OUT */ 261 262void Gsm_RPE_Encoding ( 263 /*-struct gsm_state * S,-*/ 264 int16_t * e, /* -5..-1][0..39][40..44 IN/OUT */ 265 int16_t * xmaxc, /* OUT */ 266 int16_t * Mc, /* OUT */ 267 int16_t * xMc) ; /* [0..12] OUT */ 268 269void Gsm_Short_Term_Synthesis_Filter ( 270 struct gsm_state * S, 271 int16_t * LARcr, /* log area ratios [0..7] IN */ 272 int16_t * drp, /* received d [0...39] IN */ 273 int16_t * s) ; /* signal s [0..159] OUT */ 274 275void Gsm_Update_of_reconstructed_short_time_residual_signal ( 276 int16_t * dpp, /* [0...39] IN */ 277 int16_t * ep, /* [0...39] IN */ 278 int16_t * dp) ; /* [-120...-1] IN/OUT */ 279 280/* 281 * Tables from table.c 282 */ 283#ifndef GSM_TABLE_C 284 285extern int16_t gsm_A [8], gsm_B [8], gsm_MIC [8], gsm_MAC [8] ; 286extern int16_t gsm_INVA [8] ; 287extern int16_t gsm_DLB [4], gsm_QLB [4] ; 288extern int16_t gsm_H [11] ; 289extern int16_t gsm_NRFAC [8] ; 290extern int16_t gsm_FAC [8] ; 291 292#endif /* GSM_TABLE_C */ 293 294 295#if __GNUC__ 296#define ALWAYS_INLINE __attribute__ ((always_inline)) 297#elif defined _MSC_VER 298#define ALWAYS_INLINE __forceinline 299#else 300#define ALWAYS_INLINE 301#endif 302 303 304static inline int32_t ALWAYS_INLINE 305arith_shift_left (int32_t x, int shift) 306{ return (int32_t) (((uint32_t) x) << shift) ; 307} /* arith_shift_left */ 308 309static inline int32_t ALWAYS_INLINE 310arith_shift_right (int32_t x, int shift) 311{ if (x >= 0) 312 return x << shift ; 313 return ~ ((~x) << shift) ; 314} /* arith_shift_right */ 315 316 317/* 318 * Debugging 319 */ 320#ifdef NDEBUG 321 322# define gsm_debug_int16_ts(a, b, c, d) /* nil */ 323# define gsm_debug_int32_ts(a, b, c, d) /* nil */ 324# define gsm_debug_int16_t(a, b) /* nil */ 325# define gsm_debug_int32_t(a, b) /* nil */ 326 327#else /* !NDEBUG => DEBUG */ 328 329 void gsm_debug_int16_ts (char * name, int, int, int16_t *) ; 330 void gsm_debug_int32_ts (char * name, int, int, int32_t *) ; 331 void gsm_debug_int32_t (char * name, int32_t) ; 332 void gsm_debug_int16_t (char * name, int16_t) ; 333 334#endif /* !NDEBUG */ 335 336#endif /* PRIVATE_H */ 337 338