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