OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [mp3/] [sw/] [mad-xess/] [libmad/] [layer3.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 291 simons
/*
2
 * mad - MPEG audio decoder
3
 * Copyright (C) 2000-2001 Robert Leslie
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 *
19
 * $Id: layer3.c,v 1.3 2001-11-06 17:01:28 simons Exp $
20
 */
21
 
22
# ifdef HAVE_CONFIG_H
23
#  include "config.h"
24
# endif
25
 
26
# include "global.h"
27
 
28
# ifndef EMBED
29
#  include <string.h>
30
#  include <assert.h>
31
#  include <stdio.h>
32
#  ifdef HAVE_LIMITS_H
33
#   include <limits.h>
34
#  else
35
#   define CHAR_BIT  8
36
#  endif
37
#else
38
 
39
# define assert(x)
40
# define CHAR_BIT  8
41
#endif
42
 
43
# include "fixed.h"
44
# include "bit.h"
45
# include "stream.h"
46
# include "frame.h"
47
# include "huffman.h"
48
# include "layer3.h"
49
 
50
/* --- Layer III ----------------------------------------------------------- */
51
 
52
enum {
53
  count1table_select = 0x01,
54
  scalefac_scale     = 0x02,
55
  preflag            = 0x04,
56
  mixed_block_flag   = 0x08
57
};
58
 
59
struct sideinfo {
60
  unsigned int main_data_begin;
61
  unsigned int private_bits;
62
 
63
  unsigned char scfsi[2];
64
 
65
  struct granule {
66
    struct channel {
67
      /* from side info */
68
      unsigned short part2_3_length;
69
      unsigned short big_values;
70
      unsigned short global_gain;
71
      unsigned short scalefac_compress;
72
 
73
      unsigned char flags;
74
      unsigned char block_type;
75
      unsigned char table_select[3];
76
      unsigned char subblock_gain[3];
77
      unsigned char region0_count;
78
      unsigned char region1_count;
79
 
80
      /* from main_data */
81
      unsigned char scalefac[39];       /* scalefac_l and/or scalefac_s */
82
    } ch[2];
83
  } gr[2];
84
};
85
 
86
/*
87
 * scalefactor bit lengths
88
 * derived from section 2.4.2.7 of ISO/IEC 11172-3
89
 */
90
static
91
struct {
92
  unsigned char slen1;
93
  unsigned char slen2;
94
} const sflen_table[16] = {
95
  { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 },
96
  { 3, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 },
97
  { 2, 1 }, { 2, 2 }, { 2, 3 }, { 3, 1 },
98
  { 3, 2 }, { 3, 3 }, { 4, 2 }, { 4, 3 }
99
};
100
 
101
/*
102
 * number of LSF scalefactor band values
103
 * derived from section 2.4.3.2 of ISO/IEC 13818-3
104
 */
105
static
106
unsigned char const nsfb_table[6][3][4] = {
107
  { {  6,  5,  5, 5 },
108
    {  9,  9,  9, 9 },
109
    {  6,  9,  9, 9 } },
110
 
111
  { {  6,  5,  7, 3 },
112
    {  9,  9, 12, 6 },
113
    {  6,  9, 12, 6 } },
114
 
115
  { { 11, 10,  0, 0 },
116
    { 18, 18,  0, 0 },
117
    { 15, 18,  0, 0 } },
118
 
119
  { {  7,  7,  7, 0 },
120
    { 12, 12, 12, 0 },
121
    {  6, 15, 12, 0 } },
122
 
123
  { {  6,  6,  6, 3 },
124
    { 12,  9,  9, 6 },
125
    {  6, 12,  9, 6 } },
126
 
127
  { {  8,  8,  5, 0 },
128
    { 15, 12,  9, 0 },
129
    {  6, 18,  9, 0 } }
130
};
131
 
132
/*
133
 * MPEG-1 scalefactor band widths
134
 * derived from Table B.8 of ISO/IEC 11172-3
135
 */
136
static
137
unsigned char const sfb_48000_long[] = {
138
   4,  4,  4,  4,  4,  4,  6,  6,  6,   8,  10,
139
  12, 16, 18, 22, 28, 34, 40, 46, 54,  54, 192
140
};
141
 
142
static
143
unsigned char const sfb_44100_long[] = {
144
   4,  4,  4,  4,  4,  4,  6,  6,  8,   8,  10,
145
  12, 16, 20, 24, 28, 34, 42, 50, 54,  76, 158
146
};
147
 
148
static
149
unsigned char const sfb_32000_long[] = {
150
   4,  4,  4,  4,  4,  4,  6,  6,  8,  10,  12,
151
  16, 20, 24, 30, 38, 46, 56, 68, 84, 102,  26
152
};
153
 
154
static
155
unsigned char const sfb_48000_short[] = {
156
   4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  6,
157
   6,  6,  6,  6,  6, 10, 10, 10, 12, 12, 12, 14, 14,
158
  14, 16, 16, 16, 20, 20, 20, 26, 26, 26, 66, 66, 66
159
};
160
 
161
static
162
unsigned char const sfb_44100_short[] = {
163
   4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  6,
164
   6,  6,  8,  8,  8, 10, 10, 10, 12, 12, 12, 14, 14,
165
  14, 18, 18, 18, 22, 22, 22, 30, 30, 30, 56, 56, 56
166
};
167
 
168
static
169
unsigned char const sfb_32000_short[] = {
170
   4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  6,
171
   6,  6,  8,  8,  8, 12, 12, 12, 16, 16, 16, 20, 20,
172
  20, 26, 26, 26, 34, 34, 34, 42, 42, 42, 12, 12, 12
173
};
174
 
175
static
176
unsigned char const sfb_48000_mixed[] = {
177
  /* long */   4,  4,  4,  4,  4,  4,  6,  6,
178
  /* short */  4,  4,  4,  6,  6,  6,  6,  6,  6, 10,
179
              10, 10, 12, 12, 12, 14, 14, 14, 16, 16,
180
              16, 20, 20, 20, 26, 26, 26, 66, 66, 66
181
};
182
 
183
static
184
unsigned char const sfb_44100_mixed[] = {
185
  /* long */   4,  4,  4,  4,  4,  4,  6,  6,
186
  /* short */  4,  4,  4,  6,  6,  6,  8,  8,  8, 10,
187
              10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
188
              18, 22, 22, 22, 30, 30, 30, 56, 56, 56
189
};
190
 
191
static
192
unsigned char const sfb_32000_mixed[] = {
193
  /* long */   4,  4,  4,  4,  4,  4,  6,  6,
194
  /* short */  4,  4,  4,  6,  6,  6,  8,  8,  8, 12,
195
              12, 12, 16, 16, 16, 20, 20, 20, 26, 26,
196
              26, 34, 34, 34, 42, 42, 42, 12, 12, 12
197
};
198
 
199
/*
200
 * MPEG-2 scalefactor band widths
201
 * derived from Table B.2 of ISO/IEC 13818-3
202
 */
203
static
204
unsigned char const sfb_24000_long[] = {
205
   6,  6,  6,  6,  6,  6,  8, 10, 12,  14,  16,
206
  18, 22, 26, 32, 38, 46, 54, 62, 70,  76,  36
207
};
208
 
209
static
210
unsigned char const sfb_22050_long[] = {
211
   6,  6,  6,  6,  6,  6,  8, 10, 12,  14,  16,
212
  20, 24, 28, 32, 38, 46, 52, 60, 68,  58,  54
213
};
214
 
215
# define sfb_16000_long  sfb_22050_long
216
 
217
static
218
unsigned char const sfb_24000_short[] = {
219
   4,  4,  4,  4,  4,  4,  4,  4,  4,  6,  6,  6,  8,
220
   8,  8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
221
  18, 24, 24, 24, 32, 32, 32, 44, 44, 44, 12, 12, 12
222
};
223
 
224
static
225
unsigned char const sfb_22050_short[] = {
226
   4,  4,  4,  4,  4,  4,  4,  4,  4,  6,  6,  6,  6,
227
   6,  6,  8,  8,  8, 10, 10, 10, 14, 14, 14, 18, 18,
228
  18, 26, 26, 26, 32, 32, 32, 42, 42, 42, 18, 18, 18
229
};
230
 
231
static
232
unsigned char const sfb_16000_short[] = {
233
   4,  4,  4,  4,  4,  4,  4,  4,  4,  6,  6,  6,  8,
234
   8,  8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
235
  18, 24, 24, 24, 30, 30, 30, 40, 40, 40, 18, 18, 18
236
};
237
 
238
static
239
unsigned char const sfb_24000_mixed[] = {
240
  /* long */   6,  6,  6,  6,  6,  6,
241
  /* short */  6,  6,  6,  8,  8,  8, 10, 10, 10, 12,
242
              12, 12, 14, 14, 14, 18, 18, 18, 24, 24,
243
              24, 32, 32, 32, 44, 44, 44, 12, 12, 12
244
};
245
 
246
static
247
unsigned char const sfb_22050_mixed[] = {
248
  /* long */   6,  6,  6,  6,  6,  6,
249
  /* short */  6,  6,  6,  6,  6,  6,  8,  8,  8, 10,
250
              10, 10, 14, 14, 14, 18, 18, 18, 26, 26,
251
              26, 32, 32, 32, 42, 42, 42, 18, 18, 18
252
};
253
 
254
static
255
unsigned char const sfb_16000_mixed[] = {
256
  /* long */   6,  6,  6,  6,  6,  6,
257
  /* short */  6,  6,  6,  8,  8,  8, 10, 10, 10, 12,
258
              12, 12, 14, 14, 14, 18, 18, 18, 24, 24,
259
              24, 30, 30, 30, 40, 40, 40, 18, 18, 18
260
};
261
 
262
/*
263
 * MPEG 2.5 scalefactor band widths
264
 * derived from public sources
265
 */
266
# define sfb_12000_long  sfb_16000_long
267
# define sfb_11025_long  sfb_12000_long
268
 
269
static
270
unsigned char const sfb_8000_long[] = {
271
  12, 12, 12, 12, 12, 12, 16, 20, 24,  28,  32,
272
  40, 48, 56, 64, 76, 90,  2,  2,  2,   2,   2
273
};
274
 
275
# define sfb_12000_short  sfb_16000_short
276
# define sfb_11025_short  sfb_12000_short
277
 
278
static
279
unsigned char const sfb_8000_short[] = {
280
   8,  8,  8,  8,  8,  8,  8,  8,  8, 12, 12, 12, 16,
281
  16, 16, 20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36,
282
  36,  2,  2,  2,  2,  2,  2,  2,  2,  2, 26, 26, 26
283
};
284
 
285
# define sfb_12000_mixed  sfb_16000_mixed
286
# define sfb_11025_mixed  sfb_12000_mixed
287
 
288
/* the 8000 Hz short block scalefactor bands do not break after the first 36
289
   frequency lines, so this is probably wrong */
290
static
291
unsigned char const sfb_8000_mixed[] = {
292
  /* long */  12, 12, 12,
293
  /* short */  4,  4,  4,  8,  8,  8, 12, 12, 12, 16, 16, 16,
294
              20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36, 36,
295
               2,  2,  2,  2,  2,  2,  2,  2,  2, 26, 26, 26
296
};
297
 
298
static
299
struct {
300
  unsigned char const *l;
301
  unsigned char const *s;
302
  unsigned char const *m;
303
} const sfbwidth_table[9] = {
304
  { sfb_48000_long, sfb_48000_short, sfb_48000_mixed },
305
  { sfb_44100_long, sfb_44100_short, sfb_44100_mixed },
306
  { sfb_32000_long, sfb_32000_short, sfb_32000_mixed },
307
  { sfb_24000_long, sfb_24000_short, sfb_24000_mixed },
308
  { sfb_22050_long, sfb_22050_short, sfb_22050_mixed },
309
  { sfb_16000_long, sfb_16000_short, sfb_16000_mixed },
310
  { sfb_12000_long, sfb_12000_short, sfb_12000_mixed },
311
  { sfb_11025_long, sfb_11025_short, sfb_11025_mixed },
312
  {  sfb_8000_long,  sfb_8000_short,  sfb_8000_mixed }
313
};
314
 
315
/*
316
 * scalefactor band preemphasis (used only when preflag is set)
317
 * derived from Table B.6 of ISO/IEC 11172-3
318
 */
319
static
320
unsigned char const pretab[22] = {
321
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0
322
};
323
 
324
/*
325
 * table for requantization
326
 *
327
 * rq_table[x].mantissa * 2^(rq_table[x].exponent) = x^(4/3)
328
 * MM: shortened table
329
 */
330
#define RQ_START_BITS 7
331
static
332
struct fixedfloat {
333
  unsigned long mantissa  : 27;
334
  unsigned short exponent :  5;
335
} const rq_table[512] = {
336
# include "rq_short.dat"
337
};
338
 
339
/*
340
 * fractional powers of two
341
 * used for requantization and joint stereo decoding
342
 *
343
 * root_table[3 + x] = 2^(x/4)
344
 */
345
static
346
mad_fixed_t const root_table[7] = {
347
  MAD_F(0x09837f05) /* 2^(-3/4) == 0.59460355750136 */,
348
  MAD_F(0x0b504f33) /* 2^(-2/4) == 0.70710678118655 */,
349
  MAD_F(0x0d744fcd) /* 2^(-1/4) == 0.84089641525371 */,
350
  MAD_F(0x10000000) /* 2^( 0/4) == 1.00000000000000 */,
351
  MAD_F(0x1306fe0a) /* 2^(+1/4) == 1.18920711500272 */,
352
  MAD_F(0x16a09e66) /* 2^(+2/4) == 1.41421356237310 */,
353
  MAD_F(0x1ae89f99) /* 2^(+3/4) == 1.68179283050743 */
354
};
355
 
356
/*
357
 * coefficients for aliasing reduction
358
 * derived from Table B.9 of ISO/IEC 11172-3
359
 *
360
 *  c[]  = { -0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037 }
361
 * cs[i] =    1 / sqrt(1 + c[i]^2)
362
 * ca[i] = c[i] / sqrt(1 + c[i]^2)
363
 */
364
static
365
mad_fixed_t const cs[8] = {
366
  +MAD_F(0x0db84a81) /* +0.857492926 */, +MAD_F(0x0e1b9d7f) /* +0.881741997 */,
367
  +MAD_F(0x0f31adcf) /* +0.949628649 */, +MAD_F(0x0fbba815) /* +0.983314592 */,
368
  +MAD_F(0x0feda417) /* +0.995517816 */, +MAD_F(0x0ffc8fc8) /* +0.999160558 */,
369
  +MAD_F(0x0fff964c) /* +0.999899195 */, +MAD_F(0x0ffff8d3) /* +0.999993155 */
370
};
371
 
372
static
373
mad_fixed_t const ca[8] = {
374
  -MAD_F(0x083b5fe7) /* -0.514495755 */, -MAD_F(0x078c36d2) /* -0.471731969 */,
375
  -MAD_F(0x05039814) /* -0.313377454 */, -MAD_F(0x02e91dd1) /* -0.181913200 */,
376
  -MAD_F(0x0183603a) /* -0.094574193 */, -MAD_F(0x00a7cb87) /* -0.040965583 */,
377
  -MAD_F(0x003a2847) /* -0.014198569 */, -MAD_F(0x000f27b4) /* -0.003699975 */
378
};
379
 
380
/*
381
 * IMDCT coefficients for short blocks
382
 * derived from section 2.4.3.4.10.2 of ISO/IEC 11172-3
383
 *
384
 * imdct_s[i/even][k] = cos((PI / 24) * (2 * (i / 2) + 7) * (2 * k + 1))
385
 * imdct_s[i /odd][k] = cos((PI / 24) * (2 * (6 + (i-1)/2) + 7) * (2 * k + 1))
386
 */
387
static
388
mad_fixed_t const imdct_s[6][6] = {
389
# include "imdct_s.dat"
390
};
391
 
392
# if !defined(ASO_IMDCT)
393
/*
394
 * windowing coefficients for long blocks
395
 * derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
396
 *
397
 * window_l[i] = sin((PI / 36) * (i + 1/2))
398
 */
399
static
400
mad_fixed_t const window_l[36] = {
401
  MAD_F(0x00b2aa3e) /* 0.043619387 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
402
  MAD_F(0x03768962) /* 0.216439614 */, MAD_F(0x04cfb0e2) /* 0.300705800 */,
403
  MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x07635284) /* 0.461748613 */,
404
  MAD_F(0x0898c779) /* 0.537299608 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
405
  MAD_F(0x0acf37ad) /* 0.675590208 */, MAD_F(0x0bcbe352) /* 0.737277337 */,
406
  MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x0d7e8807) /* 0.843391446 */,
