1 /* this file is a part of amp software, (C) tomislav uzelac 1996,1997
2 */
3 
4 /* misc2.c  requantization, stereo processing, reordering(shortbl) and antialiasing butterflies
5  *
6  * misc.c was created by tomislav uzelac in May 1996, and was completely awful
7  * Created by: tomislav uzelac Dec 22 1996
8  * some more speed injected, cca. Jun 1 1997
9  */
10 #include <math.h>
11 
12 #include "amp.h"
13 #include "audio.h"
14 #include "getdata.h"
15 #include "huffman.h"
16 
17 #define MISC2
18 #include "misc2.h"
19 
20 int no_of_imdcts[2];
21 
22 static const int t_pretab[22]={0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0};
23 static const float t_is[7]={ 1.0, 0.788675134596, 0.633974596215,
24 		 0.5, 0.366025403784, 0.211324865405, 0.0};
25 static const float t_is2[2][32]={
26 {             1,       0.840896,       0.707107,       0.594604,            0.5,       0.420448,
27        0.353553,       0.297302,           0.25,       0.210224,       0.176777,       0.148651,
28           0.125,       0.105112,      0.0883883,      0.0743254},
29 {             1,       0.707107,            0.5,       0.353553,           0.25,       0.176777,
30           0.125,      0.0883883,         0.0625,      0.0441942,        0.03125,      0.0220971,
31        0.015625,      0.0110485,      0.0078125,     0.00552427}
32 };
33 
34 static const float t_downmix[2][32]={
35 { 1.000000,   0.920448,   0.853554,   0.797302,   0.750000,   0.710224,
36   0.676776,   0.648651,   0.625000,   0.605112,   0.588389,   0.574326,
37   0.562500,   0.552556,   0.544194,   0.537163},
38 { 1.000000,   0.853554,   0.750000,   0.676776,   0.625000,   0.588389,
39   0.562500,   0.544194,   0.531250,   0.522097,   0.515625,   0.511049,
40   0.507813,   0.505524,   0.503906,   0.502762}
41 };
42 
43 static const float Cs[8]={0.857492925712, 0.881741997318, 0.949628649103,
44 	      0.983314592492, 0.995517816065, 0.999160558175,
45 	      0.999899195243, 0.999993155067};
46 static const float Ca[8]={-0.5144957554270, -0.4717319685650, -0.3133774542040,
47 	      -0.1819131996110, -0.0945741925262, -0.0409655828852,
48 	      -0.0141985685725,	-0.00369997467375};
49 static const float tab[4]={1,1.189207115,1.414213562,1.6817928301};
50 static const float tabi[4]={1,0.840896415,0.707106781,0.594603557};
51 
52 static float t_43[8192];
53 
54 
55 /* leftmost index denotes header->ID, so first three are for MPEG2
56  * and the others are for MPEG1
57  */
58 static const short t_reorder[2][3][576]={{
59 {  0,   1,   2,   3,   6,   7,   8,   9,  12,  13,  14,  15,   4,   5,  18,  19,  10,  11,  24,  25,
60   16,  17,  30,  31,  20,  21,  22,  23,  26,  27,  28,  29,  32,  33,  34,  35,  36,  37,  38,  39,
61   40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,
62   60,  61,  62,  63,  64,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  90,  91,
63   78,  79,  80,  81,  82,  83,  96,  97,  84,  85,  86,  87,  88,  89, 102, 103,  92,  93,  94,  95,
64  108, 109, 110, 111, 112, 113,  98,  99, 100, 101, 114, 115, 116, 117, 118, 119, 104, 105, 106, 107,
65  120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 144, 145, 146, 147, 148, 149, 162, 163,
66  132, 133, 134, 135, 136, 137, 150, 151, 152, 153, 154, 155, 168, 169, 138, 139, 140, 141, 142, 143,
67  156, 157, 158, 159, 160, 161, 174, 175, 164, 165, 166, 167, 180, 181, 182, 183, 184, 185, 198, 199,
68  200, 201, 202, 203, 216, 217, 170, 171, 172, 173, 186, 187, 188, 189, 190, 191, 204, 205, 206, 207,
69  208, 209, 222, 223, 176, 177, 178, 179, 192, 193, 194, 195, 196, 197, 210, 211, 212, 213, 214, 215,
70  228, 229, 218, 219, 220, 221, 234, 235, 236, 237, 238, 239, 252, 253, 254, 255, 256, 257, 270, 271,
71  272, 273, 274, 275, 288, 289, 290, 291, 224, 225, 226, 227, 240, 241, 242, 243, 244, 245, 258, 259,
72  260, 261, 262, 263, 276, 277, 278, 279, 280, 281, 294, 295, 296, 297, 230, 231, 232, 233, 246, 247,
73  248, 249, 250, 251, 264, 265, 266, 267, 268, 269, 282, 283, 284, 285, 286, 287, 300, 301, 302, 303,
74  292, 293, 306, 307, 308, 309, 310, 311, 324, 325, 326, 327, 328, 329, 342, 343, 344, 345, 346, 347,
75  360, 361, 362, 363, 364, 365, 378, 379, 380, 381, 382, 383, 298, 299, 312, 313, 314, 315, 316, 317,
76  330, 331, 332, 333, 334, 335, 348, 349, 350, 351, 352, 353, 366, 367, 368, 369, 370, 371, 384, 385,
77  386, 387, 388, 389, 304, 305, 318, 319, 320, 321, 322, 323, 336, 337, 338, 339, 340, 341, 354, 355,
78  356, 357, 358, 359, 372, 373, 374, 375, 376, 377, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399,
79  400, 401, 414, 415, 416, 417, 418, 419, 432, 433, 434, 435, 436, 437, 450, 451, 452, 453, 454, 455,
80  468, 469, 470, 471, 472, 473, 486, 487, 488, 489, 490, 491, 504, 505, 506, 507, 508, 509, 402, 403,
81  404, 405, 406, 407, 420, 421, 422, 423, 424, 425, 438, 439, 440, 441, 442, 443, 456, 457, 458, 459,
82  460, 461, 474, 475, 476, 477, 478, 479, 492, 493, 494, 495, 496, 497, 510, 511, 512, 513, 514, 515,
83  408, 409, 410, 411, 412, 413, 426, 427, 428, 429, 430, 431, 444, 445, 446, 447, 448, 449, 462, 463,
84  464, 465, 466, 467, 480, 481, 482, 483, 484, 485, 498, 499, 500, 501, 502, 503, 516, 517, 518, 519,
85  520, 521, 522, 523, 524, 525, 526, 527, 540, 541, 542, 543, 544, 545, 558, 559, 560, 561, 562, 563,
86  528, 529, 530, 531, 532, 533, 546, 547, 548, 549, 550, 551, 564, 565, 566, 567, 568, 569, 534, 535,
87  536, 537, 538, 539, 552, 553, 554, 555, 556, 557, 570, 571, 572, 573, 574, 575},
88 
89 {  0,   1,   2,   3,   6,   7,   8,   9,  12,  13,  14,  15,   4,   5,  18,  19,  10,  11,  24,  25,
90   16,  17,  30,  31,  20,  21,  22,  23,  26,  27,  28,  29,  32,  33,  34,  35,  36,  37,  38,  39,
91   40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,
92   72,  73,  60,  61,  62,  63,  64,  65,  78,  79,  66,  67,  68,  69,  70,  71,  84,  85,  74,  75,
93   76,  77,  90,  91,  92,  93,  94,  95,  80,  81,  82,  83,  96,  97,  98,  99, 100, 101,  86,  87,
94   88,  89, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 126, 127, 128, 129, 130, 131,
95  114, 115, 116, 117, 118, 119, 132, 133, 134, 135, 136, 137, 120, 121, 122, 123, 124, 125, 138, 139,
96  140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 162, 163, 164, 165, 166, 167, 180, 181, 150, 151,
97  152, 153, 154, 155, 168, 169, 170, 171, 172, 173, 186, 187, 156, 157, 158, 159, 160, 161, 174, 175,
98  176, 177, 178, 179, 192, 193, 182, 183, 184, 185, 198, 199, 200, 201, 202, 203, 216, 217, 218, 219,
99  220, 221, 234, 235, 188, 189, 190, 191, 204, 205, 206, 207, 208, 209, 222, 223, 224, 225, 226, 227,
100  240, 241, 194, 195, 196, 197, 210, 211, 212, 213, 214, 215, 228, 229, 230, 231, 232, 233, 246, 247,
101  236, 237, 238, 239, 252, 253, 254, 255, 256, 257, 270, 271, 272, 273, 274, 275, 288, 289, 290, 291,
102  292, 293, 306, 307, 242, 243, 244, 245, 258, 259, 260, 261, 262, 263, 276, 277, 278, 279, 280, 281,
103  294, 295, 296, 297, 298, 299, 312, 313, 248, 249, 250, 251, 264, 265, 266, 267, 268, 269, 282, 283,
104  284, 285, 286, 287, 300, 301, 302, 303, 304, 305, 318, 319, 308, 309, 310, 311, 324, 325, 326, 327,
105  328, 329, 342, 343, 344, 345, 346, 347, 360, 361, 362, 363, 364, 365, 378, 379, 380, 381, 382, 383,
106  396, 397, 398, 399, 314, 315, 316, 317, 330, 331, 332, 333, 334, 335, 348, 349, 350, 351, 352, 353,
107  366, 367, 368, 369, 370, 371, 384, 385, 386, 387, 388, 389, 402, 403, 404, 405, 320, 321, 322, 323,
108  336, 337, 338, 339, 340, 341, 354, 355, 356, 357, 358, 359, 372, 373, 374, 375, 376, 377, 390, 391,
109  392, 393, 394, 395, 408, 409, 410, 411, 400, 401, 414, 415, 416, 417, 418, 419, 432, 433, 434, 435,
110  436, 437, 450, 451, 452, 453, 454, 455, 468, 469, 470, 471, 472, 473, 486, 487, 488, 489, 490, 491,
111  504, 505, 506, 507, 508, 509, 522, 523, 524, 525, 526, 527, 406, 407, 420, 421, 422, 423, 424, 425,
112  438, 439, 440, 441, 442, 443, 456, 457, 458, 459, 460, 461, 474, 475, 476, 477, 478, 479, 492, 493,
113  494, 495, 496, 497, 510, 511, 512, 513, 514, 515, 528, 529, 530, 531, 532, 533, 412, 413, 426, 427,
114  428, 429, 430, 431, 444, 445, 446, 447, 448, 449, 462, 463, 464, 465, 466, 467, 480, 481, 482, 483,
115  484, 485, 498, 499, 500, 501, 502, 503, 516, 517, 518, 519, 520, 521, 534, 535, 536, 537, 538, 539,
116  540, 541, 542, 543, 544, 545, 558, 559, 560, 561, 562, 563, 546, 547, 548, 549, 550, 551, 564, 565,
117  566, 567, 568, 569, 552, 553, 554, 555, 556, 557, 570, 571, 572, 573, 574, 575},
118 
119 {  0,   1,   2,   3,   6,   7,   8,   9,  12,  13,  14,  15,   4,   5,  18,  19,  10,  11,  24,  25,
120   16,  17,  30,  31,  20,  21,  22,  23,  26,  27,  28,  29,  32,  33,  34,  35,  36,  37,  38,  39,
121   40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,
122   72,  73,  60,  61,  62,  63,  64,  65,  78,  79,  66,  67,  68,  69,  70,  71,  84,  85,  74,  75,
123   76,  77,  90,  91,  92,  93,  94,  95,  80,  81,  82,  83,  96,  97,  98,  99, 100, 101,  86,  87,
124   88,  89, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 126, 127, 128, 129, 130, 131,
125  114, 115, 116, 117, 118, 119, 132, 133, 134, 135, 136, 137, 120, 121, 122, 123, 124, 125, 138, 139,
126  140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 162, 163, 164, 165, 166, 167, 180, 181, 150, 151,
127  152, 153, 154, 155, 168, 169, 170, 171, 172, 173, 186, 187, 156, 157, 158, 159, 160, 161, 174, 175,
128  176, 177, 178, 179, 192, 193, 182, 183, 184, 185, 198, 199, 200, 201, 202, 203, 216, 217, 218, 219,
129  220, 221, 234, 235, 188, 189, 190, 191, 204, 205, 206, 207, 208, 209, 222, 223, 224, 225, 226, 227,
130  240, 241, 194, 195, 196, 197, 210, 211, 212, 213, 214, 215, 228, 229, 230, 231, 232, 233, 246, 247,
131  236, 237, 238, 239, 252, 253, 254, 255, 256, 257, 270, 271, 272, 273, 274, 275, 288, 289, 290, 291,
132  292, 293, 306, 307, 242, 243, 244, 245, 258, 259, 260, 261, 262, 263, 276, 277, 278, 279, 280, 281,
133  294, 295, 296, 297, 298, 299, 312, 313, 248, 249, 250, 251, 264, 265, 266, 267, 268, 269, 282, 283,
134  284, 285, 286, 287, 300, 301, 302, 303, 304, 305, 318, 319, 308, 309, 310, 311, 324, 325, 326, 327,
135  328, 329, 342, 343, 344, 345, 346, 347, 360, 361, 362, 363, 364, 365, 378, 379, 380, 381, 382, 383,
136  396, 397, 314, 315, 316, 317, 330, 331, 332, 333, 334, 335, 348, 349, 350, 351, 352, 353, 366, 367,
137  368, 369, 370, 371, 384, 385, 386, 387, 388, 389, 402, 403, 320, 321, 322, 323, 336, 337, 338, 339,
138  340, 341, 354, 355, 356, 357, 358, 359, 372, 373, 374, 375, 376, 377, 390, 391, 392, 393, 394, 395,
139  408, 409, 398, 399, 400, 401, 414, 415, 416, 417, 418, 419, 432, 433, 434, 435, 436, 437, 450, 451,
140  452, 453, 454, 455, 468, 469, 470, 471, 472, 473, 486, 487, 488, 489, 490, 491, 504, 505, 506, 507,
141  508, 509, 404, 405, 406, 407, 420, 421, 422, 423, 424, 425, 438, 439, 440, 441, 442, 443, 456, 457,
142  458, 459, 460, 461, 474, 475, 476, 477, 478, 479, 492, 493, 494, 495, 496, 497, 510, 511, 512, 513,
143  514, 515, 410, 411, 412, 413, 426, 427, 428, 429, 430, 431, 444, 445, 446, 447, 448, 449, 462, 463,
144  464, 465, 466, 467, 480, 481, 482, 483, 484, 485, 498, 499, 500, 501, 502, 503, 516, 517, 518, 519,
145  520, 521, 522, 523, 524, 525, 526, 527, 540, 541, 542, 543, 544, 545, 558, 559, 560, 561, 562, 563,
146  528, 529, 530, 531, 532, 533, 546, 547, 548, 549, 550, 551, 564, 565, 566, 567, 568, 569, 534, 535,
147  536, 537, 538, 539, 552, 553, 554, 555, 556, 557, 570, 571, 572, 573, 574, 575}
148 },
149 {
150 {  0,   1,   2,   3,   6,   7,   8,   9,  12,  13,  14,  15,   4,   5,  18,  19,  10,  11,  24,  25,
151   16,  17,  30,  31,  20,  21,  22,  23,  26,  27,  28,  29,  32,  33,  34,  35,  36,  37,  38,  39,
152   42,  43,  44,  45,  48,  49,  50,  51,  40,  41,  54,  55,  56,  57,  46,  47,  60,  61,  62,  63,
153   52,  53,  66,  67,  68,  69,  58,  59,  72,  73,  74,  75,  76,  77,  64,  65,  78,  79,  80,  81,
154   82,  83,  70,  71,  84,  85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95, 108, 109, 110, 111,
155   96,  97,  98,  99, 100, 101, 114, 115, 116, 117, 102, 103, 104, 105, 106, 107, 120, 121, 122, 123,
156  112, 113, 126, 127, 128, 129, 130, 131, 144, 145, 146, 147, 118, 119, 132, 133, 134, 135, 136, 137,
157  150, 151, 152, 153, 124, 125, 138, 139, 140, 141, 142, 143, 156, 157, 158, 159, 148, 149, 162, 163,
158  164, 165, 166, 167, 180, 181, 182, 183, 184, 185, 154, 155, 168, 169, 170, 171, 172, 173, 186, 187,
159  188, 189, 190, 191, 160, 161, 174, 175, 176, 177, 178, 179, 192, 193, 194, 195, 196, 197, 198, 199,
160  200, 201, 202, 203, 216, 217, 218, 219, 220, 221, 234, 235, 236, 237, 238, 239, 204, 205, 206, 207,
161  208, 209, 222, 223, 224, 225, 226, 227, 240, 241, 242, 243, 244, 245, 210, 211, 212, 213, 214, 215,
162  228, 229, 230, 231, 232, 233, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 270, 271,
163  272, 273, 274, 275, 288, 289, 290, 291, 292, 293, 306, 307, 308, 309, 258, 259, 260, 261, 262, 263,
164  276, 277, 278, 279, 280, 281, 294, 295, 296, 297, 298, 299, 312, 313, 314, 315, 264, 265, 266, 267,
165  268, 269, 282, 283, 284, 285, 286, 287, 300, 301, 302, 303, 304, 305, 318, 319, 320, 321, 310, 311,
166  324, 325, 326, 327, 328, 329, 342, 343, 344, 345, 346, 347, 360, 361, 362, 363, 364, 365, 378, 379,
167  380, 381, 382, 383, 396, 397, 398, 399, 316, 317, 330, 331, 332, 333, 334, 335, 348, 349, 350, 351,
168  352, 353, 366, 367, 368, 369, 370, 371, 384, 385, 386, 387, 388, 389, 402, 403, 404, 405, 322, 323,
169  336, 337, 338, 339, 340, 341, 354, 355, 356, 357, 358, 359, 372, 373, 374, 375, 376, 377, 390, 391,
170  392, 393, 394, 395, 408, 409, 410, 411, 400, 401, 414, 415, 416, 417, 418, 419, 432, 433, 434, 435,
171  436, 437, 450, 451, 452, 453, 454, 455, 468, 469, 470, 471, 472, 473, 486, 487, 488, 489, 490, 491,
172  504, 505, 506, 507, 508, 509, 522, 523, 524, 525, 526, 527, 540, 541, 542, 543, 544, 545, 558, 559,
173  560, 561, 562, 563, 406, 407, 420, 421, 422, 423, 424, 425, 438, 439, 440, 441, 442, 443, 456, 457,
174  458, 459, 460, 461, 474, 475, 476, 477, 478, 479, 492, 493, 494, 495, 496, 497, 510, 511, 512, 513,
175  514, 515, 528, 529, 530, 531, 532, 533, 546, 547, 548, 549, 550, 551, 564, 565, 566, 567, 568, 569,
176  412, 413, 426, 427, 428, 429, 430, 431, 444, 445, 446, 447, 448, 449, 462, 463, 464, 465, 466, 467,
177  480, 481, 482, 483, 484, 485, 498, 499, 500, 501, 502, 503, 516, 517, 518, 519, 520, 521, 534, 535,
178  536, 537, 538, 539, 552, 553, 554, 555, 556, 557, 570, 571, 572, 573, 574, 575},
179 
180 {  0,   1,   2,   3,   6,   7,   8,   9,  12,  13,  14,  15,   4,   5,  18,  19,  10,  11,  24,  25,
181   16,  17,  30,  31,  20,  21,  22,  23,  26,  27,  28,  29,  32,  33,  34,  35,  36,  37,  38,  39,
182   42,  43,  44,  45,  48,  49,  50,  51,  40,  41,  54,  55,  56,  57,  46,  47,  60,  61,  62,  63,
183   52,  53,  66,  67,  68,  69,  58,  59,  72,  73,  74,  75,  64,  65,  78,  79,  80,  81,  70,  71,
184   84,  85,  86,  87,  76,  77,  90,  91,  92,  93,  94,  95, 108, 109,  82,  83,  96,  97,  98,  99,
185  100, 101, 114, 115,  88,  89, 102, 103, 104, 105, 106, 107, 120, 121, 110, 111, 112, 113, 126, 127,
186  128, 129, 130, 131, 144, 145, 116, 117, 118, 119, 132, 133, 134, 135, 136, 137, 150, 151, 122, 123,
187  124, 125, 138, 139, 140, 141, 142, 143, 156, 157, 146, 147, 148, 149, 162, 163, 164, 165, 166, 167,
188  180, 181, 182, 183, 152, 153, 154, 155, 168, 169, 170, 171, 172, 173, 186, 187, 188, 189, 158, 159,
189  160, 161, 174, 175, 176, 177, 178, 179, 192, 193, 194, 195, 184, 185, 198, 199, 200, 201, 202, 203,
190  216, 217, 218, 219, 220, 221, 234, 235, 190, 191, 204, 205, 206, 207, 208, 209, 222, 223, 224, 225,
191  226, 227, 240, 241, 196, 197, 210, 211, 212, 213, 214, 215, 228, 229, 230, 231, 232, 233, 246, 247,
192  236, 237, 238, 239, 252, 253, 254, 255, 256, 257, 270, 271, 272, 273, 274, 275, 288, 289, 290, 291,
193  242, 243, 244, 245, 258, 259, 260, 261, 262, 263, 276, 277, 278, 279, 280, 281, 294, 295, 296, 297,
194  248, 249, 250, 251, 264, 265, 266, 267, 268, 269, 282, 283, 284, 285, 286, 287, 300, 301, 302, 303,
195  292, 293, 306, 307, 308, 309, 310, 311, 324, 325, 326, 327, 328, 329, 342, 343, 344, 345, 346, 347,
196  360, 361, 362, 363, 364, 365, 298, 299, 312, 313, 314, 315, 316, 317, 330, 331, 332, 333, 334, 335,
197  348, 349, 350, 351, 352, 353, 366, 367, 368, 369, 370, 371, 304, 305, 318, 319, 320, 321, 322, 323,
198  336, 337, 338, 339, 340, 341, 354, 355, 356, 357, 358, 359, 372, 373, 374, 375, 376, 377, 378, 379,
199  380, 381, 382, 383, 396, 397, 398, 399, 400, 401, 414, 415, 416, 417, 418, 419, 432, 433, 434, 435,
200  436, 437, 450, 451, 452, 453, 454, 455, 468, 469, 470, 471, 472, 473, 486, 487, 488, 489, 490, 491,
201  504, 505, 506, 507, 508, 509, 522, 523, 524, 525, 526, 527, 540, 541, 542, 543, 544, 545, 558, 559,
202  560, 561, 562, 563, 384, 385, 386, 387, 388, 389, 402, 403, 404, 405, 406, 407, 420, 421, 422, 423,
203  424, 425, 438, 439, 440, 441, 442, 443, 456, 457, 458, 459, 460, 461, 474, 475, 476, 477, 478, 479,
204  492, 493, 494, 495, 496, 497, 510, 511, 512, 513, 514, 515, 528, 529, 530, 531, 532, 533, 546, 547,
205  548, 549, 550, 551, 564, 565, 566, 567, 568, 569, 390, 391, 392, 393, 394, 395, 408, 409, 410, 411,
206  412, 413, 426, 427, 428, 429, 430, 431, 444, 445, 446, 447, 448, 449, 462, 463, 464, 465, 466, 467,
207  480, 481, 482, 483, 484, 485, 498, 499, 500, 501, 502, 503, 516, 517, 518, 519, 520, 521, 534, 535,
208  536, 537, 538, 539, 552, 553, 554, 555, 556, 557, 570, 571, 572, 573, 574, 575},
209 
210 {  0,   1,   2,   3,   6,   7,   8,   9,  12,  13,  14,  15,   4,   5,  18,  19,  10,  11,  24,  25,
211   16,  17,  30,  31,  20,  21,  22,  23,  26,  27,  28,  29,  32,  33,  34,  35,  36,  37,  38,  39,
212   42,  43,  44,  45,  48,  49,  50,  51,  40,  41,  54,  55,  56,  57,  46,  47,  60,  61,  62,  63,
213   52,  53,  66,  67,  68,  69,  58,  59,  72,  73,  74,  75,  76,  77,  64,  65,  78,  79,  80,  81,
214   82,  83,  70,  71,  84,  85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95, 108, 109, 110, 111,
215  112, 113,  96,  97,  98,  99, 100, 101, 114, 115, 116, 117, 118, 119, 102, 103, 104, 105, 106, 107,
216  120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 144, 145, 146, 147, 148, 149, 162, 163,
217  164, 165, 132, 133, 134, 135, 136, 137, 150, 151, 152, 153, 154, 155, 168, 169, 170, 171, 138, 139,
218  140, 141, 142, 143, 156, 157, 158, 159, 160, 161, 174, 175, 176, 177, 166, 167, 180, 181, 182, 183,
219  184, 185, 198, 199, 200, 201, 202, 203, 216, 217, 218, 219, 220, 221, 172, 173, 186, 187, 188, 189,
220  190, 191, 204, 205, 206, 207, 208, 209, 222, 223, 224, 225, 226, 227, 178, 179, 192, 193, 194, 195,
221  196, 197, 210, 211, 212, 213, 214, 215, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
222  252, 253, 254, 255, 256, 257, 270, 271, 272, 273, 274, 275, 288, 289, 290, 291, 292, 293, 306, 307,
223  240, 241, 242, 243, 244, 245, 258, 259, 260, 261, 262, 263, 276, 277, 278, 279, 280, 281, 294, 295,
224  296, 297, 298, 299, 312, 313, 246, 247, 248, 249, 250, 251, 264, 265, 266, 267, 268, 269, 282, 283,
225  284, 285, 286, 287, 300, 301, 302, 303, 304, 305, 318, 319, 308, 309, 310, 311, 324, 325, 326, 327,
226  328, 329, 342, 343, 344, 345, 346, 347, 360, 361, 362, 363, 364, 365, 378, 379, 380, 381, 382, 383,
227  396, 397, 398, 399, 400, 401, 314, 315, 316, 317, 330, 331, 332, 333, 334, 335, 348, 349, 350, 351,
228  352, 353, 366, 367, 368, 369, 370, 371, 384, 385, 386, 387, 388, 389, 402, 403, 404, 405, 406, 407,
229  320, 321, 322, 323, 336, 337, 338, 339, 340, 341, 354, 355, 356, 357, 358, 359, 372, 373, 374, 375,
230  376, 377, 390, 391, 392, 393, 394, 395, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419,
231  432, 433, 434, 435, 436, 437, 450, 451, 452, 453, 454, 455, 468, 469, 470, 471, 472, 473, 486, 487,
232  488, 489, 490, 491, 504, 505, 506, 507, 508, 509, 522, 523, 524, 525, 526, 527, 420, 421, 422, 423,
233  424, 425, 438, 439, 440, 441, 442, 443, 456, 457, 458, 459, 460, 461, 474, 475, 476, 477, 478, 479,
234  492, 493, 494, 495, 496, 497, 510, 511, 512, 513, 514, 515, 528, 529, 530, 531, 532, 533, 426, 427,
235  428, 429, 430, 431, 444, 445, 446, 447, 448, 449, 462, 463, 464, 465, 466, 467, 480, 481, 482, 483,
236  484, 485, 498, 499, 500, 501, 502, 503, 516, 517, 518, 519, 520, 521, 534, 535, 536, 537, 538, 539,
237  540, 541, 542, 543, 544, 545, 558, 559, 560, 561, 562, 563, 546, 547, 548, 549, 550, 551, 564, 565,
238  566, 567, 568, 569, 552, 553, 554, 555, 556, 557, 570, 571, 572, 573, 574, 575}
239 }};
240 
241 /*
242  * fras == Formula for Requantization and All Scaling **************************
243  */
fras_l(int sfb,int global_gain,int scalefac_scale,int scalefac,int preflag)244 static inline float fras_l(int sfb,int global_gain,int scalefac_scale,int scalefac,int preflag)
245 {
246 register int a,scale;
247 	/*
248         if (scalefac_scale) scale=2;
249 	else scale=1;
250 	*/
251 	scale=scalefac_scale+1;
252 	a= global_gain -210 -(scalefac << scale);
253         if (preflag) a-=(t_pretab[sfb] << scale);
254 
255 /* bugfix, Mar 13 97: shifting won't produce a legal result if we shift by more than 31
256  * since global_gain<256, this can only occur for (very) negative values of a.
257 */
258 	if (a < -127) return 0;
259 
260 /* a minor change here as well, no point in abs() if we now that a<0
261 */
262         if (a>=0) return tab[a&3]*(1 << (a>>2));
263 	else return tabi[(-a)&3]/(1 << ((-a) >> 2));
264 }
265 
fras_s(int global_gain,int subblock_gain,int scalefac_scale,int scalefac)266 static inline float fras_s(int global_gain,int subblock_gain,int scalefac_scale,int scalefac)
267 {
268 int a;
269         a=global_gain - 210 - (subblock_gain << 3);
270 	if (scalefac_scale) a-= (scalefac << 2);
271 	else a-= (scalefac << 1);
272 
273         if (a < -127) return 0;
274 
275         if (a>=0) return tab[a&3]*(1 << (a>>2));
276         else return tabi[(-a)&3]/(1 << ((-a) >> 2));
277 }
278 
279 /* this should be faster than pow()
280  */
fras2(int is,float a)281 static inline float fras2(int is,float a)
282 {
283         if (is > 0) return t_43[is]*a;
284         else return -t_43[-is]*a;
285 }
286 
287 /*
288  * requantize_mono *************************************************************
289  */
290 
291 /* generally, the two channels do not have to be of the same block type - that's why we do two passes with requantize_mono.
292  * if ms or intensity stereo is enabled we do a single pass with requantize_ms because both channels are same block type
293  */
294 
requantize_mono(int gr,int ch,struct SIDE_INFO * info,struct AUDIO_HEADER * header)295 void requantize_mono(int gr,int ch,struct SIDE_INFO *info,struct AUDIO_HEADER *header)
296 {
297 int l,i,sfb;
298 float a;
299 int global_gain=info->global_gain[gr][ch];
300 int scalefac_scale=info->scalefac_scale[gr][ch];
301 int sfreq=header->sampling_frequency;
302 
303 
304 	no_of_imdcts[0]=no_of_imdcts[1]=32;
305 
306 	if (info->window_switching_flag[gr][ch] && info->block_type[gr][ch]==2)
307 		if (info->mixed_block_flag[gr][ch]) {
308 	/*
309 	 * requantize_mono - mixed blocks/long block part **********************
310 	 */
311 	                int window,window_len,preflag=0; /* pretab is all zero in this low frequency area */
312 	                int scalefac=scalefac_l[gr][ch][0];
313 
314 			l=0;sfb=0;
315 			a=fras_l(sfb,global_gain,scalefac_scale,scalefac,preflag);
316 	                while (l<36) {
317 	                        xr[ch][0][l]=fras2(is[ch][l],a);
318 	                        if (l==t_l[sfb]) {
319 					scalefac=scalefac_l[gr][ch][++sfb];
320 					a=fras_l(sfb,global_gain,scalefac_scale,scalefac,preflag);
321 				}
322 	                        l++;
323 	                }
324 	/*
325 	 * requantize_mono - mixed blocks/short block part *********************
326 	 */
327 			sfb=3;
328 			window_len=t_s[sfb]-t_s[sfb-1];
329 	                while (l<non_zero[ch]) {
330 	                	for (window=0;window<3;window++) {
331 	                        	int scalefac=scalefac_s[gr][ch][sfb][window];
332 	                                int subblock_gain=info->subblock_gain[gr][ch][window];
333 					a=fras_s(global_gain,subblock_gain,scalefac_scale,scalefac);
334 	                                for (i=0;i<window_len;i++) {
335       		                                xr[ch][0][t_reorder[header->ID][sfreq][l]]=fras2(is[ch][l],a);
336 	                                	l++;
337                                        	}
338                                	}
339                                	sfb++;
340                                	window_len=t_s[sfb]-t_s[sfb-1];
341 			}
342 			while (l<576) xr[ch][0][t_reorder[header->ID][sfreq][l++]]=0;
343 		} else {
344 	/*
345 	 * requantize mono - short blocks **************************************
346 	 */
347 			int window,window_len;
348 
349 			sfb=0; l=0;
350 			window_len=t_s[0]+1;
351 			while (l<non_zero[ch]) {
352 				for (window=0;window<3;window++) {
353 					int scalefac=scalefac_s[gr][ch][sfb][window];
354 					int subblock_gain=info->subblock_gain[gr][ch][window];
355 					float a=fras_s(global_gain,subblock_gain,scalefac_scale,scalefac);
356 					for (i=0;i<window_len;i++) {
357 						xr[ch][0][t_reorder[header->ID][sfreq][l]]=fras2(is[ch][l],a);
358 						l++;
359 					}
360 				}
361 				sfb++;
362 				window_len=t_s[sfb]-t_s[sfb-1];
363 			}
364 			while (l<576) xr[ch][0][t_reorder[header->ID][sfreq][l++]]=0;
365 		}
366 	else {
367 	/* long blocks */
368 		int preflag=info->preflag[gr][ch];
369 		int scalefac=scalefac_l[gr][ch][0];
370 
371 		sfb=0; l=0;
372 		a=fras_l(sfb,global_gain,scalefac_scale,scalefac,preflag);
373 		while (l<non_zero[ch]) {
374 			xr[ch][0][l]=fras2(is[ch][l],a);
375 			if (l==t_l[sfb]) {
376 				scalefac=scalefac_l[gr][ch][++sfb];
377 				a=fras_l(sfb,global_gain,scalefac_scale,scalefac,preflag);
378 			}
379 			l++;
380 		}
381 		while (l<576) xr[ch][0][l++]=0;
382 	}
383 }
384 
385 /*
386  * stereo stuff ****************************************************************
387  */
find_isbound(int isbound[3],int gr,struct SIDE_INFO * info,struct AUDIO_HEADER * header)388 static int find_isbound(int isbound[3],int gr,struct SIDE_INFO *info,struct AUDIO_HEADER *header)
389 {
390 int sfb,window,window_len,ms_flag,tmp,i;
391 
392 	isbound[0]=isbound[1]=isbound[2]=-1;
393 	no_of_imdcts[0]=no_of_imdcts[1]=32;
394 
395    if (header->mode_extension==1 || header->mode_extension==3) {
396 	if (info->window_switching_flag[gr][0] && info->block_type[gr][0]==2) {
397 
398 	/* find that isbound!
399 	 */
400 		tmp=non_zero[1];
401 		sfb=0; while ((3*t_s[sfb]+2) < tmp  && sfb < 12) sfb++;
402 		while ((isbound[0]<0 || isbound[1]<0 || isbound[2]<0) && !(info->mixed_block_flag[gr][0] && sfb<3) && sfb) {
403 			for (window=0;window<3;window++) {
404 				if (sfb==0) {
405 					window_len=t_s[0]+1;
406 					tmp=(window+1)*window_len - 1;
407 				} else {
408 					window_len=t_s[sfb]-t_s[sfb-1];
409 					tmp=(3*t_s[sfb-1]+2) + (window+1)*window_len;
410 				}
411 				if (isbound[window] < 0)
412 					for (i=0;i<window_len;i++)
413 						if (is[1][tmp--] != 0) {
414 							isbound[window]=t_s[sfb]+1;
415 							break;
416 						}
417 			}
418 			sfb--;
419 		}
420 
421 	/* mixed block magic now...
422 	 */
423 		if (sfb==2 && info->mixed_block_flag[gr][0])
424 		{
425 			if (isbound[0]<0 && isbound[1]<0 && isbound[2]<0)
426 			{
427 				tmp=35;
428 				while (is[1][tmp] == 0) tmp--;
429 				sfb=0; while (t_l[sfb] < tmp  && sfb < 21) sfb++;
430 				isbound[0]=isbound[1]=isbound[2]=t_l[sfb]+1;
431 			} else for (window=0;window<3;window++)
432 				if (isbound[window]<0) isbound[window]=36;
433 		}
434 		if (header->ID==1) isbound[0]=isbound[1]=isbound[2]=MAX(isbound[0],MAX(isbound[1],isbound[2]));
435 
436 	/* just how many imdcts?
437 	 */
438 		tmp=non_zero[0];
439 		sfb=0; while ((3*t_s[sfb]+2) < tmp && sfb < 12) sfb++;
440 		no_of_imdcts[0]=no_of_imdcts[1]=(t_s[sfb]-1)/6+1;
441 	} else {
442 
443 	/* long blocks now
444 	 */
445                 tmp=non_zero[1];
446                 while (is[1][tmp] == 0) tmp--;
447                 sfb=0; while (t_l[sfb] < tmp && sfb < 21) sfb++;
448 		isbound[0]=isbound[1]=isbound[2]=t_l[sfb]+1;
449 		no_of_imdcts[0]=no_of_imdcts[1]=(non_zero[0]-1)/18+1;
450 	}
451 	if (header->mode_extension==1) ms_flag=0;
452 	else ms_flag=1;
453    } else {
454 
455    /* intensity stereo is, regretably, turned off
456     */
457 	ms_flag=1;
458 
459 	/* i really put a lot of work in this, but it still looks like shit (works, though)
460 	 */
461 	if (!info->window_switching_flag[gr][0] || (info->window_switching_flag[gr][0] && info->block_type[gr][0]!=2))
462 		isbound[0]=isbound[1]=isbound[2]=(MAX(non_zero[0],non_zero[1]));
463 	else isbound[0]=isbound[1]=isbound[2]=576;
464 
465 	if (info->window_switching_flag[gr][0] && info->block_type[gr][0]==2) {
466 		/* should do for mixed blocks too, though i havent tested... */
467 			tmp=(MAX(non_zero[0],non_zero[1]))/3;
468 			sfb=0; while (t_s[sfb]<tmp && sfb<12) sfb++;
469 			no_of_imdcts[0]=no_of_imdcts[1]=(t_s[sfb]-1)/6+1;
470 	}
471 	else no_of_imdcts[0]=no_of_imdcts[1]=(isbound[0]-1)/18+1;
472 
473    }
474 
475    return ms_flag;
476 }
477 
stereo_s(int l,float a[2],int pos,int ms_flag,int is_pos,struct AUDIO_HEADER * header)478 static inline void stereo_s(int l,float a[2],int pos,int ms_flag,int is_pos,struct AUDIO_HEADER *header)
479 {
480 float ftmp,Mi,Si;
481 
482 	if (l>=576) return; /* brrr... */
483 
484         if ((is_pos != IS_ILLEGAL) && (header->ID==1)) {
485                 ftmp=fras2(is[0][l],a[0]);
486                 xr[0][0][pos]=(1-t_is[is_pos])*ftmp;
487                 xr[1][0][pos]=t_is[is_pos]*ftmp;
488 		return;
489 	}
490 
491 	if ((is_pos != IS_ILLEGAL) && (header->ID==0)) {
492 		ftmp=fras2(is[0][l],a[0]);
493 		if (is_pos&1) {
494 			xr[0][0][pos]= t_is2[intensity_scale][(is_pos+1)>>1] * ftmp;
495 			xr[1][0][pos]= ftmp;
496 		} else {
497 			xr[0][0][pos]= ftmp;
498 			xr[1][0][pos]= t_is2[intensity_scale][is_pos>>1] * ftmp;
499 		}
500 		return;
501 	}
502 
503         if (ms_flag) {
504                 Mi=fras2(is[0][l],a[0]);
505                 Si=fras2(is[1][l],a[1]);
506                 xr[0][0][pos]=(Mi+Si)*i_sq2;
507                 xr[1][0][pos]=(Mi-Si)*i_sq2;
508         } else {
509                 xr[0][0][pos]=fras2(is[0][l],a[0]);
510                 xr[1][0][pos]=fras2(is[1][l],a[1]);
511         }
512 }
513 
stereo_l(int l,float a[2],int ms_flag,int is_pos,struct AUDIO_HEADER * header)514 static inline void stereo_l(int l,float a[2],int ms_flag,int is_pos,struct AUDIO_HEADER *header)
515 {
516 float ftmp,Mi,Si;
517 	if (l>=576) return;
518 
519 /* new code by ???
520 */
521 	if (is_pos != IS_ILLEGAL) {
522 		ftmp = fras2(is[0][l], a[0]);
523 		if (header -> ID ==1) {
524 			xr[0][0][l] = (1 - t_is[is_pos]) * ftmp;
525 			xr[1][0][l] = t_is[is_pos] * ftmp;
526 			return;
527 		} else if (is_pos & 1) {
528 			xr[0][0][l] = t_is2[intensity_scale][(is_pos + 1) >> 1] * ftmp;
529 			xr[1][0][l] = ftmp;
530 		} else {
531 			xr[0][0][l] = ftmp;
532 			xr[1][0][l] = t_is2[intensity_scale][is_pos >> 1] * ftmp;
533 		}
534 		return;
535 	}
536 
537 	if (ms_flag) {
538 		Mi=fras2(is[0][l],a[0]);
539 		Si=fras2(is[1][l],a[1]);
540 		xr[0][0][l]=(Mi+Si)*i_sq2;
541 		xr[1][0][l]=(Mi-Si)*i_sq2;
542 	} else {
543 		xr[0][0][l]=fras2(is[0][l],a[0]);
544 		xr[1][0][l]=fras2(is[1][l],a[1]);
545 	}
546 
547 }
548 
549 
550 /*
551  * requantize_ms ***************************************************************
552  */
requantize_ms(int gr,struct SIDE_INFO * info,struct AUDIO_HEADER * header)553 void requantize_ms(int gr,struct SIDE_INFO *info,struct AUDIO_HEADER *header)
554 {
555 int l,sfb,ms_flag,is_pos,i,ch;
556 int *global_gain,subblock_gain[2],*scalefac_scale,scalefac[2],isbound[3];
557 int sfreq=header->sampling_frequency;
558 int id = header->ID;
559 float a[2];
560 
561 global_gain=info->global_gain[gr];
562 scalefac_scale=info->scalefac_scale[gr];
563 
564         if (info->window_switching_flag[gr][0] && info->block_type[gr][0]==2)
565 	{
566                 if (info->mixed_block_flag[gr][0])
567                 {
568 	/*
569 	 * mixed blocks w/stereo processing - long block part ******************
570 	 */
571                         int window,window_len;
572                         int preflag[2]={0,0};
573 
574 			ms_flag=find_isbound(isbound,gr,info,header);
575 
576 			sfb=0; l=0;
577 			for (ch=0;ch<2;ch++) {
578 				scalefac[ch]=scalefac_l[gr][ch][0];
579 				a[ch]=fras_l(0,global_gain[ch],scalefac_scale[ch],scalefac[ch],preflag[ch]);
580 			}
581 
582 
583 			while (l<36) {
584 				int is_pos;
585 				if (l<isbound[0])
586 					is_pos=IS_ILLEGAL;
587 				else
588 				{
589 					is_pos=scalefac[1];
590 					if (id==1) /* MPEG1 */
591 					{
592 						if (is_pos==7)
593 							is_pos=IS_ILLEGAL;
594 					}
595 					else /* MPEG2 */
596 					{
597 						if (is_pos==is_max[sfb])
598 							is_pos=IS_ILLEGAL;
599 					}
600 				}
601 
602 				stereo_l(l,a,ms_flag,is_pos,header);
603 
604 				if (l==t_l[sfb])
605 				{
606 					sfb++;
607 					for (ch=0;ch<2;ch++)
608 					{
609 						scalefac[ch]=scalefac_l[gr][ch][sfb];
610 						a[ch]=fras_l(sfb,global_gain[ch],scalefac_scale[ch],scalefac[ch],preflag[ch]);
611 					}
612 				}
613 
614 				l++;
615 			}
616 	/*
617 	 * mixed blocks w/stereo processing - short block part *****************
618 	 */
619                         sfb=3;
620                         window_len=t_s[sfb]-t_s[sfb-1];
621 
622                         while (l<(MAX(non_zero[0],non_zero[1])))
623                         {
624                                 for (window=0;window<3;window++)
625                                 {
626                                         subblock_gain[0]=info->subblock_gain[gr][0][window];
627 					subblock_gain[1]=info->subblock_gain[gr][1][window];
628 					scalefac[0]=scalefac_s[gr][0][sfb][window];
629 					scalefac[1]=scalefac_s[gr][1][sfb][window];
630 
631 					if (t_s[sfb] < isbound[window])
632 					{
633 						is_pos=IS_ILLEGAL;
634 						a[0]=fras_s(global_gain[0],subblock_gain[0],scalefac_scale[0],scalefac[0]);
635 						a[1]=fras_s(global_gain[1],subblock_gain[1],scalefac_scale[1],scalefac[1]);
636 					}
637 					else
638 					{
639 						is_pos=scalefac[1];
640 						if (id==1) /* MPEG1 */
641 						{
642 							if (is_pos==7)
643 								is_pos=IS_ILLEGAL;
644 						}
645 						else /* MPEG2 */
646 						{
647 							if (is_pos==is_max[sfb+6])
648 								is_pos=IS_ILLEGAL;
649 						}
650 						a[0]=fras_s(global_gain[0],subblock_gain[0],scalefac_scale[0],scalefac[0]);
651 					}
652 
653                                         for (i=0;i<window_len;i++)
654                                         {
655                                                 stereo_s(l,a,t_reorder[id][sfreq][l],ms_flag,is_pos,header);
656                                                 l++;
657                                         }
658                                 }
659                                 sfb++;
660                                 window_len=t_s[sfb]-t_s[sfb-1];
661                         }
662                         while (l<576)
663                         {
664 			        int reorder = t_reorder[id][sfreq][l++];
665 
666 				xr[0][0][reorder]=xr[1][0][reorder]=0;
667 			}
668                 }
669                 else
670                 {
671         /*
672 	 * requantize_ms - short blocks w/stereo processing ********************
673 	 */
674                         int window,window_len;
675 
676 			ms_flag=find_isbound(isbound,gr,info,header);
677 			sfb=0; l=0;
678 			window_len=t_s[0]+1;
679 
680                         while (l<(MAX(non_zero[0],non_zero[1])))
681                         {
682                                 for (window=0;window<3;window++)
683                                 {
684 					subblock_gain[0]=info->subblock_gain[gr][0][window];
685 					subblock_gain[1]=info->subblock_gain[gr][1][window];
686 					scalefac[0]=scalefac_s[gr][0][sfb][window];
687 					scalefac[1]=scalefac_s[gr][1][sfb][window];
688 
689                                         if (t_s[sfb] < isbound[window])
690                                         {
691                                                 is_pos=IS_ILLEGAL;
692 						a[0]=fras_s(global_gain[0],subblock_gain[0],scalefac_scale[0],scalefac[0]);
693 						a[1]=fras_s(global_gain[1],subblock_gain[1],scalefac_scale[1],scalefac[1]);
694                                         }
695                                         else
696                                         {
697                                                 is_pos=scalefac[1];
698                                                 if (id==1) /* MPEG1 */
699                                         	{
700                                                         if (is_pos==7)
701                                                         	is_pos=IS_ILLEGAL;
702 						}
703                                                 else /* MPEG2 */
704                                                 {
705                                                         if (is_pos==is_max[sfb+6])
706                                                         	is_pos=IS_ILLEGAL;
707 						}
708                                                 a[0]=fras_s(global_gain[0],subblock_gain[0],scalefac_scale[0],scalefac[0]);
709                                         }
710 
711                                         for (i=0;i<window_len;i++)
712                                         {
713 						stereo_s(l,a,t_reorder[id][sfreq][l],ms_flag,is_pos,header);
714 						l++;
715 					}
716                                 }
717                                 /* this won't do anymore
718 				 * window_len=-t_s[sfb]+t_s[++sfb];
719 				 */
720 				window_len  = -t_s[sfb++];
721 				window_len +=  t_s[sfb];
722 
723                         }
724                         while (l<576)
725                         {
726 			        int reorder = t_reorder[id][sfreq][l++];
727 
728 				xr[0][0][reorder]=xr[1][0][reorder]=0;
729 			}
730                 }
731         }
732         else
733         {
734         /*
735 	 * long blocks w/stereo processing *************************************
736 	 */
737                 int *preflag=info->preflag[gr];
738 
739 		ms_flag=find_isbound(isbound,gr,info,header);
740 
741 		sfb=0; l=0;
742 		scalefac[0]=scalefac_l[gr][0][sfb];
743 		a[0]=fras_l(sfb,global_gain[0],scalefac_scale[0],scalefac[0],preflag[0]);
744 		scalefac[1]=scalefac_l[gr][1][sfb];
745 		a[1]=fras_l(sfb,global_gain[1],scalefac_scale[1],scalefac[1],preflag[1]);
746 
747 		/* no intensity stereo part
748 		*/
749 	if (ms_flag)
750                 while (l< isbound[0]) {
751 #if defined(PENTIUM_RDTSC)
752 
753 unsigned int cnt4, cnt3, cnt2, cnt1;
754 static int min_cycles = 99999999;
755 
756         __asm__(".byte 0x0f,0x31" : "=a" (cnt1), "=d" (cnt4));
757 #endif
758 
759 			{
760 				register float Mi = fras2(is[0][l],a[0]);
761 				register float Si = fras2(is[1][l],a[1]);
762 				register float tmp = i_sq2;
763 				xr[0][0][l]=(Mi+Si)*tmp;
764 				xr[1][0][l]=(Mi-Si)*tmp;
765 			}
766 
767 #if defined(PENTIUM_RDTSC)
768                         __asm__(".byte 0x0f,0x31" : "=a" (cnt2), "=d" (cnt4));
769 
770                         if (cnt2-cnt1 < min_cycles) {
771                           min_cycles = cnt2-cnt1;
772                           /*printf("%d cycles\n", min_cycles);*/
773                         }
774 
775 #endif
776                         if (l==t_l[sfb]) {
777 #if defined(PENTIUM_RDTSC2)
778 
779 unsigned int cnt4, cnt2, cnt1;
780 static int min_cycles = 99999999;
781 
782         __asm__(".byte 0x0f,0x31" : "=a" (cnt1), "=d" (cnt4));
783 #endif
784 
785                                 sfb++;
786 				scalefac[0]=scalefac_l[gr][0][sfb];
787 				a[0]=fras_l(sfb,global_gain[0],scalefac_scale[0],scalefac[0],preflag[0]);
788 				scalefac[1]=scalefac_l[gr][1][sfb];
789 				a[1]=fras_l(sfb,global_gain[1],scalefac_scale[1],scalefac[1],preflag[1]);
790 #if defined(PENTIUM_RDTSC2)
791                         __asm__(".byte 0x0f,0x31" : "=a" (cnt2), "=d" (cnt4));
792 
793                         if (cnt2-cnt1 < min_cycles) {
794                           min_cycles = cnt2-cnt1;
795                           /*printf("%d cycles, sfb %d\n", min_cycles, sfb);*/
796                         }
797 
798 #endif
799                         }
800                         l++;
801                 }
802 	else
803                 while (l< isbound[0]) {
804                         xr[0][0][l]=fras2(is[0][l],a[0]);
805                         xr[1][0][l]=fras2(is[1][l],a[1]);
806                         if (l==t_l[sfb]) {
807                                 sfb++;
808                                 scalefac[0]=scalefac_l[gr][0][sfb];
809                                 a[0]=fras_l(sfb,global_gain[0],scalefac_scale[0],scalefac[0],preflag[0]);
810                                 scalefac[1]=scalefac_l[gr][1][sfb];
811                                 a[1]=fras_l(sfb,global_gain[1],scalefac_scale[1],scalefac[1],preflag[1]);
812                         }
813                         l++;
814                 }
815 
816 
817 		/* intensity stereo part
818 		*/
819 		while (l<(MAX(non_zero[0],non_zero[1]))) {
820 			int is_pos=scalefac[1];
821 
822 			if (id==1) /* MPEG1 */
823 			{
824 				if (is_pos==7)
825 					is_pos=IS_ILLEGAL;
826 			}
827 			else /* MPEG2 */
828 			{
829 				if (is_pos==is_max[sfb])
830 					is_pos=IS_ILLEGAL;
831 			}
832 			stereo_l(l,a,ms_flag,is_pos,header);
833 
834 			if (l==t_l[sfb])
835 			{
836 				sfb++;
837 				scalefac[0]=scalefac_l[gr][0][sfb];
838 				scalefac[1]=scalefac_l[gr][1][sfb];
839 				a[0]=fras_l(sfb,global_gain[0],scalefac_scale[0],scalefac[0],preflag[0]);
840 			}
841 			l++;
842 		}
843 
844                 while (l<576) {
845 			xr[0][0][l]=0;
846 			xr[1][0][l]=0;
847 			l++;
848 		}
849         }
850 }
851 
852 /*
853  * requantize_downmix **********************************************************
854  */
requantize_downmix(int gr,struct SIDE_INFO * info,struct AUDIO_HEADER * header)855 void requantize_downmix(int gr,struct SIDE_INFO *info,struct AUDIO_HEADER *header)
856 {
857 int l,sfb,ms_flag,i;
858 int *global_gain,subblock_gain[2],*scalefac_scale,scalefac[2];
859 int sfreq=header->sampling_frequency;
860 int id = header->ID;
861 float a[2];
862 
863 	/* first set some variables
864 	*/
865 	global_gain=info->global_gain[gr];
866 	scalefac_scale=info->scalefac_scale[gr];
867 
868 	if (header->mode_extension==2 || header->mode_extension==3) ms_flag=1;
869 	else ms_flag=0;
870 
871 	/* ... and then we're off for requantization
872 	*/
873 	if (info->window_switching_flag[gr][0] && info->block_type[gr][0]==2)
874 	{
875 		if (info->mixed_block_flag[gr][0])
876 		{
877 			die("mixed block? hmmmm...., downmix for mixed blocks is not yet implemented\n");
878 		}
879 		else
880 		{
881 			int window,window_len;
882 			int isbound[3];
883 			int is_pos;
884 
885 			find_isbound(isbound,gr,info,header); /* ugly hack, part3 */
886 
887 			sfb=0; l=0; window_len=t_s[0]+1;
888 
889 			while (l<(MAX(non_zero[0],non_zero[1])))
890 			{
891 				for (window=0;window<3;window++)
892 				{
893 					subblock_gain[0]=info->subblock_gain[gr][0][window];
894 					subblock_gain[1]=info->subblock_gain[gr][1][window];
895 					scalefac[0]=scalefac_s[gr][0][sfb][window];
896 					is_pos=scalefac[1]=scalefac_s[gr][1][sfb][window];
897 
898 					if (t_s[sfb] < isbound[window])
899 					{
900 						a[0]=fras_s(global_gain[0],subblock_gain[0],scalefac_scale[0],scalefac[0]);
901 						if (ms_flag)
902 						{
903 							for (i=0;i<window_len;i++)
904 							{
905 								register float Mi=fras2(is[0][l],a[0]);
906 								xr[0][0][t_reorder[id][sfreq][l]]=Mi*i_sq2;
907 								l++;
908 							}
909 						}
910 						else
911 						{
912 							a[1]=fras_s(global_gain[1],subblock_gain[1],scalefac_scale[1],scalefac[1]);
913 							for (i=0;i<window_len;i++)
914 							{
915 								register float tmp1=fras2(is[0][l],a[0]);
916 								register float tmp2=fras2(is[1][l],a[1]);
917 								xr[0][0][t_reorder[id][sfreq][l]]=(tmp1+tmp2)*0.5f;
918 								l++;
919 							}
920 						}
921 					}
922 					else
923 					{
924 						a[0]=fras_s(global_gain[0],subblock_gain[0],scalefac_scale[0],scalefac[0]);
925 						for (i=0;i<window_len;i++)
926 						{
927 							register float ftmp = fras2(is[0][l], a[0]);
928 							if (id==0 && is_pos<is_max[sfb])
929 								ftmp*=t_downmix[intensity_scale][(is_pos+1)>>1];
930 							xr[0][0][t_reorder[id][sfreq][l]] = ftmp;
931 							l++;
932 						}
933 					}
934 				}
935 				window_len  = -t_s[sfb++];
936 				window_len +=  t_s[sfb];
937 			}
938 			while (l<576)
939 			{
940 				xr[0][0][l]=0;
941 				l++;
942 			}
943 		}
944 	}
945 	else
946 	{
947 		int *preflag=info->preflag[gr];
948 		int isbound;
949 
950 		if (header->mode_extension==1 || header->mode_extension==3)
951 		{
952 			int tmp=non_zero[1];
953 			while (is[1][tmp] == 0) tmp--;
954 			sfb=0; while (t_l[sfb] < tmp && sfb < 21) sfb++;
955 			isbound=t_l[sfb]+1;
956 			no_of_imdcts[0]=no_of_imdcts[1]=(non_zero[0]-1)/18+1;
957 		}
958 		else
959 		{
960 			isbound=(MAX(non_zero[0],non_zero[1]));
961 			no_of_imdcts[0]=no_of_imdcts[1]=(isbound-1)/18+1;
962 		}
963 
964                 sfb=0; l=0;
965                 scalefac[0]=scalefac_l[gr][0][sfb];
966                 a[0]=fras_l(sfb,global_gain[0],scalefac_scale[0],scalefac[0],preflag[0]);
967                 scalefac[1]=scalefac_l[gr][1][sfb];
968                 a[1]=fras_l(sfb,global_gain[1],scalefac_scale[1],scalefac[1],preflag[1]);
969 
970 		/* no intensity stereo part
971 		 */
972 		if (ms_flag)
973 		{
974 			while (l < isbound)
975 			{
976 				register float Mi = fras2(is[0][l],a[0]);
977 				register float tmp = i_sq2;
978 				xr[0][0][l]=Mi*tmp;
979 
980 				if (l==t_l[sfb])
981 				{
982 					sfb++;
983                                 	scalefac[0]=scalefac_l[gr][0][sfb];
984                                 	a[0]=fras_l(sfb,global_gain[0],scalefac_scale[0],scalefac[0],preflag[0]);
985 					scalefac[1]=scalefac_l[gr][1][sfb];
986 				}
987 				l++;
988 			}
989 		}
990 		else
991 		{
992 			while (l < isbound)
993 			{
994 				register float tmp1=fras2(is[0][l],a[0]);
995 				register float tmp2=fras2(is[1][l],a[1]);
996 				xr[0][0][l]=(tmp1+tmp2)*0.5f;
997 	                        if (l==t_l[sfb])
998 	                        {
999         	                        sfb++;
1000                 	                scalefac[0]=scalefac_l[gr][0][sfb];
1001                         	        a[0]=fras_l(sfb,global_gain[0],scalefac_scale[0],scalefac[0],preflag[0]);
1002                                 	scalefac[1]=scalefac_l[gr][1][sfb];
1003                                 	a[1]=fras_l(sfb,global_gain[1],scalefac_scale[1],scalefac[1],preflag[1]);
1004                         	}
1005                         	l++;
1006 			}
1007 		}
1008 		/* intensity stereo part
1009 		*/
1010 		while (l<(MAX(non_zero[0],non_zero[1])))
1011 		{
1012 			int is_pos=scalefac[1];
1013 			register float ftmp=fras2(is[0][l], a[0]);
1014 
1015 			if (id==0  && is_pos<is_max[sfb])
1016 			{
1017 				ftmp*=t_downmix[intensity_scale][(is_pos+1)>>1];
1018 			}
1019 
1020 			xr[0][0][l] = ftmp;
1021 
1022 			if (l==t_l[sfb])
1023 			{
1024 				sfb++;
1025 				scalefac[0]=scalefac_l[gr][0][sfb];
1026 				a[0]=fras_l(sfb,global_gain[0],scalefac_scale[0],scalefac[0],preflag[0]);
1027 				scalefac[1]=scalefac_l[gr][1][sfb];
1028 			}
1029 			l++;
1030 		}
1031 
1032 		/* _always_ zero out everything else
1033 		*/
1034 		while (l<576)
1035 		{
1036 			xr[0][0][l]=0;
1037 			l++;
1038 		}
1039 	}
1040 }
1041 
1042 /*
1043  * antialiasing butterflies ****************************************************
1044  *
1045  */
alias_reduction(int ch)1046 void alias_reduction(int ch)
1047 {
1048 unsigned int sb;
1049 
1050         for (sb=1;sb<32;sb++)
1051         {
1052 	        float *x = xr[ch][sb];
1053 		register float a, b;
1054 
1055 	        a = x[0];
1056 		b = x[-1];
1057                 x[-1] = b * Cs[0] - a * Ca[0];
1058                 x[0]  = a * Cs[0] + b * Ca[0];
1059 
1060 	        a = x[1];
1061 		b = x[-2];
1062                 x[-2] = b * Cs[1] - a * Ca[1];
1063                 x[1]  = a * Cs[1] + b * Ca[1];
1064 
1065 	        a = x[2];
1066 		b = x[-3];
1067                 x[-3] = b * Cs[2] - a * Ca[2];
1068                 x[2]  = a * Cs[2] + b * Ca[2];
1069 
1070 	        a = x[3];
1071 		b = x[-4];
1072                 x[-4] = b * Cs[3] - a * Ca[3];
1073                 x[3]  = a * Cs[3] + b * Ca[3];
1074 
1075 	        a = x[4];
1076 		b = x[-5];
1077                 x[-5] = b * Cs[4] - a * Ca[4];
1078                 x[4]  = a * Cs[4] + b * Ca[4];
1079 
1080 	        a = x[5];
1081 		b = x[-6];
1082                 x[-6] = b * Cs[5] - a * Ca[5];
1083                 x[5]  = a * Cs[5] + b * Ca[5];
1084 
1085 	        a = x[6];
1086 		b = x[-7];
1087                 x[-7] = b * Cs[6] - a * Ca[6];
1088                 x[6]  = a * Cs[6] + b * Ca[6];
1089 
1090 	        a = x[7];
1091 		b = x[-8];
1092                 x[-8] = b * Cs[7] - a * Ca[7];
1093                 x[7]  = a * Cs[7] + b * Ca[7];
1094 	}
1095 }
1096 
1097 /* calculating t_43 instead of having that big table in misc2.h
1098  */
1099 
calculate_t43(void)1100 void calculate_t43(void)
1101 {
1102 int i;
1103 	for (i=0;i<8192;i++)
1104 		t_43[i]=(float)pow((float)i,1.33333333333f);
1105 }
1106