summaryrefslogtreecommitdiff
path: root/tools/mkboot.c
blob: 77f65d9dc70868d832506f517839660f6d3230c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 by Linus Nielsen Feltzing
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mkboot.h"

#ifndef RBUTIL
static void usage(void)
{
    printf("usage: mkboot <target> <firmware file> <boot file> <output file>\n");
    printf("available targets:\n"
           "\t-h100     Iriver H1x0\n"
           "\t-h300     Iriver H3x0\n"
           "\t-iax5     iAudio X5\n"
           "\t-iam5     iAudio M5\n");

    exit(1);
}
#endif

#ifndef RBUTIL
int main(int argc, char *argv[])
{
    if(argc != 5)
    {
        usage();
        return 1;
    }

    if ( ! strcmp(argv[1], "-h100"))
        return mkboot_iriver(argv[2], argv[3], argv[4], 0x1f0000);
    
    if ( ! strcmp(argv[1], "-h300"))
        return mkboot_iriver(argv[2], argv[3], argv[4], 0x3f0000);
    
    if ( ! strcmp(argv[1], "-iax5"))
        return mkboot_iaudio(argv[2], argv[3], argv[4], 0);
        
    if ( ! strcmp(argv[1], "-iam5"))
        return mkboot_iaudio(argv[2], argv[3], argv[4], 1);

    usage();
    return 1;
}
#endif

static unsigned char image[0x400000 + 0x220 + 0x400000/0x200];

int mkboot_iriver(const char* infile, const char* bootfile, const char* outfile, int origin)
{
    FILE *f;
    int i;
    int len;
    int actual_length, total_length, binary_length, num_chksums;

    memset(image, 0xff, sizeof(image));

    /* First, read the iriver original firmware into the image */
    f = fopen(infile, "rb");
    if(!f) {
        perror(infile);
        return -1;
    }

    i = fread(image, 1, 16, f);
    if(i < 16) {
        perror(infile);
        fclose(f);
        return -2;
    }

    /* This is the length of the binary image without the scrambling
       overhead (but including the ESTFBINR header) */
    binary_length = image[4] + (image[5] << 8) +
        (image[6] << 16) + (image[7] << 24);

    /* Read the rest of the binary data, but not the checksum block */
    len = binary_length+0x200-16;
    i = fread(image+16, 1, len, f);
    if(i < len) {
        perror(infile);
        fclose(f);
        return -3;
    }
    
    fclose(f);

    /* Now, read the boot loader into the image */
    f = fopen(bootfile, "rb");
    if(!f) {
        perror(bootfile);
        fclose(f);
        return -4;
    }

    fseek(f, 0, SEEK_END);
    len = ftell(f);

    fseek(f, 0, SEEK_SET);

    i = fread(image+0x220 + origin, 1, len, f);
    if(i < len) {
        perror(bootfile);
        fclose(f);
        return -5;
    }

    fclose(f);

    f = fopen(outfile, "wb");
    if(!f) {
        perror(outfile);
        return -6;
    }

    /* Patch the reset vector to start the boot loader */
    image[0x220 + 4] = image[origin + 0x220 + 4];
    image[0x220 + 5] = image[origin + 0x220 + 5];
    image[0x220 + 6] = image[origin + 0x220 + 6];
    image[0x220 + 7] = image[origin + 0x220 + 7];

    /* This is the actual length of the binary, excluding all headers */
    actual_length = origin + len;

    /* Patch the ESTFBINR header */
    image[0x20c] = (actual_length >> 24) & 0xff;
    image[0x20d] = (actual_length >> 16) & 0xff;
    image[0x20e] = (actual_length >> 8) & 0xff;
    image[0x20f] = actual_length & 0xff;
    
    image[0x21c] = (actual_length >> 24) & 0xff;
    image[0x21d] = (actual_length >> 16) & 0xff;
    image[0x21e] = (actual_length >> 8) & 0xff;
    image[0x21f] = actual_length & 0xff;

    /* This is the length of the binary, including the ESTFBINR header and
       rounded up to the nearest 0x200 boundary */
    binary_length = (actual_length + 0x20 + 0x1ff) & 0xfffffe00;

    /* The number of checksums, i.e number of 0x200 byte blocks */
    num_chksums = binary_length / 0x200;

    /* The total file length, including all headers and checksums */
    total_length = binary_length + num_chksums + 0x200;

    /* Patch the scrambler header with the new length info */
    image[0] = total_length & 0xff;
    image[1] = (total_length >> 8) & 0xff;
    image[2] = (total_length >> 16) & 0xff;
    image[3] = (total_length >> 24) & 0xff;
    
    image[4] = binary_length & 0xff;
    image[5] = (binary_length >> 8) & 0xff;
    image[6] = (binary_length >> 16) & 0xff;
    image[7] = (binary_length >> 24) & 0xff;
    
    image[8] = num_chksums & 0xff;
    image[9] = (num_chksums >> 8) & 0xff;
    image[10] = (num_chksums >> 16) & 0xff;
    image[11] = (num_chksums >> 24) & 0xff;
    
    i = fwrite(image, 1, total_length, f);
    if(i < total_length) {
        perror(outfile);
        fclose(f);
        return -7;
    }

    printf("Wrote 0x%x bytes in %s\n", total_length, outfile);
    
    fclose(f);
    
    return 0;
}

/* iAudio firmware update file header size */
#define HEADER_SIZE 0x1030
/* Address of flash contents that get overwritten by a firmware update.
 * Contents before this address contain the preloader and are not affected
 * by a firmware update.
 * -> Firmware update file contents starting at offset HEADER_SIZE end up
 * in flash at address FLASH_START
 */
#define FLASH_START        0x00010000
/* Start of unused space in original firmware (flash address, not file
 * offset!) where we patch in the Rockbox loader */
#define ROCKBOX_BOOTLOADER 0x00150000
/* End of unused space in original firmware */
#define BOOTLOADER_LIMIT   0x00170000 

/* Patch the Rockbox bootloader into free space in the original firmware
 * (starting at 0x150000). The preloader starts execution of the OF at
 * 0x10000 which normally contains a jsr 0x10010. We also patch this to
 * do a jsr 0x150000 to the Rockbox dual boot loader instead. If it then
 * decides to start the OF instead of Rockbox, it simply does a jmp
 * 0x10010 instead of loading Rockbox from disk.
 */
int mkboot_iaudio(const char* infile, const char* bootfile, const char* outfile, int model_nr)
{
    size_t flength, blength;
    unsigned char *bbuf, *fbuf, *p;
    const unsigned char fsig[] = {
        0x4e, 0xb9, 0x00, 0x01, 0x00, 0x10 };               /* jsr 0x10010 */
    unsigned char bsig[2][8] = {
        /* dualboot signatures */
        { 0x60, 0x06, 0x44, 0x42, 0x69, 0x61, 0x78, 0x35 }, /* X5 */
        { 0x60, 0x06, 0x44, 0x42, 0x69, 0x61, 0x6d, 0x35 }, /* M5 */ };
    FILE *ffile, *bfile, *ofile;
    unsigned char sum = 0;
    int i;

    /* read input files */
    if ((bfile = fopen(bootfile, "rb")) == NULL) {
        perror("Cannot open Rockbox bootloader file.\n");
        return 1;
    }

    fseek(bfile, 0, SEEK_END);
    blength = ftell(bfile);
    fseek(bfile, 0, SEEK_SET);

    if (blength + ROCKBOX_BOOTLOADER >= BOOTLOADER_LIMIT) {
        fprintf(stderr, "Rockbox bootloader is too big.\n");
        return 1;
    }
 
    if ((ffile = fopen(infile, "rb")) == NULL) {
        perror("Cannot open original firmware file.");
        return 1;
    }
  
    fseek(ffile, 0, SEEK_END);
    flength = ftell(ffile);
    fseek(ffile, 0, SEEK_SET);

    bbuf = malloc(blength);
    fbuf = malloc(flength);

    if (!bbuf || !fbuf) {
        fprintf(stderr, "Out of memory.\n");
        return 1;
    }

    if (   fread(bbuf, 1, blength, bfile) < blength
        || fread(fbuf, 1, flength, ffile) < flength) {
        fprintf(stderr, "Read error.\n");
        return 1;
    }
    fclose(bfile);
    fclose(ffile);

    /* verify format of input files */
    if (blength < 0x10 || memcmp(bbuf, bsig[model_nr], sizeof(bsig[0]))) {
        fprintf(stderr, "Rockbox bootloader format error (is it bootloader.bin?).\n");
        return 1;
    }
    if (flength < HEADER_SIZE-FLASH_START+BOOTLOADER_LIMIT
        || memcmp(fbuf+HEADER_SIZE, fsig, sizeof(fsig))) {
        fprintf(stderr, "Original firmware format error.\n");
        return 1;
    }

    /* verify firmware is not overrun */
    for (i = ROCKBOX_BOOTLOADER; i < BOOTLOADER_LIMIT; i++) {
        if (fbuf[HEADER_SIZE-FLASH_START+i] != 0xff) {
            fprintf(stderr, "Original firmware has grown too much.\n");
            return 1;
        }
    }

    /* change jsr 0x10010 to jsr DUAL_BOOTLOADER */
    p = fbuf + HEADER_SIZE + 2;
    *p++ = (ROCKBOX_BOOTLOADER >> 24) & 0xff;
    *p++ = (ROCKBOX_BOOTLOADER >> 16) & 0xff;
    *p++ = (ROCKBOX_BOOTLOADER >>  8) & 0xff;
    *p++ = (ROCKBOX_BOOTLOADER      ) & 0xff;

    p = fbuf + HEADER_SIZE + ROCKBOX_BOOTLOADER - FLASH_START;
    memcpy(p, bbuf, blength);

    /* recalc checksum */
    for (i = HEADER_SIZE; (size_t)i < flength; i++)
        sum += fbuf[i];
    fbuf[0x102b] = sum;

    /* write output */
    if ((ofile = fopen(outfile, "wb")) == NULL) {
        perror("Cannot open output file");
        return 1;
    }
    if (fwrite(fbuf, 1, flength, ofile) < flength) {
        fprintf(stderr, "Write error.\n");
        return 1;
    }
    fclose(ofile);
    free(bbuf);
    free(fbuf);

    return 0;
}
99 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485
/*  Shine is an MP3 encoder
 *  Copyright (C) 1999-2000  Gabriel Bouvigne
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details. */

#include "plugin.h"

PLUGIN_HEADER
PLUGIN_IRAM_DECLARE

static struct plugin_api* rb;

MEM_FUNCTION_WRAPPERS(rb);

#define SAMP_PER_FRAME       1152
#define SAMPL2                576
#define SBLIMIT                32
#define HTN                    16
#define memcpy         rb->memcpy
#define memset         rb->memset
#define putlong(c, s)  if(s+sz <= 32) { cc = (cc << s) | c;      sz+= s; } \
                       else           { putbits(cc, sz); cc = c; sz = s; }

enum e_byte_order { order_unknown, order_bigEndian, order_littleEndian };

typedef unsigned  long uint32;
typedef unsigned short uint16;
typedef unsigned  char  uint8;


typedef struct {
    int   type; /* 0=(22.05,24,16kHz) 1=(44.1,48,32kHz) */
    int   mode; /* 0=stereo, 1=jstereo, 2=dual, 3=mono  */
    int   bitrate;
    int   padding;
    int   num_bands;
    long  bitr_id;
    int   smpl_id;
} mpeg_t;

/* Side information */
typedef struct {
    uint32 part2_3_length;
    int    count1;          /* number of 0-1-quadruples  */
    uint32 global_gain;
    uint32 table_select[4];
    uint32 region_0_1;
    uint32 address1;
    uint32 address2;
    uint32 address3;
    long   quantStep;
    long   additStep;
    long   max_val;
} side_info_t;

