3756
|
1 /*
|
|
2 Unified ADPCM Decoder for MPlayer
|
|
3
|
|
4 (C) 2001 Mike Melanson
|
|
5 */
|
|
6
|
|
7 #include "config.h"
|
|
8 #include "bswap.h"
|
|
9 #include "adpcm.h"
|
|
10
|
|
11 #define BE_16(x) (be2me_16(*(unsigned short *)(x)))
|
|
12 #define BE_32(x) (be2me_32(*(unsigned int *)(x)))
|
|
13 #define LE_16(x) (le2me_16(*(unsigned short *)(x)))
|
|
14 #define LE_32(x) (le2me_32(*(unsigned int *)(x)))
|
|
15
|
|
16 // clamp a number between 0 and 88
|
|
17 #define CLAMP_0_TO_88(x) if (x < 0) x = 0; else if (x > 88) x = 88;
|
|
18 // clamp a number within a signed 16-bit range
|
|
19 #define CLAMP_S16(x) if (x < -32768) x = -32768; \
|
|
20 else if (x > 32767) x = 32767;
|
|
21 // sign extend a 16-bit value
|
|
22 #define SE_16BIT(x) if (x & 0x8000) x -= 0x10000;
|
|
23
|
|
24 void ima_dvi_decode_nibbles(unsigned short *output, int channels,
|
|
25 int predictor_l, int index_l,
|
|
26 int predictor_r, int index_r)
|
|
27 {
|
|
28 int step[2];
|
|
29 int predictor[2];
|
|
30 int index[2];
|
|
31 int diff;
|
|
32 int i;
|
|
33 int sign;
|
|
34 int delta;
|
|
35 int channel_number = 0;
|
|
36
|
|
37 step[0] = adpcm_step[index_l];
|
|
38 step[1] = adpcm_step[index_r];
|
|
39 predictor[0] = predictor_l;
|
|
40 predictor[1] = predictor_r;
|
|
41 index[0] = index_l;
|
|
42 index[1] = index_r;
|
|
43
|
|
44 for (i = 0; i < IMA_ADPCM_SAMPLES_PER_BLOCK * channels; i++)
|
|
45 {
|
|
46 delta = output[i];
|
|
47
|
|
48 index[channel_number] += adpcm_index[delta];
|
|
49 CLAMP_0_TO_88(index[channel_number]);
|
|
50
|
|
51 sign = delta & 8;
|
|
52 delta = delta & 7;
|
|
53
|
|
54 diff = step[channel_number] >> 3;
|
|
55 if (delta & 4) diff += step[channel_number];
|
|
56 if (delta & 2) diff += step[channel_number] >> 1;
|
|
57 if (delta & 1) diff += step[channel_number] >> 2;
|
|
58
|
|
59 if (sign)
|
|
60 predictor[channel_number] -= diff;
|
|
61 else
|
|
62 predictor[channel_number] += diff;
|
|
63
|
|
64 CLAMP_S16(predictor[channel_number]);
|
|
65 output[i] = predictor[channel_number];
|
|
66 step[channel_number] = adpcm_step[index[channel_number]];
|
|
67
|
|
68 // toggle channel
|
|
69 channel_number ^= channels - 1;
|
|
70 }
|
|
71 }
|
|
72
|
|
73 int ima_adpcm_decode_block(unsigned short *output, unsigned char *input,
|
|
74 int channels)
|
|
75 {
|
|
76 int initial_predictor_l = 0;
|
|
77 int initial_predictor_r = 0;
|
|
78 int initial_index_l = 0;
|
|
79 int initial_index_r = 0;
|
|
80 int i;
|
|
81
|
3763
|
82 initial_predictor_l = BE_16(&input[0]);
|
3756
|
83 initial_index_l = initial_predictor_l;
|
|
84
|
|
85 // mask, sign-extend, and clamp the predictor portion
|
|
86 initial_predictor_l &= 0xFF80;
|
|
87 SE_16BIT(initial_predictor_l);
|
|
88 CLAMP_S16(initial_predictor_l);
|
|
89
|
|
90 // mask and clamp the index portion
|
|
91 initial_index_l &= 0x7F;
|
|
92 CLAMP_0_TO_88(initial_index_l);
|
|
93
|
|
94 // handle stereo
|
|
95 if (channels > 1)
|
|
96 {
|
3763
|
97 initial_predictor_r = BE_16(&input[IMA_ADPCM_BLOCK_SIZE]);
|
3756
|
98 initial_index_r = initial_predictor_r;
|
|
99
|
|
100 // mask, sign-extend, and clamp the predictor portion
|
|
101 initial_predictor_r &= 0xFF80;
|
|
102 SE_16BIT(initial_predictor_r);
|
|
103 CLAMP_S16(initial_predictor_r);
|
|
104
|
|
105 // mask and clamp the index portion
|
|
106 initial_index_r &= 0x7F;
|
|
107 CLAMP_0_TO_88(initial_index_r);
|
|
108 }
|
|
109
|
|
110 // break apart all of the nibbles in the block
|
|
111 if (channels == 1)
|
|
112 for (i = 0; i < IMA_ADPCM_SAMPLES_PER_BLOCK / 2; i++)
|
|
113 {
|
3763
|
114 output[i * 2 + 0] = input[2 + i] & 0x0F;
|
|
115 output[i * 2 + 1] = input[2 + i] >> 4;
|
3756
|
116 }
|
|
117 else
|
|
118 for (i = 0; i < IMA_ADPCM_SAMPLES_PER_BLOCK / 2 * 2; i++)
|
|
119 {
|
3763
|
120 output[i * 4 + 0] = input[2 + i] & 0x0F;
|
|
121 output[i * 4 + 1] = input[2 + IMA_ADPCM_BLOCK_SIZE + i] & 0x0F;
|
|
122 output[i * 4 + 2] = input[2 + i] >> 4;
|
|
123 output[i * 4 + 3] = input[2 + IMA_ADPCM_BLOCK_SIZE + i] >> 4;
|
3756
|
124 }
|
|
125
|
|
126 ima_dvi_decode_nibbles(output, channels,
|
|
127 initial_predictor_l, initial_index_l,
|
|
128 initial_predictor_r, initial_index_r);
|
|
129
|
|
130 return IMA_ADPCM_SAMPLES_PER_BLOCK * channels;
|
|
131 }
|