📄 alac.c
字号:
/* * ALAC (Apple Lossless Audio Codec) decoder * Copyright (c) 2005 David Hammerton * All rights reserved. * * This is the actual decoder. * * http://crazney.net/programs/itunes/alac.html * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdint.h>#include "decomp.h"#define _Swap32(v) do { \ v = (((v) & 0x000000FF) << 0x18) | \ (((v) & 0x0000FF00) << 0x08) | \ (((v) & 0x00FF0000) >> 0x08) | \ (((v) & 0xFF000000) >> 0x18); } while(0)#define _Swap16(v) do { \ v = (((v) & 0x00FF) << 0x08) | \ (((v) & 0xFF00) >> 0x08); } while (0)extern int host_bigendian;struct alac_file{ unsigned char *input_buffer; int input_buffer_bitaccumulator; /* used so we can do arbitary bit reads */ int samplesize; int numchannels; int bytespersample; /* buffers */ int32_t *predicterror_buffer_a; int32_t *predicterror_buffer_b; int32_t *outputsamples_buffer_a; int32_t *outputsamples_buffer_b; /* stuff from setinfo */ uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */ /* max samples per frame? */ uint8_t setinfo_7a; /* 0x00 */ uint8_t setinfo_sample_size; /* 0x10 */ uint8_t setinfo_rice_historymult; /* 0x28 */ uint8_t setinfo_rice_initialhistory; /* 0x0a */ uint8_t setinfo_rice_kmodifier; /* 0x0e */ uint8_t setinfo_7f; /* 0x02 */ uint16_t setinfo_80; /* 0x00ff */ uint32_t setinfo_82; /* 0x000020e7 */ /* max sample size?? */ uint32_t setinfo_86; /* 0x00069fe4 */ /* bit rate (avarge)?? */ uint32_t setinfo_8a_rate; /* 0x0000ac44 */ /* end setinfo stuff */};static void allocate_buffers(alac_file *alac){ alac->predicterror_buffer_a = malloc(alac->setinfo_max_samples_per_frame * 4); alac->predicterror_buffer_b = malloc(alac->setinfo_max_samples_per_frame * 4); alac->outputsamples_buffer_a = malloc(alac->setinfo_max_samples_per_frame * 4); alac->outputsamples_buffer_b = malloc(alac->setinfo_max_samples_per_frame * 4);}void alac_set_info(alac_file *alac, char *inputbuffer){ char *ptr = inputbuffer; ptr += 4; /* size */ ptr += 4; /* frma */ ptr += 4; /* alac */ ptr += 4; /* size */ ptr += 4; /* alac */ ptr += 4; /* 0 ? */ alac->setinfo_max_samples_per_frame = *(uint32_t*)ptr; /* buffer size / 2 ? */ if (!host_bigendian) _Swap32(alac->setinfo_max_samples_per_frame); ptr += 4; alac->setinfo_7a = *(uint8_t*)ptr; ptr += 1; alac->setinfo_sample_size = *(uint8_t*)ptr; ptr += 1; alac->setinfo_rice_historymult = *(uint8_t*)ptr; ptr += 1; alac->setinfo_rice_initialhistory = *(uint8_t*)ptr; ptr += 1; alac->setinfo_rice_kmodifier = *(uint8_t*)ptr; ptr += 1; alac->setinfo_7f = *(uint8_t*)ptr; ptr += 1; alac->setinfo_80 = *(uint16_t*)ptr; if (!host_bigendian) _Swap16(alac->setinfo_80); ptr += 2; alac->setinfo_82 = *(uint32_t*)ptr; if (!host_bigendian) _Swap32(alac->setinfo_82); ptr += 4; alac->setinfo_86 = *(uint32_t*)ptr; if (!host_bigendian) _Swap32(alac->setinfo_86); ptr += 4; alac->setinfo_8a_rate = *(uint32_t*)ptr; if (!host_bigendian) _Swap32(alac->setinfo_8a_rate); ptr += 4; allocate_buffers(alac);}/* stream reading *//* supports reading 1 to 16 bits, in big endian format */static uint32_t readbits_16(alac_file *alac, int bits){ uint32_t result; int new_accumulator; result = (alac->input_buffer[0] << 16) | (alac->input_buffer[1] << 8) | (alac->input_buffer[2]); /* shift left by the number of bits we've already read, * so that the top 'n' bits of the 24 bits we read will * be the return bits */ result = result << alac->input_buffer_bitaccumulator; result = result & 0x00ffffff; /* and then only want the top 'n' bits from that, where * n is 'bits' */ result = result >> (24 - bits); new_accumulator = (alac->input_buffer_bitaccumulator + bits); /* increase the buffer pointer if we've read over n bytes. */ alac->input_buffer += (new_accumulator >> 3); /* and the remainder goes back into the bit accumulator */ alac->input_buffer_bitaccumulator = (new_accumulator & 7); return result;}/* supports reading 1 to 32 bits, in big endian format */static uint32_t readbits(alac_file *alac, int bits){ int32_t result = 0; if (bits > 16) { bits -= 16; result = readbits_16(alac, 16) << bits; } result |= readbits_16(alac, bits); return result;}/* reads a single bit */static int readbit(alac_file *alac){ int result; int new_accumulator; result = alac->input_buffer[0]; result = result << alac->input_buffer_bitaccumulator; result = result >> 7 & 1; new_accumulator = (alac->input_buffer_bitaccumulator + 1); alac->input_buffer += (new_accumulator / 8); alac->input_buffer_bitaccumulator = (new_accumulator % 8); return result;}static void unreadbits(alac_file *alac, int bits){ int new_accumulator = (alac->input_buffer_bitaccumulator - bits); alac->input_buffer += (new_accumulator >> 3); alac->input_buffer_bitaccumulator = (new_accumulator & 7); if (alac->input_buffer_bitaccumulator < 0) alac->input_buffer_bitaccumulator *= -1;}/* various implementations of count_leading_zero: * the first one is the original one, the simplest and most * obvious for what it's doing. never use this. * then there are the asm ones. fill in as necessary * and finally an unrolled and optimised c version * to fall back to */#if 0/* hideously inefficient. could use a bitmask search, * alternatively bsr on x86, */static int count_leading_zeros(int32_t input){ int i = 0; while (!(0x80000000 & input) && i < 32) { i++; input = input << 1; } return i;}#elif defined(__GNUC__) && (defined(_X86) || defined(__i386) || defined(i386))/* for some reason the unrolled version (below) is * actually faster than this. yay intel! */static int count_leading_zeros(int input){ int output = 0; if (!input) return 32; asm("bsr %1, %0\n" : "=r" (output) : "r" (input)); return (0x1f - output);}#elif defined(_MSC_VER) && defined(_M_IX86)static int count_leading_zeros(int input){ int output = 0; if (!input) return 32; __asm { mov eax, input; mov edx, 0x1f; bsr ecx, eax; sub edx, ecx; mov output, edx; } return output;}#else#warning using generic count leading zeroes. You may wish to write one for your CPU / compilerstatic int count_leading_zeros(int input){ int output = 0; int curbyte = 0; curbyte = input >> 24; if (curbyte) goto found; output += 8; curbyte = input >> 16; if (curbyte & 0xff) goto found; output += 8; curbyte = input >> 8; if (curbyte & 0xff) goto found; output += 8; curbyte = input; if (curbyte & 0xff) goto found; output += 8; return output;found: if (!(curbyte & 0xf0)) { output += 4; } else curbyte >>= 4; if (curbyte & 0x8) return output; if (curbyte & 0x4) return output + 1; if (curbyte & 0x2) return output + 2; if (curbyte & 0x1) return output + 3; /* shouldn't get here: */ return output + 4;}#endifvoid basterdised_rice_decompress(alac_file *alac, int32_t *output_buffer, int output_size, int readsamplesize, /* arg_10 */ int rice_initialhistory, /* arg424->b */ int rice_kmodifier, /* arg424->d */ int rice_historymult, /* arg424->c */ int rice_kmodifier_mask /* arg424->e */ ){ int output_count; unsigned int history = rice_initialhistory; int sign_modifier = 0; for (output_count = 0; output_count < output_size; output_count++) { int32_t x = 0; int32_t x_modified; int32_t final_val; /* read x - number of 1s before 0 represent the rice */ while (x <= 8 && readbit(alac)) { x++; } if (x > 8) /* RICE THRESHOLD */ { /* use alternative encoding */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -