/* 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; } 04'>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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2007 by Michael Sevakis
 *
 * 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.
 *
 ****************************************************************************/

    .global     mpeg2_idct_copy
    .type       mpeg2_idct_copy, %function
    .global     mpeg2_idct_add
    .type       mpeg2_idct_add, %function


/* Custom calling convention:
 * r0 contains block pointer and is non-volatile
 * all non-volatile c context saved and restored on its behalf
 */
.idct:
    add    r12, r0, #128
1:
    ldrsh  r1, [r0, #0]           /* d0 */
    ldrsh  r2, [r0, #2]           /* d1 */
    ldrsh  r3, [r0, #4]           /* d2 */
    ldrsh  r4, [r0, #6]           /* d3 */
    ldrsh  r5, [r0, #8]           /* d0 */
    ldrsh  r6, [r0, #10]          /* d1 */
    ldrsh  r7, [r0, #12]          /* d2 */
    ldrsh  r8, [r0, #14]          /* d3 */
    orrs   r9, r2, r3
    orreqs r9, r4, r5
    orreqs r9, r6, r7
    cmpeq  r8, #0
    bne    2f
    mov    r1, r1, asl #15
    bic    r1, r1, #0x8000
    orr    r1, r1, r1, lsr #16
    str    r1, [r0], #4
    str    r1, [r0], #4
    str    r1, [r0], #4
    str    r1, [r0], #4
    cmp    r0, r12
    blo    1b
    b      3f
2:
    mov    r1, r1, asl #11        /* r1 = d0 = (block[0] << 11) + 2048 */
    add    r1, r1, #2048
    add    r1, r1, r3, asl #11    /* r1 = t0 = d0 + (block[2] << 11) */
    sub    r3, r1, r3, asl #12    /* r3 = t1 = d0 - (block[2] << 11) */

    add    r9, r2, r4             /* r9 = tmp = (d1+d3)*(1108/4) */
    add    r10, r9, r9, asl #2
    add    r10, r10, r9, asl #4
    add    r9, r10, r9, asl #8

    add    r10, r2, r2, asl #4    /* r2 = t2 = tmp + (d1*(1568/32)*8) */
    add    r2, r10, r2, asl #5
    add    r2, r9, r2, asl #3

    add    r10, r4, r4, asl #2    /* r4 = t3 = tmp - (d3*(3784/8)*2) */
    rsb    r10, r10, r4, asl #6
    add    r4, r4, r10, asl #3
    sub    r4, r9, r4, asl #1
    /* t2 & t3 are 1/4 final value here */
    add    r1, r1, r2, asl #2     /* r1 = a0 = t0 + t2 */
    sub    r2, r1, r2, asl #3     /* r2 = a3 = t0 - t2 */
    add    r3, r3, r4, asl #2     /* r3 = a1 = t1 + t3 */
    sub    r4, r3, r4, asl #3     /* r4 = a2 = t1 - t3 */

    add    r9, r8, r5             /* r9 = tmp = 565*(d3 + d0) */
    add    r10, r9, r9, asl #4
    add    r10, r10, r10, asl #5
    add    r9, r10, r9, asl #2

    add    r10, r5, r5, asl #4    /* r5 = t0 = tmp + (((2276/4)*d0)*4) */
    add    r10, r10, r10, asl #5
    add    r5, r10, r5, asl #3
    add    r5, r9, r5, asl #2

    add    r10, r8, r8, asl #2    /* r8 = t1 = tmp - (((3406/2)*d3)*2) */
    add    r10, r10, r10, asl #4
    add    r10, r10, r8, asl #7
    rsb    r8, r8, r10, asl #3
    sub    r8, r9, r8, asl #1

    add    r9, r6, r7             /* r9 = tmp = (2408/8)*(d1 + d2) */
    add    r10, r9, r9, asl #3
    add    r10, r10, r10, asl #5
    add    r9, r10, r9, asl #2

    add    r10, r7, r7, asl #3    /* r7 = t2 = (tmp*8) - 799*d2 */
    add    r10, r10, r7, asl #4
    rsb    r7, r7, r10, asl #5
    rsb    r7, r7, r9, asl #3

    sub    r10, r6, r6, asl #4    /* r6 = t3 = (tmp*8) - 4017*d1 */
    sub    r10, r10, r6, asl #6
    add    r10, r10, r6, asl #12
    add    r6, r10, r6
    rsb    r6, r6, r9, asl #3
    /* t0 = r5, t1 = r8, t2 = r7, t3 = r6*/
    add    r9, r5, r7             /* r9 = b0 = t0 + t2 */
    add    r10, r8, r6            /* r10 = b3 = t1 + t3 */
    sub    r5, r5, r7             /* t0 -= t2 */
    sub    r8, r8, r6             /* t1 -= t3 */
    add    r6, r5, r8             /* r6 = t0 + t1 */
    sub    r7, r5, r8             /* r7 = t0 - t1 */

    add    r11, r6, r6, asr #2    /* r6 = b1 = r6*(181/128) */
    add    r11, r11, r11, asr #5
    add    r6, r11, r6, asr #3
    add    r11, r7, r7, asr #2    /* r7 = b2 = r7*(181/128) */
    add    r11, r11, r11, asr #5
    add    r7, r11, r7, asr #3
    /* r1 = a0, r3 = a1,   r4 = a2,   r2 = a3 */
    /* r9 = b0, r6 = b1*2, r7 = b2*2, r10 = b3 */
    add    r5, r1, r9             /* block[0] = (a0 + b0) >> 12 */
    mov    r5, r5, asr #12
    strh   r5, [r0], #2
    add    r8, r3, r6, asr #1     /* block[1] = (a1 + b1) >> 12 */
    mov    r8, r8, asr #12
    strh   r8, [r0], #2
    add    r5, r4, r7, asr #1     /* block[2] = (a2 + b2) >> 12 */
    mov    r5, r5, asr #12
    strh   r5, [r0], #2
    add    r8, r2, r10            /* block[3] = (a3 + b3) >> 12 */
    mov    r8, r8, asr #12
    strh   r8, [r0], #2
    sub    r5, r2, r10            /* block[4] = (a3 - b3) >> 12 */
    mov    r5, r5, asr #12
    strh   r5, [r0], #2
    sub    r8, r4, r7, asr #1     /* block[5] = (a2 - b2) >> 12 */
    mov    r8, r8, asr #12
    strh   r8, [r0], #2
    sub    r5, r3, r6, asr #1     /* block[6] = (a1 - b1) >> 12 */
    mov    r5, r5, asr #12
    strh   r5, [r0], #2
    sub    r8, r1, r9             /* block[7] = (a0 - b0) >> 12 */
    mov    r8, r8, asr #12
    strh   r8, [r0], #2
    cmp    r0, r12
    blo    1b
3:
    sub    r0, r0, #128
    add    r12, r0, #16
4:
    ldrsh  r1, [r0, #0*8]         /* d0 */
    ldrsh  r2, [r0, #2*8]         /* d1 */
    ldrsh  r3, [r0, #4*8]         /* d2 */
    ldrsh  r4, [r0, #6*8]         /* d3 */
    ldrsh  r5, [r0, #8*8]         /* d0 */
    ldrsh  r6, [r0, #10*8]        /* d1 */
    ldrsh  r7, [r0, #12*8]        /* d2 */
    ldrsh  r8, [r0, #14*8]        /* d3 */

    mov    r1, r1, asl #11        /* r1 = d0 = (block[0] << 11) + 2048 */
    add    r1, r1, #65536
    add    r1, r1, r3, asl #11    /* r1 = t0 = d0 + d2:(block[2] << 11) */
    sub    r3, r1, r3, asl #12    /* r3 = t1 = d0 - d2:(block[2] << 11) */

    add    r9, r2, r4             /* r9 = tmp = (d1+d3)*(1108/4) */
    add    r10, r9, r9, asl #2
    add    r10, r10, r9, asl #4
    add    r9, r10, r9, asl #8

    add    r11, r2, r2, asl #4    /* r2 = t2 = tmp + (d1*(1568/32)*8) */
    add    r2, r11, r2, asl #5
    add    r2, r9, r2, asl #3

    add    r10, r4, r4, asl #2    /* r4 = t3 = tmp - (d3*(3784/8)*2) */
    rsb    r10, r10, r4, asl #6
    add    r4, r4, r10, asl #3
    sub    r4, r9, r4, asl #1
    /* t2 & t3 are 1/4 final value here */
    add    r1, r1, r2, asl #2     /* r1 = a0 = t0 + t2 */
    sub    r2, r1, r2, asl #3     /* r2 = a3 = t0 - t2 */
    add    r3, r3, r4, asl #2     /* r3 = a1 = t1 + t3 */
    sub    r4, r3, r4, asl #3     /* r4 = a2 = t1 - t3 */

    add    r9, r8, r5             /* r9 = tmp = 565*(d3 + d0) */
    add    r10, r9, r9, asl #4
    add    r10, r10, r10, asl #5
    add    r9, r10, r9, asl #2

    add    r10, r5, r5, asl #4    /* r5 = t0 = tmp + (((2276/4)*d0)*4) */
    add    r10, r10, r10, asl #5
    add    r5, r10, r5, asl #3
    add    r5, r9, r5, asl #2

    add    r10, r8, r8, asl #2    /* r8 = t1 = tmp - (((3406/2)*d3)*2) */
    add    r10, r10, r10, asl #4
    add    r10, r10, r8, asl #7
    rsb    r8, r8, r10, asl #3
    sub    r8, r9, r8, asl #1

    add    r9, r6, r7             /* r9 = tmp = (2408/8)*(d1 + d2) */
    add    r10, r9, r9, asl #3
    add    r10, r10, r10, asl #5
    add    r9, r10, r9, asl #2

    add    r10, r7, r7, asl #3    /* r7 = t2 = (tmp*8) - 799*d2 */
    add    r10, r10, r7, asl #4
    rsb    r7, r7, r10, asl #5
    rsb    r7, r7, r9, asl #3

    sub    r10, r6, r6, asl #4    /* r6 = t3 = (tmp*8) - 4017*d1 */
    sub    r10, r10, r6, asl #6
    add    r10, r10, r6, asl #12
    add    r6, r10, r6
    rsb    r6, r6, r9, asl #3
                                    /* t0=r5, t1=r8, t2=r7, t3=r6*/
    add    r9, r5, r7             /* r9 = b0 = t0 + t2 */
    add    r10, r8, r6            /* r10 = b3 = t1 + t3 */
    sub    r5, r5, r7             /* t0 -= t2 */
    sub    r8, r8, r6             /* t1 -= t3 */
    add    r6, r5, r8             /* r6 = t0 + t1 */
    sub    r7, r5, r8             /* r7 = t0 - t1 */

    add    r11, r6, r6, asr #2    /* r6 = b1 = r5*(181/128) */
    add    r11, r11, r11, asr #5
    add    r6, r11, r6, asr #3
    add    r11, r7, r7, asr #2    /* r7 = b2 = r6*(181/128) */
    add    r11, r11, r11, asr #5
    add    r7, r11, r7, asr #3
    /* r1 = a0, r3 = a1,   r4 = a2,    r2 = a3 */
    /* r9 = b0, r6 = b1*2, r7 = b2*2, r10 = b3 */
    add    r5, r1, r9             /* block[0] = (a0 + b0) >> 17 */
    mov    r5, r5, asr #17
    strh   r5, [r0, #0*8]
    add    r8, r3, r6, asr #1     /* block[1] = (a1 + b1) >> 17 */
    mov    r8, r8, asr #17
    strh   r8, [r0, #2*8]
    add    r5, r4, r7, asr #1     /* block[2] = (a2 + b2) >> 17 */
    mov    r5, r5, asr #17
    strh   r5, [r0, #4*8]
    add    r8, r2, r10            /* block[3] = (a3 + b3) >> 17 */
    mov    r8, r8, asr #17
    strh   r8, [r0, #6*8]
    sub    r5, r2, r10            /* block[4] = (a3 - b3) >> 17 */
    mov    r5, r5, asr #17
    strh   r5, [r0, #8*8]
    sub    r8, r4, r7, asr #1     /* block[5] = (a2 - b2) >> 17 */
    mov    r8, r8, asr #17
    strh   r8, [r0, #10*8]
    sub    r5, r3, r6, asr #1     /* block[6] = (a1 - b1) >> 17 */
    mov    r5, r5, asr #17
    strh   r5, [r0, #12*8]