407
 
408
  MAD_F(0x0e313245) /* 0.887010833 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
409
  MAD_F(0x0f426cb5) /* 0.953716951 */, MAD_F(0x0f9ee890) /* 0.976296007 */,
410
  MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ffc19fd) /* 0.999048222 */,
411
  MAD_F(0x0ffc19fd) /* 0.999048222 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
412
  MAD_F(0x0f9ee890) /* 0.976296007 */, MAD_F(0x0f426cb5) /* 0.953716951 */,
413
  MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0e313245) /* 0.887010833 */,
414
 
415
  MAD_F(0x0d7e8807) /* 0.843391446 */, MAD_F(0x0cb19346) /* 0.793353340 */,
416
  MAD_F(0x0bcbe352) /* 0.737277337 */, MAD_F(0x0acf37ad) /* 0.675590208 */,
417
  MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0898c779) /* 0.537299608 */,
418
  MAD_F(0x07635284) /* 0.461748613 */, MAD_F(0x061f78aa) /* 0.382683432 */,
419
  MAD_F(0x04cfb0e2) /* 0.300705800 */, MAD_F(0x03768962) /* 0.216439614 */,
420
  MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x00b2aa3e) /* 0.043619387 */,
421
};
422
# endif  /* ASO_IMDCT */
423
 
424
/*
425
 * windowing coefficients for short blocks
426
 * derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
427
 *
428
 * window_s[i] = sin((PI / 12) * (i + 1/2))
429
 */
430
static
431
mad_fixed_t const window_s[12] = {
432
  MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x061f78aa) /* 0.382683432 */,
433
  MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0cb19346) /* 0.793353340 */,
434
  MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
435
  MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
436
  MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
437
  MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
438
};
439
 
440
/*
441
 * coefficients for intensity stereo processing
442
 * derived from section 2.4.3.4.9.3 of ISO/IEC 11172-3
443
 *
444
 * is_ratio[i] = tan(i * (PI / 12))
445
 * is_table[i] = is_ratio[i] / (1 + is_ratio[i])
446
 */
447
static
448
mad_fixed_t const is_table[7] = {
449
  MAD_F(0x00000000) /* 0.000000000 */,
450
  MAD_F(0x0361962f) /* 0.211324865 */,
451
  MAD_F(0x05db3d74) /* 0.366025404 */,
452
  MAD_F(0x08000000) /* 0.500000000 */,
453
  MAD_F(0x0a24c28c) /* 0.633974596 */,
454
  MAD_F(0x0c9e69d1) /* 0.788675135 */,
455
  MAD_F(0x10000000) /* 1.000000000 */
456
};
457
 
458
/*
459
 * coefficients for LSF intensity stereo processing
460
 * derived from section 2.4.3.2 of ISO/IEC 13818-3
461
 *
462
 * is_lsf_table[0][i] = (1 / sqrt(sqrt(2)))^(i + 1)
463
 * is_lsf_table[1][i] = (1 / sqrt(2))^(i + 1)
464
 */
465
static
466
mad_fixed_t const is_lsf_table[2][15] = {
467
  {
468
    MAD_F(0x0d744fcd) /* 0.840896415 */,
469
    MAD_F(0x0b504f33) /* 0.707106781 */,
470
    MAD_F(0x09837f05) /* 0.594603558 */,
471
    MAD_F(0x08000000) /* 0.500000000 */,
472
    MAD_F(0x06ba27e6) /* 0.420448208 */,
473
    MAD_F(0x05a8279a) /* 0.353553391 */,
474
    MAD_F(0x04c1bf83) /* 0.297301779 */,
475
    MAD_F(0x04000000) /* 0.250000000 */,
476
    MAD_F(0x035d13f3) /* 0.210224104 */,
477
    MAD_F(0x02d413cd) /* 0.176776695 */,
478
    MAD_F(0x0260dfc1) /* 0.148650889 */,
479
    MAD_F(0x02000000) /* 0.125000000 */,
480
    MAD_F(0x01ae89fa) /* 0.105112052 */,
481
    MAD_F(0x016a09e6) /* 0.088388348 */,
482
    MAD_F(0x01306fe1) /* 0.074325445 */
483
  }, {
484
    MAD_F(0x0b504f33) /* 0.707106781 */,
485
    MAD_F(0x08000000) /* 0.500000000 */,
486
    MAD_F(0x05a8279a) /* 0.353553391 */,
487
    MAD_F(0x04000000) /* 0.250000000 */,
488
    MAD_F(0x02d413cd) /* 0.176776695 */,
489
    MAD_F(0x02000000) /* 0.125000000 */,
490
    MAD_F(0x016a09e6) /* 0.088388348 */,
491
    MAD_F(0x01000000) /* 0.062500000 */,
492
    MAD_F(0x00b504f3) /* 0.044194174 */,
493
    MAD_F(0x00800000) /* 0.031250000 */,
494
    MAD_F(0x005a827a) /* 0.022097087 */,
495
    MAD_F(0x00400000) /* 0.015625000 */,
496
    MAD_F(0x002d413d) /* 0.011048543 */,
497
    MAD_F(0x00200000) /* 0.007812500 */,
498
    MAD_F(0x0016a09e) /* 0.005524272 */
499
  }
500
};
501
 
502
/*
503
 * NAME:        III_sideinfo()
504
 * DESCRIPTION: decode frame side information from a bitstream
505
 */
506
static
507
enum mad_error III_sideinfo(struct mad_bitptr *ptr, unsigned int nch,
508
                            int lsf, struct sideinfo *si,
509
                            unsigned int *data_bitlen,
510
                            unsigned int *priv_bitlen)
511
{
512
  unsigned int ngr, gr, ch, i;
513
  enum mad_error result = 0;
514
 
515
  *data_bitlen = 0;
516
  *priv_bitlen = lsf ? ((nch == 1) ? 1 : 2) : ((nch == 1) ? 5 : 3);
517
 
518
  si->main_data_begin = mad_bit_read(ptr, lsf ? 8 : 9);
519
  si->private_bits    = mad_bit_read(ptr, *priv_bitlen);
520
 
521
  ngr = 1;
522
  if (!lsf) {
523
    ngr = 2;
524
 
525
    for (ch = 0; ch < nch; ++ch)
526
      si->scfsi[ch] = mad_bit_read(ptr, 4);
527
  }
528
 
529
  for (gr = 0; gr < ngr; ++gr) {
530
    struct granule *granule = &si->gr[gr];
531
 
532
    for (ch = 0; ch < nch; ++ch) {
533
      struct channel *channel = &granule->ch[ch];
534
 
535
      channel->part2_3_length    = mad_bit_read(ptr, 12);
536
      channel->big_values        = mad_bit_read(ptr, 9);
537
      channel->global_gain       = mad_bit_read(ptr, 8);
538
      channel->scalefac_compress = mad_bit_read(ptr, lsf ? 9 : 4);
539
 
540
      *data_bitlen += channel->part2_3_length;
541
 
542
      if (channel->big_values > 288 && result == 0)
543
        result = MAD_ERROR_BADBIGVALUES;
544
 
545
      channel->flags = 0;
546
 
547
      /* window_switching_flag */
548
      if (mad_bit_read(ptr, 1)) {
549
        channel->block_type = mad_bit_read(ptr, 2);
550
 
551
        if (channel->block_type == 0 && result == 0)
552
          result = MAD_ERROR_BADBLOCKTYPE;
553
 
554
        if (!lsf && channel->block_type == 2 && si->scfsi[ch] && result == 0)
555
          result = MAD_ERROR_BADSCFSI;
556
 
557
        channel->region0_count = 7;
558
        channel->region1_count = 36;
559
 
560
        if (mad_bit_read(ptr, 1))
561
          channel->flags |= mixed_block_flag;
562
        else if (channel->block_type == 2)
563
          channel->region0_count = 8;
564
 
565
        for (i = 0; i < 2; ++i)
566
          channel->table_select[i] = mad_bit_read(ptr, 5);
567
 
568
# if defined(DEBUG)
569
        channel->table_select[2] = 4;  /* not used */
570
# endif
571
 
572
        for (i = 0; i < 3; ++i)
573
          channel->subblock_gain[i] = mad_bit_read(ptr, 3);
574
      }
575
      else {
576
        channel->block_type = 0;
577
 
578
        for (i = 0; i < 3; ++i)
579
          channel->table_select[i] = mad_bit_read(ptr, 5);
580
 
581
        channel->region0_count = mad_bit_read(ptr, 4);
582
        channel->region1_count = mad_bit_read(ptr, 3);
583
      }
584
 
585
      /* [preflag,] scalefac_scale, count1table_select */
586
      channel->flags |= mad_bit_read(ptr, lsf ? 2 : 3);
587
    }
588
  }
589
 
590
  return result;
591
}
592
 
