Mercurial > mplayer.hg
annotate libmpcodecs/ad_dk3adpcm.c @ 7186:02b1976e12e2
fix vfm and add afm support
author | pontscho |
---|---|
date | Sat, 31 Aug 2002 09:45:59 +0000 |
parents | 28677d779205 |
children | 1eadce15446c |
rev | line source |
---|---|
5408 | 1 /* |
2 DK3 ADPCM Decoder for MPlayer | |
3 by Mike Melanson | |
4 | |
5 This file is responsible for decoding audio data encoded with | |
6 Duck Corp's DK3 ADPCM algorithm. Details about the data format | |
7 can be found here: | |
8 http://www.pcisys.net/~melanson/codecs/ | |
9 */ | |
10 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
11 #include <stdio.h> |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
12 #include <stdlib.h> |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
13 #include <unistd.h> |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
14 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
15 #include "config.h" |
5408 | 16 #include "bswap.h" |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
17 #include "ad_internal.h" |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
18 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
19 static ad_info_t info = |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
20 { |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
21 "Duck DK3 ADPCM decoder", |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
22 "dk3adpcm", |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
23 "Nick Kurshev", |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
24 "Mike Melanson", |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
25 "This format number was used by Duck Corp. but not officially registered with Microsoft" |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
26 }; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
27 |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
28 LIBAD_EXTERN(dk3adpcm) |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
29 |
5408 | 30 #define DK3_ADPCM_PREAMBLE_SIZE 16 |
31 | |
32 #define LE_16(x) (le2me_16(*(unsigned short *)(x))) | |
33 #define LE_32(x) (le2me_32(*(unsigned int *)(x))) | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
34 |
5408 | 35 // useful macros |
36 // clamp a number between 0 and 88 | |
37 #define CLAMP_0_TO_88(x) if (x < 0) x = 0; else if (x > 88) x = 88; | |
38 // clamp a number within a signed 16-bit range | |
39 #define CLAMP_S16(x) if (x < -32768) x = -32768; \ | |
40 else if (x > 32767) x = 32767; | |
41 // clamp a number above 16 | |
42 #define CLAMP_ABOVE_16(x) if (x < 16) x = 16; | |
43 // sign extend a 16-bit value | |
44 #define SE_16BIT(x) if (x & 0x8000) x -= 0x10000; | |
45 // sign extend a 4-bit value | |
46 #define SE_4BIT(x) if (x & 0x8) x -= 0x10; | |
47 | |
48 // pertinent tables | |
49 static int adpcm_step[89] = | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
50 { |
5408 | 51 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, |
52 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, | |
53 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, | |
54 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, | |
55 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, | |
56 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, | |
57 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, | |
58 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, | |
59 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 | |
60 }; | |
61 | |
62 static int adpcm_index[16] = | |
63 { | |
64 -1, -1, -1, -1, 2, 4, 6, 8, | |
65 -1, -1, -1, -1, 2, 4, 6, 8 | |
66 }; | |
67 | |
68 static int preinit(sh_audio_t *sh_audio) | |
69 { | |
70 sh_audio->audio_out_minsize = sh_audio->wf->nBlockAlign * 6; | |
71 sh_audio->ds->ss_div = | |
72 (sh_audio->wf->nBlockAlign - DK3_ADPCM_PREAMBLE_SIZE) * 8 / 3; | |
5458 | 73 sh_audio->audio_in_minsize= |
5408 | 74 sh_audio->ds->ss_mul = sh_audio->wf->nBlockAlign; |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
75 return 1; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
76 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
77 |
5408 | 78 static int init(sh_audio_t *sh_audio) |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
79 { |
5408 | 80 sh_audio->channels = sh_audio->wf->nChannels; |
81 sh_audio->samplerate = sh_audio->wf->nSamplesPerSec; | |
82 sh_audio->i_bps = | |
83 (sh_audio->ds->ss_mul * sh_audio->samplerate) / sh_audio->ds->ss_div; | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
84 return 1; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
85 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
86 |
5408 | 87 static void uninit(sh_audio_t *sh_audio) |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
88 { |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
89 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
90 |
5481 | 91 static int control(sh_audio_t *sh_audio,int cmd,void* arg, ...) |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
92 { |
5481 | 93 if(cmd==ADCTRL_SKIP_FRAME){ |
94 demux_read_data(sh_audio->ds, sh_audio->a_in_buffer,sh_audio->ds->ss_mul); | |
95 return CONTROL_TRUE; | |
96 } | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
97 return CONTROL_UNKNOWN; |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
98 } |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
99 |
5408 | 100 #define DK3_GET_NEXT_NIBBLE() \ |
101 if (decode_top_nibble_next) \ | |
102 { \ | |
103 nibble = (last_byte >> 4) & 0x0F; \ | |
104 decode_top_nibble_next = 0; \ | |
105 } \ | |
106 else \ | |
107 { \ | |
108 last_byte = input[in_ptr++]; \ | |
109 nibble = last_byte & 0x0F; \ | |
110 decode_top_nibble_next = 1; \ | |
111 } | |
112 | |
113 // note: This decoder assumes the format 0x62 data always comes in | |
114 // stereo flavor | |
115 static int dk3_adpcm_decode_block(unsigned short *output, unsigned char *input, | |
116 int block_size) | |
117 { | |
118 int sum_pred; | |
119 int diff_pred; | |
120 int sum_index; | |
121 int diff_index; | |
122 int diff_channel; | |
123 int in_ptr = 0x10; | |
124 int out_ptr = 0; | |
125 | |
126 unsigned char last_byte = 0; | |
127 unsigned char nibble; | |
128 int decode_top_nibble_next = 0; | |
129 | |
130 // ADPCM work variables | |
131 int sign; | |
132 int delta; | |
133 int step; | |
134 int diff; | |
135 | |
136 sum_pred = LE_16(&input[10]); | |
137 diff_pred = LE_16(&input[12]); | |
138 SE_16BIT(sum_pred); | |
139 SE_16BIT(diff_pred); | |
140 diff_channel = diff_pred; | |
141 sum_index = input[14]; | |
142 diff_index = input[15]; | |
143 | |
144 while (in_ptr < block_size) | |
145 // while (in_ptr < 2048) | |
146 { | |
147 // process the first predictor of the sum channel | |
148 DK3_GET_NEXT_NIBBLE(); | |
149 | |
150 step = adpcm_step[sum_index]; | |
151 | |
152 sign = nibble & 8; | |
153 delta = nibble & 7; | |
154 | |
155 diff = step >> 3; | |
156 if (delta & 4) diff += step; | |
157 if (delta & 2) diff += step >> 1; | |
158 if (delta & 1) diff += step >> 2; | |
159 | |
160 if (sign) | |
161 sum_pred -= diff; | |
162 else | |
163 sum_pred += diff; | |
164 | |
165 CLAMP_S16(sum_pred); | |
166 | |
167 sum_index += adpcm_index[nibble]; | |
168 CLAMP_0_TO_88(sum_index); | |
169 | |
170 // process the diff channel predictor | |
171 DK3_GET_NEXT_NIBBLE(); | |
172 | |
173 step = adpcm_step[diff_index]; | |
174 | |
175 sign = nibble & 8; | |
176 delta = nibble & 7; | |
177 | |
178 diff = step >> 3; | |
179 if (delta & 4) diff += step; | |
180 if (delta & 2) diff += step >> 1; | |
181 if (delta & 1) diff += step >> 2; | |
182 | |
183 if (sign) | |
184 diff_pred -= diff; | |
185 else | |
186 diff_pred += diff; | |
187 | |
188 CLAMP_S16(diff_pred); | |
189 | |
190 diff_index += adpcm_index[nibble]; | |
191 CLAMP_0_TO_88(diff_index); | |
192 | |
193 // output the first pair of stereo PCM samples | |
194 diff_channel = (diff_channel + diff_pred) / 2; | |
195 output[out_ptr++] = sum_pred + diff_channel; | |
196 output[out_ptr++] = sum_pred - diff_channel; | |
197 | |
198 // process the second predictor of the sum channel | |
199 DK3_GET_NEXT_NIBBLE(); | |
200 | |
201 step = adpcm_step[sum_index]; | |
202 | |
203 sign = nibble & 8; | |
204 delta = nibble & 7; | |
205 | |
206 diff = step >> 3; | |
207 if (delta & 4) diff += step; | |
208 if (delta & 2) diff += step >> 1; | |
209 if (delta & 1) diff += step >> 2; | |
210 | |
211 if (sign) | |
212 sum_pred -= diff; | |
213 else | |
214 sum_pred += diff; | |
215 | |
216 CLAMP_S16(sum_pred); | |
217 | |
218 sum_index += adpcm_index[nibble]; | |
219 CLAMP_0_TO_88(sum_index); | |
220 | |
221 // output the second pair of stereo PCM samples | |
222 output[out_ptr++] = sum_pred + diff_channel; | |
223 output[out_ptr++] = sum_pred - diff_channel; | |
224 } | |
225 | |
226 return out_ptr; | |
227 } | |
228 | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
229 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen) |
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
230 { |
5408 | 231 if (demux_read_data(sh_audio->ds, sh_audio->a_in_buffer, |
232 sh_audio->ds->ss_mul) != | |
233 sh_audio->ds->ss_mul) | |
234 return -1; /* EOF */ | |
235 | |
236 return 2 * dk3_adpcm_decode_block( | |
237 (unsigned short*)buf, sh_audio->a_in_buffer, | |
238 sh_audio->wf->nBlockAlign); | |
5340
0f12fb7c1c5d
imported from MPlayerXP, dlopen() hack removed, some bugs fixed, interface functions changed to static, info->author field added
arpi
parents:
diff
changeset
|
239 } |