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 | /**************************************************************************************** |
| 14 | Filename: common.c |
| 15 | |
| 16 | Purpose: Contains the functions used for both G.722.1 Annex C encoder and decoder |
| 17 | |
| 18 | Design Notes: |
| 19 | |
| 20 | ****************************************************************************************/ |
| 21 | /**************************************************************************************** |
| 22 | Include files |
| 23 | ****************************************************************************************/ |
| 24 | #include "defs.h" |
| 25 | #include "huff_def.h" |
| 26 | #include "huff_tab.h" |
| 27 | #include "tables.h" |
| 28 | #include "count.h" |
| 29 | |
| 30 | /**************************************************************************************** |
| 31 | Function: categorize |
| 32 | |
| 33 | Syntax: void categorize(Word16 number_of_available_bits, |
| 34 | Word16 number_of_regions, |
| 35 | Word16 num_categorization_control_possibilities, |
| 36 | Word16 rms_index, |
| 37 | Word16 power_categories, |
| 38 | Word16 category_balances) |
| 39 | |
| 40 | inputs: number_of_regions |
| 41 | num_categorization_control_possibilities |
| 42 | number_of_available_bits |
| 43 | rms_index[MAX_NUMBER_OF_REGIONS] |
| 44 | |
| 45 | outputs: power_categories[MAX_NUMBER_OF_REGIONS] |
| 46 | category_balances[MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES-1] |
| 47 | |
| 48 | Description: Computes a series of categorizations |
| 49 | |
| 50 | WMOPS: 7kHz | 24kbit | 32kbit |
| 51 | -------|--------------|---------------- |
| 52 | AVG | 0.14 | 0.14 |
| 53 | -------|--------------|---------------- |
| 54 | MAX | 0.15 | 0.15 |
| 55 | -------|--------------|---------------- |
| 56 | |
| 57 | 14kHz | 24kbit | 32kbit | 48kbit |
| 58 | -------|--------------|----------------|---------------- |
| 59 | AVG | 0.42 | 0.45 | 0.48 |
| 60 | -------|--------------|----------------|---------------- |
| 61 | MAX | 0.47 | 0.52 | 0.52 |
| 62 | -------|--------------|----------------|---------------- |
| 63 | |
| 64 | ****************************************************************************************/ |
| 65 | void categorize(Word16 number_of_available_bits, |
| 66 | Word16 number_of_regions, |
| 67 | Word16 num_categorization_control_possibilities, |
| 68 | Word16 *rms_index, |
| 69 | Word16 *power_categories, |
| 70 | Word16 *category_balances) |
| 71 | { |
| 72 | |
| 73 | Word16 offset; |
| 74 | Word16 temp; |
| 75 | Word16 frame_size; |
| 76 | |
| 77 | /* At higher bit rates, there is an increase for most categories in average bit |
| 78 | consumption per region. We compensate for this by pretending we have fewer |
| 79 | available bits. */ |
| 80 | test(); |
| 81 | if (number_of_regions == NUMBER_OF_REGIONS) |
| 82 | { |
| 83 | frame_size = DCT_LENGTH; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | frame_size = MAX_DCT_LENGTH; |
| 88 | } |
| 89 | |
| 90 | temp = sub(number_of_available_bits,frame_size); |
| 91 | |
| 92 | test(); |
| 93 | if (temp > 0) |
| 94 | { |
| 95 | number_of_available_bits = sub(number_of_available_bits,frame_size); |
| 96 | number_of_available_bits = extract_l(L_mult0(number_of_available_bits,5)); |
| 97 | number_of_available_bits = shr_nocheck(number_of_available_bits,3); |
| 98 | number_of_available_bits = add(number_of_available_bits,frame_size); |
| 99 | } |
| 100 | |
| 101 | /* calculate the offset using the original category assignments */ |
| 102 | offset = calc_offset(rms_index,number_of_regions,number_of_available_bits); |
| 103 | |
| 104 | |
| 105 | |
| 106 | /* compute the power categories based on the uniform offset */ |
| 107 | compute_raw_pow_categories(power_categories,rms_index,number_of_regions,offset); |
| 108 | |
| 109 | |
| 110 | /* adjust the category assignments */ |
| 111 | /* compute the new power categories and category balances */ |
| 112 | comp_powercat_and_catbalance(power_categories,category_balances,rms_index,number_of_available_bits,number_of_regions,num_categorization_control_possibilities,offset); |
| 113 | |
| 114 | } |
| 115 | |
| 116 | /*************************************************************************** |
| 117 | Function: comp_powercat_and_catbalance |
| 118 | |
| 119 | Syntax: void comp_powercat_and_catbalance(Word16 *power_categories, |
| 120 | Word16 *category_balances, |
| 121 | Word16 *rms_index, |
| 122 | Word16 number_of_available_bits, |
| 123 | Word16 number_of_regions, |
| 124 | Word16 num_categorization_control_possibilities, |
| 125 | Word16 offset) |
| 126 | |
| 127 | |
| 128 | inputs: *rms_index |
| 129 | number_of_available_bits |
| 130 | number_of_regions |
| 131 | num_categorization_control_possibilities |
| 132 | offset |
| 133 | |
| 134 | outputs: *power_categories |
| 135 | *category_balances |
| 136 | |
| 137 | |
| 138 | Description: Computes the power_categories and the category balances |
| 139 | |
| 140 | WMOPS: 7kHz | 24kbit | 32kbit |
| 141 | -------|--------------|---------------- |
| 142 | AVG | 0.10 | 0.10 |
| 143 | -------|--------------|---------------- |
| 144 | MAX | 0.11 | 0.11 |
| 145 | -------|--------------|---------------- |
| 146 | |
| 147 | 14kHz | 24kbit | 32kbit | 48kbit |
| 148 | -------|--------------|----------------|---------------- |
| 149 | AVG | 0.32 | 0.35 | 0.38 |
| 150 | -------|--------------|----------------|---------------- |
| 151 | MAX | 0.38 | 0.42 | 0.43 |
| 152 | -------|--------------|----------------|---------------- |
| 153 | |
| 154 | ***************************************************************************/ |
| 155 | void comp_powercat_and_catbalance(Word16 *power_categories, |
| 156 | Word16 *category_balances, |
| 157 | Word16 *rms_index, |
| 158 | Word16 number_of_available_bits, |
| 159 | Word16 number_of_regions, |
| 160 | Word16 num_categorization_control_possibilities, |
| 161 | Word16 offset) |
| 162 | { |
| 163 | |
| 164 | Word16 expected_number_of_code_bits; |
| 165 | Word16 region; |
| 166 | Word16 max_region; |
| 167 | Word16 j; |
| 168 | Word16 max_rate_categories[MAX_NUMBER_OF_REGIONS]; |
| 169 | Word16 min_rate_categories[MAX_NUMBER_OF_REGIONS]; |
| 170 | Word16 temp_category_balances[2*MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES]; |
| 171 | Word16 raw_max, raw_min; |
| 172 | Word16 raw_max_index=0, raw_min_index=0; |
| 173 | Word16 max_rate_pointer, min_rate_pointer; |
| 174 | Word16 max, min; |
| 175 | Word16 itemp0; |
| 176 | Word16 itemp1; |
| 177 | Word16 min_plus_max; |
| 178 | Word16 two_x_number_of_available_bits; |
| 179 | |
| 180 | Word16 temp; |
| 181 | |
| 182 | expected_number_of_code_bits = 0; |
| 183 | move16(); |
| 184 | |
| 185 | for (region=0; region<number_of_regions; region++) |
| 186 | expected_number_of_code_bits = add(expected_number_of_code_bits,expected_bits_table[power_categories[region]]); |
| 187 | |
| 188 | |
| 189 | for (region=0; region<number_of_regions; region++) |
| 190 | { |
| 191 | max_rate_categories[region] = power_categories[region]; |
| 192 | move16(); |
| 193 | |
| 194 | min_rate_categories[region] = power_categories[region]; |
| 195 | move16(); |
| 196 | } |
| 197 | |
| 198 | max = expected_number_of_code_bits; |
| 199 | move16(); |
| 200 | min = expected_number_of_code_bits; |
| 201 | move16(); |
| 202 | max_rate_pointer = num_categorization_control_possibilities; |
| 203 | move16(); |
| 204 | min_rate_pointer = num_categorization_control_possibilities; |
| 205 | move16(); |
| 206 | |
| 207 | for (j=0; j<num_categorization_control_possibilities-1; j++) |
| 208 | { |
| 209 | min_plus_max = add(max,min); |
| 210 | two_x_number_of_available_bits = shl_nocheck(number_of_available_bits,1); |
| 211 | |
| 212 | temp = sub(min_plus_max,two_x_number_of_available_bits); |
| 213 | test(); |
| 214 | if (temp <= 0) |
| 215 | { |
| 216 | raw_min = 99; |
| 217 | move16(); |
| 218 | /* Search from lowest freq regions to highest for best */ |
| 219 | /* region to reassign to a higher bit rate category. */ |
| 220 | for (region=0; region<number_of_regions; region++) |
| 221 | { |
| 222 | test(); |
| 223 | if (max_rate_categories[region] > 0) |
| 224 | { |
| 225 | itemp0 = shl_nocheck(max_rate_categories[region],1); |
| 226 | itemp1 = sub(offset,rms_index[region]); |
| 227 | itemp0 = sub(itemp1,itemp0); |
| 228 | |
| 229 | temp = sub(itemp0,raw_min); |
| 230 | test(); |
| 231 | if (temp < 0) |
| 232 | { |
| 233 | raw_min = itemp0; |
| 234 | raw_min_index = region; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | max_rate_pointer = sub(max_rate_pointer,1); |
| 239 | temp_category_balances[max_rate_pointer] = raw_min_index; |
| 240 | move16(); |
| 241 | |
| 242 | max = sub(max,expected_bits_table[max_rate_categories[raw_min_index]]); |
| 243 | max_rate_categories[raw_min_index] = sub(max_rate_categories[raw_min_index],1); |
| 244 | move16(); |
| 245 | |
| 246 | max = add(max,expected_bits_table[max_rate_categories[raw_min_index]]); |
| 247 | } |
| 248 | else |
| 249 | { |
| 250 | raw_max = -99; |
| 251 | move16(); |
| 252 | /* Search from highest freq regions to lowest for best region to reassign to |
| 253 | a lower bit rate category. */ |
| 254 | max_region = sub(number_of_regions,1); |
| 255 | for (region= max_region; region >= 0; region--) |
| 256 | { |
| 257 | temp = sub(min_rate_categories[region],(NUM_CATEGORIES-1)); |
| 258 | test(); |
| 259 | if (temp < 0) |
| 260 | { |
| 261 | itemp0 = shl_nocheck(min_rate_categories[region],1); |
| 262 | itemp1 = sub(offset,rms_index[region]); |
| 263 | itemp0 = sub(itemp1,itemp0); |
| 264 | |
| 265 | temp = sub(itemp0,raw_max); |
| 266 | test(); |
| 267 | if (temp > 0) |
| 268 | { |
| 269 | raw_max = itemp0; |
| 270 | move16(); |
| 271 | raw_max_index = region; |
| 272 | move16(); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | temp_category_balances[min_rate_pointer] = raw_max_index; |
| 277 | move16(); |
| 278 | |
| 279 | min_rate_pointer = add(min_rate_pointer,1); |
| 280 | min = sub(min,expected_bits_table[min_rate_categories[raw_max_index]]); |
| 281 | |
| 282 | min_rate_categories[raw_max_index] = add(min_rate_categories[raw_max_index],1); |
| 283 | move16(); |
| 284 | |
| 285 | min = add(min,expected_bits_table[min_rate_categories[raw_max_index]]); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | for (region=0; region<number_of_regions; region++) |
| 290 | { |
| 291 | power_categories[region] = max_rate_categories[region]; |
| 292 | move16(); |
| 293 | } |
| 294 | |
| 295 | for (j=0; j<num_categorization_control_possibilities-1; j++) |
| 296 | { |
| 297 | category_balances[j] = temp_category_balances[max_rate_pointer++]; |
| 298 | move16(); |
| 299 | } |
| 300 | |
| 301 | } |
| 302 | /*************************************************************************** |
| 303 | Function: calc_offset |
| 304 | |
| 305 | Syntax: offset=calc_offset(Word16 *rms_index,Word16 number_of_regions,Word16 available_bits) |
| 306 | |
| 307 | input: Word16 *rms_index |
| 308 | Word16 number_of_regions |
| 309 | Word16 available_bits |
| 310 | |
| 311 | output: Word16 offset |
| 312 | |
| 313 | Description: Calculates the the category offset. This is the shift required |
| 314 | To get the most out of the number of available bits. A binary |
| 315 | type search is used to find the offset. |
| 316 | |
| 317 | WMOPS: 7kHz | 24kbit | 32kbit |
| 318 | -------|--------------|---------------- |
| 319 | AVG | 0.04 | 0.04 |
| 320 | -------|--------------|---------------- |
| 321 | MAX | 0.04 | 0.04 |
| 322 | -------|--------------|---------------- |
| 323 | |
| 324 | 14kHz | 24kbit | 32kbit | 48kbit |
| 325 | -------|--------------|----------------|---------------- |
| 326 | AVG | 0.08 | 0.08 | 0.08 |
| 327 | -------|--------------|----------------|---------------- |
| 328 | MAX | 0.09 | 0.09 | 0.09 |
| 329 | -------|--------------|----------------|---------------- |
| 330 | |
| 331 | ***************************************************************************/ |
| 332 | Word16 calc_offset(Word16 *rms_index,Word16 number_of_regions,Word16 available_bits) |
| 333 | { |
| 334 | |
| 335 | Word16 answer; |
| 336 | Word16 delta; |
| 337 | Word16 test_offset; |
| 338 | Word16 region,j; |
| 339 | Word16 power_cats[MAX_NUMBER_OF_REGIONS]; |
| 340 | Word16 bits; |
| 341 | Word16 offset; |
| 342 | Word16 temp; |
| 343 | |
| 344 | /* initialize vars */ |
| 345 | answer = -32; |
| 346 | move16(); |
| 347 | delta = 32; |
| 348 | move16(); |
| 349 | |
| 350 | do |
| 351 | { |
| 352 | test_offset = add(answer,delta); |
| 353 | |
| 354 | /* obtain a category for each region */ |
| 355 | /* using the test offset */ |
| 356 | for (region=0; region<number_of_regions; region++) |
| 357 | { |
| 358 | j = sub(test_offset,rms_index[region]); |
| 359 | j = shr_nocheck(j,1); |
| 360 | |
| 361 | /* Ensure j is between 0 and NUM_CAT-1 */ |
| 362 | test(); |
| 363 | if (j < 0) |
| 364 | { |
| 365 | j = 0; |
| 366 | move16(); |
| 367 | } |
| 368 | temp = sub(j,NUM_CATEGORIES-1); |
| 369 | test(); |
| 370 | if (temp > 0) |
| 371 | { |
| 372 | j = sub(NUM_CATEGORIES,1); |
| 373 | move16(); |
| 374 | } |
| 375 | power_cats[region] = j; |
| 376 | move16(); |
| 377 | } |
| 378 | bits = 0; |
| 379 | move16(); |
| 380 | |
| 381 | /* compute the number of bits that will be used given the cat assignments */ |
| 382 | for (region=0; region<number_of_regions; region++) |
| 383 | bits = add(bits,expected_bits_table[power_cats[region]]); |
| 384 | |
| 385 | /* if (bits > available_bits - 32) then divide the offset region for the bin search */ |
| 386 | offset = sub(available_bits,32); |
| 387 | temp = sub(bits,offset); |
| 388 | test(); |
| 389 | if (temp >= 0) |
| 390 | { |
| 391 | answer = test_offset; |
| 392 | move16(); |
| 393 | } |
| 394 | delta = shr_nocheck(delta,1); |
| 395 | test(); /* for the while loop */ |
| 396 | } while (delta > 0); |
| 397 | |
| 398 | return(answer); |
| 399 | } |
| 400 | /*************************************************************************** |
| 401 | Function: compute_raw_pow_categories |
| 402 | |
| 403 | Syntax: void compute_raw_pow_categories(Word16 *power_categories, |
| 404 | Word16 *rms_index, |
| 405 | Word16 number_of_regions, |
| 406 | Word16 offset) |
| 407 | inputs: *rms_index |
| 408 | number_of_regions |
| 409 | offset |
| 410 | |
| 411 | outputs: *power_categories |
| 412 | |
| 413 | |
| 414 | |
| 415 | Description: This function computes the power categories given the offset |
| 416 | This is kind of redundant since they were already computed |
| 417 | in calc_offset to determine the offset. |
| 418 | |
| 419 | WMOPS: | 24kbit | 32kbit |
| 420 | -------|--------------|---------------- |
| 421 | AVG | 0.01 | 0.01 |
| 422 | -------|--------------|---------------- |
| 423 | MAX | 0.01 | 0.01 |
| 424 | -------|--------------|---------------- |
| 425 | |
| 426 | 14kHz | 24kbit | 32kbit | 48kbit |
| 427 | -------|--------------|----------------|---------------- |
| 428 | AVG | 0.01 | 0.01 | 0.01 |
| 429 | -------|--------------|----------------|---------------- |
| 430 | MAX | 0.01 | 0.01 | 0.01 |
| 431 | -------|--------------|----------------|---------------- |
| 432 | |
| 433 | ***************************************************************************/ |
| 434 | void compute_raw_pow_categories(Word16 *power_categories,Word16 *rms_index,Word16 number_of_regions,Word16 offset) |
| 435 | { |
| 436 | Word16 region; |
| 437 | Word16 j; |
| 438 | Word16 temp; |
| 439 | |
| 440 | for (region=0; region<number_of_regions; region++) |
| 441 | { |
| 442 | j = sub(offset,rms_index[region]); |
| 443 | j = shr_nocheck(j,1); |
| 444 | |
| 445 | /* make sure j is between 0 and NUM_CAT-1 */ |
| 446 | test(); |
| 447 | if (j < 0) |
| 448 | { |
| 449 | j = 0; |
| 450 | move16(); |
| 451 | } |
| 452 | temp = sub(j,(NUM_CATEGORIES-1)); |
| 453 | test(); |
| 454 | if (temp > 0) |
| 455 | j = sub(NUM_CATEGORIES,1); |
| 456 | |
| 457 | power_categories[region] = j; |
| 458 | move16(); |
| 459 | } |
| 460 | } |
| 461 | |