summaryrefslogtreecommitdiffstats
path: root/apps/codecs/libmusepack/mpc_bits_reader.h
blob: 1233720c742ad7e7207dc6f13ed6722e1f4736f9 (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
/*
  Copyright (c) 2007-2009, The Musepack Development Team
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:

  * Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

  * Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the following
  disclaimer in the documentation and/or other materials provided
  with the distribution.

  * Neither the name of the The Musepack Development Team nor the
  names of its contributors may be used to endorse or promote
  products derived from this software without specific prior
  written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#define MAX_ENUM 32

MPC_API int mpc_bits_get_block(mpc_bits_reader * r, mpc_block * p_block);
mpc_int32_t mpc_bits_golomb_dec(mpc_bits_reader * r, const mpc_uint_t k);
MPC_API unsigned int mpc_bits_get_size(mpc_bits_reader * r, mpc_uint64_t * p_size);
mpc_uint32_t mpc_bits_log_dec(mpc_bits_reader * r, mpc_uint_t max);

extern const mpc_uint32_t Cnk     [MAX_ENUM / 2][MAX_ENUM];
extern const mpc_uint8_t  Cnk_len [MAX_ENUM / 2][MAX_ENUM];
extern const mpc_uint32_t Cnk_lost[MAX_ENUM / 2][MAX_ENUM];

// can read up to 31 bits
static mpc_inline mpc_uint32_t mpc_bits_read(mpc_bits_reader * r, const unsigned int nb_bits)
{
    mpc_uint32_t ret;

    r->buff -= (int)(r->count - nb_bits) >> 3;
    r->count = (r->count - nb_bits) & 0x07;

    ret = (r->buff[0] | (r->buff[-1] << 8)) >> r->count;
    if (nb_bits > (16 - r->count)) {
        ret |= (mpc_uint32_t)((r->buff[-2] << 16) | (r->buff[-3] << 24)) >> r->count;
        if (nb_bits > 24 && r->count != 0)
            ret |= r->buff[-4] << (32 - r->count);
    }

    return ret & ((1 << nb_bits) - 1);
}

#if defined(CPU_COLDFIRE)
/* rockbox: This is specific code to optimize demux performance on Coldfire
 * CPUs. Coldfire CPUs are very sensible to RAM accesses. As the bitstream
 * buffer does not fit into IRAM the read accesses to the uint8 buffer are very
 * expensive in terms of CPU cycles.
 * The following code uses two variables in IRAM. The variable last_code keeps
 * the 4-byte value of buf[0]<<16 | buf[1]<<8 | buf[2]. As long as buf[0] will 
 * read from the same address the following code will avoid re-reading of the 
 * buffers. If buf[0] did advance to the next uint8-entry since the last call
 * the following will only need to load 1 uint8-entry instead of 3. 
 */
static mpc_inline mpc_uint16_t get_code_from_buffer(mpc_bits_reader *r)
{
    /* Buffer advanced by 1 entry since last call */
    if (r->buff == r->buffered_addr + 1) {
        r->buffered_code = (r->buffered_code<<8) | r->buff[2];
        r->buffered_addr = r->buff;
    }
    /* Buffer must be fully re-read */
    else if (r->buff != r->buffered_addr) {
        r->buffered_code = (r->buff[0] << 16) | (r->buff[1] << 8) | r->buff[2];
        r->buffered_addr = r->buff;
    }

    return (mpc_uint16_t)((r->buffered_code >> r->count) & 0xFFFF);
}
#else
/* Use the decoder's default implementation. This is faster on non-Coldfire targets */
#define get_code_from_buffer(r) (mpc_uint16_t)((((r->buff[0] << 16) | (r->buff[1] << 8) | r->buff[2]) >> r->count) & 0xFFFF);
#endif

// basic huffman decoding routine
// works with maximum lengths up to 16
static mpc_inline mpc_int32_t mpc_bits_huff_dec(mpc_bits_reader * r, const mpc_huffman *Table)
{
    const mpc_uint16_t code = get_code_from_buffer(r);

    while (code < Table->Code) Table++;

    r->buff -= (int)(r->count - Table->Length) >> 3;
    r->count = (r->count - Table->Length) & 0x07;

    return Table->Value;
}

static mpc_inline mpc_int32_t mpc_bits_can_dec(mpc_bits_reader * r, const mpc_can_data *can)
{
    const mpc_uint16_t code = get_code_from_buffer(r);
    const mpc_huff_lut tmp  = can->lut[code >> (16 - LUT_DEPTH)];
    const mpc_huffman * Table;

    if (tmp.Length != 0) {
        r->buff -= (int)(r->count - tmp.Length) >> 3;
        r->count = (r->count - tmp.Length) & 0x07;
        return tmp.Value;
    }

    Table = can->table + (unsigned char)tmp.Value;
    while (code < Table->Code) Table++;

    r->buff -= (int)(r->count - Table->Length) >> 3;
    r->count = (r->count - Table->Length) & 0x07;

    return can->sym[(Table->Value - (code >> (16 - Table->Length))) & 0xFF] ;
}

// LUT-based huffman decoding routine
// works with maximum lengths up to 16
static mpc_inline mpc_int32_t mpc_bits_huff_lut(mpc_bits_reader * r, const mpc_lut_data *lut)
{
    const mpc_uint16_t code = get_code_from_buffer(r);
    const mpc_huff_lut tmp  = lut->lut[code >> (16 - LUT_DEPTH)];
    const mpc_huffman * Table;

    if (tmp.Length != 0) {
        r->buff -= (int)(r->count - tmp.Length) >> 3;
        r->count = (r->count - tmp.Length) & 0x07;
        return tmp.Value;
    }

    Table = lut->table + (unsigned char)tmp.Value;
    while (code < Table->Code) Table++;

    r->buff -= (int)(r->count - Table->Length) >> 3;
    r->count = (r->count - Table->Length) & 0x07;

    return Table->Value;
}

static mpc_inline mpc_uint32_t mpc_bits_enum_dec(mpc_bits_reader * r, mpc_uint_t k, mpc_uint_t n)
{
    mpc_uint32_t bits = 0;
    mpc_uint32_t code;
    const mpc_uint32_t * C = Cnk[k-1];

    code = mpc_bits_read(r, Cnk_len[k-1][n-1] - 1);

    if (code >= Cnk_lost[k-1][n-1])
        code = ((code << 1) | mpc_bits_read(r, 1)) - Cnk_lost[k-1][n-1];

    do {
        n--;
        if (code >= C[n]) {
            bits |= 1 << n;
            code -= C[n];
            C -= MAX_ENUM;
            k--;
        }
    } while(k > 0);

    return bits;
}