typedef struct {
    enum e_byte_order byte_order;
    side_info_t       cod_info[2][2];
    mpeg_t            mpg;
    long              frac_per_frame;
    long              byte_per_frame;
    long              slot_lag;
    int               sideinfo_len;
    int               mean_bits;
    int               ResvSize;
    int               channels;
    int               granules;
    int               resample;
    long              samplerate;
} config_t;

typedef struct {
    int     bitpos;     /* current bitpos for writing   */
    uint32  bbuf[263];
} BF_Data;

struct huffcodetab {
  int          len;     /* max. index                   */
  const uint8  *table;  /* pointer to array[len][len]   */
  const uint8  *hlen;   /* pointer to array[len][len]   */
};

struct huffcodebig {
  int          len;     /* max. index                   */
  int          linbits; /* number of linbits            */
  int          linmax;  /* max number stored in linbits */
};

#define shft4(x)    ((x +     8) >>  4)
#define shft9(x)    ((x +   256) >>  9)
#define shft13(x)   ((x +  4096) >> 13)
#define shft15(x)   ((x + 16384) >> 15)
#define shft16(x)   ((x + 32768) >> 16)
#define shft_n(x,n) ((x) >> n)
#define SQRT        724 /* sqrt(2) * 512 */

short     mfbuf       [2*(1152+512)]          IBSS_ATTR; /*  3328 Bytes */
int       sb_data     [2][2][18][SBLIMIT]     IBSS_ATTR; /* 13824 Bytes */
int       mdct_freq   [SAMPL2]                IBSS_ATTR; /*  9216 Bytes */
short     enc_data    [SAMPL2]                IBSS_ATTR; /*  4608 Bytes */
uint32    scalefac    [23]                    IBSS_ATTR; /*    92 Bytes */
BF_Data   CodedData                           IBSS_ATTR; /*  1056 Bytes */
int       ca          [8]                     IBSS_ATTR; /*    32 Bytes */
int       cs          [8]                     IBSS_ATTR; /*    32 Bytes */
int       cx          [9]                     IBSS_ATTR; /*    36 Bytes */
int       win         [18][4]                 IBSS_ATTR; /*   288 Bytes */
short     enwindow    [15*27+24]              IBSS_ATTR; /*   862 Bytes */
short     int2idx     [4096]                  IBSS_ATTR; /*  8192 Bytes */
uint8     ht_count    [2][2][16]              IBSS_ATTR; /*    64 Bytes */
uint32    tab01       [ 16]                   IBSS_ATTR; /*    64 Bytes */
uint32    tab23       [  9]                   IBSS_ATTR; /*    36 Bytes */
uint32    tab56       [ 16]                   IBSS_ATTR; /*    64 Bytes */
uint32    tab1315     [256]                   IBSS_ATTR; /*  1024 Bytes */
uint32    tab1624     [256]                   IBSS_ATTR; /*  1024 Bytes */
uint32    tab789      [ 36]                   IBSS_ATTR; /*   144 Bytes */
uint32    tabABC      [ 64]                   IBSS_ATTR; /*   256 Bytes */
uint8     t1HB        [  4]                   IBSS_ATTR;
uint8     t2HB        [  9]                   IBSS_ATTR;
uint8     t3HB        [  9]                   IBSS_ATTR;
uint8     t5HB        [ 16]                   IBSS_ATTR;
uint8     t6HB        [ 16]                   IBSS_ATTR;
uint8     t7HB        [ 36]                   IBSS_ATTR;
uint8     t8HB        [ 36]                   IBSS_ATTR;
uint8     t9HB        [ 36]                   IBSS_ATTR;
uint8     t10HB       [ 64]                   IBSS_ATTR;
uint8     t11HB       [ 64]                   IBSS_ATTR;
uint8     t12HB       [ 64]                   IBSS_ATTR;
uint8     t13HB       [256]                   IBSS_ATTR;
uint8     t15HB       [256]                   IBSS_ATTR;
uint16    t16HB       [256]                   IBSS_ATTR;
uint16    t24HB       [256]                   IBSS_ATTR;
uint8     t1l         [  8]                   IBSS_ATTR;
uint8     t2l         [  9]                   IBSS_ATTR;
uint8     t3l         [  9]                   IBSS_ATTR;
uint8     t5l         [ 16]                   IBSS_ATTR;
uint8     t6l         [ 16]                   IBSS_ATTR;
uint8     t7l         [ 36]                   IBSS_ATTR;
uint8     t8l         [ 36]                   IBSS_ATTR;
uint8     t9l         [ 36]                   IBSS_ATTR;
uint8     t10l        [ 64]                   IBSS_ATTR;
uint8     t11l        [ 64]                   IBSS_ATTR;
uint8     t12l        [ 64]                   IBSS_ATTR;
uint8     t13l        [256]                   IBSS_ATTR;
uint8     t15l        [256]                   IBSS_ATTR;
uint8     t16l        [256]                   IBSS_ATTR;
uint8     t24l        [256]                   IBSS_ATTR;
struct huffcodetab ht [HTN]                   IBSS_ATTR;

static const uint8 ht_count_const[2][2][16] =
{ { { 1,  5,  4,  5,  6,  5, 4, 4, 7, 3, 6, 0, 7, 2, 3, 1 },     /* table0 */
    { 1,  5,  5,  7,  5,  8, 7, 9, 5, 7, 7, 9, 7, 9, 9,10 } },   /* hleng0 */
  { {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 },     /* table1 */
    { 4,  5,  5,  6,  5,  6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 } } }; /* hleng1 */

static const uint8  t1HB_const[4]  = {1,1,1,0}; 
static const uint8  t2HB_const[9]  = {1,2,1,3,1,1,3,2,0};
static const uint8  t3HB_const[9]  = {3,2,1,1,1,1,3,2,0};
static const uint8  t5HB_const[16] = {1,2,6,5,3,1,4,4,7,5,7,1,6,1,1,0};
static const uint8  t6HB_const[16] = {7,3,5,1,6,2,3,2,5,4,4,1,3,3,2,0};

static const uint8  t7HB_const[36] =
{  1, 2,10,19,16,10, 3, 3, 7,10, 5, 3,11, 4,13,17, 8, 4,
  12,11,18,15,11, 2, 7, 6, 9,14, 3, 1, 6, 4, 5, 3, 2, 0 };

static const uint8  t8HB_const[36] =
{  3, 4, 6,18,12, 5, 5, 1, 2,16, 9, 3, 7, 3, 5,14, 7, 3,
  19,17,15,13,10, 4,13, 5, 8,11, 5, 1,12, 4, 4, 1, 1, 0 };

static const uint8  t9HB_const[36] =
{  7, 5, 9,14,15, 7, 6, 4, 5, 5, 6, 7, 7, 6, 8, 8, 8, 5,
  15, 6, 9,10, 5, 1,11, 7, 9, 6, 4, 1,14, 4, 6, 2, 6, 0 };

static const uint8 t10HB_const[64] =
{1,2,10,23,35,30,12,17,3,3,8,12,18,21,12,7,11,9,15,21,32,
 40,19,6,14,13,22,34,46,23,18,7,20,19,33,47,27,22,9,3,31,22,
 41,26,21,20,5,3,14,13,10,11,16,6,5,1,9,8,7,8,4,4,2,0 };

static const uint8 t11HB_const[64] =
{3,4,10,24,34,33,21,15,5,3,4,10,32,17,11,10,11,7,13,18,30,
 31,20,5,25,11,19,59,27,18,12,5,35,33,31,58,30,16,7,5,28,26,
 32,19,17,15,8,14,14,12,9,13,14,9,4,1,11,4,6,6,6,3,2,0 };

static const uint8 t12HB_const[64] =
{9,6,16,33,41,39,38,26,7,5,6,9,23,16,26,11,17,7,11,14,21,
30,10,7,17,10,15,12,18,28,14,5,32,13,22,19,18,16,9,5,40,17,
31,29,17,13,4,2,27,12,11,15,10,7,4,1,27,12,8,12,6,3,1,0 };

static const uint8 t13HB_const[256] =
{1,5,14,21,34,51,46,71,42,52,68,52,67,44,43,19,3,4,12,19,31,26,44,33,31,24,32,
 24,31,35,22,14,15,13,23,36,59,49,77,65,29,40,30,40,27,33,42,16,22,20,37,61,56,
 79,73,64,43,76,56,37,26,31,25,14,35,16,60,57,97,75,114,91,54,73,55,41,48,53,
 23,24,58,27,50,96,76,70,93,84,77,58,79,29,74,49,41,17,47,45,78,74,115,94,90,
 79,69,83,71,50,59,38,36,15,72,34,56,95,92,85,91,90,86,73,77,65,51,44,43,42,43,
 20,30,44,55,78,72,87,78,61,46,54,37,30,20,16,53,25,41,37,44,59,54,81,66,76,57,
 54,37,18,39,11,35,33,31,57,42,82,72,80,47,58,55,21,22,26,38,22,53,25,23,38,70,
 60,51,36,55,26,34,23,27,14,9,7,34,32,28,39,49,75,30,52,48,40,52,28,18,17,9,5,
 45,21,34,64,56,50,49,45,31,19,12,15,10,7,6,3,48,23,20,39,36,35,53,21,16,23,13,
 10,6,1,4,2,16,15,17,27,25,20,29,11,17,12,16,8,1,1,0,1 };

static const uint8 t15HB_const[256] =
{7,12,18,53,47,76,124,108,89,123,108,119,107,81,122,63,13,5,16,27,46,36,61,51,
 42,70,52,83,65,41,59,36,19,17,15,24,41,34,59,48,40,64,50,78,62,80,56,33,29,28,
 25,43,39,63,55,93,76,59,93,72,54,75,50,29,52,22,42,40,67,57,95,79,72,57,89,69,
 49,66,46,27,77,37,35,66,58,52,91,74,62,48,79,63,90,62,40,38,125,32,60,56,50,
 92,78,65,55,87,71,51,73,51,70,30,109,53,49,94,88,75,66,122,91,73,56,42,64,44,
 21,25,90,43,41,77,73,63,56,92,77,66,47,67,48,53,36,20,71,34,67,60,58,49,88,76,
 67,106,71,54,38,39,23,15,109,53,51,47,90,82,58,57,48,72,57,41,23,27,62,9,86,
 42,40,37,70,64,52,43,70,55,42,25,29,18,11,11,118,68,30,55,50,46,74,65,49,39,
 24,16,22,13,14,7,91,44,39,38,34,63,52,45,31,52,28,19,14,8,9,3,123,60,58,53,47,
 43,32,22,37,24,17,12,15,10,2,1,71,37,34,30,28,20,17,26,21,16,10,6,8,6,2,0};

static const uint16 t16HB_const[256] =
{1,5,14,44,74,63,110,93,172,149,138,242,225,195,376,17,3,4,12,20,35,62,53,47,
 83,75,68,119,201,107,207,9,15,13,23,38,67,58,103,90,161,72,127,117,110,209,
 206,16,45,21,39,69,64,114,99,87,158,140,252,212,199,387,365,26,75,36,68,65,
 115,101,179,164,155,264,246,226,395,382,362,9,66,30,59,56,102,185,173,265,142,
 253,232,400,388,378,445,16,111,54,52,100,184,178,160,133,257,244,228,217,385,
 366,715,10,98,48,91,88,165,157,148,261,248,407,397,372,380,889,884,8,85,84,81,
 159,156,143,260,249,427,401,392,383,727,713,708,7,154,76,73,141,131,256,245,
 426,406,394,384,735,359,710,352,11,139,129,67,125,247,233,229,219,393,743,737,
 720,885,882,439,4,243,120,118,115,227,223,396,746,742,736,721,712,706,223,436,
 6,202,224,222,218,216,389,386,381,364,888,443,707,440,437,1728,4,747,211,210,
 208,370,379,734,723,714,1735,883,877,876,3459,865,2,377,369,102,187,726,722,
 358,711,709,866,1734,871,3458,870,434,0,12,10,7,11,10,17,11,9,13,12,10,7,5,3,
 1,3};

