Asterisk - The Open Source Telephony Project  18.5.0
alaw.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 A-Law to Signed linear conversion
21  */
22 
23 #ifndef _ASTERISK_ALAW_H
24 #define _ASTERISK_ALAW_H
25 
26 
27 /*! \brief
28  * To init the alaw to slinear conversion stuff, this needs to be run.
29  */
30 void ast_alaw_init(void);
31 
32 #define AST_ALAW_BIT_LOSS 4
33 #define AST_ALAW_STEP (1 << AST_ALAW_BIT_LOSS)
34 #define AST_ALAW_TAB_SIZE (32768 / AST_ALAW_STEP + 1)
35 #define AST_ALAW_SIGN_BIT 0x80
36 #define AST_ALAW_AMI_MASK 0x55
37 
38 
39 /*! \brief converts signed linear to alaw */
40 #ifndef G711_NEW_ALGORITHM
41 extern unsigned char __ast_lin2a[8192];
42 #else
43 extern unsigned char __ast_lin2a[AST_ALAW_TAB_SIZE];
44 #endif
45 
46 /*! help */
47 extern short __ast_alaw[256];
48 
49 #ifndef G711_NEW_ALGORITHM
50 #define AST_LIN2A(a) (__ast_lin2a[((unsigned short)(a)) >> 3])
51 #else
52 #define AST_LIN2A_LOOKUP(mag) \
53  __ast_lin2a[(mag) >> AST_ALAW_BIT_LOSS]
54 
55 /*! \brief Convert signed linear sample to sign-magnitude pair for a-Law */
56 static inline void ast_alaw_get_sign_mag(short sample, unsigned *sign, unsigned *mag)
57 {
58  /* It may look illogical to retrive the sign this way in both cases,
59  * but this helps gcc eliminate the branch below and produces
60  * faster code */
61  *sign = ((unsigned short)sample >> 8) & AST_ALAW_SIGN_BIT;
62 #if defined(G711_REDUCED_BRANCHING)
63  {
64  unsigned dual_mag = (-sample << 16) | (unsigned short)sample;
65  *mag = (dual_mag >> (*sign >> 3)) & 0xffffU;
66  }
67 #else
68  if (sample < 0)
69  *mag = -sample;
70  else
71  *mag = sample;
72 #endif /* G711_REDUCED_BRANCHING */
73  *sign ^= AST_ALAW_SIGN_BIT;
74 }
75 
76 static inline unsigned char AST_LIN2A(short sample)
77 {
78  unsigned mag, sign;
79  ast_alaw_get_sign_mag(sample, &sign, &mag);
80  return (sign | AST_LIN2A_LOOKUP(mag)) ^ AST_ALAW_AMI_MASK;
81 }
82 #endif
83 
84 #define AST_ALAW(a) (__ast_alaw[(int)(a)])
85 
86 #endif /* _ASTERISK_ALAW_H */
short __ast_alaw[256]
Definition: alaw.c:150
unsigned char __ast_lin2a[8192]
converts signed linear to alaw
Definition: alaw.c:146
#define AST_ALAW_AMI_MASK
Definition: alaw.h:36
void ast_alaw_init(void)
To init the alaw to slinear conversion stuff, this needs to be run.
Definition: alaw.c:152
#define AST_ALAW_SIGN_BIT
Definition: alaw.h:35
#define AST_ALAW_TAB_SIZE
Definition: alaw.h:34
#define AST_LIN2A(a)
Definition: alaw.h:50