g722.h File Reference

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  g722_decode_state_t
struct  g722_encode_state_t

Defines

#define _G722_H_
#define INT16_MAX   32767
#define INT16_MIN   (-32768)

Enumerations

enum  { G722_SAMPLE_RATE_8000 = 0x0001, G722_PACKED = 0x0002 }

Functions

int g722_decode (g722_decode_state_t *s, int16_t amp[], const uint8_t g722_data[], int len)
g722_decode_state_tg722_decode_init (g722_decode_state_t *s, int rate, int options)
int g722_decode_release (g722_decode_state_t *s)
int g722_encode (g722_encode_state_t *s, uint8_t g722_data[], const int16_t amp[], int len)
g722_encode_state_tg722_encode_init (g722_encode_state_t *s, int rate, int options)
int g722_encode_release (g722_encode_state_t *s)


Detailed Description

Definition in file g722.h.


Define Documentation

#define _G722_H_

Definition at line 28 of file g722.h.

#define INT16_MAX   32767

Definition at line 50 of file g722.h.

Referenced by fsaturate(), and saturate().

#define INT16_MIN   (-32768)

Definition at line 53 of file g722.h.

Referenced by fsaturate(), and saturate().


Enumeration Type Documentation

anonymous enum

Enumerator:
G722_SAMPLE_RATE_8000 
G722_PACKED 

Definition at line 43 of file g722.h.

00044 {
00045     G722_SAMPLE_RATE_8000 = 0x0001,
00046     G722_PACKED = 0x0002
00047 };


Function Documentation

int g722_decode ( g722_decode_state_t s,
int16_t  amp[],
const uint8_t  g722_data[],
int  len 
)

Definition at line 186 of file g722_decode.c.

References g722_decode_state_t::band, g722_decode_state_t::bits_per_sample, block4(), g722_decode_state_t::det, g722_decode_state_t::eight_k, g722_decode_state_t::in_bits, g722_decode_state_t::in_buffer, g722_decode_state_t::itu_test_mode, g722_decode_state_t::nb, g722_decode_state_t::packed, g722_decode_state_t::s, and g722_decode_state_t::x.

Referenced by g722tolin_framein().