static const uint16 t24HB_const[256] =
{15,13,46,80,146,262,248,434,426,669,653,649,621,517,1032,88,14,12,21,38,71,
 130,122,216,209,198,327,345,319,297,279,42,47,22,41,74,68,128,120,221,207,194,
 182,340,315,295,541,18,81,39,75,70,134,125,116,220,204,190,178,325,311,293,
 271,16,147,72,69,135,127,118,112,210,200,188,352,323,306,285,540,14,263,66,
 129,126,119,114,214,202,192,180,341,317,301,281,262,12,249,123,121,117,113,
 215,206,195,185,347,330,308,291,272,520,10,435,115,111,109,211,203,196,187,
 353,332,313,298,283,531,381,17,427,212,208,205,201,193,186,177,169,320,303,
 286,268,514,377,16,335,199,197,191,189,181,174,333,321,305,289,275,521,379,
 371,11,668,184,183,179,175,344,331,314,304,290,277,530,383,373,366,10,652,346,
 171,168,164,318,309,299,287,276,263,513,375,368,362,6,648,322,316,312,307,302,
 292,284,269,261,512,376,370,364,359,4,620,300,296,294,288,282,273,266,515,380,
 374,369,365,361,357,2,1033,280,278,274,267,264,259,382,378,372,367,363,360,
 358,356,0,43,20,19,17,15,13,11,9,7,6,4,7,5,3,1,3};

static const uint32 tab1315_const[256] =
{ 0x010003,0x050005,0x070006,0x080008,0x090008,0x0a0009,0x0a000a,0x0b000a,
  0x0a000a,0x0b000b,0x0c000b,0x0c000c,0x0d000c,0x0d000c,0x0e000d,0x0e000e,
  0x040005,0x060005,0x080007,0x090008,0x0a0009,0x0a0009,0x0b000a,0x0b000a,
  0x0b000a,0x0b000b,0x0c000b,0x0c000c,0x0d000c,0x0e000c,0x0e000d,0x0e000d,
  0x070006,0x080007,0x090007,0x0a0008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,
  0x0b000a,0x0c000b,0x0c000b,0x0d000c,0x0d000c,0x0e000d,0x0f000d,0x0f000d,
  0x080007,0x090008,0x0a0008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,0x0c000b,
  0x0c000b,0x0d000b,0x0d000c,0x0d000c,0x0d000c,0x0e000d,0x0f000d,0x0f000d,
  0x090008,0x090008,0x0b0009,0x0b0009,0x0c000a,0x0c000a,0x0d000b,0x0d000b,
  0x0c000b,0x0d000b,0x0d000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000d,
  0x0a0009,0x0a0009,0x0b0009,0x0c000a,0x0c000a,0x0c000a,0x0d000b,0x0d000b,
  0x0d000b,0x0d000b,0x0e000c,0x0d000c,0x0f000d,0x0f000d,0x10000d,0x10000e,
  0x0a000a,0x0b0009,0x0c000a,0x0c000a,0x0d000a,0x0d000b,0x0d000b,0x0d000b,
  0x0d000b,0x0e000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000e,0x10000e,
  0x0b000a,0x0b000a,0x0c000a,0x0d000b,0x0d000b,0x0d000b,0x0e000b,0x0e000c,
  0x0e000c,0x0e000c,0x0f000c,0x0f000c,0x0f000d,0x10000d,0x12000d,0x12000e,
  0x0a000a,0x0a000a,0x0b000a,0x0c000b,0x0c000b,0x0d000b,0x0d000b,0x0e000c,
  0x0e000c,0x0e000c,0x0e000c,0x0f000d,0x0f000d,0x10000e,0x11000e,0x11000e,
  0x0b000a,0x0b000a,0x0c000b,0x0c000b,0x0d000b,0x0d000b,0x0d000c,0x0f000c,
  0x0e000c,0x0f000d,0x0f000d,0x10000d,0x10000d,0x10000e,0x12000e,0x11000e,
  0x0b000b,0x0c000b,0x0c000b,0x0d000b,0x0d000c,0x0e000c,0x0e000c,0x0f000c,
  0x0e000c,0x0f000d,0x10000d,0x0f000d,0x10000d,0x11000e,0x12000f,0x13000e,
  0x0c000b,0x0c000b,0x0c000b,0x0d000b,0x0e000c,0x0e000c,0x0e000c,0x0e000c,
  0x0f000d,0x0f000d,0x0f000d,0x10000d,0x11000e,0x11000e,0x11000e,0x12000f,
  0x0c000c,0x0d000c,0x0d000b,0x0e000c,0x0e000c,0x0f000c,0x0e000d,0x0f000d,
  0x10000d,0x10000d,0x11000d,0x11000d,0x11000e,0x12000e,0x12000f,0x12000f,
  0x0d000c,0x0d000c,0x0e000c,0x0f000c,0x0f000c,0x0f000d,0x10000d,0x10000d,
  0x10000d,0x10000e,0x10000e,0x11000e,0x12000e,0x11000e,0x12000f,0x12000f,
  0x0e000d,0x0e000d,0x0e000d,0x0f000d,0x0f000d,0x0f000d,0x11000d,0x10000d,
  0x10000e,0x13000e,0x11000e,0x11000e,0x11000f,0x13000f,0x12000e,0x12000f,
  0x0d000d,0x0e000d,0x0f000d,0x10000d,0x10000d,0x10000d,0x11000d,0x10000e,
  0x11000e,0x11000e,0x12000e,0x12000e,0x15000f,0x14000f,0x15000f,0x12000f };

static const uint32 tab01_const[16] =
{ 0x10004,0x50005,0x50005,0x70006,0x50005,0x80006,0x70006,0x90007,
  0x50005,0x70006,0x70006,0x90007,0x70006,0x90007,0x90007,0xa0008 };

static const uint32 tab23_const[ 9] =
{ 0x10002,0x40003,0x70007,0x40004,0x50004,0x70007,0x60006,0x70007,0x80008 };

static const uint32 tab56_const[16] =
{ 0x10003,0x40004,0x70006,0x80008,0x40004,0x50004,0x80006,0x90007,
  0x70005,0x80006,0x90007,0xa0008,0x80007,0x80007,0x90008,0xa0009 };

static const uint32 tab789_const[36] =
{0x00100803,0x00401004,0x00701c06,0x00902407,0x00902409,0x00a0280a,0x00401004,
 0x00601005,0x00801806,0x00902807,0x00902808,0x00a0280a,0x00701c05,0x00701806,
 0x00902007,0x00a02808,0x00a02809,0x00b02c0a,0x00802407,0x00902807,0x00a02808,
 0x00b02c09,0x00b02c09,0x00b0300a,0x00802408,0x00902408,0x00a02809,0x00b02c09,
 0x00b0300a,0x00c0300b,0x00902809,0x00a02809,0x00b02c0a,0x00c02c0a,0x00c0340b,
 0x00c0340b};

static const uint32 tabABC_const[64] =
{0x00100804,0x00401004,0x00701806,0x00902008,0x00a02409,0x00a0280a,0x00a0240a,
 0x00b0280a,0x00401004,0x00601405,0x00801806,0x00902007,0x00a02809,0x00b02809,
 0x00a0240a,0x00a0280a,0x00701806,0x00801c06,0x00902007,0x00a02408,0x00b02809,
 0x00c02c0a,0x00b02809,0x00b0280a,0x00802007,0x00902007,0x00a02408,0x00b02c08,
 0x00c02809,0x00c0300a,0x00b0280a,0x00c02c0a,0x00902408,0x00a02808,0x00b02809,
 0x00c02c09,0x00c02c0a,0x00c0300a,0x00c02c0a,0x00c0300b,0x00a02409,0x00b02809,
 0x00c02c0a,0x00c0300a,0x00d0300a,0x00d0340b,0x00c0300a,0x00d0340b,0x00902409,
 0x00a02409,0x00b02409,0x00c0280a,0x00c02c0a,0x00c0300b,0x00d0300b,0x00d0300c,
 0x00a0240a,0x00a0240a,0x00b0280a,0x00c02c0b,0x00c0300b,0x00d0300b,0x00d0300b,
 0x00d0300c};

static const uint32 tab1624_const[256] =
{0x00010004,0x00050005,0x00070007,0x00090008,0x000a0009,0x000a000a,0x000b000a,
 0x000b000b,0x000c000b,0x000c000c,0x000c000c,0x000d000c,0x000d000c,0x000d000c,
 0x000e000d,0x000a000a,0x00040005,0x00060006,0x00080007,0x00090008,0x000a0009,
 0x000b000a,0x000b000a,0x000b000b,0x000c000b,0x000c000b,0x000c000c,0x000d000c,
 0x000e000c,0x000d000c,0x000e000c,0x000a000a,0x00070007,0x00080007,0x00090008,
 0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000b,0x000d000b,0x000c000b,
 0x000d000b,0x000d000c,0x000d000c,0x000e000c,0x000e000d,0x000b0009,0x00090008,
 0x00090008,0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000a,0x000c000b,
 0x000d000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000f000c,0x000f000c,
 0x000c0009,0x000a0009,0x000a0009,0x000b0009,0x000b000a,0x000c000a,0x000c000a,
 0x000d000a,0x000d000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000f000c,
 0x000f000c,0x000f000d,0x000b0009,0x000a000a,0x000a0009,0x000b000a,0x000b000a,
 0x000c000a,0x000d000a,0x000d000b,0x000e000b,0x000d000b,0x000e000b,0x000e000c,
 0x000f000c,0x000f000c,0x000f000c,0x0010000c,0x000c0009,0x000b000a,0x000b000a,
 0x000b000a,0x000c000a,0x000d000a,0x000d000b,0x000d000b,0x000d000b,0x000e000b,
 0x000e000c,0x000e000c,0x000e000c,0x000f000c,0x000f000c,0x0010000d,0x000c0009,
 0x000b000b,0x000b000a,0x000c000a,0x000c000a,0x000d000b,0x000d000b,0x000d000b,
 0x000e000b,0x000e000c,0x000f000c,0x000f000c,0x000f000c,0x000f000c,0x0011000d,
 0x0011000d,0x000c000a,0x000b000b,0x000c000b,0x000c000b,0x000d000b,0x000d000b,
 0x000d000b,0x000e000b,0x000e000b,0x000f000b,0x000f000c,0x000f000c,0x000f000c,
 0x0010000c,0x0010000d,0x0010000d,0x000c000a,0x000c000b,0x000c000b,0x000c000b,
 0x000d000b,0x000d000b,0x000e000b,0x000e000b,0x000f000c,0x000f000c,0x000f000c,
 0x000f000c,0x0010000c,0x000f000d,0x0010000d,0x000f000d,0x000d000a,0x000c000c,
 0x000d000b,0x000c000b,0x000d000b,0x000e000b,0x000e000c,0x000e000c,0x000e000c,
 0x000f000c,0x0010000c,0x0010000c,0x0010000d,0x0011000d,0x0011000d,0x0010000d,
 0x000c000a,0x000d000c,0x000d000c,0x000d000b,0x000d000b,0x000e000b,0x000e000c,
 0x000f000c,0x0010000c,0x0010000c,0x0010000c,0x0010000c,0x0010000d,0x0010000d,
 0x000f000d,0x0010000d,0x000d000a,0x000d000c,0x000e000c,0x000e000c,0x000e000c,
 0x000e000c,0x000f000c,0x000f000c,0x000f000c,0x000f000c,0x0011000c,0x0010000d,
 0x0010000d,0x0010000d,0x0010000d,0x0012000d,0x000d000a,0x000f000c,0x000e000c,
 0x000e000c,0x000e000c,0x000f000c,0x000f000c,0x0010000c,0x0010000c,0x0010000d,
 0x0012000d,0x0011000d,0x0011000d,0x0011000d,0x0013000d,0x0011000d,0x000d000a,
 0x000e000d,0x000f000c,0x000d000c,0x000e000c,0x0010000c,0x0010000c,0x000f000c,
 0x0010000d,0x0010000d,0x0011000d,0x0012000d,0x0011000d,0x0013000d,0x0011000d,
 0x0010000d,0x000d000a,0x000a0009,0x000a0009,0x000a0009,0x000b0009,0x000b0009,
 0x000c0009,0x000c0009,0x000c0009,0x000d0009,0x000d0009,0x000d0009,0x000d000a,
 0x000d000a,0x000d000a,0x000d000a,0x000a0006};

