encode.c

Go to the documentation of this file.
00001 /*
00002 
00003 $Log$
00004 Revision 1.15  2004/06/26 03:50:14  markster
00005 Merge source cleanups (bug #1911)
00006 
00007 Revision 1.14  2003/02/12 13:59:15  matteo
00008 mer feb 12 14:56:57 CET 2003
00009 
00010 Revision 1.1.1.1  2003/02/12 13:59:15  matteo
00011 mer feb 12 14:56:57 CET 2003
00012 
00013 Revision 1.2  2000/01/05 08:20:39  markster
00014 Some OSS fixes and a few lpc changes to make it actually work
00015 
00016  * Revision 1.1  1996/08/19  22:32:21  jaf
00017  * Initial revision
00018  *
00019 
00020 */
00021 
00022 /*  -- translated by f2c (version 19951025).
00023    You must link the resulting object file with the libraries:
00024    -lf2c -lm   (in that order)
00025 */
00026 
00027 #include "f2c.h"
00028 
00029 #ifdef P_R_O_T_O_T_Y_P_E_S
00030 extern int encode_(integer *voice, integer *pitch, real *rms, real *rc, integer *ipitch, integer *irms, integer *irc);
00031 /* comlen contrl_ 12 */
00032 #endif
00033 
00034 /* Common Block Declarations */
00035 
00036 extern struct {
00037     integer order, lframe;
00038     logical corrp;
00039 } contrl_;
00040 
00041 #define contrl_1 contrl_
00042 
00043 /* Table of constant values */
00044 
00045 static integer c__2 = 2;
00046 
00047 /* ***************************************************************** */
00048 
00049 /*    ENCODE Version 54 */
00050 
00051 /* $Log$
00052  * Revision 1.15  2004/06/26 03:50:14  markster
00053  * Merge source cleanups (bug #1911)
00054  *
00055  * Revision 1.14  2003/02/12 13:59:15  matteo
00056  * mer feb 12 14:56:57 CET 2003
00057  *
00058  * Revision 1.1.1.1  2003/02/12 13:59:15  matteo
00059  * mer feb 12 14:56:57 CET 2003
00060  *
00061  * Revision 1.2  2000/01/05 08:20:39  markster
00062  * Some OSS fixes and a few lpc changes to make it actually work
00063  *
00064  * Revision 1.1  1996/08/19  22:32:21  jaf
00065  * Initial revision
00066  * */
00067 /* Revision 1.5  1996/03/26  19:35:50  jaf */
00068 /* Commented out trace statements. */
00069 
00070 /* Revision 1.4  1996/03/21  00:26:29  jaf */
00071 /* Added the comment that this subroutine has no local state. */
00072 
00073 /* In the last check-in, I forgot to mention that I had added comments */
00074 /* explaining which indices of array arguments are read or written. */
00075 
00076 /* Revision 1.3  1996/03/21  00:22:39  jaf */
00077 /* Added comments explaining that all local arrays are effectively */
00078 /* constants. */
00079 
00080 /* Revision 1.2  1996/03/13  18:48:33  jaf */
00081 /* Comments added explaining that none of the local variables of this */
00082 /* subroutine need to be saved from one invocation to the next. */
00083 
00084 /* Revision 1.1  1996/02/07 14:45:29  jaf */
00085 /* Initial revision */
00086 
00087 
00088 /* ***************************************************************** */
00089 
00090 /*  Quantize LPC parameters for transmission */
00091 
00092 /* INPUTS: */
00093 /*  VOICE  - Half frame voicing decisions */
00094 /*           Indices 1 through 2 read. */
00095 /*  PITCH  - Pitch */
00096 /*  RMS    - Energy */
00097 /*  RC     - Reflection coefficients */
00098 /*           Indices 1 through ORDER read. */
00099 /*  CORRP  - Error Correction: TRUE = yes, FALSE = none */
00100 /*           (this is defined in file control.fh) */
00101 /* OUTPUTS: */
00102 /*  IPITCH - Coded pitch and voicing */
00103 /*  IRMS   - Quantized energy */
00104 /*  IRC    - Quantized reflection coefficients */
00105 /*           Indices 1 through MAX(ORDER,2) written. */
00106 /*           If CORRP is .TRUE., then indices 1 through 10 written */
00107 /*           for unvoiced frames. */
00108 
00109 /* This subroutine has no local state. */
00110 
00111 /* Subroutine */ int encode_(integer *voice, integer *pitch, real *rms, real *
00112    rc, integer *ipitch, integer *irms, integer *irc)
00113 {
00114     /* Initialized data */
00115 
00116     static integer enctab[16] = { 0,7,11,12,13,10,6,1,14,9,5,2,3,4,8,15 };
00117     static integer entau[60] = { 19,11,27,25,29,21,23,22,30,14,15,7,39,38,46,
00118        42,43,41,45,37,53,49,51,50,54,52,60,56,58,26,90,88,92,84,86,82,83,
00119        81,85,69,77,73,75,74,78,70,71,67,99,97,113,112,114,98,106,104,108,
00120        100,101,76 };
00121     static integer enadd[8] = { 1920,-768,2432,1280,3584,1536,2816,-1152 };
00122     static real enscl[8] = { .0204f,.0167f,.0145f,.0147f,.0143f,.0135f,.0125f,
00123        .0112f };
00124     static integer enbits[8] = { 6,5,4,4,4,4,3,3 };
00125     static integer entab6[64] = { 0,0,0,0,0,0,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,
00126        3,3,3,3,3,4,4,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,9,9,
00127        9,10,10,11,11,12,13,14,15 };
00128     static integer rmst[64] = { 1024,936,856,784,718,656,600,550,502,460,420,
00129        384,352,328,294,270,246,226,206,188,172,158,144,132,120,110,102,
00130        92,84,78,70,64,60,54,50,46,42,38,34,32,30,26,24,22,20,18,17,16,15,
00131        14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 };
00132 
00133     /* System generated locals */
00134     integer i__1, i__2;
00135 
00136     /* Builtin functions */
00137     integer pow_ii(integer *, integer *);
00138 
00139     /* Local variables */
00140     integer idel, nbit, i__, j, i2, i3, mrk;
00141 
00142 /* $Log$
00143  * Revision 1.15  2004/06/26 03:50:14  markster
00144  * Merge source cleanups (bug #1911)
00145  *
00146  * Revision 1.14  2003/02/12 13:59:15  matteo
00147  * mer feb 12 14:56:57 CET 2003
00148  *
00149  * Revision 1.1.1.1  2003/02/12 13:59:15  matteo
00150  * mer feb 12 14:56:57 CET 2003
00151  *
00152  * Revision 1.2  2000/01/05 08:20:39  markster
00153  * Some OSS fixes and a few lpc changes to make it actually work
00154  *
00155  * Revision 1.1  1996/08/19  22:32:21  jaf
00156  * Initial revision
00157  * */
00158 /* Revision 1.3  1996/03/29  22:03:47  jaf */
00159 /* Removed definitions for any constants that were no longer used. */
00160 
00161 /* Revision 1.2  1996/03/26  19:34:33  jaf */
00162 /* Added comments indicating which constants are not needed in an */
00163 /* application that uses the LPC-10 coder. */
00164 
00165 /* Revision 1.1  1996/02/07  14:43:51  jaf */
00166 /* Initial revision */
00167 
00168 /*   LPC Configuration parameters: */
00169 /* Frame size, Prediction order, Pitch period */
00170 /*       Arguments */
00171 /* $Log$
00172  * Revision 1.15  2004/06/26 03:50:14  markster
00173  * Merge source cleanups (bug #1911)
00174  *
00175  * Revision 1.14  2003/02/12 13:59:15  matteo
00176  * mer feb 12 14:56:57 CET 2003
00177  *
00178  * Revision 1.1.1.1  2003/02/12 13:59:15  matteo
00179  * mer feb 12 14:56:57 CET 2003
00180  *
00181  * Revision 1.2  2000/01/05 08:20:39  markster
00182  * Some OSS fixes and a few lpc changes to make it actually work
00183  *
00184  * Revision 1.1  1996/08/19  22:32:21  jaf
00185  * Initial revision
00186  * */
00187 /* Revision 1.3  1996/03/29  22:05:55  jaf */
00188 /* Commented out the common block variables that are not needed by the */
00189 /* embedded version. */
00190 
00191 /* Revision 1.2  1996/03/26  19:34:50  jaf */
00192 /* Added comments indicating which constants are not needed in an */
00193 /* application that uses the LPC-10 coder. */
00194 
00195 /* Revision 1.1  1996/02/07  14:44:09  jaf */
00196 /* Initial revision */
00197 
00198 /*   LPC Processing control variables: */
00199 
00200 /* *** Read-only: initialized in setup */
00201 
00202 /*  Files for Speech, Parameter, and Bitstream Input & Output, */
00203 /*    and message and debug outputs. */
00204 
00205 /* Here are the only files which use these variables: */
00206 
00207 /* lpcsim.f setup.f trans.f error.f vqsetup.f */
00208 
00209 /* Many files which use fdebug are not listed, since it is only used in */
00210 /* those other files conditionally, to print trace statements. */
00211 /*    integer fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
00212 /*  LPC order, Frame size, Quantization rate, Bits per frame, */
00213 /*    Error correction */
00214 /* Subroutine SETUP is the only place where order is assigned a value, */
00215 /* and that value is 10.  It could increase efficiency 1% or so to */
00216 /* declare order as a constant (i.e., a Fortran PARAMETER) instead of as 
00217 */
00218 /* a variable in a COMMON block, since it is used in many places in the */
00219 /* core of the coding and decoding routines.  Actually, I take that back. 
00220 */
00221 /* At least when compiling with f2c, the upper bound of DO loops is */
00222 /* stored in a local variable before the DO loop begins, and then that is 
00223 */
00224 /* compared against on each iteration. */
00225 /* Similarly for lframe, which is given a value of MAXFRM in SETUP. */
00226 /* Similarly for quant, which is given a value of 2400 in SETUP.  quant */
00227 /* is used in only a few places, and never in the core coding and */
00228 /* decoding routines, so it could be eliminated entirely. */
00229 /* nbits is similar to quant, and is given a value of 54 in SETUP. */
00230 /* corrp is given a value of .TRUE. in SETUP, and is only used in the */
00231 /* subroutines ENCODE and DECODE.  It doesn't affect the speed of the */
00232 /* coder significantly whether it is .TRUE. or .FALSE., or whether it is 
00233 */
00234 /* a constant or a variable, since it is only examined once per frame. */
00235 /* Leaving it as a variable that is set to .TRUE.  seems like a good */
00236 /* idea, since it does enable some error-correction capability for */
00237 /* unvoiced frames, with no change in the coding rate, and no noticeable 
00238 */
00239 /* quality difference in the decoded speech. */
00240 /*    integer quant, nbits */
00241 /* *** Read/write: variables for debugging, not needed for LPC algorithm 
00242 */
00243 
00244 /*  Current frame, Unstable frames, Output clip count, Max onset buffer, 
00245 */
00246 /*    Debug listing detail level, Line count on listing page */
00247 
00248 /* nframe is not needed for an embedded LPC10 at all. */
00249 /* nunsfm is initialized to 0 in SETUP, and incremented in subroutine */
00250 /* ERROR, which is only called from RCCHK.  When LPC10 is embedded into */
00251 /* an application, I would recommend removing the call to ERROR in RCCHK, 
00252 */
00253 /* and remove ERROR and nunsfm completely. */
00254 /* iclip is initialized to 0 in SETUP, and incremented in entry SWRITE in 
00255 */
00256 /* sread.f.  When LPC10 is embedded into an application, one might want */
00257 /* to cause it to be incremented in a routine that takes the output of */
00258 /* SYNTHS and sends it to an audio device.  It could be optionally */
00259 /* displayed, for those that might want to know what it is. */
00260 /* maxosp is never initialized to 0 in SETUP, although it probably should 
00261 */
00262 /* be, and it is updated in subroutine ANALYS.  I doubt that its value */
00263 /* would be of much interest to an application in which LPC10 is */
00264 /* embedded. */
00265 /* listl and lincnt are not needed for an embedded LPC10 at all. */
00266 /*    integer nframe, nunsfm, iclip, maxosp, listl, lincnt */
00267 /*    common /contrl/ fsi, fso, fpi, fpo, fbi, fbo, pbin, fmsg, fdebug */
00268 /*    common /contrl/ quant, nbits */
00269 /*    common /contrl/ nframe, nunsfm, iclip, maxosp, listl, lincnt */
00270 /*       Parameters/constants */
00271 /*       These arrays are not Fortran PARAMETER's, but they are defined */
00272 /*       by DATA statements below, and their contents are never altered. 
00273 */
00274 /*       Local variables that need not be saved */
00275     /* Parameter adjustments */
00276     --irc;
00277     --rc;
00278     --voice;
00279 
00280     /* Function Body */
00281 /*  Scale RMS and RC's to integers */
00282     *irms = (integer)*rms;
00283     i__1 = contrl_1.order;
00284     for (i__ = 1; i__ <= i__1; ++i__) {
00285    irc[i__] = (integer)(rc[i__] * 32768.f);
00286     }
00287 /*    IF(LISTL.GE.3)WRITE(FDEBUG,800)VOICE,PITCH,IRMS,(IRC(I),I=1,ORDER) */
00288 /* 800   FORMAT(1X,/,' <<ENCODE IN>>',T32,2I3,I6,I5,T50,10I8) */
00289 /*  Encode pitch and voicing */
00290     if (voice[1] != 0 && voice[2] != 0) {
00291    *ipitch = entau[*pitch - 1];
00292     } else {
00293    if (contrl_1.corrp) {
00294        *ipitch = 0;
00295        if (voice[1] != voice[2]) {
00296       *ipitch = 127;
00297        }
00298    } else {
00299        *ipitch = (voice[1] << 1) + voice[2];
00300    }
00301     }
00302 /*  Encode RMS by binary table search */
00303     j = 32;
00304     idel = 16;
00305     *irms = min(*irms,1023);
00306     while(idel > 0) {
00307    if (*irms > rmst[j - 1]) {
00308        j -= idel;
00309    }
00310    if (*irms < rmst[j - 1]) {
00311        j += idel;
00312    }
00313    idel /= 2;
00314     }
00315     if (*irms > rmst[j - 1]) {
00316    --j;
00317     }
00318     *irms = 31 - j / 2;
00319 /*  Encode RC(1) and (2) as log-area-ratios */
00320     for (i__ = 1; i__ <= 2; ++i__) {
00321    i2 = irc[i__];
00322    mrk = 0;
00323    if (i2 < 0) {
00324        i2 = -i2;
00325        mrk = 1;
00326    }
00327    i2 /= 512;
00328    i2 = min(i2,63);
00329    i2 = entab6[i2];
00330    if (mrk != 0) {
00331        i2 = -i2;
00332    }
00333    irc[i__] = i2;
00334     }
00335 /*  Encode RC(3) - (10) linearly, remove bias then scale */
00336     i__1 = contrl_1.order;
00337     for (i__ = 3; i__ <= i__1; ++i__) {
00338    i2 = irc[i__] / 2;
00339    i2 = (integer)((i2 + enadd[contrl_1.order + 1 - i__ - 1]) * enscl[
00340       contrl_1.order + 1 - i__ - 1]);
00341 /* Computing MIN */
00342    i__2 = max(i2,-127);
00343    i2 = min(i__2,127);
00344    nbit = enbits[contrl_1.order + 1 - i__ - 1];
00345    i3 = 0;
00346    if (i2 < 0) {
00347        i3 = -1;
00348    }
00349    i2 /= pow_ii(&c__2, &nbit);
00350    if (i3 == -1) {
00351        --i2;
00352    }
00353    irc[i__] = i2;
00354     }
00355 /*          Protect the most significant bits of the most */
00356 /*     important parameters during non-voiced frames. */
00357 /*     RC(1) - RC(4) are protected using 20 parity bits */
00358 /*     replacing RC(5) - RC(10). */
00359     if (contrl_1.corrp) {
00360    if (*ipitch == 0 || *ipitch == 127) {
00361        irc[5] = enctab[(irc[1] & 30) / 2];
00362        irc[6] = enctab[(irc[2] & 30) / 2];
00363        irc[7] = enctab[(irc[3] & 30) / 2];
00364        irc[8] = enctab[(*irms & 30) / 2];
00365        irc[9] = enctab[(irc[4] & 30) / 2] / 2;
00366        irc[10] = enctab[(irc[4] & 30) / 2] & 1;
00367    }
00368     }
00369 /*    IF(LISTL.GE.3)WRITE(FDEBUG,801)VOICE,IPITCH,IRMS,(IRC(J),J=1,ORDER) */
00370 /* 801   FORMAT(1X,'<<ENCODE OUT>>',T32,2I3,I6,I5,T50,10I8) */
00371     return 0;
00372 } /* encode_ */
00373 

Generated on Thu Apr 16 06:27:33 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6