593
/*
594
 * NAME:        III_scalefactors_lsf()
595
 * DESCRIPTION: decode channel scalefactors for LSF from a bitstream
596
 */
597
static
598
unsigned int III_scalefactors_lsf(struct mad_bitptr *ptr,
599
                                  struct channel *channel,
600
                                  struct channel *gr1ch, int mode_extension)
601
{
602
  struct mad_bitptr start;
603
  unsigned int scalefac_compress, index, slen[4], part, n, i;
604
  unsigned char const *nsfb;
605
 
606
  start = *ptr;
607
 
608
  scalefac_compress = channel->scalefac_compress;
609
  index = (channel->block_type == 2) ?
610
    ((channel->flags & mixed_block_flag) ? 2 : 1) : 0;
611
 
612
  if (!((mode_extension & 0x1) && gr1ch)) {
613
    if (scalefac_compress < 400) {
614
      slen[0] = (scalefac_compress >> 4) / 5;
615
      slen[1] = (scalefac_compress >> 4) % 5;
616
      slen[2] = (scalefac_compress % 16) >> 2;
617
      slen[3] =  scalefac_compress %  4;
618
 
619
      nsfb = nsfb_table[0][index];
620
    }
621
    else if (scalefac_compress < 500) {
622
      scalefac_compress -= 400;
623
 
624
      slen[0] = (scalefac_compress >> 2) / 5;
625
      slen[1] = (scalefac_compress >> 2) % 5;
626
      slen[2] =  scalefac_compress %  4;
627
      slen[3] = 0;
628
 
629
      nsfb = nsfb_table[1][index];
630
    }
631
    else {
632
      scalefac_compress -= 500;
633
 
634
      slen[0] = scalefac_compress / 3;
635
      slen[1] = scalefac_compress % 3;
636
      slen[2] = 0;
637
      slen[3] = 0;
638
 
639
      channel->flags |= preflag;
640
 
641
      nsfb = nsfb_table[2][index];
642
    }
643
 
644
    n = 0;
645
    for (part = 0; part < 4; ++part) {
646
      for (i = 0; i < nsfb[part]; ++i)
647
        channel->scalefac[n++] = mad_bit_read(ptr, slen[part]);
648
    }
649
 
650
    while (n < 39)
651
      channel->scalefac[n++] = 0;
652
  }
653
  else {  /* (mode_extension & 0x1) && gr1ch (i.e. ch == 1) */
654
    scalefac_compress >>= 1;
655
 
656
    if (scalefac_compress < 180) {
657
      slen[0] =  scalefac_compress / 36;
658
      slen[1] = (scalefac_compress % 36) / 6;
659
      slen[2] = (scalefac_compress % 36) % 6;
660
      slen[3] = 0;
661
 
662
      nsfb = nsfb_table[3][index];
663
    }
664
    else if (scalefac_compress < 244) {
665
      scalefac_compress -= 180;
666
 
667
      slen[0] = (scalefac_compress % 64) >> 4;
668
      slen[1] = (scalefac_compress % 16) >> 2;
669
      slen[2] =  scalefac_compress %  4;
670
      slen[3] = 0;
671
 
672
      nsfb = nsfb_table[4][index];
673
    }
674
    else {
675
      scalefac_compress -= 244;
676
 
677
      slen[0] = scalefac_compress / 3;
678
      slen[1] = scalefac_compress % 3;
679
      slen[2] = 0;
680
      slen[3] = 0;
681
 
682
      nsfb = nsfb_table[5][index];
683
    }
684
 
685
    n = 0;
686
    for (part = 0; part < 4; ++part) {
687
      unsigned int max, is_pos;
688
 
689
      max = (1 << slen[part]) - 1;
690
 
691
      for (i = 0; i < nsfb[part]; ++i) {
692
        is_pos = mad_bit_read(ptr, slen[part]);
693
 
694
        channel->scalefac[n] = is_pos;
695
        gr1ch->scalefac[n++] = (is_pos == max);
696
      }
697
    }
698
 
699
    while (n < 39) {
700
      channel->scalefac[n] = 0;
701
      gr1ch->scalefac[n++] = 0;  /* apparently not illegal */
702
    }
703
  }
704
 
705
  return mad_bit_length(&start, ptr);
706
}
707
 
708
/*
709
 * NAME:        III_scalefactors()
710
 * DESCRIPTION: decode channel scalefactors of one granule from a bitstream
711
 */
712
static
713
unsigned int III_scalefactors(struct mad_bitptr *ptr, struct channel *channel,
714
                              struct channel const *gr0ch, unsigned int scfsi)
715
{
716
  struct mad_bitptr start;
717
  unsigned int slen1, slen2, sfbi;
718
 
719
  start = *ptr;
720
 
721
  slen1 = sflen_table[channel->scalefac_compress].slen1;
722
  slen2 = sflen_table[channel->scalefac_compress].slen2;
723
 
724
  if (channel->block_type == 2) {
725
    unsigned int nsfb;
726
 
727
    sfbi = 0;
728
 
729
    nsfb = (channel->flags & mixed_block_flag) ? 8 + 3 * 3 : 6 * 3;
730
    while (nsfb--)
731
      channel->scalefac[sfbi++] = mad_bit_read(ptr, slen1);
732
 
733
    nsfb = 6 * 3;
734
    while (nsfb--)
735
      channel->scalefac[sfbi++] = mad_bit_read(ptr, slen2);
736
 
737
    nsfb = 1 * 3;
738
    while (nsfb--)
739
      channel->scalefac[sfbi++] = 0;
740
  }
741
  else {  /* channel->block_type != 2 */
742
    if (scfsi & 0x8) {
743
      for (sfbi = 0; sfbi < 6; ++sfbi)
744
        channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
745
    }
746
    else {
747
      for (sfbi = 0; sfbi < 6; ++sfbi)
748
        channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
749
    }
750
 
751
    if (scfsi & 0x4) {
752
      for (sfbi = 6; sfbi < 11; ++sfbi)
753
        channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
754
    }
755
    else {
756
      for (sfbi = 6; sfbi < 11; ++sfbi)
757
        channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
758
    }
759
 
760
    if (scfsi & 0x2) {
761
      for (sfbi = 11; sfbi < 16; ++sfbi)
762
        channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
763
    }
764
    else {
765
      for (sfbi = 11; sfbi < 16; ++sfbi)
766
        channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
767
    }
768
 
769
    if (scfsi & 0x1) {
770
      for (sfbi = 16; sfbi < 21; ++sfbi)
771
        channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
772
    }
773
    else {
774
      for (sfbi = 16; sfbi < 21; ++sfbi)
775
        channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
776
    }
777
 
778
    channel->scalefac[21] = 0;
779
  }
780
 
781
  return mad_bit_length(&start, ptr);
782
}
783
 
784
/*
785
 * NAME:        III_exponents()
786
 * DESCRIPTION: calculate scalefactor exponents
787
 */
788
static
789
void III_exponents(struct channel const *channel,
790
                   unsigned char const *sfbwidth, signed int exponents[39])
791
{
792
  signed int gain;
793
  unsigned int scalefac_multiplier, sfbi;
794
 
795
  gain = (signed int) channel->global_gain - 210;
796
  scalefac_multiplier = (channel->flags & scalefac_scale) ? 2 : 1;
797
 
798
  if (channel->block_type == 2) {
799
    unsigned int l;
800
    signed int gain0, gain1, gain2;
801
 
802
    sfbi = l = 0;
803
 
804
    if (channel->flags & mixed_block_flag) {
805
      unsigned int premask;
806
 
807
      premask = (channel->flags & preflag) ? ~0 : 0;
808
 
809
      /* long block subbands 0-1 */
810
 
811
      while (l < 36) {
812
        exponents[sfbi] = gain -
813
          (signed int) ((channel->scalefac[sfbi] + (pretab[sfbi] & premask)) <<
814
                        scalefac_multiplier);
815
 
816
        l += sfbwidth[sfbi++];
817
      }
818
    }
819
 
820
    /* this is probably wrong for 8000 Hz short/mixed blocks */
821
 
822
    gain0 = gain - 8 * (signed int) channel->subblock_gain[0];
823
    gain1 = gain - 8 * (signed int) channel->subblock_gain[1];
824
    gain2 = gain - 8 * (signed int) channel->subblock_gain[2];
825
 
826
    while (l < 576) {
827
      exponents[sfbi + 0] = gain0 -
828
        (signed int) (channel->scalefac[sfbi + 0] << scalefac_multiplier);
829
      exponents[sfbi + 1] = gain1 -
830
        (signed int) (channel->scalefac[sfbi + 1] << scalefac_multiplier);
831
      exponents[sfbi + 2] = gain2 -
832
        (signed int) (channel->scalefac[sfbi + 2] << scalefac_multiplier);
833
 
834
      l    += 3 * sfbwidth[sfbi];
835
      sfbi += 3;
836
    }
837
  }
838
  else {  /* channel->block_type != 2 */
839
    if (channel->flags & preflag) {
840
      for (sfbi = 0; sfbi < 22; ++sfbi) {
841
        exponents[sfbi] = gain -
842
          (signed int) ((channel->scalefac[sfbi] + pretab[sfbi]) <<
843
                        scalefac_multiplier);
844
      }
845
    }
846
    else {
847
      for (sfbi = 0; sfbi < 22; ++sfbi) {
848
        exponents[sfbi] = gain -
849
          (signed int) (channel->scalefac[sfbi] << scalefac_multiplier);
850
      }
851
    }
852
  }
853
}
854
 
855
/*
856
 * NAME:        III_requantize()
857
 * DESCRIPTION: requantize one (positive) value
858
 */
859
static
860
mad_fixed_t III_requantize(unsigned int value, signed int exp)
861
{
862
  mad_fixed_t requantized;
863
  signed int frac;
864
  struct fixedfloat const *power;
865
 
866
  /*
867
   * long blocks:
868
   * xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
869
   *         2^((1/4) * (global_gain - 210)) *
870
   *         2^-(scalefac_multiplier *
871
   *             (scalefac_l[sfb] + preflag * pretab[sfb]))
872
   *
873
   * short blocks:
874
   * xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
875
   *         2^((1/4) * (global_gain - 210 - 8 * subblock_gain[w])) *
876
   *         2^-(scalefac_multiplier * scalefac_s[sfb][w])
877
   *
878
   * where:
879
   * scalefac_multiplier = (scalefac_scale + 1) / 2
880
   */
881
 
882
  frac = exp % 4;
883
  exp /= 4;
884
 
885
  /* MM: recalculation for short table */
886
  if (value < (1 << RQ_START_BITS))
887
    power = &rq_table[value];
888
  else {
889
    int bits = 0, tmp = value;
890
    while (tmp != 0) {
891
      tmp <<= 1;
892
      bits++;
893
    }
894
    bits -= RQ_START_BITS;
895
    value &= ~((1 << bits) - 1);
896
    power = &rq_table[value];
897
  }
898
 
899
  requantized = power->mantissa;
900
  exp += power->exponent;
901
 
902
  if (exp < 0) {
903
    if (-exp >= sizeof(mad_fixed_t) * CHAR_BIT) {
904
      /* underflow */
905
      requantized = 0;
906
    }
907
    else
908
      requantized >>= -exp;
909
  }
910
  else {
911
    if (exp >= 5) {
912
      /* overflow */
913
# if defined(DEBUG)
914
      fprintf(stderr, "requantize overflow (%f * 2^%d)\n",
915
              mad_f_todouble(requantized), exp);
916
# endif
917
      requantized = MAD_F_MAX;
918
    }
919
    else
920
      requantized <<= exp;
921
  }
922
 
923
  return frac ? mad_f_mul(requantized, root_table[3 + frac]) : requantized;
924
}
925
 
