Asterisk - The Open Source Telephony Project  18.5.0
ulaw.h
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <[email protected]>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  * \brief u-Law to Signed linear conversion
21  */
22 
23 #ifndef _ASTERISK_ULAW_H
24 #define _ASTERISK_ULAW_H
25 
26 
27 /*!
28  * To init the ulaw to slinear conversion stuff, this needs to be run.
29  */
30 void ast_ulaw_init(void);
31 
32 #define AST_ULAW_BIT_LOSS 3
33 #define AST_ULAW_STEP (1 << AST_ULAW_BIT_LOSS)
34 #define AST_ULAW_TAB_SIZE (32768 / AST_ULAW_STEP + 1)
35 #define AST_ULAW_SIGN_BIT 0x80
36 
37 /*! \brief converts signed linear to mulaw */
38 #ifndef G711_NEW_ALGORITHM
39 extern unsigned char __ast_lin2mu[16384];
40 #else
41 extern unsigned char __ast_lin2mu[AST_ULAW_TAB_SIZE];
42 #endif
43 
44 /*! help */
45 extern short __ast_mulaw[256];
46 
47 #ifndef G711_NEW_ALGORITHM
48 
49 #define AST_LIN2MU(a) (__ast_lin2mu[((unsigned short)(a)) >> 2])
50 
51 #else
52 
53 #define AST_LIN2MU_LOOKUP(mag) \
54  __ast_lin2mu[((mag) + AST_ULAW_STEP / 2) >> AST_ULAW_BIT_LOSS]
55 
56 
57 /*! \brief convert signed linear sample to sign-magnitude pair for u-Law */
58 static inline void ast_ulaw_get_sign_mag(short sample, unsigned *sign, unsigned *mag)
59 {
60  /* It may look illogical to retrive the sign this way in both cases,
61  * but this helps gcc eliminate the branch below and produces
62  * faster code */
63  *sign = ((unsigned short)sample >> 8) & AST_ULAW_SIGN_BIT;
64 #if defined(G711_REDUCED_BRANCHING)
65  {
66  unsigned dual_mag = (-sample << 16) | (unsigned short)sample;
67  *mag = (dual_mag >> (*sign >> 3)) & 0xffffU;
68  }
69 #else
70  if (sample < 0)
71  *mag = -sample;
72  else
73  *mag = sample;
74 #endif /* G711_REDUCED_BRANCHING */
75 }
76 
77 static inline unsigned char AST_LIN2MU(short sample)
78 {
79  unsigned mag, sign;
80  ast_ulaw_get_sign_mag(sample, &sign, &mag);
81  return ~(sign | AST_LIN2MU_LOOKUP(mag));
82 }
83 #endif
84 
85 #define AST_MULAW(a) (__ast_mulaw[(a)])
86 
87 #endif /* _ASTERISK_ULAW_H */
short __ast_mulaw[256]
Definition: ulaw.c:50
void ast_ulaw_init(void)
Set up mu-law conversion table.
Definition: ulaw.c:173
unsigned char __ast_lin2mu[16384]
converts signed linear to mulaw
Definition: ulaw.c:49
#define AST_ULAW_SIGN_BIT
Definition: ulaw.h:35
#define AST_ULAW_TAB_SIZE
Definition: ulaw.h:34
#define AST_LIN2MU(a)
Definition: ulaw.h:49