defs.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /***********************************************************************
  2. **
  3. ** ITU-T G.722.1 (2005-05) - Fixed point implementation for main body and Annex C
  4. ** > Software Release 2.1 (2008-06)
  5. ** (Simple repackaging; no change from 2005-05 Release 2.0 code)
  6. **
  7. ** © 2004 Polycom, Inc.
  8. **
  9. ** All rights reserved.
  10. **
  11. ***********************************************************************/
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include <stdlib.h>
  15. #include "g7221/common/typedef.h"
  16. #include "g7221/common/basic_op.h"
  17. #define PI 3.141592653589793238462
  18. #define MAX_DCT_LENGTH 640
  19. #define DCT_LENGTH 320
  20. #define DCT_LENGTH_DIV_2 160
  21. #define DCT_LENGTH_DIV_4 80
  22. #define DCT_LENGTH_DIV_8 40
  23. #define DCT_LENGTH_DIV_16 20
  24. #define DCT_LENGTH_DIV_32 10
  25. #define DCT_LENGTH_DIV_64 5
  26. #define MAX(a,b) (a > b ? a : b)
  27. #define MIN(a,b) (a < b ? a : b)
  28. #define NUM_CATEGORIES 8
  29. #define NUM_CATEGORIZATION_CONTROL_BITS 4
  30. #define NUM_CATEGORIZATION_CONTROL_POSSIBILITIES 16
  31. #define CORE_SIZE 10
  32. #define DCT_LENGTH_LOG 6
  33. #define MAX_DCT_LENGTH_LOG 7
  34. /* region_size = (BLOCK_SIZE * 0.875)/NUM_REGIONS; */
  35. #define NUMBER_OF_REGIONS 14
  36. #define MAX_NUMBER_OF_REGIONS 28
  37. #define REGION_SIZE 20
  38. #define NUMBER_OF_VALID_COEFS (NUMBER_OF_REGIONS * REGION_SIZE)
  39. #define MAX_NUMBER_OF_VALID_COEFS (MAX_NUMBER_OF_REGIONS * REGION_SIZE)
  40. #define REGION_POWER_TABLE_SIZE 64
  41. #define REGION_POWER_TABLE_NUM_NEGATIVES 24
  42. #define MAX_NUM_CATEGORIZATION_CONTROL_BITS 5
  43. #define MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES 32
  44. #define ENCODER_SCALE_FACTOR 18318.0
  45. /* The MLT output is incorrectly scaled by the factor
  46. product of ENCODER_SCALE_FACTOR and sqrt(160.)
  47. This is now (9/30/96) 1.0/2^(4.5) or 1/22.627.
  48. In the current implementation this
  49. must be an integer power of sqrt(2). The
  50. integer power is ESF_ADJUSTMENT_TO_RMS_INDEX.
  51. The -2 is to conform with the range defined in the spec. */
  52. #define ESF_ADJUSTMENT_TO_RMS_INDEX (9-2)
  53. #define INTERMEDIATE_FILES_FLAG 0
  54. /* Max bit rate is 48000 bits/sec. */
  55. #define MAX_BITS_PER_FRAME 960
  56. /***************************************************************************/
  57. /* Type definitions */
  58. /***************************************************************************/
  59. typedef struct
  60. {
  61. Word16 code_bit_count; /* bit count of the current word */
  62. Word16 current_word; /* current word in the bitstream being processed */
  63. Word16 *code_word_ptr; /* pointer to the bitstream */
  64. Word16 number_of_bits_left; /* number of bits left in the current word */
  65. Word16 next_bit; /* next bit in the current word */
  66. }Bit_Obj;
  67. typedef struct
  68. {
  69. Word16 seed0;
  70. Word16 seed1;
  71. Word16 seed2;
  72. Word16 seed3;
  73. }Rand_Obj;
  74. /***************************************************************************/
  75. /* Function definitions */
  76. /***************************************************************************/
  77. extern Word16 compute_region_powers(Word16 *mlt_coefs,
  78. Word16 mag_shift,
  79. Word16 *drp_num_bits,
  80. UWord16 *drp_code_bits,
  81. Word16 *absolute_region_power_index,
  82. Word16 number_of_regions);
  83. void vector_quantize_mlts(Word16 number_of_available_bits,
  84. Word16 number_of_regions,
  85. Word16 num_categorization_control_possibilities,
  86. Word16 *mlt_coefs,
  87. Word16 *absolute_region_power_index,
  88. Word16 *power_categories,
  89. Word16 *category_balances,
  90. Word16 *p_categorization_control,
  91. Word16 *region_mlt_bit_counts,
  92. UWord32 *region_mlt_bits);
  93. Word16 vector_huffman(Word16 category,
  94. Word16 power_index,
  95. Word16 *raw_mlt_ptr,
  96. UWord32 *word_ptr);
  97. void adjust_abs_region_power_index(Word16 *absolute_region_power_index,Word16 *mlt_coefs,Word16 number_of_regions);
  98. void bits_to_words(UWord32 *region_mlt_bits,Word16 *region_mlt_bit_counts,
  99. Word16 *drp_num_bits,UWord16 *drp_code_bits,Word16 *out_words,
  100. Word16 categorization_control, Word16 number_of_regions,
  101. Word16 num_categorization_control_bits, Word16 number_of_bits_per_frame);
  102. void encoder(Word16 number_of_available_bits,
  103. Word16 number_of_regions,
  104. Word16 *mlt_coefs,
  105. Word16 mag_shift,
  106. Word16 *out_words);
  107. void decoder(Bit_Obj *bitobj,
  108. Rand_Obj *randobj,
  109. Word16 number_of_regions,
  110. Word16 *decoder_mlt_coefs,
  111. Word16 *p_mag_shift,
  112. Word16 *p_old_mag_shift,
  113. Word16 *old_decoder_mlt_coefs,
  114. Word16 frame_error_flag);
  115. Word16 samples_to_rmlt_coefs(const Word16 *new_samples,Word16 *history,Word16 *coefs,Word16 dct_length);
  116. void rmlt_coefs_to_samples(Word16 *coefs,
  117. Word16 *old_samples,
  118. Word16 *out_samples,
  119. Word16 dct_length,
  120. Word16 mag_shift);
  121. Word16 index_to_array(Word16 index,Word16 *array,Word16 category);
  122. void categorize(Word16 number_of_available_bits,
  123. Word16 number_of_regions,
  124. Word16 num_categorization_control_possibilities,
  125. Word16 *rms_index,
  126. Word16 *power_categories,
  127. Word16 *category_balances);
  128. Word16 calc_offset(Word16 *rms_index,Word16 number_of_regions,Word16 available_bits);
  129. void compute_raw_pow_categories(Word16 *power_categories,Word16 *rms_index,Word16 number_of_regions,Word16 offset);
  130. void comp_powercat_and_catbalance(Word16 *power_categories,
  131. Word16 *category_balances,
  132. Word16 *rms_index,
  133. Word16 number_of_available_bits,
  134. Word16 number_of_regions,
  135. Word16 num_categorization_control_possibilities,
  136. Word16 offset);
  137. void dct_type_iv_a (Word16 *input,Word16 *output,Word16 dct_length);
  138. void dct_type_iv_s(Word16 *input,Word16 *output,Word16 dct_length);
  139. void decode_envelope(Bit_Obj *bitobj,
  140. Word16 number_of_regions,
  141. Word16 *decoder_region_standard_deviation,
  142. Word16 *absolute_region_power_index,
  143. Word16 *p_mag_shift);
  144. void decode_vector_quantized_mlt_indices(Bit_Obj *bitobj,
  145. Rand_Obj *randobj,
  146. Word16 number_of_regions,
  147. Word16 *decoder_region_standard_deviation,
  148. Word16 *dedecoder_power_categories,
  149. Word16 *dedecoder_mlt_coefs);
  150. void rate_adjust_categories(Word16 categorization_control,
  151. Word16 *decoder_power_categories,
  152. Word16 *decoder_category_balances);
  153. void get_next_bit(Bit_Obj *bitobj);
  154. Word16 get_rand(Rand_Obj *randobj);
  155. void test_4_frame_errors(Bit_Obj *bitobj,
  156. Word16 number_of_regions,
  157. Word16 num_categorization_control_possibilities,
  158. Word16 *frame_error_flag,
  159. Word16 categorization_control,
  160. Word16 *absolute_region_power_index);
  161. void error_handling(Word16 number_of_coefs,
  162. Word16 number_of_valid_coefs,
  163. Word16 *frame_error_flag,
  164. Word16 *decoder_mlt_coefs,
  165. Word16 *old_decoder_mlt_coefs,
  166. Word16 *p_mag_shift,
  167. Word16 *p_old_mag_shift);