~ubuntu-dev/mplayer/ubuntu-feisty

« back to all changes in this revision

Viewing changes to libfaad2/common.c

  • Committer: Reinhard Tartler
  • Date: 2006-07-08 08:47:54 UTC
  • Revision ID: siretart@tauware.de-20060708084754-c3ff228cc9c2d8de
upgrade to pre8

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3
3
** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
4
 
**  
 
4
**
5
5
** This program is free software; you can redistribute it and/or modify
6
6
** it under the terms of the GNU General Public License as published by
7
7
** the Free Software Foundation; either version 2 of the License, or
8
8
** (at your option) any later version.
9
 
** 
 
9
**
10
10
** This program is distributed in the hope that it will be useful,
11
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
13
** GNU General Public License for more details.
14
 
** 
 
14
**
15
15
** You should have received a copy of the GNU General Public License
16
 
** along with this program; if not, write to the Free Software 
 
16
** along with this program; if not, write to the Free Software
17
17
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
18
**
19
19
** Any non-GPL usage of this software or parts of this software is strictly
22
22
** Commercial non-GPL licensing of this software is possible.
23
23
** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
24
24
**
25
 
** $Id: common.c,v 1.19 2004/06/30 12:45:56 menno Exp $
 
25
** $Id: common.c,v 1.22 2004/09/08 09:43:11 gcp Exp $
26
26
**/
27
27
 
28
28
/* just some common functions that could be used anywhere */
33
33
#include <stdlib.h>
34
34
#include "syntax.h"
35
35
 
36
 
#ifdef USE_SSE
37
 
__declspec(naked) static int32_t __fastcall test_cpuid(void)
38
 
{
39
 
    __asm
40
 
    {
41
 
        pushf
42
 
        pop eax
43
 
        mov ecx,eax
44
 
        xor eax,(1<<21)
45
 
        push eax
46
 
        popf
47
 
        pushf
48
 
        pop eax
49
 
        push ecx
50
 
        popf
51
 
        cmp eax,ecx
52
 
        mov eax,0
53
 
        setne al
54
 
        ret
55
 
    }
56
 
}
57
 
 
58
 
__declspec(naked) static void __fastcall run_cpuid(int32_t param, int32_t out[4])
59
 
{
60
 
    __asm
61
 
    {
62
 
        pushad
63
 
        push edx
64
 
        mov eax,ecx
65
 
        cpuid
66
 
        pop edi
67
 
        mov [edi+0],eax
68
 
        mov [edi+4],ebx
69
 
        mov [edi+8],ecx
70
 
        mov [edi+12],edx
71
 
        popad
72
 
        ret
73
 
    }
74
 
}
75
 
 
76
 
uint8_t cpu_has_sse()
77
 
{
78
 
    int32_t features[4];
79
 
 
80
 
    if (test_cpuid())
81
 
    {
82
 
        run_cpuid(1, features);
83
 
    }
84
 
 
85
 
    /* check for SSE */
86
 
    if (features[3] & 0x02000000)
87
 
        return 1;
88
 
 
89
 
    return 0;
90
 
}
91
 
#else
92
 
uint8_t cpu_has_sse()
93
 
{
94
 
    return 0;
95
 
}
96
 
#endif
97
36
 
98
37
/* Returns the sample rate index based on the samplerate */
99
38
uint8_t get_sr_index(const uint32_t samplerate)
146
85
uint8_t max_tns_sfb(const uint8_t sr_index, const uint8_t object_type,
147
86
                    const uint8_t is_short)
148
87
{
149
 
    /* entry for each sampling rate     
 
88
    /* entry for each sampling rate
150
89
     * 1    Main/LC long window
151
90
     * 2    Main/LC short window
152
91
     * 3    SSR long window
230
169
    return -1;
231
170
}
232
171
 
233
 
/* common malloc function */
234
172
void *faad_malloc(size_t size)
235
173
{
236
174
#if 0 // defined(_WIN32) && !defined(_WIN32_WCE)
237
175
    return _aligned_malloc(size, 16);
238
 
#else
 
176
#else   // #ifdef 0
239
177
    return malloc(size);
240
 
#endif
 
178
#endif  // #ifdef 0
241
179
}
242
180
 
243
181
/* common free function */
247
185
    _aligned_free(b);
248
186
#else
249
187
    free(b);
 
188
}
250
189
#endif
251
 
}
252
190
 