926
/* we must take care that sz >= bits and sz < sizeof(long) lest bits == 0 */
927
# define MASK(cache, sz, bits)  \
928
    (((cache) >> ((sz) - (bits))) & ((1 << (bits)) - 1))
929
# define MASK1BIT(cache, sz)  \
930
    ((cache) & (1 << ((sz) - 1)))
931
 
932
/*
933
 * NAME:        III_huffdecode()
934
 * DESCRIPTION: decode Huffman code words of one channel of one granule
935
 */
936
static
937
enum mad_error III_huffdecode(struct mad_bitptr *ptr, mad_fixed_t xr[576],
938
                              struct channel *channel,
939
                              unsigned char const *sfbwidth,
940
                              unsigned int part2_length)
941
{
942
  signed int exponents[39], exp;
943
  signed int const *expptr;
944
  struct mad_bitptr peek;
945
  signed int bits_left, cachesz;
946
  register mad_fixed_t *xrptr;
947
  mad_fixed_t const *sfbound;
948
  register unsigned long bitcache;
949
 
950
  bits_left = (signed) channel->part2_3_length - (signed) part2_length;
951
  if (bits_left < 0)
952
    return MAD_ERROR_BADPART3LEN;
953
 
954
  III_exponents(channel, sfbwidth, exponents);
955
 
956
  peek = *ptr;
957
  mad_bit_skip(ptr, bits_left);
958
 
959
  /* align bit reads to byte boundaries */
960
  cachesz  = mad_bit_bitsleft(&peek);
961
  cachesz += ((32 - 1 - 24) + (24 - cachesz)) & ~7;
962
 
963
  bitcache   = mad_bit_read(&peek, cachesz);
964
  bits_left -= cachesz;
965
 
966
  xrptr = &xr[0];
967
 
968
  /* big_values */
969
  {
970
    unsigned int region, rcount;
971
    struct hufftable const *entry;
972
    union huffpair const *table;
973
    unsigned int linbits, startbits, big_values, reqhits;
974
    mad_fixed_t reqcache[16];
975
 
976
    sfbound = xrptr + *sfbwidth++;
977
    rcount  = channel->region0_count + 1;
978
 
979
    entry     = &mad_huff_pair_table[channel->table_select[region = 0]];
980
    table     = entry->table;
981
    linbits   = entry->linbits;
982
    startbits = entry->startbits;
983
 
984
    if (table == 0)
985
      return MAD_ERROR_BADHUFFTABLE;
986
 
987
    expptr  = &exponents[0];
988
    exp     = *expptr++;
989
    reqhits = 0;
990
 
991
    big_values = channel->big_values;
992
 
993
    while (big_values-- && cachesz + bits_left > 0) {
994
      union huffpair const *pair;
995
      unsigned int clumpsz, value;
996
      register mad_fixed_t requantized;
997
 
998
      if (xrptr == sfbound) {
999
        sfbound += *sfbwidth++;
1000
 
1001
        /* change table if region boundary */
1002
 
1003
        if (--rcount == 0) {
1004
          if (region == 0)
1005
            rcount = channel->region1_count + 1;
1006
          else
1007
            rcount = 0;  /* all remaining */
1008
 
1009
          entry     = &mad_huff_pair_table[channel->table_select[++region]];
1010
          table     = entry->table;
1011
          linbits   = entry->linbits;
1012
          startbits = entry->startbits;
1013
 
1014
          if (table == 0)
1015
            return MAD_ERROR_BADHUFFTABLE;
1016
        }
1017
 
1018
        if (exp != *expptr) {
1019
          exp = *expptr;
1020
          reqhits = 0;
1021
        }
1022
 
1023
        ++expptr;
1024
      }
1025
 
1026
      if (cachesz < 21) {
1027
        unsigned int bits;
1028
 
1029
        bits       = ((32 - 1 - 21) + (21 - cachesz)) & ~7;
1030
        bitcache   = (bitcache << bits) | mad_bit_read(&peek, bits);
1031
        cachesz   += bits;
1032
        bits_left -= bits;
1033
      }
1034
 
1035
      /* hcod (0..19) */
1036
 
1037
      clumpsz = startbits;
1038
      pair    = &table[MASK(bitcache, cachesz, clumpsz)];
1039
 
1040
      while (!pair->final) {
1041
        cachesz -= clumpsz;
1042
 
1043
        clumpsz = pair->ptr.bits;
1044
        pair    = &table[pair->ptr.offset + MASK(bitcache, cachesz, clumpsz)];
1045
      }
1046
 
1047
      cachesz -= pair->value.hlen;
1048
 
1049
      {
1050
        int xy_sel;
1051
 
1052
        /* MM: joint x and y */
1053
        value = pair->value.x;
1054
 
1055
        /* x (0..14) and y (0..14) and x (0..1) and y (0..1) */
1056
        for (xy_sel = 0; xy_sel < 2; xy_sel++) {
1057
          if (value == 0)
1058
            xrptr[xy_sel] = 0;
1059
          else {
1060
            if (value == 15 && linbits) {
1061
              if (cachesz < linbits + 2 - xy_sel) {
1062
                bitcache   = (bitcache << 16) | mad_bit_read(&peek, 16);
1063
                cachesz   += 16;
1064
                bits_left -= 16;
1065
              }
1066
 
1067
              value += MASK(bitcache, cachesz, linbits);
1068
              cachesz -= linbits;
1069
 
1070
              requantized = III_requantize(value, exp);
1071
            }
1072
            else {
1073
              if (reqhits & (1 << value))
1074
                requantized = reqcache[value];
1075
              else {
1076
                reqhits |= (1 << value);
1077
                requantized = reqcache[value] = III_requantize(value, exp);
1078
              }
1079
            }
1080
            xrptr[xy_sel] = MASK1BIT(bitcache, cachesz--) ?
1081
              -requantized : requantized;
1082
          }
1083
          value = pair->value.y;
1084
        }
1085
      }
1086
 
1087
      xrptr += 2;
1088
    }
1089
  }
1090
 
1091
  if (cachesz + bits_left < 0)
1092
    return MAD_ERROR_BADHUFFDATA;  /* big_values overrun */
1093
 
1094
  /* count1 */
1095
  {
1096
    union huffquad const *table;
1097
    register mad_fixed_t requantized;
1098
 
1099
    table = mad_huff_quad_table[channel->flags & count1table_select];
1100
 
1101
    requantized = III_requantize(1, exp);
1102
 
1103
    while (cachesz + bits_left > 0 && xrptr <= &xr[572]) {
1104
      union huffquad const *quad;
1105
 
1106
      /* hcod (1..6) */
1107
 
1108
      if (cachesz < 10) {
1109
        bitcache   = (bitcache << 16) | mad_bit_read(&peek, 16);
1110
        cachesz   += 16;
1111
        bits_left -= 16;
1112
      }
1113
 
1114
      quad = &table[MASK(bitcache, cachesz, 4)];
1115
 
1116
      /* quad tables guaranteed to have at most one extra lookup */
1117
      if (!quad->final) {
1118
        cachesz -= 4;
1119
 
1120
        quad = &table[quad->ptr.offset +
1121
                      MASK(bitcache, cachesz, quad->ptr.bits)];
1122
      }
1123
 
1124
      cachesz -= quad->value.hlen;
1125
 
1126
      /* MM: code joined */
1127
      {
1128
        int sel_12;
1129
        int vw_xy_sel = *((int *) &quad->value);
1130
        for (sel_12 = 0; sel_12 < 2; sel_12++) {
1131
          if (xrptr == sfbound) {
1132
            sfbound += *sfbwidth++;
1133
 
1134
            if (exp != *expptr) {
1135
              exp = *expptr;
1136
              requantized = III_requantize(1, exp);
1137
            }
1138
 
1139
            ++expptr;
1140
          }
1141
          {
1142
            int tmp = MASK1BIT(bitcache, cachesz--) ? -requantized : requantized;
1143
 
1144
            /* v (0..1) or x (0..1) */
1145
            xrptr[0] = vw_xy_sel&4 ? tmp : 0;
1146
 
1147
            /* w (0..1) or y (0..1) */
1148
            xrptr[1] = vw_xy_sel&8 ? tmp : 0;
1149
          }
1150
          xrptr += 2;
1151
          vw_xy_sel >>= 2;
1152
        }
1153
      }
1154
    }
1155
 
1156
    if (cachesz + bits_left < 0) {
1157
# if 0 && defined(DEBUG)
1158
      fprintf(stderr, "huffman count1 overrun (%d bits)\n",
1159
              -(cachesz + bits_left));
1160
# endif
1161
 
1162
      /* technically the bitstream is misformatted, but apparently
1163
         some encoders are just a bit sloppy with stuffing bits */
1164
 
1165
      xrptr -= 4;
1166
    }
1167
  }
1168
 
1169
  assert(-bits_left <= MAD_BUFFER_GUARD * CHAR_BIT);
1170
 
1171
# if 0 && defined(DEBUG)
1172
  if (bits_left < 0)
1173
    fprintf(stderr, "read %d bits too many\n", -bits_left);
1174
  else if (cachesz + bits_left > 0)
1175
    fprintf(stderr, "%d stuffing bits\n", cachesz + bits_left);
1176
# endif
1177
 
1178
  /* rzero */
1179
  while (xrptr < &xr[576]) {
1180
    xrptr[0] = 0;
1181
    xrptr[1] = 0;
1182
 
1183
    xrptr += 2;
1184
  }
1185
 
1186
  return 0;
1187
}
1188
 
1189
# undef MASK
1190
# undef MASK1BIT
1191
 
1192
/*
1193
 * NAME:        III_reorder()
1194
 * DESCRIPTION: reorder frequency lines of a short block into subband order
1195
 */
1196
static
1197
void III_reorder(mad_fixed_t xr[576], struct channel const *channel,
1198
                 unsigned char const sfbwidth[39])
1199
{
1200
  mad_fixed_t tmp[32][3][6];
1201
  unsigned int sb, l, sfbi, f, w, sbw[3], sw[3];
1202
 
1203
  /* this is probably wrong for 8000 Hz mixed blocks */
1204
 
1205
  if (channel->flags & mixed_block_flag)
1206
    sb = 2, sfbi = 3 * 3;
1207
  else
1208
    sb = 0, sfbi = 0;
1209
 
1210
  for (w = 0; w < 3; ++w) {
1211
    sbw[w] = sb;
1212
    sw[w]  = 0;
1213
  }
1214
 
1215
  f = sfbwidth[sfbi];
1216
  w = 0;
1217
 
1218
  for (l = 18 * sb; l < 576; ++l) {
1219
    tmp[sbw[w]][w][sw[w]++] = xr[l];
1220
 
1221
    if (sw[w] == 6) {
1222
      sw[w] = 0;
1223
      ++sbw[w];
1224
    }
1225
 
1226
    if (--f == 0) {
1227
      if (++w == 3)
1228
        w = 0;
1229
 
1230
      f = sfbwidth[++sfbi];
1231
    }
1232
  }
1233
 
1234
  memcpy(&xr[18 * sb], &tmp[sb], (576 - 18 * sb) * sizeof(mad_fixed_t));
1235
}
1236
 
1237
/*
1238
 * NAME:        III_stereo()
1239
 * DESCRIPTION: perform joint stereo processing on a granule
1240
 */
1241
static
1242
enum mad_error III_stereo(mad_fixed_t xr[2][576],
1243
                          struct granule const *granule,
1244
                          struct mad_header *header,
1245
                          unsigned char const *sfbwidth)
