src/spandsp/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.14 2008/05/02 17:57:32 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
00066 {
00067     /*! One of the G.711_xxx options */
00068     int mode;
00069 } g711_state_t;
00070 
00071 #if defined(__cplusplus)
00072 extern "C"
00073 {
00074 #endif
00075 
00076 /* N.B. It is tempting to use look-up tables for A-law and u-law conversion.
00077  *      However, you should consider the cache footprint.
00078  *
00079  *      A 64K byte table for linear to x-law and a 512 byte table for x-law to
00080  *      linear sound like peanuts these days, and shouldn't an array lookup be
00081  *      real fast? No! When the cache sloshes as badly as this one will, a tight
00082  *      calculation may be better. The messiest part is normally finding the
00083  *      segment, but a little inline assembly can fix that on an i386, x86_64 and
00084  *      many other modern processors.
00085  */
00086  
00087 /*
00088  * Mu-law is basically as follows:
00089  *
00090  *      Biased Linear Input Code        Compressed Code
00091  *      ------------------------        ---------------
00092  *      00000001wxyza                   000wxyz
00093  *      0000001wxyzab                   001wxyz
00094  *      000001wxyzabc                   010wxyz
00095  *      00001wxyzabcd                   011wxyz
00096  *      0001wxyzabcde                   100wxyz
00097  *      001wxyzabcdef                   101wxyz
00098  *      01wxyzabcdefg                   110wxyz
00099  *      1wxyzabcdefgh                   111wxyz
00100  *
00101  * Each biased linear code has a leading 1 which identifies the segment
00102  * number. The value of the segment number is equal to 7 minus the number
00103  * of leading 0's. The quantization interval is directly available as the
00104  * four bits wxyz.  * The trailing bits (a - h) are ignored.
00105  *
00106  * Ordinarily the complement of the resulting code word is used for
00107  * transmission, and so the code word is complemented before it is returned.
00108  *
00109  * For further information see John C. Bellamy's Digital Telephony, 1982,
00110  * John Wiley & Sons, pps 98-111 and 472-476.
00111  */
00112 
00113 //#define ULAW_ZEROTRAP                 /* turn on the trap as per the MIL-STD */
00114 #define ULAW_BIAS        0x84           /* Bias for linear code. */
00115 
00116 /*! \brief Encode a linear sample to u-law
00117     \param linear The sample to encode.
00118     \return The u-law value.
00119 */
00120 static __inline__ uint8_t linear_to_ulaw(int linear)
00121 {
00122     uint8_t u_val;
00123     int mask;
00124     int seg;
00125 
00126     /* Get the sign and the magnitude of the value. */
00127     if (linear >= 0)
00128     {
00129         linear = ULAW_BIAS + linear;
00130         mask = 0xFF;
00131     }
00132     else
00133     {
00134         linear = ULAW_BIAS - linear;
00135         mask = 0x7F;
00136     }
00137 
00138     seg = top_bit(linear | 0xFF) - 7;
00139 
00140     /*
00141      * Combine the sign, segment, quantization bits,
00142      * and complement the code word.
00143      */
00144     if (seg >= 8)
00145         u_val = (uint8_t) (0x7F ^ mask);
00146     else
00147         u_val = (uint8_t) (((seg << 4) | ((linear >> (seg + 3)) & 0xF)) ^ mask);
00148 #ifdef ULAW_ZEROTRAP
00149     /* Optional ITU trap */
00150     if (u_val == 0)
00151         u_val = 0x02;
00152 #endif
00153     return  u_val;
00154 }
00155 /*- End of function --------------------------------------------------------*/
00156 
00157 /*! \brief Decode an u-law sample to a linear value.
00158     \param ulaw The u-law sample to decode.
00159     \return The linear value.
00160 */
00161 static __inline__ int16_t ulaw_to_linear(uint8_t ulaw)
00162 {
00163     int t;
00164     
00165     /* Complement to obtain normal u-law value. */
00166     ulaw = ~ulaw;
00167     /*
00168      * Extract and bias the quantization bits. Then
00169      * shift up by the segment number and subtract out the bias.
00170      */
00171     t = (((ulaw & 0x0F) << 3) + ULAW_BIAS) << (((int) ulaw & 0x70) >> 4);
00172     return  (int16_t) ((ulaw & 0x80)  ?  (ULAW_BIAS - t)  :  (t - ULAW_BIAS));
00173 }
00174 /*- End of function --------------------------------------------------------*/
00175 
00176 /*
00177  * A-law is basically as follows:
00178  *
00179  *      Linear Input Code        Compressed Code
00180  *      -----------------        ---------------
00181  *      0000000wxyza             000wxyz
00182  *      0000001wxyza             001wxyz
00183  *      000001wxyzab             010wxyz
00184  *      00001wxyzabc             011wxyz
00185  *      0001wxyzabcd             100wxyz
00186  *      001wxyzabcde             101wxyz
00187  *      01wxyzabcdef             110wxyz
00188  *      1wxyzabcdefg             111wxyz
00189  *
00190  * For further information see John C. Bellamy's Digital Telephony, 1982,
00191  * John Wiley & Sons, pps 98-111 and 472-476.
00192  */
00193 
00194 #define ALAW_AMI_MASK       0x55
00195 
00196 /*! \brief Encode a linear sample to A-law
00197     \param linear The sample to encode.
00198     \return The A-law value.
00199 */
00200 static __inline__ uint8_t linear_to_alaw(int linear)
00201 {
00202     int mask;
00203     int seg;
00204     
00205     if (linear >= 0)
00206     {
00207         /* Sign (bit 7) bit = 1 */
00208         mask = ALAW_AMI_MASK | 0x80;
00209     }
00210     else
00211     {
00212         /* Sign (bit 7) bit = 0 */
00213         mask = ALAW_AMI_MASK;
00214         linear = -linear - 1;
00215     }
00216 
00217     /* Convert the scaled magnitude to segment number. */
00218     seg = top_bit(linear | 0xFF) - 7;
00219     if (seg >= 8)
00220     {
00221         if (linear >= 0)
00222         {
00223             /* Out of range. Return maximum value. */
00224             return (uint8_t) (0x7F ^ mask);
00225         }
00226         /* We must be just a tiny step below zero */
00227         return (uint8_t) (0x00 ^ mask);
00228     }
00229     /* Combine the sign, segment, and quantization bits. */
00230     return (uint8_t) (((seg << 4) | ((linear >> ((seg)  ?  (seg + 3)  :  4)) & 0x0F)) ^ mask);
00231 }
00232 /*- End of function --------------------------------------------------------*/
00233 
00234 /*! \brief Decode an A-law sample to a linear value.
00235     \param alaw The A-law sample to decode.
00236     \return The linear value.
00237 */
00238 static __inline__ int16_t alaw_to_linear(uint8_t alaw)
00239 {
00240     int i;
00241     int seg;
00242 
00243     alaw ^= ALAW_AMI_MASK;
00244     i = ((alaw & 0x0F) << 4);
00245     seg = (((int) alaw & 0x70) >> 4);
00246     if (seg)
00247         i = (i + 0x108) << (seg - 1);
00248     else
00249         i += 8;
00250     return (int16_t) ((alaw & 0x80)  ?  i  :  -i);
00251 }
00252 /*- End of function --------------------------------------------------------*/
00253 
00254 /*! \brief Transcode from A-law to u-law, using the procedure defined in G.711.
00255     \param alaw The A-law sample to transcode.
00256     \return The best matching u-law value.
00257 */
00258 uint8_t alaw_to_ulaw(uint8_t alaw);
00259 
00260 /*! \brief Transcode from u-law to A-law, using the procedure defined in G.711.
00261     \param ulaw The u-law sample to transcode.
00262     \return The best matching A-law value.
00263 */
00264 uint8_t ulaw_to_alaw(uint8_t ulaw);
00265 
00266 int g711_decode(g711_state_t *s,
00267                 int16_t amp[],
00268                 const uint8_t g711_data[],
00269                 int g711_bytes);
00270 
00271 int g711_encode(g711_state_t *s,
00272                 uint8_t g711_data[],
00273                 const int16_t amp[],
00274                 int len);
00275 
00276 int g711_transcode(g711_state_t *s,
00277                    uint8_t g711_out[],
00278                    const uint8_t g711_in[],
00279                    int g711_bytes);
00280 
00281 /*! Initialise a G.711 encode or decode context.
00282     \param s The G.711 context.
00283     \param mode The G.711 mode.
00284     \return A pointer to the G.711 context, or NULL for error. */
00285 g711_state_t *g711_init(g711_state_t *s, int mode);
00286 
00287 /*! Free a G.711 encode or decode context.
00288     \param s The G.711 context.
00289     \return 0 for OK. */
00290 int g711_release(g711_state_t *s);
00291 
00292 #if defined(__cplusplus)
00293 }
00294 #endif
00295 
00296 #endif
00297 /*- End of file ------------------------------------------------------------*/

Generated on Tue Oct 7 20:25:46 2008 for spandsp by  doxygen 1.5.6