00187 {
00188     static const int wl[8] = {-60, -30, 58, 172, 334, 538, 1198, 3042 };
00189     static const int rl42[16] = {0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3,  2, 1, 0 };
00190     static const int ilb[32] =
00191     {
00192         2048, 2093, 2139, 2186, 2233, 2282, 2332,
00193         2383, 2435, 2489, 2543, 2599, 2656, 2714,
00194         2774, 2834, 2896, 2960, 3025, 3091, 3158,
00195         3228, 3298, 3371, 3444, 3520, 3597, 3676,
00196         3756, 3838, 3922, 4008
00197     };
00198     static const int wh[3] = {0, -214, 798};
00199     static const int rh2[4] = {2, 1, 2, 1};
00200     static const int qm2[4] = {-7408, -1616,  7408,   1616};
00201     static const int qm4[16] = 
00202     {
00203               0, -20456, -12896,  -8968, 
00204           -6288,  -4240,  -2584,  -1200,
00205           20456,  12896,   8968,   6288,
00206            4240,   2584,   1200,      0
00207     };
00208     static const int qm5[32] =
00209     {
00210            -280,   -280, -23352, -17560,
00211          -14120, -11664,  -9752,  -8184,
00212           -6864,  -5712,  -4696,  -3784,
00213           -2960,  -2208,  -1520,   -880,
00214           23352,  17560,  14120,  11664,
00215            9752,   8184,   6864,   5712,
00216            4696,   3784,   2960,   2208,
00217            1520,    880,    280,   -280
00218     };
00219     static const int qm6[64] =
00220     {
00221            -136,   -136,   -136,   -136,
00222          -24808, -21904, -19008, -16704,
00223          -14984, -13512, -12280, -11192,
00224          -10232,  -9360,  -8576,  -7856,
00225           -7192,  -6576,  -6000,  -5456,
00226           -4944,  -4464,  -4008,  -3576,
00227           -3168,  -2776,  -2400,  -2032,
00228           -1688,  -1360,  -1040,   -728,
00229           24808,  21904,  19008,  16704,
00230           14984,  13512,  12280,  11192,
00231           10232,   9360,   8576,   7856,
00232            7192,   6576,   6000,   5456,
00233            4944,   4464,   4008,   3576,
00234            3168,   2776,   2400,   2032,
00235            1688,   1360,   1040,    728,
00236             432,    136,   -432,   -136
00237     };
00238     static const int qmf_coeffs[12] =
00239     {
00240            3,  -11,   12,   32, -210,  951, 3876, -805,  362, -156,   53,  -11,
00241     };
00242 
00243     int dlowt;
00244     int rlow;
00245     int ihigh;
00246     int dhigh;
00247     int rhigh;
00248     int xout1;
00249     int xout2;
00250     int wd1;
00251     int wd2;
00252     int wd3;
00253     int code;
00254     int outlen;
00255     int i;
00256     int j;
00257 
00258     outlen = 0;
00259     rhigh = 0;
00260     for (j = 0;  j < len;  )
00261     {
00262         if (s->packed)
00263         {
00264             /* Unpack the code bits */
00265             if (s->in_bits < s->bits_per_sample)
00266             {
00267                 s->in_buffer |= (g722_data[j++] << s->in_bits);
00268                 s->in_bits += 8;
00269             }
00270             code = s->in_buffer & ((1 << s->bits_per_sample) - 1);
00271             s->in_buffer >>= s->bits_per_sample;
00272             s->in_bits -= s->bits_per_sample;
00273         }
00274         else
00275         {
00276             code = g722_data[j++];
00277         }
00278 
00279         switch (s->bits_per_sample)
00280         {
00281         default:
00282         case 8:
00283             wd1 = code & 0x3F;
00284             ihigh = (code >> 6) & 0x03;
00285             wd2 = qm6[wd1];
00286             wd1 >>= 2;
00287             break;
00288         case 7:
00289             wd1 = code & 0x1F;
00290             ihigh = (code >> 5) & 0x03;
00291             wd2 = qm5[wd1];
00292             wd1 >>= 1;
00293             break;
00294         case 6:
00295             wd1 = code & 0x0F;
00296             ihigh = (code >> 4) & 0x03;
00297             wd2 = qm4[wd1];
00298             break;
00299         }
00300         /* Block 5L, LOW BAND INVQBL */
00301         wd2 = (s->band[0].det*wd2) >> 15;
00302         /* Block 5L, RECONS */
00303         rlow = s->band[0].s + wd2;
00304         /* Block 6L, LIMIT */
00305         if (rlow > 16383)
00306             rlow = 16383;
00307         else if (rlow < -16384)
00308             rlow = -16384;
00309 
00310         /* Block 2L, INVQAL */
00311         wd2 = qm4[wd1];
00312         dlowt = (s->band[0].det*wd2) >> 15;
00313 
00314         /* Block 3L, LOGSCL */
00315         wd2 = rl42[wd1];
00316         wd1 = (s->band[0].nb*127) >> 7;
00317         wd1 += wl[wd2];
00318         if (wd1 < 0)
00319             wd1 = 0;
00320         else if (wd1 > 18432)
00321             wd1 = 18432;
00322         s->band[0].nb = wd1;
00323             
00324         /* Block 3L, SCALEL */
00325         wd1 = (s->band[0].nb >> 6) & 31;
00326         wd2 = 8 - (s->band[0].nb >> 11);
00327         wd3 = (wd2 < 0)  ?  (ilb[wd1] << -wd2)  :  (ilb[wd1] >> wd2);
00328         s->band[0].det = wd3 << 2;
00329 
00330         block4(s, 0, dlowt);
00331         
00332         if (!s->eight_k)
00333         {
00334             /* Block 2H, INVQAH */
00335             wd2 = qm2[ihigh];
00336             dhigh = (s->band[1].det*wd2) >> 15;
00337             /* Block 5H, RECONS */
00338             rhigh = dhigh + s->band[1].s;
00339             /* Block 6H, LIMIT */
00340             if (rhigh > 16383)
00341                 rhigh = 16383;
00342             else if (rhigh < -16384)
00343                 rhigh = -16384;
00344 
00345             /* Block 2H, INVQAH */
00346             wd2 = rh2[ihigh];
00347             wd1 = (s->band[1].nb*127) >> 7;
00348             wd1 += wh[wd2];
00349             if (wd1 < 0)
00350                 wd1 = 0;
00351             else if (wd1 > 22528)
00352                 wd1 = 22528;
00353             s->band[1].nb = wd1;
00354             
00355             /* Block 3H, SCALEH */
00356             wd1 = (s->band[1].nb >> 6) & 31;
00357             wd2 = 10 - (s->band[1].nb >> 11);
00358             wd3 = (wd2 < 0)  ?  (ilb[wd1] << -wd2)  :  (ilb[wd1] >> wd2);
00359             s->band[1].det = wd3 << 2;
00360 
00361             block4(s, 1, dhigh);
00362         }
00363 
00364         if (s->itu_test_mode)
00365         {
00366             amp[outlen++] = (int16_t) (rlow << 1);
00367             amp[outlen++] = (int16_t) (rhigh << 1);
00368         }
00369         else
00370         {
00371             if (s->eight_k)
00372             {
00373                 amp[outlen++] = (int16_t) (rlow << 1);
00374             }
00375             else
00376             {
00377                 /* Apply the receive QMF */
00378                 for (i = 0;  i < 22;  i++)
00379                     s->x[i] = s->x[i + 2];
00380                 s->x[22] = rlow + rhigh;
00381                 s->x[23] = rlow - rhigh;
00382 
00383                 xout1 = 0;
00384                 xout2 = 0;
00385                 for (i = 0;  i < 12;  i++)
00386                 {
00387                     xout2 += s->x[2*i]*qmf_coeffs[i];
00388                     xout1 += s->x[2*i + 1]*qmf_coeffs[11 - i];
00389                 }
00390                 amp[outlen++] = (int16_t) (xout1 >> 11);
00391                 amp[outlen++] = (int16_t) (xout2 >> 11);
00392             }
00393         }
00394     }
00395     return outlen;
00396 }

