Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_wb / dec / src / mime_io.cpp
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
20
21     3GPP TS 26.173
22     ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
23     Available from http://www.3gpp.org
24
25 (C) 2007, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
26 Permission to distribute, modify and use this file under the standard license
27 terms listed above has been obtained from the copyright holder.
28 ****************************************************************************************/
29 /*
30 ------------------------------------------------------------------------------
31
32
33  Pathname: ./src/mime_io.cpp
34
35 ------------------------------------------------------------------------------
36  INPUT AND OUTPUT DEFINITIONS
37
38  Inputs:
39     [input_variable_name] = [description of the input to module, its type
40                  definition, and length (when applicable)]
41
42  Local Stores/Buffers/Pointers Needed:
43     [local_store_name] = [description of the local store, its type
44                   definition, and length (when applicable)]
45     [local_buffer_name] = [description of the local buffer, its type
46                    definition, and length (when applicable)]
47     [local_ptr_name] = [description of the local pointer, its type
48                 definition, and length (when applicable)]
49
50  Global Stores/Buffers/Pointers Needed:
51     [global_store_name] = [description of the global store, its type
52                    definition, and length (when applicable)]
53     [global_buffer_name] = [description of the global buffer, its type
54                 definition, and length (when applicable)]
55     [global_ptr_name] = [description of the global pointer, its type
56                  definition, and length (when applicable)]
57
58  Outputs:
59     [return_variable_name] = [description of data/pointer returned
60                   by module, its type definition, and length
61                   (when applicable)]
62
63  Pointers and Buffers Modified:
64     [variable_bfr_ptr] points to the [describe where the
65       variable_bfr_ptr points to, its type definition, and length
66       (when applicable)]
67     [variable_bfr] contents are [describe the new contents of
68       variable_bfr]
69
70  Local Stores Modified:
71     [local_store_name] = [describe new contents, its type
72                   definition, and length (when applicable)]
73
74  Global Stores Modified:
75     [global_store_name] = [describe new contents, its type
76                    definition, and length (when applicable)]
77
78 ------------------------------------------------------------------------------
79  FUNCTION DESCRIPTION
80
81  [Describe what the module does by using the variable names
82  listed in the Input and Output Definitions Section above.]
83
84 ------------------------------------------------------------------------------
85  REQUIREMENTS
86
87  [List requirements to be satisfied by this module.]
88
89 ------------------------------------------------------------------------------
90  REFERENCES
91
92  [List all references used in designing this module.]
93
94 ------------------------------------------------------------------------------
95  PSEUDO-CODE
96
97  ------------------------------------------------------------------------------
98  RESOURCES USED
99
100  STACK USAGE:
101
102  DATA MEMORY USED: x words
103
104  PROGRAM MEMORY USED: x words
105
106  CLOCK CYCLES:
107
108 ------------------------------------------------------------------------------
109 */
110
111
112 /*----------------------------------------------------------------------------
113 ; INCLUDES
114 ----------------------------------------------------------------------------*/
115
116 #include "pv_amr_wb_type_defs.h"
117 #include "pvamrwbdecoder_api.h"
118 #include "pvamrwbdecoder.h"
119 #include "pvamrwbdecoder_mem_funcs.h"
120 #include "pvamrwbdecoder_cnst.h"
121 #include "dtx.h"
122 #include "mime_io.h"
123
124 /*----------------------------------------------------------------------------
125 ; MACROS
126 ; Define module specific macros here
127 ----------------------------------------------------------------------------*/
128
129
130 /*----------------------------------------------------------------------------
131 ; DEFINES
132 ; Include all pre-processor statements here. Include conditional
133 ; compile variables also.
134 ----------------------------------------------------------------------------*/
135
136 #define MRSID 9
137
138 /*----------------------------------------------------------------------------
139 ; LOCAL FUNCTION DEFINITIONS
140 ; Function Prototype declaration
141 ----------------------------------------------------------------------------*/
142
143 /*----------------------------------------------------------------------------
144 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
145 ; Variable declaration - defined here and used outside this module
146 ----------------------------------------------------------------------------*/
147
148 const uint8 toc_byte[16] = {0x04, 0x0C, 0x14, 0x1C, 0x24, 0x2C, 0x34, 0x3C,
149                             0x44, 0x4C, 0x54, 0x5C, 0x64, 0x6C, 0x74, 0x7C
150                            };
151
152 /* number of speech bits for all modes */
153 const int16 unpacked_size[16] =
154 {
155     132,  177, 253, 285,
156     317,  365, 397, 461,
157     477,   35,   0,   0,
158     0,    0,   0,   0
159 };
160
161 /* size of packed frame for each mode, excluding TOC byte */
162 const int16 packed_size[16] = {17, 23, 32, 36, 40, 46, 50, 58,
163                                60,  5,  0,  0,  0,  0,  0,  0
164                               };
165
166 /* number of unused speech bits in packed format for each mode */
167 const int16 unused_size[16] = {4, 7, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 0};
168
169 /* sorting tables for all modes */
170
171 const int16 sort_660[132] =
172 {
173     0,   5,   6,   7,  61,  84, 107, 130,  62,  85,
174     8,   4,  37,  38,  39,  40,  58,  81, 104, 127,
175     60,  83, 106, 129, 108, 131, 128,  41,  42,  80,
176     126,   1,   3,  57, 103,  82, 105,  59,   2,  63,
177     109, 110,  86,  19,  22,  23,  64,  87,  18,  20,
178     21,  17,  13,  88,  43,  89,  65, 111,  14,  24,
179     25,  26,  27,  28,  15,  16,  44,  90,  66, 112,
180     9,  11,  10,  12,  67, 113,  29,  30,  31,  32,
181     34,  33,  35,  36,  45,  51,  68,  74,  91,  97,
182     114, 120,  46,  69,  92, 115,  52,  75,  98, 121,
183     47,  70,  93, 116,  53,  76,  99, 122,  48,  71,
184     94, 117,  54,  77, 100, 123,  49,  72,  95, 118,
185     55,  78, 101, 124,  50,  73,  96, 119,  56,  79,
186     102, 125
187 };
188
189 const int16 sort_885[177] =
190 {
191     0,   4,   6,   7,   5,   3,  47,  48,  49, 112,
192     113, 114,  75, 106, 140, 171,  80, 111, 145, 176,
193     77, 108, 142, 173,  78, 109, 143, 174,  79, 110,
194     144, 175,  76, 107, 141, 172,  50, 115,  51,   2,
195     1,  81, 116, 146,  19,  21,  12,  17,  18,  20,
196     16,  25,  13,  10,  14,  24,  23,  22,  26,   8,
197     15,  52, 117,  31,  82, 147,   9,  33,  11,  83,
198     148,  53, 118,  28,  27,  84, 149,  34,  35,  29,
199     46,  32,  30,  54, 119,  37,  36,  39,  38,  40,
200     85, 150,  41,  42,  43,  44,  45,  55,  60,  65,
201     70,  86,  91,  96, 101, 120, 125, 130, 135, 151,
202     156, 161, 166,  56,  87, 121, 152,  61,  92, 126,
203     157,  66,  97, 131, 162,  71, 102, 136, 167,  57,
204     88, 122, 153,  62,  93, 127, 158,  67,  98, 132,
205     163,  72, 103, 137, 168,  58,  89, 123, 154,  63,
206     94, 128, 159,  68,  99, 133, 164,  73, 104, 138,
207     169,  59,  90, 124, 155,  64,  95, 129, 160,  69,
208     100, 134, 165,  74, 105, 139, 170
209 };
210
211 const int16 sort_1265[253] =
212 {
213     0,   4,   6,  93, 143, 196, 246,   7,   5,   3,
214     47,  48,  49,  50,  51, 150, 151, 152, 153, 154,
215     94, 144, 197, 247,  99, 149, 202, 252,  96, 146,
216     199, 249,  97, 147, 200, 250, 100, 203,  98, 148,
217     201, 251,  95, 145, 198, 248,  52,   2,   1, 101,
218     204, 155,  19,  21,  12,  17,  18,  20,  16,  25,
219     13,  10,  14,  24,  23,  22,  26,   8,  15,  53,
220     156,  31, 102, 205,   9,  33,  11, 103, 206,  54,
221     157,  28,  27, 104, 207,  34,  35,  29,  46,  32,
222     30,  55, 158,  37,  36,  39,  38,  40, 105, 208,
223     41,  42,  43,  44,  45,  56, 106, 159, 209,  57,
224     66,  75,  84, 107, 116, 125, 134, 160, 169, 178,
225     187, 210, 219, 228, 237,  58, 108, 161, 211,  62,
226     112, 165, 215,  67, 117, 170, 220,  71, 121, 174,
227     224,  76, 126, 179, 229,  80, 130, 183, 233,  85,
228     135, 188, 238,  89, 139, 192, 242,  59, 109, 162,
229     212,  63, 113, 166, 216,  68, 118, 171, 221,  72,
230     122, 175, 225,  77, 127, 180, 230,  81, 131, 184,
231     234,  86, 136, 189, 239,  90, 140, 193, 243,  60,
232     110, 163, 213,  64, 114, 167, 217,  69, 119, 172,
233     222,  73, 123, 176, 226,  78, 128, 181, 231,  82,
234     132, 185, 235,  87, 137, 190, 240,  91, 141, 194,
235     244,  61, 111, 164, 214,  65, 115, 168, 218,  70,
236     120, 173, 223,  74, 124, 177, 227,  79, 129, 182,
237     232,  83, 133, 186, 236,  88, 138, 191, 241,  92,
238     142, 195, 245
239 };
240
241 const int16 sort_1425[285] =
242 {
243     0,   4,   6, 101, 159, 220, 278,   7,   5,   3,
244     47,  48,  49,  50,  51, 166, 167, 168, 169, 170,
245     102, 160, 221, 279, 107, 165, 226, 284, 104, 162,
246     223, 281, 105, 163, 224, 282, 108, 227, 106, 164,
247     225, 283, 103, 161, 222, 280,  52,   2,   1, 109,
248     228, 171,  19,  21,  12,  17,  18,  20,  16,  25,
249     13,  10,  14,  24,  23,  22,  26,   8,  15,  53,
250     172,  31, 110, 229,   9,  33,  11, 111, 230,  54,
251     173,  28,  27, 112, 231,  34,  35,  29,  46,  32,
252     30,  55, 174,  37,  36,  39,  38,  40, 113, 232,
253     41,  42,  43,  44,  45,  56, 114, 175, 233,  62,
254     120, 181, 239,  75, 133, 194, 252,  57, 115, 176,
255     234,  63, 121, 182, 240,  70, 128, 189, 247,  76,
256     134, 195, 253,  83, 141, 202, 260,  92, 150, 211,
257     269,  84, 142, 203, 261,  93, 151, 212, 270,  85,
258     143, 204, 262,  94, 152, 213, 271,  86, 144, 205,
259     263,  95, 153, 214, 272,  64, 122, 183, 241,  77,
260     135, 196, 254,  65, 123, 184, 242,  78, 136, 197,
261     255,  87, 145, 206, 264,  96, 154, 215, 273,  58,
262     116, 177, 235,  66, 124, 185, 243,  71, 129, 190,
263     248,  79, 137, 198, 256,  88, 146, 207, 265,  97,
264     155, 216, 274,  59, 117, 178, 236,  67, 125, 186,
265     244,  72, 130, 191, 249,  80, 138, 199, 257,  89,
266     147, 208, 266,  98, 156, 217, 275,  60, 118, 179,
267     237,  68, 126, 187, 245,  73, 131, 192, 250,  81,
268     139, 200, 258,  90, 148, 209, 267,  99, 157, 218,
269     276,  61, 119, 180, 238,  69, 127, 188, 246,  74,
270     132, 193, 251,  82, 140, 201, 259,  91, 149, 210,
271     268, 100, 158, 219, 277
272 };
273
274 const int16 sort_1585[317] =
275 {
276     0,   4,   6, 109, 175, 244, 310,   7,   5,   3,
277     47,  48,  49,  50,  51, 182, 183, 184, 185, 186,
278     110, 176, 245, 311, 115, 181, 250, 316, 112, 178,
279     247, 313, 113, 179, 248, 314, 116, 251, 114, 180,
280     249, 315, 111, 177, 246, 312,  52,   2,   1, 117,
281     252, 187,  19,  21,  12,  17,  18,  20,  16,  25,
282     13,  10,  14,  24,  23,  22,  26,   8,  15,  53,
283     188,  31, 118, 253,   9,  33,  11, 119, 254,  54,
284     189,  28,  27, 120, 255,  34,  35,  29,  46,  32,
285     30,  55, 190,  37,  36,  39,  38,  40, 121, 256,
286     41,  42,  43,  44,  45,  56, 122, 191, 257,  63,
287     129, 198, 264,  76, 142, 211, 277,  89, 155, 224,
288     290, 102, 168, 237, 303,  57, 123, 192, 258,  70,
289     136, 205, 271,  83, 149, 218, 284,  96, 162, 231,
290     297,  62, 128, 197, 263,  75, 141, 210, 276,  88,
291     154, 223, 289, 101, 167, 236, 302,  58, 124, 193,
292     259,  71, 137, 206, 272,  84, 150, 219, 285,  97,
293     163, 232, 298,  59, 125, 194, 260,  64, 130, 199,
294     265,  67, 133, 202, 268,  72, 138, 207, 273,  77,
295     143, 212, 278,  80, 146, 215, 281,  85, 151, 220,
296     286,  90, 156, 225, 291,  93, 159, 228, 294,  98,
297     164, 233, 299, 103, 169, 238, 304, 106, 172, 241,
298     307,  60, 126, 195, 261,  65, 131, 200, 266,  68,
299     134, 203, 269,  73, 139, 208, 274,  78, 144, 213,
300     279,  81, 147, 216, 282,  86, 152, 221, 287,  91,
301     157, 226, 292,  94, 160, 229, 295,  99, 165, 234,
302     300, 104, 170, 239, 305, 107, 173, 242, 308,  61,
303     127, 196, 262,  66, 132, 201, 267,  69, 135, 204,
304     270,  74, 140, 209, 275,  79, 145, 214, 280,  82,
305     148, 217, 283,  87, 153, 222, 288,  92, 158, 227,
306     293,  95, 161, 230, 296, 100, 166, 235, 301, 105,
307     171, 240, 306, 108, 174, 243, 309
308 };
309
310 const int16 sort_1825[365] =
311 {
312     0,   4,   6, 121, 199, 280, 358,   7,   5,   3,
313     47,  48,  49,  50,  51, 206, 207, 208, 209, 210,
314     122, 200, 281, 359, 127, 205, 286, 364, 124, 202,
315     283, 361, 125, 203, 284, 362, 128, 287, 126, 204,
316     285, 363, 123, 201, 282, 360,  52,   2,   1, 129,
317     288, 211,  19,  21,  12,  17,  18,  20,  16,  25,
318     13,  10,  14,  24,  23,  22,  26,   8,  15,  53,
319     212,  31, 130, 289,   9,  33,  11, 131, 290,  54,
320     213,  28,  27, 132, 291,  34,  35,  29,  46,  32,
321     30,  55, 214,  37,  36,  39,  38,  40, 133, 292,
322     41,  42,  43,  44,  45,  56, 134, 215, 293, 198,
323     299, 136, 120, 138,  60, 279,  58,  62, 357, 139,
324     140, 295, 156,  57, 219, 297,  63, 217, 137, 170,
325     300, 222,  64, 106,  61,  78, 294,  92, 142, 141,
326     135, 221, 296, 301, 343,  59, 298, 184, 329, 315,
327     220, 216, 265, 251, 218, 237, 352, 223, 157,  86,
328     171,  87, 164, 351, 111, 302,  65, 178, 115, 323,
329     72, 192, 101, 179,  93,  73, 193, 151, 337, 309,
330     143, 274,  69, 324, 165, 150,  97, 338, 110, 310,
331     330, 273,  68, 107, 175, 245, 114,  79, 113, 189,
332     246, 259, 174,  71, 185,  96, 344, 100, 322,  83,
333     334, 316, 333, 252, 161, 348, 147,  82, 269, 232,
334     260, 308, 353, 347, 163, 231, 306, 320, 188, 270,
335     146, 177, 266, 350, 256,  85, 149, 116, 191, 160,
336     238, 258, 336, 305, 255,  88, 224,  99, 339, 230,
337     228, 227, 272, 242, 241, 319, 233, 311, 102,  74,
338     180, 275,  66, 194, 152, 325, 172, 247, 244, 261,
339     117, 158, 166, 354,  75, 144, 108, 312,  94, 186,
340     303,  80, 234,  89, 195, 112, 340, 181, 345, 317,
341     326, 276, 239, 167, 118, 313,  70, 355, 327, 253,
342     190, 176, 271, 104,  98, 153, 103,  90,  76, 267,
343     277, 248, 225, 262, 182,  84, 154, 235, 335, 168,
344     331, 196, 341, 249, 162, 307, 148, 349, 263, 321,
345     257, 243, 229, 356, 159, 119,  67, 187, 173, 145,
346     240,  77, 304, 332, 314, 342, 109, 254,  81, 278,
347     105,  91, 346, 318, 183, 250, 197, 328,  95, 155,
348     169, 268, 226, 236, 264
349 };
350
351 const int16 sort_1985[397] =
352 {
353     0,   4,   6, 129, 215, 304, 390,   7,   5,   3,
354     47,  48,  49,  50,  51, 222, 223, 224, 225, 226,
355     130, 216, 305, 391, 135, 221, 310, 396, 132, 218,
356     307, 393, 133, 219, 308, 394, 136, 311, 134, 220,
357     309, 395, 131, 217, 306, 392,  52,   2,   1, 137,
358     312, 227,  19,  21,  12,  17,  18,  20,  16,  25,
359     13,  10,  14,  24,  23,  22,  26,   8,  15,  53,
360     228,  31, 138, 313,   9,  33,  11, 139, 314,  54,
361     229,  28,  27, 140, 315,  34,  35,  29,  46,  32,
362     30,  55, 230,  37,  36,  39,  38,  40, 141, 316,
363     41,  42,  43,  44,  45,  56, 142, 231, 317,  63,
364     73,  92, 340,  82, 324, 149, 353, 159, 334, 165,
365     338, 178, 163, 254,  77, 168, 257, 153, 343,  57,
366     248, 238,  79, 252, 166,  67,  80, 201, 101, 267,
367     143, 164, 341, 255, 339, 187, 376, 318,  78, 328,
368     362, 115, 232, 242, 253, 290, 276,  62,  58, 158,
369     68,  93, 179, 319, 148, 169, 154,  72, 385, 329,
370     333, 344, 102,  83, 144, 233, 323, 124, 243, 192,
371     354, 237,  64, 247, 202, 209, 150, 116, 335, 268,
372     239, 299, 188, 196, 298,  94, 195, 258, 123, 363,
373     384, 109, 325, 371, 170, 370,  84, 110, 295, 180,
374     74, 210, 191, 106, 291, 205, 367, 381, 377, 206,
375     355, 122, 119, 120, 383, 160, 105, 108, 277, 380,
376     294, 284, 285, 345, 208, 269, 249, 366, 386, 300,
377     297, 259, 125, 369, 197,  97, 194, 286, 211, 281,
378     280, 183, 372,  87, 155, 283,  59, 348, 327, 184,
379     76, 111, 330, 203, 349,  69,  98, 152, 145, 189,
380     66, 320, 337, 173, 358, 251, 198, 174, 263, 262,
381     126, 241, 193,  88, 388, 117,  95, 387, 112, 359,
382     287, 244, 103, 272, 301, 171, 162, 234, 273, 127,
383     373, 181, 292,  85, 378, 302, 121, 107, 364, 346,
384     356, 212, 278, 213,  65, 382, 288, 207, 113, 175,
385     99, 296, 374, 368, 199, 260, 185, 336, 331, 161,
386     270, 264, 250, 240,  75, 350, 151,  60,  89, 321,
387     156, 274, 360, 326,  70, 282, 167, 146, 352,  81,
388     91, 389, 266, 245, 177, 235, 190, 256, 204, 342,
389     128, 118, 303, 104, 379, 182, 114, 375, 200,  96,
390     293, 172, 214, 365, 279,  86, 289, 351, 347, 357,
391     261, 186, 176, 271,  90, 100, 147, 322, 275, 361,
392     71, 332,  61, 265, 157, 246, 236
393 };
394
395 const int16 sort_2305[461] =
396 {
397     0,   4,   6, 145, 247, 352, 454,   7,   5,   3,
398     47,  48,  49,  50,  51, 254, 255, 256, 257, 258,
399     146, 248, 353, 455, 151, 253, 358, 460, 148, 250,
400     355, 457, 149, 251, 356, 458, 152, 359, 150, 252,
401     357, 459, 147, 249, 354, 456,  52,   2,   1, 153,
402     360, 259,  19,  21,  12,  17,  18,  20,  16,  25,
403     13,  10,  14,  24,  23,  22,  26,   8,  15,  53,
404     260,  31, 154, 361,   9,  33,  11, 155, 362,  54,
405     261,  28,  27, 156, 363,  34,  35,  29,  46,  32,
406     30,  55, 262,  37,  36,  39,  38,  40, 157, 364,
407     41,  42,  43,  44,  45,  56, 158, 263, 365, 181,
408     192, 170,  79,  57, 399,  90, 159, 297, 377, 366,
409     275,  68, 183, 388, 286, 194, 299, 92 ,  70, 182,
410     401, 172,  59,  91,  58, 400, 368, 161,  81, 160,
411     264, 171,  80, 389, 390, 378, 379, 193, 298,  69,
412     266, 265, 367, 277, 288, 276, 287, 184,  60, 195,
413     82,  93,  71, 369, 402, 173, 162, 444, 300, 391,
414     98,  76, 278,  61, 267, 374, 135, 411, 167, 102,
415     380, 200,  87, 178,  65,  94, 204, 124,  72, 342,
416     189, 305, 381, 396, 433, 301, 226, 407, 289, 237,
417     113, 215, 185, 128, 309, 403, 116, 320, 196, 331,
418     370, 422, 174,  64, 392,  83, 425, 219, 134, 188,
419     432, 112, 427, 139, 279, 163, 436, 208, 447, 218,
420     236, 229,  97, 294, 385, 230, 166, 268, 177, 443,
421     225, 426, 101, 272, 138, 127, 290, 117, 347, 199,
422     414,  95, 140, 240, 410, 395, 209, 129, 283, 346,
423     105, 241, 437,  86, 308, 448, 203, 345, 186, 107,
424     220, 415, 334, 319, 106, 313, 118, 123,  73, 207,
425     421, 214, 384, 373, 438,  62, 371, 341,  75, 449,
426     168, 323, 164, 242, 416, 324, 304, 197, 335, 404,
427     271,  63, 191, 325,  96, 169, 231, 280, 312, 187,
428     406,  84, 201, 100,  67, 382, 175, 336, 202, 330,
429     269, 393, 376, 383, 293, 307, 409, 179, 285, 314,
430     302, 372, 398, 190, 180,  89,  99, 103, 232,  78,
431     88,  77, 136, 387, 165, 198, 394, 125, 176, 428,
432     74, 375, 238, 227,  66, 273, 282, 141, 306, 412,
433     114,  85, 130, 348, 119, 291, 296, 386, 233, 397,
434     303, 405, 284, 445, 423, 221, 210, 205, 450, 108,
435     274, 434, 216, 343, 337, 142, 243, 321, 408, 451,
436     310, 292, 120, 109, 281, 439, 270, 429, 332, 295,
437     418, 211, 315, 222, 326, 131, 430, 244, 327, 349,
438     417, 316, 143, 338, 440, 234, 110, 212, 452, 245,
439     121, 419, 350, 223, 132, 441, 328, 413, 317, 339,
440     126, 104, 137, 446, 344, 239, 435, 115, 333, 206,
441     322, 217, 228, 424, 453, 311, 351, 111, 442, 224,
442     213, 122, 431, 340, 235, 246, 133, 144, 420, 329,
443     318
444 };
445
446 const int16 sort_2385[477] =
447 {
448     0,   4,   6, 145, 251, 360, 466,   7,   5,   3,
449     47,  48,  49,  50,  51, 262, 263, 264, 265, 266,
450     146, 252, 361, 467, 151, 257, 366, 472, 148, 254,
451     363, 469, 149, 255, 364, 470, 156, 371, 150, 256,
452     365, 471, 147, 253, 362, 468,  52,   2,   1, 157,
453     372, 267,  19,  21,  12,  17,  18,  20,  16,  25,
454     13,  10,  14,  24,  23,  22,  26,   8,  15,  53,
455     268,  31, 152, 153, 154, 155, 258, 259, 260, 261,
456     367, 368, 369, 370, 473, 474, 475, 476, 158, 373,
457     9,  33,  11, 159, 374,  54, 269,  28,  27, 160,
458     375,  34,  35,  29,  46,  32,  30,  55, 270, 37,
459     36,  39,  38,  40, 161, 376,  41,  42,  43,  44,
460     45,  56, 162, 271, 377, 185, 196, 174,  79,  57,
461     411,  90, 163, 305, 389, 378, 283,  68, 187, 400,
462     294, 198, 307,  92,  70, 186, 413, 176,  59,  91,
463     58, 412, 380, 165,  81, 164, 272, 175,  80, 401,
464     402, 390, 391, 197, 306,  69, 274, 273, 379, 285,
465     296, 284, 295, 188,  60, 199,  82,  93,  71, 381,
466     414, 177, 166, 456, 308, 403,  98,  76, 286,  61,
467     275, 386, 135, 423, 171, 102, 392, 204,  87, 182,
468     65,  94, 208, 124,  72, 350, 193, 313, 393, 408,
469     445, 309, 230, 419, 297, 241, 113, 219, 189, 128,
470     317, 415, 116, 328, 200, 339, 382, 434, 178,  64,
471     404,  83, 437, 223, 134, 192, 444, 112, 439, 139,
472     287, 167, 448, 212, 459, 222, 240, 233,  97, 302,
473     397, 234, 170, 276, 181, 455, 229, 438, 101, 280,
474     138, 127, 298, 117, 355, 203, 426,  95, 140, 244,
475     422, 407, 213, 129, 291, 354, 105, 245, 449,  86,
476     316, 460, 207, 353, 190, 107, 224, 427, 342, 327,
477     106, 321, 118, 123,  73, 211, 433, 218, 396, 385,
478     450,  62, 383, 349,  75, 461, 172, 331, 168, 246,
479     428, 332, 312, 201, 343, 416, 279,  63, 195, 333,
480     96, 173, 235, 288, 320, 191, 418,  84, 205, 100,
481     67, 394, 179, 344, 206, 338, 277, 405, 388, 395,
482     301, 315, 421, 183, 293, 322, 310, 384, 410, 194,
483     184,  89,  99, 103, 236,  78,  88,  77, 136, 399,
484     169, 202, 406, 125, 180, 440,  74, 387, 242, 231,
485     66, 281, 290, 141, 314, 424, 114,  85, 130, 356,
486     119, 299, 304, 398, 237, 409, 311, 417, 292, 457,
487     435, 225, 214, 209, 462, 108, 282, 446, 220, 351,
488     345, 142, 247, 329, 420, 463, 318, 300, 120, 109,
489     289, 451, 278, 441, 340, 303, 430, 215, 323, 226,
490     334, 131, 442, 248, 335, 357, 429, 324, 143, 346,
491     452, 238, 110, 216, 464, 249, 121, 431, 358, 227,
492     132, 453, 336, 425, 325, 347, 126, 104, 137, 458,
493     352, 243, 447, 115, 341, 210, 330, 221, 232, 436,
494     465, 319, 359, 111, 454, 228, 217, 122, 443, 348,
495     239, 250, 133, 144, 432, 337, 326
496 };
497
498 const int16 sort_SID[35] =
499 {
500     0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
501     10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
502     20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
503     30, 31, 32, 33, 34
504 };
505
506
507 /*----------------------------------------------------------------------------
508 ; EXTERNAL FUNCTION REFERENCES
509 ; Declare functions defined elsewhere and referenced in this module
510 ----------------------------------------------------------------------------*/
511
512 /*----------------------------------------------------------------------------
513 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
514 ; Declare variables used in this module but defined elsewhere
515 ----------------------------------------------------------------------------*/
516
517 /*----------------------------------------------------------------------------
518 ; FUNCTION CODE
519 ----------------------------------------------------------------------------*/
520
521 void mime_unsorting(uint8 unsorted_bits[],
522                     int16 sorted_bits_into_int16[],
523                     int16 * frame_type,
524                     int16 * mode,
525                     uint8 quality,
526                     RX_State *st)
527 {
528
529     int16 i;
530     int16 j;
531     uint8 temp = 0;
532     uint8 *unsorted_bits_ptr = (uint8*)unsorted_bits;
533
534     /* pointer table for bit sorting tables */
535     const int16 *AmrWbSortingTables[16] =
536     {
537         sort_660,   sort_885, sort_1265, sort_1425,
538         sort_1585,  sort_1825, sort_1985, sort_2305,
539         sort_2385,   sort_SID,      NULL,      NULL,
540         NULL,       NULL,      NULL,      NULL
541     };
542
543     const int16 * pt_AmrWbSortingTables  = AmrWbSortingTables[*mode];
544
545     /* clear compressed speech bit buffer */
546     pv_memset(sorted_bits_into_int16,
547               0,
548               unpacked_size[*mode]*sizeof(*sorted_bits_into_int16));
549
550     /* unpack and unsort speech or SID bits */
551
552
553     for (i = unpacked_size[*mode] >> 3; i != 0; i--)
554     {
555         temp = *(unsorted_bits_ptr++);
556
557         for (j = 2; j != 0; j--)
558         {
559             switch (temp & 0xf0)
560             {
561                 case 0xf0:
562                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
563                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
564                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
565                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
566                     break;
567                 case 0xe0:
568                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
569                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
570                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
571                     pt_AmrWbSortingTables++;
572                     break;
573                 case 0xd0:
574                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
575                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
576                     pt_AmrWbSortingTables++;
577                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
578                     break;
579                 case 0xc0:
580                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
581                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
582                     pt_AmrWbSortingTables += 2;
583                     break;
584                 case 0xb0:
585                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
586                     pt_AmrWbSortingTables++;
587                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
588                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
589                     break;
590                 case 0xa0:
591                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
592                     pt_AmrWbSortingTables++;
593                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
594                     pt_AmrWbSortingTables++;
595                     break;
596                 case 0x90:
597                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
598                     pt_AmrWbSortingTables += 2;
599                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
600                     break;
601                 case 0x80:
602                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
603                     pt_AmrWbSortingTables += 3;
604                     break;
605                 case 0x70:
606                     pt_AmrWbSortingTables++;
607                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
608                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
609                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
610                     break;
611                 case 0x60:
612                     pt_AmrWbSortingTables++;
613                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
614                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
615                     pt_AmrWbSortingTables++;
616                     break;
617                 case 0x50:
618                     pt_AmrWbSortingTables++;
619                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
620                     pt_AmrWbSortingTables++;
621                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
622                     break;
623                 case 0x40:
624                     pt_AmrWbSortingTables++;
625                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
626                     pt_AmrWbSortingTables += 2;
627                     break;
628                 case 0x30:
629                     pt_AmrWbSortingTables += 2;
630                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
631                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
632                     break;
633                 case 0x20:
634                     pt_AmrWbSortingTables += 2;
635                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
636                     pt_AmrWbSortingTables++;
637                     break;
638                 case 0x10:
639                     pt_AmrWbSortingTables += 3;
640                     sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
641                     break;
642                 default:
643                     pt_AmrWbSortingTables += 4;
644                     break;
645             }
646             temp <<= 4;
647         }
648     }
649
650     if (unpacked_size[*mode] % 4)
651     {
652         temp <<= 1;
653
654         if (temp & 0x80)
655         {
656             sorted_bits_into_int16[*(pt_AmrWbSortingTables++)] = BIT_1;
657         }
658     }
659
660     /* set frame type */
661     switch (*mode)
662     {
663         case MODE_7k:
664         case MODE_9k:
665         case MODE_12k:
666         case MODE_14k:
667         case MODE_16k:
668         case MODE_18k:
669         case MODE_20k:
670         case MODE_23k:
671         case MODE_24k:
672             if (quality)
673             {
674                 *frame_type = RX_SPEECH_GOOD;
675             }
676             else
677             {
678                 *frame_type = RX_SPEECH_BAD;
679             }
680             break;
681
682         case MRSID:
683             if (quality)
684             {
685                 if (temp & 0x80)
686                 {
687                     *frame_type = RX_SID_UPDATE;
688                 }
689                 else
690                 {
691                     *frame_type = RX_SID_FIRST;
692                 }
693             }
694             else
695             {
696                 *frame_type = RX_SID_BAD;
697             }
698
699             /* set mode index */
700             *mode = st->prev_mode;
701             break;
702         case 14:        /* SPEECH_LOST */
703             *frame_type = RX_SPEECH_LOST;
704             *mode = st->prev_mode;
705             break;
706         case 15:        /* NO_DATA */
707             *frame_type = RX_NO_DATA;
708             *mode = st->prev_mode;
709             break;
710         default:        /* replace frame with unused mode index by NO_DATA frame */
711             *frame_type = RX_NO_DATA;
712             *mode = st->prev_mode;
713             break;
714     }
715
716     st->prev_mode = *mode;
717
718 }
719
720
721