/project/jni/sndfile/src/G72x/g723_16.c

https://github.com/aichunyu/FFPlayer · C · 162 lines · 56 code · 27 blank · 79 comment · 4 complexity · daf95cfc7f2169e10459be7215cca5ab MD5 · raw file

  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. /* 16kbps version created, used 24kbps code and changing as little as possible.
  27. * G.726 specs are available from ITU's gopher or WWW site (http://www.itu.ch)
  28. * If any errors are found, please contact me at mrand@tamu.edu
  29. * -Marc Randolph
  30. */
  31. /*
  32. * g723_16.c
  33. *
  34. * Description:
  35. *
  36. * g723_16_encoder(), g723_16_decoder()
  37. *
  38. * These routines comprise an implementation of the CCITT G.726 16 Kbps
  39. * ADPCM coding algorithm. Essentially, this implementation is identical to
  40. * the bit level description except for a few deviations which take advantage
  41. * of workstation attributes, such as hardware 2's complement arithmetic.
  42. *
  43. */
  44. #include "g72x.h"
  45. #include "g72x_priv.h"
  46. /*
  47. * Maps G.723_16 code word to reconstructed scale factor normalized log
  48. * magnitude values. Comes from Table 11/G.726
  49. */
  50. static short _dqlntab[4] = { 116, 365, 365, 116};
  51. /* Maps G.723_16 code word to log of scale factor multiplier.
  52. *
  53. * _witab[4] is actually {-22 , 439, 439, -22}, but FILTD wants it
  54. * as WI << 5 (multiplied by 32), so we'll do that here
  55. */
  56. static short _witab[4] = {-704, 14048, 14048, -704};
  57. /*
  58. * Maps G.723_16 code words to a set of values whose long and short
  59. * term averages are computed and then compared to give an indication
  60. * how stationary (steady state) the signal is.
  61. */
  62. /* Comes from FUNCTF */
  63. static short _fitab[4] = {0, 0xE00, 0xE00, 0};
  64. /* Comes from quantizer decision level tables (Table 7/G.726)
  65. */
  66. static short qtab_723_16[1] = {261};
  67. /*
  68. * g723_16_encoder()
  69. *
  70. * Encodes a linear PCM, A-law or u-law input sample and returns its 2-bit code.
  71. * Returns -1 if invalid input coding value.
  72. */
  73. int
  74. g723_16_encoder(
  75. int sl,
  76. G72x_STATE *state_ptr)
  77. {
  78. short sei, sezi, se, sez; /* ACCUM */
  79. short d; /* SUBTA */
  80. short y; /* MIX */
  81. short sr; /* ADDB */
  82. short dqsez; /* ADDC */
  83. short dq, i;
  84. /* linearize input sample to 14-bit PCM */
  85. sl >>= 2; /* sl of 14-bit dynamic range */
  86. sezi = predictor_zero(state_ptr);
  87. sez = sezi >> 1;
  88. sei = sezi + predictor_pole(state_ptr);
  89. se = sei >> 1; /* se = estimated signal */
  90. d = sl - se; /* d = estimation diff. */
  91. /* quantize prediction difference d */
  92. y = step_size(state_ptr); /* quantizer step size */
  93. i = quantize(d, y, qtab_723_16, 1); /* i = ADPCM code */
  94. /* Since quantize() only produces a three level output
  95. * (1, 2, or 3), we must create the fourth one on our own
  96. */
  97. if (i == 3) /* i code for the zero region */
  98. if ((d & 0x8000) == 0) /* If d > 0, i=3 isn't right... */
  99. i = 0;
  100. dq = reconstruct(i & 2, _dqlntab[i], y); /* quantized diff. */
  101. sr = (dq < 0) ? se - (dq & 0x3FFF) : se + dq; /* reconstructed signal */
  102. dqsez = sr + sez - se; /* pole prediction diff. */
  103. update(2, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
  104. return (i);
  105. }
  106. /*
  107. * g723_16_decoder()
  108. *
  109. * Decodes a 2-bit CCITT G.723_16 ADPCM code and returns
  110. * the resulting 16-bit linear PCM, A-law or u-law sample value.
  111. * -1 is returned if the output coding is unknown.
  112. */
  113. int
  114. g723_16_decoder(
  115. int i,
  116. G72x_STATE *state_ptr)
  117. {
  118. short sezi, sei, sez, se; /* ACCUM */
  119. short y; /* MIX */
  120. short sr; /* ADDB */
  121. short dq;
  122. short dqsez;
  123. i &= 0x03; /* mask to get proper bits */
  124. sezi = predictor_zero(state_ptr);
  125. sez = sezi >> 1;
  126. sei = sezi + predictor_pole(state_ptr);
  127. se = sei >> 1; /* se = estimated signal */
  128. y = step_size(state_ptr); /* adaptive quantizer step size */
  129. dq = reconstruct(i & 0x02, _dqlntab[i], y); /* unquantize pred diff */
  130. sr = (dq < 0) ? (se - (dq & 0x3FFF)) : (se + dq); /* reconst. signal */
  131. dqsez = sr - se + sez; /* pole prediction diff. */
  132. update(2, y, _witab[i], _fitab[i], dq, sr, dqsez, state_ptr);
  133. /* sr was of 14-bit dynamic range */
  134. return (sr << 2);
  135. }