g722_decode_state_t* g722_decode_init ( g722_decode_state_t s,
int  rate,
int  options 
)

Definition at line 153 of file g722_decode.c.

References g722_decode_state_t::band, g722_decode_state_t::bits_per_sample, g722_decode_state_t::det, g722_decode_state_t::eight_k, FALSE, G722_PACKED, G722_SAMPLE_RATE_8000, malloc, NULL, g722_decode_state_t::packed, and TRUE.

Referenced by g722tolin16_new(), and g722tolin_new().

00154 {
00155     if (s == NULL)
00156     {
00157         if ((s = (g722_decode_state_t *) malloc(sizeof(*s))) == NULL)
00158             return NULL;
00159     }
00160     memset(s, 0, sizeof(*s));
00161     if (rate == 48000)
00162         s->bits_per_sample = 6;
00163     else if (rate == 56000)
00164         s->bits_per_sample = 7;
00165     else
00166         s->bits_per_sample = 8;
00167     if ((options & G722_SAMPLE_RATE_8000))
00168         s->eight_k = TRUE;
00169     if ((options & G722_PACKED)  &&  s->bits_per_sample != 8)
00170         s->packed = TRUE;
00171     else
00172         s->packed = FALSE;
00173     s->band[0].det = 32;
00174     s->band[1].det = 8;
00175     return s;
00176 }

int g722_decode_release ( g722_decode_state_t s  ) 

Definition at line 179 of file g722_decode.c.

References free.

00180 {
00181     free(s);
00182     return 0;
00183 }

int g722_encode ( g722_encode_state_t s,
uint8_t  g722_data[],
const int16_t  amp[],
int  len 
)

Definition at line 185 of file g722_encode.c.

References g722_encode_state_t::band, g722_encode_state_t::bits_per_sample, block4(), g722_encode_state_t::det, g722_encode_state_t::eight_k, el, g722_encode_state_t::itu_test_mode, g722_encode_state_t::nb, g722_encode_state_t::out_bits, g722_encode_state_t::out_buffer, g722_encode_state_t::packed, g722_encode_state_t::s, saturate, and g722_encode_state_t::x.

Referenced by lintog722_framein().

