aboutsummaryrefslogtreecommitdiff
path: root/decoder/ixheaacd_mps_calc_m1m2_common.c
blob: a1532d8492a0dca70cb1d93f36b4d481575ba4dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
/******************************************************************************
 *
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
#include "ixheaacd_type_def.h"
#include "ixheaacd_mps_struct_def.h"
#include "ixheaacd_mps_res_rom.h"
#include "ixheaacd_mps_aac_struct.h"
#include "ixheaacd_constants.h"
#include "ixheaacd_basic_ops32.h"
#include "ixheaacd_basic_ops40.h"
#include "ixheaacd_bitbuffer.h"
#include "ixheaacd_common_rom.h"
#include "ixheaacd_sbrdecsettings.h"
#include "ixheaacd_sbr_scale.h"
#include "ixheaacd_env_extr_part.h"
#include "ixheaacd_sbr_rom.h"
#include "ixheaacd_hybrid.h"
#include "ixheaacd_ps_dec.h"
#include "ixheaacd_error_standards.h"
#include "ixheaacd_mps_polyphase.h"
#include "ixheaacd_config.h"
#include "ixheaacd_qmf_dec.h"
#include "ixheaacd_mps_dec.h"
#include "ixheaacd_mps_macro_def.h"
#include "ixheaacd_mps_basic_op.h"

VOID ixheaacd_get_matrix_inversion_weights(
    WORD32 iid_lf_ls_idx, WORD32 iid_rf_rs_idx, WORD32 prediction_mode, WORD32 c1, WORD32 c2,
    WORD32 *weight1, WORD32 *weight2, ia_mps_dec_mps_tables_struct *ia_mps_dec_mps_table_ptr) {
  WORD32 temp, temp_1, temp_2;
  WORD16 qtemp;
  WORD32 w1 = ia_mps_dec_mps_table_ptr->m1_m2_table_ptr->cld_tab_2[iid_lf_ls_idx + 15];
  WORD32 w2 = ia_mps_dec_mps_table_ptr->m1_m2_table_ptr->cld_tab_2[iid_rf_rs_idx + 15];

  if (prediction_mode == 1) {
    if (abs(c1) >= ONE_IN_Q15) {
      c1 = ONE_IN_Q15;
    } else if ((c1 < MINUS_ONE_IN_Q14) && (c1 > MINUS_ONE_IN_Q15)) {
      c1 = MINUS_ONE_IN_Q15 - (c1 << 1);
    } else {
      qtemp = 15;
      temp = ixheaacd_mps_mult32(TWO_BY_THREE_Q15, c1, &qtemp, 15);
      temp = ixheaacd_mps_convert_to_qn(temp, qtemp, 15);
      c1 = ONE_BY_THREE_Q15 + temp;
    }

    if (abs(c2) >= ONE_IN_Q15) {
      c2 = ONE_IN_Q15;
    } else if ((c2 < MINUS_ONE_IN_Q14) && (c2 > MINUS_ONE_IN_Q15)) {
      c2 = MINUS_ONE_IN_Q15 - (c2 << 1);
    } else {
      qtemp = 15;
      temp = ixheaacd_mps_mult32(TWO_BY_THREE_Q15, c2, &qtemp, 15);
      temp = ixheaacd_mps_convert_to_qn(temp, qtemp, 15);
      qtemp = 15;
      temp = ixheaacd_mps_add32(temp, ONE_BY_THREE_Q15, &qtemp, 15);
      c2 = ixheaacd_mps_convert_to_qn(temp, qtemp, 15);
    }
  } else {
    WORD32 c1p, c2p;
    WORD64 acc;
    const WORD32 *cld_tab_3 = ia_mps_dec_mps_table_ptr->m1_m2_table_ptr->cld_tab_3;
    const WORD32 *sqrt_tab = ia_mps_dec_mps_table_ptr->common_table_ptr->sqrt_tab;

    c1p = cld_tab_3[c1 + 15];
    c2p = cld_tab_3[c2 + 15];

    acc = (WORD64)((WORD64)c1p * (WORD64)c2p);
    acc >>= 15;
    temp = (WORD32)acc;
    temp_1 = (ONE_IN_Q15 + c2p) << 1;
    acc += temp_1;
    temp_2 = (WORD32)acc;

    temp = ixheaacd_mps_div_32(temp, temp_2, &qtemp);

    c1 = ixheaacd_mps_sqrt(temp, &qtemp, sqrt_tab);
    c1 = ixheaacd_mps_convert_to_qn(c1, qtemp, 15);

    temp_2 = c1p + temp_1;
    temp = ixheaacd_mps_div_32(c1p, temp_2, &qtemp);
    c2 = ixheaacd_mps_sqrt(temp, &qtemp, sqrt_tab);
    c2 = ixheaacd_mps_convert_to_qn(c2, qtemp, 15);
  }
  temp_1 = ONE_IN_Q15 + w1;
  temp_2 = ixheaacd_mps_mult32_shr_15(c1, w1);
  *weight1 = ixheaacd_mps_div32_in_q15(temp_2, temp_1);

  temp_1 = ONE_IN_Q15 + w2;
  temp_2 = ixheaacd_mps_mult32_shr_15(c2, w2);
  *weight2 = ixheaacd_mps_div32_in_q15(temp_2, temp_1);
}

VOID ixheaacd_invert_matrix(WORD32 weight1, WORD32 weight2, WORD32 h_real[][2],
                            WORD32 h_imag[][2],
                            const ia_mps_dec_common_tables_struct *common_tab_ptr) {
  WORD32 h11_f_real, h12_f_real, h21_f_real, h22_f_real;
  WORD32 h11_f_imag, h12_f_imag, h21_f_imag, h22_f_imag;

  WORD32 inv_norm_real, inv_norm_imag, inv_norm;

  WORD32 len1, len2;
  WORD16 q_len1 = 0, q_len2 = 0;

  WORD64 acc1, acc2;

  len1 = ixheaacd_mps_sqrt((ONE_IN_Q15 - (weight1 << 1) + ((weight1 * weight1) >> 14)), &q_len1,
                           common_tab_ptr->sqrt_tab);

  len2 = ixheaacd_mps_sqrt((ONE_IN_Q15 - (weight2 << 1) + ((weight2 * weight2) >> 14)), &q_len2,
                           common_tab_ptr->sqrt_tab);

  len1 = ixheaacd_mps_convert_to_qn(len1, q_len1, 15);
  len2 = ixheaacd_mps_convert_to_qn(len2, q_len2, 15);

  h11_f_real = ixheaacd_mps_div32_in_q15((ONE_IN_Q15 - weight1), len1);

  h11_f_imag = ixheaacd_mps_div32_in_q15(weight1, len1);

  h22_f_imag = -(ixheaacd_mps_div32_in_q15(weight2, len2));

  h12_f_real = 0;

  h12_f_imag = ixheaacd_mps_mult32_shr_15(h22_f_imag, ONE_BY_SQRT_3_Q15);

  h21_f_real = 0;

  h21_f_imag = ixheaacd_mps_mult32_shr_15(h11_f_imag, -(ONE_BY_SQRT_3_Q15));

  h22_f_real = ixheaacd_mps_div32_in_q15((ONE_IN_Q15 - weight2), len2);

  acc1 =
      (WORD64)((WORD64)h11_f_real * (WORD64)h22_f_real - (WORD64)h11_f_imag * (WORD64)h22_f_imag);
  acc1 >>= 15;

  acc2 =
      (WORD64)((WORD64)h12_f_real * (WORD64)h21_f_real - (WORD64)h12_f_imag * (WORD64)h21_f_imag);
  acc2 >>= 15;
  inv_norm_real = (WORD32)(acc1 - acc2);

  acc1 =
      (WORD64)((WORD64)h11_f_real * (WORD64)h22_f_imag + (WORD64)h11_f_imag * (WORD64)h22_f_real);
  acc1 >>= 15;

  acc2 =
      (WORD64)((WORD64)h12_f_real * (WORD64)h21_f_imag + (WORD64)h12_f_imag * (WORD64)h21_f_real);
  acc2 >>= 15;
  inv_norm_imag = (WORD32)(acc1 + acc2);

  acc1 = (WORD64)((WORD64)inv_norm_real * (WORD64)inv_norm_real +
                  (WORD64)inv_norm_imag * (WORD64)inv_norm_imag);
  acc1 >>= 15;
  inv_norm = (WORD32)acc1;

  inv_norm_real = ixheaacd_mps_div32_in_q15(inv_norm_real, inv_norm);
  inv_norm_imag = -(ixheaacd_mps_div32_in_q15(inv_norm_imag, inv_norm));

  acc1 = (WORD64)((WORD64)h22_f_real * (WORD64)inv_norm_real -
                  (WORD64)h22_f_imag * (WORD64)inv_norm_imag);
  acc1 >>= 15;
  h_real[0][0] = (WORD32)acc1;

  acc1 = (WORD64)((WORD64)h22_f_real * (WORD64)inv_norm_imag +
                  (WORD64)h22_f_imag * (WORD64)inv_norm_real);
  acc1 >>= 15;
  h_imag[0][0] = (WORD32)acc1;

  acc1 = (WORD64)((WORD64)h12_f_imag * (WORD64)inv_norm_imag -
                  (WORD64)h12_f_real * (WORD64)inv_norm_real);
  acc1 >>= 15;
  h_real[0][1] = (WORD32)acc1;

  acc1 = (WORD64)((WORD64)h12_f_real * (WORD64)inv_norm_imag +
                  (WORD64)h12_f_imag * (WORD64)inv_norm_real);
  acc1 = -(acc1 >> 15);
  h_imag[0][1] = (WORD32)acc1;

  acc1 = (WORD64)((WORD64)h21_f_imag * (WORD64)inv_norm_imag -
                  (WORD64)h21_f_real * (WORD64)inv_norm_real);
  acc1 >>= 15;
  h_real[1][0] = (WORD32)acc1;

  acc1 = (WORD64)((WORD64)h21_f_real * (WORD64)inv_norm_imag +
                  (WORD64)h21_f_imag * (WORD64)inv_norm_real);
  acc1 = -(acc1 >> 15);
  h_imag[1][0] = (WORD32)acc1;

  acc1 = (WORD64)((WORD64)h11_f_real * (WORD64)inv_norm_real -
                  (WORD64)h11_f_imag * (WORD64)inv_norm_imag);
  acc1 >>= 15;
  h_real[1][1] = (WORD32)acc1;

  acc1 = (WORD64)((WORD64)h11_f_real * (WORD64)inv_norm_imag +
                  (WORD64)h11_f_imag * (WORD64)inv_norm_real);
  acc1 >>= 15;
  h_imag[1][1] = (WORD32)acc1;
}

WORD32 ixheaacd_dequant_icc_band(WORD32 iccband, WORD32 cldband) {
  if (iccband < 6) {
    return iccband;
  }
  if (iccband == 6) {
    if (cldband > 9 && cldband < 21) {
      switch (cldband) {
        case 10:
        case 20:
          return 10;
        case 11:
        case 19:
          return 11;
        case 12:
        case 18:
          return 12;
        case 13:
        case 17:
          return 13;
        case 14:
        case 16:
          return 14;
        case 15:
          return 15;
        default:
          return iccband;
      }
    } else {
      return iccband;
    }
  }
  if (7 == iccband) {
    if (cldband > 7 && cldband < 23) {
      switch (cldband) {
        case 8:
        case 22:
          return 8;
        case 9:
        case 21:
          return 9;
        case 10:
        case 20:
          return 10;
        case 11:
        case 19:
          return 11;
        case 12:
        case 18:
          return 12;
        case 13:
        case 17:
          return 13;
        case 14:
        case 16:
          return 14;
        case 15:
          return 15;
        default:
          return iccband;
      }
    } else {
      return iccband;
    }
  } else {
    return iccband;
  }
}

WORD32 ixheaacd_dequant_cld_band(WORD32 cld) {
  switch (cld) {
    case -4915200:
      return 0;
    case -1474560:
      return 1;
    case -1310720:
      return 2;
    case -1146880:
      return 3;
    case -983040:
      return 4;
    case -819200:
      return 5;
    case -720896:
      return 6;
    case -622592:
      return 7;
    case -524288:
      return 8;
    case -425984:
      return 9;
    case -327680:
      return 10;
    case -262144:
      return 11;
    case -196608:
      return 12;
    case -131072:
      return 13;
    case -65536:
      return 14;
    case 0:
      return 15;
    case 65536:
      return 16;
    case 131072:
      return 17;
    case 196608:
      return 18;
    case 262144:
      return 19;
    case 327680:
      return 20;
    case 425984:
      return 21;
    case 524288:
      return 22;
    case 622592:
      return 23;
    case 720896:
      return 24;
    case 819200:
      return 25;
    case 983040:
      return 26;
    case 1146880:
      return 27;
    case 1310720:
      return 28;
    case 1474560:
      return 29;
    case 4915200:
      return 30;
    default:
      return 0;
  }
}

VOID ixheaacd_param_2_umx_ps_core_tables(
    WORD32 cld[MAX_PARAMETER_BANDS], WORD32 icc[MAX_PARAMETER_BANDS], WORD32 num_ott_bands,
    WORD32 res_bands, WORD32 h11[MAX_PARAMETER_BANDS], WORD32 h12[MAX_PARAMETER_BANDS],
    WORD32 h21[MAX_PARAMETER_BANDS], WORD32 h22[MAX_PARAMETER_BANDS],
    WORD32 h12_res[MAX_PARAMETER_BANDS], WORD32 h22_res[MAX_PARAMETER_BANDS],
    WORD16 c_l[MAX_PARAMETER_BANDS], WORD16 c_r[MAX_PARAMETER_BANDS],
    const ia_mps_dec_m1_m2_tables_struct *ixheaacd_mps_dec_m1_m2_tables) {
  WORD32 band;
  WORD32 quant_band_cld, quant_band_icc;

  for (band = 0; band < num_ott_bands; band++) {
    quant_band_cld = ixheaacd_dequant_cld_band(cld[band]);

    c_l[band] = (WORD16)ixheaacd_mps_dec_m1_m2_tables->c_l_table[quant_band_cld];
    c_r[band] = (WORD16)ixheaacd_mps_dec_m1_m2_tables->c_l_table[30 - quant_band_cld];
  }

  for (band = 0; band < num_ott_bands; band++) {
    if (band < res_bands) {
      quant_band_cld = ixheaacd_dequant_cld_band(cld[band]);
      quant_band_icc = ixheaacd_dequant_icc_band(icc[band], quant_band_cld);

      h11[band] = ixheaacd_mps_dec_m1_m2_tables->cos_table[quant_band_icc][quant_band_cld];
      h11[band] = ixheaacd_mps_mult32_shr_15(h11[band], c_l[band]);
      h21[band] = ixheaacd_mps_dec_m1_m2_tables->cos_table[quant_band_icc][30 - quant_band_cld];
      h21[band] = ixheaacd_mps_mult32_shr_15(h21[band], c_r[band]);

      h12[band] = 0;
      h22[band] = 0;
      h12_res[band] = ONE_IN_Q15;
      h22_res[band] = MINUS_ONE_IN_Q15;
    } else {
      quant_band_cld = ixheaacd_dequant_cld_band(cld[band]);
      if (quant_band_cld < 0 || quant_band_cld >= 31) {
        quant_band_cld = 30;
      }

      quant_band_icc = icc[band];

      if (quant_band_icc < 0 || quant_band_icc >= 8) {
        quant_band_icc = 7;
      }
      h11[band] = ixheaacd_mps_dec_m1_m2_tables->cos_table[quant_band_icc][quant_band_cld];
      h11[band] = ixheaacd_mps_mult32_shr_15(h11[band], c_l[band]);
      h21[band] = ixheaacd_mps_dec_m1_m2_tables->cos_table[quant_band_icc][30 - quant_band_cld];
      h21[band] = ixheaacd_mps_mult32_shr_15(h21[band], c_r[band]);
      h12[band] = ixheaacd_mps_dec_m1_m2_tables->sin_table[quant_band_icc][quant_band_cld];
      h12[band] = ixheaacd_mps_mult32_shr_15(h12[band], c_l[band]);
      h22[band] = -ixheaacd_mps_dec_m1_m2_tables->sin_table[quant_band_icc][30 - quant_band_cld];
      h22[band] = ixheaacd_mps_mult32_shr_15(h22[band], c_r[band]);

      h12_res[band] = 0;
      h22_res[band] = 0;
    }
  }
  return;
}

VOID ixheaacd_param_2_umx_ps(ia_heaac_mps_state_struct *pstr_mps_state, WORD32 *h11, WORD32 *h12,
                             WORD32 *h21, WORD32 *h22, WORD32 *h12_res, WORD32 *h22_res,
                             WORD16 *c_l, WORD16 *c_r, WORD32 ott_box_indx,
                             WORD32 parameter_set_indx, WORD32 res_bands) {
  WORD32 band;
  ia_mps_dec_spatial_bs_frame_struct *p_cur_bs = pstr_mps_state->bs_frame;
  ia_mps_dec_auxilary_struct *p_aux_struct = pstr_mps_state->aux_struct;
  WORD32 num_parameter_bands = pstr_mps_state->num_parameter_bands;

  ixheaacd_param_2_umx_ps_core_tables(p_aux_struct->ott_cld[ott_box_indx][parameter_set_indx],
                                      p_cur_bs->ott_icc_idx[ott_box_indx][parameter_set_indx],
                                      p_aux_struct->num_ott_bands[ott_box_indx], res_bands, h11,
                                      h12, h21, h22, h12_res, h22_res, c_l, c_r,
                                      pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr);

  for (band = p_aux_struct->num_ott_bands[ott_box_indx]; band < num_parameter_bands; band++) {
    h11[band] = h21[band] = h12[band] = h22[band] = h12_res[band] = h22_res[band] = 0;
  }
  return;
}

static WORD32 ixheaacd_dequant_one_by_icc(WORD32 icc) {
  switch (icc) {
    case 32768:
      return 32768;
    case 30704:
      return 34971;
    case 27564:
      return 38955;
    case 19691:
      return 54530;
    case 12047:
      return 89131;
    case 0:
      return 0;
    case -19300:
      return -55633;
    case -32440:
      return -33099;
    default:
      return 0;
  }
}

static WORD16 ixheaacd_map_cld_index(WORD32 cld_val) {
  WORD32 temp = cld_val;
  WORD16 idx = 0;
  if (cld_val == 0) {
    return 15;
  } else {
    if (cld_val < 0) {
      temp = -cld_val;
    }
    switch (temp) {
      case 150:
        idx = 15;
        break;
      case 45:
        idx = 14;
        break;
      case 40:
        idx = 13;
        break;
      case 35:
        idx = 12;
        break;
      case 30:
        idx = 11;
        break;
      case 25:
        idx = 10;
        break;
      case 22:
        idx = 9;
        break;
      case 19:
        idx = 8;
        break;
      case 16:
        idx = 7;
        break;
      case 13:
        idx = 6;
        break;
      case 10:
        idx = 5;
        break;
      case 8:
        idx = 4;
        break;
      case 6:
        idx = 3;
        break;
      case 4:
        idx = 2;
        break;
      case 2:
        idx = 1;
        break;
      default:
        idx = 0;
        break;
    }
  }

  return (cld_val >= 0) ? idx + 15 : 15 - idx;
}

VOID ixheaacd_calculate_ttt(ia_heaac_mps_state_struct *pstr_mps_state, WORD32 ps, WORD32 pb,
                            WORD32 ttt_mode, WORD32 m_ttt[][3]) {
  ia_mps_dec_auxilary_struct *p_aux_struct = pstr_mps_state->aux_struct;
  WORD32 col;

  if (ttt_mode < 2) {
    m_ttt[0][0] = (p_aux_struct->ttt_cpc_1[0][ps][pb] + ONE_IN_Q16);
    m_ttt[0][1] = (p_aux_struct->ttt_cpc_2[0][ps][pb] - ONE_IN_Q15);
    m_ttt[1][0] = (p_aux_struct->ttt_cpc_1[0][ps][pb] - ONE_IN_Q15);
    m_ttt[1][1] = (p_aux_struct->ttt_cpc_2[0][ps][pb] + ONE_IN_Q16);
    m_ttt[2][0] = (ONE_IN_Q15 - p_aux_struct->ttt_cpc_1[0][ps][pb]);
    m_ttt[2][1] = (ONE_IN_Q15 - p_aux_struct->ttt_cpc_2[0][ps][pb]);

    if (pb >= pstr_mps_state->res_bands[3]) {
      WORD32 one_by_icc;
      one_by_icc = ixheaacd_dequant_one_by_icc(p_aux_struct->ttt_icc[0][ps][pb]);

      m_ttt[0][0] = ixheaacd_mps_mult32_shr_15(m_ttt[0][0], one_by_icc);
      m_ttt[0][1] = ixheaacd_mps_mult32_shr_15(m_ttt[0][1], one_by_icc);
      m_ttt[1][0] = ixheaacd_mps_mult32_shr_15(m_ttt[1][0], one_by_icc);
      m_ttt[1][1] = ixheaacd_mps_mult32_shr_15(m_ttt[1][1], one_by_icc);
      m_ttt[2][0] = ixheaacd_mps_mult32_shr_15(m_ttt[2][0], one_by_icc);
      m_ttt[2][1] = ixheaacd_mps_mult32_shr_15(m_ttt[2][1], one_by_icc);
    }

    m_ttt[0][0] = ixheaacd_mult32x16in32(m_ttt[0][0], TWO_BY_THREE_Q15);
    m_ttt[0][1] = ixheaacd_mult32x16in32(m_ttt[0][1], TWO_BY_THREE_Q15);
    m_ttt[1][0] = ixheaacd_mult32x16in32(m_ttt[1][0], TWO_BY_THREE_Q15);
    m_ttt[1][1] = ixheaacd_mult32x16in32(m_ttt[1][1], TWO_BY_THREE_Q15);
    m_ttt[2][0] = ixheaacd_mult32x16in32(m_ttt[2][0], TWO_BY_THREE_Q15);
    m_ttt[2][1] = ixheaacd_mult32x16in32(m_ttt[2][1], TWO_BY_THREE_Q15);
  } else {
    WORD32 center_wiener;
    WORD32 center_subtraction;
    WORD32 c1d, c2d;
    WORD64 prod;
    WORD32 w11, w00, w20, w21;
    WORD16 q_w11, q_w00, q_w20, q_w21;

    const WORD32 *ten_cld_by_10 =
        pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr->ten_cld_by_10;

    ia_mps_dec_spatial_bs_frame_struct *p_cur_bs = pstr_mps_state->bs_frame;
    WORD16 index = ixheaacd_map_cld_index(p_aux_struct->ttt_cld_1[0][ps][pb] >> 15);

    c1d = ten_cld_by_10[index];

    index = ixheaacd_map_cld_index(p_aux_struct->ttt_cld_2[0][ps][pb] >> 15);
    c2d = ten_cld_by_10[index];

    if (p_cur_bs->cmp_ttt_cld_1_idx[0][ps][pb] == 15 ||
        p_cur_bs->cmp_ttt_cld_2_idx[0][ps][pb] == 15) {
      if (p_cur_bs->cmp_ttt_cld_1_idx[0][ps][pb] == 15) {
        if (p_cur_bs->cmp_ttt_cld_2_idx[0][ps][pb] == -15) {
          w00 = ONE_BY_SQRT_2_Q15;
          w20 = ONE_BY_SQRT_8_Q15;
        } else {
          w00 = ONE_IN_Q15;
          w20 = 0;
        }

        if (p_cur_bs->cmp_ttt_cld_2_idx[0][ps][pb] == 15) {
          w11 = ONE_BY_SQRT_2_Q15;
          w21 = ONE_BY_SQRT_8_Q15;
        } else {
          w11 = ONE_IN_Q15;
          w21 = 0;
        }

        m_ttt[0][0] = w00;
        m_ttt[2][0] = w20;
        m_ttt[2][1] = w21;
        m_ttt[1][1] = w11;
      }

      if (p_cur_bs->cmp_ttt_cld_2_idx[0][ps][pb] == 15) {
        const WORD32 *w00_cld2_15 =
            pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr->w00_cld2_15;

        if (p_cur_bs->cmp_ttt_cld_1_idx[0][ps][pb] == 15) {
          w11 = ONE_BY_SQRT_2_Q15;
          w21 = ONE_BY_SQRT_8_Q15;
        } else {
          w11 = 0;
          w21 = ONE_IN_Q14;
        }

        w00 = w00_cld2_15[p_cur_bs->cmp_ttt_cld_1_idx[0][ps][pb] + 15];
        w20 = w00_cld2_15[15 - p_cur_bs->cmp_ttt_cld_1_idx[0][ps][pb]] / 2;
        m_ttt[0][0] = w00;
        m_ttt[2][0] = w20;
        m_ttt[2][1] = w21;
        m_ttt[1][1] = w11;
      }

      m_ttt[0][1] = 0;
      m_ttt[1][0] = 0;
    } else {
      WORD32 temporary;
      const WORD32 *sqrt_tab = pstr_mps_state->ia_mps_dec_mps_table.common_table_ptr->sqrt_tab;
      prod = ixheaacd_mps_mult32_shr_15(c1d, c2d);

      temporary = ixheaacd_add32_sat(ONE_IN_Q15, c2d);
      temporary = ixheaacd_add32_sat(temporary, (WORD32)prod);
      w00 = ixheaacd_mps_div_32((WORD32)prod, temporary, &q_w00);

      w11 = ixheaacd_mps_div_32(c1d, (ixheaacd_add32_sat3(c1d, c2d, ONE_IN_Q15)), &q_w11);

      w20 = ixheaacd_mps_div_32((ixheaacd_add32_sat(c2d, ONE_IN_Q15)),
                                ixheaacd_add32_sat3(ONE_IN_Q15, (WORD32)prod, c2d), &q_w20);

      w21 = ixheaacd_mps_div_32(ixheaacd_add32_sat(c2d, ONE_IN_Q15),
                                (ixheaacd_add32_sat3(c1d, c2d, ONE_IN_Q15)), &q_w21);

      m_ttt[0][0] = ixheaacd_mps_sqrt(w00, &q_w00, sqrt_tab);
      m_ttt[0][0] = ixheaacd_mps_convert_to_qn(m_ttt[0][0], q_w00, 15);

      m_ttt[0][1] = 0;
      m_ttt[1][0] = 0;

      m_ttt[1][1] = ixheaacd_mps_sqrt(w11, &q_w11, sqrt_tab);
      m_ttt[1][1] = ixheaacd_mps_convert_to_qn(m_ttt[1][1], q_w11, 15);

      m_ttt[2][0] = ixheaacd_mps_sqrt(w20, &q_w20, sqrt_tab) >> 1;

      m_ttt[2][0] = ixheaacd_mps_convert_to_qn(m_ttt[2][0], q_w20, 15);

      m_ttt[2][1] = ixheaacd_mps_sqrt(w21, &q_w21, sqrt_tab) >> 1;

      m_ttt[2][1] = ixheaacd_mps_convert_to_qn(m_ttt[2][1], q_w21, 15);
      if (p_aux_struct->ttt_cld_1[0][ps][pb] == 4915200) {
        m_ttt[0][0] = 32767;
        m_ttt[1][1] = 32767;
        m_ttt[2][0] = 0;
        m_ttt[2][1] = 0;
      }
    }

    center_wiener = 0;
    center_subtraction = (ttt_mode == 2 || ttt_mode == 3);

    if (center_wiener) {
      WORD32 cld_1_idx = p_cur_bs->cmp_ttt_cld_1_idx[0][ps][pb];
      WORD32 cld_2_idx = p_cur_bs->cmp_ttt_cld_2_idx[0][ps][pb];

      if (cld_1_idx == 15 && cld_2_idx == 15) {
        m_ttt[2][0] = 0;
        m_ttt[2][1] = ONE_BY_SQRT_2_Q15;
      } else if (cld_1_idx == 15) {
        if (cld_2_idx == -15)
          m_ttt[2][0] = ONE_BY_SQRT_2_Q15;
        else
          m_ttt[2][0] = 0;
        m_ttt[2][1] = 0;
      } else if (cld_2_idx == 15) {
        m_ttt[2][0] = 0;
        m_ttt[2][1] = ONE_IN_Q15;
      } else {
        WORD32 temp;
        WORD16 q_temp;
        const WORD32 *sqrt_tab = pstr_mps_state->ia_mps_dec_mps_table.common_table_ptr->sqrt_tab;

        prod = ixheaacd_mps_mult32_shr_15(c2d, (c2d + c1d + ONE_IN_Q16)) + ONE_IN_Q15;

        temp = ixheaacd_mps_div_32((WORD32)ONE_IN_Q15, (WORD32)prod, &q_temp);

        m_ttt[2][0] = ixheaacd_mps_sqrt(temp, &q_temp, sqrt_tab);
        m_ttt[2][0] = ixheaacd_mps_convert_to_qn(m_ttt[2][0], q_temp, 15);

        m_ttt[2][1] = ixheaacd_mps_mult32_shr_15(c2d, m_ttt[2][0]);
      }
    }

    if (center_subtraction) {
      WORD32 wl1, wl2, wr1, wr2;
      WORD16 q_wl1, q_wr1;
      WORD32 cld_1_idx = p_cur_bs->cmp_ttt_cld_1_idx[0][ps][pb];
      WORD32 cld_2_idx = p_cur_bs->cmp_ttt_cld_2_idx[0][ps][pb];

      if (cld_1_idx == 15 && cld_2_idx == 15) {
        m_ttt[0][0] = ONE_IN_Q15;
        m_ttt[0][1] = MINUS_ONE_IN_Q14;
        m_ttt[1][1] = ONE_BY_SQRT_2_Q15;
        m_ttt[1][0] = 0;
      } else if (cld_1_idx == 15) {
        if (cld_2_idx == -15) {
          m_ttt[0][0] = ONE_BY_SQRT_2_Q15;
          m_ttt[1][0] = MINUS_ONE_IN_Q14;
        } else {
          m_ttt[0][0] = ONE_IN_Q15;
          m_ttt[1][0] = 0;
        }

        m_ttt[0][1] = 0;
        m_ttt[1][1] = ONE_IN_Q15;
      } else if (cld_2_idx == 15) {
        m_ttt[0][0] = ONE_IN_Q15;
        m_ttt[0][1] = MINUS_ONE_IN_Q15;
        m_ttt[1][1] = 0;
        m_ttt[1][0] = 0;
      } else {
        WORD32 temp, temp_1, q_a;
        WORD16 q_c, q_l, q_r, q_temp, q_temp1;
        WORD32 c;
        WORD32 r;
        WORD32 l;
        const WORD32 *sqrt_tab = pstr_mps_state->ia_mps_dec_mps_table.common_table_ptr->sqrt_tab;

        c = ixheaacd_mps_div_32(ONE_IN_Q15, (ixheaacd_add32_sat(c1d, ONE_IN_Q15)), &q_c);
        r = ixheaacd_mps_div_32(c1d, (ixheaacd_add32_sat(c2d, ONE_IN_Q15)), &q_r);
        r = ixheaacd_mps_mult32_shr_30(r, c);
        q_r = q_r + q_c - 30;

        l = ixheaacd_mps_mult32_shr_30(c2d, r);
        q_l = q_r - 15;

        temp = ixheaacd_mps_div_32(r, l, &q_temp);
        q_temp += (q_r - q_l);

        if (q_temp > 28) {
          temp = temp >> (q_temp - 28);
          q_temp = 28;
        }

        temp += ((1 << q_temp) - 1);

        temp = ixheaacd_add32_sat(
            ixheaacd_mps_mult32_shr_n(c, temp, (WORD16)(q_c + q_temp - q_r)), r);
        q_temp = q_r;

        if (q_c > q_r) {
          temp_1 = r + (c >> (q_c - q_r));
          q_temp1 = q_r;
        } else {
          temp_1 = (r >> (q_r - q_c)) + c;
          q_temp1 = q_c;
        }

        temp = ixheaacd_div32(temp_1, temp, &q_a);
        q_wl1 = q_a + q_temp1 - q_temp;
        wl1 = ixheaacd_mps_sqrt(temp, &q_wl1, sqrt_tab);
        m_ttt[0][0] = ixheaacd_mps_convert_to_qn(wl1, q_wl1, 15);

        temp = ixheaacd_div32(wl1, temp_1, &q_a);
        q_temp = q_a + (q_wl1 - q_temp1);
        wl2 = ixheaacd_mps_mult32_shr_n(c, temp, (WORD16)(q_c + q_temp - 15));
        m_ttt[0][1] = ixheaacd_negate32_sat(wl2);

        temp = ixheaacd_mps_div_32(l, r, &q_temp);
        q_temp += (q_l - q_r);

        if (q_temp > 28) {
          temp = temp >> (q_temp - 28);
          q_temp = 28;
        }

        temp = ixheaacd_add32_sat((1 << q_temp) - 1, temp);

        temp = ixheaacd_add32_sat(
                   ixheaacd_mps_mult32_shr_n(c, temp, (WORD16)(q_c + q_temp - q_l)), l);

        q_temp = q_l;

        if (q_c > q_l) {
          temp_1 = l + (c >> (q_c - q_l));
          q_temp1 = q_l;
        } else {
          temp_1 = (l >> (q_l - q_c)) + c;
          q_temp1 = q_c;
        }

        temp = ixheaacd_div32(temp_1, temp, &q_a);
        q_wr1 = q_a + q_temp1 - q_temp;
        wr1 = ixheaacd_mps_sqrt(temp, &q_wr1, sqrt_tab);
        m_ttt[1][1] = ixheaacd_mps_convert_to_qn(wr1, q_wr1, 15);

        temp = ixheaacd_div32(wr1, temp_1, &q_a);
        q_temp = q_a + (q_wl1 - q_temp1);
        wr2 = ixheaacd_mps_mult32_shr_n(c, temp, (WORD16)(q_c + q_temp - 15));
        m_ttt[1][0] = ixheaacd_negate32_sat(wr2);
      }
    }
  }

  m_ttt[0][2] = ONE_BY_THREE_Q15;
  m_ttt[1][2] = ONE_BY_THREE_Q15;
  m_ttt[2][2] = MINUS_ONE_BY_THREE_Q15;

  for (col = 0; col < 3; col++) {
    m_ttt[2][col] = ixheaacd_mps_mult32_shr_15(m_ttt[2][col], SQRT_TWO_Q15);
  }
}

VOID ixheaacd_calculate_mtx_inv(ia_heaac_mps_state_struct *pstr_mps_state, WORD32 ps, WORD32 pb,
                                WORD32 mode, WORD32 h_real[][2], WORD32 h_imag[][2]) {
  ia_mps_dec_auxilary_struct *p_aux_struct = pstr_mps_state->aux_struct;

  WORD32 weight1;
  WORD32 weight2;
  ia_mps_dec_spatial_bs_frame_struct *p_cur_bs = pstr_mps_state->bs_frame;

  if (mode < 2) {
    ixheaacd_get_matrix_inversion_weights(
        p_cur_bs->ott_cld_idx[1][ps][pb], p_cur_bs->ott_cld_idx[2][ps][pb], 1,
        p_aux_struct->ttt_cpc_1[0][ps][pb], p_aux_struct->ttt_cpc_2[0][ps][pb], &weight1,
        &weight2, &(pstr_mps_state->ia_mps_dec_mps_table));
  } else {
    ixheaacd_get_matrix_inversion_weights(
        p_cur_bs->ott_cld_idx[1][ps][pb], p_cur_bs->ott_cld_idx[2][ps][pb], 0,
        p_cur_bs->cmp_ttt_cld_1_idx[0][ps][pb], p_cur_bs->cmp_ttt_cld_2_idx[0][ps][pb], &weight1,
        &weight2, &(pstr_mps_state->ia_mps_dec_mps_table));
  }

  ixheaacd_invert_matrix(weight1, weight2, h_real, h_imag,
                         pstr_mps_state->ia_mps_dec_mps_table.common_table_ptr);
}

VOID ixheaacd_calculate_arb_dmx_mtx(ia_heaac_mps_state_struct *pstr_mps_state, WORD32 ps,
                                    WORD32 pb, WORD32 g_real[]) {
  WORD32 ch;
  WORD32 gain;

  WORD32 *arbdmx_alpha_prev = pstr_mps_state->mps_persistent_mem.arbdmx_alpha_prev;
  WORD32 *arbdmx_alpha_upd_set = pstr_mps_state->aux_struct->arbdmx_alpha_upd_set;
  WORD32 *arbdmx_alpha = pstr_mps_state->aux_struct->arbdmx_alpha;

  WORD32 n_ch_in = pstr_mps_state->num_input_channels;
  WORD32 temp_1;
  for (ch = 0; ch < n_ch_in; ch++) {
    temp_1 = pstr_mps_state->bs_frame->arbdmx_gain_idx[ch][ps][pb] + 15;
    gain = pstr_mps_state->ia_mps_dec_mps_table.m1_m2_table_ptr->dec_pow[temp_1];

    if (pb < pstr_mps_state->arbdmx_residual_bands) {
      if ((ps == 0) && (arbdmx_alpha_upd_set[ch] == 1)) {
        g_real[ch] = ixheaacd_mps_mult32_shr_15(*arbdmx_alpha_prev, gain);
      } else {
        g_real[ch] = ixheaacd_mps_mult32_shr_15(arbdmx_alpha[ch], gain);
      }
    } else {
      g_real[ch] = gain;
    }
    arbdmx_alpha_prev++;
  }
}

WORD32 ixheaacd_quantize(WORD32 cld) {
  switch (cld) {
    case -150:
      return -15;
    case -45:
      return -14;
    case -40:
      return -13;
    case -35:
      return -12;
    case -30:
      return -11;
    case -25:
      return -10;
    case -22:
      return -9;
    case -19:
      return -8;
    case -16:
      return -7;
    case -13:
      return -6;
    case -10:
      return -5;
    case -8:
      return -4;
    case -6:
      return -3;
    case -4:
      return -2;
    case -2:
      return -1;
    case 0:
      return 0;
    case 2:
      return 1;
    case 4:
      return 2;
    case 6:
      return 3;
    case 8:
      return 4;
    case 10:
      return 5;
    case 13:
      return 6;
    case 16:
      return 7;
    case 19:
      return 8;
    case 22:
      return 9;
    case 25:
      return 10;
    case 30:
      return 11;
    case 35:
      return 12;
    case 40:
      return 13;
    case 45:
      return 14;
    case 150:
      return 15;
    default:
      return 0;
  }
}