1246
{
1247
  short modes[39];
1248
  unsigned int sfbi, l, n, i;
1249
 
1250
  enum {
1251
    i_stereo  = 0x1,
1252
    ms_stereo = 0x2
1253
  };
1254
 
1255
  if (granule->ch[0].block_type !=
1256
      granule->ch[1].block_type ||
1257
      (granule->ch[0].flags & mixed_block_flag) !=
1258
      (granule->ch[1].flags & mixed_block_flag))
1259
    return MAD_ERROR_BADSTEREO;
1260
 
1261
  for (i = 0; i < 39; ++i)
1262
    modes[i] = header->mode_extension;
1263
 
1264
  /* intensity stereo */
1265
 
1266
  if (header->mode_extension & i_stereo) {
1267
    struct channel const *right_ch = &granule->ch[1];
1268
    mad_fixed_t const *right_xr = xr[1];
1269
    unsigned int is_pos;
1270
 
1271
    header->flags |= MAD_FLAG_I_STEREO;
1272
 
1273
    /* first determine which scalefactor bands are to be processed */
1274
 
1275
    if (right_ch->block_type == 2) {
1276
      unsigned int lower, start, max, bound[3], w;
1277
 
1278
      lower = start = max = bound[0] = bound[1] = bound[2] = 0;
1279
 
1280
      sfbi = l = 0;
1281
 
1282
      if (right_ch->flags & mixed_block_flag) {
1283
        while (l < 36) {
1284
          n = sfbwidth[sfbi++];
1285
 
1286
          for (i = 0; i < n; ++i) {
1287
            if (right_xr[i]) {
1288
              lower = sfbi;
1289
              break;
1290
            }
1291
          }
1292
 
1293
          right_xr += n;
1294
          l += n;
1295
        }
1296
 
1297
        start = sfbi;
1298
      }
1299
 
1300
      w = 0;
1301
      while (l < 576) {
1302
        n = sfbwidth[sfbi++];
1303
 
1304
        for (i = 0; i < n; ++i) {
1305
          if (right_xr[i]) {
1306
            max = bound[w] = sfbi;
1307
            break;
1308
          }
1309
        }
1310
 
1311
        right_xr += n;
1312
        l += n;
1313
        w = (w + 1) % 3;
1314
      }
1315
 
1316
      if (max)
1317
        lower = start;
1318
 
1319
      /* long blocks */
1320
 
1321
      for (i = 0; i < lower; ++i)
1322
        modes[i] = header->mode_extension & ~i_stereo;
1323
 
1324
      /* short blocks */
1325
 
1326
      w = 0;
1327
      for (i = start; i < max; ++i) {
1328
        if (i < bound[w])
1329
          modes[i] = header->mode_extension & ~i_stereo;
1330
 
1331
        w = (w + 1) % 3;
1332
      }
1333
    }
1334
    else {  /* right_ch->block_type != 2 */
1335
      unsigned int bound;
1336
 
1337
      bound = 0;
1338
      for (sfbi = l = 0; l < 576; l += n) {
1339
        n = sfbwidth[sfbi++];
1340
 
1341
        for (i = 0; i < n; ++i) {
1342
          if (right_xr[i]) {
1343
            bound = sfbi;
1344
            break;
1345
          }
1346
        }
1347
 
1348
        right_xr += n;
1349
      }
1350
 
1351
      for (i = 0; i < bound; ++i)
1352
        modes[i] = header->mode_extension & ~i_stereo;
1353
    }
1354
 
1355
    /* now do the actual processing */
1356
 
1357
    if (header->flags & MAD_FLAG_LSF_EXT) {
1358
      unsigned char const *illegal_pos = granule[1].ch[1].scalefac;
1359
      mad_fixed_t const *lsf_scale;
1360
 
1361
      /* intensity_scale */
1362
      lsf_scale = is_lsf_table[right_ch->scalefac_compress & 0x1];
1363
 
1364
      for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
1365
        n = sfbwidth[sfbi];
1366
 
1367
        if (!(modes[sfbi] & i_stereo))
1368
          continue;
1369
 
1370
        if (illegal_pos[sfbi]) {
1371
          modes[sfbi] &= ~i_stereo;
1372
          continue;
1373
        }
1374
 
1375
        is_pos = right_ch->scalefac[sfbi];
1376
 
1377
        for (i = 0; i < n; ++i) {
1378
          register mad_fixed_t left;
1379
 
1380
          left = xr[0][l + i];
1381
 
1382
          if (is_pos == 0)
1383
            xr[1][l + i] = left;
1384
          else {
1385
            register mad_fixed_t opposite;
1386
 
1387
            opposite = mad_f_mul(left, lsf_scale[(is_pos - 1) / 2]);
1388
 
1389
            if (is_pos & 1) {
1390
              xr[0][l + i] = opposite;
1391
              xr[1][l + i] = left;
1392
            }
1393
            else
1394
              xr[1][l + i] = opposite;
1395
          }
1396
        }
1397
      }
1398
    }
1399
    else {  /* !(header->flags & MAD_FLAG_LSF_EXT) */
1400
      for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
1401
        n = sfbwidth[sfbi];
1402
 
1403
        if (!(modes[sfbi] & i_stereo))
1404
          continue;
1405
 
1406
        is_pos = right_ch->scalefac[sfbi];
1407
 
1408
        if (is_pos >= 7) {  /* illegal intensity position */
1409
          modes[sfbi] &= ~i_stereo;
1410
          continue;
1411
        }
1412
 
1413
        for (i = 0; i < n; ++i) {
1414
          register mad_fixed_t left;
1415
 
1416
          left = xr[0][l + i];
1417
 
1418
          xr[0][l + i] = mad_f_mul(left, is_table[    is_pos]);
1419
          xr[1][l + i] = mad_f_mul(left, is_table[6 - is_pos]);
1420
        }
1421
      }
1422
    }
1423
  }
1424
 
1425
  /* middle/side stereo */
1426
 
1427
  if (header->mode_extension & ms_stereo) {
1428
    register mad_fixed_t invsqrt2;
1429
 
1430
    header->flags |= MAD_FLAG_MS_STEREO;
1431
 
1432
    invsqrt2 = root_table[3 + -2];
1433
 
1434
    for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
1435
      n = sfbwidth[sfbi];
1436
 
1437
      if (modes[sfbi] != ms_stereo)
1438
        continue;
1439
 
1440
      for (i = 0; i < n; ++i) {
1441
        register mad_fixed_t m, s;
1442
 
1443
        m = xr[0][l + i];
1444
        s = xr[1][l + i];
1445
 
1446
        xr[0][l + i] = mad_f_mul(m + s, invsqrt2);  /* l = (m + s) / sqrt(2) */
1447
        xr[1][l + i] = mad_f_mul(m - s, invsqrt2);  /* r = (m - s) / sqrt(2) */
1448
      }
1449
    }
1450
  }
1451
 
1452
  return 0;
1453
}
1454
 
1455
/*
1456
 * NAME:        III_aliasreduce()
1457
 * DESCRIPTION: perform frequency line alias reduction
1458
 */
1459
static
1460
void III_aliasreduce(mad_fixed_t xr[576], int lines)
1461
{
1462
  mad_fixed_t const *bound;
1463
  int i;
1464
 
1465
  bound = &xr[lines];
1466
  for (xr += 18; xr < bound; xr += 18) {
1467
    for (i = 0; i < 8; ++i) {
1468
      register mad_fixed_t *aptr, *bptr, a, b;
1469
      register mad_fixed64hi_t hi;
1470
      register mad_fixed64lo_t lo;
1471
 
1472
      aptr = &xr[-1 - i];
1473
      bptr = &xr[     i];
1474
 
1475
      a = *aptr;
1476
      b = *bptr;
1477
 
1478
# if defined(ASO_ZEROCHECK)
1479
      if (a | b) {
1480
# endif
1481
        MAD_F_ML0(hi, lo,  a, cs[i]);
1482
        MAD_F_MLA(hi, lo, -b, ca[i]);
1483
 
1484
        *aptr = MAD_F_MLZ(hi, lo);
1485
 
1486
        MAD_F_ML0(hi, lo,  b, cs[i]);
1487
        MAD_F_MLA(hi, lo,  a, ca[i]);
1488
 
1489
        *bptr = MAD_F_MLZ(hi, lo);
1490
# if defined(ASO_ZEROCHECK)
1491
      }
1492
# endif
1493
    }
1494
  }
1495
}
1496
 
1497
# if defined(ASO_IMDCT)
1498
void III_imdct_l(mad_fixed_t const [18], mad_fixed_t [36], unsigned int);
1499
# else
1500
/*
1501
 * NAME:        imdct36
1502
 * DESCRIPTION: perform X[18]->x[36] IMDCT
1503
 */
1504
static inline
1505
void imdct36(mad_fixed_t const X[18], mad_fixed_t x[36])
1506
{
1507
  mad_fixed_t t0, t1, t2,  t3,  t4,  t5,  t6,  t7;
1508
  mad_fixed_t t8, t9, t10, t11, t12, t13, t14, t15;
1509
  register mad_fixed64hi_t hi;
1510
  register mad_fixed64lo_t lo;
1511
 
1512
  MAD_F_ML0(hi, lo, X[4],  MAD_F(0x0ec835e8));
1513
  MAD_F_MLA(hi, lo, X[13], MAD_F(0x061f78aa));
1514
 
1515
  t6 = MAD_F_MLZ(hi, lo);
1516
 
1517
  MAD_F_MLA(hi, lo, (t14 = X[1] - X[10]), -MAD_F(0x061f78aa));
1518
  MAD_F_MLA(hi, lo, (t15 = X[7] + X[16]), -MAD_F(0x0ec835e8));
1519
 
1520
  t0 = MAD_F_MLZ(hi, lo);
1521
 
1522
  MAD_F_MLA(hi, lo, (t8  = X[0] - X[11] - X[12]),  MAD_F(0x0216a2a2));
1523
  MAD_F_MLA(hi, lo, (t9  = X[2] - X[9]  - X[14]),  MAD_F(0x09bd7ca0));
1524
  MAD_F_MLA(hi, lo, (t10 = X[3] - X[8]  - X[15]), -MAD_F(0x0cb19346));
1525
  MAD_F_MLA(hi, lo, (t11 = X[5] - X[6]  - X[17]), -MAD_F(0x0fdcf549));
1526
 
1527
  x[7]  = MAD_F_MLZ(hi, lo);
1528
  x[10] = -x[7];
1529
 
1530
  MAD_F_ML0(hi, lo, t8,  -MAD_F(0x0cb19346));
1531
  MAD_F_MLA(hi, lo, t9,   MAD_F(0x0fdcf549));
1532
  MAD_F_MLA(hi, lo, t10,  MAD_F(0x0216a2a2));
1533
  MAD_F_MLA(hi, lo, t11, -MAD_F(0x09bd7ca0));
1534
 
1535
  x[19] = x[34] = MAD_F_MLZ(hi, lo) - t0;
1536
 
1537
  t12 = X[0] - X[3] + X[8] - X[11] - X[12] + X[15];
1538
  t13 = X[2] + X[5] - X[6] - X[9]  - X[14] - X[17];
1539
 
1540
  MAD_F_ML0(hi, lo, t12, -MAD_F(0x0ec835e8));
1541
  MAD_F_MLA(hi, lo, t13,  MAD_F(0x061f78aa));
1542
 
1543
  x[22] = x[31] = MAD_F_MLZ(hi, lo) + t0;
1544
 
1545
  MAD_F_ML0(hi, lo, X[1],  -MAD_F(0x09bd7ca0));
1546
  MAD_F_MLA(hi, lo, X[7],   MAD_F(0x0216a2a2));
1547
  MAD_F_MLA(hi, lo, X[10], -MAD_F(0x0fdcf549));
1548
  MAD_F_MLA(hi, lo, X[16],  MAD_F(0x0cb19346));
1549
 
1550
  t1 = MAD_F_MLZ(hi, lo) + t6;
1551
 
1552
  MAD_F_ML0(hi, lo, X[0],   MAD_F(0x03768962));
1553
  MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0e313245));
1554
  MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x0ffc19fd));
1555
  MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0acf37ad));
1556
  MAD_F_MLA(hi, lo, X[6],   MAD_F(0x04cfb0e2));
1557
  MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x0898c779));
1558
  MAD_F_MLA(hi, lo, X[9],   MAD_F(0x0d7e8807));
1559
  MAD_F_MLA(hi, lo, X[11],  MAD_F(0x0f426cb5));
1560
  MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0bcbe352));
1561
  MAD_F_MLA(hi, lo, X[14],  MAD_F(0x00b2aa3e));
1562
  MAD_F_MLA(hi, lo, X[15], -MAD_F(0x07635284));
1563
  MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0f9ee890));
1564
 
1565
  x[6]  = MAD_F_MLZ(hi, lo) + t1;
1566
  x[11] = -x[6];
1567
 
1568
  MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0f426cb5));
1569
  MAD_F_MLA(hi, lo, X[2],  -MAD_F(0x00b2aa3e));
1570
  MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0898c779));
1571
  MAD_F_MLA(hi, lo, X[5],   MAD_F(0x0f9ee890));
1572
  MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0acf37ad));
1573
  MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x07635284));
1574
  MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x0e313245));
1575
  MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0bcbe352));