00186 {
00187     static const int q6[32] =
00188     {
00189            0,   35,   72,  110,  150,  190,  233,  276,
00190          323,  370,  422,  473,  530,  587,  650,  714,
00191          786,  858,  940, 1023, 1121, 1219, 1339, 1458,
00192         1612, 1765, 1980, 2195, 2557, 2919,    0,    0
00193     };
00194     static const int iln[32] =
00195     {
00196          0, 63, 62, 31, 30, 29, 28, 27,
00197         26, 25, 24, 23, 22, 21, 20, 19,
00198         18, 17, 16, 15, 14, 13, 12, 11,
00199         10,  9,  8,  7,  6,  5,  4,  0
00200     };
00201     static const int ilp[32] =
00202     {
00203          0, 61, 60, 59, 58, 57, 56, 55,
00204         54, 53, 52, 51, 50, 49, 48, 47,
00205         46, 45, 44, 43, 42, 41, 40, 39,
00206         38, 37, 36, 35, 34, 33, 32,  0
00207     };
00208     static const int wl[8] =
00209     {
00210         -60, -30, 58, 172, 334, 538, 1198, 3042
00211     };
00212     static const int rl42[16] =
00213     {
00214         0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0
00215     };
00216     static const int ilb[32] =
00217     {
00218         2048, 2093, 2139, 2186, 2233, 2282, 2332,
00219         2383, 2435, 2489, 2543, 2599, 2656, 2714,
00220         2774, 2834, 2896, 2960, 3025, 3091, 3158,
00221         3228, 3298, 3371, 3444, 3520, 3597, 3676,
00222         3756, 3838, 3922, 4008
00223     };
00224     static const int qm4[16] =
00225     {
00226              0, -20456, -12896, -8968,
00227          -6288,  -4240,  -2584, -1200,
00228          20456,  12896,   8968,  6288,
00229           4240,   2584,   1200,     0
00230     };
00231     static const int qm2[4] =
00232     {
00233         -7408,  -1616,   7408,   1616
00234     };
00235     static const int qmf_coeffs[12] =
00236     {
00237            3,  -11,   12,   32, -210,  951, 3876, -805,  362, -156,   53,  -11,
00238     };
00239     static const int ihn[3] = {0, 1, 0};
00240     static const int ihp[3] = {0, 3, 2};
00241     static const int wh[3] = {0, -214, 798};
00242     static const int rh2[4] = {2, 1, 2, 1};
00243 
00244     int dlow;
00245     int dhigh;
00246     int el;
00247     int wd;
00248     int wd1;
00249     int ril;
00250     int wd2;
00251     int il4;
00252     int ih2;
00253     int wd3;
00254     int eh;
00255     int mih;
00256     int i;
00257     int j;
00258     /* Low and high band PCM from the QMF */
00259     int xlow;
00260     int xhigh;
00261     int g722_bytes;
00262     /* Even and odd tap accumulators */
00263     int sumeven;
00264     int sumodd;
00265     int ihigh;
00266     int ilow;
00267     int code;
00268 
00269     g722_bytes = 0;
00270     xhigh = 0;
00271     for (j = 0;  j < len;  )
00272     {
00273         if (s->itu_test_mode)
00274         {
00275             xlow =
00276             xhigh = amp[j++] >> 1;
00277         }
00278         else
00279         {
00280             if (s->eight_k)
00281             {
00282                 xlow = amp[j++] >> 1;
00283             }
00284             else
00285             {
00286                 /* Apply the transmit QMF */
00287                 /* Shuffle the buffer down */
00288                 for (i = 0;  i < 22;  i++)
00289                     s->x[i] = s->x[i + 2];
00290                 s->x[22] = amp[j++];
00291                 s->x[23] = amp[j++];
00292     
00293                 /* Discard every other QMF output */
00294                 sumeven = 0;
00295                 sumodd = 0;
00296                 for (i = 0;  i < 12;  i++)
00297                 {
00298                     sumodd += s->x[2*i]*qmf_coeffs[i];
00299                     sumeven += s->x[2*i + 1]*qmf_coeffs[11 - i];
00300                 }
00301                 xlow = (sumeven + sumodd) >> 14;
00302                 xhigh = (sumeven - sumodd) >> 14;
00303             }
00304         }
00305         /* Block 1L, SUBTRA */
00306         el = saturate(xlow - s->band[0].s);
00307 
00308         /* Block 1L, QUANTL */
00309         wd = (el >= 0)  ?  el  :  -(el + 1);
00310 
00311         for (i = 1;  i < 30;  i++)
00312         {
00313             wd1 = (q6[i]*s->band[0].det) >> 12;
00314             if (wd < wd1)
00315                 break;
00316         }
00317         ilow = (el < 0)  ?  iln[i]  :  ilp[i];
00318 
00319         /* Block 2L, INVQAL */
00320         ril = ilow >> 2;
00321         wd2 = qm4[ril];
00322         dlow = (s->band[0].det*wd2) >> 15;
00323 
00324         /* Block 3L, LOGSCL */
00325         il4 = rl42[ril];
00326         wd = (s->band[0].nb*127) >> 7;
00327         s->band[0].nb = wd + wl[il4];
00328         if (s->band[0].nb < 0)
00329             s->band[0].nb = 0;
00330         else if (s->band[0].nb > 18432)
00331             s->band[0].nb = 18432;
00332 
00333         /* Block 3L, SCALEL */
00334         wd1 = (s->band[0].nb >> 6) & 31;
00335         wd2 = 8 - (s->band[0].nb >> 11);
00336         wd3 = (wd2 < 0)  ?  (ilb[wd1] << -wd2)  :  (ilb[wd1] >> wd2);
00337         s->band[0].det = wd3 << 2;
00338 
00339         block4(s, 0, dlow);
00340         
00341         if (s->eight_k)
00342         {
00343             /* Just leave the high bits as zero */
00344             code = (0xC0 | ilow) >> (8 - s->bits_per_sample);
00345         }
00346         else
00347         {
00348             /* Block 1H, SUBTRA */
00349             eh = saturate(xhigh - s->band[1].s);
00350 
00351             /* Block 1H, QUANTH */
00352             wd = (eh >= 0)  ?  eh  :  -(eh + 1);
00353             wd1 = (564*s->band[1].det) >> 12;
00354             mih = (wd >= wd1)  ?  2  :  1;
00355             ihigh = (eh < 0)  ?  ihn[mih]  :  ihp[mih];
00356 
00357             /* Block 2H, INVQAH */
00358             wd2 = qm2[ihigh];
00359             dhigh = (s->band[1].det*wd2) >> 15;
00360 
00361             /* Block 3H, LOGSCH */
00362             ih2 = rh2[ihigh];
00363             wd = (s->band[1].nb*127) >> 7;
00364             s->band[1].nb = wd + wh[ih2];
00365             if (s->band[1].nb < 0)
00366                 s->band[1].nb = 0;
00367             else if (s->band[1].nb > 22528)
00368                 s->band[1].nb = 22528;
00369 
00370             /* Block 3H, SCALEH */
00371             wd1 = (s->band[1].nb >> 6) & 31;
00372             wd2 = 10 - (s->band[1].nb >> 11);
00373             wd3 = (wd2 < 0)  ?  (ilb[wd1] << -wd2)  :  (ilb[wd1] >> wd2);
00374             s->band[1].det = wd3 << 2;
00375 
00376             block4(s, 1, dhigh);
00377             code = ((ihigh << 6) | ilow) >> (8 - s->bits_per_sample);
00378         }
00379 
00380         if (s->packed)
00381         {
00382             /* Pack the code bits */
00383             s->out_buffer |= (code << s->out_bits);
00384             s->out_bits += s->bits_per_sample;
00385             if (s->out_bits >= 8)
00386             {
00387                 g722_data[g722_bytes++] = (uint8_t) (s->out_buffer & 0xFF);
00388                 s->out_bits -= 8;
00389                 s->out_buffer >>= 8;
00390             }
00391         }
00392         else
00393         {
00394             g722_data[g722_bytes++] = (uint8_t) code;
00395         }
00396     }
00397     return g722_bytes;
00398 }

