Alexandre Lision | 8af73cb | 2013-12-10 14:11:20 -0500 | [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: encoder.c |
| 15 | |
| 16 | Purpose: Contains files used to implement the G.722.1 Annex C encoder |
| 17 | |
| 18 | Design Notes: |
| 19 | |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | /*************************************************************************** |
| 23 | Include files |
| 24 | ***************************************************************************/ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <math.h> |
| 28 | #include "defs.h" |
| 29 | #include "huff_def.h" |
| 30 | #include "tables.h" |
| 31 | #include "count.h" |
| 32 | |
| 33 | /*************************************************************************** |
| 34 | Function: encoder |
| 35 | |
| 36 | Syntax: void encoder(Word16 number_of_available_bits, |
| 37 | Word16 number_of_regions, |
| 38 | Word16 mlt_coefs, |
| 39 | Word16 mag_shift, |
| 40 | Word16 out_words) |
| 41 | |
| 42 | inputs: number_of_available_bits |
| 43 | number_of_regions |
| 44 | mag_shift |
| 45 | mlt_coefs[DCT_LENGTH] |
| 46 | |
| 47 | outputs: out_words[MAX_BITS_PER_FRAME/16] |
| 48 | |
| 49 | |
| 50 | Description: Encodes the mlt coefs into out_words using G.722.1 Annex C |
| 51 | |
| 52 | |
| 53 | WMOPS: 7kHz | 24kbit | 32kbit |
| 54 | -------|--------------|---------------- |
| 55 | AVG | 0.93 | 1.04 |
| 56 | -------|--------------|---------------- |
| 57 | MAX | 1.20 | 1.28 |
| 58 | -------|--------------|---------------- |
| 59 | |
| 60 | 14kHz | 24kbit | 32kbit | 48kbit |
| 61 | -------|--------------|----------------|---------------- |
| 62 | AVG | 1.39 | 1.71 | 2.01 |
| 63 | -------|--------------|----------------|---------------- |
| 64 | MAX | 2.00 | 2.30 | 2.52 |
| 65 | -------|--------------|----------------|---------------- |
| 66 | |
| 67 | ***************************************************************************/ |
| 68 | |
| 69 | void encoder(Word16 number_of_available_bits, |
| 70 | Word16 number_of_regions, |
| 71 | Word16 *mlt_coefs, |
| 72 | Word16 mag_shift, |
| 73 | Word16 *out_words) |
| 74 | { |
| 75 | |
| 76 | Word16 num_categorization_control_bits; |
| 77 | Word16 num_categorization_control_possibilities; |
| 78 | Word16 number_of_bits_per_frame; |
| 79 | Word16 number_of_envelope_bits; |
| 80 | Word16 categorization_control; |
| 81 | Word16 region; |
| 82 | Word16 absolute_region_power_index[MAX_NUMBER_OF_REGIONS]; |
| 83 | Word16 power_categories[MAX_NUMBER_OF_REGIONS]; |
| 84 | Word16 category_balances[MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES-1]; |
| 85 | Word16 drp_num_bits[MAX_NUMBER_OF_REGIONS+1]; |
| 86 | UWord16 drp_code_bits[MAX_NUMBER_OF_REGIONS+1]; |
| 87 | Word16 region_mlt_bit_counts[MAX_NUMBER_OF_REGIONS]; |
| 88 | UWord32 region_mlt_bits[4*MAX_NUMBER_OF_REGIONS]; |
| 89 | Word16 mag_shift_offset; |
| 90 | |
| 91 | Word16 temp; |
| 92 | |
| 93 | /* initialize variables */ |
| 94 | test(); |
| 95 | if (number_of_regions == NUMBER_OF_REGIONS) |
| 96 | { |
| 97 | num_categorization_control_bits = NUM_CATEGORIZATION_CONTROL_BITS; |
| 98 | move16(); |
| 99 | num_categorization_control_possibilities = NUM_CATEGORIZATION_CONTROL_POSSIBILITIES; |
| 100 | move16(); |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | num_categorization_control_bits = MAX_NUM_CATEGORIZATION_CONTROL_BITS; |
| 105 | move16(); |
| 106 | num_categorization_control_possibilities = MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES; |
| 107 | move16(); |
| 108 | } |
| 109 | |
| 110 | number_of_bits_per_frame = number_of_available_bits; |
| 111 | move16(); |
| 112 | |
| 113 | for (region=0; region<number_of_regions; region++) |
| 114 | { |
| 115 | region_mlt_bit_counts[region] = 0; |
| 116 | move16(); |
| 117 | } |
| 118 | |
| 119 | /* Estimate power envelope. */ |
| 120 | number_of_envelope_bits = compute_region_powers(mlt_coefs, |
| 121 | mag_shift, |
| 122 | drp_num_bits, |
| 123 | drp_code_bits, |
| 124 | absolute_region_power_index, |
| 125 | number_of_regions); |
| 126 | |
| 127 | /* Adjust number of available bits based on power envelope estimate */ |
| 128 | temp = sub(number_of_available_bits,number_of_envelope_bits); |
| 129 | number_of_available_bits = sub(temp,num_categorization_control_bits); |
| 130 | |
| 131 | /* get categorizations */ |
| 132 | categorize(number_of_available_bits, |
| 133 | number_of_regions, |
| 134 | num_categorization_control_possibilities, |
| 135 | absolute_region_power_index, |
| 136 | power_categories, |
| 137 | category_balances); |
| 138 | |
| 139 | /* Adjust absolute_region_category_index[] for mag_shift. |
| 140 | This assumes that REGION_POWER_STEPSIZE_DB is defined |
| 141 | to be exactly 3.010299957 or 20.0 times log base 10 |
| 142 | of square root of 2. */ |
| 143 | temp = shl_nocheck(mag_shift,1); |
| 144 | mag_shift_offset = add(temp,REGION_POWER_TABLE_NUM_NEGATIVES); |
| 145 | |
| 146 | for (region=0; region<number_of_regions; region++) |
| 147 | { |
| 148 | absolute_region_power_index[region] = add(absolute_region_power_index[region],mag_shift_offset); |
| 149 | move16(); |
| 150 | } |
| 151 | |
| 152 | /* adjust the absolute power region index based on the mlt coefs */ |
| 153 | adjust_abs_region_power_index(absolute_region_power_index,mlt_coefs,number_of_regions); |
| 154 | |
| 155 | |
| 156 | /* quantize and code the mlt coefficients based on categorizations */ |
| 157 | vector_quantize_mlts(number_of_available_bits, |
| 158 | number_of_regions, |
| 159 | num_categorization_control_possibilities, |
| 160 | mlt_coefs, |
| 161 | absolute_region_power_index, |
| 162 | power_categories, |
| 163 | category_balances, |
| 164 | &categorization_control, |
| 165 | region_mlt_bit_counts, |
| 166 | region_mlt_bits); |
| 167 | |
| 168 | /* stuff bits into words */ |
| 169 | bits_to_words(region_mlt_bits, |
| 170 | region_mlt_bit_counts, |
| 171 | drp_num_bits, |
| 172 | drp_code_bits, |
| 173 | out_words, |
| 174 | categorization_control, |
| 175 | number_of_regions, |
| 176 | num_categorization_control_bits, |
| 177 | number_of_bits_per_frame); |
| 178 | |
| 179 | } |
| 180 | |
| 181 | /*************************************************************************** |
| 182 | Function: bits_to_words |
| 183 | |
| 184 | Syntax: bits_to_words(UWord32 *region_mlt_bits, |
| 185 | Word16 *region_mlt_bit_counts, |
| 186 | Word16 *drp_num_bits, |
| 187 | UWord16 *drp_code_bits, |
| 188 | Word16 *out_words, |
| 189 | Word16 categorization_control, |
| 190 | Word16 number_of_regions, |
| 191 | Word16 num_categorization_control_bits, |
| 192 | Word16 number_of_bits_per_frame) |
| 193 | |
| 194 | |
| 195 | Description: Stuffs the bits into words for output |
| 196 | |
| 197 | WMOPS: 7kHz | 24kbit | 32kbit |
| 198 | -------|--------------|---------------- |
| 199 | AVG | 0.09 | 0.12 |
| 200 | -------|--------------|---------------- |
| 201 | MAX | 0.10 | 0.13 |
| 202 | -------|--------------|---------------- |
| 203 | |
| 204 | 14kHz | 24kbit | 32kbit | 48kbit |
| 205 | -------|--------------|----------------|---------------- |
| 206 | AVG | 0.12 | 0.15 | 0.19 |
| 207 | -------|--------------|----------------|---------------- |
| 208 | MAX | 0.14 | 0.17 | 0.21 |
| 209 | -------|--------------|----------------|---------------- |
| 210 | |
| 211 | ***************************************************************************/ |
| 212 | void bits_to_words(UWord32 *region_mlt_bits, |
| 213 | Word16 *region_mlt_bit_counts, |
| 214 | Word16 *drp_num_bits, |
| 215 | UWord16 *drp_code_bits, |
| 216 | Word16 *out_words, |
| 217 | Word16 categorization_control, |
| 218 | Word16 number_of_regions, |
| 219 | Word16 num_categorization_control_bits, |
| 220 | Word16 number_of_bits_per_frame) |
| 221 | { |
| 222 | Word16 out_word_index = 0; |
| 223 | Word16 j; |
| 224 | Word16 region; |
| 225 | Word16 out_word; |
| 226 | Word16 region_bit_count; |
| 227 | Word16 current_word_bits_left; |
| 228 | UWord16 slice; |
| 229 | Word16 out_word_bits_free = 16; |
| 230 | UWord32 *in_word_ptr; |
| 231 | UWord32 current_word; |
| 232 | |
| 233 | Word32 acca = 0; |
| 234 | Word32 accb; |
| 235 | Word16 temp; |
| 236 | |
| 237 | /* First set up the categorization control bits to look like one more set of region power bits. */ |
| 238 | out_word = 0; |
| 239 | move16(); |
| 240 | |
| 241 | drp_num_bits[number_of_regions] = num_categorization_control_bits; |
| 242 | move16(); |
| 243 | |
| 244 | drp_code_bits[number_of_regions] = (UWord16)categorization_control; |
| 245 | move16(); |
| 246 | |
| 247 | /* These code bits are right justified. */ |
| 248 | for (region=0; region <= number_of_regions; region++) |
| 249 | { |
| 250 | current_word_bits_left = drp_num_bits[region]; |
| 251 | move16(); |
| 252 | |
| 253 | current_word = (UWord32)drp_code_bits[region]; |
| 254 | move16(); |
| 255 | |
| 256 | j = sub(current_word_bits_left,out_word_bits_free); |
| 257 | |
| 258 | test(); |
| 259 | if (j >= 0) |
| 260 | { |
| 261 | temp = extract_l(L_shr_nocheck(current_word,j)); |
| 262 | out_word = add(out_word,temp); |
| 263 | |
| 264 | out_words[out_word_index++] = out_word; |
| 265 | move16(); |
| 266 | |
| 267 | out_word_bits_free = 16; |
| 268 | move16(); |
| 269 | |
| 270 | out_word_bits_free = sub(out_word_bits_free,j); |
| 271 | |
| 272 | acca = (current_word << out_word_bits_free); |
| 273 | out_word = extract_l(acca); |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | j = negate(j); |
| 278 | |
| 279 | acca = (current_word << j); |
| 280 | accb = L_deposit_l(out_word); |
| 281 | acca = L_add(accb,acca); |
| 282 | out_word = extract_l(acca); |
| 283 | |
| 284 | out_word_bits_free = sub(out_word_bits_free,current_word_bits_left); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /* These code bits are left justified. */ |
| 289 | |
| 290 | for (region=0;region<number_of_regions; region++) |
| 291 | { |
| 292 | accb = L_deposit_l(out_word_index); |
| 293 | accb = L_shl_nocheck(accb,4); |
| 294 | accb = L_sub(accb,number_of_bits_per_frame); |
| 295 | test(); |
| 296 | if(accb < 0) |
| 297 | { |
| 298 | temp = shl_nocheck(region,2); |
| 299 | in_word_ptr = ®ion_mlt_bits[temp]; |
| 300 | region_bit_count = region_mlt_bit_counts[region]; |
| 301 | move16(); |
| 302 | |
| 303 | temp = sub(32,region_bit_count); |
| 304 | test(); |
| 305 | if(temp > 0) |
| 306 | current_word_bits_left = region_bit_count; |
| 307 | else |
| 308 | current_word_bits_left = 32; |
| 309 | |
| 310 | current_word = *in_word_ptr++; |
| 311 | |
| 312 | acca = L_deposit_l(out_word_index); |
| 313 | acca = L_shl_nocheck(acca,4); |
| 314 | acca = L_sub(acca,number_of_bits_per_frame); |
| 315 | |
| 316 | /* from while loop */ |
| 317 | test(); |
| 318 | test(); |
| 319 | logic16(); |
| 320 | while ((region_bit_count > 0) && (acca < 0)) |
| 321 | { |
| 322 | /* from while loop */ |
| 323 | test(); |
| 324 | test(); |
| 325 | logic16(); |
| 326 | |
| 327 | temp = sub(current_word_bits_left,out_word_bits_free); |
| 328 | test(); |
| 329 | if (temp >= 0) |
| 330 | { |
| 331 | temp = sub(32,out_word_bits_free); |
| 332 | accb = LU_shr(current_word,temp); |
| 333 | slice = (UWord16)extract_l(accb); |
| 334 | |
| 335 | out_word = add(out_word,slice); |
| 336 | |
| 337 | test(); |
| 338 | current_word <<= out_word_bits_free; |
| 339 | |
| 340 | current_word_bits_left = sub(current_word_bits_left,out_word_bits_free); |
| 341 | out_words[out_word_index++] = extract_l(out_word); |
| 342 | move16(); |
| 343 | |
| 344 | out_word = 0; |
| 345 | move16(); |
| 346 | |
| 347 | out_word_bits_free = 16; |
| 348 | move16(); |
| 349 | } |
| 350 | else |
| 351 | { |
| 352 | temp = sub(32,current_word_bits_left); |
| 353 | accb = LU_shr(current_word,temp); |
| 354 | slice = (UWord16)extract_l(accb); |
| 355 | |
| 356 | temp = sub(out_word_bits_free,current_word_bits_left); |
| 357 | test(); |
| 358 | accb = slice << temp; |
| 359 | acca = L_deposit_l(out_word); |
| 360 | acca = L_add(acca,accb); |
| 361 | out_word = extract_l(acca); |
| 362 | out_word_bits_free = sub(out_word_bits_free,current_word_bits_left); |
| 363 | |
| 364 | current_word_bits_left = 0; |
| 365 | move16(); |
| 366 | } |
| 367 | |
| 368 | test(); |
| 369 | if (current_word_bits_left == 0) |
| 370 | { |
| 371 | current_word = *in_word_ptr++; |
| 372 | region_bit_count = sub(region_bit_count,32); |
| 373 | |
| 374 | /* current_word_bits_left = MIN(32,region_bit_count); */ |
| 375 | temp = sub(32,region_bit_count); |
| 376 | test(); |
| 377 | if(temp > 0) |
| 378 | current_word_bits_left = region_bit_count; |
| 379 | else |
| 380 | current_word_bits_left = 32; |
| 381 | |
| 382 | } |
| 383 | acca = L_deposit_l(out_word_index); |
| 384 | acca = L_shl_nocheck(acca,4); |
| 385 | acca = L_sub(acca,number_of_bits_per_frame); |
| 386 | } |
| 387 | accb = L_deposit_l(out_word_index); |
| 388 | accb = L_shl_nocheck(accb,4); |
| 389 | accb = L_sub(accb,number_of_bits_per_frame); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /* Fill out with 1's. */ |
| 394 | |
| 395 | test(); |
| 396 | while (acca < 0) |
| 397 | { |
| 398 | test(); |
| 399 | current_word = 0x0000ffff; |
| 400 | move32(); |
| 401 | |
| 402 | temp = sub(16,out_word_bits_free); |
| 403 | acca = LU_shr(current_word,temp); |
| 404 | slice = (UWord16)extract_l(acca); |
| 405 | |
| 406 | out_word = add(out_word,slice); |
| 407 | out_words[out_word_index++] = out_word; |
| 408 | move16(); |
| 409 | |
| 410 | out_word = 0; |
| 411 | move16(); |
| 412 | |
| 413 | out_word_bits_free = 16; |
| 414 | move16(); |
| 415 | |
| 416 | acca = L_deposit_l(out_word_index); |
| 417 | acca = L_shl_nocheck(acca,4); |
| 418 | acca = L_sub(acca,number_of_bits_per_frame); |
| 419 | } |
| 420 | } |
| 421 | /*************************************************************************** |
| 422 | Function: adjust_abs_region_power_index |
| 423 | |
| 424 | Syntax: adjust_abs_region_power_index(Word16 *absolute_region_power_index, |
| 425 | Word16 *mlt_coefs, |
| 426 | Word16 number_of_regions) |
| 427 | |
| 428 | inputs: *mlt_coefs |
| 429 | *absolute_region_power_index |
| 430 | number_of_regions |
| 431 | |
| 432 | outputs: *absolute_region_power_index |
| 433 | |
| 434 | Description: Adjusts the absolute power index |
| 435 | |
| 436 | |
| 437 | WMOPS: 7kHz | 24kbit | 32kbit |
| 438 | -------|--------------|---------------- |
| 439 | AVG | 0.03 | 0.03 |
| 440 | -------|--------------|---------------- |
| 441 | MAX | 0.12 | 0.12 |
| 442 | -------|--------------|---------------- |
| 443 | |
| 444 | 14kHz | 24kbit | 32kbit | 48kbit |
| 445 | -------|--------------|----------------|---------------- |
| 446 | AVG | 0.03 | 0.03 | 0.03 |
| 447 | -------|--------------|----------------|---------------- |
| 448 | MAX | 0.14 | 0.14 | 0.14 |
| 449 | -------|--------------|----------------|---------------- |
| 450 | |
| 451 | ***************************************************************************/ |
| 452 | void adjust_abs_region_power_index(Word16 *absolute_region_power_index,Word16 *mlt_coefs,Word16 number_of_regions) |
| 453 | { |
| 454 | Word16 n,i; |
| 455 | Word16 region; |
| 456 | Word16 *raw_mlt_ptr; |
| 457 | |
| 458 | Word32 acca; |
| 459 | Word16 temp; |
| 460 | |
| 461 | for (region=0; region<number_of_regions; region++) |
| 462 | { |
| 463 | n = sub(absolute_region_power_index[region],39); |
| 464 | n = shr_nocheck(n,1); |
| 465 | |
| 466 | test(); |
| 467 | if (n > 0) |
| 468 | { |
| 469 | temp = extract_l(L_mult0(region,REGION_SIZE)); |
| 470 | |
| 471 | raw_mlt_ptr = &mlt_coefs[temp]; |
| 472 | |
| 473 | for (i=0; i<REGION_SIZE; i++) |
| 474 | { |
| 475 | acca = L_shl_nocheck(*raw_mlt_ptr,16); |
| 476 | acca = L_add(acca,32768L); |
| 477 | acca = L_shr_nocheck(acca,n); |
| 478 | acca = L_shr_nocheck(acca,16); |
| 479 | *raw_mlt_ptr++ = extract_l(acca); |
| 480 | } |
| 481 | |
| 482 | temp = shl_nocheck(n,1); |
| 483 | temp = sub(absolute_region_power_index[region],temp); |
| 484 | absolute_region_power_index[region] = temp; |
| 485 | move16(); |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | /*************************************************************************** |
| 491 | Function: compute_region_powers |
| 492 | |
| 493 | Syntax: Word16 compute_region_powers(Word16 *mlt_coefs, |
| 494 | Word16 mag_shift, |
| 495 | Word16 *drp_num_bits, |
| 496 | UWord16 *drp_code_bits, |
| 497 | Word16 *absolute_region_power_index, |
| 498 | Word16 number_of_regions) |
| 499 | mlt_coefs[DCT_LENGTH]; |
| 500 | mag_shift; |
| 501 | drp_num_bits[MAX_NUMBER_OF_REGIONS]; |
| 502 | drp_code_bits[MAX_NUMBER_OF_REGIONS]; |
| 503 | absolute_region_power_index[MAX_NUMBER_OF_REGIONS]; |
| 504 | number_of_regions; |
| 505 | |
| 506 | Description: Computes the power for each of the regions |
| 507 | |
| 508 | |
| 509 | WMOPS: 7kHz | 24kbit | 32kbit |
| 510 | -------|--------------|---------------- |
| 511 | AVG | 0.09 | 0.09 |
| 512 | -------|--------------|---------------- |
| 513 | MAX | 0.13 | 0.13 |
| 514 | -------|--------------|---------------- |
| 515 | |
| 516 | 14kHz | 24kbit | 32kbit | 48kbit |
| 517 | -------|--------------|----------------|---------------- |
| 518 | AVG | 0.20 | 0.20 | 0.20 |
| 519 | -------|--------------|----------------|---------------- |
| 520 | MAX | 0.29 | 0.29 | 0.29 |
| 521 | -------|--------------|----------------|---------------- |
| 522 | |
| 523 | ***************************************************************************/ |
| 524 | |
| 525 | Word16 compute_region_powers(Word16 *mlt_coefs, |
| 526 | Word16 mag_shift, |
| 527 | Word16 *drp_num_bits, |
| 528 | UWord16 *drp_code_bits, |
| 529 | Word16 *absolute_region_power_index, |
| 530 | Word16 number_of_regions) |
| 531 | { |
| 532 | |
| 533 | Word16 *input_ptr; |
| 534 | Word32 long_accumulator; |
| 535 | Word16 itemp1; |
| 536 | Word16 power_shift; |
| 537 | Word16 region; |
| 538 | Word16 j; |
| 539 | Word16 differential_region_power_index[MAX_NUMBER_OF_REGIONS]; |
| 540 | Word16 number_of_bits; |
| 541 | |
| 542 | Word32 acca; |
| 543 | Word16 temp; |
| 544 | Word16 temp1; |
| 545 | Word16 temp2; |
| 546 | |
| 547 | |
| 548 | input_ptr = mlt_coefs; |
| 549 | for (region=0; region<number_of_regions; region++) |
| 550 | { |
| 551 | long_accumulator = L_deposit_l(0); |
| 552 | |
| 553 | for (j=0; j<REGION_SIZE; j++) |
| 554 | { |
| 555 | itemp1 = *input_ptr++; |
| 556 | move16(); |
| 557 | long_accumulator = L_mac0(long_accumulator,itemp1,itemp1); |
| 558 | } |
| 559 | |
| 560 | power_shift = 0; |
| 561 | move16(); |
| 562 | |
| 563 | acca = (long_accumulator & 0x7fff0000L); |
| 564 | logic32(); |
| 565 | |
| 566 | test(); |
| 567 | while (acca > 0) |
| 568 | { |
| 569 | test(); |
| 570 | long_accumulator = L_shr_nocheck(long_accumulator,1); |
| 571 | |
| 572 | acca = (long_accumulator & 0x7fff0000L); |
| 573 | logic32(); |
| 574 | |
| 575 | power_shift = add(power_shift,1); |
| 576 | } |
| 577 | |
| 578 | acca = L_sub(long_accumulator,32767); |
| 579 | |
| 580 | temp = add(power_shift,15); |
| 581 | test(); |
| 582 | test(); |
| 583 | logic16(); |
| 584 | while ((acca <= 0) && (temp >= 0)) |
| 585 | { |
| 586 | test(); |
| 587 | test(); |
| 588 | logic16(); |
| 589 | |
| 590 | long_accumulator = L_shl_nocheck(long_accumulator,1); |
| 591 | acca = L_sub(long_accumulator,32767); |
| 592 | power_shift--; |
| 593 | temp = add(power_shift,15); |
| 594 | } |
| 595 | long_accumulator = L_shr_nocheck(long_accumulator,1); |
| 596 | /* 28963 corresponds to square root of 2 times REGION_SIZE(20). */ |
| 597 | acca = L_sub(long_accumulator,28963); |
| 598 | |
| 599 | test(); |
| 600 | if (acca >= 0) |
| 601 | power_shift = add(power_shift,1); |
| 602 | |
| 603 | acca = L_deposit_l(mag_shift); |
| 604 | acca = L_shl_nocheck(acca,1); |
| 605 | acca = L_sub(power_shift,acca); |
| 606 | acca = L_add(35,acca); |
| 607 | acca = L_sub(acca,REGION_POWER_TABLE_NUM_NEGATIVES); |
| 608 | absolute_region_power_index[region] = extract_l(acca); |
| 609 | } |
| 610 | |
| 611 | |
| 612 | /* Before we differentially encode the quantized region powers, adjust upward the |
| 613 | valleys to make sure all the peaks can be accurately represented. */ |
| 614 | temp = sub(number_of_regions,2); |
| 615 | |
| 616 | for (region = temp; region >= 0; region--) |
| 617 | { |
| 618 | temp1 = sub(absolute_region_power_index[region+1],DRP_DIFF_MAX); |
| 619 | temp2 = sub(absolute_region_power_index[region],temp1); |
| 620 | test(); |
| 621 | if (temp2 < 0) |
| 622 | { |
| 623 | absolute_region_power_index[region] = temp1; |
| 624 | move16(); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | /* The MLT is currently scaled too low by the factor |
| 629 | ENCODER_SCALE_FACTOR(=18318)/32768 * (1./sqrt(160). |
| 630 | This is the ninth power of 1 over the square root of 2. |
| 631 | So later we will add ESF_ADJUSTMENT_TO_RMS_INDEX (now 9) |
| 632 | to drp_code_bits[0]. */ |
| 633 | |
| 634 | /* drp_code_bits[0] can range from 1 to 31. 0 will be used only as an escape sequence. */ |
| 635 | temp1 = sub(1,ESF_ADJUSTMENT_TO_RMS_INDEX); |
| 636 | temp2 = sub(absolute_region_power_index[0],temp1); |
| 637 | test(); |
| 638 | if (temp2 < 0) |
| 639 | { |
| 640 | absolute_region_power_index[0] = temp1; |
| 641 | move16(); |
| 642 | } |
| 643 | |
| 644 | temp1 = sub(31,ESF_ADJUSTMENT_TO_RMS_INDEX); |
| 645 | |
| 646 | /* |
| 647 | * The next line was corrected in Release 1.2 |
| 648 | */ |
| 649 | |
| 650 | temp2 = sub(absolute_region_power_index[0], temp1); |
| 651 | test(); |
| 652 | if (temp2 > 0) |
| 653 | { |
| 654 | absolute_region_power_index[0] = temp1; |
| 655 | move16(); |
| 656 | } |
| 657 | |
| 658 | differential_region_power_index[0] = absolute_region_power_index[0]; |
| 659 | move16(); |
| 660 | |
| 661 | number_of_bits = 5; |
| 662 | move16(); |
| 663 | |
| 664 | drp_num_bits[0] = 5; |
| 665 | move16(); |
| 666 | |
| 667 | drp_code_bits[0] = (UWord16)add(absolute_region_power_index[0],ESF_ADJUSTMENT_TO_RMS_INDEX); |
| 668 | move16(); |
| 669 | |
| 670 | /* Lower limit the absolute region power indices to -8 and upper limit them to 31. Such extremes |
| 671 | may be mathematically impossible anyway.*/ |
| 672 | for (region=1; region<number_of_regions; region++) |
| 673 | { |
| 674 | temp1 = sub(-8,ESF_ADJUSTMENT_TO_RMS_INDEX); |
| 675 | temp2 = sub(absolute_region_power_index[region],temp1); |
| 676 | test(); |
| 677 | if (temp2 < 0) |
| 678 | { |
| 679 | absolute_region_power_index[region] = temp1; |
| 680 | move16(); |
| 681 | } |
| 682 | |
| 683 | temp1 = sub(31,ESF_ADJUSTMENT_TO_RMS_INDEX); |
| 684 | temp2 = sub(absolute_region_power_index[region],temp1); |
| 685 | test(); |
| 686 | if (temp2 > 0) |
| 687 | { |
| 688 | absolute_region_power_index[region] = temp1; |
| 689 | move16(); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | for (region=1; region<number_of_regions; region++) |
| 694 | { |
| 695 | j = sub(absolute_region_power_index[region],absolute_region_power_index[region-1]); |
| 696 | temp = sub(j,DRP_DIFF_MIN); |
| 697 | test(); |
| 698 | if (temp < 0) |
| 699 | { |
| 700 | j = DRP_DIFF_MIN; |
| 701 | } |
| 702 | j = sub(j,DRP_DIFF_MIN); |
| 703 | move16(); |
| 704 | differential_region_power_index[region] = j; |
| 705 | move16(); |
| 706 | |
| 707 | temp = add(absolute_region_power_index[region-1],differential_region_power_index[region]); |
| 708 | temp = add(temp,DRP_DIFF_MIN); |
| 709 | absolute_region_power_index[region] = temp; |
| 710 | move16(); |
| 711 | |
| 712 | number_of_bits = add(number_of_bits,differential_region_power_bits[region][j]); |
| 713 | drp_num_bits[region] = differential_region_power_bits[region][j]; |
| 714 | move16(); |
| 715 | drp_code_bits[region] = differential_region_power_codes[region][j]; |
| 716 | move16(); |
| 717 | } |
| 718 | |
| 719 | return (number_of_bits); |
| 720 | } |
| 721 | |
| 722 | /*************************************************************************** |
| 723 | Function: vector_quantize_mlts |
| 724 | |
| 725 | Syntax: void vector_quantize_mlts(number_of_available_bits, |
| 726 | number_of_regions, |
| 727 | num_categorization_control_possibilities, |
| 728 | mlt_coefs, |
| 729 | absolute_region_power_index, |
| 730 | power_categories, |
| 731 | category_balances, |
| 732 | p_categorization_control, |
| 733 | region_mlt_bit_counts, |
| 734 | region_mlt_bits) |
| 735 | |
| 736 | Word16 number_of_available_bits; |
| 737 | Word16 number_of_regions; |
| 738 | Word16 num_categorization_control_possibilities; |
| 739 | Word16 mlt_coefs[DCT_LENGTH]; |
| 740 | Word16 absolute_region_power_index[MAX_NUMBER_OF_REGIONS]; |
| 741 | Word16 power_categories[MAX_NUMBER_OF_REGIONS]; |
| 742 | Word16 category_balances[MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES-1]; |
| 743 | Word16 *p_categorization_control; |
| 744 | Word16 region_mlt_bit_counts[MAX_NUMBER_OF_REGIONS]; |
| 745 | Word32 region_mlt_bits[4*MAX_NUMBER_OF_REGIONS]; |
| 746 | |
| 747 | Description: Scalar quantized vector Huffman coding (SQVH) |
| 748 | |
| 749 | |
| 750 | WMOPS: 7kHz | 24kbit | 32kbit |
| 751 | -------|--------------|---------------- |
| 752 | AVG | 0.57 | 0.65 |
| 753 | -------|--------------|---------------- |
| 754 | MAX | 0.78 | 0.83 |
| 755 | -------|--------------|---------------- |
| 756 | |
| 757 | 14kHz | 24kbit | 32kbit | 48kbit |
| 758 | -------|--------------|----------------|---------------- |
| 759 | AVG | 0.62 | 0.90 | 1.11 |
| 760 | -------|--------------|----------------|---------------- |
| 761 | MAX | 1.16 | 1.39 | 1.54 |
| 762 | -------|--------------|----------------|---------------- |
| 763 | |
| 764 | ***************************************************************************/ |
| 765 | |
| 766 | void vector_quantize_mlts(Word16 number_of_available_bits, |
| 767 | Word16 number_of_regions, |
| 768 | Word16 num_categorization_control_possibilities, |
| 769 | Word16 *mlt_coefs, |
| 770 | Word16 *absolute_region_power_index, |
| 771 | Word16 *power_categories, |
| 772 | Word16 *category_balances, |
| 773 | Word16 *p_categorization_control, |
| 774 | Word16 *region_mlt_bit_counts, |
| 775 | UWord32 *region_mlt_bits) |
| 776 | { |
| 777 | |
| 778 | Word16 *raw_mlt_ptr; |
| 779 | Word16 region; |
| 780 | Word16 category; |
| 781 | Word16 total_mlt_bits = 0; |
| 782 | |
| 783 | Word16 temp; |
| 784 | Word16 temp1; |
| 785 | Word16 temp2; |
| 786 | |
| 787 | /* Start in the middle of the categorization control range. */ |
| 788 | temp = shr_nocheck(num_categorization_control_possibilities,1); |
| 789 | temp = sub(temp,1); |
| 790 | for (*p_categorization_control = 0; *p_categorization_control < temp; (*p_categorization_control)++) |
| 791 | { |
| 792 | region = category_balances[*p_categorization_control]; |
| 793 | move16(); |
| 794 | power_categories[region] = add(power_categories[region],1); |
| 795 | move16(); |
| 796 | } |
| 797 | |
| 798 | for (region=0; region<number_of_regions; region++) |
| 799 | { |
| 800 | category = power_categories[region]; |
| 801 | move16(); |
| 802 | temp = extract_l(L_mult0(region,REGION_SIZE)); |
| 803 | raw_mlt_ptr = &mlt_coefs[temp]; |
| 804 | move16(); |
| 805 | temp = sub(category,(NUM_CATEGORIES-1)); |
| 806 | test(); |
| 807 | if (temp < 0) |
| 808 | { |
| 809 | region_mlt_bit_counts[region] = |
| 810 | vector_huffman(category, absolute_region_power_index[region],raw_mlt_ptr, |
| 811 | ®ion_mlt_bits[shl_nocheck(region,2)]); |
| 812 | } |
| 813 | else |
| 814 | { |
| 815 | region_mlt_bit_counts[region] = 0; |
| 816 | move16(); |
| 817 | } |
| 818 | total_mlt_bits = add(total_mlt_bits,region_mlt_bit_counts[region]); |
| 819 | } |
| 820 | |
| 821 | |
| 822 | /* If too few bits... */ |
| 823 | temp = sub(total_mlt_bits,number_of_available_bits); |
| 824 | test(); |
| 825 | test(); |
| 826 | logic16(); |
| 827 | while ((temp < 0) && (*p_categorization_control > 0)) |
| 828 | { |
| 829 | test(); |
| 830 | test(); |
| 831 | logic16(); |
| 832 | (*p_categorization_control)--; |
| 833 | region = category_balances[*p_categorization_control]; |
| 834 | move16(); |
| 835 | |
| 836 | power_categories[region] = sub(power_categories[region],1); |
| 837 | move16(); |
| 838 | |
| 839 | total_mlt_bits = sub(total_mlt_bits,region_mlt_bit_counts[region]); |
| 840 | category = power_categories[region]; |
| 841 | move16(); |
| 842 | |
| 843 | raw_mlt_ptr = &mlt_coefs[region*REGION_SIZE]; |
| 844 | move16(); |
| 845 | |
| 846 | temp = sub(category,(NUM_CATEGORIES-1)); |
| 847 | test(); |
| 848 | if (temp < 0) |
| 849 | { |
| 850 | region_mlt_bit_counts[region] = |
| 851 | vector_huffman(category, absolute_region_power_index[region],raw_mlt_ptr, |
| 852 | ®ion_mlt_bits[shl_nocheck(region,2)]); |
| 853 | } |
| 854 | else |
| 855 | { |
| 856 | region_mlt_bit_counts[region] = 0; |
| 857 | move16(); |
| 858 | } |
| 859 | total_mlt_bits = add(total_mlt_bits,region_mlt_bit_counts[region]); |
| 860 | temp = sub(total_mlt_bits,number_of_available_bits); |
| 861 | } |
| 862 | |
| 863 | /* If too many bits... */ |
| 864 | /* Set up for while loop test */ |
| 865 | temp1 = sub(total_mlt_bits,number_of_available_bits); |
| 866 | temp2 = sub(*p_categorization_control,sub(num_categorization_control_possibilities,1)); |
| 867 | test(); |
| 868 | test(); |
| 869 | logic16(); |
| 870 | |
| 871 | while ((temp1 > 0) && (temp2 < 0)) |
| 872 | { |
| 873 | /* operations for while contitions */ |
| 874 | test(); |
| 875 | test(); |
| 876 | logic16(); |
| 877 | |
| 878 | region = category_balances[*p_categorization_control]; |
| 879 | move16(); |
| 880 | |
| 881 | power_categories[region] = add(power_categories[region],1); |
| 882 | move16(); |
| 883 | |
| 884 | total_mlt_bits = sub(total_mlt_bits,region_mlt_bit_counts[region]); |
| 885 | category = power_categories[region]; |
| 886 | move16(); |
| 887 | |
| 888 | temp = extract_l(L_mult0(region,REGION_SIZE)); |
| 889 | raw_mlt_ptr = &mlt_coefs[temp]; |
| 890 | move16(); |
| 891 | |
| 892 | temp = sub(category,(NUM_CATEGORIES-1)); |
| 893 | test(); |
| 894 | if (temp < 0) |
| 895 | { |
| 896 | region_mlt_bit_counts[region] = |
| 897 | vector_huffman(category, absolute_region_power_index[region],raw_mlt_ptr, |
| 898 | ®ion_mlt_bits[shl_nocheck(region,2)]); |
| 899 | } |
| 900 | else |
| 901 | { |
| 902 | region_mlt_bit_counts[region] = 0; |
| 903 | move16(); |
| 904 | } |
| 905 | total_mlt_bits = add(total_mlt_bits,region_mlt_bit_counts[region]); |
| 906 | (*p_categorization_control)++; |
| 907 | |
| 908 | temp1 = sub(total_mlt_bits,number_of_available_bits); |
| 909 | temp2 = sub(*p_categorization_control,sub(num_categorization_control_possibilities,1)); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | /*************************************************************************** |
| 914 | Function: vector_huffman |
| 915 | |
| 916 | Syntax: Word16 vector_huffman(Word16 category, |
| 917 | Word16 power_index, |
| 918 | Word16 *raw_mlt_ptr, |
| 919 | UWord32 *word_ptr) |
| 920 | |
| 921 | inputs: Word16 category |
| 922 | Word16 power_index |
| 923 | Word16 *raw_mlt_ptr |
| 924 | |
| 925 | outputs: number_of_region_bits |
| 926 | *word_ptr |
| 927 | |
| 928 | |
| 929 | Description: Huffman encoding for each region based on category and power_index |
| 930 | |
| 931 | WMOPS: 7kHz | 24kbit | 32kbit |
| 932 | -------|--------------|---------------- |
| 933 | AVG | 0.03 | 0.03 |
| 934 | -------|--------------|---------------- |
| 935 | MAX | 0.04 | 0.04 |
| 936 | -------|--------------|---------------- |
| 937 | |
| 938 | 14kHz | 24kbit | 32kbit | 48kbit |
| 939 | -------|--------------|----------------|---------------- |
| 940 | AVG | 0.03 | 0.03 | 0.03 |
| 941 | -------|--------------|----------------|---------------- |
| 942 | MAX | 0.04 | 0.04 | 0.04 |
| 943 | -------|--------------|----------------|---------------- |
| 944 | |
| 945 | ***************************************************************************/ |
| 946 | Word16 vector_huffman(Word16 category, |
| 947 | Word16 power_index, |
| 948 | Word16 *raw_mlt_ptr, |
| 949 | UWord32 *word_ptr) |
| 950 | { |
| 951 | |
| 952 | |
| 953 | Word16 inv_of_step_size_times_std_dev; |
| 954 | Word16 j,n; |
| 955 | Word16 k; |
| 956 | Word16 number_of_region_bits; |
| 957 | Word16 number_of_non_zero; |
| 958 | Word16 vec_dim; |
| 959 | Word16 num_vecs; |
| 960 | Word16 kmax, kmax_plus_one; |
| 961 | Word16 index,signs_index; |
| 962 | Word16 *bitcount_table_ptr; |
| 963 | UWord16 *code_table_ptr; |
| 964 | Word32 code_bits; |
| 965 | Word16 number_of_code_bits; |
| 966 | UWord32 current_word; |
| 967 | Word16 current_word_bits_free; |
| 968 | |
| 969 | Word32 acca; |
| 970 | Word32 accb; |
| 971 | Word16 temp; |
| 972 | |
| 973 | Word16 mytemp; /* new variable in Release 1.2 */ |
| 974 | Word16 myacca; /* new variable in Release 1.2 */ |
| 975 | |
| 976 | |
| 977 | /* initialize variables */ |
| 978 | vec_dim = vector_dimension[category]; |
| 979 | move16(); |
| 980 | |
| 981 | num_vecs = number_of_vectors[category]; |
| 982 | move16(); |
| 983 | |
| 984 | kmax = max_bin[category]; |
| 985 | move16(); |
| 986 | |
| 987 | kmax_plus_one = add(kmax,1); |
| 988 | move16(); |
| 989 | |
| 990 | current_word = 0L; |
| 991 | move16(); |
| 992 | |
| 993 | current_word_bits_free = 32; |
| 994 | move16(); |
| 995 | |
| 996 | number_of_region_bits = 0; |
| 997 | move16(); |
| 998 | |
| 999 | /* set up table pointers */ |
| 1000 | bitcount_table_ptr = (Word16 *)table_of_bitcount_tables[category]; |
| 1001 | code_table_ptr = (UWord16 *) table_of_code_tables[category]; |
| 1002 | |
| 1003 | /* compute inverse of step size * standard deviation */ |
| 1004 | acca = L_mult(step_size_inverse_table[category],standard_deviation_inverse_table[power_index]); |
| 1005 | acca = L_shr_nocheck(acca,1); |
| 1006 | acca = L_add(acca,4096); |
| 1007 | acca = L_shr_nocheck(acca,13); |
| 1008 | |
| 1009 | /* |
| 1010 | * The next two lines are new to Release 1.2 |
| 1011 | */ |
| 1012 | |
| 1013 | mytemp = (Word16)(acca & 0x3); |
| 1014 | acca = L_shr_nocheck(acca,2); |
| 1015 | |
| 1016 | inv_of_step_size_times_std_dev = extract_l(acca); |
| 1017 | |
| 1018 | |
| 1019 | for (n=0; n<num_vecs; n++) |
| 1020 | { |
| 1021 | index = 0; |
| 1022 | move16(); |
| 1023 | |
| 1024 | signs_index = 0; |
| 1025 | move16(); |
| 1026 | |
| 1027 | number_of_non_zero = 0; |
| 1028 | move16(); |
| 1029 | |
| 1030 | for (j=0; j<vec_dim; j++) |
| 1031 | { |
| 1032 | k = abs_s(*raw_mlt_ptr); |
| 1033 | |
| 1034 | acca = L_mult(k,inv_of_step_size_times_std_dev); |
| 1035 | acca = L_shr_nocheck(acca,1); |
| 1036 | |
| 1037 | /* |
| 1038 | * The next four lines are new to Release 1.2 |
| 1039 | */ |
| 1040 | |
| 1041 | myacca = (Word16)L_mult(k,mytemp); |
| 1042 | myacca = (Word16)L_shr_nocheck(myacca,1); |
| 1043 | myacca = (Word16)L_add(myacca,int_dead_zone_low_bits[category]); |
| 1044 | myacca = (Word16)L_shr_nocheck(myacca,2); |
| 1045 | |
| 1046 | acca = L_add(acca,int_dead_zone[category]); |
| 1047 | |
| 1048 | /* |
| 1049 | * The next two lines are new to Release 1.2 |
| 1050 | */ |
| 1051 | |
| 1052 | acca = L_add(acca,myacca); |
| 1053 | acca = L_shr_nocheck(acca,13); |
| 1054 | |
| 1055 | k = extract_l(acca); |
| 1056 | |
| 1057 | test(); |
| 1058 | if (k != 0) |
| 1059 | { |
| 1060 | number_of_non_zero = add(number_of_non_zero,1); |
| 1061 | signs_index = shl_nocheck(signs_index,1); |
| 1062 | |
| 1063 | test(); |
| 1064 | if (*raw_mlt_ptr > 0) |
| 1065 | { |
| 1066 | signs_index = add(signs_index,1); |
| 1067 | } |
| 1068 | |
| 1069 | temp = sub(k,kmax); |
| 1070 | test(); |
| 1071 | if (temp > 0) |
| 1072 | { |
| 1073 | k = kmax; |
| 1074 | move16(); |
| 1075 | } |
| 1076 | } |
| 1077 | acca = L_shr_nocheck(L_mult(index,(kmax_plus_one)),1); |
| 1078 | index = extract_l(acca); |
| 1079 | index = add(index,k); |
| 1080 | raw_mlt_ptr++; |
| 1081 | } |
| 1082 | |
| 1083 | code_bits = *(code_table_ptr+index); |
| 1084 | number_of_code_bits = add((*(bitcount_table_ptr+index)),number_of_non_zero); |
| 1085 | number_of_region_bits = add(number_of_region_bits,number_of_code_bits); |
| 1086 | |
| 1087 | acca = code_bits << number_of_non_zero; |
| 1088 | accb = L_deposit_l(signs_index); |
| 1089 | acca = L_add(acca,accb); |
| 1090 | code_bits = acca; |
| 1091 | move32(); |
| 1092 | |
| 1093 | /* msb of codebits is transmitted first. */ |
| 1094 | j = sub(current_word_bits_free,number_of_code_bits); |
| 1095 | test(); |
| 1096 | if (j >= 0) |
| 1097 | { |
| 1098 | test(); |
| 1099 | acca = code_bits << j; |
| 1100 | current_word = L_add(current_word,acca); |
| 1101 | current_word_bits_free = j; |
| 1102 | move16(); |
| 1103 | } |
| 1104 | else |
| 1105 | { |
| 1106 | j = negate(j); |
| 1107 | acca = L_shr_nocheck(code_bits,j); |
| 1108 | current_word = L_add(current_word,acca); |
| 1109 | |
| 1110 | *word_ptr++ = current_word; |
| 1111 | move16(); |
| 1112 | |
| 1113 | current_word_bits_free = sub(32,j); |
| 1114 | test(); |
| 1115 | current_word = code_bits << current_word_bits_free; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | *word_ptr++ = current_word; |
| 1120 | move16(); |
| 1121 | |
| 1122 | return (number_of_region_bits); |
| 1123 | } |
| 1124 | |
| 1125 | |