1576
  MAD_F_MLA(hi, lo, X[12], -MAD_F(0x03768962));
1577
  MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0d7e8807));
1578
  MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0ffc19fd));
1579
  MAD_F_MLA(hi, lo, X[17],  MAD_F(0x04cfb0e2));
1580
 
1581
  x[23] = x[30] = MAD_F_MLZ(hi, lo) + t1;
1582
 
1583
  MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0bcbe352));
1584
  MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0d7e8807));
1585
  MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x07635284));
1586
  MAD_F_MLA(hi, lo, X[5],   MAD_F(0x04cfb0e2));
1587
  MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0f9ee890));
1588
  MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x0ffc19fd));
1589
  MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x00b2aa3e));
1590
  MAD_F_MLA(hi, lo, X[11],  MAD_F(0x03768962));
1591
  MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0f426cb5));
1592
  MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0e313245));
1593
  MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0898c779));
1594
  MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0acf37ad));
1595
 
1596
  x[18] = x[35] = MAD_F_MLZ(hi, lo) - t1;
1597
 
1598
  MAD_F_ML0(hi, lo, X[4],   MAD_F(0x061f78aa));
1599
  MAD_F_MLA(hi, lo, X[13], -MAD_F(0x0ec835e8));
1600
 
1601
  t7 = MAD_F_MLZ(hi, lo);
1602
 
1603
  MAD_F_MLA(hi, lo, X[1],  -MAD_F(0x0cb19346));
1604
  MAD_F_MLA(hi, lo, X[7],   MAD_F(0x0fdcf549));
1605
  MAD_F_MLA(hi, lo, X[10],  MAD_F(0x0216a2a2));
1606
  MAD_F_MLA(hi, lo, X[16], -MAD_F(0x09bd7ca0));
1607
 
1608
  t2 = MAD_F_MLZ(hi, lo);
1609
 
1610
  MAD_F_MLA(hi, lo, X[0],   MAD_F(0x04cfb0e2));
1611
  MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0ffc19fd));
1612
  MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x0d7e8807));
1613
  MAD_F_MLA(hi, lo, X[5],   MAD_F(0x03768962));
1614
  MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x0bcbe352));
1615
  MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x0e313245));
1616
  MAD_F_MLA(hi, lo, X[9],   MAD_F(0x07635284));
1617
  MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0acf37ad));
1618
  MAD_F_MLA(hi, lo, X[12],  MAD_F(0x0f9ee890));
1619
  MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0898c779));
1620
  MAD_F_MLA(hi, lo, X[15],  MAD_F(0x00b2aa3e));
1621
  MAD_F_MLA(hi, lo, X[17],  MAD_F(0x0f426cb5));
1622
 
1623
  x[5]  = MAD_F_MLZ(hi, lo);
1624
  x[12] = -x[5];
1625
 
1626
  MAD_F_ML0(hi, lo, X[0],   MAD_F(0x0acf37ad));
1627
  MAD_F_MLA(hi, lo, X[2],  -MAD_F(0x0898c779));
1628
  MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0e313245));
1629
  MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0f426cb5));
1630
  MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x03768962));
1631
  MAD_F_MLA(hi, lo, X[8],   MAD_F(0x00b2aa3e));
1632
  MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x0ffc19fd));
1633
  MAD_F_MLA(hi, lo, X[11],  MAD_F(0x0f9ee890));
1634
  MAD_F_MLA(hi, lo, X[12], -MAD_F(0x04cfb0e2));
1635
  MAD_F_MLA(hi, lo, X[14],  MAD_F(0x07635284));
1636
  MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0d7e8807));
1637
  MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0bcbe352));
1638
 
1639
  x[0]  = MAD_F_MLZ(hi, lo) + t2;
1640
  x[17] = -x[0];
1641
 
1642
  MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0f9ee890));
1643
  MAD_F_MLA(hi, lo, X[2],  -MAD_F(0x07635284));
1644
  MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x00b2aa3e));
1645
  MAD_F_MLA(hi, lo, X[5],   MAD_F(0x0bcbe352));
1646
  MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0f426cb5));
1647
  MAD_F_MLA(hi, lo, X[8],   MAD_F(0x0d7e8807));
1648
  MAD_F_MLA(hi, lo, X[9],   MAD_F(0x0898c779));
1649
  MAD_F_MLA(hi, lo, X[11], -MAD_F(0x04cfb0e2));
1650
  MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0acf37ad));
1651
  MAD_F_MLA(hi, lo, X[14], -MAD_F(0x0ffc19fd));
1652
  MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0e313245));
1653
  MAD_F_MLA(hi, lo, X[17], -MAD_F(0x03768962));
1654
 
1655
  x[24] = x[29] = MAD_F_MLZ(hi, lo) + t2;
1656
 
1657
  MAD_F_ML0(hi, lo, X[1],  -MAD_F(0x0216a2a2));
1658
  MAD_F_MLA(hi, lo, X[7],  -MAD_F(0x09bd7ca0));
1659
  MAD_F_MLA(hi, lo, X[10],  MAD_F(0x0cb19346));
1660
  MAD_F_MLA(hi, lo, X[16],  MAD_F(0x0fdcf549));
1661
 
1662
  t3 = MAD_F_MLZ(hi, lo) + t7;
1663
 
1664
  MAD_F_ML0(hi, lo, X[0],   MAD_F(0x00b2aa3e));
1665
  MAD_F_MLA(hi, lo, X[2],   MAD_F(0x03768962));
1666
  MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x04cfb0e2));
1667
  MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x07635284));
1668
  MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0898c779));
1669
  MAD_F_MLA(hi, lo, X[8],   MAD_F(0x0acf37ad));
1670
  MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x0bcbe352));
1671
  MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0d7e8807));
1672
  MAD_F_MLA(hi, lo, X[12],  MAD_F(0x0e313245));
1673
  MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0f426cb5));
1674
  MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0f9ee890));
1675
  MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0ffc19fd));
1676
 
1677
  x[8] = MAD_F_MLZ(hi, lo) + t3;
1678
  x[9] = -x[8];
1679
 
1680
  MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0e313245));
1681
  MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0bcbe352));
1682
  MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0f9ee890));
1683
  MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0898c779));
1684
  MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x0ffc19fd));
1685
  MAD_F_MLA(hi, lo, X[8],   MAD_F(0x04cfb0e2));
1686
  MAD_F_MLA(hi, lo, X[9],   MAD_F(0x0f426cb5));
1687
  MAD_F_MLA(hi, lo, X[11], -MAD_F(0x00b2aa3e));
1688
  MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0d7e8807));
1689
  MAD_F_MLA(hi, lo, X[14], -MAD_F(0x03768962));
1690
  MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0acf37ad));
1691
  MAD_F_MLA(hi, lo, X[17],  MAD_F(0x07635284));
1692
 
1693
  x[21] = x[32] = MAD_F_MLZ(hi, lo) + t3;
1694
 
1695
  MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0d7e8807));
1696
  MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0f426cb5));
1697
  MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0acf37ad));
1698
  MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0ffc19fd));
1699
  MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x07635284));
1700
  MAD_F_MLA(hi, lo, X[8],   MAD_F(0x0f9ee890));
1701
  MAD_F_MLA(hi, lo, X[9],   MAD_F(0x03768962));
1702
  MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0e313245));
1703
  MAD_F_MLA(hi, lo, X[12],  MAD_F(0x00b2aa3e));
1704
  MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0bcbe352));
1705
  MAD_F_MLA(hi, lo, X[15], -MAD_F(0x04cfb0e2));
1706
  MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0898c779));
1707
 
1708
  x[20] = x[33] = MAD_F_MLZ(hi, lo) - t3;
1709
 
1710
  MAD_F_ML0(hi, lo, t14, -MAD_F(0x0ec835e8));
1711
  MAD_F_MLA(hi, lo, t15,  MAD_F(0x061f78aa));
1712
 
1713
  t4 = MAD_F_MLZ(hi, lo) - t7;
1714
 
1715
  MAD_F_ML0(hi, lo, t12, MAD_F(0x061f78aa));
1716
  MAD_F_MLA(hi, lo, t13, MAD_F(0x0ec835e8));
1717
 
1718
  x[4]  = MAD_F_MLZ(hi, lo) + t4;
1719
  x[13] = -x[4];
1720
 
1721
  MAD_F_ML0(hi, lo, t8,   MAD_F(0x09bd7ca0));
1722
  MAD_F_MLA(hi, lo, t9,  -MAD_F(0x0216a2a2));
1723
  MAD_F_MLA(hi, lo, t10,  MAD_F(0x0fdcf549));
1724
  MAD_F_MLA(hi, lo, t11, -MAD_F(0x0cb19346));
1725
 
1726
  x[1]  = MAD_F_MLZ(hi, lo) + t4;
1727
  x[16] = -x[1];
1728
 
1729
  MAD_F_ML0(hi, lo, t8,  -MAD_F(0x0fdcf549));
1730
  MAD_F_MLA(hi, lo, t9,  -MAD_F(0x0cb19346));
1731
  MAD_F_MLA(hi, lo, t10, -MAD_F(0x09bd7ca0));
1732
  MAD_F_MLA(hi, lo, t11, -MAD_F(0x0216a2a2));
1733
 
1734
  x[25] = x[28] = MAD_F_MLZ(hi, lo) + t4;
1735
 
1736
  MAD_F_ML0(hi, lo, X[1],  -MAD_F(0x0fdcf549));
1737
  MAD_F_MLA(hi, lo, X[7],  -MAD_F(0x0cb19346));
1738
  MAD_F_MLA(hi, lo, X[10], -MAD_F(0x09bd7ca0));
1739
  MAD_F_MLA(hi, lo, X[16], -MAD_F(0x0216a2a2));
1740
 
1741
  t5 = MAD_F_MLZ(hi, lo) - t6;
1742
 
1743
  MAD_F_ML0(hi, lo, X[0],   MAD_F(0x0898c779));
1744
  MAD_F_MLA(hi, lo, X[2],   MAD_F(0x04cfb0e2));
1745
  MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0bcbe352));
1746
  MAD_F_MLA(hi, lo, X[5],   MAD_F(0x00b2aa3e));
1747
  MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0e313245));
1748
  MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x03768962));
1749
  MAD_F_MLA(hi, lo, X[9],   MAD_F(0x0f9ee890));
1750
  MAD_F_MLA(hi, lo, X[11], -MAD_F(0x07635284));
1751
  MAD_F_MLA(hi, lo, X[12],  MAD_F(0x0ffc19fd));
1752
  MAD_F_MLA(hi, lo, X[14], -MAD_F(0x0acf37ad));
1753
  MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0f426cb5));
1754
  MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0d7e8807));
1755
 
1756
  x[2]  = MAD_F_MLZ(hi, lo) + t5;
1757
  x[15] = -x[2];
1758
 
1759
  MAD_F_ML0(hi, lo, X[0],   MAD_F(0x07635284));
1760
  MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0acf37ad));
1761
  MAD_F_MLA(hi, lo, X[3],   MAD_F(0x03768962));
1762
  MAD_F_MLA(hi, lo, X[5],   MAD_F(0x0d7e8807));
1763
  MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x00b2aa3e));
1764
  MAD_F_MLA(hi, lo, X[8],   MAD_F(0x0f426cb5));
1765
  MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x04cfb0e2));
1766
  MAD_F_MLA(hi, lo, X[11],  MAD_F(0x0ffc19fd));
1767
  MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0898c779));
1768
  MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0f9ee890));
1769
  MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0bcbe352));
1770
  MAD_F_MLA(hi, lo, X[17],  MAD_F(0x0e313245));
1771
 
1772
  x[3]  = MAD_F_MLZ(hi, lo) + t5;
1773
  x[14] = -x[3];
1774
 
1775
  MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0ffc19fd));
1776
  MAD_F_MLA(hi, lo, X[2],  -MAD_F(0x0f9ee890));
1777
  MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x0f426cb5));
1778
  MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0e313245));
1779
  MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x0d7e8807));
1780
  MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x0bcbe352));
1781
  MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x0acf37ad));
1782
  MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0898c779));
1783
  MAD_F_MLA(hi, lo, X[12], -MAD_F(0x07635284));
1784
  MAD_F_MLA(hi, lo, X[14], -MAD_F(0x04cfb0e2));
1785
  MAD_F_MLA(hi, lo, X[15], -MAD_F(0x03768962));
