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

Generated on Tue Aug 4 03:35:58 2009 for spandsp by  doxygen 1.5.9