g722_enc_dec.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * g722.h - The ITU G.722 codec.
  5. *
  6. * Written by Steve Underwood <[email protected]>
  7. *
  8. * Copyright (C) 2005 Steve Underwood
  9. *
  10. * Despite my general liking of the GPL, I place my own contributions
  11. * to this code in the public domain for the benefit of all mankind -
  12. * even the slimy ones who might try to proprietize my work and use it
  13. * to my detriment.
  14. *
  15. * Based on a single channel G.722 codec which is:
  16. *
  17. ***** Copyright (c) CMU 1993 *****
  18. * Computer Science, Speech Group
  19. * Chengxiang Lu and Alex Hauptmann
  20. *
  21. * $Id: g722.h 48959 2006-12-25 06:42:15Z rizzo $
  22. */
  23. /*! \file */
  24. #if !defined(_G722_H_)
  25. #define _G722_H_
  26. /*! \page g722_page G.722 encoding and decoding
  27. \section g722_page_sec_1 What does it do?
  28. The G.722 module is a bit exact implementation of the ITU G.722 specification for all three
  29. specified bit rates - 64000bps, 56000bps and 48000bps. It passes the ITU tests.
  30. To allow fast and flexible interworking with narrow band telephony, the encoder and decoder
  31. support an option for the linear audio to be an 8k samples/second stream. In this mode the
  32. codec is considerably faster, and still fully compatible with wideband terminals using G.722.
  33. \section g722_page_sec_2 How does it work?
  34. ???.
  35. */
  36. /* Format DAC12 is added to decode directly into samples suitable for
  37. a 12-bit DAC using offset binary representation. */
  38. enum
  39. {
  40. G722_SAMPLE_RATE_8000 = 0x0001,
  41. G722_PACKED = 0x0002,
  42. G722_FORMAT_DAC12 = 0x0004,
  43. };
  44. #ifdef BUILD_FEATURE_DAC
  45. #define NLDECOMPRESS_APPLY_GAIN(s,g) (((s) * (int32_t)(g)) >> 16)
  46. // Equivalent to shift 16, add 0x8000, shift 4
  47. #define NLDECOMPRESS_APPLY_GAIN_CONVERTED_DAC(s,g) (uint16_t)((uint16_t)(((s) * (int32_t)(g)) >> 20) + 0x800)
  48. #else
  49. #define NLDECOMPRESS_APPLY_GAIN(s,g) (((int32_t)(s) * (int32_t)(g)) >> 16)
  50. #endif
  51. #ifdef BUILD_FEATURE_DAC
  52. #define NLDECOMPRESS_PREPROCESS_PCM_SAMPLE_WITH_GAIN(s,g) NLDECOMPRESS_APPLY_GAIN_CONVERTED_DAC((s),(g))
  53. #define NLDECOMPRESS_PREPROCESS_SAMPLE_WITH_GAIN(s,g) ((int16_t)NLDECOMPRESS_APPLY_GAIN((s),(g)))
  54. #else
  55. #define NLDECOMPRESS_PREPROCESS_PCM_SAMPLE_WITH_GAIN NLDECOMPRESS_PREPROCESS_SAMPLE_WITH_GAIN
  56. #define NLDECOMPRESS_PREPROCESS_SAMPLE_WITH_GAIN(s,g) ((int16_t)(NLDECOMPRESS_APPLY_GAIN((s),(g))))
  57. #endif
  58. typedef struct {
  59. int s;
  60. int sp;
  61. int sz;
  62. int r[3];
  63. int a[3];
  64. int ap[3];
  65. int p[3];
  66. int d[7];
  67. int b[7];
  68. int bp[7];
  69. int nb;
  70. int det;
  71. } g722_band_t;
  72. typedef struct
  73. {
  74. /*! TRUE if the operating in the special ITU test mode, with the band split filters
  75. disabled. */
  76. int itu_test_mode;
  77. /*! TRUE if the G.722 data is packed */
  78. int packed;
  79. /*! TRUE if encode from 8k samples/second */
  80. int eight_k;
  81. /*! 6 for 48000kbps, 7 for 56000kbps, or 8 for 64000kbps. */
  82. int bits_per_sample;
  83. /*! Signal history for the QMF */
  84. int x[24];
  85. g722_band_t band[2];
  86. unsigned int in_buffer;
  87. int in_bits;
  88. unsigned int out_buffer;
  89. int out_bits;
  90. } g722_encode_state_t;
  91. typedef struct
  92. {
  93. /*! TRUE if the operating in the special ITU test mode, with the band split filters
  94. disabled. */
  95. int itu_test_mode;
  96. /*! TRUE if the G.722 data is packed */
  97. int packed;
  98. /*! TRUE if decode to 8k samples/second */
  99. int eight_k;
  100. /*! 6 for 48000kbps, 7 for 56000kbps, or 8 for 64000kbps. */
  101. int bits_per_sample;
  102. /*! TRUE if offset binary for a 12-bit DAC */
  103. int dac_pcm;
  104. /*! Signal history for the QMF */
  105. int x[24];
  106. g722_band_t band[2];
  107. unsigned int in_buffer;
  108. int in_bits;
  109. unsigned int out_buffer;
  110. int out_bits;
  111. } g722_decode_state_t;
  112. #ifdef __cplusplus
  113. extern "C" {
  114. #endif
  115. g722_encode_state_t *g722_encode_init(g722_encode_state_t *s, unsigned int rate, int options);
  116. int g722_encode_release(g722_encode_state_t *s);
  117. int g722_encode(g722_encode_state_t *s, uint8_t g722_data[], const int16_t amp[], int len);
  118. g722_decode_state_t *g722_decode_init(g722_decode_state_t *s, unsigned int rate, int options);
  119. int g722_decode_release(g722_decode_state_t *s);
  120. uint32_t g722_decode(g722_decode_state_t *s, int16_t amp[], const uint8_t g722_data[], int len, uint16_t aGain);
  121. #ifdef __cplusplus
  122. }
  123. #endif
  124. #endif