1786
  MAD_F_MLA(hi, lo, X[17], -MAD_F(0x00b2aa3e));
1787
 
1788
  x[26] = x[27] = MAD_F_MLZ(hi, lo) + t5;
1789
}
1790
 
1791
/*
1792
 * NAME:        III_imdct_l()
1793
 * DESCRIPTION: perform IMDCT and windowing for long blocks
1794
 */
1795
static
1796
void III_imdct_l(mad_fixed_t const X[18], mad_fixed_t z[36],
1797
                 unsigned int block_type)
1798
{
1799
  unsigned int i;
1800
 
1801
  /* IMDCT */
1802
 
1803
  imdct36(X, z);
1804
 
1805
  /* windowing */
1806
 
1807
  switch (block_type) {
1808
  case 0:  /* normal window */
1809
# if defined(ASO_INTERLEAVE1)
1810
    {
1811
      register mad_fixed_t tmp1, tmp2;
1812
 
1813
      tmp1 = window_l[0];
1814
      tmp2 = window_l[1];
1815
 
1816
      for (i = 0; i < 34; i += 2) {
1817
        z[i + 0] = mad_f_mul(z[i + 0], tmp1);
1818
        tmp1 = window_l[i + 2];
1819
        z[i + 1] = mad_f_mul(z[i + 1], tmp2);
1820
        tmp2 = window_l[i + 3];
1821
      }
1822
 
1823
      z[34] = mad_f_mul(z[34], tmp1);
1824
      z[35] = mad_f_mul(z[35], tmp2);
1825
    }
1826
# elif defined(ASO_INTERLEAVE2)
1827
    {
1828
      register mad_fixed_t tmp1, tmp2;
1829
 
1830
      tmp1 = z[0];
1831
      tmp2 = window_l[0];
1832
 
1833
      for (i = 0; i < 35; ++i) {
1834
        z[i] = mad_f_mul(tmp1, tmp2);
1835
        tmp1 = z[i + 1];
1836
        tmp2 = window_l[i + 1];
1837
      }
1838
 
1839
      z[35] = mad_f_mul(tmp1, tmp2);
1840
    }
1841
# elif 1
1842
    for (i = 0; i < 36; i += 4) {
1843
      z[i + 0] = mad_f_mul(z[i + 0], window_l[i + 0]);
1844
      z[i + 1] = mad_f_mul(z[i + 1], window_l[i + 1]);
1845
      z[i + 2] = mad_f_mul(z[i + 2], window_l[i + 2]);
1846
      z[i + 3] = mad_f_mul(z[i + 3], window_l[i + 3]);
1847
    }
1848
# else
1849
    for (i =  0; i < 36; ++i) z[i] = mad_f_mul(z[i], window_l[i]);
1850
# endif
1851
    break;
1852
 
1853
  case 1:  /* start block */
1854
    for (i =  0; i < 18; ++i) z[i] = mad_f_mul(z[i], window_l[i]);
1855
    /*  (i = 18; i < 24; ++i) z[i] unchanged */
1856
    for (i = 24; i < 30; ++i) z[i] = mad_f_mul(z[i], window_s[i - 18]);
1857
    for (i = 30; i < 36; ++i) z[i] = 0;
1858
    break;
1859
 
1860
  case 3:  /* stop block */
1861
    for (i =  0; i <  6; ++i) z[i] = 0;
1862
    for (i =  6; i < 12; ++i) z[i] = mad_f_mul(z[i], window_s[i - 6]);
1863
    /*  (i = 12; i < 18; ++i) z[i] unchanged */
1864
    for (i = 18; i < 36; ++i) z[i] = mad_f_mul(z[i], window_l[i]);
1865
    break;
1866
  }
1867
}
1868
# endif  /* ASO_IMDCT */
1869
 
1870
/*
1871
 * NAME:        III_imdct_s()
1872
 * DESCRIPTION: perform IMDCT and windowing for short blocks
1873
 */
1874
static
1875
void III_imdct_s(mad_fixed_t const X[18], mad_fixed_t z[36])
1876
{
1877
  mad_fixed_t y[36], *yptr;
1878
  mad_fixed_t const *wptr;
1879
  int w, i;
1880
  register mad_fixed64hi_t hi;
1881
  register mad_fixed64lo_t lo;
1882
 
1883
  /* IMDCT */
1884
 
1885
  yptr = &y[0];
1886
 
1887
  for (w = 0; w < 3; ++w) {
1888
    register mad_fixed_t const (*s)[6];
1889
 
1890
    s = imdct_s;
1891
 
1892
    for (i = 0; i < 3; ++i) {
1893
      MAD_F_ML0(hi, lo, X[0], (*s)[0]);
1894
      MAD_F_MLA(hi, lo, X[1], (*s)[1]);
1895
      MAD_F_MLA(hi, lo, X[2], (*s)[2]);
1896
      MAD_F_MLA(hi, lo, X[3], (*s)[3]);
1897
      MAD_F_MLA(hi, lo, X[4], (*s)[4]);
1898
      MAD_F_MLA(hi, lo, X[5], (*s)[5]);
1899
 
1900
      yptr[i + 0] = MAD_F_MLZ(hi, lo);
1901
      yptr[5 - i] = -yptr[i + 0];
1902
 
1903
      ++s;
1904
 
1905
      MAD_F_ML0(hi, lo, X[0], (*s)[0]);
1906
      MAD_F_MLA(hi, lo, X[1], (*s)[1]);
1907
      MAD_F_MLA(hi, lo, X[2], (*s)[2]);
1908
      MAD_F_MLA(hi, lo, X[3], (*s)[3]);
1909
      MAD_F_MLA(hi, lo, X[4], (*s)[4]);
1910
      MAD_F_MLA(hi, lo, X[5], (*s)[5]);
1911
 
1912
      yptr[ i + 6] = MAD_F_MLZ(hi, lo);
1913
      yptr[11 - i] = yptr[i + 6];
1914
 
1915
      ++s;
1916
    }
1917
 
1918
    yptr += 12;
1919
    X    += 6;
1920
  }
1921
 
1922
  /* windowing, overlapping and concatenation */
1923
 
1924
  yptr = &y[0];
1925
  wptr = &window_s[0];
1926
 
1927
  for (i = 0; i < 6; ++i) {
1928
    z[i +  0] = 0;
1929
    z[i +  6] = mad_f_mul(yptr[ 0 + 0], wptr[0]);
1930
 
1931
    MAD_F_ML0(hi, lo, yptr[ 0 + 6], wptr[6]);
1932
    MAD_F_MLA(hi, lo, yptr[12 + 0], wptr[0]);
1933
 
1934
    z[i + 12] = MAD_F_MLZ(hi, lo);
1935
 
1936
    MAD_F_ML0(hi, lo, yptr[12 + 6], wptr[6]);
1937
    MAD_F_MLA(hi, lo, yptr[24 + 0], wptr[0]);
1938
 
1939
    z[i + 18] = MAD_F_MLZ(hi, lo);
1940
 
1941
    z[i + 24] = mad_f_mul(yptr[24 + 6], wptr[6]);
1942
    z[i + 30] = 0;
1943
 
1944
    ++yptr;
1945
    ++wptr;
1946
  }
1947
}
1948
 
1949
/*
1950
 * NAME:        III_overlap()
1951
 * DESCRIPTION: perform overlap-add of windowed IMDCT outputs
1952
 */
1953
static
1954
void III_overlap(mad_fixed_t const output[36], mad_fixed_t overlap[18],
1955
                 mad_fixed_t sample[18][32], unsigned int sb)
1956
{
1957
  unsigned int i;
1958
 
1959
# if defined(ASO_INTERLEAVE2)
1960
  {
1961
    register mad_fixed_t tmp1, tmp2;
1962
 
1963
    tmp1 = overlap[0];
1964
    tmp2 = overlap[1];
1965
 
1966
    for (i = 0; i < 16; i += 2) {
1967
      sample[i + 0][sb] = output[i + 0] + tmp1;
1968
      overlap[i + 0]    = output[i + 0 + 18];
1969
      tmp1 = overlap[i + 2];
1970
 
1971
      sample[i + 1][sb] = output[i + 1] + tmp2;
1972
      overlap[i + 1]    = output[i + 1 + 18];
1973
      tmp2 = overlap[i + 3];
1974
    }
1975
 
1976
    sample[16][sb] = output[16] + tmp1;
1977
    overlap[16]    = output[16 + 18];
1978
    sample[17][sb] = output[17] + tmp2;
1979
    overlap[17]    = output[17 + 18];
1980
  }
1981
# elif 0
1982
  for (i = 0; i < 18; i += 2) {
1983
    sample[i + 0][sb] = output[i + 0] + overlap[i + 0];
1984
    overlap[i + 0]    = output[i + 0 + 18];
1985
 
1986
    sample[i + 1][sb] = output[i + 1] + overlap[i + 1];
1987
    overlap[i + 1]    = output[i + 1 + 18];
1988
  }
1989
# else
1990
  for (i = 0; i < 18; ++i) {
1991
    sample[i][sb] = output[i] + overlap[i];
1992
    overlap[i]    = output[i + 18];
1993
  }
1994
# endif
1995
}
1996
 
1997
/*
1998
 * NAME:        III_overlap_z()
1999
 * DESCRIPTION: perform "overlap-add" of zero IMDCT outputs
2000
 */
2001
static inline
2002
void III_overlap_z(mad_fixed_t overlap[18],
2003
                   mad_fixed_t sample[18][32], unsigned int sb)
2004
{
2005
  unsigned int i;
2006
 
2007
# if defined(ASO_INTERLEAVE2)
2008
  {
2009
    register mad_fixed_t tmp1, tmp2;
2010
 
2011
    tmp1 = overlap[0];
2012
    tmp2 = overlap[1];
2013
 
2014
    for (i = 0; i < 16; i += 2) {
2015
      sample[i + 0][sb] = tmp1;
2016
      overlap[i + 0]    = 0;
2017
      tmp1 = overlap[i + 2];
2018
 
2019
      sample[i + 1][sb] = tmp2;
2020
      overlap[i + 1]    = 0;
2021
      tmp2 = overlap[i + 3];
2022
    }
2023
 
2024
    sample[16][sb] = tmp1;
2025
    overlap[16]    = 0;
2026
    sample[17][sb] = tmp2;
2027
    overlap[17]    = 0;
2028
  }
2029
# else
2030
  for (i = 0; i < 18; ++i) {
2031
    sample[i][sb] = overlap[i];
2032
    overlap[i]    = 0;
2033
  }
2034
# endif
2035
}
2036
 
2037
/*
2038
 * NAME:        III_freqinver()
2039
 * DESCRIPTION: perform subband frequency inversion for odd sample lines
2040
 */
2041
static
2042
void III_freqinver(mad_fixed_t sample[18][32], unsigned int sb)
2043
{
2044
  unsigned int i;
2045
 
2046
# if 1 || defined(ASO_INTERLEAVE1) || defined(ASO_INTERLEAVE2)
2047
  {
2048
    register mad_fixed_t tmp1, tmp2;
2049
 
2050
    tmp1 = sample[1][sb];
2051
    tmp2 = sample[3][sb];
2052
 
2053
    for (i = 1; i < 13; i += 4) {
2054
      sample[i + 0][sb] = -tmp1;
2055
      tmp1 = sample[i + 4][sb];
2056
      sample[i + 2][sb] = -tmp2;
2057
      tmp2 = sample[i + 6][sb];
2058
    }
2059
 
2060
    sample[13][sb] = -tmp1;
2061
    tmp1 = sample[17][sb];
2062
    sample[15][sb] = -tmp2;
2063
    sample[17][sb] = -tmp1;
2064
  }
2065
# else
2066
  for (i = 1; i < 18; i += 2)
2067
    sample[i][sb] = -sample[i][sb];
2068
# endif
2069
}
2070
 
2071
/*
2072
 * NAME:        III_decode()
2073
 * DESCRIPTION: decode frame main_data
2074
 */
2075
static
2076
int III_decode(struct mad_bitptr *ptr, struct mad_frame *frame,
2077
                struct sideinfo *si, unsigned int nch)