static const uint8 t1l_const[8]  = {1,3,2,3,1,4,3,5};
static const uint8 t2l_const[9]  = {1,3,6,3,3,5,5,5,6};
static const uint8 t3l_const[9]  = {2,2,6,3,2,5,5,5,6};
static const uint8 t5l_const[16] = {1,3,6,7,3,3,6,7,6,6,7,8,7,6,7,8};
static const uint8 t6l_const[16] = {3,3,5,7,3,2,4,5,4,4,5,6,6,5,6,7};

static const uint8 t7l_const[36] =
{1,3,6,8,8,9,3,4,6,7,7,8,6,5,7,8,8,9,7,7,8,9,9,9,7,7,8,9,9,10,8,8,9,10,10,10};

static const uint8 t8l_const[36] =
{2,3,6,8,8,9,3,2,4,8,8,8,6,4,6,8,8,9,8,8,8,9,9,10,8,7,8,9,10,10,9,8,9,9,11,11};

static const uint8 t9l_const[36] =
{3,3,5,6,8,9,3,3,4,5,6,8,4,4,5,6,7,8,6,5,6,7,7,8,7,6,7,7,8,9,8,7,8,8,9,9};

static const uint8 t10l_const[64] =
{1,3,6,8,9,9,9,10,3,4,6,7,8,9,8,8,6,6,7,8,9,10,9,9,7,7,8,9,10,10,9,10,8,8,9,10,
 10,10,10,10,9,9,10,10,11,11,10,11,8,8,9,10,10,10,11,11,9,8,9,10,10,11,11,11};

static const uint8 t11l_const[64] =
{2,3,5,7,8,9,8,9,3,3,4,6,8,8,7,8,5,5,6,7,8,9,8,8,7,6,7,9,8,10,8,9,8,8,8,9,9,10,
 9,10,8,8,9,10,10,11,10,11,8,7,7,8,9,10,10,10,8,7,8,9,10,10,10,10};

static const uint8 t12l_const[64] =
{4,3,5,7,8,9,9,9,3,3,4,5,7,7,8,8,5,4,5,6,7,8,7,8,6,5,6,6,7,8,8,8,7,6,7,7,8,
 8,8,9,8,7,8,8,8,9,8,9,8,7,7,8,8,9,9,10,9,8,8,9,9,9,9,10};

static const uint8 t13l_const[256] =
{1,4,6,7,8,9,9,10,9,10,11,11,12,12,13,13,3,4,6,7,8,8,9,9,9,9,10,10,11,12,12,12,
 6,6,7,8,9,9,10,10,9,10,10,11,11,12,13,13,7,7,8,9,9,10,10,10,10,11,11,11,11,12,
 13,13,8,7,9,9,10,10,11,11,10,11,11,12,12,13,13,14,9,8,9,10,10,10,11,11,11,11,
 12,11,13,13,14,14,9,9,10,10,11,11,11,11,11,12,12,12,13,13,14,14,10,9,10,11,11,
 11,12,12,12,12,13,13,13,14,16,16,9,8,9,10,10,11,11,12,12,12,12,13,13,14,15,15,
 10,9,10,10,11,11,11,13,12,13,13,14,14,14,16,15,10,10,10,11,11,12,12,13,12,13,
 14,13,14,15,16,17,11,10,10,11,12,12,12,12,13,13,13,14,15,15,15,16,11,11,11,12,
 12,13,12,13,14,14,15,15,15,16,16,16,12,11,12,13,13,13,14,14,14,14,14,15,16,15,
 16,16,13,12,12,13,13,13,15,14,14,17,15,15,15,17,16,16,12,12,13,14,14,14,15,14,
 15,15,16,16,19,18,19,16};

static const uint8 t15l_const[256] =
{3,4,5,7,7,8,9,9,9,10,10,11,11,11,12,13,4,3,5,6,7,7,8,8,8,9,9,10,10,10,11,11,5,
 5,5,6,7,7,8,8,8,9,9,10,10,11,11,11,6,6,6,7,7,8,8,9,9,9,10,10,10,11,11,11,7,6,
 7,7,8,8,9,9,9,9,10,10,10,11,11,11,8,7,7,8,8,8,9,9,9,9,10,10,11,11,11,12,9,7,8,
 8,8,9,9,9,9,10,10,10,11,11,12,12,9,8,8,9,9,9,9,10,10,10,10,10,11,11,11,12,9,8,
 8,9,9,9,9,10,10,10,10,11,11,12,12,12,9,8,9,9,9,9,10,10,10,11,11,11,11,12,12,
 12,10,9,9,9,10,10,10,10,10,11,11,11,11,12,13,12,10,9,9,9,10,10,10,10,11,11,11,
 11,12,12,12,13,11,10,9,10,10,10,11,11,11,11,11,11,12,12,13,13,11,10,10,10,10,
 11,11,11,11,12,12,12,12,12,13,13,12,11,11,11,11,11,11,11,12,12,12,12,13,13,12,
 13,12,11,11,11,11,11,11,12,12,12,12,12,13,13,13,13};

static const uint8 t16l_const[256] =
{1,4,6,8,9,9,10,10,11,11,11,12,12,12,13,9,3,4,6,7,8,9,9,9,10,10,10,11,12,11,12,
 8,6,6,7,8,9,9,10,10,11,10,11,11,11,12,12,9,8,7,8,9,9,10,10,10,11,11,12,12,12,
 13,13,10,9,8,9,9,10,10,11,11,11,12,12,12,13,13,13,9,9,8,9,9,10,11,11,12,11,12,
 12,13,13,13,14,10,10,9,9,10,11,11,11,11,12,12,12,12,13,13,14,10,10,9,10,10,11,
 11,11,12,12,13,13,13,13,15,15,10,10,10,10,11,11,11,12,12,13,13,13,13,14,14,14,
 10,11,10,10,11,11,12,12,13,13,13,13,14,13,14,13,11,11,11,10,11,12,12,12,12,13,
 14,14,14,15,15,14,10,12,11,11,11,12,12,13,14,14,14,14,14,14,13,14,11,12,12,12,
 12,12,13,13,13,13,15,14,14,14,14,16,11,14,12,12,12,13,13,14,14,14,16,15,15,15,
 17,15,11,13,13,11,12,14,14,13,14,14,15,16,15,17,15,14,11,9,8,8,9,9,10,10,10,
 11,11,11,11,11,11,11,8};

static const uint8 t24l_const[256] =
{4,4,6,7,8,9,9,10,10,11,11,11,11,11,12,9,4,4,5,6,7,8,8,9,9,9,10,10,10,10,10,8,
 6,5,6,7,7,8,8,9,9,9,9,10,10,10,11,7,7,6,7,7,8,8,8,9,9,9,9,10,10,10,10,7,8,7,7,
 8,8,8,8,9,9,9,10,10,10,10,11,7,9,7,8,8,8,8,9,9,9,9,10,10,10,10,10,7,9,8,8,8,8,
 9,9,9,9,10,10,10,10,10,11,7,10,8,8,8,9,9,9,9,10,10,10,10,10,11,11,8,10,9,9,9,
 9,9,9,9,9,10,10,10,10,11,11,8,10,9,9,9,9,9,9,10,10,10,10,10,11,11,11,8,11,9,9,
 9,9,10,10,10,10,10,10,11,11,11,11,8,11,10,9,9,9,10,10,10,10,10,10,11,11,11,11,
 8,11,10,10,10,10,10,10,10,10,10,11,11,11,11,11,8,11,10,10,10,10,10,10,10,11,
 11,11,11,11,11,11,8,12,10,10,10,10,10,10,11,11,11,11,11,11,11,11,8,8,7,7,7,7,
 7,7,7,7,7,7,8,8,8,8,4};

static const struct huffcodetab ht_const[HTN] =
{ { 0, NULL, NULL}, /* Apparently not used */
  { 2, t1HB,  t1l},
  { 3, t2HB,  t2l},
  { 3, t3HB,  t3l},
  { 0, NULL, NULL}, /* Apparently not used */
  { 4, t5HB,  t5l},
  { 4, t6HB,  t6l},
  { 6, t7HB,  t7l},
  { 6, t8HB,  t8l},
  { 6, t9HB,  t9l},
  { 8, t10HB, t10l},
  { 8, t11HB, t11l},
  { 8, t12HB, t12l},
  {16, t13HB, t13l},
  { 0, NULL,  NULL}, /* Apparently not used */
  {16, t15HB, t15l} };

static const struct huffcodebig ht_big[HTN] =
{ { 16,  1,    1 },
  { 16,  2,    3 },
  { 16,  3,    7 },
  { 16,  4,   15 },
  { 16,  6,   63 },
  { 16,  8,  255 },
  { 16, 10, 1023 },
  { 16, 13, 8191 },
  { 16,  4,   15 },
  { 16,  5,   31 },
  { 16,  6,   63 },
  { 16,  7,  127 },
  { 16,  8,  255 },
  { 16,  9,  511 },
  { 16, 11, 2047 },
  { 16, 13, 8191 } };

static const struct
{
  uint32 region0_cnt;
  uint32 region1_cnt;
} subdv_table[23] =
{ {0, 0}, /*  0 bands */
  {0, 0}, /*  1 bands */
  {0, 0}, /*  2 bands */
  {0, 0}, /*  3 bands */
  {0, 0}, /*  4 bands */
  {0, 1}, /*  5 bands */
  {1, 1}, /*  6 bands */
  {1, 1}, /*  7 bands */
  {1, 2}, /*  8 bands */
  {2, 2}, /*  9 bands */
  {2, 3}, /* 10 bands */
  {2, 3}, /* 11 bands */
  {3, 4}, /* 12 bands */
  {3, 4}, /* 13 bands */
  {3, 4}, /* 14 bands */
  {4, 5}, /* 15 bands */
  {4, 5}, /* 16 bands */
  {4, 6}, /* 17 bands */
  {5, 6}, /* 18 bands */
  {5, 6}, /* 19 bands */
  {5, 7}, /* 20 bands */
  {6, 7}, /* 21 bands */
  {6, 7}, /* 22 bands */
};

