Main Page | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

g711.h

Go to the documentation of this file.
00001 /*
00002  * SpanDSP - a series of DSP components for telephony
00003  *
00004  * g711.h - In line A-law and u-law conversion routines
00005  *
00006  * Written by Steve Underwood <steveu@coppice.org>
00007  *
00008  * Copyright (C) 2001 Steve Underwood
00009  *
00010  * All rights reserved.
00011  *
00012  * This program is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU Lesser General Public License version 2.1,
00014  * as published by the Free Software Foundation.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00024  *
00025  * $Id: g711.h,v 1.18 2009/02/10 13:06:47 steveu Exp $
00026  */
00027 
00028 /*! \file */
00029 
00030 /*! \page g711_page A-law and mu-law handling
00031 Lookup tables for A-law and u-law look attractive, until you consider the impact
00032 on the CPU cache. If it causes a substantial area of your processor cache to get
00033 hit too often, cache sloshing will severely slow things down. The main reason
00034 these routines are slow in C, is the lack of direct access to the CPU's "find
00035 the first 1" instruction. A little in-line assembler fixes that, and the
00036 conversion routines can be faster than lookup tables, in most real world usage.
00037 A "find the first 1" instruction is available on most modern CPUs, and is a
00038 much underused feature. 
00039 
00040 If an assembly language method of bit searching is not available, these routines
00041 revert to a method that can be a little slow, so the cache thrashing might not
00042 seem so bad :(
00043 
00044 Feel free to submit patches to add fast "find the first 1" support for your own
00045 favourite processor.
00046 
00047 Look up tables are used for transcoding between A-law and u-law, since it is
00048 difficult to achieve the precise transcoding procedure laid down in the G.711
00049 specification by other means.
00050 */
00051 
00052 #if !defined(_SPANDSP_G711_H_)
00053 #define _SPANDSP_G711_H_
00054 
00055 /* The usual values to use on idle channels, to emulate silence */
00056 #define G711_ALAW_IDLE_OCTET        0x5D
00057 #define G711_ULAW_IDLE_OCTET        0xFF
00058 
00059 enum
00060 {
00061     G711_ALAW = 0,
00062     G711_ULAW
00063 };
00064 
00065 typedef struct g711_state_s g711_state_t;
00066 
00067 #if defined(__cplusplus)
00068 extern "C"
00069 {
00070 #endif
00071 
00072 /* N.B. It is tempting to use look-up tables for A-law and u-law conversion.
00073  *      However, you should consider the cache footprint.
00074  *
00075  *      A 64K byte table for linear to x-law and a 512 byte table for x-law to
00076  *      linear sound like peanuts these days, and shouldn't an array lookup be
00077  *      real fast? No! When the cache sloshes as badly as this one will, a tight
00078  *      calculation may be better. The messiest part is normally finding the
00079  *      segment, but a little inline assembly can fix that on an i386, x86_64 and
00080  *      many other modern processors.
00081  */
00082  
00083 /*
00084  * Mu-law is basically as follows:
00085  *
00086  *      Biased Linear Input Code        Compressed Code
00087  *      ------------------------        ---------------
00088  *      00000001wxyza                   000wxyz
00089  *      0000001wxyzab                   001wxyz
00090  *      000001wxyzabc                   010wxyz
00091  *      00001wxyzabcd                   011wxyz
00092  *      0001wxyzabcde                   100wxyz
00093  *      001wxyzabcdef                   101wxyz
00094  *      01wxyzabcdefg                   110wxyz
00095  *      1wxyzabcdefgh                   111wxyz
00096  *
00097  * Each biased linear code has a leading 1 which identifies the segment
00098  * number. The value of the segment number is equal to 7 minus the number
00099  * of leading 0's. The quantization interval is directly available as the
00100  * four bits wxyz.  * The trailing bits (a - h) are ignored.
00101  *
00102  * Ordinarily the complement of the resulting code word is used for
00103  * transmission, and so the code word is complemented before it is returned.
00104  *
00105  * For further information see John C. Bellamy's Digital Telephony, 1982,
00106  * John Wiley & Sons, pps 98-111 and 472-476.
00107  */
00108 
00109 //#define ULAW_ZEROTRAP                 /* turn on the trap as per the MIL-STD */
00110 #define ULAW_BIAS        0x84           /* Bias for linear code. */
00111 
00112 /*! \brief Encode a linear sample to u-law
00113     \param linear The sample to encode.
00114     \return The u-law value.
00115 */
00116 static __inline__ uint8_t linear_to_ulaw(int linear)
00117 {
00118     uint8_t u_val;
00119     int mask;
00120     int seg;
00121 
00122     /* Get the sign and the magnitude of the value. */
00123     if (linear >= 0)
00124     {
00125         linear = ULAW_BIAS + linear;
00126         mask = 0xFF;
00127     }
00128     else
00129     {
00130         linear = ULAW_BIAS - linear;
00131         mask = 0x7F;
00132     }
00133 
00134     seg = top_bit(linear | 0xFF) - 7;
00135 
00136     /*
00137      * Combine the sign, segment, quantization bits,
00138      * and complement the code word.
00139      */
00140     if (seg >= 8)
00141         u_val = (uint8_t) (0x7F ^ mask);
00142     else
00143         u_val = (uint8_t) (((seg << 4) | ((linear >> (seg + 3)) & 0xF)) ^ mask);
00144 #ifdef ULAW_ZEROTRAP
00145     /* Optional ITU trap */
00146     if (u_val == 0)
00147         u_val = 0x02;
00148 #endif
00149     return  u_val;
00150 }
00151 /*- End of function --------------------------------------------------------*/
00152 
00153 /*! \brief Decode an u-law sample to a linear value.
00154     \param ulaw The u-law sample to decode.
00155     \return The linear value.
00156 */
00157 static __inline__ int16_t ulaw_to_linear(uint8_t ulaw)
00158 {
00159     int t;
00160     
00161     /* Complement to obtain normal u-law value. */
00162     ulaw = ~ulaw;
00163     /*
00164      * Extract and bias the quantization bits. Then
00165      * shift up by the segment number and subtract out the bias.
00166      */
00167     t = (((ulaw & 0x0F) << 3) + ULAW_BIAS) << (((int) ulaw & 0x70) >> 4);
00168     return  (int16_t) ((ulaw & 0x80)  ?  (ULAW_BIAS - t)  :  (t - ULAW_BIAS));
00169 }
00170 /*- End of function --------------------------------------------------------*/
00171 
00172 /*
00173  * A-law is basically as follows:
00174  *
00175  *      Linear Input Code        Compressed Code
00176  *      -----------------        ---------------
00177  *      0000000wxyza             000wxyz
00178  *      0000001wxyza             001wxyz
00179  *      000001wxyzab             010wxyz
00180  *      00001wxyzabc             011wxyz
00181  *      0001wxyzabcd             100wxyz
00182  *      001wxyzabcde             101wxyz
00183  *      01wxyzabcdef             110wxyz
00184  *      1wxyzabcdefg             111wxyz
00185  *
00186  * For further information see John C. Bellamy's Digital Telephony, 1982,
00187  * John Wiley & Sons, pps 98-111 and 472-476.
00188  */
00189 
00190 #define ALAW_AMI_MASK       0x55
00191 
00192 /*! \brief Encode a linear sample to A-law
00193     \param linear The sample to encode.
00194     \return The A-law value.
00195 */
00196 static __inline__ uint8_t linear_to_alaw(int linear)
00197 {
00198     int mask;
00199     int seg;
00200     
00201     if (linear >= 0)
00202     {
00203         /* Sign (bit 7) bit = 1 */
00204         mask = ALAW_AMI_MASK | 0x80;
00205     }
00206     else
00207     {
00208         /* Sign (bit 7) bit = 0 */
00209         mask = ALAW_AMI_MASK;
00210         linear = -linear - 1;
00211     }
00212 
00213     /* Convert the scaled magnitude to segment number. */
00214     seg = top_bit(linear | 0xFF) - 7;
00215     if (seg >= 8)
00216     {
00217         if (linear >= 0)
00218         {
00219             /* Out of range. Return maximum value. */
00220             return (uint8_t) (0x7F ^ mask);
00221         }
00222         /* We must be just a tiny step below zero */
00223         return (uint8_t) (0x00 ^ mask);
00224     }
00225     /* Combine the sign, segment, and quantization bits. */
00226     return (uint8_t) (((seg << 4) | ((linear >> ((seg)  ?  (seg + 3)  :  4)) & 0x0F)) ^ mask);
00227 }
00228 /*- End of function --------------------------------------------------------*/
00229 
00230 /*! \brief Decode an A-law sample to a linear value.
00231     \param alaw The A-law sample to decode.
00232     \return The linear value.
00233 */
00234 static __inline__ int16_t alaw_to_linear(uint8_t alaw)
00235 {
00236     int i;
00237     int seg;
00238 
00239     alaw ^= ALAW_AMI_MASK;
00240     i = ((alaw & 0x0F) << 4);
00241     seg = (((int) alaw & 0x70) >> 4);
00242     if (seg)
00243         i = (i + 0x108) << (seg - 1);
00244     else
00245         i += 8;
00246     return (int16_t) ((alaw & 0x80)  ?  i  :  -i);
00247 }
00248 /*- End of function --------------------------------------------------------*/
00249 
00250 /*! \brief Transcode from A-law to u-law, using the procedure defined in G.711.
00251     \param alaw The A-law sample to transcode.
00252     \return The best matching u-law value.
00253 */
00254 SPAN_DECLARE(uint8_t) alaw_to_ulaw(uint8_t alaw);
00255 
00256 /*! \brief Transcode from u-law to A-law, using the procedure defined in G.711.
00257     \param ulaw The u-law sample to transcode.
00258     \return The best matching A-law value.
00259 */
00260 SPAN_DECLARE(uint8_t) ulaw_to_alaw(uint8_t ulaw);
00261 
00262 SPAN_DECLARE(int) g711_decode(g711_state_t *s,
00263                               int16_t amp[],
00264                               const uint8_t g711_data[],
00265                               int g711_bytes);
00266 
00267 SPAN_DECLARE(int) g711_encode(g711_state_t *s,
00268                               uint8_t g711_data[],
00269                               const int16_t amp[],
00270                               int len);
00271 
00272 SPAN_DECLARE(int) g711_transcode(g711_state_t *s,
00273                                  uint8_t g711_out[],
00274                                  const uint8_t g711_in[],
00275                                  int g711_bytes);
00276 
00277 /*! Initialise a G.711 encode or decode context.
00278     \param s The G.711 context.
00279     \param mode The G.711 mode.
00280     \return A pointer to the G.711 context, or NULL for error. */
00281 SPAN_DECLARE(g711_state_t *) g711_init(g711_state_t *s, int mode);
00282 
00283 /*! Release a G.711 encode or decode context.
00284     \param s The G.711 context.
00285     \return 0 for OK. */
00286 SPAN_DECLARE(int) g711_release(g711_state_t *s);
00287 
00288 /*! Free a G.711 encode or decode context.
00289     \param s The G.711 context.
00290     \return 0 for OK. */
00291 SPAN_DECLARE(int) g711_free(g711_state_t *s);
00292 
00293 #if defined(__cplusplus)
00294 }
00295 #endif
00296 
00297 #endif
00298 /*- End of file ------------------------------------------------------------*/

Generated on Fri Aug 28 20:12:26 2009 for spandsp by  doxygen 1.3.9.1