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