alaw_ulaw.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJMEDIA_ALAW_ULAW_H__
  20. #define __PJMEDIA_ALAW_ULAW_H__
  21. #include <pjmedia/types.h>
  22. PJ_BEGIN_DECL
  23. #if defined(PJMEDIA_HAS_ALAW_ULAW_TABLE) && PJMEDIA_HAS_ALAW_ULAW_TABLE!=0
  24. extern const pj_uint8_t pjmedia_linear2ulaw_tab[16384];
  25. extern const pj_uint8_t pjmedia_linear2alaw_tab[16384];
  26. extern const pj_int16_t pjmedia_ulaw2linear_tab[256];
  27. extern const pj_int16_t pjmedia_alaw2linear_tab[256];
  28. /**
  29. * Convert 16-bit linear PCM value to 8-bit A-Law.
  30. *
  31. * @param pcm_val 16-bit linear PCM value.
  32. * @return 8-bit A-Law value.
  33. */
  34. #define pjmedia_linear2alaw(pcm_val) \
  35. pjmedia_linear2alaw_tab[(((pj_int16_t)pcm_val) >> 2) & 0x3fff]
  36. /**
  37. * Convert 8-bit A-Law value to 16-bit linear PCM value.
  38. *
  39. * @param chara_val 8-bit A-Law value.
  40. * @return 16-bit linear PCM value.
  41. */
  42. #define pjmedia_alaw2linear(chara_val) \
  43. pjmedia_alaw2linear_tab[chara_val]
  44. /**
  45. * Convert 16-bit linear PCM value to 8-bit U-Law.
  46. *
  47. * @param pcm_val 16-bit linear PCM value.
  48. * @return U-bit A-Law value.
  49. */
  50. #define pjmedia_linear2ulaw(pcm_val) \
  51. pjmedia_linear2ulaw_tab[(((pj_int16_t)pcm_val) >> 2) & 0x3fff]
  52. /**
  53. * Convert 8-bit U-Law value to 16-bit linear PCM value.
  54. *
  55. * @param u_val 8-bit U-Law value.
  56. * @return 16-bit linear PCM value.
  57. */
  58. #define pjmedia_ulaw2linear(u_val) \
  59. pjmedia_ulaw2linear_tab[u_val]
  60. /**
  61. * Convert 8-bit A-Law value to 8-bit U-Law value.
  62. *
  63. * @param aval 8-bit A-Law value.
  64. * @return 8-bit U-Law value.
  65. */
  66. #define pjmedia_alaw2ulaw(aval) \
  67. pjmedia_linear2ulaw(pjmedia_alaw2linear(aval))
  68. /**
  69. * Convert 8-bit U-Law value to 8-bit A-Law value.
  70. *
  71. * @param uval 8-bit U-Law value.
  72. * @return 8-bit A-Law value.
  73. */
  74. #define pjmedia_ulaw2alaw(uval) \
  75. pjmedia_linear2alaw(pjmedia_ulaw2linear(uval))
  76. #else
  77. /**
  78. * Convert 16-bit linear PCM value to 8-bit A-Law.
  79. *
  80. * @param pcm_val 16-bit linear PCM value.
  81. * @return 8-bit A-Law value.
  82. */
  83. PJ_DECL(pj_uint8_t) pjmedia_linear2alaw(int pcm_val);
  84. /**
  85. * Convert 8-bit A-Law value to 16-bit linear PCM value.
  86. *
  87. * @param chara_val 8-bit A-Law value.
  88. * @return 16-bit linear PCM value.
  89. */
  90. PJ_DECL(int) pjmedia_alaw2linear(unsigned chara_val);
  91. /**
  92. * Convert 16-bit linear PCM value to 8-bit U-Law.
  93. *
  94. * @param pcm_val 16-bit linear PCM value.
  95. * @return U-bit A-Law value.
  96. */
  97. PJ_DECL(unsigned char) pjmedia_linear2ulaw(int pcm_val);
  98. /**
  99. * Convert 8-bit U-Law value to 16-bit linear PCM value.
  100. *
  101. * @param u_val 8-bit U-Law value.
  102. * @return 16-bit linear PCM value.
  103. */
  104. PJ_DECL(int) pjmedia_ulaw2linear(unsigned char u_val);
  105. /**
  106. * Convert 8-bit A-Law value to 8-bit U-Law value.
  107. *
  108. * @param aval 8-bit A-Law value.
  109. * @return 8-bit U-Law value.
  110. */
  111. PJ_DECL(unsigned char) pjmedia_alaw2ulaw(unsigned char aval);
  112. /**
  113. * Convert 8-bit U-Law value to 8-bit A-Law value.
  114. *
  115. * @param uval 8-bit U-Law value.
  116. * @return 8-bit A-Law value.
  117. */
  118. PJ_DECL(unsigned char) pjmedia_ulaw2alaw(unsigned char uval);
  119. #endif
  120. /**
  121. * Encode 16-bit linear PCM data to 8-bit U-Law data.
  122. *
  123. * @param dst Destination buffer for 8-bit U-Law data.
  124. * @param src Source, 16-bit linear PCM data.
  125. * @param count Number of samples.
  126. */
  127. PJ_INLINE(void) pjmedia_ulaw_encode(pj_uint8_t *dst, const pj_int16_t *src,
  128. pj_size_t count)
  129. {
  130. const pj_int16_t *end = src + count;
  131. while (src < end) {
  132. *dst++ = pjmedia_linear2ulaw(*src++);
  133. }
  134. }
  135. /**
  136. * Encode 16-bit linear PCM data to 8-bit A-Law data.
  137. *
  138. * @param dst Destination buffer for 8-bit A-Law data.
  139. * @param src Source, 16-bit linear PCM data.
  140. * @param count Number of samples.
  141. */
  142. PJ_INLINE(void) pjmedia_alaw_encode(pj_uint8_t *dst, const pj_int16_t *src,
  143. pj_size_t count)
  144. {
  145. const pj_int16_t *end = src + count;
  146. while (src < end) {
  147. *dst++ = pjmedia_linear2alaw(*src++);
  148. }
  149. }
  150. /**
  151. * Decode 8-bit U-Law data to 16-bit linear PCM data.
  152. *
  153. * @param dst Destination buffer for 16-bit PCM data.
  154. * @param src Source, 8-bit U-Law data.
  155. * @param len Encoded frame/source length in bytes.
  156. */
  157. PJ_INLINE(void) pjmedia_ulaw_decode(pj_int16_t *dst, const pj_uint8_t *src,
  158. pj_size_t len)
  159. {
  160. const pj_uint8_t *end = src + len;
  161. while (src < end) {
  162. *dst++ = pjmedia_ulaw2linear(*src++);
  163. }
  164. }
  165. /**
  166. * Decode 8-bit A-Law data to 16-bit linear PCM data.
  167. *
  168. * @param dst Destination buffer for 16-bit PCM data.
  169. * @param src Source, 8-bit A-Law data.
  170. * @param len Encoded frame/source length in bytes.
  171. */
  172. PJ_INLINE(void) pjmedia_alaw_decode(pj_int16_t *dst, const pj_uint8_t *src,
  173. pj_size_t len)
  174. {
  175. const pj_uint8_t *end = src + len;
  176. while (src < end) {
  177. *dst++ = pjmedia_alaw2linear(*src++);
  178. }
  179. }
  180. PJ_END_DECL
  181. #endif /* __PJMEDIA_ALAW_ULAW_H__ */