static const uint32 sfBand[6][23] =
{
/* Table B.2.b: 22.05 kHz */
{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
/* Table B.2.c: 24 kHz */
{0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,330,394,464,540,576},
/* Table B.2.a: 16 kHz */
{0,6,12,18,24,30,36,44,45,66,80,96,116,140,168,200,238,248,336,396,464,522,576},
/* Table B.8.b: 44.1 kHz */
{0,4, 8,12,16,20,24,30,36,44,52,62, 74, 90,110,134,162,196,238,288,342,418,576},
/* Table B.8.c: 48 kHz */
{0,4, 8,12,16,20,24,30,36,42,50,60, 72, 88,106,128,156,190,230,276,330,384,576},
/* Table B.8.a: 32 kHz */
{0,4, 8,12,16,20,24,30,36,44,54,66, 82,102,126,156,194,240,296,364,448,550,576} };


static const short int2idx_const[4096] = /*  int2idx[i] = sqrt(i*sqrt(i)); */
{
  0,  1,  2,  2,  3,  3,  4,  4,  5,  5,  6,  6,  6,  7,  7,  8,  8,  8,  9,  9,
  9, 10, 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16,
 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21,
 22, 22, 22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26,
 27, 27, 27, 27, 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31,
 32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 34, 35, 35, 35, 35, 36, 36, 36,
 36, 36, 37, 37, 37, 37, 38, 38, 38, 38, 38, 39, 39, 39, 39, 40, 40, 40, 40, 40,
 41, 41, 41, 41, 42, 42, 42, 42, 42, 43, 43, 43, 43, 44, 44, 44, 44, 44, 45, 45,
 45, 45, 45, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 48, 48, 48, 48, 49, 49, 49,
 49, 49, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 52, 52, 52, 52, 52, 53, 53, 53,
 53, 53, 54, 54, 54, 54, 54, 55, 55, 55, 55, 55, 56, 56, 56, 56, 56, 57, 57, 57,
 57, 57, 58, 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 60, 60, 60, 60, 60, 61, 61,
 61, 61, 61, 62, 62, 62, 62, 62, 62, 63, 63, 63, 63, 63, 64, 64, 64, 64, 64, 65,
 65, 65, 65, 65, 65, 66, 66, 66, 66, 66, 67, 67, 67, 67, 67, 68, 68, 68, 68, 68,
 68, 69, 69, 69, 69, 69, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 72, 72, 72,
 72, 72, 72, 73, 73, 73, 73, 73, 74, 74, 74, 74, 74, 74, 75, 75, 75, 75, 75, 75,
 76, 76, 76, 76, 76, 77, 77, 77, 77, 77, 77, 78, 78, 78, 78, 78, 78, 79, 79, 79,
 79, 79, 80, 80, 80, 80, 80, 80, 81, 81, 81, 81, 81, 81, 82, 82, 82, 82, 82, 82,
 83, 83, 83, 83, 83, 84, 84, 84, 84, 84, 84, 85, 85, 85, 85, 85, 85, 86, 86, 86,
 86, 86, 86, 87, 87, 87, 87, 87, 87, 88, 88, 88, 88, 88, 88, 89, 89, 89, 89, 89,
 89, 90, 90, 90, 90, 90, 90, 91, 91, 91, 91, 91, 91, 92, 92, 92, 92, 92, 92, 93,
 93, 93, 93, 93, 93, 94, 94, 94, 94, 94, 94, 95, 95, 95, 95, 95, 95, 96, 96, 96,
 96, 96, 96, 97, 97, 97, 97, 97, 97, 98, 98, 98, 98, 98, 98, 99, 99, 99, 99, 99,
 99, 99,100,100,100,100,100,100,101,101,101,101,101,101,102,102,102,102,102,102,
103,103,103,103,103,103,104,104,104,104,104,104,104,105,105,105,105,105,105,106,
106,106,106,106,106,107,107,107,107,107,107,107,108,108,108,108,108,108,109,109,
109,109,109,109,110,110,110,110,110,110,110,111,111,111,111,111,111,112,112,112,
112,112,112,112,113,113,113,113,113,113,114,114,114,114,114,114,115,115,115,115,
115,115,115,116,116,116,116,116,116,117,117,117,117,117,117,117,118,118,118,118,
118,118,118,119,119,119,119,119,119,120,120,120,120,120,120,120,121,121,121,121,
121,121,122,122,122,122,122,122,122,123,123,123,123,123,123,123,124,124,124,124,
124,124,125,125,125,125,125,125,125,126,126,126,126,126,126,126,127,127,127,127,
127,127,128,128,128,128,128,128,128,129,129,129,129,129,129,129,130,130,130,130,
130,130,131,131,131,131,131,131,131,132,132,132,132,132,132,132,133,133,133,133,
133,133,133,134,134,134,134,134,134,134,135,135,135,135,135,135,136,136,136,136,
136,136,136,137,137,137,137,137,137,137,138,138,138,138,138,138,138,139,139,139,
139,139,139,139,140,140,140,140,140,140,140,141,141,141,141,141,141,141,142,142,
142,142,142,142,142,143,143,143,143,143,143,143,144,144,144,144,144,144,144,145,
145,145,145,145,145,145,146,146,146,146,146,146,146,147,147,147,147,147,147,147,
148,148,148,148,148,148,148,149,149,149,149,149,149,149,150,150,150,150,150,150,
150,151,151,151,151,151,151,151,152,152,152,152,152,152,152,153,153,153,153,153,
153,153,154,154,154,154,154,154,154,154,155,155,155,155,155,155,155,156,156,156,
156,156,156,156,157,157,157,157,157,157,157,158,158,158,158,158,158,158,159,159,
159,159,159,159,159,160,160,160,160,160,160,160,160,161,161,161,161,161,161,161,
162,162,162,162,162,162,162,163,163,163,163,163,163,163,163,164,164,164,164,164,
164,164,165,165,165,165,165,165,165,166,166,166,166,166,166,166,167,167,167,167,
167,167,167,167,168,168,168,168,168,168,168,169,169,169,169,169,169,169,169,170,
170,170,170,170,170,170,171,171,171,171,171,171,171,172,172,172,172,172,172,172,
172,173,173,173,173,173,173,173,174,174,174,174,174,174,174,174,175,175,175,175,
175,175,175,176,176,176,176,176,176,176,176,177,177,177,177,177,177,177,178,178,
178,178,178,178,178,178,179,179,179,179,179,179,179,180,180,180,180,180,180,180,
180,181,181,181,181,181,181,181,182,182,182,182,182,182,182,182,183,183,183,183,
183,183,183,184,184,184,184,184,184,184,184,185,185,185,185,185,185,185,186,186,
186,186,186,186,186,186,187,187,187,187,187,187,187,187,188,188,188,188,188,188,
188,189,189,189,189,189,189,189,189,190,190,190,190,190,190,190,190,191,191,191,
191,191,191,191,192,192,192,192,192,192,192,192,193,193,193,193,193,193,193,193,
194,194,194,194,194,194,194,195,195,195,195,195,195,195,195,196,196,196,196,196,
196,196,196,197,197,197,197,197,197,197,197,198,198,198,198,198,198,198,199,199,
199,199,199,199,199,199,200,200,200,200,200,200,200,200,201,201,201,201,201,201,
201,201,202,202,202,202,202,202,202,202,203,203,203,203,203,203,203,204,204,204,
204,204,204,204,204,205,205,205,205,205,205,205,205,206,206,206,206,206,206,206,
206,207,207,207,207,207,207,207,207,208,208,208,208,208,208,208,208,209,209,209,
209,209,209,209,209,210,210,210,210,210,210,210,210,211,211,211,211,211,211,211,
211,212,212,212,212,212,212,212,212,213,213,213,213,213,213,213,213,214,214,214,
214,214,214,214,214,215,215,215,215,215,215,215,215,216,216,216,216,216,216,216,
216,217,217,217,217,217,217,217,217,218,218,218,218,218,218,218,218,219,219,219,
219,219,219,219,219,220,220,220,220,220,220,220,220,221,221,221,221,221,221,221,
221,222,222,222,222,222,222,222,222,223,223,223,223,223,223,223,223,224,224,224,
224,224,224,224,224,225,225,225,225,225,225,225,225,226,226,226,226,226,226,226,
226,227,227,227,227,227,227,227,227,228,228,228,228,228,228,228,228,229,229,229,
229,229,229,229,229,229,230,230,230,230,230,230,230,230,231,231,231,231,231,231,
231,231,232,232,232,232,232,232,232,232,233,233,233,233,233,233,233,233,234,234,
234,234,234,234,234,234,234,235,235,235,235,235,235,235,235,236,236,236,236,236,
236,236,236,237,237,237,237,237,237,237,237,238,238,238,238,238,238,238,238,238,
239,239,239,239,239,239,239,239,240,240,240,240,240,240,240,240,241,241,241,241,
241,241,241,241,242,242,242,242,242,242,242,242,242,243,243,243,243,243,243,243,
243,244,244,244,244,244,244,244,244,245,245,245,245,245,245,245,245,245,246,246,
246,246,246,246,246,246,247,247,247,247,247,247,247,247,248,248,248,248,248,248,
248,248,248,249,249,249,249,249,249,249,249,250,250,250,250,250,250,250,250,250,
251,251,251,251,251,251,251,251,252,252,252,252,252,252,252,252,253,253,253,253,
253,253,253,253,253,254,254,254,254,254,254,254,254,255,255,255,255,255,255,255,
255,255,256,256,256,256,256,256,256,256,257,257,257,257,257,257,257,257,257,258,
258,258,258,258,258,258,258,259,259,259,259,259,259,259,259,259,260,260,260,260,
260,260,260,260,261,261,261,261,261,261,261,261,261,262,262,262,262,262,262,262,
262,263,263,263,263,263,263,263,263,263,264,264,264,264,264,264,264,264,265,265,
265,265,265,265,265,265,265,266,266,266,266,266,266,266,266,267,267,267,267,267,
267,267,267,267,268,268,268,268,268,268,268,268,268,269,269,269,269,269,269,269,
269,270,270,270,270,270,270,270,270,270,271,271,271,271,271,271,271,271,271,272,
272,272,272,272,272,272,272,273,273,273,273,273,273,273,273,273,274,274,274,274,
274,274,274,274,275,275,275,275,275,275,275,275,275,276,276,276,276,276,276,276,
276,276,277,277,277,277,277,277,277,277,277,278,278,278,278,278,278,278,278,279,
279,279,279,279,279,279,279,279,280,280,280,280,280,280,280,280,280,281,281,281,
281,281,281,281,281,282,282,282,282,282,282,282,282,282,283,283,283,283,283,283,
283,283,283,284,284,284,284,284,284,284,284,284,285,285,285,285,285,285,285,285,
286,286,286,286,286,286,286,286,286,287,287,287,287,287,287,287,287,287,288,288,
288,288,288,288,288,288,288,289,289,289,289,289,289,289,289,289,290,290,290,290,
290,290,290,290,291,291,291,291,291,291,291,291,291,292,292,292,292,292,292,292,
292,292,293,293,293,293,293,293,293,293,293,294,294,294,294,294,294,294,294,294,
295,295,295,295,295,295,295,295,295,296,296,296,296,296,296,296,296,296,297,297,
297,297,297,297,297,297,297,298,298,298,298,298,298,298,298,299,299,299,299,299,
299,299,299,299,300,300,300,300,300,300,300,300,300,301,301,301,301,301,301,301,
301,301,302,302,302,302,302,302,302,302,302,303,303,303,303,303,303,303,303,303,
304,304,304,304,304,304,304,304,304,305,305,305,305,305,305,305,305,305,306,306,
306,306,306,306,306,306,306,307,307,307,307,307,307,307,307,307,308,308,308,308,
308,308,308,308,308,309,309,309,309,309,309,309,309,309,310,310,310,310,310,310,
310,310,310,311,311,311,311,311,311,311,311,311,312,312,312,312,312,312,312,312,
312,313,313,313,313,313,313,313,313,313,314,314,314,314,314,314,314,314,314,315,
315,315,315,315,315,315,315,315,316,316,316,316,316,316,316,316,316,317,317,317,
317,317,317,317,317,317,318,318,318,318,318,318,318,318,318,318,319,319,319,319,
319,319,319,319,319,320,320,320,320,320,320,320,320,320,321,321,321,321,321,321,
321,321,321,322,322,322,322,322,322,322,322,322,323,323,323,323,323,323,323,323,
323,324,324,324,324,324,324,324,324,324,325,325,325,325,325,325,325,325,325,325,
326,326,326,326,326,326,326,326,326,327,327,327,327,327,327,327,327,327,328,328,
328,328,328,328,328,328,328,329,329,329,329,329,329,329,329,329,330,330,330,330,
330,330,330,330,330,330,331,331,331,331,331,331,331,331,331,332,332,332,332,332,
332,332,332,332,333,333,333,333,333,333,333,333,333,334,334,334,334,334,334,334,
334,334,335,335,335,335,335,335,335,335,335,335,336,336,336,336,336,336,336,336,
336,337,337,337,337,337,337,337,337,337,338,338,338,338,338,338,338,338,338,338,
339,339,339,339,339,339,339,339,339,340,340,340,340,340,340,340,340,340,341,341,
341,341,341,341,341,341,341,341,342,342,342,342,342,342,342,342,342,343,343,343,
343,343,343,343,343,343,344,344,344,344,344,344,344,344,344,344,345,345,345,345,
345,345,345,345,345,346,346,346,346,346,346,346,346,346,347,347,347,347,347,347,
347,347,347,347,348,348,348,348,348,348,348,348,348,349,349,349,349,349,349,349,
349,349,350,350,350,350,350,350,350,350,350,350,351,351,351,351,351,351,351,351,
351,352,352,352,352,352,352,352,352,352,352,353,353,353,353,353,353,353,353,353,
354,354,354,354,354,354,354,354,354,355,355,355,355,355,355,355,355,355,355,356,
356,356,356,356,356,356,356,356,357,357,357,357,357,357,357,357,357,357,358,358,
358,358,358,358,358,358,358,359,359,359,359,359,359,359,359,359,359,360,360,360,
360,360,360,360,360,360,361,361,361,361,361,361,361,361,361,361,362,362,362,362,
362,362,362,362,362,363,363,363,363,363,363,363,363,363,363,364,364,364,364,364,
364,364,364,364,365,365,365,365,365,365,365,365,365,365,366,366,366,366,366,366,
366,366,366,367,367,367,367,367,367,367,367,367,367,368,368,368,368,368,368,368,
368,368,369,369,369,369,369,369,369,369,369,369,370,370,370,370,370,370,370,370,
370,370,371,371,371,371,371,371,371,371,371,372,372,372,372,372,372,372,372,372,
372,373,373,373,373,373,373,373,373,373,374,374,374,374,374,374,374,374,374,374,
375,375,375,375,375,375,375,375,375,375,376,376,376,376,376,376,376,376,376,377,
377,377,377,377,377,377,377,377,377,378,378,378,378,378,378,378,378,378,379,379,
379,379,379,379,379,379,379,379,380,380,380,380,380,380,380,380,380,380,381,381,
381,381,381,381,381,381,381,382,382,382,382,382,382,382,382,382,382,383,383,383,
383,383,383,383,383,383,383,384,384,384,384,384,384,384,384,384,385,385,385,385,
385,385,385,385,385,385,386,386,386,386,386,386,386,386,386,386,387,387,387,387,
387,387,387,387,387,387,388,388,388,388,388,388,388,388,388,389,389,389,389,389,
389,389,389,389,389,390,390,390,390,390,390,390,390,390,390,391,391,391,391,391,
391,391,391,391,391,392,392,392,392,392,392,392,392,392,393,393,393,393,393,393,
393,393,393,393,394,394,394,394,394,394,394,394,394,394,395,395,395,395,395,395,
395,395,395,395,396,396,396,396,396,396,396,396,396,397,397,397,397,397,397,397,
397,397,397,398,398,398,398,398,398,398,398,398,398,399,399,399,399,399,399,399,
399,399,399,400,400,400,400,400,400,400,400,400,400,401,401,401,401,401,401,401,
401,401,402,402,402,402,402,402,402,402,402,402,403,403,403,403,403,403,403,403,
403,403,404,404,404,404,404,404,404,404,404,404,405,405,405,405,405,405,405,405,
405,405,406,406,406,406,406,406,406,406,406,406,407,407,407,407,407,407,407,407,
407,407,408,408,408,408,408,408,408,408,408,408,409,409,409,409,409,409,409,409,
409,410,410,410,410,410,410,410,410,410,410,411,411,411,411,411,411,411,411,411,
411,412,412,412,412,412,412,412,412,412,412,413,413,413,413,413,413,413,413,413,
413,414,414,414,414,414,414,414,414,414,414,415,415,415,415,415,415,415,415,415,
415,416,416,416,416,416,416,416,416,416,416,417,417,417,417,417,417,417,417,417,
417,418,418,418,418,418,418,418,418,418,418,419,419,419,419,419,419,419,419,419,
419,420,420,420,420,420,420,420,420,420,420,421,421,421,421,421,421,421,421,421,
421,422,422,422,422,422,422,422,422,422,422,423,423,423,423,423,423,423,423,423,
423,424,424,424,424,424,424,424,424,424,424,425,425,425,425,425,425,425,425,425,
425,426,426,426,426,426,426,426,426,426,426,427,427,427,427,427,427,427,427,427,
427,428,428,428,428,428,428,428,428,428,428,429,429,429,429,429,429,429,429,429,
429,430,430,430,430,430,430,430,430,430,430,431,431,431,431,431,431,431,431,431,
431,432,432,432,432,432,432,432,432,432,432,433,433,433,433,433,433,433,433,433,
433,434,434,434,434,434,434,434,434,434,434,435,435,435,435,435,435,435,435,435,
435,435,436,436,436,436,436,436,436,436,436,436,437,437,437,437,437,437,437,437,
437,437,438,438,438,438,438,438,438,438,438,438,439,439,439,439,439,439,439,439,
439,439,440,440,440,440,440,440,440,440,440,440,441,441,441,441,441,441,441,441,
441,441,442,442,442,442,442,442,442,442,442,442,443,443,443,443,443,443,443,443,
443,443,443,444,444,444,444,444,444,444,444,444,444,445,445,445,445,445,445,445,
445,445,445,446,446,446,446,446,446,446,446,446,446,447,447,447,447,447,447,447,
447,447,447,448,448,448,448,448,448,448,448,448,448,448,449,449,449,449,449,449,
449,449,449,449,450,450,450,450,450,450,450,450,450,450,451,451,451,451,451,451,
451,451,451,451,452,452,452,452,452,452,452,452,452,452,453,453,453,453,453,453,
453,453,453,453,453,454,454,454,454,454,454,454,454,454,454,455,455,455,455,455,
455,455,455,455,455,456,456,456,456,456,456,456,456,456,456,457,457,457,457,457,
457,457,457,457,457,457,458,458,458,458,458,458,458,458,458,458,459,459,459,459,
459,459,459,459,459,459,460,460,460,460,460,460,460,460,460,460,460,461,461,461,
461,461,461,461,461,461,461,462,462,462,462,462,462,462,462,462,462,463,463,463,
463,463,463,463,463,463,463,463,464,464,464,464,464,464,464,464,464,464,465,465,
465,465,465,465,465,465,465,465,466,466,466,466,466,466,466,466,466,466,466,467,
467,467,467,467,467,467,467,467,467,468,468,468,468,468,468,468,468,468,468,469,
469,469,469,469,469,469,469,469,469,469,470,470,470,470,470,470,470,470,470,470,
471,471,471,471,471,471,471,471,471,471,472,472,472,472,472,472,472,472,472,472,
472,473,473,473,473,473,473,473,473,473,473,474,474,474,474,474,474,474,474,474,
474,475,475,475,475,475,475,475,475,475,475,475,476,476,476,476,476,476,476,476,
476,476,477,477,477,477,477,477,477,477,477,477,477,478,478,478,478,478,478,478,
478,478,478,479,479,479,479,479,479,479,479,479,479,479,480,480,480,480,480,480,
480,480,480,480,481,481,481,481,481,481,481,481,481,481,482,482,482,482,482,482,
482,482,482,482,482,483,483,483,483,483,483,483,483,483,483,484,484,484,484,484,
484,484,484,484,484,484,485,485,485,485,485,485,485,485,485,485,486,486,486,486,
486,486,486,486,486,486,486,487,487,487,487,487,487,487,487,487,487,488,488,488,
488,488,488,488,488,488,488,488,489,489,489,489,489,489,489,489,489,489,490,490,
490,490,490,490,490,490,490,490,490,491,491,491,491,491,491,491,491,491,491,492,
492,492,492,492,492,492,492,492,492,492,493,493,493,493,493,493,493,493,493,493,
494,494,494,494,494,494,494,494,494,494,494,495,495,495,495,495,495,495,495,495,
495,496,496,496,496,496,496,496,496,496,496,496,497,497,497,497,497,497,497,497,
497,497,497,498,498,498,498,498,498,498,498,498,498,499,499,499,499,499,499,499,
499,499,499,499,500,500,500,500,500,500,500,500,500,500,501,501,501,501,501,501,
501,501,501,501,501,502,502,502,502,502,502,502,502,502,502,503,503,503,503,503,
503,503,503,503,503,503,504,504,504,504,504,504,504,504,504,504,504,505,505,505,
505,505,505,505,505,505,505,506,506,506,506,506,506,506,506,506,506,506,507,507,
507,507,507,507,507,507,507,507,507,508,508,508,508,508,508,508,508,508,508,509,
509,509,509,509,509,509,509,509,509,509,510,510,510,510,510,510,510,510,510,510,
510,511,511,511,511,511,511,511,511,511,511,512,512,512,512,512 };

static const int order[32] = 
{ 0, 1, 16, 17, 8, 9, 24, 25, 4, 5, 20, 21, 12, 13, 28, 29,
  2, 3, 18, 19,10,11, 26, 27, 6, 7, 22, 23, 14, 15, 30, 31 };

static const int bitr_index[2][15] =
{ {0, 8,16,24,32,40,48,56, 64, 80, 96,112,128,144,160},
  {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320} };

static const int num_bands[3][15]  =
{ {0,10,10,10,10,12,14,16, 20, 22, 24, 26, 28, 30, 32},
  {0,10,10,10,10,10,12,14, 18, 24, 26, 28, 30, 32, 32},
  {0,10,12,14,18,24,26,28, 30, 32, 32, 32, 32, 32, 32} };

static const int cx_const[9] =
{ 16135, 10531,  5604, 15396, -2845,-12551, 14189,  8192, 16384 };

static const int ca_const[8] =
{-16859,-15458,-10269, -5961, -3099, -1342,  -465,  -121 };

static const int cs_const[8] =
{ 28098, 28893, 31117, 32221, 32621, 32740, 32765, 32768 };

static const short enwindow_const[15*27+24] = 
{ 0,   65,  593,  1766, 22228, 2115, 611, 62,
  8,  119, 1419, 10564,-11659,-1635,-154, -9,
 -8, -119,-1419,-10564, 11659, 1635, 154,  9, 464,  100,  91,
  0,   69,  604,  1635, 23148, 2363, 643, 62,
  7,  107, 1368, 10449,-12733,-1818,-180,-11,
 -7, -107,-1368,-10449, 12733, 1818, 180, 11, 420,  200, 164,
  0,   72,  608,  1465, 23979, 2600, 671, 63,
  7,   94, 1305, 10265,-13818,-2004,-207,-12,
 -7,  -94,-1305,-10265, 13818, 2004, 207, 12, 380,  297, 220,
  0,   76,  606,  1256, 24718, 2825, 693, 63,
  6,   81, 1232, 10016,-14908,-2192,-236,-14,
 -6,  -81,-1232,-10016, 14908, 2192, 236, 14, 342,  392, 262,
  0,   78,  597,  1007, 25359, 3033, 712, 63,
  6,   68, 1150,  9706,-15995,-2380,-267,-15,
 -6,  -68,-1150, -9706, 15995, 2380, 267, 15, 307,  483, 289,
  0,   80,  580,   719, 25901, 3224, 726, 62,
  6,   54, 1060,  9343,-17072,-2565,-299,-17,
 -6,  -54,-1060, -9343, 17072, 2565, 299, 17, 274,  569, 304,
 -1,   82,  555,   391, 26339, 3395, 735, 61,
  5,   40,  963,  8930,-18131,-2747,-332,-19,
 -5,  -40, -963, -8930, 18131, 2747, 332, 19, 242,  650, 307,
 -1,   83,  523,    26, 26672, 3545, 740, 60,
  5,   27,  861,  8474,-19164,-2923,-366,-21,
 -5,  -27, -861, -8474, 19164, 2923, 366, 21, 212,  724, 300,
 -1,   83,  482,  -376, 26900, 3672, 739, 58,
  4,   14,  756,  7981,-20163,-3092,-401,-24,
 -4,  -14, -756, -7981, 20163, 3092, 401, 24, 183,  792, 283,
 -1,   82,  433,  -812, 27022, 3776, 735, 56,
  4,    1,  648,  7456,-21122,-3250,-435,-26,
 -4,   -1, -648, -7456, 21122, 3250, 435, 26, 155,  851, 258,
 -1,   81,  376, -1281, 27038, 3855, 726, 54,
  3,  -11,  539,  6907,-22032,-3397,-470,-28,
 -3,   11, -539, -6907, 22032, 3397, 470, 28, 128,  903, 226,
 -1,   78,  312, -1778, 26951, 3910, 713, 52,
  3,  -22,  430,  6338,-22887,-3530,-503,-31,
 -3,   22, -430, -6338, 22887, 3530, 503, 31, 102,  946, 188,
 -2,   75,  239, -2302, 26761, 3941, 696, 49,
  3,  -33,  322,  5757,-23678,-3648,-537,-34,
 -3,   33, -322, -5757, 23678, 3648, 537, 34,  76,  980, 145,
 -2,   70,  160, -2848, 26472, 3948, 676, 47,
  3,  -42,  217,  5167,-24399,-3749,-568,-36,
 -3,   42, -217, -5167, 24399, 3749, 568, 36,  50, 1004,  99,
 -2,   65,   74, -3412, 26087, 3931, 653, 44,
  2,  -51,  115,  4577,-25045,-3830,-599,-39,
 -2,   51, -115, -4577, 25045, 3830, 599, 39,  25, 1019,  50,

 25610,3891,627,42,-3990,-18,58,-2,
 21226,-21226,10604,-10604,1860,-1860,1458,-1458,576,-576,130,-130,60,-60,8,-8
};

static const int win_const[18][4] = {
  { -3072, -134, -146, 3352 },
  { -2747, -362, -471, 3579 },
  { -2387, -529, -831, 3747 },
  { -2004, -632,-1214, 3850 },
  { -1609, -666,-1609, 3884 },
  { -1214, -632,-2004, 3850 },
  {  -831, -529,-2387, 3747 },
  {  -471, -362,-2747, 3579 },
  {  -146, -134,-3072, 3352 },
  {   134,-3072,-3352, -146 },
  {   362,-2747,-3579, -471 },
  {   529,-2387,-3747, -831 },
  {   632,-2004,-3850,-1214 },
  {   666,-1609,-3884,-1609 },
  {   632,-1214,-3850,-2004 },
  {   529, -831,-3747,-2387 },
  {   362, -471,-3579,-2747 },
  {   134, -146,-3352,-3072 } };


static char*       wav_filename;
static int         mp3file, wavfile, wav_size, frames;
static uint32      enc_buffer[16384]; /* storage for 65536 Bytes */
static int         enc_chunk = 0;     /* encode chunk counter    */
static int         enc_size;
static config_t    cfg;

/* forward declarations */
int  HuffmanCode( short *ix, int *xr, uint32 begin, uint32 end, int table);
int  HuffmanCod1( short *ix, int *xr, uint32 begin, uint32 end, int table);
void putbits(uint32 val, uint32 nbit);
int  find_best_2( short *ix, uint32 start, uint32 end, const uint32 *table,
                  uint32 len, int *bits);
int  find_best_3( short *ix, uint32 start, uint32 end, const uint32 *table,
                  uint32 len, int *bits);
int  count_bit1 ( short *ix, uint32 start, uint32 end, int *bits );
int  count_bigv ( short *ix, uint32 start, uint32 end, int table0, int table1,
                  int *bits);


bool checkString(int fd, char *string)
{
  char temp[4];

  rb->read(fd, temp, 4);

  return (*(long*)temp == *(long*)string) ? 1 : 0;
}

int Read16BitsLowHigh(int fd)
{
  char first, second;

  rb->read(fd, &first,  1);
  rb->read(fd, &second, 1);

  return ((int)second << 8) | (first & 0xff);
}


int Read32BitsLowHigh(int fd)
{
  int first  = 0xffff & Read16BitsLowHigh(fd);
  int second = 0xffff & Read16BitsLowHigh(fd);

  return (second << 16) + first;
}

int wave_open(void)
{
  unsigned short  wFormatTag;
  unsigned long   dAvgBytesPerSec;
  unsigned short  wBlockAlign;
  unsigned short  bits_per_samp;
  long            header_size;

  if((wavfile = rb->open(wav_filename, O_RDONLY)) < 0)
    return -1;
  
  if(!checkString(wavfile,"RIFF")) return -2;
  Read32BitsLowHigh(wavfile); /* complete wave chunk size */
  if(!checkString(wavfile,"WAVE")) return -3;
  if(!checkString(wavfile,"fmt ")) return -4;
  
  header_size = Read32BitsLowHigh(wavfile); /* chunk size */
  wFormatTag  = Read16BitsLowHigh(wavfile);
  
  cfg.channels    = Read16BitsLowHigh(wavfile);
  cfg.samplerate  = Read32BitsLowHigh(wavfile);
  dAvgBytesPerSec = Read32BitsLowHigh(wavfile);
  wBlockAlign     = Read16BitsLowHigh(wavfile);
  bits_per_samp   = Read16BitsLowHigh(wavfile);
  
  if(wFormatTag != 0x0001)         return -5;
  if(bits_per_samp != 16)          return -6;
  if(cfg.channels > 2)             return -7;
  if(!checkString(wavfile,"data")) return -8;
  
  header_size = 0x28;
  wav_size = rb->filesize(wavfile);
  rb->lseek(wavfile, header_size, SEEK_SET);
  
  return 0;
}

int read_samples(uint32 *buffer, int num_samples)
{
  int s, samples = rb->read(wavfile, buffer, 4 * num_samples) / 4;
  /* Pad last sample with zeros */
  for(s=samples; s<num_samples; s++)
    buffer[s] = 0;

  return samples;
}

inline uint32 myswap32(uint32 val)
{
  const uint8* v = (const uint8*)&val;

  return ((uint32)v[0]<<24) | ((uint32)v[1]<<16) | ((uint32)v[2]<<8) | v[3];
}

void encodeSideInfo( side_info_t si[2][2] )
{
  int gr, ch, header;
  uint32  cc=0, sz=0;
  
  header  = 0xfff00000;
  header |= cfg.mpg.type    << 19; /* mp3 type: 1  */
  header |= 1               << 17; /* mp3 layer: 1 */
  header |= 1               << 16; /* mp3 crc: 0   */
  header |= cfg.mpg.bitr_id << 12;
  header |= cfg.mpg.smpl_id << 10;
  header |= cfg.mpg.padding <<  9;
  header |= cfg.mpg.mode    <<  6;
  header |= 1               <<  2; /* mp3 original: 1 */
  putbits( header, 32 );

  if(cfg.mpg.type)
  { /* MPEG1 */
    if(cfg.channels == 2)  { putlong( 0, 20); }
    else                   { putlong( 0, 18); }

    for(gr=0; gr<cfg.granules; gr++)
      for(ch=0; ch<cfg.channels; ch++)
      {
        side_info_t *gi = &si[gr][ch];

        putlong( gi->part2_3_length,  12 );
        putlong( gi->address3>>1,      9 );
        putlong( gi->global_gain,      8 );
        putlong( gi->table_select[0], 10 );
        putlong( gi->table_select[1],  5 );
        putlong( gi->table_select[2],  5 );
        putlong( gi->region_0_1,       7 );
        putlong( gi->table_select[3],  3 );
      }
  }
  else
  { /* MPEG2 */
    if(cfg.channels == 2)  { putlong( 0, 10); }
    else                   { putlong( 0,  9); }

    for(ch=0; ch<cfg.channels; ch++)
    {
      side_info_t *gi = &si[0][ch];

      putlong( gi->part2_3_length,    12);
      putlong( gi->address3>>1,        9);
      putlong( gi->global_gain,        8);
      putlong( gi->table_select[0],   15);
      putlong( gi->table_select[1],    5);
      putlong( gi->table_select[2],    5);
      putlong( gi->region_0_1,         7);
      putlong( gi->table_select[3],    2);
    }
  }
  /* flush remaining bits */
  putbits(cc, sz);
}

/* Note the discussion of huffmancodebits() on pages 28 and 29 of the IS,
   as well as the definitions of the side information on pages 26 and 27. */
void Huffmancodebits( short *ix, int *xr, side_info_t *gi )
{
  int    region1   = gi->address1;
  int    region2   = gi->address2;
  int    bigvals   = gi->address3;
  int    count1    = bigvals + (gi->count1 << 2);
  int    stuffBits = 0;
  int    bits      = 0;

  if(region1 > 0)
    bits += HuffmanCode(ix, xr,   0    , region1, gi->table_select[0]);

  if(region2 > region1)
    bits += HuffmanCode(ix, xr, region1, region2, gi->table_select[1]);

  if(bigvals > region2)
    bits += HuffmanCode(ix, xr, region2, bigvals, gi->table_select[2]);
 
  if(count1 > bigvals)
    bits += HuffmanCod1(ix, xr, bigvals,  count1, gi->table_select[3]);

  if((stuffBits = gi->part2_3_length - bits) > 0)
  {
    int stuffWords = stuffBits >> 5;
    int remainBits = stuffBits & 31;

    if( remainBits )
      putbits( ~0, remainBits );

    while( stuffWords-- )
      putbits( ~0, 32 ); /* Huffman code tables leed to padding ones */
  }
}

int HuffmanCod1( short *ix, int *xr, uint32 begin, uint32 end, int tbl)
{
  uint32  cc=0, sz=0;
  uint32  i, d, p;
  int     sumbit=0, s=0, l=0, v, w, x, y;
  #define sgnv (xr[i+0] < 0 ? 1 : 0)
  #define sgnw (xr[i+1] < 0 ? 1 : 0)
  #define sgnx (xr[i+2] < 0 ? 1 : 0)
  #define sgny (xr[i+3] < 0 ? 1 : 0)

  for(i=begin; i<end; i+=4)
  {
    v = ix[i+0];
    w = ix[i+1];
    x = ix[i+2];
    y = ix[i+3];
    p = (v << 3) + (w << 2) + (x << 1) + y;

    switch(p)
    {
      case  0: l=0; s = 0; break;
      case  1: l=1; s = sgnv; break;
      case  2: l=1; s =               sgnw; break;
      case  3: l=2; s = (sgnv << 1) + sgnw; break;
      case  4: l=1; s =                             sgnx; break;
      case  5: l=2; s = (sgnv << 1)               + sgnx; break;
      case  6: l=2; s =               (sgnw << 1) + sgnx; break;
      case  7: l=3; s = (sgnv << 2) + (sgnw << 1) + sgnx; break;
      case  8: l=1; s =                                           sgny; break;
      case  9: l=2; s = (sgnv << 1)                             + sgny; break;
      case 10: l=2; s =               (sgnw << 1)               + sgny; break;
      case 11: l=3; s = (sgnv << 2) + (sgnw << 1)               + sgny; break;
      case 12: l=2; s =                             (sgnx << 1) + sgny; break;
      case 13: l=3; s = (sgnv << 2)               + (sgnx << 1) + sgny; break;
      case 14: l=3; s =               (sgnw << 2) + (sgnx << 1) + sgny; break;
      case 15: l=4; s = (sgnv << 3) + (sgnw << 2) + (sgnx << 1) + sgny; break;
    }

    d = (ht_count[tbl][0][p] << l) + s;
    l =  ht_count[tbl][1][p];
    putlong( d, l );
    sumbit += l;
  }

  /* flush remaining bits */
  putbits(cc, sz);

  return sumbit;
}

/* Implements the pseudocode of page 98 of the IS */
int HuffmanCode( short *ix, int *xr, uint32 begin, uint32 end, int table)
{
  uint32       cc=0, sz=0, code;
  uint32       i, xl=0, yl=0, idx;
  int          x, y, bit, sumbit=0;
  #define sign_x (xr[i+0] < 0 ? 1 : 0)
  #define sign_y (xr[i+1] < 0 ? 1 : 0)

  if(table == 0)
    return 0;

  if( table > 15 )
  { /* ESC-table is used */
    uint32 linbits  = ht_big[table-16].linbits;
    uint16 *hffcode = table < 24 ? t16HB : t24HB;
    uint8  *hlen    = table < 24 ? t16l  : t24l;

    for(i=begin; i<end; i+=2)
    {
      x = ix[ i ];
      y = ix[i+1];

      if(x > 14) { xl = x - 15;  x = 15; }
      if(y > 14) { yl = y - 15;  y = 15; }

      idx  = x * 16 + y;
      code = hffcode[idx];
      bit  = hlen   [idx];

      if(x)
      {
        if(x > 14)
        {
          code = (code << linbits) | xl;
          bit += linbits;
        }

        code = (code << 1) | sign_x;
        bit += 1;
      }

      if(y)
      {
        if(y > 14)
        {
          code = (code << linbits) | yl;
          bit += linbits;
        }

        code = (code << 1) | sign_y;
        bit += 1;
      }

      putlong( code, bit );
      sumbit += bit;
    }
  }
  else
  { /* No ESC-words */
    const struct huffcodetab *h = &ht[table];

    for(i=begin; i<end; i+=2)
    {
      x = ix[i];
      y = ix[i+1];

      idx  = x * h->len + y;
      code = h->table[idx];
      bit  = h->hlen [idx];

      if(x)
      {
        code = (code << 1) | sign_x;
        bit += 1;
      }

      if(y)
      {
        code = (code << 1) | sign_y;
        bit += 1;
      }

      putlong( code, bit );
      sumbit += bit;
    }
  }

  /* flush remaining bits */
  putbits(cc, sz);

  return sumbit;
}

void putbits(uint32 val, uint32 nbit)
{
  int new_bitpos = CodedData.bitpos + nbit;
  int ptrpos     = CodedData.bitpos >> 5;

  val = val & (0xffffffff >> (32 - nbit));

  /* data fit in one uint32 */
  if(((new_bitpos - 1) >> 5) == ptrpos)
    CodedData.bbuf[ptrpos] |= val << ((32 - new_bitpos) & 31);
  else
  {
    CodedData.bbuf[ptrpos  ] |= val >> ((new_bitpos - 32) & 31);
    CodedData.bbuf[ptrpos+1] |= val << ((32 - new_bitpos) & 31);
  }

  CodedData.bitpos = new_bitpos;
}

/***************************************************************************/
/*  Choose the Huffman table that will encode ix[begin..end] with          */
/*  the fewest bits.                                                       */
/*  Note: This code contains knowledge about the sizes and characteristic  */
/*  of the Huffman tables as defined in the IS (Table B.7), and will not   */
/*  work with any arbitrary tables.                                        */
/***************************************************************************/
int choose_table( short *ix, uint32 begin, uint32 end, int *bits )
{
  uint32 i;
  int    max, table0, table1;
  
  for(i=begin,max=0; i<end; i++)
    if(ix[i] > max)
      max = ix[i];

  if(max < 16)
  {
    /* tables without linbits */
    /* indx: 0  1  2  3  4  5  6  7  8  9 10 11 12  13 14  15 */
    /*  len: 0, 2, 3, 3, 0, 4, 4, 6, 6, 6, 8, 8, 8, 16, 0, 16 */
    switch(max)
    {
      case 0:  return  0;
      case 1:  return       count_bit1(ix, begin, end, bits);
      case 2:  return  2 + find_best_2(ix, begin, end, tab23, 3, bits);
      case 3:  return  5 + find_best_2(ix, begin, end, tab56, 4, bits);
      case 4:
      case 5:  return  7 + find_best_3(ix, begin, end, tab789, 6, bits);
      case 6:
      case 7:  return 10 + find_best_3(ix, begin, end, tabABC, 8, bits);
      default: return 13 + find_best_2(ix, begin, end, tab1315, 16, bits) * 2;
    }
  }
  else
  {
    /* tables with linbits */
    max -= 15;

    for(table0=0; table0<8; table0++)
      if(ht_big[table0].linmax >= max)
        break;

    for(table1=8; table1<16; table1++)
      if(ht_big[table1].linmax >= max)
        break;

    return 16 + count_bigv(ix, begin, end, table0, table1, bits);
  }
}

int find_best_2(short *ix, uint32 start, uint32 end, const uint32 *table,
                uint32 len, int *bits)
{
  uint32 i, sum = 0;

  for(i=start; i<end; i+=2)
    sum += table[ix[i] * len + ix[i+1]];

  if((sum & 0xffff) <= (sum >> 16))
  {
    *bits = (sum & 0xffff);
    return 1;
  }
  else
  {
    *bits = sum >> 16;
    return 0;
  }
}

int find_best_3(short *ix, uint32 start, uint32 end, const uint32 *table,
                uint32 len, int *bits)
{
  uint32 i, j, sum  = 0;
  int          sum1 = 0;
  int          sum2 = 0;
  int          sum3 = 0;

  /* avoid overflow in packed additions: 78*13 < 1024 */
  for(i=start; i<end; )
  {
    j = i + 2*78 > end ? end : i + 2*78;

    for(sum=0; i<j; i+=2)
      sum += table[ix[i] * len + ix[i+1]];

    sum1 += (sum >> 20);
    sum2 += (sum >> 10) & 0x3ff;
    sum3 += (sum >>  0) & 0x3ff;
  }

  i = 0;
  if(sum1 > sum2) { sum1 = sum2;  i = 1; }
  if(sum1 > sum3) { sum1 = sum3;  i = 2; }

  *bits = sum1;

  return i;
}

/*************************************************************************/
/* Function: Count the number of bits necessary to code the subregion.   */
/*************************************************************************/
int count_bit1(short *ix, uint32 start, uint32 end, int *bits )
{
  uint32 i, sum = 0;

  for(i=start; i<end; i+=2)
    sum += t1l[4 + ix[i] * 2 + ix[i+1]];

  *bits = sum;

  return 1; /* this is table1 */
}

int count_bigv(short *ix, uint32 start, uint32 end, int table0,
               int table1, int *bits )
{
  uint32  i, sum0, sum1, sum=0, bigv=0, x, y;

  /* ESC-table is used */
  for(i=start; i<end; i+=2)
  {
    x = ix[i];
    y = ix[i+1];

    if(x > 14) { x = 15; bigv++; }
    if(y > 14) { y = 15; bigv++; }

    sum += tab1624[x * 16 + y];
  }

  sum0 = (sum  >>  16)  + bigv * ht_big[table0].linbits;
  sum1 = (sum & 0xffff) + bigv * ht_big[table1].linbits;

  if(sum0 <= sum1)
  {
    *bits = sum0;
    return table0;
  }
  else
  {
    *bits = sum1;
    return table1;
  }
}

/*************************************************************************/
/* Function: Calculation of rzero, count1, address3                      */
/* (Partitions ix into big values, quadruples and zeros).                */
/*************************************************************************/
int calc_runlen( short *ix, side_info_t *si )
{
  int  p, i, sum = 0;

  for(i=SAMPL2; i-=2; )
    if(*(uint32*)&ix[i-2]) /* !!!! short *ix; !!!!! */
      break;

  si->count1 = 0;

  for( ; i>3; i-=4)
  {
    int v = ix[i-1];
    int w = ix[i-2];
    int x = ix[i-3];
    int y = ix[i-4];
    
    if((v | w | x | y) <= 1)
    {
      p = (y<<3) + (x<<2) + (w<<1) + (v);

      sum += tab01[p];

      si->count1++;
    }
    else break;
  }

  si->address3 = i;

  if((sum >> 16) < (sum & 0xffff))
  {
    si->table_select[3] = 0;
    return sum >> 16;
  }
  else
  {
    si->table_select[3] = 1;
    return sum & 0xffff;
  }
}


/*************************************************************************/
/*   Function: Quantization of the vector xr ( -> ix)                    */
/*************************************************************************/
int quantize_int(int *xr, short *ix, side_info_t *si)
{
  int   i, s, frac_pow[] = { 0x10000, 0xd745, 0xb505, 0x9838 };

  s = frac_pow[si->quantStep & 3] >> si->quantStep / 4;

  /* check for integer overflow */
  if(((si->max_val + 256) >> 8) * s >= (1622 << 8))
    return 0;

  for(i=SAMPL2; i--; )
    ix[i] = int2idx[(abs(xr[i]) * s + 0x8000) >> 16];

  return 1;
}

/*************************************************************************/
/* subdivides the bigvalue region which will use separate Huffman tables */
/*************************************************************************/
void subdivide(side_info_t *si)
{
  int scfb, count0, count1;
  
  if( !si->address3 )
  { /* no bigvalue region */
    si->region_0_1 = 0;
    si->address1   = 0;
    si->address2   = 0;
  }
  else
  {
    /* Calculate scale factor band index */
    for(scfb=0; scalefac[scfb] < si->address3; )
      scfb++;

    count0 = subdv_table[scfb].region0_cnt;
    count1 = subdv_table[scfb].region1_cnt;

    si->region_0_1 = (count0 << 3) | count1;
    si->address1   = scalefac[count0 + 1];
    si->address2   = scalefac[count0 + 1 + count1 + 1];
  }
}

/*******************************************************************/
/* Count the number of bits necessary to code the bigvalues region */
/*******************************************************************/
int bigv_bitcount(short *ix, side_info_t *gi)
{
  int b1=0, b2=0, b3=0;

  /* Select huffman code tables for bigvalues regions */
  gi->table_select[0] = 0;
  gi->table_select[1] = 0;
  gi->table_select[2] = 0;

  if( gi->address1 > 0 )            /* region0 */
    gi->table_select[0] = choose_table(ix, 0           , gi->address1, &b1);

  if( gi->address2 > gi->address1 ) /* region1 */
    gi->table_select[1] = choose_table(ix, gi->address1, gi->address2, &b2);

  if( gi->address3 > gi->address2 ) /* region2 */
    gi->table_select[2] = choose_table(ix, gi->address2, gi->address3, &b3);

  return b1+b2+b3;
}

int quantize_and_count_bits(int *xr, short *ix, side_info_t *si)
{
  int bits = 10000;

  if(quantize_int(xr, ix, si))
  {
    bits = calc_runlen(ix, si);      /* rzero,count1,address3  */
    subdivide(si);                   /* bigvalues sfb division */
    bits += bigv_bitcount(ix,si);    /* bit count */
  }

  return bits;
}

/***********************************************************************/
/* The code selects the best quantStep for a particular set of scalefacs                                                            */
/***********************************************************************/ 
int inner_loop(int *xr, int max_bits, side_info_t *si)
{
  int bits;

  while((bits=quantize_and_count_bits(xr, enc_data, si)) < max_bits-64)
  {
    if(si->quantStep == 0)
      break;

    if(si->quantStep <= 2)
      si->quantStep  = 0;
    else
      si->quantStep -= 2;
  }

  while(bits > max_bits)
  {
    si->quantStep++;
    bits = quantize_and_count_bits(xr, enc_data, si);
  }

  return bits;
}

void iteration_loop(int *xr, side_info_t *si, int gr_cnt)
{
  int remain, tar_bits, max_bits = cfg.mean_bits;

  /* distribute reserved bits to remaining granules */
  tar_bits = max_bits + (cfg.ResvSize / gr_cnt & ~7);
  if(tar_bits > max_bits + max_bits/2)
    tar_bits = max_bits + max_bits/2;

  si->part2_3_length = inner_loop(xr, tar_bits, si);
  si->global_gain    = si->quantStep + 142 - si->additStep;

  /* unused bits of the reservoir can be used for remaining granules */
  cfg.ResvSize += max_bits - si->part2_3_length;

  /* end: distribute the reserved bits to one or two granules */
  if(gr_cnt == 1)
  {
    si->part2_3_length += cfg.ResvSize;
    /* mp3 format allows max 12bits for granule length */
    if(si->part2_3_length > 4092)
    {
      remain = (si->part2_3_length - 4092 + 31) >> 5;
      si->part2_3_length    -= remain << 5;