📄 pcmdecode.c
字号:
fwrite(buf, 1, *length, ofp); } #endif DPRINTF((M_TEXT("convert %d \n"), *length)); if (undownmixed_number_of_channels > 2) { obuf = downmix_to_stereo((short *)buf, length); memcpy(buf, obuf, *length); } if (sampling_rate_conversion_factor) { obuf = convert_sampling_rate((short *)buf, length); memcpy(buf, obuf, *length); } DPRINTF((M_TEXT(" %d \n"), *length)); #if 1 { static FILE *ofp = NULL; if (ofp == NULL) ofp = fopen("\\temp\\dumpwavconv.pcm", "wb"); if (ofp != NULL) fwrite(buf, 1, *length, ofp); } #endif}void close_pcm_decoders(void){ DPRINTF(("close_pcm_decoders() calling close_src\n")); close_src(); DPRINTF(("close_pcm_decoders() calling close_downmix\n")); close_downmix();}#else void close_pcm_decoders(void) { } #define do_pcm_conversions(buf, length) #endif /* ] */static int big_endian;static void flip_byte_order16(unsigned short *obuf, unsigned short *ibuf, int size){ int ii; unsigned short temp; unsigned short tempi; size >>= 1; /* because the size was based on bytes, not words */ for (ii = 0; ii < size; ii++) { temp = tempi = ibuf[ii]; tempi >>= 8; tempi |= (temp << 8); obuf[ii] = tempi; }}short alaw2short[] ={ -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736, -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784, -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368, -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,-22016,-20992,-24064,-23040,-17920,-16896,-19968,-18944,-30208,-29184,-32256,-31232,-26112,-25088,-28160,-27136,-11008,-10496,-12032,-11520, -8960, -8448, -9984, -9472,-15104,-14592,-16128,-15616,-13056,-12544,-14080,-13568, -344, -328, -376, -360, -280, -264, -312, -296, -472, -456, -504, -488, -408, -392, -440, -424, -88, -72, -120, -104, -24, -8, -56, -40, -216, -200, -248, -232, -152, -136, -184, -168, -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184, -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696, -688, -656, -752, -720, -560, -528, -624, -592, -944, -912, -1008, -976, -816, -784, -880, -848, 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736, 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784, 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368, 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392, 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944, 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136, 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472, 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568, 344, 328, 376, 360, 280, 264, 312, 296, 472, 456, 504, 488, 408, 392, 440, 424, 88, 72, 120, 104, 24, 8, 56, 40, 216, 200, 248, 232, 152, 136, 184, 168, 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184, 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696, 688, 656, 752, 720, 560, 528, 624, 592, 944, 912, 1008, 976, 816, 784, 880, 848};short ulaw2short[] ={-32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,-23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,-15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,-11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316, -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140, -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004, -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436, -1372, -1308, -1244, -1180, -1116, -1052, -988, -924, -876, -844, -812, -780, -748, -716, -684, -652, -620, -588, -556, -524, -492, -460, -428, -396, -372, -356, -340, -324, -308, -292, -276, -260, -244, -228, -212, -196, -180, -164, -148, -132, -120, -112, -104, -96, -88, -80, -72, -64, -56, -48, -40, -32, -24, -16, -8, 0, 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956, 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764, 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412, 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140, 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004, 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436, 1372, 1308, 1244, 1180, 1116, 1052, 988, 924, 876, 844, 812, 780, 748, 716, 684, 652, 620, 588, 556, 524, 492, 460, 428, 396, 372, 356, 340, 324, 308, 292, 276, 260, 244, 228, 212, 196, 180, 164, 148, 132, 120, 112, 104, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0};#define NUM_RATES 24static unsigned int rates_exfloat[NUM_RATES][8] = { { 192000, 0x40, 0x10, 0xbb, 0x80, 0x00, 0x00, 0x00, }, { 176400, 0x40, 0x10, 0xac, 0x44, 0x00, 0x00, 0x00, }, { 131072, 0x40, 0x10, 0x80, 0x00, 0x00, 0x00, 0x00, }, { 128000, 0x40, 0x0f, 0xfa, 0x00, 0x00, 0x00, 0x00, }, { 96000, 0x40, 0x0f, 0xbb, 0x80, 0x00, 0x00, 0x00, }, { 88200, 0x40, 0x0f, 0xac, 0x44, 0x00, 0x00, 0x00, }, { 65536, 0x40, 0x0f, 0x80, 0x00, 0x00, 0x00, 0x00, }, { 64000, 0x40, 0x0e, 0xfa, 0x00, 0x00, 0x00, 0x00, }, { 49152, 0x40, 0x0e, 0xc0, 0x00, 0x00, 0x00, 0x00, }, { 48000, 0x40, 0x0e, 0xbb, 0x80, 0x00, 0x00, 0x00, }, { 44100, 0x40, 0x0e, 0xac, 0x44, 0x00, 0x00, 0x00, }, { 32768, 0x40, 0x0e, 0x80, 0x00, 0x00, 0x00, 0x00, }, { 32000, 0x40, 0x0d, 0xfa, 0x00, 0x00, 0x00, 0x00, }, { 24000, 0x40, 0x0d, 0xbb, 0x80, 0x00, 0x00, 0x00, }, { 22050, 0x40, 0x0d, 0xac, 0x44, 0x00, 0x00, 0x00, }, { 16384, 0x40, 0x0d, 0x80, 0x00, 0x00, 0x00, 0x00, }, { 16000, 0x40, 0x0c, 0xfa, 0x00, 0x00, 0x00, 0x00, }, { 12000, 0x40, 0x0c, 0xbb, 0x80, 0x00, 0x00, 0x00, }, { 11025, 0x40, 0x0c, 0xac, 0x44, 0x00, 0x00, 0x00, }, { 8000, 0x40, 0x0b, 0xfa, 0x00, 0x00, 0x00, 0x00, }, { 6000, 0x40, 0x0b, 0xbb, 0x80, 0x00, 0x00, 0x00, }, { 5000, 0x40, 0x0b, 0x9c, 0x40, 0x00, 0x00, 0x00, }, { 4000, 0x40, 0x0a, 0xfa, 0x00, 0x00, 0x00, 0x00, }, { 1000, 0x40, 0x08, 0xfa, 0x00, 0x00, 0x00, 0x00, },};unsigned int ConvertFromIeeeExtended(unsigned char *bytes){ unsigned int ii; for (ii = 0; ii < NUM_RATES; ii++) { if ((rates_exfloat[ii][1] == bytes[0]) && (rates_exfloat[ii][2] == bytes[1]) && (rates_exfloat[ii][3] == bytes[2]) && (rates_exfloat[ii][4] == bytes[3]) && (rates_exfloat[ii][5] == bytes[4]) && (rates_exfloat[ii][6] == bytes[5]) && (rates_exfloat[ii][7] == bytes[6])) return rates_exfloat[ii][0]; } return 0;}int mac_gobble(char *str){ int sequence_length; sequence_length = (unsigned int)(*str++) << 24; sequence_length |= (unsigned int)(*str++) << 16; sequence_length |= (unsigned int)(*str++) << 8; sequence_length |= (unsigned int)(*str++); return sequence_length;}#define MS_IMA_ADPCM_PREAMBLE_SIZE 4#define QT_IMA_ADPCM_PREAMBLE_SIZE 2#define QT_IMA_ADPCM_BLOCK_SIZE 0x22#define QT_IMA_ADPCM_SAMPLES_PER_BLOCK 64#define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)#if 1#define be2me_16(x) bswap_16(x)#define le2me_16(x) (x)#else#define be2me_16(x) (x)#define le2me_16(x) bswap_16(x)#endif#define BE_16(x) (be2me_16(*(unsigned short *)(x)))#define LE_16(x) (le2me_16(*(unsigned short *)(x)))#define MS_ADPCM_PREAMBLE_SIZE 6#define LESS_OR_EQ_16(x) ((x)[0]+(256*((x)[1])))#define CLAMP_0_TO_88(x) if (x < 0) x = 0; else if (x > 88) x = 88;#define SATURATE_SHORT(x) if (x < -32768) x = -32768; else if (x > 32767) x = 32767;#define CLAMP_ABOVE_16(x) if (x < 16) x = 16;#define SIGN_EXTEND_16BIT(x) if (x & 0x8000) x -= 0x10000;#define SIGN_EXTEND_4BIT(x) if (x & 0x8) x -= 0x10;// pertinent tables for IMA ADPCMstatic int adpcm_step[89] ={ 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767};static int adpcm_index[16] ={ -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8};static void decode_nibbles(unsigned short *output, int output_size, int channels, int predictor_l, int index_l, int predictor_r, int index_r){ int step[2]; int predictor[2]; int index[2]; int diff; int i; int sign; int delta; int channel_number = 0; step[0] = adpcm_step[index_l]; step[1] = adpcm_step[index_r]; predictor[0] = predictor_l; predictor[1] = predictor_r; index[0] = index_l; index[1] = index_r; for (i = 0; i < output_size; i++) { delta = output[i]; index[channel_number] += adpcm_index[delta]; CLAMP_0_TO_88(index[channel_number]); sign = delta & 8; delta = delta & 7; diff = step[channel_number] >> 3; if (delta & 4) diff += step[channel_number]; if (delta & 2) diff += step[channel_number] >> 1; if (delta & 1) diff += step[channel_number] >> 2; if (sign) predictor[channel_number] -= diff; else predictor[channel_number] += diff; SATURATE_SHORT(predictor[channel_number]); output[i] = predictor[channel_number]; step[channel_number] = adpcm_step[index[channel_number]]; // toggle channel channel_number ^= channels - 1; }}static int qt_ima_adpcm_decode_block(unsigned short *output, unsigned char *input, int channels){ int initial_predictor_l = 0; int initial_predictor_r = 0; int initial_index_l = 0; int initial_index_r = 0; int i; initial_predictor_l = BE_16(&input[0]); initial_index_l = initial_predictor_l; // mask, sign-extend, and clamp the predictor portion initial_predictor_l &= 0xFF80; SIGN_EXTEND_16BIT(initial_predictor_l); SATURATE_SHORT(initial_predictor_l); // mask and clamp the index portion initial_index_l &= 0x7F; CLAMP_0_TO_88(initial_index_l); // handle stereo if (channels > 1) { initial_predictor_r = BE_16(&input[QT_IMA_ADPCM_BLOCK_SIZE]); initial_index_r = initial_predictor_r; // mask, sign-extend, and clamp the predictor portion initial_predictor_r &= 0xFF80; SIGN_EXTEND_16BIT(initial_predictor_r); SATURATE_SHORT(initial_predictor_r); // mask and clamp the index portion initial_index_r &= 0x7F; CLAMP_0_TO_88(initial_index_r); } // break apart all of the nibbles in the block if (channels == 1) for (i = 0; i < QT_IMA_ADPCM_SAMPLES_PER_BLOCK / 2; i++) { output[i * 2 + 0] = input[2 + i] & 0x0F; output[i * 2 + 1] = input[2 + i] >> 4; } else for (i = 0; i < QT_IMA_ADPCM_SAMPLES_PER_BLOCK / 2 * 2; i++) { output[i * 4 + 0] = input[2 + i] & 0x0F; output[i * 4 + 1] = input[2 + QT_IMA_ADPCM_BLOCK_SIZE + i] & 0x0F; output[i * 4 + 2] = input[2 + i] >> 4; output[i * 4 + 3] = input[2 + QT_IMA_ADPCM_BLOCK_SIZE + i] >> 4; } decode_nibbles(output, QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels, channels, initial_predictor_l, initial_index_l, initial_predictor_r, initial_index_r); return QT_IMA_ADPCM_SAMPLES_PER_BLOCK * channels;}static int ms_ima_adpcm_decode_block(unsigned short *output, unsigned char *input, int channels, int block_size){ int predictor_l = 0; int predictor_r = 0; int index_l = 0; int index_r = 0; int i; int channel_counter; int channel_index; int channel_index_l; int channel_index_r; predictor_l = LE_16(&input[0]); SIGN_EXTEND_16BIT(predictor_l); index_l = input[2]; if (channels == 2) { predictor_r = LE_16(&input[4]); SIGN_EXTEND_16BIT(predictor_r); index_r = input[6]; } if (channels == 1) for (i = 0; i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels); i++) { output[i * 2 + 0] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] & 0x0F; output[i * 2 + 1] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] >> 4; } else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -