Coverage Report

Created: 2026-10-17 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libsndfile/src/G72x/g72x.c
Line
Count
Source
1
/*
2
 * This source code is a product of Sun Microsystems, Inc. and is provided
3
 * for unrestricted use.  Users may copy or modify this source code without
4
 * charge.
5
 *
6
 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
7
 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
8
 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
9
 *
10
 * Sun source code is provided with no support and without any obligation on
11
 * the part of Sun Microsystems, Inc. to assist in its use, correction,
12
 * modification or enhancement.
13
 *
14
 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
15
 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
16
 * OR ANY PART THEREOF.
17
 *
18
 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
19
 * or profits or other special, indirect and consequential damages, even if
20
 * Sun has been advised of the possibility of such damages.
21
 *
22
 * Sun Microsystems, Inc.
23
 * 2550 Garcia Avenue
24
 * Mountain View, California  94043
25
 */
26
27
/*
28
 * g72x.c
29
 *
30
 * Common routines for G.721 and G.723 conversions.
31
 */
32
33
#include <stdio.h>
34
#include <stdlib.h>
35
#include <string.h>
36
37
#include "g72x.h"
38
#include "g72x_priv.h"
39
40
static G72x_STATE * g72x_state_new (void) ;
41
static int unpack_bytes (int bits, int blocksize, const unsigned char * block, short * samples) ;
42
static int pack_bytes (int bits, const short * samples, unsigned char * block) ;
43
44
static
45
short power2 [15] =
46
{ 1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80,
47
  0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000
48
} ;
49
50
/*
51
 * quan ()
52
 *
53
 * quantizes the input val against the table of size short integers.
54
 * It returns i if table [i - 1] <= val < table [i].
55
 *
56
 * Using linear search for simple coding.
57
 */
58
static
59
int quan (int val, short *table, int size)
60
93.2M
{
61
93.2M
  int   i ;
62
63
817M
  for (i = 0 ; i < size ; i++)
64
816M
    if (val < *table++)
65
92.7M
      break ;
66
93.2M
  return i ;
67
93.2M
}
68
69
/*
70
 * fmult ()
71
 *
72
 * returns the integer product of the 14-bit integer "an" and
73
 * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
74
 */
75
static
76
int fmult (int an, int srn)
77
82.0M
{
78
82.0M
  short   anmag, anexp, anmant ;
79
82.0M
  short   wanexp, wanmant ;
80
82.0M
  short   retval ;
81
82
82.0M
  anmag = (an > 0) ? an : ((-an) & 0x1FFF) ;
83
82.0M
  anexp = quan (anmag, power2, 15) - 6 ;
84
82.0M
  anmant = (anmag == 0) ? 32 :
85
82.0M
        (anexp >= 0) ? anmag >> anexp : anmag << -anexp ;
86
82.0M
  wanexp = anexp + ((srn >> 6) & 0xF) - 13 ;
87
88
  /*
89
  ** The original was :
90
  **    wanmant = (anmant * (srn & 0x3F) + 0x30) >> 4 ;
91
  ** but could see no valid reason for the + 0x30.
92
  ** Removed it and it improved the SNR of the codec.
93
  */
94
95
82.0M
  wanmant = (anmant * (srn & 0x3F)) >> 4 ;
96
97
82.0M
  retval = (wanexp >= 0) ? ((wanmant << wanexp) & 0x7FFF) : (wanmant >> -wanexp) ;
98
99
82.0M
  return (((an ^ srn) < 0) ? -retval : retval) ;
100
82.0M
}
101
102
static G72x_STATE * g72x_state_new (void)
103
667
{ return calloc (1, sizeof (G72x_STATE)) ;
104
667
}
105
106
/*
107
 * private_init_state ()
108
 *
109
 * This routine initializes and/or resets the G72x_PRIVATE structure
110
 * pointed to by 'state_ptr'.
111
 * All the initial state values are specified in the CCITT G.721 document.
112
 */
113
void private_init_state (G72x_STATE *state_ptr)
114
667
{
115
667
  int   cnta ;
116
117
667
  state_ptr->yl = 34816 ;
118
667
  state_ptr->yu = 544 ;
119
667
  state_ptr->dms = 0 ;
120
667
  state_ptr->dml = 0 ;
121
667
  state_ptr->ap = 0 ;
122
2.00k
  for (cnta = 0 ; cnta < 2 ; cnta++)
123
1.33k
  { state_ptr->a [cnta] = 0 ;
124
1.33k
    state_ptr->pk [cnta] = 0 ;
125
1.33k
    state_ptr->sr [cnta] = 32 ;
126
1.33k
    }
127
4.66k
  for (cnta = 0 ; cnta < 6 ; cnta++)
128
4.00k
  { state_ptr->b [cnta] = 0 ;
129
4.00k
    state_ptr->dq [cnta] = 32 ;
130
4.00k
    }
131
667
  state_ptr->td = 0 ;
132
667
}  /* private_init_state */
133
134
struct g72x_state * g72x_reader_init (int codec, int *blocksize, int *samplesperblock)
135
667
{ G72x_STATE *pstate ;
136
137
667
  if ((pstate = g72x_state_new ()) == NULL)
138
0
    return NULL ;
139
140
667
  private_init_state (pstate) ;
141
142
667
  pstate->encoder = NULL ;
143
144
667
  switch (codec)
145
667
  { case G723_16_BITS_PER_SAMPLE : /* 2 bits per sample. */
146
0
        pstate->decoder = g723_16_decoder ;
147
0
        *blocksize = G723_16_BYTES_PER_BLOCK ;
148
0
        *samplesperblock = G723_16_SAMPLES_PER_BLOCK ;
149
0
        pstate->codec_bits = 2 ;
150
0
        pstate->blocksize = G723_16_BYTES_PER_BLOCK ;
151
0
        pstate->samplesperblock = G723_16_SAMPLES_PER_BLOCK ;
152
0
        break ;
153
154
370
    case G723_24_BITS_PER_SAMPLE : /* 3 bits per sample. */
155
370
        pstate->decoder = g723_24_decoder ;
156
370
        *blocksize = G723_24_BYTES_PER_BLOCK ;
157
370
        *samplesperblock = G723_24_SAMPLES_PER_BLOCK ;
158
370
        pstate->codec_bits = 3 ;
159
370
        pstate->blocksize = G723_24_BYTES_PER_BLOCK ;
160
370
        pstate->samplesperblock = G723_24_SAMPLES_PER_BLOCK ;
161
370
        break ;
162
163
182
    case G721_32_BITS_PER_SAMPLE : /* 4 bits per sample. */
164
182
        pstate->decoder = g721_decoder ;
165
182
        *blocksize = G721_32_BYTES_PER_BLOCK ;
166
182
        *samplesperblock = G721_32_SAMPLES_PER_BLOCK ;
167
182
        pstate->codec_bits = 4 ;
168
182
        pstate->blocksize = G721_32_BYTES_PER_BLOCK ;
169
182
        pstate->samplesperblock = G721_32_SAMPLES_PER_BLOCK ;
170
182
        break ;
171
172
115
    case G721_40_BITS_PER_SAMPLE : /* 5 bits per sample. */
173
115
        pstate->decoder = g723_40_decoder ;
174
115
        *blocksize = G721_40_BYTES_PER_BLOCK ;
175
115
        *samplesperblock = G721_40_SAMPLES_PER_BLOCK ;
176
115
        pstate->codec_bits = 5 ;
177
115
        pstate->blocksize = G721_40_BYTES_PER_BLOCK ;
178
115
        pstate->samplesperblock = G721_40_SAMPLES_PER_BLOCK ;
179
115
        break ;
180
181
0
    default :
182
0
        free (pstate) ;
183
0
        return NULL ;
184
667
    } ;
185
186
667
  return pstate ;
187
667
}  /* g72x_reader_init */
188
189
struct g72x_state * g72x_writer_init (int codec, int *blocksize, int *samplesperblock)
190
0
{ G72x_STATE *pstate ;
191
192
0
  if ((pstate = g72x_state_new ()) == NULL)
193
0
    return NULL ;
194
195
0
  private_init_state (pstate) ;
196
0
  pstate->decoder = NULL ;
197
198
0
  switch (codec)
199
0
  { case G723_16_BITS_PER_SAMPLE : /* 2 bits per sample. */
200
0
        pstate->encoder = g723_16_encoder ;
201
0
        *blocksize = G723_16_BYTES_PER_BLOCK ;
202
0
        *samplesperblock = G723_16_SAMPLES_PER_BLOCK ;
203
0
        pstate->codec_bits = 2 ;
204
0
        pstate->blocksize = G723_16_BYTES_PER_BLOCK ;
205
0
        pstate->samplesperblock = G723_16_SAMPLES_PER_BLOCK ;
206
0
        break ;
207
208
0
    case G723_24_BITS_PER_SAMPLE : /* 3 bits per sample. */
209
0
        pstate->encoder = g723_24_encoder ;
210
0
        *blocksize = G723_24_BYTES_PER_BLOCK ;
211
0
        *samplesperblock = G723_24_SAMPLES_PER_BLOCK ;
212
0
        pstate->codec_bits = 3 ;
213
0
        pstate->blocksize = G723_24_BYTES_PER_BLOCK ;
214
0
        pstate->samplesperblock = G723_24_SAMPLES_PER_BLOCK ;
215
0
        break ;
216
217
0
    case G721_32_BITS_PER_SAMPLE : /* 4 bits per sample. */
218
0
        pstate->encoder = g721_encoder ;
219
0
        *blocksize = G721_32_BYTES_PER_BLOCK ;
220
0
        *samplesperblock = G721_32_SAMPLES_PER_BLOCK ;
221
0
        pstate->codec_bits = 4 ;
222
0
        pstate->blocksize = G721_32_BYTES_PER_BLOCK ;
223
0
        pstate->samplesperblock = G721_32_SAMPLES_PER_BLOCK ;
224
0
        break ;
225
226
0
    case G721_40_BITS_PER_SAMPLE : /* 5 bits per sample. */
227
0
        pstate->encoder = g723_40_encoder ;
228
0
        *blocksize = G721_40_BYTES_PER_BLOCK ;
229
0
        *samplesperblock = G721_40_SAMPLES_PER_BLOCK ;
230
0
        pstate->codec_bits = 5 ;
231
0
        pstate->blocksize = G721_40_BYTES_PER_BLOCK ;
232
0
        pstate->samplesperblock = G721_40_SAMPLES_PER_BLOCK ;
233
0
        break ;
234
235
0
    default :
236
0
        free (pstate) ;
237
0
        return NULL ;
238
0
    } ;
239
240
0
  return pstate ;
241
0
}  /* g72x_writer_init */
242
243
int g72x_decode_block (G72x_STATE *pstate, const unsigned char *block, short *samples)
244
85.4k
{ int k, count ;
245
246
85.4k
  count = unpack_bytes (pstate->codec_bits, pstate->blocksize, block, samples) ;
247
248
10.3M
  for (k = 0 ; k < count ; k++)
249
10.2M
    samples [k] = pstate->decoder (samples [k], pstate) ;
250
251
85.4k
  return 0 ;
252
85.4k
}  /* g72x_decode_block */
253
254
int g72x_encode_block (G72x_STATE *pstate, short *samples, unsigned char *block)
255
0
{ int k, count ;
256
257
0
  for (k = 0 ; k < pstate->samplesperblock ; k++)
258
0
    samples [k] = pstate->encoder (samples [k], pstate) ;
259
260
0
  count = pack_bytes (pstate->codec_bits, samples, block) ;
261
262
0
  return count ;
263
0
}  /* g72x_encode_block */
264
265
/*
266
 * predictor_zero ()
267
 *
268
 * computes the estimated signal from 6-zero predictor.
269
 *
270
 */
271
int predictor_zero (G72x_STATE *state_ptr)
272
10.2M
{
273
10.2M
  int   i ;
274
10.2M
  int   sezi ;
275
276
10.2M
  sezi = fmult (state_ptr->b [0] >> 2, state_ptr->dq [0]) ;
277
61.5M
  for (i = 1 ; i < 6 ; i++)      /* ACCUM */
278
51.2M
    sezi += fmult (state_ptr->b [i] >> 2, state_ptr->dq [i]) ;
279
10.2M
  return sezi ;
280
10.2M
}
281
/*
282
 * predictor_pole ()
283
 *
284
 * computes the estimated signal from 2-pole predictor.
285
 *
286
 */
287
int predictor_pole (G72x_STATE *state_ptr)
288
10.2M
{
289
10.2M
  return (fmult (state_ptr->a [1] >> 2, state_ptr->sr [1]) +
290
10.2M
      fmult (state_ptr->a [0] >> 2, state_ptr->sr [0])) ;
291
10.2M
}
292
/*
293
 * step_size ()
294
 *
295
 * computes the quantization step size of the adaptive quantizer.
296
 *
297
 */
298
int step_size (G72x_STATE *state_ptr)
299
10.2M
{
300
10.2M
  int   y ;
301
10.2M
  int   dif ;
302
10.2M
  int   al ;
303
304
10.2M
  if (state_ptr->ap >= 256)
305
6.87M
    return (state_ptr->yu) ;
306
3.37M
  else {
307
3.37M
    y = state_ptr->yl >> 6 ;
308
3.37M
    dif = state_ptr->yu - y ;
309
3.37M
    al = state_ptr->ap >> 2 ;
310
3.37M
    if (dif > 0)
311
1.37M
      y += (dif * al) >> 6 ;
312
2.00M
    else if (dif < 0)
313
1.99M
      y += (dif * al + 0x3F) >> 6 ;
314
3.37M
    return y ;
315
3.37M
  }
316
10.2M
}
317
318
/*
319
 * quantize ()
320
 *
321
 * Given a raw sample, 'd', of the difference signal and a
322
 * quantization step size scale factor, 'y', this routine returns the
323
 * ADPCM codeword to which that sample gets quantized.  The step
324
 * size scale factor division operation is done in the log base 2 domain
325
 * as a subtraction.
326
 */
327
int quantize (
328
  int   d,  /* Raw difference signal sample */
329
  int   y,  /* Step size multiplier */
330
  short *table, /* quantization table */
331
  int   size) /* table size of short integers */
332
0
{
333
0
  short   dqm ; /* Magnitude of 'd' */
334
0
  short   expon ; /* Integer part of base 2 log of 'd' */
335
0
  short   mant ;  /* Fractional part of base 2 log */
336
0
  short   dl ;  /* Log of magnitude of 'd' */
337
0
  short   dln ; /* Step size scale factor normalized log */
338
0
  int   i ;
339
340
  /*
341
   * LOG
342
   *
343
   * Compute base 2 log of 'd', and store in 'dl'.
344
   */
345
0
  dqm = abs (d) ;
346
0
  expon = quan (dqm >> 1, power2, 15) ;
347
0
  mant = ((dqm << 7) >> expon) & 0x7F ; /* Fractional portion. */
348
0
  dl = (expon << 7) + mant ;
349
350
  /*
351
   * SUBTB
352
   *
353
   * "Divide" by step size multiplier.
354
   */
355
0
  dln = dl - (y >> 2) ;
356
357
  /*
358
   * QUAN
359
   *
360
   * Obtain codword i for 'd'.
361
   */
362
0
  i = quan (dln, table, size) ;
363
0
  if (d < 0)     /* take 1's complement of i */
364
0
    return ((size << 1) + 1 - i) ;
365
0
  else if (i == 0)   /* take 1's complement of 0 */
366
0
    return ((size << 1) + 1) ; /* new in 1988 */
367
368
0
  return i ;
369
0
}
370
/*
371
 * reconstruct ()
372
 *
373
 * Returns reconstructed difference signal 'dq' obtained from
374
 * codeword 'i' and quantization step size scale factor 'y'.
375
 * Multiplication is performed in log base 2 domain as addition.
376
 */
377
int
378
reconstruct (
379
  int   sign, /* 0 for non-negative value */
380
  int   dqln, /* G.72x codeword */
381
  int   y)  /* Step size multiplier */
382
10.2M
{
383
10.2M
  short   dql ; /* Log of 'dq' magnitude */
384
10.2M
  short   dex ; /* Integer part of log */
385
10.2M
  short   dqt ;
386
10.2M
  short   dq ;  /* Reconstructed difference signal sample */
387
388
10.2M
  dql = dqln + (y >> 2) ; /* ADDA */
389
390
10.2M
  if (dql < 0)
391
5.57M
    return ((sign) ? -0x8000 : 0) ;
392
4.68M
  else    /* ANTILOG */
393
4.68M
  { dex = (dql >> 7) & 15 ;
394
4.68M
    dqt = 128 + (dql & 127) ;
395
4.68M
    dq = (dqt << 7) >> (14 - dex) ;
396
4.68M
    return ((sign) ? (dq - 0x8000) : dq) ;
397
4.68M
    }
398
10.2M
}
399
400
401
/*
402
 * update ()
403
 *
404
 * updates the state variables for each output code
405
 */
406
void
407
update (
408
  int   code_size,  /* distinguish 723_40 with others */
409
  int   y,    /* quantizer step size */
410
  int   wi,   /* scale factor multiplier */
411
  int   fi,   /* for long/short term energies */
412
  int   dq,   /* quantized prediction difference */
413
  int   sr,   /* reconstructed signal */
414
  int   dqsez,    /* difference from 2-pole predictor */
415
  G72x_STATE *state_ptr)  /* coder state pointer */
416
10.2M
{
417
10.2M
  int   cnt ;
418
10.2M
  short   mag, expon ;  /* Adaptive predictor, FLOAT A */
419
10.2M
  short   a2p = 0 ; /* LIMC */
420
10.2M
  short   a1ul ;    /* UPA1 */
421
10.2M
  short   pks1 ;    /* UPA2 */
422
10.2M
  short   fa1 ;
423
10.2M
  char    tr ;    /* tone/transition detector */
424
10.2M
  short   ylint, thr2, dqthr ;
425
10.2M
  short   ylfrac, thr1 ;
426
10.2M
  short   pk0 ;
427
428
10.2M
  pk0 = (dqsez < 0) ? 1 : 0 ;  /* needed in updating predictor poles */
429
430
10.2M
  mag = dq & 0x7FFF ;   /* prediction difference magnitude */
431
  /* TRANS */
432
10.2M
  ylint = state_ptr->yl >> 15 ; /* exponent part of yl */
433
10.2M
  ylfrac = (state_ptr->yl >> 10) & 0x1F ; /* fractional part of yl */
434
10.2M
  thr1 = (32 + ylfrac) << ylint ;   /* threshold */
435
10.2M
  thr2 = (ylint > 9) ? 31 << 10 : thr1 ;  /* limit thr2 to 31 << 10 */
436
10.2M
  dqthr = (thr2 + (thr2 >> 1)) >> 1 ; /* dqthr = 0.75 * thr2 */
437
10.2M
  if (state_ptr->td == 0)    /* signal supposed voice */
438
10.0M
    tr = 0 ;
439
156k
  else if (mag <= dqthr)    /* supposed data, but small mag */
440
154k
    tr = 0 ;      /* treated as voice */
441
1.24k
  else        /* signal is data (modem) */
442
1.24k
    tr = 1 ;
443
444
  /*
445
   * Quantizer scale factor adaptation.
446
   */
447
448
  /* FUNCTW & FILTD & DELAY */
449
  /* update non-steady state step size multiplier */
450
10.2M
  state_ptr->yu = y + ((wi - y) >> 5) ;
451
452
  /* LIMB */
453
10.2M
  if (state_ptr->yu < 544)  /* 544 <= yu <= 5120 */
454
2.81M
    state_ptr->yu = 544 ;
455
7.43M
  else if (state_ptr->yu > 5120)
456
666k
    state_ptr->yu = 5120 ;
457
458
  /* FILTE & DELAY */
459
  /* update steady state step size multiplier */
460
10.2M
  state_ptr->yl += state_ptr->yu + ((-state_ptr->yl) >> 6) ;
461
462
  /*
463
   * Adaptive predictor coefficients.
464
   */
465
10.2M
  if (tr == 1) {     /* reset a's and b's for modem signal */
466
1.24k
    state_ptr->a [0] = 0 ;
467
1.24k
    state_ptr->a [1] = 0 ;
468
1.24k
    state_ptr->b [0] = 0 ;
469
1.24k
    state_ptr->b [1] = 0 ;
470
1.24k
    state_ptr->b [2] = 0 ;
471
1.24k
    state_ptr->b [3] = 0 ;
472
1.24k
    state_ptr->b [4] = 0 ;
473
1.24k
    state_ptr->b [5] = 0 ;
474
1.24k
    }
475
10.2M
  else      /* update a's and b's */
476
10.2M
  { pks1 = pk0 ^ state_ptr->pk [0] ;    /* UPA2 */
477
478
    /* update predictor pole a [1] */
479
10.2M
    a2p = state_ptr->a [1] - (state_ptr->a [1] >> 7) ;
480
10.2M
    if (dqsez != 0)
481
6.29M
    { fa1 = (pks1) ? state_ptr->a [0] : -state_ptr->a [0] ;
482
6.29M
      if (fa1 < -8191)  /* a2p = function of fa1 */
483
525k
        a2p -= 0x100 ;
484
5.76M
      else if (fa1 > 8191)
485
258k
        a2p += 0xFF ;
486
5.50M
      else
487
5.50M
        a2p += fa1 >> 5 ;
488
489
6.29M
      if (pk0 ^ state_ptr->pk [1])
490
3.41M
      { /* LIMC */
491
3.41M
        if (a2p <= -12160)
492
57.8k
          a2p = -12288 ;
493
3.35M
        else if (a2p >= 12416)
494
0
          a2p = 12288 ;
495
3.35M
        else
496
3.35M
          a2p -= 0x80 ;
497
3.41M
        }
498
2.88M
      else if (a2p <= -12416)
499
30.6k
        a2p = -12288 ;
500
2.84M
      else if (a2p >= 12160)
501
266
        a2p = 12288 ;
502
2.84M
      else
503
2.84M
        a2p += 0x80 ;
504
6.29M
    }
505
506
    /* TRIGB & DELAY */
507
10.2M
    state_ptr->a [1] = a2p ;
508
509
    /* UPA1 */
510
    /* update predictor pole a [0] */
511
10.2M
    state_ptr->a [0] -= state_ptr->a [0] >> 8 ;
512
10.2M
    if (dqsez != 0)
513
6.29M
    { if (pks1 == 0)
514
3.10M
        state_ptr->a [0] += 192 ;
515
3.18M
      else
516
3.18M
        state_ptr->a [0] -= 192 ;
517
6.29M
      } ;
518
519
    /* LIMD */
520
10.2M
    a1ul = 15360 - a2p ;
521
10.2M
    if (state_ptr->a [0] < -a1ul)
522
4.24k
      state_ptr->a [0] = -a1ul ;
523
10.2M
    else if (state_ptr->a [0] > a1ul)
524
3.87k
      state_ptr->a [0] = a1ul ;
525
526
    /* UPB : update predictor zeros b [6] */
527
71.7M
    for (cnt = 0 ; cnt < 6 ; cnt++)
528
61.5M
    { if (code_size == 5)    /* for 40Kbps G.723 */
529
31.4M
        state_ptr->b [cnt] -= state_ptr->b [cnt] >> 9 ;
530
30.0M
      else      /* for G.721 and 24Kbps G.723 */
531
30.0M
        state_ptr->b [cnt] -= state_ptr->b [cnt] >> 8 ;
532
61.5M
      if (dq & 0x7FFF)      /* XOR */
533
28.0M
      { if ((dq ^ state_ptr->dq [cnt]) >= 0)
534
13.6M
          state_ptr->b [cnt] += 128 ;
535
14.4M
        else
536
14.4M
          state_ptr->b [cnt] -= 128 ;
537
28.0M
        }
538
61.5M
      }
539
10.2M
    }
540
541
61.5M
  for (cnt = 5 ; cnt > 0 ; cnt--)
542
51.2M
    state_ptr->dq [cnt] = state_ptr->dq [cnt - 1] ;
543
  /* FLOAT A : convert dq [0] to 4-bit exp, 6-bit mantissa f.p. */
544
10.2M
  if (mag == 0)
545
5.57M
    state_ptr->dq [0] = (dq >= 0) ? 0x20 : 0xFC20 ;
546
4.68M
  else
547
4.68M
  { expon = quan (mag, power2, 15) ;
548
4.68M
    state_ptr->dq [0] = (dq >= 0) ?
549
2.51M
      (expon << 6) + ((mag << 6) >> expon) :
550
4.68M
      (expon << 6) + ((mag << 6) >> expon) - 0x400 ;
551
4.68M
    }
552
553
10.2M
  state_ptr->sr [1] = state_ptr->sr [0] ;
554
  /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
555
10.2M
  if (sr == 0)
556
3.68M
    state_ptr->sr [0] = 0x20 ;
557
6.56M
  else if (sr > 0)
558
3.17M
  { expon = quan (sr, power2, 15) ;
559
3.17M
    state_ptr->sr [0] = (expon << 6) + ((sr << 6) >> expon) ;
560
3.17M
    }
561
3.38M
  else if (sr > -32768)
562
3.38M
  { mag = -sr ;
563
3.38M
    expon = quan (mag, power2, 15) ;
564
3.38M
    state_ptr->sr [0] = (expon << 6) + ((mag << 6) >> expon) - 0x400 ;
565
3.38M
    }
566
192
  else
567
192
    state_ptr->sr [0] = (short) 0xFC20 ;
568
569
  /* DELAY A */
570
10.2M
  state_ptr->pk [1] = state_ptr->pk [0] ;
571
10.2M
  state_ptr->pk [0] = pk0 ;
572
573
  /* TONE */
574
10.2M
  if (tr == 1)    /* this sample has been treated as data */
575
1.24k
    state_ptr->td = 0 ;  /* next one will be treated as voice */
576
10.2M
  else if (a2p < -11776)  /* small sample-to-sample correlation */
577
156k
    state_ptr->td = 1 ;  /* signal may be data */
578
10.0M
  else        /* signal is voice */
579
10.0M
    state_ptr->td = 0 ;
580
581
  /*
582
   * Adaptation speed control.
583
   */
584
10.2M
  state_ptr->dms += (fi - state_ptr->dms) >> 5 ;    /* FILTA */
585
10.2M
  state_ptr->dml += (((fi << 2) - state_ptr->dml) >> 7) ; /* FILTB */
586
587
10.2M
  if (tr == 1)
588
1.24k
    state_ptr->ap = 256 ;
589
10.2M
  else if (y < 1536)          /* SUBTC */
590
3.41M
    state_ptr->ap += (0x200 - state_ptr->ap) >> 4 ;
591
6.83M
  else if (state_ptr->td == 1)
592
155k
    state_ptr->ap += (0x200 - state_ptr->ap) >> 4 ;
593
6.68M
  else if (abs ((state_ptr->dms << 2) - state_ptr->dml) >= (state_ptr->dml >> 3))
594
3.21M
    state_ptr->ap += (0x200 - state_ptr->ap) >> 4 ;
595
3.47M
  else
596
3.47M
    state_ptr->ap += (-state_ptr->ap) >> 4 ;
597
598
10.2M
  return ;
599
10.2M
} /* update */
600
601
/*------------------------------------------------------------------------------
602
*/
603
604
static int
605
unpack_bytes (int bits, int blocksize, const unsigned char * block, short * samples)
606
85.4k
{ unsigned int  in_buffer = 0 ;
607
85.4k
  unsigned char in_byte ;
608
85.4k
  int       k, in_bits = 0, bindex = 0 ;
609
610
10.3M
  for (k = 0 ; bindex <= blocksize && k < G72x_BLOCK_SIZE ; k++)
611
10.2M
  { if (in_bits < bits)
612
5.16M
    { in_byte = block [bindex++] ;
613
614
5.16M
      in_buffer |= (in_byte << in_bits) ;
615
5.16M
      in_bits += 8 ;
616
5.16M
      }
617
10.2M
    samples [k] = in_buffer & ((1 << bits) - 1) ;
618
10.2M
    in_buffer >>= bits ;
619
10.2M
    in_bits -= bits ;
620
10.2M
    } ;
621
622
85.4k
  return k ;
623
85.4k
} /* unpack_bytes */
624
625
static int
626
pack_bytes (int bits, const short * samples, unsigned char * block)
627
0
{
628
0
  unsigned int  out_buffer = 0 ;
629
0
  int       k, bindex = 0, out_bits = 0 ;
630
0
  unsigned char out_byte ;
631
632
0
  for (k = 0 ; k < G72x_BLOCK_SIZE ; k++)
633
0
  { out_buffer |= (samples [k] << out_bits) ;
634
0
    out_bits += bits ;
635
0
    if (out_bits >= 8)
636
0
    { out_byte = out_buffer & 0xFF ;
637
0
      out_bits -= 8 ;
638
0
      out_buffer >>= 8 ;
639
0
      block [bindex++] = out_byte ;
640
0
      }
641
0
    } ;
642
643
0
  return bindex ;
644
0
} /* pack_bytes */
645