blob: c6b8c065d1e6087c8d80fd0e94d6a433227fa8d0 [file] [log] [blame]
Nanang Izzuddin57b88572009-04-01 12:05:34 +00001/***************************************************************************
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: decoder.c
15
16 Purpose: Contains files used to implement the G.722.1 Annex C decoder
17
18 Design Notes:
19
20***************************************************************************/
21
22/***************************************************************************
23 Include files
24***************************************************************************/
25#include "defs.h"
26#include "tables.h"
27#include "huff_def.h"
28#include "count.h"
29
30
31/***************************************************************************
32 Function: decoder
33
34 Syntax: void decoder(Bit_Obj *bitobj,
35 Rand_Obj *randobj,
36 Word16 number_of_regions,
37 Word16 *decoder_mlt_coefs,
38 Word16 *p_mag_shift,
39 Word16 *p_old_mag_shift,
40 Word16 *old_decoder_mlt_coefs,
41 Word16 frame_error_flag)
42
43 inputs: Bit_Obj *bitobj
44 Rand_Obj *randobj
45 Word16 number_of_regions
46 Word16 *p_old_mag_shift
47 Word16 *old_decoder_mlt_coefs
48 Word16 frame_error_flag
49
50 outputs: Word16 *decoder_mlt_coefs,
51 Word16 *p_mag_shift,
52
53
54
55 Description: Decodes the out_words into mlt coefs using G.722.1 Annex C
56
57 Design Notes:
58
59 WMOPS: 7kHz | 24kbit | 32kbit
60 -------|-------------|----------------
61 AVG | 0.84 | 0.94
62 -------|-------------|----------------
63 MAX | 0.90 | 1.00
64 -------|-------------|----------------
65
66 14kHz | 24kbit | 32kbit | 48kbit
67 -------|-------------|----------------|----------------
68 AVG | 1.31 | 1.56 | 1.88
69 -------|-------------|----------------|----------------
70 MAX | 1.59 | 1.80 | 1.98
71 -------|-------------|----------------|----------------
72
73***************************************************************************/
74void decoder(Bit_Obj *bitobj,
75 Rand_Obj *randobj,
76 Word16 number_of_regions,
77 Word16 *decoder_mlt_coefs,
78 Word16 *p_mag_shift,
79 Word16 *p_old_mag_shift,
80 Word16 *old_decoder_mlt_coefs,
81 Word16 frame_error_flag)
82{
83
84
85 Word16 absolute_region_power_index[MAX_NUMBER_OF_REGIONS];
86 Word16 decoder_power_categories[MAX_NUMBER_OF_REGIONS];
87 Word16 decoder_category_balances[MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES-1];
88 UWord16 categorization_control;
89 Word16 decoder_region_standard_deviation[MAX_NUMBER_OF_REGIONS];
90 Word16 i;
91
92 Word16 num_categorization_control_bits;
93 Word16 num_categorization_control_possibilities;
94 Word16 number_of_coefs;
95 Word16 number_of_valid_coefs;
96
97
98 test();
99 if (number_of_regions==NUMBER_OF_REGIONS)
100 {
101 num_categorization_control_bits = NUM_CATEGORIZATION_CONTROL_BITS;
102 move16();
103 num_categorization_control_possibilities = NUM_CATEGORIZATION_CONTROL_POSSIBILITIES;
104 move16();
105 number_of_coefs = DCT_LENGTH;
106 move16();
107 number_of_valid_coefs = NUMBER_OF_VALID_COEFS;
108 move16();
109 }
110 else
111 {
112 num_categorization_control_bits = MAX_NUM_CATEGORIZATION_CONTROL_BITS;
113 move16();
114 num_categorization_control_possibilities = MAX_NUM_CATEGORIZATION_CONTROL_POSSIBILITIES;
115 move16();
116 number_of_coefs = MAX_DCT_LENGTH;
117 move16();
118 number_of_valid_coefs = MAX_NUMBER_OF_VALID_COEFS;
119 move16();
120 }
121
122 test();
123 if (frame_error_flag == 0)
124 {
125
126 /* convert the bits to absolute region power index and decoder_region_standard_deviation */
127
128 decode_envelope(bitobj,
129 number_of_regions,
130 decoder_region_standard_deviation,
131 absolute_region_power_index,
132 p_mag_shift);
133
134 /* fill the categorization_control with NUM_CATEGORIZATION_CONTROL_BITS */
135 categorization_control = 0;
136 for (i=0; i<num_categorization_control_bits; i++)
137 {
138 get_next_bit(bitobj);
139 categorization_control = shl(categorization_control,1);
140 categorization_control = add(categorization_control,bitobj->next_bit);
141 }
142
143 bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,num_categorization_control_bits);
144
145 /* obtain decoder power categories and category balances */
146 /* based on the absolute region power index */
147 categorize(bitobj->number_of_bits_left,
148 number_of_regions,
149 num_categorization_control_possibilities,
150 absolute_region_power_index,
151 decoder_power_categories,
152 decoder_category_balances);
153
154 /* perform adjustmaents to the power categories and category balances based on the cat control */
155 rate_adjust_categories(categorization_control,
156 decoder_power_categories,
157 decoder_category_balances);
158
159 /* decode the quantized bits into mlt coefs */
160 decode_vector_quantized_mlt_indices(bitobj,
161 randobj,
162 number_of_regions,
163 decoder_region_standard_deviation,
164 decoder_power_categories,
165 decoder_mlt_coefs);
166
167 /* test for frame errors */
168 test_4_frame_errors(bitobj,
169 number_of_regions,
170 num_categorization_control_possibilities,
171 &frame_error_flag,
172 categorization_control,
173 absolute_region_power_index);
174 }
175
176 /* perform error handling operations */
177 error_handling(number_of_coefs,
178 number_of_valid_coefs,
179 &frame_error_flag,
180 decoder_mlt_coefs,
181 old_decoder_mlt_coefs,
182 p_mag_shift,
183 p_old_mag_shift);
184
185}
186
187/***************************************************************************
188 Function: decode_envelope
189
190 Syntax: void decode_envelope(Bit_Obj *bitobj,
191 Word16 number_of_regions,
192 Word16 *decoder_region_standard_deviation,
193 Word16 *absolute_region_power_index,
194 Word16 *p_mag_shift)
195
196 inputs: Bit_Obj *bitobj
197 Word16 number_of_regions
198
199
200 outputs: Word16 *decoder_region_standard_deviation
201 Word16 *absolute_region_power_index
202 Word16 *p_mag_shift
203
204
205 Description: Recover differential_region_power_index from code bits
206
207 Design Notes:
208
209 WMOPS: 7kHz | 24kbit | 32kbit
210 -------|--------------|----------------
211 AVG | 0.04 | 0.04
212 -------|--------------|----------------
213 MAX | 0.05 | 0.05
214 -------|--------------|----------------
215
216 14kHz | 24kbit | 32kbit | 48kbit
217 -------|--------------|----------------|----------------
218 AVG | 0.08 | 0.08 | 0.08
219 -------|--------------|----------------|----------------
220 MAX | 0.10 | 0.10 | 0.10
221 -------|--------------|----------------|----------------
222
223***************************************************************************/
224void decode_envelope(Bit_Obj *bitobj,
225 Word16 number_of_regions,
226 Word16 *decoder_region_standard_deviation,
227 Word16 *absolute_region_power_index,
228 Word16 *p_mag_shift)
229
230{
231 Word16 region;
232 Word16 i;
233 Word16 index;
234 Word16 differential_region_power_index[MAX_NUMBER_OF_REGIONS];
235 Word16 max_index;
236
237 Word16 temp;
238 Word16 temp1;
239 Word16 temp2;
240 Word32 acca;
241
242 index = 0;
243 move16();
244
245 /* get 5 bits from the current code word */
246 for (i=0; i<5; i++)
247 {
248 get_next_bit(bitobj);
249 index = shl(index,1);
250 index = add(index,bitobj->next_bit);
251 }
252 bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,5);
253
254 /* ESF_ADJUSTMENT_TO_RMS_INDEX compensates for the current (9/30/96)
255 IMLT being scaled to high by the ninth power of sqrt(2). */
256 differential_region_power_index[0] = sub(index,ESF_ADJUSTMENT_TO_RMS_INDEX);
257 move16();
258
259 /* obtain differential_region_power_index */
260 for (region=1; region<number_of_regions; region++)
261 {
262 index = 0;
263 move16();
264 do
265 {
266 get_next_bit(bitobj);
267 test();
268 if (bitobj->next_bit == 0)
269 {
270 index = differential_region_power_decoder_tree[region][index][0];
271 move16();
272 }
273 else
274 {
275 index = differential_region_power_decoder_tree[region][index][1];
276 move16();
277 }
278 bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,1);
279 test();
280 } while (index > 0);
281
282 differential_region_power_index[region] = negate(index);
283 move16();
284 }
285
286 /* Reconstruct absolute_region_power_index[] from differential_region_power_index[]. */
287 absolute_region_power_index[0] = differential_region_power_index[0];
288 move16();
289 for (region=1; region<number_of_regions; region++)
290 {
291 acca = L_add(absolute_region_power_index[region-1],differential_region_power_index[region]);
292 acca = L_add(acca,DRP_DIFF_MIN);
293 absolute_region_power_index[region] = extract_l(acca);
294 }
295
296 /* Reconstruct decoder_region_standard_deviation[] from absolute_region_power_index[]. */
297 /* DEBUG!!!! - This integer method jointly computes the mag_shift
298 and the standard deviations already mag_shift compensated. It
299 relies on REGION_POWER_STEPSIZE_DB being exactly 3.010299957 db
300 or a square root of 2 chnage in standard deviation. If
301 REGION_POWER_STEPSIZE_DB changes, this software must be
302 reworked. */
303
304 temp = 0;
305 move16();
306 max_index = 0;
307 move16();
308 for (region=0; region<number_of_regions; region++)
309 {
310 acca = L_add(absolute_region_power_index[region],REGION_POWER_TABLE_NUM_NEGATIVES);
311 i = extract_l(acca);
312
313 temp1 = sub(i,max_index);
314 test();
315 if (temp1 > 0)
316 {
317 max_index = i;
318 move16();
319 }
320 temp = add(temp,int_region_standard_deviation_table[i]);
321 }
322 i = 9;
323 move16();
324
325 temp1 = sub(temp,8);
326 temp2 = sub(max_index,28);
327 test();
328 test();
329 logic16();
330 test();
331 logic16();
332 while ((i >= 0) && ((temp1 >= 0) || (temp2 > 0)))
333 {
334 i = sub(i,1);
335 temp = shr(temp,1);
336 max_index = sub(max_index,2);
337 temp1 = sub(temp,8);
338 temp2 = sub(max_index,28);
339 test();
340 test();
341 logic16();
342 test();
343 logic16();
344 }
345
346 *p_mag_shift = i;
347 move16();
348
349 /* pointer arithmetic */
350 temp = (Word16 )(REGION_POWER_TABLE_NUM_NEGATIVES + (*p_mag_shift * 2));
351
352 for (region=0; region<number_of_regions; region++)
353 {
354 acca = L_add(absolute_region_power_index[region],temp);
355 i = extract_l(acca);
356 decoder_region_standard_deviation[region] = int_region_standard_deviation_table[i];
357 move16();
358 }
359
360}
361
362/***************************************************************************
363 Function: rate_adjust_categories
364
365 Syntax: void rate_adjust_categories(Word16 categorization_control,
366 Word16 *decoder_power_categories,
367 Word16 *decoder_category_balances)
368
369 inputs: Word16 categorization_control,
370 Word16 *decoder_power_categories,
371 Word16 *decoder_category_balances
372
373 outputs: Word16 categorization_control,
374 Word16 *decoder_power_categories,
375
376 Description: Adjust the power categories based on the categorization control
377
378 Design Notes:
379
380 WMOPS: 7kHz | 24kbit | 32kbit
381 -------|--------------|----------------
382 AVG | 0.00 | 0.00
383 -------|--------------|----------------
384 MAX | 0.00 | 0.00
385 -------|--------------|----------------
386
387 14kHz | 24kbit | 32kbit | 48kbit
388 -------|--------------|----------------|----------------
389 AVG | 0.00 | 0.00 | 0.00
390 -------|--------------|----------------|----------------
391 MAX | 0.01 | 0.01 | 0.01
392 -------|--------------|----------------|----------------
393
394***************************************************************************/
395void rate_adjust_categories(Word16 categorization_control,
396 Word16 *decoder_power_categories,
397 Word16 *decoder_category_balances)
398{
399 Word16 i;
400 Word16 region;
401
402 i = 0;
403 move16();
404
405 test();
406 while (categorization_control > 0)
407 {
408 region = decoder_category_balances[i++];
409 move16();
410 decoder_power_categories[region] = add(decoder_power_categories[region],1);
411 move16();
412 categorization_control = sub(categorization_control,1);
413 }
414
415}
416
417/***************************************************************************
418 Function: decode_vector_quantized_mlt_indices
419
420 Syntax: void decode_vector_quantized_mlt_indices(Bit_Obj *bitobj,
421 Rand_Obj *randobj,
422 Word16 number_of_regions,
423 Word16 *decoder_region_standard_deviation,
424 Word16 *decoder_power_categories,
425 Word16 *decoder_mlt_coefs)
426 inputs: Bit_Obj *bitobj
427 Rand_Obj *randobj
428 Word16 number_of_regions
429 Word16 *decoder_region_standard_deviation
430 Word16 *decoder_power_categories
431
432
433 outputs: Word16 *decoder_mlt_coefs
434
435
436 Description: Decode MLT coefficients
437
438 Design Notes:
439
440 WMOPS: 7kHz | 24kbit | 32kbit
441 -------|--------------|----------------
442 AVG | 0.60 | 0.72
443 -------|--------------|----------------
444 MAX | 0.67 | 0.76
445 -------|--------------|----------------
446
447 14kHz | 24kbit | 32kbit | 48kbit
448 -------|--------------|----------------|----------------
449 AVG | 0.77 | 0.98 | 1.28
450 -------|--------------|----------------|----------------
451 MAX | 1.05 | 1.18 | 1.36
452 -------|--------------|----------------|----------------
453
454***************************************************************************/
455void decode_vector_quantized_mlt_indices(Bit_Obj *bitobj,
456 Rand_Obj *randobj,
457 Word16 number_of_regions,
458 Word16 *decoder_region_standard_deviation,
459 Word16 *decoder_power_categories,
460 Word16 *decoder_mlt_coefs)
461{
462 Word16 standard_deviation;
463 Word16 *decoder_mlt_ptr;
464 Word16 decoder_mlt_value;
465 Word16 noifillpos;
466 Word16 noifillneg;
467 Word16 noise_fill_factor[3] = {5793,8192,23170};
468 Word16 region;
469 Word16 category;
470 Word16 j,n;
471 Word16 k[MAX_VECTOR_DIMENSION];
472 Word16 vec_dim;
473 Word16 num_vecs;
474 Word16 index;
475 Word16 signs_index;
476 Word16 bit;
477 Word16 num_sign_bits;
478 Word16 ran_out_of_bits_flag;
479 Word16 *decoder_table_ptr;
480 Word16 random_word;
481
482 Word16 temp1;
483 Word16 temp;
484 Word32 acca;
485
486 ran_out_of_bits_flag = 0;
487 move16();
488
489 for (region=0; region<number_of_regions; region++)
490 {
491 category = (Word16)decoder_power_categories[region];
492 move16();
493 acca = L_mult0(region,REGION_SIZE);
494 index = extract_l(acca);
495 decoder_mlt_ptr = &decoder_mlt_coefs[index];
496 move16();
497 standard_deviation = decoder_region_standard_deviation[region];
498 move16();
499
500 temp = sub(category,7);
501 test();
502 if (temp < 0)
503 {
504 /* Get the proper table of decoder tables, vec_dim, and num_vecs for the cat */
505 decoder_table_ptr = (Word16 *) table_of_decoder_tables[category];
506 move16();
507 vec_dim = vector_dimension[category];
508 move16();
509 num_vecs = number_of_vectors[category];
510 move16();
511
512 for (n=0; n<num_vecs; n++)
513 {
514 index = 0;
515 move16();
516
517 /* get index */
518 do
519 {
520 test();
521 if (bitobj->number_of_bits_left <= 0)
522 {
523 ran_out_of_bits_flag = 1;
524 move16();
525 break;
526 }
527
528 get_next_bit(bitobj);
529
530 test();
531 if (bitobj->next_bit == 0)
532 {
533 temp = shl(index,1);
534 index = (Word16)*(decoder_table_ptr + temp);
535 move16();
536 }
537 else
538 {
539 temp = shl(index,1);
540 index = (Word16)*(decoder_table_ptr + temp + 1);
541 move16();
542 }
543 bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,1);
544 test();
545
546 } while (index > 0);
547
548 test();
549 if (ran_out_of_bits_flag != 0)
550 break;
551
552 index = negate(index);
553
554 /* convert index into array used to access the centroid table */
555 /* get the number of sign bits in the index */
556 num_sign_bits = index_to_array(index,k,category);
557
558 temp = sub(bitobj->number_of_bits_left,num_sign_bits);
559 test();
560 if (temp >= 0)
561 {
562 test();
563 if (num_sign_bits != 0)
564 {
565 signs_index = 0;
566 move16();
567 for (j=0; j<num_sign_bits; j++)
568 {
569 get_next_bit(bitobj);
570 signs_index = shl(signs_index,1);
571 signs_index = add(signs_index,bitobj->next_bit);
572 bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,1);
573 }
574 temp = sub(num_sign_bits,1);
575 bit = shl(1,(temp));
576 }
577
578 for (j=0; j<vec_dim; j++)
579 {
580 acca = L_mult0(standard_deviation,mlt_quant_centroid[category][k[j]]);
581 acca = L_shr(acca,12);
582 decoder_mlt_value = extract_l(acca);
583
584 test();
585 if (decoder_mlt_value != 0)
586 {
587 test();
588 if ((signs_index & bit) == 0)
589 decoder_mlt_value = negate(decoder_mlt_value);
590 bit = shr(bit,1);
591 }
592 *decoder_mlt_ptr++ = decoder_mlt_value;
593 move16();
594 }
595 }
596 else
597 {
598 ran_out_of_bits_flag = 1;
599 move16();
600 break;
601 }
602 }
603 /* If ran out of bits during decoding do noise fill for remaining regions. */
604 /* DEBUG!! - For now also redo all of last region with all noise fill. */
605 test();
606 if (ran_out_of_bits_flag != 0)
607 {
608 temp = add(region,1);
609 for (j=temp; j<number_of_regions; j++)
610 {
611 decoder_power_categories[j] = 7;
612 move16();
613 }
614 category = 7;
615 move16();
616 decoder_mlt_ptr = &decoder_mlt_coefs[region*REGION_SIZE];
617 move16();
618 }
619 }
620
621 temp = sub(category,5);
622 temp1 = sub(category,6);
623 test();
624 test();
625 logic16();
626 if ((temp == 0) || (temp1 == 0))
627 {
628
629 decoder_mlt_ptr = &decoder_mlt_coefs[region*REGION_SIZE];
630 move16();
631 noifillpos = mult(standard_deviation,noise_fill_factor[category - 5]);
632 noifillneg = negate(noifillpos);
633
634 random_word = get_rand(randobj);
635
636 for (j=0; j<10; j++)
637 {
638 test();
639 if (*decoder_mlt_ptr == 0)
640 {
641 logic16();
642 test();
643 if ((random_word & 1) == 0)
644 {
645 temp1 = noifillneg;
646 move16();
647 }
648 else
649 {
650 temp1 = noifillpos;
651 move16();
652 }
653 *decoder_mlt_ptr = temp1;
654 move16();
655 random_word = shr(random_word,1);
656 }
657 /* pointer arithmetic */
658 decoder_mlt_ptr++;
659 }
660 random_word = get_rand(randobj);
661 for (j=0; j<10; j++)
662 {
663 test();
664 if (*decoder_mlt_ptr == 0)
665 {
666 logic16();
667 test();
668 if ((random_word & 1) == 0)
669 {
670 temp1 = noifillneg;
671 move16();
672 }
673 else
674 {
675 temp1 = noifillpos;
676 move16();
677 }
678 *decoder_mlt_ptr = temp1;
679 move16();
680 random_word = shr(random_word,1);
681 }
682 /* pointer arithmetic */
683 decoder_mlt_ptr++;
684 }
685 }
686
687 /* if (category == 7) */
688 temp1 = sub(category,7);
689 test();
690 if (temp1 == 0)
691 {
692 index = sub(category,5);
693 noifillpos = mult(standard_deviation,noise_fill_factor[index]);
694 noifillneg = negate(noifillpos);
695
696 random_word = get_rand(randobj);
697 for (j=0; j<10; j++)
698 {
699 logic16();
700 test();
701 if ((random_word & 1) == 0)
702 {
703 temp1 = noifillneg;
704 move16();
705 }
706 else
707 {
708 temp1 = noifillpos;
709 move16();
710 }
711 *decoder_mlt_ptr++ = temp1;
712 move16();
713 random_word = shr(random_word,1);
714 }
715 random_word = get_rand(randobj);
716 for (j=0; j<10; j++)
717 {
718 logic16();
719 test();
720 if ((random_word & 1) == 0)
721 {
722 temp1 = noifillneg;
723 move16();
724 }
725 else
726 {
727 temp1 = noifillpos;
728 move16();
729 }
730
731 *decoder_mlt_ptr++ = temp1;
732 move16();
733 random_word = shr(random_word,1);
734 }
735 }
736 }
737
738 test();
739 if (ran_out_of_bits_flag)
740 bitobj->number_of_bits_left = sub(bitobj->number_of_bits_left,1);
741}
742/****************************************************************************************
743 Function: index_to_array
744
745 Syntax: number_of_non_zero = index_to_array(Word16 index,
746 Word16 array[MAX_VECTOR_DIMENSION],
747 Word16 category)
748
749 inputs: Word16 index
750 Word16 category
751
752 outputs: Word16 array[MAX_VECTOR_DIMENSION] - used in decoder to access
753 mlt_quant_centroid table
754
755 Word16 number_of_non_zero - number of non zero elements
756 in the array
757
758 Description: Computes an array of sign bits with the length of the category vector
759 Returns the number of sign bits and the array
760
761 WMOPS: 7kHz | 24kbit | 32kbit
762 -------|--------------|----------------
763 AVG | 0.00 | 0.00
764 -------|--------------|----------------
765 MAX | 0.00 | 0.00
766 -------|--------------|----------------
767
768 14kHz | 24kbit | 32kbit | 48kbit
769 -------|--------------|----------------|----------------
770 AVG | 0.00 | 0.00 | 0.00
771 -------|--------------|----------------|----------------
772 MAX | 0.00 | 0.00 | 0.00
773 -------|--------------|----------------|----------------
774
775****************************************************************************************/
776Word16 index_to_array(Word16 index,Word16 *array,Word16 category)
777{
778 Word16 j,q,p;
779 Word16 number_of_non_zero;
780 Word16 max_bin_plus_one;
781 Word16 inverse_of_max_bin_plus_one;
782 Word16 temp;
783
784 number_of_non_zero = 0;
785 move16();
786
787 p = index;
788 move16();
789
790 max_bin_plus_one = add(max_bin[category],1);
791 inverse_of_max_bin_plus_one = max_bin_plus_one_inverse[category];
792 move16();
793
794 temp = sub(vector_dimension[category],1);
795 for (j=temp; j>=0; j--)
796 {
797 q = mult(p,inverse_of_max_bin_plus_one);
798 temp = extract_l(L_mult0(q,max_bin_plus_one));
799 array[j] = sub(p,temp);
800 move16();
801
802 p = q;
803 move16();
804
805 temp = array[j];
806 move16();
807 test();
808 if (temp != 0)
809 number_of_non_zero = add(number_of_non_zero,1);
810 }
811 return(number_of_non_zero);
812}
813/***************************************************************************
814 Function: test_4_frame_errors
815
816 Syntax: void test_4_frame_errors(Bit_Obj *bitobj,
817 Word16 number_of_regions,
818 Word16 num_categorization_control_possibilities,
819 Word16 *frame_error_flag,
820 Word16 categorization_control,
821 Word16 *absolute_region_power_index)
822
823 inputs: bit_obj
824 number_of_regions
825 num_categorization_control_possibilities
826 frame_error_flag
827 categorization_control
828 absolute_region_power_index
829
830
831 outputs: frame_error_flag
832
833
834
835
836 Description: Tests for error conditions and sets the frame_error_flag accordingly
837
838 Design Notes:
839
840 WMOPS: 7kHz | 24kbit | 32kbit
841 -------|--------------|----------------
842 AVG | 0.01 | 0.01
843 -------|--------------|----------------
844 MAX | 0.04 | 0.08
845 -------|--------------|----------------
846
847 14kHz | 24kbit | 32kbit | 48kbit
848 -------|--------------|----------------|----------------
849 AVG | 0.01 | 0.01 | 0.01
850 -------|--------------|----------------|----------------
851 MAX | 0.02 | 0.06 | 0.08
852 -------|--------------|----------------|----------------
853
854***************************************************************************/
855void test_4_frame_errors(Bit_Obj *bitobj,
856 Word16 number_of_regions,
857 Word16 num_categorization_control_possibilities,
858 Word16 *frame_error_flag,
859 Word16 categorization_control,
860 Word16 *absolute_region_power_index)
861{
862 Word16 region;
863 Word16 i;
864 Word16 temp;
865 Word32 acca;
866 Word32 accb;
867
868 /* Test for bit stream errors. */
869
870 test();
871 if (bitobj->number_of_bits_left > 0)
872 {
873 for (i=0; i<bitobj->number_of_bits_left; i++)
874 {
875 get_next_bit(bitobj);
876 test();
877 if (bitobj->next_bit == 0)
878 {
879 *frame_error_flag = 1;
880 move16();
881 }
882 }
883 }
884 else
885 {
886 temp = sub(categorization_control,sub(num_categorization_control_possibilities,1));
887 test();
888 if (temp < 0)
889 {
890 test();
891 if (bitobj->number_of_bits_left < 0)
892 {
893 *frame_error_flag |= 2;
894 logic16();
895 }
896 }
897 }
898
899 /* checks to ensure that abs_region_power_index is within range */
900 /* the error flag is set if it is out of range */
901 for (region=0; region<number_of_regions; region++)
902 {
903 /* the next two lines of comments were modified in release 1.2
904 * to correct the description of the range of
905 * absolute_region_power_index[] to be tested in the next
906 * 9 lines of code.
907 */
908 /* if ((absolute_region_power_index[region] > 31) ||
909 (absolute_region_power_index[region] < -8) */
910
911 acca = L_add(absolute_region_power_index[region],ESF_ADJUSTMENT_TO_RMS_INDEX);
912 accb = L_sub(acca,31);
913 acca = L_add(acca,8);
914 test();
915
916 /* the next line was modifed in release 1.2 to
917 * correct miss typed code and error checking.
918 */
919 if ((accb > 0) || (acca < 0))
920 {
921 *frame_error_flag |= 4;
922 logic16();
923 }
924 }
925
926}
927/***************************************************************************
928 Function: error_handling
929
930 Syntax: void error_handling(Word16 number_of_coefs,
931 Word16 number_of_valid_coefs,
932 Word16 *frame_error_flag,
933 Word16 *decoder_mlt_coefs,
934 Word16 *old_decoder_mlt_coefs,
935 Word16 *p_mag_shift,
936 Word16 *p_old_mag_shift)
937
938 inputs: number_of_coefs
939 number_of_valid_coefs
940 frame_error_flag
941 old_decoder_mlt_coefs
942 p_old_mag_shift
943
944
945 outputs: decoder_mlt_coefs
946 old_decoder_mlt_coefs
947 p_mag_shift
948 p_old_mag_shift
949
950
951
952 Description: If both the current and previous frames are errored,
953 set the mlt coefficients to 0. If only the current frame
954 is errored, then repeat the previous frame's mlt coefficients.
955
956 Design Notes:
957
958 WMOPS: 7kHz | 24kbit | 32kbit
959 -------|--------------|----------------
960 AVG | 0.02 | 0.02
961 -------|--------------|----------------
962 MAX | 0.03 | 0.03
963 -------|--------------|----------------
964
965 14kHz | 24kbit | 32kbit | 48kbit
966 -------|--------------|----------------|----------------
967 AVG | 0.03 | 0.03 | 0.03
968 -------|--------------|----------------|----------------
969 MAX | 0.03 | 0.03 | 0.06
970 -------|--------------|----------------|----------------
971
972***************************************************************************/
973void error_handling(Word16 number_of_coefs,
974 Word16 number_of_valid_coefs,
975 Word16 *frame_error_flag,
976 Word16 *decoder_mlt_coefs,
977 Word16 *old_decoder_mlt_coefs,
978 Word16 *p_mag_shift,
979 Word16 *p_old_mag_shift)
980{
981 Word16 i;
982
983 test();
984 if (*frame_error_flag != 0)
985 {
986
987 for (i = 0; i < number_of_valid_coefs; i++)
988 {
989 decoder_mlt_coefs[i] = old_decoder_mlt_coefs[i];
990 move16();
991 }
992
993 for (i = 0; i < number_of_valid_coefs; i++)
994 {
995 old_decoder_mlt_coefs[i] = 0;
996 move16();
997 }
998
999 *p_mag_shift = *p_old_mag_shift;
1000 move16();
1001
1002 *p_old_mag_shift = 0;
1003 move16();
1004 }
1005 else
1006 {
1007 /* Store in case next frame is errored. */
1008 for (i = 0; i < number_of_valid_coefs; i++)
1009 {
1010 old_decoder_mlt_coefs[i] = decoder_mlt_coefs[i];
1011 move16();
1012 }
1013
1014 *p_old_mag_shift = *p_mag_shift;
1015 move16();
1016 }
1017
1018
1019 /* Zero out the upper 1/8 of the spectrum. */
1020 for (i = number_of_valid_coefs; i < number_of_coefs; i++)
1021 {
1022 decoder_mlt_coefs[i] = 0;
1023 move16();
1024 }
1025
1026}
1027/****************************************************************************************
1028 Function: get_next_bit
1029
1030 Syntax: void get_next_bit(Bit_Obj *bitobj)
1031
1032 Description: Returns the next bit in the current word inside the bit object
1033
1034 WMOPS: 7kHz | 24kbit | 32kbit
1035 -------|--------------|----------------
1036 AVG | 0.00 | 0.00
1037 -------|--------------|----------------
1038 MAX | 0.00 | 0.00
1039 -------|--------------|----------------
1040
1041 14kHz | 24kbit | 32kbit | 48kbit
1042 -------|--------------|----------------|----------------
1043 AVG | 0.00 | 0.00 | 0.00
1044 -------|--------------|----------------|----------------
1045 MAX | 0.00 | 0.00 | 0.00
1046 -------|--------------|----------------|----------------
1047
1048****************************************************************************************/
1049void get_next_bit(Bit_Obj *bitobj)
1050{
1051 Word16 temp;
1052
1053 test();
1054 if (bitobj->code_bit_count == 0)
1055 {
1056 bitobj->current_word = *bitobj->code_word_ptr++;
1057 move16();
1058 bitobj->code_bit_count = 16;
1059 move16();
1060 }
1061 bitobj->code_bit_count = sub(bitobj->code_bit_count,1);
1062 temp = shr(bitobj->current_word,bitobj->code_bit_count);
1063 logic16();
1064 bitobj->next_bit = (Word16 )(temp & 1);
1065
1066}
1067/****************************************************************************************
1068 Function: get_rand
1069
1070 Syntax: Word16 get_rand(Rand_Obj *randobj)
1071
1072 Description: Returns a random Word16 based on the seeds inside the rand object
1073
1074 WMOPS: 7kHz | 24kbit | 32kbit
1075 -------|--------------|----------------
1076 AVG | 0.00 | 0.00
1077 -------|--------------|----------------
1078 MAX | 0.00 | 0.00
1079 -------|--------------|----------------
1080
1081 14kHz | 24kbit | 32kbit | 48kbit
1082 -------|--------------|----------------|----------------
1083 AVG | 0.00 | 0.00 | 0.00
1084 -------|--------------|----------------|----------------
1085 MAX | 0.00 | 0.00 | 0.00
1086 -------|--------------|----------------|----------------
1087
1088****************************************************************************************/
1089Word16 get_rand(Rand_Obj *randobj)
1090{
1091 Word16 random_word;
1092 Word32 acca;
1093
1094 acca = L_add(randobj->seed0,randobj->seed3);
1095 random_word = extract_l(acca);
1096
1097 logic16();
1098 test();
1099 if ((random_word & 32768L) != 0)
1100 random_word = add(random_word,1);
1101
1102 randobj->seed3 = randobj->seed2;
1103 move16();
1104 randobj->seed2 = randobj->seed1;
1105 move16();
1106 randobj->seed1 = randobj->seed0;
1107 move16();
1108 randobj->seed0 = random_word;
1109 move16();
1110
1111 return(random_word);
1112}