g722_encode_state_t* g722_encode_init ( g722_encode_state_t s,
int  rate,
int  options 
)

Definition at line 152 of file g722_encode.c.

References g722_encode_state_t::band, g722_encode_state_t::bits_per_sample, g722_encode_state_t::det, g722_encode_state_t::eight_k, FALSE, G722_PACKED, G722_SAMPLE_RATE_8000, malloc, NULL, g722_encode_state_t::packed, and TRUE.

Referenced by lin16tog722_new(), and lintog722_new().

00153 {
00154     if (s == NULL)
00155     {
00156         if ((s = (g722_encode_state_t *) malloc(sizeof(*s))) == NULL)
00157             return NULL;
00158     }
00159     memset(s, 0, sizeof(*s));
00160     if (rate == 48000)
00161         s->bits_per_sample = 6;
00162     else if (rate == 56000)
00163         s->bits_per_sample = 7;
00164     else
00165         s->bits_per_sample = 8;
00166     if ((options & G722_SAMPLE_RATE_8000))
00167         s->eight_k = TRUE;
00168     if ((options & G722_PACKED)  &&  s->bits_per_sample != 8)
00169         s->packed = TRUE;
00170     else
00171         s->packed = FALSE;
00172     s->band[0].det = 32;
00173     s->band[1].det = 8;
00174     return s;
00175 }

int g722_encode_release ( g722_encode_state_t s  ) 

Definition at line 178 of file g722_encode.c.

References free.

00179 {
00180     free(s);
00181     return 0;
00182 }


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