layer3.c

00001 /*
00002  * libmad - MPEG audio decoder library
00003  * Copyright (C) 2000-2003 Underbit Technologies, Inc.
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018  *
00019  * $Id: layer3.c,v 1.1 2003/08/31 18:59:46 gabest Exp $
00020  */
00021 
00022 # ifdef HAVE_CONFIG_H
00023 #  include "config.h"
00024 # endif
00025 
00026 # include "global.h"
00027 
00028 # include <stdlib.h>
00029 # include <string.h>
00030 
00031 # ifdef HAVE_ASSERT_H
00032 #  include <assert.h>
00033 # endif
00034 
00035 # ifdef HAVE_LIMITS_H
00036 #  include <limits.h>
00037 # else
00038 #  define CHAR_BIT  8
00039 # endif
00040 
00041 # include "fixed.h"
00042 # include "bit.h"
00043 # include "stream.h"
00044 # include "frame.h"
00045 # include "huffman.h"
00046 # include "layer3.h"
00047 
00048 /* --- Layer III ----------------------------------------------------------- */
00049 
00050 enum {
00051   count1table_select = 0x01,
00052   scalefac_scale     = 0x02,
00053   preflag            = 0x04,
00054   mixed_block_flag   = 0x08
00055 };
00056 
00057 enum {
00058   I_STEREO  = 0x1,
00059   MS_STEREO = 0x2
00060 };
00061 
00062 struct sideinfo {
00063   unsigned int main_data_begin;
00064   unsigned int private_bits;
00065 
00066   unsigned char scfsi[2];
00067 
00068   struct granule {
00069     struct channel {
00070       /* from side info */
00071       unsigned short part2_3_length;
00072       unsigned short big_values;
00073       unsigned short global_gain;
00074       unsigned short scalefac_compress;
00075 
00076       unsigned char flags;
00077       unsigned char block_type;
00078       unsigned char table_select[3];
00079       unsigned char subblock_gain[3];
00080       unsigned char region0_count;
00081       unsigned char region1_count;
00082 
00083       /* from main_data */
00084       unsigned char scalefac[39];       /* scalefac_l and/or scalefac_s */
00085     } ch[2];
00086   } gr[2];
00087 };
00088 
00089 /*
00090  * scalefactor bit lengths
00091  * derived from section 2.4.2.7 of ISO/IEC 11172-3
00092  */
00093 static
00094 struct {
00095   unsigned char slen1;
00096   unsigned char slen2;
00097 } const sflen_table[16] = {
00098   { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 },
00099   { 3, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 },
00100   { 2, 1 }, { 2, 2 }, { 2, 3 }, { 3, 1 },
00101   { 3, 2 }, { 3, 3 }, { 4, 2 }, { 4, 3 }
00102 };
00103 
00104 /*
00105  * number of LSF scalefactor band values
00106  * derived from section 2.4.3.2 of ISO/IEC 13818-3
00107  */
00108 static
00109 unsigned char const nsfb_table[6][3][4] = {
00110   { {  6,  5,  5, 5 },
00111     {  9,  9,  9, 9 },
00112     {  6,  9,  9, 9 } },
00113 
00114   { {  6,  5,  7, 3 },
00115     {  9,  9, 12, 6 },
00116     {  6,  9, 12, 6 } },
00117 
00118   { { 11, 10,  0, 0 },
00119     { 18, 18,  0, 0 },
00120     { 15, 18,  0, 0 } },
00121 
00122   { {  7,  7,  7, 0 },
00123     { 12, 12, 12, 0 },
00124     {  6, 15, 12, 0 } },
00125 
00126   { {  6,  6,  6, 3 },
00127     { 12,  9,  9, 6 },
00128     {  6, 12,  9, 6 } },
00129 
00130   { {  8,  8,  5, 0 },
00131     { 15, 12,  9, 0 },
00132     {  6, 18,  9, 0 } }
00133 };
00134 
00135 /*
00136  * MPEG-1 scalefactor band widths
00137  * derived from Table B.8 of ISO/IEC 11172-3
00138  */
00139 static
00140 unsigned char const sfb_48000_long[] = {
00141    4,  4,  4,  4,  4,  4,  6,  6,  6,   8,  10,
00142   12, 16, 18, 22, 28, 34, 40, 46, 54,  54, 192
00143 };
00144 
00145 static
00146 unsigned char const sfb_44100_long[] = {
00147    4,  4,  4,  4,  4,  4,  6,  6,  8,   8,  10,
00148   12, 16, 20, 24, 28, 34, 42, 50, 54,  76, 158
00149 };
00150 
00151 static
00152 unsigned char const sfb_32000_long[] = {
00153    4,  4,  4,  4,  4,  4,  6,  6,  8,  10,  12,
00154   16, 20, 24, 30, 38, 46, 56, 68, 84, 102,  26
00155 };
00156 
00157 static
00158 unsigned char const sfb_48000_short[] = {
00159    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  6,
00160    6,  6,  6,  6,  6, 10, 10, 10, 12, 12, 12, 14, 14,
00161   14, 16, 16, 16, 20, 20, 20, 26, 26, 26, 66, 66, 66
00162 };
00163 
00164 static
00165 unsigned char const sfb_44100_short[] = {
00166    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  6,
00167    6,  6,  8,  8,  8, 10, 10, 10, 12, 12, 12, 14, 14,
00168   14, 18, 18, 18, 22, 22, 22, 30, 30, 30, 56, 56, 56
00169 };
00170 
00171 static
00172 unsigned char const sfb_32000_short[] = {
00173    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  6,
00174    6,  6,  8,  8,  8, 12, 12, 12, 16, 16, 16, 20, 20,
00175   20, 26, 26, 26, 34, 34, 34, 42, 42, 42, 12, 12, 12
00176 };
00177 
00178 static
00179 unsigned char const sfb_48000_mixed[] = {
00180   /* long */   4,  4,  4,  4,  4,  4,  6,  6,
00181   /* short */  4,  4,  4,  6,  6,  6,  6,  6,  6, 10,
00182               10, 10, 12, 12, 12, 14, 14, 14, 16, 16,
00183               16, 20, 20, 20, 26, 26, 26, 66, 66, 66
00184 };
00185 
00186 static
00187 unsigned char const sfb_44100_mixed[] = {
00188   /* long */   4,  4,  4,  4,  4,  4,  6,  6,
00189   /* short */  4,  4,  4,  6,  6,  6,  8,  8,  8, 10,
00190               10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
00191               18, 22, 22, 22, 30, 30, 30, 56, 56, 56
00192 };
00193 
00194 static
00195 unsigned char const sfb_32000_mixed[] = {
00196   /* long */   4,  4,  4,  4,  4,  4,  6,  6,
00197   /* short */  4,  4,  4,  6,  6,  6,  8,  8,  8, 12,
00198               12, 12, 16, 16, 16, 20, 20, 20, 26, 26,
00199               26, 34, 34, 34, 42, 42, 42, 12, 12, 12
00200 };
00201 
00202 /*
00203  * MPEG-2 scalefactor band widths
00204  * derived from Table B.2 of ISO/IEC 13818-3
00205  */
00206 static
00207 unsigned char const sfb_24000_long[] = {
00208    6,  6,  6,  6,  6,  6,  8, 10, 12,  14,  16,
00209   18, 22, 26, 32, 38, 46, 54, 62, 70,  76,  36
00210 };
00211 
00212 static
00213 unsigned char const sfb_22050_long[] = {
00214    6,  6,  6,  6,  6,  6,  8, 10, 12,  14,  16,
00215   20, 24, 28, 32, 38, 46, 52, 60, 68,  58,  54
00216 };
00217 
00218 # define sfb_16000_long  sfb_22050_long
00219 
00220 static
00221 unsigned char const sfb_24000_short[] = {
00222    4,  4,  4,  4,  4,  4,  4,  4,  4,  6,  6,  6,  8,
00223    8,  8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
00224   18, 24, 24, 24, 32, 32, 32, 44, 44, 44, 12, 12, 12
00225 };
00226 
00227 static
00228 unsigned char const sfb_22050_short[] = {
00229    4,  4,  4,  4,  4,  4,  4,  4,  4,  6,  6,  6,  6,
00230    6,  6,  8,  8,  8, 10, 10, 10, 14, 14, 14, 18, 18,
00231   18, 26, 26, 26, 32, 32, 32, 42, 42, 42, 18, 18, 18
00232 };
00233 
00234 static
00235 unsigned char const sfb_16000_short[] = {
00236    4,  4,  4,  4,  4,  4,  4,  4,  4,  6,  6,  6,  8,
00237    8,  8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18,
00238   18, 24, 24, 24, 30, 30, 30, 40, 40, 40, 18, 18, 18
00239 };
00240 
00241 static
00242 unsigned char const sfb_24000_mixed[] = {
00243   /* long */   6,  6,  6,  6,  6,  6,
00244   /* short */  6,  6,  6,  8,  8,  8, 10, 10, 10, 12,
00245               12, 12, 14, 14, 14, 18, 18, 18, 24, 24,
00246               24, 32, 32, 32, 44, 44, 44, 12, 12, 12
00247 };
00248 
00249 static
00250 unsigned char const sfb_22050_mixed[] = {
00251   /* long */   6,  6,  6,  6,  6,  6,
00252   /* short */  6,  6,  6,  6,  6,  6,  8,  8,  8, 10,
00253               10, 10, 14, 14, 14, 18, 18, 18, 26, 26,
00254               26, 32, 32, 32, 42, 42, 42, 18, 18, 18
00255 };
00256 
00257 static
00258 unsigned char const sfb_16000_mixed[] = {
00259   /* long */   6,  6,  6,  6,  6,  6,
00260   /* short */  6,  6,  6,  8,  8,  8, 10, 10, 10, 12,
00261               12, 12, 14, 14, 14, 18, 18, 18, 24, 24,
00262               24, 30, 30, 30, 40, 40, 40, 18, 18, 18
00263 };
00264 
00265 /*
00266  * MPEG 2.5 scalefactor band widths
00267  * derived from public sources
00268  */
00269 # define sfb_12000_long  sfb_16000_long
00270 # define sfb_11025_long  sfb_12000_long
00271 
00272 static
00273 unsigned char const sfb_8000_long[] = {
00274   12, 12, 12, 12, 12, 12, 16, 20, 24,  28,  32,
00275   40, 48, 56, 64, 76, 90,  2,  2,  2,   2,   2
00276 };
00277 
00278 # define sfb_12000_short  sfb_16000_short
00279 # define sfb_11025_short  sfb_12000_short
00280 
00281 static
00282 unsigned char const sfb_8000_short[] = {
00283    8,  8,  8,  8,  8,  8,  8,  8,  8, 12, 12, 12, 16,
00284   16, 16, 20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36,
00285   36,  2,  2,  2,  2,  2,  2,  2,  2,  2, 26, 26, 26
00286 };
00287 
00288 # define sfb_12000_mixed  sfb_16000_mixed
00289 # define sfb_11025_mixed  sfb_12000_mixed
00290 
00291 /* the 8000 Hz short block scalefactor bands do not break after
00292    the first 36 frequency lines, so this is probably wrong */
00293 static
00294 unsigned char const sfb_8000_mixed[] = {
00295   /* long */  12, 12, 12,
00296   /* short */  4,  4,  4,  8,  8,  8, 12, 12, 12, 16, 16, 16,
00297               20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36, 36,
00298                2,  2,  2,  2,  2,  2,  2,  2,  2, 26, 26, 26
00299 };
00300 
00301 static
00302 struct {
00303   unsigned char const *l;
00304   unsigned char const *s;
00305   unsigned char const *m;
00306 } const sfbwidth_table[9] = {
00307   { sfb_48000_long, sfb_48000_short, sfb_48000_mixed },
00308   { sfb_44100_long, sfb_44100_short, sfb_44100_mixed },
00309   { sfb_32000_long, sfb_32000_short, sfb_32000_mixed },
00310   { sfb_24000_long, sfb_24000_short, sfb_24000_mixed },
00311   { sfb_22050_long, sfb_22050_short, sfb_22050_mixed },
00312   { sfb_16000_long, sfb_16000_short, sfb_16000_mixed },
00313   { sfb_12000_long, sfb_12000_short, sfb_12000_mixed },
00314   { sfb_11025_long, sfb_11025_short, sfb_11025_mixed },
00315   {  sfb_8000_long,  sfb_8000_short,  sfb_8000_mixed }
00316 };
00317 
00318 /*
00319  * scalefactor band preemphasis (used only when preflag is set)
00320  * derived from Table B.6 of ISO/IEC 11172-3
00321  */
00322 static
00323 unsigned char const pretab[22] = {
00324   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0
00325 };
00326 
00327 /*
00328  * table for requantization
00329  *
00330  * rq_table[x].mantissa * 2^(rq_table[x].exponent) = x^(4/3)
00331  */
00332 static
00333 struct fixedfloat {
00334   unsigned long mantissa  : 27;
00335   unsigned short exponent :  5;
00336 } const rq_table[8207] = {
00337 # include "rq_table.dat"
00338 };
00339 
00340 /*
00341  * fractional powers of two
00342  * used for requantization and joint stereo decoding
00343  *
00344  * root_table[3 + x] = 2^(x/4)
00345  */
00346 static
00347 mad_fixed_t const root_table[7] = {
00348   MAD_F(0x09837f05) /* 2^(-3/4) == 0.59460355750136 */,
00349   MAD_F(0x0b504f33) /* 2^(-2/4) == 0.70710678118655 */,
00350   MAD_F(0x0d744fcd) /* 2^(-1/4) == 0.84089641525371 */,
00351   MAD_F(0x10000000) /* 2^( 0/4) == 1.00000000000000 */,
00352   MAD_F(0x1306fe0a) /* 2^(+1/4) == 1.18920711500272 */,
00353   MAD_F(0x16a09e66) /* 2^(+2/4) == 1.41421356237310 */,
00354   MAD_F(0x1ae89f99) /* 2^(+3/4) == 1.68179283050743 */
00355 };
00356 
00357 /*
00358  * coefficients for aliasing reduction
00359  * derived from Table B.9 of ISO/IEC 11172-3
00360  *
00361  *  c[]  = { -0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037 }
00362  * cs[i] =    1 / sqrt(1 + c[i]^2)
00363  * ca[i] = c[i] / sqrt(1 + c[i]^2)
00364  */
00365 static
00366 mad_fixed_t const cs[8] = {
00367   +MAD_F(0x0db84a81) /* +0.857492926 */, +MAD_F(0x0e1b9d7f) /* +0.881741997 */,
00368   +MAD_F(0x0f31adcf) /* +0.949628649 */, +MAD_F(0x0fbba815) /* +0.983314592 */,
00369   +MAD_F(0x0feda417) /* +0.995517816 */, +MAD_F(0x0ffc8fc8) /* +0.999160558 */,
00370   +MAD_F(0x0fff964c) /* +0.999899195 */, +MAD_F(0x0ffff8d3) /* +0.999993155 */
00371 };
00372 
00373 static
00374 mad_fixed_t const ca[8] = {
00375   -MAD_F(0x083b5fe7) /* -0.514495755 */, -MAD_F(0x078c36d2) /* -0.471731969 */,
00376   -MAD_F(0x05039814) /* -0.313377454 */, -MAD_F(0x02e91dd1) /* -0.181913200 */,
00377   -MAD_F(0x0183603a) /* -0.094574193 */, -MAD_F(0x00a7cb87) /* -0.040965583 */,
00378   -MAD_F(0x003a2847) /* -0.014198569 */, -MAD_F(0x000f27b4) /* -0.003699975 */
00379 };
00380 
00381 /*
00382  * IMDCT coefficients for short blocks
00383  * derived from section 2.4.3.4.10.2 of ISO/IEC 11172-3
00384  *
00385  * imdct_s[i/even][k] = cos((PI / 24) * (2 *       (i / 2) + 7) * (2 * k + 1))
00386  * imdct_s[i /odd][k] = cos((PI / 24) * (2 * (6 + (i-1)/2) + 7) * (2 * k + 1))
00387  */
00388 static
00389 mad_fixed_t const imdct_s[6][6] = {
00390 # include "imdct_s.dat"
00391 };
00392 
00393 # if !defined(ASO_IMDCT)
00394 /*
00395  * windowing coefficients for long blocks
00396  * derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
00397  *
00398  * window_l[i] = sin((PI / 36) * (i + 1/2))
00399  */
00400 static
00401 mad_fixed_t const window_l[36] = {
00402   MAD_F(0x00b2aa3e) /* 0.043619387 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
00403   MAD_F(0x03768962) /* 0.216439614 */, MAD_F(0x04cfb0e2) /* 0.300705800 */,
00404   MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x07635284) /* 0.461748613 */,
00405   MAD_F(0x0898c779) /* 0.537299608 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
00406   MAD_F(0x0acf37ad) /* 0.675590208 */, MAD_F(0x0bcbe352) /* 0.737277337 */,
00407   MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x0d7e8807) /* 0.843391446 */,
00408 
00409   MAD_F(0x0e313245) /* 0.887010833 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
00410   MAD_F(0x0f426cb5) /* 0.953716951 */, MAD_F(0x0f9ee890) /* 0.976296007 */,
00411   MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ffc19fd) /* 0.999048222 */,
00412   MAD_F(0x0ffc19fd) /* 0.999048222 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
00413   MAD_F(0x0f9ee890) /* 0.976296007 */, MAD_F(0x0f426cb5) /* 0.953716951 */,
00414   MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0e313245) /* 0.887010833 */,
00415 
00416   MAD_F(0x0d7e8807) /* 0.843391446 */, MAD_F(0x0cb19346) /* 0.793353340 */,
00417   MAD_F(0x0bcbe352) /* 0.737277337 */, MAD_F(0x0acf37ad) /* 0.675590208 */,
00418   MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0898c779) /* 0.537299608 */,
00419   MAD_F(0x07635284) /* 0.461748613 */, MAD_F(0x061f78aa) /* 0.382683432 */,
00420   MAD_F(0x04cfb0e2) /* 0.300705800 */, MAD_F(0x03768962) /* 0.216439614 */,
00421   MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x00b2aa3e) /* 0.043619387 */,
00422 };
00423 # endif  /* ASO_IMDCT */
00424 
00425 /*
00426  * windowing coefficients for short blocks
00427  * derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
00428  *
00429  * window_s[i] = sin((PI / 12) * (i + 1/2))
00430  */
00431 static
00432 mad_fixed_t const window_s[12] = {
00433   MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x061f78aa) /* 0.382683432 */,
00434   MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0cb19346) /* 0.793353340 */,
00435   MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
00436   MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
00437   MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
00438   MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
00439 };
00440 
00441 /*
00442  * coefficients for intensity stereo processing
00443  * derived from section 2.4.3.4.9.3 of ISO/IEC 11172-3
00444  *
00445  * is_ratio[i] = tan(i * (PI / 12))
00446  * is_table[i] = is_ratio[i] / (1 + is_ratio[i])
00447  */
00448 static
00449 mad_fixed_t const is_table[7] = {
00450   MAD_F(0x00000000) /* 0.000000000 */,
00451   MAD_F(0x0361962f) /* 0.211324865 */,
00452   MAD_F(0x05db3d74) /* 0.366025404 */,
00453   MAD_F(0x08000000) /* 0.500000000 */,
00454   MAD_F(0x0a24c28c) /* 0.633974596 */,
00455   MAD_F(0x0c9e69d1) /* 0.788675135 */,
00456   MAD_F(0x10000000) /* 1.000000000 */
00457 };
00458 
00459 /*
00460  * coefficients for LSF intensity stereo processing
00461  * derived from section 2.4.3.2 of ISO/IEC 13818-3
00462  *
00463  * is_lsf_table[0][i] = (1 / sqrt(sqrt(2)))^(i + 1)
00464  * is_lsf_table[1][i] = (1 /      sqrt(2)) ^(i + 1)
00465  */
00466 static
00467 mad_fixed_t const is_lsf_table[2][15] = {
00468   {
00469     MAD_F(0x0d744fcd) /* 0.840896415 */,
00470     MAD_F(0x0b504f33) /* 0.707106781 */,
00471     MAD_F(0x09837f05) /* 0.594603558 */,
00472     MAD_F(0x08000000) /* 0.500000000 */,
00473     MAD_F(0x06ba27e6) /* 0.420448208 */,
00474     MAD_F(0x05a8279a) /* 0.353553391 */,
00475     MAD_F(0x04c1bf83) /* 0.297301779 */,
00476     MAD_F(0x04000000) /* 0.250000000 */,
00477     MAD_F(0x035d13f3) /* 0.210224104 */,
00478     MAD_F(0x02d413cd) /* 0.176776695 */,
00479     MAD_F(0x0260dfc1) /* 0.148650889 */,
00480     MAD_F(0x02000000) /* 0.125000000 */,
00481     MAD_F(0x01ae89fa) /* 0.105112052 */,
00482     MAD_F(0x016a09e6) /* 0.088388348 */,
00483     MAD_F(0x01306fe1) /* 0.074325445 */
00484   }, {
00485     MAD_F(0x0b504f33) /* 0.707106781 */,
00486     MAD_F(0x08000000) /* 0.500000000 */,
00487     MAD_F(0x05a8279a) /* 0.353553391 */,
00488     MAD_F(0x04000000) /* 0.250000000 */,
00489     MAD_F(0x02d413cd) /* 0.176776695 */,
00490     MAD_F(0x02000000) /* 0.125000000 */,
00491     MAD_F(0x016a09e6) /* 0.088388348 */,
00492     MAD_F(0x01000000) /* 0.062500000 */,
00493     MAD_F(0x00b504f3) /* 0.044194174 */,
00494     MAD_F(0x00800000) /* 0.031250000 */,
00495     MAD_F(0x005a827a) /* 0.022097087 */,
00496     MAD_F(0x00400000) /* 0.015625000 */,
00497     MAD_F(0x002d413d) /* 0.011048543 */,
00498     MAD_F(0x00200000) /* 0.007812500 */,
00499     MAD_F(0x0016a09e) /* 0.005524272 */
00500   }
00501 };
00502 
00503 /*
00504  * NAME:        III_sideinfo()
00505  * DESCRIPTION: decode frame side information from a bitstream
00506  */
00507 static
00508 enum mad_error III_sideinfo(struct mad_bitptr *ptr, unsigned int nch,
00509                             int lsf, struct sideinfo *si,
00510                             unsigned int *data_bitlen,
00511                             unsigned int *priv_bitlen)
00512 {
00513   unsigned int ngr, gr, ch, i;
00514   enum mad_error result = MAD_ERROR_NONE;
00515 
00516   *data_bitlen = 0;
00517   *priv_bitlen = lsf ? ((nch == 1) ? 1 : 2) : ((nch == 1) ? 5 : 3);
00518 
00519   si->main_data_begin = mad_bit_read(ptr, lsf ? 8 : 9);
00520   si->private_bits    = mad_bit_read(ptr, *priv_bitlen);
00521 
00522   ngr = 1;
00523   if (!lsf) {
00524     ngr = 2;
00525 
00526     for (ch = 0; ch < nch; ++ch)
00527       si->scfsi[ch] = mad_bit_read(ptr, 4);
00528   }
00529 
00530   for (gr = 0; gr < ngr; ++gr) {
00531     struct granule *granule = &si->gr[gr];
00532 
00533     for (ch = 0; ch < nch; ++ch) {
00534       struct channel *channel = &granule->ch[ch];
00535 
00536       channel->part2_3_length    = mad_bit_read(ptr, 12);
00537       channel->big_values        = mad_bit_read(ptr, 9);
00538       channel->global_gain       = mad_bit_read(ptr, 8);
00539       channel->scalefac_compress = mad_bit_read(ptr, lsf ? 9 : 4);
00540 
00541       *data_bitlen += channel->part2_3_length;
00542 
00543       if (channel->big_values > 288 && result == 0)
00544         result = MAD_ERROR_BADBIGVALUES;
00545 
00546       channel->flags = 0;
00547 
00548       /* window_switching_flag */
00549       if (mad_bit_read(ptr, 1)) {
00550         channel->block_type = mad_bit_read(ptr, 2);
00551 
00552         if (channel->block_type == 0 && result == 0)
00553           result = MAD_ERROR_BADBLOCKTYPE;
00554 
00555         if (!lsf && channel->block_type == 2 && si->scfsi[ch] && result == 0)
00556           result = MAD_ERROR_BADSCFSI;
00557 
00558         channel->region0_count = 7;
00559         channel->region1_count = 36;
00560 
00561         if (mad_bit_read(ptr, 1))
00562           channel->flags |= mixed_block_flag;
00563         else if (channel->block_type == 2)
00564           channel->region0_count = 8;
00565 
00566         for (i = 0; i < 2; ++i)
00567           channel->table_select[i] = mad_bit_read(ptr, 5);
00568 
00569 # if defined(DEBUG)
00570         channel->table_select[2] = 4;  /* not used */
00571 # endif
00572 
00573         for (i = 0; i < 3; ++i)
00574           channel->subblock_gain[i] = mad_bit_read(ptr, 3);
00575       }
00576       else {
00577         channel->block_type = 0;
00578 
00579         for (i = 0; i < 3; ++i)
00580           channel->table_select[i] = mad_bit_read(ptr, 5);
00581 
00582         channel->region0_count = mad_bit_read(ptr, 4);
00583         channel->region1_count = mad_bit_read(ptr, 3);
00584       }
00585 
00586       /* [preflag,] scalefac_scale, count1table_select */
00587       channel->flags |= mad_bit_read(ptr, lsf ? 2 : 3);
00588     }
00589   }
00590 
00591   return result;
00592 }
00593 
00594 /*
00595  * NAME:        III_scalefactors_lsf()
00596  * DESCRIPTION: decode channel scalefactors for LSF from a bitstream
00597  */
00598 static
00599 unsigned int III_scalefactors_lsf(struct mad_bitptr *ptr,
00600                                   struct channel *channel,
00601                                   struct channel *gr1ch, int mode_extension)
00602 {
00603   struct mad_bitptr start;
00604   unsigned int scalefac_compress, index, slen[4], part, n, i;
00605   unsigned char const *nsfb;
00606 
00607   start = *ptr;
00608 
00609   scalefac_compress = channel->scalefac_compress;
00610   index = (channel->block_type == 2) ?
00611     ((channel->flags & mixed_block_flag) ? 2 : 1) : 0;
00612 
00613   if (!((mode_extension & I_STEREO) && gr1ch)) {
00614     if (scalefac_compress < 400) {
00615       slen[0] = (scalefac_compress >> 4) / 5;
00616       slen[1] = (scalefac_compress >> 4) % 5;
00617       slen[2] = (scalefac_compress % 16) >> 2;
00618       slen[3] =  scalefac_compress %  4;
00619 
00620       nsfb = nsfb_table[0][index];
00621     }
00622     else if (scalefac_compress < 500) {
00623       scalefac_compress -= 400;
00624 
00625       slen[0] = (scalefac_compress >> 2) / 5;
00626       slen[1] = (scalefac_compress >> 2) % 5;
00627       slen[2] =  scalefac_compress %  4;
00628       slen[3] = 0;
00629 
00630       nsfb = nsfb_table[1][index];
00631     }
00632     else {
00633       scalefac_compress -= 500;
00634 
00635       slen[0] = scalefac_compress / 3;
00636       slen[1] = scalefac_compress % 3;
00637       slen[2] = 0;
00638       slen[3] = 0;
00639 
00640       channel->flags |= preflag;
00641 
00642       nsfb = nsfb_table[2][index];
00643     }
00644 
00645     n = 0;
00646     for (part = 0; part < 4; ++part) {
00647       for (i = 0; i < nsfb[part]; ++i)
00648         channel->scalefac[n++] = mad_bit_read(ptr, slen[part]);
00649     }
00650 
00651     while (n < 39)
00652       channel->scalefac[n++] = 0;
00653   }
00654   else {  /* (mode_extension & I_STEREO) && gr1ch (i.e. ch == 1) */
00655     scalefac_compress >>= 1;
00656 
00657     if (scalefac_compress < 180) {
00658       slen[0] =  scalefac_compress / 36;
00659       slen[1] = (scalefac_compress % 36) / 6;
00660       slen[2] = (scalefac_compress % 36) % 6;
00661       slen[3] = 0;
00662 
00663       nsfb = nsfb_table[3][index];
00664     }
00665     else if (scalefac_compress < 244) {
00666       scalefac_compress -= 180;
00667 
00668       slen[0] = (scalefac_compress % 64) >> 4;
00669       slen[1] = (scalefac_compress % 16) >> 2;
00670       slen[2] =  scalefac_compress %  4;
00671       slen[3] = 0;
00672 
00673       nsfb = nsfb_table[4][index];
00674     }
00675     else {
00676       scalefac_compress -= 244;
00677 
00678       slen[0] = scalefac_compress / 3;
00679       slen[1] = scalefac_compress % 3;
00680       slen[2] = 0;
00681       slen[3] = 0;
00682 
00683       nsfb = nsfb_table[5][index];
00684     }
00685 
00686     n = 0;
00687     for (part = 0; part < 4; ++part) {
00688       unsigned int max, is_pos;
00689 
00690       max = (1 << slen[part]) - 1;
00691 
00692       for (i = 0; i < nsfb[part]; ++i) {
00693         is_pos = mad_bit_read(ptr, slen[part]);
00694 
00695         channel->scalefac[n] = is_pos;
00696         gr1ch->scalefac[n++] = (is_pos == max);
00697       }
00698     }
00699 
00700     while (n < 39) {
00701       channel->scalefac[n] = 0;
00702       gr1ch->scalefac[n++] = 0;  /* apparently not illegal */
00703     }
00704   }
00705 
00706   return mad_bit_length(&start, ptr);
00707 }
00708 
00709 /*
00710  * NAME:        III_scalefactors()
00711  * DESCRIPTION: decode channel scalefactors of one granule from a bitstream
00712  */
00713 static
00714 unsigned int III_scalefactors(struct mad_bitptr *ptr, struct channel *channel,
00715                               struct channel const *gr0ch, unsigned int scfsi)
00716 {
00717   struct mad_bitptr start;
00718   unsigned int slen1, slen2, sfbi;
00719 
00720   start = *ptr;
00721 
00722   slen1 = sflen_table[channel->scalefac_compress].slen1;
00723   slen2 = sflen_table[channel->scalefac_compress].slen2;
00724 
00725   if (channel->block_type == 2) {
00726     unsigned int nsfb;
00727 
00728     sfbi = 0;
00729 
00730     nsfb = (channel->flags & mixed_block_flag) ? 8 + 3 * 3 : 6 * 3;
00731     while (nsfb--)
00732       channel->scalefac[sfbi++] = mad_bit_read(ptr, slen1);
00733 
00734     nsfb = 6 * 3;
00735     while (nsfb--)
00736       channel->scalefac[sfbi++] = mad_bit_read(ptr, slen2);
00737 
00738     nsfb = 1 * 3;
00739     while (nsfb--)
00740       channel->scalefac[sfbi++] = 0;
00741   }
00742   else {  /* channel->block_type != 2 */
00743     if (scfsi & 0x8) {
00744       for (sfbi = 0; sfbi < 6; ++sfbi)
00745         channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
00746     }
00747     else {
00748       for (sfbi = 0; sfbi < 6; ++sfbi)
00749         channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
00750     }
00751 
00752     if (scfsi & 0x4) {
00753       for (sfbi = 6; sfbi < 11; ++sfbi)
00754         channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
00755     }
00756     else {
00757       for (sfbi = 6; sfbi < 11; ++sfbi)
00758         channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
00759     }
00760 
00761     if (scfsi & 0x2) {
00762       for (sfbi = 11; sfbi < 16; ++sfbi)
00763         channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
00764     }
00765     else {
00766       for (sfbi = 11; sfbi < 16; ++sfbi)
00767         channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
00768     }
00769 
00770     if (scfsi & 0x1) {
00771       for (sfbi = 16; sfbi < 21; ++sfbi)
00772         channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
00773     }
00774     else {
00775       for (sfbi = 16; sfbi < 21; ++sfbi)
00776         channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
00777     }
00778 
00779     channel->scalefac[21] = 0;
00780   }
00781 
00782   return mad_bit_length(&start, ptr);
00783 }
00784 
00785 /*
00786  * The Layer III formula for requantization and scaling is defined by
00787  * section 2.4.3.4.7.1 of ISO/IEC 11172-3, as follows:
00788  *
00789  *   long blocks:
00790  *   xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
00791  *           2^((1/4) * (global_gain - 210)) *
00792  *           2^-(scalefac_multiplier *
00793  *               (scalefac_l[sfb] + preflag * pretab[sfb]))
00794  *
00795  *   short blocks:
00796  *   xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
00797  *           2^((1/4) * (global_gain - 210 - 8 * subblock_gain[w])) *
00798  *           2^-(scalefac_multiplier * scalefac_s[sfb][w])
00799  *
00800  *   where:
00801  *   scalefac_multiplier = (scalefac_scale + 1) / 2
00802  *
00803  * The routines III_exponents() and III_requantize() facilitate this
00804  * calculation.
00805  */
00806 
00807 /*
00808  * NAME:        III_exponents()
00809  * DESCRIPTION: calculate scalefactor exponents
00810  */
00811 static
00812 void III_exponents(struct channel const *channel,
00813                    unsigned char const *sfbwidth, signed int exponents[39])
00814 {
00815   signed int gain;
00816   unsigned int scalefac_multiplier, sfbi;
00817 
00818   gain = (signed int) channel->global_gain - 210;
00819   scalefac_multiplier = (channel->flags & scalefac_scale) ? 2 : 1;
00820 
00821   if (channel->block_type == 2) {
00822     unsigned int l;
00823     signed int gain0, gain1, gain2;
00824 
00825     sfbi = l = 0;
00826 
00827     if (channel->flags & mixed_block_flag) {
00828       unsigned int premask;
00829 
00830       premask = (channel->flags & preflag) ? ~0 : 0;
00831 
00832       /* long block subbands 0-1 */
00833 
00834       while (l < 36) {
00835         exponents[sfbi] = gain -
00836           (signed int) ((channel->scalefac[sfbi] + (pretab[sfbi] & premask)) <<
00837                         scalefac_multiplier);
00838 
00839         l += sfbwidth[sfbi++];
00840       }
00841     }
00842 
00843     /* this is probably wrong for 8000 Hz short/mixed blocks */
00844 
00845     gain0 = gain - 8 * (signed int) channel->subblock_gain[0];
00846     gain1 = gain - 8 * (signed int) channel->subblock_gain[1];
00847     gain2 = gain - 8 * (signed int) channel->subblock_gain[2];
00848 
00849     while (l < 576) {
00850       exponents[sfbi + 0] = gain0 -
00851         (signed int) (channel->scalefac[sfbi + 0] << scalefac_multiplier);
00852       exponents[sfbi + 1] = gain1 -
00853         (signed int) (channel->scalefac[sfbi + 1] << scalefac_multiplier);
00854       exponents[sfbi + 2] = gain2 -
00855         (signed int) (channel->scalefac[sfbi + 2] << scalefac_multiplier);
00856 
00857       l    += 3 * sfbwidth[sfbi];
00858       sfbi += 3;
00859     }
00860   }
00861   else {  /* channel->block_type != 2 */
00862     if (channel->flags & preflag) {
00863       for (sfbi = 0; sfbi < 22; ++sfbi) {
00864         exponents[sfbi] = gain -
00865           (signed int) ((channel->scalefac[sfbi] + pretab[sfbi]) <<
00866                         scalefac_multiplier);
00867       }
00868     }
00869     else {
00870       for (sfbi = 0; sfbi < 22; ++sfbi) {
00871         exponents[sfbi] = gain -
00872           (signed int) (channel->scalefac[sfbi] << scalefac_multiplier);
00873       }
00874     }
00875   }
00876 }
00877 
00878 /*
00879  * NAME:        III_requantize()
00880  * DESCRIPTION: requantize one (positive) value
00881  */
00882 static
00883 mad_fixed_t III_requantize(unsigned int value, signed int exp)
00884 {
00885   mad_fixed_t requantized;
00886   signed int frac;
00887   struct fixedfloat const *power;
00888 
00889   frac = exp % 4;  /* assumes sign(frac) == sign(exp) */
00890   exp /= 4;
00891 
00892   power = &rq_table[value];
00893   requantized = power->mantissa;
00894   exp += power->exponent;
00895 
00896   if (exp < 0) {
00897     if (-exp >= sizeof(mad_fixed_t) * CHAR_BIT) {
00898       /* underflow */
00899       requantized = 0;
00900     }
00901     else {
00902       requantized += 1L << (-exp - 1);
00903       requantized >>= -exp;
00904     }
00905   }
00906   else {
00907     if (exp >= 5) {
00908       /* overflow */
00909 # if defined(DEBUG)
00910       fprintf(stderr, "requantize overflow (%f * 2^%d)\n",
00911               mad_f_todouble(requantized), exp);
00912 # endif
00913       requantized = MAD_F_MAX;
00914     }
00915     else
00916       requantized <<= exp;
00917   }
00918 
00919   return frac ? mad_f_mul(requantized, root_table[3 + frac]) : requantized;
00920 }
00921 
00922 /* we must take care that sz >= bits and sz < sizeof(long) lest bits == 0 */
00923 # define MASK(cache, sz, bits)  \
00924     (((cache) >> ((sz) - (bits))) & ((1 << (bits)) - 1))
00925 # define MASK1BIT(cache, sz)  \
00926     ((cache) & (1 << ((sz) - 1)))
00927 
00928 /*
00929  * NAME:        III_huffdecode()
00930  * DESCRIPTION: decode Huffman code words of one channel of one granule
00931  */
00932 static
00933 enum mad_error III_huffdecode(struct mad_bitptr *ptr, mad_fixed_t xr[576],
00934                               struct channel *channel,
00935                               unsigned char const *sfbwidth,
00936                               unsigned int part2_length)
00937 {
00938   signed int exponents[39], exp;
00939   signed int const *expptr;
00940   struct mad_bitptr peek;
00941   signed int bits_left, cachesz;
00942   register mad_fixed_t *xrptr;
00943   mad_fixed_t const *sfbound;
00944   register unsigned long bitcache;
00945 
00946   bits_left = (signed) channel->part2_3_length - (signed) part2_length;
00947   if (bits_left < 0)
00948     return MAD_ERROR_BADPART3LEN;
00949 
00950   III_exponents(channel, sfbwidth, exponents);
00951 
00952   peek = *ptr;
00953   mad_bit_skip(ptr, bits_left);
00954 
00955   /* align bit reads to byte boundaries */
00956   cachesz  = mad_bit_bitsleft(&peek);
00957   cachesz += ((32 - 1 - 24) + (24 - cachesz)) & ~7;
00958 
00959   bitcache   = mad_bit_read(&peek, cachesz);
00960   bits_left -= cachesz;
00961 
00962   xrptr = &xr[0];
00963 
00964   /* big_values */
00965   {
00966     unsigned int region, rcount;
00967     struct hufftable const *entry;
00968     union huffpair const *table;
00969     unsigned int linbits, startbits, big_values, reqhits;
00970     mad_fixed_t reqcache[16];
00971 
00972     sfbound = xrptr + *sfbwidth++;
00973     rcount  = channel->region0_count + 1;
00974 
00975     entry     = &mad_huff_pair_table[channel->table_select[region = 0]];
00976     table     = entry->table;
00977     linbits   = entry->linbits;
00978     startbits = entry->startbits;
00979 
00980     if (table == 0)
00981       return MAD_ERROR_BADHUFFTABLE;
00982 
00983     expptr  = &exponents[0];
00984     exp     = *expptr++;
00985     reqhits = 0;
00986 
00987     big_values = channel->big_values;
00988 
00989     while (big_values-- && cachesz + bits_left > 0) {
00990       union huffpair const *pair;
00991       unsigned int clumpsz, value;
00992       register mad_fixed_t requantized;
00993 
00994       if (xrptr == sfbound) {
00995         sfbound += *sfbwidth++;
00996 
00997         /* change table if region boundary */
00998 
00999         if (--rcount == 0) {
01000           if (region == 0)
01001             rcount = channel->region1_count + 1;
01002           else
01003             rcount = 0;  /* all remaining */
01004 
01005           entry     = &mad_huff_pair_table[channel->table_select[++region]];
01006           table     = entry->table;
01007           linbits   = entry->linbits;
01008           startbits = entry->startbits;
01009 
01010           if (table == 0)
01011             return MAD_ERROR_BADHUFFTABLE;
01012         }
01013 
01014         if (exp != *expptr) {
01015           exp = *expptr;
01016           reqhits = 0;
01017         }
01018 
01019         ++expptr;
01020       }
01021 
01022       if (cachesz < 21) {
01023         unsigned int bits;
01024 
01025         bits       = ((32 - 1 - 21) + (21 - cachesz)) & ~7;
01026         bitcache   = (bitcache << bits) | mad_bit_read(&peek, bits);
01027         cachesz   += bits;
01028         bits_left -= bits;
01029       }
01030 
01031       /* hcod (0..19) */
01032 
01033       clumpsz = startbits;
01034       pair    = &table[MASK(bitcache, cachesz, clumpsz)];
01035 
01036       while (!pair->final) {
01037         cachesz -= clumpsz;
01038 
01039         clumpsz = pair->ptr.bits;
01040         pair    = &table[pair->ptr.offset + MASK(bitcache, cachesz, clumpsz)];
01041       }
01042 
01043       cachesz -= pair->value.hlen;
01044 
01045       if (linbits) {
01046         /* x (0..14) */
01047 
01048         value = pair->value.x;
01049 
01050         switch (value) {
01051         case 0:
01052           xrptr[0] = 0;
01053           break;
01054 
01055         case 15:
01056           if (cachesz < linbits + 2) {
01057             bitcache   = (bitcache << 16) | mad_bit_read(&peek, 16);
01058             cachesz   += 16;
01059             bits_left -= 16;
01060           }
01061 
01062           value += MASK(bitcache, cachesz, linbits);
01063           cachesz -= linbits;
01064 
01065           requantized = III_requantize(value, exp);
01066           goto x_final;
01067 
01068         default:
01069           if (reqhits & (1 << value))
01070             requantized = reqcache[value];
01071           else {
01072             reqhits |= (1 << value);
01073             requantized = reqcache[value] = III_requantize(value, exp);
01074           }
01075 
01076         x_final:
01077           xrptr[0] = MASK1BIT(bitcache, cachesz--) ?
01078             -requantized : requantized;
01079         }
01080 
01081         /* y (0..14) */
01082 
01083         value = pair->value.y;
01084 
01085         switch (value) {
01086         case 0:
01087           xrptr[1] = 0;
01088           break;
01089 
01090         case 15:
01091           if (cachesz < linbits + 1) {
01092             bitcache   = (bitcache << 16) | mad_bit_read(&peek, 16);
01093             cachesz   += 16;
01094             bits_left -= 16;
01095           }
01096 
01097           value += MASK(bitcache, cachesz, linbits);
01098           cachesz -= linbits;
01099 
01100           requantized = III_requantize(value, exp);
01101           goto y_final;
01102 
01103         default:
01104           if (reqhits & (1 << value))
01105             requantized = reqcache[value];
01106           else {
01107             reqhits |= (1 << value);
01108             requantized = reqcache[value] = III_requantize(value, exp);
01109           }
01110 
01111         y_final:
01112           xrptr[1] = MASK1BIT(bitcache, cachesz--) ?
01113             -requantized : requantized;
01114         }
01115       }
01116       else {
01117         /* x (0..1) */
01118 
01119         value = pair->value.x;
01120 
01121         if (value == 0)
01122           xrptr[0] = 0;
01123         else {
01124           if (reqhits & (1 << value))
01125             requantized = reqcache[value];
01126           else {
01127             reqhits |= (1 << value);
01128             requantized = reqcache[value] = III_requantize(value, exp);
01129           }
01130 
01131           xrptr[0] = MASK1BIT(bitcache, cachesz--) ?
01132             -requantized : requantized;
01133         }
01134 
01135         /* y (0..1) */
01136 
01137         value = pair->value.y;
01138 
01139         if (value == 0)
01140           xrptr[1] = 0;
01141         else {
01142           if (reqhits & (1 << value))
01143             requantized = reqcache[value];
01144           else {
01145             reqhits |= (1 << value);
01146             requantized = reqcache[value] = III_requantize(value, exp);
01147           }
01148 
01149           xrptr[1] = MASK1BIT(bitcache, cachesz--) ?
01150             -requantized : requantized;
01151         }
01152       }
01153 
01154       xrptr += 2;
01155     }
01156   }
01157 
01158   if (cachesz + bits_left < 0)
01159     return MAD_ERROR_BADHUFFDATA;  /* big_values overrun */
01160 
01161   /* count1 */
01162   {
01163     union huffquad const *table;
01164     register mad_fixed_t requantized;
01165 
01166     table = mad_huff_quad_table[channel->flags & count1table_select];
01167 
01168     requantized = III_requantize(1, exp);
01169 
01170     while (cachesz + bits_left > 0 && xrptr <= &xr[572]) {
01171       union huffquad const *quad;
01172 
01173       /* hcod (1..6) */
01174 
01175       if (cachesz < 10) {
01176         bitcache   = (bitcache << 16) | mad_bit_read(&peek, 16);
01177         cachesz   += 16;
01178         bits_left -= 16;
01179       }
01180 
01181       quad = &table[MASK(bitcache, cachesz, 4)];
01182 
01183       /* quad tables guaranteed to have at most one extra lookup */
01184       if (!quad->final) {
01185         cachesz -= 4;
01186 
01187         quad = &table[quad->ptr.offset +
01188                       MASK(bitcache, cachesz, quad->ptr.bits)];
01189       }
01190 
01191       cachesz -= quad->value.hlen;
01192 
01193       if (xrptr == sfbound) {
01194         sfbound += *sfbwidth++;
01195 
01196         if (exp != *expptr) {
01197           exp = *expptr;
01198           requantized = III_requantize(1, exp);
01199         }
01200 
01201         ++expptr;
01202       }
01203 
01204       /* v (0..1) */
01205 
01206       xrptr[0] = quad->value.v ?
01207         (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
01208 
01209       /* w (0..1) */
01210 
01211       xrptr[1] = quad->value.w ?
01212         (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
01213 
01214       xrptr += 2;
01215 
01216       if (xrptr == sfbound) {
01217         sfbound += *sfbwidth++;
01218 
01219         if (exp != *expptr) {
01220           exp = *expptr;
01221           requantized = III_requantize(1, exp);
01222         }
01223 
01224         ++expptr;
01225       }
01226 
01227       /* x (0..1) */
01228 
01229       xrptr[0] = quad->value.x ?
01230         (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
01231 
01232       /* y (0..1) */
01233 
01234       xrptr[1] = quad->value.y ?
01235         (MASK1BIT(bitcache, cachesz--) ? -requantized : requantized) : 0;
01236 
01237       xrptr += 2;
01238     }
01239 
01240     if (cachesz + bits_left < 0) {
01241 # if 0 && defined(DEBUG)
01242       fprintf(stderr, "huffman count1 overrun (%d bits)\n",
01243               -(cachesz + bits_left));
01244 # endif
01245 
01246       /* technically the bitstream is misformatted, but apparently
01247          some encoders are just a bit sloppy with stuffing bits */
01248 
01249       xrptr -= 4;
01250     }
01251   }
01252 
01253   assert(-bits_left <= MAD_BUFFER_GUARD * CHAR_BIT);
01254 
01255 # if 0 && defined(DEBUG)
01256   if (bits_left < 0)
01257     fprintf(stderr, "read %d bits too many\n", -bits_left);
01258   else if (cachesz + bits_left > 0)
01259     fprintf(stderr, "%d stuffing bits\n", cachesz + bits_left);
01260 # endif
01261 
01262   /* rzero */
01263   while (xrptr < &xr[576]) {
01264     xrptr[0] = 0;
01265     xrptr[1] = 0;
01266 
01267     xrptr += 2;
01268   }
01269 
01270   return MAD_ERROR_NONE;
01271 }
01272 
01273 # undef MASK
01274 # undef MASK1BIT
01275 
01276 /*
01277  * NAME:        III_reorder()
01278  * DESCRIPTION: reorder frequency lines of a short block into subband order
01279  */
01280 static
01281 void III_reorder(mad_fixed_t xr[576], struct channel const *channel,
01282                  unsigned char const sfbwidth[39])
01283 {
01284   mad_fixed_t tmp[32][3][6];
01285   unsigned int sb, l, f, w, sbw[3], sw[3];
01286 
01287   /* this is probably wrong for 8000 Hz mixed blocks */
01288 
01289   sb = 0;
01290   if (channel->flags & mixed_block_flag) {
01291     sb = 2;
01292 
01293     l = 0;
01294     while (l < 36)
01295       l += *sfbwidth++;
01296   }
01297 
01298   for (w = 0; w < 3; ++w) {
01299     sbw[w] = sb;
01300     sw[w]  = 0;
01301   }
01302 
01303   f = *sfbwidth++;
01304   w = 0;
01305 
01306   for (l = 18 * sb; l < 576; ++l) {
01307     if (f-- == 0) {
01308       f = *sfbwidth++ - 1;
01309       w = (w + 1) % 3;
01310     }
01311 
01312     tmp[sbw[w]][w][sw[w]++] = xr[l];
01313 
01314     if (sw[w] == 6) {
01315       sw[w] = 0;
01316       ++sbw[w];
01317     }
01318   }
01319 
01320   memcpy(&xr[18 * sb], &tmp[sb], (576 - 18 * sb) * sizeof(mad_fixed_t));
01321 }
01322 
01323 /*
01324  * NAME:        III_stereo()
01325  * DESCRIPTION: perform joint stereo processing on a granule
01326  */
01327 static
01328 enum mad_error III_stereo(mad_fixed_t xr[2][576],
01329                           struct granule const *granule,
01330                           struct mad_header *header,
01331                           unsigned char const *sfbwidth)
01332 {
01333   short modes[39];
01334   unsigned int sfbi, l, n, i;
01335 
01336   if (granule->ch[0].block_type !=
01337       granule->ch[1].block_type ||
01338       (granule->ch[0].flags & mixed_block_flag) !=
01339       (granule->ch[1].flags & mixed_block_flag))
01340     return MAD_ERROR_BADSTEREO;
01341 
01342   for (i = 0; i < 39; ++i)
01343     modes[i] = header->mode_extension;
01344 
01345   /* intensity stereo */
01346 
01347   if (header->mode_extension & I_STEREO) {
01348     struct channel const *right_ch = &granule->ch[1];
01349     mad_fixed_t const *right_xr = xr[1];
01350     unsigned int is_pos;
01351 
01352     header->flags |= MAD_FLAG_I_STEREO;
01353 
01354     /* first determine which scalefactor bands are to be processed */
01355 
01356     if (right_ch->block_type == 2) {
01357       unsigned int lower, start, max, bound[3], w;
01358 
01359       lower = start = max = bound[0] = bound[1] = bound[2] = 0;
01360 
01361       sfbi = l = 0;
01362 
01363       if (right_ch->flags & mixed_block_flag) {
01364         while (l < 36) {
01365           n = sfbwidth[sfbi++];
01366 
01367           for (i = 0; i < n; ++i) {
01368             if (right_xr[i]) {
01369               lower = sfbi;
01370               break;
01371             }
01372           }
01373 
01374           right_xr += n;
01375           l += n;
01376         }
01377 
01378         start = sfbi;
01379       }
01380 
01381       w = 0;
01382       while (l < 576) {
01383         n = sfbwidth[sfbi++];
01384 
01385         for (i = 0; i < n; ++i) {
01386           if (right_xr[i]) {
01387             max = bound[w] = sfbi;
01388             break;
01389           }
01390         }
01391 
01392         right_xr += n;
01393         l += n;
01394         w = (w + 1) % 3;
01395       }
01396 
01397       if (max)
01398         lower = start;
01399 
01400       /* long blocks */
01401 
01402       for (i = 0; i < lower; ++i)
01403         modes[i] = header->mode_extension & ~I_STEREO;
01404 
01405       /* short blocks */
01406 
01407       w = 0;
01408       for (i = start; i < max; ++i) {
01409         if (i < bound[w])
01410           modes[i] = header->mode_extension & ~I_STEREO;
01411 
01412         w = (w + 1) % 3;
01413       }
01414     }
01415     else {  /* right_ch->block_type != 2 */
01416       unsigned int bound;
01417 
01418       bound = 0;
01419       for (sfbi = l = 0; l < 576; l += n) {
01420         n = sfbwidth[sfbi++];
01421 
01422         for (i = 0; i < n; ++i) {
01423           if (right_xr[i]) {
01424             bound = sfbi;
01425             break;
01426           }
01427         }
01428 
01429         right_xr += n;
01430       }
01431 
01432       for (i = 0; i < bound; ++i)
01433         modes[i] = header->mode_extension & ~I_STEREO;
01434     }
01435 
01436     /* now do the actual processing */
01437 
01438     if (header->flags & MAD_FLAG_LSF_EXT) {
01439       unsigned char const *illegal_pos = granule[1].ch[1].scalefac;
01440       mad_fixed_t const *lsf_scale;
01441 
01442       /* intensity_scale */
01443       lsf_scale = is_lsf_table[right_ch->scalefac_compress & 0x1];
01444 
01445       for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
01446         n = sfbwidth[sfbi];
01447 
01448         if (!(modes[sfbi] & I_STEREO))
01449           continue;
01450 
01451         if (illegal_pos[sfbi]) {
01452           modes[sfbi] &= ~I_STEREO;
01453           continue;
01454         }
01455 
01456         is_pos = right_ch->scalefac[sfbi];
01457 
01458         for (i = 0; i < n; ++i) {
01459           register mad_fixed_t left;
01460 
01461           left = xr[0][l + i];
01462 
01463           if (is_pos == 0)
01464             xr[1][l + i] = left;
01465           else {
01466             register mad_fixed_t opposite;
01467 
01468             opposite = mad_f_mul(left, lsf_scale[(is_pos - 1) / 2]);
01469 
01470             if (is_pos & 1) {
01471               xr[0][l + i] = opposite;
01472               xr[1][l + i] = left;
01473             }
01474             else
01475               xr[1][l + i] = opposite;
01476           }
01477         }
01478       }
01479     }
01480     else {  /* !(header->flags & MAD_FLAG_LSF_EXT) */
01481       for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
01482         n = sfbwidth[sfbi];
01483 
01484         if (!(modes[sfbi] & I_STEREO))
01485           continue;
01486 
01487         is_pos = right_ch->scalefac[sfbi];
01488 
01489         if (is_pos >= 7) {  /* illegal intensity position */
01490           modes[sfbi] &= ~I_STEREO;
01491           continue;
01492         }
01493 
01494         for (i = 0; i < n; ++i) {
01495           register mad_fixed_t left;
01496 
01497           left = xr[0][l + i];
01498 
01499           xr[0][l + i] = mad_f_mul(left, is_table[    is_pos]);
01500           xr[1][l + i] = mad_f_mul(left, is_table[6 - is_pos]);
01501         }
01502       }
01503     }
01504   }
01505 
01506   /* middle/side stereo */
01507 
01508   if (header->mode_extension & MS_STEREO) {
01509     register mad_fixed_t invsqrt2;
01510 
01511     header->flags |= MAD_FLAG_MS_STEREO;
01512 
01513     invsqrt2 = root_table[3 + -2];
01514 
01515     for (sfbi = l = 0; l < 576; ++sfbi, l += n) {
01516       n = sfbwidth[sfbi];
01517 
01518       if (modes[sfbi] != MS_STEREO)
01519         continue;
01520 
01521       for (i = 0; i < n; ++i) {
01522         register mad_fixed_t m, s;
01523 
01524         m = xr[0][l + i];
01525         s = xr[1][l + i];
01526 
01527         xr[0][l + i] = mad_f_mul(m + s, invsqrt2);  /* l = (m + s) / sqrt(2) */
01528         xr[1][l + i] = mad_f_mul(m - s, invsqrt2);  /* r = (m - s) / sqrt(2) */
01529       }
01530     }
01531   }
01532 
01533   return MAD_ERROR_NONE;
01534 }
01535 
01536 /*
01537  * NAME:        III_aliasreduce()
01538  * DESCRIPTION: perform frequency line alias reduction
01539  */
01540 static
01541 void III_aliasreduce(mad_fixed_t xr[576], int lines)
01542 {
01543   mad_fixed_t const *bound;
01544   int i;
01545 
01546   bound = &xr[lines];
01547   for (xr += 18; xr < bound; xr += 18) {
01548     for (i = 0; i < 8; ++i) {
01549       register mad_fixed_t a, b;
01550       register mad_fixed64hi_t hi;
01551       register mad_fixed64lo_t lo;
01552 
01553       a = xr[-1 - i];
01554       b = xr[     i];
01555 
01556 # if defined(ASO_ZEROCHECK)
01557       if (a | b) {
01558 # endif
01559         MAD_F_ML0(hi, lo,  a, cs[i]);
01560         MAD_F_MLA(hi, lo, -b, ca[i]);
01561 
01562         xr[-1 - i] = MAD_F_MLZ(hi, lo);
01563 
01564         MAD_F_ML0(hi, lo,  b, cs[i]);
01565         MAD_F_MLA(hi, lo,  a, ca[i]);
01566 
01567         xr[     i] = MAD_F_MLZ(hi, lo);
01568 # if defined(ASO_ZEROCHECK)
01569       }
01570 # endif
01571     }
01572   }
01573 }
01574 
01575 # if defined(ASO_IMDCT)
01576 void III_imdct_l(mad_fixed_t const [18], mad_fixed_t [36], unsigned int);
01577 # else
01578 /*
01579  * NAME:        imdct36
01580  * DESCRIPTION: perform X[18]->x[36] IMDCT
01581  */
01582 static inline
01583 void imdct36(mad_fixed_t const X[18], mad_fixed_t x[36])
01584 {
01585   mad_fixed_t t0, t1, t2,  t3,  t4,  t5,  t6,  t7;
01586   mad_fixed_t t8, t9, t10, t11, t12, t13, t14, t15;
01587   register mad_fixed64hi_t hi;
01588   register mad_fixed64lo_t lo;
01589 
01590   MAD_F_ML0(hi, lo, X[4],  MAD_F(0x0ec835e8));
01591   MAD_F_MLA(hi, lo, X[13], MAD_F(0x061f78aa));
01592 
01593   t6 = MAD_F_MLZ(hi, lo);
01594 
01595   MAD_F_MLA(hi, lo, (t14 = X[1] - X[10]), -MAD_F(0x061f78aa));
01596   MAD_F_MLA(hi, lo, (t15 = X[7] + X[16]), -MAD_F(0x0ec835e8));
01597 
01598   t0 = MAD_F_MLZ(hi, lo);
01599 
01600   MAD_F_MLA(hi, lo, (t8  = X[0] - X[11] - X[12]),  MAD_F(0x0216a2a2));
01601   MAD_F_MLA(hi, lo, (t9  = X[2] - X[9]  - X[14]),  MAD_F(0x09bd7ca0));
01602   MAD_F_MLA(hi, lo, (t10 = X[3] - X[8]  - X[15]), -MAD_F(0x0cb19346));
01603   MAD_F_MLA(hi, lo, (t11 = X[5] - X[6]  - X[17]), -MAD_F(0x0fdcf549));
01604 
01605   x[7]  = MAD_F_MLZ(hi, lo);
01606   x[10] = -x[7];
01607 
01608   MAD_F_ML0(hi, lo, t8,  -MAD_F(0x0cb19346));
01609   MAD_F_MLA(hi, lo, t9,   MAD_F(0x0fdcf549));
01610   MAD_F_MLA(hi, lo, t10,  MAD_F(0x0216a2a2));
01611   MAD_F_MLA(hi, lo, t11, -MAD_F(0x09bd7ca0));
01612 
01613   x[19] = x[34] = MAD_F_MLZ(hi, lo) - t0;
01614 
01615   t12 = X[0] - X[3] + X[8] - X[11] - X[12] + X[15];
01616   t13 = X[2] + X[5] - X[6] - X[9]  - X[14] - X[17];
01617 
01618   MAD_F_ML0(hi, lo, t12, -MAD_F(0x0ec835e8));
01619   MAD_F_MLA(hi, lo, t13,  MAD_F(0x061f78aa));
01620 
01621   x[22] = x[31] = MAD_F_MLZ(hi, lo) + t0;
01622 
01623   MAD_F_ML0(hi, lo, X[1],  -MAD_F(0x09bd7ca0));
01624   MAD_F_MLA(hi, lo, X[7],   MAD_F(0x0216a2a2));
01625   MAD_F_MLA(hi, lo, X[10], -MAD_F(0x0fdcf549));
01626   MAD_F_MLA(hi, lo, X[16],  MAD_F(0x0cb19346));
01627 
01628   t1 = MAD_F_MLZ(hi, lo) + t6;
01629 
01630   MAD_F_ML0(hi, lo, X[0],   MAD_F(0x03768962));
01631   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0e313245));
01632   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x0ffc19fd));
01633   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0acf37ad));
01634   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x04cfb0e2));
01635   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x0898c779));
01636   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x0d7e8807));
01637   MAD_F_MLA(hi, lo, X[11],  MAD_F(0x0f426cb5));
01638   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0bcbe352));
01639   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x00b2aa3e));
01640   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x07635284));
01641   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0f9ee890));
01642 
01643   x[6]  = MAD_F_MLZ(hi, lo) + t1;
01644   x[11] = -x[6];
01645 
01646   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0f426cb5));
01647   MAD_F_MLA(hi, lo, X[2],  -MAD_F(0x00b2aa3e));
01648   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0898c779));
01649   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x0f9ee890));
01650   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0acf37ad));
01651   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x07635284));
01652   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x0e313245));
01653   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0bcbe352));
01654   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x03768962));
01655   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0d7e8807));
01656   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0ffc19fd));
01657   MAD_F_MLA(hi, lo, X[17],  MAD_F(0x04cfb0e2));
01658 
01659   x[23] = x[30] = MAD_F_MLZ(hi, lo) + t1;
01660 
01661   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0bcbe352));
01662   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0d7e8807));
01663   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x07635284));
01664   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x04cfb0e2));
01665   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0f9ee890));
01666   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x0ffc19fd));
01667   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x00b2aa3e));
01668   MAD_F_MLA(hi, lo, X[11],  MAD_F(0x03768962));
01669   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0f426cb5));
01670   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0e313245));
01671   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0898c779));
01672   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0acf37ad));
01673 
01674   x[18] = x[35] = MAD_F_MLZ(hi, lo) - t1;
01675 
01676   MAD_F_ML0(hi, lo, X[4],   MAD_F(0x061f78aa));
01677   MAD_F_MLA(hi, lo, X[13], -MAD_F(0x0ec835e8));
01678 
01679   t7 = MAD_F_MLZ(hi, lo);
01680 
01681   MAD_F_MLA(hi, lo, X[1],  -MAD_F(0x0cb19346));
01682   MAD_F_MLA(hi, lo, X[7],   MAD_F(0x0fdcf549));
01683   MAD_F_MLA(hi, lo, X[10],  MAD_F(0x0216a2a2));
01684   MAD_F_MLA(hi, lo, X[16], -MAD_F(0x09bd7ca0));
01685 
01686   t2 = MAD_F_MLZ(hi, lo);
01687 
01688   MAD_F_MLA(hi, lo, X[0],   MAD_F(0x04cfb0e2));
01689   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0ffc19fd));
01690   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x0d7e8807));
01691   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x03768962));
01692   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x0bcbe352));
01693   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x0e313245));
01694   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x07635284));
01695   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0acf37ad));
01696   MAD_F_MLA(hi, lo, X[12],  MAD_F(0x0f9ee890));
01697   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0898c779));
01698   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x00b2aa3e));
01699   MAD_F_MLA(hi, lo, X[17],  MAD_F(0x0f426cb5));
01700 
01701   x[5]  = MAD_F_MLZ(hi, lo);
01702   x[12] = -x[5];
01703 
01704   MAD_F_ML0(hi, lo, X[0],   MAD_F(0x0acf37ad));
01705   MAD_F_MLA(hi, lo, X[2],  -MAD_F(0x0898c779));
01706   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0e313245));
01707   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0f426cb5));
01708   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x03768962));
01709   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x00b2aa3e));
01710   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x0ffc19fd));
01711   MAD_F_MLA(hi, lo, X[11],  MAD_F(0x0f9ee890));
01712   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x04cfb0e2));
01713   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x07635284));
01714   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0d7e8807));
01715   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0bcbe352));
01716 
01717   x[0]  = MAD_F_MLZ(hi, lo) + t2;
01718   x[17] = -x[0];
01719 
01720   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0f9ee890));
01721   MAD_F_MLA(hi, lo, X[2],  -MAD_F(0x07635284));
01722   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x00b2aa3e));
01723   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x0bcbe352));
01724   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0f426cb5));
01725   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x0d7e8807));
01726   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x0898c779));
01727   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x04cfb0e2));
01728   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0acf37ad));
01729   MAD_F_MLA(hi, lo, X[14], -MAD_F(0x0ffc19fd));
01730   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0e313245));
01731   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x03768962));
01732 
01733   x[24] = x[29] = MAD_F_MLZ(hi, lo) + t2;
01734 
01735   MAD_F_ML0(hi, lo, X[1],  -MAD_F(0x0216a2a2));
01736   MAD_F_MLA(hi, lo, X[7],  -MAD_F(0x09bd7ca0));
01737   MAD_F_MLA(hi, lo, X[10],  MAD_F(0x0cb19346));
01738   MAD_F_MLA(hi, lo, X[16],  MAD_F(0x0fdcf549));
01739 
01740   t3 = MAD_F_MLZ(hi, lo) + t7;
01741 
01742   MAD_F_ML0(hi, lo, X[0],   MAD_F(0x00b2aa3e));
01743   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x03768962));
01744   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x04cfb0e2));
01745   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x07635284));
01746   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0898c779));
01747   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x0acf37ad));
01748   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x0bcbe352));
01749   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0d7e8807));
01750   MAD_F_MLA(hi, lo, X[12],  MAD_F(0x0e313245));
01751   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0f426cb5));
01752   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0f9ee890));
01753   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0ffc19fd));
01754 
01755   x[8] = MAD_F_MLZ(hi, lo) + t3;
01756   x[9] = -x[8];
01757 
01758   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0e313245));
01759   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0bcbe352));
01760   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0f9ee890));
01761   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0898c779));
01762   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x0ffc19fd));
01763   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x04cfb0e2));
01764   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x0f426cb5));
01765   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x00b2aa3e));
01766   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0d7e8807));
01767   MAD_F_MLA(hi, lo, X[14], -MAD_F(0x03768962));
01768   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0acf37ad));
01769   MAD_F_MLA(hi, lo, X[17],  MAD_F(0x07635284));
01770 
01771   x[21] = x[32] = MAD_F_MLZ(hi, lo) + t3;
01772 
01773   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0d7e8807));
01774   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0f426cb5));
01775   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0acf37ad));
01776   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0ffc19fd));
01777   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x07635284));
01778   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x0f9ee890));
01779   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x03768962));
01780   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0e313245));
01781   MAD_F_MLA(hi, lo, X[12],  MAD_F(0x00b2aa3e));
01782   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0bcbe352));
01783   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x04cfb0e2));
01784   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0898c779));
01785 
01786   x[20] = x[33] = MAD_F_MLZ(hi, lo) - t3;
01787 
01788   MAD_F_ML0(hi, lo, t14, -MAD_F(0x0ec835e8));
01789   MAD_F_MLA(hi, lo, t15,  MAD_F(0x061f78aa));
01790 
01791   t4 = MAD_F_MLZ(hi, lo) - t7;
01792 
01793   MAD_F_ML0(hi, lo, t12, MAD_F(0x061f78aa));
01794   MAD_F_MLA(hi, lo, t13, MAD_F(0x0ec835e8));
01795 
01796   x[4]  = MAD_F_MLZ(hi, lo) + t4;
01797   x[13] = -x[4];
01798 
01799   MAD_F_ML0(hi, lo, t8,   MAD_F(0x09bd7ca0));
01800   MAD_F_MLA(hi, lo, t9,  -MAD_F(0x0216a2a2));
01801   MAD_F_MLA(hi, lo, t10,  MAD_F(0x0fdcf549));
01802   MAD_F_MLA(hi, lo, t11, -MAD_F(0x0cb19346));
01803 
01804   x[1]  = MAD_F_MLZ(hi, lo) + t4;
01805   x[16] = -x[1];
01806 
01807   MAD_F_ML0(hi, lo, t8,  -MAD_F(0x0fdcf549));
01808   MAD_F_MLA(hi, lo, t9,  -MAD_F(0x0cb19346));
01809   MAD_F_MLA(hi, lo, t10, -MAD_F(0x09bd7ca0));
01810   MAD_F_MLA(hi, lo, t11, -MAD_F(0x0216a2a2));
01811 
01812   x[25] = x[28] = MAD_F_MLZ(hi, lo) + t4;
01813 
01814   MAD_F_ML0(hi, lo, X[1],  -MAD_F(0x0fdcf549));
01815   MAD_F_MLA(hi, lo, X[7],  -MAD_F(0x0cb19346));
01816   MAD_F_MLA(hi, lo, X[10], -MAD_F(0x09bd7ca0));
01817   MAD_F_MLA(hi, lo, X[16], -MAD_F(0x0216a2a2));
01818 
01819   t5 = MAD_F_MLZ(hi, lo) - t6;
01820 
01821   MAD_F_ML0(hi, lo, X[0],   MAD_F(0x0898c779));
01822   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x04cfb0e2));
01823   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x0bcbe352));
01824   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x00b2aa3e));
01825   MAD_F_MLA(hi, lo, X[6],   MAD_F(0x0e313245));
01826   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x03768962));
01827   MAD_F_MLA(hi, lo, X[9],   MAD_F(0x0f9ee890));
01828   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x07635284));
01829   MAD_F_MLA(hi, lo, X[12],  MAD_F(0x0ffc19fd));
01830   MAD_F_MLA(hi, lo, X[14], -MAD_F(0x0acf37ad));
01831   MAD_F_MLA(hi, lo, X[15],  MAD_F(0x0f426cb5));
01832   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x0d7e8807));
01833 
01834   x[2]  = MAD_F_MLZ(hi, lo) + t5;
01835   x[15] = -x[2];
01836 
01837   MAD_F_ML0(hi, lo, X[0],   MAD_F(0x07635284));
01838   MAD_F_MLA(hi, lo, X[2],   MAD_F(0x0acf37ad));
01839   MAD_F_MLA(hi, lo, X[3],   MAD_F(0x03768962));
01840   MAD_F_MLA(hi, lo, X[5],   MAD_F(0x0d7e8807));
01841   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x00b2aa3e));
01842   MAD_F_MLA(hi, lo, X[8],   MAD_F(0x0f426cb5));
01843   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x04cfb0e2));
01844   MAD_F_MLA(hi, lo, X[11],  MAD_F(0x0ffc19fd));
01845   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x0898c779));
01846   MAD_F_MLA(hi, lo, X[14],  MAD_F(0x0f9ee890));
01847   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x0bcbe352));
01848   MAD_F_MLA(hi, lo, X[17],  MAD_F(0x0e313245));
01849 
01850   x[3]  = MAD_F_MLZ(hi, lo) + t5;
01851   x[14] = -x[3];
01852 
01853   MAD_F_ML0(hi, lo, X[0],  -MAD_F(0x0ffc19fd));
01854   MAD_F_MLA(hi, lo, X[2],  -MAD_F(0x0f9ee890));
01855   MAD_F_MLA(hi, lo, X[3],  -MAD_F(0x0f426cb5));
01856   MAD_F_MLA(hi, lo, X[5],  -MAD_F(0x0e313245));
01857   MAD_F_MLA(hi, lo, X[6],  -MAD_F(0x0d7e8807));
01858   MAD_F_MLA(hi, lo, X[8],  -MAD_F(0x0bcbe352));
01859   MAD_F_MLA(hi, lo, X[9],  -MAD_F(0x0acf37ad));
01860   MAD_F_MLA(hi, lo, X[11], -MAD_F(0x0898c779));
01861   MAD_F_MLA(hi, lo, X[12], -MAD_F(0x07635284));
01862   MAD_F_MLA(hi, lo, X[14], -MAD_F(0x04cfb0e2));
01863   MAD_F_MLA(hi, lo, X[15], -MAD_F(0x03768962));
01864   MAD_F_MLA(hi, lo, X[17], -MAD_F(0x00b2aa3e));
01865 
01866   x[26] = x[27] = MAD_F_MLZ(hi, lo) + t5;
01867 }
01868 
01869 /*
01870  * NAME:        III_imdct_l()
01871  * DESCRIPTION: perform IMDCT and windowing for long blocks
01872  */
01873 static
01874 void III_imdct_l(mad_fixed_t const X[18], mad_fixed_t z[36],
01875                  unsigned int block_type)
01876 {
01877   unsigned int i;
01878 
01879   /* IMDCT */
01880 
01881   imdct36(X, z);
01882 
01883   /* windowing */
01884 
01885   switch (block_type) {
01886   case 0:  /* normal window */
01887 # if defined(ASO_INTERLEAVE1)
01888     {
01889       register mad_fixed_t tmp1, tmp2;
01890 
01891       tmp1 = window_l[0];
01892       tmp2 = window_l[1];
01893 
01894       for (i = 0; i < 34; i += 2) {
01895         z[i + 0] = mad_f_mul(z[i + 0], tmp1);
01896         tmp1 = window_l[i + 2];
01897         z[i + 1] = mad_f_mul(z[i + 1], tmp2);
01898         tmp2 = window_l[i + 3];
01899       }
01900 
01901       z[34] = mad_f_mul(z[34], tmp1);
01902       z[35] = mad_f_mul(z[35], tmp2);
01903     }
01904 # elif defined(ASO_INTERLEAVE2)
01905     {
01906       register mad_fixed_t tmp1, tmp2;
01907 
01908       tmp1 = z[0];
01909       tmp2 = window_l[0];
01910 
01911       for (i = 0; i < 35; ++i) {
01912         z[i] = mad_f_mul(tmp1, tmp2);
01913         tmp1 = z[i + 1];
01914         tmp2 = window_l[i + 1];
01915       }
01916 
01917       z[35] = mad_f_mul(tmp1, tmp2);
01918     }
01919 # elif 1
01920     for (i = 0; i < 36; i += 4) {
01921       z[i + 0] = mad_f_mul(z[i + 0], window_l[i + 0]);
01922       z[i + 1] = mad_f_mul(z[i + 1], window_l[i + 1]);
01923       z[i + 2] = mad_f_mul(z[i + 2], window_l[i + 2]);
01924       z[i + 3] = mad_f_mul(z[i + 3], window_l[i + 3]);
01925     }
01926 # else
01927     for (i =  0; i < 36; ++i) z[i] = mad_f_mul(z[i], window_l[i]);
01928 # endif
01929     break;
01930 
01931   case 1:  /* start block */
01932     for (i =  0; i < 18; ++i) z[i] = mad_f_mul(z[i], window_l[i]);
01933     /*  (i = 18; i < 24; ++i) z[i] unchanged */
01934     for (i = 24; i < 30; ++i) z[i] = mad_f_mul(z[i], window_s[i - 18]);
01935     for (i = 30; i < 36; ++i) z[i] = 0;
01936     break;
01937 
01938   case 3:  /* stop block */
01939     for (i =  0; i <  6; ++i) z[i] = 0;
01940     for (i =  6; i < 12; ++i) z[i] = mad_f_mul(z[i], window_s[i - 6]);
01941     /*  (i = 12; i < 18; ++i) z[i] unchanged */
01942     for (i = 18; i < 36; ++i) z[i] = mad_f_mul(z[i], window_l[i]);
01943     break;
01944   }
01945 }
01946 # endif  /* ASO_IMDCT */
01947 
01948 /*
01949  * NAME:        III_imdct_s()
01950  * DESCRIPTION: perform IMDCT and windowing for short blocks
01951  */
01952 static
01953 void III_imdct_s(mad_fixed_t const X[18], mad_fixed_t z[36])
01954 {
01955   mad_fixed_t y[36], *yptr;
01956   mad_fixed_t const *wptr;
01957   int w, i;
01958   register mad_fixed64hi_t hi;
01959   register mad_fixed64lo_t lo;
01960 
01961   /* IMDCT */
01962 
01963   yptr = &y[0];
01964 
01965   for (w = 0; w < 3; ++w) {
01966     register mad_fixed_t const (*s)[6];
01967 
01968     s = imdct_s;
01969 
01970     for (i = 0; i < 3; ++i) {
01971       MAD_F_ML0(hi, lo, X[0], (*s)[0]);
01972       MAD_F_MLA(hi, lo, X[1], (*s)[1]);
01973       MAD_F_MLA(hi, lo, X[2], (*s)[2]);
01974       MAD_F_MLA(hi, lo, X[3], (*s)[3]);
01975       MAD_F_MLA(hi, lo, X[4], (*s)[4]);
01976       MAD_F_MLA(hi, lo, X[5], (*s)[5]);
01977 
01978       yptr[i + 0] = MAD_F_MLZ(hi, lo);
01979       yptr[5 - i] = -yptr[i + 0];
01980 
01981       ++s;
01982 
01983       MAD_F_ML0(hi, lo, X[0], (*s)[0]);
01984       MAD_F_MLA(hi, lo, X[1], (*s)[1]);
01985       MAD_F_MLA(hi, lo, X[2], (*s)[2]);
01986       MAD_F_MLA(hi, lo, X[3], (*s)[3]);
01987       MAD_F_MLA(hi, lo, X[4], (*s)[4]);
01988       MAD_F_MLA(hi, lo, X[5], (*s)[5]);
01989 
01990       yptr[ i + 6] = MAD_F_MLZ(hi, lo);
01991       yptr[11 - i] = yptr[i + 6];
01992 
01993       ++s;
01994     }
01995 
01996     yptr += 12;
01997     X    += 6;
01998   }
01999 
02000   /* windowing, overlapping and concatenation */
02001 
02002   yptr = &y[0];
02003   wptr = &window_s[0];
02004 
02005   for (i = 0; i < 6; ++i) {
02006     z[i +  0] = 0;
02007     z[i +  6] = mad_f_mul(yptr[ 0 + 0], wptr[0]);
02008 
02009     MAD_F_ML0(hi, lo, yptr[ 0 + 6], wptr[6]);
02010     MAD_F_MLA(hi, lo, yptr[12 + 0], wptr[0]);
02011 
02012     z[i + 12] = MAD_F_MLZ(hi, lo);
02013 
02014     MAD_F_ML0(hi, lo, yptr[12 + 6], wptr[6]);
02015     MAD_F_MLA(hi, lo, yptr[24 + 0], wptr[0]);
02016 
02017     z[i + 18] = MAD_F_MLZ(hi, lo);
02018 
02019     z[i + 24] = mad_f_mul(yptr[24 + 6], wptr[6]);
02020     z[i + 30] = 0;
02021 
02022     ++yptr;
02023     ++wptr;
02024   }
02025 }
02026 
02027 /*
02028  * NAME:        III_overlap()
02029  * DESCRIPTION: perform overlap-add of windowed IMDCT outputs
02030  */
02031 static
02032 void III_overlap(mad_fixed_t const output[36], mad_fixed_t overlap[18],
02033                  mad_fixed_t sample[18][32], unsigned int sb)
02034 {
02035   unsigned int i;
02036 
02037 # if defined(ASO_INTERLEAVE2)
02038   {
02039     register mad_fixed_t tmp1, tmp2;
02040 
02041     tmp1 = overlap[0];
02042     tmp2 = overlap[1];
02043 
02044     for (i = 0; i < 16; i += 2) {
02045       sample[i + 0][sb] = output[i + 0 +  0] + tmp1;
02046       overlap[i + 0]    = output[i + 0 + 18];
02047       tmp1 = overlap[i + 2];
02048 
02049       sample[i + 1][sb] = output[i + 1 +  0] + tmp2;
02050       overlap[i + 1]    = output[i + 1 + 18];
02051       tmp2 = overlap[i + 3];
02052     }
02053 
02054     sample[16][sb] = output[16 +  0] + tmp1;
02055     overlap[16]    = output[16 + 18];
02056     sample[17][sb] = output[17 +  0] + tmp2;
02057     overlap[17]    = output[17 + 18];
02058   }
02059 # elif 0
02060   for (i = 0; i < 18; i += 2) {
02061     sample[i + 0][sb] = output[i + 0 +  0] + overlap[i + 0];
02062     overlap[i + 0]    = output[i + 0 + 18];
02063 
02064     sample[i + 1][sb] = output[i + 1 +  0] + overlap[i + 1];
02065     overlap[i + 1]    = output[i + 1 + 18];
02066   }
02067 # else
02068   for (i = 0; i < 18; ++i) {
02069     sample[i][sb] = output[i +  0] + overlap[i];
02070     overlap[i]    = output[i + 18];
02071   }
02072 # endif
02073 }
02074 
02075 /*
02076  * NAME:        III_overlap_z()
02077  * DESCRIPTION: perform "overlap-add" of zero IMDCT outputs
02078  */
02079 static inline
02080 void III_overlap_z(mad_fixed_t overlap[18],
02081                    mad_fixed_t sample[18][32], unsigned int sb)
02082 {
02083   unsigned int i;
02084 
02085 # if defined(ASO_INTERLEAVE2)
02086   {
02087     register mad_fixed_t tmp1, tmp2;
02088 
02089     tmp1 = overlap[0];
02090     tmp2 = overlap[1];
02091 
02092     for (i = 0; i < 16; i += 2) {
02093       sample[i + 0][sb] = tmp1;
02094       overlap[i + 0]    = 0;
02095       tmp1 = overlap[i + 2];
02096 
02097       sample[i + 1][sb] = tmp2;
02098       overlap[i + 1]    = 0;
02099       tmp2 = overlap[i + 3];
02100     }
02101 
02102     sample[16][sb] = tmp1;
02103     overlap[16]    = 0;
02104     sample[17][sb] = tmp2;
02105     overlap[17]    = 0;
02106   }
02107 # else
02108   for (i = 0; i < 18; ++i) {
02109     sample[i][sb] = overlap[i];
02110     overlap[i]    = 0;
02111   }
02112 # endif
02113 }
02114 
02115 /*
02116  * NAME:        III_freqinver()
02117  * DESCRIPTION: perform subband frequency inversion for odd sample lines
02118  */
02119 static
02120 void III_freqinver(mad_fixed_t sample[18][32], unsigned int sb)
02121 {
02122   unsigned int i;
02123 
02124 # if 1 || defined(ASO_INTERLEAVE1) || defined(ASO_INTERLEAVE2)
02125   {
02126     register mad_fixed_t tmp1, tmp2;
02127 
02128     tmp1 = sample[1][sb];
02129     tmp2 = sample[3][sb];
02130 
02131     for (i = 1; i < 13; i += 4) {
02132       sample[i + 0][sb] = -tmp1;
02133       tmp1 = sample[i + 4][sb];
02134       sample[i + 2][sb] = -tmp2;
02135       tmp2 = sample[i + 6][sb];
02136     }
02137 
02138     sample[13][sb] = -tmp1;
02139     tmp1 = sample[17][sb];
02140     sample[15][sb] = -tmp2;
02141     sample[17][sb] = -tmp1;
02142   }
02143 # else
02144   for (i = 1; i < 18; i += 2)
02145     sample[i][sb] = -sample[i][sb];
02146 # endif
02147 }
02148 
02149 /*
02150  * NAME:        III_decode()
02151  * DESCRIPTION: decode frame main_data
02152  */
02153 static
02154 enum mad_error III_decode(struct mad_bitptr *ptr, struct mad_frame *frame,
02155                           struct sideinfo *si, unsigned int nch)
02156 {
02157   struct mad_header *header = &frame->header;
02158   unsigned int sfreqi, ngr, gr;
02159 
02160   {
02161     unsigned int sfreq;
02162 
02163     sfreq = header->samplerate;
02164     if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
02165       sfreq *= 2;
02166 
02167     /* 48000 => 0, 44100 => 1, 32000 => 2,
02168        24000 => 3, 22050 => 4, 16000 => 5 */
02169     sfreqi = ((sfreq >>  7) & 0x000f) +
02170              ((sfreq >> 15) & 0x0001) - 8;
02171 
02172     if (header->flags & MAD_FLAG_MPEG_2_5_EXT)
02173       sfreqi += 3;
02174   }
02175 
02176   /* scalefactors, Huffman decoding, requantization */
02177 
02178   ngr = (header->flags & MAD_FLAG_LSF_EXT) ? 1 : 2;
02179 
02180   for (gr = 0; gr < ngr; ++gr) {
02181     struct granule *granule = &si->gr[gr];
02182     unsigned char const *sfbwidth[2];
02183     mad_fixed_t xr[2][576];
02184     unsigned int ch;
02185     enum mad_error error;
02186 
02187     for (ch = 0; ch < nch; ++ch) {
02188       struct channel *channel = &granule->ch[ch];
02189       unsigned int part2_length;
02190 
02191       sfbwidth[ch] = sfbwidth_table[sfreqi].l;
02192       if (channel->block_type == 2) {
02193         sfbwidth[ch] = (channel->flags & mixed_block_flag) ?
02194           sfbwidth_table[sfreqi].m : sfbwidth_table[sfreqi].s;
02195       }
02196 
02197       if (header->flags & MAD_FLAG_LSF_EXT) {
02198         part2_length = III_scalefactors_lsf(ptr, channel,
02199                                             ch == 0 ? 0 : &si->gr[1].ch[1],
02200                                             header->mode_extension);
02201       }
02202       else {
02203         part2_length = III_scalefactors(ptr, channel, &si->gr[0].ch[ch],
02204                                         gr == 0 ? 0 : si->scfsi[ch]);
02205       }
02206 
02207       error = III_huffdecode(ptr, xr[ch], channel, sfbwidth[ch], part2_length);
02208       if (error)
02209         return error;
02210     }
02211 
02212     /* joint stereo processing */
02213 
02214     if (header->mode == MAD_MODE_JOINT_STEREO && header->mode_extension) {
02215       error = III_stereo(xr, granule, header, sfbwidth[0]);
02216       if (error)
02217         return error;
02218     }
02219 
02220     /* reordering, alias reduction, IMDCT, overlap-add, frequency inversion */
02221 
02222     for (ch = 0; ch < nch; ++ch) {
02223       struct channel const *channel = &granule->ch[ch];
02224       mad_fixed_t (*sample)[32] = &frame->sbsample[ch][18 * gr];
02225       unsigned int sb, l, i, sblimit;
02226       mad_fixed_t output[36];
02227 
02228       if (channel->block_type == 2) {
02229         III_reorder(xr[ch], channel, sfbwidth[ch]);
02230 
02231 # if !defined(OPT_STRICT)
02232         /*
02233          * According to ISO/IEC 11172-3, "Alias reduction is not applied for
02234          * granules with block_type == 2 (short block)." However, other
02235          * sources suggest alias reduction should indeed be performed on the
02236          * lower two subbands of mixed blocks. Most other implementations do
02237          * this, so by default we will too.
02238          */
02239         if (channel->flags & mixed_block_flag)
02240           III_aliasreduce(xr[ch], 36);
02241 # endif
02242       }
02243       else
02244         III_aliasreduce(xr[ch], 576);
02245 
02246       l = 0;
02247 
02248       /* subbands 0-1 */
02249 
02250       if (channel->block_type != 2 || (channel->flags & mixed_block_flag)) {
02251         unsigned int block_type;
02252 
02253         block_type = channel->block_type;
02254         if (channel->flags & mixed_block_flag)
02255           block_type = 0;
02256 
02257         /* long blocks */
02258         for (sb = 0; sb < 2; ++sb, l += 18) {
02259           III_imdct_l(&xr[ch][l], output, block_type);
02260           III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
02261         }
02262       }
02263       else {
02264         /* short blocks */
02265         for (sb = 0; sb < 2; ++sb, l += 18) {
02266           III_imdct_s(&xr[ch][l], output);
02267           III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
02268         }
02269       }
02270 
02271       III_freqinver(sample, 1);
02272 
02273       /* (nonzero) subbands 2-31 */
02274 
02275       i = 576;
02276       while (i > 36 && xr[ch][i - 1] == 0)
02277         --i;
02278 
02279       sblimit = 32 - (576 - i) / 18;
02280 
02281       if (channel->block_type != 2) {
02282         /* long blocks */
02283         for (sb = 2; sb < sblimit; ++sb, l += 18) {
02284           III_imdct_l(&xr[ch][l], output, channel->block_type);
02285           III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
02286 
02287           if (sb & 1)
02288             III_freqinver(sample, sb);
02289         }
02290       }
02291       else {
02292         /* short blocks */
02293         for (sb = 2; sb < sblimit; ++sb, l += 18) {
02294           III_imdct_s(&xr[ch][l], output);
02295           III_overlap(output, (*frame->overlap)[ch][sb], sample, sb);
02296 
02297           if (sb & 1)
02298             III_freqinver(sample, sb);
02299         }
02300       }
02301 
02302       /* remaining (zero) subbands */
02303 
02304       for (sb = sblimit; sb < 32; ++sb) {
02305         III_overlap_z((*frame->overlap)[ch][sb], sample, sb);
02306 
02307         if (sb & 1)
02308           III_freqinver(sample, sb);
02309       }
02310     }
02311   }
02312 
02313   return MAD_ERROR_NONE;
02314 }
02315 
02316 /*
02317  * NAME:        layer->III()
02318  * DESCRIPTION: decode a single Layer III frame
02319  */
02320 int mad_layer_III(struct mad_stream *stream, struct mad_frame *frame)
02321 {
02322   struct mad_header *header = &frame->header;
02323   unsigned int nch, priv_bitlen, next_md_begin = 0;
02324   unsigned int si_len, data_bitlen, md_len;
02325   unsigned int frame_space, frame_used, frame_free;
02326   struct mad_bitptr ptr;
02327   struct sideinfo si;
02328   enum mad_error error;
02329   int result = 0;
02330 
02331   /* allocate Layer III dynamic structures */
02332 
02333   if (stream->main_data == 0) {
02334     stream->main_data = malloc(MAD_BUFFER_MDLEN);
02335     if (stream->main_data == 0) {
02336       stream->error = MAD_ERROR_NOMEM;
02337       return -1;
02338     }
02339   }
02340 
02341   if (frame->overlap == 0) {
02342     frame->overlap = calloc(2 * 32 * 18, sizeof(mad_fixed_t));
02343     if (frame->overlap == 0) {
02344       stream->error = MAD_ERROR_NOMEM;
02345       return -1;
02346     }
02347   }
02348 
02349   nch = MAD_NCHANNELS(header);
02350   si_len = (header->flags & MAD_FLAG_LSF_EXT) ?
02351     (nch == 1 ? 9 : 17) : (nch == 1 ? 17 : 32);
02352 
02353   /* check frame sanity */
02354 
02355   if (stream->next_frame - mad_bit_nextbyte(&stream->ptr) <
02356       (signed int) si_len) {
02357     stream->error = MAD_ERROR_BADFRAMELEN;
02358     stream->md_len = 0;
02359     return -1;
02360   }
02361 
02362   /* check CRC word */
02363 
02364   if (header->flags & MAD_FLAG_PROTECTION) {
02365     header->crc_check =
02366       mad_bit_crc(stream->ptr, si_len * CHAR_BIT, header->crc_check);
02367 
02368     if (header->crc_check != header->crc_target &&
02369         !(frame->options & MAD_OPTION_IGNORECRC)) {
02370       stream->error = MAD_ERROR_BADCRC;
02371       result = -1;
02372     }
02373   }
02374 
02375   /* decode frame side information */
02376 
02377   error = III_sideinfo(&stream->ptr, nch, header->flags & MAD_FLAG_LSF_EXT,
02378                        &si, &data_bitlen, &priv_bitlen);
02379   if (error && result == 0) {
02380     stream->error = error;
02381     result = -1;
02382   }
02383 
02384   header->flags        |= priv_bitlen;
02385   header->private_bits |= si.private_bits;
02386 
02387   /* find main_data of next frame */
02388 
02389   {
02390     struct mad_bitptr peek;
02391     unsigned long header;
02392 
02393     mad_bit_init(&peek, stream->next_frame);
02394 
02395     header = mad_bit_read(&peek, 32);
02396     if ((header & 0xffe60000L) /* syncword | layer */ == 0xffe20000L) {
02397       if (!(header & 0x00010000L))  /* protection_bit */
02398         mad_bit_skip(&peek, 16);  /* crc_check */
02399 
02400       next_md_begin =
02401         mad_bit_read(&peek, (header & 0x00080000L) /* ID */ ? 9 : 8);
02402     }
02403 
02404     mad_bit_finish(&peek);
02405   }
02406 
02407   /* find main_data of this frame */
02408 
02409   frame_space = stream->next_frame - mad_bit_nextbyte(&stream->ptr);
02410 
02411   if (next_md_begin > si.main_data_begin + frame_space)
02412     next_md_begin = 0;
02413 
02414   md_len = si.main_data_begin + frame_space - next_md_begin;
02415 
02416   frame_used = 0;
02417 
02418   if (si.main_data_begin == 0) {
02419     ptr = stream->ptr;
02420     stream->md_len = 0;
02421 
02422     frame_used = md_len;
02423   }
02424   else {
02425     if (si.main_data_begin > stream->md_len) {
02426       if (result == 0) {
02427         stream->error = MAD_ERROR_BADDATAPTR;
02428         result = -1;
02429       }
02430     }
02431     else {
02432       mad_bit_init(&ptr,
02433                    *stream->main_data + stream->md_len - si.main_data_begin);
02434 
02435       if (md_len > si.main_data_begin) {
02436         assert(stream->md_len + md_len -
02437                si.main_data_begin <= MAD_BUFFER_MDLEN);
02438 
02439         memcpy(*stream->main_data + stream->md_len,
02440                mad_bit_nextbyte(&stream->ptr),
02441                frame_used = md_len - si.main_data_begin);
02442         stream->md_len += frame_used;
02443       }
02444     }
02445   }
02446 
02447   frame_free = frame_space - frame_used;
02448 
02449   /* decode main_data */
02450 
02451   if (result == 0) {
02452     error = III_decode(&ptr, frame, &si, nch);
02453     if (error) {
02454       stream->error = error;
02455       result = -1;
02456     }
02457 
02458     /* designate ancillary bits */
02459 
02460     stream->anc_ptr    = ptr;
02461     stream->anc_bitlen = md_len * CHAR_BIT - data_bitlen;
02462   }
02463 
02464 # if 0 && defined(DEBUG)
02465   fprintf(stderr,
02466           "main_data_begin:%u, md_len:%u, frame_free:%u, "
02467           "data_bitlen:%u, anc_bitlen: %u\n",
02468           si.main_data_begin, md_len, frame_free,
02469           data_bitlen, stream->anc_bitlen);
02470 # endif
02471 
02472   /* preload main_data buffer with up to 511 bytes for next frame(s) */
02473 
02474   if (frame_free >= next_md_begin) {
02475     memcpy(*stream->main_data,
02476            stream->next_frame - next_md_begin, next_md_begin);
02477     stream->md_len = next_md_begin;
02478   }
02479   else {
02480     if (md_len < si.main_data_begin) {
02481       unsigned int extra;
02482 
02483       extra = si.main_data_begin - md_len;
02484       if (extra + frame_free > next_md_begin)
02485         extra = next_md_begin - frame_free;
02486 
02487       if (extra < stream->md_len) {
02488         memmove(*stream->main_data,
02489                 *stream->main_data + stream->md_len - extra, extra);
02490         stream->md_len = extra;
02491       }
02492     }
02493     else
02494       stream->md_len = 0;
02495 
02496     memcpy(*stream->main_data + stream->md_len,
02497            stream->next_frame - frame_free, frame_free);
02498     stream->md_len += frame_free;
02499   }
02500 
02501   return result;
02502 }

Generated on Tue Dec 13 14:47:48 2005 for guliverkli by  doxygen 1.4.5