2078
{
2079
  struct mad_header *header = &frame->header;
2080
  unsigned int sfreqi, ngr, gr;
2081
 
2082
  {
2083
    unsigned int sfreq;
2084
 
2085
    sfreq = header->samplerate;
2086
    if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
2087
      sfreq *= 2;
2088
 
2089
    /* 48000 => 0, 44100 => 1, 32000 => 2,
2090
       24000 => 3, 22050 => 4, 16000 => 5 */
2091
    sfreqi = ((sfreq >>  7) & 0x000f) +
2092
             ((sfreq >> 15) & 0x0001) - 8;
2093
 
2094
    if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
2095
      sfreqi += 3;
2096
  }
2097
 
2098
  /* scalefactors, Huffman decoding, requantization */
2099
 
2100
  ngr = (header->flags & MAD_FLAG_LSF_EXT) ? 1 : 2;
2101
 
2102
  for (gr = 0; gr < ngr; ++gr) {
2103
    struct granule *granule = &si->gr[gr];
2104
    unsigned char const *sfbwidth = 0;
2105
    mad_fixed_t xr[2][576];
2106
    unsigned int ch;
2107
    enum mad_error error;
2108
 
2109
    for (ch = 0; ch < nch; ++ch) {
2110
      struct channel *channel = &granule->ch[ch];
2111
      unsigned int part2_length;
2112
 
2113
      sfbwidth = sfbwidth_table[sfreqi].l;
2114
      if (channel->block_type == 2) {
2115
        sfbwidth = (channel->flags & mixed_block_flag) ?
2116
          sfbwidth_table[sfreqi].m : sfbwidth_table[sfreqi].s;
2117
      }
2118
 
2119
      if (header->flags & MAD_FLAG_LSF_EXT) {
2120
        part2_length = III_scalefactors_lsf(ptr, channel,
2121
                                            ch == 0 ? 0 : &si->gr[1].ch[1],
2122
                                            header->mode_extension);
2123
      }
2124
      else {
2125
        part2_length = III_scalefactors(ptr, channel, &si->gr[0].ch[ch],
2126
                                        gr == 0 ? 0 : si->scfsi[ch]);
2127
      }
2128
 
2129
      error = III_huffdecode(ptr, xr[ch], channel, sfbwidth, part2_length);
2130
      if (error)
2131
        return error;
2132
    }
2133
 
2134
    /* joint stereo processing */
2135
 
2136
    if (header->mode == MAD_MODE_JOINT_STEREO && header->mode_extension) {
2137
      error = III_stereo(xr, granule, header, sfbwidth);
2138
      if (error)
2139
        return error;
2140
    }
2141
 
2142
    /* reordering, alias reduction, IMDCT, overlap-add, frequency inversion */
2143
 
2144
    for (ch = 0; ch < nch; ++ch) {
2145
      struct channel const *channel = &granule->ch[ch];
2146
      mad_fixed_t (*sample)[32] = &frame->sbsample[ch][18 * gr];
2147
      unsigned int sb, l, i, sblimit;
2148
      mad_fixed_t output[36];
2149
 
2150
      if (channel->block_type == 2) {
2151
        III_reorder(xr[ch], channel, sfbwidth_table[sfreqi].s);
2152
 
2153
# if !defined(OPT_STRICT)
2154
        /*
2155
         * According to ISO/IEC 11172-3, "Alias reduction is not applied for
2156
         * granules with block_type == 2 (short block)." However, other
2157
         * sources suggest alias reduction should indeed be performed on the
2158
         * lower two subbands of mixed blocks. Most other implementations do
2159
         * this, so by default we will too.
2160
         */
2161
        if (channel->flags & mixed_block_flag)
2162
          III_aliasreduce(xr[ch], 36);
2163
# endif
2164
      }
2165
      else
2166
        III_aliasreduce(xr[ch], 576);
2167
 
2168
      l = 0;
2169
 
2170
      /* subbands 0-1 */
2171
 
2172
      if (channel->block_type != 2 || (channel->flags & mixed_block_flag)) {
2173
        unsigned int block_type;
2174
 
2175
        block_type = channel->block_type;
2176
        if (channel->flags & mixed_block_flag)
2177
          block_type = 0;
2178
 
2179
        /* long blocks */
2180
        for (sb = 0; sb < 2; ++sb, l += 18) {
2181
          III_imdct_l(&xr[ch][l], output, block_type);
2182
          III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
2183
        }
2184
      }
2185
      else {
2186
        /* short blocks */
2187
        for (sb = 0; sb < 2; ++sb, l += 18) {
2188
          III_imdct_s(&xr[ch][l], output);
2189
          III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
2190
        }
2191
      }
2192
 
2193
      III_freqinver(sample, 1);
2194
 
2195
      /* (nonzero) subbands 2-31 */
2196
 
2197
      i = 576;
2198
      while (i > 36 && xr[ch][i - 1] == 0)
2199
        --i;
2200
 
2201
      sblimit = 32 - (576 - i) / 18;
2202
 
2203
      if (channel->block_type != 2) {
2204
        /* long blocks */
2205
        for (sb = 2; sb < sblimit; ++sb, l += 18) {
2206
          III_imdct_l(&xr[ch][l], output, channel->block_type);
2207
          III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
2208
 
2209
          if (sb & 1)
2210
            III_freqinver(sample, sb);
2211
        }
2212
      }
2213
      else {
2214
        /* short blocks */
2215
        for (sb = 2; sb < sblimit; ++sb, l += 18) {
2216
          III_imdct_s(&xr[ch][l], output);
2217
          III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
2218
 
2219
          if (sb & 1)
2220
            III_freqinver(sample, sb);
2221
        }
2222
      }
2223
 
2224
      /* remaining (zero) subbands */
2225
 
2226
      for (sb = sblimit; sb < 32; ++sb) {
2227
        III_overlap_z((*frame->overlap)[ch][sb], sample, sb);
2228
 
2229
        if (sb & 1)
2230
          III_freqinver(sample, sb);
2231
      }
2232
    }
2233
  }
2234
 
2235
  return 0;
2236
}
2237
 
2238
unsigned char g_main_data[MAD_BUFFER_MDLEN];
2239
mad_fixed_t g_frame_overlap [2][32][18];
2240
 
2241
/*
2242
 * NAME:        layer->III()
2243
 * DESCRIPTION: decode a single Layer III frame
2244
 */
2245
int mad_layer_III(struct mad_stream *stream, struct mad_frame *frame)
2246
{
2247
  struct mad_header *header = &frame->header;
2248
  unsigned int nch, priv_bitlen, next_md_begin = 0;
2249
  unsigned int si_len, data_bitlen, md_len;
2250
  unsigned int frame_space, frame_used, frame_free;
2251
  struct mad_bitptr ptr;
2252
  struct sideinfo si;
2253
  enum mad_error error;
2254
  int result = 0;
2255
 
2256
  /* allocate Layer III dynamic structures */
2257
 
2258
  if (!stream->main_data)
2259
    stream->main_data = (unsigned char *)stream->buffer; //&g_main_data;
2260
  frame->overlap = &g_frame_overlap;
2261
 
2262
  nch = MAD_NCHANNELS(header);
2263
  si_len = (header->flags & MAD_FLAG_LSF_EXT) ?
2264
    (nch == 1 ? 9 : 17) : (nch == 1 ? 17 : 32);
2265
 
2266
  /* check frame sanity */
2267
 
2268
  if (stream->next_frame - mad_bit_nextbyte(&stream->ptr) <
2269
      (signed int) si_len) {
2270
    stream->error = MAD_ERROR_BADFRAMELEN;
2271
    stream->md_len = 0;
2272
    return -1;
2273
  }
2274
 
2275
  /* check CRC word */
2276
 
2277
  if (header->flags & MAD_FLAG_PROTECTION) {
2278
    header->crc_check =
2279
      mad_bit_crc(stream->ptr, si_len * CHAR_BIT, header->crc_check);
2280
 
2281
    if (header->crc_check != header->crc_target &&
2282
        !(frame->options & MAD_OPTION_IGNORECRC)) {
2283
      stream->error = MAD_ERROR_BADCRC;
2284
      result = -1;
2285
    }
2286
  }
2287
 
2288
  /* decode frame side information */
2289
 
2290
  error = III_sideinfo(&stream->ptr, nch, header->flags & MAD_FLAG_LSF_EXT,
2291
                       &si, &data_bitlen, &priv_bitlen);
2292
  if (error && result == 0) {
2293
    stream->error = error;
2294
    result = -1;
2295
  }
2296
 
2297
  header->flags        |= priv_bitlen;
2298
  header->private_bits |= si.private_bits;
2299
 
2300
  /* find main_data of next frame */
2301
 
2302
  {
2303
    struct mad_bitptr peek;
2304
    unsigned long header;
2305
 
2306
    mad_bit_init(&peek, stream->next_frame);
2307
 
2308
    header = mad_bit_read(&peek, 32);
2309
    if ((header & 0xffe60000L) /* syncword | layer */ == 0xffe20000L) {
2310
      if (!(header & 0x00010000L))  /* protection_bit */
2311
        mad_bit_skip(&peek, 16);  /* crc_check */
2312
 
2313
      next_md_begin =
2314
        mad_bit_read(&peek, (header & 0x00080000L) /* ID */ ? 9 : 8);
2315
    }
2316
 
2317
    mad_bit_finish(&peek);
2318
  }
2319
 
2320
  /* find main_data of this frame */
2321
 
2322
  frame_space = stream->next_frame - mad_bit_nextbyte(&stream->ptr);
2323
 
2324
  if (next_md_begin > si.main_data_begin + frame_space)
2325
    next_md_begin = 0;
2326
 
2327
  md_len = si.main_data_begin + frame_space - next_md_begin;
2328
 
2329
  frame_used = 0;
2330
 
2331
  if (si.main_data_begin == 0) {
2332
    ptr = stream->ptr;
2333
    stream->md_len = 0;
2334
 
2335
    frame_used = md_len;
2336
  }
2337
  else {
2338
    if (si.main_data_begin > stream->md_len) {
2339
      if (result == 0) {
2340
        stream->error = MAD_ERROR_BADDATAPTR;
2341
        result = -1;
2342
      }
2343
    }
2344
    else {
2345
       mad_bit_init(&ptr,
2346
                   stream->main_data + stream->md_len - si.main_data_begin);
2347
 
2348
      if (md_len > si.main_data_begin) {
2349
        assert(stream->md_len + md_len -
2350
               si.main_data_begin <= MAD_BUFFER_MDLEN);
2351
 
2352
        memcpy(stream->main_data + stream->md_len,
2353
               mad_bit_nextbyte(&stream->ptr),
2354
               frame_used = md_len - si.main_data_begin);
2355
        stream->md_len += frame_used;
2356
      }
2357
    }
2358
  }
2359
 
2360
  frame_free = frame_space - frame_used;
2361
 
2362
  /* decode main_data */
2363
 
2364
  if (result == 0) {
2365
    error = III_decode(&ptr, frame, &si, nch);
2366
    if (error) {
2367
      stream->error = error;
2368
      result = -1;
2369
    }
2370
  }
2371
 
2372
  /* designate ancillary bits */
2373
 
2374
  stream->anc_ptr    = ptr;
2375
  stream->anc_bitlen = md_len * CHAR_BIT - data_bitlen;
2376
 
2377
  /*  fprintf(stderr,
2378
          "main_data_begin:%u, md_len:%u, frame_free:%u, "
2379
          "data_bitlen:%u, anc_bitlen: %u\n",
2380
          si.main_data_begin, md_len, frame_free,
2381
          data_bitlen, stream->anc_bitlen);*/
2382
 
2383
  /* preload main_data buffer with up to 511 bytes for next frame(s) */
2384
 
2385
  if (frame_free >= next_md_begin) {
2386
    memcpy(stream->main_data,
2387
           stream->next_frame - next_md_begin, next_md_begin);
2388
 
2389
    stream->md_len = next_md_begin;
2390
  }
2391
  else {
2392
    if (md_len < si.main_data_begin) {
2393
      unsigned int extra;
2394
      extra = si.main_data_begin - md_len;
2395
      if (extra + frame_free > next_md_begin)
2396
        extra = next_md_begin - frame_free;
2397
 
2398
      if (extra < stream->md_len) {
2399
        memmove(stream->main_data,
2400
                stream->main_data + stream->md_len - extra, extra);
2401
        stream->md_len = extra;
2402
      }
2403
    }
2404
    else
2405
      stream->md_len = 0;
2406
 
2407
    memcpy(stream->main_data + stream->md_len,
2408
      stream->next_frame - frame_free, frame_free);
2409
    stream->md_len += frame_free;
2410
  }
2411
  return result;
2412
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.