1
/****************************************************************************************
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)
7
** � 2004 Polycom, Inc.
9
** All rights reserved.
11
****************************************************************************************/
13
/****************************************************************************************
16
Purpose: Contains the functions used for both G.722.1 Annex C encoder and decoder
20
****************************************************************************************/
21
/****************************************************************************************
23
****************************************************************************************/
30
/****************************************************************************************
33
Syntax: void categorize(Word16 number_of_available_bits,
34
Word16 number_of_regions,
35
Word16 num_categorization_control_possibilities,
37
Word16 power_categories,
38
Word16 category_balances)
40
inputs: number_of_regions
41
num_categorization_control_possibilities
42
number_of_available_bits
43
rms_index[MAX_NUMBER_OF_REGIONS]
45
outputs: power_categories[MAX_NUMBER_OF_REGIONS]
46
category_balances[MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES-1]
48
Description: Computes a series of categorizations
50
WMOPS: 7kHz | 24kbit | 32kbit
51
-------|--------------|----------------
53
-------|--------------|----------------
55
-------|--------------|----------------
57
14kHz | 24kbit | 32kbit | 48kbit
58
-------|--------------|----------------|----------------
59
AVG | 0.42 | 0.45 | 0.48
60
-------|--------------|----------------|----------------
61
MAX | 0.47 | 0.52 | 0.52
62
-------|--------------|----------------|----------------
64
****************************************************************************************/
65
void categorize(Word16 number_of_available_bits,
66
Word16 number_of_regions,
67
Word16 num_categorization_control_possibilities,
69
Word16 *power_categories,
70
Word16 *category_balances)
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
81
if (number_of_regions == NUMBER_OF_REGIONS)
83
frame_size = DCT_LENGTH;
87
frame_size = MAX_DCT_LENGTH;
90
temp = sub(number_of_available_bits,frame_size);
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);
101
/* calculate the offset using the original category assignments */
102
offset = calc_offset(rms_index,number_of_regions,number_of_available_bits);
106
/* compute the power categories based on the uniform offset */
107
compute_raw_pow_categories(power_categories,rms_index,number_of_regions,offset);
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);
116
/***************************************************************************
117
Function: comp_powercat_and_catbalance
119
Syntax: void comp_powercat_and_catbalance(Word16 *power_categories,
120
Word16 *category_balances,
122
Word16 number_of_available_bits,
123
Word16 number_of_regions,
124
Word16 num_categorization_control_possibilities,
129
number_of_available_bits
131
num_categorization_control_possibilities
134
outputs: *power_categories
138
Description: Computes the power_categories and the category balances
140
WMOPS: 7kHz | 24kbit | 32kbit
141
-------|--------------|----------------
143
-------|--------------|----------------
145
-------|--------------|----------------
147
14kHz | 24kbit | 32kbit | 48kbit
148
-------|--------------|----------------|----------------
149
AVG | 0.32 | 0.35 | 0.38
150
-------|--------------|----------------|----------------
151
MAX | 0.38 | 0.42 | 0.43
152
-------|--------------|----------------|----------------
154
***************************************************************************/
155
void comp_powercat_and_catbalance(Word16 *power_categories,
156
Word16 *category_balances,
158
Word16 number_of_available_bits,
159
Word16 number_of_regions,
160
Word16 num_categorization_control_possibilities,
164
Word16 expected_number_of_code_bits;
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;
178
Word16 two_x_number_of_available_bits;
182
expected_number_of_code_bits = 0;
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]]);
189
for (region=0; region<number_of_regions; region++)
191
max_rate_categories[region] = power_categories[region];
194
min_rate_categories[region] = power_categories[region];
198
max = expected_number_of_code_bits;
200
min = expected_number_of_code_bits;
202
max_rate_pointer = num_categorization_control_possibilities;
204
min_rate_pointer = num_categorization_control_possibilities;
207
for (j=0; j<num_categorization_control_possibilities-1; j++)
209
min_plus_max = add(max,min);
210
two_x_number_of_available_bits = shl_nocheck(number_of_available_bits,1);
212
temp = sub(min_plus_max,two_x_number_of_available_bits);
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++)
223
if (max_rate_categories[region] > 0)
225
itemp0 = shl_nocheck(max_rate_categories[region],1);
226
itemp1 = sub(offset,rms_index[region]);
227
itemp0 = sub(itemp1,itemp0);
229
temp = sub(itemp0,raw_min);
234
raw_min_index = region;
238
max_rate_pointer = sub(max_rate_pointer,1);
239
temp_category_balances[max_rate_pointer] = raw_min_index;
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);
246
max = add(max,expected_bits_table[max_rate_categories[raw_min_index]]);
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--)
257
temp = sub(min_rate_categories[region],(NUM_CATEGORIES-1));
261
itemp0 = shl_nocheck(min_rate_categories[region],1);
262
itemp1 = sub(offset,rms_index[region]);
263
itemp0 = sub(itemp1,itemp0);
265
temp = sub(itemp0,raw_max);
271
raw_max_index = region;
276
temp_category_balances[min_rate_pointer] = raw_max_index;
279
min_rate_pointer = add(min_rate_pointer,1);
280
min = sub(min,expected_bits_table[min_rate_categories[raw_max_index]]);
282
min_rate_categories[raw_max_index] = add(min_rate_categories[raw_max_index],1);
285
min = add(min,expected_bits_table[min_rate_categories[raw_max_index]]);
289
for (region=0; region<number_of_regions; region++)
291
power_categories[region] = max_rate_categories[region];
295
for (j=0; j<num_categorization_control_possibilities-1; j++)
297
category_balances[j] = temp_category_balances[max_rate_pointer++];
302
/***************************************************************************
303
Function: calc_offset
305
Syntax: offset=calc_offset(Word16 *rms_index,Word16 number_of_regions,Word16 available_bits)
307
input: Word16 *rms_index
308
Word16 number_of_regions
309
Word16 available_bits
311
output: Word16 offset
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.
317
WMOPS: 7kHz | 24kbit | 32kbit
318
-------|--------------|----------------
320
-------|--------------|----------------
322
-------|--------------|----------------
324
14kHz | 24kbit | 32kbit | 48kbit
325
-------|--------------|----------------|----------------
326
AVG | 0.08 | 0.08 | 0.08
327
-------|--------------|----------------|----------------
328
MAX | 0.09 | 0.09 | 0.09
329
-------|--------------|----------------|----------------
331
***************************************************************************/
332
Word16 calc_offset(Word16 *rms_index,Word16 number_of_regions,Word16 available_bits)
339
Word16 power_cats[MAX_NUMBER_OF_REGIONS];
344
/* initialize vars */
352
test_offset = add(answer,delta);
354
/* obtain a category for each region */
355
/* using the test offset */
356
for (region=0; region<number_of_regions; region++)
358
j = sub(test_offset,rms_index[region]);
359
j = shr_nocheck(j,1);
361
/* Ensure j is between 0 and NUM_CAT-1 */
368
temp = sub(j,NUM_CATEGORIES-1);
372
j = sub(NUM_CATEGORIES,1);
375
power_cats[region] = j;
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]]);
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);
391
answer = test_offset;
394
delta = shr_nocheck(delta,1);
395
test(); /* for the while loop */
400
/***************************************************************************
401
Function: compute_raw_pow_categories
403
Syntax: void compute_raw_pow_categories(Word16 *power_categories,
405
Word16 number_of_regions,
411
outputs: *power_categories
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.
419
WMOPS: | 24kbit | 32kbit
420
-------|--------------|----------------
422
-------|--------------|----------------
424
-------|--------------|----------------
426
14kHz | 24kbit | 32kbit | 48kbit
427
-------|--------------|----------------|----------------
428
AVG | 0.01 | 0.01 | 0.01
429
-------|--------------|----------------|----------------
430
MAX | 0.01 | 0.01 | 0.01
431
-------|--------------|----------------|----------------
433
***************************************************************************/
434
void compute_raw_pow_categories(Word16 *power_categories,Word16 *rms_index,Word16 number_of_regions,Word16 offset)
440
for (region=0; region<number_of_regions; region++)
442
j = sub(offset,rms_index[region]);
443
j = shr_nocheck(j,1);
445
/* make sure j is between 0 and NUM_CAT-1 */
452
temp = sub(j,(NUM_CATEGORIES-1));
455
j = sub(NUM_CATEGORIES,1);
457
power_categories[region] = j;