253
191
static const  uint8_t    Parity [256] = {  // parity
254
 
        0,1,1,0,1,0,0,1,1,0,0,1,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    1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0
262
200
};
263
201
 
264
202
static uint32_t  __r1 = 1;
293
231
 */
294
232
uint32_t random_int(void)
295
233
{
296
 
        uint32_t  t1, t2, t3, t4;
297
 
 
298
 
        t3   = t1 = __r1;   t4   = t2 = __r2;       // Parity calculation is done via table lookup, this is also available
299
 
        t1  &= 0xF5;        t2 >>= 25;              // on CPUs without parity, can be implemented in C and avoid unpredictable
300
 
        t1   = Parity [t1]; t2  &= 0x63;            // jumps and slow rotate through the carry flag operations.
301
 
        t1 <<= 31;          t2   = Parity [t2];
302
 
 
303
 
        return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 );
 
234
    uint32_t  t1, t2, t3, t4;
 
235
 
 
236
    t3   = t1 = __r1;   t4   = t2 = __r2;       // Parity calculation is done via table lookup, this is also available
 
237
    t1  &= 0xF5;        t2 >>= 25;              // on CPUs without parity, can be implemented in C and avoid unpredictable
 
238
    t1   = Parity [t1]; t2  &= 0x63;            // jumps and slow rotate through the carry flag operations.
 
239
    t1 <<= 31;          t2   = Parity [t2];
 
240
 
 
241
    return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 );
304
242
}
305
243
 
306
244
uint32_t ones32(uint32_t x)
496
434
{
497
435
    uint32_t frac;
498
436
    uint32_t whole = (val);
499
 
    int8_t exp = 0;
 
437
    int32_t exp = 0;
500
438
    uint32_t index;
501
439
    uint32_t index_frac;
502
440
    uint32_t x1, x2;
534
472
 
535
473
    return ((exp+REAL_BITS) << REAL_BITS) + errcorr + x1;
536
474
}
 
475
 
 
476
/* ld(x) = ld(x*y/y) = ld(x/y) + ld(y), with y=2^N and [1 <= (x/y) < 2] */
 
477
real_t log2_fix(uint32_t val)
 
478
{
 
479
    uint32_t frac;
 
480
    uint32_t whole = (val >> REAL_BITS);
 
481
    int8_t exp = 0;
 
482
    uint32_t index;
 
483
    uint32_t index_frac;
 
484
    uint32_t x1, x2;
 
485
    uint32_t errcorr;
 
486
 
 
487
    /* error */
 
488
    if (val == 0)
 
489
        return -100000;
 
490
 
 
491
    exp = floor_log2(val);
 
492
    exp -= REAL_BITS;
 
493
 
 
494
    /* frac = [1..2] */
 
495
    if (exp >= 0)
 
496
        frac = val >> exp;
 
497
    else
 
498
        frac = val << -exp;
 
499
 
 
500
    /* index in the log2 table */
 
501
    index = frac >> (REAL_BITS-TABLE_BITS);
 
502
 
 
503
    /* leftover part for linear interpolation */
 
504
    index_frac = frac & ((1<<(REAL_BITS-TABLE_BITS))-1);
 
505
 
 
506
    /* leave INTERP_BITS bits */
 
507
    index_frac = index_frac >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
 
508
 
 
509
    x1 = log2_tab[index & ((1<<TABLE_BITS)-1)];
 
510
    x2 = log2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
 
511
 
 
512
    /* linear interpolation */
 
513
    /* retval = exp + ((index_frac)*x2 + (1-index_frac)*x1) */
 
514
 
 
515
    errcorr = (index_frac * (x2-x1)) >> INTERP_BITS;
 
516
 
 
517
    return (exp << REAL_BITS) + errcorr + x1;
 
518
}
537
519
#endif